framehook.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * David Vossel <dvossel@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief FrameHook Architecture
  20. */
  21. /*!
  22. \page AstFrameHookAPI Asterisk FrameHook API
  23. \section FrameHookFunctionality How FrameHooks Work
  24. FrameHooks work by intercepting all frames being written and read off
  25. a channel and allowing those frames to be viewed and manipulated within a
  26. call back function. Frame interception occurs before any processing is
  27. done on the frame, which means this hook can be used to transparently
  28. manipulate a frame before it is read from the channel or written
  29. to the tech_pvt. This API can be thought of as a layer between the
  30. channel API and the Asterisk core when going in the READ direction, and
  31. as a layer between the Channel API and the tech_pvt when going in the
  32. WRITE direction.
  33. \section FrameHookAPIUsage How to Use an FrameHook
  34. Attaching and detaching an FrameHook to a channel is very simple. There are only
  35. two functions involved, ast_framehook_attach() which will return an id representing
  36. the new FrameHook on the channel, and ast_framehook_detach() which signals the
  37. FrameHook for detachment and destruction. Below is detailed information each of these
  38. functions and their usage.
  39. \code
  40. struct ast_framehook_interface interface = {
  41. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  42. .event_cb = hook_event_cb,
  43. .destroy_cb = hook_destroy_cb,
  44. .data = data, // where the data ptr points to any custom data used later by the hook cb.
  45. };
  46. int id = ast_framehook_attach(channel, &interface);
  47. \endcode
  48. The ast_framehook_attach() function creates and attaches a new FrameHook onto
  49. a channel. Once attached to the channel, the FrameHook will call the event_callback
  50. function each time a frame is written or read on the channel. A custom data
  51. pointer can be provided to this function to store on the FrameHook as well. This
  52. pointer can be used to keep up with any statefull information associated with the FrameHook
  53. and is provided during the event_callback function. The destroy_callback function is optional.
  54. This function exists so any custom data stored on the FrameHook can be destroyed before
  55. the Framehook if destroyed.
  56. \code
  57. ast_framehook_detach(channel, id);
  58. \endcode
  59. The ast_framehook_detach() function signals the FrameHook represented by an id to
  60. be detached and destroyed on a channel. Since it is possible this function may be called
  61. during the FrameHook's event callback, it is impossible to synchronously detach the
  62. FrameHook from the channel during this function call. It is guaranteed that the next
  63. event proceeding the ast_framehook_detach() will be of type AST_FRAMEHOOK_EVENT_DETACH,
  64. and that after that event occurs no other event will ever be issued for that FrameHook.
  65. Once the FrameHook is destroyed, the destroy callback function will be called if it was
  66. provided. Note that if this function is never called, the FrameHook will be detached
  67. on channel destruction.
  68. \section FrameHookAPICodeExample FrameHook Example Code
  69. The example code below attaches an FrameHook on a channel, and then detachs it when
  70. the first ast_frame is read or written to the event callback function. The Framehook's id
  71. is stored on the FrameHook's data pointer so it can be detached within the callback.
  72. \code
  73. static void destroy_cb(void *data) {
  74. ast_free(data);
  75. }
  76. static struct ast_frame *event_cb(struct ast_channel *chan,
  77. struct ast_frame *frame,
  78. enum ast_framehook_event event,
  79. void *data) {
  80. int *id = data;
  81. if (!frame) {
  82. return frame;
  83. }
  84. if (event == AST_FRAMEHOOK_EVENT_WRITE) {
  85. ast_log(LOG_NOTICE, "YAY we received a frame in the write direction, Type: %d\n", frame->frametype)
  86. ast_framehook_detach(chan, id); // the channel is guaranteed to be locked during this function call.
  87. } else if (event == AST_FRAMEHOOK_EVENT_READ) {
  88. ast_log(LOG_NOTICE, "YAY we received a frame in the read direction: Type: %d\n", frame->frametype);
  89. ast_framehook_detach(chan, id); // the channel is guaranteed to be locked during this function call.
  90. }
  91. return frame;
  92. {
  93. int some_function()
  94. {
  95. struct ast_framehook_interface interface = {
  96. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  97. .event_cb = hook_event_cb,
  98. .destroy_cb = hook_destroy_cb,
  99. };
  100. int *id = ast_calloc(1, sizeof(int));
  101. if (!id) {
  102. return -1;
  103. }
  104. interface.data = id; // This data will be returned to us in the callbacks.
  105. ast_channel_lock(chan);
  106. *id = ast_framehook_attach(chan, &interface);
  107. ast_channel_unlock(chan);
  108. if (*id < 0) {
  109. // framehook attach failed, free data
  110. ast_free(id);
  111. return -1;
  112. }
  113. return 0;
  114. }
  115. \endcode
  116. */
  117. #ifndef _AST_FRAMEHOOK_H_
  118. #define _AST_FRAMEHOOK_H_
  119. #include "asterisk/linkedlists.h"
  120. #include "asterisk/frame.h"
  121. struct ast_framehook;
  122. struct ast_framehook_list;
  123. /*!
  124. * \brief These are the types of events that the framehook's event callback can receive
  125. * \since 1.8
  126. */
  127. enum ast_framehook_event {
  128. AST_FRAMEHOOK_EVENT_READ, /*!< frame is intercepted in the read direction on the channel. */
  129. AST_FRAMEHOOK_EVENT_WRITE, /*!< frame is intercepted on the write direction on the channel. */
  130. AST_FRAMEHOOK_EVENT_ATTACHED, /*!< framehook is attached and running on the channel, the first message sent to event_cb. */
  131. AST_FRAMEHOOK_EVENT_DETACHED /*!< framehook is detached from the channel, last message sent to event_cb. */
  132. };
  133. /*!
  134. * \brief This callback is called every time an event occurs on the framehook.
  135. * \since 1.8
  136. *
  137. * \details Two events are guaranteed to occur once the ast_framehook_attach()
  138. * function is called. These events are AST_FRAMEHOOK_EVENT_ATTACHED, which occurs
  139. * immediately after the framehook is attached to a channel, and
  140. * AST_FRAMEHOOK_EVENT_DETACHED, which occurs right after the framehook is
  141. * detached.
  142. *
  143. * It is completely valid for the frame variable to be set to NULL. Always do a NULL
  144. * check on the frame before attempted to access it. When the frame variable is present,
  145. * it is safe to view and manipulate that frame in any way possible. It is even safe
  146. * to return a completely different frame, but when that occurs this function is in
  147. * charge of freeing the previous frame.
  148. *
  149. * The ast_channel will always be locked during this callback. Never attempt to unlock the
  150. * channel for any reason.
  151. *
  152. * \param channel, The ast_channel this framehook is attached to
  153. * \param frame, The ast_frame being intercepted for viewing and manipulation
  154. * \param event, The type of event which is occurring
  155. * \param data, The data pointer provided at framehook initilization.
  156. *
  157. * \retval the resulting frame.
  158. */
  159. typedef struct ast_frame *(*ast_framehook_event_callback)(
  160. struct ast_channel *chan,
  161. struct ast_frame *frame,
  162. enum ast_framehook_event event,
  163. void *data);
  164. /*!
  165. * \brief This callback is called immediately before the framehook is destroyed.
  166. * \since 1.8
  167. * \note This function should be used to clean up any pointers pointing to the
  168. * framehook structure as the framehook will be freed immediately afterwards.
  169. *
  170. * \param data, The data pointer provided at framehook initialization. This
  171. * is a good place to clean up any state data allocated for the framehook stored in this
  172. * pointer.
  173. */
  174. typedef void (*ast_framehook_destroy_callback)(void *data);
  175. /*!
  176. * \brief This callback is called to determine if the framehook is currently consuming
  177. * frames of a given type
  178. * \since 12
  179. *
  180. * \param data, The data pointer provided at framehook initilization.
  181. * \param type, The type of frame.
  182. *
  183. * \return 0 if frame type is being ignored
  184. * \return 1 if frame type is not being ignored
  185. */
  186. typedef int (*ast_framehook_consume_callback)(void *data, enum ast_frame_type type);
  187. /*!
  188. * \brief This callback is called when a masquerade occurs on a channel with a framehook
  189. * \since 12
  190. *
  191. * \param data, The data pointer provided at framehook initialization.
  192. * \param framehook_id, The framehook ID where the framehook lives now
  193. * \param old_chan, The channel that was masqueraded.
  194. * \param new_chan, The channel that the masqueraded channel became.
  195. */
  196. typedef void (*ast_framehook_chan_fixup_callback)(void *data, int framehook_id,
  197. struct ast_channel *old_chan, struct ast_channel *new_chan);
  198. #define AST_FRAMEHOOK_INTERFACE_VERSION 4
  199. /*! This interface is required for attaching a framehook to a channel. */
  200. struct ast_framehook_interface {
  201. /*! framehook interface version number */
  202. uint16_t version;
  203. /*! event_cb represents the function that will be called everytime an event occurs on the framehook. */
  204. ast_framehook_event_callback event_cb;
  205. /*! destroy_cb is optional. This function is called immediately before the framehook
  206. * is destroyed to allow for stored_data cleanup. */
  207. ast_framehook_destroy_callback destroy_cb;
  208. /*! consume_cb is optional. This function is called to query whether the framehook is consuming
  209. * frames of a specific type at this time. If this callback is not implemented it is assumed that the
  210. * framehook will consume frames of all types. */
  211. ast_framehook_consume_callback consume_cb;
  212. /*! chan_fixup_cb is optional. This function is called when the channel that a framehook is running
  213. * on is masqueraded and should be used to move any essential framehook data onto the channel the
  214. * old channel was masqueraded to. */
  215. ast_framehook_chan_fixup_callback chan_fixup_cb;
  216. /*! chan_breakdown_cb is optional. This function is called when another channel is masqueraded into
  217. * the channel that a framehook is running on and should be used to evaluate whether the framehook
  218. * should remain on the channel. */
  219. ast_framehook_chan_fixup_callback chan_breakdown_cb;
  220. /*! disable_inheritance is optional. If set to non-zero, when a channel using this framehook is
  221. * masqueraded, detach and destroy the framehook instead of moving it to the new channel. */
  222. int disable_inheritance;
  223. /*! This pointer can represent any custom data to be stored on the !framehook. This
  224. * data pointer will be provided during each event callback which allows the framehook
  225. * to store any stateful data associated with the application using the hook. */
  226. void *data;
  227. };
  228. /*!
  229. * \brief Attach an framehook onto a channel for frame interception.
  230. * \since 1.8
  231. *
  232. * \param chan ast_channel The channel to attach the hook on to.
  233. * \param i framehook interface, The framehook's callback functions and stored data.
  234. *
  235. * \pre The Channel must be locked during this function call.
  236. *
  237. * \note The data pointer is never touched by the framehook API except to
  238. * provide it during the event and destruction callbacks. It is entirely up to the
  239. * application using this API to manage the memory associated with the data pointer.
  240. *
  241. * \retval On success, non-negative id representing this hook on the channel
  242. * \retval On failure, -1
  243. */
  244. int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i);
  245. /*!
  246. * \brief Detach an framehook from a channel.
  247. * \since 1.8
  248. *
  249. * \pre The Channel must be locked during this function call.
  250. * If this function is never called after attaching an framehook,
  251. * the framehook will be detached and destroyed during channel
  252. * destruction.
  253. *
  254. * \param chan The channel the framehook is attached to
  255. * \param framehook_id The framehook's id
  256. *
  257. * \retval 0 success
  258. * \retval -1 framehook did not exist on the channel. This means the
  259. * framehook either never existed on the channel, or was already detached.
  260. */
  261. int ast_framehook_detach(struct ast_channel *chan, int framehook_id);
  262. /*!
  263. * \brief This is used by the channel API to detach and destroy all
  264. * framehooks on a channel during channel destruction.
  265. * \since 1.8
  266. *
  267. * \pre The Channel must be locked during this function call.
  268. *
  269. * \param chan channel containing the framehook list to destroy.
  270. * \retval 0 success
  271. * \retval -1 failure
  272. */
  273. int ast_framehook_list_destroy(struct ast_channel *chan);
  274. /*!
  275. * \brief This is used by the channel API during a masquerade operation
  276. * to move all mobile framehooks from the original channel to the clone channel.
  277. * \since 12.5.0
  278. *
  279. * \pre Both channels must be locked prior to this function call.
  280. *
  281. * \param old_chan The channel being cloned from
  282. * \param new_chan The channel being cloned to
  283. */
  284. void ast_framehook_list_fixup(struct ast_channel *old_chan, struct ast_channel *new_chan);
  285. /*!
  286. * \brief This is used by the channel API push a frame read event to a channel's framehook list.
  287. * \since 1.8
  288. *
  289. * \details After this function completes, the resulting frame that is returned could be anything,
  290. * even NULL. There is nothing to keep up with after this function. If the frame is modified, the
  291. * framehook callback is in charge of any memory management associated with that modification.
  292. *
  293. * \pre The Channel must be locked during this function call.
  294. *
  295. * \param framehooks list to push event to.
  296. * \param frame being pushed to the framehook list.
  297. *
  298. * \return The resulting frame after being viewed and modified by the framehook callbacks.
  299. */
  300. struct ast_frame *ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame);
  301. /*!
  302. * \brief This is used by the channel API push a frame write event to a channel's framehook list.
  303. * \since 1.8
  304. *
  305. * \details After this function completes, the resulting frame that is returned could be anything,
  306. * even NULL. There is nothing to keep up with after this function. If the frame is modified, the
  307. * framehook callback is in charge of any memory management associated with that modification.
  308. *
  309. * \pre The Channel must be locked during this function call.
  310. *
  311. * \param framehooks list to push event to.
  312. * \param frame being pushed to the framehook list.
  313. *
  314. * \return The resulting frame after being viewed and modified by the framehook callbacks.
  315. */
  316. struct ast_frame *ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame);
  317. /*!
  318. * \brief Determine if an framehook list is empty or not
  319. * \since 1.8
  320. * \pre The Channel must be locked during this function call.
  321. *
  322. * \param framehooks the framehook list
  323. * \retval 0, not empty
  324. * \retval 1, is empty
  325. */
  326. int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks);
  327. /*!
  328. * \brief Determine if a framehook list is free of active framehooks or not
  329. * \since 12.0.0
  330. * \pre The channel must be locked during this function call.
  331. *
  332. * \param framehooks the framehook list
  333. * \retval 0, not empty
  334. * \retval 1, is empty (aside from dying framehooks)
  335. *
  336. * \note This function is very similar to ast_framehook_list_is_empty, but it checks individual
  337. * framehooks to see if they have been marked for destruction and doesn't count them if they are.
  338. */
  339. int ast_framehook_list_contains_no_active(struct ast_framehook_list *framehooks);
  340. /*!
  341. * \brief Determine if a framehook list is free of active framehooks consuming a specific type of frame
  342. * \since 12.3.0
  343. * \pre The channel must be locked during this function call.
  344. *
  345. * \param framehooks the framehook list
  346. * \retval 0, not empty
  347. * \retval 1, is empty (aside from dying framehooks)
  348. *
  349. * \note This function is very similar to ast_framehook_list_is_empty, but it checks individual
  350. * framehooks to see if they have been marked for destruction and doesn't count them if they are.
  351. */
  352. int ast_framehook_list_contains_no_active_of_type(struct ast_framehook_list *framehooks,
  353. enum ast_frame_type type);
  354. #endif /* _AST_FRAMEHOOK_H */