[Asterisk-code-review] Binaural synthesis (confbridge): interleaved two-channel audio. (asterisk[master])

Frank Haase asteriskteam at digium.com
Fri Aug 12 11:33:27 CDT 2016


Frank Haase has uploaded a new change for review.

  https://gerrit.asterisk.org/3521

Change subject: Binaural synthesis (confbridge): interleaved two-channel audio.
......................................................................

Binaural synthesis (confbridge): interleaved two-channel audio.

Asterisk only supports mono audio at the moment.
This patch adds interleaved two-channel audio to Asterisk's channels.

ASTERISK-26292

Change-Id: I7a547cea0fd3c6d1e502709d9e7e39605035757a
---
M include/asterisk/channel.h
M include/asterisk/translate.h
M main/channel.c
3 files changed, 44 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/21/3521/1

diff --git a/include/asterisk/channel.h b/include/asterisk/channel.h
index 14bd32c..94f72b8 100644
--- a/include/asterisk/channel.h
+++ b/include/asterisk/channel.h
@@ -2017,6 +2017,16 @@
 int ast_set_write_format(struct ast_channel *chan, struct ast_format *format);
 
 /*!
+ * \brief Sets write format for a channel.
+ * All internal data will than be handled in an interleaved format. (needed by binaural opus)
+ *
+ * \param chan channel to change
+ * \param format format to set for writing
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_set_write_format_interleaved_stereo(struct ast_channel *chan, struct ast_format *format);
+
+/*!
  * \brief Sends text to a channel
  *
  * \param chan channel to act upon
diff --git a/include/asterisk/translate.h b/include/asterisk/translate.h
index b8cd219..880c365 100644
--- a/include/asterisk/translate.h
+++ b/include/asterisk/translate.h
@@ -210,6 +210,7 @@
 	struct ast_translator *t;
 	struct ast_frame f;         /*!< used in frameout */
 	int samples;                /*!< samples available in outbuf */
+	int interleaved_stereo; 	/*!< indicates if samples are in interleaved order, for stereo lin */
 	/*! \brief actual space used in outbuf */
 	int datalen;
 	void *pvt;                  /*!< more private data, if any */
diff --git a/main/channel.c b/main/channel.c
index 1f18d53..fc8d010 100644
--- a/main/channel.c
+++ b/main/channel.c
@@ -5407,7 +5407,7 @@
 	.setoption = AST_OPTION_FORMAT_WRITE,
 };
 
-static int set_format(struct ast_channel *chan, struct ast_format_cap *cap_set, const int direction)
+static int set_format(struct ast_channel *chan, struct ast_format_cap *cap_set, const int direction, int interleaved_stereo)
 {
 	struct ast_trans_pvt *trans_pvt;
 	struct ast_format_cap *cap_native;
@@ -5512,9 +5512,14 @@
 	if ((ast_format_cmp(rawformat, best_native_fmt) != AST_FORMAT_CMP_NOT_EQUAL) &&
 		(ast_format_cmp(format, best_set_fmt) != AST_FORMAT_CMP_NOT_EQUAL) &&
 		((ast_format_cmp(rawformat, format) != AST_FORMAT_CMP_NOT_EQUAL) || access->get_trans(chan))) {
-		/* the channel is already in these formats, so nothing to do */
-		ast_channel_unlock(chan);
-		return 0;
+		/* the channel is already in these formats, so nothing to do, unless the interleaved format is not set correctly */
+		trans_pvt = access->get_trans(chan);
+		if (trans_pvt != NULL) {
+			if (trans_pvt->interleaved_stereo == interleaved_stereo) {
+				ast_channel_unlock(chan);
+				return 0;
+			}
+		}
 	}
 
 	/* Free any translation we have right now */
@@ -5536,9 +5541,11 @@
 		if (!direction) {
 			/* reading */
 			trans_pvt = ast_translator_build_path(best_set_fmt, best_native_fmt);
+			trans_pvt->interleaved_stereo = 0;
 		} else {
 			/* writing */
 			trans_pvt = ast_translator_build_path(best_native_fmt, best_set_fmt);
+			trans_pvt->interleaved_stereo = interleaved_stereo;
 		}
 		access->set_trans(chan, trans_pvt);
 		res = trans_pvt ? 0 : -1;
@@ -5578,7 +5585,7 @@
 	}
 	ast_format_cap_append(cap, format, 0);
 
-	res = set_format(chan, cap, 0);
+	res = set_format(chan, cap, 0, 0);
 
 	ao2_cleanup(cap);
 	return res;
@@ -5586,7 +5593,25 @@
 
 int ast_set_read_format_from_cap(struct ast_channel *chan, struct ast_format_cap *cap)
 {
-	return set_format(chan, cap, 0);
+	return set_format(chan, cap, 0, 0);
+}
+
+int ast_set_write_format_interleaved_stereo(struct ast_channel *chan, struct ast_format *format)
+{
+	struct ast_format_cap *cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	int res;
+
+	ast_assert(format != NULL);
+
+	if (!cap) {
+		return -1;
+	}
+	ast_format_cap_append(cap, format, 0);
+
+	res = set_format(chan, cap, 1, 1);
+
+	ao2_cleanup(cap);
+	return res;
 }
 
 int ast_set_write_format(struct ast_channel *chan, struct ast_format *format)
@@ -5601,7 +5626,7 @@
 	}
 	ast_format_cap_append(cap, format, 0);
 
-	res = set_format(chan, cap, 1);
+	res = set_format(chan, cap, 1, 0);
 
 	ao2_cleanup(cap);
 	return res;
@@ -5609,7 +5634,7 @@
 
 int ast_set_write_format_from_cap(struct ast_channel *chan, struct ast_format_cap *cap)
 {
-	return set_format(chan, cap, 1);
+	return set_format(chan, cap, 1, 0);
 }
 
 const char *ast_channel_reason2str(int reason)

-- 
To view, visit https://gerrit.asterisk.org/3521
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7a547cea0fd3c6d1e502709d9e7e39605035757a
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Frank Haase <fra.haase at googlemail.com>



More information about the asterisk-code-review mailing list