settings/agent/earcons/
volume_change_handler.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::collections::{HashMap, HashSet};

use crate::trace;
use anyhow::Error;
use futures::StreamExt;
use {fuchsia_async as fasync, fuchsia_trace as ftrace};

use crate::agent::earcons::agent::CommonEarconsParams;
use crate::agent::earcons::sound_ids::{VOLUME_CHANGED_SOUND_ID, VOLUME_MAX_SOUND_ID};
use crate::agent::earcons::utils::{connect_to_sound_player, play_sound};
use crate::audio::types::{
    AudioInfo, AudioSettingSource, AudioStream, AudioStreamType, SetAudioStream,
};
use crate::audio::{create_default_modified_counters, ModifiedCounters};
use crate::base::{SettingInfo, SettingType};
use crate::handler::base::{Payload, Request};
use crate::message::base::Audience;
use crate::message::receptor::extract_payload;
use crate::{event, service};

/// The `VolumeChangeHandler` takes care of the earcons functionality on volume change.
pub(super) struct VolumeChangeHandler {
    common_earcons_params: CommonEarconsParams,
    last_user_volumes: HashMap<AudioStreamType, f32>,
    modified_counters: ModifiedCounters,
    publisher: event::Publisher,
    messenger: service::message::Messenger,
}

/// The maximum volume level.
const MAX_VOLUME: f32 = 1.0;

/// The file path for the earcon to be played for max sound level.
const VOLUME_MAX_FILE_PATH: &str = "volume-max.wav";

/// The file path for the earcon to be played for volume changes below max volume level.
const VOLUME_CHANGED_FILE_PATH: &str = "volume-changed.wav";

impl VolumeChangeHandler {
    pub(super) async fn create(
        publisher: event::Publisher,
        params: CommonEarconsParams,
        messenger: service::message::Messenger,
    ) -> Result<(), Error> {
        let mut receptor = messenger.message(
            Payload::Request(Request::Get).into(),
            Audience::Address(service::Address::Handler(SettingType::Audio)),
        );

        // Get initial user media volume level.
        let last_user_volumes =
            if let Ok((Payload::Response(Ok(Some(SettingInfo::Audio(info)))), _)) =
                receptor.next_of::<Payload>().await
            {
                // Create map from stream type to user volume levels for each stream.
                info.streams
                    .iter()
                    .filter(|x| {
                        x.stream_type == AudioStreamType::Media
                            || x.stream_type == AudioStreamType::Interruption
                    })
                    .map(|stream| (stream.stream_type, stream.user_volume_level))
                    .collect()
            } else {
                // Could not extract info from response, default to empty volumes.
                HashMap::new()
            };

        fasync::Task::local(async move {
            let mut handler = Self {
                common_earcons_params: params,
                last_user_volumes,
                modified_counters: create_default_modified_counters(),
                publisher,
                messenger: messenger.clone(),
            };

            let listen_receptor = messenger
                .message(
                    Payload::Request(Request::Listen).into(),
                    Audience::Address(service::Address::Handler(SettingType::Audio)),
                )
                .fuse();
            futures::pin_mut!(listen_receptor);

            loop {
                futures::select! {
                    volume_change_event = listen_receptor.next() => {
                        if let Some(
                            service::Payload::Setting(Payload::Response(Ok(Some(
                                SettingInfo::Audio(audio_info)))))
                        ) = extract_payload(volume_change_event) {
                            handler.on_audio_info(audio_info).await;
                        }
                    }
                    complete => break,
                }
            }
        })
        .detach();

        Ok(())
    }

    /// Calculates and returns the streams that were changed based on
    /// their timestamps, updating them in the stored timestamps if
    /// they were changed.
    fn calculate_changed_streams(
        &mut self,
        all_streams: [AudioStream; 5],
        new_modified_counters: ModifiedCounters,
    ) -> Vec<AudioStream> {
        let mut changed_stream_types = HashSet::new();
        for (stream_type, timestamp) in new_modified_counters {
            if self.modified_counters.get(&stream_type) != Some(&timestamp) {
                let _ = changed_stream_types.insert(stream_type);
                let _ = self.modified_counters.insert(stream_type, timestamp);
            }
        }

        IntoIterator::into_iter(all_streams)
            .filter(|stream| changed_stream_types.contains(&stream.stream_type))
            .collect()
    }

