sdp.h 10.7 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2019 Savoir-faire Linux Inc.
3
 *
4
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
5
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
Guillaume Roguez's avatar
Guillaume Roguez committed
6
 *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
20
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
21 22
 */

23
#pragma once
24

Adrien Béraud's avatar
Adrien Béraud committed
25
#include "noncopyable.h"
26 27
#include "sdes_negotiator.h"
#include "sip_utils.h"
28
#include "ip_utils.h"
29
#include "ice_transport.h"
30
#include "media_codec.h"
Guillaume Roguez's avatar
Guillaume Roguez committed
31
#include "sip_utils.h"
Adrien Béraud's avatar
Adrien Béraud committed
32

33 34 35 36
#include <pjmedia/sdp.h>
#include <pjmedia/sdp_neg.h>
#include <pjsip/sip_transport.h>
#include <pjlib.h>
37 38
#include <pjsip_ua.h>
#include <pjmedia/errno.h>
39 40
#include <pj/pool.h>
#include <pj/assert.h>
Adrien Béraud's avatar
Adrien Béraud committed
41

42
#include <map>
43
#include <vector>
44
#include <string>
45
#include <stdexcept>
46

Adrien Béraud's avatar
Adrien Béraud committed
47
namespace jami {
48 49 50

namespace test {
class SDPTest;
51 52
}

53 54
class AudioCodec;

55
class SdpException : public std::runtime_error {
56
    public:
57
        SdpException(const std::string& str="") :
aviau's avatar
aviau committed
58
            std::runtime_error("SDP: SdpException occurred: " + str) {}
59 60
};

61
class Sdp {
62
    public:
Julien Bonjean's avatar
Julien Bonjean committed
63

64 65 66
        /*
         * Class Constructor.
         *
Tristan Matthews's avatar
Tristan Matthews committed
67
         * @param memory pool
68
         */
69
        Sdp(const std::string& id);
70

71 72
        ~Sdp();

73 74 75 76 77
        /**
         *  Read accessor. Get the local passive sdp session information before negotiation
         *
         *  @return The structure that describes a SDP session
         */
78
        pjmedia_sdp_session *getLocalSdpSession() {
79
            return localSession_;
80 81
        }

82 83 84 85
        const pjmedia_sdp_session *getActiveLocalSdpSession() const {
            return activeLocalSession_;
        }

86 87 88 89 90
        /**
         * Read accessor. Get the remote passive sdp session information before negotiation
         *
         * @return The structure that describe the SDP session
         */
91
        pjmedia_sdp_session *getRemoteSdpSession() {
92
            return remoteSession_;
93 94
        }

95 96 97 98
        const pjmedia_sdp_session *getActiveRemoteSdpSession() const {
            return activeRemoteSession_;
        }

99
        /**
100
         * Set the negotiated sdp offer from the sip payload.
101
         *
102
         * @param sdp   the negotiated offer
103
         */
104
        void setActiveLocalSdpSession(const pjmedia_sdp_session *sdp);
105 106

