fuchsia_audio_device/
audio_frame_sink.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
// Copyright 2023 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 fuchsia_inspect as inspect;
use fuchsia_inspect_derive::{AttachError, Inspect};
use fuchsia_sync::Mutex;
use futures::task::{Context, Poll};
use futures::{io, FutureExt};
use std::pin::Pin;
use std::sync::Arc;
use tracing::warn;

use crate::frame_vmo;
use crate::stream_config::{SoftStreamConfig, StreamConfigOrTask};
use crate::types::{Error, Result};

/// A sink that accepts audio frames to send as input to Fuchsia audio
/// Usually acquired via SoftStreamConfig::create_input()
pub struct AudioFrameSink {
    /// Handle to the VMO that is receiving the frames.
    frame_vmo: Arc<Mutex<frame_vmo::FrameVmo>>,
    /// The index of the next frame we are writing.
    next_frame_index: usize,
    /// StreamConfig this is attached to, or the SoftStreamConfig::process_requests task
    stream_config: StreamConfigOrTask,
    /// Inspect node
    inspect: inspect::Node,
}

impl AudioFrameSink {
    pub fn new(stream_config: SoftStreamConfig) -> AudioFrameSink {
        AudioFrameSink {
            frame_vmo: stream_config.frame_vmo(),
            next_frame_index: 0,
            stream_config: StreamConfigOrTask::StreamConfig(stream_config),
            inspect: Default::default(),
        }
    }

    /// Start the requests task if not started, and poll the task.
    fn poll_task(&mut self, cx: &mut Context<'_>) -> Poll<Result<()>> {
        if let StreamConfigOrTask::Complete = &self.stream_config {
            return Poll::Ready(Err(Error::InvalidState));
        }
        if let StreamConfigOrTask::Task(ref mut task) = &mut self.stream_config {
            return task.poll_unpin(cx);
        }
        self.stream_config.start();
        self.poll_task(cx)
    }
}

impl Inspect for &mut AudioFrameSink {
    fn iattach(
        self,
        parent: &fuchsia_inspect::Node,
        name: impl AsRef<str>,
    ) -> core::result::Result<(), AttachError> {
        self.inspect = parent.create_child(name.as_ref());
        if let StreamConfigOrTask::StreamConfig(ref mut o) = &mut self.stream_config {
            return o.iattach(&self.inspect, "soft_stream_config");
        }
        Ok(())
    }
}

