Index: /pjproject/branches/projects/aps-direct/build.symbian/bld.inf
===================================================================
--- /pjproject/branches/projects/aps-direct/build.symbian/bld.inf	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/build.symbian/bld.inf	(revision 2438)
@@ -21,4 +21,5 @@
 libgsmcodec.mmp
 libspeexcodec.mmp
+libpassthroughcodec.mmp
 
 /* Sound device impl */
Index: /pjproject/branches/projects/aps-direct/build.symbian/libpassthroughcodec.mmp
===================================================================
--- /pjproject/branches/projects/aps-direct/build.symbian/libpassthroughcodec.mmp	(revision 2438)
+++ /pjproject/branches/projects/aps-direct/build.symbian/libpassthroughcodec.mmp	(revision 2438)
@@ -0,0 +1,26 @@
+TARGET		libpassthroughcodec.lib
+TARGETTYPE	lib
+
+MACRO		HAVE_CONFIG_H
+MACRO		PJ_M_I386=1
+MACRO		PJ_SYMBIAN=1
+
+//
+// GCCE optimization setting
+//
+OPTION		GCCE -O2 -fno-unit-at-a-time
+
+//
+// Passthrough codecs wrapper for pjmedia-codec
+//
+SOURCEPATH 	..\pjmedia\src\pjmedia-codec
+SOURCE		passthrough.c
+
+//
+// Header files
+//
+SYSTEMINCLUDE	..\pjmedia\include
+SYSTEMINCLUDE	..\pjlib\include 
+
+SYSTEMINCLUDE	\epoc32\include 
+SYSTEMINCLUDE	\epoc32\include\libc
Index: /pjproject/branches/projects/aps-direct/build.symbian/symbian_ua.mmp
===================================================================
--- /pjproject/branches/projects/aps-direct/build.symbian/symbian_ua.mmp	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/build.symbian/symbian_ua.mmp	(revision 2438)
@@ -35,5 +35,5 @@
 STATICLIBRARY		pjnath.lib pjlib_util.lib pjlib.lib
 STATICLIBRARY		libsrtp.lib
-STATICLIBRARY		libgsmcodec.lib libspeexcodec.lib
+STATICLIBRARY		libgsmcodec.lib libspeexcodec.lib libpassthroughcodec.lib
 STATICLIBRARY		symbian_audio.lib
 
Index: /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia-codec.h
===================================================================
--- /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia-codec.h	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia-codec.h	(revision 2438)
@@ -32,4 +32,5 @@
 #include <pjmedia-codec/g722.h>
 #include <pjmedia-codec/ipp_codecs.h>
+#include <pjmedia-codec/passthrough.h>
 
 
Index: /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia-codec/config.h
===================================================================
--- /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia-codec/config.h	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia-codec/config.h	(revision 2438)
@@ -218,8 +218,10 @@
 #ifndef PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMU
 #   define PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMU	1
-#endif
-
-#ifndef PJMEDIA_HAS_PASSTHROUGH_CODEC_G729
+#   undef PJMEDIA_HAS_G711_CODEC
+#endif
+
+#ifndef PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMA
 #   define PJMEDIA_HAS_PASSTHROUGH_CODEC_PCMA	1
+#   undef PJMEDIA_HAS_G711_CODEC
 #endif
 
Index: /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/port.h
===================================================================
--- /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/port.h	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/port.h	(revision 2438)
@@ -360,4 +360,40 @@
 	
 /**
+ * Pop out first n subframes from #pjmedia_frame_ext.
+ *
+ * @param frm		    The #pjmedia_frame_ext.
+ * @param n		    Number of first subframes to be popped out.
+ *
+ * @return		    PJ_SUCCESS, or PJ_ENOTFOUND if frame is empty.
+ */
+PJ_INLINE(pj_status_t) 
+	  pjmedia_frame_ext_pop_subframes(pjmedia_frame_ext *frm, unsigned n)
+{
+    pjmedia_frame_ext_subframe *sf;
+    pj_uint8_t *move_src;
+    unsigned move_len;
+
+    if (frm->subframe_cnt <= n) {
+	frm->subframe_cnt = 0;
+	frm->samples_cnt = 0;
+	return PJ_SUCCESS;
+    }
+
+    move_src = (pj_uint8_t*)pjmedia_frame_ext_get_subframe(frm, n);
+    sf = pjmedia_frame_ext_get_subframe(frm, frm->subframe_cnt-1);
+    move_len = (pj_uint8_t*)sf - move_src + sf->bitlen/8;
+    if (sf->bitlen % 8 != 0)
+	++move_len;
+    pj_memmove((pj_uint8_t*)frm+sizeof(pjmedia_frame_ext), 
+	       move_src, move_len);
+	    
+    frm->samples_cnt = (pj_uint16_t)
+		   (frm->samples_cnt - n*frm->samples_cnt/frm->subframe_cnt);
+    frm->subframe_cnt = (pj_uint16_t) (frm->subframe_cnt - n);
+
+    return PJ_SUCCESS;
+}
+	
+/**
  * Port interface.
  */
Index: /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/sound.h
===================================================================
--- /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/sound.h	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/sound.h	(revision 2438)
@@ -92,4 +92,20 @@
     unsigned	play_latency;	    /**< Playback latency, in samples.	    */
 } pjmedia_snd_stream_info;
