Index: pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia/sound_port.c
===================================================================
--- pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia/sound_port.c	(revision 2460)
+++ pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia/sound_port.c	(revision 2468)
@@ -23,5 +23,4 @@
 #include <pjmedia/echo.h>
 #include <pjmedia/errno.h>
-#include <pjmedia/plc.h>
 #include <pj/assert.h>
 #include <pj/log.h>
@@ -29,5 +28,4 @@
 #include <pj/string.h>	    /* pj_memset() */
 
-//#define SIMULATE_LOST_PCT   20
 #define AEC_TAIL	    128	    /* default AEC length in ms */
 #define AEC_SUSPEND_LIMIT   5	    /* seconds of no activity	*/
@@ -41,16 +39,9 @@
     int			 rec_id;
     int			 play_id;
-    pjmedia_snd_stream	*snd_stream;
+    pj_uint32_t		 aud_caps;
+    pjmedia_aud_param	 aud_param;
+    pjmedia_aud_stream	*aud_stream;
     pjmedia_dir		 dir;
     pjmedia_port	*port;
-
-    pjmedia_echo_state	*ec_state;
-    unsigned		 aec_tail_len;
-
-    pj_bool_t		 ec_suspended;
-    unsigned		 ec_suspend_count;
-    unsigned		 ec_suspend_limit;
-
-    pjmedia_plc		*plc;
 
     unsigned		 clock_rate;
@@ -58,16 +49,12 @@
     unsigned		 samples_per_frame;
     unsigned		 bits_per_sample;
-    pjmedia_snd_setting  setting;
-
-#if PJMEDIA_SOUND_USE_DELAYBUF
-    pjmedia_delay_buf	*delay_buf;
-#endif
-
-    /* Encoded sound emulation */
-#if !defined(PJMEDIA_SND_SUPPORT_OPEN2) || !PJMEDIA_SND_SUPPORT_OPEN2
-    unsigned		 frm_buf_size;
-    pj_uint8_t		*put_frm_buf;
-    pj_uint8_t		*get_frm_buf;
-#endif
+
+    /* software ec */
+    pjmedia_echo_state	*ec_state;
+    unsigned		 ec_options;
+    unsigned		 ec_tail_len;
+    pj_bool_t		 ec_suspended;
+    unsigned		 ec_suspend_count;
+    unsigned		 ec_suspend_limit;
 };
 
@@ -76,12 +63,9 @@
  * played.
  */
-static pj_status_t play_cb(/* in */   void *user_data,
-			   /* in */   pj_uint32_t timestamp,
-			   /* out */  void *output,
-			   /* out */  unsigned size)
+static pj_status_t play_cb(void *user_data, pjmedia_frame *frame)
 {
     pjmedia_snd_port *snd_port = (pjmedia_snd_port*) user_data;
     pjmedia_port *port;
-    pjmedia_frame frame;
+    unsigned required_size = frame->size;
     pj_status_t status;
 
@@ -90,56 +74,14 @@
 	goto no_frame;
 
-    frame.buf = output;
-    frame.size = size;
-    frame.timestamp.u32.hi = 0;
-    frame.timestamp.u32.lo = timestamp;
-
-#if PJMEDIA_SOUND_USE_DELAYBUF
-    if (snd_port->delay_buf) {
-	status = pjmedia_delay_buf_get(snd_port->delay_buf, (pj_int16_t*)output);
-	if (status != PJ_SUCCESS)
-	    pj_bzero(output, size);
-
-	frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
-	pjmedia_port_put_frame(port, &frame);
-
-#ifdef TEST_OVERFLOW_UNDERFLOW
-	{
-	    static int count = 1;
-	    if (++count % 10 == 0) {
-		status = pjmedia_delay_buf_get(snd_port->delay_buf, 
-					       (pj_int16_t*)output);
-		if (status != PJ_SUCCESS)
-		    pj_bzero(output, size);
-
-		frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
-		pjmedia_port_put_frame(port, &frame);
-	    }
-	}
-#endif
-
-    }
-#endif
-
-    status = pjmedia_port_get_frame(port, &frame);
+    status = pjmedia_port_get_frame(port, frame);
     if (status != PJ_SUCCESS)
 	goto no_frame;
 
-    if (frame.type != PJMEDIA_FRAME_TYPE_AUDIO)
+    if (frame->type != PJMEDIA_FRAME_TYPE_AUDIO)
 	goto no_frame;
 
     /* Must supply the required samples */
-    pj_assert(frame.size == size);
-
-#ifdef SIMULATE_LOST_PCT
-    /* Simulate packet lost */
-    if (pj_rand() % 100 < SIMULATE_LOST_PCT) {
-	PJ_LOG(4,(THIS_FILE, "Frame dropped"));
-	goto no_frame;
-    }
-#endif
-
-    if (snd_port->plc)
-	pjmedia_plc_save(snd_port->plc, (pj_int16_t*) output);
+    PJ_UNUSED_ARG(required_size);
+    pj_assert(frame->size == required_size);
 
     if (snd_port->ec_state) {
@@ -150,5 +92,5 @@
 	}
 	snd_port->ec_suspend_count = 0;
-	pjmedia_echo_playback(snd_port->ec_state, (pj_int16_t*)output);
+	pjmedia_echo_playback(snd_port->ec_state, (pj_int16_t*)frame->buf);
     }
 
