diff --git a/common/audio_streams/src/audio_streams.rs b/common/audio_streams/src/audio_streams.rs index 881f7a33b6..ec47ddfaa6 100644 --- a/common/audio_streams/src/audio_streams.rs +++ b/common/audio_streams/src/audio_streams.rs @@ -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> { + fn keep_rds(&self) -> Option> { 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( + &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(&mut self, size: usize, cb: F) -> io::Result { self.buffer.write_copy_cb(size, cb)