WIP: use new async mpvipc
This commit is contained in:
parent
18d186b997
commit
b16924ec08
File diff suppressed because it is too large
Load Diff
|
@ -11,7 +11,7 @@ axum = { version = "0.6.20", features = ["macros"] }
|
||||||
clap = { version = "4.4.1", features = ["derive"] }
|
clap = { version = "4.4.1", features = ["derive"] }
|
||||||
env_logger = "0.10.0"
|
env_logger = "0.10.0"
|
||||||
log = "0.4.20"
|
log = "0.4.20"
|
||||||
mpvipc = "1.3.0"
|
mpvipc = { git = "https://git.pvv.ntnu.no/oysteikt/mpvipc.git" }
|
||||||
serde = { version = "1.0.188", features = ["derive"] }
|
serde = { version = "1.0.188", features = ["derive"] }
|
||||||
serde_json = "1.0.105"
|
serde_json = "1.0.105"
|
||||||
tokio = { version = "1.32.0", features = ["full"] }
|
tokio = { version = "1.32.0", features = ["full"] }
|
||||||
|
|
153
src/api/base.rs
153
src/api/base.rs
|
@ -1,61 +1,56 @@
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use log::trace;
|
|
||||||
use mpvipc::{
|
use mpvipc::{
|
||||||
Mpv, NumberChangeOptions, PlaylistAddOptions, PlaylistAddTypeOptions, SeekOptions, Switch,
|
Mpv, NumberChangeOptions, PlaylistAddOptions, PlaylistAddTypeOptions, SeekOptions, Switch,
|
||||||
};
|
};
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
use tokio::sync::Mutex;
|
|
||||||
|
|
||||||
/// Add item to playlist
|
/// Add item to playlist
|
||||||
pub async fn loadfile(mpv: Arc<Mutex<Mpv>>, path: &str) -> anyhow::Result<()> {
|
pub async fn loadfile(mpv: Mpv, path: &str) -> anyhow::Result<()> {
|
||||||
trace!("api::loadfile({:?})", path);
|
log::trace!("api::loadfile({:?})", path);
|
||||||
mpv.lock().await.playlist_add(
|
mpv.playlist_add(
|
||||||
path,
|
path,
|
||||||
PlaylistAddTypeOptions::File,
|
PlaylistAddTypeOptions::File,
|
||||||
PlaylistAddOptions::Append,
|
PlaylistAddOptions::Append,
|
||||||
)?;
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check whether the player is paused or playing
|
/// Check whether the player is paused or playing
|
||||||
pub async fn play_get(mpv: Arc<Mutex<Mpv>>) -> anyhow::Result<Value> {
|
pub async fn play_get(mpv: Mpv) -> anyhow::Result<Value> {
|
||||||
trace!("api::play_get()");
|
log::trace!("api::play_get()");
|
||||||
let paused: bool = mpv.lock().await.get_property("pause")?;
|
let paused: bool = mpv.get_property("pause").await?;
|
||||||
Ok(json!(!paused))
|
Ok(json!(!paused))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set whether the player is paused or playing
|
/// Set whether the player is paused or playing
|
||||||
pub async fn play_set(mpv: Arc<Mutex<Mpv>>, should_play: bool) -> anyhow::Result<()> {
|
pub async fn play_set(mpv: Mpv, should_play: bool) -> anyhow::Result<()> {
|
||||||
trace!("api::play_set({:?})", should_play);
|
log::trace!("api::play_set({:?})", should_play);
|
||||||
mpv.lock()
|
mpv.set_property("pause", if should_play { "no" } else { "yes" })
|
||||||
.await
|
.await
|
||||||
.set_property("pause", !should_play)
|
|
||||||
.map_err(|e| e.into())
|
.map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the current player volume
|
/// Get the current player volume
|
||||||
pub async fn volume_get(mpv: Arc<Mutex<Mpv>>) -> anyhow::Result<Value> {
|
pub async fn volume_get(mpv: Mpv) -> anyhow::Result<Value> {
|
||||||
trace!("api::volume_get()");
|
log::trace!("api::volume_get()");
|
||||||
let volume: f64 = mpv.lock().await.get_property("volume")?;
|
let volume: f64 = mpv.get_property("volume").await?;
|
||||||
Ok(json!(volume))
|
Ok(json!(volume))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the player volume
|
/// Set the player volume
|
||||||
pub async fn volume_set(mpv: Arc<Mutex<Mpv>>, value: f64) -> anyhow::Result<()> {
|
pub async fn volume_set(mpv: Mpv, value: f64) -> anyhow::Result<()> {
|
||||||
trace!("api::volume_set({:?})", value);
|
log::trace!("api::volume_set({:?})", value);
|
||||||
mpv.lock()
|
mpv.set_volume(value, NumberChangeOptions::Absolute)
|
||||||
.await
|
.await
|
||||||
.set_volume(value, NumberChangeOptions::Absolute)
|
|
||||||
.map_err(|e| e.into())
|
.map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get current playback position
|
/// Get current playback position
|
||||||
pub async fn time_get(mpv: Arc<Mutex<Mpv>>) -> anyhow::Result<Value> {
|
pub async fn time_get(mpv: Mpv) -> anyhow::Result<Value> {
|
||||||
trace!("api::time_get()");
|
log::trace!("api::time_get()");
|
||||||
let current: f64 = mpv.lock().await.get_property("time-pos")?;
|
let current: f64 = mpv.get_property("time-pos").await?;
|
||||||
let remaining: f64 = mpv.lock().await.get_property("time-remaining")?;
|
let remaining: f64 = mpv.get_property("time-remaining").await?;
|
||||||
let total = current + remaining;
|
let total = current + remaining;
|
||||||
|
|
||||||
Ok(json!({
|
Ok(json!({
|
||||||
|
@ -66,22 +61,16 @@ pub async fn time_get(mpv: Arc<Mutex<Mpv>>) -> anyhow::Result<Value> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set playback position
|
/// Set playback position
|
||||||
pub async fn time_set(
|
pub async fn time_set(mpv: Mpv, pos: Option<f64>, percent: Option<f64>) -> anyhow::Result<()> {
|
||||||
mpv: Arc<Mutex<Mpv>>,
|
log::trace!("api::time_set({:?}, {:?})", pos, percent);
|
||||||
pos: Option<f64>,
|
|
||||||
percent: Option<f64>,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
trace!("api::time_set({:?}, {:?})", pos, percent);
|
|
||||||
if pos.is_some() && percent.is_some() {
|
if pos.is_some() && percent.is_some() {
|
||||||
anyhow::bail!("pos and percent cannot be provided at the same time");
|
anyhow::bail!("pos and percent cannot be provided at the same time");
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(pos) = pos {
|
if let Some(pos) = pos {
|
||||||
mpv.lock().await.seek(pos, SeekOptions::Absolute)?;
|
mpv.seek(pos, SeekOptions::Absolute).await?;
|
||||||
} else if let Some(percent) = percent {
|
} else if let Some(percent) = percent {
|
||||||
mpv.lock()
|
mpv.seek(percent, SeekOptions::AbsolutePercent).await?;
|
||||||
.await
|
|
||||||
.seek(percent, SeekOptions::AbsolutePercent)?;
|
|
||||||
} else {
|
} else {
|
||||||
anyhow::bail!("Either pos or percent must be provided");
|
anyhow::bail!("Either pos or percent must be provided");
|
||||||
};
|
};
|
||||||
|
@ -90,10 +79,10 @@ pub async fn time_set(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the current playlist
|
/// Get the current playlist
|
||||||
pub async fn playlist_get(mpv: Arc<Mutex<Mpv>>) -> anyhow::Result<Value> {
|
pub async fn playlist_get(mpv: Mpv) -> anyhow::Result<Value> {
|
||||||
trace!("api::playlist_get()");
|
log::trace!("api::playlist_get()");
|
||||||
let playlist: mpvipc::Playlist = mpv.lock().await.get_playlist()?;
|
let playlist: mpvipc::Playlist = mpv.get_playlist().await?;
|
||||||
let is_playing: bool = mpv.lock().await.get_property("pause")?;
|
let is_playing: bool = mpv.get_property("pause").await?;
|
||||||
|
|
||||||
let items: Vec<Value> = playlist
|
let items: Vec<Value> = playlist
|
||||||
.0
|
.0
|
||||||
|
@ -116,74 +105,64 @@ pub async fn playlist_get(mpv: Arc<Mutex<Mpv>>) -> anyhow::Result<Value> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Skip to the next item in the playlist
|
/// Skip to the next item in the playlist
|
||||||
pub async fn playlist_next(mpv: Arc<Mutex<Mpv>>) -> anyhow::Result<()> {
|
pub async fn playlist_next(mpv: Mpv) -> anyhow::Result<()> {
|
||||||
trace!("api::playlist_next()");
|
log::trace!("api::playlist_next()");
|
||||||
mpv.lock().await.next().map_err(|e| e.into())
|
mpv.next().await.map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Go back to the previous item in the playlist
|
/// Go back to the previous item in the playlist
|
||||||
pub async fn playlist_previous(mpv: Arc<Mutex<Mpv>>) -> anyhow::Result<()> {
|
pub async fn playlist_previous(mpv: Mpv) -> anyhow::Result<()> {
|
||||||
trace!("api::playlist_previous()");
|
log::trace!("api::playlist_previous()");
|
||||||
mpv.lock().await.prev().map_err(|e| e.into())
|
mpv.prev().await.map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Go chosen item in the playlist
|
/// Go chosen item in the playlist
|
||||||
pub async fn playlist_goto(mpv: Arc<Mutex<Mpv>>, index: usize) -> anyhow::Result<()> {
|
pub async fn playlist_goto(mpv: Mpv, index: usize) -> anyhow::Result<()> {
|
||||||
trace!("api::playlist_goto({:?})", index);
|
log::trace!("api::playlist_goto({:?})", index);
|
||||||
mpv.lock()
|
mpv.playlist_play_id(index).await.map_err(|e| e.into())
|
||||||
.await
|
|
||||||
.playlist_play_id(index)
|
|
||||||
.map_err(|e| e.into())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clears the playlist
|
/// Clears the playlist
|
||||||
pub async fn playlist_clear(mpv: Arc<Mutex<Mpv>>) -> anyhow::Result<()> {
|
pub async fn playlist_clear(mpv: Mpv) -> anyhow::Result<()> {
|
||||||
trace!("api::playlist_clear()");
|
log::trace!("api::playlist_clear()");
|
||||||
mpv.lock().await.playlist_clear().map_err(|e| e.into())
|
mpv.playlist_clear().await.map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove an item from the playlist by index
|
/// Remove an item from the playlist by index
|
||||||
pub async fn playlist_remove(mpv: Arc<Mutex<Mpv>>, index: usize) -> anyhow::Result<()> {
|
pub async fn playlist_remove(mpv: Mpv, index: usize) -> anyhow::Result<()> {
|
||||||
trace!("api::playlist_remove({:?})", index);
|
log::trace!("api::playlist_remove({:?})", index);
|
||||||
mpv.lock()
|
mpv.playlist_remove_id(index).await.map_err(|e| e.into())
|
||||||
.await
|
|
||||||
.playlist_remove_id(index)
|
|
||||||
.map_err(|e| e.into())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Move an item in the playlist from one index to another
|
/// Move an item in the playlist from one index to another
|
||||||
pub async fn playlist_move(mpv: Arc<Mutex<Mpv>>, from: usize, to: usize) -> anyhow::Result<()> {
|
pub async fn playlist_move(mpv: Mpv, from: usize, to: usize) -> anyhow::Result<()> {
|
||||||
trace!("api::playlist_move({:?}, {:?})", from, to);
|
log::trace!("api::playlist_move({:?}, {:?})", from, to);
|
||||||
mpv.lock()
|
mpv.playlist_move_id(from, to).await.map_err(|e| e.into())
|
||||||
.await
|
|
||||||
.playlist_move_id(from, to)
|
|
||||||
.map_err(|e| e.into())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shuffle the playlist
|
/// Shuffle the playlist
|
||||||
pub async fn shuffle(mpv: Arc<Mutex<Mpv>>) -> anyhow::Result<()> {
|
pub async fn shuffle(mpv: Mpv) -> anyhow::Result<()> {
|
||||||
trace!("api::shuffle()");
|
log::trace!("api::shuffle()");
|
||||||
mpv.lock().await.playlist_shuffle().map_err(|e| e.into())
|
mpv.playlist_shuffle().await.map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// See whether it loops the playlist or not
|
/// See whether it loops the playlist or not
|
||||||
pub async fn playlist_get_looping(mpv: Arc<Mutex<Mpv>>) -> anyhow::Result<Value> {
|
pub async fn playlist_get_looping(mpv: Mpv) -> anyhow::Result<Value> {
|
||||||
trace!("api::playlist_get_looping()");
|
log::trace!("api::playlist_get_looping()");
|
||||||
let loop_playlist = mpv.lock().await.get_property_string("loop-playlist")? == "inf";
|
|
||||||
Ok(json!(loop_playlist))
|
let loop_status = match mpv.get_property_value("loop-playlist").await? {
|
||||||
|
Value::String(s) => s == "inf",
|
||||||
|
Value::Bool(b) => b,
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(json!(loop_status))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn playlist_set_looping(mpv: Arc<Mutex<Mpv>>, r#loop: bool) -> anyhow::Result<()> {
|
pub async fn playlist_set_looping(mpv: Mpv, r#loop: bool) -> anyhow::Result<()> {
|
||||||
trace!("api::playlist_set_looping({:?})", r#loop);
|
log::trace!("api::playlist_set_looping({:?})", r#loop);
|
||||||
if r#loop {
|
|
||||||
mpv.lock()
|
mpv.set_loop_playlist(if r#loop { Switch::On } else { Switch::Off })
|
||||||
.await
|
.await
|
||||||
.set_loop_playlist(Switch::On)
|
.map_err(|e| e.into())
|
||||||
.map_err(|e| e.into())
|
|
||||||
} else {
|
|
||||||
mpv.lock()
|
|
||||||
.await
|
|
||||||
.set_loop_playlist(Switch::Off)
|
|
||||||
.map_err(|e| e.into())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
|
debug_handler,
|
||||||
extract::{Query, State},
|
extract::{Query, State},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
|
@ -9,11 +8,10 @@ use axum::{
|
||||||
};
|
};
|
||||||
use mpvipc::Mpv;
|
use mpvipc::Mpv;
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
use tokio::sync::Mutex;
|
|
||||||
|
|
||||||
use super::base;
|
use super::base;
|
||||||
|
|
||||||
pub fn rest_api_routes(mpv: Arc<Mutex<Mpv>>) -> Router {
|
pub fn rest_api_routes(mpv: Mpv) -> Router {
|
||||||
Router::new()
|
Router::new()
|
||||||
.route("/load", post(loadfile))
|
.route("/load", post(loadfile))
|
||||||
.route("/play", get(play_get))
|
.route("/play", get(play_get))
|
||||||
|
@ -54,7 +52,7 @@ impl IntoResponse for RestResponse {
|
||||||
Ok(value) => (StatusCode::OK, Json(value)).into_response(),
|
Ok(value) => (StatusCode::OK, Json(value)).into_response(),
|
||||||
Err(err) => (
|
Err(err) => (
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
Json(json!({ "error": err.to_string(), "success": false })),
|
Json(json!({ "error": err.to_string(), "errortext": err.to_string(), "success": false })),
|
||||||
)
|
)
|
||||||
.into_response(),
|
.into_response(),
|
||||||
}
|
}
|
||||||
|
@ -72,14 +70,12 @@ struct LoadFileArgs {
|
||||||
path: String,
|
path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn loadfile(
|
#[debug_handler]
|
||||||
State(mpv): State<Arc<Mutex<Mpv>>>,
|
async fn loadfile(State(mpv): State<Mpv>, Query(query): Query<LoadFileArgs>) -> RestResponse {
|
||||||
Query(query): Query<LoadFileArgs>,
|
|
||||||
) -> RestResponse {
|
|
||||||
base::loadfile(mpv, &query.path).await.into()
|
base::loadfile(mpv, &query.path).await.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn play_get(State(mpv): State<Arc<Mutex<Mpv>>>) -> RestResponse {
|
async fn play_get(State(mpv): State<Mpv>) -> RestResponse {
|
||||||
base::play_get(mpv).await.into()
|
base::play_get(mpv).await.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,15 +84,12 @@ struct PlaySetArgs {
|
||||||
play: String,
|
play: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn play_set(
|
async fn play_set(State(mpv): State<Mpv>, Query(query): Query<PlaySetArgs>) -> RestResponse {
|
||||||
State(mpv): State<Arc<Mutex<Mpv>>>,
|
|
||||||
Query(query): Query<PlaySetArgs>,
|
|
||||||
) -> RestResponse {
|
|
||||||
let play = query.play.to_lowercase() == "true";
|
let play = query.play.to_lowercase() == "true";
|
||||||
base::play_set(mpv, play).await.into()
|
base::play_set(mpv, play).await.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn volume_get(State(mpv): State<Arc<Mutex<Mpv>>>) -> RestResponse {
|
async fn volume_get(State(mpv): State<Mpv>) -> RestResponse {
|
||||||
base::volume_get(mpv).await.into()
|
base::volume_get(mpv).await.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,14 +98,11 @@ struct VolumeSetArgs {
|
||||||
volume: f64,
|
volume: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn volume_set(
|
async fn volume_set(State(mpv): State<Mpv>, Query(query): Query<VolumeSetArgs>) -> RestResponse {
|
||||||
State(mpv): State<Arc<Mutex<Mpv>>>,
|
|
||||||
Query(query): Query<VolumeSetArgs>,
|
|
||||||
) -> RestResponse {
|
|
||||||
base::volume_set(mpv, query.volume).await.into()
|
base::volume_set(mpv, query.volume).await.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn time_get(State(mpv): State<Arc<Mutex<Mpv>>>) -> RestResponse {
|
async fn time_get(State(mpv): State<Mpv>) -> RestResponse {
|
||||||
base::time_get(mpv).await.into()
|
base::time_get(mpv).await.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,22 +112,19 @@ struct TimeSetArgs {
|
||||||
percent: Option<f64>,
|
percent: Option<f64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn time_set(
|
async fn time_set(State(mpv): State<Mpv>, Query(query): Query<TimeSetArgs>) -> RestResponse {
|
||||||
State(mpv): State<Arc<Mutex<Mpv>>>,
|
|
||||||
Query(query): Query<TimeSetArgs>,
|
|
||||||
) -> RestResponse {
|
|
||||||
base::time_set(mpv, query.pos, query.percent).await.into()
|
base::time_set(mpv, query.pos, query.percent).await.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn playlist_get(State(mpv): State<Arc<Mutex<Mpv>>>) -> RestResponse {
|
async fn playlist_get(State(mpv): State<Mpv>) -> RestResponse {
|
||||||
base::playlist_get(mpv).await.into()
|
base::playlist_get(mpv).await.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn playlist_next(State(mpv): State<Arc<Mutex<Mpv>>>) -> RestResponse {
|
async fn playlist_next(State(mpv): State<Mpv>) -> RestResponse {
|
||||||
base::playlist_next(mpv).await.into()
|
base::playlist_next(mpv).await.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn playlist_previous(State(mpv): State<Arc<Mutex<Mpv>>>) -> RestResponse {
|
async fn playlist_previous(State(mpv): State<Mpv>) -> RestResponse {
|
||||||
base::playlist_previous(mpv).await.into()
|
base::playlist_previous(mpv).await.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,7 +134,7 @@ struct PlaylistGotoArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn playlist_goto(
|
async fn playlist_goto(
|
||||||
State(mpv): State<Arc<Mutex<Mpv>>>,
|
State(mpv): State<Mpv>,
|
||||||
Query(query): Query<PlaylistGotoArgs>,
|
Query(query): Query<PlaylistGotoArgs>,
|
||||||
) -> RestResponse {
|
) -> RestResponse {
|
||||||
base::playlist_goto(mpv, query.index).await.into()
|
base::playlist_goto(mpv, query.index).await.into()
|
||||||
|
@ -159,7 +146,7 @@ struct PlaylistRemoveOrClearArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn playlist_remove_or_clear(
|
async fn playlist_remove_or_clear(
|
||||||
State(mpv): State<Arc<Mutex<Mpv>>>,
|
State(mpv): State<Mpv>,
|
||||||
Query(query): Query<PlaylistRemoveOrClearArgs>,
|
Query(query): Query<PlaylistRemoveOrClearArgs>,
|
||||||
) -> RestResponse {
|
) -> RestResponse {
|
||||||
match query.index {
|
match query.index {
|
||||||
|
@ -175,7 +162,7 @@ struct PlaylistMoveArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn playlist_move(
|
async fn playlist_move(
|
||||||
State(mpv): State<Arc<Mutex<Mpv>>>,
|
State(mpv): State<Mpv>,
|
||||||
Query(query): Query<PlaylistMoveArgs>,
|
Query(query): Query<PlaylistMoveArgs>,
|
||||||
) -> RestResponse {
|
) -> RestResponse {
|
||||||
base::playlist_move(mpv, query.index1, query.index2)
|
base::playlist_move(mpv, query.index1, query.index2)
|
||||||
|
@ -183,11 +170,11 @@ async fn playlist_move(
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn shuffle(State(mpv): State<Arc<Mutex<Mpv>>>) -> RestResponse {
|
async fn shuffle(State(mpv): State<Mpv>) -> RestResponse {
|
||||||
base::shuffle(mpv).await.into()
|
base::shuffle(mpv).await.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn playlist_get_looping(State(mpv): State<Arc<Mutex<Mpv>>>) -> RestResponse {
|
async fn playlist_get_looping(State(mpv): State<Mpv>) -> RestResponse {
|
||||||
base::playlist_get_looping(mpv).await.into()
|
base::playlist_get_looping(mpv).await.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,7 +184,7 @@ struct PlaylistSetLoopingArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn playlist_set_looping(
|
async fn playlist_set_looping(
|
||||||
State(mpv): State<Arc<Mutex<Mpv>>>,
|
State(mpv): State<Mpv>,
|
||||||
Query(query): Query<PlaylistSetLoopingArgs>,
|
Query(query): Query<PlaylistSetLoopingArgs>,
|
||||||
) -> RestResponse {
|
) -> RestResponse {
|
||||||
base::playlist_set_looping(mpv, query.r#loop).await.into()
|
base::playlist_set_looping(mpv, query.r#loop).await.into()
|
||||||
|
|
16
src/main.rs
16
src/main.rs
|
@ -6,12 +6,8 @@ use std::{
|
||||||
fs::create_dir_all,
|
fs::create_dir_all,
|
||||||
net::{IpAddr, SocketAddr},
|
net::{IpAddr, SocketAddr},
|
||||||
path::Path,
|
path::Path,
|
||||||
sync::Arc,
|
|
||||||
};
|
|
||||||
use tokio::{
|
|
||||||
process::{Child, Command},
|
|
||||||
sync::Mutex,
|
|
||||||
};
|
};
|
||||||
|
use tokio::process::{Child, Command};
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
|
|
||||||
|
@ -112,7 +108,7 @@ async fn connect_to_mpv(args: &MpvConnectionArgs) -> anyhow::Result<(Mpv, Option
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Mpv::connect(&args.socket_path).context(format!(
|
Mpv::connect(&args.socket_path).await.context(format!(
|
||||||
"Failed to connect to mpv socket: {}",
|
"Failed to connect to mpv socket: {}",
|
||||||
&args.socket_path
|
&args.socket_path
|
||||||
))?,
|
))?,
|
||||||
|
@ -146,16 +142,17 @@ async fn main() -> anyhow::Result<()> {
|
||||||
let addr = SocketAddr::new(resolve(&args.host).await?, args.port);
|
let addr = SocketAddr::new(resolve(&args.host).await?, args.port);
|
||||||
log::info!("Starting API on {}", addr);
|
log::info!("Starting API on {}", addr);
|
||||||
|
|
||||||
let mpv = Arc::new(Mutex::new(mpv));
|
let app = Router::new().nest("/api", api::rest_api_routes(mpv.clone()));
|
||||||
let app = Router::new().nest("/api", api::rest_api_routes(mpv));
|
|
||||||
|
|
||||||
if let Some(mut proc) = proc {
|
if let Some(mut proc) = proc {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
exit_status = proc.wait() => {
|
exit_status = proc.wait() => {
|
||||||
log::warn!("mpv process exited with status: {}", exit_status?);
|
log::warn!("mpv process exited with status: {}", exit_status?);
|
||||||
|
mpv.disconnect().await?;
|
||||||
}
|
}
|
||||||
_ = tokio::signal::ctrl_c() => {
|
_ = tokio::signal::ctrl_c() => {
|
||||||
log::info!("Received Ctrl-C, exiting");
|
log::info!("Received Ctrl-C, exiting");
|
||||||
|
mpv.disconnect().await?;
|
||||||
proc.kill().await?;
|
proc.kill().await?;
|
||||||
}
|
}
|
||||||
result = async {
|
result = async {
|
||||||
|
@ -165,6 +162,7 @@ async fn main() -> anyhow::Result<()> {
|
||||||
}
|
}
|
||||||
} => {
|
} => {
|
||||||
log::info!("API server exited");
|
log::info!("API server exited");
|
||||||
|
mpv.disconnect().await?;
|
||||||
proc.kill().await?;
|
proc.kill().await?;
|
||||||
result?;
|
result?;
|
||||||
}
|
}
|
||||||
|
@ -173,9 +171,11 @@ async fn main() -> anyhow::Result<()> {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
_ = tokio::signal::ctrl_c() => {
|
_ = tokio::signal::ctrl_c() => {
|
||||||
log::info!("Received Ctrl-C, exiting");
|
log::info!("Received Ctrl-C, exiting");
|
||||||
|
mpv.disconnect().await?;
|
||||||
}
|
}
|
||||||
_ = Server::bind(&addr.clone()).serve(app.into_make_service()) => {
|
_ = Server::bind(&addr.clone()).serve(app.into_make_service()) => {
|
||||||
log::info!("API server exited");
|
log::info!("API server exited");
|
||||||
|
mpv.disconnect().await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue