Index: pjproject/trunk/pjsip/include/pjsua2/config.hpp
===================================================================
--- pjproject/trunk/pjsip/include/pjsua2/config.hpp	(revision 5877)
+++ pjproject/trunk/pjsip/include/pjsua2/config.hpp	(revision 5878)
@@ -40,4 +40,11 @@
 #endif
 
+/**
+ * Maximum buffer length to print SDP content for SdpSession. Set this to 0
+ * if the printed SDP is not needed.
+ */
+#ifndef PJSUA2_MAX_SDP_BUF_LEN
+#   define PJSUA2_MAX_SDP_BUF_LEN		1024
+#endif
 
 /**
Index: pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 5877)
+++ pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 5878)
@@ -1359,4 +1359,5 @@
  */
 static void sort_media2(const pjsua_call_media *call_med,
+			pj_bool_t check_tp,
 			unsigned call_med_cnt,
 			pjmedia_type type,
@@ -1387,5 +1388,5 @@
 
 	/* Is it active? */
-	if (!call_med[i].tp) {
+	if (check_tp && !call_med[i].tp) {
 	    score[i] -= 10;
 	}
@@ -2047,10 +2048,17 @@
 	 * Otherwise, apply media count from the call setting directly.
 	 */
-	if (reinit && (call->opt.flag & PJSUA_CALL_REINIT_MEDIA) == 0) {
+	if (reinit) {
+	    pj_bool_t sort_check_tp;
+
+	    /* Media sorting below will check transport, i.e: media without
+	     * transport will have lower priority. If PJSUA_CALL_REINIT_MEDIA
+	     * is set, we must not check transport.
+	     */
+	    sort_check_tp = !(call->opt.flag & PJSUA_CALL_REINIT_MEDIA);
 
 	    /* We are sending reoffer, check media count for each media type
 	     * from the existing call media list.
 	     */
-	    sort_media2(call->media_prov, call->med_prov_cnt,
+	    sort_media2(call->media_prov, sort_check_tp, call->med_prov_cnt,
 			PJMEDIA_TYPE_AUDIO, maudidx, &maudcnt, &mtotaudcnt);
 
@@ -2058,5 +2066,5 @@
 	    //pj_assert(maudcnt > 0);
 
-	    sort_media2(call->media_prov, call->med_prov_cnt,
+	    sort_media2(call->media_prov, sort_check_tp, call->med_prov_cnt,
 			PJMEDIA_TYPE_VIDEO, mvididx, &mvidcnt, &mtotvidcnt);
 
@@ -2088,4 +2096,15 @@
 	    }
 	    mvidcnt = call->opt.vid_cnt;
+
+	    /* In case of media reinit, 'med_prov_cnt' may be decreased
+	     * because the new call->opt says so. As media count should
+	     * never decrease, we should verify 'med_prov_cnt' to be
+	     * at least equal to 'med_cnt' (see also #1987).
+	     */
+	    if ((call->opt.flag & PJSUA_CALL_REINIT_MEDIA) &&
+		call->med_prov_cnt < call->med_cnt)
+	    {
+		call->med_prov_cnt = call->med_cnt;
+	    }
 
 	} else {
@@ -2113,15 +2132,4 @@
 		}
 #endif
-	    }
-
-	    /* In case of media reinit, 'med_prov_cnt' may be decreased
-	     * because the new call->opt says so. As media count should
-	     * never decrease, we should verify 'med_prov_cnt' to be
-	     * at least equal to 'med_cnt' (see also #1987).
-	     */
-	    if (reinit && (call->opt.flag & PJSUA_CALL_REINIT_MEDIA) &&
-		call->med_prov_cnt < call->med_cnt)
-	    {
-		call->med_prov_cnt = call->med_cnt;
 	    }
 	}
Index: pjproject/trunk/pjsip/src/pjsua2/call.cpp
===================================================================
--- pjproject/trunk/pjsip/src/pjsua2/call.cpp	(revision 5877)
+++ pjproject/trunk/pjsip/src/pjsua2/call.cpp	(revision 5878)
@@ -30,5 +30,4 @@
 ///////////////////////////////////////////////////////////////////////////////
 
-#define SDP_BUFFER_SIZE 1024
 
 MathStat::MathStat()
@@ -111,9 +110,13 @@
 void SdpSession::fromPj(const pjmedia_sdp_session &sdp)
 {
-    char buf[SDP_BUFFER_SIZE];
+#if PJSUA2_MAX_SDP_BUF_LEN
+    char buf[PJSUA2_MAX_SDP_BUF_LEN];
     int len;
 
     len = pjmedia_sdp_print(&sdp, buf, sizeof(buf));
     wholeSdp = (len > -1? string(buf, len): "");
+#else
+    wholeSdp = "";
+#endif    
     pjSdpSession = (void *)&sdp;
 }
@@ -168,4 +171,9 @@
      */
     void setPortId(int id);
+
+    /**
+     * Destructor
+     */
+    virtual ~CallAudioMedia();
 };
 
@@ -174,4 +182,9 @@
 {
     this->id = pid;
+}
+
+CallAudioMedia::~CallAudioMedia()
+{
+    id = PJSUA_INVALID_ID;
 }
 
@@ -419,14 +432,18 @@
     p_reason = (reason.slen == 0? NULL: &reason);
 
-    if (sdp_str == "") {
-    	sdp = NULL;
-    } else {
+    sdp = NULL;
+    if (sdp_str != "") {
         pj_str_t dup_pj_sdp;
         pj_str_t pj_sdp_str = {(char*)sdp_str.c_str(),
         		       (pj_ssize_t)sdp_str.size()};
+	pj_status_t status;
 
         pj_strdup(pool, &dup_pj_sdp, &pj_sdp_str);        
-        pjmedia_sdp_parse(pool, dup_pj_sdp.ptr,
-                          dup_pj_sdp.slen, &sdp);
+        status = pjmedia_sdp_parse(pool, dup_pj_sdp.ptr,
+				   dup_pj_sdp.slen, &sdp);
+	if (status != PJ_SUCCESS) {
+	    PJ_PERROR(4,(THIS_FILE, status,
+			 "Failed to parse SDP for call param"));
+	}
     }
 }
@@ -753,4 +770,13 @@
     
     if (pjsua_call_get_info(id, &pj_ci) == PJ_SUCCESS) {
+	if (medias.size()) {
+	    /* Clear medias. */
+	    for (mi = 0; mi < medias.size(); mi++) {
+		if (medias[mi])
+		    delete medias[mi];
+	    }
+	    medias.clear();	
+	}
+
         for (mi = 0; mi < pj_ci.media_cnt; mi++) {
             if (mi >= medias.size()) {
Index: pjproject/trunk/pjsip/src/pjsua2/endpoint.cpp
===================================================================
--- pjproject/trunk/pjsip/src/pjsua2/endpoint.cpp	(revision 5877)
+++ pjproject/trunk/pjsip/src/pjsua2/endpoint.cpp	(revision 5878)
@@ -1076,9 +1076,15 @@
         pj_str_t new_sdp_str = {(char*)prm.sdp.wholeSdp.c_str(),
         			(pj_ssize_t)prm.sdp.wholeSdp.size()};
+	pj_status_t status;
 
         pj_strdup(pool, &dup_new_sdp, &new_sdp_str);        
-        pjmedia_sdp_parse(pool, dup_new_sdp.ptr,
-                          dup_new_sdp.slen, &new_sdp);
-        pj_memcpy(sdp, new_sdp, sizeof(*sdp));
+        status = pjmedia_sdp_parse(pool, dup_new_sdp.ptr,
+				   dup_new_sdp.slen, &new_sdp);
+	if (status != PJ_SUCCESS) {
+	    PJ_PERROR(4,(THIS_FILE, status,
+			 "Failed to parse the modified SDP"));
+	} else {
+	    pj_memcpy(sdp, new_sdp, sizeof(*sdp));
+	}
     }
 }
