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