        /**
107
         * Retrieve the negotiated sdp offer from the sip payload.
108
         *
109
         * @param sdp   the negotiated offer
110
         */
111
        void setActiveRemoteSdpSession(const pjmedia_sdp_session *sdp);
112

113 114
        /*
         * On building an invite outside a dialog, build the local offer and create the
115
         * SDP negotiator instance with it.
116
         * @returns true if offer was created, false otherwise
117
         */
118 119
        bool createOffer(const std::vector<std::shared_ptr<AccountCodecInfo>>& selectedAudioCodecs,
                         const std::vector<std::shared_ptr<AccountCodecInfo>>& selectedVideoCodecs,
120 121
                         sip_utils::KeyExchangeProtocol,
                         bool holding=false);
122

Julien Bonjean's avatar
Julien Bonjean committed
123 124
        /*
        * On receiving an invite outside a dialog, build the local offer and create the
125
        * SDP negotiator instance with the remote offer.
Julien Bonjean's avatar
Julien Bonjean committed
126 127 128
        *
        * @param remote    The remote offer
        */
129
        void receiveOffer(const pjmedia_sdp_session* remote,
130 131
                          const std::vector<std::shared_ptr<AccountCodecInfo>>& selectedAudioCodecs,
                          const std::vector<std::shared_ptr<AccountCodecInfo>>& selectedVideoCodecs,
132 133
                          sip_utils::KeyExchangeProtocol,
                          bool holding=false);
Julien Bonjean's avatar
Julien Bonjean committed
134

135
        /**
136
         * Start the sdp negotiation.
137
         */
138
        void startNegotiation();
Julien Bonjean's avatar
Julien Bonjean committed
139

140
        /**
141
         * Remove all media in the session media vector.
142
         */
143
        void cleanSessionMedia();
144

145 146 147
        /*
         * Write accessor. Set the local IP address that will be used in the sdp session
         */
148
        void setPublishedIP(const std::string &addr, pj_uint16_t addr_type =  pj_AF_UNSPEC());
Adrien Béraud's avatar
Adrien Béraud committed
149
        void setPublishedIP(const IpAddr& addr);
150

151 152 153
        /*
         * Read accessor. Get the local IP address
         */
Adrien Béraud's avatar
Adrien Béraud committed
154 155
        IpAddr getPublishedIPAddr() const {
            return publishedIpAddr_;
156 157 158
        }

        std::string getPublishedIP() const {
159
            return publishedIpAddr_;
Julien Bonjean's avatar
Julien Bonjean committed
160
        }
161

162
        void setLocalPublishedAudioPort(int port) {
163 164
            localAudioDataPort_ = port;
            localAudioControlPort_ = port + 1;
165
        }
166

167 168 169 170 171
        void setLocalPublishedAudioPorts(int audio_port, int control_port) {
            localAudioDataPort_ = audio_port;
            localAudioControlPort_ = control_port;
        }

172
        void setLocalPublishedVideoPort (int port) {
173 174
            localVideoDataPort_ = port;
            localVideoControlPort_ = port + 1;
175 176
        }

177 178 179 180 181
        void setLocalPublishedVideoPorts(int video_port, int control_port) {
            localVideoDataPort_ = video_port;
            localVideoControlPort_ = control_port;
        }

182
        unsigned int getLocalVideoPort() const {
183
            return localVideoDataPort_;
184 185
        }

186 187 188 189
        unsigned int getLocalVideoControlPort() const {
            return localVideoControlPort_;
        }

190 191 192 193
        unsigned int getLocalAudioPort() const {
            return localAudioDataPort_;
        }

194 195 196 197
        unsigned int getLocalAudioControlPort() const {
            return localAudioControlPort_;
        }

198 199
        std::vector<MediaDescription>
        getMediaSlots(const pjmedia_sdp_session* session, bool remote) const;
200

Guillaume Roguez's avatar
Guillaume Roguez committed
201 202
        using MediaSlot = std::pair<MediaDescription, MediaDescription>;
        std::vector<MediaSlot> getMediaSlots() const;
203

204
        unsigned int getTelephoneEventType() const {
205
            return telephoneEventPayload_;
206 207
        }

208 209
        void addIceAttributes(const IceTransport::Attribute&& ice_attrs);
        IceTransport::Attribute getIceAttributes() const;
210
        static IceTransport::Attribute getIceAttributes(const pjmedia_sdp_session* session);
211 212 213 214 215 216

        void addIceCandidates(unsigned media_index,
                              const std::vector<std::string>& cands);

