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 ast_debug(1, "Started bridge thread for %p\n", bridge);
381 /* Loop around until we are told to stop */
382 while (!bridge->stop && bridge->array_num && !res) {
383 /* In case the refresh bit was set simply set it back to off */
386 ast_debug(1, "Launching bridge thread function %p for bridge %p\n", (bridge->technology->thread ? bridge->technology->thread : &generic_thread_loop), bridge);
388 /* Execute the appropriate thread function. If the technology does not provide one we use the generic one */
389 res = (bridge->technology->thread ? bridge->technology->thread(bridge) : generic_thread_loop(bridge));
392 ast_debug(1, "Ending bridge thread for %p\n", bridge);
394 /* Indicate the bridge thread is no longer active */
395 bridge->thread = AST_PTHREADT_NULL;
403 /*! \brief Helper function used to find the "best" bridge technology given a specified capabilities */
404 static struct ast_bridge_technology *find_best_technology(uint32_t capabilities)
406 struct ast_bridge_technology *current = NULL, *best = NULL;
408 AST_RWLIST_RDLOCK(&bridge_technologies);
409 AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
410 if (current->suspended) {
411 ast_debug(1, "Bridge technology %s is suspended. Skipping.\n", current->name);
414 if (!(current->capabilities & capabilities)) {
415 ast_debug(1, "Bridge technology %s does not have the capabilities we need.\n", current->name);
418 if (best && best->preference < current->preference) {
419 ast_debug(1, "Bridge technology %s has preference %d while %s has preference %d. Skipping.\n", current->name, current->preference, best->name, best->preference);
426 /* Increment it's module reference count if present so it does not get unloaded while in use */
428 ast_module_ref(best->mod);
430 ast_debug(1, "Chose bridge technology %s\n", best->name);
433 AST_RWLIST_UNLOCK(&bridge_technologies);
438 static void destroy_bridge(void *obj)
440 struct ast_bridge *bridge = obj;
442 ast_debug(1, "Actually destroying bridge %p, nobody wants it anymore\n", bridge);
444 /* Pass off the bridge to the technology to destroy if needed */
445 if (bridge->technology->destroy) {
446 ast_debug(1, "Giving bridge technology %s the bridge structure %p to destroy\n", bridge->technology->name, bridge);
447 if (bridge->technology->destroy(bridge)) {
448 ast_debug(1, "Bridge technology %s failed to destroy bridge structure %p... trying our best\n", bridge->technology->name, bridge);
452 /* We are no longer using the bridge technology so decrement the module reference count on it */
453 if (bridge->technology->mod) {
454 ast_module_unref(bridge->technology->mod);
457 /* Last but not least clean up the features configuration */
458 ast_bridge_features_cleanup(&bridge->features);
460 /* Drop the array of channels */
461 ast_free(bridge->array);
463 cleanup_video_mode(bridge);
468 struct ast_bridge *ast_bridge_new(uint32_t capabilities, int flags)
470 struct ast_bridge *bridge = NULL;
471 struct ast_bridge_technology *bridge_technology = NULL;
473 /* If we need to be a smart bridge see if we can move between 1to1 and multimix bridges */
474 if (flags & AST_BRIDGE_FLAG_SMART) {
475 struct ast_bridge *other_bridge;
477 if (!(other_bridge = ast_bridge_new((capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX) ? AST_BRIDGE_CAPABILITY_MULTIMIX : AST_BRIDGE_CAPABILITY_1TO1MIX, 0))) {
481 ast_bridge_destroy(other_bridge);
484 /* If capabilities were provided use our helper function to find the "best" bridge technology, otherwise we can
485 * just look for the most basic capability needed, single 1to1 mixing. */
486 bridge_technology = (capabilities ? find_best_technology(capabilities) : find_best_technology(AST_BRIDGE_CAPABILITY_1TO1MIX));
488 /* If no bridge technology was found we can't possibly do bridging so fail creation of the bridge */
489 if (!bridge_technology) {
493 /* We have everything we need to create this bridge... so allocate the memory, link things together, and fire her up! */
494 if (!(bridge = ao2_alloc(sizeof(*bridge), destroy_bridge))) {
498 bridge->technology = bridge_technology;
499 bridge->thread = AST_PTHREADT_NULL;
501 /* Create an array of pointers for the channels that will be joining us */
502 bridge->array = ast_calloc(BRIDGE_ARRAY_START, sizeof(struct ast_channel*));
503 bridge->array_size = BRIDGE_ARRAY_START;
505 ast_set_flag(&bridge->feature_flags, flags);
507 /* Pass off the bridge to the technology to manipulate if needed */
508 if (bridge->technology->create) {
509 ast_debug(1, "Giving bridge technology %s the bridge structure %p to setup\n", bridge->technology->name, bridge);
510 if (bridge->technology->create(bridge)) {
511 ast_debug(1, "Bridge technology %s failed to setup bridge structure %p\n", bridge->technology->name, bridge);
520 int ast_bridge_check(uint32_t capabilities)
522 struct ast_bridge_technology *bridge_technology = NULL;
524 if (!(bridge_technology = find_best_technology(capabilities))) {
528 ast_module_unref(bridge_technology->mod);
533 int ast_bridge_destroy(struct ast_bridge *bridge)
535 struct ast_bridge_channel *bridge_channel = NULL;
543 ast_debug(1, "Telling all channels in bridge %p to end and leave the party\n", bridge);
545 /* Drop every bridged channel, the last one will cause the bridge thread (if it exists) to exit */
546 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
547 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
557 static int bridge_make_compatible(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
559 struct ast_format formats[2];
560 ast_format_copy(&formats[0], ast_channel_readformat(bridge_channel->chan));
561 ast_format_copy(&formats[1], ast_channel_writeformat(bridge_channel->chan));
563 /* Are the formats currently in use something ths bridge can handle? */
564 if (!ast_format_cap_iscompatible(bridge->technology->format_capabilities, ast_channel_readformat(bridge_channel->chan))) {
565 struct ast_format best_format;
566 ast_best_codec(bridge->technology->format_capabilities, &best_format);
568 /* Read format is a no go... */
571 ast_debug(1, "Bridge technology %s wants to read any of formats %s but channel has %s\n", bridge->technology->name,
572 ast_getformatname_multiple(codec_buf, sizeof(codec_buf), bridge->technology->format_capabilities),
573 ast_getformatname(&formats[0]));
575 /* Switch read format to the best one chosen */
576 if (ast_set_read_format(bridge_channel->chan, &best_format)) {
577 ast_log(LOG_WARNING, "Failed to set channel %s to read format %s\n", ast_channel_name(bridge_channel->chan), ast_getformatname(&best_format));
580 ast_debug(1, "Bridge %p put channel %s into read format %s\n", bridge, ast_channel_name(bridge_channel->chan), ast_getformatname(&best_format));
582 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]));
585 if (!ast_format_cap_iscompatible(bridge->technology->format_capabilities, &formats[1])) {
586 struct ast_format best_format;
587 ast_best_codec(bridge->technology->format_capabilities, &best_format);
589 /* Write format is a no go... */
592 ast_debug(1, "Bridge technology %s wants to write any of formats %s but channel has %s\n", bridge->technology->name,
593 ast_getformatname_multiple(codec_buf, sizeof(codec_buf), bridge->technology->format_capabilities),
594 ast_getformatname(&formats[1]));
596 /* Switch write format to the best one chosen */
597 if (ast_set_write_format(bridge_channel->chan, &best_format)) {
598 ast_log(LOG_WARNING, "Failed to set channel %s to write format %s\n", ast_channel_name(bridge_channel->chan), ast_getformatname(&best_format));
601 ast_debug(1, "Bridge %p put channel %s into write format %s\n", bridge, ast_channel_name(bridge_channel->chan), ast_getformatname(&best_format));
603 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]));
609 /*! \brief Perform the smart bridge operation. Basically sees if a new bridge technology should be used instead of the current one. */
610 static int smart_bridge_operation(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, int count)
612 uint32_t new_capabilities = 0;
613 struct ast_bridge_technology *new_technology = NULL, *old_technology = bridge->technology;
614 struct ast_bridge temp_bridge = {
615 .technology = bridge->technology,
616 .bridge_pvt = bridge->bridge_pvt,
618 struct ast_bridge_channel *bridge_channel2 = NULL;
620 /* Based on current feature determine whether we want to change bridge technologies or not */
621 if (bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX) {
623 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);
626 new_capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX;
627 } else if (bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) {
629 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);
632 new_capabilities = AST_BRIDGE_CAPABILITY_1TO1MIX;
635 if (!new_capabilities) {
636 ast_debug(1, "Bridge '%p' has no new capabilities, not performing smart bridge operation.\n", bridge);
640 /* Attempt to find a new bridge technology to satisfy the capabilities */
641 if (!(new_technology = find_best_technology(new_capabilities))) {
645 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);
647 /* If a thread is currently executing for the current technology tell it to stop */
648 if (bridge->thread != AST_PTHREADT_NULL) {
649 /* 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. */
650 if (new_technology->capabilities & AST_BRIDGE_CAPABILITY_THREAD) {
651 ast_debug(1, "Telling current bridge thread for bridge %p to refresh\n", bridge);
655 pthread_t bridge_thread = bridge->thread;
656 ast_debug(1, "Telling current bridge thread for bridge %p to stop\n", bridge);
660 pthread_join(bridge_thread, NULL);
665 /* 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 */
666 bridge->bridge_pvt = NULL;
667 bridge->technology = new_technology;
669 /* Pass the bridge to the new bridge technology so it can set it up */
670 if (new_technology->create) {
671 ast_debug(1, "Giving bridge technology %s the bridge structure %p to setup\n", new_technology->name, bridge);
672 if (new_technology->create(bridge)) {
673 ast_debug(1, "Bridge technology %s failed to setup bridge structure %p\n", new_technology->name, bridge);
677 /* Move existing channels over to the new technology, while taking them away from the old one */
678 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel2, entry) {
679 /* Skip over channel that initiated the smart bridge operation */
680 if (bridge_channel == bridge_channel2) {
684 /* First we part them from the old technology */
685 if (old_technology->leave) {
686 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);
687 if (old_technology->leave(&temp_bridge, bridge_channel2)) {
688 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);
692 /* Second we make them compatible again with the bridge */
693 bridge_make_compatible(bridge, bridge_channel2);
695 /* Third we join them to the new technology */
696 if (new_technology->join) {
697 ast_debug(1, "Giving bridge technology %s notification that %p is joining bridge %p\n", new_technology->name, bridge_channel2, bridge);
698 if (new_technology->join(bridge, bridge_channel2)) {
699 ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n", new_technology->name, bridge_channel2, bridge);
703 /* Fourth we tell them to wake up so they become aware that they above has happened */
704 pthread_kill(bridge_channel2->thread, SIGURG);
705 ao2_lock(bridge_channel2);
706 ast_cond_signal(&bridge_channel2->cond);
707 ao2_unlock(bridge_channel2);
710 /* 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 */
711 if (old_technology->destroy) {
712 ast_debug(1, "Giving bridge technology %s the bridge structure %p (really %p) to destroy\n", old_technology->name, &temp_bridge, bridge);
713 if (old_technology->destroy(&temp_bridge)) {
714 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);
718 /* Finally if the old technology has module referencing remove our reference, we are no longer going to use it */
719 if (old_technology->mod) {
720 ast_module_unref(old_technology->mod);
726 /*! \brief Run in a multithreaded model. Each joined channel does writing/reading in their own thread. TODO: Improve */
727 static enum ast_bridge_channel_state bridge_channel_join_multithreaded(struct ast_bridge_channel *bridge_channel)
729 int fds[4] = { -1, }, nfds = 0, i = 0, outfd = -1, ms = -1;
730 struct ast_channel *chan = NULL;
732 /* Add any file descriptors we may want to monitor */
733 if (bridge_channel->bridge->technology->fd) {
734 for (i = 0; i < 4; i ++) {
735 if (bridge_channel->fds[i] >= 0) {
736 fds[nfds++] = bridge_channel->fds[i];
741 ao2_unlock(bridge_channel->bridge);
743 ao2_lock(bridge_channel);
744 /* Wait for data to either come from the channel or us to be signalled */
745 if (!bridge_channel->suspended) {
746 ao2_unlock(bridge_channel);
747 ast_debug(10, "Going into a multithreaded waitfor for bridge channel %p of bridge %p\n", bridge_channel, bridge_channel->bridge);
748 chan = ast_waitfor_nandfds(&bridge_channel->chan, 1, fds, nfds, NULL, &outfd, &ms);
750 ast_debug(10, "Going into a multithreaded signal wait for bridge channel %p of bridge %p\n", bridge_channel, bridge_channel->bridge);
751 ast_cond_wait(&bridge_channel->cond, ao2_object_get_lockaddr(bridge_channel));
752 ao2_unlock(bridge_channel);
755 ao2_lock(bridge_channel->bridge);
757 if (!bridge_channel->suspended) {
758 ast_bridge_handle_trip(bridge_channel->bridge, bridge_channel, chan, outfd);
761 return bridge_channel->state;
764 /*! \brief Run in a singlethreaded model. Each joined channel yields itself to the main bridge thread. TODO: Improve */
765 static enum ast_bridge_channel_state bridge_channel_join_singlethreaded(struct ast_bridge_channel *bridge_channel)
767 ao2_unlock(bridge_channel->bridge);
768 ao2_lock(bridge_channel);
769 if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
770 ast_debug(1, "Going into a single threaded signal wait for bridge channel %p of bridge %p\n", bridge_channel, bridge_channel->bridge);
771 ast_cond_wait(&bridge_channel->cond, ao2_object_get_lockaddr(bridge_channel));
773 ao2_unlock(bridge_channel);
774 ao2_lock(bridge_channel->bridge);
776 return bridge_channel->state;
779 /*! \brief Internal function that suspends a channel from a bridge */
780 static void bridge_channel_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
782 ao2_lock(bridge_channel);
783 bridge_channel->suspended = 1;
784 bridge_array_remove(bridge, bridge_channel->chan);
785 ao2_unlock(bridge_channel);
787 if (bridge->technology->suspend) {
788 bridge->technology->suspend(bridge, bridge_channel);
794 /*! \brief Internal function that unsuspends a channel from a bridge */
795 static void bridge_channel_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
797 ao2_lock(bridge_channel);
798 bridge_channel->suspended = 0;
799 bridge_array_add(bridge, bridge_channel->chan);
800 ast_cond_signal(&bridge_channel->cond);
801 ao2_unlock(bridge_channel);
803 if (bridge->technology->unsuspend) {
804 bridge->technology->unsuspend(bridge, bridge_channel);
813 * \brief Internal function that executes a feature on a bridge channel
814 * \note Neither the bridge nor the bridge_channel locks should be held when entering
817 static void bridge_channel_feature(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
819 struct ast_bridge_features *features = (bridge_channel->features ? bridge_channel->features : &bridge->features);
820 struct ast_bridge_features_hook *hook = NULL;
821 char dtmf[MAXIMUM_DTMF_FEATURE_STRING] = "";
822 int look_for_dtmf = 1, dtmf_len = 0;
824 /* 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 */
825 ast_set_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_END_DTMF_ONLY);
827 /* Wait for DTMF on the channel and put it into a buffer. If the buffer matches any feature hook execute the hook. */
828 while (look_for_dtmf) {
829 int res = ast_waitfordigit(bridge_channel->chan, 3000);
831 /* If the above timed out simply exit */
833 ast_debug(1, "DTMF feature string collection on bridge channel %p timed out\n", bridge_channel);
835 } else if (res < 0) {
836 ast_debug(1, "DTMF feature string collection failed on bridge channel %p for some reason\n", bridge_channel);
840 /* Add the above DTMF into the DTMF string so we can do our matching */
841 dtmf[dtmf_len++] = res;
843 ast_debug(1, "DTMF feature string on bridge channel %p is now '%s'\n", bridge_channel, dtmf);
845 /* Assume that we do not want to look for DTMF any longer */
848 /* See if a DTMF feature hook matches or can match */
849 AST_LIST_TRAVERSE(&features->hooks, hook, entry) {
850 /* If this hook matches just break out now */
851 if (!strcmp(hook->dtmf, dtmf)) {
852 ast_debug(1, "DTMF feature hook %p matched DTMF string '%s' on bridge channel %p\n", hook, dtmf, bridge_channel);
855 } else if (!strncmp(hook->dtmf, dtmf, dtmf_len)) {
856 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);
859 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);
863 /* If we have reached the maximum length of a DTMF feature string bail out */
864 if (dtmf_len == MAXIMUM_DTMF_FEATURE_STRING) {
869 /* Since we are done bringing DTMF in return to using both begin and end frames */
870 ast_clear_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_END_DTMF_ONLY);
872 /* If a hook was actually matched execute it on this channel, otherwise stream up the DTMF to the other channels */
874 hook->callback(bridge, bridge_channel, hook->hook_pvt);
875 /* If we are handing the channel off to an external hook for ownership,
876 * we are not guaranteed what kind of state it will come back in. If
877 * the channel hungup, we need to detect that here. */
878 if (bridge_channel->chan && ast_check_hangup_locked(bridge_channel->chan)) {
879 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
882 ast_bridge_dtmf_stream(bridge, dtmf, bridge_channel->chan);
885 /* if the channel is still in feature state, revert it back to wait state */
886 if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_FEATURE) {
887 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
893 static void bridge_channel_talking(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
895 struct ast_bridge_features *features = (bridge_channel->features ? bridge_channel->features : &bridge->features);
897 if (features && features->talker_cb) {
898 features->talker_cb(bridge, bridge_channel, features->talker_pvt_data);
900 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
903 /*! \brief Internal function that plays back DTMF on a bridge channel */
904 static void bridge_channel_dtmf_stream(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
908 ast_copy_string(dtmf_q, bridge_channel->dtmf_stream_q, sizeof(dtmf_q));
909 bridge_channel->dtmf_stream_q[0] = '\0';
911 ast_debug(1, "Playing DTMF stream '%s' out to bridge channel %p\n", dtmf_q, bridge_channel);
912 ast_dtmf_stream(bridge_channel->chan, NULL, dtmf_q, 250, 0);
914 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
919 /*! \brief Join a channel to a bridge and handle anything the bridge may want us to do */
920 static enum ast_bridge_channel_state bridge_channel_join(struct ast_bridge_channel *bridge_channel)
922 struct ast_format formats[2];
923 enum ast_bridge_channel_state state;
924 ast_format_copy(&formats[0], ast_channel_readformat(bridge_channel->chan));
925 ast_format_copy(&formats[1], ast_channel_writeformat(bridge_channel->chan));
927 /* Record the thread that will be the owner of us */
928 bridge_channel->thread = pthread_self();
930 ast_debug(1, "Joining bridge channel %p to bridge %p\n", bridge_channel, bridge_channel->bridge);
932 ao2_lock(bridge_channel->bridge);
934 state = bridge_channel->state;
936 /* Add channel into the bridge */
937 AST_LIST_INSERT_TAIL(&bridge_channel->bridge->channels, bridge_channel, entry);
938 bridge_channel->bridge->num++;
940 bridge_array_add(bridge_channel->bridge, bridge_channel->chan);
942 if (bridge_channel->swap) {
943 struct ast_bridge_channel *bridge_channel2 = NULL;
945 /* If we are performing a swap operation we do not need
946 * to execute the smart bridge operation as the actual number
947 * of channels involved will not have changed, we just need to
948 * tell the other channel to leave */
949 if ((bridge_channel2 = find_bridge_channel(bridge_channel->bridge, bridge_channel->swap))) {
950 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);
951 ast_bridge_change_state(bridge_channel2, AST_BRIDGE_CHANNEL_STATE_HANGUP);
954 bridge_channel->swap = NULL;
955 } else if (ast_test_flag(&bridge_channel->bridge->feature_flags, AST_BRIDGE_FLAG_SMART)) {
956 /* Perform the smart bridge operation, basically see if we need to move around between technologies */
957 smart_bridge_operation(bridge_channel->bridge, bridge_channel, bridge_channel->bridge->num);
960 /* Make the channel compatible with the bridge */
961 bridge_make_compatible(bridge_channel->bridge, bridge_channel);
963 /* Tell the bridge technology we are joining so they set us up */
964 if (bridge_channel->bridge->technology->join) {
965 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);
966 if (bridge_channel->bridge->technology->join(bridge_channel->bridge, bridge_channel)) {
967 ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n", bridge_channel->bridge->technology->name, bridge_channel, bridge_channel->bridge);
971 /* Actually execute the respective threading model, and keep our bridge thread alive */
972 while (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
973 /* Update bridge pointer on channel */
974 ast_channel_internal_bridge_set(bridge_channel->chan, bridge_channel->bridge);
975 /* If the technology requires a thread and one is not running, start it up */
976 if (bridge_channel->bridge->thread == AST_PTHREADT_NULL && (bridge_channel->bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_THREAD)) {
977 bridge_channel->bridge->stop = 0;
978 ast_debug(1, "Starting a bridge thread for bridge %p\n", bridge_channel->bridge);
979 ao2_ref(bridge_channel->bridge, +1);
980 if (ast_pthread_create(&bridge_channel->bridge->thread, NULL, bridge_thread, bridge_channel->bridge)) {
981 ast_debug(1, "Failed to create a bridge thread for bridge %p, giving it another go.\n", bridge_channel->bridge);
982 ao2_ref(bridge_channel->bridge, -1);
986 /* Execute the threading model */
987 state = (bridge_channel->bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTITHREADED ? bridge_channel_join_multithreaded(bridge_channel) : bridge_channel_join_singlethreaded(bridge_channel));
988 /* Depending on the above state see what we need to do */
990 case AST_BRIDGE_CHANNEL_STATE_FEATURE:
991 bridge_channel_suspend(bridge_channel->bridge, bridge_channel);
992 ao2_unlock(bridge_channel->bridge);
993 bridge_channel_feature(bridge_channel->bridge, bridge_channel);
994 ao2_lock(bridge_channel->bridge);
995 bridge_channel_unsuspend(bridge_channel->bridge, bridge_channel);
997 case AST_BRIDGE_CHANNEL_STATE_DTMF:
998 bridge_channel_suspend(bridge_channel->bridge, bridge_channel);
999 bridge_channel_dtmf_stream(bridge_channel->bridge, bridge_channel);
1000 bridge_channel_unsuspend(bridge_channel->bridge, bridge_channel);
1002 case AST_BRIDGE_CHANNEL_STATE_START_TALKING:
1003 case AST_BRIDGE_CHANNEL_STATE_STOP_TALKING:
1004 ao2_unlock(bridge_channel->bridge);
1005 bridge_channel_talking(bridge_channel->bridge, bridge_channel);
1006 ao2_lock(bridge_channel->bridge);
1013 ast_channel_internal_bridge_set(bridge_channel->chan, NULL);
1015 /* See if we need to dissolve the bridge itself if they hung up */
1016 if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_END) {
1017 bridge_check_dissolve(bridge_channel->bridge, bridge_channel);
1020 /* Tell the bridge technology we are leaving so they tear us down */
1021 if (bridge_channel->bridge->technology->leave) {
1022 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);
1023 if (bridge_channel->bridge->technology->leave(bridge_channel->bridge, bridge_channel)) {
1024 ast_debug(1, "Bridge technology %s failed to leave %p from bridge %p\n", bridge_channel->bridge->technology->name, bridge_channel, bridge_channel->bridge);
1028 /* Remove channel from the bridge */
1029 bridge_channel->bridge->num--;
1030 AST_LIST_REMOVE(&bridge_channel->bridge->channels, bridge_channel, entry);
1032 bridge_array_remove(bridge_channel->bridge, bridge_channel->chan);
1034 /* Perform the smart bridge operation if needed since a channel has left */
1035 if (ast_test_flag(&bridge_channel->bridge->feature_flags, AST_BRIDGE_FLAG_SMART)) {
1036 smart_bridge_operation(bridge_channel->bridge, NULL, bridge_channel->bridge->num);
1039 ao2_unlock(bridge_channel->bridge);
1041 /* Restore original formats of the channel as they came in */
1042 if (ast_format_cmp(ast_channel_readformat(bridge_channel->chan), &formats[0]) == AST_FORMAT_CMP_NOT_EQUAL) {
1043 ast_debug(1, "Bridge is returning %p to read format %s(%d)\n", bridge_channel, ast_getformatname(&formats[0]), formats[0].id);
1044 if (ast_set_read_format(bridge_channel->chan, &formats[0])) {
1045 ast_debug(1, "Bridge failed to return channel %p to read format %s(%d)\n", bridge_channel, ast_getformatname(&formats[0]), formats[0].id);
1048 if (ast_format_cmp(ast_channel_writeformat(bridge_channel->chan), &formats[1]) == AST_FORMAT_CMP_NOT_EQUAL) {
1049 ast_debug(1, "Bridge is returning %p to write format %s(%d)\n", bridge_channel, ast_getformatname(&formats[1]), formats[1].id);
1050 if (ast_set_write_format(bridge_channel->chan, &formats[1])) {
1051 ast_debug(1, "Bridge failed to return channel %p to write format %s(%d)\n", bridge_channel, ast_getformatname(&formats[1]), formats[1].id);
1055 return bridge_channel->state;
1058 static void bridge_channel_destroy(void *obj)
1060 struct ast_bridge_channel *bridge_channel = obj;
1062 if (bridge_channel->bridge) {
1063 ao2_ref(bridge_channel->bridge, -1);
1064 bridge_channel->bridge = NULL;
1066 /* Destroy elements of the bridge channel structure and the bridge channel structure itself */
1067 ast_cond_destroy(&bridge_channel->cond);
1070 static struct ast_bridge_channel *bridge_channel_alloc(struct ast_bridge *bridge)
1072 struct ast_bridge_channel *bridge_channel = ao2_alloc(sizeof(struct ast_bridge_channel), bridge_channel_destroy);
1073 if (!(bridge_channel)) {
1076 ast_cond_init(&bridge_channel->cond, NULL);
1078 bridge_channel->bridge = bridge;
1079 ao2_ref(bridge_channel->bridge, +1);
1081 return bridge_channel;
1084 enum ast_bridge_channel_state ast_bridge_join(struct ast_bridge *bridge,
1085 struct ast_channel *chan,
1086 struct ast_channel *swap,
1087 struct ast_bridge_features *features,
1088 struct ast_bridge_tech_optimizations *tech_args)
1090 struct ast_bridge_channel *bridge_channel = bridge_channel_alloc(bridge);
1091 enum ast_bridge_channel_state state = AST_BRIDGE_CHANNEL_STATE_HANGUP;
1093 if (!bridge_channel) {
1097 memcpy(&bridge_channel->tech_args, tech_args, sizeof(bridge_channel->tech_args));
1100 /* Initialize various other elements of the bridge channel structure that we can't do above */
1101 bridge_channel->chan = chan;
1102 bridge_channel->swap = swap;
1103 bridge_channel->features = features;
1105 state = bridge_channel_join(bridge_channel);
1107 /* Cleanup all the data in the bridge channel after it leaves the bridge. */
1108 ao2_lock(bridge_channel);
1109 bridge_channel->chan = NULL;
1110 bridge_channel->swap = NULL;
1111 bridge_channel->features = NULL;
1112 ao2_unlock(bridge_channel);
1114 ao2_ref(bridge_channel, -1);
1119 /*! \brief Thread responsible for imparted bridged channels */
1120 static void *bridge_channel_thread(void *data)
1122 struct ast_bridge_channel *bridge_channel = data;
1123 enum ast_bridge_channel_state state;
1125 state = bridge_channel_join(bridge_channel);
1127 /* 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 */
1128 if (bridge_channel->allow_impart_hangup && (state == AST_BRIDGE_CHANNEL_STATE_END || state == AST_BRIDGE_CHANNEL_STATE_HANGUP)) {
1129 ast_hangup(bridge_channel->chan);
1133 ao2_lock(bridge_channel);
1134 bridge_channel->chan = NULL;
1135 bridge_channel->swap = NULL;
1136 bridge_channel->features = NULL;
1137 ao2_unlock(bridge_channel);
1139 ao2_ref(bridge_channel, -1);
1144 int ast_bridge_impart(struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap, struct ast_bridge_features *features, int allow_hangup)
1146 struct ast_bridge_channel *bridge_channel = bridge_channel_alloc(bridge);
1147 /* Try to allocate a structure for the bridge channel */
1148 if (!(bridge_channel)) {
1152 /* Setup various parameters */
1153 bridge_channel->chan = chan;
1154 bridge_channel->swap = swap;
1155 bridge_channel->features = features;
1156 bridge_channel->allow_impart_hangup = allow_hangup;
1159 /* Actually create the thread that will handle the channel */
1160 if (ast_pthread_create(&bridge_channel->thread, NULL, bridge_channel_thread, bridge_channel)) {
1161 ao2_ref(bridge_channel, -1);
1168 int ast_bridge_depart(struct ast_bridge *bridge, struct ast_channel *chan)
1170 struct ast_bridge_channel *bridge_channel = NULL;
1175 /* Try to find the channel that we want to depart */
1176 if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1181 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_DEPART);
1182 thread = bridge_channel->thread;
1186 pthread_join(thread, NULL);
1191 int ast_bridge_remove(struct ast_bridge *bridge, struct ast_channel *chan)
1193 struct ast_bridge_channel *bridge_channel = NULL;
1197 /* Try to find the channel that we want to remove */
1198 if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1203 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
1210 int ast_bridge_merge(struct ast_bridge *bridge0, struct ast_bridge *bridge1)
1212 struct ast_bridge_channel *bridge_channel = NULL;
1217 /* If the first bridge currently has 2 channels and is not capable of becoming a multimixing bridge we can not merge */
1218 if ((bridge0->num + bridge1->num) > 2 && (!(bridge0->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) && !ast_test_flag(&bridge0->feature_flags, AST_BRIDGE_FLAG_SMART))) {
1219 ao2_unlock(bridge1);
1220 ao2_unlock(bridge0);
1221 ast_debug(1, "Can't merge bridge %p into bridge %p, multimix is needed and it could not be acquired.\n", bridge1, bridge0);
1225 ast_debug(1, "Merging channels from bridge %p into bridge %p\n", bridge1, bridge0);
1227 /* Perform smart bridge operation on bridge we are merging into so it can change bridge technology if needed */
1228 if (smart_bridge_operation(bridge0, NULL, bridge0->num + bridge1->num)) {
1229 ao2_unlock(bridge1);
1230 ao2_unlock(bridge0);
1231 ast_debug(1, "Can't merge bridge %p into bridge %p, tried to perform smart bridge operation and failed.\n", bridge1, bridge0);
1235 /* If a thread is currently executing on bridge1 tell it to stop */
1236 if (bridge1->thread) {
1237 ast_debug(1, "Telling bridge thread on bridge %p to stop as it is being merged into %p\n", bridge1, bridge0);
1238 bridge1->thread = AST_PTHREADT_STOP;
1241 /* Move channels from bridge1 over to bridge0 */
1242 while ((bridge_channel = AST_LIST_REMOVE_HEAD(&bridge1->channels, entry))) {
1243 /* Tell the technology handling bridge1 that the bridge channel is leaving */
1244 if (bridge1->technology->leave) {
1245 ast_debug(1, "Giving bridge technology %s notification that %p is leaving bridge %p\n", bridge1->technology->name, bridge_channel, bridge1);
1246 if (bridge1->technology->leave(bridge1, bridge_channel)) {
1247 ast_debug(1, "Bridge technology %s failed to allow %p to leave bridge %p\n", bridge1->technology->name, bridge_channel, bridge1);
1251 /* Drop channel count and reference count on the bridge they are leaving */
1253 ao2_ref(bridge1, -1);
1255 bridge_array_remove(bridge1, bridge_channel->chan);
1257 /* Now add them into the bridge they are joining, increase channel count, and bump up reference count */
1258 bridge_channel->bridge = bridge0;
1259 AST_LIST_INSERT_TAIL(&bridge0->channels, bridge_channel, entry);
1261 ao2_ref(bridge0, +1);
1263 bridge_array_add(bridge0, bridge_channel->chan);
1265 /* Make the channel compatible with the new bridge it is joining or else formats would go amuck */
1266 bridge_make_compatible(bridge0, bridge_channel);
1268 /* Tell the technology handling bridge0 that the bridge channel is joining */
1269 if (bridge0->technology->join) {
1270 ast_debug(1, "Giving bridge technology %s notification that %p is joining bridge %p\n", bridge0->technology->name, bridge_channel, bridge0);
1271 if (bridge0->technology->join(bridge0, bridge_channel)) {
1272 ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n", bridge0->technology->name, bridge_channel, bridge0);
1276 /* Poke the bridge channel, this will cause it to wake up and execute the proper threading model for the new bridge it is in */
1277 pthread_kill(bridge_channel->thread, SIGURG);
1278 ao2_lock(bridge_channel);
1279 ast_cond_signal(&bridge_channel->cond);
1280 ao2_unlock(bridge_channel);
1283 ast_debug(1, "Merged channels from bridge %p into bridge %p\n", bridge1, bridge0);
1285 ao2_unlock(bridge1);
1286 ao2_unlock(bridge0);
1291 int ast_bridge_suspend(struct ast_bridge *bridge, struct ast_channel *chan)
1293 struct ast_bridge_channel *bridge_channel;
1297 if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1302 bridge_channel_suspend(bridge, bridge_channel);
1309 int ast_bridge_unsuspend(struct ast_bridge *bridge, struct ast_channel *chan)
1311 struct ast_bridge_channel *bridge_channel;
1315 if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1320 bridge_channel_unsuspend(bridge, bridge_channel);
1327 void ast_bridge_technology_suspend(struct ast_bridge_technology *technology)
1329 technology->suspended = 1;
1333 void ast_bridge_technology_unsuspend(struct ast_bridge_technology *technology)
1335 technology->suspended = 0;
1339 int ast_bridge_features_register(enum ast_bridge_builtin_feature feature, ast_bridge_features_hook_callback callback, const char *dtmf)
1341 if (builtin_features_handlers[feature]) {
1345 if (!ast_strlen_zero(dtmf)) {
1346 ast_copy_string(builtin_features_dtmf[feature], dtmf, sizeof(builtin_features_dtmf[feature]));
1349 builtin_features_handlers[feature] = callback;
1354 int ast_bridge_features_unregister(enum ast_bridge_builtin_feature feature)
1356 if (!builtin_features_handlers[feature]) {
1360 builtin_features_handlers[feature] = NULL;
1365 int ast_bridge_features_hook(struct ast_bridge_features *features,
1367 ast_bridge_features_hook_callback callback,
1369 ast_bridge_features_hook_pvt_destructor destructor)
1371 struct ast_bridge_features_hook *hook = NULL;
1373 /* Allocate new memory and setup it's various variables */
1374 if (!(hook = ast_calloc(1, sizeof(*hook)))) {
1378 ast_copy_string(hook->dtmf, dtmf, sizeof(hook->dtmf));
1379 hook->callback = callback;
1380 hook->destructor = destructor;
1381 hook->hook_pvt = hook_pvt;
1383 /* Once done we add it onto the list. Now it will be picked up when DTMF is used */
1384 AST_LIST_INSERT_TAIL(&features->hooks, hook, entry);
1386 features->usable = 1;
1391 int ast_bridge_features_set_talk_detector(struct ast_bridge_features *features,
1392 ast_bridge_talking_indicate_callback talker_cb,
1393 ast_bridge_talking_indicate_destructor talker_destructor,
1396 features->talker_cb = talker_cb;
1397 features->talker_destructor_cb = talker_destructor;
1398 features->talker_pvt_data = pvt_data;
1402 int ast_bridge_features_enable(struct ast_bridge_features *features, enum ast_bridge_builtin_feature feature, const char *dtmf, void *config)
1404 /* If no alternate DTMF stream was provided use the default one */
1405 if (ast_strlen_zero(dtmf)) {
1406 dtmf = builtin_features_dtmf[feature];
1407 /* If no DTMF is still available (ie: it has been disabled) then error out now */
1408 if (ast_strlen_zero(dtmf)) {
1409 ast_debug(1, "Failed to enable built in feature %d on %p, no DTMF string is available for it.\n", feature, features);
1414 if (!builtin_features_handlers[feature]) {
1418 /* The rest is basically pretty easy. We create another hook using the built in feature's callback and DTMF, easy as pie. */
1419 return ast_bridge_features_hook(features, dtmf, builtin_features_handlers[feature], config, NULL);
1422 int ast_bridge_features_set_flag(struct ast_bridge_features *features, enum ast_bridge_feature_flags flag)
1424 ast_set_flag(&features->feature_flags, flag);
1425 features->usable = 1;
1429 int ast_bridge_features_init(struct ast_bridge_features *features)
1431 /* Zero out the structure */
1432 memset(features, 0, sizeof(*features));
1434 /* Initialize the hooks list, just in case */
1435 AST_LIST_HEAD_INIT_NOLOCK(&features->hooks);
1440 int ast_bridge_features_cleanup(struct ast_bridge_features *features)
1442 struct ast_bridge_features_hook *hook = NULL;
1444 /* This is relatively simple, hooks are kept as a list on the features structure so we just pop them off and free them */
1445 while ((hook = AST_LIST_REMOVE_HEAD(&features->hooks, entry))) {
1446 if (hook->destructor) {
1447 hook->destructor(hook->hook_pvt);
1451 if (features->talker_destructor_cb && features->talker_pvt_data) {
1452 features->talker_destructor_cb(features->talker_pvt_data);
1453 features->talker_pvt_data = NULL;
1459 int ast_bridge_dtmf_stream(struct ast_bridge *bridge, const char *dtmf, struct ast_channel *chan)
1461 struct ast_bridge_channel *bridge_channel = NULL;
1465 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1466 if (bridge_channel->chan == chan) {
1469 ast_copy_string(bridge_channel->dtmf_stream_q, dtmf, sizeof(bridge_channel->dtmf_stream_q));
1470 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_DTMF);
1478 void ast_bridge_set_mixing_interval(struct ast_bridge *bridge, unsigned int mixing_interval)
1481 bridge->internal_mixing_interval = mixing_interval;
1485 void ast_bridge_set_internal_sample_rate(struct ast_bridge *bridge, unsigned int sample_rate)
1489 bridge->internal_sample_rate = sample_rate;
1493 static void cleanup_video_mode(struct ast_bridge *bridge)
1495 switch (bridge->video_mode.mode) {
1496 case AST_BRIDGE_VIDEO_MODE_NONE:
1498 case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
1499 if (bridge->video_mode.mode_data.single_src_data.chan_vsrc) {
1500 ast_channel_unref(bridge->video_mode.mode_data.single_src_data.chan_vsrc);
1503 case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
1504 if (bridge->video_mode.mode_data.talker_src_data.chan_vsrc) {
1505 ast_channel_unref(bridge->video_mode.mode_data.talker_src_data.chan_vsrc);
1507 if (bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc) {
1508 ast_channel_unref(bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc);
1511 memset(&bridge->video_mode, 0, sizeof(bridge->video_mode));
1514 void ast_bridge_set_single_src_video_mode(struct ast_bridge *bridge, struct ast_channel *video_src_chan)
1517 cleanup_video_mode(bridge);
1518 bridge->video_mode.mode = AST_BRIDGE_VIDEO_MODE_SINGLE_SRC;
1519 bridge->video_mode.mode_data.single_src_data.chan_vsrc = ast_channel_ref(video_src_chan);
1520 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));
1521 ast_indicate(video_src_chan, AST_CONTROL_VIDUPDATE);
1525 void ast_bridge_set_talker_src_video_mode(struct ast_bridge *bridge)
1528 cleanup_video_mode(bridge);
1529 bridge->video_mode.mode = AST_BRIDGE_VIDEO_MODE_TALKER_SRC;
1530 ast_test_suite_event_notify("BRIDGE_VIDEO_MODE", "Message: video mode set to talker source\r\nVideo Mode: %d", bridge->video_mode.mode);
1534 void ast_bridge_update_talker_src_video_mode(struct ast_bridge *bridge, struct ast_channel *chan, int talker_energy, int is_keyframe)
1536 struct ast_bridge_video_talker_src_data *data;
1537 /* If the channel doesn't support video, we don't care about it */
1538 if (!ast_format_cap_has_type(ast_channel_nativeformats(chan), AST_FORMAT_TYPE_VIDEO)) {
1543 data = &bridge->video_mode.mode_data.talker_src_data;
1545 if (data->chan_vsrc == chan) {
1546 data->average_talking_energy = talker_energy;
1547 } else if ((data->average_talking_energy < talker_energy) && is_keyframe) {
1548 if (data->chan_old_vsrc) {
1549 ast_channel_unref(data->chan_old_vsrc);
1551 if (data->chan_vsrc) {
1552 data->chan_old_vsrc = data->chan_vsrc;
1553 ast_indicate(data->chan_old_vsrc, AST_CONTROL_VIDUPDATE);
1555 data->chan_vsrc = ast_channel_ref(chan);
1556 data->average_talking_energy = talker_energy;
1557 ast_test_suite_event_notify("BRIDGE_VIDEO_SRC", "Message: video source updated\r\nVideo Channel: %s", ast_channel_name(data->chan_vsrc));
1558 ast_indicate(data->chan_vsrc, AST_CONTROL_VIDUPDATE);
1559 } else if ((data->average_talking_energy < talker_energy) && !is_keyframe) {
1560 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
1561 } else if (!data->chan_vsrc && is_keyframe) {
1562 data->chan_vsrc = ast_channel_ref(chan);
1563 data->average_talking_energy = talker_energy;
1564 ast_test_suite_event_notify("BRIDGE_VIDEO_SRC", "Message: video source updated\r\nVideo Channel: %s", ast_channel_name(data->chan_vsrc));
1565 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
1566 } else if (!data->chan_old_vsrc && is_keyframe) {
1567 data->chan_old_vsrc = ast_channel_ref(chan);
1568 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
1573 int ast_bridge_number_video_src(struct ast_bridge *bridge)
1578 switch (bridge->video_mode.mode) {
1579 case AST_BRIDGE_VIDEO_MODE_NONE:
1581 case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
1582 if (bridge->video_mode.mode_data.single_src_data.chan_vsrc) {
1586 case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
1587 if (bridge->video_mode.mode_data.talker_src_data.chan_vsrc) {
1590 if (bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc) {
1598 int ast_bridge_is_video_src(struct ast_bridge *bridge, struct ast_channel *chan)
1603 switch (bridge->video_mode.mode) {
1604 case AST_BRIDGE_VIDEO_MODE_NONE:
1606 case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
1607 if (bridge->video_mode.mode_data.single_src_data.chan_vsrc == chan) {
1611 case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
1612 if (bridge->video_mode.mode_data.talker_src_data.chan_vsrc == chan) {
1614 } else if (bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc == chan) {
1623 void ast_bridge_remove_video_src(struct ast_bridge *bridge, struct ast_channel *chan)
1626 switch (bridge->video_mode.mode) {
1627 case AST_BRIDGE_VIDEO_MODE_NONE:
1629 case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
1630 if (bridge->video_mode.mode_data.single_src_data.chan_vsrc == chan) {
1631 if (bridge->video_mode.mode_data.single_src_data.chan_vsrc) {
1632 ast_channel_unref(bridge->video_mode.mode_data.single_src_data.chan_vsrc);
1634 bridge->video_mode.mode_data.single_src_data.chan_vsrc = NULL;
1637 case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
1638 if (bridge->video_mode.mode_data.talker_src_data.chan_vsrc == chan) {
1639 if (bridge->video_mode.mode_data.talker_src_data.chan_vsrc) {
1640 ast_channel_unref(bridge->video_mode.mode_data.talker_src_data.chan_vsrc);
1642 bridge->video_mode.mode_data.talker_src_data.chan_vsrc = NULL;
1643 bridge->video_mode.mode_data.talker_src_data.average_talking_energy = 0;
1645 if (bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc == chan) {
1646 if (bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc) {
1647 ast_channel_unref(bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc);
1649 bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc = NULL;