Undo r414123
[asterisk/asterisk.git] / main / framehook.c
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  *
21  * \brief FrameHooks Architecture
22  *
23  * \author David Vossel <dvossel@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/channel.h"
35 #include "asterisk/linkedlists.h"
36 #include "asterisk/framehook.h"
37 #include "asterisk/frame.h"
38
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 */
44         unsigned int id;
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;
49 };
50
51 struct ast_framehook_list {
52         /*! the number of hooks currently present */
53         unsigned int count;
54         /*! id for next framehook added */
55         unsigned int id_count;
56         AST_LIST_HEAD_NOLOCK(, ast_framehook) list;
57 };
58
59 static void framehook_detach_and_destroy(struct ast_framehook *framehook)
60 {
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. */
65         if (frame) {
66                 ast_frfree(frame);
67         }
68         framehook->chan = NULL;
69
70         if (framehook->i.destroy_cb) {
71                 framehook->i.destroy_cb(framehook->i.data);
72         }
73         ast_free(framehook);
74 }
75
76 static struct ast_frame *framehook_list_push_event(struct ast_framehook_list *framehooks, struct ast_frame *frame, enum ast_framehook_event event)
77 {
78         struct ast_framehook *framehook;
79         struct ast_frame *original_frame;
80         int *skip;
81         size_t skip_size;
82
83         if (!framehooks) {
84                 return frame;
85         }
86
87         skip_size = sizeof(int) * framehooks->count;
88         skip = alloca(skip_size);
89         memset(skip, 0, skip_size);
90
91         do {
92                 unsigned int num = 0;
93                 original_frame = frame;
94
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);
100                                 continue;
101                         }
102
103                         /* If this framehook has been marked as needing to be skipped, do so */
104                         if (skip[num]) {
105                                 num++;
106                                 continue;
107                         }
108
109                         frame = framehook->i.event_cb(framehook->chan, frame, event, framehook->i.data);
110
111                         if (frame != original_frame) {
112                                 /* To prevent looping we skip any framehooks that have already provided a modified frame */
113                                 skip[num] = 1;
114                                 break;
115                         }
116
117                         num++;
118                 }
119                 AST_LIST_TRAVERSE_SAFE_END;
120         } while (frame != original_frame);
121
122         return frame;
123 }
124
125 int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i)
126 {
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);
133                 return -1;
134         }
135         if (!i->event_cb || !(framehook = ast_calloc(1, sizeof(*framehook)))) {
136                 return -1;
137         }
138         framehook->i = *i;
139         framehook->chan = chan;
140
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))))) {
144                         ast_free(framehook);
145                         return -1;
146                 }
147                 ast_channel_framehooks_set(chan, fh_list);
148         }
149
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);
153
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);
156
157         /* Never assume anything about this function. If you can return a frame during
158          * the attached event, then assume someone will. */
159         if (frame) {
160                 ast_frfree(frame);
161         }
162
163         if (ast_channel_is_bridged(chan)) {
164                 ast_softhangup_nolock(chan, AST_SOFTHANGUP_UNBRIDGE);
165         }
166
167         return framehook->id;
168 }
169
170 int ast_framehook_detach(struct ast_channel *chan, int id)
171 {
172         struct ast_framehook *framehook;
173         int res = -1;
174
175         if (!ast_channel_framehooks(chan)) {
176                 return res;
177         }
178
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;
186                         res = 0;
187                         break;
188                 }
189         }
190         AST_LIST_TRAVERSE_SAFE_END;
191
192         return res;
193 }
194
195 int ast_framehook_list_destroy(struct ast_channel *chan)
196 {
197         struct ast_framehook *framehook;
198
199         if (!ast_channel_framehooks(chan)) {
200                 return 0;
201         }
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);
205         }
206         AST_LIST_TRAVERSE_SAFE_END;
207         ast_free(ast_channel_framehooks(chan));
208         ast_channel_framehooks_set(chan, NULL);
209         return 0;
210 }
211
212 int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
213 {
214         if (!framehooks) {
215                 return 1;
216         }
217         return AST_LIST_EMPTY(&framehooks->list) ? 1 : 0;
218 }
219
220 int ast_framehook_list_contains_no_active(struct ast_framehook_list *framehooks)
221 {
222         return ast_framehook_list_contains_no_active_of_type(framehooks, 0);
223 }
224
225 int ast_framehook_list_contains_no_active_of_type(struct ast_framehook_list *framehooks,
226         enum ast_frame_type type)
227 {
228         struct ast_framehook *cur;
229
230         if (!framehooks) {
231                 return 1;
232         }
233
234         if (AST_LIST_EMPTY(&framehooks->list)) {
235                 return 1;
236         }
237
238         AST_LIST_TRAVERSE(&framehooks->list, cur, list) {
239                 if (cur->detach_and_destroy_me) {
240                         continue;
241                 }
242                 if (type && cur->i.consume_cb && !cur->i.consume_cb(cur->i.data, type)) {
243                         continue;
244                 }
245                 return 0;
246         }
247
248         return 1;
249 }
250
251 struct ast_frame *ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
252 {
253         return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_WRITE);
254 }
255
256 struct ast_frame *ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
257 {
258         return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_READ);
259 }