[asterisk-commits] file: branch group/media_formats r408004 - /team/group/media_formats/res/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Feb 13 11:13:29 CST 2014


Author: file
Date: Thu Feb 13 11:13:26 2014
New Revision: 408004

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=408004
Log:
I dream of a world without fax.

Modified:
    team/group/media_formats/res/res_fax.c
    team/group/media_formats/res/res_fax_spandsp.c

Modified: team/group/media_formats/res/res_fax.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats/res/res_fax.c?view=diff&rev=408004&r1=408003&r2=408004
==============================================================================
--- team/group/media_formats/res/res_fax.c (original)
+++ team/group/media_formats/res/res_fax.c Thu Feb 13 11:13:26 2014
@@ -90,6 +90,7 @@
 #include "asterisk/stasis.h"
 #include "asterisk/stasis_channels.h"
 #include "asterisk/smoother.h"
+#include "asterisk/format_cache.h"
 
 /*** DOCUMENTATION
 	<application name="ReceiveFAX" language="en_US" module="res_fax">
@@ -272,10 +273,10 @@
 	/*! \brief a flag to track the state of our negotiation */
 	enum ast_t38_state t38_state;
 	/*! \brief original audio formats */
-	struct ast_format chan_read_format;
-	struct ast_format chan_write_format;
-	struct ast_format peer_read_format;
-	struct ast_format peer_write_format;
+	struct ast_format *chan_read_format;
+	struct ast_format *chan_write_format;
+	struct ast_format *peer_read_format;
+	struct ast_format *peer_write_format;
 };
 
 /*! \brief used for fax detect framehook */
@@ -287,7 +288,7 @@
 	/*! \brief DSP Processor */
 	struct ast_dsp *dsp;
 	/*! \brief original audio formats */
-	struct ast_format orig_format;
+	struct ast_format *orig_format;
 	/*! \brief fax session details */
 	struct ast_fax_session_details *details;
 	/*! \brief mode */
@@ -1361,13 +1362,11 @@
 	struct ast_fax_session *fax = NULL;
 	struct ast_frame *frame = NULL;
 	struct ast_channel *c = chan;
-	struct ast_format orig_write_format;
-	struct ast_format orig_read_format;
+	RAII_VAR(struct ast_format *, orig_write_format, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_format *, orig_read_format, NULL, ao2_cleanup);
 	int remaining_time;
 	struct timeval start;
 
-	ast_format_clear(&orig_write_format);
-	ast_format_clear(&orig_read_format);
 	chancount = 1;
 
 	/* create the FAX session */
