[asterisk-commits] file: branch group/media_formats r408286 - in /team/group/media_formats: chan...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Feb 17 11:44:54 CST 2014


Author: file
Date: Mon Feb 17 11:44:51 2014
New Revision: 408286

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=408286
Log:
Move PJSIP over to new media formats.

Packetization in RTP isn't done yet so that's commented out.

Modified:
    team/group/media_formats/channels/chan_pjsip.c
    team/group/media_formats/channels/pjsip/dialplan_functions.c
    team/group/media_formats/include/asterisk/format_cache.h
    team/group/media_formats/include/asterisk/res_pjsip.h
    team/group/media_formats/include/asterisk/res_pjsip_session.h
    team/group/media_formats/res/res_pjsip/pjsip_configuration.c
    team/group/media_formats/res/res_pjsip_sdp_rtp.c
    team/group/media_formats/res/res_pjsip_session.c

Modified: team/group/media_formats/channels/chan_pjsip.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats/channels/chan_pjsip.c?view=diff&rev=408286&r1=408285&r2=408286
==============================================================================
--- team/group/media_formats/channels/chan_pjsip.c (original)
+++ team/group/media_formats/channels/chan_pjsip.c Mon Feb 17 11:44:51 2014
@@ -57,6 +57,7 @@
 #include "asterisk/stasis_endpoints.h"
 #include "asterisk/stasis_channels.h"
 #include "asterisk/indications.h"
+#include "asterisk/format_cache.h"
 
 #include "asterisk/res_pjsip.h"
 #include "asterisk/res_pjsip_session.h"
@@ -201,7 +202,7 @@
 {
 	struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
 
-	ast_format_cap_copy(result, channel->session->endpoint->media.codecs);
+	ast_format_cap_append_by_type(result, channel->session->endpoint->media.codecs, AST_MEDIA_TYPE_UNKNOWN);
 }
 
 static int send_direct_media_request(void *data)
@@ -313,8 +314,9 @@
 		return 0;
 	}
 
