Ticket #933: Incoming OPTIONS may trigger assertion if it arrives when PJSUA-LIB is being shutdown (thanks Johan Lantz for the report)
 - destroy the media subsystem after busy_sleep(1000) in the shutdown sequence
 - also handle the case when OPTIONS arrives just when PJSUA-LIB is being initialized and media transport is not ready (in this case just reply OPTIONS without message body)


git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@2871 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjsip/src/pjsua-lib/pjsua_core.c b/pjsip/src/pjsua-lib/pjsua_core.c
index c4f38d9..91b0271 100644
--- a/pjsip/src/pjsua-lib/pjsua_core.c
+++ b/pjsip/src/pjsua-lib/pjsua_core.c
@@ -348,15 +348,17 @@
 	pjsip_msg_add_hdr(tdata->msg, h);
     }
 
-    /* Get media socket info */
-    pjmedia_transport_info_init(&tpinfo);
-    pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &tpinfo);
+    /* Get media socket info, make sure transport is ready */
+    if (pjsua_var.calls[0].med_tp) {
+	pjmedia_transport_info_init(&tpinfo);
+	pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &tpinfo);
 
-    /* Add SDP body, using call0's RTP address */
-    status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
-				      &tpinfo.sock_info, &sdp);
-    if (status == PJ_SUCCESS) {
-	pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
+	/* Add SDP body, using call0's RTP address */
+	status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
+					  &tpinfo.sock_info, &sdp);
+	if (status == PJ_SUCCESS) {
+	    pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
+	}
     }
 
     /* Send response statelessly */
@@ -1248,9 +1250,6 @@
 	}
     }
 
-    /* Destroy media */
-    pjsua_media_subsys_destroy();
-
     /* Destroy endpoint. */
     if (pjsua_var.endpt) {
 
@@ -1272,6 +1271,16 @@
 
 	PJ_LOG(4,(THIS_FILE, "Destroying..."));
 
+	/* Terminate all calls again, just in case there's new call
+	 * picked up during busy_sleep()
+	 */
+	pjsua_call_hangup_all();
+
+	/* Destroy media after all polling is done, as there may be
+	 * incoming request that needs handling (e.g. OPTIONS)
+	 */
+	pjsua_media_subsys_destroy();
+
 	/* Must destroy endpoint first before destroying pools in
 	 * buddies or accounts, since shutting down transaction layer
 	 * may emit events which trigger some buddy or account callbacks
@@ -1295,6 +1304,9 @@
 		pjsua_var.acc[i].pool = NULL;
 	    }
 	}
+    } else {
+	/* Destroy media */
+	pjsua_media_subsys_destroy();
     }
 
     /* Destroy mutex */