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 #include "asterisk/channel.h"
33 #include "asterisk/linkedlists.h"
34 #include "asterisk/framehook.h"
35 #include "asterisk/frame.h"
37 struct ast_framehook {
38 struct ast_framehook_interface i;
39 /*! This pointer to ast_channel the framehook is attached to. */
40 struct ast_channel *chan;
41 /*! the id representing this framehook on a channel */
43 /*! when set, this signals the read and write function to detach the hook */
44 int detach_and_destroy_me;
45 /*! list entry for ast_framehook_list object */
46 AST_LIST_ENTRY(ast_framehook) list;
49 struct ast_framehook_list {
50 /*! the number of hooks currently present */
52 /*! id for next framehook added */
53 unsigned int id_count;
54 AST_LIST_HEAD_NOLOCK(, ast_framehook) list;
57 enum framehook_detachment_mode
59 /*! Destroy the framehook outright. */
60 FRAMEHOOK_DETACH_DESTROY = 0,
61 /*! Remove the framehook from the channel, but don't destroy the data since
62 * it will be used by a replacement framehook on another channel. */
63 FRAMEHOOK_DETACH_PRESERVE,
66 static void framehook_detach(struct ast_framehook *framehook, enum framehook_detachment_mode mode)
68 struct ast_frame *frame;
69 frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_DETACHED, framehook->i.data);
70 /* never assume anything about this function. If you can return a frame during
71 * the detached event, then assume someone will. */
75 framehook->chan = NULL;
77 if (mode == FRAMEHOOK_DETACH_DESTROY && framehook->i.destroy_cb) {
78 framehook->i.destroy_cb(framehook->i.data);
83 static struct ast_frame *framehook_list_push_event(struct ast_framehook_list *framehooks, struct ast_frame *frame, enum ast_framehook_event event)
85 struct ast_framehook *framehook;
86 struct ast_frame *original_frame;
94 skip_size = sizeof(int) * framehooks->count;
95 skip = ast_alloca(skip_size);
96 memset(skip, 0, skip_size);
100 original_frame = frame;
102 AST_LIST_TRAVERSE_SAFE_BEGIN(&framehooks->list, framehook, list) {
103 if (framehook->detach_and_destroy_me) {
104 /* this guy is signaled for destruction */
105 AST_LIST_REMOVE_CURRENT(list);
106 framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
110 /* If this framehook has been marked as needing to be skipped, do so */
116 frame = framehook->i.event_cb(framehook->chan, frame, event, framehook->i.data);
118 if (frame != original_frame) {
119 /* To prevent looping we skip any framehooks that have already provided a modified frame */
126 AST_LIST_TRAVERSE_SAFE_END;
127 } while (frame != original_frame);
132 int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i)
134 struct ast_framehook *framehook;
135 struct ast_framehook_list *fh_list;
136 struct ast_frame *frame;
137 if (i->version != AST_FRAMEHOOK_INTERFACE_VERSION) {
138 ast_log(LOG_ERROR, "Version '%hu' of framehook interface not what we compiled against (%i)\n",
139 i->version, AST_FRAMEHOOK_INTERFACE_VERSION);
142 if (!i->event_cb || !(framehook = ast_calloc(1, sizeof(*framehook)))) {
146 framehook->chan = chan;
148 /* create the framehook list if it didn't already exist */
149 if (!ast_channel_framehooks(chan)) {
150 if (!(fh_list = ast_calloc(1, sizeof(*ast_channel_framehooks(chan))))) {
154 ast_channel_framehooks_set(chan, fh_list);
157 ast_channel_framehooks(chan)->count++;
158 framehook->id = ++ast_channel_framehooks(chan)->id_count;
159 AST_LIST_INSERT_TAIL(&ast_channel_framehooks(chan)->list, framehook, list);
161 /* Tell the event callback we're live and rocking */
162 frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_ATTACHED, framehook->i.data);
164 /* Never assume anything about this function. If you can return a frame during
165 * the attached event, then assume someone will. */
170 if (ast_channel_is_bridged(chan)) {
171 ast_channel_set_unbridged_nolock(chan, 1);
174 return framehook->id;
177 int ast_framehook_detach(struct ast_channel *chan, int id)
179 struct ast_framehook *framehook;
182 if (!ast_channel_framehooks(chan)) {
186 AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
187 if (framehook->id == id) {
188 /* we mark for detachment rather than doing explicitly here because
189 * it needs to be safe for this function to be called within the
190 * event callback. If we allowed the hook to actually be destroyed
191 * immediately here, the event callback would crash on exit. */
192 framehook->detach_and_destroy_me = 1;
197 AST_LIST_TRAVERSE_SAFE_END;
199 if (!res && ast_channel_is_bridged(chan)) {
200 ast_channel_set_unbridged_nolock(chan, 1);
206 int ast_framehook_list_destroy(struct ast_channel *chan)
208 struct ast_framehook *framehook;
210 if (!ast_channel_framehooks(chan)) {
213 AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
214 AST_LIST_REMOVE_CURRENT(list);
215 framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
217 AST_LIST_TRAVERSE_SAFE_END;
218 ast_free(ast_channel_framehooks(chan));
219 ast_channel_framehooks_set(chan, NULL);
223 void ast_framehook_list_fixup(struct ast_channel *old_chan, struct ast_channel *new_chan)
225 struct ast_framehook *framehook;
226 int moved_framehook_id;
228 if (ast_channel_framehooks(new_chan)) {
229 AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(new_chan)->list, framehook, list) {
230 if (framehook->i.disable_inheritance) {
231 ast_framehook_detach(new_chan, framehook->id);
235 if (framehook->i.chan_breakdown_cb) {
236 framehook->i.chan_breakdown_cb(framehook->i.data, framehook->id,
240 AST_LIST_TRAVERSE_SAFE_END;
243 if (!ast_channel_framehooks(old_chan)) {
247 if (!AST_LIST_EMPTY(&ast_channel_framehooks(old_chan)->list)
248 && ast_channel_is_bridged(old_chan)) {
249 ast_channel_set_unbridged_nolock(old_chan, 1);
251 while ((framehook = AST_LIST_REMOVE_HEAD(&ast_channel_framehooks(old_chan)->list, list))) {
252 /* If inheritance is not allowed for this framehook, just destroy it. */
253 if (framehook->i.disable_inheritance) {
254 framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
258 /* Otherwise move it to the other channel and perform any fixups set by the framehook interface */
259 moved_framehook_id = ast_framehook_attach(new_chan, &framehook->i);
260 if (moved_framehook_id < 0) {
261 ast_log(LOG_WARNING, "Failed framehook copy during masquerade. Expect loss of features.\n");
262 framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
264 if (framehook->i.chan_fixup_cb) {
265 framehook->i.chan_fixup_cb(framehook->i.data, moved_framehook_id,
269 framehook_detach(framehook, FRAMEHOOK_DETACH_PRESERVE);
274 int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
279 return AST_LIST_EMPTY(&framehooks->list) ? 1 : 0;
282 int ast_framehook_list_contains_no_active(struct ast_framehook_list *framehooks)
284 return ast_framehook_list_contains_no_active_of_type(framehooks, 0);
287 int ast_framehook_list_contains_no_active_of_type(struct ast_framehook_list *framehooks,
288 enum ast_frame_type type)
290 struct ast_framehook *cur;
296 if (AST_LIST_EMPTY(&framehooks->list)) {
300 AST_LIST_TRAVERSE(&framehooks->list, cur, list) {
301 if (cur->detach_and_destroy_me) {
304 if (type && cur->i.consume_cb && !cur->i.consume_cb(cur->i.data, type)) {
313 struct ast_frame *ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
315 return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_WRITE);
318 struct ast_frame *ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
320 return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_READ);