summaryrefslogtreecommitdiff
path: root/pjmedia/include/pjmedia/event.h
blob: ddcffc029a09c72d340b79b393b83bdccb381c5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
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
133
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* $Id$ */
/* 
 * Copyright (C) 2011-2011 Teluu Inc. (http://www.teluu.com)
 *
 * 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 
 */
#ifndef __PJMEDIA_EVENT_H__
#define __PJMEDIA_EVENT_H__

/**
 * @file pjmedia/event.h
 * @brief Event framework
 */
#include <pjmedia/format.h>
#include <pjmedia/signatures.h>
#include <pj/list.h>

PJ_BEGIN_DECL

/**
 * @defgroup PJMEDIA_EVENT Event Framework
 * @brief PJMEDIA event framework
 * @{
 */

/**
 * This enumeration describes list of media events.
 */
typedef enum pjmedia_event_type
{
    /**
     * No event.
     */
    PJMEDIA_EVENT_NONE,

    /**
     * Media format has changed event.
     */
    PJMEDIA_EVENT_FMT_CHANGED	= PJMEDIA_FOURCC('F', 'M', 'C', 'H'),

    /**
     * Video window is being closed.
     */
    PJMEDIA_EVENT_WND_CLOSING	= PJMEDIA_FOURCC('W', 'N', 'C', 'L'),

    /**
     * Video window has been closed event.
     */
    PJMEDIA_EVENT_WND_CLOSED	= PJMEDIA_FOURCC('W', 'N', 'C', 'O'),

    /**
     * Video window has been resized event.
     */
    PJMEDIA_EVENT_WND_RESIZED	= PJMEDIA_FOURCC('W', 'N', 'R', 'Z'),

    /**
     * Mouse button has been pressed event.
     */
    PJMEDIA_EVENT_MOUSE_BTN_DOWN = PJMEDIA_FOURCC('M', 'S', 'D', 'N'),

    /**
     * Video key frame has just been decoded event.
     */
    PJMEDIA_EVENT_KEY_FRAME_FOUND = PJMEDIA_FOURCC('I', 'F', 'R', 'F'),

    /**
     * Video decoding error due to missing key frame event.
     */
    PJMEDIA_EVENT_KEY_FRAME_MISSING = PJMEDIA_FOURCC('I', 'F', 'R', 'M')

} pjmedia_event_type;

/**
 * Forward declaration for event subscription.
 */
typedef struct pjmedia_event_subscription pjmedia_event_subscription;

/**
 * Forward declaration for event publisher.
 */
typedef struct pjmedia_event_publisher pjmedia_event_publisher;

/**
 * Additional data/parameters for media format changed event
 * (PJMEDIA_EVENT_FMT_CHANGED).
 */
typedef struct pjmedia_event_fmt_changed_data
{
    /** The media flow direction */
    pjmedia_dir		dir;

    /** The new media format. */
    pjmedia_format	new_fmt;
} pjmedia_event_fmt_changed_data;

/**
 * Additional data/parameters are not needed.
 */
typedef struct pjmedia_event_dummy_data
{
    /** Dummy data */
    int			dummy;
} pjmedia_event_dummy_data;

/**
 * Additional data/parameters for window resized event
 * (PJMEDIA_EVENT_WND_RESIZED).
 */
typedef struct pjmedia_event_wnd_resized_data
{
    /**
     * The new window size.
     */
    pjmedia_rect_size	new_size;
} pjmedia_event_wnd_resized_data;

/**
 * Additional data/parameters for window closing event.
 */
typedef struct pjmedia_event_wnd_closing_data
{
    /** Consumer may set this field to PJ_TRUE to cancel the closing */
    pj_bool_t		cancel;
} pjmedia_event_wnd_closing_data;

/** Additional parameters for window changed event. */
typedef pjmedia_event_dummy_data pjmedia_event_wnd_closed_data;

/** Additional parameters for mouse button down event */
typedef pjmedia_event_dummy_data pjmedia_event_mouse_btn_down_data;

/** Additional parameters for key frame found event */
typedef pjmedia_event_dummy_data pjmedia_event_key_frame_found_data;

