Skip to content
Snippets Groups Projects
res_pjsip_sdp_rtp.c 73.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * Asterisk -- An open source telephony toolkit.
     *
     * Copyright (C) 2013, Digium, Inc.
     *
     * Joshua Colp <jcolp@digium.com>
     * Kevin Harwell <kharwell@digium.com>
     *
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
     * This program is free software, distributed under the terms of
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    /*! \file
     *
     * \author Joshua Colp <jcolp@digium.com>
     *
     * \brief SIP SDP media stream handling
     */
    
    /*** MODULEINFO
    	<depend>pjproject</depend>
    
    	<depend>res_pjsip</depend>
    	<depend>res_pjsip_session</depend>
    
    	<support_level>core</support_level>
     ***/
    
    #include "asterisk.h"
    
    #include <pjsip.h>
    #include <pjsip_ua.h>
    #include <pjmedia.h>
    #include <pjlib.h>
    
    
    #include "asterisk/utils.h"
    
    #include "asterisk/module.h"
    
    #include "asterisk/format.h"
    #include "asterisk/format_cap.h"
    
    #include "asterisk/rtp_engine.h"
    #include "asterisk/netsock2.h"
    #include "asterisk/channel.h"
    #include "asterisk/causes.h"
    #include "asterisk/sched.h"
    #include "asterisk/acl.h"
    
    #include "asterisk/sdp_srtp.h"
    
    #include "asterisk/dsp.h"
    
    #include "asterisk/linkedlists.h"       /* for AST_LIST_NEXT */
    
    #include "asterisk/stream.h"
    #include "asterisk/format_cache.h"
    
    #include "asterisk/res_pjsip.h"
    #include "asterisk/res_pjsip_session.h"
    
    
    /*! \brief Scheduler for RTCP purposes */
    static struct ast_sched_context *sched;
    
    
    /*! \brief Address for RTP */
    static struct ast_sockaddr address_rtp;
    
    
    static const char STR_AUDIO[] = "audio";
    static const char STR_VIDEO[] = "video";
    
    
    static int send_keepalive(const void *data)
    {
    	struct ast_sip_session_media *session_media = (struct ast_sip_session_media *) data;
    	struct ast_rtp_instance *rtp = session_media->rtp;
    	int keepalive;
    	time_t interval;
    	int send_keepalive;
    
    
    	keepalive = ast_rtp_instance_get_keepalive(rtp);
    
    	if (!ast_sockaddr_isnull(&session_media->direct_media_addr)) {
    		ast_debug(3, "Not sending RTP keepalive on RTP instance %p since direct media is in use\n", rtp);
    		return keepalive * 1000;
    	}
    
    	interval = time(NULL) - ast_rtp_instance_get_last_tx(rtp);
    	send_keepalive = interval >= keepalive;
    
    	ast_debug(3, "It has been %d seconds since RTP was last sent on instance %p. %sending keepalive\n",
    			(int) interval, rtp, send_keepalive ? "S" : "Not s");
    
    	if (send_keepalive) {
    		ast_rtp_instance_sendcng(rtp, 0);
    		return keepalive * 1000;
    	}
    
    	return (keepalive - interval) * 1000;
    }
    
    
    /*! \brief Check whether RTP is being received or not */
    static int rtp_check_timeout(const void *data)
    {
    	struct ast_sip_session_media *session_media = (struct ast_sip_session_media *)data;
    	struct ast_rtp_instance *rtp = session_media->rtp;
    	int elapsed;
    	struct ast_channel *chan;
    
    	if (!rtp) {
    		return 0;
    	}
    
    	elapsed = time(NULL) - ast_rtp_instance_get_last_rx(rtp);
    	if (elapsed < ast_rtp_instance_get_timeout(rtp)) {
    		return (ast_rtp_instance_get_timeout(rtp) - elapsed) * 1000;
    	}
    
    	chan = ast_channel_get_by_name(ast_rtp_instance_get_channel_id(rtp));
    	if (!chan) {
    		return 0;
    	}
    
    	ast_log(LOG_NOTICE, "Disconnecting channel '%s' for lack of RTP activity in %d seconds\n",
    		ast_channel_name(chan), elapsed);
    
    
    	ast_channel_lock(chan);
    	ast_channel_hangupcause_set(chan, AST_CAUSE_REQUESTED_CHAN_UNAVAIL);
    	ast_channel_unlock(chan);
    
    
    	ast_softhangup(chan, AST_SOFTHANGUP_DEV);
    	ast_channel_unref(chan);
    
    	return 0;
    }
    
    
    Mark Michelson's avatar
    Mark Michelson committed
    /*!
     * \brief Enable RTCP on an RTP session.
     */
    static void enable_rtcp(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
    	const struct pjmedia_sdp_media *remote_media)
    {
    	enum ast_rtp_instance_rtcp rtcp_type;
    
    	if (session->endpoint->media.rtcp_mux && session_media->remote_rtcp_mux) {
    		rtcp_type = AST_RTP_INSTANCE_RTCP_MUX;
    	} else {
    		rtcp_type = AST_RTP_INSTANCE_RTCP_STANDARD;
    	}
    
    	ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RTCP, rtcp_type);
    }
    
    
    /*!
     * \brief Enable an RTP extension on an RTP session.
     */
    static void enable_rtp_extension(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
    	enum ast_rtp_extension extension, enum ast_rtp_extension_direction direction,
    	const pjmedia_sdp_session *sdp)
    {
    	int id = -1;
    
    	/* For a bundle group the local unique identifier space is shared across all streams within
    	 * it.
    	 */
    	if (session_media->bundle_group != -1) {
    		int index;
    
    		for (index = 0; index < sdp->media_count; ++index) {
    			struct ast_sip_session_media *other_session_media;
    			int other_id;
    
    			if (index >= AST_VECTOR_SIZE(&session->pending_media_state->sessions)) {
    				break;
    			}
    
    			other_session_media = AST_VECTOR_GET(&session->pending_media_state->sessions, index);
    			if (!other_session_media->rtp || other_session_media->bundle_group != session_media->bundle_group) {
    				continue;
    			}
    
    			other_id = ast_rtp_instance_extmap_get_id(other_session_media->rtp, extension);
    			if (other_id == -1) {
    				/* Worst case we have to fall back to the highest available free local unique identifier
    				 * for the bundle group.
    				 */
    				other_id = ast_rtp_instance_extmap_count(other_session_media->rtp) + 1;
    				if (id < other_id) {
    					id = other_id;
    				}
    				continue;
    			}
    
    			id = other_id;
    			break;
    		}
    	}
    
    	ast_rtp_instance_extmap_enable(session_media->rtp, id, extension, direction);
    }
    
    
    /*! \brief Internal function which creates an RTP instance */
    
    static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
    	const pjmedia_sdp_session *sdp)
    
    {
    	struct ast_rtp_engine_ice *ice;
    
    	struct ast_sockaddr temp_media_address;
    
    	struct ast_sockaddr *media_address =  &address_rtp;
    
    	if (session->endpoint->media.bind_rtp_to_media_address && !ast_strlen_zero(session->endpoint->media.address)) {
    
    		if (ast_sockaddr_parse(&temp_media_address, session->endpoint->media.address, 0)) {
    			ast_debug(1, "Endpoint %s: Binding RTP media to %s\n",
    				ast_sorcery_object_get_id(session->endpoint),
    				session->endpoint->media.address);
    			media_address = &temp_media_address;
    		} else {
    			ast_debug(1, "Endpoint %s: RTP media address invalid: %s\n",
    				ast_sorcery_object_get_id(session->endpoint),
    				session->endpoint->media.address);
    		}
    
    		struct ast_sip_transport *transport;
    
    		transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport",
    			session->endpoint->transport);
    		if (transport) {
    			struct ast_sip_transport_state *trans_state;
    
    			trans_state = ast_sip_get_transport_state(ast_sorcery_object_get_id(transport));
    			if (trans_state) {
    				char hoststr[PJ_INET6_ADDRSTRLEN];
    
    				pj_sockaddr_print(&trans_state->host, hoststr, sizeof(hoststr), 0);
    				if (ast_sockaddr_parse(&temp_media_address, hoststr, 0)) {
    					ast_debug(1, "Transport %s bound to %s: Using it for RTP media.\n",
    						session->endpoint->transport, hoststr);
    					media_address = &temp_media_address;
    				} else {
    					ast_debug(1, "Transport %s bound to %s: Invalid for RTP media.\n",
    						session->endpoint->transport, hoststr);
    				}
    				ao2_ref(trans_state, -1);
    
    	}
    
    	if (!(session_media->rtp = ast_rtp_instance_new(session->endpoint->media.rtp.engine, sched, media_address, NULL))) {
    
    		ast_log(LOG_ERROR, "Unable to create RTP instance using RTP engine '%s'\n", session->endpoint->media.rtp.engine);
    
    	ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_NAT, session->endpoint->media.rtp.symmetric);
    
    	ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_ASYMMETRIC_CODEC, session->endpoint->asymmetric_rtp_codec);
    
    	if (!session->endpoint->media.rtp.ice_support && (ice = ast_rtp_instance_get_ice(session_media->rtp))) {
    
    		ice->stop(session_media->rtp);
    	}
    
    
    	if (session->dtmf == AST_SIP_DTMF_RFC_4733 || session->dtmf == AST_SIP_DTMF_AUTO || session->dtmf == AST_SIP_DTMF_AUTO_INFO) {
    
    		ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_RFC2833);
    
    		ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_DTMF, 1);
    
    	} else if (session->dtmf == AST_SIP_DTMF_INBAND) {
    
    		ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND);
    	}
    
    
    	if (session_media->type == AST_MEDIA_TYPE_AUDIO &&
    
    			(session->endpoint->media.tos_audio || session->endpoint->media.cos_audio)) {
    
    		ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_audio,
    				session->endpoint->media.cos_audio, "SIP RTP Audio");
    
    	} else if (session_media->type == AST_MEDIA_TYPE_VIDEO) {
    		ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RETRANS_RECV, session->endpoint->media.webrtc);
    		ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RETRANS_SEND, session->endpoint->media.webrtc);
    
    		ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_REMB, session->endpoint->media.webrtc);
    
    		if (session->endpoint->media.webrtc) {
    			enable_rtp_extension(session, session_media, AST_RTP_EXTENSION_ABS_SEND_TIME, AST_RTP_EXTENSION_DIRECTION_SENDRECV, sdp);
    		}
    
    		if (session->endpoint->media.tos_video || session->endpoint->media.cos_video) {
    			ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_video,
    					session->endpoint->media.cos_video, "SIP RTP Video");
    		}
    
    	ast_rtp_instance_set_last_rx(session_media->rtp, time(NULL));
    
    
    static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp_media *stream, struct ast_rtp_codecs *codecs,
    
    	struct ast_sip_session_media *session_media)
    
    {
    	pjmedia_sdp_attr *attr;
    	pjmedia_sdp_rtpmap *rtpmap;
    	pjmedia_sdp_fmtp fmtp;
    	struct ast_format *format;
    
    	int i, num = 0, tel_event = 0;
    
    	char name[256];
    	char media[20];
    	char fmt_param[256];
    
    	enum ast_rtp_options options = session->endpoint->media.g726_non_standard ?
    		AST_RTP_OPT_G726_NONSTANDARD : 0;
    
    
    	ast_rtp_codecs_payloads_initialize(codecs);
    
    	/* Iterate through provided formats */
    	for (i = 0; i < stream->desc.fmt_count; ++i) {
    		/* The payload is kept as a string for things like t38 but for video it is always numerical */
    		ast_rtp_codecs_payloads_set_m_type(codecs, NULL, pj_strtoul(&stream->desc.fmt[i]));
    		/* Look for the optional rtpmap attribute */
    		if (!(attr = pjmedia_sdp_media_find_attr2(stream, "rtpmap", &stream->desc.fmt[i]))) {
    			continue;
    		}
    
    		/* Interpret the attribute as an rtpmap */
    		if ((pjmedia_sdp_attr_to_rtpmap(session->inv_session->pool_prov, attr, &rtpmap)) != PJ_SUCCESS) {
    			continue;
    		}
    
    		ast_copy_pj_str(name, &rtpmap->enc_name, sizeof(name));
    
    		if (strcmp(name, "telephone-event") == 0) {
    			tel_event++;
    		}
    
    		ast_copy_pj_str(media, (pj_str_t*)&stream->desc.media, sizeof(media));
    
    		ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, NULL,
    			pj_strtoul(&stream->desc.fmt[i]), media, name, options, rtpmap->clock_rate);
    
    		/* Look for an optional associated fmtp attribute */
    		if (!(attr = pjmedia_sdp_media_find_attr2(stream, "fmtp", &rtpmap->pt))) {
    			continue;
    		}
    
    		if ((pjmedia_sdp_attr_get_fmtp(attr, &fmtp)) == PJ_SUCCESS) {
    
    			ast_copy_pj_str(fmt_param, &fmtp.fmt, sizeof(fmt_param));
    			if (sscanf(fmt_param, "%30d", &num) != 1) {
    				continue;
    			}
    
    
    			if ((format = ast_rtp_codecs_get_payload_format(codecs, num))) {
    
    				ast_copy_pj_str(fmt_param, &fmtp.fmt_param, sizeof(fmt_param));
    
    
    				format_parsed = ast_format_parse_sdp_fmtp(format, fmt_param);
    				if (format_parsed) {
    					ast_rtp_codecs_payload_replace_format(codecs, num, format_parsed);
    					ao2_ref(format_parsed, -1);
    				}
    
    				ao2_ref(format, -1);
    
    	if (!tel_event && (session->dtmf == AST_SIP_DTMF_AUTO)) {
    
    		ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND);
    
    	if (session->dtmf == AST_SIP_DTMF_AUTO_INFO) {
    
    		if  (tel_event) {
    			ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_RFC2833);
    		} else {
    			ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_NONE);
    		}
    	}
    
    
    
    	/* Get the packetization, if it exists */
    	if ((attr = pjmedia_sdp_media_find_attr2(stream, "ptime", NULL))) {
    		unsigned long framing = pj_strtoul(pj_strltrim(&attr->value));
    		if (framing && session->endpoint->media.rtp.use_ptime) {
    			ast_rtp_codecs_set_framing(codecs, framing);
    		}
    	}
    
    static int set_caps(struct ast_sip_session *session,
    	struct ast_sip_session_media *session_media,
    
    	struct ast_sip_session_media *session_media_transport,
    
    	int is_offer, struct ast_stream *asterisk_stream)
    
    	RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
    	RAII_VAR(struct ast_format_cap *, peer, NULL, ao2_cleanup);
    	RAII_VAR(struct ast_format_cap *, joint, NULL, ao2_cleanup);
    
    	RAII_VAR(struct ast_format_cap *, endpoint_caps, NULL, ao2_cleanup);
    	enum ast_media_type media_type = session_media->type;
    
    	struct ast_rtp_codecs codecs = AST_RTP_CODECS_NULL_INIT;
    
    	int fmts = 0;
    	int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) &&
    
    		ast_format_cap_count(session->direct_media_cap);
    
    	int dsp_features = 0;
    
    	if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
    	    !(peer = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
    	    !(joint = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
    
    		ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n",
    			ast_codec_media_type2str(session_media->type));
    
    		return -1;
    	}
    
    	/* get the endpoint capabilities */
    	if (direct_media_enabled) {
    
    		ast_format_cap_get_compatible(session->endpoint->media.codecs, session->direct_media_cap, caps);
    
    		ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, media_type);
    
    	}
    
    	/* get the capabilities on the peer */
    
    	get_codecs(session, stream, &codecs,  session_media);
    
    	ast_rtp_codecs_payload_formats(&codecs, peer, &fmts);
    
    	/* get the joint capabilities between peer and endpoint */
    
    	ast_format_cap_get_compatible(caps, peer, joint);
    	if (!ast_format_cap_count(joint)) {
    
    		struct ast_str *usbuf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
    		struct ast_str *thembuf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
    
    
    		ast_rtp_codecs_payloads_destroy(&codecs);
    
    		ast_log(LOG_NOTICE, "No joint capabilities for '%s' media stream between our configuration(%s) and incoming SDP(%s)\n",
    
    			ast_codec_media_type2str(session_media->type),
    
    			ast_format_cap_get_names(caps, &usbuf),
    			ast_format_cap_get_names(peer, &thembuf));
    
    	if (is_offer) {
    		/*
    		 * Setup rx payload type mapping to prefer the mapping
    		 * from the peer that the RFC says we SHOULD use.
    		 */
    		ast_rtp_codecs_payloads_xover(&codecs, &codecs, NULL);
    	}
    
    	ast_rtp_codecs_payloads_copy(&codecs, ast_rtp_instance_get_codecs(session_media->rtp),
    
    		session_media->rtp);
    
    	ast_stream_set_formats(asterisk_stream, joint);
    
    	/* If this is a bundled stream then apply the payloads to RTP instance acting as transport to prevent conflicts */
    	if (session_media_transport != session_media && session_media->bundled) {
    		int index;
    
    		for (index = 0; index < ast_format_cap_count(joint); ++index) {
    			struct ast_format *format = ast_format_cap_get_format(joint, index);
    			int rtp_code;
    
    			/* Ensure this payload is in the bundle group transport codecs, this purposely doesn't check the return value for
    			 * things as the format is guaranteed to have a payload already.
    			 */
    			rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 1, format, 0);
    			ast_rtp_codecs_payload_set_rx(ast_rtp_instance_get_codecs(session_media_transport->rtp), rtp_code, format);
    
    			ao2_ref(format, -1);
    		}
    	}
    
    
    	if (session->channel && ast_sip_session_is_pending_stream_default(session, asterisk_stream)) {
    
    		ast_channel_lock(session->channel);
    
    		ast_format_cap_remove_by_type(caps, AST_MEDIA_TYPE_UNKNOWN);
    
    		ast_format_cap_append_from_cap(caps, ast_channel_nativeformats(session->channel),
    			AST_MEDIA_TYPE_UNKNOWN);
    
    		ast_format_cap_remove_by_type(caps, media_type);
    
    		if (session->endpoint->preferred_codec_only){
    			struct ast_format *preferred_fmt = ast_format_cap_get_format(joint, 0);
    			ast_format_cap_append(caps, preferred_fmt, 0);
    			ao2_ref(preferred_fmt, -1);
    
    		} else if (!session->endpoint->asymmetric_rtp_codec) {
    			struct ast_format *best;
    			/*
    			 * If we don't allow the sending codec to be changed on our side
    			 * then get the best codec from the joint capabilities of the media
    			 * type and use only that. This ensures the core won't start sending
    			 * out a format that we aren't currently sending.
    			 */
    
    			best = ast_format_cap_get_best_by_type(joint, media_type);
    			if (best) {
    				ast_format_cap_append(caps, best, ast_format_cap_get_framing(joint));
    				ao2_ref(best, -1);
    			}
    
    		} else {
    			ast_format_cap_append_from_cap(caps, joint, media_type);
    		}
    
    		/*
    		 * Apply the new formats to the channel, potentially changing
    		 * raw read/write formats and translation path while doing so.
    		 */
    
    		ast_channel_nativeformats_set(session->channel, caps);
    
    		if (media_type == AST_MEDIA_TYPE_AUDIO) {
    			ast_set_read_format(session->channel, ast_channel_readformat(session->channel));
    			ast_set_write_format(session->channel, ast_channel_writeformat(session->channel));
    		}
    
    		if ( ((session->dtmf == AST_SIP_DTMF_AUTO) || (session->dtmf == AST_SIP_DTMF_AUTO_INFO) )
    
    		    && (ast_rtp_instance_dtmf_mode_get(session_media->rtp) == AST_RTP_DTMF_MODE_RFC2833)
    		    && (session->dsp)) {
    			dsp_features = ast_dsp_get_features(session->dsp);
    			dsp_features &= ~DSP_FEATURE_DIGIT_DETECT;
    			if (dsp_features) {
    				ast_dsp_set_features(session->dsp, dsp_features);
    			} else {
    				ast_dsp_free(session->dsp);
    				session->dsp = NULL;
    			}
    		}
    
    
    		if (ast_channel_is_bridged(session->channel)) {
    			ast_channel_set_unbridged_nolock(session->channel, 1);
    		}
    
    
    		ast_channel_unlock(session->channel);
    
    	}
    
    	ast_rtp_codecs_payloads_destroy(&codecs);
    
    static pjmedia_sdp_attr* generate_rtpmap_attr(struct ast_sip_session *session, pjmedia_sdp_media *media, pj_pool_t *pool,
    					      int rtp_code, int asterisk_format, struct ast_format *format, int code)
    
    Joshua Colp's avatar
    Joshua Colp committed
    #ifndef HAVE_PJSIP_ENDPOINT_COMPACT_FORM
    
    	extern pj_bool_t pjsip_use_compact_form;
    
    Joshua Colp's avatar
    Joshua Colp committed
    #else
    	pj_bool_t pjsip_use_compact_form = pjsip_cfg()->endpt.use_compact_form;
    #endif
    
    	pjmedia_sdp_rtpmap rtpmap;
    	pjmedia_sdp_attr *attr = NULL;
    	char tmp[64];
    
    	enum ast_rtp_options options = session->endpoint->media.g726_non_standard ?
    		AST_RTP_OPT_G726_NONSTANDARD : 0;
    
    
    	snprintf(tmp, sizeof(tmp), "%d", rtp_code);
    	pj_strdup2(pool, &media->desc.fmt[media->desc.fmt_count++], tmp);
    
    
    	if (rtp_code <= AST_RTP_PT_LAST_STATIC && pjsip_use_compact_form) {
    		return NULL;
    	}
    
    
    	rtpmap.pt = media->desc.fmt[media->desc.fmt_count - 1];
    	rtpmap.clock_rate = ast_rtp_lookup_sample_rate2(asterisk_format, format, code);
    
    	pj_strdup2(pool, &rtpmap.enc_name, ast_rtp_lookup_mime_subtype2(asterisk_format, format, code, options));
    
    	if (!pj_stricmp2(&rtpmap.enc_name, "opus")) {
    		pj_cstr(&rtpmap.param, "2");
    	} else {
    		pj_cstr(&rtpmap.param, NULL);
    	}
    
    
    	pjmedia_sdp_rtpmap_to_attr(pool, &rtpmap, &attr);
    
    	return attr;
    }
    
    static pjmedia_sdp_attr* generate_fmtp_attr(pj_pool_t *pool, struct ast_format *format, int rtp_code)
    {
    	struct ast_str *fmtp0 = ast_str_alloca(256);
    	pj_str_t fmtp1;
    	pjmedia_sdp_attr *attr = NULL;
    	char *tmp;
    
    
    	ast_format_generate_sdp_fmtp(format, rtp_code, &fmtp0);
    
    	if (ast_str_strlen(fmtp0)) {
    		tmp = ast_str_buffer(fmtp0) + ast_str_strlen(fmtp0) - 1;
    		/* remove any carriage return line feeds */
    		while (*tmp == '\r' || *tmp == '\n') --tmp;
    		*++tmp = '\0';
    		/* ast...generate gives us everything, just need value */
    		tmp = strchr(ast_str_buffer(fmtp0), ':');
    
    		if (tmp && tmp[1] != '\0') {
    
    			fmtp1 = pj_str(tmp + 1);
    		} else {
    			fmtp1 = pj_str(ast_str_buffer(fmtp0));
    		}
    		attr = pjmedia_sdp_attr_create(pool, "fmtp", &fmtp1);
    	}
    	return attr;
    }
    
    /*! \brief Function which adds ICE attributes to a media stream */
    
    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,
    	unsigned int include_candidates)
    
    {
    	struct ast_rtp_engine_ice *ice;
    	struct ao2_container *candidates;
    	const char *username, *password;
    	pj_str_t stmp;
    	pjmedia_sdp_attr *attr;
    	struct ao2_iterator it_candidates;
    	struct ast_rtp_engine_ice_candidate *candidate;
    
    
    	if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp))) {
    
    	if (!session_media->remote_ice) {
    		return;
    	}
    
    
    	if ((username = ice->get_ufrag(session_media->rtp))) {
    		attr = pjmedia_sdp_attr_create(pool, "ice-ufrag", pj_cstr(&stmp, username));
    		media->attr[media->attr_count++] = attr;
    	}
    
    	if ((password = ice->get_password(session_media->rtp))) {
    		attr = pjmedia_sdp_attr_create(pool, "ice-pwd", pj_cstr(&stmp, password));
    		media->attr[media->attr_count++] = attr;
    	}
    
    
    	if (!include_candidates) {
    		return;
    	}
    
    	candidates = ice->get_local_candidates(session_media->rtp);
    	if (!candidates) {
    		return;
    	}
    
    
    	it_candidates = ao2_iterator_init(candidates, 0);
    	for (; (candidate = ao2_iterator_next(&it_candidates)); ao2_ref(candidate, -1)) {
    		struct ast_str *attr_candidate = ast_str_create(128);
    
    
    		ast_str_set(&attr_candidate, -1, "%s %u %s %d %s ", candidate->foundation, candidate->id, candidate->transport,
    
    					candidate->priority, ast_sockaddr_stringify_addr_remote(&candidate->address));
    
    		ast_str_append(&attr_candidate, -1, "%s typ ", ast_sockaddr_stringify_port(&candidate->address));
    
    		switch (candidate->type) {
    			case AST_RTP_ICE_CANDIDATE_TYPE_HOST:
    				ast_str_append(&attr_candidate, -1, "host");
    				break;
    			case AST_RTP_ICE_CANDIDATE_TYPE_SRFLX:
    				ast_str_append(&attr_candidate, -1, "srflx");
    				break;
    			case AST_RTP_ICE_CANDIDATE_TYPE_RELAYED:
    				ast_str_append(&attr_candidate, -1, "relay");
    				break;
    		}
    
    		if (!ast_sockaddr_isnull(&candidate->relay_address)) {
    
    			ast_str_append(&attr_candidate, -1, " raddr %s rport", ast_sockaddr_stringify_addr_remote(&candidate->relay_address));
    
    			ast_str_append(&attr_candidate, -1, " %s", ast_sockaddr_stringify_port(&candidate->relay_address));
    		}
    
    		attr = pjmedia_sdp_attr_create(pool, "candidate", pj_cstr(&stmp, ast_str_buffer(attr_candidate)));
    		media->attr[media->attr_count++] = attr;
    
    		ast_free(attr_candidate);
    	}
    
    	ao2_iterator_destroy(&it_candidates);
    
    /*! \brief Function which checks for ice attributes in an audio stream */
    static void check_ice_support(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
    				   const struct pjmedia_sdp_media *remote_stream)
    {
    	struct ast_rtp_engine_ice *ice;
    	const pjmedia_sdp_attr *attr;
    	unsigned int attr_i;
    
    	if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp))) {
    		session_media->remote_ice = 0;
    		return;
    	}
    
    	/* Find all of the candidates */
    	for (attr_i = 0; attr_i < remote_stream->attr_count; ++attr_i) {
    		attr = remote_stream->attr[attr_i];
    		if (!pj_strcmp2(&attr->name, "candidate")) {
    			session_media->remote_ice = 1;
    			break;
    		}
    	}
    
    	if (attr_i == remote_stream->attr_count) {
    		session_media->remote_ice = 0;
    	}
    }
    
    
    /*! \brief Function which processes ICE attributes in an audio stream */
    static void process_ice_attributes(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
    				   const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream)
    {
    	struct ast_rtp_engine_ice *ice;
    	const pjmedia_sdp_attr *attr;
    	char attr_value[256];
    	unsigned int attr_i;
    
    	/* If ICE support is not enabled or available exit early */
    
    	if (!session->endpoint->media.rtp.ice_support || !(ice = ast_rtp_instance_get_ice(session_media->rtp))) {
    
    	attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-ufrag", NULL);
    	if (!attr) {
    		attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-ufrag", NULL);
    	}
    	if (attr) {
    
    		ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
    		ice->set_authentication(session_media->rtp, attr_value, NULL);
    
    	attr = pjmedia_sdp_media_find_attr2(remote_stream, "ice-pwd", NULL);
    	if (!attr) {
    
    		attr = pjmedia_sdp_attr_find2(remote->attr_count, remote->attr, "ice-pwd", NULL);
    
    		ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
    		ice->set_authentication(session_media->rtp, NULL, attr_value);
    
    	}
    
    	if (pjmedia_sdp_media_find_attr2(remote_stream, "ice-lite", NULL)) {
    		ice->ice_lite(session_media->rtp);
    	}
    
    	/* Find all of the candidates */
    	for (attr_i = 0; attr_i < remote_stream->attr_count; ++attr_i) {
    
    		char foundation[33], transport[32], address[PJ_INET6_ADDRSTRLEN + 1], cand_type[6], relay_address[PJ_INET6_ADDRSTRLEN + 1] = "";
    
    		unsigned int port, relay_port = 0;
    
    		struct ast_rtp_engine_ice_candidate candidate = { 0, };
    
    		attr = remote_stream->attr[attr_i];
    
    		/* If this is not a candidate line skip it */
    		if (pj_strcmp2(&attr->name, "candidate")) {
    			continue;
    		}
    
    		ast_copy_pj_str(attr_value, (pj_str_t*)&attr->value, sizeof(attr_value));
    
    
    		if (sscanf(attr_value, "%32s %30u %31s %30u %46s %30u typ %5s %*s %23s %*s %30u", foundation, &candidate.id, transport,
    
    			(unsigned *)&candidate.priority, address, &port, cand_type, relay_address, &relay_port) < 7) {
    
    			/* Candidate did not parse properly */
    			continue;
    		}
    
    
    Mark Michelson's avatar
    Mark Michelson committed
    		if (session->endpoint->media.rtcp_mux && session_media->remote_rtcp_mux && candidate.id > 1) {
    			/* Remote side may have offered RTP and RTCP candidates. However, if we're using RTCP MUX,
    			 * then we should ignore RTCP candidates.
    			 */
    			continue;
    		}
    
    
    		candidate.foundation = foundation;
    		candidate.transport = transport;
    
    		ast_sockaddr_parse(&candidate.address, address, PARSE_PORT_FORBID);
    		ast_sockaddr_set_port(&candidate.address, port);
    
    		if (!strcasecmp(cand_type, "host")) {
    			candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_HOST;
    		} else if (!strcasecmp(cand_type, "srflx")) {
    			candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX;
    		} else if (!strcasecmp(cand_type, "relay")) {
    			candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED;
    		} else {
    			continue;
    		}
    
    		if (!ast_strlen_zero(relay_address)) {
    			ast_sockaddr_parse(&candidate.relay_address, relay_address, PARSE_PORT_FORBID);
    		}
    
    		if (relay_port) {
    			ast_sockaddr_set_port(&candidate.relay_address, relay_port);
    		}
    
    		ice->add_remote_candidate(session_media->rtp, &candidate);
    	}
    
    
    	ice->set_role(session_media->rtp, pjmedia_sdp_neg_was_answer_remote(session->inv_session->neg) == PJ_TRUE ?
    		AST_RTP_ICE_ROLE_CONTROLLING : AST_RTP_ICE_ROLE_CONTROLLED);
    
    	ice->start(session_media->rtp);
    }
    
    
    /*! \brief figure out if media stream has crypto lines for sdes */
    static int media_stream_has_crypto(const struct pjmedia_sdp_media *stream)
    {
    	int i;
    
    	for (i = 0; i < stream->attr_count; i++) {
    		pjmedia_sdp_attr *attr;
    
    		/* check the stream for the required crypto attribute */
    		attr = stream->attr[i];
    		if (pj_strcmp2(&attr->name, "crypto")) {
    			continue;
    		}
    
    		return 1;
    	}
    
    	return 0;
    }
    
    
    /*! \brief figure out media transport encryption type from the media transport string */
    
    static enum ast_sip_session_media_encryption get_media_encryption_type(pj_str_t transport,
    	const struct pjmedia_sdp_media *stream, unsigned int *optimistic)
    
    {
    	RAII_VAR(char *, transport_str, ast_strndup(transport.ptr, transport.slen), ast_free);
    
    	if (!transport_str) {
    		return AST_SIP_MEDIA_TRANSPORT_INVALID;
    	}
    
    	if (strstr(transport_str, "UDP/TLS")) {
    		return AST_SIP_MEDIA_ENCRYPT_DTLS;
    	} else if (strstr(transport_str, "SAVP")) {
    		return AST_SIP_MEDIA_ENCRYPT_SDES;
    
    	} else if (media_stream_has_crypto(stream)) {
    		*optimistic = 1;
    		return AST_SIP_MEDIA_ENCRYPT_SDES;
    
    	} else {
    		return AST_SIP_MEDIA_ENCRYPT_NONE;
    	}
    }
    
    /*!
     * \brief Checks whether the encryption offered in SDP is compatible with the endpoint's configuration
     * \internal
     *
     * \param endpoint_encryption Media encryption configured for the endpoint
     * \param stream pjmedia_sdp_media stream description
     *
     * \retval AST_SIP_MEDIA_TRANSPORT_INVALID on encryption mismatch
     * \retval The encryption requested in the SDP
     */
    static enum ast_sip_session_media_encryption check_endpoint_media_transport(
    	struct ast_sip_endpoint *endpoint,
    	const struct pjmedia_sdp_media *stream)
    {
    	enum ast_sip_session_media_encryption incoming_encryption;
    
    	char transport_end = stream->desc.transport.ptr[stream->desc.transport.slen - 1];
    
    	unsigned int optimistic;
    
    	if ((transport_end == 'F' && !endpoint->media.rtp.use_avpf)
    		|| (transport_end != 'F' && endpoint->media.rtp.use_avpf)) {
    		return AST_SIP_MEDIA_TRANSPORT_INVALID;
    
    	incoming_encryption = get_media_encryption_type(stream->desc.transport, stream, &optimistic);
    
    	if (incoming_encryption == endpoint->media.rtp.encryption) {
    
    		return incoming_encryption;
    	}
    
    
    	if (endpoint->media.rtp.force_avp ||
    		endpoint->media.rtp.encryption_optimistic) {
    
    	/* If an optimistic offer has been made but encryption is not enabled consider it as having
    	 * no offer of crypto at all instead of invalid so the session proceeds.
    	 */
    	if (optimistic) {
    		return AST_SIP_MEDIA_ENCRYPT_NONE;
    	}
    
    
    	return AST_SIP_MEDIA_TRANSPORT_INVALID;
    }
    
    
    static int setup_srtp(struct ast_sip_session_media *session_media)
    {
    	if (!session_media->srtp) {
    		session_media->srtp = ast_sdp_srtp_alloc();
    		if (!session_media->srtp) {
    			return -1;
    		}
    	}
    
    	if (!session_media->srtp->crypto) {
    		session_media->srtp->crypto = ast_sdp_crypto_alloc();
    		if (!session_media->srtp->crypto) {
    			return -1;
    		}
    	}
    
    	return 0;
    }
    
    static int setup_dtls_srtp(struct ast_sip_session *session,
    	struct ast_sip_session_media *session_media)
    {
    	struct ast_rtp_engine_dtls *dtls;
    
    
    	if (!session->endpoint->media.rtp.dtls_cfg.enabled || !session_media->rtp) {
    
    		return -1;
    	}
    
    	dtls = ast_rtp_instance_get_dtls(session_media->rtp);
    	if (!dtls) {
    		return -1;
    	}
    
    
    	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);
    	if (dtls->set_configuration(session_media->rtp, &session->endpoint->media.rtp.dtls_cfg)) {
    
    		ast_log(LOG_ERROR, "Attempted to set an invalid DTLS-SRTP configuration on RTP instance '%p'\n",
    			session_media->rtp);
    		return -1;
    	}
    
    	if (setup_srtp(session_media)) {
    		return -1;
    	}
    	return 0;
    }
    
    
    static void apply_dtls_attrib(struct ast_sip_session_media *session_media,
    	pjmedia_sdp_attr *attr)
    
    {
    	struct ast_rtp_engine_dtls *dtls = ast_rtp_instance_get_dtls(session_media->rtp);
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	if (!attr->value.ptr || !dtls) {
    
    	if (!pj_strcmp2(&attr->name, "setup")) {
    		if (!pj_stricmp2(value, "active")) {
    			dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_ACTIVE);
    		} else if (!pj_stricmp2(value, "passive")) {
    			dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_PASSIVE);
    		} else if (!pj_stricmp2(value, "actpass")) {
    			dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_ACTPASS);
    		} else if (!pj_stricmp2(value, "holdconn")) {
    			dtls->set_setup(session_media->rtp, AST_RTP_DTLS_SETUP_HOLDCONN);
    		} else {
    			ast_log(LOG_WARNING, "Unsupported setup attribute value '%*s'\n", (int)value->slen, value->ptr);
    		}
    	} else if (!pj_strcmp2(&attr->name, "connection")) {
    		if (!pj_stricmp2(value, "new")) {
    			dtls->reset(session_media->rtp);
    		} else if (!pj_stricmp2(value, "existing")) {
    			/* Do nothing */
    		} else {
    			ast_log(LOG_WARNING, "Unsupported connection attribute value '%*s'\n", (int)value->slen, value->ptr);
    		}
    	} else if (!pj_strcmp2(&attr->name, "fingerprint")) {
    		char hash_value[256], hash[32];
    		char fingerprint_text[value->slen + 1];
    		ast_copy_pj_str(fingerprint_text, value, sizeof(fingerprint_text));
    
    			if (sscanf(fingerprint_text, "%31s %255s", hash, hash_value) == 2) {
    
    			if (!strcasecmp(hash, "sha-1")) {
    				dtls->set_fingerprint(session_media->rtp, AST_RTP_DTLS_HASH_SHA1, hash_value);
    			} else if (!strcasecmp(hash, "sha-256")) {
    				dtls->set_fingerprint(session_media->rtp, AST_RTP_DTLS_HASH_SHA256, hash_value);
    			} else {
    				ast_log(LOG_WARNING, "Unsupported fingerprint hash type '%s'\n",
    				hash);
    
    }
    
    static int parse_dtls_attrib(struct ast_sip_session_media *session_media,
    	const struct pjmedia_sdp_session *sdp,
    	const struct pjmedia_sdp_media *stream)
    {
    	int i;
    
    	for (i = 0; i < sdp->attr_count; i++) {
    		apply_dtls_attrib(session_media, sdp->attr[i]);
    	}
    
    	for (i = 0; i < stream->attr_count; i++) {
    		apply_dtls_attrib(session_media, stream->attr[i]);
    	}
    
    
    	ast_set_flag(session_media->srtp, AST_SRTP_CRYPTO_OFFER_OK);
    
    	return 0;
    }
    
    
    static int setup_sdes_srtp(struct ast_sip_session_media *session_media,
    	const struct pjmedia_sdp_media *stream)
    {
    	int i;
    
    	for (i = 0; i < stream->attr_count; i++) {
    		pjmedia_sdp_attr *attr;
    		RAII_VAR(char *, crypto_str, NULL, ast_free);
    
    		/* check the stream for the required crypto attribute */
    		attr = stream->attr[i];
    		if (pj_strcmp2(&attr->name, "crypto")) {
    			continue;
    		}
    
    		crypto_str = ast_strndup(attr->value.ptr, attr->value.slen);
    		if (!crypto_str) {
    			return -1;
    		}
    
    
    		if (setup_srtp(session_media)) {