[asterisk-commits] file: branch group/media_formats-reviewed r414878 - in /team/group/media_form...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri May 30 05:48:17 CDT 2014


Author: file
Date: Fri May 30 05:48:06 2014
New Revision: 414878

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

Review: https://reviewboard.asterisk.org/r/3516/

Modified:
    team/group/media_formats-reviewed/channels/chan_sip.c
    team/group/media_formats-reviewed/channels/sip/include/sip.h
    team/group/media_formats-reviewed/include/asterisk/format_cache.h

Modified: team/group/media_formats-reviewed/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats-reviewed/channels/chan_sip.c?view=diff&rev=414878&r1=414877&r2=414878
==============================================================================
--- team/group/media_formats-reviewed/channels/chan_sip.c (original)
+++ team/group/media_formats-reviewed/channels/chan_sip.c Fri May 30 05:48:06 2014
@@ -299,6 +299,7 @@
 #include "asterisk/stasis_channels.h"
 #include "asterisk/features_config.h"
 #include "asterisk/http_websocket.h"
+#include "asterisk/format_cache.h"
 
 /*** DOCUMENTATION
 	<application name="SIPDtmfMode" language="en_US">
@@ -795,7 +796,6 @@
 static char default_parkinglot[AST_MAX_CONTEXT];   /*!< Parkinglot */
 static char default_engine[256];                   /*!< Default RTP engine */
 static int default_maxcallbitrate;                 /*!< Maximum bitrate for call */
-static struct ast_codec_pref default_prefs;        /*!< Default codec prefs */
 static char default_zone[MAX_TONEZONE_COUNTRY];        /*!< Default tone zone for channels created from the SIP driver */
 static unsigned int default_transports;            /*!< Default Transports (enum ast_transport) that are acceptable */
 static unsigned int default_primary_transport;     /*!< Default primary Transport (enum ast_transport) for outbound connections to devices */
@@ -1346,7 +1346,6 @@
 static const char *insecure2str(int mode) attribute_const;
 static const char *allowoverlap2str(int mode) attribute_const;
 static void cleanup_stale_contexts(char *new, char *old);
-static void print_codec_to_cli(int fd, struct ast_codec_pref *pref);
 static const char *domain_mode_to_text(const enum domain_mode mode);
 static char *sip_show_domains(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
 static char *_sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[]);
@@ -5348,7 +5347,7 @@
 
 	ast_string_field_free_memory(peer);
 
-	peer->caps = ast_format_cap_destroy(peer->caps);
+	ao2_cleanup(peer->caps);
 
 	ast_rtp_dtls_cfg_free(&peer->dtls_cfg);
 
@@ -6004,7 +6003,7 @@
 	}
 
 	if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT_ALWAYS) ||
-			(ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT) && (ast_format_cap_has_type(dialog->caps, AST_FORMAT_TYPE_VIDEO)))) {
+			(ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT) && (ast_format_cap_has_type(dialog->caps, AST_MEDIA_TYPE_VIDEO)))) {
 		if (!(dialog->vrtp = ast_rtp_instance_new(dialog->engine, sched, &bindaddr_tmp, NULL))) {
 			return -1;
 		}
@@ -6090,8 +6089,7 @@
 	ast_copy_flags(&dialog->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
 	ast_copy_flags(&dialog->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
 	ast_copy_flags(&dialog->flags[2], &peer->flags[2], SIP_PAGE3_FLAGS_TO_COPY);
-	ast_format_cap_copy(dialog->caps, peer->caps);
-	dialog->prefs = peer->prefs;
+	ast_format_cap_append_by_type(dialog->caps, peer->caps, AST_MEDIA_TYPE_UNKNOWN);
 	dialog->amaflags = peer->amaflags;
 
 	ast_string_field_set(dialog, engine, peer->engine);
@@ -6115,7 +6113,10 @@
 		ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_DTMF, ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833);
 		ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_DTMF_COMPENSATE, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE));
 		/* Set Frame packetization */
+		/* BUGBUG: RTP - Needs to be updated if API changes */
+#if 0
 		ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(dialog->rtp), dialog->rtp, &dialog->prefs);
+#endif
 		dialog->autoframing = peer->autoframing;
 	}
 
@@ -6493,7 +6494,7 @@
 	p->jointnoncodeccapability = p->noncodeccapability;
 
 	/* If there are no audio formats left to offer, punt */
