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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Do nothing on unix as win_audio is windows only.
#![cfg(windows)]
#![allow(non_upper_case_globals)]
include!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/src/r8brain_sys/bindings.rs"
));

macro_rules! check_hresult {
    ($hr: expr, $error: expr, $msg: expr) => {
        if winapi::shared::winerror::FAILED($hr) {
            base::warn!("{}: {}", $msg, $hr);
            Err($error)
        } else {
            Ok($hr)
        }
    };
}

pub mod intermediate_resampler_buffer;
mod win_audio_impl;
use std::error;
use std::sync::Arc;

use audio_streams::capture::AsyncCaptureBufferStream;
use audio_streams::capture::NoopCaptureStream;
use audio_streams::AsyncPlaybackBufferStream;
use audio_streams::NoopStream;
use audio_streams::NoopStreamSource;
use audio_streams::NoopStreamSourceGenerator;
use audio_streams::PlaybackBufferStream;
use audio_streams::SampleFormat;
use audio_streams::StreamSource;
use audio_util::FileStreamSourceGenerator;
use base::error;
use base::info;
use base::warn;
pub use intermediate_resampler_buffer::ANDROID_CAPTURE_FRAME_SIZE_BYTES;
pub use intermediate_resampler_buffer::BYTES_PER_32FLOAT;
use sync::Mutex;
use win_audio_impl::async_stream::WinAudioStreamSourceGenerator;
pub use win_audio_impl::*;

pub type BoxError = Box<dyn error::Error + Send + Sync>;

pub trait WinStreamSourceGenerator: Send + Sync {
    fn generate(&self) -> Result<Box<dyn WinAudioServer>, BoxError>;
}

impl WinStreamSourceGenerator for WinAudioStreamSourceGenerator {
    fn generate(&self) -> std::result::Result<Box<dyn WinAudioServer>, BoxError> {
        Ok(Box::new(WinAudio::new()?))
    }
}

impl WinStreamSourceGenerator for NoopStreamSourceGenerator {
    fn generate(&self) -> Result<Box<dyn WinAudioServer>, BoxError> {
        Ok(Box::new(NoopStreamSource))
    }
}

impl WinStreamSourceGenerator for FileStreamSourceGenerator {
    fn generate(&self) -> Result<Box<dyn WinAudioServer>, BoxError> {
        unimplemented!();
    }
}

/// Contains information about the audio engine's properties, such as its audio sample format
/// and its period in frames.
///
/// This does exclude whether the bit depth is in the form of floats or ints. The bit depth form
/// isn't used for sample rate conversion so it's excluded.
#[derive(Clone, Copy)]
pub struct AudioSharedFormat {
    pub bit_depth: usize,
    pub frame_rate: usize,
    pub shared_audio_engine_period_in_frames: usize,
    pub channels: usize,
    // Only available for WAVEFORMATEXTENSIBLE
    pub channel_mask: Option<u32>,
}

impl AudioSharedFormat {
    fn get_shared_audio_engine_period_in_bytes(&self) -> usize {
        let frame_size_bytes = self.bit_depth * self.channels / 8;
        self.shared_audio_engine_period_in_frames * frame_size_bytes
    }
}

/// Implementation of StreamSource which will create the playback stream for the Windows
/// audio engine.
///
/// Extending the StreamSource trait will allow us to make necessary changes without modifying
/// the third party audiostream library.
pub trait WinAudioServer: StreamSource {
    fn new_playback_stream_and_get_shared_format(
        &mut self,
        num_channels: usize,
        format: SampleFormat,
        frame_rate: usize,
        buffer_size: usize,
    ) -> Result<(Arc<Mutex<Box<dyn PlaybackBufferStream>>>, AudioSharedFormat), BoxError>;

    fn new_async_playback_stream_and_get_shared_format(
        &mut self,
        _num_channels: usize,
        _format: SampleFormat,
        _frame_rate: usize,
        _buffer_size: usize,
        _ex: &dyn audio_streams::AudioStreamsExecutor,
        _audio_client_guid: Option<String>,
    ) -> Result<(Box<dyn AsyncPlaybackBufferStream>, AudioSharedFormat), BoxError> {
        unimplemented!()
    }

