app_confbridge: Only use b_profile options from the conference.
[asterisk/asterisk.git] / apps / confbridge / conf_state_multi_marked.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012, Terry Wilson
5  *
6  * Terry Wilson <twilson@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  * Please follow coding guidelines
19  * http://svn.digium.com/view/asterisk/trunk/doc/CODING-GUIDELINES
20  */
21
22 /*! \file
23  *
24  * \brief Confbridge state handling for the MULTI_MARKED state
25  *
26  * \author\verbatim Terry Wilson <twilson@digium.com> \endverbatim
27  *
28  * \ingroup applications
29  */
30
31 /*** MODULEINFO
32         <support_level>core</support_level>
33  ***/
34
35 #include "asterisk.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/linkedlists.h"
38 #include "include/confbridge.h"
39 #include "asterisk/musiconhold.h"
40 #include "include/conf_state.h"
41 #include "asterisk/pbx.h"
42
43 static void join_active(struct confbridge_user *user);
44 static void join_marked(struct confbridge_user *user);
45 static void leave_active(struct confbridge_user *user);
46 static void leave_marked(struct confbridge_user *user);
47 static void transition_to_marked(struct confbridge_user *user);
48
49 static struct confbridge_state STATE_MULTI_MARKED = {
50         .name = "MULTI_MARKED",
51         .join_unmarked = join_active,
52         .join_waitmarked = join_active,
53         .join_marked = join_marked,
54         .leave_unmarked = leave_active,
55         .leave_waitmarked = leave_active,
56         .leave_marked = leave_marked,
57         .entry = transition_to_marked,
58 };
59 struct confbridge_state *CONF_STATE_MULTI_MARKED = &STATE_MULTI_MARKED;
60
61 static void join_active(struct confbridge_user *user)
62 {
63         conf_add_user_active(user->conference, user);
64         conf_update_user_mute(user);
65 }
66
67 static void join_marked(struct confbridge_user *user)
68 {
69         conf_add_user_marked(user->conference, user);
70         conf_update_user_mute(user);
71 }
72
73 static void leave_active(struct confbridge_user *user)
74 {
75         conf_remove_user_active(user->conference, user);
76         if (user->conference->activeusers == 1) {
77                 conf_change_state(user, CONF_STATE_SINGLE_MARKED);
78         }
79 }
80
81 static void leave_marked(struct confbridge_user *user)
82 {
83         struct confbridge_user *user_iter;
84         int need_prompt = 0;
85
86         conf_remove_user_marked(user->conference, user);
87
88         if (user->conference->markedusers == 0) {
89                 AST_LIST_TRAVERSE_SAFE_BEGIN(&user->conference->active_list, user_iter, list) {
90                         /* Kick ENDMARKED cbu_iters */
91                         if (ast_test_flag(&user_iter->u_profile, USER_OPT_ENDMARKED) && !user_iter->kicked) {
92                                 if (ast_test_flag(&user_iter->u_profile, USER_OPT_WAITMARKED)
93                                         && !ast_test_flag(&user_iter->u_profile, USER_OPT_MARKEDUSER)) {
94                                         AST_LIST_REMOVE_CURRENT(list);
95                                         user_iter->conference->activeusers--;
96                                         AST_LIST_INSERT_TAIL(&user_iter->conference->waiting_list, user_iter, list);
97                                         user_iter->conference->waitingusers++;
98                                 }
99                                 user_iter->kicked = 1;
100                                 pbx_builtin_setvar_helper(user_iter->chan, "CONFBRIDGE_RESULT", "ENDMARKED");
101                                 ast_bridge_remove(user_iter->conference->bridge, user_iter->chan);
102                         } else if (ast_test_flag(&user_iter->u_profile, USER_OPT_WAITMARKED)
103                                 && !ast_test_flag(&user_iter->u_profile, USER_OPT_MARKEDUSER)) {
104                                 need_prompt = 1;
105
106                                 AST_LIST_REMOVE_CURRENT(list);
107                                 user_iter->conference->activeusers--;
108                                 AST_LIST_INSERT_TAIL(&user_iter->conference->waiting_list, user_iter, list);
109                                 user_iter->conference->waitingusers++;
110                         } else {
111                                 /* User is neither wait_marked nor end_marked; however, they
112                                  * should still hear the prompt.
113                                  */
114                                 need_prompt = 1;
115                         }
116                 }
117                 AST_LIST_TRAVERSE_SAFE_END;
118         }
119
120         switch (user->conference->activeusers) {
121         case 0:
122                 /* Implies markedusers == 0 */
123                 switch (user->conference->waitingusers) {
124                 case 0:
125                         conf_change_state(user, CONF_STATE_EMPTY);
126                         break;
127                 default:
128                         conf_change_state(user, CONF_STATE_INACTIVE);
129                         break;
130                 }
131                 break;
132         case 1:
133                 switch (user->conference->markedusers) {
134                 case 0:
135                         conf_change_state(user, CONF_STATE_SINGLE);
136                         break;
137                 case 1:
138                         /* XXX I seem to remember doing this for a reason, but right now it escapes me
139                          * how we could possibly ever have a waiting user while we have a marked user */
140                         switch (user->conference->waitingusers) {
141                         case 0:
142                                 conf_change_state(user, CONF_STATE_SINGLE_MARKED);
143                                 break;
144                         case 1:
145                                 break; /* Stay in marked */
146                         }
147                         break;
148                 }
149                 break;
150         default:
151                 switch (user->conference->markedusers) {
152                 case 0:
153                         conf_change_state(user, CONF_STATE_MULTI);
154                         break;
155                 default:
156                         break; /* Stay in marked */
157                 }
158         }
159
160         if (need_prompt) {
161                 /* Play back the audio prompt saying the leader has left the conference */
162                 if (!ast_test_flag(&user->u_profile, USER_OPT_QUIET)) {
163                         ao2_unlock(user->conference);
164                         ast_autoservice_start(user->chan);
165                         play_sound_file(user->conference,
166                                 conf_get_sound(CONF_SOUND_LEADER_HAS_LEFT, user->conference->b_profile.sounds));
167                         ast_autoservice_stop(user->chan);
168                         ao2_lock(user->conference);
169                 }
170
171                 AST_LIST_TRAVERSE(&user->conference->waiting_list, user_iter, list) {
172                         if (user_iter->kicked) {
173                                 continue;
174                         }
175
176                         if (ast_test_flag(&user_iter->u_profile, USER_OPT_MUSICONHOLD)) {
177                                 conf_moh_start(user_iter);
178                         }
179
180                         conf_update_user_mute(user_iter);
181                 }
182         }
183 }
184
185 static int post_join_play_begin(struct confbridge_user *user)
186 {
187         int res;
188
189         ast_autoservice_start(user->chan);
190         res = play_sound_file(user->conference,
191                 conf_get_sound(CONF_SOUND_BEGIN, user->conference->b_profile.sounds));
192         ast_autoservice_stop(user->chan);
193         return res;
194 }
195
196 static void transition_to_marked(struct confbridge_user *user)
197 {
198         struct confbridge_user *user_iter;
199         int waitmarked_moved = 0;
200
201         /* Move all waiting users to active, stopping MOH and unmuting if necessary */
202         AST_LIST_TRAVERSE_SAFE_BEGIN(&user->conference->waiting_list, user_iter, list) {
203                 AST_LIST_REMOVE_CURRENT(list);
204                 user->conference->waitingusers--;
205                 AST_LIST_INSERT_TAIL(&user->conference->active_list, user_iter, list);
206                 user->conference->activeusers++;
207                 if (user_iter->playing_moh) {
208                         conf_moh_stop(user_iter);
209                 }
210                 conf_update_user_mute(user_iter);
211                 waitmarked_moved++;
212         }
213         AST_LIST_TRAVERSE_SAFE_END;
214
215         /* Play the audio file stating that the conference is beginning */
216         if (user->conference->markedusers == 1
217                 && ast_test_flag(&user->u_profile, USER_OPT_MARKEDUSER)
218                 && !ast_test_flag(&user->u_profile, USER_OPT_QUIET)
219                 && waitmarked_moved) {
220                 conf_add_post_join_action(user, post_join_play_begin);
221         }
222 }