Re #1420: Create a pool for the event manager so subscriber doesn't need to supply its own pool.



git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@3905 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjmedia/include/pjmedia/event.h b/pjmedia/include/pjmedia/event.h
index ba06625..84baba9 100644
--- a/pjmedia/include/pjmedia/event.h
+++ b/pjmedia/include/pjmedia/event.h
@@ -331,7 +331,6 @@
  * events from other publishers.
  *
  * @param mgr		The event manager.
- * @param pool          Pool to allocate memory from.
  * @param cb            The callback function to receive the event.
  * @param user_data     The user data to be associated with the callback
  *                      function.
@@ -340,7 +339,6 @@
  * @return		PJ_SUCCESS on success or the appropriate error code.
  */
 PJ_DECL(pj_status_t) pjmedia_event_subscribe(pjmedia_event_mgr *mgr,
-                                             pj_pool_t *pool,
                                              pjmedia_event_cb *cb,
                                              void *user_data,
                                              void *epub);
diff --git a/pjmedia/src/pjmedia/event.c b/pjmedia/src/pjmedia/event.c
index 10df2b5..0dfcacb 100644
--- a/pjmedia/src/pjmedia/event.c
+++ b/pjmedia/src/pjmedia/event.c
@@ -49,6 +49,7 @@
 
 struct pjmedia_event_mgr
 {
+    pj_pool_t      *pool;
     pj_thread_t    *thread;             /**< worker thread.             */
     pj_bool_t       is_quitting;
     pj_sem_t       *sem;
@@ -56,6 +57,7 @@
     event_queue     ev_queue;
     event_queue    *pub_ev_queue;       /**< publish() event queue.     */
     esub            esub_list;          /**< list of subscribers.       */
+    esub            free_esub_list;     /**< list of subscribers.       */
     esub           *th_next_sub,        /**< worker thread's next sub.  */
                    *pub_next_sub;       /**< publish() next sub.        */
 };
@@ -154,14 +156,18 @@
     pj_status_t status;
 
     mgr = PJ_POOL_ZALLOC_T(pool, pjmedia_event_mgr);
+    mgr->pool = pj_pool_create(pool->factory, "evt mgr", 500, 500, NULL);
     pj_list_init(&mgr->esub_list);
+    pj_list_init(&mgr->free_esub_list);
 
     if (!(options & PJMEDIA_EVENT_MGR_NO_THREAD)) {
-        status = pj_sem_create(pool, "ev_sem", 0, MAX_EVENTS + 1, &mgr->sem);
+        status = pj_sem_create(mgr->pool, "ev_sem", 0, MAX_EVENTS + 1,
+                               &mgr->sem);
         if (status != PJ_SUCCESS)
             return status;
 
-        status = pj_thread_create(pool, "ev_thread", &event_worker_thread,
+        status = pj_thread_create(mgr->pool, "ev_thread",
+                                  &event_worker_thread,
                                   mgr, 0, 0, &mgr->thread);
         if (status != PJ_SUCCESS) {
             pjmedia_event_mgr_destroy(mgr);
@@ -169,7 +175,7 @@
         }
     }
 
-    status = pj_mutex_create_recursive(pool, "ev_mutex", &mgr->mutex);
+    status = pj_mutex_create_recursive(mgr->pool, "ev_mutex", &mgr->mutex);
     if (status != PJ_SUCCESS) {
         pjmedia_event_mgr_destroy(mgr);
         return status;
@@ -196,8 +202,6 @@
 
 PJ_DEF(void) pjmedia_event_mgr_destroy(pjmedia_event_mgr *mgr)
 {
-    esub *sub;
-
     if (!mgr) mgr = pjmedia_event_mgr_instance();
     PJ_ASSERT_ON_FAIL(mgr != NULL, return);
 
@@ -217,12 +221,8 @@
         mgr->mutex = NULL;
     }
 
-    sub = mgr->esub_list.next;
-    while (sub != &mgr->esub_list) {
-	esub *next = sub->next;
-	pj_list_erase(sub);
-	sub = next;
-    }
+    if (mgr->pool)
+        pj_pool_release(mgr->pool);
 
     if (event_manager_instance == mgr)
 	event_manager_instance = NULL;
@@ -241,14 +241,13 @@
 }
 
 PJ_DEF(pj_status_t) pjmedia_event_subscribe( pjmedia_event_mgr *mgr,
-                                             pj_pool_t *pool,
                                              pjmedia_event_cb *cb,
                                              void *user_data,
                                              void *epub)
 {
     esub *sub;
 
-    PJ_ASSERT_RETURN(pool && cb, PJ_EINVAL);
+    PJ_ASSERT_RETURN(cb, PJ_EINVAL);
 
     if (!mgr) mgr = pjmedia_event_mgr_instance();
     PJ_ASSERT_RETURN(mgr, PJ_EINVAL);
@@ -270,7 +269,11 @@
 	sub = next;
     }
 
-    sub = PJ_POOL_ZALLOC_T(pool, esub);
+    if (mgr->free_esub_list.next != &mgr->free_esub_list) {
+        sub = mgr->free_esub_list.next;
+        pj_list_erase(sub);
+    } else
+        sub = PJ_POOL_ZALLOC_T(mgr->pool, esub);
     sub->cb = cb;
     sub->user_data = user_data;
     sub->epub = epub;
@@ -309,6 +312,7 @@
             if (mgr->pub_next_sub == sub)
                 mgr->pub_next_sub = sub->next;
             pj_list_erase(sub);
+            pj_list_push_back(&mgr->free_esub_list, sub);
             if (user_data && epub)
                 break;
         }
diff --git a/pjmedia/src/pjmedia/vid_port.c b/pjmedia/src/pjmedia/vid_port.c
index 1c1cded..1e292fb 100644
--- a/pjmedia/src/pjmedia/vid_port.c
+++ b/pjmedia/src/pjmedia/vid_port.c
@@ -267,7 +267,7 @@
 	      vparam.fmt.det.vid.fps.num, vparam.fmt.det.vid.fps.denum));
 
     /* Subscribe to device's events */
