Skip to content

Commit

Permalink
Merge pull request #14 from gyaur/api_dev
Browse files Browse the repository at this point in the history
Api dev
  • Loading branch information
gyaur authored Nov 29, 2020
2 parents b5d280b + 4d9c5ab commit cf722ee
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 36 deletions.
9 changes: 8 additions & 1 deletion api/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
.*
target
target
.*
*.sh
*.sqlite*
Makefile
*.html
*.py
*.md
22 changes: 13 additions & 9 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,46 +40,52 @@ 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
* 500 - Internal server error


**/play [POST]**
* Body: Empty
* Responses:
* 200 - OK
* Starts playback
* 500 - Internal server error

**/pause [POST]**
* Body: Empty
* Responses:
* 200 - OK
* Starts playback
* 500 - Internal server error


**/playback [GET]**
* Body: Empty
* Responses:
* 200 - OK
* [json] `{"status": playback_status}`
Expand All @@ -100,7 +106,6 @@ All request and response bodies are empty unless specified otherwise


**/volume [GET]**
* Body: Empty
* Responses:
* 200 - OK
* [json] `{"volume": volume}`
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions api/src/lib/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ pub struct Config {
pub streamer_port: u16,
pub command_port: u16,
pub num_workers: u16,
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,
pub playback_start_timeout: Duration,
pub playback_loadscreen_timeout: Duration,
pub command_wait_timeout: Duration,
Expand All @@ -22,6 +30,14 @@ impl ::std::default::Default for Config {
streamer_port: 5002,
command_port: 5003,
num_workers: 4,
proxy_address: format!("http://localhost:{}", 5000),
queue_address: format!("http://localhost:{}", 5001),
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),
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),
Expand Down
14 changes: 6 additions & 8 deletions api/src/lib/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use reqwest::blocking::Client;
use subprocess::Popen;

pub trait Player {
fn start(&self, media: &Url, volume: &Volume) -> Result<Popen>;
fn start(&self, media: Url, volume: &Volume) -> Result<Popen>;
fn stop(&self, process: &mut Popen) -> Result<()>;
fn play(&self, process: &mut Popen) -> Result<()>;
fn pause(&self, process: &mut Popen) -> Result<()>;
Expand All @@ -28,15 +28,14 @@ pub trait Player {
cmd: &Action,
client: &Client,
cfg: &Config,
playback_address: &str,
) -> Result<()>;
}

pub struct OMXPlayer;

impl Player for OMXPlayer {
fn start(&self, media: &Url, volume: &Volume) -> Result<Popen> {
stream(&media, *volume)
fn start(&self, media: Url, volume: &Volume) -> Result<Popen> {
stream(media, *volume)
}

fn stop(&self, process: &mut Popen) -> Result<()> {
Expand Down Expand Up @@ -106,14 +105,13 @@ impl Player for OMXPlayer {
cmd: &Action,
client: &Client,
cfg: &Config,
playback_address: &str,
) -> Result<()> {
match cmd {
Action::Skip => self.skip(&mut process),
Action::Seek(ammount) => self.seek(&mut process, *ammount),
Action::Stream(url) => {
self.stop(&mut process)?;
*process = self.start(&url, &current_volume)?;
*process = self.start(url.clone(), &current_volume)?;
sleep(cfg.playback_start_timeout);
self.pause(&mut process)?;
sleep(cfg.playback_loadscreen_timeout);
Expand All @@ -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()?;
}
Expand All @@ -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()?;
}
Expand Down
14 changes: 11 additions & 3 deletions api/src/lib/video_processing.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<Url, CustomError> {
if url.is_ip() {
Expand Down Expand Up @@ -42,7 +50,7 @@ pub fn extract_url(url: Url) -> Result<Url, CustomError> {
}

#[cfg(target_arch = "arm")]
pub fn stream(url: &Url, volume: Volume) -> Result<Popen, Box<dyn Error>> {
pub fn stream(url: Url, volume: Volume) -> Result<Popen, Box<dyn Error>> {
let url = extract_url(url)?;

let p = Popen::create(
Expand All @@ -68,7 +76,7 @@ pub fn stream(url: &Url, volume: Volume) -> Result<Popen, Box<dyn Error>> {
}

#[cfg(target_arch = "x86_64")]
pub fn stream(_url: &Url, _volume: Volume) -> Result<Popen, Box<dyn Error>> {
pub fn stream(_url: Url, _volume: Volume) -> Result<Popen, Box<dyn Error>> {
let argv = &["ping", "-c", "10", "8.8.8.8"];

let p = Popen::create(
Expand Down
5 changes: 4 additions & 1 deletion api/src/proxy/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -53,7 +54,8 @@ fn setup_rocket() -> Result<Rocket> {
play,
pause,
playback,
set_playback
set_playback,
current_get
],
)
.attach(cors)
Expand Down Expand Up @@ -98,6 +100,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
Expand Down
21 changes: 7 additions & 14 deletions api/src/streamer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,40 @@ 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<dyn Player>) -> 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::<Volume>()?;
let curr = reqwest::blocking::get(queue_address)?.json::<Option<Url>>()?;
let mut volume = get(&cfg.command_volume_address)?.json::<Volume>()?;
let curr = get(&cfg.queue_front_address)?.json::<Option<Url>>()?;
// 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)
.post(&cfg.command_playback_address)
.json(&PlaybackStatus::new(playback_status))
.send()?;
sleep(cfg.playback_start_timeout);
player.pause(&mut process)?;
sleep(cfg.playback_loadscreen_timeout);
player.play(&mut process)?;
loop {
if let Some(cmd) =
reqwest::blocking::get(command_address)?.json::<Option<Action>>()?
{
if let Some(cmd) = get(&cfg.command_front_address)?.json::<Option<Action>>()? {
println!("{:?}", cmd);
let result = player.work(
&mut process,
&mut volume,
&mut playback_status,
&cmd,
&client,
&cfg,
playback_address,
);
println!("{:?}", result);
assert!(result.is_ok());
}
if let Some(_val) = process.poll() {
break;
Expand Down

0 comments on commit cf722ee

Please sign in to comment.