mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-06 02:25:23 +00:00
ac97: combine PlaybackError and CaptureError
PlaybackError and CaptureError encode the same error cases. Combine the two into one Error type, AudioError. BUG=None TEST=builds Change-Id: I44259227d67a0284c9a11c4aafd86fafe1006f8b Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1925727 Reviewed-by: Chih-Yang Hsia <paulhsia@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Commit-Queue: Fletcher Woodruff <fletcherw@chromium.org>
This commit is contained in:
parent
38b9ad8620
commit
277ea5f4b4
1 changed files with 15 additions and 45 deletions
|
@ -95,23 +95,17 @@ impl Display for GuestMemoryError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<GuestMemoryError> for PlaybackError {
|
impl From<GuestMemoryError> for AudioError {
|
||||||
fn from(err: GuestMemoryError) -> Self {
|
fn from(err: GuestMemoryError) -> Self {
|
||||||
PlaybackError::ReadingGuestError(err)
|
AudioError::ReadingGuestError(err)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<GuestMemoryError> for CaptureError {
|
|
||||||
fn from(err: GuestMemoryError) -> Self {
|
|
||||||
CaptureError::ReadingGuestError(err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type GuestMemoryResult<T> = std::result::Result<T, GuestMemoryError>;
|
type GuestMemoryResult<T> = std::result::Result<T, GuestMemoryError>;
|
||||||
|
|
||||||
// Internal error type used for reporting errors from the audio playback thread.
|
// Internal error type used for reporting errors from the audio thread.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum PlaybackError {
|
enum AudioError {
|
||||||
// Failure to read guest memory.
|
// Failure to read guest memory.
|
||||||
ReadingGuestError(GuestMemoryError),
|
ReadingGuestError(GuestMemoryError),
|
||||||
// Failure to get an buffer from the stream.
|
// Failure to get an buffer from the stream.
|
||||||
|
@ -120,11 +114,11 @@ enum PlaybackError {
|
||||||
WritingOutput(std::io::Error),
|
WritingOutput(std::io::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for PlaybackError {}
|
impl std::error::Error for AudioError {}
|
||||||
|
|
||||||
impl Display for PlaybackError {
|
impl Display for AudioError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
use self::PlaybackError::*;
|
use self::AudioError::*;
|
||||||
|
|
||||||
match self {
|
match self {
|
||||||
ReadingGuestError(e) => write!(f, "Failed to read guest memory: {}.", e),
|
ReadingGuestError(e) => write!(f, "Failed to read guest memory: {}.", e),
|
||||||
|
@ -134,31 +128,7 @@ impl Display for PlaybackError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type PlaybackResult<T> = std::result::Result<T, PlaybackError>;
|
type AudioResult<T> = std::result::Result<T, AudioError>;
|
||||||
|
|
||||||
// Internal error type used for reporting errors from the audio capture thread.
|
|
||||||
#[derive(Debug)]
|
|
||||||
enum CaptureError {
|
|
||||||
// Failure to read guest memory.
|
|
||||||
ReadingGuestError(GuestMemoryError),
|
|
||||||
// Failure to get an buffer from the stream.
|
|
||||||
StreamError(Box<dyn Error>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for CaptureError {}
|
|
||||||
|
|
||||||
impl Display for CaptureError {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
use self::CaptureError::*;
|
|
||||||
|
|
||||||
match self {
|
|
||||||
ReadingGuestError(e) => write!(f, "Failed to read guest memory: {}.", e),
|
|
||||||
StreamError(e) => write!(f, "Failed to get a buffer from the stream: {}", e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type CaptureResult<T> = std::result::Result<T, CaptureError>;
|
|
||||||
|
|
||||||
// Audio thread book-keeping data
|
// Audio thread book-keeping data
|
||||||
struct AudioThreadInfo {
|
struct AudioThreadInfo {
|
||||||
|
@ -633,7 +603,7 @@ fn play_buffer(
|
||||||
regs: &mut Ac97BusMasterRegs,
|
regs: &mut Ac97BusMasterRegs,
|
||||||
mem: &GuestMemory,
|
mem: &GuestMemory,
|
||||||
out_buffer: &mut PlaybackBuffer,
|
out_buffer: &mut PlaybackBuffer,
|
||||||
) -> PlaybackResult<()> {
|
) -> AudioResult<()> {
|
||||||
// If the current buffer had any samples in it, mark it as done.
|
// If the current buffer had any samples in it, mark it as done.
|
||||||
if regs.func_regs_mut(Ac97Function::Output).picb > 0 {
|
if regs.func_regs_mut(Ac97Function::Output).picb > 0 {
|
||||||
buffer_completed(regs, mem, Ac97Function::Output)?
|
buffer_completed(regs, mem, Ac97Function::Output)?
|
||||||
|
@ -646,7 +616,7 @@ fn play_buffer(
|
||||||
let zeros = vec![0u8; buffer_len as usize];
|
let zeros = vec![0u8; buffer_len as usize];
|
||||||
out_buffer
|
out_buffer
|
||||||
.write(&zeros)
|
.write(&zeros)
|
||||||
.map_err(PlaybackError::WritingOutput)?;
|
.map_err(AudioError::WritingOutput)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -695,11 +665,11 @@ fn audio_out_thread(
|
||||||
mem: GuestMemory,
|
mem: GuestMemory,
|
||||||
thread_run: &AtomicBool,
|
thread_run: &AtomicBool,
|
||||||
mut output_stream: Box<dyn PlaybackBufferStream>,
|
mut output_stream: Box<dyn PlaybackBufferStream>,
|
||||||
) -> PlaybackResult<()> {
|
) -> AudioResult<()> {
|
||||||
while thread_run.load(Ordering::Relaxed) {
|
while thread_run.load(Ordering::Relaxed) {
|
||||||
output_stream
|
output_stream
|
||||||
.next_playback_buffer()
|
.next_playback_buffer()
|
||||||
.map_err(PlaybackError::StreamError)
|
.map_err(AudioError::StreamError)
|
||||||
.and_then(|mut pb_buf| play_buffer(&mut regs.lock(), &mem, &mut pb_buf))?;
|
.and_then(|mut pb_buf| play_buffer(&mut regs.lock(), &mem, &mut pb_buf))?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -710,7 +680,7 @@ fn capture_buffer(
|
||||||
regs: &mut Ac97BusMasterRegs,
|
regs: &mut Ac97BusMasterRegs,
|
||||||
mem: &GuestMemory,
|
mem: &GuestMemory,
|
||||||
in_buffer: &mut CaptureBuffer,
|
in_buffer: &mut CaptureBuffer,
|
||||||
) -> CaptureResult<()> {
|
) -> AudioResult<()> {
|
||||||
// If the current buffer had any samples in it, mark it as done.
|
// If the current buffer had any samples in it, mark it as done.
|
||||||
if regs.func_regs_mut(Ac97Function::Input).picb > 0 {
|
if regs.func_regs_mut(Ac97Function::Input).picb > 0 {
|
||||||
buffer_completed(regs, mem, Ac97Function::Input)?
|
buffer_completed(regs, mem, Ac97Function::Input)?
|
||||||
|
@ -728,11 +698,11 @@ fn audio_in_thread(
|
||||||
mem: GuestMemory,
|
mem: GuestMemory,
|
||||||
thread_run: &AtomicBool,
|
thread_run: &AtomicBool,
|
||||||
mut input_stream: Box<dyn CaptureBufferStream>,
|
mut input_stream: Box<dyn CaptureBufferStream>,
|
||||||
) -> CaptureResult<()> {
|
) -> AudioResult<()> {
|
||||||
while thread_run.load(Ordering::Relaxed) {
|
while thread_run.load(Ordering::Relaxed) {
|
||||||
input_stream
|
input_stream
|
||||||
.next_capture_buffer()
|
.next_capture_buffer()
|
||||||
.map_err(CaptureError::StreamError)
|
.map_err(AudioError::StreamError)
|
||||||
.and_then(|mut cp_buf| capture_buffer(&mut regs.lock(), &mem, &mut cp_buf))?;
|
.and_then(|mut cp_buf| capture_buffer(&mut regs.lock(), &mem, &mut cp_buf))?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue