| 1 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 2 | pub enum MuteCommand { |
| 3 | Mute, |
| 4 | Unmute, |
| 5 | Toggle, |
| 6 | } |
| 7 | |
| 8 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 9 | pub struct MuteEffect { |
| 10 | pub muted: bool, |
| 11 | pub track_enabled: bool, |
| 12 | pub endpoint_is_muted: bool, |
| 13 | } |
| 14 | |
| 15 | impl MuteCommand { |
| 16 | pub fn target(self, currently_muted: bool) -> bool { |
| 17 | match self { |
| 18 | MuteCommand::Mute => true, |
| 19 | MuteCommand::Unmute => false, |
| 20 | MuteCommand::Toggle => !currently_muted, |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 26 | pub struct ServerMuteSync { |
| 27 | pub muted: bool, |
| 28 | pub cleared: bool, |
| 29 | pub silence_without_signal: bool, |
| 30 | } |
| 31 | |
| 32 | /// Applies an organizer mute locally. A local unmute while the flag is still set |
| 33 | /// stays cleared until `server_muted` drops, so the same roster update cannot mute again. |
| 34 | pub fn sync_server_mute(server_muted: bool, muted: bool, cleared: bool) -> ServerMuteSync { |
| 35 | if !server_muted { |
| 36 | return ServerMuteSync { |
| 37 | muted, |
| 38 | cleared: false, |
| 39 | silence_without_signal: false, |
| 40 | }; |
| 41 | } |
| 42 | if cleared || muted { |
| 43 | return ServerMuteSync { |
| 44 | muted, |
| 45 | cleared, |
| 46 | silence_without_signal: false, |
| 47 | }; |
| 48 | } |
| 49 | ServerMuteSync { |
| 50 | muted: true, |
| 51 | cleared: false, |
| 52 | silence_without_signal: true, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | impl MuteEffect { |
| 57 | pub fn for_muted(muted: bool) -> Self { |
| 58 | MuteEffect { |
| 59 | muted, |
| 60 | track_enabled: !muted, |
| 61 | endpoint_is_muted: muted, |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | pub fn initial(is_meeting: bool) -> Self { |
| 66 | MuteEffect::for_muted(is_meeting) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | #[cfg(test)] |
| 71 | mod tests { |
| 72 | use super::*; |
| 73 | |
| 74 | #[test] |
| 75 | fn commands_map_to_a_target_state() { |
| 76 | assert!(MuteCommand::Mute.target(false)); |
| 77 | assert!(MuteCommand::Mute.target(true)); |
| 78 | assert!(!MuteCommand::Unmute.target(true)); |
| 79 | assert!(MuteCommand::Toggle.target(false)); |
| 80 | assert!(!MuteCommand::Toggle.target(true)); |
| 81 | } |
| 82 | |
| 83 | #[test] |
| 84 | fn muting_stops_the_track_and_tells_teams() { |
| 85 | let muted = MuteEffect::for_muted(true); |
| 86 | assert!(!muted.track_enabled); |
| 87 | assert!(muted.endpoint_is_muted); |
| 88 | let open = MuteEffect::for_muted(false); |
| 89 | assert!(open.track_enabled); |
| 90 | assert!(!open.endpoint_is_muted); |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn an_organizer_mute_closes_the_mic_until_the_user_clears_it() { |
| 95 | let applied = sync_server_mute(true, false, false); |
| 96 | assert!(applied.silence_without_signal); |
| 97 | assert!(applied.muted); |
| 98 | assert!(!applied.cleared); |
| 99 | let held = sync_server_mute(true, false, true); |
| 100 | assert!(!held.silence_without_signal); |
| 101 | assert!(!held.muted); |
| 102 | assert!(held.cleared); |
| 103 | let again = sync_server_mute(false, false, true); |
| 104 | assert!(!again.cleared); |
| 105 | let fresh = sync_server_mute(true, false, again.cleared); |
| 106 | assert!(fresh.silence_without_signal); |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn meetings_start_muted_and_other_calls_do_not() { |
| 111 | let meeting = MuteEffect::initial(true); |
| 112 | assert!(meeting.muted && !meeting.track_enabled && meeting.endpoint_is_muted); |
| 113 | let direct = MuteEffect::initial(false); |
| 114 | assert!(!direct.muted && direct.track_enabled && !direct.endpoint_is_muted); |
| 115 | } |
| 116 | } |