aboutsummaryrefslogtreecommitdiffstats
path: root/main/bridging.c
blob: a256cf038a2d05a06d656de6f53559c3f9378947 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2007 - 2009, Digium, Inc.
 *
 * Joshua Colp <jcolp@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Channel Bridging API
 *
 * \author Joshua Colp <jcolp@digium.com>
 */

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include <signal.h>

#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/options.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/linkedlists.h"
#include "asterisk/bridging.h"
#include "asterisk/bridging_technology.h"
#include "asterisk/app.h"
#include "asterisk/file.h"
#include "asterisk/module.h"
#include "asterisk/astobj2.h"

static AST_RWLIST_HEAD_STATIC(bridge_technologies, ast_bridge_technology);

/* Initial starting point for the bridge array of channels */
#define BRIDGE_ARRAY_START 128

/* Grow rate of bridge array of channels */
#define BRIDGE_ARRAY_GROW 32

/*! Default DTMF keys for built in features */
static char builtin_features_dtmf[AST_BRIDGE_BUILTIN_END][MAXIMUM_DTMF_FEATURE_STRING];

/*! Function handlers for the built in features */
static void *builtin_features_handlers[AST_BRIDGE_BUILTIN_END];

int __ast_bridge_technology_register(struct ast_bridge_technology *technology, struct ast_module *module)
{
	struct ast_bridge_technology *current = NULL;

	/* Perform a sanity check to make sure the bridge technology conforms to our needed requirements */
	if (ast_strlen_zero(technology->name) || !technology->capabilities || !technology->write) {
		ast_log(LOG_WARNING, "Bridge technology %s failed registration sanity check.\n", technology->name);
		return -1;
	}

	AST_RWLIST_WRLOCK(&bridge_technologies);

	/* Look for duplicate bridge technology already using this name, or already registered */
	AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
		if ((!strcasecmp(current->name, technology->name)) || (current == technology)) {
			ast_log(LOG_WARNING, "A bridge technology of %s already claims to exist in our world.\n", technology->name);
			AST_RWLIST_UNLOCK(&bridge_technologies);
			return -1;
		}
	}

	/* Copy module pointer so reference counting can keep the module from unloading */
	technology->mod = module;

	/* Insert our new bridge technology into the list and print out a pretty message */
	AST_RWLIST_INSERT_TAIL(&bridge_technologies, technology, entry);

	AST_RWLIST_UNLOCK(&bridge_technologies);

	if (option_verbose > 1) {
		ast_verbose(VERBOSE_PREFIX_2 "Registered bridge technology %s\n", technology->name);
	}

	return 0;
}

int ast_bridge_technology_unregister(struct ast_bridge_technology *technology)
{
	struct ast_bridge_technology *current = NULL;

	AST_RWLIST_WRLOCK(&bridge_technologies);

	/* Ensure the bridge technology is registered before removing it */
	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&bridge_technologies, current, entry) {
		if (current == technology) {
			AST_RWLIST_REMOVE_CURRENT(entry);
			if (option_verbose > 1) {
				ast_verbose(VERBOSE_PREFIX_2 "Unregistered bridge technology %s\n", technology->name);
			}
			break;
		}
	}
	AST_RWLIST_TRAVERSE_SAFE_END;

	AST_RWLIST_UNLOCK(&bridge_technologies);

	return current ? 0 : -1;
}

void ast_bridge_change_state(struct ast_bridge_channel *bridge_channel, enum ast_bridge_channel_state new_state)
{
	/* Change the state on the bridge channel */
	bridge_channel->state = new_state;

	/* Only poke the channel's thread if it is not us */
	if (!pthread_equal(pthread_self(), bridge_channel->thread)) {
		pthread_kill(bridge_channel->thread, SIGURG);
		ast_mutex_lock(&bridge_channel->lock);
		ast_cond_signal(&bridge_channel->cond);
		ast_mutex_unlock(&bridge_channel->lock);
	}

	return;
}

/*! \brief Helper function to poke the bridge thread */
static void bridge_poke(struct ast_bridge *bridge)
{
	/* Poke the thread just in case */
	if (bridge->thread != AST_PTHREADT_NULL && bridge->thread != AST_PTHREADT_STOP) {
		pthread_kill(bridge->thread, SIGURG);
	}

	return;
}

/*! \brief Helper function to add a channel to the bridge array
 *
 * \note This function assumes the bridge is locked.
 */
static void bridge_array_add(struct ast_bridge *bridge, struct ast_channel *chan)
{
	/* We have to make sure the bridge thread is not using the bridge array before messing with it */
	while (bridge->waiting) {
		bridge_poke(bridge);
		sched_yield();
	}

	bridge->array[bridge->array_num++] = chan;

	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);

	/* If the next addition of a channel will exceed our array size grow it out */
	if (bridge->array_num == bridge->array_size) {
		struct ast_channel **tmp;
		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);
		if (!(tmp = ast_realloc(bridge->array, (bridge->array_size + BRIDGE_ARRAY_GROW) * sizeof(struct ast_channel *)))) {
			ast_log(LOG_ERROR, "Failed to allocate more space for another channel on bridge '%p', this is not going to end well\n", bridge);
			return;
		}
		bridge->array = tmp;
		bridge->array_size += BRIDGE_ARRAY_GROW;
	}

	return;
}

/*! \brief Helper function to remove a channel from the bridge array
 *
 * \note This function assumes the bridge is locked.
 */
static void bridge_array_remove(struct ast_bridge *bridge, struct ast_channel *chan)
{
	int i;

	/* We have to make sure the bridge thread is not using the bridge array before messing with it */
	while (bridge->waiting) {
		bridge_poke(bridge);
		sched_yield();
	}

	for (i = 0; i < bridge->array_num; i++) {
		if (bridge->array[i] == chan) {
			bridge->array[i] = (bridge->array[(bridge->array_num - 1)] != chan ? bridge->array[(bridge->array_num - 1)] : NULL);
			bridge->array[(bridge->array_num - 1)] = NULL;
			bridge->array_num--;
			ast_debug(1, "Removed channel %p from bridge array on %p, new count is %d\n", chan, bridge, (int)bridge->array_num);
			break;
		}
	}

	return;
}

