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