forked from asterisk/asterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge_channel.c
3179 lines (2815 loc) · 97.6 KB
/
bridge_channel.c
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
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2007 - 2009, Digium, Inc.
*
* Joshua Colp <[email protected]>
*
* 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 Bridging Channel API
*
* \author Joshua Colp <[email protected]>
* \author Richard Mudgett <[email protected]>
* \author Matt Jordan <[email protected]>
*
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
#include <signal.h>
#include "asterisk/heap.h"
#include "asterisk/alertpipe.h"
#include "asterisk/astobj2.h"
#include "asterisk/stringfields.h"
#include "asterisk/app.h"
#include "asterisk/pbx.h"
#include "asterisk/channel.h"
#include "asterisk/timing.h"
#include "asterisk/bridge.h"
#include "asterisk/bridge_channel.h"
#include "asterisk/bridge_after.h"
#include "asterisk/bridge_channel_internal.h"
#include "asterisk/bridge_internal.h"
#include "asterisk/stasis_bridges.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/musiconhold.h"
#include "asterisk/features_config.h"
#include "asterisk/parking.h"
#include "asterisk/causes.h"
#include "asterisk/test.h"
#include "asterisk/sem.h"
#include "asterisk/stream.h"
#include "asterisk/message.h"
/*!
* \brief Used to queue an action frame onto a bridge channel and write an action frame into a bridge.
* \since 12.0.0
*
* \param bridge_channel Which channel work with.
* \param action Type of bridge action frame.
* \param data Frame payload data to pass.
* \param datalen Frame payload data length to pass.
*
* \retval 0 on success.
* \retval -1 on error.
*/
typedef int (*ast_bridge_channel_post_action_data)(struct ast_bridge_channel *bridge_channel, enum bridge_channel_action_type action, const void *data, size_t datalen);
/*!
* \brief Counter used for assigning synchronous bridge action IDs
*/
static int sync_ids;
/*!
* \brief Frame payload for synchronous bridge actions.
*
* The payload serves as a wrapper around the actual payload of the
* frame, with the addition of an id used to find the associated
* bridge_sync object.
*/
struct sync_payload {
/*! Unique ID for this synchronous action */
unsigned int id;
/*! Actual frame data to process */
unsigned char data[0];
};
/*!
* \brief Synchronous bridge action object.
*
* Synchronous bridge actions require the ability for one thread to wait
* and for another thread to indicate that the action has completed. This
* structure facilitates that goal by providing synchronization structures.
*/
struct bridge_sync {
/*! Unique ID of this synchronization object. Corresponds with ID in synchronous frame payload */
unsigned int id;
/*! Semaphore used for synchronization */
struct ast_sem sem;
/*! Pointer to next entry in the list */
AST_LIST_ENTRY(bridge_sync) list;
};
/*!
* \brief List holding active synchronous action objects.
*/
static AST_RWLIST_HEAD_STATIC(sync_structs, bridge_sync);
/*!
* \brief initialize a synchronous bridge object.
*
* This both initializes the structure and adds it to the list of
* synchronization structures.
*
* \param sync_struct The synchronization object to initialize.
* \param id ID to assign to the synchronization object.
*/
static void bridge_sync_init(struct bridge_sync *sync_struct, unsigned int id)
{
memset(sync_struct, 0, sizeof(*sync_struct));
sync_struct->id = id;
ast_sem_init(&sync_struct->sem, 0, 0);
AST_RWLIST_WRLOCK(&sync_structs);
AST_RWLIST_INSERT_TAIL(&sync_structs, sync_struct, list);
AST_RWLIST_UNLOCK(&sync_structs);
}
/*!
* \brief Clean up a syncrhonization bridge object.
*
* This frees fields within the synchronization object and removes
* it from the list of active synchronization objects.
*
* Since synchronization objects are stack-allocated, it is vital
* that this is called before the synchronization object goes
* out of scope.
*
* \param sync_struct Synchronization object to clean up.
*/
static void bridge_sync_cleanup(struct bridge_sync *sync_struct)
{
struct bridge_sync *iter;
AST_RWLIST_WRLOCK(&sync_structs);
AST_LIST_TRAVERSE_SAFE_BEGIN(&sync_structs, iter, list) {
if (iter->id == sync_struct->id) {
AST_LIST_REMOVE_CURRENT(list);
break;
}
}
AST_LIST_TRAVERSE_SAFE_END;
AST_RWLIST_UNLOCK(&sync_structs);
ast_sem_destroy(&sync_struct->sem);
}
/*!
* \brief Failsafe for synchronous bridge action waiting.
*
* When waiting for a synchronous bridge action to complete,
* if there is a frame resource leak somewhere, it is possible
* that we will never get notified that the synchronous action
* completed.
*
* If a significant amount of time passes, then we will abandon
* waiting for the synchrnous bridge action to complete.
*
* This constant represents the number of milliseconds we will
* wait for the bridge action to complete.
*/
#define PLAYBACK_TIMEOUT (600 * 1000)
/*!
* \brief Wait for a synchronous bridge action to complete.
*
* \param sync_struct Synchronization object corresponding to the bridge action.
*/
static void bridge_sync_wait(struct bridge_sync *sync_struct)
{
struct timeval timeout_val = ast_tvadd(ast_tvnow(), ast_samp2tv(PLAYBACK_TIMEOUT, 1000));
struct timespec timeout_spec = {
.tv_sec = timeout_val.tv_sec,
.tv_nsec = timeout_val.tv_usec * 1000,
};
ast_sem_timedwait(&sync_struct->sem, &timeout_spec);
}
/*!
* \brief Signal that waiting for a synchronous bridge action is no longer necessary.
*
* This may occur for several reasons
* \li The synchronous bridge action has completed.
* \li The bridge channel has been removed from the bridge.
* \li The synchronous bridge action could not be queued.
*
* \param sync_struct Synchronization object corresponding to the bridge action.
*/
static void bridge_sync_signal(struct bridge_sync *sync_struct)
{
ast_sem_post(&sync_struct->sem);
}
struct ast_channel *ast_bridge_channel_get_chan(struct ast_bridge_channel *bridge_channel)
{
struct ast_channel *chan;
ao2_lock(bridge_channel);
chan = ao2_bump(bridge_channel->chan);
ao2_unlock(bridge_channel);
return chan;
}
void ast_bridge_channel_lock_bridge(struct ast_bridge_channel *bridge_channel)
{
struct ast_bridge *bridge;
for (;;) {
/* Safely get the bridge pointer */
ast_bridge_channel_lock(bridge_channel);
bridge = bridge_channel->bridge;
ao2_ref(bridge, +1);
ast_bridge_channel_unlock(bridge_channel);
/* Lock the bridge and see if it is still the bridge we need to lock. */
ast_bridge_lock(bridge);
if (bridge == bridge_channel->bridge) {
ao2_ref(bridge, -1);
return;
}
ast_bridge_unlock(bridge);
ao2_ref(bridge, -1);
}
}
int ast_bridge_channel_notify_talking(struct ast_bridge_channel *bridge_channel, int started_talking)
{
struct ast_frame action = {
.frametype = AST_FRAME_BRIDGE_ACTION,
.subclass.integer = started_talking
? BRIDGE_CHANNEL_ACTION_TALKING_START : BRIDGE_CHANNEL_ACTION_TALKING_STOP,
};
return ast_bridge_channel_queue_frame(bridge_channel, &action);
}
/*!
* \internal
* \brief Poke the bridge_channel thread
*/
static void bridge_channel_poke(struct ast_bridge_channel *bridge_channel)
{
if (!pthread_equal(pthread_self(), bridge_channel->thread)) {
/* Wake up the bridge channel thread. */
ast_queue_frame(bridge_channel->chan, &ast_null_frame);
}
}
/*!
* \internal
* \brief Set actual cause on channel.
* \since 12.0.0
*
* \param chan Channel to set cause.
* \param cause Cause to set on channel.
* If cause <= 0 then use cause on channel if cause still <= 0 use AST_CAUSE_NORMAL_CLEARING.
*
* \return Actual cause set on channel.
*/
static int channel_set_cause(struct ast_channel *chan, int cause)
{
ast_channel_lock(chan);
if (cause <= 0) {
cause = ast_channel_hangupcause(chan);
if (cause <= 0) {
cause = AST_CAUSE_NORMAL_CLEARING;
}
}
ast_channel_hangupcause_set(chan, cause);
ast_channel_unlock(chan);
return cause;
}
void ast_bridge_channel_leave_bridge_nolock(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
{
if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
return;
}
ast_debug(1, "Setting %p(%s) state from:%u to:%u\n",
bridge_channel, ast_channel_name(bridge_channel->chan), bridge_channel->state,
new_state);
channel_set_cause(bridge_channel->chan, cause);
ast_channel_lock(bridge_channel->chan);
ast_bridge_vars_set(bridge_channel->chan, NULL, NULL);
ast_channel_unlock(bridge_channel->chan);
/* Change the state on the bridge channel */
bridge_channel->state = new_state;
bridge_channel_poke(bridge_channel);
}
void ast_bridge_channel_leave_bridge(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
{
ast_bridge_channel_lock(bridge_channel);
ast_bridge_channel_leave_bridge_nolock(bridge_channel, new_state, cause);
ast_bridge_channel_unlock(bridge_channel);
}
struct ast_bridge_channel *ast_bridge_channel_peer(struct ast_bridge_channel *bridge_channel)
{
struct ast_bridge *bridge = bridge_channel->bridge;
struct ast_bridge_channel *other = NULL;
if (bridge_channel->in_bridge && bridge->num_channels == 2) {
AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
if (other != bridge_channel) {
break;
}
}
}
return other;
}
void ast_bridge_channel_restore_formats(struct ast_bridge_channel *bridge_channel)
{
ast_assert(bridge_channel->read_format != NULL);
ast_assert(bridge_channel->write_format != NULL);
ast_channel_lock(bridge_channel->chan);
/* Restore original formats of the channel as they came in */
if (ast_format_cmp(ast_channel_readformat(bridge_channel->chan), bridge_channel->read_format) == AST_FORMAT_CMP_NOT_EQUAL) {
ast_debug(1, "Bridge is returning %p(%s) to read format %s\n",
bridge_channel, ast_channel_name(bridge_channel->chan),
ast_format_get_name(bridge_channel->read_format));
if (ast_set_read_format(bridge_channel->chan, bridge_channel->read_format)) {
ast_debug(1, "Bridge failed to return %p(%s) to read format %s\n",
bridge_channel, ast_channel_name(bridge_channel->chan),
ast_format_get_name(bridge_channel->read_format));
}
}
if (ast_format_cmp(ast_channel_writeformat(bridge_channel->chan), bridge_channel->write_format) == AST_FORMAT_CMP_NOT_EQUAL) {
ast_debug(1, "Bridge is returning %p(%s) to write format %s\n",
bridge_channel, ast_channel_name(bridge_channel->chan),
ast_format_get_name(bridge_channel->write_format));
if (ast_set_write_format(bridge_channel->chan, bridge_channel->write_format)) {
ast_debug(1, "Bridge failed to return %p(%s) to write format %s\n",
bridge_channel, ast_channel_name(bridge_channel->chan),
ast_format_get_name(bridge_channel->write_format));
}
}
ast_channel_unlock(bridge_channel->chan);
}
struct ast_bridge *ast_bridge_channel_merge_inhibit(struct ast_bridge_channel *bridge_channel, int request)
{
struct ast_bridge *bridge;
ast_bridge_channel_lock_bridge(bridge_channel);
bridge = bridge_channel->bridge;
ao2_ref(bridge, +1);
bridge_merge_inhibit_nolock(bridge, request);
ast_bridge_unlock(bridge);
return bridge;
}
void ast_bridge_channel_update_linkedids(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
{
struct ast_bridge_channel *other;
struct ast_bridge *bridge = bridge_channel->bridge;
struct ast_channel *oldest_linkedid_chan = bridge_channel->chan;
AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
if (other == swap) {
continue;
}
oldest_linkedid_chan = ast_channel_internal_oldest_linkedid(
oldest_linkedid_chan, other->chan);
}
ast_channel_lock(bridge_channel->chan);
ast_channel_internal_copy_linkedid(bridge_channel->chan, oldest_linkedid_chan);
ast_channel_unlock(bridge_channel->chan);
AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
if (other == swap) {
continue;
}
ast_channel_lock(other->chan);
ast_channel_internal_copy_linkedid(other->chan, oldest_linkedid_chan);
ast_channel_unlock(other->chan);
}
}
/*!
* \internal
* \brief Set dest's empty peeraccount with the src's non-empty accountcode.
* \since 12.5.0
*
* \param dest Channel to update peeraccount.
* \param src Channel to get accountcode from.
*
* \note Both channels are already locked.
*
* \return Nothing
*/
static void channel_fill_empty_peeraccount(struct ast_channel *dest, struct ast_channel *src)
{
if (ast_strlen_zero(ast_channel_peeraccount(dest))
&& !ast_strlen_zero(ast_channel_accountcode(src))) {
ast_debug(1, "Setting channel %s peeraccount with channel %s accountcode '%s'.\n",
ast_channel_name(dest),
ast_channel_name(src), ast_channel_accountcode(src));
ast_channel_peeraccount_set(dest, ast_channel_accountcode(src));
}
}
/*!
* \internal
* \brief Set dest's empty accountcode with the src's non-empty peeraccount.
* \since 12.5.0
*
* \param dest Channel to update accountcode.
* \param src Channel to get peeraccount from.
*
* \note Both channels are already locked.
*
* \return Nothing
*/
static void channel_fill_empty_accountcode(struct ast_channel *dest, struct ast_channel *src)
{
if (ast_strlen_zero(ast_channel_accountcode(dest))
&& !ast_strlen_zero(ast_channel_peeraccount(src))) {
ast_debug(1, "Setting channel %s accountcode with channel %s peeraccount '%s'.\n",
ast_channel_name(dest),
ast_channel_name(src), ast_channel_peeraccount(src));
ast_channel_accountcode_set(dest, ast_channel_peeraccount(src));
}
}
/*!
* \internal
* \brief Set empty peeraccount and accountcode in a channel from the other channel.
* \since 12.5.0
*
* \param c0 First bridge channel to update.
* \param c1 Second bridge channel to update.
*
* \note Both channels are already locked.
*
* \return Nothing
*/
static void channel_set_empty_accountcodes(struct ast_channel *c0, struct ast_channel *c1)
{
/* Set empty peeraccount from the other channel's accountcode. */
channel_fill_empty_peeraccount(c0, c1);
channel_fill_empty_peeraccount(c1, c0);
/* Set empty accountcode from the other channel's peeraccount. */
channel_fill_empty_accountcode(c0, c1);
channel_fill_empty_accountcode(c1, c0);
}
/*!
* \internal
* \brief Update dest's peeraccount with the src's different accountcode.
* \since 12.5.0
*
* \param dest Channel to update peeraccount.
* \param src Channel to get accountcode from.
*
* \note Both channels are already locked.
*
* \return Nothing
*/
static void channel_update_peeraccount(struct ast_channel *dest, struct ast_channel *src)
{
if (strcmp(ast_channel_accountcode(src), ast_channel_peeraccount(dest))) {
ast_debug(1, "Changing channel %s peeraccount '%s' to match channel %s accountcode '%s'.\n",
ast_channel_name(dest), ast_channel_peeraccount(dest),
ast_channel_name(src), ast_channel_accountcode(src));
ast_channel_peeraccount_set(dest, ast_channel_accountcode(src));
}
}
/*!
* \internal
* \brief Update peeraccounts to match the other channel's accountcode.
* \since 12.5.0
*
* \param c0 First channel to update.
* \param c1 Second channel to update.
*
* \note Both channels are already locked.
*
* \return Nothing
*/
static void channel_update_peeraccounts(struct ast_channel *c0, struct ast_channel *c1)
{
channel_update_peeraccount(c0, c1);
channel_update_peeraccount(c1, c0);
}
/*!
* \internal
* \brief Update channel accountcodes because a channel is joining a bridge.
* \since 12.5.0
*
* \param joining Channel joining the bridge.
* \param swap Channel being replaced by the joining channel. May be NULL.
*
* \note The bridge must be locked prior to calling this function.
*
* \return Nothing
*/
static void bridge_channel_update_accountcodes_joining(struct ast_bridge_channel *joining, struct ast_bridge_channel *swap)
{
struct ast_bridge *bridge = joining->bridge;
struct ast_bridge_channel *other;
unsigned int swap_in_bridge = 0;
unsigned int will_be_two_party;
/*
* Only update the peeraccount to match if the joining channel
* will make it a two party bridge.
*/
if (bridge->num_channels <= 2 && swap) {
AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
if (other == swap) {
swap_in_bridge = 1;
break;
}
}
}
will_be_two_party = (1 == bridge->num_channels - swap_in_bridge);
AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
if (other == swap) {
continue;
}
ast_assert(joining != other);
ast_channel_lock_both(joining->chan, other->chan);
channel_set_empty_accountcodes(joining->chan, other->chan);
if (will_be_two_party) {
channel_update_peeraccounts(joining->chan, other->chan);
}
ast_channel_unlock(joining->chan);
ast_channel_unlock(other->chan);
}
}
/*!
* \internal
* \brief Update channel peeraccount codes because a channel has left a bridge.
* \since 12.5.0
*
* \param leaving Channel leaving the bridge. (Has already been removed actually)
*
* \note The bridge must be locked prior to calling this function.
*
* \return Nothing
*/
static void bridge_channel_update_accountcodes_leaving(struct ast_bridge_channel *leaving)
{
struct ast_bridge *bridge = leaving->bridge;
struct ast_bridge_channel *first;
struct ast_bridge_channel *second;
if (bridge->num_channels != 2 || bridge->dissolved) {
return;
}
first = AST_LIST_FIRST(&bridge->channels);
second = AST_LIST_LAST(&bridge->channels);
ast_assert(first && first != second);
ast_channel_lock_both(first->chan, second->chan);
channel_set_empty_accountcodes(first->chan, second->chan);
channel_update_peeraccounts(first->chan, second->chan);
ast_channel_unlock(second->chan);
ast_channel_unlock(first->chan);
}
void ast_bridge_channel_update_accountcodes(struct ast_bridge_channel *joining, struct ast_bridge_channel *leaving)
{
if (joining) {
bridge_channel_update_accountcodes_joining(joining, leaving);
} else {
bridge_channel_update_accountcodes_leaving(leaving);
}
}
void ast_bridge_channel_kick(struct ast_bridge_channel *bridge_channel, int cause)
{
struct ast_bridge_features *features = bridge_channel->features;
struct ast_bridge_hook *hook;
struct ao2_iterator iter;
ast_bridge_channel_lock(bridge_channel);
if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
channel_set_cause(bridge_channel->chan, cause);
cause = 0;
}
ast_bridge_channel_unlock(bridge_channel);
/* Run any hangup hooks. */
iter = ao2_iterator_init(features->other_hooks, 0);
for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
int remove_me;
if (hook->type != AST_BRIDGE_HOOK_TYPE_HANGUP) {
continue;
}
remove_me = hook->callback(bridge_channel, hook->hook_pvt);
if (remove_me) {
ast_debug(1, "Hangup hook %p is being removed from %p(%s)\n",
hook, bridge_channel, ast_channel_name(bridge_channel->chan));
ao2_unlink(features->other_hooks, hook);
}
}
ao2_iterator_destroy(&iter);
/* Default hangup action. */
ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, cause);
}
/*!
* \internal
* \brief Write an \ref ast_frame into the bridge
* \since 12.0.0
*
* \param bridge_channel Which channel is queueing the frame.
* \param frame The frame to write into the bridge
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int bridge_channel_write_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
{
const struct ast_control_t38_parameters *t38_parameters;
int unmapped_stream_num;
int deferred;
ast_assert(frame->frametype != AST_FRAME_BRIDGE_ACTION_SYNC);
ast_bridge_channel_lock_bridge(bridge_channel);
/* Map the frame to the bridge. */
if (ast_channel_is_multistream(bridge_channel->chan)) {
unmapped_stream_num = frame->stream_num;
switch (frame->frametype) {
case AST_FRAME_VOICE:
case AST_FRAME_VIDEO:
case AST_FRAME_TEXT:
case AST_FRAME_IMAGE:
case AST_FRAME_RTCP:
/* These frames need to be mapped to an appropriate write stream */
if (frame->stream_num < 0) {
/* Map to default stream */
frame->stream_num = -1;
break;
}
ast_bridge_channel_lock(bridge_channel);
if (frame->stream_num < (int)AST_VECTOR_SIZE(&bridge_channel->stream_map.to_bridge)) {
frame->stream_num = AST_VECTOR_GET(
&bridge_channel->stream_map.to_bridge, frame->stream_num);
if (0 <= frame->stream_num) {
ast_bridge_channel_unlock(bridge_channel);
break;
}
}
ast_bridge_channel_unlock(bridge_channel);
ast_bridge_unlock(bridge_channel->bridge);
/*
* Ignore frame because we don't know how to map the frame
* or the bridge is not expecting any media from that
* stream.
*/
return 0;
case AST_FRAME_DTMF_BEGIN:
case AST_FRAME_DTMF_END:
/*
* XXX It makes sense that DTMF could be on any audio stream.
* For now we will only put it on the default audio stream.
*/
default:
frame->stream_num = -1;
break;
}
} else {
unmapped_stream_num = -1;
frame->stream_num = -1;
}
deferred = bridge_channel->bridge->technology->write(bridge_channel->bridge, bridge_channel, frame);
if (deferred) {
struct ast_frame *dup;
dup = ast_frdup(frame);
if (dup) {
/*
* We have to unmap the deferred frame so it comes back
* in like a new frame.
*/
dup->stream_num = unmapped_stream_num;
ast_bridge_channel_lock(bridge_channel);
AST_LIST_INSERT_HEAD(&bridge_channel->deferred_queue, dup, frame_list);
ast_bridge_channel_unlock(bridge_channel);
}
}
/* Remember any owed events to the bridge. */
switch (frame->frametype) {
case AST_FRAME_DTMF_BEGIN:
bridge_channel->owed.dtmf_tv = ast_tvnow();
bridge_channel->owed.dtmf_digit = frame->subclass.integer;
break;
case AST_FRAME_DTMF_END:
bridge_channel->owed.dtmf_digit = '\0';
break;
case AST_FRAME_CONTROL:
/*
* We explicitly will not remember HOLD/UNHOLD frames because
* things like attended transfers will handle them.
*/
switch (frame->subclass.integer) {
case AST_CONTROL_T38_PARAMETERS:
t38_parameters = frame->data.ptr;
switch (t38_parameters->request_response) {
case AST_T38_REQUEST_NEGOTIATE:
case AST_T38_NEGOTIATED:
bridge_channel->owed.t38_terminate = 1;
break;
case AST_T38_REQUEST_TERMINATE:
case AST_T38_TERMINATED:
case AST_T38_REFUSED:
bridge_channel->owed.t38_terminate = 0;
break;
default:
break;
}
break;
default:
break;
}
break;
default:
break;
}
ast_bridge_unlock(bridge_channel->bridge);
/*
* Claim successful write to bridge. If deferred frame
* support is added, claim successfully deferred.
*/
return 0;
}
/*!
* \internal
* \brief Cancel owed events by the channel to the bridge.
* \since 13.8.0
*
* \param bridge_channel Channel that owes events to the bridge.
*
* \note On entry, the bridge_channel->bridge is already locked.
*
* \return Nothing
*/
static void bridge_channel_cancel_owed_events(struct ast_bridge_channel *bridge_channel)
{
bridge_channel->owed.dtmf_digit = '\0';
bridge_channel->owed.t38_terminate = 0;
}
void bridge_channel_settle_owed_events(struct ast_bridge *orig_bridge, struct ast_bridge_channel *bridge_channel)
{
if (bridge_channel->owed.dtmf_digit) {
struct ast_frame frame = {
.frametype = AST_FRAME_DTMF_END,
.subclass.integer = bridge_channel->owed.dtmf_digit,
.src = "Bridge channel owed DTMF",
};
frame.len = ast_tvdiff_ms(ast_tvnow(), bridge_channel->owed.dtmf_tv);
if (frame.len < option_dtmfminduration) {
frame.len = option_dtmfminduration;
}
ast_log(LOG_DTMF, "DTMF end '%c' simulated to bridge %s because %s left. Duration %ld ms.\n",
bridge_channel->owed.dtmf_digit, orig_bridge->uniqueid,
ast_channel_name(bridge_channel->chan), frame.len);
bridge_channel->owed.dtmf_digit = '\0';
orig_bridge->technology->write(orig_bridge, NULL, &frame);
}
if (bridge_channel->owed.t38_terminate) {
struct ast_control_t38_parameters t38_parameters = {
.request_response = AST_T38_TERMINATED,
};
struct ast_frame frame = {
.frametype = AST_FRAME_CONTROL,
.subclass.integer = AST_CONTROL_T38_PARAMETERS,
.data.ptr = &t38_parameters,
.datalen = sizeof(t38_parameters),
.src = "Bridge channel owed T.38 terminate",
};
ast_debug(1, "T.38 terminate simulated to bridge %s because %s left.\n",
orig_bridge->uniqueid, ast_channel_name(bridge_channel->chan));
bridge_channel->owed.t38_terminate = 0;
orig_bridge->technology->write(orig_bridge, NULL, &frame);
}
}
void bridge_channel_queue_deferred_frames(struct ast_bridge_channel *bridge_channel)
{
struct ast_frame *frame;
ast_bridge_channel_lock(bridge_channel);
ast_channel_lock(bridge_channel->chan);
while ((frame = AST_LIST_REMOVE_HEAD(&bridge_channel->deferred_queue, frame_list))) {
ast_queue_frame_head(bridge_channel->chan, frame);
ast_frfree(frame);
}
ast_channel_unlock(bridge_channel->chan);
ast_bridge_channel_unlock(bridge_channel);
}
/*!
* \internal
* \brief Suspend a channel from a bridge.
*
* \param bridge_channel Channel to suspend.
*
* \note This function assumes bridge_channel->bridge is locked.
*
* \return Nothing
*/
void bridge_channel_internal_suspend_nolock(struct ast_bridge_channel *bridge_channel)
{
bridge_channel->suspended = 1;
if (bridge_channel->in_bridge) {
--bridge_channel->bridge->num_active;
}
/* Get technology bridge threads off of the channel. */
if (bridge_channel->bridge->technology->suspend) {
bridge_channel->bridge->technology->suspend(bridge_channel->bridge, bridge_channel);
}
}
/*!
* \internal
* \brief Suspend a channel from a bridge.
*
* \param bridge_channel Channel to suspend.
*
* \return Nothing
*/
static void bridge_channel_suspend(struct ast_bridge_channel *bridge_channel)
{
ast_bridge_channel_lock_bridge(bridge_channel);
bridge_channel_internal_suspend_nolock(bridge_channel);
ast_bridge_unlock(bridge_channel->bridge);
}
/*!
* \internal
* \brief Unsuspend a channel from a bridge.
*
* \param bridge_channel Channel to unsuspend.
*
* \note This function assumes bridge_channel->bridge is locked.
*
* \return Nothing
*/
void bridge_channel_internal_unsuspend_nolock(struct ast_bridge_channel *bridge_channel)
{
bridge_channel->suspended = 0;
if (bridge_channel->in_bridge) {
++bridge_channel->bridge->num_active;
}
/* Wake technology bridge threads to take care of channel again. */
if (bridge_channel->bridge->technology->unsuspend) {
bridge_channel->bridge->technology->unsuspend(bridge_channel->bridge, bridge_channel);
}
/* Wake suspended channel. */
ast_bridge_channel_lock(bridge_channel);
ast_cond_signal(&bridge_channel->cond);
ast_bridge_channel_unlock(bridge_channel);
}
/*!
* \internal
* \brief Unsuspend a channel from a bridge.
*
* \param bridge_channel Channel to unsuspend.
*
* \return Nothing
*/
static void bridge_channel_unsuspend(struct ast_bridge_channel *bridge_channel)
{
ast_bridge_channel_lock_bridge(bridge_channel);
bridge_channel_internal_unsuspend_nolock(bridge_channel);
ast_bridge_unlock(bridge_channel->bridge);
}
/*!
* \internal
* \brief Queue an action frame onto the bridge channel with data.
* \since 12.0.0
*
* \param bridge_channel Which channel to queue the frame onto.
* \param action Type of bridge action frame.
* \param data Frame payload data to pass.
* \param datalen Frame payload data length to pass.
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int bridge_channel_queue_action_data(struct ast_bridge_channel *bridge_channel,
enum bridge_channel_action_type action, const void *data, size_t datalen)
{
struct ast_frame frame = {
.frametype = AST_FRAME_BRIDGE_ACTION,
.subclass.integer = action,
.datalen = datalen,
.data.ptr = (void *) data,
};
return ast_bridge_channel_queue_frame(bridge_channel, &frame);
}
/*!
* \internal
* \brief Queue an action frame onto the bridge channel with data synchronously.
* \since 12.2.0
*
* The function will not return until the queued frame is freed.
*
* \param bridge_channel Which channel to queue the frame onto.
* \param action Type of bridge action frame.
* \param data Frame payload data to pass.
* \param datalen Frame payload data length to pass.
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int bridge_channel_queue_action_data_sync(struct ast_bridge_channel *bridge_channel,
enum bridge_channel_action_type action, const void *data, size_t datalen)
{
struct sync_payload *sync_payload;
int sync_payload_len = sizeof(*sync_payload) + datalen;
struct bridge_sync sync_struct;
struct ast_frame frame = {
.frametype = AST_FRAME_BRIDGE_ACTION_SYNC,
.subclass.integer = action,
};
/* Make sure we don't end up trying to wait on ourself to deliver the frame */
ast_assert(!pthread_equal(pthread_self(), bridge_channel->thread));
sync_payload = ast_alloca(sync_payload_len);
sync_payload->id = ast_atomic_fetchadd_int(&sync_ids, +1);
memcpy(sync_payload->data, data, datalen);
frame.datalen = sync_payload_len;
frame.data.ptr = sync_payload;
bridge_sync_init(&sync_struct, sync_payload->id);
if (ast_bridge_channel_queue_frame(bridge_channel, &frame)) {
bridge_sync_cleanup(&sync_struct);
return -1;
}
bridge_sync_wait(&sync_struct);
bridge_sync_cleanup(&sync_struct);
return 0;
}
/*!
* \internal
* \brief Write an action frame onto the bridge channel with data.
* \since 12.0.0
*
* \param bridge_channel Which channel to queue the frame onto.
* \param action Type of bridge action frame.
* \param data Frame payload data to pass.