1 /*
2  * The oRTP library is an RTP (Realtime Transport Protocol - rfc3550) implementation with additional features.
3  * Copyright (C) 2017 Belledonne Communications SARL
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program 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
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19 
20 #ifndef ortp_events_h
21 #define ortp_events_h
22 
23 #include "ortp/str_utils.h"
24 #include "ortp/rtcp.h"
25 #include "bctoolbox/list.h"
26 
27 typedef mblk_t OrtpEvent;
28 
29 typedef unsigned long OrtpEventType;
30 
31 typedef enum {
32 	OrtpRTPSocket,
33 	OrtpRTCPSocket
34 } OrtpSocketType;
35 
36 struct _OrtpEventData{
37 	mblk_t *packet;	/* most events are associated to a received packet */
38 	struct sockaddr_storage source_addr;
39 	socklen_t source_addrlen;
40 	ortpTimeSpec ts;
41 	union {
42 		int telephone_event;
43 		int payload_type;
44 		bool_t dtls_stream_encrypted;
45 		bool_t zrtp_stream_encrypted;
46 		bool_t ice_processing_successful;
47 		struct _ZrtpInfo{
48 			char sas[32]; // up to 31 + null characters
49 			bool_t verified;
50 			bool_t cache_mismatch;
51 			bool_t pad[2];
52 		} zrtp_info;
53 		OrtpSocketType socket_type;
54 		uint32_t received_rtt_character;
55 		bool_t congestion_detected;
56 		float video_bandwidth_available;
57 	} info;
58 };
59 
60 typedef struct _OrtpEventData OrtpEventData;
61 
62 
63 #ifdef __cplusplus
64 extern "C"{
65 #endif
66 
67 ORTP_PUBLIC OrtpEvent * ortp_event_new(OrtpEventType tp);
68 ORTP_PUBLIC OrtpEventType ortp_event_get_type(const OrtpEvent *ev);
69 /* type is one of the following*/
70 #define ORTP_EVENT_STUN_PACKET_RECEIVED		1
71 #define ORTP_EVENT_PAYLOAD_TYPE_CHANGED 	2
72 #define ORTP_EVENT_TELEPHONE_EVENT		3
73 #define ORTP_EVENT_RTCP_PACKET_RECEIVED		4 /**<when a RTCP packet is received from far end */
74 #define ORTP_EVENT_RTCP_PACKET_EMITTED		5 /**<fired when oRTP decides to send an automatic RTCP SR or RR */
75 #define ORTP_EVENT_ZRTP_ENCRYPTION_CHANGED	6
76 #define ORTP_EVENT_ZRTP_SAS_READY		7
77 #define ORTP_EVENT_ICE_CHECK_LIST_PROCESSING_FINISHED	8
78 #define ORTP_EVENT_ICE_SESSION_PROCESSING_FINISHED	9
79 #define ORTP_EVENT_ICE_GATHERING_FINISHED		10
80 #define ORTP_EVENT_ICE_LOSING_PAIRS_COMPLETED		11
81 #define ORTP_EVENT_ICE_RESTART_NEEDED			12
82 #define ORTP_EVENT_DTLS_ENCRYPTION_CHANGED		13
83 #define ORTP_EVENT_RTT_CHARACTER_RECEIVED		15
84 #define ORTP_EVENT_CONGESTION_STATE_CHANGED		16
85 #define ORTP_EVENT_ZRTP_CACHE_MISMATCH			17
86 #define ORTP_EVENT_ZRTP_PEER_VERSION_OBSOLETE		18
87 #define ORTP_EVENT_NEW_VIDEO_BANDWIDTH_ESTIMATION_AVAILABLE             19
88 
89 ORTP_PUBLIC OrtpEventData * ortp_event_get_data(OrtpEvent *ev);
90 ORTP_PUBLIC void ortp_event_destroy(OrtpEvent *ev);
91 ORTP_PUBLIC OrtpEvent *ortp_event_dup(OrtpEvent *ev);
92 
93 typedef struct OrtpEvQueue{
94 	queue_t q;
95 	ortp_mutex_t mutex;
96 } OrtpEvQueue;
97 
98 ORTP_PUBLIC OrtpEvQueue * ortp_ev_queue_new(void);
99 ORTP_PUBLIC void ortp_ev_queue_destroy(OrtpEvQueue *q);
100 ORTP_PUBLIC OrtpEvent * ortp_ev_queue_get(OrtpEvQueue *q);
101 ORTP_PUBLIC void ortp_ev_queue_flush(OrtpEvQueue * qp);
102 
103 struct _RtpSession;
104 
105 /**
106  * Callback function when a RTCP packet of the interested type is found.
107  *
108  * @param evd the packet. Read-only, must NOT be changed.
109  * @param user_data user data provided when registered the callback
110  *
111  */
112 typedef void (*OrtpEvDispatcherCb)(const OrtpEventData *evd, void *user_data);
113 typedef struct OrtpEvDispatcherData{
114 	OrtpEventType type;
115 	rtcp_type_t subtype;
116 	OrtpEvDispatcherCb on_found;
117 	void* user_data;
118 } OrtpEvDispatcherData;
119 
120 typedef struct OrtpEvDispatcher{
121 	OrtpEvQueue *q;
122 	struct _RtpSession* session;
123 	bctbx_list_t *cbs;
124 } OrtpEvDispatcher;
125 
126 /**
127  * Constructs an OrtpEvDispatcher object. This object can be used to be notified
128  * when any RTCP type packet is received or emitted on the rtp session,
129  * given a callback registered with \a ortp_ev_dispatcher_connect
130  *
131  * @param session RTP session to listen on. Cannot be NULL.
132  *
133  * @return OrtpEvDispatcher object newly created.
134  */
135 ORTP_PUBLIC OrtpEvDispatcher * ortp_ev_dispatcher_new(struct _RtpSession* session);
136 /**
137  * Frees the memory for the given dispatcher. Note that user_data must be freed
138  * by caller, and so does the OrtpEvQueue.
139  *
140  * @param d OrtpEvDispatcher object
141  */
142 ORTP_PUBLIC void ortp_ev_dispatcher_destroy(OrtpEvDispatcher *d);
143 /**
144  * Iterate method to be called periodically. If a RTCP packet is found and
145  * its type matches one of the callback connected with \a ortp_ev_dispatcher_connect,
146  * this callback will be invoked in the current thread.
147  *
148  * @param d OrtpEvDispatcher object
149  */
150 ORTP_PUBLIC void ortp_ev_dispatcher_iterate(OrtpEvDispatcher *d);
151 /**
152  * Connects a callback to the given type of event packet and, in case of RTCP event,
153  * for the given RTCP subtype.
154  * When any event is found, the callback is invoked. Multiple
155  * callbacks can be connected to the same type of event, and the same callback
156  * can be connected to multiple type of event.
157  *
158  * @param d OrtpEvDispatcher object
159  * @param type type of event to be notified of.
160  * @param subtype when type is set to ORTP_EVENT_RTCP_PACKET_RECEIVED or ORTP_EVENT_RTCP_PACKET_EMITTED, subtype of RTCP packet to be notified of. Otherwise this parameter is not used.
161  * @param on_receive function to call when a RTCP packet of the given type is found.
162  * @param user_data user data given as last argument of the callback. Can be NULL. MUST be freed by user.
163  */
164 ORTP_PUBLIC void ortp_ev_dispatcher_connect(OrtpEvDispatcher *d
165 											, OrtpEventType type
166 											, rtcp_type_t subtype
167 											, OrtpEvDispatcherCb on_receive
168 											, void *user_data);
169 
170 /**
171  * Disconnects the given callback for the given type and subtype on the given dispatcher.
172 */
173 ORTP_PUBLIC void ortp_ev_dispatcher_disconnect(OrtpEvDispatcher *d
174 								, OrtpEventType type
175 								, rtcp_type_t subtype
176 								, OrtpEvDispatcherCb cb);
177 
178 #ifdef __cplusplus
179 }
180 #endif
181 
182 #endif
183 
184