use alux_ext::ext;
use alux_sdk::trait_algebra;
use rsynko_media::{FormatPredicateAlg, FormatPredicateExt, FormatSelectionAlg};
pub trait RequestOptionsAlg {
fn performer(&self) -> Performer;
fn media_streams(&self) -> Option<MediaStreams>;
fn chosen_choice(&self) -> Option<&str>;
fn selectable_choices(&self) -> impl Iterator<Item = &str>;
fn choice_summary(&self, choice: &str) -> Option<&str>;
}
#[trait_algebra(derive(Clone, Debug, PartialEq, Eq))]
pub trait FormatDiscovery {
type Format;
fn started(&self);
fn available(&self, formats: Vec<Self::Format>);
fn failed(&self, message: String);
}
#[ext(name = RequestSelectionExt)]
pub impl<This> This
where
This: FormatPredicateAlg + FormatSelectionAlg,
{
fn request_selection(&self, options: &impl RequestOptionsAlg) -> This::Selection {
let predicate = match options.chosen_choice() {
Some(choice) => self.format_id(choice),
None => self.stream_role_format(options.media_streams().unwrap_or(MediaStreams::AudioVideo)),
};
self.best_format(predicate)
}
}
#[ext(name = MediaStreamsExt)]
pub impl<This> This
where
This: FormatPredicateAlg,
{
fn stream_role_format(&self, streams: MediaStreams) -> This::Predicate {
match streams {
MediaStreams::AudioVideo => self.progressive_format(),
MediaStreams::Audio => self.audio_only_format(),
MediaStreams::Video => self.video_only_format(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Performer {
Program,
Retrieval,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MediaStreams {
AudioVideo,
Audio,
Video,
}
impl MediaStreams {
pub const OFFERED: [Self; 3] = [Self::AudioVideo, Self::Video, Self::Audio];
}