+
+
+/** 
+ * Stream setting for opening sound device with non-PCM data.
+ */
+typedef struct pjmedia_snd_setting
+{
+    pjmedia_fourcc	format;	  /**< Format (FourCC ID).	    */ 
+    pj_uint32_t		bitrate;  /**< Bitrate (bps).		    */
+    pj_uint32_t		mode;	  /**< Mode, e.g: iLBC format has
+				       20ms or 30ms frame size.	    */
+    pj_bool_t		plc;	  /**< PLC enabled/disabled.	    */
+    pj_bool_t		vad;	  /**< VAD enabled/disabled.	    */
+    pj_bool_t		cng;	  /**< CNG enabled/disabled.	    */
+    pj_bool_t		loudspk;  /**< Audio routed to loudspeaker. */
+} pjmedia_snd_setting;
 
 
@@ -274,4 +290,44 @@
 
 /**
+ * Create sound stream for capturing audio and/or audio playback, from the 
+ * same device. This also allows opening sound stream with extended settings, 
+ * e.g: stream format, see #pjmedia_snd_setting.
+ *
+ * @param dir		    Sound stream direction.
+ * @param rec_id	    Device index for recorder/capture stream, or
+ *			    -1 to use the first capable device.
+ * @param play_id	    Device index for playback stream, or -1 to use 
+ *			    the first capable device.
+ * @param clock_rate	    Sound device's clock rate to set.
+ * @param channel_count	    Set number of channels, 1 for mono, or 2 for
+ *			    stereo. The channel count determines the format
+ *			    of the frame.
+ * @param samples_per_frame Number of samples per frame.
+ * @param bits_per_sample   Set the number of bits per sample. The normal 
+ *			    value for this parameter is 16 bits per sample.
+ * @param rec_cb	    Callback to handle captured audio samples.
+ * @param play_cb	    Callback to be called when the sound player needs
+ *			    more audio samples to play.
+ * @param user_data	    User data to be associated with the stream.
+ * @param setting	    Sound device extended setting.
+ * @param p_snd_strm	    Pointer to receive the stream instance.
+ *
+ * @return		    PJ_SUCCESS on success.
+ */
+PJ_DECL(pj_status_t) pjmedia_snd_open2( pjmedia_dir dir,
+				        int rec_id,
+				        int play_id,
+				        unsigned clock_rate,
+				        unsigned channel_count,
+				        unsigned samples_per_frame,
+				        unsigned bits_per_sample,
+				        pjmedia_snd_rec_cb rec_cb,
+				        pjmedia_snd_play_cb play_cb,
+				        void *user_data,
+					const pjmedia_snd_setting *setting,
+				        pjmedia_snd_stream **p_snd_strm);
+
+
+/**
  * Get information about live stream.
  *
Index: /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/sound_port.h
===================================================================
--- /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/sound_port.h	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/sound_port.h	(revision 2438)
@@ -160,5 +160,42 @@
 						    unsigned options,
 						    pjmedia_snd_port **p_port);
-					      
+
+
+/**
+ * Create unidirectional or bidirectional sound port. This also allows
+ * creating sound port with extended settings, e.g: audio format, see 
+ * #pjmedia_snd_setting.
+ *
+ * @param pool		    Pool to allocate sound port structure.
+ * @param dir		    Sound device direction.
+ * @param rec_id	    Device index for recorder/capture stream, or
+ *			    -1 to use the first capable device.
+ * @param play_id	    Device index for playback stream, or -1 to use 
+ *			    the first capable device.
+ * @param clock_rate	    Sound device's clock rate to set.
+ * @param channel_count	    Set number of channels, 1 for mono, or 2 for
+ *			    stereo. The channel count determines the format
+ *			    of the frame.
+ * @param samples_per_frame Number of samples per frame.
+ * @param bits_per_sample   Set the number of bits per sample. The normal 
+ *			    value for this parameter is 16 bits per sample.
+ * @param setting	    Sound device extended settings, see 
+ *			    #pjmedia_snd_setting.
+ * @param p_port	    Pointer to receive the sound device port instance.
+ *
+ * @return		    PJ_SUCCESS on success, or the appropriate error
+ *			    code.
+ */
+PJ_DECL(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);
+
 
 /**
Index: /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/symbian_sound_aps.h
===================================================================
--- /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/symbian_sound_aps.h	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/symbian_sound_aps.h	(revision 2438)
@@ -54,6 +54,5 @@
  *
  * @param stream	The sound device stream, the stream should be started 
- *			before calling this function. This param can be NULL
- *			to set the behaviour of next opened stream.
+ *			before calling this function.
  * @param active	Specify PJ_TRUE to activate loudspeaker, and PJ_FALSE
  *			otherwise.
@@ -65,16 +64,4 @@
 						pj_bool_t active);
 
-
-/**
- * Set a codec and its settings to be used on the next sound device session.
- *
- * @param setting	APS sound device setting, see @pjmedia_snd_aps_setting.
- *
- * @return		PJ_SUCCESS on success.
- */
-PJ_DECL(pj_status_t) pjmedia_snd_aps_modify_setting(
-				    const pjmedia_snd_aps_setting *setting);
-
-
 PJ_END_DECL
 
Index: /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/types.h
===================================================================
--- /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/types.h	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/pjmedia/include/pjmedia/types.h	(revision 2438)
@@ -191,5 +191,5 @@
  * FourCC packing macro.
  */
-#define PJMEDIA_FOURCC_PACK(C1, C2, C3, C4) ( C1<<24 | C2<<16 | C3<<8 | C4 )
+#define PJMEDIA_FOURCC_PACK(C1, C2, C3, C4) ( C4<<24 | C3<<16 | C2<<8 | C1 )
 
 /**
Index: /pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia-codec/passthrough.c
===================================================================
--- /pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia-codec/passthrough.c	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia-codec/passthrough.c	(revision 2438)
@@ -460,11 +460,11 @@
 				    codec_desc[i].channel_count / 
 				    codec_desc[i].clock_rate);
-	    attr->info.format.u32 = codec_desc[i].format.u32;
+	    attr->info.format = codec_desc[i].format;
 
 	    /* Default flags. */
 	    attr->setting.frm_per_pkt = codec_desc[i].frm_per_pkt;
-	    attr->setting.plc = 1;
+	    attr->setting.plc = 0;
 	    attr->setting.penh= 0;
-	    attr->setting.vad = 1;
+	    attr->setting.vad = 0;
 	    attr->setting.cng = attr->setting.vad;
 	    attr->setting.dec_fmtp = codec_desc[i].dec_fmtp;
@@ -720,5 +720,5 @@
 	frames[count].timestamp.u64 = ts->u64 + count*desc->samples_per_frame;
 
-	pkt = ((char*)pkt) + codec_data->avg_frame_size;
+	pkt = (pj_uint8_t*)pkt + codec_data->avg_frame_size;
 	pkt_size -= codec_data->avg_frame_size;
 
@@ -800,6 +800,5 @@
     pjmedia_frame_ext *output_ = (pjmedia_frame_ext*) output;
 
-    /* Check if input is formatted in pjmedia_frame */
-    pj_assert(input && input->type == PJMEDIA_FRAME_TYPE_AUDIO);
+    pj_assert(input && input->size > 0);
 
 #if PJMEDIA_HAS_PASSTHROUGH_CODEC_AMR
@@ -818,18 +817,14 @@
     }
 #endif
-
-    pj_bzero(output_, sizeof(pjmedia_frame_ext));
-    output_->base.type = PJMEDIA_FRAME_TYPE_EXTENDED;
-    
-    if (input && input->size > 0) {
-	PJ_ASSERT_RETURN(output_buf_len >= sizeof(pjmedia_frame_ext) +
-					   sizeof(pjmedia_frame_ext_subframe) +
-					   input->size,
-			 PJMEDIA_CODEC_EFRMTOOSHORT);
-
-	pjmedia_frame_ext_append_subframe(output_, input->buf, 
-					  (pj_uint16_t)(input->size << 3),
-					  (pj_uint16_t)desc->samples_per_frame);
-    }
+    /*
+    PJ_ASSERT_RETURN(output_buf_len >= sizeof(pjmedia_frame_ext) +
+				       sizeof(pjmedia_frame_ext_subframe) +
+				       input->size,
+		     PJMEDIA_CODEC_EFRMTOOSHORT);
+     */
+
+    pjmedia_frame_ext_append_subframe(output_, input->buf, 
+				      (pj_uint16_t)(input->size << 3),
+				      (pj_uint16_t)desc->samples_per_frame);
 
     return PJ_SUCCESS;
@@ -843,5 +838,14 @@
 				  struct pjmedia_frame *output)
 {
-    return codec_decode(codec, NULL, output_buf_len, output);
+    codec_private_t *codec_data = (codec_private_t*) codec->codec_data;
+    struct codec_desc *desc = &codec_desc[codec_data->codec_idx];
+    pjmedia_frame_ext *output_ = (pjmedia_frame_ext*) output;
+
+    PJ_UNUSED_ARG(output_buf_len);
+
+    pjmedia_frame_ext_append_subframe(output_, NULL, 0,
+				      (pj_uint16_t)desc->samples_per_frame);
+
+    return PJ_SUCCESS;
 }
 
Index: /pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia/conf_switch.c
===================================================================
--- /pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia/conf_switch.c	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia/conf_switch.c	(revision 2438)
@@ -997,12 +997,13 @@
 					   (pjmedia_frame*)f_dst);
 
