Index: pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 2027)
+++ pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 2032)
@@ -653,5 +653,5 @@
     switch (op) {
     case PJ_ICE_STRANS_OP_INIT:
-	pjsua_var.calls[id].med_tp_st = result;
+	pjsua_var.calls[id].med_tp_ready = result;
 	break;
     case PJ_ICE_STRANS_OP_NEGOTIATION:
@@ -753,5 +753,5 @@
 	ice_cb.on_ice_complete = &on_ice_complete;
 	pj_ansi_snprintf(name, sizeof(name), "icetp%02d", i);
-	pjsua_var.calls[i].med_tp_st = PJ_EPENDING;
+	pjsua_var.calls[i].med_tp_ready = PJ_EPENDING;
 
 	comp_cnt = 1;
@@ -770,12 +770,12 @@
 	/* Wait until transport is initialized, or time out */
 	PJSUA_UNLOCK();
-	while (pjsua_var.calls[i].med_tp_st == PJ_EPENDING) {
+	while (pjsua_var.calls[i].med_tp_ready == PJ_EPENDING) {
 	    pjsua_handle_events(100);
 	}
 	PJSUA_LOCK();
-	if (pjsua_var.calls[i].med_tp_st != PJ_SUCCESS) {
+	if (pjsua_var.calls[i].med_tp_ready != PJ_SUCCESS) {
 	    pjsua_perror(THIS_FILE, "Error initializing ICE media transport",
-		         pjsua_var.calls[i].med_tp_st);
-	    status = pjsua_var.calls[i].med_tp_st;
+		         pjsua_var.calls[i].med_tp_ready);
+	    status = pjsua_var.calls[i].med_tp_ready;
 	    goto on_error;
 	}
@@ -848,7 +848,11 @@
 				     pjsip_role_e role,
 				     int security_level,
+				     pj_pool_t *tmp_pool,
+				     const pjmedia_sdp_session *rem_sdp,
 				     int *sip_err_code)
 {
+    enum { MEDIA_IDX = 0 };
     pjsua_call *call = &pjsua_var.calls[call_id];
+    pj_status_t status;
 
 #if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
@@ -856,5 +860,4 @@
     pjmedia_srtp_setting srtp_opt;
     pjmedia_transport *srtp;
-    pj_status_t status;
 #endif
 
@@ -905,4 +908,14 @@
 #endif
 
+    /* Create the media transport */
+    status = pjmedia_transport_media_create(call->med_tp, tmp_pool, 0,
+					    rem_sdp, MEDIA_IDX);
+    if (status != PJ_SUCCESS) {
+	if (sip_err_code) *sip_err_code = PJSIP_SC_NOT_ACCEPTABLE;
+	pjsua_media_channel_deinit(call_id);
+	return status;
+    }
+
+    call->med_tp_st = PJSUA_MED_TP_INIT;
     return PJ_SUCCESS;
 }
@@ -927,4 +940,16 @@
     }
 
+    /* Create media if it's not created. This could happen when call is
+     * currently on-hold
+     */
+    if (call->med_tp_st == PJSUA_MED_TP_IDLE) {
+	pjsip_role_e role;
+	role = (rem_sdp ? PJSIP_ROLE_UAS : PJSIP_ROLE_UAC);
+	status = pjsua_media_channel_init(call_id, role, call->secure_level, 
+					  pool, rem_sdp, sip_status_code);
+	if (status != PJ_SUCCESS)
+	    return status;
+    }
+
     /* Get media socket info */
     pjmedia_transport_info_init(&tpinfo);
@@ -936,5 +961,5 @@
     if (status != PJ_SUCCESS) {
 	if (sip_status_code) *sip_status_code = 500;
-	goto on_error;
+	return status;
     }
 
@@ -964,18 +989,13 @@
 
     /* Give the SDP to media transport */
-    status = pjmedia_transport_media_create(call->med_tp, pool, 0,
-					    sdp, rem_sdp, MEDIA_IDX);
+    status = pjmedia_transport_encode_sdp(call->med_tp, pool, sdp, rem_sdp, 
+					  MEDIA_IDX);
     if (status != PJ_SUCCESS) {
 	if (sip_status_code) *sip_status_code = PJSIP_SC_NOT_ACCEPTABLE;
-	goto on_error;
+	return status;
     }
 
     *p_sdp = sdp;
     return PJ_SUCCESS;
-
-on_error:
-    pjsua_media_channel_deinit(call_id);
-    return status;
-
 }
 
@@ -984,7 +1004,4 @@
 {
     pjsua_call *call = &pjsua_var.calls[call_id];
-
-    //see ticket #525
-    //pjmedia_transport_media_stop(call->med_tp);
 
     if (call->conf_slot != PJSUA_INVALID_ID) {
@@ -1017,5 +1034,8 @@
     stop_media_session(call_id);
 
-    pjmedia_transport_media_stop(call->med_tp);
+    if (call->med_tp_st != PJSUA_MED_TP_IDLE) {
+	pjmedia_transport_media_stop(call->med_tp);
+	call->med_tp_st = PJSUA_MED_TP_IDLE;
+    }
 
     if (call->med_orig && call->med_tp != call->med_orig) {
@@ -1049,5 +1069,5 @@
 
 pj_status_t pjsua_media_channel_update(pjsua_call_id call_id,
-				       pjmedia_sdp_session *local_sdp,
+				       const pjmedia_sdp_session *local_sdp,
 				       const pjmedia_sdp_session *remote_sdp)
 {
@@ -1113,4 +1133,5 @@
 	/* Shutdown transport's session */
 	pjmedia_transport_media_stop(call->med_tp);
+	call->med_tp_st = PJSUA_MED_TP_IDLE;
 
 	/* No need because we need keepalive? */
@@ -1123,5 +1144,5 @@
 
     } else {
-	/* Start media transport */
+	/* Start/restart media transport */
 	status = pjmedia_transport_media_start(call->med_tp, 
 					       call->inv->pool,
@@ -1129,4 +1150,6 @@
 	if (status != PJ_SUCCESS)
 	    return status;
+
+	call->med_tp_st = PJSUA_MED_TP_RUNNING;
 
 	/* Override ptime, if this option is specified. */
