BuildSystem: Remove unused variables.
[asterisk/asterisk.git] / main / sdp_state.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2017, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 #include "asterisk.h"
20 #include "asterisk/sdp_state.h"
21 #include "asterisk/sdp_options.h"
22 #include "asterisk/sdp_translator.h"
23 #include "asterisk/vector.h"
24 #include "asterisk/utils.h"
25 #include "asterisk/netsock2.h"
26 #include "asterisk/rtp_engine.h"
27 #include "asterisk/format.h"
28 #include "asterisk/format_cap.h"
29 #include "asterisk/config.h"
30 #include "asterisk/codec.h"
31 #include "asterisk/udptl.h"
32
33 #include "asterisk/sdp.h"
34 #include "asterisk/stream.h"
35
36 #include "sdp_private.h"
37
38 enum ast_sdp_role {
39         /*!
40          * \brief The role has not yet been determined.
41          *
42          * When the SDP state is allocated, this is the starting role.
43          * Similarly, when the SDP state is reset, the role is reverted
44          * to this.
45          */
46         SDP_ROLE_NOT_SET,
47         /*!
48          * \brief We are the offerer.
49          *
50          * If a local SDP is requested before a remote SDP has been set, then
51          * we assume the role of offerer. This means that we will generate an
52          * SDP from the local capabilities and configured options.
53          */
54         SDP_ROLE_OFFERER,
55         /*!
56          * \brief We are the answerer.
57          *
58          * If a remote SDP is set before a local SDP is requested, then we
59          * assume the role of answerer. This means that we will generate an
60          * SDP based on a merge of the remote capabilities and our local capabilities.
61          */
62         SDP_ROLE_ANSWERER,
63 };
64
65 typedef int (*state_fn)(struct ast_sdp_state *state);
66
67 struct sdp_state_rtp {
68         /*! The underlying RTP instance */
69         struct ast_rtp_instance *instance;
70 };
71
72 struct sdp_state_udptl {
73         /*! The underlying UDPTL instance */
74         struct ast_udptl *instance;
75 };
76
77 struct sdp_state_stream {
78         /*! Type of the stream */
79         enum ast_media_type type;
80         union {
81                 /*! The underlying RTP instance */
82                 struct sdp_state_rtp *rtp;
83                 /*! The underlying UDPTL instance */
84                 struct sdp_state_udptl *udptl;
85         };
86         /*! An explicit connection address for this stream */
87         struct ast_sockaddr connection_address;
88         /*!
89          * \brief Stream is on hold by remote side
90          *
91          * \note This flag is never set on the
92          * sdp_state->proposed_capabilities->streams states.  This is useful
93          * when the remote sends us a reINVITE with a deferred SDP to place
94          * us on and off of hold.
95          */
96         unsigned int remotely_held:1;
97         /*! Stream is on hold by local side */
98         unsigned int locally_held:1;
99         /*! UDPTL session parameters */
100         struct ast_control_t38_parameters t38_local_params;
101 };
102
103 static int sdp_is_stream_type_supported(enum ast_media_type type)
104 {
105         int is_supported = 0;
106
107         switch (type) {
108         case AST_MEDIA_TYPE_AUDIO:
109         case AST_MEDIA_TYPE_VIDEO:
110         case AST_MEDIA_TYPE_IMAGE:
111                 is_supported = 1;
112                 break;
113         case AST_MEDIA_TYPE_UNKNOWN:
114         case AST_MEDIA_TYPE_TEXT:
115         case AST_MEDIA_TYPE_END:
116                 break;
117         }
118         return is_supported;
119 }
120
121 static void sdp_state_rtp_destroy(void *obj)
122 {
123         struct sdp_state_rtp *rtp = obj;
124
125         if (rtp->instance) {
126                 ast_rtp_instance_stop(rtp->instance);
127                 ast_rtp_instance_destroy(rtp->instance);
128         }
129 }
130
131 static void sdp_state_udptl_destroy(void *obj)
132 {
133         struct sdp_state_udptl *udptl = obj;
134
135         if (udptl->instance) {
136                 ast_udptl_destroy(udptl->instance);
137         }
138 }
139
140 static void sdp_state_stream_free(struct sdp_state_stream *state_stream)
141 {
142         switch (state_stream->type) {
143         case AST_MEDIA_TYPE_AUDIO:
144         case AST_MEDIA_TYPE_VIDEO:
145                 ao2_cleanup(state_stream->rtp);
146                 break;
147         case AST_MEDIA_TYPE_IMAGE:
148                 ao2_cleanup(state_stream->udptl);
149                 break;
150         case AST_MEDIA_TYPE_UNKNOWN:
151         case AST_MEDIA_TYPE_TEXT:
152         case AST_MEDIA_TYPE_END:
153                 break;
154         }
155         ast_free(state_stream);
156 }
157
158 AST_VECTOR(sdp_state_streams, struct sdp_state_stream *);
159
160 struct sdp_state_capabilities {
161         /*! Stream topology */
162         struct ast_stream_topology *topology;
163         /*! Additional information about the streams */
164         struct sdp_state_streams streams;
165 };
166
167 static void sdp_state_capabilities_free(struct sdp_state_capabilities *capabilities)
168 {
169         if (!capabilities) {
170                 return;
171         }
172
173         ast_stream_topology_free(capabilities->topology);
174         AST_VECTOR_CALLBACK_VOID(&capabilities->streams, sdp_state_stream_free);
175         AST_VECTOR_FREE(&capabilities->streams);
176         ast_free(capabilities);
177 }
178
179 /*! \brief Internal function which creates an RTP instance */
180 static struct sdp_state_rtp *create_rtp(const struct ast_sdp_options *options,
181         enum ast_media_type media_type)
182 {
183         struct sdp_state_rtp *rtp;
184         struct ast_rtp_engine_ice *ice;
185         static struct ast_sockaddr address_rtp;
186         struct ast_sockaddr *media_address = &address_rtp;
187
188         if (!ast_strlen_zero(options->interface_address)) {
189                 if (!ast_sockaddr_parse(&address_rtp, options->interface_address, 0)) {
190                         ast_log(LOG_ERROR, "Attempted to bind RTP to invalid media address: %s\n",
191                                 options->interface_address);
192                         return NULL;
193                 }
194         } else {
195                 if (ast_check_ipv6()) {
196                         ast_sockaddr_parse(&address_rtp, "::", 0);
197                 } else {
198                         ast_sockaddr_parse(&address_rtp, "0.0.0.0", 0);
199                 }
200         }
201
202         rtp = ao2_alloc_options(sizeof(*rtp), sdp_state_rtp_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
203         if (!rtp) {
204                 return NULL;
205         }
206
207         rtp->instance = ast_rtp_instance_new(options->rtp_engine,
208                 ast_sdp_options_get_sched_type(options, media_type), media_address, NULL);
209         if (!rtp->instance) {
210                 ast_log(LOG_ERROR, "Unable to create RTP instance using RTP engine '%s'\n",
211                         options->rtp_engine);
212                 ao2_ref(rtp, -1);
213                 return NULL;
214         }
215
216         ast_rtp_instance_set_prop(rtp->instance, AST_RTP_PROPERTY_RTCP,
217                 AST_RTP_INSTANCE_RTCP_STANDARD);
218         ast_rtp_instance_set_prop(rtp->instance, AST_RTP_PROPERTY_NAT,
219                 options->rtp_symmetric);
220
221         if (options->ice == AST_SDP_ICE_DISABLED
222                 && (ice = ast_rtp_instance_get_ice(rtp->instance))) {
223                 ice->stop(rtp->instance);
224         }
225
226         if (options->dtmf == AST_SDP_DTMF_RFC_4733 || options->dtmf == AST_SDP_DTMF_AUTO) {
227                 ast_rtp_instance_dtmf_mode_set(rtp->instance, AST_RTP_DTMF_MODE_RFC2833);
228                 ast_rtp_instance_set_prop(rtp->instance, AST_RTP_PROPERTY_DTMF, 1);
229         } else if (options->dtmf == AST_SDP_DTMF_INBAND) {
230                 ast_rtp_instance_dtmf_mode_set(rtp->instance, AST_RTP_DTMF_MODE_INBAND);
231         }
232
233         switch (media_type) {
234         case AST_MEDIA_TYPE_AUDIO:
235                 if (options->tos_audio || options->cos_audio) {
236                         ast_rtp_instance_set_qos(rtp->instance, options->tos_audio,
237                                 options->cos_audio, "SIP RTP Audio");
238                 }
239                 break;
240         case AST_MEDIA_TYPE_VIDEO:
241                 if (options->tos_video || options->cos_video) {
242                         ast_rtp_instance_set_qos(rtp->instance, options->tos_video,
243                                 options->cos_video, "SIP RTP Video");
244                 }
245                 break;
246         case AST_MEDIA_TYPE_IMAGE:
247         case AST_MEDIA_TYPE_TEXT:
248         case AST_MEDIA_TYPE_UNKNOWN:
249         case AST_MEDIA_TYPE_END:
250                 break;
251         }
252
253         ast_rtp_instance_set_last_rx(rtp->instance, time(NULL));
254
255         return rtp;
256 }
257
258 /*! \brief Internal function which creates a UDPTL instance */
259 static struct sdp_state_udptl *create_udptl(const struct ast_sdp_options *options)
260 {
261         struct sdp_state_udptl *udptl;
262         static struct ast_sockaddr address_udptl;
263         struct ast_sockaddr *media_address = &address_udptl;
264
265         if (!ast_strlen_zero(options->interface_address)) {
266                 if (!ast_sockaddr_parse(&address_udptl, options->interface_address, 0)) {
267                         ast_log(LOG_ERROR, "Attempted to bind UDPTL to invalid media address: %s\n",
268                                 options->interface_address);
269                         return NULL;
270                 }
271         } else {
272                 if (ast_check_ipv6()) {
273                         ast_sockaddr_parse(&address_udptl, "::", 0);
274                 } else {
275                         ast_sockaddr_parse(&address_udptl, "0.0.0.0", 0);
276                 }
277         }
278
279         udptl = ao2_alloc_options(sizeof(*udptl), sdp_state_udptl_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
280         if (!udptl) {
281                 return NULL;
282         }
283
284         udptl->instance = ast_udptl_new_with_bindaddr(NULL, NULL, 0, media_address);
285         if (!udptl->instance) {
286                 ao2_ref(udptl, -1);
287                 return NULL;
288         }
289
290         ast_udptl_set_error_correction_scheme(udptl->instance, ast_sdp_options_get_udptl_error_correction(options));
291         ast_udptl_setnat(udptl->instance, ast_sdp_options_get_udptl_symmetric(options));
292         ast_udptl_set_far_max_datagram(udptl->instance, ast_sdp_options_get_udptl_far_max_datagram(options));
293
294         return udptl;
295 }
296
297 static struct ast_stream *merge_local_stream(const struct ast_sdp_options *options,
298         const struct ast_stream *update);
299
300 static struct sdp_state_capabilities *sdp_initialize_state_capabilities(const struct ast_stream_topology *topology,
301         const struct ast_sdp_options *options)
302 {
303         struct sdp_state_capabilities *capabilities;
304         struct ast_stream *stream;
305         unsigned int topology_count;
306         unsigned int max_streams;
307         unsigned int idx;
308
309         capabilities = ast_calloc(1, sizeof(*capabilities));
310         if (!capabilities) {
311                 return NULL;
312         }
313
314         capabilities->topology = ast_stream_topology_alloc();
315         if (!capabilities->topology) {
316                 sdp_state_capabilities_free(capabilities);
317                 return NULL;
318         }
319
320         max_streams = ast_sdp_options_get_max_streams(options);
321         if (topology) {
322                 topology_count = ast_stream_topology_get_count(topology);
323         } else {
324                 topology_count = 0;
325         }
326
327         /* Gather acceptable streams from the initial topology */
328         for (idx = 0; idx < topology_count; ++idx) {
329                 stream = ast_stream_topology_get_stream(topology, idx);
330                 if (!sdp_is_stream_type_supported(ast_stream_get_type(stream))) {
331                         /* Delete the unsupported stream from the initial topology */
332                         continue;
333                 }
334                 if (max_streams <= ast_stream_topology_get_count(capabilities->topology)) {
335                         /* Cannot support any more streams */
336                         break;
337                 }
338
339                 stream = merge_local_stream(options, stream);
340                 if (!stream) {
341                         sdp_state_capabilities_free(capabilities);
342                         return NULL;
343                 }
344
345                 if (ast_stream_topology_append_stream(capabilities->topology, stream) < 0) {
346                         ast_stream_free(stream);
347                         sdp_state_capabilities_free(capabilities);
348                         return NULL;
349                 }
350         }
351
352         /*
353          * Remove trailing declined streams from the initial built topology.
354          * No need to waste space in the SDP with these unused slots.
355          */
356         for (idx = ast_stream_topology_get_count(capabilities->topology); idx--;) {
357                 stream = ast_stream_topology_get_stream(capabilities->topology, idx);
358                 if (ast_stream_get_state(stream) != AST_STREAM_STATE_REMOVED) {
359                         break;
360                 }
361                 ast_stream_topology_del_stream(capabilities->topology, idx);
362         }
363
364         topology_count = ast_stream_topology_get_count(capabilities->topology);
365         if (AST_VECTOR_INIT(&capabilities->streams, topology_count)) {
366                 sdp_state_capabilities_free(capabilities);
367                 return NULL;
368         }
369
370         for (idx = 0; idx < topology_count; ++idx) {
371                 struct sdp_state_stream *state_stream;
372
373                 state_stream = ast_calloc(1, sizeof(*state_stream));
374                 if (!state_stream) {
375                         sdp_state_capabilities_free(capabilities);
376                         return NULL;
377                 }
378
379                 stream = ast_stream_topology_get_stream(capabilities->topology, idx);
380                 state_stream->type = ast_stream_get_type(stream);
381                 if (ast_stream_get_state(stream) != AST_STREAM_STATE_REMOVED) {
382                         switch (state_stream->type) {
383                         case AST_MEDIA_TYPE_AUDIO:
384                         case AST_MEDIA_TYPE_VIDEO:
385                                 state_stream->rtp = create_rtp(options, state_stream->type);
386                                 if (!state_stream->rtp) {
387                                         sdp_state_stream_free(state_stream);
388                                         sdp_state_capabilities_free(capabilities);
389                                         return NULL;
390                                 }
391                                 break;
392                         case AST_MEDIA_TYPE_IMAGE:
393                                 state_stream->udptl = create_udptl(options);
394                                 if (!state_stream->udptl) {
395                                         sdp_state_stream_free(state_stream);
396                                         sdp_state_capabilities_free(capabilities);
397                                         return NULL;
398                                 }
399                                 break;
400                         case AST_MEDIA_TYPE_UNKNOWN:
401                         case AST_MEDIA_TYPE_TEXT:
402                         case AST_MEDIA_TYPE_END:
403                                 /* Unsupported stream type already handled earlier */
404                                 ast_assert(0);
405                                 break;
406                         }
407                 }
408
409                 if (AST_VECTOR_APPEND(&capabilities->streams, state_stream)) {
410                         sdp_state_stream_free(state_stream);
411                         sdp_state_capabilities_free(capabilities);
412                         return NULL;
413                 }
414         }
415
416         return capabilities;
417 }
418
419 /*!
420  * \brief SDP state, the main structure used to keep track of SDP negotiation
421  * and settings.
422  *
423  * Most fields are pretty self-explanatory, but negotiated_capabilities and
424  * proposed_capabilities could use some further explanation. When an SDP
425  * state is allocated, a stream topology is provided that dictates the
426  * types of streams to offer in the resultant SDP. At the time the SDP
427  * is allocated, this topology is used to create the proposed_capabilities.
428  *
429  * If we are the SDP offerer, then the proposed_capabilities are what are used
430  * to generate the offer SDP. When the answer SDP arrives, the proposed capabilities
431  * are merged with the answer SDP to create the negotiated capabilities.
432  *
433  * If we are the SDP answerer, then the incoming offer SDP is merged with our
434  * proposed capabilities to create the negotiated capabilities. These negotiated
435  * capabilities are what we send in our answer SDP.
436  *
437  * Any changes that a user of the API performs will occur on the proposed capabilities.
438  * The negotiated capabilities are only altered based on actual SDP negotiation. This is
439  * done so that the negotiated capabilities can be fallen back on if the proposed
440  * capabilities run into some sort of issue.
441  */
442 struct ast_sdp_state {
443         /*! Current capabilities */
444         struct sdp_state_capabilities *negotiated_capabilities;
445         /*! Proposed capabilities */
446         struct sdp_state_capabilities *proposed_capabilities;
447         /*!
448          * \brief New topology waiting to be merged.
449          *
450          * \details
451          * Repeated topology updates are merged into each other here until
452          * negotiations are restarted and we create an offer.
453          */
454         struct ast_stream_topology *pending_topology_update;
455         /*! Local SDP. Generated via the options and negotiated/proposed capabilities. */
456         struct ast_sdp *local_sdp;
457         /*! Saved remote SDP */
458         struct ast_sdp *remote_sdp;
459         /*! SDP options. Configured options beyond media capabilities. */
460         struct ast_sdp_options *options;
461         /*! Translator that puts SDPs into the expected representation */
462         struct ast_sdp_translator *translator;
463         /*! An explicit global connection address */
464         struct ast_sockaddr connection_address;
465         /*! The role that we occupy in SDP negotiation */
466         enum ast_sdp_role role;
467         /*! TRUE if all streams on hold by local side */
468         unsigned int locally_held:1;
469         /*! TRUE if the remote offer resulted in all streams being declined. */
470         unsigned int remote_offer_rejected:1;
471 };
472
473 struct ast_sdp_state *ast_sdp_state_alloc(struct ast_stream_topology *topology,
474         struct ast_sdp_options *options)
475 {
476         struct ast_sdp_state *sdp_state;
477
478         sdp_state = ast_calloc(1, sizeof(*sdp_state));
479         if (!sdp_state) {
480                 return NULL;
481         }
482
483         sdp_state->options = options;
484
485         sdp_state->translator = ast_sdp_translator_new(ast_sdp_options_get_impl(sdp_state->options));
486         if (!sdp_state->translator) {
487                 ast_sdp_state_free(sdp_state);
488                 return NULL;
489         }
490
491         sdp_state->proposed_capabilities = sdp_initialize_state_capabilities(topology, options);
492         if (!sdp_state->proposed_capabilities) {
493                 ast_sdp_state_free(sdp_state);
494                 return NULL;
495         }
496
497         sdp_state->role = SDP_ROLE_NOT_SET;
498
499         return sdp_state;
500 }
501
502 void ast_sdp_state_free(struct ast_sdp_state *sdp_state)
503 {
504         if (!sdp_state) {
505                 return;
506         }
507
508         sdp_state_capabilities_free(sdp_state->negotiated_capabilities);
509         sdp_state_capabilities_free(sdp_state->proposed_capabilities);
510         ao2_cleanup(sdp_state->local_sdp);
511         ao2_cleanup(sdp_state->remote_sdp);
512         ast_sdp_options_free(sdp_state->options);
513         ast_sdp_translator_free(sdp_state->translator);
514         ast_free(sdp_state);
515 }
516
517 /*!
518  * \internal
519  * \brief Allow a configured callback to alter the new negotiated joint topology.
520  * \since 15.0.0
521  *
522  * \details
523  * The callback can alter topology stream names, formats, or decline streams.
524  *
525  * \param sdp_state
526  * \param topology Joint topology that we intend to generate the answer SDP.
527  *
528  * \return Nothing
529  */
530 static void sdp_state_cb_answerer_modify_topology(const struct ast_sdp_state *sdp_state,
531         struct ast_stream_topology *topology)
532 {
533         ast_sdp_answerer_modify_cb cb;
534
535         cb = ast_sdp_options_get_answerer_modify_cb(sdp_state->options);
536         if (cb) {
537                 void *context;
538                 const struct ast_stream_topology *neg_topology;/*!< Last negotiated topology */
539 #ifdef AST_DEVMODE
540                 struct ast_stream *stream;
541                 int idx;
542                 enum ast_media_type type[ast_stream_topology_get_count(topology)];
543                 enum ast_stream_state state[ast_stream_topology_get_count(topology)];
544
545                 /*
546                  * Save stream types and states to validate that they don't
547                  * get changed unexpectedly.
548                  */
549                 for (idx = 0; idx < ast_stream_topology_get_count(topology); ++idx) {
550                         stream = ast_stream_topology_get_stream(topology, idx);
551                         type[idx] = ast_stream_get_type(stream);
552                         state[idx] = ast_stream_get_state(stream);
553                 }
554 #endif
555
556                 context = ast_sdp_options_get_state_context(sdp_state->options);
557                 neg_topology = sdp_state->negotiated_capabilities
558                         ? sdp_state->negotiated_capabilities->topology : NULL;
559                 cb(context, neg_topology, topology);
560
561 #ifdef AST_DEVMODE
562                 for (idx = 0; idx < ast_stream_topology_get_count(topology); ++idx) {
563                         stream = ast_stream_topology_get_stream(topology, idx);
564
565                         /* Check that active streams have at least one format */
566                         ast_assert(ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED
567                                 || (ast_stream_get_formats(stream)
568                                         && ast_format_cap_count(ast_stream_get_formats(stream))));
569
570                         /* Check that stream types didn't change. */
571                         ast_assert(type[idx] == ast_stream_get_type(stream));
572
573                         /* Check that streams didn't get resurected. */
574                         ast_assert(state[idx] != AST_STREAM_STATE_REMOVED
575                                 || ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED);
576                 }
577 #endif
578         }
579 }
580
581 /*!
582  * \internal
583  * \brief Allow a configured callback to alter the merged local topology.
584  * \since 15.0.0
585  *
586  * \details
587  * The callback can modify streams in the merged topology.  The
588  * callback can decline, add/remove/update formats, or rename
589  * streams.  Changing anything else on the streams is likely to not
590  * end well.
591  *
592  * \param sdp_state
593  * \param topology Merged topology that we intend to generate the offer SDP.
594  *
595  * \return Nothing
596  */
597 static void sdp_state_cb_offerer_modify_topology(const struct ast_sdp_state *sdp_state,
598         struct ast_stream_topology *topology)
599 {
600         ast_sdp_offerer_modify_cb cb;
601
602         cb = ast_sdp_options_get_offerer_modify_cb(sdp_state->options);
603         if (cb) {
604                 void *context;
605                 const struct ast_stream_topology *neg_topology;/*!< Last negotiated topology */
606
607                 context = ast_sdp_options_get_state_context(sdp_state->options);
608                 neg_topology = sdp_state->negotiated_capabilities
609                         ? sdp_state->negotiated_capabilities->topology : NULL;
610                 cb(context, neg_topology, topology);
611
612 #ifdef AST_DEVMODE
613                 {
614                         struct ast_stream *stream;
615                         int idx;
616
617                         /* Check that active streams have at least one format */
618                         for (idx = 0; idx < ast_stream_topology_get_count(topology); ++idx) {
619                                 stream = ast_stream_topology_get_stream(topology, idx);
620                                 ast_assert(ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED
621                                         || (ast_stream_get_formats(stream)
622                                                 && ast_format_cap_count(ast_stream_get_formats(stream))));
623                         }
624                 }
625 #endif
626         }
627 }
628
629 /*!
630  * \internal
631  * \brief Allow a configured callback to configure the merged local topology.
632  * \since 15.0.0
633  *
634  * \details
635  * The callback can configure other parameters associated with each
636  * active stream on the topology.  The callback can call several SDP
637  * API calls to configure the proposed capabilities of the streams
638  * before we create the offer SDP.  For example, the callback could
639  * configure a stream specific connection address, T.38 parameters,
640  * RTP instance, or UDPTL instance parameters.
641  *
642  * \param sdp_state
643  * \param topology Merged topology that we intend to generate the offer SDP.
644  *
645  * \return Nothing
646  */
647 static void sdp_state_cb_offerer_config_topology(const struct ast_sdp_state *sdp_state,
648         const struct ast_stream_topology *topology)
649 {
650         ast_sdp_offerer_config_cb cb;
651
652         cb = ast_sdp_options_get_offerer_config_cb(sdp_state->options);
653         if (cb) {
654                 void *context;
655
656                 context = ast_sdp_options_get_state_context(sdp_state->options);
657                 cb(context, topology);
658         }
659 }
660
661 /*!
662  * \internal
663  * \brief Call any registered pre-apply topology callback.
664  * \since 15.0.0
665  *
666  * \param sdp_state
667  * \param topology
668  *
669  * \return Nothing
670  */
671 static void sdp_state_cb_preapply_topology(const struct ast_sdp_state *sdp_state,
672         const struct ast_stream_topology *topology)
673 {
674         ast_sdp_preapply_cb cb;
675
676         cb = ast_sdp_options_get_preapply_cb(sdp_state->options);
677         if (cb) {
678                 void *context;
679
680                 context = ast_sdp_options_get_state_context(sdp_state->options);
681                 cb(context, topology);
682         }
683 }
684
685 /*!
686  * \internal
687  * \brief Call any registered post-apply topology callback.
688  * \since 15.0.0
689  *
690  * \param sdp_state
691  * \param topology
692  *
693  * \return Nothing
694  */
695 static void sdp_state_cb_postapply_topology(const struct ast_sdp_state *sdp_state,
696         const struct ast_stream_topology *topology)
697 {
698         ast_sdp_postapply_cb cb;
699
700         cb = ast_sdp_options_get_postapply_cb(sdp_state->options);
701         if (cb) {
702                 void *context;
703
704                 context = ast_sdp_options_get_state_context(sdp_state->options);
705                 cb(context, topology);
706         }
707 }
708
709 static const struct sdp_state_capabilities *sdp_state_get_joint_capabilities(
710         const struct ast_sdp_state *sdp_state)
711 {
712         ast_assert(sdp_state != NULL);
713
714         if (sdp_state->negotiated_capabilities) {
715                 return sdp_state->negotiated_capabilities;
716         }
717
718         return sdp_state->proposed_capabilities;
719 }
720
721 static struct sdp_state_stream *sdp_state_get_stream(const struct ast_sdp_state *sdp_state, int stream_index)
722 {
723         if (stream_index >= AST_VECTOR_SIZE(&sdp_state->proposed_capabilities->streams)) {
724                 return NULL;
725         }
726
727         return AST_VECTOR_GET(&sdp_state->proposed_capabilities->streams, stream_index);
728 }
729
730 static struct sdp_state_stream *sdp_state_get_joint_stream(const struct ast_sdp_state *sdp_state, int stream_index)
731 {
732         const struct sdp_state_capabilities *capabilities;
733
734         capabilities = sdp_state_get_joint_capabilities(sdp_state);
735         if (AST_VECTOR_SIZE(&capabilities->streams) <= stream_index) {
736                 return NULL;
737         }
738
739         return AST_VECTOR_GET(&capabilities->streams, stream_index);
740 }
741
742 struct ast_rtp_instance *ast_sdp_state_get_rtp_instance(
743         const struct ast_sdp_state *sdp_state, int stream_index)
744 {
745         struct sdp_state_stream *stream_state;
746
747         ast_assert(sdp_state != NULL);
748         ast_assert(ast_stream_get_type(ast_stream_topology_get_stream(sdp_state->proposed_capabilities->topology,
749                 stream_index)) == AST_MEDIA_TYPE_AUDIO || ast_stream_get_type(ast_stream_topology_get_stream(
750                         sdp_state->proposed_capabilities->topology, stream_index)) == AST_MEDIA_TYPE_VIDEO);
751
752         stream_state = sdp_state_get_stream(sdp_state, stream_index);
753         if (!stream_state || !stream_state->rtp) {
754                 return NULL;
755         }
756
757         return stream_state->rtp->instance;
758 }
759
760 struct ast_udptl *ast_sdp_state_get_udptl_instance(
761         const struct ast_sdp_state *sdp_state, int stream_index)
762 {
763         struct sdp_state_stream *stream_state;
764
765         ast_assert(sdp_state != NULL);
766         ast_assert(ast_stream_get_type(ast_stream_topology_get_stream(sdp_state->proposed_capabilities->topology,
767                 stream_index)) == AST_MEDIA_TYPE_IMAGE);
768
769         stream_state = sdp_state_get_stream(sdp_state, stream_index);
770         if (!stream_state || !stream_state->udptl) {
771                 return NULL;
772         }
773
774         return stream_state->udptl->instance;
775 }
776
777 const struct ast_sockaddr *ast_sdp_state_get_connection_address(const struct ast_sdp_state *sdp_state)
778 {
779         ast_assert(sdp_state != NULL);
780
781         return &sdp_state->connection_address;
782 }
783
784 static int sdp_state_stream_get_connection_address(const struct ast_sdp_state *sdp_state,
785         struct sdp_state_stream *stream_state, struct ast_sockaddr *address)
786 {
787         ast_assert(sdp_state != NULL);
788         ast_assert(stream_state != NULL);
789         ast_assert(address != NULL);
790
791         /* If an explicit connection address has been provided for the stream return it */
792         if (!ast_sockaddr_isnull(&stream_state->connection_address)) {
793                 ast_sockaddr_copy(address, &stream_state->connection_address);
794                 return 0;
795         }
796
797         switch (stream_state->type) {
798         case AST_MEDIA_TYPE_AUDIO:
799         case AST_MEDIA_TYPE_VIDEO:
800                 if (!stream_state->rtp->instance) {
801                         return -1;
802                 }
803                 ast_rtp_instance_get_local_address(stream_state->rtp->instance, address);
804                 break;
805         case AST_MEDIA_TYPE_IMAGE:
806                 if (!stream_state->udptl->instance) {
807                         return -1;
808                 }
809                 ast_udptl_get_us(stream_state->udptl->instance, address);
810                 break;
811         case AST_MEDIA_TYPE_UNKNOWN:
812         case AST_MEDIA_TYPE_TEXT:
813         case AST_MEDIA_TYPE_END:
814                 return -1;
815         }
816
817         if (ast_sockaddr_isnull(address)) {
818                 /* No address is set on the stream state. */
819                 return -1;
820         }
821
822         /* If an explicit global connection address is set use it here for the IP part */
823         if (!ast_sockaddr_isnull(&sdp_state->connection_address)) {
824                 int port = ast_sockaddr_port(address);
825
826                 ast_sockaddr_copy(address, &sdp_state->connection_address);
827                 ast_sockaddr_set_port(address, port);
828         }
829
830         return 0;
831 }
832
833 int ast_sdp_state_get_stream_connection_address(const struct ast_sdp_state *sdp_state,
834         int stream_index, struct ast_sockaddr *address)
835 {
836         struct sdp_state_stream *stream_state;
837
838         ast_assert(sdp_state != NULL);
839         ast_assert(address != NULL);
840
841         stream_state = sdp_state_get_stream(sdp_state, stream_index);
842         if (!stream_state) {
843                 return -1;
844         }
845
846         return sdp_state_stream_get_connection_address(sdp_state, stream_state, address);
847 }
848
849 const struct ast_stream_topology *ast_sdp_state_get_joint_topology(
850         const struct ast_sdp_state *sdp_state)
851 {
852         const struct sdp_state_capabilities *capabilities;
853
854         capabilities = sdp_state_get_joint_capabilities(sdp_state);
855         return capabilities->topology;
856 }
857
858 const struct ast_stream_topology *ast_sdp_state_get_local_topology(
859         const struct ast_sdp_state *sdp_state)
860 {
861         ast_assert(sdp_state != NULL);
862
863         return sdp_state->proposed_capabilities->topology;
864 }
865
866 const struct ast_sdp_options *ast_sdp_state_get_options(
867         const struct ast_sdp_state *sdp_state)
868 {
869         ast_assert(sdp_state != NULL);
870
871         return sdp_state->options;
872 }
873
874 static struct ast_stream *decline_stream(enum ast_media_type type, const char *name)
875 {
876         struct ast_stream *stream;
877
878         if (!name) {
879                 name = ast_codec_media_type2str(type);
880         }
881         stream = ast_stream_alloc(name, type);
882         if (!stream) {
883                 return NULL;
884         }
885         ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
886         return stream;
887 }
888
889 /*!
890  * \brief Merge an update stream into a local stream.
891  *
892  * \param options SDP Options
893  * \param update An updated stream
894  *
895  * \retval NULL An error occurred
896  * \retval non-NULL The joint stream created
897  */
898 static struct ast_stream *merge_local_stream(const struct ast_sdp_options *options,
899         const struct ast_stream *update)
900 {
901         struct ast_stream *joint_stream;
902         struct ast_format_cap *joint_cap;
903         struct ast_format_cap *allowed_cap;
904         struct ast_format_cap *update_cap;
905         enum ast_stream_state joint_state;
906
907         joint_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
908         if (!joint_cap) {
909                 return NULL;
910         }
911
912         update_cap = ast_stream_get_formats(update);
913         allowed_cap = ast_sdp_options_get_format_cap_type(options,
914                 ast_stream_get_type(update));
915         if (allowed_cap && update_cap) {
916                 struct ast_str *allowed_buf = ast_str_alloca(128);
917                 struct ast_str *update_buf = ast_str_alloca(128);
918                 struct ast_str *joint_buf = ast_str_alloca(128);
919
920                 ast_format_cap_get_compatible(allowed_cap, update_cap, joint_cap);
921                 ast_debug(3,
922                         "Filtered update '%s' with allowed '%s' to get joint '%s'. Joint has %zu formats\n",
923                         ast_format_cap_get_names(update_cap, &update_buf),
924                         ast_format_cap_get_names(allowed_cap, &allowed_buf),
925                         ast_format_cap_get_names(joint_cap, &joint_buf),
926                         ast_format_cap_count(joint_cap));
927         }
928
929         /* Determine the joint stream state */
930         joint_state = AST_STREAM_STATE_REMOVED;
931         if (ast_stream_get_state(update) != AST_STREAM_STATE_REMOVED
932                 && ast_format_cap_count(joint_cap)) {
933                 joint_state = AST_STREAM_STATE_SENDRECV;
934         }
935
936         joint_stream = ast_stream_alloc(ast_stream_get_name(update),
937                 ast_stream_get_type(update));
938         if (joint_stream) {
939                 ast_stream_set_state(joint_stream, joint_state);
940                 if (joint_state != AST_STREAM_STATE_REMOVED) {
941                         ast_stream_set_formats(joint_stream, joint_cap);
942                 }
943         }
944
945         ao2_ref(joint_cap, -1);
946
947         return joint_stream;
948 }
949
950 /*!
951  * \brief Merge a remote stream into a local stream.
952  *
953  * \param sdp_state
954  * \param local Our local stream (NULL if creating new stream)
955  * \param locally_held Nonzero if the local stream is held
956  * \param remote A remote stream
957  *
958  * \retval NULL An error occurred
959  * \retval non-NULL The joint stream created
960  */
961 static struct ast_stream *merge_remote_stream(const struct ast_sdp_state *sdp_state,
962         const struct ast_stream *local, unsigned int locally_held,
963         const struct ast_stream *remote)
964 {
965         struct ast_stream *joint_stream;
966         struct ast_format_cap *joint_cap;
967         struct ast_format_cap *local_cap;
968         struct ast_format_cap *remote_cap;
969         const char *joint_name;
970         enum ast_stream_state joint_state;
971         enum ast_stream_state remote_state;
972
973         joint_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
974         if (!joint_cap) {
975                 return NULL;
976         }
977
978         remote_cap = ast_stream_get_formats(remote);
979         if (local) {
980                 local_cap = ast_stream_get_formats(local);
981         } else {
982                 local_cap = ast_sdp_options_get_format_cap_type(sdp_state->options,
983                         ast_stream_get_type(remote));
984         }
985         if (local_cap && remote_cap) {
986                 struct ast_str *local_buf = ast_str_alloca(128);
987                 struct ast_str *remote_buf = ast_str_alloca(128);
988                 struct ast_str *joint_buf = ast_str_alloca(128);
989
990                 ast_format_cap_get_compatible(local_cap, remote_cap, joint_cap);
991                 ast_debug(3,
992                         "Combined local '%s' with remote '%s' to get joint '%s'. Joint has %zu formats\n",
993                         ast_format_cap_get_names(local_cap, &local_buf),
994                         ast_format_cap_get_names(remote_cap, &remote_buf),
995                         ast_format_cap_get_names(joint_cap, &joint_buf),
996                         ast_format_cap_count(joint_cap));
997         }
998
999         /* Determine the joint stream state */
1000         remote_state = ast_stream_get_state(remote);
1001         joint_state = AST_STREAM_STATE_REMOVED;
1002         if ((!local || ast_stream_get_state(local) != AST_STREAM_STATE_REMOVED)
1003                 && ast_format_cap_count(joint_cap)) {
1004                 if (sdp_state->locally_held || locally_held) {
1005                         switch (remote_state) {
1006                         case AST_STREAM_STATE_REMOVED:
1007                                 break;
1008                         case AST_STREAM_STATE_INACTIVE:
1009                                 joint_state = AST_STREAM_STATE_INACTIVE;
1010                                 break;
1011                         case AST_STREAM_STATE_SENDRECV:
1012                                 joint_state = AST_STREAM_STATE_SENDONLY;
1013                                 break;
1014                         case AST_STREAM_STATE_SENDONLY:
1015                                 joint_state = AST_STREAM_STATE_INACTIVE;
1016                                 break;
1017                         case AST_STREAM_STATE_RECVONLY:
1018                                 joint_state = AST_STREAM_STATE_SENDONLY;
1019                                 break;
1020                         }
1021                 } else {
1022                         switch (remote_state) {
1023                         case AST_STREAM_STATE_REMOVED:
1024                                 break;
1025                         case AST_STREAM_STATE_INACTIVE:
1026                                 joint_state = AST_STREAM_STATE_RECVONLY;
1027                                 break;
1028                         case AST_STREAM_STATE_SENDRECV:
1029                                 joint_state = AST_STREAM_STATE_SENDRECV;
1030                                 break;
1031                         case AST_STREAM_STATE_SENDONLY:
1032                                 joint_state = AST_STREAM_STATE_RECVONLY;
1033                                 break;
1034                         case AST_STREAM_STATE_RECVONLY:
1035                                 joint_state = AST_STREAM_STATE_SENDRECV;
1036                                 break;
1037                         }
1038                 }
1039         }
1040
1041         if (local) {
1042                 joint_name = ast_stream_get_name(local);
1043         } else {
1044                 joint_name = ast_codec_media_type2str(ast_stream_get_type(remote));
1045         }
1046         joint_stream = ast_stream_alloc(joint_name, ast_stream_get_type(remote));
1047         if (joint_stream) {
1048                 ast_stream_set_state(joint_stream, joint_state);
1049                 if (joint_state != AST_STREAM_STATE_REMOVED) {
1050                         ast_stream_set_formats(joint_stream, joint_cap);
1051                 }
1052         }
1053
1054         ao2_ref(joint_cap, -1);
1055
1056         return joint_stream;
1057 }
1058
1059 /*!
1060  * \internal
1061  * \brief Determine if a merged topology should be rejected.
1062  * \since 15.0.0
1063  *
1064  * \param topology What topology to determine if we reject
1065  *
1066  * \retval 0 if not rejected.
1067  * \retval non-zero if rejected.
1068  */
1069 static int sdp_topology_is_rejected(struct ast_stream_topology *topology)
1070 {
1071         int idx;
1072         struct ast_stream *stream;
1073
1074         for (idx = ast_stream_topology_get_count(topology); idx--;) {
1075                 stream = ast_stream_topology_get_stream(topology, idx);
1076                 if (ast_stream_get_state(stream) != AST_STREAM_STATE_REMOVED) {
1077                         /* At least one stream is not declined */
1078                         return 0;
1079                 }
1080         }
1081
1082         /* All streams are declined */
1083         return 1;
1084 }
1085
1086 static void sdp_state_stream_copy_common(struct sdp_state_stream *dst, const struct sdp_state_stream *src)
1087 {
1088         ast_sockaddr_copy(&dst->connection_address,
1089                 &src->connection_address);
1090         /* Explicitly does not copy the local or remote hold states. */
1091         dst->t38_local_params = src->t38_local_params;
1092 }
1093
1094 static void sdp_state_stream_copy(struct sdp_state_stream *dst, const struct sdp_state_stream *src)
1095 {
1096         *dst = *src;
1097
1098         switch (dst->type) {
1099         case AST_MEDIA_TYPE_AUDIO:
1100         case AST_MEDIA_TYPE_VIDEO:
1101                 ao2_bump(dst->rtp);
1102                 break;
1103         case AST_MEDIA_TYPE_IMAGE:
1104                 ao2_bump(dst->udptl);
1105                 break;
1106         case AST_MEDIA_TYPE_UNKNOWN:
1107         case AST_MEDIA_TYPE_TEXT:
1108         case AST_MEDIA_TYPE_END:
1109                 break;
1110         }
1111 }
1112
1113 /*!
1114  * \internal
1115  * \brief Initialize an int vector and default the contents to the member index.
1116  * \since 15.0.0
1117  *
1118  * \param vect Vetctor to initialize and set to default values.
1119  * \param size Size of the vector to setup.
1120  *
1121  * \retval 0 on success.
1122  * \retval -1 on failure.
1123  */
1124 static int sdp_vect_idx_init(struct ast_vector_int *vect, size_t size)
1125 {
1126         int idx;
1127
1128         if (AST_VECTOR_INIT(vect, size)) {
1129                 return -1;
1130         }
1131         for (idx = 0; idx < size; ++idx) {
1132                 AST_VECTOR_APPEND(vect, idx);
1133         }
1134         return 0;
1135 }
1136
1137 /*!
1138  * \internal
1139  * \brief Compare stream types for sort order.
1140  * \since 15.0.0
1141  *
1142  * \param left Stream parameter on left
1143  * \param right Stream parameter on right
1144  *
1145  * \retval <0 left stream sorts first.
1146  * \retval =0 streams match.
1147  * \retval >0 right stream sorts first.
1148  */
1149 static int sdp_stream_cmp_by_type(const struct ast_stream *left, const struct ast_stream *right)
1150 {
1151         enum ast_media_type left_type = ast_stream_get_type(left);
1152         enum ast_media_type right_type = ast_stream_get_type(right);
1153
1154         /* Treat audio and image as the same for T.38 support */
1155         if (left_type == AST_MEDIA_TYPE_IMAGE) {
1156                 left_type = AST_MEDIA_TYPE_AUDIO;
1157         }
1158         if (right_type == AST_MEDIA_TYPE_IMAGE) {
1159                 right_type = AST_MEDIA_TYPE_AUDIO;
1160         }
1161
1162         return left_type - right_type;
1163 }
1164
1165 /*!
1166  * \internal
1167  * \brief Compare stream names and types for sort order.
1168  * \since 15.0.0
1169  *
1170  * \param left Stream parameter on left
1171  * \param right Stream parameter on right
1172  *
1173  * \retval <0 left stream sorts first.
1174  * \retval =0 streams match.
1175  * \retval >0 right stream sorts first.
1176  */
1177 static int sdp_stream_cmp_by_name(const struct ast_stream *left, const struct ast_stream *right)
1178 {
1179         int cmp;
1180         const char *left_name;
1181
1182         left_name = ast_stream_get_name(left);
1183         cmp = strcmp(left_name, ast_stream_get_name(right));
1184         if (!cmp) {
1185                 cmp = sdp_stream_cmp_by_type(left, right);
1186                 if (!cmp) {
1187                         /* Are the stream names real or type names which aren't matchable? */
1188                         if (ast_strlen_zero(left_name)
1189                                 || !strcmp(left_name, ast_codec_media_type2str(ast_stream_get_type(left)))
1190                                 || !strcmp(left_name, ast_codec_media_type2str(ast_stream_get_type(right)))) {
1191                                 /* The streams don't actually have real names */
1192                                 cmp = -1;
1193                         }
1194                 }
1195         }
1196         return cmp;
1197 }
1198
1199 /*!
1200  * \internal
1201  * \brief Merge topology streams by the match function.
1202  * \since 15.0.0
1203  *
1204  * \param sdp_state
1205  * \param current_topology Topology to update with state.
1206  * \param update_topology Topology to merge into the current topology.
1207  * \param current_vect Stream index vector of remaining current_topology streams.
1208  * \param update_vect Stream index vector of remaining update_topology streams.
1209  * \param backfill_candidate Array of flags marking current_topology streams
1210  *            that can be reused for a different stream.
1211  * \param match Stream comparison function to identify corresponding streams
1212  *            between the current_topology and update_topology.
1213  * \param merged_topology Output topology of merged streams.
1214  * \param compact_streams TRUE if backfill and limit number of streams.
1215  *
1216  * \retval 0 on success.
1217  * \retval -1 on failure.
1218  */
1219 static int sdp_merge_streams_match(
1220         const struct ast_sdp_state *sdp_state,
1221         const struct ast_stream_topology *current_topology,
1222         const struct ast_stream_topology *update_topology,
1223         struct ast_vector_int *current_vect,
1224         struct ast_vector_int *update_vect,
1225         char backfill_candidate[],
1226         int (*match)(const struct ast_stream *left, const struct ast_stream *right),
1227         struct ast_stream_topology *merged_topology,
1228         int compact_streams)
1229 {
1230         struct ast_stream *current_stream;
1231         struct ast_stream *update_stream;
1232         int current_idx;
1233         int update_idx;
1234         int idx;
1235
1236         for (current_idx = 0; current_idx < AST_VECTOR_SIZE(current_vect);) {
1237                 idx = AST_VECTOR_GET(current_vect, current_idx);
1238                 current_stream = ast_stream_topology_get_stream(current_topology, idx);
1239
1240                 for (update_idx = 0; update_idx < AST_VECTOR_SIZE(update_vect); ++update_idx) {
1241                         idx = AST_VECTOR_GET(update_vect, update_idx);
1242                         update_stream = ast_stream_topology_get_stream(update_topology, idx);
1243
1244                         if (match(current_stream, update_stream)) {
1245                                 continue;
1246                         }
1247
1248                         if (!compact_streams
1249                                 || ast_stream_get_state(current_stream) != AST_STREAM_STATE_REMOVED
1250                                 || ast_stream_get_state(update_stream) != AST_STREAM_STATE_REMOVED) {
1251                                 struct ast_stream *merged_stream;
1252
1253                                 merged_stream = merge_local_stream(sdp_state->options, update_stream);
1254                                 if (!merged_stream) {
1255                                         return -1;
1256                                 }
1257                                 idx = AST_VECTOR_GET(current_vect, current_idx);
1258                                 if (ast_stream_topology_set_stream(merged_topology, idx, merged_stream)) {
1259                                         ast_stream_free(merged_stream);
1260                                         return -1;
1261                                 }
1262
1263                                 /*
1264                                  * The current_stream cannot be considered a backfill_candidate
1265                                  * anymore since it got updated.
1266                                  *
1267                                  * XXX It could be argued that if the declined status didn't
1268                                  * change because the merged_stream became declined then we
1269                                  * shouldn't remove the stream slot as a backfill_candidate
1270                                  * and we shouldn't update the merged_topology stream.  If we
1271                                  * then backfilled the stream we would likely mess up the core
1272                                  * if it is matching streams by type since the core attempted
1273                                  * to update the stream with an incompatible stream.  Any
1274                                  * backfilled streams could cause a stream type ordering
1275                                  * problem.  However, we do need to reclaim declined stream
1276                                  * slots sometime.
1277                                  */
1278                                 backfill_candidate[idx] = 0;
1279                         }
1280
1281                         AST_VECTOR_REMOVE_ORDERED(current_vect, current_idx);
1282                         AST_VECTOR_REMOVE_ORDERED(update_vect, update_idx);
1283                         goto matched_next;
1284                 }
1285
1286                 ++current_idx;
1287 matched_next:;
1288         }
1289         return 0;
1290 }
1291
1292 /*!
1293  * \internal
1294  * \brief Merge the current local topology with an updated topology.
1295  * \since 15.0.0
1296  *
1297  * \param sdp_state
1298  * \param current_topology Topology to update with state.
1299  * \param update_topology Topology to merge into the current topology.
1300  * \param compact_streams TRUE if backfill and limit number of streams.
1301  *
1302  * \retval merged topology on success.
1303  * \retval NULL on failure.
1304  */
1305 static struct ast_stream_topology *merge_local_topologies(
1306         const struct ast_sdp_state *sdp_state,
1307         const struct ast_stream_topology *current_topology,
1308         const struct ast_stream_topology *update_topology,
1309         int compact_streams)
1310 {
1311         struct ast_stream_topology *merged_topology;
1312         struct ast_stream *current_stream;
1313         struct ast_stream *update_stream;
1314         struct ast_stream *merged_stream;
1315         struct ast_vector_int current_vect;
1316         struct ast_vector_int update_vect;
1317         int current_idx = ast_stream_topology_get_count(current_topology);
1318         int update_idx;
1319         int idx;
1320         char backfill_candidate[current_idx];
1321
1322         memset(backfill_candidate, 0, current_idx);
1323
1324         if (compact_streams) {
1325                 /* Limit matching consideration to the maximum allowed live streams. */
1326                 idx = ast_sdp_options_get_max_streams(sdp_state->options);
1327                 if (idx < current_idx) {
1328                         current_idx = idx;
1329                 }
1330         }
1331         if (sdp_vect_idx_init(&current_vect, current_idx)) {
1332                 return NULL;
1333         }
1334
1335         if (sdp_vect_idx_init(&update_vect, ast_stream_topology_get_count(update_topology))) {
1336                 AST_VECTOR_FREE(&current_vect);
1337                 return NULL;
1338         }
1339
1340         merged_topology = ast_stream_topology_clone(current_topology);
1341         if (!merged_topology) {
1342                 goto fail;
1343         }
1344
1345         /*
1346          * Remove any unsupported current streams from match consideration
1347          * and mark potential backfill candidates.
1348          */
1349         for (current_idx = AST_VECTOR_SIZE(&current_vect); current_idx--;) {
1350                 idx = AST_VECTOR_GET(&current_vect, current_idx);
1351                 current_stream = ast_stream_topology_get_stream(current_topology, idx);
1352                 if (ast_stream_get_state(current_stream) == AST_STREAM_STATE_REMOVED
1353                         && compact_streams) {
1354                         /* The declined stream is a potential backfill candidate */
1355                         backfill_candidate[idx] = 1;
1356                 }
1357                 if (sdp_is_stream_type_supported(ast_stream_get_type(current_stream))) {
1358                         continue;
1359                 }
1360                 /* Unsupported current streams should always be declined */
1361                 ast_assert(ast_stream_get_state(current_stream) == AST_STREAM_STATE_REMOVED);
1362
1363                 AST_VECTOR_REMOVE_ORDERED(&current_vect, current_idx);
1364         }
1365
1366         /* Remove any unsupported update streams from match consideration. */
1367         for (update_idx = AST_VECTOR_SIZE(&update_vect); update_idx--;) {
1368                 idx = AST_VECTOR_GET(&update_vect, update_idx);
1369                 update_stream = ast_stream_topology_get_stream(update_topology, idx);
1370                 if (sdp_is_stream_type_supported(ast_stream_get_type(update_stream))) {
1371                         continue;
1372                 }
1373
1374                 AST_VECTOR_REMOVE_ORDERED(&update_vect, update_idx);
1375         }
1376
1377         /* Match by stream name and type */
1378         if (sdp_merge_streams_match(sdp_state, current_topology, update_topology,
1379                 &current_vect, &update_vect, backfill_candidate, sdp_stream_cmp_by_name,
1380                 merged_topology, compact_streams)) {
1381                 goto fail;
1382         }
1383
1384         /* Match by stream type */
1385         if (sdp_merge_streams_match(sdp_state, current_topology, update_topology,
1386                 &current_vect, &update_vect, backfill_candidate, sdp_stream_cmp_by_type,
1387                 merged_topology, compact_streams)) {
1388                 goto fail;
1389         }
1390
1391         /* Decline unmatched current stream slots */
1392         for (current_idx = AST_VECTOR_SIZE(&current_vect); current_idx--;) {
1393                 idx = AST_VECTOR_GET(&current_vect, current_idx);
1394                 current_stream = ast_stream_topology_get_stream(current_topology, idx);
1395
1396                 if (ast_stream_get_state(current_stream) == AST_STREAM_STATE_REMOVED) {
1397                         /* Stream is already declined. */
1398                         continue;
1399                 }
1400
1401                 merged_stream = decline_stream(ast_stream_get_type(current_stream),
1402                         ast_stream_get_name(current_stream));
1403                 if (!merged_stream) {
1404                         goto fail;
1405                 }
1406                 if (ast_stream_topology_set_stream(merged_topology, idx, merged_stream)) {
1407                         ast_stream_free(merged_stream);
1408                         goto fail;
1409                 }
1410         }
1411
1412         /* Backfill new update stream slots into pre-existing declined current stream slots */
1413         while (AST_VECTOR_SIZE(&update_vect)) {
1414                 idx = ast_stream_topology_get_count(current_topology);
1415                 for (current_idx = 0; current_idx < idx; ++current_idx) {
1416                         if (backfill_candidate[current_idx]) {
1417                                 break;
1418                         }
1419                 }
1420                 if (idx <= current_idx) {
1421                         /* No more backfill candidates remain. */
1422                         break;
1423                 }
1424                 /* There should only be backfill stream slots when we are compact_streams */
1425                 ast_assert(compact_streams);
1426
1427                 idx = AST_VECTOR_GET(&update_vect, 0);
1428                 update_stream = ast_stream_topology_get_stream(update_topology, idx);
1429                 AST_VECTOR_REMOVE_ORDERED(&update_vect, 0);
1430
1431                 if (ast_stream_get_state(update_stream) == AST_STREAM_STATE_REMOVED) {
1432                         /* New stream is already declined so don't bother adding it. */
1433                         continue;
1434                 }
1435
1436                 merged_stream = merge_local_stream(sdp_state->options, update_stream);
1437                 if (!merged_stream) {
1438                         goto fail;
1439                 }
1440                 if (ast_stream_get_state(merged_stream) == AST_STREAM_STATE_REMOVED) {
1441                         /* New stream not compatible so don't bother adding it. */
1442                         ast_stream_free(merged_stream);
1443                         continue;
1444                 }
1445
1446                 /* Add the new stream into the backfill stream slot. */
1447                 if (ast_stream_topology_set_stream(merged_topology, current_idx, merged_stream)) {
1448                         ast_stream_free(merged_stream);
1449                         goto fail;
1450                 }
1451                 backfill_candidate[current_idx] = 0;
1452         }
1453
1454         /* Append any remaining new update stream slots that can fit. */
1455         while (AST_VECTOR_SIZE(&update_vect)
1456                 && (!compact_streams
1457                         || ast_stream_topology_get_count(merged_topology)
1458                                 < ast_sdp_options_get_max_streams(sdp_state->options))) {
1459                 idx = AST_VECTOR_GET(&update_vect, 0);
1460                 update_stream = ast_stream_topology_get_stream(update_topology, idx);
1461                 AST_VECTOR_REMOVE_ORDERED(&update_vect, 0);
1462
1463                 if (ast_stream_get_state(update_stream) == AST_STREAM_STATE_REMOVED) {
1464                         /* New stream is already declined so don't bother adding it. */
1465                         continue;
1466                 }
1467
1468                 merged_stream = merge_local_stream(sdp_state->options, update_stream);
1469                 if (!merged_stream) {
1470                         goto fail;
1471                 }
1472                 if (ast_stream_get_state(merged_stream) == AST_STREAM_STATE_REMOVED) {
1473                         /* New stream not compatible so don't bother adding it. */
1474                         ast_stream_free(merged_stream);
1475                         continue;
1476                 }
1477
1478                 /* Append the new update stream. */
1479                 if (ast_stream_topology_append_stream(merged_topology, merged_stream) < 0) {
1480                         ast_stream_free(merged_stream);
1481                         goto fail;
1482                 }
1483         }
1484
1485         AST_VECTOR_FREE(&current_vect);
1486         AST_VECTOR_FREE(&update_vect);
1487         return merged_topology;
1488
1489 fail:
1490         ast_stream_topology_free(merged_topology);
1491         AST_VECTOR_FREE(&current_vect);
1492         AST_VECTOR_FREE(&update_vect);
1493         return NULL;
1494 }
1495
1496 /*!
1497  * \internal
1498  * \brief Remove declined streams appended beyond orig_topology.
1499  * \since 15.0.0
1500  *
1501  * \param sdp_state
1502  * \param orig_topology Negotiated or initial topology.
1503  * \param new_topology New proposed topology.
1504  *
1505  * \return Nothing
1506  */
1507 static void remove_appended_declined_streams(const struct ast_sdp_state *sdp_state,
1508         const struct ast_stream_topology *orig_topology,
1509         struct ast_stream_topology *new_topology)
1510 {
1511         struct ast_stream *stream;
1512         int orig_count;
1513         int idx;
1514
1515         orig_count = ast_stream_topology_get_count(orig_topology);
1516         for (idx = ast_stream_topology_get_count(new_topology); orig_count < idx;) {
1517                 --idx;
1518                 stream = ast_stream_topology_get_stream(new_topology, idx);
1519                 if (ast_stream_get_state(stream) != AST_STREAM_STATE_REMOVED) {
1520                         continue;
1521                 }
1522                 ast_stream_topology_del_stream(new_topology, idx);
1523         }
1524 }
1525
1526 /*!
1527  * \internal
1528  * \brief Setup a new state stream from a possibly existing state stream.
1529  * \since 15.0.0
1530  *
1531  * \param sdp_state
1532  * \param new_state_stream What state stream to setup
1533  * \param old_state_stream Source of previous state stream information.
1534  *            May be NULL.
1535  * \param new_type Type of the new state stream.
1536  *
1537  * \retval 0 on success.
1538  * \retval -1 on failure.
1539  */
1540 static int setup_new_stream_capabilities(
1541         const struct ast_sdp_state *sdp_state,
1542         struct sdp_state_stream *new_state_stream,
1543         struct sdp_state_stream *old_state_stream,
1544         enum ast_media_type new_type)
1545 {
1546         if (old_state_stream) {
1547                 /*
1548                  * Copy everything potentially useful for a new stream state type
1549                  * from the old stream of a possible different type.
1550                  */
1551                 sdp_state_stream_copy_common(new_state_stream, old_state_stream);
1552                 /* We also need to preserve the locally_held state for the new stream. */
1553                 new_state_stream->locally_held = old_state_stream->locally_held;
1554         }
1555         new_state_stream->type = new_type;
1556
1557         switch (new_type) {
1558         case AST_MEDIA_TYPE_AUDIO:
1559         case AST_MEDIA_TYPE_VIDEO:
1560                 new_state_stream->rtp = create_rtp(sdp_state->options, new_type);
1561                 if (!new_state_stream->rtp) {
1562                         return -1;
1563                 }
1564                 break;
1565         case AST_MEDIA_TYPE_IMAGE:
1566                 new_state_stream->udptl = create_udptl(sdp_state->options);
1567                 if (!new_state_stream->udptl) {
1568                         return -1;
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         return 0;
1577 }
1578
1579 /*!
1580  * \brief Merge existing stream capabilities and a new topology.
1581  *
1582  * \param sdp_state The state needing capabilities merged
1583  * \param new_topology The topology to merge with our proposed capabilities
1584  *
1585  * \details
1586  *
1587  * This is a bit complicated. The idea is that we already have some
1588  * capabilities set, and we've now been confronted with a new stream
1589  * topology from the system.  We want to take what we had before and
1590  * merge them with the new topology from the system.
1591  *
1592  * According to the RFC, stream slots can change their types only if
1593  * they are carrying the same logical information or an offer is
1594  * reusing a declined slot or new stream slots are added to the end
1595  * of the list.  Switching a stream from audio to T.38 makes sense
1596  * because the stream slot is carrying the same information just in a
1597  * different format.
1598  *
1599  * We can setup new streams offered by the system up to our
1600  * configured maximum stream slots.  New stream slots requested over
1601  * the maximum are discarded.
1602  *
1603  * \retval NULL An error occurred
1604  * \retval non-NULL The merged capabilities
1605  */
1606 static struct sdp_state_capabilities *merge_local_capabilities(
1607         const struct ast_sdp_state *sdp_state,
1608         const struct ast_stream_topology *new_topology)
1609 {
1610         const struct sdp_state_capabilities *current = sdp_state->proposed_capabilities;
1611         struct sdp_state_capabilities *merged_capabilities;
1612         int idx;
1613
1614         ast_assert(current != NULL);
1615
1616         merged_capabilities = ast_calloc(1, sizeof(*merged_capabilities));
1617         if (!merged_capabilities) {
1618                 return NULL;
1619         }
1620
1621         merged_capabilities->topology = merge_local_topologies(sdp_state, current->topology,
1622                 new_topology, 1);
1623         if (!merged_capabilities->topology) {
1624                 goto fail;
1625         }
1626         sdp_state_cb_offerer_modify_topology(sdp_state, merged_capabilities->topology);
1627         remove_appended_declined_streams(sdp_state, current->topology,
1628                 merged_capabilities->topology);
1629
1630         if (AST_VECTOR_INIT(&merged_capabilities->streams,
1631                 ast_stream_topology_get_count(merged_capabilities->topology))) {
1632                 goto fail;
1633         }
1634
1635         for (idx = 0; idx < ast_stream_topology_get_count(merged_capabilities->topology); ++idx) {
1636                 struct sdp_state_stream *merged_state_stream;
1637                 struct sdp_state_stream *current_state_stream;
1638                 struct ast_stream *merged_stream;
1639                 struct ast_stream *current_stream;
1640                 enum ast_media_type merged_stream_type;
1641                 enum ast_media_type current_stream_type;
1642
1643                 merged_state_stream = ast_calloc(1, sizeof(*merged_state_stream));
1644                 if (!merged_state_stream) {
1645                         goto fail;
1646                 }
1647
1648                 merged_stream = ast_stream_topology_get_stream(merged_capabilities->topology, idx);
1649                 merged_stream_type = ast_stream_get_type(merged_stream);
1650
1651                 if (idx < ast_stream_topology_get_count(current->topology)) {
1652                         current_state_stream = AST_VECTOR_GET(&current->streams, idx);
1653                         current_stream = ast_stream_topology_get_stream(current->topology, idx);
1654                         current_stream_type = ast_stream_get_type(current_stream);
1655                 } else {
1656                         /* The merged topology is adding a stream */
1657                         current_state_stream = NULL;
1658                         current_stream = NULL;
1659                         current_stream_type = AST_MEDIA_TYPE_UNKNOWN;
1660                 }
1661
1662                 if (ast_stream_get_state(merged_stream) == AST_STREAM_STATE_REMOVED) {
1663                         if (current_state_stream) {
1664                                 /* Copy everything potentially useful to a declined stream state. */
1665                                 sdp_state_stream_copy_common(merged_state_stream, current_state_stream);
1666                         }
1667                         merged_state_stream->type = merged_stream_type;
1668                 } else if (!current_stream
1669                         || ast_stream_get_state(current_stream) == AST_STREAM_STATE_REMOVED) {
1670                         /* This is a new stream */
1671                         if (setup_new_stream_capabilities(sdp_state, merged_state_stream,
1672                                 current_state_stream, merged_stream_type)) {
1673                                 sdp_state_stream_free(merged_state_stream);
1674                                 goto fail;
1675                         }
1676                 } else if (merged_stream_type == current_stream_type) {
1677                         /* Stream type is not changing. */
1678                         sdp_state_stream_copy(merged_state_stream, current_state_stream);
1679                 } else {
1680                         /*
1681                          * Stream type is changing.  Need to replace the stream.
1682                          *
1683                          * Unsupported streams should already be handled earlier because
1684                          * they are always declined.
1685                          */
1686                         ast_assert(sdp_is_stream_type_supported(merged_stream_type));
1687
1688                         /*
1689                          * XXX We might need to keep the old RTP instance if the new
1690                          * stream type is also RTP.  We would just be changing between
1691                          * audio and video in that case.  However we will create a new
1692                          * RTP instance anyway since its purpose has to be changing.
1693                          * Any RTP packets in flight from the old stream type might
1694                          * cause mischief.
1695                          */
1696                         if (setup_new_stream_capabilities(sdp_state, merged_state_stream,
1697                                 current_state_stream, merged_stream_type)) {
1698                                 sdp_state_stream_free(merged_state_stream);
1699                                 goto fail;
1700                         }
1701                 }
1702
1703                 if (AST_VECTOR_APPEND(&merged_capabilities->streams, merged_state_stream)) {
1704                         sdp_state_stream_free(merged_state_stream);
1705                         goto fail;
1706                 }
1707         }
1708
1709         return merged_capabilities;
1710
1711 fail:
1712         sdp_state_capabilities_free(merged_capabilities);
1713         return NULL;
1714 }
1715
1716 static void merge_remote_stream_capabilities(
1717         const struct ast_sdp_state *sdp_state,
1718         struct sdp_state_stream *joint_state_stream,
1719         struct sdp_state_stream *local_state_stream,
1720         struct ast_stream *remote_stream)
1721 {
1722         struct ast_rtp_codecs *codecs;
1723
1724         *joint_state_stream = *local_state_stream;
1725         /*
1726          * Need to explicitly set the type to the remote because we could
1727          * be changing the type between audio and video.
1728          */
1729         joint_state_stream->type = ast_stream_get_type(remote_stream);
1730
1731         switch (joint_state_stream->type) {
1732         case AST_MEDIA_TYPE_AUDIO:
1733         case AST_MEDIA_TYPE_VIDEO:
1734                 ao2_bump(joint_state_stream->rtp);
1735                 codecs = ast_stream_get_data(remote_stream, AST_STREAM_DATA_RTP_CODECS);
1736                 ast_assert(codecs != NULL);
1737                 if (sdp_state->role == SDP_ROLE_ANSWERER) {
1738                         /*
1739                          * Setup rx payload type mapping to prefer the mapping
1740                          * from the peer that the RFC says we SHOULD use.
1741                          */
1742                         ast_rtp_codecs_payloads_xover(codecs, codecs, NULL);
1743                 }
1744                 ast_rtp_codecs_payloads_copy(codecs,
1745                         ast_rtp_instance_get_codecs(joint_state_stream->rtp->instance),
1746                         joint_state_stream->rtp->instance);
1747                 break;
1748         case AST_MEDIA_TYPE_IMAGE:
1749                 joint_state_stream->udptl = ao2_bump(joint_state_stream->udptl);
1750                 break;
1751         case AST_MEDIA_TYPE_UNKNOWN:
1752         case AST_MEDIA_TYPE_TEXT:
1753         case AST_MEDIA_TYPE_END:
1754                 break;
1755         }
1756 }
1757
1758 static int create_remote_stream_capabilities(
1759         const struct ast_sdp_state *sdp_state,
1760         struct sdp_state_stream *joint_state_stream,
1761         struct sdp_state_stream *local_state_stream,
1762         struct ast_stream *remote_stream)
1763 {
1764         struct ast_rtp_codecs *codecs;
1765
1766         /* We can only create streams if we are the answerer */
1767         ast_assert(sdp_state->role == SDP_ROLE_ANSWERER);
1768
1769         if (local_state_stream) {
1770                 /*
1771                  * Copy everything potentially useful for a new stream state type
1772                  * from the old stream of a possible different type.
1773                  */
1774                 sdp_state_stream_copy_common(joint_state_stream, local_state_stream);
1775                 /* We also need to preserve the locally_held state for the new stream. */
1776                 joint_state_stream->locally_held = local_state_stream->locally_held;
1777         }
1778         joint_state_stream->type = ast_stream_get_type(remote_stream);
1779
1780         switch (joint_state_stream->type) {
1781         case AST_MEDIA_TYPE_AUDIO:
1782         case AST_MEDIA_TYPE_VIDEO:
1783                 joint_state_stream->rtp = create_rtp(sdp_state->options, joint_state_stream->type);
1784                 if (!joint_state_stream->rtp) {
1785                         return -1;
1786                 }
1787
1788                 /*
1789                  * Setup rx payload type mapping to prefer the mapping
1790                  * from the peer that the RFC says we SHOULD use.
1791                  */
1792                 codecs = ast_stream_get_data(remote_stream, AST_STREAM_DATA_RTP_CODECS);
1793                 ast_assert(codecs != NULL);
1794                 ast_rtp_codecs_payloads_xover(codecs, codecs, NULL);
1795                 ast_rtp_codecs_payloads_copy(codecs,
1796                         ast_rtp_instance_get_codecs(joint_state_stream->rtp->instance),
1797                         joint_state_stream->rtp->instance);
1798                 break;
1799         case AST_MEDIA_TYPE_IMAGE:
1800                 joint_state_stream->udptl = create_udptl(sdp_state->options);
1801                 if (!joint_state_stream->udptl) {
1802                         return -1;
1803                 }
1804                 break;
1805         case AST_MEDIA_TYPE_UNKNOWN:
1806         case AST_MEDIA_TYPE_TEXT:
1807         case AST_MEDIA_TYPE_END:
1808                 break;
1809         }
1810         return 0;
1811 }
1812
1813 /*!
1814  * \internal
1815  * \brief Create a joint topology from the remote topology.
1816  * \since 15.0.0
1817  *
1818  * \param sdp_state The state needing capabilities merged.
1819  * \param local Capabilities to merge the remote topology into.
1820  * \param remote_topology The topology to merge with our local capabilities.
1821  *
1822  * \retval joint topology on success.
1823  * \retval NULL on failure.
1824  */
1825 static struct ast_stream_topology *merge_remote_topology(
1826         const struct ast_sdp_state *sdp_state,
1827         const struct sdp_state_capabilities *local,
1828         const struct ast_stream_topology *remote_topology)
1829 {
1830         struct ast_stream_topology *joint_topology;
1831         int idx;
1832
1833         joint_topology = ast_stream_topology_alloc();
1834         if (!joint_topology) {
1835                 return NULL;
1836         }
1837
1838         for (idx = 0; idx < ast_stream_topology_get_count(remote_topology); ++idx) {
1839                 enum ast_media_type local_stream_type;
1840                 enum ast_media_type remote_stream_type;
1841                 struct ast_stream *remote_stream;
1842                 struct ast_stream *local_stream;
1843                 struct ast_stream *joint_stream;
1844                 struct sdp_state_stream *local_state_stream;
1845
1846                 remote_stream = ast_stream_topology_get_stream(remote_topology, idx);
1847                 remote_stream_type = ast_stream_get_type(remote_stream);
1848
1849                 if (idx < ast_stream_topology_get_count(local->topology)) {
1850                         local_state_stream = AST_VECTOR_GET(&local->streams, idx);
1851                         local_stream = ast_stream_topology_get_stream(local->topology, idx);
1852                         local_stream_type = ast_stream_get_type(local_stream);
1853                 } else {
1854                         /* The remote is adding a stream slot */
1855                         local_state_stream = NULL;
1856                         local_stream = NULL;
1857                         local_stream_type = AST_MEDIA_TYPE_UNKNOWN;
1858
1859                         if (sdp_state->role != SDP_ROLE_ANSWERER) {
1860                                 /* Remote cannot add a new stream slot in an answer SDP */
1861                                 ast_debug(1,
1862                                         "Bad.  Ignoring new %s stream slot remote answer SDP trying to add.\n",
1863                                         ast_codec_media_type2str(remote_stream_type));
1864                                 continue;
1865                         }
1866                 }
1867
1868                 if (local_stream
1869                         && ast_stream_get_state(local_stream) != AST_STREAM_STATE_REMOVED) {
1870                         if (remote_stream_type == local_stream_type) {
1871                                 /* Stream type is not changing. */
1872                                 joint_stream = merge_remote_stream(sdp_state, local_stream,
1873                                         local_state_stream->locally_held, remote_stream);
1874                         } else if (sdp_state->role == SDP_ROLE_ANSWERER) {
1875                                 /* Stream type is changing. */
1876                                 joint_stream = merge_remote_stream(sdp_state, NULL,
1877                                         local_state_stream->locally_held, remote_stream);
1878                         } else {
1879                                 /*
1880                                  * Remote cannot change the stream type we offered.
1881                                  * Mark as declined.
1882                                  */
1883                                 ast_debug(1,
1884                                         "Bad.  Remote answer SDP trying to change the stream type from %s to %s.\n",
1885                                         ast_codec_media_type2str(local_stream_type),
1886                                         ast_codec_media_type2str(remote_stream_type));
1887                                 joint_stream = decline_stream(local_stream_type,
1888                                         ast_stream_get_name(local_stream));
1889                         }
1890                 } else {
1891                         /* Local stream is either dead/declined or nonexistent. */
1892                         if (sdp_state->role == SDP_ROLE_ANSWERER) {
1893                                 if (sdp_is_stream_type_supported(remote_stream_type)
1894                                         && ast_stream_get_state(remote_stream) != AST_STREAM_STATE_REMOVED
1895                                         && idx < ast_sdp_options_get_max_streams(sdp_state->options)) {
1896                                         /* Try to create the new stream */
1897                                         joint_stream = merge_remote_stream(sdp_state, NULL,
1898                                                 local_state_stream ? local_state_stream->locally_held : 0,
1899                                                 remote_stream);
1900                                 } else {
1901                                         const char *stream_name;
1902
1903                                         /* Decline the remote stream. */
1904                                         if (local_stream
1905                                                 && local_stream_type == remote_stream_type) {
1906                                                 /* Preserve the previous stream name */
1907                                                 stream_name = ast_stream_get_name(local_stream);
1908                                         } else {
1909                                                 stream_name = NULL;
1910                                         }
1911                                         joint_stream = decline_stream(remote_stream_type, stream_name);
1912                                 }
1913                         } else {
1914                                 /* Decline the stream. */
1915                                 if (DEBUG_ATLEAST(1)
1916                                         && ast_stream_get_state(remote_stream) != AST_STREAM_STATE_REMOVED) {
1917                                         /*
1918                                          * Remote cannot request a new stream in place of a declined
1919                                          * stream in an answer SDP.
1920                                          */
1921                                         ast_log(LOG_DEBUG,
1922                                                 "Bad.  Remote answer SDP trying to use a declined stream slot for %s.\n",
1923                                                 ast_codec_media_type2str(remote_stream_type));
1924                                 }
1925                                 joint_stream = decline_stream(local_stream_type,
1926                                         ast_stream_get_name(local_stream));
1927                         }
1928                 }
1929
1930                 if (!joint_stream) {
1931                         goto fail;
1932                 }
1933                 if (ast_stream_topology_append_stream(joint_topology, joint_stream) < 0) {
1934                         ast_stream_free(joint_stream);
1935                         goto fail;
1936                 }
1937         }
1938
1939         return joint_topology;
1940
1941 fail:
1942         ast_stream_topology_free(joint_topology);
1943         return NULL;
1944 }
1945
1946 /*!
1947  * \brief Merge our stream capabilities and a remote topology into joint capabilities.
1948  *
1949  * \param sdp_state The state needing capabilities merged
1950  * \param remote_topology The topology to merge with our proposed capabilities
1951  *
1952  * \details
1953  * This is a bit complicated. The idea is that we already have some
1954  * capabilities set, and we've now been confronted with a stream
1955  * topology from the remote end.  We want to take what's been
1956  * presented to us and merge those new capabilities with our own.
1957  *
1958  * According to the RFC, stream slots can change their types only if
1959  * they are carrying the same logical information or an offer is
1960  * reusing a declined slot or new stream slots are added to the end
1961  * of the list.  Switching a stream from audio to T.38 makes sense
1962  * because the stream slot is carrying the same information just in a
1963  * different format.
1964  *
1965  * When we are the answerer we can setup new streams offered by the
1966  * remote up to our configured maximum stream slots.  New stream
1967  * slots offered over the maximum are unconditionally declined.
1968  *
1969  * \retval NULL An error occurred
1970  * \retval non-NULL The merged capabilities
1971  */
1972 static struct sdp_state_capabilities *merge_remote_capabilities(
1973         const struct ast_sdp_state *sdp_state,
1974         const struct ast_stream_topology *remote_topology)
1975 {
1976         const struct sdp_state_capabilities *local = sdp_state->proposed_capabilities;
1977         struct sdp_state_capabilities *joint_capabilities;
1978         int idx;
1979
1980         ast_assert(local != NULL);
1981
1982         joint_capabilities = ast_calloc(1, sizeof(*joint_capabilities));
1983         if (!joint_capabilities) {
1984                 return NULL;
1985         }
1986
1987         joint_capabilities->topology = merge_remote_topology(sdp_state, local, remote_topology);
1988         if (!joint_capabilities->topology) {
1989                 goto fail;
1990         }
1991
1992         if (sdp_state->role == SDP_ROLE_ANSWERER) {
1993                 sdp_state_cb_answerer_modify_topology(sdp_state, joint_capabilities->topology);
1994         }
1995         idx = ast_stream_topology_get_count(joint_capabilities->topology);
1996         if (AST_VECTOR_INIT(&joint_capabilities->streams, idx)) {
1997                 goto fail;
1998         }
1999
2000         for (idx = 0; idx < ast_stream_topology_get_count(remote_topology); ++idx) {
2001                 enum ast_media_type local_stream_type;
2002                 enum ast_media_type remote_stream_type;
2003                 struct ast_stream *remote_stream;
2004                 struct ast_stream *local_stream;
2005                 struct ast_stream *joint_stream;
2006                 struct sdp_state_stream *local_state_stream;
2007                 struct sdp_state_stream *joint_state_stream;
2008
2009                 joint_state_stream = ast_calloc(1, sizeof(*joint_state_stream));
2010                 if (!joint_state_stream) {
2011                         goto fail;
2012                 }
2013
2014                 remote_stream = ast_stream_topology_get_stream(remote_topology, idx);
2015                 remote_stream_type = ast_stream_get_type(remote_stream);
2016
2017                 if (idx < ast_stream_topology_get_count(local->topology)) {
2018                         local_state_stream = AST_VECTOR_GET(&local->streams, idx);
2019                         local_stream = ast_stream_topology_get_stream(local->topology, idx);
2020                         local_stream_type = ast_stream_get_type(local_stream);
2021                 } else {
2022                         /* The remote is adding a stream slot */
2023                         local_state_stream = NULL;
2024                         local_stream = NULL;
2025                         local_stream_type = AST_MEDIA_TYPE_UNKNOWN;
2026
2027                         if (sdp_state->role != SDP_ROLE_ANSWERER) {
2028                                 /* Remote cannot add a new stream slot in an answer SDP */
2029                                 sdp_state_stream_free(joint_state_stream);
2030                                 break;
2031                         }
2032                 }
2033
2034                 joint_stream = ast_stream_topology_get_stream(joint_capabilities->topology,
2035                         idx);
2036
2037                 if (local_stream
2038                         && ast_stream_get_state(local_stream) != AST_STREAM_STATE_REMOVED) {
2039                         if (ast_stream_get_state(joint_stream) == AST_STREAM_STATE_REMOVED) {
2040                                 /* Copy everything potentially useful to a declined stream state. */
2041                                 sdp_state_stream_copy_common(joint_state_stream, local_state_stream);
2042
2043                                 joint_state_stream->type = ast_stream_get_type(joint_stream);
2044                         } else if (remote_stream_type == local_stream_type) {
2045                                 /* Stream type is not changing. */
2046                                 merge_remote_stream_capabilities(sdp_state, joint_state_stream,
2047                                         local_state_stream, remote_stream);
2048                                 ast_assert(joint_state_stream->type == ast_stream_get_type(joint_stream));
2049                         } else {
2050                                 /*
2051                                  * Stream type is changing.  Need to replace the stream.
2052                                  *
2053                                  * XXX We might need to keep the old RTP instance if the new
2054                                  * stream type is also RTP.  We would just be changing between
2055                                  * audio and video in that case.  However we will create a new
2056                                  * RTP instance anyway since its purpose has to be changing.
2057                                  * Any RTP packets in flight from the old stream type might
2058                                  * cause mischief.
2059                                  */
2060                                 if (create_remote_stream_capabilities(sdp_state, joint_state_stream,
2061                                         local_state_stream, remote_stream)) {
2062                                         sdp_state_stream_free(joint_state_stream);
2063                                         goto fail;
2064                                 }
2065                                 ast_assert(joint_state_stream->type == ast_stream_get_type(joint_stream));
2066                         }
2067                 } else {
2068                         /* Local stream is either dead/declined or nonexistent. */
2069                         if (sdp_state->role == SDP_ROLE_ANSWERER) {
2070                                 if (ast_stream_get_state(joint_stream) == AST_STREAM_STATE_REMOVED) {
2071                                         if (local_state_stream) {
2072                                                 /* Copy everything potentially useful to a declined stream state. */
2073                                                 sdp_state_stream_copy_common(joint_state_stream, local_state_stream);
2074                                         }
2075                                         joint_state_stream->type = ast_stream_get_type(joint_stream);
2076                                 } else {
2077                                         /* Try to create the new stream */
2078                                         if (create_remote_stream_capabilities(sdp_state, joint_state_stream,
2079                                                 local_state_stream, remote_stream)) {
2080                                                 sdp_state_stream_free(joint_state_stream);
2081                                                 goto fail;
2082                                         }
2083                                         ast_assert(joint_state_stream->type == ast_stream_get_type(joint_stream));
2084                                 }
2085                         } else {
2086                                 /* Decline the stream. */
2087                                 ast_assert(ast_stream_get_state(joint_stream) == AST_STREAM_STATE_REMOVED);
2088                                 if (local_state_stream) {
2089                                         /* Copy everything potentially useful to a declined stream state. */
2090                                         sdp_state_stream_copy_common(joint_state_stream, local_state_stream);
2091                                 }
2092                                 joint_state_stream->type = ast_stream_get_type(joint_stream);
2093                         }
2094                 }
2095
2096                 /* Determine if the remote placed the stream on hold. */
2097                 joint_state_stream->remotely_held = 0;
2098                 if (ast_stream_get_state(joint_stream) != AST_STREAM_STATE_REMOVED) {
2099                         enum ast_stream_state remote_state;
2100
2101                         remote_state = ast_stream_get_state(remote_stream);
2102                         switch (remote_state) {
2103                         case AST_STREAM_STATE_INACTIVE:
2104                         case AST_STREAM_STATE_SENDONLY:
2105                                 joint_state_stream->remotely_held = 1;
2106                                 break;
2107                         default:
2108                                 break;
2109                         }
2110                 }
2111
2112                 if (AST_VECTOR_APPEND(&joint_capabilities->streams, joint_state_stream)) {
2113                         sdp_state_stream_free(joint_state_stream);
2114                         goto fail;
2115                 }
2116         }
2117
2118         return joint_capabilities;
2119
2120 fail:
2121         sdp_state_capabilities_free(joint_capabilities);
2122         return NULL;
2123 }
2124
2125 /*!
2126  * \brief Apply remote SDP's ICE information to our RTP session
2127  *
2128  * \param state The SDP state on which negotiation has taken place
2129  * \param options The SDP options we support
2130  * \param remote_sdp The SDP we most recently received
2131  * \param remote_m_line The stream on which we are examining ICE candidates
2132  */
2133 static void update_ice(const struct ast_sdp_state *state, struct ast_rtp_instance *rtp, const struct ast_sdp_options *options,
2134         const struct ast_sdp *remote_sdp, const struct ast_sdp_m_line *remote_m_line)
2135 {
2136         struct ast_rtp_engine_ice *ice;
2137         const struct ast_sdp_a_line *attr;
2138         const struct ast_sdp_a_line *attr_rtcp_mux;
2139         unsigned int attr_i;
2140
2141         /* If ICE support is not enabled or available exit early */
2142         if (ast_sdp_options_get_ice(options) != AST_SDP_ICE_ENABLED_STANDARD || !(ice = ast_rtp_instance_get_ice(rtp))) {
2143                 return;
2144         }
2145
2146         attr = ast_sdp_m_find_attribute(remote_m_line, "ice-ufrag", -1);
2147         if (!attr) {
2148                 attr = ast_sdp_find_attribute(remote_sdp, "ice-ufrag", -1);
2149         }
2150         if (attr) {
2151                 ice->set_authentication(rtp, attr->value, NULL);
2152         } else {
2153                 return;
2154         }
2155
2156         attr = ast_sdp_m_find_attribute(remote_m_line, "ice-pwd", -1);
2157         if (!attr) {
2158                 attr = ast_sdp_find_attribute(remote_sdp, "ice-pwd", -1);
2159         }
2160         if (attr) {
2161                 ice->set_authentication(rtp, NULL, attr->value);
2162         } else {
2163                 return;
2164         }
2165
2166         if (ast_sdp_find_attribute(remote_sdp, "ice-lite", -1)) {
2167                 ice->ice_lite(rtp);
2168         }
2169
2170         attr_rtcp_mux = ast_sdp_m_find_attribute(remote_m_line, "rtcp-mux", -1);
2171
2172         /* Find all of the candidates */
2173         for (attr_i = 0; attr_i < ast_sdp_m_get_a_count(remote_m_line); ++attr_i) {
2174                 char foundation[33];
2175                 char transport[32];
2176                 char address[INET6_ADDRSTRLEN + 1];
2177                 char cand_type[6];
2178                 char relay_address[INET6_ADDRSTRLEN + 1] = "";
2179                 unsigned int port;
2180                 unsigned int relay_port = 0;
2181                 struct ast_rtp_engine_ice_candidate candidate = { 0, };
2182
2183                 attr = ast_sdp_m_get_a(remote_m_line, attr_i);
2184
2185                 /* If this is not a candidate line skip it */
2186                 if (strcmp(attr->name, "candidate")) {
2187                         continue;
2188                 }
2189
2190                 if (sscanf(attr->value, "%32s %30u %31s %30u %46s %30u typ %5s %*s %23s %*s %30u",
2191                         foundation, &candidate.id, transport, (unsigned *)&candidate.priority, address,
2192                         &port, cand_type, relay_address, &relay_port) < 7) {
2193                         /* Candidate did not parse properly */
2194                         continue;
2195                 }
2196
2197                 if (candidate.id > 1
2198                         && attr_rtcp_mux
2199                         && ast_sdp_options_get_rtcp_mux(options)) {
2200                         /* Remote side may have offered RTP and RTCP candidates. However, if we're using RTCP MUX,
2201                          * then we should ignore RTCP candidates.
2202                          */
2203                         continue;
2204                 }
2205
2206                 candidate.foundation = foundation;
2207                 candidate.transport = transport;
2208
2209                 ast_sockaddr_parse(&candidate.address, address, PARSE_PORT_FORBID);
2210                 ast_sockaddr_set_port(&candidate.address, port);
2211
2212                 if (!strcasecmp(cand_type, "host")) {
2213                         candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_HOST;
2214                 } else if (!strcasecmp(cand_type, "srflx")) {
2215                         candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX;
2216                 } else if (!strcasecmp(cand_type, "relay")) {
2217                         candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED;
2218                 } else {
2219                         continue;
2220                 }
2221
2222                 if (!ast_strlen_zero(relay_address)) {
2223                         ast_sockaddr_parse(&candidate.relay_address, relay_address, PARSE_PORT_FORBID);
2224                 }
2225
2226                 if (relay_port) {
2227                         ast_sockaddr_set_port(&candidate.relay_address, relay_port);
2228                 }
2229
2230                 ice->add_remote_candidate(rtp, &candidate);
2231         }
2232
2233         if (state->role == SDP_ROLE_OFFERER) {
2234                 ice->set_role(rtp, AST_RTP_ICE_ROLE_CONTROLLING);
2235         } else {
2236                 ice->set_role(rtp, AST_RTP_ICE_ROLE_CONTROLLED);
2237         }
2238
2239         ice->start(rtp);
2240 }
2241
2242 /*!
2243  * \brief Update RTP instances based on merged SDPs
2244  *
2245  * RTP instances, when first allocated, cannot make assumptions about what the other
2246  * side supports and thus has to go with some default behaviors. This function gets
2247  * called after we know both what we support and what the remote endpoint supports.
2248  * This way, we can update the RTP instance to reflect what is supported by both
2249  * sides.
2250  *
2251  * \param state The SDP state in which SDPs have been negotiated
2252  * \param rtp The RTP wrapper that is being updated
2253  * \param options Our locally-supported SDP options
2254  * \param remote_sdp The SDP we most recently received
2255  * \param remote_m_line The remote SDP stream that corresponds to the RTP instance we are modifying
2256  */
2257 static void update_rtp_after_merge(const struct ast_sdp_state *state,
2258         struct sdp_state_rtp *rtp,
2259     const struct ast_sdp_options *options,
2260         const struct ast_sdp *remote_sdp,
2261         const struct ast_sdp_m_line *remote_m_line)
2262 {
2263         struct ast_sdp_c_line *c_line;
2264         struct ast_sockaddr *addrs;
2265
2266         c_line = remote_m_line->c_line;
2267         if (!c_line) {
2268                 c_line = remote_sdp->c_line;
2269         }
2270         /*
2271          * There must be a c= line somewhere but that would be an error by
2272          * the far end that should have been caught by a validation check
2273          * before we processed the SDP.
2274          */
2275         ast_assert(c_line != NULL);
2276
2277         if (ast_sockaddr_resolve(&addrs, c_line->address, PARSE_PORT_FORBID, AST_AF_UNSPEC) > 0) {
2278                 /* Apply connection information to the RTP instance */
2279                 ast_sockaddr_set_port(addrs, remote_m_line->port);
2280                 ast_rtp_instance_set_remote_address(rtp->instance, addrs);
2281                 ast_free(addrs);
2282         }
2283
2284         if (ast_sdp_options_get_rtcp_mux(options)
2285                 && ast_sdp_m_find_attribute(remote_m_line, "rtcp-mux", -1)) {
2286                 ast_rtp_instance_set_prop(rtp->instance, AST_RTP_PROPERTY_RTCP,
2287                         AST_RTP_INSTANCE_RTCP_MUX);
2288         } else {
2289                 ast_rtp_instance_set_prop(rtp->instance, AST_RTP_PROPERTY_RTCP,
2290                         AST_RTP_INSTANCE_RTCP_STANDARD);
2291         }
2292
2293         update_ice(state, rtp->instance, options, remote_sdp, remote_m_line);
2294 }
2295
2296 /*!
2297  * \brief Update UDPTL instances based on merged SDPs
2298  *
2299  * UDPTL instances, when first allocated, cannot make assumptions about what the other
2300  * side supports and thus has to go with some default behaviors. This function gets
2301  * called after we know both what we support and what the remote endpoint supports.
2302  * This way, we can update the UDPTL instance to reflect what is supported by both
2303  * sides.
2304  *
2305  * \param state The SDP state in which SDPs have been negotiated
2306  * \param udptl The UDPTL instance that is being updated
2307  * \param options Our locally-supported SDP options
2308  * \param remote_sdp The SDP we most recently received
2309  * \param remote_m_line The remote SDP stream that corresponds to the RTP instance we are modifying
2310  */
2311 static void update_udptl_after_merge(const struct ast_sdp_state *state, struct sdp_state_udptl *udptl,
2312     const struct ast_sdp_options *options,
2313         const struct ast_sdp *remote_sdp,
2314         const struct ast_sdp_m_line *remote_m_line)
2315 {
2316         struct ast_sdp_a_line *a_line;
2317         struct ast_sdp_c_line *c_line;
2318         unsigned int fax_max_datagram;
2319         struct ast_sockaddr *addrs;
2320
2321         a_line = ast_sdp_m_find_attribute(remote_m_line, "t38faxmaxdatagram", -1);
2322         if (!a_line) {
2323                 a_line = ast_sdp_m_find_attribute(remote_m_line, "t38maxdatagram", -1);
2324         }
2325         if (a_line && !ast_sdp_options_get_udptl_far_max_datagram(options) &&
2326                 (sscanf(a_line->value, "%30u", &fax_max_datagram) == 1)) {
2327                 ast_udptl_set_far_max_datagram(udptl->instance, fax_max_datagram);
2328         }
2329
2330         a_line = ast_sdp_m_find_attribute(remote_m_line, "t38faxudpec", -1);
2331         if (a_line) {
2332                 if (!strcasecmp(a_line->value, "t38UDPRedundancy")) {
2333                         ast_udptl_set_error_correction_scheme(udptl->instance, UDPTL_ERROR_CORRECTION_REDUNDANCY);
2334                 } else if (!strcasecmp(a_line->value, "t38UDPFEC")) {
2335                         ast_udptl_set_error_correction_scheme(udptl->instance, UDPTL_ERROR_CORRECTION_FEC);
2336                 } else {
2337                         ast_udptl_set_error_correction_scheme(udptl->instance, UDPTL_ERROR_CORRECTION_NONE);
2338                 }
2339         }
2340
2341         c_line = remote_m_line->c_line;
2342         if (!c_line) {
2343                 c_line = remote_sdp->c_line;
2344         }
2345         /*
2346          * There must be a c= line somewhere but that would be an error by
2347          * the far end that should have been caught by a validation check
2348          * before we processed the SDP.
2349          */
2350         ast_assert(c_line != NULL);
2351
2352         if (ast_sockaddr_resolve(&addrs, c_line->address, PARSE_PORT_FORBID, AST_AF_UNSPEC) > 0) {
2353                 /* Apply connection information to the UDPTL instance */
2354                 ast_sockaddr_set_port(addrs, remote_m_line->port);
2355                 ast_udptl_set_peer(udptl->instance, addrs);
2356                 ast_free(addrs);
2357         }
2358 }
2359
2360 static void sdp_apply_negotiated_state(struct ast_sdp_state *sdp_state)
2361 {
2362         struct sdp_state_capabilities *capabilities = sdp_state->negotiated_capabilities;
2363         int idx;
2364
2365         if (!capabilities) {
2366                 /* Nothing to apply */
2367                 return;
2368         }
2369
2370         sdp_state_cb_preapply_topology(sdp_state, capabilities->topology);
2371         for (idx = 0; idx < AST_VECTOR_SIZE(&capabilities->streams); ++idx) {
2372                 struct sdp_state_stream *state_stream;
2373                 struct ast_stream *stream;
2374
2375                 stream = ast_stream_topology_get_stream(capabilities->topology, idx);
2376                 if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) {
2377                         /* Stream is declined */
2378                         continue;
2379                 }
2380
2381                 state_stream = AST_VECTOR_GET(&capabilities->streams, idx);
2382                 switch (ast_stream_get_type(stream)) {
2383                 case AST_MEDIA_TYPE_AUDIO:
2384                 case AST_MEDIA_TYPE_VIDEO:
2385                         update_rtp_after_merge(sdp_state, state_stream->rtp, sdp_state->options,
2386                                 sdp_state->remote_sdp, ast_sdp_get_m(sdp_state->remote_sdp, idx));
2387                         break;
2388                 case AST_MEDIA_TYPE_IMAGE:
2389                         update_udptl_after_merge(sdp_state, state_stream->udptl, sdp_state->options,
2390                                 sdp_state->remote_sdp, ast_sdp_get_m(sdp_state->remote_sdp, idx));
2391                         break;
2392                 case AST_MEDIA_TYPE_UNKNOWN:
2393                 case AST_MEDIA_TYPE_TEXT:
2394                 case AST_MEDIA_TYPE_END:
2395                         /* All unsupported streams are declined */
2396                         ast_assert(0);
2397                         break;
2398                 }
2399         }
2400         sdp_state_cb_postapply_topology(sdp_state, capabilities->topology);
2401 }
2402
2403 static void set_negotiated_capabilities(struct ast_sdp_state *sdp_state,
2404         struct sdp_state_capabilities *new_capabilities)
2405 {
2406         struct sdp_state_capabilities *old_capabilities = sdp_state->negotiated_capabilities;
2407
2408         sdp_state->negotiated_capabilities = new_capabilities;
2409         sdp_state_capabilities_free(old_capabilities);
2410 }
2411
2412 static void set_proposed_capabilities(struct ast_sdp_state *sdp_state,
2413         struct sdp_state_capabilities *new_capabilities)
2414 {
2415         struct sdp_state_capabilities *old_capabilities = sdp_state->proposed_capabilities;
2416
2417         sdp_state->proposed_capabilities = new_capabilities;
2418         sdp_state_capabilities_free(old_capabilities);
2419 }
2420
2421 /*!
2422  * \internal
2423  * \brief Copy the new capabilities into the proposed capabilities.
2424  * \since 15.0.0
2425  *
2426  * \param sdp_state The current SDP state
2427  * \param new_capabilities Capabilities to copy
2428  *
2429  * \retval 0 on success.
2430  * \retval -1 on failure.
2431  */
2432 static int update_proposed_capabilities(struct ast_sdp_state *sdp_state,
2433         struct sdp_state_capabilities *new_capabilities)
2434 {
2435         struct sdp_state_capabilities *proposed_capabilities;
2436         int idx;
2437
2438         proposed_capabilities = ast_calloc(1, sizeof(*proposed_capabilities));
2439         if (!proposed_capabilities) {
2440                 return -1;
2441         }
2442
2443         proposed_capabilities->topology = ast_stream_topology_clone(new_capabilities->topology);
2444         if (!proposed_capabilities->topology) {
2445                 goto fail;
2446         }
2447
2448         if (AST_VECTOR_INIT(&proposed_capabilities->streams,
2449                 AST_VECTOR_SIZE(&new_capabilities->streams))) {
2450                 goto fail;
2451         }
2452
2453         for (idx = 0; idx < AST_VECTOR_SIZE(&new_capabilities->streams); ++idx) {
2454                 struct sdp_state_stream *proposed_state_stream;
2455                 struct sdp_state_stream *new_state_stream;
2456
2457                 proposed_state_stream = ast_calloc(1, sizeof(*proposed_state_stream));
2458                 if (!proposed_state_stream) {
2459                         goto fail;
2460                 }
2461
2462                 new_state_stream = AST_VECTOR_GET(&new_capabilities->streams, idx);
2463                 *proposed_state_stream = *new_state_stream;
2464
2465                 switch (proposed_state_stream->type) {
2466                 case AST_MEDIA_TYPE_AUDIO:
2467                 case AST_MEDIA_TYPE_VIDEO:
2468                         ao2_bump(proposed_state_stream->rtp);
2469                         break;
2470                 case AST_MEDIA_TYPE_IMAGE:
2471                         ao2_bump(proposed_state_stream->udptl);
2472                         break;
2473                 case AST_MEDIA_TYPE_UNKNOWN:
2474                 case AST_MEDIA_TYPE_TEXT:
2475                 case AST_MEDIA_TYPE_END:
2476                         break;
2477                 }
2478
2479                 /* This is explicitly never set on the proposed capabilities struct */
2480                 proposed_state_stream->remotely_held = 0;
2481
2482                 if (AST_VECTOR_APPEND(&proposed_capabilities->streams, proposed_state_stream)) {
2483                         sdp_state_stream_free(proposed_state_stream);
2484                         goto fail;
2485                 }
2486         }
2487
2488         set_proposed_capabilities(sdp_state, proposed_capabilities);
2489         return 0;
2490
2491 fail:
2492         sdp_state_capabilities_free(proposed_capabilities);
2493         return -1;
2494 }
2495
2496 static struct ast_sdp *sdp_create_from_state(const struct ast_sdp_state *sdp_state,
2497         const struct sdp_state_capabilities *capabilities);
2498
2499 /*!
2500  * \brief Merge SDPs into a joint SDP.
2501  *
2502  * This function is used to take a remote SDP and merge it with our local
2503  * capabilities to produce a new local SDP.  After creating the new local SDP,
2504  * it then iterates through media instances and updates them as necessary.  For
2505  * instance, if a specific RTP feature is supported by both us and the far end,
2506  * then we can ensure that the feature is enabled.
2507  *
2508  * \param sdp_state The current SDP state
2509  *
2510  * \retval 0 Success
2511  * \retval -1 Failure
2512  *         Use ast_sdp_state_is_offer_rejected() to see if the offer SDP was rejected.
2513  */
2514 static int merge_sdps(struct ast_sdp_state *sdp_state, const struct ast_sdp *remote_sdp)
2515 {
2516         struct sdp_state_capabilities *joint_capabilities;
2517         struct ast_stream_topology *remote_capabilities;
2518
2519         remote_capabilities = ast_get_topology_from_sdp(remote_sdp,
2520                 ast_sdp_options_get_g726_non_standard(sdp_state->options));
2521         if (!remote_capabilities) {
2522                 return -1;
2523         }
2524
2525         joint_capabilities = merge_remote_capabilities(sdp_state, remote_capabilities);
2526         ast_stream_topology_free(remote_capabilities);
2527         if (!joint_capabilities) {
2528                 return -1;
2529         }
2530         if (sdp_state->role == SDP_ROLE_ANSWERER) {
2531                 sdp_state->remote_offer_rejected =
2532                         sdp_topology_is_rejected(joint_capabilities->topology) ? 1 : 0;
2533                 if (sdp_state->remote_offer_rejected) {
2534                         sdp_state_capabilities_free(joint_capabilities);
2535                         return -1;
2536                 }
2537         }
2538         set_negotiated_capabilities(sdp_state, joint_capabilities);
2539
2540         ao2_cleanup(sdp_state->remote_sdp);
2541         sdp_state->remote_sdp = ao2_bump((struct ast_sdp *) remote_sdp);
2542
2543         sdp_apply_negotiated_state(sdp_state);
2544
2545         return 0;
2546 }
2547
2548 const struct ast_sdp *ast_sdp_state_get_local_sdp(struct ast_sdp_state *sdp_state)
2549 {
2550         ast_assert(sdp_state != NULL);
2551
2552         switch (sdp_state->role) {
2553         case SDP_ROLE_NOT_SET:
2554                 ast_assert(sdp_state->local_sdp == NULL);
2555                 sdp_state->role = SDP_ROLE_OFFERER;
2556
2557                 if (sdp_state->pending_topology_update) {
2558                         struct sdp_state_capabilities *capabilities;
2559
2560                         /* We have a topology update to perform before generating the offer */
2561                         capabilities = merge_local_capabilities(sdp_state,
2562                                 sdp_state->pending_topology_update);
2563                         if (!capabilities) {
2564                                 break;
2565                         }
2566                         ast_stream_topology_free(sdp_state->pending_topology_update);
2567                         sdp_state->pending_topology_update = NULL;
2568                         set_proposed_capabilities(sdp_state, capabilities);
2569                 }
2570
2571                 /*
2572                  * Allow the system to configure the topology streams
2573                  * before we create the offer SDP.
2574                  */
2575                 sdp_state_cb_offerer_config_topology(sdp_state,
2576                         sdp_state->proposed_capabilities->topology);
2577
2578                 sdp_state->local_sdp = sdp_create_from_state(sdp_state, sdp_state->proposed_capabilities);
2579                 break;
2580         case SDP_ROLE_OFFERER:
2581                 break;
2582         case SDP_ROLE_ANSWERER:
2583                 if (!sdp_state->local_sdp
2584                         && sdp_state->negotiated_capabilities
2585                         && !sdp_state->remote_offer_rejected) {
2586                         sdp_state->local_sdp = sdp_create_from_state(sdp_state, sdp_state->negotiated_capabilities);
2587                 }
2588                 break;
2589         }
2590
2591         return sdp_state->local_sdp;
2592 }
2593
2594 const void *ast_sdp_state_get_local_sdp_impl(struct ast_sdp_state *sdp_state)
2595 {
2596         const struct ast_sdp *sdp = ast_sdp_state_get_local_sdp(sdp_state);
2597
2598         if (!sdp) {
2599                 return NULL;
2600         }
2601
2602         return ast_sdp_translator_from_sdp(sdp_state->translator, sdp);
2603 }
2604
2605 int ast_sdp_state_set_remote_sdp(struct ast_sdp_state *sdp_state, const struct ast_sdp *sdp)
2606 {
2607         ast_assert(sdp_state != NULL);
2608
2609         if (sdp_state->role == SDP_ROLE_NOT_SET) {
2610                 sdp_state->role = SDP_ROLE_ANSWERER;
2611         }
2612
2613         return merge_sdps(sdp_state, sdp);
2614 }
2615
2616 int ast_sdp_state_set_remote_sdp_from_impl(struct ast_sdp_state *sdp_state, const void *remote)
2617 {
2618         struct ast_sdp *sdp;
2619         int ret;
2620
2621         ast_assert(sdp_state != NULL);
2622
2623         sdp = ast_sdp_translator_to_sdp(sdp_state->translator, remote);
2624         if (!sdp) {
2625                 return -1;
2626         }
2627         ret = ast_sdp_state_set_remote_sdp(sdp_state, sdp);
2628         ao2_ref(sdp, -1);
2629         return ret;
2630 }
2631
2632 int ast_sdp_state_is_offer_rejected(struct ast_sdp_state *sdp_state)
2633 {
2634         return sdp_state->remote_offer_rejected;
2635 }
2636
2637 int ast_sdp_state_is_offerer(struct ast_sdp_state *sdp_state)
2638 {
2639         return sdp_state->role == SDP_ROLE_OFFERER;
2640 }
2641
2642 int ast_sdp_state_is_answerer(struct ast_sdp_state *sdp_state)
2643 {
2644         return sdp_state->role == SDP_ROLE_ANSWERER;
2645 }
2646
2647 int ast_sdp_state_restart_negotiations(struct ast_sdp_state *sdp_state)
2648 {
2649         ast_assert(sdp_state != NULL);
2650
2651         ao2_cleanup(sdp_state->local_sdp);
2652         sdp_state->local_sdp = NULL;
2653
2654         sdp_state->role = SDP_ROLE_NOT_SET;
2655         sdp_state->remote_offer_rejected = 0;
2656
2657         if (sdp_state->negotiated_capabilities) {
2658                 update_proposed_capabilities(sdp_state, sdp_state->negotiated_capabilities);
2659         }
2660
2661         return 0;
2662 }
2663
2664 int ast_sdp_state_update_local_topology(struct ast_sdp_state *sdp_state, struct ast_stream_topology *topology)
2665 {
2666         struct ast_stream_topology *merged_topology;
2667
2668         ast_assert(sdp_state != NULL);
2669         ast_assert(topology != NULL);
2670
2671         if (sdp_state->pending_topology_update) {
2672                 merged_topology = merge_local_topologies(sdp_state,
2673                         sdp_state->pending_topology_update, topology, 0);
2674                 if (!merged_topology) {
2675                         return -1;
2676                 }
2677                 ast_stream_topology_free(sdp_state->pending_topology_update);
2678                 sdp_state->pending_topology_update = merged_topology;
2679         } else {
2680                 sdp_state->pending_topology_update = ast_stream_topology_clone(topology);
2681                 if (!sdp_state->pending_topology_update) {
2682                         return -1;
2683                 }
2684         }
2685
2686         return 0;
2687 }
2688
2689 void ast_sdp_state_set_local_address(struct ast_sdp_state *sdp_state, struct ast_sockaddr *address)
2690 {
2691         ast_assert(sdp_state != NULL);
2692
2693         if (!address) {
2694                 ast_sockaddr_setnull(&sdp_state->connection_address);
2695         } else {
2696                 ast_sockaddr_copy(&sdp_state->connection_address, address);
2697         }
2698 }
2699
2700 int ast_sdp_state_set_connection_address(struct ast_sdp_state *sdp_state, int stream_index,
2701         struct ast_sockaddr *address)
2702 {
2703         struct sdp_state_stream *stream_state;
2704         ast_assert(sdp_state != NULL);
2705
2706         stream_state = sdp_state_get_stream(sdp_state, stream_index);
2707         if (!stream_state) {
2708                 return -1;
2709         }
2710
2711         if (!address) {
2712                 ast_sockaddr_setnull(&stream_state->connection_address);
2713         } else {
2714                 ast_sockaddr_copy(&stream_state->connection_address, address);
2715         }
2716
2717         return 0;
2718 }
2719
2720 void ast_sdp_state_set_global_locally_held(struct ast_sdp_state *sdp_state, unsigned int locally_held)
2721 {
2722         ast_assert(sdp_state != NULL);
2723
2724         sdp_state->locally_held = locally_held ? 1 : 0;
2725 }
2726
2727 unsigned int ast_sdp_state_get_global_locally_held(const struct ast_sdp_state *sdp_state)
2728 {
2729         ast_assert(sdp_state != NULL);
2730
2731         return sdp_state->locally_held;
2732 }
2733
2734 void ast_sdp_state_set_locally_held(struct ast_sdp_state *sdp_state,
2735         int stream_index, unsigned int locally_held)
2736 {
2737         struct sdp_state_stream *stream_state;
2738         ast_assert(sdp_state != NULL);
2739
2740         locally_held = locally_held ? 1 : 0;
2741
2742         stream_state = sdp_state_get_joint_stream(sdp_state, stream_index);
2743         if (stream_state) {
2744                 stream_state->locally_held = locally_held;
2745         }
2746
2747         stream_state = sdp_state_get_stream(sdp_state, stream_index);
2748         if (stream_state) {
2749                 stream_state->locally_held = locally_held;
2750         }
2751 }
2752
2753 unsigned int ast_sdp_state_get_locally_held(const struct ast_sdp_state *sdp_state,
2754         int stream_index)
2755 {
2756         struct sdp_state_stream *stream_state;
2757         ast_assert(sdp_state != NULL);
2758
2759         stream_state = sdp_state_get_joint_stream(sdp_state, stream_index);
2760         if (!stream_state) {
2761                 return 0;
2762         }
2763
2764         return stream_state->locally_held;
2765 }
2766
2767 unsigned int ast_sdp_state_get_remotely_held(const struct ast_sdp_state *sdp_state,
2768         int stream_index)
2769 {
2770         struct sdp_state_stream *stream_state;
2771
2772         ast_assert(sdp_state != NULL);
2773
2774         stream_state = sdp_state_get_joint_stream(sdp_state, stream_index);
2775         if (!stream_state) {
2776                 return 0;
2777         }
2778
2779         return stream_state->remotely_held;
2780 }
2781
2782 void ast_sdp_state_set_t38_parameters(struct ast_sdp_state *sdp_state,
2783         int stream_index, struct ast_control_t38_parameters *params)
2784 {
2785         struct sdp_state_stream *stream_state;
2786         ast_assert(sdp_state != NULL && params != NULL);
2787
2788         stream_state = sdp_state_get_stream(sdp_state, stream_index);
2789         if (stream_state) {
2790                 stream_state->t38_local_params = *params;
2791         }
2792 }
2793
2794 /*!
2795  * \brief Add SSRC-level attributes if appropriate.
2796  *
2797  * This function does nothing if the SDP options indicate not to add SSRC-level attributes.
2798  *
2799  * Currently, the only attribute added is cname, which is retrieved from the RTP instance.
2800  *
2801  * \param m_line The m_line on which to add the SSRC attributes
2802  * \param options Options that indicate what, if any, SSRC attributes to add
2803  * \param rtp RTP instance from which we get SSRC-level information
2804  */
2805 static void add_ssrc_attributes(struct ast_sdp_m_line *m_line, const struct ast_sdp_options *options,
2806         struct ast_rtp_instance *rtp)
2807 {
2808         struct ast_sdp_a_line *a_line;
2809         char attr_buffer[128];
2810
2811         if (!ast_sdp_options_get_ssrc(options)) {
2812                 return;
2813         }
2814
2815         snprintf(attr_buffer, sizeof(attr_buffer), "%u cname:%s", ast_rtp_instance_get_ssrc(rtp),
2816                 ast_rtp_instance_get_cname(rtp));
2817
2818         a_line = ast_sdp_a_alloc("ssrc", attr_buffer);
2819         if (!a_line) {
2820                 return;
2821         }
2822         ast_sdp_m_add_a(m_line, a_line);
2823 }
2824
2825 /*!
2826  * \internal
2827  * \brief Create a declined m-line from a remote requested stream.
2828  * \since 15.0.0
2829  *
2830  * \details
2831  * Using the last received remote SDP create a declined stream
2832  * m-line for the requested stream.  The stream may be unsupported.
2833  *
2834  * \param sdp Our SDP under construction to append the declined stream.
2835  * \param sdp_state
2836  * \param stream_index Which remote SDP stream we are declining.
2837  *
2838  * \retval 0 on success.
2839  * \retval -1 on failure.
2840  */
2841 static int sdp_add_m_from_declined_remote_stream(struct ast_sdp *sdp,
2842         const struct ast_sdp_state *sdp_state, int stream_index)
2843 {
2844         const struct ast_sdp_m_line *m_line_remote;
2845         struct ast_sdp_m_line *m_line;
2846         int idx;
2847
2848         ast_assert(sdp && sdp_state && sdp_state->remote_sdp);
2849         ast_assert(stream_index < ast_sdp_get_m_count(sdp_state->remote_sdp));
2850
2851         /*
2852          * The only way we can generate a declined unsupported stream
2853          * m-line is if the remote offered it to us.
2854          */
2855         m_line_remote = ast_sdp_get_m(sdp_state->remote_sdp, stream_index);
2856
2857         /* Copy remote SDP stream m-line except for port number. */
2858         m_line = ast_sdp_m_alloc(m_line_remote->type, 0, m_line_remote->port_count,
2859                 m_line_remote->proto, NULL);
2860         if (!m_line) {
2861                 return -1;
2862         }
2863
2864         /* Copy any m-line payload strings from the remote SDP */
2865         for (idx = 0; idx < ast_sdp_m_get_payload_count(m_line_remote); ++idx) {
2866                 const struct ast_sdp_payload *payload_remote;
2867                 struct ast_sdp_payload *payload;
2868
2869                 payload_remote = ast_sdp_m_get_payload(m_line_remote, idx);
2870                 payload = ast_sdp_payload_alloc(payload_remote->fmt);
2871                 if (!payload) {
2872                         ast_sdp_m_free(m_line);
2873                         return -1;
2874                 }
2875                 if (ast_sdp_m_add_payload(m_line, payload)) {
2876                         ast_sdp_payload_free(payload);
2877                         ast_sdp_m_free(m_line);
2878                         return -1;
2879                 }
2880         }
2881
2882         if (ast_sdp_add_m(sdp, m_line)) {
2883                 ast_sdp_m_free(m_line);
2884                 return -1;
2885         }
2886
2887         return 0;
2888 }
2889
2890 /*!
2891  * \internal
2892  * \brief Create a declined m-line for our SDP stream.
2893  * \since 15.0.0
2894  *
2895  * \param sdp Our SDP under construction to append the declined stream.
2896  * \param sdp_state
2897  * \param type Stream type we are declining.
2898  * \param stream_index Which remote SDP stream we are declining.
2899  *
2900  * \retval 0 on success.
2901  * \retval -1 on failure.
2902  */
2903 static int sdp_add_m_from_declined_stream(struct ast_sdp *sdp,
2904         const struct ast_sdp_state *sdp_state, enum ast_media_type type, int stream_index)
2905 {
2906         struct ast_sdp_m_line *m_line;
2907         const char *proto;
2908         const char *fmt;
2909         struct ast_sdp_payload *payload;
2910
2911         if (sdp_state->role == SDP_ROLE_ANSWERER) {
2912                 /* We are declining the remote stream or it is still declined. */
2913                 return sdp_add_m_from_declined_remote_stream(sdp, sdp_state, stream_index);
2914         }
2915
2916         /* Send declined remote stream in our offer if the type matches. */
2917         if (sdp_state->remote_sdp
2918                 && stream_index < ast_sdp_get_m_count(sdp_state->remote_sdp)) {
2919                 if (!sdp_is_stream_type_supported(type)
2920                         || !strcasecmp(ast_sdp_get_m(sdp_state->remote_sdp, stream_index)->type,
2921                                 ast_codec_media_type2str(type))) {
2922                         /* Stream is still declined */
2923                         return sdp_add_m_from_declined_remote_stream(sdp, sdp_state, stream_index);
2924                 }
2925         }
2926
2927         /* Build a new declined stream in our offer. */
2928         switch (type) {
2929         case AST_MEDIA_TYPE_AUDIO:
2930         case AST_MEDIA_TYPE_VIDEO:
2931                 proto = "RTP/AVP";
2932                 break;
2933         case AST_MEDIA_TYPE_IMAGE:
2934                 proto = "udptl";
2935                 break;
2936         default:
2937                 /* Stream type not supported */
2938                 ast_assert(0);
2939                 return -1;
2940         }
2941         m_line = ast_sdp_m_alloc(ast_codec_media_type2str(type), 0, 1, proto, NULL);
2942         if (!m_line) {
2943                 return -1;
2944         }
2945
2946         /* Add a dummy static payload type */
2947         switch (type) {
2948         case AST_MEDIA_TYPE_AUDIO:
2949                 fmt = "0"; /* ulaw */
2950                 break;
2951         case AST_MEDIA_TYPE_VIDEO:
2952                 fmt = "31"; /* H.261 */
2953                 break;
2954         case AST_MEDIA_TYPE_IMAGE:
2955                 fmt = "t38"; /* T.38 */
2956                 break;
2957         default:
2958                 /* Stream type not supported */
2959                 ast_assert(0);
2960                 ast_sdp_m_free(m_line);
2961                 return -1;
2962         }
2963         payload = ast_sdp_payload_alloc(fmt);
2964         if (!payload || ast_sdp_m_add_payload(m_line, payload)) {
2965                 ast_sdp_payload_free(payload);
2966                 ast_sdp_m_free(m_line);
2967                 return -1;
2968         }
2969
2970         if (ast_sdp_add_m(sdp, m_line)) {
2971                 ast_sdp_m_free(m_line);
2972                 return -1;
2973         }
2974
2975         return 0;
2976 }
2977
2978 static int sdp_add_m_from_rtp_stream(struct ast_sdp *sdp, const struct ast_sdp_state *sdp_state,
2979         const struct sdp_state_capabilities *capabilities, int stream_index)
2980 {
2981         struct ast_stream *stream;
2982         struct ast_sdp_m_line *m_line;
2983         struct ast_format_cap *caps;
2984         int i;
2985         int rtp_code;
2986         int rtp_port;
2987         int min_packet_size = 0;
2988         int max_packet_size = 0;
2989         enum ast_media_type media_type;
2990         char tmp[64];
2991         struct sdp_state_stream *stream_state;
2992         struct ast_rtp_instance *rtp;
2993         struct ast_sdp_a_line *a_line;
2994         const struct ast_sdp_options *options;
2995         const char *direction;
2996
2997         stream = ast_stream_topology_get_stream(capabilities->topology, stream_index);
2998
2999         ast_assert(sdp && sdp_state && stream);
3000
3001         options = sdp_state->options;
3002         caps = ast_stream_get_formats(stream);
3003
3004         stream_state = AST_VECTOR_GET(&capabilities->streams, stream_index);
3005         if (stream_state->rtp && caps && ast_format_cap_count(caps)
3006                 && AST_STREAM_STATE_REMOVED != ast_stream_get_state(stream)) {
3007                 rtp = stream_state->rtp->instance;
3008         } else {
3009                 /* This is a disabled stream */
3010                 rtp = NULL;
3011         }
3012
3013         if (rtp) {
3014                 struct ast_sockaddr address_rtp;
3015
3016                 if (sdp_state_stream_get_connection_address(sdp_state, stream_state, &address_rtp)) {
3017                         return -1;
3018                 }
3019                 rtp_port = ast_sockaddr_port(&address_rtp);
3020         } else {
3021                 rtp_port = 0;
3022         }
3023
3024         media_type = ast_stream_get_type(stream);
3025         if (!rtp_port) {
3026                 /* Declined/disabled stream */
3027                 return sdp_add_m_from_declined_stream(sdp, sdp_state, media_type, stream_index);
3028         }
3029
3030         /* Stream is not declined/disabled */
3031         m_line = ast_sdp_m_alloc(ast_codec_media_type2str(media_type), rtp_port, 1,
3032                 options->encryption != AST_SDP_ENCRYPTION_DISABLED ? "RTP/SAVP" : "RTP/AVP",
3033                 NULL);
3034         if (!m_line) {
3035                 return -1;
3036         }
3037
3038         for (i = 0; i < ast_format_cap_count(caps); i++) {
3039                 struct ast_format *format = ast_format_cap_get_format(caps, i);
3040
3041                 rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(rtp), 1,
3042                         format, 0);
3043                 if (rtp_code == -1) {
3044                         ast_log(LOG_WARNING,"Unable to get rtp codec payload code for %s\n",
3045                                 ast_format_get_name(format));
3046                         ao2_ref(format, -1);
3047                         continue;
3048                 }
3049
3050                 if (ast_sdp_m_add_format(m_line, options, rtp_code, 1, format, 0)) {
3051                         ast_sdp_m_free(m_line);
3052                         ao2_ref(format, -1);
3053                         return -1;
3054                 }
3055
3056                 if (ast_format_get_maximum_ms(format)
3057                         && ((ast_format_get_maximum_ms(format) < max_packet_size)
3058                                 || !max_packet_size)) {
3059                         max_packet_size = ast_format_get_maximum_ms(format);
3060                 }
3061
3062                 ao2_ref(format, -1);
3063         }
3064
3065         if (media_type != AST_MEDIA_TYPE_VIDEO
3066                 && (options->dtmf == AST_SDP_DTMF_RFC_4733 || options->dtmf == AST_SDP_DTMF_AUTO)) {
3067                 i = AST_RTP_DTMF;
3068                 rtp_code = ast_rtp_codecs_payload_code(
3069                         ast_rtp_instance_get_codecs(rtp), 0, NULL, i);
3070                 if (-1 < rtp_code) {
3071                         if (ast_sdp_m_add_format(m_line, options, rtp_code, 0, NULL, i)) {
3072                                 ast_sdp_m_free(m_line);
3073                                 return -1;
3074                         }
3075
3076                         snprintf(tmp, sizeof(tmp), "%d 0-16", rtp_code);
3077                         a_line = ast_sdp_a_alloc("fmtp", tmp);
3078                         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
3079                                 ast_sdp_a_free(a_line);
3080                                 ast_sdp_m_free(m_line);
3081                                 return -1;
3082                         }
3083                 }
3084         }
3085
3086         /* If ptime is set add it as an attribute */
3087         min_packet_size = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(rtp));
3088         if (!min_packet_size) {
3089                 min_packet_size = ast_format_cap_get_framing(caps);
3090         }
3091         if (min_packet_size) {
3092                 snprintf(tmp, sizeof(tmp), "%d", min_packet_size);
3093
3094                 a_line = ast_sdp_a_alloc("ptime", tmp);
3095                 if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
3096                         ast_sdp_a_free(a_line);
3097                         ast_sdp_m_free(m_line);
3098                         return -1;
3099                 }
3100         }
3101
3102         if (max_packet_size) {
3103                 snprintf(tmp, sizeof(tmp), "%d", max_packet_size);
3104                 a_line = ast_sdp_a_alloc("maxptime", tmp);
3105                 if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
3106                         ast_sdp_a_free(a_line);
3107                         ast_sdp_m_free(m_line);
3108                         return -1;
3109                 }
3110         }
3111
3112         if (sdp_state->locally_held || stream_state->locally_held) {
3113                 if (stream_state->remotely_held) {
3114                         direction = "inactive";
3115                 } else {
3116                         direction = "sendonly";
3117                 }
3118         } else {
3119                 if (stream_state->remotely_held) {
3120                         direction = "recvonly";
3121                 } else {
3122                         /* Default is "sendrecv" */
3123                         direction = NULL;
3124                 }
3125         }
3126         if (direction) {
3127                 a_line = ast_sdp_a_alloc(direction, "");
3128                 if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
3129                         ast_sdp_a_free(a_line);
3130                         ast_sdp_m_free(m_line);
3131                         return -1;
3132                 }
3133         }
3134
3135         add_ssrc_attributes(m_line, options, rtp);
3136
3137         if (ast_sdp_add_m(sdp, m_line)) {
3138                 ast_sdp_m_free(m_line);
3139                 return -1;
3140         }
3141
3142         return 0;
3143 }
3144
3145 /*! \brief Get Max T.38 Transmission rate from T38 capabilities */
3146 static unsigned int t38_get_rate(enum ast_control_t38_rate rate)
3147 {
3148         switch (rate) {
3149         case AST_T38_RATE_2400:
3150                 return 2400;
3151         case AST_T38_RATE_4800:
3152                 return 4800;
3153         case AST_T38_RATE_7200:
3154                 return 7200;
3155         case AST_T38_RATE_9600:
3156                 return 9600;
3157         case AST_T38_RATE_12000:
3158                 return 12000;
3159         case AST_T38_RATE_14400:
3160                 return 14400;
3161         default:
3162                 return 0;
3163         }
3164 }
3165
3166 static int sdp_add_m_from_udptl_stream(struct ast_sdp *sdp, const struct ast_sdp_state *sdp_state,
3167         const struct sdp_state_capabilities *capabilities, int stream_index)
3168 {
3169         struct ast_stream *stream;
3170         struct ast_sdp_m_line *m_line;
3171         struct ast_sdp_payload *payload;
3172         enum ast_media_type media_type;
3173         char tmp[64];
3174         struct sdp_state_udptl *udptl;
3175         struct ast_sdp_a_line *a_line;
3176         struct sdp_state_stream *stream_state;
3177         int udptl_port;
3178
3179         stream = ast_stream_topology_get_stream(capabilities->topology, stream_index);
3180
3181         ast_assert(sdp && sdp_state && stream);
3182
3183         stream_state = AST_VECTOR_GET(&capabilities->streams, stream_index);
3184         if (stream_state->udptl
3185                 && AST_STREAM_STATE_REMOVED != ast_stream_get_state(stream)) {
3186                 udptl = stream_state->udptl;
3187         } else {
3188                 /* This is a disabled stream */
3189                 udptl = NULL;
3190         }
3191
3192         if (udptl) {
3193                 struct ast_sockaddr address_udptl;
3194
3195                 if (sdp_state_stream_get_connection_address(sdp_state, stream_state, &address_udptl)) {
3196                         return -1;
3197                 }
3198                 udptl_port = ast_sockaddr_port(&address_udptl);
3199         } else {
3200                 udptl_port = 0;
3201         }
3202
3203         media_type = ast_stream_get_type(stream);
3204         if (!udptl_port) {
3205                 /* Declined/disabled stream */
3206                 return sdp_add_m_from_declined_stream(sdp, sdp_state, media_type, stream_index);
3207         }
3208
3209         /* Stream is not declined/disabled */
3210         m_line = ast_sdp_m_alloc(ast_codec_media_type2str(media_type), udptl_port, 1,
3211                 "udptl", NULL);
3212         if (!m_line) {
3213                 return -1;
3214         }
3215
3216         payload = ast_sdp_payload_alloc("t38");
3217         if (!payload || ast_sdp_m_add_payload(m_line, payload)) {
3218                 ast_sdp_payload_free(payload);
3219                 ast_sdp_m_free(m_line);
3220                 return -1;
3221         }
3222
3223         snprintf(tmp, sizeof(tmp), "%u", stream_state->t38_local_params.version);
3224         a_line = ast_sdp_a_alloc("T38FaxVersion", tmp);
3225         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
3226                 ast_sdp_a_free(a_line);
3227                 ast_sdp_m_free(m_line);
3228                 return -1;
3229         }
3230
3231         snprintf(tmp, sizeof(tmp), "%u", t38_get_rate(stream_state->t38_local_params.rate));
3232         a_line = ast_sdp_a_alloc("T38FaxMaxBitRate", tmp);
3233         if (!a_line || ast_sdp_m_add_a(m_line, a_line)) {
3234                 ast_sdp_a_free(a_line);
323