/*! \brief Helper function to find a bridge channel given a channel */
static struct ast_bridge_channel *find_bridge_channel(struct ast_bridge *bridge, struct ast_channel *chan)
{
	struct ast_bridge_channel *bridge_channel = NULL;

	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
		if (bridge_channel->chan == chan) {
			break;
		}
	}

	return bridge_channel;
}

/*! \brief Internal function to see whether a bridge should dissolve, and if so do it */
static void bridge_check_dissolve(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
{
	struct ast_bridge_channel *bridge_channel2 = NULL;

	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))) {
		return;
	}

	ast_debug(1, "Dissolving bridge %p\n", bridge);

	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel2, entry) {
		if (bridge_channel2->state != AST_BRIDGE_CHANNEL_STATE_END && bridge_channel2->state != AST_BRIDGE_CHANNEL_STATE_DEPART) {
			ast_bridge_change_state(bridge_channel2, AST_BRIDGE_CHANNEL_STATE_HANGUP);
		}
	}

	/* Since all the channels are going away let's go ahead and stop our on thread */
	bridge->stop = 1;

	return;
}

/*! \brief Internal function to handle DTMF from a channel */
static struct ast_frame *bridge_handle_dtmf(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
{
	struct ast_bridge_features *features = (bridge_channel->features ? bridge_channel->features : &bridge->features);
	struct ast_bridge_features_hook *hook = NULL;

	/* If the features structure we grabbed is not usable immediately return the frame */
	if (!features->usable) {
		return frame;
	}

	/* 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 */
	AST_LIST_TRAVERSE(&features->hooks, hook, entry) {
		if (hook->dtmf[0] == frame->subclass.integer) {
			ast_frfree(frame);
			frame = NULL;
			ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_FEATURE);
			break;
		}
	}

	return frame;
}

/*! \brief Internal function used to determine whether a control frame should be dropped or not */
static int bridge_drop_control_frame(int subclass)
{
	switch (subclass) {
	case AST_CONTROL_ANSWER:
	case -1:
		return 1;
	default:
		return 0;
	}
}

void ast_bridge_handle_trip(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_channel *chan, int outfd)
{
	/* If no bridge channel has been provided and the actual channel has been provided find it */
	if (chan && !bridge_channel) {
		bridge_channel = find_bridge_channel(bridge, chan);
	}

	/* If a bridge channel with actual channel is present read a frame and handle it */
	if (chan && bridge_channel) {
		struct ast_frame *frame = (((bridge->features.mute) || (bridge_channel->features && bridge_channel->features->mute)) ? ast_read_noaudio(chan) : ast_read(chan));

		/* This is pretty simple... see if they hung up */
		if (!frame || (frame->frametype == AST_FRAME_CONTROL && frame->subclass.integer == AST_CONTROL_HANGUP)) {
			/* Signal the thread that is handling the bridged channel that it should be ended */
			ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
		} else if (frame->frametype == AST_FRAME_CONTROL && bridge_drop_control_frame(frame->subclass.integer)) {
			ast_debug(1, "Dropping control frame from bridge channel %p\n", bridge_channel);
		} else {
			if (frame->frametype == AST_FRAME_DTMF_BEGIN) {
				frame = bridge_handle_dtmf(bridge, bridge_channel, frame);
			}
			/* Simply write the frame out to the bridge technology if it still exists */
			if (frame) {
				bridge->technology->write(bridge, bridge_channel, frame);
			}
		}

		if (frame) {
			ast_frfree(frame);
		}
		return;
	}

	/* If a file descriptor actually tripped pass it off to the bridge technology */
	if (outfd > -1 && bridge->technology->fd) {
		bridge->technology->fd(bridge, bridge_channel, outfd);
		return;
	}

	/* If all else fails just poke the bridge */
	if (bridge->technology->poke && bridge_channel) {
		bridge->technology->poke(bridge, bridge_channel);
		return;
	}

	return;
}

/*! \brief Generic thread loop, TODO: Rethink this/improve it */
static int generic_thread_loop(struct ast_bridge *bridge)
{
	while (!bridge->stop && !bridge->refresh && bridge->array_num) {
		struct ast_channel *winner = NULL;
		int to = -1;

		/* Move channels around for priority reasons if we have more than one channel in our array */
		if (bridge->array_num > 1) {
			struct ast_channel *first = bridge->array[0];
			memmove(bridge->array, bridge->array + 1, sizeof(struct ast_channel *) * (bridge->array_num - 1));
			bridge->array[(bridge->array_num - 1)] = first;
		}

		/* Wait on the channels */
		bridge->waiting = 1;
		ao2_unlock(bridge);
		winner = ast_waitfor_n(bridge->array, (int)bridge->array_num, &to);
		bridge->waiting = 0;
		ao2_lock(bridge);

		/* Process whatever they did */
		ast_bridge_handle_trip(bridge, NULL, winner, -1);
	}

	return 0;
}

/*! \brief Bridge thread function */
static void *bridge_thread(void *data)
{
	struct ast_bridge *bridge = data;
	int res = 0;

	ao2_lock(bridge);

	ast_debug(1, "Started bridge thread for %p\n", bridge);

	/* Loop around until we are told to stop */
	while (!bridge->stop && bridge->array_num && !res) {
		/* In case the refresh bit was set simply set it back to off */
		bridge->refresh = 0;

		ast_debug(1, "Launching bridge thread function %p for bridge %p\n", (bridge->technology->thread ? bridge->technology->thread : &generic_thread_loop), bridge);

		/* Execute the appropriate thread function. If the technology does not provide one we use the generic one */
		res = (bridge->technology->thread ? bridge->technology->thread(bridge) : generic_thread_loop(bridge));
	}

	ast_debug(1, "Ending bridge thread for %p\n", bridge);

	/* Indicate the bridge thread is no longer active */
	bridge->thread = AST_PTHREADT_NULL;
	ao2_unlock(bridge);

	ao2_ref(bridge, -1);

	return NULL;
}

