[asterisk-commits] dvossel: branch dvossel/fixtheworld_phase1_step3 r302316 - in /team/dvossel/f...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Jan 18 15:54:36 CST 2011


Author: dvossel
Date: Tue Jan 18 15:54:31 2011
New Revision: 302316

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=302316
Log:
conversion of chan_h323 to use ast_format api

Modified:
    team/dvossel/fixtheworld_phase1_step3/channels/chan_h323.c
    team/dvossel/fixtheworld_phase1_step3/channels/h323/ast_h323.cxx
    team/dvossel/fixtheworld_phase1_step3/channels/h323/ast_h323.h
    team/dvossel/fixtheworld_phase1_step3/channels/h323/chan_h323.h

Modified: team/dvossel/fixtheworld_phase1_step3/channels/chan_h323.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/fixtheworld_phase1_step3/channels/chan_h323.c?view=diff&rev=302316&r1=302315&r2=302316
==============================================================================
--- team/dvossel/fixtheworld_phase1_step3/channels/chan_h323.c (original)
+++ team/dvossel/fixtheworld_phase1_step3/channels/chan_h323.c Tue Jan 18 15:54:31 2011
@@ -129,7 +129,6 @@
 static char default_context[AST_MAX_CONTEXT] = "default";
 static struct sockaddr_in bindaddr;
 
-#define GLOBAL_CAPABILITY (AST_FORMAT_G723_1 | AST_FORMAT_GSM | AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_G729A | AST_FORMAT_G726_AAL2 | AST_FORMAT_H261)
 
 /** H.323 configuration values */
 static int h323_signalling_port = 1720;
@@ -166,17 +165,17 @@
 	int amaflags;				/*!< AMA Flags */
 	struct ast_rtp_instance *rtp;		/*!< RTP Session */
 	struct ast_dsp *vad;			/*!< Used for in-band DTMF detection */
-	int nativeformats;			/*!< Codec formats supported by a channel */
 	int needhangup;				/*!< Send hangup when Asterisk is ready */
 	int hangupcause;			/*!< Hangup cause from OpenH323 layer */
 	int newstate;				/*!< Pending state change */
 	int newcontrol;				/*!< Pending control to send */
 	int newdigit;				/*!< Pending DTMF digit to send */
 	int newduration;			/*!< Pending DTMF digit duration to send */
-	format_t pref_codec;				/*!< Preferred codec */
-	format_t peercapability;			/*!< Capabilities learned from peer */
-	format_t jointcapability;			/*!< Common capabilities for local and remote side */
-	struct ast_codec_pref peer_prefs;	/*!< Preferenced list of codecs which remote side supports */
+	struct ast_format pref_codec;     /*!< Preferred codec */
+	struct ast_format_cap *peercap;   /*!< Capabilities learned from peer */
+	struct ast_format_cap *jointcap;  /*!< Common capabilities for local and remote side */
+	struct ast_format_cap *nativeformats; /*!< Codec formats supported by a channel */
+	struct ast_codec_pref peer_prefs;   /*!< Preferenced list of codecs which remote side supports */
 	int dtmf_pt[2];				/*!< Payload code used for RFC2833/CISCO messages */
 	int curDTMF;				/*!< DTMF tone being generated to Asterisk side */
 	int DTMFsched;				/*!< Scheduler descriptor for DTMF */
@@ -231,7 +230,7 @@
 static void delete_aliases(void);
 static void prune_peers(void);
 
-static struct ast_channel *oh323_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause);
+static struct ast_channel *oh323_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause);
 static int oh323_digit_begin(struct ast_channel *c, char digit);
 static int oh323_digit_end(struct ast_channel *c, char digit, unsigned int duration);
 static int oh323_call(struct ast_channel *c, char *dest, int timeout);
@@ -242,10 +241,9 @@
 static int oh323_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen);
 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
 
