Index: /pjproject/trunk/pjmedia/src/pjmedia/conf_switch.c
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia/conf_switch.c	(revision 2580)
+++ /pjproject/trunk/pjmedia/src/pjmedia/conf_switch.c	(revision 2581)
@@ -474,8 +474,12 @@
     PJ_ASSERT_RETURN(conf && slot<conf->max_ports, PJ_EINVAL);
 
+    pj_mutex_lock(conf->mutex);
+
     /* Port must be valid. */
-    PJ_ASSERT_RETURN(conf->ports[slot] != NULL, PJ_EINVAL);
-
     conf_port = conf->ports[slot];
+    if (conf_port == NULL) {
+	pj_mutex_unlock(conf->mutex);
+	return PJ_EINVAL;
+    }
 
     if (tx != PJMEDIA_PORT_NO_CHANGE)
@@ -484,4 +488,6 @@
     if (rx != PJMEDIA_PORT_NO_CHANGE)
 	conf_port->rx_setting = rx;
+
+    pj_mutex_unlock(conf->mutex);
 
     return PJ_SUCCESS;
@@ -505,8 +511,4 @@
 		     sink_slot<conf->max_ports, PJ_EINVAL);
 
-    /* Ports must be valid. */
-    PJ_ASSERT_RETURN(conf->ports[src_slot] != NULL, PJ_EINVAL);
-    PJ_ASSERT_RETURN(conf->ports[sink_slot] != NULL, PJ_EINVAL);
-
     /* For now, level MUST be zero. */
     PJ_ASSERT_RETURN(level == 0, PJ_EINVAL);
@@ -514,6 +516,11 @@
     pj_mutex_lock(conf->mutex);
 
+    /* Ports must be valid. */
     src_port = conf->ports[src_slot];
     dst_port = conf->ports[sink_slot];
+    if (!src_port || !dst_port) {
+	pj_mutex_unlock(conf->mutex);
+	return PJ_EINVAL;
+    }
 
     /* Format must match. */
@@ -603,12 +610,13 @@
 		     sink_slot<conf->max_ports, PJ_EINVAL);
 
+    pj_mutex_lock(conf->mutex);
+
     /* Ports must be valid. */
-    PJ_ASSERT_RETURN(conf->ports[src_slot] != NULL, PJ_EINVAL);
-    PJ_ASSERT_RETURN(conf->ports[sink_slot] != NULL, PJ_EINVAL);
-
-    pj_mutex_lock(conf->mutex);
-
     src_port = conf->ports[src_slot];
     dst_port = conf->ports[sink_slot];
+    if (!src_port || !dst_port) {
+	pj_mutex_unlock(conf->mutex);
+	return PJ_EINVAL;
+    }
 
     /* Check if connection has been made */
@@ -686,7 +694,4 @@
     PJ_ASSERT_RETURN(conf && port < conf->max_ports, PJ_EINVAL);
 
-    /* Port must be valid. */
-    PJ_ASSERT_RETURN(conf->ports[port] != NULL, PJ_EINVAL);
-
     /* Suspend the sound devices.
      * Don't want to remove port while port is being accessed by sound
@@ -696,5 +701,11 @@
     pj_mutex_lock(conf->mutex);
 
+    /* Port must be valid. */
     conf_port = conf->ports[port];
+    if (conf_port == NULL) {
+	pj_mutex_unlock(conf->mutex);
+	return PJ_EINVAL;
+    }
+
     conf_port->tx_setting = PJMEDIA_PORT_DISABLE;
     conf_port->rx_setting = PJMEDIA_PORT_DISABLE;
@@ -773,4 +784,7 @@
     PJ_ASSERT_RETURN(conf && p_count && ports, PJ_EINVAL);
 
+    /* Lock mutex */
+    pj_mutex_lock(conf->mutex);
+
     for (i=0; i<conf->max_ports && count<*p_count; ++i) {
 	if (!conf->ports[i])
@@ -780,4 +794,7 @@
     }
 
+    /* Unlock mutex */
+    pj_mutex_unlock(conf->mutex);
+
     *p_count = count;
     return PJ_SUCCESS;
@@ -796,8 +813,13 @@
     PJ_ASSERT_RETURN(conf && slot<conf->max_ports, PJ_EINVAL);
 
+    /* Lock mutex */
+    pj_mutex_lock(conf->mutex);
+
     /* Port must be valid. */
-    PJ_ASSERT_RETURN(conf->ports[slot] != NULL, PJ_EINVAL);
-
     conf_port = conf->ports[slot];
+    if (conf_port == NULL) {
+	pj_mutex_unlock(conf->mutex);
+	return PJ_EINVAL;
+    }
 
     pj_bzero(info, sizeof(pjmedia_conf_port_info));
@@ -818,4 +840,7 @@
     info->rx_adj_level = conf_port->rx_adj_level - NORMAL_LEVEL;
 
+    /* Unlock mutex */
+    pj_mutex_unlock(conf->mutex);
+
     return PJ_SUCCESS;
 }
@@ -830,4 +855,7 @@
     PJ_ASSERT_RETURN(conf && size && info, PJ_EINVAL);
 
