Added to pjsua: write new media settings, modify call stat output, fixed general bug

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@293 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjsip/src/pjsua-lib/pjsua_call.c b/pjsip/src/pjsua-lib/pjsua_call.c
index 1fc8060..b245a49 100644
--- a/pjsip/src/pjsua-lib/pjsua_call.c
+++ b/pjsip/src/pjsua-lib/pjsua_call.c
@@ -283,14 +283,27 @@
      */
     
     status = pjsip_inv_initial_answer(inv, rdata, 
-				      (pjsua.auto_answer ? 200 : 100), 
+				      (pjsua.auto_answer ? pjsua.auto_answer 
+					: 100), 
 				      NULL, NULL, &response);
     if (status != PJ_SUCCESS) {
 	
-	pjsua_perror(THIS_FILE, "Unable to create 100 response", status);
+	int st_code;
 
-	pjsip_dlg_respond(dlg, rdata, 500, NULL, NULL, NULL);
-	pjsip_inv_terminate(inv, 500, PJ_FALSE);
+	pjsua_perror(THIS_FILE, "Unable to send answer to incoming INVITE", 
+		     status);
+
+	/* If failed to send 2xx response, there's a good chance that it is
+	 * because SDP negotiation has failed.
+	 */
+	if (pjsua.auto_answer/100 == 2)
+	    st_code = PJSIP_SC_UNSUPPORTED_MEDIA_TYPE;
+	else
+	    st_code = 500;
+
+	pjsip_dlg_respond(dlg, rdata, st_code, NULL, NULL, NULL);
+	pjsip_inv_terminate(inv, st_code, PJ_FALSE);
+	return PJ_TRUE;
 
     } else {
 	status = pjsip_inv_send_msg(inv, response, NULL);
@@ -762,8 +775,12 @@
 
 	pjsua_perror(THIS_FILE, "SDP negotiation has failed", status);
 
-	/* Disconnect call if this is not a re-INVITE */
-	if (inv->state != PJSIP_INV_STATE_CONFIRMED) {
+	/* Disconnect call if we're not in the middle of initializing an
+	 * UAS dialog and if this is not a re-INVITE 
+	 */
+	if (inv->state != PJSIP_INV_STATE_NULL &&
+	    inv->state != PJSIP_INV_STATE_CONFIRMED) 
+	{
 	    call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE);
 	}
 	return;
diff --git a/pjsip/src/pjsua-lib/pjsua_core.c b/pjsip/src/pjsua-lib/pjsua_core.c
index 2744f4e..aaf5601 100644
--- a/pjsip/src/pjsua-lib/pjsua_core.c
+++ b/pjsip/src/pjsua-lib/pjsua_core.c
@@ -74,7 +74,7 @@
 
     /* Default for media: */
     pjsua.clock_rate = 8000;
-    pjsua.complexity = 4;
+    pjsua.complexity = -1;
     pjsua.quality = 4;
 
 
@@ -84,6 +84,7 @@
 	pjsua.acc[i].index = i;
 	pjsua.acc[i].local_uri = pj_str(PJSUA_LOCAL_URI);
 	pjsua.acc[i].reg_timeout = 55;
+	pjsua.acc[i].online_status = PJ_TRUE;
 	pj_list_init(&pjsua.acc[i].route_set);
 	pj_list_init(&pjsua.acc[i].pres_srv_list);
     }
diff --git a/pjsip/src/pjsua-lib/pjsua_settings.c b/pjsip/src/pjsua-lib/pjsua_settings.c
index 18aec9a..4f02c23 100644
--- a/pjsip/src/pjsua-lib/pjsua_settings.c
+++ b/pjsip/src/pjsua-lib/pjsua_settings.c
@@ -92,7 +92,7 @@
     puts  ("  --auto-conf         Automatically put incoming calls to conference");
     puts  ("  --rtp-port=N        Base port to try for RTP (default=4000)");
     puts  ("  --add-codec=name    Specify alternate codec order");
-    puts  ("  --complexity=N      Specify encoding complexity (0-10, default=4)");
+    puts  ("  --complexity=N      Specify encoding complexity (0-10, default=none(-1))");
     puts  ("  --quality=N         Specify encoding quality (0-10, default=4)");
     puts  ("");
     puts  ("Buddy List (can be more than one):");
@@ -393,6 +393,7 @@
 		return PJ_EINVAL;
 	    }
 	    cur_acc->local_uri = pj_str(optarg);
+	    pjsua.has_acc = 1;
 	    break;
 
 	case OPT_CONTACT:   /* contact */
