Confbridge: empty conference not being torn down
[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
42 static void join_active(struct confbridge_user *user);
43 static void join_marked(struct confbridge_user *user);
44 static void leave_active(struct confbridge_user *user);
45 static void leave_marked(struct confbridge_user *user);
46 static void transition_to_marked(struct confbridge_user *user);
47
48 static struct confbridge_state STATE_MULTI_MARKED = {
49         .name = "MULTI_MARKED",
50         .join_unmarked = join_active,
51         .join_waitmarked = join_active,
52         .join_marked = join_marked,
53         .leave_unmarked = leave_active,
54         .leave_waitmarked = leave_active,
55         .leave_marked = leave_marked,
56         .entry = transition_to_marked,
57 };
58 struct confbridge_state *CONF_STATE_MULTI_MARKED = &STATE_MULTI_MARKED;
59
60 static void join_active(struct confbridge_user *user)
61 {
62         conf_add_user_active(user->conference, user);
63 }
64
65 static void join_marked(struct confbridge_user *user)
66 {
67         conf_add_user_marked(user->conference, user);
68 }
69
70 static void leave_active(struct confbridge_user *user)
71 {
72         conf_remove_user_active(user->conference, user);
73         if (user->conference->activeusers == 1) {
74                 conf_change_state(user, CONF_STATE_SINGLE_MARKED);
75         }
76 }
77
78 static void leave_marked(struct confbridge_user *user)
79 {
80         struct confbridge_user *user_iter;
81
82         conf_remove_user_marked(user->conference, user);
83
84         if (user->conference->markedusers == 0) {
85                 /* Play back the audio prompt saying the leader has left the conference */
86                 if (!ast_test_flag(&user->u_profile, USER_OPT_QUIET)) {
87                         ao2_unlock(user->conference);
88                         ast_autoservice_start(user->chan);
89                         play_sound_file(user->conference,
90                                 conf_get_sound(CONF_SOUND_LEADER_HAS_LEFT, user->b_profile.sounds));
91                         ast_autoservice_stop(user->chan);
92                         ao2_lock(user->conference);
93                 }
94
95                 AST_LIST_TRAVERSE_SAFE_BEGIN(&user->conference->active_list, user_iter, list) {
96                         /* Kick ENDMARKED user_iters */
97                         if (ast_test_flag(&user_iter->u_profile, USER_OPT_ENDMARKED)) {
98                                 if (ast_test_flag(&user_iter->u_profile, USER_OPT_WAITMARKED) &&
99                                                   !ast_test_flag(&user_iter->u_profile, USER_OPT_MARKEDUSER)) {
100                                         AST_LIST_REMOVE_CURRENT(list);
101                                         user_iter->conference->activeusers--;
102                                         AST_LIST_INSERT_TAIL(&user_iter->conference->waiting_list, user_iter, list);
103                                         user_iter->conference->waitingusers++;
104                                 }
105                                 user_iter->kicked = 1;
106                                 ast_bridge_remove(user_iter->conference->bridge, user_iter->chan);
107                         } else if (ast_test_flag(&user_iter->u_profile, USER_OPT_WAITMARKED) &&
108                                         !ast_test_flag(&user_iter->u_profile, USER_OPT_MARKEDUSER)) {
109                                 AST_LIST_REMOVE_CURRENT(list);
110                                 user_iter->conference->activeusers--;
111                                 AST_LIST_INSERT_TAIL(&user_iter->conference->waiting_list, user_iter, list);
112                                 user_iter->conference->waitingusers++;
113                                 /* Handle muting/moh of user_iter if necessary */
114                                 if (ast_test_flag(&user_iter->u_profile, USER_OPT_MUSICONHOLD)) {
115                                         user_iter->features.mute = 1;
116                                         conf_moh_start(user_iter);
117                                 }
118                         }
119                 }
120                 AST_LIST_TRAVERSE_SAFE_END;
121         }
122
123         switch (user->conference->activeusers) {
124         case 0:
125                 /* Implies markedusers == 0 */
126                 switch (user->conference->waitingusers) {
127                 case 0:
128                         conf_change_state(user, CONF_STATE_EMPTY);
129                         break;
130                 default:
131                         conf_change_state(user, CONF_STATE_INACTIVE);
132                         break;
133                 }
134                 break;
135         case 1:
136                 switch (user->conference->markedusers) {
137                 case 0:
138                         conf_change_state(user, CONF_STATE_SINGLE);
139                         break;
140                 case 1:
141                         /* XXX I seem to remember doing this for a reason, but right now it escapes me
142                          * how we could possibly ever have a waiting user while we have a marked user */
143                         switch (user->conference->waitingusers) {
144                         case 0:
145                                 conf_change_state(user, CONF_STATE_SINGLE_MARKED);
146                                 break;
147                         case 1:
148                                 break; /* Stay in marked */
149                         }
150                         break;
151                 }
152                 break;
153         default:
154                 switch (user->conference->markedusers) {
155                 case 0:
156                         conf_change_state(user, CONF_STATE_MULTI);
157                         break;
158                 default:
159                         break; /* Stay in marked */
160                 }
161         }
162 }
163
164 static void transition_to_marked(struct confbridge_user *user)
165 {
166         struct confbridge_user *user_iter;
167
168         /* Play the audio file stating they are going to be placed into the conference */
169         if (user->conference->markedusers == 1 && ast_test_flag(&user->u_profile, USER_OPT_MARKEDUSER)) {
170                 conf_handle_first_marked_common(user);
171         }
172
173         /* Move all waiting users to active, stopping MOH and umuting if necessary */
174         AST_LIST_TRAVERSE_SAFE_BEGIN(&user->conference->waiting_list, user_iter, list) {
175                 AST_LIST_REMOVE_CURRENT(list);
176                 user->conference->waitingusers--;
177                 AST_LIST_INSERT_TAIL(&user->conference->active_list, user_iter, list);
178                 user->conference->activeusers++;
179                 if (user_iter->playing_moh) {
180                         conf_moh_stop(user_iter);
181                 }
182                 /* only unmute them if they are not supposed to start muted */
183                 if (!ast_test_flag(&user_iter->u_profile, USER_OPT_STARTMUTED)) {
184                         user_iter->features.mute = 0;
185                 }
186         }
187         AST_LIST_TRAVERSE_SAFE_END;
188 }