use std::{
cell::RefCell,
rc::Rc,
sync::{Arc, Weak},
};
use super::{Command, Media, MediaError, MediaInfo, Shared};
thread_local! {
static DRIVEN: RefCell<Option<Rc<RefCell<Vec<Arc<Shared>>>>>> = const { RefCell::new(None) };
static LOOSE: RefCell<Vec<Weak<Shared>>> = const { RefCell::new(Vec::new()) };
}
#[doc(hidden)]
pub struct DrivenMedias {
opened: Rc<RefCell<Vec<Arc<Shared>>>>,
}
#[doc(hidden)]
pub fn drive_media() -> DrivenMedias {
let opened = Rc::new(RefCell::new(Vec::new()));
DRIVEN.with(|driven| *driven.borrow_mut() = Some(opened.clone()));
DrivenMedias { opened }
}
impl Drop for DrivenMedias {
fn drop(&mut self) {
DRIVEN.with(|driven| *driven.borrow_mut() = None);
}
}
impl DrivenMedias {
pub fn count(&self) -> usize {
self.opened.borrow().len()
}
pub fn media(&self, index: usize) -> MediaDriver {
MediaDriver(self.opened.borrow()[index].clone())
}
pub fn last(&self) -> MediaDriver {
MediaDriver(self.opened.borrow().last().expect("no media was opened").clone())
}
}
pub(super) fn take(shared: &Arc<Shared>) -> bool {
DRIVEN.with(|driven| match &*driven.borrow() {
Some(opened) => {
opened.borrow_mut().push(shared.clone());
true
}
None => false,
})
}
#[doc(hidden)]
#[derive(Clone)]
pub struct MediaDriver(Arc<Shared>);
impl MediaDriver {
pub fn open(&self, info: MediaInfo) {
{
let mut inner = self.0.inner();
inner.duration = info.duration;
inner.size = info.size;
inner.sound = info.sound;
inner.opened = true;
inner.waiting = false;
inner.hold();
}
self.0.wake();
}
pub fn wait(&self, waiting: bool) {
{
let mut inner = self.0.inner();
inner.hold();
inner.waiting = waiting;
}
self.0.wake();
}
pub fn present(&self) {
self.0.presented();
}
pub fn fetch(&self, to: Option<f64>) {
self.0.inner().fetched = to;
self.0.wake();
}
pub fn end(&self) {
{
let mut inner = self.0.inner();
inner.hold();
if inner.looped && inner.playing {
inner.at = 0.0;
inner.rounds += 1;
} else {
if let Some(duration) = inner.duration {
inner.at = duration;
}
inner.ended = true;
inner.playing = false;
}
}
self.0.wake();
}
pub fn fail(&self, why: MediaError) {
self.0.fail(why);
}
pub fn pending(&self) -> usize {
self.0.take_driven(false).len()
}
pub fn playing(&self) -> bool {
self.0.inner().playing
}
}
impl Media {
#[doc(hidden)]
pub fn driven() -> (Self, MediaDriver) {
let shared = Shared::new();
LOOSE.with(|loose| loose.borrow_mut().push(Arc::downgrade(&shared)));
(Media::of(shared.clone()), MediaDriver(shared))
}
}
pub(super) fn answer() {
let opened: Vec<Arc<Shared>> = DRIVEN.with(|driven| {
driven
.borrow()
.as_ref()
.map(|opened| opened.borrow().clone())
.unwrap_or_default()
});
let loose: Vec<Arc<Shared>> = LOOSE.with(|loose| {
let mut loose = loose.borrow_mut();
loose.retain(|shared| shared.strong_count() > 0);
loose.iter().filter_map(Weak::upgrade).collect()
});
for shared in opened.iter().chain(&loose) {
answer_one(shared);
}
}
fn answer_one(shared: &Shared) {
if !shared.inner().opened && shared.inner().error.is_none() {
return;
}
let commands = shared.take_driven(true);
if commands.is_empty() {
return;
}
{
let mut inner = shared.inner();
for command in commands {
match command {
Command::Play if inner.error.is_none() => {
if inner.ended {
inner.ended = false;
inner.at = 0.0;
}
inner.hold();
inner.playing = true;
}
Command::Pause => {
inner.hold();
inner.playing = false;
}
Command::Seek(to) => {
inner.at = to;
inner.ended = false;
}
Command::Rate(rate) => {
inner.hold();
inner.rate = rate;
}
_ => {}
}
}
}
shared.wake();
}