/*! \brief Helper function used to find the "best" bridge technology given a specified capabilities */
static struct ast_bridge_technology *find_best_technology(format_t capabilities)
{
	struct ast_bridge_technology *current = NULL, *best = NULL;

	AST_RWLIST_RDLOCK(&bridge_technologies);
	AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
		char tmp1[256], tmp2[256];
		ast_debug(1, "Bridge technology %s has capabilities %s and we want %s\n", current->name,
			ast_getformatname_multiple(tmp1, sizeof(tmp1), current->capabilities),
			ast_getformatname_multiple(tmp2, sizeof(tmp2), capabilities));
		if (current->suspended) {
			ast_debug(1, "Bridge technology %s is suspended. Skipping.\n", current->name);
			continue;
		}
		if (!(current->capabilities & capabilities)) {
			ast_debug(1, "Bridge technology %s does not have the capabilities we need.\n", current->name);
			continue;
		}
		if (best && best->preference < current->preference) {
			ast_debug(1, "Bridge technology %s has preference %d while %s has preference %d. Skipping.\n", current->name, current->preference, best->name, best->preference);
			continue;
		}
		best = current;
	}

	if (best) {
		/* Increment it's module reference count if present so it does not get unloaded while in use */
		if (best->mod) {
			ast_module_ref(best->mod);
		}
		ast_debug(1, "Chose bridge technology %s\n", best->name);
	}

	AST_RWLIST_UNLOCK(&bridge_technologies);

	return best;
}

static void destroy_bridge(void *obj)
{
	struct ast_bridge *bridge = obj;

	ast_debug(1, "Actually destroying bridge %p, nobody wants it anymore\n", bridge);

	/* Pass off the bridge to the technology to destroy if needed */
	if (bridge->technology->destroy) {
		ast_debug(1, "Giving bridge technology %s the bridge structure %p to destroy\n", bridge->technology->name, bridge);
		if (bridge->technology->destroy(bridge)) {
			ast_debug(1, "Bridge technology %s failed to destroy bridge structure %p... trying our best\n", bridge->technology->name, bridge);
		}
	}

	/* We are no longer using the bridge technology so decrement the module reference count on it */
	if (bridge->technology->mod) {
		ast_module_unref(bridge->technology->mod);
	}

	/* Last but not least clean up the features configuration */
	ast_bridge_features_cleanup(&bridge->features);

	/* Drop the array of channels */
	ast_free(bridge->array);

	return;
}

struct ast_bridge *ast_bridge_new(format_t capabilities, int flags)
{
	struct ast_bridge *bridge = NULL;
	struct ast_bridge_technology *bridge_technology = NULL;

	/* If we need to be a smart bridge see if we can move between 1to1 and multimix bridges */
	if (flags & AST_BRIDGE_FLAG_SMART) {
		struct ast_bridge *other_bridge;

		if (!(other_bridge = ast_bridge_new((capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX) ? AST_BRIDGE_CAPABILITY_MULTIMIX : AST_BRIDGE_CAPABILITY_1TO1MIX, 0))) {
			return NULL;
		}

		ast_bridge_destroy(other_bridge);
	}

	/* If capabilities were provided use our helper function to find the "best" bridge technology, otherwise we can
	 * just look for the most basic capability needed, single 1to1 mixing. */
	bridge_technology = (capabilities ? find_best_technology(capabilities) : find_best_technology(AST_BRIDGE_CAPABILITY_1TO1MIX));

	/* If no bridge technology was found we can't possibly do bridging so fail creation of the bridge */
	if (!bridge_technology) {
		char codec_buf[256];
		ast_debug(1, "Failed to find a bridge technology to satisfy capabilities %s\n",
			ast_getformatname_multiple(codec_buf, sizeof(codec_buf), capabilities));
		return NULL;
	}

	/* We have everything we need to create this bridge... so allocate the memory, link things together, and fire her up! */
	if (!(bridge = ao2_alloc(sizeof(*bridge), destroy_bridge))) {
		return NULL;
	}

	bridge->technology = bridge_technology;
	bridge->thread = AST_PTHREADT_NULL;

	/* Create an array of pointers for the channels that will be joining us */
	bridge->array = ast_calloc(BRIDGE_ARRAY_START, sizeof(struct ast_channel*));
	bridge->array_size = BRIDGE_ARRAY_START;

	ast_set_flag(&bridge->feature_flags, flags);

	/* Pass off the bridge to the technology to manipulate if needed */
	if (bridge->technology->create) {
		ast_debug(1, "Giving bridge technology %s the bridge structure %p to setup\n", bridge->technology->name, bridge);
		if (bridge->technology->create(bridge)) {
			ast_debug(1, "Bridge technology %s failed to setup bridge structure %p\n", bridge->technology->name, bridge);
			ao2_ref(bridge, -1);
			bridge = NULL;
		}
	}

	return bridge;
}

int ast_bridge_check(format_t capabilities)
{
	struct ast_bridge_technology *bridge_technology = NULL;

	if (!(bridge_technology = find_best_technology(capabilities))) {
		return 0;
	}

	ast_module_unref(bridge_technology->mod);

	return 1;
}

int ast_bridge_destroy(struct ast_bridge *bridge)
{
	struct ast_bridge_channel *bridge_channel = NULL;

	ao2_lock(bridge);

	bridge->stop = 1;

	bridge_poke(bridge);

	ast_debug(1, "Telling all channels in bridge %p to end and leave the party\n", bridge);

	/* Drop every bridged channel, the last one will cause the bridge thread (if it exists) to exit */
	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
		ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
	}

	ao2_unlock(bridge);

	ao2_ref(bridge, -1);

	return 0;
}

