Index: pjproject/trunk/pjmedia/src/pjmedia/endpoint.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia/endpoint.c	(revision 4240)
+++ pjproject/trunk/pjmedia/src/pjmedia/endpoint.c	(revision 4474)
@@ -210,4 +210,101 @@
 {
     exit_cb *ecb;
+
+    pjmedia_endpt_stop_threads(endpt);
+
+    /* Destroy internal ioqueue */
+    if (endpt->ioqueue && endpt->own_ioqueue) {
+	pj_ioqueue_destroy(endpt->ioqueue);
+	endpt->ioqueue = NULL;
+    }
+
+    endpt->pf = NULL;
+
+    pjmedia_codec_mgr_destroy(&endpt->codec_mgr);
+    pjmedia_aud_subsys_shutdown();
+
+    /* Call all registered exit callbacks */
+    ecb = endpt->exit_cb_list.next;
+    while (ecb != &endpt->exit_cb_list) {
+	(*ecb->func)(endpt);
+	ecb = ecb->next;
+    }
+
+    pj_pool_release (endpt->pool);
+
+    return PJ_SUCCESS;
+}
+
+PJ_DEF(pj_status_t) pjmedia_endpt_set_flag( pjmedia_endpt *endpt,
+					    pjmedia_endpt_flag flag,
+					    const void *value)
+{
+    PJ_ASSERT_RETURN(endpt, PJ_EINVAL);
+
+    switch (flag) {
+    case PJMEDIA_ENDPT_HAS_TELEPHONE_EVENT_FLAG:
+	endpt->has_telephone_event = *(pj_bool_t*)value;
+	break;
+    default:
+	return PJ_EINVAL;
+    }
+
+    return PJ_SUCCESS;
+}
+
+PJ_DEF(pj_status_t) pjmedia_endpt_get_flag( pjmedia_endpt *endpt,
+					    pjmedia_endpt_flag flag,
+					    void *value)
+{
+    PJ_ASSERT_RETURN(endpt, PJ_EINVAL);
+
+    switch (flag) {
+    case PJMEDIA_ENDPT_HAS_TELEPHONE_EVENT_FLAG:
+	*(pj_bool_t*)value = endpt->has_telephone_event;
+	break;
+    default:
+	return PJ_EINVAL;
+    }
+
+    return PJ_SUCCESS;
+}
+
+/**
+ * Get the ioqueue instance of the media endpoint.
+ */
+PJ_DEF(pj_ioqueue_t*) pjmedia_endpt_get_ioqueue(pjmedia_endpt *endpt)
+{
+    PJ_ASSERT_RETURN(endpt, NULL);
+    return endpt->ioqueue;
+}
+
+/**
+ * Get the number of worker threads in media endpoint.
+ */
+PJ_DEF(unsigned) pjmedia_endpt_get_thread_count(pjmedia_endpt *endpt)
+{
+    PJ_ASSERT_RETURN(endpt, 0);
+    return endpt->thread_cnt;
+}
+
+/**
+ * Get a reference to one of the worker threads of the media endpoint 
+ */
+PJ_DEF(pj_thread_t*) pjmedia_endpt_get_thread(pjmedia_endpt *endpt, 
+					      unsigned index)
+{
+    PJ_ASSERT_RETURN(endpt, NULL);
+    PJ_ASSERT_RETURN(index < endpt->thread_cnt, NULL);
+
+    /* here should be an assert on index >= 0 < endpt->thread_cnt */
+
+    return endpt->thread[index];
+}
+
+/**
+ * Stop and destroy the worker threads of the media endpoint
+ */
+PJ_DEF(pj_status_t) pjmedia_endpt_stop_threads(pjmedia_endpt *endpt)
+{
     unsigned i;
 
@@ -225,91 +322,5 @@
     }
 
-    /* Destroy internal ioqueue */
-    if (endpt->ioqueue && endpt->own_ioqueue) {
-	pj_ioqueue_destroy(endpt->ioqueue);
-	endpt->ioqueue = NULL;
-    }
-
-    endpt->pf = NULL;
-
-    pjmedia_codec_mgr_destroy(&endpt->codec_mgr);
-    pjmedia_aud_subsys_shutdown();
-
-    /* Call all registered exit callbacks */
-    ecb = endpt->exit_cb_list.next;
-    while (ecb != &endpt->exit_cb_list) {
-	(*ecb->func)(endpt);
-	ecb = ecb->next;
-    }
-
-    pj_pool_release (endpt->pool);
-
-    return PJ_SUCCESS;
-}
-
-PJ_DEF(pj_status_t) pjmedia_endpt_set_flag( pjmedia_endpt *endpt,
-					    pjmedia_endpt_flag flag,
-					    const void *value)
-{
-    PJ_ASSERT_RETURN(endpt, PJ_EINVAL);
-
-    switch (flag) {
-    case PJMEDIA_ENDPT_HAS_TELEPHONE_EVENT_FLAG:
-	endpt->has_telephone_event = *(pj_bool_t*)value;
-	break;
-    default:
-	return PJ_EINVAL;
-    }
-
-    return PJ_SUCCESS;
-}
-
-PJ_DEF(pj_status_t) pjmedia_endpt_get_flag( pjmedia_endpt *endpt,
-					    pjmedia_endpt_flag flag,
-					    void *value)
-{
-    PJ_ASSERT_RETURN(endpt, PJ_EINVAL);
-
-    switch (flag) {
-    case PJMEDIA_ENDPT_HAS_TELEPHONE_EVENT_FLAG:
-	*(pj_bool_t*)value = endpt->has_telephone_event;
-	break;
-    default:
-	return PJ_EINVAL;
-    }
-
-    return PJ_SUCCESS;
-}
-
-/**
- * Get the ioqueue instance of the media endpoint.
- */
-PJ_DEF(pj_ioqueue_t*) pjmedia_endpt_get_ioqueue(pjmedia_endpt *endpt)
-{
-    PJ_ASSERT_RETURN(endpt, NULL);
-    return endpt->ioqueue;
-}
-
-/**
- * Get the number of worker threads in media endpoint.
- */
-PJ_DEF(unsigned) pjmedia_endpt_get_thread_count(pjmedia_endpt *endpt)
-{
-    PJ_ASSERT_RETURN(endpt, 0);
-    return endpt->thread_cnt;
-}
-
-/**
- * Get a reference to one of the worker threads of the media endpoint 
- */
-PJ_DEF(pj_thread_t*) pjmedia_endpt_get_thread(pjmedia_endpt *endpt, 
-					      unsigned index)
-{
-    PJ_ASSERT_RETURN(endpt, NULL);
-    PJ_ASSERT_RETURN(index < endpt->thread_cnt, NULL);
-
-    /* here should be an assert on index >= 0 < endpt->thread_cnt */
-
-    return endpt->thread[index];
+    return PJ_SUCCESS;
 }
 