@@ -166,19 +108,7 @@
 	if (snd_port->ec_state) {
 	    /* To maintain correct delay in EC */
-	    pjmedia_echo_playback(snd_port->ec_state, (pj_int16_t*)output);
-	}
-    }
-
-    /* Apply PLC */
-    if (snd_port->plc) {
-
-	pjmedia_plc_generate(snd_port->plc, (pj_int16_t*) output);
-#ifdef SIMULATE_LOST_PCT
-	PJ_LOG(4,(THIS_FILE, "Lost frame generated"));
-#endif
-    } else {
-	pj_bzero(output, size);
-    }
-
+	    pjmedia_echo_playback(snd_port->ec_state, (pj_int16_t*)frame->buf);
+	}
+    }
 
     return PJ_SUCCESS;
@@ -190,12 +120,8 @@
  * frame.
  */
-static pj_status_t rec_cb(/* in */   void *user_data,
-			  /* in */   pj_uint32_t timestamp,
-			  /* in */   void *input,
-			  /* in*/    unsigned size)
+static pj_status_t rec_cb(void *user_data, pjmedia_frame *frame)
 {
     pjmedia_snd_port *snd_port = (pjmedia_snd_port*) user_data;
     pjmedia_port *port;
-    pjmedia_frame frame;
 
     port = snd_port->port;
@@ -205,26 +131,8 @@
     /* Cancel echo */
     if (snd_port->ec_state && !snd_port->ec_suspended) {
-	pjmedia_echo_capture(snd_port->ec_state, (pj_int16_t*) input, 0);
-    }
-
-#if PJMEDIA_SOUND_USE_DELAYBUF
-    if (snd_port->delay_buf) {
-	pjmedia_delay_buf_put(snd_port->delay_buf, (pj_int16_t*)input);
-    } else {
-	frame.buf = (void*)input;
-	frame.size = size;
-	frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
-	frame.timestamp.u32.lo = timestamp;
-
-	pjmedia_port_put_frame(port, &frame);
-    }
-#else
-    frame.buf = (void*)input;
-    frame.size = size;
-    frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
-    frame.timestamp.u32.lo = timestamp;
-
-    pjmedia_port_put_frame(port, &frame);
-#endif
+	pjmedia_echo_capture(snd_port->ec_state, (pj_int16_t*) frame->buf, 0);
+    }
+
+    pjmedia_port_put_frame(port, frame);
 
     return PJ_SUCCESS;
@@ -235,22 +143,9 @@
  * played. This version is for non-PCM data.
  */