-    pjmedia_event_subscribe(NULL, vp->pool, &vidstream_event_cb,
+    pjmedia_event_subscribe(NULL, &vidstream_event_cb,
                             vp, vp->strm);
 
     if (vp->dir & PJMEDIA_DIR_CAPTURE) {
@@ -410,7 +410,7 @@
     vp->client_port = port;
 
     /* Subscribe to client port's events */
-    pjmedia_event_subscribe(NULL, vp->pool, &client_port_event_cb, vp,
+    pjmedia_event_subscribe(NULL, &client_port_event_cb, vp,
                             vp->client_port);
 
     return PJ_SUCCESS;
diff --git a/pjmedia/src/pjmedia/vid_stream.c b/pjmedia/src/pjmedia/vid_stream.c
index 8d05577..7b0b1f9 100644
--- a/pjmedia/src/pjmedia/vid_stream.c
+++ b/pjmedia/src/pjmedia/vid_stream.c
@@ -1361,7 +1361,7 @@
 	return status;
 
     /* Subscribe to codec events */
-    pjmedia_event_subscribe(NULL, pool, &stream_event_cb, stream,
+    pjmedia_event_subscribe(NULL, &stream_event_cb, stream,
                             stream->codec);
 
     /* Estimate the maximum frame size */
diff --git a/pjmedia/src/test/vid_codec_test.c b/pjmedia/src/test/vid_codec_test.c
index 3df532a..5d1af67 100644
--- a/pjmedia/src/test/vid_codec_test.c
+++ b/pjmedia/src/test/vid_codec_test.c
@@ -320,7 +320,7 @@
 	codec_param.dec_fmt.det = codec_param.enc_fmt.det;
 
 	/* Subscribe to codec events */
-	pjmedia_event_subscribe(NULL, pool, &codec_on_event, &codec_port_data,
+	pjmedia_event_subscribe(NULL, &codec_on_event, &codec_port_data,
                                 codec);
     }
 
diff --git a/pjmedia/src/test/vid_dev_test.c b/pjmedia/src/test/vid_dev_test.c
index 5782337..8f78094 100644
--- a/pjmedia/src/test/vid_dev_test.c
+++ b/pjmedia/src/test/vid_dev_test.c
@@ -160,7 +160,7 @@
     }
 
     /* Set event handler */
-    pjmedia_event_subscribe(NULL, pool, &vid_event_cb, NULL, renderer);
+    pjmedia_event_subscribe(NULL, &vid_event_cb, NULL, renderer);
 
     /* Connect capture to renderer */
     status = pjmedia_vid_port_connect(
diff --git a/pjmedia/src/test/vid_port_test.c b/pjmedia/src/test/vid_port_test.c
index dd3bf37..82f0c1b 100644
--- a/pjmedia/src/test/vid_port_test.c
+++ b/pjmedia/src/test/vid_port_test.c
@@ -117,7 +117,7 @@
     }
 
     /* Set event handler */
-    pjmedia_event_subscribe(NULL, pool, &vid_event_cb, NULL, renderer);
+    pjmedia_event_subscribe(NULL, &vid_event_cb, NULL, renderer);
 
     /* Connect capture to renderer */
     status = pjmedia_vid_port_connect(
diff --git a/pjsip-apps/src/samples/aviplay.c b/pjsip-apps/src/samples/aviplay.c
index bdeab3f..03fbe80 100644
--- a/pjsip-apps/src/samples/aviplay.c
+++ b/pjsip-apps/src/samples/aviplay.c
@@ -401,7 +401,7 @@
         pjmedia_vid_port_set_cb(renderer, &cb, &avi_port);
 
         /* subscribe events */
-        pjmedia_event_subscribe(NULL, pool, &avi_event_cb, &avi_port,
+        pjmedia_event_subscribe(NULL, &avi_event_cb, &avi_port,
                                 renderer);
 
         if (snd_port) {
diff --git a/pjsip/src/pjsua-lib/pjsua_vid.c b/pjsip/src/pjsua-lib/pjsua_vid.c
index b8768c0..1ca322f 100644
--- a/pjsip/src/pjsua-lib/pjsua_vid.c
+++ b/pjsip/src/pjsua-lib/pjsua_vid.c
@@ -829,7 +829,7 @@
 
 #if ENABLE_EVENT
 	    /* Register to video events */
-	    pjmedia_event_subscribe(NULL, w->pool, &call_media_on_event,
+	    pjmedia_event_subscribe(NULL, &call_media_on_event,
                                     call_med, w->vp_rend);
 #endif
 	    
@@ -899,7 +899,7 @@
 
 	    w = &pjsua_var.win[wid];
 #if ENABLE_EVENT
-            pjmedia_event_subscribe(NULL, w->pool, &call_media_on_event,
+            pjmedia_event_subscribe(NULL, &call_media_on_event,
                                     call_med, w->vp_cap);
 #endif
 	    
@@ -1908,7 +1908,7 @@
     }
 
 #if ENABLE_EVENT
-    pjmedia_event_subscribe(NULL, new_w->pool, &call_media_on_event,
+    pjmedia_event_subscribe(NULL, &call_media_on_event,
                             call_med, new_w->vp_cap);
 #endif
 
@@ -1950,7 +1950,7 @@
 
 #if ENABLE_EVENT
     /* Resubscribe */
-    pjmedia_event_subscribe(NULL, w->pool, &call_media_on_event,
+    pjmedia_event_subscribe(NULL, &call_media_on_event,
                             call_med, w->vp_cap);
 #endif