    fn new_async_capture_stream_and_get_shared_format(
        &mut self,
        _num_channels: usize,
        _format: SampleFormat,
        _frame_rate: u32,
        _buffer_size: usize,
        _ex: &dyn audio_streams::AudioStreamsExecutor,
    ) -> Result<(Box<dyn AsyncCaptureBufferStream>, AudioSharedFormat), BoxError> {
        unimplemented!()
    }

    /// Evict the playback stream cache so that the audio device can be released, thus allowing
    /// for machines to go to sleep.
    fn evict_playback_stream_cache(&mut self) {
        unimplemented!()
    }

    /// Returns true if audio server is a noop stream. This determine if evicting a cache is worth
    /// doing
    fn is_noop_stream(&self) -> bool;
}

impl WinAudioServer for WinAudio {
    fn new_playback_stream_and_get_shared_format(
        &mut self,
        num_channels: usize,
        format: SampleFormat,
        frame_rate: usize,
        buffer_size: usize,
    ) -> Result<(Arc<Mutex<Box<dyn PlaybackBufferStream>>>, AudioSharedFormat), BoxError> {
        let hr = WinAudio::co_init_once_per_thread();
        let _ = check_hresult!(hr, WinAudioError::from(hr), "Co Initialized failed");

        // Return the existing stream if we have one.
        // This is mainly to reduce audio skips caused by a buffer underrun on the guest. An
        // underrun causes the guest to stop the audio stream, but then start it back up when the
        // guest buffer is filled again.
        if let Some((playback_buffer_stream, audio_format)) =
            self.cached_playback_buffer_stream.as_ref()
        {
            info!("Reusing playback_buffer_stream.");
            return Ok((playback_buffer_stream.clone(), *audio_format));
        }

        let (playback_buffer_stream, audio_shared_format): (
            Arc<Mutex<Box<dyn PlaybackBufferStream>>>,
            AudioSharedFormat,
        ) = match win_audio_impl::WinAudioRenderer::new(
            num_channels,
            format,
            frame_rate as u32,
            buffer_size,
        ) {
            Ok(renderer) => {
                let audio_shared_format = renderer.get_audio_shared_format();
                let renderer_arc = Arc::new(Mutex::new(
                    Box::new(renderer) as Box<dyn PlaybackBufferStream>
                ));
                self.cached_playback_buffer_stream =
                    Some((renderer_arc.clone(), audio_shared_format));
                (renderer_arc, audio_shared_format)
            }
            Err(e) => {
                error!(
                    "Failed to create WinAudioRenderer and in an unrecoverable state. Fallback to \
                     NoopStream with error: {}",
                    e
                );
                (
                    Arc::new(Mutex::new(Box::new(NoopStream::new(
                        num_channels,
                        SampleFormat::S16LE,
                        frame_rate as u32,
                        buffer_size,
                    )))),
                    AudioSharedFormat {
                        bit_depth: 16,
                        frame_rate,
                        channels: 2,
                        shared_audio_engine_period_in_frames: frame_rate / 100,
                        channel_mask: None,
                    },
                )
            }
        };

        Ok((playback_buffer_stream, audio_shared_format))
    }

    // TODO(b/275406212): AudioSharedFormat not used outside of this crate anymore. Clean up before
    // upstreaming.
    fn new_async_playback_stream_and_get_shared_format(
        &mut self,
        num_channels: usize,
        guest_bit_depth: SampleFormat,
        frame_rate: usize,
        buffer_size: usize,
        ex: &dyn audio_streams::AudioStreamsExecutor,
        audio_client_guid: Option<String>,
    ) -> Result<(Box<dyn AsyncPlaybackBufferStream>, AudioSharedFormat), BoxError> {
        let hr = WinAudio::co_init_once_per_thread();
        let _ = check_hresult!(hr, WinAudioError::from(hr), "Co Initialized failed");

        let (async_playback_buffer_stream, audio_shared_format): (
            Box<dyn AsyncPlaybackBufferStream>,
            AudioSharedFormat,
        ) = match win_audio_impl::WinAudioRenderer::new_async(
            num_channels,
            guest_bit_depth,
            frame_rate as u32,
            buffer_size,
            ex,
            audio_client_guid,
        ) {
            Ok(renderer) => {
                let audio_shared_format = renderer.get_audio_shared_format();
                let renderer_box = Box::new(renderer) as Box<dyn AsyncPlaybackBufferStream>;
                (renderer_box, audio_shared_format)
            }
            Err(e) => {
                error!(
                    "Failed to create WinAudioRenderer and in an unrecoverable state. Fallback to \
                     NoopStream with error: {}",
                    e
                );
                (
                    Box::new(NoopStream::new(
                        num_channels,
                        SampleFormat::S32LE,
                        frame_rate as u32,
                        buffer_size,
                    )),
                    AudioSharedFormat {
                        bit_depth: 32,
                        frame_rate,
                        channels: 2,
                        shared_audio_engine_period_in_frames: frame_rate / 100,
                        channel_mask: None,
                    },
                )
            }
        };

        Ok((async_playback_buffer_stream, audio_shared_format))
    }