    /// Retrieve a user volume of the specified `stream_type` from the given `changed_streams`.
    fn get_user_volume(
        &self,
        changed_streams: Vec<AudioStream>,
        stream_type: AudioStreamType,
    ) -> Option<f32> {
        changed_streams.iter().find(|&&x| x.stream_type == stream_type).map(|x| x.user_volume_level)
    }

    /// Retrieve the change source of the specified `stream_type` from the given `changed_streams`.
    fn get_change_source(
        &self,
        changed_streams: Vec<AudioStream>,
        stream_type: AudioStreamType,
    ) -> Option<AudioSettingSource> {
        changed_streams.iter().find(|&&x| x.stream_type == stream_type).map(|x| x.source)
    }

    /// Helper for on_audio_info. Handles the changes for a specific AudioStreamType.
    /// Enables separate handling of earcons on different streams.
    async fn on_audio_info_for_stream(
        &mut self,
        new_user_volume: f32,
        stream_type: AudioStreamType,
        change_source: Option<AudioSettingSource>,
    ) {
        let volume_is_max = new_user_volume == MAX_VOLUME;
        let last_user_volume = self.last_user_volumes.get(&stream_type);

        // Logging for debugging volume changes.
        tracing::debug!(
            "[earcons_agent] New {:?} user volume: {:?}, Last {:?} user volume: {:?}",
            stream_type,
            new_user_volume,
            stream_type,
            last_user_volume,
        );

        if last_user_volume != Some(&new_user_volume) || volume_is_max {
            // On restore, the last media user volume is set for the first time, and registers
            // as different from the last seen volume, because it is initially None. Don't play
            // the earcons sound on that set.
            //
            // If the change_source is System, do not play a sound since the user doesn't need
            // to hear feedback for such changes. For system sounds that need to play earcons,
            // the source should be AudioSettingSource::SystemWithFeedback. An example
            // of a system change is when the volume is reset after night mode deactivates.
            if last_user_volume.is_some() && change_source != Some(AudioSettingSource::System) {
                let id = ftrace::Id::new();
                trace!(id, c"volume_change_handler set background");
                let mut receptor = self.messenger.message(
                    Payload::Request(Request::SetVolume(
                        vec![SetAudioStream {
                            stream_type: AudioStreamType::Background,
                            source: AudioSettingSource::System,
                            user_volume_level: Some(new_user_volume),
                            user_volume_muted: None,
                        }],
                        id,
                    ))
                    .into(),
                    Audience::Address(service::Address::Handler(SettingType::Audio)),
                );
                if let Err(e) = receptor.next_payload().await {
                    tracing::error!(
                        "Failed to play sound after waiting for message response: {e:?}"
                    );
                } else {
                    self.play_volume_sound(new_user_volume);
                }
            }

            let _ = self.last_user_volumes.insert(stream_type, new_user_volume);
        }
    }

    /// Invoked when a new `AudioInfo` is retrieved. Determines whether an
    /// earcon should be played and plays sound if necessary.
    async fn on_audio_info(&mut self, audio_info: AudioInfo) {
        let changed_streams = match audio_info.modified_counters {
            None => Vec::new(),
            Some(counters) => self.calculate_changed_streams(audio_info.streams, counters),
        };

        let media_user_volume =
            self.get_user_volume(changed_streams.clone(), AudioStreamType::Media);
        let interruption_user_volume =
            self.get_user_volume(changed_streams.clone(), AudioStreamType::Interruption);
        let media_change_source =
            self.get_change_source(changed_streams.clone(), AudioStreamType::Media);

        if let Some(media_user_volume) = media_user_volume {
            self.on_audio_info_for_stream(
                media_user_volume,
                AudioStreamType::Media,
                media_change_source,
            )
            .await;
        }
        if let Some(interruption_user_volume) = interruption_user_volume {
            self.on_audio_info_for_stream(
                interruption_user_volume,
                AudioStreamType::Interruption,
                None,
            )
            .await;
        }
    }

