예제 #1
0
파일: event.c 프로젝트: LuLei2013/pjproject
PJ_DEF(pj_status_t) pjmedia_event_publish( pjmedia_event_mgr *mgr,
                                           void *epub,
                                           pjmedia_event *event,
                                           pjmedia_event_publish_flag flag)
{
    pj_status_t err = PJ_SUCCESS;

    PJ_ASSERT_RETURN(epub && event, PJ_EINVAL);

    if (!mgr) mgr = pjmedia_event_mgr_instance();
    PJ_ASSERT_RETURN(mgr, PJ_EINVAL);

    event->epub = epub;

    pj_mutex_lock(mgr->mutex);
    if (flag & PJMEDIA_EVENT_PUBLISH_POST_EVENT) {
        if (event_queue_add_event(&mgr->ev_queue, event) == PJ_SUCCESS)
            pj_sem_post(mgr->sem);
    } else {
        /* For nested pjmedia_event_publish() calls, i.e. calling publish()
         * inside the subscriber's callback, the function will only add
         * the event to the event queue of the first publish() call. It
         * is the first publish() call that will be responsible to
         * distribute the events.
         */
        if (mgr->pub_ev_queue) {
            event_queue_add_event(mgr->pub_ev_queue, event);
        } else {
            static event_queue ev_queue;
            pj_status_t status;

            ev_queue.head = ev_queue.tail = 0;
            ev_queue.is_full = PJ_FALSE;
            mgr->pub_ev_queue = &ev_queue;

            event_queue_add_event(mgr->pub_ev_queue, event);

            do {
                status = event_mgr_distribute_events(mgr, mgr->pub_ev_queue,
                                                     &mgr->pub_next_sub,
                                                     PJ_FALSE);
                if (status != PJ_SUCCESS && err == PJ_SUCCESS)
	            err = status;
            } while(ev_queue.head != ev_queue.tail || ev_queue.is_full);

            mgr->pub_ev_queue = NULL;
        }
    }
    pj_mutex_unlock(mgr->mutex);

    return err;
}
예제 #2
0
static e_uint32 event_queue_peep_events(Event* events, int numevents, EventAction action)
{
	int i, used;
	if(!EventQueue.active){
		return -1;
	}

	used = 0;
	if(mutex_lock(&EventQueue.lock) == 1){
		//DMSG((STDOUT,"EventQ locked \n"));

		if(action == ADDEVENT)
		{
			for(i = 0; i < numevents; i++){
				DMSG((STDOUT,"add event\n"));
				used += event_queue_add_event(&events[i]);
			}
		}
		else
		{
			Event tmpevent;
			int spot;
			if(events == NULL){
				action = PEEPEVENT;
				numevents = 1;
				events = &tmpevent;
			}
			spot = EventQueue.head;
			while((used < numevents) && (spot != EventQueue.tail)){
				events[used++] = EventQueue.event[spot];
				if(action == GETEVENT){
					//DMSG((STDOUT,"get Event: type %s\n",(char *)event_to_str(EventQueue.event[spot].type)));
					spot = event_queue_cut_event(spot);
				}else{
					spot = (spot + 1) % MAXEVENTS;
				}
			}
		}
		mutex_unlock(&EventQueue.lock);
	}else{
		DMSG((STDOUT,"Couldn't lock the event queue\n"));
		used = -1;
	}
	return used;
}