-		    /* Update TX timestamp. */
-		    pj_add_timestamp32(&cport_dst->ts_tx, 
-				       cport_dst->samples_per_frame);
-
 		    /* Reset TX buffer. */
 		    f_dst->subframe_cnt = 0;
 		    f_dst->samples_cnt = 0;
 		}
+
+		/* Update TX timestamp. */
+		pj_add_timestamp32(&cport_dst->ts_tx, 
+				   cport_dst->samples_per_frame);
+
 	    }
 	}
@@ -1037,11 +1038,11 @@
 		    pjmedia_port_put_frame(cport_dst->port, frm_dst);
 
-		    /* Update TX timestamp. */
-		    pj_add_timestamp32(&cport_dst->ts_tx, 
-				       cport_dst->samples_per_frame);
-		 
 		    /* Reset TX buffer. */
 		    frm_dst->size = 0;
 		}
+
+		/* Update TX timestamp. */
+		pj_add_timestamp32(&cport_dst->ts_tx, 
+				   cport_dst->samples_per_frame);
 	    }
 	}
@@ -1063,13 +1064,14 @@
 		frm_dst->type = PJMEDIA_FRAME_TYPE_AUDIO;
 		frm_dst->size = cport_dst->samples_per_frame << 1;
-		if (cport_dst->port)
+		if (cport_dst->port) {
 		    pjmedia_port_put_frame(cport_dst->port, frm_dst);
+
+		    /* Reset TX buffer. */
+		    frm_dst->size = 0;
+		}
 
 		/* Update TX timestamp. */
 		pj_add_timestamp32(&cport_dst->ts_tx, 
 				   cport_dst->samples_per_frame);
-
-		/* Reset TX buffer. */
-		frm_dst->size = 0;
 	    }
 	} else {
@@ -1080,15 +1082,16 @@
 		pjmedia_frame_ext_append_subframe(f_dst, NULL, 0, (pj_uint16_t)
 						  (cport_dst->samples_per_frame-
-						  f_dst->samples_cnt));
-		if (cport_dst->port)
+						   f_dst->samples_cnt));
+		if (cport_dst->port) {
 		    pjmedia_port_put_frame(cport_dst->port, frm_dst);
+
+		    /* Reset TX buffer. */
+		    f_dst->subframe_cnt = 0;
+		    f_dst->samples_cnt = 0;
+		}
 
 		/* Update TX timestamp. */
 		pj_add_timestamp32(&cport_dst->ts_tx, 
 				   cport_dst->samples_per_frame);
-
-		/* Reset TX buffer. */
-		f_dst->subframe_cnt = 0;
-		f_dst->samples_cnt = 0;
 	    }
 	}
@@ -1096,11 +1099,12 @@
 	/* Synchronize clock. */
 	while (pj_cmp_timestamp(&cport_dst->ts_clock, 
-				&cport_dst->ts_tx) >= 0)
+				&cport_dst->ts_tx) > 0)
 	{
-	    if (cport_dst->port) {
-		frm_dst->type = PJMEDIA_FRAME_TYPE_NONE;
-		frm_dst->timestamp = cport_dst->ts_tx;
+	    frm_dst->type = PJMEDIA_FRAME_TYPE_NONE;
+	    frm_dst->timestamp = cport_dst->ts_tx;
+	    if (cport_dst->port)
 		pjmedia_port_put_frame(cport_dst->port, frm_dst);
-	    }
+
+	    /* Update TX timestamp. */
 	    pj_add_timestamp32(&cport_dst->ts_tx, cport_dst->samples_per_frame);
 	}
@@ -1235,5 +1239,5 @@
 	    cport->tx_level = 0;
 
