Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


framehook.h
Go to the documentation of this file.
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 
19 /*! \file
20  * \brief FrameHook Architecture
21  */
22 
23 /*!
24 
25 \page AstFrameHookAPI Asterisk FrameHook API
26 
27 \section FrameHookFunctionality How FrameHooks Work
28  FrameHooks work by intercepting all frames being written and read off
29  a channel and allowing those frames to be viewed and manipulated within a
30  call back function. Frame interception occurs before any processing is
31  done on the frame, which means this hook can be used to transparently
32  manipulate a frame before it is read from the channel or written
33  to the tech_pvt. This API can be thought of as a layer between the
34  channel API and the Asterisk core when going in the READ direction, and
35  as a layer between the Channel API and the tech_pvt when going in the
36  WRITE direction.
37 
38 \section FrameHookAPIUsage How to Use an FrameHook
39  Attaching and detaching an FrameHook to a channel is very simple. There are only
40  two functions involved, ast_framehook_attach() which will return an id representing
41  the new FrameHook on the channel, and ast_framehook_detach() which signals the
42  FrameHook for detachment and destruction. Below is detailed information each of these
43  functions and their usage.
44 
45 \code
46  struct ast_framehook_interface interface = {
47  .version = AST_FRAMEHOOK_INTERFACE_VERSION,
48  .event_cb = hook_event_cb,
49  .destroy_cb = hook_destroy_cb,
50  .data = data, // where the data ptr points to any custom data used later by the hook cb.
51  };
52  int id = ast_framehook_attach(channel, &interface);
53 \endcode
54 
55  The ast_framehook_attach() function creates and attaches a new FrameHook onto
56  a channel. Once attached to the channel, the FrameHook will call the event_callback
57  function each time a frame is written or read on the channel. A custom data
58  pointer can be provided to this function to store on the FrameHook as well. This
59  pointer can be used to keep up with any statefull information associated with the FrameHook
60  and is provided during the event_callback function. The destroy_callback function is optional.
61  This function exists so any custom data stored on the FrameHook can be destroyed before
62  the Framehook if destroyed.
63 
64 \code
65  ast_framehook_detach(channel, id);
66 \endcode
67 
68  The ast_framehook_detach() function signals the FrameHook represented by an id to
69  be detached and destroyed on a channel. Since it is possible this function may be called
70  during the FrameHook's event callback, it is impossible to synchronously detach the
71  FrameHook from the channel during this function call. It is guaranteed that the next
72  event proceeding the ast_framehook_detach() will be of type AST_FRAMEHOOK_EVENT_DETACH,
73  and that after that event occurs no other event will ever be issued for that FrameHook.
74  Once the FrameHook is destroyed, the destroy callback function will be called if it was
75  provided. Note that if this function is never called, the FrameHook will be detached
76  on channel destruction.
77 
78 \section FrameHookAPICodeExample FrameHook Example Code
79  The example code below attaches an FrameHook on a channel, and then detachs it when
80  the first ast_frame is read or written to the event callback function. The Framehook's id
81  is stored on the FrameHook's data pointer so it can be detached within the callback.
82 
83 \code
84  static void destroy_cb(void *data) {
85  ast_free(data);
86  }
87 
88  static struct ast_frame *event_cb(struct ast_channel *chan,
89  struct ast_frame *frame,
90  enum ast_framehook_event event,
91  void *data) {
92 
93  int *id = data;
94 
95  if (!frame) {
96  return frame;
97  }
98 
99  if (event == AST_FRAMEHOOK_EVENT_WRITE) {
100  ast_log(LOG_NOTICE, "YAY we received a frame in the write direction, Type: %d\n", frame->frametype)
101  ast_framehook_detach(chan, id); // the channel is guaranteed to be locked during this function call.
102  } else if (event == AST_FRAMEHOOK_EVENT_READ) {
103  ast_log(LOG_NOTICE, "YAY we received a frame in the read direction: Type: %d\n", frame->frametype);
104  ast_framehook_detach(chan, id); // the channel is guaranteed to be locked during this function call.
105  }
106 
107  return frame;
108  {
109 
110  int some_function()
111  {
112  struct ast_framehook_interface interface = {
113  .version = AST_FRAMEHOOK_INTERFACE_VERSION,
114  .event_cb = hook_event_cb,
115  .destroy_cb = hook_destroy_cb,
116  };
117  int *id = ast_calloc(1, sizeof(int));
118 
119  if (!id) {
120  return -1;
121  }
122 
123  interface.data = id; // This data will be returned to us in the callbacks.
124 
125  ast_channel_lock(chan);
126  *id = ast_framehook_attach(chan, &interface);
127  ast_channel_unlock(chan);
128 
129  if (*id < 0) {
130  // framehook attach failed, free data
131  ast_free(id);
132  return -1;
133  }
134  return 0;
135  }
136 \endcode
137 */
138 
139 #ifndef _AST_FRAMEHOOK_H_
140 #define _AST_FRAMEHOOK_H_
141 #include "asterisk/linkedlists.h"
142 #include "asterisk/frame.h"
143 
144 struct ast_framehook;
145 struct ast_framehook_list;
146 
147 /*!
148  * \brief These are the types of events that the framehook's event callback can receive
149  * \since 1.8
150  */
152  AST_FRAMEHOOK_EVENT_READ, /*!< frame is intercepted in the read direction on the channel. */
153  AST_FRAMEHOOK_EVENT_WRITE, /*!< frame is intercepted on the write direction on the channel. */
154  AST_FRAMEHOOK_EVENT_ATTACHED, /*!< framehook is attached and running on the channel, the first message sent to event_cb. */
155  AST_FRAMEHOOK_EVENT_DETACHED /*!< framehook is detached from the channel, last message sent to event_cb. */
156 };
157 
158 /*!
159  * \brief This callback is called every time an event occurs on the framehook.
160  * \since 1.8
161  *
162  * \details Two events are guaranteed to occur once the ast_framehook_attach()
163  * function is called. These events are AST_FRAMEHOOK_EVENT_ATTACHED, which occurs
164  * immediately after the framehook is attached to a channel, and
165  * AST_FRAMEHOOK_EVENT_DETACHED, which occurs right after the framehook is
166  * detached.
167  *
168  * It is completely valid for the frame variable to be set to NULL. Always do a NULL
169  * check on the frame before attempted to access it. When the frame variable is present,
170  * it is safe to view and manipulate that frame in any way possible. It is even safe
171  * to return a completely different frame, but when that occurs this function is in
172  * charge of freeing the previous frame.
173  *
174  * The ast_channel will always be locked during this callback. Never attempt to unlock the
175  * channel for any reason.
176  *
177  * \param channel, The ast_channel this framehook is attached to
178  * \param frame, The ast_frame being intercepted for viewing and manipulation
179  * \param event, The type of event which is occurring
180  * \param data, The data pointer provided at framehook initilization.
181  *
182  * \retval the resulting frame.
183  */
184 typedef struct ast_frame *(*ast_framehook_event_callback)(
185  struct ast_channel *chan,
186  struct ast_frame *frame,
187  enum ast_framehook_event event,
188  void *data);
189 
190 /*!
191  * \brief This callback is called immediately before the framehook is destroyed.
192  * \since 1.8
193  * \note This function should be used to clean up any pointers pointing to the
194  * framehook structure as the framehook will be freed immediately afterwards.
195  *
196  * \param data, The data pointer provided at framehook initialization. This
197  * is a good place to clean up any state data allocated for the framehook stored in this
198  * pointer.
199  */
200 typedef void (*ast_framehook_destroy_callback)(void *data);
201 
202 #define AST_FRAMEHOOK_INTERFACE_VERSION 1
203 /*! This interface is required for attaching a framehook to a channel. */
205  /*! framehook interface version number */
206  uint16_t version;
207  /*! event_cb represents the function that will be called everytime an event occurs on the framehook. */
209  /*! destroy_cb is optional. This function is called immediately before the framehook
210  * is destroyed to allow for stored_data cleanup. */
212  /*! This pointer can represent any custom data to be stored on the !framehook. This
213  * data pointer will be provided during each event callback which allows the framehook
214  * to store any stateful data associated with the application using the hook. */
215  void *data;
216 };
217 
218 /*!
219  * \brief Attach an framehook onto a channel for frame interception.
220  * \since 1.8
221  *
222  * \param ast_channel, The channel to attach the hook on to.
223  * \param framehook interface, The framehook's callback functions and stored data.
224 *
225  * \pre XXX The Channel must be locked during this function all.
226  *
227  * \note The data pointer is never touched by the framehook API except to
228  * provide it during the event and destruction callbacks. It is entirely up to the
229  * application using this API to manage the memory associated with the data pointer.
230  *
231  * \retval On success, positive id representing this hook on the channel
232  * \retval On failure, -1
233  */
234 int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i);
235 
236 /*!
237  * \brief Detach an framehook from a channel.
238  * \since 1.8
239  *
240  * \pre XXX The Channel must be locked during this function all.
241  * If this function is never called after attaching an framehook,
242  * the framehook will be detached and destroyed during channel
243  * destruction.
244  *
245  * \param The channel the framehook is attached to
246  * \param The framehook's id
247  *
248  * \retval 0 success
249  * \retval -1 framehook did not exist on the channel. This means the
250  * framehook either never existed on the channel, or was already detached.
251  */
252 int ast_framehook_detach(struct ast_channel *chan, int framehook_id);
253 
254 /*!
255  * \brief This is used by the channel API to detach and destroy all
256  * framehooks on a channel during channel destruction.
257  * \since 1.8
258  *
259  * \pre XXX The Channel must be locked during this function all.
260  *
261  * \param channel containing the framehook list to destroy.
262  * \retval 0 success
263  * \retval -1 failure
264  */
265 int ast_framehook_list_destroy(struct ast_channel *chan);
266 
267 /*!
268  * \brief This is used by the channel API push a frame read event to a channel's framehook list.
269  * \since 1.8
270  *
271  * \details After this function completes, the resulting frame that is returned could be anything,
272  * even NULL. There is nothing to keep up with after this function. If the frame is modified, the
273  * framehook callback is in charge of any memory management associated with that modification.
274  *
275  * \pre XXX The Channel must be locked during this function all.
276  *
277  * \param framehook list to push event to.
278  * \param frame being pushed to the framehook list.
279  *
280  * \return The resulting frame after being viewed and modified by the framehook callbacks.
281  */
282 struct ast_frame *ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame);
283 
284 /*!
285  * \brief This is used by the channel API push a frame write event to a channel's framehook list.
286  * \since 1.8
287  *
288  * \details After this function completes, the resulting frame that is returned could be anything,
289  * even NULL. There is nothing to keep up with after this function. If the frame is modified, the
290  * framehook callback is in charge of any memory management associated with that modification.
291  *
292  * \pre XXX The Channel must be locked during this function all.
293  *
294  * \param framehook list to push event to.
295  * \param frame being pushed to the framehook list.
296  *
297  * \return The resulting frame after being viewed and modified by the framehook callbacks.
298  */
299 struct ast_frame *ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame);
300 
301 /*!
302  * \brief Determine if an framehook list is empty or not
303  * \since 1.8
304  * \pre XXX The Channel must be locked during this function all.
305  *
306  * \param the framehook list
307  * \retval 0, not empty
308  * \retval 1, is empty
309  */
310 int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks);
311 #endif /* _AST_FRAMEHOOK_H */
ast_framehook_event_callback event_cb
Definition: framehook.h:208
Main Channel structure associated with a channel.
Definition: channel.h:742
int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
Determine if an framehook list is empty or not.
Definition: framehook.c:172
ast_framehook_event
These are the types of events that the framehook&#39;s event callback can receive.
Definition: framehook.h:151
int ast_framehook_detach(struct ast_channel *chan, int framehook_id)
Detach an framehook from a channel.
Definition: framehook.c:130
ast_framehook_destroy_callback destroy_cb
Definition: framehook.h:211
int ast_framehook_list_destroy(struct ast_channel *chan)
This is used by the channel API to detach and destroy all framehooks on a channel during channel dest...
Definition: framehook.c:155
int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i)
Attach an framehook onto a channel for frame interception.
Definition: framehook.c:94
Asterisk internal frame definitions.
A set of macros to manage forward-linked lists.
struct ast_frame *(* ast_framehook_event_callback)(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data)
This callback is called every time an event occurs on the framehook.
Definition: framehook.h:184
struct ast_frame * ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
This is used by the channel API push a frame write event to a channel&#39;s framehook list...
Definition: framehook.c:180
void(* ast_framehook_destroy_callback)(void *data)
This callback is called immediately before the framehook is destroyed.
Definition: framehook.h:200
Data structure associated with a single frame of data.
Definition: frame.h:142
union ast_frame::@172 data
struct ast_frame * ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
This is used by the channel API push a frame read event to a channel&#39;s framehook list.
Definition: framehook.c:185