-	if (cap && !ast_format_cap_is_empty(cap) && !ast_format_cap_identical(session->direct_media_cap, cap)) {
-		ast_format_cap_copy(session->direct_media_cap, cap);
+	if (cap && ast_format_cap_count(cap)) {
+		ast_format_cap_remove_bytype(session->direct_media_cap, AST_MEDIA_TYPE_UNKNOWN);
+		ast_format_cap_append_by_type(session->direct_media_cap, cap, AST_MEDIA_TYPE_UNKNOWN);
 		changed = 1;
 	}
 
@@ -343,7 +345,8 @@
 static struct ast_channel *chan_pjsip_new(struct ast_sip_session *session, int state, const char *exten, const char *title, const char *linkedid, const char *cid_name)
 {
 	struct ast_channel *chan;
-	struct ast_format fmt;
+	struct ast_format_cap *caps;
+	struct ast_format *fmt;
 	RAII_VAR(struct chan_pjsip_pvt *, pvt, NULL, ao2_cleanup);
 	struct ast_sip_channel_pvt *channel;
 	struct ast_variable *var;
@@ -352,14 +355,21 @@
 		return NULL;
 	}
 
+	caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	if (!caps) {
+		return NULL;
+	}
+
 	if (!(chan = ast_channel_alloc(1, state, S_OR(session->id.number.str, ""), S_OR(session->id.name.str, ""), "", "", "", linkedid, 0, "PJSIP/%s-%08x", ast_sorcery_object_get_id(session->endpoint),
 		ast_atomic_fetchadd_int((int *)&chan_idx, +1)))) {
+		ao2_ref(caps, -1);
 		return NULL;
 	}
 
 	ast_channel_tech_set(chan, &chan_pjsip_tech);
 
 	if (!(channel = ast_sip_channel_pvt_alloc(pvt, session))) {
+		ao2_ref(caps, -1);
 		ast_channel_unlock(chan);
 		ast_hangup(chan);
 		return NULL;
@@ -375,17 +385,19 @@
 
 	ast_channel_tech_pvt_set(chan, channel);
 
-	if (ast_format_cap_is_empty(session->req_caps) || !ast_format_cap_has_joint(session->req_caps, session->endpoint->media.codecs)) {
-		ast_format_cap_copy(ast_channel_nativeformats(chan), session->endpoint->media.codecs);
+	if (!ast_format_cap_count(session->req_caps) ||
+		!ast_format_cap_iscompatible(session->req_caps, session->endpoint->media.codecs)) {
+		ast_format_cap_append_by_type(caps, session->endpoint->media.codecs, AST_MEDIA_TYPE_UNKNOWN);
 	} else {
-		ast_format_cap_copy(ast_channel_nativeformats(chan), session->req_caps);
-	}
-
-	ast_codec_choose(&session->endpoint->media.prefs, ast_channel_nativeformats(chan), 1, &fmt);
-	ast_format_copy(ast_channel_writeformat(chan), &fmt);
-	ast_format_copy(ast_channel_rawwriteformat(chan), &fmt);
-	ast_format_copy(ast_channel_readformat(chan), &fmt);
-	ast_format_copy(ast_channel_rawreadformat(chan), &fmt);
+		ast_format_cap_append_by_type(caps, session->req_caps, AST_MEDIA_TYPE_UNKNOWN);
+	}
+
+	fmt = ast_format_cap_get_format(caps, 0);
+	ast_channel_set_writeformat(chan, fmt);
+	ast_channel_set_rawwriteformat(chan, fmt);
+	ast_channel_set_readformat(chan, fmt);
+	ast_channel_set_rawreadformat(chan, fmt);
+	ao2_ref(fmt, -1);
 
 	if (state == AST_STATE_RING) {
 		ast_channel_rings_set(chan, 1);
@@ -565,9 +577,18 @@
 		return f;
 	}
 
-	if (!(ast_format_cap_iscompatible(ast_channel_nativeformats(ast), &f->subclass.format))) {
-		ast_debug(1, "Oooh, format changed to %s\n", ast_getformatname(&f->subclass.format));
-		ast_format_cap_set(ast_channel_nativeformats(ast), &f->subclass.format);
+	if (!(ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), f->subclass.format))) {
+		struct ast_format_cap *caps;
+
+		ast_debug(1, "Oooh, format changed to %s\n", f->subclass.format->codec->name);
+
+		caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+		if (caps) {
+			ast_format_cap_add(caps, f->subclass.format, 0);
+			ast_channel_nativeformats_set(ast, caps);
+			ao2_ref(caps, -1);
+		}
+
 		ast_set_read_format(ast, ast_channel_readformat(ast));
 		ast_set_write_format(ast, ast_channel_writeformat(ast));
 	}
@@ -604,15 +625,15 @@
 		if (!media) {
 			return 0;
 		}
-		if (!(ast_format_cap_iscompatible(ast_channel_nativeformats(ast), &frame->subclass.format))) {
+		if (!(ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format))) {
 			char buf[256];
 
 			ast_log(LOG_WARNING,
 				"Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
-				ast_getformatname(&frame->subclass.format),
+				frame->subclass.format->codec->name,
 				ast_getformatname_multiple(buf, sizeof(buf), ast_channel_nativeformats(ast)),
-				ast_getformatname(ast_channel_readformat(ast)),
-				ast_getformatname(ast_channel_writeformat(ast)));
+				ast_channel_readformat(ast)->codec->name,
+				ast_channel_writeformat(ast)->codec->name);
 			return 0;
 		}
 		if (media->rtp) {
@@ -1094,10 +1115,9 @@
 		if (media && media->rtp) {
 			/* FIXME: Only use this for VP8. Additional work would have to be done to
 			 * fully support other video codecs */
-			struct ast_format_cap *fcap = ast_channel_nativeformats(ast);
-			struct ast_format vp8;
-			ast_format_set(&vp8, AST_FORMAT_VP8, 0);
-			if (ast_format_cap_iscompatible(fcap, &vp8)) {
+
+			if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast),
+				ast_format_vp8)) {
 				/* FIXME Fake RTP write, this will be sent as an RTCP packet. Ideally the
 				 * RTP engine would provide a way to externally write/schedule RTCP
 				 * packets */
@@ -2032,11 +2052,11 @@
 {
 	struct ao2_container *endpoints;
 
-	if (!(chan_pjsip_tech.capabilities = ast_format_cap_alloc(0))) {
+	if (!(chan_pjsip_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
 
-	ast_format_cap_add_all_by_type(chan_pjsip_tech.capabilities, AST_FORMAT_TYPE_AUDIO);
+	ast_format_cap_add_all_by_type(chan_pjsip_tech.capabilities, AST_MEDIA_TYPE_AUDIO);
 
 	ast_rtp_glue_register(&chan_pjsip_rtp_glue);
 

Modified: team/group/media_formats/channels/pjsip/dialplan_functions.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats/channels/pjsip/dialplan_functions.c?view=diff&rev=408286&r1=408285&r2=408286
==============================================================================
--- team/group/media_formats/channels/pjsip/dialplan_functions.c (original)
+++ team/group/media_formats/channels/pjsip/dialplan_functions.c Mon Feb 17 11:44:51 2014
@@ -800,35 +800,32 @@
 }
 
 static int media_offer_read_av(struct ast_sip_session *session, char *buf,
-			       size_t len, enum ast_format_type media_type)
+			       size_t len, enum ast_media_type media_type)
 {
 	int i, size = 0;
-	struct ast_format fmt;
-	const char *name;
-
-	for (i = 0; ast_codec_pref_index(&session->override_prefs, i, &fmt); ++i) {
-		if (AST_FORMAT_GET_TYPE(fmt.id) != media_type) {
+
+	for (i = 0; i < ast_format_cap_count(session->req_caps); i++) {
+		struct ast_format *fmt = ast_format_cap_get_format(session->req_caps, i);
+
+		if (fmt->codec->type != media_type) {
+			ao2_ref(fmt, -1);
 			continue;
 		}
 
-		name = ast_getformatname(&fmt);
-
-		if (ast_strlen_zero(name)) {
-			ast_log(LOG_WARNING, "PJSIP_MEDIA_OFFER unrecognized format %s\n", name);
-			continue;
-		}
-
 		/* add one since we'll include a comma */
-		size = strlen(name) + 1;
+		size = strlen(fmt->codec->name) + 1;
 		len -= size;
 		if ((len) < 0) {
+			ao2_ref(fmt, -1);
 			break;
 		}
 
 		/* no reason to use strncat here since we have already ensured buf has
                    enough space, so strcat can be safely used */
-		strcat(buf, name);
+		strcat(buf, fmt->codec->name);
 		strcat(buf, ",");
+
+		ao2_ref(fmt, -1);
 	}
 
 	if (size) {
@@ -840,23 +837,16 @@
 
 struct media_offer_data {
 	struct ast_sip_session *session;
-	enum ast_format_type media_type;
+	enum ast_media_type media_type;
 	const char *value;
 };
 
 static int media_offer_write_av(void *obj)
 {
 	struct media_offer_data *data = obj;
-	int i;
-	struct ast_format fmt;
-	/* remove all of the given media type first */
-	for (i = 0; ast_codec_pref_index(&data->session->override_prefs, i, &fmt); ++i) {
-		if (AST_FORMAT_GET_TYPE(fmt.id) == data->media_type) {
-			ast_codec_pref_remove(&data->session->override_prefs, &fmt);
-		}
-	}
+
 	ast_format_cap_remove_bytype(data->session->req_caps, data->media_type);
-	ast_parse_allow_disallow(&data->session->override_prefs, data->session->req_caps, data->value, 1);
+	ast_parse_allow_disallow(data->session->req_caps, data->value, 1);
 
 	return 0;
 }
@@ -866,9 +856,9 @@
 	struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
 
 	if (!strcmp(data, "audio")) {
-		return media_offer_read_av(channel->session, buf, len, AST_FORMAT_TYPE_AUDIO);
+		return media_offer_read_av(channel->session, buf, len, AST_MEDIA_TYPE_AUDIO);
 	} else if (!strcmp(data, "video")) {
-		return media_offer_read_av(channel->session, buf, len, AST_FORMAT_TYPE_VIDEO);
+		return media_offer_read_av(channel->session, buf, len, AST_MEDIA_TYPE_VIDEO);
 	}
 
 	return 0;
@@ -884,9 +874,9 @@
 	};
 
 	if (!strcmp(data, "audio")) {
-		mdata.media_type = AST_FORMAT_TYPE_AUDIO;
+		mdata.media_type = AST_MEDIA_TYPE_AUDIO;
 	} else if (!strcmp(data, "video")) {
-		mdata.media_type = AST_FORMAT_TYPE_VIDEO;
+		mdata.media_type = AST_MEDIA_TYPE_VIDEO;
 	}
 
 	return ast_sip_push_task_synchronous(channel->session->serializer, media_offer_write_av, &mdata);

Modified: team/group/media_formats/include/asterisk/format_cache.h
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats/include/asterisk/format_cache.h?view=diff&rev=408286&r1=408285&r2=408286
==============================================================================
--- team/group/media_formats/include/asterisk/format_cache.h (original)
+++ team/group/media_formats/include/asterisk/format_cache.h Mon Feb 17 11:44:51 2014
@@ -149,6 +149,11 @@
 extern struct ast_format *ast_format_h264;
 
 /*!
+ * \brief Built-in cached vp8 format.
+ */
+extern struct ast_format *ast_format_vp8;
+
+/*!
  * \brief Built-in cached jpeg format.
  */
 extern struct ast_format *ast_format_jpeg;

Modified: team/group/media_formats/include/asterisk/res_pjsip.h
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats/include/asterisk/res_pjsip.h?view=diff&rev=408286&r1=408285&r2=408286
==============================================================================
--- team/group/media_formats/include/asterisk/res_pjsip.h (original)
+++ team/group/media_formats/include/asterisk/res_pjsip.h Mon Feb 17 11:44:51 2014
@@ -523,8 +523,6 @@
 	struct ast_sip_direct_media_configuration direct_media;
 	/*! T.38 (FoIP) options */
 	struct ast_sip_t38_configuration t38;
-	/*! Codec preferences */
-	struct ast_codec_pref prefs;
 	/*! Configured codecs */
 	struct ast_format_cap *codecs;
 	/*! DSCP TOS bits for audio streams */

Modified: team/group/media_formats/include/asterisk/res_pjsip_session.h
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats/include/asterisk/res_pjsip_session.h?view=diff&rev=408286&r1=408285&r2=408286
==============================================================================
--- team/group/media_formats/include/asterisk/res_pjsip_session.h (original)
+++ team/group/media_formats/include/asterisk/res_pjsip_session.h Mon Feb 17 11:44:51 2014
@@ -124,8 +124,6 @@
 	struct ast_party_id id;
 	/* Requested capabilities */
 	struct ast_format_cap *req_caps;
-	/* Codecs overriden by dialplan on an outgoing request */
-	struct ast_codec_pref override_prefs;
 	/* Optional DSP, used only for inband DTMF detection if configured */
 	struct ast_dsp *dsp;
 	/* Whether the termination of the session should be deferred */

Modified: team/group/media_formats/res/res_pjsip/pjsip_configuration.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats/res/res_pjsip/pjsip_configuration.c?view=diff&rev=408286&r1=408285&r2=408286
==============================================================================
--- team/group/media_formats/res/res_pjsip/pjsip_configuration.c (original)
+++ team/group/media_formats/res/res_pjsip/pjsip_configuration.c Mon Feb 17 11:44:51 2014
@@ -1450,8 +1450,8 @@
 
 	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "type", "", OPT_NOOP_T, 0, 0);
 	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "context", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, context));
-	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "disallow", "", OPT_CODEC_T, 0, FLDSET(struct ast_sip_endpoint, media.prefs, media.codecs));
-	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow", "", OPT_CODEC_T, 1, FLDSET(struct ast_sip_endpoint, media.prefs, media.codecs));
+	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "disallow", "", OPT_CODEC_T, 0, FLDSET(struct ast_sip_endpoint, media.codecs));
+	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow", "", OPT_CODEC_T, 1, FLDSET(struct ast_sip_endpoint, media.codecs));
 	ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtmf_mode", "rfc4733", dtmf_handler, dtmf_to_str, 0, 0);
 	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ipv6));
 	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_symmetric", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.symmetric));
