Skip to content

Commit

Permalink
Block player thread when nothing is playing.
Browse files Browse the repository at this point in the history
  • Loading branch information
plietar committed Dec 28, 2015
1 parent 17d4534 commit 22d586e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/mercury.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use byteorder::{BigEndian, ByteOrder, ReadBytesExt, WriteBytesExt};
use eventual;
use eventual::{self, Async};
use protobuf::{self, Message};
use std::collections::HashMap;
use std::io::{Cursor, Read, Write};
Expand Down Expand Up @@ -100,7 +100,7 @@ impl MercuryManager {
uri: uri,
content_type: None,
payload: Vec::new()
});
}).fire();

rx
}
Expand Down
20 changes: 13 additions & 7 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,14 @@ impl PlayerInternal {
let mut decoder = None;

loop {
match self.commands.try_recv() {
Ok(PlayerCommand::Load(track_id, play, position)) => {
let cmd = if self.state.0.lock().unwrap().status == PlayStatus::kPlayStatusPlay {
self.commands.try_recv().ok()
} else {
Some(self.commands.recv().unwrap())
};

match cmd {
Some(PlayerCommand::Load(track_id, play, position)) => {
self.update(|state| {
if state.status == PlayStatus::kPlayStatusPlay {
stream.stop().unwrap();
Expand Down Expand Up @@ -133,23 +139,23 @@ impl PlayerInternal {
});
println!("Load Done");
}
Ok(PlayerCommand::Seek(ms)) => {
Some(PlayerCommand::Seek(ms)) => {
decoder.as_mut().unwrap().time_seek(ms as f64 / 1000f64).unwrap();
self.update(|state| {
state.position_ms = (decoder.as_mut().unwrap().time_tell().unwrap() * 1000f64) as u32;
state.position_measured_at = util::now_ms();
return true;
});
},
Ok(PlayerCommand::Play) => {
Some(PlayerCommand::Play) => {
self.update(|state| {
state.status = PlayStatus::kPlayStatusPlay;
return true;
});

stream.start().unwrap();
},
Ok(PlayerCommand::Pause) => {
Some(PlayerCommand::Pause) => {
self.update(|state| {
state.status = PlayStatus::kPlayStatusPause;
state.update_time = util::now_ms();
Expand All @@ -158,7 +164,7 @@ impl PlayerInternal {

stream.stop().unwrap();
},
Ok(PlayerCommand::Stop) => {
Some(PlayerCommand::Stop) => {
self.update(|state| {
if state.status == PlayStatus::kPlayStatusPlay {
state.status = PlayStatus::kPlayStatusPause;
Expand All @@ -169,7 +175,7 @@ impl PlayerInternal {
stream.stop().unwrap();
decoder = None;
},
Err(..) => (),
None => (),
}

if self.state.0.lock().unwrap().status == PlayStatus::kPlayStatusPlay {
Expand Down

0 comments on commit 22d586e

Please sign in to comment.