settings/agent/earcons/
bluetooth_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
// 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 crate::agent::earcons::agent::CommonEarconsParams;
use crate::agent::earcons::sound_ids::{
    BLUETOOTH_CONNECTED_SOUND_ID, BLUETOOTH_DISCONNECTED_SOUND_ID,
};
use crate::agent::earcons::utils::{connect_to_sound_player, play_sound};
use crate::audio::types::{AudioSettingSource, AudioStreamType, SetAudioStream};
use crate::base::{SettingInfo, SettingType};
use crate::event::Publisher;
use crate::handler::base::{Payload, Request};
use crate::message::base::Audience;
use crate::{call, service, trace};

use anyhow::{format_err, Context, Error};
use fidl::endpoints::create_request_stream;
use fidl_fuchsia_media_sessions2::{
    DiscoveryMarker, SessionsWatcherRequest, SessionsWatcherRequestStream, WatchOptions,
};
use futures::stream::TryStreamExt;
use std::collections::HashSet;
use {fuchsia_async as fasync, fuchsia_trace as ftrace};

/// Type for uniquely identifying bluetooth media sessions.
type SessionId = u64;

/// The file path for the earcon to be played for bluetooth connecting.
const BLUETOOTH_CONNECTED_FILE_PATH: &str = "bluetooth-connected.wav";

/// The file path for the earcon to be played for bluetooth disconnecting.
const BLUETOOTH_DISCONNECTED_FILE_PATH: &str = "bluetooth-disconnected.wav";

pub(crate) const BLUETOOTH_DOMAIN: &str = "Bluetooth";

/// The `BluetoothHandler` takes care of the earcons functionality on bluetooth connection
/// and disconnection.
#[derive(Debug)]
pub(super) struct BluetoothHandler {
    // Parameters common to all earcons handlers.
    common_earcons_params: CommonEarconsParams,
    // The publisher to use for connecting to services.
    publisher: Publisher,
    // The ids of the media sessions that are currently active.
    active_sessions: HashSet<SessionId>,
    // A messenger with which to send a requests via the message hub.
    messenger: service::message::Messenger,
}

/// The type of bluetooth earcons sound.
enum BluetoothSoundType {
    Connected,
    Disconnected,
}

impl BluetoothHandler {
    pub(super) async fn create(
        publisher: Publisher,
        params: CommonEarconsParams,
        messenger: service::message::Messenger,
    ) -> Result<(), Error> {
        let mut handler = Self {
            common_earcons_params: params,
            publisher,
            active_sessions: HashSet::<SessionId>::new(),
            messenger,
        };
        handler.watch_bluetooth_connections().await
    }

    /// Watch for media session changes. The media sessions that have the
    /// Bluetooth mode in their metadata signify a bluetooth connection.
    /// The id of a disconnected device will be received on removal.
    pub(super) async fn watch_bluetooth_connections(&mut self) -> Result<(), Error> {
        // Connect to media session Discovery service.
        let discovery_connection_result = self
            .common_earcons_params
            .service_context
            .connect_with_publisher::<DiscoveryMarker>(self.publisher.clone())
            .await
            .context("Connecting to fuchsia.media.sessions2.Discovery");

        let discovery_proxy = discovery_connection_result.map_err(|e| {
            format_err!("Failed to connect to fuchsia.media.sessions2.Discovery: {:?}", e)
        })?;

        // Create and handle the request stream of media sessions.
        let (watcher_client, watcher_requests) = create_request_stream();

        call!(discovery_proxy =>
            watch_sessions(&WatchOptions::default(), watcher_client))
        .map_err(|e| format_err!("Unable to start discovery of MediaSessions: {:?}", e))?;

        self.handle_bluetooth_connections(watcher_requests);
        Ok(())
    }

