LCOV - code coverage report
Current view: top level - foo/src/sip - sdp.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 12 19 63.2 %
Date: 2025-12-18 10:07:43 Functions: 6 11 54.5 %

          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             : #pragma once
      18             : 
      19             : #include "noncopyable.h"
      20             : #include "sdes_negotiator.h"
      21             : #include "media/media_codec.h"
      22             : #include "media/media_attribute.h"
      23             : #include "connectivity/sip_utils.h"
      24             : 
      25             : #include <dhtnet/ip_utils.h>
      26             : #include <dhtnet/ice_transport.h>
      27             : 
      28             : #include <pjmedia/sdp.h>
      29             : #include <pjmedia/sdp_neg.h>
      30             : #include <pjsip/sip_transport.h>
      31             : #include <pjlib.h>
      32             : #include <pjsip_ua.h>
      33             : #include <pjmedia/errno.h>
      34             : #include <pj/pool.h>
      35             : #include <pj/assert.h>
      36             : 
      37             : #include <map>
      38             : #include <vector>
      39             : #include <string>
      40             : #include <stdexcept>
      41             : 
      42             : namespace jami {
      43             : 
      44             : namespace test {
      45             : class SDPTest;
      46             : }
      47             : 
      48             : class AudioCodec;
      49             : 
      50             : class SdpException : public std::runtime_error
      51             : {
      52             : public:
      53           0 :     SdpException(const std::string& str = "")
      54           0 :         : std::runtime_error("SDP: SdpException occurred: " + str)
      55           0 :     {}
      56             : };
      57             : 
      58             : enum class SdpDirection { OFFER, ANSWER, NONE };
      59             : 
      60             : class Sdp
      61             : {
      62             : public:
      63             :     /*
      64             :      * Class Constructor.
      65             :      *
      66             :      * @param memory pool
      67             :      */
      68             :     Sdp(const std::string& id);
      69             : 
      70             :     ~Sdp();
      71             : 
      72             :     /**
      73             :      * Set the local media capabilities.
      74             :      * @param List of codec in preference order
      75             :      */
      76             :     void setLocalMediaCapabilities(MediaType type, const std::vector<std::shared_ptr<SystemCodecInfo>>& selectedCodecs);
      77             : 
      78             :     /**
      79             :      *  Read accessor. Get the local passive sdp session information before negotiation
      80             :      *
      81             :      *  @return The structure that describes a SDP session
      82             :      */
      83         242 :     pjmedia_sdp_session* getLocalSdpSession() { return localSession_; }
      84             : 
      85             :     const pjmedia_sdp_session* getActiveLocalSdpSession() const { return activeLocalSession_; }
      86             : 
      87             :     /**
      88             :      * Read accessor. Get the remote passive sdp session information before negotiation
      89             :      *
      90             :      * @return The structure that describe the SDP session
      91             :      */
      92          25 :     pjmedia_sdp_session* getRemoteSdpSession() { return remoteSession_; }
      93             : 
      94         176 :     const pjmedia_sdp_session* getActiveRemoteSdpSession() const { return activeRemoteSession_; }
      95             : 
      96             :     /**
      97             :      * Set the negotiated sdp offer from the sip payload.
      98             :      *
      99             :      * @param sdp   the negotiated offer
     100             :      */
     101             :     void setActiveLocalSdpSession(const pjmedia_sdp_session* sdp);
     102             : 
     103             :     /**
     104             :      * Retrieve the negotiated sdp offer from the sip payload.
     105             :      *
     106             :      * @param sdp   the negotiated offer
     107             :      */
     108             :     void setActiveRemoteSdpSession(const pjmedia_sdp_session* sdp);
     109             : 
     110             :     /*
     111             :      * On building an invite outside a dialog, build the local offer and create the
     112             :      * SDP negotiator instance with it.
     113             :      * @returns true if offer was created, false otherwise
     114             :      */
     115             :     bool createOffer(const std::vector<MediaAttribute>& mediaList);
     116             : 
     117             :     void setReceivedOffer(const pjmedia_sdp_session* remote);
     118             : 
     119             :     /**
     120             :      * Build a new SDP answer using mediaList.
     121             :      *
     122             :      * @param mediaList The list of media attributes to build the answer
     123             :      */
     124             :     bool processIncomingOffer(const std::vector<MediaAttribute>& mediaList);
     125             : 
     126             :     /**
     127             :      * Start the sdp negotiation.
     128             :      */
     129             :     bool startNegotiation();
     130             : 
     131             :     /**
     132             :      * Remove all media in the session media vector.
     133             :      */
     134             :     void cleanSessionMedia();
     135             : 
     136             :     /*
     137             :      * Write accessor. Set the local IP address that will be used in the sdp session
     138             :      */
     139             :     void setPublishedIP(const std::string& addr, pj_uint16_t addr_type = pj_AF_UNSPEC());
     140             :     void setPublishedIP(const dhtnet::IpAddr& addr);
     141             : 
     142             :     /*
     143             :      * Read accessor. Get the local IP address
     144             :      */
     145             :     dhtnet::IpAddr getPublishedIPAddr() const { return std::string_view(publishedIpAddr_); }
     146             : 
     147             :     std::string_view getPublishedIP() const { return publishedIpAddr_; }
     148             : 
     149         396 :     void setLocalPublishedAudioPorts(uint16_t audio_port, uint16_t control_port)
     150             :     {
     151         396 :         localAudioRtpPort_ = audio_port;
     152         396 :         localAudioRtcpPort_ = control_port;
     153         396 :     }
     154             : 
     155         396 :     void setLocalPublishedVideoPorts(uint16_t video_port, uint16_t control_port)
     156             :     {
     157         396 :         localVideoRtpPort_ = video_port;
     158         396 :         localVideoRtcpPort_ = control_port;
     159         396 :     }
     160             : 
     161           0 :     uint16_t getLocalVideoPort() const { return localVideoRtpPort_; }
     162             : 
     163           0 :     uint16_t getLocalVideoControlPort() const { return localVideoRtcpPort_; }
     164             : 
     165           0 :     uint16_t getLocalAudioPort() const { return localAudioRtpPort_; }
     166             : 
     167           0 :     uint16_t getLocalAudioControlPort() const { return localAudioRtcpPort_; }
     168             : 
     169             :     std::vector<MediaDescription> getActiveMediaDescription(bool remote) const;
     170             : 
     171             :     std::vector<MediaDescription> getMediaDescriptions(const pjmedia_sdp_session* session, bool remote) const;
     172             : 
     173             :     static std::vector<MediaAttribute> getMediaAttributeListFromSdp(const pjmedia_sdp_session* sdpSession,
     174             :                                                                     bool ignoreDisabled = false);
     175             : 
     176             :     using MediaSlot = std::pair<MediaDescription, MediaDescription>;
     177             :     std::vector<MediaSlot> getMediaSlots() const;
     178             : 
     179             :     unsigned int getTelephoneEventType() const { return telephoneEventPayload_; }
     180             : 
     181             :     void addIceAttributes(const dhtnet::IceTransport::Attribute&& ice_attrs);
     182             :     dhtnet::IceTransport::Attribute getIceAttributes() const;
     183             :     static dhtnet::IceTransport::Attribute getIceAttributes(const pjmedia_sdp_session* session);
     184             : 
     185             :     void addIceCandidates(unsigned media_index, const std::vector<std::string>& cands);
     186             : 
     187             :     std::vector<std::string> getIceCandidates(unsigned media_index) const;
     188             : 
     189             :     void clearIce();
     190             : 
     191         454 :     SdpDirection getSdpDirection() const { return sdpDirection_; };
     192             :     static constexpr std::string_view getSdpDirectionStr(SdpDirection direction);
     193             : 
     194             :     /// \brief Log the given session
     195             :     /// \note crypto lines with are removed for security
     196             :     static void printSession(const pjmedia_sdp_session* session, const char* header, SdpDirection direction);
     197             : 
     198             : private:
     199             :     friend class test::SDPTest;
     200             : 
     201             :     NON_COPYABLE(Sdp);
     202             : 
     203             :     void getProfileLevelID(const pjmedia_sdp_session* session, std::string& dest, int payload) const;
     204             : 
     205             :     /**
     206             :      * Returns the printed original SDP filtered with only the specified media index and codec
     207             :      * remaining.
     208             :      */
     209             :     static std::string getFilteredSdp(const pjmedia_sdp_session* session, unsigned media_keep, unsigned pt_keep);
     210             : 
     211             :     static void clearIce(pjmedia_sdp_session* session);
     212             : 
     213             :     /*
     214             :      * Build the sdp media section
     215             :      * Add rtpmap field if necessary
     216             :      */
     217             :     pjmedia_sdp_media* addMediaDescription(const MediaAttribute& mediaAttr);
     218             : 
     219             :     // Determine media direction
     220             :     char const* mediaDirection(const MediaAttribute& mediaAttr);
     221             : 
     222             :     // Get media direction
     223             :     static MediaDirection getMediaDirection(pjmedia_sdp_media* media);
     224             : 
     225             :     // Get the transport type
     226             :     static MediaTransport getMediaTransport(pjmedia_sdp_media* media);
     227             : 
     228             :     // Get the crypto materials
     229             :     static std::vector<std::string> getCrypto(pjmedia_sdp_media* media);
     230             : 
     231             :     pjmedia_sdp_attr* generateSdesAttribute();
     232             : 
     233             :     void setTelephoneEventRtpmap(pjmedia_sdp_media* med);
     234             : 
     235             :     /*
     236             :      * Create a new SDP
     237             :      */
     238             :     void createLocalSession(SdpDirection direction);
     239             : 
     240             :     /*
     241             :      * Validate SDP
     242             :      */
     243             :     int validateSession() const;
     244             : 
     245             :     /*
     246             :      * Adds a sdes attribute to the given media section.
     247             :      *
     248             :      * @param media The media to add the srtp attribute to
     249             :      * @throw SdpException
     250             :      */
     251             :     void addSdesAttribute(const std::vector<std::string>& crypto);
     252             : 
     253             :     void addRTCPAttribute(pjmedia_sdp_media* med, uint16_t port);
     254             : 
     255             :     std::shared_ptr<SystemCodecInfo> findCodecByPayload(const unsigned payloadType);
     256             :     std::shared_ptr<SystemCodecInfo> findCodecBySpec(std::string_view codecName, const unsigned clockrate = 0) const;
     257             : 
     258             :     // Data members
     259             :     std::unique_ptr<pj_pool_t, std::function<void(pj_pool_t*)>> memPool_;
     260             :     pjmedia_sdp_neg* negotiator_ {nullptr};
     261             :     pjmedia_sdp_session* localSession_ {nullptr};
     262             :     pjmedia_sdp_session* remoteSession_ {nullptr};
     263             : 
     264             :     /**
     265             :      * The negotiated SDP remote session
     266             :      * Explanation: each endpoint's offer is negotiated, and a new sdp offer results from this
     267             :      * negotiation, with the compatible media from each part
     268             :      */
     269             :     const pjmedia_sdp_session* activeLocalSession_ {nullptr};
     270             : 
     271             :     /**
     272             :      * The negotiated SDP remote session
     273             :      * Explanation: each endpoint's offer is negotiated, and a new sdp offer results from this
     274             :      * negotiation, with the compatible media from each part
     275             :      */
     276             :     const pjmedia_sdp_session* activeRemoteSession_ {nullptr};
     277             : 
     278             :     /**
     279             :      * Codec Map used for offer
     280             :      */
     281             :     std::vector<std::shared_ptr<SystemCodecInfo>> audio_codec_list_;
     282             :     std::vector<std::shared_ptr<SystemCodecInfo>> video_codec_list_;
     283             : 
     284             :     std::string publishedIpAddr_;
     285             :     pj_uint16_t publishedIpAddrType_;
     286             : 
     287             :     uint16_t localAudioRtpPort_ {0};
     288             :     uint16_t localAudioRtcpPort_ {0};
     289             :     uint16_t localVideoRtpPort_ {0};
     290             :     uint16_t localVideoRtcpPort_ {0};
     291             : 
     292             :     unsigned int telephoneEventPayload_;
     293             : 
     294             :     // The call Id of the SDP owner
     295             :     std::string sessionName_ {};
     296             : 
     297             :     // Offer/Answer flag.
     298             :     SdpDirection sdpDirection_ {SdpDirection::NONE};
     299             : };
     300             : 
     301             : } // namespace jami

Generated by: LCOV version 1.14