/
app_confbridge.c
3444 lines (3050 loc) · 105 KB
/
app_confbridge.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-2008, Digium, Inc.
*
* Joshua Colp <jcolp@digium.com>
* David Vossel <dvossel@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 Conference Bridge application
*
* \author\verbatim Joshua Colp <jcolp@digium.com> \endverbatim
* \author\verbatim David Vossel <dvossel@digium.com> \endverbatim
*
* This is a conference bridge application utilizing the bridging core.
* \ingroup applications
*/
/*! \li \ref app_confbridge.c uses the configuration file \ref confbridge.conf
* \addtogroup configuration_file Configuration Files
*/
/*!
* \page confbridge.conf confbridge.conf
* \verbinclude confbridge.conf.sample
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include "asterisk/cli.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/lock.h"
#include "asterisk/bridge.h"
#include "asterisk/musiconhold.h"
#include "asterisk/say.h"
#include "asterisk/audiohook.h"
#include "asterisk/astobj2.h"
#include "confbridge/include/confbridge.h"
#include "asterisk/paths.h"
#include "asterisk/manager.h"
#include "asterisk/test.h"
#include "asterisk/stasis.h"
#include "asterisk/stasis_bridges.h"
#include "asterisk/json.h"
#include "asterisk/format_cache.h"
/*** DOCUMENTATION
<application name="ConfBridge" language="en_US">
<synopsis>
Conference bridge application.
</synopsis>
<syntax>
<parameter name="conference" required="true">
<para>Name of the conference bridge. You are not limited to just
numbers.</para>
</parameter>
<parameter name="bridge_profile">
<para>The bridge profile name from confbridge.conf. When left blank,
a dynamically built bridge profile created by the CONFBRIDGE dialplan
function is searched for on the channel and used. If no dynamic
profile is present, the 'default_bridge' profile found in
confbridge.conf is used. </para>
<para>It is important to note that while user profiles may be unique
for each participant, mixing bridge profiles on a single conference
is _NOT_ recommended and will produce undefined results.</para>
</parameter>
<parameter name="user_profile">
<para>The user profile name from confbridge.conf. When left blank,
a dynamically built user profile created by the CONFBRIDGE dialplan
function is searched for on the channel and used. If no dynamic
profile is present, the 'default_user' profile found in
confbridge.conf is used.</para>
</parameter>
<parameter name="menu">
<para>The name of the DTMF menu in confbridge.conf to be applied to
this channel. When left blank, a dynamically built menu profile
created by the CONFBRIDGE dialplan function is searched for on
the channel and used. If no dynamic profile is present, the
'default_menu' profile found in confbridge.conf is used.</para>
</parameter>
</syntax>
<description>
<para>Enters the user into a specified conference bridge. The user can
exit the conference by hangup or DTMF menu option.</para>
<para>This application sets the following channel variable upon completion:</para>
<variablelist>
<variable name="CONFBRIDGE_RESULT">
<value name="FAILED">The channel encountered an error and could not enter the conference.</value>
<value name="HANGUP">The channel exited the conference by hanging up.</value>
<value name="KICKED">The channel was kicked from the conference.</value>
<value name="ENDMARKED">The channel left the conference as a result of the last marked user leaving.</value>
<value name="DTMF">The channel pressed a DTMF sequence to exit the conference.</value>
</variable>
</variablelist>
</description>
<see-also>
<ref type="application">ConfBridge</ref>
<ref type="function">CONFBRIDGE</ref>
<ref type="function">CONFBRIDGE_INFO</ref>
</see-also>
</application>
<function name="CONFBRIDGE" language="en_US">
<synopsis>
Set a custom dynamic bridge, user, or menu profile on a channel for the ConfBridge application using the same options defined in confbridge.conf.
</synopsis>
<syntax>
<parameter name="type" required="true">
<para>Type refers to which type of profile the option belongs too. Type can be <literal>bridge</literal>, <literal>user</literal>, or
<literal>menu</literal>.</para>
</parameter>
<parameter name="option" required="true">
<para>Option refers to <filename>confbridge.conf</filename> option that is being set dynamically on this channel, or
<literal>clear</literal> to remove already applied options from the channel.</para>
</parameter>
</syntax>
<description>
<para>---- Example 1 ----</para>
<para>In this example the custom set user profile on this channel will automatically be used by the ConfBridge app.</para>
<para>exten => 1,1,Answer() </para>
<para>exten => 1,n,Set(CONFBRIDGE(user,announce_join_leave)=yes)</para>
<para>exten => 1,n,Set(CONFBRIDGE(user,startmuted)=yes)</para>
<para>exten => 1,n,ConfBridge(1) </para>
<para>---- Example 2 ----</para>
<para>This example shows how to use a predefined user or bridge profile in confbridge.conf as a template for a dynamic profile. Here we make a admin/marked user out of the default_user profile that is already defined in confbridge.conf.</para>
<para>exten => 1,1,Answer() </para>
<para>exten => 1,n,Set(CONFBRIDGE(user,template)=default_user)</para>
<para>exten => 1,n,Set(CONFBRIDGE(user,admin)=yes)</para>
<para>exten => 1,n,Set(CONFBRIDGE(user,marked)=yes)</para>
<para>exten => 1,n,ConfBridge(1)</para>
</description>
</function>
<function name="CONFBRIDGE_INFO" language="en_US">
<synopsis>
Get information about a ConfBridge conference.
</synopsis>
<syntax>
<parameter name="type" required="true">
<para>Type can be <literal>parties</literal>, <literal>admins</literal>, <literal>marked</literal>, or <literal>locked</literal>.</para>
</parameter>
<parameter name="conf" required="true">
<para>Conf refers to the name of the conference being referenced.</para>
</parameter>
</syntax>
<description>
<para>This function returns a non-negative integer for valid conference identifiers (0 or 1 for <literal>locked</literal>) and "" for invalid conference identifiers.</para>
</description>
</function>
<manager name="ConfbridgeList" language="en_US">
<synopsis>
List participants in a conference.
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
<parameter name="Conference" required="true">
<para>Conference number.</para>
</parameter>
</syntax>
<description>
<para>Lists all users in a particular ConfBridge conference.
ConfbridgeList will follow as separate events, followed by a final event called
ConfbridgeListComplete.</para>
</description>
</manager>
<manager name="ConfbridgeListRooms" language="en_US">
<synopsis>
List active conferences.
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
</syntax>
<description>
<para>Lists data about all active conferences.
ConfbridgeListRooms will follow as separate events, followed by a final event called
ConfbridgeListRoomsComplete.</para>
</description>
</manager>
<manager name="ConfbridgeMute" language="en_US">
<synopsis>
Mute a Confbridge user.
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
<parameter name="Conference" required="true" />
<parameter name="Channel" required="true">
<para>If this parameter is not a complete channel name, the first channel with this prefix will be used.</para>
<para>If this parameter is "all", all channels will be muted.</para>
<para>If this parameter is "participants", all non-admin channels will be muted.</para>
</parameter>
</syntax>
<description>
</description>
</manager>
<manager name="ConfbridgeUnmute" language="en_US">
<synopsis>
Unmute a Confbridge user.
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
<parameter name="Conference" required="true" />
<parameter name="Channel" required="true">
<para>If this parameter is not a complete channel name, the first channel with this prefix will be used.</para>
<para>If this parameter is "all", all channels will be unmuted.</para>
<para>If this parameter is "participants", all non-admin channels will be unmuted.</para>
</parameter>
</syntax>
<description>
</description>
</manager>
<manager name="ConfbridgeKick" language="en_US">
<synopsis>
Kick a Confbridge user.
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
<parameter name="Conference" required="true" />
<parameter name="Channel" required="true" >
<para>If this parameter is "all", all channels will be kicked from the conference.</para>
<para>If this parameter is "participants", all non-admin channels will be kicked from the conference.</para>
</parameter>
</syntax>
<description>
</description>
</manager>
<manager name="ConfbridgeLock" language="en_US">
<synopsis>
Lock a Confbridge conference.
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
<parameter name="Conference" required="true" />
</syntax>
<description>
</description>
</manager>
<manager name="ConfbridgeUnlock" language="en_US">
<synopsis>
Unlock a Confbridge conference.
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
<parameter name="Conference" required="true" />
</syntax>
<description>
</description>
</manager>
<manager name="ConfbridgeStartRecord" language="en_US">
<synopsis>
Start recording a Confbridge conference.
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
<parameter name="Conference" required="true" />
<parameter name="RecordFile" required="false" />
</syntax>
<description>
<para>Start recording a conference. If recording is already present an error will be returned. If RecordFile is not provided, the default record file specified in the conference's bridge profile will be used, if that is not present either a file will automatically be generated in the monitor directory.</para>
</description>
</manager>
<manager name="ConfbridgeStopRecord" language="en_US">
<synopsis>
Stop recording a Confbridge conference.
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
<parameter name="Conference" required="true" />
</syntax>
<description>
</description>
</manager>
<manager name="ConfbridgeSetSingleVideoSrc" language="en_US">
<synopsis>
Set a conference user as the single video source distributed to all other participants.
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
<parameter name="Conference" required="true" />
<parameter name="Channel" required="true">
<para>If this parameter is not a complete channel name, the first channel with this prefix will be used.</para>
</parameter>
</syntax>
<description>
</description>
</manager>
***/
/*!
* \par Playing back a file to a channel in a conference
* You might notice in this application that while playing a sound file
* to a channel the actual conference bridge lock is not held. This is done so
* that other channels are not blocked from interacting with the conference bridge.
* Unfortunately because of this it is possible for things to change after the sound file
* is done being played. Data must therefore be checked after reacquiring the conference
* bridge lock if it is important.
*/
static const char app[] = "ConfBridge";
/* Number of buckets our conference bridges container can have */
#define CONFERENCE_BRIDGE_BUCKETS 53
enum {
CONF_RECORD_EXIT = 0,
CONF_RECORD_START,
CONF_RECORD_STOP,
};
/*! \brief Container to hold all conference bridges in progress */
struct ao2_container *conference_bridges;
static void leave_conference(struct confbridge_user *user);
static int play_sound_number(struct confbridge_conference *conference, int say_number);
static int execute_menu_entry(struct confbridge_conference *conference,
struct confbridge_user *user,
struct ast_bridge_channel *bridge_channel,
struct conf_menu_entry *menu_entry,
struct conf_menu *menu);
/*! \brief Hashing function used for conference bridges container */
static int conference_bridge_hash_cb(const void *obj, const int flags)
{
const struct confbridge_conference *conference = obj;
const char *name = obj;
int hash;
switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
default:
case OBJ_POINTER:
name = conference->name;
/* Fall through */
case OBJ_KEY:
hash = ast_str_case_hash(name);
break;
case OBJ_PARTIAL_KEY:
/* Should never happen in hash callback. */
ast_assert(0);
hash = 0;
break;
}
return hash;
}
/*! \brief Comparison function used for conference bridges container */
static int conference_bridge_cmp_cb(void *obj, void *arg, int flags)
{
const struct confbridge_conference *left = obj;
const struct confbridge_conference *right = arg;
const char *right_name = arg;
int cmp;
switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
default:
case OBJ_POINTER:
right_name = right->name;
/* Fall through */
case OBJ_KEY:
cmp = strcasecmp(left->name, right_name);
break;
case OBJ_PARTIAL_KEY:
cmp = strncasecmp(left->name, right_name, strlen(right_name));
break;
}
return cmp ? 0 : CMP_MATCH;
}
const char *conf_get_sound(enum conf_sounds sound, struct bridge_profile_sounds *custom_sounds)
{
switch (sound) {
case CONF_SOUND_HAS_JOINED:
return S_OR(custom_sounds->hasjoin, "conf-hasjoin");
case CONF_SOUND_HAS_LEFT:
return S_OR(custom_sounds->hasleft, "conf-hasleft");
case CONF_SOUND_KICKED:
return S_OR(custom_sounds->kicked, "conf-kicked");
case CONF_SOUND_MUTED:
return S_OR(custom_sounds->muted, "conf-muted");
case CONF_SOUND_UNMUTED:
return S_OR(custom_sounds->unmuted, "conf-unmuted");
case CONF_SOUND_ONLY_ONE:
return S_OR(custom_sounds->onlyone, "conf-onlyone");
case CONF_SOUND_THERE_ARE:
return S_OR(custom_sounds->thereare, "conf-thereare");
case CONF_SOUND_OTHER_IN_PARTY:
return S_OR(custom_sounds->otherinparty, "conf-otherinparty");
case CONF_SOUND_PLACE_IN_CONF:
return S_OR(custom_sounds->placeintoconf, "conf-placeintoconf");
case CONF_SOUND_WAIT_FOR_LEADER:
return S_OR(custom_sounds->waitforleader, "conf-waitforleader");
case CONF_SOUND_LEADER_HAS_LEFT:
return S_OR(custom_sounds->leaderhasleft, "conf-leaderhasleft");
case CONF_SOUND_GET_PIN:
return S_OR(custom_sounds->getpin, "conf-getpin");
case CONF_SOUND_INVALID_PIN:
return S_OR(custom_sounds->invalidpin, "conf-invalidpin");
case CONF_SOUND_ONLY_PERSON:
return S_OR(custom_sounds->onlyperson, "conf-onlyperson");
case CONF_SOUND_LOCKED:
return S_OR(custom_sounds->locked, "conf-locked");
case CONF_SOUND_LOCKED_NOW:
return S_OR(custom_sounds->lockednow, "conf-lockednow");
case CONF_SOUND_UNLOCKED_NOW:
return S_OR(custom_sounds->unlockednow, "conf-unlockednow");
case CONF_SOUND_ERROR_MENU:
return S_OR(custom_sounds->errormenu, "conf-errormenu");
case CONF_SOUND_JOIN:
return S_OR(custom_sounds->join, "confbridge-join");
case CONF_SOUND_LEAVE:
return S_OR(custom_sounds->leave, "confbridge-leave");
case CONF_SOUND_PARTICIPANTS_MUTED:
return S_OR(custom_sounds->participantsmuted, "conf-now-muted");
case CONF_SOUND_PARTICIPANTS_UNMUTED:
return S_OR(custom_sounds->participantsunmuted, "conf-now-unmuted");
case CONF_SOUND_BEGIN:
return S_OR(custom_sounds->begin, "confbridge-conf-begin");
}
return "";
}
static void send_conf_stasis(struct confbridge_conference *conference, struct ast_channel *chan,
struct stasis_message_type *type, struct ast_json *extras, int channel_topic)
{
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
json_object = ast_json_pack("{s: s}",
"conference", conference->name);
if (!json_object) {
return;
}
if (extras) {
ast_json_object_update(json_object, extras);
}
ast_bridge_lock(conference->bridge);
msg = ast_bridge_blob_create(type,
conference->bridge,
chan,
json_object);
ast_bridge_unlock(conference->bridge);
if (!msg) {
return;
}
if (channel_topic) {
stasis_publish(ast_channel_topic(chan), msg);
} else {
stasis_publish(ast_bridge_topic(conference->bridge), msg);
}
}
static void send_conf_start_event(struct confbridge_conference *conference)
{
send_conf_stasis(conference, NULL, confbridge_start_type(), NULL, 0);
}
static void send_conf_end_event(struct confbridge_conference *conference)
{
send_conf_stasis(conference, NULL, confbridge_end_type(), NULL, 0);
}
static void send_join_event(struct confbridge_user *user, struct confbridge_conference *conference)
{
struct ast_json *json_object;
json_object = ast_json_pack("{s: b}",
"admin", ast_test_flag(&user->u_profile, USER_OPT_ADMIN)
);
if (!json_object) {
return;
}
send_conf_stasis(conference, user->chan, confbridge_join_type(), json_object, 0);
ast_json_unref(json_object);
}
static void send_leave_event(struct confbridge_user *user, struct confbridge_conference *conference)
{
struct ast_json *json_object;
json_object = ast_json_pack("{s: b}",
"admin", ast_test_flag(&user->u_profile, USER_OPT_ADMIN)
);
if (!json_object) {
return;
}
send_conf_stasis(conference, user->chan, confbridge_leave_type(), json_object, 0);
ast_json_unref(json_object);
}
static void send_start_record_event(struct confbridge_conference *conference)
{
send_conf_stasis(conference, NULL, confbridge_start_record_type(), NULL, 0);
}
static void send_stop_record_event(struct confbridge_conference *conference)
{
send_conf_stasis(conference, NULL, confbridge_stop_record_type(), NULL, 0);
}
static void send_mute_event(struct confbridge_user *user, struct confbridge_conference *conference)
{
struct ast_json *json_object;
json_object = ast_json_pack("{s: b}",
"admin", ast_test_flag(&user->u_profile, USER_OPT_ADMIN)
);
if (!json_object) {
return;
}
send_conf_stasis(conference, user->chan, confbridge_mute_type(), json_object, 1);
ast_json_unref(json_object);
}
static void send_unmute_event(struct confbridge_user *user, struct confbridge_conference *conference)
{
struct ast_json *json_object;
json_object = ast_json_pack("{s: b}",
"admin", ast_test_flag(&user->u_profile, USER_OPT_ADMIN)
);
if (!json_object) {
return;
}
send_conf_stasis(conference, user->chan, confbridge_unmute_type(), json_object, 1);
ast_json_unref(json_object);
}
static void set_rec_filename(struct confbridge_conference *conference, struct ast_str **filename, int is_new)
{
char *rec_file = conference->b_profile.rec_file;
char *ext;
time_t now;
if (ast_str_strlen(*filename)
&& ast_test_flag(&conference->b_profile, BRIDGE_OPT_RECORD_FILE_APPEND)
&& !is_new) {
return;
}
time(&now);
ast_str_reset(*filename);
if (ast_strlen_zero(rec_file)) {
ast_str_set(filename, 0, "confbridge-%s-%u.wav", conference->name,
(unsigned int) now);
} else if (ast_test_flag(&conference->b_profile, BRIDGE_OPT_RECORD_FILE_TIMESTAMP)) {
/* insert time before file extension */
ext = strrchr(rec_file, '.');
if (ext) {
ast_str_set_substr(filename, 0, rec_file, ext - rec_file);
ast_str_append(filename, 0, "-%u%s", (unsigned int) now, ext);
} else {
ast_str_set(filename, 0, "%s-%u", rec_file, (unsigned int) now);
}
} else {
ast_str_set(filename, 0, "%s", rec_file);
}
ast_str_append(filename, 0, ",%s%s,%s",
ast_test_flag(&conference->b_profile, BRIDGE_OPT_RECORD_FILE_APPEND) ? "a" : "",
conference->b_profile.rec_options,
conference->b_profile.rec_command);
}
static int is_new_rec_file(const char *rec_file, struct ast_str **orig_rec_file)
{
if (!ast_strlen_zero(rec_file)) {
if (!*orig_rec_file) {
*orig_rec_file = ast_str_create(PATH_MAX);
}
if (strcmp(ast_str_buffer(*orig_rec_file), rec_file)) {
ast_str_set(orig_rec_file, 0, "%s", rec_file);
return 1;
}
}
return 0;
}
static void *record_thread(void *obj)
{
struct confbridge_conference *conference = obj;
struct ast_app *mixmonapp = pbx_findapp("MixMonitor");
struct ast_channel *chan;
struct ast_str *filename = ast_str_alloca(PATH_MAX);
struct ast_str *orig_rec_file = NULL;
struct ast_bridge_features features;
ast_mutex_lock(&conference->record_lock);
if (!mixmonapp) {
ast_log(LOG_WARNING, "Can not record ConfBridge, MixMonitor app is not installed\n");
conference->record_thread = AST_PTHREADT_NULL;
ast_mutex_unlock(&conference->record_lock);
ao2_ref(conference, -1);
return NULL;
}
if (ast_bridge_features_init(&features)) {
ast_bridge_features_cleanup(&features);
conference->record_thread = AST_PTHREADT_NULL;
ast_mutex_unlock(&conference->record_lock);
ao2_ref(conference, -1);
return NULL;
}
ast_set_flag(&features.feature_flags, AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE);
/* XXX If we get an EXIT right here, START will essentially be a no-op */
while (conference->record_state != CONF_RECORD_EXIT) {
set_rec_filename(conference, &filename,
is_new_rec_file(conference->b_profile.rec_file, &orig_rec_file));
chan = ast_channel_ref(conference->record_chan);
ast_answer(chan);
pbx_exec(chan, mixmonapp, ast_str_buffer(filename));
ast_bridge_join(conference->bridge, chan, NULL, &features, NULL, 0);
ast_hangup(chan); /* This will eat this thread's reference to the channel as well */
/* STOP has been called. Wait for either a START or an EXIT */
ast_cond_wait(&conference->record_cond, &conference->record_lock);
}
ast_bridge_features_cleanup(&features);
ast_free(orig_rec_file);
ast_mutex_unlock(&conference->record_lock);
ao2_ref(conference, -1);
return NULL;
}
/*! \brief Returns whether or not conference is being recorded.
* \param conference The bridge to check for recording
* \retval 1, conference is recording.
* \retval 0, conference is NOT recording.
*/
static int conf_is_recording(struct confbridge_conference *conference)
{
return conference->record_state == CONF_RECORD_START;
}
/*! \brief Stop recording a conference bridge
* \internal
* \param conference The conference bridge on which to stop the recording
* \retval -1 Failure
* \retval 0 Success
*/
static int conf_stop_record(struct confbridge_conference *conference)
{
struct ast_channel *chan;
if (conference->record_thread == AST_PTHREADT_NULL || !conf_is_recording(conference)) {
return -1;
}
conference->record_state = CONF_RECORD_STOP;
chan = ast_channel_ref(conference->record_chan);
ast_bridge_remove(conference->bridge, chan);
ast_queue_frame(chan, &ast_null_frame);
chan = ast_channel_unref(chan);
ast_test_suite_event_notify("CONF_STOP_RECORD", "Message: stopped conference recording channel\r\nConference: %s", conference->b_profile.name);
send_stop_record_event(conference);
return 0;
}
/*!
* \internal
* \brief Stops the confbridge recording thread.
*
* \note Must be called with the conference locked
*/
static int conf_stop_record_thread(struct confbridge_conference *conference)
{
if (conference->record_thread == AST_PTHREADT_NULL) {
return -1;
}
conf_stop_record(conference);
ast_mutex_lock(&conference->record_lock);
conference->record_state = CONF_RECORD_EXIT;
ast_cond_signal(&conference->record_cond);
ast_mutex_unlock(&conference->record_lock);
pthread_join(conference->record_thread, NULL);
conference->record_thread = AST_PTHREADT_NULL;
/* this is the reference given to the channel during the channel alloc */
if (conference->record_chan) {
conference->record_chan = ast_channel_unref(conference->record_chan);
}
return 0;
}
/*! \brief Start recording the conference
* \internal
* \note The conference must be locked when calling this function
* \param conference The conference bridge to start recording
* \retval 0 success
* \rteval non-zero failure
*/
static int conf_start_record(struct confbridge_conference *conference)
{
struct ast_format_cap *cap;
if (conference->record_state != CONF_RECORD_STOP) {
return -1;
}
if (!pbx_findapp("MixMonitor")) {
ast_log(LOG_WARNING, "Can not record ConfBridge, MixMonitor app is not installed\n");
return -1;
}
cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
if (!cap) {
return -1;
}
ast_format_cap_append(cap, ast_format_slin, 0);
conference->record_chan = ast_request("CBRec", cap, NULL, NULL,
conference->name, NULL);
ao2_ref(cap, -1);
if (!conference->record_chan) {
return -1;
}
conference->record_state = CONF_RECORD_START;
ast_mutex_lock(&conference->record_lock);
ast_cond_signal(&conference->record_cond);
ast_mutex_unlock(&conference->record_lock);
ast_test_suite_event_notify("CONF_START_RECORD", "Message: started conference recording channel\r\nConference: %s", conference->b_profile.name);
send_start_record_event(conference);
return 0;
}
/*! \brief Start the recording thread on a conference bridge
* \internal
* \param conference The conference bridge on which to start the recording thread
* \retval 0 success
* \retval -1 failure
*/
static int start_conf_record_thread(struct confbridge_conference *conference)
{
conf_start_record(conference);
/*
* if the thread has already been started, don't start another
*/
if (conference->record_thread != AST_PTHREADT_NULL) {
return 0;
}
ao2_ref(conference, +1); /* give the record thread a ref */
if (ast_pthread_create_background(&conference->record_thread, NULL, record_thread, conference)) {
ast_log(LOG_WARNING, "Failed to create recording channel for conference %s\n", conference->name);
ao2_ref(conference, -1); /* error so remove ref */
return -1;
}
return 0;
}
/*!
* \internal
* \brief Complain if the given sound file does not exist.
*
* \param filename Sound file to check if exists.
*
* \retval non-zero if the file exists.
*/
static int sound_file_exists(const char *filename)
{
if (ast_fileexists(filename, NULL, NULL)) {
return -1;
}
ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
return 0;
}
/*!
* \brief Announce number of users in the conference bridge to the caller
*
* \param conference Conference bridge to peek at
* \param user Optional Caller
*
* \note if caller is NULL, the announcment will be sent to all participants in the conference.
* \return Returns 0 on success, -1 if the user hung up
*/
static int announce_user_count(struct confbridge_conference *conference, struct confbridge_user *user)
{
const char *other_in_party = conf_get_sound(CONF_SOUND_OTHER_IN_PARTY, conference->b_profile.sounds);
const char *only_one = conf_get_sound(CONF_SOUND_ONLY_ONE, conference->b_profile.sounds);
const char *there_are = conf_get_sound(CONF_SOUND_THERE_ARE, conference->b_profile.sounds);
if (conference->activeusers <= 1) {
/* Awww we are the only person in the conference bridge OR we only have waitmarked users */
return 0;
} else if (conference->activeusers == 2) {
if (user) {
/* Eep, there is one other person */
if (ast_stream_and_wait(user->chan,
only_one,
"")) {
return -1;
}
} else {
play_sound_file(conference, only_one);
}
} else {
/* Alas multiple others in here */
if (user) {
if (ast_stream_and_wait(user->chan,
there_are,
"")) {
return -1;
}
if (ast_say_number(user->chan, conference->activeusers - 1, "", ast_channel_language(user->chan), NULL)) {
return -1;
}
if (ast_stream_and_wait(user->chan,
other_in_party,
"")) {
return -1;
}
} else if (sound_file_exists(there_are) && sound_file_exists(other_in_party)) {
play_sound_file(conference, there_are);
play_sound_number(conference, conference->activeusers - 1);
play_sound_file(conference, other_in_party);
}
}
return 0;
}
/*!
* \brief Play back an audio file to a channel
*
* \param user User to play audio prompt to
* \param filename Prompt to play
*
* \return Returns 0 on success, -1 if the user hung up
* \note Generally this should be called when the conference is unlocked to avoid blocking
* the entire conference while the sound is played. But don't unlock the conference bridge
* in the middle of a state transition.
*/
static int play_prompt_to_user(struct confbridge_user *user, const char *filename)
{
return ast_stream_and_wait(user->chan, filename, "");
}
static void handle_video_on_join(struct confbridge_conference *conference, struct ast_channel *chan, int marked)
{
/* Right now, only marked users are automatically set as the single src of video.*/
if (!marked) {
return;
}
if (ast_test_flag(&conference->b_profile, BRIDGE_OPT_VIDEO_SRC_FIRST_MARKED)) {
int set = 1;
struct confbridge_user *user = NULL;
ao2_lock(conference);
/* see if anyone is already the video src */
AST_LIST_TRAVERSE(&conference->active_list, user, list) {
if (user->chan == chan) {
continue;
}
if (ast_bridge_is_video_src(conference->bridge, user->chan)) {
set = 0;
break;
}
}
ao2_unlock(conference);
if (set) {
ast_bridge_set_single_src_video_mode(conference->bridge, chan);
}
} else if (ast_test_flag(&conference->b_profile, BRIDGE_OPT_VIDEO_SRC_LAST_MARKED)) {
/* we joined and are video capable, we override anyone else that may have already been the video feed */
ast_bridge_set_single_src_video_mode(conference->bridge, chan);
}
}
static void handle_video_on_exit(struct confbridge_conference *conference, struct ast_channel *chan)
{
struct confbridge_user *user = NULL;
/* if this isn't a video source, nothing to update */
if (!ast_bridge_is_video_src(conference->bridge, chan)) {
return;
}
ast_bridge_remove_video_src(conference->bridge, chan);
/* If in follow talker mode, make sure to restore this mode on the
* bridge when a source is removed. It is possible this channel was
* only set temporarily as a video source by an AMI or DTMF action. */
if (ast_test_flag(&conference->b_profile, BRIDGE_OPT_VIDEO_SRC_FOLLOW_TALKER)) {
ast_bridge_set_talker_src_video_mode(conference->bridge);
}
/* if the video_mode isn't set to automatically pick the video source, do nothing on exit. */
if (!ast_test_flag(&conference->b_profile, BRIDGE_OPT_VIDEO_SRC_FIRST_MARKED) &&
!ast_test_flag(&conference->b_profile, BRIDGE_OPT_VIDEO_SRC_LAST_MARKED)) {
return;
}
/* Make the next available marked user the video src. */
ao2_lock(conference);
AST_LIST_TRAVERSE(&conference->active_list, user, list) {
if (user->chan == chan) {
continue;
}
if (ast_test_flag(&user->u_profile, USER_OPT_MARKEDUSER)) {
ast_bridge_set_single_src_video_mode(conference->bridge, user->chan);
break;
}
}
ao2_unlock(conference);
}
/*!
* \brief Destroy a conference bridge
*
* \param obj The conference bridge object
*
* \return Returns nothing
*/
static void destroy_conference_bridge(void *obj)
{
struct confbridge_conference *conference = obj;
ast_debug(1, "Destroying conference bridge '%s'\n", conference->name);
if (conference->playback_chan) {
conf_announce_channel_depart(conference->playback_chan);
ast_hangup(conference->playback_chan);
conference->playback_chan = NULL;
}
/* Destroying a conference bridge is simple, all we have to do is destroy the bridging object */
if (conference->bridge) {
ast_bridge_destroy(conference->bridge, 0);
conference->bridge = NULL;
}
conf_bridge_profile_destroy(&conference->b_profile);
ast_cond_destroy(&conference->record_cond);
ast_mutex_destroy(&conference->record_lock);
ast_mutex_destroy(&conference->playback_lock);
}
/*! \brief Call the proper join event handler for the user for the conference bridge's current state
* \internal
* \param user The conference bridge user that is joining
* \retval 0 success
* \retval -1 failure
*/
static int handle_conf_user_join(struct confbridge_user *user)
{
conference_event_fn handler;
if (ast_test_flag(&user->u_profile, USER_OPT_MARKEDUSER)) {
handler = user->conference->state->join_marked;
} else if (ast_test_flag(&user->u_profile, USER_OPT_WAITMARKED)) {
handler = user->conference->state->join_waitmarked;
} else {
handler = user->conference->state->join_unmarked;
}
ast_assert(handler != NULL);
if (!handler) {
conf_invalid_event_fn(user);
return -1;
}
handler(user);