    /// Handles the stream of media session updates, and possibly plays earcons
    /// sounds based on what type of update is received.
    fn handle_bluetooth_connections(&mut self, mut watcher_requests: SessionsWatcherRequestStream) {
        let mut active_sessions_clone = self.active_sessions.clone();
        let publisher = self.publisher.clone();
        let common_earcons_params = self.common_earcons_params.clone();
        let messenger = self.messenger.clone();

        fasync::Task::local(async move {
            loop {
                let maybe_req = watcher_requests.try_next().await;
                match maybe_req {
                    Ok(Some(req)) => {
                        match req {
                            SessionsWatcherRequest::SessionUpdated {
                                session_id: id,
                                session_info_delta: delta,
                                responder,
                            } => {
                                if let Err(e) = responder.send() {
                                    tracing::error!("Failed to acknowledge delta from SessionWatcher: {:?}", e);
                                    return;
                                }

                                if active_sessions_clone.contains(&id)
                                    || !matches!(delta.domain, Some(name) if name == BLUETOOTH_DOMAIN)
                                {
                                    continue;
                                }
                                let _ = active_sessions_clone.insert(id);

                                let publisher = publisher.clone();
                                let common_earcons_params = common_earcons_params.clone();
                                let messenger = messenger.clone();
                                fasync::Task::local(async move {
                                    play_bluetooth_sound(
                                        common_earcons_params,
                                        publisher,
                                        BluetoothSoundType::Connected,
                                        messenger,
                                    )
                                    .await;
                                })
                                .detach();
                            }
                            SessionsWatcherRequest::SessionRemoved { session_id, responder } => {
                                if let Err(e) = responder.send() {
                                    tracing::error!(
                                        "Failed to acknowledge session removal from SessionWatcher: {:?}",
                                        e
                                    );
                                    return;
                                }

                                if !active_sessions_clone.contains(&session_id) {
                                    tracing::warn!(
                                        "Tried to remove nonexistent media session id {:?}",
                                        session_id
                                    );
                                    continue;
                                }
                                let _ = active_sessions_clone.remove(&session_id);
                                let publisher = publisher.clone();
                                let common_earcons_params = common_earcons_params.clone();
                                let messenger = messenger.clone();
                                fasync::Task::local(async move {
                                    play_bluetooth_sound(
                                        common_earcons_params,
                                        publisher,
                                        BluetoothSoundType::Disconnected,
                                        messenger,
                                    )
                                    .await;
                                })
                                .detach();
                            }
                        }
                    },
                    Ok(None) => {
                        tracing::warn!("stream ended on fuchsia.media.sessions2.SessionsWatcher");
                        break;
                    },
                    Err(e) => {
                        tracing::error!("failed to watch fuchsia.media.sessions2.SessionsWatcher: {:?}", &e);
                        break;
                    },
                }
            }
        })
        .detach();
    }
}

/// Play a bluetooth earcons sound.
async fn play_bluetooth_sound(
    common_earcons_params: CommonEarconsParams,
    publisher: Publisher,
    sound_type: BluetoothSoundType,
    messenger: service::message::Messenger,
) {
    // 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 = common_earcons_params.sound_player_connection.clone();
    let sound_player_connection_lock = sound_player_connection.lock().await;
    let sound_player_added_files = common_earcons_params.sound_player_added_files.clone();

    if let Some(sound_player_proxy) = sound_player_connection_lock.as_ref() {
        match_background_to_media(messenger).await;
        match sound_type {
            BluetoothSoundType::Connected => {
                if play_sound(
                    sound_player_proxy,
                    BLUETOOTH_CONNECTED_FILE_PATH,
                    BLUETOOTH_CONNECTED_SOUND_ID,
                    sound_player_added_files.clone(),
                )
                .await
                .is_err()
                {
                    tracing::error!("[bluetooth_earcons_handler] failed to play bluetooth earcon connection sound");
                }
            }
            BluetoothSoundType::Disconnected => {
                if play_sound(
                    sound_player_proxy,
                    BLUETOOTH_DISCONNECTED_FILE_PATH,
                    BLUETOOTH_DISCONNECTED_SOUND_ID,
                    sound_player_added_files.clone(),
                )
                .await
                .is_err()
                {
                    tracing::error!("[bluetooth_earcons_handler] failed to play bluetooth earcon disconnection sound");
                }
            }
        };
    } else {
        tracing::error!("[bluetooth_earcons_handler] failed to play bluetooth earcon sound: no sound player connection");
    }
}

/// Match the background volume to the current media volume before playing the bluetooth earcon.
async fn match_background_to_media(messenger: service::message::Messenger) {
    // Get the current audio info.
    let mut get_receptor = messenger.message(
        Payload::Request(Request::Get).into(),
        Audience::Address(service::Address::Handler(SettingType::Audio)),
    );

    // Extract media and background volumes.
    let mut media_volume = 0.0;
    let mut background_volume = 0.0;
    if let Ok((Payload::Response(Ok(Some(SettingInfo::Audio(info)))), _)) =
        get_receptor.next_of::<Payload>().await
    {
        info.streams.iter().for_each(|stream| {
            if stream.stream_type == AudioStreamType::Media {
                media_volume = stream.user_volume_level;
            } else if stream.stream_type == AudioStreamType::Background {
                background_volume = stream.user_volume_level;
            }
        })
    } else {
        tracing::error!("Could not extract background and media volumes")
    };

    // If they are different, set the background volume to match the media volume.
    if media_volume != background_volume {
        let id = ftrace::Id::new();
        trace!(id, c"bluetooth_handler set background volume");
        let mut receptor = messenger.message(
            Payload::Request(Request::SetVolume(
                vec![SetAudioStream {
                    stream_type: AudioStreamType::Background,
                    source: AudioSettingSource::System,
                    user_volume_level: Some(media_volume),
                    user_volume_muted: None,
                }],
                id,
            ))
            .into(),
            Audience::Address(service::Address::Handler(SettingType::Audio)),
        );

        if receptor.next_payload().await.is_err() {
            tracing::error!(
                "Failed to play bluetooth connection sound after waiting for message response"
            );
        }
    }
}