]> sjero.net Git - linphone/blob - linphone/oRTP/src/event.c
00718e2e756917362434bf5a7da0883e65ede74e
[linphone] / linphone / oRTP / src / event.c
1  /*
2   The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) stack.
3   Copyright (C) 2001  Simon MORLAT simon.morlat@linphone.org
4
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #include "ortp/event.h"
21 #include "ortp/ortp.h"
22
23 RtpEndpoint *rtp_endpoint_new(struct sockaddr *addr, socklen_t addrlen){
24         RtpEndpoint *ep=ortp_new(RtpEndpoint,1);
25         if (sizeof(ep->addr)<addrlen){
26                 ortp_free(ep);
27                 ortp_fatal("Bad socklen_t size !");
28                 return NULL;
29         }
30         memcpy(&ep->addr,addr,addrlen);
31         ep->addrlen=addrlen;
32         return ep;
33 }
34
35 void rtp_endpoint_destroy(RtpEndpoint *ep){
36         ortp_free(ep);
37 }
38
39 RtpEndpoint *rtp_endpoint_dup(const RtpEndpoint *ep){
40         return rtp_endpoint_new((struct sockaddr*)&ep->addr,ep->addrlen);
41 }
42
43 OrtpEvent * ortp_event_new(unsigned long type){
44         const int size=sizeof(OrtpEventType)+sizeof(OrtpEventData);
45         mblk_t *m=allocb(size,0);
46         memset(m->b_wptr,0,size);
47         *((OrtpEventType*)m->b_wptr)=type;
48         return m;
49 }
50
51 OrtpEvent *ortp_event_dup(OrtpEvent *ev){
52 #if 0
53         OrtpEvent *nev=dupb(ev);
54 #else
55         OrtpEvent *nev = ortp_event_new(ortp_event_get_type(ev));
56         OrtpEventData * ed = ortp_event_get_data(ev);
57         OrtpEventData * edv = ortp_event_get_data(nev);
58
59         if (ed->ep) edv->ep = rtp_endpoint_dup(ed->ep);
60         if (ed->packet) edv->packet = copymsg(ed->packet);
61         edv->info.telephone_event = ed->info.telephone_event;
62 #endif
63         return nev;
64 }
65
66 OrtpEventType ortp_event_get_type(const OrtpEvent *ev){
67         return ((OrtpEventType*)ev->b_rptr)[0];
68 }
69
70 OrtpEventData * ortp_event_get_data(OrtpEvent *ev){
71         return (OrtpEventData*)(ev->b_rptr+sizeof(OrtpEventType));
72 }
73
74 void ortp_event_destroy(OrtpEvent *ev){
75         OrtpEventData *d=ortp_event_get_data(ev);
76         if (ev->b_datap->db_ref==1){
77                 if (d->packet)  freemsg(d->packet);
78                 if (d->ep) rtp_endpoint_destroy(d->ep);
79         }
80         freemsg(ev);
81 }
82
83 OrtpEvQueue * ortp_ev_queue_new(){
84         OrtpEvQueue *q=ortp_new(OrtpEvQueue,1);
85         qinit(&q->q);
86         ortp_mutex_init(&q->mutex,NULL);
87         return q;
88 }
89
90 void ortp_ev_queue_flush(OrtpEvQueue * qp){
91         OrtpEvent *ev;
92         while((ev=ortp_ev_queue_get(qp))!=NULL){
93                 ortp_event_destroy(ev);
94         }
95 }
96
97 OrtpEvent * ortp_ev_queue_get(OrtpEvQueue *q){
98         OrtpEvent *ev;
99         ortp_mutex_lock(&q->mutex);
100         ev=getq(&q->q);
101         ortp_mutex_unlock(&q->mutex);
102         return ev;
103 }
104
105 void ortp_ev_queue_destroy(OrtpEvQueue * qp){
106         ortp_ev_queue_flush(qp);
107         ortp_mutex_destroy(&qp->mutex);
108         ortp_free(qp);
109 }
110
111 void ortp_ev_queue_put(OrtpEvQueue *q, OrtpEvent *ev){
112         ortp_mutex_lock(&q->mutex);
113         putq(&q->q,ev);
114         ortp_mutex_unlock(&q->mutex);
115 }
116