static int bridge_make_compatible(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
{
	format_t formats[2] = {bridge_channel->chan->readformat, bridge_channel->chan->writeformat};

	/* Are the formats currently in use something ths bridge can handle? */
	if (!(bridge->technology->formats & bridge_channel->chan->readformat)) {
		format_t best_format = ast_best_codec(bridge->technology->formats);

		/* Read format is a no go... */
		if (option_debug) {
			char codec_buf[512];
			ast_debug(1, "Bridge technology %s wants to read any of formats %s but channel has %s\n", bridge->technology->name,
				ast_getformatname_multiple(codec_buf, sizeof(codec_buf), bridge->technology->formats),
				ast_getformatname(formats[0]));
		}
		/* Switch read format to the best one chosen */
		if (ast_set_read_format(bridge_channel->chan, best_format)) {
			ast_log(LOG_WARNING, "Failed to set channel %s to read format %s\n", bridge_channel->chan->name, ast_getformatname(best_format));
			return -1;
		}
		ast_debug(1, "Bridge %p put channel %s into read format %s\n", bridge, bridge_channel->chan->name, ast_getformatname(best_format));
	} else {
		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]));
	}

	if (!(bridge->technology->formats & formats[1])) {
		int best_format = ast_best_codec(bridge->technology->formats);

		/* Write format is a no go... */
		if (option_debug) {
			char codec_buf[512];
			ast_debug(1, "Bridge technology %s wants to write any of formats %s but channel has %s\n", bridge->technology->name,
				ast_getformatname_multiple(codec_buf, sizeof(codec_buf), bridge->technology->formats),
				ast_getformatname(formats[1]));
		}
		/* Switch write format to the best one chosen */
		if (ast_set_write_format(bridge_channel->chan, best_format)) {
			ast_log(LOG_WARNING, "Failed to set channel %s to write format %s\n", bridge_channel->chan->name, ast_getformatname(best_format));
			return -1;
		}
		ast_debug(1, "Bridge %p put channel %s into write format %s\n", bridge, bridge_channel->chan->name, ast_getformatname(best_format));
	} else {
		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]));
	}

	return 0;
}

/*! \brief Perform the smart bridge operation. Basically sees if a new bridge technology should be used instead of the current one. */
static int smart_bridge_operation(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, int count)
{
	format_t new_capabilities = 0;
	struct ast_bridge_technology *new_technology = NULL, *old_technology = bridge->technology;
	struct ast_bridge temp_bridge = {
		.technology = bridge->technology,
		.bridge_pvt = bridge->bridge_pvt,
	};
	struct ast_bridge_channel *bridge_channel2 = NULL;

	/* Based on current feature determine whether we want to change bridge technologies or not */
	if (bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX) {
		if (count <= 2) {
			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);
			return 0;
		}
		new_capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX;
	} else if (bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) {
		if (count > 2) {
			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);
			return 0;
		}
		new_capabilities = AST_BRIDGE_CAPABILITY_1TO1MIX;
	}

	if (!new_capabilities) {
		ast_debug(1, "Bridge '%p' has no new capabilities, not performing smart bridge operation.\n", bridge);
		return 0;
	}

	/* Attempt to find a new bridge technology to satisfy the capabilities */
	if (!(new_technology = find_best_technology(new_capabilities))) {
		char codec_buf[256];
		ast_debug(1, "Smart bridge operation was unable to find new bridge technology with capabilities %s to satisfy bridge %p\n",
			ast_getformatname_multiple(codec_buf, sizeof(codec_buf), new_capabilities), bridge);
		return -1;
	}

	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);

	/* If a thread is currently executing for the current technology tell it to stop */
	if (bridge->thread != AST_PTHREADT_NULL) {
		/* 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. */
		if (new_technology->capabilities & AST_BRIDGE_CAPABILITY_THREAD) {
			ast_debug(1, "Telling current bridge thread for bridge %p to refresh\n", bridge);
			bridge->refresh = 1;
			bridge_poke(bridge);
		} else {
			pthread_t bridge_thread = bridge->thread;
			ast_debug(1, "Telling current bridge thread for bridge %p to stop\n", bridge);
			bridge->stop = 1;
			bridge_poke(bridge);
			ao2_unlock(bridge);
			pthread_join(bridge_thread, NULL);
			ao2_lock(bridge);
		}
	}

	/* 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 */
	bridge->bridge_pvt = NULL;
	bridge->technology = new_technology;

	/* Pass the bridge to the new bridge technology so it can set it up */
	if (new_technology->create) {
		ast_debug(1, "Giving bridge technology %s the bridge structure %p to setup\n", new_technology->name, bridge);
		if (new_technology->create(bridge)) {
			ast_debug(1, "Bridge technology %s failed to setup bridge structure %p\n", new_technology->name, bridge);
		}
	}

	/* Move existing channels over to the new technology, while taking them away from the old one */
	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel2, entry) {
		/* Skip over channel that initiated the smart bridge operation */
		if (bridge_channel == bridge_channel2) {
			continue;
		}

		/* First we part them from the old technology */
		if (old_technology->leave) {
			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);
			if (old_technology->leave(&temp_bridge, bridge_channel2)) {
				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);
			}
		}

		/* Second we make them compatible again with the bridge */
		bridge_make_compatible(bridge, bridge_channel2);

		/* Third we join them to the new technology */
		if (new_technology->join) {
			ast_debug(1, "Giving bridge technology %s notification that %p is joining bridge %p\n", new_technology->name, bridge_channel2, bridge);
			if (new_technology->join(bridge, bridge_channel2)) {
				ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n", new_technology->name, bridge_channel2, bridge);
			}
		}

		/* Fourth we tell them to wake up so they become aware that they above has happened */
		pthread_kill(bridge_channel2->thread, SIGURG);
		ast_mutex_lock(&bridge_channel2->lock);
		ast_cond_signal(&bridge_channel2->cond);
		ast_mutex_unlock(&bridge_channel2->lock);
	}

	/* 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 */
	if (old_technology->destroy) {
		ast_debug(1, "Giving bridge technology %s the bridge structure %p (really %p) to destroy\n", old_technology->name, &temp_bridge, bridge);
		if (old_technology->destroy(&temp_bridge)) {
			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);
		}
	}

	/* Finally if the old technology has module referencing remove our reference, we are no longer going to use it */
	if (old_technology->mod) {
		ast_module_unref(old_technology->mod);
	}

	return 0;
}

/*! \brief Run in a multithreaded model. Each joined channel does writing/reading in their own thread. TODO: Improve */
static enum ast_bridge_channel_state bridge_channel_join_multithreaded(struct ast_bridge_channel *bridge_channel)
{
	int fds[4] = { -1, }, nfds = 0, i = 0, outfd = -1, ms = -1;
	struct ast_channel *chan = NULL;