        std::vector<std::string> getIceCandidates(unsigned media_index) const;

217 218
        void clearIce();

219 220 221 222
        /// \brief Log the given session
        /// \note crypto lines with are removed for security
        static void printSession(const pjmedia_sdp_session *session, const char* header);

223
    private:
224
        friend class test::SDPTest;
225

226 227
        NON_COPYABLE(Sdp);

228
        std::string getLineFromSession(const pjmedia_sdp_session *sess, const std::string &keyword) const;
229
        std::string getOutgoingVideoField(const std::string &codec, const char *key) const;
230
        void getProfileLevelID(const pjmedia_sdp_session *session, std::string &dest, int payload) const;
Guillaume Roguez's avatar
Guillaume Roguez committed
231

232 233 234 235
        /**
         * Returns the printed original SDP filtered with only the specified media index and codec remaining.
         */
        static std::string getFilteredSdp(const pjmedia_sdp_session* session, unsigned media_keep, unsigned pt_keep);
236

237 238
        static void clearIce(pjmedia_sdp_session* session);

239
        /**
240
         * The pool to allocate memory
241
         */
242
        std::unique_ptr<pj_pool_t, std::function<void(pj_pool_t*)>> memPool_;
243

244
        /** negotiator */
245
        pjmedia_sdp_neg *negotiator_ {nullptr};
246

247 248 249
        /**
         * Local SDP
         */
250
        pjmedia_sdp_session *localSession_ {nullptr};
Julien Bonjean's avatar
Julien Bonjean committed
251

252 253 254
        /**
         * Remote SDP
         */
255
        pjmedia_sdp_session *remoteSession_ {nullptr};
256

257
        /**
258 259 260
         * The negotiated SDP remote session
         * Explanation: each endpoint's offer is negotiated, and a new sdp offer results from this
         * negotiation, with the compatible media from each part
261
         */
262
        const pjmedia_sdp_session *activeLocalSession_ {nullptr};
263

264
        /**
265 266 267
         * The negotiated SDP remote session
         * Explanation: each endpoint's offer is negotiated, and a new sdp offer results from this
         * negotiation, with the compatible media from each part
268
         */
269
        const pjmedia_sdp_session *activeRemoteSession_ {nullptr};
270

271 272 273
        /**
         * Codec Map used for offer
         */
274 275
        std::vector<std::shared_ptr<AccountCodecInfo>> audio_codec_list_;
        std::vector<std::shared_ptr<AccountCodecInfo>> video_codec_list_;
276

277 278 279
        std::string publishedIpAddr_;
        pj_uint16_t publishedIpAddrType_;

280 281 282 283
        int localAudioDataPort_ {0};
        int localAudioControlPort_ {0};
        int localVideoDataPort_ {0};
        int localVideoControlPort_ {0};
284

285
        SdesNegotiator sdesNego_;
286

287
        unsigned int telephoneEventPayload_;
288

289 290 291 292
        /*
         * Build the sdp media section
         * Add rtpmap field if necessary
         */
293
        pjmedia_sdp_media *setMediaDescriptorLines(bool audio, bool holding, sip_utils::KeyExchangeProtocol);
Guillaume Roguez's avatar
Guillaume Roguez committed
294
        pjmedia_sdp_attr *generateSdesAttribute();
295

296 297
        void setTelephoneEventRtpmap(pjmedia_sdp_media *med);

298 299 300 301
        /**
         * Build the local media capabilities for this session
         * @param List of codec in preference order
         */
302 303
        void setLocalMediaAudioCapabilities(const std::vector<std::shared_ptr<AccountCodecInfo>>& selectedAudioCodecs);
        void setLocalMediaVideoCapabilities(const std::vector<std::shared_ptr<AccountCodecInfo>>& selectedVideoCodecs);
304

305 306 307
        /*
         * Build the local SDP offer
         */
308 309
        int createLocalSession(const std::vector<std::shared_ptr<AccountCodecInfo>>& selectedAudioCodecs,
                               const std::vector<std::shared_ptr<AccountCodecInfo>>& selectedVideoCodecs,
310 311 312
                               sip_utils::KeyExchangeProtocol,
                               bool holding);

313
        /*
314
         * Adds a sdes attribute to the given media section.
315
         *
Julien Bonjean's avatar
Julien Bonjean committed
316
         * @param media The media to add the srtp attribute to
317
         * @throw SdpException
318
         */
319
        void addSdesAttribute(const std::vector<std::string>& crypto);
320

321
        void addRTCPAttribute(pjmedia_sdp_media *med);
322 323

        std::shared_ptr<AccountCodecInfo> findCodecByPayload(const unsigned payloadType);
324
        std::shared_ptr<AccountCodecInfo> findCodecBySpec(const std::string &codecName, const unsigned clockrate=0) const;
325 326
};

Adrien Béraud's avatar
Adrien Béraud committed
327
} // namespace jami