Index: pjproject/branches/projects/aps-direct/pjsip/include/pjsua-lib/pjsua.h
===================================================================
--- pjproject/branches/projects/aps-direct/pjsip/include/pjsua-lib/pjsua.h	(revision 2492)
+++ pjproject/branches/projects/aps-direct/pjsip/include/pjsua-lib/pjsua.h	(revision 2493)
@@ -4287,7 +4287,8 @@
     /**
      * Specify idle time of sound device before it is automatically closed,
-     * in seconds.
-     *
-     * Default : -1 (Disable the auto-close feature of sound device)
+     * in seconds. Use value -1 to disable the auto-close feature of sound
+     * device
+     *
+     * Default : 1
      */
     int			snd_auto_close_time;
@@ -4802,5 +4803,5 @@
 
 /**
- * Enum all sound devices installed in the system.
+ * Enum all audio devices installed in the system.
  *
  * @param info		Array of info to be initialized.
@@ -4810,4 +4811,17 @@
  *
  * @return		PJ_SUCCESS on success, or the appropriate error code.
+ */
+PJ_DECL(pj_status_t) pjsua_enum_aud_devs(pjmedia_aud_dev_info info[],
+					 unsigned *count);
+
+/**
+ * Enum all sound devices installed in the system (old API).
+ *
+ * @param info		Array of info to be initialized.
+ * @param count		On input, specifies max elements in the array.
+ *			On return, it contains actual number of elements
+ *			that have been initialized.
+ *
+ * @return		PJ_SUCCESS on success, or the appropriate error code.
  *
  *
@@ -4819,12 +4833,6 @@
  *
  */
-#if PJMEDIA_AUDIO_API==PJMEDIA_AUDIO_API_NEW_ONLY
-PJ_DECL(pj_status_t) pjsua_enum_snd_devs(pjmedia_aud_dev_info info[],
-					 unsigned *count);
-#else
 PJ_DECL(pj_status_t) pjsua_enum_snd_devs(pjmedia_snd_dev_info info[],
 					 unsigned *count);
-#endif
-
 
 /**
@@ -4898,7 +4906,10 @@
 
 /**
- * Change the echo cancellation settings. The behavior of this function 
- * depends on whether the sound device is currently active, and if it is,
- * whether device or software AEC is being used. 
+ * Change the echo cancellation settings. Application may also use the
+ * #pjsua_snd_set_setting() to retrieve the echo cancellation setting.
+ *
+ * The behavior of this function depends on whether the sound device is
+ * currently active, and if it is, whether device or software AEC is 
+ * being used. 
  *
  * If the sound device is currently active, and if the device supports AEC,
@@ -4929,5 +4940,6 @@
 
 /**
- * Get current echo canceller tail length.
+ * Get current echo canceller tail length. Application may also use the
+ * #pjsua_snd_set_setting() to retrieve the echo cancellation setting.
  *
  * @param p_tail_ms	Pointer to receive the tail length, in miliseconds. 
@@ -4945,10 +4957,59 @@
 
 /**
- * Get active audio device stream instance.
- *
- * @return		Audio device stream instance if any, otherwise 
- *			NULL will be returned.
- */
-PJ_DECL(pjmedia_aud_stream*) pjsua_get_aud_stream();
+ * Check whether the sound device is currently active. The sound device
+ * may be inactive if the application has set the auto close feature to
+ * non-zero (the snd_auto_close_time setting in #pjsua_media_config), or
+ * if null sound device or no sound device has been configured via the
+ * #pjsua_set_no_snd_dev() function.
+ */
+PJ_DECL(pj_bool_t) pjsua_snd_is_active(void);
+
+    
+/**
+ * Configure sound device setting to the sound device being used. If sound 
+ * device is currently active, the function will forward the setting to the
+ * sound device instance to be applied immediately, if it supports it. 
+ *
+ * The setting will be saved for future opening of the sound device, if the 
+ * "keep" argument is set to non-zero. If the sound device is currently
+ * inactive, and the "keep" argument is false, this function will return
+ * error.
+ * 
+ * Note that in case the setting is kept for future use, it will be applied
+ * to any devices, even when application has changed the sound device to be
+ * used.
+ *
+ * See also #pjmedia_aud_stream_set_cap() for more information about setting
+ * an audio device capability.
+ *
+ * @param cap		The sound device setting to change.
+ * @param pval		Pointer to value. Please see #pjmedia_aud_dev_cap
+ *			documentation about the type of value to be 
+ *			supplied for each setting.
+ * @param keep		Specify whether the setting is to be kept for future
+ *			use.
+ *
+ * @return		PJ_SUCCESS on success or the appropriate error code.
+ */
+PJ_DECL(pj_status_t) pjsua_snd_set_setting(pjmedia_aud_dev_cap cap,
+					   const void *pval,
+					   pj_bool_t keep);
+
+/**
+ * Retrieve a sound device setting. If sound device is currently active,
+ * the function will forward the request to the sound device. If sound device
+ * is currently inactive, and if application had previously set the setting
+ * and mark the setting as kept, then that setting will be returned.
+ * Otherwise, this function will return error.
+ *
+ * @param cap		The sound device setting to retrieve.
+ * @param pval		Pointer to receive the value. 
+ *			Please see #pjmedia_aud_dev_cap documentation about
+ *			the type of value to be supplied for each setting.
+ *
+ * @return		PJ_SUCCESS on success or the appropriate error code.
+ */
+PJ_DECL(pj_status_t) pjsua_snd_get_setting(pjmedia_aud_dev_cap cap,
+					   void *pval);
 
 
Index: pjproject/branches/projects/aps-direct/pjsip/include/pjsua-lib/pjsua_internal.h
===================================================================
--- pjproject/branches/projects/aps-direct/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 2492)
+++ pjproject/branches/projects/aps-direct/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 2493)
@@ -273,6 +273,13 @@
     pjsua_conf_setting	 mconf_cfg; /**< Additionan conf. bridge. param */
     pjmedia_conf	*mconf;	    /**< Conference bridge.		*/