@@ -1622,9 +1622,7 @@
 
 	ast_string_field_free_memory(endpoint);
 
-	if (endpoint->media.codecs) {
-		ast_format_cap_destroy(endpoint->media.codecs);
-	}
+	ao2_cleanup(endpoint->media.codecs);
 	subscription_configuration_destroy(&endpoint->subscription);
 	info_configuration_destroy(&endpoint->info);
 	media_configuration_destroy(&endpoint->media);
@@ -1662,7 +1660,7 @@
 		ao2_cleanup(endpoint);
 		return NULL;
 	}
-	if (!(endpoint->media.codecs = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK))) {
+	if (!(endpoint->media.codecs = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
 		ao2_cleanup(endpoint);
 		return NULL;
 	}

Modified: team/group/media_formats/res/res_pjsip_sdp_rtp.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats/res/res_pjsip_sdp_rtp.c?view=diff&rev=408286&r1=408285&r2=408286
==============================================================================
--- team/group/media_formats/res/res_pjsip_sdp_rtp.c (original)
+++ team/group/media_formats/res/res_pjsip_sdp_rtp.c Mon Feb 17 11:44:51 2014
@@ -41,6 +41,8 @@
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #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"
@@ -68,38 +70,39 @@
 static const int FD_VIDEO = 2;
 
 /*! \brief Retrieves an ast_format_type based on the given stream_type */
-static enum ast_format_type stream_to_media_type(const char *stream_type)
+static enum ast_media_type stream_to_media_type(const char *stream_type)
 {
 	if (!strcasecmp(stream_type, STR_AUDIO)) {
-		return AST_FORMAT_TYPE_AUDIO;
+		return AST_MEDIA_TYPE_AUDIO;
 	} else if (!strcasecmp(stream_type, STR_VIDEO)) {
-		return AST_FORMAT_TYPE_VIDEO;
+		return AST_MEDIA_TYPE_VIDEO;
 	}
 
 	return 0;
 }
 
 /*! \brief Get the starting descriptor for a media type */
-static int media_type_to_fdno(enum ast_format_type media_type)
+static int media_type_to_fdno(enum ast_media_type media_type)
 {
 	switch (media_type) {
-	case AST_FORMAT_TYPE_AUDIO: return FD_AUDIO;
-	case AST_FORMAT_TYPE_VIDEO: return FD_VIDEO;
-	case AST_FORMAT_TYPE_TEXT:
-	case AST_FORMAT_TYPE_IMAGE: break;
+	case AST_MEDIA_TYPE_AUDIO: return FD_AUDIO;
+	case AST_MEDIA_TYPE_VIDEO: return FD_VIDEO;
+	case AST_MEDIA_TYPE_TEXT:
+	case AST_MEDIA_TYPE_UNKNOWN:
+	case AST_MEDIA_TYPE_IMAGE: break;
 	}
 	return -1;
 }
 
 /*! \brief Remove all other cap types but the one given */
-static void format_cap_only_type(struct ast_format_cap *caps, enum ast_format_type media_type)
-{
-	int i = AST_FORMAT_INC;
-	while (i <= AST_FORMAT_TYPE_TEXT) {
+static void format_cap_only_type(struct ast_format_cap *caps, enum ast_media_type media_type)
+{
+	int i = 0;
+	while (i <= AST_MEDIA_TYPE_TEXT) {
 		if (i != media_type) {
 			ast_format_cap_remove_bytype(caps, i);
 		}
-		i += AST_FORMAT_INC;
+		i += 1;
 	}
 }
 
@@ -116,8 +119,8 @@
 	ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RTCP, 1);
 	ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_NAT, session->endpoint->media.rtp.symmetric);
 
-	ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(session_media->rtp),
-					 session_media->rtp, &session->endpoint->media.prefs);
+//	ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(session_media->rtp),
+//					 session_media->rtp, &session->endpoint->media.prefs);
 
 	if (session->endpoint->dtmf == AST_SIP_DTMF_INBAND) {
 		ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND);
@@ -195,36 +198,37 @@
 static int set_caps(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
 		    const struct pjmedia_sdp_media *stream)
 {
-	RAII_VAR(struct ast_format_cap *, caps, NULL, ast_format_cap_destroy);
-	RAII_VAR(struct ast_format_cap *, peer, NULL, ast_format_cap_destroy);
-	RAII_VAR(struct ast_format_cap *, joint, NULL, ast_format_cap_destroy);
-	enum ast_format_type media_type = stream_to_media_type(session_media->stream_type);
+	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);
+	enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
 	struct ast_rtp_codecs codecs;
-	struct ast_format fmt;
 	int fmts = 0;
 	int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) &&
