From 64d328285a45d85462cbbf2c740c9f5d7865e04c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Fri, 15 May 2020 16:26:39 -0400
Subject: [PATCH] sipcall: lock when starting/stopping media

Change-Id: Ia12da6f9e58ec0cd0100eaba206e1d9e711ca0e1
---
 src/sip/sipcall.cpp | 3 ++-
 src/sip/sipcall.h   | 3 +--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/sip/sipcall.cpp b/src/sip/sipcall.cpp
index cf541fc855..c4e1c578f9 100644
--- a/src/sip/sipcall.cpp
+++ b/src/sip/sipcall.cpp
@@ -929,7 +929,6 @@ SIPCall::getAudioCodec() const
 void
 SIPCall::startAllMedia()
 {
-    std::lock_guard<std::recursive_mutex> lock(callMutex_);
     if (!transport_) return;
     JAMI_WARN("[call:%s] startAllMedia()", getCallId().c_str());
     if (isSecure() && not transport_->isSecure()) {
@@ -1135,6 +1134,7 @@ SIPCall::onMediaUpdate()
     // to a negotiated transport.
     runOnMainThread([w = weak()] {
         if (auto this_ = w.lock()) {
+            std::lock_guard<std::recursive_mutex> lk {this_->callMutex_};
             // The call is already ended, so we don't need to restart medias
             if (!this_->inv or this_->inv->state == PJSIP_INV_STATE_DISCONNECTED) return;
             // If ICE is not used, start medias now
@@ -1185,6 +1185,7 @@ SIPCall::waitForIceAndStartMedia()
             // Negotiation waiting task
             Manager::instance().addTask([weak_call] {
                 if (auto call = weak_call.lock()) {
+                    std::lock_guard<std::recursive_mutex> lk {call->callMutex_};
                     auto ice = call->getIceMediaTransport();
 
                     if (not ice or ice->isFailed()) {
diff --git a/src/sip/sipcall.h b/src/sip/sipcall.h
index 167323e329..9e2cd2b273 100644
--- a/src/sip/sipcall.h
+++ b/src/sip/sipcall.h
@@ -222,8 +222,6 @@ public: // NOT SIP RELATED (good candidates to be moved elsewhere)
 
     void generateMediaPorts();
 
-    void startAllMedia();
-
     void openPortsUPnP();
 
     void setPeerRegistredName(const std::string& name) {
@@ -256,6 +254,7 @@ private:
 
     void waitForIceAndStartMedia();
 
+    void startAllMedia();
     void stopAllMedia();
 
     /**
-- 
GitLab