event.c 5.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) implementation with additional features.
 * Copyright (C) 2017 Belledonne Communications SARL
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
aymeric's avatar
aymeric committed
19 20 21

#include "ortp/event.h"
#include "ortp/ortp.h"
22 23
#include "ortp/rtpsession.h"
#include "utils.h"
aymeric's avatar
aymeric committed
24 25

OrtpEvent * ortp_event_new(unsigned long type){
26
	OrtpEventData *ed;
aymeric's avatar
aymeric committed
27 28 29 30
	const int size=sizeof(OrtpEventType)+sizeof(OrtpEventData);
	mblk_t *m=allocb(size,0);
	memset(m->b_wptr,0,size);
	*((OrtpEventType*)m->b_wptr)=type;
31 32
	ed = ortp_event_get_data(m);
	ortp_get_cur_time(&ed->ts);
aymeric's avatar
aymeric committed
33 34 35 36 37 38 39
	return m;
}

OrtpEvent *ortp_event_dup(OrtpEvent *ev){
	OrtpEvent *nev = ortp_event_new(ortp_event_get_type(ev));
	OrtpEventData * ed = ortp_event_get_data(ev);
	OrtpEventData * edv = ortp_event_get_data(nev);
40
	memcpy(edv,ed,sizeof(OrtpEventData));
aymeric's avatar
aymeric committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	if (ed->packet) edv->packet = copymsg(ed->packet);
	return nev;
}

OrtpEventType ortp_event_get_type(const OrtpEvent *ev){
	return ((OrtpEventType*)ev->b_rptr)[0];
}

OrtpEventData * ortp_event_get_data(OrtpEvent *ev){
	return (OrtpEventData*)(ev->b_rptr+sizeof(OrtpEventType));
}

void ortp_event_destroy(OrtpEvent *ev){
	OrtpEventData *d=ortp_event_get_data(ev);
	if (ev->b_datap->db_ref==1){
		if (d->packet) 	freemsg(d->packet);
	}
	freemsg(ev);
}

OrtpEvQueue * ortp_ev_queue_new(){
	OrtpEvQueue *q=ortp_new(OrtpEvQueue,1);
	qinit(&q->q);
	ortp_mutex_init(&q->mutex,NULL);
	return q;
}

void ortp_ev_queue_flush(OrtpEvQueue * qp){
	OrtpEvent *ev;
	while((ev=ortp_ev_queue_get(qp))!=NULL){
		ortp_event_destroy(ev);
	}
}

OrtpEvent * ortp_ev_queue_get(OrtpEvQueue *q){
	OrtpEvent *ev;
	ortp_mutex_lock(&q->mutex);
	ev=getq(&q->q);
	ortp_mutex_unlock(&q->mutex);
	return ev;
}

void ortp_ev_queue_destroy(OrtpEvQueue * qp){
	ortp_ev_queue_flush(qp);
	ortp_mutex_destroy(&qp->mutex);
	ortp_free(qp);
}

void ortp_ev_queue_put(OrtpEvQueue *q, OrtpEvent *ev){
	ortp_mutex_lock(&q->mutex);
	putq(&q->q,ev);
	ortp_mutex_unlock(&q->mutex);
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
static bool_t rtcp_is_type(const mblk_t *m, rtcp_type_t type){
	const rtcp_common_header_t *ch=rtcp_get_common_header(m);
	return (ch!=NULL && rtcp_common_header_get_packet_type(ch)==type);
}

OrtpEvDispatcher * ortp_ev_dispatcher_new(RtpSession* session) {
	OrtpEvDispatcher *d=ortp_new(OrtpEvDispatcher,1);
	d->session = session;
	d->q = ortp_ev_queue_new();
	rtp_session_register_event_queue(session, d->q);
	d->cbs = NULL;

	return d;
}

void ortp_ev_dispatcher_destroy(OrtpEvDispatcher *d) {
	OList* it;
	for (it=d->cbs;it!=NULL;it=it->next){
		ortp_free(it->data);
	}
	o_list_free(d->cbs);
	rtp_session_unregister_event_queue(d->session, d->q);
	ortp_ev_queue_destroy(d->q);
	ortp_free(d);
}

static bool_t is_rtcp_event(OrtpEventType type) {
	return (type == ORTP_EVENT_RTCP_PACKET_RECEIVED || type == ORTP_EVENT_RTCP_PACKET_EMITTED);
}

static void iterate_cbs(OrtpEvDispatcher *disp, OrtpEvent *ev) {
	OrtpEventData *d = ortp_event_get_data(ev);
	do {
		/*for each packet part, if ANY iterate through the whole callback list to see if
		anyone is interested in it*/
		OrtpEventData *d = ortp_event_get_data(ev);
		OList* it;
		OrtpEventType evt = ortp_event_get_type(ev);
133
		for (it=disp->cbs;it!=NULL;it=it->next){
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
			OrtpEvDispatcherData *data = (OrtpEvDispatcherData *)it->data;
			if (evt == data->type) {
				if (!is_rtcp_event(data->type) || rtcp_is_type(d->packet, data->subtype)) {
					data->on_found(d, data->user_data);
				}
			}
		}
	} while (d->packet!=NULL && rtcp_next_packet(d->packet));
}

void ortp_ev_dispatcher_iterate(OrtpEvDispatcher *d) {
	OrtpEvent *ev = NULL;
	while ((ev = ortp_ev_queue_get(d->q)) != NULL) {
		iterate_cbs(d, ev);
		ortp_event_destroy(ev);
	}
}

void ortp_ev_dispatcher_connect(OrtpEvDispatcher *d
								, OrtpEventType type
								, rtcp_type_t subtype
								, OrtpEvDispatcherCb cb
								, void *user_data) {
	OrtpEvDispatcherData *data=ortp_new(OrtpEvDispatcherData,1);
	data->type = type;
	data->subtype = subtype;
	data->on_found = cb;
	data->user_data = user_data;
	d->cbs = o_list_append(d->cbs, data);
}

void ortp_ev_dispatcher_disconnect(OrtpEvDispatcher *d
								, OrtpEventType type
								, rtcp_type_t subtype
								, OrtpEvDispatcherCb cb) {
Sylvain Berfini's avatar
Sylvain Berfini committed
169
	OList *it = NULL;
170 171 172
	if(!d) {
		return;
	}
Sylvain Berfini's avatar
Sylvain Berfini committed
173
	it = d->cbs;
174 175 176 177 178 179 180 181 182 183 184 185
	while (it) {
		OrtpEvDispatcherData *data = (OrtpEvDispatcherData*)it->data;
		if (data && data->type == type && data->subtype == subtype && data->on_found == cb) {
			OList *tofree = it;
			it = it->next;
			ortp_free(data);
			d->cbs = o_list_remove_link(d->cbs, tofree);
		} else {
			it = it->next;
		}
	}
}