[asterisk-commits] format ogg opus: New format (asterisk[master])

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Sep 27 14:12:42 CDT 2016


George Joseph has submitted this change and it was merged.

Change subject: format_ogg_opus: New format
......................................................................


format_ogg_opus: New format

Add Ogg/Opus playback support.

This uses libopusfile in order to be able to read .opus files and play
them back.

Writing/recording support is not present at this time.

ASTERISK-26409

Change-Id: I8815d23345108d8ca7c0bd640f6a1ce6b4f56955
(cherry picked from commit daee8bbd5209b4158bc1785eede845a26e6cbeaa)
---
M build_tools/menuselect-deps.in
M configure
M configure.ac
A formats/format_ogg_opus.c
M include/asterisk/autoconfig.h.in
M makeopts.in
6 files changed, 392 insertions(+), 0 deletions(-)

Approvals:
  George Joseph: Looks good to me, approved; Verified



diff --git a/build_tools/menuselect-deps.in b/build_tools/menuselect-deps.in
index 9b07768..18d4a01 100644
--- a/build_tools/menuselect-deps.in
+++ b/build_tools/menuselect-deps.in
@@ -45,6 +45,7 @@
 OGG=@PBX_OGG@
 OPENH323=@PBX_OPENH323@
 OPUS=@PBX_OPUS@
+OPUSFILE=@PBX_OPUSFILE@
 OSPTK=@PBX_OSPTK@
 OSS=@PBX_OSS@
 PGSQL=@PBX_PGSQL@
diff --git a/configure b/configure
index a5da739..dba401d 100755
--- a/configure
+++ b/configure
@@ -994,6 +994,10 @@
 OSPTK_DIR
 OSPTK_INCLUDE
 OSPTK_LIB
+PBX_OPUSFILE
+OPUSFILE_DIR
+OPUSFILE_INCLUDE
+OPUSFILE_LIB
 PBX_OPUS
 OPUS_DIR
 OPUS_INCLUDE
@@ -1398,6 +1402,7 @@
 with_ogg
 with_openr2
 with_opus
+with_opusfile
 with_osptk
 with_oss
 with_postgres
@@ -2156,6 +2161,7 @@
   --with-ogg=PATH         use OGG files in PATH
   --with-openr2=PATH      use MFR2 files in PATH
   --with-opus=PATH        use Opus files in PATH
+  --with-opusfile=PATH    use Opusfile files in PATH
   --with-osptk=PATH       use OSP Toolkit files in PATH
   --with-oss=PATH         use Open Sound System files in PATH
   --with-postgres=PATH    use PostgreSQL files in PATH
@@ -10830,6 +10836,38 @@
 	*)
 	OPUS_DIR="${withval}"
 	ac_mandatory_list="${ac_mandatory_list} OPUS"
+	;;
+	esac
+
+fi
+
+
+
+
+
+
+
+
+    OPUSFILE_DESCRIP="Opusfile"
+    OPUSFILE_OPTION="opusfile"
+    PBX_OPUSFILE=0
+
+# Check whether --with-opusfile was given.
+if test "${with_opusfile+set}" = set; then :
+  withval=$with_opusfile;
+	case ${withval} in
+	n|no)
+	USE_OPUSFILE=no
+	# -1 is a magic value used by menuselect to know that the package
+	# was disabled, other than 'not found'
+	PBX_OPUSFILE=-1
+	;;
+	y|ye|yes)
+	ac_mandatory_list="${ac_mandatory_list} OPUSFILE"
+	;;
+	*)
+	OPUSFILE_DIR="${withval}"
+	ac_mandatory_list="${ac_mandatory_list} OPUSFILE"
 	;;
 	esac
 
@@ -29512,6 +29550,116 @@
 fi
 
 