	/* Add any file descriptors we may want to monitor */
	if (bridge_channel->bridge->technology->fd) {
		for (i = 0; i < 4; i ++) {
			if (bridge_channel->fds[i] >= 0) {
				fds[nfds++] = bridge_channel->fds[i];
			}
		}
	}

	ao2_unlock(bridge_channel->bridge);

	/* Wait for data to either come from the channel or us to be signalled */
	if (!bridge_channel->suspended) {
		ast_debug(1, "Going into a multithreaded waitfor for bridge channel %p of bridge %p\n", bridge_channel, bridge_channel->bridge);
		chan = ast_waitfor_nandfds(&bridge_channel->chan, 1, fds, nfds, NULL, &outfd, &ms);
	} else {
		ast_mutex_lock(&bridge_channel->lock);
		ast_debug(1, "Going into a multithreaded signal wait for bridge channel %p of bridge %p\n", bridge_channel, bridge_channel->bridge);
		ast_cond_wait(&bridge_channel->cond, &bridge_channel->lock);
		ast_mutex_unlock(&bridge_channel->lock);
	}

	ao2_lock(bridge_channel->bridge);

	if (!bridge_channel->suspended) {
		ast_bridge_handle_trip(bridge_channel->bridge, bridge_channel, chan, outfd);
	}

	return bridge_channel->state;
}

/*! \brief Run in a singlethreaded model. Each joined channel yields itself to the main bridge thread. TODO: Improve */
static enum ast_bridge_channel_state bridge_channel_join_singlethreaded(struct ast_bridge_channel *bridge_channel)
{
	ao2_unlock(bridge_channel->bridge);
	ast_mutex_lock(&bridge_channel->lock);
	if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
		ast_debug(1, "Going into a single threaded signal wait for bridge channel %p of bridge %p\n", bridge_channel, bridge_channel->bridge);
		ast_cond_wait(&bridge_channel->cond, &bridge_channel->lock);
	}
	ast_mutex_unlock(&bridge_channel->lock);
	ao2_lock(bridge_channel->bridge);

	return bridge_channel->state;
}

/*! \brief Internal function that suspends a channel from a bridge */
static void bridge_channel_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
{
	bridge_channel->suspended = 1;

	bridge_array_remove(bridge, bridge_channel->chan);

	if (bridge->technology->suspend) {
		bridge->technology->suspend(bridge, bridge_channel);
	}

	return;
}

/*! \brief Internal function that unsuspends a channel from a bridge */
static void bridge_channel_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
{
	bridge_channel->suspended =0;

	bridge_array_add(bridge, bridge_channel->chan);

	if (bridge->technology->unsuspend) {
		bridge->technology->unsuspend(bridge, bridge_channel);
	}

	return;
}

/*! \brief Internal function that executes a feature on a bridge channel */
static void bridge_channel_feature(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
{
	struct ast_bridge_features *features = (bridge_channel->features ? bridge_channel->features : &bridge->features);
	struct ast_bridge_features_hook *hook = NULL;
	char dtmf[MAXIMUM_DTMF_FEATURE_STRING] = "";
	int look_for_dtmf = 1, dtmf_len = 0;

	/* 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 */
	ast_set_flag(bridge_channel->chan, AST_FLAG_END_DTMF_ONLY);

	/* Wait for DTMF on the channel and put it into a buffer. If the buffer matches any feature hook execute the hook. */
	while (look_for_dtmf) {
		int res = ast_waitfordigit(bridge_channel->chan, 3000);

		/* If the above timed out simply exit */
		if (!res) {
			ast_debug(1, "DTMF feature string collection on bridge channel %p timed out\n", bridge_channel);
			break;
		} else if (res < 0) {
			ast_debug(1, "DTMF feature string collection failed on bridge channel %p for some reason\n", bridge_channel);
			break;
		}

		/* Add the above DTMF into the DTMF string so we can do our matching */
		dtmf[dtmf_len++] = res;

		ast_debug(1, "DTMF feature string on bridge channel %p is now '%s'\n", bridge_channel, dtmf);

		/* Assume that we do not want to look for DTMF any longer */
		look_for_dtmf = 0;

		/* See if a DTMF feature hook matches or can match */
		AST_LIST_TRAVERSE(&features->hooks, hook, entry) {
			/* If this hook matches just break out now */
			if (!strcmp(hook->dtmf, dtmf)) {
				ast_debug(1, "DTMF feature hook %p matched DTMF string '%s' on bridge channel %p\n", hook, dtmf, bridge_channel);
				break;
			} else if (!strncmp(hook->dtmf, dtmf, dtmf_len)) {
				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);
				look_for_dtmf = 1;
			} else {
				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);
			}
		}

		/* If we have reached the maximum length of a DTMF feature string bail out */
		if (dtmf_len == MAXIMUM_DTMF_FEATURE_STRING) {
			break;
		}
	}

	/* Since we are done bringing DTMF in return to using both begin and end frames */
	ast_clear_flag(bridge_channel->chan, AST_FLAG_END_DTMF_ONLY);

	/* If a hook was actually matched execute it on this channel, otherwise stream up the DTMF to the other channels */
	if (hook) {
		hook->callback(bridge, bridge_channel, hook->hook_pvt);
	} else {
		ast_bridge_dtmf_stream(bridge, dtmf, bridge_channel->chan);
		ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
	}

	return;
}

/*! \brief Internal function that plays back DTMF on a bridge channel */
static void bridge_channel_dtmf_stream(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
{
	char dtmf_q[8] = "";

	ast_copy_string(dtmf_q, bridge_channel->dtmf_stream_q, sizeof(dtmf_q));
	bridge_channel->dtmf_stream_q[0] = '\0';

	ast_debug(1, "Playing DTMF stream '%s' out to bridge channel %p\n", dtmf_q, bridge_channel);
	ast_dtmf_stream(bridge_channel->chan, NULL, dtmf_q, 250, 0);

	ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);

	return;
}

