Ignore:
Timestamp:
Dec 1, 2011 10:49:07 AM (12 years ago)
Author:
ming
Message:

Closed #1420: Add support for event manager

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/include/pjmedia/event.h

    r3774 r3893  
    2626#include <pjmedia/format.h> 
    2727#include <pjmedia/signatures.h> 
    28 #include <pj/list.h> 
    2928 
    3029PJ_BEGIN_DECL 
     
    8988 
    9089/** 
    91  * Forward declaration for event subscription. 
    92  */ 
    93 typedef struct pjmedia_event_subscription pjmedia_event_subscription; 
    94  
    95 /** 
    96  * Forward declaration for event publisher. 
    97  */ 
    98 typedef struct pjmedia_event_publisher pjmedia_event_publisher; 
    99  
    100 /** 
    10190 * Additional data/parameters for media format changed event 
    10291 * (PJMEDIA_EVENT_FMT_CHANGED). 
     
    163152/** 
    164153 * This structure describes a media event. It consists mainly of the event 
    165  * type and additional data/parameters for the event. Event publishers need 
    166  * to use #pjmedia_event_init() to initialize this event structure with 
     154 * type and additional data/parameters for the event. Applications can 
     155 * use #pjmedia_event_init() to initialize this event structure with 
    167156 * basic information about the event. 
    168157 */ 
     
    180169 
    181170    /** 
    182      * This keeps count on the number of subscribers that have 
    183      * processed this event. 
    184      */ 
    185     unsigned                             proc_cnt; 
    186  
    187     /** 
    188      * The object signature of the event publisher. Application may use 
    189      * this to check which publisher published the event. 
    190      */ 
    191     pjmedia_obj_sig                      epub_sig; 
    192  
    193     /** 
    194171     * Pointer information about the source of this event. This field 
    195      * is provided mainly so that the event subscribers can compare it 
    196      * against the publisher that it subscribed the events from initially, 
    197      * a publisher can republish events from other publisher. Event 
    198      * subscription must be careful when using this pointer other than for 
    199      * comparison purpose, since access to the publisher may require special 
    200      * care (e.g. mutex locking). 
    201      */ 
    202     const pjmedia_event_publisher       *epub; 
     172     * is provided mainly for comparison purpose so that event subscribers 
     173     * can check which source the event originated from. Usage of this 
     174     * pointer for other purpose may require special care such as mutex 
     175     * locking or checking whether the object is already destroyed. 
     176     */ 
     177    const void                          *src; 
     178 
     179    /** 
     180     * Pointer information about the publisher of this event. This field 
     181     * is provided mainly for comparison purpose so that event subscribers 
     182     * can check which object published the event. Usage of this 
     183     * pointer for other purpose may require special care such as mutex 
     184     * locking or checking whether the object is already destroyed. 
     185     */ 
     186    const void                          *epub; 
    203187 
    204188    /** 
     
    239223 
    240224/** 
    241  * The callback to receive media events. The callback should increase 
    242  * \a proc_cnt field of the event if it processes the event. 
    243  * 
    244  * @param esub          The subscription that was made initially to receive 
    245  *                      this event. 
    246  * @param event         The media event itself. 
     225 * The callback to receive media events. 
     226 * 
     227 * @param event         The media event. 
     228 * @param user_data     The user data associated with the callback. 
    247229 * 
    248230 * @return              If the callback returns non-PJ_SUCCESS, this return 
    249  *                      code may be propagated back to the producer. 
    250  */ 
    251 typedef pj_status_t pjmedia_event_cb(pjmedia_event_subscription *esub, 
    252                                      pjmedia_event *event); 
    253  
    254 /** 
    255  * This structure keeps the data needed to maintain an event subscription. 
    256  * This data is normally kept by event publishers. 
    257  */ 
    258 struct pjmedia_event_subscription 
    259 { 
    260     /** Standard list members */ 
    261     PJ_DECL_LIST_MEMBER(pjmedia_event_subscription); 
    262  
    263     /** Callback that will be called by publisher to report events. */ 
    264     pjmedia_event_cb    *cb; 
    265  
    266     /** User data for this subscription */ 
    267     void                *user_data; 
    268  
    269     /** Current publisher it is subscribed to */ 
    270     pjmedia_event_publisher *subscribe_to; 
    271 }; 
    272  
    273 /** 
    274  * This describes an event publisher. An event publisher is an object that 
    275  * maintains event subscriptions. When an event is published on behalf of 
    276  * a publisher with #pjmedia_event_publish(), that event will be propagated 
    277  * to all of the subscribers registered to the publisher. 
    278  */ 
    279 struct pjmedia_event_publisher 
    280 { 
    281     /** The object signature of the publisher */ 
    282     pjmedia_obj_sig             sig; 
    283  
    284     /** List of subscriptions for this event publisher */ 
    285     pjmedia_event_subscription  subscription_list; 
    286 }; 
     231 *                      code may be propagated back to the caller. 
     232 */ 
     233typedef pj_status_t pjmedia_event_cb(pjmedia_event *event, 
     234                                     void *user_data); 
     235 
     236/** 
     237 * This enumeration describes flags for event publication via 
     238 * #pjmedia_event_publish(). 
     239 */ 
     240typedef enum pjmedia_event_publish_flag 
     241{ 
     242    /** 
     243     * Publisher will only post the event to the event manager. It is the 
     244     * event manager that will later notify all the publisher's subscribers. 
     245     */ 
     246    PJMEDIA_EVENT_PUBLISH_POST_EVENT = 1 
     247 
     248} pjmedia_event_publish_flag; 
     249 
     250/** 
     251 * Event manager flag. 
     252 */ 
     253typedef enum pjmedia_event_mgr_flag 
     254{ 
     255    /** 
     256     * Tell the event manager not to create any event worker thread. 
     257     */ 
     258    PJMEDIA_EVENT_MGR_NO_THREAD = 1 
     259 
     260} pjmedia_event_mgr_flag; 
     261 
     262/** 
     263 * Opaque data type for event manager. Typically, the event manager 
     264 * is a singleton instance, although application may instantiate more than one 
     265 * instances of this if required. 
     266 */ 
     267typedef struct pjmedia_event_mgr pjmedia_event_mgr; 
     268 
     269/** 
     270 * Create a new event manager instance. This will also set the pointer 
     271 * to the singleton instance if the value is still NULL. 
     272 * 
     273 * @param pool          Pool to allocate memory from. 
     274 * @param options       Options. Bitmask flags from #pjmedia_event_mgr_flag 
     275 * @param mgr           Pointer to hold the created instance of the 
     276 *                      event manager. 
     277 * 
     278 * @return              PJ_SUCCESS on success or the appropriate error code. 
     279 */ 
     280PJ_DECL(pj_status_t) pjmedia_event_mgr_create(pj_pool_t *pool, 
     281                                              unsigned options, 
     282                                              pjmedia_event_mgr **mgr); 
     283 
     284/** 
     285 * Get the singleton instance of the event manager. 
     286 * 
     287 * @return              The instance. 
     288 */ 
     289PJ_DECL(pjmedia_event_mgr*) pjmedia_event_mgr_instance(void); 
     290 
     291/** 
     292 * Manually assign a specific event manager instance as the singleton 
     293 * instance. Normally this is not needed if only one instance is ever 
     294 * going to be created, as the library automatically assign the singleton 
     295 * instance. 
     296 * 
     297 * @param mgr           The instance to be used as the singleton instance. 
     298 *                      Application may specify NULL to clear the singleton 
     299 *                      singleton instance. 
     300 */ 
     301PJ_DECL(void) pjmedia_event_mgr_set_instance(pjmedia_event_mgr *mgr); 
     302 
     303/** 
     304 * Destroy an event manager. If the manager happens to be the singleton 
     305 * instance, the singleton instance will be set to NULL. 
     306 * 
     307 * @param mgr           The eventmanager. Specify NULL to use 
     308 *                      the singleton instance. 
     309 */ 
     310PJ_DECL(void) pjmedia_event_mgr_destroy(pjmedia_event_mgr *mgr); 
    287311 
    288312/** 
     
    293317 * @param ts            Event timestamp. May be set to NULL to set the event 
    294318 *                      timestamp to zero. 
    295  * @param epub          Event publisher. 
     319 * @param src           Event source. 
    296320 */ 
    297321PJ_DECL(void) pjmedia_event_init(pjmedia_event *event, 
    298322                                 pjmedia_event_type type, 
    299323                                 const pj_timestamp *ts, 
    300                                  const pjmedia_event_publisher *epub); 
    301  
    302 /** 
    303  * Initialize an event publisher structure. 
    304  * 
    305  * @param epub          The event publisher. 
    306  * @param sig           The object signature of the publisher. 
    307  */ 
    308 PJ_DECL(void) pjmedia_event_publisher_init(pjmedia_event_publisher *epub, 
    309                                            pjmedia_obj_sig sig); 
    310  
    311 /** 
    312  * Initialize subscription data. 
    313  * 
    314  * @param esub          The event subscription. 
    315  * @param cb            The callback to receive events. 
    316  * @param user_data     Arbitrary user data to be associated with the 
    317  *                      subscription. 
    318  */ 
    319 PJ_DECL(void) pjmedia_event_subscription_init(pjmedia_event_subscription *esub, 
    320                                               pjmedia_event_cb *cb, 
    321                                               void *user_data); 
    322  
    323 /** 
    324  * Subscribe to events published by the specified publisher using the 
    325  * specified subscription object. The callback and user data fields of 
    326  * the subscription object must have been initialized prior to calling 
    327  * this function, and the subscription object must be kept alive throughout 
    328  * the duration of the subscription (e.g. it must not be allocated from 
    329  * the stack). 
    330  * 
    331  * Note that the subscriber may receive not only events emitted by 
     324                                 const void *src); 
     325 
     326/** 
     327 * Subscribe a callback function to events published by the specified 
     328 * publisher. Note that the subscriber may receive not only events emitted by 
    332329 * the specific publisher specified in the argument, but also from other 
    333330 * publishers contained by the publisher, if the publisher is republishing 
    334331 * events from other publishers. 
    335332 * 
     333 * @param mgr           The event manager. 
     334 * @param pool          Pool to allocate memory from. 
     335 * @param cb            The callback function to receive the event. 
     336 * @param user_data     The user data to be associated with the callback 
     337 *                      function. 
    336338 * @param epub          The event publisher. 
    337  * @param esub          The event subscription object. 
    338339 * 
    339340 * @return              PJ_SUCCESS on success or the appropriate error code. 
    340341 */ 
    341 PJ_DECL(pj_status_t) pjmedia_event_subscribe(pjmedia_event_publisher *epub, 
    342                                              pjmedia_event_subscription *esub); 
    343  
    344 /** 
    345  * Unsubscribe the specified subscription object from publisher it is 
    346  * currently subscribed to. If the subscription object is not currently 
    347  * subscribed to anything, the function will do nothing. 
    348  * 
    349  * @param esub          The event subscription object, which must be the same 
    350  *                      object that was given to #pjmedia_event_subscribe(). 
     342PJ_DECL(pj_status_t) pjmedia_event_subscribe(pjmedia_event_mgr *mgr, 
     343                                             pj_pool_t *pool, 
     344                                             pjmedia_event_cb *cb, 
     345                                             void *user_data, 
     346                                             void *epub); 
     347 
     348/** 
     349 * Unsubscribe the callback associated with the user data from a publisher. 
     350 * If the user data is not specified, this function will do the 
     351 * unsubscription for all user data. If the publisher, epub, is not 
     352 * specified, this function will do the unsubscription from all publishers. 
     353 * 
     354 * @param mgr           The event manager. 
     355 * @param cb            The callback function. 
     356 * @param user_data     The user data associated with the callback 
     357 *                      function, can be NULL. 
     358 * @param epub          The event publisher, can be NULL. 
    351359 * 
    352360 * @return              PJ_SUCCESS on success or the appropriate error code. 
    353361 */ 
    354362PJ_DECL(pj_status_t) 
    355 pjmedia_event_unsubscribe(pjmedia_event_subscription *esub); 
    356  
    357 /** 
    358  * Check if the specified publisher has subscribers. 
    359  * 
    360  * @param epub          The event publisher. 
    361  * 
    362  * @return              PJ_TRUE if the publisher has at least one subscriber. 
    363  */ 
    364 PJ_DECL(pj_bool_t) 
    365 pjmedia_event_publisher_has_sub(pjmedia_event_publisher *epub); 
     363pjmedia_event_unsubscribe(pjmedia_event_mgr *mgr, 
     364                          pjmedia_event_cb *cb, 
     365                          void *user_data, 
     366                          void *epub); 
    366367 
    367368/** 
    368369 * Publish the specified event to all subscribers of the specified event 
    369  * publisher. 
    370  * 
     370 * publisher. By default, the function will call all the subcribers' 
     371 * callbacks immediately. If the publisher uses the flag 
     372 * PJMEDIA_EVENT_PUBLISH_POST_EVENT, publisher will only post the event 
     373 * to the event manager and return immediately. It is the event manager 
     374 * that will later notify all the publisher's subscribers. 
     375 * 
     376 * @param mgr           The event manager. 
    371377 * @param epub          The event publisher. 
    372378 * @param event         The event to be published. 
     379 * @param flag          Publication flag. 
    373380 * 
    374381 * @return              PJ_SUCCESS only if all subscription callbacks returned 
    375382 *                      PJ_SUCCESS. 
    376383 */ 
    377 PJ_DECL(pj_status_t) pjmedia_event_publish(pjmedia_event_publisher *epub, 
    378                                            pjmedia_event *event); 
    379  
    380 /** 
    381  * Subscribe to events produced by the source publisher in \a esrc and 
    382  * republish the events to all subscribers in \a epub publisher. 
    383  * 
    384  * @param esrc          The event source from which events will be 
    385  *                      republished. 
    386  * @param epub          Events from the event source above will be 
    387  *                      republished to subscribers of this publisher. 
    388  * @param esub          The subscription object to be used to subscribe 
    389  *                      to \a esrc. This doesn't need to be initialized, 
    390  *                      but it must be kept alive throughout the lifetime 
    391  *                      of the subsciption. 
    392  * 
    393  * @return              PJ_SUCCESS only if all subscription callbacks returned 
    394  *                      PJ_SUCCESS. 
    395  */ 
    396 PJ_DECL(pj_status_t) pjmedia_event_republish(pjmedia_event_publisher *esrc, 
    397                                              pjmedia_event_publisher *epub, 
    398                                              pjmedia_event_subscription *esub); 
     384PJ_DECL(pj_status_t) pjmedia_event_publish(pjmedia_event_mgr *mgr, 
     385                                           void *epub, 
     386                                           pjmedia_event *event, 
     387                                           pjmedia_event_publish_flag flag); 
     388 
    399389 
    400390/** 
Note: See TracChangeset for help on using the changeset viewer.