ARI: Fix a crash caused by hanging during playback to a channel in a bridge
[asterisk/asterisk.git] / res / stasis / stasis_bridge.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Digium, Inc.
5  *
6  * Richard Mudgett <rmudgett@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 /*!
20  * \file
21  * \brief Stasis bridge subclass.
22  *
23  * \author Richard Mudgett <rmudgett@digium.com>
24  *
25  * See Also:
26  * \arg \ref AstCREDITS
27  */
28
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/bridge.h"
35 #include "asterisk/bridge_after.h"
36 #include "asterisk/bridge_internal.h"
37 #include "asterisk/bridge_features.h"
38 #include "asterisk/stasis_app.h"
39 #include "asterisk/stasis_channels.h"
40 #include "stasis_bridge.h"
41 #include "control.h"
42 #include "command.h"
43 #include "app.h"
44 #include "asterisk/stasis_app.h"
45 #include "asterisk/pbx.h"
46
47 /* ------------------------------------------------------------------- */
48
49 static struct ast_bridge_methods bridge_stasis_v_table;
50
51 static void bridge_stasis_run_cb(struct ast_channel *chan, void *data)
52 {
53         RAII_VAR(char *, app_name, NULL, ast_free);
54         struct ast_app *app_stasis;
55
56         /* Take ownership of the swap_app memory from the datastore */
57         app_name = app_get_replace_channel_app(chan);
58         if (!app_name) {
59                 ast_log(LOG_ERROR, "Failed to get app name for %s (%p)\n", ast_channel_name(chan), chan);
60                 return;
61         }
62
63         /* find Stasis() */
64         app_stasis = pbx_findapp("Stasis");
65         if (!app_stasis) {
66                 ast_log(LOG_WARNING, "Could not find application (Stasis)\n");
67                 return;
68         }
69
70         if (ast_check_hangup_locked(chan)) {
71                 /* channel hungup, don't run Stasis() */
72                 return;
73         }
74
75         /* run Stasis() */
76         pbx_exec(chan, app_stasis, app_name);
77 }
78
79 static int add_channel_to_bridge(
80         struct stasis_app_control *control,
81         struct ast_channel *chan, void *obj)
82 {
83         struct ast_bridge *bridge = obj;
84         int res;
85
86         res = control_add_channel_to_bridge(control,
87                 chan, bridge);
88         return res;
89 }
90
91 static void bridge_stasis_queue_join_action(struct ast_bridge *self,
92         struct ast_bridge_channel *bridge_channel)
93 {
94         ast_channel_lock(bridge_channel->chan);
95         command_prestart_queue_command(bridge_channel->chan, add_channel_to_bridge,
96                 ao2_bump(self), __ao2_cleanup);
97         ast_channel_unlock(bridge_channel->chan);
98 }
99
100 /*!
101  * \internal
102  * \brief Push this channel into the Stasis bridge.
103  * \since 12.5.0
104  *
105  * \param self Bridge to operate upon.
106  * \param bridge_channel Bridge channel to push.
107  * \param swap Bridge channel to swap places with if not NULL.
108  *
109  * \note On entry, self is already locked.
110  *
111  * \retval 0 on success.
112  * \retval -1 on failure.  The channel did not get pushed.
113  */
114 static int bridge_stasis_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
115 {
116         struct stasis_app_control *control = stasis_app_control_find_by_channel(bridge_channel->chan);
117
118         if (!control && !stasis_app_channel_is_internal(bridge_channel->chan)) {
119                 /* channel not in Stasis(), get it there */
120                 /* Attach after-bridge callback and pass ownership of swap_app to it */
121                 if (ast_bridge_set_after_callback(bridge_channel->chan,
122                         bridge_stasis_run_cb, NULL, NULL)) {
123                         ast_log(LOG_ERROR, "Failed to set after bridge callback\n");
124                         return -1;
125                 }
126
127                 bridge_stasis_queue_join_action(self, bridge_channel);
128
129                 /* Return -1 so the push fails and the after-bridge callback gets called */
130                 return -1;
131         }
132
133         ao2_cleanup(control);
134         if (self->allowed_capabilities & STASIS_BRIDGE_MIXING_CAPABILITIES) {
135                 ast_bridge_channel_update_linkedids(bridge_channel, swap);
136                 if (ast_test_flag(&self->feature_flags, AST_BRIDGE_FLAG_SMART)) {
137                         ast_bridge_channel_update_accountcodes(bridge_channel, swap);
138                 }
139         }
140
141         return ast_bridge_base_v_table.push(self, bridge_channel, swap);
142 }
143
144 static int bridge_stasis_moving(struct ast_bridge_channel *bridge_channel, void *hook_pvt,
145                 struct ast_bridge *src, struct ast_bridge *dst)
146 {
147         if (src->v_table == &bridge_stasis_v_table &&
148                         dst->v_table != &bridge_stasis_v_table) {
149                 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
150                 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
151                 struct ast_channel *chan;
152
153                 chan = bridge_channel->chan;
154                 ast_assert(chan != NULL);
155
156                 control = stasis_app_control_find_by_channel(chan);
157                 if (!control) {
158                         return -1;
159                 }
160
161                 blob = ast_json_pack("{s: s}", "app", app_name(control_app(control)));
162
163                 stasis_app_channel_set_stasis_end_published(chan);
164
165                 ast_channel_publish_blob(chan, ast_stasis_end_message_type(), blob);
166         }
167
168         return -1;
169 }
170
171 /*!
172  * \internal
173  * \brief Pull this channel from the Stasis bridge.
174  * \since 12.5.0
175  *
176  * \param self Bridge to operate upon.
177  * \param bridge_channel Bridge channel to pull.
178  *
179  * \note On entry, self is already locked.
180  *
181  * \return Nothing
182  */
183 static void bridge_stasis_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
184 {
185         if ((self->allowed_capabilities & STASIS_BRIDGE_MIXING_CAPABILITIES)
186                 && ast_test_flag(&self->feature_flags, AST_BRIDGE_FLAG_SMART)) {
187                 ast_bridge_channel_update_accountcodes(NULL, bridge_channel);
188         }
189
190         ast_bridge_move_hook(bridge_channel->features, bridge_stasis_moving, NULL, NULL, 0);
191
192         ast_bridge_base_v_table.pull(self, bridge_channel);
193 }
194
195 struct ast_bridge *bridge_stasis_new(uint32_t capabilities, unsigned int flags, const char *name, const char *id)
196 {
197         void *bridge;
198
199         bridge = bridge_alloc(sizeof(struct ast_bridge), &bridge_stasis_v_table);
200         bridge = bridge_base_init(bridge, capabilities, flags, "Stasis", name, id);
201         bridge = bridge_register(bridge);
202
203         return bridge;
204 }
205
206 void bridge_stasis_init(void)
207 {
208         /* Setup the Stasis bridge subclass v_table. */
209         bridge_stasis_v_table = ast_bridge_base_v_table;
210         bridge_stasis_v_table.name = "stasis";
211         bridge_stasis_v_table.push = bridge_stasis_push;
212         bridge_stasis_v_table.pull = bridge_stasis_pull;
213 }