2022-01-04 02:15:28 +00:00
|
|
|
use std::sync::mpsc::{channel, Receiver, Sender};
|
2021-12-10 21:53:22 +00:00
|
|
|
use std::thread::JoinHandle;
|
|
|
|
|
|
|
|
use mps_interpreter::tokens::MpsTokenReader;
|
|
|
|
|
2022-01-04 02:15:28 +00:00
|
|
|
use super::os_controls::SystemControlWrapper;
|
|
|
|
use super::player_wrapper::{ControlAction, MpsPlayerServer, PlayerAction};
|
2021-12-10 21:53:22 +00:00
|
|
|
use super::MpsPlayer;
|
|
|
|
use super::PlaybackError;
|
|
|
|
|
2022-01-04 02:15:28 +00:00
|
|
|
/// A controller for a MpsPlayer running on another thread.
|
|
|
|
/// This receives and sends events like media buttons and script errors for the MpsPlayer.
|
2021-12-10 21:53:22 +00:00
|
|
|
pub struct MpsController {
|
|
|
|
control: Sender<ControlAction>,
|
|
|
|
event: Receiver<PlayerAction>,
|
|
|
|
handle: JoinHandle<()>,
|
2022-01-04 02:15:28 +00:00
|
|
|
sys_ctrl: SystemControlWrapper,
|
2021-12-10 21:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MpsController {
|
|
|
|
pub fn create<F: FnOnce() -> MpsPlayer<T> + Send + 'static, T: MpsTokenReader>(
|
2022-01-03 22:53:57 +00:00
|
|
|
player_gen: F,
|
2021-12-10 21:53:22 +00:00
|
|
|
) -> Self {
|
|
|
|
let (control_tx, control_rx) = channel();
|
|
|
|
let (event_tx, event_rx) = channel();
|
2022-01-05 22:37:13 +00:00
|
|
|
let (playback_tx, playback_rx) = channel();
|
2021-12-10 21:53:22 +00:00
|
|
|
let mut sys_ctrl = SystemControlWrapper::new(control_tx.clone());
|
2022-01-05 22:37:13 +00:00
|
|
|
sys_ctrl.init(playback_rx);
|
2022-01-04 02:15:28 +00:00
|
|
|
let handle =
|
2022-01-05 22:37:13 +00:00
|
|
|
MpsPlayerServer::spawn(player_gen, control_tx.clone(), control_rx, event_tx, playback_tx, false);
|
2022-01-03 22:53:57 +00:00
|
|
|
Self {
|
|
|
|
control: control_tx,
|
|
|
|
event: event_rx,
|
|
|
|
handle: handle,
|
|
|
|
sys_ctrl: sys_ctrl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_repl<F: FnOnce() -> MpsPlayer<T> + Send + 'static, T: MpsTokenReader>(
|
|
|
|
player_gen: F,
|
|
|
|
) -> Self {
|
|
|
|
let (control_tx, control_rx) = channel();
|
|
|
|
let (event_tx, event_rx) = channel();
|
2022-01-05 22:37:13 +00:00
|
|
|
let (playback_tx, playback_rx) = channel();
|
2022-01-03 22:53:57 +00:00
|
|
|
let mut sys_ctrl = SystemControlWrapper::new(control_tx.clone());
|
2022-01-05 22:37:13 +00:00
|
|
|
sys_ctrl.init(playback_rx);
|
2022-01-04 02:15:28 +00:00
|
|
|
let handle =
|
2022-01-05 22:37:13 +00:00
|
|
|
MpsPlayerServer::spawn(player_gen, control_tx.clone(), control_rx, event_tx, playback_tx, true);
|
2021-12-10 21:53:22 +00:00
|
|
|
Self {
|
|
|
|
control: control_tx,
|
|
|
|
event: event_rx,
|
|
|
|
handle: handle,
|
|
|
|
sys_ctrl: sys_ctrl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_confirm(&self, to_send: ControlAction) -> Result<(), PlaybackError> {
|
2022-01-04 02:15:28 +00:00
|
|
|
self.control
|
|
|
|
.send(to_send.clone())
|
|
|
|
.map_err(PlaybackError::from_err)?;
|
2021-12-10 21:53:22 +00:00
|
|
|
let mut response = self.event.recv().map_err(PlaybackError::from_err)?;
|
|
|
|
while !response.is_acknowledgement() {
|
2022-01-05 22:37:13 +00:00
|
|
|
self.handle_event(response)?;
|
2021-12-10 21:53:22 +00:00
|
|
|
response = self.event.recv().map_err(PlaybackError::from_err)?;
|
|
|
|
}
|
|
|
|
if let PlayerAction::Acknowledge(action) = response {
|
|
|
|
if action == to_send {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(PlaybackError {
|
2022-01-04 02:15:28 +00:00
|
|
|
msg: "Incorrect acknowledgement received for MpsController control action"
|
|
|
|
.into(),
|
2021-12-10 21:53:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(PlaybackError {
|
2022-01-04 02:15:28 +00:00
|
|
|
msg: "Invalid acknowledgement received for MpsController control action".into(),
|
2021-12-10 21:53:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 22:37:13 +00:00
|
|
|
fn handle_event(&self, event: PlayerAction) -> Result<(), PlaybackError> {
|
2021-12-10 21:53:22 +00:00
|
|
|
match event {
|
|
|
|
PlayerAction::Acknowledge(_) => Ok(()),
|
|
|
|
PlayerAction::Exception(e) => Err(e),
|
2022-01-03 22:53:57 +00:00
|
|
|
PlayerAction::End => Ok(()),
|
|
|
|
PlayerAction::Empty => Ok(()),
|
2022-01-05 22:37:13 +00:00
|
|
|
//PlayerAction::Enqueued(item) => Ok(()),
|
2021-12-10 21:53:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn next(&self) -> Result<(), PlaybackError> {
|
2022-01-04 02:15:28 +00:00
|
|
|
self.send_confirm(ControlAction::Next { ack: true })
|
2021-12-10 21:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn previous(&self) -> Result<(), PlaybackError> {
|
2022-01-04 02:15:28 +00:00
|
|
|
self.send_confirm(ControlAction::Previous { ack: true })
|
2021-12-10 21:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn play(&self) -> Result<(), PlaybackError> {
|
2022-01-04 02:15:28 +00:00
|
|
|
self.send_confirm(ControlAction::Play { ack: true })
|
2021-12-10 21:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pause(&self) -> Result<(), PlaybackError> {
|
2022-01-04 02:15:28 +00:00
|
|
|
self.send_confirm(ControlAction::Pause { ack: true })
|
2021-12-10 21:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stop(&self) -> Result<(), PlaybackError> {
|
2022-01-04 02:15:28 +00:00
|
|
|
self.send_confirm(ControlAction::Stop { ack: true })
|
2021-12-10 21:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enqueue(&self, count: usize) -> Result<(), PlaybackError> {
|
2022-01-04 02:15:28 +00:00
|
|
|
self.send_confirm(ControlAction::Enqueue {
|
|
|
|
amount: count,
|
|
|
|
ack: true,
|
|
|
|
})
|
2021-12-10 21:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ping(&self) -> Result<(), PlaybackError> {
|
2022-01-04 02:15:28 +00:00
|
|
|
self.send_confirm(ControlAction::NoOp { ack: true })
|
2021-12-10 21:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exit(self) -> Result<(), PlaybackError> {
|
2022-01-04 02:15:28 +00:00
|
|
|
self.send_confirm(ControlAction::Exit { ack: true })?;
|
2021-12-10 21:53:22 +00:00
|
|
|
self.sys_ctrl.exit();
|
|
|
|
match self.handle.join() {
|
|
|
|
Ok(x) => Ok(x),
|
|
|
|
Err(_) => Err(PlaybackError {
|
2022-01-04 02:15:28 +00:00
|
|
|
msg: "MpsPlayerServer did not exit correctly".into(),
|
|
|
|
}),
|
2021-12-10 21:53:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn wait_for_done(&self) -> Result<(), PlaybackError> {
|
|
|
|
loop {
|
|
|
|
let msg = self.event.recv().map_err(PlaybackError::from_err)?;
|
|
|
|
if let PlayerAction::End = msg {
|
|
|
|
break;
|
|
|
|
} else {
|
2022-01-05 22:37:13 +00:00
|
|
|
self.handle_event(msg)?;
|
2021-12-10 21:53:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-01-03 22:53:57 +00:00
|
|
|
|
|
|
|
pub fn wait_for_empty(&self) -> Result<(), PlaybackError> {
|
|
|
|
for msg in self.event.try_iter() {
|
2022-01-05 22:37:13 +00:00
|
|
|
self.handle_event(msg)?;
|
2022-01-03 22:53:57 +00:00
|
|
|
}
|
2022-01-04 02:15:28 +00:00
|
|
|
self.control
|
|
|
|
.send(ControlAction::CheckEmpty { ack: true })
|
|
|
|
.map_err(PlaybackError::from_err)?;
|
2022-01-03 22:53:57 +00:00
|
|
|
loop {
|
|
|
|
let msg = self.event.recv().map_err(PlaybackError::from_err)?;
|
|
|
|
if let PlayerAction::Empty = msg {
|
|
|
|
break;
|
|
|
|
} else {
|
2022-01-05 22:37:13 +00:00
|
|
|
self.handle_event(msg)?;
|
2022-01-03 22:53:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check for any errors in the event queue.
|
|
|
|
/// This is non-blocking, so it only handles events sent since the last time events were handled.
|
|
|
|
pub fn check(&self) -> Vec<PlaybackError> {
|
|
|
|
let mut result = Vec::new();
|
|
|
|
for msg in self.event.try_iter() {
|
2022-01-05 22:37:13 +00:00
|
|
|
if let Err(e) = self.handle_event(msg) {
|
2022-01-03 22:53:57 +00:00
|
|
|
result.push(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like check(), but it also waits for an acknowledgement to ensure it gets the latest events.
|
|
|
|
pub fn check_ack(&self) -> Vec<PlaybackError> {
|
2022-01-05 22:37:13 +00:00
|
|
|
let mut result = self.check(); // clear existing messages first
|
2022-01-04 02:15:28 +00:00
|
|
|
let to_send = ControlAction::NoOp { ack: true };
|
|
|
|
if let Err(e) = self
|
|
|
|
.control
|
|
|
|
.send(to_send.clone())
|
|
|
|
.map_err(PlaybackError::from_err)
|
|
|
|
{
|
2022-01-03 22:53:57 +00:00
|
|
|
result.push(e);
|
|
|
|
}
|
|
|
|
for msg in self.event.iter() {
|
|
|
|
if let PlayerAction::Acknowledge(action) = msg {
|
|
|
|
if action == to_send {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
result.push(PlaybackError {
|
2022-01-04 02:15:28 +00:00
|
|
|
msg: "Incorrect acknowledgement received for MpsController control action"
|
|
|
|
.into(),
|
2022-01-03 22:53:57 +00:00
|
|
|
});
|
|
|
|
}
|
2022-01-05 22:37:13 +00:00
|
|
|
} else if let Err(e) = self.handle_event(msg) {
|
2022-01-03 22:53:57 +00:00
|
|
|
result.push(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
2021-12-10 21:53:22 +00:00
|
|
|
}
|