    // TODO(b/275406212): AudioSharedFormat not used outside of this crate anymore. Clean up before
    // upstreaming.
    fn new_async_capture_stream_and_get_shared_format(
        &mut self,
        num_channels: usize,
        guest_bit_depth: SampleFormat,
        frame_rate: u32,
        buffer_size: usize,
        ex: &dyn audio_streams::AudioStreamsExecutor,
    ) -> Result<(Box<dyn AsyncCaptureBufferStream>, AudioSharedFormat), BoxError> {
        let hr = WinAudio::co_init_once_per_thread();
        let _ = check_hresult!(hr, WinAudioError::from(hr), "Co Initialized failed");

        let (capturer, audio_shared_format): (
            Box<dyn AsyncCaptureBufferStream>,
            AudioSharedFormat,
        ) = match WinAudioCapturer::new_async(
            num_channels,
            guest_bit_depth,
            frame_rate,
            buffer_size,
            ex,
        ) {
            Ok(capturer) => {
                let audio_shared_format = capturer.get_audio_shared_format();
                (Box::new(capturer), audio_shared_format)
            }
            Err(e) => {
                warn!("Failed to create WinAudioCapturer. Fallback to NoopCaptureStream with error: {}", e);
                (
                    Box::new(NoopCaptureStream::new(
                        num_channels,
                        SampleFormat::S32LE,
                        frame_rate,
                        buffer_size,
                    )),
                    AudioSharedFormat {
                        bit_depth: 32,
                        frame_rate: frame_rate as usize,
                        channels: 2,
                        shared_audio_engine_period_in_frames: frame_rate as usize / 100,
                        channel_mask: None,
                    },
                )
            }
        };

        Ok((capturer, audio_shared_format))
    }

    fn evict_playback_stream_cache(&mut self) {
        self.cached_playback_buffer_stream = None;
    }

    fn is_noop_stream(&self) -> bool {
        false
    }
}

impl WinAudioServer for NoopStreamSource {
    fn new_playback_stream_and_get_shared_format(
        &mut self,
        num_channels: usize,
        format: SampleFormat,
        frame_rate: usize,
        buffer_size: usize,
    ) -> Result<(Arc<Mutex<Box<dyn PlaybackBufferStream>>>, AudioSharedFormat), BoxError> {
        let (_, playback_buffer_stream) = self
            .new_playback_stream(num_channels, format, frame_rate as u32, buffer_size)
            .unwrap();
        Ok((
            Arc::new(Mutex::new(playback_buffer_stream)),
            AudioSharedFormat {
                bit_depth: 16,
                frame_rate,
                channels: 2,
                shared_audio_engine_period_in_frames: frame_rate / 100,
                channel_mask: None,
            },
        ))
    }

    fn new_async_playback_stream_and_get_shared_format(
        &mut self,
        num_channels: usize,
        format: SampleFormat,
        frame_rate: usize,
        buffer_size: usize,
        ex: &dyn audio_streams::AudioStreamsExecutor,
        _audio_client_guid: Option<String>,
    ) -> Result<(Box<dyn AsyncPlaybackBufferStream>, AudioSharedFormat), BoxError> {
        let (_, playback_stream) = self
            .new_async_playback_stream(num_channels, format, frame_rate as u32, buffer_size, ex)
            .unwrap();

        // Set shared format to be the same as the incoming audio format.
        let format = AudioSharedFormat {
            bit_depth: format.sample_bytes() * 8,
            frame_rate,
            channels: num_channels,
            shared_audio_engine_period_in_frames: buffer_size * format.sample_bytes(),
            channel_mask: None,
        };
        Ok((playback_stream, format))
    }

    fn is_noop_stream(&self) -> bool {
        true
    }
}

pub fn create_win_audio_device() -> Result<WinAudio, BoxError> {
    WinAudio::new()
}