@@ -576,6 +577,23 @@
 	buf[len] = '\0';
 }
 
+static const char *good_number(char *buf, pj_int32_t val)
+{
+    if (val < 1000) {
+	pj_ansi_sprintf(buf, "%d", val);
+    } else if (val < 1000000) {
+	pj_ansi_sprintf(buf, "%d.%dK", 
+			val / 1000,
+			(val % 1000) / 100);
+    } else {
+	pj_ansi_sprintf(buf, "%d.%02dM", 
+			val / 1000000,
+			(val % 1000000) / 10000);
+    }
+
+    return buf;
+}
+
 static void dump_media_session(pjmedia_session *session)
 {
     unsigned i;
@@ -588,6 +606,7 @@
 	const char *rem_addr;
 	int rem_port;
 	const char *dir;
+	char stxpkt[10], stxoct[10], srxpkt[10], srxoct[10];
 
 	pjmedia_session_get_stream_stat(session, i, &strm_stat);
 	rem_addr = pj_inet_ntoa(info.stream_info[i].rem_addr.sin_addr);
@@ -604,18 +623,23 @@
 
 	
 	PJ_LOG(3,(THIS_FILE, 
-		  "%s[Media strm#%d] %.*s, %s, peer=%s:%d",
+		  "%s#%d %.*s @%dKHz, %s, peer=%s:%d",
 		  "               ",
 		  i,
 		  info.stream_info[i].fmt.encoding_name.slen,
 		  info.stream_info[i].fmt.encoding_name.ptr,
+		  info.stream_info[i].fmt.sample_rate / 1000,
 		  dir,
 		  rem_addr, rem_port));
 	PJ_LOG(3,(THIS_FILE, 
-		  "%s tx {pkt=%u, bytes=%u} rx {pkt=%u, bytes=%u}",
-		  "                             ",
-		  strm_stat.enc.pkt, strm_stat.enc.bytes,
-		  strm_stat.dec.pkt, strm_stat.dec.bytes));
+		  "%s tx{pt=%d,pkt=%s,oct=%s} rx{pt=%d,pkt=%s,oct=%s}",
+		  "                 ",
+		  info.stream_info[i].tx_pt,
+		  good_number(stxpkt, strm_stat.enc.pkt), 
+		  good_number(stxoct, strm_stat.enc.bytes),
+		  info.stream_info[i].fmt.pt,
+		  good_number(srxpkt, strm_stat.dec.pkt), 
+		  good_number(srxoct, strm_stat.dec.bytes)));
 
     }
 }
@@ -762,12 +786,14 @@
 
 
     /* Save account settings. */
-    for (acc_index=0; acc_index < pjsua.acc_cnt; ++acc_index) {
-	
-	save_account_settings(acc_index, &cfg);
+    if (pjsua.has_acc) {
+	for (acc_index=0; acc_index < pjsua.acc_cnt; ++acc_index) {
+	    
+	    save_account_settings(acc_index, &cfg);
 
-	if (acc_index < pjsua.acc_cnt-1)
-	    pj_strcat2(&cfg, "--next-account\n");
+	    if (acc_index < pjsua.acc_cnt-1)
+		pj_strcat2(&cfg, "--next-account\n");
+	}
     }
 
     /* Credentials. */
@@ -849,7 +875,31 @@
 			pjsua.wav_file);
 	pj_strcat2(&cfg, line);
     }
+    /* Media clock rate. */
+    if (pjsua.clock_rate >= 32000)
+	pj_strcat2(&cfg, "--uwb\n");
+    else if (pjsua.clock_rate >= 16000)
+	pj_strcat2(&cfg, "--wb\n");
 
+    /* Encoding quality and complexity */
+    pj_ansi_sprintf(line, "--quality %d\n",
+		    pjsua.quality);
+    pj_strcat2(&cfg, line);
+    pj_ansi_sprintf(line, "--complexity %d\n",
+		    pjsua.complexity);
+    pj_strcat2(&cfg, line);
+
+    /* Start RTP port. */
+    pj_ansi_sprintf(line, "--rtp-port %d\n",
+		    pjsua.start_rtp_port);
+    pj_strcat2(&cfg, line);
+
+    /* Add codec. */
+    for (i=0; i<pjsua.codec_cnt; ++i) {
+	pj_ansi_sprintf(line, "--add-codec %s\n",
+		    pjsua.codec_arg[i].ptr);
+	pj_strcat2(&cfg, line);
+    }
 
     pj_strcat2(&cfg, "#\n# User agent:\n#\n");