SDP: Explicitly stop a RTP instance before destoying it.
[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);
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);
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         if (!rtp) {
1001                 /* This is a dummy stream */
1002                 return;
1003         }
1004
1005         if (ast_sdp_options_get_rtcp_mux(options)
1006                 && ast_sdp_m_find_attribute(remote_m_line, "rtcp-mux", -1)) {
1007                 ast_rtp_instance_set_prop(rtp->instance, AST_RTP_PROPERTY_RTCP,
1008                         AST_RTP_INSTANCE_RTCP_MUX);
1009         } else {
1010                 ast_rtp_instance_set_prop(rtp->instance, AST_RTP_PROPERTY_RTCP,
1011                         AST_RTP_INSTANCE_RTCP_STANDARD);
1012         }
1013
1014         if (ast_sdp_options_get_ice(options) == AST_SDP_ICE_ENABLED_STANDARD) {
1015                 update_ice(state, rtp->instance, options, remote_sdp, remote_m_line);
1016         }
1017 }
1018
1019 /*!
1020  * \brief Update UDPTL instances based on merged SDPs
1021  *
1022  * UDPTL instances, when first allocated, cannot make assumptions about what the other
1023  * side supports and thus has to go with some default behaviors. This function gets
1024  * called after we know both what we support and what the remote endpoint supports.
1025  * This way, we can update the UDPTL instance to reflect what is supported by both
1026  * sides.
1027  *
1028  * \param state The SDP state in which SDPs have been negotiated
1029  * \param udptl The UDPTL instance that is being updated
1030  * \param options Our locally-supported SDP options
1031  * \param remote_sdp The SDP we most recently received
1032  * \param remote_m_line The remote SDP stream that corresponds to the RTP instance we are modifying
1033  */
1034 static void update_udptl_after_merge(const struct ast_sdp_state *state, struct sdp_state_udptl *udptl,
1035     const struct ast_sdp_options *options,
1036         const struct ast_sdp *remote_sdp,
1037         const struct ast_sdp_m_line *remote_m_line)
1038 {
1039         struct ast_sdp_a_line *a_line;
1040         struct ast_sdp_c_line *c_line;
1041         unsigned int fax_max_datagram;
1042         struct ast_sockaddr *addrs;
1043
1044         if (!udptl) {
1045                 /* This is a dummy stream */
1046                 return;
1047         }
1048
1049         a_line = ast_sdp_m_find_attribute(remote_m_line, "t38faxmaxdatagram", -1);
1050         if (!a_line) {
1051                 a_line = ast_sdp_m_find_attribute(remote_m_line, "t38maxdatagram", -1);
1052         }
1053         if (a_line && !ast_sdp_options_get_udptl_far_max_datagram(options) &&
1054                 (sscanf(a_line->value, "%30u", &fax_max_datagram) == 1)) {
1055                 ast_udptl_set_far_max_datagram(udptl->instance, fax_max_datagram);
1056         }
1057
1058         a_line = ast_sdp_m_find_attribute(remote_m_line, "t38faxudpec", -1);
1059         if (a_line) {
1060                 if (!strcasecmp(a_line->value, "t38UDPRedundancy")) {
1061                         ast_udptl_set_error_correction_scheme(udptl->instance, UDPTL_ERROR_CORRECTION_REDUNDANCY);
1062                 } else if (!strcasecmp(a_line->value, "t38UDPFEC")) {
1063                         ast_udptl_set_error_correction_scheme(udptl->instance, UDPTL_ERROR_CORRECTION_FEC);
1064                 } else {
1065                         ast_udptl_set_error_correction_scheme(udptl->instance, UDPTL_ERROR_CORRECTION_NONE);
1066                 }
1067         }
1068
1069         c_line = remote_sdp->c_line;
1070         if (remote_m_line->c_line) {
1071                 c_line = remote_m_line->c_line;
1072         }
1073
1074         if (ast_sockaddr_resolve(&addrs, c_line->address, PARSE_PORT_FORBID, AST_AF_UNSPEC) > 0) {
1075                 ast_sockaddr_set_port(addrs, remote_m_line->port);
1076                 ast_udptl_set_peer(udptl->instance, addrs);
1077                 ast_free(addrs);
1078         }
1079 }
1080
1081 static void set_negotiated_capabilities(struct ast_sdp_state *sdp_state,
1082         struct sdp_state_capabilities *new_capabilities)
1083 {
1084         struct sdp_state_capabilities *old_capabilities = sdp_state->negotiated_capabilities;
1085
1086         sdp_state->negotiated_capabilities = new_capabilities;
1087         sdp_state_capabilities_free(old_capabilities);
1088 }
1089
1090 static void set_proposed_capabilities(struct ast_sdp_state *sdp_state,
1091         struct sdp_state_capabilities *new_capabilities)
1092 {
1093         struct sdp_state_capabilities *old_capabilities = sdp_state->proposed_capabilities;
1094
1095         sdp_state->proposed_capabilities = new_capabilities;
1096         sdp_state_capabilities_free(old_capabilities);
1097 }
1098
1099 static struct ast_sdp *sdp_create_from_state(const struct ast_sdp_state *sdp_state,
1100         const struct sdp_state_capabilities *capabilities);
1101
1102 /*!
1103  * \brief Merge SDPs into a joint SDP.
1104  *
1105  * This function is used to take a remote SDP and merge it with our local
1106  * capabilities to produce a new local SDP. After creating the new local SDP,
1107  * it then iterates through media instances and updates them as necessary. For
1108  * instance, if a specific RTP feature is supported by both us and the far end,
1109  * then we can ensure that the feature is enabled.
1110  *
1111  * \param sdp_state The current SDP state
1112  * \retval -1 Failure
1113  * \retval 0 Success
1114  */
1115 static int merge_sdps(struct ast_sdp_state *sdp_state, const struct ast_sdp *remote_sdp)
1116 {
1117         struct sdp_state_capabilities *joint_capabilities;
1118         struct ast_stream_topology *remote_capabilities;
1119         int i;
1120
1121         remote_capabilities = ast_get_topology_from_sdp(remote_sdp,
1122                 sdp_state->options->g726_non_standard);
1123         if (!remote_capabilities) {
1124                 return -1;
1125         }
1126
1127         joint_capabilities = merge_capabilities(sdp_state, remote_capabilities, 0);
1128         ast_stream_topology_free(remote_capabilities);
1129         if (!joint_capabilities) {
1130                 return -1;
1131         }
1132         set_negotiated_capabilities(sdp_state, joint_capabilities);
1133
1134         if (sdp_state->local_sdp) {
1135                 ast_sdp_free(sdp_state->local_sdp);
1136                 sdp_state->local_sdp = NULL;
1137         }
1138
1139         sdp_state->local_sdp = sdp_create_from_state(sdp_state, joint_capabilities);
1140         if (!sdp_state->local_sdp) {
1141                 return -1;
1142         }
1143
1144         for (i = 0; i < AST_VECTOR_SIZE(&joint_capabilities->streams); ++i) {
1145                 struct sdp_state_stream *state_stream;
1146
1147                 state_stream = AST_VECTOR_GET(&joint_capabilities->streams, i);
1148
1149                 switch (ast_stream_get_type(ast_stream_topology_get_stream(joint_capabilities->topology, i))) {
1150                 case AST_MEDIA_TYPE_AUDIO:
1151                 case AST_MEDIA_TYPE_VIDEO:
1152                         update_rtp_after_merge(sdp_state, state_stream->rtp, sdp_state->options,
1153                                 remote_sdp, ast_sdp_get_m(remote_sdp, i));
1154                         break;
1155                 case AST_MEDIA_TYPE_IMAGE:
1156                         update_udptl_after_merge(sdp_state, state_stream->udptl, sdp_state->options,
1157                                 remote_sdp, ast_sdp_get_m(remote_sdp, i));
1158                         break;
1159                 case AST_MEDIA_TYPE_UNKNOWN:
1160                 case AST_MEDIA_TYPE_TEXT:
1161                 case AST_MEDIA_TYPE_END:
1162                         break;
1163                 }
1164         }
1165
1166         return 0;
1167 }
1168
1169 const struct ast_sdp *ast_sdp_state_get_local_sdp(struct ast_sdp_state *sdp_state)
1170 {
1171         ast_assert(sdp_state != NULL);
1172
1173         if (sdp_state->role == SDP_ROLE_NOT_SET) {
1174                 ast_assert(sdp_state->local_sdp == NULL);
1175                 sdp_state->role = SDP_ROLE_OFFERER;
1176                 sdp_state->local_sdp = sdp_create_from_state(sdp_state, sdp_state->proposed_capabilities);
1177         }
1178
1179         return sdp_state->local_sdp;
1180 }
1181
1182 const void *ast_sdp_state_get_local_sdp_impl(struct ast_sdp_state *sdp_state)
1183 {
1184         const struct ast_sdp *sdp = ast_sdp_state_get_local_sdp(sdp_state);
1185
1186         if (!sdp) {
1187                 return NULL;
1188         }
1189
1190         return ast_sdp_translator_from_sdp(sdp_state->translator, sdp);
1191 }
1192
1193 int ast_sdp_state_set_remote_sdp(struct ast_sdp_state *sdp_state, const struct ast_sdp *sdp)
1194 {
1195         ast_assert(sdp_state != NULL);
1196
1197         if (sdp_state->role == SDP_ROLE_NOT_SET) {
1198                 sdp_state->role = SDP_ROLE_ANSWERER;
1199         }
1200
1201         return merge_sdps(sdp_state, sdp);
1202 }
1203
1204 int ast_sdp_state_set_remote_sdp_from_impl(struct ast_sdp_state *sdp_state, const void *remote)
1205 {
1206         struct ast_sdp *sdp;
1207         int ret;
1208
1209         ast_assert(sdp_state != NULL);
1210
1211         sdp = ast_sdp_translator_to_sdp(sdp_state->translator, remote);
1212         if (!sdp) {
1213                 return -1;
1214         }
1215         ret = ast_sdp_state_set_remote_sdp(sdp_state, sdp);
1216         ast_sdp_free(sdp);
1217         return ret;
1218 }
1219
1220 int ast_sdp_state_reset(struct ast_sdp_state *sdp_state)
1221 {
1222         ast_assert(sdp_state != NULL);
1223
1224         ast_sdp_free(sdp_state->local_sdp);
1225         sdp_state->local_sdp = NULL;
1226
1227         set_proposed_capabilities(sdp_state, NULL);
1228
1229         sdp_state->role = SDP_ROLE_NOT_SET;
1230
1231         return 0;
1232 }
1233
1234 int ast_sdp_state_update_local_topology(struct ast_sdp_state *sdp_state, struct ast_stream_topology *streams)
1235 {
1236         struct sdp_state_capabilities *capabilities;
1237         ast_assert(sdp_state != NULL);
1238         ast_assert(streams != NULL);
1239
1240         capabilities = merge_capabilities(sdp_state, streams, 1);
1241         if (!capabilities) {
1242                 return -1;
1243         }
1244         set_proposed_capabilities(sdp_state, capabilities);
1245
1246         return 0;
1247 }
1248
1249 void ast_sdp_state_set_local_address(struct ast_sdp_state *sdp_state, struct ast_sockaddr *address)
1250 {
1251         ast_assert(sdp_state != NULL);
1252
1253         if (!address) {
1254                 ast_sockaddr_setnull(&sdp_state->proposed_capabilities->connection_address);
1255         } else {
1256                 ast_sockaddr_copy(&sdp_state->proposed_capabilities->connection_address, address);
1257         }
1258 }
1259
1260 int ast_sdp_state_set_connection_address(struct ast_sdp_state *sdp_state, int stream_index,
1261         struct ast_sockaddr *address)
1262 {
1263         struct sdp_state_stream *stream_state;
1264         ast_assert(sdp_state != NULL);
1265
1266         stream_state = sdp_state_get_stream(sdp_state, stream_index);
1267         if (!stream_state) {
1268                 return -1;
1269         }
1270
1271         if (!address) {
1272                 ast_sockaddr_setnull(&stream_state->connection_address);
1273         } else {
1274                 ast_sockaddr_copy(&stream_state->connection_address, address);
1275         }
1276
1277         return 0;
1278 }
1279
1280 void ast_sdp_state_set_locally_held(struct ast_sdp_state *sdp_state,
1281         int stream_index, unsigned int locally_held)
1282 {
1283         struct sdp_state_stream *stream_state;
1284         ast_assert(sdp_state != NULL);
1285
1286         stream_state = sdp_state_get_stream(sdp_state, stream_index);
1287         if (!stream_state) {
1288                 return;
1289         }
1290
1291         stream_state->locally_held = locally_held;
1292 }
1293
1294 unsigned int ast_sdp_state_get_locally_held(const struct ast_sdp_state *sdp_state,
1295         int stream_index)
1296 {
1297         struct sdp_state_stream *stream_state;
1298         ast_assert(sdp_state != NULL);
1299
1300         stream_state = sdp_state_get_stream(sdp_state, stream_index);
1301         if (!stream_state) {
1302                 return 0;
1303         }
1304
1305         return stream_state->locally_held;
1306 }
1307
1308 void ast_sdp_state_set_t38_parameters(struct ast_sdp_state *sdp_state,
1309         int stream_index, struct ast_control_t38_parameters *params)
1310 {
1311         struct sdp_state_stream *stream_state;
1312         ast_assert(sdp_state != NULL && params != NULL);
1313
1314         stream_state = sdp_state_get_stream(sdp_state, stream_index);
1315         if (!stream_state) {
1316                 return;
1317         }
1318
1319         stream_state->t38_local_params = *params;
1320 }
1321
1322 /*!
1323  * \brief Add SSRC-level attributes if appropriate.
1324  *
1325  * This function does nothing if the SDP options indicate not to add SSRC-level attributes.
1326  *
1327  * Currently, the only attribute added is cname, which is retrieved from the RTP instance.
1328  *
1329  * \param m_line The m_line on which to add the SSRC attributes
1330  * \param options Options that indicate what, if any, SSRC attributes to add
1331  * \param rtp RTP instance from which we get SSRC-level information
1332  */
1333 static void add_ssrc_attributes(struct ast_sdp_m_line *m_line, const struct ast_sdp_options *options,
1334         struct ast_rtp_instance *rtp)
1335 {
1336         struct ast_sdp_a_line *a_line;
1337         char attr_buffer[128];
1338
1339         if (!ast_sdp_options_get_ssrc(options)) {
1340                 return;
1341         }
1342
1343         snprintf(attr_buffer, sizeof(attr_buffer), "%u cname:%s", ast_rtp_instance_get_ssrc(rtp),
1344                 ast_rtp_instance_get_cname(rtp));
1345
1346         a_line = ast_sdp_a_alloc("ssrc", attr_buffer);
1347         if (!a_line) {
1348                 return;
1349         }
1350         ast_sdp_m_add_a(m_line, a_line);
1351 }
1352
1353 static int sdp_add_m_from_rtp_stream(struct ast_sdp *sdp, const struct ast_sdp_state *sdp_state,
1354         const struct ast_sdp_options *options, const struct sdp_state_capabilities *capabilities, int stream_index)
1355 {
1356         struct ast_stream *stream;
1357         struct ast_sdp_m_line *m_line;
1358         struct ast_format_cap *caps;
1359         int i;
1360         int rtp_code;
1361         int rtp_port;
1362         int min_packet_size = 0;
1363         int max_packet_size = 0;
1364         enum ast_media_type media_type;
1365         char tmp[64];
1366         struct sdp_state_stream *stream_state;
1367         struct ast_rtp_instance *rtp;
1368         struct ast_sdp_a_line *a_line;
1369
1370         stream = ast_stream_topology_get_stream(capabilities->topology, stream_index);
1371
1372         ast_assert(sdp && options && stream);
1373
1374         caps = ast_stream_get_formats(stream);
1375
1376         stream_state = AST_VECTOR_GET(&capabilities->streams, stream_index);
1377         if (stream_state->rtp && caps && ast_format_cap_count(caps)) {
1378                 rtp = stream_state->rtp->instance;
1379         } else {
1380                 /* This is a disabled stream */
1381                 rtp = NULL;
1382         }
1383
1384         if (rtp) {
1385                 struct ast_sockaddr address_rtp;
1386
1387                 if (ast_sdp_state_get_stream_connection_address(sdp_state, 0, &address_rtp)) {
1388                         return -1;
1389                 }
1390                 rtp_port = ast_sockaddr_port(&address_rtp);
1391         } else {
1392                 rtp_port = 0;
1393         }
1394
1395         m_line = ast_sdp_m_alloc(
1396                 ast_codec_media_type2str(ast_stream_get_type(stream)),
1397                 rtp_port, 1,
1398                 options->encryption != AST_SDP_ENCRYPTION_DISABLED ? "RTP/SAVP" : "RTP/AVP",
1399                 NULL);
1400         if (!m_line) {
1401                 return -1;
1402         }
1403
1404         if (rtp_port) {
1405                 /* Stream is not declined/disabled */
1406                 for (i = 0; i < ast_format_cap_count(caps); i++) {
1407                         struct ast_format *format = ast_format_cap_get_format(caps, i);
1408
1409                         rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(rtp), 1,
1410                                 format, 0);
1411                         if (rtp_code == -1) {
1412                                 ast_log(LOG_WARNING,"Unable to get rtp codec payload code for %s\n",
1413                                         ast_format_get_name(format));
1414                                 ao2_ref(format, -1);
1415                                 continue;
1416                         }
1417
1418                         if (ast_sdp_m_add_format(m_line, options, rtp_code, 1, format, 0)) {
1419                                 ast_sdp_m_free(m_line);
1420                                 ao2_ref(format, -1);
1421                                 return -1;
1422                         }
1423
1424                         if (ast_format_get_maximum_ms(format)
1425                                 && ((ast_format_get_maximum_ms(format) < max_packet_size)
1426                                         || !max_packet_size)) {
1427                                 max_packet_size = ast_format_get_maximum_ms(format);
1428                         }
1429
1430                         ao2_ref(format, -1);
1431                 }
1432
1433                 media_type = ast_stream_get_type(stream);
1434                 if (media_type != AST_MEDIA_TYPE_VIDEO
1435                         && (options->dtmf == AST_SDP_DTMF_RFC_4733 || options->dtmf == AST_SDP_DTMF_AUTO)) {
1436                         i = AST_RTP_DTMF;
1437                         rtp_code = ast_rtp_codecs_payload_code(
1438                                 ast_rtp_instance_get_codecs(rtp), 0, NULL, i);
1439                         if (-1 < rtp_code) {
1440                                 if (ast_sdp_m_add_format(m_line, options, rtp_code, 0, NULL, i)) {
1441                                         ast_sdp_m_free(m_line);
1442                                         return -1;
1443                                 }
1444
1445                                 snprintf(tmp, sizeof(tmp), "%d 0-16", rtp_code);
1446                                 a_line = ast_sdp_a_alloc("fmtp", tmp);
1447                                 if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1448                                         ast_sdp_a_free(a_line);
1449                                         ast_sdp_m_free(m_line);
1450                                         return -1;
1451                                 }
1452                         }
1453                 }
1454
1455                 /* If ptime is set add it as an attribute */
1456                 min_packet_size = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(rtp));
1457                 if (!min_packet_size) {
1458                         min_packet_size = ast_format_cap_get_framing(caps);
1459                 }
1460                 if (min_packet_size) {
1461                         snprintf(tmp, sizeof(tmp), "%d", min_packet_size);
1462
1463                         a_line = ast_sdp_a_alloc("ptime", tmp);
1464                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1465                                 ast_sdp_a_free(a_line);
1466                                 ast_sdp_m_free(m_line);
1467                                 return -1;
1468                         }
1469                 }
1470
1471                 if (max_packet_size) {
1472                         snprintf(tmp, sizeof(tmp), "%d", max_packet_size);
1473                         a_line = ast_sdp_a_alloc("maxptime", tmp);
1474                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1475                                 ast_sdp_a_free(a_line);
1476                                 ast_sdp_m_free(m_line);
1477                                 return -1;
1478                         }
1479                 }
1480
1481                 a_line = ast_sdp_a_alloc(ast_sdp_state_get_locally_held(sdp_state, stream_index)
1482                         ? "sendonly" : "sendrecv", "");
1483                 if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1484                         ast_sdp_a_free(a_line);
1485                         ast_sdp_m_free(m_line);
1486                         return -1;
1487                 }
1488
1489                 add_ssrc_attributes(m_line, options, rtp);
1490         } else {
1491                 /* Declined/disabled stream */
1492                 struct ast_sdp_payload *payload;
1493                 const char *fmt;
1494
1495                 /*
1496                  * Add a static payload type placeholder to the declined/disabled stream.
1497                  *
1498                  * XXX We should use the default payload type in the received offer but
1499                  * we don't have that available.
1500                  */
1501                 switch (ast_stream_get_type(stream)) {
1502                 default:
1503                 case AST_MEDIA_TYPE_AUDIO:
1504                         fmt = "0"; /* ulaw */
1505                         break;
1506                 case AST_MEDIA_TYPE_VIDEO:
1507                         fmt = "31"; /* H.261 */
1508                         break;
1509                 }
1510                 payload = ast_sdp_payload_alloc(fmt);
1511                 if (!payload || ast_sdp_m_add_payload(m_line, payload)) {
1512                         ast_sdp_payload_free(payload);
1513                         ast_sdp_m_free(m_line);
1514                         return -1;
1515                 }
1516         }
1517
1518         if (ast_sdp_add_m(sdp, m_line)) {
1519                 ast_sdp_m_free(m_line);
1520                 return -1;
1521         }
1522
1523         return 0;
1524 }
1525
1526 /*! \brief Get Max T.38 Transmission rate from T38 capabilities */
1527 static unsigned int t38_get_rate(enum ast_control_t38_rate rate)
1528 {
1529         switch (rate) {
1530         case AST_T38_RATE_2400:
1531                 return 2400;
1532         case AST_T38_RATE_4800:
1533                 return 4800;
1534         case AST_T38_RATE_7200:
1535                 return 7200;
1536         case AST_T38_RATE_9600:
1537                 return 9600;
1538         case AST_T38_RATE_12000:
1539                 return 12000;
1540         case AST_T38_RATE_14400:
1541                 return 14400;
1542         default:
1543                 return 0;
1544         }
1545 }
1546
1547 static int sdp_add_m_from_udptl_stream(struct ast_sdp *sdp, const struct ast_sdp_state *sdp_state,
1548         const struct ast_sdp_options *options, const struct sdp_state_capabilities *capabilities, int stream_index)
1549 {
1550         struct ast_stream *stream;
1551         struct ast_sdp_m_line *m_line;
1552         struct ast_sdp_payload *payload;
1553         char tmp[64];
1554         struct sdp_state_udptl *udptl;
1555         struct ast_sdp_a_line *a_line;
1556         struct sdp_state_stream *stream_state;
1557         int udptl_port;
1558
1559         stream = ast_stream_topology_get_stream(capabilities->topology, stream_index);
1560
1561         ast_assert(sdp && options && stream);
1562
1563         stream_state = AST_VECTOR_GET(&capabilities->streams, stream_index);
1564         if (stream_state->udptl) {
1565                 udptl = stream_state->udptl;
1566         } else {
1567                 /* This is a disabled stream */
1568                 udptl = NULL;
1569         }
1570
1571         if (udptl) {
1572                 struct ast_sockaddr address_udptl;
1573
1574                 if (ast_sdp_state_get_stream_connection_address(sdp_state, 0, &address_udptl)) {
1575                         return -1;
1576                 }
1577                 udptl_port = ast_sockaddr_port(&address_udptl);
1578         } else {
1579                 udptl_port = 0;
1580         }
1581
1582         m_line = ast_sdp_m_alloc(
1583                 ast_codec_media_type2str(ast_stream_get_type(stream)),
1584                 udptl_port, 1, "udptl", NULL);
1585         if (!m_line) {
1586                 return -1;
1587         }
1588
1589         payload = ast_sdp_payload_alloc("t38");
1590         if (!payload || ast_sdp_m_add_payload(m_line, payload)) {
1591                 ast_sdp_payload_free(payload);
1592                 ast_sdp_m_free(m_line);
1593                 return -1;
1594         }
1595
1596         if (udptl_port) {
1597                 /* Stream is not declined/disabled */
1598                 stream_state = sdp_state_get_stream(sdp_state, stream_index);
1599
1600                 snprintf(tmp, sizeof(tmp), "%u", stream_state->t38_local_params.version);
1601                 a_line = ast_sdp_a_alloc("T38FaxVersion", tmp);
1602                 if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1603                         ast_sdp_a_free(a_line);
1604                         ast_sdp_m_free(m_line);
1605                         return -1;
1606                 }
1607
1608                 snprintf(tmp, sizeof(tmp), "%u", t38_get_rate(stream_state->t38_local_params.rate));
1609                 a_line = ast_sdp_a_alloc("T38FaxMaxBitRate", tmp);
1610                 if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1611                         ast_sdp_a_free(a_line);
1612                         ast_sdp_m_free(m_line);
1613                         return -1;
1614                 }
1615
1616                 if (stream_state->t38_local_params.fill_bit_removal) {
1617                         a_line = ast_sdp_a_alloc("T38FaxFillBitRemoval", "");
1618                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1619                                 ast_sdp_a_free(a_line);
1620                                 ast_sdp_m_free(m_line);
1621                                 return -1;
1622                         }
1623                 }
1624
1625                 if (stream_state->t38_local_params.transcoding_mmr) {
1626                         a_line = ast_sdp_a_alloc("T38FaxTranscodingMMR", "");
1627                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1628                                 ast_sdp_a_free(a_line);
1629                                 ast_sdp_m_free(m_line);
1630                                 return -1;
1631                         }
1632                 }
1633
1634                 if (stream_state->t38_local_params.transcoding_jbig) {
1635                         a_line = ast_sdp_a_alloc("T38FaxTranscodingJBIG", "");
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
1643                 switch (stream_state->t38_local_params.rate_management) {
1644                 case AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF:
1645                         a_line = ast_sdp_a_alloc("T38FaxRateManagement", "transferredTCF");
1646                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1647                                 ast_sdp_a_free(a_line);
1648                                 ast_sdp_m_free(m_line);
1649                                 return -1;
1650                         }
1651                         break;
1652                 case AST_T38_RATE_MANAGEMENT_LOCAL_TCF:
1653                         a_line = ast_sdp_a_alloc("T38FaxRateManagement", "localTCF");
1654                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1655                                 ast_sdp_a_free(a_line);
1656                                 ast_sdp_m_free(m_line);
1657                                 return -1;
1658                         }
1659                         break;
1660                 }
1661
1662                 snprintf(tmp, sizeof(tmp), "%u", ast_udptl_get_local_max_datagram(udptl->instance));
1663                 a_line = ast_sdp_a_alloc("T38FaxMaxDatagram", tmp);
1664                 if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1665                         ast_sdp_a_free(a_line);
1666                         ast_sdp_m_free(m_line);
1667                         return -1;
1668                 }
1669
1670                 switch (ast_udptl_get_error_correction_scheme(udptl->instance)) {
1671                 case UDPTL_ERROR_CORRECTION_NONE:
1672                         break;
1673                 case UDPTL_ERROR_CORRECTION_FEC:
1674                         a_line = ast_sdp_a_alloc("T38FaxUdpEC", "t38UDPFEC");
1675                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1676                                 ast_sdp_a_free(a_line);
1677                                 ast_sdp_m_free(m_line);
1678                                 return -1;
1679                         }
1680                         break;
1681                 case UDPTL_ERROR_CORRECTION_REDUNDANCY:
1682                         a_line = ast_sdp_a_alloc("T38FaxUdpEC", "t38UDPRedundancy");
1683                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
1684                                 ast_sdp_a_free(a_line);
1685                                 ast_sdp_m_free(m_line);
1686                                 return -1;
1687                         }
1688                         break;
1689                 }
1690         }
1691
1692         if (ast_sdp_add_m(sdp, m_line)) {
1693                 ast_sdp_m_free(m_line);
1694                 return -1;
1695         }
1696
1697         return 0;
1698 }
1699
1700 /*!
1701  * \brief Create an SDP based on current SDP state
1702  *
1703  * \param sdp_state The current SDP state
1704  * \retval NULL Failed to create SDP
1705  * \retval non-NULL Newly-created SDP
1706  */
1707 static struct ast_sdp *sdp_create_from_state(const struct ast_sdp_state *sdp_state,
1708         const struct sdp_state_capabilities *capabilities)
1709 {
1710         struct ast_sdp *sdp = NULL;
1711         struct ast_stream_topology *topology;
1712         const struct ast_sdp_options *options;
1713         int stream_num;
1714         struct ast_sdp_o_line *o_line = NULL;
1715         struct ast_sdp_c_line *c_line = NULL;
1716         struct ast_sdp_s_line *s_line = NULL;
1717         struct ast_sdp_t_line *t_line = NULL;
1718         char *address_type;
1719         struct timeval tv = ast_tvnow();
1720         uint32_t t;
1721         int stream_count;
1722
1723         options = ast_sdp_state_get_options(sdp_state);
1724         topology = capabilities->topology;
1725
1726         t = tv.tv_sec + 2208988800UL;
1727         address_type = (strchr(options->media_address, ':') ? "IP6" : "IP4");
1728
1729         o_line = ast_sdp_o_alloc(options->sdpowner, t, t, address_type, options->media_address);
1730         if (!o_line) {
1731                 goto error;
1732         }
1733         c_line = ast_sdp_c_alloc(address_type, options->media_address);
1734         if (!c_line) {
1735                 goto error;
1736         }
1737
1738         s_line = ast_sdp_s_alloc(options->sdpsession);
1739         if (!s_line) {
1740                 goto error;
1741         }
1742
1743         sdp = ast_sdp_alloc(o_line, c_line, s_line, NULL);
1744         if (!sdp) {
1745                 goto error;
1746         }
1747
1748         stream_count = ast_stream_topology_get_count(topology);
1749
1750         for (stream_num = 0; stream_num < stream_count; stream_num++) {
1751                 switch (ast_stream_get_type(ast_stream_topology_get_stream(topology, stream_num))) {
1752                 case AST_MEDIA_TYPE_AUDIO:
1753                 case AST_MEDIA_TYPE_VIDEO:
1754                         if (sdp_add_m_from_rtp_stream(sdp, sdp_state, options, capabilities, stream_num)) {
1755                                 goto error;
1756                         }
1757                         break;
1758                 case AST_MEDIA_TYPE_IMAGE:
1759                         if (sdp_add_m_from_udptl_stream(sdp, sdp_state, options, capabilities, stream_num)) {
1760                                 goto error;
1761                         }
1762                         break;
1763                 case AST_MEDIA_TYPE_UNKNOWN:
1764                 case AST_MEDIA_TYPE_TEXT:
1765                 case AST_MEDIA_TYPE_END:
1766                         break;
1767                 }
1768         }
1769
1770         return sdp;
1771
1772 error:
1773         if (sdp) {
1774                 ast_sdp_free(sdp);
1775         } else {
1776                 ast_sdp_t_free(t_line);
1777                 ast_sdp_s_free(s_line);
1778                 ast_sdp_c_free(c_line);
1779                 ast_sdp_o_free(o_line);
1780         }
1781
1782         return NULL;
1783 }
1784