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