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