/*! \brief Join a channel to a bridge and handle anything the bridge may want us to do */
static enum ast_bridge_channel_state bridge_channel_join(struct ast_bridge_channel *bridge_channel)
{
	int formats[2] = { bridge_channel->chan->readformat, bridge_channel->chan->writeformat };
	enum ast_bridge_channel_state state;

	/* Record the thread that will be the owner of us */
	bridge_channel->thread = pthread_self();

	ast_debug(1, "Joining bridge channel %p to bridge %p\n", bridge_channel, bridge_channel->bridge);

	ao2_lock(bridge_channel->bridge);

	state = bridge_channel->state;

	/* Add channel into the bridge */
	AST_LIST_INSERT_TAIL(&bridge_channel->bridge->channels, bridge_channel, entry);
	bridge_channel->bridge->num++;

	bridge_array_add(bridge_channel->bridge, bridge_channel->chan);

	if (bridge_channel->swap) {
		struct ast_bridge_channel *bridge_channel2 = NULL;

		/* If we are performing a swap operation we do not need to execute the smart bridge operation as the actual number of channels involved will not have changed, we just need to tell the other channel to leave */
		if ((bridge_channel2 = find_bridge_channel(bridge_channel->bridge, bridge_channel->swap))) {
			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);
			ast_bridge_change_state(bridge_channel2, AST_BRIDGE_CHANNEL_STATE_HANGUP);
		}

		bridge_channel->swap = NULL;
	} else if (ast_test_flag(&bridge_channel->bridge->feature_flags, AST_BRIDGE_FLAG_SMART)) {
		/* Perform the smart bridge operation, basically see if we need to move around between technologies */
		smart_bridge_operation(bridge_channel->bridge, bridge_channel, bridge_channel->bridge->num);
	}

	/* Make the channel compatible with the bridge */
	bridge_make_compatible(bridge_channel->bridge, bridge_channel);

	/* Tell the bridge technology we are joining so they set us up */
	if (bridge_channel->bridge->technology->join) {
		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);
		if (bridge_channel->bridge->technology->join(bridge_channel->bridge, bridge_channel)) {
			ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n", bridge_channel->bridge->technology->name, bridge_channel, bridge_channel->bridge);
		}
	}

	/* Actually execute the respective threading model, and keep our bridge thread alive */
	while (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
		/* Update bridge pointer on channel */
		bridge_channel->chan->bridge = bridge_channel->bridge;
		/* If the technology requires a thread and one is not running, start it up */
		if (bridge_channel->bridge->thread == AST_PTHREADT_NULL && (bridge_channel->bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_THREAD)) {
			bridge_channel->bridge->stop = 0;
			ast_debug(1, "Starting a bridge thread for bridge %p\n", bridge_channel->bridge);
			ao2_ref(bridge_channel->bridge, +1);
			if (ast_pthread_create(&bridge_channel->bridge->thread, NULL, bridge_thread, bridge_channel->bridge)) {
				ast_debug(1, "Failed to create a bridge thread for bridge %p, giving it another go.\n", bridge_channel->bridge);
				ao2_ref(bridge_channel->bridge, -1);
				continue;
			}
		}
		/* Execute the threading model */
		state = (bridge_channel->bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTITHREADED ? bridge_channel_join_multithreaded(bridge_channel) : bridge_channel_join_singlethreaded(bridge_channel));
		/* Depending on the above state see what we need to do */
		if (state == AST_BRIDGE_CHANNEL_STATE_FEATURE) {
			bridge_channel_suspend(bridge_channel->bridge, bridge_channel);
			bridge_channel_feature(bridge_channel->bridge, bridge_channel);
			bridge_channel_unsuspend(bridge_channel->bridge, bridge_channel);
		} else if (state == AST_BRIDGE_CHANNEL_STATE_DTMF) {
			bridge_channel_suspend(bridge_channel->bridge, bridge_channel);
			bridge_channel_dtmf_stream(bridge_channel->bridge, bridge_channel);
			bridge_channel_unsuspend(bridge_channel->bridge, bridge_channel);
		}
	}

	bridge_channel->chan->bridge = NULL;

	/* See if we need to dissolve the bridge itself if they hung up */
	if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_END) {
		bridge_check_dissolve(bridge_channel->bridge, bridge_channel);
	}

	/* Tell the bridge technology we are leaving so they tear us down */
	if (bridge_channel->bridge->technology->leave) {
		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);
		if (bridge_channel->bridge->technology->leave(bridge_channel->bridge, bridge_channel)) {
			ast_debug(1, "Bridge technology %s failed to leave %p from bridge %p\n", bridge_channel->bridge->technology->name, bridge_channel, bridge_channel->bridge);
		}
	}

	/* Remove channel from the bridge */
	bridge_channel->bridge->num--;
	AST_LIST_REMOVE(&bridge_channel->bridge->channels, bridge_channel, entry);

	bridge_array_remove(bridge_channel->bridge, bridge_channel->chan);

	/* Perform the smart bridge operation if needed since a channel has left */
	if (ast_test_flag(&bridge_channel->bridge->feature_flags, AST_BRIDGE_FLAG_SMART)) {
		smart_bridge_operation(bridge_channel->bridge, NULL, bridge_channel->bridge->num);
	}

	ao2_unlock(bridge_channel->bridge);

	/* Restore original formats of the channel as they came in */
	if (bridge_channel->chan->readformat != formats[0]) {
		ast_debug(1, "Bridge is returning %p to read format %s(%d)\n", bridge_channel, ast_getformatname(formats[0]), formats[0]);
		if (ast_set_read_format(bridge_channel->chan, formats[0])) {
			ast_debug(1, "Bridge failed to return channel %p to read format %s(%d)\n", bridge_channel, ast_getformatname(formats[0]), formats[0]);
		}
	}
	if (bridge_channel->chan->writeformat != formats[1]) {
		ast_debug(1, "Bridge is returning %p to write format %s(%d)\n", bridge_channel, ast_getformatname(formats[1]), formats[1]);
		if (ast_set_write_format(bridge_channel->chan, formats[1])) {
			ast_debug(1, "Bridge failed to return channel %p to write format %s(%d)\n", bridge_channel, ast_getformatname(formats[1]), formats[1]);
		}
	}

	return bridge_channel->state;
}

