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