-	if (!(ast_format_cap_has_type(p->jointcaps, AST_FORMAT_TYPE_AUDIO))) {
+	if (!(ast_format_cap_has_type(p->jointcaps, AST_MEDIA_TYPE_AUDIO))) {
 		ast_log(LOG_WARNING, "No audio format found to offer. Cancelling call to %s\n", p->username);
 		res = -1;
 	} else {
@@ -6761,11 +6762,11 @@
 	p->named_callgroups = ast_unref_namedgroups(p->named_callgroups);
 	p->named_pickupgroups = ast_unref_namedgroups(p->named_pickupgroups);
 
-	p->caps = ast_format_cap_destroy(p->caps);
-	p->jointcaps = ast_format_cap_destroy(p->jointcaps);
-	p->peercaps = ast_format_cap_destroy(p->peercaps);
-	p->redircaps = ast_format_cap_destroy(p->redircaps);
-	p->prefcaps = ast_format_cap_destroy(p->prefcaps);
+	ao2_cleanup(p->caps);
+	ao2_cleanup(p->jointcaps);
+	ao2_cleanup(p->peercaps);
+	ao2_cleanup(p->redircaps);
+	ao2_cleanup(p->prefcaps);
 
 	ast_rtp_dtls_cfg_free(&p->dtls_cfg);
 
@@ -7355,7 +7356,6 @@
 /*! \brief Try setting the codecs suggested by the SIP_CODEC channel variable */
 static void try_suggested_sip_codec(struct sip_pvt *p)
 {
-	struct ast_format fmt;
 	const char *codec_list;
 	char *codec_list_copy;
 	struct ast_format_cap *original_jointcaps;
@@ -7375,31 +7375,43 @@
 	}
 
 	codec_list_copy = ast_strdupa(codec_list);
-	original_jointcaps = ast_format_cap_dup(p->jointcaps);
+
+	original_jointcaps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	if (!original_jointcaps) {
+		return;
+	}
+	ast_format_cap_append_by_type(original_jointcaps, p->jointcaps, AST_MEDIA_TYPE_UNKNOWN);
 
 	for (codec = strtok_r(codec_list_copy, ",", &strtok_ptr); codec; codec = strtok_r(NULL, ",", &strtok_ptr)) {
+		struct ast_format *fmt;
+
 		codec = ast_strip(codec);
 
-		if (!ast_getformatbyname(codec, &fmt)) {
+		fmt = ast_format_cache_get(codec);
+		if (!fmt) {
 			ast_log(AST_LOG_NOTICE, "Ignoring ${SIP_CODEC*} variable because of unrecognized/not configured codec %s (check allow/disallow in sip.conf)\n", codec);
 			continue;
 		}
-		if (ast_format_cap_iscompatible(original_jointcaps, &fmt)) {
+		if (ast_format_cap_iscompatible_format(original_jointcaps, fmt)) {
 			if (first_codec) {
 				ast_verb(4, "Set codec to '%s' for this call because of ${SIP_CODEC*} variable\n", codec);
-				ast_format_cap_set(p->jointcaps, &fmt);
-				ast_format_cap_set(p->caps, &fmt);
+				ast_format_cap_remove_bytype(p->jointcaps, AST_MEDIA_TYPE_UNKNOWN);
+				ast_format_cap_add(p->jointcaps, fmt, 0);
+				ast_format_cap_remove_bytype(p->caps, AST_MEDIA_TYPE_UNKNOWN);
+				ast_format_cap_add(p->caps, fmt, 0);
 				first_codec = 0;
 			} else {
 				ast_verb(4, "Add codec to '%s' for this call because of ${SIP_CODEC*} variable\n", codec);
-				ast_format_cap_add(p->jointcaps, &fmt);
-				ast_format_cap_add(p->caps, &fmt);
+				ast_format_cap_add(p->jointcaps, fmt, 0);
+				ast_format_cap_add(p->caps, fmt, 0);
 			}
 		} else {
 			ast_log(AST_LOG_NOTICE, "Ignoring ${SIP_CODEC*} variable because it is not shared by both ends: %s\n", codec);
 		}
-	}
-	ast_format_cap_destroy(original_jointcaps);
+
+		ao2_ref(fmt, -1);
+	}
+	ao2_ref(original_jointcaps, -1);
 	return;
  }
 
@@ -7443,13 +7455,13 @@
 
 	switch (frame->frametype) {
 	case AST_FRAME_VOICE:
-		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 s1[512];
 			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),
+				ast_format_get_name(frame->subclass.format),
 				ast_getformatname_multiple(s1, sizeof(s1), ast_channel_nativeformats(ast)),
-				ast_getformatname(ast_channel_readformat(ast)),
-				ast_getformatname(ast_channel_writeformat(ast)));
+				ast_format_get_name(ast_channel_readformat(ast)),
+				ast_format_get_name(ast_channel_writeformat(ast)));
 			return 0;
 		}
 		if (p) {
@@ -7954,10 +7966,7 @@
 		if (p->vrtp && !p->novideo) {
 			/* 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 */
@@ -8061,15 +8070,21 @@
  */
 static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title, const char *linkedid, struct ast_callid *callid)
 {
+	struct ast_format_cap *caps;
 	struct ast_channel *tmp;
 	struct ast_variable *v = NULL;
-	struct ast_format fmt;
+	struct ast_format *fmt;
 	struct ast_format_cap *what = NULL; /* SHALLOW COPY DO NOT DESTROY! */
 	int needvideo = 0;
 	int needtext = 0;
 	char buf[SIPBUFSIZE];
 	char *exten;
 
+	caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	if (!caps) {
+		return NULL;
+	}
+
 	{
 		const char *my_name;	/* pick a good name */
 
@@ -8085,6 +8100,7 @@
 	}
 	if (!tmp) {
 		ast_log(LOG_WARNING, "Unable to allocate AST channel structure for SIP channel\n");
+		ao2_ref(caps, -1);
 		sip_pvt_lock(i);
 		return NULL;
 	}
@@ -8093,6 +8109,7 @@
 		if (ast_endpoint_add_channel(i->relatedpeer->endpoint, tmp)) {
 			ast_channel_unlock(tmp);
 			ast_channel_unref(tmp);
+			ao2_ref(caps, -1);
 			sip_pvt_lock(i);
 			return NULL;
 		}
@@ -8113,26 +8130,28 @@
 
 	/* Select our native format based on codec preference until we receive
 	   something from another device to the contrary. */
-	if (!(ast_format_cap_is_empty(i->jointcaps))) {	/* The joint capabilities of us and peer */
+	if (ast_format_cap_count(i->jointcaps)) {	/* The joint capabilities of us and peer */
 		what = i->jointcaps;
-	} else if (!(ast_format_cap_is_empty(i->caps))) {		/* Our configured capability for this peer */
+	} else if (ast_format_cap_count(i->caps)) {		/* Our configured capability for this peer */
 		what = i->caps;
 	} else {
 		what = sip_cfg.caps;
 	}
 
 	/* Set the native formats */
-	ast_format_cap_copy(ast_channel_nativeformats(tmp), what);
-	/* choose and use only the best audio format for our native formats */
-	ast_codec_choose(&i->prefs, ast_channel_nativeformats(tmp), 1, &fmt); /* get the best audio format */
-	ast_format_cap_remove_bytype(ast_channel_nativeformats(tmp), AST_FORMAT_TYPE_AUDIO); /* remove only the other audio formats */
-	ast_format_cap_add(ast_channel_nativeformats(tmp), &fmt); /* add our best choice back */
+	ast_format_cap_append_by_type(caps, what, AST_MEDIA_TYPE_UNKNOWN);
+	/* Use only the preferred audio format, which is stored at the '0' index */
+	fmt = ast_format_cap_get_format(what, 0); /* get the best audio format */
+	ast_format_cap_remove_bytype(caps, AST_MEDIA_TYPE_AUDIO); /* remove only the other audio formats */
+	ast_format_cap_add(caps, fmt, 0); /* add our best choice back */
+	ast_channel_nativeformats_set(tmp, caps);
+	ao2_ref(caps, -1);
 
 	ast_debug(3, "*** Our native formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, ast_channel_nativeformats(tmp)));
 	ast_debug(3, "*** Joint capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->jointcaps));
 	ast_debug(3, "*** Our capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->caps));
-	ast_debug(3, "*** AST_CODEC_CHOOSE formats are %s \n", ast_getformatname(&fmt));
-	if (!ast_format_cap_is_empty(i->prefcaps)) {
+	ast_debug(3, "*** AST_CODEC_CHOOSE formats are %s \n", ast_format_get_name(fmt));
+	if (ast_format_cap_count(i->prefcaps)) {
 		ast_debug(3, "*** Our preferred formats from the incoming channel are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->prefcaps));
 	}
 
@@ -8143,17 +8162,17 @@
 	if (i->vrtp) {
 		if (ast_test_flag(&i->flags[1], SIP_PAGE2_VIDEOSUPPORT))
 			needvideo = 1;
-		else if (!ast_format_cap_is_empty(i->prefcaps))
-			needvideo = ast_format_cap_has_type(i->prefcaps, AST_FORMAT_TYPE_VIDEO);	/* Outbound call */
+		else if (ast_format_cap_count(i->prefcaps))
+			needvideo = ast_format_cap_has_type(i->prefcaps, AST_MEDIA_TYPE_VIDEO);	/* Outbound call */
 		else
-			needvideo = ast_format_cap_has_type(i->jointcaps, AST_FORMAT_TYPE_VIDEO);	/* Inbound call */
+			needvideo = ast_format_cap_has_type(i->jointcaps, AST_MEDIA_TYPE_VIDEO);	/* Inbound call */
 	}
 
 	if (i->trtp) {
-		if (!ast_format_cap_is_empty(i->prefcaps))
-			needtext = ast_format_cap_has_type(i->prefcaps, AST_FORMAT_TYPE_TEXT);	/* Outbound call */
+		if (ast_format_cap_count(i->prefcaps))
+			needtext = ast_format_cap_has_type(i->prefcaps, AST_MEDIA_TYPE_TEXT);	/* Outbound call */
 		else
-			needtext = ast_format_cap_has_type(i->jointcaps, AST_FORMAT_TYPE_TEXT);	/* Inbound call */
+			needtext = ast_format_cap_has_type(i->jointcaps, AST_MEDIA_TYPE_TEXT);	/* Inbound call */
 	}
 
 	if (needvideo) {
@@ -8181,8 +8200,8 @@
 	if (i->rtp) {
 		ast_channel_set_fd(tmp, 0, ast_rtp_instance_fd(i->rtp, 0));
 		ast_channel_set_fd(tmp, 1, ast_rtp_instance_fd(i->rtp, 1));
-		ast_rtp_instance_set_write_format(i->rtp, &fmt);
-		ast_rtp_instance_set_read_format(i->rtp, &fmt);
+		ast_rtp_instance_set_write_format(i->rtp, fmt);
+		ast_rtp_instance_set_read_format(i->rtp, fmt);
 	}
 	if (needvideo && i->vrtp) {
 		ast_channel_set_fd(tmp, 2, ast_rtp_instance_fd(i->vrtp, 0));
@@ -8200,11 +8219,13 @@
 	}
 	ast_channel_adsicpe_set(tmp, AST_ADSI_UNAVAILABLE);
 
-	ast_format_copy(ast_channel_writeformat(tmp), &fmt);
-	ast_format_copy(ast_channel_rawwriteformat(tmp), &fmt);
-
-	ast_format_copy(ast_channel_readformat(tmp), &fmt);
-	ast_format_copy(ast_channel_rawreadformat(tmp), &fmt);
+	ast_channel_set_writeformat(tmp, fmt);
+	ast_channel_set_rawwriteformat(tmp, fmt);
+
+	ast_channel_set_readformat(tmp, fmt);
+	ast_channel_set_rawreadformat(tmp, fmt);
+
+	ao2_ref(fmt, -1);
 
 	ast_channel_tech_pvt_set(tmp, dialog_ref(i, "sip_new: set chan->tech_pvt to i"));
 
@@ -8556,17 +8577,26 @@
 		return f;
 	}
 
-	if (f && !ast_format_cap_iscompatible(ast_channel_nativeformats(p->owner), &f->subclass.format)) {
-		if (!ast_format_cap_iscompatible(p->jointcaps, &f->subclass.format)) {
+	if (f && !ast_format_cap_iscompatible_format(ast_channel_nativeformats(p->owner), f->subclass.format)) {
+		struct ast_format_cap *caps;
+
+		if (!ast_format_cap_iscompatible_format(p->jointcaps, f->subclass.format)) {
 			ast_debug(1, "Bogus frame of format '%s' received from '%s'!\n",
-				ast_getformatname(&f->subclass.format), ast_channel_name(p->owner));
+				ast_format_get_name(f->subclass.format), ast_channel_name(p->owner));
 			ast_frfree(f);
 			return &ast_null_frame;
 		}
 		ast_debug(1, "Oooh, format changed to %s\n",
-			ast_getformatname(&f->subclass.format));
-		ast_format_cap_remove_bytype(ast_channel_nativeformats(p->owner), AST_FORMAT_TYPE_AUDIO);
-		ast_format_cap_add(ast_channel_nativeformats(p->owner), &f->subclass.format);
+			ast_format_get_name(f->subclass.format));
+
+		caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+		if (caps) {
+			ast_format_cap_append_by_type(caps, ast_channel_nativeformats(p->owner), AST_MEDIA_TYPE_UNKNOWN);
+			ast_format_cap_remove_bytype(caps, AST_MEDIA_TYPE_AUDIO);
+			ast_format_cap_add(caps, f->subclass.format, 0);
+			ast_channel_nativeformats_set(p->owner, caps);
+			ao2_ref(caps, -1);
+		}
 		ast_set_read_format(p->owner, ast_channel_readformat(p->owner));
 		ast_set_write_format(p->owner, ast_channel_writeformat(p->owner));
 	}
@@ -8818,18 +8848,18 @@
 		sip_pvt_callid_set(p, logger_callid);
 	}
 
-	p->caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
-	p->jointcaps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
-	p->peercaps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
-	p->redircaps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
-	p->prefcaps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
-
-	if (!p->caps|| !p->jointcaps || !p->peercaps || !p->redircaps) {
-		p->caps = ast_format_cap_destroy(p->caps);
-		p->jointcaps = ast_format_cap_destroy(p->jointcaps);
-		p->peercaps = ast_format_cap_destroy(p->peercaps);
-		p->redircaps = ast_format_cap_destroy(p->redircaps);
-		p->prefcaps = ast_format_cap_destroy(p->prefcaps);
+	p->caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	p->jointcaps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	p->peercaps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	p->redircaps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	p->prefcaps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+
+	if (!p->caps|| !p->jointcaps || !p->peercaps || !p->redircaps || !p->prefcaps) {
+		ao2_cleanup(p->caps);
+		ao2_cleanup(p->jointcaps);
+		ao2_cleanup(p->peercaps);
+		ao2_cleanup(p->redircaps);
+		ao2_cleanup(p->prefcaps);
 		ao2_t_ref(p, -1, "Yuck, couldn't allocate format capabilities. Get rid o' p");
 		return NULL;
 	}
@@ -8879,7 +8909,6 @@
 	p->sessionversion_remote = -1;
 	p->session_modify = TRUE;
 	p->stimer = NULL;
-	p->prefs = default_prefs;		/* Set default codecs for this call */
 	ast_copy_string(p->zone, default_zone, sizeof(p->zone));
 	p->maxforwards = sip_cfg.default_max_forwards;
 
@@ -8931,7 +8960,7 @@
 	/* Assign default music on hold class */
 	ast_string_field_set(p, mohinterpret, default_mohinterpret);
 	ast_string_field_set(p, mohsuggest, default_mohsuggest);
-	ast_format_cap_copy(p->caps, sip_cfg.caps);
+	ast_format_cap_append_by_type(p->caps, sip_cfg.caps, AST_MEDIA_TYPE_UNKNOWN);
 	p->allowtransfer = sip_cfg.allowtransfer;
 	if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
 	    (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO)) {
@@ -10056,15 +10085,15 @@
 	int udptlportno = -1;			/*!< UDPTL image destination port number */
 
 	/* Peer capability is the capability in the SDP, non codec is RFC2833 DTMF (101) */
-	struct ast_format_cap *peercapability = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
-	struct ast_format_cap *vpeercapability = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
-	struct ast_format_cap *tpeercapability = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
+	struct ast_format_cap *peercapability = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	struct ast_format_cap *vpeercapability = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	struct ast_format_cap *tpeercapability = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
 
 	int peernoncodeccapability = 0, vpeernoncodeccapability = 0, tpeernoncodeccapability = 0;
 
 	struct ast_rtp_codecs newaudiortp = { 0, }, newvideortp = { 0, }, newtextrtp = { 0, };
-	struct ast_format_cap *newjointcapability = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK); /* Negotiated capability */
-	struct ast_format_cap *newpeercapability = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
+	struct ast_format_cap *newjointcapability = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT); /* Negotiated capability */
+	struct ast_format_cap *newpeercapability = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
 	int newnoncodeccapability;
 
 	const char *codecs;
@@ -10085,7 +10114,7 @@
 
 	/* START UNKNOWN */
 	char buf[SIPBUFSIZE];
-	struct ast_format tmp_fmt;
+	struct ast_format *tmp_fmt;
 	/* END UNKNOWN */
 
 	/* Initial check */
@@ -10630,10 +10659,12 @@
 		/* Ensure crypto lines are provided where necessary */
 		if (audio && secure_audio && !processed_crypto) {
 			ast_log(LOG_WARNING, "Rejecting secure audio stream without encryption details: %s\n", m);
-			return -1;
+			res = -1;
+			goto process_sdp_cleanup;
 		} else if (video && secure_video && !processed_crypto) {
 			ast_log(LOG_WARNING, "Rejecting secure video stream without encryption details: %s\n", m);
-			return -1;
+			res = -1;
+			goto process_sdp_cleanup;
 		}
 	}
 
@@ -10692,12 +10723,12 @@
 	ast_rtp_codecs_payload_formats(&newvideortp, vpeercapability, &vpeernoncodeccapability);
 	ast_rtp_codecs_payload_formats(&newtextrtp, tpeercapability, &tpeernoncodeccapability);
 
-	ast_format_cap_append(newpeercapability, peercapability);
-	ast_format_cap_append(newpeercapability, vpeercapability);
-	ast_format_cap_append(newpeercapability, tpeercapability);
-
-	ast_format_cap_joint_copy(p->caps, newpeercapability, newjointcapability);
-	if (ast_format_cap_is_empty(newjointcapability) && udptlportno == -1) {
+	ast_format_cap_append_by_type(newpeercapability, peercapability, AST_MEDIA_TYPE_AUDIO);
+	ast_format_cap_append_by_type(newpeercapability, vpeercapability, AST_MEDIA_TYPE_VIDEO);
+	ast_format_cap_append_by_type(newpeercapability, tpeercapability, AST_MEDIA_TYPE_TEXT);
+
+	ast_format_cap_get_compatible(p->caps, newpeercapability, newjointcapability);
+	if (!ast_format_cap_count(newjointcapability) && udptlportno == -1) {
 		ast_log(LOG_NOTICE, "No compatible codecs, not accepting this offer!\n");
 		/* Do NOT Change current setting */
 		res = -1;
@@ -10731,14 +10762,21 @@
 	if (portno != -1 || vportno != -1 || tportno != -1) {
 		/* We are now ready to change the sip session and RTP structures with the offered codecs, since
 		   they are acceptable */
-		ast_format_cap_copy(p->jointcaps, newjointcapability);                /* Our joint codec profile for this call */
-		ast_format_cap_copy(p->peercaps, newpeercapability);                  /* The other side's capability in latest offer */
+		ast_format_cap_remove_bytype(p->jointcaps, AST_MEDIA_TYPE_UNKNOWN);
+		ast_format_cap_append_by_type(p->jointcaps, newjointcapability, AST_MEDIA_TYPE_UNKNOWN); /* Our joint codec profile for this call */
+		ast_format_cap_remove_bytype(p->peercaps, AST_MEDIA_TYPE_UNKNOWN);
+		ast_format_cap_append_by_type(p->peercaps, newpeercapability, AST_MEDIA_TYPE_UNKNOWN); /* The other side's capability in latest offer */
 		p->jointnoncodeccapability = newnoncodeccapability;     /* DTMF capabilities */
 
 		/* respond with single most preferred joint codec, limiting the other side's choice */
 		if (ast_test_flag(&p->flags[1], SIP_PAGE2_PREFERRED_CODEC)) {
-			ast_codec_choose(&p->prefs, p->jointcaps, 1, &tmp_fmt);
-			ast_format_cap_set(p->jointcaps, &tmp_fmt);
+			unsigned int framing;
+
+			tmp_fmt = ast_format_cap_get_format(p->jointcaps, 0);
+			framing = ast_format_cap_get_framing(p->jointcaps, tmp_fmt);
+			ast_format_cap_remove_bytype(p->jointcaps, AST_MEDIA_TYPE_UNKNOWN);
+			ast_format_cap_add(p->jointcaps, tmp_fmt, framing);
+			ao2_ref(tmp_fmt, -1);
 		}
 	}
 
@@ -10818,7 +10856,7 @@
 				ast_verbose("Peer T.140 RTP is at port %s\n",
 					    ast_sockaddr_stringify(tsa));
 			}
-			if (ast_format_cap_iscompatible(p->jointcaps, ast_format_set(&tmp_fmt, AST_FORMAT_T140RED, 0))) {
+			if (ast_format_cap_iscompatible_format(p->jointcaps, ast_format_t140_red)) {
 				p->red = 1;
 				ast_rtp_red_init(p->trtp, 300, red_data_pt, 2);
 			} else {
@@ -10903,7 +10941,10 @@
 	}
 
 	ast_debug(4, "We have an owner, now see if we need to change this call\n");
-	if (ast_format_cap_has_type(p->jointcaps, AST_FORMAT_TYPE_AUDIO)) {
+	if (ast_format_cap_has_type(p->jointcaps, AST_MEDIA_TYPE_AUDIO)) {
+		struct ast_format_cap *caps;
+		unsigned int framing;
+
 		if (debug) {
 			char s1[SIPBUFSIZE], s2[SIPBUFSIZE];
 			ast_debug(1, "Setting native formats after processing SDP. peer joint formats %s, old nativeformats %s\n",
@@ -10911,11 +10952,17 @@
 				ast_getformatname_multiple(s2, SIPBUFSIZE, ast_channel_nativeformats(p->owner)));
 		}
 
-		ast_codec_choose(&p->prefs, p->jointcaps, 1, &tmp_fmt);
-
-		ast_format_cap_set(ast_channel_nativeformats(p->owner), &tmp_fmt);
-		ast_format_cap_joint_append(p->caps, vpeercapability, ast_channel_nativeformats(p->owner));
-		ast_format_cap_joint_append(p->caps, tpeercapability, ast_channel_nativeformats(p->owner));
+		tmp_fmt = ast_format_cap_get_format(p->jointcaps, 0);
+		framing = ast_format_cap_get_framing(p->jointcaps, tmp_fmt);
+
+		caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+		if (caps) {
+			ast_format_cap_add(caps, tmp_fmt, framing);
+			ast_format_cap_append_by_type(caps, vpeercapability, AST_MEDIA_TYPE_VIDEO);
+			ast_format_cap_append_by_type(caps, tpeercapability, AST_MEDIA_TYPE_TEXT);
+			ast_channel_nativeformats_set(p->owner, caps);
+			ao2_ref(caps, -1);
+		}
 
 		ast_set_read_format(p->owner, ast_channel_readformat(p->owner));
 		ast_set_write_format(p->owner, ast_channel_writeformat(p->owner));
@@ -10947,11 +10994,11 @@
 	ast_rtp_codecs_payloads_destroy(&newtextrtp);
 	ast_rtp_codecs_payloads_destroy(&newvideortp);
 	ast_rtp_codecs_payloads_destroy(&newaudiortp);
-	ast_format_cap_destroy(peercapability);
-	ast_format_cap_destroy(vpeercapability);
-	ast_format_cap_destroy(tpeercapability);
-	ast_format_cap_destroy(newjointcapability);
-	ast_format_cap_destroy(newpeercapability);
+	ao2_cleanup(peercapability);
+	ao2_cleanup(vpeercapability);
+	ao2_cleanup(tpeercapability);
+	ao2_cleanup(newjointcapability);
+	ao2_cleanup(newpeercapability);
 	return res;
 }
 
@@ -11206,6 +11253,8 @@
 				ast_debug(1, "Can't read framing from SDP: %s\n", a);
 			}
 		}
+	/* BUGBUG: RTP - Needs to be updated if API changes */
+#if 0
 		if (framing && p->autoframing) {
 			struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(p->rtp)->pref;
 			int codec_n;
@@ -11218,6 +11267,7 @@
 			}
 			ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, pref);
 		}
+#endif
 		found = TRUE;
 	} else if (sscanf(a, "rtpmap: %30u %127[^/]/%30u", &codec, mimeSubtype, &sample_rate) == 3) {
 		/* We have a rtpmap to handle */
@@ -11250,8 +11300,7 @@
 				ast_rtp_codecs_payloads_unset(newaudiortp, NULL, codec);
 			}
 
-			switch ((int) format->id) {
-			case AST_FORMAT_SIREN7:
+			if (ast_format_cmp(format, ast_format_siren7) == AST_FORMAT_CMP_EQUAL) {
 				if (sscanf(fmtp_string, "bitrate=%30u", &bit_rate) == 1) {
 					if (bit_rate != 32000) {
 						ast_log(LOG_WARNING, "Got Siren7 offer at %d bps, but only 32000 bps supported; ignoring.\n", bit_rate);
@@ -11260,8 +11309,7 @@
 						found = TRUE;
 					}
 				}
-				break;
-			case AST_FORMAT_SIREN14:
+			} else if (ast_format_cmp(format, ast_format_siren14) == AST_FORMAT_CMP_EQUAL) {
 				if (sscanf(fmtp_string, "bitrate=%30u", &bit_rate) == 1) {
 					if (bit_rate != 48000) {
 						ast_log(LOG_WARNING, "Got Siren14 offer at %d bps, but only 48000 bps supported; ignoring.\n", bit_rate);
@@ -11270,8 +11318,7 @@
 						found = TRUE;
 					}
 				}
-				break;
-			case AST_FORMAT_G719:
+			} else if (ast_format_cmp(format, ast_format_g719) == AST_FORMAT_CMP_EQUAL) {
 				if (sscanf(fmtp_string, "bitrate=%30u", &bit_rate) == 1) {
 					if (bit_rate != 64000) {
 						ast_log(LOG_WARNING, "Got G.719 offer at %d bps, but only 64000 bps supported; ignoring.\n", bit_rate);
@@ -11280,7 +11327,6 @@
 						found = TRUE;
 					}
 				}
-				break;
 			}
 		}
 	}
@@ -12881,27 +12927,20 @@
 	int *max_packet_size)
 {
 	int rtp_code;
-	struct ast_format_list fmt;
 	const char *mime;
-	unsigned int rate;
+	unsigned int rate, framing;
 
 	if (debug)
-		ast_verbose("Adding codec %d (%s) to SDP\n", format->id, ast_getformatname(format));
+		ast_verbose("Adding codec %s to SDP\n", ast_format_get_name(format));
 
 	if (((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->rtp), 1, format, 0)) == -1) ||
 	    !(mime = ast_rtp_lookup_mime_subtype2(1, format, 0, ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0)) ||
 	    !(rate = ast_rtp_lookup_sample_rate2(1, format, 0))) {
 		return;
 	}
