Index: pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c
===================================================================
--- pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c	(revision 5917)
+++ pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c	(revision 5918)
@@ -294,5 +294,6 @@
 
     /* Start ringback */
-    ring_start(call_id);
+    if (call_info.rem_aud_cnt)
+    	ring_start(call_id);
     
     if (app_config.auto_answer > 0) {
Index: pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h
===================================================================
--- pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h	(revision 5917)
+++ pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h	(revision 5918)
@@ -4724,11 +4724,22 @@
     pj_str_t		last_status_text;
 
-    /** Media status of the first audio stream. */
+    /** Media status of the default audio stream. Default audio stream 
+     *  is chosen according to this priority:
+     *  1. enabled, i.e: SDP media port not zero
+     *  2. transport protocol in the SDP matching account config's
+     *     secure media transport usage (\a use_srtp field).
+     *  3. active, i.e: SDP media direction is not "inactive"
+     *  4. media order (according to the SDP).
+     */
     pjsua_call_media_status media_status;
 
-    /** Media direction of the first audio stream. */
+    /** Media direction of the default audio stream.
+     *  See \a media_status above on how the default is chosen.
+     */
     pjmedia_dir		media_dir;
 
-    /** The conference port number for the first audio stream. */
+    /** The conference port number for the default audio stream.
+     *  See \a media_status above on how the default is chosen.
+     */
     pjsua_conf_port_id	conf_slot;
 
Index: pjproject/trunk/pjsip/src/pjsua-lib/pjsua_aud.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsua-lib/pjsua_aud.c	(revision 5917)
+++ pjproject/trunk/pjsip/src/pjsua-lib/pjsua_aud.c	(revision 5918)
@@ -78,5 +78,6 @@
 
     call = &pjsua_var.calls[call_id];
-    port_id = call->media[call->audio_idx].strm.a.conf_slot;
+    if (call->audio_idx >= 0)
+	port_id = call->media[call->audio_idx].strm.a.conf_slot;
 
 on_return:
Index: pjproject/trunk/pjsip/src/pjsua-lib/pjsua_call.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsua-lib/pjsua_call.c	(revision 5917)
+++ pjproject/trunk/pjsip/src/pjsua-lib/pjsua_call.c	(revision 5918)
@@ -788,4 +788,40 @@
     PJSUA_LOCK();
 
+    acc = &pjsua_var.acc[acc_id];
+    if (!acc->valid) {
+	pjsua_perror(THIS_FILE, "Unable to make call because account "
+		     "is not valid", PJ_EINVALIDOP);
+	status = PJ_EINVALIDOP;
+	goto on_error;
+    }
+
+    /* Find free call slot. */
+    call_id = alloc_call_id();
+
+    if (call_id == PJSUA_INVALID_ID) {
+	pjsua_perror(THIS_FILE, "Error making call", PJ_ETOOMANY);
+	status = PJ_ETOOMANY;
+	goto on_error;
+    }
+
+    /* Clear call descriptor */
+    reset_call(call_id);
+
+    call = &pjsua_var.calls[call_id];
+
+    /* Associate session with account */
+    call->acc_id = acc_id;
+    call->call_hold_type = acc->cfg.call_hold_type;
+
+    /* Generate per-session RTCP CNAME, according to RFC 7022. */
+    pj_create_random_string(call->cname_buf, call->cname.slen);
+
+    /* Apply call setting */
+    status = apply_call_setting(call, opt, NULL);
+    if (status != PJ_SUCCESS) {
+	pjsua_perror(THIS_FILE, "Failed to apply call setting", status);
+	goto on_error;
+    }
+    
     /* Create sound port if none is instantiated, to check if sound device
      * can be used. But only do this with the conference bridge, as with
@@ -794,5 +830,5 @@
      */
     if (!pjsua_var.is_mswitch && pjsua_var.snd_port==NULL &&
-	pjsua_var.null_snd==NULL && !pjsua_var.no_snd)
+	pjsua_var.null_snd==NULL && !pjsua_var.no_snd && call->opt.aud_cnt > 0)
     {
 	status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev);
@@ -801,40 +837,4 @@
     }
 
-    acc = &pjsua_var.acc[acc_id];
-    if (!acc->valid) {
-	pjsua_perror(THIS_FILE, "Unable to make call because account "
-		     "is not valid", PJ_EINVALIDOP);
-	status = PJ_EINVALIDOP;
-	goto on_error;
-    }
-
-    /* Find free call slot. */
-    call_id = alloc_call_id();
-
-    if (call_id == PJSUA_INVALID_ID) {
-	pjsua_perror(THIS_FILE, "Error making call", PJ_ETOOMANY);
-	status = PJ_ETOOMANY;
-	goto on_error;
-    }
-
-    /* Clear call descriptor */
-    reset_call(call_id);
-
-    call = &pjsua_var.calls[call_id];
-
-    /* Associate session with account */
-    call->acc_id = acc_id;
-    call->call_hold_type = acc->cfg.call_hold_type;
-
-    /* Generate per-session RTCP CNAME, according to RFC 7022. */
-    pj_create_random_string(call->cname_buf, call->cname.slen);
-
-    /* Apply call setting */
-    status = apply_call_setting(call, opt, NULL);
-    if (status != PJ_SUCCESS) {
-	pjsua_perror(THIS_FILE, "Failed to apply call setting", status);
-	goto on_error;
-    }
-    
     /* Create temporary pool */
     tmp_pool = pjsua_pool_create("tmpcall10", 512, 256);
Index: pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 5917)
+++ pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 5918)
@@ -1247,8 +1247,11 @@
 
 /* Go through the list of media in the SDP, find acceptable media, and
- * sort them based on the "quality" of the media, and store the indexes
- * in the specified array. Media with the best quality will be listed
- * first in the array. The quality factors considered currently is
- * encryption.
+ * sort them based on the below criteria, and store the indexes
+ * in the specified array. The criteria is as follows:
+ * 1. enabled, i.e: SDP media port not zero
+ * 2. transport protocol in the SDP matching account config's
+ *    secure media transport usage (\a use_srtp field).
+ * 3. active, i.e: SDP media direction is not "inactive"
+ * 4. media order (according to the SDP).
  */
 static void sort_media(const pjmedia_sdp_session *sdp,
@@ -2014,10 +2017,4 @@
 	sort_media(rem_sdp, &STR_AUDIO, acc->cfg.use_srtp,
 		   maudidx, &maudcnt, &mtotaudcnt);
-	if (maudcnt==0) {
-	    /* Expecting audio in the offer */
-	    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;
-	}
 
 #if PJMEDIA_HAS_VIDEO
@@ -2028,4 +2025,11 @@
 	PJ_UNUSED_ARG(STR_VIDEO);
 #endif
+
+	if (maudcnt + mvidcnt == 0) {
+	    /* Expecting audio or video in the offer */
+	    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;
+	}
 
 	/* Update media count only when remote add any media, this media count
@@ -2231,8 +2235,12 @@
     }
 
-    call->audio_idx = maudidx[0];
-
-    PJ_LOG(4,(THIS_FILE, "Media index %d selected for audio call %d",
-	      call->audio_idx, call->index));
+    if (maudcnt > 0) {
+    	call->audio_idx = maudidx[0];
+
+    	PJ_LOG(4,(THIS_FILE, "Media index %d selected for audio call %d",
+	      	  call->audio_idx, call->index));
+    } else {
+    	call->audio_idx = -1;
+    }
 
     if (pending_med_tp) {
