Debian Codegraph @

res/stasis/stasis_bridge.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2014, Digium, Inc.
5 *
6 * Richard Mudgett <[email protected]>
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 <[email protected]>
24 *
25 * See Also:
26 * \arg \ref AstCREDITS
27 */
28
29
30 #include "asterisk.h"
31
32 #include "asterisk/bridge.h"
33 #include "asterisk/bridge_after.h"
34 #include "asterisk/bridge_internal.h"
35 #include "asterisk/bridge_features.h"
36 #include "asterisk/stasis_app.h"
37 #include "asterisk/stasis_channels.h"
38 #include "stasis_bridge.h"
39 #include "control.h"
40 #include "command.h"
41 #include "app.h"
42 #include "asterisk/stasis_app.h"
43 #include "asterisk/pbx.h"
44
45 /* ------------------------------------------------------------------- */
46
47 static struct ast_bridge_methods bridge_stasis_v_table;
48
49 static void bridge_stasis_run_cb(struct ast_channel *chan, void *data)
50 {
51 RAII_VAR(char *, app_name, NULL, ast_free);
52 struct ast_app *app_stasis;
53
54 /* Take ownership of the swap_app memory from the datastore */
55 app_name = app_get_replace_channel_app(chan);
56 if (!app_name) {
57 ast_log(LOG_ERROR, "Failed to get app name for %s (%p)\n", ast_channel_name(chan), chan);
58 return;
59 }
60
61 /* find Stasis() */
62 app_stasis = pbx_findapp("Stasis");
63 if (!app_stasis) {
64 ast_log(LOG_WARNING, "Could not find application (Stasis)\n");
65 return;
66 }
67
68 if (ast_check_hangup_locked(chan)) {
69 /* channel hungup, don't run Stasis() */
70 return;
71 }
72
73 /* run Stasis() */
74 pbx_exec(chan, app_stasis, app_name);
75 }
76
77 struct defer_bridge_add_obj {
78 /*! Bridge to join (has ref) */
79 struct ast_bridge *bridge;
80 /*!
81 * \brief Channel to swap with in the bridge. (has ref)
82 *
83 * \note NULL if not swapping with a channel.
84 */
85 struct ast_channel *swap;
86 };
87
88 static void defer_bridge_add_dtor(void *obj)
89 {
90 struct defer_bridge_add_obj *defer = obj;
91
92 ao2_cleanup(defer->bridge);
93 ast_channel_cleanup(defer->swap);
94 }
95
96 static int defer_bridge_add(
97 struct stasis_app_control *control,
98 struct ast_channel *chan, void *obj)
99 {
100 struct defer_bridge_add_obj *defer = obj;
101
102 return control_swap_channel_in_bridge(control, defer->bridge, chan, defer->swap);
103 }
104
105 static void bridge_stasis_queue_join_action(struct ast_bridge *self,
106 struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
107 {
108 struct defer_bridge_add_obj *defer;
109
110 defer = ao2_alloc_options(sizeof(*defer), defer_bridge_add_dtor,
111 AO2_ALLOC_OPT_LOCK_NOLOCK);
112 if (!defer) {
113 return;
114 }
115 ao2_ref(self, +1);
116 defer->bridge = self;
117 if (swap) {
118 ast_channel_ref(swap->chan);
119 defer->swap = swap->chan;
120 }
121
122 ast_channel_lock(bridge_channel->chan);
123 command_prestart_queue_command(bridge_channel->chan, defer_bridge_add,
124 defer, __ao2_cleanup);
125 ast_channel_unlock(bridge_channel->chan);
126 }
127
128 /*!
129 * \internal
130 * \brief Peek at channel before it is pushed into bridge
131 * \since 13.2.0
132 *
133 * \param self Bridge to operate upon.
134 * \param bridge_channel Bridge channel to push.
135 * \param swap Bridge channel to swap places with if not NULL.
136 *
137 * \note On entry, self is already locked.
138 *
139 * \retval 0 on success.
140 * \retval -1 on failure. The channel should not be pushed.
141 */
142 static int bridge_stasis_push_peek(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
143 {
144 struct stasis_app_control *swap_control;
145 struct ast_channel_snapshot *to_be_replaced;
146
147 if (!swap) {
148 goto done;
149 }
150
151 swap_control = stasis_app_control_find_by_channel(swap->chan);
152 if (!swap_control) {
153 ast_log(LOG_ERROR,"Failed to find stasis app control for swapped channel %s\n", ast_channel_name(swap->chan));
154 return -1;
155 }
156 to_be_replaced = ast_channel_snapshot_get_latest(ast_channel_uniqueid(swap->chan));
157
158 ast_debug(3, "Copying stasis app name %s from %s to %s\n", stasis_app_name(control_app(swap_control)),
159 ast_channel_name(swap->chan), ast_channel_name(bridge_channel->chan));
160
161 ast_channel_lock(bridge_channel->chan);
162
163 /* copy the app name from the swap channel */
164 app_set_replace_channel_app(bridge_channel->chan, stasis_app_name(control_app(swap_control)));
165
166 /* set the replace channel snapshot */
167 app_set_replace_channel_snapshot(bridge_channel->chan, to_be_replaced);
168
169 ast_channel_unlock(bridge_channel->chan);
170
171 ao2_ref(swap_control, -1);
172 ao2_cleanup(to_be_replaced);
173
174 done:
175 return ast_bridge_base_v_table.push_peek(self, bridge_channel, swap);
176 }
177
178 /*!
179 * \internal
180 * \brief Push this channel into the Stasis bridge.
181 * \since 12.5.0
182 *
183 * \param self Bridge to operate upon.
184 * \param bridge_channel Bridge channel to push.
185 * \param swap Bridge channel to swap places with if not NULL.
186 *
187 * \note On entry, self is already locked.
188 *
189 * \retval 0 on success.
190 * \retval -1 on failure. The channel did not get pushed.
191 */
192 static int bridge_stasis_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
193 {
194 struct stasis_app_control *control = stasis_app_control_find_by_channel(bridge_channel->chan);
195
196 if (!control && !stasis_app_channel_is_internal(bridge_channel->chan)) {
197 /* channel not in Stasis(), get it there */
198 ast_debug(1, "Bridge %s: pushing non-stasis %p(%s) setup to come back in under stasis\n",
199 self->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
200
201 /* Attach after-bridge callback and pass ownership of swap_app to it */
202 if (ast_bridge_set_after_callback(bridge_channel->chan,
203 bridge_stasis_run_cb, NULL, NULL)) {
204 ast_log(LOG_ERROR,
205 "Failed to set after bridge callback for bridge %s non-stasis push of %s\n",
206 self->uniqueid, ast_channel_name(bridge_channel->chan));
207 return -1;
208 }
209
210 bridge_stasis_queue_join_action(self, bridge_channel, swap);
211
212 /* Return -1 so the push fails and the after-bridge callback gets called
213 * This keeps the bridging framework from putting the channel into the bridge
214 * until the Stasis thread gets started, and then the channel is put into the bridge.
215 */
216 return -1;
217 }
218 ao2_cleanup(control);
219
220 /*
221 * If going into a holding bridge, default the role to participant, if
222 * it has no compatible role currently
223 */
224 if ((self->technology->capabilities & AST_BRIDGE_CAPABILITY_HOLDING)
225 && !ast_channel_has_role(bridge_channel->chan, "announcer")
226 && !ast_channel_has_role(bridge_channel->chan, "holding_participant")) {
227 if (ast_channel_add_bridge_role(bridge_channel->chan, "holding_participant")) {
228 ast_log(LOG_ERROR, "Failed to set holding participant on %s\n", ast_channel_name(bridge_channel->chan));
229 return -1;
230 }
231
232 if (ast_channel_set_bridge_role_option(bridge_channel->chan, "holding_participant", "idle_mode", "none")) {
233 ast_log(LOG_ERROR, "Failed to set holding participant mode on %s\n", ast_channel_name(bridge_channel->chan));
234 return -1;
235 }
236 }
237
238 if (self->allowed_capabilities & STASIS_BRIDGE_MIXING_CAPABILITIES) {
239 ast_bridge_channel_update_linkedids(bridge_channel, swap);
240 if (ast_test_flag(&self->feature_flags, AST_BRIDGE_FLAG_SMART)) {
241 ast_bridge_channel_update_accountcodes(bridge_channel, swap);
242 }
243 }
244
245 return ast_bridge_base_v_table.push(self, bridge_channel, swap);
246 }
247
248 static int bridge_stasis_moving(struct ast_bridge_channel *bridge_channel, void *hook_pvt,
249 struct ast_bridge *src, struct ast_bridge *dst)
250 {
251 if (src->v_table == &bridge_stasis_v_table &&
252 dst->v_table != &bridge_stasis_v_table) {
253 struct stasis_app_control *control;
254 struct ast_channel *chan;
255
256 chan = bridge_channel->chan;
257 ast_assert(chan != NULL);
258
259 control = stasis_app_control_find_by_channel(chan);
260 if (!control) {
261 return -1;
262 }
263
264 stasis_app_channel_set_stasis_end_published(chan);
265 app_send_end_msg(control_app(control), chan);
266 ao2_ref(control, -1);
267 }
268
269 return -1;
270 }
271
272 /*!
273 * \internal
274 * \brief Pull this channel from the Stasis bridge.
275 * \since 12.5.0
276 *
277 * \param self Bridge to operate upon.
278 * \param bridge_channel Bridge channel to pull.
279 *
280 * \note On entry, self is already locked.
281 */
282 static void bridge_stasis_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
283 {
284 if ((self->allowed_capabilities & STASIS_BRIDGE_MIXING_CAPABILITIES)
285 && ast_test_flag(&self->feature_flags, AST_BRIDGE_FLAG_SMART)) {
286 ast_bridge_channel_update_accountcodes(NULL, bridge_channel);
287 }
288
289 if (self->technology->capabilities & AST_BRIDGE_CAPABILITY_HOLDING) {
290 ast_channel_clear_bridge_roles(bridge_channel->chan);
291 }
292
293 ast_bridge_move_hook(bridge_channel->features, bridge_stasis_moving, NULL, NULL, 0);
294
295 ast_bridge_base_v_table.pull(self, bridge_channel);
296 }
297
298 struct ast_bridge *bridge_stasis_new(uint32_t capabilities, unsigned int flags, const char *name, const char *id, enum ast_bridge_video_mode_type video_mode, unsigned int send_sdp_label)
299 {
300 struct ast_bridge *bridge;
301
302 ast_debug(1, "Creating Stasis bridge '%s' with id '%s'\n",
303 S_OR(name, "<unknown>"), S_OR(id, "<unknown>"));
304 bridge = bridge_alloc(sizeof(struct ast_bridge), &bridge_stasis_v_table);
305 bridge = bridge_base_init(bridge, capabilities, flags, "Stasis", name, id);
306 if (!bridge) {
307 return NULL;
308 }
309
310 if (video_mode == AST_BRIDGE_VIDEO_MODE_SFU) {
311 ast_bridge_set_sfu_video_mode(bridge);
312 /* We require a minimum 5 seconds between video updates to stop floods from clients,
313 * this should rarely be changed but should become configurable in the future.
314 */
315 ast_bridge_set_video_update_discard(bridge, 5);
316 } else if (video_mode == AST_BRIDGE_VIDEO_MODE_SINGLE_SRC) {
317 ast_bridge_set_single_src_video_mode(bridge, NULL);
318 } else {
319 ast_bridge_set_talker_src_video_mode(bridge);
320 }
321
322 if (send_sdp_label) {
323 ast_bridge_set_send_sdp_label(bridge, 1);
324 }
325
326 bridge = bridge_register(bridge);
327 if (!bridge) {
328 ast_log(LOG_ERROR, "Failed to register Stasis bridge %s(%s)\n",
329 S_OR(id, "<unknown>"), S_OR(name, "<unknown>"));
330 return NULL;
331 }
332 ast_debug(1, "Created Stasis bridge " BRIDGE_PRINTF_SPEC "\n",
333 BRIDGE_PRINTF_VARS(bridge));
334
335 return bridge;
336 }
337
338 void bridge_stasis_init(void)
339 {
340 /* Setup the Stasis bridge subclass v_table. */
341 bridge_stasis_v_table = ast_bridge_base_v_table;
342 bridge_stasis_v_table.name = "stasis";
343 bridge_stasis_v_table.push = bridge_stasis_push;
344 bridge_stasis_v_table.pull = bridge_stasis_pull;
345 bridge_stasis_v_table.push_peek = bridge_stasis_push_peek;
346 }