event.h 6.21 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 22

#ifndef ortp_events_h
#define ortp_events_h

23 24 25
#include "ortp/str_utils.h"
#include "ortp/rtcp.h"
#include "bctoolbox/list.h"
aymeric's avatar
aymeric committed
26 27 28 29 30

typedef mblk_t OrtpEvent;

typedef unsigned long OrtpEventType;

31 32 33 34 35
typedef enum {
	OrtpRTPSocket,
	OrtpRTCPSocket
} OrtpSocketType;

aymeric's avatar
aymeric committed
36 37
struct _OrtpEventData{
	mblk_t *packet;	/* most events are associated to a received packet */
38 39
	struct sockaddr_storage source_addr;
	socklen_t source_addrlen;
40
	ortpTimeSpec ts;
aymeric's avatar
aymeric committed
41 42 43
	union {
		int telephone_event;
		int payload_type;
44
		bool_t dtls_stream_encrypted;
45
		bool_t zrtp_stream_encrypted;
Simon Morlat's avatar
Simon Morlat committed
46
		bool_t ice_processing_successful;
47
		struct _ZrtpInfo{
48
			char sas[32]; // up to 31 + null characters
49
			bool_t verified;
50 51 52
			bool_t cache_mismatch;
			bool_t pad[2];
		} zrtp_info;
53
		OrtpSocketType socket_type;
54
		uint32_t received_rtt_character;
55
		bool_t congestion_detected;
56
		float video_bandwidth_available;
aymeric's avatar
aymeric committed
57 58 59 60 61 62 63 64 65 66
	} info;
};

typedef struct _OrtpEventData OrtpEventData;


#ifdef __cplusplus
extern "C"{
#endif

67 68
ORTP_PUBLIC OrtpEvent * ortp_event_new(OrtpEventType tp);
ORTP_PUBLIC OrtpEventType ortp_event_get_type(const OrtpEvent *ev);
aymeric's avatar
aymeric committed
69 70 71 72
/* type is one of the following*/
#define ORTP_EVENT_STUN_PACKET_RECEIVED		1
#define ORTP_EVENT_PAYLOAD_TYPE_CHANGED 	2
#define ORTP_EVENT_TELEPHONE_EVENT		3
Simon Morlat's avatar
Simon Morlat committed
73 74
#define ORTP_EVENT_RTCP_PACKET_RECEIVED		4 /**<when a RTCP packet is received from far end */
#define ORTP_EVENT_RTCP_PACKET_EMITTED		5 /**<fired when oRTP decides to send an automatic RTCP SR or RR */
75 76
#define ORTP_EVENT_ZRTP_ENCRYPTION_CHANGED	6
#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 84
#define ORTP_EVENT_RTT_CHARACTER_RECEIVED		15
#define ORTP_EVENT_CONGESTION_STATE_CHANGED		16
johan's avatar
johan committed
85 86
#define ORTP_EVENT_ZRTP_CACHE_MISMATCH			17
#define ORTP_EVENT_ZRTP_PEER_VERSION_OBSOLETE		18
87
#define ORTP_EVENT_NEW_VIDEO_BANDWIDTH_ESTIMATION_AVAILABLE             19
88
#define ORTP_EVENT_ICE_DEACTIVATION_NEEDED		20
89

90 91 92
ORTP_PUBLIC OrtpEventData * ortp_event_get_data(OrtpEvent *ev);
ORTP_PUBLIC void ortp_event_destroy(OrtpEvent *ev);
ORTP_PUBLIC OrtpEvent *ortp_event_dup(OrtpEvent *ev);
aymeric's avatar
aymeric committed
93 94 95 96 97 98

typedef struct OrtpEvQueue{
	queue_t q;
	ortp_mutex_t mutex;
} OrtpEvQueue;

99 100 101 102
ORTP_PUBLIC OrtpEvQueue * ortp_ev_queue_new(void);
ORTP_PUBLIC void ortp_ev_queue_destroy(OrtpEvQueue *q);
ORTP_PUBLIC OrtpEvent * ortp_ev_queue_get(OrtpEvQueue *q);
ORTP_PUBLIC void ortp_ev_queue_flush(OrtpEvQueue * qp);
aymeric's avatar
aymeric committed
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
struct _RtpSession;

/**
 * Callback function when a RTCP packet of the interested type is found.
 *
 * @param evd the packet. Read-only, must NOT be changed.
 * @param user_data user data provided when registered the callback
 *
 */
typedef void (*OrtpEvDispatcherCb)(const OrtpEventData *evd, void *user_data);
typedef struct OrtpEvDispatcherData{
	OrtpEventType type;
	rtcp_type_t subtype;
	OrtpEvDispatcherCb on_found;
	void* user_data;
} OrtpEvDispatcherData;

typedef struct OrtpEvDispatcher{
	OrtpEvQueue *q;
	struct _RtpSession* session;
124
	bctbx_list_t *cbs;
125 126 127 128 129
} OrtpEvDispatcher;

/**
 * Constructs an OrtpEvDispatcher object. This object can be used to be notified
 * when any RTCP type packet is received or emitted on the rtp session,
130
 * given a callback registered with \a ortp_ev_dispatcher_connect
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
 *
 * @param session RTP session to listen on. Cannot be NULL.
 *
 * @return OrtpEvDispatcher object newly created.
 */
ORTP_PUBLIC OrtpEvDispatcher * ortp_ev_dispatcher_new(struct _RtpSession* session);
/**
 * Frees the memory for the given dispatcher. Note that user_data must be freed
 * by caller, and so does the OrtpEvQueue.
 *
 * @param d OrtpEvDispatcher object
 */
ORTP_PUBLIC void ortp_ev_dispatcher_destroy(OrtpEvDispatcher *d);
/**
 * Iterate method to be called periodically. If a RTCP packet is found and
146
 * its type matches one of the callback connected with \a ortp_ev_dispatcher_connect,
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
 * this callback will be invoked in the current thread.
 *
 * @param d OrtpEvDispatcher object
 */
ORTP_PUBLIC void ortp_ev_dispatcher_iterate(OrtpEvDispatcher *d);
/**
 * Connects a callback to the given type of event packet and, in case of RTCP event,
 * for the given RTCP subtype.
 * When any event is found, the callback is invoked. Multiple
 * callbacks can be connected to the same type of event, and the same callback
 * can be connected to multiple type of event.
 *
 * @param d OrtpEvDispatcher object
 * @param type type of event to be notified of.
 * @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.
 * @param on_receive function to call when a RTCP packet of the given type is found.
 * @param user_data user data given as last argument of the callback. Can be NULL. MUST be freed by user.
 */
ORTP_PUBLIC void ortp_ev_dispatcher_connect(OrtpEvDispatcher *d
											, OrtpEventType type
											, rtcp_type_t subtype
											, OrtpEvDispatcherCb on_receive
											, void *user_data);

/**
 * Disconnects the given callback for the given type and subtype on the given dispatcher.
*/
Ghislain MARY's avatar
Ghislain MARY committed
174
ORTP_PUBLIC void ortp_ev_dispatcher_disconnect(OrtpEvDispatcher *d
175 176 177 178
								, OrtpEventType type
								, rtcp_type_t subtype
								, OrtpEvDispatcherCb cb);

aymeric's avatar
aymeric committed
179 180 181 182 183 184
#ifdef __cplusplus
}
#endif

#endif