sdp.h 9.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 *  Copyright (C) 2009 Savoir-Faire Linux inc.
 *
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
 *
 *  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
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef _SDP_H
#define _SDP_H

#include <pjmedia/sdp.h>
#include <pjmedia/sdp_neg.h>
#include <pjsip/sip_transport.h>
#include <pjlib.h>
28 29
#include <pjsip_ua.h>
#include <pjmedia/errno.h>
30 31 32 33
#include <pj/pool.h>
#include <pj/assert.h>

#include "audio/codecDescriptor.h"
34
#include "sdpmedia.h"
35 36 37 38

class Sdp {

    public:
39 40 41 42 43 44
        
        /*
         * Class Constructor.
         *
         * @param ip_addr
         */
45
        Sdp(pj_pool_t *pool);
46

47
        /* Class destructor */
48 49
        ~Sdp();

50 51 52 53
        /*
         * Read accessor. Get the list of the local media capabilities. 
         *
         * @return std::vector<sdpMedia*>   the vector containing the different media
54
         */
55
        std::vector<sdpMedia*> get_local_media_cap( void ) { return _local_media_cap; }
56

57 58 59 60
         /*
         *  Read accessor. Get the sdp session information
         *
         *  @return pjmedia_sdp_session   The structure that describes a SDP session
61
         */
62
        pjmedia_sdp_session* get_local_sdp_session( void ) { return _local_offer; }
63

64 65
        /*
         * Write accessor. Set the local IP address that will be used in the sdp session
66
         */
67
        void set_ip_address( std::string ip_addr ) { _ip_addr = ip_addr; }
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        /*
         * Read accessor. Get the local IP address
         */
        std::string get_ip_address( void ) { return _ip_addr; }
        
        /*
         * Build the local SDP offer
         */
        int create_local_offer( );

        /*
         * Build the sdp media section
         * Add rtpmap field if necessary
         *
         * @param media     The media to add to SDP
         * @param med   The structure to receive the media section
85
         */
86
        void set_media_descriptor_line( sdpMedia* media, pjmedia_sdp_media** p_med );
87

88 89 90 91 92
        /*
         * On building an invite outside a dialog, build the local offer and create the
         * SDP negociator instance with it.
         */
        int create_initial_offer( );
93

94 95 96 97 98 99 100 101
         /*
         * On receiving an invite outside a dialog, build the local offer and create the
         * SDP negociator instance with the remote offer.
         *
         * @param remote    The remote offer
         */
        int receiving_initial_offer( pjmedia_sdp_session* remote );
        
102 103 104 105 106 107 108 109 110
        /*
         * On receiving a message, check if it contains SDP and negotiate. Should be used for
         * SDP answer and offer but currently is only used for answer.
         * SDP negociator instance with the remote offer.
         *
         * @param inv       The  the invitation
         * @param rdata     The remote data
         */
        
111
        pj_status_t check_sdp_answer(pjsip_inv_session *inv, pjsip_rx_data *rdata);
112
        
113 114
        /*
         * Remove all media in the session media vector.
115
         */
116
        void clean_session_media();
117

118 119 120
        /*
         * Return a string description of the media added to the session,
         * ie the local media capabilities
121
         */
122
        std::string media_to_string( void );
123

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        /*
         * Return the codec of the first media after negociation
         */
        AudioCodec* get_session_media( void );

        /*
         * read accessor. Return the negociated offer
         *
         * @return pjmedia_sdp_session  The negociated offer
         */
        pjmedia_sdp_session* get_negociated_offer( void ){
            return _negociated_offer;
        }

         /*
         * Start the sdp negociation.
         *
         * @return pj_status_t  0 on success
         *                      1 otherwise
143
         */
144 145 146 147 148 149 150 151 152 153
        pj_status_t start_negociation( void ){
            return pjmedia_sdp_neg_negotiate(
                       _pool, _negociator, 0);
        }

         /*
         * Retrieve the negociated sdp offer from the sip payload.
         *
         * @param sdp   the negociated offer
         */
154
        void set_negotiated_sdp ( const pjmedia_sdp_session *sdp );
155

156 157 158 159 160 161 162 163
        /*
         * Attribute the specified port to every medias provided
         * This is valid only because we are using one media
         * We should change this to support multiple medias
         *
         * @param port  The media port
         */
        void attribute_port_to_all_media (int port);
164

Emmanuel Milou's avatar
Emmanuel Milou committed
165
        void  set_local_extern_audio_port(int port){ _local_extern_audio_port = port; }
166

Emmanuel Milou's avatar
Emmanuel Milou committed
167
        int  get_local_extern_audio_port (void){ return _local_extern_audio_port; }
168

169 170
        void toString (void);

171 172 173 174
        /** 
         * Set remote's IP addr. [not protected]
         * @param ip  The remote IP address
         */
175 176 177 178 179 180 181
        void set_remote_ip(const std::string& ip)    { _remote_ip_addr = ip; }
        
