Asterisk - The Open Source Telephony Project  GIT-master-a24979a
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"
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. */
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 {
189 
191  if (bridge_manager->stop) {
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. */
201  return;
202  }
203  ao2_ref(bridge, +1);
204  request->bridge = bridge;
205 
206  /* Put request into the queue and wake the bridge manager. */
210 }
211 
213 {
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 */
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. */
242  /* Put the highest preference tech's first in the list. */
243  if (technology->preference >= current->preference) {
245 
246  break;
247  }
248  }
250 
251  if (!current) {
252  /* Insert our new bridge technology to the end of the list. */
254  }
255 
257 
258  ast_verb(2, "Registered bridge technology %s\n", technology->name);
259 
260  return 0;
261 }
262 
264 {
266 
268 
269  /* Ensure the bridge technology is registered before removing it */
271  if (current == technology) {
273  ast_verb(2, "Unregistered bridge technology %s\n", technology->name);
274  break;
275  }
276  }
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);
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) {
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
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  */
386 {
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)) ||
397  ast_check_hangup_locked(bridge_channel->chan) ||
398  ast_check_hangup_locked(peer->chan)) {
399  return;
400  }
401 
403 
404  ast_channel_lock(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);
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 
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),
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),
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  */
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 {
502  struct ast_bridge_technology *best = NULL;
503 
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 
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. */
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
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 
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 {
754  ast_string_field_set(self, uniqueid, uuid_hold);
755  }
757  if (!ast_strlen_zero(creator)) {
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  */
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 {
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++;
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--;
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);
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)) {
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) {
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  */
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. */
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  */
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  */
1387 {
1390  return;
1391  }
1392  if (bridge->num_channels < 2) {
1393  return;
1394  }
1395  if (bridge->num_channels == 2) {
1397  AST_LIST_LAST(&bridge->channels)->chan);
1398  } else {
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;
1411  /* Smart bridge failed. */
1412  bridge_dissolve(bridge, 0);
1413  return;
1414  }
1416 
1417  if (bridge->dissolved) {
1418  return;
1419  }
1423 
1424  if (colp_update) {
1426  }
1427 }
1428 
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 */
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  */
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 {
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  */
1530 }
1531 
1532 static const struct ast_datastore_info bridge_channel_impart_ds_info = {
1533  .type = "bridge-impart-ds",
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  */
1550 {
1551  struct ast_datastore *datastore;
1552  struct bridge_channel_impart_ds_head *ds_head;
1553 
1554  ast_channel_lock(chan);
1555 
1557  if (!datastore) {
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 
1583 {
1584  struct ast_datastore *datastore;
1585 
1586  ast_channel_lock(chan);
1588  if (datastore) {
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  */
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 
1655  res = -1;
1656  } else {
1658  }
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 
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);
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. */
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:
1702  /* Claim the after bridge goto is an async goto destination. */
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) {
1749  }
1750 
1752  chan = bridge_channel->chan;
1753 
1754  /* cleanup */
1755  ast_channel_lock(chan);
1757  ast_channel_unlock(chan);
1758  /* Lock here for ast_bridge_channel_get_chan */
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");
1767 
1768  ao2_ref(bridge_channel, -1);
1769 
1771  /* If join failed there will be impart threads waiting. */
1774  return NULL;
1775 }
1776 
1778  struct ast_channel *chan,
1779  struct ast_channel *swap,
1780  struct ast_bridge_features *features,
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",
1792  return -1;
1793  }
1794 
1795  /* Supply an empty features structure if the caller did not. */
1796  if (!features) {
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) {
1807  return -1;
1808  }
1809 
1812  ast_log(AST_LOG_NOTICE, "Channel %s is a zombie and cannot be imparted into bridge %s\n",
1814  res = -1;
1815  } else {
1817  }
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 
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);
1837  }
1838 
1839  /* Actually create the thread that will handle the channel */
1840  if (!res) {
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) {
1854  }
1855  }
1856 
1857  if (res) {
1858  /* cleanup */
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 
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 */
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 
1913  bridge_channel = ast_channel_internal_bridge_channel(chan);
1914  departable = bridge_channel && bridge_channel->depart_wait;
1916  if (!departable) {
1917  ast_log(LOG_ERROR, "Channel %s cannot be departed.\n",
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,
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 
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 
1952 {
1953  struct ast_bridge_channel *bridge_channel;
1954 
1955  ast_debug(1, "Removing channel %s from bridge %s\n",
1957 
1959 
1960  /* Try to find the channel that we want to remove */
1961  if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
1963  return -1;
1964  }
1965 
1966  ast_bridge_channel_leave_bridge(bridge_channel,
1968 
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 {
1977 }
1978 
1980 {
1981  struct ast_bridge_channel *bridge_channel;
1982  int res;
1983 
1985 
1986  /* Try to find the channel that we want to kick. */
1987  if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
1989  return -1;
1990  }
1991 
1992  res = ast_bridge_channel_queue_callback(bridge_channel, 0, kick_it, NULL, 0);
1993 
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,
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) {
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  }
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
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
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  }
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,
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,
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 
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 
2480  chan_bridge = ast_channel_get_bridge(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. */
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);
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);
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));
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 channl 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 
2561 {
2562  return !(bridge->inhibit_merge
2563  || bridge->dissolved
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  */
2580 {
2581  struct ast_bridge *bridge;
2582  struct ast_bridge_channel *bridge_channel;
2583 
2585  return NULL;
2586  }
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) ||
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  */
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  }
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  }
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
2717  /* Can swap optimize only one way. */
2718  return SWAP_TO_PEER_BRIDGE;
2719  } else if (peer_bridge->num_channels == 2
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);
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,
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
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,
2909  id);
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 
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 
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 
3001 {
3002  ast_bridge_lock(bridge);
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 
3015 
3016  if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
3018  return -1;
3019  }
3020 
3022 
3024 
3025  return 0;
3026 }
3027 
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 
3034 
3035  if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
3037  return -1;
3038  }
3039 
3041 
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 */
3172  if (hook) {
3173  hook->callback = callback;
3174  hook->destructor = destructor;
3175  hook->hook_pvt = hook_pvt;
3177  }
3178 
3179  return hook;
3180 }
3181 
3183  const char *dtmf,
3185  void *hook_pvt,
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  }
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,
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 
3267  void *hook_pvt,
3270 {
3273 }
3274 
3277  void *hook_pvt,
3280 {
3283 }
3284 
3287  void *hook_pvt,
3290 {
3293 }
3294 
3297  void *hook_pvt,
3300 {
3302 
3305 }
3306 
3309  void *hook_pvt,
3312 {
3314 
3317 }
3318 
3320  enum ast_bridge_hook_timer_option flags,
3321  unsigned int interval,
3323  void *hook_pvt,
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  }
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;
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  */
3466 {
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  */
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 {
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 */
3606  for (idx = 1; (hook = ast_heap_peek(from->interval_hooks, idx)); idx++) {
3607  wrap_hook(into, hook);
3608  }
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 */
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 */
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;
3734  }
3735  break;
3739  }
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) {
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 {
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 {
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 {
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 */
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),
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),
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;
3874  res = 1;
3875  }
3876  break;
3879  res++;
3880  }
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) {
3927  }
3929  }
3930  break;
3932  if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc == chan) {
3935  }
3938  }
3942  }
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 
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);
4042  ast_bridge_unlock(bridge);
4043 
4044  return channels;
4045 }
4046 
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
4056  int in_bridge = 0;
4057 
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 
4076 {
4077  struct ast_channel *peer;
4078 
4080  peer = ast_bridge_peer_nolock(bridge, chan);
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 
4122  snprintf(chan_name, sizeof(chan_name), "%s@%s", exten, context);
4123  local = ast_request("Local", ast_channel_nativeformats(transferer), NULL, transferer,
4124  chan_name, &cause);
4125  if (!local) {
4126  return AST_BRIDGE_TRANSFER_FAIL;
4127  }
4128 
4129  ast_channel_lock_both(local, transferer);
4131 
4133  if (!transfer_message->replace_channel) {
4134  ast_hangup(local);
4135  return AST_BRIDGE_TRANSFER_FAIL;
4136  }
4137 
4139  ast_channel_unlock(local);
4140  ast_channel_unlock(transferer);
4141 
4142  if (new_channel_cb) {
4143  new_channel_cb(local, user_data_wrapper, AST_BRIDGE_TRANSFER_MULTI_PARTY);
4144  }
4145 
4146  if (ast_call(local, chan_name, 0)) {
4147  ast_hangup(local);
4148  return AST_BRIDGE_TRANSFER_FAIL;
4149  }
4150 
4151  if (ast_bridge_impart(bridge, local, transferer, NULL,
4153  ast_hangup(local);
4154  return AST_BRIDGE_TRANSFER_FAIL;
4155  }
4156 
4158 }
4159 
4160 /*!
4161  * \internal
4162  * \brief Get the transferee channel
4163  *
4164  * This is only applicable to cases where a transfer is occurring on a
4165  * two-party bridge. The channels container passed in is expected to only
4166  * contain two channels, the transferer and the transferee. The transferer
4167  * channel is passed in as a parameter to ensure we don't return it as
4168  * the transferee channel.
4169  *
4170  * \param channels A two-channel container containing the transferer and transferee
4171  * \param transferer The party that is transfering the call
4172  * \return The party that is being transferred
4173  */
4174 static struct ast_channel *get_transferee(struct ao2_container *channels, struct ast_channel *transferer)
4175 {
4176  struct ao2_iterator channel_iter;
4177  struct ast_channel *transferee;
4178 
4179  for (channel_iter = ao2_iterator_init(channels, 0);
4180  (transferee = ao2_iterator_next(&channel_iter));
4181  ao2_cleanup(transferee)) {
4182  if (transferee != transferer) {
4183  break;
4184  }
4185  }
4186 
4187  ao2_iterator_destroy(&channel_iter);
4188  return transferee;
4189 }
4190 
4191 /*!
4192  * \brief Perform an attended transfer of a bridge
4193  *
4194  * This performs an attended transfer of an entire bridge to a target.
4195  * The target varies, depending on what bridges exist during the transfer
4196  * attempt.
4197  *
4198  * If two bridges exist, then a local channel is created to link the two
4199  * bridges together.
4200  *
4201  * If only one bridge exists, then a local channel is created with one end
4202  * placed into the existing bridge and the other end masquerading into
4203  * the unbridged channel.
4204  *
4205  * \param chan1 Transferer channel. Guaranteed to be bridged.
4206  * \param chan2 Other transferer channel. May or may not be bridged.
4207  * \param bridge1 Bridge that chan1 is in. Guaranteed to be non-NULL.
4208  * \param bridge2 Bridge that chan2 is in. If NULL, then chan2 is not bridged.
4209  * \param transfer_msg Data to publish for a stasis attended transfer message.
4210  * \retval AST_BRIDGE_TRANSFER_FAIL Internal error occurred
4211  * \retval AST_BRIDGE_TRANSFER_SUCCESS Succesfully transferred the bridge
4212  */
4213 static enum ast_transfer_result attended_transfer_bridge(struct ast_channel *chan1,
4214  struct ast_channel *chan2, struct ast_bridge *bridge1, struct ast_bridge *bridge2,
4215  struct ast_attended_transfer_message *transfer_msg)
4216 {
4217 #define BRIDGE_LOCK_ONE_OR_BOTH(b1, b2) \
4218  do { \
4219  if (b2) { \
4220  ast_bridge_lock_both(b1, b2); \
4221  } else { \
4222  ast_bridge_lock(b1); \
4223  } \
4224  } while (0)
4225 
4226  static const char *dest = "_attended@transfer/m";
4227  struct ast_channel *local_chan;
4228  int cause;
4229  int res;
4230  const char *app = NULL;
4231 
4232  local_chan = ast_request("Local", ast_channel_nativeformats(chan1), NULL, chan1,
4233  dest, &cause);
4234  if (!local_chan) {
4235  return AST_BRIDGE_TRANSFER_FAIL;
4236  }
4237 
4238  ast_channel_lock_both(local_chan, chan1);
4241  ast_channel_unlock(local_chan);
4242  ast_channel_unlock(chan1);
4243 
4244  if (bridge2) {
4245  res = ast_local_setup_bridge(local_chan, bridge2, chan2, NULL);
4246  } else {
4247  app = ast_strdupa(ast_channel_appl(chan2));
4248  res = ast_local_setup_masquerade(local_chan, chan2);
4249  }
4250 
4251  if (res) {
4252  ast_hangup(local_chan);
4253  return AST_BRIDGE_TRANSFER_FAIL;
4254  }
4255 
4256  /*
4257  * Since bridges need to be unlocked before entering ast_bridge_impart and
4258  * core_local may call into it then the bridges need to be unlocked here.
4259  */
4260  ast_bridge_unlock(bridge1);
4261  if (bridge2) {
4262  ast_bridge_unlock(bridge2);
4263  }
4264 
4265  if (ast_call(local_chan, dest, 0)) {
4266  ast_hangup(local_chan);
4267  BRIDGE_LOCK_ONE_OR_BOTH(bridge1, bridge2);
4268  return AST_BRIDGE_TRANSFER_FAIL;
4269  }
4270 
4271  /* Get a ref for use later since this one is being stolen */
4272  ao2_ref(local_chan, +1);
4273  if (ast_bridge_impart(bridge1, local_chan, chan1, NULL,
4275  ast_hangup(local_chan);
4276  ao2_cleanup(local_chan);
4277  BRIDGE_LOCK_ONE_OR_BOTH(bridge1, bridge2);
4278  return AST_BRIDGE_TRANSFER_FAIL;
4279  }
4280  BRIDGE_LOCK_ONE_OR_BOTH(bridge1, bridge2);
4281 
4282  if (bridge2) {
4283  void *tech;
4284  struct ast_channel *locals[2];
4285 
4286  /* Have to lock everything just in case a hangup comes in early */
4287  ast_local_lock_all(local_chan, &tech, &locals[0], &locals[1]);
4288  if (!locals[0] || !locals[1]) {
4289  ast_log(LOG_ERROR, "Transfer failed probably due to an early hangup - "
4290  "missing other half of '%s'\n", ast_channel_name(local_chan));
4292  ao2_cleanup(local_chan);
4293  return AST_BRIDGE_TRANSFER_FAIL;
4294  }
4295 
4296  /* Make sure the peer is properly set */
4297  if (local_chan != locals[0]) {
4298  SWAP(locals[0], locals[1]);
4299  }
4300 
4303  } else {
4304  ast_attended_transfer_message_add_app(transfer_msg, app, local_chan);
4305  }
4306 
4307  ao2_cleanup(local_chan);
4309 }
4310 
4311 static enum ast_transfer_result try_parking(struct ast_channel *transferer,
4312  const char *context, const char *exten, transfer_channel_cb new_channel_cb,
4313  struct transfer_channel_data *user_data_wrapper)
4314 {
4315  RAII_VAR(struct ast_bridge_channel *, transferer_bridge_channel, NULL, ao2_cleanup);
4316 
4318  return AST_BRIDGE_TRANSFER_FAIL;
4319  }
4320 
4321  ast_channel_lock(transferer);
4322  transferer_bridge_channel = ast_channel_get_bridge_channel(transferer);
4323  ast_channel_unlock(transferer);
4324 
4325  if (!transferer_bridge_channel) {
4326  return AST_BRIDGE_TRANSFER_FAIL;
4327  }
4328 
4329  if (ast_parking_blind_transfer_park(transferer_bridge_channel,
4330  context, exten, new_channel_cb, user_data_wrapper)) {
4331  return AST_BRIDGE_TRANSFER_FAIL;
4332  }
4333 
4335 }
4336 
4337 void ast_bridge_set_transfer_variables(struct ast_channel *chan, const char *value, int attended)
4338 {
4339  char *writevar;
4340  char *erasevar;
4341 
4342  if (attended) {
4343  writevar = ATTENDEDTRANSFER;
4344  erasevar = BLINDTRANSFER;
4345  } else {
4346  writevar = BLINDTRANSFER;
4347  erasevar = ATTENDEDTRANSFER;
4348  }
4349 
4350  pbx_builtin_setvar_helper(chan, writevar, value);
4351  pbx_builtin_setvar_helper(chan, erasevar, NULL);
4352 }
4353 
4354 /*!
4355  * \internal
4356  * \brief Set the transfer variable as appropriate on channels involved in the transfer
4357  *
4358  * The transferer channel will have its variable set the same as its BRIDGEPEER
4359  * variable. This will account for all channels that it is bridged to. The other channels
4360  * involved in the transfer will have their variable set to the transferer
4361  * channel's name.
4362  *
4363  * \param transferer The channel performing the transfer
4364  * \param channels The channels belonging to the bridge
4365  * \param is_attended false set BLINDTRANSFER and unset ATTENDEDTRANSFER
4366  * true set ATTENDEDTRANSFER and unset BLINDTRANSFER
4367  */
4368 static void set_transfer_variables_all(struct ast_channel *transferer, struct ao2_container *channels, int is_attended)
4369 {
4370  struct ao2_iterator iter;
4371  struct ast_channel *chan;
4372  const char *transferer_name;
4373  const char *transferer_bridgepeer;
4374 
4375  ast_channel_lock(transferer);
4376  transferer_name = ast_strdupa(ast_channel_name(transferer));
4377  transferer_bridgepeer = ast_strdupa(S_OR(pbx_builtin_getvar_helper(transferer, "BRIDGEPEER"), ""));
4378  ast_channel_unlock(transferer);
4379 
4380  for (iter = ao2_iterator_init(channels, 0);
4381  (chan = ao2_iterator_next(&iter));
4382  ao2_cleanup(chan)) {
4383  if (chan == transferer) {
4384  ast_bridge_set_transfer_variables(chan, transferer_bridgepeer, is_attended);
4385  } else {
4386  ast_bridge_set_transfer_variables(chan, transferer_name, is_attended);
4387  }
4388  }
4389 
4390  ao2_iterator_destroy(&iter);
4391 }
4392 
4394 {
4395  struct ast_bridge *bridge;
4396 
4397  ast_channel_lock(chan);
4398  bridge = ast_channel_get_bridge(chan);
4399  ast_channel_unlock(chan);
4400 
4401  if (bridge && ast_test_flag(&bridge->feature_flags,
4403  ao2_ref(bridge, -1);
4404  bridge = NULL;
4405  }
4406 
4407  return bridge;
4408 }
4409 
4410 enum ast_transfer_result ast_bridge_transfer_blind(int is_external,
4411  struct ast_channel *transferer, const char *exten, const char *context,
4412  transfer_channel_cb new_channel_cb, void *user_data)
4413 {
4414  RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
4415  RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
4417  RAII_VAR(struct ast_channel *, transferee, NULL, ast_channel_cleanup);
4418  RAII_VAR(struct transfer_channel_data *, user_data_wrapper, NULL, ao2_cleanup);
4419  RAII_VAR(struct ast_blind_transfer_message *, transfer_message, NULL, ao2_cleanup);
4420  int do_bridge_transfer;
4421  int transfer_prohibited;
4422  enum ast_transfer_result transfer_result;
4423 
4424  transfer_message = ast_blind_transfer_message_create(is_external, transferer, exten, context);
4425  if (!transfer_message) {
4426  /* Out of memory. Not even possible to publish a Stasis message about the
4427  * failure
4428  */
4429  ast_log(LOG_ERROR, "Unable to allocate memory for blind transfer publication from %s\n",
4430  ast_channel_name(transferer));
4431  return AST_BRIDGE_TRANSFER_FAIL;
4432  }
4433 
4434  bridge = ast_bridge_transfer_acquire_bridge(transferer);
4435  if (!bridge) {
4436  transfer_result = AST_BRIDGE_TRANSFER_INVALID;
4437  goto publish;
4438  }
4439 
4440  ast_bridge_lock(bridge);
4441  transfer_message->bridge = ast_bridge_snapshot_create(bridge);
4442  ast_bridge_unlock(bridge);
4443  if (!transfer_message->bridge) {
4444  transfer_result = AST_BRIDGE_TRANSFER_FAIL;
4445  goto publish;
4446  }
4447 
4448  transferee = ast_bridge_peer(bridge, transferer);
4449  if (transferee) {
4450  transfer_message->transferee = ast_channel_snapshot_get_latest(ast_channel_uniqueid(transferee));
4451  if (!transfer_message->transferee) {
4452  transfer_result = AST_BRIDGE_TRANSFER_FAIL;
4453  goto publish;
4454  }
4455  }
4456 
4457  ast_channel_lock(transferer);
4458  bridge_channel = ast_channel_get_bridge_channel(transferer);
4459  ast_channel_unlock(transferer);
4460  if (!bridge_channel) {
4461  transfer_result = AST_BRIDGE_TRANSFER_INVALID;
4462  goto publish;
4463  }
4464 
4465  user_data_wrapper = ao2_alloc(sizeof(*user_data_wrapper), NULL);
4466  if (!user_data_wrapper) {
4467  transfer_result = AST_BRIDGE_TRANSFER_FAIL;
4468  goto publish;
4469  }
4470 
4471  user_data_wrapper->data = user_data;
4472 
4473  /* Take off hold if they are on hold. */
4474  ast_bridge_channel_write_unhold(bridge_channel);
4475 
4476  transfer_result = try_parking(transferer, context, exten, new_channel_cb, user_data_wrapper);
4477  if (transfer_result == AST_BRIDGE_TRANSFER_SUCCESS) {
4478  goto publish;
4479  }
4480 
4481  /* Since parking didn't take control of the user_data_wrapper, we are just going to raise the completed flag now. */
4482  user_data_wrapper->completed = 1;
4483 
4484  {
4486 
4488  if (!channels) {
4489  transfer_result = AST_BRIDGE_TRANSFER_FAIL;
4490  goto publish;
4491  }
4492  if (ao2_container_count(channels) <= 1) {
4493  transfer_result = AST_BRIDGE_TRANSFER_INVALID;
4494  goto publish;
4495  }
4496  transfer_prohibited = ast_test_flag(&bridge->feature_flags,
4498  do_bridge_transfer = ast_test_flag(&bridge->feature_flags,
4501  }
4502 
4503  if (transfer_prohibited) {
4504  transfer_result = AST_BRIDGE_TRANSFER_NOT_PERMITTED;
4505  goto publish;
4506  }
4507 
4508  set_transfer_variables_all(transferer, channels, 0);
4509 
4510  if (do_bridge_transfer) {
4511  transfer_result = blind_transfer_bridge(is_external, transferer, bridge,
4512  exten, context, transferee, new_channel_cb, user_data_wrapper, transfer_message);
4513  goto publish;
4514  }
4515 
4516  /* Reaching this portion means that we're dealing with a two-party bridge */
4517 
4518  if (!transferee) {
4519  transfer_result = AST_BRIDGE_TRANSFER_FAIL;
4520  goto publish;
4521  }
4522 
4524  new_channel_cb, user_data_wrapper)) {
4525  transfer_result = AST_BRIDGE_TRANSFER_FAIL;
4526  goto publish;
4527  }
4528 
4529  ast_bridge_remove(bridge, transferer);
4530  transfer_result = AST_BRIDGE_TRANSFER_SUCCESS;
4531 
4532 publish:
4533  transfer_message->result = transfer_result;
4534  ast_bridge_publish_blind_transfer(transfer_message);
4535  return transfer_result;
4536 }
4537 
4538 /*!
4539  * \internal
4540  * \brief Performs an attended transfer by moving a channel from one bridge to another
4541  *
4542  * The channel that is bridged to the source_channel is moved into the dest_bridge from
4543  * the source_bridge_channel's bridge. The swap_channel is swapped out of the dest_bridge and placed in
4544  * the source_bridge_channel's bridge.
4545  *
4546  * \note dest_bridge and source_bridge_channel's bridge MUST be locked before calling this function.
4547  *
4548  * \param dest_bridge The final bridge for the attended transfer
4549  * \param source_bridge_channel Channel who is bridged to the channel that will move
4550  * \param swap_channel Channel to be swapped out of the dest_bridge
4551  * \return The success or failure of the swap attempt
4552  */
4553 static enum ast_transfer_result bridge_swap_attended_transfer(struct ast_bridge *dest_bridge,
4554  struct ast_bridge_channel *source_bridge_channel, struct ast_channel *swap_channel)
4555 {
4556  struct ast_bridge_channel *bridged_to_source;
4557 
4558  bridged_to_source = ast_bridge_channel_peer(source_bridge_channel);
4559  if (bridged_to_source
4560  && bridged_to_source->state == BRIDGE_CHANNEL_STATE_WAIT
4561  && !ast_test_flag(&bridged_to_source->features->feature_flags,
4563  bridged_to_source->swap = swap_channel;
4564  if (bridge_do_move(dest_bridge, bridged_to_source, 1, 0)) {
4565  return AST_BRIDGE_TRANSFER_FAIL;
4566  }
4567  /* Must kick the source channel out of its bridge. */
4568  ast_bridge_channel_leave_bridge(source_bridge_channel,
4571  } else {
4573  }
4574 }
4575 
4576 /*!
4577  * \internal
4578  * \brief Function that performs an attended transfer when both transferer channels are bridged
4579  *
4580  * The method by which the transfer is performed is dependent on whether the bridges allow for
4581  * optimization to occur between them. If no optimization is permitted, then an unreal channel
4582  * is placed as a link between the two bridges. If optimization is permitted, then that means
4583  * we are free to perform move or merge operations in order to perform the transfer.
4584  *
4585  * \note to_transferee_bridge and to_target_bridge MUST be locked before calling this function
4586  *
4587  * \param to_transferee The channel that is bridged to the transferee
4588  * \param to_transferee_bridge_channel to_transferee's bridge_channel
4589  * \param to_transfer_target The channel that is bridged to the transfer target
4590  * \param to_target_bridge_channel to_transfer_target's bridge_channel
4591  * \param to_transferee_bridge The bridge between to_transferee and the transferee
4592  * \param to_target_bridge The bridge between to_transfer_target and the transfer_target
4593  * \param transfer_msg Data to publish for a stasis attended transfer message
4594  * \return The success or failure of the attended transfer
4595  */
4596 static enum ast_transfer_result two_bridge_attended_transfer(struct ast_channel *to_transferee,
4597  struct ast_bridge_channel *to_transferee_bridge_channel,
4598  struct ast_channel *to_transfer_target,
4599  struct ast_bridge_channel *to_target_bridge_channel,
4600  struct ast_bridge *to_transferee_bridge, struct ast_bridge *to_target_bridge,
4601  struct ast_attended_transfer_message *transfer_msg)
4602 {
4603  struct ast_bridge_channel *kick_me[] = {
4604  to_transferee_bridge_channel,
4605  to_target_bridge_channel,
4606  };
4607  enum ast_transfer_result res;
4608  struct ast_bridge *final_bridge = NULL;
4610 
4611  channels = ast_bridge_peers_nolock(to_transferee_bridge);
4612 
4613  if (!channels) {
4615  goto end;
4616  }
4617 
4618  set_transfer_variables_all(to_transferee, channels, 1);
4619 
4620  switch (ast_bridges_allow_optimization(to_transferee_bridge, to_target_bridge)) {
4622  final_bridge = to_transferee_bridge;
4623  res = bridge_swap_attended_transfer(to_transferee_bridge, to_target_bridge_channel, to_transferee);
4624  goto end;
4626  final_bridge = to_target_bridge;
4627  res = bridge_swap_attended_transfer(to_target_bridge, to_transferee_bridge_channel, to_transfer_target);
4628  goto end;
4630  final_bridge = to_transferee_bridge;
4631  bridge_do_merge(to_transferee_bridge, to_target_bridge, kick_me, ARRAY_LEN(kick_me), 0);
4633  goto end;
4635  final_bridge = to_target_bridge;
4636  bridge_do_merge(to_target_bridge, to_transferee_bridge, kick_me, ARRAY_LEN(kick_me), 0);
4638  goto end;
4640  default:
4641  /* Just because optimization wasn't doable doesn't necessarily mean
4642  * that we can actually perform the transfer. Some reasons for non-optimization
4643  * indicate bridge invalidity, so let's check those before proceeding.
4644  */
4645  if (to_transferee_bridge->inhibit_merge || to_transferee_bridge->dissolved ||
4646  to_target_bridge->inhibit_merge || to_target_bridge->dissolved) {
4648  }
4649 
4650  return attended_transfer_bridge(to_transferee, to_transfer_target,
4651  to_transferee_bridge, to_target_bridge, transfer_msg);
4652  }
4653 
4654 end:
4655  if (res == AST_BRIDGE_TRANSFER_SUCCESS) {
4656  ast_attended_transfer_message_add_merge(transfer_msg, final_bridge);
4657  }
4658 
4659  return res;
4660 }
4661 
4663  struct ast_channel *to_transfer_target)
4664 {
4665  RAII_VAR(struct ast_bridge *, to_transferee_bridge, NULL, ao2_cleanup);
4666  RAII_VAR(struct ast_bridge *, to_target_bridge, NULL, ao2_cleanup);
4667  RAII_VAR(struct ast_bridge_channel *, to_transferee_bridge_channel, NULL, ao2_cleanup);
4668  RAII_VAR(struct ast_bridge_channel *, to_target_bridge_channel, NULL, ao2_cleanup);
4670  RAII_VAR(struct ast_channel *, transferee, NULL, ao2_cleanup);
4671  RAII_VAR(struct ast_attended_transfer_message *, transfer_msg, NULL, ao2_cleanup);
4672  struct ast_bridge *the_bridge = NULL;
4673  struct ast_channel *chan_bridged;
4674  struct ast_channel *chan_unbridged;
4675  int transfer_prohibited;
4676  int do_bridge_transfer;
4677  enum ast_transfer_result res;
4678  const char *app = NULL;
4679  int hangup_target = 0;
4680 
4681  to_transferee_bridge = ast_bridge_transfer_acquire_bridge(to_transferee);
4682  to_target_bridge = ast_bridge_transfer_acquire_bridge(to_transfer_target);
4683 
4684  transfer_msg = ast_attended_transfer_message_create(1, to_transferee, to_transferee_bridge,
4685  to_transfer_target, to_target_bridge, NULL, NULL);
4686  if (!transfer_msg) {
4687  ast_log(LOG_ERROR, "Unable to create Stasis publication for attended transfer from %s\n",
4688  ast_channel_name(to_transferee));
4689  return AST_BRIDGE_TRANSFER_FAIL;
4690  }
4691 
4692  /* They can't both be unbridged, you silly goose! */
4693  if (!to_transferee_bridge && !to_target_bridge) {
4695  goto end;
4696  }
4697 
4698  ast_channel_lock(to_transferee);
4699  to_transferee_bridge_channel = ast_channel_get_bridge_channel(to_transferee);
4700  ast_channel_unlock(to_transferee);
4701 
4702  ast_channel_lock(to_transfer_target);
4703  to_target_bridge_channel = ast_channel_get_bridge_channel(to_transfer_target);
4704  ast_channel_unlock(to_transfer_target);
4705 
4706  if (to_transferee_bridge_channel) {
4707  /* Take off hold if they are on hold. */
4708  if (ast_bridge_channel_write_unhold(to_transferee_bridge_channel)) {
4709  ast_log(LOG_ERROR, "Transferee channel disappeared during transfer!\n");
4711  goto end;
4712  }
4713  }
4714 
4715  if (to_target_bridge_channel) {
4716  const char *target_complete_sound;
4717 
4718  /* Take off hold if they are on hold. */
4719  if (ast_bridge_channel_write_unhold(to_target_bridge_channel)) {
4720  ast_log(LOG_ERROR, "Target channel disappeared during transfer!\n");
4722  goto end;
4723  }
4724 
4725  /* Is there a courtesy sound to play to the target? */
4726  ast_channel_lock(to_transfer_target);
4727  target_complete_sound = pbx_builtin_getvar_helper(to_transfer_target,
4728  "ATTENDED_TRANSFER_COMPLETE_SOUND");
4729  if (!ast_strlen_zero(target_complete_sound)) {
4730  target_complete_sound = ast_strdupa(target_complete_sound);
4731  } else {
4732  target_complete_sound = NULL;
4733  }
4734  ast_channel_unlock(to_transfer_target);
4735  if (!target_complete_sound) {
4736  ast_channel_lock(to_transferee);
4737  target_complete_sound = pbx_builtin_getvar_helper(to_transferee,
4738  "ATTENDED_TRANSFER_COMPLETE_SOUND");
4739  if (!ast_strlen_zero(target_complete_sound)) {
4740  target_complete_sound = ast_strdupa(target_complete_sound);
4741  } else {
4742  target_complete_sound = NULL;
4743  }
4744  ast_channel_unlock(to_transferee);
4745  }
4746  if (target_complete_sound) {
4747  ast_bridge_channel_write_playfile(to_target_bridge_channel, NULL,
4748  target_complete_sound, NULL);
4749  }
4750  }
4751 
4752  /* Let's get the easy one out of the way first */
4753  if (to_transferee_bridge && to_target_bridge) {
4754 
4755  if (!to_transferee_bridge_channel || !to_target_bridge_channel) {
4757  goto end;
4758  }
4759 
4760  ast_bridge_lock_both(to_transferee_bridge, to_target_bridge);
4761  res = two_bridge_attended_transfer(to_transferee, to_transferee_bridge_channel,
4762  to_transfer_target, to_target_bridge_channel,
4763  to_transferee_bridge, to_target_bridge, transfer_msg);
4764  ast_bridge_unlock(to_transferee_bridge);
4765  ast_bridge_unlock(to_target_bridge);
4766 
4767  hangup_target = 1;
4768  goto end;
4769  }
4770 
4771  the_bridge = to_transferee_bridge ?: to_target_bridge;
4772  chan_bridged = to_transferee_bridge ? to_transferee : to_transfer_target;
4773  chan_unbridged = to_transferee_bridge ? to_transfer_target : to_transferee;
4774 
4775  /*
4776  * Race condition makes it possible for app to be NULL, so get the app prior to
4777  * transferring with a fallback of "unknown".
4778  */
4779  app = ast_strdupa(ast_channel_appl(chan_unbridged) ?: "unknown");
4780 
4781  {
4782  int chan_count;
4784 
4785  channels = ast_bridge_peers_nolock(the_bridge);
4786  if (!channels) {
4788  goto end;
4789  }
4790  chan_count = ao2_container_count(channels);
4791  if (chan_count <= 1) {
4793  goto end;
4794  }
4795  transfer_prohibited = ast_test_flag(&the_bridge->feature_flags,
4797  do_bridge_transfer = ast_test_flag(&the_bridge->feature_flags,
4799  chan_count > 2;
4800  }
4801 
4802  if (transfer_prohibited) {
4804  goto end;
4805  }
4806 
4807  set_transfer_variables_all(to_transferee, channels, 1);
4808 
4809  if (do_bridge_transfer) {
4810  /*
4811  * Hang up the target if it was bridged. Note, if it is not bridged
4812  * it is hung up during the masquerade.
4813  */
4814  hangup_target = chan_bridged == to_transfer_target;
4815  ast_bridge_lock(the_bridge);
4816  res = attended_transfer_bridge(chan_bridged, chan_unbridged, the_bridge, NULL, transfer_msg);
4817  ast_bridge_unlock(the_bridge);
4818  goto end;
4819  }
4820 
4821  transferee = get_transferee(channels, chan_bridged);
4822  if (!transferee) {
4824  goto end;
4825  }
4826 
4827  if (bridge_channel_internal_queue_attended_transfer(transferee, chan_unbridged)) {
4829  goto end;
4830  }
4831 
4832  ast_bridge_remove(the_bridge, chan_bridged);
4833 
4836 
4837 end:
4838  if ((res == AST_BRIDGE_TRANSFER_SUCCESS && hangup_target) || res == AST_BRIDGE_TRANSFER_FAIL) {
4839  ast_softhangup(to_transfer_target, AST_SOFTHANGUP_DEV);
4840  }
4841 
4842  transfer_msg->result = res;
4844  return res;
4845 }
4846 
4847 /*!
4848  * \internal
4849  * \brief Service the bridge manager request.
4850  * \since 12.0.0
4851  *
4852  * \param bridge requesting service.
4853  */
4855 {
4857  if (bridge->callid) {
4858