-static pj_status_t play_cb_ext(/* in */   void *user_data,
-			       /* in */   pj_uint32_t timestamp,
-			       /* out */  void *output,
-			       /* out */  unsigned size)
-{
-#if defined(PJMEDIA_SND_SUPPORT_OPEN2) && PJMEDIA_SND_SUPPORT_OPEN2!=0
-    /* This is the version to use when the sound device supports
-     * open2().
-     */
+static pj_status_t play_cb_ext(void *user_data, pjmedia_frame *frame)
+{
     pjmedia_snd_port *snd_port = (pjmedia_snd_port*) user_data;
-    pjmedia_port *port;
-    pjmedia_frame *frame = (pjmedia_frame*) output;
-    pj_status_t status;
-
-    PJ_UNUSED_ARG(size);
-    PJ_UNUSED_ARG(timestamp);
-
-    port = snd_port->port;
+    pjmedia_port *port = snd_port->port;
+
     if (port == NULL) {
 	frame->type = PJMEDIA_FRAME_TYPE_NONE;
@@ -258,93 +153,7 @@
     }
 
-    status = pjmedia_port_get_frame(port, frame);
-
-    return status;
-#else
-    /* This is the emulation version */
-    pjmedia_snd_port *snd_port = (pjmedia_snd_port*) user_data;
-    pjmedia_port *port = snd_port->port;
-    pjmedia_frame_ext *fx = (pjmedia_frame_ext*) snd_port->get_frm_buf;
-    pj_status_t status;
-
-    if (port==NULL) {
-	goto no_frame;
-    }
-
-    pj_bzero(fx, sizeof(*fx));
-    fx->base.type = PJMEDIA_FRAME_TYPE_NONE;
-    fx->base.buf = ((pj_uint8_t*)fx) + sizeof(*fx);
-    fx->base.size = snd_port->frm_buf_size - sizeof(*fx);
-    fx->base.timestamp.u32.hi = 0;
-    fx->base.timestamp.u32.lo = timestamp;
-
-    status = pjmedia_port_get_frame(port, &fx->base);
-    if (status != PJ_SUCCESS)
-	goto no_frame;
-
-    if (fx->base.type == PJMEDIA_FRAME_TYPE_AUDIO) {
-	pj_assert(fx->base.size == size);
-	pj_memcpy(output, fx->base.buf, size);
-    } else if (fx->base.type == PJMEDIA_FRAME_TYPE_EXTENDED) {
-	void (*decoder)(pj_int16_t*, const pj_uint8_t*, pj_size_t) = NULL;
-	unsigned i, size_decoded;
-
-	switch (snd_port->setting.format.id) {
-	case PJMEDIA_FORMAT_PCMA:
-	    decoder = &pjmedia_alaw_decode;
-	    break;
-	case PJMEDIA_FORMAT_PCMU:
-	    decoder = &pjmedia_ulaw_decode;
-	    break;
-	default:
-	    PJ_LOG(1,(THIS_FILE, "Unsupported format %d", 
-		      snd_port->setting.format.id));
-	    goto no_frame;
-	}
-
-	if (fx->samples_cnt > size>>1) {
-	    PJ_LOG(4,(THIS_FILE, "Frame too large by %d samples", 
-		      fx->samples_cnt - (size>>1)));
-	} else if (fx->samples_cnt < size>>1) {
-	    PJ_LOG(4,(THIS_FILE, "Not enough frame by %d samples", 
-		      (size>>1) - fx->samples_cnt));
-	}
-
-	for (i=0, size_decoded=0; 
-	     i<fx->subframe_cnt && size_decoded<size; 
-	     ++i) 
-	{
-	    pjmedia_frame_ext_subframe *subfrm;
-
-	    subfrm = pjmedia_frame_ext_get_subframe(fx, i);	    
-
-	    if (!subfrm || subfrm->bitlen==0)
-		continue;
-
-	    if ((subfrm->bitlen>>3) > (int)(size-size_decoded)) {
-		subfrm->bitlen = (pj_uint16_t)((size-size_decoded) << 3);
-	    }
-
-	    (*decoder)((short*)((pj_uint8_t*)output + size_decoded),
-		       subfrm->data, subfrm->bitlen>>3);
-
-	    size_decoded += (subfrm->bitlen>>3) << 1;
-	}
-
-	if (size_decoded < size) {
-	    pj_bzero((pj_uint8_t*)output + size_decoded, size-size_decoded);
-	}
-
-    } else {
-	goto no_frame;
-    }
-
-    return PJ_SUCCESS;
-
-no_frame:
-    pj_bzero(output, size);
-    return PJ_SUCCESS;
-
-#endif	/* PJMEDIA_SND_SUPPORT_OPEN2 */
+    pjmedia_port_get_frame(port, frame);
+
+    return PJ_SUCCESS;
 }
 
@@ -354,19 +163,8 @@
  * frame. This version is for non-PCM data.
  */
-static pj_status_t rec_cb_ext(/* in */   void *user_data,
-			      /* in */   pj_uint32_t timestamp,
-			      /* in */   void *input,
-			      /* in*/    unsigned size)
-{
-#if defined(PJMEDIA_SND_SUPPORT_OPEN2) && PJMEDIA_SND_SUPPORT_OPEN2!=0
-    /* This is the version to use when the sound device supports
-     * open2().
-     */
+static pj_status_t rec_cb_ext(void *user_data, pjmedia_frame *frame)
+{
     pjmedia_snd_port *snd_port = (pjmedia_snd_port*) user_data;
     pjmedia_port *port;
-    pjmedia_frame *frame = (pjmedia_frame*)input;
-
-    PJ_UNUSED_ARG(size);
-    PJ_UNUSED_ARG(timestamp);
 
     port = snd_port->port;
@@ -377,40 +175,4 @@
 
     return PJ_SUCCESS;
-#else
-    pjmedia_snd_port *snd_port = (pjmedia_snd_port*) user_data;
-    pjmedia_port *port = snd_port->port;
-    pjmedia_frame_ext *fx = (pjmedia_frame_ext*) snd_port->put_frm_buf;
-    void (*encoder)(pj_uint8_t*, const pj_int16_t*, pj_size_t) = NULL;
-
-    if (port==NULL)
-	return PJ_SUCCESS;
-
-    pj_bzero(fx, sizeof(*fx));
-    fx->base.buf = NULL;
-    fx->base.size = snd_port->frm_buf_size - sizeof(*fx);
-    fx->base.type = PJMEDIA_FRAME_TYPE_EXTENDED;
-    fx->base.timestamp.u32.lo = timestamp;
-
-    switch (snd_port->setting.format.id) {
-    case PJMEDIA_FORMAT_PCMA:
-	encoder = &pjmedia_alaw_encode;
-	break;
-    case PJMEDIA_FORMAT_PCMU:
-	encoder = &pjmedia_ulaw_encode;
-	break;
-    default:
-	PJ_LOG(1,(THIS_FILE, "Unsupported format %d", 
-		  snd_port->setting.format.id));
-	return PJ_SUCCESS;
-    }
-
-    (*encoder)((pj_uint8_t*)input, (pj_int16_t*)input, size >> 1);
-
-    pjmedia_frame_ext_append_subframe(fx, input, (size >> 1) << 3, 
-				      size >> 1);
-    pjmedia_port_put_frame(port, &fx->base);
-
-    return PJ_SUCCESS;
-#endif
 }
 
@@ -422,10 +184,11 @@
 				       pjmedia_snd_port *snd_port )
 {
-    pjmedia_snd_rec_cb snd_rec_cb;
-    pjmedia_snd_play_cb snd_play_cb;
+    pjmedia_aud_rec_cb snd_rec_cb;
+    pjmedia_aud_play_cb snd_play_cb;
+    pjmedia_aud_param param_copy;
     pj_status_t status;
 
     /* Check if sound has been started. */
-    if (snd_port->snd_stream != NULL)
+    if (snd_port->aud_stream != NULL)
 	return PJ_SUCCESS;
 
@@ -435,5 +198,36 @@
 		     PJ_EBUG);
 
-    if (snd_port->setting.format.id == PJMEDIA_FORMAT_L16) {
+    /* Get device caps */
+    if (snd_port->aud_param.dir & PJMEDIA_DIR_CAPTURE) {
+	pjmedia_aud_dev_info dev_info;
+
+	status = pjmedia_aud_dev_get_info(snd_port->aud_param.rec_id, 
+					  &dev_info);
+	if (status != PJ_SUCCESS)
+	    return status;
+
+	snd_port->aud_caps = dev_info.caps;
+    } else {
+	snd_port->aud_caps = 0;
+    }
+
+    /* Process EC settings */
+    pj_memcpy(&param_copy, &snd_port->aud_param, sizeof(param_copy));
+    if (param_copy.flags & PJMEDIA_AUD_DEV_CAP_EC) {
+	/* EC is wanted */
+	if (snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC) {
+	    /* Device supports EC */
+	    /* Nothing to do */
+	} else {
+	    /* Device doesn't support EC, remove EC settings from
+	     * device parameters
+	     */
+	    param_copy.flags &= ~(PJMEDIA_AUD_DEV_CAP_EC |
+				  PJMEDIA_AUD_DEV_CAP_EC_TAIL);
+	}
+    }
+
+    /* Use different callback if format is not PCM */
+    if (snd_port->aud_param.ext_fmt.id == PJMEDIA_FORMAT_L16) {
 	snd_rec_cb = &rec_cb;
 	snd_play_cb = &play_cb;
@@ -443,54 +237,13 @@
     }
 
-#if defined(PJMEDIA_SND_SUPPORT_OPEN2) && PJMEDIA_SND_SUPPORT_OPEN2!=0
-    status = pjmedia_snd_open2( snd_port->dir,
-				snd_port->rec_id, 
-				snd_port->play_id,
-				snd_port->clock_rate,
-				snd_port->channel_count,
-				snd_port->samples_per_frame,
-				snd_port->bits_per_sample,
-				snd_rec_cb,
-				snd_play_cb,
-				snd_port,
-				&snd_port->setting,
-				&snd_port->snd_stream);
-#else
-    status = pjmedia_snd_open(  snd_port->rec_id, 
-				snd_port->play_id,
-				snd_port->clock_rate,
-				snd_port->channel_count,
-				snd_port->samples_per_frame,
-				snd_port->bits_per_sample,
-				snd_rec_cb,
-				snd_play_cb,
-				snd_port,
-				&snd_port->snd_stream);
-#endif
+    /* Open the device */
+    status = pjmedia_aud_stream_create(&param_copy,
+				       snd_rec_cb,
+				       snd_play_cb,
+				       snd_port,
+				       &snd_port->aud_stream);
 
     if (status != PJ_SUCCESS)
 	return status;
-
-
-#ifdef SIMULATE_LOST_PCT
-    snd_port->setting.plc = PJ_TRUE;
-#endif
-
-    /* If we have player components, allocate buffer to save the last
-     * frame played to the speaker. The last frame is used for packet
-     * lost concealment (PLC) algorithm.
-     */
-    if ((snd_port->dir & PJMEDIA_DIR_PLAYBACK) &&
-	(snd_port->setting.plc)) 
-    {
-	status = pjmedia_plc_create(pool, snd_port->clock_rate, 
-				    snd_port->samples_per_frame * 
-					snd_port->channel_count,
-				    0, &snd_port->plc);
-	if (status != PJ_SUCCESS) {
-	    PJ_LOG(4,(THIS_FILE, "Unable to create PLC"));
-	    snd_port->plc = NULL;
-	}
-    }
 
     /* Inactivity limit before EC is suspended. */
@@ -499,9 +252,31 @@
 				  snd_port->samples_per_frame);
 
+    /* Create software EC if parameter specifies EC but device 
+     * doesn't support EC
+     */
+    if ((snd_port->aud_param.flags & PJMEDIA_AUD_DEV_CAP_EC) &&
+	(snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC)==0)
+    {
+	if ((snd_port->aud_param.flags & PJMEDIA_AUD_DEV_CAP_EC_TAIL)==0) {
+	    snd_port->aud_param.flags |= PJMEDIA_AUD_DEV_CAP_EC_TAIL;
+	    snd_port->aud_param.ec_tail_ms = AEC_TAIL;
+	    PJ_LOG(4,(THIS_FILE, "AEC tail is set to default %u ms",
+				 snd_port->aud_param.ec_tail_ms));
+	}
+	    
+	status = pjmedia_snd_port_set_ec(snd_port, pool, 
+					 snd_port->aud_param.ec_tail_ms, 0);
+	if (status != PJ_SUCCESS) {
+	    pjmedia_aud_stream_destroy(snd_port->aud_stream);
+	    snd_port->aud_stream = NULL;
+	    return status;
+	}
+    }
+
     /* Start sound stream. */
-    status = pjmedia_snd_stream_start(snd_port->snd_stream);
+    status = pjmedia_aud_stream_start(snd_port->aud_stream);
     if (status != PJ_SUCCESS) {
-	pjmedia_snd_stream_close(snd_port->snd_stream);
-	snd_port->snd_stream = NULL;
+	pjmedia_aud_stream_destroy(snd_port->aud_stream);
+	snd_port->aud_stream = NULL;
 	return status;
     }
@@ -518,8 +293,8 @@
 {
     /* Check if we have sound stream device. */
-    if (snd_port->snd_stream) {
-	pjmedia_snd_stream_stop(snd_port->snd_stream);
-	pjmedia_snd_stream_close(snd_port->snd_stream);
-	snd_port->snd_stream = NULL;
+    if (snd_port->aud_stream) {
+	pjmedia_aud_stream_stop(snd_port->aud_stream);
+	pjmedia_aud_stream_destroy(snd_port->aud_stream);
+	snd_port->aud_stream = NULL;
     }
 
@@ -547,46 +322,22 @@
 					     pjmedia_snd_port **p_port)
 {
-    pjmedia_snd_port *snd_port;
+    pjmedia_aud_param param;
+    pj_status_t status;
 
     PJ_UNUSED_ARG(options);
 
-    PJ_ASSERT_RETURN(pool && p_port, PJ_EINVAL);
-
-    snd_port = PJ_POOL_ZALLOC_T(pool, pjmedia_snd_port);
-    PJ_ASSERT_RETURN(snd_port, PJ_ENOMEM);
-
-    snd_port->rec_id = rec_id;
-    snd_port->play_id = play_id;
-    snd_port->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
-    snd_port->clock_rate = clock_rate;
-    snd_port->channel_count = channel_count;
-    snd_port->samples_per_frame = samples_per_frame;
-    snd_port->bits_per_sample = bits_per_sample;
-    
-#if PJMEDIA_SOUND_USE_DELAYBUF
-    do {
-	pj_status_t status;
-	unsigned ptime;
-    
-	ptime = samples_per_frame * 1000 / (clock_rate * channel_count);
-    
-	status = pjmedia_delay_buf_create(pool, "snd_buff", 
-					  clock_rate, samples_per_frame,
-					  channel_count,
-					  PJMEDIA_SOUND_BUFFER_COUNT * ptime,
-					  0, &snd_port->delay_buf);
-	PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
-    } while (0);
-#endif
-
-    *p_port = snd_port;
-
-
-    /* Start sound device immediately.
-     * If there's no port connected, the sound callback will return
-     * empty signal.
-     */
-    return start_sound_device( pool, snd_port );
-
+    status = pjmedia_aud_dev_default_param(rec_id, &param);
+    if (status != PJ_SUCCESS)
+	return status;
+
+    param.dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
+    param.rec_id = rec_id;
+    param.play_id = play_id;
+    param.clock_rate = clock_rate;
+    param.channel_count = channel_count;
+    param.samples_per_frame = samples_per_frame;
+    param.bits_per_sample = bits_per_sample;
+
+    return pjmedia_snd_port_create2(pool, &param, p_port);
 }
 
@@ -603,27 +354,21 @@
 						 pjmedia_snd_port **p_port)
 {
-    pjmedia_snd_port *snd_port;
+    pjmedia_aud_param param;
+    pj_status_t status;
 
     PJ_UNUSED_ARG(options);
 
-    PJ_ASSERT_RETURN(pool && p_port, PJ_EINVAL);
-
-    snd_port = PJ_POOL_ZALLOC_T(pool, pjmedia_snd_port);
-    PJ_ASSERT_RETURN(snd_port, PJ_ENOMEM);
-
-    snd_port->rec_id = dev_id;
-    snd_port->dir = PJMEDIA_DIR_CAPTURE;
-    snd_port->clock_rate = clock_rate;
-    snd_port->channel_count = channel_count;
-    snd_port->samples_per_frame = samples_per_frame;
-    snd_port->bits_per_sample = bits_per_sample;
-
-    *p_port = snd_port;
-
-    /* Start sound device immediately.
-     * If there's no port connected, the sound callback will return
-     * empty signal.
-     */
-    return start_sound_device( pool, snd_port );
+    status = pjmedia_aud_dev_default_param(dev_id, &param);
+    if (status != PJ_SUCCESS)
+	return status;
+
+    param.dir = PJMEDIA_DIR_CAPTURE;
+    param.rec_id = dev_id;
+    param.clock_rate = clock_rate;
+    param.channel_count = channel_count;
+    param.samples_per_frame = samples_per_frame;
+    param.bits_per_sample = bits_per_sample;
+
+    return pjmedia_snd_port_create2(pool, &param, p_port);
 }
 
@@ -641,21 +386,50 @@
 						    pjmedia_snd_port **p_port)
 {
+    pjmedia_aud_param param;
+    pj_status_t status;
+
+    PJ_UNUSED_ARG(options);
+
+    status = pjmedia_aud_dev_default_param(dev_id, &param);
+    if (status != PJ_SUCCESS)
+	return status;
+
+    param.dir = PJMEDIA_DIR_PLAYBACK;
+    param.play_id = dev_id;
+    param.clock_rate = clock_rate;
+    param.channel_count = channel_count;
+    param.samples_per_frame = samples_per_frame;
+    param.bits_per_sample = bits_per_sample;
+
+    return pjmedia_snd_port_create2(pool, &param, p_port);
+}
+
+
+/*
+ * Create sound port.
+ */
+PJ_DEF(pj_status_t) pjmedia_snd_port_create2(pj_pool_t *pool,
+					     const pjmedia_aud_param *prm,
+					     pjmedia_snd_port **p_port)
+{
     pjmedia_snd_port *snd_port;
 
-    PJ_UNUSED_ARG(options);
-
-    PJ_ASSERT_RETURN(pool && p_port, PJ_EINVAL);
+    PJ_ASSERT_RETURN(pool && prm && p_port, PJ_EINVAL);
 
     snd_port = PJ_POOL_ZALLOC_T(pool, pjmedia_snd_port);
     PJ_ASSERT_RETURN(snd_port, PJ_ENOMEM);
 
-    snd_port->play_id = dev_id;
-    snd_port->dir = PJMEDIA_DIR_PLAYBACK;
-    snd_port->clock_rate = clock_rate;
-    snd_port->channel_count = channel_count;
-    snd_port->samples_per_frame = samples_per_frame;
-    snd_port->bits_per_sample = bits_per_sample;
-
+    snd_port->dir = prm->dir;
+    snd_port->rec_id = prm->rec_id;
+    snd_port->play_id = prm->play_id;
+    snd_port->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
+    snd_port->clock_rate = prm->clock_rate;
+    snd_port->channel_count = prm->channel_count;
+    snd_port->samples_per_frame = prm->samples_per_frame;
+    snd_port->bits_per_sample = prm->bits_per_sample;
+    pj_memcpy(&snd_port->aud_param, prm, sizeof(*prm));
+    
     *p_port = snd_port;
+
 
     /* Start sound device immediately.
@@ -664,75 +438,4 @@
      */
     return start_sound_device( pool, snd_port );
-}
-
-
-/*
- * Create bidirectional port.
- */
-PJ_DEF(pj_status_t) pjmedia_snd_port_create2(pj_pool_t *pool,
-					     pjmedia_dir dir,
-					     int rec_id,
-					     int play_id,
-					     unsigned clock_rate,
-					     unsigned channel_count,
-					     unsigned samples_per_frame,
-					     unsigned bits_per_sample,
-					     const pjmedia_snd_setting *setting,
-					     pjmedia_snd_port **p_port)
-{
-    pjmedia_snd_port *snd_port;
-
-    PJ_ASSERT_RETURN(pool && p_port, PJ_EINVAL);
-
-    snd_port = PJ_POOL_ZALLOC_T(pool, pjmedia_snd_port);
-    PJ_ASSERT_RETURN(snd_port, PJ_ENOMEM);
-
-    snd_port->dir = dir;
-    snd_port->rec_id = rec_id;
-    snd_port->play_id = play_id;
-    snd_port->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
-    snd_port->clock_rate = clock_rate;
-    snd_port->channel_count = channel_count;
-    snd_port->samples_per_frame = samples_per_frame;
-    snd_port->bits_per_sample = bits_per_sample;
-    pj_memcpy(&snd_port->setting, setting, sizeof(*setting));
-    
-#if PJMEDIA_SOUND_USE_DELAYBUF
-    if (snd_port->setting.format.u32 == PJMEDIA_FORMAT_L16) {
-	pj_status_t status;
-	unsigned ptime;
-    
-	ptime = samples_per_frame * 1000 / (clock_rate * channel_count);
-    
-	status = pjmedia_delay_buf_create(pool, "snd_buff", 
-					  clock_rate, samples_per_frame,
-					  channel_count,
-					  PJMEDIA_SOUND_BUFFER_COUNT * ptime,
-					  0, &snd_port->delay_buf);
-	PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
-    }
-#endif
-
-#if !defined(PJMEDIA_SND_SUPPORT_OPEN2) || PJMEDIA_SND_SUPPORT_OPEN2==0
-    /* For devices that doesn't support open2(), enable simulation */
-    if (snd_port->setting.format.id != PJMEDIA_FORMAT_L16) {
-	snd_port->frm_buf_size = sizeof(pjmedia_frame_ext) + 
-				 (samples_per_frame << 1) +
-				 16 * sizeof(pjmedia_frame_ext_subframe);
-	snd_port->put_frm_buf = (pj_uint8_t*)
-				pj_pool_alloc(pool, snd_port->frm_buf_size);
-	snd_port->get_frm_buf = (pj_uint8_t*)
-				pj_pool_alloc(pool, snd_port->frm_buf_size);
-    }
-#endif
-
-    *p_port = snd_port;
-
-
-    /* Start sound device immediately.
-     * If there's no port connected, the sound callback will return
-     * empty signal.
-     */
-    return start_sound_device( pool, snd_port );
 
 }