-    int			 cap_dev;   /**< Capture device ID.		*/
-    int			 play_dev;  /**< Playback device ID.		*/
+    pj_bool_t		 is_mswitch;/**< Are we using audio switchboard
+				         (a.k.a APS-Direct)		*/
+
+    /* Sound device */
+    pjmedia_aud_dev_index cap_dev;  /**< Capture device ID.		*/
+    pjmedia_aud_dev_index play_dev; /**< Playback device ID.		*/
+    pj_uint32_t		 aud_svmask;/**< Which settings to save		*/
+    pjmedia_aud_param	 aud_param; /**< User settings to sound dev	*/
+    pj_bool_t		 aud_open_cnt;/**< How many # device is opened	*/
     pj_bool_t		 no_snd;    /**< No sound (app will manage it)	*/
     pj_pool_t		*snd_pool;  /**< Sound's private pool.		*/
Index: pjproject/branches/projects/aps-direct/pjsip/src/pjsua-lib/pjsua_call.c
===================================================================
--- pjproject/branches/projects/aps-direct/pjsip/src/pjsua-lib/pjsua_call.c	(revision 2492)
+++ pjproject/branches/projects/aps-direct/pjsip/src/pjsua-lib/pjsua_call.c	(revision 2493)
@@ -371,7 +371,11 @@
     PJSUA_LOCK();
 
-    /* Create sound port if none is instantiated */
-    if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && 
-	!pjsua_var.no_snd) 
+    /* Create sound port if none is instantiated, to check if sound device
+     * can be used. But only do this with the conference bridge, as with 
+     * audio switchboard (i.e. APS-Direct), we can only open the sound 
+     * device once the correct format has been known
+     */
+    if (!pjsua_var.is_mswitch && pjsua_var.snd_port==NULL && 
+	pjsua_var.null_snd==NULL && !pjsua_var.no_snd) 
     {
 	pj_status_t status;
Index: pjproject/branches/projects/aps-direct/pjsip/src/pjsua-lib/pjsua_core.c
===================================================================
--- pjproject/branches/projects/aps-direct/pjsip/src/pjsua-lib/pjsua_core.c	(revision 2492)
+++ pjproject/branches/projects/aps-direct/pjsip/src/pjsua-lib/pjsua_core.c	(revision 2493)
@@ -181,5 +181,5 @@
     cfg->snd_play_latency = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
     cfg->jb_init = cfg->jb_min_pre = cfg->jb_max_pre = cfg->jb_max = -1;
-    cfg->snd_auto_close_time = -1;
+    cfg->snd_auto_close_time = 1;
 
     cfg->turn_conn_type = PJ_TURN_TP_UDP;
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 2492)
+++ pjproject/branches/projects/aps-direct/pjsip/src/pjsua-lib/pjsua_media.c	(revision 2493)
@@ -36,6 +36,16 @@
 static pj_uint16_t next_rtp_port;
 
+/* Open sound dev */
+static pj_status_t open_snd_dev(pjmedia_aud_param *param);
 /* Close existing sound device */
 static void close_snd_dev(void);
+/* Create audio device param */
+static pj_status_t create_aud_param(pjmedia_aud_param *param,
+				    pjmedia_aud_dev_index capture_dev,
+				    pjmedia_aud_dev_index playback_dev,
+				    unsigned clock_rate,
+				    unsigned channel_count,
+				    unsigned samples_per_frame,
+				    unsigned bits_per_sample);
 
 
@@ -60,4 +70,11 @@
     /* To suppress warning about unused var when all codecs are disabled */
     PJ_UNUSED_ARG(codec_id);
+
+    /* Specify which audio device settings are save-able */
+    pjsua_var.aud_svmask = 0xFFFFFFFF;
+    /* These are not-settable */
+    pjsua_var.aud_svmask &= ~(PJMEDIA_AUD_DEV_CAP_EXT_FORMAT |
+			      PJMEDIA_AUD_DEV_CAP_INPUT_SIGNAL_METER |
+			      PJMEDIA_AUD_DEV_CAP_OUTPUT_SIGNAL_METER);
 
     /* Copy configuration */
@@ -236,4 +253,8 @@
 	return status;
     }
+
+    /* Are we using the audio switchboard (a.k.a APS-Direct)? */
+    pjsua_var.is_mswitch = pjmedia_conf_get_master_port(pjsua_var.mconf)
+			    ->info.signature == PJMEDIA_CONF_SWITCH_SIGNATURE;
 
     /* Create null port just in case user wants to use null sound. */
@@ -1510,128 +1531,4 @@
 }
 
