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
// Copyright 2022 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 anyhow::{anyhow, Context, Error};
use fidl::endpoints::ServerEnd;
use fidl_fuchsia_audio_controller as fac;
use futures::TryStreamExt;
use std::sync::atomic::{AtomicBool, Ordering};

pub mod dai;
pub mod device;
pub mod format;
pub mod format_set;
pub mod registry;

pub use format::{parse_duration, str_to_clock, Format};
pub use registry::Registry;

pub async fn stop_listener(
    canceler: ServerEnd<fac::RecordCancelerMarker>,
    stop_signal: &AtomicBool,
) -> Result<(), Error> {
    let mut stream = canceler
        .into_stream()
        .map_err(|e| anyhow!("Error turning canceler server into stream {}", e))?;

    let item = stream.try_next().await;
    stop_signal.store(true, Ordering::SeqCst);

    match item {
        Ok(Some(request)) => match request {
            fac::RecordCancelerRequest::Cancel { responder } => {
                responder.send(Ok(())).context("FIDL error with stop request")
            }
            _ => Err(anyhow!("Unimplemented method on canceler")),
        },
        Ok(None) => Ok(()),
        Err(e) => Err(anyhow!("FIDL error with stop request: {e}")),
    }
}