LCOV - code coverage report
Current view: top level - foo/src/sip - sipcall.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 1409 1982 71.1 %
Date: 2025-08-24 09:11:10 Functions: 161 212 75.9 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2025 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  This program is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       7             :  *  (at your option) any later version.
       8             :  *
       9             :  *  This program is distributed in the hope that it will be useful,
      10             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "call_factory.h"
      19             : #include "sip/sipcall.h"
      20             : #include "sip/sipaccount.h"
      21             : #include "sip/sipaccountbase.h"
      22             : #include "sip/sipvoiplink.h"
      23             : #include "jamidht/jamiaccount.h"
      24             : #include "logger.h"
      25             : #include "sdp.h"
      26             : #include "manager.h"
      27             : #include "string_utils.h"
      28             : 
      29             : #include "connectivity/sip_utils.h"
      30             : #include "audio/audio_rtp_session.h"
      31             : #include "system_codec_container.h"
      32             : #include "im/instant_messaging.h"
      33             : #include "jami/account_const.h"
      34             : #include "jami/call_const.h"
      35             : #include "jami/media_const.h"
      36             : #include "client/ring_signal.h"
      37             : #include "pjsip-ua/sip_inv.h"
      38             : 
      39             : #ifdef ENABLE_PLUGIN
      40             : #include "plugin/jamipluginmanager.h"
      41             : #endif
      42             : 
      43             : #ifdef ENABLE_VIDEO
      44             : #include "client/videomanager.h"
      45             : #include "video/video_rtp_session.h"
      46             : #include "jami/videomanager_interface.h"
      47             : #include <chrono>
      48             : #include <libavutil/display.h>
      49             : #include <video/sinkclient.h>
      50             : #include "media/video/video_mixer.h"
      51             : #endif
      52             : #include "audio/ringbufferpool.h"
      53             : #include "jamidht/channeled_transport.h"
      54             : 
      55             : #include "errno.h"
      56             : 
      57             : #include <dhtnet/upnp/upnp_control.h>
      58             : #include <dhtnet/ice_transport_factory.h>
      59             : 
      60             : #include <opendht/crypto.h>
      61             : #include <opendht/thread_pool.h>
      62             : #include <fmt/ranges.h>
      63             : 
      64             : #include "tracepoint.h"
      65             : 
      66             : #include "media/media_decoder.h"
      67             : 
      68             : namespace jami {
      69             : 
      70             : using sip_utils::CONST_PJ_STR;
      71             : using namespace libjami::Call;
      72             : 
      73             : #ifdef ENABLE_VIDEO
      74             : static DeviceParams
      75         315 : getVideoSettings()
      76             : {
      77         315 :     if (auto videomon = jami::getVideoDeviceMonitor())
      78         630 :         return videomon->getDeviceParams(videomon->getDefaultDevice());
      79           0 :     return DeviceParams {};
      80             : }
      81             : #endif
      82             : 
      83             : static constexpr std::chrono::seconds DEFAULT_ICE_INIT_TIMEOUT {35}; // seconds
      84             : static constexpr std::chrono::milliseconds EXPECTED_ICE_INIT_MAX_TIME {5000};
      85             : static constexpr std::chrono::seconds DEFAULT_ICE_NEGO_TIMEOUT {60}; // seconds
      86             : static constexpr std::chrono::milliseconds MS_BETWEEN_2_KEYFRAME_REQUEST {1000};
      87             : static constexpr int ICE_COMP_ID_RTP {1};
      88             : static constexpr int ICE_COMP_COUNT_PER_STREAM {2};
      89             : static constexpr auto MULTISTREAM_REQUIRED_VERSION_STR = "10.0.2"sv;
      90             : static const std::vector<unsigned> MULTISTREAM_REQUIRED_VERSION
      91             :     = split_string_to_unsigned(MULTISTREAM_REQUIRED_VERSION_STR, '.');
      92             : static constexpr auto MULTIICE_REQUIRED_VERSION_STR = "13.3.0"sv;
      93             : static const std::vector<unsigned> MULTIICE_REQUIRED_VERSION
      94             :     = split_string_to_unsigned(MULTIICE_REQUIRED_VERSION_STR, '.');
      95             : static constexpr auto NEW_CONFPROTOCOL_VERSION_STR = "13.1.0"sv;
      96             : static const std::vector<unsigned> NEW_CONFPROTOCOL_VERSION
      97             :     = split_string_to_unsigned(NEW_CONFPROTOCOL_VERSION_STR, '.');
      98             : static constexpr auto REUSE_ICE_IN_REINVITE_REQUIRED_VERSION_STR = "11.0.2"sv;
      99             : static const std::vector<unsigned> REUSE_ICE_IN_REINVITE_REQUIRED_VERSION
     100             :     = split_string_to_unsigned(REUSE_ICE_IN_REINVITE_REQUIRED_VERSION_STR, '.');
     101             : static constexpr auto MULTIAUDIO_REQUIRED_VERSION_STR = "13.11.0"sv;
     102             : static const std::vector<unsigned> MULTIAUDIO_REQUIRED_VERSION
     103             :     = split_string_to_unsigned(MULTIAUDIO_REQUIRED_VERSION_STR, '.');
     104             : 
     105         395 : SIPCall::SIPCall(const std::shared_ptr<SIPAccountBase>& account,
     106             :                  const std::string& callId,
     107             :                  Call::CallType type,
     108         395 :                  const std::vector<libjami::MediaMap>& mediaList)
     109             :     : Call(account, callId, type)
     110         395 :     , sdp_(new Sdp(callId))
     111         395 :     , enableIce_(account->isIceForMediaEnabled())
     112        1185 :     , srtpEnabled_(account->isSrtpEnabled())
     113             : {
     114             :     jami_tracepoint(call_start, callId.c_str());
     115             : 
     116         395 :     if (account->getUPnPActive())
     117           0 :         upnp_ = std::make_shared<dhtnet::upnp::Controller>(Manager::instance().upnpContext());
     118             : 
     119         395 :     setCallMediaLocal();
     120             : 
     121             :     // Set the media caps.
     122         790 :     sdp_->setLocalMediaCapabilities(MediaType::MEDIA_AUDIO,
     123         790 :                                     account->getActiveAccountCodecInfoList(MEDIA_AUDIO));
     124             : #ifdef ENABLE_VIDEO
     125         790 :     sdp_->setLocalMediaCapabilities(MediaType::MEDIA_VIDEO,
     126         790 :                                     account->getActiveAccountCodecInfoList(MEDIA_VIDEO));
     127             : #endif
     128             : 
     129         395 :     auto mediaAttrList = MediaAttribute::buildMediaAttributesList(mediaList, isSrtpEnabled());
     130             : 
     131         395 :     if (mediaAttrList.size() == 0) {
     132           0 :         if (type_ == Call::CallType::INCOMING) {
     133             :             // Handle incoming call without media offer.
     134           0 :             JAMI_WARN(
     135             :                 "[call:%s] No media offered in the incoming invite. An offer will be provided in "
     136             :                 "the answer",
     137             :                 getCallId().c_str());
     138           0 :             mediaAttrList = getSIPAccount()->createDefaultMediaList(false,
     139           0 :                                                                     getState() == CallState::HOLD);
     140             :         } else {
     141           0 :             JAMI_WARN("[call:%s] Creating an outgoing call with empty offer", getCallId().c_str());
     142             :         }
     143             :     }
     144             : 
     145        1185 :     JAMI_DEBUG("[call:{:s}] Create a new [{:s}] SIP call with {:d} media",
     146             :                getCallId(),
     147             :                type == Call::CallType::INCOMING
     148             :                    ? "INCOMING"
     149             :                    : (type == Call::CallType::OUTGOING ? "OUTGOING" : "MISSED"),
     150             :                mediaList.size());
     151             : 
     152         395 :     initMediaStreams(mediaAttrList);
     153         395 : }
     154             : 
     155         395 : SIPCall::~SIPCall()
     156             : {
     157         395 :     std::lock_guard lk {callMutex_};
     158             : 
     159         395 :     setSipTransport({});
     160         395 :     setInviteSession(); // prevents callback usage
     161             : 
     162             : #ifdef ENABLE_VIDEO
     163         395 :     closeMediaPlayer(mediaPlayerId_);
     164             : #endif
     165         395 : }
     166             : 
     167             : int
     168         687 : SIPCall::findRtpStreamIndex(const std::string& label) const
     169             : {
     170         687 :     const auto iter = std::find_if(rtpStreams_.begin(),
     171             :                                    rtpStreams_.end(),
     172        2168 :                                    [&label](const RtpStream& rtp) {
     173        1084 :                                        return label == rtp.mediaAttribute_->label_;
     174             :                                    });
     175             : 
     176             :     // Return the index if there is a match.
     177         687 :     if (iter != rtpStreams_.end())
     178         677 :         return std::distance(rtpStreams_.begin(), iter);
     179             : 
     180             :     // No match found.
     181          10 :     return -1;
     182             : }
     183             : 
     184             : void
     185         712 : SIPCall::createRtpSession(RtpStream& stream)
     186             : {
     187         712 :     if (not stream.mediaAttribute_)
     188           0 :         throw std::runtime_error("Missing media attribute");
     189             : 
     190             :     // To get audio_0 ; video_0
     191         712 :     auto streamId = sip_utils::streamId(id_, stream.mediaAttribute_->label_);
     192         712 :     if (stream.mediaAttribute_->type_ == MediaType::MEDIA_AUDIO) {
     193         397 :         stream.rtpSession_ = std::make_shared<AudioRtpSession>(id_, streamId, recorder_);
     194             :     }
     195             : #ifdef ENABLE_VIDEO
     196         315 :     else if (stream.mediaAttribute_->type_ == MediaType::MEDIA_VIDEO) {
     197         630 :         stream.rtpSession_ = std::make_shared<video::VideoRtpSession>(id_,
     198             :                                                                       streamId,
     199         315 :                                                                       getVideoSettings(),
     200         630 :                                                                       recorder_);
     201         315 :         std::static_pointer_cast<video::VideoRtpSession>(stream.rtpSession_)->setRotation(rotation_);
     202             :     }
     203             : #endif
     204             :     else {
     205           0 :         throw std::runtime_error("Unsupported media type");
     206             :     }
     207             : 
     208             :     // Must be valid at this point.
     209         712 :     if (not stream.rtpSession_)
     210           0 :         throw std::runtime_error("Failed to create RTP session");
     211             :     ;
     212         712 : }
     213             : 
     214             : void
     215         340 : SIPCall::configureRtpSession(const std::shared_ptr<RtpSession>& rtpSession,
     216             :                              const std::shared_ptr<MediaAttribute>& mediaAttr,
     217             :                              const MediaDescription& localMedia,
     218             :                              const MediaDescription& remoteMedia)
     219             : {
     220         340 :     JAMI_DBG("[call:%s] Configuring [%s] RTP session",
     221             :              getCallId().c_str(),
     222             :              MediaAttribute::mediaTypeToString(mediaAttr->type_));
     223             : 
     224         340 :     if (not rtpSession)
     225           0 :         throw std::runtime_error("Must have a valid RTP session");
     226             : 
     227             :     // Configure the media stream
     228         340 :     auto new_mtu = sipTransport_->getTlsMtu();
     229         340 :     rtpSession->setMtu(new_mtu);
     230         340 :     rtpSession->updateMedia(remoteMedia, localMedia);
     231             : 
     232             :     // Mute/un-mute media
     233         340 :     if (mediaAttr->muted_) {
     234          11 :         rtpSession->setMuted(true);
     235             :         // TODO. Setting mute to true should be enough to mute.
     236             :         // Kept for backward compatiblity.
     237          11 :         rtpSession->setMediaSource("");
     238             :     } else {
     239         329 :         rtpSession->setMuted(false);
     240         329 :         rtpSession->setMediaSource(mediaAttr->sourceUri_);
     241             :     }
     242             : 
     243         340 :     rtpSession->setSuccessfulSetupCb([w = weak()](MediaType, bool) {
     244             :         // This sends SIP messages on socket, so move to io
     245         525 :         dht::ThreadPool::io().run([w = std::move(w)] {
     246         525 :             if (auto thisPtr = w.lock())
     247         525 :                 thisPtr->rtpSetupSuccess();
     248         525 :         });
     249         525 :     });
     250             : 
     251         340 :     if (localMedia.type == MediaType::MEDIA_AUDIO) {
     252         190 :         setupVoiceCallback(rtpSession);
     253             :     }
     254             : 
     255             : #ifdef ENABLE_VIDEO
     256         340 :     if (localMedia.type == MediaType::MEDIA_VIDEO) {
     257         150 :         auto videoRtp = std::dynamic_pointer_cast<video::VideoRtpSession>(rtpSession);
     258         150 :         assert(videoRtp && mediaAttr);
     259         150 :         auto streamIdx = findRtpStreamIndex(mediaAttr->label_);
     260         150 :         videoRtp->setRequestKeyFrameCallback([w = weak(), streamIdx] {
     261             :             // This sends SIP messages on socket, so move to I/O
     262           0 :             dht::ThreadPool::io().run([w = std::move(w), streamIdx] {
     263           0 :                 if (auto thisPtr = w.lock())
     264           0 :                     thisPtr->requestKeyframe(streamIdx);
     265           0 :             });
     266           0 :         });
     267         150 :         videoRtp->setChangeOrientationCallback([w = weak(), streamIdx](int angle) {
     268             :             // This sends SIP messages on socket, so move to I/O
     269          49 :             dht::ThreadPool::io().run([w, angle, streamIdx] {
     270          49 :                 if (auto thisPtr = w.lock())
     271          49 :                     thisPtr->setVideoOrientation(streamIdx, angle);
     272          49 :             });
     273          49 :         });
     274         150 :     }
     275             : #endif
     276         340 : }
     277             : 
     278             : void
     279         190 : SIPCall::setupVoiceCallback(const std::shared_ptr<RtpSession>& rtpSession)
     280             : {
     281             :     // need to downcast to access setVoiceCallback
     282         190 :     auto audioRtp = std::dynamic_pointer_cast<AudioRtpSession>(rtpSession);
     283             : 
     284         190 :     audioRtp->setVoiceCallback([w = weak()](bool voice) {
     285             :         // this is called whenever voice is detected on the local audio
     286             : 
     287           0 :         runOnMainThread([w, voice] {
     288           0 :             if (auto thisPtr = w.lock()) {
     289             :                 // TODO: once we support multiple streams, change this to the right one
     290           0 :                 std::string streamId = "";
     291             : 
     292             : #ifdef ENABLE_VIDEO
     293           0 :                 if (auto videoManager = Manager::instance().getVideoManager()) {
     294           0 :                     if (not videoManager->videoDeviceMonitor.getDeviceList().empty()) {
     295             :                         // if we have a video device
     296           0 :                         streamId = sip_utils::streamId("", sip_utils::DEFAULT_VIDEO_STREAMID);
     297             :                     }
     298             :                 }
     299             : #endif
     300             : 
     301             :                 // send our local voice activity
     302           0 :                 if (auto conference = thisPtr->conf_.lock()) {
     303             :                     // we are in a conference
     304             : 
     305             :                     // updates conference info and sends it to others via ConfInfo
     306             :                     // (only if there was a change)
     307             :                     // also emits signal with updated conference info
     308           0 :                     conference->setVoiceActivity(streamId, voice);
     309             :                 } else {
     310             :                     // we are in a one-to-one call
     311             :                     // send voice activity over SIP
     312             :                     // TODO: change the streamID once multiple streams are supported
     313           0 :                     thisPtr->sendVoiceActivity("-1", voice);
     314             : 
     315             :                     // TODO: maybe emit signal here for local voice activity
     316           0 :                 }
     317           0 :             } else {
     318           0 :                 JAMI_ERR("Voice activity callback unable to lock weak ptr to SIPCall");
     319           0 :             }
     320           0 :         });
     321           0 :     });
     322         190 : }
     323             : 
     324             : std::shared_ptr<SIPAccountBase>
     325        1943 : SIPCall::getSIPAccount() const
     326             : {
     327        1943 :     return std::static_pointer_cast<SIPAccountBase>(getAccount().lock());
     328             : }
     329             : 
     330             : #ifdef ENABLE_PLUGIN
     331             : void
     332         255 : SIPCall::createCallAVStreams()
     333             : {
     334             : #ifdef ENABLE_VIDEO
     335         433 :     for (const auto& videoRtp : getRtpSessionList(MediaType::MEDIA_VIDEO)) {
     336         209 :         if (std::static_pointer_cast<video::VideoRtpSession>(videoRtp)->hasConference()) {
     337          31 :             clearCallAVStreams();
     338          31 :             return;
     339             :         }
     340         255 :     }
     341             : #endif
     342             : 
     343         224 :     auto baseId = getCallId();
     344           0 :     auto mediaMap = [](const std::shared_ptr<jami::MediaFrame>& m) -> AVFrame* {
     345           0 :         return m->pointer();
     346             :     };
     347             : 
     348         630 :     for (const auto& rtpSession : getRtpSessionList()) {
     349         406 :         auto isVideo = rtpSession->getMediaType() == MediaType::MEDIA_VIDEO;
     350         406 :         auto streamType = isVideo ? StreamType::video : StreamType::audio;
     351         406 :         StreamData previewStreamData {baseId, false, streamType, getPeerNumber(), getAccountId()};
     352         406 :         StreamData receiveStreamData {baseId, true, streamType, getPeerNumber(), getAccountId()};
     353             : #ifdef ENABLE_VIDEO
     354         406 :         if (isVideo) {
     355             :             // Preview
     356         178 :             auto videoRtp = std::static_pointer_cast<video::VideoRtpSession>(rtpSession);
     357         178 :             if (auto& videoPreview = videoRtp->getVideoLocal())
     358          14 :                 createCallAVStream(previewStreamData,
     359          14 :                                    *videoPreview,
     360          28 :                                    std::make_shared<MediaStreamSubject>(mediaMap));
     361             :             // Receive
     362         178 :             if (auto& videoReceive = videoRtp->getVideoReceive())
     363          59 :                 createCallAVStream(receiveStreamData,
     364          59 :                                    *videoReceive,
     365         118 :                                    std::make_shared<MediaStreamSubject>(mediaMap));
     366         178 :         } else {
     367             : #endif
     368         228 :             auto audioRtp = std::static_pointer_cast<AudioRtpSession>(rtpSession);
     369             :             // Preview
     370         228 :             if (auto& localAudio = audioRtp->getAudioLocal())
     371          52 :                 createCallAVStream(previewStreamData,
     372          52 :                                    *localAudio,
     373         104 :                                    std::make_shared<MediaStreamSubject>(mediaMap));
     374             :             // Receive
     375         228 :             if (auto& audioReceive = audioRtp->getAudioReceive())
     376          51 :                 createCallAVStream(receiveStreamData,
     377          51 :                                    (AVMediaStream&) *audioReceive,
     378         102 :                                    std::make_shared<MediaStreamSubject>(mediaMap));
     379             : #ifdef ENABLE_VIDEO
     380         228 :         }
     381             : #endif
     382         630 :     }
     383         224 : }
     384             : 
     385             : void
     386         176 : SIPCall::createCallAVStream(const StreamData& StreamData,
     387             :                             AVMediaStream& streamSource,
     388             :                             const std::shared_ptr<MediaStreamSubject>& mediaStreamSubject)
     389             : {
     390         352 :     const std::string AVStreamId = StreamData.id + std::to_string(static_cast<int>(StreamData.type))
     391         352 :                                    + std::to_string(StreamData.direction);
     392         176 :     std::lock_guard lk(avStreamsMtx_);
     393         176 :     auto it = callAVStreams.find(AVStreamId);
     394         176 :     if (it != callAVStreams.end())
     395           0 :         return;
     396         176 :     it = callAVStreams.insert(it, {AVStreamId, mediaStreamSubject});
     397         176 :     streamSource.attachPriorityObserver(it->second);
     398         176 :     jami::Manager::instance()
     399         176 :         .getJamiPluginManager()
     400         176 :         .getCallServicesManager()
     401         176 :         .createAVSubject(StreamData, it->second);
     402         176 : }
     403             : 
     404             : void
     405         545 : SIPCall::clearCallAVStreams()
     406             : {
     407         545 :     std::lock_guard lk(avStreamsMtx_);
     408         545 :     callAVStreams.clear();
     409         545 : }
     410             : #endif // ENABLE_PLUGIN
     411             : 
     412             : void
     413         395 : SIPCall::setCallMediaLocal()
     414             : {
     415         395 :     if (localAudioPort_ == 0
     416             : #ifdef ENABLE_VIDEO
     417           0 :         || localVideoPort_ == 0
     418             : #endif
     419             :     )
     420         395 :         generateMediaPorts();
     421         395 : }
     422             : 
     423             : void
     424         424 : SIPCall::generateMediaPorts()
     425             : {
     426         424 :     auto account = getSIPAccount();
     427         424 :     if (!account) {
     428           0 :         JAMI_ERR("No account detected");
     429           0 :         return;
     430             :     }
     431             : 
     432             :     // TODO. Setting specfic range for RTP ports is obsolete, in
     433             :     // particular in the context of ICE.
     434             : 
     435             :     // Reference: http://www.cs.columbia.edu/~hgs/rtp/faq.html#ports
     436             :     // We only want to set ports to new values if they haven't been set
     437         424 :     const unsigned callLocalAudioPort = account->generateAudioPort();
     438         424 :     if (localAudioPort_ != 0)
     439          29 :         account->releasePort(localAudioPort_);
     440         424 :     localAudioPort_ = callLocalAudioPort;
     441         848 :     sdp_->setLocalPublishedAudioPorts(callLocalAudioPort,
     442         424 :                                       rtcpMuxEnabled_ ? 0 : callLocalAudioPort + 1);
     443             : 
     444             : #ifdef ENABLE_VIDEO
     445             :     // https://projects.savoirfairelinux.com/issues/17498
     446         424 :     const unsigned int callLocalVideoPort = account->generateVideoPort();
     447         424 :     if (localVideoPort_ != 0)
     448          29 :         account->releasePort(localVideoPort_);
     449             :     // this should already be guaranteed by SIPAccount
     450         424 :     assert(localAudioPort_ != callLocalVideoPort);
     451         424 :     localVideoPort_ = callLocalVideoPort;
     452         848 :     sdp_->setLocalPublishedVideoPorts(callLocalVideoPort,
     453         424 :                                       rtcpMuxEnabled_ ? 0 : callLocalVideoPort + 1);
     454             : #endif
     455         424 : }
     456             : 
     457             : const std::string&
     458         204 : SIPCall::getContactHeader() const
     459             : {
     460         204 :     return contactHeader_;
     461             : }
     462             : 
     463             : void
     464        1095 : SIPCall::setSipTransport(const std::shared_ptr<SipTransport>& transport,
     465             :                          const std::string& contactHdr)
     466             : {
     467        1095 :     if (transport != sipTransport_) {
     468         584 :         JAMI_DBG("[call:%s] Setting transport to [%p]", getCallId().c_str(), transport.get());
     469             :     }
     470             : 
     471        1095 :     sipTransport_ = transport;
     472        1095 :     contactHeader_ = contactHdr;
     473             : 
     474        1095 :     if (not transport) {
     475             :         // Done.
     476         803 :         return;
     477             :     }
     478             : 
     479         292 :     if (contactHeader_.empty()) {
     480           0 :         JAMI_WARN("[call:%s] Contact header is empty", getCallId().c_str());
     481             :     }
     482             : 
     483         292 :     if (isSrtpEnabled() and not sipTransport_->isSecure()) {
     484          18 :         JAMI_WARN("[call:%s] Crypto (SRTP) is negotiated over an unencrypted signaling channel",
     485             :                   getCallId().c_str());
     486             :     }
     487             : 
     488         292 :     if (not isSrtpEnabled() and sipTransport_->isSecure()) {
     489           0 :         JAMI_WARN("[call:%s] The signaling channel is encrypted but the media is unencrypted",
     490             :                   getCallId().c_str());
     491             :     }
     492             : 
     493         292 :     const auto list_id = reinterpret_cast<uintptr_t>(this);
     494         292 :     sipTransport_->removeStateListener(list_id);
     495             : 
     496             :     // Listen for transport destruction
     497         292 :     sipTransport_->addStateListener(
     498         128 :         list_id, [wthis_ = weak()](pjsip_transport_state state, const pjsip_transport_state_info*) {
     499         128 :             if (auto this_ = wthis_.lock()) {
     500          17 :                 JAMI_DBG("[call:%s] SIP transport state [%i] - connection state [%u]",
     501             :                          this_->getCallId().c_str(),
     502             :                          state,
     503             :                          static_cast<unsigned>(this_->getConnectionState()));
     504             : 
     505             :                 // End the call if the SIP transport was shut down
     506          17 :                 auto isAlive = SipTransport::isAlive(state);
     507          17 :                 if (not isAlive and this_->getConnectionState() != ConnectionState::DISCONNECTED) {
     508           6 :                     JAMI_WARN("[call:%s] Ending call because underlying SIP transport was closed",
     509             :                               this_->getCallId().c_str());
     510           6 :                     this_->stopAllMedia();
     511           6 :                     this_->detachAudioFromConference();
     512           6 :                     this_->onFailure(ECONNRESET);
     513             :                 }
     514         128 :             }
     515         128 :         });
     516             : }
     517             : 
     518             : void
     519          15 : SIPCall::requestReinvite(const std::vector<MediaAttribute>& mediaAttrList, bool needNewIce)
     520             : {
     521          15 :     JAMI_DBG("[call:%s] Sending a SIP re-invite to request media change", getCallId().c_str());
     522             : 
     523          15 :     if (isWaitingForIceAndMedia_) {
     524           0 :         remainingRequest_ = Request::SwitchInput;
     525             :     } else {
     526          15 :         if (SIPSessionReinvite(mediaAttrList, needNewIce) == PJ_SUCCESS and reinvIceMedia_) {
     527           8 :             isWaitingForIceAndMedia_ = true;
     528             :         }
     529             :     }
     530          15 : }
     531             : 
     532             : /**
     533             :  * Send a reINVITE inside an active dialog to modify its state
     534             :  * Local SDP session should be modified before calling this method
     535             :  */
     536             : int
     537          30 : SIPCall::SIPSessionReinvite(const std::vector<MediaAttribute>& mediaAttrList, bool needNewIce)
     538             : {
     539          30 :     assert(not mediaAttrList.empty());
     540             : 
     541          30 :     std::lock_guard lk {callMutex_};
     542             : 
     543             :     // Do nothing if no invitation processed yet
     544          30 :     if (not inviteSession_ or inviteSession_->invite_tsx)
     545           1 :         return PJ_SUCCESS;
     546             : 
     547          29 :     JAMI_DBG("[call:%s] Preparing and sending a re-invite (state=%s)",
     548             :              getCallId().c_str(),
     549             :              pjsip_inv_state_name(inviteSession_->state));
     550          29 :     JAMI_DBG("[call:%s] New ICE required for this re-invite: [%s]",
     551             :              getCallId().c_str(),
     552             :              needNewIce ? "Yes" : "No");
     553             : 
     554             :     // Generate new ports to receive the new media stream
     555             :     // LibAV doesn't discriminate SSRCs and will be confused about Seq changes on a given port
     556          29 :     generateMediaPorts();
     557             : 
     558          29 :     sdp_->clearIce();
     559          29 :     sdp_->setActiveRemoteSdpSession(nullptr);
     560          29 :     sdp_->setActiveLocalSdpSession(nullptr);
     561             : 
     562          29 :     auto acc = getSIPAccount();
     563          29 :     if (not acc) {
     564           0 :         JAMI_ERR("No account detected");
     565           0 :         return !PJ_SUCCESS;
     566             :     }
     567             : 
     568          29 :     if (not sdp_->createOffer(mediaAttrList))
     569           0 :         return !PJ_SUCCESS;
     570             : 
     571          29 :     if (isIceEnabled() and needNewIce) {
     572          14 :         if (not createIceMediaTransport(true) or not initIceMediaTransport(true)) {
     573           0 :             return !PJ_SUCCESS;
     574             :         }
     575          14 :         addLocalIceAttributes();
     576             :         // Media transport changed, must restart the media.
     577          14 :         mediaRestartRequired_ = true;
     578             :     }
     579             : 
     580             :     pjsip_tx_data* tdata;
     581          29 :     auto local_sdp = sdp_->getLocalSdpSession();
     582          29 :     auto result = pjsip_inv_reinvite(inviteSession_.get(), nullptr, local_sdp, &tdata);
     583          29 :     if (result == PJ_SUCCESS) {
     584          28 :         if (!tdata)
     585           0 :             return PJ_SUCCESS;
     586             : 
     587             :         // Add user-agent header
     588          28 :         sip_utils::addUserAgentHeader(acc->getUserAgentName(), tdata);
     589             : 
     590          28 :         result = pjsip_inv_send_msg(inviteSession_.get(), tdata);
     591          28 :         if (result == PJ_SUCCESS)
     592          28 :             return PJ_SUCCESS;
     593           0 :         JAMI_ERR("[call:%s] Failed to send REINVITE msg (pjsip: %s)",
     594             :                  getCallId().c_str(),
     595             :                  sip_utils::sip_strerror(result).c_str());
     596             :         // Canceling internals without sending (anyways the send has just failed!)
     597           0 :         pjsip_inv_cancel_reinvite(inviteSession_.get(), &tdata);
     598             :     } else
     599           1 :         JAMI_ERR("[call:%s] Failed to create REINVITE msg (pjsip: %s)",
     600             :                  getCallId().c_str(),
     601             :                  sip_utils::sip_strerror(result).c_str());
     602             : 
     603           1 :     return !PJ_SUCCESS;
     604          30 : }
     605             : 
     606             : int
     607           8 : SIPCall::SIPSessionReinvite()
     608             : {
     609           8 :     auto mediaList = getMediaAttributeList();
     610          16 :     return SIPSessionReinvite(mediaList, isNewIceMediaRequired(mediaList));
     611           8 : }
     612             : 
     613             : void
     614         223 : SIPCall::sendSIPInfo(std::string_view body, std::string_view subtype)
     615             : {
     616         223 :     std::lock_guard lk {callMutex_};
     617         223 :     if (not inviteSession_ or not inviteSession_->dlg)
     618           1 :         throw VoipLinkException("Unable to get invite dialog");
     619             : 
     620         222 :     constexpr pj_str_t methodName = CONST_PJ_STR("INFO");
     621         222 :     constexpr pj_str_t type = CONST_PJ_STR("application");
     622             : 
     623             :     pjsip_method method;
     624         222 :     pjsip_method_init_np(&method, (pj_str_t*) &methodName);
     625             : 
     626             :     /* Create request message. */
     627             :     pjsip_tx_data* tdata;
     628         222 :     if (pjsip_dlg_create_request(inviteSession_->dlg, &method, -1, &tdata) != PJ_SUCCESS) {
     629           0 :         JAMI_ERR("[call:%s] Unable to create dialog", getCallId().c_str());
     630           0 :         return;
     631             :     }
     632             : 
     633             :     /* Create "application/<subtype>" message body. */
     634         222 :     pj_str_t content = CONST_PJ_STR(body);
     635         222 :     pj_str_t pj_subtype = CONST_PJ_STR(subtype);
     636         222 :     tdata->msg->body = pjsip_msg_body_create(tdata->pool, &type, &pj_subtype, &content);
     637         222 :     if (tdata->msg->body == NULL)
     638           0 :         pjsip_tx_data_dec_ref(tdata);
     639             :     else
     640         222 :         pjsip_dlg_send_request(inviteSession_->dlg,
     641             :                                tdata,
     642         222 :                                Manager::instance().sipVoIPLink().getModId(),
     643             :                                NULL);
     644         223 : }
     645             : 
     646             : void
     647          16 : SIPCall::updateRecState(bool state)
     648             : {
     649             :     std::string BODY = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
     650             :                        "<media_control><vc_primitive><to_encoder>"
     651             :                        "<recording_state="
     652          32 :                        + std::to_string(state)
     653             :                        + "/>"
     654          16 :                          "</to_encoder></vc_primitive></media_control>";
     655             :     // see https://tools.ietf.org/html/rfc5168 for XML Schema for Media Control details
     656             : 
     657          16 :     JAMI_DBG("Sending recording state via SIP INFO");
     658             : 
     659             :     try {
     660          16 :         sendSIPInfo(BODY, "media_control+xml");
     661           0 :     } catch (const std::exception& e) {
     662           0 :         JAMI_ERR("Error sending recording state: %s", e.what());
     663           0 :     }
     664          16 : }
     665             : 
     666             : void
     667         154 : SIPCall::requestKeyframe(int streamIdx)
     668             : {
     669         154 :     auto now = clock::now();
     670         154 :     if ((now - lastKeyFrameReq_) < MS_BETWEEN_2_KEYFRAME_REQUEST
     671         154 :         and lastKeyFrameReq_ != time_point::min())
     672           1 :         return;
     673             : 
     674         153 :     std::string streamIdPart;
     675         153 :     if (streamIdx != -1)
     676         153 :         streamIdPart = fmt::format("<stream_id>{}</stream_id>", streamIdx);
     677             :     std::string BODY = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
     678             :                        "<media_control><vc_primitive> "
     679         306 :                        + streamIdPart + "<to_encoder>"
     680             :                        + "<picture_fast_update/>"
     681         153 :                          "</to_encoder></vc_primitive></media_control>";
     682         153 :     JAMI_DBG("Sending video keyframe request via SIP INFO");
     683             :     try {
     684         153 :         sendSIPInfo(BODY, "media_control+xml");
     685           1 :     } catch (const std::exception& e) {
     686           1 :         JAMI_ERR("Error sending video keyframe request: %s", e.what());
     687           1 :     }
     688         153 :     lastKeyFrameReq_ = now;
     689         153 : }
     690             : 
     691             : void
     692           5 : SIPCall::sendMuteState(bool state)
     693             : {
     694             :     std::string BODY = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
     695             :                        "<media_control><vc_primitive><to_encoder>"
     696             :                        "<mute_state="
     697          10 :                        + std::to_string(state)
     698             :                        + "/>"
     699           5 :                          "</to_encoder></vc_primitive></media_control>";
     700             :     // see https://tools.ietf.org/html/rfc5168 for XML Schema for Media Control details
     701             : 
     702           5 :     JAMI_DBG("Sending mute state via SIP INFO");
     703             : 
     704             :     try {
     705           5 :         sendSIPInfo(BODY, "media_control+xml");
     706           0 :     } catch (const std::exception& e) {
     707           0 :         JAMI_ERR("Error sending mute state: %s", e.what());
     708           0 :     }
     709           5 : }
     710             : 
     711             : void
     712           0 : SIPCall::sendVoiceActivity(std::string_view streamId, bool state)
     713             : {
     714             :     // dont send streamId if it's -1
     715           0 :     std::string streamIdPart = "";
     716           0 :     if (streamId != "-1" && !streamId.empty()) {
     717           0 :         streamIdPart = fmt::format("<stream_id>{}</stream_id>", streamId);
     718             :     }
     719             : 
     720             :     std::string BODY = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
     721             :                        "<media_control><vc_primitive>"
     722           0 :                        + streamIdPart
     723           0 :                        + "<to_encoder>"
     724             :                          "<voice_activity="
     725           0 :                        + std::to_string(state)
     726             :                        + "/>"
     727           0 :                          "</to_encoder></vc_primitive></media_control>";
     728             : 
     729             :     try {
     730           0 :         sendSIPInfo(BODY, "media_control+xml");
     731           0 :     } catch (const std::exception& e) {
     732           0 :         JAMI_ERR("Error sending voice activity state: %s", e.what());
     733           0 :     }
     734           0 : }
     735             : 
     736             : void
     737        1225 : SIPCall::setInviteSession(pjsip_inv_session* inviteSession)
     738             : {
     739        1225 :     std::lock_guard lk {callMutex_};
     740             : 
     741        1225 :     if (inviteSession == nullptr and inviteSession_) {
     742         214 :         JAMI_DBG("[call:%s] Delete current invite session", getCallId().c_str());
     743        1011 :     } else if (inviteSession != nullptr) {
     744             :         // NOTE: The first reference of the invite session is owned by pjsip. If
     745             :         // that counter goes down to zero the invite will be destroyed, and the
     746             :         // unique_ptr will point freed datas.  To avoid this, we increment the
     747             :         // ref counter and let our unique_ptr share the ownership of the session
     748             :         // with pjsip.
     749         214 :         if (PJ_SUCCESS != pjsip_inv_add_ref(inviteSession)) {
     750           0 :             JAMI_WARN("[call:%s] Attempting to set invalid invite session [%p]",
     751             :                       getCallId().c_str(),
     752             :                       inviteSession);
     753           0 :             inviteSession_.reset(nullptr);
     754           0 :             return;
     755             :         }
     756         214 :         JAMI_DBG("[call:%s] Set new invite session [%p]", getCallId().c_str(), inviteSession);
     757             :     } else {
     758             :         // Nothing to do.
     759         797 :         return;
     760             :     }
     761             : 
     762         428 :     inviteSession_.reset(inviteSession);
     763        1225 : }
     764             : 
     765             : void
     766         208 : SIPCall::terminateSipSession(int status)
     767             : {
     768         208 :     JAMI_DBG("[call:%s] Terminate SIP session", getCallId().c_str());
     769         208 :     std::lock_guard lk {callMutex_};
     770         208 :     if (inviteSession_ and inviteSession_->state != PJSIP_INV_STATE_DISCONNECTED) {
     771         105 :         pjsip_tx_data* tdata = nullptr;
     772         105 :         auto ret = pjsip_inv_end_session(inviteSession_.get(), status, nullptr, &tdata);
     773         105 :         if (ret == PJ_SUCCESS) {
     774         105 :             if (tdata) {
     775         105 :                 auto account = getSIPAccount();
     776         105 :                 if (account) {
     777         105 :                     sip_utils::addContactHeader(contactHeader_, tdata);
     778             :                     // Add user-agent header
     779         105 :                     sip_utils::addUserAgentHeader(account->getUserAgentName(), tdata);
     780             :                 } else {
     781           0 :                     JAMI_ERR("No account detected");
     782           0 :                     throw std::runtime_error(fmt::format("[call:{}] The account owning this call is invalid", getCallId()));
     783             :                 }
     784             : 
     785         105 :                 ret = pjsip_inv_send_msg(inviteSession_.get(), tdata);
     786         105 :                 if (ret != PJ_SUCCESS)
     787           0 :                     JAMI_ERR("[call:%s] Failed to send terminate msg, SIP error (%s)",
     788             :                              getCallId().c_str(),
     789             :                              sip_utils::sip_strerror(ret).c_str());
     790         105 :             }
     791             :         } else
     792           0 :             JAMI_ERR("[call:%s] Failed to terminate INVITE@%p, SIP error (%s)",
     793             :                      getCallId().c_str(),
     794             :                      inviteSession_.get(),
     795             :                      sip_utils::sip_strerror(ret).c_str());
     796             :     }
     797         208 :     setInviteSession();
     798         208 : }
     799             : 
     800             : void
     801           0 : SIPCall::answer()
     802             : {
     803           0 :     std::lock_guard lk {callMutex_};
     804           0 :     auto account = getSIPAccount();
     805           0 :     if (!account) {
     806           0 :         JAMI_ERR("No account detected");
     807           0 :         return;
     808             :     }
     809             : 
     810           0 :     if (not inviteSession_)
     811           0 :         throw VoipLinkException("[call:" + getCallId()
     812           0 :                                 + "] Answer: no invite session for this call");
     813             : 
     814           0 :     if (!inviteSession_->neg) {
     815           0 :         JAMI_WARN("[call:%s] Negotiator is NULL, INVITE received without an SDP",
     816             :                   getCallId().c_str());
     817             : 
     818           0 :         Manager::instance().sipVoIPLink().createSDPOffer(inviteSession_.get());
     819             :     }
     820             : 
     821             :     pjsip_tx_data* tdata;
     822           0 :     if (!inviteSession_->last_answer)
     823           0 :         throw std::runtime_error("Should only be called for initial answer");
     824             : 
     825             :     // answer with SDP if no SDP was given in initial invite (i.e. inv->neg is NULL)
     826           0 :     if (pjsip_inv_answer(inviteSession_.get(),
     827             :                          PJSIP_SC_OK,
     828             :                          NULL,
     829           0 :                          !inviteSession_->neg ? sdp_->getLocalSdpSession() : NULL,
     830             :                          &tdata)
     831           0 :         != PJ_SUCCESS)
     832           0 :         throw std::runtime_error("Unable to init invite request answer (200 OK)");
     833             : 
     834           0 :     if (contactHeader_.empty()) {
     835           0 :         throw std::runtime_error("Unable to answer with an invalid contact header");
     836             :     }
     837             : 
     838           0 :     JAMI_DBG("[call:%s] Answering with contact header: %s",
     839             :              getCallId().c_str(),
     840             :              contactHeader_.c_str());
     841             : 
     842           0 :     sip_utils::addContactHeader(contactHeader_, tdata);
     843             : 
     844             :     // Add user-agent header
     845           0 :     sip_utils::addUserAgentHeader(account->getUserAgentName(), tdata);
     846             : 
     847           0 :     if (pjsip_inv_send_msg(inviteSession_.get(), tdata) != PJ_SUCCESS) {
     848           0 :         setInviteSession();
     849           0 :         throw std::runtime_error("Unable to send invite request answer (200 OK)");
     850             :     }
     851             : 
     852           0 :     setState(CallState::ACTIVE, ConnectionState::CONNECTED);
     853           0 : }
     854             : 
     855             : void
     856          97 : SIPCall::answer(const std::vector<libjami::MediaMap>& mediaList)
     857             : {
     858          97 :     std::lock_guard lk {callMutex_};
     859          97 :     auto account = getSIPAccount();
     860          97 :     if (not account) {
     861           0 :         JAMI_ERR("No account detected");
     862           0 :         return;
     863             :     }
     864             : 
     865          97 :     if (not inviteSession_) {
     866           0 :         JAMI_ERR("[call:%s] No invite session for this call", getCallId().c_str());
     867           0 :         return;
     868             :     }
     869             : 
     870          97 :     if (not sdp_) {
     871           0 :         JAMI_ERR("[call:%s] No SDP session for this call", getCallId().c_str());
     872           0 :         return;
     873             :     }
     874             : 
     875          97 :     auto newMediaAttrList = MediaAttribute::buildMediaAttributesList(mediaList, isSrtpEnabled());
     876             : 
     877          97 :     if (newMediaAttrList.empty() and rtpStreams_.empty()) {
     878           0 :         JAMI_ERR("[call:%s] Media list must not be empty!", getCallId().c_str());
     879           0 :         return;
     880             :     }
     881             : 
     882             :     // If the media list is empty, use the current media (this could happen
     883             :     // with auto-answer for instance), otherwise update the current media.
     884          97 :     if (newMediaAttrList.empty()) {
     885          68 :         JAMI_DBG("[call:%s] Media list is empty, using current media", getCallId().c_str());
     886          29 :     } else if (newMediaAttrList.size() != rtpStreams_.size()) {
     887             :         // This should never happen, as we make sure that the sizes match earlier
     888             :         // in handleIncomingConversationCall.
     889           0 :         JAMI_ERROR("[call:{:s}] Media list size {:d} in answer does not match. Expected {:d}",
     890             :                    getCallId(),
     891             :                    newMediaAttrList.size(),
     892             :                    rtpStreams_.size());
     893           0 :         return;
     894             :     }
     895             : 
     896          97 :     auto const& mediaAttrList = newMediaAttrList.empty() ? getMediaAttributeList()
     897          97 :                                                          : newMediaAttrList;
     898             : 
     899          97 :     JAMI_DBG("[call:%s] Answering incoming call with following media:", getCallId().c_str());
     900         270 :     for (size_t idx = 0; idx < mediaAttrList.size(); idx++) {
     901         173 :         auto const& mediaAttr = mediaAttrList.at(idx);
     902         519 :         JAMI_DEBUG("[call:{:s}] Media @{:d} - {:s}", getCallId(), idx, mediaAttr.toString(true));
     903             :     }
     904             : 
     905             :     // Apply the media attributes.
     906         270 :     for (size_t idx = 0; idx < mediaAttrList.size(); idx++) {
     907         173 :         updateMediaStream(mediaAttrList[idx], idx);
     908             :     }
     909             : 
     910             :     // Create the SDP answer
     911          97 :     sdp_->processIncomingOffer(mediaAttrList);
     912             : 
     913          97 :     if (isIceEnabled() and remoteHasValidIceAttributes()) {
     914          95 :         setupIceResponse();
     915             :     }
     916             : 
     917          97 :     if (not inviteSession_->neg) {
     918             :         // We are answering to an INVITE that did not include a media offer (SDP).
     919             :         // The SIP specification (RFCs 3261/6337) requires that if a UA wants to
     920             :         // proceed with the call, it must provide a media offer (SDP) if the initial
     921             :         // INVITE did not offer one. In this case, the SDP offer will be included in
     922             :         // the SIP OK (200) answer. The peer UA will then include its SDP answer in
     923             :         // the SIP ACK message.
     924             : 
     925             :         // TODO. This code should be unified with the code used by accounts to create
     926             :         // SDP offers.
     927             : 
     928           0 :         JAMI_WARN("[call:%s] No negotiator session, peer sent an empty INVITE (without SDP)",
     929             :                   getCallId().c_str());
     930             : 
     931           0 :         Manager::instance().sipVoIPLink().createSDPOffer(inviteSession_.get());
     932             : 
     933           0 :         generateMediaPorts();
     934             : 
     935             :         // Setup and create ICE offer
     936           0 :         if (isIceEnabled()) {
     937           0 :             sdp_->clearIce();
     938           0 :             sdp_->setActiveRemoteSdpSession(nullptr);
     939           0 :             sdp_->setActiveLocalSdpSession(nullptr);
     940             : 
     941           0 :             auto opts = account->getIceOptions();
     942             : 
     943           0 :             auto publicAddr = account->getPublishedIpAddress();
     944             : 
     945           0 :             if (publicAddr) {
     946           0 :                 opts.accountPublicAddr = publicAddr;
     947           0 :                 if (auto interfaceAddr = dhtnet::ip_utils::getInterfaceAddr(account->getLocalInterface(),
     948           0 :                                                                     publicAddr.getFamily())) {
     949           0 :                     opts.accountLocalAddr = interfaceAddr;
     950           0 :                     if (createIceMediaTransport(false)
     951           0 :                         and initIceMediaTransport(true, std::move(opts))) {
     952           0 :                         addLocalIceAttributes();
     953             :                     }
     954             :                 } else {
     955           0 :                     JAMI_WARN("[call:%s] Unable to init ICE transport, missing local address",
     956             :                               getCallId().c_str());
     957             :                 }
     958             :             } else {
     959           0 :                 JAMI_WARN("[call:%s] Unable to init ICE transport, missing public address",
     960             :                           getCallId().c_str());
     961             :             }
     962           0 :         }
     963             :     }
     964             : 
     965          97 :     if (!inviteSession_->last_answer)
     966           0 :         throw std::runtime_error("Should only be called for initial answer");
     967             : 
     968             :     // Set the SIP final answer (200 OK).
     969             :     pjsip_tx_data* tdata;
     970          97 :     if (pjsip_inv_answer(inviteSession_.get(), PJSIP_SC_OK, NULL, sdp_->getLocalSdpSession(), &tdata)
     971          97 :         != PJ_SUCCESS)
     972           0 :         throw std::runtime_error("Unable to init invite request answer (200 OK)");
     973             : 
     974          97 :     if (contactHeader_.empty()) {
     975           0 :         throw std::runtime_error("Unable to answer with an invalid contact header");
     976             :     }
     977             : 
     978          97 :     JAMI_DBG("[call:%s] Answering with contact header: %s",
     979             :              getCallId().c_str(),
     980             :              contactHeader_.c_str());
     981             : 
     982          97 :     sip_utils::addContactHeader(contactHeader_, tdata);
     983             : 
     984             :     // Add user-agent header
     985          97 :     sip_utils::addUserAgentHeader(account->getUserAgentName(), tdata);
     986             : 
     987          97 :     if (pjsip_inv_send_msg(inviteSession_.get(), tdata) != PJ_SUCCESS) {
     988           0 :         setInviteSession();
     989           0 :         throw std::runtime_error("Unable to send invite request answer (200 OK)");
     990             :     }
     991             : 
     992          97 :     setState(CallState::ACTIVE, ConnectionState::CONNECTED);
     993          97 : }
     994             : 
     995             : void
     996          28 : SIPCall::answerMediaChangeRequest(const std::vector<libjami::MediaMap>& mediaList, bool isRemote)
     997             : {
     998          28 :     std::lock_guard lk {callMutex_};
     999             : 
    1000          28 :     auto account = getSIPAccount();
    1001          28 :     if (not account) {
    1002           0 :         JAMI_ERR("[call:%s] No account detected", getCallId().c_str());
    1003           0 :         return;
    1004             :     }
    1005             : 
    1006          28 :     auto mediaAttrList = MediaAttribute::buildMediaAttributesList(mediaList, isSrtpEnabled());
    1007             : 
    1008             :     // TODO. is the right place?
    1009             :     // Disable video if disabled in the account.
    1010          28 :     if (not account->isVideoEnabled()) {
    1011           0 :         for (auto& mediaAttr : mediaAttrList) {
    1012           0 :             if (mediaAttr.type_ == MediaType::MEDIA_VIDEO) {
    1013           0 :                 mediaAttr.enabled_ = false;
    1014             :             }
    1015             :         }
    1016             :     }
    1017             : 
    1018          28 :     if (mediaAttrList.empty()) {
    1019           0 :         JAMI_WARN("[call:%s] Media list is empty. Ignoring the media change request",
    1020             :                   getCallId().c_str());
    1021           0 :         return;
    1022             :     }
    1023             : 
    1024          28 :     if (not sdp_) {
    1025           0 :         JAMI_ERR("[call:%s] No valid SDP session", getCallId().c_str());
    1026           0 :         return;
    1027             :     }
    1028             : 
    1029          28 :     JAMI_DBG("[call:%s] Current media", getCallId().c_str());
    1030          28 :     unsigned idx = 0;
    1031          79 :     for (auto const& rtp : rtpStreams_) {
    1032          51 :         JAMI_DBG("[call:%s] Media @%u: %s",
    1033             :                  getCallId().c_str(),
    1034             :                  idx++,
    1035             :                  rtp.mediaAttribute_->toString(true).c_str());
    1036             :     }
    1037             : 
    1038          28 :     JAMI_DBG("[call:%s] Answering to media change request with new media", getCallId().c_str());
    1039          28 :     idx = 0;
    1040          83 :     for (auto const& newMediaAttr : mediaAttrList) {
    1041          55 :         JAMI_DBG("[call:%s] Media @%u: %s",
    1042             :                  getCallId().c_str(),
    1043             :                  idx++,
    1044             :                  newMediaAttr.toString(true).c_str());
    1045             :     }
    1046             : 
    1047          28 :     if (!updateAllMediaStreams(mediaAttrList, isRemote))
    1048           0 :         return;
    1049             : 
    1050          28 :     if (not sdp_->processIncomingOffer(mediaAttrList)) {
    1051           0 :         JAMI_WARN("[call:%s] Unable to process the new offer, ignoring", getCallId().c_str());
    1052           0 :         return;
    1053             :     }
    1054             : 
    1055          28 :     if (not sdp_->getRemoteSdpSession()) {
    1056           0 :         JAMI_ERR("[call:%s] No valid remote SDP session", getCallId().c_str());
    1057           0 :         return;
    1058             :     }
    1059             : 
    1060          28 :     if (isIceEnabled() and remoteHasValidIceAttributes()) {
    1061          13 :         JAMI_WARN("[call:%s] Requesting a new ICE media", getCallId().c_str());
    1062          13 :         setupIceResponse(true);
    1063             :     }
    1064             : 
    1065          28 :     if (not sdp_->startNegotiation()) {
    1066           0 :         JAMI_ERR("[call:%s] Unable to start media negotiation for a re-invite request",
    1067             :                  getCallId().c_str());
    1068           0 :         return;
    1069             :     }
    1070             : 
    1071          28 :     if (pjsip_inv_set_sdp_answer(inviteSession_.get(), sdp_->getLocalSdpSession()) != PJ_SUCCESS) {
    1072           0 :         JAMI_ERR("[call:%s] Unable to start media negotiation for a re-invite request",
    1073             :                  getCallId().c_str());
    1074           0 :         return;
    1075             :     }
    1076             : 
    1077             :     pjsip_tx_data* tdata;
    1078          28 :     if (pjsip_inv_answer(inviteSession_.get(), PJSIP_SC_OK, NULL, NULL, &tdata) != PJ_SUCCESS) {
    1079           2 :         JAMI_ERR("[call:%s] Unable to init answer to a re-invite request", getCallId().c_str());
    1080           2 :         return;
    1081             :     }
    1082             : 
    1083          26 :     if (not contactHeader_.empty()) {
    1084          26 :         sip_utils::addContactHeader(contactHeader_, tdata);
    1085             :     }
    1086             : 
    1087             :     // Add user-agent header
    1088          26 :     sip_utils::addUserAgentHeader(account->getUserAgentName(), tdata);
    1089             : 
    1090          26 :     if (pjsip_inv_send_msg(inviteSession_.get(), tdata) != PJ_SUCCESS) {
    1091           0 :         JAMI_ERR("[call:%s] Unable to send answer to a re-invite request", getCallId().c_str());
    1092           0 :         setInviteSession();
    1093           0 :         return;
    1094             :     }
    1095             : 
    1096          26 :     JAMI_DBG("[call:%s] Successfully answered the media change request", getCallId().c_str());
    1097          32 : }
    1098             : 
    1099             : void
    1100         135 : SIPCall::hangup(int reason)
    1101             : {
    1102         135 :     std::lock_guard lk {callMutex_};
    1103         135 :     pendingRecord_ = false;
    1104         135 :     if (inviteSession_ and inviteSession_->dlg) {
    1105         106 :         pjsip_route_hdr* route = inviteSession_->dlg->route_set.next;
    1106         106 :         while (route and route != &inviteSession_->dlg->route_set) {
    1107             :             char buf[1024];
    1108           0 :             int printed = pjsip_hdr_print_on(route, buf, sizeof(buf));
    1109           0 :             if (printed >= 0) {
    1110           0 :                 buf[printed] = '\0';
    1111           0 :                 JAMI_DBG("[call:%s] Route header %s", getCallId().c_str(), buf);
    1112             :             }
    1113           0 :             route = route->next;
    1114             :         }
    1115             : 
    1116         106 :         int status = PJSIP_SC_OK;
    1117         106 :         if (reason)
    1118           2 :             status = reason;
    1119         104 :         else if (inviteSession_->state <= PJSIP_INV_STATE_EARLY
    1120         104 :                  and inviteSession_->role != PJSIP_ROLE_UAC)
    1121           1 :             status = PJSIP_SC_CALL_TSX_DOES_NOT_EXIST;
    1122         103 :         else if (inviteSession_->state >= PJSIP_INV_STATE_DISCONNECTED)
    1123           3 :             status = PJSIP_SC_DECLINE;
    1124             : 
    1125             :         // Notify the peer
    1126         106 :         terminateSipSession(status);
    1127             :     }
    1128             : 
    1129             :     // Stop all RTP streams
    1130         135 :     stopAllMedia();
    1131         135 :     detachAudioFromConference();
    1132         135 :     setState(Call::ConnectionState::DISCONNECTED, reason);
    1133         135 :     dht::ThreadPool::io().run([w = weak()] {
    1134         135 :         if (auto shared = w.lock())
    1135         135 :             shared->removeCall();
    1136         135 :     });
    1137         135 : }
    1138             : 
    1139             : void
    1140         247 : SIPCall::detachAudioFromConference()
    1141             : {
    1142             : #ifdef ENABLE_VIDEO
    1143         247 :     if (auto conf = getConference()) {
    1144           2 :         if (auto mixer = conf->getVideoMixer()) {
    1145           4 :             for (auto& stream : getRtpSessionList(MediaType::MEDIA_AUDIO)) {
    1146           2 :                 mixer->removeAudioOnlySource(getCallId(), stream->streamId());
    1147           2 :             }
    1148           2 :         }
    1149         247 :     }
    1150             : #endif
    1151         247 : }
    1152             : 
    1153             : void
    1154           2 : SIPCall::refuse()
    1155             : {
    1156           2 :     if (!isIncoming() or getConnectionState() == ConnectionState::CONNECTED or !inviteSession_)
    1157           0 :         return;
    1158             : 
    1159           2 :     stopAllMedia();
    1160             : 
    1161             :     // Notify the peer
    1162           2 :     terminateSipSession(PJSIP_SC_DECLINE);
    1163             : 
    1164           2 :     setState(Call::ConnectionState::DISCONNECTED, ECONNABORTED);
    1165           2 :     removeCall();
    1166             : }
    1167             : 
    1168             : static void
    1169           5 : transfer_client_cb(pjsip_evsub* sub, pjsip_event* event)
    1170             : {
    1171           5 :     auto mod_ua_id = Manager::instance().sipVoIPLink().getModId();
    1172             : 
    1173           5 :     switch (pjsip_evsub_get_state(sub)) {
    1174           2 :     case PJSIP_EVSUB_STATE_ACCEPTED:
    1175           2 :         if (!event)
    1176           0 :             return;
    1177             : 
    1178           2 :         pj_assert(event->type == PJSIP_EVENT_TSX_STATE
    1179             :                   && event->body.tsx_state.type == PJSIP_EVENT_RX_MSG);
    1180           2 :         break;
    1181             : 
    1182           1 :     case PJSIP_EVSUB_STATE_TERMINATED:
    1183           1 :         pjsip_evsub_set_mod_data(sub, mod_ua_id, NULL);
    1184           1 :         break;
    1185             : 
    1186           0 :     case PJSIP_EVSUB_STATE_ACTIVE: {
    1187           0 :         if (!event)
    1188           0 :             return;
    1189             : 
    1190           0 :         pjsip_rx_data* r_data = event->body.rx_msg.rdata;
    1191             : 
    1192           0 :         if (!r_data)
    1193           0 :             return;
    1194             : 
    1195           0 :         std::string request(pjsip_rx_data_get_info(r_data));
    1196             : 
    1197           0 :         pjsip_status_line status_line = {500, *pjsip_get_status_text(500)};
    1198             : 
    1199           0 :         if (!r_data->msg_info.msg)
    1200           0 :             return;
    1201             : 
    1202           0 :         if (r_data->msg_info.msg->line.req.method.id == PJSIP_OTHER_METHOD
    1203           0 :             and request.find("NOTIFY") != std::string::npos) {
    1204           0 :             pjsip_msg_body* body = r_data->msg_info.msg->body;
    1205             : 
    1206           0 :             if (!body)
    1207           0 :                 return;
    1208             : 
    1209           0 :             if (pj_stricmp2(&body->content_type.type, "message")
    1210           0 :                 or pj_stricmp2(&body->content_type.subtype, "sipfrag"))
    1211           0 :                 return;
    1212             : 
    1213           0 :             if (pjsip_parse_status_line((char*) body->data, body->len, &status_line) != PJ_SUCCESS)
    1214           0 :                 return;
    1215             :         }
    1216             : 
    1217           0 :         if (!r_data->msg_info.cid)
    1218           0 :             return;
    1219             : 
    1220           0 :         auto call = static_cast<SIPCall*>(pjsip_evsub_get_mod_data(sub, mod_ua_id));
    1221           0 :         if (!call)
    1222           0 :             return;
    1223             : 
    1224           0 :         if (status_line.code / 100 == 2) {
    1225           0 :             if (call->inviteSession_)
    1226           0 :                 call->terminateSipSession(PJSIP_SC_GONE);
    1227           0 :             Manager::instance().hangupCall(call->getAccountId(), call->getCallId());
    1228           0 :             pjsip_evsub_set_mod_data(sub, mod_ua_id, NULL);
    1229             :         }
    1230             : 
    1231           0 :         break;
    1232           0 :     }
    1233             : 
    1234           2 :     case PJSIP_EVSUB_STATE_NULL:
    1235             :     case PJSIP_EVSUB_STATE_SENT:
    1236             :     case PJSIP_EVSUB_STATE_PENDING:
    1237             :     case PJSIP_EVSUB_STATE_UNKNOWN:
    1238             :     default:
    1239           2 :         break;
    1240             :     }
    1241             : }
    1242             : 
    1243             : bool
    1244           2 : SIPCall::transferCommon(const pj_str_t* dst)
    1245             : {
    1246           2 :     if (not inviteSession_ or not inviteSession_->dlg)
    1247           0 :         return false;
    1248             : 
    1249             :     pjsip_evsub_user xfer_cb;
    1250           2 :     pj_bzero(&xfer_cb, sizeof(xfer_cb));
    1251           2 :     xfer_cb.on_evsub_state = &transfer_client_cb;
    1252             : 
    1253             :     pjsip_evsub* sub;
    1254             : 
    1255           2 :     if (pjsip_xfer_create_uac(inviteSession_->dlg, &xfer_cb, &sub) != PJ_SUCCESS)
    1256           0 :         return false;
    1257             : 
    1258             :     /* Associate this VoIPLink of call with the client subscription
    1259             :      * We are unable to just associate call with the client subscription
    1260             :      * because after this function, we are unable to find the corresponding
    1261             :      * VoIPLink from the call any more. But the VoIPLink is useful!
    1262             :      */
    1263           2 :     pjsip_evsub_set_mod_data(sub, Manager::instance().sipVoIPLink().getModId(), this);
    1264             : 
    1265             :     /*
    1266             :      * Create REFER request.
    1267             :      */
    1268             :     pjsip_tx_data* tdata;
    1269             : 
    1270           2 :     if (pjsip_xfer_initiate(sub, dst, &tdata) != PJ_SUCCESS)
    1271           0 :         return false;
    1272             : 
    1273             :     /* Send. */
    1274           2 :     if (pjsip_xfer_send_request(sub, tdata) != PJ_SUCCESS)
    1275           0 :         return false;
    1276             : 
    1277           2 :     return true;
    1278             : }
    1279             : 
    1280             : void
    1281           2 : SIPCall::transfer(const std::string& to)
    1282             : {
    1283           2 :     auto account = getSIPAccount();
    1284           2 :     if (!account) {
    1285           0 :         JAMI_ERR("No account detected");
    1286           0 :         return;
    1287             :     }
    1288             : 
    1289           2 :     deinitRecorder();
    1290           2 :     if (Call::isRecording())
    1291           0 :         stopRecording();
    1292             : 
    1293           2 :     std::string toUri = account->getToUri(to);
    1294           2 :     const pj_str_t dst(CONST_PJ_STR(toUri));
    1295             : 
    1296           2 :     JAMI_DBG("[call:%s] Transferring to %.*s", getCallId().c_str(), (int) dst.slen, dst.ptr);
    1297             : 
    1298           2 :     if (!transferCommon(&dst))
    1299           0 :         throw VoipLinkException("Unable to transfer");
    1300           2 : }
    1301             : 
    1302             : bool
    1303           0 : SIPCall::attendedTransfer(const std::string& to)
    1304             : {
    1305           0 :     auto toCall = Manager::instance().callFactory.getCall<SIPCall>(to);
    1306           0 :     if (!toCall)
    1307           0 :         return false;
    1308             : 
    1309           0 :     if (not toCall->inviteSession_ or not toCall->inviteSession_->dlg)
    1310           0 :         return false;
    1311             : 
    1312           0 :     pjsip_dialog* target_dlg = toCall->inviteSession_->dlg;
    1313           0 :     pjsip_uri* uri = (pjsip_uri*) pjsip_uri_get_uri(target_dlg->remote.info->uri);
    1314             : 
    1315           0 :     char str_dest_buf[PJSIP_MAX_URL_SIZE * 2] = {'<'};
    1316           0 :     pj_str_t dst = {str_dest_buf, 1};
    1317             : 
    1318           0 :     dst.slen += pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
    1319             :                                 uri,
    1320             :                                 str_dest_buf + 1,
    1321             :                                 sizeof(str_dest_buf) - 1);
    1322           0 :     dst.slen += pj_ansi_snprintf(str_dest_buf + dst.slen,
    1323           0 :                                  sizeof(str_dest_buf) - dst.slen,
    1324             :                                  "?"
    1325             :                                  "Replaces=%.*s"
    1326             :                                  "%%3Bto-tag%%3D%.*s"
    1327             :                                  "%%3Bfrom-tag%%3D%.*s>",
    1328           0 :                                  (int) target_dlg->call_id->id.slen,
    1329           0 :                                  target_dlg->call_id->id.ptr,
    1330           0 :                                  (int) target_dlg->remote.info->tag.slen,
    1331           0 :                                  target_dlg->remote.info->tag.ptr,
    1332           0 :                                  (int) target_dlg->local.info->tag.slen,
    1333           0 :                                  target_dlg->local.info->tag.ptr);
    1334             : 
    1335           0 :     return transferCommon(&dst);
    1336           0 : }
    1337             : 
    1338             : bool
    1339           8 : SIPCall::onhold(OnReadyCb&& cb)
    1340             : {
    1341             :     // If ICE is currently negotiating, we must wait before hold the call
    1342           8 :     if (isWaitingForIceAndMedia_) {
    1343           0 :         holdCb_ = std::move(cb);
    1344           0 :         remainingRequest_ = Request::HoldingOn;
    1345           0 :         return false;
    1346             :     }
    1347             : 
    1348           8 :     auto result = hold();
    1349             : 
    1350           8 :     if (cb)
    1351           8 :         cb(result);
    1352             : 
    1353           8 :     return result;
    1354             : }
    1355             : 
    1356             : bool
    1357           8 : SIPCall::hold()
    1358             : {
    1359           8 :     if (getConnectionState() != ConnectionState::CONNECTED) {
    1360           0 :         JAMI_WARN("[call:%s] Not connected, ignoring hold request", getCallId().c_str());
    1361           0 :         return false;
    1362             :     }
    1363             : 
    1364           8 :     if (not setState(CallState::HOLD)) {
    1365           0 :         JAMI_WARN("[call:%s] Failed to set state to HOLD", getCallId().c_str());
    1366           0 :         return false;
    1367             :     }
    1368             : 
    1369           8 :     stopAllMedia();
    1370             : 
    1371          23 :     for (auto& stream : rtpStreams_) {
    1372          15 :         stream.mediaAttribute_->onHold_ = true;
    1373             :     }
    1374             : 
    1375           8 :     if (SIPSessionReinvite() != PJ_SUCCESS) {
    1376           0 :         JAMI_WARN("[call:%s] Reinvite failed", getCallId().c_str());
    1377           0 :         return false;
    1378             :     }
    1379             : 
    1380             :     // TODO. Do we need to check for reinvIceMedia_ ?
    1381           8 :     isWaitingForIceAndMedia_ = (reinvIceMedia_ != nullptr);
    1382             : 
    1383           8 :     JAMI_DBG("[call:%s] Set state to HOLD", getCallId().c_str());
    1384           8 :     return true;
    1385             : }
    1386             : 
    1387             : bool
    1388           3 : SIPCall::offhold(OnReadyCb&& cb)
    1389             : {
    1390             :     // If ICE is currently negotiating, we must wait before unhold the call
    1391           3 :     if (isWaitingForIceAndMedia_) {
    1392           0 :         JAMI_DBG("[call:%s] ICE negotiation in progress. Resume request will be once ICE "
    1393             :                  "negotiation completes",
    1394             :                  getCallId().c_str());
    1395           0 :         offHoldCb_ = std::move(cb);
    1396           0 :         remainingRequest_ = Request::HoldingOff;
    1397           0 :         return false;
    1398             :     }
    1399           3 :     JAMI_DBG("[call:%s] Resuming the call", getCallId().c_str());
    1400           3 :     auto result = unhold();
    1401             : 
    1402           3 :     if (cb)
    1403           3 :         cb(result);
    1404             : 
    1405           3 :     return result;
    1406             : }
    1407             : 
    1408             : bool
    1409           3 : SIPCall::unhold()
    1410             : {
    1411           3 :     auto account = getSIPAccount();
    1412           3 :     if (!account) {
    1413           0 :         JAMI_ERR("No account detected");
    1414           0 :         return false;
    1415             :     }
    1416             : 
    1417           3 :     bool success = false;
    1418             :     try {
    1419           3 :         success = internalOffHold([] {});
    1420           0 :     } catch (const SdpException& e) {
    1421           0 :         JAMI_ERR("[call:%s] %s", getCallId().c_str(), e.what());
    1422           0 :         throw VoipLinkException("SDP issue in offhold");
    1423           0 :     }
    1424             : 
    1425             :     // Only wait for ICE if we have an ICE re-invite in progress
    1426           3 :     isWaitingForIceAndMedia_ = success and (reinvIceMedia_ != nullptr);
    1427             : 
    1428           3 :     return success;
    1429           3 : }
    1430             : 
    1431             : bool
    1432           3 : SIPCall::internalOffHold(const std::function<void()>& sdp_cb)
    1433             : {
    1434           3 :     if (getConnectionState() != ConnectionState::CONNECTED) {
    1435           0 :         JAMI_WARN("[call:%s] Not connected, ignoring resume request", getCallId().c_str());
    1436             :     }
    1437             : 
    1438           3 :     if (not setState(CallState::ACTIVE))
    1439           0 :         return false;
    1440             : 
    1441           3 :     sdp_cb();
    1442             : 
    1443             :     {
    1444           8 :         for (auto& stream : rtpStreams_) {
    1445           5 :             stream.mediaAttribute_->onHold_ = false;
    1446             :         }
    1447             :         // For now, call resume will always require new ICE negotiation.
    1448           3 :         if (SIPSessionReinvite(getMediaAttributeList(), true) != PJ_SUCCESS) {
    1449           0 :             JAMI_WARN("[call:%s] Resuming hold", getCallId().c_str());
    1450           0 :             if (isWaitingForIceAndMedia_) {
    1451           0 :                 remainingRequest_ = Request::HoldingOn;
    1452             :             } else {
    1453           0 :                 hold();
    1454             :             }
    1455           0 :             return false;
    1456             :         }
    1457             :     }
    1458             : 
    1459           3 :     return true;
    1460             : }
    1461             : 
    1462             : void
    1463           4 : SIPCall::switchInput(const std::string& source)
    1464             : {
    1465           4 :     JAMI_DBG("[call:%s] Set selected source to %s", getCallId().c_str(), source.c_str());
    1466             : 
    1467          11 :     for (auto const& stream : rtpStreams_) {
    1468           7 :         auto mediaAttr = stream.mediaAttribute_;
    1469           7 :         mediaAttr->sourceUri_ = source;
    1470           7 :     }
    1471             : 
    1472             :     // Check if the call is being recorded in order to continue
    1473             :     // … the recording after the switch
    1474           4 :     bool isRec = Call::isRecording();
    1475             : 
    1476           4 :     if (isWaitingForIceAndMedia_) {
    1477           0 :         remainingRequest_ = Request::SwitchInput;
    1478             :     } else {
    1479             :         // For now, switchInput will always trigger a re-invite
    1480             :         // with new ICE session.
    1481           4 :         if (SIPSessionReinvite(getMediaAttributeList(), true) == PJ_SUCCESS and reinvIceMedia_) {
    1482           2 :             isWaitingForIceAndMedia_ = true;
    1483             :         }
    1484             :     }
    1485           4 :     if (isRec) {
    1486           0 :         readyToRecord_ = false;
    1487           0 :         pendingRecord_ = true;
    1488             :     }
    1489           4 : }
    1490             : 
    1491             : void
    1492         106 : SIPCall::peerHungup()
    1493             : {
    1494         106 :     pendingRecord_ = false;
    1495             :     // Stop all RTP streams
    1496         106 :     stopAllMedia();
    1497             : 
    1498         106 :     if (inviteSession_)
    1499         100 :         terminateSipSession(PJSIP_SC_NOT_FOUND);
    1500         106 :     detachAudioFromConference();
    1501         106 :     Call::peerHungup();
    1502         106 : }
    1503             : 
    1504             : void
    1505           0 : SIPCall::carryingDTMFdigits(char code)
    1506             : {
    1507           0 :     int duration = Manager::instance().voipPreferences.getPulseLength();
    1508             :     char dtmf_body[1000];
    1509             :     int ret;
    1510             : 
    1511             :     // handle flash code
    1512           0 :     if (code == '!') {
    1513           0 :         ret = snprintf(dtmf_body, sizeof dtmf_body - 1, "Signal=16\r\nDuration=%d\r\n", duration);
    1514             :     } else {
    1515           0 :         ret = snprintf(dtmf_body,
    1516             :                        sizeof dtmf_body - 1,
    1517             :                        "Signal=%c\r\nDuration=%d\r\n",
    1518             :                        code,
    1519             :                        duration);
    1520             :     }
    1521             : 
    1522             :     try {
    1523           0 :         sendSIPInfo({dtmf_body, (size_t) ret}, "dtmf-relay");
    1524           0 :     } catch (const std::exception& e) {
    1525           0 :         JAMI_ERR("Error sending DTMF: %s", e.what());
    1526           0 :     }
    1527           0 : }
    1528             : 
    1529             : void
    1530          49 : SIPCall::setVideoOrientation(int streamIdx, int rotation)
    1531             : {
    1532          49 :     std::string streamIdPart;
    1533          49 :     if (streamIdx != -1)
    1534          49 :         streamIdPart = fmt::format("<stream_id>{}</stream_id>", streamIdx);
    1535             :     std::string sip_body = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
    1536             :                            "<media_control><vc_primitive><to_encoder>"
    1537             :                            "<device_orientation="
    1538          98 :                            + std::to_string(-rotation) + "/>" + "</to_encoder>" + streamIdPart
    1539          49 :                            + "</vc_primitive></media_control>";
    1540             : 
    1541          49 :     JAMI_DBG("Sending device orientation via SIP INFO %d for stream %u", rotation, streamIdx);
    1542             : 
    1543          49 :     sendSIPInfo(sip_body, "media_control+xml");
    1544          49 : }
    1545             : 
    1546             : void
    1547         198 : SIPCall::sendTextMessage(const std::map<std::string, std::string>& messages, const std::string& from)
    1548             : {
    1549             :     // TODO: for now we ignore the "from" (the previous implementation for sending this info was
    1550             :     //      buggy and verbose), another way to send the original message sender will be implemented
    1551             :     //      in the future
    1552         198 :     if (not subcalls_.empty()) {
    1553           0 :         pendingOutMessages_.emplace_back(messages, from);
    1554           0 :         for (auto& c : subcalls_)
    1555           0 :             c->sendTextMessage(messages, from);
    1556             :     } else {
    1557         198 :         if (inviteSession_) {
    1558             :             try {
    1559             :                 // Ignore if the peer does not allow "MESSAGE" SIP method
    1560             :                 // NOTE:
    1561             :                 // The SIP "Allow" header is not mandatory as per RFC-3261. If it's
    1562             :                 // not present and since "MESSAGE" method is an extention method,
    1563             :                 // we choose to assume that the peer does not support the "MESSAGE"
    1564             :                 // method to prevent unexpected behavior when interoperating with
    1565             :                 // some SIP implementations.
    1566         198 :                 if (not isSipMethodAllowedByPeer(sip_utils::SIP_METHODS::MESSAGE)) {
    1567           0 :                     JAMI_WARN() << fmt::format("[call:{}] Peer does not allow \"{}\" method",
    1568           0 :                                                getCallId(),
    1569           0 :                                                sip_utils::SIP_METHODS::MESSAGE);
    1570             : 
    1571             :                     // Print peer's allowed methods
    1572           0 :                     JAMI_INFO() << fmt::format("[call:{}] Peer's allowed methods: {}",
    1573           0 :                                                getCallId(),
    1574           0 :                                                peerAllowedMethods_);
    1575           0 :                     return;
    1576             :                 }
    1577             : 
    1578         198 :                 im::sendSipMessage(inviteSession_.get(), messages);
    1579             : 
    1580           0 :             } catch (...) {
    1581           0 :                 JAMI_ERR("[call:%s] Failed to send SIP text message", getCallId().c_str());
    1582           0 :             }
    1583             :         } else {
    1584           0 :             pendingOutMessages_.emplace_back(messages, from);
    1585           0 :             JAMI_ERR("[call:%s] sendTextMessage: no invite session for this call",
    1586             :                      getCallId().c_str());
    1587             :         }
    1588             :     }
    1589             : }
    1590             : 
    1591             : void
    1592         408 : SIPCall::removeCall()
    1593             : {
    1594             : #ifdef ENABLE_PLUGIN
    1595         408 :     jami::Manager::instance().getJamiPluginManager().getCallServicesManager().clearCallHandlerMaps(
    1596             :         getCallId());
    1597             : #endif
    1598         408 :     std::lock_guard lk {callMutex_};
    1599         408 :     JAMI_DBG("[call:%s] removeCall()", getCallId().c_str());
    1600         408 :     if (sdp_) {
    1601         330 :         sdp_->setActiveLocalSdpSession(nullptr);
    1602         330 :         sdp_->setActiveRemoteSdpSession(nullptr);
    1603             :     }
    1604         408 :     Call::removeCall();
    1605             : 
    1606             :     {
    1607         408 :         std::lock_guard lk(transportMtx_);
    1608         408 :         resetTransport(std::move(iceMedia_));
    1609         408 :         resetTransport(std::move(reinvIceMedia_));
    1610         408 :     }
    1611             : 
    1612         408 :     setInviteSession();
    1613         408 :     setSipTransport({});
    1614         408 : }
    1615             : 
    1616             : void
    1617         102 : SIPCall::onFailure(signed cause)
    1618             : {
    1619         102 :     if (setState(CallState::MERROR, ConnectionState::DISCONNECTED, cause)) {
    1620         101 :         runOnMainThread([w = weak()] {
    1621         101 :             if (auto shared = w.lock()) {
    1622          97 :                 auto& call = *shared;
    1623          97 :                 Manager::instance().callFailure(call);
    1624          97 :                 call.removeCall();
    1625         101 :             }
    1626         101 :         });
    1627             :     }
    1628         102 : }
    1629             : 
    1630             : void
    1631           2 : SIPCall::onBusyHere()
    1632             : {
    1633           2 :     if (getCallType() == CallType::OUTGOING)
    1634           1 :         setState(CallState::PEER_BUSY, ConnectionState::DISCONNECTED);
    1635             :     else
    1636           1 :         setState(CallState::BUSY, ConnectionState::DISCONNECTED);
    1637             : 
    1638           2 :     runOnMainThread([w = weak()] {
    1639           2 :         if (auto shared = w.lock()) {
    1640           0 :             auto& call = *shared;
    1641           0 :             Manager::instance().callBusy(call);
    1642           0 :             call.removeCall();
    1643           2 :         }
    1644           2 :     });
    1645           2 : }
    1646             : 
    1647             : void
    1648         102 : SIPCall::onClosed()
    1649             : {
    1650         102 :     runOnMainThread([w = weak()] {
    1651         102 :         if (auto shared = w.lock()) {
    1652         102 :             auto& call = *shared;
    1653         102 :             Manager::instance().peerHungupCall(call);
    1654         102 :             call.removeCall();
    1655         102 :         }
    1656         102 :     });
    1657         102 : }
    1658             : 
    1659             : void
    1660         192 : SIPCall::onAnswered()
    1661             : {
    1662         192 :     JAMI_WARN("[call:%s] onAnswered()", getCallId().c_str());
    1663         192 :     runOnMainThread([w = weak()] {
    1664         192 :         if (auto shared = w.lock()) {
    1665         192 :             if (shared->getConnectionState() != ConnectionState::CONNECTED) {
    1666          96 :                 shared->setState(CallState::ACTIVE, ConnectionState::CONNECTED);
    1667          96 :                 if (not shared->isSubcall()) {
    1668          18 :                     Manager::instance().peerAnsweredCall(*shared);
    1669             :                 }
    1670             :             }
    1671         192 :         }
    1672         192 :     });
    1673         192 : }
    1674             : 
    1675             : void
    1676         151 : SIPCall::sendKeyframe(int streamIdx)
    1677             : {
    1678             : #ifdef ENABLE_VIDEO
    1679         151 :     dht::ThreadPool::computation().run([w = weak(), streamIdx] {
    1680         151 :         if (auto sthis = w.lock()) {
    1681         151 :             JAMI_DBG("Handling picture fast update request");
    1682         151 :             if (streamIdx == -1) {
    1683           0 :                 for (const auto& videoRtp : sthis->getRtpSessionList(MediaType::MEDIA_VIDEO))
    1684           0 :                     std::static_pointer_cast<video::VideoRtpSession>(videoRtp)->forceKeyFrame();
    1685         151 :             } else if (streamIdx > -1 && streamIdx < static_cast<int>(sthis->rtpStreams_.size())) {
    1686             :                 // Apply request for requested stream
    1687         151 :                 auto& stream = sthis->rtpStreams_[streamIdx];
    1688         151 :                 if (stream.rtpSession_
    1689         151 :                     && stream.rtpSession_->getMediaType() == MediaType::MEDIA_VIDEO)
    1690         302 :                     std::static_pointer_cast<video::VideoRtpSession>(stream.rtpSession_)
    1691         151 :                         ->forceKeyFrame();
    1692             :             }
    1693         151 :         }
    1694         151 :     });
    1695             : #endif
    1696         151 : }
    1697             : 
    1698             : bool
    1699        1089 : SIPCall::isIceEnabled() const
    1700             : {
    1701        1089 :     return enableIce_;
    1702             : }
    1703             : 
    1704             : void
    1705         504 : SIPCall::setPeerUaVersion(std::string_view ua)
    1706             : {
    1707         504 :     if (peerUserAgent_ == ua or ua.empty()) {
    1708             :         // Silently ignore if it did not change or empty.
    1709         292 :         return;
    1710             :     }
    1711             : 
    1712         213 :     if (peerUserAgent_.empty()) {
    1713         639 :         JAMI_DEBUG("[call:{}] Set peer's User-Agent to [{}]",
    1714             :                    getCallId(),
    1715             :                    ua);
    1716           0 :     } else if (not peerUserAgent_.empty()) {
    1717             :         // Unlikely, but should be handled since we don't have control over the peer.
    1718             :         // Even if it's unexpected, we still attempt to parse the UA version.
    1719           0 :         JAMI_WARNING("[call:{}] Peer's User-Agent unexpectedly changed from [{}] to [{}]",
    1720             :                      getCallId(),
    1721             :                      peerUserAgent_,
    1722             :                      ua);
    1723             :     }
    1724             : 
    1725         213 :     peerUserAgent_ = ua;
    1726             : 
    1727             :     // User-agent parsing
    1728         213 :     constexpr std::string_view PACK_NAME(PACKAGE_NAME " ");
    1729         213 :     auto pos = ua.find(PACK_NAME);
    1730         213 :     if (pos == std::string_view::npos) {
    1731             :         // Must have the expected package name.
    1732           1 :         JAMI_WARN("Unable to find the expected package name in peer's User-Agent");
    1733           1 :         return;
    1734             :     }
    1735             : 
    1736         212 :     ua = ua.substr(pos + PACK_NAME.length());
    1737             : 
    1738         212 :     std::string_view version;
    1739             :     // Unstable (un-released) versions has a hyphen + commit ID after
    1740             :     // the version number. Find the commit ID if any, and ignore it.
    1741         212 :     pos = ua.find('-');
    1742         212 :     if (pos != std::string_view::npos) {
    1743             :         // Get the version and ignore the commit ID.
    1744           0 :         version = ua.substr(0, pos);
    1745             :     } else {
    1746             :         // Extract the version number.
    1747         212 :         pos = ua.find(' ');
    1748         212 :         if (pos != std::string_view::npos) {
    1749         212 :             version = ua.substr(0, pos);
    1750             :         }
    1751             :     }
    1752             : 
    1753         212 :     if (version.empty()) {
    1754           0 :         JAMI_DEBUG("[call:{}] Unable to parse peer's version", getCallId());
    1755           0 :         return;
    1756             :     }
    1757             : 
    1758         212 :     auto peerVersion = split_string_to_unsigned(version, '.');
    1759         212 :     if (peerVersion.size() > 4u) {
    1760           0 :         JAMI_WARNING("[call:{}] Unable to parse peer's version", getCallId());
    1761           0 :         return;
    1762             :     }
    1763             : 
    1764             :     // Check if peer's version is at least 10.0.2 to enable multi-stream.
    1765         212 :     peerSupportMultiStream_ = Account::meetMinimumRequiredVersion(peerVersion,
    1766             :                                                                   MULTISTREAM_REQUIRED_VERSION);
    1767         212 :     if (not peerSupportMultiStream_) {
    1768           0 :         JAMI_DEBUG("Peer's version [{}] does not support multi-stream. "
    1769             :                    "Min required version: [{}]",
    1770             :                    version,
    1771             :                    MULTISTREAM_REQUIRED_VERSION_STR);
    1772             :     }
    1773             : 
    1774             :     // Check if peer's version is at least 13.11.0 to enable multi-audio-stream.
    1775         212 :     peerSupportMultiAudioStream_ = Account::meetMinimumRequiredVersion(peerVersion,
    1776             :                                                                        MULTIAUDIO_REQUIRED_VERSION);
    1777         212 :     if (not peerSupportMultiAudioStream_) {
    1778           0 :         JAMI_DEBUG("Peer's version [{}] does not support multi-audio-stream. "
    1779             :                    "Min required version: [{}]",
    1780             :                    version,
    1781             :                    MULTIAUDIO_REQUIRED_VERSION_STR);
    1782             :     }
    1783             : 
    1784             :     // Check if peer's version is at least 13.3.0 to enable multi-ICE.
    1785         212 :     peerSupportMultiIce_ = Account::meetMinimumRequiredVersion(peerVersion,
    1786             :                                                                MULTIICE_REQUIRED_VERSION);
    1787         212 :     if (not peerSupportMultiIce_) {
    1788           0 :         JAMI_DEBUG("Peer's version [{}] does not support more than 2 ICE media streams. "
    1789             :                    "Min required version: [{}]",
    1790             :                    version,
    1791             :                    MULTIICE_REQUIRED_VERSION_STR);
    1792             :     }
    1793             : 
    1794             :     // Check if peer's version supports re-invite without ICE renegotiation.
    1795             :     peerSupportReuseIceInReinv_
    1796         212 :         = Account::meetMinimumRequiredVersion(peerVersion, REUSE_ICE_IN_REINVITE_REQUIRED_VERSION);
    1797         212 :     if (not peerSupportReuseIceInReinv_) {
    1798           0 :         JAMI_LOG("Peer's version [{:s}] does not support re-invite without ICE renegotiation. "
    1799             :                  "Min required version: [{:s}]",
    1800             :                  version,
    1801             :                  REUSE_ICE_IN_REINVITE_REQUIRED_VERSION_STR);
    1802             :     }
    1803         212 : }
    1804             : 
    1805             : void
    1806         314 : SIPCall::setPeerAllowMethods(std::vector<std::string> methods)
    1807             : {
    1808         314 :     std::lock_guard lock {callMutex_};
    1809         314 :     peerAllowedMethods_ = std::move(methods);
    1810         314 : }
    1811             : 
    1812             : bool
    1813         198 : SIPCall::isSipMethodAllowedByPeer(const std::string_view method) const
    1814             : {
    1815         198 :     std::lock_guard lock {callMutex_};
    1816             : 
    1817         198 :     return std::find(peerAllowedMethods_.begin(), peerAllowedMethods_.end(), method)
    1818         594 :            != peerAllowedMethods_.end();
    1819         198 : }
    1820             : 
    1821             : void
    1822         213 : SIPCall::onPeerRinging()
    1823             : {
    1824         213 :     JAMI_DBG("[call:%s] Peer ringing", getCallId().c_str());
    1825         213 :     setState(ConnectionState::RINGING);
    1826         213 : }
    1827             : 
    1828             : void
    1829         229 : SIPCall::addLocalIceAttributes()
    1830             : {
    1831         229 :     if (not isIceEnabled())
    1832           2 :         return;
    1833             : 
    1834         227 :     auto iceMedia = getIceMedia();
    1835             : 
    1836         227 :     if (not iceMedia) {
    1837           0 :         JAMI_ERR("[call:%s] Invalid ICE instance", getCallId().c_str());
    1838           0 :         return;
    1839             :     }
    1840             : 
    1841         227 :     auto start = std::chrono::steady_clock::now();
    1842             : 
    1843         227 :     if (not iceMedia->isInitialized()) {
    1844         116 :         JAMI_DBG("[call:%s] Waiting for ICE initialization", getCallId().c_str());
    1845             :         // we need an initialized ICE to progress further
    1846         116 :         if (iceMedia->waitForInitialization(DEFAULT_ICE_INIT_TIMEOUT) <= 0) {
    1847           0 :             JAMI_ERR("[call:%s] ICE initialization timed out", getCallId().c_str());
    1848           0 :             return;
    1849             :         }
    1850             :         // ICE initialization may take longer than usual in some cases,
    1851             :         // for instance when TURN servers do not respond in time (DNS
    1852             :         // resolution or other issues).
    1853         116 :         auto duration = std::chrono::steady_clock::now() - start;
    1854         116 :         if (duration > EXPECTED_ICE_INIT_MAX_TIME) {
    1855           0 :             JAMI_WARNING("[call:{:s}] ICE initialization time was unexpectedly high ({})",
    1856             :                          getCallId(),
    1857             :                          std::chrono::duration_cast<std::chrono::milliseconds>(duration));
    1858             :         }
    1859             :     }
    1860             : 
    1861             :     // Check the state of ICE instance, the initialization may have failed.
    1862         227 :     if (not iceMedia->isInitialized()) {
    1863           0 :         JAMI_ERR("[call:%s] ICE session is uninitialized", getCallId().c_str());
    1864           0 :         return;
    1865             :     }
    1866             : 
    1867             :     // Check the state, the call might have been canceled while waiting.
    1868             :     // for initialization.
    1869         227 :     if (getState() == Call::CallState::OVER) {
    1870           0 :         JAMI_WARN("[call:%s] The call was terminated while waiting for ICE initialization",
    1871             :                   getCallId().c_str());
    1872           0 :         return;
    1873             :     }
    1874             : 
    1875         227 :     auto account = getSIPAccount();
    1876         227 :     if (not account) {
    1877           0 :         JAMI_ERR("No account detected");
    1878           0 :         return;
    1879             :     }
    1880         227 :     if (not sdp_) {
    1881           0 :         JAMI_ERR("No sdp detected");
    1882           0 :         return;
    1883             :     }
    1884             : 
    1885         227 :     JAMI_DBG("[call:%s] Add local attributes for ICE instance [%p]",
    1886             :              getCallId().c_str(),
    1887             :              iceMedia.get());
    1888             : 
    1889         227 :     sdp_->addIceAttributes(iceMedia->getLocalAttributes());
    1890             : 
    1891         227 :     if (account->isIceCompIdRfc5245Compliant()) {
    1892           2 :         unsigned streamIdx = 0;
    1893           6 :         for (auto const& stream : rtpStreams_) {
    1894           4 :             if (not stream.mediaAttribute_->enabled_) {
    1895             :                 // Dont add ICE candidates if the media is disabled
    1896           0 :                 JAMI_DBG("[call:%s] Media [%s] @ %u is disabled, don't add local candidates",
    1897             :                          getCallId().c_str(),
    1898             :                          stream.mediaAttribute_->toString().c_str(),
    1899             :                          streamIdx);
    1900           0 :                 continue;
    1901             :             }
    1902           4 :             JAMI_DBG("[call:%s] Add ICE local candidates for media [%s] @ %u",
    1903             :                      getCallId().c_str(),
    1904             :                      stream.mediaAttribute_->toString().c_str(),
    1905             :                      streamIdx);
    1906             :             // RTP
    1907           8 :             sdp_->addIceCandidates(streamIdx,
    1908           8 :                                    iceMedia->getLocalCandidates(streamIdx, ICE_COMP_ID_RTP));
    1909             :             // RTCP if it has its own port
    1910           4 :             if (not rtcpMuxEnabled_) {
    1911           8 :                 sdp_->addIceCandidates(streamIdx,
    1912           8 :                                        iceMedia->getLocalCandidates(streamIdx, ICE_COMP_ID_RTP + 1));
    1913             :             }
    1914             : 
    1915           4 :             streamIdx++;
    1916             :         }
    1917             :     } else {
    1918         225 :         unsigned idx = 0;
    1919         225 :         unsigned compId = 1;
    1920         634 :         for (auto const& stream : rtpStreams_) {
    1921         409 :             if (not stream.mediaAttribute_->enabled_) {
    1922             :                 // Skipping local ICE candidates if the media is disabled
    1923           3 :                 continue;
    1924             :             }
    1925         406 :             JAMI_DBG("[call:%s] Add ICE local candidates for media [%s] @ %u",
    1926             :                      getCallId().c_str(),
    1927             :                      stream.mediaAttribute_->toString().c_str(),
    1928             :                      idx);
    1929             :             // RTP
    1930         406 :             sdp_->addIceCandidates(idx, iceMedia->getLocalCandidates(compId));
    1931         406 :             compId++;
    1932             : 
    1933             :             // RTCP if it has its own port
    1934         406 :             if (not rtcpMuxEnabled_) {
    1935         406 :                 sdp_->addIceCandidates(idx, iceMedia->getLocalCandidates(compId));
    1936         406 :                 compId++;
    1937             :             }
    1938             : 
    1939         406 :             idx++;
    1940             :         }
    1941             :     }
    1942         227 : }
    1943             : 
    1944             : std::vector<IceCandidate>
    1945         215 : SIPCall::getAllRemoteCandidates(dhtnet::IceTransport& transport) const
    1946             : {
    1947         215 :     std::vector<IceCandidate> rem_candidates;
    1948         608 :     for (unsigned mediaIdx = 0; mediaIdx < static_cast<unsigned>(rtpStreams_.size()); mediaIdx++) {
    1949             :         IceCandidate cand;
    1950        4023 :         for (auto& line : sdp_->getIceCandidates(mediaIdx)) {
    1951        3630 :             if (transport.parseIceAttributeLine(mediaIdx, line, cand)) {
    1952        3630 :                 JAMI_DBG("[call:%s] Add remote ICE candidate: %s",
    1953             :                          getCallId().c_str(),
    1954             :                          line.c_str());
    1955        3630 :                 rem_candidates.emplace_back(std::move(cand));
    1956             :             }
    1957         393 :         }
    1958             :     }
    1959         215 :     return rem_candidates;
    1960           0 : }
    1961             : 
    1962             : std::shared_ptr<SystemCodecInfo>
    1963         196 : SIPCall::getVideoCodec() const
    1964             : {
    1965             : #ifdef ENABLE_VIDEO
    1966             :     // Return first video codec as we negotiate only one codec for the call
    1967             :     // Note: with multistream we can negotiate codecs/stream, but it's not the case
    1968             :     // in practice (same for audio), so just return the first video codec.
    1969         196 :     for (const auto& videoRtp : getRtpSessionList(MediaType::MEDIA_VIDEO))
    1970         196 :         return videoRtp->getCodec();
    1971             : #endif
    1972          35 :     return {};
    1973             : }
    1974             : 
    1975             : std::shared_ptr<SystemCodecInfo>
    1976           0 : SIPCall::getAudioCodec() const
    1977             : {
    1978             :     // Return first video codec as we negotiate only one codec for the call
    1979           0 :     for (const auto& audioRtp : getRtpSessionList(MediaType::MEDIA_AUDIO))
    1980           0 :         return audioRtp->getCodec();
    1981           0 :     return {};
    1982             : }
    1983             : 
    1984             : void
    1985         712 : SIPCall::addMediaStream(const MediaAttribute& mediaAttr)
    1986             : {
    1987             :     // Create and add the media stream with the provided attribute.
    1988             :     // Do not create the RTP sessions yet.
    1989         712 :     RtpStream stream;
    1990         712 :     stream.mediaAttribute_ = std::make_shared<MediaAttribute>(mediaAttr);
    1991             : 
    1992             :     // Set default media source if empty. Kept for backward compatibility.
    1993             : #ifdef ENABLE_VIDEO
    1994         712 :     if (stream.mediaAttribute_->sourceUri_.empty()) {
    1995         698 :         if (auto videoManager = Manager::instance().getVideoManager()) {
    1996         698 :             stream.mediaAttribute_->sourceUri_
    1997        1396 :                 = videoManager->videoDeviceMonitor.getMRLForDefaultDevice();
    1998             :         }
    1999             :     }
    2000             : #endif
    2001             : 
    2002         712 :     rtpStreams_.emplace_back(std::move(stream));
    2003         712 : }
    2004             : 
    2005             : size_t
    2006         395 : SIPCall::initMediaStreams(const std::vector<MediaAttribute>& mediaAttrList)
    2007             : {
    2008        1097 :     for (size_t idx = 0; idx < mediaAttrList.size(); idx++) {
    2009         702 :         auto const& mediaAttr = mediaAttrList.at(idx);
    2010         702 :         if (mediaAttr.type_ != MEDIA_AUDIO && mediaAttr.type_ != MEDIA_VIDEO) {
    2011           0 :             JAMI_ERR("[call:%s] Unexpected media type %u", getCallId().c_str(), mediaAttr.type_);
    2012           0 :             assert(false);
    2013             :         }
    2014             : 
    2015         702 :         addMediaStream(mediaAttr);
    2016         702 :         auto& stream = rtpStreams_.back();
    2017         702 :         createRtpSession(stream);
    2018             : 
    2019        2106 :         JAMI_DEBUG("[call:{:s}] Added media @{:d}: {:s}",
    2020             :                    getCallId(),
    2021             :                    idx,
    2022             :                    stream.mediaAttribute_->toString(true));
    2023             :     }
    2024             : 
    2025        1185 :     JAMI_DEBUG("[call:{:s}] Created {:d} media stream(s)", getCallId(), rtpStreams_.size());
    2026             : 
    2027         395 :     return rtpStreams_.size();
    2028             : }
    2029             : 
    2030             : bool
    2031        1460 : SIPCall::hasVideo() const
    2032             : {
    2033             : #ifdef ENABLE_VIDEO
    2034        2695 :     std::function<bool(const RtpStream& stream)> videoCheck = [](auto const& stream) {
    2035        2695 :         bool validVideo = stream.mediaAttribute_
    2036        2695 :                           && stream.mediaAttribute_->hasValidVideo();
    2037        2695 :         bool validRemoteVideo = stream.remoteMediaAttribute_
    2038        2695 :                                 && stream.remoteMediaAttribute_->hasValidVideo();
    2039        2695 :         return validVideo || validRemoteVideo;
    2040        1460 :     };
    2041             : 
    2042        1460 :     const auto iter = std::find_if(rtpStreams_.begin(), rtpStreams_.end(), videoCheck);
    2043             : 
    2044        2920 :     return iter != rtpStreams_.end();
    2045             : #else
    2046             :     return false;
    2047             : #endif
    2048        1460 : }
    2049             : 
    2050             : bool
    2051         752 : SIPCall::isCaptureDeviceMuted(const MediaType& mediaType) const
    2052             : {
    2053             :     // Return true only if all media of type 'mediaType' that use capture devices
    2054             :     // source, are muted.
    2055        2102 :     std::function<bool(const RtpStream& stream)> mutedCheck = [&mediaType](auto const& stream) {
    2056        1051 :         return (stream.mediaAttribute_->type_ == mediaType and not stream.mediaAttribute_->muted_);
    2057         752 :     };
    2058         752 :     const auto iter = std::find_if(rtpStreams_.begin(), rtpStreams_.end(), mutedCheck);
    2059        1504 :     return iter == rtpStreams_.end();
    2060         752 : }
    2061             : 
    2062             : void
    2063         186 : SIPCall::setupNegotiatedMedia()
    2064             : {
    2065         186 :     JAMI_DBG("[call:%s] Updating negotiated media", getCallId().c_str());
    2066             : 
    2067         186 :     if (not sipTransport_ or not sdp_) {
    2068           0 :         JAMI_ERR("[call:%s] Call is in an invalid state", getCallId().c_str());
    2069           0 :         return;
    2070             :     }
    2071             : 
    2072         186 :     auto slots = sdp_->getMediaSlots();
    2073         186 :     bool peer_holding {true};
    2074         186 :     int streamIdx = -1;
    2075             : 
    2076         530 :     for (const auto& slot : slots) {
    2077         344 :         streamIdx++;
    2078         344 :         const auto& local = slot.first;
    2079         344 :         const auto& remote = slot.second;
    2080             : 
    2081             :         // Skip disabled media
    2082         344 :         if (not local.enabled) {
    2083           4 :             JAMI_DBG("[call:%s] [SDP:slot#%u] The media is disabled, skipping",
    2084             :                      getCallId().c_str(),
    2085             :                      streamIdx);
    2086           4 :             continue;
    2087             :         }
    2088             : 
    2089         340 :         if (static_cast<size_t>(streamIdx) >= rtpStreams_.size()) {
    2090           0 :             throw std::runtime_error("Stream index is out-of-range");
    2091             :         }
    2092             : 
    2093         340 :         auto const& rtpStream = rtpStreams_[streamIdx];
    2094             : 
    2095         340 :         if (not rtpStream.mediaAttribute_) {
    2096           0 :             throw std::runtime_error("Missing media attribute");
    2097             :         }
    2098             : 
    2099             :         // To enable a media, it must be enabled on both sides.
    2100         340 :         rtpStream.mediaAttribute_->enabled_ = local.enabled and remote.enabled;
    2101             : 
    2102         340 :         if (not rtpStream.rtpSession_)
    2103           0 :             throw std::runtime_error("Must have a valid RTP session");
    2104             : 
    2105         340 :         if (local.type != MEDIA_AUDIO && local.type != MEDIA_VIDEO) {
    2106           0 :             JAMI_ERR("[call:%s] Unexpected media type %u", getCallId().c_str(), local.type);
    2107           0 :             throw std::runtime_error("Invalid media attribute");
    2108             :         }
    2109             : 
    2110         340 :         if (local.type != remote.type) {
    2111           0 :             JAMI_ERR("[call:%s] [SDP:slot#%u] Inconsistent media type between local and remote",
    2112             :                      getCallId().c_str(),
    2113             :                      streamIdx);
    2114           0 :             continue;
    2115             :         }
    2116             : 
    2117         340 :         if (local.enabled and not local.codec) {
    2118           0 :             JAMI_WARN("[call:%s] [SDP:slot#%u] Missing local codec", getCallId().c_str(), streamIdx);
    2119           0 :             continue;
    2120             :         }
    2121             : 
    2122         340 :         if (remote.enabled and not remote.codec) {
    2123           0 :             JAMI_WARN("[call:%s] [SDP:slot#%u] Missing remote codec",
    2124             :                       getCallId().c_str(),
    2125             :                       streamIdx);
    2126           0 :             continue;
    2127             :         }
    2128             : 
    2129         340 :         if (isSrtpEnabled() and local.enabled and not local.crypto) {
    2130           0 :             JAMI_WARN("[call:%s] [SDP:slot#%u] Secure mode but no local crypto attributes. "
    2131             :                       "Ignoring the media",
    2132             :                       getCallId().c_str(),
    2133             :                       streamIdx);
    2134           0 :             continue;
    2135             :         }
    2136             : 
    2137         340 :         if (isSrtpEnabled() and remote.enabled and not remote.crypto) {
    2138           0 :             JAMI_WARN("[call:%s] [SDP:slot#%u] Secure mode but no crypto remote attributes. "
    2139             :                       "Ignoring the media",
    2140             :                       getCallId().c_str(),
    2141             :                       streamIdx);
    2142           0 :             continue;
    2143             :         }
    2144             : 
    2145             :         // Aggregate holding info over all remote streams
    2146         340 :         peer_holding &= remote.onHold;
    2147             : 
    2148         340 :         configureRtpSession(rtpStream.rtpSession_, rtpStream.mediaAttribute_, local, remote);
    2149             :     }
    2150             : 
    2151             :     // TODO. Do we really use this?
    2152         186 :     if (not isSubcall() and peerHolding_ != peer_holding) {
    2153           0 :         peerHolding_ = peer_holding;
    2154           0 :         emitSignal<libjami::CallSignal::PeerHold>(getCallId(), peerHolding_);
    2155             :     }
    2156         186 : }
    2157             : 
    2158             : void
    2159         186 : SIPCall::startAllMedia()
    2160             : {
    2161         186 :     JAMI_DBG("[call:%s] Starting all media", getCallId().c_str());
    2162             : 
    2163         186 :     if (not sipTransport_ or not sdp_) {
    2164           0 :         JAMI_ERR("[call:%s] The call is in invalid state", getCallId().c_str());
    2165           0 :         return;
    2166             :     }
    2167             : 
    2168         186 :     if (isSrtpEnabled() && not sipTransport_->isSecure()) {
    2169          18 :         JAMI_WARN("[call:%s] Crypto (SRTP) is negotiated over an insecure signaling transport",
    2170             :                   getCallId().c_str());
    2171             :     }
    2172             : 
    2173             :     // reset
    2174         186 :     readyToRecord_ = false;
    2175             : 
    2176             :     // Not restarting media loop on hold as it's a huge waste of CPU resources
    2177         186 :     if (getState() != CallState::HOLD) {
    2178         186 :         bool iceRunning = isIceRunning();
    2179         186 :         auto remoteMediaList = Sdp::getMediaAttributeListFromSdp(sdp_->getActiveRemoteSdpSession());
    2180         186 :         size_t idx = 0;
    2181         530 :         for (auto& rtpStream : rtpStreams_) {
    2182         344 :             if (not rtpStream.mediaAttribute_) {
    2183           0 :                 throw std::runtime_error("Missing media attribute");
    2184             :             }
    2185         344 :             if (idx >= remoteMediaList.size()) {
    2186           0 :                 JAMI_ERR("[call:%s] Remote media list smaller than streams (idx=%zu, size=%zu)",
    2187             :                          getCallId().c_str(), idx, remoteMediaList.size());
    2188           0 :                 break;
    2189             :             }
    2190         344 :             rtpStream.remoteMediaAttribute_ = std::make_shared<MediaAttribute>(remoteMediaList[idx]);
    2191         344 :             if (rtpStream.remoteMediaAttribute_->type_ == MediaType::MEDIA_VIDEO) {
    2192         154 :                 rtpStream.rtpSession_->setMuted(rtpStream.remoteMediaAttribute_->muted_, RtpSession::Direction::RECV);
    2193             :             }
    2194         688 :             dht::ThreadPool::io().run([
    2195             :                 w = weak(),
    2196             :                 idx,
    2197         344 :                 isVideo = rtpStream.remoteMediaAttribute_->type_ == MediaType::MEDIA_VIDEO,
    2198             :                 iceRunning,
    2199         344 :                 rtpSession = rtpStream.rtpSession_,
    2200         344 :                 rtpSocketPair = std::make_shared<std::pair<std::unique_ptr<dhtnet::IceSocket>, std::unique_ptr<dhtnet::IceSocket>>>(std::move(rtpStream.rtpSocket_), std::move(rtpStream.rtcpSocket_))
    2201         842 :             ]() mutable {
    2202             :                 try {
    2203         344 :                     if (iceRunning) {
    2204         332 :                         rtpSession->start(std::move(rtpSocketPair->first), std::move(rtpSocketPair->second));
    2205             :                     } else {
    2206          12 :                         rtpSession->start(nullptr, nullptr);
    2207             :                     }
    2208         344 :                     if (isVideo) {
    2209         154 :                         if (auto call = w.lock())
    2210         154 :                             call->requestKeyframe(idx);
    2211             :                     }
    2212           0 :                 } catch (const std::exception& e) {
    2213           0 :                     JAMI_ERR("[call:%s] Failed to start RTP session %zu: %s",
    2214             :                             w.lock() ? w.lock()->getCallId().c_str() : "unknown", idx, e.what());
    2215           0 :                 }
    2216         344 :             });
    2217         344 :             idx++;
    2218             :         }
    2219         186 :     }
    2220             : 
    2221             :     // Media is restarted, we can process the last holding request.
    2222         186 :     isWaitingForIceAndMedia_ = false;
    2223         186 :     if (remainingRequest_ != Request::NoRequest) {
    2224           0 :         bool result = true;
    2225           0 :         switch (remainingRequest_) {
    2226           0 :         case Request::HoldingOn:
    2227           0 :             result = hold();
    2228           0 :             if (holdCb_) {
    2229           0 :                 holdCb_(result);
    2230           0 :                 holdCb_ = nullptr;
    2231             :             }
    2232           0 :             break;
    2233           0 :         case Request::HoldingOff:
    2234           0 :             result = unhold();
    2235           0 :             if (offHoldCb_) {
    2236           0 :                 offHoldCb_(result);
    2237           0 :                 offHoldCb_ = nullptr;
    2238             :             }
    2239           0 :             break;
    2240           0 :         case Request::SwitchInput:
    2241           0 :             SIPSessionReinvite();
    2242           0 :             break;
    2243           0 :         default:
    2244           0 :             break;
    2245             :         }
    2246           0 :         remainingRequest_ = Request::NoRequest;
    2247             :     }
    2248             : 
    2249         186 :     mediaRestartRequired_ = false;
    2250             : 
    2251             : #ifdef ENABLE_PLUGIN
    2252             :     // Create AVStreams associated with the call
    2253         186 :     createCallAVStreams();
    2254             : #endif
    2255             : }
    2256             : 
    2257             : void
    2258           0 : SIPCall::restartMediaSender()
    2259             : {
    2260           0 :     JAMI_DBG("[call:%s] Restarting TX media streams", getCallId().c_str());
    2261           0 :     for (const auto& rtpSession : getRtpSessionList())
    2262           0 :         rtpSession->restartSender();
    2263           0 : }
    2264             : 
    2265             : void
    2266         443 : SIPCall::stopAllMedia()
    2267             : {
    2268         443 :     JAMI_DBG("[call:%s] Stopping all media", getCallId().c_str());
    2269             : 
    2270             : #ifdef ENABLE_VIDEO
    2271             :     {
    2272         443 :         std::lock_guard lk(sinksMtx_);
    2273         444 :         for (auto it = callSinksMap_.begin(); it != callSinksMap_.end();) {
    2274           2 :             for (const auto& videoRtp : getRtpSessionList(MediaType::MEDIA_VIDEO)) {
    2275           2 :                 auto& videoReceive = std::static_pointer_cast<video::VideoRtpSession>(videoRtp)
    2276           1 :                                          ->getVideoReceive();
    2277           1 :                 if (videoReceive) {
    2278           1 :                     auto& sink = videoReceive->getSink();
    2279           1 :                     sink->detach(it->second.get());
    2280             :                 }
    2281           1 :             }
    2282           1 :             it->second->stop();
    2283           1 :             it = callSinksMap_.erase(it);
    2284             :         }
    2285         443 :     }
    2286             : #endif
    2287             :     // Stop all RTP sessions in parallel
    2288         443 :     std::mutex mtx;
    2289         443 :     std::condition_variable cv;
    2290         443 :     unsigned int stoppedCount = 0;
    2291         443 :     unsigned int totalStreams = rtpStreams_.size();
    2292             : 
    2293        1257 :     for (const auto& rtpSession : getRtpSessionList()) {
    2294         814 :         dht::ThreadPool::io().run([&, rtpSession]() {
    2295             :             try {
    2296         814 :                 rtpSession->stop();
    2297           0 :             } catch (const std::exception& e) {
    2298           0 :                 JAMI_ERR("Failed to stop RTP session: %s", e.what());
    2299           0 :             }
    2300             : 
    2301         814 :             std::lock_guard lk(mtx);
    2302         814 :             stoppedCount++;
    2303         814 :             cv.notify_one();
    2304         814 :         });
    2305         443 :     }
    2306             : 
    2307             :     // Wait for all streams to be stopped
    2308         443 :     std::unique_lock lk(mtx);
    2309         443 :     cv.wait(lk, [&] {
    2310        1238 :         return stoppedCount == totalStreams;
    2311             :     });
    2312             : 
    2313             : #ifdef ENABLE_PLUGIN
    2314             :     {
    2315         443 :         clearCallAVStreams();
    2316         443 :         std::lock_guard lk(avStreamsMtx_);
    2317         443 :         Manager::instance().getJamiPluginManager().getCallServicesManager().clearAVSubject(
    2318             :             getCallId());
    2319         443 :     }
    2320             : #endif
    2321         443 : }
    2322             : 
    2323             : void
    2324           4 : SIPCall::muteMedia(const std::string& mediaType, bool mute)
    2325             : {
    2326           4 :     auto type = MediaAttribute::stringToMediaType(mediaType);
    2327             : 
    2328           4 :     if (type == MediaType::MEDIA_AUDIO) {
    2329           2 :         JAMI_WARN("[call:%s] %s all audio media",
    2330             :                   getCallId().c_str(),
    2331             :                   mute ? "muting " : "unmuting ");
    2332             : 
    2333           2 :     } else if (type == MediaType::MEDIA_VIDEO) {
    2334           2 :         JAMI_WARN("[call:%s] %s all video media",
    2335             :                   getCallId().c_str(),
    2336             :                   mute ? "muting" : "unmuting");
    2337             :     } else {
    2338           0 :         JAMI_ERR("[call:%s] Invalid media type %s", getCallId().c_str(), mediaType.c_str());
    2339           0 :         assert(false);
    2340             :     }
    2341             : 
    2342             :     // Get the current media attributes.
    2343           4 :     auto mediaList = getMediaAttributeList();
    2344             : 
    2345             :     // Mute/Unmute all medias with matching type.
    2346          12 :     for (auto& mediaAttr : mediaList) {
    2347           8 :         if (mediaAttr.type_ == type) {
    2348           4 :             mediaAttr.muted_ = mute;
    2349             :         }
    2350             :     }
    2351             : 
    2352             :     // Apply
    2353           4 :     requestMediaChange(MediaAttribute::mediaAttributesToMediaMaps(mediaList));
    2354           4 : }
    2355             : 
    2356             : void
    2357         383 : SIPCall::updateMediaStream(const MediaAttribute& newMediaAttr, size_t streamIdx)
    2358             : {
    2359         383 :     assert(streamIdx < rtpStreams_.size());
    2360             : 
    2361         383 :     auto const& rtpStream = rtpStreams_[streamIdx];
    2362         383 :     assert(rtpStream.rtpSession_);
    2363             : 
    2364         383 :     auto const& mediaAttr = rtpStream.mediaAttribute_;
    2365         383 :     assert(mediaAttr);
    2366             : 
    2367         383 :     bool notifyMute = false;
    2368             : 
    2369         383 :     if (newMediaAttr.muted_ == mediaAttr->muted_) {
    2370             :         // Nothing to do. Already in the desired state.
    2371        1110 :         JAMI_DEBUG("[call:{}] [{}] already {}",
    2372             :                  getCallId(),
    2373             :                  mediaAttr->label_,
    2374             :                  mediaAttr->muted_ ? "muted " : "unmuted ");
    2375             : 
    2376             :     } else {
    2377             :         // Update
    2378          13 :         mediaAttr->muted_ = newMediaAttr.muted_;
    2379          13 :         notifyMute = true;
    2380          39 :         JAMI_DEBUG("[call:{}] {} [{}]",
    2381             :                  getCallId(),
    2382             :                  mediaAttr->muted_ ? "muting" : "unmuting",
    2383             :                  mediaAttr->label_);
    2384             :     }
    2385             : 
    2386             :     // Only update source and type if actually set.
    2387         383 :     if (not newMediaAttr.sourceUri_.empty())
    2388          13 :         mediaAttr->sourceUri_ = newMediaAttr.sourceUri_;
    2389             : 
    2390         383 :     if (notifyMute and mediaAttr->type_ == MediaType::MEDIA_AUDIO) {
    2391           5 :         rtpStream.rtpSession_->setMediaSource(mediaAttr->sourceUri_);
    2392           5 :         rtpStream.rtpSession_->setMuted(mediaAttr->muted_);
    2393           5 :         sendMuteState(mediaAttr->muted_);
    2394           5 :         if (not isSubcall())
    2395           5 :             emitSignal<libjami::CallSignal::AudioMuted>(getCallId(), mediaAttr->muted_);
    2396           5 :         return;
    2397             :     }
    2398             : 
    2399             : #ifdef ENABLE_VIDEO
    2400         378 :     if (notifyMute and mediaAttr->type_ == MediaType::MEDIA_VIDEO) {
    2401           8 :         rtpStream.rtpSession_->setMediaSource(mediaAttr->sourceUri_);
    2402           8 :         rtpStream.rtpSession_->setMuted(mediaAttr->muted_);
    2403             : 
    2404           8 :         if (not isSubcall())
    2405           8 :             emitSignal<libjami::CallSignal::VideoMuted>(getCallId(), mediaAttr->muted_);
    2406             :     }
    2407             : #endif
    2408             : }
    2409             : 
    2410             : bool
    2411         116 : SIPCall::updateAllMediaStreams(const std::vector<MediaAttribute>& mediaAttrList, bool isRemote)
    2412             : {
    2413         116 :     JAMI_DBG("[call:%s] New local media", getCallId().c_str());
    2414             : 
    2415         116 :     if (mediaAttrList.size() > PJ_ICE_MAX_COMP / 2) {
    2416           0 :         JAMI_DEBUG("[call:{:s}] Too many media streams, limit it ({:d} vs {:d})",
    2417             :                    getCallId().c_str(),
    2418             :                    mediaAttrList.size(),
    2419             :                    PJ_ICE_MAX_COMP);
    2420           0 :         return false;
    2421             :     }
    2422             : 
    2423         116 :     unsigned idx = 0;
    2424         336 :     for (auto const& newMediaAttr : mediaAttrList) {
    2425         220 :         JAMI_DBG("[call:%s] Media @%u: %s",
    2426             :                  getCallId().c_str(),
    2427             :                  idx++,
    2428             :                  newMediaAttr.toString(true).c_str());
    2429             :     }
    2430             : 
    2431         116 :     JAMI_DBG("[call:%s] Updating local media streams", getCallId().c_str());
    2432             : 
    2433         336 :     for (auto const& newAttr : mediaAttrList) {
    2434         220 :         auto streamIdx = findRtpStreamIndex(newAttr.label_);
    2435             : 
    2436         220 :         if (streamIdx < 0) {
    2437             :             // Media does not exist, add a new one.
    2438          10 :             addMediaStream(newAttr);
    2439          10 :             auto& stream = rtpStreams_.back();
    2440             :             // If the remote asks for a new stream, our side sends nothing
    2441          10 :             stream.mediaAttribute_->muted_ = isRemote ? true : stream.mediaAttribute_->muted_;
    2442          10 :             createRtpSession(stream);
    2443          10 :             JAMI_DBG("[call:%s] Added a new media stream [%s] @ index %i",
    2444             :                      getCallId().c_str(),
    2445             :                      stream.mediaAttribute_->label_.c_str(),
    2446             :                      streamIdx);
    2447             :         } else {
    2448         210 :             updateMediaStream(newAttr, streamIdx);
    2449             :         }
    2450             :     }
    2451             : 
    2452         116 :     if (mediaAttrList.size() < rtpStreams_.size()) {
    2453             : #ifdef ENABLE_VIDEO
    2454             :         // If new media stream list got more media streams than current size, we can remove old media streams from conference
    2455           4 :         for (auto i = mediaAttrList.size(); i < rtpStreams_.size(); ++i) {
    2456           2 :             auto& stream = rtpStreams_[i];
    2457           2 :             if (stream.rtpSession_->getMediaType() == MediaType::MEDIA_VIDEO)
    2458           4 :                 std::static_pointer_cast<video::VideoRtpSession>(stream.rtpSession_)
    2459           2 :                     ->exitConference();
    2460             :         }
    2461             : #endif
    2462           2 :         rtpStreams_.resize(mediaAttrList.size());
    2463             :     }
    2464         116 :     return true;
    2465             : }
    2466             : 
    2467             : bool
    2468          88 : SIPCall::isReinviteRequired(const std::vector<MediaAttribute>& mediaAttrList)
    2469             : {
    2470          88 :     if (mediaAttrList.size() != rtpStreams_.size())
    2471           6 :         return true;
    2472             : 
    2473         224 :     for (auto const& newAttr : mediaAttrList) {
    2474         151 :         auto streamIdx = findRtpStreamIndex(newAttr.label_);
    2475             : 
    2476         151 :         if (streamIdx < 0) {
    2477             :             // Always needs a re-invite when a new media is added.
    2478           9 :             return true;
    2479             :         }
    2480             : 
    2481             :         // Changing the source needs a re-invite
    2482         151 :         if (newAttr.sourceUri_ != rtpStreams_[streamIdx].mediaAttribute_->sourceUri_) {
    2483           2 :             return true;
    2484             :         }
    2485             : 
    2486             : #ifdef ENABLE_VIDEO
    2487         149 :         if (newAttr.type_ == MediaType::MEDIA_VIDEO) {
    2488             :             // For now, only video mute triggers a re-invite.
    2489             :             // Might be done for audio as well if required.
    2490          67 :             if (newAttr.muted_ != rtpStreams_[streamIdx].mediaAttribute_->muted_) {
    2491           7 :                 return true;
    2492             :             }
    2493             :         }
    2494             : #endif
    2495             :     }
    2496             : 
    2497          73 :     return false;
    2498             : }
    2499             : 
    2500             : bool
    2501          96 : SIPCall::isNewIceMediaRequired(const std::vector<MediaAttribute>& mediaAttrList)
    2502             : {
    2503             :     // Always needs a new ICE media if the peer does not support
    2504             :     // re-invite without ICE renegotiation
    2505          96 :     if (not peerSupportReuseIceInReinv_)
    2506           0 :         return true;
    2507             : 
    2508             :     // Always needs a new ICE media when the number of media changes.
    2509          96 :     if (mediaAttrList.size() != rtpStreams_.size())
    2510           6 :         return true;
    2511             : 
    2512         254 :     for (auto const& newAttr : mediaAttrList) {
    2513         166 :         auto streamIdx = findRtpStreamIndex(newAttr.label_);
    2514         166 :         if (streamIdx < 0) {
    2515             :             // Always needs a new ICE media when a media is added or replaced.
    2516           2 :             return true;
    2517             :         }
    2518         166 :         auto const& currAttr = rtpStreams_[streamIdx].mediaAttribute_;
    2519         166 :         if (newAttr.sourceUri_ != currAttr->sourceUri_) {
    2520             :             // For now, media will be restarted if the source changes.
    2521             :             // TODO. This should not be needed if the decoder/receiver
    2522             :             // correctly handles dynamic media properties changes.
    2523           2 :             return true;
    2524             :         }
    2525             :     }
    2526             : 
    2527          88 :     return false;
    2528             : }
    2529             : 
    2530             : bool
    2531          88 : SIPCall::requestMediaChange(const std::vector<libjami::MediaMap>& mediaList)
    2532             : {
    2533          88 :     std::lock_guard lk {callMutex_};
    2534          88 :     auto mediaAttrList = MediaAttribute::buildMediaAttributesList(mediaList, isSrtpEnabled());
    2535          88 :     bool hasFileSharing {false};
    2536             : 
    2537         253 :     for (const auto& media : mediaAttrList) {
    2538         165 :         if (!media.enabled_ || media.sourceUri_.empty())
    2539         162 :             continue;
    2540             : 
    2541             :         // Supported MRL schemes
    2542          10 :         static const std::string sep = libjami::Media::VideoProtocolPrefix::SEPARATOR;
    2543             : 
    2544          10 :         const auto pos = media.sourceUri_.find(sep);
    2545          10 :         if (pos == std::string::npos)
    2546           7 :             continue;
    2547             : 
    2548           3 :         const auto prefix = media.sourceUri_.substr(0, pos);
    2549           3 :         if ((pos + sep.size()) >= media.sourceUri_.size())
    2550           0 :             continue;
    2551             : 
    2552           3 :         if (prefix == libjami::Media::VideoProtocolPrefix::FILE) {
    2553           3 :             hasFileSharing = true;
    2554           3 :             mediaPlayerId_ = media.sourceUri_;
    2555             : #ifdef ENABLE_VIDEO
    2556           3 :             createMediaPlayer(mediaPlayerId_);
    2557             : #endif
    2558             :         }
    2559           3 :     }
    2560             : 
    2561          88 :     if (!hasFileSharing) {
    2562             : #ifdef ENABLE_VIDEO
    2563          85 :         closeMediaPlayer(mediaPlayerId_);
    2564             : #endif
    2565          85 :         mediaPlayerId_ = "";
    2566             :     }
    2567             : 
    2568             :     // Disable video if disabled in the account.
    2569          88 :     auto account = getSIPAccount();
    2570          88 :     if (not account) {
    2571           0 :         JAMI_ERROR("[call:{}] No account detected", getCallId());
    2572           0 :         return false;
    2573             :     }
    2574          88 :     if (not account->isVideoEnabled()) {
    2575           0 :         for (auto& mediaAttr : mediaAttrList) {
    2576           0 :             if (mediaAttr.type_ == MediaType::MEDIA_VIDEO) {
    2577             :                 // This an API misuse. The new medialist should not contain video
    2578             :                 // if it was disabled in the account settings.
    2579           0 :                 JAMI_ERROR("[call:{}] New media has video, but it's disabled in the account. "
    2580             :                            "Ignoring the change request!",
    2581             :                            getCallId());
    2582           0 :                 return false;
    2583             :             }
    2584             :         }
    2585             :     }
    2586             : 
    2587             :     // If the peer does not support multi-stream and the size of the new
    2588             :     // media list is different from the current media list, the media
    2589             :     // change request will be ignored.
    2590          88 :     if (not peerSupportMultiStream_ and rtpStreams_.size() != mediaAttrList.size()) {
    2591           0 :         JAMI_WARNING("[call:{}] Peer does not support multi-stream. Media change request ignored",
    2592             :                      getCallId());
    2593           0 :         return false;
    2594             :     }
    2595             : 
    2596             :     // If the peer does not support multi-audio-stream and the new
    2597             :     // media list has more than one audio. Ignore the one that comes from a file.
    2598          88 :     if (not peerSupportMultiAudioStream_ and rtpStreams_.size() != mediaAttrList.size() and hasFileSharing) {
    2599           0 :         JAMI_WARNING("[call:{}] Peer does not support multi-audio-stream. New Audio will be ignored",
    2600             :                      getCallId());
    2601           0 :         for (auto it = mediaAttrList.begin(); it != mediaAttrList.end();) {
    2602           0 :             if (it->type_ == MediaType::MEDIA_AUDIO and !it->sourceUri_.empty() and mediaPlayerId_ == it->sourceUri_) {
    2603           0 :                 it = mediaAttrList.erase(it);
    2604           0 :                 continue;
    2605             :             }
    2606           0 :             ++it;
    2607             :         }
    2608             :     }
    2609             : 
    2610             :     // If peer doesn't support multiple ice, keep only the last audio/video
    2611             :     // This keep the old behaviour (if sharing both camera + sharing a file, will keep the shared file)
    2612          88 :     if (!peerSupportMultiIce_) {
    2613           0 :         if (mediaList.size() > 2)
    2614           0 :             JAMI_WARNING("[call:{}] Peer does not support more than 2 ICE medias. "
    2615             :                          "Media change request modified",
    2616             :                          getCallId());
    2617           0 :         MediaAttribute audioAttr;
    2618           0 :         MediaAttribute videoAttr;
    2619           0 :         auto hasVideo = false, hasAudio = false;
    2620           0 :         for (auto it = mediaAttrList.rbegin(); it != mediaAttrList.rend(); ++it) {
    2621           0 :             if (it->type_ == MediaType::MEDIA_VIDEO && !hasVideo) {
    2622           0 :                 videoAttr = *it;
    2623           0 :                 videoAttr.label_ = sip_utils::DEFAULT_VIDEO_STREAMID;
    2624           0 :                 hasVideo = true;
    2625           0 :             } else if (it->type_ == MediaType::MEDIA_AUDIO && !hasAudio) {
    2626           0 :                 audioAttr = *it;
    2627           0 :                 audioAttr.label_ = sip_utils::DEFAULT_AUDIO_STREAMID;
    2628           0 :                 hasAudio = true;
    2629             :             }
    2630           0 :             if (hasVideo && hasAudio)
    2631           0 :                 break;
    2632             :         }
    2633           0 :         mediaAttrList.clear();
    2634             :         // Note: use the order VIDEO/AUDIO to avoid reinvite.
    2635           0 :         mediaAttrList.emplace_back(audioAttr);
    2636           0 :         if (hasVideo)
    2637           0 :             mediaAttrList.emplace_back(videoAttr);
    2638           0 :     }
    2639             : 
    2640          88 :     if (mediaAttrList.empty()) {
    2641           0 :         JAMI_ERROR("[call:{}] Invalid media change request: new media list is empty", getCallId());
    2642           0 :         return false;
    2643             :     }
    2644         264 :     JAMI_DEBUG("[call:{}] Requesting media change. List of new media:", getCallId());
    2645             : 
    2646          88 :     unsigned idx = 0;
    2647         253 :     for (auto const& newMediaAttr : mediaAttrList) {
    2648         495 :         JAMI_DEBUG("[call:{}] Media @{:d}: {}",
    2649             :                    getCallId(),
    2650             :                    idx++,
    2651             :                    newMediaAttr.toString(true));
    2652             :     }
    2653             : 
    2654          88 :     auto needReinvite = isReinviteRequired(mediaAttrList);
    2655          88 :     auto needNewIce = isNewIceMediaRequired(mediaAttrList);
    2656             : 
    2657          88 :     if (!updateAllMediaStreams(mediaAttrList, false))
    2658           0 :         return false;
    2659             : 
    2660          88 :     if (needReinvite) {
    2661          45 :         JAMI_DEBUG("[call:{}] Media change requires a new negotiation (re-invite)",
    2662             :                    getCallId());
    2663          15 :         requestReinvite(mediaAttrList, needNewIce);
    2664             :     } else {
    2665         219 :         JAMI_DEBUG("[call:{}] Media change DOES NOT require a new negotiation (re-invite)",
    2666             :                    getCallId());
    2667          73 :         reportMediaNegotiationStatus();
    2668             :     }
    2669             : 
    2670          88 :     return true;
    2671          88 : }
    2672             : 
    2673             : std::vector<std::map<std::string, std::string>>
    2674         279 : SIPCall::currentMediaList() const
    2675             : {
    2676         558 :     return MediaAttribute::mediaAttributesToMediaMaps(getMediaAttributeList());
    2677             : }
    2678             : 
    2679             : std::vector<MediaAttribute>
    2680        1986 : SIPCall::getMediaAttributeList() const
    2681             : {
    2682        1986 :     std::lock_guard lk {callMutex_};
    2683        1986 :     std::vector<MediaAttribute> mediaList;
    2684        1986 :     mediaList.reserve(rtpStreams_.size());
    2685        5582 :     for (auto const& stream : rtpStreams_)
    2686        3596 :         mediaList.emplace_back(*stream.mediaAttribute_);
    2687        3972 :     return mediaList;
    2688        1986 : }
    2689             : 
    2690             : std::map<std::string, bool>
    2691        1009 : SIPCall::getAudioStreams() const
    2692             : {
    2693        1009 :     std::map<std::string, bool> audioMedias {};
    2694        1009 :     auto medias = getMediaAttributeList();
    2695        2834 :     for (const auto& media : medias) {
    2696        1825 :         if (media.type_ == MEDIA_AUDIO) {
    2697        1015 :             auto label = fmt::format("{}_{}", getCallId(), media.label_);
    2698        1015 :             audioMedias.emplace(label, media.muted_);
    2699        1015 :         }
    2700             :     }
    2701        2018 :     return audioMedias;
    2702        1009 : }
    2703             : 
    2704             : void
    2705         247 : SIPCall::onMediaNegotiationComplete()
    2706             : {
    2707         247 :     runOnMainThread([w = weak()] {
    2708         247 :         if (auto this_ = w.lock()) {
    2709         245 :             std::lock_guard lk {this_->callMutex_};
    2710         245 :             JAMI_DBG("[call:%s] Media negotiation complete", this_->getCallId().c_str());
    2711             : 
    2712             :             // If the call has already ended, we don't need to start the media.
    2713         245 :             if (not this_->inviteSession_
    2714         244 :                 or this_->inviteSession_->state == PJSIP_INV_STATE_DISCONNECTED
    2715         489 :                 or not this_->sdp_) {
    2716           2 :                 return;
    2717             :             }
    2718             : 
    2719             :             // This method is called to report media negotiation (SDP) for initial
    2720             :             // invite or subsequent invites (re-invite).
    2721             :             // If ICE is negotiated, the media update will be handled in the
    2722             :             // ICE callback, otherwise, it will be handled here.
    2723             :             // Note that ICE can be negotiated in the first invite and not negotiated
    2724             :             // in the re-invite. In this case, the media transport is unchanged (reused).
    2725         243 :             if (this_->isIceEnabled() and this_->remoteHasValidIceAttributes()) {
    2726         215 :                 if (not this_->isSubcall()) {
    2727             :                     // Start ICE checks. Media will be started once ICE checks complete.
    2728         137 :                     this_->startIceMedia();
    2729             :                 }
    2730             :             } else {
    2731             :                 // Update the negotiated media.
    2732          28 :                 if (this_->mediaRestartRequired_) {
    2733           8 :                     this_->setupNegotiatedMedia();
    2734             :                     // No ICE, start media now.
    2735           8 :                     JAMI_WARN("[call:%s] ICE media disabled, using default media ports",
    2736             :                               this_->getCallId().c_str());
    2737             :                     // Start the media.
    2738           8 :                     this_->stopAllMedia();
    2739           8 :                     this_->startAllMedia();
    2740             :                 }
    2741             : 
    2742             :                 //this_->updateRemoteMedia();
    2743          28 :                 this_->reportMediaNegotiationStatus();
    2744             :             }
    2745         492 :         }
    2746             :     });
    2747         247 : }
    2748             : 
    2749             : void
    2750         279 : SIPCall::reportMediaNegotiationStatus()
    2751             : {
    2752             :     // Notify using the parent Id if it's a subcall.
    2753         279 :     auto callId = isSubcall() ? parent_->getCallId() : getCallId();
    2754         279 :     emitSignal<libjami::CallSignal::MediaNegotiationStatus>(
    2755             :         callId,
    2756             :         libjami::Media::MediaNegotiationStatusEvents::NEGOTIATION_SUCCESS,
    2757         558 :         currentMediaList());
    2758         279 :     auto previousState = isAudioOnly_;
    2759         279 :     auto newState = !hasVideo();
    2760             : 
    2761         279 :     if (previousState != newState && Call::isRecording()) {
    2762           1 :         deinitRecorder();
    2763           1 :         toggleRecording();
    2764           1 :         pendingRecord_ = true;
    2765             :     }
    2766         279 :     isAudioOnly_ = newState;
    2767             : 
    2768         279 :     if (pendingRecord_ && readyToRecord_) {
    2769           0 :         toggleRecording();
    2770             :     }
    2771         279 : }
    2772             : 
    2773             : void
    2774         215 : SIPCall::startIceMedia()
    2775             : {
    2776         215 :     JAMI_DBG("[call:%s] Starting ICE", getCallId().c_str());
    2777         215 :     auto iceMedia = getIceMedia();
    2778         215 :     if (not iceMedia or iceMedia->isFailed()) {
    2779           0 :         JAMI_ERR("[call:%s] Media ICE init failed", getCallId().c_str());
    2780           0 :         onFailure(EIO);
    2781           0 :         return;
    2782             :     }
    2783             : 
    2784         215 :     if (iceMedia->isStarted()) {
    2785             :         // NOTE: for incoming calls, the ICE is already there and running
    2786           0 :         if (iceMedia->isRunning())
    2787           0 :             onIceNegoSucceed();
    2788           0 :         return;
    2789             :     }
    2790             : 
    2791         215 :     if (not iceMedia->isInitialized()) {
    2792             :         // In this case, onInitDone will occurs after the startIceMedia
    2793           0 :         waitForIceInit_ = true;
    2794           0 :         return;
    2795             :     }
    2796             : 
    2797             :     // Start transport on SDP data and wait for negotiation
    2798         215 :     if (!sdp_)
    2799           0 :         return;
    2800         215 :     auto rem_ice_attrs = sdp_->getIceAttributes();
    2801         215 :     if (rem_ice_attrs.ufrag.empty() or rem_ice_attrs.pwd.empty()) {
    2802           0 :         JAMI_ERR("[call:%s] Missing remote media ICE attributes", getCallId().c_str());
    2803           0 :         onFailure(EIO);
    2804           0 :         return;
    2805             :     }
    2806         215 :     if (not iceMedia->startIce(rem_ice_attrs, getAllRemoteCandidates(*iceMedia))) {
    2807           1 :         JAMI_ERR("[call:%s] ICE media failed to start", getCallId().c_str());
    2808           1 :         onFailure(EIO);
    2809             :     }
    2810         215 : }
    2811             : 
    2812             : void
    2813         178 : SIPCall::onIceNegoSucceed()
    2814             : {
    2815         178 :     std::lock_guard lk {callMutex_};
    2816             : 
    2817         178 :     JAMI_DBG("[call:%s] ICE negotiation succeeded", getCallId().c_str());
    2818             : 
    2819             :     // Check if the call is already ended, so we don't need to restart medias
    2820             :     // This is typically the case in a multi-device context where one device
    2821             :     // can stop a call. So do not start medias
    2822         178 :     if (not inviteSession_ or inviteSession_->state == PJSIP_INV_STATE_DISCONNECTED or not sdp_) {
    2823           0 :         JAMI_ERR("[call:%s] ICE negotiation succeeded, but call is in invalid state",
    2824             :                  getCallId().c_str());
    2825           0 :         return;
    2826             :     }
    2827             : 
    2828             :     // Update the negotiated media.
    2829         178 :     setupNegotiatedMedia();
    2830             : 
    2831             :     // If this callback is for a re-invite session then update
    2832             :     // the ICE media transport.
    2833         178 :     if (isIceEnabled())
    2834         178 :         switchToIceReinviteIfNeeded();
    2835             : 
    2836         510 :     for (unsigned int idx = 0, compId = 1; idx < rtpStreams_.size(); idx++, compId += 2) {
    2837             :         // Create sockets for RTP and RTCP, and start the session.
    2838         332 :         auto& rtpStream = rtpStreams_[idx];
    2839         332 :         rtpStream.rtpSocket_ = newIceSocket(compId);
    2840             : 
    2841         332 :         if (not rtcpMuxEnabled_) {
    2842         332 :             rtpStream.rtcpSocket_ = newIceSocket(compId + 1);
    2843             :         }
    2844             :     }
    2845             : 
    2846             :     // Start/Restart the media using the new transport
    2847         178 :     stopAllMedia();
    2848         178 :     startAllMedia();
    2849         178 :     reportMediaNegotiationStatus();
    2850         178 : }
    2851             : 
    2852             : bool
    2853          26 : SIPCall::checkMediaChangeRequest(const std::vector<libjami::MediaMap>& remoteMediaList)
    2854             : {
    2855             :     // The current media is considered to have changed if one of the
    2856             :     // following condtions is true:
    2857             :     //
    2858             :     // - the number of media changed
    2859             :     // - the type of one of the media changed (unlikely)
    2860             :     // - one of the media was enabled/disabled
    2861             : 
    2862          26 :     JAMI_DBG("[call:%s] Received a media change request", getCallId().c_str());
    2863             : 
    2864             :     auto remoteMediaAttrList = MediaAttribute::buildMediaAttributesList(remoteMediaList,
    2865          26 :                                                                         isSrtpEnabled());
    2866          26 :     if (remoteMediaAttrList.size() != rtpStreams_.size())
    2867           4 :         return true;
    2868             : 
    2869          63 :     for (size_t i = 0; i < rtpStreams_.size(); i++) {
    2870          41 :         if (remoteMediaAttrList[i].type_ != rtpStreams_[i].mediaAttribute_->type_)
    2871           0 :             return true;
    2872          41 :         if (remoteMediaAttrList[i].enabled_ != rtpStreams_[i].mediaAttribute_->enabled_)
    2873           0 :             return true;
    2874             :     }
    2875             : 
    2876          22 :     return false;
    2877          26 : }
    2878             : 
    2879             : void
    2880          26 : SIPCall::handleMediaChangeRequest(const std::vector<libjami::MediaMap>& remoteMediaList)
    2881             : {
    2882          26 :     JAMI_DBG("[call:%s] Handling media change request", getCallId().c_str());
    2883             : 
    2884          26 :     auto account = getAccount().lock();
    2885          26 :     if (not account) {
    2886           0 :         JAMI_ERR("No account detected");
    2887           0 :         return;
    2888             :     }
    2889             : 
    2890             :     // If the offered media does not differ from the current local media, the
    2891             :     // request is answered using the current local media.
    2892          26 :     if (not checkMediaChangeRequest(remoteMediaList)) {
    2893          22 :         answerMediaChangeRequest(
    2894          44 :             MediaAttribute::mediaAttributesToMediaMaps(getMediaAttributeList()));
    2895          22 :         return;
    2896             :     }
    2897             : 
    2898           4 :     if (account->isAutoAnswerEnabled()) {
    2899             :         // NOTE:
    2900             :         // Since the auto-answer is enabled in the account, newly
    2901             :         // added media are accepted too.
    2902             :         // This also means that if original call was an audio-only call,
    2903             :         // the local camera will be enabled, unless the video is disabled
    2904             :         // in the account settings.
    2905             : 
    2906           1 :         std::vector<libjami::MediaMap> newMediaList;
    2907           1 :         newMediaList.reserve(remoteMediaList.size());
    2908           2 :         for (auto const& stream : rtpStreams_) {
    2909           1 :             newMediaList.emplace_back(MediaAttribute::toMediaMap(*stream.mediaAttribute_));
    2910             :         }
    2911             : 
    2912           1 :         assert(remoteMediaList.size() > 0);
    2913           1 :         if (remoteMediaList.size() > newMediaList.size()) {
    2914           2 :             for (auto idx = newMediaList.size(); idx < remoteMediaList.size(); idx++) {
    2915           1 :                 newMediaList.emplace_back(remoteMediaList[idx]);
    2916             :             }
    2917             :         }
    2918           1 :         answerMediaChangeRequest(newMediaList, true);
    2919           1 :         return;
    2920           1 :     }
    2921             : 
    2922             :     // Report the media change request.
    2923           6 :     emitSignal<libjami::CallSignal::MediaChangeRequested>(getAccountId(),
    2924           3 :                                                           getCallId(),
    2925             :                                                           remoteMediaList);
    2926          26 : }
    2927             : 
    2928             : pj_status_t
    2929          28 : SIPCall::onReceiveReinvite(const pjmedia_sdp_session* offer, pjsip_rx_data* rdata)
    2930             : {
    2931          28 :     JAMI_DBG("[call:%s] Received a re-invite", getCallId().c_str());
    2932             : 
    2933          28 :     pj_status_t res = PJ_SUCCESS;
    2934             : 
    2935          28 :     if (not sdp_) {
    2936           0 :         JAMI_ERR("SDP session is invalid");
    2937           0 :         return res;
    2938             :     }
    2939             : 
    2940          28 :     sdp_->clearIce();
    2941          28 :     sdp_->setActiveRemoteSdpSession(nullptr);
    2942          28 :     sdp_->setActiveLocalSdpSession(nullptr);
    2943             : 
    2944          28 :     auto acc = getSIPAccount();
    2945          28 :     if (not acc) {
    2946           0 :         JAMI_ERR("No account detected");
    2947           0 :         return res;
    2948             :     }
    2949             : 
    2950          28 :     Sdp::printSession(offer, "Remote session (media change request)", SdpDirection::OFFER);
    2951             : 
    2952          28 :     sdp_->setReceivedOffer(offer);
    2953             : 
    2954             :     // Note: For multistream, here we must ignore disabled remote medias, because
    2955             :     // we will answer from our medias and remote enabled medias.
    2956             :     // Example: if remote disables its camera and share its screen, the offer will
    2957             :     // have an active and a disabled media (with port = 0).
    2958             :     // In this case, if we have only one video, we can just negotiate 1 video instead of 2
    2959             :     // with 1 disabled.
    2960             :     // cf. pjmedia_sdp_neg_modify_local_offer2 for more details.
    2961          28 :     auto const& mediaAttrList = Sdp::getMediaAttributeListFromSdp(offer, true);
    2962          28 :     if (mediaAttrList.empty()) {
    2963           0 :         JAMI_WARN("[call:%s] Media list is empty, ignoring", getCallId().c_str());
    2964           0 :         return res;
    2965             :     }
    2966             : 
    2967          28 :     if (upnp_) {
    2968           0 :         openPortsUPnP();
    2969             :     }
    2970             : 
    2971          28 :     pjsip_tx_data* tdata = nullptr;
    2972          28 :     if (pjsip_inv_initial_answer(inviteSession_.get(), rdata, PJSIP_SC_TRYING, NULL, NULL, &tdata)
    2973          28 :         != PJ_SUCCESS) {
    2974           0 :         JAMI_ERR("[call:%s] Unable to create answer TRYING", getCallId().c_str());
    2975           0 :         return res;
    2976             :     }
    2977             : 
    2978          28 :     dht::ThreadPool::io().run([callWkPtr = weak(), mediaAttrList] {
    2979          28 :         if (auto call = callWkPtr.lock()) {
    2980             :             // Report the change request.
    2981          28 :             auto const& remoteMediaList = MediaAttribute::mediaAttributesToMediaMaps(mediaAttrList);
    2982          28 :             if (auto conf = call->getConference()) {
    2983           2 :                 conf->handleMediaChangeRequest(call, remoteMediaList);
    2984             :             } else {
    2985          26 :                 call->handleMediaChangeRequest(remoteMediaList);
    2986          28 :             }
    2987          56 :         }
    2988          28 :     });
    2989             : 
    2990          28 :     return res;
    2991          28 : }
    2992             : 
    2993             : void
    2994           0 : SIPCall::onReceiveOfferIn200OK(const pjmedia_sdp_session* offer)
    2995             : {
    2996           0 :     if (not rtpStreams_.empty()) {
    2997           0 :         JAMI_ERR("[call:%s] Unexpected offer in '200 OK' answer", getCallId().c_str());
    2998           0 :         return;
    2999             :     }
    3000             : 
    3001           0 :     auto acc = getSIPAccount();
    3002           0 :     if (not acc) {
    3003           0 :         JAMI_ERR("No account detected");
    3004           0 :         return;
    3005             :     }
    3006             : 
    3007           0 :     if (not sdp_) {
    3008           0 :         JAMI_ERR("Invalid SDP session");
    3009           0 :         return;
    3010             :     }
    3011             : 
    3012           0 :     JAMI_DBG("[call:%s] Received an offer in '200 OK' answer", getCallId().c_str());
    3013             : 
    3014           0 :     auto mediaList = Sdp::getMediaAttributeListFromSdp(offer);
    3015             :     // If this method is called, it means we are expecting an offer
    3016             :     // in the 200OK answer.
    3017           0 :     if (mediaList.empty()) {
    3018           0 :         JAMI_WARN("[call:%s] Remote media list is empty, ignoring", getCallId().c_str());
    3019           0 :         return;
    3020             :     }
    3021             : 
    3022           0 :     Sdp::printSession(offer, "Remote session (offer in 200 OK answer)", SdpDirection::OFFER);
    3023             : 
    3024           0 :     sdp_->clearIce();
    3025           0 :     sdp_->setActiveRemoteSdpSession(nullptr);
    3026           0 :     sdp_->setActiveLocalSdpSession(nullptr);
    3027             : 
    3028           0 :     sdp_->setReceivedOffer(offer);
    3029             : 
    3030             :     // If we send an empty offer, video will be accepted only if locally
    3031             :     // enabled by the user.
    3032           0 :     for (auto& mediaAttr : mediaList) {
    3033           0 :         if (mediaAttr.type_ == MediaType::MEDIA_VIDEO and not acc->isVideoEnabled()) {
    3034           0 :             mediaAttr.enabled_ = false;
    3035             :         }
    3036             :     }
    3037             : 
    3038           0 :     initMediaStreams(mediaList);
    3039             : 
    3040           0 :     sdp_->processIncomingOffer(mediaList);
    3041             : 
    3042           0 :     if (upnp_) {
    3043           0 :         openPortsUPnP();
    3044             :     }
    3045             : 
    3046           0 :     if (isIceEnabled() and remoteHasValidIceAttributes()) {
    3047           0 :         setupIceResponse();
    3048             :     }
    3049             : 
    3050           0 :     sdp_->startNegotiation();
    3051             : 
    3052           0 :     if (pjsip_inv_set_sdp_answer(inviteSession_.get(), sdp_->getLocalSdpSession()) != PJ_SUCCESS) {
    3053           0 :         JAMI_ERR("[call:%s] Unable to start media negotiation for a re-invite request",
    3054             :                  getCallId().c_str());
    3055             :     }
    3056           0 : }
    3057             : 
    3058             : void
    3059           0 : SIPCall::openPortsUPnP()
    3060             : {
    3061           0 :     if (not sdp_) {
    3062           0 :         JAMI_ERR("[call:%s] Current SDP instance is invalid", getCallId().c_str());
    3063           0 :         return;
    3064             :     }
    3065             : 
    3066             :     /**
    3067             :      * Attempt to open the desired ports with UPnP,
    3068             :      * if they are used, use the alternative port and update the SDP session with the newly
    3069             :      * chosen port(s)
    3070             :      *
    3071             :      * TODO:
    3072             :      * No need to request mappings for specfic port numbers. Set the port to '0' to
    3073             :      * request the first available port (faster and more likely to succeed).
    3074             :      */
    3075           0 :     JAMI_DBG("[call:%s] Opening ports via UPnP for SDP session", getCallId().c_str());
    3076             : 
    3077             :     // RTP port.
    3078           0 :     upnp_->reserveMapping(sdp_->getLocalAudioPort(), dhtnet::upnp::PortType::UDP);
    3079             :     // RTCP port.
    3080           0 :     upnp_->reserveMapping(sdp_->getLocalAudioControlPort(), dhtnet::upnp::PortType::UDP);
    3081             : 
    3082             : #ifdef ENABLE_VIDEO
    3083             :     // RTP port.
    3084           0 :     upnp_->reserveMapping(sdp_->getLocalVideoPort(), dhtnet::upnp::PortType::UDP);
    3085             :     // RTCP port.
    3086           0 :     upnp_->reserveMapping(sdp_->getLocalVideoControlPort(), dhtnet::upnp::PortType::UDP);
    3087             : #endif
    3088             : }
    3089             : 
    3090             : std::map<std::string, std::string>
    3091         370 : SIPCall::getDetails() const
    3092             : {
    3093         370 :     auto acc = getSIPAccount();
    3094         370 :     if (!acc) {
    3095           0 :         JAMI_ERR("No account detected");
    3096           0 :         return {};
    3097             :     }
    3098             : 
    3099         370 :     auto details = Call::getDetails();
    3100             : 
    3101         370 :     details.emplace(libjami::Call::Details::PEER_HOLDING, peerHolding_ ? TRUE_STR : FALSE_STR);
    3102             : 
    3103        1032 :     for (auto const& stream : rtpStreams_) {
    3104         662 :         if (stream.mediaAttribute_->type_ == MediaType::MEDIA_VIDEO) {
    3105         292 :             details.emplace(libjami::Call::Details::VIDEO_SOURCE,
    3106         292 :                             stream.mediaAttribute_->sourceUri_);
    3107             : #ifdef ENABLE_VIDEO
    3108         292 :             if (auto const& rtpSession = stream.rtpSession_) {
    3109         292 :                 if (auto codec = rtpSession->getCodec()) {
    3110          89 :                     details.emplace(libjami::Call::Details::VIDEO_CODEC,
    3111          89 :                                     codec->name);
    3112          89 :                     details.emplace(libjami::Call::Details::VIDEO_MIN_BITRATE,
    3113         178 :                                     std::to_string(codec->minBitrate));
    3114          89 :                     details.emplace(libjami::Call::Details::VIDEO_MAX_BITRATE,
    3115         178 :                                     std::to_string(codec->maxBitrate));
    3116          89 :                     if (const auto& curvideoRtpSession
    3117          89 :                         = std::static_pointer_cast<video::VideoRtpSession>(rtpSession)) {
    3118          89 :                         details.emplace(libjami::Call::Details::VIDEO_BITRATE,
    3119         178 :                                         std::to_string(curvideoRtpSession->getVideoBitrateInfo()
    3120          89 :                                                        .videoBitrateCurrent));
    3121          89 :                     }
    3122             :                 } else
    3123         292 :                     details.emplace(libjami::Call::Details::VIDEO_CODEC, "");
    3124             :             }
    3125             : #endif
    3126         370 :         } else if (stream.mediaAttribute_->type_ == MediaType::MEDIA_AUDIO) {
    3127         370 :             if (auto const& rtpSession = stream.rtpSession_) {
    3128         370 :                 if (auto codec = rtpSession->getCodec()) {
    3129         105 :                     details.emplace(libjami::Call::Details::AUDIO_CODEC,
    3130         105 :                                     codec->name);
    3131         105 :                     details.emplace(libjami::Call::Details::AUDIO_SAMPLE_RATE,
    3132         210 :                                     codec->getCodecSpecifications()
    3133         105 :                                     [libjami::Account::ConfProperties::CodecInfo::SAMPLE_RATE]);
    3134             :                 } else {
    3135         265 :                     details.emplace(libjami::Call::Details::AUDIO_CODEC, "");
    3136         265 :                     details.emplace(libjami::Call::Details::AUDIO_SAMPLE_RATE, "");
    3137         370 :                 }
    3138             :             }
    3139             :         }
    3140             :     }
    3141             : 
    3142             : #ifdef ENABLE_NAMESERVER
    3143         370 :     if (not peerRegisteredName_.empty())
    3144           2 :         details.emplace(libjami::Call::Details::REGISTERED_NAME, peerRegisteredName_);
    3145             : #endif
    3146             : 
    3147             : #ifdef ENABLE_CLIENT_CERT
    3148             :     std::lock_guard lk {callMutex_};
    3149             :     if (transport_ and transport_->isSecure()) {
    3150             :         const auto& tlsInfos = transport_->getTlsInfos();
    3151             :         if (tlsInfos.cipher != PJ_TLS_UNKNOWN_CIPHER) {
    3152             :             const auto& cipher = pj_ssl_cipher_name(tlsInfos.cipher);
    3153             :             details.emplace(libjami::TlsTransport::TLS_CIPHER, cipher ? cipher : "");
    3154             :         } else {
    3155             :             details.emplace(libjami::TlsTransport::TLS_CIPHER, "");
    3156             :         }
    3157             :         if (tlsInfos.peerCert) {
    3158             :             details.emplace(libjami::TlsTransport::TLS_PEER_CERT, tlsInfos.peerCert->toString());
    3159             :             auto ca = tlsInfos.peerCert->issuer;
    3160             :             unsigned n = 0;
    3161             :             while (ca) {
    3162             :                 std::ostringstream name_str;
    3163             :                 name_str << libjami::TlsTransport::TLS_PEER_CA_ << n++;
    3164             :                 details.emplace(name_str.str(), ca->toString());
    3165             :                 ca = ca->issuer;
    3166             :             }
    3167             :             details.emplace(libjami::TlsTransport::TLS_PEER_CA_NUM, std::to_string(n));
    3168             :         } else {
    3169             :             details.emplace(libjami::TlsTransport::TLS_PEER_CERT, "");
    3170             :             details.emplace(libjami::TlsTransport::TLS_PEER_CA_NUM, "");
    3171             :         }
    3172             :     }
    3173             : #endif
    3174         370 :     if (auto transport = getIceMedia()) {
    3175         340 :         if (transport && transport->isRunning())
    3176         101 :             details.emplace(libjami::Call::Details::SOCKETS, transport->link().c_str());
    3177         370 :     }
    3178         370 :     return details;
    3179         370 : }
    3180             : 
    3181             : void
    3182          71 : SIPCall::enterConference(std::shared_ptr<Conference> conference)
    3183             : {
    3184         213 :     JAMI_DEBUG("[call:{}] Entering conference [{}]",
    3185             :                getCallId(),
    3186             :                conference->getConfId());
    3187          71 :     conf_ = conference;
    3188             :     // Unbind audio. It will be rebinded in the conference if needed
    3189          71 :     auto const hasAudio = !getRtpSessionList(MediaType::MEDIA_AUDIO).empty();
    3190          71 :     if (hasAudio) {
    3191          71 :         auto& rbPool = Manager::instance().getRingBufferPool();
    3192          71 :         auto medias = getAudioStreams();
    3193         142 :         for (const auto& media : medias) {
    3194          71 :             rbPool.unbindRingBuffers(media.first, RingBufferPool::DEFAULT_ID);
    3195             :         }
    3196          71 :         rbPool.flush(RingBufferPool::DEFAULT_ID);
    3197          71 :     }
    3198             : 
    3199             : #ifdef ENABLE_VIDEO
    3200          71 :     if (conference->isVideoEnabled())
    3201         130 :         for (const auto& videoRtp : getRtpSessionList(MediaType::MEDIA_VIDEO))
    3202         130 :             std::static_pointer_cast<video::VideoRtpSession>(videoRtp)->enterConference(*conference);
    3203             : #endif
    3204             : 
    3205             : #ifdef ENABLE_PLUGIN
    3206          71 :     clearCallAVStreams();
    3207             : #endif
    3208          71 : }
    3209             : 
    3210             : void
    3211          69 : SIPCall::exitConference()
    3212             : {
    3213          69 :     std::lock_guard lk {callMutex_};
    3214          69 :     JAMI_DBG("[call:%s] Leaving conference", getCallId().c_str());
    3215             : 
    3216          69 :     auto const hasAudio = !getRtpSessionList(MediaType::MEDIA_AUDIO).empty();
    3217          69 :     if (hasAudio) {
    3218          69 :         auto& rbPool = Manager::instance().getRingBufferPool();
    3219          69 :         auto medias = getAudioStreams();
    3220         138 :         for (const auto& media : medias) {
    3221          69 :             if (!media.second) {
    3222          64 :                 rbPool.bindRingBuffers(media.first, RingBufferPool::DEFAULT_ID);
    3223             :             }
    3224             :         }
    3225          69 :         rbPool.flush(RingBufferPool::DEFAULT_ID);
    3226          69 :     }
    3227             : #ifdef ENABLE_VIDEO
    3228         125 :     for (const auto& videoRtp : getRtpSessionList(MediaType::MEDIA_VIDEO))
    3229         125 :         std::static_pointer_cast<video::VideoRtpSession>(videoRtp)->exitConference();
    3230             : #endif
    3231             : #ifdef ENABLE_PLUGIN
    3232          69 :     createCallAVStreams();
    3233             : #endif
    3234          69 :     conf_.reset();
    3235          69 : }
    3236             : 
    3237             : void
    3238           0 : SIPCall::setActiveMediaStream(const std::string& accountUri,
    3239             :     const std::string& deviceId,
    3240             :     const std::string& streamId,
    3241             :     const bool& state)
    3242             : {
    3243           0 :     auto remoteStreamId = streamId;
    3244             : #ifdef ENABLE_VIDEO
    3245             :     {
    3246           0 :         std::lock_guard lk(sinksMtx_);
    3247           0 :         const auto& localIt = local2RemoteSinks_.find(streamId);
    3248           0 :         if (localIt != local2RemoteSinks_.end()) {
    3249           0 :             remoteStreamId = localIt->second;
    3250             :         }
    3251           0 :     }
    3252             : #endif
    3253             : 
    3254           0 :     if (Call::conferenceProtocolVersion() == 1) {
    3255           0 :         Json::Value sinkVal;
    3256           0 :         sinkVal["active"] = state;
    3257           0 :         Json::Value mediasObj;
    3258           0 :         mediasObj[remoteStreamId] = sinkVal;
    3259           0 :         Json::Value deviceVal;
    3260           0 :         deviceVal["medias"] = mediasObj;
    3261           0 :         Json::Value deviceObj;
    3262           0 :         deviceObj[deviceId] = deviceVal;
    3263           0 :         Json::Value accountVal;
    3264           0 :         deviceVal["devices"] = deviceObj;
    3265           0 :         Json::Value root;
    3266           0 :         root[accountUri] = deviceVal;
    3267           0 :         root["version"] = 1;
    3268           0 :         Call::sendConfOrder(root);
    3269           0 :     } else if (Call::conferenceProtocolVersion() == 0) {
    3270           0 :         Json::Value root;
    3271           0 :         root["activeParticipant"] = accountUri;
    3272           0 :         Call::sendConfOrder(root);
    3273           0 :     }
    3274           0 : }
    3275             : 
    3276             : #ifdef ENABLE_VIDEO
    3277             : void
    3278          49 : SIPCall::setRotation(int streamIdx, int rotation)
    3279             : {
    3280             :     // Retrigger on another thread to avoid to lock PJSIP
    3281          49 :     dht::ThreadPool::io().run([w = weak(), streamIdx, rotation] {
    3282          49 :         if (auto shared = w.lock()) {
    3283          49 :             std::lock_guard lk {shared->callMutex_};
    3284          49 :             shared->rotation_ = rotation;
    3285          49 :             if (streamIdx == -1) {
    3286           0 :                 for (const auto& videoRtp : shared->getRtpSessionList(MediaType::MEDIA_VIDEO))
    3287           0 :                     std::static_pointer_cast<video::VideoRtpSession>(videoRtp)->setRotation(rotation);
    3288          49 :             } else if (streamIdx > -1 && streamIdx < static_cast<int>(shared->rtpStreams_.size())) {
    3289             :                 // Apply request for requested stream
    3290          49 :                 auto& stream = shared->rtpStreams_[streamIdx];
    3291          49 :                 if (stream.rtpSession_ && stream.rtpSession_->getMediaType() == MediaType::MEDIA_VIDEO)
    3292          98 :                     std::static_pointer_cast<video::VideoRtpSession>(stream.rtpSession_)
    3293          49 :                         ->setRotation(rotation);
    3294             :             }
    3295          98 :         }
    3296          49 :     });
    3297             : 
    3298          49 : }
    3299             : 
    3300             : void
    3301         184 : SIPCall::createSinks(ConfInfo& infos)
    3302             : {
    3303         184 :     std::lock_guard lk(callMutex_);
    3304         184 :     std::lock_guard lkS(sinksMtx_);
    3305         184 :     if (!hasVideo())
    3306          23 :         return;
    3307             : 
    3308         641 :     for (auto& participant : infos) {
    3309         960 :         if (string_remove_suffix(participant.uri, '@') == account_.lock()->getUsername()
    3310        1098 :             && participant.device
    3311         618 :                 == std::dynamic_pointer_cast<JamiAccount>(account_.lock())->currentDeviceId()) {
    3312         417 :             for (auto iter = rtpStreams_.begin(); iter != rtpStreams_.end(); iter++) {
    3313         279 :                 if (!iter->mediaAttribute_ || iter->mediaAttribute_->type_ == MediaType::MEDIA_AUDIO) {
    3314         138 :                     continue;
    3315             :                 }
    3316         282 :                 auto localVideo = std::static_pointer_cast<video::VideoRtpSession>(iter->rtpSession_)
    3317         141 :                                     ->getVideoLocal().get();
    3318         141 :                 auto size = std::make_pair(10, 10);
    3319         141 :                 if (localVideo) {
    3320         129 :                     size = std::make_pair(localVideo->getWidth(), localVideo->getHeight());
    3321             :                 }
    3322         141 :                 const auto& mediaAttribute = iter->mediaAttribute_;
    3323         141 :                 if (participant.sinkId.find(mediaAttribute->label_) != std::string::npos) {
    3324         136 :                     local2RemoteSinks_[mediaAttribute->sourceUri_] = participant.sinkId;
    3325         136 :                     participant.sinkId = mediaAttribute->sourceUri_;
    3326         136 :                     participant.videoMuted = mediaAttribute->muted_;
    3327         136 :                     participant.w = size.first;
    3328         136 :                     participant.h = size.second;
    3329         136 :                     participant.x = 0;
    3330         136 :                     participant.y = 0;
    3331             :                 }
    3332             :             }
    3333             :         }
    3334             :     }
    3335             : 
    3336         161 :     std::vector<std::shared_ptr<video::VideoFrameActiveWriter>> sinks;
    3337         325 :     for (const auto& videoRtp : getRtpSessionList(MediaType::MEDIA_VIDEO)) {
    3338         328 :         auto& videoReceive = std::static_pointer_cast<video::VideoRtpSession>(videoRtp)
    3339         164 :                                  ->getVideoReceive();
    3340         164 :         if (!videoReceive)
    3341          24 :             continue;
    3342         140 :         sinks.emplace_back(
    3343         280 :             std::static_pointer_cast<video::VideoFrameActiveWriter>(videoReceive->getSink()));
    3344         161 :     }
    3345         161 :     auto conf = conf_.lock();
    3346         161 :     const auto& id = conf ? conf->getConfId() : getCallId();
    3347         161 :     Manager::instance().createSinkClients(id, infos, sinks, callSinksMap_);
    3348         207 : }
    3349             : #endif
    3350             : 
    3351             : std::vector<std::shared_ptr<RtpSession>>
    3352        1653 : SIPCall::getRtpSessionList(MediaType type) const
    3353             : {
    3354        1653 :     std::vector<std::shared_ptr<RtpSession>> rtpList;
    3355        1653 :     rtpList.reserve(rtpStreams_.size());
    3356        4705 :     for (auto const& stream : rtpStreams_) {
    3357        3052 :         if (type == MediaType::MEDIA_ALL || stream.rtpSession_->getMediaType() == type)
    3358        2175 :             rtpList.emplace_back(stream.rtpSession_);
    3359             :     }
    3360        1653 :     return rtpList;
    3361           0 : }
    3362             : 
    3363             : void
    3364         196 : SIPCall::monitor() const
    3365             : {
    3366         196 :     if (isSubcall())
    3367           0 :         return;
    3368         196 :     auto acc = getSIPAccount();
    3369         196 :     if (!acc) {
    3370           0 :         JAMI_ERROR("No account detected");
    3371           0 :         return;
    3372             :     }
    3373         588 :     JAMI_LOG("- Call {} with {}:", getCallId(), getPeerNumber());
    3374         588 :     JAMI_LOG("\t- Duration: {}", dht::print_duration(getCallDuration()));
    3375         555 :     for (const auto& stream : rtpStreams_)
    3376        1077 :         JAMI_LOG("\t- Media: {}", stream.mediaAttribute_->toString(true));
    3377             : #ifdef ENABLE_VIDEO
    3378         196 :     if (auto codec = getVideoCodec())
    3379         452 :         JAMI_LOG("\t- Video codec: {}", codec->name);
    3380             : #endif
    3381         196 :     if (auto transport = getIceMedia()) {
    3382         187 :         if (transport->isRunning())
    3383         417 :             JAMI_LOG("\t- Media stream(s): {}", transport->link());
    3384         196 :     }
    3385         196 : }
    3386             : 
    3387             : bool
    3388          17 : SIPCall::toggleRecording()
    3389             : {
    3390          17 :     pendingRecord_ = true;
    3391          17 :     if (not readyToRecord_)
    3392           5 :         return true;
    3393             : 
    3394             :     // add streams to recorder before starting the record
    3395          12 :     if (not Call::isRecording()) {
    3396           8 :         auto account = getSIPAccount();
    3397           8 :         if (!account) {
    3398           0 :             JAMI_ERR("No account detected");
    3399           0 :             return false;
    3400             :         }
    3401             :         auto title = fmt::format("Conversation at %TIMESTAMP between {} and {}",
    3402           8 :                                  account->getUserUri(),
    3403          16 :                                  peerUri_);
    3404           8 :         recorder_->setMetadata(title, ""); // use default description
    3405          21 :         for (const auto& rtpSession : getRtpSessionList())
    3406          21 :             rtpSession->initRecorder();
    3407           8 :     } else {
    3408           4 :         updateRecState(false);
    3409             :     }
    3410          12 :     pendingRecord_ = false;
    3411          12 :     auto state = Call::toggleRecording();
    3412          12 :     if (state)
    3413           8 :         updateRecState(state);
    3414          12 :     return state;
    3415             : }
    3416             : 
    3417             : void
    3418           4 : SIPCall::deinitRecorder()
    3419             : {
    3420          11 :     for (const auto& rtpSession : getRtpSessionList())
    3421          11 :         rtpSession->deinitRecorder();
    3422           4 : }
    3423             : 
    3424             : void
    3425         214 : SIPCall::InvSessionDeleter::operator()(pjsip_inv_session* inv) const noexcept
    3426             : {
    3427             :     // prevent this from getting accessed in callbacks
    3428             :     // JAMI_WARN: this is not thread-safe!
    3429         214 :     if (!inv)
    3430           0 :         return;
    3431         214 :     inv->mod_data[Manager::instance().sipVoIPLink().getModId()] = nullptr;
    3432             :     // NOTE: the counter is incremented by sipvoiplink (transaction_request_cb)
    3433         214 :     pjsip_inv_dec_ref(inv);
    3434             : }
    3435             : 
    3436             : bool
    3437         230 : SIPCall::createIceMediaTransport(bool isReinvite)
    3438             : {
    3439         230 :     auto mediaTransport = Manager::instance().getIceTransportFactory()->createTransport(getCallId());
    3440         230 :     if (mediaTransport) {
    3441         230 :         JAMI_DBG("[call:%s] Successfully created media ICE transport [ice:%p]",
    3442             :                  getCallId().c_str(),
    3443             :                  mediaTransport.get());
    3444             :     } else {
    3445           0 :         JAMI_ERR("[call:%s] Failed to create media ICE transport", getCallId().c_str());
    3446           0 :         return {};
    3447             :     }
    3448             : 
    3449         230 :     setIceMedia(mediaTransport, isReinvite);
    3450             : 
    3451         230 :     return mediaTransport != nullptr;
    3452         230 : }
    3453             : 
    3454             : bool
    3455         230 : SIPCall::initIceMediaTransport(bool master, std::optional<dhtnet::IceTransportOptions> options)
    3456             : {
    3457         230 :     auto acc = getSIPAccount();
    3458         230 :     if (!acc) {
    3459           0 :         JAMI_ERR("No account detected");
    3460           0 :         return false;
    3461             :     }
    3462             : 
    3463         230 :     JAMI_DBG("[call:%s] Init media ICE transport", getCallId().c_str());
    3464             : 
    3465         230 :     auto const& iceMedia = getIceMedia();
    3466         230 :     if (not iceMedia) {
    3467           0 :         JAMI_ERR("[call:%s] Invalid media ICE transport", getCallId().c_str());
    3468           0 :         return false;
    3469             :     }
    3470             : 
    3471         230 :     auto iceOptions = options == std::nullopt ? acc->getIceOptions() : *options;
    3472             : 
    3473         230 :     auto optOnInitDone = std::move(iceOptions.onInitDone);
    3474         230 :     auto optOnNegoDone = std::move(iceOptions.onNegoDone);
    3475         230 :     iceOptions.onInitDone = [w = weak(), cb = std::move(optOnInitDone)](bool ok) {
    3476         230 :         runOnMainThread([w = std::move(w), cb = std::move(cb), ok] {
    3477         230 :             auto call = w.lock();
    3478         230 :             if (cb)
    3479           0 :                 cb(ok);
    3480         230 :             if (!ok or !call or !call->waitForIceInit_.exchange(false))
    3481         230 :                 return;
    3482             : 
    3483           0 :             std::lock_guard lk {call->callMutex_};
    3484           0 :             auto rem_ice_attrs = call->sdp_->getIceAttributes();
    3485             :             // Init done but no remote_ice_attributes, the ice->start will be triggered later
    3486           0 :             if (rem_ice_attrs.ufrag.empty() or rem_ice_attrs.pwd.empty())
    3487           0 :                 return;
    3488           0 :             call->startIceMedia();
    3489         230 :         });
    3490         690 :     };
    3491         230 :     iceOptions.onNegoDone = [w = weak(), cb = std::move(optOnNegoDone)](bool ok) {
    3492         178 :         runOnMainThread([w = std::move(w), cb = std::move(cb), ok] {
    3493         178 :             if (cb)
    3494           0 :                 cb(ok);
    3495         178 :             if (auto call = w.lock()) {
    3496             :                 // The ICE is related to subcalls, but medias are handled by parent call
    3497         178 :                 std::lock_guard lk {call->callMutex_};
    3498         178 :                 call = call->isSubcall() ? std::dynamic_pointer_cast<SIPCall>(call->parent_) : call;
    3499         178 :                 if (!ok) {
    3500           0 :                     JAMI_ERR("[call:%s] Media ICE negotiation failed", call->getCallId().c_str());
    3501           0 :                     call->onFailure(EIO);
    3502           0 :                     return;
    3503             :                 }
    3504         178 :                 call->onIceNegoSucceed();
    3505         356 :             }
    3506             :         });
    3507         638 :     };
    3508             : 
    3509         230 :     iceOptions.master = master;
    3510         230 :     iceOptions.streamsCount = static_cast<unsigned>(rtpStreams_.size());
    3511             :     // Each RTP stream requires a pair of ICE components (RTP + RTCP).
    3512         230 :     iceOptions.compCountPerStream = ICE_COMP_COUNT_PER_STREAM;
    3513         230 :     iceOptions.qosType.reserve(rtpStreams_.size() * ICE_COMP_COUNT_PER_STREAM);
    3514         648 :     for (const auto& stream : rtpStreams_) {
    3515         418 :         iceOptions.qosType.push_back(stream.mediaAttribute_->type_ == MediaType::MEDIA_AUDIO
    3516         418 :                                      ? dhtnet::QosType::VOICE
    3517             :                                      : dhtnet::QosType::VIDEO);
    3518         418 :         iceOptions.qosType.push_back(dhtnet::QosType::CONTROL);
    3519             :     }
    3520             : 
    3521             :     // Init ICE.
    3522         230 :     iceMedia->initIceInstance(iceOptions);
    3523             : 
    3524         230 :     return true;
    3525         230 : }
    3526             : 
    3527             : std::vector<std::string>
    3528           0 : SIPCall::getLocalIceCandidates(unsigned compId) const
    3529             : {
    3530           0 :     std::lock_guard lk(transportMtx_);
    3531           0 :     if (not iceMedia_) {
    3532           0 :         JAMI_WARN("[call:%s] No media ICE transport", getCallId().c_str());
    3533           0 :         return {};
    3534             :     }
    3535           0 :     return iceMedia_->getLocalCandidates(compId);
    3536           0 : }
    3537             : 
    3538             : void
    3539        1402 : SIPCall::resetTransport(std::shared_ptr<dhtnet::IceTransport>&& transport)
    3540             : {
    3541             :     // Move the transport to another thread and destroy it there if possible
    3542        1402 :     if (transport) {
    3543         816 :         dht::ThreadPool::io().run(
    3544         816 :             [transport = std::move(transport)]() mutable { transport.reset(); });
    3545             :     }
    3546        1402 : }
    3547             : 
    3548             : void
    3549          78 : SIPCall::merge(Call& call)
    3550             : {
    3551          78 :     JAMI_DBG("[call:%s] Merge subcall %s", getCallId().c_str(), call.getCallId().c_str());
    3552             : 
    3553             :     // This static cast is safe as this method is private and overload Call::merge
    3554          78 :     auto& subcall = static_cast<SIPCall&>(call);
    3555             : 
    3556          78 :     std::lock(callMutex_, subcall.callMutex_);
    3557          78 :     std::lock_guard lk1 {callMutex_, std::adopt_lock};
    3558          78 :     std::lock_guard lk2 {subcall.callMutex_, std::adopt_lock};
    3559          78 :     inviteSession_ = std::move(subcall.inviteSession_);
    3560          78 :     if (inviteSession_)
    3561          78 :         inviteSession_->mod_data[Manager::instance().sipVoIPLink().getModId()] = this;
    3562          78 :     setSipTransport(std::move(subcall.sipTransport_), std::move(subcall.contactHeader_));
    3563          78 :     sdp_ = std::move(subcall.sdp_);
    3564          78 :     peerHolding_ = subcall.peerHolding_;
    3565          78 :     upnp_ = std::move(subcall.upnp_);
    3566          78 :     localAudioPort_ = subcall.localAudioPort_;
    3567          78 :     localVideoPort_ = subcall.localVideoPort_;
    3568          78 :     peerUserAgent_ = subcall.peerUserAgent_;
    3569          78 :     peerSupportMultiStream_ = subcall.peerSupportMultiStream_;
    3570          78 :     peerSupportMultiAudioStream_ = subcall.peerSupportMultiAudioStream_;
    3571          78 :     peerSupportMultiIce_ = subcall.peerSupportMultiIce_;
    3572          78 :     peerAllowedMethods_ = subcall.peerAllowedMethods_;
    3573          78 :     peerSupportReuseIceInReinv_ = subcall.peerSupportReuseIceInReinv_;
    3574             : 
    3575          78 :     Call::merge(subcall);
    3576          78 :     if (isIceEnabled())
    3577          78 :         startIceMedia();
    3578          78 : }
    3579             : 
    3580             : bool
    3581         362 : SIPCall::remoteHasValidIceAttributes() const
    3582             : {
    3583         362 :     if (not sdp_) {
    3584           0 :         throw std::runtime_error("Must have a valid SDP Session");
    3585             :     }
    3586             : 
    3587         362 :     auto rem_ice_attrs = sdp_->getIceAttributes();
    3588         362 :     if (rem_ice_attrs.ufrag.empty()) {
    3589          39 :         JAMI_DBG("[call:%s] No ICE username fragment attribute in remote SDP", getCallId().c_str());
    3590          39 :         return false;
    3591             :     }
    3592             : 
    3593         323 :     if (rem_ice_attrs.pwd.empty()) {
    3594           0 :         JAMI_DBG("[call:%s] No ICE password attribute in remote SDP", getCallId().c_str());
    3595           0 :         return false;
    3596             :     }
    3597             : 
    3598         323 :     return true;
    3599         362 : }
    3600             : 
    3601             : void
    3602         408 : SIPCall::setIceMedia(std::shared_ptr<dhtnet::IceTransport> ice, bool isReinvite)
    3603             : {
    3604         408 :     std::lock_guard lk(transportMtx_);
    3605             : 
    3606         408 :     if (isReinvite) {
    3607          27 :         JAMI_DBG("[call:%s] Setting re-invite ICE session [%p]", getCallId().c_str(), ice.get());
    3608          27 :         resetTransport(std::move(reinvIceMedia_));
    3609          27 :         reinvIceMedia_ = std::move(ice);
    3610             :     } else {
    3611         381 :         JAMI_DBG("[call:%s] Setting ICE session [%p]", getCallId().c_str(), ice.get());
    3612         381 :         resetTransport(std::move(iceMedia_));
    3613         381 :         iceMedia_ = std::move(ice);
    3614             :     }
    3615         408 : }
    3616             : 
    3617             : void
    3618         178 : SIPCall::switchToIceReinviteIfNeeded()
    3619             : {
    3620         178 :     std::lock_guard lk(transportMtx_);
    3621             : 
    3622         178 :     if (reinvIceMedia_) {
    3623          20 :         JAMI_DBG("[call:%s] Switching to re-invite ICE session [%p]",
    3624             :                  getCallId().c_str(),
    3625             :                  reinvIceMedia_.get());
    3626          20 :         std::swap(reinvIceMedia_, iceMedia_);
    3627             :     }
    3628             : 
    3629         178 :     resetTransport(std::move(reinvIceMedia_));
    3630         178 : }
    3631             : 
    3632             : void
    3633         108 : SIPCall::setupIceResponse(bool isReinvite)
    3634             : {
    3635         108 :     JAMI_DBG("[call:%s] Setup ICE response", getCallId().c_str());
    3636             : 
    3637         108 :     auto account = getSIPAccount();
    3638         108 :     if (not account) {
    3639           0 :         JAMI_ERR("No account detected");
    3640             :     }
    3641             : 
    3642         108 :     auto opt = account->getIceOptions();
    3643             : 
    3644             :     // Attempt to use the discovered public address. If not available,
    3645             :     // fallback on local address.
    3646         108 :     opt.accountPublicAddr = account->getPublishedIpAddress();
    3647         108 :     if (opt.accountPublicAddr) {
    3648         100 :         opt.accountLocalAddr = dhtnet::ip_utils::getInterfaceAddr(account->getLocalInterface(),
    3649         100 :                                                           opt.accountPublicAddr.getFamily());
    3650             :     } else {
    3651             :         // Just set the local address for both, most likely the account is not
    3652             :         // registered.
    3653           8 :         opt.accountLocalAddr = dhtnet::ip_utils::getInterfaceAddr(account->getLocalInterface(), AF_INET);
    3654           8 :         opt.accountPublicAddr = opt.accountLocalAddr;
    3655             :     }
    3656             : 
    3657         108 :     if (not opt.accountLocalAddr) {
    3658           0 :         JAMI_ERR("[call:%s] No local address, unable to initialize ICE", getCallId().c_str());
    3659           0 :         onFailure(EIO);
    3660           0 :         return;
    3661             :     }
    3662             : 
    3663         108 :     if (not createIceMediaTransport(isReinvite) or not initIceMediaTransport(false, opt)) {
    3664           0 :         JAMI_ERR("[call:%s] ICE initialization failed", getCallId().c_str());
    3665             :         // Fatal condition
    3666             :         // TODO: what's SIP rfc says about that?
    3667             :         // (same question in startIceMedia)
    3668           0 :         onFailure(EIO);
    3669           0 :         return;
    3670             :     }
    3671             : 
    3672             :     // Media transport changed, must restart the media.
    3673         108 :     mediaRestartRequired_ = true;
    3674             : 
    3675             :     // WARNING: This call blocks! (need ICE init done)
    3676         108 :     addLocalIceAttributes();
    3677         108 : }
    3678             : 
    3679             : bool
    3680         186 : SIPCall::isIceRunning() const
    3681             : {
    3682         186 :     std::lock_guard lk(transportMtx_);
    3683         372 :     return iceMedia_ and iceMedia_->isRunning();
    3684         186 : }
    3685             : 
    3686             : std::unique_ptr<dhtnet::IceSocket>
    3687         664 : SIPCall::newIceSocket(unsigned compId)
    3688             : {
    3689         664 :     return std::unique_ptr<dhtnet::IceSocket> {new dhtnet::IceSocket(getIceMedia(), compId)};
    3690             : }
    3691             : 
    3692             : void
    3693         525 : SIPCall::rtpSetupSuccess()
    3694             : {
    3695         525 :     std::lock_guard lk {setupSuccessMutex_};
    3696             : 
    3697         525 :     readyToRecord_ = true; // We're ready to record whenever a stream is ready
    3698             : 
    3699         525 :     auto previousState = isAudioOnly_;
    3700         525 :     auto newState = !hasVideo();
    3701             : 
    3702         525 :     if (previousState != newState && Call::isRecording()) {
    3703           1 :         deinitRecorder();
    3704           1 :         toggleRecording();
    3705           1 :         pendingRecord_ = true;
    3706             :     }
    3707         525 :     isAudioOnly_ = newState;
    3708             : 
    3709         525 :     if (pendingRecord_ && readyToRecord_)
    3710           5 :         toggleRecording();
    3711         525 : }
    3712             : 
    3713             : void
    3714          16 : SIPCall::peerRecording(bool state)
    3715             : {
    3716          16 :     auto conference = conf_.lock();
    3717          16 :     const std::string& id = conference ? conference->getConfId() : getCallId();
    3718          16 :     if (state) {
    3719          10 :         JAMI_WARN("[call:%s] Peer is recording", getCallId().c_str());
    3720          10 :         emitSignal<libjami::CallSignal::RemoteRecordingChanged>(id, getPeerNumber(), true);
    3721             :     } else {
    3722           6 :         JAMI_WARN("Peer stopped recording");
    3723           6 :         emitSignal<libjami::CallSignal::RemoteRecordingChanged>(id, getPeerNumber(), false);
    3724             :     }
    3725          16 :     peerRecording_ = state;
    3726          16 :     if (auto conf = conf_.lock())
    3727          16 :         conf->updateRecording();
    3728          16 : }
    3729             : 
    3730             : void
    3731           5 : SIPCall::peerMuted(bool muted, int streamIdx)
    3732             : {
    3733           5 :     if (muted) {
    3734           5 :         JAMI_WARN("Peer muted");
    3735             :     } else {
    3736           0 :         JAMI_WARN("Peer unmuted");
    3737             :     }
    3738             : 
    3739           5 :     if (streamIdx == -1) {
    3740          10 :         for (const auto& audioRtp : getRtpSessionList(MediaType::MEDIA_AUDIO))
    3741          10 :             audioRtp->setMuted(muted, RtpSession::Direction::RECV);
    3742           0 :     } else if (streamIdx > -1 && streamIdx < static_cast<int>(rtpStreams_.size())) {
    3743           0 :         auto& stream = rtpStreams_[streamIdx];
    3744           0 :         if (stream.rtpSession_ && stream.rtpSession_->getMediaType() == MediaType::MEDIA_AUDIO)
    3745           0 :             stream.rtpSession_->setMuted(muted, RtpSession::Direction::RECV);
    3746             :     }
    3747             : 
    3748           5 :     peerMuted_ = muted;
    3749           5 :     if (auto conf = conf_.lock())
    3750           5 :         conf->updateMuted();
    3751           5 : }
    3752             : 
    3753             : void
    3754           0 : SIPCall::peerVoice(bool voice)
    3755             : {
    3756           0 :     peerVoice_ = voice;
    3757             : 
    3758           0 :     if (auto conference = conf_.lock()) {
    3759           0 :         conference->updateVoiceActivity();
    3760             :     } else {
    3761             :         // one-to-one call
    3762             :         // maybe emit signal with partner voice activity
    3763           0 :     }
    3764           0 : }
    3765             : 
    3766             : } // namespace jami

Generated by: LCOV version 1.14