Asterisk - The Open Source Telephony Project GIT-master-3dae2cf
stasis_bridge.c
Go to the documentation of this file.
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#include "asterisk/bridge.h"
36#include "asterisk/stasis_app.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
48
49static 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 */
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
78 /*! Bridge to join (has ref) */
80 /*!
81 * \brief Channel to swap with in the bridge. (has ref)
82 *
83 * \note NULL if not swapping with a channel.
84 */
86};
87
88static void defer_bridge_add_dtor(void *obj)
89{
90 struct defer_bridge_add_obj *defer = obj;
91
92 ao2_cleanup(defer->bridge);
94}
95
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
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,
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);
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 */
142static 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 }
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
174done:
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 */
192static 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,
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 */
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
239 ast_bridge_channel_update_linkedids(bridge_channel, swap);
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
248static 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 &&
253 struct stasis_app_control *control;
254 struct ast_channel *chan;
255
256 chan = bridge_channel->chan;
257 ast_assert(chan != NULL);
258
260 if (!control) {
261 return -1;
262 }
263
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 */
283{
287 }
288
291 }
292
294
296}
297
298struct 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 void *bridge;
301
302 bridge = bridge_alloc(sizeof(struct ast_bridge), &bridge_stasis_v_table);
303 bridge = bridge_base_init(bridge, capabilities, flags, "Stasis", name, id);
304 if (!bridge) {
305 return NULL;
306 }
307
308 if (video_mode == AST_BRIDGE_VIDEO_MODE_SFU) {
310 /* We require a minimum 5 seconds between video updates to stop floods from clients,
311 * this should rarely be changed but should become configurable in the future.
312 */
314 } else if (video_mode == AST_BRIDGE_VIDEO_MODE_SINGLE_SRC) {
316 } else {
318 }
319
320 if (send_sdp_label) {
322 }
323
324 bridge = bridge_register(bridge);
325
326 return bridge;
327}
328
330{
331 /* Setup the Stasis bridge subclass v_table. */
333 bridge_stasis_v_table.name = "stasis";
337}
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_log
Definition: astobj2.c:42
@ AO2_ALLOC_OPT_LOCK_NOLOCK
Definition: astobj2.h:367
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:404
void __ao2_cleanup(void *obj)
Definition: astobj2.c:677
Bridging API.
void ast_bridge_set_talker_src_video_mode(struct ast_bridge *bridge)
Set the bridge to pick the strongest talker supporting video as the single source video feed.
Definition: bridge.c:3766
void ast_bridge_set_sfu_video_mode(struct ast_bridge *bridge)
Set the bridge to be a selective forwarding unit.
Definition: bridge.c:3774
void ast_bridge_set_video_update_discard(struct ast_bridge *bridge, unsigned int video_update_discard)
Set the amount of time to discard subsequent video updates after a video update has been sent.
Definition: bridge.c:3782
struct ast_bridge_methods ast_bridge_base_v_table
Bridge base class virtual method table.
Definition: bridge.c:923
@ AST_BRIDGE_CAPABILITY_HOLDING
Definition: bridge.h:86
void ast_bridge_set_single_src_video_mode(struct ast_bridge *bridge, struct ast_channel *video_src_chan)
Set a bridge to feed a single video source to all participants.
Definition: bridge.c:3749
void ast_bridge_set_send_sdp_label(struct ast_bridge *bridge, unsigned int send_sdp_label)
Controls whether to send a "label" attribute in each stream in an SDP.
Definition: bridge.c:3966
ast_bridge_video_mode_type
Video source modes.
Definition: bridge.h:98
@ AST_BRIDGE_VIDEO_MODE_SINGLE_SRC
Definition: bridge.h:102
@ AST_BRIDGE_VIDEO_MODE_SFU
Definition: bridge.h:109
After Bridge Execution API.
int ast_bridge_set_after_callback(struct ast_channel *chan, ast_bridge_after_cb callback, ast_bridge_after_cb_failed failed, void *data)
Setup an after bridge callback for when the channel leaves the bridging system.
Definition: bridge_after.c:251
void ast_bridge_channel_update_linkedids(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
void ast_bridge_channel_update_accountcodes(struct ast_bridge_channel *joining, struct ast_bridge_channel *leaving)
Channel Bridging API.
int ast_bridge_move_hook(struct ast_bridge_features *features, ast_bridge_move_indicate_callback callback, void *hook_pvt, ast_bridge_hook_pvt_destructor destructor, enum ast_bridge_hook_remove_flags remove_flags)
Attach a bridge channel move detection hook to a bridge features structure.
Definition: bridge.c:3307
@ AST_BRIDGE_FLAG_SMART
Private Bridging API.
struct ast_bridge * bridge_register(struct ast_bridge *bridge)
Register the new bridge with the system.
Definition: bridge.c:691
struct ast_bridge * bridge_base_init(struct ast_bridge *self, uint32_t capabilities, unsigned int flags, const char *creator, const char *name, const char *id)
Initialize the base class of the bridge.
Definition: bridge.c:742
struct ast_bridge * bridge_alloc(size_t size, const struct ast_bridge_methods *v_table)
Definition: bridge.c:706
int ast_channel_set_bridge_role_option(struct ast_channel *channel, const char *role_name, const char *option, const char *value)
Set a role option on a channel.
Definition: bridge_roles.c:375
int ast_channel_has_role(struct ast_channel *channel, const char *role_name)
Check if a role exists on a channel.
Definition: bridge_roles.c:394
int ast_channel_add_bridge_role(struct ast_channel *chan, const char *role_name)
Adds a bridge role to a channel.
Definition: bridge_roles.c:313
void ast_channel_clear_bridge_roles(struct ast_channel *chan)
Removes all bridge roles currently on a channel.
Definition: bridge_roles.c:356
const char * ast_channel_name(const struct ast_channel *chan)
#define ast_channel_lock(chan)
Definition: channel.h:2968
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2993
const char * ast_channel_uniqueid(const struct ast_channel *chan)
int ast_check_hangup_locked(struct ast_channel *chan)
Definition: channel.c:459
#define ast_channel_cleanup(c)
Cleanup a channel reference.
Definition: channel.h:3015
#define ast_channel_unlock(chan)
Definition: channel.h:2969
int command_prestart_queue_command(struct ast_channel *chan, stasis_app_command_cb command_fn, void *data, command_data_destructor_fn data_destructor)
Queue a Stasis() prestart command for a channel.
Definition: command.c:123
Internal API for the Stasis application commands.
int control_swap_channel_in_bridge(struct stasis_app_control *control, struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap)
Command for swapping a channel in a bridge.
Definition: control.c:1293
struct stasis_app * control_app(struct stasis_app_control *control)
Returns the pointer (non-reffed) to the app associated with this control.
Definition: control.c:1572
Internal API for the Stasis application controller.
static const char name[]
Definition: format_mp3.c:68
struct ast_channel_snapshot * ast_channel_snapshot_get_latest(const char *uniqueid)
Obtain the latest ast_channel_snapshot from the Stasis Message Bus API cache. This is an ao2 object,...
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_WARNING
Core PBX routines and definitions.
int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data)
Execute an application.
Definition: pbx_app.c:471
struct ast_app * pbx_findapp(const char *app)
Look up an application.
Definition: ael_main.c:165
const char * app_name(struct ast_app *app)
Definition: pbx_app.c:463
Internal API for the Stasis application controller.
int app_set_replace_channel_app(struct ast_channel *chan, const char *replace_app)
Set the app that the replacement channel will be controlled by.
Definition: res_stasis.c:934
int app_set_replace_channel_snapshot(struct ast_channel *chan, struct ast_channel_snapshot *replace_snapshot)
Set the snapshot of the channel that this channel will replace.
Definition: res_stasis.c:922
int app_send_end_msg(struct stasis_app *app, struct ast_channel *chan)
Send StasisEnd message to the listening app.
Definition: res_stasis.c:1086
char * app_get_replace_channel_app(struct ast_channel *chan)
Get the app that the replacement channel will be controlled by.
Definition: res_stasis.c:970
#define NULL
Definition: resample.c:96
Stasis Application API. See Stasis Application API for detailed documentation.
int stasis_app_channel_is_internal(struct ast_channel *chan)
Is this channel internal to Stasis?
Definition: res_stasis.c:2325
void stasis_app_channel_set_stasis_end_published(struct ast_channel *chan)
Indicate that this channel has had a StasisEnd published for it.
Definition: res_stasis.c:1290
const char * stasis_app_name(const struct stasis_app *app)
Retrieve an application's name.
struct stasis_app_control * stasis_app_control_find_by_channel(const struct ast_channel *chan)
Returns the handler for the given channel.
Definition: res_stasis.c:338
static void defer_bridge_add_dtor(void *obj)
Definition: stasis_bridge.c:88
static void bridge_stasis_queue_join_action(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
static void bridge_stasis_run_cb(struct ast_channel *chan, void *data)
Definition: stasis_bridge.c:49
static void bridge_stasis_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
static int bridge_stasis_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
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)
static int bridge_stasis_moving(struct ast_bridge_channel *bridge_channel, void *hook_pvt, struct ast_bridge *src, struct ast_bridge *dst)
static struct ast_bridge_methods bridge_stasis_v_table
Definition: stasis_bridge.c:47
static int bridge_stasis_push_peek(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
void bridge_stasis_init(void)
static int defer_bridge_add(struct stasis_app_control *control, struct ast_channel *chan, void *obj)
Definition: stasis_bridge.c:96
Internal API for the Stasis bridge subclass.
#define STASIS_BRIDGE_MIXING_CAPABILITIES
Definition: stasis_bridge.h:39
ast_app: A registered application
Definition: pbx_app.c:45
Structure that contains information regarding a channel in a bridge.
struct ast_bridge_features * features
struct ast_channel * chan
Bridge virtual methods table definition.
Definition: bridge.h:257
ast_bridge_push_channel_fn push
Definition: bridge.h:265
const char * name
Definition: bridge.h:259
ast_bridge_pull_channel_fn pull
Definition: bridge.h:267
ast_bridge_push_channel_fn push_peek
Definition: bridge.h:273
Structure that contains information about a bridge.
Definition: bridge.h:349
const struct ast_bridge_methods * v_table
Definition: bridge.h:351
const ast_string_field uniqueid
Definition: bridge.h:401
struct ast_bridge_technology * technology
Definition: bridge.h:355
struct ast_flags feature_flags
Definition: bridge.h:369
uint32_t allowed_capabilities
Definition: bridge.h:371
Structure representing a snapshot of channel state.
Main Channel structure associated with a channel.
struct ast_bridge_channel * bridge_channel
struct ast_channel * swap
Channel to swap with in the bridge. (has ref)
Definition: stasis_bridge.c:85
struct ast_bridge * bridge
Definition: stasis_bridge.c:79
int done
Definition: test_amihooks.c:48
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
#define ast_assert(a)
Definition: utils.h:739