20cfb7999929e754ba92728d14fa6062f443a3c1
[asterisk/asterisk.git] / main / bridging.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007 - 2009, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Channel Bridging API
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <signal.h>
35
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"
49
50 static AST_RWLIST_HEAD_STATIC(bridge_technologies, ast_bridge_technology);
51
52 /* Initial starting point for the bridge array of channels */
53 #define BRIDGE_ARRAY_START 128
54
55 /* Grow rate of bridge array of channels */
56 #define BRIDGE_ARRAY_GROW 32
57
58 static void cleanup_video_mode(struct ast_bridge *bridge);
59
60 /*! Default DTMF keys for built in features */
61 static char builtin_features_dtmf[AST_BRIDGE_BUILTIN_END][MAXIMUM_DTMF_FEATURE_STRING];
62
63 /*! Function handlers for the built in features */
64 static void *builtin_features_handlers[AST_BRIDGE_BUILTIN_END];
65
66 int __ast_bridge_technology_register(struct ast_bridge_technology *technology, struct ast_module *module)
67 {
68         struct ast_bridge_technology *current = NULL;
69
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);
73                 return -1;
74         }
75
76         AST_RWLIST_WRLOCK(&bridge_technologies);
77
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);
83                         return -1;
84                 }
85         }
86
87         /* Copy module pointer so reference counting can keep the module from unloading */
88         technology->mod = module;
89
90         /* Insert our new bridge technology into the list and print out a pretty message */
91         AST_RWLIST_INSERT_TAIL(&bridge_technologies, technology, entry);
92
93         AST_RWLIST_UNLOCK(&bridge_technologies);
94
95         ast_verb(2, "Registered bridge technology %s\n", technology->name);
96
97         return 0;
98 }
99
100 int ast_bridge_technology_unregister(struct ast_bridge_technology *technology)
101 {
102         struct ast_bridge_technology *current = NULL;
103
104         AST_RWLIST_WRLOCK(&bridge_technologies);
105
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);
111                         break;
112                 }
113         }
114         AST_RWLIST_TRAVERSE_SAFE_END;
115
116         AST_RWLIST_UNLOCK(&bridge_technologies);
117
118         return current ? 0 : -1;
119 }
120
121 static void bridge_channel_poke(struct ast_bridge_channel *bridge_channel)
122 {
123         ao2_lock(bridge_channel);
124         pthread_kill(bridge_channel->thread, SIGURG);
125         ast_cond_signal(&bridge_channel->cond);
126         ao2_unlock(bridge_channel);
127 }
128
129 /*! \note This function assumes the bridge_channel is locked. */
130 static void ast_bridge_change_state_nolock(struct ast_bridge_channel *bridge_channel, enum ast_bridge_channel_state new_state)
131 {
132         /* Change the state on the bridge channel */
133         bridge_channel->state = new_state;
134
135         /* Only poke the channel's thread if it is not us */
136         if (!pthread_equal(pthread_self(), bridge_channel->thread)) {
137                 pthread_kill(bridge_channel->thread, SIGURG);
138                 ast_cond_signal(&bridge_channel->cond);
139         }
140 }
141
142 void ast_bridge_change_state(struct ast_bridge_channel *bridge_channel, enum ast_bridge_channel_state new_state)
143 {
144         ao2_lock(bridge_channel);
145         ast_bridge_change_state_nolock(bridge_channel, new_state);
146         ao2_unlock(bridge_channel);
147 }
148
149 /*!
150  * \brief Helper function to poke the bridge thread
151  *
152  * \note This function assumes the bridge is locked.
153  */
154 static void bridge_poke(struct ast_bridge *bridge)
155 {
156         /* Poke the thread just in case */
157         if (bridge->thread != AST_PTHREADT_NULL && bridge->thread != AST_PTHREADT_STOP) {
158                 pthread_kill(bridge->thread, SIGURG);
159         }
160 }
161
162 /*!
163  * \internal
164  * \brief Stop the bridge.
165  * \since 12.0.0
166  *
167  * \note This function assumes the bridge is locked.
168  *
169  * \return Nothing
170  */
171 static void bridge_stop(struct ast_bridge *bridge)
172 {
173         pthread_t thread = bridge->thread;
174
175         bridge->stop = 1;
176         bridge_poke(bridge);
177         ao2_unlock(bridge);
178         pthread_join(thread, NULL);
179         ao2_lock(bridge);
180 }
181
182 /*!
183  * \brief Helper function to add a channel to the bridge array
184  *
185  * \note This function assumes the bridge is locked.
186  */
187 static void bridge_array_add(struct ast_bridge *bridge, struct ast_channel *chan)
188 {
189         /* We have to make sure the bridge thread is not using the bridge array before messing with it */
190         while (bridge->waiting) {
191                 bridge_poke(bridge);
192                 sched_yield();
193         }
194
195         bridge->array[bridge->array_num++] = chan;
196
197         ast_debug(1, "Added channel %s(%p) to bridge array on %p, new count is %d\n",
198                 ast_channel_name(chan), chan, bridge, (int) bridge->array_num);
199
200         /* If the next addition of a channel will exceed our array size grow it out */
201         if (bridge->array_num == bridge->array_size) {
202                 struct ast_channel **new_array;
203
204                 ast_debug(1, "Growing bridge array on %p from %d to %d\n",
205                         bridge, (int) bridge->array_size, (int) bridge->array_size + BRIDGE_ARRAY_GROW);
206                 new_array = ast_realloc(bridge->array,
207                         (bridge->array_size + BRIDGE_ARRAY_GROW) * sizeof(*bridge->array));
208                 if (!new_array) {
209                         return;
210                 }
211                 bridge->array = new_array;
212                 bridge->array_size += BRIDGE_ARRAY_GROW;
213         }
214 }
215
216 /*! \brief Helper function to remove a channel from the bridge array
217  *
218  * \note This function assumes the bridge is locked.
219  */
220 static void bridge_array_remove(struct ast_bridge *bridge, struct ast_channel *chan)
221 {
222         int idx;
223
224         /* We have to make sure the bridge thread is not using the bridge array before messing with it */
225         while (bridge->waiting) {
226                 bridge_poke(bridge);
227                 sched_yield();
228         }
229
230         for (idx = 0; idx < bridge->array_num; ++idx) {
231                 if (bridge->array[idx] == chan) {
232                         --bridge->array_num;
233                         bridge->array[idx] = bridge->array[bridge->array_num];
234                         ast_debug(1, "Removed channel %p from bridge array on %p, new count is %d\n",
235                                 chan, bridge, (int) bridge->array_num);
236                         break;
237                 }
238         }
239 }
240
241 /*! \brief Helper function to find a bridge channel given a channel */
242 static struct ast_bridge_channel *find_bridge_channel(struct ast_bridge *bridge, struct ast_channel *chan)
243 {
244         struct ast_bridge_channel *bridge_channel;
245
246         AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
247                 if (bridge_channel->chan == chan) {
248                         break;
249                 }
250         }
251
252         return bridge_channel;
253 }
254
255 /*!
256  * \internal
257  * \brief Force out all channels that are not already going out of the bridge.
258  * \since 12.0.0
259  *
260  * \param bridge Bridge to eject all channels
261  *
262  * \note On entry, bridge is already locked.
263  *
264  * \return Nothing
265  */
266 static void bridge_force_out_all(struct ast_bridge *bridge)
267 {
268         struct ast_bridge_channel *bridge_channel;
269
270         AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
271                 ao2_lock(bridge_channel);
272                 switch (bridge_channel->state) {
273                 case AST_BRIDGE_CHANNEL_STATE_END:
274                 case AST_BRIDGE_CHANNEL_STATE_HANGUP:
275                 case AST_BRIDGE_CHANNEL_STATE_DEPART:
276                         break;
277                 default:
278                         ast_bridge_change_state_nolock(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
279                         break;
280                 }
281                 ao2_unlock(bridge_channel);
282         }
283 }
284
285 /*! \brief Internal function to see whether a bridge should dissolve, and if so do it */
286 static void bridge_check_dissolve(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
287 {
288         if (!ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE)
289                 && (!bridge_channel->features
290                         || !bridge_channel->features->usable
291                         || !ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_FLAG_DISSOLVE))) {
292                 return;
293         }
294
295         ast_debug(1, "Dissolving bridge %p\n", bridge);
296         bridge_force_out_all(bridge);
297 }
298
299 /*! \brief Internal function to handle DTMF from a channel */
300 static struct ast_frame *bridge_handle_dtmf(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
301 {
302         struct ast_bridge_features *features = (bridge_channel->features ? bridge_channel->features : &bridge->features);
303         struct ast_bridge_features_hook *hook;
304
305         /* If the features structure we grabbed is not usable immediately return the frame */
306         if (!features->usable) {
307                 return frame;
308         }
309
310         /* 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 */
311         AST_LIST_TRAVERSE(&features->hooks, hook, entry) {
312                 if (hook->dtmf[0] == frame->subclass.integer) {
313                         ast_frfree(frame);
314                         frame = NULL;
315                         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_FEATURE);
316                         break;
317                 }
318         }
319
320         return frame;
321 }
322
323 /*! \brief Internal function used to determine whether a control frame should be dropped or not */
324 static int bridge_drop_control_frame(int subclass)
325 {
326         switch (subclass) {
327         case AST_CONTROL_ANSWER:
328         case -1:
329                 return 1;
330         default:
331                 return 0;
332         }
333 }
334
335 void ast_bridge_notify_talking(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, int started_talking)
336 {
337         if (started_talking) {
338                 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_START_TALKING);
339         } else {
340                 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_STOP_TALKING);
341         }
342 }
343
344 void ast_bridge_handle_trip(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_channel *chan, int outfd)
345 {
346         /* If no bridge channel has been provided and the actual channel has been provided find it */
347         if (chan && !bridge_channel) {
348                 bridge_channel = find_bridge_channel(bridge, chan);
349         }
350
351         /* If a bridge channel with actual channel is present read a frame and handle it */
352         if (chan && bridge_channel) {
353                 struct ast_frame *frame;
354
355                 if (bridge->features.mute
356                         || (bridge_channel->features && bridge_channel->features->mute)) {
357                         frame = ast_read_noaudio(chan);
358                 } else {
359                         frame = ast_read(chan);
360                 }
361                 /* This is pretty simple... see if they hung up */
362                 if (!frame || (frame->frametype == AST_FRAME_CONTROL && frame->subclass.integer == AST_CONTROL_HANGUP)) {
363                         /* Signal the thread that is handling the bridged channel that it should be ended */
364                         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
365                 } else if (frame->frametype == AST_FRAME_CONTROL && bridge_drop_control_frame(frame->subclass.integer)) {
366                         ast_debug(1, "Dropping control frame %d from bridge channel %p\n",
367                                 frame->subclass.integer, bridge_channel);
368                 } else if (frame->frametype == AST_FRAME_DTMF_BEGIN || frame->frametype == AST_FRAME_DTMF_END) {
369                         int dtmf_passthrough = bridge_channel->features ?
370                                 bridge_channel->features->dtmf_passthrough :
371                                 bridge->features.dtmf_passthrough;
372
373                         if (frame->frametype == AST_FRAME_DTMF_BEGIN) {
374                                 frame = bridge_handle_dtmf(bridge, bridge_channel, frame);
375                         }
376
377                         if (frame && dtmf_passthrough) {
378                                 bridge->technology->write(bridge, bridge_channel, frame);
379                         }
380                 } else {
381                         /* Simply write the frame out to the bridge technology if it still exists */
382                         bridge->technology->write(bridge, bridge_channel, frame);
383                 }
384
385                 if (frame) {
386                         ast_frfree(frame);
387                 }
388                 return;
389         }
390
391         /* If a file descriptor actually tripped pass it off to the bridge technology */
392         if (outfd > -1 && bridge->technology->fd) {
393                 bridge->technology->fd(bridge, bridge_channel, outfd);
394                 return;
395         }
396
397         /* If all else fails just poke the bridge */
398         if (bridge->technology->poke && bridge_channel) {
399                 bridge->technology->poke(bridge, bridge_channel);
400                 return;
401         }
402 }
403
404 /*! \brief Generic thread loop, TODO: Rethink this/improve it */
405 static int generic_thread_loop(struct ast_bridge *bridge)
406 {
407         while (!bridge->stop && !bridge->refresh && bridge->array_num) {
408                 struct ast_channel *winner;
409                 int to = -1;
410
411                 /* Move channels around for priority reasons if we have more than one channel in our array */
412                 if (bridge->array_num > 1) {
413                         struct ast_channel *first = bridge->array[0];
414                         memmove(bridge->array, bridge->array + 1, sizeof(struct ast_channel *) * (bridge->array_num - 1));
415                         bridge->array[(bridge->array_num - 1)] = first;
416                 }
417
418                 /* Wait on the channels */
419                 bridge->waiting = 1;
420                 ao2_unlock(bridge);
421                 winner = ast_waitfor_n(bridge->array, (int) bridge->array_num, &to);
422                 bridge->waiting = 0;
423                 ao2_lock(bridge);
424
425                 /* Process whatever they did */
426                 ast_bridge_handle_trip(bridge, NULL, winner, -1);
427         }
428
429         return 0;
430 }
431
432 /*! \brief Bridge thread function */
433 static void *bridge_thread(void *data)
434 {
435         struct ast_bridge *bridge = data;
436         int res = 0;
437
438         ao2_lock(bridge);
439
440         if (bridge->callid) {
441                 ast_callid_threadassoc_add(bridge->callid);
442         }
443
444         ast_debug(1, "Started bridge thread for %p\n", bridge);
445
446         /* Loop around until we are told to stop */
447         while (!bridge->stop && bridge->array_num && !res) {
448                 /* In case the refresh bit was set simply set it back to off */
449                 bridge->refresh = 0;
450
451                 ast_debug(1, "Launching bridge thread function %p for bridge %p\n",
452                         bridge->technology->thread ? bridge->technology->thread : generic_thread_loop,
453                         bridge);
454
455                 /*
456                  * Execute the appropriate thread function.  If the technology
457                  * does not provide one we use the generic one.
458                  */
459                 res = bridge->technology->thread
460                         ? bridge->technology->thread(bridge)
461                         : generic_thread_loop(bridge);
462         }
463
464         ast_debug(1, "Ending bridge thread for %p\n", bridge);
465
466         /* Indicate the bridge thread is no longer active */
467         bridge->thread = AST_PTHREADT_NULL;
468         ao2_unlock(bridge);
469
470         ao2_ref(bridge, -1);
471
472         return NULL;
473 }
474
475 /*! \brief Helper function used to find the "best" bridge technology given a specified capabilities */
476 static struct ast_bridge_technology *find_best_technology(uint32_t capabilities)
477 {
478         struct ast_bridge_technology *current = NULL, *best = NULL;
479
480         AST_RWLIST_RDLOCK(&bridge_technologies);
481         AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
482                 if (current->suspended) {
483                         ast_debug(1, "Bridge technology %s is suspended. Skipping.\n", current->name);
484                         continue;
485                 }
486                 if (!(current->capabilities & capabilities)) {
487                         ast_debug(1, "Bridge technology %s does not have the capabilities we need.\n", current->name);
488                         continue;
489                 }
490                 if (best && best->preference < current->preference) {
491                         ast_debug(1, "Bridge technology %s has preference %d while %s has preference %d. Skipping.\n", current->name, current->preference, best->name, best->preference);
492                         continue;
493                 }
494                 best = current;
495         }
496
497         if (best) {
498                 /* Increment it's module reference count if present so it does not get unloaded while in use */
499                 ast_module_ref(best->mod);
500                 ast_debug(1, "Chose bridge technology %s\n", best->name);
501         }
502
503         AST_RWLIST_UNLOCK(&bridge_technologies);
504
505         return best;
506 }
507
508 static void destroy_bridge(void *obj)
509 {
510         struct ast_bridge *bridge = obj;
511
512         ast_debug(1, "Actually destroying bridge %p, nobody wants it anymore\n", bridge);
513
514         /* There should not be any channels left in the bridge. */
515         ast_assert(AST_LIST_EMPTY(&bridge->channels));
516
517         /* Pass off the bridge to the technology to destroy if needed */
518         if (bridge->technology->destroy) {
519                 ast_debug(1, "Giving bridge technology %s the bridge structure %p to destroy\n", bridge->technology->name, bridge);
520                 if (bridge->technology->destroy(bridge)) {
521                         ast_debug(1, "Bridge technology %s failed to destroy bridge structure %p... some memory may have leaked\n",
522                                 bridge->technology->name, bridge);
523                 }
524         }
525
526         cleanup_video_mode(bridge);
527
528         /* Clean up the features configuration */
529         ast_bridge_features_cleanup(&bridge->features);
530
531         /* We are no longer using the bridge technology so decrement the module reference count on it */
532         ast_module_unref(bridge->technology->mod);
533
534         /* Drop the array of channels */
535         ast_free(bridge->array);
536 }
537
538 struct ast_bridge *ast_bridge_new(uint32_t capabilities, int flags)
539 {
540         struct ast_bridge *bridge;
541         struct ast_bridge_technology *bridge_technology;
542
543         /* If we need to be a smart bridge see if we can move between 1to1 and multimix bridges */
544         if (flags & AST_BRIDGE_FLAG_SMART) {
545                 if (!ast_bridge_check((capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX)
546                         ? AST_BRIDGE_CAPABILITY_MULTIMIX : AST_BRIDGE_CAPABILITY_1TO1MIX)) {
547                         return NULL;
548                 }
549         }
550
551         /*
552          * If capabilities were provided use our helper function to find
553          * the "best" bridge technology, otherwise we can just look for
554          * the most basic capability needed, single 1to1 mixing.
555          */
556         bridge_technology = capabilities
557                 ? find_best_technology(capabilities)
558                 : find_best_technology(AST_BRIDGE_CAPABILITY_1TO1MIX);
559
560         /* If no bridge technology was found we can't possibly do bridging so fail creation of the bridge */
561         if (!bridge_technology) {
562                 return NULL;
563         }
564
565         /* We have everything we need to create this bridge... so allocate the memory, link things together, and fire her up! */
566         bridge = ao2_alloc(sizeof(*bridge), destroy_bridge);
567         if (!bridge) {
568                 ast_module_unref(bridge_technology->mod);
569                 return NULL;
570         }
571
572         bridge->technology = bridge_technology;
573         bridge->thread = AST_PTHREADT_NULL;
574
575         /* Create an array of pointers for the channels that will be joining us */
576         bridge->array = ast_malloc(BRIDGE_ARRAY_START * sizeof(*bridge->array));
577         if (!bridge->array) {
578                 ao2_ref(bridge, -1);
579                 return NULL;
580         }
581         bridge->array_size = BRIDGE_ARRAY_START;
582
583         ast_set_flag(&bridge->feature_flags, flags);
584
585         /* Pass off the bridge to the technology to manipulate if needed */
586         if (bridge->technology->create) {
587                 ast_debug(1, "Giving bridge technology %s the bridge structure %p to setup\n", bridge->technology->name, bridge);
588                 if (bridge->technology->create(bridge)) {
589                         ast_debug(1, "Bridge technology %s failed to setup bridge structure %p\n", bridge->technology->name, bridge);
590                         ao2_ref(bridge, -1);
591                         return NULL;
592                 }
593         }
594
595         return bridge;
596 }
597
598 int ast_bridge_check(uint32_t capabilities)
599 {
600         struct ast_bridge_technology *bridge_technology;
601
602         if (!(bridge_technology = find_best_technology(capabilities))) {
603                 return 0;
604         }
605
606         ast_module_unref(bridge_technology->mod);
607
608         return 1;
609 }
610
611 int ast_bridge_destroy(struct ast_bridge *bridge)
612 {
613         ao2_lock(bridge);
614
615         if (bridge->callid) {
616                 bridge->callid = ast_callid_unref(bridge->callid);
617         }
618
619         if (bridge->thread != AST_PTHREADT_NULL) {
620                 bridge_stop(bridge);
621         }
622
623         ast_debug(1, "Telling all channels in bridge %p to leave the party\n", bridge);
624
625         /* Drop every bridged channel, the last one will cause the bridge thread (if it exists) to exit */
626         bridge_force_out_all(bridge);
627
628         ao2_unlock(bridge);
629
630         ao2_ref(bridge, -1);
631
632         return 0;
633 }
634
635 static int bridge_make_compatible(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
636 {
637         struct ast_format formats[2];
638         ast_format_copy(&formats[0], ast_channel_readformat(bridge_channel->chan));
639         ast_format_copy(&formats[1], ast_channel_writeformat(bridge_channel->chan));
640
641         /* Are the formats currently in use something ths bridge can handle? */
642         if (!ast_format_cap_iscompatible(bridge->technology->format_capabilities, ast_channel_readformat(bridge_channel->chan))) {
643                 struct ast_format best_format;
644                 ast_best_codec(bridge->technology->format_capabilities, &best_format);
645
646                 /* Read format is a no go... */
647                 if (option_debug) {
648                         char codec_buf[512];
649                         ast_debug(1, "Bridge technology %s wants to read any of formats %s but channel has %s\n", bridge->technology->name,
650                                 ast_getformatname_multiple(codec_buf, sizeof(codec_buf), bridge->technology->format_capabilities),
651                                 ast_getformatname(&formats[0]));
652                 }
653                 /* Switch read format to the best one chosen */
654                 if (ast_set_read_format(bridge_channel->chan, &best_format)) {
655                         ast_log(LOG_WARNING, "Failed to set channel %s to read format %s\n", ast_channel_name(bridge_channel->chan), ast_getformatname(&best_format));
656                         return -1;
657                 }
658                 ast_debug(1, "Bridge %p put channel %s into read format %s\n", bridge, ast_channel_name(bridge_channel->chan), ast_getformatname(&best_format));
659         } else {
660                 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]));
661         }
662
663         if (!ast_format_cap_iscompatible(bridge->technology->format_capabilities, &formats[1])) {
664                 struct ast_format best_format;
665                 ast_best_codec(bridge->technology->format_capabilities, &best_format);
666
667                 /* Write format is a no go... */
668                 if (option_debug) {
669                         char codec_buf[512];
670                         ast_debug(1, "Bridge technology %s wants to write any of formats %s but channel has %s\n", bridge->technology->name,
671                                 ast_getformatname_multiple(codec_buf, sizeof(codec_buf), bridge->technology->format_capabilities),
672                                 ast_getformatname(&formats[1]));
673                 }
674                 /* Switch write format to the best one chosen */
675                 if (ast_set_write_format(bridge_channel->chan, &best_format)) {
676                         ast_log(LOG_WARNING, "Failed to set channel %s to write format %s\n", ast_channel_name(bridge_channel->chan), ast_getformatname(&best_format));
677                         return -1;
678                 }
679                 ast_debug(1, "Bridge %p put channel %s into write format %s\n", bridge, ast_channel_name(bridge_channel->chan), ast_getformatname(&best_format));
680         } else {
681                 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]));
682         }
683
684         return 0;
685 }
686
687 /*! \brief Perform the smart bridge operation. Basically sees if a new bridge technology should be used instead of the current one. */
688 static int smart_bridge_operation(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, int count)
689 {
690         uint32_t new_capabilities = 0;
691         struct ast_bridge_technology *new_technology;
692         struct ast_bridge_technology *old_technology = bridge->technology;
693         struct ast_bridge temp_bridge = {
694                 .technology = bridge->technology,
695                 .bridge_pvt = bridge->bridge_pvt,
696         };
697         struct ast_bridge_channel *bridge_channel2;
698
699         /* Based on current feature determine whether we want to change bridge technologies or not */
700         if (bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX) {
701                 if (count <= 2) {
702                         ast_debug(1, "Bridge %p channel count (%d) is within limits for bridge technology %s, not performing smart bridge operation.\n",
703                                 bridge, count, bridge->technology->name);
704                         return 0;
705                 }
706                 new_capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX;
707         } else if (bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) {
708                 if (count > 2) {
709                         ast_debug(1, "Bridge %p channel count (%d) is within limits for bridge technology %s, not performing smart bridge operation.\n",
710                                 bridge, count, bridge->technology->name);
711                         return 0;
712                 }
713                 new_capabilities = AST_BRIDGE_CAPABILITY_1TO1MIX;
714         }
715
716         if (!new_capabilities) {
717                 ast_debug(1, "Bridge '%p' has no new capabilities, not performing smart bridge operation.\n", bridge);
718                 return 0;
719         }
720
721         /* Attempt to find a new bridge technology to satisfy the capabilities */
722         if (!(new_technology = find_best_technology(new_capabilities))) {
723                 return -1;
724         }
725
726         ast_debug(1, "Performing smart bridge operation on bridge %p, moving from bridge technology %s to %s\n",
727                 bridge, old_technology->name, new_technology->name);
728
729         /* If a thread is currently executing for the current technology tell it to stop */
730         if (bridge->thread != AST_PTHREADT_NULL) {
731                 /*
732                  * If the new bridge technology also needs a thread simply tell
733                  * the bridge thread to refresh itself.  This has the benefit of
734                  * not incurring the cost/time of tearing down and bringing up a
735                  * new thread.
736                  */
737                 if (new_technology->capabilities & AST_BRIDGE_CAPABILITY_THREAD) {
738                         ast_debug(1, "Telling current bridge thread for bridge %p to refresh\n", bridge);
739                         bridge->refresh = 1;
740                         bridge_poke(bridge);
741                 } else {
742                         ast_debug(1, "Telling current bridge thread for bridge %p to stop\n", bridge);
743                         bridge_stop(bridge);
744                 }
745         }
746
747         /*
748          * Since we are soon going to pass this bridge to a new
749          * technology we need to NULL out the bridge_pvt pointer but
750          * don't worry as it still exists in temp_bridge, ditto for the
751          * old technology.
752          */
753         bridge->bridge_pvt = NULL;
754         bridge->technology = new_technology;
755
756         /* Pass the bridge to the new bridge technology so it can set it up */
757         if (new_technology->create) {
758                 ast_debug(1, "Giving bridge technology %s the bridge structure %p to setup\n",
759                         new_technology->name, bridge);
760                 if (new_technology->create(bridge)) {
761                         ast_debug(1, "Bridge technology %s failed to setup bridge structure %p\n",
762                                 new_technology->name, bridge);
763                 }
764         }
765
766         /* Move existing channels over to the new technology, while taking them away from the old one */
767         AST_LIST_TRAVERSE(&bridge->channels, bridge_channel2, entry) {
768                 /* Skip over channel that initiated the smart bridge operation */
769                 if (bridge_channel == bridge_channel2) {
770                         continue;
771                 }
772
773                 /* First we part them from the old technology */
774                 if (old_technology->leave) {
775                         ast_debug(1, "Giving bridge technology %s notification that %p is leaving bridge %p (really %p)\n",
776                                 old_technology->name, bridge_channel2, &temp_bridge, bridge);
777                         if (old_technology->leave(&temp_bridge, bridge_channel2)) {
778                                 ast_debug(1, "Bridge technology %s failed to allow %p (really %p) to leave bridge %p\n",
779                                         old_technology->name, bridge_channel2, &temp_bridge, bridge);
780                         }
781                 }
782
783                 /* Second we make them compatible again with the bridge */
784                 bridge_make_compatible(bridge, bridge_channel2);
785
786                 /* Third we join them to the new technology */
787                 if (new_technology->join) {
788                         ast_debug(1, "Giving bridge technology %s notification that %p is joining bridge %p\n",
789                                 new_technology->name, bridge_channel2, bridge);
790                         if (new_technology->join(bridge, bridge_channel2)) {
791                                 ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n",
792                                         new_technology->name, bridge_channel2, bridge);
793                         }
794                 }
795
796                 /* Fourth we tell them to wake up so they become aware that the above has happened */
797                 bridge_channel_poke(bridge_channel2);
798         }
799
800         /* 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 */
801         if (old_technology->destroy) {
802                 ast_debug(1, "Giving bridge technology %s the bridge structure %p (really %p) to destroy\n",
803                         old_technology->name, &temp_bridge, bridge);
804                 if (old_technology->destroy(&temp_bridge)) {
805                         ast_debug(1, "Bridge technology %s failed to destroy bridge structure %p (really %p)... some memory may have leaked\n",
806                                 old_technology->name, &temp_bridge, bridge);
807                 }
808         }
809
810         /* Finally if the old technology has module referencing remove our reference, we are no longer going to use it */
811         ast_module_unref(old_technology->mod);
812
813         return 0;
814 }
815
816 /*! \brief Run in a multithreaded model. Each joined channel does writing/reading in their own thread. TODO: Improve */
817 static enum ast_bridge_channel_state bridge_channel_join_multithreaded(struct ast_bridge_channel *bridge_channel)
818 {
819         int fds[4] = { -1, }, nfds = 0, i = 0, outfd = -1, ms = -1;
820         struct ast_channel *chan;
821
822         /* Add any file descriptors we may want to monitor */
823         if (bridge_channel->bridge->technology->fd) {
824                 for (i = 0; i < 4; i ++) {
825                         if (bridge_channel->fds[i] >= 0) {
826                                 fds[nfds++] = bridge_channel->fds[i];
827                         }
828                 }
829         }
830
831         ao2_unlock(bridge_channel->bridge);
832
833         ao2_lock(bridge_channel);
834         /* Wait for data to either come from the channel or us to be signalled */
835         if (!bridge_channel->suspended) {
836                 ao2_unlock(bridge_channel);
837                 ast_debug(10, "Going into a multithreaded waitfor for bridge channel %p of bridge %p\n", bridge_channel, bridge_channel->bridge);
838                 chan = ast_waitfor_nandfds(&bridge_channel->chan, 1, fds, nfds, NULL, &outfd, &ms);
839         } else {
840                 ast_debug(10, "Going into a multithreaded signal wait for bridge channel %p of bridge %p\n", bridge_channel, bridge_channel->bridge);
841                 ast_cond_wait(&bridge_channel->cond, ao2_object_get_lockaddr(bridge_channel));
842                 ao2_unlock(bridge_channel);
843                 chan = NULL;
844         }
845
846         ao2_lock(bridge_channel->bridge);
847
848         if (!bridge_channel->suspended) {
849                 ast_bridge_handle_trip(bridge_channel->bridge, bridge_channel, chan, outfd);
850         }
851
852         return bridge_channel->state;
853 }
854
855 /*! \brief Run in a singlethreaded model. Each joined channel yields itself to the main bridge thread. TODO: Improve */
856 static enum ast_bridge_channel_state bridge_channel_join_singlethreaded(struct ast_bridge_channel *bridge_channel)
857 {
858         ao2_unlock(bridge_channel->bridge);
859         ao2_lock(bridge_channel);
860         if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
861                 ast_debug(1, "Going into a single threaded signal wait for bridge channel %p of bridge %p\n", bridge_channel, bridge_channel->bridge);
862                 ast_cond_wait(&bridge_channel->cond, ao2_object_get_lockaddr(bridge_channel));
863         }
864         ao2_unlock(bridge_channel);
865         ao2_lock(bridge_channel->bridge);
866
867         return bridge_channel->state;
868 }
869
870 /*! \brief Internal function that suspends a channel from a bridge */
871 static void bridge_channel_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
872 {
873         ao2_lock(bridge_channel);
874         bridge_channel->suspended = 1;
875         bridge_array_remove(bridge, bridge_channel->chan);
876         ao2_unlock(bridge_channel);
877
878         if (bridge->technology->suspend) {
879                 bridge->technology->suspend(bridge, bridge_channel);
880         }
881 }
882
883 /*! \brief Internal function that unsuspends a channel from a bridge */
884 static void bridge_channel_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
885 {
886         ao2_lock(bridge_channel);
887         bridge_channel->suspended = 0;
888         bridge_array_add(bridge, bridge_channel->chan);
889         ast_cond_signal(&bridge_channel->cond);
890         ao2_unlock(bridge_channel);
891
892         if (bridge->technology->unsuspend) {
893                 bridge->technology->unsuspend(bridge, bridge_channel);
894         }
895 }
896
897 /*!
898  * \brief Internal function that executes a feature on a bridge channel
899  * \note Neither the bridge nor the bridge_channel locks should be held when entering
900  * this function.
901  */
902 static void bridge_channel_feature(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
903 {
904         struct ast_bridge_features *features = (bridge_channel->features ? bridge_channel->features : &bridge->features);
905         struct ast_bridge_features_hook *hook = NULL;
906         char dtmf[MAXIMUM_DTMF_FEATURE_STRING] = "";
907         int look_for_dtmf = 1, dtmf_len = 0;
908
909         /* 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 */
910         ast_set_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_END_DTMF_ONLY);
911
912         /* Wait for DTMF on the channel and put it into a buffer. If the buffer matches any feature hook execute the hook. */
913         while (look_for_dtmf) {
914                 int res = ast_waitfordigit(bridge_channel->chan, 3000);
915
916                 /* If the above timed out simply exit */
917                 if (!res) {
918                         ast_debug(1, "DTMF feature string collection on bridge channel %p timed out\n", bridge_channel);
919                         break;
920                 } else if (res < 0) {
921                         ast_debug(1, "DTMF feature string collection failed on bridge channel %p for some reason\n", bridge_channel);
922                         break;
923                 }
924
925                 /* Add the above DTMF into the DTMF string so we can do our matching */
926                 dtmf[dtmf_len++] = res;
927
928                 ast_debug(1, "DTMF feature string on bridge channel %p is now '%s'\n", bridge_channel, dtmf);
929
930                 /* Assume that we do not want to look for DTMF any longer */
931                 look_for_dtmf = 0;
932
933                 /* See if a DTMF feature hook matches or can match */
934                 AST_LIST_TRAVERSE(&features->hooks, hook, entry) {
935                         /* If this hook matches just break out now */
936                         if (!strcmp(hook->dtmf, dtmf)) {
937                                 ast_debug(1, "DTMF feature hook %p matched DTMF string '%s' on bridge channel %p\n", hook, dtmf, bridge_channel);
938                                 look_for_dtmf = 0;
939                                 break;
940                         } else if (!strncmp(hook->dtmf, dtmf, dtmf_len)) {
941                                 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);
942                                 look_for_dtmf = 1;
943                         } else {
944                                 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);
945                         }
946                 }
947
948                 /* If we have reached the maximum length of a DTMF feature string bail out */
949                 if (dtmf_len == MAXIMUM_DTMF_FEATURE_STRING) {
950                         break;
951                 }
952         }
953
954         /* Since we are done bringing DTMF in return to using both begin and end frames */
955         ast_clear_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_END_DTMF_ONLY);
956
957         /* If a hook was actually matched execute it on this channel, otherwise stream up the DTMF to the other channels */
958         if (hook) {
959                 hook->callback(bridge, bridge_channel, hook->hook_pvt);
960                 /*
961                  * If we are handing the channel off to an external hook for
962                  * ownership, we are not guaranteed what kind of state it will
963                  * come back in.  If the channel hungup, we need to detect that
964                  * here if the hook did not already change the state.
965                  */
966                 if (bridge_channel->chan && ast_check_hangup_locked(bridge_channel->chan)) {
967                         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
968                 }
969         } else {
970                 ast_bridge_dtmf_stream(bridge, dtmf, bridge_channel->chan);
971         }
972 }
973
974 static void bridge_channel_talking(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
975 {
976         struct ast_bridge_features *features = (bridge_channel->features ? bridge_channel->features : &bridge->features);
977
978         if (features && features->talker_cb) {
979                 features->talker_cb(bridge, bridge_channel, features->talker_pvt_data);
980         }
981 }
982
983 /*! \brief Internal function that plays back DTMF on a bridge channel */
984 static void bridge_channel_dtmf_stream(struct ast_bridge_channel *bridge_channel)
985 {
986         char dtmf_q[8] = "";
987
988         ast_copy_string(dtmf_q, bridge_channel->dtmf_stream_q, sizeof(dtmf_q));
989         bridge_channel->dtmf_stream_q[0] = '\0';
990
991         ast_debug(1, "Playing DTMF stream '%s' out to bridge channel %p\n", dtmf_q, bridge_channel);
992         ast_dtmf_stream(bridge_channel->chan, NULL, dtmf_q, 250, 0);
993 }
994
995 /*! \brief Join a channel to a bridge and handle anything the bridge may want us to do */
996 static enum ast_bridge_channel_state bridge_channel_join(struct ast_bridge_channel *bridge_channel)
997 {
998         struct ast_format formats[2];
999         enum ast_bridge_channel_state state;
1000         ast_format_copy(&formats[0], ast_channel_readformat(bridge_channel->chan));
1001         ast_format_copy(&formats[1], ast_channel_writeformat(bridge_channel->chan));
1002
1003         /* Record the thread that will be the owner of us */
1004         bridge_channel->thread = pthread_self();
1005
1006         ast_debug(1, "Joining bridge channel %p to bridge %p\n", bridge_channel, bridge_channel->bridge);
1007
1008         ao2_lock(bridge_channel->bridge);
1009
1010         if (!bridge_channel->bridge->callid) {
1011                 bridge_channel->bridge->callid = ast_read_threadstorage_callid();
1012         }
1013
1014         /* Add channel into the bridge */
1015         AST_LIST_INSERT_TAIL(&bridge_channel->bridge->channels, bridge_channel, entry);
1016         bridge_channel->bridge->num++;
1017
1018         bridge_array_add(bridge_channel->bridge, bridge_channel->chan);
1019
1020         if (bridge_channel->swap) {
1021                 struct ast_bridge_channel *bridge_channel2;
1022
1023                 /*
1024                  * If we are performing a swap operation we do not need to
1025                  * execute the smart bridge operation as the actual number of
1026                  * channels involved will not have changed, we just need to tell
1027                  * the other channel to leave.
1028                  */
1029                 bridge_channel2 = find_bridge_channel(bridge_channel->bridge, bridge_channel->swap);
1030                 bridge_channel->swap = NULL;
1031                 if (bridge_channel2) {
1032                         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);
1033                         ast_bridge_change_state(bridge_channel2, AST_BRIDGE_CHANNEL_STATE_HANGUP);
1034                 }
1035         } else if (ast_test_flag(&bridge_channel->bridge->feature_flags, AST_BRIDGE_FLAG_SMART)) {
1036                 /* Perform the smart bridge operation, basically see if we need to move around between technologies */
1037                 smart_bridge_operation(bridge_channel->bridge, bridge_channel, bridge_channel->bridge->num);
1038         }
1039
1040         /* Make the channel compatible with the bridge */
1041         bridge_make_compatible(bridge_channel->bridge, bridge_channel);
1042
1043         /* Tell the bridge technology we are joining so they set us up */
1044         if (bridge_channel->bridge->technology->join) {
1045                 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);
1046                 if (bridge_channel->bridge->technology->join(bridge_channel->bridge, bridge_channel)) {
1047                         ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n", bridge_channel->bridge->technology->name, bridge_channel, bridge_channel->bridge);
1048                 }
1049         }
1050
1051         /* Actually execute the respective threading model, and keep our bridge thread alive */
1052         while (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
1053                 /* Update bridge pointer on channel */
1054                 ast_channel_internal_bridge_set(bridge_channel->chan, bridge_channel->bridge);
1055                 /* If the technology requires a thread and one is not running, start it up */
1056                 if (bridge_channel->bridge->thread == AST_PTHREADT_NULL
1057                         && (bridge_channel->bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_THREAD)) {
1058                         bridge_channel->bridge->stop = 0;
1059                         ast_debug(1, "Starting a bridge thread for bridge %p\n", bridge_channel->bridge);
1060                         ao2_ref(bridge_channel->bridge, +1);
1061                         if (ast_pthread_create(&bridge_channel->bridge->thread, NULL, bridge_thread, bridge_channel->bridge)) {
1062                                 ast_debug(1, "Failed to create a bridge thread for bridge %p, giving it another go.\n", bridge_channel->bridge);
1063                                 ao2_ref(bridge_channel->bridge, -1);
1064                                 continue;
1065                         }
1066                 }
1067                 /* Execute the threading model */
1068                 state = (bridge_channel->bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTITHREADED)
1069                         ? bridge_channel_join_multithreaded(bridge_channel)
1070                         : bridge_channel_join_singlethreaded(bridge_channel);
1071                 /* Depending on the above state see what we need to do */
1072                 switch (state) {
1073                 case AST_BRIDGE_CHANNEL_STATE_FEATURE:
1074                         bridge_channel_suspend(bridge_channel->bridge, bridge_channel);
1075                         ao2_unlock(bridge_channel->bridge);
1076                         bridge_channel_feature(bridge_channel->bridge, bridge_channel);
1077                         ao2_lock(bridge_channel->bridge);
1078                         ao2_lock(bridge_channel);
1079                         if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_FEATURE) {
1080                                 ast_bridge_change_state_nolock(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
1081                         }
1082                         ao2_unlock(bridge_channel);
1083                         bridge_channel_unsuspend(bridge_channel->bridge, bridge_channel);
1084                         break;
1085                 case AST_BRIDGE_CHANNEL_STATE_DTMF:
1086                         bridge_channel_suspend(bridge_channel->bridge, bridge_channel);
1087                         ao2_unlock(bridge_channel->bridge);
1088                         bridge_channel_dtmf_stream(bridge_channel);
1089                         ao2_lock(bridge_channel->bridge);
1090                         ao2_lock(bridge_channel);
1091                         if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_DTMF) {
1092                                 ast_bridge_change_state_nolock(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
1093                         }
1094                         ao2_unlock(bridge_channel);
1095                         bridge_channel_unsuspend(bridge_channel->bridge, bridge_channel);
1096                         break;
1097                 case AST_BRIDGE_CHANNEL_STATE_START_TALKING:
1098                 case AST_BRIDGE_CHANNEL_STATE_STOP_TALKING:
1099                         ao2_unlock(bridge_channel->bridge);
1100                         bridge_channel_talking(bridge_channel->bridge, bridge_channel);
1101                         ao2_lock(bridge_channel->bridge);
1102                         ao2_lock(bridge_channel);
1103                         switch (bridge_channel->state) {
1104                         case AST_BRIDGE_CHANNEL_STATE_START_TALKING:
1105                         case AST_BRIDGE_CHANNEL_STATE_STOP_TALKING:
1106                                 ast_bridge_change_state_nolock(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
1107                                 break;
1108                         default:
1109                                 break;
1110                         }
1111                         ao2_unlock(bridge_channel);
1112                         break;
1113                 default:
1114                         break;
1115                 }
1116         }
1117
1118         ast_channel_internal_bridge_set(bridge_channel->chan, NULL);
1119
1120         /* See if we need to dissolve the bridge itself if they hung up */
1121         if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_END) {
1122                 bridge_check_dissolve(bridge_channel->bridge, bridge_channel);
1123         }
1124
1125         /* Tell the bridge technology we are leaving so they tear us down */
1126         if (bridge_channel->bridge->technology->leave) {
1127                 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);
1128                 if (bridge_channel->bridge->technology->leave(bridge_channel->bridge, bridge_channel)) {
1129                         ast_debug(1, "Bridge technology %s failed to leave %p from bridge %p\n", bridge_channel->bridge->technology->name, bridge_channel, bridge_channel->bridge);
1130                 }
1131         }
1132
1133         /* Remove channel from the bridge */
1134         bridge_channel->bridge->num--;
1135         AST_LIST_REMOVE(&bridge_channel->bridge->channels, bridge_channel, entry);
1136
1137         bridge_array_remove(bridge_channel->bridge, bridge_channel->chan);
1138
1139         /* Perform the smart bridge operation if needed since a channel has left */
1140         if (ast_test_flag(&bridge_channel->bridge->feature_flags, AST_BRIDGE_FLAG_SMART)) {
1141                 smart_bridge_operation(bridge_channel->bridge, NULL, bridge_channel->bridge->num);
1142         }
1143
1144         ao2_unlock(bridge_channel->bridge);
1145
1146         /* Restore original formats of the channel as they came in */
1147         if (ast_format_cmp(ast_channel_readformat(bridge_channel->chan), &formats[0]) == AST_FORMAT_CMP_NOT_EQUAL) {
1148                 ast_debug(1, "Bridge is returning %p to read format %s(%d)\n", bridge_channel, ast_getformatname(&formats[0]), formats[0].id);
1149                 if (ast_set_read_format(bridge_channel->chan, &formats[0])) {
1150                         ast_debug(1, "Bridge failed to return channel %p to read format %s(%d)\n", bridge_channel, ast_getformatname(&formats[0]), formats[0].id);
1151                 }
1152         }
1153         if (ast_format_cmp(ast_channel_writeformat(bridge_channel->chan), &formats[1]) == AST_FORMAT_CMP_NOT_EQUAL) {
1154                 ast_debug(1, "Bridge is returning %p to write format %s(%d)\n", bridge_channel, ast_getformatname(&formats[1]), formats[1].id);
1155                 if (ast_set_write_format(bridge_channel->chan, &formats[1])) {
1156                         ast_debug(1, "Bridge failed to return channel %p to write format %s(%d)\n", bridge_channel, ast_getformatname(&formats[1]), formats[1].id);
1157                 }
1158         }
1159
1160         return bridge_channel->state;
1161 }
1162
1163 static void bridge_channel_destroy(void *obj)
1164 {
1165         struct ast_bridge_channel *bridge_channel = obj;
1166
1167         if (bridge_channel->callid) {
1168                 bridge_channel->callid = ast_callid_unref(bridge_channel->callid);
1169         }
1170
1171         if (bridge_channel->bridge) {
1172                 ao2_ref(bridge_channel->bridge, -1);
1173                 bridge_channel->bridge = NULL;
1174         }
1175         /* Destroy elements of the bridge channel structure and the bridge channel structure itself */
1176         ast_cond_destroy(&bridge_channel->cond);
1177 }
1178
1179 static struct ast_bridge_channel *bridge_channel_alloc(struct ast_bridge *bridge)
1180 {
1181         struct ast_bridge_channel *bridge_channel;
1182
1183         bridge_channel = ao2_alloc(sizeof(struct ast_bridge_channel), bridge_channel_destroy);
1184         if (!bridge_channel) {
1185                 return NULL;
1186         }
1187         ast_cond_init(&bridge_channel->cond, NULL);
1188         if (bridge) {
1189                 bridge_channel->bridge = bridge;
1190                 ao2_ref(bridge_channel->bridge, +1);
1191         }
1192         return bridge_channel;
1193 }
1194
1195 enum ast_bridge_channel_state ast_bridge_join(struct ast_bridge *bridge,
1196         struct ast_channel *chan,
1197         struct ast_channel *swap,
1198         struct ast_bridge_features *features,
1199         struct ast_bridge_tech_optimizations *tech_args)
1200 {
1201         struct ast_bridge_channel *bridge_channel;
1202         enum ast_bridge_channel_state state;
1203
1204         bridge_channel = bridge_channel_alloc(bridge);
1205         if (!bridge_channel) {
1206                 return AST_BRIDGE_CHANNEL_STATE_HANGUP;
1207         }
1208         if (tech_args) {
1209                 bridge_channel->tech_args = *tech_args;
1210         }
1211
1212         /* Initialize various other elements of the bridge channel structure that we can't do above */
1213         bridge_channel->chan = chan;
1214         bridge_channel->swap = swap;
1215         bridge_channel->features = features;
1216
1217         state = bridge_channel_join(bridge_channel);
1218
1219         /* Cleanup all the data in the bridge channel after it leaves the bridge. */
1220         bridge_channel->chan = NULL;
1221         bridge_channel->swap = NULL;
1222         bridge_channel->features = NULL;
1223
1224         ao2_ref(bridge_channel, -1);
1225         return state;
1226 }
1227
1228 /*! \brief Thread responsible for imparted bridged channels */
1229 static void *bridge_channel_thread(void *data)
1230 {
1231         struct ast_bridge_channel *bridge_channel = data;
1232         enum ast_bridge_channel_state state;
1233
1234         if (bridge_channel->callid) {
1235                 ast_callid_threadassoc_add(bridge_channel->callid);
1236         }
1237
1238         state = bridge_channel_join(bridge_channel);
1239
1240         /* 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 */
1241         if (bridge_channel->allow_impart_hangup && (state == AST_BRIDGE_CHANNEL_STATE_END || state == AST_BRIDGE_CHANNEL_STATE_HANGUP)) {
1242                 ast_hangup(bridge_channel->chan);
1243         }
1244
1245         /* cleanup */
1246         ao2_lock(bridge_channel);
1247         bridge_channel->chan = NULL;
1248         bridge_channel->swap = NULL;
1249         bridge_channel->features = NULL;
1250         ao2_unlock(bridge_channel);
1251
1252         ao2_ref(bridge_channel, -1);
1253
1254         return NULL;
1255 }
1256
1257 int ast_bridge_impart(struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap, struct ast_bridge_features *features, int allow_hangup)
1258 {
1259         struct ast_bridge_channel *bridge_channel;
1260
1261         /* Try to allocate a structure for the bridge channel */
1262         bridge_channel = bridge_channel_alloc(bridge);
1263         if (!bridge_channel) {
1264                 return -1;
1265         }
1266
1267         /* Setup various parameters */
1268         bridge_channel->chan = chan;
1269         bridge_channel->swap = swap;
1270         bridge_channel->features = features;
1271         bridge_channel->allow_impart_hangup = allow_hangup;
1272         bridge_channel->callid = ast_read_threadstorage_callid();
1273
1274         /* Actually create the thread that will handle the channel */
1275         if (ast_pthread_create(&bridge_channel->thread, NULL, bridge_channel_thread, bridge_channel)) {
1276                 ao2_ref(bridge_channel, -1);
1277                 return -1;
1278         }
1279
1280         return 0;
1281 }
1282
1283 int ast_bridge_depart(struct ast_bridge *bridge, struct ast_channel *chan)
1284 {
1285         struct ast_bridge_channel *bridge_channel;
1286         pthread_t thread;
1287
1288         ao2_lock(bridge);
1289
1290         /* Try to find the channel that we want to depart */
1291         if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1292                 ao2_unlock(bridge);
1293                 return -1;
1294         }
1295
1296         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_DEPART);
1297         thread = bridge_channel->thread;
1298
1299         ao2_unlock(bridge);
1300
1301         pthread_join(thread, NULL);
1302
1303         return 0;
1304 }
1305
1306 int ast_bridge_remove(struct ast_bridge *bridge, struct ast_channel *chan)
1307 {
1308         struct ast_bridge_channel *bridge_channel;
1309
1310         ao2_lock(bridge);
1311
1312         /* Try to find the channel that we want to remove */
1313         if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1314                 ao2_unlock(bridge);
1315                 return -1;
1316         }
1317
1318         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
1319
1320         ao2_unlock(bridge);
1321
1322         return 0;
1323 }
1324
1325 int ast_bridge_merge(struct ast_bridge *bridge0, struct ast_bridge *bridge1)
1326 {
1327         struct ast_bridge_channel *bridge_channel;
1328
1329         ao2_lock(bridge0);
1330         ao2_lock(bridge1);
1331
1332         /* If the first bridge currently has 2 channels and is not capable of becoming a multimixing bridge we can not merge */
1333         if (bridge0->num + bridge1->num > 2
1334                 && !(bridge0->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX)
1335                 && !ast_test_flag(&bridge0->feature_flags, AST_BRIDGE_FLAG_SMART)) {
1336                 ao2_unlock(bridge1);
1337                 ao2_unlock(bridge0);
1338                 ast_debug(1, "Can't merge bridge %p into bridge %p, multimix is needed and it could not be acquired.\n", bridge1, bridge0);
1339                 return -1;
1340         }
1341
1342         ast_debug(1, "Merging channels from bridge %p into bridge %p\n", bridge1, bridge0);
1343
1344         /* Perform smart bridge operation on bridge we are merging into so it can change bridge technology if needed */
1345         if (smart_bridge_operation(bridge0, NULL, bridge0->num + bridge1->num)) {
1346                 ao2_unlock(bridge1);
1347                 ao2_unlock(bridge0);
1348                 ast_debug(1, "Can't merge bridge %p into bridge %p, tried to perform smart bridge operation and failed.\n", bridge1, bridge0);
1349                 return -1;
1350         }
1351
1352         /* If a thread is currently executing on bridge1 tell it to stop */
1353         if (bridge1->thread) {
1354                 ast_debug(1, "Telling bridge thread on bridge %p to stop as it is being merged into %p\n", bridge1, bridge0);
1355                 bridge1->thread = AST_PTHREADT_STOP;
1356         }
1357
1358         /* Move channels from bridge1 over to bridge0 */
1359         while ((bridge_channel = AST_LIST_REMOVE_HEAD(&bridge1->channels, entry))) {
1360                 /* Tell the technology handling bridge1 that the bridge channel is leaving */
1361                 if (bridge1->technology->leave) {
1362                         ast_debug(1, "Giving bridge technology %s notification that %p is leaving bridge %p\n", bridge1->technology->name, bridge_channel, bridge1);
1363                         if (bridge1->technology->leave(bridge1, bridge_channel)) {
1364                                 ast_debug(1, "Bridge technology %s failed to allow %p to leave bridge %p\n", bridge1->technology->name, bridge_channel, bridge1);
1365                         }
1366                 }
1367
1368                 /* Drop channel count and reference count on the bridge they are leaving */
1369                 bridge1->num--;
1370                 ao2_ref(bridge1, -1);
1371
1372                 bridge_array_remove(bridge1, bridge_channel->chan);
1373
1374                 /* Now add them into the bridge they are joining, increase channel count, and bump up reference count */
1375                 bridge_channel->bridge = bridge0;
1376                 AST_LIST_INSERT_TAIL(&bridge0->channels, bridge_channel, entry);
1377                 bridge0->num++;
1378                 ao2_ref(bridge0, +1);
1379
1380                 bridge_array_add(bridge0, bridge_channel->chan);
1381
1382                 /* Make the channel compatible with the new bridge it is joining or else formats would go amuck */
1383                 bridge_make_compatible(bridge0, bridge_channel);
1384
1385                 /* Tell the technology handling bridge0 that the bridge channel is joining */
1386                 if (bridge0->technology->join) {
1387                         ast_debug(1, "Giving bridge technology %s notification that %p is joining bridge %p\n", bridge0->technology->name, bridge_channel, bridge0);
1388                         if (bridge0->technology->join(bridge0, bridge_channel)) {
1389                                 ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n", bridge0->technology->name, bridge_channel, bridge0);
1390                         }
1391                 }
1392
1393                 /* Poke the bridge channel, this will cause it to wake up and execute the proper threading model for the new bridge it is in */
1394                 bridge_channel_poke(bridge_channel);
1395         }
1396
1397         ast_debug(1, "Merged channels from bridge %p into bridge %p\n", bridge1, bridge0);
1398
1399         ao2_unlock(bridge1);
1400         ao2_unlock(bridge0);
1401
1402         return 0;
1403 }
1404
1405 int ast_bridge_suspend(struct ast_bridge *bridge, struct ast_channel *chan)
1406 {
1407         struct ast_bridge_channel *bridge_channel;
1408
1409         ao2_lock(bridge);
1410
1411         if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1412                 ao2_unlock(bridge);
1413                 return -1;
1414         }
1415
1416         bridge_channel_suspend(bridge, bridge_channel);
1417
1418         ao2_unlock(bridge);
1419
1420         return 0;
1421 }
1422
1423 int ast_bridge_unsuspend(struct ast_bridge *bridge, struct ast_channel *chan)
1424 {
1425         struct ast_bridge_channel *bridge_channel;
1426
1427         ao2_lock(bridge);
1428
1429         if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1430                 ao2_unlock(bridge);
1431                 return -1;
1432         }
1433
1434         bridge_channel_unsuspend(bridge, bridge_channel);
1435
1436         ao2_unlock(bridge);
1437
1438         return 0;
1439 }
1440
1441 void ast_bridge_technology_suspend(struct ast_bridge_technology *technology)
1442 {
1443         technology->suspended = 1;
1444 }
1445
1446 void ast_bridge_technology_unsuspend(struct ast_bridge_technology *technology)
1447 {
1448         technology->suspended = 0;
1449 }
1450
1451 int ast_bridge_features_register(enum ast_bridge_builtin_feature feature, ast_bridge_features_hook_callback callback, const char *dtmf)
1452 {
1453         if (ARRAY_LEN(builtin_features_handlers) <= feature
1454                 || builtin_features_handlers[feature]) {
1455                 return -1;
1456         }
1457
1458         if (!ast_strlen_zero(dtmf)) {
1459                 ast_copy_string(builtin_features_dtmf[feature], dtmf, sizeof(builtin_features_dtmf[feature]));
1460         }
1461
1462         builtin_features_handlers[feature] = callback;
1463
1464         return 0;
1465 }
1466
1467 int ast_bridge_features_unregister(enum ast_bridge_builtin_feature feature)
1468 {
1469         if (ARRAY_LEN(builtin_features_handlers) <= feature
1470                 || !builtin_features_handlers[feature]) {
1471                 return -1;
1472         }
1473
1474         builtin_features_handlers[feature] = NULL;
1475
1476         return 0;
1477 }
1478
1479 int ast_bridge_features_hook(struct ast_bridge_features *features,
1480         const char *dtmf,
1481         ast_bridge_features_hook_callback callback,
1482         void *hook_pvt,
1483         ast_bridge_features_hook_pvt_destructor destructor)
1484 {
1485         struct ast_bridge_features_hook *hook;
1486
1487         /* Allocate new memory and setup it's various variables */
1488         hook = ast_calloc(1, sizeof(*hook));
1489         if (!hook) {
1490                 return -1;
1491         }
1492         ast_copy_string(hook->dtmf, dtmf, sizeof(hook->dtmf));
1493         hook->callback = callback;
1494         hook->destructor = destructor;
1495         hook->hook_pvt = hook_pvt;
1496
1497         /* Once done we add it onto the list. Now it will be picked up when DTMF is used */
1498         AST_LIST_INSERT_TAIL(&features->hooks, hook, entry);
1499
1500         features->usable = 1;
1501
1502         return 0;
1503 }
1504
1505 void ast_bridge_features_set_talk_detector(struct ast_bridge_features *features,
1506         ast_bridge_talking_indicate_callback talker_cb,
1507         ast_bridge_talking_indicate_destructor talker_destructor,
1508         void *pvt_data)
1509 {
1510         features->talker_cb = talker_cb;
1511         features->talker_destructor_cb = talker_destructor;
1512         features->talker_pvt_data = pvt_data;
1513 }
1514
1515 int ast_bridge_features_enable(struct ast_bridge_features *features, enum ast_bridge_builtin_feature feature, const char *dtmf, void *config)
1516 {
1517         if (ARRAY_LEN(builtin_features_handlers) <= feature
1518                 || !builtin_features_handlers[feature]) {
1519                 return -1;
1520         }
1521
1522         /* If no alternate DTMF stream was provided use the default one */
1523         if (ast_strlen_zero(dtmf)) {
1524                 dtmf = builtin_features_dtmf[feature];
1525                 /* If no DTMF is still available (ie: it has been disabled) then error out now */
1526                 if (ast_strlen_zero(dtmf)) {
1527                         ast_debug(1, "Failed to enable built in feature %d on %p, no DTMF string is available for it.\n", feature, features);
1528                         return -1;
1529                 }
1530         }
1531
1532         /* The rest is basically pretty easy. We create another hook using the built in feature's callback and DTMF, easy as pie. */
1533         return ast_bridge_features_hook(features, dtmf, builtin_features_handlers[feature], config, NULL);
1534 }
1535
1536 void ast_bridge_features_set_flag(struct ast_bridge_features *features, enum ast_bridge_feature_flags flag)
1537 {
1538         ast_set_flag(&features->feature_flags, flag);
1539         features->usable = 1;
1540 }
1541
1542 int ast_bridge_features_init(struct ast_bridge_features *features)
1543 {
1544         /* Zero out the structure */
1545         memset(features, 0, sizeof(*features));
1546
1547         /* Initialize the hooks list, just in case */
1548         AST_LIST_HEAD_INIT_NOLOCK(&features->hooks);
1549
1550         return 0;
1551 }
1552
1553 void ast_bridge_features_cleanup(struct ast_bridge_features *features)
1554 {
1555         struct ast_bridge_features_hook *hook;
1556
1557         /* This is relatively simple, hooks are kept as a list on the features structure so we just pop them off and free them */
1558         while ((hook = AST_LIST_REMOVE_HEAD(&features->hooks, entry))) {
1559                 if (hook->destructor) {
1560                         hook->destructor(hook->hook_pvt);
1561                 }
1562                 ast_free(hook);
1563         }
1564         if (features->talker_destructor_cb && features->talker_pvt_data) {
1565                 features->talker_destructor_cb(features->talker_pvt_data);
1566                 features->talker_pvt_data = NULL;
1567         }
1568 }
1569
1570 int ast_bridge_dtmf_stream(struct ast_bridge *bridge, const char *dtmf, struct ast_channel *chan)
1571 {
1572         struct ast_bridge_channel *bridge_channel;
1573
1574         ao2_lock(bridge);
1575
1576         AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1577                 if (bridge_channel->chan == chan) {
1578                         continue;
1579                 }
1580                 ast_copy_string(bridge_channel->dtmf_stream_q, dtmf, sizeof(bridge_channel->dtmf_stream_q));
1581                 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_DTMF);
1582         }
1583
1584         ao2_unlock(bridge);
1585
1586         return 0;
1587 }
1588
1589 void ast_bridge_set_mixing_interval(struct ast_bridge *bridge, unsigned int mixing_interval)
1590 {
1591         ao2_lock(bridge);
1592         bridge->internal_mixing_interval = mixing_interval;
1593         ao2_unlock(bridge);
1594 }
1595
1596 void ast_bridge_set_internal_sample_rate(struct ast_bridge *bridge, unsigned int sample_rate)
1597 {
1598
1599         ao2_lock(bridge);
1600         bridge->internal_sample_rate = sample_rate;
1601         ao2_unlock(bridge);
1602 }
1603
1604 static void cleanup_video_mode(struct ast_bridge *bridge)
1605 {
1606         switch (bridge->video_mode.mode) {
1607         case AST_BRIDGE_VIDEO_MODE_NONE:
1608                 break;
1609         case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
1610                 if (bridge->video_mode.mode_data.single_src_data.chan_vsrc) {
1611                         ast_channel_unref(bridge->video_mode.mode_data.single_src_data.chan_vsrc);
1612                 }
1613                 break;
1614         case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
1615                 if (bridge->video_mode.mode_data.talker_src_data.chan_vsrc) {
1616                         ast_channel_unref(bridge->video_mode.mode_data.talker_src_data.chan_vsrc);
1617                 }
1618                 if (bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc) {
1619                         ast_channel_unref(bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc);
1620                 }
1621         }
1622         memset(&bridge->video_mode, 0, sizeof(bridge->video_mode));
1623 }
1624
1625 void ast_bridge_set_single_src_video_mode(struct ast_bridge *bridge, struct ast_channel *video_src_chan)
1626 {
1627         ao2_lock(bridge);
1628         cleanup_video_mode(bridge);
1629         bridge->video_mode.mode = AST_BRIDGE_VIDEO_MODE_SINGLE_SRC;
1630         bridge->video_mode.mode_data.single_src_data.chan_vsrc = ast_channel_ref(video_src_chan);
1631         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));
1632         ast_indicate(video_src_chan, AST_CONTROL_VIDUPDATE);
1633         ao2_unlock(bridge);
1634 }
1635
1636 void ast_bridge_set_talker_src_video_mode(struct ast_bridge *bridge)
1637 {
1638         ao2_lock(bridge);
1639         cleanup_video_mode(bridge);
1640         bridge->video_mode.mode = AST_BRIDGE_VIDEO_MODE_TALKER_SRC;
1641         ast_test_suite_event_notify("BRIDGE_VIDEO_MODE", "Message: video mode set to talker source\r\nVideo Mode: %d", bridge->video_mode.mode);
1642         ao2_unlock(bridge);
1643 }
1644
1645 void ast_bridge_update_talker_src_video_mode(struct ast_bridge *bridge, struct ast_channel *chan, int talker_energy, int is_keyframe)
1646 {
1647         struct ast_bridge_video_talker_src_data *data;
1648         /* If the channel doesn't support video, we don't care about it */
1649         if (!ast_format_cap_has_type(ast_channel_nativeformats(chan), AST_FORMAT_TYPE_VIDEO)) {
1650                 return;
1651         }
1652
1653         ao2_lock(bridge);
1654         data = &bridge->video_mode.mode_data.talker_src_data;
1655
1656         if (data->chan_vsrc == chan) {
1657                 data->average_talking_energy = talker_energy;
1658         } else if ((data->average_talking_energy < talker_energy) && is_keyframe) {
1659                 if (data->chan_old_vsrc) {
1660                         ast_channel_unref(data->chan_old_vsrc);
1661                 }
1662                 if (data->chan_vsrc) {
1663                         data->chan_old_vsrc = data->chan_vsrc;
1664                         ast_indicate(data->chan_old_vsrc, AST_CONTROL_VIDUPDATE);
1665                 }
1666                 data->chan_vsrc = ast_channel_ref(chan);
1667                 data->average_talking_energy = talker_energy;
1668                 ast_test_suite_event_notify("BRIDGE_VIDEO_SRC", "Message: video source updated\r\nVideo Channel: %s", ast_channel_name(data->chan_vsrc));
1669                 ast_indicate(data->chan_vsrc, AST_CONTROL_VIDUPDATE);
1670         } else if ((data->average_talking_energy < talker_energy) && !is_keyframe) {
1671                 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
1672         } else if (!data->chan_vsrc && is_keyframe) {
1673                 data->chan_vsrc = ast_channel_ref(chan);
1674                 data->average_talking_energy = talker_energy;
1675                 ast_test_suite_event_notify("BRIDGE_VIDEO_SRC", "Message: video source updated\r\nVideo Channel: %s", ast_channel_name(data->chan_vsrc));
1676                 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
1677         } else if (!data->chan_old_vsrc && is_keyframe) {
1678                 data->chan_old_vsrc = ast_channel_ref(chan);
1679                 ast_indicate(chan, AST_CONTROL_VIDUPDATE);
1680         }
1681         ao2_unlock(bridge);
1682 }
1683
1684 int ast_bridge_number_video_src(struct ast_bridge *bridge)
1685 {
1686         int res = 0;
1687
1688         ao2_lock(bridge);
1689         switch (bridge->video_mode.mode) {
1690         case AST_BRIDGE_VIDEO_MODE_NONE:
1691                 break;
1692         case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
1693                 if (bridge->video_mode.mode_data.single_src_data.chan_vsrc) {
1694                         res = 1;
1695                 }
1696                 break;
1697         case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
1698                 if (bridge->video_mode.mode_data.talker_src_data.chan_vsrc) {
1699                         res++;
1700                 }
1701                 if (bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc) {
1702                         res++;
1703                 }
1704         }
1705         ao2_unlock(bridge);
1706         return res;
1707 }
1708
1709 int ast_bridge_is_video_src(struct ast_bridge *bridge, struct ast_channel *chan)
1710 {
1711         int res = 0;
1712
1713         ao2_lock(bridge);
1714         switch (bridge->video_mode.mode) {
1715         case AST_BRIDGE_VIDEO_MODE_NONE:
1716                 break;
1717         case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
1718                 if (bridge->video_mode.mode_data.single_src_data.chan_vsrc == chan) {
1719                         res = 1;
1720                 }
1721                 break;
1722         case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
1723                 if (bridge->video_mode.mode_data.talker_src_data.chan_vsrc == chan) {
1724                         res = 1;
1725                 } else if (bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc == chan) {
1726                         res = 2;
1727                 }
1728
1729         }
1730         ao2_unlock(bridge);
1731         return res;
1732 }
1733
1734 void ast_bridge_remove_video_src(struct ast_bridge *bridge, struct ast_channel *chan)
1735 {
1736         ao2_lock(bridge);
1737         switch (bridge->video_mode.mode) {
1738         case AST_BRIDGE_VIDEO_MODE_NONE:
1739                 break;
1740         case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
1741                 if (bridge->video_mode.mode_data.single_src_data.chan_vsrc == chan) {
1742                         if (bridge->video_mode.mode_data.single_src_data.chan_vsrc) {
1743                                 ast_channel_unref(bridge->video_mode.mode_data.single_src_data.chan_vsrc);
1744                         }
1745                         bridge->video_mode.mode_data.single_src_data.chan_vsrc = NULL;
1746                 }
1747                 break;
1748         case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
1749                 if (bridge->video_mode.mode_data.talker_src_data.chan_vsrc == chan) {
1750                         if (bridge->video_mode.mode_data.talker_src_data.chan_vsrc) {
1751                                 ast_channel_unref(bridge->video_mode.mode_data.talker_src_data.chan_vsrc);
1752                         }
1753                         bridge->video_mode.mode_data.talker_src_data.chan_vsrc = NULL;
1754                         bridge->video_mode.mode_data.talker_src_data.average_talking_energy = 0;
1755                 }
1756                 if (bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc == chan) {
1757                         if (bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc) {
1758                                 ast_channel_unref(bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc);
1759                         }
1760                         bridge->video_mode.mode_data.talker_src_data.chan_old_vsrc = NULL;
1761                 }
1762         }
1763         ao2_unlock(bridge);
1764 }