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

View File

@ -85,11 +85,23 @@ impl Default for AudioControl {
} }
impl AudioControl { impl AudioControl {
pub fn play_pause(&self) { pub fn playing(&self) -> bool {
if *self.playing.read().unwrap() { *self.playing.read().unwrap()
self.pipeline.set_state(gstreamer::State::Paused).unwrap(); }
} else {
self.pipeline.set_state(gstreamer::State::Playing).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() move || serde_json::to_string(&state.tracks()).unwrap()
}); });
let play_track = warp::put() let enable_track = warp::put()
.and(warp::path!("playing")) .and(warp::path!("playing"))
.and(warp::body::json()) .and(warp::body::json())
.map({ .map({
let state = state.clone(); let state = state.clone();
move |params: PlayTrackParams| { move |params: PlayTrackParams| {
state.play(params.track_name); state.enable_track(&params.track_name);
"".to_owned() "".to_owned()
} }
}); });
let stop_track = warp::delete() let disable_track = warp::delete()
.and(warp::path!("playing")) .and(warp::path!("playing"))
.and(warp::body::json()) .and(warp::body::json())
.map({ .map({
let state = state.clone(); let state = state.clone();
move |params: PlayTrackParams| { move |params: PlayTrackParams| {
state.stop(params.track_name); state.disable_track(&params.track_name);
"".to_owned() "".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(); let state = state.clone();
move || { 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() "".to_owned()
} }
}); });
@ -70,9 +78,10 @@ async fn server_main(state: App) {
let routes = root let routes = root
.or(list_output_devices) .or(list_output_devices)
.or(list_tracks) .or(list_tracks)
.or(play_track) .or(enable_track)
.or(stop_track) .or(disable_track)
.or(stop_all_tracks) .or(play_all)
.or(stop_all)
.or(now_playing); .or(now_playing);
serve(routes).run(server_addr).await; serve(routes).run(server_addr).await;
@ -115,6 +124,7 @@ fn pipewire_main(state: App) {
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
gstreamer::init();
let state = App::default(); let state = App::default();
spawn_blocking({ spawn_blocking({