WIP: use new async mpvipc

This commit is contained in:
Oystein Kristoffer Tveit 2024-04-18 22:02:39 +02:00
parent eb2fc0c40e
commit 8cf2410ef2
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
4 changed files with 369 additions and 290 deletions

604
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -11,7 +11,7 @@ axum = { version = "0.6.20", features = ["macros"] }
clap = { version = "4.4.1", features = ["derive"] }
env_logger = "0.10.0"
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_json = "1.0.105"
tokio = { version = "1.32.0", features = ["full"] }

View File

@ -1,4 +1,3 @@
use log::trace;
use mpvipc::{
Mpv, NumberChangeOptions, PlaylistAddOptions, PlaylistAddTypeOptions, SeekOptions, Switch,
};
@ -6,7 +5,7 @@ use serde_json::{json, Value};
/// Add item to playlist
pub async fn loadfile(mpv: Mpv, path: &str) -> anyhow::Result<()> {
trace!("api::loadfile({:?})", path);
log::trace!("api::loadfile({:?})", path);
mpv.playlist_add(
path,
PlaylistAddTypeOptions::File,
@ -19,29 +18,29 @@ pub async fn loadfile(mpv: Mpv, path: &str) -> anyhow::Result<()> {
/// Check whether the player is paused or playing
pub async fn play_get(mpv: Mpv) -> anyhow::Result<Value> {
trace!("api::play_get()");
log::trace!("api::play_get()");
let paused: bool = mpv.get_property("pause").await?;
Ok(json!(!paused))
}
/// Set whether the player is paused or playing
pub async fn play_set(mpv: Mpv, should_play: bool) -> anyhow::Result<()> {
trace!("api::play_set({:?})", should_play);
mpv.set_property("pause", !should_play)
log::trace!("api::play_set({:?})", should_play);
mpv.set_property("pause", if should_play { "no" } else { "yes" })
.await
.map_err(|e| e.into())
}
/// Get the current player volume
pub async fn volume_get(mpv: Mpv) -> anyhow::Result<Value> {
trace!("api::volume_get()");
log::trace!("api::volume_get()");
let volume: f64 = mpv.get_property("volume").await?;
Ok(json!(volume))
}
/// Set the player volume
pub async fn volume_set(mpv: Mpv, value: f64) -> anyhow::Result<()> {
trace!("api::volume_set({:?})", value);
log::trace!("api::volume_set({:?})", value);
mpv.set_volume(value, NumberChangeOptions::Absolute)
.await
.map_err(|e| e.into())
@ -49,7 +48,7 @@ pub async fn volume_set(mpv: Mpv, value: f64) -> anyhow::Result<()> {
/// Get current playback position
pub async fn time_get(mpv: Mpv) -> anyhow::Result<Value> {
trace!("api::time_get()");
log::trace!("api::time_get()");
let current: f64 = mpv.get_property("time-pos").await?;
let remaining: f64 = mpv.get_property("time-remaining").await?;
let total = current + remaining;
@ -63,7 +62,7 @@ pub async fn time_get(mpv: Mpv) -> anyhow::Result<Value> {
/// Set playback position
pub async fn time_set(mpv: Mpv, pos: Option<f64>, percent: Option<f64>) -> anyhow::Result<()> {
trace!("api::time_set({:?}, {:?})", pos, percent);
log::trace!("api::time_set({:?}, {:?})", pos, percent);
if pos.is_some() && percent.is_some() {
anyhow::bail!("pos and percent cannot be provided at the same time");
}
@ -81,7 +80,7 @@ pub async fn time_set(mpv: Mpv, pos: Option<f64>, percent: Option<f64>) -> anyho
/// Get the current playlist
pub async fn playlist_get(mpv: Mpv) -> anyhow::Result<Value> {
trace!("api::playlist_get()");
log::trace!("api::playlist_get()");
let playlist: mpvipc::Playlist = mpv.get_playlist().await?;
let is_playing: bool = mpv.get_property("pause").await?;
@ -107,56 +106,61 @@ pub async fn playlist_get(mpv: Mpv) -> anyhow::Result<Value> {
/// Skip to the next item in the playlist
pub async fn playlist_next(mpv: Mpv) -> anyhow::Result<()> {
trace!("api::playlist_next()");
log::trace!("api::playlist_next()");
mpv.next().await.map_err(|e| e.into())
}
/// Go back to the previous item in the playlist
pub async fn playlist_previous(mpv: Mpv) -> anyhow::Result<()> {
trace!("api::playlist_previous()");
log::trace!("api::playlist_previous()");
mpv.prev().await.map_err(|e| e.into())
}
/// Go chosen item in the playlist
pub async fn playlist_goto(mpv: Mpv, index: usize) -> anyhow::Result<()> {
trace!("api::playlist_goto({:?})", index);
log::trace!("api::playlist_goto({:?})", index);
mpv.playlist_play_id(index).await.map_err(|e| e.into())
}
/// Clears the playlist
pub async fn playlist_clear(mpv: Mpv) -> anyhow::Result<()> {
trace!("api::playlist_clear()");
log::trace!("api::playlist_clear()");
mpv.playlist_clear().await.map_err(|e| e.into())
}
/// Remove an item from the playlist by index
pub async fn playlist_remove(mpv: Mpv, index: usize) -> anyhow::Result<()> {
trace!("api::playlist_remove({:?})", index);
log::trace!("api::playlist_remove({:?})", index);
mpv.playlist_remove_id(index).await.map_err(|e| e.into())
}
/// Move an item in the playlist from one index to another
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.playlist_move_id(from, to).await.map_err(|e| e.into())
}
/// Shuffle the playlist
pub async fn shuffle(mpv: Mpv) -> anyhow::Result<()> {
trace!("api::shuffle()");
log::trace!("api::shuffle()");
mpv.playlist_shuffle().await.map_err(|e| e.into())
}
/// See whether it loops the playlist or not
pub async fn playlist_get_looping(mpv: Mpv) -> anyhow::Result<Value> {
trace!("api::playlist_get_looping()");
let loop_playlist =
mpv.get_property_value("loop-playlist").await? == Value::String("inf".to_owned());
Ok(json!(loop_playlist))
log::trace!("api::playlist_get_looping()");
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: 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.set_loop_playlist(Switch::On)
.await

View File

@ -1,5 +1,3 @@
use std::sync::Arc;
use axum::{
debug_handler,
extract::{Query, State},
@ -10,7 +8,6 @@ use axum::{
};
use mpvipc::Mpv;
use serde_json::{json, Value};
use tokio::sync::Mutex;
use super::base;
@ -55,7 +52,7 @@ impl IntoResponse for RestResponse {
Ok(value) => (StatusCode::OK, Json(value)).into_response(),
Err(err) => (
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(),
}