2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007 - 2009, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
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.
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.
23 * \author Joshua Colp <jcolp@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/logger.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/options.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/lock.h"
39 #include "asterisk/linkedlists.h"
40 #include "asterisk/bridge.h"
41 #include "asterisk/bridge_internal.h"
42 #include "asterisk/bridge_channel_internal.h"
43 #include "asterisk/bridge_features.h"
44 #include "asterisk/bridge_basic.h"
45 #include "asterisk/bridge_technology.h"
46 #include "asterisk/bridge_channel.h"
47 #include "asterisk/bridge_after.h"
48 #include "asterisk/stasis_bridges.h"
49 #include "asterisk/stasis_channels.h"
50 #include "asterisk/stasis_cache_pattern.h"
51 #include "asterisk/app.h"
52 #include "asterisk/file.h"
53 #include "asterisk/module.h"
54 #include "asterisk/astobj2.h"
55 #include "asterisk/pbx.h"
56 #include "asterisk/test.h"
57 #include "asterisk/_private.h"
59 #include "asterisk/heap.h"
60 #include "asterisk/say.h"
61 #include "asterisk/timing.h"
62 #include "asterisk/stringfields.h"
63 #include "asterisk/musiconhold.h"
64 #include "asterisk/features.h"
65 #include "asterisk/cli.h"
66 #include "asterisk/parking.h"
67 #include "asterisk/core_local.h"
68 #include "asterisk/core_unreal.h"
70 /*! All bridges container. */
71 static struct ao2_container *bridges;
73 static AST_RWLIST_HEAD_STATIC(bridge_technologies, ast_bridge_technology);
75 /* Initial starting point for the bridge array of channels */
76 #define BRIDGE_ARRAY_START 128
78 /* Grow rate of bridge array of channels */
79 #define BRIDGE_ARRAY_GROW 32
81 static void cleanup_video_mode(struct ast_bridge *bridge);
82 static int bridge_make_compatible(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel);
84 /*! Default DTMF keys for built in features */
85 static char builtin_features_dtmf[AST_BRIDGE_BUILTIN_END][MAXIMUM_DTMF_FEATURE_STRING];
87 /*! Function handlers for the built in features */
88 static ast_bridge_hook_callback builtin_features_handlers[AST_BRIDGE_BUILTIN_END];
90 /*! Function handlers for built in interval features */
91 static ast_bridge_builtin_set_limits_fn builtin_interval_handlers[AST_BRIDGE_BUILTIN_INTERVAL_END];
93 /*! Bridge manager service request */
94 struct bridge_manager_request {
95 /*! List of bridge service requests. */
96 AST_LIST_ENTRY(bridge_manager_request) node;
97 /*! Refed bridge requesting service. */
98 struct ast_bridge *bridge;
101 struct bridge_manager_controller {
102 /*! Condition, used to wake up the bridge manager thread. */
104 /*! Queue of bridge service requests. */
105 AST_LIST_HEAD_NOLOCK(, bridge_manager_request) service_requests;
106 /*! Manager thread */
108 /*! TRUE if the manager needs to stop. */
112 /*! Bridge manager controller. */
113 static struct bridge_manager_controller *bridge_manager;
117 * \brief Request service for a bridge from the bridge manager.
120 * \param bridge Requesting service.
124 static void bridge_manager_service_req(struct ast_bridge *bridge)
126 struct bridge_manager_request *request;
128 ao2_lock(bridge_manager);
129 if (bridge_manager->stop) {
130 ao2_unlock(bridge_manager);
134 /* Create the service request. */
135 request = ast_calloc(1, sizeof(*request));
137 /* Well. This isn't good. */
138 ao2_unlock(bridge_manager);
142 request->bridge = bridge;
144 /* Put request into the queue and wake the bridge manager. */
145 AST_LIST_INSERT_TAIL(&bridge_manager->service_requests, request, node);
146 ast_cond_signal(&bridge_manager->cond);
147 ao2_unlock(bridge_manager);
150 int __ast_bridge_technology_register(struct ast_bridge_technology *technology, struct ast_module *module)
152 struct ast_bridge_technology *current;
154 /* Perform a sanity check to make sure the bridge technology conforms to our needed requirements */
155 if (ast_strlen_zero(technology->name)
156 || !technology->capabilities
157 || !technology->write) {
158 ast_log(LOG_WARNING, "Bridge technology %s failed registration sanity check.\n",
163 AST_RWLIST_WRLOCK(&bridge_technologies);
165 /* Look for duplicate bridge technology already using this name, or already registered */
166 AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
167 if ((!strcasecmp(current->name, technology->name)) || (current == technology)) {
168 ast_log(LOG_WARNING, "A bridge technology of %s already claims to exist in our world.\n",
170 AST_RWLIST_UNLOCK(&bridge_technologies);
175 /* Copy module pointer so reference counting can keep the module from unloading */
176 technology->mod = module;
178 /* Insert our new bridge technology into the list and print out a pretty message */
179 AST_RWLIST_INSERT_TAIL(&bridge_technologies, technology, entry);
181 AST_RWLIST_UNLOCK(&bridge_technologies);
183 ast_verb(2, "Registered bridge technology %s\n", technology->name);
188 int ast_bridge_technology_unregister(struct ast_bridge_technology *technology)
190 struct ast_bridge_technology *current;
192 AST_RWLIST_WRLOCK(&bridge_technologies);
194 /* Ensure the bridge technology is registered before removing it */
195 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&bridge_technologies, current, entry) {
196 if (current == technology) {
197 AST_RWLIST_REMOVE_CURRENT(entry);
198 ast_verb(2, "Unregistered bridge technology %s\n", technology->name);
202 AST_RWLIST_TRAVERSE_SAFE_END;
204 AST_RWLIST_UNLOCK(&bridge_technologies);
206 return current ? 0 : -1;
211 * \brief Put an action onto the specified bridge. Don't dup the action frame.
214 * \param bridge What to queue the action on.
215 * \param action What to do.
219 static void bridge_queue_action_nodup(struct ast_bridge *bridge, struct ast_frame *action)
221 ast_debug(1, "Bridge %s: queueing action type:%d sub:%d\n",
222 bridge->uniqueid, action->frametype, action->subclass.integer);
224 ast_bridge_lock(bridge);
225 AST_LIST_INSERT_TAIL(&bridge->action_queue, action, frame_list);
226 ast_bridge_unlock(bridge);
227 bridge_manager_service_req(bridge);
230 int ast_bridge_queue_action(struct ast_bridge *bridge, struct ast_frame *action)
232 struct ast_frame *dup;
234 dup = ast_frdup(action);
238 bridge_queue_action_nodup(bridge, dup);
244 * \brief Dissolve the bridge.
247 * \param bridge Bridge to eject all channels
250 * Force out all channels that are not already going out of the
251 * bridge. Any new channels joining will leave immediately.
253 * \note On entry, bridge is already locked.
257 void bridge_dissolve(struct ast_bridge *bridge)
259 struct ast_bridge_channel *bridge_channel;
260 struct ast_frame action = {
261 .frametype = AST_FRAME_BRIDGE_ACTION,
262 .subclass.integer = BRIDGE_CHANNEL_ACTION_DEFERRED_DISSOLVING,
265 if (bridge->dissolved) {
268 bridge->dissolved = 1;
270 ast_debug(1, "Bridge %s: dissolving bridge\n", bridge->uniqueid);
272 /* BUGBUG need a cause code on the bridge for the later ejected channels. */
273 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
274 ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE);
277 /* Must defer dissolving bridge because it is already locked. */
278 ast_bridge_queue_action(bridge, &action);
283 * \brief Check if a bridge should dissolve because of a stolen channel and do it.
286 * \param bridge Bridge to check.
287 * \param bridge_channel Stolen channel causing the check. It is not in the bridge to check and may be in another bridge.
289 * \note On entry, bridge and bridge_channel->bridge are already locked.
293 static void bridge_dissolve_check_stolen(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
295 if (bridge->dissolved) {
299 if (bridge_channel->features->usable
300 && ast_test_flag(&bridge_channel->features->feature_flags,
301 AST_BRIDGE_CHANNEL_FLAG_DISSOLVE_HANGUP)) {
302 /* The stolen channel controlled the bridge it was stolen from. */
303 bridge_dissolve(bridge);
306 if (bridge->num_channels < 2
307 && ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_HANGUP)) {
309 * The stolen channel has not left enough channels to keep the
310 * bridge alive. Assume the stolen channel hung up.
312 bridge_dissolve(bridge);
319 * \brief Update connected line information after a bridge has been reconfigured.
321 * \param bridge The bridge itself.
325 static void bridge_reconfigured_connected_line_update(struct ast_bridge *bridge)
327 struct ast_party_connected_line connected;
328 struct ast_bridge_channel *bridge_channel = AST_LIST_FIRST(&bridge->channels), *peer;
329 unsigned char data[1024];
332 if (!bridge_channel ||
333 !(bridge->technology->capabilities & (AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE)) ||
334 !(peer = ast_bridge_channel_peer(bridge_channel)) ||
335 ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_ZOMBIE) ||
336 ast_test_flag(ast_channel_flags(peer->chan), AST_FLAG_ZOMBIE) ||
337 ast_check_hangup_locked(bridge_channel->chan) ||
338 ast_check_hangup_locked(peer->chan)) {
342 ast_party_connected_line_init(&connected);
344 ast_channel_lock(bridge_channel->chan);
345 ast_connected_line_copy_from_caller(&connected, ast_channel_caller(bridge_channel->chan));
346 ast_channel_unlock(bridge_channel->chan);
348 if ((datalen = ast_connected_line_build_data(data, sizeof(data), &connected, NULL)) != (size_t) -1) {
349 ast_bridge_channel_queue_control_data(peer, AST_CONTROL_CONNECTED_LINE, data, datalen);
352 ast_channel_lock(peer->chan);
353 ast_connected_line_copy_from_caller(&connected, ast_channel_caller(peer->chan));
354 ast_channel_unlock(peer->chan);
356 if ((datalen = ast_connected_line_build_data(data, sizeof(data), &connected, NULL)) != (size_t) -1) {
357 ast_bridge_channel_queue_control_data(bridge_channel, AST_CONTROL_CONNECTED_LINE, data, datalen);
360 ast_party_connected_line_free(&connected);
365 * \brief Complete joining a channel to the bridge.
368 * \param bridge What to operate upon.
369 * \param bridge_channel What is joining the bridge technology.
371 * \note On entry, bridge is already locked.
375 static void bridge_channel_complete_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
377 /* Make the channel compatible with the bridge */
378 bridge_make_compatible(bridge, bridge_channel);
380 /* Tell the bridge technology we are joining so they set us up */
381 ast_debug(1, "Bridge %s: %p(%s) is joining %s technology\n",
382 bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
383 bridge->technology->name);
384 if (bridge->technology->join
385 && bridge->technology->join(bridge, bridge_channel)) {
386 ast_debug(1, "Bridge %s: %p(%s) failed to join %s technology\n",
387 bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
388 bridge->technology->name);
389 bridge_channel->just_joined = 1;
393 bridge_channel->just_joined = 0;
398 * \brief Complete joining new channels to the bridge.
401 * \param bridge Check for new channels on this bridge.
403 * \note On entry, bridge is already locked.
407 static void bridge_complete_join(struct ast_bridge *bridge)
409 struct ast_bridge_channel *bridge_channel;
411 if (bridge->dissolved) {
413 * No sense in completing the join on channels for a dissolved
414 * bridge. They are just going to be removed soon anyway.
415 * However, we do have reason to abort here because the bridge
416 * technology may not be able to handle the number of channels
417 * still in the bridge.
422 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
423 if (!bridge_channel->just_joined) {
426 bridge_channel_complete_join(bridge, bridge_channel);
430 /*! \brief Helper function used to find the "best" bridge technology given specified capabilities */
431 static struct ast_bridge_technology *find_best_technology(uint32_t capabilities, struct ast_bridge *bridge)
433 struct ast_bridge_technology *current;
434 struct ast_bridge_technology *best = NULL;
436 AST_RWLIST_RDLOCK(&bridge_technologies);
437 AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
438 if (current->suspended) {
439 ast_debug(1, "Bridge technology %s is suspended. Skipping.\n",
443 if (!(current->capabilities & capabilities)) {
444 ast_debug(1, "Bridge technology %s does not have any capabilities we want.\n",
448 if (best && current->preference <= best->preference) {
449 ast_debug(1, "Bridge technology %s has less preference than %s (%d <= %d). Skipping.\n",
450 current->name, best->name, current->preference, best->preference);
453 if (current->compatible && !current->compatible(bridge)) {
454 ast_debug(1, "Bridge technology %s is not compatible with properties of existing bridge.\n",
462 /* Increment it's module reference count if present so it does not get unloaded while in use */
463 ast_module_ref(best->mod);
464 ast_debug(1, "Chose bridge technology %s\n", best->name);
467 AST_RWLIST_UNLOCK(&bridge_technologies);
472 struct tech_deferred_destroy {
473 struct ast_bridge_technology *tech;
479 * \brief Deferred destruction of bridge tech private structure.
482 * \param bridge What to execute the action on.
483 * \param action Deferred bridge tech destruction.
485 * \note On entry, bridge must not be locked.
489 static void bridge_tech_deferred_destroy(struct ast_bridge *bridge, struct ast_frame *action)
491 struct tech_deferred_destroy *deferred = action->data.ptr;
492 struct ast_bridge dummy_bridge = {
493 .technology = deferred->tech,
494 .tech_pvt = deferred->tech_pvt,
497 ast_copy_string(dummy_bridge.uniqueid, bridge->uniqueid, sizeof(dummy_bridge.uniqueid));
498 ast_debug(1, "Bridge %s: calling %s technology destructor (deferred, dummy)\n",
499 dummy_bridge.uniqueid, dummy_bridge.technology->name);
500 dummy_bridge.technology->destroy(&dummy_bridge);
501 ast_module_unref(dummy_bridge.technology->mod);
506 * \brief Handle bridge action frame.
509 * \param bridge What to execute the action on.
510 * \param action What to do.
512 * \note On entry, bridge is already locked.
513 * \note Can be called by the bridge destructor.
517 static void bridge_action_bridge(struct ast_bridge *bridge, struct ast_frame *action)
519 #if 0 /* In case we need to know when the destructor is calling us. */
520 int in_destructor = !ao2_ref(bridge, 0);
523 switch (action->subclass.integer) {
524 case BRIDGE_CHANNEL_ACTION_DEFERRED_TECH_DESTROY:
525 ast_bridge_unlock(bridge);
526 bridge_tech_deferred_destroy(bridge, action);
527 ast_bridge_lock(bridge);
529 case BRIDGE_CHANNEL_ACTION_DEFERRED_DISSOLVING:
530 ast_bridge_unlock(bridge);
531 bridge->v_table->dissolving(bridge);
532 ast_bridge_lock(bridge);
535 /* Unexpected deferred action type. Should never happen. */
543 * \brief Do any pending bridge actions.
546 * \param bridge What to do actions on.
548 * \note On entry, bridge is already locked.
549 * \note Can be called by the bridge destructor.
553 static void bridge_handle_actions(struct ast_bridge *bridge)
555 struct ast_frame *action;
557 while ((action = AST_LIST_REMOVE_HEAD(&bridge->action_queue, frame_list))) {
558 switch (action->frametype) {
559 case AST_FRAME_BRIDGE_ACTION:
560 bridge_action_bridge(bridge, action);
563 /* Unexpected deferred frame type. Should never happen. */
571 static struct stasis_message *create_bridge_snapshot_message(struct ast_bridge *bridge)
573 RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup);
575 snapshot = ast_bridge_snapshot_create(bridge);
580 return stasis_message_create(ast_bridge_snapshot_type(), snapshot);
583 static void destroy_bridge(void *obj)
585 struct ast_bridge *bridge = obj;
587 ast_debug(1, "Bridge %s: actually destroying %s bridge, nobody wants it anymore\n",
588 bridge->uniqueid, bridge->v_table->name);
590 if (bridge->construction_completed) {
591 RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);
593 clear_msg = create_bridge_snapshot_message(bridge);
595 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
597 msg = stasis_cache_clear_create(clear_msg);
599 stasis_publish(ast_bridge_topic(bridge), msg);
604 /* Do any pending actions in the context of destruction. */
605 ast_bridge_lock(bridge);
606 bridge_handle_actions(bridge);
607 ast_bridge_unlock(bridge);
609 /* There should not be any channels left in the bridge. */
610 ast_assert(AST_LIST_EMPTY(&bridge->channels));
612 ast_debug(1, "Bridge %s: calling %s bridge destructor\n",
613 bridge->uniqueid, bridge->v_table->name);
614 bridge->v_table->destroy(bridge);
616 /* Pass off the bridge to the technology to destroy if needed */
617 if (bridge->technology) {
618 ast_debug(1, "Bridge %s: calling %s technology stop\n",
619 bridge->uniqueid, bridge->technology->name);
620 if (bridge->technology->stop) {
621 ast_bridge_lock(bridge);
622 bridge->technology->stop(bridge);
623 ast_bridge_unlock(bridge);
625 ast_debug(1, "Bridge %s: calling %s technology destructor\n",
626 bridge->uniqueid, bridge->technology->name);
627 if (bridge->technology->destroy) {
628 bridge->technology->destroy(bridge);
630 ast_module_unref(bridge->technology->mod);
631 bridge->technology = NULL;
634 if (bridge->callid) {
635 bridge->callid = ast_callid_unref(bridge->callid);
638 cleanup_video_mode(bridge);
640 stasis_cp_single_unsubscribe(bridge->topics);
643 struct ast_bridge *bridge_register(struct ast_bridge *bridge)
646 bridge->construction_completed = 1;
647 ast_bridge_publish_state(bridge);
648 if (!ao2_link(bridges, bridge)) {
649 ast_bridge_destroy(bridge);
656 struct ast_bridge *bridge_alloc(size_t size, const struct ast_bridge_methods *v_table)
658 struct ast_bridge *bridge;
660 /* Check v_table that all methods are present. */
664 || !v_table->dissolving
667 || !v_table->notify_masquerade
668 || !v_table->get_merge_priority) {
669 ast_log(LOG_ERROR, "Virtual method table for bridge class %s not complete.\n",
670 v_table && v_table->name ? v_table->name : "<unknown>");
675 bridge = ao2_alloc(size, destroy_bridge);
677 bridge->v_table = v_table;
682 struct ast_bridge *bridge_base_init(struct ast_bridge *self, uint32_t capabilities, unsigned int flags)
688 ast_uuid_generate_str(self->uniqueid, sizeof(self->uniqueid));
689 ast_set_flag(&self->feature_flags, flags);
690 self->allowed_capabilities = capabilities;
692 if (bridge_topics_init(self) != 0) {
693 ast_log(LOG_WARNING, "Bridge %s: Could not initialize topics\n",
699 /* Use our helper function to find the "best" bridge technology. */
700 self->technology = find_best_technology(capabilities, self);
701 if (!self->technology) {
702 ast_log(LOG_WARNING, "Bridge %s: Could not create class %s. No technology to support it.\n",
703 self->uniqueid, self->v_table->name);
708 /* Pass off the bridge to the technology to manipulate if needed */
709 ast_debug(1, "Bridge %s: calling %s technology constructor\n",
710 self->uniqueid, self->technology->name);
711 if (self->technology->create && self->technology->create(self)) {
712 ast_log(LOG_WARNING, "Bridge %s: failed to setup bridge technology %s\n",
713 self->uniqueid, self->technology->name);
717 ast_debug(1, "Bridge %s: calling %s technology start\n",
718 self->uniqueid, self->technology->name);
719 if (self->technology->start && self->technology->start(self)) {
720 ast_log(LOG_WARNING, "Bridge %s: failed to start bridge technology %s\n",
721 self->uniqueid, self->technology->name);
726 if (!ast_bridge_topic(self)) {
736 * \brief ast_bridge base class destructor.
739 * \param self Bridge to operate upon.
741 * \note Stub because of nothing to do.
745 static void bridge_base_destroy(struct ast_bridge *self)
751 * \brief The bridge is being dissolved.
754 * \param self Bridge to operate upon.
758 static void bridge_base_dissolving(struct ast_bridge *self)
760 ao2_unlink(bridges, self);
765 * \brief ast_bridge base push method.
768 * \param self Bridge to operate upon.
769 * \param bridge_channel Bridge channel to push.
770 * \param swap Bridge channel to swap places with if not NULL.
772 * \note On entry, self is already locked.
773 * \note Stub because of nothing to do.
775 * \retval 0 on success
776 * \retval -1 on failure
778 static int bridge_base_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
785 * \brief ast_bridge base pull method.
788 * \param self Bridge to operate upon.
789 * \param bridge_channel Bridge channel to pull.
791 * \note On entry, self is already locked.
795 static void bridge_base_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
797 ast_bridge_features_remove(bridge_channel->features, AST_BRIDGE_HOOK_REMOVE_ON_PULL);
802 * \brief ast_bridge base notify_masquerade method.
805 * \param self Bridge to operate upon.
806 * \param bridge_channel Bridge channel that was masqueraded.
808 * \note On entry, self is already locked.
812 static void bridge_base_notify_masquerade(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
814 self->reconfigured = 1;
819 * \brief Get the merge priority of this bridge.
822 * \param self Bridge to operate upon.
824 * \note On entry, self is already locked.
826 * \return Merge priority
828 static int bridge_base_get_merge_priority(struct ast_bridge *self)
833 struct ast_bridge_methods ast_bridge_base_v_table = {
835 .destroy = bridge_base_destroy,
836 .dissolving = bridge_base_dissolving,
837 .push = bridge_base_push,
838 .pull = bridge_base_pull,
839 .notify_masquerade = bridge_base_notify_masquerade,
840 .get_merge_priority = bridge_base_get_merge_priority,
843 struct ast_bridge *ast_bridge_base_new(uint32_t capabilities, unsigned int flags)
847 bridge = bridge_alloc(sizeof(struct ast_bridge), &ast_bridge_base_v_table);
848 bridge = bridge_base_init(bridge, capabilities, flags);
849 bridge = bridge_register(bridge);
853 int ast_bridge_destroy(struct ast_bridge *bridge)
855 ast_debug(1, "Bridge %s: telling all channels to leave the party\n", bridge->uniqueid);
856 ast_bridge_lock(bridge);
857 bridge_dissolve(bridge);
858 ast_bridge_unlock(bridge);
865 static int bridge_make_compatible(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
867 struct ast_format read_format;
868 struct ast_format write_format;
869 struct ast_format best_format;
872 ast_format_copy(&read_format, ast_channel_readformat(bridge_channel->chan));
873 ast_format_copy(&write_format, ast_channel_writeformat(bridge_channel->chan));
875 /* Are the formats currently in use something this bridge can handle? */
876 if (!ast_format_cap_iscompatible(bridge->technology->format_capabilities, ast_channel_readformat(bridge_channel->chan))) {
877 ast_best_codec(bridge->technology->format_capabilities, &best_format);
879 /* Read format is a no go... */
880 ast_debug(1, "Bridge technology %s wants to read any of formats %s but channel has %s\n",
881 bridge->technology->name,
882 ast_getformatname_multiple(codec_buf, sizeof(codec_buf), bridge->technology->format_capabilities),
883 ast_getformatname(&read_format));
885 /* Switch read format to the best one chosen */
886 if (ast_set_read_format(bridge_channel->chan, &best_format)) {
887 ast_log(LOG_WARNING, "Failed to set channel %s to read format %s\n",
888 ast_channel_name(bridge_channel->chan), ast_getformatname(&best_format));
891 ast_debug(1, "Bridge %s put channel %s into read format %s\n",
892 bridge->uniqueid, ast_channel_name(bridge_channel->chan),
893 ast_getformatname(&best_format));
895 ast_debug(1, "Bridge %s is happy that channel %s already has read format %s\n",
896 bridge->uniqueid, ast_channel_name(bridge_channel->chan),
897 ast_getformatname(&read_format));
900 if (!ast_format_cap_iscompatible(bridge->technology->format_capabilities, &write_format)) {
901 ast_best_codec(bridge->technology->format_capabilities, &best_format);
903 /* Write format is a no go... */
904 ast_debug(1, "Bridge technology %s wants to write any of formats %s but channel has %s\n",
905 bridge->technology->name,
906 ast_getformatname_multiple(codec_buf, sizeof(codec_buf), bridge->technology->format_capabilities),
907 ast_getformatname(&write_format));
909 /* Switch write format to the best one chosen */
910 if (ast_set_write_format(bridge_channel->chan, &best_format)) {
911 ast_log(LOG_WARNING, "Failed to set channel %s to write format %s\n",
912 ast_channel_name(bridge_channel->chan), ast_getformatname(&best_format));
915 ast_debug(1, "Bridge %s put channel %s into write format %s\n",
916 bridge->uniqueid, ast_channel_name(bridge_channel->chan),
917 ast_getformatname(&best_format));
919 ast_debug(1, "Bridge %s is happy that channel %s already has write format %s\n",
920 bridge->uniqueid, ast_channel_name(bridge_channel->chan),
921 ast_getformatname(&write_format));
929 * \brief Perform the smart bridge operation.
932 * \param bridge Work on this bridge.
935 * Basically see if a new bridge technology should be used instead
936 * of the current one.
938 * \note On entry, bridge is already locked.
940 * \retval 0 on success.
941 * \retval -1 on error.
943 static int smart_bridge_operation(struct ast_bridge *bridge)
945 uint32_t new_capabilities;
946 struct ast_bridge_technology *new_technology;
947 struct ast_bridge_technology *old_technology = bridge->technology;
948 struct ast_bridge_channel *bridge_channel;
949 struct ast_frame *deferred_action;
950 struct ast_bridge dummy_bridge = {
951 .technology = bridge->technology,
952 .tech_pvt = bridge->tech_pvt,
955 if (bridge->dissolved) {
956 ast_debug(1, "Bridge %s is dissolved, not performing smart bridge operation.\n",
961 /* Determine new bridge technology capabilities needed. */
962 if (2 < bridge->num_channels) {
963 new_capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX;
964 new_capabilities &= bridge->allowed_capabilities;
966 new_capabilities = AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_1TO1MIX;
967 new_capabilities &= bridge->allowed_capabilities;
968 if (!new_capabilities
969 && (bridge->allowed_capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX)) {
970 /* Allow switching between different multimix bridge technologies. */
971 new_capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX;
975 /* Find a bridge technology to satisfy the new capabilities. */
976 new_technology = find_best_technology(new_capabilities, bridge);
977 if (!new_technology) {
978 int is_compatible = 0;
980 if (old_technology->compatible) {
981 is_compatible = old_technology->compatible(bridge);
982 } else if (old_technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) {
984 } else if (bridge->num_channels <= 2
985 && (old_technology->capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX)) {
990 ast_debug(1, "Bridge %s could not get a new technology, staying with old technology.\n",
994 ast_log(LOG_WARNING, "Bridge %s has no technology available to support it.\n",
998 if (new_technology == old_technology) {
999 ast_debug(1, "Bridge %s is already using the new technology.\n",
1001 ast_module_unref(old_technology->mod);
1005 ast_copy_string(dummy_bridge.uniqueid, bridge->uniqueid, sizeof(dummy_bridge.uniqueid));
1007 if (old_technology->destroy) {
1008 struct tech_deferred_destroy deferred_tech_destroy = {
1009 .tech = dummy_bridge.technology,
1010 .tech_pvt = dummy_bridge.tech_pvt,
1012 struct ast_frame action = {
1013 .frametype = AST_FRAME_BRIDGE_ACTION,
1014 .subclass.integer = BRIDGE_CHANNEL_ACTION_DEFERRED_TECH_DESTROY,
1015 .data.ptr = &deferred_tech_destroy,
1016 .datalen = sizeof(deferred_tech_destroy),
1020 * We need to defer the bridge technology destroy callback
1021 * because we have the bridge locked.
1023 deferred_action = ast_frdup(&action);
1024 if (!deferred_action) {
1025 ast_module_unref(new_technology->mod);
1029 deferred_action = NULL;
1033 * We are now committed to changing the bridge technology. We
1034 * must not release the bridge lock until we have installed the
1035 * new bridge technology.
1037 ast_verb(4, "Bridge %s: switching from %s technology to %s\n",
1038 bridge->uniqueid, old_technology->name, new_technology->name);
1041 * Since we are soon going to pass this bridge to a new
1042 * technology we need to NULL out the tech_pvt pointer but
1043 * don't worry as it still exists in dummy_bridge, ditto for the
1046 bridge->tech_pvt = NULL;
1047 bridge->technology = new_technology;
1049 /* Setup the new bridge technology. */
1050 ast_debug(1, "Bridge %s: calling %s technology constructor\n",
1051 bridge->uniqueid, new_technology->name);
1052 if (new_technology->create && new_technology->create(bridge)) {
1053 ast_log(LOG_WARNING, "Bridge %s: failed to setup bridge technology %s\n",
1054 bridge->uniqueid, new_technology->name);
1055 bridge->tech_pvt = dummy_bridge.tech_pvt;
1056 bridge->technology = dummy_bridge.technology;
1057 ast_module_unref(new_technology->mod);
1061 ast_debug(1, "Bridge %s: calling %s technology stop\n",
1062 dummy_bridge.uniqueid, old_technology->name);
1063 if (old_technology->stop) {
1064 old_technology->stop(&dummy_bridge);
1068 * Move existing channels over to the new technology and
1069 * complete joining any new channels to the bridge.
1071 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1072 if (!bridge_channel->just_joined) {
1073 /* Take existing channel from the old technology. */
1074 ast_debug(1, "Bridge %s: %p(%s) is leaving %s technology (dummy)\n",
1075 dummy_bridge.uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
1076 old_technology->name);
1077 if (old_technology->leave) {
1078 old_technology->leave(&dummy_bridge, bridge_channel);
1082 /* Add any new channels or re-add an existing channel to the bridge. */
1083 bridge_channel_complete_join(bridge, bridge_channel);
1086 ast_debug(1, "Bridge %s: calling %s technology start\n",
1087 bridge->uniqueid, new_technology->name);
1088 if (new_technology->start && new_technology->start(bridge)) {
1089 ast_log(LOG_WARNING, "Bridge %s: failed to start bridge technology %s\n",
1090 bridge->uniqueid, new_technology->name);
1094 * Now that all the channels have been moved over we need to get
1095 * rid of all the information the old technology may have left
1098 if (old_technology->destroy) {
1099 ast_debug(1, "Bridge %s: deferring %s technology destructor\n",
1100 dummy_bridge.uniqueid, old_technology->name);
1101 bridge_queue_action_nodup(bridge, deferred_action);
1103 ast_debug(1, "Bridge %s: calling %s technology destructor\n",
1104 dummy_bridge.uniqueid, old_technology->name);
1105 ast_module_unref(old_technology->mod);
1113 * \brief Bridge channel to check if a BRIDGE_PLAY_SOUND needs to be played.
1116 * \param bridge_channel What to check.
1120 static void check_bridge_play_sound(struct ast_bridge_channel *bridge_channel)
1122 const char *play_file;
1124 ast_channel_lock(bridge_channel->chan);
1125 play_file = pbx_builtin_getvar_helper(bridge_channel->chan, "BRIDGE_PLAY_SOUND");
1126 if (!ast_strlen_zero(play_file)) {
1127 play_file = ast_strdupa(play_file);
1128 pbx_builtin_setvar_helper(bridge_channel->chan, "BRIDGE_PLAY_SOUND", NULL);
1132 ast_channel_unlock(bridge_channel->chan);
1135 ast_bridge_channel_queue_playfile(bridge_channel, NULL, play_file, NULL);
1141 * \brief Check for any BRIDGE_PLAY_SOUND channel variables in the bridge.
1144 * \param bridge What to operate on.
1146 * \note On entry, the bridge is already locked.
1150 static void check_bridge_play_sounds(struct ast_bridge *bridge)
1152 struct ast_bridge_channel *bridge_channel;
1154 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1155 check_bridge_play_sound(bridge_channel);
1159 static void update_bridge_vars_set(struct ast_channel *chan, const char *name, const char *pvtid)
1161 pbx_builtin_setvar_helper(chan, "BRIDGEPEER", name);
1162 pbx_builtin_setvar_helper(chan, "BRIDGEPVTCALLID", pvtid);
1167 * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in a 2 party bridge.
1170 * \param c0 Party of the first part.
1171 * \param c1 Party of the second part.
1173 * \note On entry, the bridge is already locked.
1174 * \note The bridge is expected to have exactly two parties.
1178 static void set_bridge_peer_vars_2party(struct ast_channel *c0, struct ast_channel *c1)
1180 const char *c0_name;
1181 const char *c1_name;
1182 const char *c0_pvtid = NULL;
1183 const char *c1_pvtid = NULL;
1184 #define UPDATE_BRIDGE_VARS_GET(chan, name, pvtid) \
1186 name = ast_strdupa(ast_channel_name(chan)); \
1187 if (ast_channel_tech(chan)->get_pvt_uniqueid) { \
1188 pvtid = ast_strdupa(ast_channel_tech(chan)->get_pvt_uniqueid(chan)); \
1192 ast_channel_lock(c1);
1193 UPDATE_BRIDGE_VARS_GET(c1, c1_name, c1_pvtid);
1194 ast_channel_unlock(c1);
1196 ast_channel_lock(c0);
1197 update_bridge_vars_set(c0, c1_name, c1_pvtid);
1198 UPDATE_BRIDGE_VARS_GET(c0, c0_name, c0_pvtid);
1199 ast_channel_unlock(c0);
1201 ast_channel_lock(c1);
1202 update_bridge_vars_set(c1, c0_name, c0_pvtid);
1203 ast_channel_unlock(c1);
1208 * \brief Fill the BRIDGEPEER value buffer with a comma separated list of channel names.
1211 * \param buf Buffer to fill. The caller must guarantee the buffer is large enough.
1212 * \param cur_idx Which index into names[] to skip.
1213 * \param names Channel names to put in the buffer.
1214 * \param num_names Number of names in the array.
1218 static void fill_bridgepeer_buf(char *buf, unsigned int cur_idx, const char *names[], unsigned int num_names)
1220 int need_separator = 0;
1226 for (idx = 0; idx < num_names; ++idx) {
1227 if (idx == cur_idx) {
1231 if (need_separator) {
1236 /* Copy name into buffer. */
1247 * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in a multi-party bridge.
1250 * \param bridge What to operate on.
1252 * \note On entry, the bridge is already locked.
1253 * \note The bridge is expected to have more than two parties.
1257 static void set_bridge_peer_vars_multiparty(struct ast_bridge *bridge)
1260 * Set a maximum number of channel names for the BRIDGEPEER
1261 * list. The plus one is for the current channel which is not
1264 #define MAX_BRIDGEPEER_CHANS (10 + 1)
1267 unsigned int num_names;
1271 struct ast_bridge_channel *bridge_channel;
1273 /* Get first MAX_BRIDGEPEER_CHANS channel names. */
1274 num_names = MIN(bridge->num_channels, MAX_BRIDGEPEER_CHANS);
1275 names = ast_alloca(num_names * sizeof(*names));
1277 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1278 if (num_names <= idx) {
1281 ast_channel_lock(bridge_channel->chan);
1282 names[idx++] = ast_strdupa(ast_channel_name(bridge_channel->chan));
1283 ast_channel_unlock(bridge_channel->chan);
1286 /* Determine maximum buf size needed. */
1288 for (idx = 0; idx < num_names; ++idx) {
1289 len += strlen(names[idx]);
1291 buf = ast_alloca(len);
1293 /* Set the bridge channel variables. */
1296 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1297 if (idx < num_names) {
1298 fill_bridgepeer_buf(buf, idx, names, num_names);
1302 ast_channel_lock(bridge_channel->chan);
1303 update_bridge_vars_set(bridge_channel->chan, buf, NULL);
1304 ast_channel_unlock(bridge_channel->chan);
1310 * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in a holding bridge.
1313 * \param bridge What to operate on.
1315 * \note On entry, the bridge is already locked.
1319 static void set_bridge_peer_vars_holding(struct ast_bridge *bridge)
1321 struct ast_bridge_channel *bridge_channel;
1323 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1324 ast_channel_lock(bridge_channel->chan);
1325 update_bridge_vars_set(bridge_channel->chan, NULL, NULL);
1326 ast_channel_unlock(bridge_channel->chan);
1332 * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in the bridge.
1335 * \param bridge What to operate on.
1337 * \note On entry, the bridge is already locked.
1341 static void set_bridge_peer_vars(struct ast_bridge *bridge)
1343 if (bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_HOLDING) {
1344 set_bridge_peer_vars_holding(bridge);
1347 if (bridge->num_channels < 2) {
1350 if (bridge->num_channels == 2) {
1351 set_bridge_peer_vars_2party(AST_LIST_FIRST(&bridge->channels)->chan,
1352 AST_LIST_LAST(&bridge->channels)->chan);
1354 set_bridge_peer_vars_multiparty(bridge);
1358 void bridge_reconfigured(struct ast_bridge *bridge, unsigned int colp_update)
1360 if (!bridge->reconfigured) {
1363 bridge->reconfigured = 0;
1364 if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_SMART)
1365 && smart_bridge_operation(bridge)) {
1366 /* Smart bridge failed. */
1367 bridge_dissolve(bridge);
1370 bridge_complete_join(bridge);
1372 if (bridge->dissolved) {
1375 check_bridge_play_sounds(bridge);
1376 set_bridge_peer_vars(bridge);
1377 ast_bridge_publish_state(bridge);
1380 bridge_reconfigured_connected_line_update(bridge);
1384 struct ast_bridge_channel *bridge_find_channel(struct ast_bridge *bridge, struct ast_channel *chan)
1386 struct ast_bridge_channel *bridge_channel;
1388 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1389 if (bridge_channel->chan == chan) {
1394 return bridge_channel;
1397 void ast_bridge_notify_masquerade(struct ast_channel *chan)
1399 struct ast_bridge_channel *bridge_channel;
1400 struct ast_bridge *bridge;
1402 /* Safely get the bridge_channel pointer for the chan. */
1403 ast_channel_lock(chan);
1404 bridge_channel = ast_channel_get_bridge_channel(chan);
1405 ast_channel_unlock(chan);
1406 if (!bridge_channel) {
1407 /* Not in a bridge */
1411 ast_bridge_channel_lock_bridge(bridge_channel);
1412 bridge = bridge_channel->bridge;
1413 if (bridge_channel == bridge_find_channel(bridge, chan)) {
1414 /* BUGBUG this needs more work. The channels need to be made compatible again if the formats change. The bridge_channel thread needs to monitor for this case. */
1415 /* The channel we want to notify is still in a bridge. */
1416 bridge->v_table->notify_masquerade(bridge, bridge_channel);
1417 bridge_reconfigured(bridge, 1);
1419 ast_bridge_unlock(bridge);
1420 ao2_ref(bridge_channel, -1);
1424 * BUGBUG make ast_bridge_join() require features to be allocated just like ast_bridge_impart() and not expect the struct back.
1426 * This change is really going to break ConfBridge. All other
1427 * users are easily changed. However, it is needed so the
1428 * bridging code can manipulate features on all channels
1429 * consistently no matter how they joined.
1431 * Need to update the features parameter doxygen when this
1432 * change is made to be like ast_bridge_impart().
1434 int ast_bridge_join(struct ast_bridge *bridge,
1435 struct ast_channel *chan,
1436 struct ast_channel *swap,
1437 struct ast_bridge_features *features,
1438 struct ast_bridge_tech_optimizations *tech_args,
1441 struct ast_bridge_channel *bridge_channel;
1444 bridge_channel = bridge_channel_internal_alloc(bridge);
1445 if (pass_reference) {
1446 ao2_ref(bridge, -1);
1448 if (!bridge_channel) {
1452 /* BUGBUG features cannot be NULL when passed in. When it is changed to allocated we can do like ast_bridge_impart() and allocate one. */
1453 ast_assert(features != NULL);
1455 ao2_ref(bridge_channel, -1);
1460 bridge_channel->tech_args = *tech_args;
1463 ast_channel_lock(chan);
1464 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)) {
1467 ast_channel_internal_bridge_channel_set(chan, bridge_channel);
1469 ast_channel_unlock(chan);
1470 bridge_channel->thread = pthread_self();
1471 bridge_channel->chan = chan;
1472 bridge_channel->swap = swap;
1473 bridge_channel->features = features;
1476 res = bridge_channel_internal_join(bridge_channel);
1479 /* Cleanup all the data in the bridge channel after it leaves the bridge. */
1480 ast_channel_lock(chan);
1481 ast_channel_internal_bridge_channel_set(chan, NULL);
1482 ast_channel_unlock(chan);
1483 bridge_channel->chan = NULL;
1484 bridge_channel->swap = NULL;
1485 bridge_channel->features = NULL;
1487 ao2_ref(bridge_channel, -1);
1490 ast_bridge_run_after_callback(chan);
1491 if (!(ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO)
1492 && !ast_bridge_setup_after_goto(chan)) {
1493 /* Claim the after bridge goto is an async goto destination. */
1494 ast_channel_lock(chan);
1495 ast_softhangup_nolock(chan, AST_SOFTHANGUP_ASYNCGOTO);
1496 ast_channel_unlock(chan);
1501 /*! \brief Thread responsible for imparted bridged channels to be departed */
1502 static void *bridge_channel_depart_thread(void *data)
1504 struct ast_bridge_channel *bridge_channel = data;
1506 if (bridge_channel->callid) {
1507 ast_callid_threadassoc_add(bridge_channel->callid);
1510 bridge_channel_internal_join(bridge_channel);
1513 bridge_channel->swap = NULL;
1514 ast_bridge_features_destroy(bridge_channel->features);
1515 bridge_channel->features = NULL;
1517 ast_bridge_discard_after_callback(bridge_channel->chan, AST_BRIDGE_AFTER_CB_REASON_DEPART);
1518 ast_bridge_discard_after_goto(bridge_channel->chan);
1523 /*! \brief Thread responsible for independent imparted bridged channels */
1524 static void *bridge_channel_ind_thread(void *data)
1526 struct ast_bridge_channel *bridge_channel = data;
1527 struct ast_channel *chan;
1529 if (bridge_channel->callid) {
1530 ast_callid_threadassoc_add(bridge_channel->callid);
1533 bridge_channel_internal_join(bridge_channel);
1534 chan = bridge_channel->chan;
1537 ast_channel_lock(chan);
1538 ast_channel_internal_bridge_channel_set(chan, NULL);
1539 ast_channel_unlock(chan);
1540 bridge_channel->chan = NULL;
1541 bridge_channel->swap = NULL;
1542 ast_bridge_features_destroy(bridge_channel->features);
1543 bridge_channel->features = NULL;
1545 ao2_ref(bridge_channel, -1);
1547 ast_bridge_run_after_callback(chan);
1548 ast_bridge_run_after_goto(chan);
1552 int ast_bridge_impart(struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap, struct ast_bridge_features *features, int independent)
1555 struct ast_bridge_channel *bridge_channel;
1557 /* Imparted channels cannot have a PBX. */
1558 if (ast_channel_pbx(chan)) {
1559 ast_log(AST_LOG_WARNING, "Channel %s has a PBX thread and cannot be imparted into bridge %s\n",
1560 ast_channel_name(chan), bridge->uniqueid);
1564 /* Supply an empty features structure if the caller did not. */
1566 features = ast_bridge_features_new();
1572 /* Try to allocate a structure for the bridge channel */
1573 bridge_channel = bridge_channel_internal_alloc(bridge);
1574 if (!bridge_channel) {
1575 ast_bridge_features_destroy(features);
1579 ast_channel_lock(chan);
1580 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)) {
1581 ast_log(AST_LOG_NOTICE, "Channel %s is a zombie and cannot be imparted into bridge %s\n",
1582 ast_channel_name(chan), bridge->uniqueid);
1585 ast_channel_internal_bridge_channel_set(chan, bridge_channel);
1587 ast_channel_unlock(chan);
1588 bridge_channel->chan = chan;
1589 bridge_channel->swap = swap;
1590 bridge_channel->features = features;
1591 bridge_channel->depart_wait = independent ? 0 : 1;
1592 bridge_channel->callid = ast_read_threadstorage_callid();
1594 /* Actually create the thread that will handle the channel */
1597 res = ast_pthread_create_detached(&bridge_channel->thread, NULL,
1598 bridge_channel_ind_thread, bridge_channel);
1600 res = ast_pthread_create(&bridge_channel->thread, NULL,
1601 bridge_channel_depart_thread, bridge_channel);
1607 ast_channel_lock(chan);
1608 ast_channel_internal_bridge_channel_set(chan, NULL);
1609 ast_channel_unlock(chan);
1610 bridge_channel->chan = NULL;
1611 bridge_channel->swap = NULL;
1612 ast_bridge_features_destroy(bridge_channel->features);
1613 bridge_channel->features = NULL;
1615 ao2_ref(bridge_channel, -1);
1622 int ast_bridge_depart(struct ast_channel *chan)
1624 struct ast_bridge_channel *bridge_channel;
1627 ast_channel_lock(chan);
1628 bridge_channel = ast_channel_internal_bridge_channel(chan);
1629 departable = bridge_channel && bridge_channel->depart_wait;
1630 ast_channel_unlock(chan);
1632 ast_log(LOG_ERROR, "Channel %s cannot be departed.\n",
1633 ast_channel_name(chan));
1635 * Should never happen. It likely means that
1636 * ast_bridge_depart() is called by two threads for the same
1637 * channel, the channel was never imparted to be departed, or it
1638 * has already been departed.
1645 * We are claiming the reference held by the depart bridge
1649 ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE);
1651 /* Wait for the depart thread to die */
1652 ast_debug(1, "Waiting for %p(%s) bridge thread to die.\n",
1653 bridge_channel, ast_channel_name(bridge_channel->chan));
1654 pthread_join(bridge_channel->thread, NULL);
1656 ast_channel_lock(chan);
1657 ast_channel_internal_bridge_channel_set(chan, NULL);
1658 ast_channel_unlock(chan);
1660 /* We can get rid of the bridge_channel after the depart thread has died. */
1661 ao2_ref(bridge_channel, -1);
1665 int ast_bridge_remove(struct ast_bridge *bridge, struct ast_channel *chan)
1667 struct ast_bridge_channel *bridge_channel;
1669 ast_bridge_lock(bridge);
1671 /* Try to find the channel that we want to remove */
1672 if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
1673 ast_bridge_unlock(bridge);
1677 ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE);
1679 ast_bridge_unlock(bridge);
1686 * \brief Point the bridge_channel to a new bridge.
1689 * \param bridge_channel What is to point to a new bridge.
1690 * \param new_bridge Where the bridge channel should point.
1694 static void bridge_channel_change_bridge(struct ast_bridge_channel *bridge_channel, struct ast_bridge *new_bridge)
1696 struct ast_bridge *old_bridge;
1698 ao2_ref(new_bridge, +1);
1699 ast_bridge_channel_lock(bridge_channel);
1700 ast_channel_lock(bridge_channel->chan);
1701 old_bridge = bridge_channel->bridge;
1702 bridge_channel->bridge = new_bridge;
1703 ast_channel_internal_bridge_set(bridge_channel->chan, new_bridge);
1704 ast_channel_unlock(bridge_channel->chan);
1705 ast_bridge_channel_unlock(bridge_channel);
1706 ao2_ref(old_bridge, -1);
1709 void bridge_do_merge(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, struct ast_bridge_channel **kick_me, unsigned int num_kick,
1710 unsigned int optimized)
1712 struct ast_bridge_channel *bridge_channel;
1715 ast_debug(1, "Merging bridge %s into bridge %s\n",
1716 src_bridge->uniqueid, dst_bridge->uniqueid);
1718 ast_bridge_publish_merge(dst_bridge, src_bridge);
1721 * Move channels from src_bridge over to dst_bridge.
1723 * We must use AST_LIST_TRAVERSE_SAFE_BEGIN() because
1724 * bridge_channel_internal_pull() alters the list we are traversing.
1726 AST_LIST_TRAVERSE_SAFE_BEGIN(&src_bridge->channels, bridge_channel, entry) {
1727 if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
1729 * The channel is already leaving let it leave normally because
1730 * pulling it may delete hooks that should run for this channel.
1734 if (ast_test_flag(&bridge_channel->features->feature_flags,
1735 AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE)) {
1740 for (idx = 0; idx < num_kick; ++idx) {
1741 if (bridge_channel == kick_me[idx]) {
1742 ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE);
1747 bridge_channel_internal_pull(bridge_channel);
1748 if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
1750 * The channel died as a result of being pulled or it was
1751 * kicked. Leave it pointing to the original bridge.
1756 /* Point to new bridge.*/
1757 bridge_channel_change_bridge(bridge_channel, dst_bridge);
1759 if (bridge_channel_internal_push(bridge_channel)) {
1760 ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE);
1763 AST_LIST_TRAVERSE_SAFE_END;
1767 * Now we can kick any channels in the dst_bridge without
1768 * potentially dissolving the bridge.
1770 for (idx = 0; idx < num_kick; ++idx) {
1771 bridge_channel = kick_me[idx];
1772 ast_bridge_channel_lock(bridge_channel);
1773 if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
1774 ast_bridge_channel_leave_bridge_nolock(bridge_channel, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE);
1775 bridge_channel_internal_pull(bridge_channel);
1777 ast_bridge_channel_unlock(bridge_channel);
1781 bridge_reconfigured(dst_bridge, !optimized);
1782 bridge_reconfigured(src_bridge, !optimized);
1784 ast_debug(1, "Merged bridge %s into bridge %s\n",
1785 src_bridge->uniqueid, dst_bridge->uniqueid);
1788 struct merge_direction {
1789 /*! Destination merge bridge. */
1790 struct ast_bridge *dest;
1791 /*! Source merge bridge. */
1792 struct ast_bridge *src;
1797 * \brief Determine which bridge should merge into the other.
1800 * \param bridge1 A bridge for merging
1801 * \param bridge2 A bridge for merging
1803 * \note The two bridges are assumed already locked.
1805 * \return Which bridge merges into which or NULL bridges if cannot merge.
1807 static struct merge_direction bridge_merge_determine_direction(struct ast_bridge *bridge1, struct ast_bridge *bridge2)
1809 struct merge_direction merge = { NULL, NULL };
1810 int bridge1_priority;
1811 int bridge2_priority;
1813 if (!ast_test_flag(&bridge1->feature_flags,
1814 AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)
1815 && !ast_test_flag(&bridge2->feature_flags,
1816 AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)) {
1818 * Can merge either way. Merge to the higher priority merge
1819 * bridge. Otherwise merge to the larger bridge.
1821 bridge1_priority = bridge1->v_table->get_merge_priority(bridge1);
1822 bridge2_priority = bridge2->v_table->get_merge_priority(bridge2);
1823 if (bridge2_priority < bridge1_priority) {
1824 merge.dest = bridge1;
1825 merge.src = bridge2;
1826 } else if (bridge1_priority < bridge2_priority) {
1827 merge.dest = bridge2;
1828 merge.src = bridge1;
1830 /* Merge to the larger bridge. */
1831 if (bridge2->num_channels <= bridge1->num_channels) {
1832 merge.dest = bridge1;
1833 merge.src = bridge2;
1835 merge.dest = bridge2;
1836 merge.src = bridge1;
1839 } else if (!ast_test_flag(&bridge1->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_TO)
1840 && !ast_test_flag(&bridge2->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)) {
1841 /* Can merge only one way. */
1842 merge.dest = bridge1;
1843 merge.src = bridge2;
1844 } else if (!ast_test_flag(&bridge2->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_TO)
1845 && !ast_test_flag(&bridge1->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)) {
1846 /* Can merge only one way. */
1847 merge.dest = bridge2;
1848 merge.src = bridge1;
1856 * \brief Merge two bridges together
1859 * \param dst_bridge Destination bridge of merge.
1860 * \param src_bridge Source bridge of merge.
1861 * \param merge_best_direction TRUE if don't care about which bridge merges into the other.
1862 * \param kick_me Array of channels to kick from the bridges.
1863 * \param num_kick Number of channels in the kick_me array.
1865 * \note The dst_bridge and src_bridge are assumed already locked.
1867 * \retval 0 on success
1868 * \retval -1 on failure
1870 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)
1872 struct merge_direction merge;
1873 struct ast_bridge_channel **kick_them = NULL;
1876 ast_assert(dst_bridge && src_bridge && dst_bridge != src_bridge && (!num_kick || kick_me));
1878 if (dst_bridge->dissolved || src_bridge->dissolved) {
1879 ast_debug(1, "Can't merge bridges %s and %s, at least one bridge is dissolved.\n",
1880 src_bridge->uniqueid, dst_bridge->uniqueid);
1883 if (ast_test_flag(&dst_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)
1884 || ast_test_flag(&src_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)) {
1885 ast_debug(1, "Can't merge bridges %s and %s, masquerade only.\n",
1886 src_bridge->uniqueid, dst_bridge->uniqueid);
1889 if (dst_bridge->inhibit_merge || src_bridge->inhibit_merge) {
1890 ast_debug(1, "Can't merge bridges %s and %s, merging temporarily inhibited.\n",
1891 src_bridge->uniqueid, dst_bridge->uniqueid);
1895 if (merge_best_direction) {
1896 merge = bridge_merge_determine_direction(dst_bridge, src_bridge);
1898 merge.dest = dst_bridge;
1899 merge.src = src_bridge;
1903 || ast_test_flag(&merge.dest->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_TO)
1904 || ast_test_flag(&merge.src->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)) {
1905 ast_debug(1, "Can't merge bridges %s and %s, merging inhibited.\n",
1906 src_bridge->uniqueid, dst_bridge->uniqueid);
1909 if (merge.src->num_channels < 2) {
1911 * For a two party bridge, a channel may be temporarily removed
1912 * from the source bridge or the initial bridge members have not
1915 ast_debug(1, "Can't merge bridge %s into bridge %s, not enough channels in source bridge.\n",
1916 merge.src->uniqueid, merge.dest->uniqueid);
1919 if (2 + num_kick < merge.dest->num_channels + merge.src->num_channels
1920 && !(merge.dest->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX)
1921 && (!ast_test_flag(&merge.dest->feature_flags, AST_BRIDGE_FLAG_SMART)
1922 || !(merge.dest->allowed_capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX))) {
1923 ast_debug(1, "Can't merge bridge %s into bridge %s, multimix is needed and it cannot be acquired.\n",
1924 merge.src->uniqueid, merge.dest->uniqueid);
1929 unsigned int num_to_kick = 0;
1932 kick_them = ast_alloca(num_kick * sizeof(*kick_them));
1933 for (idx = 0; idx < num_kick; ++idx) {
1934 kick_them[num_to_kick] = bridge_find_channel(merge.src, kick_me[idx]);
1935 if (!kick_them[num_to_kick]) {
1936 kick_them[num_to_kick] = bridge_find_channel(merge.dest, kick_me[idx]);
1938 if (kick_them[num_to_kick]) {
1943 if (num_to_kick != num_kick) {
1944 ast_debug(1, "Can't merge bridge %s into bridge %s, at least one kicked channel is not in either bridge.\n",
1945 merge.src->uniqueid, merge.dest->uniqueid);
1950 bridge_do_merge(merge.dest, merge.src, kick_them, num_kick, 0);
1954 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)
1959 ast_assert(dst_bridge && src_bridge);
1961 ast_bridge_lock_both(dst_bridge, src_bridge);
1962 res = bridge_merge_locked(dst_bridge, src_bridge, merge_best_direction, kick_me, num_kick);
1963 ast_bridge_unlock(src_bridge);
1964 ast_bridge_unlock(dst_bridge);
1968 int bridge_do_move(struct ast_bridge *dst_bridge, struct ast_bridge_channel *bridge_channel, int attempt_recovery,
1969 unsigned int optimized)
1971 struct ast_bridge *orig_bridge;
1975 if (bridge_channel->swap) {
1976 ast_debug(1, "Moving %p(%s) into bridge %s swapping with %s\n",
1977 bridge_channel, ast_channel_name(bridge_channel->chan), dst_bridge->uniqueid,
1978 ast_channel_name(bridge_channel->swap));
1980 ast_debug(1, "Moving %p(%s) into bridge %s\n",
1981 bridge_channel, ast_channel_name(bridge_channel->chan), dst_bridge->uniqueid);
1984 orig_bridge = bridge_channel->bridge;
1985 was_in_bridge = bridge_channel->in_bridge;
1987 bridge_channel_internal_pull(bridge_channel);
1988 if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
1990 * The channel died as a result of being pulled. Leave it
1991 * pointing to the original bridge.
1993 bridge_reconfigured(orig_bridge, 0);
1997 /* Point to new bridge.*/
1998 ao2_ref(orig_bridge, +1);/* Keep a ref in case the push fails. */
1999 bridge_channel_change_bridge(bridge_channel, dst_bridge);
2001 if (bridge_channel_internal_push(bridge_channel)) {
2002 /* Try to put the channel back into the original bridge. */
2003 if (attempt_recovery && was_in_bridge) {
2004 /* Point back to original bridge. */
2005 bridge_channel_change_bridge(bridge_channel, orig_bridge);
2007 if (bridge_channel_internal_push(bridge_channel)) {
2008 ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE);
2011 ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE);
2016 bridge_reconfigured(dst_bridge, !optimized);
2017 bridge_reconfigured(orig_bridge, !optimized);
2018 ao2_ref(orig_bridge, -1);
2024 * \brief Move a channel from one bridge to another.
2027 * \param dst_bridge Destination bridge of bridge channel move.
2028 * \param src_bridge Source bridge of bridge channel move.
2029 * \param chan Channel to move.
2030 * \param swap Channel to replace in dst_bridge.
2031 * \param attempt_recovery TRUE if failure attempts to push channel back into original bridge.
2033 * \note The dst_bridge and src_bridge are assumed already locked.
2035 * \retval 0 on success.
2036 * \retval -1 on failure.
2038 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)
2040 struct ast_bridge_channel *bridge_channel;
2042 if (dst_bridge->dissolved || src_bridge->dissolved) {
2043 ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, at least one bridge is dissolved.\n",
2044 ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
2047 if (ast_test_flag(&dst_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)
2048 || ast_test_flag(&src_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)) {
2049 ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, masquerade only.\n",
2050 ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
2053 if (dst_bridge->inhibit_merge || src_bridge->inhibit_merge) {
2054 ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, temporarily inhibited.\n",
2055 ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
2059 bridge_channel = bridge_find_channel(src_bridge, chan);
2060 if (!bridge_channel) {
2061 ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, channel not in bridge.\n",
2062 ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
2065 if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
2066 ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, channel leaving bridge.\n",
2067 ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
2070 if (ast_test_flag(&bridge_channel->features->feature_flags,
2071 AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE)) {
2072 ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, channel immovable.\n",
2073 ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
2078 struct ast_bridge_channel *bridge_channel_swap;
2080 bridge_channel_swap = bridge_find_channel(dst_bridge, swap);
2081 if (!bridge_channel_swap) {
2082 ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, swap channel %s not in bridge.\n",
2083 ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid,
2084 ast_channel_name(swap));
2087 if (bridge_channel_swap->state != BRIDGE_CHANNEL_STATE_WAIT) {
2088 ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, swap channel %s leaving bridge.\n",
2089 ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid,
2090 ast_channel_name(swap));
2095 bridge_channel->swap = swap;
2096 return bridge_do_move(dst_bridge, bridge_channel, attempt_recovery, 0);
2099 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)
2103 ast_bridge_lock_both(dst_bridge, src_bridge);
2104 res = bridge_move_locked(dst_bridge, src_bridge, chan, swap, attempt_recovery);
2105 ast_bridge_unlock(src_bridge);
2106 ast_bridge_unlock(dst_bridge);
2110 int ast_bridge_add_channel(struct ast_bridge *bridge, struct ast_channel *chan,
2111 struct ast_bridge_features *features, int play_tone, const char *xfersound)
2113 RAII_VAR(struct ast_bridge *, chan_bridge, NULL, ao2_cleanup);
2114 RAII_VAR(struct ast_channel *, yanked_chan, NULL, ao2_cleanup);
2116 ast_channel_lock(chan);
2117 chan_bridge = ast_channel_get_bridge(chan);
2118 ast_channel_unlock(chan);
2121 struct ast_bridge_channel *bridge_channel;
2123 ast_bridge_lock_both(bridge, chan_bridge);
2124 bridge_channel = bridge_find_channel(chan_bridge, chan);
2126 if (bridge_move_locked(bridge, chan_bridge, chan, NULL, 1)) {
2127 ast_bridge_unlock(chan_bridge);
2128 ast_bridge_unlock(bridge);
2133 * bridge_move_locked() will implicitly ensure that
2134 * bridge_channel is not NULL.
2136 ast_assert(bridge_channel != NULL);
2139 * Additional checks if the channel we just stole dissolves the
2142 bridge_dissolve_check_stolen(chan_bridge, bridge_channel);
2143 ast_bridge_unlock(chan_bridge);
2144 ast_bridge_unlock(bridge);
2146 /* The channel was in a bridge so it is not getting any new features. */
2147 ast_bridge_features_destroy(features);
2149 /* Slightly less easy case. We need to yank channel A from
2150 * where he currently is and impart him into our bridge.
2152 yanked_chan = ast_channel_yank(chan);
2154 ast_log(LOG_WARNING, "Could not gain control of channel %s\n", ast_channel_name(chan));
2157 if (ast_channel_state(yanked_chan) != AST_STATE_UP) {
2158 ast_answer(yanked_chan);
2160 ast_channel_ref(yanked_chan);
2161 if (ast_bridge_impart(bridge, yanked_chan, NULL, features, 1)) {
2162 /* It is possible for us to yank a channel and have some other
2163 * thread start a PBX on the channl after we yanked it. In particular,
2164 * this can theoretically happen on the ;2 of a Local channel if we
2165 * yank it prior to the ;1 being answered. Make sure that it isn't
2166 * executing a PBX before hanging it up.
2168 if (ast_channel_pbx(yanked_chan)) {
2169 ast_channel_unref(yanked_chan);
2171 ast_hangup(yanked_chan);
2177 if (play_tone && !ast_strlen_zero(xfersound)) {
2178 struct ast_channel *play_chan = yanked_chan ?: chan;
2179 RAII_VAR(struct ast_bridge_channel *, play_bridge_channel, NULL, ao2_cleanup);
2181 ast_channel_lock(play_chan);
2182 play_bridge_channel = ast_channel_get_bridge_channel(play_chan);
2183 ast_channel_unlock(play_chan);
2185 if (!play_bridge_channel) {
2186 ast_log(LOG_WARNING, "Unable to play tone for channel %s. No longer in a bridge.\n",
2187 ast_channel_name(play_chan));
2189 ast_bridge_channel_queue_playfile(play_bridge_channel, NULL, xfersound, NULL);
2195 static int bridge_allows_optimization(struct ast_bridge *bridge)
2197 return !(bridge->inhibit_merge
2198 || bridge->dissolved
2199 || ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY));
2204 * \brief Lock the unreal channel stack for chan and prequalify it.
2207 * \param chan Unreal channel writing a frame into the channel driver.
2209 * \note It is assumed that chan is already locked.
2211 * \retval bridge on success with bridge and bridge_channel locked.
2212 * \retval NULL if cannot do optimization now.
2214 static struct ast_bridge *optimize_lock_chan_stack(struct ast_channel *chan)
2216 struct ast_bridge *bridge;
2217 struct ast_bridge_channel *bridge_channel;
2219 if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
2222 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF)) {
2225 if (ast_channel_has_audio_frame_or_monitor(chan)) {
2226 /* Channel has an active monitor, audiohook, or framehook. */
2229 bridge_channel = ast_channel_internal_bridge_channel(chan);
2230 if (!bridge_channel || ast_bridge_channel_trylock(bridge_channel)) {
2233 bridge = bridge_channel->bridge;
2234 if (bridge_channel->activity != BRIDGE_CHANNEL_THREAD_SIMPLE
2235 || bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT
2236 || ast_bridge_trylock(bridge)) {
2237 ast_bridge_channel_unlock(bridge_channel);
2240 if (!bridge_channel_internal_allows_optimization(bridge_channel) ||
2241 !bridge_allows_optimization(bridge)) {
2242 ast_bridge_unlock(bridge);
2243 ast_bridge_channel_unlock(bridge_channel);
2251 * \brief Lock the unreal channel stack for peer and prequalify it.
2254 * \param peer Other unreal channel in the pair.
2256 * \retval bridge on success with bridge, bridge_channel, and peer locked.
2257 * \retval NULL if cannot do optimization now.
2259 static struct ast_bridge *optimize_lock_peer_stack(struct ast_channel *peer)
2261 struct ast_bridge *bridge;
2262 struct ast_bridge_channel *bridge_channel;
2264 if (ast_channel_trylock(peer)) {
2267 if (!AST_LIST_EMPTY(ast_channel_readq(peer))) {
2268 ast_channel_unlock(peer);
2271 if (ast_test_flag(ast_channel_flags(peer), AST_FLAG_EMULATE_DTMF)) {
2272 ast_channel_unlock(peer);
2275 if (ast_channel_has_audio_frame_or_monitor(peer)) {
2276 /* Peer has an active monitor, audiohook, or framehook. */
2277 ast_channel_unlock(peer);
2280 bridge_channel = ast_channel_internal_bridge_channel(peer);
2281 if (!bridge_channel || ast_bridge_channel_trylock(bridge_channel)) {
2282 ast_channel_unlock(peer);
2285 bridge = bridge_channel->bridge;
2286 if (bridge_channel->activity != BRIDGE_CHANNEL_THREAD_IDLE
2287 || bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT
2288 || ast_bridge_trylock(bridge)) {
2289 ast_bridge_channel_unlock(bridge_channel);
2290 ast_channel_unlock(peer);
2293 if (!bridge_allows_optimization(bridge) ||
2294 !bridge_channel_internal_allows_optimization(bridge_channel)) {
2295 ast_bridge_unlock(bridge);
2296 ast_bridge_channel_unlock(bridge_channel);
2297 ast_channel_unlock(peer);
2305 * \brief Indicates allowability of a swap optimization
2307 enum bridge_allow_swap {
2308 /*! Bridges cannot allow for a swap optimization to occur */
2310 /*! Bridge swap optimization can occur into the chan_bridge */
2311 SWAP_TO_CHAN_BRIDGE,
2312 /*! Bridge swap optimization can occur into the peer_bridge */
2313 SWAP_TO_PEER_BRIDGE,
2318 * \brief Determine if two bridges allow for swap optimization to occur
2320 * \param chan_bridge First bridge being tested
2321 * \param peer_bridge Second bridge being tested
2322 * \return Allowability of swap optimization
2324 static enum bridge_allow_swap bridges_allow_swap_optimization(struct ast_bridge *chan_bridge,
2325 struct ast_bridge *peer_bridge)
2330 if (!ast_test_flag(&chan_bridge->feature_flags,
2331 AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM |
2332 AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY)
2333 && !ast_test_flag(&peer_bridge->feature_flags,
2334 AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM |
2335 AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY)) {
2337 * Can swap either way. Swap to the higher priority merge
2340 chan_priority = chan_bridge->v_table->get_merge_priority(chan_bridge);
2341 peer_priority = peer_bridge->v_table->get_merge_priority(peer_bridge);
2342 if (chan_bridge->num_channels == 2
2343 && chan_priority <= peer_priority) {
2344 return SWAP_TO_PEER_BRIDGE;
2345 } else if (peer_bridge->num_channels == 2
2346 && peer_priority <= chan_priority) {
2347 return SWAP_TO_CHAN_BRIDGE;
2349 } else if (chan_bridge->num_channels == 2
2350 && !ast_test_flag(&chan_bridge->feature_flags, AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY)
2351 && !ast_test_flag(&peer_bridge->feature_flags, AST_BRIDGE_FLAG_SWAP_INHIBIT_TO)) {
2352 /* Can swap optimize only one way. */
2353 return SWAP_TO_PEER_BRIDGE;
2354 } else if (peer_bridge->num_channels == 2
2355 && !ast_test_flag(&peer_bridge->feature_flags, AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY)
2356 && !ast_test_flag(&chan_bridge->feature_flags, AST_BRIDGE_FLAG_SWAP_INHIBIT_TO)) {
2357 /* Can swap optimize only one way. */
2358 return SWAP_TO_CHAN_BRIDGE;
2361 return SWAP_PROHIBITED;
2366 * \brief Check and attempt to swap optimize out the unreal channels.
2369 * \param chan_bridge
2370 * \param chan_bridge_channel
2371 * \param peer_bridge
2372 * \param peer_bridge_channel
2373 * \param pvt Unreal data containing callbacks to call if the optimization actually
2376 * \retval 1 if unreal channels failed to optimize out.
2377 * \retval 0 if unreal channels were not optimized out.
2378 * \retval -1 if unreal channels were optimized out.
2380 static int try_swap_optimize_out(struct ast_bridge *chan_bridge,
2381 struct ast_bridge_channel *chan_bridge_channel, struct ast_bridge *peer_bridge,
2382 struct ast_bridge_channel *peer_bridge_channel,
2383 struct ast_unreal_pvt *pvt)
2385 struct ast_bridge *dst_bridge;
2386 struct ast_bridge_channel *dst_bridge_channel;
2387 struct ast_bridge_channel *src_bridge_channel;
2388 struct ast_bridge_channel *other;
2391 switch (bridges_allow_swap_optimization(chan_bridge, peer_bridge)) {
2392 case SWAP_TO_CHAN_BRIDGE:
2393 dst_bridge = chan_bridge;
2394 dst_bridge_channel = chan_bridge_channel;
2395 src_bridge_channel = peer_bridge_channel;
2397 case SWAP_TO_PEER_BRIDGE:
2398 dst_bridge = peer_bridge;
2399 dst_bridge_channel = peer_bridge_channel;
2400 src_bridge_channel = chan_bridge_channel;
2402 case SWAP_PROHIBITED:
2407 other = ast_bridge_channel_peer(src_bridge_channel);
2408 if (other && other->state == BRIDGE_CHANNEL_STATE_WAIT) {
2409 ast_verb(3, "Move-swap optimizing %s <-- %s.\n",
2410 ast_channel_name(dst_bridge_channel->chan),
2411 ast_channel_name(other->chan));
2413 if (pvt && !ast_test_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN) && pvt->callbacks
2414 && pvt->callbacks->optimization_started) {
2415 pvt->callbacks->optimization_started(pvt);
2416 ast_set_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN);
2418 other->swap = dst_bridge_channel->chan;
2419 if (!bridge_do_move(dst_bridge, other, 1, 1)) {
2420 ast_bridge_channel_leave_bridge(src_bridge_channel, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE);
2422 if (pvt && pvt->callbacks && pvt->callbacks->optimization_finished) {
2423 pvt->callbacks->optimization_finished(pvt);
2432 * \brief Indicates allowability of a merge optimization
2434 enum bridge_allow_merge {
2435 /*! Bridge properties prohibit merge optimization */
2437 /*! Merge optimization cannot occur because the source bridge has too few channels */
2438 MERGE_NOT_ENOUGH_CHANNELS,
2439 /*! Merge optimization cannot occur because multimix capability could not be requested */
2441 /*! Merge optimization allowed between bridges */
2447 * \brief Determines allowability of a merge optimization
2449 * \note The merge output parameter is undefined if MERGE_PROHIBITED is returned. For success
2450 * and other failure returns, a merge direction was determined, and the parameter is safe to
2453 * \param chan_bridge First bridge being tested
2454 * \param peer_bridge Second bridge being tested
2455 * \param num_kick_channels The number of channels to remove from the bridges during merging
2456 * \param[out] merge Indicates the recommended direction for the bridge merge
2458 static enum bridge_allow_merge bridges_allow_merge_optimization(struct ast_bridge *chan_bridge,
2459 struct ast_bridge *peer_bridge, int num_kick_channels, struct merge_direction *merge)
2461 *merge = bridge_merge_determine_direction(chan_bridge, peer_bridge);
2463 return MERGE_PROHIBITED;
2465 if (merge->src->num_channels < 2) {
2466 return MERGE_NOT_ENOUGH_CHANNELS;
2467 } else if ((2 + num_kick_channels) < merge->dest->num_channels + merge->src->num_channels
2468 && !(merge->dest->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX)
2469 && (!ast_test_flag(&merge->dest->feature_flags, AST_BRIDGE_FLAG_SMART)
2470 || !(merge->dest->allowed_capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX))) {
2471 return MERGE_NO_MULTIMIX;
2474 return MERGE_ALLOWED;
2479 * \brief Check and attempt to merge optimize out the unreal channels.
2482 * \param chan_bridge
2483 * \param chan_bridge_channel
2484 * \param peer_bridge
2485 * \param peer_bridge_channel
2486 * \param pvt Unreal data containing callbacks to call if the optimization actually
2489 * \retval 0 if unreal channels were not optimized out.
2490 * \retval -1 if unreal channels were optimized out.
2492 static int try_merge_optimize_out(struct ast_bridge *chan_bridge,
2493 struct ast_bridge_channel *chan_bridge_channel, struct ast_bridge *peer_bridge,
2494 struct ast_bridge_channel *peer_bridge_channel,
2495 struct ast_unreal_pvt *pvt)
2497 struct merge_direction merge;
2498 struct ast_bridge_channel *kick_me[] = {
2499 chan_bridge_channel,
2500 peer_bridge_channel,
2503 switch (bridges_allow_merge_optimization(chan_bridge, peer_bridge, ARRAY_LEN(kick_me), &merge)) {
2506 case MERGE_PROHIBITED:
2508 case MERGE_NOT_ENOUGH_CHANNELS:
2509 ast_debug(4, "Can't optimize %s -- %s out, not enough channels in bridge %s.\n",
2510 ast_channel_name(chan_bridge_channel->chan),
2511 ast_channel_name(peer_bridge_channel->chan),
2512 merge.src->uniqueid);
2514 case MERGE_NO_MULTIMIX:
2515 ast_debug(4, "Can't optimize %s -- %s out, multimix is needed and it cannot be acquired.\n",
2516 ast_channel_name(chan_bridge_channel->chan),
2517 ast_channel_name(peer_bridge_channel->chan));
2521 ast_verb(3, "Merge optimizing %s -- %s out.\n",
2522 ast_channel_name(chan_bridge_channel->chan),
2523 ast_channel_name(peer_bridge_channel->chan));
2525 if (pvt && !ast_test_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN) && pvt->callbacks
2526 && pvt->callbacks->optimization_started) {
2527 pvt->callbacks->optimization_started(pvt);
2528 ast_set_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN);
2530 bridge_do_merge(merge.dest, merge.src, kick_me, ARRAY_LEN(kick_me), 1);
2531 if (pvt && pvt->callbacks && pvt->callbacks->optimization_finished) {
2532 pvt->callbacks->optimization_finished(pvt);
2538 int ast_bridge_unreal_optimize_out(struct ast_channel *chan, struct ast_channel *peer, struct ast_unreal_pvt *pvt)
2540 struct ast_bridge *chan_bridge;
2541 struct ast_bridge *peer_bridge;
2542 struct ast_bridge_channel *chan_bridge_channel;
2543 struct ast_bridge_channel *peer_bridge_channel;
2546 chan_bridge = optimize_lock_chan_stack(chan);
2550 chan_bridge_channel = ast_channel_internal_bridge_channel(chan);
2552 peer_bridge = optimize_lock_peer_stack(peer);
2554 peer_bridge_channel = ast_channel_internal_bridge_channel(peer);
2556 res = try_swap_optimize_out(chan_bridge, chan_bridge_channel,
2557 peer_bridge, peer_bridge_channel, pvt);
2559 res = try_merge_optimize_out(chan_bridge, chan_bridge_channel,
2560 peer_bridge, peer_bridge_channel, pvt);
2561 } else if (0 < res) {
2565 /* Release peer locks. */
2566 ast_bridge_unlock(peer_bridge);
2567 ast_bridge_channel_unlock(peer_bridge_channel);
2568 ast_channel_unlock(peer);
2571 /* Release chan locks. */
2572 ast_bridge_unlock(chan_bridge);
2573 ast_bridge_channel_unlock(chan_bridge_channel);
2578 enum ast_bridge_optimization ast_bridges_allow_optimization(struct ast_bridge *chan_bridge,
2579 struct ast_bridge *peer_bridge)
2581 struct merge_direction merge;
2583 if (!bridge_allows_optimization(chan_bridge) || !bridge_allows_optimization(peer_bridge)) {
2584 return AST_BRIDGE_OPTIMIZE_PROHIBITED;
2587 switch (bridges_allow_swap_optimization(chan_bridge, peer_bridge)) {
2588 case SWAP_TO_CHAN_BRIDGE:
2589 return AST_BRIDGE_OPTIMIZE_SWAP_TO_CHAN_BRIDGE;
2590 case SWAP_TO_PEER_BRIDGE:
2591 return AST_BRIDGE_OPTIMIZE_SWAP_TO_PEER_BRIDGE;
2592 case SWAP_PROHIBITED:
2597 /* Two channels will be kicked from the bridges, the unreal;1 and unreal;2 channels */
2598 if (bridges_allow_merge_optimization(chan_bridge, peer_bridge, 2, &merge) != MERGE_ALLOWED) {
2599 return AST_BRIDGE_OPTIMIZE_PROHIBITED;
2602 if (merge.dest == chan_bridge) {
2603 return AST_BRIDGE_OPTIMIZE_MERGE_TO_CHAN_BRIDGE;
2605 return AST_BRIDGE_OPTIMIZE_MERGE_TO_PEER_BRIDGE;
2611 * \brief Adjust the bridge merge inhibit request count.
2614 * \param bridge What to operate on.
2615 * \param request Inhibit request increment.
2616 * (Positive to add requests. Negative to remove requests.)
2618 * \note This function assumes bridge is locked.
2622 void bridge_merge_inhibit_nolock(struct ast_bridge *bridge, int request)
2626 new_request = bridge->inhibit_merge + request;
2627 ast_assert(0 <= new_request);
2628 bridge->inhibit_merge = new_request;
2631 void ast_bridge_merge_inhibit(struct ast_bridge *bridge, int request)
2633 ast_bridge_lock(bridge);
2634 bridge_merge_inhibit_nolock(bridge, request);
2635 ast_bridge_unlock(bridge);
2638 int ast_bridge_suspend(struct ast_bridge *bridge, struct ast_channel *chan)
2640 struct ast_bridge_channel *bridge_channel;
2641 /* BUGBUG the case of a disolved bridge while channel is suspended is not handled. */
2642 /* BUGBUG suspend/unsuspend needs to be rethought. The caller must block until it has successfully suspended the channel for temporary control. */
2643 /* BUGBUG external suspend/unsuspend needs to be eliminated. The channel may be playing a file at the time and stealing it then is not good. */
2645 ast_bridge_lock(bridge);
2647 if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
2648 ast_bridge_unlock(bridge);
2652 bridge_channel_internal_suspend_nolock(bridge_channel);
2654 ast_bridge_unlock(bridge);
2659 int ast_bridge_unsuspend(struct ast_bridge *bridge, struct ast_channel *chan)
2661 struct ast_bridge_channel *bridge_channel;
2662 /* BUGBUG the case of a disolved bridge while channel is suspended is not handled. */
2664 ast_bridge_lock(bridge);
2666 if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
2667 ast_bridge_unlock(bridge);
2671 bridge_channel_internal_unsuspend_nolock(bridge_channel);
2673 ast_bridge_unlock(bridge);
2678 void ast_bridge_technology_suspend(struct ast_bridge_technology *technology)
2680 technology->suspended = 1;
2683 void ast_bridge_technology_unsuspend(struct ast_bridge_technology *technology)
2686 * XXX We may want the act of unsuspending a bridge technology
2687 * to prod all existing bridges to see if they should start
2690 technology->suspended = 0;
2693 int ast_bridge_features_register(enum ast_bridge_builtin_feature feature, ast_bridge_hook_callback callback, const char *dtmf)
2695 if (ARRAY_LEN(builtin_features_handlers) <= feature
2696 || builtin_features_handlers[feature]) {
2700 if (!ast_strlen_zero(dtmf)) {
2701 ast_copy_string(builtin_features_dtmf[feature], dtmf, sizeof(builtin_features_dtmf[feature]));
2704 builtin_features_handlers[feature] = callback;
2709 int ast_bridge_features_unregister(enum ast_bridge_builtin_feature feature)
2711 if (ARRAY_LEN(builtin_features_handlers) <= feature
2712 || !builtin_features_handlers[feature]) {
2716 builtin_features_handlers[feature] = NULL;
2721 int ast_bridge_features_do(enum ast_bridge_builtin_feature feature, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
2723 ast_bridge_hook_callback callback;
2725 if (ARRAY_LEN(builtin_features_handlers) <= feature) {
2729 callback = builtin_features_handlers[feature];
2733 callback(bridge_channel, hook_pvt);
2738 int ast_bridge_interval_register(enum ast_bridge_builtin_interval interval, ast_bridge_builtin_set_limits_fn callback)
2740 if (ARRAY_LEN(builtin_interval_handlers) <= interval
2741 || builtin_interval_handlers[interval]) {
2745 builtin_interval_handlers[interval] = callback;
2750 int ast_bridge_interval_unregister(enum ast_bridge_builtin_interval interval)
2752 if (ARRAY_LEN(builtin_interval_handlers) <= interval
2753 || !builtin_interval_handlers[interval]) {
2757 builtin_interval_handlers[interval] = NULL;
2765 * \brief Bridge hook destructor.
2768 * \param vhook Object to destroy.
2772 static void bridge_hook_destroy(void *vhook)
2774 struct ast_bridge_hook *hook = vhook;
2776 if (hook->destructor) {
2777 hook->destructor(hook->hook_pvt);
2783 * \brief Allocate and setup a generic bridge hook.
2786 * \param size How big an object to allocate.
2787 * \param callback Function to execute upon activation
2788 * \param hook_pvt Unique data
2789 * \param destructor Optional destructor callback for hook_pvt data
2790 * \param remove_flags Dictates what situations the hook should be removed.
2792 * \retval hook on success.
2793 * \retval NULL on error.
2795 static struct ast_bridge_hook *bridge_hook_generic(size_t size,
2796 ast_bridge_hook_callback callback,
2798 ast_bridge_hook_pvt_destructor destructor,
2799 enum ast_bridge_hook_remove_flags remove_flags)
2801 struct ast_bridge_hook *hook;
2803 /* Allocate new hook and setup it's basic variables */
2804 hook = ao2_alloc_options(size, bridge_hook_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
2806 hook->callback = callback;
2807 hook->destructor = destructor;
2808 hook->hook_pvt = hook_pvt;
2809 ast_set_flag(&hook->remove_flags, remove_flags);
2815 int ast_bridge_dtmf_hook(struct ast_bridge_features *features,
2817 ast_bridge_hook_callback callback,
2819 ast_bridge_hook_pvt_destructor destructor,
2820 enum ast_bridge_hook_remove_flags remove_flags)
2822 struct ast_bridge_hook_dtmf *hook;
2825 /* Allocate new hook and setup it's various variables */
2826 hook = (struct ast_bridge_hook_dtmf *) bridge_hook_generic(sizeof(*hook), callback,
2827 hook_pvt, destructor, remove_flags);
2831 hook->generic.type = AST_BRIDGE_HOOK_TYPE_DTMF;
2832 ast_copy_string(hook->dtmf.code, dtmf, sizeof(hook->dtmf.code));
2834 /* Once done we put it in the container. */
2835 res = ao2_link(features->dtmf_hooks, hook) ? 0 : -1;
2838 * Could not link the hook into the container.
2840 * Remove the hook_pvt destructor call from the hook since we
2841 * are returning failure to install the hook.
2843 hook->generic.destructor = NULL;
2852 * \brief Attach an other hook to a bridge features structure
2854 * \param features Bridge features structure
2855 * \param callback Function to execute upon activation
2856 * \param hook_pvt Unique data
2857 * \param destructor Optional destructor callback for hook_pvt data
2858 * \param remove_flags Dictates what situations the hook should be removed.
2859 * \param type What type of hook is being attached.
2861 * \retval 0 on success
2862 * \retval -1 on failure (The caller must cleanup any hook_pvt resources.)
2864 static int bridge_other_hook(struct ast_bridge_features *features,
2865 ast_bridge_hook_callback callback,
2867 ast_bridge_hook_pvt_destructor destructor,
2868 enum ast_bridge_hook_remove_flags remove_flags,
2869 enum ast_bridge_hook_type type)
2871 struct ast_bridge_hook *hook;
2874 /* Allocate new hook and setup it's various variables */
2875 hook = bridge_hook_generic(sizeof(*hook), callback, hook_pvt, destructor,
2882 /* Once done we put it in the container. */
2883 res = ao2_link(features->other_hooks, hook) ? 0 : -1;
2886 * Could not link the hook into the container.
2888 * Remove the hook_pvt destructor call from the hook since we
2889 * are returning failure to install the hook.
2891 hook->destructor = NULL;
2898 int ast_bridge_hangup_hook(struct ast_bridge_features *features,
2899 ast_bridge_hook_callback callback,
2901 ast_bridge_hook_pvt_destructor destructor,
2902 enum ast_bridge_hook_remove_flags remove_flags)
2904 return bridge_other_hook(features, callback, hook_pvt, destructor, remove_flags,
2905 AST_BRIDGE_HOOK_TYPE_HANGUP);
2908 int ast_bridge_join_hook(struct ast_bridge_features *features,
2909 ast_bridge_hook_callback callback,
2911 ast_bridge_hook_pvt_destructor destructor,
2912 enum ast_bridge_hook_remove_flags remove_flags)
2914 return bridge_other_hook(features, callback, hook_pvt, destructor, remove_flags,
2915 AST_BRIDGE_HOOK_TYPE_JOIN);
2918 int ast_bridge_leave_hook(struct ast_bridge_features *features,
2919 ast_bridge_hook_callback callback,
2921 ast_bridge_hook_pvt_destructor destructor,
2922 enum ast_bridge_hook_remove_flags remove_flags)
2924 return bridge_other_hook(features, callback, hook_pvt, destructor, remove_flags,
2925 AST_BRIDGE_HOOK_TYPE_LEAVE);
2928 int ast_bridge_talk_detector_hook(struct ast_bridge_features *features,
2929 ast_bridge_talking_indicate_callback callback,
2931 ast_bridge_hook_pvt_destructor destructor,
2932 enum ast_bridge_hook_remove_flags remove_flags)
2934 ast_bridge_hook_callback hook_cb = (ast_bridge_hook_callback) callback;
2936 return bridge_other_hook(features, hook_cb, hook_pvt, destructor, remove_flags,
2937 AST_BRIDGE_HOOK_TYPE_TALK);
2940 int ast_bridge_interval_hook(struct ast_bridge_features *features,
2941 unsigned int interval,
2942 ast_bridge_hook_callback callback,
2944 ast_bridge_hook_pvt_destructor destructor,
2945 enum ast_bridge_hook_remove_flags remove_flags)
2947 struct ast_bridge_hook_timer *hook;
2950 if (!features ||!interval || !callback) {
2954 /* Allocate new hook and setup it's various variables */
2955 hook = (struct ast_bridge_hook_timer *) bridge_hook_generic(sizeof(*hook), callback,
2956 hook_pvt, destructor, remove_flags);
2960 hook->generic.type = AST_BRIDGE_HOOK_TYPE_TIMER;
2961 hook->timer.interval = interval;
2962 hook->timer.trip_time = ast_tvadd(ast_tvnow(), ast_samp2tv(hook->timer.interval, 1000));
2963 hook->timer.seqno = ast_atomic_fetchadd_int((int *) &features->interval_sequence, +1);
2965 ast_debug(1, "Putting interval hook %p with interval %u in the heap on features %p\n",
2966 hook, hook->timer.interval, features);
2967 ast_heap_wrlock(features->interval_hooks);
2968 res = ast_heap_push(features->interval_hooks, hook);
2969 ast_heap_unlock(features->interval_hooks);
2972 * Could not push the hook into the heap
2974 * Remove the hook_pvt destructor call from the hook since we
2975 * are returning failure to install the hook.
2977 hook->generic.destructor = NULL;
2981 return res ? -1 : 0;
2984 int ast_bridge_features_enable(struct ast_bridge_features *features,
2985 enum ast_bridge_builtin_feature feature,
2988 ast_bridge_hook_pvt_destructor destructor,
2989 enum ast_bridge_hook_remove_flags remove_flags)
2991 if (ARRAY_LEN(builtin_features_handlers) <= feature
2992 || !builtin_features_handlers[feature]) {
2996 /* If no alternate DTMF stream was provided use the default one */
2997 if (ast_strlen_zero(dtmf)) {
2998 dtmf = builtin_features_dtmf[feature];
2999 /* If no DTMF is still available (ie: it has been disabled) then error out now */
3000 if (ast_strlen_zero(dtmf)) {
3001 ast_debug(1, "Failed to enable built in feature %d on %p, no DTMF string is available for it.\n",
3008 * The rest is basically pretty easy. We create another hook
3009 * using the built in feature's DTMF callback. Easy as pie.
3011 return ast_bridge_dtmf_hook(features, dtmf, builtin_features_handlers[feature],
3012 config, destructor, remove_flags);
3015 int ast_bridge_features_limits_construct(struct ast_bridge_features_limits *limits)
3017 memset(limits, 0, sizeof(*limits));
3019 if (ast_string_field_init(limits, 256)) {
3026 void ast_bridge_features_limits_destroy(struct ast_bridge_features_limits *limits)
3028 ast_string_field_free_memory(limits);
3031 int ast_bridge_features_set_limits(struct ast_bridge_features *features,
3032 struct ast_bridge_features_limits *limits,
3033 enum ast_bridge_hook_remove_flags remove_flags)
3035 if (builtin_interval_handlers[AST_BRIDGE_BUILTIN_INTERVAL_LIMITS]) {
3036 ast_bridge_builtin_set_limits_fn callback;
3038 callback = builtin_interval_handlers[AST_BRIDGE_BUILTIN_INTERVAL_LIMITS];
3039 return callback(features, limits, remove_flags);
3042 ast_log(LOG_ERROR, "Attempted to set limits without an AST_BRIDGE_BUILTIN_INTERVAL_LIMITS callback registered.\n");
3046 void ast_bridge_features_set_flag(struct ast_bridge_features *features, unsigned int flag)
3048 ast_set_flag(&features->feature_flags, flag);
3049 features->usable = 1;
3054 * \brief ao2 object match hooks with appropriate remove_flags.
3057 * \param obj Feature hook object.
3058 * \param arg Removal flags
3059 * \param flags Not used
3061 * \retval CMP_MATCH if hook's remove_flags match the removal flags set.
3062 * \retval 0 if not match.
3064 static int hook_remove_match(void *obj, void *arg, int flags)
3066 struct ast_bridge_hook *hook = obj;
3067 enum ast_bridge_hook_remove_flags *remove_flags = arg;
3069 if (ast_test_flag(&hook->remove_flags, *remove_flags)) {
3078 * \brief Remove all hooks with appropriate remove_flags in the container.
3081 * \param hooks Hooks container to work on.
3082 * \param remove_flags Determinator for whether hook is removed
3086 static void hooks_remove_container(struct ao2_container *hooks, enum ast_bridge_hook_remove_flags remove_flags)
3088 ao2_callback(hooks, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE,
3089 hook_remove_match, &remove_flags);
3094 * \brief Remove all hooks in the heap with appropriate remove_flags set.
3097 * \param hooks Hooks heap to work on.
3098 * \param remove_flags Determinator for whether hook is removed
3102 static void hooks_remove_heap(struct ast_heap *hooks, enum ast_bridge_hook_remove_flags remove_flags)
3104 struct ast_bridge_hook *hook;
3107 ast_heap_wrlock(hooks);
3112 for (idx = ast_heap_size(hooks); idx; --idx) {
3113 hook = ast_heap_peek(hooks, idx);
3114 if (ast_test_flag(&hook->remove_flags, remove_flags)) {
3115 ast_heap_remove(hooks, hook);
3121 ast_heap_unlock(hooks);
3124 void ast_bridge_features_remove(struct ast_bridge_features *features, enum ast_bridge_hook_remove_flags remove_flags)
3126 hooks_remove_container(features->dtmf_hooks, remove_flags);
3127 hooks_remove_container(features->other_hooks, remove_flags);
3128 hooks_remove_heap(features->interval_hooks, remove_flags);
3131 static int interval_hook_time_cmp(void *a, void *b)
3133 struct ast_bridge_hook_timer *hook_a = a;
3134 struct ast_bridge_hook_timer *hook_b = b;
3137 cmp = ast_tvcmp(hook_b->timer.trip_time, hook_a->timer.trip_time);
3142 cmp = hook_b->timer.seqno - hook_a->timer.seqno;
3148 * \brief DTMF hook container sort comparison function.
3151 * \param obj_left pointer to the (user-defined part) of an object.
3152 * \param obj_right pointer to the (user-defined part) of an object.
3153 * \param flags flags from ao2_callback()
3154 * OBJ_POINTER - if set, 'obj_right', is an object.
3155 * OBJ_KEY - if set, 'obj_right', is a search key item that is not an object.
3156 * OBJ_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
3158 * \retval <0 if obj_left < obj_right
3159 * \retval =0 if obj_left == obj_right
3160 * \retval >0 if obj_left > obj_right
3162 static int bridge_dtmf_hook_sort(const void *obj_left, const void *obj_right, int flags)
3164 const struct ast_bridge_hook_dtmf *hook_left = obj_left;
3165 const struct ast_bridge_hook_dtmf *hook_right = obj_right;
3166 const char *right_key = obj_right;
3169 switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
3172 right_key = hook_right->dtmf.code;
3175 cmp = strcasecmp(hook_left->dtmf.code, right_key);
3177 case OBJ_PARTIAL_KEY:
3178 cmp = strncasecmp(hook_left->dtmf.code, right_key, strlen(right_key));
3184 /* BUGBUG make ast_bridge_features_init() static when make ast_bridge_join() requires features to be allocated. */
3185 int ast_bridge_features_init(struct ast_bridge_features *features)
3187 /* Zero out the structure */
3188 memset(features, 0, sizeof(*features));
3190 /* Initialize the DTMF hooks container */
3191 features->dtmf_hooks = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX,
3192 AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE, bridge_dtmf_hook_sort, NULL);
3193 if (!features->dtmf_hooks) {
3197 /* Initialize the miscellaneous other hooks container */
3198 features->other_hooks = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL,
3200 if (!features->other_hooks) {
3204 /* Initialize the interval hooks heap */
3205 features->interval_hooks = ast_heap_create(8, interval_hook_time_cmp,
3206 offsetof(struct ast_bridge_hook_timer, timer.heap_index));
3207 if (!features->interval_hooks) {
3211 features->dtmf_passthrough = 1;
3216 /* BUGBUG make ast_bridge_features_cleanup() static when make ast_bridge_join() requires features to be allocated. */
3217 void ast_bridge_features_cleanup(struct ast_bridge_features *features)
3219 struct ast_bridge_hook_timer *hook;
3221 /* Destroy the interval hooks heap. */
3222 if (features->interval_hooks) {
3223 while ((hook = ast_heap_pop(features->interval_hooks))) {
3226 features->interval_hooks = ast_heap_destroy(features->interval_hooks);
3229 /* Destroy the miscellaneous other hooks container. */
3230 ao2_cleanup(features->other_hooks);
3231 features->other_hooks = NULL;
3233 /* Destroy the DTMF hooks container. */
3234 ao2_cleanup(features->dtmf_hooks);
3235 features->dtmf_hooks = NULL;
3238 void ast_bridge_features_destroy(struct ast_bridge_features *features)
3243 ast_bridge_features_cleanup(features);
3247 struct ast_bridge_features *ast_bridge_features_new(void)
3249 struct ast_bridge_features *features;
3251 features = ast_malloc(sizeof(*features));
3253 if (ast_bridge_features_init(features)) {
3254 ast_bridge_features_destroy(features);
3262 void ast_bridge_set_mixing_interval(struct ast_bridge *bridge, unsigned int mixing_interval)
3264 ast_bridge_lock(bridge);
3265 bridge->softmix.internal_mixing_interval = mixing_interval;
3266 ast_bridge_unlock(bridge);
3269 void ast_bridge_set_internal_sample_rate(struct ast_bridge *bridge, unsigned int sample_rate)
3271 ast_bridge_lock(bridge);
3272 bridge->softmix.internal_sample_rate = sample_rate;
3273 ast_bridge_unlock(bridge);
3276 static void cleanup_video_mode(struct ast_bridge *bridge)
3278 switch (bridge->softmix.video_mode.mode) {
3279 case AST_BRIDGE_VIDEO_MODE_NONE:
3281 case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
3282 if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc) {
3283 ast_channel_unref(bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc);
3286 case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
3287 if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc) {
3288 ast_channel_unref(bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc);
3290 if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc) {
3291 ast_channel_unref(bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc);
3294 memset(&bridge->softmix.video_mode, 0, sizeof(bridge->softmix.video_mode));
3297 void ast_bridge_set_single_src_video_mode(struct ast_bridge *bridge, struct ast_channel *video_src_chan)
3299 ast_bridge_lock(bridge);
3300 cleanup_video_mode(bridge);
3301 bridge->softmix.video_mode.mode = AST_BRIDGE_VIDEO_MODE_SINGLE_SRC;
3302 bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc = ast_channel_ref(video_src_chan);
3303 ast_test_suite_event_notify("BRIDGE_VIDEO_MODE", "Message: video mode set to single source\r\nVideo Mode: %d\r\nVideo Channel: %s",
3304 bridge->softmix.video_mode.mode, ast_channel_name(video_src_chan));
3305 ast_indicate(video_src_chan, AST_CONTROL_VIDUPDATE);
3306 ast_bridge_unlock(bridge);
3309 void ast_bridge_set_talker_src_video_mode(struct ast_bridge *bridge)
3311 ast_bridge_lock(bridge);
3312 cleanup_video_mode(bridge);
3313 bridge->softmix.video_mode.mode = AST_BRIDGE_VIDEO_MODE_TALKER_SRC;
3314 ast_test_suite_event_notify("BRIDGE_VIDEO_MODE", "Message: video mode set to talker source\r\nVideo Mode: %d",
3315 bridge->softmix.video_mode.mode);
3316 ast_bridge_unlock(bridge);
3319 void ast_bridge_update_talker_src_video_mode(struct ast_bridge *bridge, struct ast_channel *chan, int talker_energy, int is_keyframe)
3321 struct ast_bridge_video_talker_src_data *data;
3323 /* If the channel doesn't support video, we don't care about it */
3324 if (!ast_format_cap_has_type(ast_channel_nativeformats(chan), AST_FORMAT_TYPE_VIDEO)) {
3328 ast_bridge_lock(bridge);
3329 data = &bridge->softmix.video_mode.mode_data.talker_src_data;
3331 if (data->chan_vsrc == chan) {
3332 data->average_talking_energy = talker_energy;
3333 } else if ((data->average_talking_energy < talker_energy) && is_keyframe) {
3334 if (data->chan_old_vsrc) {
3335 ast_channel_unref(data->chan_old_vsrc);
3337 if (data->chan_vsrc) {
3338 data->chan_old_vsrc = data->chan_vsrc;
3339 ast_indicate(data->chan_old_vsrc, AST_CONTROL_VIDUPDATE);
3341 data->chan_vsrc = ast_channel_ref(chan);
3342 data->average_talking_energy = talker_energy;
3343 ast_test_suite_event_notify("BRIDGE_VIDEO_SRC", "Message: video source updated\r\nVideo Channel: %s", ast_channel_name(data->chan_vsrc));
3344 ast_indicate(data->chan_vsrc, AST_CONTROL_VIDUPDATE);
3345 } else if ((data->average_talking_energy < talker_energy) && !is_keyframe) {
3346 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
3347 } else if (!data->chan_vsrc && is_keyframe) {
3348 data->chan_vsrc = ast_channel_ref(chan);
3349 data->average_talking_energy = talker_energy;
3350 ast_test_suite_event_notify("BRIDGE_VIDEO_SRC", "Message: video source updated\r\nVideo Channel: %s", ast_channel_name(data->chan_vsrc));
3351 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
3352 } else if (!data->chan_old_vsrc && is_keyframe) {
3353 data->chan_old_vsrc = ast_channel_ref(chan);
3354 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
3356 ast_bridge_unlock(bridge);
3359 int ast_bridge_number_video_src(struct ast_bridge *bridge)
3363 ast_bridge_lock(bridge);
3364 switch (bridge->softmix.video_mode.mode) {
3365 case AST_BRIDGE_VIDEO_MODE_NONE:
3367 case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
3368 if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc) {
3372 case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
3373 if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc) {
3376 if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc) {
3380 ast_bridge_unlock(bridge);
3384 int ast_bridge_is_video_src(struct ast_bridge *bridge, struct ast_channel *chan)
3388 ast_bridge_lock(bridge);
3389 switch (bridge->softmix.video_mode.mode) {
3390 case AST_BRIDGE_VIDEO_MODE_NONE:
3392 case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
3393 if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc == chan) {
3397 case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
3398 if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc == chan) {
3400 } else if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc == chan) {
3405 ast_bridge_unlock(bridge);
3409 void ast_bridge_remove_video_src(struct ast_bridge *bridge, struct ast_channel *chan)