use log::{info,error,warn,trace,debug};
use crate::macros::{
ok_debug,
recv,
send,
};
use crate::collection::{
Collection,
Keychain,
ArtistKey,
AlbumKey,
SongKey,
};
use crate::kernel::State;
use std::sync::Arc;
use rolock::RoLock;
use super::msg::{
AudioToKernel,
KernelToAudio,
};
use crossbeam_channel::{Sender,Receiver};
pub(crate) struct Audio {
collection: Arc<Collection>, state: RoLock<State>, to_kernel: Sender<AudioToKernel>, from_kernel: Receiver<KernelToAudio>, }
impl Audio {
#[inline(always)]
pub(crate) fn init(
collection: Arc<Collection>,
state: RoLock<State>,
to_kernel: Sender<AudioToKernel>,
from_kernel: Receiver<KernelToAudio>,
) {
let audio = Self {
collection,
state,
to_kernel,
from_kernel,
};
Self::main(audio);
}
}
const AUDIO_MESSAGE_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(15);
impl Audio {
#[inline(always)]
fn main(mut self) {
ok_debug!("Audio");
loop {
self = self.msg();
}
}
#[inline(always)]
fn msg(mut self) -> Self {
let msg = match self.from_kernel.recv_timeout(AUDIO_MESSAGE_TIMEOUT) {
Ok(msg) => msg,
_ => return self,
};
use KernelToAudio::*;
match msg {
Play => todo!(),
Stop => todo!(),
Next => todo!(),
Last => todo!(),
Seek(f64) => todo!(),
Volume(f64) => todo!(),
PlayQueueKey(queue_key) => todo!(),
DropCollection => self = self.msg_drop(),
NewCollection(arc) => self.collection = arc,
NewState(rolock) => self.state = rolock,
}
self
}
#[inline(always)]
fn msg_drop(mut self) -> Self {
drop(self.collection);
debug!("Audio: Dropped Collection, waiting...");
loop {
if let KernelToAudio::NewCollection(arc) = recv!(self.from_kernel) {
ok_debug!("Audio: New Collection");
self.collection = arc;
return self
}
error!("Audio: Incorrect message received");
}
}
}