feat(audio): add guest voice controls and reliable lifecycle delivery
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
use super::AudioCommand;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
/// One pending frame plus an independent lifecycle lane. Control state never
|
||||
/// competes with guest commands for capacity. The lifecycle batch can include
|
||||
/// an interrupted frame prefix (at most 1024 + 1024 commands). Only one consumer
|
||||
/// may drain it.
|
||||
#[derive(Clone, Default)]
|
||||
pub struct AudioTransport(Arc<Mutex<Mailbox>>);
|
||||
|
||||
#[derive(Default)]
|
||||
struct Mailbox {
|
||||
epoch: u64,
|
||||
paused: bool,
|
||||
frame: Option<Vec<AudioCommand>>,
|
||||
lifecycle: Option<Vec<AudioCommand>>,
|
||||
}
|
||||
|
||||
pub struct AudioDelivery {
|
||||
pub epoch: u64,
|
||||
pub paused: bool,
|
||||
pub frame: Option<Vec<AudioCommand>>,
|
||||
pub lifecycle: Option<Vec<AudioCommand>>,
|
||||
}
|
||||
|
||||
impl AudioTransport {
|
||||
/// The real-time consumer never waits for the producer's short critical section.
|
||||
pub fn try_take(&self) -> Option<AudioDelivery> {
|
||||
let mut state = self.0.try_lock().ok()?;
|
||||
Some(AudioDelivery {
|
||||
epoch: state.epoch,
|
||||
paused: state.paused,
|
||||
frame: state.frame.take(),
|
||||
lifecycle: state.lifecycle.take(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn consume_silently(&self) {
|
||||
let _ = self.try_take();
|
||||
}
|
||||
|
||||
pub fn ready(&self) -> bool {
|
||||
let state = self.0.lock().expect("audio mailbox");
|
||||
state.frame.is_none() && state.lifecycle.is_none()
|
||||
}
|
||||
|
||||
pub fn lifecycle_ready(&self) -> bool {
|
||||
self.0.lock().expect("audio mailbox").lifecycle.is_none()
|
||||
}
|
||||
|
||||
pub(super) fn publish(&self, commands: Vec<AudioCommand>, lifecycle: bool) {
|
||||
if commands.is_empty() {
|
||||
return;
|
||||
}
|
||||
let mut state = self.0.lock().expect("audio mailbox");
|
||||
let target = if lifecycle { &mut state.lifecycle } else { &mut state.frame };
|
||||
assert!(target.is_none(), "audio producer must respect backpressure");
|
||||
*target = Some(commands);
|
||||
}
|
||||
|
||||
pub(super) fn reset(&self) {
|
||||
let mut state = self.0.lock().expect("audio mailbox");
|
||||
state.epoch = state.epoch.checked_add(1).expect("audio epoch exhausted");
|
||||
state.frame = None;
|
||||
state.lifecycle = None;
|
||||
state.paused = false;
|
||||
}
|
||||
|
||||
pub(super) fn set_paused(&self, paused: bool) {
|
||||
self.0.lock().expect("audio mailbox").paused = paused;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn realtime_consumer_does_not_wait_for_publication_lock() {
|
||||
let transport = AudioTransport::default();
|
||||
let guard = transport.0.lock().unwrap();
|
||||
assert!(transport.try_take().is_none());
|
||||
drop(guard);
|
||||
assert!(transport.try_take().is_some());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user