@@ -753,14 +456,14 @@
  * Retrieve the sound stream associated by this sound device port.
  */
-PJ_DEF(pjmedia_snd_stream*) pjmedia_snd_port_get_snd_stream(
+PJ_DEF(pjmedia_aud_stream*) pjmedia_snd_port_get_snd_stream(
 						pjmedia_snd_port *snd_port)
 {
     PJ_ASSERT_RETURN(snd_port, NULL);
-    return snd_port->snd_stream;
-}
-
-
-/*
- * Enable AEC
+    return snd_port->aud_stream;
+}
+
+
+/*
+ * Change EC settings.
  */
 PJ_DEF(pj_status_t) pjmedia_snd_port_set_ec( pjmedia_snd_port *snd_port,
@@ -769,5 +472,5 @@
 					     unsigned options)
 {
-    pjmedia_snd_stream_info si;
+    pjmedia_aud_param prm;
     pj_status_t status;
 
@@ -777,41 +480,98 @@
 		     PJ_EINVALIDOP);
 
-    /* Sound port must have 16bits per sample */
-    PJ_ASSERT_RETURN(snd_port->bits_per_sample == 16,
-		     PJ_EINVALIDOP);
-
-    /* Destroy AEC */
-    if (snd_port->ec_state) {
-	pjmedia_echo_destroy(snd_port->ec_state);
-	snd_port->ec_state = NULL;
-    }
-
-    snd_port->aec_tail_len = tail_ms;
-
-    if (tail_ms != 0) {
-	unsigned delay_ms;
-
-	status = pjmedia_snd_stream_get_info(snd_port->snd_stream, &si);
+    /* Determine whether we use device or software EC */
+    if (snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC) {
+	/* We use device EC */
+	pj_bool_t ec_enabled;
+
+	/* Query EC status */
+	status = pjmedia_aud_stream_get_cap(snd_port->aud_stream,
+					    PJMEDIA_AUD_DEV_CAP_EC,
+					    &ec_enabled);
 	if (status != PJ_SUCCESS)
-	    si.rec_latency = si.play_latency = 0;
-
-	//No need to add input latency in the latency calculation,
-	//since actual input latency should be zero.
-	//delay_ms = (si.rec_latency + si.play_latency) * 1000 /
-	//	   snd_port->clock_rate;
-	delay_ms = si.play_latency * 1000 / snd_port->clock_rate;
-	status = pjmedia_echo_create2(pool, snd_port->clock_rate, 
-				      snd_port->channel_count,
-				      snd_port->samples_per_frame, 
-				      tail_ms, delay_ms,
-				      options, &snd_port->ec_state);
-	if (status != PJ_SUCCESS)
+	    return status;
+
+	if (tail_ms != 0) {
+	    /* Change EC setting */
+
+	    if (!ec_enabled) {
+		/* Enable EC first */
+		pj_bool_t value = PJ_TRUE;
+		status = pjmedia_aud_stream_set_cap(snd_port->aud_stream, 
+						    PJMEDIA_AUD_DEV_CAP_EC,
+						    &value);
+		if (status != PJ_SUCCESS)
+		    return status;
+	    }
+
+	    if ((snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC_TAIL)==0) {
+		/* Device does not support setting EC tail */
+		return PJMEDIA_EAUD_INVCAP;
+	    }
+
+	    return pjmedia_aud_stream_set_cap(snd_port->aud_stream,
+					      PJMEDIA_AUD_DEV_CAP_EC_TAIL,
+					      &tail_ms);
+
+	} else if (ec_enabled) {
+	    /* Disable EC */
+	    pj_bool_t value = PJ_FALSE;
+	    return pjmedia_aud_stream_set_cap(snd_port->aud_stream, 
+					      PJMEDIA_AUD_DEV_CAP_EC,
+					      &value);
+	} else {
+	    /* Request to disable EC but EC has been disabled */
+	    /* Do nothing */
+	    return PJ_SUCCESS;
+	}
+
+    } else {
+	/* We use software EC */
+	/* Sound port must have 16bits per sample */
+	PJ_ASSERT_RETURN(snd_port->bits_per_sample == 16,
+			 PJ_EINVALIDOP);
+
+	/* Check if there is change in parameters */
+	if (tail_ms==snd_port->ec_tail_len && options==snd_port->ec_options) {
+	    PJ_LOG(5,(THIS_FILE, "pjmedia_snd_port_set_ec() ignored, no "
+				 "change in settings"));
+	    return PJ_SUCCESS;
+	}
+
+	/* Destroy AEC */
+	if (snd_port->ec_state) {
+	    pjmedia_echo_destroy(snd_port->ec_state);
 	    snd_port->ec_state = NULL;
-	else
-	    snd_port->ec_suspended = PJ_FALSE;
-    } else {
-	PJ_LOG(4,(THIS_FILE, "Echo canceller is now disabled in the "
-			     "sound port"));
-	status = PJ_SUCCESS;
+	}
+
+	snd_port->ec_options = options;
+	snd_port->ec_tail_len = tail_ms;
+
+	if (tail_ms != 0) {
+	    unsigned delay_ms;
+
+	    status = pjmedia_aud_stream_get_param(snd_port->aud_stream, &prm);
+	    if (status != PJ_SUCCESS)
+		prm.input_latency_ms = prm.output_latency_ms = 0;
+
+	    //No need to add input latency in the latency calculation,
+	    //since actual input latency should be zero.
+	    //delay_ms = (si.rec_latency + si.play_latency) * 1000 /
+	    //	   snd_port->clock_rate;
+	    delay_ms = prm.output_latency_ms;
+	    status = pjmedia_echo_create2(pool, snd_port->clock_rate, 
+					  snd_port->channel_count,
+					  snd_port->samples_per_frame, 
+					  tail_ms, delay_ms,
+					  options, &snd_port->ec_state);
+	    if (status != PJ_SUCCESS)
+		snd_port->ec_state = NULL;
+	    else
+		snd_port->ec_suspended = PJ_FALSE;
+	} else {
+	    PJ_LOG(4,(THIS_FILE, "Echo canceller is now disabled in the "
+				 "sound port"));
+	    status = PJ_SUCCESS;
+	}
     }
 
@@ -825,8 +585,38 @@
 {
     PJ_ASSERT_RETURN(snd_port && p_length, PJ_EINVAL);
-    *p_length =  snd_port->ec_state ? snd_port->aec_tail_len : 0;
-    return PJ_SUCCESS;
-}
-
+
+    /* Determine whether we use device or software EC */
+    if (snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC) {
+	/* We use device EC */
+	pj_bool_t ec_enabled;
+	pj_status_t status;
+
+	/* Query EC status */
+	status = pjmedia_aud_stream_get_cap(snd_port->aud_stream,
+					    PJMEDIA_AUD_DEV_CAP_EC,
+					    &ec_enabled);
+	if (status != PJ_SUCCESS)
+	    return status;
+
+	if (!ec_enabled) {
+	    *p_length = 0;
+	} else if (snd_port->aud_caps & PJMEDIA_AUD_DEV_CAP_EC_TAIL) {
+	    /* Get device EC tail */
+	    status = pjmedia_aud_stream_get_cap(snd_port->aud_stream,
+						PJMEDIA_AUD_DEV_CAP_EC_TAIL,
+						p_length);
+	    if (status != PJ_SUCCESS)
+		return status;
+	} else {
+	    /* Just use default */
+	    *p_length = AEC_TAIL;
+	}
+
+    } else {
+	/* We use software EC */
+	*p_length =  snd_port->ec_state ? snd_port->ec_tail_len : 0;
+    }
+    return PJ_SUCCESS;
+}
 
 
