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