Index: pjproject/trunk/pjmedia/src/pjmedia/vid_port.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia/vid_port.c	(revision 3758)
+++ pjproject/trunk/pjmedia/src/pjmedia/vid_port.c	(revision 3893)
@@ -62,8 +62,4 @@
     pjmedia_conversion_param conv_param;
 
-    pjmedia_event_publisher    epub;
-    pjmedia_event_subscription esub_dev;
-    pjmedia_event_subscription esub_client_port;
-
     pjmedia_clock           *clock;
     pjmedia_clock_src        clocksrc;
@@ -95,8 +91,8 @@
 				       void *user_data,
 				       pjmedia_frame *frame);
-static pj_status_t vidstream_event_cb(pjmedia_event_subscription *esub,
-			              pjmedia_event *event);
-static pj_status_t client_port_event_cb(pjmedia_event_subscription *esub,
-                                        pjmedia_event *event);
+static pj_status_t vidstream_event_cb(pjmedia_event *event,
+                                      void *user_data);
+static pj_status_t client_port_event_cb(pjmedia_event *event,
+                                        void *user_data);
 
 static void enc_clock_cb(const pj_timestamp *ts, void *user_data);
@@ -207,5 +203,4 @@
     vp->dir = prm->vidparam.dir;
 //    vp->cap_size = vfd->size;
-    pjmedia_event_publisher_init(&vp->epub, SIGNATURE);
 
     vparam = prm->vidparam;
@@ -273,8 +268,6 @@
 
     /* Subscribe to device's events */
-    pjmedia_event_subscription_init(&vp->esub_dev, vidstream_event_cb, vp);
-    pjmedia_event_subscribe(
-	    pjmedia_vid_dev_stream_get_event_publisher(vp->strm),
-	    &vp->esub_dev);
+    pjmedia_event_subscribe(NULL, vp->pool, &vidstream_event_cb,
+                            vp, vp->strm);
 
     if (vp->dir & PJMEDIA_DIR_CAPTURE) {
@@ -371,11 +364,4 @@
 }
 
-PJ_DEF(pjmedia_event_publisher*)
-pjmedia_vid_port_get_event_publisher(pjmedia_vid_port *vid_port)
-{
-    PJ_ASSERT_RETURN(vid_port, NULL);
-    return &vid_port->epub;
-}
-
 PJ_DEF(pjmedia_vid_dev_stream*)
 pjmedia_vid_port_get_stream(pjmedia_vid_port *vp)
@@ -420,6 +406,4 @@
 					      pj_bool_t destroy)
 {
-    pjmedia_event_publisher *epub;
-
     PJ_ASSERT_RETURN(vp && vp->role==ROLE_ACTIVE, PJ_EINVAL);
     vp->destroy_client_port = destroy;
@@ -427,11 +411,7 @@
 
     /* Subscribe to client port's events */
-    epub = pjmedia_port_get_event_publisher(port);
-    if (epub) {
-	pjmedia_event_subscription_init(&vp->esub_client_port,
-	                                &client_port_event_cb,
-	                                vp);
-	pjmedia_event_subscribe(epub, &vp->esub_client_port);
-    }
+    pjmedia_event_subscribe(NULL, vp->pool, &client_port_event_cb, vp,
+                            vp->client_port);
+
     return PJ_SUCCESS;
 }
@@ -442,8 +422,8 @@
     PJ_ASSERT_RETURN(vp && vp->role==ROLE_ACTIVE, PJ_EINVAL);
 
-    if (vp->client_port) {
-	pjmedia_event_unsubscribe(&vp->esub_client_port);
-	vp->client_port = NULL;
-    }
+    pjmedia_event_unsubscribe(NULL, &client_port_event_cb, vp,
+                              vp->client_port);
+    vp->client_port = NULL;
+
     return PJ_SUCCESS;
 }
@@ -511,8 +491,11 @@
     }
     if (vp->strm) {
+        pjmedia_event_unsubscribe(NULL, &vidstream_event_cb, vp, vp->strm);
 	pjmedia_vid_dev_stream_destroy(vp->strm);
 	vp->strm = NULL;
     }
     if (vp->client_port) {
+        pjmedia_event_unsubscribe(NULL, &client_port_event_cb, vp,
+                                  vp->client_port);
 	if (vp->destroy_client_port)
 	    pjmedia_port_destroy(vp->client_port);
@@ -561,17 +544,17 @@
 
 /* Handle event from vidstream */
-static pj_status_t vidstream_event_cb(pjmedia_event_subscription *esub,
-				      pjmedia_event *event)
-{
-    pjmedia_vid_port *vp = (pjmedia_vid_port*)esub->user_data;
+static pj_status_t vidstream_event_cb(pjmedia_event *event,
+                                      void *user_data)
+{
+    pjmedia_vid_port *vp = (pjmedia_vid_port*)user_data;
     
     /* Just republish the event to our client */
-    return pjmedia_event_publish(&vp->epub, event);
-}
-
-static pj_status_t client_port_event_cb(pjmedia_event_subscription *esub,
-                                        pjmedia_event *event)
-{
-    pjmedia_vid_port *vp = (pjmedia_vid_port*)esub->user_data;
+    return pjmedia_event_publish(NULL, vp, event, 0);
+}
+
+static pj_status_t client_port_event_cb(pjmedia_event *event,
+                                        void *user_data)
+{
+    pjmedia_vid_port *vp = (pjmedia_vid_port*)user_data;
 
     if (event->type == PJMEDIA_EVENT_FMT_CHANGED) {
@@ -579,6 +562,4 @@
         pj_status_t status;
         
-        ++event->proc_cnt;
-
 	pjmedia_vid_port_stop(vp);
         
@@ -634,6 +615,9 @@
     }
     
-    /* Republish the event */
-    return pjmedia_event_publish(&vp->epub, event);
+    /* Republish the event, post the event to the event manager
+     * to avoid deadlock if vidport is trying to stop the clock.
+     */
+    return pjmedia_event_publish(NULL, vp, event,
+                                 PJMEDIA_EVENT_PUBLISH_POST_EVENT);
 }
 