/** Additional parameters for key frame missing event */
typedef pjmedia_event_dummy_data pjmedia_event_key_frame_missing_data;

/**
 * Maximum size of additional parameters section in pjmedia_event structure
 */
#define PJMEDIA_EVENT_DATA_MAX_SIZE	sizeof(pjmedia_event_fmt_changed_data)

/** Type of storage to hold user data in pjmedia_event structure */
typedef char pjmedia_event_user_data[PJMEDIA_EVENT_DATA_MAX_SIZE];

/**
 * This structure describes a media event. It consists mainly of the event
 * type and additional data/parameters for the event. Event publishers need
 * to use #pjmedia_event_init() to initialize this event structure with
 * basic information about the event.
 */
typedef struct pjmedia_event
{
    /**
     * The event type.
     */
    pjmedia_event_type			 type;

    /**
     * The media timestamp when the event occurs.
     */
    pj_timestamp		 	 timestamp;

    /**
     * This keeps count on the number of subscribers that have
     * processed this event.
     */
    unsigned				 proc_cnt;

    /**
     * The object signature of the event publisher. Application may use
     * this to check which publisher published the event.
     */
    pjmedia_obj_sig			 epub_sig;

    /**
     * Pointer information about the source of this event. This field
     * is provided mainly so that the event subscribers can compare it
     * against the publisher that it subscribed the events from initially,
     * a publisher can republish events from other publisher. Event
     * subscription must be careful when using this pointer other than for
     * comparison purpose, since access to the publisher may require special
     * care (e.g. mutex locking).
     */
    const pjmedia_event_publisher	*epub;

    /**
     * Additional data/parameters about the event. The type of data
     * will be specific to the event type being reported.
     */
    union {
	/** Media format changed event data. */
	pjmedia_event_fmt_changed_data		fmt_changed;

	/** Window resized event data */
	pjmedia_event_wnd_resized_data		wnd_resized;

	/** Window closing event data. */
	pjmedia_event_wnd_closing_data		wnd_closing;

	/** Window closed event data */
	pjmedia_event_wnd_closed_data		wnd_closed;

	/** Mouse button down event data */
	pjmedia_event_mouse_btn_down_data	mouse_btn_down;

	/** Key frame found event data */
	pjmedia_event_key_frame_found_data	key_frm_found;

	/** Key frame missing event data */
	pjmedia_event_key_frame_missing_data	key_frm_missing;

	/** Storage for user event data */
	pjmedia_event_user_data			user;

	/** Pointer to storage to user event data, if it's outside
	 * this struct
	 */
	void					*ptr;
    } data;
} pjmedia_event;

/**
 * The callback to receive media events. The callback should increase
 * \a proc_cnt field of the event if it processes the event.
 *
 * @param esub		The subscription that was made initially to receive
 * 			this event.
 * @param event		The media event itself.
 *
 * @return		If the callback returns non-PJ_SUCCESS, this return
 * 			code may be propagated back to the producer.
 */
typedef pj_status_t pjmedia_event_cb(pjmedia_event_subscription *esub,
				     pjmedia_event *event);

/**
 * This structure keeps the data needed to maintain an event subscription.
 * This data is normally kept by event publishers.
 */
struct pjmedia_event_subscription
{
    /** Standard list members */
    PJ_DECL_LIST_MEMBER(pjmedia_event_subscription);

    /** Callback that will be called by publisher to report events. */
    pjmedia_event_cb	*cb;

    /** User data for this subscription */
    void		*user_data;

    /** Current publisher it is subscribed to */
    pjmedia_event_publisher *subscribe_to;
};

/**
 * This describes an event publisher. An event publisher is an object that
 * maintains event subscriptions. When an event is published on behalf of
 * a publisher with #pjmedia_event_publish(), that event will be propagated
 * to all of the subscribers registered to the publisher.
 */
struct pjmedia_event_publisher
{
    /** The object signature of the publisher */
    pjmedia_obj_sig		sig;

    /** List of subscriptions for this event publisher */
    pjmedia_event_subscription	subscription_list;
};

