use crate::{
audio::runner::Control as AudioControl, common::errors::MyError,
pipeline::runner::Control as PipelineControl,
};
use crossbeam_channel::{select, Receiver, Sender};
#[derive(Debug, PartialEq)]
pub enum Control {
PauseContinue,
Replay,
Exit,
MuteUnmute,
SetCharMap(u32),
Resize(u16, u16),
SetGrayscale(bool),
Seek(f64),
CycleSubtitle,
ToggleSubtitle,
AdjustSpeed(f64),
ResetSpeed,
}
type BrokerControl = Control;
pub struct MessageBroker {
rx_channel_terminal: Receiver<BrokerControl>,
tx_channel_pipeline: Option<Sender<PipelineControl>>,
tx_channel_audio: Option<Sender<AudioControl>>,
}
impl MessageBroker {
pub fn new(
rx_channel_terminal: Receiver<BrokerControl>,
tx_channel_pipeline: Option<Sender<PipelineControl>>,
tx_channel_audio: Option<Sender<AudioControl>>,
) -> Self {
Self {
rx_channel_terminal,
tx_channel_pipeline,
tx_channel_audio,
}
}
pub fn run(&mut self, barrier: std::sync::Arc<std::sync::Barrier>) -> Result<(), MyError> {
barrier.wait();
let mut running = true;
while running || !self.rx_channel_terminal.is_empty() {
select! {
recv(self.rx_channel_terminal) -> msg => {
match msg {
Ok(BrokerControl::Exit) => {
running = false;
if let Some(tx) = &self.tx_channel_pipeline {
let _ = tx.send(PipelineControl::Exit);
}
if let Some(tx) = &self.tx_channel_audio{
let _ = tx.send(AudioControl::Exit);
}
}
Ok(BrokerControl::PauseContinue) => {
if let Some(tx) = &self.tx_channel_pipeline {
let _ = tx.send(PipelineControl::PauseContinue);
}
if let Some(tx) = &self.tx_channel_audio {
let _ = tx.send(AudioControl::PauseContinue);
}
}
Ok(BrokerControl::Replay) => {
if let Some(tx) = &self.tx_channel_pipeline {
let _ = tx.send(PipelineControl::Replay);
}
if let Some(tx) = &self.tx_channel_audio {
let _ = tx.send(AudioControl::Replay);
}
}
Ok(BrokerControl::Resize(width, height)) => {
if let Some(tx) = &self.tx_channel_pipeline {
let _ = tx.send(PipelineControl::Resize(width, height));
}
}
Ok(BrokerControl::SetCharMap(char_map)) => {
if let Some(tx) = &self.tx_channel_pipeline {
let _ = tx.send(PipelineControl::SetCharMap(char_map));
}
}
Ok(BrokerControl::SetGrayscale(grayscale)) => {
if let Some(tx) = &self.tx_channel_pipeline {
let _ = tx.send(PipelineControl::SetGrayscale(grayscale));
}
}
Ok(BrokerControl::MuteUnmute) => {
if let Some(tx) = &self.tx_channel_audio {
let _ = tx.send(AudioControl::MuteUnmute);
}
}
Ok(BrokerControl::Seek(seconds)) => {
if let Some(tx) = &self.tx_channel_pipeline {
let _ = tx.send(PipelineControl::Seek(seconds));
}
if let Some(tx) = &self.tx_channel_audio {
let _ = tx.send(AudioControl::Seek(seconds));
}
}
Ok(BrokerControl::CycleSubtitle) => {
if let Some(tx) = &self.tx_channel_audio {
let _ = tx.send(AudioControl::CycleSubtitle);
}
}
Ok(BrokerControl::ToggleSubtitle) => {
if let Some(tx) = &self.tx_channel_audio {
let _ = tx.send(AudioControl::ToggleSubtitle);
}
}
Ok(BrokerControl::AdjustSpeed(delta)) => {
if let Some(tx) = &self.tx_channel_audio {
let _ = tx.send(AudioControl::AdjustSpeed(delta));
}
}
Ok(BrokerControl::ResetSpeed) => {
if let Some(tx) = &self.tx_channel_audio {
let _ = tx.send(AudioControl::ResetSpeed);
}
}
Err(_) => {
}
}
}
}
}
Ok(())
}
}