2017-01-29 14:11:20 +00:00
|
|
|
use futures::sync::oneshot;
|
2017-01-20 12:56:42 +00:00
|
|
|
use futures::{future, Future};
|
2018-02-23 19:16:03 +00:00
|
|
|
use futures;
|
2018-02-10 15:26:08 +00:00
|
|
|
use std;
|
2016-02-05 20:54:47 +00:00
|
|
|
use std::borrow::Cow;
|
2018-02-10 15:26:08 +00:00
|
|
|
use std::io::{Read, Seek, SeekFrom, Result};
|
2017-01-29 14:11:20 +00:00
|
|
|
use std::mem;
|
2017-11-28 23:18:12 +00:00
|
|
|
use std::sync::mpsc::{RecvError, TryRecvError, RecvTimeoutError};
|
2017-01-20 12:56:42 +00:00
|
|
|
use std::thread;
|
2017-11-28 23:18:12 +00:00
|
|
|
use std::time::Duration;
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2018-02-20 20:57:42 +00:00
|
|
|
use config::{Bitrate, PlayerConfig};
|
2017-08-03 18:58:44 +00:00
|
|
|
use core::session::Session;
|
2018-02-12 20:02:27 +00:00
|
|
|
use core::spotify_id::SpotifyId;
|
2017-08-03 18:58:44 +00:00
|
|
|
|
2016-03-20 16:16:11 +00:00
|
|
|
use audio_backend::Sink;
|
2017-08-03 20:22:08 +00:00
|
|
|
use audio::{AudioFile, AudioDecrypt};
|
|
|
|
use audio::{VorbisDecoder, VorbisPacket};
|
2017-05-16 00:04:10 +00:00
|
|
|
use metadata::{FileFormat, Track, Metadata};
|
2017-01-27 13:20:31 +00:00
|
|
|
use mixer::AudioFilter;
|
2016-03-13 15:15:15 +00:00
|
|
|
|
2015-09-01 11:20:37 +00:00
|
|
|
pub struct Player {
|
2017-11-27 23:35:04 +00:00
|
|
|
commands: Option<std::sync::mpsc::Sender<PlayerCommand>>,
|
|
|
|
thread_handle: Option<thread::JoinHandle<()>>,
|
2015-07-02 19:42:49 +00:00
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-09-01 11:20:37 +00:00
|
|
|
struct PlayerInternal {
|
|
|
|
session: Session,
|
2017-08-03 18:31:15 +00:00
|
|
|
config: PlayerConfig,
|
2017-01-20 12:56:42 +00:00
|
|
|
commands: std::sync::mpsc::Receiver<PlayerCommand>,
|
2017-01-29 14:11:20 +00:00
|
|
|
|
|
|
|
state: PlayerState,
|
|
|
|
sink: Box<Sink>,
|
2017-11-28 23:18:12 +00:00
|
|
|
sink_running: bool,
|
2017-02-21 22:46:19 +00:00
|
|
|
audio_filter: Option<Box<AudioFilter + Send>>,
|
2018-02-23 19:16:03 +00:00
|
|
|
event_sender: futures::sync::mpsc::UnboundedSender<PlayerEvent>,
|
2015-07-02 19:42:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 20:08:14 +00:00
|
|
|
enum PlayerCommand {
|
2017-01-29 14:11:20 +00:00
|
|
|
Load(SpotifyId, bool, u32, oneshot::Sender<()>),
|
2015-07-09 20:08:14 +00:00
|
|
|
Play,
|
|
|
|
Pause,
|
|
|
|
Stop,
|
2016-01-02 15:19:39 +00:00
|
|
|
Seek(u32),
|
2015-07-02 19:42:49 +00:00
|
|
|
}
|
|
|
|
|
2018-02-20 20:57:42 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum PlayerEvent {
|
|
|
|
Started {
|
|
|
|
track_id: SpotifyId,
|
|
|
|
},
|
|
|
|
|
|
|
|
Changed {
|
|
|
|
old_track_id: SpotifyId,
|
|
|
|
new_track_id: SpotifyId,
|
|
|
|
},
|
|
|
|
|
|
|
|
Stopped {
|
|
|
|
track_id: SpotifyId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 19:16:03 +00:00
|
|
|
type PlayerEventChannel = futures::sync::mpsc::UnboundedReceiver<PlayerEvent>;
|
2015-09-01 11:20:37 +00:00
|
|
|
impl Player {
|
2017-08-03 18:31:15 +00:00
|
|
|
pub fn new<F>(config: PlayerConfig, session: Session,
|
|
|
|
audio_filter: Option<Box<AudioFilter + Send>>,
|
2018-02-20 22:09:48 +00:00
|
|
|
sink_builder: F) -> (Player, PlayerEventChannel)
|
2017-08-03 18:31:15 +00:00
|
|
|
where F: FnOnce() -> Box<Sink> + Send + 'static
|
|
|
|
{
|
2017-01-20 12:56:42 +00:00
|
|
|
let (cmd_tx, cmd_rx) = std::sync::mpsc::channel();
|
2018-02-23 19:16:03 +00:00
|
|
|
let (event_sender, event_receiver) = futures::sync::mpsc::unbounded();
|
2015-07-02 19:42:49 +00:00
|
|
|
|
2017-11-27 23:35:04 +00:00
|
|
|
let handle = thread::spawn(move || {
|
2017-02-22 04:17:04 +00:00
|
|
|
debug!("new Player[{}]", session.session_id());
|
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
let internal = PlayerInternal {
|
|
|
|
session: session,
|
2017-08-03 18:31:15 +00:00
|
|
|
config: config,
|
2017-01-29 14:11:20 +00:00
|
|
|
commands: cmd_rx,
|
2015-07-02 19:42:49 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
state: PlayerState::Stopped,
|
|
|
|
sink: sink_builder(),
|
2017-11-28 23:18:12 +00:00
|
|
|
sink_running: false,
|
2017-02-21 22:46:19 +00:00
|
|
|
audio_filter: audio_filter,
|
2018-02-20 22:09:48 +00:00
|
|
|
event_sender: event_sender,
|
2017-01-29 14:11:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
internal.run();
|
|
|
|
});
|
2015-09-01 11:20:37 +00:00
|
|
|
|
2018-02-20 22:09:48 +00:00
|
|
|
(Player { commands: Some(cmd_tx), thread_handle: Some(handle) },
|
|
|
|
event_receiver)
|
2015-07-02 19:42:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 20:08:14 +00:00
|
|
|
fn command(&self, cmd: PlayerCommand) {
|
2017-11-27 23:35:04 +00:00
|
|
|
self.commands.as_ref().unwrap().send(cmd).unwrap();
|
2015-07-02 19:42:49 +00:00
|
|
|
}
|
2016-01-20 14:11:49 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
pub fn load(&self, track: SpotifyId, start_playing: bool, position_ms: u32)
|
|
|
|
-> oneshot::Receiver<()>
|
|
|
|
{
|
|
|
|
let (tx, rx) = oneshot::channel();
|
|
|
|
self.command(PlayerCommand::Load(track, start_playing, position_ms, tx));
|
|
|
|
|
|
|
|
rx
|
2016-01-20 14:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn play(&self) {
|
|
|
|
self.command(PlayerCommand::Play)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pause(&self) {
|
|
|
|
self.command(PlayerCommand::Pause)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stop(&self) {
|
|
|
|
self.command(PlayerCommand::Stop)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn seek(&self, position_ms: u32) {
|
|
|
|
self.command(PlayerCommand::Seek(position_ms));
|
|
|
|
}
|
2015-07-02 19:42:49 +00:00
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2017-11-27 23:35:04 +00:00
|
|
|
impl Drop for Player {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
debug!("Shutting down player thread ...");
|
|
|
|
self.commands = None;
|
|
|
|
if let Some(handle) = self.thread_handle.take() {
|
|
|
|
match handle.join() {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(_) => error!("Player thread panicked!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:08 +00:00
|
|
|
type Decoder = VorbisDecoder<Subfile<AudioDecrypt<AudioFile>>>;
|
2017-01-29 14:11:20 +00:00
|
|
|
enum PlayerState {
|
|
|
|
Stopped,
|
|
|
|
Paused {
|
2018-02-15 23:16:38 +00:00
|
|
|
track_id: SpotifyId,
|
2017-01-29 14:11:20 +00:00
|
|
|
decoder: Decoder,
|
|
|
|
end_of_track: oneshot::Sender<()>,
|
|
|
|
},
|
|
|
|
Playing {
|
2018-02-15 23:16:38 +00:00
|
|
|
track_id: SpotifyId,
|
2017-01-29 14:11:20 +00:00
|
|
|
decoder: Decoder,
|
|
|
|
end_of_track: oneshot::Sender<()>,
|
|
|
|
},
|
2018-02-15 23:16:38 +00:00
|
|
|
EndOfTrack { track_id: SpotifyId },
|
2017-01-29 14:11:20 +00:00
|
|
|
Invalid,
|
2016-02-05 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
impl PlayerState {
|
|
|
|
fn is_playing(&self) -> bool {
|
|
|
|
use self::PlayerState::*;
|
|
|
|
match *self {
|
2018-02-15 23:16:38 +00:00
|
|
|
Stopped | EndOfTrack { .. } | Paused { .. } => false,
|
2017-01-29 14:11:20 +00:00
|
|
|
Playing { .. } => true,
|
|
|
|
Invalid => panic!("invalid state"),
|
|
|
|
}
|
2016-05-04 08:11:03 +00:00
|
|
|
}
|
2017-01-05 13:25:14 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
fn decoder(&mut self) -> Option<&mut Decoder> {
|
|
|
|
use self::PlayerState::*;
|
|
|
|
match *self {
|
2018-02-15 23:16:38 +00:00
|
|
|
Stopped | EndOfTrack { .. } => None,
|
2017-01-29 14:11:20 +00:00
|
|
|
Paused { ref mut decoder, .. } |
|
|
|
|
Playing { ref mut decoder, .. } => Some(decoder),
|
|
|
|
Invalid => panic!("invalid state"),
|
2016-05-04 08:11:03 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
}
|
2016-05-04 08:11:03 +00:00
|
|
|
|
2018-02-20 20:58:02 +00:00
|
|
|
fn playing_to_end_of_track(&mut self) {
|
|
|
|
use self::PlayerState::*;
|
2018-02-23 19:16:03 +00:00
|
|
|
match mem::replace(self, Invalid) {
|
|
|
|
Playing { track_id, end_of_track, ..} => {
|
|
|
|
end_of_track.send(());
|
2018-02-20 20:58:02 +00:00
|
|
|
let old_state = mem::replace(self, EndOfTrack { track_id });
|
2018-02-20 21:09:53 +00:00
|
|
|
},
|
|
|
|
_ => panic!("Called playing_to_end_of_track in non-playing state.")
|
2016-05-04 08:11:03 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
}
|
2016-05-04 08:11:03 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
fn paused_to_playing(&mut self) {
|
|
|
|
use self::PlayerState::*;
|
|
|
|
match ::std::mem::replace(self, Invalid) {
|
2018-02-15 23:16:38 +00:00
|
|
|
Paused { decoder, end_of_track, track_id } => {
|
2017-01-29 14:11:20 +00:00
|
|
|
*self = Playing {
|
|
|
|
decoder: decoder,
|
|
|
|
end_of_track: end_of_track,
|
2018-02-15 23:16:38 +00:00
|
|
|
track_id: track_id,
|
2017-01-29 14:11:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
_ => panic!("invalid state"),
|
|
|
|
}
|
|
|
|
}
|
2016-09-08 18:49:17 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
fn playing_to_paused(&mut self) {
|
|
|
|
use self::PlayerState::*;
|
|
|
|
match ::std::mem::replace(self, Invalid) {
|
2018-02-15 23:16:38 +00:00
|
|
|
Playing { decoder, end_of_track, track_id } => {
|
2017-01-29 14:11:20 +00:00
|
|
|
*self = Paused {
|
|
|
|
decoder: decoder,
|
|
|
|
end_of_track: end_of_track,
|
2018-02-15 23:16:38 +00:00
|
|
|
track_id: track_id,
|
2017-01-29 14:11:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
_ => panic!("invalid state"),
|
|
|
|
}
|
|
|
|
}
|
2016-09-08 18:49:17 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 11:20:37 +00:00
|
|
|
impl PlayerInternal {
|
2017-01-29 14:11:20 +00:00
|
|
|
fn run(mut self) {
|
2015-07-02 19:42:49 +00:00
|
|
|
loop {
|
2017-01-29 14:11:20 +00:00
|
|
|
let cmd = if self.state.is_playing() {
|
2017-11-28 23:18:12 +00:00
|
|
|
if self.sink_running
|
|
|
|
{
|
|
|
|
match self.commands.try_recv() {
|
|
|
|
Ok(cmd) => Some(cmd),
|
|
|
|
Err(TryRecvError::Empty) => None,
|
|
|
|
Err(TryRecvError::Disconnected) => return,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
match self.commands.recv_timeout(Duration::from_secs(5)) {
|
|
|
|
Ok(cmd) => Some(cmd),
|
|
|
|
Err(RecvTimeoutError::Timeout) => None,
|
|
|
|
Err(RecvTimeoutError::Disconnected) => return,
|
|
|
|
}
|
2017-01-31 08:21:30 +00:00
|
|
|
}
|
2015-12-28 17:45:13 +00:00
|
|
|
} else {
|
2017-01-31 08:21:30 +00:00
|
|
|
match self.commands.recv() {
|
|
|
|
Ok(cmd) => Some(cmd),
|
|
|
|
Err(RecvError) => return,
|
|
|
|
}
|
2015-12-28 17:45:13 +00:00
|
|
|
};
|
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
if let Some(cmd) = cmd {
|
|
|
|
self.handle_command(cmd);
|
|
|
|
}
|
2016-05-04 08:11:03 +00:00
|
|
|
|
2017-11-28 23:18:12 +00:00
|
|
|
if self.state.is_playing() && ! self.sink_running {
|
|
|
|
self.start_sink();
|
|
|
|
}
|
2015-12-28 16:47:53 +00:00
|
|
|
|
2017-11-28 23:18:12 +00:00
|
|
|
if self.sink_running {
|
|
|
|
let packet = if let PlayerState::Playing { ref mut decoder, .. } = self.state {
|
|
|
|
Some(decoder.next_packet().expect("Vorbis error"))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(packet) = packet {
|
|
|
|
self.handle_packet(packet);
|
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-02 15:19:39 +00:00
|
|
|
|
2017-11-28 23:18:12 +00:00
|
|
|
fn start_sink(&mut self) {
|
|
|
|
match self.sink.start() {
|
|
|
|
Ok(()) => self.sink_running = true,
|
|
|
|
Err(err) => error!("Could not start audio: {}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop_sink_if_running(&mut self) {
|
|
|
|
if self.sink_running {
|
|
|
|
self.stop_sink();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop_sink(&mut self) {
|
|
|
|
self.sink.stop().unwrap();
|
|
|
|
self.sink_running = false;
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:08 +00:00
|
|
|
fn handle_packet(&mut self, packet: Option<VorbisPacket>) {
|
2017-01-29 14:11:20 +00:00
|
|
|
match packet {
|
2017-08-03 20:22:08 +00:00
|
|
|
Some(mut packet) => {
|
2017-02-21 22:46:19 +00:00
|
|
|
if let Some(ref editor) = self.audio_filter {
|
2017-08-03 20:22:08 +00:00
|
|
|
editor.modify_stream(&mut packet.data_mut())
|
2017-02-21 22:46:19 +00:00
|
|
|
};
|
2015-09-01 11:20:37 +00:00
|
|
|
|
2017-11-28 23:18:12 +00:00
|
|
|
if let Err(err) = self.sink.write(&packet.data()) {
|
|
|
|
error!("Could not write audio: {}", err);
|
|
|
|
self.stop_sink();
|
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
None => {
|
2017-11-28 23:18:12 +00:00
|
|
|
self.stop_sink();
|
2018-02-20 20:58:02 +00:00
|
|
|
self.state.playing_to_end_of_track();
|
2017-01-29 14:11:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-02 19:42:49 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
fn handle_command(&mut self, cmd: PlayerCommand) {
|
2017-02-09 01:27:52 +00:00
|
|
|
debug!("command={:?}", cmd);
|
2017-01-29 14:11:20 +00:00
|
|
|
match cmd {
|
|
|
|
PlayerCommand::Load(track_id, play, position, end_of_track) => {
|
|
|
|
if self.state.is_playing() {
|
2017-11-28 23:18:12 +00:00
|
|
|
self.stop_sink_if_running();
|
2015-07-02 19:42:49 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
|
|
|
match self.load_track(track_id, position as i64) {
|
|
|
|
Some(decoder) => {
|
|
|
|
if play {
|
2018-02-15 23:16:38 +00:00
|
|
|
match self.state {
|
|
|
|
PlayerState::Playing { track_id: old_track_id, ..}
|
|
|
|
| PlayerState::EndOfTrack { track_id: old_track_id, .. } =>
|
|
|
|
self.send_event(PlayerEvent::Changed {
|
|
|
|
old_track_id: old_track_id,
|
|
|
|
new_track_id: track_id
|
|
|
|
}),
|
|
|
|
_ => self.send_event(PlayerEvent::Started { track_id }),
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:18:12 +00:00
|
|
|
self.start_sink();
|
2017-01-29 14:11:20 +00:00
|
|
|
|
|
|
|
self.state = PlayerState::Playing {
|
2018-02-15 23:16:38 +00:00
|
|
|
track_id: track_id,
|
2017-01-29 14:11:20 +00:00
|
|
|
decoder: decoder,
|
|
|
|
end_of_track: end_of_track,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
self.state = PlayerState::Paused {
|
2018-02-15 23:16:38 +00:00
|
|
|
track_id: track_id,
|
2017-01-29 14:11:20 +00:00
|
|
|
decoder: decoder,
|
|
|
|
end_of_track: end_of_track,
|
|
|
|
};
|
2018-02-20 21:03:21 +00:00
|
|
|
match self.state {
|
|
|
|
PlayerState::Playing { track_id: old_track_id, ..}
|
|
|
|
| PlayerState::EndOfTrack { track_id: old_track_id, .. } =>
|
|
|
|
self.send_event(PlayerEvent::Changed {
|
|
|
|
old_track_id: old_track_id,
|
|
|
|
new_track_id: track_id
|
|
|
|
}),
|
|
|
|
_ => (),
|
|
|
|
}
|
2018-02-15 23:16:38 +00:00
|
|
|
self.send_event(PlayerEvent::Stopped { track_id });
|
2017-01-29 14:11:20 +00:00
|
|
|
}
|
2016-12-07 11:50:46 +00:00
|
|
|
}
|
2016-01-02 15:48:44 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
None => {
|
2018-01-21 18:51:48 +00:00
|
|
|
let _ = end_of_track.send(());
|
2017-01-29 14:11:20 +00:00
|
|
|
}
|
2016-01-02 15:19:39 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
}
|
2016-04-24 13:48:15 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
PlayerCommand::Seek(position) => {
|
|
|
|
if let Some(decoder) = self.state.decoder() {
|
2017-08-03 20:22:08 +00:00
|
|
|
match decoder.seek(position as i64) {
|
2016-12-07 11:50:46 +00:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => error!("Vorbis error: {:?}", err),
|
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
} else {
|
|
|
|
warn!("Player::seek called from invalid state");
|
2016-01-02 15:19:39 +00:00
|
|
|
}
|
2015-07-02 19:42:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
PlayerCommand::Play => {
|
2018-02-15 23:16:38 +00:00
|
|
|
if let PlayerState::Paused { track_id, .. } = self.state {
|
2017-01-29 14:11:20 +00:00
|
|
|
self.state.paused_to_playing();
|
2016-03-14 00:49:21 +00:00
|
|
|
|
2018-02-15 23:16:38 +00:00
|
|
|
self.send_event(PlayerEvent::Started { track_id });
|
2017-11-28 23:18:12 +00:00
|
|
|
self.start_sink();
|
2017-01-29 14:11:20 +00:00
|
|
|
} else {
|
|
|
|
warn!("Player::play called from invalid state");
|
|
|
|
}
|
|
|
|
}
|
2016-04-24 13:48:15 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
PlayerCommand::Pause => {
|
2018-02-15 23:16:38 +00:00
|
|
|
if let PlayerState::Playing { track_id, .. } = self.state {
|
2017-01-29 14:11:20 +00:00
|
|
|
self.state.playing_to_paused();
|
|
|
|
|
2017-11-28 23:18:12 +00:00
|
|
|
self.stop_sink_if_running();
|
2018-02-15 23:16:38 +00:00
|
|
|
self.send_event(PlayerEvent::Stopped { track_id });
|
2017-01-29 14:11:20 +00:00
|
|
|
} else {
|
|
|
|
warn!("Player::pause called from invalid state");
|
|
|
|
}
|
|
|
|
}
|
2016-04-24 13:48:15 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
PlayerCommand::Stop => {
|
|
|
|
match self.state {
|
2018-02-15 23:16:38 +00:00
|
|
|
PlayerState::Playing { track_id, .. }
|
|
|
|
| PlayerState::Paused { track_id, .. }
|
|
|
|
| PlayerState::EndOfTrack { track_id } => {
|
2017-11-28 23:18:12 +00:00
|
|
|
self.stop_sink_if_running();
|
2018-02-15 23:16:38 +00:00
|
|
|
self.send_event(PlayerEvent::Stopped { track_id });
|
2017-01-29 14:11:20 +00:00
|
|
|
self.state = PlayerState::Stopped;
|
|
|
|
},
|
|
|
|
PlayerState::Stopped => {
|
|
|
|
warn!("Player::stop called from invalid state");
|
2015-07-09 21:32:40 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
PlayerState::Invalid => panic!("invalid state"),
|
2015-07-02 19:42:49 +00:00
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
2015-07-09 21:04:19 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2018-02-15 23:16:38 +00:00
|
|
|
fn send_event(&mut self, event: PlayerEvent) {
|
2018-02-20 22:31:33 +00:00
|
|
|
let _ = self.event_sender.unbounded_send(event.clone());
|
2015-07-09 20:08:14 +00:00
|
|
|
}
|
2016-01-20 13:55:36 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
fn find_available_alternative<'a>(&self, track: &'a Track) -> Option<Cow<'a, Track>> {
|
|
|
|
if track.available {
|
|
|
|
Some(Cow::Borrowed(track))
|
|
|
|
} else {
|
|
|
|
let alternatives = track.alternatives
|
|
|
|
.iter()
|
|
|
|
.map(|alt_id| {
|
2017-05-16 00:04:10 +00:00
|
|
|
Track::get(&self.session, *alt_id)
|
2017-01-29 14:11:20 +00:00
|
|
|
});
|
|
|
|
let alternatives = future::join_all(alternatives).wait().unwrap();
|
|
|
|
|
|
|
|
alternatives.into_iter().find(|alt| alt.available).map(Cow::Owned)
|
|
|
|
}
|
2016-01-15 00:12:08 +00:00
|
|
|
}
|
2015-07-09 20:08:14 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
fn load_track(&self, track_id: SpotifyId, position: i64) -> Option<Decoder> {
|
2017-05-16 00:04:10 +00:00
|
|
|
let track = Track::get(&self.session, track_id).wait().unwrap();
|
2017-01-29 14:11:20 +00:00
|
|
|
|
|
|
|
info!("Loading track \"{}\"", track.name);
|
|
|
|
|
|
|
|
let track = match self.find_available_alternative(&track) {
|
|
|
|
Some(track) => track,
|
|
|
|
None => {
|
|
|
|
warn!("Track \"{}\" is not available", track.name);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
2015-07-09 22:09:40 +00:00
|
|
|
|
2017-08-03 18:31:15 +00:00
|
|
|
let format = match self.config.bitrate {
|
2017-01-29 14:11:20 +00:00
|
|
|
Bitrate::Bitrate96 => FileFormat::OGG_VORBIS_96,
|
|
|
|
Bitrate::Bitrate160 => FileFormat::OGG_VORBIS_160,
|
|
|
|
Bitrate::Bitrate320 => FileFormat::OGG_VORBIS_320,
|
|
|
|
};
|
|
|
|
|
|
|
|
let file_id = match track.files.get(&format) {
|
|
|
|
Some(&file_id) => file_id,
|
|
|
|
None => {
|
|
|
|
warn!("Track \"{}\" is not available in format {:?}", track.name, format);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let key = self.session.audio_key().request(track.id, file_id).wait().unwrap();
|
|
|
|
|
2017-05-16 00:04:10 +00:00
|
|
|
let encrypted_file = AudioFile::open(&self.session, file_id).wait().unwrap();
|
2017-01-29 14:11:20 +00:00
|
|
|
let audio_file = Subfile::new(AudioDecrypt::new(key, encrypted_file), 0xa7);
|
2017-05-16 00:04:10 +00:00
|
|
|
|
2017-08-03 20:22:08 +00:00
|
|
|
let mut decoder = VorbisDecoder::new(audio_file).unwrap();
|
2017-01-29 14:11:20 +00:00
|
|
|
|
2017-08-03 20:22:08 +00:00
|
|
|
match decoder.seek(position) {
|
2017-01-29 14:11:20 +00:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => error!("Vorbis error: {:?}", err),
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("Track \"{}\" loaded", track.name);
|
|
|
|
|
|
|
|
Some(decoder)
|
2015-07-09 22:09:40 +00:00
|
|
|
}
|
2015-07-09 20:08:14 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
2017-02-22 04:17:04 +00:00
|
|
|
impl Drop for PlayerInternal {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
debug!("drop Player[{}]", self.session.session_id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 01:27:52 +00:00
|
|
|
impl ::std::fmt::Debug for PlayerCommand {
|
|
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
|
|
|
match *self {
|
|
|
|
PlayerCommand::Load(track, play, position, _) => {
|
|
|
|
f.debug_tuple("Load")
|
|
|
|
.field(&track)
|
|
|
|
.field(&play)
|
|
|
|
.field(&position)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
PlayerCommand::Play => {
|
|
|
|
f.debug_tuple("Play").finish()
|
|
|
|
}
|
|
|
|
PlayerCommand::Pause => {
|
|
|
|
f.debug_tuple("Pause").finish()
|
|
|
|
}
|
|
|
|
PlayerCommand::Stop => {
|
|
|
|
f.debug_tuple("Stop").finish()
|
|
|
|
}
|
|
|
|
PlayerCommand::Seek(position) => {
|
|
|
|
f.debug_tuple("Seek")
|
|
|
|
.field(&position)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-10 15:26:08 +00:00
|
|
|
|
|
|
|
struct Subfile<T: Read + Seek> {
|
|
|
|
stream: T,
|
|
|
|
offset: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Read + Seek> Subfile<T> {
|
|
|
|
pub fn new(mut stream: T, offset: u64) -> Subfile<T> {
|
|
|
|
stream.seek(SeekFrom::Start(offset)).unwrap();
|
|
|
|
Subfile {
|
|
|
|
stream: stream,
|
|
|
|
offset: offset,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Read + Seek> Read for Subfile<T> {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
|
|
|
self.stream.read(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Read + Seek> Seek for Subfile<T> {
|
|
|
|
fn seek(&mut self, mut pos: SeekFrom) -> Result<u64> {
|
|
|
|
pos = match pos {
|
|
|
|
SeekFrom::Start(offset) => SeekFrom::Start(offset + self.offset),
|
|
|
|
x => x,
|
|
|
|
};
|
|
|
|
|
|
|
|
let newpos = try!(self.stream.seek(pos));
|
|
|
|
if newpos > self.offset {
|
|
|
|
Ok(newpos - self.offset)
|
|
|
|
} else {
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|