Index: /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_media.c
===================================================================
--- /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_media.c	(revision 3912)
+++ /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_media.c	(revision 3913)
@@ -1985,5 +1985,4 @@
 	pjsua_var.snd_idle_timer.id = PJ_FALSE;
     }
-    PJSUA_UNLOCK();
 
 
@@ -2039,4 +2038,5 @@
 		if (status != PJ_SUCCESS) {
 		    pjsua_perror(THIS_FILE, "Error opening sound device", status);
+		    PJSUA_UNLOCK();
 		    return status;
 		}
@@ -2052,4 +2052,5 @@
 		if (status != PJ_SUCCESS) {
 		    pjsua_perror(THIS_FILE, "Error opening sound device", status);
+		    PJSUA_UNLOCK();
 		    return status;
 		}
@@ -2059,4 +2060,5 @@
 		if (status != PJ_SUCCESS) {
 		    pjsua_perror(THIS_FILE, "Error opening sound device", status);
+		    PJSUA_UNLOCK();
 		    return status;
 		}
@@ -2076,4 +2078,5 @@
 	    if (status != PJ_SUCCESS) {
 		pjsua_perror(THIS_FILE, "Error opening sound device", status);
+		PJSUA_UNLOCK();
 		return status;
 	    }
@@ -2082,4 +2085,5 @@
     }
 
+    PJSUA_UNLOCK();
     return pjmedia_conf_connect_port(pjsua_var.mconf, source, sink, 0);
 }
@@ -2917,6 +2921,9 @@
     pj_status_t status = -1;
 
+    PJSUA_LOCK();
+
     /* Null-sound */
     if (capture_dev==NULL_SND_DEV_ID && playback_dev==NULL_SND_DEV_ID) {
+	PJSUA_UNLOCK();
 	return pjsua_set_null_snd_dev();
     }
@@ -2949,6 +2956,8 @@
 				  alt_cr[i], pjsua_var.media_cfg.channel_count,
 				  samples_per_frame, 16);
-	if (status != PJ_SUCCESS)
+	if (status != PJ_SUCCESS) {
+	    PJSUA_UNLOCK();
 	    return status;
+	}
 
 	/* Open! */
@@ -2961,4 +2970,5 @@
     if (status != PJ_SUCCESS) {
 	pjsua_perror(THIS_FILE, "Unable to open sound device", status);
+	PJSUA_UNLOCK();
 	return status;
     }
@@ -2966,4 +2976,5 @@
     pjsua_var.no_snd = PJ_FALSE;
 
+    PJSUA_UNLOCK();
     return PJ_SUCCESS;
 }
@@ -2978,4 +2989,6 @@
 				      int *playback_dev)
 {
+    PJSUA_LOCK();
+
     if (capture_dev) {
 	*capture_dev = pjsua_var.cap_dev;
@@ -2985,4 +2998,6 @@
     }
 
+    PJSUA_UNLOCK();
+
     return PJ_SUCCESS;
 }
@@ -2997,4 +3012,6 @@
     pj_status_t status;
 
+    PJSUA_LOCK();
+
     /* Close existing sound device */
     close_snd_dev();
@@ -3002,5 +3019,5 @@
     /* 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);
+    PJ_ASSERT_ON_FAIL(pjsua_var.snd_pool, {PJSUA_UNLOCK(); return PJ_ENOMEM;});
 
     PJ_LOG(4,(THIS_FILE, "Opening null sound device.."));
@@ -3018,4 +3035,5 @@
 	pjsua_perror(THIS_FILE, "Unable to create null sound device",
 		     status);
+	PJSUA_UNLOCK();
 	return status;
     }
@@ -3023,5 +3041,5 @@
     /* Start the master port */
     status = pjmedia_master_port_start(pjsua_var.null_snd);
-    PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
+    PJ_ASSERT_ON_FAIL(status == PJ_SUCCESS, {PJSUA_UNLOCK(); return status;});
 
     pjsua_var.cap_dev = NULL_SND_DEV_ID;
@@ -3030,4 +3048,5 @@
     pjsua_var.no_snd = PJ_FALSE;
 
+    PJSUA_UNLOCK();
     return PJ_SUCCESS;
 }
@@ -3040,8 +3059,12 @@
 PJ_DEF(pjmedia_port*) pjsua_set_no_snd_dev(void)
 {
+    PJSUA_LOCK();
+
     /* Close existing sound device */
     close_snd_dev();
-
     pjsua_var.no_snd = PJ_TRUE;
+
+    PJSUA_UNLOCK();
+
     return pjmedia_conf_get_master_port(pjsua_var.mconf);
 }
@@ -3053,11 +3076,16 @@
 PJ_DEF(pj_status_t) pjsua_set_ec(unsigned tail_ms, unsigned options)
 {
+    pj_status_t status = PJ_SUCCESS;
+
+    PJSUA_LOCK();
+
     pjsua_var.media_cfg.ec_tail_len = tail_ms;
 
     if (pjsua_var.snd_port)
-	return pjmedia_snd_port_set_ec( pjsua_var.snd_port, pjsua_var.pool,
-					tail_ms, options);
+	status = pjmedia_snd_port_set_ec(pjsua_var.snd_port, pjsua_var.pool,
+					 tail_ms, options);
     
-    return PJ_SUCCESS;
+    PJSUA_UNLOCK();
+    return status;
 }
 
@@ -3095,4 +3123,6 @@
 	return PJMEDIA_EAUD_INVCAP;
     }
+
+    PJSUA_LOCK();
 
     /* If sound is active, set it immediately */
@@ -3106,6 +3136,8 @@
     }
 
-    if (status != PJ_SUCCESS)
+    if (status != PJ_SUCCESS) {
+	PJSUA_UNLOCK();
 	return status;
+    }
 
     /* Save in internal param for later device open */
@@ -3115,4 +3147,6 @@
     }
 
+    PJSUA_UNLOCK();
+
     return status;
 }
@@ -3124,4 +3158,8 @@
 					   void *pval)
 {
+    pj_status_t status;
+
+    PJSUA_LOCK();
+
     /* If sound device has never been opened before, open it to 
      * retrieve the initial setting from the device (e.g. audio
@@ -3139,10 +3177,13 @@
 	
 	strm = pjmedia_snd_port_get_snd_stream(pjsua_var.snd_port);
-	return pjmedia_aud_stream_get_cap(strm, cap, pval);
+	status = 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);
-    }
+	status = pjmedia_aud_param_get_cap(&pjsua_var.aud_param,
+					   cap, pval);
+    }
+
+    PJSUA_UNLOCK();
+    return status;
 }
 
