2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
7 * Kevin Harwell <kharwell@digium.com>
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.
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.
22 * \author Joshua Colp <jcolp@digium.com>
24 * \brief SIP SDP media stream handling
28 <depend>pjproject</depend>
29 <depend>res_pjsip</depend>
30 <depend>res_pjsip_session</depend>
31 <support_level>core</support_level>
41 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
43 #include "asterisk/module.h"
44 #include "asterisk/rtp_engine.h"
45 #include "asterisk/netsock2.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/causes.h"
48 #include "asterisk/sched.h"
49 #include "asterisk/acl.h"
50 #include "asterisk/sdp_srtp.h"
52 #include "asterisk/res_pjsip.h"
53 #include "asterisk/res_pjsip_session.h"
55 /*! \brief Scheduler for RTCP purposes */
56 static struct ast_sched_context *sched;
58 /*! \brief Address for IPv4 RTP */
59 static struct ast_sockaddr address_ipv4;
61 /*! \brief Address for IPv6 RTP */
62 static struct ast_sockaddr address_ipv6;
64 static const char STR_AUDIO[] = "audio";
65 static const int FD_AUDIO = 0;
67 static const char STR_VIDEO[] = "video";
68 static const int FD_VIDEO = 2;
70 /*! \brief Retrieves an ast_format_type based on the given stream_type */
71 static enum ast_format_type stream_to_media_type(const char *stream_type)
73 if (!strcasecmp(stream_type, STR_AUDIO)) {
74 return AST_FORMAT_TYPE_AUDIO;
75 } else if (!strcasecmp(stream_type, STR_VIDEO)) {
76 return AST_FORMAT_TYPE_VIDEO;
82 /*! \brief Get the starting descriptor for a media type */
83 static int media_type_to_fdno(enum ast_format_type media_type)
86 case AST_FORMAT_TYPE_AUDIO: return FD_AUDIO;
87 case AST_FORMAT_TYPE_VIDEO: return FD_VIDEO;
88 case AST_FORMAT_TYPE_TEXT:
89 case AST_FORMAT_TYPE_IMAGE: break;
94 /*! \brief Remove all other cap types but the one given */
95 static void format_cap_only_type(struct ast_format_cap *caps, enum ast_format_type media_type)
97 int i = AST_FORMAT_INC;
98 while (i <= AST_FORMAT_TYPE_TEXT) {
99 if (i != media_type) {
100 ast_format_cap_remove_bytype(caps, i);
106 /*! \brief Internal function which creates an RTP instance */
107 static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_media *session_media, unsigned int ipv6)
109 struct ast_rtp_engine_ice *ice;
111 if (!(session_media->rtp = ast_rtp_instance_new(session->endpoint->media.rtp.engine, sched, ipv6 ? &address_ipv6 : &address_ipv4, NULL))) {
112 ast_log(LOG_ERROR, "Unable to create RTP instance using RTP engine '%s'\n", session->endpoint->media.rtp.engine);
116 ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RTCP, 1);
117 ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_NAT, session->endpoint->media.rtp.symmetric);
119 ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(session_media->rtp),
120 session_media->rtp, &session->endpoint->media.prefs);
122 if (session->endpoint->dtmf == AST_SIP_DTMF_INBAND) {
123 ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND);
126 if (!session->endpoint->media.rtp.ice_support && (ice = ast_rtp_instance_get_ice(session_media->rtp))) {
127 ice->stop(session_media->rtp);
130 if (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733) {
131 ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_RFC2833);
132 } else if (session->endpoint->dtmf == AST_SIP_DTMF_INBAND) {
133 ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND);
136 if (!strcmp(session_media->stream_type, STR_AUDIO) &&
137 (session->endpoint->media.tos_audio || session->endpoint->media.cos_video)) {
138 ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_audio,
139 session->endpoint->media.cos_audio, "SIP RTP Audio");
140 } else if (!strcmp(session_media->stream_type, STR_VIDEO) &&
141 (session->endpoint->media.tos_video || session->endpoint->media.cos_video)) {
142 ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_video,
143 session->endpoint->media.cos_video, "SIP RTP Video");
149 static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp_media *stream, struct ast_rtp_codecs *codecs)
151 pjmedia_sdp_attr *attr;
152 pjmedia_sdp_rtpmap *rtpmap;
153 pjmedia_sdp_fmtp fmtp;
154 struct ast_format *format;
160 ast_rtp_codecs_payloads_initialize(codecs);
162 /* Iterate through provided formats */
163 for (i = 0; i < stream->desc.fmt_count; ++i) {
164 /* The payload is kept as a string for things like t38 but for video it is always numerical */
165 ast_rtp_codecs_payloads_set_m_type(codecs, NULL, pj_strtoul(&stream->desc.fmt[i]));
166 /* Look for the optional rtpmap attribute */
167 if (!(attr = pjmedia_sdp_media_find_attr2(stream, "rtpmap", &stream->desc.fmt[i]))) {
171 /* Interpret the attribute as an rtpmap */
172 if ((pjmedia_sdp_attr_to_rtpmap(session->inv_session->pool_prov, attr, &rtpmap)) != PJ_SUCCESS) {
176 ast_copy_pj_str(name, &rtpmap->enc_name, sizeof(name));
177 ast_copy_pj_str(media, (pj_str_t*)&stream->desc.media, sizeof(media));
178 ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, NULL, pj_strtoul(&stream->desc.fmt[i]),
179 media, name, 0, rtpmap->clock_rate);
180 /* Look for an optional associated fmtp attribute */
181 if (!(attr = pjmedia_sdp_media_find_attr2(stream, "fmtp", &rtpmap->pt))) {
185 if ((pjmedia_sdp_attr_get_fmtp(attr, &fmtp)) == PJ_SUCCESS) {
186 sscanf(pj_strbuf(&fmtp.fmt), "%d", &num);
187 if ((format = ast_rtp_codecs_get_payload_format(codecs, num))) {
188 ast_copy_pj_str(fmt_param, &fmtp.fmt_param, sizeof(fmt_param));
189 ast_format_sdp_parse(format, fmt_param);
195 static int set_caps(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
196 const struct pjmedia_sdp_media *stream)
198 RAII_VAR(struct ast_format_cap *, caps, NULL, ast_format_cap_destroy);
199 RAII_VAR(struct ast_format_cap *, peer, NULL, ast_format_cap_destroy);
200 RAII_VAR(struct ast_format_cap *, joint, NULL, ast_format_cap_destroy);
201 enum ast_format_type media_type = stream_to_media_type(session_media->stream_type);
202 struct ast_rtp_codecs codecs;
203 struct ast_format fmt;
205 int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) &&
206 !ast_format_cap_is_empty(session->direct_media_cap);
208 if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK)) ||
209 !(peer = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK))) {
210 ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n", session_media->stream_type);
214 /* get the endpoint capabilities */
215 if (direct_media_enabled) {
216 ast_format_cap_joint_copy(session->endpoint->media.codecs, session->direct_media_cap, caps);
218 ast_format_cap_copy(caps, session->endpoint->media.codecs);
220 format_cap_only_type(caps, media_type);
222 /* get the capabilities on the peer */
223 get_codecs(session, stream, &codecs);
224 ast_rtp_codecs_payload_formats(&codecs, peer, &fmts);
226 /* get the joint capabilities between peer and endpoint */
227 if (!(joint = ast_format_cap_joint(caps, peer))) {
228 char usbuf[64], thembuf[64];
230 ast_rtp_codecs_payloads_destroy(&codecs);
232 ast_getformatname_multiple(usbuf, sizeof(usbuf), caps);
233 ast_getformatname_multiple(thembuf, sizeof(thembuf), peer);
234 ast_log(LOG_WARNING, "No joint capabilities between our configuration(%s) and incoming SDP(%s)\n", usbuf, thembuf);
238 ast_rtp_codecs_payloads_copy(&codecs, ast_rtp_instance_get_codecs(session_media->rtp),
241 ast_format_cap_copy(caps, session->req_caps);
242 ast_format_cap_remove_bytype(caps, media_type);
243 ast_format_cap_append(caps, joint);
244 ast_format_cap_append(session->req_caps, caps);
246 if (session->channel) {
247 ast_format_cap_copy(caps, ast_channel_nativeformats(session->channel));
248 ast_format_cap_remove_bytype(caps, media_type);
249 ast_codec_choose(&session->endpoint->media.prefs, joint, 1, &fmt);
250 ast_format_cap_add(caps, &fmt);
252 /* Apply the new formats to the channel, potentially changing read/write formats while doing so */
253 ast_format_cap_copy(ast_channel_nativeformats(session->channel), caps);
254 ast_format_copy(ast_channel_rawwriteformat(session->channel), &fmt);
255 ast_format_copy(ast_channel_rawreadformat(session->channel), &fmt);
256 ast_set_read_format(session->channel, ast_channel_readformat(session->channel));
257 ast_set_write_format(session->channel, ast_channel_writeformat(session->channel));
260 ast_rtp_codecs_payloads_destroy(&codecs);
264 static pjmedia_sdp_attr* generate_rtpmap_attr(pjmedia_sdp_media *media, pj_pool_t *pool, int rtp_code,
265 int asterisk_format, struct ast_format *format, int code)
267 pjmedia_sdp_rtpmap rtpmap;
268 pjmedia_sdp_attr *attr = NULL;
271 snprintf(tmp, sizeof(tmp), "%d", rtp_code);
272 pj_strdup2(pool, &media->desc.fmt[media->desc.fmt_count++], tmp);
273 rtpmap.pt = media->desc.fmt[media->desc.fmt_count - 1];
274 rtpmap.clock_rate = ast_rtp_lookup_sample_rate2(asterisk_format, format, code);
275 pj_strdup2(pool, &rtpmap.enc_name, ast_rtp_lookup_mime_subtype2(asterisk_format, format, code, 0));
276 rtpmap.param.slen = 0;
277 rtpmap.param.ptr = NULL;
279 pjmedia_sdp_rtpmap_to_attr(pool, &rtpmap, &attr);
284 static pjmedia_sdp_attr* generate_fmtp_attr(pj_pool_t *pool, struct ast_format *format, int rtp_code)
286 struct ast_str *fmtp0 = ast_str_alloca(256);
288 pjmedia_sdp_attr *attr = NULL;
291 ast_format_sdp_generate(format, rtp_code, &fmtp0);
292 if (ast_str_strlen(fmtp0)) {
293 tmp = ast_str_buffer(fmtp0) + ast_str_strlen(fmtp0) - 1;
294 /* remove any carriage return line feeds */
295 while (*tmp == '\r' || *tmp == '\n') --tmp;
297 /* ast...generate gives us everything, just need value */
298 tmp = strchr(ast_str_buffer(fmtp0), ':');
299 if (tmp && tmp + 1) {
300 fmtp1 = pj_str(tmp + 1);
302 fmtp1 = pj_str(ast_str_buffer(fmtp0));
304 attr = pjmedia_sdp_attr_create(pool, "fmtp", &fmtp1);
309 static int codec_pref_has_type(struct ast_codec_pref *prefs, enum ast_format_type media_type)
312 struct ast_format fmt;
313 for (i = 0; ast_codec_pref_index(prefs, i, &fmt); ++i) {
314 if (AST_FORMAT_GET_TYPE(fmt.id) == media_type) {
321 /*! \brief Function which adds ICE attributes to a media stream */
322 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)
324 struct ast_rtp_engine_ice *ice;
325 struct ao2_container *candidates;
326 const char *username, *password;
328 pjmedia_sdp_attr *attr;
329 struct ao2_iterator it_candidates;
330 struct ast_rtp_engine_ice_candidate *candidate;
332 if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp)) ||
333 !(candidates = ice->get_local_candidates(session_media->rtp))) {
337 if ((username = ice->get_ufrag(session_media->rtp))) {
338 attr = pjmedia_sdp_attr_create(pool, "ice-ufrag", pj_cstr(&stmp, username));
339 media->attr[media->attr_count++] = attr;
342 if ((password = ice->get_password(session_media->rtp))) {
343 attr = pjmedia_sdp_attr_create(pool, "ice-pwd", pj_cstr(&stmp, password));
344 media->attr[media->attr_count++] = attr;
347 it_candidates = ao2_iterator_init(candidates, 0);
348 for (; (candidate = ao2_iterator_next(&it_candidates)); ao2_ref(candidate, -1)) {
349 struct ast_str *attr_candidate = ast_str_create(128);
351 ast_str_set(&attr_candidate, -1, "%s %u %s %d %s ", candidate->foundation, candidate->id, candidate->transport,
352 candidate->priority, ast_sockaddr_stringify_host(&candidate->address));
353 ast_str_append(&attr_candidate, -1, "%s typ ", ast_sockaddr_stringify_port(&candidate->address));
355 switch (candidate->type) {
356 case AST_RTP_ICE_CANDIDATE_TYPE_HOST:
357 ast_str_append(&attr_candidate, -1, "host");
359 case AST_RTP_ICE_CANDIDATE_TYPE_SRFLX:
360 ast_str_append(&attr_candidate, -1, "srflx");
362 case AST_RTP_ICE_CANDIDATE_TYPE_RELAYED:
363 ast_str_append(&attr_candidate, -1, "relay");
367 if (!ast_sockaddr_isnull(&candidate->relay_address)) {
368 ast_str_append(&attr_candidate, -1, " raddr %s rport ", ast_sockaddr_stringify_host(&candidate->relay_address));
369 ast_str_append(&attr_candidate, -1, " %s", ast_sockaddr_stringify_port(&candidate->relay_address));
372 attr = pjmedia_sdp_attr_create(pool, "candidate", pj_cstr(&stmp, ast_str_buffer(attr_candidate)));
373 media->attr[media->attr_count++] = attr;
375 ast_free(attr_candidate);
378 ao2_iterator_destroy(&it_candidates);
381 /*! \brief Function which processes ICE attributes in an audio stream */
382 static void process_ice_attributes(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
383 const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream)
385 struct ast_rtp_engine_ice *ice;
386 const pjmedia_sdp_attr *attr;
387 char attr_value[256];
390 /* If ICE support is not enabled or available exit early */
391 if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp))) {
395 if ((attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-ufrag", NULL))) {
396 ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
397 ice->set_authentication(session_media->rtp, attr_value, NULL);
400 if ((attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-pwd", NULL))) {
401 ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
402 ice->set_authentication(session_media->rtp, NULL, attr_value);
405 if (pjmedia_sdp_media_find_attr2(remote_stream, "ice-lite", NULL)) {
406 ice->ice_lite(session_media->rtp);
409 /* Find all of the candidates */
410 for (attr_i = 0; attr_i < remote_stream->attr_count; ++attr_i) {
411 char foundation[32], transport[32], address[PJ_INET6_ADDRSTRLEN + 1], cand_type[6], relay_address[PJ_INET6_ADDRSTRLEN + 1] = "";
412 unsigned int port, relay_port = 0;
413 struct ast_rtp_engine_ice_candidate candidate = { 0, };
415 attr = remote_stream->attr[attr_i];
417 /* If this is not a candidate line skip it */
418 if (pj_strcmp2(&attr->name, "candidate")) {
422 ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
424 if (sscanf(attr_value, "%31s %30u %31s %30u %46s %30u typ %5s %*s %23s %*s %30u", foundation, &candidate.id, transport,
425 (unsigned *)&candidate.priority, address, &port, cand_type, relay_address, &relay_port) < 7) {
426 /* Candidate did not parse properly */
430 candidate.foundation = foundation;
431 candidate.transport = transport;
433 ast_sockaddr_parse(&candidate.address, address, PARSE_PORT_FORBID);
434 ast_sockaddr_set_port(&candidate.address, port);
436 if (!strcasecmp(cand_type, "host")) {
437 candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_HOST;
438 } else if (!strcasecmp(cand_type, "srflx")) {
439 candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX;
440 } else if (!strcasecmp(cand_type, "relay")) {
441 candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED;
446 if (!ast_strlen_zero(relay_address)) {
447 ast_sockaddr_parse(&candidate.relay_address, relay_address, PARSE_PORT_FORBID);
451 ast_sockaddr_set_port(&candidate.relay_address, relay_port);
454 ice->add_remote_candidate(session_media->rtp, &candidate);
457 ice->start(session_media->rtp);
460 static void apply_packetization(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
461 const struct pjmedia_sdp_media *remote_stream)
463 pjmedia_sdp_attr *attr;
465 unsigned long framing;
467 struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(session_media->rtp)->pref;
469 /* Apply packetization if available and configured to do so */
470 if (!session->endpoint->media.rtp.use_ptime || !(attr = pjmedia_sdp_media_find_attr2(remote_stream, "ptime", NULL))) {
475 framing = pj_strtoul(pj_strltrim(&value));
477 for (codec = 0; codec < AST_RTP_MAX_PT; codec++) {
478 struct ast_rtp_payload_type format = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(
479 session_media->rtp), codec);
481 if (!format.asterisk_format) {
485 ast_codec_pref_setsize(pref, &format.format, framing);
488 ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(session_media->rtp),
489 session_media->rtp, pref);
492 /*! \brief figure out media transport encryption type from the media transport string */
493 static enum ast_sip_session_media_encryption get_media_encryption_type(pj_str_t transport)
495 RAII_VAR(char *, transport_str, ast_strndup(transport.ptr, transport.slen), ast_free);
496 if (strstr(transport_str, "UDP/TLS")) {
497 return AST_SIP_MEDIA_ENCRYPT_DTLS;
498 } else if (strstr(transport_str, "SAVP")) {
499 return AST_SIP_MEDIA_ENCRYPT_SDES;
501 return AST_SIP_MEDIA_ENCRYPT_NONE;
506 * \brief Checks whether the encryption offered in SDP is compatible with the endpoint's configuration
509 * \param endpoint_encryption Media encryption configured for the endpoint
510 * \param stream pjmedia_sdp_media stream description
512 * \retval AST_SIP_MEDIA_TRANSPORT_INVALID on encryption mismatch
513 * \retval The encryption requested in the SDP
515 static enum ast_sip_session_media_encryption check_endpoint_media_transport(
516 struct ast_sip_endpoint *endpoint,
517 const struct pjmedia_sdp_media *stream)
519 enum ast_sip_session_media_encryption incoming_encryption;
521 if (endpoint->media.rtp.use_avpf) {
522 char transport_end = stream->desc.transport.ptr[stream->desc.transport.slen - 1];
523 if (transport_end != 'F') {
524 return AST_SIP_MEDIA_TRANSPORT_INVALID;
528 incoming_encryption = get_media_encryption_type(stream->desc.transport);
530 if (incoming_encryption == endpoint->media.rtp.encryption) {
531 return incoming_encryption;
534 return AST_SIP_MEDIA_TRANSPORT_INVALID;
537 static int setup_srtp(struct ast_sip_session_media *session_media)
539 if (!session_media->srtp) {
540 session_media->srtp = ast_sdp_srtp_alloc();
541 if (!session_media->srtp) {
546 if (!session_media->srtp->crypto) {
547 session_media->srtp->crypto = ast_sdp_crypto_alloc();
548 if (!session_media->srtp->crypto) {
556 static int setup_dtls_srtp(struct ast_sip_session *session,
557 struct ast_sip_session_media *session_media)
559 struct ast_rtp_engine_dtls *dtls;
561 if (!session->endpoint->media.rtp.dtls_cfg.enabled || !session_media->rtp) {
565 dtls = ast_rtp_instance_get_dtls(session_media->rtp);
570 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);
571 if (dtls->set_configuration(session_media->rtp, &session->endpoint->media.rtp.dtls_cfg)) {
572 ast_log(LOG_ERROR, "Attempted to set an invalid DTLS-SRTP configuration on RTP instance '%p'\n",
577 if (setup_srtp(session_media)) {
583 static int parse_dtls_attrib(struct ast_sip_session_media *session_media,
584 const struct pjmedia_sdp_media *stream)
587 struct ast_rtp_engine_dtls *dtls = ast_rtp_instance_get_dtls(session_media->rtp);
589 for (i = 0; i < stream->attr_count; i++) {
590 pjmedia_sdp_attr *attr = stream->attr[i];
593 if (!attr->value.ptr) {
597 value = pj_strtrim(&attr->value);
599 if (!pj_strcmp2(&attr->name, "setup")) {
600 if (!pj_stricmp2(value, "active")) {
601 dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_ACTIVE);
602 } else if (!pj_stricmp2(value, "passive")) {
603 dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_PASSIVE);
604 } else if (!pj_stricmp2(value, "actpass")) {
605 dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_ACTPASS);
606 } else if (!pj_stricmp2(value, "holdconn")) {
607 dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_HOLDCONN);
609 ast_log(LOG_WARNING, "Unsupported setup attribute value '%*s'\n", (int)value->slen, value->ptr);
611 } else if (!pj_strcmp2(&attr->name, "connection")) {
612 if (!pj_stricmp2(value, "new")) {
613 dtls->reset(session_media->rtp);
614 } else if (!pj_stricmp2(value, "existing")) {
617 ast_log(LOG_WARNING, "Unsupported connection attribute value '%*s'\n", (int)value->slen, value->ptr);
619 } else if (!pj_strcmp2(&attr->name, "fingerprint")) {
620 char hash_value[256], hash[6];
621 char fingerprint_text[value->slen + 1];
622 ast_copy_pj_str(fingerprint_text, value, sizeof(fingerprint_text));
624 if (sscanf(fingerprint_text, "%5s %255s", hash, hash_value) == 2) {
625 if (!strcasecmp(hash, "sha-1")) {
626 dtls->set_fingerprint(session_media->rtp, AST_RTP_DTLS_HASH_SHA1, hash_value);
628 ast_log(LOG_WARNING, "Unsupported fingerprint hash type '%s'\n",
634 ast_set_flag(session_media->srtp, AST_SRTP_CRYPTO_OFFER_OK);
639 static int setup_sdes_srtp(struct ast_sip_session_media *session_media,
640 const struct pjmedia_sdp_media *stream)
644 for (i = 0; i < stream->attr_count; i++) {
645 pjmedia_sdp_attr *attr;
646 RAII_VAR(char *, crypto_str, NULL, ast_free);
648 /* check the stream for the required crypto attribute */
649 attr = stream->attr[i];
650 if (pj_strcmp2(&attr->name, "crypto")) {
654 crypto_str = ast_strndup(attr->value.ptr, attr->value.slen);
659 if (setup_srtp(session_media)) {
663 if (!ast_sdp_crypto_process(session_media->rtp, session_media->srtp, crypto_str)) {
664 /* found a valid crypto attribute */
668 ast_debug(1, "Ignoring crypto offer with unsupported parameters: %s\n", crypto_str);
671 /* no usable crypto attributes found */
675 static int setup_media_encryption(struct ast_sip_session *session,
676 struct ast_sip_session_media *session_media,
677 const struct pjmedia_sdp_media *stream)
679 switch (session->endpoint->media.rtp.encryption) {
680 case AST_SIP_MEDIA_ENCRYPT_SDES:
681 if (setup_sdes_srtp(session_media, stream)) {
685 case AST_SIP_MEDIA_ENCRYPT_DTLS:
686 if (setup_dtls_srtp(session, session_media)) {
689 if (parse_dtls_attrib(session_media, stream)) {
693 case AST_SIP_MEDIA_TRANSPORT_INVALID:
694 case AST_SIP_MEDIA_ENCRYPT_NONE:
701 /*! \brief Function which negotiates an incoming media stream */
702 static int negotiate_incoming_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
703 const struct pjmedia_sdp_session *sdp, const struct pjmedia_sdp_media *stream)
705 char host[NI_MAXHOST];
706 RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free_ptr);
707 enum ast_format_type media_type = stream_to_media_type(session_media->stream_type);
709 /* If no type formats have been configured reject this stream */
710 if (!ast_format_cap_has_type(session->endpoint->media.codecs, media_type)) {
714 /* Ensure incoming transport is compatible with the endpoint's configuration */
715 if (check_endpoint_media_transport(session->endpoint, stream) == AST_SIP_MEDIA_TRANSPORT_INVALID) {
719 ast_copy_pj_str(host, stream->conn ? &stream->conn->addr : &sdp->conn->addr, sizeof(host));
721 /* Ensure that the address provided is valid */
722 if (ast_sockaddr_resolve(&addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC) <= 0) {
723 /* The provided host was actually invalid so we error out this negotiation */
727 /* Using the connection information create an appropriate RTP instance */
728 if (!session_media->rtp && create_rtp(session, session_media, ast_sockaddr_is_ipv6(addrs))) {
732 if (setup_media_encryption(session, session_media, stream)) {
736 if (set_caps(session, session_media, stream)) {
740 if (media_type == AST_FORMAT_TYPE_AUDIO) {
741 apply_packetization(session, session_media, stream);
747 static int add_crypto_to_stream(struct ast_sip_session *session,
748 struct ast_sip_session_media *session_media,
749 pj_pool_t *pool, pjmedia_sdp_media *media)
752 pjmedia_sdp_attr *attr;
753 const char *crypto_attribute;
754 struct ast_rtp_engine_dtls *dtls;
755 static const pj_str_t STR_NEW = { "new", 3 };
756 static const pj_str_t STR_EXISTING = { "existing", 8 };
757 static const pj_str_t STR_ACTIVE = { "active", 6 };
758 static const pj_str_t STR_PASSIVE = { "passive", 7 };
759 static const pj_str_t STR_ACTPASS = { "actpass", 7 };
760 static const pj_str_t STR_HOLDCONN = { "holdconn", 8 };
762 switch (session->endpoint->media.rtp.encryption) {
763 case AST_SIP_MEDIA_ENCRYPT_NONE:
764 case AST_SIP_MEDIA_TRANSPORT_INVALID:
766 case AST_SIP_MEDIA_ENCRYPT_SDES:
767 if (!session_media->srtp) {
768 session_media->srtp = ast_sdp_srtp_alloc();
769 if (!session_media->srtp) {
774 crypto_attribute = ast_sdp_srtp_get_attrib(session_media->srtp,
775 0 /* DTLS running? No */,
776 session->endpoint->media.rtp.srtp_tag_32 /* 32 byte tag length? */);
777 if (!crypto_attribute) {
778 /* No crypto attribute to add, bad news */
782 attr = pjmedia_sdp_attr_create(pool, "crypto", pj_cstr(&stmp, crypto_attribute));
783 media->attr[media->attr_count++] = attr;
785 case AST_SIP_MEDIA_ENCRYPT_DTLS:
786 if (setup_dtls_srtp(session, session_media)) {
790 dtls = ast_rtp_instance_get_dtls(session_media->rtp);
795 switch (dtls->get_connection(session_media->rtp)) {
796 case AST_RTP_DTLS_CONNECTION_NEW:
797 attr = pjmedia_sdp_attr_create(pool, "connection", &STR_NEW);
798 media->attr[media->attr_count++] = attr;
800 case AST_RTP_DTLS_CONNECTION_EXISTING:
801 attr = pjmedia_sdp_attr_create(pool, "connection", &STR_EXISTING);
802 media->attr[media->attr_count++] = attr;
808 switch (dtls->get_setup(session_media->rtp)) {
809 case AST_RTP_DTLS_SETUP_ACTIVE:
810 attr = pjmedia_sdp_attr_create(pool, "setup", &STR_ACTIVE);
811 media->attr[media->attr_count++] = attr;
813 case AST_RTP_DTLS_SETUP_PASSIVE:
814 attr = pjmedia_sdp_attr_create(pool, "setup", &STR_PASSIVE);
815 media->attr[media->attr_count++] = attr;
817 case AST_RTP_DTLS_SETUP_ACTPASS:
818 attr = pjmedia_sdp_attr_create(pool, "setup", &STR_ACTPASS);
819 media->attr[media->attr_count++] = attr;
821 case AST_RTP_DTLS_SETUP_HOLDCONN:
822 attr = pjmedia_sdp_attr_create(pool, "setup", &STR_HOLDCONN);
823 media->attr[media->attr_count++] = attr;
829 if ((crypto_attribute = dtls->get_fingerprint(session_media->rtp, AST_RTP_DTLS_HASH_SHA1))) {
830 RAII_VAR(struct ast_str *, fingerprint, ast_str_create(64), ast_free);
835 ast_str_set(&fingerprint, 0, "SHA-1 %s", crypto_attribute);
837 attr = pjmedia_sdp_attr_create(pool, "fingerprint", pj_cstr(&stmp, ast_str_buffer(fingerprint)));
838 media->attr[media->attr_count++] = attr;
846 /*! \brief Function which creates an outgoing stream */
847 static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
848 struct pjmedia_sdp_session *sdp)
850 pj_pool_t *pool = session->inv_session->pool_prov;
851 static const pj_str_t STR_IN = { "IN", 2 };
852 static const pj_str_t STR_IP4 = { "IP4", 3};
853 static const pj_str_t STR_IP6 = { "IP6", 3};
854 static const pj_str_t STR_SENDRECV = { "sendrecv", 8 };
855 pjmedia_sdp_media *media;
856 char hostip[PJ_INET6_ADDRSTRLEN+2];
857 struct ast_sockaddr addr;
860 pjmedia_sdp_attr *attr;
862 int noncodec = (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733) ? AST_RTP_DTMF : 0;
863 int min_packet_size = 0, max_packet_size = 0;
865 struct ast_format format;
866 RAII_VAR(struct ast_format_cap *, caps, NULL, ast_format_cap_destroy);
867 enum ast_format_type media_type = stream_to_media_type(session_media->stream_type);
869 int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) &&
870 !ast_format_cap_is_empty(session->direct_media_cap);
872 int use_override_prefs = session->override_prefs.formats[0].id;
873 struct ast_codec_pref *prefs = use_override_prefs ?
874 &session->override_prefs : &session->endpoint->media.prefs;
876 if ((use_override_prefs && !codec_pref_has_type(&session->override_prefs, media_type)) ||
877 (!use_override_prefs && !ast_format_cap_has_type(session->endpoint->media.codecs, media_type))) {
878 /* If no type formats are configured don't add a stream */
880 } else if (!session_media->rtp && create_rtp(session, session_media, session->endpoint->media.rtp.ipv6)) {
884 if (!(media = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_media))) ||
885 !(media->conn = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_conn)))) {
889 if (add_crypto_to_stream(session, session_media, pool, media)) {
893 media->desc.media = pj_str(session_media->stream_type);
894 media->desc.transport = pj_str(ast_sdp_get_rtp_profile(
895 session->endpoint->media.rtp.encryption == AST_SIP_MEDIA_ENCRYPT_SDES,
896 session_media->rtp, session->endpoint->media.rtp.use_avpf));
898 /* Add connection level details */
899 if (direct_media_enabled) {
900 ast_copy_string(hostip, ast_sockaddr_stringify_fmt(&session_media->direct_media_addr, AST_SOCKADDR_STR_ADDR), sizeof(hostip));
901 } else if (ast_strlen_zero(session->endpoint->media.address)) {
902 pj_sockaddr localaddr;
904 if (pj_gethostip(session->endpoint->media.rtp.ipv6 ? pj_AF_INET6() : pj_AF_INET(), &localaddr)) {
907 pj_sockaddr_print(&localaddr, hostip, sizeof(hostip), 2);
909 ast_copy_string(hostip, session->endpoint->media.address, sizeof(hostip));
912 media->conn->net_type = STR_IN;
913 media->conn->addr_type = session->endpoint->media.rtp.ipv6 ? STR_IP6 : STR_IP4;
914 pj_strdup2(pool, &media->conn->addr, hostip);
915 ast_rtp_instance_get_local_address(session_media->rtp, &addr);
916 media->desc.port = direct_media_enabled ? ast_sockaddr_port(&session_media->direct_media_addr) : (pj_uint16_t) ast_sockaddr_port(&addr);
917 media->desc.port_count = 1;
919 /* Add ICE attributes and candidates */
920 add_ice_to_stream(session, session_media, pool, media);
922 if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK))) {
923 ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n", session_media->stream_type);
927 if (direct_media_enabled) {
928 ast_format_cap_joint_copy(session->endpoint->media.codecs, session->direct_media_cap, caps);
929 } else if (ast_format_cap_is_empty(session->req_caps) || !ast_format_cap_has_joint(session->req_caps, session->endpoint->media.codecs)) {
930 ast_format_cap_copy(caps, session->endpoint->media.codecs);
932 ast_format_cap_copy(caps, session->req_caps);
935 for (index = 0; ast_codec_pref_index(prefs, index, &format); ++index) {
936 struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(session_media->rtp)->pref;
938 if (AST_FORMAT_GET_TYPE(format.id) != media_type) {
942 if (!use_override_prefs && !ast_format_cap_get_compatible_format(caps, &format, &format)) {
946 if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 1, &format, 0)) == -1) {
947 ast_log(LOG_WARNING,"Unable to get rtp codec payload code for %s\n",ast_getformatname(&format));
951 if (!(attr = generate_rtpmap_attr(media, pool, rtp_code, 1, &format, 0))) {
955 media->attr[media->attr_count++] = attr;
957 if ((attr = generate_fmtp_attr(pool, &format, rtp_code))) {
958 media->attr[media->attr_count++] = attr;
961 if (pref && media_type != AST_FORMAT_TYPE_VIDEO) {
962 struct ast_format_list fmt = ast_codec_pref_getsize(pref, &format);
963 if (fmt.cur_ms && ((fmt.cur_ms < min_packet_size) || !min_packet_size)) {
964 min_packet_size = fmt.cur_ms;
967 if (fmt.max_ms && ((fmt.max_ms < max_packet_size) || !max_packet_size)) {
968 max_packet_size = fmt.max_ms;
973 /* Add non-codec formats */
974 if (media_type != AST_FORMAT_TYPE_VIDEO) {
975 for (index = 1LL; index <= AST_RTP_MAX; index <<= 1) {
976 if (!(noncodec & index) || (rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp),
977 0, NULL, index)) == -1) {
981 if (!(attr = generate_rtpmap_attr(media, pool, rtp_code, 0, NULL, index))) {
985 media->attr[media->attr_count++] = attr;
987 if (index == AST_RTP_DTMF) {
988 snprintf(tmp, sizeof(tmp), "%d 0-16", rtp_code);
989 attr = pjmedia_sdp_attr_create(pool, "fmtp", pj_cstr(&stmp, tmp));
990 media->attr[media->attr_count++] = attr;
995 /* If no formats were actually added to the media stream don't add it to the SDP */
996 if (!media->desc.fmt_count) {
1000 /* If ptime is set add it as an attribute */
1001 if (min_packet_size) {
1002 snprintf(tmp, sizeof(tmp), "%d", min_packet_size);
1003 attr = pjmedia_sdp_attr_create(pool, "ptime", pj_cstr(&stmp, tmp));
1004 media->attr[media->attr_count++] = attr;
1007 if (max_packet_size) {
1008 snprintf(tmp, sizeof(tmp), "%d", max_packet_size);
1009 attr = pjmedia_sdp_attr_create(pool, "maxptime", pj_cstr(&stmp, tmp));
1010 media->attr[media->attr_count++] = attr;
1013 /* Add the sendrecv attribute - we purposely don't keep track because pjmedia-sdp will automatically change our offer for us */
1014 attr = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_attr);
1015 attr->name = STR_SENDRECV;
1016 media->attr[media->attr_count++] = attr;
1018 /* Add the media stream to the SDP */
1019 sdp->media[sdp->media_count++] = media;
1024 static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
1025 const struct pjmedia_sdp_session *local, const struct pjmedia_sdp_media *local_stream,
1026 const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream)
1028 RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free_ptr);
1029 enum ast_format_type media_type = stream_to_media_type(session_media->stream_type);
1030 char host[NI_MAXHOST];
1033 if (!session->channel) {
1037 /* Ensure incoming transport is compatible with the endpoint's configuration */
1038 if (check_endpoint_media_transport(session->endpoint, remote_stream) == AST_SIP_MEDIA_TRANSPORT_INVALID) {
1042 /* Create an RTP instance if need be */
1043 if (!session_media->rtp && create_rtp(session, session_media, session->endpoint->media.rtp.ipv6)) {
1047 if (setup_media_encryption(session, session_media, remote_stream)) {
1051 ast_copy_pj_str(host, remote_stream->conn ? &remote_stream->conn->addr : &remote->conn->addr, sizeof(host));
1053 /* Ensure that the address provided is valid */
1054 if (ast_sockaddr_resolve(&addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC) <= 0) {
1055 /* The provided host was actually invalid so we error out this negotiation */
1059 /* Apply connection information to the RTP instance */
1060 ast_sockaddr_set_port(addrs, remote_stream->desc.port);
1061 ast_rtp_instance_set_remote_address(session_media->rtp, addrs);
1063 if (set_caps(session, session_media, local_stream)) {
1067 if (media_type == AST_FORMAT_TYPE_AUDIO) {
1068 apply_packetization(session, session_media, remote_stream);
1071 if ((fdno = media_type_to_fdno(media_type)) < 0) {
1074 ast_channel_set_fd(session->channel, fdno, ast_rtp_instance_fd(session_media->rtp, 0));
1075 ast_channel_set_fd(session->channel, fdno + 1, ast_rtp_instance_fd(session_media->rtp, 1));
1077 /* If ICE support is enabled find all the needed attributes */
1078 process_ice_attributes(session, session_media, remote, remote_stream);
1080 /* Ensure the RTP instance is active */
1081 ast_rtp_instance_activate(session_media->rtp);
1083 /* audio stream handles music on hold */
1084 if (media_type != AST_FORMAT_TYPE_AUDIO) {
1088 if (ast_sockaddr_isnull(addrs) ||
1089 ast_sockaddr_is_any(addrs) ||
1090 pjmedia_sdp_media_find_attr2(remote_stream, "sendonly", NULL)) {
1091 if (!session_media->held) {
1092 /* The remote side has put us on hold */
1093 ast_queue_hold(session->channel, session->endpoint->mohsuggest);
1094 ast_rtp_instance_stop(session_media->rtp);
1095 ast_queue_frame(session->channel, &ast_null_frame);
1096 session_media->held = 1;
1098 } else if (session_media->held) {
1099 /* The remote side has taken us off hold */
1100 ast_queue_unhold(session->channel);
1101 ast_queue_frame(session->channel, &ast_null_frame);
1102 session_media->held = 0;
1108 /*! \brief Function which updates the media stream with external media address, if applicable */
1109 static void change_outgoing_sdp_stream_media_address(pjsip_tx_data *tdata, struct pjmedia_sdp_media *stream, struct ast_sip_transport *transport)
1111 char host[NI_MAXHOST];
1112 struct ast_sockaddr addr = { { 0, } };
1114 /* If the stream has been rejected there will be no connection line */
1115 if (!stream->conn) {
1119 ast_copy_pj_str(host, &stream->conn->addr, sizeof(host));
1120 ast_sockaddr_parse(&addr, host, PARSE_PORT_FORBID);
1122 /* Is the address within the SDP inside the same network? */
1123 if (ast_apply_ha(transport->localnet, &addr) == AST_SENSE_ALLOW) {
1127 pj_strdup2(tdata->pool, &stream->conn->addr, transport->external_media_address);
1130 /*! \brief Function which destroys the RTP instance when session ends */
1131 static void stream_destroy(struct ast_sip_session_media *session_media)
1133 if (session_media->rtp) {
1134 ast_rtp_instance_stop(session_media->rtp);
1135 ast_rtp_instance_destroy(session_media->rtp);
1139 /*! \brief SDP handler for 'audio' media stream */
1140 static struct ast_sip_session_sdp_handler audio_sdp_handler = {
1142 .negotiate_incoming_sdp_stream = negotiate_incoming_sdp_stream,
1143 .create_outgoing_sdp_stream = create_outgoing_sdp_stream,
1144 .apply_negotiated_sdp_stream = apply_negotiated_sdp_stream,
1145 .change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address,
1146 .stream_destroy = stream_destroy,
1149 /*! \brief SDP handler for 'video' media stream */
1150 static struct ast_sip_session_sdp_handler video_sdp_handler = {
1152 .negotiate_incoming_sdp_stream = negotiate_incoming_sdp_stream,
1153 .create_outgoing_sdp_stream = create_outgoing_sdp_stream,
1154 .apply_negotiated_sdp_stream = apply_negotiated_sdp_stream,
1155 .change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address,
1156 .stream_destroy = stream_destroy,
1159 static int video_info_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
1161 struct pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
1162 pjsip_tx_data *tdata;
1164 if (!ast_sip_is_content_type(&rdata->msg_info.msg->body->content_type,
1166 "media_control+xml")) {
1170 ast_queue_control(session->channel, AST_CONTROL_VIDUPDATE);
1172 if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, 200, NULL, &tdata) == PJ_SUCCESS) {
1173 pjsip_dlg_send_response(session->inv_session->dlg, tsx, tdata);
1179 static struct ast_sip_session_supplement video_info_supplement = {
1181 .incoming_request = video_info_incoming_request,
1184 /*! \brief Unloads the sdp RTP/AVP module from Asterisk */
1185 static int unload_module(void)
1187 ast_sip_session_unregister_supplement(&video_info_supplement);
1188 ast_sip_session_unregister_sdp_handler(&video_sdp_handler, STR_VIDEO);
1189 ast_sip_session_unregister_sdp_handler(&audio_sdp_handler, STR_AUDIO);
1192 ast_sched_context_destroy(sched);
1199 * \brief Load the module
1201 * Module loading including tests for configuration or dependencies.
1202 * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
1203 * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
1204 * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
1205 * configuration file or other non-critical problem return
1206 * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
1208 static int load_module(void)
1210 ast_sockaddr_parse(&address_ipv4, "0.0.0.0", 0);
1211 ast_sockaddr_parse(&address_ipv6, "::", 0);
1213 if (!(sched = ast_sched_context_create())) {
1214 ast_log(LOG_ERROR, "Unable to create scheduler context.\n");
1218 if (ast_sched_start_thread(sched)) {
1219 ast_log(LOG_ERROR, "Unable to create scheduler context thread.\n");
1223 if (ast_sip_session_register_sdp_handler(&audio_sdp_handler, STR_AUDIO)) {
1224 ast_log(LOG_ERROR, "Unable to register SDP handler for %s stream type\n", STR_AUDIO);
1228 if (ast_sip_session_register_sdp_handler(&video_sdp_handler, STR_VIDEO)) {
1229 ast_log(LOG_ERROR, "Unable to register SDP handler for %s stream type\n", STR_VIDEO);
1233 ast_sip_session_register_supplement(&video_info_supplement);
1235 return AST_MODULE_LOAD_SUCCESS;
1239 return AST_MODULE_LOAD_FAILURE;
1242 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP SDP RTP/AVP stream handler",
1243 .load = load_module,
1244 .unload = unload_module,
1245 .load_pri = AST_MODPRI_CHANNEL_DRIVER,