use crate::context::ConcreteBaseAudioContext;
use crate::context::{AudioContextState, AudioNodeId};
use crate::{AudioBuffer, AudioRenderCapacityEvent};
use std::any::Any;
use std::collections::HashMap;
use std::hash::Hash;
use std::ops::ControlFlow;
use std::sync::{Arc, Mutex};
use crossbeam_channel::Receiver;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Event {
pub type_: &'static str,
}
#[derive(Hash, Eq, PartialEq, Debug)]
pub(crate) enum EventType {
Ended(AudioNodeId),
SinkChange,
StateChange,
RenderCapacity,
ProcessorError(AudioNodeId),
Diagnostics,
Message(AudioNodeId),
Complete,
AudioProcessing(AudioNodeId),
}
#[non_exhaustive]
#[derive(Debug)]
pub struct ErrorEvent {
pub message: String,
pub error: Box<dyn Any + Send>,
pub event: Event,
}
#[non_exhaustive]
#[derive(Debug)]
pub struct AudioProcessingEvent {
pub input_buffer: AudioBuffer,
pub output_buffer: AudioBuffer,
pub playback_time: f64,
pub(crate) registration: Option<(ConcreteBaseAudioContext, AudioNodeId)>,
}
impl Drop for AudioProcessingEvent {
fn drop(&mut self) {
if let Some((context, id)) = self.registration.take() {
let wrapped = crate::message::ControlMessage::NodeMessage {
id,
msg: llq::Node::new(Box::new(self.output_buffer.clone())),
};
context.send_control_msg(wrapped);
}
}
}
#[non_exhaustive]
#[derive(Debug)]
pub struct OfflineAudioCompletionEvent {
pub rendered_buffer: AudioBuffer,
pub event: Event,
}
#[derive(Debug)]
pub(crate) enum EventPayload {
None,
RenderCapacity(AudioRenderCapacityEvent),
ProcessorError(ErrorEvent),
Diagnostics(Vec<u8>),
Message(Box<dyn Any + Send + 'static>),
AudioContextState(AudioContextState),
Complete(AudioBuffer),
AudioProcessing(AudioProcessingEvent),
}
#[derive(Debug)]
pub(crate) struct EventDispatch {
type_: EventType,
payload: EventPayload,
}
impl EventDispatch {
pub fn ended(id: AudioNodeId) -> Self {
EventDispatch {
type_: EventType::Ended(id),
payload: EventPayload::None,
}
}
pub fn sink_change() -> Self {
EventDispatch {
type_: EventType::SinkChange,
payload: EventPayload::None,
}
}
pub fn state_change(state: AudioContextState) -> Self {
EventDispatch {
type_: EventType::StateChange,
payload: EventPayload::AudioContextState(state),
}
}
pub fn render_capacity(value: AudioRenderCapacityEvent) -> Self {
EventDispatch {
type_: EventType::RenderCapacity,
payload: EventPayload::RenderCapacity(value),
}
}
pub fn processor_error(id: AudioNodeId, value: ErrorEvent) -> Self {
EventDispatch {
type_: EventType::ProcessorError(id),
payload: EventPayload::ProcessorError(value),
}
}
pub fn diagnostics(value: Vec<u8>) -> Self {
EventDispatch {
type_: EventType::Diagnostics,
payload: EventPayload::Diagnostics(value),
}
}
pub fn message(id: AudioNodeId, value: Box<dyn Any + Send + 'static>) -> Self {
EventDispatch {
type_: EventType::Message(id),
payload: EventPayload::Message(value),
}
}
pub fn complete(buffer: AudioBuffer) -> Self {
EventDispatch {
type_: EventType::Complete,
payload: EventPayload::Complete(buffer),
}
}
pub fn audio_processing(id: AudioNodeId, value: AudioProcessingEvent) -> Self {
EventDispatch {
type_: EventType::AudioProcessing(id),
payload: EventPayload::AudioProcessing(value),
}
}
}
pub(crate) enum EventHandler {
Once(Box<dyn FnOnce(EventPayload) + Send + 'static>),
Multiple(Box<dyn FnMut(EventPayload) + Send + 'static>),
}
#[derive(Clone)]
pub(crate) struct EventLoop {
event_recv: Receiver<EventDispatch>,
event_handlers: Arc<Mutex<HashMap<EventType, EventHandler>>>,
}
impl EventLoop {
pub fn new(event_recv: Receiver<EventDispatch>) -> Self {
Self {
event_recv,
event_handlers: Default::default(),
}
}
fn handle_event(&self, mut event: EventDispatch) -> ControlFlow<()> {
let mut result = ControlFlow::Continue(());
if matches!(
event.payload,
EventPayload::AudioContextState(AudioContextState::Closed)
) {
event.payload = EventPayload::None; result = ControlFlow::Break(());
}
#[cfg(test)]
if let EventPayload::ProcessorError(e) = event.payload {
panic!("Rethrowing exception during tests: {:?}", e);
}
let mut event_handler_lock = self.event_handlers.lock().unwrap();
let callback_option = event_handler_lock.remove(&event.type_);
drop(event_handler_lock);
if let Some(callback) = callback_option {
match callback {
EventHandler::Once(f) => (f)(event.payload),
EventHandler::Multiple(mut f) => {
(f)(event.payload);
self.event_handlers
.lock()
.unwrap()
.insert(event.type_, EventHandler::Multiple(f));
}
};
}
result
}
#[inline(always)]
pub fn handle_pending_events(&self) -> bool {
let mut events_were_handled = false;
for event in self.event_recv.try_iter() {
self.handle_event(event);
events_were_handled = true;
}
events_were_handled
}
pub fn run_in_thread(&self) {
log::debug!("Entering event thread");
let self_clone = self.clone();
std::thread::spawn(move || {
for event in self_clone.event_recv.iter() {
let result = self_clone.handle_event(event);
if result.is_break() {
break;
}
}
log::debug!("Event loop has terminated");
});
}
pub fn set_handler(&self, event: EventType, callback: EventHandler) {
self.event_handlers.lock().unwrap().insert(event, callback);
}
pub fn clear_handler(&self, event: EventType) {
self.event_handlers.lock().unwrap().remove(&event);
}
}