SDP: Set the remote c= line in RTP instance.
[asterisk/asterisk.git] / main / sdp_state.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2017, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 #include "asterisk.h"
20 #include "asterisk/sdp_state.h"
21 #include "asterisk/sdp_options.h"
22 #include "asterisk/sdp_translator.h"
23 #include "asterisk/vector.h"
24 #include "asterisk/utils.h"
25 #include "asterisk/netsock2.h"
26 #include "asterisk/rtp_engine.h"
27 #include "asterisk/format.h"
28 #include "asterisk/format_cap.h"
29 #include "asterisk/config.h"
30 #include "asterisk/codec.h"
31 #include "asterisk/udptl.h"
32
33 #include "asterisk/sdp.h"
34 #include "asterisk/stream.h"
35
36 #include "sdp_private.h"
37
38 enum ast_sdp_role {
39         /*!
40          * \brief The role has not yet been determined.
41          *
42          * When the SDP state is allocated, this is the starting role.
43          * Similarly, when the SDP state is reset, the role is reverted
44          * to this.
45          */
46         SDP_ROLE_NOT_SET,
47         /*!
48          * \brief We are the offerer.
49          *
50          * If a local SDP is requested before a remote SDP has been set, then
51          * we assume the role of offerer. This means that we will generate an
52          * SDP from the local capabilities and configured options.
53          */
54         SDP_ROLE_OFFERER,
55         /*!
56          * \brief We are the answerer.
57          *
58          * If a remote SDP is set before a local SDP is requested, then we
59          * assume the role of answerer. This means that we will generate an
60          * SDP based on a merge of the remote capabilities and our local capabilities.
61          */
62         SDP_ROLE_ANSWERER,
63 };
64
65 typedef int (*state_fn)(struct ast_sdp_state *state);
66
67 struct sdp_state_rtp {
68         /*! The underlying RTP instance */
69         struct ast_rtp_instance *instance;
70 };
71
72 struct sdp_state_udptl {
73         /*! The underlying UDPTL instance */
74         struct ast_udptl *instance;
75 };
76
77 struct sdp_state_stream {
78         /*! Type of the stream */
79         enum ast_media_type type;
80         union {
81                 /*! The underlying RTP instance */
82                 struct sdp_state_rtp *rtp;
83                 /*! The underlying UDPTL instance */
84                 struct sdp_state_udptl *udptl;
85         };
86         /*! An explicit connection address for this stream */
87         struct ast_sockaddr connection_address;
88         /*! Whether this stream is held or not */
89         unsigned int locally_held;
90         /*! UDPTL session parameters */
91         struct ast_control_t38_parameters t38_local_params;
92 };
93
94 static void sdp_state_rtp_destroy(void *obj)
95 {
96         struct sdp_state_rtp *rtp = obj;
97
98         if (rtp->instance) {
99                 ast_rtp_instance_stop(rtp->instance);
100                 ast_rtp_instance_destroy(rtp->instance);
101         }
102 }
103
104 static void sdp_state_udptl_destroy(void *obj)
105 {
106         struct sdp_state_udptl *udptl = obj;
107
108         if (udptl->instance) {
109                 ast_udptl_destroy(udptl->instance);
110         }
111 }
112
113 static void sdp_state_stream_free(struct sdp_state_stream *state_stream)
114 {
115         switch (state_stream->type) {
116         case AST_MEDIA_TYPE_AUDIO:
117         case AST_MEDIA_TYPE_VIDEO:
118                 ao2_cleanup(state_stream->rtp);
119                 break;
120         case AST_MEDIA_TYPE_IMAGE:
121                 ao2_cleanup(state_stream->udptl);
122                 break;
123         case AST_MEDIA_TYPE_UNKNOWN:
124         case AST_MEDIA_TYPE_TEXT:
125         case AST_MEDIA_TYPE_END:
126                 break;
127         }
128         ast_free(state_stream);
129 }
130
131 AST_VECTOR(sdp_state_streams, struct sdp_state_stream *);
132
133 struct sdp_state_capabilities {
134         /*! Stream topology */
135         struct ast_stream_topology *topology;
136         /*! Additional information about the streams */
137         struct sdp_state_streams streams;
138         /*! An explicit global connection address */
139         struct ast_sockaddr connection_address;
140 };
141
142 static void sdp_state_capabilities_free(struct sdp_state_capabilities *capabilities)
143 {
144         if (!capabilities) {
145                 return;
146         }
147
148         ast_stream_topology_free(capabilities->topology);
149         AST_VECTOR_CALLBACK_VOID(&capabilities->streams, sdp_state_stream_free);
150         AST_VECTOR_FREE(&capabilities->streams);
151         ast_free(capabilities);
152 }
153
154 /* TODO
155  * This isn't set anywhere yet.
156  */
157 /*! \brief Scheduler for RTCP purposes */
158 static struct ast_sched_context *sched;
159
160 /*! \brief Internal function which creates an RTP instance */
161 static struct sdp_state_rtp *create_rtp(const struct ast_sdp_options *options,
162         enum ast_media_type media_type)
163 {
164         struct sdp_state_rtp *rtp;
165         struct ast_rtp_engine_ice *ice;
166         static struct ast_sockaddr address_rtp;
167         struct ast_sockaddr *media_address = &address_rtp;
168
169         if (!ast_strlen_zero(options->interface_address)) {
170                 if (!ast_sockaddr_parse(&address_rtp, options->interface_address, 0)) {
171                         ast_log(LOG_ERROR, "Attempted to bind RTP to invalid media address: %s\n",
172                                 options->interface_address);
173                         return NULL;
174                 }
175         } else {
176                 if (ast_check_ipv6()) {
177                         ast_sockaddr_parse(&address_rtp, "::", 0);
178                 } else {
179                         ast_sockaddr_parse(&address_rtp, "0.0.0.0", 0);
180                 }
181         }
182
183         rtp = ao2_alloc_options(sizeof(*rtp), sdp_state_rtp_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
184         if (!rtp) {
185                 return NULL;
186         }
187
188         rtp->instance = ast_rtp_instance_new(options->rtp_engine, sched, media_address, NULL);
189         if (!rtp->instance) {
190                 ast_log(LOG_ERROR, "Unable to create RTP instance using RTP engine '%s'\n",
191                         options->rtp_engine);
192                 ao2_ref(rtp, -1);
193                 return NULL;
194         }
195
196         ast_rtp_instance_set_prop(rtp->instance, AST_RTP_PROPERTY_RTCP,
197                 AST_RTP_INSTANCE_RTCP_STANDARD);
198         ast_rtp_instance_set_prop(rtp->instance, AST_RTP_PROPERTY_NAT,
199                 options->rtp_symmetric);
200
201         if (options->ice == AST_SDP_ICE_DISABLED
202                 && (ice = ast_rtp_instance_get_ice(rtp->instance))) {
203                 ice->stop(rtp->instance);
204         }
205
206         if (options->dtmf == AST_SDP_DTMF_RFC_4733 || options->dtmf == AST_SDP_DTMF_AUTO) {
207                 ast_rtp_instance_dtmf_mode_set(rtp->instance, AST_RTP_DTMF_MODE_RFC2833);
208                 ast_rtp_instance_set_prop(rtp->instance, AST_RTP_PROPERTY_DTMF, 1);
209         } else if (options->dtmf == AST_SDP_DTMF_INBAND) {
210                 ast_rtp_instance_dtmf_mode_set(rtp->instance, AST_RTP_DTMF_MODE_INBAND);
211         }
212
213         switch (media_type) {
214         case AST_MEDIA_TYPE_AUDIO:
215                 if (options->tos_audio || options->cos_audio) {
216                         ast_rtp_instance_set_qos(rtp->instance, options->tos_audio,
217                                 options->cos_audio, "SIP RTP Audio");
218                 }
219                 break;
220         case AST_MEDIA_TYPE_VIDEO:
221                 if (options->tos_video || options->cos_video) {
222                         ast_rtp_instance_set_qos(rtp->instance, options->tos_video,
223                                 options->cos_video, "SIP RTP Video");
224                 }
225                 break;
226         case AST_MEDIA_TYPE_IMAGE:
227         case AST_MEDIA_TYPE_TEXT:
228         case AST_MEDIA_TYPE_UNKNOWN:
229         case AST_MEDIA_TYPE_END:
230                 break;
231         }
232
233         ast_rtp_instance_set_last_rx(rtp->instance, time(NULL));
234
235         return rtp;
236 }
237
238 /*! \brief Internal function which creates a UDPTL instance */
239 static struct sdp_state_udptl *create_udptl(const struct ast_sdp_options *options)
240 {
241         struct sdp_state_udptl *udptl;
242         static struct ast_sockaddr address_udptl;
243         struct ast_sockaddr *media_address = &address_udptl;
244
245         if (!ast_strlen_zero(options->interface_address)) {
246                 if (!ast_sockaddr_parse(&address_udptl, options->interface_address, 0)) {
247                         ast_log(LOG_ERROR, "Attempted to bind UDPTL to invalid media address: %s\n",
248                                 options->interface_address);
249                         return NULL;
250                 }
251         } else {
252                 if (ast_check_ipv6()) {
253                         ast_sockaddr_parse(&address_udptl, "::", 0);
254                 } else {
255                         ast_sockaddr_parse(&address_udptl, "0.0.0.0", 0);
256                 }
257         }
258
259         udptl = ao2_alloc_options(sizeof(*udptl), sdp_state_udptl_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
260         if (!udptl) {
261                 return NULL;
262         }
263
264         udptl->instance = ast_udptl_new_with_bindaddr(NULL, NULL, 0, media_address);
265         if (!udptl->instance) {
266                 ao2_ref(udptl, -1);
267                 return NULL;
268         }
269
270         ast_udptl_set_error_correction_scheme(udptl->instance, ast_sdp_options_get_udptl_error_correction(options));
271         ast_udptl_setnat(udptl->instance, ast_sdp_options_get_udptl_symmetric(options));
272         ast_udptl_set_far_max_datagram(udptl->instance, ast_sdp_options_get_udptl_far_max_datagram(options));
273
274         return udptl;
275 }
276
277 static struct sdp_state_capabilities *sdp_initialize_state_capabilities(const struct ast_stream_topology *topology,
278         const struct ast_sdp_options *options)
279 {
280         struct sdp_state_capabilities *capabilities;
281         int i;
282
283         capabilities = ast_calloc(1, sizeof(*capabilities));
284         if (!capabilities) {
285                 return NULL;
286         }
287
288         capabilities->topology = ast_stream_topology_clone(topology);
289         if (!capabilities->topology) {
290                 sdp_state_capabilities_free(capabilities);
291                 return NULL;
292         }
293
294         if (AST_VECTOR_INIT(&capabilities->streams, ast_stream_topology_get_count(topology))) {
295                 sdp_state_capabilities_free(capabilities);
296                 return NULL;
297         }
298         ast_sockaddr_setnull(&capabilities->connection_address);
299
300         for (i = 0; i < ast_stream_topology_get_count(topology); ++i) {
301                 struct sdp_state_stream *state_stream;
302
303                 state_stream = ast_calloc(1, sizeof(*state_stream));
304                 if (!state_stream) {
305                         sdp_state_capabilities_free(capabilities);
306                         return NULL;
307                 }
308
309                 state_stream->type = ast_stream_get_type(ast_stream_topology_get_stream(topology, i));
310                 switch (state_stream->type) {
311                 case AST_MEDIA_TYPE_AUDIO:
312                 case AST_MEDIA_TYPE_VIDEO:
313                         state_stream->rtp = create_rtp(options, state_stream->type);
314                         if (!state_stream->rtp) {
315                                 sdp_state_stream_free(state_stream);
316                                 sdp_state_capabilities_free(capabilities);
317                                 return NULL;
318                         }
319                         break;
320                 case AST_MEDIA_TYPE_IMAGE:
321                         state_stream->udptl = create_udptl(options);
322                         if (!state_stream->udptl) {
323                                 sdp_state_stream_free(state_stream);
324                                 sdp_state_capabilities_free(capabilities);
325                                 return NULL;
326                         }
327                         break;
328                 case AST_MEDIA_TYPE_UNKNOWN:
329                 case AST_MEDIA_TYPE_TEXT:
330                 case AST_MEDIA_TYPE_END:
331                         ast_assert(0);
332                         sdp_state_stream_free(state_stream);
333                         sdp_state_capabilities_free(capabilities);
334                         return NULL;
335                 }
336
337                 if (AST_VECTOR_APPEND(&capabilities->streams, state_stream)) {
338                         sdp_state_stream_free(state_stream);
339                         sdp_state_capabilities_free(capabilities);
340                         return NULL;
341                 }
342         }
343
344         return capabilities;
345 }
346
347 /*!
348  * \brief SDP state, the main structure used to keep track of SDP negotiation
349  * and settings.
350  *
351  * Most fields are pretty self-explanatory, but negotiated_capabilities and
352  * proposed_capabilities could use some further explanation. When an SDP
353  * state is allocated, a stream topology is provided that dictates the
354  * types of streams to offer in the resultant SDP. At the time the SDP
355  * is allocated, this topology is used to create the proposed_capabilities.
356  *
357  * If we are the SDP offerer, then the proposed_capabilities are what are used
358  * to generate the SDP offer. When the SDP answer arrives, the proposed capabilities
359  * are merged with the SDP answer to create the negotiated capabilities.
360  *
361  * If we are the SDP answerer, then the incoming SDP offer is merged with our
362  * proposed capabilities to to create the negotiated capabilities. These negotiated
363  * capabilities are what we send in our SDP answer.
364  *
365  * Any changes that a user of the API performs will occur on the proposed capabilities.
366  * The negotiated capabilities are only altered based on actual SDP negotiation. This is
367  * done so that the negotiated capabilities can be fallen back on if the proposed
368  * capabilities run into some sort of issue.
369  */
370 struct ast_sdp_state {
371         /*! Current capabilities */
372         struct sdp_state_capabilities *negotiated_capabilities;
373         /*! Proposed capabilities */
374         struct sdp_state_capabilities *proposed_capabilities;
375         /*! Local SDP. Generated via the options and currently negotiated/proposed capabilities. */
376         struct ast_sdp *local_sdp;
377         /*! SDP options. Configured options beyond media capabilities. */
378         struct ast_sdp_options *options;
379         /*! Translator that puts SDPs into the expected representation */
380         struct ast_sdp_translator *translator;
381         /*! The role that we occupy in SDP negotiation */
382         enum ast_sdp_role role;
383 };
384
385 struct ast_sdp_state *ast_sdp_state_alloc(struct ast_stream_topology *streams,
386         struct ast_sdp_options *options)
387 {
388         struct ast_sdp_state *sdp_state;
389
390         sdp_state = ast_calloc(1, sizeof(*sdp_state));
391         if (!sdp_state) {
392                 return NULL;
393         }
394
395         sdp_state->options = options;
396
397         sdp_state->translator = ast_sdp_translator_new(ast_sdp_options_get_impl(sdp_state->options));
398         if (!sdp_state->translator) {
399                 ast_sdp_state_free(sdp_state);
400                 return NULL;
401         }
402
403         sdp_state->proposed_capabilities = sdp_initialize_state_capabilities(streams, options);
404         if (!sdp_state->proposed_capabilities) {
405                 ast_sdp_state_free(sdp_state);
406                 return NULL;
407         }
408
409         sdp_state->role = SDP_ROLE_NOT_SET;
410
411         return sdp_state;
412 }
413
414 void ast_sdp_state_free(struct ast_sdp_state *sdp_state)
415 {
416         if (!sdp_state) {
417                 return;
418         }
419
420         sdp_state_capabilities_free(sdp_state->negotiated_capabilities);
421         sdp_state_capabilities_free(sdp_state->proposed_capabilities);
422         ast_sdp_free(sdp_state->local_sdp);
423         ast_sdp_options_free(sdp_state->options);
424         ast_sdp_translator_free(sdp_state->translator);
425         ast_free(sdp_state);
426 }
427
428 static struct sdp_state_stream *sdp_state_get_stream(const struct ast_sdp_state *sdp_state, int stream_index)
429 {
430         if (stream_index >= AST_VECTOR_SIZE(&sdp_state->proposed_capabilities->streams)) {
431                 return NULL;
432         }
433
434         return AST_VECTOR_GET(&sdp_state->proposed_capabilities->streams, stream_index);
435 }
436
437 struct ast_rtp_instance *ast_sdp_state_get_rtp_instance(
438         const struct ast_sdp_state *sdp_state, int stream_index)
439 {
440         struct sdp_state_stream *stream_state;
441
442         ast_assert(sdp_state != NULL);
443         ast_assert(ast_stream_get_type(ast_stream_topology_get_stream(sdp_state->proposed_capabilities->topology,
444                 stream_index)) == AST_MEDIA_TYPE_AUDIO || ast_stream_get_type(ast_stream_topology_get_stream(
445                         sdp_state->proposed_capabilities->topology, stream_index)) == AST_MEDIA_TYPE_VIDEO);
446
447         stream_state = sdp_state_get_stream(sdp_state, stream_index);
448         if (!stream_state || !stream_state->rtp) {
449                 return NULL;
450         }
451
452         return stream_state->rtp->instance;
453 }
454
455 struct ast_udptl *ast_sdp_state_get_udptl_instance(
456         const struct ast_sdp_state *sdp_state, int stream_index)
457 {
458         struct sdp_state_stream *stream_state;
459
460         ast_assert(sdp_state != NULL);
461         ast_assert(ast_stream_get_type(ast_stream_topology_get_stream(sdp_state->proposed_capabilities->topology,
462                 stream_index)) == AST_MEDIA_TYPE_IMAGE);
463
464         stream_state = sdp_state_get_stream(sdp_state, stream_index);
465         if (!stream_state || !stream_state->udptl) {
466                 return NULL;
467         }
468
469         return stream_state->udptl->instance;
470 }
471
472 const struct ast_sockaddr *ast_sdp_state_get_connection_address(const struct ast_sdp_state *sdp_state)
473 {
474         ast_assert(sdp_state != NULL);
475
476         return &sdp_state->proposed_capabilities->connection_address;
477 }
478
479 int ast_sdp_state_get_stream_connection_address(const struct ast_sdp_state *sdp_state,
480         int stream_index, struct ast_sockaddr *address)
481 {
482         struct sdp_state_stream *stream_state;
483
484         ast_assert(sdp_state != NULL);
485         ast_assert(address != NULL);
486
487         stream_state = sdp_state_get_stream(sdp_state, stream_index);
488         if (!stream_state) {
489                 return -1;
490         }
491
492         /* If an explicit connection address has been provided for the stream return it */
493         if (!ast_sockaddr_isnull(&stream_state->connection_address)) {
494                 ast_sockaddr_copy(address, &stream_state->connection_address);
495                 return 0;
496         }
497
498         switch (ast_stream_get_type(ast_stream_topology_get_stream(sdp_state->proposed_capabilities->topology,
499                 stream_index))) {
500         case AST_MEDIA_TYPE_AUDIO:
501         case AST_MEDIA_TYPE_VIDEO:
502                 ast_rtp_instance_get_local_address(stream_state->rtp->instance, address);
503                 break;
504         case AST_MEDIA_TYPE_IMAGE:
505                 ast_udptl_get_us(stream_state->udptl->instance, address);
506                 break;
507         case AST_MEDIA_TYPE_UNKNOWN:
508         case AST_MEDIA_TYPE_TEXT:
509         case AST_MEDIA_TYPE_END:
510                 return -1;
511         }
512
513         /* If an explicit global connection address is set use it here for the IP part */
514         if (!ast_sockaddr_isnull(&sdp_state->proposed_capabilities->connection_address)) {
515                 int port = ast_sockaddr_port(address);
516
517                 ast_sockaddr_copy(address, &sdp_state->proposed_capabilities->connection_address);
518                 ast_sockaddr_set_port(address, port);
519         }
520
521         return 0;
522 }
523
524 const struct ast_stream_topology *ast_sdp_state_get_joint_topology(
525         const struct ast_sdp_state *sdp_state)
526 {
527         ast_assert(sdp_state != NULL);
528
529         if (sdp_state->negotiated_capabilities) {
530                 return sdp_state->negotiated_capabilities->topology;
531         }
532
533         return sdp_state->proposed_capabilities->topology;
534 }
535
536 const struct ast_stream_topology *ast_sdp_state_get_local_topology(
537         const struct ast_sdp_state *sdp_state)
538 {
539         ast_assert(sdp_state != NULL);
540
541         return sdp_state->proposed_capabilities->topology;
542 }
543
544 const struct ast_sdp_options *ast_sdp_state_get_options(
545         const struct ast_sdp_state *sdp_state)
546 {
547         ast_assert(sdp_state != NULL);
548
549         return sdp_state->options;
550 }
551
552 /*!
553  * \brief Merge two streams into a joint stream.
554  *
555  * \param local Our local stream
556  * \param remote A remote stream
557  *
558  * \retval NULL An error occurred
559  * \retval non-NULL The joint stream created
560  */
561 static struct ast_stream *merge_streams(const struct ast_stream *local,
562         const struct ast_stream *remote)
563 {
564         struct ast_stream *joint_stream;
565         struct ast_format_cap *joint_cap;
566         struct ast_format_cap *local_cap;
567         struct ast_format_cap *remote_cap;
568         struct ast_str *local_buf = ast_str_alloca(128);
569         struct ast_str *remote_buf = ast_str_alloca(128);
570         struct ast_str *joint_buf = ast_str_alloca(128);
571
572         joint_stream = ast_stream_alloc(ast_codec_media_type2str(ast_stream_get_type(remote)),
573                 ast_stream_get_type(remote));
574         if (!joint_stream) {
575                 return NULL;
576         }
577
578         joint_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
579         if (!joint_cap) {
580                 ast_stream_free(joint_stream);
581                 return NULL;
582         }
583
584         local_cap = ast_stream_get_formats(local);
585         remote_cap = ast_stream_get_formats(remote);
586
587         ast_format_cap_get_compatible(local_cap, remote_cap, joint_cap);
588
589         ast_debug(3, "Combined local '%s' with remote '%s' to get joint '%s'. Joint has %zu formats\n",
590                 ast_format_cap_get_names(local_cap, &local_buf),
591                 ast_format_cap_get_names(remote_cap, &remote_buf),
592                 ast_format_cap_get_names(joint_cap, &joint_buf),
593                 ast_format_cap_count(joint_cap));
594
595         ast_stream_set_formats(joint_stream, joint_cap);
596
597         ao2_ref(joint_cap, -1);
598
599         return joint_stream;
600 }
601
602 /*!
603  * \brief Get a local stream that corresponds with a remote stream.
604  *
605  * \param local The local topology
606  * \param media_type The type of stream we are looking for
607  * \param[in,out] media_indices Keeps track of where to start searching in the topology
608  *
609  * \retval -1 No corresponding stream found
610  * \retval index The corresponding stream index
611  */
612 static int get_corresponding_index(const struct ast_stream_topology *local,
613         enum ast_media_type media_type, int *media_indices)
614 {
615         int i;
616
617         for (i = media_indices[media_type]; i < ast_stream_topology_get_count(local); ++i) {
618                 struct ast_stream *candidate;
619
620                 candidate = ast_stream_topology_get_stream(local, i);
621                 if (ast_stream_get_type(candidate) == media_type) {
622                         media_indices[media_type] = i + 1;
623                         return i;
624                 }
625         }
626
627         /* No stream of the type left in the topology */
628         media_indices[media_type] = i;
629         return -1;
630 }
631
632 /*!
633  * XXX TODO The merge_capabilities() function needs to be split into
634  * merging for new local topologies and new remote topologies.  Also
635  * the possibility of changing the stream types needs consideration.
636  * Audio to video may or may not need us to keep the same RTP instance
637  * because the stream position is still RTP.  A new RTP instance would
638  * cause us to change ports.  Audio to image is definitely going to
639  * happen for T.38.
640  *
641  * A new remote topology as an initial offer needs to dictate the
642  * number of streams and the order.  As a sdp_state option we may
643  * allow creation of new active streams not defined by the current
644  * local topology.  A subsequent remote offer can change the stream
645  * types and add streams.  The sdp_state option could regulate
646  * creation of new active streams here as well.  An answer cannot
647  * change stream types or the number of streams but can decline
648  * streams.  Any attempt to do so should report an error and possibly
649  * disconnect the call.
650  *
651  * A local topology update needs to be merged differently.  It cannot
652  * reduce the number of streams already defined without violating the
653  * SDP RFC.  The local merge could take the new topology stream
654  * verbatim and add declined streams to fill out any shortfall with
655  * the exiting topology.  This strategy is needed if we want to change
656  * an audio stream to an image stream for T.38 fax and vice versa.
657  * The local merge could take the new topology and map the streams to
658  * the existing local topology.  The new topology stream format caps
659  * would be copied into the merged topology so we could change what
660  * codecs are negotiated.
661  */
662 /*!
663  * \brief Merge existing stream capabilities and a new topology into joint capabilities.
664  *
665  * \param sdp_state The state needing capabilities merged
666  * \param new_topology The new topology to base merged capabilities on
667  * \param is_local If new_topology is a local update.
668  *
669  * \details
670  * This is a bit complicated. The idea is that we already have some
671  * capabilities set, and we've now been confronted with a new stream
672  * topology.  We want to take what's been presented to us and merge
673  * those new capabilities with our own.
674  *
675  * For each of the new streams, we try to find a corresponding stream
676  * in our proposed capabilities.  If we find one, then we get the
677  * compatible formats of the two streams and create a new stream with
678  * those formats set.  We then will re-use the underlying media
679  * instance (such as an RTP instance) on this merged stream.
680  *
681  * The is_local parameter determines whether we should attempt to
682  * create new media instances.  If we do not find a corresponding
683  * stream, then we create a new one.  If the is_local parameter is
684  * true, this created stream is made a clone of the new stream, and a
685  * media instance is created.  If the is_local parameter is not true,
686  * then the created stream has no formats set and no media instance is
687  * created for it.
688  *
689  * \retval NULL An error occurred
690  * \retval non-NULL The merged capabilities
691  */
692 static struct sdp_state_capabilities *merge_capabilities(const struct ast_sdp_state *sdp_state,
693         const struct ast_stream_topology *new_topology, int is_local)
694 {
695         const struct sdp_state_capabilities *local = sdp_state->proposed_capabilities;
696         struct sdp_state_capabilities *joint_capabilities;
697         int media_indices[AST_MEDIA_TYPE_END] = {0};
698         int i;
699         static const char dummy_name[] = "dummy";
700
701         ast_assert(local != NULL);
702
703         joint_capabilities = ast_calloc(1, sizeof(*joint_capabilities));
704         if (!joint_capabilities) {
705                 return NULL;
706         }
707
708         joint_capabilities->topology = ast_stream_topology_alloc();
709         if (!joint_capabilities->topology) {
710                 goto fail;
711         }
712
713         if (AST_VECTOR_INIT(&joint_capabilities->streams, AST_VECTOR_SIZE(&local->streams))) {
714                 goto fail;
715         }
716         ast_sockaddr_copy(&joint_capabilities->connection_address, &local->connection_address);
717
718         for (i = 0; i < ast_stream_topology_get_count(new_topology); ++i) {
719                 enum ast_media_type new_stream_type;
720                 struct ast_stream *new_stream;
721                 struct ast_stream *local_stream;
722                 struct ast_stream *joint_stream;
723                 struct sdp_state_stream *joint_state_stream;
724                 int local_index;
725
726                 joint_state_stream = ast_calloc(1, sizeof(*joint_state_stream));
727                 if (!joint_state_stream) {
728                         goto fail;
729                 }
730
731                 new_stream = ast_stream_topology_get_stream(new_topology, i);
732                 new_stream_type = ast_stream_get_type(new_stream);
733
734                 local_index = get_corresponding_index(local->topology, new_stream_type, media_indices);
735                 if (0 <= local_index) {
736                         local_stream = ast_stream_topology_get_stream(local->topology, local_index);
737                         if (!strcmp(ast_stream_get_name(local_stream), dummy_name)) {
738                                 /* The local stream is a non-exixtent dummy stream. */
739                                 local_stream = NULL;
740                         }
741                 } else {
742                         local_stream = NULL;
743                 }
744                 if (local_stream) {
745                         struct sdp_state_stream *local_state_stream;
746                         struct ast_rtp_codecs *codecs;
747
748                         if (is_local) {
749                                 /* Replace the local stream with the new local stream. */
750                                 joint_stream = ast_stream_clone(new_stream, NULL);
751                         } else {
752                                 joint_stream = merge_streams(local_stream, new_stream);
753                         }
754                         if (!joint_stream) {
755                                 sdp_state_stream_free(joint_state_stream);
756                                 goto fail;
757                         }
758
759                         local_state_stream = AST_VECTOR_GET(&local->streams, local_index);
760                         joint_state_stream->type = local_state_stream->type;
761
762                         switch (joint_state_stream->type) {
763                         case AST_MEDIA_TYPE_AUDIO:
764                         case AST_MEDIA_TYPE_VIDEO:
765                                 joint_state_stream->rtp = ao2_bump(local_state_stream->rtp);
766                                 if (is_local) {
767                                         break;
768                                 }
769                                 codecs = ast_stream_get_data(new_stream, AST_STREAM_DATA_RTP_CODECS);
770                                 ast_assert(codecs != NULL);
771                                 if (sdp_state->role == SDP_ROLE_ANSWERER) {
772                                         /*
773                                          * Setup rx payload type mapping to prefer the mapping
774                                          * from the peer that the RFC says we SHOULD use.
775                                          */
776                                         ast_rtp_codecs_payloads_xover(codecs, codecs, NULL);
777                                 }
778                                 ast_rtp_codecs_payloads_copy(codecs,
779                                         ast_rtp_instance_get_codecs(joint_state_stream->rtp->instance),
780                                         joint_state_stream->rtp->instance);
781                                 break;
782                         case AST_MEDIA_TYPE_IMAGE:
783                                 joint_state_stream->udptl = ao2_bump(local_state_stream->udptl);
784                                 joint_state_stream->t38_local_params = local_state_stream->t38_local_params;
785                                 break;
786                         case AST_MEDIA_TYPE_UNKNOWN:
787                         case AST_MEDIA_TYPE_TEXT:
788                         case AST_MEDIA_TYPE_END:
789                                 break;
790                         }
791
792                         if (!ast_sockaddr_isnull(&local_state_stream->connection_address)) {
793                                 ast_sockaddr_copy(&joint_state_stream->connection_address,
794                                         &local_state_stream->connection_address);
795                         } else {
796                                 ast_sockaddr_setnull(&joint_state_stream->connection_address);
797                         }
798                         joint_state_stream->locally_held = local_state_stream->locally_held;
799                 } else if (is_local) {
800                         /* We don't have a stream state that corresponds to the stream in the new topology, so
801                          * create a stream state as appropriate.
802                          */
803                         joint_stream = ast_stream_clone(new_stream, NULL);
804                         if (!joint_stream) {
805                                 sdp_state_stream_free(joint_state_stream);
806                                 goto fail;
807                         }
808
809                         switch (new_stream_type) {
810                         case AST_MEDIA_TYPE_AUDIO:
811                         case AST_MEDIA_TYPE_VIDEO:
812                                 joint_state_stream->rtp = create_rtp(sdp_state->options,
813                                         new_stream_type);
814                                 if (!joint_state_stream->rtp) {
815                                         ast_stream_free(joint_stream);
816                                         sdp_state_stream_free(joint_state_stream);
817                                         goto fail;
818                                 }
819                                 break;
820                         case AST_MEDIA_TYPE_IMAGE:
821                                 joint_state_stream->udptl = create_udptl(sdp_state->options);
822                                 if (!joint_state_stream->udptl) {
823                                         ast_stream_free(joint_stream);
824                                         sdp_state_stream_free(joint_state_stream);
825                                         goto fail;
826                                 }
827                                 break;
828                         case AST_MEDIA_TYPE_UNKNOWN:
829                         case AST_MEDIA_TYPE_TEXT:
830                         case AST_MEDIA_TYPE_END:
831                                 break;
832                         }
833                         ast_sockaddr_setnull(&joint_state_stream->connection_address);
834                         joint_state_stream->locally_held = 0;
835                 } else {
836                         /* We don't have a stream that corresponds to the stream in the new topology. Create a
837                          * dummy stream to go in its place so that the resulting SDP created will contain
838                          * the stream but will have no port or codecs set
839                          */
840                         joint_stream = ast_stream_alloc(dummy_name, new_stream_type);
841                         if (!joint_stream) {
842                                 sdp_state_stream_free(joint_state_stream);
843                                 goto fail;
844                         }
845                 }
846
847                 if (ast_stream_topology_append_stream(joint_capabilities->topology, joint_stream) < 0) {
848                         ast_stream_free(joint_stream);
849                         sdp_state_stream_free(joint_state_stream);
850                         goto fail;
851                 }
852                 if (AST_VECTOR_APPEND(&joint_capabilities->streams, joint_state_stream)) {
853                         sdp_state_stream_free(joint_state_stream);
854                         goto fail;
855                 }
856         }
857
858         return joint_capabilities;
859
860 fail:
861         sdp_state_capabilities_free(joint_capabilities);
862         return NULL;
863 }
864
865 /*!
866  * \brief Apply remote SDP's ICE information to our RTP session
867  *
868  * \param state The SDP state on which negotiation has taken place
869  * \param options The SDP options we support
870  * \param remote_sdp The SDP we most recently received
871  * \param remote_m_line The stream on which we are examining ICE candidates
872  */
873 static void update_ice(const struct ast_sdp_state *state, struct ast_rtp_instance *rtp, const struct ast_sdp_options *options,
874         const struct ast_sdp *remote_sdp, const struct ast_sdp_m_line *remote_m_line)
875 {
876         struct ast_rtp_engine_ice *ice;
877         const struct ast_sdp_a_line *attr;
878         unsigned int attr_i;
879
880         /* If ICE support is not enabled or available exit early */
881         if (ast_sdp_options_get_ice(options) != AST_SDP_ICE_ENABLED_STANDARD || !(ice = ast_rtp_instance_get_ice(rtp))) {
882                 return;
883         }
884
885         attr = ast_sdp_m_find_attribute(remote_m_line, "ice-ufrag", -1);
886         if (!attr) {
887                 attr = ast_sdp_find_attribute(remote_sdp, "ice-ufrag", -1);
888         }
889         if (attr) {
890                 ice->set_authentication(rtp, attr->value, NULL);
891         } else {
892                 return;
893         }
894
895         attr = ast_sdp_m_find_attribute(remote_m_line, "ice-pwd", -1);
896         if (!attr) {
897                 attr = ast_sdp_find_attribute(remote_sdp, "ice-pwd", -1);
898         }
899         if (attr) {
900                 ice->set_authentication(rtp, NULL, attr->value);
901         } else {
902                 return;
903         }
904
905         if (ast_sdp_m_find_attribute(remote_m_line, "ice-lite", -1)) {
906                 ice->ice_lite(rtp);
907         }
908
909         /* Find all of the candidates */
910         for (attr_i = 0; attr_i < ast_sdp_m_get_a_count(remote_m_line); ++attr_i) {
911                 char foundation[32];
912                 char transport[32];
913                 char address[INET6_ADDRSTRLEN + 1];
914                 char cand_type[6];
915                 char relay_address[INET6_ADDRSTRLEN + 1] = "";
916                 unsigned int port;
917                 unsigned int relay_port = 0;
918                 struct ast_rtp_engine_ice_candidate candidate = { 0, };
919
920                 attr = ast_sdp_m_get_a(remote_m_line, attr_i);
921
922                 /* If this is not a candidate line skip it */
923                 if (strcmp(attr->name, "candidate")) {
924                         continue;
925                 }
926
927                 if (sscanf(attr->value, "%31s %30u %31s %30u %46s %30u typ %5s %*s %23s %*s %30u",
928                         foundation, &candidate.id, transport, (unsigned *)&candidate.priority, address,
929                         &port, cand_type, relay_address, &relay_port) < 7) {
930                         /* Candidate did not parse properly */
931                         continue;
932                 }
933
934                 if (ast_sdp_options_get_rtcp_mux(options)
935                         && ast_sdp_m_find_attribute(remote_m_line, "rtcp-mux", -1)
936                         && candidate.id > 1) {
937                         /* Remote side may have offered RTP and RTCP candidates. However, if we're using RTCP MUX,
938                          * then we should ignore RTCP candidates.
939                          */
940                         continue;
941                 }
942
943                 candidate.foundation = foundation;
944                 candidate.transport = transport;
945
946                 ast_sockaddr_parse(&candidate.address, address, PARSE_PORT_FORBID);
947                 ast_sockaddr_set_port(&candidate.address, port);
948
949                 if (!strcasecmp(cand_type, "host")) {
950                         candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_HOST;
951                 } else if (!strcasecmp(cand_type, "srflx")) {
952                         candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX;
953                 } else if (!strcasecmp(cand_type, "relay")) {
954                         candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED;
955                 } else {
956                         continue;
957                 }
958
959                 if (!ast_strlen_zero(relay_address)) {
960                         ast_sockaddr_parse(&candidate.relay_address, relay_address, PARSE_PORT_FORBID);
961                 }
962
963                 if (relay_port) {
964                         ast_sockaddr_set_port(&candidate.relay_address, relay_port);
965                 }
966
967                 ice->add_remote_candidate(rtp, &candidate);
968         }
969
970         if (state->role == SDP_ROLE_OFFERER) {
971                 ice->set_role(rtp, AST_RTP_ICE_ROLE_CONTROLLING);
972         } else {
973                 ice->set_role(rtp, AST_RTP_ICE_ROLE_CONTROLLED);
974         }
975
976         ice->start(rtp);
977 }
978
979 /*!
980  * \brief Update RTP instances based on merged SDPs
981  *
982  * RTP instances, when first allocated, cannot make assumptions about what the other
983  * side supports and thus has to go with some default behaviors. This function gets
984  * called after we know both what we support and what the remote endpoint supports.
985  * This way, we can update the RTP instance to reflect what is supported by both
986  * sides.
987  *
988  * \param state The SDP state in which SDPs have been negotiated
989  * \param rtp The RTP wrapper that is being updated
990  * \param options Our locally-supported SDP options
991  * \param remote_sdp The SDP we most recently received
992  * \param remote_m_line The remote SDP stream that corresponds to the RTP instance we are modifying
993  */
994 static void update_rtp_after_merge(const struct ast_sdp_state *state,
995         struct sdp_state_rtp *rtp,
996     const struct ast_sdp_options *options,
997         const struct ast_sdp *remote_sdp,
998         const struct ast_sdp_m_line *remote_m_line)
999 {
1000         struct ast_sdp_c_line *c_line;
1001         struct ast_sockaddr *addrs;
1002
1003         if (!rtp) {
1004                 /* This is a dummy stream */
1005                 return;
1006         }
1007
1008         c_line = remote_m_line->c_line;
1009         if (!c_line) {
1010                 c_line = remote_sdp->c_line;
1011         }
1012         /*
1013          * There must be a c= line somewhere but that would be an error by
1014          * the far end that should have been caught by a validation check
1015          * before we processed the SDP.
1016          */
1017         ast_assert(c_line != NULL);
1018
1019         if (ast_sockaddr_resolve(&addrs, c_line->address, PARSE_PORT_FORBID, AST_AF_UNSPEC) > 0) {
1020                 /* Apply connection information to the RTP instance */
1021                 ast_sockaddr_set_port(addrs, remote_m_line->port);
1022                 ast_rtp_instance_set_remote_address(rtp->instance, addrs);
1023                 ast_free(addrs);
1024         }
1025
1026         if (ast_sdp_options_get_rtcp_mux(options)
1027                 && ast_sdp_m_find_attribute(remote_m_line, "rtcp-mux", -1)) {
1028                 ast_rtp_instance_set_prop(rtp->instance, AST_RTP_PROPERTY_RTCP,
1029                         AST_RTP_INSTANCE_RTCP_MUX);
1030         } else {
1031                 ast_rtp_instance_set_prop(rtp->instance, AST_RTP_PROPERTY_RTCP,
1032                         AST_RTP_INSTANCE_RTCP_STANDARD);
1033         }
1034
1035         update_ice(state, rtp->instance, options, remote_sdp, remote_m_line);
1036 }
1037
1038 /*!
1039  * \brief Update UDPTL instances based on merged SDPs
1040  *
1041  * UDPTL instances, when first allocated, cannot make assumptions about what the other
1042  * side supports and thus has to go with some default behaviors. This function gets
1043  * called after we know both what we support and what the remote endpoint supports.
1044  * This way, we can update the UDPTL instance to reflect what is supported by both
1045  * sides.
1046  *
1047  * \param state The SDP state in which SDPs have been negotiated
1048  * \param udptl The UDPTL instance that is being updated
1049  * \param options Our locally-supported SDP options
1050  * \param remote_sdp The SDP we most recently received
1051  * \param remote_m_line The remote SDP stream that corresponds to the RTP instance we are modifying
1052  */
1053 static void update_udptl_after_merge(const struct ast_sdp_state *state, struct sdp_state_udptl *udptl,
1054     const struct ast_sdp_options *options,
1055         const struct ast_sdp *remote_sdp,
1056         const struct ast_sdp_m_line *remote_m_line)
1057 {
1058         struct ast_sdp_a_line *a_line;
1059         struct ast_sdp_c_line *c_line;
1060         unsigned int fax_max_datagram;
1061         struct ast_sockaddr *addrs;
1062
1063         if (!udptl) {
1064                 /* This is a dummy stream */
1065                 return;
1066         }
1067
1068         a_line = ast_sdp_m_find_attribute(remote_m_line, "t38faxmaxdatagram", -1);
1069         if (!a_line) {
1070                 a_line = ast_sdp_m_find_attribute(remote_m_line, "t38maxdatagram", -1);
1071         }
1072         if (a_line && !ast_sdp_options_get_udptl_far_max_datagram(options) &&
1073                 (sscanf(a_line->value, "%30u", &fax_max_datagram) == 1)) {
1074                 ast_udptl_set_far_max_datagram(udptl->instance, fax_max_datagram);
1075         }
1076
1077         a_line = ast_sdp_m_find_attribute(remote_m_line, "t38faxudpec", -1);
1078         if (a_line) {
1079                 if (!strcasecmp(a_line->value, "t38UDPRedundancy")) {
1080                         ast_udptl_set_error_correction_scheme(udptl->instance, UDPTL_ERROR_CORRECTION_REDUNDANCY);
1081                 } else if (!strcasecmp(a_line->value, "t38UDPFEC")) {
1082                         ast_udptl_set_error_correction_scheme(udptl->instance, UDPTL_ERROR_CORRECTION_FEC);
1083                 } else {
1084                         ast_udptl_set_error_correction_scheme(udptl->instance, UDPTL_ERROR_CORRECTION_NONE);
1085                 }
1086         }
1087
1088         c_line = remote_m_line->c_line;
1089         if (!c_line) {
1090                 c_line = remote_sdp->c_line;
1091         }
1092         /*
1093          * There must be a c= line somewhere but that would be an error by
1094          * the far end that should have been caught by a validation check
1095          * before we processed the SDP.
1096          */
1097         ast_assert(c_line != NULL);
1098
1099         if (ast_sockaddr_resolve(&addrs, c_line->address, PARSE_PORT_FORBID, AST_AF_UNSPEC) > 0) {
1100                 /* Apply connection information to the UDPTL instance */
1101                 ast_sockaddr_set_port(addrs, remote_m_line->port);
1102                 ast_udptl_set_peer(udptl->instance, addrs);
1103                 ast_free(addrs);
1104         }
1105 }
1106
1107 static void set_negotiated_capabilities(struct ast_sdp_state *sdp_state,
1108         struct sdp_state_capabilities *new_capabilities)
1109 {
1110         struct sdp_state_capabilities *old_capabilities = sdp_state->negotiated_capabilities;
1111
1112         sdp_state->negotiated_capabilities = new_capabilities;
1113         sdp_state_capabilities_free(old_capabilities);
1114 }
1115
1116 static void set_proposed_capabilities(struct ast_sdp_state *sdp_state,
1117         struct sdp_state_capabilities *new_capabilities)
1118 {
1119         struct sdp_state_capabilities *old_capabilities = sdp_state->proposed_capabilities;
1120
1121         sdp_state->proposed_capabilities = new_capabilities;
1122         sdp_state_capabilities_free(old_capabilities);
1123 }
1124
1125 static struct ast_sdp *sdp_create_from_state(const struct ast_sdp_state *sdp_state,
1126         const struct sdp_state_capabilities *capabilities);
1127
1128 /*!
1129  * \brief Merge SDPs into a joint SDP.
1130  *
1131  * This function is used to take a remote SDP and merge it with our local
1132  * capabilities to produce a new local SDP. After creating the new local SDP,
1133  * it then iterates through media instances and updates them as necessary. For
1134  * instance, if a specific RTP feature is supported by both us and the far end,
1135  * then we can ensure that the feature is enabled.
1136  *
1137  * \param sdp_state The current SDP state
1138  * \retval -1 Failure
1139  * \retval 0 Success
1140  */
1141 static int merge_sdps(struct ast_sdp_state *sdp_state, const struct ast_sdp *remote_sdp)
1142 {
1143         struct sdp_state_capabilities *joint_capabilities;
1144         struct ast_stream_topology *remote_capabilities;
1145         int i;
1146
1147         remote_capabilities = ast_get_topology_from_sdp(remote_sdp,
1148                 sdp_state->options->g726_non_standard);
1149         if (!remote_capabilities) {
1150                 return -1;
1151         }
1152
1153         joint_capabilities = merge_capabilities(sdp_state, remote_capabilities, 0);
1154         ast_stream_topology_free(remote_capabilities);
1155         if (!joint_capabilities) {
1156                 return -1;
1157         }
1158         set_negotiated_capabilities(sdp_state, joint_capabilities);
1159
1160         if (sdp_state->local_sdp) {
1161                 ast_sdp_free(sdp_state->local_sdp);
1162                 sdp_state->local_sdp = NULL;
1163         }
1164
1165         sdp_state->local_sdp = sdp_create_from_state(sdp_state, joint_capabilities);
1166         if (!sdp_state->local_sdp) {
1167                 return -1;
1168         }
1169
1170         for (i = 0; i < AST_VECTOR_SIZE(&joint_capabilities->streams); ++i) {
1171                 struct sdp_state_stream *state_stream;
1172
1173                 state_stream = AST_VECTOR_GET(&joint_capabilities->streams, i);
1174
1175                 switch (ast_stream_get_type(ast_stream_topology_get_stream(joint_capabilities->topology, i))) {
1176                 case AST_MEDIA_TYPE_AUDIO:
1177                 case AST_MEDIA_TYPE_VIDEO:
1178                         update_rtp_after_merge(sdp_state, state_stream->rtp, sdp_state->options,
1179                                 remote_sdp, ast_sdp_get_m(remote_sdp, i));
1180                         break;
1181                 case AST_MEDIA_TYPE_IMAGE:
1182                         update_udptl_after_merge(sdp_state, state_stream->udptl, sdp_state->options,
1183                                 remote_sdp, ast_sdp_get_m(remote_sdp, i));
1184                         break;
1185                 case AST_MEDIA_TYPE_UNKNOWN:
1186                 case AST_MEDIA_TYPE_TEXT:
1187                 case AST_MEDIA_TYPE_END:
1188                         break;
1189                 }
1190         }
1191
1192         return 0;
1193 }
1194
1195 const struct ast_sdp *ast_sdp_state_get_local_sdp(struct ast_sdp_state *sdp_state)
1196 {
1197         ast_assert(sdp_state != NULL);
1198
1199         if (sdp_state->role == SDP_ROLE_NOT_SET) {
1200                 ast_assert(sdp_state->local_sdp == NULL);
1201                 sdp_state->role = SDP_ROLE_OFFERER;
1202                 sdp_state->local_sdp = sdp_create_from_state(sdp_state, sdp_state->proposed_capabilities);
1203         }
1204
1205         return sdp_state->local_sdp;
1206 }
1207
1208 const void *ast_sdp_state_get_local_sdp_impl(struct ast_sdp_state *sdp_state)
1209 {
1210         const struct ast_sdp *sdp = ast_sdp_state_get_local_sdp(sdp_state);
1211
1212         if (!sdp) {
1213                 return NULL;
1214         }
1215
1216         return ast_sdp_translator_from_sdp(sdp_state->translator, sdp);
1217 }
1218
1219 int ast_sdp_state_set_remote_sdp(struct ast_sdp_state *sdp_state, const struct ast_sdp *sdp)
1220 {
1221         ast_assert(sdp_state != NULL);
1222
1223         if (sdp_state->role == SDP_ROLE_NOT_SET) {
1224                 sdp_state->role = SDP_ROLE_ANSWERER;
1225         }
1226
1227         return merge_sdps(sdp_state, sdp);
1228 }
1229
1230 int ast_sdp_state_set_remote_sdp_from_impl(struct ast_sdp_state *sdp_state, const void *remote)
1231 {
1232         struct ast_sdp *sdp;
1233         int ret;
1234
1235         ast_assert(sdp_state != NULL);
1236
1237         sdp = ast_sdp_translator_to_sdp(sdp_state->translator, remote);
1238         if (!sdp) {
1239                 return -1;
1240         }
1241         ret = ast_sdp_state_set_remote_sdp(sdp_state, sdp);
1242         ast_sdp_free(sdp);
1243         return ret;
1244 }
1245
1246 int ast_sdp_state_reset(struct ast_sdp_state *sdp_state)
1247 {
1248         ast_assert(sdp_state != NULL);
1249
1250         ast_sdp_free(sdp_state->local_sdp);
1251         sdp_state->local_sdp = NULL;
1252
1253         set_proposed_capabilities(sdp_state, NULL);
1254
1255         sdp_state->role = SDP_ROLE_NOT_SET;
1256
1257         return 0;
1258 }
1259
1260 int ast_sdp_state_update_local_topology(struct ast_sdp_state *sdp_state, struct ast_stream_topology *streams)
1261 {
1262         struct sdp_state_capabilities *capabilities;
1263         ast_assert(sdp_state != NULL);
1264         ast_assert(streams != NULL);
1265
1266         capabilities = merge_capabilities(sdp_state, streams, 1);
1267         if (!capabilities) {
1268                 return -1;
1269         }
1270         set_proposed_capabilities(sdp_state, capabilities);
1271
1272         return 0;
1273 }
1274
1275 void ast_sdp_state_set_local_address(struct ast_sdp_state *sdp_state, struct ast_sockaddr *address)
1276 {
1277         ast_assert(sdp_state != NULL);
1278
1279         if (!address) {
1280                 ast_sockaddr_setnull(&sdp_state->proposed_capabilities->connection_address);
1281         } else {
1282                 ast_sockaddr_copy(&sdp_state->proposed_capabilities->connection_address, address);
1283         }
1284 }
1285
1286 int ast_sdp_state_set_connection_address(struct ast_sdp_state *sdp_state, int stream_index,
1287         struct ast_sockaddr *address)
1288 {
1289         struct sdp_state_stream *stream_state;
1290         ast_assert(sdp_state != NULL);
1291
1292         stream_state = sdp_state_get_stream(sdp_state, stream_index);
1293         if (!stream_state) {
1294                 return -1;
1295         }
1296
1297         if (!address) {
1298                 ast_sockaddr_setnull(&stream_state->connection_address);
1299         } else {
1300                 ast_sockaddr_copy(&stream_state->connection_address, address);
1301         }
1302
1303         return 0;
1304 }
1305
1306 void ast_sdp_state_set_locally_held(struct ast_sdp_state *sdp_state,
1307         int stream_index, unsigned int locally_held)
1308 {
1309         struct sdp_state_stream *stream_state;
1310         ast_assert(sdp_state != NULL);
1311
1312         stream_state = sdp_state_get_stream(sdp_state, stream_index);
1313         if (!stream_state) {
1314                 return;
1315         }
1316
1317         stream_state->locally_held = locally_held;
1318 }
1319
1320 unsigned int ast_sdp_state_get_locally_held(const struct ast_sdp_state *sdp_state,
1321         int stream_index)
1322 {
1323         struct sdp_state_stream *stream_state;
1324         ast_assert(sdp_state != NULL);
1325
1326         stream_state = sdp_state_get_stream(sdp_state, stream_index);
1327         if (!stream_state) {
1328                 return 0;
1329         }
1330
1331         return stream_state->locally_held;
1332 }
1333
1334 void ast_sdp_state_set_t38_parameters(struct ast_sdp_state *sdp_state,
1335         int stream_index, struct ast_control_t38_parameters *params)
1336 {
1337         struct sdp_state_stream *stream_state;
1338         ast_assert(sdp_state != NULL && params != NULL);
1339
1340         stream_state = sdp_state_get_stream(sdp_state, stream_index);
1341         if (!stream_state) {
1342                 return;
1343         }
1344
1345         stream_state->t38_local_params = *params;
1346 }
1347
1348 /*!
1349  * \brief Add SSRC-level attributes if appropriate.
1350  *
1351  * This function does nothing if the SDP options indicate not to add SSRC-level attributes.
1352  *
1353  * Currently, the only attribute added is cname, which is retrieved from the RTP instance.
1354  *
1355  * \param m_line The m_line on which to add the SSRC attributes
1356  * \param options Options that indicate what, if any, SSRC attributes to add
1357  * \param rtp RTP instance from which we get SSRC-level information
1358  */
1359 static void add_ssrc_attributes(struct ast_sdp_m_line *m_line, const struct ast_sdp_options *options,
1360         struct ast_rtp_instance *rtp)
1361 {
1362         struct ast_sdp_a_line *a_line;
1363         char attr_buffer[128];
1364
1365         if (!ast_sdp_options_get_ssrc(options)) {
1366                 return;
1367         }
1368
1369         snprintf(attr_buffer, sizeof(attr_buffer), "%u cname:%s", ast_rtp_instance_get_ssrc(rtp),
1370                 ast_rtp_instance_get_cname(rtp));
1371
1372         a_line = ast_sdp_a_alloc("ssrc", attr_buffer);
1373         if (!a_line) {
1374                 return;
1375         }
1376         ast_sdp_m_add_a(m_line, a_line);
1377 }
1378
1379 static int sdp_add_m_from_rtp_stream(struct ast_sdp *sdp, const struct ast_sdp_state *sdp_state,
1380         const struct ast_sdp_options *options, const struct sdp_state_capabilities *capabilities, int stream_index)
1381 {
1382         struct ast_stream *stream;
1383         struct ast_sdp_m_line *m_line;
1384         struct ast_format_cap *caps;
1385         int i;
1386         int rtp_code;
1387         int rtp_port;
1388         int min_packet_size = 0;
1389         int max_packet_size = 0;
1390         enum ast_media_type media_type;
1391         char tmp[64];
1392         struct sdp_state_stream *stream_state;
1393         struct ast_rtp_instance *rtp;
1394         struct ast_sdp_a_line *a_line;
1395
1396         stream = ast_stream_topology_get_stream(capabilities->topology, stream_index);
1397
1398         ast_assert(sdp && options && stream);
1399
1400         caps = ast_stream_get_formats(stream);
1401
1402         stream_state = AST_VECTOR_GET(&capabilities->streams, stream_index);
1403         if (stream_state->rtp && caps && ast_format_cap_count(caps)) {
1404                 rtp = stream_state->rtp->instance;
1405         } else {
1406                 /* This is a disabled stream */
1407                 rtp = NULL;
1408         }
1409
1410         if (rtp) {
1411                 struct ast_sockaddr address_rtp;
1412
1413                 if (ast_sdp_state_get_stream_connection_address(sdp_state, 0, &address_rtp)) {
1414                         return -1;
1415                 }
1416                 rtp_port = ast_sockaddr_port(&address_rtp);
1417         } else {
1418                 rtp_port = 0;
1419         }
1420
1421         m_line = ast_sdp_m_alloc(
1422                 ast_codec_media_type2str(ast_stream_get_type(stream)),
1423                 rtp_port, 1,
1424                 options->encryption != AST_SDP_ENCRYPTION_DISABLED ? "RTP/SAVP" : "RTP/AVP",
1425                 NULL);
1426         if (!m_line) {
1427                 return -1;
1428         }
1429
1430         if (rtp_port) {
1431                 /* Stream is not declined/disabled */
1432                 for (i = 0; i < ast_format_cap_count(caps); i++) {
1433                         struct ast_format *format = ast_format_cap_get_format(caps, i);
1434
1435                         rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(rtp), 1,
1436                                 format, 0);
1437                         if (rtp_code == -1) {
1438                                 ast_log(LOG_WARNING,"Unable to get rtp codec payload code for %s\n",
1439                                         ast_format_get_name(format));
1440                                 ao2_ref(format, -1);
1441                                 continue;
1442                         }
1443
1444                         if (ast_sdp_m_add_format(m_line, options, rtp_code, 1, format, 0)) {
1445                                 ast_sdp_m_free(m_line);
1446                                 ao2_ref(format, -1);
1447                                 return -1;
1448                         }
1449
1450                         if (ast_format_get_maximum_ms(format)
1451                                 && ((ast_format_get_maximum_ms(format) < max_packet_size)
1452                                         || !max_packet_size)) {
1453                                 max_packet_size = ast_format_get_maximum_ms(format);
1454                         }
1455
1456                         ao2_ref(format, -1);
1457                 }
1458
1459                 media_type = ast_stream_get_type(stream);
1460                 if (media_type != AST_MEDIA_TYPE_VIDEO
1461                         && (options->dtmf == AST_SDP_DTMF_RFC_4733 || options->dtmf == AST_SDP_DTMF_AUTO)) {
1462                         i = AST_RTP_DTMF;
1463                         rtp_code = ast_rtp_codecs_payload_code(
1464                                 ast_rtp_instance_get_codecs(rtp), 0, NULL, i);
1465                         if (-1 < rtp_code) {
1466                                 if (ast_sdp_m_add_format(m_line, options, rtp_code, 0, NULL, i)) {
1467                                         ast_sdp_m_free(m_line);
1468                                         return -1;
1469                                 }
1470
1471                                 snprintf(tmp, sizeof(tmp), "%d 0-16", rtp_code);
1472                                 a_line = ast_sdp_a_alloc("fmtp", tmp);
1473                                 if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1474                                         ast_sdp_a_free(a_line);
1475                                         ast_sdp_m_free(m_line);
1476                                         return -1;
1477                                 }
1478                         }
1479                 }
1480
1481                 /* If ptime is set add it as an attribute */
1482                 min_packet_size = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(rtp));
1483                 if (!min_packet_size) {
1484                         min_packet_size = ast_format_cap_get_framing(caps);
1485                 }
1486                 if (min_packet_size) {
1487                         snprintf(tmp, sizeof(tmp), "%d", min_packet_size);
1488
1489                         a_line = ast_sdp_a_alloc("ptime", tmp);
1490                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1491                                 ast_sdp_a_free(a_line);
1492                                 ast_sdp_m_free(m_line);
1493                                 return -1;
1494                         }
1495                 }
1496
1497                 if (max_packet_size) {
1498                         snprintf(tmp, sizeof(tmp), "%d", max_packet_size);
1499                         a_line = ast_sdp_a_alloc("maxptime", tmp);
1500                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1501                                 ast_sdp_a_free(a_line);
1502                                 ast_sdp_m_free(m_line);
1503                                 return -1;
1504                         }
1505                 }
1506
1507                 a_line = ast_sdp_a_alloc(ast_sdp_state_get_locally_held(sdp_state, stream_index)
1508                         ? "sendonly" : "sendrecv", "");
1509                 if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1510                         ast_sdp_a_free(a_line);
1511                         ast_sdp_m_free(m_line);
1512                         return -1;
1513                 }
1514
1515                 add_ssrc_attributes(m_line, options, rtp);
1516         } else {
1517                 /* Declined/disabled stream */
1518                 struct ast_sdp_payload *payload;
1519                 const char *fmt;
1520
1521                 /*
1522                  * Add a static payload type placeholder to the declined/disabled stream.
1523                  *
1524                  * XXX We should use the default payload type in the received offer but
1525                  * we don't have that available.
1526                  */
1527                 switch (ast_stream_get_type(stream)) {
1528                 default:
1529                 case AST_MEDIA_TYPE_AUDIO:
1530                         fmt = "0"; /* ulaw */
1531                         break;
1532                 case AST_MEDIA_TYPE_VIDEO:
1533                         fmt = "31"; /* H.261 */
1534                         break;
1535                 }
1536                 payload = ast_sdp_payload_alloc(fmt);
1537                 if (!payload || ast_sdp_m_add_payload(m_line, payload)) {
1538                         ast_sdp_payload_free(payload);
1539                         ast_sdp_m_free(m_line);
1540                         return -1;
1541                 }
1542         }
1543
1544         if (ast_sdp_add_m(sdp, m_line)) {
1545                 ast_sdp_m_free(m_line);
1546                 return -1;
1547         }
1548
1549         return 0;
1550 }
1551
1552 /*! \brief Get Max T.38 Transmission rate from T38 capabilities */
1553 static unsigned int t38_get_rate(enum ast_control_t38_rate rate)
1554 {
1555         switch (rate) {
1556         case AST_T38_RATE_2400:
1557                 return 2400;
1558         case AST_T38_RATE_4800:
1559                 return 4800;
1560         case AST_T38_RATE_7200:
1561                 return 7200;
1562         case AST_T38_RATE_9600:
1563                 return 9600;
1564         case AST_T38_RATE_12000:
1565                 return 12000;
1566         case AST_T38_RATE_14400:
1567                 return 14400;
1568         default:
1569                 return 0;
1570         }
1571 }
1572
1573 static int sdp_add_m_from_udptl_stream(struct ast_sdp *sdp, const struct ast_sdp_state *sdp_state,
1574         const struct ast_sdp_options *options, const struct sdp_state_capabilities *capabilities, int stream_index)
1575 {
1576         struct ast_stream *stream;
1577         struct ast_sdp_m_line *m_line;
1578         struct ast_sdp_payload *payload;
1579         char tmp[64];
1580         struct sdp_state_udptl *udptl;
1581         struct ast_sdp_a_line *a_line;
1582         struct sdp_state_stream *stream_state;
1583         int udptl_port;
1584
1585         stream = ast_stream_topology_get_stream(capabilities->topology, stream_index);
1586
1587         ast_assert(sdp && options && stream);
1588
1589         stream_state = AST_VECTOR_GET(&capabilities->streams, stream_index);
1590         if (stream_state->udptl) {
1591                 udptl = stream_state->udptl;
1592         } else {
1593                 /* This is a disabled stream */
1594                 udptl = NULL;
1595         }
1596
1597         if (udptl) {
1598                 struct ast_sockaddr address_udptl;
1599
1600                 if (ast_sdp_state_get_stream_connection_address(sdp_state, 0, &address_udptl)) {
1601                         return -1;
1602                 }
1603                 udptl_port = ast_sockaddr_port(&address_udptl);
1604         } else {
1605                 udptl_port = 0;
1606         }
1607
1608         m_line = ast_sdp_m_alloc(
1609                 ast_codec_media_type2str(ast_stream_get_type(stream)),
1610                 udptl_port, 1, "udptl", NULL);
1611         if (!m_line) {
1612                 return -1;
1613         }
1614
1615         payload = ast_sdp_payload_alloc("t38");
1616         if (!payload || ast_sdp_m_add_payload(m_line, payload)) {
1617                 ast_sdp_payload_free(payload);
1618                 ast_sdp_m_free(m_line);
1619                 return -1;
1620         }
1621
1622         if (udptl_port) {
1623                 /* Stream is not declined/disabled */
1624                 stream_state = sdp_state_get_stream(sdp_state, stream_index);
1625
1626                 snprintf(tmp, sizeof(tmp), "%u", stream_state->t38_local_params.version);
1627                 a_line = ast_sdp_a_alloc("T38FaxVersion", tmp);
1628                 if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1629                         ast_sdp_a_free(a_line);
1630                         ast_sdp_m_free(m_line);
1631                         return -1;
1632                 }
1633
1634                 snprintf(tmp, sizeof(tmp), "%u", t38_get_rate(stream_state->t38_local_params.rate));
1635                 a_line = ast_sdp_a_alloc("T38FaxMaxBitRate", tmp);
1636                 if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1637                         ast_sdp_a_free(a_line);
1638                         ast_sdp_m_free(m_line);
1639                         return -1;
1640                 }
1641
1642                 if (stream_state->t38_local_params.fill_bit_removal) {
1643                         a_line = ast_sdp_a_alloc("T38FaxFillBitRemoval", "");
1644                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1645                                 ast_sdp_a_free(a_line);
1646                                 ast_sdp_m_free(m_line);
1647                                 return -1;
1648                         }
1649                 }
1650
1651                 if (stream_state->t38_local_params.transcoding_mmr) {
1652                         a_line = ast_sdp_a_alloc("T38FaxTranscodingMMR", "");
1653                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1654                                 ast_sdp_a_free(a_line);
1655                                 ast_sdp_m_free(m_line);
1656                                 return -1;
1657                         }
1658                 }
1659
1660                 if (stream_state->t38_local_params.transcoding_jbig) {
1661                         a_line = ast_sdp_a_alloc("T38FaxTranscodingJBIG", "");
1662                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1663                                 ast_sdp_a_free(a_line);
1664                                 ast_sdp_m_free(m_line);
1665                                 return -1;
1666                         }
1667                 }
1668
1669                 switch (stream_state->t38_local_params.rate_management) {
1670                 case AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF:
1671                         a_line = ast_sdp_a_alloc("T38FaxRateManagement", "transferredTCF");
1672                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1673                                 ast_sdp_a_free(a_line);
1674                                 ast_sdp_m_free(m_line);
1675                                 return -1;
1676                         }
1677                         break;
1678                 case AST_T38_RATE_MANAGEMENT_LOCAL_TCF:
1679                         a_line = ast_sdp_a_alloc("T38FaxRateManagement", "localTCF");
1680                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1681                                 ast_sdp_a_free(a_line);
1682                                 ast_sdp_m_free(m_line);
1683                                 return -1;
1684                         }
1685                         break;
1686                 }
1687
1688                 snprintf(tmp, sizeof(tmp), "%u", ast_udptl_get_local_max_datagram(udptl->instance));
1689                 a_line = ast_sdp_a_alloc("T38FaxMaxDatagram", tmp);
1690                 if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1691                         ast_sdp_a_free(a_line);
1692                         ast_sdp_m_free(m_line);
1693                         return -1;
1694                 }
1695
1696                 switch (ast_udptl_get_error_correction_scheme(udptl->instance)) {
1697                 case UDPTL_ERROR_CORRECTION_NONE:
1698                         break;
1699                 case UDPTL_ERROR_CORRECTION_FEC:
1700                         a_line = ast_sdp_a_alloc("T38FaxUdpEC", "t38UDPFEC");
1701                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1702                                 ast_sdp_a_free(a_line);
1703                                 ast_sdp_m_free(m_line);
1704                                 return -1;
1705                         }
1706                         break;
1707                 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
1708                         a_line = ast_sdp_a_alloc("T38FaxUdpEC", "t38UDPRedundancy");
1709                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1710                                 ast_sdp_a_free(a_line);
1711                                 ast_sdp_m_free(m_line);
1712                                 return -1;
1713                         }
1714                         break;
1715                 }
1716         }
1717
1718         if (ast_sdp_add_m(sdp, m_line)) {
1719                 ast_sdp_m_free(m_line);
1720                 return -1;
1721         }
1722
1723         return 0;
1724 }
1725
1726 /*!
1727  * \brief Create an SDP based on current SDP state
1728  *
1729  * \param sdp_state The current SDP state
1730  * \retval NULL Failed to create SDP
1731  * \retval non-NULL Newly-created SDP
1732  */
1733 static struct ast_sdp *sdp_create_from_state(const struct ast_sdp_state *sdp_state,
1734         const struct sdp_state_capabilities *capabilities)
1735 {
1736         struct ast_sdp *sdp = NULL;
1737         struct ast_stream_topology *topology;
1738         const struct ast_sdp_options *options;
1739         int stream_num;
1740         struct ast_sdp_o_line *o_line = NULL;
1741         struct ast_sdp_c_line *c_line = NULL;
1742         struct ast_sdp_s_line *s_line = NULL;
1743         struct ast_sdp_t_line *t_line = NULL;
1744         char *address_type;
1745         struct timeval tv = ast_tvnow();
1746         uint32_t t;
1747         int stream_count;
1748
1749         options = ast_sdp_state_get_options(sdp_state);
1750         topology = capabilities->topology;
1751
1752         t = tv.tv_sec + 2208988800UL;
1753         address_type = (strchr(options->media_address, ':') ? "IP6" : "IP4");
1754
1755         o_line = ast_sdp_o_alloc(options->sdpowner, t, t, address_type, options->media_address);
1756         if (!o_line) {
1757                 goto error;
1758         }
1759         c_line = ast_sdp_c_alloc(address_type, options->media_address);
1760         if (!c_line) {
1761                 goto error;
1762         }
1763         s_line = ast_sdp_s_alloc(options->sdpsession);
1764         if (!s_line) {
1765                 goto error;
1766         }
1767         t_line = ast_sdp_t_alloc(0, 0);
1768         if (!t_line) {
1769                 goto error;
1770         }
1771
1772         sdp = ast_sdp_alloc(o_line, c_line, s_line, t_line);
1773         if (!sdp) {
1774                 goto error;
1775         }
1776
1777         stream_count = ast_stream_topology_get_count(topology);
1778
1779         for (stream_num = 0; stream_num < stream_count; stream_num++) {
1780                 switch (ast_stream_get_type(ast_stream_topology_get_stream(topology, stream_num))) {
1781                 case AST_MEDIA_TYPE_AUDIO:
1782                 case AST_MEDIA_TYPE_VIDEO:
1783                         if (sdp_add_m_from_rtp_stream(sdp, sdp_state, options, capabilities, stream_num)) {
1784                                 goto error;
1785                         }
1786                         break;
1787                 case AST_MEDIA_TYPE_IMAGE:
1788                         if (sdp_add_m_from_udptl_stream(sdp, sdp_state, options, capabilities, stream_num)) {
1789                                 goto error;
1790                         }
1791                         break;
1792                 case AST_MEDIA_TYPE_UNKNOWN:
1793                 case AST_MEDIA_TYPE_TEXT:
1794                 case AST_MEDIA_TYPE_END:
1795                         break;
1796                 }
1797         }
1798
1799         return sdp;
1800
1801 error:
1802         if (sdp) {
1803                 ast_sdp_free(sdp);
1804         } else {
1805                 ast_sdp_t_free(t_line);
1806                 ast_sdp_s_free(s_line);
1807                 ast_sdp_c_free(c_line);
1808                 ast_sdp_o_free(o_line);
1809         }
1810
1811         return NULL;
1812 }
1813