-
-	if (p->rtp) {
-		struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(p->rtp)->pref;
-		fmt = ast_codec_pref_getsize(pref, format);
-	} else /* I don't see how you couldn't have p->rtp, but good to check for and error out if not there like earlier code */
-		return;
 	ast_str_append(m_buf, 0, " %d", rtp_code);
 	/* Opus mandates 2 channels in rtpmap */
-	if ((int)format->id == AST_FORMAT_OPUS) {
+	if (ast_format_cmp(format, ast_format_opus) == AST_FORMAT_CMP_EQUAL) {
 		ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d/2\r\n", rtp_code, mime, rate);
 	} else {
 		ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code, mime, rate);
@@ -12909,48 +12948,44 @@
 
 	ast_format_sdp_generate(format, rtp_code, a_buf);
 
-	switch ((int) format->id) {
-	case AST_FORMAT_G729A:
+	framing = ast_format_cap_get_framing(p->caps, format);
+
+	if (ast_format_cmp(format, ast_format_g729) == AST_FORMAT_CMP_EQUAL) {
 		/* Indicate that we don't support VAD (G.729 annex B) */
 		ast_str_append(a_buf, 0, "a=fmtp:%d annexb=no\r\n", rtp_code);
-		break;
-	case AST_FORMAT_G723_1:
+	} else if (ast_format_cmp(format, ast_format_g723) == AST_FORMAT_CMP_EQUAL) {
 		/* Indicate that we don't support VAD (G.723.1 annex A) */
 		ast_str_append(a_buf, 0, "a=fmtp:%d annexa=no\r\n", rtp_code);
-		break;
-	case AST_FORMAT_ILBC:
+	} else if (ast_format_cmp(format, ast_format_ilbc) == AST_FORMAT_CMP_EQUAL) {
 		/* Add information about us using only 20/30 ms packetization */
-		ast_str_append(a_buf, 0, "a=fmtp:%d mode=%d\r\n", rtp_code, fmt.cur_ms);
-		break;
-	case AST_FORMAT_SIREN7:
+		ast_str_append(a_buf, 0, "a=fmtp:%d mode=%d\r\n", rtp_code, framing);
+	} else if (ast_format_cmp(format, ast_format_siren7) == AST_FORMAT_CMP_EQUAL) {
 		/* Indicate that we only expect 32Kbps */
 		ast_str_append(a_buf, 0, "a=fmtp:%d bitrate=32000\r\n", rtp_code);
-		break;
-	case AST_FORMAT_SIREN14:
+	} else if (ast_format_cmp(format, ast_format_siren14) == AST_FORMAT_CMP_EQUAL) {
 		/* Indicate that we only expect 48Kbps */
 		ast_str_append(a_buf, 0, "a=fmtp:%d bitrate=48000\r\n", rtp_code);
-		break;
-	case AST_FORMAT_G719:
+	} else if (ast_format_cmp(format, ast_format_g719) == AST_FORMAT_CMP_EQUAL) {
 		/* Indicate that we only expect 64Kbps */
 		ast_str_append(a_buf, 0, "a=fmtp:%d bitrate=64000\r\n", rtp_code);
-		break;
-	}
-
-	if (max_packet_size && fmt.max_ms && (fmt.max_ms < *max_packet_size)) {
-		*max_packet_size = fmt.max_ms;
-	}
-
-	if (fmt.cur_ms && (fmt.cur_ms < *min_packet_size)) {
-		*min_packet_size = fmt.cur_ms;
+	}
+
+	if (max_packet_size && ast_format_get_maximum_ms(format) &&
+		(ast_format_get_maximum_ms(format) < *max_packet_size)) {
+		*max_packet_size = ast_format_get_maximum_ms(format);
+	}
+
+	if (framing && (framing < *min_packet_size)) {
+		*min_packet_size = framing;
 	}
 
 	/* Our first codec packetization processed cannot be zero */
-	if ((*min_packet_size) == 0 && fmt.cur_ms) {
-		*min_packet_size = fmt.cur_ms;
-	}
-
-	if ((*max_packet_size) == 0 && fmt.max_ms) {
-		*max_packet_size = fmt.max_ms;
+	if ((*min_packet_size) == 0 && framing) {
+		*min_packet_size = framing;
+	}
+
+	if ((*max_packet_size) == 0 && ast_format_get_maximum_ms(format)) {
+		*max_packet_size = ast_format_get_maximum_ms(format);
 	}
 }
 
@@ -12968,7 +13003,7 @@
 		return;
 
 	if (debug)
-		ast_verbose("Adding video codec %d (%s) to SDP\n", format->id, ast_getformatname(format));
+		ast_verbose("Adding video codec %s to SDP\n", ast_format_get_name(format));
 
 	if (((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->vrtp), 1, format, 0)) == -1) ||
 	    !(subtype = ast_rtp_lookup_mime_subtype2(1, format, 0, 0)) ||
@@ -12979,7 +13014,7 @@
 	ast_str_append(m_buf, 0, " %d", rtp_code);
 	ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code, subtype, rate);
 	/* VP8: add RTCP FIR support */
