summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLiong Sauw Ming <ming@teluu.com>2016-02-04 06:11:58 +0000
committerLiong Sauw Ming <ming@teluu.com>2016-02-04 06:11:58 +0000
commit56f6e91963bbd507c300e4f2a806a8db2935566d (patch)
treee0ab33100a9c31f1c7872ddc22787011a04b64c3
parente6a13543eb90a73b095c4aa84038753d31ff2b39 (diff)
Fixed #1904: Support for Opus codec
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@5239 74dad513-b988-da41-8d7b-12977e46ad98
-rwxr-xr-xaconfigure110
-rw-r--r--aconfigure.ac48
-rw-r--r--pjmedia/build/Makefile2
-rw-r--r--pjmedia/include/pjmedia-codec.h1
-rw-r--r--pjmedia/include/pjmedia-codec/config.h50
-rw-r--r--pjmedia/include/pjmedia-codec/config_auto.h.in5
-rw-r--r--pjmedia/include/pjmedia-codec/opus.h155
-rw-r--r--pjmedia/include/pjmedia-codec/types.h1
-rw-r--r--pjmedia/include/pjmedia/config.h1
-rw-r--r--pjmedia/src/pjmedia-codec/audio_codecs.c7
-rw-r--r--pjmedia/src/pjmedia-codec/opus.c1070
-rw-r--r--pjmedia/src/pjmedia/stream.c15
-rw-r--r--pjmedia/src/pjmedia/stream_info.c49
13 files changed, 1513 insertions, 1 deletions
diff --git a/aconfigure b/aconfigure
index e3b7c4f1..e174bbf0 100755
--- a/aconfigure
+++ b/aconfigure
@@ -623,6 +623,9 @@ LIBOBJS
ac_main_obj
ac_host
ac_linux_poll
+opus_present
+opus_h_present
+ac_no_opus
silk_present
silk_h_present
ac_no_silk
@@ -816,6 +819,8 @@ with_opencore_amrwbenc
enable_opencore_amr
with_silk
enable_silk
+with_opus
+enable_opus
'
ac_precious_vars='build_alias
host_alias
@@ -1482,6 +1487,9 @@ Optional Features:
--disable-silk Exclude SILK support from the build (default:
autodetect)
+ --disable-opus Exclude OPUS support from the build (default:
+ autodetect)
+
Optional Packages:
--with-PACKAGE[=ARG] use PACKAGE [ARG=yes]
@@ -1523,6 +1531,7 @@ Optional Packages:
--with-opencore-amrwbenc=DIR
Specify alternate libvo-amrwbenc prefix
--with-silk=DIR Specify alternate SILK prefix
+ --with-opus=DIR Specify alternate OPUS prefix
Some influential environment variables:
CC C compiler command
@@ -8226,6 +8235,107 @@ $as_echo "SILK library found, SILK support enabled" >&6; }
fi
+if test "x$ac_cross_compile" != "x" -a "x$with_opus" = "xno"; then
+ enable_opus=no
+fi
+
+
+# Check whether --with-opus was given.
+if test "${with_opus+set}" = set; then :
+ withval=$with_opus;
+else
+ with_opus=no
+
+fi
+
+
+
+# Check whether --enable-opus was given.
+if test "${enable_opus+set}" = set; then :
+ enableval=$enable_opus;
+ if test "$enable_opus" = "no"; then
+ ac_no_opus=1
+ $as_echo "#define PJMEDIA_HAS_OPUS_CODEC 0" >>confdefs.h
+
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: Checking if OPUS support is disabled... yes" >&5
+$as_echo "Checking if OPUS support is disabled... yes" >&6; }
+ fi
+
+else
+
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: checking for OPUS installations.." >&5
+$as_echo "checking for OPUS installations.." >&6; }
+ if test "x$with_opus" != "xno" -a "x$with_opus" != "x"; then
+ CFLAGS="$CFLAGS -I$with_opus/include"
+ CPPFLAGS="$CPPFLAGS -I$with_opus/include"
+ LDFLAGS="$LDFLAGS -L$with_opus/lib"
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: Using OPUS prefix... $with_opus" >&5
+$as_echo "Using OPUS prefix... $with_opus" >&6; }
+ fi
+
+
+ ac_fn_c_check_header_mongrel "$LINENO" "opus/opus.h" "ac_cv_header_opus_opus_h" "$ac_includes_default"
+if test "x$ac_cv_header_opus_opus_h" = xyes; then :
+ opus_h_present=1
+fi
+
+
+ { $as_echo "$as_me:${as_lineno-$LINENO}: checking for opus_repacketizer_get_size in -lopus" >&5
+$as_echo_n "checking for opus_repacketizer_get_size in -lopus... " >&6; }
+if ${ac_cv_lib_opus_opus_repacketizer_get_size+:} false; then :
+ $as_echo_n "(cached) " >&6
+else
+ ac_check_lib_save_LIBS=$LIBS
+LIBS="-lopus $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 opus_repacketizer_get_size ();
+int
+main ()
+{
+return opus_repacketizer_get_size ();
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+ ac_cv_lib_opus_opus_repacketizer_get_size=yes
+else
+ ac_cv_lib_opus_opus_repacketizer_get_size=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+ conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_opus_opus_repacketizer_get_size" >&5
+$as_echo "$ac_cv_lib_opus_opus_repacketizer_get_size" >&6; }
+if test "x$ac_cv_lib_opus_opus_repacketizer_get_size" = xyes; then :
+ opus_present=1 && LIBS="$LIBS -lopus"
+fi
+
+ if test "x$opus_h_present" = "x1" -a "x$opus_present" = "x1"; then
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: OPUS library found, OPUS support enabled" >&5
+$as_echo "OPUS library found, OPUS support enabled" >&6; }
+ $as_echo "#define PJMEDIA_HAS_OPUS_CODEC 1" >>confdefs.h
+
+ else
+ ac_no_opus=1
+ { $as_echo "$as_me:${as_lineno-$LINENO}: result: OPUS library not found, OPUS support disabled" >&5
+$as_echo "OPUS library not found, OPUS support disabled" >&6; }
+ $as_echo "#define PJMEDIA_HAS_OPUS_CODEC 0" >>confdefs.h
+
+ fi
+
+fi
+
+
diff --git a/aconfigure.ac b/aconfigure.ac
index d0e88a72..2f71abbe 100644
--- a/aconfigure.ac
+++ b/aconfigure.ac
@@ -1761,6 +1761,54 @@ AC_ARG_ENABLE(silk,
fi
])
+dnl # Do not use default OPUS installation if we are cross-compiling
+if test "x$ac_cross_compile" != "x" -a "x$with_opus" = "xno"; then
+ enable_opus=no
+fi
+
+dnl # OPUS prefix
+AC_ARG_WITH(opus,
+ AC_HELP_STRING([--with-opus=DIR],
+ [Specify alternate OPUS prefix]),
+ [],
+ [with_opus=no]
+ )
+
+dnl # Include OPUS support
+AC_SUBST(ac_no_opus)
+AC_ARG_ENABLE(opus,
+ AC_HELP_STRING([--disable-opus],
+ [Exclude OPUS support from the build (default: autodetect)])
+ ,
+ [
+ if test "$enable_opus" = "no"; then
+ [ac_no_opus=1]
+ AC_DEFINE(PJMEDIA_HAS_OPUS_CODEC,0)
+ AC_MSG_RESULT([Checking if OPUS support is disabled... yes])
+ fi
+ ],
+ [
+ AC_MSG_RESULT([checking for OPUS installations..])
+ if test "x$with_opus" != "xno" -a "x$with_opus" != "x"; then
+ CFLAGS="$CFLAGS -I$with_opus/include"
+ CPPFLAGS="$CPPFLAGS -I$with_opus/include"
+ LDFLAGS="$LDFLAGS -L$with_opus/lib"
+ AC_MSG_RESULT([Using OPUS prefix... $with_opus])
+ fi
+ AC_SUBST(opus_h_present)
+ AC_SUBST(opus_present)
+ AC_CHECK_HEADER(opus/opus.h,[opus_h_present=1])
+ AC_CHECK_LIB(opus,opus_repacketizer_get_size,[opus_present=1 && LIBS="$LIBS -lopus"])
+ if test "x$opus_h_present" = "x1" -a "x$opus_present" = "x1"; then
+ AC_MSG_RESULT([OPUS library found, OPUS support enabled])
+ AC_DEFINE(PJMEDIA_HAS_OPUS_CODEC,1)
+ else
+ [ac_no_opus=1]
+ AC_MSG_RESULT([OPUS library not found, OPUS support disabled])
+ AC_DEFINE(PJMEDIA_HAS_OPUS_CODEC,0)
+ fi
+ ])
+
dnl ##########################################
dnl #
diff --git a/pjmedia/build/Makefile b/pjmedia/build/Makefile
index 26f09538..2a4af87d 100644
--- a/pjmedia/build/Makefile
+++ b/pjmedia/build/Makefile
@@ -138,7 +138,7 @@ export PJMEDIA_CODEC_SRCDIR = ../src/pjmedia-codec
export PJMEDIA_CODEC_OBJS += audio_codecs.o ffmpeg_vid_codecs.o openh264.o \
h263_packetizer.o h264_packetizer.o \
$(OS_OBJS) $(M_OBJS) $(CC_OBJS) $(HOST_OBJS) \
- ipp_codecs.o silk.o $(CODEC_OBJS) \
+ ipp_codecs.o silk.o opus.o $(CODEC_OBJS) \
g7221_sdp_match.o amr_sdp_match.o
export PJMEDIA_CODEC_CFLAGS += $(_CFLAGS) $(GSM_CFLAGS) $(SPEEX_CFLAGS) \
$(ILBC_CFLAGS) $(IPP_CFLAGS) $(G7221_CFLAGS)
diff --git a/pjmedia/include/pjmedia-codec.h b/pjmedia/include/pjmedia-codec.h
index a35c8540..082c5db7 100644
--- a/pjmedia/include/pjmedia-codec.h
+++ b/pjmedia/include/pjmedia-codec.h
@@ -38,6 +38,7 @@
#include <pjmedia-codec/openh264.h>
#include <pjmedia-codec/passthrough.h>
#include <pjmedia-codec/silk.h>
+#include <pjmedia-codec/opus.h>
#endif /* __PJMEDIA_CODEC_PJMEDIA_CODEC_H__ */
diff --git a/pjmedia/include/pjmedia-codec/config.h b/pjmedia/include/pjmedia-codec/config.h
index 875c821a..9a69099f 100644
--- a/pjmedia/include/pjmedia-codec/config.h
+++ b/pjmedia/include/pjmedia-codec/config.h
@@ -420,6 +420,56 @@
/**
+ * Enable OPUS codec.
+ *
+ * Default: 0
+ */
+#ifndef PJMEDIA_HAS_OPUS_CODEC
+# define PJMEDIA_HAS_OPUS_CODEC 0
+#endif
+
+/**
+ * OPUS codec sample rate.
+ *
+ * Default: 48000
+ */
+#ifndef PJMEDIA_CODEC_OPUS_DEFAULT_SAMPLE_RATE
+# define PJMEDIA_CODEC_OPUS_DEFAULT_SAMPLE_RATE 48000
+#endif
+
+/**
+ * OPUS codec default maximum average bit rate.
+ *
+ * Default: 0 (leave it to default value specified by Opus, which will
+ * take into account factors such as media content (speech/music), sample
+ * rate, channel count, etc).
+ */
+#ifndef PJMEDIA_CODEC_OPUS_DEFAULT_BIT_RATE
+# define PJMEDIA_CODEC_OPUS_DEFAULT_BIT_RATE 0
+#endif
+
+
+/**
+ * OPUS default encoding complexity, which is an integer from
+ * 0 to 10, where 0 is the lowest complexity and 10 is the highest.
+ *
+ * Default: 5
+ */
+#ifndef PJMEDIA_CODEC_OPUS_DEFAULT_COMPLEXITY
+# define PJMEDIA_CODEC_OPUS_DEFAULT_COMPLEXITY 5
+#endif
+
+
+/**
+ * OPUS default CBR (constant bit rate) setting
+ *
+ * Default: PJ_FALSE (which means Opus will use VBR (variable bit rate))
+ */
+#ifndef PJMEDIA_CODEC_OPUS_DEFAULT_CBR
+# define PJMEDIA_CODEC_OPUS_DEFAULT_CBR PJ_FALSE
+#endif
+
+/**
* Specify if FFMPEG codecs are available.
*
* Default: PJMEDIA_HAS_LIBAVCODEC
diff --git a/pjmedia/include/pjmedia-codec/config_auto.h.in b/pjmedia/include/pjmedia-codec/config_auto.h.in
index 746c1fc7..909d4d74 100644
--- a/pjmedia/include/pjmedia-codec/config_auto.h.in
+++ b/pjmedia/include/pjmedia-codec/config_auto.h.in
@@ -84,6 +84,11 @@
#undef PJMEDIA_HAS_SILK_CODEC
#endif
+/* OPUS codec */
+#ifndef PJMEDIA_HAS_OPUS_CODEC
+#undef PJMEDIA_HAS_OPUS_CODEC
+#endif
+
#endif /* __PJMEDIA_CODEC_CONFIG_AUTO_H_ */
diff --git a/pjmedia/include/pjmedia-codec/opus.h b/pjmedia/include/pjmedia-codec/opus.h
new file mode 100644
index 00000000..6f1e8ef5
--- /dev/null
+++ b/pjmedia/include/pjmedia-codec/opus.h
@@ -0,0 +1,155 @@
+/* $Id$ */
+/*
+ * Copyright (C) 2015-2016 Teluu Inc. (http://www.teluu.com)
+ * Copyright (C) 2012-2015 Zaark Technology AB
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+/* This file is the header of Opus codec wrapper and was contributed by
+ * Zaark Technology AB
+ */
+
+#ifndef __PJMEDIA_CODEC_OPUS_H__
+#define __PJMEDIA_CODEC_OPUS_H__
+
+/**
+ * @file opus.h
+ * @brief Opus codec.
+ */
+
+#include <pjmedia-codec/types.h>
+
+PJ_BEGIN_DECL
+
+/**
+ * @defgroup PJMED_OPUS Opus Codec Family
+ * @ingroup PJMEDIA_CODEC_CODECS
+ * @brief Opus codec wrapper
+ * @{
+ *
+ * This section describes functions to initialize and register Opus codec
+ * factory to the codec manager. After the codec factory has been registered,
+ * application can use @ref PJMEDIA_CODEC API to manipulate the codec.
+ *
+ * Opus codec uses multiple bit rates, and supports fullband (48 kHz
+ * sampling rate), super wideband (24 kHz sampling rate), wideband (16 kHz
+ * sampling rate), medium band (12kHz sampling rate), and narrowband
+ * (8 kHz sampling rate).
+ *
+ *
+ * \section codec_setting Codec Settings
+ *
+ * General codec settings for this codec such as VAD and PLC can be
+ * manipulated through the <tt>setting</tt> field in #pjmedia_codec_param
+ * (see the documentation of #pjmedia_codec_param for more info).
+ *
+ * For Opus codec specific settings, such as sample rate,
+ * channel count, bit rate, complexity, and CBR, can be configured
+ * in #pjmedia_codec_opus_config.
+ * The default setting of sample rate is specified in
+ * #PJMEDIA_CODEC_OPUS_DEFAULT_SAMPLE_RATE. The default setting of
+ * bitrate is specified in #PJMEDIA_CODEC_OPUS_DEFAULT_BIT_RATE.
+ * And the default setting of complexity is specified in
+ * #PJMEDIA_CODEC_OPUS_DEFAULT_COMPLEXITY.
+ *
+ * After modifying any of these settings, application needs to call
+ * #pjmedia_codec_opus_set_default_param(), which will generate the
+ * appropriate decoding fmtp attributes.
+ *
+ * Here is an example of modifying the codec settings:
+ \code
+ pjmedia_codec_param param;
+ pjmedia_codec_opus_config opus_cfg;
+
+ pjmedia_codec_mgr_get_default_param(.., &param);
+ pjmedia_codec_opus_get_config(&opus_cfg);
+ ...
+ // Set VAD
+ param.setting.vad = 1;
+ // Set PLC
+ param.setting.vad = 1;
+ // Set sample rate
+ opus_cfg.sample_rate = 16000;
+ // Set channel count
+ opus_cfg.channel_cnt = 2;
+ // Set bit rate
+ opus_cfg.bit_rate = 20000;
+ ...
+ pjmedia_codec_opus_set_default_param(&opus_cfg, &param);
+ \endcode
+ *
+ */
+
+/**
+ * Opus codec configuration.
+ */
+typedef struct pjmedia_codec_opus_config
+{
+ unsigned sample_rate; /**< Sample rate in Hz. */
+ unsigned channel_cnt; /**< Number of channels. */
+ unsigned bit_rate; /**< Encoder bit rate in bps. */
+ unsigned packet_loss; /**< Encoder's expected packet loss pct. */
+ unsigned complexity; /**< Encoder complexity, 0-10(10 is highest)*/
+ pj_bool_t cbr; /**< Constant bit rate? */
+} pjmedia_codec_opus_config;
+
+
+/**
+ * Initialize and register Opus codec factory to pjmedia endpoint.
+ *
+ * @param endpt The pjmedia endpoint.
+ *
+ * @return PJ_SUCCESS on success.
+ */
+PJ_DECL(pj_status_t) pjmedia_codec_opus_init( pjmedia_endpt *endpt );
+
+/**
+ * Unregister Opus codec factory from pjmedia endpoint and deinitialize
+ * the Opus codec library.
+ *
+ * @return PJ_SUCCESS on success.
+ */
+PJ_DECL(pj_status_t) pjmedia_codec_opus_deinit( void );
+
+/**
+ * Get the default Opus configuration.
+ *
+ * @param cfg Opus codec configuration.
+ *
+ * @return PJ_SUCCESS on success.
+ */
+PJ_DECL(pj_status_t)
+pjmedia_codec_opus_get_config( pjmedia_codec_opus_config *cfg );
+
+/**
+ * Set the default Opus configuration and set the default Opus codec param.
+ * Note that the function will call #pjmedia_codec_mgr_set_default_param().
+ *
+ * @param cfg Opus codec configuration.
+ * @param param The new default Opus codec parameter.
+ *
+ * @return PJ_SUCCESS on success.
+ */
+PJ_DECL(pj_status_t)
+pjmedia_codec_opus_set_default_param(const pjmedia_codec_opus_config *cfg,
+ pjmedia_codec_param *param );
+
+PJ_END_DECL
+
+/**
+ * @}
+ */
+
+#endif /* __PJMEDIA_CODEC_OPUS_H__ */
diff --git a/pjmedia/include/pjmedia-codec/types.h b/pjmedia/include/pjmedia-codec/types.h
index d0549f5e..98368eff 100644
--- a/pjmedia/include/pjmedia-codec/types.h
+++ b/pjmedia/include/pjmedia-codec/types.h
@@ -83,6 +83,7 @@ enum pjmedia_audio_pt
PJMEDIA_RTP_PT_G7221C_48, /**< G722.1 Annex C (48Kbps)*/
PJMEDIA_RTP_PT_G7221_RSV1, /**< G722.1 reserve */
PJMEDIA_RTP_PT_G7221_RSV2, /**< G722.1 reserve */
+ PJMEDIA_RTP_PT_OPUS, /**< OPUS */
PJMEDIA_RTP_PT_L16_8KHZ_MONO, /**< L16 @ 8KHz, mono */
PJMEDIA_RTP_PT_L16_8KHZ_STEREO, /**< L16 @ 8KHz, stereo */
//PJMEDIA_RTP_PT_L16_11KHZ_MONO, /**< L16 @ 11KHz, mono */
diff --git a/pjmedia/include/pjmedia/config.h b/pjmedia/include/pjmedia/config.h
index f2f8e829..8d739d2a 100644
--- a/pjmedia/include/pjmedia/config.h
+++ b/pjmedia/include/pjmedia/config.h
@@ -972,6 +972,7 @@
* See:
* - G.722 : RFC 3551 4.5.2
* - MPEG audio : RFC 3551 4.5.13 & RFC 3119
+ * - OPUS : RFC 7587
*
* Also when this feature is enabled, some handling will be performed
* to deal with clock rate incompatibilities of some phones.
diff --git a/pjmedia/src/pjmedia-codec/audio_codecs.c b/pjmedia/src/pjmedia-codec/audio_codecs.c
index d8eb555f..214d70c0 100644
--- a/pjmedia/src/pjmedia-codec/audio_codecs.c
+++ b/pjmedia/src/pjmedia-codec/audio_codecs.c
@@ -121,6 +121,13 @@ pjmedia_codec_register_audio_codecs(pjmedia_endpt *endpt,
return status;
#endif
+#if PJMEDIA_HAS_OPUS_CODEC
+ /* Register OPUS */
+ status = pjmedia_codec_opus_init(endpt);
+ if (status != PJ_SUCCESS)
+ return status;
+#endif
+
return PJ_SUCCESS;
}
diff --git a/pjmedia/src/pjmedia-codec/opus.c b/pjmedia/src/pjmedia-codec/opus.c
new file mode 100644
index 00000000..6dd4b085
--- /dev/null
+++ b/pjmedia/src/pjmedia-codec/opus.c
@@ -0,0 +1,1070 @@
+/* $Id$ */
+/*
+ * Copyright (C) 2015-2016 Teluu Inc. (http://www.teluu.com)
+ * Copyright (C) 2012-2015 Zaark Technology AB
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+/* This file is the implementation of Opus codec wrapper and was contributed by
+ * Zaark Technology AB
+ */
+
+#include <pjmedia-codec/opus.h>
+#include <pjmedia/errno.h>
+#include <pjmedia/endpoint.h>
+#include <pj/log.h>
+
+#if defined(PJMEDIA_HAS_OPUS_CODEC) && (PJMEDIA_HAS_OPUS_CODEC!=0)
+
+#include <opus/opus.h>
+
+#define THIS_FILE "opus.c"
+
+/* Default packet loss concealment setting. */
+#define OPUS_DEFAULT_PLC 1
+/* Default Voice Activity Detector setting. */
+#define OPUS_DEFAULT_VAD 0
+
+/* Maximum size of an encoded packet.
+ * If the the actual size is bigger, the encode/parse will fail.
+ */
+#define MAX_ENCODED_PACKET_SIZE 1280
+
+/* Tracing */
+#if 0
+# define TRACE_(expr) PJ_LOG(4,expr)
+#else
+# define TRACE_(expr)
+#endif
+
+
+/* Prototypes for Opus factory */
+static pj_status_t factory_test_alloc( pjmedia_codec_factory *factory,
+ const pjmedia_codec_info *ci );
+static pj_status_t factory_default_attr( pjmedia_codec_factory *factory,
+ const pjmedia_codec_info *ci,
+ pjmedia_codec_param *attr );
+static pj_status_t factory_enum_codecs( pjmedia_codec_factory *factory,
+ unsigned *count,
+ pjmedia_codec_info codecs[]);
+static pj_status_t factory_alloc_codec( pjmedia_codec_factory *factory,
+ const pjmedia_codec_info *ci,
+ pjmedia_codec **p_codec);
+static pj_status_t factory_dealloc_codec( pjmedia_codec_factory *factory,
+ pjmedia_codec *codec );
+
+
+/* Prototypes for Opus implementation. */
+static pj_status_t codec_init( pjmedia_codec *codec,
+ pj_pool_t *pool );
+static pj_status_t codec_open( pjmedia_codec *codec,
+ pjmedia_codec_param *attr );
+static pj_status_t codec_close( pjmedia_codec *codec );
+static pj_status_t codec_modify( pjmedia_codec *codec,
+ const pjmedia_codec_param *attr );
+static pj_status_t codec_parse( pjmedia_codec *codec,
+ void *pkt,
+ pj_size_t pkt_size,
+ const pj_timestamp *ts,
+ unsigned *frame_cnt,
+ pjmedia_frame frames[]);
+static pj_status_t codec_encode( pjmedia_codec *codec,
+ const struct pjmedia_frame *input,
+ unsigned output_buf_len,
+ struct pjmedia_frame *output);
+static pj_status_t codec_decode( pjmedia_codec *codec,
+ const struct pjmedia_frame *input,
+ unsigned output_buf_len,
+ struct pjmedia_frame *output);
+static pj_status_t codec_recover( pjmedia_codec *codec,
+ unsigned output_buf_len,
+ struct pjmedia_frame *output);
+
+/* Definition for Opus operations. */
+static pjmedia_codec_op opus_op =
+{
+ &codec_init,
+ &codec_open,
+ &codec_close,
+ &codec_modify,
+ &codec_parse,
+ &codec_encode,
+ &codec_decode,
+ &codec_recover
+};
+
+/* Definition for Opus factory operations. */
+static pjmedia_codec_factory_op opus_factory_op =
+{
+ &factory_test_alloc,
+ &factory_default_attr,
+ &factory_enum_codecs,
+ &factory_alloc_codec,
+ &factory_dealloc_codec,
+ &pjmedia_codec_opus_deinit
+};
+
+
+/* Opus factory */
+struct opus_codec_factory
+{
+ pjmedia_codec_factory base;
+ pjmedia_endpt *endpt;
+ pj_pool_t *pool;
+};
+
+/* Opus codec private data. */
+struct opus_data
+{
+ pj_pool_t *pool;
+ pj_mutex_t *mutex;
+ OpusEncoder *enc;
+ OpusDecoder *dec;
+ OpusRepacketizer *enc_packer;
+ OpusRepacketizer *dec_packer;
+ pjmedia_codec_opus_config cfg;
+ unsigned ptime;
+ pjmedia_frame dec_frame[2];
+ int dec_frame_index;
+};
+
+/* Codec factory instance */
+static struct opus_codec_factory opus_codec_factory;
+
+/* Opus default configuration */
+static pjmedia_codec_opus_config opus_cfg =
+{
+ PJMEDIA_CODEC_OPUS_DEFAULT_SAMPLE_RATE, /* Sample rate */
+ 1, /* Channel count */
+ PJMEDIA_CODEC_OPUS_DEFAULT_BIT_RATE, /* Bit rate */
+ 5, /* Expected packet loss */
+ PJMEDIA_CODEC_OPUS_DEFAULT_COMPLEXITY, /* Complexity */
+ PJMEDIA_CODEC_OPUS_DEFAULT_CBR, /* Constant bit rate */
+};
+
+
+static int get_opus_bw_constant (unsigned sample_rate)
+{
+ if (sample_rate <= 8000)
+ return OPUS_BANDWIDTH_NARROWBAND;
+ else if (sample_rate <= 12000)
+ return OPUS_BANDWIDTH_MEDIUMBAND;
+ else if (sample_rate <= 16000)
+ return OPUS_BANDWIDTH_WIDEBAND;
+ else if (sample_rate <= 24000)
+ return OPUS_BANDWIDTH_SUPERWIDEBAND;
+ else
+ return OPUS_BANDWIDTH_FULLBAND;
+}
+
+
+/*
+ * Initialize and register Opus codec factory to pjmedia endpoint.
+ */
+PJ_DEF(pj_status_t) pjmedia_codec_opus_init( pjmedia_endpt *endpt )
+{
+ pj_status_t status;
+ pjmedia_codec_mgr *codec_mgr;
+
+ PJ_ASSERT_RETURN(endpt, PJ_EINVAL);
+
+ if (opus_codec_factory.pool != NULL)
+ return PJ_SUCCESS;
+
+ /* Create the Opus codec factory */
+ opus_codec_factory.base.op = &opus_factory_op;
+ opus_codec_factory.base.factory_data = &opus_codec_factory;
+ opus_codec_factory.endpt = endpt;
+
+ opus_codec_factory.pool = pjmedia_endpt_create_pool(endpt, "opus-factory",
+ 1024, 1024);
+ if (!opus_codec_factory.pool) {
+ PJ_LOG(2, (THIS_FILE, "Unable to create memory pool for Opus codec"));
+ return PJ_ENOMEM;
+ }
+
+ /* Get the codec manager */
+ codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
+ if (!codec_mgr) {
+ PJ_LOG(2, (THIS_FILE, "Unable to get the codec manager"));
+ status = PJ_EINVALIDOP;
+ goto on_codec_factory_error;
+ }
+
+ /* Register the codec factory */
+ status = pjmedia_codec_mgr_register_factory (codec_mgr,
+ &opus_codec_factory.base);
+ if (status != PJ_SUCCESS) {
+ PJ_LOG(2, (THIS_FILE, "Unable to register the codec factory"));
+ goto on_codec_factory_error;
+ }
+
+ return PJ_SUCCESS;
+
+on_codec_factory_error:
+ pj_pool_release(opus_codec_factory.pool);
+ opus_codec_factory.pool = NULL;
+ return status;
+}
+
+
+/*
+ * Unregister Opus codec factory from pjmedia endpoint and
+ * deinitialize the codec.
+ */
+PJ_DEF(pj_status_t) pjmedia_codec_opus_deinit( void )
+{
+ pj_status_t status;
+ pjmedia_codec_mgr *codec_mgr;
+
+ if (opus_codec_factory.pool == NULL)
+ return PJ_SUCCESS;
+
+ /* Get the codec manager */
+ codec_mgr = pjmedia_endpt_get_codec_mgr(opus_codec_factory.endpt);
+ if (!codec_mgr) {
+ PJ_LOG(2, (THIS_FILE, "Unable to get the codec manager"));
+ pj_pool_release(opus_codec_factory.pool);
+ opus_codec_factory.pool = NULL;
+ return PJ_EINVALIDOP;
+ }
+
+ /* Unregister the codec factory */
+ status = pjmedia_codec_mgr_unregister_factory(codec_mgr,
+ &opus_codec_factory.base);
+ if (status != PJ_SUCCESS)
+ PJ_LOG(2, (THIS_FILE, "Unable to unregister the codec factory"));
+
+ /* Release the memory pool */
+ pj_pool_release(opus_codec_factory.pool);
+ opus_codec_factory.pool = NULL;
+
+ return status;
+}
+
+
+/**
+ * Get the opus configuration for a specific sample rate.
+ */
+PJ_DEF(pj_status_t)
+pjmedia_codec_opus_get_config( pjmedia_codec_opus_config *cfg )
+{
+ PJ_ASSERT_RETURN(cfg, PJ_EINVAL);
+
+ pj_memcpy(cfg, &opus_cfg, sizeof(pjmedia_codec_opus_config));
+ return PJ_SUCCESS;
+}
+
+
+static pj_str_t STR_MAX_PLAYBACK = {"maxplaybackrate", 15};
+static pj_str_t STR_MAX_CAPTURE = {"sprop-maxcapturerate", 20};
+static pj_str_t STR_STEREO = {"stereo", 6};
+static pj_str_t STR_SPROP_STEREO = {"sprop-stereo", 12};
+static pj_str_t STR_MAX_BIT_RATE = {"maxaveragebitrate", 17};
+static pj_str_t STR_INBAND_FEC = {"useinbandfec", 12};
+static pj_str_t STR_DTX = {"usedtx", 6};
+static pj_str_t STR_CBR = {"cbr", 3};
+
+static int find_fmtp(pjmedia_codec_fmtp *fmtp, pj_str_t *name, pj_bool_t add)
+{
+ int i;
+ for (i = 0; i < fmtp->cnt; i++) {
+ if (pj_stricmp(&fmtp->param[i].name, name) == 0)
+ return i;
+ }
+
+ if (add && (i < PJMEDIA_CODEC_MAX_FMTP_CNT)) {
+ fmtp->param[i].name = *name;
+ fmtp->cnt++;
+ return i;
+ } else
+ return -1;
+}
+
+static pj_status_t generate_fmtp(pjmedia_codec_param *attr)
+{
+ int idx;
+ static char bitrate_str[12];
+ static char clockrate_str[12];
+
+ if (attr->info.clock_rate != 48000) {
+ pj_ansi_snprintf(clockrate_str, sizeof(clockrate_str), "%u",
+ attr->info.clock_rate);
+
+ idx = find_fmtp(&attr->setting.dec_fmtp, &STR_MAX_PLAYBACK, PJ_TRUE);
+ if (idx >= 0)
+ attr->setting.dec_fmtp.param[idx].val = pj_str(clockrate_str);
+
+ idx = find_fmtp(&attr->setting.dec_fmtp, &STR_MAX_CAPTURE, PJ_TRUE);
+ if (idx >= 0)
+ attr->setting.dec_fmtp.param[idx].val = pj_str(clockrate_str);
+ }
+
+ /* Check if we need to set parameter 'maxaveragebitrate' */
+ if (opus_cfg.bit_rate > 0) {
+ idx = find_fmtp(&attr->setting.dec_fmtp, &STR_MAX_BIT_RATE, PJ_TRUE);
+ if (idx >= 0) {
+ pj_ansi_snprintf(bitrate_str, sizeof(bitrate_str), "%u",
+ attr->info.avg_bps);
+ attr->setting.dec_fmtp.param[idx].val = pj_str(bitrate_str);
+ }
+ }
+
+ if (attr->info.channel_cnt > 1) {
+ idx = find_fmtp(&attr->setting.dec_fmtp, &STR_STEREO, PJ_TRUE);
+ if (idx >= 0)
+ attr->setting.dec_fmtp.param[idx].val = pj_str("1");
+
+ idx = find_fmtp(&attr->setting.dec_fmtp, &STR_SPROP_STEREO, PJ_TRUE);
+ if (idx >= 0)
+ attr->setting.dec_fmtp.param[idx].val = pj_str("1");
+ }
+
+ if (opus_cfg.cbr) {
+ idx = find_fmtp(&attr->setting.dec_fmtp, &STR_CBR, PJ_TRUE);
+ if (idx >= 0)
+ attr->setting.dec_fmtp.param[idx].val = pj_str("1");
+ }
+
+ if (attr->setting.plc) {
+ idx = find_fmtp(&attr->setting.dec_fmtp, &STR_INBAND_FEC, PJ_TRUE);
+ if (idx >= 0)
+ attr->setting.dec_fmtp.param[idx].val = pj_str("1");
+ }
+
+ if (attr->setting.vad) {
+ idx = find_fmtp(&attr->setting.dec_fmtp, &STR_DTX, PJ_TRUE);
+ if (idx >= 0)
+ attr->setting.dec_fmtp.param[idx].val = pj_str("1");
+ }
+
+ return PJ_SUCCESS;
+}
+
+/**
+ * Set the opus configuration and default param.
+ */
+PJ_DEF(pj_status_t)
+pjmedia_codec_opus_set_default_param(const pjmedia_codec_opus_config *cfg,
+ pjmedia_codec_param *param )
+{
+ const pj_str_t opus_str = {"opus", 4};
+ const pjmedia_codec_info *info[1];
+ pjmedia_codec_mgr *codec_mgr;
+ unsigned count = 1;
+ pj_status_t status;
+
+ TRACE_((THIS_FILE, "%s:%d: - TRACE", __FUNCTION__, __LINE__));
+ PJ_ASSERT_RETURN(cfg, PJ_EINVAL);
+
+ codec_mgr = pjmedia_endpt_get_codec_mgr(opus_codec_factory.endpt);
+
+ status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, &opus_str,
+ &count, info, NULL);
+ if (status != PJ_SUCCESS)
+ return status;
+
+ /* Set sample rate */
+ if (cfg->sample_rate != 8000 && cfg->sample_rate != 12000 &&
+ cfg->sample_rate != 16000 && cfg->sample_rate != 24000 &&
+ cfg->sample_rate != 48000)
+ {
+ return PJ_EINVAL;
+ }
+ param->info.clock_rate = opus_cfg.sample_rate = cfg->sample_rate;
+
+ /* Set channel count */
+ if (cfg->channel_cnt != 1 && cfg->channel_cnt != 2)
+ return PJ_EINVAL;
+ param->info.channel_cnt = opus_cfg.channel_cnt = cfg->channel_cnt;
+
+ /* Set bit_rate */
+ if (cfg->bit_rate < 6000 || cfg->bit_rate > 510000) {
+ return PJ_EINVAL;
+ }
+ opus_cfg.bit_rate = cfg->bit_rate;
+
+ /* Set expected packet loss */
+ if (cfg->packet_loss >= 100)
+ return PJ_EINVAL;
+ opus_cfg.packet_loss = cfg->packet_loss;
+
+ /* Set complexity */
+ if (cfg->complexity > 10)
+ return PJ_EINVAL;
+ opus_cfg.complexity = cfg->complexity;
+
+ opus_cfg.cbr = cfg->cbr;
+
+ generate_fmtp(param);
+
+ status = pjmedia_codec_mgr_set_default_param(codec_mgr, info[0], param);
+ return status;
+}
+
+
+/*
+ * Check if factory can allocate the specified codec.
+ */
+static pj_status_t factory_test_alloc( pjmedia_codec_factory *factory,
+ const pjmedia_codec_info *ci )
+{
+ const pj_str_t opus_tag = {"OPUS", 4};
+
+ PJ_UNUSED_ARG(factory);
+ PJ_ASSERT_RETURN(factory==&opus_codec_factory.base, PJ_EINVAL);
+
+ /* Type MUST be audio. */
+ if (ci->type != PJMEDIA_TYPE_AUDIO)
+ return PJMEDIA_CODEC_EUNSUP;
+
+ /* Check encoding name. */
+ if (pj_stricmp(&ci->encoding_name, &opus_tag) != 0)
+ return PJMEDIA_CODEC_EUNSUP;
+
+ /* Check clock rate */
+ if (ci->clock_rate != 8000 && ci->clock_rate != 12000 &&
+ ci->clock_rate != 16000 && ci->clock_rate != 24000 &&
+ ci->clock_rate != 48000)
+ {
+ return PJMEDIA_CODEC_EUNSUP;
+ }
+
+ return PJ_SUCCESS;
+}
+
+
+/*
+ * Generate default attribute.
+ */
+static pj_status_t factory_default_attr( pjmedia_codec_factory *factory,
+ const pjmedia_codec_info *ci,
+ pjmedia_codec_param *attr )
+{
+ TRACE_((THIS_FILE, "%s:%d: - TRACE", __FUNCTION__, __LINE__));
+
+ pj_bzero(attr, sizeof(pjmedia_codec_param));
+ attr->info.pt = (pj_uint8_t)ci->pt;
+ attr->info.clock_rate = opus_cfg.sample_rate;
+ attr->info.channel_cnt = opus_cfg.channel_cnt;
+ attr->info.avg_bps = (opus_cfg.bit_rate > 0) ?
+ opus_cfg.bit_rate :
+ opus_cfg.sample_rate; // Estimate
+ attr->info.max_bps = opus_cfg.bit_rate * 2;
+ attr->info.frm_ptime = 20;
+ attr->setting.frm_per_pkt = 1;
+ attr->info.pcm_bits_per_sample = 16;
+ attr->setting.vad = OPUS_DEFAULT_VAD;
+ attr->setting.plc = OPUS_DEFAULT_PLC;
+
+ generate_fmtp(attr);
+
+ return PJ_SUCCESS;
+}
+
+
+/*
+ * Enum codecs supported by this factory.
+ */
+static pj_status_t factory_enum_codecs( pjmedia_codec_factory *factory,
+ unsigned *count,
+ pjmedia_codec_info codecs[] )
+{
+ PJ_UNUSED_ARG(factory);
+ PJ_ASSERT_RETURN(codecs, PJ_EINVAL);
+
+ if (*count > 0) {
+ pj_bzero(&codecs[0], sizeof(pjmedia_codec_info));
+ codecs[0].type = PJMEDIA_TYPE_AUDIO;
+ codecs[0].pt = PJMEDIA_RTP_PT_OPUS;
+ /*
+ * RFC 7587, Section 7:
+ * The media subtype ("opus") goes in SDP "a=rtpmap" as the encoding
+ * name. The RTP clock rate in "a=rtpmap" MUST be 48000 and the
+ * number of channels MUST be 2.
+ */
+ codecs[0].encoding_name = pj_str("opus");
+ codecs[0].clock_rate = 48000;
+ codecs[0].channel_cnt = 2;
+ *count = 1;
+ }
+
+ return PJ_SUCCESS;
+}
+
+
+/*
+ * Allocate a new Opus codec instance.
+ */
+static pj_status_t factory_alloc_codec( pjmedia_codec_factory *factory,
+ const pjmedia_codec_info *ci,
+ pjmedia_codec **p_codec )
+{
+ pjmedia_codec *codec;
+ pj_pool_t *pool;
+ pj_status_t status;
+ struct opus_data *opus_data;
+ struct opus_codec_factory *f = (struct opus_codec_factory*) factory;
+
+ TRACE_((THIS_FILE, "%s:%d: - TRACE", __FUNCTION__, __LINE__));
+
+ pool = pjmedia_endpt_create_pool(f->endpt, "opus", 512, 512);
+ if (!pool) return PJ_ENOMEM;
+
+ opus_data = PJ_POOL_ZALLOC_T(pool, struct opus_data);
+ codec = PJ_POOL_ZALLOC_T(pool, pjmedia_codec);
+
+ status = pj_mutex_create_simple (pool, "opus_mutex", &opus_data->mutex);
+ if (status != PJ_SUCCESS) {
+ pj_pool_release(pool);
+ return status;
+ }
+
+ pj_memcpy(&opus_data->cfg, &opus_cfg, sizeof(pjmedia_codec_opus_config));
+ opus_data->pool = pool;
+ codec->op = &opus_op;
+ codec->factory = factory;
+ codec->codec_data = opus_data;
+
+ *p_codec = codec;
+ return PJ_SUCCESS;
+}
+
+
+/*
+ * Free codec.
+ */
+static pj_status_t factory_dealloc_codec( pjmedia_codec_factory *factory,
+ pjmedia_codec *codec )
+{
+ struct opus_data *opus_data;
+
+ PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL);
+ PJ_ASSERT_RETURN(factory == &opus_codec_factory.base, PJ_EINVAL);
+
+ opus_data = (struct opus_data *)codec->codec_data;
+ if (opus_data) {
+ pj_mutex_destroy(opus_data->mutex);
+ opus_data->mutex = NULL;
+ pj_pool_release(opus_data->pool);
+ }
+
+ return PJ_SUCCESS;
+}
+
+
+/*
+ * Init codec.
+ */
+static pj_status_t codec_init( pjmedia_codec *codec,
+ pj_pool_t *pool )
+{
+ PJ_UNUSED_ARG(codec);
+ PJ_UNUSED_ARG(pool);
+ return PJ_SUCCESS;
+}
+
+
+/*
+ * Open codec.
+ */
+static pj_status_t codec_open( pjmedia_codec *codec,
+ pjmedia_codec_param *attr )
+{
+ struct opus_data *opus_data = (struct opus_data *)codec->codec_data;
+ int idx, err;
+
+ PJ_ASSERT_RETURN(codec && attr && opus_data, PJ_EINVAL);
+
+ pj_mutex_lock (opus_data->mutex);
+
+ TRACE_((THIS_FILE, "%s:%d: - TRACE", __FUNCTION__, __LINE__));
+
+ opus_data->cfg.sample_rate = attr->info.clock_rate;
+ opus_data->cfg.channel_cnt = attr->info.channel_cnt;
+ opus_data->ptime = attr->info.frm_ptime;
+
+ /* Allocate memory used by the codec */
+ if (!opus_data->enc) {
+ /* Allocate memory for max 2 channels */
+ opus_data->enc = pj_pool_zalloc(opus_data->pool,
+ opus_encoder_get_size(2));
+ }
+ if (!opus_data->dec) {
+ /* Allocate memory for max 2 channels */
+ opus_data->dec = pj_pool_zalloc(opus_data->pool,
+ opus_decoder_get_size(2));
+ }
+ if (!opus_data->enc_packer) {
+ opus_data->enc_packer = pj_pool_zalloc(opus_data->pool,
+ opus_repacketizer_get_size());
+ }
+ if (!opus_data->dec_packer) {
+ opus_data->dec_packer = pj_pool_zalloc(opus_data->pool,
+ opus_repacketizer_get_size());
+ }
+ if (!opus_data->enc || !opus_data->dec ||
+ !opus_data->enc_packer || !opus_data->dec_packer)
+ {
+ PJ_LOG(2, (THIS_FILE, "Unable to allocate memory for the codec"));
+ pj_mutex_unlock (opus_data->mutex);
+ return PJ_ENOMEM;
+ }
+
+ /* Check max average bit rate */
+ idx = find_fmtp(&attr->setting.enc_fmtp, &STR_MAX_BIT_RATE, PJ_FALSE);
+ if (idx >= 0) {
+ unsigned rate;
+ rate = (unsigned)pj_strtoul(&attr->setting.enc_fmtp.param[idx].val);
+ if (rate < attr->info.avg_bps)
+ attr->info.avg_bps = rate;
+ }
+
+ /* Check plc */
+ idx = find_fmtp(&attr->setting.enc_fmtp, &STR_INBAND_FEC, PJ_FALSE);
+ if (idx >= 0) {
+ unsigned plc;
+ plc = (unsigned) pj_strtoul(&attr->setting.enc_fmtp.param[idx].val);
+ attr->setting.plc = plc > 0? PJ_TRUE: PJ_FALSE;
+ }
+
+ /* Check vad */
+ idx = find_fmtp(&attr->setting.enc_fmtp, &STR_DTX, PJ_FALSE);
+ if (idx >= 0) {
+ unsigned vad;
+ vad = (unsigned) pj_strtoul(&attr->setting.enc_fmtp.param[idx].val);
+ attr->setting.vad = vad > 0? PJ_TRUE: PJ_FALSE;
+ }
+
+ /* Check cbr */
+ idx = find_fmtp(&attr->setting.enc_fmtp, &STR_CBR, PJ_FALSE);
+ if (idx >= 0) {
+ unsigned cbr;
+ cbr = (unsigned) pj_strtoul(&attr->setting.enc_fmtp.param[idx].val);
+ opus_data->cfg.cbr = cbr > 0? PJ_TRUE: PJ_FALSE;
+ }
+
+ /* Check max average bit rate */
+ idx = find_fmtp(&attr->setting.dec_fmtp, &STR_MAX_BIT_RATE, PJ_FALSE);
+ if (idx >= 0) {
+ unsigned rate;
+ rate = (unsigned) pj_strtoul(&attr->setting.dec_fmtp.param[idx].val);
+ if (rate < attr->info.avg_bps)
+ attr->info.avg_bps = rate;
+ }
+
+ TRACE_((THIS_FILE, "%s:%d: sample_rate: %u",
+ __FUNCTION__, __LINE__, opus_data->cfg.sample_rate));
+
+ /* Initialize encoder */
+ err = opus_encoder_init(opus_data->enc,
+ opus_data->cfg.sample_rate,
+ attr->info.channel_cnt,
+ OPUS_APPLICATION_VOIP);
+ if (err != OPUS_OK) {
+ PJ_LOG(2, (THIS_FILE, "Unable to create encoder"));
+ return PJMEDIA_CODEC_EFAILED;
+ }
+
+ /* Set signal type */
+ opus_encoder_ctl(opus_data->enc, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
+ /* Set bitrate */
+ opus_encoder_ctl(opus_data->enc, OPUS_SET_BITRATE(attr->info.avg_bps));
+ /* Set VAD */
+ opus_encoder_ctl(opus_data->enc, OPUS_SET_DTX(attr->setting.vad ? 1 : 0));
+ /* Set PLC */
+ opus_encoder_ctl(opus_data->enc,
+ OPUS_SET_INBAND_FEC(attr->setting.plc ? 1 : 0));
+ /* Set bandwidth */
+ opus_encoder_ctl(opus_data->enc,
+ OPUS_SET_MAX_BANDWIDTH(get_opus_bw_constant(
+ opus_data->cfg.sample_rate)));
+ /* Set expected packet loss */
+ opus_encoder_ctl(opus_data->enc,
+ OPUS_SET_PACKET_LOSS_PERC(opus_data->cfg.packet_loss));
+ /* Set complexity */
+ opus_encoder_ctl(opus_data->enc,
+ OPUS_SET_COMPLEXITY(opus_data->cfg.complexity));
+ /* Set constant bit rate */
+ opus_encoder_ctl(opus_data->enc,
+ OPUS_SET_VBR(opus_data->cfg.cbr ? 0 : 1));
+
+ PJ_LOG(5, (THIS_FILE, "Initialize Opus encoder, sample rate: %d, "
+ "avg bitrate: %d, vad: %d, plc: %d, pkt loss: %d, "
+ "complexity: %d, constant bit rate: %d",
+ opus_data->cfg.sample_rate,
+ attr->info.avg_bps, attr->setting.vad?1:0,
+ attr->setting.plc?1:0,
+ opus_data->cfg.packet_loss,
+ opus_data->cfg.complexity,
+ opus_data->cfg.cbr?1:0));
+
+ /* Initialize decoder */
+ err = opus_decoder_init (opus_data->dec,
+ opus_data->cfg.sample_rate,
+ attr->info.channel_cnt);
+ if (err != OPUS_OK) {
+ PJ_LOG(2, (THIS_FILE, "Unable to initialize decoder"));
+ return PJMEDIA_CODEC_EFAILED;
+ }
+
+ /* Initialize temporary decode frames used for FEC */
+ opus_data->dec_frame[0].type = PJMEDIA_FRAME_TYPE_NONE;
+ opus_data->dec_frame[0].buf = pj_pool_zalloc(opus_data->pool,
+ (opus_data->cfg.sample_rate / 1000)
+ * 60 * attr->info.channel_cnt * 2 /* bytes per sample */);
+ opus_data->dec_frame[1].type = PJMEDIA_FRAME_TYPE_NONE;
+ opus_data->dec_frame[1].buf = pj_pool_zalloc(opus_data->pool,
+ (opus_data->cfg.sample_rate / 1000)
+ * 60 * attr->info.channel_cnt * 2 /* bytes per sample */);
+ opus_data->dec_frame_index = -1;
+
+ /* Initialize the repacketizers */
+ opus_repacketizer_init(opus_data->enc_packer);
+ opus_repacketizer_init(opus_data->dec_packer);
+
+ pj_mutex_unlock (opus_data->mutex);
+ return PJ_SUCCESS;
+}
+
+
+/*
+ * Close codec.
+ */
+static pj_status_t codec_close( pjmedia_codec *codec )
+{
+ PJ_UNUSED_ARG(codec);
+ return PJ_SUCCESS;
+}
+
+
+/*
+ * Modify codec settings.
+ */
+static pj_status_t codec_modify( pjmedia_codec *codec,
+ const pjmedia_codec_param *attr )
+{
+ struct opus_data *opus_data = (struct opus_data *)codec->codec_data;
+
+ pj_mutex_lock (opus_data->mutex);
+
+ TRACE_((THIS_FILE, "%s:%d: - TRACE", __FUNCTION__, __LINE__));
+
+ /* Set bitrate */
+ opus_data->cfg.bit_rate = attr->info.avg_bps;
+ opus_encoder_ctl(opus_data->enc, OPUS_SET_BITRATE(attr->info.avg_bps));
+ /* Set VAD */
+ opus_encoder_ctl(opus_data->enc, OPUS_SET_DTX(attr->setting.vad ? 1 : 0));
+ /* Set PLC */
+ opus_encoder_ctl(opus_data->enc,
+ OPUS_SET_INBAND_FEC(attr->setting.plc ? 1 : 0));
+
+ pj_mutex_unlock (opus_data->mutex);
+ return PJ_SUCCESS;
+}
+
+
+/*
+ * Get frames in the packet.
+ */
+static pj_status_t codec_parse( pjmedia_codec *codec,
+ void *pkt,
+ pj_size_t pkt_size,
+ const pj_timestamp *ts,
+ unsigned *frame_cnt,
+ pjmedia_frame frames[] )
+{
+ struct opus_data *opus_data = (struct opus_data *)codec->codec_data;
+ unsigned char tmp_buf[MAX_ENCODED_PACKET_SIZE];
+ int i, num_frames;
+ int size, out_pos;
+ unsigned samples_per_frame;
+#if (USE_INCOMING_WORSE_SETTINGS)
+ int bw;
+#endif
+
+ pj_mutex_lock (opus_data->mutex);
+
+ if (pkt_size > sizeof(tmp_buf)) {
+ PJ_LOG(5, (THIS_FILE, "Encoded size bigger than buffer"));
+ pj_mutex_unlock (opus_data->mutex);
+ return PJMEDIA_CODEC_EFRMTOOSHORT;
+ }
+
+ samples_per_frame = (opus_data->cfg.sample_rate *
+ opus_data->ptime) / 1000;
+
+ pj_memcpy(tmp_buf, pkt, pkt_size);
+
+ opus_repacketizer_init(opus_data->dec_packer);
+ opus_repacketizer_cat(opus_data->dec_packer, tmp_buf, pkt_size);
+
+ num_frames = opus_repacketizer_get_nb_frames(opus_data->dec_packer);
+ out_pos = 0;
+ for (i = 0; i < num_frames; ++i) {
+ size = opus_repacketizer_out_range(opus_data->dec_packer, i, i+1,
+ ((unsigned char*)pkt) + out_pos,
+ sizeof(tmp_buf));
+ if (size < 0) {
+ PJ_LOG(5, (THIS_FILE, "Parse failed! (%d)", pkt_size));
+ pj_mutex_unlock (opus_data->mutex);
+ return PJMEDIA_CODEC_EFAILED;
+ }
+ frames[i].type = PJMEDIA_FRAME_TYPE_AUDIO;
+ frames[i].buf = ((char*)pkt) + out_pos;
+ frames[i].size = size;
+ frames[i].timestamp.u64 = ts->u64 + i * samples_per_frame;
+ out_pos += size;
+ }
+ *frame_cnt = num_frames;
+
+ pj_mutex_unlock (opus_data->mutex);
+ return PJ_SUCCESS;
+}
+
+
+/*
+ * Encode frame.
+ */
+static pj_status_t codec_encode( pjmedia_codec *codec,
+ const struct pjmedia_frame *input,
+ unsigned output_buf_len,
+ struct pjmedia_frame *output )
+{
+ struct opus_data *opus_data = (struct opus_data *)codec->codec_data;
+ opus_int32 size = 0;
+ unsigned in_pos = 0;
+ unsigned out_pos = 0;
+ unsigned frame_size;
+ unsigned samples_per_frame;
+ unsigned char tmp_buf[MAX_ENCODED_PACKET_SIZE];
+ unsigned tmp_bytes_left = sizeof(tmp_buf);
+
+ pj_mutex_lock (opus_data->mutex);
+
+ samples_per_frame = (opus_data->cfg.sample_rate *
+ opus_data->ptime) / 1000;
+ frame_size = samples_per_frame * opus_data->cfg.channel_cnt *
+ sizeof(opus_int16);
+
+ opus_repacketizer_init(opus_data->enc_packer);
+ while (input->size - in_pos >= frame_size) {
+ size = opus_encode(opus_data->enc,
+ (const opus_int16*)(((char*)input->buf) + in_pos),
+ samples_per_frame,
+ tmp_buf + out_pos,
+ (tmp_bytes_left < frame_size ?
+ tmp_bytes_left : frame_size));
+ if (size < 0) {
+ PJ_LOG(4, (THIS_FILE, "Encode failed! (%d)", size));
+ pj_mutex_unlock (opus_data->mutex);
+ return PJMEDIA_CODEC_EFAILED;
+ } else if (size > 0) {
+ /* Only add packets containing more than the TOC */
+ opus_repacketizer_cat(opus_data->enc_packer,
+ tmp_buf + out_pos,
+ size);
+ out_pos += size;
+ tmp_bytes_left -= size;
+ }
+ in_pos += frame_size;
+ }
+
+ if (!opus_repacketizer_get_nb_frames(opus_data->enc_packer)) {
+ /* Empty packet */
+ output->size = 0;
+ output->type = PJMEDIA_FRAME_TYPE_NONE;
+ output->timestamp = input->timestamp;
+ }
+
+ if (size) {
+ size = opus_repacketizer_out(opus_data->enc_packer,
+ output->buf,
+ output_buf_len);
+ if (size < 0) {
+ PJ_LOG(4, (THIS_FILE, "Encode failed! (%d), out_size: %u",
+ size, output_buf_len));
+ pj_mutex_unlock (opus_data->mutex);
+ return PJMEDIA_CODEC_EFAILED;
+ }
+ }
+
+ output->size = (unsigned)size;
+ output->type = PJMEDIA_FRAME_TYPE_AUDIO;
+ output->timestamp = input->timestamp;
+
+ pj_mutex_unlock (opus_data->mutex);
+ return PJ_SUCCESS;
+}
+
+
+/*
+ * Decode frame.
+ */
+static pj_status_t codec_decode( pjmedia_codec *codec,
+ const struct pjmedia_frame *input,
+ unsigned output_buf_len,
+ struct pjmedia_frame *output )
+{
+ struct opus_data *opus_data = (struct opus_data *)codec->codec_data;
+ int decoded_samples;
+ pjmedia_frame *inframe;
+ int fec = 0;
+
+ pj_mutex_lock (opus_data->mutex);
+
+ if (opus_data->dec_frame_index == -1) {
+ /* First packet, buffer it. */
+ opus_data->dec_frame[0].type = input->type;
+ opus_data->dec_frame[0].size = input->size;
+ opus_data->dec_frame[0].timestamp = input->timestamp;
+ pj_memcpy(opus_data->dec_frame[0].buf, input->buf, input->size);
+ opus_data->dec_frame_index = 0;
+ pj_mutex_unlock (opus_data->mutex);
+
+ /* Return zero decoded bytes */
+ output->size = 0;
+ output->type = PJMEDIA_FRAME_TYPE_NONE;
+ output->timestamp = input->timestamp;
+
+ return PJ_SUCCESS;
+ }
+
+ inframe = &opus_data->dec_frame[opus_data->dec_frame_index];
+
+ if (inframe->type != PJMEDIA_FRAME_TYPE_AUDIO) {
+ /* Update current frame index */
+ opus_data->dec_frame_index++;
+ if (opus_data->dec_frame_index > 1)
+ opus_data->dec_frame_index = 0;
+ /* Copy original input buffer to current indexed frame */
+ inframe = &opus_data->dec_frame[opus_data->dec_frame_index];
+ inframe->type = input->type;
+ inframe->size = input->size;
+ inframe->timestamp = input->timestamp;
+ pj_memcpy(inframe->buf, input->buf, input->size);
+ fec = 1;
+ }
+
+ decoded_samples = opus_decode(opus_data->dec,
+ inframe->type==PJMEDIA_FRAME_TYPE_AUDIO ?
+ inframe->buf : NULL,
+ inframe->type==PJMEDIA_FRAME_TYPE_AUDIO ?
+ inframe->size : 0,
+ (opus_int16*)output->buf,
+ output->size / (sizeof(opus_int16) *
+ opus_data->cfg.channel_cnt),
+ fec);
+ output->timestamp = inframe->timestamp;
+
+ if (inframe->type == PJMEDIA_FRAME_TYPE_AUDIO) {
+ /* Mark current indexed frame as invalid */
+ inframe->type = PJMEDIA_FRAME_TYPE_NONE;
+ /* Update current frame index */
+ opus_data->dec_frame_index++;
+ if (opus_data->dec_frame_index > 1)
+ opus_data->dec_frame_index = 0;
+ /* Copy original input buffer to current indexed frame */
+ inframe = &opus_data->dec_frame[opus_data->dec_frame_index];
+ inframe->type = input->type;
+ inframe->size = input->size;
+ inframe->timestamp = input->timestamp;
+ pj_memcpy(inframe->buf, input->buf, input->size);
+ }
+
+ if (decoded_samples < 0) {
+ PJ_LOG(4, (THIS_FILE, "Decode failed!"));
+ pj_mutex_unlock (opus_data->mutex);
+ return PJMEDIA_CODEC_EFAILED;
+ }
+
+ output->size = decoded_samples * sizeof(opus_int16) *
+ opus_data->cfg.channel_cnt;
+ output->type = PJMEDIA_FRAME_TYPE_AUDIO;
+
+ pj_mutex_unlock (opus_data->mutex);
+ return PJ_SUCCESS;
+}
+
+
+/*
+ * Recover lost frame.
+ */
+static pj_status_t codec_recover( pjmedia_codec *codec,
+ unsigned output_buf_len,
+ struct pjmedia_frame *output )
+{
+ struct opus_data *opus_data = (struct opus_data *)codec->codec_data;
+ int decoded_samples;
+ pjmedia_frame *inframe;
+
+ pj_mutex_lock (opus_data->mutex);
+
+ if (opus_data->dec_frame_index == -1) {
+ /* Recover the first packet? Don't think so, fill it with zeroes. */
+ pj_uint16_t samples_per_frame;
+ samples_per_frame = (opus_data->cfg.sample_rate *
+ opus_data->ptime) / 1000;
+ output->type = PJMEDIA_FRAME_TYPE_AUDIO;
+ output->size = samples_per_frame << 1;
+ pjmedia_zero_samples((pj_int16_t*)output->buf, samples_per_frame);
+ pj_mutex_unlock (opus_data->mutex);
+
+ return PJ_SUCCESS;
+ }
+
+ inframe = &opus_data->dec_frame[opus_data->dec_frame_index];
+ decoded_samples = opus_decode(opus_data->dec,
+ inframe->type==PJMEDIA_FRAME_TYPE_AUDIO ?
+ inframe->buf : NULL,
+ inframe->type==PJMEDIA_FRAME_TYPE_AUDIO ?
+ inframe->size : 0,
+ (opus_int16*)output->buf,
+ output->size / (sizeof(opus_int16) *
+ opus_data->cfg.channel_cnt),
+ 0);
+
+ /* Mark current indexed frame as invalid */
+ inframe->type = PJMEDIA_FRAME_TYPE_NONE;
+
+ /* Update current frame index */
+ opus_data->dec_frame_index++;
+ if (opus_data->dec_frame_index > 1)
+ opus_data->dec_frame_index = 0;
+ /* Mark current indexed frame as invalid */
+ inframe = &opus_data->dec_frame[opus_data->dec_frame_index];
+ inframe->type = PJMEDIA_FRAME_TYPE_NONE;
+
+ if (decoded_samples < 0) {
+ PJ_LOG(4, (THIS_FILE, "Recover failed!"));
+ pj_mutex_unlock (opus_data->mutex);
+ return PJMEDIA_CODEC_EFAILED;
+ }
+
+ output->size = decoded_samples * sizeof(opus_int16) *
+ opus_data->cfg.channel_cnt;
+ output->type = PJMEDIA_FRAME_TYPE_AUDIO;
+ output->timestamp = inframe->timestamp;
+
+ pj_mutex_unlock (opus_data->mutex);
+ return PJ_SUCCESS;
+}
+
+#if defined(_MSC_VER)
+# pragma comment(lib, "libopus.a")
+#endif
+
+
+#endif /* PJMEDIA_HAS_OPUS_CODEC */
diff --git a/pjmedia/src/pjmedia/stream.c b/pjmedia/src/pjmedia/stream.c
index 1b66dd87..d799dfd4 100644
--- a/pjmedia/src/pjmedia/stream.c
+++ b/pjmedia/src/pjmedia/stream.c
@@ -2109,6 +2109,15 @@ PJ_DEF(pj_status_t) pjmedia_stream_create( pjmedia_endpt *endpt,
goto err_cleanup;
/* Open the codec. */
+
+ /* The clock rate for Opus codec is not static,
+ * it's negotiated in the SDP.
+ */
+ if (!pj_stricmp2(&info->fmt.encoding_name, "opus")) {
+ stream->codec_param.info.clock_rate = info->fmt.clock_rate;
+ stream->codec_param.info.channel_cnt = info->fmt.channel_cnt;
+ }
+
status = pjmedia_codec_open(stream->codec, &stream->codec_param);
if (status != PJ_SUCCESS)
goto err_cleanup;
@@ -2223,6 +2232,12 @@ PJ_DEF(pj_status_t) pjmedia_stream_create( pjmedia_endpt *endpt,
stream->has_g722_mpeg_bug = PJ_TRUE;
/* RTP clock rate = 1/2 real clock rate */
stream->rtp_tx_ts_len_per_pkt >>= 1;
+ } else if (!pj_stricmp2(&info->fmt.encoding_name, "opus")) {
+ unsigned opus_ts_modifier = 48000 / afd->clock_rate;
+ stream->rtp_rx_check_cnt = 0;
+ stream->has_g722_mpeg_bug = PJ_TRUE;
+ stream->rtp_tx_ts_len_per_pkt *= opus_ts_modifier;
+ stream->rtp_rx_ts_len_per_frame *= opus_ts_modifier;
}
#endif
diff --git a/pjmedia/src/pjmedia/stream_info.c b/pjmedia/src/pjmedia/stream_info.c
index 3ed32f7e..878d44c1 100644
--- a/pjmedia/src/pjmedia/stream_info.c
+++ b/pjmedia/src/pjmedia/stream_info.c
@@ -33,6 +33,47 @@ static const pj_str_t ID_RTP_SAVP = { "RTP/SAVP", 8 };
static const pj_str_t ID_RTPMAP = { "rtpmap", 6 };
static const pj_str_t ID_TELEPHONE_EVENT = { "telephone-event", 15 };
+static void get_opus_channels_and_clock_rate(const pjmedia_codec_fmtp *enc_fmtp,
+ const pjmedia_codec_fmtp *dec_fmtp,
+ unsigned *channel_cnt,
+ unsigned *clock_rate)
+{
+ unsigned i;
+ unsigned enc_channel_cnt = 0, local_channel_cnt = 0;
+ unsigned enc_clock_rate = 0, local_clock_rate = 0;
+
+ for (i = 0; i < dec_fmtp->cnt; ++i) {
+ if (!pj_stricmp2(&dec_fmtp->param[i].name, "sprop-maxcapturerate")) {
+ local_clock_rate = (unsigned)pj_strtoul(&dec_fmtp->param[i].val);
+ } else if (!pj_stricmp2(&dec_fmtp->param[i].name, "sprop-stereo")) {
+ local_channel_cnt = (unsigned)pj_strtoul(&dec_fmtp->param[i].val);
+ local_channel_cnt = (local_channel_cnt > 0) ? 2 : 1;
+ }
+ }
+ if (!local_clock_rate) local_clock_rate = *clock_rate;
+ if (!local_channel_cnt) local_channel_cnt = *channel_cnt;
+
+ for (i = 0; i < enc_fmtp->cnt; ++i) {
+ if (!pj_stricmp2(&enc_fmtp->param[i].name, "maxplaybackrate")) {
+ enc_clock_rate = (unsigned)pj_strtoul(&enc_fmtp->param[i].val);
+ } else if (!pj_stricmp2(&enc_fmtp->param[i].name, "stereo")) {
+ enc_channel_cnt = (unsigned)pj_strtoul(&enc_fmtp->param[i].val);
+ enc_channel_cnt = (enc_channel_cnt > 0) ? 2 : 1;
+ }
+ }
+ /* The default is a standard mono session with 48000 Hz clock rate
+ * (RFC 7587, section 7)
+ */
+ if (!enc_clock_rate) enc_clock_rate = 48000;
+ if (!enc_channel_cnt) enc_channel_cnt = 1;
+
+ *clock_rate = (enc_clock_rate < local_clock_rate) ? enc_clock_rate :
+ local_clock_rate;
+
+ *channel_cnt = (enc_channel_cnt < local_channel_cnt) ? enc_channel_cnt :
+ local_channel_cnt;
+}
+
/*
* Internal function for collecting codec info and param from the SDP media.
*/
@@ -218,6 +259,14 @@ static pj_status_t get_audio_codec_info_param(pjmedia_stream_info *si,
pjmedia_stream_info_parse_fmtp(pool, local_m, si->rx_pt,
&si->param->setting.dec_fmtp);
+ if (!pj_stricmp2(&si->fmt.encoding_name, "opus")) {
+ get_opus_channels_and_clock_rate(&si->param->setting.enc_fmtp,
+ &si->param->setting.dec_fmtp,
+ &si->fmt.channel_cnt,
+ &si->fmt.clock_rate);
+ }
+
+
/* Get the remote ptime for our encoder. */
attr = pjmedia_sdp_attr_find2(rem_m->attr_count, rem_m->attr,
"ptime", NULL);