@@ -1391,10 +1390,10 @@
 	report_fax_status(chan, details, "Allocating Resources");
 
 	if (details->caps & AST_FAX_TECH_AUDIO) {
-		expected_frametype = AST_FRAME_VOICE;;
-		ast_format_set(&expected_framesubclass.format, AST_FORMAT_SLINEAR, 0);
-		ast_format_copy(&orig_write_format, ast_channel_writeformat(chan));
-		if (ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR) < 0) {
+		expected_frametype = AST_FRAME_VOICE;
+		expected_framesubclass.format = ast_format_slin;
+		orig_write_format = ast_format_copy(ast_channel_writeformat(chan));
+		if (ast_set_write_format(chan, ast_format_slin) < 0) {
 			ast_log(LOG_ERROR, "channel '%s' failed to set write format to signed linear'.\n", ast_channel_name(chan));
  			ao2_lock(faxregistry.container);
  			ao2_unlink(faxregistry.container, fax);
@@ -1403,8 +1402,8 @@
 			ast_channel_unlock(chan);
 			return -1;
 		}
-		ast_format_copy(&orig_read_format, ast_channel_readformat(chan));
-		if (ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR) < 0) {
+		orig_read_format = ast_format_copy(ast_channel_readformat(chan));
+		if (ast_set_read_format(chan, ast_format_slin) < 0) {
 			ast_log(LOG_ERROR, "channel '%s' failed to set read format to signed linear.\n", ast_channel_name(chan));
  			ao2_lock(faxregistry.container);
  			ao2_unlink(faxregistry.container, fax);
@@ -1593,11 +1592,11 @@
 	 * restore them now
 	 */
 	if (chancount) {
-		if (orig_read_format.id) {
-			ast_set_read_format(chan, &orig_read_format);
-		}
-		if (orig_write_format.id) {
-			ast_set_write_format(chan, &orig_write_format);
+		if (orig_read_format) {
+			ast_set_read_format(chan, orig_read_format);
+		}
+		if (orig_write_format) {
+			ast_set_write_format(chan, orig_write_format);
 		}
 	}
 
@@ -2606,6 +2605,11 @@
 		ao2_ref(gateway->s, -1);
 		gateway->s = NULL;
 	}
+
+	ao2_cleanup(gateway->chan_read_format);
+	ao2_cleanup(gateway->chan_write_format);
+	ao2_cleanup(gateway->peer_read_format);
+	ao2_cleanup(gateway->peer_write_format);
 }
 
 /*! \brief Create a new fax gateway object.
@@ -3057,14 +3061,14 @@
 		set_channel_variables(chan, details);
 
 		if (gateway->bridged) {
-			ast_set_read_format(chan, &gateway->chan_read_format);
-			ast_set_read_format(chan, &gateway->chan_write_format);
+			ast_set_read_format(chan, gateway->chan_read_format);
+			ast_set_read_format(chan, gateway->chan_write_format);
 
 			ast_channel_unlock(chan);
 			peer = ast_channel_bridge_peer(chan);
 			if (peer) {
-				ast_set_read_format(peer, &gateway->peer_read_format);
-				ast_set_read_format(peer, &gateway->peer_write_format);
+				ast_set_read_format(peer, gateway->peer_read_format);
+				ast_set_read_format(peer, gateway->peer_write_format);
 				ast_channel_make_compatible(chan, peer);
 			}
 			ast_channel_lock(chan);
@@ -3109,18 +3113,18 @@
 
 		/* we are bridged, change r/w formats to SLIN for v21 preamble
 		 * detection and T.30 */
-		ast_format_copy(&gateway->chan_read_format, ast_channel_readformat(chan));
-		ast_format_copy(&gateway->chan_write_format, ast_channel_readformat(chan));
-
-		ast_format_copy(&gateway->peer_read_format, ast_channel_readformat(peer));
-		ast_format_copy(&gateway->peer_write_format, ast_channel_readformat(peer));
-
-		ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR);
-		ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR);
+		gateway->chan_read_format = ast_format_copy(ast_channel_readformat(chan));
+		gateway->chan_write_format = ast_format_copy(ast_channel_readformat(chan));
+
+		gateway->peer_read_format = ast_format_copy(ast_channel_readformat(peer));
+		gateway->peer_write_format = ast_format_copy(ast_channel_readformat(peer));
+
+		ast_set_read_format(chan, ast_format_slin);
+		ast_set_write_format(chan, ast_format_slin);
 
 		ast_channel_unlock(chan);
-		ast_set_read_format_by_id(peer, AST_FORMAT_SLINEAR);
-		ast_set_write_format_by_id(peer, AST_FORMAT_SLINEAR);
+		ast_set_read_format(peer, ast_format_slin);
+		ast_set_write_format(peer, ast_format_slin);
 
 		ast_channel_make_compatible(chan, peer);
 		ast_channel_lock(chan);
