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