-		!ast_format_cap_is_empty(session->direct_media_cap);
-
-	if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK)) ||
-	    !(peer = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK))) {
+		ast_format_cap_count(session->direct_media_cap);
+
+	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", session_media->stream_type);
 		return -1;
 	}
 
 	/* get the endpoint capabilities */
 	if (direct_media_enabled) {
-		ast_format_cap_joint_copy(session->endpoint->media.codecs, session->direct_media_cap, caps);
+		ast_format_cap_get_compatible(session->endpoint->media.codecs, session->direct_media_cap, caps);
+		format_cap_only_type(caps, media_type);
 	} else {
-		ast_format_cap_copy(caps, session->endpoint->media.codecs);
-	}
-	format_cap_only_type(caps, media_type);
+		ast_format_cap_append_by_type(caps, session->endpoint->media.codecs, media_type);
+	}
 
 	/* get the capabilities on the peer */
 	get_codecs(session, stream, &codecs);
 	ast_rtp_codecs_payload_formats(&codecs, peer, &fmts);
 
 	/* get the joint capabilities between peer and endpoint */
-	if (!(joint = ast_format_cap_joint(caps, peer))) {
+	ast_format_cap_get_compatible(caps, peer, joint);
+	if (!ast_format_cap_count(joint)) {
 		char usbuf[64], thembuf[64];
 
 		ast_rtp_codecs_payloads_destroy(&codecs);
@@ -238,23 +242,28 @@
 	ast_rtp_codecs_payloads_copy(&codecs, ast_rtp_instance_get_codecs(session_media->rtp),
 				     session_media->rtp);
 
-	ast_format_cap_copy(caps, session->req_caps);
+	ast_format_cap_remove_bytype(caps, AST_MEDIA_TYPE_UNKNOWN);
+	ast_format_cap_append_by_type(caps, session->req_caps, AST_MEDIA_TYPE_UNKNOWN);
 	ast_format_cap_remove_bytype(caps, media_type);
-	ast_format_cap_append(caps, joint);
-	ast_format_cap_append(session->req_caps, caps);
+	ast_format_cap_append_by_type(caps, joint, AST_MEDIA_TYPE_UNKNOWN);
+	ast_format_cap_append_by_type(session->req_caps, caps, AST_MEDIA_TYPE_UNKNOWN);
 
 	if (session->channel) {
-		ast_format_cap_copy(caps, ast_channel_nativeformats(session->channel));
+		struct ast_format *fmt;
+
+		ast_format_cap_append_by_type(caps, ast_channel_nativeformats(session->channel), AST_MEDIA_TYPE_UNKNOWN);
 		ast_format_cap_remove_bytype(caps, media_type);
-		ast_codec_choose(&session->endpoint->media.prefs, joint, 1, &fmt);
-		ast_format_cap_add(caps, &fmt);
+		fmt = ast_format_cap_get_format(joint, 0);
+		ast_format_cap_add(caps, fmt, 0);
 
 		/* Apply the new formats to the channel, potentially changing read/write formats while doing so */
-		ast_format_cap_copy(ast_channel_nativeformats(session->channel), caps);
-		ast_format_copy(ast_channel_rawwriteformat(session->channel), &fmt);
-		ast_format_copy(ast_channel_rawreadformat(session->channel), &fmt);
+		ast_channel_nativeformats_set(session->channel, caps);
+		ast_channel_set_rawwriteformat(session->channel, fmt);
+		ast_channel_set_rawreadformat(session->channel, fmt);
 		ast_set_read_format(session->channel, ast_channel_readformat(session->channel));
 		ast_set_write_format(session->channel, ast_channel_writeformat(session->channel));
+
+		ao2_ref(fmt, -1);
 	}
 
 	ast_rtp_codecs_payloads_destroy(&codecs);
@@ -306,18 +315,6 @@
 	return attr;
 }
 
-static int codec_pref_has_type(struct ast_codec_pref *prefs, enum ast_format_type media_type)
-{
-	int i;
-	struct ast_format fmt;
-	for (i = 0; ast_codec_pref_index(prefs, i, &fmt); ++i) {
-		if (AST_FORMAT_GET_TYPE(fmt.id) == media_type) {
-			return 1;
-		}
-	}
-	return 0;
-}
-
 /*! \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)
 {
@@ -460,11 +457,11 @@
 static void apply_packetization(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
 			 const struct pjmedia_sdp_media *remote_stream)
 {
+#if 0
 	pjmedia_sdp_attr *attr;
 	pj_str_t value;
 	unsigned long framing;
 	int codec;
-	struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(session_media->rtp)->pref;
 
 	/* Apply packetization if available and configured to do so */
 	if (!session->endpoint->media.rtp.use_ptime || !(attr = pjmedia_sdp_media_find_attr2(remote_stream, "ptime", NULL))) {
@@ -487,6 +484,7 @@
 
 	ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(session_media->rtp),
 					 session_media->rtp, pref);
+#endif
 }
 
 /*! \brief figure out media transport encryption type from the media transport string */