-static const struct ast_channel_tech oh323_tech = {
+static struct ast_channel_tech oh323_tech = {
 	.type = "H323",
 	.description = tdesc,
-	.capabilities = AST_FORMAT_AUDIO_MASK,
 	.properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
 	.requester = oh323_request,
 	.send_digit_begin = oh323_digit_begin,
@@ -260,6 +258,14 @@
 	.bridge = ast_rtp_instance_bridge,
 };
 
+static void call_options_copy(call_options_t *dst, call_options_t *src)
+{
+	struct ast_format_cap *tmpcap = dst->cap;
+	memcpy(dst, src, sizeof(*dst));
+	dst->cap = tmpcap;
+	ast_format_cap_copy2(dst->cap, src->cap);
+}
+
 static const char* redirectingreason2str(int redirectingreason)
 {
 	switch (redirectingreason) {
@@ -288,6 +294,7 @@
 	if (h323debug)
 		ast_debug(1, "Destroying user '%s'\n", user->name);
 	ast_free_ha(user->ha);
+	user->options.cap = ast_format_cap_destroy(user->options.cap);
 	ast_free(user);
 }
 
@@ -296,6 +303,7 @@
 	if (h323debug)
 		ast_debug(1, "Destroying peer '%s'\n", peer->name);
 	ast_free_ha(peer->ha);
+	peer->options.cap = ast_format_cap_destroy(peer->options.cap);
 	ast_free(peer);
 }
 
@@ -331,12 +339,12 @@
 /*! \brief Channel and private structures should be already locked */
 static void __oh323_update_info(struct ast_channel *c, struct oh323_pvt *pvt)
 {
-	if (c->nativeformats != pvt->nativeformats) {
+	if (!(ast_format_cap_identical(c->nativeformats, pvt->nativeformats))) {
 		if (h323debug)
 			ast_debug(1, "Preparing %s for new native format\n", c->name);
-		c->nativeformats = pvt->nativeformats;
-		ast_set_read_format(c, c->readformat);
-		ast_set_write_format(c, c->writeformat);
+		ast_format_cap_copy2(c->nativeformats, pvt->nativeformats);
+		ast_set_read_format(c, &c->readformat);
+		ast_set_write_format(c, &c->writeformat);
 	}
 	if (pvt->needhangup) {
 		if (h323debug)
@@ -481,6 +489,10 @@
 	} else {
 		ast_mutex_unlock(&pvt->lock);
 		ast_mutex_destroy(&pvt->lock);
+		pvt->peercap = ast_format_cap_destroy(pvt->peercap);
+		pvt->jointcap = ast_format_cap_destroy(pvt->jointcap);
+		pvt->nativeformats = ast_format_cap_destroy(pvt->nativeformats);
+		pvt->options.cap = ast_format_cap_destroy(pvt->options.cap);
 		ast_free(pvt);
 	}
 }
@@ -764,31 +776,38 @@
 	if (pvt->owner) {
 		/* We already hold the channel lock */
 		if (f->frametype == AST_FRAME_VOICE) {
-			if (f->subclass.codec != pvt->owner->nativeformats) {
+			if (!(ast_format_cap_iscompatible(pvt->owner->nativeformats, &f->subclass.format))) {
 				/* Try to avoid deadlock */
 				if (ast_channel_trylock(pvt->owner)) {
 					ast_log(LOG_NOTICE, "Format changed but channel is locked. Ignoring frame...\n");
 					return &ast_null_frame;
 				}
 				if (h323debug)
-					ast_debug(1, "Oooh, format changed to '%s'\n", ast_getformatname(f->subclass.codec));
-				pvt->owner->nativeformats = f->subclass.codec;
-				pvt->nativeformats = f->subclass.codec;
-				ast_set_read_format(pvt->owner, pvt->owner->readformat);
-				ast_set_write_format(pvt->owner, pvt->owner->writeformat);
+					ast_debug(1, "Oooh, format changed to '%s'\n", ast_getformatname(&f->subclass.format));
+				ast_format_cap_remove_all(pvt->owner->nativeformats);
+				ast_format_cap_add(pvt->owner->nativeformats, &f->subclass.format);
+
+				ast_format_cap_remove_all(pvt->nativeformats);
+				ast_format_cap_add(pvt->nativeformats, &f->subclass.format);
+
+				ast_set_read_format(pvt->owner, &pvt->owner->readformat);
+				ast_set_write_format(pvt->owner, &pvt->owner->writeformat);
 				ast_channel_unlock(pvt->owner);
 			}
 			/* Do in-band DTMF detection */
 			if ((pvt->options.dtmfmode & H323_DTMF_INBAND) && pvt->vad) {
-				if ((pvt->nativeformats & (AST_FORMAT_SLINEAR | AST_FORMAT_ALAW | AST_FORMAT_ULAW))) {
+				struct ast_format tmpfmt;
+				if (ast_format_cap_iscompatible(pvt->nativeformats, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0)) ||
+					ast_format_cap_iscompatible(pvt->nativeformats, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0)) ||
+					ast_format_cap_iscompatible(pvt->nativeformats, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0))) {
 					if (!ast_channel_trylock(pvt->owner)) {
 						f = ast_dsp_process(pvt->owner, pvt->vad, f);
 						ast_channel_unlock(pvt->owner);
 					}
 					else
 						ast_log(LOG_NOTICE, "Unable to process inband DTMF while channel is locked\n");
-				} else if (pvt->nativeformats && !pvt->noInbandDtmf) {
-					ast_log(LOG_NOTICE, "Inband DTMF is not supported on codec %s. Use RFC2833\n", ast_getformatname(f->subclass.codec));
+				} else if (!ast_format_cap_is_empty(pvt->nativeformats) && !pvt->noInbandDtmf) {
+					ast_log(LOG_NOTICE, "Inband DTMF is not supported on codec %s. Use RFC2833\n", ast_getformatname(&f->subclass.format));
 					pvt->noInbandDtmf = 1;
 				}
 				if (f &&(f->frametype == AST_FRAME_DTMF)) {
@@ -838,10 +857,10 @@
 			return 0;
 		}
 	} else {
-		if (!(frame->subclass.codec & c->nativeformats)) {
+		if (!(ast_format_cap_iscompatible(c->nativeformats, &frame->subclass.format))) {
 			char tmp[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.codec), ast_getformatname_multiple(tmp, sizeof(tmp), c->nativeformats), ast_getformatname(c->readformat), ast_getformatname(c->writeformat));
+				ast_getformatname(&frame->subclass.format), ast_getformatname_multiple(tmp, sizeof(tmp), c->nativeformats), ast_getformatname(&c->readformat), ast_getformatname(&c->writeformat));
 			return 0;
 		}
 	}
@@ -991,8 +1010,9 @@
 	if (pvt->dtmf_pt[1] > 0)
 		ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(pvt->rtp), pvt->rtp, pvt->dtmf_pt[1], "audio", "cisco-telephone-event", 0);
 
-	if (pvt->peercapability)
+	if (!(ast_format_cap_is_empty(pvt->peercap))) {
 		ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(pvt->rtp), pvt->rtp, &pvt->peer_prefs);
+	}
 
 	if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
 		ast_jb_configure(pvt->owner, &global_jbconf);
