Merged revisions 287647 via svnmerge from
[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 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include "asterisk/channel.h"
31 #include "asterisk/linkedlists.h"
32 #include "asterisk/framehook.h"
33 #include "asterisk/frame.h"
34
35 struct ast_framehook {
36         struct ast_framehook_interface i;
37         /*! This pointer to ast_channel the framehook is attached to. */
38         struct ast_channel *chan;
39         /*! the id representing this framehook on a channel */
40         unsigned int id;
41         /*! when set, this signals the read and write function to detach the hook */
42         int detach_and_destroy_me;
43         /*! list entry for ast_framehook_list object */
44         AST_LIST_ENTRY(ast_framehook) list;
45 };
46
47 struct ast_framehook_list {
48         unsigned int id_count;
49         AST_LIST_HEAD_NOLOCK(, ast_framehook) list;
50 };
51
52 static void framehook_detach_and_destroy(struct ast_framehook *framehook)
53 {
54         struct ast_frame *frame;
55         frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_DETACHED, framehook->i.data);
56         /* never assume anything about this function. If you can return a frame during
57          * the detached event, then assume someone will. */
58         if (frame) {
59                 ast_frfree(frame);
60         }
61         framehook->chan = NULL;
62
63         if (framehook->i.destroy_cb) {
64                 framehook->i.destroy_cb(framehook->i.data);
65         }
66         ast_free(framehook);
67 }
68
69 static struct ast_frame *framehook_list_push_event(struct ast_framehook_list *framehooks, struct ast_frame *frame, enum ast_framehook_event event)
70 {
71         struct ast_framehook *framehook;
72
73         if (!framehooks) {
74                 return frame;
75         }
76
77         AST_LIST_TRAVERSE_SAFE_BEGIN(&framehooks->list, framehook, list) {
78                 if (framehook->detach_and_destroy_me) {
79                         /* this guy is signaled for destruction */
80                         AST_LIST_REMOVE_CURRENT(list);
81                         framehook_detach_and_destroy(framehook);
82                 } else {
83                         frame = framehook->i.event_cb(framehook->chan, frame, event, framehook->i.data);
84                 }
85         }
86         AST_LIST_TRAVERSE_SAFE_END;
87         return frame;
88 }
89
90 int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i)
91 {
92         struct ast_framehook *framehook;
93         struct ast_frame *frame;
94         if (i->version != AST_FRAMEHOOK_INTERFACE_VERSION) {
95                 ast_log(LOG_ERROR, "Version '%hu' of framehook interface not what we compiled against (%hu)\n",
96                         i->version, AST_FRAMEHOOK_INTERFACE_VERSION);
97                 return -1;
98         }
99         if (!i->event_cb || !(framehook = ast_calloc(1, sizeof(*framehook)))) {
100                 return -1;
101         }
102         framehook->i = *i;
103         framehook->chan = chan;
104
105         /* create the framehook list if it didn't already exist */
106         if (!chan->framehooks && !(chan->framehooks = ast_calloc(1, sizeof(*chan->framehooks)))) {
107                 ast_free(framehook);
108                 return -1;
109         }
110
111         framehook->id = ++chan->framehooks->id_count;
112         AST_LIST_INSERT_TAIL(&chan->framehooks->list, framehook, list);
113
114         /* Tell the event callback we're live and rocking */
115         frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_ATTACHED, framehook->i.data);
116
117         /* Never assume anything about this function. If you can return a frame during
118          * the attached event, then assume someone will. */
119         if (frame) {
120                 ast_frfree(frame);
121         }
122
123         return framehook->id;
124 }
125
126 int ast_framehook_detach(struct ast_channel *chan, int id)
127 {
128         struct ast_framehook *framehook;
129         int res = -1;
130
131         if (!chan->framehooks) {
132                 return res;
133         }
134
135         AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->framehooks->list, framehook, list) {
136                 if (framehook->id == id) {
137                         /* we mark for detachment rather than doing explicitly here because
138                          * it needs to be safe for this function to be called within the
139                          * event callback.  If we allowed the hook to actually be destroyed
140                          * immediately here, the event callback would crash on exit. */
141                         framehook->detach_and_destroy_me = 1;
142                         res = 0;
143                         break;
144                 }
145         }
146         AST_LIST_TRAVERSE_SAFE_END;
147
148         return res;
149 }
150
151 int ast_framehook_list_destroy(struct ast_channel *chan)
152 {
153         struct ast_framehook *framehook;
154
155         if (!chan->framehooks) {
156                 return 0;
157         }
158         AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->framehooks->list, framehook, list) {
159                 AST_LIST_REMOVE_CURRENT(list);
160                 framehook_detach_and_destroy(framehook);
161         }
162         AST_LIST_TRAVERSE_SAFE_END;
163         ast_free(chan->framehooks);
164         chan->framehooks = NULL;
165         return 0;
166 }
167
168 int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
169 {
170         if (!framehooks) {
171                 return 1;
172         }
173         return AST_LIST_EMPTY(&framehooks->list) ? 1 : 0;
174 }
175
176 struct ast_frame *ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
177 {
178         return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_WRITE);
179 }
180
181 struct ast_frame *ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
182 {
183         return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_READ);
184 }