        /** 
         * Return IP of destination [mutex protected]
         * @return const std:string	The remote IP address
         */
        const std::string& get_remote_ip() { return _remote_ip_addr; }
182 183 184 185 186

        /** 
         * Set remote's audio port. [not protected]
         * @param port  The remote audio port
         */
Emmanuel Milou's avatar
Emmanuel Milou committed
187
        void set_remote_audio_port(unsigned int port) { _remote_audio_port = port; }
188 189 190 191 192

        /** 
         * Return audio port at destination [mutex protected] 
         * @return unsigned int The remote audio port
         */
Emmanuel Milou's avatar
Emmanuel Milou committed
193
        unsigned int get_remote_audio_port() { return _remote_audio_port; }
194

195
        void set_media_transport_info_from_remote_sdp (const pjmedia_sdp_session *remote_sdp);
Emmanuel Milou's avatar
Emmanuel Milou committed
196

197 198
        std::vector<sdpMedia*> get_session_media_list (void) { return _session_media; }

199
    private:
200 201 202 203 204 205 206 207
        /** Codec Map */
        std::vector<sdpMedia*> _local_media_cap;

        /* The media that will be used by the session (after the SDP negociation) */
        std::vector<sdpMedia*> _session_media;

        /** negociator */
        pjmedia_sdp_neg *_negociator;
208 209

        /** IP address */
210
        std::string _ip_addr;
211

212 213 214
        /** Remote's IP address */
        std::string  _remote_ip_addr;
        
215 216
        /** Local SDP */
        pjmedia_sdp_session *_local_offer;
217

218 219 220 221
        /* The negociated SDP offer */
        // Explanation: each endpoint's offer is negociated, and a new sdp offer results from this
        // negociation, with the compatible media from each part 
        pjmedia_sdp_session *_negociated_offer;
222

223 224 225
        // The pool to allocate memory
        pj_pool_t *_pool;

226 227 228 229 230 231
        /** Local audio port */
        int _local_extern_audio_port;

        /** Remote's audio port */
        unsigned int _remote_audio_port;

232 233
        Sdp(const Sdp&); //No Copy Constructor
        Sdp& operator=(const Sdp&); //No Assignment Operator
234

235
        void set_local_media_capabilities ();
236

237 238 239 240
        /*
         *  Mandatory field: Origin ("o=")
         *  Gives the originator of the session.
         *  Serves as a globally unique identifier for this version of this session description.
241
         */
242
        void sdp_add_origin( void );
243

244 245 246 247 248
        /*
         *  Mandatory field: Protocol version ("v=")
         *  Add the protocol version in the SDP session description
         */
        void sdp_add_protocol( void );
249

250 251 252 253 254 255 256 257 258
        /*
         *  Optional field: Connection data ("c=")
         *  Contains connection data.
         */
        void sdp_add_connection_info( void );
        
        /*
         *  Mandatory field: Session name ("s=")
         *  Add a textual session name.
259
         */
260
        void sdp_add_session_name( void );
261

262 263 264
        /*
         *  Optional field: Session information ("s=")
         *  Provides textual information about the session.
265
         */
266
        void sdp_add_session_info( void ){}
267

268 269 270 271 272
        /*
         *  Optional field: Uri ("u=")
         *  Add a pointer to additional information about the session.
         */
        void sdp_add_uri( void ) {}
273

274 275 276 277 278
        /*
         *  Optional fields: Email address and phone number ("e=" and "p=")
         *  Add contact information for the person responsible for the conference.
         */
        void sdp_add_email( void ) {}
279

280 281 282 283 284
        /*
         *  Optional field: Bandwidth ("b=")
         *  Denotes the proposed bandwidth to be used by the session or the media .
         */
        void sdp_add_bandwidth( void ) {}
285

286 287 288 289 290
        /*
         *  Mandatory field: Timing ("t=")
         *  Specify the start and the stop time for a session.
         */
        void sdp_add_timing( void );
291

292 293
        /*
         * Optional field: Time zones ("z=")
294
         */
295
        void sdp_add_time_zone( void ) {}
296

297 298
        /*
         * Optional field: Encryption keys ("k=")
299
         */
300 301 302 303 304 305 306 307 308
        void sdp_add_encryption_key( void ) {}

        /*
         * Optional field: Attributes ("a=")
         */
        void sdp_add_attributes( );

        /*
         * Mandatory field: Media descriptions ("m=")
309
         */
310 311
        void sdp_add_media_description();

312 313
        std::string convert_int_to_string (int value);

314
        void set_remote_ip_from_sdp (const pjmedia_sdp_session *r_sdp);
Emmanuel Milou's avatar
Emmanuel Milou committed
315
        
316
        void set_remote_audio_port_from_sdp (pjmedia_sdp_media *r_media);
Emmanuel Milou's avatar
Emmanuel Milou committed
317

318
        void get_remote_sdp_media_from_offer (const pjmedia_sdp_session* r_sdp, pjmedia_sdp_media** r_media);
319 320 321 322

//////////////////////////////////////////////////////////////////3
////////////////////////////////////////////////////////////////////
              
323 324 325 326
};


#endif