summaryrefslogtreecommitdiff
path: root/pjmedia
diff options
context:
space:
mode:
authorBenny Prijono <bennylp@teluu.com>2006-08-02 18:22:22 +0000
committerBenny Prijono <bennylp@teluu.com>2006-08-02 18:22:22 +0000
commit2c5b8f5dca277dd3fee1ab1298bb0b2308372eae (patch)
tree395e0e06ffdb66993cb79cd31f5b38ec561e0c67 /pjmedia
parent6d4d05a0b62d4d7be91f807943009f2ec57c2421 (diff)
Added experimental AEC media port (aec_port.[hc]) based on Speex AEC in pjmedia and pjsua-lib.
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@642 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjmedia')
-rw-r--r--pjmedia/build/pjmedia.dsp12
-rw-r--r--pjmedia/include/pjmedia.h1
-rw-r--r--pjmedia/include/pjmedia/aec_port.h65
-rw-r--r--pjmedia/src/pjmedia/aec_port.c162
4 files changed, 238 insertions, 2 deletions
diff --git a/pjmedia/build/pjmedia.dsp b/pjmedia/build/pjmedia.dsp
index e024f655..62a544d3 100644
--- a/pjmedia/build/pjmedia.dsp
+++ b/pjmedia/build/pjmedia.dsp
@@ -41,7 +41,7 @@ RSC=rc.exe
# PROP Intermediate_Dir ".\output\pjmedia-i386-win32-vc6-release"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c
-# ADD CPP /nologo /MD /W4 /GX /O2 /I "../include" /I "../../pjlib/include" /I "../../pjlib-util/include" /I "../src/pjmedia/portaudio" /D "NDEBUG" /D "PA_NO_ASIO" /D "WIN32" /D "_MBCS" /D "_LIB" /D PJ_WIN32=1 /D PJ_M_I386=1 /FR /FD /c
+# ADD CPP /nologo /MD /W4 /GX /O2 /I "../include" /I "../../pjlib/include" /I "../../pjlib-util/include" /I "../src/pjmedia/portaudio" /I "../src/pjmedia-codec" /D "NDEBUG" /D "PA_NO_ASIO" /D "WIN32" /D "_MBCS" /D "_LIB" /D PJ_WIN32=1 /D PJ_M_I386=1 /FR /FD /c
# SUBTRACT CPP /YX
# ADD BASE RSC /l 0x409 /d "NDEBUG"
# ADD RSC /l 0x409 /d "NDEBUG"
@@ -65,7 +65,7 @@ LIB32=link.exe -lib
# PROP Intermediate_Dir ".\output\pjmedia-i386-win32-vc6-debug"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c
-# ADD CPP /nologo /MTd /W4 /Gm /GX /ZI /Od /I "../include" /I "../../pjlib/include" /I "../../pjlib-util/include" /I "../src/pjmedia/portaudio" /D "_DEBUG" /D "PA_NO_ASIO" /D "PA_NO_WMME" /D "WIN32" /D "_MBCS" /D "_LIB" /D PJ_WIN32=1 /D PJ_M_I386=1 /FR /FD /GZ /c
+# ADD CPP /nologo /MTd /W4 /Gm /GX /ZI /Od /I "../include" /I "../../pjlib/include" /I "../../pjlib-util/include" /I "../src/pjmedia/portaudio" /I "../src/pjmedia-codec" /D "_DEBUG" /D "PA_NO_ASIO" /D "PA_NO_WMME" /D "WIN32" /D "_MBCS" /D "_LIB" /D PJ_WIN32=1 /D PJ_M_I386=1 /FR /FD /GZ /c
# SUBTRACT CPP /YX
# ADD BASE RSC /l 0x409 /d "_DEBUG"
# ADD RSC /l 0x409 /d "_DEBUG"
@@ -87,6 +87,10 @@ LIB32=link.exe -lib
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
+SOURCE=..\src\pjmedia\aec_port.c
+# End Source File
+# Begin Source File
+
SOURCE=..\src\pjmedia\alaw_ulaw.c
# End Source File
# Begin Source File
@@ -227,6 +231,10 @@ SOURCE=..\src\pjmedia\wave.c
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
+SOURCE=..\include\pjmedia\aec_port.h
+# End Source File
+# Begin Source File
+
SOURCE=..\include\pjmedia\clock.h
# End Source File
# Begin Source File
diff --git a/pjmedia/include/pjmedia.h b/pjmedia/include/pjmedia.h
index a6c0e49b..fc79c463 100644
--- a/pjmedia/include/pjmedia.h
+++ b/pjmedia/include/pjmedia.h
@@ -25,6 +25,7 @@
*/
#include <pjmedia/types.h>
+#include <pjmedia/aec_port.h>
#include <pjmedia/clock.h>
#include <pjmedia/codec.h>
#include <pjmedia/conference.h>
diff --git a/pjmedia/include/pjmedia/aec_port.h b/pjmedia/include/pjmedia/aec_port.h
new file mode 100644
index 00000000..f18b1378
--- /dev/null
+++ b/pjmedia/include/pjmedia/aec_port.h
@@ -0,0 +1,65 @@
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
+ *
+ * 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
+ */
+#ifndef __PJMEDIA_AEC_PORT_H__
+#define __PJMEDIA_AEC_PORT_H__
+
+/**
+ * @file aec_port.h
+ * @brief AEC (Accoustic Echo Cancellation) media port.
+ */
+#include <pjmedia/port.h>
+
+
+
+/**
+ * @defgroup PJMEDIA_AEC_PORT AEC Port
+ * @ingroup PJMEDIA_PORT
+ * @brief AEC (Accoustic Echo Cancellation) media port.
+ * @{
+ */
+
+
+PJ_BEGIN_DECL
+
+
+/**
+ * Create AEC port.
+ *
+ * @param pool Pool to allocate memory.
+ * @param dn_port Downstream port.
+ * @param tail_length Tail length in samples.
+ * @param p_port Pointer to receive the port instance.
+ *
+ * @return PJ_SUCCESS on success.
+ */
+PJ_DECL(pj_status_t) pjmedia_aec_port_create( pj_pool_t *pool,
+ pjmedia_port *dn_port,
+ unsigned tail_length,
+ pjmedia_port **p_port );
+
+
+
+PJ_END_DECL
+
+/**
+ * @}
+ */
+
+
+#endif /* __PJMEDIA_AEC_PORT_H__ */
diff --git a/pjmedia/src/pjmedia/aec_port.c b/pjmedia/src/pjmedia/aec_port.c
new file mode 100644
index 00000000..d00d9298
--- /dev/null
+++ b/pjmedia/src/pjmedia/aec_port.c
@@ -0,0 +1,162 @@
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
+ *
+ * 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
+ */
+#include <pjmedia/aec_port.h>
+#include "../pjmedia-codec/speex/speex_echo.h"
+#include <pjmedia/errno.h>
+#include <pj/assert.h>
+#include <pj/log.h>
+#include <pj/pool.h>
+
+
+#define THIS_FILE "aec_port.c"
+#define SIGNATURE PJMEDIA_PORT_SIGNATURE('A', 'E', 'C', ' ')
+
+
+struct aec_port
+{
+ pjmedia_port base;
+ pjmedia_port *dn_port;
+ SpeexEchoState *state;
+ pj_int16_t *tmp_frame;
+ pj_bool_t has_frame;
+ pj_int16_t *last_frame;
+};
+
+
+static pj_status_t aec_put_frame(pjmedia_port *this_port,
+ const pjmedia_frame *frame);
+static pj_status_t aec_get_frame(pjmedia_port *this_port,
+ pjmedia_frame *frame);
+static pj_status_t aec_on_destroy(pjmedia_port *this_port);
+
+
+PJ_DEF(pj_status_t) pjmedia_aec_port_create( pj_pool_t *pool,
+ pjmedia_port *dn_port,
+ unsigned tail_length,
+ pjmedia_port **p_port )
+{
+ const pj_str_t AEC = { "AEC", 3 };
+ struct aec_port *aec_port;
+ int sampling_rate;
+
+ PJ_ASSERT_RETURN(pool && dn_port && p_port, PJ_EINVAL);
+ PJ_ASSERT_RETURN(dn_port->info.bits_per_sample==16 && tail_length,
+ PJ_EINVAL);
+
+ /* Create and initialize the port */
+ aec_port = pj_pool_zalloc(pool, sizeof(struct aec_port));
+
+ pjmedia_port_info_init(&aec_port->base.info, &AEC, SIGNATURE,
+ dn_port->info.clock_rate,
+ dn_port->info.channel_count,
+ dn_port->info.bits_per_sample,
+ dn_port->info.samples_per_frame);
+
+ aec_port->state = speex_echo_state_init(dn_port->info.samples_per_frame,
+ tail_length);
+
+ /* Set sampling rate */
+ sampling_rate = 0;
+ speex_echo_ctl(aec_port->state, SPEEX_ECHO_GET_SAMPLING_RATE,
+ &sampling_rate);
+ sampling_rate = dn_port->info.clock_rate;
+ speex_echo_ctl(aec_port->state, SPEEX_ECHO_SET_SAMPLING_RATE,
+ &sampling_rate);
+
+ /* More init */
+ aec_port->dn_port = dn_port;
+ aec_port->base.get_frame = &aec_get_frame;
+ aec_port->base.put_frame = &aec_put_frame;
+ aec_port->base.on_destroy = &aec_on_destroy;
+
+ aec_port->last_frame = pj_pool_zalloc(pool, sizeof(pj_int16_t) *
+ dn_port->info.samples_per_frame);
+ aec_port->tmp_frame = pj_pool_zalloc(pool, sizeof(pj_int16_t) *
+ dn_port->info.samples_per_frame);
+
+ /* Done */
+ *p_port = &aec_port->base;
+
+ PJ_LOG(4,(THIS_FILE, "AEC created for port %.*s",
+ (int)dn_port->info.name.slen,
+ dn_port->info.name.ptr));
+ return PJ_SUCCESS;
+}
+
+
+static pj_status_t aec_put_frame(pjmedia_port *this_port,
+ const pjmedia_frame *frame)
+{
+ struct aec_port *aec_port = (struct aec_port*)this_port;
+
+ PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE, PJ_EINVAL);
+
+ if (frame->type == PJMEDIA_FRAME_TYPE_NONE || !aec_port->has_frame) {
+ return pjmedia_port_put_frame(aec_port->dn_port, frame);
+ }
+
+ PJ_ASSERT_RETURN(frame->size == this_port->info.samples_per_frame * 2,
+ PJ_EINVAL);
+
+ speex_echo_cancel(aec_port->state,
+ (const spx_int16_t*)frame->buf,
+ (const spx_int16_t*)aec_port->last_frame,
+ (spx_int16_t*)aec_port->tmp_frame,
+ NULL);
+
+ pjmedia_copy_samples(frame->buf, aec_port->tmp_frame,
+ this_port->info.samples_per_frame);
+
+ return pjmedia_port_put_frame(aec_port->dn_port, frame);
+}
+
+
+static pj_status_t aec_get_frame( pjmedia_port *this_port,
+ pjmedia_frame *frame)
+{
+ struct aec_port *aec_port = (struct aec_port*)this_port;
+ pj_status_t status;
+
+ PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE, PJ_EINVAL);
+
+ status = pjmedia_port_get_frame(aec_port->dn_port, frame);
+ if (status==PJ_SUCCESS && frame->type==PJMEDIA_FRAME_TYPE_AUDIO) {
+ aec_port->has_frame = PJ_TRUE;
+ pjmedia_copy_samples(aec_port->tmp_frame, frame->buf,
+ this_port->info.samples_per_frame);
+ } else {
+ aec_port->has_frame = PJ_FALSE;
+ }
+
+ return status;
+}
+
+
+static pj_status_t aec_on_destroy(pjmedia_port *this_port)
+{
+ struct aec_port *aec_port = (struct aec_port*)this_port;
+
+ PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE, PJ_EINVAL);
+
+ speex_echo_state_destroy(aec_port->state);
+
+ return PJ_SUCCESS;
+}
+
+