-	if ((int)format->id == AST_FORMAT_VP8) {
+	if (ast_format_cmp(format, ast_format_vp8) == AST_FORMAT_CMP_EQUAL) {
 		ast_str_append(a_buf, 0, "a=rtcp-fb:* ccm fir\r\n");
 	}
 
@@ -12997,7 +13032,7 @@
 		return;
 
 	if (debug)
-		ast_verbose("Adding text codec %d (%s) to SDP\n", format->id, ast_getformatname(format));
+		ast_verbose("Adding text codec %s to SDP\n", ast_format_get_name(format));
 
 	if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->trtp), 1, format, 0)) == -1)
 		return;
@@ -13008,9 +13043,8 @@
 		       ast_rtp_lookup_sample_rate2(1, format, 0));
 	/* Add fmtp code here */
 
-	if (format->id == AST_FORMAT_T140RED) {
-		struct ast_format tmp_fmt;
-		int t140code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->trtp), 1, ast_format_set(&tmp_fmt, AST_FORMAT_T140, 0), 0);
+	if (ast_format_cmp(format, ast_format_t140_red) == AST_FORMAT_CMP_EQUAL) {
+		int t140code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->trtp), 1, ast_format_t140, 0);
 		ast_str_append(a_buf, 0, "a=fmtp:%d %d/%d/%d\r\n", rtp_code,
 			 t140code,
 			 t140code,
@@ -13185,8 +13219,8 @@
 */
 static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38)
 {
-	struct ast_format_cap *alreadysent = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
-	struct ast_format_cap *tmpcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
+	struct ast_format_cap *alreadysent = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	struct ast_format_cap *tmpcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
 	int res = AST_SUCCESS;
 	int doing_directmedia = FALSE;
 	struct ast_sockaddr addr = { {0,} };
@@ -13220,7 +13254,7 @@
 	RAII_VAR(char *, t_a_crypto, NULL, ast_free);
 
 	int x;
-	struct ast_format tmp_fmt;
+	struct ast_format *tmp_fmt;
 	int needaudio = FALSE;
 	int needvideo = FALSE;
 	int needtext = FALSE;
@@ -13259,11 +13293,11 @@
 	}
 
 	if (add_audio) {
-		doing_directmedia = (!ast_sockaddr_isnull(&p->redirip) && !(ast_format_cap_is_empty(p->redircaps))) ? TRUE : FALSE;
+		doing_directmedia = (!ast_sockaddr_isnull(&p->redirip) && (ast_format_cap_count(p->redircaps))) ? TRUE : FALSE;
 		/* Check if we need video in this call */
-		if ((ast_format_cap_has_type(p->jointcaps, AST_FORMAT_TYPE_VIDEO)) && !p->novideo) {
-			ast_format_cap_joint_copy(p->jointcaps, p->redircaps, tmpcap);
-			if (doing_directmedia && !ast_format_cap_has_type(tmpcap, AST_FORMAT_TYPE_VIDEO)) {
+		if ((ast_format_cap_has_type(p->jointcaps, AST_MEDIA_TYPE_VIDEO)) && !p->novideo) {
+			ast_format_cap_get_compatible(p->jointcaps, p->redircaps, tmpcap);
+			if (doing_directmedia && !ast_format_cap_has_type(tmpcap, AST_MEDIA_TYPE_VIDEO)) {
 				ast_debug(2, "This call needs video offers, but caller probably did not offer it!\n");
 			} else if (p->vrtp) {
 				needvideo = TRUE;
@@ -13273,7 +13307,7 @@
 			}
 		}
 		/* Check if we need text in this call */
-		if ((ast_format_cap_has_type(p->jointcaps, AST_FORMAT_TYPE_TEXT)) && !p->notext) {
+		if ((ast_format_cap_has_type(p->jointcaps, AST_MEDIA_TYPE_TEXT)) && !p->notext) {
 			if (sipdebug_text)
 				ast_verbose("We think we can do text\n");
 			if (p->trtp) {
@@ -13313,7 +13347,7 @@
 			hold = "a=sendrecv\r\n";
 		}
 
-		ast_format_cap_copy(tmpcap, p->jointcaps);
+		ast_format_cap_append_by_type(tmpcap, p->jointcaps, AST_MEDIA_TYPE_UNKNOWN);
 
 		/* XXX note, Video and Text are negated - 'true' means 'no' */
 		ast_debug(1, "** Our capability: %s Video flag: %s Text flag: %s\n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), tmpcap),
@@ -13321,12 +13355,12 @@
 		ast_debug(1, "** Our prefcodec: %s \n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), p->prefcaps));
 
 		if (doing_directmedia) {
-			ast_format_cap_joint_copy(p->jointcaps, p->redircaps, tmpcap);
+			ast_format_cap_get_compatible(p->jointcaps, p->redircaps, tmpcap);
 			ast_debug(1, "** Our native-bridge filtered capablity: %s\n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), tmpcap));
 		}
 
 		/* Check if we need audio */
-		if (ast_format_cap_has_type(tmpcap, AST_FORMAT_TYPE_AUDIO))
+		if (ast_format_cap_has_type(tmpcap, AST_MEDIA_TYPE_AUDIO))
 			needaudio = TRUE;
 
 		if (debug) {
@@ -13403,57 +13437,61 @@
 		 * configured codecs.
 		 */
 		if (!ast_test_flag(&p->flags[2], SIP_PAGE3_IGNORE_PREFCAPS)) {
-			ast_format_cap_iter_start(p->prefcaps);
-			while (!(ast_format_cap_iter_next(p->prefcaps, &tmp_fmt))) {
-				if (AST_FORMAT_GET_TYPE(tmp_fmt.id) != AST_FORMAT_TYPE_AUDIO ||
-					!ast_format_cap_iscompatible(tmpcap, &tmp_fmt)) {
+			for (x = 0; x < ast_format_cap_count(p->prefcaps); x++) {
+				tmp_fmt = ast_format_cap_get_format(p->prefcaps, x);
+
+				if ((ast_format_get_type(tmp_fmt) != AST_MEDIA_TYPE_AUDIO) ||
+					(!ast_format_cap_iscompatible_format(tmpcap, tmp_fmt))) {
+					ao2_ref(tmp_fmt, -1);
 					continue;
 				}
-				add_codec_to_sdp(p, &tmp_fmt, &m_audio, &a_audio, debug, &min_audio_packet_size, &max_audio_packet_size);
-				ast_format_cap_add(alreadysent, &tmp_fmt);
-			}
-			ast_format_cap_iter_end(p->prefcaps);
+
+				add_codec_to_sdp(p, tmp_fmt, &m_audio, &a_audio, debug, &min_audio_packet_size, &max_audio_packet_size);
+				ast_format_cap_add(alreadysent, tmp_fmt, 0);
+				ao2_ref(tmp_fmt, -1);
+			}
 		}
 
 		/* Start by sending our preferred audio/video codecs */
-		for (x = 0; x < AST_CODEC_PREF_SIZE; x++) {
-			struct ast_format pref;
-
-			if (!(ast_codec_pref_index(&p->prefs, x, &pref)))
-				break;
-
-			if (!ast_format_cap_get_compatible_format(tmpcap, &pref, &tmp_fmt))
+		for (x = 0; x < ast_format_cap_count(p->caps); x++) {
+			tmp_fmt = ast_format_cap_get_format(p->caps, x);
+
+			if (ast_format_cap_iscompatible_format(alreadysent, tmp_fmt)) {
+				ao2_ref(tmp_fmt, -1);
 				continue;
-
-			if (ast_format_cap_iscompatible(alreadysent, &tmp_fmt))
+			}
+
+			if (ast_format_get_type(tmp_fmt) == AST_MEDIA_TYPE_AUDIO) {
+				add_codec_to_sdp(p, tmp_fmt, &m_audio, &a_audio, debug, &min_audio_packet_size, &max_audio_packet_size);
+			} else if (needvideo && ast_format_get_type(tmp_fmt) == AST_MEDIA_TYPE_VIDEO) {
+				add_vcodec_to_sdp(p, tmp_fmt, &m_video, &a_video, debug, &min_video_packet_size);
+			} else if (needtext && ast_format_get_type(tmp_fmt) == AST_MEDIA_TYPE_TEXT) {
+				add_tcodec_to_sdp(p, tmp_fmt, &m_text, &a_text, debug, &min_text_packet_size);
+			}
+
+			ast_format_cap_add(alreadysent, tmp_fmt, 0);
+			ao2_ref(tmp_fmt, -1);
+		}
+
+		/* Now send any other common audio and video codecs, and non-codec formats: */
+		for (x = 0; x < ast_format_cap_count(tmpcap); x++) {
+			tmp_fmt = ast_format_cap_get_format(tmpcap, x);
+
+			if (ast_format_cap_iscompatible_format(alreadysent, tmp_fmt)) {
+				ao2_ref(tmp_fmt, -1);
 				continue;
-
-			if (AST_FORMAT_GET_TYPE(tmp_fmt.id) == AST_FORMAT_TYPE_AUDIO) {
-				add_codec_to_sdp(p, &tmp_fmt, &m_audio, &a_audio, debug, &min_audio_packet_size, &max_audio_packet_size);
-			} else if (needvideo && (AST_FORMAT_GET_TYPE(tmp_fmt.id) == AST_FORMAT_TYPE_VIDEO)) {
-				add_vcodec_to_sdp(p, &tmp_fmt, &m_video, &a_video, debug, &min_video_packet_size);
-			} else if (needtext && (AST_FORMAT_GET_TYPE(tmp_fmt.id) == AST_FORMAT_TYPE_TEXT)) {
-				add_tcodec_to_sdp(p, &tmp_fmt, &m_text, &a_text, debug, &min_text_packet_size);
-			}
-
-			ast_format_cap_add(alreadysent, &tmp_fmt);
-		}
-
-		/* Now send any other common audio and video codecs, and non-codec formats: */
-		ast_format_cap_iter_start(tmpcap);
-		while (!(ast_format_cap_iter_next(tmpcap, &tmp_fmt))) {
-			if (ast_format_cap_iscompatible(alreadysent, &tmp_fmt))
-				continue;
-
-			if (AST_FORMAT_GET_TYPE(tmp_fmt.id) == AST_FORMAT_TYPE_AUDIO) {
-				add_codec_to_sdp(p, &tmp_fmt, &m_audio, &a_audio, debug, &min_audio_packet_size, &max_audio_packet_size);
-			} else if (needvideo && (AST_FORMAT_GET_TYPE(tmp_fmt.id) == AST_FORMAT_TYPE_VIDEO)) {
-				add_vcodec_to_sdp(p, &tmp_fmt, &m_video, &a_video, debug, &min_video_packet_size);
-			} else if (needtext && (AST_FORMAT_GET_TYPE(tmp_fmt.id) == AST_FORMAT_TYPE_TEXT)) {
-				add_tcodec_to_sdp(p, &tmp_fmt, &m_text, &a_text, debug, &min_text_packet_size);
-			}
-		}
-		ast_format_cap_iter_end(tmpcap);
+			}
+
+			if (ast_format_get_type(tmp_fmt) == AST_MEDIA_TYPE_AUDIO) {

[... 446 lines stripped ...]



More information about the asterisk-commits mailing list