Index: pjproject/trunk/pjsip/src/pjsua-lib/pjsua_call.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsua-lib/pjsua_call.c	(revision 2027)
+++ pjproject/trunk/pjsip/src/pjsua-lib/pjsua_call.c	(revision 2032)
@@ -213,6 +213,4 @@
 
 
-#define LATE_SDP    0
-
 /* Allocate one call id */
 static pjsua_call_id alloc_call_id(void)
@@ -288,4 +286,5 @@
 }
 
+/*
 static int call_get_secure_level(pjsua_call *call)
 {
@@ -317,4 +316,5 @@
     return 0;
 }
+*/
 
 
@@ -450,5 +450,6 @@
     /* Init media channel */
     status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC, 
-				      call->secure_level, NULL);
+				      call->secure_level, dlg->pool,
+				      NULL, NULL);
     if (status != PJ_SUCCESS) {
 	pjsua_perror(THIS_FILE, "Error initializing media channel", status);
@@ -456,15 +457,11 @@
     }
 
-    /* Create SDP offer */
-#if LATE_SDP
-    offer = NULL;
-#else
-    status = pjsua_media_channel_create_sdp(call->index, dlg->pool, NULL, 
+    /* Create offer */
+    status = pjsua_media_channel_create_sdp(call->index, dlg->pool, NULL,
 					    &offer, NULL);
     if (status != PJ_SUCCESS) {
-	pjsua_perror(THIS_FILE, "pjmedia unable to create SDP", status);
+	pjsua_perror(THIS_FILE, "Error initializing media channel", status);
 	goto on_error;
     }
-#endif
 
     /* Create the INVITE session: */
@@ -719,16 +716,4 @@
 	call->secure_level = 0;
 
-    /* Init media channel */
-    status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAS, 
-				      call->secure_level, &sip_err_code);
-    if (status != PJ_SUCCESS) {
-	pjsua_perror(THIS_FILE, "Error initializing media channel", status);
-	pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 
-				      sip_err_code, NULL,
-				      NULL, NULL);
-	PJSUA_UNLOCK();
-	return PJ_TRUE;
-    }
-
     /* Parse SDP from incoming request */
     if (rdata->msg_info.msg->body) {
@@ -740,7 +725,6 @@
 	    pjsua_perror(THIS_FILE, "Error parsing SDP in incoming INVITE", 
 			 status);
-	    pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, &reason,
-					  NULL, NULL);
-	    pjsua_media_channel_deinit(call->index);
+	    pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, 400, 
+				&reason, NULL, NULL, NULL);
 	    PJSUA_UNLOCK();
 	    return PJ_TRUE;
@@ -762,16 +746,28 @@
     }
 
-    /* Get media capability from media endpoint: */
+    /* Init media channel */
+    status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAS, 
+				      call->secure_level, 
+				      rdata->tp_info.pool, offer,
+				      &sip_err_code);
+    if (status != PJ_SUCCESS) {
+	pjsua_perror(THIS_FILE, "Error initializing media channel", status);
+	pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata,
+			    sip_err_code, NULL, NULL, NULL, NULL);
+	PJSUA_UNLOCK();
+	return PJ_TRUE;
+    }
+
+    /* Create answer */
     status = pjsua_media_channel_create_sdp(call->index, rdata->tp_info.pool, 
 					    offer, &answer, &sip_err_code);
     if (status != PJ_SUCCESS) {
 	pjsua_perror(THIS_FILE, "Error creating SDP answer", status);
-	pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 
-				      sip_err_code, NULL,
-				      NULL, NULL);
-	pjsua_media_channel_deinit(call->index);
+	pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata,
+			    sip_err_code, NULL, NULL, NULL, NULL);
 	PJSUA_UNLOCK();
 	return PJ_TRUE;
     }
+
 
     /* Verify that we can handle the request. */
@@ -795,8 +791,7 @@
 
 	} else {
-
 	    /* Respond with 500 (Internal Server Error) */
-	    pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL,
-					  NULL, NULL);
+	    pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, 500, NULL,
+				NULL, NULL, NULL);
 	}
 
@@ -1475,16 +1470,4 @@
     }
 
-    /* Update call secure level */
-    call->secure_level = call_get_secure_level(call);
-
-    /* Init media channel */
-    status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC,
-				      call->secure_level, NULL);
-    if (status != PJ_SUCCESS) {
-	pjsua_perror(THIS_FILE, "Error initializing media channel", status);
-	pjsip_dlg_dec_lock(dlg);
-	return PJSIP_ESESSIONSTATE;
-    }
-
     /* Create SDP */
     PJ_UNUSED_ARG(unhold);
@@ -1548,16 +1531,4 @@
 	return status;
 
-    /* Update call's secure level */
-    call->secure_level = call_get_secure_level(call);
-
-    /* Init media channel */
-    status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC,
-				      call->secure_level, NULL);
-    if (status != PJ_SUCCESS) {
-	pjsua_perror(THIS_FILE, "Error initializing media channel", status);
-	pjsip_dlg_dec_lock(dlg);
-	return PJSIP_ESESSIONSTATE;
-    }
-
     /* Create SDP */
     status = pjsua_media_channel_create_sdp(call->index, call->inv->pool, 
@@ -1570,5 +1541,7 @@
     }
 
-    /* Create re-INVITE with new offer */
+    update_sdp_version(call, sdp);
+
+    /* Create UPDATE with new offer */
     status = pjsip_inv_update(call->inv, NULL, sdp, &tdata);
     if (status != PJ_SUCCESS) {
@@ -2538,6 +2511,5 @@
 {
     pjsua_call *call;
-    const pjmedia_sdp_session *c_local;
-    pjmedia_sdp_session *local_sdp;
+    const pjmedia_sdp_session *local_sdp;
     const pjmedia_sdp_session *remote_sdp;
 
@@ -2568,5 +2540,5 @@
 
     /* Get local and remote SDP */
-    status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &c_local);
+    status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &local_sdp);
     if (status != PJ_SUCCESS) {
 	pjsua_perror(THIS_FILE, 
@@ -2577,5 +2549,4 @@
 	return;
     }
-    local_sdp = (pjmedia_sdp_session*) c_local;
 
     status = pjmedia_sdp_neg_get_active_remote(call->inv->neg, &remote_sdp);
@@ -2713,16 +2684,4 @@
 		  call->index));
 
-	/* Update call's secure level */
-	call->secure_level = call_get_secure_level(call);
-
-	/* Init media channel */
-	status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAS,
-					  call->secure_level, NULL);
-	if (status != PJ_SUCCESS) {
-	    pjsua_perror(THIS_FILE, "Error initializing media channel", status);
-	    PJSUA_UNLOCK();
-	    return;
-	}
-
 	status = pjsua_media_channel_create_sdp(call->index, call->inv->pool, 
 					        offer, &answer, NULL);
@@ -2768,16 +2727,4 @@
 	PJ_LOG(4,(THIS_FILE, "Call %d: asked to send a new offer",
 		  call->index));
-
-	/* Update call's secure level */
-	call->secure_level = call_get_secure_level(call);
-
-	/* Init media channel */
-	status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC,
-					  call->secure_level, NULL);
-	if (status != PJ_SUCCESS) {
-	    pjsua_perror(THIS_FILE, "Error initializing media channel", status);
-	    PJSUA_UNLOCK();
-	    return;
-	}
 
 	status = pjsua_media_channel_create_sdp(call->index, call->inv->pool, 