@@ -704,7 +702,7 @@
 {
 	char host[NI_MAXHOST];
 	RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free_ptr);
-	enum ast_format_type media_type = stream_to_media_type(session_media->stream_type);
+	enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
 
 	/* If no type formats have been configured reject this stream */
 	if (!ast_format_cap_has_type(session->endpoint->media.codecs, media_type)) {
@@ -854,18 +852,14 @@
 	int noncodec = (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733) ? AST_RTP_DTMF : 0;
 	int min_packet_size = 0, max_packet_size = 0;
 	int rtp_code;
-	struct ast_format format;
-	RAII_VAR(struct ast_format_cap *, caps, NULL, ast_format_cap_destroy);
-	enum ast_format_type media_type = stream_to_media_type(session_media->stream_type);
+	RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
+	enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
+	int use_override_prefs = ast_format_cap_count(session->req_caps);
 
 	int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) &&
-		!ast_format_cap_is_empty(session->direct_media_cap);
-
-	int use_override_prefs = session->override_prefs.formats[0].id;
-	struct ast_codec_pref *prefs = use_override_prefs ?
-		&session->override_prefs : &session->endpoint->media.prefs;
-
-	if ((use_override_prefs && !codec_pref_has_type(&session->override_prefs, media_type)) ||
+		ast_format_cap_count(session->direct_media_cap);
+
+	if ((use_override_prefs && !ast_format_cap_has_type(session->req_caps, media_type)) ||
 	    (!use_override_prefs && !ast_format_cap_has_type(session->endpoint->media.codecs, media_type))) {
 		/* If no type formats are configured don't add a stream */
 		return 0;
@@ -911,59 +905,60 @@
 	/* Add ICE attributes and candidates */
 	add_ice_to_stream(session, session_media, pool, media);
 
-	if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK))) {
+	if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
 		ast_log(LOG_ERROR, "Failed to allocate %s capabilities\n", session_media->stream_type);
 		return -1;
 	}
 
 	if (direct_media_enabled) {
-		ast_format_cap_joint_copy(session->endpoint->media.codecs, session->direct_media_cap, caps);
-	} else if (ast_format_cap_is_empty(session->req_caps) || !ast_format_cap_has_joint(session->req_caps, session->endpoint->media.codecs)) {
-		ast_format_cap_copy(caps, session->endpoint->media.codecs);
+		ast_format_cap_get_compatible(session->endpoint->media.codecs, session->direct_media_cap, caps);
+	} else if (!ast_format_cap_count(session->req_caps) ||
+		!ast_format_cap_iscompatible(session->req_caps, session->endpoint->media.codecs)) {
+		ast_format_cap_append_by_type(caps, session->endpoint->media.codecs, media_type);
 	} else {
-		ast_format_cap_copy(caps, session->req_caps);
-	}
-
-	for (index = 0; ast_codec_pref_index(prefs, index, &format); ++index) {
-		struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(session_media->rtp)->pref;
-
-		if (AST_FORMAT_GET_TYPE(format.id) != media_type) {
+		ast_format_cap_append_by_type(caps, session->req_caps, media_type);
+	}
+
+	for (index = 0; index < ast_format_cap_count(caps); ++index) {
+		struct ast_format *format = ast_format_cap_get_format(caps, index);
+
+		if (format->codec->type != media_type) {
+			ao2_ref(format, -1);
 			continue;
 		}
 
-		if (!use_override_prefs && !ast_format_cap_get_compatible_format(caps, &format, &format)) {
+		if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 1, format, 0)) == -1) {
+			ast_log(LOG_WARNING,"Unable to get rtp codec payload code for %s\n", format->codec->name);
+			ao2_ref(format, -1);
 			continue;
 		}
 
