446cc097f183345202d5b859a997bd4323b4d1a5
[asterisk/asterisk.git] / apps / app_confbridge.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007-2008, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Conference Bridge application
22  *
23  * \author\verbatim Joshua Colp <jcolp@digium.com> \endverbatim
24  *
25  * This is a conference bridge application utilizing the bridging core.
26  * \ingroup applications
27  */
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <signal.h>
38
39 #include "asterisk/file.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/module.h"
44 #include "asterisk/lock.h"
45 #include "asterisk/app.h"
46 #include "asterisk/bridging.h"
47 #include "asterisk/musiconhold.h"
48 #include "asterisk/say.h"
49 #include "asterisk/audiohook.h"
50 #include "asterisk/astobj2.h"
51
52 /*** DOCUMENTATION
53         <application name="ConfBridge" language="en_US">
54                 <synopsis>
55                         Conference bridge application.
56                 </synopsis>
57                 <syntax>
58                         <parameter name="confno">
59                                 <para>The conference number</para>
60                         </parameter>
61                         <parameter name="options">
62                                 <optionlist>
63                                         <option name="a">
64                                                 <para>Set admin mode.</para>
65                                         </option>
66                                         <option name="A">
67                                                 <para>Set marked mode.</para>
68                                         </option>
69                                         <option name="c">
70                                                 <para>Announce user(s) count on joining a conference.</para>
71                                         </option>
72                                         <option name="m">
73                                                 <para>Set initially muted.</para>
74                                         </option>
75                                         <option name="M" hasparams="optional">
76                                                 <para>Enable music on hold when the conference has a single caller. Optionally,
77                                                 specify a musiconhold class to use. If one is not provided, it will use the
78                                                 channel's currently set music class, or <literal>default</literal>.</para>
79                                                 <argument name="class" required="true" />
80                                         </option>
81                                         <option name="1">
82                                                 <para>Do not play message when first person enters</para>
83                                         </option>
84                                         <option name="s">
85                                                 <para>Present menu (user or admin) when <literal>*</literal> is received
86                                                 (send to menu).</para>
87                                         </option>
88                                         <option name="w">
89                                                 <para>Wait until the marked user enters the conference.</para>
90                                         </option>
91                                         <option name="q">
92                                                 <para>Quiet mode (don't play enter/leave sounds).</para>
93                                         </option>
94                                 </optionlist>
95                       </parameter>
96                 </syntax>
97                 <description>
98                         <para>Enters the user into a specified conference bridge. The user can exit the conference by hangup only.</para>
99                         <para>The join sound can be set using the <literal>CONFBRIDGE_JOIN_SOUND</literal> variable and the leave sound can be set using the <literal>CONFBRIDGE_LEAVE_SOUND</literal> variable. These can be unique to the caller.</para>
100                 </description>
101         </application>
102 ***/
103
104 /*!
105  * \par Playing back a file to a channel in a conference
106  * You might notice in this application that while playing a sound file
107  * to a channel the actual conference bridge lock is not held. This is done so
108  * that other channels are not blocked from interacting with the conference bridge.
109  * Unfortunately because of this it is possible for things to change after the sound file
110  * is done being played. Data must therefore be checked after reacquiring the conference
111  * bridge lock if it is important.
112  */
113
114 static const char app[] = "ConfBridge";
115
116 enum {
117         OPTION_ADMIN = (1 << 0),             /*!< Set if the caller is an administrator */
118         OPTION_MENU = (1 << 1),              /*!< Set if the caller should have access to the conference bridge IVR menu */
119         OPTION_MUSICONHOLD = (1 << 2),       /*!< Set if music on hold should be played if nobody else is in the conference bridge */
120         OPTION_NOONLYPERSON = (1 << 3),      /*!< Set if the "you are currently the only person in this conference" sound file should not be played */
121         OPTION_STARTMUTED = (1 << 4),        /*!< Set if the caller should be initially set muted */
122         OPTION_ANNOUNCEUSERCOUNT = (1 << 5), /*!< Set if the number of users should be announced to the caller */
123         OPTION_MARKEDUSER = (1 << 6),        /*!< Set if the caller is a marked user */
124         OPTION_WAITMARKED = (1 << 7),        /*!< Set if the conference must wait for a marked user before starting */
125         OPTION_QUIET = (1 << 8),             /*!< Set if no audio prompts should be played */
126 };
127
128 enum {
129         OPTION_MUSICONHOLD_CLASS,            /*!< If the 'M' option is set, the music on hold class to play */
130         /*This must be the last element */
131         OPTION_ARRAY_SIZE,
132 };
133
134 AST_APP_OPTIONS(app_opts,{
135         AST_APP_OPTION('A', OPTION_MARKEDUSER),
136         AST_APP_OPTION('a', OPTION_ADMIN),
137         AST_APP_OPTION('c', OPTION_ANNOUNCEUSERCOUNT),
138         AST_APP_OPTION('m', OPTION_MENU),
139         AST_APP_OPTION_ARG('M', OPTION_MUSICONHOLD, OPTION_MUSICONHOLD_CLASS),
140         AST_APP_OPTION('1', OPTION_NOONLYPERSON),
141         AST_APP_OPTION('s', OPTION_STARTMUTED),
142         AST_APP_OPTION('w', OPTION_WAITMARKED),
143         AST_APP_OPTION('q', OPTION_QUIET),
144 });
145
146 /* Maximum length of a conference bridge name */
147 #define MAX_CONF_NAME 32
148
149 /* Number of buckets our conference bridges container can have */
150 #define CONFERENCE_BRIDGE_BUCKETS 53
151
152 /*! \brief The structure that represents a conference bridge */
153 struct conference_bridge {
154         char name[MAX_CONF_NAME];                                         /*!< Name of the conference bridge */
155         struct ast_bridge *bridge;                                        /*!< Bridge structure doing the mixing */
156         unsigned int users;                                               /*!< Number of users present */
157         unsigned int markedusers;                                         /*!< Number of marked users present */
158         unsigned int locked:1;                                            /*!< Is this conference bridge locked? */
159         AST_LIST_HEAD_NOLOCK(, conference_bridge_user) users_list;        /*!< List of users participating in the conference bridge */
160         struct ast_channel *playback_chan;                                /*!< Channel used for playback into the conference bridge */
161         ast_mutex_t playback_lock;                                        /*!< Lock used for playback channel */
162 };
163
164 /*! \brief The structure that represents a conference bridge user */
165 struct conference_bridge_user {
166         struct conference_bridge *conference_bridge; /*!< Conference bridge they are participating in */
167         struct ast_channel *chan;                    /*!< Asterisk channel participating */
168         struct ast_flags flags;                      /*!< Flags passed in when the application was called */
169         char *opt_args[OPTION_ARRAY_SIZE];           /*!< Arguments to options passed when application was called */
170         struct ast_bridge_features features;         /*!< Bridge features structure */
171         unsigned int kicked:1;                       /*!< User has been kicked from the conference */
172         AST_LIST_ENTRY(conference_bridge_user) list; /*!< Linked list information */
173 };
174
175 /*! \brief Container to hold all conference bridges in progress */
176 static struct ao2_container *conference_bridges;
177
178 static int play_sound_file(struct conference_bridge *conference_bridge, const char *filename);
179
180 /*! \brief Hashing function used for conference bridges container */
181 static int conference_bridge_hash_cb(const void *obj, const int flags)
182 {
183         const struct conference_bridge *conference_bridge = obj;
184         return ast_str_case_hash(conference_bridge->name);
185 }
186
187 /*! \brief Comparison function used for conference bridges container */
188 static int conference_bridge_cmp_cb(void *obj, void *arg, int flags)
189 {
190         const struct conference_bridge *conference_bridge0 = obj, *conference_bridge1 = arg;
191         return (!strcasecmp(conference_bridge0->name, conference_bridge1->name) ? CMP_MATCH | CMP_STOP : 0);
192 }
193
194 /*!
195  * \brief Announce number of users in the conference bridge to the caller
196  *
197  * \param conference_bridge Conference bridge to peek at
198  * \param conference_bridge_user Caller
199  *
200  * \return Returns nothing
201  */
202 static void announce_user_count(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
203 {
204         if (conference_bridge->users == 1) {
205                 /* Awww we are the only person in the conference bridge */
206                 return;
207         } else if (conference_bridge->users == 2) {
208                 /* Eep, there is one other person */
209                 if (ast_stream_and_wait(conference_bridge_user->chan, "conf-onlyone", "")) {
210                         return;
211                 }
212         } else {
213                 /* Alas multiple others in here */
214                 if (ast_stream_and_wait(conference_bridge_user->chan, "conf-thereare", "")) {
215                         return;
216                 }
217                 if (ast_say_number(conference_bridge_user->chan, conference_bridge->users - 1, "", conference_bridge_user->chan->language, NULL)) {
218                         return;
219                 }
220                 if (ast_stream_and_wait(conference_bridge_user->chan, "conf-otherinparty", "")) {
221                         return;
222                 }
223         }
224 }
225
226 /*!
227  * \brief Play back an audio file to a channel
228  *
229  * \param conference_bridge Conference bridge they are in
230  * \param chan Channel to play audio prompt to
231  * \param file Prompt to play
232  *
233  * \return Returns nothing
234  *
235  * \note This function assumes that conference_bridge is locked
236  */
237 static void play_prompt_to_channel(struct conference_bridge *conference_bridge, struct ast_channel *chan, const char *file)
238 {
239         ao2_unlock(conference_bridge);
240         ast_stream_and_wait(chan, file, "");
241         ao2_lock(conference_bridge);
242 }
243
244 /*!
245  * \brief Perform post-joining marked specific actions
246  *
247  * \param conference_bridge Conference bridge being joined
248  * \param conference_bridge_user Conference bridge user joining
249  *
250  * \return Returns nothing
251  */
252 static void post_join_marked(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
253 {
254         if (ast_test_flag(&conference_bridge_user->flags, OPTION_MARKEDUSER)) {
255                 struct conference_bridge_user *other_conference_bridge_user = NULL;
256
257                 /* If we are not the first marked user to join just bail out now */
258                 if (conference_bridge->markedusers >= 2) {
259                         return;
260                 }
261
262                 /* Iterate through every participant stopping MOH on them if need be */
263                 AST_LIST_TRAVERSE(&conference_bridge->users_list, other_conference_bridge_user, list) {
264                         if (other_conference_bridge_user == conference_bridge_user) {
265                                 continue;
266                         }
267                         if (ast_test_flag(&other_conference_bridge_user->flags, OPTION_MUSICONHOLD) && !ast_bridge_suspend(conference_bridge->bridge, other_conference_bridge_user->chan)) {
268                                 ast_moh_stop(other_conference_bridge_user->chan);
269                                 ast_bridge_unsuspend(conference_bridge->bridge, other_conference_bridge_user->chan);
270                         }
271                 }
272
273                 /* Next play the audio file stating they are going to be placed into the conference */
274                 if (!ast_test_flag(&conference_bridge_user->flags, OPTION_QUIET)) {
275                         ao2_unlock(conference_bridge);
276                         ast_autoservice_start(conference_bridge_user->chan);
277                         play_sound_file(conference_bridge, "conf-placeintoconf");
278                         ast_autoservice_stop(conference_bridge_user->chan);
279                         ao2_lock(conference_bridge);
280                 }
281
282                 /* Finally iterate through and unmute them all */
283                 AST_LIST_TRAVERSE(&conference_bridge->users_list, other_conference_bridge_user, list) {
284                         if (other_conference_bridge_user == conference_bridge_user) {
285                                 continue;
286                         }
287                         other_conference_bridge_user->features.mute = 0;
288                 }
289
290         } else {
291                 /* If a marked user already exists in the conference bridge we can just bail out now */
292                 if (conference_bridge->markedusers) {
293                         return;
294                 }
295                 /* Be sure we are muted so we can't talk to anybody else waiting */
296                 conference_bridge_user->features.mute = 1;
297                 /* If we have not been quieted play back that they are waiting for the leader */
298                 if (!ast_test_flag(&conference_bridge_user->flags, OPTION_QUIET)) {
299                         play_prompt_to_channel(conference_bridge, conference_bridge_user->chan, "conf-waitforleader");
300                 }
301                 /* Start music on hold if needed */
302                 /* We need to recheck the markedusers value here. play_prompt_to_channel unlocks the conference bridge, potentially
303                  * allowing a marked user to enter while the prompt was playing
304                  */
305                 if (!conference_bridge->markedusers && ast_test_flag(&conference_bridge_user->flags, OPTION_MUSICONHOLD)) {
306                         ast_moh_start(conference_bridge_user->chan, conference_bridge_user->opt_args[OPTION_MUSICONHOLD_CLASS], NULL);
307                 }
308         }
309 }
310
311 /*!
312  * \brief Perform post-joining non-marked specific actions
313  *
314  * \param conference_bridge Conference bridge being joined
315  * \param conference_bridge_user Conference bridge user joining
316  *
317  * \return Returns nothing
318  */
319 static void post_join_unmarked(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
320 {
321         /* Play back audio prompt and start MOH if need be if we are the first participant */
322         if (conference_bridge->users == 1) {
323                 /* If audio prompts have not been quieted or this prompt quieted play it on out */
324                 if (!ast_test_flag(&conference_bridge_user->flags, OPTION_QUIET | OPTION_NOONLYPERSON)) {
325                         play_prompt_to_channel(conference_bridge, conference_bridge_user->chan, "conf-onlyperson");
326                 }
327                 /* If we need to start music on hold on the channel do so now */
328                 /* We need to re-check the number of users in the conference bridge here because another conference bridge
329                  * participant could have joined while the above prompt was playing for the first user.
330                  */
331                 if (conference_bridge->users == 1 && ast_test_flag(&conference_bridge_user->flags, OPTION_MUSICONHOLD)) {
332                         ast_moh_start(conference_bridge_user->chan, conference_bridge_user->opt_args[OPTION_MUSICONHOLD_CLASS], NULL);
333                 }
334                 return;
335         }
336
337         /* Announce number of users if need be */
338         if (ast_test_flag(&conference_bridge_user->flags, OPTION_ANNOUNCEUSERCOUNT)) {
339                 ao2_unlock(conference_bridge);
340                 announce_user_count(conference_bridge, conference_bridge_user);
341                 ao2_lock(conference_bridge);
342         }
343
344         /* If we are the second participant we may need to stop music on hold on the first */
345         if (conference_bridge->users == 2) {
346                 struct conference_bridge_user *first_participant = AST_LIST_FIRST(&conference_bridge->users_list);
347
348                 /* Temporarily suspend the above participant from the bridge so we have control to stop MOH if needed */
349                 if (ast_test_flag(&first_participant->flags, OPTION_MUSICONHOLD) && !ast_bridge_suspend(conference_bridge->bridge, first_participant->chan)) {
350                         ast_moh_stop(first_participant->chan);
351                         ast_bridge_unsuspend(conference_bridge->bridge, first_participant->chan);
352                 }
353         }
354 }
355
356 /*!
357  * \brief Destroy a conference bridge
358  *
359  * \param obj The conference bridge object
360  *
361  * \return Returns nothing
362  */
363 static void destroy_conference_bridge(void *obj)
364 {
365         struct conference_bridge *conference_bridge = obj;
366
367         ast_debug(1, "Destroying conference bridge '%s'\n", conference_bridge->name);
368
369         ast_mutex_destroy(&conference_bridge->playback_lock);
370
371         if (conference_bridge->playback_chan) {
372                 struct ast_channel *underlying_channel = conference_bridge->playback_chan->tech->bridged_channel(conference_bridge->playback_chan, NULL);
373                 ast_hangup(underlying_channel);
374                 ast_hangup(conference_bridge->playback_chan);
375                 conference_bridge->playback_chan = NULL;
376         }
377
378         /* Destroying a conference bridge is simple, all we have to do is destroy the bridging object */
379         if (conference_bridge->bridge) {
380                 ast_bridge_destroy(conference_bridge->bridge);
381                 conference_bridge->bridge = NULL;
382         }
383 }
384
385 /*!
386  * \brief Join a conference bridge
387  *
388  * \param name The conference name
389  * \param conference_bridge_user Conference bridge user structure
390  *
391  * \return A pointer to the conference bridge struct, or NULL if the conference room wasn't found.
392  */
393 static struct conference_bridge *join_conference_bridge(const char *name, struct conference_bridge_user *conference_bridge_user)
394 {
395         struct conference_bridge *conference_bridge = NULL;
396         struct conference_bridge tmp;
397
398         ast_copy_string(tmp.name, name, sizeof(tmp.name));
399
400         /* We explictly lock the conference bridges container ourselves so that other callers can not create duplicate conferences at the same */
401         ao2_lock(conference_bridges);
402
403         ast_debug(1, "Trying to find conference bridge '%s'\n", name);
404
405         /* Attempt to find an existing conference bridge */
406         conference_bridge = ao2_find(conference_bridges, &tmp, OBJ_POINTER);
407
408         /* When finding a conference bridge that already exists make sure that it is not locked, and if so that we are not an admin */
409         if (conference_bridge && conference_bridge->locked && !ast_test_flag(&conference_bridge_user->flags, OPTION_ADMIN)) {
410                 ao2_unlock(conference_bridges);
411                 ao2_ref(conference_bridge, -1);
412                 ast_debug(1, "Conference bridge '%s' is locked and caller is not an admin\n", name);
413                 ast_stream_and_wait(conference_bridge_user->chan, "conf-locked", "");
414                 return NULL;
415         }
416
417         /* If no conference bridge was found see if we can create one */
418         if (!conference_bridge) {
419                 /* Try to allocate memory for a new conference bridge, if we fail... this won't end well. */
420                 if (!(conference_bridge = ao2_alloc(sizeof(*conference_bridge), destroy_conference_bridge))) {
421                         ao2_unlock(conference_bridges);
422                         ast_log(LOG_ERROR, "Conference bridge '%s' does not exist.\n", name);
423                         return NULL;
424                 }
425
426                 /* Setup conference bridge parameters */
427                 ast_copy_string(conference_bridge->name, name, sizeof(conference_bridge->name));
428
429                 /* Create an actual bridge that will do the audio mixing */
430                 if (!(conference_bridge->bridge = ast_bridge_new(AST_BRIDGE_CAPABILITY_1TO1MIX, AST_BRIDGE_FLAG_SMART))) {
431                         ao2_ref(conference_bridge, -1);
432                         conference_bridge = NULL;
433                         ao2_unlock(conference_bridges);
434                         ast_log(LOG_ERROR, "Conference bridge '%s' could not be created.\n", name);
435                         return NULL;
436                 }
437
438                 /* Setup lock for playback channel */
439                 ast_mutex_init(&conference_bridge->playback_lock);
440
441                 /* Link it into the conference bridges container */
442                 ao2_link(conference_bridges, conference_bridge);
443
444                 ast_debug(1, "Created conference bridge '%s' and linked to container '%p'\n", name, conference_bridges);
445         }
446
447         ao2_unlock(conference_bridges);
448
449         /* Setup conference bridge user parameters */
450         conference_bridge_user->conference_bridge = conference_bridge;
451
452         ao2_lock(conference_bridge);
453
454         /* All good to go, add them in */
455         AST_LIST_INSERT_TAIL(&conference_bridge->users_list, conference_bridge_user, list);
456
457         /* Increment the users count on the bridge, but record it as it is going to need to be known right after this */
458         conference_bridge->users++;
459
460         /* If the caller is a marked user bump up the count */
461         if (ast_test_flag(&conference_bridge_user->flags, OPTION_MARKEDUSER)) {
462                 conference_bridge->markedusers++;
463         }
464
465         /* If the caller is a marked user or is waiting for a marked user to enter pass 'em off, otherwise pass them off to do regular joining stuff */
466         if (ast_test_flag(&conference_bridge_user->flags, OPTION_MARKEDUSER | OPTION_WAITMARKED)) {
467                 post_join_marked(conference_bridge, conference_bridge_user);
468         } else {
469                 post_join_unmarked(conference_bridge, conference_bridge_user);
470         }
471
472         ao2_unlock(conference_bridge);
473
474         return conference_bridge;
475 }
476
477 /*!
478  * \brief Leave a conference bridge
479  *
480  * \param conference_bridge The conference bridge to leave
481  * \param conference_bridge_user The conference bridge user structure
482  *
483  */
484 static void  leave_conference_bridge(struct conference_bridge *conference_bridge, struct conference_bridge_user *conference_bridge_user)
485 {
486         ao2_lock(conference_bridge);
487
488         /* If this caller is a marked user bump down the count */
489         if (ast_test_flag(&conference_bridge_user->flags, OPTION_MARKEDUSER)) {
490                 conference_bridge->markedusers--;
491         }
492
493         /* Decrement the users count while keeping the previous participant count */
494         conference_bridge->users--;
495
496         /* Drop conference bridge user from the list, they be going bye bye */
497         AST_LIST_REMOVE(&conference_bridge->users_list, conference_bridge_user, list);
498
499         /* If there are still users in the conference bridge we may need to do things (such as start MOH on them) */
500         if (conference_bridge->users) {
501                 if (ast_test_flag(&conference_bridge_user->flags, OPTION_MARKEDUSER) && !conference_bridge->markedusers) {
502                         struct conference_bridge_user *other_participant = NULL;
503
504                         /* Start out with muting everyone */
505                         AST_LIST_TRAVERSE(&conference_bridge->users_list, other_participant, list) {
506                                 other_participant->features.mute = 1;
507                         }
508
509                         /* Play back the audio prompt saying the leader has left the conference */
510                         if (!ast_test_flag(&conference_bridge_user->flags, OPTION_QUIET)) {
511                                 ao2_unlock(conference_bridge);
512                                 ast_autoservice_start(conference_bridge_user->chan);
513                                 play_sound_file(conference_bridge, "conf-leaderhasleft");
514                                 ast_autoservice_stop(conference_bridge_user->chan);
515                                 ao2_lock(conference_bridge);
516                         }
517
518                         /* Now on to starting MOH if needed */
519                         AST_LIST_TRAVERSE(&conference_bridge->users_list, other_participant, list) {
520                                 if (ast_test_flag(&other_participant->flags, OPTION_MUSICONHOLD) && !ast_bridge_suspend(conference_bridge->bridge, other_participant->chan)) {
521                                         ast_moh_start(other_participant->chan, other_participant->opt_args[OPTION_MUSICONHOLD_CLASS], NULL);
522                                         ast_bridge_unsuspend(conference_bridge->bridge, other_participant->chan);
523                                 }
524                         }
525                 } else if (conference_bridge->users == 1) {
526                         /* Of course if there is one other person in here we may need to start up MOH on them */
527                         struct conference_bridge_user *first_participant = AST_LIST_FIRST(&conference_bridge->users_list);
528
529                         if (ast_test_flag(&first_participant->flags, OPTION_MUSICONHOLD) && !ast_bridge_suspend(conference_bridge->bridge, first_participant->chan)) {
530                                 ast_moh_start(first_participant->chan, first_participant->opt_args[OPTION_MUSICONHOLD_CLASS], NULL);
531                                 ast_bridge_unsuspend(conference_bridge->bridge, first_participant->chan);
532                         }
533                 }
534         } else {
535                 ao2_unlink(conference_bridges, conference_bridge);
536         }
537
538         /* Done mucking with the conference bridge, huzzah */
539         ao2_unlock(conference_bridge);
540
541         ao2_ref(conference_bridge, -1);
542 }
543
544 /*!
545  * \brief Play sound file into conference bridge
546  *
547  * \param conference_bridge The conference bridge to play sound file into
548  * \param filename Sound file to play
549  *
550  * \retval 0 success
551  * \retval -1 failure
552  */
553 static int play_sound_file(struct conference_bridge *conference_bridge, const char *filename)
554 {
555         struct ast_channel *underlying_channel;
556
557         ast_mutex_lock(&conference_bridge->playback_lock);
558
559         if (!(conference_bridge->playback_chan)) {
560                 int cause;
561
562                 if (!(conference_bridge->playback_chan = ast_request("Bridge", AST_FORMAT_SLINEAR, NULL, "", &cause))) {
563                         ast_mutex_unlock(&conference_bridge->playback_lock);
564                         return -1;
565                 }
566
567                 conference_bridge->playback_chan->bridge = conference_bridge->bridge;
568
569                 if (ast_call(conference_bridge->playback_chan, "", 0)) {
570                         ast_hangup(conference_bridge->playback_chan);
571                         conference_bridge->playback_chan = NULL;
572                         ast_mutex_unlock(&conference_bridge->playback_lock);
573                         return -1;
574                 }
575
576                 ast_debug(1, "Created a playback channel to conference bridge '%s'\n", conference_bridge->name);
577
578                 underlying_channel = conference_bridge->playback_chan->tech->bridged_channel(conference_bridge->playback_chan, NULL);
579         } else {
580                 /* Channel was already available so we just need to add it back into the bridge */
581                 underlying_channel = conference_bridge->playback_chan->tech->bridged_channel(conference_bridge->playback_chan, NULL);
582                 ast_bridge_impart(conference_bridge->bridge, underlying_channel, NULL, NULL);
583         }
584
585         /* The channel is all under our control, in goes the prompt */
586         ast_stream_and_wait(conference_bridge->playback_chan, filename, "");
587
588         ast_debug(1, "Departing underlying channel '%s' from bridge '%p'\n", underlying_channel->name, conference_bridge->bridge);
589         ast_bridge_depart(conference_bridge->bridge, underlying_channel);
590
591         ast_mutex_unlock(&conference_bridge->playback_lock);
592
593         return 0;
594 }
595
596 /*!
597  * \brief DTMF Menu Callback
598  *
599  * \param bridge Bridge this is involving
600  * \param bridge_channel Bridged channel this is involving
601  * \param hook_pvt User's conference bridge structure
602  *
603  * \retval 0 success
604  * \retval -1 failure
605  */
606 static int menu_callback(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
607 {
608         struct conference_bridge_user *conference_bridge_user = hook_pvt;
609         struct conference_bridge *conference_bridge = conference_bridge_user->conference_bridge;
610         int digit, res = 0, isadmin = ast_test_flag(&conference_bridge_user->flags, OPTION_ADMIN);
611
612         /* See if music on hold is playing */
613         ao2_lock(conference_bridge);
614         if (conference_bridge->users == 1 && ast_test_flag(&conference_bridge_user->flags, OPTION_MUSICONHOLD)) {
615                 /* Just us so MOH is probably indeed going, let's stop it */
616                 ast_moh_stop(bridge_channel->chan);
617         }
618         ao2_unlock(conference_bridge);
619
620         /* Try to play back the user menu, if it fails pass this back up so the bridging core will act on it */
621         if (ast_streamfile(bridge_channel->chan, (isadmin ? "conf-adminmenu" : "conf-usermenu"), bridge_channel->chan->language)) {
622                 res = -1;
623                 goto finished;
624         }
625
626         /* Wait for them to enter a digit from the user menu options */
627         digit = ast_waitstream(bridge_channel->chan, AST_DIGIT_ANY);
628         ast_stopstream(bridge_channel->chan);
629
630         if (digit == '1') {
631                 /* 1 - Mute or unmute yourself, note we only allow manipulation if they aren't waiting for a marked user or if marked users exist */
632                 if (!ast_test_flag(&conference_bridge_user->flags, OPTION_WAITMARKED) || conference_bridge->markedusers) {
633                         conference_bridge_user->features.mute = (!conference_bridge_user->features.mute ? 1 : 0);
634                 }
635                 res = ast_stream_and_wait(bridge_channel->chan, (conference_bridge_user->features.mute ? "conf-muted" : "conf-unmuted"), "");
636         } else if (isadmin && digit == '2') {
637                 /* 2 - Unlock or lock conference */
638                 conference_bridge->locked = (!conference_bridge->locked ? 1 : 0);
639                 res = ast_stream_and_wait(bridge_channel->chan, (conference_bridge->locked ? "conf-lockednow" : "conf-unlockednow"), "");
640         } else if (isadmin && digit == '3') {
641                 /* 3 - Eject last user */
642                 struct conference_bridge_user *last_participant = NULL;
643
644                 ao2_lock(conference_bridge);
645                 if (((last_participant = AST_LIST_LAST(&conference_bridge->users_list)) == conference_bridge_user) || (ast_test_flag(&last_participant->flags, OPTION_ADMIN))) {
646                         ao2_unlock(conference_bridge);
647                         res = ast_stream_and_wait(bridge_channel->chan, "conf-errormenu", "");
648                 } else {
649                         last_participant->kicked = 1;
650                         ast_bridge_remove(conference_bridge->bridge, last_participant->chan);
651                         ao2_unlock(conference_bridge);
652                 }
653         } else if (digit == '4') {
654                 /* 4 - Decrease listening volume */
655                 ast_audiohook_volume_adjust(conference_bridge_user->chan, AST_AUDIOHOOK_DIRECTION_WRITE, -1);
656         } else if (digit == '6') {
657                 /* 6 - Increase listening volume */
658                 ast_audiohook_volume_adjust(conference_bridge_user->chan, AST_AUDIOHOOK_DIRECTION_WRITE, 1);
659         } else if (digit == '7') {
660                 /* 7 - Decrease talking volume */
661                 ast_audiohook_volume_adjust(conference_bridge_user->chan, AST_AUDIOHOOK_DIRECTION_READ, -1);
662         } else if (digit == '8') {
663                 /* 8 - Exit the IVR */
664         } else if (digit == '9') {
665                 /* 9 - Increase talking volume */
666                 ast_audiohook_volume_adjust(conference_bridge_user->chan, AST_AUDIOHOOK_DIRECTION_READ, 1);
667         } else {
668                 /* No valid option was selected */
669                 res = ast_stream_and_wait(bridge_channel->chan, "conf-errormenu", "");
670         }
671
672  finished:
673         /* See if music on hold needs to be started back up again */
674         ao2_lock(conference_bridge);
675         if (conference_bridge->users == 1 && ast_test_flag(&conference_bridge_user->flags, OPTION_MUSICONHOLD)) {
676                 ast_moh_start(bridge_channel->chan, conference_bridge_user->opt_args[OPTION_MUSICONHOLD_CLASS], NULL);
677         }
678         ao2_unlock(conference_bridge);
679
680         bridge_channel->state = AST_BRIDGE_CHANNEL_STATE_WAIT;
681
682         return res;
683 }
684
685 /*! \brief The ConfBridge application */
686 static int confbridge_exec(struct ast_channel *chan, const char *data)
687 {
688         int res = 0, volume_adjustments[2];
689         char *parse;
690         struct conference_bridge *conference_bridge = NULL;
691         struct conference_bridge_user conference_bridge_user = {
692                 .chan = chan,
693         };
694         const char *tmp, *join_sound = NULL, *leave_sound = NULL;
695         AST_DECLARE_APP_ARGS(args,
696                 AST_APP_ARG(conf_name);
697                 AST_APP_ARG(options);
698         );
699
700         if (ast_strlen_zero(data)) {
701                 ast_log(LOG_WARNING, "%s requires an argument (conference name[,options])\n", app);
702                 return -1;
703         }
704
705         /* We need to make a copy of the input string if we are going to modify it! */
706         parse = ast_strdupa(data);
707
708         AST_STANDARD_APP_ARGS(args, parse);
709
710         if (args.argc == 2) {
711                 ast_app_parse_options(app_opts, &conference_bridge_user.flags, conference_bridge_user.opt_args, args.options);
712         }
713
714         /* Look for a conference bridge matching the provided name */
715         if (!(conference_bridge = join_conference_bridge(args.conf_name, &conference_bridge_user))) {
716                 return -1;
717         }
718
719         /* Keep a copy of volume adjustments so we can restore them later if need be */
720         volume_adjustments[0] = ast_audiohook_volume_get(chan, AST_AUDIOHOOK_DIRECTION_READ);
721         volume_adjustments[1] = ast_audiohook_volume_get(chan, AST_AUDIOHOOK_DIRECTION_WRITE);
722
723         /* Always initialize the features structure, we are in most cases always going to need it. */
724         ast_bridge_features_init(&conference_bridge_user.features);
725
726         /* If the menu option is enabled provide a user or admin menu as a custom feature hook */
727         if (ast_test_flag(&conference_bridge_user.flags, OPTION_MENU)) {
728                 ast_bridge_features_hook(&conference_bridge_user.features, "#", menu_callback, &conference_bridge_user);
729         }
730
731         /* If the caller should be joined already muted, make it so */
732         if (ast_test_flag(&conference_bridge_user.flags, OPTION_STARTMUTED)) {
733                 conference_bridge_user.features.mute = 1;
734         }
735
736         /* Grab join/leave sounds from the channel */
737         ast_channel_lock(chan);
738         if ((tmp = pbx_builtin_getvar_helper(chan, "CONFBRIDGE_JOIN_SOUND"))) {
739                 join_sound = ast_strdupa(tmp);
740         }
741         if ((tmp = pbx_builtin_getvar_helper(chan, "CONFBRIDGE_LEAVE_SOUND"))) {
742                 leave_sound = ast_strdupa(tmp);
743         }
744         ast_channel_unlock(chan);
745
746         /* If there is 1 or more people already in the conference then play our join sound unless overridden */
747         if (!ast_test_flag(&conference_bridge_user.flags, OPTION_QUIET) && !ast_strlen_zero(join_sound) && conference_bridge->users >= 2) {
748                 ast_autoservice_start(chan);
749                 play_sound_file(conference_bridge, join_sound);
750                 ast_autoservice_stop(chan);
751         }
752
753         /* Join our conference bridge for real */
754         ast_bridge_join(conference_bridge->bridge, chan, NULL, &conference_bridge_user.features);
755
756         /* If there is 1 or more people (not including us) already in the conference then play our leave sound unless overridden */
757         if (!ast_test_flag(&conference_bridge_user.flags, OPTION_QUIET) && !ast_strlen_zero(leave_sound) && conference_bridge->users >= 2) {
758                 ast_autoservice_start(chan);
759                 play_sound_file(conference_bridge, leave_sound);
760                 ast_autoservice_stop(chan);
761         }
762
763         /* Easy as pie, depart this channel from the conference bridge */
764         leave_conference_bridge(conference_bridge, &conference_bridge_user);
765         conference_bridge = NULL;
766
767         /* Can't forget to clean up the features structure, or else we risk a memory leak */
768         ast_bridge_features_cleanup(&conference_bridge_user.features);
769
770         /* If the user was kicked from the conference play back the audio prompt for it */
771         if (!ast_test_flag(&conference_bridge_user.flags, OPTION_QUIET) && conference_bridge_user.kicked) {
772                 res = ast_stream_and_wait(chan, "conf-kicked", "");
773         }
774
775         /* Restore volume adjustments to previous values in case they were changed */
776         if (volume_adjustments[0]) {
777                 ast_audiohook_volume_set(chan, AST_AUDIOHOOK_DIRECTION_READ, volume_adjustments[0]);
778         }
779         if (volume_adjustments[1]) {
780                 ast_audiohook_volume_set(chan, AST_AUDIOHOOK_DIRECTION_WRITE, volume_adjustments[1]);
781         }
782
783         return res;
784 }
785
786 /*! \brief Called when module is being unloaded */
787 static int unload_module(void)
788 {
789         int res = ast_unregister_application(app);
790
791         /* Get rid of the conference bridges container. Since we only allow dynamic ones none will be active. */
792         ao2_ref(conference_bridges, -1);
793
794         return res;
795 }
796
797 /*! \brief Called when module is being loaded */
798 static int load_module(void)
799 {
800         /* Create a container to hold the conference bridges */
801         if (!(conference_bridges = ao2_container_alloc(CONFERENCE_BRIDGE_BUCKETS, conference_bridge_hash_cb, conference_bridge_cmp_cb))) {
802                 return AST_MODULE_LOAD_DECLINE;
803         }
804
805         if (ast_register_application_xml(app, confbridge_exec)) {
806                 ao2_ref(conference_bridges, -1);
807                 return AST_MODULE_LOAD_DECLINE;
808         }
809
810         return AST_MODULE_LOAD_SUCCESS;
811 }
812
813 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Conference Bridge Application");