+# opusfile.h includes <opus_multistream.h> so we need to make sure that
+# either $OPUS_INCLUDE or /usr/include/opus is added to the search path.
+__opus_include=${OPUS_INCLUDE}
+if test -z "$__opus_include" -o x"$__opus_include" = x" " ; then
+	__opus_include=-I/usr/include/opus
+fi
+
+if test "x${PBX_OPUSFILE}" != "x1" -a "${USE_OPUSFILE}" != "no"; then
+   pbxlibdir=""
+   # if --with-OPUSFILE=DIR has been specified, use it.
+   if test "x${OPUSFILE_DIR}" != "x"; then
+      if test -d ${OPUSFILE_DIR}/lib; then
+         pbxlibdir="-L${OPUSFILE_DIR}/lib"
+      else
+         pbxlibdir="-L${OPUSFILE_DIR}"
+      fi
+   fi
+   pbxfuncname="op_open_callbacks"
+   if test "x${pbxfuncname}" = "x" ; then   # empty lib, assume only headers
+      AST_OPUSFILE_FOUND=yes
+   else
+      ast_ext_lib_check_save_CFLAGS="${CFLAGS}"
+      CFLAGS="${CFLAGS} $__opus_include"
+      as_ac_Lib=`$as_echo "ac_cv_lib_opusfile_${pbxfuncname}" | $as_tr_sh`
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for ${pbxfuncname} in -lopusfile" >&5
+$as_echo_n "checking for ${pbxfuncname} in -lopusfile... " >&6; }
+if eval \${$as_ac_Lib+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_check_lib_save_LIBS=$LIBS
+LIBS="-lopusfile ${pbxlibdir}  $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char ${pbxfuncname} ();
+int
+main ()
+{
+return ${pbxfuncname} ();
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  eval "$as_ac_Lib=yes"
+else
+  eval "$as_ac_Lib=no"
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+eval ac_res=\$$as_ac_Lib
+	       { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
+$as_echo "$ac_res" >&6; }
+if eval test \"x\$"$as_ac_Lib"\" = x"yes"; then :
+  AST_OPUSFILE_FOUND=yes
+else
+  AST_OPUSFILE_FOUND=no
+fi
+
+      CFLAGS="${ast_ext_lib_check_save_CFLAGS}"
+   fi
+
+   # now check for the header.
+   if test "${AST_OPUSFILE_FOUND}" = "yes"; then
+      OPUSFILE_LIB="${pbxlibdir} -lopusfile "
+      # if --with-OPUSFILE=DIR has been specified, use it.
+      if test "x${OPUSFILE_DIR}" != "x"; then
+         OPUSFILE_INCLUDE="-I${OPUSFILE_DIR}/include"
+      fi
+      OPUSFILE_INCLUDE="${OPUSFILE_INCLUDE} $__opus_include"
+      if test "xopus/opusfile.h" = "x" ; then	# no header, assume found
+         OPUSFILE_HEADER_FOUND="1"
+      else				# check for the header
+         ast_ext_lib_check_saved_CPPFLAGS="${CPPFLAGS}"
+         CPPFLAGS="${CPPFLAGS} ${OPUSFILE_INCLUDE}"
+         ac_fn_c_check_header_mongrel "$LINENO" "opus/opusfile.h" "ac_cv_header_opus_opusfile_h" "$ac_includes_default"
+if test "x$ac_cv_header_opus_opusfile_h" = xyes; then :
+  OPUSFILE_HEADER_FOUND=1
+else
+  OPUSFILE_HEADER_FOUND=0
+fi
+
+
+         CPPFLAGS="${ast_ext_lib_check_saved_CPPFLAGS}"
+      fi
+      if test "x${OPUSFILE_HEADER_FOUND}" = "x0" ; then
+         OPUSFILE_LIB=""
+         OPUSFILE_INCLUDE=""
+      else
+         if test "x${pbxfuncname}" = "x" ; then		# only checking headers -> no library
+            OPUSFILE_LIB=""
+         fi
+         PBX_OPUSFILE=1
+         cat >>confdefs.h <<_ACEOF
+#define HAVE_OPUSFILE 1
+_ACEOF
+
+      fi
+   fi
+fi
+
+
 
 if test "${USE_PWLIB}" != "no"; then
 	if test -n "${PWLIB_DIR}"; then
diff --git a/configure.ac b/configure.ac
index 17b8872..107df97 100644
--- a/configure.ac
+++ b/configure.ac
@@ -468,6 +468,7 @@
 AST_EXT_LIB_SETUP([OGG], [OGG], [ogg])
 AST_EXT_LIB_SETUP([OPENR2], [MFR2], [openr2])
 AST_EXT_LIB_SETUP([OPUS], [Opus], [opus])
+AST_EXT_LIB_SETUP([OPUSFILE], [Opusfile], [opusfile])
 AST_EXT_LIB_SETUP([OSPTK], [OSP Toolkit], [osptk])
 AST_EXT_LIB_SETUP([OSS], [Open Sound System], [oss])
 AST_EXT_LIB_SETUP([PGSQL], [PostgreSQL], [postgres])
@@ -2293,6 +2294,13 @@
 AST_EXT_LIB_CHECK([OPENR2], [openr2], [openr2_chan_new], [openr2.h])
 
 AST_EXT_LIB_CHECK([OPUS], [opus], [opus_encoder_create], [opus/opus.h])
+# opusfile.h includes <opus_multistream.h> so we need to make sure that
+# either $OPUS_INCLUDE or /usr/include/opus is added to the search path.
+__opus_include=${OPUS_INCLUDE}
+if test -z "$__opus_include" -o x"$__opus_include" = x" " ; then
+	__opus_include=-I/usr/include/opus
+fi
+AST_EXT_LIB_CHECK([OPUSFILE], [opusfile], [op_open_callbacks], [opus/opusfile.h], [], [$__opus_include])
 
 if test "${USE_PWLIB}" != "no"; then
 	if test -n "${PWLIB_DIR}"; then
diff --git a/formats/format_ogg_opus.c b/formats/format_ogg_opus.c
new file mode 100644
index 0000000..f6a4c6c
--- /dev/null
+++ b/formats/format_ogg_opus.c
@@ -0,0 +1,229 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2016, Digium, Inc.
+ *
+ * Mark Michelson <mmichelson at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*** MODULEINFO
+	<depend>opusfile</depend>
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include <opus/opus.h>
+#include <opus/opusfile.h>
+#include "asterisk/mod_format.h"
+#include "asterisk/utils.h"
+#include "asterisk/module.h"
+#include "asterisk/format_cache.h"
+
+/* 120ms of 48KHz audio */
+#define SAMPLES_MAX 5760
+#define BUF_SIZE (2 * SAMPLES_MAX)
+
+struct ogg_opus_desc {
+	OggOpusFile *of;
+};
+
+static int fread_wrapper(void *_stream, unsigned char *_ptr, int _nbytes)
+{
+	FILE *stream = _stream;
+	size_t bytes_read;
+
+	if (!stream || _nbytes < 0) {
+		return -1;
+	}
+
+	bytes_read = fread(_ptr, 1, _nbytes, stream);
+
+	return bytes_read > 0 || feof(stream) ? (int) bytes_read : OP_EREAD;
+}
+
+static int fseek_wrapper(void *_stream, opus_int64 _offset, int _whence)
+{
+	FILE *stream = _stream;
+
+	return fseeko(stream, (off_t) _offset, _whence);
+}
+
+static opus_int64 ftell_wrapper(void *_stream)
+{
+	FILE *stream = _stream;
+
+	return ftello(stream);
+}
+
+static int ogg_opus_open(struct ast_filestream *s)
+{
+	struct ogg_opus_desc *desc = (struct ogg_opus_desc *) s->_private;
+	OpusFileCallbacks cb = {
+		.read = fread_wrapper,
+		.seek = fseek_wrapper,
+		.tell = ftell_wrapper,
+		.close = NULL,
+	};
+
+	memset(desc, 0, sizeof(*desc));
+	desc->of = op_open_callbacks(s->f, &cb, NULL, 0, NULL);
+	if (!desc->of) {
+		return -1;
+	}
+
+	return 0;
+}
+
+static int ogg_opus_rewrite(struct ast_filestream *s, const char *comment)
+{
+	/* XXX Unimplemented. We currently only can read from OGG/Opus streams */
+	ast_log(LOG_ERROR, "Cannot write OGG/Opus streams. Sorry :(\n");
+	return -1;
+}
+
+static int ogg_opus_write(struct ast_filestream *fs, struct ast_frame *f)
+{
+	/* XXX Unimplemented. We currently only can read from OGG/Opus streams */
+	ast_log(LOG_ERROR, "Cannot write OGG/Opus streams. Sorry :(\n");
+	return -1;
+}
+
+static int ogg_opus_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
+{
+	int seek_result = -1;
+	off_t relative_pcm_pos;
+	struct ogg_opus_desc *desc = fs->_private;
+
+	switch (whence) {
+	case SEEK_SET:
+		seek_result = op_pcm_seek(desc->of, sample_offset);
+		break;
+	case SEEK_CUR:
+		if ((relative_pcm_pos = op_pcm_tell(desc->of)) < 0) {
+			seek_result = -1;
+			break;
+		}
+		seek_result = op_pcm_seek(desc->of, relative_pcm_pos + sample_offset);
+		break;
+	case SEEK_END:
+		if ((relative_pcm_pos = op_pcm_total(desc->of, -1)) < 0) {
+			seek_result = -1;
+			break;
+		}
+		seek_result = op_pcm_seek(desc->of, relative_pcm_pos - sample_offset);
+		break;
+	default:
+		ast_log(LOG_WARNING, "Unknown *whence* to seek on OGG/Opus streams!\n");
+		break;
+	}
+
+	/* normalize error value to -1,0 */
+	return (seek_result == 0) ? 0 : -1;
+}
+
+static int ogg_opus_trunc(struct ast_filestream *fs)
+{
+	/* XXX Unimplemented. This is only used when recording, and we don't support that right now. */
+	ast_log(LOG_ERROR, "Truncation is not supported on OGG/Opus streams!\n");
+	return -1;
+}
+
+static off_t ogg_opus_tell(struct ast_filestream *fs)
+{
+	struct ogg_opus_desc *desc = fs->_private;
+	off_t pos;
+
+	pos = (off_t) op_pcm_tell(desc->of);
+	if (pos < 0) {
+		return -1;
+	}
+	return pos;
+}
+
+static struct ast_frame *ogg_opus_read(struct ast_filestream *fs, int *whennext)
+{
+	struct ogg_opus_desc *desc = fs->_private;
+	int hole = 1;
+	int samples_read;
+	opus_int16 *out_buf;
+
+	AST_FRAME_SET_BUFFER(&fs->fr, fs->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
+
+	out_buf = (opus_int16 *) fs->fr.data.ptr;
+
+	while (hole) {
+		samples_read = op_read(
+			desc->of,
+			out_buf,
+			SAMPLES_MAX,
+			NULL);
+
+		if (samples_read != OP_HOLE) {
+			hole = 0;
+		}
+	}
+
+	if (samples_read <= 0) {
+		return NULL;
+	}
+
+	fs->fr.datalen = samples_read * 2;
+	fs->fr.samples = samples_read;
+	*whennext = fs->fr.samples;
+
+	return &fs->fr;
+}
+
+static void ogg_opus_close(struct ast_filestream *fs)
+{
+	struct ogg_opus_desc *desc = fs->_private;
+
+	op_free(desc->of);
+}
+
+static struct ast_format_def opus_f = {
+	.name = "ogg_opus",
+	.exts = "opus",
+	.open = ogg_opus_open,
+	.rewrite = ogg_opus_rewrite,
+	.write = ogg_opus_write,
+	.seek = ogg_opus_seek,
+	.trunc = ogg_opus_trunc,
+	.tell = ogg_opus_tell,
+	.read = ogg_opus_read,
+	.close = ogg_opus_close,
+	.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
+	.desc_size = sizeof(struct ogg_opus_desc),
+};
+
+static int load_module(void)
+{
+	opus_f.format = ast_format_slin48;
+	if (ast_format_def_register(&opus_f)) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+	return ast_format_def_unregister(opus_f.name);
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "OGG/Opus audio",
+	.support_level = AST_MODULE_SUPPORT_CORE,
+	.load = load_module,
+	.unload = unload_module,
+	.load_pri = AST_MODPRI_APP_DEPEND
+);
diff --git a/include/asterisk/autoconfig.h.in b/include/asterisk/autoconfig.h.in
index e16b668..d34a38a 100644
--- a/include/asterisk/autoconfig.h.in
+++ b/include/asterisk/autoconfig.h.in
@@ -559,6 +559,9 @@
 /* Define to 1 if you have the Opus library. */
 #undef HAVE_OPUS
 
+/* Define to 1 if you have the Opusfile library. */
+#undef HAVE_OPUSFILE
+
 /* Define this to indicate the ${OSPTK_DESCRIP} library */
 #undef HAVE_OSPTK
 
diff --git a/makeopts.in b/makeopts.in
index b2b394b..0b268b8 100644
--- a/makeopts.in
+++ b/makeopts.in
@@ -223,6 +223,9 @@
 OPUS_INCLUDE=@OPUS_INCLUDE@
 OPUS_LIB=@OPUS_LIB@
 
+OPUSFILE_INCLUDE=@OPUSFILE_INCLUDE@
+OPUSFILE_LIB=@OPUSFILE_LIB@
+
 OSPTK_INCLUDE=@OSPTK_INCLUDE@
 OSPTK_LIB=@OSPTK_LIB@
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I8815d23345108d8ca7c0bd640f6a1ce6b4f56955
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>



More information about the asterisk-commits mailing list