settings/agent/earcons/
utils.rs1use anyhow::{Context as _, Error, anyhow};
5use fidl::endpoints::Proxy as _;
6use fidl_fuchsia_media::AudioRenderUsage2;
7use fidl_fuchsia_media_sounds::{PlayerMarker, PlayerProxy};
8use futures::lock::Mutex;
9use settings_common::call_async;
10use settings_common::inspect::event::ExternalEventPublisher;
11use settings_common::service_context::{ExternalServiceProxy, ServiceContext};
12use std::collections::HashSet;
13use std::rc::Rc;
14use {fidl_fuchsia_io as fio, fuchsia_async as fasync};
15
16fn resource_file(name: &str) -> Result<fidl::endpoints::ClientEnd<fio::FileMarker>, Error> {
18 let path = format!("/config/data/{name}");
19 fuchsia_fs::file::open_in_namespace(&path, fio::PERM_READABLE)
20 .with_context(|| format!("opening resource file: {path}"))?
21 .into_client_end()
22 .map_err(|_: fio::FileProxy| {
23 anyhow!("failed to convert new Proxy to ClientEnd for resource file {path}")
24 })
25}
26
27pub(super) async fn connect_to_sound_player(
30 external_publisher: ExternalEventPublisher,
31 service_context_handle: Rc<ServiceContext>,
32 sound_player_connection: Rc<
33 Mutex<Option<ExternalServiceProxy<PlayerProxy, ExternalEventPublisher>>>,
34 >,
35) {
36 let mut sound_player_connection_lock = sound_player_connection.lock().await;
37 if sound_player_connection_lock.is_none() {
38 *sound_player_connection_lock = service_context_handle
39 .connect_with_publisher::<PlayerMarker, _>(external_publisher)
40 .await
41 .context("Connecting to fuchsia.media.sounds.Player")
42 .map_err(|e| log::error!("Failed to connect to fuchsia.media.sounds.Player: {}", e))
43 .ok()
44 }
45}
46
47pub(super) async fn play_sound<'a>(
52 sound_player_proxy: &ExternalServiceProxy<PlayerProxy, ExternalEventPublisher>,
53 file_name: &'a str,
54 id: u32,
55 added_files: Rc<Mutex<HashSet<&'a str>>>,
56) -> Result<(), Error> {
57 if added_files.lock().await.insert(file_name) {
59 let sound_file_channel = match resource_file(file_name) {
60 Ok(file) => Some(file),
61 Err(e) => return Err(anyhow!("[earcons] Failed to convert sound file: {}", e)),
62 };
63 if let Some(file_channel) = sound_file_channel {
64 match call_async!(sound_player_proxy => add_sound_from_file(id, file_channel)).await {
65 Ok(_) => log::debug!("[earcons] Added sound to Player: {}", file_name),
66 Err(e) => {
67 return Err(anyhow!("[earcons] Unable to add sound to Player: {}", e));
68 }
69 };
70 }
71 }
72
73 let sound_player_proxy = sound_player_proxy.clone();
74 fasync::Task::local(async move {
77 if let Err(e) =
78 call_async!(sound_player_proxy => play_sound2(id, AudioRenderUsage2::Background)).await
79 {
80 log::error!("[earcons] Unable to Play sound from Player: {}", e);
81 };
82 })
83 .detach();
84 Ok(())
85}