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