    /// Play the earcons sound given the changed volume streams.
    fn play_volume_sound(&self, volume: f32) {
        let common_earcons_params = self.common_earcons_params.clone();

        let publisher = self.publisher.clone();
        fasync::Task::local(async move {
            // Connect to the SoundPlayer if not already connected.
            connect_to_sound_player(
                publisher,
                common_earcons_params.service_context.clone(),
                common_earcons_params.sound_player_connection.clone(),
            )
            .await;

            let sound_player_connection_clone =
                common_earcons_params.sound_player_connection.clone();
            let sound_player_connection = sound_player_connection_clone.lock().await;
            let sound_player_added_files = common_earcons_params.sound_player_added_files;

            if let (Some(sound_player_proxy), volume_level) =
                (sound_player_connection.as_ref(), volume)
            {
                let play_sound_result = if volume_level >= 1.0 {
                    play_sound(
                        sound_player_proxy,
                        VOLUME_MAX_FILE_PATH,
                        VOLUME_MAX_SOUND_ID,
                        sound_player_added_files.clone(),
                    )
                    .await
                } else if volume_level > 0.0 {
                    play_sound(
                        sound_player_proxy,
                        VOLUME_CHANGED_FILE_PATH,
                        VOLUME_CHANGED_SOUND_ID,
                        sound_player_added_files.clone(),
                    )
                    .await
                } else {
                    Ok(())
                };
                if let Err(e) = play_sound_result {
                    tracing::warn!("Failed to play sound: {:?}", e);
                }
            }
        })
        .detach();
    }
}

#[cfg(test)]
mod tests {
    use fuchsia_inspect::component;
    use futures::lock::Mutex;
    use std::rc::Rc;

    use crate::audio::build_audio_default_settings;
    use crate::inspect::config_logger::InspectConfigLogger;
    use crate::message::base::MessengerType;
    use crate::service_context::ServiceContext;

    use super::*;

    fn fake_values() -> (
        [AudioStream; 5], // fake_streams
        ModifiedCounters, // old_counters
        ModifiedCounters, // new_counters
        Vec<AudioStream>, // expected_changed_streams
    ) {
        let config_logger =
            Rc::new(std::sync::Mutex::new(InspectConfigLogger::new(component::inspector().root())));
        let mut settings = build_audio_default_settings(config_logger);
        let settings = settings
            .load_default_value()
            .expect("config data should exist and be parseable for tests")
            .unwrap();
        let fake_streams = settings.streams;
        let old_timestamps = create_default_modified_counters();
        let new_timestamps = [
            (AudioStreamType::Background, 0),
            (AudioStreamType::Media, 1),
            (AudioStreamType::Interruption, 0),
            (AudioStreamType::SystemAgent, 2),
            (AudioStreamType::Communication, 3),
        ]
        .iter()
        .cloned()
        .collect();
        let expected_changed_streams = [fake_streams[1], fake_streams[3], fake_streams[4]].to_vec();
        (fake_streams, old_timestamps, new_timestamps, expected_changed_streams)
    }

    #[fuchsia::test(allow_stalls = false)]
    async fn test_changed_streams() {
        let (fake_streams, old_timestamps, new_timestamps, expected_changed_streams) =
            fake_values();
        let delegate = service::MessageHub::create_hub();
        let (messenger, _) = delegate.create(MessengerType::Unbound).await.expect("messenger");
        let publisher = event::Publisher::create(&delegate, MessengerType::Unbound).await;
        let last_user_volumes: HashMap<_, _> =
            [(AudioStreamType::Media, 1.0), (AudioStreamType::Interruption, 0.5)].into();

        let mut handler = VolumeChangeHandler {
            common_earcons_params: CommonEarconsParams {
                service_context: Rc::new(ServiceContext::new(None, None)),
                sound_player_added_files: Rc::new(Mutex::new(HashSet::new())),
                sound_player_connection: Rc::new(Mutex::new(None)),
            },
            last_user_volumes,
            modified_counters: old_timestamps,
            publisher,
            messenger,
        };
        let changed_streams = handler.calculate_changed_streams(fake_streams, new_timestamps);
        assert_eq!(changed_streams, expected_changed_streams);
    }
}