Asterisk - The Open Source Telephony Project  18.5.0
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 <[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"
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 
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 */
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) */
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 
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  */
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  }
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,
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 
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  * \return Nothing
283  */
284 static void bridge_stasis_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
285 {
286  if ((self->allowed_capabilities & STASIS_BRIDGE_MIXING_CAPABILITIES)
287  && ast_test_flag(&self->feature_flags, AST_BRIDGE_FLAG_SMART)) {
289  }
290 
291  if (self->technology->capabilities & AST_BRIDGE_CAPABILITY_HOLDING) {
292  ast_channel_clear_bridge_roles(bridge_channel->chan);
293  }
294 
296 
297  ast_bridge_base_v_table.pull(self, bridge_channel);
298 }
299 
300 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)
301 {
302  void *bridge;
303 
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) {
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  */
316  } else if (video_mode == AST_BRIDGE_VIDEO_MODE_SINGLE_SRC) {
318  } else {
320  }
321 
322  bridge = bridge_register(bridge);
323 
324  return bridge;
325 }
326 
328 {
329  /* Setup the Stasis bridge subclass v_table. */
331  bridge_stasis_v_table.name = "stasis";
335 }
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:3370
void ast_bridge_set_sfu_video_mode(struct ast_bridge *bridge)
Set the bridge to be a selective forwarding unit.
Definition: bridge.c:3841
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:931
#define ast_channel_lock(chan)
Definition: channel.h:2945
Main Channel structure associated with a channel.
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:1284
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
Asterisk main include file. File version handling, generic pbx functions.
struct ast_bridge_features * features
ast_bridge_video_mode_type
Video source modes.
Definition: bridge.h:98
struct ast_bridge * bridge_register(struct ast_bridge *bridge)
Register the new bridge with the system.
Definition: bridge.c:709
int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data)
Execute an application.
Definition: pbx_app.c:471
Internal API for the Stasis application commands.
static void defer_bridge_add_dtor(void *obj)
Definition: stasis_bridge.c:88
#define ast_test_flag(p, flag)
Definition: utils.h:63
struct ast_bridge * bridge
Definition: stasis_bridge.c:79
const char * name
Definition: bridge.h:267
int ast_check_hangup_locked(struct ast_channel *chan)
Definition: channel.c:459
#define LOG_WARNING
Definition: logger.h:274
void __ao2_cleanup(void *obj)
Definition: astobj2.c:674
Structure representing a snapshot of channel state.
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:919
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:406
#define ast_assert(a)
Definition: utils.h:695
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:317
#define NULL
Definition: resample.c:96
static struct ast_bridge_methods bridge_stasis_v_table
Definition: stasis_bridge.c:47
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:1563
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:259
static int bridge_stasis_push_peek(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
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:1287
struct ast_bridge_methods ast_bridge_base_v_table
Bridge base class virtual method table.
Definition: bridge.c:949
int app_send_end_msg(struct stasis_app *app, struct ast_channel *chan)
Send StasisEnd message to the listening app.
Definition: res_stasis.c:1083
int done
Definition: test_amihooks.c:48
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:379
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
static int bridge_stasis_moving(struct ast_bridge_channel *bridge_channel, void *hook_pvt, struct ast_bridge *src, struct ast_bridge *dst)
void ast_bridge_channel_update_linkedids(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
#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:911
const char * stasis_app_name(const struct stasis_app *app)
Retrieve an application&#39;s name.
#define ast_channel_cleanup(c)
Cleanup a channel reference.
Definition: channel.h:2992
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static void bridge_stasis_run_cb(struct ast_channel *chan, void *data)
Definition: stasis_bridge.c:49
struct ast_bridge * bridge_alloc(size_t size, const struct ast_bridge_methods *v_table)
Definition: bridge.c:724
Internal API for the Stasis application controller.
Core PBX routines and definitions.
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:3849
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:760
const char * ast_channel_uniqueid(const struct ast_channel *chan)
const struct ast_bridge_methods * v_table
Definition: bridge.h:359
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:398
Structure that contains information about a bridge.
Definition: bridge.h:357
#define LOG_ERROR
Definition: logger.h:285
static int bridge_stasis_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
Internal API for the Stasis bridge subclass.
static int defer_bridge_add(struct stasis_app_control *control, struct ast_channel *chan, void *obj)
Definition: stasis_bridge.c:96
const char * app_name(struct ast_app *app)
Definition: pbx_app.c:463
Bridge virtual methods table definition.
Definition: bridge.h:265
#define ast_channel_unlock(chan)
Definition: channel.h:2946
#define STASIS_BRIDGE_MIXING_CAPABILITIES
Definition: stasis_bridge.h:39
static const char name[]
Definition: cdr_mysql.c:74
#define ast_free(a)
Definition: astmm.h:182
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:3833
struct ast_channel * swap
Channel to swap with in the bridge. (has ref)
Definition: stasis_bridge.c:85
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:3816
int stasis_app_channel_is_internal(struct ast_channel *chan)
Is this channel internal to Stasis?
Definition: res_stasis.c:2322
void ast_channel_clear_bridge_roles(struct ast_channel *chan)
Removes all bridge roles currently on a channel.
Definition: bridge_roles.c:360
void ast_bridge_channel_update_accountcodes(struct ast_bridge_channel *joining, struct ast_bridge_channel *leaving)
struct ast_channel * chan
Structure that contains information regarding a channel in a bridge.
After Bridge Execution API.
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2970
ast_bridge_push_channel_fn push_peek
Definition: bridge.h:281
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 ao2_cleanup(obj)
Definition: astobj2.h:1958
Internal API for the Stasis application controller.
ast_app: A registered application
Definition: pbx_app.c:45
const char * ast_channel_name(const struct ast_channel *chan)
static void bridge_stasis_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
ast_bridge_pull_channel_fn pull
Definition: bridge.h:275
ast_bridge_push_channel_fn push
Definition: bridge.h:273
void bridge_stasis_init(void)
Private Bridging API.
Stasis Application API. See Stasis Application API for detailed documentation.
struct ast_app * pbx_findapp(const char *app)
Look up an application.
Definition: ael_main.c:165
Bridging API.
static void bridge_stasis_queue_join_action(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
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:967
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)
Channel Bridging API.
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