librespot/src/player_event_handler.rs

86 lines
3.1 KiB
Rust
Raw Normal View History

2018-02-20 20:57:42 +00:00
use librespot::playback::player::PlayerEvent;
2020-01-17 17:11:07 +00:00
use log::info;
2018-02-26 01:50:41 +00:00
use std::collections::HashMap;
use std::io;
2018-10-16 09:32:17 +00:00
use std::process::Command;
use tokio_process::{Child, CommandExt};
2020-03-10 12:26:01 +00:00
use futures::Future;
use librespot::playback::player::SinkStatus;
fn run_program(program: &str, env_vars: HashMap<&str, String>) -> io::Result<Child> {
let mut v: Vec<&str> = program.split_whitespace().collect();
info!("Running {:?} with environment variables {:?}", v, env_vars);
Command::new(&v.remove(0))
.args(&v)
.envs(env_vars.iter())
2018-10-16 09:32:17 +00:00
.spawn_async()
}
pub fn run_program_on_events(event: PlayerEvent, onevent: &str) -> Option<io::Result<Child>> {
let mut env_vars = HashMap::new();
match event {
2018-02-26 01:50:41 +00:00
PlayerEvent::Changed {
old_track_id,
new_track_id,
} => {
2020-09-25 14:11:30 +00:00
env_vars.insert("PLAYER_EVENT", "changed".to_string());
env_vars.insert("OLD_TRACK_ID", old_track_id.to_base62());
env_vars.insert("TRACK_ID", new_track_id.to_base62());
2018-02-26 01:50:41 +00:00
}
PlayerEvent::Started { track_id, .. } => {
2020-09-25 14:11:30 +00:00
env_vars.insert("PLAYER_EVENT", "started".to_string());
env_vars.insert("TRACK_ID", track_id.to_base62());
}
PlayerEvent::Stopped { track_id, .. } => {
2020-09-25 14:11:30 +00:00
env_vars.insert("PLAYER_EVENT", "stopped".to_string());
env_vars.insert("TRACK_ID", track_id.to_base62());
}
PlayerEvent::Playing {
2020-03-20 06:31:18 +00:00
track_id,
duration_ms,
position_ms,
..
} => {
env_vars.insert("PLAYER_EVENT", "playing".to_string());
2020-03-20 06:31:18 +00:00
env_vars.insert("TRACK_ID", track_id.to_base62());
env_vars.insert("DURATION_MS", duration_ms.to_string());
env_vars.insert("POSITION_MS", position_ms.to_string());
}
PlayerEvent::Paused {
2020-03-20 06:31:18 +00:00
track_id,
duration_ms,
position_ms,
..
} => {
env_vars.insert("PLAYER_EVENT", "paused".to_string());
2020-03-20 06:31:18 +00:00
env_vars.insert("TRACK_ID", track_id.to_base62());
env_vars.insert("DURATION_MS", duration_ms.to_string());
env_vars.insert("POSITION_MS", position_ms.to_string());
}
PlayerEvent::Preloading { track_id, .. } => {
env_vars.insert("PLAYER_EVENT", "preloading".to_string());
env_vars.insert("TRACK_ID", track_id.to_base62());
}
PlayerEvent::VolumeSet { volume } => {
env_vars.insert("PLAYER_EVENT", "volume_set".to_string());
env_vars.insert("VOLUME", volume.to_string());
}
_ => return None,
}
Some(run_program(onevent, env_vars))
}
2020-03-10 12:26:01 +00:00
pub fn emit_sink_event(sink_status: SinkStatus, onevent: &str) {
let mut env_vars = HashMap::new();
env_vars.insert("PLAYER_EVENT", "sink".to_string());
let sink_status = match sink_status {
SinkStatus::Running => "running",
SinkStatus::TemporarilyClosed => "temporarily_closed",
SinkStatus::Closed => "closed",
};
env_vars.insert("SINK_STATUS", sink_status.to_string());
let _ = run_program(onevent, env_vars).and_then(|child| child.wait());
}