From 2a3bb8fd5a21f882eac1cbeb880aaa2c1810e8d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Gyarmathy?= Date: Fri, 27 Nov 2020 14:26:43 +0100 Subject: [PATCH 1/7] Fix build for arm --- api/src/lib/player.rs | 8 ++++---- api/src/lib/video_processing.rs | 14 +++++++++++--- api/src/streamer/main.rs | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/api/src/lib/player.rs b/api/src/lib/player.rs index 231ab59..e3c37c0 100644 --- a/api/src/lib/player.rs +++ b/api/src/lib/player.rs @@ -6,7 +6,7 @@ use reqwest::blocking::Client; use subprocess::Popen; pub trait Player { - fn start(&self, media: &Url, volume: &Volume) -> Result; + fn start(&self, media: Url, volume: &Volume) -> Result; fn stop(&self, process: &mut Popen) -> Result<()>; fn play(&self, process: &mut Popen) -> Result<()>; fn pause(&self, process: &mut Popen) -> Result<()>; @@ -35,8 +35,8 @@ pub trait Player { pub struct OMXPlayer; impl Player for OMXPlayer { - fn start(&self, media: &Url, volume: &Volume) -> Result { - stream(&media, *volume) + fn start(&self, media: Url, volume: &Volume) -> Result { + stream(media, *volume) } fn stop(&self, process: &mut Popen) -> Result<()> { @@ -113,7 +113,7 @@ impl Player for OMXPlayer { Action::Seek(ammount) => self.seek(&mut process, *ammount), Action::Stream(url) => { self.stop(&mut process)?; - *process = self.start(&url, ¤t_volume)?; + *process = self.start(url.clone(), ¤t_volume)?; sleep(cfg.playback_start_timeout); self.pause(&mut process)?; sleep(cfg.playback_loadscreen_timeout); diff --git a/api/src/lib/video_processing.rs b/api/src/lib/video_processing.rs index 2f8ca2f..1ce790b 100644 --- a/api/src/lib/video_processing.rs +++ b/api/src/lib/video_processing.rs @@ -1,8 +1,8 @@ use crate::Url; use crate::Volume; use cached::proc_macro::cached; -use std::error::Error; use std::io::Write; +use std::{error::Error, fmt}; use subprocess::{Popen, PopenConfig, Redirection}; use youtube_dl::YoutubeDl; @@ -11,6 +11,14 @@ pub enum CustomError { FeatureError(String), } +impl fmt::Display for CustomError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Oh no, something bad went down") + } +} + +impl Error for CustomError {} + #[cached(size = 100)] pub fn extract_url(url: Url) -> Result { if url.is_ip() { @@ -42,7 +50,7 @@ pub fn extract_url(url: Url) -> Result { } #[cfg(target_arch = "arm")] -pub fn stream(url: &Url, volume: Volume) -> Result> { +pub fn stream(url: Url, volume: Volume) -> Result> { let url = extract_url(url)?; let p = Popen::create( @@ -68,7 +76,7 @@ pub fn stream(url: &Url, volume: Volume) -> Result> { } #[cfg(target_arch = "x86_64")] -pub fn stream(_url: &Url, _volume: Volume) -> Result> { +pub fn stream(_url: Url, _volume: Volume) -> Result> { let argv = &["ping", "-c", "10", "8.8.8.8"]; let p = Popen::create( diff --git a/api/src/streamer/main.rs b/api/src/streamer/main.rs index e9d7796..73ff331 100644 --- a/api/src/streamer/main.rs +++ b/api/src/streamer/main.rs @@ -18,7 +18,7 @@ fn stream_loop(cfg: CliViewConfig, player: Box) -> Result<()> { // get next video from squeue service if let Some(url) = curr.clone() { //start process - let mut process = player.start(&url, &volume)?; + let mut process = player.start(url, &volume)?; let mut playback_status = true; client .post(playback_address) From 85084e843f0f1cb4842164d94267c139aa173065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Gyarmathy?= Date: Sun, 29 Nov 2020 16:13:22 +0100 Subject: [PATCH 2/7] Refactor config --- api/src/lib/config.rs | 16 ++++++++++++++++ api/src/lib/player.rs | 6 ++---- api/src/streamer/main.rs | 13 ++++--------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/api/src/lib/config.rs b/api/src/lib/config.rs index ec9df0a..75285ec 100644 --- a/api/src/lib/config.rs +++ b/api/src/lib/config.rs @@ -11,6 +11,14 @@ pub struct Config { pub playback_start_timeout: Duration, pub playback_loadscreen_timeout: Duration, pub command_wait_timeout: Duration, + pub proxy_address: String, + pub queue_address: String, + pub command_address: String, + pub streamer_address: String, + pub queue_front_address: String, + pub command_front_address: String, + pub command_volume_address: String, + pub command_playback_address: String, } /// `Config` implements `Default` @@ -25,6 +33,14 @@ impl ::std::default::Default for Config { playback_start_timeout: Duration::from_millis(500), playback_loadscreen_timeout: Duration::from_secs(3), command_wait_timeout: Duration::from_millis(100), + proxy_address: format!("http://localhost:{}", 5000), + queue_address: format!("http://localhost:{}", 5001), + command_address: format!("http://localhost:{}", 5002), + streamer_address: format!("http://localhost:{}", 5003), + queue_front_address: format!("http://localhost:{}/front", 5001), + command_front_address: format!("http://localhost:{}/front", 5003), + command_volume_address: format!("http://localhost:{}/volume", 5003), + command_playback_address: format!("http://localhost:{}/playback", 5003), } } } diff --git a/api/src/lib/player.rs b/api/src/lib/player.rs index e3c37c0..430dba6 100644 --- a/api/src/lib/player.rs +++ b/api/src/lib/player.rs @@ -28,7 +28,6 @@ pub trait Player { cmd: &Action, client: &Client, cfg: &Config, - playback_address: &str, ) -> Result<()>; } @@ -106,7 +105,6 @@ impl Player for OMXPlayer { cmd: &Action, client: &Client, cfg: &Config, - playback_address: &str, ) -> Result<()> { match cmd { Action::Skip => self.skip(&mut process), @@ -136,7 +134,7 @@ impl Player for OMXPlayer { self.pause(&mut process)?; *playback_status = true; client - .post(playback_address) + .post(&cfg.command_playback_address) .json(&PlaybackStatus::new(*playback_status)) .send()?; } @@ -147,7 +145,7 @@ impl Player for OMXPlayer { self.pause(&mut process)?; *playback_status = false; client - .post(playback_address) + .post(&cfg.command_playback_address) .json(&PlaybackStatus::new(*playback_status)) .send()?; } diff --git a/api/src/streamer/main.rs b/api/src/streamer/main.rs index 73ff331..db3a167 100644 --- a/api/src/streamer/main.rs +++ b/api/src/streamer/main.rs @@ -8,20 +8,16 @@ use std::time::Duration; fn stream_loop(cfg: CliViewConfig, player: Box) -> Result<()> { loop { - let queue_address: &str = &format!("http://localhost:{}/front", cfg.queue_port); - let command_address: &str = &format!("http://localhost:{}/front", cfg.command_port); - let volume_address: &str = &format!("http://localhost:{}/volume", cfg.command_port); - let playback_address: &str = &format!("http://localhost:{}/playback", cfg.command_port); let client = reqwest::blocking::Client::new(); - let mut volume = reqwest::blocking::get(volume_address)?.json::()?; - let curr = reqwest::blocking::get(queue_address)?.json::>()?; + let mut volume = reqwest::blocking::get(&cfg.command_volume_address)?.json::()?; + let curr = reqwest::blocking::get(&cfg.queue_front_address)?.json::>()?; // get next video from squeue service if let Some(url) = curr.clone() { //start process let mut process = player.start(url, &volume)?; let mut playback_status = true; client - .post(playback_address) + .post(&cfg.command_playback_address) .json(&PlaybackStatus::new(playback_status)) .send()?; sleep(cfg.playback_start_timeout); @@ -30,7 +26,7 @@ fn stream_loop(cfg: CliViewConfig, player: Box) -> Result<()> { player.play(&mut process)?; loop { if let Some(cmd) = - reqwest::blocking::get(command_address)?.json::>()? + reqwest::blocking::get(&cfg.command_front_address)?.json::>()? { let result = player.work( &mut process, @@ -39,7 +35,6 @@ fn stream_loop(cfg: CliViewConfig, player: Box) -> Result<()> { &cmd, &client, &cfg, - playback_address, ); println!("{:?}", result); assert!(result.is_ok()); From 9ec3041f21f6a130e60cf5000cb88985e211dc79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Gyarmathy?= Date: Sun, 29 Nov 2020 16:43:26 +0100 Subject: [PATCH 3/7] Fix config refactor --- api/src/lib/config.rs | 12 ++++++------ api/src/streamer/main.rs | 12 +++++------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/api/src/lib/config.rs b/api/src/lib/config.rs index 75285ec..59b6105 100644 --- a/api/src/lib/config.rs +++ b/api/src/lib/config.rs @@ -8,9 +8,6 @@ pub struct Config { pub streamer_port: u16, pub command_port: u16, pub num_workers: u16, - pub playback_start_timeout: Duration, - pub playback_loadscreen_timeout: Duration, - pub command_wait_timeout: Duration, pub proxy_address: String, pub queue_address: String, pub command_address: String, @@ -19,6 +16,9 @@ pub struct Config { pub command_front_address: String, pub command_volume_address: String, pub command_playback_address: String, + pub playback_start_timeout: Duration, + pub playback_loadscreen_timeout: Duration, + pub command_wait_timeout: Duration, } /// `Config` implements `Default` @@ -30,9 +30,6 @@ impl ::std::default::Default for Config { streamer_port: 5002, command_port: 5003, num_workers: 4, - playback_start_timeout: Duration::from_millis(500), - playback_loadscreen_timeout: Duration::from_secs(3), - command_wait_timeout: Duration::from_millis(100), proxy_address: format!("http://localhost:{}", 5000), queue_address: format!("http://localhost:{}", 5001), command_address: format!("http://localhost:{}", 5002), @@ -41,6 +38,9 @@ impl ::std::default::Default for Config { command_front_address: format!("http://localhost:{}/front", 5003), command_volume_address: format!("http://localhost:{}/volume", 5003), command_playback_address: format!("http://localhost:{}/playback", 5003), + playback_start_timeout: Duration::from_millis(500), + playback_loadscreen_timeout: Duration::from_secs(3), + command_wait_timeout: Duration::from_millis(100), } } } diff --git a/api/src/streamer/main.rs b/api/src/streamer/main.rs index db3a167..b80eca9 100644 --- a/api/src/streamer/main.rs +++ b/api/src/streamer/main.rs @@ -2,15 +2,15 @@ use lib::Url; use lib::{Action, Config as CliViewConfig}; use lib::{GenericResult as Result, Player, Volume}; use lib::{OMXPlayer, PlaybackStatus}; -// use reqwest; +use reqwest::blocking::get; use std::thread::sleep; use std::time::Duration; fn stream_loop(cfg: CliViewConfig, player: Box) -> Result<()> { loop { let client = reqwest::blocking::Client::new(); - let mut volume = reqwest::blocking::get(&cfg.command_volume_address)?.json::()?; - let curr = reqwest::blocking::get(&cfg.queue_front_address)?.json::>()?; + let mut volume = get(&cfg.command_volume_address)?.json::()?; + let curr = get(&cfg.queue_front_address)?.json::>()?; // get next video from squeue service if let Some(url) = curr.clone() { //start process @@ -25,9 +25,8 @@ fn stream_loop(cfg: CliViewConfig, player: Box) -> Result<()> { sleep(cfg.playback_loadscreen_timeout); player.play(&mut process)?; loop { - if let Some(cmd) = - reqwest::blocking::get(&cfg.command_front_address)?.json::>()? - { + if let Some(cmd) = get(&cfg.command_front_address)?.json::>()? { + println!("{:?}", cmd); let result = player.work( &mut process, &mut volume, @@ -37,7 +36,6 @@ fn stream_loop(cfg: CliViewConfig, player: Box) -> Result<()> { &cfg, ); println!("{:?}", result); - assert!(result.is_ok()); } if let Some(_val) = process.poll() { break; From 03a23e8ddc7874a23c09e6cf2fe7ba796c7239c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Gyarmathy?= Date: Sun, 29 Nov 2020 20:28:50 +0100 Subject: [PATCH 4/7] Add /current to proxy --- api/src/proxy/main.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/src/proxy/main.rs b/api/src/proxy/main.rs index c29df9c..79988b7 100644 --- a/api/src/proxy/main.rs +++ b/api/src/proxy/main.rs @@ -12,6 +12,7 @@ use rocket::{ //Queue service functions gen_proxy_function!(queue_get, "/queue", get, queue_port); gen_proxy_function!(queue_post, "/queue", post, queue_port); +gen_proxy_function!(current_get, "/current", get, queue_port); //Command service functions gen_proxy_function!(stream, "/stream", post, command_port); @@ -98,6 +99,7 @@ mod test { //Queue service tests generate_test!(test_queue_get, queue_port, get, "/queue"); + generate_test!(test_current_get, queue_port, get, "/current"); generate_test!(test_queue_post, queue_port, post, "/queue"); //Command service tests From 50feb9778976da9c28bbff741689ccc84c9f7ae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Gyarmathy?= Date: Sun, 29 Nov 2020 20:29:41 +0100 Subject: [PATCH 5/7] Update docs --- api/README.md | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/api/README.md b/api/README.md index 43b3009..dc103a3 100644 --- a/api/README.md +++ b/api/README.md @@ -40,23 +40,32 @@ All request and response bodies are empty unless specified otherwise **/queue [GET]** -* Body: Empty * Responses: * 200 - OK - * [json] List of videos in the queue `{"queue": [{"url": url}],...}` + * [json] List of videos in the queue `{"queue": [{"url": url, "extracted_url": extracted_url},...]}` + * where *url* is the url that was passed in and the *extracted_url* is the direct link to the video + * 400 - Bad Request + * bad link or incorrect json + * 500 - Internal server error + +**/current [GET]** +* Responses: + * 200 - OK + * [json] currently playing videos url `{"url": url, "extracted_url": extracted_url}` + * where *url* is the url that was passed in and the *extracted_url* is the direct link to the video + * *url* is always a string, but *extracted_url* could be null + * if no video is playing the the value is null * 400 - Bad Request * bad link or incorrect json * 500 - Internal server error **/inc [POST]** -* Body: Empty * Responses: * 200 - OK * Increases volume of playback by 1 * 500 - Internal server error **/dec [POST]** -* Body: Empty * Responses: * 200 - OK * Decreases volume of playback by 1 @@ -64,14 +73,12 @@ All request and response bodies are empty unless specified otherwise **/play [POST]** -* Body: Empty * Responses: * 200 - OK * Starts playback * 500 - Internal server error **/pause [POST]** -* Body: Empty * Responses: * 200 - OK * Starts playback @@ -79,7 +86,6 @@ All request and response bodies are empty unless specified otherwise **/playback [GET]** -* Body: Empty * Responses: * 200 - OK * [json] `{"status": playback_status}` @@ -100,7 +106,6 @@ All request and response bodies are empty unless specified otherwise **/volume [GET]** -* Body: Empty * Responses: * 200 - OK * [json] `{"volume": volume}` @@ -121,7 +126,6 @@ All request and response bodies are empty unless specified otherwise **/skip [POST]** -* Body: Empty * Responses: * 200 - OK * currently playing video is skipeed From c694e225a2417a025c49e8f1096020aaa7ad3701 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Gyarmathy?= Date: Sun, 29 Nov 2020 20:29:54 +0100 Subject: [PATCH 6/7] Update .gitignore --- api/.gitignore | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/api/.gitignore b/api/.gitignore index b617a6f..a229a4b 100644 --- a/api/.gitignore +++ b/api/.gitignore @@ -1,2 +1,9 @@ .* -target \ No newline at end of file +target +.* +*.sh +*.sqlite* +Makefile +*.html +*.py +*.md \ No newline at end of file From 4d9c5ab4b65879e2555876e36e7cd6fc4dd9fa24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Gyarmathy?= Date: Sun, 29 Nov 2020 20:58:02 +0100 Subject: [PATCH 7/7] Minor fixes --- api/src/lib/config.rs | 4 ++-- api/src/proxy/main.rs | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/api/src/lib/config.rs b/api/src/lib/config.rs index 59b6105..e5aae0f 100644 --- a/api/src/lib/config.rs +++ b/api/src/lib/config.rs @@ -32,8 +32,8 @@ impl ::std::default::Default for Config { num_workers: 4, proxy_address: format!("http://localhost:{}", 5000), queue_address: format!("http://localhost:{}", 5001), - command_address: format!("http://localhost:{}", 5002), - streamer_address: format!("http://localhost:{}", 5003), + streamer_address: format!("http://localhost:{}", 5002), + command_address: format!("http://localhost:{}", 5003), queue_front_address: format!("http://localhost:{}/front", 5001), command_front_address: format!("http://localhost:{}/front", 5003), command_volume_address: format!("http://localhost:{}/volume", 5003), diff --git a/api/src/proxy/main.rs b/api/src/proxy/main.rs index 79988b7..7b248ef 100644 --- a/api/src/proxy/main.rs +++ b/api/src/proxy/main.rs @@ -54,7 +54,8 @@ fn setup_rocket() -> Result { play, pause, playback, - set_playback + set_playback, + current_get ], ) .attach(cors)