@@ -1011,7 +1031,8 @@
 {
 	struct ast_channel *ch;
 	char *cid_num, *cid_name;
-	int fmt;
+	struct ast_format_cap *tmpcap; /* SHALLOW COPY DO NOT DESTROY */
+	struct ast_format tmpfmt;
 
 	if (!ast_strlen_zero(pvt->options.cid_num))
 		cid_num = pvt->options.cid_num;
@@ -1031,15 +1052,24 @@
 	ast_mutex_lock(&pvt->lock);
 	if (ch) {
 		ch->tech = &oh323_tech;
-		if (!(fmt = pvt->jointcapability) && !(fmt = pvt->options.capability))
-			fmt = global_options.capability;
-		ch->nativeformats = ast_codec_choose(&pvt->options.prefs, fmt, 1)/* | (pvt->jointcapability & AST_FORMAT_VIDEO_MASK)*/;
-		pvt->nativeformats = ch->nativeformats;
-		fmt = ast_best_codec(ch->nativeformats);
-		ch->writeformat = fmt;
-		ch->rawwriteformat = fmt;
-		ch->readformat = fmt;
-		ch->rawreadformat = fmt;
+		if (!(ast_format_cap_is_empty(pvt->jointcap))) {
+			tmpcap = pvt->jointcap;
+		} else if (!(ast_format_cap_is_empty(pvt->options.cap))) {
+			tmpcap = pvt->options.cap;
+		} else {
+			tmpcap = global_options.cap;
+		}
+
+		ast_codec_choose(&pvt->options.prefs, tmpcap, 1, &tmpfmt)/* | (pvt->jointcapability & AST_FORMAT_VIDEO_MASK)*/;
+		ast_format_cap_add(ch->nativeformats, &tmpfmt);
+		ast_format_cap_copy2(pvt->nativeformats, ch->nativeformats);
+
+		ast_best_codec(ch->nativeformats, &tmpfmt);
+		ast_format_copy(&ch->writeformat, &tmpfmt);
+		ast_format_copy(&ch->rawwriteformat, &tmpfmt);
+		ast_format_copy(&ch->readformat, &tmpfmt);
+		ast_format_copy(&ch->rawreadformat, &tmpfmt);
+
 		if (!pvt->rtp)
 			__oh323_rtp_create(pvt);
 #if 0
@@ -1123,6 +1153,15 @@
 		ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
 		return NULL;
 	}
+	pvt->peercap = ast_format_cap_alloc();
+	pvt->jointcap = ast_format_cap_alloc();
+	pvt->nativeformats = ast_format_cap_alloc();
+	pvt->options.cap = ast_format_cap_alloc();
+	if (!pvt->peercap || !pvt->jointcap || !pvt->options.cap || !pvt->nativeformats) {
+		ast_free(pvt);
+		return NULL;
+	}
+
 	pvt->cd.redirect_reason = -1;
 	pvt->cd.transfer_capability = -1;
 	/* Ensure the call token is allocated for outgoing call */
@@ -1139,8 +1178,8 @@
 		memset((char *)(pvt->cd).call_token, 0, 128);
 		pvt->cd.call_reference = callid;
 	}
