Skip to content

Commit 60a92a5

Browse files
committed
refactor(aaudio): simplify Stream from enum to struct
Replace `Stream` enum with a struct containing `inner: Arc<Mutex<AudioStream>>` and `direction: DeviceDirection` fields. This eliminates code duplication while maintaining the same functionality.
1 parent 858feb1 commit 60a92a5

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

src/host/aaudio/mod.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ pub struct Device(Option<AudioDeviceInfo>);
131131
/// - The pointer in AudioStream (NonNull<AAudioStreamStruct>) is valid for the lifetime
132132
/// of the stream and AAudio C API functions are thread-safe at the C level
133133
#[derive(Clone)]
134-
pub enum Stream {
135-
Input(Arc<Mutex<AudioStream>>),
136-
Output(Arc<Mutex<AudioStream>>),
134+
pub struct Stream {
135+
inner: Arc<Mutex<AudioStream>>,
136+
direction: DeviceDirection,
137137
}
138138

139139
// SAFETY: AudioStream can be safely sent between threads. The AAudio C API is thread-safe
@@ -281,7 +281,7 @@ fn configure_for_device(
281281
BufferSize::Default => {
282282
// Use the optimal burst size from AudioManager:
283283
// https://developer.android.com/ndk/guides/audio/audio-latency#buffer-size
284-
AudioManager::get_frames_per_buffer().ok()
284+
AudioManager::get_frames_per_buffer().ok().map(|s| s as u32)
285285
}
286286
BufferSize::Fixed(size) => Some(size),
287287
};
@@ -339,7 +339,10 @@ where
339339
// is safe because the Mutex provides exclusive access and AudioStream's thread safety
340340
// is documented in the AAudio C API.
341341
#[allow(clippy::arc_with_non_send_sync)]
342-
Ok(Stream::Input(Arc::new(Mutex::new(stream))))
342+
Ok(Stream {
343+
inner: Arc::new(Mutex::new(stream)),
344+
direction: DeviceDirection::Input,
345+
})
343346
}
344347

345348
fn build_output_stream<D, E>(
@@ -385,7 +388,10 @@ where
385388
// is safe because the Mutex provides exclusive access and AudioStream's thread safety
386389
// is documented in the AAudio C API.
387390
#[allow(clippy::arc_with_non_send_sync)]
388-
Ok(Stream::Output(Arc::new(Mutex::new(stream))))
391+
Ok(Stream {
392+
inner: Arc::new(Mutex::new(stream)),
393+
direction: DeviceDirection::Output,
394+
})
389395
}
390396

391397
impl DeviceTrait for Device {
@@ -588,47 +594,40 @@ impl DeviceTrait for Device {
588594

589595
impl StreamTrait for Stream {
590596
fn play(&self) -> Result<(), PlayStreamError> {
591-
match self {
592-
Self::Input(stream) => stream
593-
.lock()
594-
.unwrap()
595-
.request_start()
596-
.map_err(PlayStreamError::from),
597-
Self::Output(stream) => stream
598-
.lock()
599-
.unwrap()
600-
.request_start()
601-
.map_err(PlayStreamError::from),
602-
}
597+
self.inner
598+
.lock()
599+
.unwrap()
600+
.request_start()
601+
.map_err(PlayStreamError::from)
603602
}
604603

605604
fn pause(&self) -> Result<(), PauseStreamError> {
606-
match self {
607-
Self::Input(_) => Err(BackendSpecificError {
605+
match self.direction {
606+
DeviceDirection::Input => Err(BackendSpecificError {
608607
description: "Pause called on the input stream.".to_owned(),
609608
}
610609
.into()),
611-
Self::Output(stream) => stream
610+
DeviceDirection::Output => self
611+
.inner
612612
.lock()
613613
.unwrap()
614614
.request_pause()
615615
.map_err(PauseStreamError::from),
616+
DeviceDirection::Duplex => Err(BackendSpecificError {
617+
description: "Duplex streams not supported in AAudio.".to_owned(),
618+
}
619+
.into()),
616620
}
617621
}
618622

619623
fn buffer_size(&self) -> Option<crate::FrameCount> {
620-
let stream = match self {
621-
Self::Input(stream) => stream.lock().ok()?,
622-
Self::Output(stream) => stream.lock().ok()?,
623-
};
624+
let stream = self.inner.lock().ok()?;
624625

625626
// If frames_per_data_callback was not explicitly set (returning 0),
626627
// fall back to the burst size as that's what AAudio uses by default.
627-
match stream.get_frames_per_data_callback() {
628+
match stream.frames_per_data_callback() {
628629
Some(size) if size > 0 => Some(size as crate::FrameCount),
629-
_ => stream
630-
.get_frames_per_burst()
631-
.map(|f| f as crate::FrameCount),
630+
_ => stream.frames_per_burst().map(|f| f as crate::FrameCount),
632631
}
633632
}
634633
}

0 commit comments

Comments
 (0)