Index: /pjproject/trunk/pjmedia/build/pjmedia.dsp
===================================================================
--- /pjproject/trunk/pjmedia/build/pjmedia.dsp	(revision 641)
+++ /pjproject/trunk/pjmedia/build/pjmedia.dsp	(revision 642)
@@ -42,5 +42,5 @@
 # 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"
@@ -66,5 +66,5 @@
 # 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"
@@ -88,4 +88,8 @@
 # Begin Source File
 
+SOURCE=..\src\pjmedia\aec_port.c
+# End Source File
+# Begin Source File
+
 SOURCE=..\src\pjmedia\alaw_ulaw.c
 # End Source File
@@ -226,4 +230,8 @@
 
 # PROP Default_Filter "h;hpp;hxx;hm;inl"
+# Begin Source File
+
+SOURCE=..\include\pjmedia\aec_port.h
+# End Source File
 # Begin Source File
 
Index: /pjproject/trunk/pjmedia/include/pjmedia.h
===================================================================
--- /pjproject/trunk/pjmedia/include/pjmedia.h	(revision 641)
+++ /pjproject/trunk/pjmedia/include/pjmedia.h	(revision 642)
@@ -26,4 +26,5 @@
 
 #include <pjmedia/types.h>
+#include <pjmedia/aec_port.h>
 #include <pjmedia/clock.h>
 #include <pjmedia/codec.h>
Index: /pjproject/trunk/pjmedia/include/pjmedia/aec_port.h
===================================================================
--- /pjproject/trunk/pjmedia/include/pjmedia/aec_port.h	(revision 642)
+++ /pjproject/trunk/pjmedia/include/pjmedia/aec_port.h	(revision 642)
@@ -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__ */
Index: /pjproject/trunk/pjmedia/src/pjmedia/aec_port.c
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia/aec_port.c	(revision 642)
+++ /pjproject/trunk/pjmedia/src/pjmedia/aec_port.c	(revision 642)
@@ -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;
+}
+
+
Index: /pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h
===================================================================
--- /pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h	(revision 641)
+++ /pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h	(revision 642)
@@ -2129,4 +2129,10 @@
     unsigned		rx_drop_pct;
 
+    /**
+     * Echo canceller tail length, in miliseconds.
+     *
+     * Default: 256
+     */
+    unsigned		ec_tail_len;
 };
 
@@ -2147,4 +2153,5 @@
     cfg->quality = 6;
     cfg->ilbc_mode = 20;
+    cfg->ec_tail_len = 256;
 }
 
Index: /pjproject/trunk/pjsip/include/pjsua-lib/pjsua_internal.h
===================================================================
--- /pjproject/trunk/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 641)
+++ /pjproject/trunk/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 642)
@@ -205,4 +205,5 @@
     int			 cap_dev;   /**< Capture device ID.		*/
     int			 play_dev;  /**< Playback device ID.		*/
+    pjmedia_port	*aec_port;  /**< AEC port to conf bridge.	*/
     pj_bool_t		 no_snd;    /**< No sound (app will manage it)	*/
     pjmedia_snd_port	*snd_port;  /**< Sound port.			*/
Index: /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c
===================================================================
--- /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 641)
+++ /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 642)
@@ -969,6 +969,21 @@
     pj_assert(conf_port != NULL);
 
-    /* Connect to the conference port */
-    status = pjmedia_snd_port_connect(pjsua_var.snd_port, conf_port);
+    /* Create AEC if it's not created */
+    if (pjsua_var.aec_port == NULL) {
+	status = pjmedia_aec_port_create(pjsua_var.pool, conf_port,
+					 conf_port->info.clock_rate * 
+					    pjsua_var.media_cfg.ec_tail_len /
+					    1000,
+					 &pjsua_var.aec_port);
+	if (status != PJ_SUCCESS) {
+	    pjsua_perror(THIS_FILE, "Unable to create AEC port", status);
+	    pjmedia_snd_port_destroy(pjsua_var.snd_port);
+	    pjsua_var.snd_port = NULL;
+	    return status;
+	}
+    }
+
+    /* Connect to the AEC port */
+    status = pjmedia_snd_port_connect(pjsua_var.snd_port, pjsua_var.aec_port);
     if (status != PJ_SUCCESS) {
 	pjsua_perror(THIS_FILE, "Unable to connect conference port to "