-#if PJMEDIA_CONF_USE_SWITCH_BOARD
-
-/*
- * Open sound device with extended setting.
- */
-static pj_status_t open_snd_dev_ext(pjmedia_aud_param *param)
-{
-    pjmedia_port *conf_port;
-    pj_status_t status;
-
-    PJ_ASSERT_RETURN(param, PJ_EINVAL);
-
-    /* Check if NULL sound device is used */
-    if (NULL_SND_DEV_ID==param->rec_id || NULL_SND_DEV_ID==param->play_id) {
-	return pjsua_set_null_snd_dev();
-    }
-
-    /* Close existing sound port */
-    close_snd_dev();
-
-    /* Create memory pool for sound device. */
-    pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000);
-    PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM);
-
-
-    /* Get the port0 of the conference bridge. */
-    conf_port = pjmedia_conf_get_master_port(pjsua_var.mconf);
-    pj_assert(conf_port != NULL);
-
-    PJ_LOG(4,(THIS_FILE, "Opening sound device @%d/%d/%s",
-	      param->clock_rate, param->channel_count,
-	      (param->ext_fmt.id==PJMEDIA_FORMAT_L16?"pcm":"encoded")));
-
-    status = pjmedia_snd_port_create2( pjsua_var.snd_pool, 
-				       param,  &pjsua_var.snd_port);
-
-    /* Update port 0 info when sound dev opened successfully. */
-    if (status == PJ_SUCCESS) {
-	pj_memcpy(&conf_port->info.format, &param->ext_fmt, 
-		  sizeof(conf_port->info.format));
-	conf_port->info.clock_rate = param->clock_rate;
-	conf_port->info.samples_per_frame = param->samples_per_frame;
-	conf_port->info.channel_count = param->channel_count;
-	conf_port->info.bits_per_sample = 16;
-    } else {
-	pjsua_perror(THIS_FILE, "Unable to open sound device", status);
-	return status;
-    }
-
-    /* Connect sound port to the bridge */
-    status = pjmedia_snd_port_connect(pjsua_var.snd_port, 	 
-				      conf_port ); 	 
-    if (status != PJ_SUCCESS) { 	 
-	pjsua_perror(THIS_FILE, "Unable to connect conference port to "
-			        "sound device", status); 	 
-	pjmedia_snd_port_destroy(pjsua_var.snd_port); 	 
-	pjsua_var.snd_port = NULL; 	 
-	return status; 	 
-    }
-
-    /* Save the device IDs */
-    pjsua_var.cap_dev = param->rec_id;
-    pjsua_var.play_dev = param->play_id;
-
-    /* Update sound device name. */
-    {
-	pjmedia_aud_dev_info play_info;
-	pjmedia_aud_stream *strm;
-	pjmedia_aud_param si;
-        pj_str_t tmp;
-
-	strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
-	pjmedia_aud_stream_get_param(strm, &si);
-	if (pjmedia_aud_dev_get_info(si.play_id, &play_info)==PJ_SUCCESS) {
-	    pjmedia_conf_set_port0_name(pjsua_var.mconf, 
-					pj_cstr(&tmp, play_info.name));
-	}
-    };
-
-    return PJ_SUCCESS;
-}
-
-#endif
-
-
-/* Close existing sound device */
-static void close_snd_dev(void)
-{
-    /* Close sound device */
-    if (pjsua_var.snd_port) {
-	pjmedia_aud_dev_info cap_info, play_info;
-	pjmedia_aud_stream *strm;
-	pjmedia_aud_param param;
-
-	strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
-	pjmedia_aud_stream_get_param(strm, &param);
-
-	if (pjmedia_aud_dev_get_info(param.rec_id, &cap_info) != PJ_SUCCESS)
-	    cap_info.name[0] = '\0';
-	if (pjmedia_aud_dev_get_info(param.play_id, &play_info) != PJ_SUCCESS)
-	    play_info.name[0] = '\0';
-
-	PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
-			     "%s sound capture device",
-			     play_info.name, cap_info.name));
-
-	pjmedia_snd_port_disconnect(pjsua_var.snd_port);
-	pjmedia_snd_port_destroy(pjsua_var.snd_port);
-	pjsua_var.snd_port = NULL;
-    }
-
-    /* Close null sound device */
-    if (pjsua_var.null_snd) {
-	PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
-	pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
-	pjsua_var.null_snd = NULL;
-    }
-
-    if (pjsua_var.snd_pool) 
-	pj_pool_release(pjsua_var.snd_pool);
-    pjsua_var.snd_pool = NULL;
-}
-
-
 /*
  * Get maxinum number of conference ports.
@@ -1748,12 +1645,12 @@
     }
 
-#if PJMEDIA_CONF_USE_SWITCH_BOARD
-
-    /* Check if sound device need to be reopened, i.e: its attributes
+
+    /* For audio switchboard (i.e. APS-Direct):
+     * Check if sound device need to be reopened, i.e: its attributes
      * (format, clock rate, channel count) must match to peer's. 
      * Note that sound device can be reopened only if it doesn't have
      * any connection.
      */