-	memcpy(&pvt->options, &global_options, sizeof(pvt->options));
-	pvt->jointcapability = pvt->options.capability;
+	call_options_copy(&pvt->options, &global_options);
+	ast_format_cap_copy2(pvt->jointcap, pvt->options.cap);
 	if (pvt->options.dtmfmode & (H323_DTMF_RFC2833 | H323_DTMF_CISCO)) {
 		pvt->nonCodecCapability |= AST_RTP_DTMF;
 	} else {
@@ -1270,11 +1309,11 @@
 	char *val, *opt;
 
 	if (!strcasecmp(v->name, "allow")) {
-		ast_parse_allow_disallow(&options->prefs, &options->capability, v->value, 1);
+		ast_parse_allow_disallow(&options->prefs, options->cap, v->value, 1);
 	} else if (!strcasecmp(v->name, "autoframing")) {
 		options->autoframing = ast_true(v->value);
 	} else if (!strcasecmp(v->name, "disallow")) {
-		ast_parse_allow_disallow(&options->prefs, &options->capability, v->value, 0);
+		ast_parse_allow_disallow(&options->prefs, options->cap, v->value, 0);
 	} else if (!strcasecmp(v->name, "dtmfmode")) {
 		val = ast_strdupa(v->value);
 		if ((opt = strchr(val, ':')) != (char *)NULL) {
@@ -1385,13 +1424,20 @@
 	if (user)
 		found++;
 	else {
-		if (!(user = ast_calloc(1, sizeof(*user))))
+		if (!(user = ast_calloc(1, sizeof(*user)))) {
 			return NULL;
+		}
+		if (!(user->options.cap = ast_format_cap_alloc())) {
+			ast_free(user);
+			return NULL;
+		}
 		ASTOBJ_INIT(user);
 	}
+
+
 	oldha = user->ha;
 	user->ha = (struct ast_ha *)NULL;
-	memcpy(&user->options, &global_options, sizeof(user->options));
+	call_options_copy(&user->options, &global_options);
 	user->options.dtmfmode = 0;
 	user->options.holdHandling = 0;
 	/* Set default context */
@@ -1507,13 +1553,18 @@
 	if (peer)
 		found++;
 	else {
-		if (!(peer = ast_calloc(1, sizeof(*peer))))
+		if (!(peer = ast_calloc(1, sizeof(*peer)))) {
 			return NULL;
+		}
+		if (!(peer->options.cap = ast_format_cap_alloc())) {
+			ast_free(peer);
+			return NULL;
+		}
 		ASTOBJ_INIT(peer);
 	}
 	oldha = peer->ha;
 	peer->ha = NULL;
-	memcpy(&peer->options, &global_options, sizeof(peer->options));
+	call_options_copy(&peer->options, &global_options);
 	peer->options.dtmfmode = 0;
 	peer->options.holdHandling = 0;
 	peer->addr.sin_port = htons(h323_signalling_port);
@@ -1694,8 +1745,8 @@
 	p = find_peer(peer, NULL, 1);
 	if (p) {
 		found++;
-		memcpy(&pvt->options, &p->options, sizeof(pvt->options));
-		pvt->jointcapability = pvt->options.capability;
+		call_options_copy(&pvt->options, &p->options);
+		ast_format_cap_copy2(pvt->jointcap, pvt->options.cap);
 		if (pvt->options.dtmfmode) {
 			if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
 				pvt->nonCodecCapability |= AST_RTP_DTMF;
@@ -1722,8 +1773,8 @@
 			pvt->sa.sin_port = htons(portno);
 			/* Look peer by address */
 			p = find_peer(NULL, &pvt->sa, 1);
-			memcpy(&pvt->options, (p ? &p->options : &global_options), sizeof(pvt->options));
-			pvt->jointcapability = pvt->options.capability;
+			call_options_copy(&pvt->options, (p ? &p->options : &global_options));
+			ast_format_cap_copy2(pvt->jointcap, pvt->options.cap);
 			if (p) {
 				ASTOBJ_UNREF(p, oh323_destroy_peer);
 			}
@@ -1745,9 +1796,8 @@
 		return 0;
 	}
 }
-static struct ast_channel *oh323_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
-{
-	format_t oldformat;
+static struct ast_channel *oh323_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause)
+{
 	struct oh323_pvt *pvt;
 	struct ast_channel *tmpc = NULL;
 	char *dest = (char *)data;
@@ -1756,17 +1806,15 @@
 	char tmp[256], tmp1[256];
 
 	if (h323debug)
-		ast_debug(1, "type=%s, format=%s, data=%s.\n", type, ast_getformatname_multiple(tmp, sizeof(tmp), format), (char *)data);
+		ast_debug(1, "type=%s, format=%s, data=%s.\n", type, ast_getformatname_multiple(tmp, sizeof(tmp), cap), (char *)data);
 
 	pvt = oh323_alloc(0);
 	if (!pvt) {
 		ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
 		return NULL;
 	}
-	oldformat = format;
-	format &= AST_FORMAT_AUDIO_MASK;
-	if (!format) {
-		ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n", ast_getformatname_multiple(tmp, sizeof(tmp), format));
+	if (!(ast_format_cap_has_type(cap, AST_FORMAT_TYPE_AUDIO))) {
+		ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n", ast_getformatname_multiple(tmp, sizeof(tmp), cap));
 		oh323_destroy(pvt);
 		if (cause)
 			*cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
@@ -1803,8 +1851,8 @@
 		}
 	}
 	else {
-		memcpy(&pvt->options, &global_options, sizeof(pvt->options));
-		pvt->jointcapability = pvt->options.capability;
+		call_options_copy(&pvt->options, &global_options);
+		ast_format_cap_copy2(pvt->jointcap, pvt->options.cap);
 		if (pvt->options.dtmfmode) {
 			if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
 				pvt->nonCodecCapability |= AST_RTP_DTMF;
@@ -1969,6 +2017,7 @@
 	struct oh323_pvt *pvt;
 	struct sockaddr_in them;
 	int nativeformats_changed;
+	struct ast_format tmpfmt;
 	enum { NEED_NONE, NEED_HOLD, NEED_UNHOLD } rtp_change = NEED_NONE;
 
 	if (h323debug)
@@ -1988,7 +2037,7 @@
 	if (!pvt->rtp)
 		__oh323_rtp_create(pvt);
 
-	if ((pt == 2) && (pvt->jointcapability & AST_FORMAT_G726_AAL2)) {
+	if ((pt == 2) && (ast_format_cap_iscompatible(pvt->jointcap, ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0)))) {
 		ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(pvt->rtp), pvt->rtp, pt, "audio", "G726-32", AST_RTP_OPT_G726_NONSTANDARD);
 	}
 
@@ -2020,11 +2069,18 @@
 	nativeformats_changed = 0;
 	if (pt != 128 && pvt->rtp) {	/* Payload type is invalid, so try to use previously decided */
 		struct ast_rtp_payload_type rtptype = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(pvt->rtp), pt);
-		if (h323debug)
-			ast_debug(1, "Native format is set to %llu from %d by RTP payload type %d\n", (unsigned long long) rtptype.code, pvt->nativeformats, pt);
-		if (pvt->nativeformats != rtptype.code) {
-			pvt->nativeformats = rtptype.code;
-			nativeformats_changed = 1;
+		char tmp[256];
+		if (rtptype.asterisk_format) {
+			if (h323debug) {
+				ast_debug(1, "Native format is set to %s from %s by RTP payload type %d\n",
+					ast_getformatname(&rtptype.format),
+					ast_getformatname_multiple(tmp, sizeof(tmp), pvt->nativeformats), pt);
+			}
+			if (!(ast_format_cap_iscompatible(pvt->nativeformats, &rtptype.format))) {
+				ast_format_cap_remove_all(pvt->nativeformats);
+				ast_format_cap_add(pvt->nativeformats, &rtptype.format);
+				nativeformats_changed = 1;
+			}
 		}
 	} else if (h323debug)
 		ast_log(LOG_NOTICE, "Payload type is unknown, formats isn't changed\n");
@@ -2033,14 +2089,14 @@
 	if (nativeformats_changed || pvt->options.progress_audio || (rtp_change != NEED_NONE)) {
 		if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
 			/* Re-build translation path only if native format(s) has been changed */
-			if (pvt->owner->nativeformats != pvt->nativeformats) {
+			if (!ast_format_cap_identical(pvt->owner->nativeformats, pvt->nativeformats)) {
 				if (h323debug) {
 					char tmp[256], tmp2[256];
-					ast_debug(1, "Native format changed to '%s' from '%s', read format is %s, write format is %s\n", ast_getformatname_multiple(tmp, sizeof(tmp), pvt->nativeformats), ast_getformatname_multiple(tmp2, sizeof(tmp2), pvt->owner->nativeformats), ast_getformatname(pvt->owner->readformat), ast_getformatname(pvt->owner->writeformat));
+					ast_debug(1, "Native format changed to '%s' from '%s', read format is %s, write format is %s\n", ast_getformatname_multiple(tmp, sizeof(tmp), pvt->nativeformats), ast_getformatname_multiple(tmp2, sizeof(tmp2), pvt->owner->nativeformats), ast_getformatname(&pvt->owner->readformat), ast_getformatname(&pvt->owner->writeformat));
 				}
-				pvt->owner->nativeformats = pvt->nativeformats;
-				ast_set_read_format(pvt->owner, pvt->owner->readformat);
-				ast_set_write_format(pvt->owner, pvt->owner->writeformat);
+				ast_format_cap_copy2(pvt->owner->nativeformats, pvt->nativeformats);
+				ast_set_read_format(pvt->owner, &pvt->owner->readformat);
+				ast_set_write_format(pvt->owner, &pvt->owner->writeformat);
 			}
 			if (pvt->options.progress_audio)
 				ast_queue_control(pvt->owner, AST_CONTROL_PROGRESS);
@@ -2154,8 +2210,8 @@
 
 	/* Populate the call details in the private structure */
 	memcpy(&pvt->cd, cd, sizeof(pvt->cd));
-	memcpy(&pvt->options, &global_options, sizeof(pvt->options));
-	pvt->jointcapability = pvt->options.capability;
+	call_options_copy(&pvt->options, &global_options);
+	ast_format_cap_copy2(pvt->jointcap, pvt->options.cap);
 
 	if (h323debug) {
 		ast_verb(3, "Setting up Call\n");
@@ -2230,8 +2286,8 @@
 				}
 			}
 			ast_copy_string(pvt->context, user->context, sizeof(pvt->context));
-			memcpy(&pvt->options, &user->options, sizeof(pvt->options));
-			pvt->jointcapability = pvt->options.capability;
+			call_options_copy(&pvt->options, &user->options);
+			ast_format_cap_copy2(pvt->jointcap, pvt->options.cap);
 			if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
 				ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
 			} else {
@@ -2472,7 +2528,7 @@
 		ast_debug(1, "DTMF payload on %s set to %d\n", token, payload);
 }
 
-static void set_peer_capabilities(unsigned call_reference, const char *token, int capabilities, struct ast_codec_pref *prefs)
+static void set_peer_capabilities(unsigned call_reference, const char *token, struct ast_format_cap *cap, struct ast_codec_pref *prefs)
 {
 	struct oh323_pvt *pvt;
 
@@ -2482,16 +2538,16 @@
 	pvt = find_call_locked(call_reference, token);
 	if (!pvt)
 		return;
-	pvt->peercapability = capabilities;
-	pvt->jointcapability = pvt->options.capability & capabilities;
+	ast_format_cap_copy2(pvt->peercap, cap);
+	ast_format_cap_joint_copy(pvt->options.cap, cap, pvt->jointcap);
 	if (prefs) {
 		memcpy(&pvt->peer_prefs, prefs, sizeof(pvt->peer_prefs));
 		if (h323debug) {
 			int i;
-			for (i = 0; i < 32; ++i) {
+			for (i = 0; i < AST_CODEC_PREF_SIZE; ++i) {
 				if (!prefs->order[i])
 					break;
-				ast_debug(1, "prefs[%d]=%s:%d\n", i, (prefs->order[i] ? ast_getformatname(1 << (prefs->order[i]-1)) : "<none>"), prefs->framing[i]);
+				ast_debug(1, "prefs[%d]=%s:%d\n", i, (prefs->order[i] ? ast_getformatname(&prefs->formats[i]) : "<none>"), prefs->framing[i]);
 			}
 		}
 		if (pvt->rtp) {
@@ -2510,7 +2566,9 @@
 static void set_local_capabilities(unsigned call_reference, const char *token)
 {
 	struct oh323_pvt *pvt;
-	int capability, dtmfmode, pref_codec;
+	struct ast_format_cap *cap;
+	int dtmfmode;
+	struct ast_format pref_codec;
 	struct ast_codec_pref prefs;
 
 	if (h323debug)
@@ -2519,19 +2577,25 @@
 	pvt = find_call_locked(call_reference, token);
 	if (!pvt)
 		return;
-	capability = (pvt->jointcapability) ? pvt->jointcapability : pvt->options.capability;
+	cap = ast_format_cap_copy((!(ast_format_cap_is_empty(pvt->jointcap))) ? pvt->jointcap : pvt->options.cap);
 	dtmfmode = pvt->options.dtmfmode;
 	prefs = pvt->options.prefs;
-	pref_codec = pvt->pref_codec;
+	ast_format_copy(&pref_codec, &pvt->pref_codec);
 	ast_mutex_unlock(&pvt->lock);
-	h323_set_capabilities(token, capability, dtmfmode, &prefs, pref_codec);
+
+	if (cap) {
+		h323_set_capabilities(token, cap, dtmfmode, &prefs, &pref_codec);
+		cap = ast_format_cap_destroy(cap);
+	} else {
+		return;
+	}
 
 	if (h323debug) {
 		int i;
 		for (i = 0; i < 32; i++) {
 			if (!prefs.order[i])
 				break;
-			ast_debug(1, "local prefs[%d]=%s:%d\n", i, (prefs.order[i] ? ast_getformatname(1 << (prefs.order[i]-1)) : "<none>"), prefs.framing[i]);
+			ast_debug(1, "local prefs[%d]=%s:%d\n", i, (prefs.order[i] ? ast_getformatname(&prefs.formats[i]) : "<none>"), prefs.framing[i]);
 		}
 		ast_debug(1, "Capabilities for connection %s is set\n", token);
 	}
@@ -2880,6 +2944,8 @@
 	char _gatekeeper[100];
 	int gk_discover, gk_disable, gk_changed;
 	struct ast_flags config_flags = { is_reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
+	struct ast_format tmpfmt;
+	struct ast_format_cap *tmpcap;
 
 	cfg = ast_config_load(config, config_flags);
 
@@ -2927,14 +2993,26 @@
 	gk_discover = gatekeeper_discover;
 	gk_disable = gatekeeper_disable;
 	memset(&bindaddr, 0, sizeof(bindaddr));
+
+	tmpcap = global_options.cap;
 	memset(&global_options, 0, sizeof(global_options));
+	global_options.cap = tmpcap;
 	global_options.fastStart = 1;
 	global_options.h245Tunneling = 1;
 	global_options.dtmfcodec[0] = H323_DTMF_RFC2833_PT;
 	global_options.dtmfcodec[1] = H323_DTMF_CISCO_PT;
 	global_options.dtmfmode = 0;
 	global_options.holdHandling = 0;
-	global_options.capability = GLOBAL_CAPABILITY;
+	ast_format_cap_remove_all(global_options.cap);
+	/* add the default capabilities */
+	ast_format_cap_add(global_options.cap, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0));
+	ast_format_cap_add(global_options.cap, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0));
+	ast_format_cap_add(global_options.cap, ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0));
+	ast_format_cap_add(global_options.cap, ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0));
+	ast_format_cap_add(global_options.cap, ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0));
+	ast_format_cap_add(global_options.cap, ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0));
+	ast_format_cap_add(global_options.cap, ast_format_set(&tmpfmt, AST_FORMAT_H261, 0));
+
 	global_options.bridge = 1;		/* Do native bridging by default */
 	global_options.autoframing = 0;
 	strcpy(default_context, "default");
@@ -3191,9 +3269,9 @@
 	return res;
 }
 
-static char *convertcap(format_t cap)
-{
-	switch (cap) {
+static char *convertformat(struct ast_format *format)
+{
+	switch (format->id) {
 	case AST_FORMAT_G723_1:
 		return "G.723";
 	case AST_FORMAT_GSM:
@@ -3213,12 +3291,12 @@
 	case AST_FORMAT_ILBC:
 		return "ILBC";
 	default:
-		ast_log(LOG_NOTICE, "Don't know how to deal with mode %" PRId64 "\n", cap);
+		ast_log(LOG_NOTICE, "Don't know how to deal with mode %s\n", ast_getformatname(format));
 		return NULL;
 	}
 }
 
-static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, format_t codecs, int nat_active)
+static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, const struct ast_format_cap *cap, int nat_active)
 {
 	/* XXX Deal with Video */
 	struct oh323_pvt *pvt;
@@ -3230,7 +3308,7 @@
 		return 0;
 	}
 
-	mode = convertcap(chan->writeformat);
+	mode = convertformat(&chan->writeformat);
 	pvt = (struct oh323_pvt *) chan->tech_pvt;
 	if (!pvt) {
 		ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
@@ -3260,6 +3338,13 @@
 {
 	int res;
 
+	if (!(oh323_tech.capabilities = ast_format_cap_alloc())) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
+	if (!(global_options.cap = ast_format_cap_alloc())) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
+	ast_format_cap_add_all_by_type(oh323_tech.capabilities, AST_FORMAT_TYPE_AUDIO);
 	h323debug = 0;
 	sched = ast_sched_context_create();
 	if (!sched) {
@@ -3430,6 +3515,8 @@
 	ASTOBJ_CONTAINER_DESTROYALL(&aliasl, oh323_destroy_alias);
 	ASTOBJ_CONTAINER_DESTROY(&aliasl);
 
+	oh323_tech.capabilities = ast_format_cap_destroy(oh323_tech.capabilities);
+	global_options.cap = ast_format_cap_destroy(global_options.cap);
 	return 0;
 }
 

Modified: team/dvossel/fixtheworld_phase1_step3/channels/h323/ast_h323.cxx
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/fixtheworld_phase1_step3/channels/h323/ast_h323.cxx?view=diff&rev=302316&r1=302315&r2=302316
==============================================================================
--- team/dvossel/fixtheworld_phase1_step3/channels/h323/ast_h323.cxx (original)
+++ team/dvossel/fixtheworld_phase1_step3/channels/h323/ast_h323.cxx Tue Jan 18 15:54:31 2011
@@ -1758,7 +1758,11 @@
 	}
 
 	memset(&prefs, 0, sizeof(prefs));
-	int peer_capabilities = 0;
+	struct ast_format_cap *peer_cap = ast_format_cap_alloc();
+	if (!peer_cap) {
+		return FALSE;
+	}
+
 	for (int i = 0; i < remoteCapabilities.GetSize(); ++i) {
 		unsigned int subType = remoteCapabilities[i].GetSubType();
 		if (h323debug) {
@@ -1768,22 +1772,23 @@
 		case H323Capability::e_Audio:
 			for (int x = 0; codecs[x].asterisk_codec > 0; ++x) {
 				if ((subType == codecs[x].h245_cap) && (!codecs[x].formatName || (!strcmp(codecs[x].formatName, (const char *)remoteCapabilities[i].GetFormatName())))) {
-					int ast_codec = codecs[x].asterisk_codec;
+					struct ast_format ast_codec;
 					int ms = 0;
-					if (!(peer_capabilities & ast_codec)) {
+					ast_format_set(&ast_codec, (enum ast_format_id) codecs[x].asterisk_codec, 0);
+					if (!(ast_format_cap_iscompatible(peer_cap, &ast_codec))) {
 						struct ast_format_list format;
-						ast_codec_pref_append(&prefs, ast_codec);
-						format = ast_codec_pref_getsize(&prefs, ast_codec);
-						if ((ast_codec == AST_FORMAT_ALAW) || (ast_codec == AST_FORMAT_ULAW)) {
+						ast_codec_pref_append(&prefs, &ast_codec);
+						format = ast_codec_pref_getsize(&prefs, &ast_codec);
+						if ((ast_codec.id == AST_FORMAT_ALAW) || (ast_codec.id == AST_FORMAT_ULAW)) {
 							ms = remoteCapabilities[i].GetTxFramesInPacket();
 						} else
 							ms = remoteCapabilities[i].GetTxFramesInPacket() * format.inc_ms;
-						ast_codec_pref_setsize(&prefs, ast_codec, ms);
+						ast_codec_pref_setsize(&prefs, &ast_codec, ms);
 					}
 					if (h323debug) {
-						cout << "Found peer capability " << remoteCapabilities[i] << ", Asterisk code is " << ast_codec << ", frame size (in ms) is " << ms << endl;
+						cout << "Found peer capability " << remoteCapabilities[i] << ", Asterisk code is " << ast_getformatname(&ast_codec) << ", frame size (in ms) is " << ms << endl;
 					}
-					peer_capabilities |= ast_codec;
+					ast_format_cap_add(peer_cap, &ast_codec);
 				}
 			}
 			break;
@@ -1849,15 +1854,16 @@
 	if (h323debug) {
 		char caps_str[1024], caps2_str[1024];
 		ast_codec_pref_string(&prefs, caps2_str, sizeof(caps2_str));
-		cout << "Peer capabilities = " << ast_getformatname_multiple(caps_str, sizeof(caps_str), peer_capabilities)
+		cout << "Peer capabilities = " << ast_getformatname_multiple(caps_str, sizeof(caps_str), peer_cap)
 				<< ", ordered list is " << caps2_str << endl;
 	}
 #if 0
 	redir_capabilities &= peer_capabilities;
 #endif
 	if (on_setpeercapabilities)
-		on_setpeercapabilities(GetCallReference(), (const char *)callToken, peer_capabilities, &prefs);
-
+		on_setpeercapabilities(GetCallReference(), (const char *)callToken, peer_cap, &prefs);
+
+	ast_format_cap_destroy(peer_cap);
 	return TRUE;
 }
 
@@ -1891,44 +1897,56 @@
 	return connectionState != ShuttingDownConnection;
 }
 
-void MyH323Connection::SetCapabilities(int caps, int dtmf_mode, void *_prefs, int pref_codec)
+void MyH323Connection::SetCapabilities(struct ast_format_cap *astcaps, int dtmf_mode, void *_prefs, struct ast_format *pref_codec)
 {
 	PINDEX lastcap = -1; /* last common capability index */
-	int alreadysent = 0;
-	int codec;
-	int x, y;
+	struct ast_format_cap *alreadysent = ast_format_cap_alloc();
+	struct ast_format tmpfmt;
+	int index;
 	char caps_str[1024];
 	struct ast_codec_pref *prefs = (struct ast_codec_pref *)_prefs;
 	struct ast_format_list format;
 	int frames_per_packet;
 	H323Capability *cap;
 
+	if (!alreadysent) {
+		return;
+	}
+
 	localCapabilities.RemoveAll();
 
 	if (h323debug) {
-		cout << "Setting capabilities to " << ast_getformatname_multiple(caps_str, sizeof(caps_str), caps) << endl;
+		cout << "Setting capabilities to " << ast_getformatname_multiple(caps_str, sizeof(caps_str), astcaps) << endl;
 		ast_codec_pref_string(prefs, caps_str, sizeof(caps_str));
 		cout << "Capabilities in preference order is " << caps_str << endl;
 	}
 	/* Add audio codecs in preference order first, then
 	   audio codecs without preference as allowed by mask */
-	for (y = 0, x = -1; x < 32 + 32; ++x) {
-		if (x < 0)
-			codec = pref_codec;
-		else if (y || (!(codec = ast_codec_pref_index(prefs, x)))) {
-			if (!y)
-				y = 1;
-			else
-				y <<= 1;
-			codec = y;
-		}
-		if (!(caps & codec) || (alreadysent & codec) || !(codec & AST_FORMAT_AUDIO_MASK))
+	ast_format_cap_iter_start(astcaps);
+	for (index = -1;;) {
+		ast_format_clear(&tmpfmt);
+		if (index < 0) {
+			ast_format_copy(&tmpfmt, pref_codec);
+		} else if (index < AST_CODEC_PREF_SIZE) {
+			ast_codec_pref_index(prefs, index, &tmpfmt);
+			index++;
+		} else if (!(ast_format_cap_iter_next(astcaps, &tmpfmt))) {
+			/*format is set to next in capabilities iterator */
+		} else {
+			break;
+		}
+		if (!(ast_format_cap_iscompatible(astcaps, &tmpfmt)) ||
+			ast_format_cap_iscompatible(alreadysent, &tmpfmt) ||
+			AST_FORMAT_GET_TYPE(tmpfmt.id) != AST_FORMAT_TYPE_AUDIO) {
 			continue;
-		alreadysent |= codec;
+		}
+		ast_format_cap_add(alreadysent, &tmpfmt);
+
 		/* format.cur_ms will be set to default if packetization is not explicitly set */
-		format = ast_codec_pref_getsize(prefs, codec);
+		format = ast_codec_pref_getsize(prefs, &tmpfmt);
 		frames_per_packet = (format.inc_ms ? format.cur_ms / format.inc_ms : format.cur_ms);
-		switch(codec) {
+
+		switch(tmpfmt.id) {
 #if 0
 		case AST_FORMAT_SPEEX:
 			/* Not real sure if Asterisk acutally supports all
@@ -1978,10 +1996,13 @@
 			g726Cap->SetTxFramesInPacket(format.cur_ms);
 			break;
 		default:
-			alreadysent &= ~codec;
-			break;
-		}
-	}
+			ast_format_cap_remove(alreadysent, &tmpfmt);
+			break;
+		}
+	}
+	ast_format_cap_iter_end(astcaps);
+	ast_format_cap_destroy(alreadysent);
+	alreadysent = NULL;
 
 	cap = new H323_UserInputCapability(H323_UserInputCapability::HookFlashH245);
 	if (cap && cap->IsUsable(*this)) {
@@ -2316,7 +2337,7 @@
 /**
  * Add capability to the capability table of the end point.
  */
-int h323_set_capabilities(const char *token, int cap, int dtmf_mode, struct ast_codec_pref *prefs, int pref_codec)
+int h323_set_capabilities(const char *token, struct ast_format_cap *cap, int dtmf_mode, struct ast_codec_pref *prefs, struct ast_format *pref_codec)
 {
 	MyH323Connection *conn;
 

Modified: team/dvossel/fixtheworld_phase1_step3/channels/h323/ast_h323.h
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/fixtheworld_phase1_step3/channels/h323/ast_h323.h?view=diff&rev=302316&r1=302315&r2=302316
==============================================================================
--- team/dvossel/fixtheworld_phase1_step3/channels/h323/ast_h323.h (original)
+++ team/dvossel/fixtheworld_phase1_step3/channels/h323/ast_h323.h Tue Jan 18 15:54:31 2011
@@ -30,6 +30,7 @@
 #define AST_H323_H
 
 #include "ast_ptlib.h"
+#include "asterisk/format.h"
 
 #define VERSION(a,b,c) ((a)*10000+(b)*100+(c))
 
@@ -85,7 +86,7 @@
 	PBoolean MySendProgress();
 	void OnSendCapabilitySet(H245_TerminalCapabilitySet &);
 	void OnSetLocalCapabilities();
-	void SetCapabilities(int, int, void *, int);
+	void SetCapabilities(struct ast_format_cap *cap, int, void *, struct ast_format *format);
 	PBoolean OnReceivedCapabilitySet(const H323Capabilities &, const H245_MultiplexCapability *,
 			H245_TerminalCapabilitySetReject &);
 	void SetCause(int _cause) { cause = _cause; };

Modified: team/dvossel/fixtheworld_phase1_step3/channels/h323/chan_h323.h
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/fixtheworld_phase1_step3/channels/h323/chan_h323.h?view=diff&rev=302316&r1=302315&r2=302316
==============================================================================
--- team/dvossel/fixtheworld_phase1_step3/channels/h323/chan_h323.h (original)
+++ team/dvossel/fixtheworld_phase1_step3/channels/h323/chan_h323.h Tue Jan 18 15:54:31 2011
@@ -65,7 +65,7 @@
 	int				progress_audio;
 	int				dtmfcodec[2];
 	int				dtmfmode;
-	format_t        capability;
+	struct ast_format_cap *cap;
 	int				bridge;
 	int				nat;
 	int				tunnelOptions;
@@ -195,7 +195,7 @@
 typedef void (*setcapabilities_cb)(unsigned, const char *);
 extern setcapabilities_cb on_setcapabilities;
 
-typedef void (*setpeercapabilities_cb)(unsigned, const char *, int, struct ast_codec_pref *);
+typedef void (*setpeercapabilities_cb)(unsigned, const char *, struct ast_format_cap *cap, struct ast_codec_pref *);
 extern setpeercapabilities_cb on_setpeercapabilities;
 
 typedef void (*onhold_cb)(unsigned, const char *, int);
@@ -239,7 +239,7 @@
 					setcapabilities_cb,
 					setpeercapabilities_cb,
 					onhold_cb);
-	int h323_set_capabilities(const char *, int, int, struct ast_codec_pref *, int);
+	int h323_set_capabilities(const char *, struct ast_format_cap *cap, int, struct ast_codec_pref *, struct ast_format *format);
 	int h323_set_alias(struct oh323_alias *);
 	int h323_set_gk(int, char *, char *);
 	void h323_set_id(char *);




More information about the asterisk-commits mailing list