apps/app_confbridge: Ensure 'normal' users hear message when last marked leaves
[asterisk/asterisk.git] / apps / confbridge / conf_state_multi_marked.c
index 2739be5..5d977f7 100644 (file)
@@ -38,6 +38,7 @@
 #include "include/confbridge.h"
 #include "asterisk/musiconhold.h"
 #include "include/conf_state.h"
+#include "asterisk/pbx.h"
 
 static void join_active(struct confbridge_user *user);
 static void join_marked(struct confbridge_user *user);
@@ -60,11 +61,13 @@ struct confbridge_state *CONF_STATE_MULTI_MARKED = &STATE_MULTI_MARKED;
 static void join_active(struct confbridge_user *user)
 {
        conf_add_user_active(user->conference, user);
+       conf_update_user_mute(user);
 }
 
 static void join_marked(struct confbridge_user *user)
 {
        conf_add_user_marked(user->conference, user);
+       conf_update_user_mute(user);
 }
 
 static void leave_active(struct confbridge_user *user)
@@ -78,43 +81,37 @@ static void leave_active(struct confbridge_user *user)
 static void leave_marked(struct confbridge_user *user)
 {
        struct confbridge_user *user_iter;
+       int need_prompt = 0;
 
        conf_remove_user_marked(user->conference, user);
 
        if (user->conference->markedusers == 0) {
-               /* Play back the audio prompt saying the leader has left the conference */
-               if (!ast_test_flag(&user->u_profile, USER_OPT_QUIET)) {
-                       ao2_unlock(user->conference);
-                       ast_autoservice_start(user->chan);
-                       play_sound_file(user->conference,
-                               conf_get_sound(CONF_SOUND_LEADER_HAS_LEFT, user->b_profile.sounds));
-                       ast_autoservice_stop(user->chan);
-                       ao2_lock(user->conference);
-               }
-
                AST_LIST_TRAVERSE_SAFE_BEGIN(&user->conference->active_list, user_iter, list) {
-                       /* Kick ENDMARKED user_iters */
-                       if (ast_test_flag(&user_iter->u_profile, USER_OPT_ENDMARKED)) {
-                               if (ast_test_flag(&user_iter->u_profile, USER_OPT_WAITMARKED) &&
-                                                 !ast_test_flag(&user_iter->u_profile, USER_OPT_MARKEDUSER)) {
+                       /* Kick ENDMARKED cbu_iters */
+                       if (ast_test_flag(&user_iter->u_profile, USER_OPT_ENDMARKED) && !user_iter->kicked) {
+                               if (ast_test_flag(&user_iter->u_profile, USER_OPT_WAITMARKED)
+                                       && !ast_test_flag(&user_iter->u_profile, USER_OPT_MARKEDUSER)) {
                                        AST_LIST_REMOVE_CURRENT(list);
                                        user_iter->conference->activeusers--;
                                        AST_LIST_INSERT_TAIL(&user_iter->conference->waiting_list, user_iter, list);
                                        user_iter->conference->waitingusers++;
                                }
                                user_iter->kicked = 1;
+                               pbx_builtin_setvar_helper(user_iter->chan, "CONFBRIDGE_RESULT", "ENDMARKED");
                                ast_bridge_remove(user_iter->conference->bridge, user_iter->chan);
-                       } else if (ast_test_flag(&user_iter->u_profile, USER_OPT_WAITMARKED) &&
-                                       !ast_test_flag(&user_iter->u_profile, USER_OPT_MARKEDUSER)) {
+                       } else if (ast_test_flag(&user_iter->u_profile, USER_OPT_WAITMARKED)
+                               && !ast_test_flag(&user_iter->u_profile, USER_OPT_MARKEDUSER)) {
+                               need_prompt = 1;
+
                                AST_LIST_REMOVE_CURRENT(list);
                                user_iter->conference->activeusers--;
                                AST_LIST_INSERT_TAIL(&user_iter->conference->waiting_list, user_iter, list);
                                user_iter->conference->waitingusers++;
-                               /* Handle muting/moh of user_iter if necessary */
-                               if (ast_test_flag(&user_iter->u_profile, USER_OPT_MUSICONHOLD)) {
-                                       user_iter->features.mute = 1;
-                                       conf_moh_start(user_iter);
-                               }
+                       } else {
+                               /* User is neither wait_marked nor end_marked; however, they
+                                * should still hear the prompt.
+                                */
+                               need_prompt = 1;
                        }
                }
                AST_LIST_TRAVERSE_SAFE_END;
@@ -159,18 +156,49 @@ static void leave_marked(struct confbridge_user *user)
                        break; /* Stay in marked */
                }
        }
+
+       if (need_prompt) {
+               /* Play back the audio prompt saying the leader has left the conference */
+               if (!ast_test_flag(&user->u_profile, USER_OPT_QUIET)) {
+                       ao2_unlock(user->conference);
+                       ast_autoservice_start(user->chan);
+                       play_sound_file(user->conference,
+                               conf_get_sound(CONF_SOUND_LEADER_HAS_LEFT, user->b_profile.sounds));
+                       ast_autoservice_stop(user->chan);
+                       ao2_lock(user->conference);
+               }
+
+               AST_LIST_TRAVERSE(&user->conference->waiting_list, user_iter, list) {
+                       if (user_iter->kicked) {
+                               continue;
+                       }
+
+                       if (ast_test_flag(&user_iter->u_profile, USER_OPT_MUSICONHOLD)) {
+                               conf_moh_start(user_iter);
+                       }
+
+                       conf_update_user_mute(user_iter);
+               }
+       }
+}
+
+static int post_join_play_begin(struct confbridge_user *cbu)
+{
+       int res;
+
+       ast_autoservice_start(cbu->chan);
+       res = play_sound_file(cbu->conference,
+               conf_get_sound(CONF_SOUND_BEGIN, cbu->b_profile.sounds));
+       ast_autoservice_stop(cbu->chan);
+       return res;
 }
 
 static void transition_to_marked(struct confbridge_user *user)
 {
        struct confbridge_user *user_iter;
+       int waitmarked_moved = 0;
 
-       /* Play the audio file stating they are going to be placed into the conference */
-       if (user->conference->markedusers == 1 && ast_test_flag(&user->u_profile, USER_OPT_MARKEDUSER)) {
-               conf_handle_first_marked_common(user);
-       }
-
-       /* Move all waiting users to active, stopping MOH and umuting if necessary */
+       /* Move all waiting users to active, stopping MOH and unmuting if necessary */
        AST_LIST_TRAVERSE_SAFE_BEGIN(&user->conference->waiting_list, user_iter, list) {
                AST_LIST_REMOVE_CURRENT(list);
                user->conference->waitingusers--;
@@ -179,10 +207,16 @@ static void transition_to_marked(struct confbridge_user *user)
                if (user_iter->playing_moh) {
                        conf_moh_stop(user_iter);
                }
-               /* only unmute them if they are not supposed to start muted */
-               if (!ast_test_flag(&user_iter->u_profile, USER_OPT_STARTMUTED)) {
-                       user_iter->features.mute = 0;
-               }
+               conf_update_user_mute(user_iter);
+               waitmarked_moved++;
        }
        AST_LIST_TRAVERSE_SAFE_END;
+
+       /* Play the audio file stating that the conference is beginning */
+       if (user->conference->markedusers == 1
+               && ast_test_flag(&user->u_profile, USER_OPT_MARKEDUSER)
+               && !ast_test_flag(&user->u_profile, USER_OPT_QUIET)
+               && waitmarked_moved) {
+               conf_add_post_join_action(user, post_join_play_begin);
+       }
 }