event.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Copyright (C) 2011-2011 Teluu Inc. (http://www.teluu.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #ifndef __PJMEDIA_EVENT_H__
  19. #define __PJMEDIA_EVENT_H__
  20. /**
  21. * @file pjmedia/event.h
  22. * @brief Event framework
  23. */
  24. #include <pjmedia/audiodev.h>
  25. #include <pjmedia/format.h>
  26. #include <pjmedia/rtcp_fb.h>
  27. #include <pjmedia/signatures.h>
  28. #include <pjmedia/videodev.h>
  29. PJ_BEGIN_DECL
  30. /**
  31. * @defgroup PJMEDIA_EVENT Event Framework
  32. * @brief PJMEDIA event framework
  33. * @{
  34. */
  35. /**
  36. * This enumeration describes list of media events.
  37. */
  38. typedef enum pjmedia_event_type
  39. {
  40. /**
  41. * No event.
  42. */
  43. PJMEDIA_EVENT_NONE,
  44. /**
  45. * Media format has changed event.
  46. */
  47. PJMEDIA_EVENT_FMT_CHANGED = PJMEDIA_FOURCC('F', 'M', 'C', 'H'),
  48. /**
  49. * Video window is being closed.
  50. */
  51. PJMEDIA_EVENT_WND_CLOSING = PJMEDIA_FOURCC('W', 'N', 'C', 'L'),
  52. /**
  53. * Video window has been closed event.
  54. */
  55. PJMEDIA_EVENT_WND_CLOSED = PJMEDIA_FOURCC('W', 'N', 'C', 'O'),
  56. /**
  57. * Video window has been resized event.
  58. */
  59. PJMEDIA_EVENT_WND_RESIZED = PJMEDIA_FOURCC('W', 'N', 'R', 'Z'),
  60. /**
  61. * Mouse button has been pressed event.
  62. */
  63. PJMEDIA_EVENT_MOUSE_BTN_DOWN = PJMEDIA_FOURCC('M', 'S', 'D', 'N'),
  64. /**
  65. * Video keyframe has just been decoded event.
  66. */
  67. PJMEDIA_EVENT_KEYFRAME_FOUND = PJMEDIA_FOURCC('I', 'F', 'R', 'F'),
  68. /**
  69. * Video decoding error due to missing keyframe event.
  70. */
  71. PJMEDIA_EVENT_KEYFRAME_MISSING = PJMEDIA_FOURCC('I', 'F', 'R', 'M'),
  72. /**
  73. * Video orientation has been changed event.
  74. */
  75. PJMEDIA_EVENT_ORIENT_CHANGED = PJMEDIA_FOURCC('O', 'R', 'N', 'T'),
  76. /**
  77. * RTCP-FB has been received.
  78. */
  79. PJMEDIA_EVENT_RX_RTCP_FB = PJMEDIA_FOURCC('R', 'T', 'F', 'B'),
  80. /**
  81. * Audio device stopped on error.
  82. */
  83. PJMEDIA_EVENT_AUD_DEV_ERROR = PJMEDIA_FOURCC('A', 'E', 'R', 'R'),
  84. /**
  85. * Video device stopped on error.
  86. */
  87. PJMEDIA_EVENT_VID_DEV_ERROR = PJMEDIA_FOURCC('V', 'E', 'R', 'R'),
  88. /**
  89. * Transport media error.
  90. */
  91. PJMEDIA_EVENT_MEDIA_TP_ERR = PJMEDIA_FOURCC('T', 'E', 'R', 'R'),
  92. /**
  93. * Callback event. Currently for internal use only.
  94. */
  95. PJMEDIA_EVENT_CALLBACK = PJMEDIA_FOURCC('C', 'B', ' ', ' ')
  96. } pjmedia_event_type;
  97. /**
  98. * Additional data/parameters for media format changed event
  99. * (PJMEDIA_EVENT_FMT_CHANGED).
  100. */
  101. typedef struct pjmedia_event_fmt_changed_data
  102. {
  103. /** The media flow direction */
  104. pjmedia_dir dir;
  105. /** The new media format. */
  106. pjmedia_format new_fmt;
  107. } pjmedia_event_fmt_changed_data;
  108. /**
  109. * Additional data/parameters are not needed.
  110. */
  111. typedef struct pjmedia_event_dummy_data
  112. {
  113. /** Dummy data */
  114. int dummy;
  115. } pjmedia_event_dummy_data;
  116. /**
  117. * Additional data/parameters for window resized event
  118. * (PJMEDIA_EVENT_WND_RESIZED).
  119. */
  120. typedef struct pjmedia_event_wnd_resized_data
  121. {
  122. /**
  123. * The new window size.
  124. */
  125. pjmedia_rect_size new_size;
  126. } pjmedia_event_wnd_resized_data;
  127. /**
  128. * Additional data/parameters for window closing event.
  129. */
  130. typedef struct pjmedia_event_wnd_closing_data
  131. {
  132. /** Consumer may set this field to PJ_TRUE to cancel the closing */
  133. pj_bool_t cancel;
  134. } pjmedia_event_wnd_closing_data;
  135. /**
  136. * Additional data/parameters for audio device error event.
  137. */
  138. typedef struct pjmedia_event_aud_dev_err_data
  139. {
  140. /** The media direction that fails */
  141. pjmedia_dir dir;
  142. /** The audio device ID */
  143. pjmedia_aud_dev_index id;
  144. /** The error code */
  145. pj_status_t status;
  146. } pjmedia_event_aud_dev_err_data;
  147. /**
  148. * Additional data/parameters for video device error event.
  149. */
  150. typedef struct pjmedia_event_vid_dev_err_data
  151. {
  152. /** The media direction that fails */
  153. pjmedia_dir dir;
  154. /** The video device ID */
  155. pjmedia_vid_dev_index id;
  156. /** The error code */
  157. pj_status_t status;
  158. } pjmedia_event_vid_dev_err_data;
  159. /**
  160. * Additional data/parameters for media transmit error event.
  161. */
  162. typedef struct pjmedia_event_media_tp_err_data
  163. {
  164. /** The media type */
  165. pjmedia_type type;
  166. /** RTP/RTCP? */
  167. pj_bool_t is_rtp;
  168. /** Media direction */
  169. pjmedia_dir dir;
  170. /** The error code */
  171. pj_status_t status;
  172. } pjmedia_event_media_tp_err_data;
  173. /** Additional parameters for window changed event. */
  174. typedef pjmedia_event_dummy_data pjmedia_event_wnd_closed_data;
  175. /** Additional parameters for mouse button down event */
  176. typedef pjmedia_event_dummy_data pjmedia_event_mouse_btn_down_data;
  177. /** Additional parameters for keyframe found event */
  178. typedef pjmedia_event_dummy_data pjmedia_event_keyframe_found_data;
  179. /** Additional parameters for keyframe missing event */
  180. typedef pjmedia_event_dummy_data pjmedia_event_keyframe_missing_data;
  181. /**
  182. * Maximum size of additional parameters section in pjmedia_event structure
  183. */
  184. #define PJMEDIA_EVENT_DATA_MAX_SIZE sizeof(pjmedia_event_fmt_changed_data)
  185. /** Type of storage to hold user data in pjmedia_event structure */
  186. typedef char pjmedia_event_user_data[PJMEDIA_EVENT_DATA_MAX_SIZE];
  187. /**
  188. * This structure describes a media event. It consists mainly of the event
  189. * type and additional data/parameters for the event. Applications can
  190. * use #pjmedia_event_init() to initialize this event structure with
  191. * basic information about the event.
  192. */
  193. typedef struct pjmedia_event
  194. {
  195. /**
  196. * The event type.
  197. */
  198. pjmedia_event_type type;
  199. /**
  200. * The media timestamp when the event occurs.
  201. */
  202. pj_timestamp timestamp;
  203. /**
  204. * Pointer information about the source of this event. This field
  205. * is provided mainly for comparison purpose so that event subscribers
  206. * can check which source the event originated from. Usage of this
  207. * pointer for other purpose may require special care such as mutex
  208. * locking or checking whether the object is already destroyed.
  209. */
  210. const void *src;
  211. /**
  212. * Pointer information about the publisher of this event. This field
  213. * is provided mainly for comparison purpose so that event subscribers
  214. * can check which object published the event. Usage of this
  215. * pointer for other purpose may require special care such as mutex
  216. * locking or checking whether the object is already destroyed.
  217. */
  218. const void *epub;
  219. /**
  220. * Additional data/parameters about the event. The type of data
  221. * will be specific to the event type being reported.
  222. */
  223. union {
  224. /** Media format changed event data. */
  225. pjmedia_event_fmt_changed_data fmt_changed;
  226. /** Window resized event data */
  227. pjmedia_event_wnd_resized_data wnd_resized;
  228. /** Window closing event data. */
  229. pjmedia_event_wnd_closing_data wnd_closing;
  230. /** Window closed event data */
  231. pjmedia_event_wnd_closed_data wnd_closed;
  232. /** Mouse button down event data */
  233. pjmedia_event_mouse_btn_down_data mouse_btn_down;
  234. /** Keyframe found event data */
  235. pjmedia_event_keyframe_found_data keyframe_found;
  236. /** Keyframe missing event data */
  237. pjmedia_event_keyframe_missing_data keyframe_missing;
  238. /** Audio device error event data */
  239. pjmedia_event_aud_dev_err_data aud_dev_err;
  240. /** Video device error event data */
  241. pjmedia_event_vid_dev_err_data vid_dev_err;
  242. /** Storage for user event data */
  243. pjmedia_event_user_data user;
  244. /** Media transport error event data */
  245. pjmedia_event_media_tp_err_data med_tp_err;
  246. /** Receiving RTCP-FB event data */
  247. pjmedia_event_rx_rtcp_fb_data rx_rtcp_fb;
  248. /** Pointer to storage to user event data, if it's outside
  249. * this struct
  250. */
  251. void *ptr;
  252. } data;
  253. } pjmedia_event;
  254. /**
  255. * The callback to receive media events.
  256. *
  257. * @param event The media event.
  258. * @param user_data The user data associated with the callback.
  259. *
  260. * @return If the callback returns non-PJ_SUCCESS, this return
  261. * code may be propagated back to the caller.
  262. */
  263. typedef pj_status_t pjmedia_event_cb(pjmedia_event *event,
  264. void *user_data);
  265. /**
  266. * This enumeration describes flags for event publication via
  267. * #pjmedia_event_publish().
  268. */
  269. typedef enum pjmedia_event_publish_flag
  270. {
  271. /**
  272. * Default flag.
  273. */
  274. PJMEDIA_EVENT_PUBLISH_DEFAULT,
  275. /**
  276. * Publisher will only post the event to the event manager. It is the
  277. * event manager that will later notify all the publisher's subscribers.
  278. */
  279. PJMEDIA_EVENT_PUBLISH_POST_EVENT = 1
  280. } pjmedia_event_publish_flag;
  281. /**
  282. * Event manager flag.
  283. */
  284. typedef enum pjmedia_event_mgr_flag
  285. {
  286. /**
  287. * Tell the event manager not to create any event worker thread.
  288. * Do not set this flag if app plans to publish an event using
  289. * PJMEDIA_EVENT_PUBLISH_POST_EVENT.
  290. */
  291. PJMEDIA_EVENT_MGR_NO_THREAD = 1
  292. } pjmedia_event_mgr_flag;
  293. /**
  294. * Opaque data type for event manager. Typically, the event manager
  295. * is a singleton instance, although application may instantiate more than one
  296. * instances of this if required.
  297. */
  298. typedef struct pjmedia_event_mgr pjmedia_event_mgr;
  299. /**
  300. * Create a new event manager instance. This will also set the pointer
  301. * to the singleton instance if the value is still NULL.
  302. *
  303. * @param pool Pool to allocate memory from.
  304. * @param options Options. Bitmask flags from #pjmedia_event_mgr_flag
  305. * @param mgr Pointer to hold the created instance of the
  306. * event manager.
  307. *
  308. * @return PJ_SUCCESS on success or the appropriate error code.
  309. */
  310. PJ_DECL(pj_status_t) pjmedia_event_mgr_create(pj_pool_t *pool,
  311. unsigned options,
  312. pjmedia_event_mgr **mgr);
  313. /**
  314. * Get the singleton instance of the event manager.
  315. *
  316. * @return The instance.
  317. */
  318. PJ_DECL(pjmedia_event_mgr*) pjmedia_event_mgr_instance(void);
  319. /**
  320. * Manually assign a specific event manager instance as the singleton
  321. * instance. Normally this is not needed if only one instance is ever
  322. * going to be created, as the library automatically assign the singleton
  323. * instance.
  324. *
  325. * @param mgr The instance to be used as the singleton instance.
  326. * Application may specify NULL to clear the singleton
  327. * singleton instance.
  328. */
  329. PJ_DECL(void) pjmedia_event_mgr_set_instance(pjmedia_event_mgr *mgr);
  330. /**
  331. * Destroy an event manager. If the manager happens to be the singleton
  332. * instance, the singleton instance will be set to NULL.
  333. *
  334. * @param mgr The eventmanager. Specify NULL to use
  335. * the singleton instance.
  336. */
  337. PJ_DECL(void) pjmedia_event_mgr_destroy(pjmedia_event_mgr *mgr);
  338. /**
  339. * Initialize event structure with basic data about the event.
  340. *
  341. * @param event The event to be initialized.
  342. * @param type The event type to be set for this event.
  343. * @param ts Event timestamp. May be set to NULL to set the event
  344. * timestamp to zero.
  345. * @param src Event source.
  346. */
  347. PJ_DECL(void) pjmedia_event_init(pjmedia_event *event,
  348. pjmedia_event_type type,
  349. const pj_timestamp *ts,
  350. const void *src);
  351. /**
  352. * Subscribe a callback function to events published by the specified
  353. * publisher. Note that the subscriber may receive not only events emitted by
  354. * the specific publisher specified in the argument, but also from other
  355. * publishers contained by the publisher, if the publisher is republishing
  356. * events from other publishers.
  357. *
  358. * @param mgr The event manager.
  359. * @param cb The callback function to receive the event.
  360. * @param user_data The user data to be associated with the callback
  361. * function.
  362. * @param epub The event publisher.
  363. *
  364. * @return PJ_SUCCESS on success or the appropriate error code.
  365. */
  366. PJ_DECL(pj_status_t) pjmedia_event_subscribe(pjmedia_event_mgr *mgr,
  367. pjmedia_event_cb *cb,
  368. void *user_data,
  369. void *epub);
  370. /**
  371. * Unsubscribe the callback associated with the user data from a publisher.
  372. * If the user data is not specified, this function will do the
  373. * unsubscription for all user data. If the publisher, epub, is not
  374. * specified, this function will do the unsubscription from all publishers.
  375. *
  376. * @param mgr The event manager.
  377. * @param cb The callback function.
  378. * @param user_data The user data associated with the callback
  379. * function, can be NULL.
  380. * @param epub The event publisher, can be NULL.
  381. *
  382. * @return PJ_SUCCESS on success or the appropriate error code.
  383. */
  384. PJ_DECL(pj_status_t)
  385. pjmedia_event_unsubscribe(pjmedia_event_mgr *mgr,
  386. pjmedia_event_cb *cb,
  387. void *user_data,
  388. void *epub);
  389. /**
  390. * Publish the specified event to all subscribers of the specified event
  391. * publisher. By default, the function will call all the subcribers'
  392. * callbacks immediately. If the publisher uses the flag
  393. * PJMEDIA_EVENT_PUBLISH_POST_EVENT, publisher will only post the event
  394. * to the event manager and return immediately. It is the event manager
  395. * that will later notify all the publisher's subscribers.
  396. *
  397. * @param mgr The event manager.
  398. * @param epub The event publisher.
  399. * @param event The event to be published.
  400. * @param flag Publication flag.
  401. *
  402. * @return PJ_SUCCESS only if all subscription callbacks returned
  403. * PJ_SUCCESS.
  404. */
  405. PJ_DECL(pj_status_t) pjmedia_event_publish(pjmedia_event_mgr *mgr,
  406. void *epub,
  407. pjmedia_event *event,
  408. pjmedia_event_publish_flag flag);
  409. /**
  410. * @} PJMEDIA_EVENT
  411. */
  412. PJ_END_DECL
  413. #endif /* __PJMEDIA_EVENT_H__ */