-    do {
+    if (pjsua_var.is_mswitch) {
 	pjmedia_conf_port_info port0_info;
 	pjmedia_conf_port_info peer_info;
@@ -1790,39 +1687,19 @@
 	    pjmedia_aud_param param;
 
-	    status = pjmedia_aud_dev_default_param(pjsua_var.cap_dev, &param);
-	    if (status != PJ_SUCCESS) {
-		pjsua_perror(THIS_FILE, "Error retrieving default audio "
-					"device parameters", status);
-		return status;
+	    /* Create parameter based on peer info */
+	    status = create_aud_param(&param, pjsua_var.cap_dev, 
+				      pjsua_var.play_dev,
+				      peer_info.clock_rate,
+				      peer_info.channel_count,
+				      peer_info.samples_per_frame,
+				      peer_info.bits_per_sample);
+
+	    /* And peer format */
+	    if (peer_info.format.id != PJMEDIA_FORMAT_PCM) {
+		param.flags |= PJMEDIA_AUD_DEV_CAP_EXT_FORMAT;
+		param.ext_fmt = peer_info.format;
 	    }
 
-	    param.dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
-	    param.rec_id = pjsua_var.cap_dev;
-	    param.play_id = pjsua_var.play_dev;
-	    param.clock_rate = peer_info.clock_rate;
-	    param.samples_per_frame = peer_info.samples_per_frame;
-	    param.channel_count = peer_info.channel_count;
-	    param.bits_per_sample = peer_info.bits_per_sample;
-	    /* Latency setting */
-	    param.flags |= (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY | 
-			    PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY);
-	    param.input_latency_ms = pjsua_var.media_cfg.snd_rec_latency;
-	    param.output_latency_ms = pjsua_var.media_cfg.snd_play_latency;
-	    /* EC settings */
-	    if (pjsua_var.media_cfg.ec_tail_len) {
-		param.flags |= (PJMEDIA_AUD_DEV_CAP_EC | PJMEDIA_AUD_DEV_CAP_EC_TAIL);
-		param.ec_enabled = PJ_TRUE;
-		param.ec_tail_ms = pjsua_var.media_cfg.ec_tail_len;
-	    } else {
-		param.flags &= ~(PJMEDIA_AUD_DEV_CAP_EC | PJMEDIA_AUD_DEV_CAP_EC_TAIL);
-	    }
-	    /* Format */
-	    param.flags |= (PJMEDIA_AUD_DEV_CAP_EXT_FORMAT | 
-			    PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE);
-	    param.ext_fmt = peer_info.format;
-	    //param.plc = PJ_FALSE;
-	    param.out_route = PJMEDIA_AUD_DEV_ROUTE_DEFAULT;
-	    
-	    status = open_snd_dev_ext(&param);
+	    status = open_snd_dev(&param);
 	    if (status != PJ_SUCCESS) {
 		pjsua_perror(THIS_FILE, "Error opening sound device", status);
@@ -1830,22 +1707,22 @@
 	    }
 	}
-    } while(0);
-
-#else
-
-    /* Create sound port if none is instantiated */
-    if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && 
-	!pjsua_var.no_snd) 
-    {
-	pj_status_t status;
-
-	status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
-	if (status != PJ_SUCCESS) {
-	    pjsua_perror(THIS_FILE, "Error opening sound device", status);
-	    return status;
-	}
-    }
-
-#endif
+
+    } else {
+	/* The bridge version */
+
+	/* Create sound port if none is instantiated */
+	if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && 
+	    !pjsua_var.no_snd) 
+	{
+	    pj_status_t status;
+
+	    status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
+	    if (status != PJ_SUCCESS) {
+		pjsua_perror(THIS_FILE, "Error opening sound device", status);
+		return status;
+	    }
+	}
+
+    }
 
     return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
@@ -2323,6 +2200,6 @@
  * Enum sound devices.
  */
-#if PJMEDIA_AUDIO_API==PJMEDIA_AUDIO_API_NEW_ONLY
-PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_aud_dev_info info[],
+
+PJ_DEF(pj_status_t) pjsua_enum_aud_devs( pjmedia_aud_dev_info info[],
 					 unsigned *count)
 {
@@ -2345,5 +2222,6 @@
     return PJ_SUCCESS;
 }
-#else	/* PJMEDIA_AUDIO_API */
+
+
 PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
 					 unsigned *count)
@@ -2375,21 +2253,22 @@
     return PJ_SUCCESS;
 }
