Index: pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c	(revision 482)
+++ pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c	(revision 487)
@@ -52,4 +52,6 @@
 
     cfg->thread_cnt = 1;
+    cfg->media_has_ioqueue = 1;
+    cfg->media_thread_cnt = 1;
     cfg->udp_port = 5060;
     cfg->start_rtp_port = 4000;
@@ -247,4 +249,23 @@
 }
 
+/**
+ * Poll pjsua.
+ */
+PJ_DECL(int) pjsua_handle_events(unsigned msec_timeout)
+{
+    unsigned count = 0;
+    pj_time_val tv;
+    pj_status_t status;
+
+    tv.sec = 0;
+    tv.msec = msec_timeout;
+    pj_time_val_normalize(&tv);
+
+    status = pjsip_endpt_handle_events2(pjsua.endpt, &tv, &count);
+    if (status != PJ_SUCCESS)
+	return -status;
+
+    return count;
+}
 
 
@@ -505,5 +526,7 @@
     /* Must create media endpoint too */
     status = pjmedia_endpt_create(&pjsua.cp.factory, 
-				  pjsip_endpt_get_ioqueue(pjsua.endpt), 0,
+				  pjsua.config.media_has_ioqueue? NULL :
+				       pjsip_endpt_get_ioqueue(pjsua.endpt), 
+				  pjsua.config.media_thread_cnt,
 				  &pjsua.med_endpt);
     if (status != PJ_SUCCESS) {
@@ -755,4 +778,55 @@
 
 
+/*****************************************************************************
+ * Console application custom logging:
+ */
+
+
+static void log_writer(int level, const char *buffer, int len)
+{
+    /* Write to both stdout and file. */
+
+    if (level <= (int)pjsua.config.app_log_level)
+	pj_log_write(level, buffer, len);
+
+    if (pjsua.log_file) {
+	fwrite(buffer, len, 1, pjsua.log_file);
+	fflush(pjsua.log_file);
+    }
+}
+
+
+static pj_status_t logging_init()
+{
+    /* Redirect log function to ours */
+
+    pj_log_set_log_func( &log_writer );
+
+    /* If output log file is desired, create the file: */
+
+    if (pjsua.config.log_filename.slen) {
+	pjsua.log_file = fopen(pjsua.config.log_filename.ptr, "wt");
+	if (pjsua.log_file == NULL) {
+	    PJ_LOG(1,(THIS_FILE, "Unable to open log file %s", 
+		      pjsua.config.log_filename.ptr));   
+	    return -1;
+	}
+    }
+
+    return PJ_SUCCESS;
+}
+
+
+static void logging_shutdown(void)
+{
+    /* Close logging file, if any: */
+
+    if (pjsua.log_file) {
+	fclose(pjsua.log_file);
+	pjsua.log_file = NULL;
+    }
+}
+
+
 /*
  * Initialize pjsua application.
@@ -807,4 +881,8 @@
     pj_log_set_level(pjsua.config.log_level);
     pj_log_set_decor(pjsua.config.log_decor);
+
+    status = logging_init();
+    if (status != PJ_SUCCESS)
+	goto on_error;
 
 
@@ -854,5 +932,5 @@
 	}
 	status = pjmedia_transport_udp_attach(pjsua.med_endpt, NULL,
-					      &pjsua.calls[i].skinfo,
+					      &pjsua.calls[i].skinfo, 0,
 					      &pjsua.calls[i].med_tp);
     }
@@ -1369,5 +1447,5 @@
  * Get conference port associated with player.
  */
-PJ_DEF(unsigned) pjsua_player_get_conf_port(pjsua_player_id id)
+PJ_DEF(int) pjsua_player_get_conf_port(pjsua_player_id id)
 {
     PJ_ASSERT_RETURN(id>=0 && id < PJ_ARRAY_SIZE(pjsua.player), PJ_EINVAL);
@@ -1453,5 +1531,5 @@
  * Get conference port associated with recorder.
  */
-PJ_DEF(unsigned) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
+PJ_DEF(int) pjsua_recorder_get_conf_port(pjsua_recorder_id id)
 {
     PJ_ASSERT_RETURN(id>=0 && id < PJ_ARRAY_SIZE(pjsua.recorder), PJ_EINVAL);
@@ -1521,17 +1599,4 @@
     /* Signal threads to quit: */
     pjsua.quit_flag = 1;
-
-    /* Terminate all calls. */
-    pjsua_call_hangup_all();
-
-    /* Terminate all presence subscriptions. */
-    pjsua_pres_shutdown();
-
-    /* Unregister, if required: */
-    for (i=0; i<(int)pjsua.config.acc_cnt; ++i) {
-	if (pjsua.acc[i].regc) {
-	    pjsua_acc_set_registration(i, PJ_FALSE);
-	}
-    }
 
     /* Wait worker threads to quit: */
@@ -1545,7 +1610,20 @@
     }
 
-
-    /* Wait for some time to allow unregistration to complete: */
+    
     if (pjsua.endpt) {
+	/* Terminate all calls. */
+	pjsua_call_hangup_all();
+
+	/* Terminate all presence subscriptions. */
+	pjsua_pres_shutdown();
+
+	/* Unregister, if required: */
+	for (i=0; i<(int)pjsua.config.acc_cnt; ++i) {
+	    if (pjsua.acc[i].regc) {
+		pjsua_acc_set_registration(i, PJ_FALSE);
+	    }
+	}
+
+	/* Wait for some time to allow unregistration to complete: */
 	PJ_LOG(4,(THIS_FILE, "Shutting down..."));
 	busy_sleep(1000);
@@ -1630,4 +1708,9 @@
 
 
+    PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
+
+    /* End logging */
+    logging_shutdown();
+
     /* Done. */
 
