rtp: Add support for RTP extension negotiation and abs-send-time.
[asterisk/asterisk.git] / res / res_pjsip_sdp_rtp.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  * Kevin Harwell <kharwell@digium.com>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*! \file
21  *
22  * \author Joshua Colp <jcolp@digium.com>
23  *
24  * \brief SIP SDP media stream handling
25  */
26
27 /*** MODULEINFO
28         <depend>pjproject</depend>
29         <depend>res_pjsip</depend>
30         <depend>res_pjsip_session</depend>
31         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 #include <pjsip.h>
37 #include <pjsip_ua.h>
38 #include <pjmedia.h>
39 #include <pjlib.h>
40
41 #include "asterisk/utils.h"
42 #include "asterisk/module.h"
43 #include "asterisk/format.h"
44 #include "asterisk/format_cap.h"
45 #include "asterisk/rtp_engine.h"
46 #include "asterisk/netsock2.h"
47 #include "asterisk/channel.h"
48 #include "asterisk/causes.h"
49 #include "asterisk/sched.h"
50 #include "asterisk/acl.h"
51 #include "asterisk/sdp_srtp.h"
52 #include "asterisk/dsp.h"
53 #include "asterisk/linkedlists.h"       /* for AST_LIST_NEXT */
54 #include "asterisk/stream.h"
55 #include "asterisk/format_cache.h"
56
57 #include "asterisk/res_pjsip.h"
58 #include "asterisk/res_pjsip_session.h"
59
60 /*! \brief Scheduler for RTCP purposes */
61 static struct ast_sched_context *sched;
62
63 /*! \brief Address for RTP */
64 static struct ast_sockaddr address_rtp;
65
66 static const char STR_AUDIO[] = "audio";
67 static const char STR_VIDEO[] = "video";
68
69 static int send_keepalive(const void *data)
70 {
71         struct ast_sip_session_media *session_media = (struct ast_sip_session_media *) data;
72         struct ast_rtp_instance *rtp = session_media->rtp;
73         int keepalive;
74         time_t interval;
75         int send_keepalive;
76
77         if (!rtp) {
78                 return 0;
79         }
80
81         keepalive = ast_rtp_instance_get_keepalive(rtp);
82
83         if (!ast_sockaddr_isnull(&session_media->direct_media_addr)) {
84                 ast_debug(3, "Not sending RTP keepalive on RTP instance %p since direct media is in use\n", rtp);
85                 return keepalive * 1000;
86         }
87
88         interval = time(NULL) - ast_rtp_instance_get_last_tx(rtp);
89         send_keepalive = interval >= keepalive;
90
91         ast_debug(3, "It has been %d seconds since RTP was last sent on instance %p. %sending keepalive\n",
92                         (int) interval, rtp, send_keepalive ? "S" : "Not s");
93
94         if (send_keepalive) {
95                 ast_rtp_instance_sendcng(rtp, 0);
96                 return keepalive * 1000;
97         }
98
99         return (keepalive - interval) * 1000;
100 }
101
102 /*! \brief Check whether RTP is being received or not */
103 static int rtp_check_timeout(const void *data)
104 {
105         struct ast_sip_session_media *session_media = (struct ast_sip_session_media *)data;
106         struct ast_rtp_instance *rtp = session_media->rtp;
107         int elapsed;
108         struct ast_channel *chan;
109
110         if (!rtp) {
111                 return 0;
112         }
113
114         elapsed = time(NULL) - ast_rtp_instance_get_last_rx(rtp);
115         if (elapsed < ast_rtp_instance_get_timeout(rtp)) {
116                 return (ast_rtp_instance_get_timeout(rtp) - elapsed) * 1000;
117         }
118
119         chan = ast_channel_get_by_name(ast_rtp_instance_get_channel_id(rtp));
120         if (!chan) {
121                 return 0;
122         }
123
124         ast_log(LOG_NOTICE, "Disconnecting channel '%s' for lack of RTP activity in %d seconds\n",
125                 ast_channel_name(chan), elapsed);
126
127         ast_channel_lock(chan);
128         ast_channel_hangupcause_set(chan, AST_CAUSE_REQUESTED_CHAN_UNAVAIL);
129         ast_channel_unlock(chan);
130
131         ast_softhangup(chan, AST_SOFTHANGUP_DEV);
132         ast_channel_unref(chan);
133
134         return 0;
135 }
136
137 /*!
138  * \brief Enable RTCP on an RTP session.
139  */
140 static void enable_rtcp(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
141         const struct pjmedia_sdp_media *remote_media)
142 {
143         enum ast_rtp_instance_rtcp rtcp_type;
144
145         if (session->endpoint->media.rtcp_mux && session_media->remote_rtcp_mux) {
146                 rtcp_type = AST_RTP_INSTANCE_RTCP_MUX;
147         } else {
148                 rtcp_type = AST_RTP_INSTANCE_RTCP_STANDARD;
149         }
150
151         ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RTCP, rtcp_type);
152 }
153
154 /*!
155  * \brief Enable an RTP extension on an RTP session.
156  */
157 static void enable_rtp_extension(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
158         enum ast_rtp_extension extension, enum ast_rtp_extension_direction direction,
159         const pjmedia_sdp_session *sdp)
160 {
161         int id = -1;
162
163         /* For a bundle group the local unique identifier space is shared across all streams within
164          * it.
165          */
166         if (session_media->bundle_group != -1) {
167                 int index;
168
169                 for (index = 0; index < sdp->media_count; ++index) {
170                         struct ast_sip_session_media *other_session_media;
171                         int other_id;
172
173                         if (index >= AST_VECTOR_SIZE(&session->pending_media_state->sessions)) {
174                                 break;
175                         }
176
177                         other_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, index);
178                         if (!other_session_media->rtp || other_session_media->bundle_group != session_media->bundle_group) {
179                                 continue;
180                         }
181
182                         other_id = ast_rtp_instance_extmap_get_id(other_session_media->rtp, extension);
183                         if (other_id == -1) {
184                                 /* Worst case we have to fall back to the highest available free local unique identifier
185                                  * for the bundle group.
186                                  */
187                                 other_id = ast_rtp_instance_extmap_count(other_session_media->rtp) + 1;
188                                 if (id < other_id) {
189                                         id = other_id;
190                                 }
191                                 continue;
192                         }
193
194                         id = other_id;
195                         break;
196                 }
197         }
198
199         ast_rtp_instance_extmap_enable(session_media->rtp, id, extension, direction);
200 }
201
202 /*! \brief Internal function which creates an RTP instance */
203 static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
204         const pjmedia_sdp_session *sdp)
205 {
206         struct ast_rtp_engine_ice *ice;
207         struct ast_sockaddr temp_media_address;
208         struct ast_sockaddr *media_address =  &address_rtp;
209
210         if (session->endpoint->media.bind_rtp_to_media_address && !ast_strlen_zero(session->endpoint->media.address)) {
211                 if (ast_sockaddr_parse(&temp_media_address, session->endpoint->media.address, 0)) {
212                         ast_debug(1, "Endpoint %s: Binding RTP media to %s\n",
213                                 ast_sorcery_object_get_id(session->endpoint),
214                                 session->endpoint->media.address);
215                         media_address = &temp_media_address;
216                 } else {
217                         ast_debug(1, "Endpoint %s: RTP media address invalid: %s\n",
218                                 ast_sorcery_object_get_id(session->endpoint),
219                                 session->endpoint->media.address);
220                 }
221         } else {
222                 struct ast_sip_transport *transport;
223
224                 transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport",
225                         session->endpoint->transport);
226                 if (transport) {
227                         struct ast_sip_transport_state *trans_state;
228
229                         trans_state = ast_sip_get_transport_state(ast_sorcery_object_get_id(transport));
230                         if (trans_state) {
231                                 char hoststr[PJ_INET6_ADDRSTRLEN];
232
233                                 pj_sockaddr_print(&trans_state->host, hoststr, sizeof(hoststr), 0);
234                                 if (ast_sockaddr_parse(&temp_media_address, hoststr, 0)) {
235                                         ast_debug(1, "Transport %s bound to %s: Using it for RTP media.\n",
236                                                 session->endpoint->transport, hoststr);
237                                         media_address = &temp_media_address;
238                                 } else {
239                                         ast_debug(1, "Transport %s bound to %s: Invalid for RTP media.\n",
240                                                 session->endpoint->transport, hoststr);
241                                 }
242                                 ao2_ref(trans_state, -1);
243                         }
244                         ao2_ref(transport, -1);
245                 }
246         }
247
248         if (!(session_media->rtp = ast_rtp_instance_new(session->endpoint->media.rtp.engine, sched, media_address, NULL))) {
249                 ast_log(LOG_ERROR, "Unable to create RTP instance using RTP engine '%s'\n", session->endpoint->media.rtp.engine);
250                 return -1;
251         }
252
253         ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_NAT, session->endpoint->media.rtp.symmetric);
254         ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_ASYMMETRIC_CODEC, session->endpoint->asymmetric_rtp_codec);
255
256         if (!session->endpoint->media.rtp.ice_support && (ice = ast_rtp_instance_get_ice(session_media->rtp))) {
257                 ice->stop(session_media->rtp);
258         }
259
260         if (session->dtmf == AST_SIP_DTMF_RFC_4733 || session->dtmf == AST_SIP_DTMF_AUTO || session->dtmf == AST_SIP_DTMF_AUTO_INFO) {
261                 ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_RFC2833);
262                 ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_DTMF, 1);
263         } else if (session->dtmf == AST_SIP_DTMF_INBAND) {
264                 ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND);
265         }
266
267         if (session_media->type == AST_MEDIA_TYPE_AUDIO &&
268                         (session->endpoint->media.tos_audio || session->endpoint->media.cos_audio)) {
269                 ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_audio,
270                                 session->endpoint->media.cos_audio, "SIP RTP Audio");
271         } else if (session_media->type == AST_MEDIA_TYPE_VIDEO) {
272                 ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RETRANS_RECV, session->endpoint->media.webrtc);
273                 ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RETRANS_SEND, session->endpoint->media.webrtc);
274                 ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_REMB, session->endpoint->media.webrtc);
275                 enable_rtp_extension(session, session_media, AST_RTP_EXTENSION_ABS_SEND_TIME, AST_RTP_EXTENSION_DIRECTION_SENDRECV, sdp);
276                 if (session->endpoint->media.tos_video || session->endpoint->media.cos_video) {
277                         ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_video,
278                                         session->endpoint->media.cos_video, "SIP RTP Video");
279                 }
280         }
281
282         ast_rtp_instance_set_last_rx(session_media->rtp, time(NULL));
283
284         return 0;
285 }
286
287 static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp_media *stream, struct ast_rtp_codecs *codecs,
288         struct ast_sip_session_media *session_media)
289 {
290         pjmedia_sdp_attr *attr;
291         pjmedia_sdp_rtpmap *rtpmap;
292         pjmedia_sdp_fmtp fmtp;
293         struct ast_format *format;
294         int i, num = 0, tel_event = 0;
295         char name[256];
296         char media[20];
297         char fmt_param[256];
298         enum ast_rtp_options options = session->endpoint->media.g726_non_standard ?
299                 AST_RTP_OPT_G726_NONSTANDARD : 0;
300
301         ast_rtp_codecs_payloads_initialize(codecs);
302
303         /* Iterate through provided formats */
304         for (i = 0; i < stream->desc.fmt_count; ++i) {
305                 /* The payload is kept as a string for things like t38 but for video it is always numerical */
306                 ast_rtp_codecs_payloads_set_m_type(codecs, NULL, pj_strtoul(&stream->desc.fmt[i]));
307                 /* Look for the optional rtpmap attribute */
308                 if (!(attr = pjmedia_sdp_media_find_attr2(stream, "rtpmap", &stream->desc.fmt[i]))) {
309                         continue;
310                 }
311
312                 /* Interpret the attribute as an rtpmap */
313                 if ((pjmedia_sdp_attr_to_rtpmap(session->inv_session->pool_prov, attr, &rtpmap)) != PJ_SUCCESS) {
314                         continue;
315                 }
316
317                 ast_copy_pj_str(name, &rtpmap->enc_name, sizeof(name));
318                 if (strcmp(name, "telephone-event") == 0) {
319                         tel_event++;
320                 }
321
322                 ast_copy_pj_str(media, (pj_str_t*)&stream->desc.media, sizeof(media));
323                 ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, NULL,
324                         pj_strtoul(&stream->desc.fmt[i]), media, name, options, rtpmap->clock_rate);
325                 /* Look for an optional associated fmtp attribute */
326                 if (!(attr = pjmedia_sdp_media_find_attr2(stream, "fmtp", &rtpmap->pt))) {
327                         continue;
328                 }
329
330                 if ((pjmedia_sdp_attr_get_fmtp(attr, &fmtp)) == PJ_SUCCESS) {
331                         ast_copy_pj_str(fmt_param, &fmtp.fmt, sizeof(fmt_param));
332                         if (sscanf(fmt_param, "%30d", &num) != 1) {
333                                 continue;
334                         }
335
336                         if ((format = ast_rtp_codecs_get_payload_format(codecs, num))) {
337                                 struct ast_format *format_parsed;
338
339                                 ast_copy_pj_str(fmt_param, &fmtp.fmt_param, sizeof(fmt_param));
340
341                                 format_parsed = ast_format_parse_sdp_fmtp(format, fmt_param);
342                                 if (format_parsed) {
343                                         ast_rtp_codecs_payload_replace_format(codecs, num, format_parsed);
344                                         ao2_ref(format_parsed, -1);
345                                 }
346
347                                 ao2_ref(format, -1);
348                         }
349                 }
350         }
351         if (!tel_event && (session->dtmf == AST_SIP_DTMF_AUTO)) {
352                 ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND);
353         }
354
355         if (session->dtmf == AST_SIP_DTMF_AUTO_INFO) {
356                 if  (tel_event) {
357                         ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_RFC2833);
358                 } else {
359                         ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_NONE);
360                 }
361         }
362
363
364         /* Get the packetization, if it exists */
365         if ((attr = pjmedia_sdp_media_find_attr2(stream, "ptime", NULL))) {
366                 unsigned long framing = pj_strtoul(pj_strltrim(&attr->value));
367                 if (framing && session->endpoint->media.rtp.use_ptime) {
368                         ast_rtp_codecs_set_framing(codecs, framing);
369                 }
370         }
371 }
372
373 static int set_caps(struct ast_sip_session *session,
374         struct ast_sip_session_media *session_media,
375         struct ast_sip_session_media *session_media_transport,
376         const struct pjmedia_sdp_media *stream,
377         int is_offer, struct ast_stream *asterisk_stream)
378 {
379         RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
380         RAII_VAR(struct ast_format_cap *, peer, NULL, ao2_cleanup);
381         RAII_VAR(struct ast_format_cap *, joint, NULL, ao2_cleanup);
382         RAII_VAR(struct ast_format_cap *, endpoint_caps, NULL, ao2_cleanup);
383         enum ast_media_type media_type = session_media->type;
384         struct ast_rtp_codecs codecs = AST_RTP_CODECS_NULL_INIT;
385         int fmts = 0;
386         int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) &&
387                 ast_format_cap_count(session->direct_media_cap);
388         int dsp_features = 0;
389
390         if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
391             !(peer = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
392             !(joint = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
393                 ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n",
394                         ast_codec_media_type2str(session_media->type));
395                 return -1;
396         }
397
398         /* get the endpoint capabilities */
399         if (direct_media_enabled) {
400                 ast_format_cap_get_compatible(session->endpoint->media.codecs, session->direct_media_cap, caps);
401         } else {
402                 ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, media_type);
403         }
404
405         /* get the capabilities on the peer */
406         get_codecs(session, stream, &codecs,  session_media);
407         ast_rtp_codecs_payload_formats(&codecs, peer, &fmts);
408
409         /* get the joint capabilities between peer and endpoint */
410         ast_format_cap_get_compatible(caps, peer, joint);
411         if (!ast_format_cap_count(joint)) {
412                 struct ast_str *usbuf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
413                 struct ast_str *thembuf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
414
415                 ast_rtp_codecs_payloads_destroy(&codecs);
416                 ast_log(LOG_NOTICE, "No joint capabilities for '%s' media stream between our configuration(%s) and incoming SDP(%s)\n",
417                         ast_codec_media_type2str(session_media->type),
418                         ast_format_cap_get_names(caps, &usbuf),
419                         ast_format_cap_get_names(peer, &thembuf));
420                 return -1;
421         }
422
423         if (is_offer) {
424                 /*
425                  * Setup rx payload type mapping to prefer the mapping
426                  * from the peer that the RFC says we SHOULD use.
427                  */
428                 ast_rtp_codecs_payloads_xover(&codecs, &codecs, NULL);
429         }
430         ast_rtp_codecs_payloads_copy(&codecs, ast_rtp_instance_get_codecs(session_media->rtp),
431                 session_media->rtp);
432
433         ast_stream_set_formats(asterisk_stream, joint);
434
435         /* If this is a bundled stream then apply the payloads to RTP instance acting as transport to prevent conflicts */
436         if (session_media_transport != session_media && session_media->bundled) {
437                 int index;
438
439                 for (index = 0; index < ast_format_cap_count(joint); ++index) {
440                         struct ast_format *format = ast_format_cap_get_format(joint, index);
441                         int rtp_code;
442
443                         /* Ensure this payload is in the bundle group transport codecs, this purposely doesn't check the return value for
444                          * things as the format is guaranteed to have a payload already.
445                          */
446                         rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 1, format, 0);
447                         ast_rtp_codecs_payload_set_rx(ast_rtp_instance_get_codecs(session_media_transport->rtp), rtp_code, format);
448
449                         ao2_ref(format, -1);
450                 }
451         }
452
453         if (session->channel && ast_sip_session_is_pending_stream_default(session, asterisk_stream)) {
454                 ast_channel_lock(session->channel);
455                 ast_format_cap_remove_by_type(caps, AST_MEDIA_TYPE_UNKNOWN);
456                 ast_format_cap_append_from_cap(caps, ast_channel_nativeformats(session->channel),
457                         AST_MEDIA_TYPE_UNKNOWN);
458                 ast_format_cap_remove_by_type(caps, media_type);
459
460                 if (session->endpoint->preferred_codec_only){
461                         struct ast_format *preferred_fmt = ast_format_cap_get_format(joint, 0);
462                         ast_format_cap_append(caps, preferred_fmt, 0);
463                         ao2_ref(preferred_fmt, -1);
464                 } else if (!session->endpoint->asymmetric_rtp_codec) {
465                         struct ast_format *best;
466                         /*
467                          * If we don't allow the sending codec to be changed on our side
468                          * then get the best codec from the joint capabilities of the media
469                          * type and use only that. This ensures the core won't start sending
470                          * out a format that we aren't currently sending.
471                          */
472
473                         best = ast_format_cap_get_best_by_type(joint, media_type);
474                         if (best) {
475                                 ast_format_cap_append(caps, best, ast_format_cap_get_framing(joint));
476                                 ao2_ref(best, -1);
477                         }
478                 } else {
479                         ast_format_cap_append_from_cap(caps, joint, media_type);
480                 }
481
482                 /*
483                  * Apply the new formats to the channel, potentially changing
484                  * raw read/write formats and translation path while doing so.
485                  */
486                 ast_channel_nativeformats_set(session->channel, caps);
487                 if (media_type == AST_MEDIA_TYPE_AUDIO) {
488                         ast_set_read_format(session->channel, ast_channel_readformat(session->channel));
489                         ast_set_write_format(session->channel, ast_channel_writeformat(session->channel));
490                 }
491
492                 if ( ((session->dtmf == AST_SIP_DTMF_AUTO) || (session->dtmf == AST_SIP_DTMF_AUTO_INFO) )
493                     && (ast_rtp_instance_dtmf_mode_get(session_media->rtp) == AST_RTP_DTMF_MODE_RFC2833)
494                     && (session->dsp)) {
495                         dsp_features = ast_dsp_get_features(session->dsp);
496                         dsp_features &= ~DSP_FEATURE_DIGIT_DETECT;
497                         if (dsp_features) {
498                                 ast_dsp_set_features(session->dsp, dsp_features);
499                         } else {
500                                 ast_dsp_free(session->dsp);
501                                 session->dsp = NULL;
502                         }
503                 }
504
505                 if (ast_channel_is_bridged(session->channel)) {
506                         ast_channel_set_unbridged_nolock(session->channel, 1);
507                 }
508
509                 ast_channel_unlock(session->channel);
510         }
511
512         ast_rtp_codecs_payloads_destroy(&codecs);
513         return 0;
514 }
515
516 static pjmedia_sdp_attr* generate_rtpmap_attr(struct ast_sip_session *session, pjmedia_sdp_media *media, pj_pool_t *pool,
517                                               int rtp_code, int asterisk_format, struct ast_format *format, int code)
518 {
519         extern pj_bool_t pjsip_use_compact_form;
520         pjmedia_sdp_rtpmap rtpmap;
521         pjmedia_sdp_attr *attr = NULL;
522         char tmp[64];
523         enum ast_rtp_options options = session->endpoint->media.g726_non_standard ?
524                 AST_RTP_OPT_G726_NONSTANDARD : 0;
525
526         snprintf(tmp, sizeof(tmp), "%d", rtp_code);
527         pj_strdup2(pool, &media->desc.fmt[media->desc.fmt_count++], tmp);
528
529         if (rtp_code <= AST_RTP_PT_LAST_STATIC && pjsip_use_compact_form) {
530                 return NULL;
531         }
532
533         rtpmap.pt = media->desc.fmt[media->desc.fmt_count - 1];
534         rtpmap.clock_rate = ast_rtp_lookup_sample_rate2(asterisk_format, format, code);
535         pj_strdup2(pool, &rtpmap.enc_name, ast_rtp_lookup_mime_subtype2(asterisk_format, format, code, options));
536         if (!pj_stricmp2(&rtpmap.enc_name, "opus")) {
537                 pj_cstr(&rtpmap.param, "2");
538         } else {
539                 pj_cstr(&rtpmap.param, NULL);
540         }
541
542         pjmedia_sdp_rtpmap_to_attr(pool, &rtpmap, &attr);
543
544         return attr;
545 }
546
547 static pjmedia_sdp_attr* generate_fmtp_attr(pj_pool_t *pool, struct ast_format *format, int rtp_code)
548 {
549         struct ast_str *fmtp0 = ast_str_alloca(256);
550         pj_str_t fmtp1;
551         pjmedia_sdp_attr *attr = NULL;
552         char *tmp;
553
554         ast_format_generate_sdp_fmtp(format, rtp_code, &fmtp0);
555         if (ast_str_strlen(fmtp0)) {
556                 tmp = ast_str_buffer(fmtp0) + ast_str_strlen(fmtp0) - 1;
557                 /* remove any carriage return line feeds */
558                 while (*tmp == '\r' || *tmp == '\n') --tmp;
559                 *++tmp = '\0';
560                 /* ast...generate gives us everything, just need value */
561                 tmp = strchr(ast_str_buffer(fmtp0), ':');
562                 if (tmp && tmp[1] != '\0') {
563                         fmtp1 = pj_str(tmp + 1);
564                 } else {
565                         fmtp1 = pj_str(ast_str_buffer(fmtp0));
566                 }
567                 attr = pjmedia_sdp_attr_create(pool, "fmtp", &fmtp1);
568         }
569         return attr;
570 }
571
572 /*! \brief Function which adds ICE attributes to a media stream */
573 static void add_ice_to_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media, pj_pool_t *pool, pjmedia_sdp_media *media,
574         unsigned int include_candidates)
575 {
576         struct ast_rtp_engine_ice *ice;
577         struct ao2_container *candidates;
578         const char *username, *password;
579         pj_str_t stmp;
580         pjmedia_sdp_attr *attr;
581         struct ao2_iterator it_candidates;
582         struct ast_rtp_engine_ice_candidate *candidate;
583
584         if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp))) {
585                 return;
586         }
587
588         if ((username = ice->get_ufrag(session_media->rtp))) {
589                 attr = pjmedia_sdp_attr_create(pool, "ice-ufrag", pj_cstr(&stmp, username));
590                 media->attr[media->attr_count++] = attr;
591         }
592
593         if ((password = ice->get_password(session_media->rtp))) {
594                 attr = pjmedia_sdp_attr_create(pool, "ice-pwd", pj_cstr(&stmp, password));
595                 media->attr[media->attr_count++] = attr;
596         }
597
598         if (!include_candidates) {
599                 return;
600         }
601
602         candidates = ice->get_local_candidates(session_media->rtp);
603         if (!candidates) {
604                 return;
605         }
606
607         it_candidates = ao2_iterator_init(candidates, 0);
608         for (; (candidate = ao2_iterator_next(&it_candidates)); ao2_ref(candidate, -1)) {
609                 struct ast_str *attr_candidate = ast_str_create(128);
610
611                 ast_str_set(&attr_candidate, -1, "%s %u %s %d %s ", candidate->foundation, candidate->id, candidate->transport,
612                                         candidate->priority, ast_sockaddr_stringify_addr_remote(&candidate->address));
613                 ast_str_append(&attr_candidate, -1, "%s typ ", ast_sockaddr_stringify_port(&candidate->address));
614
615                 switch (candidate->type) {
616                         case AST_RTP_ICE_CANDIDATE_TYPE_HOST:
617                                 ast_str_append(&attr_candidate, -1, "host");
618                                 break;
619                         case AST_RTP_ICE_CANDIDATE_TYPE_SRFLX:
620                                 ast_str_append(&attr_candidate, -1, "srflx");
621                                 break;
622                         case AST_RTP_ICE_CANDIDATE_TYPE_RELAYED:
623                                 ast_str_append(&attr_candidate, -1, "relay");
624                                 break;
625                 }
626
627                 if (!ast_sockaddr_isnull(&candidate->relay_address)) {
628                         ast_str_append(&attr_candidate, -1, " raddr %s rport", ast_sockaddr_stringify_addr_remote(&candidate->relay_address));
629                         ast_str_append(&attr_candidate, -1, " %s", ast_sockaddr_stringify_port(&candidate->relay_address));
630                 }
631
632                 attr = pjmedia_sdp_attr_create(pool, "candidate", pj_cstr(&stmp, ast_str_buffer(attr_candidate)));
633                 media->attr[media->attr_count++] = attr;
634
635                 ast_free(attr_candidate);
636         }
637
638         ao2_iterator_destroy(&it_candidates);
639         ao2_ref(candidates, -1);
640 }
641
642 /*! \brief Function which processes ICE attributes in an audio stream */
643 static void process_ice_attributes(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
644                                    const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream)
645 {
646         struct ast_rtp_engine_ice *ice;
647         const pjmedia_sdp_attr *attr;
648         char attr_value[256];
649         unsigned int attr_i;
650
651         /* If ICE support is not enabled or available exit early */
652         if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp))) {
653                 return;
654         }
655
656         attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-ufrag", NULL);
657         if (!attr) {
658                 attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-ufrag", NULL);
659         }
660         if (attr) {
661                 ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
662                 ice->set_authentication(session_media->rtp, attr_value, NULL);
663         } else {
664                 return;
665         }
666
667         attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-pwd", NULL);
668         if (!attr) {
669                 attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-pwd", NULL);
670         }
671         if (attr) {
672                 ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
673                 ice->set_authentication(session_media->rtp, NULL, attr_value);
674         } else {
675                 return;
676         }
677
678         if (pjmedia_sdp_media_find_attr2(remote_stream, "ice-lite", NULL)) {
679                 ice->ice_lite(session_media->rtp);
680         }
681
682         /* Find all of the candidates */
683         for (attr_i = 0; attr_i < remote_stream->attr_count; ++attr_i) {
684                 char foundation[33], transport[32], address[PJ_INET6_ADDRSTRLEN + 1], cand_type[6], relay_address[PJ_INET6_ADDRSTRLEN + 1] = "";
685                 unsigned int port, relay_port = 0;
686                 struct ast_rtp_engine_ice_candidate candidate = { 0, };
687
688                 attr = remote_stream->attr[attr_i];
689
690                 /* If this is not a candidate line skip it */
691                 if (pj_strcmp2(&attr->name, "candidate")) {
692                         continue;
693                 }
694
695                 ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
696
697                 if (sscanf(attr_value, "%32s %30u %31s %30u %46s %30u typ %5s %*s %23s %*s %30u", foundation, &candidate.id, transport,
698                         (unsigned *)&candidate.priority, address, &port, cand_type, relay_address, &relay_port) < 7) {
699                         /* Candidate did not parse properly */
700                         continue;
701                 }
702
703                 if (session->endpoint->media.rtcp_mux && session_media->remote_rtcp_mux && candidate.id > 1) {
704                         /* Remote side may have offered RTP and RTCP candidates. However, if we're using RTCP MUX,
705                          * then we should ignore RTCP candidates.
706                          */
707                         continue;
708                 }
709
710                 candidate.foundation = foundation;
711                 candidate.transport = transport;
712
713                 ast_sockaddr_parse(&candidate.address, address, PARSE_PORT_FORBID);
714                 ast_sockaddr_set_port(&candidate.address, port);
715
716                 if (!strcasecmp(cand_type, "host")) {
717                         candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_HOST;
718                 } else if (!strcasecmp(cand_type, "srflx")) {
719                         candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX;
720                 } else if (!strcasecmp(cand_type, "relay")) {
721                         candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED;
722                 } else {
723                         continue;
724                 }
725
726                 if (!ast_strlen_zero(relay_address)) {
727                         ast_sockaddr_parse(&candidate.relay_address, relay_address, PARSE_PORT_FORBID);
728                 }
729
730                 if (relay_port) {
731                         ast_sockaddr_set_port(&candidate.relay_address, relay_port);
732                 }
733
734                 ice->add_remote_candidate(session_media->rtp, &candidate);
735         }
736
737         ice->set_role(session_media->rtp, pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_TRUE ?
738                 AST_RTP_ICE_ROLE_CONTROLLING : AST_RTP_ICE_ROLE_CONTROLLED);
739         ice->start(session_media->rtp);
740 }
741
742 /*! \brief figure out if media stream has crypto lines for sdes */
743 static int media_stream_has_crypto(const struct pjmedia_sdp_media *stream)
744 {
745         int i;
746
747         for (i = 0; i < stream->attr_count; i++) {
748                 pjmedia_sdp_attr *attr;
749
750                 /* check the stream for the required crypto attribute */
751                 attr = stream->attr[i];
752                 if (pj_strcmp2(&attr->name, "crypto")) {
753                         continue;
754                 }
755
756                 return 1;
757         }
758
759         return 0;
760 }
761
762 /*! \brief figure out media transport encryption type from the media transport string */
763 static enum ast_sip_session_media_encryption get_media_encryption_type(pj_str_t transport,
764         const struct pjmedia_sdp_media *stream, unsigned int *optimistic)
765 {
766         RAII_VAR(char *, transport_str, ast_strndup(transport.ptr, transport.slen), ast_free);
767
768         *optimistic = 0;
769
770         if (!transport_str) {
771                 return AST_SIP_MEDIA_TRANSPORT_INVALID;
772         }
773         if (strstr(transport_str, "UDP/TLS")) {
774                 return AST_SIP_MEDIA_ENCRYPT_DTLS;
775         } else if (strstr(transport_str, "SAVP")) {
776                 return AST_SIP_MEDIA_ENCRYPT_SDES;
777         } else if (media_stream_has_crypto(stream)) {
778                 *optimistic = 1;
779                 return AST_SIP_MEDIA_ENCRYPT_SDES;
780         } else {
781                 return AST_SIP_MEDIA_ENCRYPT_NONE;
782         }
783 }
784
785 /*!
786  * \brief Checks whether the encryption offered in SDP is compatible with the endpoint's configuration
787  * \internal
788  *
789  * \param endpoint_encryption Media encryption configured for the endpoint
790  * \param stream pjmedia_sdp_media stream description
791  *
792  * \retval AST_SIP_MEDIA_TRANSPORT_INVALID on encryption mismatch
793  * \retval The encryption requested in the SDP
794  */
795 static enum ast_sip_session_media_encryption check_endpoint_media_transport(
796         struct ast_sip_endpoint *endpoint,
797         const struct pjmedia_sdp_media *stream)
798 {
799         enum ast_sip_session_media_encryption incoming_encryption;
800         char transport_end = stream->desc.transport.ptr[stream->desc.transport.slen - 1];
801         unsigned int optimistic;
802
803         if ((transport_end == 'F' && !endpoint->media.rtp.use_avpf)
804                 || (transport_end != 'F' && endpoint->media.rtp.use_avpf)) {
805                 return AST_SIP_MEDIA_TRANSPORT_INVALID;
806         }
807
808         incoming_encryption = get_media_encryption_type(stream->desc.transport, stream, &optimistic);
809
810         if (incoming_encryption == endpoint->media.rtp.encryption) {
811                 return incoming_encryption;
812         }
813
814         if (endpoint->media.rtp.force_avp ||
815                 endpoint->media.rtp.encryption_optimistic) {
816                 return incoming_encryption;
817         }
818
819         /* If an optimistic offer has been made but encryption is not enabled consider it as having
820          * no offer of crypto at all instead of invalid so the session proceeds.
821          */
822         if (optimistic) {
823                 return AST_SIP_MEDIA_ENCRYPT_NONE;
824         }
825
826         return AST_SIP_MEDIA_TRANSPORT_INVALID;
827 }
828
829 static int setup_srtp(struct ast_sip_session_media *session_media)
830 {
831         if (!session_media->srtp) {
832                 session_media->srtp = ast_sdp_srtp_alloc();
833                 if (!session_media->srtp) {
834                         return -1;
835                 }
836         }
837
838         if (!session_media->srtp->crypto) {
839                 session_media->srtp->crypto = ast_sdp_crypto_alloc();
840                 if (!session_media->srtp->crypto) {
841                         return -1;
842                 }
843         }
844
845         return 0;
846 }
847
848 static int setup_dtls_srtp(struct ast_sip_session *session,
849         struct ast_sip_session_media *session_media)
850 {
851         struct ast_rtp_engine_dtls *dtls;
852
853         if (!session->endpoint->media.rtp.dtls_cfg.enabled || !session_media->rtp) {
854                 return -1;
855         }
856
857         dtls = ast_rtp_instance_get_dtls(session_media->rtp);
858         if (!dtls) {
859                 return -1;
860         }
861
862         session->endpoint->media.rtp.dtls_cfg.suite = ((session->endpoint->media.rtp.srtp_tag_32) ? AST_AES_CM_128_HMAC_SHA1_32 : AST_AES_CM_128_HMAC_SHA1_80);
863         if (dtls->set_configuration(session_media->rtp, &session->endpoint->media.rtp.dtls_cfg)) {
864                 ast_log(LOG_ERROR, "Attempted to set an invalid DTLS-SRTP configuration on RTP instance '%p'\n",
865                         session_media->rtp);
866                 return -1;
867         }
868
869         if (setup_srtp(session_media)) {
870                 return -1;
871         }
872         return 0;
873 }
874
875 static void apply_dtls_attrib(struct ast_sip_session_media *session_media,
876         pjmedia_sdp_attr *attr)
877 {
878         struct ast_rtp_engine_dtls *dtls = ast_rtp_instance_get_dtls(session_media->rtp);
879         pj_str_t *value;
880
881         if (!attr->value.ptr || !dtls) {
882                 return;
883         }
884
885         value = pj_strtrim(&attr->value);
886
887         if (!pj_strcmp2(&attr->name, "setup")) {
888                 if (!pj_stricmp2(value, "active")) {
889                         dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_ACTIVE);
890                 } else if (!pj_stricmp2(value, "passive")) {
891                         dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_PASSIVE);
892                 } else if (!pj_stricmp2(value, "actpass")) {
893                         dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_ACTPASS);
894                 } else if (!pj_stricmp2(value, "holdconn")) {
895                         dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_HOLDCONN);
896                 } else {
897                         ast_log(LOG_WARNING, "Unsupported setup attribute value '%*s'\n", (int)value->slen, value->ptr);
898                 }
899         } else if (!pj_strcmp2(&attr->name, "connection")) {
900                 if (!pj_stricmp2(value, "new")) {
901                         dtls->reset(session_media->rtp);
902                 } else if (!pj_stricmp2(value, "existing")) {
903                         /* Do nothing */
904                 } else {
905                         ast_log(LOG_WARNING, "Unsupported connection attribute value '%*s'\n", (int)value->slen, value->ptr);
906                 }
907         } else if (!pj_strcmp2(&attr->name, "fingerprint")) {
908                 char hash_value[256], hash[32];
909                 char fingerprint_text[value->slen + 1];
910                 ast_copy_pj_str(fingerprint_text, value, sizeof(fingerprint_text));
911                         if (sscanf(fingerprint_text, "%31s %255s", hash, hash_value) == 2) {
912                         if (!strcasecmp(hash, "sha-1")) {
913                                 dtls->set_fingerprint(session_media->rtp, AST_RTP_DTLS_HASH_SHA1, hash_value);
914                         } else if (!strcasecmp(hash, "sha-256")) {
915                                 dtls->set_fingerprint(session_media->rtp, AST_RTP_DTLS_HASH_SHA256, hash_value);
916                         } else {
917                                 ast_log(LOG_WARNING, "Unsupported fingerprint hash type '%s'\n",
918                                 hash);
919                         }
920                 }
921         }
922 }
923
924 static int parse_dtls_attrib(struct ast_sip_session_media *session_media,
925         const struct pjmedia_sdp_session *sdp,
926         const struct pjmedia_sdp_media *stream)
927 {
928         int i;
929
930         for (i = 0; i < sdp->attr_count; i++) {
931                 apply_dtls_attrib(session_media, sdp->attr[i]);
932         }
933
934         for (i = 0; i < stream->attr_count; i++) {
935                 apply_dtls_attrib(session_media, stream->attr[i]);
936         }
937
938         ast_set_flag(session_media->srtp, AST_SRTP_CRYPTO_OFFER_OK);
939
940         return 0;
941 }
942
943 static int setup_sdes_srtp(struct ast_sip_session_media *session_media,
944         const struct pjmedia_sdp_media *stream)
945 {
946         int i;
947
948         for (i = 0; i < stream->attr_count; i++) {
949                 pjmedia_sdp_attr *attr;
950                 RAII_VAR(char *, crypto_str, NULL, ast_free);
951
952                 /* check the stream for the required crypto attribute */
953                 attr = stream->attr[i];
954                 if (pj_strcmp2(&attr->name, "crypto")) {
955                         continue;
956                 }
957
958                 crypto_str = ast_strndup(attr->value.ptr, attr->value.slen);
959                 if (!crypto_str) {
960                         return -1;
961                 }
962
963                 if (setup_srtp(session_media)) {
964                         return -1;
965                 }
966
967                 if (!ast_sdp_crypto_process(session_media->rtp, session_media->srtp, crypto_str)) {
968                         /* found a valid crypto attribute */
969                         return 0;
970                 }
971
972                 ast_debug(1, "Ignoring crypto offer with unsupported parameters: %s\n", crypto_str);
973         }
974
975         /* no usable crypto attributes found */
976         return -1;
977 }
978
979 static int setup_media_encryption(struct ast_sip_session *session,
980         struct ast_sip_session_media *session_media,
981         const struct pjmedia_sdp_session *sdp,
982         const struct pjmedia_sdp_media *stream)
983 {
984         switch (session_media->encryption) {
985         case AST_SIP_MEDIA_ENCRYPT_SDES:
986                 if (setup_sdes_srtp(session_media, stream)) {
987                         return -1;
988                 }
989                 break;
990         case AST_SIP_MEDIA_ENCRYPT_DTLS:
991                 if (setup_dtls_srtp(session, session_media)) {
992                         return -1;
993                 }
994                 if (parse_dtls_attrib(session_media, sdp, stream)) {
995                         return -1;
996                 }
997                 break;
998         case AST_SIP_MEDIA_TRANSPORT_INVALID:
999         case AST_SIP_MEDIA_ENCRYPT_NONE:
1000                 break;
1001         }
1002
1003         return 0;
1004 }
1005
1006 static void set_ice_components(struct ast_sip_session *session, struct ast_sip_session_media *session_media)
1007 {
1008         struct ast_rtp_engine_ice *ice;
1009
1010         ast_assert(session_media->rtp != NULL);
1011
1012         ice = ast_rtp_instance_get_ice(session_media->rtp);
1013         if (!session->endpoint->media.rtp.ice_support || !ice) {
1014                 return;
1015         }
1016
1017         if (session->endpoint->media.rtcp_mux && session_media->remote_rtcp_mux) {
1018                 /* We both support RTCP mux. Only one ICE component necessary */
1019                 ice->change_components(session_media->rtp, 1);
1020         } else {
1021                 /* They either don't support RTCP mux or we don't know if they do yet. */
1022                 ice->change_components(session_media->rtp, 2);
1023         }
1024 }
1025
1026 /*! \brief Function which adds ssrc attributes to a media stream */
1027 static void add_ssrc_to_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media, pj_pool_t *pool, pjmedia_sdp_media *media)
1028 {
1029         pj_str_t stmp;
1030         pjmedia_sdp_attr *attr;
1031         char tmp[128];
1032
1033         if (!session->endpoint->media.bundle || session_media->bundle_group == -1) {
1034                 return;
1035         }
1036
1037         snprintf(tmp, sizeof(tmp), "%u cname:%s", ast_rtp_instance_get_ssrc(session_media->rtp), ast_rtp_instance_get_cname(session_media->rtp));
1038         attr = pjmedia_sdp_attr_create(pool, "ssrc", pj_cstr(&stmp, tmp));
1039         media->attr[media->attr_count++] = attr;
1040 }
1041
1042 /*! \brief Function which processes ssrc attributes in a stream */
1043 static void process_ssrc_attributes(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
1044                                    const struct pjmedia_sdp_media *remote_stream)
1045 {
1046         int index;
1047
1048         if (!session->endpoint->media.bundle) {
1049                 return;
1050         }
1051
1052         for (index = 0; index < remote_stream->attr_count; ++index) {
1053                 pjmedia_sdp_attr *attr = remote_stream->attr[index];
1054                 char attr_value[pj_strlen(&attr->value) + 1];
1055                 char *ssrc_attribute_name, *ssrc_attribute_value = NULL;
1056                 unsigned int ssrc;
1057
1058                 /* We only care about ssrc attributes */
1059                 if (pj_strcmp2(&attr->name, "ssrc")) {
1060                         continue;
1061                 }
1062
1063                 ast_copy_pj_str(attr_value, &attr->value, sizeof(attr_value));
1064
1065                 if ((ssrc_attribute_name = strchr(attr_value, ' '))) {
1066                         /* This has an actual attribute */
1067                         *ssrc_attribute_name++ = '\0';
1068                         ssrc_attribute_value = strchr(ssrc_attribute_name, ':');
1069                         if (ssrc_attribute_value) {
1070                                 /* Values are actually optional according to the spec */
1071                                 *ssrc_attribute_value++ = '\0';
1072                         }
1073                 }
1074
1075                 if (sscanf(attr_value, "%30u", &ssrc) < 1) {
1076                         continue;
1077                 }
1078
1079                 /* If we are currently negotiating as a result of the remote side renegotiating then
1080                  * determine if the source for this stream has changed.
1081                  */
1082                 if (pjmedia_sdp_neg_get_state(session->inv_session->neg) == PJMEDIA_SDP_NEG_STATE_REMOTE_OFFER &&
1083                         session->active_media_state) {
1084                         struct ast_rtp_instance_stats stats = { 0, };
1085
1086                         if (!ast_rtp_instance_get_stats(session_media->rtp, &stats, AST_RTP_INSTANCE_STAT_REMOTE_SSRC) &&
1087                                 stats.remote_ssrc != ssrc) {
1088                                 session_media->changed = 1;
1089                         }
1090                 }
1091
1092                 ast_rtp_instance_set_remote_ssrc(session_media->rtp, ssrc);
1093         }
1094 }
1095
1096 static void add_msid_to_stream(struct ast_sip_session *session,
1097         struct ast_sip_session_media *session_media, pj_pool_t *pool, pjmedia_sdp_media *media,
1098         struct ast_stream *stream)
1099 {
1100         pj_str_t stmp;
1101         pjmedia_sdp_attr *attr;
1102         char msid[(AST_UUID_STR_LEN * 2) + 2];
1103
1104         if (!session->endpoint->media.webrtc) {
1105                 return;
1106         }
1107
1108         if (ast_strlen_zero(session_media->mslabel)) {
1109                 /* If this stream is grouped with another then use its media stream label if possible */
1110                 if (ast_stream_get_group(stream) != -1) {
1111                         struct ast_sip_session_media *group_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, ast_stream_get_group(stream));
1112
1113                         ast_copy_string(session_media->mslabel, group_session_media->mslabel, sizeof(session_media->mslabel));
1114                 }
1115
1116                 if (ast_strlen_zero(session_media->mslabel)) {
1117                         ast_uuid_generate_str(session_media->mslabel, sizeof(session_media->mslabel));
1118                 }
1119         }
1120
1121         if (ast_strlen_zero(session_media->label)) {
1122                 ast_uuid_generate_str(session_media->label, sizeof(session_media->label));
1123         }
1124
1125         snprintf(msid, sizeof(msid), "%s %s", session_media->mslabel, session_media->label);
1126         attr = pjmedia_sdp_attr_create(pool, "msid", pj_cstr(&stmp, msid));
1127         pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr);
1128 }
1129
1130 static void add_rtcp_fb_to_stream(struct ast_sip_session *session,
1131         struct ast_sip_session_media *session_media, pj_pool_t *pool, pjmedia_sdp_media *media)
1132 {
1133         pj_str_t stmp;
1134         pjmedia_sdp_attr *attr;
1135
1136         if (!session->endpoint->media.webrtc || session_media->type != AST_MEDIA_TYPE_VIDEO) {
1137                 return;
1138         }
1139
1140         /*
1141          * For now just automatically add it the stream even though it hasn't
1142          * necessarily been negotiated.
1143          */
1144         attr = pjmedia_sdp_attr_create(pool, "rtcp-fb", pj_cstr(&stmp, "* ccm fir"));
1145         pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr);
1146
1147         attr = pjmedia_sdp_attr_create(pool, "rtcp-fb", pj_cstr(&stmp, "* goog-remb"));
1148         pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr);
1149
1150         attr = pjmedia_sdp_attr_create(pool, "rtcp-fb", pj_cstr(&stmp, "* nack"));
1151         pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr);
1152 }
1153
1154 static void add_extmap_to_stream(struct ast_sip_session *session,
1155         struct ast_sip_session_media *session_media, pj_pool_t *pool, pjmedia_sdp_media *media)
1156 {
1157         int idx;
1158         char extmap_value[256];
1159
1160         if (!session->endpoint->media.webrtc || session_media->type != AST_MEDIA_TYPE_VIDEO) {
1161                 return;
1162         }
1163
1164         /* RTP extension local unique identifiers start at '1' */
1165         for (idx = 1; idx <= ast_rtp_instance_extmap_count(session_media->rtp); ++idx) {
1166                 enum ast_rtp_extension extension = ast_rtp_instance_extmap_get_extension(session_media->rtp, idx);
1167                 const char *direction_str = "";
1168                 pj_str_t stmp;
1169                 pjmedia_sdp_attr *attr;
1170
1171                 /* If this is an unsupported RTP extension we can't place it into the SDP */
1172                 if (extension == AST_RTP_EXTENSION_UNSUPPORTED) {
1173                         continue;
1174                 }
1175
1176                 switch (ast_rtp_instance_extmap_get_direction(session_media->rtp, idx)) {
1177                 case AST_RTP_EXTENSION_DIRECTION_SENDRECV:
1178                         /* Lack of a direction indicates sendrecv, so we leave it out */
1179                         direction_str = "";
1180                         break;
1181                 case AST_RTP_EXTENSION_DIRECTION_SENDONLY:
1182                         direction_str = "/sendonly";
1183                         break;
1184                 case AST_RTP_EXTENSION_DIRECTION_RECVONLY:
1185                         direction_str = "/recvonly";
1186                         break;
1187                 case AST_RTP_EXTENSION_DIRECTION_NONE:
1188                         /* It is impossible for a "none" direction extension to be negotiated but just in case
1189                          * we treat it as inactive.
1190                          */
1191                 case AST_RTP_EXTENSION_DIRECTION_INACTIVE:
1192                         direction_str = "/inactive";
1193                         break;
1194                 }
1195
1196                 snprintf(extmap_value, sizeof(extmap_value), "%d%s %s", idx, direction_str,
1197                         ast_rtp_instance_extmap_get_uri(session_media->rtp, idx));
1198                 attr = pjmedia_sdp_attr_create(pool, "extmap", pj_cstr(&stmp, extmap_value));
1199                 pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr);
1200         }
1201 }
1202
1203 /*! \brief Function which processes extmap attributes in a stream */
1204 static void process_extmap_attributes(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
1205                                    const struct pjmedia_sdp_media *remote_stream)
1206 {
1207         int index;
1208
1209         if (!session->endpoint->media.webrtc || session_media->type != AST_MEDIA_TYPE_VIDEO) {
1210                 return;
1211         }
1212
1213         ast_rtp_instance_extmap_clear(session_media->rtp);
1214
1215         for (index = 0; index < remote_stream->attr_count; ++index) {
1216                 pjmedia_sdp_attr *attr = remote_stream->attr[index];
1217                 char attr_value[pj_strlen(&attr->value) + 1];
1218                 char *uri;
1219                 int id;
1220                 char direction_str[10] = "";
1221                 char *attributes;
1222                 enum ast_rtp_extension_direction direction = AST_RTP_EXTENSION_DIRECTION_SENDRECV;
1223
1224                 /* We only care about extmap attributes */
1225                 if (pj_strcmp2(&attr->name, "extmap")) {
1226                         continue;
1227                 }
1228
1229                 ast_copy_pj_str(attr_value, &attr->value, sizeof(attr_value));
1230
1231                 /* Split the combined unique identifier and direction away from the URI and attributes for easier parsing */
1232                 uri = strchr(attr_value, ' ');
1233                 if (ast_strlen_zero(uri)) {
1234                         continue;
1235                 }
1236                 *uri++ = '\0';
1237
1238                 if ((sscanf(attr_value, "%30d%9s", &id, direction_str) < 1) || (id < 1)) {
1239                         /* We require at a minimum the unique identifier */
1240                         continue;
1241                 }
1242
1243                 /* Convert from the string to the internal representation */
1244                 if (!strcasecmp(direction_str, "/sendonly")) {
1245                         direction = AST_RTP_EXTENSION_DIRECTION_SENDONLY;
1246                 } else if (!strcasecmp(direction_str, "/recvonly")) {
1247                         direction = AST_RTP_EXTENSION_DIRECTION_RECVONLY;
1248                 } else if (!strcasecmp(direction_str, "/inactive")) {
1249                         direction = AST_RTP_EXTENSION_DIRECTION_INACTIVE;
1250                 }
1251
1252                 attributes = strchr(uri, ' ');
1253                 if (!ast_strlen_zero(attributes)) {
1254                         *attributes++ = '\0';
1255                 }
1256
1257                 ast_rtp_instance_extmap_negotiate(session_media->rtp, id, direction, uri, attributes);
1258         }
1259 }
1260
1261 /*! \brief Function which negotiates an incoming media stream */
1262 static int negotiate_incoming_sdp_stream(struct ast_sip_session *session,
1263         struct ast_sip_session_media *session_media, const pjmedia_sdp_session *sdp,
1264         int index, struct ast_stream *asterisk_stream)
1265 {
1266         char host[NI_MAXHOST];
1267         RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free);
1268         pjmedia_sdp_media *stream = sdp->media[index];
1269         struct ast_sip_session_media *session_media_transport;
1270         enum ast_media_type media_type = session_media->type;
1271         enum ast_sip_session_media_encryption encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
1272         int res;
1273
1274         /* If no type formats have been configured reject this stream */
1275         if (!ast_format_cap_has_type(session->endpoint->media.codecs, media_type)) {
1276                 ast_debug(3, "Endpoint has no codecs for media type '%s', declining stream\n",
1277                         ast_codec_media_type2str(session_media->type));
1278                 return 0;
1279         }
1280
1281         /* Ensure incoming transport is compatible with the endpoint's configuration */
1282         if (!session->endpoint->media.rtp.use_received_transport) {
1283                 encryption = check_endpoint_media_transport(session->endpoint, stream);
1284
1285                 if (encryption == AST_SIP_MEDIA_TRANSPORT_INVALID) {
1286                         return -1;
1287                 }
1288         }
1289
1290         ast_copy_pj_str(host, stream->conn ? &stream->conn->addr : &sdp->conn->addr, sizeof(host));
1291
1292         /* Ensure that the address provided is valid */
1293         if (ast_sockaddr_resolve(&addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC) <= 0) {
1294                 /* The provided host was actually invalid so we error out this negotiation */
1295                 return -1;
1296         }
1297
1298         /* Using the connection information create an appropriate RTP instance */
1299         if (!session_media->rtp && create_rtp(session, session_media, sdp)) {
1300                 return -1;
1301         }
1302
1303         process_ssrc_attributes(session, session_media, stream);
1304         process_extmap_attributes(session, session_media, stream);
1305         session_media_transport = ast_sip_session_media_get_transport(session, session_media);
1306
1307         if (session_media_transport == session_media || !session_media->bundled) {
1308                 /* If this media session is carrying actual traffic then set up those aspects */
1309                 session_media->remote_rtcp_mux = (pjmedia_sdp_media_find_attr2(stream, "rtcp-mux", NULL) != NULL);
1310                 set_ice_components(session, session_media);
1311
1312                 enable_rtcp(session, session_media, stream);
1313
1314                 res = setup_media_encryption(session, session_media, sdp, stream);
1315                 if (res) {
1316                         if (!session->endpoint->media.rtp.encryption_optimistic ||
1317                                 !pj_strncmp2(&stream->desc.transport, "RTP/SAVP", 8)) {
1318                                 /* If optimistic encryption is disabled and crypto should have been enabled
1319                                  * but was not this session must fail. This must also fail if crypto was
1320                                  * required in the offer but could not be set up.
1321                                  */
1322                                 return -1;
1323                         }
1324                         /* There is no encryption, sad. */
1325                         session_media->encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
1326                 }
1327
1328                 /* If we've been explicitly configured to use the received transport OR if
1329                  * encryption is on and crypto is present use the received transport.
1330                  * This is done in case of optimistic because it may come in as RTP/AVP or RTP/SAVP depending
1331                  * on the configuration of the remote endpoint (optimistic themselves or mandatory).
1332                  */
1333                 if ((session->endpoint->media.rtp.use_received_transport) ||
1334                         ((encryption == AST_SIP_MEDIA_ENCRYPT_SDES) && !res)) {
1335                         pj_strdup(session->inv_session->pool, &session_media->transport, &stream->desc.transport);
1336                 }
1337         } else {
1338                 /* This is bundled with another session, so mark it as such */
1339                 ast_rtp_instance_bundle(session_media->rtp, session_media_transport->rtp);
1340
1341                 enable_rtcp(session, session_media, stream);
1342         }
1343
1344         if (set_caps(session, session_media, session_media_transport, stream, 1, asterisk_stream)) {
1345                 return 0;
1346         }
1347
1348         return 1;
1349 }
1350
1351 static int add_crypto_to_stream(struct ast_sip_session *session,
1352         struct ast_sip_session_media *session_media,
1353         pj_pool_t *pool, pjmedia_sdp_media *media)
1354 {
1355         pj_str_t stmp;
1356         pjmedia_sdp_attr *attr;
1357         enum ast_rtp_dtls_hash hash;
1358         const char *crypto_attribute;
1359         struct ast_rtp_engine_dtls *dtls;
1360         struct ast_sdp_srtp *tmp;
1361         static const pj_str_t STR_NEW = { "new", 3 };
1362         static const pj_str_t STR_EXISTING = { "existing", 8 };
1363         static const pj_str_t STR_ACTIVE = { "active", 6 };
1364         static const pj_str_t STR_PASSIVE = { "passive", 7 };
1365         static const pj_str_t STR_ACTPASS = { "actpass", 7 };
1366         static const pj_str_t STR_HOLDCONN = { "holdconn", 8 };
1367         enum ast_rtp_dtls_setup setup;
1368
1369         switch (session_media->encryption) {
1370         case AST_SIP_MEDIA_ENCRYPT_NONE:
1371         case AST_SIP_MEDIA_TRANSPORT_INVALID:
1372                 break;
1373         case AST_SIP_MEDIA_ENCRYPT_SDES:
1374                 if (!session_media->srtp) {
1375                         session_media->srtp = ast_sdp_srtp_alloc();
1376                         if (!session_media->srtp) {
1377                                 return -1;
1378                         }
1379                 }
1380
1381                 tmp = session_media->srtp;
1382
1383                 do {
1384                         crypto_attribute = ast_sdp_srtp_get_attrib(tmp,
1385                                 0 /* DTLS running? No */,
1386                                 session->endpoint->media.rtp.srtp_tag_32 /* 32 byte tag length? */);
1387                         if (!crypto_attribute) {
1388                                 /* No crypto attribute to add, bad news */
1389                                 return -1;
1390                         }
1391
1392                         attr = pjmedia_sdp_attr_create(pool, "crypto",
1393                                 pj_cstr(&stmp, crypto_attribute));
1394                         media->attr[media->attr_count++] = attr;
1395                 } while ((tmp = AST_LIST_NEXT(tmp, sdp_srtp_list)));
1396
1397                 break;
1398         case AST_SIP_MEDIA_ENCRYPT_DTLS:
1399                 if (setup_dtls_srtp(session, session_media)) {
1400                         return -1;
1401                 }
1402
1403                 dtls = ast_rtp_instance_get_dtls(session_media->rtp);
1404                 if (!dtls) {
1405                         return -1;
1406                 }
1407
1408                 switch (dtls->get_connection(session_media->rtp)) {
1409                 case AST_RTP_DTLS_CONNECTION_NEW:
1410                         attr = pjmedia_sdp_attr_create(pool, "connection", &STR_NEW);
1411                         media->attr[media->attr_count++] = attr;
1412                         break;
1413                 case AST_RTP_DTLS_CONNECTION_EXISTING:
1414                         attr = pjmedia_sdp_attr_create(pool, "connection", &STR_EXISTING);
1415                         media->attr[media->attr_count++] = attr;
1416                         break;
1417                 default:
1418                         break;
1419                 }
1420
1421                 /* If this is an answer we need to use our current state, if it's an offer we need to use
1422                  * the configured value.
1423                  */
1424                 if (session->inv_session->neg
1425                         && pjmedia_sdp_neg_get_state(session->inv_session->neg) != PJMEDIA_SDP_NEG_STATE_DONE) {
1426                         setup = dtls->get_setup(session_media->rtp);
1427                 } else {
1428                         setup = session->endpoint->media.rtp.dtls_cfg.default_setup;
1429                 }
1430
1431                 switch (setup) {
1432                 case AST_RTP_DTLS_SETUP_ACTIVE:
1433                         attr = pjmedia_sdp_attr_create(pool, "setup", &STR_ACTIVE);
1434                         media->attr[media->attr_count++] = attr;
1435                         break;
1436                 case AST_RTP_DTLS_SETUP_PASSIVE:
1437                         attr = pjmedia_sdp_attr_create(pool, "setup", &STR_PASSIVE);
1438                         media->attr[media->attr_count++] = attr;
1439                         break;
1440                 case AST_RTP_DTLS_SETUP_ACTPASS:
1441                         attr = pjmedia_sdp_attr_create(pool, "setup", &STR_ACTPASS);
1442                         media->attr[media->attr_count++] = attr;
1443                         break;
1444                 case AST_RTP_DTLS_SETUP_HOLDCONN:
1445                         attr = pjmedia_sdp_attr_create(pool, "setup", &STR_HOLDCONN);
1446                         break;
1447                 default:
1448                         break;
1449                 }
1450
1451                 hash = dtls->get_fingerprint_hash(session_media->rtp);
1452                 crypto_attribute = dtls->get_fingerprint(session_media->rtp);
1453                 if (crypto_attribute && (hash == AST_RTP_DTLS_HASH_SHA1 || hash == AST_RTP_DTLS_HASH_SHA256)) {
1454                         RAII_VAR(struct ast_str *, fingerprint, ast_str_create(64), ast_free);
1455                         if (!fingerprint) {
1456                                 return -1;
1457                         }
1458
1459                         if (hash == AST_RTP_DTLS_HASH_SHA1) {
1460                                 ast_str_set(&fingerprint, 0, "SHA-1 %s", crypto_attribute);
1461                         } else {
1462                                 ast_str_set(&fingerprint, 0, "SHA-256 %s", crypto_attribute);
1463                         }
1464
1465                         attr = pjmedia_sdp_attr_create(pool, "fingerprint", pj_cstr(&stmp, ast_str_buffer(fingerprint)));
1466                         media->attr[media->attr_count++] = attr;
1467                 }
1468                 break;
1469         }
1470
1471         return 0;
1472 }
1473
1474 /*! \brief Function which creates an outgoing stream */
1475 static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
1476                                       struct pjmedia_sdp_session *sdp, const struct pjmedia_sdp_session *remote, struct ast_stream *stream)
1477 {
1478         pj_pool_t *pool = session->inv_session->pool_prov;
1479         static const pj_str_t STR_RTP_AVP = { "RTP/AVP", 7 };
1480         static const pj_str_t STR_IN = { "IN", 2 };
1481         static const pj_str_t STR_IP4 = { "IP4", 3};
1482         static const pj_str_t STR_IP6 = { "IP6", 3};
1483         static const pj_str_t STR_SENDRECV = { "sendrecv", 8 };
1484         static const pj_str_t STR_SENDONLY = { "sendonly", 8 };
1485         pjmedia_sdp_media *media;
1486         const char *hostip = NULL;
1487         struct ast_sockaddr addr;
1488         char tmp[512];
1489         pj_str_t stmp;
1490         pjmedia_sdp_attr *attr;
1491         int index = 0;
1492         int noncodec = (session->dtmf == AST_SIP_DTMF_RFC_4733 || session->dtmf == AST_SIP_DTMF_AUTO || session->dtmf == AST_SIP_DTMF_AUTO_INFO) ? AST_RTP_DTMF : 0;
1493         int min_packet_size = 0, max_packet_size = 0;
1494         int rtp_code;
1495         RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
1496         enum ast_media_type media_type = session_media->type;
1497         struct ast_sip_session_media *session_media_transport;
1498         pj_sockaddr ip;
1499
1500         int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) &&
1501                 ast_format_cap_count(session->direct_media_cap);
1502
1503         media = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_media));
1504         if (!media) {
1505                 return -1;
1506         }
1507         pj_strdup2(pool, &media->desc.media, ast_codec_media_type2str(session_media->type));
1508
1509         /* If this is a removed (or declined) stream OR if no formats exist then construct a minimal stream in SDP */
1510         if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED || !ast_stream_get_formats(stream) ||
1511                 !ast_format_cap_count(ast_stream_get_formats(stream))) {
1512                 media->desc.port = 0;
1513                 media->desc.port_count = 1;
1514
1515                 if (remote && remote->media[ast_stream_get_position(stream)]) {
1516                         pjmedia_sdp_media *remote_media = remote->media[ast_stream_get_position(stream)];
1517                         int index;
1518
1519                         media->desc.transport = remote_media->desc.transport;
1520
1521                         /* Preserve existing behavior by copying the formats provided from the offer */
1522                         for (index = 0; index < remote_media->desc.fmt_count; ++index) {
1523                                 media->desc.fmt[index] = remote_media->desc.fmt[index];
1524                         }
1525                         media->desc.fmt_count = remote_media->desc.fmt_count;
1526                 } else {
1527                         /* This is actually an offer so put a dummy payload in that is ignored and sane transport */
1528                         media->desc.transport = STR_RTP_AVP;
1529                         pj_strdup2(pool, &media->desc.fmt[media->desc.fmt_count++], "32");
1530                 }
1531
1532                 sdp->media[sdp->media_count++] = media;
1533                 ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
1534
1535                 return 1;
1536         }
1537
1538         if (!session_media->rtp && create_rtp(session, session_media, sdp)) {
1539                 return -1;
1540         }
1541
1542         /* If this stream has not been bundled already it is new and we need to ensure there is no SSRC conflict */
1543         if (session_media->bundle_group != -1 && !session_media->bundled) {
1544                 for (index = 0; index < sdp->media_count; ++index) {
1545                         struct ast_sip_session_media *other_session_media;
1546
1547                         other_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, index);
1548                         if (!other_session_media->rtp || other_session_media->bundle_group != session_media->bundle_group) {
1549                                 continue;
1550                         }
1551
1552                         if (ast_rtp_instance_get_ssrc(session_media->rtp) == ast_rtp_instance_get_ssrc(other_session_media->rtp)) {
1553                                 ast_rtp_instance_change_source(session_media->rtp);
1554                                 /* Start the conflict check over again */
1555                                 index = -1;
1556                                 continue;
1557                         }
1558                 }
1559         }
1560
1561         session_media_transport = ast_sip_session_media_get_transport(session, session_media);
1562
1563         if (session_media_transport == session_media || !session_media->bundled) {
1564                 set_ice_components(session, session_media);
1565                 enable_rtcp(session, session_media, NULL);
1566
1567                 /* Crypto has to be added before setting the media transport so that SRTP is properly
1568                  * set up according to the configuration. This ends up changing the media transport.
1569                  */
1570                 if (add_crypto_to_stream(session, session_media, pool, media)) {
1571                         return -1;
1572                 }
1573
1574                 if (pj_strlen(&session_media->transport)) {
1575                         /* If a transport has already been specified use it */
1576                         media->desc.transport = session_media->transport;
1577                 } else {
1578                         media->desc.transport = pj_str(ast_sdp_get_rtp_profile(
1579                                 /* Optimistic encryption places crypto in the normal RTP/AVP profile */
1580                                 !session->endpoint->media.rtp.encryption_optimistic &&
1581                                         (session_media->encryption == AST_SIP_MEDIA_ENCRYPT_SDES),
1582                                 session_media->rtp, session->endpoint->media.rtp.use_avpf,
1583                                 session->endpoint->media.rtp.force_avp));
1584                 }
1585
1586                 media->conn = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_conn));
1587                 if (!media->conn) {
1588                         return -1;
1589                 }
1590
1591                 /* Add connection level details */
1592                 if (direct_media_enabled) {
1593                         hostip = ast_sockaddr_stringify_fmt(&session_media->direct_media_addr, AST_SOCKADDR_STR_ADDR);
1594                 } else if (ast_strlen_zero(session->endpoint->media.address)) {
1595                         hostip = ast_sip_get_host_ip_string(session->endpoint->media.rtp.ipv6 ? pj_AF_INET6() : pj_AF_INET());
1596                 } else {
1597                         hostip = session->endpoint->media.address;
1598                 }
1599
1600                 if (ast_strlen_zero(hostip)) {
1601                         ast_log(LOG_ERROR, "No local host IP available for stream %s\n",
1602                                 ast_codec_media_type2str(session_media->type));
1603                         return -1;
1604                 }
1605
1606                 media->conn->net_type = STR_IN;
1607                 /* Assume that the connection will use IPv4 until proven otherwise */
1608                 media->conn->addr_type = STR_IP4;
1609                 pj_strdup2(pool, &media->conn->addr, hostip);
1610
1611                 if ((pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &media->conn->addr, &ip) == PJ_SUCCESS) &&
1612                         (ip.addr.sa_family == pj_AF_INET6())) {
1613                         media->conn->addr_type = STR_IP6;
1614                 }
1615
1616                 /* Add ICE attributes and candidates */
1617                 add_ice_to_stream(session, session_media, pool, media, 1);
1618
1619                 ast_rtp_instance_get_local_address(session_media->rtp, &addr);
1620                 media->desc.port = direct_media_enabled ? ast_sockaddr_port(&session_media->direct_media_addr) : (pj_uint16_t) ast_sockaddr_port(&addr);
1621                 media->desc.port_count = 1;
1622         } else {
1623                 pjmedia_sdp_media *bundle_group_stream = sdp->media[session_media_transport->stream_num];
1624
1625                 /* As this is in a bundle group it shares the same details as the group instance */
1626                 media->desc.transport = bundle_group_stream->desc.transport;
1627                 media->conn = bundle_group_stream->conn;
1628                 media->desc.port = bundle_group_stream->desc.port;
1629
1630                 if (add_crypto_to_stream(session, session_media_transport, pool, media)) {
1631                         return -1;
1632                 }
1633
1634                 add_ice_to_stream(session, session_media_transport, pool, media, 0);
1635
1636                 enable_rtcp(session, session_media, NULL);
1637         }
1638
1639         if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
1640                 ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n",
1641                         ast_codec_media_type2str(session_media->type));
1642                 return -1;
1643         }
1644
1645         if (direct_media_enabled) {
1646                 ast_format_cap_get_compatible(session->endpoint->media.codecs, session->direct_media_cap, caps);
1647         } else {
1648                 ast_format_cap_append_from_cap(caps, ast_stream_get_formats(stream), media_type);
1649         }
1650
1651         for (index = 0; index < ast_format_cap_count(caps); ++index) {
1652                 struct ast_format *format = ast_format_cap_get_format(caps, index);
1653
1654                 if (ast_format_get_type(format) != media_type) {
1655                         ao2_ref(format, -1);
1656                         continue;
1657                 }
1658
1659                 /* If this stream is not a transport we need to use the transport codecs structure for payload management to prevent
1660                  * conflicts.
1661                  */
1662                 if (session_media_transport != session_media) {
1663                         if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media_transport->rtp), 1, format, 0)) == -1) {
1664                                 ast_log(LOG_WARNING,"Unable to get rtp codec payload code for %s\n", ast_format_get_name(format));
1665                                 ao2_ref(format, -1);
1666                                 continue;
1667                         }
1668                         /* Our instance has to match the payload number though */
1669                         ast_rtp_codecs_payload_set_rx(ast_rtp_instance_get_codecs(session_media->rtp), rtp_code, format);
1670                 } else {
1671                         if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 1, format, 0)) == -1) {
1672                                 ast_log(LOG_WARNING,"Unable to get rtp codec payload code for %s\n", ast_format_get_name(format));
1673                                 ao2_ref(format, -1);
1674                                 continue;
1675                         }
1676                 }
1677
1678                 if ((attr = generate_rtpmap_attr(session, media, pool, rtp_code, 1, format, 0))) {
1679                         media->attr[media->attr_count++] = attr;
1680                 }
1681
1682                 if ((attr = generate_fmtp_attr(pool, format, rtp_code))) {
1683                         media->attr[media->attr_count++] = attr;
1684                 }
1685
1686                 if (ast_format_get_maximum_ms(format) &&
1687                         ((ast_format_get_maximum_ms(format) < max_packet_size) || !max_packet_size)) {
1688                         max_packet_size = ast_format_get_maximum_ms(format);
1689                 }
1690                 ao2_ref(format, -1);
1691
1692                 if (media->desc.fmt_count == PJMEDIA_MAX_SDP_FMT) {
1693                         break;
1694                 }
1695         }
1696
1697         /* Add non-codec formats */
1698         if (ast_sip_session_is_pending_stream_default(session, stream) && media_type != AST_MEDIA_TYPE_VIDEO
1699                 && media->desc.fmt_count < PJMEDIA_MAX_SDP_FMT) {
1700                 for (index = 1LL; index <= AST_RTP_MAX; index <<= 1) {
1701                         if (!(noncodec & index)) {
1702                                 continue;
1703                         }
1704                         rtp_code = ast_rtp_codecs_payload_code(
1705                                 ast_rtp_instance_get_codecs(session_media->rtp), 0, NULL, index);
1706                         if (rtp_code == -1) {
1707                                 continue;
1708                         }
1709
1710                         if ((attr = generate_rtpmap_attr(session, media, pool, rtp_code, 0, NULL, index))) {
1711                                 media->attr[media->attr_count++] = attr;
1712                         }
1713
1714                         if (index == AST_RTP_DTMF) {
1715                                 snprintf(tmp, sizeof(tmp), "%d 0-16", rtp_code);
1716                                 attr = pjmedia_sdp_attr_create(pool, "fmtp", pj_cstr(&stmp, tmp));
1717                                 media->attr[media->attr_count++] = attr;
1718                         }
1719
1720                         if (media->desc.fmt_count == PJMEDIA_MAX_SDP_FMT) {
1721                                 break;
1722                         }
1723                 }
1724         }
1725
1726
1727         /* If no formats were actually added to the media stream don't add it to the SDP */
1728         if (!media->desc.fmt_count) {
1729                 return 1;
1730         }
1731
1732         /* If ptime is set add it as an attribute */
1733         min_packet_size = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(session_media->rtp));
1734         if (!min_packet_size) {
1735                 min_packet_size = ast_format_cap_get_framing(caps);
1736         }
1737         if (min_packet_size) {
1738                 snprintf(tmp, sizeof(tmp), "%d", min_packet_size);
1739                 attr = pjmedia_sdp_attr_create(pool, "ptime", pj_cstr(&stmp, tmp));
1740                 media->attr[media->attr_count++] = attr;
1741         }
1742
1743         if (max_packet_size) {
1744                 snprintf(tmp, sizeof(tmp), "%d", max_packet_size);
1745                 attr = pjmedia_sdp_attr_create(pool, "maxptime", pj_cstr(&stmp, tmp));
1746                 media->attr[media->attr_count++] = attr;
1747         }
1748
1749         /* Add the sendrecv attribute - we purposely don't keep track because pjmedia-sdp will automatically change our offer for us */
1750         attr = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_attr);
1751         attr->name = !session_media->locally_held ? STR_SENDRECV : STR_SENDONLY;
1752         media->attr[media->attr_count++] = attr;
1753
1754         /* If we've got rtcp-mux enabled, just unconditionally offer it in all SDPs */
1755         if (session->endpoint->media.rtcp_mux) {
1756                 attr = pjmedia_sdp_attr_create(pool, "rtcp-mux", NULL);
1757                 pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr);
1758         }
1759
1760         add_ssrc_to_stream(session, session_media, pool, media);
1761         add_msid_to_stream(session, session_media, pool, media, stream);
1762         add_rtcp_fb_to_stream(session, session_media, pool, media);
1763         add_extmap_to_stream(session, session_media, pool, media);
1764
1765         /* Add the media stream to the SDP */
1766         sdp->media[sdp->media_count++] = media;
1767
1768         return 1;
1769 }
1770
1771 static struct ast_frame *media_session_rtp_read_callback(struct ast_sip_session *session, struct ast_sip_session_media *session_media)
1772 {
1773         struct ast_frame *f;
1774
1775         if (!session_media->rtp) {
1776                 return &ast_null_frame;
1777         }
1778
1779         f = ast_rtp_instance_read(session_media->rtp, 0);
1780         if (!f) {
1781                 return NULL;
1782         }
1783
1784         ast_rtp_instance_set_last_rx(session_media->rtp, time(NULL));
1785
1786         return f;
1787 }
1788
1789 static struct ast_frame *media_session_rtcp_read_callback(struct ast_sip_session *session, struct ast_sip_session_media *session_media)
1790 {
1791         struct ast_frame *f;
1792
1793         if (!session_media->rtp) {
1794                 return &ast_null_frame;
1795         }
1796
1797         f = ast_rtp_instance_read(session_media->rtp, 1);
1798         if (!f) {
1799                 return NULL;
1800         }
1801
1802         ast_rtp_instance_set_last_rx(session_media->rtp, time(NULL));
1803
1804         return f;
1805 }
1806
1807 static int media_session_rtp_write_callback(struct ast_sip_session *session, struct ast_sip_session_media *session_media, struct ast_frame *frame)
1808 {
1809         if (!session_media->rtp) {
1810                 return 0;
1811         }
1812
1813         return ast_rtp_instance_write(session_media->rtp, frame);
1814 }
1815
1816 static int apply_negotiated_sdp_stream(struct ast_sip_session *session,
1817         struct ast_sip_session_media *session_media, const struct pjmedia_sdp_session *local,
1818         const struct pjmedia_sdp_session *remote, int index, struct ast_stream *asterisk_stream)
1819 {
1820         RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free);
1821         struct pjmedia_sdp_media *remote_stream = remote->media[index];
1822         enum ast_media_type media_type = session_media->type;
1823         char host[NI_MAXHOST];
1824         int res;
1825         struct ast_sip_session_media *session_media_transport;
1826
1827         if (!session->channel) {
1828                 return 1;
1829         }
1830
1831         /* Ensure incoming transport is compatible with the endpoint's configuration */
1832         if (!session->endpoint->media.rtp.use_received_transport &&
1833                 check_endpoint_media_transport(session->endpoint, remote_stream) == AST_SIP_MEDIA_TRANSPORT_INVALID) {
1834                 return -1;
1835         }
1836
1837         /* Create an RTP instance if need be */
1838         if (!session_media->rtp && create_rtp(session, session_media, local)) {
1839                 return -1;
1840         }
1841
1842         process_ssrc_attributes(session, session_media, remote_stream);
1843         process_extmap_attributes(session, session_media, remote_stream);
1844
1845         session_media_transport = ast_sip_session_media_get_transport(session, session_media);
1846
1847         if (session_media_transport == session_media || !session_media->bundled) {
1848                 session_media->remote_rtcp_mux = (pjmedia_sdp_media_find_attr2(remote_stream, "rtcp-mux", NULL) != NULL);
1849                 set_ice_components(session, session_media);
1850
1851                 enable_rtcp(session, session_media, remote_stream);
1852
1853                 res = setup_media_encryption(session, session_media, remote, remote_stream);
1854                 if (!session->endpoint->media.rtp.encryption_optimistic && res) {
1855                         /* If optimistic encryption is disabled and crypto should have been enabled but was not
1856                          * this session must fail.
1857                          */
1858                         return -1;
1859                 }
1860
1861                 if (!remote_stream->conn && !remote->conn) {
1862                         return 1;
1863                 }
1864
1865                 ast_copy_pj_str(host, remote_stream->conn ? &remote_stream->conn->addr : &remote->conn->addr, sizeof(host));
1866
1867                 /* Ensure that the address provided is valid */
1868                 if (ast_sockaddr_resolve(&addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC) <= 0) {
1869                         /* The provided host was actually invalid so we error out this negotiation */
1870                         return -1;
1871                 }
1872
1873                 /* Apply connection information to the RTP instance */
1874                 ast_sockaddr_set_port(addrs, remote_stream->desc.port);
1875                 ast_rtp_instance_set_remote_address(session_media->rtp, addrs);
1876
1877                 ast_sip_session_media_set_write_callback(session, session_media, media_session_rtp_write_callback);
1878                 ast_sip_session_media_add_read_callback(session, session_media, ast_rtp_instance_fd(session_media->rtp, 0),
1879                         media_session_rtp_read_callback);
1880                 if (!session->endpoint->media.rtcp_mux || !session_media->remote_rtcp_mux) {
1881                         ast_sip_session_media_add_read_callback(session, session_media, ast_rtp_instance_fd(session_media->rtp, 1),
1882                                 media_session_rtcp_read_callback);
1883                 }
1884
1885                 /* If ICE support is enabled find all the needed attributes */
1886                 process_ice_attributes(session, session_media, remote, remote_stream);
1887         } else {
1888                 /* This is bundled with another session, so mark it as such */
1889                 ast_rtp_instance_bundle(session_media->rtp, session_media_transport->rtp);
1890                 ast_sip_session_media_set_write_callback(session, session_media, media_session_rtp_write_callback);
1891                 enable_rtcp(session, session_media, remote_stream);
1892         }
1893
1894         if (set_caps(session, session_media, session_media_transport, remote_stream, 0, asterisk_stream)) {
1895                 return 1;
1896         }
1897
1898         /* Set the channel uniqueid on the RTP instance now that it is becoming active */
1899         ast_channel_lock(session->channel);
1900         ast_rtp_instance_set_channel_id(session_media->rtp, ast_channel_uniqueid(session->channel));
1901         ast_channel_unlock(session->channel);
1902
1903         /* Ensure the RTP instance is active */
1904         ast_rtp_instance_set_stream_num(session_media->rtp, ast_stream_get_position(asterisk_stream));
1905         ast_rtp_instance_activate(session_media->rtp);
1906
1907         /* audio stream handles music on hold */
1908         if (media_type != AST_MEDIA_TYPE_AUDIO) {
1909                 if ((pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_FALSE)
1910                         && (session->inv_session->state == PJSIP_INV_STATE_CONFIRMED)) {
1911                         ast_queue_control(session->channel, AST_CONTROL_UPDATE_RTP_PEER);
1912                 }
1913                 return 1;
1914         }
1915
1916         if (ast_sockaddr_isnull(addrs) ||
1917                 ast_sockaddr_is_any(addrs) ||
1918                 pjmedia_sdp_media_find_attr2(remote_stream, "sendonly", NULL) ||
1919                 pjmedia_sdp_media_find_attr2(remote_stream, "inactive", NULL)) {
1920                 if (!session_media->remotely_held) {
1921                         /* The remote side has put us on hold */
1922                         ast_queue_hold(session->channel, session->endpoint->mohsuggest);
1923                         ast_rtp_instance_stop(session_media->rtp);
1924                         ast_queue_frame(session->channel, &ast_null_frame);
1925                         session_media->remotely_held = 1;
1926                 }
1927         } else if (session_media->remotely_held) {
1928                 /* The remote side has taken us off hold */
1929                 ast_queue_unhold(session->channel);
1930                 ast_queue_frame(session->channel, &ast_null_frame);
1931                 session_media->remotely_held = 0;
1932         } else if ((pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_FALSE)
1933                 && (session->inv_session->state == PJSIP_INV_STATE_CONFIRMED)) {
1934                 ast_queue_control(session->channel, AST_CONTROL_UPDATE_RTP_PEER);
1935         }
1936
1937         /* This purposely resets the encryption to the configured in case it gets added later */
1938         session_media->encryption = session->endpoint->media.rtp.encryption;
1939
1940         if (session->endpoint->media.rtp.keepalive > 0 &&
1941                         session_media->type == AST_MEDIA_TYPE_AUDIO) {
1942                 ast_rtp_instance_set_keepalive(session_media->rtp, session->endpoint->media.rtp.keepalive);
1943                 /* Schedule the initial keepalive early in case this is being used to punch holes through
1944                  * a NAT. This way there won't be an awkward delay before media starts flowing in some
1945                  * scenarios.
1946                  */
1947                 AST_SCHED_DEL(sched, session_media->keepalive_sched_id);
1948                 session_media->keepalive_sched_id = ast_sched_add_variable(sched, 500, send_keepalive,
1949                         session_media, 1);
1950         }
1951
1952         /* As the channel lock is not held during this process the scheduled item won't block if
1953          * it is hanging up the channel at the same point we are applying this negotiated SDP.
1954          */
1955         AST_SCHED_DEL(sched, session_media->timeout_sched_id);
1956
1957         /* Due to the fact that we only ever have one scheduled timeout item for when we are both
1958          * off hold and on hold we don't need to store the two timeouts differently on the RTP
1959          * instance itself.
1960          */
1961         ast_rtp_instance_set_timeout(session_media->rtp, 0);
1962         if (session->endpoint->media.rtp.timeout && !session_media->remotely_held) {
1963                 ast_rtp_instance_set_timeout(session_media->rtp, session->endpoint->media.rtp.timeout);
1964         } else if (session->endpoint->media.rtp.timeout_hold && session_media->remotely_held) {
1965                 ast_rtp_instance_set_timeout(session_media->rtp, session->endpoint->media.rtp.timeout_hold);
1966         }
1967
1968         if (ast_rtp_instance_get_timeout(session_media->rtp)) {
1969                 session_media->timeout_sched_id = ast_sched_add_variable(sched,
1970                         ast_rtp_instance_get_timeout(session_media->rtp) * 1000, rtp_check_timeout,
1971                         session_media, 1);
1972         }
1973
1974         return 1;
1975 }
1976
1977 /*! \brief Function which updates the media stream with external media address, if applicable */
1978 static void change_outgoing_sdp_stream_media_address(pjsip_tx_data *tdata, struct pjmedia_sdp_media *stream, struct ast_sip_transport *transport)
1979 {
1980         RAII_VAR(struct ast_sip_transport_state *, transport_state, ast_sip_get_transport_state(ast_sorcery_object_get_id(transport)), ao2_cleanup);
1981         char host[NI_MAXHOST];
1982         struct ast_sockaddr our_sdp_addr = { { 0, } };
1983
1984         /* If the stream has been rejected there will be no connection line */
1985         if (!stream->conn || !transport_state) {
1986                 return;
1987         }
1988
1989         ast_copy_pj_str(host, &stream->conn->addr, sizeof(host));
1990         ast_sockaddr_parse(&our_sdp_addr, host, PARSE_PORT_FORBID);
1991
1992         /* Reversed check here. We don't check the remote endpoint being
1993          * in our local net, but whether our outgoing session IP is
1994          * local. If it is not, we won't do rewriting. No localnet
1995          * configured? Always rewrite. */
1996         if (ast_sip_transport_is_nonlocal(transport_state, &our_sdp_addr) && transport_state->localnet) {
1997                 return;
1998         }
1999         ast_debug(5, "Setting media address to %s\n", ast_sockaddr_stringify_host(&transport_state->external_media_address));
2000         pj_strdup2(tdata->pool, &stream->conn->addr, ast_sockaddr_stringify_host(&transport_state->external_media_address));
2001 }
2002
2003 /*! \brief Function which stops the RTP instance */
2004 static void stream_stop(struct ast_sip_session_media *session_media)
2005 {
2006         if (!session_media->rtp) {
2007                 return;
2008         }
2009
2010         AST_SCHED_DEL(sched, session_media->keepalive_sched_id);
2011         AST_SCHED_DEL(sched, session_media->timeout_sched_id);
2012         ast_rtp_instance_stop(session_media->rtp);
2013 }
2014
2015 /*! \brief Function which destroys the RTP instance when session ends */
2016 static void stream_destroy(struct ast_sip_session_media *session_media)
2017 {
2018         if (session_media->rtp) {
2019                 stream_stop(session_media);
2020                 ast_rtp_instance_destroy(session_media->rtp);
2021         }
2022         session_media->rtp = NULL;
2023 }
2024
2025 /*! \brief SDP handler for 'audio' media stream */
2026 static struct ast_sip_session_sdp_handler audio_sdp_handler = {
2027         .id = STR_AUDIO,
2028         .negotiate_incoming_sdp_stream = negotiate_incoming_sdp_stream,
2029         .create_outgoing_sdp_stream = create_outgoing_sdp_stream,
2030         .apply_negotiated_sdp_stream = apply_negotiated_sdp_stream,
2031         .change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address,
2032         .stream_stop = stream_stop,
2033         .stream_destroy = stream_destroy,
2034 };
2035
2036 /*! \brief SDP handler for 'video' media stream */
2037 static struct ast_sip_session_sdp_handler video_sdp_handler = {
2038         .id = STR_VIDEO,
2039         .negotiate_incoming_sdp_stream = negotiate_incoming_sdp_stream,
2040         .create_outgoing_sdp_stream = create_outgoing_sdp_stream,
2041         .apply_negotiated_sdp_stream = apply_negotiated_sdp_stream,
2042         .change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address,
2043         .stream_stop = stream_stop,
2044         .stream_destroy = stream_destroy,
2045 };
2046
2047 static int video_info_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
2048 {
2049         struct pjsip_transaction *tsx;
2050         pjsip_tx_data *tdata;
2051
2052         if (!session->channel
2053                 || !ast_sip_is_content_type(&rdata->msg_info.msg->body->content_type,
2054                         "application",
2055                         "media_control+xml")) {
2056                 return 0;
2057         }
2058
2059         tsx = pjsip_rdata_get_tsx(rdata);
2060
2061         ast_queue_control(session->channel, AST_CONTROL_VIDUPDATE);
2062
2063         if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, 200, NULL, &tdata) == PJ_SUCCESS) {
2064                 pjsip_dlg_send_response(session->inv_session->dlg, tsx, tdata);
2065         }
2066
2067         return 0;
2068 }
2069
2070 static struct ast_sip_session_supplement video_info_supplement = {
2071         .method = "INFO",
2072         .incoming_request = video_info_incoming_request,
2073 };
2074
2075 /*! \brief Unloads the sdp RTP/AVP module from Asterisk */
2076 static int unload_module(void)
2077 {
2078         ast_sip_session_unregister_supplement(&video_info_supplement);
2079         ast_sip_session_unregister_sdp_handler(&video_sdp_handler, STR_VIDEO);
2080         ast_sip_session_unregister_sdp_handler(&audio_sdp_handler, STR_AUDIO);
2081
2082         if (sched) {
2083                 ast_sched_context_destroy(sched);
2084         }
2085
2086         return 0;
2087 }
2088
2089 /*!
2090  * \brief Load the module
2091  *
2092  * Module loading including tests for configuration or dependencies.
2093  * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
2094  * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
2095  * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
2096  * configuration file or other non-critical problem return
2097  * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
2098  */
2099 static int load_module(void)
2100 {
2101         if (ast_check_ipv6()) {
2102                 ast_sockaddr_parse(&address_rtp, "::", 0);
2103         } else {
2104                 ast_sockaddr_parse(&address_rtp, "0.0.0.0", 0);
2105         }
2106
2107         if (!(sched = ast_sched_context_create())) {
2108                 ast_log(LOG_ERROR, "Unable to create scheduler context.\n");
2109                 goto end;
2110         }
2111
2112         if (ast_sched_start_thread(sched)) {
2113                 ast_log(LOG_ERROR, "Unable to create scheduler context thread.\n");
2114                 goto end;
2115         }
2116
2117         if (ast_sip_session_register_sdp_handler(&audio_sdp_handler, STR_AUDIO)) {
2118                 ast_log(LOG_ERROR, "Unable to register SDP handler for %s stream type\n", STR_AUDIO);
2119                 goto end;
2120         }
2121
2122         if (ast_sip_session_register_sdp_handler(&video_sdp_handler, STR_VIDEO)) {
2123                 ast_log(LOG_ERROR, "Unable to register SDP handler for %s stream type\n", STR_VIDEO);
2124                 goto end;
2125         }
2126
2127         ast_sip_session_register_supplement(&video_info_supplement);
2128
2129         return AST_MODULE_LOAD_SUCCESS;
2130 end:
2131         unload_module();
2132
2133         return AST_MODULE_LOAD_DECLINE;
2134 }
2135
2136 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP SDP RTP/AVP stream handler",
2137         .support_level = AST_MODULE_SUPPORT_CORE,
2138         .load = load_module,
2139         .unload = unload_module,
2140         .load_pri = AST_MODPRI_CHANNEL_DRIVER,
2141         .requires = "res_pjsip,res_pjsip_session",
2142 );