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.
21 * \brief Channel Bridging API
23 * \author Joshua Colp <jcolp@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include "asterisk/logger.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/options.h"
39 #include "asterisk/utils.h"
40 #include "asterisk/lock.h"
41 #include "asterisk/linkedlists.h"
42 #include "asterisk/bridging.h"
43 #include "asterisk/bridging_technology.h"
44 #include "asterisk/app.h"
45 #include "asterisk/file.h"
46 #include "asterisk/module.h"
47 #include "asterisk/astobj2.h"
48 #include "asterisk/test.h"
50 static AST_RWLIST_HEAD_STATIC(bridge_technologies, ast_bridge_technology);
52 /* Initial starting point for the bridge array of channels */
53 #define BRIDGE_ARRAY_START 128
55 /* Grow rate of bridge array of channels */
56 #define BRIDGE_ARRAY_GROW 32
58 static void cleanup_video_mode(struct ast_bridge *bridge);
60 /*! Default DTMF keys for built in features */
61 static char builtin_features_dtmf[AST_BRIDGE_BUILTIN_END][MAXIMUM_DTMF_FEATURE_STRING];
63 /*! Function handlers for the built in features */
64 static void *builtin_features_handlers[AST_BRIDGE_BUILTIN_END];
66 int __ast_bridge_technology_register(struct ast_bridge_technology *technology, struct ast_module *module)
68 struct ast_bridge_technology *current = NULL;
70 /* Perform a sanity check to make sure the bridge technology conforms to our needed requirements */
71 if (ast_strlen_zero(technology->name) || !technology->capabilities || !technology->write) {
72 ast_log(LOG_WARNING, "Bridge technology %s failed registration sanity check.\n", technology->name);
76 AST_RWLIST_WRLOCK(&bridge_technologies);
78 /* Look for duplicate bridge technology already using this name, or already registered */
79 AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
80 if ((!strcasecmp(current->name, technology->name)) || (current == technology)) {
81 ast_log(LOG_WARNING, "A bridge technology of %s already claims to exist in our world.\n", technology->name);
82 AST_RWLIST_UNLOCK(&bridge_technologies);
87 /* Copy module pointer so reference counting can keep the module from unloading */
88 technology->mod = module;
90 /* Insert our new bridge technology into the list and print out a pretty message */
91 AST_RWLIST_INSERT_TAIL(&bridge_technologies, technology, entry);
93 AST_RWLIST_UNLOCK(&bridge_technologies);
95 ast_verb(2, "Registered bridge technology %s\n", technology->name);
100 int ast_bridge_technology_unregister(struct ast_bridge_technology *technology)
102 struct ast_bridge_technology *current = NULL;
104 AST_RWLIST_WRLOCK(&bridge_technologies);
106 /* Ensure the bridge technology is registered before removing it */
107 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&bridge_technologies, current, entry) {
108 if (current == technology) {
109 AST_RWLIST_REMOVE_CURRENT(entry);
110 ast_verb(2, "Unregistered bridge technology %s\n", technology->name);
114 AST_RWLIST_TRAVERSE_SAFE_END;
116 AST_RWLIST_UNLOCK(&bridge_technologies);
118 return current ? 0 : -1;
121 void ast_bridge_change_state(struct ast_bridge_channel *bridge_channel, enum ast_bridge_channel_state new_state)
123 /* Change the state on the bridge channel */
124 bridge_channel->state = new_state;
126 /* Only poke the channel's thread if it is not us */
127 if (!pthread_equal(pthread_self(), bridge_channel->thread)) {
128 pthread_kill(bridge_channel->thread, SIGURG);
129 ao2_lock(bridge_channel);
130 ast_cond_signal(&bridge_channel->cond);
131 ao2_unlock(bridge_channel);
137 /*! \brief Helper function to poke the bridge thread */
138 static void bridge_poke(struct ast_bridge *bridge)
140 /* Poke the thread just in case */
141 if (bridge->thread != AST_PTHREADT_NULL && bridge->thread != AST_PTHREADT_STOP) {
142 pthread_kill(bridge->thread, SIGURG);
148 /*! \brief Helper function to add a channel to the bridge array
150 * \note This function assumes the bridge is locked.
152 static void bridge_array_add(struct ast_bridge *bridge, struct ast_channel *chan)
154 /* We have to make sure the bridge thread is not using the bridge array before messing with it */
155 while (bridge->waiting) {
160 bridge->array[bridge->array_num++] = chan;
162 ast_debug(1, "Added channel %s(%p) to bridge array on %p, new count is %d\n", ast_channel_name(chan), chan, bridge, (int)bridge->array_num);
164 /* If the next addition of a channel will exceed our array size grow it out */
165 if (bridge->array_num == bridge->array_size) {
166 struct ast_channel **tmp;
167 ast_debug(1, "Growing bridge array on %p from %d to %d\n", bridge, (int)bridge->array_size, (int)bridge->array_size + BRIDGE_ARRAY_GROW);
168 if (!(tmp = ast_realloc(bridge->array, (bridge->array_size + BRIDGE_ARRAY_GROW) * sizeof(struct ast_channel *)))) {
169 ast_log(LOG_ERROR, "Failed to allocate more space for another channel on bridge '%p', this is not going to end well\n", bridge);
173 bridge->array_size += BRIDGE_ARRAY_GROW;
179 /*! \brief Helper function to remove a channel from the bridge array
181 * \note This function assumes the bridge is locked.
183 static void bridge_array_remove(struct ast_bridge *bridge, struct ast_channel *chan)
187 /* We have to make sure the bridge thread is not using the bridge array before messing with it */
188 while (bridge->waiting) {
193 for (i = 0; i < bridge->array_num; i++) {
194 if (bridge->array[i] == chan) {
195 bridge->array[i] = (bridge->array[(bridge->array_num - 1)] != chan ? bridge->array[(bridge->array_num - 1)] : NULL);
196 bridge->array[(bridge->array_num - 1)] = NULL;
198 ast_debug(1, "Removed channel %p from bridge array on %p, new count is %d\n", chan, bridge, (int)bridge->array_num);
206 /*! \brief Helper function to find a bridge channel given a channel */
207 static struct ast_bridge_channel *find_bridge_channel(struct ast_bridge *bridge, struct ast_channel *chan)
209 struct ast_bridge_channel *bridge_channel = NULL;
211 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
212 if (bridge_channel->chan == chan) {
217 return bridge_channel;
220 /*! \brief Internal function to see whether a bridge should dissolve, and if so do it */
221 static void bridge_check_dissolve(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
223 struct ast_bridge_channel *bridge_channel2 = NULL;
225 if (!ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE) && (!bridge_channel->features || !bridge_channel->features->usable || !ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_FLAG_DISSOLVE))) {
229 ast_debug(1, "Dissolving bridge %p\n", bridge);
231 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel2, entry) {
232 if (bridge_channel2->state != AST_BRIDGE_CHANNEL_STATE_END && bridge_channel2->state != AST_BRIDGE_CHANNEL_STATE_DEPART) {
233 ast_bridge_change_state(bridge_channel2, AST_BRIDGE_CHANNEL_STATE_HANGUP);
237 /* Since all the channels are going away let's go ahead and stop our on thread */
243 /*! \brief Internal function to handle DTMF from a channel */
244 static struct ast_frame *bridge_handle_dtmf(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
246 struct ast_bridge_features *features = (bridge_channel->features ? bridge_channel->features : &bridge->features);
247 struct ast_bridge_features_hook *hook = NULL;
249 /* If the features structure we grabbed is not usable immediately return the frame */
250 if (!features->usable) {
254 /* See if this DTMF matches the beginnings of any feature hooks, if so we switch to the feature state to either execute the feature or collect more DTMF */
255 AST_LIST_TRAVERSE(&features->hooks, hook, entry) {
256 if (hook->dtmf[0] == frame->subclass.integer) {
259 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_FEATURE);
267 /*! \brief Internal function used to determine whether a control frame should be dropped or not */
268 static int bridge_drop_control_frame(int subclass)
271 case AST_CONTROL_ANSWER:
279 void ast_bridge_notify_talking(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, int started_talking)
281 if (started_talking) {
282 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_START_TALKING);
284 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_STOP_TALKING);
288 void ast_bridge_handle_trip(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_channel *chan, int outfd)
290 /* If no bridge channel has been provided and the actual channel has been provided find it */
291 if (chan && !bridge_channel) {
292 bridge_channel = find_bridge_channel(bridge, chan);
295 /* If a bridge channel with actual channel is present read a frame and handle it */
296 if (chan && bridge_channel) {
297 struct ast_frame *frame = (((bridge->features.mute) || (bridge_channel->features && bridge_channel->features->mute)) ? ast_read_noaudio(chan) : ast_read(chan));
299 /* This is pretty simple... see if they hung up */
300 if (!frame || (frame->frametype == AST_FRAME_CONTROL && frame->subclass.integer == AST_CONTROL_HANGUP)) {
301 /* Signal the thread that is handling the bridged channel that it should be ended */
302 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
303 } else if (frame->frametype == AST_FRAME_CONTROL && bridge_drop_control_frame(frame->subclass.integer)) {
304 ast_debug(1, "Dropping control frame from bridge channel %p\n", bridge_channel);
305 } else if (frame->frametype == AST_FRAME_DTMF_BEGIN || frame->frametype == AST_FRAME_DTMF_END) {
306 int dtmf_passthrough = bridge_channel->features ?
307 bridge_channel->features->dtmf_passthrough :
308 bridge->features.dtmf_passthrough;
310 if (frame->frametype == AST_FRAME_DTMF_BEGIN) {
311 frame = bridge_handle_dtmf(bridge, bridge_channel, frame);
314 if (frame && dtmf_passthrough) {
315 bridge->technology->write(bridge, bridge_channel, frame);
318 /* Simply write the frame out to the bridge technology if it still exists */
319 bridge->technology->write(bridge, bridge_channel, frame);
328 /* If a file descriptor actually tripped pass it off to the bridge technology */
329 if (outfd > -1 && bridge->technology->fd) {
330 bridge->technology->fd(bridge, bridge_channel, outfd);
334 /* If all else fails just poke the bridge */
335 if (bridge->technology->poke && bridge_channel) {
336 bridge->technology->poke(bridge, bridge_channel);
343 /*! \brief Generic thread loop, TODO: Rethink this/improve it */
344 static int generic_thread_loop(struct ast_bridge *bridge)
346 while (!bridge->stop && !bridge->refresh && bridge->array_num) {
347 struct ast_channel *winner = NULL;
350 /* Move channels around for priority reasons if we have more than one channel in our array */
351 if (bridge->array_num > 1) {
352 struct ast_channel *first = bridge->array[0];
353 memmove(bridge->array, bridge->array + 1, sizeof(struct ast_channel *) * (bridge->array_num - 1));
354 bridge->array[(bridge->array_num - 1)] = first;
357 /* Wait on the channels */
360 winner = ast_waitfor_n(bridge->array, (int)bridge->array_num, &to);
364 /* Process whatever they did */
365 ast_bridge_handle_trip(bridge, NULL, winner, -1);
371 /*! \brief Bridge thread function */
372 static void *bridge_thread(void *data)
374 struct ast_bridge *bridge = data;
379 if (bridge->callid) {
380 ast_callid_threadassoc_add(bridge->callid);
383 ast_debug(1, "Started bridge thread for %p\n", bridge);
385 /* Loop around until we are told to stop */
386 while (!bridge->stop && bridge->array_num && !res) {
387 /* In case the refresh bit was set simply set it back to off */
390 ast_debug(1, "Launching bridge thread function %p for bridge %p\n", (bridge->technology->thread ? bridge->technology->thread : &generic_thread_loop), bridge);
392 /* Execute the appropriate thread function. If the technology does not provide one we use the generic one */
393 res = (bridge->technology->thread ? bridge->technology->thread(bridge) : generic_thread_loop(bridge));
396 ast_debug(1, "Ending bridge thread for %p\n", bridge);
398 /* Indicate the bridge thread is no longer active */
399 bridge->thread = AST_PTHREADT_NULL;
407 /*! \brief Helper function used to find the "best" bridge technology given a specified capabilities */
408 static struct ast_bridge_technology *find_best_technology(uint32_t capabilities)
410 struct ast_bridge_technology *current = NULL, *best = NULL;
412 AST_RWLIST_RDLOCK(&bridge_technologies);
413 AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
414 if (current->suspended) {
415 ast_debug(1, "Bridge technology %s is suspended. Skipping.\n", current->name);
418 if (!(current->capabilities & capabilities)) {
419 ast_debug(1, "Bridge technology %s does not have the capabilities we need.\n", current->name);
422 if (best && best->preference < current->preference) {
423 ast_debug(1, "Bridge technology %s has preference %d while %s has preference %d. Skipping.\n", current->name, current->preference, best->name, best->preference);
430 /* Increment it's module reference count if present so it does not get unloaded while in use */
432 ast_module_ref(best->mod);
434 ast_debug(1, "Chose bridge technology %s\n", best->name);
437 AST_RWLIST_UNLOCK(&bridge_technologies);
442 static void destroy_bridge(void *obj)
444 struct ast_bridge *bridge = obj;
446 ast_debug(1, "Actually destroying bridge %p, nobody wants it anymore\n", bridge);
448 /* Pass off the bridge to the technology to destroy if needed */
449 if (bridge->technology->destroy) {
450 ast_debug(1, "Giving bridge technology %s the bridge structure %p to destroy\n", bridge->technology->name, bridge);
451 if (bridge->technology->destroy(bridge)) {
452 ast_debug(1, "Bridge technology %s failed to destroy bridge structure %p... trying our best\n", bridge->technology->name, bridge);
456 /* We are no longer using the bridge technology so decrement the module reference count on it */
457 if (bridge->technology->mod) {
458 ast_module_unref(bridge->technology->mod);
461 /* Last but not least clean up the features configuration */
462 ast_bridge_features_cleanup(&bridge->features);
464 /* Drop the array of channels */
465 ast_free(bridge->array);
467 cleanup_video_mode(bridge);
472 struct ast_bridge *ast_bridge_new(uint32_t capabilities, int flags)
474 struct ast_bridge *bridge = NULL;
475 struct ast_bridge_technology *bridge_technology = NULL;
477 /* If we need to be a smart bridge see if we can move between 1to1 and multimix bridges */
478 if (flags & AST_BRIDGE_FLAG_SMART) {
479 struct ast_bridge *other_bridge;
481 if (!(other_bridge = ast_bridge_new((capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX) ? AST_BRIDGE_CAPABILITY_MULTIMIX : AST_BRIDGE_CAPABILITY_1TO1MIX, 0))) {
485 ast_bridge_destroy(other_bridge);
488 /* If capabilities were provided use our helper function to find the "best" bridge technology, otherwise we can
489 * just look for the most basic capability needed, single 1to1 mixing. */
490 bridge_technology = (capabilities ? find_best_technology(capabilities) : find_best_technology(AST_BRIDGE_CAPABILITY_1TO1MIX));
492 /* If no bridge technology was found we can't possibly do bridging so fail creation of the bridge */
493 if (!bridge_technology) {
497 /* We have everything we need to create this bridge... so allocate the memory, link things together, and fire her up! */
498 if (!(bridge = ao2_alloc(sizeof(*bridge), destroy_bridge))) {
502 bridge->technology = bridge_technology;
503 bridge->thread = AST_PTHREADT_NULL;
505 /* Create an array of pointers for the channels that will be joining us */
506 bridge->array = ast_calloc(BRIDGE_ARRAY_START, sizeof(struct ast_channel*));
507 bridge->array_size = BRIDGE_ARRAY_START;
509 ast_set_flag(&bridge->feature_flags, flags);
511 /* Pass off the bridge to the technology to manipulate if needed */
512 if (bridge->technology->create) {
513 ast_debug(1, "Giving bridge technology %s the bridge structure %p to setup\n", bridge->technology->name, bridge);
514 if (bridge->technology->create(bridge)) {
515 ast_debug(1, "Bridge technology %s failed to setup bridge structure %p\n", bridge->technology->name, bridge);
524 int ast_bridge_check(uint32_t capabilities)
526 struct ast_bridge_technology *bridge_technology = NULL;
528 if (!(bridge_technology = find_best_technology(capabilities))) {
532 ast_module_unref(bridge_technology->mod);
537 int ast_bridge_destroy(struct ast_bridge *bridge)
539 struct ast_bridge_channel *bridge_channel = NULL;
543 if (bridge->callid) {
544 bridge->callid = ast_callid_unref(bridge->callid);
551 ast_debug(1, "Telling all channels in bridge %p to end and leave the party\n", bridge);
553 /* Drop every bridged channel, the last one will cause the bridge thread (if it exists) to exit */
554 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
555 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
565 static int bridge_make_compatible(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
567 struct ast_format formats[2];
568 ast_format_copy(&formats[0], ast_channel_readformat(bridge_channel->chan));
569 ast_format_copy(&formats[1], ast_channel_writeformat(bridge_channel->chan));
571 /* Are the formats currently in use something ths bridge can handle? */
572 if (!ast_format_cap_iscompatible(bridge->technology->format_capabilities, ast_channel_readformat(bridge_channel->chan))) {
573 struct ast_format best_format;
574 ast_best_codec(bridge->technology->format_capabilities, &best_format);
576 /* Read format is a no go... */
579 ast_debug(1, "Bridge technology %s wants to read any of formats %s but channel has %s\n", bridge->technology->name,
580 ast_getformatname_multiple(codec_buf, sizeof(codec_buf), bridge->technology->format_capabilities),
581 ast_getformatname(&formats[0]));
583 /* Switch read format to the best one chosen */
584 if (ast_set_read_format(bridge_channel->chan, &best_format)) {
585 ast_log(LOG_WARNING, "Failed to set channel %s to read format %s\n", ast_channel_name(bridge_channel->chan), ast_getformatname(&best_format));
588 ast_debug(1, "Bridge %p put channel %s into read format %s\n", bridge, ast_channel_name(bridge_channel->chan), ast_getformatname(&best_format));
590 ast_debug(1, "Bridge %p is happy that channel %s already has read format %s\n", bridge, ast_channel_name(bridge_channel->chan), ast_getformatname(&formats[0]));
593 if (!ast_format_cap_iscompatible(bridge->technology->format_capabilities, &formats[1])) {
594 struct ast_format best_format;
595 ast_best_codec(bridge->technology->format_capabilities, &best_format);
597 /* Write format is a no go... */
600 ast_debug(1, "Bridge technology %s wants to write any of formats %s but channel has %s\n", bridge->technology->name,
601 ast_getformatname_multiple(codec_buf, sizeof(codec_buf), bridge->technology->format_capabilities),
602 ast_getformatname(&formats[1]));
604 /* Switch write format to the best one chosen */
605 if (ast_set_write_format(bridge_channel->chan, &best_format)) {
606 ast_log(LOG_WARNING, "Failed to set channel %s to write format %s\n", ast_channel_name(bridge_channel->chan), ast_getformatname(&best_format));
609 ast_debug(1, "Bridge %p put channel %s into write format %s\n", bridge, ast_channel_name(bridge_channel->chan), ast_getformatname(&best_format));
611 ast_debug(1, "Bridge %p is happy that channel %s already has write format %s\n", bridge, ast_channel_name(bridge_channel->chan), ast_getformatname(&formats[1]));
617 /*! \brief Perform the smart bridge operation. Basically sees if a new bridge technology should be used instead of the current one. */
618 static int smart_bridge_operation(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, int count)
620 uint32_t new_capabilities = 0;
621 struct ast_bridge_technology *new_technology = NULL, *old_technology = bridge->technology;
622 struct ast_bridge temp_bridge = {
623 .technology = bridge->technology,
624 .bridge_pvt = bridge->bridge_pvt,
626 struct ast_bridge_channel *bridge_channel2 = NULL;
628 /* Based on current feature determine whether we want to change bridge technologies or not */
629 if (bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX) {
631 ast_debug(1, "Bridge %p channel count (%d) is within limits for bridge technology %s, not performing smart bridge operation.\n", bridge, count, bridge->technology->name);
634 new_capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX;
635 } else if (bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) {
637 ast_debug(1, "Bridge %p channel count (%d) is within limits for bridge technology %s, not performing smart bridge operation.\n", bridge, count, bridge->technology->name);
640 new_capabilities = AST_BRIDGE_CAPABILITY_1TO1MIX;
643 if (!new_capabilities) {
644 ast_debug(1, "Bridge '%p' has no new capabilities, not performing smart bridge operation.\n", bridge);
648 /* Attempt to find a new bridge technology to satisfy the capabilities */
649 if (!(new_technology = find_best_technology(new_capabilities))) {
653 ast_debug(1, "Performing smart bridge operation on bridge %p, moving from bridge technology %s to %s\n", bridge, old_technology->name, new_technology->name);
655 /* If a thread is currently executing for the current technology tell it to stop */
656 if (bridge->thread != AST_PTHREADT_NULL) {
657 /* If the new bridge technology also needs a thread simply tell the bridge thread to refresh itself. This has the benefit of not incurring the cost/time of tearing down and bringing up a new thread. */
658 if (new_technology->capabilities & AST_BRIDGE_CAPABILITY_THREAD) {
659 ast_debug(1, "Telling current bridge thread for bridge %p to refresh\n", bridge);
663 pthread_t bridge_thread = bridge->thread;
664 ast_debug(1, "Telling current bridge thread for bridge %p to stop\n", bridge);
668 pthread_join(bridge_thread, NULL);
673 /* Since we are soon going to pass this bridge to a new technology we need to NULL out the bridge_pvt pointer but don't worry as it still exists in temp_bridge, ditto for the old technology */
674 bridge->bridge_pvt = NULL;
675 bridge->technology = new_technology;
677 /* Pass the bridge to the new bridge technology so it can set it up */
678 if (new_technology->create) {
679 ast_debug(1, "Giving bridge technology %s the bridge structure %p to setup\n", new_technology->name, bridge);
680 if (new_technology->create(bridge)) {
681 ast_debug(1, "Bridge technology %s failed to setup bridge structure %p\n", new_technology->name, bridge);
685 /* Move existing channels over to the new technology, while taking them away from the old one */
686 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel2, entry) {
687 /* Skip over channel that initiated the smart bridge operation */
688 if (bridge_channel == bridge_channel2) {
692 /* First we part them from the old technology */
693 if (old_technology->leave) {
694 ast_debug(1, "Giving bridge technology %s notification that %p is leaving bridge %p (really %p)\n", old_technology->name, bridge_channel2, &temp_bridge, bridge);
695 if (old_technology->leave(&temp_bridge, bridge_channel2)) {
696 ast_debug(1, "Bridge technology %s failed to allow %p (really %p) to leave bridge %p\n", old_technology->name, bridge_channel2, &temp_bridge, bridge);
700 /* Second we make them compatible again with the bridge */
701 bridge_make_compatible(bridge, bridge_channel2);
703 /* Third we join them to the new technology */
704 if (new_technology->join) {
705 ast_debug(1, "Giving bridge technology %s notification that %p is joining bridge %p\n", new_technology->name, bridge_channel2, bridge);
706 if (new_technology->join(bridge, bridge_channel2)) {
707 ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n", new_technology->name, bridge_channel2, bridge);
711 /* Fourth we tell them to wake up so they become aware that they above has happened */
712 pthread_kill(bridge_channel2->thread, SIGURG);
713 ao2_lock(bridge_channel2);
714 ast_cond_signal(&bridge_channel2->cond);
715 ao2_unlock(bridge_channel2);
718 /* Now that all the channels have been moved over we need to get rid of all the information the old technology may have left around */
719 if (old_technology->destroy) {
720 ast_debug(1, "Giving bridge technology %s the bridge structure %p (really %p) to destroy\n", old_technology->name, &temp_bridge, bridge);
721 if (old_technology->destroy(&temp_bridge)) {
722 ast_debug(1, "Bridge technology %s failed to destroy bridge structure %p (really %p)... some memory may have leaked\n", old_technology->name, &temp_bridge, bridge);
726 /* Finally if the old technology has module referencing remove our reference, we are no longer going to use it */
727 if (old_technology->mod) {
728 ast_module_unref(old_technology->mod);
734 /*! \brief Run in a multithreaded model. Each joined channel does writing/reading in their own thread. TODO: Improve */
735 static enum ast_bridge_channel_state bridge_channel_join_multithreaded(struct ast_bridge_channel *bridge_channel)
737 int fds[4] = { -1, }, nfds = 0, i = 0, outfd = -1, ms = -1;
738 struct ast_channel *chan = NULL;
740 /* Add any file descriptors we may want to monitor */
741 if (bridge_channel->bridge->technology->fd) {
742 for (i = 0; i < 4; i ++) {
743 if (bridge_channel->fds[i] >= 0) {
744 fds[nfds++] = bridge_channel->fds[i];
749 ao2_unlock(bridge_channel->bridge);
751 ao2_lock(bridge_channel);
752 /* Wait for data to either come from the channel or us to be signalled */
753 if (!bridge_channel->suspended) {
754 ao2_unlock(bridge_channel);
755 ast_debug(10, "Going into a multithreaded waitfor for bridge channel %p of bridge %p\n", bridge_channel, bridge_channel->bridge);
756 chan = ast_waitfor_nandfds(&bridge_channel->chan, 1, fds, nfds, NULL, &outfd, &ms);
758 ast_debug(10, "Going into a multithreaded signal wait for bridge channel %p of bridge %p\n", bridge_channel, bridge_channel->bridge);
759 ast_cond_wait(&bridge_channel->cond, ao2_object_get_lockaddr(bridge_channel));
760 ao2_unlock(bridge_channel);
763 ao2_lock(bridge_channel->bridge);
765 if (!bridge_channel->suspended) {
766 ast_bridge_handle_trip(bridge_channel->bridge, bridge_channel, chan, outfd);
769 return bridge_channel->state;
772 /*! \brief Run in a singlethreaded model. Each joined channel yields itself to the main bridge thread. TODO: Improve */
773 static enum ast_bridge_channel_state bridge_channel_join_singlethreaded(struct ast_bridge_channel *bridge_channel)
775 ao2_unlock(bridge_channel->bridge);
776 ao2_lock(bridge_channel);
777 if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
778 ast_debug(1, "Going into a single threaded signal wait for bridge channel %p of bridge %p\n", bridge_channel, bridge_channel->bridge);
779 ast_cond_wait(&bridge_channel->cond, ao2_object_get_lockaddr(bridge_channel));
781 ao2_unlock(bridge_channel);
782 ao2_lock(bridge_channel->bridge);
784 return bridge_channel->state;
787 /*! \brief Internal function that suspends a channel from a bridge */
788 static void bridge_channel_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
790 ao2_lock(bridge_channel);
791 bridge_channel->suspended = 1;
792 bridge_array_remove(bridge, bridge_channel->chan);
793 ao2_unlock(bridge_channel);
795 if (bridge->technology->suspend) {
796 bridge->technology->suspend(bridge, bridge_channel);
802 /*! \brief Internal function that unsuspends a channel from a bridge */
803 static void bridge_channel_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
805 ao2_lock(bridge_channel);
806 bridge_channel->suspended = 0;
807 bridge_array_add(bridge, bridge_channel->chan);
808 ast_cond_signal(&bridge_channel->cond);
809 ao2_unlock(bridge_channel);
811 if (bridge->technology->unsuspend) {
812 bridge->technology->unsuspend(bridge, bridge_channel);
821 * \brief Internal function that executes a feature on a bridge channel
822 * \note Neither the bridge nor the bridge_channel locks should be held when entering
825 static void bridge_channel_feature(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
827 struct ast_bridge_features *features = (bridge_channel->features ? bridge_channel->features : &bridge->features);
828 struct ast_bridge_features_hook *hook = NULL;
829 char dtmf[MAXIMUM_DTMF_FEATURE_STRING] = "";
830 int look_for_dtmf = 1, dtmf_len = 0;
832 /* The channel is now under our control and we don't really want any begin frames to do our DTMF matching so disable 'em at the core level */
833 ast_set_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_END_DTMF_ONLY);
835 /* Wait for DTMF on the channel and put it into a buffer. If the buffer matches any feature hook execute the hook. */
836 while (look_for_dtmf) {
837 int res = ast_waitfordigit(bridge_channel->chan, 3000);
839 /* If the above timed out simply exit */
841 ast_debug(1, "DTMF feature string collection on bridge channel %p timed out\n", bridge_channel);
843 } else if (res < 0) {
844 ast_debug(1, "DTMF feature string collection failed on bridge channel %p for some reason\n", bridge_channel);
848 /* Add the above DTMF into the DTMF string so we can do our matching */
849 dtmf[dtmf_len++] = res;
851 ast_debug(1, "DTMF feature string on bridge channel %p is now '%s'\n", bridge_channel, dtmf);
853 /* Assume that we do not want to look for DTMF any longer */
856 /* See if a DTMF feature hook matches or can match */
857 AST_LIST_TRAVERSE(&features->hooks, hook, entry) {
858 /* If this hook matches just break out now */
859 if (!strcmp(hook->dtmf, dtmf)) {
860 ast_debug(1, "DTMF feature hook %p matched DTMF string '%s' on bridge channel %p\n", hook, dtmf, bridge_channel);
863 } else if (!strncmp(hook->dtmf, dtmf, dtmf_len)) {
864 ast_debug(1, "DTMF feature hook %p can match DTMF string '%s', it wants '%s', on bridge channel %p\n", hook, dtmf, hook->dtmf, bridge_channel);
867 ast_debug(1, "DTMF feature hook %p does not match DTMF string '%s', it wants '%s', on bridge channel %p\n", hook, dtmf, hook->dtmf, bridge_channel);
871 /* If we have reached the maximum length of a DTMF feature string bail out */
872 if (dtmf_len == MAXIMUM_DTMF_FEATURE_STRING) {
877 /* Since we are done bringing DTMF in return to using both begin and end frames */
878 ast_clear_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_END_DTMF_ONLY);
880 /* If a hook was actually matched execute it on this channel, otherwise stream up the DTMF to the other channels */
882 hook->callback(bridge, bridge_channel, hook->hook_pvt);
883 /* If we are handing the channel off to an external hook for ownership,
884 * we are not guaranteed what kind of state it will come back in. If
885 * the channel hungup, we need to detect that here. */
886 if (bridge_channel->chan && ast_check_hangup_locked(bridge_channel->chan)) {
887 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
890 ast_bridge_dtmf_stream(bridge, dtmf, bridge_channel->chan);
893 /* if the channel is still in feature state, revert it back to wait state */
894 if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_FEATURE) {
895 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
901 static void bridge_channel_talking(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
903 struct ast_bridge_features *features = (bridge_channel->features ? bridge_channel->features : &bridge->features);
905 if (features && features->talker_cb) {
906 features->talker_cb(bridge, bridge_channel, features->talker_pvt_data);
908 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
911 /*! \brief Internal function that plays back DTMF on a bridge channel */
912 static void bridge_channel_dtmf_stream(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
916 ast_copy_string(dtmf_q, bridge_channel->dtmf_stream_q, sizeof(dtmf_q));
917 bridge_channel->dtmf_stream_q[0] = '\0';
919 ast_debug(1, "Playing DTMF stream '%s' out to bridge channel %p\n", dtmf_q, bridge_channel);
920 ast_dtmf_stream(bridge_channel->chan, NULL, dtmf_q, 250, 0);
922 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
927 /*! \brief Join a channel to a bridge and handle anything the bridge may want us to do */
928 static enum ast_bridge_channel_state bridge_channel_join(struct ast_bridge_channel *bridge_channel)
930 struct ast_format formats[2];
931 enum ast_bridge_channel_state state;
932 ast_format_copy(&formats[0], ast_channel_readformat(bridge_channel->chan));
933 ast_format_copy(&formats[1], ast_channel_writeformat(bridge_channel->chan));
935 /* Record the thread that will be the owner of us */
936 bridge_channel->thread = pthread_self();
938 ast_debug(1, "Joining bridge channel %p to bridge %p\n", bridge_channel, bridge_channel->bridge);
940 ao2_lock(bridge_channel->bridge);
942 if (!bridge_channel->bridge->callid) {
943 bridge_channel->bridge->callid = ast_read_threadstorage_callid();
946 state = bridge_channel->state;
948 /* Add channel into the bridge */
949 AST_LIST_INSERT_TAIL(&bridge_channel->bridge->channels, bridge_channel, entry);
950 bridge_channel->bridge->num++;
952 bridge_array_add(bridge_channel->bridge, bridge_channel->chan);
954 if (bridge_channel->swap) {
955 struct ast_bridge_channel *bridge_channel2 = NULL;
957 /* If we are performing a swap operation we do not need
958 * to execute the smart bridge operation as the actual number
959 * of channels involved will not have changed, we just need to
960 * tell the other channel to leave */
961 if ((bridge_channel2 = find_bridge_channel(bridge_channel->bridge, bridge_channel->swap))) {
962 ast_debug(1, "Swapping bridge channel %p out from bridge %p so bridge channel %p can slip in\n", bridge_channel2, bridge_channel->bridge, bridge_channel);
963 ast_bridge_change_state(bridge_channel2, AST_BRIDGE_CHANNEL_STATE_HANGUP);
966 bridge_channel->swap = NULL;
967 } else if (ast_test_flag(&bridge_channel->bridge->feature_flags, AST_BRIDGE_FLAG_SMART)) {
968 /* Perform the smart bridge operation, basically see if we need to move around between technologies */
969 smart_bridge_operation(bridge_channel->bridge, bridge_channel, bridge_channel->bridge->num);
972 /* Make the channel compatible with the bridge */
973 bridge_make_compatible(bridge_channel->bridge, bridge_channel);
975 /* Tell the bridge technology we are joining so they set us up */
976 if (bridge_channel->bridge->technology->join) {
977 ast_debug(1, "Giving bridge technology %s notification that %p is joining bridge %p\n", bridge_channel->bridge->technology->name, bridge_channel, bridge_channel->bridge);
978 if (bridge_channel->bridge->technology->join(bridge_channel->bridge, bridge_channel)) {
979 ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n", bridge_channel->bridge->technology->name, bridge_channel, bridge_channel->bridge);
983 /* Actually execute the respective threading model, and keep our bridge thread alive */
984 while (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
985 /* Update bridge pointer on channel */
986 ast_channel_internal_bridge_set(bridge_channel->chan, bridge_channel->bridge);
987 /* If the technology requires a thread and one is not running, start it up */
988 if (bridge_channel->bridge->thread == AST_PTHREADT_NULL && (bridge_channel->bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_THREAD)) {
989 bridge_channel->bridge->stop = 0;
990 ast_debug(1, "Starting a bridge thread for bridge %p\n", bridge_channel->bridge);
991 ao2_ref(bridge_channel->bridge, +1);
992 if (ast_pthread_create(&bridge_channel->bridge->thread, NULL, bridge_thread, bridge_channel->bridge)) {
993 ast_debug(1, "Failed to create a bridge thread for bridge %p, giving it another go.\n", bridge_channel->bridge);
994 ao2_ref(bridge_channel->bridge, -1);
998 /* Execute the threading model */
999 state = (bridge_channel->bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTITHREADED ? bridge_channel_join_multithreaded(bridge_channel) : bridge_channel_join_singlethreaded(bridge_channel));
1000 /* Depending on the above state see what we need to do */
1002 case AST_BRIDGE_CHANNEL_STATE_FEATURE:
1003 bridge_channel_suspend(bridge_channel->bridge, bridge_channel);
1004 ao2_unlock(bridge_channel->bridge);
1005 bridge_channel_feature(bridge_channel->bridge, bridge_channel);
1006 ao2_lock(bridge_channel->bridge);
1007 bridge_channel_unsuspend(bridge_channel->bridge, bridge_channel);
1009 case AST_BRIDGE_CHANNEL_STATE_DTMF:
1010 bridge_channel_suspend(bridge_channel->bridge, bridge_channel);
1011 bridge_channel_dtmf_stream(bridge_channel->bridge, bridge_channel);
1012 bridge_channel_unsuspend(bridge_channel->bridge, bridge_channel);
1014 case AST_BRIDGE_CHANNEL_STATE_START_TALKING:
1015 case AST_BRIDGE_CHANNEL_STATE_STOP_TALKING:
1016 ao2_unlock(bridge_channel->bridge);
1017 bridge_channel_talking(bridge_channel->bridge, bridge_channel);
1018 ao2_lock(bridge_channel->bridge);
1025 ast_channel_internal_bridge_set(bridge_channel->chan, NULL);
1027 /* See if we need to dissolve the bridge itself if they hung up */
1028 if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_END) {
1029 bridge_check_dissolve(bridge_channel->bridge, bridge_channel);
1032 /* Tell the bridge technology we are leaving so they tear us down */
1033 if (bridge_channel->bridge->technology->leave) {
1034 ast_debug(1, "Giving bridge technology %s notification that %p is leaving bridge %p\n", bridge_channel->bridge->technology->name, bridge_channel, bridge_channel->bridge);
1035 if (bridge_channel->bridge->technology->leave(bridge_channel->bridge, bridge_channel)) {
1036 ast_debug(1, "Bridge technology %s failed to leave %p from bridge %p\n", bridge_channel->bridge->technology->name, bridge_channel, bridge_channel->bridge);
1040 /* Remove channel from the bridge */
1041 bridge_channel->bridge->num--;
1042 AST_LIST_REMOVE(&bridge_channel->bridge->channels, bridge_channel, entry);
1044 bridge_array_remove(bridge_channel->bridge, bridge_channel->chan);
1046 /* Perform the smart bridge operation if needed since a channel has left */
1047 if (ast_test_flag(&bridge_channel->bridge->feature_flags, AST_BRIDGE_FLAG_SMART)) {
1048 smart_bridge_operation(bridge_channel->bridge, NULL, bridge_channel->bridge->num);
1051 ao2_unlock(bridge_channel->bridge);
1053 /* Restore original formats of the channel as they came in */
1054 if (ast_format_cmp(ast_channel_readformat(bridge_channel->chan), &formats[0]) == AST_FORMAT_CMP_NOT_EQUAL) {
1055 ast_debug(1, "Bridge is returning %p to read format %s(%d)\n", bridge_channel, ast_getformatname(&formats[0]), formats[0].id);
1056 if (ast_set_read_format(bridge_channel->chan, &formats[0])) {
1057 ast_debug(1, "Bridge failed to return channel %p to read format %s(%d)\n", bridge_channel, ast_getformatname(&formats[0]), formats[0].id);
1060 if (ast_format_cmp(ast_channel_writeformat(bridge_channel->chan), &formats[1]) == AST_FORMAT_CMP_NOT_EQUAL) {
1061 ast_debug(1, "Bridge is returning %p to write format %s(%d)\n", bridge_channel, ast_getformatname(&formats[1]), formats[1].id);
1062 if (ast_set_write_format(bridge_channel->chan, &formats[1])) {
1063 ast_debug(1, "Bridge failed to return channel %p to write format %s(%d)\n", bridge_channel, ast_getformatname(&formats[1]), formats[1].id);
1067 return bridge_channel->state;
1070 static void bridge_channel_destroy(void *obj)
1072 struct ast_bridge_channel *bridge_channel = obj;
1074 if (bridge_channel->callid) {
1075 bridge_channel->callid = ast_callid_unref(bridge_channel->callid);
1078 if (bridge_channel->bridge) {
1079 ao2_ref(bridge_channel->bridge, -1);
1080 bridge_channel->bridge = NULL;
1082 /* Destroy elements of the bridge channel structure and the bridge channel structure itself */
1083 ast_cond_destroy(&bridge_channel->cond);
1086 static struct ast_bridge_channel *bridge_channel_alloc(struct ast_bridge *bridge)
1088 struct ast_bridge_channel *bridge_channel = ao2_alloc(sizeof(struct ast_bridge_channel), bridge_channel_destroy);
1089 if (!(bridge_channel)) {
1092 ast_cond_init(&bridge_channel->cond, NULL);
1094 bridge_channel->bridge = bridge;
1095 ao2_ref(bridge_channel->bridge, +1);
1097 return bridge_channel;
1100 enum ast_bridge_channel_state ast_bridge_join(struct ast_bridge *bridge,
1101 struct ast_channel *chan,
1102 struct ast_channel *swap,
1103 struct ast_bridge_features *features,
1104 struct ast_bridge_tech_optimizations *tech_args)
1106 struct ast_bridge_channel *bridge_channel = bridge_channel_alloc(bridge);
1107 enum ast_bridge_channel_state state = AST_BRIDGE_CHANNEL_STATE_HANGUP;
1109 if (!bridge_channel) {
1113 memcpy(&bridge_channel->tech_args, tech_args, sizeof(bridge_channel->tech_args));
1116 /* Initialize various other elements of the bridge channel structure that we can't do above */
1117 bridge_channel->chan = chan;
1118 bridge_channel->swap = swap;
1119 bridge_channel->features = features;
1121 state = bridge_channel_join(bridge_channel);
1123 /* Cleanup all the data in the bridge channel after it leaves the bridge. */
1124 ao2_lock(bridge_channel);
1125 bridge_channel->chan = NULL;
1126 bridge_channel->swap = NULL;
1127 bridge_channel->features = NULL;
1128 ao2_unlock(bridge_channel);
1130 ao2_ref(bridge_channel, -1);
1135 /*! \brief Thread responsible for imparted bridged channels */
1136 static void *bridge_channel_thread(void *data)
1138 struct ast_bridge_channel *bridge_channel = data;
1139 enum ast_bridge_channel_state state;
1141 if (bridge_channel->callid) {
1142 ast_callid_threadassoc_add(bridge_channel->callid);
1145 state = bridge_channel_join(bridge_channel);
1147 /* If no other thread is going to take the channel then hang it up, or else we would have to service it until something else came along */
1148 if (bridge_channel->allow_impart_hangup && (state == AST_BRIDGE_CHANNEL_STATE_END || state == AST_BRIDGE_CHANNEL_STATE_HANGUP)) {
1149 ast_hangup(bridge_channel->chan);
1153 ao2_lock(bridge_channel);
1154 bridge_channel->chan = NULL;
1155 bridge_channel->swap = NULL;
1156 bridge_channel->features = NULL;
1157 ao2_unlock(bridge_channel);
1159 ao2_ref(bridge_channel, -1);
1164 int ast_bridge_impart(struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap, struct ast_bridge_features *features, int allow_hangup)
1166 struct ast_bridge_channel *bridge_channel = bridge_channel_alloc(bridge);
1168 /* Try to allocate a structure for the bridge channel */
1169 if (!(bridge_channel)) {
1173 /* Setup various parameters */
1174 bridge_channel->chan = chan;
1175 bridge_channel->swap = swap;
1176 bridge_channel->features = features;
1177 bridge_channel->allow_impart_hangup = allow_hangup;
1178 bridge_channel->callid = ast_read_threadstorage_callid();
1180 /* Actually create the thread that will handle the channel */
1181 if (ast_pthread_create(&bridge_channel->thread, NULL, bridge_channel_thread, bridge_channel)) {
1182 ao2_ref(bridge_channel, -1);
1189 int ast_bridge_depart(struct ast_bridge *bridge, struct ast_channel *chan)
1191 struct ast_bridge_channel *bridge_channel = NULL;
1196 /* Try to find the channel that we want to depart */
1197 if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1202 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_DEPART);
1203 thread = bridge_channel->thread;
1207 pthread_join(thread, NULL);
1212 int ast_bridge_remove(struct ast_bridge *bridge, struct ast_channel *chan)
1214 struct ast_bridge_channel *bridge_channel = NULL;
1218 /* Try to find the channel that we want to remove */
1219 if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1224 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
1231 int ast_bridge_merge(struct ast_bridge *bridge0, struct ast_bridge *bridge1)
1233 struct ast_bridge_channel *bridge_channel = NULL;
1238 /* If the first bridge currently has 2 channels and is not capable of becoming a multimixing bridge we can not merge */
1239 if ((bridge0->num + bridge1->num) > 2 && (!(bridge0->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) && !ast_test_flag(&bridge0->feature_flags, AST_BRIDGE_FLAG_SMART))) {
1240 ao2_unlock(bridge1);
1241 ao2_unlock(bridge0);
1242 ast_debug(1, "Can't merge bridge %p into bridge %p, multimix is needed and it could not be acquired.\n", bridge1, bridge0);
1246 ast_debug(1, "Merging channels from bridge %p into bridge %p\n", bridge1, bridge0);
1248 /* Perform smart bridge operation on bridge we are merging into so it can change bridge technology if needed */
1249 if (smart_bridge_operation(bridge0, NULL, bridge0->num + bridge1->num)) {
1250 ao2_unlock(bridge1);
1251 ao2_unlock(bridge0);
1252 ast_debug(1, "Can't merge bridge %p into bridge %p, tried to perform smart bridge operation and failed.\n", bridge1, bridge0);
1256 /* If a thread is currently executing on bridge1 tell it to stop */
1257 if (bridge1->thread) {
1258 ast_debug(1, "Telling bridge thread on bridge %p to stop as it is being merged into %p\n", bridge1, bridge0);
1259 bridge1->thread = AST_PTHREADT_STOP;
1262 /* Move channels from bridge1 over to bridge0 */
1263 while ((bridge_channel = AST_LIST_REMOVE_HEAD(&bridge1->channels, entry))) {
1264 /* Tell the technology handling bridge1 that the bridge channel is leaving */
1265 if (bridge1->technology->leave) {
1266 ast_debug(1, "Giving bridge technology %s notification that %p is leaving bridge %p\n", bridge1->technology->name, bridge_channel, bridge1);
1267 if (bridge1->technology->leave(bridge1, bridge_channel)) {
1268 ast_debug(1, "Bridge technology %s failed to allow %p to leave bridge %p\n", bridge1->technology->name, bridge_channel, bridge1);
1272 /* Drop channel count and reference count on the bridge they are leaving */
1274 ao2_ref(bridge1, -1);
1276 bridge_array_remove(bridge1, bridge_channel->chan);
1278 /* Now add them into the bridge they are joining, increase channel count, and bump up reference count */
1279 bridge_channel->bridge = bridge0;
1280 AST_LIST_INSERT_TAIL(&bridge0->channels, bridge_channel, entry);
1282 ao2_ref(bridge0, +1);
1284 bridge_array_add(bridge0, bridge_channel->chan);
1286 /* Make the channel compatible with the new bridge it is joining or else formats would go amuck */
1287 bridge_make_compatible(bridge0, bridge_channel);
1289 /* Tell the technology handling bridge0 that the bridge channel is joining */
1290 if (bridge0->technology->join) {
1291 ast_debug(1, "Giving bridge technology %s notification that %p is joining bridge %p\n", bridge0->technology->name, bridge_channel, bridge0);
1292 if (bridge0->technology->join(bridge0, bridge_channel)) {
1293 ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n", bridge0->technology->name, bridge_channel, bridge0);
1297 /* Poke the bridge channel, this will cause it to wake up and execute the proper threading model for the new bridge it is in */
1298 pthread_kill(bridge_channel->thread, SIGURG);
1299 ao2_lock(bridge_channel);
1300 ast_cond_signal(&bridge_channel->cond);
1301 ao2_unlock(bridge_channel);
1304 ast_debug(1, "Merged channels from bridge %p into bridge %p\n", bridge1, bridge0);
1306 ao2_unlock(bridge1);
1307 ao2_unlock(bridge0);
1312 int ast_bridge_suspend(struct ast_bridge *bridge, struct ast_channel *chan)
1314 struct ast_bridge_channel *bridge_channel;
1318 if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1323 bridge_channel_suspend(bridge, bridge_channel);
1330 int ast_bridge_unsuspend(struct ast_bridge *bridge, struct ast_channel *chan)
1332 struct ast_bridge_channel *bridge_channel;
1336 if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1341 bridge_channel_unsuspend(bridge, bridge_channel);
1348 void ast_bridge_technology_suspend(struct ast_bridge_technology *technology)
1350 technology->suspended = 1;
1354 void ast_bridge_technology_unsuspend(struct ast_bridge_technology *technology)
1356 technology->suspended = 0;
1360 int ast_bridge_features_register(enum ast_bridge_builtin_feature feature, ast_bridge_features_hook_callback callback, const char *dtmf)
1362 if (builtin_features_handlers[feature]) {
1366 if (!ast_strlen_zero(dtmf)) {
1367 ast_copy_string(builtin_features_dtmf[feature], dtmf, sizeof(builtin_features_dtmf[feature]));
1370 builtin_features_handlers[feature] = callback;
1375 int ast_bridge_features_unregister(enum ast_bridge_builtin_feature feature)
1377 if (!builtin_features_handlers[feature]) {
1381 builtin_features_handlers[feature] = NULL;
1386 int ast_bridge_features_hook(struct ast_bridge_features *features,
1388 ast_bridge_features_hook_callback callback,
1390 ast_bridge_features_hook_pvt_destructor destructor)
1392 struct ast_bridge_features_hook *hook = NULL;
1394 /* Allocate new memory and setup it's various variables */
1395 if (!(hook = ast_calloc(1, sizeof(*hook)))) {
1399 ast_copy_string(hook->dtmf, dtmf, sizeof(hook->dtmf));
1400 hook->callback = callback;
1401 hook->destructor = destructor;
1402 hook->hook_pvt = hook_pvt;
1404 /* Once done we add it onto the list. Now it will be picked up when DTMF is used */
1405 AST_LIST_INSERT_TAIL(&features->hooks, hook, entry);
1407 features->usable = 1;
1412 int ast_bridge_features_set_talk_detector(struct ast_bridge_features *features,
1413 ast_bridge_talking_indicate_callback talker_cb,
1414 ast_bridge_talking_indicate_destructor talker_destructor,
1417 features->talker_cb = talker_cb;
1418 features->talker_destructor_cb = talker_destructor;
1419 features->talker_pvt_data = pvt_data;
1423 int ast_bridge_features_enable(struct ast_bridge_features *features, enum ast_bridge_builtin_feature feature, const char *dtmf, void *config)
1425 /* If no alternate DTMF stream was provided use the default one */
1426 if (ast_strlen_zero(dtmf)) {
1427 dtmf = builtin_features_dtmf[feature];
1428 /* If no DTMF is still available (ie: it has been disabled) then error out now */
1429 if (ast_strlen_zero(dtmf)) {
1430 ast_debug(1, "Failed to enable built in feature %d on %p, no DTMF string is available for it.\n", feature, features);
1435 if (!builtin_features_handlers[feature]) {
1439 /* The rest is basically pretty easy. We create another hook using the built in feature's callback and DTMF, easy as pie. */
1440 return ast_bridge_features_hook(features, dtmf, builtin_features_handlers[feature], config, NULL);
1443 int ast_bridge_features_set_flag(struct ast_bridge_features *features, enum ast_bridge_feature_flags flag)
1445 ast_set_flag(&features->feature_flags, flag);
1446 features->usable = 1;
1450 int ast_bridge_features_init(struct ast_bridge_features *features)
1452 /* Zero out the structure */
1453 memset(features, 0, sizeof(*features));
1455 /* Initialize the hooks list, just in case */
1456 AST_LIST_HEAD_INIT_NOLOCK(&features->hooks);
1461 int ast_bridge_features_cleanup(struct ast_bridge_features *features)
1463 struct ast_bridge_features_hook *hook = NULL;
1465 /* This is relatively simple, hooks are kept as a list on the features structure so we just pop them off and free them */
1466 while ((hook = AST_LIST_REMOVE_HEAD(&features->hooks, entry))) {
1467 if (hook->destructor) {
1468 hook->destructor(hook->hook_pvt);
1472 if (features->talker_destructor_cb && features->talker_pvt_data) {
1473 features->talker_destructor_cb(features->talker_pvt_data);
1474 features->talker_pvt_data = NULL;
1480 int ast_bridge_dtmf_stream(struct ast_bridge *bridge, const char *dtmf, struct ast_channel *chan)
1482 struct ast_bridge_channel *bridge_channel = NULL;
1486 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1487 if (bridge_channel->chan == chan) {
1490 ast_copy_string(bridge_channel->dtmf_stream_q, dtmf, sizeof(bridge_channel->dtmf_stream_q));
1491 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_DTMF);
1499 void ast_bridge_set_mixing_interval(struct ast_bridge *bridge, unsigned int mixing_interval)
1502 bridge->internal_mixing_interval = mixing_interval;
1506 void ast_bridge_set_internal_sample_rate(struct ast_bridge *bridge, unsigned int sample_rate)
1510 bridge->internal_sample_rate = sample_rate;
1514 static void cleanup_video_mode(struct ast_bridge *bridge)
1516 switch (bridge->video_mode.mode) {
1517 case AST_BRIDGE_VIDEO_MODE_NONE:
1519 case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
1520 if (bridge->video_mode.mode_data.single_src_data.chan_vsrc) {
1521 ast_channel_unref(bridge->video_mode.mode_data.single_src_data.chan_vsrc);
1524 case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
1525 if (bridge->video_mode.mode_data.talker_src_data.chan_vsrc) {
1526 ast_channel_unref(bridge->video_mode.mode_data.talker_src_data.chan_vsrc);
1528 if (bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc) {
1529 ast_channel_unref(bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc);
1532 memset(&bridge->video_mode, 0, sizeof(bridge->video_mode));
1535 void ast_bridge_set_single_src_video_mode(struct ast_bridge *bridge, struct ast_channel *video_src_chan)
1538 cleanup_video_mode(bridge);
1539 bridge->video_mode.mode = AST_BRIDGE_VIDEO_MODE_SINGLE_SRC;
1540 bridge->video_mode.mode_data.single_src_data.chan_vsrc = ast_channel_ref(video_src_chan);
1541 ast_test_suite_event_notify("BRIDGE_VIDEO_MODE", "Message: video mode set to single source\r\nVideo Mode: %d\r\nVideo Channel: %s", bridge->video_mode.mode, ast_channel_name(video_src_chan));
1542 ast_indicate(video_src_chan, AST_CONTROL_VIDUPDATE);
1546 void ast_bridge_set_talker_src_video_mode(struct ast_bridge *bridge)
1549 cleanup_video_mode(bridge);
1550 bridge->video_mode.mode = AST_BRIDGE_VIDEO_MODE_TALKER_SRC;
1551 ast_test_suite_event_notify("BRIDGE_VIDEO_MODE", "Message: video mode set to talker source\r\nVideo Mode: %d", bridge->video_mode.mode);
1555 void ast_bridge_update_talker_src_video_mode(struct ast_bridge *bridge, struct ast_channel *chan, int talker_energy, int is_keyframe)
1557 struct ast_bridge_video_talker_src_data *data;
1558 /* If the channel doesn't support video, we don't care about it */
1559 if (!ast_format_cap_has_type(ast_channel_nativeformats(chan), AST_FORMAT_TYPE_VIDEO)) {
1564 data = &bridge->video_mode.mode_data.talker_src_data;
1566 if (data->chan_vsrc == chan) {
1567 data->average_talking_energy = talker_energy;
1568 } else if ((data->average_talking_energy < talker_energy) && is_keyframe) {
1569 if (data->chan_old_vsrc) {
1570 ast_channel_unref(data->chan_old_vsrc);
1572 if (data->chan_vsrc) {
1573 data->chan_old_vsrc = data->chan_vsrc;
1574 ast_indicate(data->chan_old_vsrc, AST_CONTROL_VIDUPDATE);
1576 data->chan_vsrc = ast_channel_ref(chan);
1577 data->average_talking_energy = talker_energy;
1578 ast_test_suite_event_notify("BRIDGE_VIDEO_SRC", "Message: video source updated\r\nVideo Channel: %s", ast_channel_name(data->chan_vsrc));
1579 ast_indicate(data->chan_vsrc, AST_CONTROL_VIDUPDATE);
1580 } else if ((data->average_talking_energy < talker_energy) && !is_keyframe) {
1581 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
1582 } else if (!data->chan_vsrc && is_keyframe) {
1583 data->chan_vsrc = ast_channel_ref(chan);
1584 data->average_talking_energy = talker_energy;
1585 ast_test_suite_event_notify("BRIDGE_VIDEO_SRC", "Message: video source updated\r\nVideo Channel: %s", ast_channel_name(data->chan_vsrc));
1586 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
1587 } else if (!data->chan_old_vsrc && is_keyframe) {
1588 data->chan_old_vsrc = ast_channel_ref(chan);
1589 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
1594 int ast_bridge_number_video_src(struct ast_bridge *bridge)
1599 switch (bridge->video_mode.mode) {
1600 case AST_BRIDGE_VIDEO_MODE_NONE:
1602 case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
1603 if (bridge->video_mode.mode_data.single_src_data.chan_vsrc) {
1607 case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
1608 if (bridge->video_mode.mode_data.talker_src_data.chan_vsrc) {
1611 if (bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc) {
1619 int ast_bridge_is_video_src(struct ast_bridge *bridge, struct ast_channel *chan)
1624 switch (bridge->video_mode.mode) {
1625 case AST_BRIDGE_VIDEO_MODE_NONE:
1627 case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
1628 if (bridge->video_mode.mode_data.single_src_data.chan_vsrc == chan) {
1632 case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
1633 if (bridge->video_mode.mode_data.talker_src_data.chan_vsrc == chan) {
1635 } else if (bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc == chan) {
1644 void ast_bridge_remove_video_src(struct ast_bridge *bridge, struct ast_channel *chan)
1647 switch (bridge->video_mode.mode) {
1648 case AST_BRIDGE_VIDEO_MODE_NONE:
1650 case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
1651 if (bridge->video_mode.mode_data.single_src_data.chan_vsrc == chan) {
1652 if (bridge->video_mode.mode_data.single_src_data.chan_vsrc) {
1653 ast_channel_unref(bridge->video_mode.mode_data.single_src_data.chan_vsrc);
1655 bridge->video_mode.mode_data.single_src_data.chan_vsrc = NULL;
1658 case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
1659 if (bridge->video_mode.mode_data.talker_src_data.chan_vsrc == chan) {
1660 if (bridge->video_mode.mode_data.talker_src_data.chan_vsrc) {
1661 ast_channel_unref(bridge->video_mode.mode_data.talker_src_data.chan_vsrc);
1663 bridge->video_mode.mode_data.talker_src_data.chan_vsrc = NULL;
1664 bridge->video_mode.mode_data.talker_src_data.average_talking_energy = 0;
1666 if (bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc == chan) {
1667 if (bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc) {
1668 ast_channel_unref(bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc);
1670 bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc = NULL;