Index: pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 2425)
+++ pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 2506)
@@ -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,14 @@
     /* 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);
+    /* EC settings use different API */
+    pjsua_var.aud_svmask &= ~(PJMEDIA_AUD_DEV_CAP_EC |
+			      PJMEDIA_AUD_DEV_CAP_EC_TAIL);
 
     /* Copy configuration */
@@ -172,4 +192,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
@@ -226,4 +256,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. */
@@ -574,4 +608,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
@@ -1496,5 +1534,4 @@
 }
 
-
 /*
  * Get maxinum number of conference ports.
@@ -1611,15 +1648,83 @@
     }
 
-    /* Create sound port if none is instantiated */
-    if (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && 
-	!pjsua_var.no_snd) 
-    {
+
+    /* 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.
+     */
+    if (pjsua_var.is_mswitch) {
+	pjmedia_conf_port_info port0_info;
+	pjmedia_conf_port_info peer_info;
+	unsigned peer_id;
+	pj_bool_t need_reopen = PJ_FALSE;
 	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;
-	}
+	peer_id = (source!=0)? source : sink;
+	status = pjmedia_conf_get_port_info(pjsua_var.mconf, peer_id, 
+					    &peer_info);
+	pj_assert(status == PJ_SUCCESS);
+
+	status = pjmedia_conf_get_port_info(pjsua_var.mconf, 0, &port0_info);
+	pj_assert(status == PJ_SUCCESS);
+
+	/* Check if sound device is instantiated. */
+	need_reopen = (pjsua_var.snd_port==NULL && pjsua_var.null_snd==NULL && 
+		      !pjsua_var.no_snd);
+
+	/* Check if sound device need to reopen because it needs to modify 
+	 * settings to match its peer. Sound device must be idle in this case 
+	 * though.
+	 */
+	if (!need_reopen && 
+	    port0_info.listener_cnt==0 && port0_info.transmitter_cnt==0) 
+	{
+	    need_reopen = (peer_info.format.id != port0_info.format.id ||
+			   peer_info.format.bitrate != port0_info.format.bitrate ||
+			   peer_info.clock_rate != port0_info.clock_rate ||
+			   peer_info.channel_count != port0_info.channel_count);
+	}
+
+	if (need_reopen) {
+	    pjmedia_aud_param param;
+
+	    /* 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;
+	    }
+
+	    status = open_snd_dev(&param);
+	    if (status != PJ_SUCCESS) {
+		pjsua_perror(THIS_FILE, "Error opening sound device", status);
+		return status;
+	    }
+	}
+
+    } 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;
+	    }
+	}
+
     }
 
@@ -2098,4 +2203,28 @@
  * Enum sound devices.
  */
+
+PJ_DEF(pj_status_t) pjsua_enum_aud_devs( pjmedia_aud_dev_info info[],
+					 unsigned *count)
+{
+    unsigned i, dev_count;
+
+    dev_count = pjmedia_aud_dev_count();
+    
+    if (dev_count > *count) dev_count = *count;
+
+    for (i=0; i<dev_count; ++i) {
+	pj_status_t status;
+
+	status = pjmedia_aud_dev_get_info(i, &info[i]);
+	if (status != PJ_SUCCESS)
+	    return status;
+    }
+
+    *count = dev_count;
+
+    return PJ_SUCCESS;
+}
+
+
 PJ_DEF(pj_status_t) pjsua_enum_snd_devs( pjmedia_snd_dev_info info[],
 					 unsigned *count)
@@ -2103,13 +2232,22 @@
     unsigned i, dev_count;
 
-    dev_count = pjmedia_snd_get_dev_count();
+    dev_count = pjmedia_aud_dev_count();
     
     if (dev_count > *count) dev_count = *count;
+    pj_bzero(info, dev_count * sizeof(pjmedia_snd_dev_info));
 
     for (i=0; i<dev_count; ++i) {
-	const pjmedia_snd_dev_info *ci;
-
-	ci = pjmedia_snd_get_dev_info(i);
-	pj_memcpy(&info[i], ci, sizeof(*ci));
+	pjmedia_aud_dev_info ai;
+	pj_status_t status;
+
+	status = pjmedia_aud_dev_get_info(i, &ai);
+	if (status != PJ_SUCCESS)
+	    return status;
+
+	strncpy(info[i].name, ai.name, sizeof(info[i].name));
+	info[i].name[sizeof(info[i].name)-1] = '\0';
+	info[i].input_count = ai.input_count;
+	info[i].output_count = ai.output_count;
+	info[i].default_samples_per_sec = ai.default_samples_per_sec;
     }
 
@@ -2119,55 +2257,122 @@
 }
 
-
-/* Close existing sound device */
-static void close_snd_dev(void)
-{
-    /* Close sound device */
-    if (pjsua_var.snd_port) {
-	const pjmedia_snd_dev_info *cap_info, *play_info;
-
-	cap_info = pjmedia_snd_get_dev_info(pjsua_var.cap_dev);
-	play_info = pjmedia_snd_get_dev_info(pjsua_var.play_dev);
-
-	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)
+/* 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);
+    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->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;
+
+    /* 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);
+    }
+
+    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;
-    const pjmedia_snd_dev_info *play_info;
-    unsigned clock_rates[] = {0, 44100, 48000, 32000, 16000, 8000};
-    unsigned selected_clock_rate = 0;
-    unsigned i;
-    pjmedia_snd_stream *strm;
-    pjmedia_snd_stream_info si;
-    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();
     }
@@ -2180,8 +2385,15 @@
     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. */
@@ -2189,83 +2401,54 @@
     pj_assert(conf_port != NULL);
 
-    /* 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];
-	unsigned samples_per_frame;
-
-	PJ_LOG(4,(THIS_FILE, 
-		  "pjsua_set_snd_dev(): attempting to open devices "
-		  "@%d Hz", clock_rates[i]));
-
-	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_create(pjsua_var.snd_pool, capture_dev,
-					 playback_dev, 
-					 clock_rates[i], 
-					 pjsua_var.media_cfg.channel_count,
-					 samples_per_frame,
-					 16, 0, &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;
-    }
-
-    /* Set AEC */
-    pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.snd_pool, 
-			     pjsua_var.media_cfg.ec_tail_len, 
-			     pjsua_var.media_cfg.ec_options);
-
-    /* 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 ); 	 
@@ -2279,23 +2462,143 @@
 
     /* 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_snd_stream_get_info(strm, &si);
-    play_info = pjmedia_snd_get_dev_info(si.rec_id);
-
-    if (si.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, si.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));
+	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;
     }
 
@@ -2405,4 +2708,79 @@
 
 
+/*
+ * 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)
+{
+    pj_status_t status;
+
+    /* Check if we are allowed to set the cap */
+    if ((cap & pjsua_var.aud_svmask) == 0) {
+	return PJMEDIA_EAUD_INVCAP;
+    }
+
+    /* If sound is active, set it immediately */
+    if (pjsua_snd_is_active()) {
+	pjmedia_aud_stream *strm;
+	
+	strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
+	status = pjmedia_aud_stream_set_cap(strm, cap, pval);
+    } else {
+	status = PJ_SUCCESS;
+    }
+
+    if (status != PJ_SUCCESS)
+	return status;
+
+    /* Save in internal param for later device open */
+    if (keep) {
+	status = pjmedia_aud_param_set_cap(&pjsua_var.aud_param,
+					   cap, pval);
+    }
+
+    return status;
+}
+
+/*
+ * 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) {
+	PJ_LOG(4,(THIS_FILE, "Opening sound device to get initial settings"));
+	pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
+	close_snd_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);
+    }
+}
+
+
 /*****************************************************************************
  * Codecs.
