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 static void framehook_detach_and_destroy(struct ast_framehook *framehook)
61 struct ast_frame *frame;
62 frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_DETACHED, framehook->i.data);
63 /* never assume anything about this function. If you can return a frame during
64 * the detached event, then assume someone will. */
68 framehook->chan = NULL;
70 if (framehook->i.destroy_cb) {
71 framehook->i.destroy_cb(framehook->i.data);
76 static struct ast_frame *framehook_list_push_event(struct ast_framehook_list *framehooks, struct ast_frame *frame, enum ast_framehook_event event)
78 struct ast_framehook *framehook;
79 struct ast_frame *original_frame;
87 skip_size = sizeof(int) * framehooks->count;
88 skip = alloca(skip_size);
89 memset(skip, 0, skip_size);
93 original_frame = frame;
95 AST_LIST_TRAVERSE_SAFE_BEGIN(&framehooks->list, framehook, list) {
96 if (framehook->detach_and_destroy_me) {
97 /* this guy is signaled for destruction */
98 AST_LIST_REMOVE_CURRENT(list);
99 framehook_detach_and_destroy(framehook);
103 /* If this framehook has been marked as needing to be skipped, do so */
109 frame = framehook->i.event_cb(framehook->chan, frame, event, framehook->i.data);
111 if (frame != original_frame) {
112 /* To prevent looping we skip any framehooks that have already provided a modified frame */
119 AST_LIST_TRAVERSE_SAFE_END;
120 } while (frame != original_frame);
125 int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i)
127 struct ast_framehook *framehook;
128 struct ast_framehook_list *fh_list;
129 struct ast_frame *frame;
130 if (i->version != AST_FRAMEHOOK_INTERFACE_VERSION) {
131 ast_log(LOG_ERROR, "Version '%hu' of framehook interface not what we compiled against (%hu)\n",
132 i->version, AST_FRAMEHOOK_INTERFACE_VERSION);
135 if (!i->event_cb || !(framehook = ast_calloc(1, sizeof(*framehook)))) {
139 framehook->chan = chan;
141 /* create the framehook list if it didn't already exist */
142 if (!ast_channel_framehooks(chan)) {
143 if (!(fh_list = ast_calloc(1, sizeof(*ast_channel_framehooks(chan))))) {
147 ast_channel_framehooks_set(chan, fh_list);
150 ast_channel_framehooks(chan)->count++;
151 framehook->id = ++ast_channel_framehooks(chan)->id_count;
152 AST_LIST_INSERT_TAIL(&ast_channel_framehooks(chan)->list, framehook, list);
154 /* Tell the event callback we're live and rocking */
155 frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_ATTACHED, framehook->i.data);
157 /* Never assume anything about this function. If you can return a frame during
158 * the attached event, then assume someone will. */
163 if (ast_channel_is_bridged(chan)) {
164 ast_softhangup_nolock(chan, AST_SOFTHANGUP_UNBRIDGE);
167 return framehook->id;
170 int ast_framehook_detach(struct ast_channel *chan, int id)
172 struct ast_framehook *framehook;
175 if (!ast_channel_framehooks(chan)) {
179 AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
180 if (framehook->id == id) {
181 /* we mark for detachment rather than doing explicitly here because
182 * it needs to be safe for this function to be called within the
183 * event callback. If we allowed the hook to actually be destroyed
184 * immediately here, the event callback would crash on exit. */
185 framehook->detach_and_destroy_me = 1;
190 AST_LIST_TRAVERSE_SAFE_END;
195 int ast_framehook_list_destroy(struct ast_channel *chan)
197 struct ast_framehook *framehook;
199 if (!ast_channel_framehooks(chan)) {
202 AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
203 AST_LIST_REMOVE_CURRENT(list);
204 framehook_detach_and_destroy(framehook);
206 AST_LIST_TRAVERSE_SAFE_END;
207 ast_free(ast_channel_framehooks(chan));
208 ast_channel_framehooks_set(chan, NULL);
212 int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
217 return AST_LIST_EMPTY(&framehooks->list) ? 1 : 0;
220 int ast_framehook_list_contains_no_active(struct ast_framehook_list *framehooks)
222 return ast_framehook_list_contains_no_active_of_type(framehooks, 0);
225 int ast_framehook_list_contains_no_active_of_type(struct ast_framehook_list *framehooks,
226 enum ast_frame_type type)
228 struct ast_framehook *cur;
234 if (AST_LIST_EMPTY(&framehooks->list)) {
238 AST_LIST_TRAVERSE(&framehooks->list, cur, list) {
239 if (cur->detach_and_destroy_me) {
242 if (type && cur->i.consume_cb && !cur->i.consume_cb(cur->i.data, type)) {
251 struct ast_frame *ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
253 return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_WRITE);
256 struct ast_frame *ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
258 return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_READ);