-	    while (pj_cmp_timestamp(&cport->ts_clock, &cport->ts_tx) >= 0)
+	    while (pj_cmp_timestamp(&cport->ts_clock, &cport->ts_tx) > 0)
 	    {
 		if (cport->tx_setting == PJMEDIA_PORT_ENABLE) {
@@ -1265,4 +1269,10 @@
 	    pj_uint16_t samples_per_subframe;
 	    
+	    if (f_src_->samples_cnt < this_cport->samples_per_frame) {
+		pj_bzero(this_cport->tx_buf, sizeof(pjmedia_frame_ext));
+		frame->type = PJMEDIA_FRAME_TYPE_NONE;
+		break;
+	    }
+
 	    f_dst->samples_cnt = 0;
 	    f_dst->subframe_cnt = 0;
@@ -1270,6 +1280,8 @@
 	    samples_per_subframe = f_src_->samples_cnt / f_src_->subframe_cnt;
 
+
 	    while (f_dst->samples_cnt < this_cport->samples_per_frame) {
 		sf = pjmedia_frame_ext_get_subframe(f_src_, i++);
+		pj_assert(sf);
 		pjmedia_frame_ext_append_subframe(f_dst, sf->data, sf->bitlen,
 						  samples_per_subframe);
@@ -1277,22 +1289,13 @@
 
 	    /* Shift left TX buffer. */
-	    sf = pjmedia_frame_ext_get_subframe(f_src_, i);
-	    if (sf) {
-		pjmedia_frame_ext_subframe *sf_end;
-		unsigned len;
-
-		sf_end = pjmedia_frame_ext_get_subframe(f_src_, 
-						    f_src_->subframe_cnt -1);
-		len = (pj_uint8_t*)sf_end - (pj_uint8_t*)sf + sf_end->bitlen/8;
-		if (sf_end->bitlen % 8 != 0)
-		    ++len;
-		pj_memmove(this_cport->tx_buf + sizeof(pjmedia_frame_ext), sf,
-			   len);
+	    pjmedia_frame_ext_pop_subframes(f_src_, i);
+
+	} else if (f_src->type == PJMEDIA_FRAME_TYPE_AUDIO) {
+	    if ((f_src->size>>1) < this_cport->samples_per_frame) {
+		pj_bzero(this_cport->tx_buf, sizeof(pjmedia_frame_ext));
+		frame->type = PJMEDIA_FRAME_TYPE_NONE;
+		break;
 	    }
-	    f_src_->samples_cnt = f_src_->samples_cnt - 
-				  (pj_uint16_t)(i * samples_per_subframe);
-	    f_src_->subframe_cnt = f_src_->subframe_cnt - (pj_uint16_t)i;
-
-	} else if (f_src->type == PJMEDIA_FRAME_TYPE_AUDIO) {
+
 	    pjmedia_copy_samples((pj_int16_t*)frame->buf, 
 				 (pj_int16_t*)f_src->buf, 
@@ -1307,4 +1310,8 @@
 				     this_cport->samples_per_frame,
 				     f_src->size >> 1);
+	} else { /* PJMEDIA_FRAME_TYPE_NONE */
+	    /* Reset TX buffer */
+	    pj_bzero(this_cport->tx_buf, sizeof(pjmedia_frame_ext));
+	    frame->type = PJMEDIA_FRAME_TYPE_NONE;
 	}
     } while (0);
@@ -1326,9 +1333,4 @@
     unsigned j;
     pj_int32_t level;
-
-    /* Check for correct size. */
-    PJ_ASSERT_RETURN( f->size == conf->samples_per_frame *
-				 conf->bits_per_sample / 8,
-		      PJMEDIA_ENCSAMPLESPFRAME);
 
     pj_add_timestamp32(&cport->ts_rx, cport->samples_per_frame);
Index: /pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia/sound_port.c
===================================================================
--- /pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia/sound_port.c	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia/sound_port.c	(revision 2438)
@@ -36,12 +36,4 @@
 //#define TEST_OVERFLOW_UNDERFLOW
 
-enum
-{
-    PJMEDIA_PLC_ENABLED	    = 1,
-};
-
-//#define DEFAULT_OPTIONS	PJMEDIA_PLC_ENABLED
-#define DEFAULT_OPTIONS	    0
-
 
 struct pjmedia_snd_port
@@ -52,5 +44,4 @@
     pjmedia_dir		 dir;
     pjmedia_port	*port;
-    unsigned		 options;
 
     pjmedia_echo_state	*ec_state;
@@ -67,4 +58,5 @@
     unsigned		 samples_per_frame;
     unsigned		 bits_per_sample;
+    pjmedia_snd_setting  setting;
 
 #if PJMEDIA_SOUND_USE_DELAYBUF
@@ -245,4 +237,70 @@
 
 /*
+ * The callback called by sound player when it needs more samples to be
+ * 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)
+{
+    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);
+
+    /* We're risking accessing the port without holding any mutex.
+     * It's possible that port is disconnected then destroyed while
+     * we're trying to access it.
+     * But in the name of performance, we'll try this approach until
+     * someone complains when it crashes.
+     */
+    port = snd_port->port;
+    if (port == NULL) {
+	frame->type = PJMEDIA_FRAME_TYPE_NONE;
+	return PJ_SUCCESS;
+    }
+
+    status = pjmedia_port_get_frame(port, frame);
+
+    return status;
+}
+
+
+/*
+ * The callback called by sound recorder when it has finished capturing a
+ * 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)
+{
+    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);
+
+    /* We're risking accessing the port without holding any mutex.
+     * It's possible that port is disconnected then destroyed while
+     * we're trying to access it.
+     * But in the name of performance, we'll try this approach until
+     * someone complains when it crashes.
+     */
+    port = snd_port->port;
+    if (port == NULL)
+	return PJ_SUCCESS;
+
+    pjmedia_port_put_frame(port, frame);
+
+    return PJ_SUCCESS;
+}
+
+/*
  * Start the sound stream.
  * This may be called even when the sound stream has already been started.
@@ -251,4 +309,6 @@
 				       pjmedia_snd_port *snd_port )
 {
+    pjmedia_snd_rec_cb snd_rec_cb;
+    pjmedia_snd_play_cb snd_play_cb;
     pj_status_t status;
 
@@ -257,40 +317,31 @@
 	return PJ_SUCCESS;
 
-    /* Open sound stream. */
-    if (snd_port->dir == PJMEDIA_DIR_CAPTURE) {
-	status = pjmedia_snd_open_rec( snd_port->rec_id, 
-				       snd_port->clock_rate,
-				       snd_port->channel_count,
-				       snd_port->samples_per_frame,
-				       snd_port->bits_per_sample,
-				       &rec_cb,
-				       snd_port,
-				       &snd_port->snd_stream);
-
-    } else if (snd_port->dir == PJMEDIA_DIR_PLAYBACK) {
-	status = pjmedia_snd_open_player( snd_port->play_id, 
-					  snd_port->clock_rate,
-					  snd_port->channel_count,
-					  snd_port->samples_per_frame,
-					  snd_port->bits_per_sample,
-					  &play_cb,
-					  snd_port,
-					  &snd_port->snd_stream);
-
-    } else if (snd_port->dir == PJMEDIA_DIR_CAPTURE_PLAYBACK) {
-	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,
-				   &rec_cb,
-				   &play_cb,
-				   snd_port,
-				   &snd_port->snd_stream);
+    PJ_ASSERT_RETURN(snd_port->dir == PJMEDIA_DIR_CAPTURE ||
+		     snd_port->dir == PJMEDIA_DIR_PLAYBACK ||
+		     snd_port->dir == PJMEDIA_DIR_CAPTURE_PLAYBACK,
+		     PJ_EBUG);
+
+    if (snd_port->setting.format.u32 == 0 ||
+	snd_port->setting.format.u32 == PJMEDIA_FOURCC_L16)
+    {
+	snd_rec_cb = &rec_cb;
+	snd_play_cb = &play_cb;
     } else {
-	pj_assert(!"Invalid dir");
-	status = PJ_EBUG;
-    }
+	snd_rec_cb = &rec_cb_ext;
+	snd_play_cb = &play_cb_ext;
+    }
+
+    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);
 
     if (status != PJ_SUCCESS)
@@ -299,5 +350,5 @@
 
 #ifdef SIMULATE_LOST_PCT
-    snd_port->options |= PJMEDIA_PLC_ENABLED;
+    snd_port->setting.plc = PJ_TRUE;
 #endif
 