impl io::AsyncWrite for AudioFrameSink {
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<std::result::Result<usize, io::Error>> {
        if let Poll::Ready(r) = self.poll_task(cx) {
            self.stream_config = StreamConfigOrTask::Complete;
            if let Some(error) = r.err() {
                return Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, error)));
            } else {
                return Poll::Ready(Err(io::ErrorKind::BrokenPipe.into()));
            }
        }
        let result = {
            let mut lock = self.frame_vmo.lock();
            futures::ready!(lock.poll_write(self.next_frame_index, buf, cx))
        };
        match result {
            Ok((latest, missed)) => {
                if missed > 0 {
                    warn!("Couldn't write {missed} frames due to slow writing");
                }
                self.next_frame_index = latest;
                // We always write the whole buffer if it's written
                Poll::Ready(Ok(buf.len()))
            }
            Err(e) => Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, e))),
        }
    }

    fn poll_flush(
        self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
    ) -> Poll<std::result::Result<(), io::Error>> {
        // No buffering is done
        Poll::Ready(Ok(()))
    }

    fn poll_close(
        mut self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
    ) -> Poll<std::result::Result<(), io::Error>> {
        self.stream_config = StreamConfigOrTask::Complete;
        Poll::Ready(Ok(()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use async_utils::PollExt;
    use fidl_fuchsia_hardware_audio::*;
    use fidl_fuchsia_media::{AudioChannelId, AudioPcmMode, PcmFormat};
    use fixture::fixture;
    use fuchsia_async as fasync;
    use futures::AsyncWriteExt;

    const TEST_UNIQUE_ID: &[u8; 16] = &[5; 16];
    const TEST_CLOCK_DOMAIN: u32 = 0x00010203;

    pub(crate) fn with_audio_frame_sink<F>(_name: &str, test: F)
    where
        F: FnOnce(fasync::TestExecutor, StreamConfigProxy, AudioFrameSink) -> (),
    {
        let exec = fasync::TestExecutor::new_with_fake_time();
        let format = PcmFormat {
            pcm_mode: AudioPcmMode::Linear,
            bits_per_sample: 16,
            frames_per_second: 44100,
            channel_map: vec![AudioChannelId::Lf, AudioChannelId::Rf],
        };
        let (client, frame_sink) = SoftStreamConfig::create_input(
            TEST_UNIQUE_ID,
            "Google",
            "UnitTest",
            TEST_CLOCK_DOMAIN,
            format,
            zx::MonotonicDuration::from_millis(100),
        )
        .expect("should always build");
        test(exec, client.into_proxy(), frame_sink)
    }

    #[fixture(with_audio_frame_sink)]
    #[fuchsia::test]
    #[rustfmt::skip]
    fn audio_in(mut exec: fasync::TestExecutor, stream_config: StreamConfigProxy, mut frame_sink: AudioFrameSink) {

        // Some test "audio" data.  Silence in signed 16-bit, for 10ms
        let mut send_audio = Vec::new();
        let mut x: u8 = 0x01;
        const BYTES_PER_SECOND: usize = 44100 * 2 * 2;  // 44100 frames, 2 bytes per frame, 2
                                                        // channels per frame.
        send_audio.resize_with(BYTES_PER_SECOND, || {
            x = x.wrapping_add(2);
            x
        });

        let mut next_byte = 0;
        // Sending 10ms packets of the buffer (882 bytes, 441 frames * 2 bytes per frame * 2
        // channels per frame)
        const TEN_MS_BYTES: usize = 441 * 2 * 2;
        let next_buf = &send_audio[next_byte..next_byte + TEN_MS_BYTES];
        let mut write_fut = frame_sink.write(next_buf);
        // Poll the frame stream, which should start the processing of proxy requests.
        exec.run_until_stalled(&mut write_fut).expect_pending("not started yet");

        let result = exec.run_until_stalled(&mut stream_config.get_properties());
        let props1 = match result {
            Poll::Ready(Ok(v)) => v,
            x => panic!("Expected result to be ready ok, got {x:?}"),
        };

        assert_eq!(props1.unique_id.unwrap(),                *TEST_UNIQUE_ID);
        assert_eq!(props1.is_input.unwrap(),                 true);
        assert_eq!(props1.can_mute.unwrap(),                 false);
        assert_eq!(props1.can_agc.unwrap(),                  false);
        assert_eq!(props1.min_gain_db.unwrap(),              0f32);
        assert_eq!(props1.max_gain_db.unwrap(),              0f32);
        assert_eq!(props1.gain_step_db.unwrap(),             0f32);
        assert_eq!(props1.plug_detect_capabilities.unwrap(), PlugDetectCapabilities::Hardwired);
        assert_eq!(props1.manufacturer.unwrap(),             "Google");
        assert_eq!(props1.product.unwrap(),                  "UnitTest");
        assert_eq!(props1.clock_domain.unwrap(),             TEST_CLOCK_DOMAIN);

        let result = exec.run_until_stalled(&mut stream_config.get_supported_formats());
        let formats = match result {
            Poll::Ready(Ok(v)) => v,
            x => panic!("Get supported formats not ready ok: {x:?}"),
        };

        let first = formats.first().to_owned().expect("supported formats to be present");
        let pcm = first.pcm_supported_formats.to_owned().expect("pcm format to be present");
        assert_eq!(pcm.channel_sets.unwrap()[0].attributes.as_ref().unwrap().len(), 2usize);
        assert_eq!(pcm.sample_formats.unwrap()[0],        SampleFormat::PcmSigned);
        assert_eq!(pcm.bytes_per_sample.unwrap()[0],      2u8);
        assert_eq!(pcm.valid_bits_per_sample.unwrap()[0], 16u8);
        assert_eq!(pcm.frame_rates.unwrap()[0],           44100);

        let (ring_buffer, server) = fidl::endpoints::create_proxy::<RingBufferMarker>();

        let format = Format {
            pcm_format: Some(fidl_fuchsia_hardware_audio::PcmFormat {
                number_of_channels:      2u8,
                sample_format:           SampleFormat::PcmSigned,
                bytes_per_sample:        2u8,
                valid_bits_per_sample:   16u8,
                frame_rate:              44100,
            }),
            ..Default::default()
        };

        stream_config.create_ring_buffer(&format, server).expect("ring buffer error");
        let props2 = match exec.run_until_stalled(&mut ring_buffer.get_properties()) {
            Poll::Ready(Ok(v)) => v,
            x => panic!("expected get_properties to be ready ok: {x:?}"),
        };
        assert_eq!(props2.needs_cache_flush_or_invalidate, Some(false));
        assert!(props2.driver_transfer_bytes.unwrap() > 0);

        const TWO_SEC_FRAMES: u32 = 44100 * 2;

        let result = exec.run_until_stalled(&mut ring_buffer.get_vmo(TWO_SEC_FRAMES, 0)); // 2 seconds.
        assert!(result.is_ready());
        let reply = match result {
            Poll::Ready(Ok(Ok(v))) => v,
            _ => panic!("ring buffer get vmo error"),
        };
        let audio_vmo = reply.1;

        // Frames * bytes per sample * channels per sample.
        let bytes_per_two_seconds: usize = BYTES_PER_SECOND * 2;
        assert!(
            bytes_per_two_seconds <= audio_vmo.get_size().expect("should always exist after getbuffer") as usize
        );

        exec.set_fake_time(fasync::MonotonicInstant::from_nanos(42));
        let _ = exec.wake_expired_timers();
        let start_time = exec.run_until_stalled(&mut ring_buffer.start());
        if let Poll::Ready(s) = start_time {
            assert_eq!(s.expect("start time error"), 42);
        } else {
            panic!("start error");
        }

        // Writing audio should succeed now. Fill up the buffer.
        let frame_result = exec.run_until_stalled(&mut write_fut).expect("should be ready");
        // Should have written 882 bytes (441 frames * 2 bytes per frame)
        assert_eq!(frame_result.unwrap(), TEN_MS_BYTES);
        let mut write_fut = loop {
            next_byte = (next_byte + TEN_MS_BYTES) % send_audio.len();
            let next_buf = &send_audio[next_byte..next_byte + TEN_MS_BYTES];
            let mut write_fut = frame_sink.write(next_buf);
            match exec.run_until_stalled(&mut write_fut) {
                Poll::Pending => break write_fut,
                Poll::Ready(Ok(len)) => assert_eq!(TEN_MS_BYTES, len),
                x => panic!("Expected writes to succeed until pending, got {x:?} {next_byte}"),
            };
        };

        // Shouldn't be able to write any more until time goes forward.
        exec.run_until_stalled(&mut write_fut).expect_pending("buffer is full");

        // Run the ring buffer for a bit over half a second.
        exec.set_fake_time(fasync::MonotonicInstant::after(zx::MonotonicDuration::from_millis(500)));
        let _ = exec.wake_expired_timers();

        // Should be able to write again now.
        let result = exec.run_until_stalled(&mut write_fut).expect("buf isn't full");
        match result {
            Ok(len) => assert_eq!(TEN_MS_BYTES, len),
            Err(x) => panic!("Ok from frame write, got {x:?}"),
        };

        let result = exec.run_until_stalled(&mut ring_buffer.stop());
        assert!(result.is_ready());

        // Watch gain only replies once.
        let result = exec.run_until_stalled(&mut stream_config.watch_gain_state());
        assert!(result.is_ready());
        let result = exec.run_until_stalled(&mut stream_config.watch_gain_state());
        assert!(!result.is_ready());

        // Watch plug state only replies once.
        let result = exec.run_until_stalled(&mut stream_config.watch_plug_state());
        assert!(result.is_ready());
        let result = exec.run_until_stalled(&mut stream_config.watch_plug_state());
        assert!(!result.is_ready());
    }
}