93542812f321993d5dfa8fc8328963b329588588
[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         ao2_cleanup(bridge);
89         return res;
90 }
91
92 static void bridge_stasis_queue_join_action(struct ast_bridge *self,
93         struct ast_bridge_channel *bridge_channel)
94 {
95         ast_channel_lock(bridge_channel->chan);
96         if (command_prestart_queue_command(bridge_channel->chan, add_channel_to_bridge, ao2_bump(self))) {
97                 ao2_cleanup(self);
98         }
99         ast_channel_unlock(bridge_channel->chan);
100 }
101
102 /*!
103  * \internal
104  * \brief Push this channel into the Stasis bridge.
105  * \since 12.5.0
106  *
107  * \param self Bridge to operate upon.
108  * \param bridge_channel Bridge channel to push.
109  * \param swap Bridge channel to swap places with if not NULL.
110  *
111  * \note On entry, self is already locked.
112  *
113  * \retval 0 on success.
114  * \retval -1 on failure.  The channel did not get pushed.
115  */
116 static int bridge_stasis_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
117 {
118         struct stasis_app_control *control = stasis_app_control_find_by_channel(bridge_channel->chan);
119
120         if (!control && !stasis_app_channel_is_internal(bridge_channel->chan)) {
121                 /* channel not in Stasis(), get it there */
122                 /* Attach after-bridge callback and pass ownership of swap_app to it */
123                 if (ast_bridge_set_after_callback(bridge_channel->chan,
124                         bridge_stasis_run_cb, NULL, NULL)) {
125                         ast_log(LOG_ERROR, "Failed to set after bridge callback\n");
126                         return -1;
127                 }
128
129                 bridge_stasis_queue_join_action(self, bridge_channel);
130
131                 /* Return -1 so the push fails and the after-bridge callback gets called */
132                 return -1;
133         }
134
135         ao2_cleanup(control);
136         if (self->allowed_capabilities & STASIS_BRIDGE_MIXING_CAPABILITIES) {
137                 ast_bridge_channel_update_linkedids(bridge_channel, swap);
138                 if (ast_test_flag(&self->feature_flags, AST_BRIDGE_FLAG_SMART)) {
139                         ast_bridge_channel_update_accountcodes(bridge_channel, swap);
140                 }
141         }
142
143         return ast_bridge_base_v_table.push(self, bridge_channel, swap);
144 }
145
146 static int bridge_stasis_moving(struct ast_bridge_channel *bridge_channel, void *hook_pvt,
147                 struct ast_bridge *src, struct ast_bridge *dst)
148 {
149         if (src->v_table == &bridge_stasis_v_table &&
150                         dst->v_table != &bridge_stasis_v_table) {
151                 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
152                 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
153                 struct ast_channel *chan;
154
155                 chan = bridge_channel->chan;
156                 ast_assert(chan != NULL);
157
158                 control = stasis_app_control_find_by_channel(chan);
159                 if (!control) {
160                         return -1;
161                 }
162
163                 blob = ast_json_pack("{s: s}", "app", app_name(control_app(control)));
164
165                 stasis_app_channel_set_stasis_end_published(chan);
166
167                 ast_channel_publish_blob(chan, ast_stasis_end_message_type(), blob);
168         }
169
170         return -1;
171 }
172
173 /*!
174  * \internal
175  * \brief Pull this channel from the Stasis bridge.
176  * \since 12.5.0
177  *
178  * \param self Bridge to operate upon.
179  * \param bridge_channel Bridge channel to pull.
180  *
181  * \note On entry, self is already locked.
182  *
183  * \return Nothing
184  */
185 static void bridge_stasis_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
186 {
187         if ((self->allowed_capabilities & STASIS_BRIDGE_MIXING_CAPABILITIES)
188                 && ast_test_flag(&self->feature_flags, AST_BRIDGE_FLAG_SMART)) {
189                 ast_bridge_channel_update_accountcodes(NULL, bridge_channel);
190         }
191
192         ast_bridge_move_hook(bridge_channel->features, bridge_stasis_moving, NULL, NULL, 0);
193
194         ast_bridge_base_v_table.pull(self, bridge_channel);
195 }
196
197 struct ast_bridge *bridge_stasis_new(uint32_t capabilities, unsigned int flags, const char *name, const char *id)
198 {
199         void *bridge;
200
201         bridge = bridge_alloc(sizeof(struct ast_bridge), &bridge_stasis_v_table);
202         bridge = bridge_base_init(bridge, capabilities, flags, "Stasis", name, id);
203         bridge = bridge_register(bridge);
204
205         return bridge;
206 }
207
208 void bridge_stasis_init(void)
209 {
210         /* Setup the Stasis bridge subclass v_table. */
211         bridge_stasis_v_table = ast_bridge_base_v_table;
212         bridge_stasis_v_table.name = "stasis";
213         bridge_stasis_v_table.push = bridge_stasis_push;
214         bridge_stasis_v_table.pull = bridge_stasis_pull;
215 }