@@ -307,5 +358,5 @@
      */
     if ((snd_port->dir & PJMEDIA_DIR_PLAYBACK) &&
-	(snd_port->options & PJMEDIA_PLC_ENABLED)) 
+	(snd_port->setting.plc)) 
     {
 	status = pjmedia_plc_create(pool, snd_port->clock_rate, 
@@ -374,4 +425,6 @@
     pjmedia_snd_port *snd_port;
 
+    PJ_UNUSED_ARG(options);
+
     PJ_ASSERT_RETURN(pool && p_port, PJ_EINVAL);
 
@@ -382,5 +435,4 @@
     snd_port->play_id = play_id;
     snd_port->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
-    snd_port->options = options | DEFAULT_OPTIONS;
     snd_port->clock_rate = clock_rate;
     snd_port->channel_count = channel_count;
@@ -429,4 +481,6 @@
     pjmedia_snd_port *snd_port;
 
+    PJ_UNUSED_ARG(options);
+
     PJ_ASSERT_RETURN(pool && p_port, PJ_EINVAL);
 
@@ -436,5 +490,4 @@
     snd_port->rec_id = dev_id;
     snd_port->dir = PJMEDIA_DIR_CAPTURE;
-    snd_port->options = options | DEFAULT_OPTIONS;
     snd_port->clock_rate = clock_rate;
     snd_port->channel_count = channel_count;
@@ -466,4 +519,6 @@
     pjmedia_snd_port *snd_port;
 
+    PJ_UNUSED_ARG(options);
+
     PJ_ASSERT_RETURN(pool && p_port, PJ_EINVAL);
 
@@ -473,5 +528,4 @@
     snd_port->play_id = dev_id;
     snd_port->dir = PJMEDIA_DIR_PLAYBACK;
-    snd_port->options = options | DEFAULT_OPTIONS;
     snd_port->clock_rate = clock_rate;
     snd_port->channel_count = channel_count;
@@ -486,4 +540,65 @@
      */
     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;
+    snd_port->setting = *setting;
+    
+#if PJMEDIA_SOUND_USE_DELAYBUF
+    if (snd_port->setting.format.u32 == 0 ||
+	snd_port->setting.format.u32 == PJMEDIA_FOURCC_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
+
+    *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 );
+
 }
 
Index: /pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia/stream.c
===================================================================
--- /pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia/stream.c	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia/stream.c	(revision 2438)
@@ -411,4 +411,114 @@
 
 
+/* The other version of get_frame callback used when stream port format
+ * is non linear PCM.
+ */
+static pj_status_t get_frame_ext( pjmedia_port *port, pjmedia_frame *frame)
+{
+    pjmedia_stream *stream = (pjmedia_stream*) port->port_data.pdata;
+    pjmedia_channel *channel = stream->dec;
+    pjmedia_frame_ext *f = (pjmedia_frame_ext*)frame;
+    unsigned samples_per_frame, samples_required;
+    pj_status_t status;
+
+    /* Return no frame if channel is paused */
+    if (channel->paused) {
+	frame->type = PJMEDIA_FRAME_TYPE_NONE;
+	return PJ_SUCCESS;
+    }
+
+    /* Repeat get frame from the jitter buffer and decode the frame
+     * until we have enough frames according to codec's ptime.
+     */
+
+    samples_required = stream->port.info.samples_per_frame;
+    samples_per_frame = stream->codec_param.info.frm_ptime *
+			stream->codec_param.info.clock_rate *
+			stream->codec_param.info.channel_cnt / 
+			1000;
+
+    pj_bzero(f, sizeof(pjmedia_frame_ext));
+    f->base.type = PJMEDIA_FRAME_TYPE_EXTENDED;
+
+    while (f->samples_cnt < samples_required) {
+	char frame_type;
+	pj_size_t frame_size;
+	pj_uint32_t bit_info;
+
+	/* Lock jitter buffer mutex first */
+	pj_mutex_lock( stream->jb_mutex );
+
+	/* Get frame from jitter buffer. */
+	pjmedia_jbuf_get_frame2(stream->jb, channel->out_pkt, &frame_size,
+			        &frame_type, &bit_info);
+	
+	/* Unlock jitter buffer mutex. */
+	pj_mutex_unlock( stream->jb_mutex );
+
+	if (frame_type == PJMEDIA_JB_NORMAL_FRAME) {
+	    /* Got "NORMAL" frame from jitter buffer */
+	    pjmedia_frame frame_in;
+
+	    /* Decode */
+	    frame_in.buf = channel->out_pkt;
+	    frame_in.size = frame_size;
+	    frame_in.bit_info = bit_info;
+	    frame_in.type = PJMEDIA_FRAME_TYPE_AUDIO;
+
+	    status = stream->codec->op->decode( stream->codec, &frame_in,
+						0, frame);
+	    if (status != PJ_SUCCESS) {
+		LOGERR_((port->info.name.ptr, "codec decode() error", 
+			 status));
+		pjmedia_frame_ext_append_subframe(f, NULL, 0,
+					    (pj_uint16_t)samples_per_frame);
+	    }
+	} else {
+	    status = (*stream->codec->op->recover)(stream->codec,
+						   0, frame);
+	    if (status != PJ_SUCCESS) {
+		pjmedia_frame_ext_append_subframe(f, NULL, 0,
+					    (pj_uint16_t)samples_per_frame);
+	    }
+
+	    if (frame_type == PJMEDIA_JB_MISSING_FRAME) {
+		PJ_LOG(1,(stream->port.info.name.ptr,  "Frame lost!"));
+	    } else if (frame_type == PJMEDIA_JB_ZERO_EMPTY_FRAME) {
+		/* Jitter buffer is empty. Check if this is the first "empty" 
+		 * state.
+		 */
+		if (frame_type != stream->jb_last_frm) {
+		    pjmedia_jb_state jb_state;
+
+		    /* Report the state of jitter buffer */
+		    pjmedia_jbuf_get_state(stream->jb, &jb_state);
+		    PJ_LOG(1,(stream->port.info.name.ptr, 
+			      "Jitter buffer empty (prefetch=%d)", 
+			      jb_state.prefetch));
+		}
+	    } else {
+		pjmedia_jb_state jb_state;
+
+		/* It can only be PJMEDIA_JB_ZERO_PREFETCH frame */
+		pj_assert(frame_type == PJMEDIA_JB_ZERO_PREFETCH_FRAME);
+
+		/* Get the state of jitter buffer */
+		pjmedia_jbuf_get_state(stream->jb, &jb_state);
+
+		if (stream->jb_last_frm != frame_type) {
+		    PJ_LOG(1,(stream->port.info.name.ptr, 
+			      "Jitter buffer is bufferring (prefetch=%d)",
+			      jb_state.prefetch));
+		}
+	    }
+	}
+
+	stream->jb_last_frm = frame_type;
+    }
+
+    return PJ_SUCCESS;
+}
+
+
 /*
  * Transmit DTMF
@@ -689,4 +799,7 @@
     if (frame->type == PJMEDIA_FRAME_TYPE_AUDIO)
 	ts_len = (frame->size >> 1) / stream->codec_param.info.channel_cnt;
+    else if (frame->type == PJMEDIA_FRAME_TYPE_EXTENDED)
+	ts_len = stream->port.info.samples_per_frame / 
+		 stream->port.info.channel_count;
     else
 	ts_len = 0;
@@ -755,4 +868,6 @@
     } else if (frame->type == PJMEDIA_FRAME_TYPE_AUDIO &&
 	       frame->buf == NULL &&
+	       (stream->port.info.format.u32 == 0 ||
+	        stream->port.info.format.u32 == PJMEDIA_FOURCC_L16) &&
 	       (stream->dir & PJMEDIA_DIR_ENCODING) &&
 	       stream->codec_param.info.frm_ptime *
@@ -1492,7 +1607,15 @@
     stream->port.info.clock_rate = info->fmt.clock_rate;
     stream->port.info.channel_count = info->fmt.channel_cnt;
+    stream->port.info.format = info->param->info.format;
     stream->port.port_data.pdata = stream;
-    stream->port.put_frame = &put_frame;
-    stream->port.get_frame = &get_frame;
+    if (stream->port.info.format.u32 == 0 ||
+	stream->port.info.format.u32 == PJMEDIA_FOURCC_L16)
+    {
+	stream->port.put_frame = &put_frame;
+	stream->port.get_frame = &get_frame;
+    } else {
+	stream->port.put_frame = &put_frame;
+	stream->port.get_frame = &get_frame_ext;
+    }
 
 
Index: /pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia/symbian_sound_aps.cpp
===================================================================
--- /pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia/symbian_sound_aps.cpp	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/pjmedia/src/pjmedia/symbian_sound_aps.cpp	(revision 2438)
@@ -18,8 +18,9 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
  */
+#include <pjmedia/sound.h>
 #include <pjmedia/symbian_sound_aps.h>
-#include <pjmedia/sound.h>
 #include <pjmedia/alaw_ulaw.h>
 #include <pjmedia/errno.h>
+#include <pjmedia/port.h>
 #include <pj/assert.h>
 #include <pj/log.h>
@@ -59,7 +60,4 @@
 /* App UID to open global APS queues to communicate with the APS server. */
 extern TPtrC		    APP_UID;
-
-/* Default setting */
-static pjmedia_snd_aps_setting def_setting;
 
 /* APS G.711 frame length */
@@ -88,4 +86,5 @@
     pjmedia_snd_play_cb	 play_cb;
     void                *user_data;
+    pjmedia_snd_setting  setting;
 
     // Audio engine
@@ -405,11 +404,11 @@
     // the client session.
     TTime start, now;
-    enum { APS_CLOSE_WAIT_TIME = 200 };
+    enum { APS_CLOSE_WAIT_TIME = 200 }; /* in msecs */
+    
     start.UniversalTime();
-    now.UniversalTime();
-    while (now.MicroSecondsFrom(start) < APS_CLOSE_WAIT_TIME * 1000) {
+    do {
 	pj_symbianos_poll(-1, APS_CLOSE_WAIT_TIME);
 	now.UniversalTime();
-    }
+    } while (now.MicroSecondsFrom(start) < APS_CLOSE_WAIT_TIME * 1000);
 
     iSession.Close();
@@ -618,4 +617,5 @@
     pjmedia_snd_stream *strm = (pjmedia_snd_stream*) user_data;
 
+    /* Buffer has to contain normal speech. */
     pj_assert(buf.iBuffer[0] == 1 && buf.iBuffer[1] == 0);
 
@@ -660,4 +660,5 @@
     unsigned g711_frame_len = aps_g711_frame_len;
 
+    /* Init buffer attributes and header. */
     buf.iCommand = CQueueHandler::EAPSPlayData;
     buf.iStatus = 0;
@@ -697,8 +698,115 @@
 static void RecCb(TAPSCommBuffer &buf, void *user_data)
 {
+    pjmedia_snd_stream *strm = (pjmedia_snd_stream*) user_data;
+    pjmedia_frame_ext *frame = (pjmedia_frame_ext*) strm->rec_buf;
+    unsigned samples_processed = 0;
+
+    switch(strm->setting.format.u32) {
+    
+    case PJMEDIA_FOURCC_G711U:
+    case PJMEDIA_FOURCC_G711A:
+	pj_assert(buf.iBuffer[0] == 1 && buf.iBuffer[1] == 0);
+
+	/* Detect the recorder G.711 frame size, player frame size will follow
+	 * this recorder frame size.
+	 */
+	if (aps_g711_frame_len == 0) {
+	    aps_g711_frame_len = buf.iBuffer.Length() < 160? 80 : 160;
+	    TRACE_((THIS_FILE, "Detected APS G.711 frame size = %u samples",
+		    aps_g711_frame_len));
+	}
+	
+	/* Convert APS buffer format into pjmedia_frame_ext. Whenever 
+	 * samples count in the frame is equal to stream's samples per frame,
+	 * call parent stream callback.
+	 */
+	while (samples_processed < aps_g711_frame_len) {
+	    unsigned tmp;
+	    const pj_uint8_t *pb = (const pj_uint8_t*)buf.iBuffer.Ptr() + 2 +
+				   samples_processed;
+
+	    tmp = PJ_MIN(strm->samples_per_frame - frame->samples_cnt,
+			 aps_g711_frame_len - samples_processed);
+	    
+	    pjmedia_frame_ext_append_subframe(frame, pb, tmp << 3, tmp);
+	    samples_processed += tmp;
+
+	    if (frame->samples_cnt == strm->samples_per_frame) {
+		frame->base.type = PJMEDIA_FRAME_TYPE_EXTENDED;
+		strm->rec_cb(strm->user_data, 0, strm->rec_buf, 0);
+		frame->samples_cnt = 0;
+		frame->subframe_cnt = 0;
+	    }
+	}
+	break;
+	
+    default:
+	break;
+    }
 }
 
 static void PlayCb(TAPSCommBuffer &buf, void *user_data)
 {
+    pjmedia_snd_stream *strm = (pjmedia_snd_stream*) user_data;
+    pjmedia_frame_ext *frame = (pjmedia_frame_ext*) strm->play_buf;
+    unsigned g711_frame_len = aps_g711_frame_len;
+    unsigned samples_ready = 0;
+
+    /* Init buffer attributes and header. */
+    buf.iCommand = CQueueHandler::EAPSPlayData;
+    buf.iStatus = 0;
+    buf.iBuffer.Zero();
+
+    switch(strm->setting.format.u32) {
+    
+    case PJMEDIA_FOURCC_G711U:
+    case PJMEDIA_FOURCC_G711A:
+	/* Add header. */
+	buf.iBuffer.Append(1);
+	buf.iBuffer.Append(0);
+
+	/* Assume frame size is 10ms if frame size hasn't been known. */
+	if (g711_frame_len == 0)
+	    g711_frame_len = 80;
+	
+	/* Call parent stream callback to get samples to play. */
+	while (samples_ready < g711_frame_len) {
+	    if (frame->samples_cnt == 0) {
+		frame->base.type = PJMEDIA_FRAME_TYPE_EXTENDED;
+		strm->play_cb(strm->user_data, 0, strm->play_buf,
+			      strm->samples_per_frame<<1);
+		
+		pj_assert(frame->base.type == PJMEDIA_FRAME_TYPE_EXTENDED ||
+			  frame->base.type == PJMEDIA_FRAME_TYPE_NONE);
+	    }
+
+	    if (frame->base.type == PJMEDIA_FRAME_TYPE_EXTENDED) { 
+		pjmedia_frame_ext_subframe *sf;
+		unsigned samples_cnt;
+		
+		sf = pjmedia_frame_ext_get_subframe(frame, 0);
+		samples_cnt = frame->samples_cnt / frame->subframe_cnt;
+		if (sf->data && sf->bitlen)
+		    buf.iBuffer.Append((TUint8*)sf->data, sf->bitlen>>3);
+		else 
+		    buf.iBuffer.AppendFill(0, samples_cnt);
+		samples_ready += samples_cnt;
+		
+		pjmedia_frame_ext_pop_subframes(frame, 1);
+	    
+	    } else { /* PJMEDIA_FRAME_TYPE_NONE */
+		buf.iBuffer.AppendFill(0, g711_frame_len - samples_ready);
+		samples_ready = g711_frame_len;
+		frame->samples_cnt = 0;
+		frame->subframe_cnt = 0;
+	    }
+	}
+	break;
+	
+    default:
+	break;
+    }
+    
+    unsigned tes = buf.iBuffer.Length();
 }
 
@@ -709,12 +817,4 @@
 {
     snd_pool_factory = factory;
-
-    def_setting.format.u32 = PJMEDIA_FOURCC_L16;
-    def_setting.mode = 0;
-    def_setting.bitrate = 128000;
-    def_setting.plc = PJ_FALSE;
-    def_setting.vad = PJ_FALSE;
-    def_setting.cng = PJ_FALSE;
-    def_setting.loudspk = PJ_FALSE;
 
     return PJ_SUCCESS;
@@ -751,9 +851,10 @@
 			      pjmedia_snd_play_cb play_cb,
 			      void *user_data,
+			      const pjmedia_snd_setting *setting,
 			      pjmedia_snd_stream **p_snd_strm)
 {
     pj_pool_t *pool;
     pjmedia_snd_stream *strm;
-    CPjAudioSetting setting;
+    CPjAudioSetting aps_setting;
     PjAudioCallback aps_rec_cb;
     PjAudioCallback aps_play_cb;
@@ -779,43 +880,43 @@
     strm->channel_count = channel_count;
     strm->samples_per_frame = samples_per_frame;
+    strm->setting = *setting;
+
+    if (strm->setting.format.u32 == 0)
+	strm->setting.format.u32 = PJMEDIA_FOURCC_L16;
 
     /* Set audio engine settings. */
-    if (def_setting.format.u32 == PJMEDIA_FOURCC_G711U ||
-	def_setting.format.u32 == PJMEDIA_FOURCC_L16)
+    if (strm->setting.format.u32 == PJMEDIA_FOURCC_G711U ||
+	strm->setting.format.u32 == PJMEDIA_FOURCC_L16)
     {
-	setting.fourcc = TFourCC(KMCPFourCCIdG711);
+	aps_setting.fourcc = TFourCC(KMCPFourCCIdG711);
     } else {
-	setting.fourcc = TFourCC(def_setting.format.u32);
-    }
-
-    if (def_setting.format.u32 == PJMEDIA_FOURCC_AMR)
+	aps_setting.fourcc = TFourCC(strm->setting.format.u32);
+    }
+
+    if (strm->setting.format.u32 == PJMEDIA_FOURCC_AMR)
     {
-	setting.mode = (TAPSCodecMode)def_setting.bitrate;
-    } else if (def_setting.format.u32 == PJMEDIA_FOURCC_G711U ||
-	       def_setting.format.u32 == PJMEDIA_FOURCC_L16   ||
-	      (def_setting.format.u32 == PJMEDIA_FOURCC_ILBC  &&
-	       def_setting.mode == 30))
+	aps_setting.mode = (TAPSCodecMode)strm->setting.bitrate;
+    } else if (strm->setting.format.u32 == PJMEDIA_FOURCC_G711U ||
+	       strm->setting.format.u32 == PJMEDIA_FOURCC_L16   ||
+	      (strm->setting.format.u32 == PJMEDIA_FOURCC_ILBC  &&
+	       strm->setting.mode == 30))
     {
-	setting.mode = EULawOr30ms;
+	aps_setting.mode = EULawOr30ms;
     } else {
-	setting.mode = EALawOr20ms;
-    }
-
-    setting.vad = def_setting.vad;
-    setting.plc = def_setting.plc;
-    setting.cng = def_setting.cng;
-    setting.loudspk = def_setting.loudspk;
-
-    if (def_setting.format.u32 == PJMEDIA_FOURCC_AMR ||
-	def_setting.format.u32 == PJMEDIA_FOURCC_G711A ||
-	def_setting.format.u32 == PJMEDIA_FOURCC_G711U ||
-	def_setting.format.u32 == PJMEDIA_FOURCC_G729 ||
-	def_setting.format.u32 == PJMEDIA_FOURCC_ILBC)
-    {
+	aps_setting.mode = EALawOr20ms;
+    }
+
+    aps_setting.vad = strm->setting.format.u32==PJMEDIA_FOURCC_L16? 
+					EFalse : strm->setting.vad;
+    aps_setting.plc = strm->setting.plc;
+    aps_setting.cng = strm->setting.cng;
+    aps_setting.loudspk = strm->setting.loudspk;
+
+    if (strm->setting.format.u32 == PJMEDIA_FOURCC_L16) {
+	aps_play_cb = &PlayCbPcm;
+	aps_rec_cb  = &RecCbPcm;
+    } else {
 	aps_play_cb = &PlayCb;
 	aps_rec_cb  = &RecCb;
-    } else {
-	aps_play_cb = &PlayCbPcm;
-	aps_rec_cb  = &RecCbPcm;
     }
 
