audio_streams: Upstream to support Windows

BUG=b:213150293
TEST=built and presubmits

Change-Id: Ia032dd4645d5a827e4b462ebbf9d10db410f5468
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3764468
Commit-Queue: Richard Zhang <rizhang@google.com>
Reviewed-by: Chih-Yang Hsia <paulhsia@chromium.org>
Tested-by: Richard Zhang <rizhang@google.com>
This commit is contained in:
Richard 2022-06-27 17:26:08 -07:00 committed by crosvm LUCI
parent 57d3db037f
commit 27d27a0402

View file

@ -47,7 +47,9 @@ use std::error;
use std::fmt::{self, Display};
use std::io::{self, Read, Write};
#[cfg(unix)]
use std::os::unix::io::RawFd;
use std::os::unix::io::RawFd as RawDescriptor;
#[cfg(windows)]
use std::os::windows::io::RawHandle as RawDescriptor;
use std::result::Result;
use std::str::FromStr;
use std::time::{Duration, Instant};
@ -233,8 +235,7 @@ pub trait StreamSource: Send {
/// Returns any open file descriptors needed by the implementor. The FD list helps users of the
/// StreamSource enter Linux jails making sure not to close needed FDs.
#[cfg(unix)]
fn keep_fds(&self) -> Option<Vec<RawFd>> {
fn keep_rds(&self) -> Option<Vec<RawDescriptor>> {
None
}
}
@ -331,6 +332,8 @@ pub trait AsyncBufferCommit {
pub enum PlaybackBufferError {
#[error("Invalid buffer length")]
InvalidLength,
#[error("Slicing of playback buffer out of bounds")]
SliceOutOfBounds,
}
/// `AudioBuffer` is one buffer that holds buffer_size audio frames.
@ -451,6 +454,26 @@ impl<'a> PlaybackBuffer<'a> {
.commit(self.buffer.offset / self.buffer.frame_size);
}
/// Writes up to `size` bytes directly to this buffer inside of the given callback function
/// with a buffer size error check.
///
/// TODO(b/238933737): Investigate removing this method for Windows when
/// switching from Ac97 to Virtio-Snd
pub fn copy_cb_with_checks<F: FnOnce(&mut [u8])>(
&mut self,
size: usize,
cb: F,
) -> Result<(), PlaybackBufferError> {
// only write complete frames.
let len = size / self.buffer.frame_size * self.buffer.frame_size;
if self.buffer.offset + len > self.buffer.buffer.len() {
return Err(PlaybackBufferError::SliceOutOfBounds);
}
cb(&mut self.buffer.buffer[self.buffer.offset..(self.buffer.offset + len)]);
self.buffer.offset += len;
Ok(())
}
/// Writes up to `size` bytes directly to this buffer inside of the given callback function.
pub fn copy_cb<F: FnOnce(&mut [u8])>(&mut self, size: usize, cb: F) -> io::Result<usize> {
self.buffer.write_copy_cb(size, cb)