-#endif
-
-
-/*
- * Select or change sound device. Application may call this function at
- * any time to replace current sound device.
- */
-PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
-				       int playback_dev)
-{
-#if PJMEDIA_CONF_USE_SWITCH_BOARD
-
-    pjmedia_aud_param param;
+
+/* Create audio device parameter to open the device */
+static pj_status_t create_aud_param(pjmedia_aud_param *param,
+				    pjmedia_aud_dev_index capture_dev,
+				    pjmedia_aud_dev_index playback_dev,
+				    unsigned clock_rate,
+				    unsigned channel_count,
+				    unsigned samples_per_frame,
+				    unsigned bits_per_sample)
+{
     pj_status_t status;
 
+    /* Normalize device ID with new convention about default device ID */
+    if (playback_dev == PJMEDIA_AUD_DEFAULT_CAPTURE_DEV)
+	playback_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;
+
     /* Create default parameters for the device */
-    status = pjmedia_aud_dev_default_param(capture_dev, &param);
+    status = pjmedia_aud_dev_default_param(capture_dev, param);
     if (status != PJ_SUCCESS) {
 	pjsua_perror(THIS_FILE, "Error retrieving default audio "
@@ -2397,44 +2276,102 @@
 	return status;
     }
-    param.dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
-    param.rec_id = capture_dev;
-    param.play_id = playback_dev;
-    param.channel_count = pjsua_var.media_cfg.channel_count;
+    param->dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
+    param->rec_id = capture_dev;
+    param->play_id = playback_dev;
+    param->clock_rate = clock_rate;
+    param->channel_count = channel_count;
+    param->samples_per_frame = samples_per_frame;
+    param->bits_per_sample = bits_per_sample;
+
+    /* Update the setting with user preference */
+#define update_param(cap, field)    \
+	if (pjsua_var.aud_param.flags & cap) { \
+	    param->flags |= cap; \
+	    param->field = pjsua_var.aud_param.field; \
+	}
+    update_param( PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol);
+    update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol);
+    update_param( PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route);
+    update_param( PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route);
+#undef update_param
+
     /* Latency settings */
-    param.flags |= (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY | 
-		    PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY);
-    param.input_latency_ms = pjsua_var.media_cfg.snd_rec_latency;
-    param.output_latency_ms = pjsua_var.media_cfg.snd_play_latency;
+    param->flags |= (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY | 
+		     PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY);
+    param->input_latency_ms = pjsua_var.media_cfg.snd_rec_latency;
+    param->output_latency_ms = pjsua_var.media_cfg.snd_play_latency;
+
     /* EC settings */
     if (pjsua_var.media_cfg.ec_tail_len) {
-	param.flags |= (PJMEDIA_AUD_DEV_CAP_EC | PJMEDIA_AUD_DEV_CAP_EC_TAIL);
-	param.ec_enabled = PJ_TRUE;
-	param.ec_tail_ms = pjsua_var.media_cfg.ec_tail_len;
+	param->flags |= (PJMEDIA_AUD_DEV_CAP_EC | PJMEDIA_AUD_DEV_CAP_EC_TAIL);
+	param->ec_enabled = PJ_TRUE;
+	param->ec_tail_ms = pjsua_var.media_cfg.ec_tail_len;
     } else {
-	param.flags &= ~(PJMEDIA_AUD_DEV_CAP_EC | PJMEDIA_AUD_DEV_CAP_EC_TAIL);
-    }
-
-    return open_snd_dev_ext(&param);
-
-#else
-
+	param->flags &= ~(PJMEDIA_AUD_DEV_CAP_EC|PJMEDIA_AUD_DEV_CAP_EC_TAIL);
+    }
+
+    return PJ_SUCCESS;
+}
+
+/* Internal: the first time the audio device is opened (during app
+ *   startup), retrieve the audio settings such as volume level
+ *   so that aud_get_settings() will work.
+ */
+static pj_status_t update_initial_aud_param()
+{
+    pjmedia_aud_stream *strm;
+    pjmedia_aud_param param;
+    pj_status_t status;
+
+    PJ_ASSERT_RETURN(pjsua_var.snd_port != NULL, PJ_EBUG);
+
+    strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
+
+    status = pjmedia_aud_stream_get_param(strm, &param);
+    if (status != PJ_SUCCESS) {
+	pjsua_perror(THIS_FILE, "Error audio stream "
+				"device parameters", status);
+	return status;
+    }
+
+#define update_saved_param(cap, field)  \
+	if (param.flags & cap) { \
+	    pjsua_var.aud_param.flags |= cap; \
+	    pjsua_var.aud_param.field = param.field; \
+	}
+
+    update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_VOLUME_SETTING, input_vol);
+    update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, output_vol);
+    update_saved_param(PJMEDIA_AUD_DEV_CAP_INPUT_ROUTE, input_route);
+    update_saved_param(PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, output_route);
+#undef update_saved_param
+
+    return PJ_SUCCESS;
+}
+
+/* Get format name */
+static const char *get_fmt_name(pj_uint32_t id)
+{
+    static char name[8];
+
+    if (id == PJMEDIA_FORMAT_L16)
+	return "PCM";
+    pj_memcpy(name, &id, 4);
+    name[4] = '\0';
+    return name;
+}
+
+/* Open sound device with the setting. */
+static pj_status_t open_snd_dev(pjmedia_aud_param *param)
+{
     pjmedia_port *conf_port;
-    pjmedia_aud_dev_info play_info;
-    pjmedia_aud_param param;
-    unsigned clock_rates[] = {0, 44100, 48000, 32000, 16000, 8000};
-    unsigned selected_clock_rate = 0;
-    unsigned i;
-    pjmedia_aud_stream *strm;
-    pj_str_t tmp;
-    pj_status_t status = -1;
+    pj_status_t status;
+
+    PJ_ASSERT_RETURN(param, PJ_EINVAL);
 
     /* Check if NULL sound device is used */
-    if (NULL_SND_DEV_ID == capture_dev || NULL_SND_DEV_ID == playback_dev) {
+    if (NULL_SND_DEV_ID==param->rec_id || NULL_SND_DEV_ID==param->play_id) {
 	return pjsua_set_null_snd_dev();
     }
-
-    /* Normalize device ID with new convention about default device ID */
-    if (playback_dev == PJMEDIA_AUD_DEFAULT_CAPTURE_DEV)
-	playback_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;
 
     /* Close existing sound port */
@@ -2442,11 +2379,18 @@
 
     /* Create memory pool for sound device. */
-    pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 1000, 1000);
+    pjsua_var.snd_pool = pjsua_pool_create("pjsua_snd", 4000, 4000);
     PJ_ASSERT_RETURN(pjsua_var.snd_pool, PJ_ENOMEM);
 
-    /* Set default clock rate */
-    clock_rates[0] = pjsua_var.media_cfg.snd_clock_rate;
-    if (clock_rates[0] == 0)
-	clock_rates[0] = pjsua_var.media_cfg.clock_rate;
+
+    PJ_LOG(4,(THIS_FILE, "Opening sound device %s@%d/%d/%dms",
+	      get_fmt_name(param->ext_fmt.id),
+	      param->clock_rate, param->channel_count,
+	      param->samples_per_frame / param->channel_count * 1000 /
+	      param->clock_rate));
+
+    status = pjmedia_snd_port_create2( pjsua_var.snd_pool, 
+				       param,  &pjsua_var.snd_port);
+    if (status != PJ_SUCCESS)
+	return status;
 
     /* Get the port0 of the conference bridge. */
@@ -2454,99 +2398,54 @@
     pj_assert(conf_port != NULL);
 
-    /* Create default parameters for the device */
-    status = pjmedia_aud_dev_default_param(capture_dev, &param);
-    if (status != PJ_SUCCESS) {
-	pjsua_perror(THIS_FILE, "Error retrieving default audio "
-				"device parameters", status);
-	return status;
-    }
-    param.dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
-    param.rec_id = capture_dev;
-    param.play_id = playback_dev;
-    param.channel_count = pjsua_var.media_cfg.channel_count;
-    /* Latency settings */
-    param.flags |= (PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY | 
-		    PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY);
-    param.input_latency_ms = pjsua_var.media_cfg.snd_rec_latency;
-    param.output_latency_ms = pjsua_var.media_cfg.snd_play_latency;
-    /* EC settings */
-    if (pjsua_var.media_cfg.ec_tail_len) {
-	param.flags |= (PJMEDIA_AUD_DEV_CAP_EC | PJMEDIA_AUD_DEV_CAP_EC_TAIL);
-	param.ec_enabled = PJ_TRUE;
-	param.ec_tail_ms = pjsua_var.media_cfg.ec_tail_len;
-    } else {
-	param.flags &= ~(PJMEDIA_AUD_DEV_CAP_EC | PJMEDIA_AUD_DEV_CAP_EC_TAIL);
-    }
-
-    /* Attempts to open the sound device with different clock rates */
-    for (i=0; i<PJ_ARRAY_SIZE(clock_rates); ++i) {
-	char errmsg[PJ_ERR_MSG_SIZE];
-
-	PJ_LOG(4,(THIS_FILE, 
-		  "pjsua_set_snd_dev(): attempting to open devices "
-		  "@%d Hz", clock_rates[i]));
-
-	param.clock_rate = clock_rates[i];
-	param.samples_per_frame = clock_rates[i] *
-				  pjsua_var.media_cfg.audio_frame_ptime *
-				  pjsua_var.media_cfg.channel_count / 1000;
-
-	/* Create the sound device. Sound port will start immediately. */
-	status = pjmedia_snd_port_create2(pjsua_var.snd_pool, &param,
-					  &pjsua_var.snd_port);
-
-	if (status == PJ_SUCCESS) {
-	    selected_clock_rate = clock_rates[i];
-
-	    /* If there's mismatch between sound port and conference's port,
-	     * create a resample port to bridge them.
-	     */
-	    if (selected_clock_rate != pjsua_var.media_cfg.clock_rate) {
-		pjmedia_port *resample_port;
-		unsigned resample_opt = 0;
-
-		if (pjsua_var.media_cfg.quality >= 3 &&
-		    pjsua_var.media_cfg.quality <= 4)
-		{
-		    resample_opt |= PJMEDIA_RESAMPLE_USE_SMALL_FILTER;
-		}
-		else if (pjsua_var.media_cfg.quality < 3) {
-		    resample_opt |= PJMEDIA_RESAMPLE_USE_LINEAR;
-		}
-		
-		status = pjmedia_resample_port_create(pjsua_var.snd_pool, 
-						      conf_port,
-						      selected_clock_rate,
-						      resample_opt, 
-						      &resample_port);
-		if (status != PJ_SUCCESS) {
-		    pj_strerror(status, errmsg, sizeof(errmsg));
-		    PJ_LOG(4, (THIS_FILE, 
-			       "Error creating resample port, trying next "
-			       "clock rate", 
-			       errmsg));
-		    pjmedia_snd_port_destroy(pjsua_var.snd_port);
-		    pjsua_var.snd_port = NULL;
-		    continue;
-		} else {
-		    conf_port = resample_port;
-		    break;
-		}
-
-	    } else {
-		break;
-	    }
-	}
-
-	pj_strerror(status, errmsg, sizeof(errmsg));
-	PJ_LOG(4, (THIS_FILE, "..failed: %s", errmsg));
-    }
-
-    if (status != PJ_SUCCESS) {
-	pjsua_perror(THIS_FILE, "Unable to open sound device", status);
-	return status;
-    }
-
-    /* Connect sound port to the bridge */ 	 
+    /* For conference bridge, resample if necessary if the bridge's
+     * clock rate is different than the sound device's clock rate.
+     */
+    if (!pjsua_var.is_mswitch &&
+	param->ext_fmt.id == PJMEDIA_FORMAT_PCM &&
+	conf_port->info.clock_rate != param->clock_rate)
+    {
+	pjmedia_port *resample_port;
+	unsigned resample_opt = 0;
+
+	if (pjsua_var.media_cfg.quality >= 3 &&
+	    pjsua_var.media_cfg.quality <= 4)
+	{
+	    resample_opt |= PJMEDIA_RESAMPLE_USE_SMALL_FILTER;
+	}
+	else if (pjsua_var.media_cfg.quality < 3) {
+	    resample_opt |= PJMEDIA_RESAMPLE_USE_LINEAR;
+	}
+	
+	status = pjmedia_resample_port_create(pjsua_var.snd_pool, 
+					      conf_port,
+					      param->clock_rate,
+					      resample_opt, 
+					      &resample_port);
+	if (status != PJ_SUCCESS) {
+	    char errmsg[PJ_ERR_MSG_SIZE];
+	    pj_strerror(status, errmsg, sizeof(errmsg));
+	    PJ_LOG(4, (THIS_FILE, 
+		       "Error creating resample port: %s", 
+		       errmsg));
+	    close_snd_dev();
+	    return status;
+	} 
+	    
+	conf_port = resample_port;
+    }
+
+    /* Otherwise for audio switchboard, the switch's port0 setting is
+     * derived from the sound device setting, so update the setting.
+     */
+    if (pjsua_var.is_mswitch) {
+	pj_memcpy(&conf_port->info.format, &param->ext_fmt, 
+		  sizeof(conf_port->info.format));
+	conf_port->info.clock_rate = param->clock_rate;
+	conf_port->info.samples_per_frame = param->samples_per_frame;
+	conf_port->info.channel_count = param->channel_count;
+	conf_port->info.bits_per_sample = 16;
+    }
+
+    /* Connect sound port to the bridge */
     status = pjmedia_snd_port_connect(pjsua_var.snd_port, 	 
 				      conf_port ); 	 
@@ -2560,29 +2459,146 @@
 
     /* Save the device IDs */
-    pjsua_var.cap_dev = capture_dev;
-    pjsua_var.play_dev = playback_dev;
+    pjsua_var.cap_dev = param->rec_id;
+    pjsua_var.play_dev = param->play_id;
 
     /* Update sound device name. */
-    strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
-    pjmedia_aud_stream_get_param(strm, &param);
-    pjmedia_aud_dev_get_info(param.play_id, &play_info);
-
-    if (param.clock_rate != pjsua_var.media_cfg.clock_rate) {
-	char tmp_buf[128];
-	int tmp_buf_len = sizeof(tmp_buf);
-
-	tmp_buf_len = pj_ansi_snprintf(tmp_buf, sizeof(tmp_buf)-1, "%s (%dKHz)",
-				       play_info.name, param.clock_rate/1000);
-	pj_strset(&tmp, tmp_buf, tmp_buf_len);
-        pjmedia_conf_set_port0_name(pjsua_var.mconf, &tmp); 
+    {
+	pjmedia_aud_dev_info rec_info;
+	pjmedia_aud_stream *strm;
+	pjmedia_aud_param si;
+        pj_str_t tmp;
+
+	strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
+	status = pjmedia_aud_stream_get_param(strm, &si);
+	if (status == PJ_SUCCESS)
+	    status = pjmedia_aud_dev_get_info(si.rec_id, &rec_info);
+
+	if (status==PJ_SUCCESS) {
+	    if (param->clock_rate != pjsua_var.media_cfg.clock_rate) {
+		char tmp_buf[128];
+		int tmp_buf_len = sizeof(tmp_buf);
+
+		tmp_buf_len = pj_ansi_snprintf(tmp_buf, sizeof(tmp_buf)-1, 
+					       "%s (%dKHz)",
+					       rec_info.name, 
+					       param->clock_rate/1000);
+		pj_strset(&tmp, tmp_buf, tmp_buf_len);
+		pjmedia_conf_set_port0_name(pjsua_var.mconf, &tmp); 
+	    } else {
+		pjmedia_conf_set_port0_name(pjsua_var.mconf, 
+					    pj_cstr(&tmp, rec_info.name));
+	    }
+	}
+
+	/* Any error is not major, let it through */
+	status = PJ_SUCCESS;
+    };
+
+    /* If this is the first time the audio device is open, retrieve some
+     * settings from the device (such as volume settings) so that the
+     * pjsua_snd_get_setting() work.
+     */
+    if (pjsua_var.aud_open_cnt == 0) {
+	update_initial_aud_param();
+	++pjsua_var.aud_open_cnt;
+    }
+
+    return PJ_SUCCESS;
+}
+
+
+/* Close existing sound device */
+static void close_snd_dev(void)
+{
+    /* Close sound device */
+    if (pjsua_var.snd_port) {
+	pjmedia_aud_dev_info cap_info, play_info;
+	pjmedia_aud_stream *strm;
+	pjmedia_aud_param param;
+
+	strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
+	pjmedia_aud_stream_get_param(strm, &param);
+
+	if (pjmedia_aud_dev_get_info(param.rec_id, &cap_info) != PJ_SUCCESS)
+	    cap_info.name[0] = '\0';
+	if (pjmedia_aud_dev_get_info(param.play_id, &play_info) != PJ_SUCCESS)
+	    play_info.name[0] = '\0';
+
+	PJ_LOG(4,(THIS_FILE, "Closing %s sound playback device and "
+			     "%s sound capture device",
+			     play_info.name, cap_info.name));
+
+	pjmedia_snd_port_disconnect(pjsua_var.snd_port);
+	pjmedia_snd_port_destroy(pjsua_var.snd_port);
+	pjsua_var.snd_port = NULL;
+    }
+
+    /* Close null sound device */
+    if (pjsua_var.null_snd) {
+	PJ_LOG(4,(THIS_FILE, "Closing null sound device.."));
+	pjmedia_master_port_destroy(pjsua_var.null_snd, PJ_FALSE);
+	pjsua_var.null_snd = NULL;
+    }
+
+    if (pjsua_var.snd_pool) 
+	pj_pool_release(pjsua_var.snd_pool);
+    pjsua_var.snd_pool = NULL;
+}
+
+
+/*
+ * Select or change sound device. Application may call this function at
+ * any time to replace current sound device.
+ */
+PJ_DEF(pj_status_t) pjsua_set_snd_dev( int capture_dev,
+				       int playback_dev)
+{
+    unsigned alt_cr_cnt = 1;
+    unsigned alt_cr[] = {0, 44100, 48000, 32000, 16000, 8000};
+    unsigned i;
+    pj_status_t status = -1;
+
+    /* Set default clock rate */
+    alt_cr[0] = pjsua_var.media_cfg.snd_clock_rate;
+    if (alt_cr[0] == 0)
+	alt_cr[0] = pjsua_var.media_cfg.clock_rate;
+
+    /* Allow retrying of different clock rate if we're using conference 
+     * bridge (meaning audio format is always PCM), otherwise lock on
+     * to one clock rate.
+     */
+    if (pjsua_var.is_mswitch) {
+	alt_cr_cnt = 1;
     } else {
-        pjmedia_conf_set_port0_name(pjsua_var.mconf, 
-				    pj_cstr(&tmp, play_info.name));
-    }
-
-    return PJ_SUCCESS;
-
-#endif /* PJMEDIA_CONF_USE_SWITCH_BOARD */
-
+	alt_cr_cnt = PJ_ARRAY_SIZE(alt_cr);
+    }
+
+    /* Attempts to open the sound device with different clock rates */
+    for (i=0; i<alt_cr_cnt; ++i) {
+	pjmedia_aud_param param;
+	unsigned samples_per_frame;
+
+	/* Create the default audio param */
+	samples_per_frame = alt_cr[i] *
+			    pjsua_var.media_cfg.audio_frame_ptime *
+			    pjsua_var.media_cfg.channel_count / 1000;
+	status = create_aud_param(&param, capture_dev, playback_dev, 
+				  alt_cr[i], pjsua_var.media_cfg.channel_count,
+				  samples_per_frame, 16);
+	if (status != PJ_SUCCESS)
+	    return status;
+
+	/* Open! */
+	status = open_snd_dev(&param);
+	if (status == PJ_SUCCESS)
+	    break;
+    }
+
+    if (status != PJ_SUCCESS) {
+	pjsua_perror(THIS_FILE, "Unable to open sound device", status);
+	return status;
+    }
+
+    return PJ_SUCCESS;
 }
 