@@ -823,5 +924,5 @@
     TRAPD(err, strm->engine = CPjAudioEngine::NewL(strm,
 						   aps_rec_cb, aps_play_cb,
-						   strm, setting));
+						   strm, aps_setting));
     if (err != KErrNone) {
     	pj_pool_release(pool);
@@ -834,10 +935,10 @@
 
     /* play_buf size is samples per frame. */
-    strm->play_buf = (pj_int16_t*)pj_pool_alloc(pool, samples_per_frame << 1);
+    strm->play_buf = (pj_int16_t*)pj_pool_zalloc(pool, samples_per_frame << 1);
     strm->play_buf_len = 0;
     strm->play_buf_start = 0;
 
     /* rec_buf size is samples per frame. */
-    strm->rec_buf  = (pj_int16_t*)pj_pool_alloc(pool, samples_per_frame << 1);
+    strm->rec_buf  = (pj_int16_t*)pj_pool_zalloc(pool, samples_per_frame << 1);
     strm->rec_buf_len = 0;
 
@@ -861,10 +962,16 @@
 					  pjmedia_snd_stream **p_snd_strm)
 {
+    pjmedia_snd_setting setting;
+    
     if (index < 0) index = 0;
     PJ_ASSERT_RETURN(index == 0, PJ_EINVAL);
 
+    pj_bzero(&setting, sizeof(setting));
+    setting.format.u32 = PJMEDIA_FOURCC_L16;
+    setting.bitrate = 128000;
+    
     return sound_open(PJMEDIA_DIR_CAPTURE, clock_rate, channel_count,
 		      samples_per_frame, bits_per_sample, rec_cb, NULL,
-		      user_data, p_snd_strm);
+		      user_data, &setting, p_snd_strm);
 }
 