/**
 * Initialize event structure with basic data about the event.
 *
 * @param event		The event to be initialized.
 * @param type		The event type to be set for this event.
 * @param ts		Event timestamp. May be set to NULL to set the event
 * 			timestamp to zero.
 * @param epub		Event publisher.
 */
PJ_DECL(void) pjmedia_event_init(pjmedia_event *event,
                                 pjmedia_event_type type,
                                 const pj_timestamp *ts,
                                 const pjmedia_event_publisher *epub);

/**
 * Initialize an event publisher structure.
 *
 * @param epub		The event publisher.
 * @param sig		The object signature of the publisher.
 */
PJ_DECL(void) pjmedia_event_publisher_init(pjmedia_event_publisher *epub,
                                           pjmedia_obj_sig sig);

/**
 * Initialize subscription data.
 *
 * @param esub		The event subscription.
 * @param cb		The callback to receive events.
 * @param user_data	Arbitrary user data to be associated with the
 * 			subscription.
 */
PJ_DECL(void) pjmedia_event_subscription_init(pjmedia_event_subscription *esub,
                                              pjmedia_event_cb *cb,
                                              void *user_data);

/**
 * Subscribe to events published by the specified publisher using the
 * specified subscription object. The callback and user data fields of
 * the subscription object must have been initialized prior to calling
 * this function, and the subscription object must be kept alive throughout
 * the duration of the subscription (e.g. it must not be allocated from
 * the stack).
 *
 * Note that the subscriber may receive not only events emitted by
 * the specific publisher specified in the argument, but also from other
 * publishers contained by the publisher, if the publisher is republishing
 * events from other publishers.
 *
 * @param epub		The event publisher.
 * @param esub 		The event subscription object.
 *
 * @return		PJ_SUCCESS on success or the appropriate error code.
 */
PJ_DECL(pj_status_t) pjmedia_event_subscribe(pjmedia_event_publisher *epub,
                                             pjmedia_event_subscription *esub);

/**
 * Unsubscribe the specified subscription object from publisher it is
 * currently subscribed to. If the subscription object is not currently
 * subscribed to anything, the function will do nothing.
 *
 * @param esub		The event subscription object, which must be the same
 * 			object that was given to #pjmedia_event_subscribe().
 *
 * @return		PJ_SUCCESS on success or the appropriate error code.
 */
PJ_DECL(pj_status_t)
pjmedia_event_unsubscribe(pjmedia_event_subscription *esub);

/**
 * Check if the specified publisher has subscribers.
 *
 * @param epub		The event publisher.
 *
 * @return		PJ_TRUE if the publisher has at least one subscriber.
 */
PJ_DECL(pj_bool_t)
pjmedia_event_publisher_has_sub(pjmedia_event_publisher *epub);

/**
 * Publish the specified event to all subscribers of the specified event
 * publisher.
 *
 * @param epub		The event publisher.
 * @param event		The event to be published.
 *
 * @return		PJ_SUCCESS only if all subscription callbacks returned
 * 			PJ_SUCCESS.
 */
PJ_DECL(pj_status_t) pjmedia_event_publish(pjmedia_event_publisher *epub,
                                           pjmedia_event *event);

/**
 * Subscribe to events produced by the source publisher in \a esrc and
 * republish the events to all subscribers in \a epub publisher.
 *
 * @param esrc		The event source from which events will be
 * 			republished.
 * @param epub		Events from the event source above will be
 * 			republished to subscribers of this publisher.
 * @param esub		The subscription object to be used to subscribe
 * 			to \a esrc. This doesn't need to be initialized,
 * 			but it must be kept alive throughout the lifetime
 * 			of the subsciption.
 *
 * @return		PJ_SUCCESS only if all subscription callbacks returned
 * 			PJ_SUCCESS.
 */
PJ_DECL(pj_status_t) pjmedia_event_republish(pjmedia_event_publisher *esrc,
                                             pjmedia_event_publisher *epub,
                                             pjmedia_event_subscription *esub);

/**
 * @}  PJMEDIA_EVENT
 */


PJ_END_DECL

#endif	/* __PJMEDIA_EVENT_H__ */