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/format.h"
45 #include "asterisk/format_cap.h"
46 #include "asterisk/rtp_engine.h"
47 #include "asterisk/netsock2.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/causes.h"
50 #include "asterisk/sched.h"
51 #include "asterisk/acl.h"
52 #include "asterisk/sdp_srtp.h"
54 #include "asterisk/res_pjsip.h"
55 #include "asterisk/res_pjsip_session.h"
57 /*! \brief Scheduler for RTCP purposes */
58 static struct ast_sched_context *sched;
60 /*! \brief Address for IPv4 RTP */
61 static struct ast_sockaddr address_ipv4;
63 /*! \brief Address for IPv6 RTP */
64 static struct ast_sockaddr address_ipv6;
66 static const char STR_AUDIO[] = "audio";
67 static const int FD_AUDIO = 0;
69 static const char STR_VIDEO[] = "video";
70 static const int FD_VIDEO = 2;
72 /*! \brief Retrieves an ast_format_type based on the given stream_type */
73 static enum ast_media_type stream_to_media_type(const char *stream_type)
75 if (!strcasecmp(stream_type, STR_AUDIO)) {
76 return AST_MEDIA_TYPE_AUDIO;
77 } else if (!strcasecmp(stream_type, STR_VIDEO)) {
78 return AST_MEDIA_TYPE_VIDEO;
84 /*! \brief Get the starting descriptor for a media type */
85 static int media_type_to_fdno(enum ast_media_type media_type)
88 case AST_MEDIA_TYPE_AUDIO: return FD_AUDIO;
89 case AST_MEDIA_TYPE_VIDEO: return FD_VIDEO;
90 case AST_MEDIA_TYPE_TEXT:
91 case AST_MEDIA_TYPE_UNKNOWN:
92 case AST_MEDIA_TYPE_IMAGE: break;
97 /*! \brief Remove all other cap types but the one given */
98 static void format_cap_only_type(struct ast_format_cap *caps, enum ast_media_type media_type)
101 while (i <= AST_MEDIA_TYPE_TEXT) {
102 if (i != media_type && i != AST_MEDIA_TYPE_UNKNOWN) {
103 ast_format_cap_remove_by_type(caps, i);
109 /*! \brief Internal function which creates an RTP instance */
110 static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_media *session_media, unsigned int ipv6)
112 struct ast_rtp_engine_ice *ice;
114 if (!(session_media->rtp = ast_rtp_instance_new(session->endpoint->media.rtp.engine, sched, ipv6 ? &address_ipv6 : &address_ipv4, NULL))) {
115 ast_log(LOG_ERROR, "Unable to create RTP instance using RTP engine '%s'\n", session->endpoint->media.rtp.engine);
119 ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RTCP, 1);
120 ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_NAT, session->endpoint->media.rtp.symmetric);
122 if (!session->endpoint->media.rtp.ice_support && (ice = ast_rtp_instance_get_ice(session_media->rtp))) {
123 ice->stop(session_media->rtp);
126 if (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733) {
127 ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_RFC2833);
128 ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_DTMF, 1);
129 } else if (session->endpoint->dtmf == AST_SIP_DTMF_INBAND) {
130 ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND);
133 if (!strcmp(session_media->stream_type, STR_AUDIO) &&
134 (session->endpoint->media.tos_audio || session->endpoint->media.cos_video)) {
135 ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_audio,
136 session->endpoint->media.cos_audio, "SIP RTP Audio");
137 } else if (!strcmp(session_media->stream_type, STR_VIDEO) &&
138 (session->endpoint->media.tos_video || session->endpoint->media.cos_video)) {
139 ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_video,
140 session->endpoint->media.cos_video, "SIP RTP Video");
146 static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp_media *stream, struct ast_rtp_codecs *codecs)
148 pjmedia_sdp_attr *attr;
149 pjmedia_sdp_rtpmap *rtpmap;
150 pjmedia_sdp_fmtp fmtp;
151 struct ast_format *format;
157 ast_rtp_codecs_payloads_initialize(codecs);
159 /* Iterate through provided formats */
160 for (i = 0; i < stream->desc.fmt_count; ++i) {
161 /* The payload is kept as a string for things like t38 but for video it is always numerical */
162 ast_rtp_codecs_payloads_set_m_type(codecs, NULL, pj_strtoul(&stream->desc.fmt[i]));
163 /* Look for the optional rtpmap attribute */
164 if (!(attr = pjmedia_sdp_media_find_attr2(stream, "rtpmap", &stream->desc.fmt[i]))) {
168 /* Interpret the attribute as an rtpmap */
169 if ((pjmedia_sdp_attr_to_rtpmap(session->inv_session->pool_prov, attr, &rtpmap)) != PJ_SUCCESS) {
173 ast_copy_pj_str(name, &rtpmap->enc_name, sizeof(name));
174 ast_copy_pj_str(media, (pj_str_t*)&stream->desc.media, sizeof(media));
175 ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, NULL, pj_strtoul(&stream->desc.fmt[i]),
176 media, name, 0, rtpmap->clock_rate);
177 /* Look for an optional associated fmtp attribute */
178 if (!(attr = pjmedia_sdp_media_find_attr2(stream, "fmtp", &rtpmap->pt))) {
182 if ((pjmedia_sdp_attr_get_fmtp(attr, &fmtp)) == PJ_SUCCESS) {
183 sscanf(pj_strbuf(&fmtp.fmt), "%d", &num);
184 if ((format = ast_rtp_codecs_get_payload_format(codecs, num))) {
185 struct ast_format *format_parsed;
187 ast_copy_pj_str(fmt_param, &fmtp.fmt_param, sizeof(fmt_param));
189 format_parsed = ast_format_parse_sdp_fmtp(format, fmt_param);
191 ast_rtp_codecs_payload_replace_format(codecs, num, format_parsed);
192 ao2_ref(format_parsed, -1);
200 /* Get the packetization, if it exists */
201 if ((attr = pjmedia_sdp_media_find_attr2(stream, "ptime", NULL))) {
202 unsigned long framing = pj_strtoul(pj_strltrim(&attr->value));
203 if (framing && session->endpoint->media.rtp.use_ptime) {
204 ast_rtp_codecs_set_framing(codecs, framing);
209 static int set_caps(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
210 const struct pjmedia_sdp_media *stream)
212 RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
213 RAII_VAR(struct ast_format_cap *, peer, NULL, ao2_cleanup);
214 RAII_VAR(struct ast_format_cap *, joint, NULL, ao2_cleanup);
215 enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
216 struct ast_rtp_codecs codecs = AST_RTP_CODECS_NULL_INIT;
218 int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) &&
219 ast_format_cap_count(session->direct_media_cap);
221 if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
222 !(peer = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
223 !(joint = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
224 ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n", session_media->stream_type);
228 /* get the endpoint capabilities */
229 if (direct_media_enabled) {
230 ast_format_cap_get_compatible(session->endpoint->media.codecs, session->direct_media_cap, caps);
231 format_cap_only_type(caps, media_type);
233 ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, media_type);
236 /* get the capabilities on the peer */
237 get_codecs(session, stream, &codecs);
238 ast_rtp_codecs_payload_formats(&codecs, peer, &fmts);
240 /* get the joint capabilities between peer and endpoint */
241 ast_format_cap_get_compatible(caps, peer, joint);
242 if (!ast_format_cap_count(joint)) {
243 struct ast_str *usbuf = ast_str_alloca(64);
244 struct ast_str *thembuf = ast_str_alloca(64);
246 ast_rtp_codecs_payloads_destroy(&codecs);
247 ast_log(LOG_NOTICE, "No joint capabilities for '%s' media stream between our configuration(%s) and incoming SDP(%s)\n",
248 session_media->stream_type,
249 ast_format_cap_get_names(caps, &usbuf),
250 ast_format_cap_get_names(peer, &thembuf));
254 ast_rtp_codecs_payloads_copy(&codecs, ast_rtp_instance_get_codecs(session_media->rtp),
257 ast_format_cap_append_from_cap(session->req_caps, joint, AST_MEDIA_TYPE_UNKNOWN);
259 if (session->channel) {
260 struct ast_format *fmt;
262 ast_channel_lock(session->channel);
263 ast_format_cap_remove_by_type(caps, AST_MEDIA_TYPE_UNKNOWN);
264 ast_format_cap_append_from_cap(caps, ast_channel_nativeformats(session->channel), AST_MEDIA_TYPE_UNKNOWN);
265 ast_format_cap_remove_by_type(caps, media_type);
268 * XXX Historically we picked the "best" joint format to use
269 * and stuck with it. It would be nice to just append the
270 * determined joint media capabilities to give translation
271 * more formats to choose from when necessary. Unfortunately,
272 * there are some areas of the system where this doesn't work
273 * very well. (The softmix bridge in particular is reluctant
274 * to pick higher fidelity formats and has a problem with
275 * asymmetric sample rates.)
277 fmt = ast_format_cap_get_format(joint, 0);
278 ast_format_cap_append(caps, fmt, 0);
281 * Apply the new formats to the channel, potentially changing
282 * raw read/write formats and translation path while doing so.
284 ast_channel_nativeformats_set(session->channel, caps);
285 ast_set_read_format(session->channel, ast_channel_readformat(session->channel));
286 ast_set_write_format(session->channel, ast_channel_writeformat(session->channel));
287 ast_channel_unlock(session->channel);
292 ast_rtp_codecs_payloads_destroy(&codecs);
296 static pjmedia_sdp_attr* generate_rtpmap_attr(pjmedia_sdp_media *media, pj_pool_t *pool, int rtp_code,
297 int asterisk_format, struct ast_format *format, int code)
299 pjmedia_sdp_rtpmap rtpmap;
300 pjmedia_sdp_attr *attr = NULL;
303 snprintf(tmp, sizeof(tmp), "%d", rtp_code);
304 pj_strdup2(pool, &media->desc.fmt[media->desc.fmt_count++], tmp);
305 rtpmap.pt = media->desc.fmt[media->desc.fmt_count - 1];
306 rtpmap.clock_rate = ast_rtp_lookup_sample_rate2(asterisk_format, format, code);
307 pj_strdup2(pool, &rtpmap.enc_name, ast_rtp_lookup_mime_subtype2(asterisk_format, format, code, 0));
308 rtpmap.param.slen = 0;
309 rtpmap.param.ptr = NULL;
311 pjmedia_sdp_rtpmap_to_attr(pool, &rtpmap, &attr);
316 static pjmedia_sdp_attr* generate_fmtp_attr(pj_pool_t *pool, struct ast_format *format, int rtp_code)
318 struct ast_str *fmtp0 = ast_str_alloca(256);
320 pjmedia_sdp_attr *attr = NULL;
323 ast_format_generate_sdp_fmtp(format, rtp_code, &fmtp0);
324 if (ast_str_strlen(fmtp0)) {
325 tmp = ast_str_buffer(fmtp0) + ast_str_strlen(fmtp0) - 1;
326 /* remove any carriage return line feeds */
327 while (*tmp == '\r' || *tmp == '\n') --tmp;
329 /* ast...generate gives us everything, just need value */
330 tmp = strchr(ast_str_buffer(fmtp0), ':');
331 if (tmp && tmp + 1) {
332 fmtp1 = pj_str(tmp + 1);
334 fmtp1 = pj_str(ast_str_buffer(fmtp0));
336 attr = pjmedia_sdp_attr_create(pool, "fmtp", &fmtp1);
341 /*! \brief Function which adds ICE attributes to a media stream */
342 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)
344 struct ast_rtp_engine_ice *ice;
345 struct ao2_container *candidates;
346 const char *username, *password;
348 pjmedia_sdp_attr *attr;
349 struct ao2_iterator it_candidates;
350 struct ast_rtp_engine_ice_candidate *candidate;
352 if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp)) ||
353 !(candidates = ice->get_local_candidates(session_media->rtp))) {
357 if ((username = ice->get_ufrag(session_media->rtp))) {
358 attr = pjmedia_sdp_attr_create(pool, "ice-ufrag", pj_cstr(&stmp, username));
359 media->attr[media->attr_count++] = attr;
362 if ((password = ice->get_password(session_media->rtp))) {
363 attr = pjmedia_sdp_attr_create(pool, "ice-pwd", pj_cstr(&stmp, password));
364 media->attr[media->attr_count++] = attr;
367 it_candidates = ao2_iterator_init(candidates, 0);
368 for (; (candidate = ao2_iterator_next(&it_candidates)); ao2_ref(candidate, -1)) {
369 struct ast_str *attr_candidate = ast_str_create(128);
371 ast_str_set(&attr_candidate, -1, "%s %u %s %d %s ", candidate->foundation, candidate->id, candidate->transport,
372 candidate->priority, ast_sockaddr_stringify_addr_remote(&candidate->address));
373 ast_str_append(&attr_candidate, -1, "%s typ ", ast_sockaddr_stringify_port(&candidate->address));
375 switch (candidate->type) {
376 case AST_RTP_ICE_CANDIDATE_TYPE_HOST:
377 ast_str_append(&attr_candidate, -1, "host");
379 case AST_RTP_ICE_CANDIDATE_TYPE_SRFLX:
380 ast_str_append(&attr_candidate, -1, "srflx");
382 case AST_RTP_ICE_CANDIDATE_TYPE_RELAYED:
383 ast_str_append(&attr_candidate, -1, "relay");
387 if (!ast_sockaddr_isnull(&candidate->relay_address)) {
388 ast_str_append(&attr_candidate, -1, " raddr %s rport", ast_sockaddr_stringify_addr_remote(&candidate->relay_address));
389 ast_str_append(&attr_candidate, -1, " %s", ast_sockaddr_stringify_port(&candidate->relay_address));
392 attr = pjmedia_sdp_attr_create(pool, "candidate", pj_cstr(&stmp, ast_str_buffer(attr_candidate)));
393 media->attr[media->attr_count++] = attr;
395 ast_free(attr_candidate);
398 ao2_iterator_destroy(&it_candidates);
399 ao2_ref(candidates, -1);
402 /*! \brief Function which processes ICE attributes in an audio stream */
403 static void process_ice_attributes(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
404 const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream)
406 struct ast_rtp_engine_ice *ice;
407 const pjmedia_sdp_attr *attr;
408 char attr_value[256];
411 /* If ICE support is not enabled or available exit early */
412 if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp))) {
416 attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-ufrag", NULL);
418 attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-ufrag", NULL);
421 ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
422 ice->set_authentication(session_media->rtp, attr_value, NULL);
427 attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-pwd", NULL);
429 attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-pwd", NULL);
432 ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
433 ice->set_authentication(session_media->rtp, NULL, attr_value);
438 if (pjmedia_sdp_media_find_attr2(remote_stream, "ice-lite", NULL)) {
439 ice->ice_lite(session_media->rtp);
442 /* Find all of the candidates */
443 for (attr_i = 0; attr_i < remote_stream->attr_count; ++attr_i) {
444 char foundation[32], transport[32], address[PJ_INET6_ADDRSTRLEN + 1], cand_type[6], relay_address[PJ_INET6_ADDRSTRLEN + 1] = "";
445 unsigned int port, relay_port = 0;
446 struct ast_rtp_engine_ice_candidate candidate = { 0, };
448 attr = remote_stream->attr[attr_i];
450 /* If this is not a candidate line skip it */
451 if (pj_strcmp2(&attr->name, "candidate")) {
455 ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
457 if (sscanf(attr_value, "%31s %30u %31s %30u %46s %30u typ %5s %*s %23s %*s %30u", foundation, &candidate.id, transport,
458 (unsigned *)&candidate.priority, address, &port, cand_type, relay_address, &relay_port) < 7) {
459 /* Candidate did not parse properly */
463 candidate.foundation = foundation;
464 candidate.transport = transport;
466 ast_sockaddr_parse(&candidate.address, address, PARSE_PORT_FORBID);
467 ast_sockaddr_set_port(&candidate.address, port);
469 if (!strcasecmp(cand_type, "host")) {
470 candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_HOST;
471 } else if (!strcasecmp(cand_type, "srflx")) {
472 candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX;
473 } else if (!strcasecmp(cand_type, "relay")) {
474 candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED;
479 if (!ast_strlen_zero(relay_address)) {
480 ast_sockaddr_parse(&candidate.relay_address, relay_address, PARSE_PORT_FORBID);
484 ast_sockaddr_set_port(&candidate.relay_address, relay_port);
487 ice->add_remote_candidate(session_media->rtp, &candidate);
490 ice->set_role(session_media->rtp, pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_TRUE ?
491 AST_RTP_ICE_ROLE_CONTROLLING : AST_RTP_ICE_ROLE_CONTROLLED);
492 ice->start(session_media->rtp);
495 /*! \brief figure out if media stream has crypto lines for sdes */
496 static int media_stream_has_crypto(const struct pjmedia_sdp_media *stream)
500 for (i = 0; i < stream->attr_count; i++) {
501 pjmedia_sdp_attr *attr;
503 /* check the stream for the required crypto attribute */
504 attr = stream->attr[i];
505 if (pj_strcmp2(&attr->name, "crypto")) {
515 /*! \brief figure out media transport encryption type from the media transport string */
516 static enum ast_sip_session_media_encryption get_media_encryption_type(pj_str_t transport,
517 const struct pjmedia_sdp_media *stream, unsigned int *optimistic)
519 RAII_VAR(char *, transport_str, ast_strndup(transport.ptr, transport.slen), ast_free);
523 if (strstr(transport_str, "UDP/TLS")) {
524 return AST_SIP_MEDIA_ENCRYPT_DTLS;
525 } else if (strstr(transport_str, "SAVP")) {
526 return AST_SIP_MEDIA_ENCRYPT_SDES;
527 } else if (media_stream_has_crypto(stream)) {
529 return AST_SIP_MEDIA_ENCRYPT_SDES;
531 return AST_SIP_MEDIA_ENCRYPT_NONE;
536 * \brief Checks whether the encryption offered in SDP is compatible with the endpoint's configuration
539 * \param endpoint_encryption Media encryption configured for the endpoint
540 * \param stream pjmedia_sdp_media stream description
542 * \retval AST_SIP_MEDIA_TRANSPORT_INVALID on encryption mismatch
543 * \retval The encryption requested in the SDP
545 static enum ast_sip_session_media_encryption check_endpoint_media_transport(
546 struct ast_sip_endpoint *endpoint,
547 const struct pjmedia_sdp_media *stream)
549 enum ast_sip_session_media_encryption incoming_encryption;
550 char transport_end = stream->desc.transport.ptr[stream->desc.transport.slen - 1];
551 unsigned int optimistic;
553 if ((transport_end == 'F' && !endpoint->media.rtp.use_avpf)
554 || (transport_end != 'F' && endpoint->media.rtp.use_avpf)) {
555 return AST_SIP_MEDIA_TRANSPORT_INVALID;
558 incoming_encryption = get_media_encryption_type(stream->desc.transport, stream, &optimistic);
560 if (incoming_encryption == endpoint->media.rtp.encryption) {
561 return incoming_encryption;
564 if (endpoint->media.rtp.force_avp ||
565 endpoint->media.rtp.encryption_optimistic) {
566 return incoming_encryption;
569 /* If an optimistic offer has been made but encryption is not enabled consider it as having
570 * no offer of crypto at all instead of invalid so the session proceeds.
573 return AST_SIP_MEDIA_ENCRYPT_NONE;
576 return AST_SIP_MEDIA_TRANSPORT_INVALID;
579 static int setup_srtp(struct ast_sip_session_media *session_media)
581 if (!session_media->srtp) {
582 session_media->srtp = ast_sdp_srtp_alloc();
583 if (!session_media->srtp) {
588 if (!session_media->srtp->crypto) {
589 session_media->srtp->crypto = ast_sdp_crypto_alloc();
590 if (!session_media->srtp->crypto) {
598 static int setup_dtls_srtp(struct ast_sip_session *session,
599 struct ast_sip_session_media *session_media)
601 struct ast_rtp_engine_dtls *dtls;
603 if (!session->endpoint->media.rtp.dtls_cfg.enabled || !session_media->rtp) {
607 dtls = ast_rtp_instance_get_dtls(session_media->rtp);
612 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);
613 if (dtls->set_configuration(session_media->rtp, &session->endpoint->media.rtp.dtls_cfg)) {
614 ast_log(LOG_ERROR, "Attempted to set an invalid DTLS-SRTP configuration on RTP instance '%p'\n",
619 if (setup_srtp(session_media)) {
625 static void apply_dtls_attrib(struct ast_sip_session_media *session_media,
626 pjmedia_sdp_attr *attr)
628 struct ast_rtp_engine_dtls *dtls = ast_rtp_instance_get_dtls(session_media->rtp);
631 if (!attr->value.ptr) {
635 value = pj_strtrim(&attr->value);
637 if (!pj_strcmp2(&attr->name, "setup")) {
638 if (!pj_stricmp2(value, "active")) {
639 dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_ACTIVE);
640 } else if (!pj_stricmp2(value, "passive")) {
641 dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_PASSIVE);
642 } else if (!pj_stricmp2(value, "actpass")) {
643 dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_ACTPASS);
644 } else if (!pj_stricmp2(value, "holdconn")) {
645 dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_HOLDCONN);
647 ast_log(LOG_WARNING, "Unsupported setup attribute value '%*s'\n", (int)value->slen, value->ptr);
649 } else if (!pj_strcmp2(&attr->name, "connection")) {
650 if (!pj_stricmp2(value, "new")) {
651 dtls->reset(session_media->rtp);
652 } else if (!pj_stricmp2(value, "existing")) {
655 ast_log(LOG_WARNING, "Unsupported connection attribute value '%*s'\n", (int)value->slen, value->ptr);
657 } else if (!pj_strcmp2(&attr->name, "fingerprint")) {
658 char hash_value[256], hash[32];
659 char fingerprint_text[value->slen + 1];
660 ast_copy_pj_str(fingerprint_text, value, sizeof(fingerprint_text));
661 if (sscanf(fingerprint_text, "%31s %255s", hash, hash_value) == 2) {
662 if (!strcasecmp(hash, "sha-1")) {
663 dtls->set_fingerprint(session_media->rtp, AST_RTP_DTLS_HASH_SHA1, hash_value);
664 } else if (!strcasecmp(hash, "sha-256")) {
665 dtls->set_fingerprint(session_media->rtp, AST_RTP_DTLS_HASH_SHA256, hash_value);
667 ast_log(LOG_WARNING, "Unsupported fingerprint hash type '%s'\n",
674 static int parse_dtls_attrib(struct ast_sip_session_media *session_media,
675 const struct pjmedia_sdp_session *sdp,
676 const struct pjmedia_sdp_media *stream)
680 for (i = 0; i < sdp->attr_count; i++) {
681 apply_dtls_attrib(session_media, sdp->attr[i]);
684 for (i = 0; i < stream->attr_count; i++) {
685 apply_dtls_attrib(session_media, stream->attr[i]);
688 ast_set_flag(session_media->srtp, AST_SRTP_CRYPTO_OFFER_OK);
693 static int setup_sdes_srtp(struct ast_sip_session_media *session_media,
694 const struct pjmedia_sdp_media *stream)
698 for (i = 0; i < stream->attr_count; i++) {
699 pjmedia_sdp_attr *attr;
700 RAII_VAR(char *, crypto_str, NULL, ast_free);
702 /* check the stream for the required crypto attribute */
703 attr = stream->attr[i];
704 if (pj_strcmp2(&attr->name, "crypto")) {
708 crypto_str = ast_strndup(attr->value.ptr, attr->value.slen);
713 if (setup_srtp(session_media)) {
717 if (!ast_sdp_crypto_process(session_media->rtp, session_media->srtp, crypto_str)) {
718 /* found a valid crypto attribute */
722 ast_debug(1, "Ignoring crypto offer with unsupported parameters: %s\n", crypto_str);
725 /* no usable crypto attributes found */
729 static int setup_media_encryption(struct ast_sip_session *session,
730 struct ast_sip_session_media *session_media,
731 const struct pjmedia_sdp_session *sdp,
732 const struct pjmedia_sdp_media *stream)
734 switch (session_media->encryption) {
735 case AST_SIP_MEDIA_ENCRYPT_SDES:
736 if (setup_sdes_srtp(session_media, stream)) {
740 case AST_SIP_MEDIA_ENCRYPT_DTLS:
741 if (setup_dtls_srtp(session, session_media)) {
744 if (parse_dtls_attrib(session_media, sdp, stream)) {
748 case AST_SIP_MEDIA_TRANSPORT_INVALID:
749 case AST_SIP_MEDIA_ENCRYPT_NONE:
756 /*! \brief Function which negotiates an incoming media stream */
757 static int negotiate_incoming_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
758 const struct pjmedia_sdp_session *sdp, const struct pjmedia_sdp_media *stream)
760 char host[NI_MAXHOST];
761 RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free);
762 enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
763 enum ast_sip_session_media_encryption encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
766 /* If port is 0, ignore this media stream */
767 if (!stream->desc.port) {
768 ast_debug(3, "Media stream '%s' is already declined\n", session_media->stream_type);
772 /* If no type formats have been configured reject this stream */
773 if (!ast_format_cap_has_type(session->endpoint->media.codecs, media_type)) {
774 ast_debug(3, "Endpoint has no codecs for media type '%s', declining stream\n", session_media->stream_type);
778 /* Ensure incoming transport is compatible with the endpoint's configuration */
779 if (!session->endpoint->media.rtp.use_received_transport) {
780 encryption = check_endpoint_media_transport(session->endpoint, stream);
782 if (encryption == AST_SIP_MEDIA_TRANSPORT_INVALID) {
787 ast_copy_pj_str(host, stream->conn ? &stream->conn->addr : &sdp->conn->addr, sizeof(host));
789 /* Ensure that the address provided is valid */
790 if (ast_sockaddr_resolve(&addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC) <= 0) {
791 /* The provided host was actually invalid so we error out this negotiation */
795 /* Using the connection information create an appropriate RTP instance */
796 if (!session_media->rtp && create_rtp(session, session_media, ast_sockaddr_is_ipv6(addrs))) {
800 res = setup_media_encryption(session, session_media, sdp, stream);
802 if (!session->endpoint->media.rtp.encryption_optimistic) {
803 /* If optimistic encryption is disabled and crypto should have been enabled
804 * but was not this session must fail.
808 /* There is no encryption, sad. */
809 session_media->encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
812 /* If we've been explicitly configured to use the received transport OR if
813 * encryption is on and crypto is present use the received transport.
814 * This is done in case of optimistic because it may come in as RTP/AVP or RTP/SAVP depending
815 * on the configuration of the remote endpoint (optimistic themselves or mandatory).
817 if ((session->endpoint->media.rtp.use_received_transport) ||
818 ((encryption == AST_SIP_MEDIA_ENCRYPT_SDES) && !res)) {
819 pj_strdup(session->inv_session->pool, &session_media->transport, &stream->desc.transport);
822 if (set_caps(session, session_media, stream)) {
828 static int add_crypto_to_stream(struct ast_sip_session *session,
829 struct ast_sip_session_media *session_media,
830 pj_pool_t *pool, pjmedia_sdp_media *media)
833 pjmedia_sdp_attr *attr;
834 enum ast_rtp_dtls_hash hash;
835 const char *crypto_attribute;
836 struct ast_rtp_engine_dtls *dtls;
837 static const pj_str_t STR_NEW = { "new", 3 };
838 static const pj_str_t STR_EXISTING = { "existing", 8 };
839 static const pj_str_t STR_ACTIVE = { "active", 6 };
840 static const pj_str_t STR_PASSIVE = { "passive", 7 };
841 static const pj_str_t STR_ACTPASS = { "actpass", 7 };
842 static const pj_str_t STR_HOLDCONN = { "holdconn", 8 };
844 switch (session_media->encryption) {
845 case AST_SIP_MEDIA_ENCRYPT_NONE:
846 case AST_SIP_MEDIA_TRANSPORT_INVALID:
848 case AST_SIP_MEDIA_ENCRYPT_SDES:
849 if (!session_media->srtp) {
850 session_media->srtp = ast_sdp_srtp_alloc();
851 if (!session_media->srtp) {
856 crypto_attribute = ast_sdp_srtp_get_attrib(session_media->srtp,
857 0 /* DTLS running? No */,
858 session->endpoint->media.rtp.srtp_tag_32 /* 32 byte tag length? */);
859 if (!crypto_attribute) {
860 /* No crypto attribute to add, bad news */
864 attr = pjmedia_sdp_attr_create(pool, "crypto", pj_cstr(&stmp, crypto_attribute));
865 media->attr[media->attr_count++] = attr;
867 case AST_SIP_MEDIA_ENCRYPT_DTLS:
868 if (setup_dtls_srtp(session, session_media)) {
872 dtls = ast_rtp_instance_get_dtls(session_media->rtp);
877 switch (dtls->get_connection(session_media->rtp)) {
878 case AST_RTP_DTLS_CONNECTION_NEW:
879 attr = pjmedia_sdp_attr_create(pool, "connection", &STR_NEW);
880 media->attr[media->attr_count++] = attr;
882 case AST_RTP_DTLS_CONNECTION_EXISTING:
883 attr = pjmedia_sdp_attr_create(pool, "connection", &STR_EXISTING);
884 media->attr[media->attr_count++] = attr;
890 switch (dtls->get_setup(session_media->rtp)) {
891 case AST_RTP_DTLS_SETUP_ACTIVE:
892 attr = pjmedia_sdp_attr_create(pool, "setup", &STR_ACTIVE);
893 media->attr[media->attr_count++] = attr;
895 case AST_RTP_DTLS_SETUP_PASSIVE:
896 attr = pjmedia_sdp_attr_create(pool, "setup", &STR_PASSIVE);
897 media->attr[media->attr_count++] = attr;
899 case AST_RTP_DTLS_SETUP_ACTPASS:
900 attr = pjmedia_sdp_attr_create(pool, "setup", &STR_ACTPASS);
901 media->attr[media->attr_count++] = attr;
903 case AST_RTP_DTLS_SETUP_HOLDCONN:
904 attr = pjmedia_sdp_attr_create(pool, "setup", &STR_HOLDCONN);
905 media->attr[media->attr_count++] = attr;
911 hash = dtls->get_fingerprint_hash(session_media->rtp);
912 crypto_attribute = dtls->get_fingerprint(session_media->rtp);
913 if (crypto_attribute && (hash == AST_RTP_DTLS_HASH_SHA1 || hash == AST_RTP_DTLS_HASH_SHA256)) {
914 RAII_VAR(struct ast_str *, fingerprint, ast_str_create(64), ast_free);
919 if (hash == AST_RTP_DTLS_HASH_SHA1) {
920 ast_str_set(&fingerprint, 0, "SHA-1 %s", crypto_attribute);
922 ast_str_set(&fingerprint, 0, "SHA-256 %s", crypto_attribute);
925 attr = pjmedia_sdp_attr_create(pool, "fingerprint", pj_cstr(&stmp, ast_str_buffer(fingerprint)));
926 media->attr[media->attr_count++] = attr;
934 /*! \brief Function which creates an outgoing stream */
935 static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
936 struct pjmedia_sdp_session *sdp)
938 pj_pool_t *pool = session->inv_session->pool_prov;
939 static const pj_str_t STR_IN = { "IN", 2 };
940 static const pj_str_t STR_IP4 = { "IP4", 3};
941 static const pj_str_t STR_IP6 = { "IP6", 3};
942 static const pj_str_t STR_SENDRECV = { "sendrecv", 8 };
943 static const pj_str_t STR_SENDONLY = { "sendonly", 8 };
944 pjmedia_sdp_media *media;
945 char hostip[PJ_INET6_ADDRSTRLEN+2];
946 struct ast_sockaddr addr;
949 pjmedia_sdp_attr *attr;
951 int noncodec = (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733) ? AST_RTP_DTMF : 0;
952 int min_packet_size = 0, max_packet_size = 0;
954 RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
955 enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
956 int use_override_prefs = ast_format_cap_count(session->req_caps);
958 int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) &&
959 ast_format_cap_count(session->direct_media_cap);
961 if ((use_override_prefs && !ast_format_cap_has_type(session->req_caps, media_type)) ||
962 (!use_override_prefs && !ast_format_cap_has_type(session->endpoint->media.codecs, media_type))) {
963 /* If no type formats are configured don't add a stream */
965 } else if (!session_media->rtp && create_rtp(session, session_media, session->endpoint->media.rtp.ipv6)) {
969 if (!(media = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_media))) ||
970 !(media->conn = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_conn)))) {
974 if (add_crypto_to_stream(session, session_media, pool, media)) {
978 media->desc.media = pj_str(session_media->stream_type);
979 if (pj_strlen(&session_media->transport)) {
980 /* If a transport has already been specified use it */
981 media->desc.transport = session_media->transport;
983 media->desc.transport = pj_str(ast_sdp_get_rtp_profile(
984 /* Optimistic encryption places crypto in the normal RTP/AVP profile */
985 !session->endpoint->media.rtp.encryption_optimistic &&
986 (session_media->encryption == AST_SIP_MEDIA_ENCRYPT_SDES),
987 session_media->rtp, session->endpoint->media.rtp.use_avpf,
988 session->endpoint->media.rtp.force_avp));
991 /* Add connection level details */
992 if (direct_media_enabled) {
993 ast_copy_string(hostip, ast_sockaddr_stringify_fmt(&session_media->direct_media_addr, AST_SOCKADDR_STR_ADDR), sizeof(hostip));
994 } else if (ast_strlen_zero(session->endpoint->media.address)) {
995 pj_sockaddr localaddr;
997 if (pj_gethostip(session->endpoint->media.rtp.ipv6 ? pj_AF_INET6() : pj_AF_INET(), &localaddr)) {
1000 pj_sockaddr_print(&localaddr, hostip, sizeof(hostip), 2);
1002 ast_copy_string(hostip, session->endpoint->media.address, sizeof(hostip));
1005 media->conn->net_type = STR_IN;
1006 media->conn->addr_type = session->endpoint->media.rtp.ipv6 ? STR_IP6 : STR_IP4;
1007 pj_strdup2(pool, &media->conn->addr, hostip);
1008 ast_rtp_instance_get_local_address(session_media->rtp, &addr);
1009 media->desc.port = direct_media_enabled ? ast_sockaddr_port(&session_media->direct_media_addr) : (pj_uint16_t) ast_sockaddr_port(&addr);
1010 media->desc.port_count = 1;
1012 /* Add ICE attributes and candidates */
1013 add_ice_to_stream(session, session_media, pool, media);
1015 if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
1016 ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n", session_media->stream_type);
1020 if (direct_media_enabled) {
1021 ast_format_cap_get_compatible(session->endpoint->media.codecs, session->direct_media_cap, caps);
1022 } else if (!ast_format_cap_count(session->req_caps) ||
1023 !ast_format_cap_iscompatible(session->req_caps, session->endpoint->media.codecs)) {
1024 ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, media_type);
1026 ast_format_cap_append_from_cap(caps, session->req_caps, media_type);
1029 for (index = 0; index < ast_format_cap_count(caps); ++index) {
1030 struct ast_format *format = ast_format_cap_get_format(caps, index);
1032 if (ast_format_get_type(format) != media_type) {
1033 ao2_ref(format, -1);
1037 if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 1, format, 0)) == -1) {
1038 ast_log(LOG_WARNING,"Unable to get rtp codec payload code for %s\n", ast_format_get_name(format));
1039 ao2_ref(format, -1);
1043 if (!(attr = generate_rtpmap_attr(media, pool, rtp_code, 1, format, 0))) {
1044 ao2_ref(format, -1);
1047 media->attr[media->attr_count++] = attr;
1049 if ((attr = generate_fmtp_attr(pool, format, rtp_code))) {
1050 media->attr[media->attr_count++] = attr;
1053 if (ast_format_get_maximum_ms(format) &&
1054 ((ast_format_get_maximum_ms(format) < max_packet_size) || !max_packet_size)) {
1055 max_packet_size = ast_format_get_maximum_ms(format);
1057 ao2_ref(format, -1);
1060 /* Add non-codec formats */
1061 if (media_type != AST_MEDIA_TYPE_VIDEO) {
1062 for (index = 1LL; index <= AST_RTP_MAX; index <<= 1) {
1063 if (!(noncodec & index) || (rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp),
1064 0, NULL, index)) == -1) {
1068 if (!(attr = generate_rtpmap_attr(media, pool, rtp_code, 0, NULL, index))) {
1072 media->attr[media->attr_count++] = attr;
1074 if (index == AST_RTP_DTMF) {
1075 snprintf(tmp, sizeof(tmp), "%d 0-16", rtp_code);
1076 attr = pjmedia_sdp_attr_create(pool, "fmtp", pj_cstr(&stmp, tmp));
1077 media->attr[media->attr_count++] = attr;
1082 /* If no formats were actually added to the media stream don't add it to the SDP */
1083 if (!media->desc.fmt_count) {
1087 /* If ptime is set add it as an attribute */
1088 min_packet_size = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(session_media->rtp));
1089 if (!min_packet_size) {
1090 min_packet_size = ast_format_cap_get_framing(caps);
1092 if (min_packet_size) {
1093 snprintf(tmp, sizeof(tmp), "%d", min_packet_size);
1094 attr = pjmedia_sdp_attr_create(pool, "ptime", pj_cstr(&stmp, tmp));
1095 media->attr[media->attr_count++] = attr;
1098 if (max_packet_size) {
1099 snprintf(tmp, sizeof(tmp), "%d", max_packet_size);
1100 attr = pjmedia_sdp_attr_create(pool, "maxptime", pj_cstr(&stmp, tmp));
1101 media->attr[media->attr_count++] = attr;
1104 /* Add the sendrecv attribute - we purposely don't keep track because pjmedia-sdp will automatically change our offer for us */
1105 attr = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_attr);
1106 attr->name = !session_media->locally_held ? STR_SENDRECV : STR_SENDONLY;
1107 media->attr[media->attr_count++] = attr;
1109 /* Add the media stream to the SDP */
1110 sdp->media[sdp->media_count++] = media;
1115 static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
1116 const struct pjmedia_sdp_session *local, const struct pjmedia_sdp_media *local_stream,
1117 const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream)
1119 RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free);
1120 enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
1121 char host[NI_MAXHOST];
1124 if (!session->channel) {
1128 if (!local_stream->desc.port || !remote_stream->desc.port) {
1132 /* Ensure incoming transport is compatible with the endpoint's configuration */
1133 if (!session->endpoint->media.rtp.use_received_transport &&
1134 check_endpoint_media_transport(session->endpoint, remote_stream) == AST_SIP_MEDIA_TRANSPORT_INVALID) {
1138 /* Create an RTP instance if need be */
1139 if (!session_media->rtp && create_rtp(session, session_media, session->endpoint->media.rtp.ipv6)) {
1143 res = setup_media_encryption(session, session_media, remote, remote_stream);
1144 if (!session->endpoint->media.rtp.encryption_optimistic && res) {
1145 /* If optimistic encryption is disabled and crypto should have been enabled but was not
1146 * this session must fail.
1151 if (!remote_stream->conn && !remote->conn) {
1155 ast_copy_pj_str(host, remote_stream->conn ? &remote_stream->conn->addr : &remote->conn->addr, sizeof(host));
1157 /* Ensure that the address provided is valid */
1158 if (ast_sockaddr_resolve(&addrs, host, PARSE_PORT_FORBID, AST_AF_UNSPEC) <= 0) {
1159 /* The provided host was actually invalid so we error out this negotiation */
1163 /* Apply connection information to the RTP instance */
1164 ast_sockaddr_set_port(addrs, remote_stream->desc.port);
1165 ast_rtp_instance_set_remote_address(session_media->rtp, addrs);
1166 if (set_caps(session, session_media, local_stream)) {
1170 if ((fdno = media_type_to_fdno(media_type)) < 0) {
1173 ast_channel_set_fd(session->channel, fdno, ast_rtp_instance_fd(session_media->rtp, 0));
1174 ast_channel_set_fd(session->channel, fdno + 1, ast_rtp_instance_fd(session_media->rtp, 1));
1176 /* If ICE support is enabled find all the needed attributes */
1177 process_ice_attributes(session, session_media, remote, remote_stream);
1179 /* Ensure the RTP instance is active */
1180 ast_rtp_instance_activate(session_media->rtp);
1182 /* audio stream handles music on hold */
1183 if (media_type != AST_MEDIA_TYPE_AUDIO) {
1184 if ((pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_FALSE)
1185 && (session->inv_session->state == PJSIP_INV_STATE_CONFIRMED)) {
1186 ast_queue_control(session->channel, AST_CONTROL_UPDATE_RTP_PEER);
1191 if (ast_sockaddr_isnull(addrs) ||
1192 ast_sockaddr_is_any(addrs) ||
1193 pjmedia_sdp_media_find_attr2(remote_stream, "sendonly", NULL) ||
1194 pjmedia_sdp_media_find_attr2(remote_stream, "inactive", NULL)) {
1195 if (!session_media->remotely_held) {
1196 /* The remote side has put us on hold */
1197 ast_queue_hold(session->channel, session->endpoint->mohsuggest);
1198 ast_rtp_instance_stop(session_media->rtp);
1199 ast_queue_frame(session->channel, &ast_null_frame);
1200 session_media->remotely_held = 1;
1202 } else if (session_media->remotely_held) {
1203 /* The remote side has taken us off hold */
1204 ast_queue_unhold(session->channel);
1205 ast_queue_frame(session->channel, &ast_null_frame);
1206 session_media->remotely_held = 0;
1207 } else if ((pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_FALSE)
1208 && (session->inv_session->state == PJSIP_INV_STATE_CONFIRMED)) {
1209 ast_queue_control(session->channel, AST_CONTROL_UPDATE_RTP_PEER);
1212 /* This purposely resets the encryption to the configured in case it gets added later */
1213 session_media->encryption = session->endpoint->media.rtp.encryption;
1218 /*! \brief Function which updates the media stream with external media address, if applicable */
1219 static void change_outgoing_sdp_stream_media_address(pjsip_tx_data *tdata, struct pjmedia_sdp_media *stream, struct ast_sip_transport *transport)
1221 char host[NI_MAXHOST];
1222 struct ast_sockaddr addr = { { 0, } };
1224 /* If the stream has been rejected there will be no connection line */
1225 if (!stream->conn) {
1229 ast_copy_pj_str(host, &stream->conn->addr, sizeof(host));
1230 ast_sockaddr_parse(&addr, host, PARSE_PORT_FORBID);
1232 /* Is the address within the SDP inside the same network? */
1233 if (ast_apply_ha(transport->localnet, &addr) == AST_SENSE_ALLOW) {
1237 pj_strdup2(tdata->pool, &stream->conn->addr, transport->external_media_address);
1240 /*! \brief Function which destroys the RTP instance when session ends */
1241 static void stream_destroy(struct ast_sip_session_media *session_media)
1243 if (session_media->rtp) {
1244 ast_rtp_instance_stop(session_media->rtp);
1245 ast_rtp_instance_destroy(session_media->rtp);
1247 session_media->rtp = NULL;
1250 /*! \brief SDP handler for 'audio' media stream */
1251 static struct ast_sip_session_sdp_handler audio_sdp_handler = {
1253 .negotiate_incoming_sdp_stream = negotiate_incoming_sdp_stream,
1254 .create_outgoing_sdp_stream = create_outgoing_sdp_stream,
1255 .apply_negotiated_sdp_stream = apply_negotiated_sdp_stream,
1256 .change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address,
1257 .stream_destroy = stream_destroy,
1260 /*! \brief SDP handler for 'video' media stream */
1261 static struct ast_sip_session_sdp_handler video_sdp_handler = {
1263 .negotiate_incoming_sdp_stream = negotiate_incoming_sdp_stream,
1264 .create_outgoing_sdp_stream = create_outgoing_sdp_stream,
1265 .apply_negotiated_sdp_stream = apply_negotiated_sdp_stream,
1266 .change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address,
1267 .stream_destroy = stream_destroy,
1270 static int video_info_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
1272 struct pjsip_transaction *tsx;
1273 pjsip_tx_data *tdata;
1275 if (!session->channel
1276 || !ast_sip_is_content_type(&rdata->msg_info.msg->body->content_type,
1278 "media_control+xml")) {
1282 tsx = pjsip_rdata_get_tsx(rdata);
1284 ast_queue_control(session->channel, AST_CONTROL_VIDUPDATE);
1286 if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, 200, NULL, &tdata) == PJ_SUCCESS) {
1287 pjsip_dlg_send_response(session->inv_session->dlg, tsx, tdata);
1293 static struct ast_sip_session_supplement video_info_supplement = {
1295 .incoming_request = video_info_incoming_request,
1298 /*! \brief Unloads the sdp RTP/AVP module from Asterisk */
1299 static int unload_module(void)
1301 ast_sip_session_unregister_supplement(&video_info_supplement);
1302 ast_sip_session_unregister_sdp_handler(&video_sdp_handler, STR_VIDEO);
1303 ast_sip_session_unregister_sdp_handler(&audio_sdp_handler, STR_AUDIO);
1306 ast_sched_context_destroy(sched);
1313 * \brief Load the module
1315 * Module loading including tests for configuration or dependencies.
1316 * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
1317 * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
1318 * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
1319 * configuration file or other non-critical problem return
1320 * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
1322 static int load_module(void)
1324 CHECK_PJSIP_SESSION_MODULE_LOADED();
1326 ast_sockaddr_parse(&address_ipv4, "0.0.0.0", 0);
1327 ast_sockaddr_parse(&address_ipv6, "::", 0);
1329 if (!(sched = ast_sched_context_create())) {
1330 ast_log(LOG_ERROR, "Unable to create scheduler context.\n");
1334 if (ast_sched_start_thread(sched)) {
1335 ast_log(LOG_ERROR, "Unable to create scheduler context thread.\n");
1339 if (ast_sip_session_register_sdp_handler(&audio_sdp_handler, STR_AUDIO)) {
1340 ast_log(LOG_ERROR, "Unable to register SDP handler for %s stream type\n", STR_AUDIO);
1344 if (ast_sip_session_register_sdp_handler(&video_sdp_handler, STR_VIDEO)) {
1345 ast_log(LOG_ERROR, "Unable to register SDP handler for %s stream type\n", STR_VIDEO);
1349 ast_sip_session_register_supplement(&video_info_supplement);
1351 return AST_MODULE_LOAD_SUCCESS;
1355 return AST_MODULE_LOAD_FAILURE;
1358 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP SDP RTP/AVP stream handler",
1359 .support_level = AST_MODULE_SUPPORT_CORE,
1360 .load = load_module,
1361 .unload = unload_module,
1362 .load_pri = AST_MODPRI_CHANNEL_DRIVER,