-		if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp), 1, &format, 0)) == -1) {
-			ast_log(LOG_WARNING,"Unable to get rtp codec payload code for %s\n",ast_getformatname(&format));
+		if (!(attr = generate_rtpmap_attr(media, pool, rtp_code, 1, format, 0))) {
+			ao2_ref(format, -1);
 			continue;
 		}
 
-		if (!(attr = generate_rtpmap_attr(media, pool, rtp_code, 1, &format, 0))) {
-			continue;
-		}
-
 		media->attr[media->attr_count++] = attr;
 
-		if ((attr = generate_fmtp_attr(pool, &format, rtp_code))) {
+		if ((attr = generate_fmtp_attr(pool, format, rtp_code))) {
 			media->attr[media->attr_count++] = attr;
 		}
 
-		if (pref && media_type != AST_FORMAT_TYPE_VIDEO) {
-			struct ast_format_list fmt = ast_codec_pref_getsize(pref, &format);
-			if (fmt.cur_ms && ((fmt.cur_ms < min_packet_size) || !min_packet_size)) {
-				min_packet_size = fmt.cur_ms;
+		if (media_type != AST_MEDIA_TYPE_VIDEO) {
+			unsigned int framing = ast_format_cap_get_framing(caps, format);
+
+			if (framing && ((framing < min_packet_size) || !min_packet_size)) {
+				min_packet_size = framing;
 			}
 
-			if (fmt.max_ms && ((fmt.max_ms < max_packet_size) || !max_packet_size)) {
-				max_packet_size = fmt.max_ms;
+			if (format->codec->maximum_ms && ((format->codec->maximum_ms < max_packet_size) || !max_packet_size)) {
+				max_packet_size = format->codec->maximum_ms;
 			}
 		}
 	}
 
 	/* Add non-codec formats */
-	if (media_type != AST_FORMAT_TYPE_VIDEO) {
+	if (media_type != AST_MEDIA_TYPE_VIDEO) {
 		for (index = 1LL; index <= AST_RTP_MAX; index <<= 1) {
 			if (!(noncodec & index) || (rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp),
 											   0, NULL, index)) == -1) {
@@ -1018,7 +1013,7 @@
 				       const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream)
 {
 	RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free_ptr);
-	enum ast_format_type media_type = stream_to_media_type(session_media->stream_type);
+	enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
 	char host[NI_MAXHOST];
 	int fdno;
 
@@ -1056,7 +1051,7 @@
 		return -1;
 	}
 
-	if (media_type == AST_FORMAT_TYPE_AUDIO) {
+	if (media_type == AST_MEDIA_TYPE_AUDIO) {
 		apply_packetization(session, session_media, remote_stream);
 	}
 
@@ -1070,7 +1065,7 @@
 	process_ice_attributes(session, session_media, remote, remote_stream);
 
 	/* audio stream handles music on hold */
-	if (media_type != AST_FORMAT_TYPE_AUDIO) {
+	if (media_type != AST_MEDIA_TYPE_AUDIO) {
 		return 1;
 	}
 

Modified: team/group/media_formats/res/res_pjsip_session.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats/res/res_pjsip_session.c?view=diff&rev=408286&r1=408285&r2=408286
==============================================================================
--- team/group/media_formats/res/res_pjsip_session.c (original)
+++ team/group/media_formats/res/res_pjsip_session.c Mon Feb 17 11:44:51 2014
@@ -1046,8 +1046,8 @@
 	ast_party_id_free(&session->id);
 	ao2_cleanup(session->endpoint);
 	ao2_cleanup(session->contact);
-	ast_format_cap_destroy(session->req_caps);
-	ast_format_cap_destroy(session->direct_media_cap);
+	ao2_cleanup(session->req_caps);
+	ao2_cleanup(session->direct_media_cap);
 
 	if (session->dsp) {
 		ast_dsp_free(session->dsp);
@@ -1146,7 +1146,7 @@
 	session->endpoint = ao2_bump(endpoint);
 	session->contact = ao2_bump(contact);
 	session->inv_session = inv_session;
-	session->req_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
+	session->req_caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
 
 	if (endpoint->dtmf == AST_SIP_DTMF_INBAND) {
 		dsp_features |= DSP_FEATURE_DIGIT_DETECT;
@@ -1174,7 +1174,7 @@
 			iter->session_begin(session);
 		}
 	}
-	session->direct_media_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
+	session->direct_media_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
 	AST_LIST_HEAD_INIT_NOLOCK(&session->delayed_requests);
 	ast_party_id_init(&session->id);
 	ao2_ref(session, +1);
@@ -1250,7 +1250,7 @@
 		return NULL;
 	}
 
-	ast_format_cap_copy(session->req_caps, req_caps);
+	ast_format_cap_append_by_type(session->req_caps, req_caps, AST_MEDIA_TYPE_UNKNOWN);
 	if ((pjsip_dlg_add_usage(dlg, &session_module, NULL) != PJ_SUCCESS)) {
 		pjsip_inv_terminate(inv_session, 500, PJ_FALSE);
 		/* Since we are not notifying ourselves that the INVITE session is being terminated




More information about the asterisk-commits mailing list