enum ast_bridge_channel_state ast_bridge_join(struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap, struct ast_bridge_features *features)
{
	struct ast_bridge_channel bridge_channel = {
		.chan = chan,
		.swap = swap,
		.bridge = bridge,
		.features = features,
	};
	enum ast_bridge_channel_state state;

	/* Initialize various other elements of the bridge channel structure that we can't do above */
	ast_mutex_init(&bridge_channel.lock);
	ast_cond_init(&bridge_channel.cond, NULL);

	ao2_ref(bridge_channel.bridge, +1);

	state = bridge_channel_join(&bridge_channel);

	ao2_ref(bridge_channel.bridge, -1);

	/* Destroy some elements of the bridge channel structure above */
	ast_mutex_destroy(&bridge_channel.lock);
	ast_cond_destroy(&bridge_channel.cond);

	return state;
}

/*! \brief Thread responsible for imparted bridged channels */
static void *bridge_channel_thread(void *data)
{
	struct ast_bridge_channel *bridge_channel = data;
	enum ast_bridge_channel_state state;

	state = bridge_channel_join(bridge_channel);

	ao2_ref(bridge_channel->bridge, -1);

	/* 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 */
	if (state == AST_BRIDGE_CHANNEL_STATE_END || state == AST_BRIDGE_CHANNEL_STATE_HANGUP) {
		ast_hangup(bridge_channel->chan);
	}

	/* Destroy elements of the bridge channel structure and the bridge channel structure itself */
	ast_mutex_destroy(&bridge_channel->lock);
	ast_cond_destroy(&bridge_channel->cond);
	ast_free(bridge_channel);

	return NULL;
}

int ast_bridge_impart(struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap, struct ast_bridge_features *features)
{
	struct ast_bridge_channel *bridge_channel = NULL;

	/* Try to allocate a structure for the bridge channel */
	if (!(bridge_channel = ast_calloc(1, sizeof(*bridge_channel)))) {
		return -1;
	}

	/* Setup various parameters */
	bridge_channel->chan = chan;
	bridge_channel->swap = swap;
	bridge_channel->bridge = bridge;
	bridge_channel->features = features;

	/* Initialize our mutex lock and condition */
	ast_mutex_init(&bridge_channel->lock);
	ast_cond_init(&bridge_channel->cond, NULL);

	/* Bump up the reference count on the bridge, it'll get decremented later */
	ao2_ref(bridge, +1);

	/* Actually create the thread that will handle the channel */
	if (ast_pthread_create(&bridge_channel->thread, NULL, bridge_channel_thread, bridge_channel)) {
		ao2_ref(bridge, -1);
		ast_cond_destroy(&bridge_channel->cond);
		ast_mutex_destroy(&bridge_channel->lock);
		ast_free(bridge_channel);
		return -1;
	}

	return 0;
}

int ast_bridge_depart(struct ast_bridge *bridge, struct ast_channel *chan)
{
	struct ast_bridge_channel *bridge_channel = NULL;
	pthread_t thread;

	ao2_lock(bridge);

	/* Try to find the channel that we want to depart */
	if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
		ao2_unlock(bridge);
		return -1;
	}

	ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_DEPART);
	thread = bridge_channel->thread;

	ao2_unlock(bridge);

	pthread_join(thread, NULL);

	return 0;
}

int ast_bridge_remove(struct ast_bridge *bridge, struct ast_channel *chan)
{
	struct ast_bridge_channel *bridge_channel = NULL;

	ao2_lock(bridge);

	/* Try to find the channel that we want to remove */
	if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
		ao2_unlock(bridge);
		return -1;
	}

	ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);

	ao2_unlock(bridge);

	return 0;
}

int ast_bridge_merge(struct ast_bridge *bridge0, struct ast_bridge *bridge1)
{
	struct ast_bridge_channel *bridge_channel = NULL;

	ao2_lock(bridge0);
	ao2_lock(bridge1);

	/* If the first bridge currently has 2 channels and is not capable of becoming a multimixing bridge we can not merge */
	if ((bridge0->num + bridge1->num) > 2 && (!(bridge0->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) && !ast_test_flag(&bridge0->feature_flags, AST_BRIDGE_FLAG_SMART))) {
		ao2_unlock(bridge1);
		ao2_unlock(bridge0);
		ast_debug(1, "Can't merge bridge %p into bridge %p, multimix is needed and it could not be acquired.\n", bridge1, bridge0);
		return -1;
	}

	ast_debug(1, "Merging channels from bridge %p into bridge %p\n", bridge1, bridge0);

	/* Perform smart bridge operation on bridge we are merging into so it can change bridge technology if needed */
	if (smart_bridge_operation(bridge0, NULL, bridge0->num + bridge1->num)) {
		ao2_unlock(bridge1);
		ao2_unlock(bridge0);
		ast_debug(1, "Can't merge bridge %p into bridge %p, tried to perform smart bridge operation and failed.\n", bridge1, bridge0);
		return -1;
	}

	/* If a thread is currently executing on bridge1 tell it to stop */
	if (bridge1->thread) {
		ast_debug(1, "Telling bridge thread on bridge %p to stop as it is being merged into %p\n", bridge1, bridge0);
		bridge1->thread = AST_PTHREADT_STOP;
	}

	/* Move channels from bridge1 over to bridge0 */
	while ((bridge_channel = AST_LIST_REMOVE_HEAD(&bridge1->channels, entry))) {
		/* Tell the technology handling bridge1 that the bridge channel is leaving */
		if (bridge1->technology->leave) {
			ast_debug(1, "Giving bridge technology %s notification that %p is leaving bridge %p\n", bridge1->technology->name, bridge_channel, bridge1);
			if (bridge1->technology->leave(bridge1, bridge_channel)) {
				ast_debug(1, "Bridge technology %s failed to allow %p to leave bridge %p\n", bridge1->technology->name, bridge_channel, bridge1);
			}
		}

		/* Drop channel count and reference count on the bridge they are leaving */
		bridge1->num--;
		ao2_ref(bridge1, -1);

		bridge_array_remove(bridge1, bridge_channel->chan);

		/* Now add them into the bridge they are joining, increase channel count, and bump up reference count */
		bridge_channel->bridge = bridge0;
		AST_LIST_INSERT_TAIL(&bridge0->channels, bridge_channel, entry);
		bridge0->num++;
		ao2_ref(bridge0, +1);

		bridge_array_add(bridge0, bridge_channel->chan);

		/* Make the channel compatible with the new bridge it is joining or else formats would go amuck */
		bridge_make_compatible(bridge0, bridge_channel);

		/* Tell the technology handling bridge0 that the bridge channel is joining */
		if (bridge0->technology->join) {
			ast_debug(1, "Giving bridge technology %s notification that %p is joining bridge %p\n", bridge0->technology->name, bridge_channel, bridge0);
			if (bridge0->technology->join(bridge0, bridge_channel)) {
				ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n", bridge0->technology->name, bridge_channel, bridge0);
			}
		}

		/* Poke the bridge channel, this will cause it to wake up and execute the proper threading model for the new bridge it is in */
		pthread_kill(bridge_channel->thread, SIGURG);
		ast_mutex_lock(&bridge_channel->lock);
		ast_cond_signal(&bridge_channel->cond);
		ast_mutex_unlock(&bridge_channel->lock);
	}

	ast_debug(1, "Merged channels from bridge %p into bridge %p\n", bridge1, bridge0);

	ao2_unlock(bridge1);
	ao2_unlock(bridge0);

	return 0;
}

