Index: pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 5944)
+++ pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 5978)
@@ -1687,12 +1687,17 @@
 
 on_return:
-    if (status != PJ_SUCCESS && call_med->tp) {
-	pjsua_set_media_tp_state(call_med, PJSUA_MED_TP_NULL);
-	pjmedia_transport_close(call_med->tp);
-	call_med->tp = NULL;
-    }
-
-    if (sip_err_code)
-        *sip_err_code = err_code;
+    if (status != PJ_SUCCESS) {
+	if (call_med->tp) {
+	    pjsua_set_media_tp_state(call_med, PJSUA_MED_TP_NULL);
+	    pjmedia_transport_close(call_med->tp);
+	    call_med->tp = NULL;
+	}
+
+	if (err_code == 0)
+	    err_code = PJSIP_ERRNO_TO_SIP_STATUS(status);
+
+	if (sip_err_code)
+	    *sip_err_code = err_code;
+    }
 
     if (call_med->med_init_cb) {
@@ -1783,4 +1788,6 @@
 
         if (status != PJ_SUCCESS) {
+	    if (sip_err_code)
+		*sip_err_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
 	    call_med->tp_ready = status;
 	    pjsua_set_media_tp_state(call_med, PJSUA_MED_TP_NULL);
@@ -1993,5 +2000,6 @@
 
     if (pjsua_get_state() != PJSUA_STATE_RUNNING) {
-        if (sip_err_code) *sip_err_code = PJSIP_SC_SERVICE_UNAVAILABLE;
+        if (sip_err_code)
+	    *sip_err_code = PJSIP_SC_SERVICE_UNAVAILABLE;
 	return PJ_EBUSY;
     }
@@ -2002,6 +2010,9 @@
 
         status = pj_mutex_create_simple(tmppool, NULL, &call->med_ch_mutex);
-        if (status != PJ_SUCCESS)
+	if (status != PJ_SUCCESS) {
+	    if (sip_err_code)
+		*sip_err_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
             return status;
+	}
     }
 
@@ -2063,5 +2074,6 @@
 	if (maudcnt + mvidcnt == 0) {
 	    /* Expecting audio or video in the offer */
-	    if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
+	    if (sip_err_code)
+		*sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
 	    status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE);
 	    goto on_error;
@@ -2179,5 +2191,6 @@
     if (call->med_prov_cnt == 0) {
 	/* Expecting at least one media */
-	if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
+	if (sip_err_code)
+	    *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE_HERE;
 	status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE_HERE);
 	goto on_error;
@@ -2293,6 +2306,10 @@
 
     status = media_channel_init_cb(call_id, NULL);
-    if (status != PJ_SUCCESS && sip_err_code)
-        *sip_err_code = call->med_ch_info.sip_err_code;
+    if (status != PJ_SUCCESS && sip_err_code) {
+	if (call->med_ch_info.sip_err_code)
+	    *sip_err_code = call->med_ch_info.sip_err_code;
+	else
+	    *sip_err_code = PJSIP_ERRNO_TO_SIP_STATUS(status);
+    }
 
     pj_log_pop_indent();
@@ -2304,4 +2321,7 @@
         call->med_ch_mutex = NULL;
     }
+
+    if (sip_err_code && *sip_err_code == 0)
+	*sip_err_code = PJSIP_ERRNO_TO_SIP_STATUS(status);
 
     pj_log_pop_indent();
@@ -2331,6 +2351,8 @@
     pj_status_t status;
 
-    if (pjsua_get_state() != PJSUA_STATE_RUNNING)
-	return PJ_EBUSY;
+    if (pjsua_get_state() != PJSUA_STATE_RUNNING) {
+	status = PJ_EBUSY;
+	goto on_error;
+    }
 
 #if 0
@@ -2397,5 +2419,5 @@
                                            &origin, &sdp);
     if (status != PJ_SUCCESS)
-	return status;
+	goto on_error;
 
     /* Process each media line */
@@ -2512,9 +2534,9 @@
 	default:
 	    pj_assert(!"Invalid call_med media type");
-	    return PJ_EBUG;
+	    status = PJ_EBUG;
 	}
 
 	if (status != PJ_SUCCESS)
-	    return status;
+	    goto on_error;
 
     	/* Add ssrc and cname attribute */
@@ -2530,5 +2552,5 @@
 	if (status != PJ_SUCCESS) {
 	    if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE;
-	    return status;
+	    goto on_error;
 	}
 
@@ -2674,4 +2696,10 @@
     *p_sdp = sdp;
     return PJ_SUCCESS;
+
+on_error:
+    if (sip_err_code && *sip_err_code == 0)
+	*sip_err_code = PJSIP_SC_INTERNAL_SERVER_ERROR;
+
+    return status;
 }
 