+    /* Lock mutex */
+    pj_mutex_lock(conf->mutex);
+
     for (i=0; i<conf->max_ports && count<*size; ++i) {
 	if (!conf->ports[i])
@@ -837,4 +865,7 @@
 	++count;
     }
+
+    /* Unlock mutex */
+    pj_mutex_unlock(conf->mutex);
 
     *size = count;
@@ -856,8 +887,13 @@
     PJ_ASSERT_RETURN(conf && slot<conf->max_ports, PJ_EINVAL);
 
+    /* Lock mutex */
+    pj_mutex_lock(conf->mutex);
+
     /* Port must be valid. */
-    PJ_ASSERT_RETURN(conf->ports[slot] != NULL, PJ_EINVAL);
-
     conf_port = conf->ports[slot];
+    if (conf_port == NULL) {
+	pj_mutex_unlock(conf->mutex);
+	return PJ_EINVAL;
+    }
 
     if (tx_level != NULL) {
@@ -867,4 +903,7 @@
     if (rx_level != NULL) 
 	*rx_level = conf_port->rx_level;
+
+    /* Unlock mutex */
+    pj_mutex_unlock(conf->mutex);
 
     return PJ_SUCCESS;
@@ -883,7 +922,4 @@
     /* Check arguments */
     PJ_ASSERT_RETURN(conf && slot<conf->max_ports, PJ_EINVAL);
-
-    /* Port must be valid. */
-    PJ_ASSERT_RETURN(conf->ports[slot] != NULL, PJ_EINVAL);
 
     /* Value must be from -128 to +127 */
@@ -893,5 +929,13 @@
     PJ_ASSERT_RETURN(adj_level >= -128, PJ_EINVAL);
 
+    /* Lock mutex */
+    pj_mutex_lock(conf->mutex);
+
+    /* Port must be valid. */
     conf_port = conf->ports[slot];
+    if (conf_port == NULL) {
+	pj_mutex_unlock(conf->mutex);
+	return PJ_EINVAL;
+    }
 
     /* Level adjustment is applicable only for ports that work with raw PCM. */
@@ -902,4 +946,7 @@
     conf_port->rx_adj_level = adj_level + NORMAL_LEVEL;
 
+    /* Unlock mutex */
+    pj_mutex_unlock(conf->mutex);
+
     return PJ_SUCCESS;
 }
@@ -917,7 +964,4 @@
     /* Check arguments */
     PJ_ASSERT_RETURN(conf && slot<conf->max_ports, PJ_EINVAL);
-
-    /* Port must be valid. */
-    PJ_ASSERT_RETURN(conf->ports[slot] != NULL, PJ_EINVAL);
 
     /* Value must be from -128 to +127 */
@@ -927,5 +971,13 @@
     PJ_ASSERT_RETURN(adj_level >= -128, PJ_EINVAL);
 
+    /* Lock mutex */
+    pj_mutex_lock(conf->mutex);
+
+    /* Port must be valid. */
     conf_port = conf->ports[slot];
+    if (conf_port == NULL) {
+	pj_mutex_unlock(conf->mutex);
+	return PJ_EINVAL;
+    }
 
     /* Level adjustment is applicable only for ports that work with raw PCM. */
@@ -935,4 +987,7 @@
     /* Set normalized adjustment level. */
     conf_port->tx_adj_level = adj_level + NORMAL_LEVEL;
+
+    /* Unlock mutex */
+    pj_mutex_unlock(conf->mutex);
 
     return PJ_SUCCESS;
@@ -1126,5 +1181,5 @@
     unsigned ci, i;
     
-    /* Must lock mutex */
+    /* Lock mutex */
     pj_mutex_lock(conf->mutex);
 
@@ -1370,7 +1425,18 @@
 {
     pjmedia_conf *conf = (pjmedia_conf*) this_port->port_data.pdata;
-    struct conf_port *cport = conf->ports[this_port->port_data.ldata];
+    struct conf_port *cport;
     unsigned j;
     pj_int32_t level = 0;
+
+    /* Lock mutex */
+    pj_mutex_lock(conf->mutex);
+
+    /* Get conf port of this port */
+    cport = conf->ports[this_port->port_data.ldata];
+    if (cport == NULL) {
+	/* Unlock mutex */
+	pj_mutex_unlock(conf->mutex);
+	return PJ_SUCCESS;
+    }
 
     pj_add_timestamp32(&cport->ts_rx, cport->info->samples_per_frame);
@@ -1379,4 +1445,6 @@
     if (cport->rx_setting == PJMEDIA_PORT_DISABLE) {
 	cport->rx_level = 0;
+	/* Unlock mutex */
+	pj_mutex_unlock(conf->mutex);
 	return PJ_SUCCESS;
     }
@@ -1385,4 +1453,6 @@
     if (cport->listener_cnt == 0) {
 	cport->rx_level = 0;
+	/* Unlock mutex */
+	pj_mutex_unlock(conf->mutex);
 	return PJ_SUCCESS;
     }
@@ -1461,4 +1531,7 @@
     }
 
+    /* Unlock mutex */
+    pj_mutex_unlock(conf->mutex);
+
     return PJ_SUCCESS;
 }