@@ -878,10 +985,16 @@
 					pjmedia_snd_stream **p_snd_strm )
 {
+    pjmedia_snd_setting setting;
+    
     if (index < 0) index = 0;
     PJ_ASSERT_RETURN(index == 0, PJ_EINVAL);
 
+    pj_bzero(&setting, sizeof(setting));
+    setting.format.u32 = PJMEDIA_FOURCC_L16;
+    setting.bitrate = 128000;
+
     return sound_open(PJMEDIA_DIR_PLAYBACK, clock_rate, channel_count,
 		      samples_per_frame, bits_per_sample, NULL, play_cb,
-		      user_data, p_snd_strm);
+		      user_data, &setting, p_snd_strm);
 }
 
@@ -897,11 +1010,39 @@
 				      pjmedia_snd_stream **p_snd_strm)
 {
+    pjmedia_snd_setting setting;
+
     if (rec_id < 0) rec_id = 0;
     if (play_id < 0) play_id = 0;
     PJ_ASSERT_RETURN(play_id == 0 && rec_id == 0, PJ_EINVAL);
 
+    pj_bzero(&setting, sizeof(setting));
+    setting.format.u32 = PJMEDIA_FOURCC_L16;
+    setting.bitrate = 128000;
+
     return sound_open(PJMEDIA_DIR_CAPTURE_PLAYBACK, clock_rate, channel_count,
 		      samples_per_frame, bits_per_sample, rec_cb, play_cb,
-		      user_data, p_snd_strm);
+		      user_data, &setting, p_snd_strm);
+}
+
+PJ_DEF(pj_status_t) pjmedia_snd_open2( pjmedia_dir dir,
+				       int rec_id,
+				       int play_id,
+				       unsigned clock_rate,
+				       unsigned channel_count,
+				       unsigned samples_per_frame,
+				       unsigned bits_per_sample,
+				       pjmedia_snd_rec_cb rec_cb,
+				       pjmedia_snd_play_cb play_cb,
+				       void *user_data,
+				       const pjmedia_snd_setting *setting,
+				       pjmedia_snd_stream **p_snd_strm)
+{
+    if (rec_id < 0) rec_id = 0;
+    if (play_id < 0) play_id = 0;
+    PJ_ASSERT_RETURN(play_id == 0 && rec_id == 0, PJ_EINVAL);
+
+    return sound_open(dir, clock_rate, channel_count,
+		      samples_per_frame, bits_per_sample, rec_cb, play_cb,
+		      user_data, setting, p_snd_strm);
 }
 
@@ -994,5 +1135,4 @@
 }
 
-
 /*
  * Activate/deactivate loudspeaker.
@@ -1002,30 +1142,12 @@
 					pj_bool_t active)
 {
-    if (stream == NULL) {
-	def_setting.loudspk = active;
-    } else {
-	if (stream->engine == NULL)
-	    return PJ_EINVAL;
-
-	TInt err = stream->engine->ActivateSpeaker(active);
-	if (err != KErrNone)
-	    return PJ_RETURN_OS_ERROR(err);
-    }
+    PJ_ASSERT_RETURN(stream && stream->engine, PJ_EINVAL);
+    
+    TInt err = stream->engine->ActivateSpeaker(active);
+    if (err != KErrNone)
+	return PJ_RETURN_OS_ERROR(err);
 
     return PJ_SUCCESS;
 }
 
-/**
- * Set a codec and its settings to be used on the next sound device session.
- */
-PJ_DEF(pj_status_t) pjmedia_snd_aps_modify_setting(
-				    const pjmedia_snd_aps_setting *setting)
-{
-    PJ_ASSERT_RETURN(setting, PJ_EINVAL);
-
-    def_setting = *setting;
-
-    return PJ_SUCCESS;
-}
-
 #endif // PJMEDIA_SOUND_IMPLEMENTATION == PJMEDIA_SOUND_SYMB_APS_SOUND