int ast_bridge_suspend(struct ast_bridge *bridge, struct ast_channel *chan)
{
	struct ast_bridge_channel *bridge_channel;

	ao2_lock(bridge);

	if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
		ao2_unlock(bridge);
		return -1;
	}

	bridge_channel_suspend(bridge, bridge_channel);

	ao2_unlock(bridge);

	return 0;
}

int ast_bridge_unsuspend(struct ast_bridge *bridge, struct ast_channel *chan)
{
	struct ast_bridge_channel *bridge_channel;

	ao2_lock(bridge);

	if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
		ao2_unlock(bridge);
		return -1;
	}

	bridge_channel_unsuspend(bridge, bridge_channel);

	ao2_unlock(bridge);

	return 0;
}

void ast_bridge_technology_suspend(struct ast_bridge_technology *technology)
{
	technology->suspended = 1;
	return;
}

void ast_bridge_technology_unsuspend(struct ast_bridge_technology *technology)
{
	technology->suspended = 0;
	return;
}

int ast_bridge_features_register(enum ast_bridge_builtin_feature feature, ast_bridge_features_hook_callback callback, const char *dtmf)
{
	if (builtin_features_handlers[feature]) {
		return -1;
	}

	if (!ast_strlen_zero(dtmf)) {
		ast_copy_string(builtin_features_dtmf[feature], dtmf, sizeof(builtin_features_dtmf[feature]));
	}

	builtin_features_handlers[feature] = callback;

	return 0;
}

int ast_bridge_features_unregister(enum ast_bridge_builtin_feature feature)
{
	if (!builtin_features_handlers[feature]) {
		return -1;
	}

	builtin_features_handlers[feature] = NULL;

	return 0;
}

int ast_bridge_features_hook(struct ast_bridge_features *features, const char *dtmf, ast_bridge_features_hook_callback callback, void *hook_pvt)
{
	struct ast_bridge_features_hook *hook = NULL;

	/* Allocate new memory and setup it's various variables */
	if (!(hook = ast_calloc(1, sizeof(*hook)))) {
		return -1;
	}

	ast_copy_string(hook->dtmf, dtmf, sizeof(hook->dtmf));
	hook->callback = callback;
	hook->hook_pvt = hook_pvt;

	/* Once done we add it onto the list. Now it will be picked up when DTMF is used */
	AST_LIST_INSERT_TAIL(&features->hooks, hook, entry);

	features->usable = 1;

	return 0;
}

int ast_bridge_features_enable(struct ast_bridge_features *features, enum ast_bridge_builtin_feature feature, const char *dtmf, void *config)
{
	/* If no alternate DTMF stream was provided use the default one */
	if (ast_strlen_zero(dtmf)) {
		dtmf = builtin_features_dtmf[feature];
		/* If no DTMF is still available (ie: it has been disabled) then error out now */
		if (ast_strlen_zero(dtmf)) {
			ast_debug(1, "Failed to enable built in feature %d on %p, no DTMF string is available for it.\n", feature, features);
			return -1;
		}
	}

	if (!builtin_features_handlers[feature]) {
		return -1;
	}

	/* The rest is basically pretty easy. We create another hook using the built in feature's callback and DTMF, easy as pie. */
	return ast_bridge_features_hook(features, dtmf, builtin_features_handlers[feature], config);
}

int ast_bridge_features_set_flag(struct ast_bridge_features *features, enum ast_bridge_feature_flags flag)
{
	ast_set_flag(&features->feature_flags, flag);
	features->usable = 1;
	return 0;
}

int ast_bridge_features_init(struct ast_bridge_features *features)
{
	/* Zero out the structure */
	memset(features, 0, sizeof(*features));

	/* Initialize the hooks list, just in case */
	AST_LIST_HEAD_INIT_NOLOCK(&features->hooks);

	return 0;
}

int ast_bridge_features_cleanup(struct ast_bridge_features *features)
{
	struct ast_bridge_features_hook *hook = NULL;

	/* This is relatively simple, hooks are kept as a list on the features structure so we just pop them off and free them */
	while ((hook = AST_LIST_REMOVE_HEAD(&features->hooks, entry))) {
		ast_free(hook);
	}

	return 0;
}

int ast_bridge_dtmf_stream(struct ast_bridge *bridge, const char *dtmf, struct ast_channel *chan)
{
	struct ast_bridge_channel *bridge_channel = NULL;

	ao2_lock(bridge);

	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
		if (bridge_channel->chan == chan) {
			continue;
		}
		ast_copy_string(bridge_channel->dtmf_stream_q, dtmf, sizeof(bridge_channel->dtmf_stream_q));
		ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_DTMF);
	}

	ao2_unlock(bridge);

	return 0;
}