Asterisk - The Open Source Telephony Project  21.4.1
bridge.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007 - 2009, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@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 /*! \file
20  *
21  * \brief Bridging API
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 /*** DOCUMENTATION
31  <manager name="BridgeTechnologyList" language="en_US">
32  <synopsis>
33  List available bridging technologies and their statuses.
34  </synopsis>
35  <syntax>
36  <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
37  </syntax>
38  <description>
39  <para>Returns detailed information about the available bridging technologies.</para>
40  </description>
41  <see-also>
42  <ref type="manager">BridgeTechnologySuspend</ref>
43  <ref type="manager">BridgeTechnologyUnsuspend</ref>
44  </see-also>
45  </manager>
46  <manager name="BridgeTechnologySuspend" language="en_US">
47  <synopsis>
48  Suspend a bridging technology.
49  </synopsis>
50  <syntax>
51  <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
52  <parameter name="BridgeTechnology" required="true">
53  <para>The name of the bridging technology to suspend.</para>
54  </parameter>
55  </syntax>
56  <description>
57  <para>Marks a bridging technology as suspended, which prevents subsequently created bridges from using it.</para>
58  </description>
59  <see-also>
60  <ref type="manager">BridgeTechnologySuspend</ref>
61  <ref type="manager">BridgeTechnologyUnsuspend</ref>
62  </see-also>
63  </manager>
64  <manager name="BridgeTechnologyUnsuspend" language="en_US">
65  <synopsis>
66  Unsuspend a bridging technology.
67  </synopsis>
68  <syntax>
69  <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
70  <parameter name="BridgeTechnology" required="true">
71  <para>The name of the bridging technology to unsuspend.</para>
72  </parameter>
73  </syntax>
74  <description>
75  <para>Clears a previously suspended bridging technology, which allows subsequently created bridges to use it.</para>
76  </description>
77  <see-also>
78  <ref type="manager">BridgeTechnologyList</ref>
79  <ref type="manager">BridgeTechnologySuspend</ref>
80  </see-also>
81  </manager>
82 ***/
83 
84 #include "asterisk.h"
85 
86 #include "asterisk/logger.h"
87 #include "asterisk/channel.h"
88 #include "asterisk/options.h"
89 #include "asterisk/utils.h"
90 #include "asterisk/lock.h"
91 #include "asterisk/linkedlists.h"
92 #include "asterisk/bridge.h"
96 #include "asterisk/bridge_basic.h"
99 #include "asterisk/bridge_after.h"
100 #include "asterisk/stasis_bridges.h"
101 #include "asterisk/stasis_channels.h"
103 #include "asterisk/app.h"
104 #include "asterisk/file.h"
105 #include "asterisk/module.h"
106 #include "asterisk/astobj2.h"
107 #include "asterisk/pbx.h"
108 #include "asterisk/test.h"
109 #include "asterisk/_private.h"
110 #include "asterisk/heap.h"
111 #include "asterisk/say.h"
112 #include "asterisk/timing.h"
113 #include "asterisk/stringfields.h"
114 #include "asterisk/musiconhold.h"
115 #include "asterisk/features.h"
116 #include "asterisk/cli.h"
117 #include "asterisk/parking.h"
118 #include "asterisk/core_local.h"
119 #include "asterisk/core_unreal.h"
120 #include "asterisk/causes.h"
121 
122 /*! All bridges container. */
123 static struct ao2_container *bridges;
124 
126 
127 static unsigned int optimization_id;
128 
129 /* Initial starting point for the bridge array of channels */
130 #define BRIDGE_ARRAY_START 128
131 
132 /* Grow rate of bridge array of channels */
133 #define BRIDGE_ARRAY_GROW 32
134 
135 /* Variable name - stores peer information about the most recent blind transfer */
136 #define BLINDTRANSFER "BLINDTRANSFER"
137 
138 /* Variable name - stores peer information about the most recent attended transfer */
139 #define ATTENDEDTRANSFER "ATTENDEDTRANSFER"
140 
141 static void cleanup_video_mode(struct ast_bridge *bridge);
142 
143 /*! Default DTMF keys for built in features */
145 
146 /*! Function handlers for the built in features */
148 
149 /*! Function handlers for built in interval features */
151 
152 /*! Bridge manager service request */
154  /*! List of bridge service requests. */
156  /*! Refed bridge requesting service. */
158 };
159 
161  /*! Condition, used to wake up the bridge manager thread. */
162  ast_cond_t cond;
163  /*! Queue of bridge service requests. */
165  /*! Manager thread */
166  pthread_t thread;
167  /*! TRUE if the manager needs to stop. */
168  unsigned int stop:1;
169 };
170 
171 /*! Bridge manager controller. */
173 
175 {
176  return ao2_bump(bridges);
177 }
178 
179 /*!
180  * \internal
181  * \brief Request service for a bridge from the bridge manager.
182  * \since 12.0.0
183  *
184  * \param bridge Requesting service.
185  */
186 static void bridge_manager_service_req(struct ast_bridge *bridge)
187 {
188  struct bridge_manager_request *request;
189 
190  ao2_lock(bridge_manager);
191  if (bridge_manager->stop) {
192  ao2_unlock(bridge_manager);
193  return;
194  }
195 
196  /* Create the service request. */
197  request = ast_calloc(1, sizeof(*request));
198  if (!request) {
199  /* Well. This isn't good. */
200  ao2_unlock(bridge_manager);
201  return;
202  }
203  ao2_ref(bridge, +1);
204  request->bridge = bridge;
205 
206  /* Put request into the queue and wake the bridge manager. */
208  ast_cond_signal(&bridge_manager->cond);
209  ao2_unlock(bridge_manager);
210 }
211 
213 {
214  struct ast_bridge_technology *current;
215 
216  /* Perform a sanity check to make sure the bridge technology conforms to our needed requirements */
217  if (ast_strlen_zero(technology->name)
218  || !technology->capabilities
219  || !technology->write) {
220  ast_log(LOG_WARNING, "Bridge technology %s failed registration sanity check.\n",
221  technology->name);
222  return -1;
223  }
224 
226 
227  /* Look for duplicate bridge technology already using this name, or already registered */
228  AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
229  if ((!strcasecmp(current->name, technology->name)) || (current == technology)) {
230  ast_log(LOG_WARNING, "A bridge technology of %s already claims to exist in our world.\n",
231  technology->name);
233  return -1;
234  }
235  }
236 
237  /* Copy module pointer so reference counting can keep the module from unloading */
238  technology->mod = module;
239 
240  /* Find the correct position to insert the technology. */
241  AST_RWLIST_TRAVERSE_SAFE_BEGIN(&bridge_technologies, current, entry) {
242  /* Put the highest preference tech's first in the list. */
243  if (technology->preference >= current->preference) {
244  AST_RWLIST_INSERT_BEFORE_CURRENT(technology, entry);
245 
246  break;
247  }
248  }
249  AST_RWLIST_TRAVERSE_SAFE_END;
250 
251  if (!current) {
252  /* Insert our new bridge technology to the end of the list. */
253  AST_RWLIST_INSERT_TAIL(&bridge_technologies, technology, entry);
254  }
255 
257 
258  ast_verb(5, "Registered bridge technology %s\n", technology->name);
259 
260  return 0;
261 }
262 
264 {
265  struct ast_bridge_technology *current;
266 
268 
269  /* Ensure the bridge technology is registered before removing it */
270  AST_RWLIST_TRAVERSE_SAFE_BEGIN(&bridge_technologies, current, entry) {
271  if (current == technology) {
272  AST_RWLIST_REMOVE_CURRENT(entry);
273  ast_verb(5, "Unregistered bridge technology %s\n", technology->name);
274  break;
275  }
276  }
277  AST_RWLIST_TRAVERSE_SAFE_END;
278 
280 
281  return current ? 0 : -1;
282 }
283 
284 /*!
285  * \internal
286  * \brief Put an action onto the specified bridge. Don't dup the action frame.
287  * \since 12.0.0
288  *
289  * \param bridge What to queue the action on.
290  * \param action What to do.
291  */
292 static void bridge_queue_action_nodup(struct ast_bridge *bridge, struct ast_frame *action)
293 {
294  ast_debug(1, "Bridge %s: queueing action type:%u sub:%d\n",
295  bridge->uniqueid, action->frametype, action->subclass.integer);
296 
297  ast_bridge_lock(bridge);
298  AST_LIST_INSERT_TAIL(&bridge->action_queue, action, frame_list);
299  ast_bridge_unlock(bridge);
300  bridge_manager_service_req(bridge);
301 }
302 
303 int ast_bridge_queue_action(struct ast_bridge *bridge, struct ast_frame *action)
304 {
305  struct ast_frame *dup;
306 
307  dup = ast_frdup(action);
308  if (!dup) {
309  return -1;
310  }
311  bridge_queue_action_nodup(bridge, dup);
312  return 0;
313 }
314 
315 void bridge_dissolve(struct ast_bridge *bridge, int cause)
316 {
317  struct ast_bridge_channel *bridge_channel;
318  struct ast_frame action = {
320  .subclass.integer = BRIDGE_CHANNEL_ACTION_DEFERRED_DISSOLVING,
321  };
322 
323  if (bridge->dissolved) {
324  return;
325  }
326  bridge->dissolved = 1;
327 
328  if (cause <= 0) {
329  cause = AST_CAUSE_NORMAL_CLEARING;
330  }
331  bridge->cause = cause;
332 
333  ast_debug(1, "Bridge %s: dissolving bridge with cause %d(%s)\n",
334  bridge->uniqueid, cause, ast_cause2str(cause));
335 
336  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
337  ast_bridge_channel_leave_bridge(bridge_channel,
339  }
340 
341  /* Must defer dissolving bridge because it is already locked. */
342  ast_bridge_queue_action(bridge, &action);
343 }
344 
345 /*!
346  * \internal
347  * \brief Check if a bridge should dissolve because of a stolen channel and do it.
348  * \since 12.0.0
349  *
350  * \param bridge Bridge to check.
351  * \param bridge_channel Stolen channel causing the check. It is not in the bridge to check and may be in another bridge.
352  *
353  * \note On entry, bridge and bridge_channel->bridge are already locked.
354  */
355 static void bridge_dissolve_check_stolen(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
356 {
357  if (bridge->dissolved) {
358  return;
359  }
360 
361  if (bridge_channel->features->usable
362  && ast_test_flag(&bridge_channel->features->feature_flags,
364  /* The stolen channel controlled the bridge it was stolen from. */
365  bridge_dissolve(bridge, 0);
366  return;
367  }
368  if (bridge->num_channels < 2
369  && ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_HANGUP)) {
370  /*
371  * The stolen channel has not left enough channels to keep the
372  * bridge alive. Assume the stolen channel hung up.
373  */
374  bridge_dissolve(bridge, 0);
375  return;
376  }
377 }
378 
379 /*!
380  * \internal
381  * \brief Update connected line information after a bridge has been reconfigured.
382  *
383  * \param bridge The bridge itself.
384  */
385 static void bridge_reconfigured_connected_line_update(struct ast_bridge *bridge)
386 {
387  struct ast_party_connected_line connected;
388  struct ast_bridge_channel *bridge_channel = AST_LIST_FIRST(&bridge->channels), *peer;
389  unsigned char data[1024];
390  size_t datalen;
391 
392  if (!bridge_channel ||
394  !(peer = ast_bridge_channel_peer(bridge_channel)) ||
395  ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_ZOMBIE) ||
396  ast_test_flag(ast_channel_flags(peer->chan), AST_FLAG_ZOMBIE) ||
397  ast_check_hangup_locked(bridge_channel->chan) ||
398  ast_check_hangup_locked(peer->chan)) {
399  return;
400  }
401 
402  ast_party_connected_line_init(&connected);
403 
404  ast_channel_lock(bridge_channel->chan);
405  ast_connected_line_copy_from_caller(&connected, ast_channel_caller(bridge_channel->chan));
406  ast_channel_unlock(bridge_channel->chan);
407 
408  if ((datalen = ast_connected_line_build_data(data, sizeof(data), &connected, NULL)) != (size_t) -1) {
410  }
411 
412  ast_channel_lock(peer->chan);
413  ast_connected_line_copy_from_caller(&connected, ast_channel_caller(peer->chan));
414  ast_channel_unlock(peer->chan);
415 
416  if ((datalen = ast_connected_line_build_data(data, sizeof(data), &connected, NULL)) != (size_t) -1) {
418  }
419 
420  ast_party_connected_line_free(&connected);
421 }
422 
423 /*!
424  * \internal
425  * \brief Complete joining a channel to the bridge.
426  * \since 12.0.0
427  *
428  * \param bridge What to operate upon.
429  * \param bridge_channel What is joining the bridge technology.
430  *
431  * \note On entry, bridge is already locked.
432  */
433 static void bridge_channel_complete_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
434 {
435  /* Tell the bridge technology we are joining so they set us up */
436  ast_debug(1, "Bridge %s: %p(%s) is joining %s technology\n",
437  bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
438  bridge->technology->name);
439  if (bridge->technology->join
440  && bridge->technology->join(bridge, bridge_channel)) {
441  /* We cannot leave the channel partially in the bridge so we must kick it out */
442  ast_debug(1, "Bridge %s: %p(%s) failed to join %s technology (Kicking it out)\n",
443  bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
444  bridge->technology->name);
445  bridge_channel->just_joined = 1;
447  return;
448  }
449 
450  bridge_channel->just_joined = 0;
451 
452  /*
453  * When a channel joins the bridge its streams need to be mapped to the bridge's
454  * media types vector. This way all streams map to the same media type index for
455  * a given channel.
456  */
457  if (bridge_channel->bridge->technology->stream_topology_changed) {
458  bridge_channel->bridge->technology->stream_topology_changed(
459  bridge_channel->bridge, bridge_channel);
460  } else {
461  ast_bridge_channel_stream_map(bridge_channel);
462  }
463 }
464 
465 /*!
466  * \internal
467  * \brief Complete joining new channels to the bridge.
468  * \since 12.0.0
469  *
470  * \param bridge Check for new channels on this bridge.
471  *
472  * \note On entry, bridge is already locked.
473  */
474 static void bridge_complete_join(struct ast_bridge *bridge)
475 {
476  struct ast_bridge_channel *bridge_channel;
477 
478  if (bridge->dissolved) {
479  /*
480  * No sense in completing the join on channels for a dissolved
481  * bridge. They are just going to be removed soon anyway.
482  * However, we do have reason to abort here because the bridge
483  * technology may not be able to handle the number of channels
484  * still in the bridge.
485  */
486  return;
487  }
488 
489  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
490  bridge_channel_queue_deferred_frames(bridge_channel);
491  if (!bridge_channel->just_joined) {
492  continue;
493  }
494  bridge_channel_complete_join(bridge, bridge_channel);
495  }
496 }
497 
498 /*! \brief Helper function used to find the "best" bridge technology given specified capabilities */
499 static struct ast_bridge_technology *find_best_technology(uint32_t capabilities, struct ast_bridge *bridge)
500 {
501  struct ast_bridge_technology *current;
502  struct ast_bridge_technology *best = NULL;
503 
505  AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
506  if (current->suspended) {
507  ast_debug(1, "Bridge technology %s is suspended. Skipping.\n",
508  current->name);
509  continue;
510  }
511  if (!(current->capabilities & capabilities)) {
512  ast_debug(1, "Bridge technology %s does not have any capabilities we want.\n",
513  current->name);
514  continue;
515  }
516  if (best && current->preference <= best->preference) {
517  ast_debug(1, "Bridge technology %s has less preference than %s (%u <= %u). Skipping.\n",
518  current->name, best->name, current->preference, best->preference);
519  continue;
520  }
521  if (current->compatible && !current->compatible(bridge)) {
522  ast_debug(1, "Bridge technology %s is not compatible with properties of existing bridge.\n",
523  current->name);
524  continue;
525  }
526  if (!ast_module_running_ref(current->mod)) {
527  ast_debug(1, "Bridge technology %s is not running, skipping.\n", current->name);
528  continue;
529  }
530  if (best) {
531  ast_module_unref(best->mod);
532  }
533  best = current;
534  }
535 
536  if (best) {
537  ast_debug(1, "Chose bridge technology %s\n", best->name);
538  }
539 
541 
542  return best;
543 }
544 
546  struct ast_bridge_technology *tech;
547  void *tech_pvt;
548 };
549 
550 /*!
551  * \internal
552  * \brief Deferred destruction of bridge tech private structure.
553  * \since 12.0.0
554  *
555  * \param bridge What to execute the action on.
556  * \param action Deferred bridge tech destruction.
557  *
558  * \note On entry, bridge must not be locked.
559  */
560 static void bridge_tech_deferred_destroy(struct ast_bridge *bridge, struct ast_frame *action)
561 {
562  struct tech_deferred_destroy *deferred = action->data.ptr;
563  struct ast_bridge dummy_bridge = {
564  .technology = deferred->tech,
565  .tech_pvt = deferred->tech_pvt,
566  .creator = bridge->creator,
567  .name = bridge->name,
568  .uniqueid = bridge->uniqueid,
569  };
570 
571  ast_debug(1, "Bridge %s: calling %s technology destructor (deferred, dummy)\n",
572  dummy_bridge.uniqueid, dummy_bridge.technology->name);
573  dummy_bridge.technology->destroy(&dummy_bridge);
574  ast_module_unref(dummy_bridge.technology->mod);
575 }
576 
577 /*!
578  * \internal
579  * \brief Handle bridge action frame.
580  * \since 12.0.0
581  *
582  * \param bridge What to execute the action on.
583  * \param action What to do.
584  *
585  * \note On entry, bridge is already locked.
586  * \note Can be called by the bridge destructor
587  */
588 static void bridge_action_bridge(struct ast_bridge *bridge, struct ast_frame *action)
589 {
590 #if 0 /* In case we need to know when the destructor is calling us. */
591  int in_destructor = !ao2_ref(bridge, 0);
592 #endif
593 
594  switch (action->subclass.integer) {
596  ast_bridge_unlock(bridge);
597  bridge_tech_deferred_destroy(bridge, action);
598  ast_bridge_lock(bridge);
599  break;
601  ast_bridge_unlock(bridge);
602  bridge->v_table->dissolving(bridge);
603  ast_bridge_lock(bridge);
604  break;
605  default:
606  /* Unexpected deferred action type. Should never happen. */
607  ast_assert(0);
608  break;
609  }
610 }
611 
612 /*!
613  * \internal
614  * \brief Do any pending bridge actions.
615  * \since 12.0.0
616  *
617  * \param bridge What to do actions on.
618  *
619  * \note On entry, bridge is already locked.
620  * \note Can be called by the bridge destructor.
621  */
622 static void bridge_handle_actions(struct ast_bridge *bridge)
623 {
624  struct ast_frame *action;
625 
626  while ((action = AST_LIST_REMOVE_HEAD(&bridge->action_queue, frame_list))) {
627  switch (action->frametype) {
629  bridge_action_bridge(bridge, action);
630  break;
631  default:
632  /* Unexpected deferred frame type. Should never happen. */
633  ast_assert(0);
634  break;
635  }
636  ast_frfree(action);
637  }
638 }
639 
640 static void destroy_bridge(void *obj)
641 {
642  struct ast_bridge *bridge = obj;
643 
644  ast_debug(1, "Bridge %s: actually destroying %s bridge, nobody wants it anymore\n",
645  bridge->uniqueid, bridge->v_table->name);
646 
647  if (bridge->construction_completed) {
648  bridge_topics_destroy(bridge);
649  }
650 
651  /* Do any pending actions in the context of destruction. */
652  ast_bridge_lock(bridge);
653  bridge_handle_actions(bridge);
654  ast_bridge_unlock(bridge);
655 
656  /* There should not be any channels left in the bridge. */
657  ast_assert(AST_LIST_EMPTY(&bridge->channels));
658 
659  ast_debug(1, "Bridge %s: calling %s bridge destructor\n",
660  bridge->uniqueid, bridge->v_table->name);
661  bridge->v_table->destroy(bridge);
662 
663  /* Pass off the bridge to the technology to destroy if needed */
664  if (bridge->technology) {
665  ast_debug(1, "Bridge %s: calling %s technology stop\n",
666  bridge->uniqueid, bridge->technology->name);
667  if (bridge->technology->stop) {
668  ast_bridge_lock(bridge);
669  bridge->technology->stop(bridge);
670  ast_bridge_unlock(bridge);
671  }
672  ast_debug(1, "Bridge %s: calling %s technology destructor\n",
673  bridge->uniqueid, bridge->technology->name);
674  if (bridge->technology->destroy) {
675  bridge->technology->destroy(bridge);
676  }
677  ast_module_unref(bridge->technology->mod);
678  bridge->technology = NULL;
679  }
680 
681  AST_VECTOR_FREE(&bridge->media_types);
682 
683  bridge->callid = 0;
684 
685  cleanup_video_mode(bridge);
686 
688  ao2_cleanup(bridge->current_snapshot);
689 }
690 
691 struct ast_bridge *bridge_register(struct ast_bridge *bridge)
692 {
693  if (bridge) {
694  bridge->construction_completed = 1;
695  ast_bridge_lock(bridge);
696  ast_bridge_publish_state(bridge);
697  ast_bridge_unlock(bridge);
698  if (!ao2_link(bridges, bridge)) {
699  ast_bridge_destroy(bridge, 0);
700  bridge = NULL;
701  }
702  }
703  return bridge;
704 }
705 
706 struct ast_bridge *bridge_alloc(size_t size, const struct ast_bridge_methods *v_table)
707 {
708  struct ast_bridge *bridge;
709 
710  /* Check v_table that all methods are present. */
711  if (!v_table
712  || !v_table->name
713  || !v_table->destroy
714  || !v_table->dissolving
715  || !v_table->push
716  || !v_table->pull
717  || !v_table->notify_masquerade
718  || !v_table->get_merge_priority) {
719  ast_log(LOG_ERROR, "Virtual method table for bridge class %s not complete.\n",
720  v_table && v_table->name ? v_table->name : "<unknown>");
721  ast_assert(0);
722  return NULL;
723  }
724 
725  bridge = ao2_alloc(size, destroy_bridge);
726  if (!bridge) {
727  return NULL;
728  }
729 
730  if (ast_string_field_init(bridge, 80)) {
731  ao2_cleanup(bridge);
732  return NULL;
733  }
734 
735  bridge->v_table = v_table;
736 
737  AST_VECTOR_INIT(&bridge->media_types, AST_MEDIA_TYPE_END);
738 
739  return bridge;
740 }
741 
742 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)
743 {
744  char uuid_hold[AST_UUID_STR_LEN];
745 
746  if (!self) {
747  return NULL;
748  }
749 
750  if (!ast_strlen_zero(id)) {
751  ast_string_field_set(self, uniqueid, id);
752  } else {
753  ast_uuid_generate_str(uuid_hold, AST_UUID_STR_LEN);
754  ast_string_field_set(self, uniqueid, uuid_hold);
755  }
756  ast_string_field_set(self, creator, creator);
757  if (!ast_strlen_zero(creator)) {
758  ast_string_field_set(self, name, name);
759  }
760 
761  ast_set_flag(&self->feature_flags, flags);
762  self->allowed_capabilities = capabilities;
763 
764  if (!(flags & AST_BRIDGE_FLAG_INVISIBLE)) {
765  if (bridge_topics_init(self) != 0) {
766  ast_log(LOG_WARNING, "Bridge %s: Could not initialize topics\n",
767  self->uniqueid);
768  ao2_ref(self, -1);
769  return NULL;
770  }
771  }
772 
773  /* Use our helper function to find the "best" bridge technology. */
774  self->technology = find_best_technology(capabilities, self);
775  if (!self->technology) {
776  ast_log(LOG_WARNING, "Bridge %s: Could not create class %s. No technology to support it.\n",
777  self->uniqueid, self->v_table->name);
778  ao2_ref(self, -1);
779  return NULL;
780  }
781 
782  /* Pass off the bridge to the technology to manipulate if needed */
783  ast_debug(1, "Bridge %s: calling %s technology constructor\n",
784  self->uniqueid, self->technology->name);
785  if (self->technology->create && self->technology->create(self)) {
786  ast_log(LOG_WARNING, "Bridge %s: failed to setup bridge technology %s\n",
787  self->uniqueid, self->technology->name);
788  ao2_ref(self, -1);
789  return NULL;
790  }
791  ast_debug(1, "Bridge %s: calling %s technology start\n",
792  self->uniqueid, self->technology->name);
793  if (self->technology->start && self->technology->start(self)) {
794  ast_log(LOG_WARNING, "Bridge %s: failed to start bridge technology %s\n",
795  self->uniqueid, self->technology->name);
796  ao2_ref(self, -1);
797  return NULL;
798  }
799 
800  if (!(flags & AST_BRIDGE_FLAG_INVISIBLE)) {
801  if (!ast_bridge_topic(self)) {
802  ao2_ref(self, -1);
803  return NULL;
804  }
805  }
806 
807  self->creationtime = ast_tvnow();
808 
809  return self;
810 }
811 
812 /*!
813  * \internal
814  * \brief ast_bridge base class destructor.
815  * \since 12.0.0
816  *
817  * \param self Bridge to operate upon.
818  *
819  * \note Stub because of nothing to do.
820  */
821 static void bridge_base_destroy(struct ast_bridge *self)
822 {
823 }
824 
825 /*!
826  * \internal
827  * \brief The bridge is being dissolved.
828  * \since 12.0.0
829  *
830  * \param self Bridge to operate upon.
831  */
832 static void bridge_base_dissolving(struct ast_bridge *self)
833 {
834  ao2_unlink(bridges, self);
835 }
836 
837 /*!
838  * \internal
839  * \brief ast_bridge base push method.
840  * \since 12.0.0
841  *
842  * \param self Bridge to operate upon.
843  * \param bridge_channel Bridge channel to push.
844  * \param swap Bridge channel to swap places with if not NULL.
845  *
846  * \note On entry, self is already locked.
847  * \note Stub because of nothing to do.
848  *
849  * \retval 0 on success
850  * \retval -1 on failure
851  */
852 static int bridge_base_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
853 {
854  return 0;
855 }
856 
857 /*!
858  * \internal
859  * \brief ast_bridge base pull method.
860  * \since 12.0.0
861  *
862  * \param self Bridge to operate upon.
863  * \param bridge_channel Bridge channel to pull.
864  *
865  * \note On entry, self is already locked.
866  */
867 static void bridge_base_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
868 {
870 }
871 
872 /*!
873  * \internal
874  * \brief ast_bridge base notify_masquerade method.
875  * \since 12.0.0
876  *
877  * \param self Bridge to operate upon.
878  * \param bridge_channel Bridge channel that was masqueraded.
879  *
880  * \note On entry, self is already locked.
881  */
882 static void bridge_base_notify_masquerade(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
883 {
884  self->reconfigured = 1;
885 }
886 
887 /*!
888  * \internal
889  * \brief Get the merge priority of this bridge.
890  * \since 12.0.0
891  *
892  * \param self Bridge to operate upon.
893  *
894  * \note On entry, self is already locked.
895  *
896  * \return Merge priority
897  */
898 static int bridge_base_get_merge_priority(struct ast_bridge *self)
899 {
900  return 0;
901 }
902 
903 /*!
904  * \internal
905  * \brief ast_bridge base push_peek method.
906  * \since 13.2.0
907  *
908  * \param self Bridge to operate upon.
909  * \param bridge_channel Bridge channel to push.
910  * \param swap Bridge channel to swap places with if not NULL.
911  *
912  * \note On entry, self is already locked.
913  * \note Stub because of nothing to do.
914  *
915  * \retval 0 on success
916  * \retval -1 on failure
917  */
918 static int bridge_base_push_peek(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
919 {
920  return 0;
921 }
922 
924  .name = "base",
925  .destroy = bridge_base_destroy,
926  .dissolving = bridge_base_dissolving,
927  .push = bridge_base_push,
928  .pull = bridge_base_pull,
929  .notify_masquerade = bridge_base_notify_masquerade,
930  .get_merge_priority = bridge_base_get_merge_priority,
931  .push_peek = bridge_base_push_peek,
932 };
933 
934 struct ast_bridge *ast_bridge_base_new(uint32_t capabilities, unsigned int flags, const char *creator, const char *name, const char *id)
935 {
936  void *bridge;
937 
938  bridge = bridge_alloc(sizeof(struct ast_bridge), &ast_bridge_base_v_table);
939  bridge = bridge_base_init(bridge, capabilities, flags, creator, name, id);
940  bridge = bridge_register(bridge);
941  return bridge;
942 }
943 
944 int ast_bridge_destroy(struct ast_bridge *bridge, int cause)
945 {
946  ast_debug(1, "Bridge %s: telling all channels to leave the party\n", bridge->uniqueid);
947  ast_bridge_lock(bridge);
948  bridge_dissolve(bridge, cause);
949  ast_bridge_unlock(bridge);
950 
951  ao2_ref(bridge, -1);
952 
953  return 0;
954 }
955 
956 /*!
957  * \internal
958  * \brief Perform the smart bridge operation.
959  * \since 12.0.0
960  *
961  * \param bridge Work on this bridge.
962  *
963  * \details
964  * Basically see if a new bridge technology should be used instead
965  * of the current one.
966  *
967  * \note On entry, bridge is already locked.
968  *
969  * \retval 0 on success.
970  * \retval -1 on error.
971  */
972 static int smart_bridge_operation(struct ast_bridge *bridge)
973 {
974  uint32_t new_capabilities;
975  struct ast_bridge_technology *new_technology;
976  struct ast_bridge_technology *old_technology = bridge->technology;
977  struct ast_bridge_channel *bridge_channel;
978  struct ast_frame *deferred_action;
979  struct ast_bridge dummy_bridge = {
980  .technology = bridge->technology,
981  .tech_pvt = bridge->tech_pvt,
982  .creator = bridge->creator,
983  .name = bridge->name,
984  .uniqueid = bridge->uniqueid,
985  };
986 
987  if (bridge->dissolved) {
988  ast_debug(1, "Bridge %s is dissolved, not performing smart bridge operation.\n",
989  bridge->uniqueid);
990  return 0;
991  }
992 
993  /* Determine new bridge technology capabilities needed. */
994  if (2 < bridge->num_channels) {
995  new_capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX;
996  new_capabilities &= bridge->allowed_capabilities;
997  } else {
999  new_capabilities &= bridge->allowed_capabilities;
1000  if (!new_capabilities
1002  /* Allow switching between different multimix bridge technologies. */
1003  new_capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX;
1004  }
1005  }
1006 
1007  /* Find a bridge technology to satisfy the new capabilities. */
1008  new_technology = find_best_technology(new_capabilities, bridge);
1009  if (!new_technology) {
1010  int is_compatible = 0;
1011 
1012  if (old_technology->compatible) {
1013  is_compatible = old_technology->compatible(bridge);
1014  } else if (old_technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) {
1015  is_compatible = 1;
1016  } else if (bridge->num_channels <= 2
1017  && (old_technology->capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX)) {
1018  is_compatible = 1;
1019  }
1020 
1021  if (is_compatible) {
1022  ast_debug(1, "Bridge %s could not get a new technology, staying with old technology.\n",
1023  bridge->uniqueid);
1024  return 0;
1025  }
1026  ast_log(LOG_WARNING, "Bridge %s has no technology available to support it.\n",
1027  bridge->uniqueid);
1028  return -1;
1029  }
1030  if (new_technology == old_technology) {
1031  ast_debug(1, "Bridge %s is already using the new technology.\n",
1032  bridge->uniqueid);
1033  ast_module_unref(old_technology->mod);
1034  return 0;
1035  }
1036 
1037  if (old_technology->destroy) {
1038  struct tech_deferred_destroy deferred_tech_destroy = {
1039  .tech = dummy_bridge.technology,
1040  .tech_pvt = dummy_bridge.tech_pvt,
1041  };
1042  struct ast_frame action = {
1044  .subclass.integer = BRIDGE_CHANNEL_ACTION_DEFERRED_TECH_DESTROY,
1045  .data.ptr = &deferred_tech_destroy,
1046  .datalen = sizeof(deferred_tech_destroy),
1047  };
1048 
1049  /*
1050  * We need to defer the bridge technology destroy callback
1051  * because we have the bridge locked.
1052  */
1053  deferred_action = ast_frdup(&action);
1054  if (!deferred_action) {
1055  ast_module_unref(new_technology->mod);
1056  return -1;
1057  }
1058  } else {
1059  deferred_action = NULL;
1060  }
1061 
1062  /*
1063  * We are now committed to changing the bridge technology. We
1064  * must not release the bridge lock until we have installed the
1065  * new bridge technology.
1066  */
1067  ast_verb(4, "Bridge %s: switching from %s technology to %s\n",
1068  bridge->uniqueid, old_technology->name, new_technology->name);
1069 
1070  /*
1071  * Since we are soon going to pass this bridge to a new
1072  * technology we need to NULL out the tech_pvt pointer but
1073  * don't worry as it still exists in dummy_bridge, ditto for the
1074  * old technology.
1075  */
1076  bridge->tech_pvt = NULL;
1077  bridge->technology = new_technology;
1078 
1079  /* Setup the new bridge technology. */
1080  ast_debug(1, "Bridge %s: calling %s technology constructor\n",
1081  bridge->uniqueid, new_technology->name);
1082  if (new_technology->create && new_technology->create(bridge)) {
1083  ast_log(LOG_WARNING, "Bridge %s: failed to setup bridge technology %s\n",
1084  bridge->uniqueid, new_technology->name);
1085  bridge->tech_pvt = dummy_bridge.tech_pvt;
1086  bridge->technology = dummy_bridge.technology;
1087  ast_module_unref(new_technology->mod);
1088  return -1;
1089  }
1090 
1091  /* To ensure that things are sane for the old technology move the channels it
1092  * expects to the dummy bridge
1093  */
1094  AST_LIST_TRAVERSE_SAFE_BEGIN(&bridge->channels, bridge_channel, entry) {
1095  if (bridge_channel->just_joined) {
1096  continue;
1097  }
1098  ast_debug(1, "Bridge %s: moving %p(%s) to dummy bridge temporarily\n",
1099  bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
1101  AST_LIST_INSERT_TAIL(&dummy_bridge.channels, bridge_channel, entry);
1102  dummy_bridge.num_channels++;
1103  if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
1104  dummy_bridge.num_lonely++;
1105  }
1106  if (!bridge_channel->suspended) {
1107  dummy_bridge.num_active++;
1108  }
1109  }
1111 
1112  /* Take all the channels out of the old technology */
1113  AST_LIST_TRAVERSE_SAFE_BEGIN(&dummy_bridge.channels, bridge_channel, entry) {
1114  ast_debug(1, "Bridge %s: %p(%s) is leaving %s technology (dummy)\n",
1115  dummy_bridge.uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
1116  old_technology->name);
1117  if (old_technology->leave) {
1118  old_technology->leave(&dummy_bridge, bridge_channel);
1119  }
1121  AST_LIST_INSERT_TAIL(&bridge->channels, bridge_channel, entry);
1122  dummy_bridge.num_channels--;
1123  if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
1124  dummy_bridge.num_lonely--;
1125  }
1126  if (!bridge_channel->suspended) {
1127  dummy_bridge.num_active--;
1128  }
1129  }
1131 
1132  ast_debug(1, "Bridge %s: calling %s technology stop\n",
1133  dummy_bridge.uniqueid, old_technology->name);
1134  if (old_technology->stop) {
1135  old_technology->stop(&dummy_bridge);
1136  }
1137 
1138  /* Add any new channels or re-add existing channels to the bridge. */
1139  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1140  bridge_channel_complete_join(bridge, bridge_channel);
1141  }
1142 
1143  ast_debug(1, "Bridge %s: calling %s technology start\n",
1144  bridge->uniqueid, new_technology->name);
1145  if (new_technology->start && new_technology->start(bridge)) {
1146  ast_log(LOG_WARNING, "Bridge %s: failed to start bridge technology %s\n",
1147  bridge->uniqueid, new_technology->name);
1148  }
1149 
1150  /*
1151  * Now that all the channels have been moved over we need to get
1152  * rid of all the information the old technology may have left
1153  * around.
1154  */
1155  if (old_technology->destroy) {
1156  ast_debug(1, "Bridge %s: deferring %s technology destructor\n",
1157  dummy_bridge.uniqueid, old_technology->name);
1158  bridge_queue_action_nodup(bridge, deferred_action);
1159  } else {
1160  ast_debug(1, "Bridge %s: calling %s technology destructor\n",
1161  dummy_bridge.uniqueid, old_technology->name);
1162  ast_module_unref(old_technology->mod);
1163  }
1164 
1165  return 0;
1166 }
1167 
1168 /*!
1169  * \internal
1170  * \brief Bridge channel to check if a BRIDGE_PLAY_SOUND needs to be played.
1171  * \since 12.0.0
1172  *
1173  * \param bridge_channel What to check.
1174  */
1175 static void check_bridge_play_sound(struct ast_bridge_channel *bridge_channel)
1176 {
1177  const char *play_file;
1178 
1179  ast_channel_lock(bridge_channel->chan);
1180  play_file = pbx_builtin_getvar_helper(bridge_channel->chan, "BRIDGE_PLAY_SOUND");
1181  if (!ast_strlen_zero(play_file)) {
1182  play_file = ast_strdupa(play_file);
1183  pbx_builtin_setvar_helper(bridge_channel->chan, "BRIDGE_PLAY_SOUND", NULL);
1184  } else {
1185  play_file = NULL;
1186  }
1187  ast_channel_unlock(bridge_channel->chan);
1188 
1189  if (play_file) {
1190  ast_bridge_channel_queue_playfile(bridge_channel, NULL, play_file, NULL);
1191  }
1192 }
1193 
1194 /*!
1195  * \internal
1196  * \brief Check for any BRIDGE_PLAY_SOUND channel variables in the bridge.
1197  * \since 12.0.0
1198  *
1199  * \param bridge What to operate on.
1200  *
1201  * \note On entry, the bridge is already locked.
1202  */
1203 static void check_bridge_play_sounds(struct ast_bridge *bridge)
1204 {
1205  struct ast_bridge_channel *bridge_channel;
1206 
1207  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1208  check_bridge_play_sound(bridge_channel);
1209  }
1210 }
1211 
1212 void ast_bridge_vars_set(struct ast_channel *chan, const char *name, const char *pvtid)
1213 {
1215  pbx_builtin_setvar_helper(chan, "BRIDGEPEER", name);
1216  pbx_builtin_setvar_helper(chan, "BRIDGEPVTCALLID", pvtid);
1218 }
1219 
1220 /*!
1221  * \internal
1222  * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in a 2 party bridge.
1223  * \since 12.0.0
1224  *
1225  * \param c0 Party of the first part.
1226  * \param c1 Party of the second part.
1227  *
1228  * \note On entry, the bridge is already locked.
1229  * \note The bridge is expected to have exactly two parties.
1230  */
1231 static void set_bridge_peer_vars_2party(struct ast_channel *c0, struct ast_channel *c1)
1232 {
1233  const char *c0_name;
1234  const char *c1_name;
1235  const char *c0_pvtid = NULL;
1236  const char *c1_pvtid = NULL;
1237 #define UPDATE_BRIDGE_VARS_GET(chan, name, pvtid) \
1238  do { \
1239  name = ast_strdupa(ast_channel_name(chan)); \
1240  if (ast_channel_tech(chan)->get_pvt_uniqueid) { \
1241  pvtid = ast_strdupa(ast_channel_tech(chan)->get_pvt_uniqueid(chan)); \
1242  } \
1243  } while (0)
1244 
1245  ast_channel_lock(c1);
1246  UPDATE_BRIDGE_VARS_GET(c1, c1_name, c1_pvtid);
1247  ast_channel_unlock(c1);
1248 
1249  ast_channel_lock(c0);
1250  ast_bridge_vars_set(c0, c1_name, c1_pvtid);
1251  UPDATE_BRIDGE_VARS_GET(c0, c0_name, c0_pvtid);
1252  ast_channel_unlock(c0);
1253 
1254  ast_channel_lock(c1);
1255  ast_bridge_vars_set(c1, c0_name, c0_pvtid);
1256  ast_channel_unlock(c1);
1257 }
1258 
1259 /*!
1260  * \internal
1261  * \brief Fill the BRIDGEPEER value buffer with a comma separated list of channel names.
1262  * \since 12.0.0
1263  *
1264  * \param buf Buffer to fill. The caller must guarantee the buffer is large enough.
1265  * \param cur_idx Which index into names[] to skip.
1266  * \param names Channel names to put in the buffer.
1267  * \param num_names Number of names in the array.
1268  */
1269 static void fill_bridgepeer_buf(char *buf, unsigned int cur_idx, const char *names[], unsigned int num_names)
1270 {
1271  int need_separator = 0;
1272  unsigned int idx;
1273  const char *src;
1274  char *pos;
1275 
1276  pos = buf;
1277  for (idx = 0; idx < num_names; ++idx) {
1278  if (idx == cur_idx) {
1279  continue;
1280  }
1281 
1282  if (need_separator) {
1283  *pos++ = ',';
1284  }
1285  need_separator = 1;
1286 
1287  /* Copy name into buffer. */
1288  src = names[idx];
1289  while (*src) {
1290  *pos++ = *src++;
1291  }
1292  }
1293  *pos = '\0';
1294 }
1295 
1296 /*!
1297  * \internal
1298  * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in a multi-party bridge.
1299  * \since 12.0.0
1300  *
1301  * \param bridge What to operate on.
1302  *
1303  * \note On entry, the bridge is already locked.
1304  * \note The bridge is expected to have more than two parties.
1305  */
1306 static void set_bridge_peer_vars_multiparty(struct ast_bridge *bridge)
1307 {
1308 /*
1309  * Set a maximum number of channel names for the BRIDGEPEER
1310  * list. The plus one is for the current channel which is not
1311  * put in the list.
1312  */
1313 #define MAX_BRIDGEPEER_CHANS (10 + 1)
1314 
1315  unsigned int idx;
1316  unsigned int num_names;
1317  unsigned int len;
1318  const char **names;
1319  char *buf;
1320  struct ast_bridge_channel *bridge_channel;
1321 
1322  /* Get first MAX_BRIDGEPEER_CHANS channel names. */
1323  num_names = MIN(bridge->num_channels, MAX_BRIDGEPEER_CHANS);
1324  names = ast_alloca(num_names * sizeof(*names));
1325  idx = 0;
1326  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1327  if (num_names <= idx) {
1328  break;
1329  }
1330  ast_channel_lock(bridge_channel->chan);
1331  names[idx++] = ast_strdupa(ast_channel_name(bridge_channel->chan));
1332  ast_channel_unlock(bridge_channel->chan);
1333  }
1334 
1335  /* Determine maximum buf size needed. */
1336  len = num_names;
1337  for (idx = 0; idx < num_names; ++idx) {
1338  len += strlen(names[idx]);
1339  }
1340  buf = ast_alloca(len);
1341 
1342  /* Set the bridge channel variables. */
1343  idx = 0;
1344  buf[0] = '\0';
1345  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1346  if (idx < num_names) {
1347  fill_bridgepeer_buf(buf, idx, names, num_names);
1348  }
1349  ++idx;
1350 
1351  ast_channel_lock(bridge_channel->chan);
1352  ast_bridge_vars_set(bridge_channel->chan, buf, NULL);
1353  ast_channel_unlock(bridge_channel->chan);
1354  }
1355 }
1356 
1357 /*!
1358  * \internal
1359  * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in a holding bridge.
1360  * \since 12.0.0
1361  *
1362  * \param bridge What to operate on.
1363  *
1364  * \note On entry, the bridge is already locked.
1365  */
1366 static void set_bridge_peer_vars_holding(struct ast_bridge *bridge)
1367 {
1368  struct ast_bridge_channel *bridge_channel;
1369 
1370  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1371  ast_channel_lock(bridge_channel->chan);
1372  ast_bridge_vars_set(bridge_channel->chan, NULL, NULL);
1373  ast_channel_unlock(bridge_channel->chan);
1374  }
1375 }
1376 
1377 /*!
1378  * \internal
1379  * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in the bridge.
1380  * \since 12.0.0
1381  *
1382  * \param bridge What to operate on.
1383  *
1384  * \note On entry, the bridge is already locked.
1385  */
1386 static void set_bridge_peer_vars(struct ast_bridge *bridge)
1387 {
1389  set_bridge_peer_vars_holding(bridge);
1390  return;
1391  }
1392  if (bridge->num_channels < 2) {
1393  return;
1394  }
1395  if (bridge->num_channels == 2) {
1396  set_bridge_peer_vars_2party(AST_LIST_FIRST(&bridge->channels)->chan,
1397  AST_LIST_LAST(&bridge->channels)->chan);
1398  } else {
1399  set_bridge_peer_vars_multiparty(bridge);
1400  }
1401 }
1402 
1403 void bridge_reconfigured(struct ast_bridge *bridge, unsigned int colp_update)
1404 {
1405  if (!bridge->reconfigured) {
1406  return;
1407  }
1408  bridge->reconfigured = 0;
1409  if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_SMART)
1410  && smart_bridge_operation(bridge)) {
1411  /* Smart bridge failed. */
1412  bridge_dissolve(bridge, 0);
1413  return;
1414  }
1415  bridge_complete_join(bridge);
1416 
1417  if (bridge->dissolved) {
1418  return;
1419  }
1420  check_bridge_play_sounds(bridge);
1421  set_bridge_peer_vars(bridge);
1422  ast_bridge_publish_state(bridge);
1423 
1424  if (colp_update) {
1425  bridge_reconfigured_connected_line_update(bridge);
1426  }
1427 }
1428 
1429 struct ast_bridge_channel *bridge_find_channel(struct ast_bridge *bridge, struct ast_channel *chan)
1430 {
1431  struct ast_bridge_channel *bridge_channel;
1432 
1433  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1434  if (bridge_channel->chan == chan) {
1435  break;
1436  }
1437  }
1438 
1439  return bridge_channel;
1440 }
1441 
1443 {
1444  struct ast_bridge_channel *bridge_channel;
1445  struct ast_bridge *bridge;
1446 
1447  /* Safely get the bridge_channel pointer for the chan. */
1448  ast_channel_lock(chan);
1449  bridge_channel = ast_channel_get_bridge_channel(chan);
1450  ast_channel_unlock(chan);
1451  if (!bridge_channel) {
1452  /* Not in a bridge */
1453  return;
1454  }
1455 
1456  ast_bridge_channel_lock_bridge(bridge_channel);
1457  bridge = bridge_channel->bridge;
1458  if (bridge_channel == bridge_find_channel(bridge, chan)) {
1459 /*
1460  * XXX ASTERISK-22366 this needs more work. The channels need
1461  * to be made compatible again if the formats change. The
1462  * bridge_channel thread needs to monitor for this case.
1463  */
1464  /* The channel we want to notify is still in a bridge. */
1465  bridge->v_table->notify_masquerade(bridge, bridge_channel);
1466  bridge_reconfigured(bridge, 1);
1467  }
1468  ast_bridge_unlock(bridge);
1469  ao2_ref(bridge_channel, -1);
1470 }
1471 
1472 /*!
1473  * \brief Internal bridge impart wait condition and associated conditional.
1474  */
1477  /*! Lock for the data structure */
1479  /*! Wait condition */
1480  ast_cond_t cond;
1481  /*! Wait until done */
1482  int done;
1483 };
1484 
1486 
1487 /*!
1488  * \internal
1489  * \brief Signal imparting threads to wake up.
1490  * \since 13.9.0
1491  *
1492  * \param ds_head List of imparting threads to wake up.
1493  */
1494 static void bridge_channel_impart_ds_head_signal(struct bridge_channel_impart_ds_head *ds_head)
1495 {
1496  if (ds_head) {
1498 
1499  while ((cond = AST_LIST_REMOVE_HEAD(ds_head, node))) {
1500  ast_mutex_lock(&cond->lock);
1501  cond->done = 1;
1502  ast_cond_signal(&cond->cond);
1503  ast_mutex_unlock(&cond->lock);
1504  }
1505  }
1506 }
1507 
1508 static void bridge_channel_impart_ds_head_dtor(void *doomed)
1509 {
1510  bridge_channel_impart_ds_head_signal(doomed);
1511  ast_free(doomed);
1512 }
1513 
1514 /*!
1515  * \internal
1516  * \brief Fixup the bridge impart datastore.
1517  * \since 13.9.0
1518  *
1519  * \param data Bridge impart datastore data to fixup from old_chan.
1520  * \param old_chan The datastore is moving from this channel.
1521  * \param new_chan The datastore is moving to this channel.
1522  */
1523 static void bridge_channel_impart_ds_head_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
1524 {
1525  /*
1526  * Signal any waiting impart threads. The masquerade is going to kill
1527  * old_chan and we don't need to be waiting on new_chan.
1528  */
1529  bridge_channel_impart_ds_head_signal(data);
1530 }
1531 
1532 static const struct ast_datastore_info bridge_channel_impart_ds_info = {
1533  .type = "bridge-impart-ds",
1534  .destroy = bridge_channel_impart_ds_head_dtor,
1535  .chan_fixup = bridge_channel_impart_ds_head_fixup,
1536 };
1537 
1538 /*!
1539  * \internal
1540  * \brief Add impart wait datastore conditional to channel.
1541  * \since 13.9.0
1542  *
1543  * \param chan Channel to add the impart wait conditional.
1544  * \param cond Imparting conditional to add.
1545  *
1546  * \retval 0 on success.
1547  * \retval -1 on error.
1548  */
1549 static int bridge_channel_impart_add(struct ast_channel *chan, struct bridge_channel_impart_cond *cond)
1550 {
1551  struct ast_datastore *datastore;
1552  struct bridge_channel_impart_ds_head *ds_head;
1553 
1554  ast_channel_lock(chan);
1555 
1556  datastore = ast_channel_datastore_find(chan, &bridge_channel_impart_ds_info, NULL);
1557  if (!datastore) {
1558  datastore = ast_datastore_alloc(&bridge_channel_impart_ds_info, NULL);
1559  if (!datastore) {
1560  ast_channel_unlock(chan);
1561  return -1;
1562  }
1563  ds_head = ast_calloc(1, sizeof(*ds_head));
1564  if (!ds_head) {
1565  ast_channel_unlock(chan);
1566  ast_datastore_free(datastore);
1567  return -1;
1568  }
1569  datastore->data = ds_head;
1570  ast_channel_datastore_add(chan, datastore);
1571  } else {
1572  ds_head = datastore->data;
1573  ast_assert(ds_head != NULL);
1574  }
1575 
1576  AST_LIST_INSERT_TAIL(ds_head, cond, node);
1577 
1578  ast_channel_unlock(chan);
1579  return 0;
1580 }
1581 
1582 void bridge_channel_impart_signal(struct ast_channel *chan)
1583 {
1584  struct ast_datastore *datastore;
1585 
1586  ast_channel_lock(chan);
1587  datastore = ast_channel_datastore_find(chan, &bridge_channel_impart_ds_info, NULL);
1588  if (datastore) {
1589  bridge_channel_impart_ds_head_signal(datastore->data);
1590  }
1591  ast_channel_unlock(chan);
1592 }
1593 
1594 /*!
1595  * \internal
1596  * \brief Block imparting channel thread until signaled.
1597  * \since 13.9.0
1598  *
1599  * \param cond Imparting conditional to wait for.
1600  */
1601 static void bridge_channel_impart_wait(struct bridge_channel_impart_cond *cond)
1602 {
1603  ast_mutex_lock(&cond->lock);
1604  while (!cond->done) {
1605  ast_cond_wait(&cond->cond, &cond->lock);
1606  }
1607  ast_mutex_unlock(&cond->lock);
1608 }
1609 
1610 /*
1611  * XXX ASTERISK-21271 make ast_bridge_join() require features to be allocated just like ast_bridge_impart() and not expect the struct back.
1612  *
1613  * This change is really going to break ConfBridge. All other
1614  * users are easily changed. However, it is needed so the
1615  * bridging code can manipulate features on all channels
1616  * consistently no matter how they joined.
1617  *
1618  * Need to update the features parameter doxygen when this
1619  * change is made to be like ast_bridge_impart().
1620  */
1621 int ast_bridge_join(struct ast_bridge *bridge,
1622  struct ast_channel *chan,
1623  struct ast_channel *swap,
1624  struct ast_bridge_features *features,
1625  struct ast_bridge_tech_optimizations *tech_args,
1626  enum ast_bridge_join_flags flags)
1627 {
1628  struct ast_bridge_channel *bridge_channel;
1629  int res = 0;
1630  SCOPE_TRACE(1, "%s Bridge: %s\n", ast_channel_name(chan), bridge->uniqueid);
1631 
1632  bridge_channel = bridge_channel_internal_alloc(bridge);
1633  if (flags & AST_BRIDGE_JOIN_PASS_REFERENCE) {
1634  ao2_ref(bridge, -1);
1635  }
1636  if (!bridge_channel) {
1637  ao2_t_cleanup(swap, "Error exit: bridge_channel alloc failed");
1638  res = -1;
1639  goto join_exit;
1640  }
1641 /* XXX ASTERISK-21271 features cannot be NULL when passed in. When it is changed to allocated we can do like ast_bridge_impart() and allocate one. */
1642  ast_assert(features != NULL);
1643  if (!features) {
1644  ao2_ref(bridge_channel, -1);
1645  ao2_t_cleanup(swap, "Error exit: features is NULL");
1646  res = -1;
1647  goto join_exit;
1648  }
1649  if (tech_args) {
1650  bridge_channel->tech_args = *tech_args;
1651  }
1652 
1653  ast_channel_lock(chan);
1654  if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)) {
1655  res = -1;
1656  } else {
1657  ast_channel_internal_bridge_channel_set(chan, bridge_channel);
1658  }
1659  ast_channel_unlock(chan);
1660  bridge_channel->thread = pthread_self();
1661  bridge_channel->chan = chan;
1662  bridge_channel->swap = swap;
1663  bridge_channel->features = features;
1664  bridge_channel->inhibit_colp = !!(flags & AST_BRIDGE_JOIN_INHIBIT_JOIN_COLP);
1665 
1666  /* allow subclass to peek at upcoming push operation */
1667  if (bridge->v_table->push_peek && !res) {
1668  struct ast_bridge_channel *bcswap = NULL;
1669 
1670  ast_bridge_lock(bridge);
1671  if (bridge_channel->swap) {
1672  bcswap = bridge_find_channel(bridge, bridge_channel->swap);
1673  }
1674  res = bridge->v_table->push_peek(bridge, bridge_channel, bcswap);
1675  ast_bridge_unlock(bridge);
1676  }
1677 
1678  if (!res) {
1679  res = bridge_channel_internal_join(bridge_channel);
1680  }
1681 
1682  /* Cleanup all the data in the bridge channel after it leaves the bridge. */
1683  ast_channel_lock(chan);
1684  ast_channel_internal_bridge_channel_set(chan, NULL);
1685  ast_channel_unlock(chan);
1686  /* Due to a race condition, we lock the bridge channel here for ast_bridge_channel_get_chan */
1687  ao2_lock(bridge_channel);
1688  bridge_channel->chan = NULL;
1689  ao2_unlock(bridge_channel);
1690  /* If bridge_channel->swap is not NULL then the join failed. */
1691  ao2_t_cleanup(bridge_channel->swap, "Bridge complete: join failed");
1692  bridge_channel->swap = NULL;
1693  bridge_channel->features = NULL;
1694 
1695  ao2_ref(bridge_channel, -1);
1696 
1697 join_exit:
1699  bridge_channel_impart_signal(chan);
1700  if (!(ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO)
1701  && !ast_bridge_setup_after_goto(chan)) {
1702  /* Claim the after bridge goto is an async goto destination. */
1703  ast_channel_lock(chan);
1704  ast_softhangup_nolock(chan, AST_SOFTHANGUP_ASYNCGOTO);
1705  ast_channel_unlock(chan);
1706  }
1707  return res;
1708 }
1709 
1710 /*! \brief Thread responsible for imparted bridged channels to be departed */
1711 static void *bridge_channel_depart_thread(void *data)
1712 {
1713  struct ast_bridge_channel *bridge_channel = data;
1714  int res = 0;
1715 
1716  if (bridge_channel->callid) {
1717  ast_callid_threadassoc_add(bridge_channel->callid);
1718  }
1719 
1720  res = bridge_channel_internal_join(bridge_channel);
1721 
1722  /*
1723  * cleanup
1724  *
1725  * If bridge_channel->swap is not NULL then the join failed.
1726  */
1727  ao2_t_cleanup(bridge_channel->swap, "Bridge complete: Departable impart join failed");
1728  bridge_channel->swap = NULL;
1729  ast_bridge_features_destroy(bridge_channel->features);
1730  bridge_channel->features = NULL;
1731 
1732  ast_bridge_discard_after_callback(bridge_channel->chan,
1734  /* If join failed there will be impart threads waiting. */
1735  bridge_channel_impart_signal(bridge_channel->chan);
1736  ast_bridge_discard_after_goto(bridge_channel->chan);
1737 
1738  return NULL;
1739 }
1740 
1741 /*! \brief Thread responsible for independent imparted bridged channels */
1742 static void *bridge_channel_ind_thread(void *data)
1743 {
1744  struct ast_bridge_channel *bridge_channel = data;
1745  struct ast_channel *chan;
1746 
1747  if (bridge_channel->callid) {
1748  ast_callid_threadassoc_add(bridge_channel->callid);
1749  }
1750 
1751  bridge_channel_internal_join(bridge_channel);
1752  chan = bridge_channel->chan;
1753 
1754  /* cleanup */
1755  ast_channel_lock(chan);
1756  ast_channel_internal_bridge_channel_set(chan, NULL);
1757  ast_channel_unlock(chan);
1758  /* Lock here for ast_bridge_channel_get_chan */
1759  ao2_lock(bridge_channel);
1760  bridge_channel->chan = NULL;
1761  ao2_unlock(bridge_channel);
1762  /* If bridge_channel->swap is not NULL then the join failed. */
1763  ao2_t_cleanup(bridge_channel->swap, "Bridge complete: Independent impart join failed");
1764  bridge_channel->swap = NULL;
1765  ast_bridge_features_destroy(bridge_channel->features);
1766  bridge_channel->features = NULL;
1767 
1768  ao2_ref(bridge_channel, -1);
1769 
1771  /* If join failed there will be impart threads waiting. */
1772  bridge_channel_impart_signal(chan);
1774  return NULL;
1775 }
1776 
1777 static int bridge_impart_internal(struct ast_bridge *bridge,
1778  struct ast_channel *chan,
1779  struct ast_channel *swap,
1780  struct ast_bridge_features *features,
1782  struct bridge_channel_impart_cond *cond)
1783 {
1784  int res = 0;
1785  struct ast_bridge_channel *bridge_channel;
1786 
1787  /* Imparted channels cannot have a PBX. */
1788  if (ast_channel_pbx(chan)) {
1789  ast_log(AST_LOG_WARNING, "Channel %s has a PBX thread and cannot be imparted into bridge %s\n",
1790  ast_channel_name(chan), bridge->uniqueid);
1791  ast_bridge_features_destroy(features);
1792  return -1;
1793  }
1794 
1795  /* Supply an empty features structure if the caller did not. */
1796  if (!features) {
1797  features = ast_bridge_features_new();
1798  if (!features) {
1799  return -1;
1800  }
1801  }
1802 
1803  /* Try to allocate a structure for the bridge channel */
1804  bridge_channel = bridge_channel_internal_alloc(bridge);
1805  if (!bridge_channel) {
1806  ast_bridge_features_destroy(features);
1807  return -1;
1808  }
1809 
1810  ast_channel_lock(chan);
1811  if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)) {
1812  ast_log(AST_LOG_NOTICE, "Channel %s is a zombie and cannot be imparted into bridge %s\n",
1813  ast_channel_name(chan), bridge->uniqueid);
1814  res = -1;
1815  } else {
1816  ast_channel_internal_bridge_channel_set(chan, bridge_channel);
1817  }
1818  ast_channel_unlock(chan);
1819  bridge_channel->chan = chan;
1820  bridge_channel->swap = ao2_t_bump(swap, "Setting up bridge impart");
1821  bridge_channel->features = features;
1822  bridge_channel->inhibit_colp = !!(flags & AST_BRIDGE_IMPART_INHIBIT_JOIN_COLP);
1823  bridge_channel->depart_wait =
1825  bridge_channel->callid = ast_read_threadstorage_callid();
1826 
1827  /* allow subclass to peek at swap channel before it can hangup */
1828  if (bridge->v_table->push_peek && !res) {
1829  struct ast_bridge_channel *bcswap = NULL;
1830 
1831  ast_bridge_lock(bridge);
1832  if (bridge_channel->swap) {
1833  bcswap = bridge_find_channel(bridge, bridge_channel->swap);
1834  }
1835  res = bridge->v_table->push_peek(bridge, bridge_channel, bcswap);
1836  ast_bridge_unlock(bridge);
1837  }
1838 
1839  /* Actually create the thread that will handle the channel */
1840  if (!res) {
1841  res = bridge_channel_impart_add(chan, cond);
1842  }
1843  if (!res) {
1845  res = ast_pthread_create_detached(&bridge_channel->thread, NULL,
1846  bridge_channel_ind_thread, bridge_channel);
1847  } else {
1848  res = ast_pthread_create(&bridge_channel->thread, NULL,
1849  bridge_channel_depart_thread, bridge_channel);
1850  }
1851 
1852  if (!res) {
1853  bridge_channel_impart_wait(cond);
1854  }
1855  }
1856 
1857  if (res) {
1858  /* cleanup */
1859  ast_channel_lock(chan);
1860  ast_channel_internal_bridge_channel_set(chan, NULL);
1861  ast_channel_unlock(chan);
1862  /* Lock here for ast_bridge_channel_get_chan */
1863  ao2_lock(bridge_channel);
1864  bridge_channel->chan = NULL;
1865  ao2_unlock(bridge_channel);
1866  ao2_t_cleanup(bridge_channel->swap, "Bridge complete: Impart failed");
1867  bridge_channel->swap = NULL;
1868  ast_bridge_features_destroy(bridge_channel->features);
1869  bridge_channel->features = NULL;
1870 
1871  ao2_ref(bridge_channel, -1);
1872  return -1;
1873  }
1874 
1875  return 0;
1876 }
1877 
1878 int ast_bridge_impart(struct ast_bridge *bridge,
1879  struct ast_channel *chan,
1880  struct ast_channel *swap,
1881  struct ast_bridge_features *features,
1882  enum ast_bridge_impart_flags flags)
1883 {
1884  struct bridge_channel_impart_cond cond = {
1885  .done = 0,
1886  };
1887  int res;
1888  SCOPE_TRACE(1, "%s Bridge: %s\n", ast_channel_name(chan), bridge->uniqueid);
1889 
1890  ast_mutex_init(&cond.lock);
1891  ast_cond_init(&cond.cond, NULL);
1892 
1893  res = bridge_impart_internal(bridge, chan, swap, features, flags, &cond);
1894  if (res) {
1895  /* Impart failed. Signal any other waiting impart threads */
1897  bridge_channel_impart_signal(chan);
1898  }
1899 
1900  ast_cond_destroy(&cond.cond);
1901  ast_mutex_destroy(&cond.lock);
1902 
1903  return res;
1904 }
1905 
1907 {
1908  struct ast_bridge_channel *bridge_channel;
1909  int departable;
1910  SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
1911 
1912  ast_channel_lock(chan);
1913  bridge_channel = ast_channel_internal_bridge_channel(chan);
1914  departable = bridge_channel && bridge_channel->depart_wait;
1915  ast_channel_unlock(chan);
1916  if (!departable) {
1917  ast_log(LOG_ERROR, "Channel %s cannot be departed.\n",
1918  ast_channel_name(chan));
1919  /*
1920  * Should never happen. It likely means that
1921  * ast_bridge_depart() is called by two threads for the same
1922  * channel, the channel was never imparted to be departed, or it
1923  * has already been departed.
1924  */
1925  ast_assert(0);
1926  return -1;
1927  }
1928 
1929  /*
1930  * We are claiming the bridge_channel reference held by
1931  * bridge_channel_depart_thread().
1932  */
1933 
1934  ast_bridge_channel_leave_bridge(bridge_channel,
1935  BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
1936 
1937  /* Wait for the depart thread to die */
1938  ast_debug(1, "Waiting for %p(%s) bridge thread to die.\n",
1939  bridge_channel, ast_channel_name(bridge_channel->chan));
1940  pthread_join(bridge_channel->thread, NULL);
1941 
1942  ast_channel_lock(chan);
1943  ast_channel_internal_bridge_channel_set(chan, NULL);
1944  ast_channel_unlock(chan);
1945 
1946  /* We can get rid of the bridge_channel after the depart thread has died. */
1947  ao2_ref(bridge_channel, -1);
1948  return 0;
1949 }
1950 
1951 int ast_bridge_remove(struct ast_bridge *bridge, struct ast_channel *chan)
1952 {
1953  struct ast_bridge_channel *bridge_channel;
1954 
1955  ast_debug(1, "Removing channel %s from bridge %s\n",
1956  ast_channel_name(chan), bridge->uniqueid);
1957 
1958  ast_bridge_lock(bridge);
1959 
1960  /* Try to find the channel that we want to remove */
1961  if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
1962  ast_bridge_unlock(bridge);
1963  return -1;
1964  }
1965 
1966  ast_bridge_channel_leave_bridge(bridge_channel,
1967  BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
1968 
1969  ast_bridge_unlock(bridge);
1970 
1971  return 0;
1972 }
1973 
1974 static void kick_it(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size)
1975 {
1976  ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
1977 }
1978 
1979 int ast_bridge_kick(struct ast_bridge *bridge, struct ast_channel *chan)
1980 {
1981  struct ast_bridge_channel *bridge_channel;
1982  int res;
1983 
1984  ast_bridge_lock(bridge);
1985 
1986  /* Try to find the channel that we want to kick. */
1987  if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
1988  ast_bridge_unlock(bridge);
1989  return -1;
1990  }
1991 
1992  res = ast_bridge_channel_queue_callback(bridge_channel, 0, kick_it, NULL, 0);
1993 
1994  ast_bridge_unlock(bridge);
1995 
1996  return res;
1997 }
1998 
1999 /*!
2000  * \internal
2001  * \brief Point the bridge_channel to a new bridge.
2002  * \since 12.0.0
2003  *
2004  * \param bridge_channel What is to point to a new bridge.
2005  * \param new_bridge Where the bridge channel should point.
2006  */
2007 static void bridge_channel_change_bridge(struct ast_bridge_channel *bridge_channel, struct ast_bridge *new_bridge)
2008 {
2009  struct ast_bridge *old_bridge;
2010 
2011  ao2_ref(new_bridge, +1);
2012  ast_bridge_channel_lock(bridge_channel);
2013  ast_channel_lock(bridge_channel->chan);
2014  old_bridge = bridge_channel->bridge;
2015  bridge_channel->bridge = new_bridge;
2016  ast_channel_internal_bridge_set(bridge_channel->chan, new_bridge);
2017  ast_channel_unlock(bridge_channel->chan);
2018  ast_bridge_channel_unlock(bridge_channel);
2019  ao2_ref(old_bridge, -1);
2020 }
2021 
2022 static void bridge_channel_moving(struct ast_bridge_channel *bridge_channel, struct ast_bridge *src, struct ast_bridge *dst)
2023 {
2024  struct ast_bridge_features *features = bridge_channel->features;
2025  struct ast_bridge_hook *hook;
2026  struct ao2_iterator iter;
2027 
2028  /* Run any moving hooks. */
2029  iter = ao2_iterator_init(features->other_hooks, 0);
2030  for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
2031  int remove_me;
2033 
2034  if (hook->type != AST_BRIDGE_HOOK_TYPE_MOVE) {
2035  continue;
2036  }
2037  move_cb = (ast_bridge_move_indicate_callback) hook->callback;
2038  remove_me = move_cb(bridge_channel, hook->hook_pvt, src, dst);
2039  if (remove_me) {
2040  ast_debug(1, "Move detection hook %p is being removed from %p(%s)\n",
2041  hook, bridge_channel, ast_channel_name(bridge_channel->chan));
2042  ao2_unlink(features->other_hooks, hook);
2043  }
2044  }
2045  ao2_iterator_destroy(&iter);
2046 }
2047 
2048 void bridge_do_merge(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, struct ast_bridge_channel **kick_me, unsigned int num_kick,
2049  unsigned int optimized)
2050 {
2051  struct ast_bridge_channel *bridge_channel;
2052  unsigned int idx;
2053 
2054  ast_debug(1, "Merging bridge %s into bridge %s\n",
2055  src_bridge->uniqueid, dst_bridge->uniqueid);
2056 
2057  ast_bridge_publish_merge(dst_bridge, src_bridge);
2058 
2059  /*
2060  * Move channels from src_bridge over to dst_bridge.
2061  *
2062  * We must use AST_LIST_TRAVERSE_SAFE_BEGIN() because
2063  * bridge_channel_internal_pull() alters the list we are traversing.
2064  */
2065  AST_LIST_TRAVERSE_SAFE_BEGIN(&src_bridge->channels, bridge_channel, entry) {
2066  if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
2067  /*
2068  * The channel is already leaving let it leave normally because
2069  * pulling it may delete hooks that should run for this channel.
2070  */
2071  continue;
2072  }
2073  if (ast_test_flag(&bridge_channel->features->feature_flags,
2075  continue;
2076  }
2077 
2078  if (kick_me) {
2079  for (idx = 0; idx < num_kick; ++idx) {
2080  if (bridge_channel == kick_me[idx]) {
2081  ast_bridge_channel_leave_bridge(bridge_channel,
2082  BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
2083  break;
2084  }
2085  }
2086  }
2087  bridge_channel_internal_pull(bridge_channel);
2088  if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
2089  /*
2090  * The channel died as a result of being pulled or it was
2091  * kicked. Leave it pointing to the original bridge.
2092  */
2093  continue;
2094  }
2095 
2096  bridge_channel_moving(bridge_channel, bridge_channel->bridge, dst_bridge);
2097 
2098  /* Point to new bridge.*/
2099  bridge_channel_change_bridge(bridge_channel, dst_bridge);
2100 
2101  if (bridge_channel_internal_push(bridge_channel)) {
2102  ast_bridge_features_remove(bridge_channel->features,
2104  ast_bridge_channel_leave_bridge(bridge_channel,
2106  }
2107  }
2109 
2110  if (kick_me) {
2111  /*
2112  * Now we can kick any channels in the dst_bridge without
2113  * potentially dissolving the bridge.
2114  */
2115  for (idx = 0; idx < num_kick; ++idx) {
2116  bridge_channel = kick_me[idx];
2117  ast_bridge_channel_lock(bridge_channel);
2118  if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
2120  BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
2121  bridge_channel_internal_pull(bridge_channel);
2122  }
2123  ast_bridge_channel_unlock(bridge_channel);
2124  }
2125  }
2126 
2127  bridge_reconfigured(dst_bridge, !optimized);
2128  bridge_reconfigured(src_bridge, !optimized);
2129 
2130  ast_debug(1, "Merged bridge %s into bridge %s\n",
2131  src_bridge->uniqueid, dst_bridge->uniqueid);
2132 }
2133 
2135  /*! Destination merge bridge. */
2136  struct ast_bridge *dest;
2137  /*! Source merge bridge. */
2138  struct ast_bridge *src;
2139 };
2140 
2141 /*!
2142  * \internal
2143  * \brief Determine which bridge should merge into the other.
2144  * \since 12.0.0
2145  *
2146  * \param bridge1 A bridge for merging
2147  * \param bridge2 A bridge for merging
2148  *
2149  * \note The two bridges are assumed already locked.
2150  *
2151  * \return Which bridge merges into which or NULL bridges if cannot merge.
2152  */
2153 static struct merge_direction bridge_merge_determine_direction(struct ast_bridge *bridge1, struct ast_bridge *bridge2)
2154 {
2155  struct merge_direction merge = { NULL, NULL };
2156  int bridge1_priority;
2157  int bridge2_priority;
2158 
2159  if (!ast_test_flag(&bridge1->feature_flags,
2161  && !ast_test_flag(&bridge2->feature_flags,
2163  /*
2164  * Can merge either way. Merge to the higher priority merge
2165  * bridge. Otherwise merge to the larger bridge.
2166  */
2167  bridge1_priority = bridge1->v_table->get_merge_priority(bridge1);
2168  bridge2_priority = bridge2->v_table->get_merge_priority(bridge2);
2169  if (bridge2_priority < bridge1_priority) {
2170  merge.dest = bridge1;
2171  merge.src = bridge2;
2172  } else if (bridge1_priority < bridge2_priority) {
2173  merge.dest = bridge2;
2174  merge.src = bridge1;
2175  } else {
2176  /* Merge to the larger bridge. */
2177  if (bridge2->num_channels <= bridge1->num_channels) {
2178  merge.dest = bridge1;
2179  merge.src = bridge2;
2180  } else {
2181  merge.dest = bridge2;
2182  merge.src = bridge1;
2183  }
2184  }
2185  } else if (!ast_test_flag(&bridge1->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_TO)
2186  && !ast_test_flag(&bridge2->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)) {
2187  /* Can merge only one way. */
2188  merge.dest = bridge1;
2189  merge.src = bridge2;
2190  } else if (!ast_test_flag(&bridge2->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_TO)
2191  && !ast_test_flag(&bridge1->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)) {
2192  /* Can merge only one way. */
2193  merge.dest = bridge2;
2194  merge.src = bridge1;
2195  }
2196 
2197  return merge;
2198 }
2199 
2200 /*!
2201  * \internal
2202  * \brief Merge two bridges together
2203  * \since 12.0.0
2204  *
2205  * \param dst_bridge Destination bridge of merge.
2206  * \param src_bridge Source bridge of merge.
2207  * \param merge_best_direction TRUE if don't care about which bridge merges into the other.
2208  * \param kick_me Array of channels to kick from the bridges.
2209  * \param num_kick Number of channels in the kick_me array.
2210  *
2211  * \note The dst_bridge and src_bridge are assumed already locked.
2212  *
2213  * \retval 0 on success
2214  * \retval -1 on failure
2215  */
2216 static int bridge_merge_locked(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, int merge_best_direction, struct ast_channel **kick_me, unsigned int num_kick)
2217 {
2218  struct merge_direction merge;
2219  struct ast_bridge_channel **kick_them = NULL;
2220 
2221  /* Sanity check. */
2222  ast_assert(dst_bridge && src_bridge && dst_bridge != src_bridge && (!num_kick || kick_me));
2223 
2224  if (dst_bridge->dissolved || src_bridge->dissolved) {
2225  ast_debug(1, "Can't merge bridges %s and %s, at least one bridge is dissolved.\n",
2226  src_bridge->uniqueid, dst_bridge->uniqueid);
2227  return -1;
2228  }
2229  if (ast_test_flag(&dst_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)
2230  || ast_test_flag(&src_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)) {
2231  ast_debug(1, "Can't merge bridges %s and %s, masquerade only.\n",
2232  src_bridge->uniqueid, dst_bridge->uniqueid);
2233  return -1;
2234  }
2235  if (dst_bridge->inhibit_merge || src_bridge->inhibit_merge) {
2236  ast_debug(1, "Can't merge bridges %s and %s, merging temporarily inhibited.\n",
2237  src_bridge->uniqueid, dst_bridge->uniqueid);
2238  return -1;
2239  }
2240 
2241  if (merge_best_direction) {
2242  merge = bridge_merge_determine_direction(dst_bridge, src_bridge);
2243  } else {
2244  merge.dest = dst_bridge;
2245  merge.src = src_bridge;
2246  }
2247 
2248  if (!merge.dest
2249  || ast_test_flag(&merge.dest->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_TO)
2250  || ast_test_flag(&merge.src->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)) {
2251  ast_debug(1, "Can't merge bridges %s and %s, merging inhibited.\n",
2252  src_bridge->uniqueid, dst_bridge->uniqueid);
2253  return -1;
2254  }
2255  if (merge.src->num_channels < 2) {
2256  /*
2257  * For a two party bridge, a channel may be temporarily removed
2258  * from the source bridge or the initial bridge members have not
2259  * joined yet.
2260  */
2261  ast_debug(1, "Can't merge bridge %s into bridge %s, not enough channels in source bridge.\n",
2262  merge.src->uniqueid, merge.dest->uniqueid);
2263  return -1;
2264  }
2265  if (2 + num_kick < merge.dest->num_channels + merge.src->num_channels
2267  && (!ast_test_flag(&merge.dest->feature_flags, AST_BRIDGE_FLAG_SMART)
2269  ast_debug(1, "Can't merge bridge %s into bridge %s, multimix is needed and it cannot be acquired.\n",
2270  merge.src->uniqueid, merge.dest->uniqueid);
2271  return -1;
2272  }
2273 
2274  if (num_kick) {
2275  unsigned int num_to_kick = 0;
2276  unsigned int idx;
2277 
2278  kick_them = ast_alloca(num_kick * sizeof(*kick_them));
2279  for (idx = 0; idx < num_kick; ++idx) {
2280  kick_them[num_to_kick] = bridge_find_channel(merge.src, kick_me[idx]);
2281  if (!kick_them[num_to_kick]) {
2282  kick_them[num_to_kick] = bridge_find_channel(merge.dest, kick_me[idx]);
2283  }
2284  if (kick_them[num_to_kick]) {
2285  ++num_to_kick;
2286  }
2287  }
2288 
2289  if (num_to_kick != num_kick) {
2290  ast_debug(1, "Can't merge bridge %s into bridge %s, at least one kicked channel is not in either bridge.\n",
2291  merge.src->uniqueid, merge.dest->uniqueid);
2292  return -1;
2293  }
2294  }
2295 
2296  bridge_do_merge(merge.dest, merge.src, kick_them, num_kick, 0);
2297  return 0;
2298 }
2299 
2300 int ast_bridge_merge(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, int merge_best_direction, struct ast_channel **kick_me, unsigned int num_kick)
2301 {
2302  int res;
2303 
2304  /* Sanity check. */
2305  ast_assert(dst_bridge && src_bridge);
2306 
2307  ast_bridge_lock_both(dst_bridge, src_bridge);
2308  res = bridge_merge_locked(dst_bridge, src_bridge, merge_best_direction, kick_me, num_kick);
2309  ast_bridge_unlock(src_bridge);
2310  ast_bridge_unlock(dst_bridge);
2311  return res;
2312 }
2313 
2314 int bridge_do_move(struct ast_bridge *dst_bridge, struct ast_bridge_channel *bridge_channel, int attempt_recovery,
2315  unsigned int optimized)
2316 {
2317  struct ast_bridge *orig_bridge;
2318  int was_in_bridge;
2319  int res = 0;
2320 
2321  if (bridge_channel->swap) {
2322  ast_debug(1, "Moving %p(%s) into bridge %s swapping with %s\n",
2323  bridge_channel, ast_channel_name(bridge_channel->chan), dst_bridge->uniqueid,
2324  ast_channel_name(bridge_channel->swap));
2325  } else {
2326  ast_debug(1, "Moving %p(%s) into bridge %s\n",
2327  bridge_channel, ast_channel_name(bridge_channel->chan), dst_bridge->uniqueid);
2328  }
2329 
2330  orig_bridge = bridge_channel->bridge;
2331  was_in_bridge = bridge_channel->in_bridge;
2332 
2333  bridge_channel_internal_pull(bridge_channel);
2334  if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
2335  /*
2336  * The channel died as a result of being pulled. Leave it
2337  * pointing to the original bridge.
2338  *
2339  * Clear out the swap channel pointer. A ref is not held
2340  * by bridge_channel->swap at this point.
2341  */
2342  bridge_channel->swap = NULL;
2343  bridge_reconfigured(orig_bridge, 0);
2344  return -1;
2345  }
2346 
2347  /* Point to new bridge.*/
2348  ao2_ref(orig_bridge, +1);/* Keep a ref in case the push fails. */
2349  bridge_channel_change_bridge(bridge_channel, dst_bridge);
2350 
2351  bridge_channel_moving(bridge_channel, orig_bridge, dst_bridge);
2352 
2353  if (bridge_channel_internal_push_full(bridge_channel, optimized)) {
2354  /* Try to put the channel back into the original bridge. */
2355  ast_bridge_features_remove(bridge_channel->features,
2357  if (attempt_recovery && was_in_bridge) {
2358  /* Point back to original bridge. */
2359  bridge_channel_change_bridge(bridge_channel, orig_bridge);
2360 
2361  if (bridge_channel_internal_push(bridge_channel)) {
2362  ast_bridge_features_remove(bridge_channel->features,
2364  ast_bridge_channel_leave_bridge(bridge_channel,
2366  }
2367  } else {
2368  ast_bridge_channel_leave_bridge(bridge_channel,
2370  bridge_channel_settle_owed_events(orig_bridge, bridge_channel);
2371  }
2372  res = -1;
2373  } else if (!optimized) {
2374  bridge_channel_settle_owed_events(orig_bridge, bridge_channel);
2375  }
2376 
2377  bridge_reconfigured(dst_bridge, !optimized);
2378  bridge_reconfigured(orig_bridge, !optimized);
2379  ao2_ref(orig_bridge, -1);
2380  return res;
2381 }
2382 
2383 /*!
2384  * \internal
2385  * \brief Move a channel from one bridge to another.
2386  * \since 12.0.0
2387  *
2388  * \param dst_bridge Destination bridge of bridge channel move.
2389  * \param src_bridge Source bridge of bridge channel move.
2390  * \param chan Channel to move.
2391  * \param swap Channel to replace in dst_bridge.
2392  * \param attempt_recovery TRUE if failure attempts to push channel back into original bridge.
2393  *
2394  * \note The dst_bridge and src_bridge are assumed already locked.
2395  *
2396  * \retval 0 on success.
2397  * \retval -1 on failure.
2398  */
2399 static int bridge_move_locked(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, struct ast_channel *chan, struct ast_channel *swap, int attempt_recovery)
2400 {
2401  struct ast_bridge_channel *bridge_channel;
2402 
2403  if (dst_bridge->dissolved || src_bridge->dissolved) {
2404  ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, at least one bridge is dissolved.\n",
2405  ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
2406  return -1;
2407  }
2408  if (ast_test_flag(&dst_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)
2409  || ast_test_flag(&src_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)) {
2410  ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, masquerade only.\n",
2411  ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
2412  return -1;
2413  }
2414  if (dst_bridge->inhibit_merge || src_bridge->inhibit_merge) {
2415  ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, temporarily inhibited.\n",
2416  ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
2417  return -1;
2418  }
2419 
2420  bridge_channel = bridge_find_channel(src_bridge, chan);
2421  if (!bridge_channel) {
2422  ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, channel not in bridge.\n",
2423  ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
2424  return -1;
2425  }
2426  if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
2427  ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, channel leaving bridge.\n",
2428  ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
2429  return -1;
2430  }
2431  if (ast_test_flag(&bridge_channel->features->feature_flags,
2433  ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, channel immovable.\n",
2434  ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
2435  return -1;
2436  }
2437 
2438  if (swap) {
2439  struct ast_bridge_channel *bridge_channel_swap;
2440 
2441  bridge_channel_swap = bridge_find_channel(dst_bridge, swap);
2442  if (!bridge_channel_swap) {
2443  ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, swap channel %s not in bridge.\n",
2444  ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid,
2445  ast_channel_name(swap));
2446  return -1;
2447  }
2448  if (bridge_channel_swap->state != BRIDGE_CHANNEL_STATE_WAIT) {
2449  ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, swap channel %s leaving bridge.\n",
2450  ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid,
2451  ast_channel_name(swap));
2452  return -1;
2453  }
2454  }
2455 
2456  bridge_channel->swap = swap;
2457  return bridge_do_move(dst_bridge, bridge_channel, attempt_recovery, 0);
2458 }
2459 
2460 int ast_bridge_move(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, struct ast_channel *chan, struct ast_channel *swap, int attempt_recovery)
2461 {
2462  int res;
2463 
2464  ast_bridge_lock_both(dst_bridge, src_bridge);
2465  res = bridge_move_locked(dst_bridge, src_bridge, chan, swap, attempt_recovery);
2466  ast_bridge_unlock(src_bridge);
2467  ast_bridge_unlock(dst_bridge);
2468  return res;
2469 }
2470 
2471 int ast_bridge_add_channel(struct ast_bridge *bridge, struct ast_channel *chan,
2472  struct ast_bridge_features *features, int play_tone, const char *xfersound)
2473 {
2474  RAII_VAR(struct ast_bridge *, chan_bridge, NULL, ao2_cleanup);
2475  RAII_VAR(struct ast_channel *, yanked_chan, NULL, ao2_cleanup);
2476 
2477  ast_moh_stop(chan);
2478 
2479  ast_channel_lock(chan);
2480  chan_bridge = ast_channel_get_bridge(chan);
2481  ast_channel_unlock(chan);
2482 
2483  if (chan_bridge) {
2484  struct ast_bridge_channel *bridge_channel;
2485 
2486  /* The channel is in a bridge so it is not getting any new features. */
2487  ast_bridge_features_destroy(features);
2488 
2489  ast_bridge_lock_both(bridge, chan_bridge);
2490  bridge_channel = bridge_find_channel(chan_bridge, chan);
2491 
2492  if (bridge_move_locked(bridge, chan_bridge, chan, NULL, 1)) {
2493  ast_bridge_unlock(chan_bridge);
2494  ast_bridge_unlock(bridge);
2495  return -1;
2496  }
2497 
2498  /*
2499  * bridge_move_locked() will implicitly ensure that
2500  * bridge_channel is not NULL.
2501  */
2502  ast_assert(bridge_channel != NULL);
2503 
2504  /*
2505  * Additional checks if the channel we just stole dissolves the
2506  * original bridge.
2507  */
2508  bridge_dissolve_check_stolen(chan_bridge, bridge_channel);
2509  ast_bridge_unlock(chan_bridge);
2510  ast_bridge_unlock(bridge);
2511  } else {
2512  /* Slightly less easy case. We need to yank channel A from
2513  * where he currently is and impart him into our bridge.
2514  */
2515  yanked_chan = ast_channel_yank(chan);
2516  if (!yanked_chan) {
2517  ast_log(LOG_WARNING, "Could not gain control of channel %s\n", ast_channel_name(chan));
2518  ast_bridge_features_destroy(features);
2519  return -1;
2520  }
2521  if (ast_channel_state(yanked_chan) != AST_STATE_UP) {
2522  ast_answer(yanked_chan);
2523  }
2524  ast_channel_ref(yanked_chan);
2525  if (ast_bridge_impart(bridge, yanked_chan, NULL, features,
2527  /* It is possible for us to yank a channel and have some other
2528  * thread start a PBX on the channel after we yanked it. In particular,
2529  * this can theoretically happen on the ;2 of a Local channel if we
2530  * yank it prior to the ;1 being answered. Make sure that it isn't
2531  * executing a PBX before hanging it up.
2532  */
2533  if (ast_channel_pbx(yanked_chan)) {
2534  ast_channel_unref(yanked_chan);
2535  } else {
2536  ast_hangup(yanked_chan);
2537  }
2538  return -1;
2539  }
2540  }
2541 
2542  if (play_tone && !ast_strlen_zero(xfersound)) {
2543  struct ast_channel *play_chan = yanked_chan ?: chan;
2544  RAII_VAR(struct ast_bridge_channel *, play_bridge_channel, NULL, ao2_cleanup);
2545 
2546  ast_channel_lock(play_chan);
2547  play_bridge_channel = ast_channel_get_bridge_channel(play_chan);
2548  ast_channel_unlock(play_chan);
2549 
2550  if (!play_bridge_channel) {
2551  ast_log(LOG_WARNING, "Unable to play tone for channel %s. No longer in a bridge.\n",
2552  ast_channel_name(play_chan));
2553  } else {
2554  ast_bridge_channel_queue_playfile(play_bridge_channel, NULL, xfersound, NULL);
2555  }
2556  }
2557  return 0;
2558 }
2559 
2560 static int bridge_allows_optimization(struct ast_bridge *bridge)
2561 {
2562  return !(bridge->inhibit_merge
2563  || bridge->dissolved
2564  || ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY));
2565 }
2566 
2567 /*!
2568  * \internal
2569  * \brief Lock the unreal channel stack for chan and prequalify it.
2570  * \since 12.0.0
2571  *
2572  * \param chan Unreal channel writing a frame into the channel driver.
2573  *
2574  * \note It is assumed that chan is already locked.
2575  *
2576  * \return bridge on success with bridge and bridge_channel locked.
2577  * \retval NULL if cannot do optimization now.
2578  */
2579 static struct ast_bridge *optimize_lock_chan_stack(struct ast_channel *chan)
2580 {
2581  struct ast_bridge *bridge;
2582  struct ast_bridge_channel *bridge_channel;
2583 
2584  if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
2585  return NULL;
2586  }
2587  if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF)) {
2588  return NULL;
2589  }
2591  /* Channel has an active monitor, audiohook, or framehook. */
2592  return NULL;
2593  }
2594  bridge_channel = ast_channel_internal_bridge_channel(chan);
2595  if (!bridge_channel || ast_bridge_channel_trylock(bridge_channel)) {
2596  return NULL;
2597  }
2598  bridge = bridge_channel->bridge;
2599  if (bridge_channel->activity != BRIDGE_CHANNEL_THREAD_SIMPLE
2600  || bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT
2601  || ast_bridge_trylock(bridge)) {
2602  ast_bridge_channel_unlock(bridge_channel);
2603  return NULL;
2604  }
2605  if (!bridge_channel_internal_allows_optimization(bridge_channel) ||
2606  !bridge_allows_optimization(bridge)) {
2607  ast_bridge_unlock(bridge);
2608  ast_bridge_channel_unlock(bridge_channel);
2609  return NULL;
2610  }
2611  return bridge;
2612 }
2613 
2614 /*!
2615  * \internal
2616  * \brief Lock the unreal channel stack for peer and prequalify it.
2617  * \since 12.0.0
2618  *
2619  * \param peer Other unreal channel in the pair.
2620  *
2621  * \return bridge on success with bridge, bridge_channel, and peer locked.
2622  * \retval NULL if cannot do optimization now.
2623  */
2624 static struct ast_bridge *optimize_lock_peer_stack(struct ast_channel *peer)
2625 {
2626  struct ast_bridge *bridge;
2627  struct ast_bridge_channel *bridge_channel;
2628 
2629  if (ast_channel_trylock(peer)) {
2630  return NULL;
2631  }
2632  if (!AST_LIST_EMPTY(ast_channel_readq(peer))) {
2633  ast_channel_unlock(peer);
2634  return NULL;
2635  }
2636  if (ast_test_flag(ast_channel_flags(peer), AST_FLAG_EMULATE_DTMF)) {
2637  ast_channel_unlock(peer);
2638  return NULL;
2639  }
2641  /* Peer has an active monitor, audiohook, or framehook. */
2642  ast_channel_unlock(peer);
2643  return NULL;
2644  }
2645  bridge_channel = ast_channel_internal_bridge_channel(peer);
2646  if (!bridge_channel || ast_bridge_channel_trylock(bridge_channel)) {
2647  ast_channel_unlock(peer);
2648  return NULL;
2649  }
2650  bridge = bridge_channel->bridge;
2651  if (bridge_channel->activity != BRIDGE_CHANNEL_THREAD_IDLE
2652  || bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT
2653  || ast_bridge_trylock(bridge)) {
2654  ast_bridge_channel_unlock(bridge_channel);
2655  ast_channel_unlock(peer);
2656  return NULL;
2657  }
2658  if (!bridge_allows_optimization(bridge) ||
2659  !bridge_channel_internal_allows_optimization(bridge_channel)) {
2660  ast_bridge_unlock(bridge);
2661  ast_bridge_channel_unlock(bridge_channel);
2662  ast_channel_unlock(peer);
2663  return NULL;
2664  }
2665  return bridge;
2666 }
2667 
2668 /*!
2669  * \internal
2670  * \brief Indicates allowability of a swap optimization
2671  */
2673  /*! Bridges cannot allow for a swap optimization to occur */
2675  /*! Bridge swap optimization can occur into the chan_bridge */
2677  /*! Bridge swap optimization can occur into the peer_bridge */
2679 };
2680 
2681 /*!
2682  * \internal
2683  * \brief Determine if two bridges allow for swap optimization to occur
2684  *
2685  * \param chan_bridge First bridge being tested
2686  * \param peer_bridge Second bridge being tested
2687  * \return Allowability of swap optimization
2688  */
2689 static enum bridge_allow_swap bridges_allow_swap_optimization(struct ast_bridge *chan_bridge,
2690  struct ast_bridge *peer_bridge)
2691 {
2692  int chan_priority;
2693  int peer_priority;
2694 
2695  if (!ast_test_flag(&chan_bridge->feature_flags,
2698  && !ast_test_flag(&peer_bridge->feature_flags,
2701  /*
2702  * Can swap either way. Swap to the higher priority merge
2703  * bridge.
2704  */
2705  chan_priority = chan_bridge->v_table->get_merge_priority(chan_bridge);
2706  peer_priority = peer_bridge->v_table->get_merge_priority(peer_bridge);
2707  if (chan_bridge->num_channels == 2
2708  && chan_priority <= peer_priority) {
2709  return SWAP_TO_PEER_BRIDGE;
2710  } else if (peer_bridge->num_channels == 2
2711  && peer_priority <= chan_priority) {
2712  return SWAP_TO_CHAN_BRIDGE;
2713  }
2714  } else if (chan_bridge->num_channels == 2
2716  && !ast_test_flag(&peer_bridge->feature_flags, AST_BRIDGE_FLAG_SWAP_INHIBIT_TO)) {
2717  /* Can swap optimize only one way. */
2718  return SWAP_TO_PEER_BRIDGE;
2719  } else if (peer_bridge->num_channels == 2
2721  && !ast_test_flag(&chan_bridge->feature_flags, AST_BRIDGE_FLAG_SWAP_INHIBIT_TO)) {
2722  /* Can swap optimize only one way. */
2723  return SWAP_TO_CHAN_BRIDGE;
2724  }
2725 
2726  return SWAP_PROHIBITED;
2727 }
2728 
2729 /*!
2730  * \internal
2731  * \brief Check and attempt to swap optimize out the unreal channels.
2732  * \since 12.0.0
2733  *
2734  * \param chan_bridge
2735  * \param chan_bridge_channel
2736  * \param peer_bridge
2737  * \param peer_bridge_channel
2738  * \param pvt Unreal data containing callbacks to call if the optimization actually
2739  * happens
2740  *
2741  * \retval 1 if unreal channels failed to optimize out.
2742  * \retval 0 if unreal channels were not optimized out.
2743  * \retval -1 if unreal channels were optimized out.
2744  */
2745 static int try_swap_optimize_out(struct ast_bridge *chan_bridge,
2746  struct ast_bridge_channel *chan_bridge_channel, struct ast_bridge *peer_bridge,
2747  struct ast_bridge_channel *peer_bridge_channel,
2748  struct ast_unreal_pvt *pvt)
2749 {
2750  struct ast_bridge *dst_bridge;
2751  struct ast_bridge_channel *dst_bridge_channel;
2752  struct ast_bridge_channel *src_bridge_channel;
2753  struct ast_bridge_channel *other;
2754  int res = 1;
2755 
2756  switch (bridges_allow_swap_optimization(chan_bridge, peer_bridge)) {
2757  case SWAP_TO_CHAN_BRIDGE:
2758  dst_bridge = chan_bridge;
2759  dst_bridge_channel = chan_bridge_channel;
2760  src_bridge_channel = peer_bridge_channel;
2761  break;
2762  case SWAP_TO_PEER_BRIDGE:
2763  dst_bridge = peer_bridge;
2764  dst_bridge_channel = peer_bridge_channel;
2765  src_bridge_channel = chan_bridge_channel;
2766  break;
2767  case SWAP_PROHIBITED:
2768  default:
2769  return 0;
2770  }
2771 
2772  other = ast_bridge_channel_peer(src_bridge_channel);
2773  if (other && other->state == BRIDGE_CHANNEL_STATE_WAIT) {
2774  unsigned int id;
2775 
2776  if (ast_channel_trylock(other->chan)) {
2777  return 1;
2778  }
2779 
2780  id = ast_atomic_fetchadd_int((int *) &optimization_id, +1);
2781 
2782  ast_verb(4, "Move-swap optimizing %s <-- %s.\n",
2783  ast_channel_name(dst_bridge_channel->chan),
2784  ast_channel_name(other->chan));
2785 
2786  if (pvt && !ast_test_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN) && pvt->callbacks
2787  && pvt->callbacks->optimization_started) {
2788  pvt->callbacks->optimization_started(pvt, other->chan,
2789  dst_bridge_channel->chan == pvt->owner ? AST_UNREAL_OWNER : AST_UNREAL_CHAN,
2790  id);
2791  ast_set_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN);
2792  }
2793  other->swap = dst_bridge_channel->chan;
2794  if (!bridge_do_move(dst_bridge, other, 1, 1)) {
2795  ast_bridge_channel_leave_bridge(src_bridge_channel,
2796  BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
2797  res = -1;
2798  }
2799  if (pvt && pvt->callbacks && pvt->callbacks->optimization_finished) {
2800  pvt->callbacks->optimization_finished(pvt, res == 1, id);
2801  }
2802  ast_channel_unlock(other->chan);
2803  }
2804  return res;
2805 }
2806 
2807 /*!
2808  * \internal
2809  * \brief Indicates allowability of a merge optimization
2810  */
2812  /*! Bridge properties prohibit merge optimization */
2814  /*! Merge optimization cannot occur because the source bridge has too few channels */
2816  /*! Merge optimization cannot occur because multimix capability could not be requested */
2818  /*! Merge optimization allowed between bridges */
2820 };
2821 
2822 /*!
2823  * \internal
2824  * \brief Determines allowability of a merge optimization
2825  *
2826  * \note The merge output parameter is undefined if MERGE_PROHIBITED is returned. For success
2827  * and other failure returns, a merge direction was determined, and the parameter is safe to
2828  * access.
2829  *
2830  * \param chan_bridge First bridge being tested
2831  * \param peer_bridge Second bridge being tested
2832  * \param num_kick_channels The number of channels to remove from the bridges during merging
2833  * \param[out] merge Indicates the recommended direction for the bridge merge
2834  */
2835 static enum bridge_allow_merge bridges_allow_merge_optimization(struct ast_bridge *chan_bridge,
2836  struct ast_bridge *peer_bridge, int num_kick_channels, struct merge_direction *merge)
2837 {
2838  *merge = bridge_merge_determine_direction(chan_bridge, peer_bridge);
2839  if (!merge->dest) {
2840  return MERGE_PROHIBITED;
2841  }
2842  if (merge->src->num_channels < 2) {
2844  } else if ((2 + num_kick_channels) < merge->dest->num_channels + merge->src->num_channels
2846  && (!ast_test_flag(&merge->dest->feature_flags, AST_BRIDGE_FLAG_SMART)
2848  return MERGE_NO_MULTIMIX;
2849  }
2850 
2851  return MERGE_ALLOWED;
2852 }
2853 
2854 /*!
2855  * \internal
2856  * \brief Check and attempt to merge optimize out the unreal channels.
2857  * \since 12.0.0
2858  *
2859  * \param chan_bridge
2860  * \param chan_bridge_channel
2861  * \param peer_bridge
2862  * \param peer_bridge_channel
2863  * \param pvt Unreal data containing callbacks to call if the optimization actually
2864  * happens
2865  *
2866  * \retval 0 if unreal channels were not optimized out.
2867  * \retval -1 if unreal channels were optimized out.
2868  */
2869 static int try_merge_optimize_out(struct ast_bridge *chan_bridge,
2870  struct ast_bridge_channel *chan_bridge_channel, struct ast_bridge *peer_bridge,
2871  struct ast_bridge_channel *peer_bridge_channel,
2872  struct ast_unreal_pvt *pvt)
2873 {
2874  struct merge_direction merge;
2875  struct ast_bridge_channel *kick_me[] = {
2876  chan_bridge_channel,
2877  peer_bridge_channel,
2878  };
2879  unsigned int id;
2880 
2881  switch (bridges_allow_merge_optimization(chan_bridge, peer_bridge, ARRAY_LEN(kick_me), &merge)) {
2882  case MERGE_ALLOWED:
2883  break;
2884  case MERGE_PROHIBITED:
2885  return 0;
2887  ast_debug(4, "Can't optimize %s -- %s out, not enough channels in bridge %s.\n",
2888  ast_channel_name(chan_bridge_channel->chan),
2889  ast_channel_name(peer_bridge_channel->chan),
2890  merge.src->uniqueid);
2891  return 0;
2892  case MERGE_NO_MULTIMIX:
2893  ast_debug(4, "Can't optimize %s -- %s out, multimix is needed and it cannot be acquired.\n",
2894  ast_channel_name(chan_bridge_channel->chan),
2895  ast_channel_name(peer_bridge_channel->chan));
2896  return 0;
2897  }
2898 
2899  ast_verb(4, "Merge optimizing %s -- %s out.\n",
2900  ast_channel_name(chan_bridge_channel->chan),
2901  ast_channel_name(peer_bridge_channel->chan));
2902 
2903  id = ast_atomic_fetchadd_int((int *) &optimization_id, +1);
2904 
2905  if (pvt && !ast_test_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN) && pvt->callbacks
2906  && pvt->callbacks->optimization_started) {
2907  pvt->callbacks->optimization_started(pvt, NULL,
2908  merge.dest == ast_channel_internal_bridge(pvt->owner) ? AST_UNREAL_OWNER : AST_UNREAL_CHAN,
2909  id);
2910  ast_set_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN);
2911  }
2912  bridge_do_merge(merge.dest, merge.src, kick_me, ARRAY_LEN(kick_me), 1);
2913  if (pvt && pvt->callbacks && pvt->callbacks->optimization_finished) {
2914  pvt->callbacks->optimization_finished(pvt, 1, id);
2915  }
2916 
2917  return -1;
2918 }
2919 
2920 int ast_bridge_unreal_optimize_out(struct ast_channel *chan, struct ast_channel *peer, struct ast_unreal_pvt *pvt)
2921 {
2922  struct ast_bridge *chan_bridge;
2923  struct ast_bridge *peer_bridge;
2924  struct ast_bridge_channel *chan_bridge_channel;
2925  struct ast_bridge_channel *peer_bridge_channel;
2926  int res = 0;
2927 
2928  chan_bridge = optimize_lock_chan_stack(chan);
2929  if (!chan_bridge) {
2930  return res;
2931  }
2932  chan_bridge_channel = ast_channel_internal_bridge_channel(chan);
2933 
2934  peer_bridge = optimize_lock_peer_stack(peer);
2935  if (peer_bridge) {
2936  peer_bridge_channel = ast_channel_internal_bridge_channel(peer);
2937 
2938  res = try_swap_optimize_out(chan_bridge, chan_bridge_channel,
2939  peer_bridge, peer_bridge_channel, pvt);
2940  if (!res) {
2941  res = try_merge_optimize_out(chan_bridge, chan_bridge_channel,
2942  peer_bridge, peer_bridge_channel, pvt);
2943  } else if (0 < res) {
2944  res = 0;
2945  }
2946 
2947  /* Release peer locks. */
2948  ast_bridge_unlock(peer_bridge);
2949  ast_bridge_channel_unlock(peer_bridge_channel);
2950  ast_channel_unlock(peer);
2951  }
2952 
2953  /* Release chan locks. */
2954  ast_bridge_unlock(chan_bridge);
2955  ast_bridge_channel_unlock(chan_bridge_channel);
2956 
2957  return res;
2958 }
2959 
2961  struct ast_bridge *peer_bridge)
2962 {
2963  struct merge_direction merge;
2964 
2965  if (!bridge_allows_optimization(chan_bridge) || !bridge_allows_optimization(peer_bridge)) {
2967  }
2968 
2969  switch (bridges_allow_swap_optimization(chan_bridge, peer_bridge)) {
2970  case SWAP_TO_CHAN_BRIDGE:
2972  case SWAP_TO_PEER_BRIDGE:
2974  case SWAP_PROHIBITED:
2975  default:
2976  break;
2977  }
2978 
2979  /* Two channels will be kicked from the bridges, the unreal;1 and unreal;2 channels */
2980  if (bridges_allow_merge_optimization(chan_bridge, peer_bridge, 2, &merge) != MERGE_ALLOWED) {
2982  }
2983 
2984  if (merge.dest == chan_bridge) {
2986  } else {
2988  }
2989 }
2990 
2991 void bridge_merge_inhibit_nolock(struct ast_bridge *bridge, int request)
2992 {
2993  int new_request;
2994 
2995  new_request = bridge->inhibit_merge + request;
2996  ast_assert(0 <= new_request);
2997  bridge->inhibit_merge = new_request;
2998 }
2999 
3000 void ast_bridge_merge_inhibit(struct ast_bridge *bridge, int request)
3001 {
3002  ast_bridge_lock(bridge);
3003  bridge_merge_inhibit_nolock(bridge, request);
3004  ast_bridge_unlock(bridge);
3005 }
3006 
3007 int ast_bridge_suspend(struct ast_bridge *bridge, struct ast_channel *chan)
3008 {
3009  struct ast_bridge_channel *bridge_channel;
3010 /* XXX ASTERISK-21271 the case of a dissolved bridge while channel is suspended is not handled. */
3011 /* XXX ASTERISK-21271 suspend/unsuspend needs to be rethought. The caller must block until it has successfully suspended the channel for temporary control. */
3012 /* XXX ASTERISK-21271 external suspend/unsuspend needs to be eliminated. The channel may be playing a file at the time and stealing it then is not good. */
3013 
3014  ast_bridge_lock(bridge);
3015 
3016  if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
3017  ast_bridge_unlock(bridge);
3018  return -1;
3019  }
3020 
3021  bridge_channel_internal_suspend_nolock(bridge_channel);
3022 
3023  ast_bridge_unlock(bridge);
3024 
3025  return 0;
3026 }
3027 
3028 int ast_bridge_unsuspend(struct ast_bridge *bridge, struct ast_channel *chan)
3029 {
3030  struct ast_bridge_channel *bridge_channel;
3031 /* XXX ASTERISK-21271 the case of a dissolved bridge while channel is suspended is not handled. */
3032 
3033  ast_bridge_lock(bridge);
3034 
3035  if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
3036  ast_bridge_unlock(bridge);
3037  return -1;
3038  }
3039 
3040  bridge_channel_internal_unsuspend_nolock(bridge_channel);
3041 
3042  ast_bridge_unlock(bridge);
3043 
3044  return 0;
3045 }
3046 
3048 {
3049  technology->suspended = 1;
3050 }
3051 
3053 {
3054  /*
3055  * XXX We may want the act of unsuspending a bridge technology
3056  * to prod all existing bridges to see if they should start
3057  * using it.
3058  */
3059  technology->suspended = 0;
3060 }
3061 
3063 {
3064  if (ARRAY_LEN(builtin_features_handlers) <= feature
3065  || builtin_features_handlers[feature]) {
3066  return -1;
3067  }
3068 
3069  if (!ast_strlen_zero(dtmf)) {
3070  ast_copy_string(builtin_features_dtmf[feature], dtmf, sizeof(builtin_features_dtmf[feature]));
3071  }
3072 
3073  builtin_features_handlers[feature] = callback;
3074 
3075  return 0;
3076 }
3077 
3079 {
3080  if (ARRAY_LEN(builtin_features_handlers) <= feature
3081  || !builtin_features_handlers[feature]) {
3082  return -1;
3083  }
3084 
3085  builtin_features_handlers[feature] = NULL;
3086 
3087  return 0;
3088 }
3089 
3090 int ast_bridge_features_do(enum ast_bridge_builtin_feature feature, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
3091 {
3092  ast_bridge_hook_callback callback;
3093 
3094  if (ARRAY_LEN(builtin_features_handlers) <= feature) {
3095  return -1;
3096  }
3097 
3098  callback = builtin_features_handlers[feature];
3099  if (!callback) {
3100  return -1;
3101  }
3102  callback(bridge_channel, hook_pvt);
3103 
3104  return 0;
3105 }
3106 
3108 {
3109  if (ARRAY_LEN(builtin_interval_handlers) <= interval
3110  || builtin_interval_handlers[interval]) {
3111  return -1;
3112  }
3113 
3114  builtin_interval_handlers[interval] = callback;
3115 
3116  return 0;
3117 }
3118 
3120 {
3121  if (ARRAY_LEN(builtin_interval_handlers) <= interval
3122  || !builtin_interval_handlers[interval]) {
3123  return -1;
3124  }
3125 
3126  builtin_interval_handlers[interval] = NULL;
3127 
3128  return 0;
3129 
3130 }
3131 
3132 /*!
3133  * \internal
3134  * \brief Bridge hook destructor.
3135  * \since 12.0.0
3136  *
3137  * \param vhook Object to destroy.
3138  */
3139 static void bridge_hook_destroy(void *vhook)
3140 {
3141  struct ast_bridge_hook *hook = vhook;
3142 
3143  if (hook->destructor) {
3144  hook->destructor(hook->hook_pvt);
3145  }
3146 }
3147 
3148 /*!
3149  * \internal
3150  * \brief Allocate and setup a generic bridge hook.
3151  * \since 12.0.0
3152  *
3153  * \param size How big an object to allocate.
3154  * \param callback Function to execute upon activation
3155  * \param hook_pvt Unique data
3156  * \param destructor Optional destructor callback for hook_pvt data
3157  * \param remove_flags Dictates what situations the hook should be removed.
3158  *
3159  * \return hook on success.
3160  * \retval NULL on error.
3161  */
3162 static struct ast_bridge_hook *bridge_hook_generic(size_t size,
3164  void *hook_pvt,
3167 {
3168  struct ast_bridge_hook *hook;
3169 
3170  /* Allocate new hook and setup it's basic variables */
3171  hook = ao2_alloc_options(size, bridge_hook_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
3172  if (hook) {
3173  hook->callback = callback;
3174  hook->destructor = destructor;
3175  hook->hook_pvt = hook_pvt;
3176  ast_set_flag(&hook->remove_flags, remove_flags);
3177  }
3178 
3179  return hook;
3180 }
3181 
3183  const char *dtmf,
3184  ast_bridge_hook_callback callback,
3185  void *hook_pvt,
3186  ast_bridge_hook_pvt_destructor destructor,
3187  enum ast_bridge_hook_remove_flags remove_flags)
3188 {
3189  struct ast_bridge_hook_dtmf *hook;
3190  int res;
3191 
3192  /* Allocate new hook and setup it's various variables */
3193  hook = (struct ast_bridge_hook_dtmf *) bridge_hook_generic(sizeof(*hook), callback,
3194  hook_pvt, destructor, remove_flags);
3195  if (!hook) {
3196  return -1;
3197  }
3198  hook->generic.type = AST_BRIDGE_HOOK_TYPE_DTMF;
3199  ast_copy_string(hook->dtmf.code, dtmf, sizeof(hook->dtmf.code));
3200 
3201  /* Once done we put it in the container. */
3202  res = ao2_link(features->dtmf_hooks, hook) ? 0 : -1;
3203  if (res) {
3204  /*
3205  * Could not link the hook into the container.
3206  *
3207  * Remove the hook_pvt destructor call from the hook since we
3208  * are returning failure to install the hook.
3209  */
3210  hook->generic.destructor = NULL;
3211  }
3212  ao2_ref(hook, -1);
3213 
3214  return res;
3215 }
3216 
3217 /*!
3218  * \internal
3219  * \brief Attach an other hook to a bridge features structure
3220  *
3221  * \param features Bridge features structure
3222  * \param callback Function to execute upon activation
3223  * \param hook_pvt Unique data
3224  * \param destructor Optional destructor callback for hook_pvt data
3225  * \param remove_flags Dictates what situations the hook should be removed.
3226  * \param type What type of hook is being attached.
3227  *
3228  * \retval 0 on success
3229  * \retval -1 on failure (The caller must cleanup any hook_pvt resources.)
3230  */
3231 static int bridge_other_hook(struct ast_bridge_features *features,
3232  ast_bridge_hook_callback callback,
3233  void *hook_pvt,
3234  ast_bridge_hook_pvt_destructor destructor,
3235  enum ast_bridge_hook_remove_flags remove_flags,
3236  enum ast_bridge_hook_type type)
3237 {
3238  struct ast_bridge_hook *hook;
3239  int res;
3240 
3241  /* Allocate new hook and setup it's various variables */
3242  hook = bridge_hook_generic(sizeof(*hook), callback, hook_pvt, destructor,
3243  remove_flags);
3244  if (!hook) {
3245  return -1;
3246  }
3247  hook->type = type;
3248 
3249  /* Once done we put it in the container. */
3250  res = ao2_link(features->other_hooks, hook) ? 0 : -1;
3251  if (res) {
3252  /*
3253  * Could not link the hook into the container.
3254  *
3255  * Remove the hook_pvt destructor call from the hook since we
3256  * are returning failure to install the hook.
3257  */
3258  hook->destructor = NULL;
3259  }
3260  ao2_ref(hook, -1);
3261 
3262  return res;
3263 }
3264 
3266  ast_bridge_hook_callback callback,
3267  void *hook_pvt,
3268  ast_bridge_hook_pvt_destructor destructor,
3269  enum ast_bridge_hook_remove_flags remove_flags)
3270 {
3271  return bridge_other_hook(features, callback, hook_pvt, destructor, remove_flags,
3272  AST_BRIDGE_HOOK_TYPE_HANGUP);
3273 }
3274 
3276  ast_bridge_hook_callback callback,
3277  void *hook_pvt,
3278  ast_bridge_hook_pvt_destructor destructor,
3279  enum ast_bridge_hook_remove_flags remove_flags)
3280 {
3281  return bridge_other_hook(features, callback, hook_pvt, destructor, remove_flags,
3282  AST_BRIDGE_HOOK_TYPE_JOIN);
3283 }
3284 
3286  ast_bridge_hook_callback callback,
3287  void *hook_pvt,
3288  ast_bridge_hook_pvt_destructor destructor,
3289  enum ast_bridge_hook_remove_flags remove_flags)
3290 {
3291  return bridge_other_hook(features, callback, hook_pvt, destructor, remove_flags,
3292  AST_BRIDGE_HOOK_TYPE_LEAVE);
3293 }
3294 
3297  void *hook_pvt,
3298  ast_bridge_hook_pvt_destructor destructor,
3299  enum ast_bridge_hook_remove_flags remove_flags)
3300 {
3302 
3303  return bridge_other_hook(features, hook_cb, hook_pvt, destructor, remove_flags,
3304  AST_BRIDGE_HOOK_TYPE_TALK);
3305 }
3306 
3309  void *hook_pvt,
3310  ast_bridge_hook_pvt_destructor destructor,
3311  enum ast_bridge_hook_remove_flags remove_flags)
3312 {
3314 
3315  return bridge_other_hook(features, hook_cb, hook_pvt, destructor, remove_flags,
3316  AST_BRIDGE_HOOK_TYPE_MOVE);
3317 }
3318 
3320  enum ast_bridge_hook_timer_option flags,
3321  unsigned int interval,
3322  ast_bridge_hook_callback callback,
3323  void *hook_pvt,
3324  ast_bridge_hook_pvt_destructor destructor,
3325  enum ast_bridge_hook_remove_flags remove_flags)
3326 {
3327  struct ast_bridge_hook_timer *hook;
3328  int res;
3329 
3330  if (!features ||!interval || !callback) {
3331  return -1;
3332  }
3333 
3334  /* Allocate new hook and setup it's various variables */
3335  hook = (struct ast_bridge_hook_timer *) bridge_hook_generic(sizeof(*hook), callback,
3336  hook_pvt, destructor, remove_flags);
3337  if (!hook) {
3338  return -1;
3339  }
3340  hook->generic.type = AST_BRIDGE_HOOK_TYPE_TIMER;
3341  hook->timer.interval = interval;
3342  hook->timer.trip_time = ast_tvadd(ast_tvnow(), ast_samp2tv(interval, 1000));
3343  hook->timer.seqno = ast_atomic_fetchadd_int((int *) &features->interval_sequence, +1);
3344  hook->timer.flags = flags;
3345 
3346  ast_debug(1, "Putting interval hook %p with interval %u in the heap on features %p\n",
3347  hook, hook->timer.interval, features);
3348  ast_heap_wrlock(features->interval_hooks);
3349  res = ast_heap_push(features->interval_hooks, hook);
3350  ast_heap_unlock(features->interval_hooks);
3351  if (res) {
3352  /*
3353  * Could not push the hook into the heap
3354  *
3355  * Remove the hook_pvt destructor call from the hook since we
3356  * are returning failure to install the hook.
3357  */
3358  hook->generic.destructor = NULL;
3359  ao2_ref(hook, -1);
3360  }
3361 
3362  return res ? -1 : 0;
3363 }
3364 
3366  enum ast_bridge_builtin_feature feature,
3367  const char *dtmf,
3368  void *config,
3369  ast_bridge_hook_pvt_destructor destructor,
3370  enum ast_bridge_hook_remove_flags remove_flags)
3371 {
3372  if (ARRAY_LEN(builtin_features_handlers) <= feature
3373  || !builtin_features_handlers[feature]) {
3374  return -1;
3375  }
3376 
3377  /* If no alternate DTMF stream was provided use the default one */
3378  if (ast_strlen_zero(dtmf)) {
3379  dtmf = builtin_features_dtmf[feature];
3380  /* If no DTMF is still available (ie: it has been disabled) then error out now */
3381  if (ast_strlen_zero(dtmf)) {
3382  ast_debug(1, "Failed to enable built in feature %u on %p, no DTMF string is available for it.\n",
3383  feature, features);
3384  return -1;
3385  }
3386  }
3387 
3388  /*
3389  * The rest is basically pretty easy. We create another hook
3390  * using the built in feature's DTMF callback. Easy as pie.
3391  */
3392  return ast_bridge_dtmf_hook(features, dtmf, builtin_features_handlers[feature],
3393  config, destructor, remove_flags);
3394 }
3395 
3397 {
3398  memset(limits, 0, sizeof(*limits));
3399 
3400  if (ast_string_field_init(limits, 256)) {
3401  return -1;
3402  }
3403 
3404  return 0;
3405 }
3406 
3408 {
3410 }
3411 
3414  enum ast_bridge_hook_remove_flags remove_flags)
3415 {
3418 
3420  return callback(features, limits, remove_flags);
3421  }
3422 
3423  ast_log(LOG_ERROR, "Attempted to set limits without an AST_BRIDGE_BUILTIN_INTERVAL_LIMITS callback registered.\n");
3424  return -1;
3425 }
3426 
3427 void ast_bridge_features_set_flag(struct ast_bridge_features *features, unsigned int flag)
3428 {
3429  ast_set_flag(&features->feature_flags, flag);
3430  features->usable = 1;
3431 }
3432 
3433 /*!
3434  * \internal
3435  * \brief ao2 object match hooks with appropriate remove_flags.
3436  * \since 12.0.0
3437  *
3438  * \param obj Feature hook object.
3439  * \param arg Removal flags
3440  * \param flags Not used
3441  *
3442  * \retval CMP_MATCH if hook's remove_flags match the removal flags set.
3443  * \retval 0 if not match.
3444  */
3445 static int hook_remove_match(void *obj, void *arg, int flags)
3446 {
3447  struct ast_bridge_hook *hook = obj;
3448  enum ast_bridge_hook_remove_flags *remove_flags = arg;
3449 
3450  if (ast_test_flag(&hook->remove_flags, *remove_flags)) {
3451  return CMP_MATCH;
3452  } else {
3453  return 0;
3454  }
3455 }
3456 
3457 /*!
3458  * \internal
3459  * \brief Remove all hooks with appropriate remove_flags in the container.
3460  * \since 12.0.0
3461  *
3462  * \param hooks Hooks container to work on.
3463  * \param remove_flags Determinator for whether hook is removed
3464  */
3465 static void hooks_remove_container(struct ao2_container *hooks, enum ast_bridge_hook_remove_flags remove_flags)
3466 {
3468  hook_remove_match, &remove_flags);
3469 }
3470 
3471 /*!
3472  * \internal
3473  * \brief Remove all hooks in the heap with appropriate remove_flags set.
3474  * \since 12.0.0
3475  *
3476  * \param hooks Hooks heap to work on.
3477  * \param remove_flags Determinator for whether hook is removed
3478  */
3479 static void hooks_remove_heap(struct ast_heap *hooks, enum ast_bridge_hook_remove_flags remove_flags)
3480 {
3481  struct ast_bridge_hook *hook;
3482  int changed;
3483 
3484  ast_heap_wrlock(hooks);
3485  do {
3486  int idx;
3487 
3488  changed = 0;
3489  for (idx = ast_heap_size(hooks); idx; --idx) {
3490  hook = ast_heap_peek(hooks, idx);
3491  if (ast_test_flag(&hook->remove_flags, remove_flags)) {
3492  ast_heap_remove(hooks, hook);
3493  ao2_ref(hook, -1);
3494  changed = 1;
3495  }
3496  }
3497  } while (changed);
3498  ast_heap_unlock(hooks);
3499 }
3500 
3502 {
3503  hooks_remove_container(features->dtmf_hooks, remove_flags);
3504  hooks_remove_container(features->other_hooks, remove_flags);
3505  hooks_remove_heap(features->interval_hooks, remove_flags);
3506 }
3507 
3508 static int interval_hook_time_cmp(void *a, void *b)
3509 {
3510  struct ast_bridge_hook_timer *hook_a = a;
3511  struct ast_bridge_hook_timer *hook_b = b;
3512  int cmp;
3513 
3514  cmp = ast_tvcmp(hook_b->timer.trip_time, hook_a->timer.trip_time);
3515  if (cmp) {
3516  return cmp;
3517  }
3518 
3519  cmp = hook_b->timer.seqno - hook_a->timer.seqno;
3520  return cmp;
3521 }
3522 
3523 /*!
3524  * \internal
3525  * \brief DTMF hook container sort comparison function.
3526  * \since 12.0.0
3527  *
3528  * \param obj_left pointer to the (user-defined part) of an object.
3529  * \param obj_right pointer to the (user-defined part) of an object.
3530  * \param flags flags from ao2_callback()
3531  * OBJ_POINTER - if set, 'obj_right', is an object.
3532  * OBJ_KEY - if set, 'obj_right', is a search key item that is not an object.
3533  * OBJ_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
3534  *
3535  * \retval <0 if obj_left < obj_right
3536  * \retval =0 if obj_left == obj_right
3537  * \retval >0 if obj_left > obj_right
3538  */
3539 static int bridge_dtmf_hook_sort(const void *obj_left, const void *obj_right, int flags)
3540 {
3541  const struct ast_bridge_hook_dtmf *hook_left = obj_left;
3542  const struct ast_bridge_hook_dtmf *hook_right = obj_right;
3543  const char *right_key = obj_right;
3544  int cmp;
3545 
3546  switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
3547  default:
3548  case OBJ_POINTER:
3549  right_key = hook_right->dtmf.code;
3550  /* Fall through */
3551  case OBJ_KEY:
3552  cmp = strcasecmp(hook_left->dtmf.code, right_key);
3553  break;
3554  case OBJ_PARTIAL_KEY:
3555  cmp = strncasecmp(hook_left->dtmf.code, right_key, strlen(right_key));
3556  break;
3557  }
3558  return cmp;
3559 }
3560 
3561 /*! \brief Callback for merging hook ao2_containers */
3562 static int merge_container_cb(void *obj, void *data, int flags)
3563 {
3564  ao2_link(data, obj);
3565  return 0;
3566 }
3567 
3568 /*! \brief Wrapper for interval hooks that calls into the wrapped hook */
3569 static int interval_wrapper_cb(struct ast_bridge_channel *bridge_channel, void *obj)
3570 {
3571  struct ast_bridge_hook_timer *hook = obj;
3572 
3573  return hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
3574 }
3575 
3576 /*! \brief Destructor for the hook wrapper */
3577 static void interval_wrapper_pvt_dtor(void *obj)
3578 {
3579  ao2_cleanup(obj);
3580 }
3581 
3582 /*! \brief Wrap the provided interval hook and add it to features */
3583 static void wrap_hook(struct ast_bridge_features *features, struct ast_bridge_hook_timer *hook)
3584 {
3585  /* Break out of the current wrapper if it exists to avoid multiple layers */
3586  if (hook->generic.callback == interval_wrapper_cb) {
3587  hook = hook->generic.hook_pvt;
3588  }
3589 
3590  ast_bridge_interval_hook(features, hook->timer.flags, hook->timer.interval,
3592  hook->generic.remove_flags.flags);
3593 }
3594 
3596 {
3597  struct ast_bridge_hook_timer *hook;
3598  int idx;
3599 
3600  /* Merge hook containers */
3603 
3604  /* Merge hook heaps */
3605  ast_heap_wrlock(from->interval_hooks);
3606  for (idx = 1; (hook = ast_heap_peek(from->interval_hooks, idx)); idx++) {
3607  wrap_hook(into, hook);
3608  }
3609  ast_heap_unlock(from->interval_hooks);
3610 
3611  /* Merge feature flags */
3612  into->feature_flags.flags |= from->feature_flags.flags;
3613  into->usable |= from->usable;
3614 
3615  into->mute |= from->mute;
3616  into->dtmf_passthrough |= from->dtmf_passthrough;
3617 }
3618 
3619 /* XXX ASTERISK-21271 make ast_bridge_features_init() static when make ast_bridge_join() requires features to be allocated. */
3621 {
3622  /* Zero out the structure */
3623  memset(features, 0, sizeof(*features));
3624 
3625  /* Initialize the DTMF hooks container */
3627  AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE, bridge_dtmf_hook_sort, NULL);
3628  if (!features->dtmf_hooks) {
3629  return -1;
3630  }
3631 
3632  /* Initialize the miscellaneous other hooks container */
3634  NULL);
3635  if (!features->other_hooks) {
3636  return -1;
3637  }
3638 
3639  /* Initialize the interval hooks heap */
3640  features->interval_hooks = ast_heap_create(8, interval_hook_time_cmp,
3641  offsetof(struct ast_bridge_hook_timer, timer.heap_index));
3642  if (!features->interval_hooks) {
3643  return -1;
3644  }
3645 
3646  features->dtmf_passthrough = 1;
3647  features->text_messaging = 1;
3648 
3649  return 0;
3650 }
3651 
3652 /* XXX ASTERISK-21271 make ast_bridge_features_cleanup() static when make ast_bridge_join() requires features to be allocated. */
3654 {
3655  struct ast_bridge_hook_timer *hook;
3656 
3657  /* Destroy the interval hooks heap. */
3658  if (features->interval_hooks) {
3659  while ((hook = ast_heap_pop(features->interval_hooks))) {
3660  ao2_ref(hook, -1);
3661  }
3662  features->interval_hooks = ast_heap_destroy(features->interval_hooks);
3663  }
3664 
3665  /* Destroy the miscellaneous other hooks container. */
3666  ao2_cleanup(features->other_hooks);
3667  features->other_hooks = NULL;
3668 
3669  /* Destroy the DTMF hooks container. */
3670  ao2_cleanup(features->dtmf_hooks);
3671  features->dtmf_hooks = NULL;
3672 }
3673 
3675 {
3676  if (!features) {
3677  return;
3678  }
3679  ast_bridge_features_cleanup(features);
3680  ast_free(features);
3681 }
3682 
3684 {
3685  struct ast_bridge_features *features;
3686 
3687  features = ast_malloc(sizeof(*features));
3688  if (features) {
3689  if (ast_bridge_features_init(features)) {
3690  ast_bridge_features_destroy(features);
3691  features = NULL;
3692  }
3693  }
3694 
3695  return features;
3696 }
3697 
3698 void ast_bridge_set_mixing_interval(struct ast_bridge *bridge, unsigned int mixing_interval)
3699 {
3700  ast_bridge_lock(bridge);
3701  bridge->softmix.internal_mixing_interval = mixing_interval;
3702  ast_bridge_unlock(bridge);
3703 }
3704 
3705 void ast_bridge_set_binaural_active(struct ast_bridge *bridge, unsigned int binaural_active)
3706 {
3707  ast_bridge_lock(bridge);
3708  bridge->softmix.binaural_active = binaural_active;
3709  ast_bridge_unlock(bridge);
3710 }
3711 
3712 void ast_bridge_set_internal_sample_rate(struct ast_bridge *bridge, unsigned int sample_rate)
3713 {
3714  ast_bridge_lock(bridge);
3715  bridge->softmix.internal_sample_rate = sample_rate;
3716  ast_bridge_unlock(bridge);
3717 }
3718 
3719 void ast_bridge_set_maximum_sample_rate(struct ast_bridge *bridge, unsigned int sample_rate)
3720 {
3721  ast_bridge_lock(bridge);
3722  bridge->softmix.maximum_sample_rate = sample_rate;
3723  ast_bridge_unlock(bridge);
3724 }
3725 
3726 static void cleanup_video_mode(struct ast_bridge *bridge)
3727 {
3728  switch (bridge->softmix.video_mode.mode) {
3730  break;
3732  if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc) {
3733  ast_channel_unref(bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc);
3734  }
3735  break;
3737  if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc) {
3738  ast_channel_unref(bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc);
3739  }
3740  if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc) {
3741  ast_channel_unref(bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc);
3742  }
3744  break;
3745  }
3746  memset(&bridge->softmix.video_mode, 0, sizeof(bridge->softmix.video_mode));
3747 }
3748 
3749 void ast_bridge_set_single_src_video_mode(struct ast_bridge *bridge, struct ast_channel *video_src_chan)
3750 {
3751  ast_bridge_lock(bridge);
3752  cleanup_video_mode(bridge);
3754  if (video_src_chan) {
3755  bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc = ast_channel_ref(video_src_chan);
3756  ast_verb(5, "Video source in bridge '%s' (%s) is now '%s' (%s)\n",
3757  bridge->name, bridge->uniqueid,
3758  ast_channel_name(video_src_chan),
3759  ast_channel_uniqueid(video_src_chan));
3760  ast_indicate(video_src_chan, AST_CONTROL_VIDUPDATE);
3761  }
3762  ast_bridge_publish_state(bridge);
3763  ast_bridge_unlock(bridge);
3764 }
3765 
3767 {
3768  ast_bridge_lock(bridge);
3769  cleanup_video_mode(bridge);
3771  ast_bridge_unlock(bridge);
3772 }
3773 
3775 {
3776  ast_bridge_lock(bridge);
3777  cleanup_video_mode(bridge);
3779  ast_bridge_unlock(bridge);
3780 }
3781 
3782 void ast_bridge_set_video_update_discard(struct ast_bridge *bridge, unsigned int video_update_discard)
3783 {
3784  ast_bridge_lock(bridge);
3785  bridge->softmix.video_mode.video_update_discard = video_update_discard;
3786  ast_bridge_unlock(bridge);
3787 }
3788 
3789 void ast_bridge_set_remb_send_interval(struct ast_bridge *bridge, unsigned int remb_send_interval)
3790 {
3791  ast_assert(bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_SFU);
3792 
3793  ast_bridge_lock(bridge);
3794  bridge->softmix.video_mode.mode_data.sfu_data.remb_send_interval = remb_send_interval;
3795  ast_bridge_unlock(bridge);
3796 }
3797 
3799 {
3800  ast_assert(bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_SFU);
3801 
3802  ast_bridge_lock(bridge);
3803  bridge->softmix.video_mode.mode_data.sfu_data.remb_behavior = behavior;
3804  ast_bridge_unlock(bridge);
3805 }
3806 
3807 void ast_bridge_set_remb_estimated_bitrate(struct ast_bridge *bridge, float estimated_bitrate)
3808 {
3809  ast_assert(bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_SFU);
3810 
3811  ast_bridge_lock(bridge);
3812  bridge->softmix.video_mode.mode_data.sfu_data.estimated_bitrate = estimated_bitrate;
3813  ast_bridge_unlock(bridge);
3814 }
3815 
3816 void ast_bridge_update_talker_src_video_mode(struct ast_bridge *bridge, struct ast_channel *chan, int talker_energy, int is_keyframe)
3817 {
3818  struct ast_bridge_video_talker_src_data *data;
3819 
3820  /* If the channel doesn't support video, we don't care about it */
3821  if (!ast_format_cap_has_type(ast_channel_nativeformats(chan), AST_MEDIA_TYPE_VIDEO)) {
3822  return;
3823  }
3824 
3825  ast_bridge_lock(bridge);
3826  data = &bridge->softmix.video_mode.mode_data.talker_src_data;
3827 
3828  if (data->chan_vsrc == chan) {
3829  data->average_talking_energy = talker_energy;
3830  } else if ((data->average_talking_energy < talker_energy) && is_keyframe) {
3831  if (data->chan_old_vsrc) {
3833  }
3834  if (data->chan_vsrc) {
3835  data->chan_old_vsrc = data->chan_vsrc;
3837  }
3838  data->chan_vsrc = ast_channel_ref(chan);
3839  data->average_talking_energy = talker_energy;
3840  ast_verb(5, "Video source in bridge '%s' (%s) is now '%s' (%s)\n",
3841  bridge->name, bridge->uniqueid,
3842  ast_channel_name(data->chan_vsrc),
3843  ast_channel_uniqueid(data->chan_vsrc));
3844  ast_bridge_publish_state(bridge);
3846  } else if ((data->average_talking_energy < talker_energy) && !is_keyframe) {
3848  } else if (!data->chan_vsrc && is_keyframe) {
3849  data->chan_vsrc = ast_channel_ref(chan);
3850  data->average_talking_energy = talker_energy;
3851  ast_verb(5, "Video source in bridge '%s' (%s) is now '%s' (%s)\n",
3852  bridge->name, bridge->uniqueid,
3853  ast_channel_name(data->chan_vsrc),
3854  ast_channel_uniqueid(data->chan_vsrc));
3855  ast_bridge_publish_state(bridge);
3857  } else if (!data->chan_old_vsrc && is_keyframe) {
3858  data->chan_old_vsrc = ast_channel_ref(chan);
3860  }
3861  ast_bridge_unlock(bridge);
3862 }
3863 
3865 {
3866  int res = 0;
3867 
3868  ast_bridge_lock(bridge);
3869  switch (bridge->softmix.video_mode.mode) {
3871  break;
3873  if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc) {
3874  res = 1;
3875  }
3876  break;
3878  if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc) {
3879  res++;
3880  }
3881  if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc) {
3882  res++;
3883  }
3885  break;
3886  }
3887  ast_bridge_unlock(bridge);
3888  return res;
3889 }
3890 
3891 int ast_bridge_is_video_src(struct ast_bridge *bridge, struct ast_channel *chan)
3892 {
3893  int res = 0;
3894 
3895  ast_bridge_lock(bridge);
3896  switch (bridge->softmix.video_mode.mode) {
3898  break;
3900  if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc == chan) {
3901  res = 1;
3902  }
3903  break;
3905  if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc == chan) {
3906  res = 1;
3907  } else if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc == chan) {
3908  res = 2;
3909  }
3911  break;
3912  }
3913  ast_bridge_unlock(bridge);
3914  return res;
3915 }
3916 
3917 void ast_bridge_remove_video_src(struct ast_bridge *bridge, struct ast_channel *chan)
3918 {
3919  ast_bridge_lock(bridge);
3920  switch (bridge->softmix.video_mode.mode) {
3922  break;
3924  if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc == chan) {
3925  if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc) {
3926  ast_channel_unref(bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc);
3927  }
3928  bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc = NULL;
3929  }
3930  break;
3932  if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc == chan) {
3933  if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc) {
3934  ast_channel_unref(bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc);
3935  }
3936  bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc = NULL;
3937  bridge->softmix.video_mode.mode_data.talker_src_data.average_talking_energy = 0;
3938  }
3939  if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc == chan) {
3940  if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc) {
3941  ast_channel_unref(bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc);
3942  }
3943  bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc = NULL;
3944  }
3946  break;
3947  }
3948  ast_bridge_unlock(bridge);
3949 }
3950 
3952 {
3953  switch (video_mode) {
3955  return "talker";
3957  return "single";
3959  return "sfu";
3961  default:
3962  return "none";
3963  }
3964 }
3965 
3966 void ast_bridge_set_send_sdp_label(struct ast_bridge *bridge, unsigned int send_sdp_label)
3967 {
3968  ast_bridge_lock(bridge);
3969  bridge->softmix.send_sdp_label = send_sdp_label;
3970  ast_bridge_unlock(bridge);
3971 }
3972 
3973 static int channel_hash(const void *obj, int flags)
3974 {
3975  const struct ast_channel *chan = obj;
3976  const char *name = obj;
3977  int hash;
3978 
3979  switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
3980  default:
3981  case OBJ_POINTER:
3982  name = ast_channel_name(chan);
3983  /* Fall through */
3984  case OBJ_KEY:
3985  hash = ast_str_hash(name);
3986  break;
3987  case OBJ_PARTIAL_KEY:
3988  /* Should never happen in hash callback. */
3989  ast_assert(0);
3990  hash = 0;
3991  break;
3992  }
3993  return hash;
3994 }
3995 
3996 static int channel_cmp(void *obj, void *arg, int flags)
3997 {
3998  const struct ast_channel *left = obj;
3999  const struct ast_channel *right = arg;
4000  const char *right_name = arg;
4001  int cmp;
4002 
4003  switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
4004  default:
4005  case OBJ_POINTER:
4006  right_name = ast_channel_name(right);
4007  /* Fall through */
4008  case OBJ_KEY:
4009  cmp = strcmp(ast_channel_name(left), right_name);
4010  break;
4011  case OBJ_PARTIAL_KEY:
4012  cmp = strncmp(ast_channel_name(left), right_name, strlen(right_name));
4013  break;
4014  }
4015  return cmp ? 0 : CMP_MATCH;
4016 }
4017 
4019 {
4020  struct ao2_container *channels;
4021  struct ast_bridge_channel *iter;
4022 
4024  13, channel_hash, NULL, channel_cmp);
4025  if (!channels) {
4026  return NULL;
4027  }
4028 
4029  AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
4030  ao2_link(channels, iter->chan);
4031  }
4032 
4033  return channels;
4034 }
4035 
4037 {
4038  struct ao2_container *channels;
4039 
4040  ast_bridge_lock(bridge);
4041  channels = ast_bridge_peers_nolock(bridge);
4042  ast_bridge_unlock(bridge);
4043 
4044  return channels;
4045 }
4046 
4047 struct ast_channel *ast_bridge_peer_nolock(struct ast_bridge *bridge, struct ast_channel *chan)
4048 {
4049  struct ast_channel *peer = NULL;
4050  struct ast_bridge_channel *iter;
4051 
4052  /* Asking for the peer channel only makes sense on a two-party bridge. */
4053  if (bridge->num_channels == 2
4054  && bridge->technology->capabilities
4056  int in_bridge = 0;
4057 
4058  AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
4059  if (iter->chan != chan) {
4060  peer = iter->chan;
4061  } else {
4062  in_bridge = 1;
4063  }
4064  }
4065  if (in_bridge && peer) {
4066  ast_channel_ref(peer);
4067  } else {
4068  peer = NULL;
4069  }
4070  }
4071 
4072  return peer;
4073 }
4074 
4075 struct ast_channel *ast_bridge_peer(struct ast_bridge *bridge, struct ast_channel *chan)
4076 {
4077  struct ast_channel *peer;
4078 
4079  ast_bridge_lock(bridge);
4080  peer = ast_bridge_peer_nolock(bridge, chan);
4081  ast_bridge_unlock(bridge);
4082 
4083  return peer;
4084 }
4085 
4086 /*!
4087  * \internal
4088  * \brief Transfer an entire bridge to a specific destination.
4089  *
4090  * This creates a local channel to dial out and swaps the called local channel
4091  * with the transferer channel. By doing so, all participants in the bridge are
4092  * connected to the specified destination.
4093  *
4094  * While this means of transferring would work for both two-party and multi-party
4095  * bridges, this method is only used for multi-party bridges since this method would
4096  * be less efficient for two-party bridges.
4097  *
4098  * \param is_external Whether the transfer is externally initiated
4099  * \param transferer The channel performing a transfer
4100  * \param bridge The bridge where the transfer is being performed
4101  * \param exten The destination extension for the blind transfer
4102  * \param context The destination context for the blind transfer
4103  * \param transferee The party being transferred if there is only one
4104  * \param new_channel_cb Callback to call on channel that is created to
4105  * facilitate the blind transfer.
4106  * \param user_data_wrapper User-provided data needed in new_channel_cb
4107  * \param transfer_message The Stasis publication for this transfer.
4108  *
4109  * \return The success or failure of the operation
4110  */
4111 static enum ast_transfer_result blind_transfer_bridge(int is_external,
4112  struct ast_channel *transferer, struct ast_bridge *bridge,
4113  const char *exten, const char *context, struct ast_channel *transferee,
4114  transfer_channel_cb new_channel_cb,
4115  struct transfer_channel_data *user_data_wrapper,
4116  struct ast_blind_transfer_message *transfer_message)
4117 {
4118  struct ast_channel *local;
4119  char chan_name[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 2];
4120  int cause;
4121  struct ast_format_cap *caps;
4122 
4123  ast_channel_lock(transferer);
4124  caps = ao2_bump(ast_channel_nativeformats(transferer));
4125  ast_channel_unlock(transferer);
4126 
4127  snprintf(chan_name, sizeof(chan_name), "%s@%s", exten, context);
4128  local = ast_request("Local", caps, NULL, transferer,
4129  chan_name, &cause);
4130 
4131  ao2_cleanup(caps);
4132 
4133  if (!local) {
4134  return AST_BRIDGE_TRANSFER_FAIL;
4135  }
4136 
4137  ast_channel_lock_both(local, transferer);
4139 
4140  transfer_message->replace_channel = ast_channel_snapshot_get_latest(ast_channel_uniqueid(local));
4141  if (!transfer_message->replace_channel) {
4142  ast_hangup(local);
4143  return AST_BRIDGE_TRANSFER_FAIL;
4144  }
4145 
4146  pbx_builtin_setvar_helper(local, BLINDTRANSFER, ast_channel_name(transferer));
4147  ast_channel_unlock(local);
4148  ast_channel_unlock(transferer);
4149 
4150  if (new_channel_cb) {
4151  new_channel_cb(local, user_data_wrapper, AST_BRIDGE_TRANSFER_MULTI_PARTY);
4152  }
4153 
4154  if (ast_call(local, chan_name, 0)) {
4155  ast_hangup(local);
4156  return AST_BRIDGE_TRANSFER_FAIL;
4157  }
4158 
4159  if (ast_bridge_impart(bridge, local, transferer, NULL,
4161  ast_hangup(local);
4162  return AST_BRIDGE_TRANSFER_FAIL;
4163  }
4164 
4166 }
4167 
4168 /*!
4169  * \internal
4170  * \brief Get the transferee channel
4171  *
4172  * This is only applicable to cases where a transfer is occurring on a
4173  * two-party bridge. The channels container passed in is expected to only
4174  * contain two channels, the transferer and the transferee. The transferer
4175  * channel is passed in as a parameter to ensure we don't return it as
4176  * the transferee channel.
4177  *
4178  * \param channels A two-channel container containing the transferer and transferee
4179  * \param transferer The party that is transfering the call
4180  * \return The party that is being transferred
4181  */
4182 static struct ast_channel *get_transferee(struct ao2_container *channels, struct ast_channel *transferer)
4183 {
4184  struct ao2_iterator channel_iter;
4185  struct ast_channel *transferee;
4186 
4187  for (channel_iter = ao2_iterator_init(channels, 0);
4188  (transferee = ao2_iterator_next(&channel_iter));
4189  ao2_cleanup(transferee)) {
4190  if (transferee != transferer) {
4191  break;
4192  }
4193  }
4194 
4195  ao2_iterator_destroy(&channel_iter);
4196  return transferee;
4197 }
4198 
4199 /*!
4200  * \brief Perform an attended transfer of a bridge
4201  *
4202  * This performs an attended transfer of an entire bridge to a target.
4203  * The target varies, depending on what bridges exist during the transfer
4204  * attempt.
4205  *
4206  * If two bridges exist, then a local channel is created to link the two
4207  * bridges together.
4208  *
4209  * If only one bridge exists, then a local channel is created with one end
4210  * placed into the existing bridge and the other end masquerading into
4211  * the unbridged channel.
4212  *
4213  * \param chan1 Transferer channel. Guaranteed to be bridged.
4214  * \param chan2 Other transferer channel. May or may not be bridged.
4215  * \param bridge1 Bridge that chan1 is in. Guaranteed to be non-NULL.
4216  * \param bridge2 Bridge that chan2 is in. If NULL, then chan2 is not bridged.
4217  * \param transfer_msg Data to publish for a stasis attended transfer message.
4218  * \retval AST_BRIDGE_TRANSFER_FAIL Internal error occurred
4219  * \retval AST_BRIDGE_TRANSFER_SUCCESS Succesfully transferred the bridge
4220  */
4222  struct ast_channel *chan2, struct ast_bridge *bridge1, struct ast_bridge *bridge2,
4223  struct ast_attended_transfer_message *transfer_msg)
4224 {
4225 #define BRIDGE_LOCK_ONE_OR_BOTH(b1, b2) \
4226  do { \
4227  if (b2) { \
4228  ast_bridge_lock_both(b1, b2); \
4229  } else { \
4230  ast_bridge_lock(b1); \
4231  } \
4232  } while (0)
4233 
4234  static const char *dest = "_attended@transfer/m";
4235  struct ast_channel *local_chan;
4236  int cause;
4237  int res;
4238  const char *app = NULL;
4239  struct ast_format_cap *caps;
4240 
4241  ast_channel_lock(chan1);
4242  caps = ao2_bump(ast_channel_nativeformats(chan1));
4243  ast_channel_unlock(chan1);
4244 
4245  local_chan = ast_request("Local", caps, NULL, chan1, dest, &cause);
4246 
4247  ao2_cleanup(caps);
4248 
4249  if (!local_chan) {
4250  return AST_BRIDGE_TRANSFER_FAIL;
4251  }
4252 
4253  ast_channel_lock_both(local_chan, chan1);
4255  pbx_builtin_setvar_helper(local_chan, ATTENDEDTRANSFER, ast_channel_name(chan1));
4256  ast_channel_unlock(local_chan);
4257  ast_channel_unlock(chan1);
4258 
4259  if (bridge2) {
4260  res = ast_local_setup_bridge(local_chan, bridge2, chan2, NULL);
4261  } else {
4262  app = ast_strdupa(ast_channel_appl(chan2));
4263  res = ast_local_setup_masquerade(local_chan, chan2);
4264  }
4265 
4266  if (res) {
4267  ast_hangup(local_chan);
4268  return AST_BRIDGE_TRANSFER_FAIL;
4269  }
4270 
4271  /*
4272  * Since bridges need to be unlocked before entering ast_bridge_impart and
4273  * core_local may call into it then the bridges need to be unlocked here.
4274  */
4275  ast_bridge_unlock(bridge1);
4276  if (bridge2) {
4277  ast_bridge_unlock(bridge2);
4278  }
4279 
4280  if (ast_call(local_chan, dest, 0)) {
4281  ast_hangup(local_chan);
4282  BRIDGE_LOCK_ONE_OR_BOTH(bridge1, bridge2);
4283  return AST_BRIDGE_TRANSFER_FAIL;
4284  }
4285 
4286  /* Get a ref for use later since this one is being stolen */
4287  ao2_ref(local_chan, +1);
4288  if (ast_bridge_impart(bridge1, local_chan, chan1, NULL,
4290  ast_hangup(local_chan);
4291  ao2_cleanup(local_chan);
4292  BRIDGE_LOCK_ONE_OR_BOTH(bridge1, bridge2);
4293  return AST_BRIDGE_TRANSFER_FAIL;
4294  }
4295  BRIDGE_LOCK_ONE_OR_BOTH(bridge1, bridge2);
4296 
4297  if (bridge2) {
4298  void *tech;
4299  struct ast_channel *locals[2];
4300 
4301  /* Have to lock everything just in case a hangup comes in early */
4302  ast_local_lock_all(local_chan, &tech, &locals[0], &locals[1]);
4303  if (!locals[0] || !locals[1]) {
4304  ast_log(LOG_ERROR, "Transfer failed probably due to an early hangup - "
4305  "missing other half of '%s'\n", ast_channel_name(local_chan));
4306  ast_local_unlock_all(tech, locals[0], locals[1]);
4307  ao2_cleanup(local_chan);
4308  return AST_BRIDGE_TRANSFER_FAIL;
4309  }
4310 
4311  /* Make sure the peer is properly set */
4312  if (local_chan != locals[0]) {
4313  SWAP(locals[0], locals[1]);
4314  }
4315 
4316  ast_attended_transfer_message_add_link(transfer_msg, locals);
4317  ast_local_unlock_all(tech, locals[0], locals[1]);
4318  } else {
4319  ast_attended_transfer_message_add_app(transfer_msg, app, local_chan);
4320  }
4321 
4322  ao2_cleanup(local_chan);
4324 }
4325 
4326 static enum ast_transfer_result try_parking(struct ast_channel *transferer,
4327  const char *context, const char *exten, transfer_channel_cb new_channel_cb,
4328  struct transfer_channel_data *user_data_wrapper)
4329 {
4330  RAII_VAR(struct ast_bridge_channel *, transferer_bridge_channel, NULL, ao2_cleanup);
4331 
4333  return AST_BRIDGE_TRANSFER_FAIL;
4334  }
4335 
4336  ast_channel_lock(transferer);
4337  transferer_bridge_channel = ast_channel_get_bridge_channel(transferer);
4338  ast_channel_unlock(transferer);
4339 
4340  if (!transferer_bridge_channel) {
4341  return AST_BRIDGE_TRANSFER_FAIL;
4342  }
4343 
4344  if (ast_parking_blind_transfer_park(transferer_bridge_channel,
4345  context, exten, new_channel_cb, user_data_wrapper)) {
4346  return AST_BRIDGE_TRANSFER_FAIL;
4347  }
4348 
4350 }
4351 
4352 void ast_bridge_set_transfer_variables(struct ast_channel *chan, const char *value, int attended)
4353 {
4354  char *writevar;
4355  char *erasevar;
4356 
4357  if (attended) {
4358  writevar = ATTENDEDTRANSFER;
4359  erasevar = BLINDTRANSFER;
4360  } else {
4361  writevar = BLINDTRANSFER;
4362  erasevar = ATTENDEDTRANSFER;
4363  }
4364 
4365  pbx_builtin_setvar_helper(chan, writevar, value);
4366  pbx_builtin_setvar_helper(chan, erasevar, NULL);
4367 }
4368 
4369 /*!
4370  * \internal
4371  * \brief Set the transfer variable as appropriate on channels involved in the transfer
4372  *
4373  * The transferer channel will have its variable set the same as its BRIDGEPEER
4374  * variable. This will account for all channels that it is bridged to. The other channels
4375  * involved in the transfer will have their variable set to the transferer
4376  * channel's name.
4377  *
4378  * \param transferer The channel performing the transfer
4379  * \param channels The channels belonging to the bridge
4380  * \param is_attended false set BLINDTRANSFER and unset ATTENDEDTRANSFER
4381  * true set ATTENDEDTRANSFER and unset BLINDTRANSFER
4382  */
4383 static void set_transfer_variables_all(struct ast_channel *transferer, struct ao2_container *channels, int is_attended)
4384 {
4385  struct ao2_iterator iter;
4386  struct ast_channel *chan;
4387  const char *transferer_name;
4388  const char *transferer_bridgepeer;
4389 
4390  ast_channel_lock(transferer);
4391  transferer_name = ast_strdupa(ast_channel_name(transferer));
4392  transferer_bridgepeer = ast_strdupa(S_OR(pbx_builtin_getvar_helper(transferer, "BRIDGEPEER"), ""));
4393  ast_channel_unlock(transferer);
4394 
4395  for (iter = ao2_iterator_init(channels, 0);
4396  (chan = ao2_iterator_next(&iter));
4397  ao2_cleanup(chan)) {
4398  if (chan == transferer) {
4399  ast_bridge_set_transfer_variables(chan, transferer_bridgepeer, is_attended);
4400  } else {
4401  ast_bridge_set_transfer_variables(chan, transferer_name, is_attended);
4402  }
4403  }
4404 
4405  ao2_iterator_destroy(&iter);
4406 }
4407 
4409 {
4410  struct ast_bridge *bridge;
4411 
4412  ast_channel_lock(chan);
4413  bridge = ast_channel_get_bridge(chan);
4414  ast_channel_unlock(chan);
4415 
4416  if (bridge && ast_test_flag(&bridge->feature_flags,
4418  ao2_ref(bridge, -1);
4419  bridge = NULL;
4420  }
4421 
4422  return bridge;
4423 }
4424 
4426  struct ast_channel *transferer, const char *exten, const char *context,
4427  transfer_channel_cb new_channel_cb, void *user_data)
4428 {
4429  RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
4430  RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
4431  RAII_VAR(struct ao2_container *, channels, NULL, ao2_cleanup);
4432  RAII_VAR(struct ast_channel *, transferee, NULL, ast_channel_cleanup);
4433  RAII_VAR(struct transfer_channel_data *, user_data_wrapper, NULL, ao2_cleanup);
4434  RAII_VAR(struct ast_blind_transfer_message *, transfer_message, NULL, ao2_cleanup);
4435  int do_bridge_transfer;
4436  int transfer_prohibited;
4437  enum ast_transfer_result transfer_result;
4438 
4439  transfer_message = ast_blind_transfer_message_create(is_external, transferer, exten, context);
4440  if (!transfer_message) {
4441  /* Out of memory. Not even possible to publish a Stasis message about the
4442  * failure
4443  */
4444  ast_log(LOG_ERROR, "Unable to allocate memory for blind transfer publication from %s\n",
4445  ast_channel_name(transferer));
4446  return AST_BRIDGE_TRANSFER_FAIL;
4447  }
4448 
4449  bridge = ast_bridge_transfer_acquire_bridge(transferer);
4450  if (!bridge) {
4451  transfer_result = AST_BRIDGE_TRANSFER_INVALID;
4452  goto publish;
4453  }
4454 
4455  ast_bridge_lock(bridge);
4456  transfer_message->bridge = ast_bridge_snapshot_create(bridge);
4457  ast_bridge_unlock(bridge);
4458  if (!transfer_message->bridge) {
4459  transfer_result = AST_BRIDGE_TRANSFER_FAIL;
4460  goto publish;
4461  }
4462 
4463  transferee = ast_bridge_peer(bridge, transferer);
4464  if (transferee) {
4465  transfer_message->transferee = ast_channel_snapshot_get_latest(ast_channel_uniqueid(transferee));
4466  if (!transfer_message->transferee) {
4467  transfer_result = AST_BRIDGE_TRANSFER_FAIL;
4468  goto publish;
4469  }
4470  }
4471 
4472  ast_channel_lock(transferer);
4473  bridge_channel = ast_channel_get_bridge_channel(transferer);
4474  ast_channel_unlock(transferer);
4475  if (!bridge_channel) {
4476  transfer_result = AST_BRIDGE_TRANSFER_INVALID;
4477  goto publish;
4478  }
4479 
4480  user_data_wrapper = ao2_alloc(sizeof(*user_data_wrapper), NULL);
4481  if (!user_data_wrapper) {
4482  transfer_result = AST_BRIDGE_TRANSFER_FAIL;
4483  goto publish;
4484  }
4485 
4486  user_data_wrapper->data = user_data;
4487 
4488  /* Take off hold if they are on hold. */
4489  ast_bridge_channel_write_unhold(bridge_channel);
4490 
4491  transfer_result = try_parking(transferer, context, exten, new_channel_cb, user_data_wrapper);
4492  if (transfer_result == AST_BRIDGE_TRANSFER_SUCCESS) {
4493  goto publish;
4494  }
4495 
4496  /* Since parking didn't take control of the user_data_wrapper, we are just going to raise the completed flag now. */
4497  user_data_wrapper->completed = 1;
4498 
4499  {
4501 
4502  channels = ast_bridge_peers_nolock(bridge);
4503  if (!channels) {
4504  transfer_result = AST_BRIDGE_TRANSFER_FAIL;
4505  goto publish;
4506  }
4507  if (ao2_container_count(channels) <= 1) {
4508  transfer_result = AST_BRIDGE_TRANSFER_INVALID;
4509  goto publish;
4510  }
4511  transfer_prohibited = ast_test_flag(&bridge->feature_flags,
4513  do_bridge_transfer = ast_test_flag(&bridge->feature_flags,
4515  ao2_container_count(channels) > 2;
4516  }
4517 
4518  if (transfer_prohibited) {
4519  transfer_result = AST_BRIDGE_TRANSFER_NOT_PERMITTED;
4520  goto publish;
4521  }
4522 
4523  set_transfer_variables_all(transferer, channels, 0);
4524 
4525  if (do_bridge_transfer) {
4526  transfer_result = blind_transfer_bridge(is_external, transferer, bridge,
4527  exten, context, transferee, new_channel_cb, user_data_wrapper, transfer_message);
4528  goto publish;
4529  }
4530 
4531  /* Reaching this portion means that we're dealing with a two-party bridge */
4532 
4533  if (!transferee) {
4534  transfer_result = AST_BRIDGE_TRANSFER_FAIL;
4535  goto publish;
4536  }
4537 
4538  if (bridge_channel_internal_queue_blind_transfer(transferee, exten, context,
4539  new_channel_cb, user_data_wrapper)) {
4540  transfer_result = AST_BRIDGE_TRANSFER_FAIL;
4541  goto publish;
4542  }
4543 
4544  ast_bridge_remove(bridge, transferer);
4545  transfer_result = AST_BRIDGE_TRANSFER_SUCCESS;
4546 
4547 publish:
4548  transfer_message->result = transfer_result;
4549  ast_bridge_publish_blind_transfer(transfer_message);
4550  return transfer_result;
4551 }
4552 
4553 /*!
4554  * \internal
4555  * \brief Performs an attended transfer by moving a channel from one bridge to another
4556  *
4557  * The channel that is bridged to the source_channel is moved into the dest_bridge from
4558  * the source_bridge_channel's bridge. The swap_channel is swapped out of the dest_bridge and placed in
4559  * the source_bridge_channel's bridge.
4560  *
4561  * \note dest_bridge and source_bridge_channel's bridge MUST be locked before calling this function.
4562  *
4563  * \param dest_bridge The final bridge for the attended transfer
4564  * \param source_bridge_channel Channel who is bridged to the channel that will move
4565  * \param swap_channel Channel to be swapped out of the dest_bridge
4566  * \return The success or failure of the swap attempt
4567  */
4568 static enum ast_transfer_result bridge_swap_attended_transfer(struct ast_bridge *dest_bridge,
4569  struct ast_bridge_channel *source_bridge_channel, struct ast_channel *swap_channel)
4570 {
4571  struct ast_bridge_channel *bridged_to_source;
4572 
4573  bridged_to_source = ast_bridge_channel_peer(source_bridge_channel);
4574  if (bridged_to_source
4575  && bridged_to_source->state == BRIDGE_CHANNEL_STATE_WAIT
4576  && !ast_test_flag(&bridged_to_source->features->feature_flags,
4578  bridged_to_source->swap = swap_channel;
4579  if (bridge_do_move(dest_bridge, bridged_to_source, 1, 0)) {
4580  return AST_BRIDGE_TRANSFER_FAIL;
4581  }
4582  /* Must kick the source channel out of its bridge. */
4583  ast_bridge_channel_leave_bridge(source_bridge_channel,
4584  BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
4586  } else {
4588  }
4589 }
4590 
4591 /*!
4592  * \internal
4593  * \brief Function that performs an attended transfer when both transferer channels are bridged
4594  *
4595  * The method by which the transfer is performed is dependent on whether the bridges allow for
4596  * optimization to occur between them. If no optimization is permitted, then an unreal channel
4597  * is placed as a link between the two bridges. If optimization is permitted, then that means
4598  * we are free to perform move or merge operations in order to perform the transfer.
4599  *
4600  * \note to_transferee_bridge and to_target_bridge MUST be locked before calling this function
4601  *
4602  * \param to_transferee The channel that is bridged to the transferee
4603  * \param to_transferee_bridge_channel to_transferee's bridge_channel
4604  * \param to_transfer_target The channel that is bridged to the transfer target
4605  * \param to_target_bridge_channel to_transfer_target's bridge_channel
4606  * \param to_transferee_bridge The bridge between to_transferee and the transferee
4607  * \param to_target_bridge The bridge between to_transfer_target and the transfer_target
4608  * \param transfer_msg Data to publish for a stasis attended transfer message
4609  * \return The success or failure of the attended transfer
4610  */
4611 static enum ast_transfer_result two_bridge_attended_transfer(struct ast_channel *to_transferee,
4612  struct ast_bridge_channel *to_transferee_bridge_channel,
4613  struct ast_channel *to_transfer_target,
4614  struct ast_bridge_channel *to_target_bridge_channel,
4615  struct ast_bridge *to_transferee_bridge, struct ast_bridge *to_target_bridge,
4616  struct ast_attended_transfer_message *transfer_msg)
4617 {
4618  struct ast_bridge_channel *kick_me[] = {
4619  to_transferee_bridge_channel,
4620  to_target_bridge_channel,
4621  };
4622  enum ast_transfer_result res;
4623  struct ast_bridge *final_bridge = NULL;
4624  RAII_VAR(struct ao2_container *, channels, NULL, ao2_cleanup);
4625 
4626  channels = ast_bridge_peers_nolock(to_transferee_bridge);
4627 
4628  if (!channels) {
4630  goto end;
4631  }
4632 
4633  set_transfer_variables_all(to_transferee, channels, 1);
4634 
4635  switch (ast_bridges_allow_optimization(to_transferee_bridge, to_target_bridge)) {
4637  final_bridge = to_transferee_bridge;
4638  res = bridge_swap_attended_transfer(to_transferee_bridge, to_target_bridge_channel, to_transferee);
4639  goto end;
4641  final_bridge = to_target_bridge;
4642  res = bridge_swap_attended_transfer(to_target_bridge, to_transferee_bridge_channel, to_transfer_target);
4643  goto end;
4645  final_bridge = to_transferee_bridge;
4646  bridge_do_merge(to_transferee_bridge, to_target_bridge, kick_me, ARRAY_LEN(kick_me), 0);
4648  goto end;
4650  final_bridge = to_target_bridge;
4651  bridge_do_merge(to_target_bridge, to_transferee_bridge, kick_me, ARRAY_LEN(kick_me), 0);
4653  goto end;
4655  default:
4656  /* Just because optimization wasn't doable doesn't necessarily mean
4657  * that we can actually perform the transfer. Some reasons for non-optimization
4658  * indicate bridge invalidity, so let's check those before proceeding.
4659  */
4660  if (to_transferee_bridge->inhibit_merge || to_transferee_bridge->dissolved ||
4661  to_target_bridge->inhibit_merge || to_target_bridge->dissolved) {
4663  }
4664 
4665  return attended_transfer_bridge(to_transferee, to_transfer_target,
4666  to_transferee_bridge, to_target_bridge, transfer_msg);
4667  }
4668 
4669 end:
4670  if (res == AST_BRIDGE_TRANSFER_SUCCESS) {
4671  ast_attended_transfer_message_add_merge(transfer_msg, final_bridge);
4672  }
4673 
4674  return res;
4675 }
4676 
4678  struct ast_channel *to_transfer_target)
4679 {
4680  RAII_VAR(struct ast_bridge *, to_transferee_bridge, NULL, ao2_cleanup);
4681  RAII_VAR(struct ast_bridge *, to_target_bridge, NULL, ao2_cleanup);
4682  RAII_VAR(struct ast_bridge_channel *, to_transferee_bridge_channel, NULL, ao2_cleanup);
4683  RAII_VAR(struct ast_bridge_channel *, to_target_bridge_channel, NULL, ao2_cleanup);
4684  RAII_VAR(struct ao2_container *, channels, NULL, ao2_cleanup);
4685  RAII_VAR(struct ast_channel *, transferee, NULL, ao2_cleanup);
4686  RAII_VAR(struct ast_attended_transfer_message *, transfer_msg, NULL, ao2_cleanup);
4687  struct ast_bridge *the_bridge = NULL;
4688  struct ast_channel *chan_bridged;
4689  struct ast_channel *chan_unbridged;
4690  int transfer_prohibited;
4691  int do_bridge_transfer;
4692  enum ast_transfer_result res;
4693  const char *app = NULL;
4694  int hangup_target = 0;
4695 
4696  to_transferee_bridge = ast_bridge_transfer_acquire_bridge(to_transferee);
4697  to_target_bridge = ast_bridge_transfer_acquire_bridge(to_transfer_target);
4698 
4699  transfer_msg = ast_attended_transfer_message_create(1, to_transferee, to_transferee_bridge,
4700  to_transfer_target, to_target_bridge, NULL, NULL);
4701  if (!transfer_msg) {
4702  ast_log(LOG_ERROR, "Unable to create Stasis publication for attended transfer from %s\n",
4703  ast_channel_name(to_transferee));
4704  return AST_BRIDGE_TRANSFER_FAIL;
4705  }
4706 
4707  /* They can't both be unbridged, you silly goose! */
4708  if (!to_transferee_bridge && !to_target_bridge) {
4710  goto end;
4711  }
4712 
4713  ast_channel_lock(to_transferee);
4714  to_transferee_bridge_channel = ast_channel_get_bridge_channel(to_transferee);
4715  ast_channel_unlock(to_transferee);
4716 
4717  ast_channel_lock(to_transfer_target);
4718  to_target_bridge_channel = ast_channel_get_bridge_channel(to_transfer_target);
4719  ast_channel_unlock(to_transfer_target);
4720 
4721  if (to_transferee_bridge_channel) {
4722  /* Take off hold if they are on hold. */
4723  if (ast_bridge_channel_write_unhold(to_transferee_bridge_channel)) {
4724  ast_log(LOG_ERROR, "Transferee channel disappeared during transfer!\n");
4726  goto end;
4727  }
4728  }
4729 
4730  if (to_target_bridge_channel) {
4731  const char *target_complete_sound;
4732 
4733  /* Take off hold if they are on hold. */
4734  if (ast_bridge_channel_write_unhold(to_target_bridge_channel)) {
4735  ast_log(LOG_ERROR, "Target channel disappeared during transfer!\n");
4737  goto end;
4738  }
4739 
4740  /* Is there a courtesy sound to play to the target? */
4741  ast_channel_lock(to_transfer_target);
4742  target_complete_sound = pbx_builtin_getvar_helper(to_transfer_target,
4743  "ATTENDED_TRANSFER_COMPLETE_SOUND");
4744  if (!ast_strlen_zero(target_complete_sound)) {
4745  target_complete_sound = ast_strdupa(target_complete_sound);
4746  } else {
4747  target_complete_sound = NULL;
4748  }
4749  ast_channel_unlock(to_transfer_target);
4750  if (!target_complete_sound) {
4751  ast_channel_lock(to_transferee);
4752  target_complete_sound = pbx_builtin_getvar_helper(to_transferee,
4753  "ATTENDED_TRANSFER_COMPLETE_SOUND");
4754  if (!ast_strlen_zero(target_complete_sound)) {
4755  target_complete_sound = ast_strdupa(target_complete_sound);
4756  } else {
4757  target_complete_sound = NULL;
4758  }
4759  ast_channel_unlock(to_transferee);
4760  }
4761  if (target_complete_sound) {
4762  ast_bridge_channel_write_playfile(to_target_bridge_channel, NULL,
4763  target_complete_sound, NULL);
4764  }
4765  }
4766 
4767  /* Let's get the easy one out of the way first */
4768  if (to_transferee_bridge && to_target_bridge) {
4769 
4770  if (!to_transferee_bridge_channel || !to_target_bridge_channel) {
4772  goto end;
4773  }
4774 
4775  ast_bridge_lock_both(to_transferee_bridge, to_target_bridge);
4776  res = two_bridge_attended_transfer(to_transferee, to_transferee_bridge_channel,
4777  to_transfer_target, to_target_bridge_channel,
4778  to_transferee_bridge, to_target_bridge, transfer_msg);
4779  ast_bridge_unlock(to_transferee_bridge);
4780  ast_bridge_unlock(to_target_bridge);
4781 
4782  hangup_target = 1;
4783  goto end;
4784  }
4785 
4786  the_bridge = to_transferee_bridge ?: to_target_bridge;
4787  chan_bridged = to_transferee_bridge ? to_transferee : to_transfer_target;
4788  chan_unbridged = to_transferee_bridge ? to_transfer_target : to_transferee;
4789 
4790  /*
4791  * Race condition makes it possible for app to be NULL, so get the app prior to
4792  * transferring with a fallback of "unknown".
4793  */
4794  app = ast_strdupa(ast_channel_appl(chan_unbridged) ?: "unknown");
4795 
4796  {
4797  int chan_count;
4799 
4800  channels = ast_bridge_peers_nolock(the_bridge);
4801  if (!channels) {
4803  goto end;
4804  }
4805  chan_count = ao2_container_count(channels);
4806  if (chan_count <= 1) {
4808  goto end;
4809  }
4810  transfer_prohibited = ast_test_flag(&the_bridge->feature_flags,
4812  do_bridge_transfer = ast_test_flag(&the_bridge->feature_flags,
4814  chan_count > 2;
4815  }
4816 
4817  if (transfer_prohibited) {
4819  goto end;
4820  }
4821 
4822  set_transfer_variables_all(to_transferee, channels, 1);
4823 
4824  if (do_bridge_transfer) {
4825  /*
4826  * Hang up the target if it was bridged. Note, if it is not bridged
4827  * it is hung up during the masquerade.
4828  */
4829  hangup_target = chan_bridged == to_transfer_target;
4830  ast_bridge_lock(the_bridge);
4831  res = attended_transfer_bridge(chan_bridged, chan_unbridged, the_bridge, NULL, transfer_msg);
4832  ast_bridge_unlock(the_bridge);
4833  goto end;
4834  }
4835 
4836  transferee = get_transferee(channels, chan_bridged);
4837  if (!transferee) {
4839  goto end;
4840  }
4841 
4842  if (bridge_channel_internal_queue_attended_transfer(transferee, chan_unbridged)) {
4844  goto end;
4845  }
4846 
4847  ast_bridge_remove(the_bridge, chan_bridged);
4848 
4849  ast_attended_transfer_message_add_app(transfer_msg, app, NULL);
4851 
4852 end:
4853  if ((res == AST_BRIDGE_TRANSFER_SUCCESS && hangup_target) || res == AST_BRIDGE_TRANSFER_FAIL) {
4854  ast_softhangup(to_transfer_target, AST_SOFTHANGUP_DEV);
4855  }
4856 
4857  transfer_msg->result = res;
4859  return res;
4860 }
4861 
4862 /*!
4863  * \internal
4864  * \brief Service the bridge manager request.
4865  * \since 12.0.0
4866  *
4867  * \param bridge requesting service.
4868  */
4869 static void bridge_manager_service(struct ast_bridge *bridge)
4870 {
4871  ast_bridge_lock(bridge);
4872  if (bridge->callid) {
4874  }
4875 
4876  /* Do any pending bridge actions. */
4877  bridge_handle_actions(bridge);
4878  ast_bridge_unlock(bridge);
4879 }
4880 
4881 /*!
4882  * \internal
4883  * \brief Bridge manager service thread.
4884  * \since 12.0.0
4885  */
4886 static void *bridge_manager_thread(void *data)
4887 {
4888  struct bridge_manager_controller *manager = data;
4889  struct bridge_manager_request *request;
4890 
4891  ao2_lock(manager);
4892  while (!manager->stop) {
4893  request = AST_LIST_REMOVE_HEAD(&manager->service_requests, node);
4894  if (!request) {
4895  ast_cond_wait(&manager->cond, ao2_object_get_lockaddr(manager));
4896  continue;
4897  }
4898  ao2_unlock(manager);
4899 
4900  /* Service the bridge. */
4901  bridge_manager_service(request->bridge);
4902  ao2_ref(request->bridge, -1);
4903  ast_free(request);
4904 
4905  ao2_lock(manager);
4906  }
4907  ao2_unlock(manager);
4908 
4909  return NULL;
4910 }
4911 
4912 /*!
4913  * \internal
4914  * \brief Destroy the bridge manager controller.
4915  * \since 12.0.0
4916  *
4917  * \param obj Bridge manager to destroy.
4918  */
4919 static void bridge_manager_destroy(void *obj)
4920 {
4921  struct bridge_manager_controller *manager = obj;
4922  struct bridge_manager_request *request;
4923 
4924  if (manager->thread != AST_PTHREADT_NULL) {
4925  /* Stop the manager thread. */
4926  ao2_lock(manager);
4927  manager->stop = 1;
4928  ast_cond_signal(&manager->cond);
4929  ao2_unlock(manager);
4930  ast_debug(1, "Waiting for bridge manager thread to die.\n");
4931  pthread_join(manager->thread, NULL);
4932  }
4933 
4934  /* Destroy the service request queue. */
4935  while ((request = AST_LIST_REMOVE_HEAD(&manager->service_requests, node))) {
4936  ao2_ref(request->bridge, -1);
4937  ast_free(request);
4938  }
4939 
4940  ast_cond_destroy(&manager->cond);
4941 }
4942 
4943 /*!
4944  * \internal
4945  * \brief Create the bridge manager controller.
4946  * \since 12.0.0
4947  *
4948  * \return manager on success.
4949  * \retval NULL on error.
4950  */
4951 static struct bridge_manager_controller *bridge_manager_create(void)
4952 {
4953  struct bridge_manager_controller *manager;
4954 
4955  manager = ao2_alloc(sizeof(*manager), bridge_manager_destroy);
4956  if (!manager) {
4957  /* Well. This isn't good. */
4958  return NULL;
4959  }
4960  ast_cond_init(&manager->cond, NULL);
4962 
4963  /* Create the bridge manager thread. */
4964  if (ast_pthread_create(&manager->thread, NULL, bridge_manager_thread, manager)) {
4965  /* Well. This isn't good either. */
4966  manager->thread = AST_PTHREADT_NULL;
4967  ao2_ref(manager, -1);
4968  manager = NULL;
4969  }
4970 
4971  return manager;
4972 }
4973 
4974 /*!
4975  * \internal
4976  * \brief Bridge ao2 container sort function.
4977  * \since 12.0.0
4978  *
4979  * \param obj_left pointer to the (user-defined part) of an object.
4980  * \param obj_right pointer to the (user-defined part) of an object.
4981  * \param flags flags from ao2_callback()
4982  * OBJ_POINTER - if set, 'obj_right', is an object.
4983  * OBJ_KEY - if set, 'obj_right', is a search key item that is not an object.
4984  * OBJ_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
4985  *
4986  * \retval <0 if obj_left < obj_right
4987  * \retval =0 if obj_left == obj_right
4988  * \retval >0 if obj_left > obj_right
4989  */
4990 static int bridge_sort_cmp(const void *obj_left, const void *obj_right, int flags)
4991 {
4992  const struct ast_bridge *bridge_left = obj_left;
4993  const struct ast_bridge *bridge_right = obj_right;
4994  const char *right_key = obj_right;
4995  int cmp;
4996 
4997  switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
4998  default:
4999  case OBJ_POINTER:
5000  right_key = bridge_right->uniqueid;
5001  /* Fall through */
5002  case OBJ_KEY:
5003  cmp = strcmp(bridge_left->uniqueid, right_key);
5004  break;
5005  case OBJ_PARTIAL_KEY:
5006  cmp = strncmp(bridge_left->uniqueid, right_key, strlen(right_key));
5007  break;
5008  }
5009  return cmp;
5010 }
5011 
5012 struct ast_bridge *ast_bridge_find_by_id(const char *bridge_id)
5013 {
5014  return ao2_find(bridges, bridge_id, OBJ_SEARCH_KEY);
5015 }
5016 
5017 static int complete_bridge_live_search(void *obj, void *arg, int flags)
5018 {
5019  struct ast_bridge *bridge = obj;
5020 
5021  if (ast_cli_completion_add(ast_strdup(bridge->uniqueid))) {
5022  return CMP_STOP;
5023  }
5024 
5025  return 0;
5026 }
5027 
5028 static char *complete_bridge_live(const char *word)
5029 {
5030  ao2_callback(bridges, ast_strlen_zero(word) ? 0 : OBJ_PARTIAL_KEY,
5031  complete_bridge_live_search, (char *) word);
5032 
5033  return NULL;
5034 }
5035 
5036 static char *handle_bridge_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
5037 {
5038 #define FORMAT_HDR "%-36s %5s %-15s %-15s %s\n"
5039 #define FORMAT_ROW "%-36s %5u %-15s %-15s %s\n"
5040 
5041  struct ao2_iterator iter;
5042  struct ast_bridge *bridge;
5043 
5044  switch (cmd) {
5045  case CLI_INIT:
5046  e->command = "bridge show all";
5047  e->usage =
5048  "Usage: bridge show all\n"
5049  " List all bridges\n";
5050  return NULL;
5051  case CLI_GENERATE:
5052  return NULL;
5053  }
5054 
5055  ast_cli(a->fd, FORMAT_HDR, "Bridge-ID", "Chans", "Type", "Technology", "Duration");
5056 
5057  iter = ao2_iterator_init(bridges, 0);
5058  for (; (bridge = ao2_iterator_next(&iter)); ao2_ref(bridge, -1)) {
5059  struct ast_bridge_snapshot *snapshot = ast_bridge_get_snapshot(bridge);
5060  char print_time[32];
5061 
5062  if (snapshot) {
5063  ast_format_duration_hh_mm_ss(ast_tvnow().tv_sec - snapshot->creationtime.tv_sec, print_time, sizeof(print_time));
5064  ast_cli(a->fd, FORMAT_ROW,
5065  snapshot->uniqueid,
5066  snapshot->num_channels,
5067  S_OR(snapshot->subclass, "<unknown>"),
5068  S_OR(snapshot->technology, "<unknown>"),
5069  print_time);
5070  ao2_ref(snapshot, -1);
5071  }
5072  }
5073  ao2_iterator_destroy(&iter);
5074 
5075  return CLI_SUCCESS;
5076 
5077 #undef FORMAT_HDR
5078 #undef FORMAT_ROW
5079 }
5080 
5081 /*! \brief Internal callback function for sending channels in a bridge to the CLI */
5082 static int bridge_show_specific_print_channel(void *obj, void *arg, int flags)
5083 {
5084  const char *uniqueid = obj;
5085  struct ast_cli_args *a = arg;
5086  struct ast_channel_snapshot *snapshot;
5087 
5088  snapshot = ast_channel_snapshot_get_latest(uniqueid);
5089  if (!snapshot) {
5090  return 0;
5091  }
5092 
5093  ast_cli(a->fd, "Channel: %s\n", snapshot->base->name);
5094  ao2_ref(snapshot, -1);
5095 
5096  return 0;
5097 }
5098 
5099 static char *handle_bridge_show_specific(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
5100 {
5101  struct ast_bridge_snapshot *snapshot;
5102  char print_time[32];
5103 
5104  switch (cmd) {
5105  case CLI_INIT:
5106  e->command = "bridge show";
5107  e->usage =
5108  "Usage: bridge show <bridge-id>\n"
5109  " Show information about the <bridge-id> bridge\n";
5110  return NULL;
5111  case CLI_GENERATE:
5112  if (a->pos == 2) {
5113  return complete_bridge_live(a->word);
5114  }
5115  return NULL;
5116  }
5117 
5118  if (a->argc != 3) {
5119  return CLI_SHOWUSAGE;
5120  }
5121 
5122  snapshot = ast_bridge_get_snapshot_by_uniqueid(a->argv[2]);
5123  if (!snapshot) {
5124  ast_cli(a->fd, "Bridge '%s' not found\n", a->argv[2]);
5125  return CLI_SUCCESS;
5126  }
5127 
5128  ast_format_duration_hh_mm_ss(ast_tvnow().tv_sec - snapshot->creationtime.tv_sec, print_time, sizeof(print_time));
5129 
5130  ast_cli(a->fd, "Id: %s\n", snapshot->uniqueid);
5131  ast_cli(a->fd, "Type: %s\n", S_OR(snapshot->subclass, "<unknown>"));
5132  ast_cli(a->fd, "Technology: %s\n", S_OR(snapshot->technology, "<unknown>"));
5133  ast_cli(a->fd, "Subclass: %s\n", snapshot->subclass);
5134  ast_cli(a->fd, "Creator: %s\n", snapshot->creator);
5135  ast_cli(a->fd, "Name: %s\n", snapshot->name);
5136  ast_cli(a->fd, "Video-Mode: %s\n", ast_bridge_video_mode_to_string(snapshot->video_mode));
5137  ast_cli(a->fd, "Video-Source-Id: %s\n", snapshot->video_source_id);
5138  ast_cli(a->fd, "Num-Channels: %u\n", snapshot->num_channels);
5139  ast_cli(a->fd, "Num-Active: %u\n", snapshot->num_active);
5140  ast_cli(a->fd, "Duration: %s\n", print_time);
5142  ao2_ref(snapshot, -1);
5143 
5144  return CLI_SUCCESS;
5145 }
5146 
5147 #ifdef AST_DEVMODE
5148 static char *handle_bridge_destroy_specific(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
5149 {
5150  struct ast_bridge *bridge;
5151 
5152  switch (cmd) {
5153  case CLI_INIT:
5154  e->command = "bridge destroy";
5155  e->usage =
5156  "Usage: bridge destroy <bridge-id>\n"
5157  " Destroy the <bridge-id> bridge\n";
5158  return NULL;
5159  case CLI_GENERATE:
5160  if (a->pos == 2) {
5161  return complete_bridge_live(a->word);
5162  }
5163  return NULL;
5164  }
5165 
5166  if (a->argc != 3) {
5167  return CLI_SHOWUSAGE;
5168  }
5169 
5170  bridge = ast_bridge_find_by_id(a->argv[2]);
5171  if (!bridge) {
5172  ast_cli(a->fd, "Bridge '%s' not found\n", a->argv[2]);
5173  return CLI_SUCCESS;
5174  }
5175 
5176  ast_cli(a->fd, "Destroying bridge '%s'\n", a->argv[2]);
5177  ast_bridge_destroy(bridge, 0);
5178 
5179  return CLI_SUCCESS;
5180 }
5181 #endif
5182 
5183 static char *complete_bridge_participant(const char *bridge_name, const char *word)
5184 {
5185  struct ast_bridge *bridge;
5186  struct ast_bridge_channel *bridge_channel;
5187  int wordlen;
5188 
5189  bridge = ast_bridge_find_by_id(bridge_name);
5190  if (!bridge) {
5191  return NULL;
5192  }
5193 
5194  wordlen = strlen(word);
5195 
5196  ast_bridge_lock(bridge);
5197  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
5198  if (!strncasecmp(ast_channel_name(bridge_channel->chan), word, wordlen)) {
5199  if (ast_cli_completion_add(ast_strdup(ast_channel_name(bridge_channel->chan)))) {
5200  break;
5201  }
5202  }
5203  }
5204  ast_bridge_unlock(bridge);
5205 
5206  ao2_ref(bridge, -1);
5207 
5208  return NULL;
5209 }
5210 
5211 static char *handle_bridge_kick_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
5212 {
5213  static const char * const completions[] = { "all", NULL };
5214  struct ast_bridge *bridge;
5215 
5216  switch (cmd) {
5217  case CLI_INIT:
5218  e->command = "bridge kick";
5219  e->usage =
5220  "Usage: bridge kick <bridge-id> <channel-name | all>\n"
5221  " Kick the <channel-name> channel out of the <bridge-id> bridge\n"
5222  " If all is specified as the channel name then all channels will be\n"
5223  " kicked out of the bridge.\n";
5224  return NULL;
5225  case CLI_GENERATE:
5226  if (a->pos == 2) {
5227  return complete_bridge_live(a->word);
5228  }
5229  if (a->pos == 3) {
5230  ast_cli_complete(a->word, completions, -1);
5231  return complete_bridge_participant(a->argv[2], a->word);
5232  }
5233  return NULL;
5234  }
5235 
5236  if (a->argc != 4) {
5237  return CLI_SHOWUSAGE;
5238  }
5239 
5240  bridge = ast_bridge_find_by_id(a->argv[2]);
5241  if (!bridge) {
5242  ast_cli(a->fd, "Bridge '%s' not found\n", a->argv[2]);
5243  return CLI_SUCCESS;
5244  }
5245 
5246  if (!strcasecmp(a->argv[3], "all")) {
5247  struct ast_bridge_channel *bridge_channel;
5248 
5249  ast_cli(a->fd, "Kicking all channels from bridge '%s'\n", a->argv[2]);
5250 
5251  ast_bridge_lock(bridge);
5252  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
5253  ast_bridge_channel_queue_callback(bridge_channel, 0, kick_it, NULL, 0);
5254  }
5255  ast_bridge_unlock(bridge);
5256  } else {
5257  struct ast_channel *chan;
5258 
5259  chan = ast_channel_get_by_name_prefix(a->argv[3], strlen(a->argv[3]));
5260  if (!chan) {
5261  ast_cli(a->fd, "Channel '%s' not found\n", a->argv[3]);
5262  ao2_ref(bridge, -1);
5263  return CLI_SUCCESS;
5264  }
5265 
5266  ast_cli(a->fd, "Kicking channel '%s' from bridge '%s'\n",
5267  ast_channel_name(chan), a->argv[2]);
5268  ast_bridge_kick(bridge, chan);
5269  ast_channel_unref(chan);
5270  }
5271 
5272  ao2_ref(bridge, -1);
5273  return CLI_SUCCESS;
5274 }
5275 
5276 /*! Bridge technology capabilities to string. */
5277 static const char *tech_capability2str(uint32_t capabilities)
5278 {
5279  const char *type;
5280 
5281  if (capabilities & AST_BRIDGE_CAPABILITY_HOLDING) {
5282  type = "Holding";
5283  } else if (capabilities & AST_BRIDGE_CAPABILITY_EARLY) {
5284  type = "Early";
5285  } else if (capabilities & AST_BRIDGE_CAPABILITY_NATIVE) {
5286  type = "Native";
5287  } else if (capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX) {
5288  type = "1to1Mix";
5289  } else if (capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) {
5290  type = "MultiMix";
5291  } else {
5292  type = "<Unknown>";
5293  }
5294  return type;
5295 }
5296 
5297 static char *handle_bridge_technology_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
5298 {
5299 #define FORMAT_HDR "%-20s %-20s %8s %s\n"
5300 #define FORMAT_ROW "%-20s %-20s %8u %s\n"
5301 
5302  struct ast_bridge_technology *cur;
5303 
5304  switch (cmd) {
5305  case CLI_INIT:
5306  e->command = "bridge technology show";
5307  e->usage =
5308  "Usage: bridge technology show\n"
5309  " List registered bridge technologies\n";
5310  return NULL;
5311  case CLI_GENERATE:
5312  return NULL;
5313  }
5314 
5315  ast_cli(a->fd, FORMAT_HDR, "Name", "Type", "Priority", "Suspended");
5317  AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {
5318  const char *type;
5319 
5320  /* Decode type for display */
5321  type = tech_capability2str(cur->capabilities);
5322 
5323  ast_cli(a->fd, FORMAT_ROW, cur->name, type, cur->preference,
5324  AST_CLI_YESNO(cur->suspended));
5325  }
5327  return CLI_SUCCESS;
5328 
5329 #undef FORMAT
5330 }
5331 
5332 static char *complete_bridge_technology(const char *word)
5333 {
5334  struct ast_bridge_technology *cur;
5335  int wordlen;
5336 
5337  wordlen = strlen(word);
5339  AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {
5340  if (!strncasecmp(cur->name, word, wordlen)) {
5341  if (ast_cli_completion_add(ast_strdup(cur->name))) {
5342  break;
5343  }
5344  }
5345  }
5347 
5348  return NULL;
5349 }
5350 
5351 static char *handle_bridge_technology_suspend(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
5352 {
5353  struct ast_bridge_technology *cur;
5354  int suspend;
5355  int successful;
5356 
5357  switch (cmd) {
5358  case CLI_INIT:
5359  e->command = "bridge technology {suspend|unsuspend}";
5360  e->usage =
5361  "Usage: bridge technology {suspend|unsuspend} <technology-name>\n"
5362  " Suspend or unsuspend a bridge technology.\n";
5363  return NULL;
5364  case CLI_GENERATE:
5365  if (a->pos == 3) {
5366  return complete_bridge_technology(a->word);
5367  }
5368  return NULL;
5369  }
5370 
5371  if (a->argc != 4) {
5372  return CLI_SHOWUSAGE;
5373  }
5374 
5375  suspend = !strcasecmp(a->argv[2], "suspend");
5376  successful = 0;
5378  AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {
5379  if (!strcasecmp(cur->name, a->argv[3])) {
5380  successful = 1;
5381  if (suspend) {
5383  } else {
5385  }
5386  break;
5387  }
5388  }
5390 
5391  if (successful) {
5392  if (suspend) {
5393  ast_cli(a->fd, "Suspended bridge technology '%s'\n", a->argv[3]);
5394  } else {
5395  ast_cli(a->fd, "Unsuspended bridge technology '%s'\n", a->argv[3]);
5396  }
5397  } else {
5398  ast_cli(a->fd, "Bridge technology '%s' not found\n", a->argv[3]);
5399  }
5400 
5401  return CLI_SUCCESS;
5402 }
5403 
5404 static struct ast_cli_entry bridge_cli[] = {
5405  AST_CLI_DEFINE(handle_bridge_show_all, "List all bridges"),
5406  AST_CLI_DEFINE(handle_bridge_show_specific, "Show information about a bridge"),
5407 #ifdef AST_DEVMODE
5408  AST_CLI_DEFINE(handle_bridge_destroy_specific, "Destroy a bridge"),
5409 #endif
5410  AST_CLI_DEFINE(handle_bridge_kick_channel, "Kick a channel from a bridge"),
5411  AST_CLI_DEFINE(handle_bridge_technology_show, "List registered bridge technologies"),
5412  AST_CLI_DEFINE(handle_bridge_technology_suspend, "Suspend/unsuspend a bridge technology"),
5413 };
5414 
5415 
5416 static int handle_manager_bridge_tech_suspend(struct mansession *s, const struct message *m, int suspend)
5417 {
5418  const char *name = astman_get_header(m, "BridgeTechnology");
5419  struct ast_bridge_technology *cur;
5420  int successful = 0;
5421 
5422  if (ast_strlen_zero(name)) {
5423  astman_send_error(s, m, "BridgeTechnology must be provided");
5424  return 0;
5425  }
5426 
5428  AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {
5429 
5430  if (!strcasecmp(cur->name, name)) {
5431  successful = 1;
5432  if (suspend) {
5434  } else {
5436  }
5437  break;
5438  }
5439  }
5441  if (!successful) {
5442  astman_send_error(s, m, "BridgeTechnology not found");
5443  return 0;
5444  }
5445 
5446  astman_send_ack(s, m, (suspend ? "Suspended bridge technology" : "Unsuspended bridge technology"));
5447  return 0;
5448 }
5449 
5450 static int manager_bridge_tech_suspend(struct mansession *s, const struct message *m)
5451 {
5452  return handle_manager_bridge_tech_suspend(s, m, 1);
5453 }
5454 
5455 static int manager_bridge_tech_unsuspend(struct mansession *s, const struct message *m)
5456 {
5457  return handle_manager_bridge_tech_suspend(s, m, 0);
5458 }
5459 
5460 static int manager_bridge_tech_list(struct mansession *s, const struct message *m)
5461 {
5462  const char *id = astman_get_header(m, "ActionID");
5463  RAII_VAR(struct ast_str *, id_text, ast_str_create(128), ast_free);
5464  struct ast_bridge_technology *cur;
5465  int num_items = 0;
5466 
5467  if (!id_text) {
5468  astman_send_error(s, m, "Internal error");
5469  return -1;
5470  }
5471 
5472  if (!ast_strlen_zero(id)) {
5473  ast_str_set(&id_text, 0, "ActionID: %s\r\n", id);
5474  }
5475 
5476  astman_send_listack(s, m, "Bridge technology listing will follow", "start");
5477 
5479  AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {
5480  const char *type;
5481 
5482  type = tech_capability2str(cur->capabilities);
5483 
5484  astman_append(s,
5485  "Event: BridgeTechnologyListItem\r\n"
5486  "BridgeTechnology: %s\r\n"
5487  "BridgeType: %s\r\n"
5488  "BridgePriority: %u\r\n"
5489  "BridgeSuspended: %s\r\n"
5490  "%s"
5491  "\r\n",
5492  cur->name, type, cur->preference, AST_YESNO(cur->suspended),
5493  ast_str_buffer(id_text));
5494  ++num_items;
5495  }
5497 
5498  astman_send_list_complete_start(s, m, "BridgeTechnologyListComplete", num_items);
5500 
5501  return 0;
5502 }
5503 
5504 /*!
5505  * \internal
5506  * \brief Print bridge object key (name).
5507  * \since 12.0.0
5508  *
5509  * \param v_obj A pointer to the object we want the key printed.
5510  * \param where User data needed by prnt to determine where to put output.
5511  * \param prnt Print output callback function to use.
5512  */
5513 static void bridge_prnt_obj(void *v_obj, void *where, ao2_prnt_fn *prnt)
5514 {
5515  struct ast_bridge *bridge = v_obj;
5516 
5517  if (!bridge) {
5518  return;
5519  }
5520  prnt(where, "%s %s chans:%u",
5521  bridge->uniqueid, bridge->v_table->name, bridge->num_channels);
5522 }
5523 
5524 /*!
5525  * \internal
5526  * \brief Shutdown the bridging system. Stuff to do on graceful shutdown.
5527  * \since 13.3.0
5528  */
5529 static void bridge_cleanup(void)
5530 {
5531  ast_manager_unregister("BridgeTechnologyList");
5532  ast_manager_unregister("BridgeTechnologySuspend");
5533  ast_manager_unregister("BridgeTechnologyUnsuspend");
5534  ast_cli_unregister_multiple(bridge_cli, ARRAY_LEN(bridge_cli));
5535  ao2_container_unregister("bridges");
5536 
5537  ao2_cleanup(bridges);
5538  bridges = NULL;
5539  ao2_cleanup(bridge_manager);
5540  bridge_manager = NULL;
5541 }
5542 
5544 {
5545  ast_register_cleanup(bridge_cleanup);
5546 
5547  if (ast_stasis_bridging_init()) {
5548  return -1;
5549  }
5550 
5551  bridge_manager = bridge_manager_create();
5552  if (!bridge_manager) {
5553  return -1;
5554  }
5555 
5557  AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE, bridge_sort_cmp, NULL);
5558  if (!bridges) {
5559  return -1;
5560  }
5561  ao2_container_register("bridges", bridges, bridge_prnt_obj);
5562 
5564 
5565  ast_cli_register_multiple(bridge_cli, ARRAY_LEN(bridge_cli));
5566 
5567  ast_manager_register_xml_core("BridgeTechnologyList", 0, manager_bridge_tech_list);
5568  ast_manager_register_xml_core("BridgeTechnologySuspend", 0, manager_bridge_tech_suspend);
5569  ast_manager_register_xml_core("BridgeTechnologyUnsuspend", 0, manager_bridge_tech_unsuspend);
5570 
5571  return 0;
5572 }
const char * type
Definition: datastore.h:32
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Definition: vector.h:174
Internal bridge impart wait condition and associated conditional.
Definition: bridge.c:1475
struct ast_blind_transfer_message * ast_blind_transfer_message_create(int is_external, struct ast_channel *transferer, const char *exten, const char *context)
Create a blind transfer message to be published.
struct ast_bridge * ast_bridge_base_new(uint32_t capabilities, unsigned int flags, const char *creator, const char *name, const char *id)
Create a new base class bridge.
Definition: bridge.c:934
int ast_bridge_interval_hook(struct ast_bridge_features *features, enum ast_bridge_hook_timer_option flags, unsigned int interval, ast_bridge_hook_callback callback, void *hook_pvt, ast_bridge_hook_pvt_destructor destructor, enum ast_bridge_hook_remove_flags remove_flags)
Attach an interval hook to a bridge features structure.
Definition: bridge.c:3319
void ast_party_connected_line_init(struct ast_party_connected_line *init)
Initialize the given connected line structure.
Definition: channel.c:2022
struct ao2_container * channels
Definition: bridge.h:331
Main Channel structure associated with a channel.
Definition: test_heap.c:38
Local proxy channel special access.
Music on hold handling.
enum bridge_channel_thread_state activity
The bridge channel thread activity.
Caching pattern for Stasis Message Bus API topics.
int ast_connected_line_build_data(unsigned char *data, size_t datalen, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
Build the connected line information data frame.
Definition: channel.c:8697
void ast_bridge_set_remb_estimated_bitrate(struct ast_bridge *bridge, float estimated_bitrate)
Force the REMB report estimated bitrate to a specific max value.
Definition: bridge.c:3807
#define ast_frdup(fr)
Copies a frame.
struct ast_bridge_methods ast_bridge_base_v_table
Bridge base class virtual method table.
Definition: bridge.c:923
void( ao2_prnt_fn)(void *where, const char *fmt,...)
Print output.
Definition: astobj2.h:1435
int ast_bridge_talk_detector_hook(struct ast_bridge_features *features, ast_bridge_talking_indicate_callback callback, void *hook_pvt, ast_bridge_hook_pvt_destructor destructor, enum ast_bridge_hook_remove_flags remove_flags)
Attach a bridge channel talk detection hook to a bridge features structure.
Definition: bridge.c:3295
void ast_bridge_run_after_goto(struct ast_channel *chan)
Run a PBX on any after bridge goto location.
Definition: bridge_after.c:525
struct timeval creationtime
Definition: bridge.h:343
struct ast_channel_snapshot_base * base
Asterisk locking-related definitions:
void astman_append(struct mansession *s, const char *fmt,...)
Definition: manager.c:3310
#define ast_bridge_lock_both(bridge1, bridge2)
Lock two bridges.
Definition: bridge.h:488
Asterisk main include file. File version handling, generic pbx functions.
struct ast_flags feature_flags
Definition: bridge.h:369
#define AST_LIST_FIRST(head)
Returns the first entry contained in a list.
Definition: linkedlists.h:421
const ast_string_field uniqueid
Definition: bridge.h:401
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
void ast_bridge_set_sfu_video_mode(struct ast_bridge *bridge)
Set the bridge to be a selective forwarding unit.
Definition: bridge.c:3774
struct ast_bridge_features * features
int ast_bridging_init(void)
Initialize the bridging system.
Definition: bridge.c:5543
struct ast_flags flags
int(* ast_bridge_talking_indicate_callback)(struct ast_bridge_channel *bridge_channel, void *hook_pvt, int talking)
Talking indicator callback.
ast_bridge_video_mode_type
Video source modes.
Definition: bridge.h:98
unsigned int num_active
Definition: bridge.h:375
int ast_bridge_features_enable(struct ast_bridge_features *features, enum ast_bridge_builtin_feature feature, const char *dtmf, void *config, ast_bridge_hook_pvt_destructor destructor, enum ast_bridge_hook_remove_flags remove_flags)
Enable a built in feature on a bridge features structure.
Definition: bridge.c:3365
unsigned int stop
Definition: bridge.c:168
struct bridge_manager_request::@311 node
struct ast_bridge::@188 action_queue
#define AST_UNREAL_OPTIMIZE_BEGUN
Definition: core_unreal.h:110
struct ast_bridge * ast_bridge_transfer_acquire_bridge(struct ast_channel *chan)
Acquire the channel's bridge for transfer purposes.
Definition: bridge.c:4408
struct ast_channel * ast_bridge_peer_nolock(struct ast_bridge *bridge, struct ast_channel *chan)
Get the channel's bridge peer only if the bridge is two-party.
Definition: bridge.c:4047
int ast_attended_transfer_message_add_app(struct ast_attended_transfer_message *transfer_msg, const char *app, struct ast_channel *replace_channel)
Add details for an attended transfer to an application.
Message representing attended transfer.
struct ast_bridge * bridge_register(struct ast_bridge *bridge)
Register the new bridge with the system.
Definition: bridge.c:691
Call Parking API.
void ast_bridge_channel_leave_bridge_nolock(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
Set bridge channel state to leave bridge (if not leaving already).
unsigned int remb_send_interval
Definition: bridge.h:151
int(* ast_bridge_move_indicate_callback)(struct ast_bridge_channel *bridge_channel, void *hook_pvt, struct ast_bridge *src, struct ast_bridge *dst)
Move indicator callback.
Structure that contains features information.
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2958
int ast_bridge_features_limits_construct(struct ast_bridge_features_limits *limits)
Constructor function for ast_bridge_features_limits.
Definition: bridge.c:3396
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101
void ast_bridge_channel_kick(struct ast_bridge_channel *bridge_channel, int cause)
Kick the channel out of the bridge.
Message published during a blind transfer.
int ast_indicate(struct ast_channel *chan, int condition)
Indicates condition of channel.
Definition: channel.c:4277
void(* suspend)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
Suspend a channel on a bridging technology instance for a bridge.
#define OBJ_KEY
Definition: astobj2.h:1151
struct ast_bridge_snapshot * ast_bridge_get_snapshot_by_uniqueid(const char *uniqueid)
Returns the current snapshot for the bridge.
struct ast_unreal_pvt_callbacks * callbacks
Definition: core_unreal.h:92
ast_bridge_hook_remove_flags
struct ast_bridge_channel * ast_bridge_channel_peer(struct ast_bridge_channel *bridge_channel)
Get the peer bridge channel of a two party bridge.
unsigned int internal_mixing_interval
The mixing interval indicates how quickly softmix mixing should occur to mix audio.
Definition: bridge.h:293
const char * name
Definition: bridge.h:259
char context[AST_MAX_CONTEXT]
#define OBJ_POINTER
Definition: astobj2.h:1150
int ast_softhangup(struct ast_channel *chan, int cause)
Softly hangup up a channel.
Definition: channel.c:2471
void astman_send_list_complete_start(struct mansession *s, const struct message *m, const char *event_name, int count)
Start the list complete event.
Definition: manager.c:3467
descriptor for a cli entry.
Definition: cli.h:171
struct ast_bridge_features * ast_bridge_features_new(void)
Allocate a new bridge features struct.
Definition: bridge.c:3683
void ast_bridge_publish_attended_transfer(struct ast_attended_transfer_message *transfer_msg)
Publish an attended transfer.
ast_callid callid
Definition: bridge.h:361
struct ao2_container * ast_bridges(void)
Returns the global bridges container.
Definition: bridge.c:174
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
static ast_bridge_hook_callback builtin_features_handlers[AST_BRIDGE_BUILTIN_END]
Definition: bridge.c:147
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container, as described below.
Definition: astobj2.h:1693
Structure that contains a snapshot of information about a bridge.
Definition: bridge.h:314
unsigned int dissolved
Definition: bridge.h:390
void ast_local_lock_all(struct ast_channel *chan, void **tech_pvt, struct ast_channel **base_chan, struct ast_channel **base_owner)
Add a reference to the local channel's private tech, lock the local channel's private base...
Definition: core_local.c:241
int ast_callid_threadassoc_change(ast_callid callid)
Sets what is stored in the thread storage to the given callid if it does not match what is already th...
Definition: logger.c:2307
int(* start)(struct ast_bridge *bridge)
Request a bridge technology instance start operations.
unsigned int reconfigured
Definition: bridge.h:388
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
struct ast_bridge_video_mode video_mode
Definition: bridge.h:279
void ast_bridge_publish_blind_transfer(struct ast_blind_transfer_message *transfer_message)
Publish a blind transfer event.
const ast_string_field name
Definition: bridge.h:401
struct ast_channel * chan_old_vsrc
Definition: bridge.h:127
#define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn)
Allocate and initialize a list container.
Definition: astobj2.h:1327
const ast_string_field video_source_id
Definition: bridge.h:328
struct ast_channel * owner
Definition: core_unreal.h:93
int ast_call(struct ast_channel *chan, const char *addr, int timeout)
Make a call.
Definition: channel.c:6461
Structure representing a snapshot of channel state.
struct ast_channel * chan_vsrc
Definition: bridge.h:116
enum bridge_channel_state state
unsigned int suspended
struct ast_bridge_hook generic
This is used for both SINGLE_SRC_TALKER mode to set what channel should be the current single video f...
Definition: bridge.h:121
Test Framework API.
Definition: heap.c:36
ast_bridge_builtin_interval
static int interval_wrapper_cb(struct ast_bridge_channel *bridge_channel, void *obj)
Wrapper for interval hooks that calls into the wrapped hook.
Definition: bridge.c:3569
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
Structure for a data store type.
Definition: datastore.h:31
ast_channel_state
ast_channel states
Definition: channelstate.h:35
struct ast_heap * ast_heap_destroy(struct ast_heap *h)
Destroy a max heap.
Definition: heap.c:146
int ast_bridge_features_unregister(enum ast_bridge_builtin_feature feature)
Unregister a handler for a built in feature.
Definition: bridge.c:3078
void(* stream_topology_changed)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
Callback for when a stream topology changes on the channel.
ast_bridge_hook_pvt_destructor destructor
struct ao2_container * dtmf_hooks
int ast_callid_threadassoc_add(ast_callid callid)
Adds a known callid to thread storage of the calling thread.
Definition: logger.c:2320
ast_bridge_dissolving_fn dissolving
Definition: bridge.h:263
int(* ast_bridge_hook_callback)(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
Hook callback type.
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
int ast_parking_blind_transfer_park(struct ast_bridge_channel *parker, const char *context, const char *exten, transfer_channel_cb parked_channel_cb, struct transfer_channel_data *parked_channel_data)
Perform a blind transfer to a parking extension.
Definition: parking.c:143
const char * ast_cause2str(int cause) attribute_pure
Gives the string form of a given cause code.
Definition: channel.c:612
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
void ast_bridge_remove_video_src(struct ast_bridge *bridge, struct ast_channel *chan)
remove a channel as a source of video for the bridge.
Definition: bridge.c:3917
struct ast_channel * ast_channel_yank(struct ast_channel *yankee)
Gain control of a channel in the system.
Definition: channel.c:10593
int ast_bridge_features_register(enum ast_bridge_builtin_feature feature, ast_bridge_hook_callback callback, const char *dtmf)
Register a handler for a built in feature.
Definition: bridge.c:3062
int ast_bridge_number_video_src(struct ast_bridge *bridge)
Returns the number of video sources currently active in the bridge.
Definition: bridge.c:3864
unsigned int inhibit_merge
Count of the active temporary requests to inhibit bridge merges. Zero if merges are allowed...
Definition: bridge.h:384
struct ast_bridge * src
Definition: bridge.c:2138
ast_callid ast_read_threadstorage_callid(void)
extracts the callerid from the thread
Definition: logger.c:2298
#define AST_LIST_EMPTY(head)
Checks whether the specified list contains any entries.
Definition: linkedlists.h:450
void astman_send_ack(struct mansession *s, const struct message *m, char *msg)
Send ack in manager transaction.
Definition: manager.c:3421
int ast_bridge_move(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, struct ast_channel *chan, struct ast_channel *swap, int attempt_recovery)
Move a channel from one bridge to another.
Definition: bridge.c:2460
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
Structure for a data store object.
Definition: datastore.h:64
void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
Destroy the connected line information contents.
Definition: channel.c:2072
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2399
static struct bridge_manager_controller * bridge_manager
Definition: bridge.c:172
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
void ast_brige_set_remb_behavior(struct ast_bridge *bridge, enum ast_bridge_video_sfu_remb_behavior behavior)
Set the REMB report generation behavior on a bridge.
Definition: bridge.c:3798
static char builtin_features_dtmf[AST_BRIDGE_BUILTIN_END][MAXIMUM_DTMF_FEATURE_STRING]
Definition: bridge.c:144
struct ast_bridge * dest
Definition: bridge.c:2136
#define ast_manager_register_xml_core(action, authority, func)
Register a manager callback using XML documentation to describe the manager.
Definition: manager.h:202
void * ast_heap_pop(struct ast_heap *h)
Pop the max element off of the heap.
Definition: heap.c:262
ast_transfer_result
Definition: bridge.h:1098
int ast_bridge_depart(struct ast_channel *chan)
Depart a channel from a bridge.
Definition: bridge.c:1906
void(*const optimization_finished)(struct ast_unreal_pvt *p, int success, unsigned int id)
Called when an optimization attempt completed successfully.
Definition: core_unreal.h:81
int(* ast_bridge_builtin_set_limits_fn)(struct ast_bridge_features *features, struct ast_bridge_features_limits *limits, enum ast_bridge_hook_remove_flags remove_flags)
Attach interval hooks to a bridge features structure.
void ast_moh_stop(struct ast_channel *chan)
Turn off music on hold on a given channel.
Definition: channel.c:7776
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:615
struct ast_bridge_softmix softmix
Definition: bridge.h:367
int ast_attended_transfer_message_add_merge(struct ast_attended_transfer_message *transfer_msg, struct ast_bridge *final_bridge)
Add details for a bridge merge to an attended transfer message.
static ast_bridge_builtin_set_limits_fn builtin_interval_handlers[AST_BRIDGE_BUILTIN_INTERVAL_END]
Definition: bridge.c:150
int ast_bridge_queue_action(struct ast_bridge *bridge, struct ast_frame *action)
Put an action onto the specified bridge.
Definition: bridge.c:303
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:757
const ast_string_field creator
Definition: bridge.h:328
void ast_bridge_discard_after_callback(struct ast_channel *chan, enum ast_bridge_after_cb_reason reason)
Run discarding any after bridge callbacks.
Definition: bridge_after.c:239
enum ast_bridge_video_sfu_remb_behavior remb_behavior
Definition: bridge.h:153
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
struct ast_bridge * bridge
Bridge this channel is participating in.
struct ast_frame_subclass subclass
#define OBJ_PARTIAL_KEY
Definition: astobj2.h:1152
Utility functions.
struct ast_channel * ast_channel_get_by_name_prefix(const char *name, size_t name_len)
Find a channel by a name prefix.
Definition: channel.c:1434
const char * astman_get_header(const struct message *m, char *var)
Get header from manager transaction.
Definition: manager.c:3050
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
struct ast_channel * ast_request(const char *type, struct ast_format_cap *request_cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *addr, int *cause)
Requests a channel.
Definition: channel.c:6354
char * ast_cli_complete(const char *word, const char *const choices[], int pos)
Definition: main/cli.c:1846
int ast_bridge_dtmf_hook(struct ast_bridge_features *features, const char *dtmf, ast_bridge_hook_callback callback, void *hook_pvt, ast_bridge_hook_pvt_destructor destructor, enum ast_bridge_hook_remove_flags remove_flags)
Attach a DTMF hook to a bridge features structure.
Definition: bridge.c:3182
static struct ast_bridge_technology * find_best_technology(uint32_t capabilities, struct ast_bridge *bridge)
Helper function used to find the "best" bridge technology given specified capabilities.
Definition: bridge.c:499
int ast_bridge_destroy(struct ast_bridge *bridge, int cause)
Destroy a bridge.
Definition: bridge.c:944
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized...
Definition: linkedlists.h:333
void ast_bridge_features_merge(struct ast_bridge_features *into, const struct ast_bridge_features *from)
Merge one ast_bridge_features into another.
Definition: bridge.c:3595
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
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
int __ast_bridge_technology_register(struct ast_bridge_technology *technology, struct ast_module *module)
Register a bridge technology for use.
Definition: bridge.c:212
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
void * ao2_object_get_lockaddr(void *obj)
Return the mutex lock address of an object.
Definition: astobj2.c:476
struct ast_bridge_technology * technology
Definition: bridge.h:355
static int merge_container_cb(void *obj, void *data, int flags)
Callback for merging hook ao2_containers.
Definition: bridge.c:3562
struct ast_flags feature_flags
void ao2_container_unregister(const char *name)
Unregister a container for CLI stats and integrity check.
unsigned int text_messaging
const char * ast_bridge_video_mode_to_string(enum ast_bridge_video_mode_type video_mode)
Converts an enum representation of a bridge video mode to string.
Definition: bridge.c:3951
struct ast_attended_transfer_message * ast_attended_transfer_message_create(int is_external, struct ast_channel *to_transferee, struct ast_bridge *transferee_bridge, struct ast_channel *to_transfer_target, struct ast_bridge *target_bridge, struct ast_channel *transferee, struct ast_channel *transfer_target)
Create an Attended transfer message to be published.
int ao2_container_register(const char *name, struct ao2_container *self, ao2_prnt_obj_fn *prnt_obj)
Register a container for CLI stats and integrity check.
Max Heap data structure.
enum ast_transfer_result result
void(* leave)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
Remove a channel from a bridging technology instance for a bridge.
struct ast_bridge * ast_channel_get_bridge(const struct ast_channel *chan)
Get the bridge associated with a channel.
Definition: channel.c:10534
void ast_bridge_merge_inhibit(struct ast_bridge *bridge, int request)
Adjust the bridge merge inhibit request count.
Definition: bridge.c:3000
#define ast_heap_push(h, elm)
Push an element on to a heap.
Definition: heap.h:125
int ast_bridge_hangup_hook(struct ast_bridge_features *features, ast_bridge_hook_callback callback, void *hook_pvt, ast_bridge_hook_pvt_destructor destructor, enum ast_bridge_hook_remove_flags remove_flags)
Attach a hangup hook to a bridge features structure.
Definition: bridge.c:3265
int(* compatible)(struct ast_bridge *bridge)
Check if a bridge is compatible with the bridging technology.
const ast_string_field technology
Definition: bridge.h:328
#define ast_bridge_channel_lock(bridge_channel)
Lock the bridge_channel.
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition: vector.h:113
struct ast_bridge * ast_bridge_find_by_id(const char *bridge_id)
Find bridge by id.
Definition: bridge.c:5012
ast_mutex_t lock
struct ao2_container * ast_bridge_peers(struct ast_bridge *bridge)
Get a container of all channels in the bridge.
Definition: bridge.c:4036
enum ast_bridge_hook_type type
ast_bridge_notify_masquerade_fn notify_masquerade
Definition: bridge.h:269
General Asterisk PBX channel definitions.
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
ast_bridge_hook_type
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
void(*const optimization_started)(struct ast_unreal_pvt *p, struct ast_channel *source, enum ast_unreal_channel_indicator dest, unsigned int id)
Called when an optimization attempt has started.
Definition: core_unreal.h:69
void ast_bridge_publish_merge(struct ast_bridge *to, struct ast_bridge *from)
Publish a bridge merge.
void astman_send_list_complete_end(struct mansession *s)
End the list complete event.
Definition: manager.c:3475
void ast_channel_internal_bridge_set(struct ast_channel *chan, struct ast_bridge *value)
int(* write)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
Write a frame into the bridging technology instance for a bridge.
bridge_allow_swap
Definition: bridge.c:2672
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:359
void ast_channel_stage_snapshot_done(struct ast_channel *chan)
Clear flag to indicate channel snapshot is being staged, and publish snapshot.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
int ast_bridge_join_hook(struct ast_bridge_features *features, ast_bridge_hook_callback callback, void *hook_pvt, ast_bridge_hook_pvt_destructor destructor, enum ast_bridge_hook_remove_flags remove_flags)
Attach a bridge channel join hook to a bridge features structure.
Definition: bridge.c:3275
enum ast_bridge_preference preference
Channel Bridging API.
void ast_bridge_features_set_flag(struct ast_bridge_features *features, unsigned int flag)
Set a flag on a bridge channel features structure.
Definition: bridge.c:3427
#define AST_MAX_EXTENSION
Definition: channel.h:134
int ast_bridge_technology_unregister(struct ast_bridge_technology *technology)
Unregister a bridge technology from use.
Definition: bridge.c:263
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:557
#define ast_channel_cleanup(c)
Cleanup a channel reference.
Definition: channel.h:2969
int ast_bridge_merge(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, int merge_best_direction, struct ast_channel **kick_me, unsigned int num_kick)
Merge two bridges together.
Definition: bridge.c:2300
bridge_allow_merge
Definition: bridge.c:2811
In case you didn't read that giant block of text above the mansession_session struct, the mansession is named this solely to keep the API the same in Asterisk. This structure really represents data that is different from Manager action to Manager action. The mansession_session pointer contained within points to session-specific data.
Definition: manager.c:1785
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
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
Definition: time.h:282
Private Bridging Channel API.
struct ast_bridge_hook_dtmf_parms dtmf
int ast_bridge_is_video_src(struct ast_bridge *bridge, struct ast_channel *chan)
Determine if a channel is a video src for the bridge.
Definition: bridge.c:3891
int ast_bridge_unsuspend(struct ast_bridge *bridge, struct ast_channel *chan)
Unsuspend a channel from a bridge.
Definition: bridge.c:3028
int ast_bridge_features_do(enum ast_bridge_builtin_feature feature, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
Invoke a built in feature hook now.
Definition: bridge.c:3090
#define SCOPED_LOCK(varname, lock, lockfunc, unlockfunc)
Scoped Locks.
Definition: lock.h:583
A set of macros to manage forward-linked lists.
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
#define MAXIMUM_DTMF_FEATURE_STRING
Maximum length of a DTMF feature string.
struct ast_bridge_snapshot * bridge
#define ast_debug(level,...)
Log a DEBUG message.
void ast_bridge_discard_after_goto(struct ast_channel *chan)
Discard channel after bridge goto location.
Definition: bridge_after.c:384
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
void(* ast_bridge_hook_pvt_destructor)(void *hook_pvt)
Hook pvt destructor callback.
void ast_channel_req_accountcodes(struct ast_channel *chan, const struct ast_channel *requestor, enum ast_channel_requestor_relationship relationship)
Setup new channel accountcodes from the requestor channel after ast_request().
Definition: channel.c:6434
unsigned int just_joined
Core PBX routines and definitions.
int ast_bridge_interval_unregister(enum ast_bridge_builtin_interval interval)
Unregisters a handler for a built in interval feature.
Definition: bridge.c:3119
struct ast_heap * interval_hooks
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
static enum ast_transfer_result attended_transfer_bridge(struct ast_channel *chan1, struct ast_channel *chan2, struct ast_bridge *bridge1, struct ast_bridge *bridge2, struct ast_attended_transfer_message *transfer_msg)
Perform an attended transfer of a bridge.
Definition: bridge.c:4221
#define ast_bridge_channel_unlock(bridge_channel)
Unlock the bridge_channel.
const struct ast_bridge_methods * v_table
Definition: bridge.h:351
unsigned int dtmf_passthrough
int ast_manager_unregister(const char *action)
Unregister a registered manager command.
Definition: manager.c:8057
#define AST_LIST_HEAD_NOLOCK(name, type)
Defines a structure to be used to hold a list of specified type (with no lock).
Definition: linkedlists.h:225
struct ast_bridge_snapshot * ast_bridge_get_snapshot(struct ast_bridge *bridge)
Returns the current snapshot for the bridge.
void ast_channel_stage_snapshot(struct ast_channel *chan)
Set flag to indicate channel snapshot is being staged.
static void * bridge_channel_ind_thread(void *data)
Thread responsible for independent imparted bridged channels.
Definition: bridge.c:1742
struct ast_bridge_snapshot * current_snapshot
Definition: bridge.h:406
Structure that contains information about a bridge.
Definition: bridge.h:349
void(* destroy)(struct ast_bridge *bridge)
Destroy a bridging technology instance for a bridge.
int ast_tvcmp(struct timeval _a, struct timeval _b)
Compress two struct timeval instances returning -1, 0, 1 if the first arg is smaller, equal or greater to the second.
Definition: time.h:137
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
Definition: astobj2.h:1303
struct ast_bridge * bridge
Definition: bridge.c:157
Support for dynamic strings.
Definition: strings.h:623
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
const ast_string_field name
Definition: bridge.h:328
void ast_bridge_set_maximum_sample_rate(struct ast_bridge *bridge, unsigned int sample_rate)
Adjust the maximum mixing sample rate of a bridge used during multimix mode.
Definition: bridge.c:3719
int ast_bridge_impart(struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap, struct ast_bridge_features *features, enum ast_bridge_impart_flags flags)
Impart a channel to a bridge (non-blocking)
Definition: bridge.c:1878
unsigned int num_lonely
Definition: bridge.h:377
Structure that contains configuration information for the limits feature.
#define ao2_unlink(container, obj)
Remove an object from a container.
Definition: astobj2.h:1578
int ast_bridge_join(struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap, struct ast_bridge_features *features, struct ast_bridge_tech_optimizations *tech_args, enum ast_bridge_join_flags flags)
Join a channel to a bridge (blocking)
Definition: bridge.c:1621
int ast_bridge_channel_write_playfile(struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
Write a bridge action play file frame into the bridge.
enum ast_bridge_video_mode_type video_mode
Definition: bridge.h:341
struct ast_bridge_tech_optimizations tech_args
ast_bridge_hook_callback callback
struct ast_module * mod
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: extconf.c:2282
Connected Line/Party information.
Definition: channel.h:456
The base pvt structure for local channel derivatives.
Definition: core_unreal.h:91
const ast_string_field name
void ast_bridge_features_limits_destroy(struct ast_bridge_features_limits *limits)
Destructor function for ast_bridge_features_limits.
Definition: bridge.c:3407
#define AST_LIST_LAST(head)
Returns the last entry contained in a list.
Definition: linkedlists.h:429
void ast_bridge_channel_leave_bridge(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
Set bridge channel state to leave bridge (if not leaving already).
struct ast_channel * ast_bridge_peer(struct ast_bridge *bridge, struct ast_channel *chan)
Get the channel's bridge peer only if the bridge is two-party.
Definition: bridge.c:4075
int ast_bridge_channel_queue_callback(struct ast_bridge_channel *bridge_channel, enum ast_bridge_channel_custom_callback_option flags, ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
Queue a bridge action custom callback frame onto the bridge channel.
int ast_bridge_leave_hook(struct ast_bridge_features *features, ast_bridge_hook_callback callback, void *hook_pvt, ast_bridge_hook_pvt_destructor destructor, enum ast_bridge_hook_remove_flags remove_flags)
Attach a bridge channel leave hook to a bridge features structure.
Definition: bridge.c:3285
int(* join)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
Add a channel to a bridging technology instance for a bridge.
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
static struct ao2_container * bridges
Definition: bridge.c:123
void ast_bridge_set_internal_sample_rate(struct ast_bridge *bridge, unsigned int sample_rate)
Adjust the internal mixing sample rate of a bridge used during multimix mode.
Definition: bridge.c:3712
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:410
unsigned int in_bridge
void ast_bridge_set_transfer_variables(struct ast_channel *chan, const char *value, int attended)
Set the relevant transfer variables for a single channel.
Definition: bridge.c:4352
int ast_parking_provider_registered(void)
Check whether a parking provider is registered.
Definition: parking.c:241
int ast_bridge_features_init(struct ast_bridge_features *features)
Initialize bridge features structure.
Definition: bridge.c:3620
ast_bridge_builtin_feature
Built in DTMF features.
Bridge virtual methods table definition.
Definition: bridge.h:257
#define ast_bridge_unlock(bridge)
Unlock the bridge.
Definition: bridge.h:481
#define AST_MAX_CONTEXT
Definition: channel.h:135
union ast_frame::@224 data
void ast_bridge_technology_suspend(struct ast_bridge_technology *technology)
Suspend a bridge technology from consideration.
Definition: bridge.c:3047
char * command
Definition: cli.h:186
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
void ast_bridge_features_destroy(struct ast_bridge_features *features)
Destroy an allocated bridge features struct.
Definition: bridge.c:3674
void ast_bridge_run_after_callback(struct ast_channel *chan)
Run any after bridge callback.
Definition: bridge_after.c:212
unsigned int inhibit_colp
int ast_bridge_features_set_limits(struct ast_bridge_features *features, struct ast_bridge_features_limits *limits, enum ast_bridge_hook_remove_flags remove_flags)
Limit the amount of time a channel may stay in the bridge and optionally play warning messages as tim...
Definition: bridge.c:3412
struct ast_bridge_snapshot * ast_bridge_snapshot_create(struct ast_bridge *bridge)
Generate a snapshot of the bridge state. This is an ao2 object, so ao2_cleanup() to deallocate...
enum ast_bridge_optimization ast_bridges_allow_optimization(struct ast_bridge *chan_bridge, struct ast_bridge *peer_bridge)
Determine if bridges allow for optimization to occur betweem them.
Definition: bridge.c:2960
void ast_bridge_set_remb_send_interval(struct ast_bridge *bridge, unsigned int remb_send_interval)
Set the interval at which a combined REMB frame will be sent to video sources.
Definition: bridge.c:3789
struct ast_bridge_hook_timer_parms timer
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2541
ast_bridge_merge_priority_fn get_merge_priority
Definition: bridge.h:271
void ast_bridging_init_basic(void)
char * ast_uuid_generate_str(char *buf, size_t size)
Generate a UUID string.
Definition: uuid.c:141
#define ast_bridge_channel_trylock(bridge_channel)
Try locking the bridge_channel.
unsigned int video_update_discard
Definition: bridge.h:168
static void wrap_hook(struct ast_bridge_features *features, struct ast_bridge_hook_timer *hook)
Wrap the provided interval hook and add it to features.
Definition: bridge.c:3583
Support for logging to various files, console and syslog Configuration in file logger.conf.
Prototypes for public functions only of internal interest,.
const ast_string_field uniqueid
Definition: bridge.h:328
struct ast_channel * swap
Basic bridge subclass API.
int ast_bridge_kick(struct ast_bridge *bridge, struct ast_channel *chan)
Kick a channel from a bridge.
Definition: bridge.c:1979
unsigned int num_channels
Definition: bridge.h:337
void ast_bridge_features_cleanup(struct ast_bridge_features *features)
Clean up the contents of a bridge features structure.
Definition: bridge.c:3653
static void interval_wrapper_pvt_dtor(void *obj)
Destructor for the hook wrapper.
Definition: bridge.c:3577
struct ao2_container * ast_bridge_peers_nolock(struct ast_bridge *bridge)
Get a container of all channels in the bridge.
Definition: bridge.c:4018
struct ast_bridge_hook generic
int cause
Definition: bridge.h:386
#define ast_heap_create(init_height, cmp_fn, index_offset)
Create a max heap.
Definition: heap.h:100
struct ast_bridge_channel * ast_channel_get_bridge_channel(struct ast_channel *chan)
Get a reference to the channel's bridge pointer.
Definition: channel.c:10582
enum ast_transfer_result ast_bridge_transfer_attended(struct ast_channel *to_transferee, struct ast_channel *to_transfer_target)
Attended transfer.
Definition: bridge.c:4677
ast_bridge_join_flags
Definition: bridge.h:533
const char * usage
Definition: cli.h:177
struct stasis_topic * ast_bridge_topic(struct ast_bridge *bridge)
A topic which publishes the events for a particular bridge.
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:483
int ast_bridge_suspend(struct ast_bridge *bridge, struct ast_channel *chan)
Suspend a channel temporarily from a bridge.
Definition: bridge.c:3007
unsigned int send_sdp_label
Definition: bridge.h:300
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
void ast_bridge_set_binaural_active(struct ast_bridge *bridge, unsigned int binaural_active)
Activates the use of binaural signals in a conference bridge.
Definition: bridge.c:3705
#define ast_module_running_ref(mod)
Hold a reference to the module if it is running.
Definition: module.h:469
#define ast_bridge_lock(bridge)
Lock the bridge.
Definition: bridge.h:470
#define ast_channel_lock_both(chan1, chan2)
Lock two channels.
Definition: channel.h:2929
unsigned int num_active
Definition: bridge.h:339
#define AST_YESNO(x)
return Yes or No depending on the argument.
Definition: strings.h:143
struct ast_bridge_channels_list channels
Definition: bridge.h:363
void * data
Definition: datastore.h:66
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
Definition: linkedlists.h:681
struct ast_channel * chan
void * ast_heap_remove(struct ast_heap *h, void *elm)
Remove a specific element from a heap.
Definition: heap.c:251
Replace objects with duplicate keys in container.
Definition: astobj2.h:1211
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:2947
ast_bridge_push_channel_fn push_peek
Definition: bridge.h:273
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...
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1821
Standard Command Line Interface.
void ast_bridge_channel_lock_bridge(struct ast_bridge_channel *bridge_channel)
Lock the bridge associated with the bridge channel.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
void ast_connected_line_copy_from_caller(struct ast_party_connected_line *dest, const struct ast_party_caller *src)
Copy the caller information to the connected line information.
Definition: channel.c:8293
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:80
struct ao2_container * other_hooks
size_t ast_heap_size(struct ast_heap *h)
Get the current size of a heap.
Definition: heap.c:276
void ast_local_unlock_all(void *tech_pvt, struct ast_channel *base_chan, struct ast_channel *base_owner)
Remove a reference to the given local channel's private tech, unlock the given local channel's privat...
Definition: core_local.c:256
void ast_bridge_technology_unsuspend(struct ast_bridge_technology *technology)
Unsuspend a bridge technology.
Definition: bridge.c:3052
ast_bridge_video_sfu_remb_behavior
REMB report behaviors.
Definition: bridge.h:131
Structure that is the essence of a bridge technology.
int ast_bridge_channel_queue_playfile(struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
Queue a bridge action play file frame onto the bridge channel.
void ast_bridge_features_remove(struct ast_bridge_features *features, enum ast_bridge_hook_remove_flags remove_flags)
Remove marked bridge channel feature hooks.
Definition: bridge.c:3501
struct ast_channel * chan_vsrc
Definition: bridge.h:123
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2805
void ast_bridge_notify_masquerade(struct ast_channel *chan)
Notify bridging that this channel was just masqueraded.
Definition: bridge.c:1442
struct ast_vector_int media_types
Definition: bridge.h:404
AO2 object that wraps data for transfer_channel_cb.
Definition: bridge.h:1119
int ast_bridge_interval_register(enum ast_bridge_builtin_interval interval, ast_bridge_builtin_set_limits_fn callback)
Register a handler for a built in interval feature.
Definition: bridge.c:3107
void * ast_heap_peek(struct ast_heap *h, unsigned int index)
Peek at an element on a heap.
Definition: heap.c:267
void ast_bridge_channel_stream_map(struct ast_bridge_channel *bridge_channel)
Maps a channel's stream topology to and from the bridge.
Data structure associated with a single frame of data.
Internal Asterisk hangup causes.
struct ast_flags remove_flags
Options provided by main asterisk program.
void * tech_pvt
Definition: bridge.h:357
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
int ast_attended_transfer_message_add_link(struct ast_attended_transfer_message *transfer_msg, struct ast_channel *locals[2])
Add details for an attended transfer that has a link between bridges.
Definition: search.h:40
void(* transfer_channel_cb)(struct ast_channel *chan, struct transfer_channel_data *user_data, enum ast_transfer_type transfer_type)
Callback function type called during blind transfers.
Definition: bridge.h:1143
ast_bridge_pull_channel_fn pull
Definition: bridge.h:267
#define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn)
Allocate and initialize a red-black tree container.
Definition: astobj2.h:1349
int ast_bridge_channel_queue_control_data(struct ast_bridge_channel *bridge_channel, enum ast_control_frame_type control, const void *data, size_t datalen)
Queue a control frame onto the bridge channel with data.
static int play_file(struct ast_bridge_channel *bridge_channel, struct ast_channel *channel, const char *filename)
Playback the given filename and monitor for any dtmf interrupts.
ast_bridge_impart_flags
Definition: bridge.h:584
ast_bridge_destructor_fn destroy
Definition: bridge.h:261
ast_bridge_push_channel_fn push
Definition: bridge.h:265
unsigned int interval_sequence
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:529
enum ast_frame_type frametype
Private Bridging API.
Generic container type.
#define AST_CLI_YESNO(x)
Return Yes or No depending on the argument.
Definition: cli.h:71
unsigned int depart_wait
void ast_format_duration_hh_mm_ss(int duration, char *buf, size_t length)
Formats a duration into HH:MM:SS.
Definition: utils.c:2297
Call Parking and Pickup API Includes code and algorithms from the Zapata library. ...
int(* create)(struct ast_bridge *bridge)
Create a bridge technology instance for a bridge.
struct ast_channel_snapshot * transferee
int ast_bridge_remove(struct ast_bridge *bridge, struct ast_channel *chan)
Remove a channel from a bridge.
Definition: bridge.c:1951
const ast_string_field creator
Definition: bridge.h:401
int ast_softhangup_nolock(struct ast_channel *chan, int cause)
Softly hangup up a channel (no channel lock)
Definition: channel.c:2458
int ast_bridge_unreal_optimize_out(struct ast_channel *chan, struct ast_channel *peer, struct ast_unreal_pvt *pvt)
Check and optimize out the unreal channels between bridges.
Definition: bridge.c:2920
int ast_cli_completion_add(char *value)
Add a result to a request for completion options.
Definition: main/cli.c:2761
int ast_bridge_channel_write_unhold(struct ast_bridge_channel *bridge_channel)
Write an unhold frame into the bridge.
Say numbers and dates (maybe words one day too)
void ast_bridge_set_mixing_interval(struct ast_bridge *bridge, unsigned int mixing_interval)
Adjust the internal mixing interval of a bridge used during multimix mode.
Definition: bridge.c:3698
Bridging API.
void(* stop)(struct ast_bridge *bridge)
Request a bridge technology instance stop in preparation for being destroyed.
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Definition: manager.c:3389
Asterisk module definitions.
const ast_string_field subclass
Definition: bridge.h:328
unsigned int internal_sample_rate
The internal sample rate softmix uses to mix channels.
Definition: bridge.h:285
ast_bridge_optimization
Tells, if optimization is allowed, how the optimization would be performed.
Definition: bridge.h:874
#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
enum ast_transfer_result ast_bridge_transfer_blind(int is_external, struct ast_channel *transferer, const char *exten, const char *context, transfer_channel_cb new_channel_cb, void *user_data)
Blind transfer target to the extension and context provided.
Definition: bridge.c:4425
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2385
Structure specific to bridge technologies capable of performing talking optimizations.
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:374
int ast_local_setup_masquerade(struct ast_channel *ast, struct ast_channel *masq)
Setup the outgoing local channel to masquerade into a channel on ast_call().
Definition: core_local.c:643
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
int ast_channel_has_audio_frame_or_monitor(struct ast_channel *chan)
Check if the channel has active audiohooks, active framehooks, or a monitor.
Definition: channel.c:2518
enum ast_transfer_result result
char code[MAXIMUM_DTMF_FEATURE_STRING]
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
uint32_t allowed_capabilities
Definition: bridge.h:371
unsigned int maximum_sample_rate
The maximum sample rate softmix uses to mix channels.
Definition: bridge.h:306
Structure that is the essence of a feature hook.
unsigned int num_channels
Definition: bridge.h:373
int ast_bridge_setup_after_goto(struct ast_channel *chan)
Setup any after bridge goto location to begin execution.
Definition: bridge_after.c:435
int ast_bridge_add_channel(struct ast_bridge *bridge, struct ast_channel *chan, struct ast_bridge_features *features, int play_tone, const char *xfersound)
Add an arbitrary channel to a bridge.
Definition: bridge.c:2471
char exten[AST_MAX_EXTENSION]
Timing source management.
ast_bridge_hook_timer_option
static const char * tech_capability2str(uint32_t capabilities)
Definition: bridge.c:5277
int ast_format_cap_has_type(const struct ast_format_cap *cap, enum ast_media_type type)
Find out if the capabilities structure has any formats of a specific type.
Definition: format_cap.c:613
Structure for mutex and tracking information.
Definition: lock.h:135
Channel Bridging API.
static int bridge_show_specific_print_channel(void *obj, void *arg, int flags)
Internal callback function for sending channels in a bridge to the CLI.
Definition: bridge.c:5082
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
void ast_bridge_vars_set(struct ast_channel *chan, const char *name, const char *pvtid)
Sets BRIDGECHANNEL and BRIDGEPVTCALLID for a channel.
Definition: bridge.c:1212
struct bridge_manager_controller::@312 service_requests
const ast_string_field name
static void * bridge_channel_depart_thread(void *data)
Thread responsible for imparted bridged channels to be departed.
Definition: bridge.c:1711
struct ast_channel_snapshot * replace_channel
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
#define ast_bridge_trylock(bridge)
Try locking the bridge.
Definition: bridge.h:459
unsigned int construction_completed
Definition: bridge.h:392
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:1259
void ast_bridge_update_talker_src_video_mode(struct ast_bridge *bridge, struct ast_channel *chan, int talker_energy, int is_keyframe)
Update information about talker energy for talker src video mode.
Definition: bridge.c:3816
int ast_local_setup_bridge(struct ast_channel *ast, struct ast_bridge *bridge, struct ast_channel *swap, struct ast_bridge_features *features)
Setup the outgoing local channel to join a bridge on ast_call().
Definition: core_local.c:601
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:521
unsigned int binaural_active
Definition: bridge.h:295
void astman_send_listack(struct mansession *s, const struct message *m, char *msg, char *listflag)
Send ack in manager transaction to begin a list.
Definition: manager.c:3431
void ast_bridge_publish_state(struct ast_bridge *bridge)
Publish the state of a bridge.
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
Unreal channel derivative framework.