LCOV - code coverage report
Current view: top level - foo/src/sip - sipcall.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 13 13 100.0 %
Date: 2026-01-22 10:39:23 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2026 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             : #pragma once
      18             : 
      19             : #ifdef HAVE_CONFIG_H
      20             : #include "config.h"
      21             : #endif
      22             : 
      23             : #include "call.h"
      24             : #include <dhtnet/ice_transport.h>
      25             : #include "connectivity/sip_utils.h"
      26             : #include "media/media_codec.h" // for MediaType enum
      27             : #include "sip/sdp.h"
      28             : 
      29             : #include "media/rtp_session.h"
      30             : #ifdef ENABLE_VIDEO
      31             : #include "media/video/video_receive_thread.h"
      32             : #include "media/video/video_rtp_session.h"
      33             : #endif
      34             : #ifdef ENABLE_PLUGIN
      35             : #include "plugin/streamdata.h"
      36             : #endif
      37             : #include "noncopyable.h"
      38             : 
      39             : #include <memory>
      40             : #include <optional>
      41             : 
      42             : extern "C" {
      43             : #include <pjsip/sip_config.h>
      44             : struct pjsip_evsub;
      45             : struct pjsip_inv_session;
      46             : struct pjmedia_sdp_session;
      47             : struct pj_ice_sess_cand;
      48             : struct pjsip_rx_data;
      49             : }
      50             : 
      51             : namespace dhtnet {
      52             : class IceSocket;
      53             : namespace upnp {
      54             : class Controller;
      55             : }
      56             : } // namespace dhtnet
      57             : 
      58             : namespace jami {
      59             : 
      60             : class Sdp;
      61             : class SIPAccountBase;
      62             : class SipTransport;
      63             : class AudioRtpSession;
      64             : 
      65             : using IceCandidate = pj_ice_sess_cand;
      66             : 
      67             : /**
      68             :  * @file sipcall.h
      69             :  * @brief SIPCall are SIP implementation of a normal Call
      70             :  */
      71             : class SIPCall : public Call
      72             : {
      73             : private:
      74             :     using clock = std::chrono::steady_clock;
      75             :     using time_point = clock::time_point;
      76             : 
      77             :     NON_COPYABLE(SIPCall);
      78             : 
      79             : public:
      80             :     static constexpr LinkType LINK_TYPE = LinkType::SIP;
      81             : 
      82             :     struct RtpStream
      83             :     {
      84             :         std::shared_ptr<RtpSession> rtpSession_ {};
      85             :         std::shared_ptr<MediaAttribute> mediaAttribute_ {};
      86             :         std::shared_ptr<MediaAttribute> remoteMediaAttribute_;
      87             :         std::unique_ptr<dhtnet::IceSocket> rtpSocket_;
      88             :         std::unique_ptr<dhtnet::IceSocket> rtcpSocket_;
      89             :     };
      90             : 
      91             :     /**
      92             :      * Destructor
      93             :      */
      94             :     ~SIPCall();
      95             : 
      96             :     /**
      97             :      * Constructor
      98             :      * @param id The call identifier
      99             :      * @param type The type of the call (incoming/outgoing)
     100             :      * @param mediaList A list of medias to include in the call
     101             :      */
     102             :     SIPCall(const std::shared_ptr<SIPAccountBase>& account,
     103             :             const std::string& id,
     104             :             Call::CallType type,
     105             :             const std::vector<libjami::MediaMap>& mediaList);
     106             : 
     107             :     // Inherited from Call class
     108         755 :     LinkType getLinkType() const override { return LINK_TYPE; }
     109             : 
     110             :     // Override of Call class
     111             : private:
     112             :     void merge(Call& call) override; // not public - only called by Call
     113             : 
     114             : public:
     115             :     void answer(const std::vector<libjami::MediaMap>& mediaList) override;
     116             :     bool checkMediaChangeRequest(const std::vector<libjami::MediaMap>& remoteMediaList) override;
     117             :     void handleMediaChangeRequest(const std::vector<libjami::MediaMap>& remoteMediaList) override;
     118             :     void answerMediaChangeRequest(const std::vector<libjami::MediaMap>& mediaList, bool isRemote = false) override;
     119             :     void hangup(int reason) override;
     120             :     void refuse() override;
     121             :     void transfer(const std::string& to) override;
     122             :     bool attendedTransfer(const std::string& to) override;
     123             :     bool onhold(OnReadyCb&& cb) override;
     124             :     bool offhold(OnReadyCb&& cb) override;
     125             :     void switchInput(const std::string& resource = {}) override;
     126             :     void peerHungup() override;
     127             :     void carryingDTMFdigits(char code) override;
     128             :     bool requestMediaChange(const std::vector<libjami::MediaMap>& mediaList) override;
     129             :     std::vector<libjami::MediaMap> currentMediaList() const override;
     130             :     void sendTextMessage(const std::map<std::string, std::string>& messages, const std::string& from) override;
     131             :     void removeCall() override;
     132             :     void muteMedia(const std::string& mediaType, bool isMuted) override;
     133             :     std::vector<MediaAttribute> getMediaAttributeList() const override;
     134             :     std::map<std::string, bool> getAudioStreams() const override;
     135             :     void restartMediaSender() override;
     136             :     std::shared_ptr<SystemCodecInfo> getAudioCodec() const override;
     137             :     std::shared_ptr<SystemCodecInfo> getVideoCodec() const override;
     138             :     void sendKeyframe(int streamIdx = -1) override;
     139             :     bool isIceEnabled() const override;
     140             :     std::map<std::string, std::string> getDetails() const override;
     141             :     void enterConference(std::shared_ptr<Conference> conference) override;
     142             :     void exitConference() override;
     143             : #ifdef ENABLE_VIDEO
     144             :     std::mutex sinksMtx_;
     145             :     void createSinks(ConfInfo& infos) override;
     146             :     std::map<std::string, std::shared_ptr<video::SinkClient>> callSinksMap_ {};
     147             :     std::map<std::string, std::string> local2RemoteSinks_ {};
     148             : #endif
     149             :     bool hasVideo() const override;
     150             : 
     151             :     // TODO: cleanup this (used by conference + Call::getDetails() (and clients can use this))
     152             :     bool isCaptureDeviceMuted(const MediaType& mediaType) const override;
     153        1968 :     bool isSrtpEnabled() const { return srtpEnabled_; }
     154             :     // End of override of Call class
     155             : 
     156             :     // Override of Recordable class
     157             :     bool toggleRecording() override;
     158             :     // End of override of Recordable class
     159             : 
     160             :     // Override PeerRecorder
     161             :     void peerRecording(bool state) override;
     162             :     void peerMuted(bool state, int streamIdx) override;
     163             :     void peerVoice(bool state) override;
     164             :     // end override PeerRecorder
     165             : 
     166             :     void monitor() const override;
     167             : 
     168             :     /**
     169             :      * Set peer's User-Agent found in the message header
     170             :      */
     171             :     void setPeerUaVersion(std::string_view ua);
     172             : 
     173             :     /**
     174             :      * Set peer's allowed methods
     175             :      */
     176             :     void setPeerAllowMethods(std::vector<std::string> methods);
     177             : 
     178             :     /**
     179             :      * Check if a SIP method is allowed by peer
     180             :      */
     181             :     bool isSipMethodAllowedByPeer(const std::string_view method) const;
     182             : 
     183             :     /**
     184             :      * Return the SDP's manager of this call
     185             :      */
     186         667 :     Sdp& getSDP() { return *sdp_; }
     187             : 
     188             :     // Implementation of events reported by SipVoipLink.
     189             :     /**
     190             :      * Call is in ringing state on peer's side
     191             :      */
     192             :     void onPeerRinging();
     193             :     /**
     194             :      * Peer answered the call
     195             :      */
     196             :     void onAnswered();
     197             :     /**
     198             :      * Called to report server/internal errors
     199             :      * @param cause Optional error code
     200             :      */
     201             :     void onFailure(signed cause = 0);
     202             :     /**
     203             :      * Peer answered busy
     204             :      */
     205             :     void onBusyHere();
     206             :     /**
     207             :      * Peer closed the connection
     208             :      */
     209             :     void onClosed();
     210             : 
     211             :     pj_status_t onReceiveReinvite(const pjmedia_sdp_session* offer, pjsip_rx_data* rdata);
     212             :     void onReceiveOfferIn200OK(const pjmedia_sdp_session* offer);
     213             : 
     214             :     /**
     215             :      * Called when the media negotiation (SDP offer/answer) has
     216             :      * completed.
     217             :      */
     218             :     void onMediaNegotiationComplete();
     219             :     // End fo SiPVoipLink events
     220             : 
     221             :     const std::string& getContactHeader() const;
     222             : 
     223             :     void setSipTransport(const std::shared_ptr<SipTransport>& transport, const std::string& contactHdr = {});
     224             : 
     225         565 :     SipTransport* getTransport() { return sipTransport_.get(); }
     226             : 
     227             :     void sendSIPInfo(std::string_view body, std::string_view subtype);
     228             : 
     229             :     void requestKeyframe(int streamIdx = -1);
     230             : 
     231             :     void updateRecState(bool state) override;
     232             : 
     233             :     std::shared_ptr<SIPAccountBase> getSIPAccount() const;
     234             : 
     235             :     bool remoteHasValidIceAttributes() const;
     236             :     void addLocalIceAttributes();
     237             : 
     238        2001 :     std::shared_ptr<dhtnet::IceTransport> getIceMedia() const
     239             :     {
     240        2001 :         std::lock_guard lk(transportMtx_);
     241        4002 :         return reinvIceMedia_ ? reinvIceMedia_ : iceMedia_;
     242        2001 :     };
     243             : 
     244             :     // Set ICE instance. Must be called only for sub-calls
     245             :     void setIceMedia(std::shared_ptr<dhtnet::IceTransport> ice, bool isReinvite = false);
     246             : 
     247             :     // Switch to re-invite ICE media if needed
     248             :     void switchToIceReinviteIfNeeded();
     249             : 
     250             :     /**
     251             :      * Setup ICE locally to answer to an ICE offer. The ICE session has
     252             :      * the controlled role (slave)
     253             :      */
     254             :     void setupIceResponse(bool isReinvite = false);
     255             : 
     256             :     void terminateSipSession(int status);
     257             : 
     258             :     /**
     259             :      * The invite session to be reused in case of transfer
     260             :      */
     261             :     struct InvSessionDeleter
     262             :     {
     263             :         void operator()(pjsip_inv_session*) const noexcept;
     264             :     };
     265             : 
     266             : #ifdef ENABLE_VIDEO
     267             :     void setRotation(int streamIdx, int rotation);
     268             : #endif
     269             :     // Get the list of current RTP sessions
     270             :     std::vector<std::shared_ptr<RtpSession>> getRtpSessionList(MediaType type = MediaType::MEDIA_ALL) const;
     271             :     static size_t getActiveMediaStreamCount(const std::vector<MediaAttribute>& mediaAttrList);
     272             :     void setActiveMediaStream(const std::string& accountUri,
     273             :                               const std::string& deviceId,
     274             :                               const std::string& streamId,
     275             :                               const bool& state);
     276             : 
     277           1 :     void setPeerRegisteredName(const std::string& name) { peerRegisteredName_ = name; }
     278             : 
     279         307 :     void setPeerUri(const std::string& peerUri) { peerUri_ = peerUri; }
     280             : 
     281          68 :     std::string_view peerUri() const { return peerUri_; }
     282             : 
     283             :     // Create a new ICE media session. If we already have an instance,
     284             :     // it will be destroyed first.
     285             :     bool createIceMediaTransport(bool isReinvite);
     286             : 
     287             :     // Initialize the ICE session.
     288             :     // The initialization is performed asynchronously, i.e, the instance
     289             :     // may not be ready to use when this method returns.
     290             :     bool initIceMediaTransport(bool master, std::optional<dhtnet::IceTransportOptions> options = std::nullopt);
     291             : 
     292             :     std::vector<std::string> getLocalIceCandidates(unsigned compId) const;
     293             : 
     294             :     void setInviteSession(pjsip_inv_session* inviteSession = nullptr);
     295             : 
     296             :     std::unique_ptr<pjsip_inv_session, InvSessionDeleter> inviteSession_;
     297             : 
     298             :     inline std::weak_ptr<const SIPCall> weak() const { return std::weak_ptr<const SIPCall>(shared()); }
     299        2800 :     inline std::weak_ptr<SIPCall> weak() { return std::weak_ptr<SIPCall>(shared()); }
     300             :     /**
     301             :      * Announce to the client that medias are successfully negotiated
     302             :      */
     303             :     void reportMediaNegotiationStatus();
     304             : 
     305             :     const std::vector<RtpStream>& getRtpStreams() const { return rtpStreams_; }
     306             : 
     307             : private:
     308             :     void generateMediaPorts();
     309             : 
     310             :     void openPortsUPnP();
     311             : 
     312             :     bool isIceRunning() const;
     313             : 
     314             :     std::unique_ptr<dhtnet::IceSocket> newIceSocket(unsigned compId);
     315             : 
     316             :     void deinitRecorder();
     317             : 
     318             :     void rtpSetupSuccess();
     319             : 
     320             :     void setupVoiceCallback(const std::shared_ptr<RtpSession>& rtpSession);
     321             : 
     322             :     void sendMuteState(bool state);
     323             :     void sendVoiceActivity(std::string_view streamId, bool state);
     324             : 
     325             :     void resetTransport(std::shared_ptr<dhtnet::IceTransport>&& transport);
     326             : 
     327             :     /**
     328             :      * Send device orientation through SIP INFO
     329             :      * @param streamIdx  The stream to rotate
     330             :      * @param rotation   Device orientation (0/90/180/270) (counterclockwise)
     331             :      */
     332             :     void setVideoOrientation(int streamIdx, int rotation);
     333             : 
     334             :     mutable std::mutex transportMtx_ {};
     335             : 
     336             : #ifdef ENABLE_PLUGIN
     337             :     /**
     338             :      * Call Streams and some typedefs
     339             :      */
     340             :     using AVMediaStream = Observable<std::shared_ptr<MediaFrame>>;
     341             :     using MediaStreamSubject = PublishMapSubject<std::shared_ptr<MediaFrame>, AVFrame*>;
     342             : 
     343             :     /**
     344             :      * @brief createCallAVStream
     345             :      * Creates a call AV stream like video input, video receive, audio input or audio receive
     346             :      * @param StreamData The type of the stream (audio/video, input/output,
     347             :      * @param streamSource
     348             :      * @param mediaStreamSubject
     349             :      */
     350             :     void createCallAVStream(const StreamData& StreamData,
     351             :                             AVMediaStream& streamSource,
     352             :                             const std::shared_ptr<MediaStreamSubject>& mediaStreamSubject);
     353             :     /**
     354             :      * @brief createCallAVStreams
     355             :      * Creates all Call AV Streams (2 if audio, 4 if audio video)
     356             :      */
     357             :     void createCallAVStreams();
     358             : 
     359             :     /**
     360             :      * @brief Detach all plugins from call streams;
     361             :      */
     362             :     void clearCallAVStreams();
     363             : 
     364             :     std::mutex avStreamsMtx_ {};
     365             :     std::map<std::string, std::shared_ptr<MediaStreamSubject>> callAVStreams;
     366             : #endif // ENABLE_PLUGIN
     367             : 
     368             :     void setCallMediaLocal();
     369             :     void startIceMedia();
     370             :     void onIceNegoSucceed();
     371             :     void setupNegotiatedMedia();
     372             :     void startAllMedia();
     373             :     void stopAllMedia();
     374             : 
     375             :     /**
     376             :      * Transfer method used for both type of transfer
     377             :      */
     378             :     bool transferCommon(const pj_str_t* dst);
     379             : 
     380             :     bool internalOffHold(const std::function<void()>& SDPUpdateFunc);
     381             : 
     382             :     bool hold();
     383             : 
     384             :     bool unhold();
     385             : 
     386             :     // Update the attributes of a media stream
     387             :     void updateMediaStream(const MediaAttribute& newMediaAttr, size_t streamIdx);
     388             :     bool updateAllMediaStreams(const std::vector<MediaAttribute>& mediaAttrList, bool isRemote);
     389             :     // Check if a SIP re-invite must be sent to negotiate the new media
     390             :     bool isReinviteRequired(const std::vector<MediaAttribute>& mediaAttrList);
     391             :     // Check if a new ICE media session is needed when performing a re-invite
     392             :     bool isNewIceMediaRequired(const std::vector<MediaAttribute>& mediaAttrList);
     393             :     void requestReinvite(const std::vector<MediaAttribute>& mediaAttrList, bool needNewIce);
     394             :     int SIPSessionReinvite(const std::vector<MediaAttribute>& mediaAttrList, bool needNewIce);
     395             :     int SIPSessionReinvite();
     396             :     // Add a media stream to the call.
     397             :     void addMediaStream(const MediaAttribute& mediaAttr);
     398             :     // Init media streams
     399             :     size_t initMediaStreams(const std::vector<MediaAttribute>& mediaAttrList);
     400             :     // Create a new stream from SDP description.
     401             :     void createRtpSession(RtpStream& rtpStream);
     402             :     // Configure the RTP session from SDP description.
     403             :     void configureRtpSession(const std::shared_ptr<RtpSession>& rtpSession,
     404             :                              const std::shared_ptr<MediaAttribute>& mediaAttr,
     405             :                              const MediaDescription& localMedia,
     406             :                              const MediaDescription& remoteMedia);
     407             :     // Find the stream index with the matching label
     408             :     int findRtpStreamIndex(const std::string& label) const;
     409             : 
     410             :     std::vector<IceCandidate> getAllRemoteCandidates(dhtnet::IceTransport& transport) const;
     411             : 
     412             :     inline std::shared_ptr<const SIPCall> shared() const
     413             :     {
     414             :         return std::static_pointer_cast<const SIPCall>(shared_from_this());
     415             :     }
     416        2800 :     inline std::shared_ptr<SIPCall> shared() { return std::static_pointer_cast<SIPCall>(shared_from_this()); }
     417             : 
     418             :     // Peer's User-Agent.
     419             :     std::string peerUserAgent_ {};
     420             :     // Flag to indicate if the peer's Daemon version supports multi-stream.
     421             :     bool peerSupportMultiStream_ {false};
     422             :     // Flag to indicate if the peer's Daemon version supports multi-stream.
     423             :     bool peerSupportMultiAudioStream_ {false};
     424             :     // Flag to indicate if the peer's Daemon version can negotiate more than 2 ICE medias
     425             :     bool peerSupportMultiIce_ {false};
     426             : 
     427             :     // Flag to indicate if the peer's Daemon version supports re-invite
     428             :     // without ICE renegotiation.
     429             :     bool peerSupportReuseIceInReinv_ {false};
     430             : 
     431             :     // Peer's allowed methods.
     432             :     std::vector<std::string> peerAllowedMethods_;
     433             : 
     434             :     // Vector holding the current RTP sessions.
     435             :     std::vector<RtpStream> rtpStreams_;
     436             : 
     437             :     /**
     438             :      * Hold the transport used for SIP communication.
     439             :      * Will be different from the account registration transport for
     440             :      * non-IP2IP calls.
     441             :      */
     442             :     std::shared_ptr<SipTransport> sipTransport_ {};
     443             : 
     444             :     /**
     445             :      * The SDP session
     446             :      */
     447             :     std::unique_ptr<Sdp> sdp_ {};
     448             :     bool peerHolding_ {false};
     449             : 
     450             :     bool isWaitingForIceAndMedia_ {false};
     451             :     enum class Request { HoldingOn, HoldingOff, SwitchInput, NoRequest };
     452             :     Request remainingRequest_ {Request::NoRequest};
     453             : 
     454             :     std::string peerRegisteredName_ {};
     455             : 
     456             :     std::string contactHeader_ {};
     457             : 
     458             :     std::shared_ptr<dhtnet::upnp::Controller> upnp_;
     459             : 
     460             :     /** Local audio port, as seen by me. */
     461             :     unsigned int localAudioPort_ {0};
     462             :     /** Local video port, as seen by me. */
     463             :     unsigned int localVideoPort_ {0};
     464             : 
     465             :     bool mediaRestartRequired_ {true};
     466             :     bool enableIce_ {true};
     467             :     bool srtpEnabled_ {false};
     468             :     bool rtcpMuxEnabled_ {false};
     469             : 
     470             :     // ICE media transport
     471             :     std::shared_ptr<dhtnet::IceTransport> iceMedia_;
     472             :     // Re-invite (temporary) ICE media transport.
     473             :     std::shared_ptr<dhtnet::IceTransport> reinvIceMedia_;
     474             : 
     475             :     std::string peerUri_ {};
     476             : 
     477             :     bool readyToRecord_ {false};
     478             :     bool pendingRecord_ {false};
     479             : 
     480             :     time_point lastKeyFrameReq_ {time_point::min()};
     481             : 
     482             :     OnReadyCb holdCb_ {};
     483             :     OnReadyCb offHoldCb_ {};
     484             : 
     485             :     std::atomic_bool waitForIceInit_ {false};
     486             : 
     487             :     void detachAudioFromConference();
     488             : 
     489             :     std::mutex mediaStateMutex_;
     490             : #ifdef ENABLE_VIDEO
     491             :     int rotation_ {0};
     492             : #endif
     493             : 
     494             :     std::string mediaPlayerId_ {};
     495             : };
     496             : 
     497             : // Helpers
     498             : 
     499             : /**
     500             :  * Obtain a shared smart pointer of instance
     501             :  */
     502             : inline std::shared_ptr<SIPCall>
     503             : getPtr(SIPCall& call)
     504             : {
     505             :     return std::static_pointer_cast<SIPCall>(call.shared_from_this());
     506             : }
     507             : 
     508             : } // namespace jami

Generated by: LCOV version 1.14