2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2010, Digium, Inc.
6 * David Vossel <dvossel@digium.com>
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.
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.
21 * \brief FrameHooks Architecture
23 * \author David Vossel <dvossel@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/channel.h"
35 #include "asterisk/linkedlists.h"
36 #include "asterisk/framehook.h"
37 #include "asterisk/frame.h"
39 struct ast_framehook {
40 struct ast_framehook_interface i;
41 /*! This pointer to ast_channel the framehook is attached to. */
42 struct ast_channel *chan;
43 /*! the id representing this framehook on a channel */
45 /*! when set, this signals the read and write function to detach the hook */
46 int detach_and_destroy_me;
47 /*! list entry for ast_framehook_list object */
48 AST_LIST_ENTRY(ast_framehook) list;
51 struct ast_framehook_list {
52 /*! the number of hooks currently present */
54 /*! id for next framehook added */
55 unsigned int id_count;
56 AST_LIST_HEAD_NOLOCK(, ast_framehook) list;
59 enum framehook_detachment_mode
61 /*! Destroy the framehook outright. */
62 FRAMEHOOK_DETACH_DESTROY = 0,
63 /*! Remove the framehook from the channel, but don't destroy the data since
64 * it will be used by a replacement framehook on another channel. */
65 FRAMEHOOK_DETACH_PRESERVE,
68 static void framehook_detach(struct ast_framehook *framehook, enum framehook_detachment_mode mode)
70 struct ast_frame *frame;
71 frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_DETACHED, framehook->i.data);
72 /* never assume anything about this function. If you can return a frame during
73 * the detached event, then assume someone will. */
77 framehook->chan = NULL;
79 if (mode == FRAMEHOOK_DETACH_DESTROY && framehook->i.destroy_cb) {
80 framehook->i.destroy_cb(framehook->i.data);
85 static struct ast_frame *framehook_list_push_event(struct ast_framehook_list *framehooks, struct ast_frame *frame, enum ast_framehook_event event)
87 struct ast_framehook *framehook;
88 struct ast_frame *original_frame;
96 skip_size = sizeof(int) * framehooks->count;
97 skip = ast_alloca(skip_size);
98 memset(skip, 0, skip_size);
101 unsigned int num = 0;
102 original_frame = frame;
104 AST_LIST_TRAVERSE_SAFE_BEGIN(&framehooks->list, framehook, list) {
105 if (framehook->detach_and_destroy_me) {
106 /* this guy is signaled for destruction */
107 AST_LIST_REMOVE_CURRENT(list);
108 framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
112 /* If this framehook has been marked as needing to be skipped, do so */
118 frame = framehook->i.event_cb(framehook->chan, frame, event, framehook->i.data);
120 if (frame != original_frame) {
121 /* To prevent looping we skip any framehooks that have already provided a modified frame */
128 AST_LIST_TRAVERSE_SAFE_END;
129 } while (frame != original_frame);
134 int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i)
136 struct ast_framehook *framehook;
137 struct ast_framehook_list *fh_list;
138 struct ast_frame *frame;
139 if (i->version != AST_FRAMEHOOK_INTERFACE_VERSION) {
140 ast_log(LOG_ERROR, "Version '%hu' of framehook interface not what we compiled against (%hu)\n",
141 i->version, AST_FRAMEHOOK_INTERFACE_VERSION);
144 if (!i->event_cb || !(framehook = ast_calloc(1, sizeof(*framehook)))) {
148 framehook->chan = chan;
150 /* create the framehook list if it didn't already exist */
151 if (!ast_channel_framehooks(chan)) {
152 if (!(fh_list = ast_calloc(1, sizeof(*ast_channel_framehooks(chan))))) {
156 ast_channel_framehooks_set(chan, fh_list);
159 ast_channel_framehooks(chan)->count++;
160 framehook->id = ++ast_channel_framehooks(chan)->id_count;
161 AST_LIST_INSERT_TAIL(&ast_channel_framehooks(chan)->list, framehook, list);
163 /* Tell the event callback we're live and rocking */
164 frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_ATTACHED, framehook->i.data);
166 /* Never assume anything about this function. If you can return a frame during
167 * the attached event, then assume someone will. */
172 if (ast_channel_is_bridged(chan)) {
173 ast_channel_set_unbridged_nolock(chan, 1);
176 return framehook->id;
179 int ast_framehook_detach(struct ast_channel *chan, int id)
181 struct ast_framehook *framehook;
184 if (!ast_channel_framehooks(chan)) {
188 AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
189 if (framehook->id == id) {
190 /* we mark for detachment rather than doing explicitly here because
191 * it needs to be safe for this function to be called within the
192 * event callback. If we allowed the hook to actually be destroyed
193 * immediately here, the event callback would crash on exit. */
194 framehook->detach_and_destroy_me = 1;
199 AST_LIST_TRAVERSE_SAFE_END;
201 if (!res && ast_channel_is_bridged(chan)) {
202 ast_channel_set_unbridged_nolock(chan, 1);
208 int ast_framehook_list_destroy(struct ast_channel *chan)
210 struct ast_framehook *framehook;
212 if (!ast_channel_framehooks(chan)) {
215 AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
216 AST_LIST_REMOVE_CURRENT(list);
217 framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
219 AST_LIST_TRAVERSE_SAFE_END;
220 ast_free(ast_channel_framehooks(chan));
221 ast_channel_framehooks_set(chan, NULL);
225 void ast_framehook_list_fixup(struct ast_channel *old_chan, struct ast_channel *new_chan)
227 struct ast_framehook *framehook;
228 int moved_framehook_id;
230 if (ast_channel_framehooks(new_chan)) {
231 AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(new_chan)->list, framehook, list) {
232 if (framehook->i.disable_inheritance) {
233 ast_framehook_detach(new_chan, framehook->id);
237 if (framehook->i.chan_breakdown_cb) {
238 framehook->i.chan_breakdown_cb(framehook->i.data, framehook->id,
242 AST_LIST_TRAVERSE_SAFE_END;
245 if (!ast_channel_framehooks(old_chan)) {
249 if (!AST_LIST_EMPTY(&ast_channel_framehooks(old_chan)->list)
250 && ast_channel_is_bridged(old_chan)) {
251 ast_channel_set_unbridged_nolock(old_chan, 1);
253 while ((framehook = AST_LIST_REMOVE_HEAD(&ast_channel_framehooks(old_chan)->list, list))) {
254 /* If inheritance is not allowed for this framehook, just destroy it. */
255 if (framehook->i.disable_inheritance) {
256 framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
260 /* Otherwise move it to the other channel and perform any fixups set by the framehook interface */
261 moved_framehook_id = ast_framehook_attach(new_chan, &framehook->i);
262 if (moved_framehook_id < 0) {
263 ast_log(LOG_WARNING, "Failed framehook copy during masquerade. Expect loss of features.\n");
264 framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
266 if (framehook->i.chan_fixup_cb) {
267 framehook->i.chan_fixup_cb(framehook->i.data, moved_framehook_id,
271 framehook_detach(framehook, FRAMEHOOK_DETACH_PRESERVE);
276 int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
281 return AST_LIST_EMPTY(&framehooks->list) ? 1 : 0;
284 int ast_framehook_list_contains_no_active(struct ast_framehook_list *framehooks)
286 return ast_framehook_list_contains_no_active_of_type(framehooks, 0);
289 int ast_framehook_list_contains_no_active_of_type(struct ast_framehook_list *framehooks,
290 enum ast_frame_type type)
292 struct ast_framehook *cur;
298 if (AST_LIST_EMPTY(&framehooks->list)) {
302 AST_LIST_TRAVERSE(&framehooks->list, cur, list) {
303 if (cur->detach_and_destroy_me) {
306 if (type && cur->i.consume_cb && !cur->i.consume_cb(cur->i.data, type)) {
315 struct ast_frame *ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
317 return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_WRITE);
320 struct ast_frame *ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
322 return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_READ);