Index: /pjproject/branches/projects/aps-direct/pjsip-apps/src/symbian_ua/ua.cpp
===================================================================
--- /pjproject/branches/projects/aps-direct/pjsip-apps/src/symbian_ua/ua.cpp	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/pjsip-apps/src/symbian_ua/ua.cpp	(revision 2438)
@@ -91,4 +91,7 @@
 static pjsua_buddy_id g_buddy_id = PJSUA_INVALID_ID;
 
+static pj_pool_t *app_pool;
+static pjmedia_snd_port *snd_port;
+
 
 /* Callback called by the library upon receiving incoming call */
@@ -130,4 +133,8 @@
     	if (call_id == g_call_id)
     	    g_call_id = PJSUA_INVALID_ID;
+    	if (snd_port) {
+    	    pjmedia_snd_port_destroy(snd_port);
+    	    snd_port = NULL;
+    	}
     } else if (ci.state != PJSIP_INV_STATE_INCOMING) {
     	if (g_call_id == PJSUA_INVALID_ID)
@@ -250,4 +257,72 @@
 }
 
+/* Notification that stream is created. */
+static void on_stream_created(pjsua_call_id call_id, 
+			      pjmedia_session *sess,
+			      unsigned stream_idx, 
+			      pjmedia_port **p_port)
+{
+    pjmedia_port *conf;
+    pjmedia_session_info sess_info;
+    pjmedia_stream_info *strm_info;
+    pjmedia_snd_setting setting;
+    unsigned samples_per_frame;
+    pj_status_t status;
+    
+    status = pjmedia_session_get_info(sess, &sess_info);
+    if (status != PJ_SUCCESS) {
+	PJ_LOG(1,(THIS_FILE, "on_stream_created() failed to get session info, "
+			     "status=%d", status));
+	return;
+    }
+    
+    strm_info = &sess_info.stream_info[stream_idx];
+    if (strm_info->type != PJMEDIA_TYPE_AUDIO)
+	return;
+
+    /* Don't need to reopen sound device when the session doesn't use
+     * PCM format. 
+     */
+    if (strm_info->param->info.format.u32 == 0 ||
+	strm_info->param->info.format.u32 == PJMEDIA_FOURCC_L16) 
+    {
+	return;
+    }
+    
+    pj_bzero(&setting, sizeof(setting));
+    setting.format = strm_info->param->info.format;
+    setting.bitrate = strm_info->param->info.avg_bps;
+    setting.cng = strm_info->param->setting.cng;
+    setting.vad = strm_info->param->setting.vad;
+    setting.plc = strm_info->param->setting.plc;
+
+    /* Reopen sound device. */
+    conf = pjsua_set_no_snd_dev();
+
+    samples_per_frame = conf->info.samples_per_frame;
+    
+    status = pjmedia_snd_port_create2(app_pool, 
+				      PJMEDIA_DIR_CAPTURE_PLAYBACK,
+				      0,
+				      0,
+				      8000,
+				      1,
+				      samples_per_frame,
+				      16,
+				      &setting,
+				      &snd_port);
+    if (status != PJ_SUCCESS) {
+	PJ_LOG(1,(THIS_FILE, "on_stream_created() failed to reopen sound "
+			     "device, status=%d", status));
+	return;
+    }
+    
+    status = pjmedia_snd_port_connect(snd_port, conf);
+    if (status != PJ_SUCCESS) {
+	PJ_LOG(1,(THIS_FILE, "on_stream_created() failed to connect sound "
+			     "device to conference, status=%d", status));
+	return;
+    }
+}
 
 //#include<e32debug.h>
@@ -290,4 +365,7 @@
     }
 
+    /* Create pool for application */
+    app_pool = pjsua_pool_create("pjsua-app", 1000, 1000);
+    
     /* Init pjsua */
     pjsua_config cfg;
@@ -310,4 +388,5 @@
     cfg.cb.on_call_replaced = &on_call_replaced;
     cfg.cb.on_nat_detect = &on_nat_detect;
+    cfg.cb.on_stream_created = &on_stream_created;
     
     if (SIP_PROXY) {
@@ -338,12 +417,8 @@
     med_cfg.has_ioqueue = PJ_FALSE;
     med_cfg.clock_rate = 8000;
-#if defined(PJMEDIA_SYM_SND_USE_APS) && (PJMEDIA_SYM_SND_USE_APS==1)
-    med_cfg.audio_frame_ptime = 20;
-#else
     med_cfg.audio_frame_ptime = 40;
-#endif
     med_cfg.ec_tail_len = 0;
     med_cfg.enable_ice = USE_ICE;
-    med_cfg.snd_auto_close_time = 5; // wait for 5 seconds idle before sound dev get auto-closed
+    med_cfg.snd_auto_close_time = 0; // wait for 0 seconds idle before sound dev get auto-closed
     
     status = pjsua_init(&cfg, &log_cfg, &med_cfg);
@@ -803,4 +878,16 @@
 #endif
 	
+    // Let pjsua destroys app pool, since the pool may still be used by app
+    // until pjsua_destroy() finished.
+    // e.g: quitting app when there is an active call may cause sound port 
+    // memory destroyed before sound port itself gets closed/destroyed.
+    /*
+    // Release application pool
+    if (app_pool) {
+	pj_pool_release(app_pool);
+	app_pool = NULL;
+    }
+    */
+    
     // Shutdown pjsua
     pjsua_destroy();
Index: /pjproject/branches/projects/aps-direct/pjsip/src/pjsua-lib/pjsua_media.c
===================================================================
--- /pjproject/branches/projects/aps-direct/pjsip/src/pjsua-lib/pjsua_media.c	(revision 2437)
+++ /pjproject/branches/projects/aps-direct/pjsip/src/pjsua-lib/pjsua_media.c	(revision 2438)
@@ -173,4 +173,14 @@
 #endif /* PJMEDIA_HAS_INTEL_IPP */
 
+#if PJMEDIA_HAS_PASSTHROUGH_CODECS
+    /* Register passthrough codecs */
+    status = pjmedia_codec_passthrough_init(pjsua_var.med_endpt);
+    if (status != PJ_SUCCESS) {
+	pjsua_perror(THIS_FILE, "Error initializing passthrough codecs",
+		     status);
+	return status;
+    }
+#endif /* PJMEDIA_HAS_PASSTHROUGH_CODECS */
+
 #if PJMEDIA_HAS_L16_CODEC
     /* Register L16 family codecs, but disable all */
@@ -574,4 +584,8 @@
 	    pjmedia_codec_ipp_deinit();
 #	endif	/* PJMEDIA_HAS_INTEL_IPP */
+
+#	if PJMEDIA_HAS_PASSTHROUGH_CODECS
+	    pjmedia_codec_passthrough_deinit();
+#	endif /* PJMEDIA_HAS_PASSTHROUGH_CODECS */
 
 #	if PJMEDIA_HAS_L16_CODEC
