Skip to content

Commit

Permalink
cleaned up code for review
Browse files Browse the repository at this point in the history
  • Loading branch information
cteters committed Aug 12, 2019
1 parent 4974eec commit d2475a0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 79 deletions.
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ glib = "0.8.1"
gstreamer-player = "0.14.0"

[dependencies.rocket_contrib]
rocket_contrib = "0.4.2"
default_features = false
features = ["tera_templates", "serve", "json"]


version = "0.4.2"
default-features = false
features = ["handlebars_templates", "tera_templates", "serve", "json"]



Expand Down
113 changes: 43 additions & 70 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ extern crate serde_derive;
extern crate rocket_contrib;

// Rocket dependencies
use rocket::request::FlashMessage;
use rocket::Rocket;
use rocket_contrib::{serve::StaticFiles, templates::Template};

Expand All @@ -28,14 +27,13 @@ use qr2term::print_qr;
// Radio dependencies
use gst::prelude::*;
use gst::*;
extern crate glib;
extern crate gstreamer as gst;
extern crate gstreamer_player as gst_player;
extern crate glib;

// Temp fix to kill radio threads
use std::time::Duration;
use std::thread;

use std::time::Duration;

//////////////////basis for the wrapped code found here
//////////////////https://stackoverflow.com/questions/19605132/is-it-possible-to-use-global-variables-in-rust
Expand All @@ -44,14 +42,12 @@ thread_local!(static SINK: RefCell<rodio::Sink> = RefCell::new(rodio::Sink::new(
thread_local!(static PLAYBIN: RefCell<gst::Element> = RefCell::new(gst::ElementFactory::make("playbin", None).unwrap()));
/////////////////end wrapped code



/////////////////////////////////////////////
////MyTrack:
//// This struct is used in order to pass
//// This struct is used in order to pass
//// a vector of tracks back and forth
//// between the front end and the back end
//// as a JSON object.
//// as a JSON object.
//// Function Author: Paul Hubbard
/////////////////////////////////////////////
#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -82,7 +78,7 @@ fn change_cover<P: AsRef<Path>>(file_path: P) {
}
///////////////////////////////////////////////////////////////////
////get_track_list:
//// This returns a list of all available tracks in a random
//// This returns a list of all available tracks in a random
//// order.
//// Function Author: Max Smiley / Paul Hubbard
///////////////////////////////////////////////////////////////////
Expand All @@ -106,11 +102,11 @@ fn get_track_list() -> Vec<mapgen::track::Track> {
///////////////////////////////////////////////////////////////////
////get_track:
//// This gets a single track from the list of available tracks
//// based on whatever track name gets passed in.
//// Parameters:
//// based on whatever track name gets passed in.
//// Parameters:
//// track_name: This is the name of the track
//// you wish to get from the available
//// tracks.
//// tracks.
//// Function Author: Max Smiley / Paul Hubbard
//////////////////////////////////////////////////////////////////
fn get_track(track_name: String) -> mapgen::track::Track {
Expand All @@ -135,7 +131,7 @@ fn get_track(track_name: String) -> mapgen::track::Track {
/////////////////////////////////////////////////
////pause:
//// This pauses the current audio being played
//// in the audio sink.
//// in the audio sink.
//// Function Author: Paul Hubbard
/////////////////////////////////////////////////
#[post("/pause")]
Expand All @@ -149,21 +145,22 @@ fn pause() {
////stop:
//// This gets called by a post from the front end
//// and it removes all of mp3's currently in the
//// sink, while also stopping the audio.
//// Function Author: Paul Hubbard
//// sink, while also stopping the audio.
//// Function Author: Paul Hubbard
///////////////////////////////////////////////
#[post("/stop")]
fn stop() {
SINK.with(|sink_cell| {
sink_cell.borrow_mut().stop();
let new_sink: RefCell<rodio::Sink> = RefCell::new(rodio::Sink::new(&rodio::default_output_device().unwrap()));
let new_sink: RefCell<rodio::Sink> =
RefCell::new(rodio::Sink::new(&rodio::default_output_device().unwrap()));
sink_cell.swap(&new_sink);
});
}
////////////////////////////////////////////////
////play:
//// This function is called from a post coming
//// from the front end, and it starts the
//// This function is called from a post coming
//// from the front end, and it starts the
//// audio sink list, then returns a string
//// telling the front end it was a success.
//// Function Author: Paul Hubbard
Expand All @@ -179,26 +176,38 @@ fn play() -> String {
"success".to_string()
}


////////////////////////////////////////////////
////radio:
//// This function plays an internet radio
//// station when given a correct web address
//// The function doesn't currently have a
//// method for killing old threads, once a
//// new thread has been started and should
//// be considered incomplete until until
//// async support is included.
//// Parameters:
//// uri: webaddress of internet radio
//// station.
////
//// Function Author:
//// Christopher Teters
///////////////////////////////////////////////
#[post("/radio", data = "<uri>")]
fn radio(uri: String) -> String{

//Different gstreamer state: Ready, Pause, Null, Playing
fn radio(uri: String) -> String {
PLAYBIN.with(|radio_cell| {
radio_cell.borrow_mut().set_state(State::Ready).expect("Unable to set the pipeline to the `Ready` state");
radio_cell
.borrow_mut()
.set_state(State::Ready)
.expect("Unable to set the pipeline to the `Ready` state");
radio_cell.borrow_mut().set_property("uri", &uri).unwrap();
radio_cell.borrow_mut().set_state(State::Playing).expect("Unable to set the pipeline to the `Playing` state");
radio_cell
.borrow_mut()
.set_state(State::Playing)
.expect("Unable to set the pipeline to the `Playing` state");
thread::sleep(Duration::from_millis(3500));
radio_cell
.borrow_mut()
.set_state(State::Ready)
.expect("Unable to set the pipeline to the `Ready` state");
/*
let playbin: RefCell<gst::Element> = RefCell::new(gst::ElementFactory::make("playbin", None).unwrap());
playbin.borrow_mut().set_property("uri", &uri).unwrap();
Expand All @@ -207,22 +216,20 @@ fn radio(uri: String) -> String{
*/
});

thread::sleep(Duration::from_millis(3000));
"success".to_string()
}


////////////////////////////////////////////////
////load_songs:
//// This function receives a playlist
//// as a JSON object from the frontend
//// and loads the songs from their mp3
//// source into a sink that will play
//// the audio.
//// the audio.
//// Parameters:
//// my_track: This is the JSON object
//// that gets sent in from the
//// user.
//// that gets sent in from the
//// user.
//// Function Author:
//// Paul Hubbard
/////////////////////////////////////////////////
Expand Down Expand Up @@ -256,38 +263,20 @@ fn load_songs(my_track: Json<MyTrack>) {
//////////////////////////////////////////////////
#[get("/get_songs")]
fn get_songs() -> Json<MyTrack> {
let mut track_list: Vec<Track> = Vec::new();
track_list = get_track_list();
let track_list: Vec<Track> = get_track_list();
let tracks: MyTrack = MyTrack { track_list };
Json(tracks)
}

#[derive(Debug, Serialize)]
struct Context<'a, 'b> {
msg: Option<(&'a str, &'b str)>,
}

impl<'a, 'b> Context<'a, 'b> {
pub fn err(msg: &'a str) -> Context<'static, 'a> {
Context {
msg: Some(("error", msg)),
}
}

pub fn raw(msg: Option<(&'a str, &'b str)>) -> Context<'a, 'b> {
Context { msg: msg }
}
struct Context<'a> {
msg: Option<(&'a str)>,
}

#[get("/")]
fn index(msg: Option<FlashMessage<'_, '_>>) -> Template {
Template::render(
"index",
&match msg {
Some(ref msg) => Context::raw(Some((msg.name(), msg.msg()))),
None => Context::raw(None),
},
)
fn index() -> Template {
let context = Context { msg: None };
Template::render("index", context)
}

fn rocket() -> Rocket {
Expand All @@ -303,22 +292,6 @@ fn rocket() -> Rocket {
fn main() {
gst::init().expect("gstreamer failed to load");

// Example playlist entry
/*
let media_dir = Path::new("media/");
let music_lib = get_map(&media_dir);
match music_lib {
Ok(a) => {
println!("HashMap has {} values\n", a.len());
for b in a.keys() {
let track = a.get(b).unwrap();
println!("{}", track.title);
}
}
Err(_) => println!("ERROR READING MUSIC LIBRARY"),
}*/

print_qr("http://192.168.1.32:8888").expect("Can't build QR Code with information given");

rocket().launch();
Expand Down
5 changes: 2 additions & 3 deletions src/mapgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ pub fn get_map(dir: &Path) -> io::Result<HashMap<String, Track>> {
};
} else {
let hm_opt = get_map(&path);
match hm_opt {
Ok(hm_new) => hm.extend(hm_new),
Err(_) => (),
if let Ok(hm_new) = hm_opt {
hm.extend(hm_new)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/mapgen/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Default for Track {
year: 0,
duration: Duration::new(0, 0),
tags: Vec::new(),
cover: None,
cover: Some(Path::new("static/img/album/current-cover.png").to_path_buf()),
}
}
}
Expand Down

0 comments on commit d2475a0

Please sign in to comment.