@@ -2690,14 +2706,69 @@
 
 /*
- * Get active sound port instance.
- */
-PJ_DEF(pjmedia_aud_stream*) pjsua_get_aud_stream()
-{
-    pjmedia_aud_stream *aud_stream = NULL;
-
-    if (pjsua_var.snd_port)
-	aud_stream = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
-
-    return aud_stream;
+ * Check whether the sound device is currently active.
+ */
+PJ_DEF(pj_bool_t) pjsua_snd_is_active(void)
+{
+    return pjsua_var.snd_port != NULL;
+}
+
+
+/*
+ * Configure sound device setting to the sound device being used. 
+ */
+PJ_DEF(pj_status_t) pjsua_snd_set_setting( pjmedia_aud_dev_cap cap,
+					   const void *pval,
+					   pj_bool_t keep)
+{
+    /* Check if we are allowed to set the cap */
+    if (cap & pjsua_var.aud_svmask) {
+	return PJMEDIA_EAUD_INVCAP;
+    }
+
+    if (keep) {
+	/* Save in internal param for later device open */
+	pj_status_t status;
+
+	status = pjmedia_aud_param_set_cap(&pjsua_var.aud_param,
+					   cap, pval);
+	if (status != PJ_SUCCESS)
+	    return status;
+    }
+
+    if (pjsua_snd_is_active()) {
+	/* Sound is active, set it immediately */
+	pjmedia_aud_stream *strm;
+	
+	strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
+	return pjmedia_aud_stream_set_cap(strm, cap, pval);
+    } else {
+	return PJ_SUCCESS;
+    }
+}
+
+/*
+ * Retrieve a sound device setting.
+ */
+PJ_DEF(pj_status_t) pjsua_snd_get_setting( pjmedia_aud_dev_cap cap,
+					   void *pval)
+{
+    /* If sound device has never been opened before, open it to 
+     * retrieve the initial setting from the device (e.g. audio
+     * volume)
+     */
+    if (pjsua_var.aud_open_cnt==0)
+	pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
+
+    if (pjsua_snd_is_active()) {
+	/* Sound is active, retrieve from device directly */
+	pjmedia_aud_stream *strm;
+	
+	strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
+	return pjmedia_aud_stream_get_cap(strm, cap, pval);
+    } else {
+	/* Otherwise retrieve from internal param */
+	return pjmedia_aud_param_get_cap(&pjsua_var.aud_param,
+					 cap, pval);
+    }
 }
 
