Work on the server API

This commit is contained in:
Savanni D'Gerinel 2024-09-03 22:36:56 -04:00
parent 281bef855b
commit 36d489e8a2
3 changed files with 54 additions and 29 deletions

View File

@ -1,9 +1,10 @@
use crate::audio_control::AudioControl;
use std::{
collections::HashSet,
sync::{Arc, RwLock},
};
use crate::audio_control::AudioControl;
struct AppState {
device_list: Vec<String>,
track_list: Vec<String>,
@ -58,29 +59,31 @@ impl App {
st.track_list.clone()
}
pub fn play_pause(&self) -> Result<(), String> {
self.internal.write().unwrap().audio_control.play_pause();
pub fn enable_track(&self, track: &str) -> Result<(), String> {
let mut st = self.internal.write().unwrap();
if !st.currently_playing.contains(track) {
st.currently_playing.insert(track.to_owned());
}
Ok(())
}
pub fn add_track(&self, track: String) -> Result<(), String> {
pub fn disable_track(&self, track: &str) -> Result<(), String> {
let mut st = self.internal.write().unwrap();
if st.track_list.contains(&track) {
st.currently_playing.insert(track.clone());
if st.currently_playing.contains(track) {
st.currently_playing.remove(track);
}
st.audio_control.add_track(track);
Ok(())
}
pub fn stop(&self, track: String) -> Result<(), String> {
let mut st = self.internal.write().unwrap();
st.currently_playing.remove(&track);
pub fn play(&self) -> Result<(), String> {
let st = self.internal.write().unwrap();
st.audio_control.play();
Ok(())
}
pub fn stop_all(&self) -> Result<(), String> {
let mut st = self.internal.write().unwrap();
st.currently_playing = HashSet::new();
pub fn stop(&self) -> Result<(), String> {
let st = self.internal.write().unwrap();
st.audio_control.stop();
Ok(())
}

View File

@ -85,11 +85,23 @@ impl Default for AudioControl {
}
impl AudioControl {
pub fn play_pause(&self) {
if *self.playing.read().unwrap() {
self.pipeline.set_state(gstreamer::State::Paused).unwrap();
} else {
self.pipeline.set_state(gstreamer::State::Playing).unwrap();
pub fn playing(&self) -> bool {
*self.playing.read().unwrap()
}
pub fn play(&self) {
let mut playing = self.playing.write().unwrap();
if !*playing {
// self.pipeline.set_state(gstreamer::State::Playing).unwrap();
*playing = true;
}
}
pub fn stop(&self) {
let mut playing = self.playing.write().unwrap();
if *playing {
// self.pipeline.set_state(gstreamer::State::Paused).unwrap();
*playing = false;
}
}

View File

@ -32,32 +32,40 @@ async fn server_main(state: App) {
move || serde_json::to_string(&state.tracks()).unwrap()
});
let play_track = warp::put()
let enable_track = warp::put()
.and(warp::path!("playing"))
.and(warp::body::json())
.map({
let state = state.clone();
move |params: PlayTrackParams| {
state.play(params.track_name);
state.enable_track(&params.track_name);
"".to_owned()
}
});
let stop_track = warp::delete()
let disable_track = warp::delete()
.and(warp::path!("playing"))
.and(warp::body::json())
.map({
let state = state.clone();
move |params: PlayTrackParams| {
state.stop(params.track_name);
state.disable_track(&params.track_name);
"".to_owned()
}
});
let stop_all_tracks = warp::delete().and(warp::path!("playing")).map({
let play_all = warp::put().and(warp::path!("playing")).map({
let state = state.clone();
move || {
state.stop_all();
state.play();
"".to_owned()
}
});
let stop_all = warp::delete().and(warp::path!("playing")).map({
let state = state.clone();
move || {
state.stop();
"".to_owned()
}
});
@ -70,9 +78,10 @@ async fn server_main(state: App) {
let routes = root
.or(list_output_devices)
.or(list_tracks)
.or(play_track)
.or(stop_track)
.or(stop_all_tracks)
.or(enable_track)
.or(disable_track)
.or(play_all)
.or(stop_all)
.or(now_playing);
serve(routes).run(server_addr).await;
@ -115,6 +124,7 @@ fn pipewire_main(state: App) {
#[tokio::main]
async fn main() {
gstreamer::init();
let state = App::default();
spawn_blocking({