@@ -3144,12 +3148,9 @@
 	/* only handle VOICE, MODEM, and CONTROL frames*/
 	switch (f->frametype) {
 	case AST_FRAME_VOICE:
-		switch (f->subclass.format.id) {
-		case AST_FORMAT_SLINEAR:
-		case AST_FORMAT_ALAW:
-		case AST_FORMAT_ULAW:
-			break;
-		default:
+		if ((ast_format_cmp(f->subclass.format, ast_format_slin) != AST_FORMAT_CMP_EQUAL) &&
+			(ast_format_cmp(f->subclass.format, ast_format_alaw) != AST_FORMAT_CMP_EQUAL) &&
+			(ast_format_cmp(f->subclass.format, ast_format_ulaw) != AST_FORMAT_CMP_EQUAL)) {
 			return f;
 		}
 		break;
@@ -3195,7 +3196,7 @@
 	if (gateway->t38_state == T38_STATE_NEGOTIATED) {
 		/* framehooks are called in __ast_read() before frame format
 		 * translation is done, so we need to translate here */
-		if ((f->frametype == AST_FRAME_VOICE) && (f->subclass.format.id != AST_FORMAT_SLINEAR)) {
+		if ((f->frametype == AST_FRAME_VOICE) && (ast_format_cmp(f->subclass.format, ast_format_slin) != AST_FORMAT_CMP_EQUAL)) {
 			if (ast_channel_readtrans(active) && (f = ast_translate(ast_channel_readtrans(active), f, 1)) == NULL) {
 				f = &ast_null_frame;
 				return f;
@@ -3207,7 +3208,8 @@
 		 * write would fail, or even if a failure would be fatal so for
 		 * now we'll just ignore the return value. */
 		gateway->s->tech->write(gateway->s, f);
-		if ((f->frametype == AST_FRAME_VOICE) && (f->subclass.format.id != AST_FORMAT_SLINEAR) && ast_channel_readtrans(active)) {
+		if ((f->frametype == AST_FRAME_VOICE) && (ast_format_cmp(f->subclass.format, ast_format_slin) != AST_FORMAT_CMP_EQUAL) &&
+			ast_channel_readtrans(active)) {
 			/* Only free the frame if we translated / duplicated it - otherwise,
 			 * let whatever is outside the frame hook do it */
 			ast_frfree(f);
@@ -3218,15 +3220,16 @@
 
 	/* force silence on the line if T.38 negotiation might be taking place */
 	if (gateway->t38_state != T38_STATE_UNAVAILABLE && gateway->t38_state != T38_STATE_REJECTED) {
-		if (f->frametype == AST_FRAME_VOICE && f->subclass.format.id == AST_FORMAT_SLINEAR) {
+		if (f->frametype == AST_FRAME_VOICE &&
+			(ast_format_cmp(f->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL)) {
 			short silence_buf[f->samples];
 			struct ast_frame silence_frame = {
 				.frametype = AST_FRAME_VOICE,
+				.subclass.format = ast_format_slin,
 				.data.ptr = silence_buf,
 				.samples = f->samples,
 				.datalen = sizeof(silence_buf),
 			};
-			ast_format_set(&silence_frame.subclass.format, AST_FORMAT_SLINEAR, 0);
 			memset(silence_buf, 0, sizeof(silence_buf));
 			return ast_frisolate(&silence_frame);
 		} else {
@@ -3295,6 +3298,7 @@
 		faxdetect->dsp = NULL;
 	}
 	ao2_ref(faxdetect->details, -1);
+	ao2_cleanup(faxdetect->orig_format);
 }
 
 /*! \brief Create a new fax detect object.
@@ -3369,23 +3373,22 @@
 	switch (event) {
 	case AST_FRAMEHOOK_EVENT_ATTACHED:
 		/* Setup format for DSP on ATTACH*/
-		ast_format_copy(&faxdetect->orig_format, ast_channel_readformat(chan));
-		switch (ast_channel_readformat(chan)->id) {
-			case AST_FORMAT_SLINEAR:
-			case AST_FORMAT_ALAW:
-			case AST_FORMAT_ULAW:
-				break;
-			default:
-				if (ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR)) {
-					ast_framehook_detach(chan, details->faxdetect_id);
-					details->faxdetect_id = -1;
-					return f;
-				}
-		}
+		faxdetect->orig_format = ast_format_copy(ast_channel_readformat(chan));
+
+		if ((ast_format_cmp(ast_channel_readformat(chan), ast_format_slin) != AST_FORMAT_CMP_EQUAL) &&
+			(ast_format_cmp(ast_channel_readformat(chan), ast_format_alaw) != AST_FORMAT_CMP_EQUAL) &&
+			(ast_format_cmp(ast_channel_readformat(chan), ast_format_ulaw) != AST_FORMAT_CMP_EQUAL)) {
+			if (ast_set_read_format(chan, ast_format_slin)) {
+				ast_framehook_detach(chan, details->faxdetect_id);
+				details->faxdetect_id = -1;
+				return f;
+			}
+		}
+
 		return NULL;
 	case AST_FRAMEHOOK_EVENT_DETACHED:
 		/* restore audio formats when we are detached */
-		ast_set_read_format(chan, &faxdetect->orig_format);
+		ast_set_read_format(chan, faxdetect->orig_format);
 		ast_channel_unlock(chan);
 		peer = ast_channel_bridge_peer(chan);
 		if (peer) {
@@ -3420,13 +3423,10 @@
 			return f;
 		}
 		/* We can only process some formats*/
-		switch (f->subclass.format.id) {
-			case AST_FORMAT_SLINEAR:
-			case AST_FORMAT_ALAW:
-			case AST_FORMAT_ULAW:
-				break;
-			default:
-				return f;
+		if ((ast_format_cmp(f->subclass.format, ast_format_slin) != AST_FORMAT_CMP_EQUAL) &&
+			(ast_format_cmp(f->subclass.format, ast_format_alaw) != AST_FORMAT_CMP_EQUAL) &&
+			(ast_format_cmp(f->subclass.format, ast_format_ulaw) != AST_FORMAT_CMP_EQUAL)) {
+			return f;
 		}
 		break;
 	case AST_FRAME_CONTROL:

Modified: team/group/media_formats/res/res_fax_spandsp.c
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats/res/res_fax_spandsp.c?view=diff&rev=408004&r1=408003&r2=408004
==============================================================================
--- team/group/media_formats/res/res_fax_spandsp.c (original)
+++ team/group/media_formats/res/res_fax_spandsp.c Thu Feb 13 11:13:26 2014
@@ -65,6 +65,7 @@
 #include "asterisk/astobj2.h"
 #include "asterisk/res_fax.h"
 #include "asterisk/channel.h"
+#include "asterisk/format_cache.h"
 
 #define SPANDSP_FAX_SAMPLES 160
 #define SPANDSP_FAX_TIMER_RATE 8000 / SPANDSP_FAX_SAMPLES	/* 50 ticks per second, 20ms, 160 samples per second */
@@ -606,9 +607,9 @@
 	struct ast_frame fax_frame = {
 		.frametype = AST_FRAME_VOICE,
 		.src = "res_fax_spandsp_g711",
+		.subclass.format = ast_format_slin,
 	};
 	struct ast_frame *f = &fax_frame;
-	ast_format_set(&fax_frame.subclass.format, AST_FORMAT_SLINEAR, 0);
 
 	if (ast_timer_ack(p->timer, 1) < 0) {
 		ast_log(LOG_ERROR, "Failed to acknowledge timer for FAX session '%d'\n", s->id);
@@ -718,6 +719,7 @@
 	struct ast_frame *f;
 	struct ast_frame t30_frame = {
 		.frametype = AST_FRAME_VOICE,
+		.subclass.format = ast_format_slin,
 		.src = "res_fax_spandsp_g711",
 		.samples = samples,
 		.flags = AST_FAX_FRFLAG_GATEWAY,
@@ -725,7 +727,6 @@
 
 	AST_FRAME_SET_BUFFER(&t30_frame, buffer, AST_FRIENDLY_OFFSET, t30_frame.samples * sizeof(int16_t));
 
-	ast_format_set(&t30_frame.subclass.format, AST_FORMAT_SLINEAR, 0);
 	if (!(f = ast_frisolate(&t30_frame))) {
 		return p->isdone ? -1 : res;
 	}
@@ -847,7 +848,8 @@
 	/* Process a IFP packet */
 	if ((f->frametype == AST_FRAME_MODEM) && (f->subclass.integer == AST_MODEM_T38)) {
 		return t38_core_rx_ifp_packet(p->t38_core_state, f->data.ptr, f->datalen, f->seqno);
-	} else if ((f->frametype == AST_FRAME_VOICE) && (f->subclass.format.id == AST_FORMAT_SLINEAR)) {
+	} else if ((f->frametype == AST_FRAME_VOICE) &&
+		(ast_format_cmp(f->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL)) {
 		return t38_gateway_rx(&p->t38_gw_state, f->data.ptr, f->samples);
 	}
 




More information about the asterisk-commits mailing list