2019-03-24 14:15:14 +00:00
|
|
|
use std;
|
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
use futures::future;
|
2020-01-31 21:41:11 +00:00
|
|
|
use futures::sync::mpsc;
|
2019-03-28 14:27:50 +00:00
|
|
|
use futures::{Async, Future, Poll, Sink, Stream};
|
2017-01-29 14:11:20 +00:00
|
|
|
use protobuf::{self, Message};
|
2019-03-24 14:15:14 +00:00
|
|
|
use rand;
|
|
|
|
use rand::seq::SliceRandom;
|
|
|
|
use serde_json;
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2019-10-08 09:31:18 +00:00
|
|
|
use crate::context::StationContext;
|
|
|
|
use crate::playback::mixer::Mixer;
|
2020-01-31 21:41:11 +00:00
|
|
|
use crate::playback::player::{Player, PlayerEvent, PlayerEventChannel};
|
2019-10-08 09:31:18 +00:00
|
|
|
use crate::protocol;
|
|
|
|
use crate::protocol::spirc::{DeviceState, Frame, MessageType, PlayStatus, State, TrackRef};
|
2019-09-16 19:00:09 +00:00
|
|
|
use librespot_core::config::ConnectConfig;
|
|
|
|
use librespot_core::mercury::MercuryError;
|
|
|
|
use librespot_core::session::Session;
|
2019-10-22 10:05:35 +00:00
|
|
|
use librespot_core::spotify_id::{SpotifyAudioType, SpotifyId, SpotifyIdError};
|
2019-09-16 19:00:09 +00:00
|
|
|
use librespot_core::util::SeqGenerator;
|
|
|
|
use librespot_core::version;
|
|
|
|
use librespot_core::volume::Volume;
|
2017-11-03 01:15:27 +00:00
|
|
|
|
2020-01-31 21:41:11 +00:00
|
|
|
enum SpircPlayStatus {
|
|
|
|
Stopped,
|
2020-02-02 22:15:15 +00:00
|
|
|
LoadingPlay {
|
|
|
|
position_ms: u32,
|
|
|
|
},
|
|
|
|
LoadingPause {
|
|
|
|
position_ms: u32,
|
|
|
|
},
|
|
|
|
Playing {
|
|
|
|
nominal_start_time: i64,
|
|
|
|
preloading_of_next_track_triggered: bool,
|
|
|
|
},
|
|
|
|
Paused {
|
|
|
|
position_ms: u32,
|
|
|
|
preloading_of_next_track_triggered: bool,
|
|
|
|
},
|
2020-01-31 21:41:11 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
pub struct SpircTask {
|
2016-01-20 14:11:49 +00:00
|
|
|
player: Player,
|
2019-10-08 09:31:18 +00:00
|
|
|
mixer: Box<dyn Mixer>,
|
2019-11-05 19:34:43 +00:00
|
|
|
config: SpircTaskConfig,
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
sequence: SeqGenerator<u32>,
|
2015-07-08 19:50:44 +00:00
|
|
|
|
|
|
|
ident: String,
|
2017-01-20 14:44:13 +00:00
|
|
|
device: DeviceState,
|
2017-01-29 14:11:20 +00:00
|
|
|
state: State,
|
2020-01-31 21:41:11 +00:00
|
|
|
play_request_id: Option<u64>,
|
|
|
|
mixer_started: bool,
|
|
|
|
play_status: SpircPlayStatus,
|
2016-02-13 01:09:15 +00:00
|
|
|
|
2019-10-08 09:31:18 +00:00
|
|
|
subscription: Box<dyn Stream<Item = Frame, Error = MercuryError>>,
|
|
|
|
sender: Box<dyn Sink<SinkItem = Frame, SinkError = MercuryError>>,
|
2017-01-20 12:56:42 +00:00
|
|
|
commands: mpsc::UnboundedReceiver<SpircCommand>,
|
2020-01-31 21:41:11 +00:00
|
|
|
player_events: PlayerEventChannel,
|
2017-01-20 12:56:42 +00:00
|
|
|
|
|
|
|
shutdown: bool,
|
2017-02-22 04:17:04 +00:00
|
|
|
session: Session,
|
2019-10-08 09:31:18 +00:00
|
|
|
context_fut: Box<dyn Future<Item = serde_json::Value, Error = MercuryError>>,
|
|
|
|
autoplay_fut: Box<dyn Future<Item = String, Error = MercuryError>>,
|
2018-10-12 17:15:26 +00:00
|
|
|
context: Option<StationContext>,
|
2017-01-20 12:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum SpircCommand {
|
2017-02-23 11:05:32 +00:00
|
|
|
Play,
|
|
|
|
PlayPause,
|
|
|
|
Pause,
|
|
|
|
Prev,
|
|
|
|
Next,
|
|
|
|
VolumeUp,
|
|
|
|
VolumeDown,
|
2018-02-11 17:52:53 +00:00
|
|
|
Shutdown,
|
2017-01-20 12:56:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 19:34:43 +00:00
|
|
|
struct SpircTaskConfig {
|
|
|
|
linear_volume: bool,
|
|
|
|
autoplay: bool,
|
|
|
|
}
|
|
|
|
|
2019-03-16 15:18:38 +00:00
|
|
|
const CONTEXT_TRACKS_HISTORY: usize = 10;
|
|
|
|
const CONTEXT_FETCH_THRESHOLD: u32 = 5;
|
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
pub struct Spirc {
|
|
|
|
commands: mpsc::UnboundedSender<SpircCommand>,
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
fn initial_state() -> State {
|
2018-02-17 09:15:09 +00:00
|
|
|
let mut frame = protocol::spirc::State::new();
|
|
|
|
frame.set_repeat(false);
|
|
|
|
frame.set_shuffle(false);
|
|
|
|
frame.set_status(PlayStatus::kPlayStatusStop);
|
|
|
|
frame.set_position_ms(0);
|
|
|
|
frame.set_position_measured_at(0);
|
|
|
|
frame
|
2017-01-29 14:11:20 +00:00
|
|
|
}
|
2018-02-17 09:15:09 +00:00
|
|
|
|
2018-05-17 01:15:17 +00:00
|
|
|
fn initial_device_state(config: ConnectConfig) -> DeviceState {
|
2018-02-16 21:04:37 +00:00
|
|
|
{
|
|
|
|
let mut msg = DeviceState::new();
|
|
|
|
msg.set_sw_version(version::version_string());
|
|
|
|
msg.set_is_active(false);
|
|
|
|
msg.set_can_play(true);
|
2018-05-17 01:15:17 +00:00
|
|
|
msg.set_volume(0);
|
2018-02-16 21:04:37 +00:00
|
|
|
msg.set_name(config.name);
|
|
|
|
{
|
|
|
|
let repeated = msg.mut_capabilities();
|
|
|
|
{
|
|
|
|
let msg = repeated.push_default();
|
|
|
|
msg.set_typ(protocol::spirc::CapabilityType::kCanBePlayer);
|
|
|
|
{
|
|
|
|
let repeated = msg.mut_intValue();
|
|
|
|
repeated.push(1)
|
|
|
|
};
|
|
|
|
msg
|
|
|
|
};
|
|
|
|
{
|
|
|
|
let msg = repeated.push_default();
|
|
|
|
msg.set_typ(protocol::spirc::CapabilityType::kDeviceType);
|
|
|
|
{
|
|
|
|
let repeated = msg.mut_intValue();
|
|
|
|
repeated.push(config.device_type as i64)
|
|
|
|
};
|
|
|
|
msg
|
|
|
|
};
|
|
|
|
{
|
|
|
|
let msg = repeated.push_default();
|
|
|
|
msg.set_typ(protocol::spirc::CapabilityType::kGaiaEqConnectId);
|
|
|
|
{
|
|
|
|
let repeated = msg.mut_intValue();
|
|
|
|
repeated.push(1)
|
|
|
|
};
|
|
|
|
msg
|
|
|
|
};
|
|
|
|
{
|
|
|
|
let msg = repeated.push_default();
|
|
|
|
msg.set_typ(protocol::spirc::CapabilityType::kSupportsLogout);
|
|
|
|
{
|
|
|
|
let repeated = msg.mut_intValue();
|
|
|
|
repeated.push(0)
|
|
|
|
};
|
|
|
|
msg
|
|
|
|
};
|
|
|
|
{
|
|
|
|
let msg = repeated.push_default();
|
|
|
|
msg.set_typ(protocol::spirc::CapabilityType::kIsObservable);
|
|
|
|
{
|
|
|
|
let repeated = msg.mut_intValue();
|
|
|
|
repeated.push(1)
|
|
|
|
};
|
|
|
|
msg
|
|
|
|
};
|
|
|
|
{
|
|
|
|
let msg = repeated.push_default();
|
|
|
|
msg.set_typ(protocol::spirc::CapabilityType::kVolumeSteps);
|
|
|
|
{
|
|
|
|
let repeated = msg.mut_intValue();
|
|
|
|
repeated.push(64)
|
|
|
|
};
|
|
|
|
msg
|
|
|
|
};
|
2018-10-12 17:15:26 +00:00
|
|
|
{
|
|
|
|
let msg = repeated.push_default();
|
|
|
|
msg.set_typ(protocol::spirc::CapabilityType::kSupportsPlaylistV2);
|
|
|
|
{
|
|
|
|
let repeated = msg.mut_intValue();
|
|
|
|
repeated.push(64)
|
|
|
|
};
|
|
|
|
msg
|
|
|
|
};
|
2018-02-16 21:04:37 +00:00
|
|
|
{
|
|
|
|
let msg = repeated.push_default();
|
|
|
|
msg.set_typ(protocol::spirc::CapabilityType::kSupportedContexts);
|
|
|
|
{
|
|
|
|
let repeated = msg.mut_stringValue();
|
|
|
|
repeated.push(::std::convert::Into::into("album"));
|
|
|
|
repeated.push(::std::convert::Into::into("playlist"));
|
|
|
|
repeated.push(::std::convert::Into::into("search"));
|
|
|
|
repeated.push(::std::convert::Into::into("inbox"));
|
|
|
|
repeated.push(::std::convert::Into::into("toplist"));
|
|
|
|
repeated.push(::std::convert::Into::into("starred"));
|
|
|
|
repeated.push(::std::convert::Into::into("publishedstarred"));
|
|
|
|
repeated.push(::std::convert::Into::into("track"))
|
|
|
|
};
|
|
|
|
msg
|
|
|
|
};
|
|
|
|
{
|
|
|
|
let msg = repeated.push_default();
|
|
|
|
msg.set_typ(protocol::spirc::CapabilityType::kSupportedTypes);
|
|
|
|
{
|
|
|
|
let repeated = msg.mut_stringValue();
|
|
|
|
repeated.push(::std::convert::Into::into("audio/local"));
|
|
|
|
repeated.push(::std::convert::Into::into("audio/track"));
|
2018-09-28 18:10:22 +00:00
|
|
|
repeated.push(::std::convert::Into::into("audio/episode"));
|
2018-02-16 21:04:37 +00:00
|
|
|
repeated.push(::std::convert::Into::into("local"));
|
|
|
|
repeated.push(::std::convert::Into::into("track"))
|
|
|
|
};
|
|
|
|
msg
|
|
|
|
};
|
|
|
|
};
|
|
|
|
msg
|
|
|
|
}
|
2017-01-20 14:44:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 10:27:28 +00:00
|
|
|
fn calc_logarithmic_volume(volume: u16) -> u16 {
|
2018-01-29 22:37:30 +00:00
|
|
|
// Volume conversion taken from https://www.dr-lex.be/info-stuff/volumecontrols.html#ideal2
|
|
|
|
// Convert the given volume [0..0xffff] to a dB gain
|
|
|
|
// We assume a dB range of 60dB.
|
2018-10-12 17:15:26 +00:00
|
|
|
// Use the equation: a * exp(b * x)
|
2018-01-29 22:37:30 +00:00
|
|
|
// in which a = IDEAL_FACTOR, b = 1/1000
|
|
|
|
const IDEAL_FACTOR: f64 = 6.908;
|
|
|
|
let normalized_volume = volume as f64 / std::u16::MAX as f64; // To get a value between 0 and 1
|
|
|
|
|
|
|
|
let mut val = std::u16::MAX;
|
|
|
|
// Prevent val > std::u16::MAX due to rounding errors
|
2018-02-09 01:05:50 +00:00
|
|
|
if normalized_volume < 0.999 {
|
2018-01-29 22:37:30 +00:00
|
|
|
let new_volume = (normalized_volume * IDEAL_FACTOR).exp() / 1000.0;
|
|
|
|
val = (new_volume * std::u16::MAX as f64) as u16;
|
2018-01-25 22:37:28 +00:00
|
|
|
}
|
2018-01-29 22:37:30 +00:00
|
|
|
|
2018-02-09 01:05:50 +00:00
|
|
|
debug!("input volume:{} to mixer: {}", volume, val);
|
2018-01-29 22:37:30 +00:00
|
|
|
|
|
|
|
// return the scale factor (0..0xffff) (equivalent to a voltage multiplier).
|
|
|
|
val
|
2018-01-25 22:37:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 10:27:28 +00:00
|
|
|
fn volume_to_mixer(volume: u16, linear_volume: bool) -> u16 {
|
|
|
|
if linear_volume {
|
|
|
|
debug!("linear volume: {}", volume);
|
|
|
|
volume
|
|
|
|
} else {
|
|
|
|
calc_logarithmic_volume(volume)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
impl Spirc {
|
2018-02-11 17:52:53 +00:00
|
|
|
pub fn new(
|
|
|
|
config: ConnectConfig,
|
|
|
|
session: Session,
|
|
|
|
player: Player,
|
2019-10-08 09:31:18 +00:00
|
|
|
mixer: Box<dyn Mixer>,
|
2018-02-11 17:52:53 +00:00
|
|
|
) -> (Spirc, SpircTask) {
|
2017-02-22 04:17:04 +00:00
|
|
|
debug!("new Spirc[{}]", session.session_id());
|
|
|
|
|
2016-03-17 03:06:56 +00:00
|
|
|
let ident = session.device_id().to_owned();
|
2016-01-01 23:16:12 +00:00
|
|
|
|
2019-02-22 13:09:01 +00:00
|
|
|
// Uri updated in response to issue #288
|
|
|
|
let uri = format!("hm://remote/user/{}/", session.username());
|
2017-01-20 12:56:42 +00:00
|
|
|
|
|
|
|
let subscription = session.mercury().subscribe(&uri as &str);
|
2018-02-11 17:52:53 +00:00
|
|
|
let subscription = subscription
|
|
|
|
.map(|stream| stream.map_err(|_| MercuryError))
|
|
|
|
.flatten_stream();
|
2018-01-21 19:29:31 +00:00
|
|
|
let subscription = Box::new(subscription.map(|response| -> Frame {
|
2017-01-20 12:56:42 +00:00
|
|
|
let data = response.payload.first().unwrap();
|
|
|
|
protobuf::parse_from_bytes(data).unwrap()
|
2018-01-21 19:29:31 +00:00
|
|
|
}));
|
2017-01-20 12:56:42 +00:00
|
|
|
|
2018-02-11 17:52:53 +00:00
|
|
|
let sender = Box::new(
|
|
|
|
session
|
|
|
|
.mercury()
|
|
|
|
.sender(uri)
|
|
|
|
.with(|frame: Frame| Ok(frame.write_to_bytes().unwrap())),
|
|
|
|
);
|
2017-01-20 12:56:42 +00:00
|
|
|
|
|
|
|
let (cmd_tx, cmd_rx) = mpsc::unbounded();
|
|
|
|
|
2018-05-17 01:15:17 +00:00
|
|
|
let volume = config.volume;
|
2019-11-05 19:34:43 +00:00
|
|
|
let task_config = SpircTaskConfig {
|
|
|
|
linear_volume: config.linear_volume,
|
|
|
|
autoplay: config.autoplay,
|
|
|
|
};
|
2018-05-17 01:15:17 +00:00
|
|
|
let device = initial_device_state(config);
|
2017-01-20 14:44:13 +00:00
|
|
|
|
2020-01-31 21:41:11 +00:00
|
|
|
let player_events = player.get_player_event_channel();
|
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
let mut task = SpircTask {
|
2016-01-20 14:11:49 +00:00
|
|
|
player: player,
|
2017-02-21 22:46:19 +00:00
|
|
|
mixer: mixer,
|
2019-11-05 19:34:43 +00:00
|
|
|
config: task_config,
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
sequence: SeqGenerator::new(1),
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2016-01-01 23:16:12 +00:00
|
|
|
ident: ident,
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
device: device,
|
|
|
|
state: initial_state(),
|
2020-01-31 21:41:11 +00:00
|
|
|
play_request_id: None,
|
|
|
|
mixer_started: false,
|
|
|
|
play_status: SpircPlayStatus::Stopped,
|
2016-02-13 01:09:15 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
subscription: subscription,
|
|
|
|
sender: sender,
|
|
|
|
commands: cmd_rx,
|
2020-01-31 21:41:11 +00:00
|
|
|
player_events: player_events,
|
2016-01-20 15:47:05 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
shutdown: false,
|
2017-02-22 04:17:04 +00:00
|
|
|
session: session.clone(),
|
2018-10-12 17:15:26 +00:00
|
|
|
|
|
|
|
context_fut: Box::new(future::empty()),
|
2019-03-24 15:42:00 +00:00
|
|
|
autoplay_fut: Box::new(future::empty()),
|
2018-10-12 17:15:26 +00:00
|
|
|
context: None,
|
2016-01-20 15:47:05 +00:00
|
|
|
};
|
|
|
|
|
2018-05-17 01:15:17 +00:00
|
|
|
task.set_volume(volume);
|
|
|
|
|
2018-02-11 17:52:53 +00:00
|
|
|
let spirc = Spirc { commands: cmd_tx };
|
2016-01-20 15:47:05 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
task.hello();
|
2016-02-16 21:26:51 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
(spirc, task)
|
2016-02-16 21:26:51 +00:00
|
|
|
}
|
2016-02-16 21:52:55 +00:00
|
|
|
|
2017-02-23 11:05:32 +00:00
|
|
|
pub fn play(&self) {
|
2018-01-21 19:38:30 +00:00
|
|
|
let _ = self.commands.unbounded_send(SpircCommand::Play);
|
2017-02-23 11:05:32 +00:00
|
|
|
}
|
|
|
|
pub fn play_pause(&self) {
|
2018-01-21 19:38:30 +00:00
|
|
|
let _ = self.commands.unbounded_send(SpircCommand::PlayPause);
|
2017-02-23 11:05:32 +00:00
|
|
|
}
|
|
|
|
pub fn pause(&self) {
|
2018-01-21 19:38:30 +00:00
|
|
|
let _ = self.commands.unbounded_send(SpircCommand::Pause);
|
2017-02-23 11:05:32 +00:00
|
|
|
}
|
|
|
|
pub fn prev(&self) {
|
2018-01-21 19:38:30 +00:00
|
|
|
let _ = self.commands.unbounded_send(SpircCommand::Prev);
|
2017-02-23 11:05:32 +00:00
|
|
|
}
|
|
|
|
pub fn next(&self) {
|
2018-01-21 19:38:30 +00:00
|
|
|
let _ = self.commands.unbounded_send(SpircCommand::Next);
|
2017-02-23 11:05:32 +00:00
|
|
|
}
|
|
|
|
pub fn volume_up(&self) {
|
2018-01-21 19:38:30 +00:00
|
|
|
let _ = self.commands.unbounded_send(SpircCommand::VolumeUp);
|
2017-02-23 11:05:32 +00:00
|
|
|
}
|
|
|
|
pub fn volume_down(&self) {
|
2018-01-21 19:38:30 +00:00
|
|
|
let _ = self.commands.unbounded_send(SpircCommand::VolumeDown);
|
2017-02-23 11:05:32 +00:00
|
|
|
}
|
2017-01-29 16:25:09 +00:00
|
|
|
pub fn shutdown(&self) {
|
2018-01-21 19:38:30 +00:00
|
|
|
let _ = self.commands.unbounded_send(SpircCommand::Shutdown);
|
2016-02-16 21:52:55 +00:00
|
|
|
}
|
2017-01-20 12:56:42 +00:00
|
|
|
}
|
2016-02-16 21:52:55 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
impl Future for SpircTask {
|
|
|
|
type Item = ();
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<(), ()> {
|
|
|
|
loop {
|
|
|
|
let mut progress = false;
|
|
|
|
|
2018-04-21 15:46:29 +00:00
|
|
|
if self.session.is_invalid() {
|
|
|
|
return Ok(Async::Ready(()));
|
|
|
|
}
|
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
if !self.shutdown {
|
|
|
|
match self.subscription.poll().unwrap() {
|
|
|
|
Async::Ready(Some(frame)) => {
|
|
|
|
progress = true;
|
|
|
|
self.handle_frame(frame);
|
|
|
|
}
|
2020-01-23 23:12:16 +00:00
|
|
|
Async::Ready(None) => {
|
|
|
|
error!("subscription terminated");
|
|
|
|
self.shutdown = true;
|
|
|
|
self.commands.close();
|
2020-01-24 01:26:16 +00:00
|
|
|
}
|
2017-01-20 12:56:42 +00:00
|
|
|
Async::NotReady => (),
|
|
|
|
}
|
2016-02-16 21:52:55 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
match self.commands.poll().unwrap() {
|
|
|
|
Async::Ready(Some(command)) => {
|
|
|
|
progress = true;
|
|
|
|
self.handle_command(command);
|
|
|
|
}
|
|
|
|
Async::Ready(None) => (),
|
|
|
|
Async::NotReady => (),
|
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
2020-01-31 21:41:11 +00:00
|
|
|
match self.player_events.poll() {
|
|
|
|
Ok(Async::NotReady) => (),
|
|
|
|
Ok(Async::Ready(None)) => (),
|
|
|
|
Err(_) => (),
|
|
|
|
Ok(Async::Ready(Some(event))) => {
|
2017-01-29 14:11:20 +00:00
|
|
|
progress = true;
|
2020-01-31 21:41:11 +00:00
|
|
|
self.handle_player_event(event);
|
2017-01-29 14:11:20 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-24 15:42:00 +00:00
|
|
|
// TODO: Refactor
|
2018-10-12 17:15:26 +00:00
|
|
|
match self.context_fut.poll() {
|
|
|
|
Ok(Async::Ready(value)) => {
|
2019-03-13 19:35:46 +00:00
|
|
|
let r_context = serde_json::from_value::<StationContext>(value.clone());
|
|
|
|
self.context = match r_context {
|
|
|
|
Ok(context) => {
|
|
|
|
info!(
|
|
|
|
"Resolved {:?} tracks from <{:?}>",
|
|
|
|
context.tracks.len(),
|
|
|
|
self.state.get_context_uri(),
|
|
|
|
);
|
|
|
|
Some(context)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("Unable to parse JSONContext {:?}\n{:?}", e, value);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// It needn't be so verbose - can be as simple as
|
|
|
|
// if let Some(ref context) = r_context {
|
|
|
|
// info!("Got {:?} tracks from <{}>", context.tracks.len(), context.uri);
|
|
|
|
// }
|
|
|
|
// self.context = r_context;
|
2018-10-12 17:15:26 +00:00
|
|
|
|
|
|
|
progress = true;
|
|
|
|
self.context_fut = Box::new(future::empty());
|
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => (),
|
|
|
|
Err(err) => {
|
|
|
|
self.context_fut = Box::new(future::empty());
|
2019-03-13 19:35:46 +00:00
|
|
|
error!("ContextError: {:?}", err)
|
2018-10-12 17:15:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-24 15:42:00 +00:00
|
|
|
|
|
|
|
match self.autoplay_fut.poll() {
|
|
|
|
Ok(Async::Ready(autoplay_station_uri)) => {
|
2019-11-05 19:34:43 +00:00
|
|
|
info!("Autoplay uri resolved to <{:?}>", autoplay_station_uri);
|
2019-03-24 15:42:00 +00:00
|
|
|
self.context_fut = self.resolve_station(&autoplay_station_uri);
|
|
|
|
progress = true;
|
|
|
|
self.autoplay_fut = Box::new(future::empty());
|
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => (),
|
|
|
|
Err(err) => {
|
|
|
|
self.autoplay_fut = Box::new(future::empty());
|
|
|
|
error!("AutoplayError: {:?}", err)
|
|
|
|
}
|
|
|
|
}
|
2017-01-20 12:56:42 +00:00
|
|
|
}
|
2016-02-17 14:24:40 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
let poll_sender = self.sender.poll_complete().unwrap();
|
2016-02-17 19:35:31 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
// Only shutdown once we've flushed out all our messages
|
|
|
|
if self.shutdown && poll_sender.is_ready() {
|
|
|
|
return Ok(Async::Ready(()));
|
|
|
|
}
|
2016-02-17 19:35:52 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
if !progress {
|
|
|
|
return Ok(Async::NotReady);
|
|
|
|
}
|
|
|
|
}
|
2016-02-17 19:35:52 +00:00
|
|
|
}
|
2016-01-20 15:47:05 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
impl SpircTask {
|
2019-03-24 14:15:14 +00:00
|
|
|
fn now_ms(&mut self) -> i64 {
|
|
|
|
let dur = match SystemTime::now().duration_since(UNIX_EPOCH) {
|
|
|
|
Ok(dur) => dur,
|
|
|
|
Err(err) => err.duration(),
|
|
|
|
};
|
2019-03-25 19:27:30 +00:00
|
|
|
((dur.as_secs() as i64 + self.session.time_delta()) * 1000
|
|
|
|
+ (dur.subsec_nanos() / 1000_000) as i64)
|
2019-03-24 14:15:14 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 21:41:11 +00:00
|
|
|
fn ensure_mixer_started(&mut self) {
|
|
|
|
if !self.mixer_started {
|
|
|
|
self.mixer.start();
|
|
|
|
self.mixer_started = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ensure_mixer_stopped(&mut self) {
|
|
|
|
if self.mixer_started {
|
|
|
|
self.mixer.stop();
|
|
|
|
self.mixer_started = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_state_position(&mut self, position_ms: u32) {
|
|
|
|
let now = self.now_ms();
|
|
|
|
self.state.set_position_measured_at(now as u64);
|
|
|
|
self.state.set_position_ms(position_ms);
|
|
|
|
}
|
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
fn handle_command(&mut self, cmd: SpircCommand) {
|
2017-02-23 11:05:32 +00:00
|
|
|
let active = self.device.get_is_active();
|
2017-01-20 12:56:42 +00:00
|
|
|
match cmd {
|
2017-02-23 11:05:32 +00:00
|
|
|
SpircCommand::Play => {
|
|
|
|
if active {
|
|
|
|
self.handle_play();
|
|
|
|
self.notify(None);
|
|
|
|
} else {
|
|
|
|
CommandSender::new(self, MessageType::kMessageTypePlay).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SpircCommand::PlayPause => {
|
|
|
|
if active {
|
|
|
|
self.handle_play_pause();
|
|
|
|
self.notify(None);
|
|
|
|
} else {
|
|
|
|
CommandSender::new(self, MessageType::kMessageTypePlayPause).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SpircCommand::Pause => {
|
|
|
|
if active {
|
|
|
|
self.handle_pause();
|
|
|
|
self.notify(None);
|
|
|
|
} else {
|
|
|
|
CommandSender::new(self, MessageType::kMessageTypePause).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SpircCommand::Prev => {
|
|
|
|
if active {
|
|
|
|
self.handle_prev();
|
|
|
|
self.notify(None);
|
|
|
|
} else {
|
|
|
|
CommandSender::new(self, MessageType::kMessageTypePrev).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SpircCommand::Next => {
|
|
|
|
if active {
|
|
|
|
self.handle_next();
|
|
|
|
self.notify(None);
|
|
|
|
} else {
|
|
|
|
CommandSender::new(self, MessageType::kMessageTypeNext).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SpircCommand::VolumeUp => {
|
|
|
|
if active {
|
|
|
|
self.handle_volume_up();
|
|
|
|
self.notify(None);
|
|
|
|
} else {
|
|
|
|
CommandSender::new(self, MessageType::kMessageTypeVolumeUp).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SpircCommand::VolumeDown => {
|
|
|
|
if active {
|
|
|
|
self.handle_volume_down();
|
|
|
|
self.notify(None);
|
|
|
|
} else {
|
|
|
|
CommandSender::new(self, MessageType::kMessageTypeVolumeDown).send();
|
|
|
|
}
|
|
|
|
}
|
2017-01-20 12:56:42 +00:00
|
|
|
SpircCommand::Shutdown => {
|
|
|
|
CommandSender::new(self, MessageType::kMessageTypeGoodbye).send();
|
|
|
|
self.shutdown = true;
|
|
|
|
self.commands.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 21:41:11 +00:00
|
|
|
fn handle_player_event(&mut self, event: PlayerEvent) {
|
|
|
|
// we only process events if the play_request_id matches. If it doesn't, it is
|
|
|
|
// an event that belongs to a previous track and only arrives now due to a race
|
|
|
|
// condition. In this case we have updated the state already and don't want to
|
|
|
|
// mess with it.
|
|
|
|
if let Some(play_request_id) = event.get_play_request_id() {
|
|
|
|
if Some(play_request_id) == self.play_request_id {
|
|
|
|
match event {
|
|
|
|
PlayerEvent::EndOfTrack { .. } => self.handle_end_of_track(),
|
|
|
|
PlayerEvent::Loading { .. } => (),
|
|
|
|
PlayerEvent::Playing { position_ms, .. } => {
|
2020-02-02 00:07:05 +00:00
|
|
|
let new_nominal_start_time = self.now_ms() - position_ms as i64;
|
2020-01-31 21:41:11 +00:00
|
|
|
match self.play_status {
|
2020-02-02 22:15:15 +00:00
|
|
|
SpircPlayStatus::Playing {
|
|
|
|
ref mut nominal_start_time,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
if (*nominal_start_time - new_nominal_start_time).abs() > 100 {
|
|
|
|
*nominal_start_time = new_nominal_start_time;
|
2020-01-31 21:41:11 +00:00
|
|
|
self.update_state_position(position_ms);
|
|
|
|
self.notify(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SpircPlayStatus::LoadingPlay { .. }
|
|
|
|
| SpircPlayStatus::LoadingPause { .. } => {
|
|
|
|
self.state.set_status(PlayStatus::kPlayStatusPlay);
|
|
|
|
self.update_state_position(position_ms);
|
|
|
|
self.notify(None);
|
|
|
|
self.play_status = SpircPlayStatus::Playing {
|
|
|
|
nominal_start_time: new_nominal_start_time,
|
2020-02-02 22:15:15 +00:00
|
|
|
preloading_of_next_track_triggered: false,
|
2020-01-31 21:41:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
trace!("==> kPlayStatusPlay");
|
|
|
|
}
|
|
|
|
PlayerEvent::Paused {
|
|
|
|
position_ms: new_position_ms,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
match self.play_status {
|
|
|
|
SpircPlayStatus::Paused {
|
|
|
|
ref mut position_ms,
|
2020-02-02 22:15:15 +00:00
|
|
|
..
|
2020-01-31 21:41:11 +00:00
|
|
|
} => {
|
|
|
|
if *position_ms != new_position_ms {
|
|
|
|
*position_ms = new_position_ms;
|
|
|
|
self.update_state_position(new_position_ms);
|
|
|
|
self.notify(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SpircPlayStatus::LoadingPlay { .. }
|
|
|
|
| SpircPlayStatus::LoadingPause { .. } => {
|
|
|
|
self.state.set_status(PlayStatus::kPlayStatusPause);
|
|
|
|
self.update_state_position(new_position_ms);
|
|
|
|
self.notify(None);
|
|
|
|
self.play_status = SpircPlayStatus::Paused {
|
|
|
|
position_ms: new_position_ms,
|
2020-02-02 22:15:15 +00:00
|
|
|
preloading_of_next_track_triggered: false,
|
2020-01-31 21:41:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
trace!("==> kPlayStatusPause");
|
|
|
|
}
|
|
|
|
PlayerEvent::Stopped { .. } => match self.play_status {
|
|
|
|
SpircPlayStatus::Stopped => (),
|
|
|
|
_ => {
|
|
|
|
warn!("The player has stopped unexpentedly.");
|
|
|
|
self.state.set_status(PlayStatus::kPlayStatusStop);
|
|
|
|
self.ensure_mixer_stopped();
|
|
|
|
self.notify(None);
|
|
|
|
self.play_status = SpircPlayStatus::Stopped;
|
|
|
|
}
|
|
|
|
},
|
2020-02-02 00:07:05 +00:00
|
|
|
PlayerEvent::TimeToPreloadNextTrack { .. } => match self.play_status {
|
2020-02-02 22:15:15 +00:00
|
|
|
SpircPlayStatus::Paused {
|
|
|
|
ref mut preloading_of_next_track_triggered,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
| SpircPlayStatus::Playing {
|
|
|
|
ref mut preloading_of_next_track_triggered,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
*preloading_of_next_track_triggered = true;
|
2020-01-31 21:41:11 +00:00
|
|
|
if let Some(track_id) = self.preview_next_track() {
|
|
|
|
self.player.preload(track_id);
|
|
|
|
}
|
|
|
|
}
|
2020-02-02 22:15:15 +00:00
|
|
|
SpircPlayStatus::LoadingPause { .. }
|
|
|
|
| SpircPlayStatus::LoadingPlay { .. }
|
|
|
|
| SpircPlayStatus::Stopped => (),
|
2020-02-02 00:07:05 +00:00
|
|
|
},
|
2020-01-31 21:41:11 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
fn handle_frame(&mut self, frame: Frame) {
|
2020-01-31 21:41:11 +00:00
|
|
|
let state_string = match frame.get_state().get_status() {
|
|
|
|
PlayStatus::kPlayStatusLoading => "kPlayStatusLoading",
|
|
|
|
PlayStatus::kPlayStatusPause => "kPlayStatusPause",
|
|
|
|
PlayStatus::kPlayStatusStop => "kPlayStatusStop",
|
|
|
|
PlayStatus::kPlayStatusPlay => "kPlayStatusPlay",
|
|
|
|
};
|
|
|
|
|
2018-02-11 17:52:53 +00:00
|
|
|
debug!(
|
2020-01-31 21:41:11 +00:00
|
|
|
"{:?} {:?} {} {} {} {}",
|
2018-02-11 17:52:53 +00:00
|
|
|
frame.get_typ(),
|
|
|
|
frame.get_device_state().get_name(),
|
|
|
|
frame.get_ident(),
|
|
|
|
frame.get_seq_nr(),
|
2020-01-31 21:41:11 +00:00
|
|
|
frame.get_state_update_id(),
|
|
|
|
state_string,
|
2018-02-11 17:52:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if frame.get_ident() == self.ident
|
|
|
|
|| (frame.get_recipient().len() > 0 && !frame.get_recipient().contains(&self.ident))
|
|
|
|
{
|
2016-01-20 15:47:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-08 19:50:44 +00:00
|
|
|
match frame.get_typ() {
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypeHello => {
|
2017-01-29 14:11:20 +00:00
|
|
|
self.notify(Some(frame.get_ident()));
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypeLoad => {
|
2017-01-20 14:44:13 +00:00
|
|
|
if !self.device.get_is_active() {
|
2019-03-24 14:15:14 +00:00
|
|
|
let now = self.now_ms();
|
2017-01-20 14:44:13 +00:00
|
|
|
self.device.set_is_active(true);
|
2019-03-24 14:15:14 +00:00
|
|
|
self.device.set_became_active_at(now);
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
self.update_tracks(&frame);
|
|
|
|
|
|
|
|
if self.state.get_track().len() > 0 {
|
2020-01-31 21:41:11 +00:00
|
|
|
let start_playing =
|
|
|
|
frame.get_state().get_status() == PlayStatus::kPlayStatusPlay;
|
|
|
|
self.load_track(start_playing, frame.get_state().get_position_ms());
|
2016-08-23 21:43:55 +00:00
|
|
|
} else {
|
2018-10-12 17:15:26 +00:00
|
|
|
info!("No more tracks left in queue");
|
2017-01-29 14:11:20 +00:00
|
|
|
self.state.set_status(PlayStatus::kPlayStatusStop);
|
2020-01-23 08:05:10 +00:00
|
|
|
self.player.stop();
|
|
|
|
self.mixer.stop();
|
2020-01-31 21:41:11 +00:00
|
|
|
self.play_status = SpircPlayStatus::Stopped;
|
2016-08-23 21:43:55 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
|
|
|
self.notify(None);
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypePlay => {
|
2017-02-23 11:05:32 +00:00
|
|
|
self.handle_play();
|
|
|
|
self.notify(None);
|
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
2017-02-23 11:05:32 +00:00
|
|
|
MessageType::kMessageTypePlayPause => {
|
|
|
|
self.handle_play_pause();
|
2017-01-29 14:11:20 +00:00
|
|
|
self.notify(None);
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypePause => {
|
2017-02-23 11:05:32 +00:00
|
|
|
self.handle_pause();
|
2017-01-29 14:11:20 +00:00
|
|
|
self.notify(None);
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypeNext => {
|
2017-02-23 11:05:32 +00:00
|
|
|
self.handle_next();
|
2017-01-29 14:11:20 +00:00
|
|
|
self.notify(None);
|
2016-01-14 03:27:34 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypePrev => {
|
2017-02-23 11:05:32 +00:00
|
|
|
self.handle_prev();
|
|
|
|
self.notify(None);
|
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
2017-02-23 11:05:32 +00:00
|
|
|
MessageType::kMessageTypeVolumeUp => {
|
|
|
|
self.handle_volume_up();
|
|
|
|
self.notify(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageType::kMessageTypeVolumeDown => {
|
|
|
|
self.handle_volume_down();
|
2017-01-29 14:11:20 +00:00
|
|
|
self.notify(None);
|
2016-01-14 03:27:34 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
2017-10-27 17:45:02 +00:00
|
|
|
MessageType::kMessageTypeRepeat => {
|
|
|
|
self.state.set_repeat(frame.get_state().get_repeat());
|
|
|
|
self.notify(None);
|
|
|
|
}
|
|
|
|
|
2017-11-03 01:15:27 +00:00
|
|
|
MessageType::kMessageTypeShuffle => {
|
|
|
|
self.state.set_shuffle(frame.get_state().get_shuffle());
|
2018-02-11 17:52:53 +00:00
|
|
|
if self.state.get_shuffle() {
|
2017-11-03 01:15:27 +00:00
|
|
|
let current_index = self.state.get_playing_track_index();
|
|
|
|
{
|
|
|
|
let tracks = self.state.mut_track();
|
|
|
|
tracks.swap(0, current_index as usize);
|
|
|
|
if let Some((_, rest)) = tracks.split_first_mut() {
|
2019-03-10 16:48:19 +00:00
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
rest.shuffle(&mut rng);
|
2017-11-03 01:15:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.state.set_playing_track_index(0);
|
|
|
|
} else {
|
|
|
|
let context = self.state.get_context_uri();
|
|
|
|
debug!("{:?}", context);
|
|
|
|
}
|
|
|
|
self.notify(None);
|
|
|
|
}
|
|
|
|
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypeSeek => {
|
2020-01-31 21:41:11 +00:00
|
|
|
self.handle_seek(frame.get_position());
|
2017-01-29 14:11:20 +00:00
|
|
|
self.notify(None);
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypeReplace => {
|
2017-01-29 14:11:20 +00:00
|
|
|
self.update_tracks(&frame);
|
|
|
|
self.notify(None);
|
2020-02-02 22:15:15 +00:00
|
|
|
|
|
|
|
if let SpircPlayStatus::Playing {
|
|
|
|
preloading_of_next_track_triggered,
|
|
|
|
..
|
|
|
|
}
|
|
|
|
| SpircPlayStatus::Paused {
|
|
|
|
preloading_of_next_track_triggered,
|
|
|
|
..
|
|
|
|
} = self.play_status
|
|
|
|
{
|
|
|
|
if preloading_of_next_track_triggered {
|
|
|
|
if let Some(track_id) = self.preview_next_track() {
|
|
|
|
self.player.preload(track_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-16 01:15:24 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
|
|
|
MessageType::kMessageTypeVolume => {
|
2018-05-17 01:15:17 +00:00
|
|
|
self.set_volume(frame.get_volume() as u16);
|
2017-01-29 14:11:20 +00:00
|
|
|
self.notify(None);
|
|
|
|
}
|
|
|
|
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypeNotify => {
|
2018-02-11 17:52:53 +00:00
|
|
|
if self.device.get_is_active() && frame.get_device_state().get_is_active() {
|
2017-01-20 14:44:13 +00:00
|
|
|
self.device.set_is_active(false);
|
2017-01-29 14:11:20 +00:00
|
|
|
self.state.set_status(PlayStatus::kPlayStatusStop);
|
2016-01-20 14:11:49 +00:00
|
|
|
self.player.stop();
|
2020-01-31 21:41:11 +00:00
|
|
|
self.ensure_mixer_stopped();
|
|
|
|
self.play_status = SpircPlayStatus::Stopped;
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
2016-01-02 15:19:39 +00:00
|
|
|
_ => (),
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 11:05:32 +00:00
|
|
|
fn handle_play(&mut self) {
|
2020-01-31 21:41:11 +00:00
|
|
|
match self.play_status {
|
2020-02-02 22:15:15 +00:00
|
|
|
SpircPlayStatus::Paused {
|
|
|
|
position_ms,
|
|
|
|
preloading_of_next_track_triggered,
|
|
|
|
} => {
|
2020-01-31 21:41:11 +00:00
|
|
|
self.ensure_mixer_started();
|
|
|
|
self.player.play();
|
|
|
|
self.state.set_status(PlayStatus::kPlayStatusPlay);
|
|
|
|
self.update_state_position(position_ms);
|
|
|
|
self.play_status = SpircPlayStatus::Playing {
|
|
|
|
nominal_start_time: self.now_ms() as i64 - position_ms as i64,
|
2020-02-02 22:15:15 +00:00
|
|
|
preloading_of_next_track_triggered,
|
2020-01-31 21:41:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
SpircPlayStatus::LoadingPause { position_ms } => {
|
|
|
|
self.ensure_mixer_started();
|
|
|
|
self.player.play();
|
|
|
|
self.play_status = SpircPlayStatus::LoadingPlay { position_ms };
|
|
|
|
}
|
|
|
|
_ => (),
|
2017-02-23 11:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_play_pause(&mut self) {
|
2020-01-31 21:41:11 +00:00
|
|
|
match self.play_status {
|
|
|
|
SpircPlayStatus::Paused { .. } | SpircPlayStatus::LoadingPause { .. } => {
|
|
|
|
self.handle_play()
|
|
|
|
}
|
|
|
|
SpircPlayStatus::Playing { .. } | SpircPlayStatus::LoadingPlay { .. } => {
|
|
|
|
self.handle_play()
|
|
|
|
}
|
2017-02-23 11:05:32 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_pause(&mut self) {
|
2020-01-31 21:41:11 +00:00
|
|
|
match self.play_status {
|
2020-02-02 22:15:15 +00:00
|
|
|
SpircPlayStatus::Playing {
|
|
|
|
nominal_start_time,
|
|
|
|
preloading_of_next_track_triggered,
|
|
|
|
} => {
|
2020-01-31 21:41:11 +00:00
|
|
|
self.player.pause();
|
|
|
|
self.state.set_status(PlayStatus::kPlayStatusPause);
|
|
|
|
let position_ms = (self.now_ms() - nominal_start_time) as u32;
|
|
|
|
self.update_state_position(position_ms);
|
2020-02-02 22:15:15 +00:00
|
|
|
self.play_status = SpircPlayStatus::Paused {
|
|
|
|
position_ms,
|
|
|
|
preloading_of_next_track_triggered,
|
|
|
|
};
|
2020-01-31 21:41:11 +00:00
|
|
|
}
|
|
|
|
SpircPlayStatus::LoadingPlay { position_ms } => {
|
|
|
|
self.player.pause();
|
|
|
|
self.play_status = SpircPlayStatus::LoadingPause { position_ms };
|
|
|
|
}
|
|
|
|
_ => (),
|
2017-02-23 11:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 21:41:11 +00:00
|
|
|
fn handle_seek(&mut self, position_ms: u32) {
|
|
|
|
self.update_state_position(position_ms);
|
|
|
|
self.player.seek(position_ms);
|
|
|
|
let now = self.now_ms();
|
|
|
|
match self.play_status {
|
|
|
|
SpircPlayStatus::Stopped => (),
|
|
|
|
SpircPlayStatus::LoadingPause {
|
|
|
|
position_ms: ref mut position,
|
|
|
|
}
|
|
|
|
| SpircPlayStatus::LoadingPlay {
|
|
|
|
position_ms: ref mut position,
|
|
|
|
}
|
|
|
|
| SpircPlayStatus::Paused {
|
|
|
|
position_ms: ref mut position,
|
2020-02-02 22:15:15 +00:00
|
|
|
..
|
2020-01-31 21:41:11 +00:00
|
|
|
} => *position = position_ms,
|
|
|
|
SpircPlayStatus::Playing {
|
|
|
|
ref mut nominal_start_time,
|
2020-02-02 22:15:15 +00:00
|
|
|
..
|
2020-01-31 21:41:11 +00:00
|
|
|
} => *nominal_start_time = now - position_ms as i64,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
Improved next/prev handling for queued tracks.
1) A queued track is removed once it has become the current track.
Note that the track doesn't need to actually play i.e. it could
have been immediately skipped over with 'next()'. This is
implemented in 'consume_queued_track()'.
2) Queued tracks are always positioned immediately after the current
track. 1) ensures this is true for 'next()' but 'prev()' requires
all the queued tracks are actually moved for this to remain the
case.
Also fixed the case where 'prev()' on the first track would incorrectly
wrap back around to the last track even when repeat was disabled. The
correct behaviour is to remain on the first track and just seek to the
start.
For example, with the following tracks and repeat enabled:
TrackA, TrackB, TrackC-Q, TrackD-Q, TrackE
^^^^^^
Here, the result of 'prev' changes the current track from TrackB to
TrackA and the queued tracks (TrackC, TrackD) move to the position
immediately after TrackA:
TrackA, TrackC-Q, TrackD-Q, TrackB, TrackE
^^^^^^
Calling 'prev' again results in the current track wrapping back around
to TrackE and the queued tracks moving after that same track:
TrackA, TrackB, TrackE, TrackC-Q, TrackD-Q
^^^^^^
2018-02-01 00:02:12 +00:00
|
|
|
fn consume_queued_track(&mut self) -> usize {
|
|
|
|
// Removes current track if it is queued
|
|
|
|
// Returns the index of the next track
|
|
|
|
let current_index = self.state.get_playing_track_index() as usize;
|
2020-01-28 18:19:18 +00:00
|
|
|
if (current_index < self.state.get_track().len())
|
|
|
|
&& self.state.get_track()[current_index].get_queued()
|
|
|
|
{
|
Improved next/prev handling for queued tracks.
1) A queued track is removed once it has become the current track.
Note that the track doesn't need to actually play i.e. it could
have been immediately skipped over with 'next()'. This is
implemented in 'consume_queued_track()'.
2) Queued tracks are always positioned immediately after the current
track. 1) ensures this is true for 'next()' but 'prev()' requires
all the queued tracks are actually moved for this to remain the
case.
Also fixed the case where 'prev()' on the first track would incorrectly
wrap back around to the last track even when repeat was disabled. The
correct behaviour is to remain on the first track and just seek to the
start.
For example, with the following tracks and repeat enabled:
TrackA, TrackB, TrackC-Q, TrackD-Q, TrackE
^^^^^^
Here, the result of 'prev' changes the current track from TrackB to
TrackA and the queued tracks (TrackC, TrackD) move to the position
immediately after TrackA:
TrackA, TrackC-Q, TrackD-Q, TrackB, TrackE
^^^^^^
Calling 'prev' again results in the current track wrapping back around
to TrackE and the queued tracks moving after that same track:
TrackA, TrackB, TrackE, TrackC-Q, TrackD-Q
^^^^^^
2018-02-01 00:02:12 +00:00
|
|
|
self.state.mut_track().remove(current_index);
|
2020-01-23 08:05:10 +00:00
|
|
|
current_index
|
|
|
|
} else {
|
|
|
|
current_index + 1
|
2017-10-27 17:45:02 +00:00
|
|
|
}
|
Improved next/prev handling for queued tracks.
1) A queued track is removed once it has become the current track.
Note that the track doesn't need to actually play i.e. it could
have been immediately skipped over with 'next()'. This is
implemented in 'consume_queued_track()'.
2) Queued tracks are always positioned immediately after the current
track. 1) ensures this is true for 'next()' but 'prev()' requires
all the queued tracks are actually moved for this to remain the
case.
Also fixed the case where 'prev()' on the first track would incorrectly
wrap back around to the last track even when repeat was disabled. The
correct behaviour is to remain on the first track and just seek to the
start.
For example, with the following tracks and repeat enabled:
TrackA, TrackB, TrackC-Q, TrackD-Q, TrackE
^^^^^^
Here, the result of 'prev' changes the current track from TrackB to
TrackA and the queued tracks (TrackC, TrackD) move to the position
immediately after TrackA:
TrackA, TrackC-Q, TrackD-Q, TrackB, TrackE
^^^^^^
Calling 'prev' again results in the current track wrapping back around
to TrackE and the queued tracks moving after that same track:
TrackA, TrackB, TrackE, TrackC-Q, TrackD-Q
^^^^^^
2018-02-01 00:02:12 +00:00
|
|
|
}
|
2017-02-23 11:05:32 +00:00
|
|
|
|
2020-01-31 21:41:11 +00:00
|
|
|
fn preview_next_track(&mut self) -> Option<SpotifyId> {
|
2020-02-02 22:15:15 +00:00
|
|
|
self.get_track_id_to_play_from_playlist(self.state.get_playing_track_index() + 1)
|
|
|
|
.and_then(|(track_id, _)| Some(track_id))
|
2020-01-31 21:41:11 +00:00
|
|
|
}
|
|
|
|
|
Improved next/prev handling for queued tracks.
1) A queued track is removed once it has become the current track.
Note that the track doesn't need to actually play i.e. it could
have been immediately skipped over with 'next()'. This is
implemented in 'consume_queued_track()'.
2) Queued tracks are always positioned immediately after the current
track. 1) ensures this is true for 'next()' but 'prev()' requires
all the queued tracks are actually moved for this to remain the
case.
Also fixed the case where 'prev()' on the first track would incorrectly
wrap back around to the last track even when repeat was disabled. The
correct behaviour is to remain on the first track and just seek to the
start.
For example, with the following tracks and repeat enabled:
TrackA, TrackB, TrackC-Q, TrackD-Q, TrackE
^^^^^^
Here, the result of 'prev' changes the current track from TrackB to
TrackA and the queued tracks (TrackC, TrackD) move to the position
immediately after TrackA:
TrackA, TrackC-Q, TrackD-Q, TrackB, TrackE
^^^^^^
Calling 'prev' again results in the current track wrapping back around
to TrackE and the queued tracks moving after that same track:
TrackA, TrackB, TrackE, TrackC-Q, TrackD-Q
^^^^^^
2018-02-01 00:02:12 +00:00
|
|
|
fn handle_next(&mut self) {
|
|
|
|
let mut new_index = self.consume_queued_track() as u32;
|
|
|
|
let mut continue_playing = true;
|
2019-03-24 15:42:00 +00:00
|
|
|
let tracks_len = self.state.get_track().len() as u32;
|
2018-10-12 17:15:26 +00:00
|
|
|
debug!(
|
|
|
|
"At track {:?} of {:?} <{:?}> update [{}]",
|
|
|
|
new_index,
|
|
|
|
self.state.get_track().len(),
|
|
|
|
self.state.get_context_uri(),
|
2019-03-24 15:42:00 +00:00
|
|
|
tracks_len - new_index < CONTEXT_FETCH_THRESHOLD
|
2018-10-12 17:15:26 +00:00
|
|
|
);
|
|
|
|
let context_uri = self.state.get_context_uri().to_owned();
|
2019-10-08 09:31:18 +00:00
|
|
|
if (context_uri.starts_with("spotify:station:")
|
|
|
|
|| context_uri.starts_with("spotify:dailymix:"))
|
2019-03-16 15:18:38 +00:00
|
|
|
&& ((self.state.get_track().len() as u32) - new_index) < CONTEXT_FETCH_THRESHOLD
|
2019-03-13 19:35:46 +00:00
|
|
|
{
|
2018-10-12 17:15:26 +00:00
|
|
|
self.context_fut = self.resolve_station(&context_uri);
|
|
|
|
self.update_tracks_from_context();
|
|
|
|
}
|
2019-11-05 19:34:43 +00:00
|
|
|
if self.config.autoplay && new_index == tracks_len - 1 {
|
2019-03-24 15:42:00 +00:00
|
|
|
// Extend the playlist
|
|
|
|
// Note: This doesn't seem to reflect in the UI
|
2019-11-05 19:34:43 +00:00
|
|
|
// the additional tracks in the frame don't show up as with station view
|
|
|
|
debug!("Extending playlist <{}>", context_uri);
|
2019-03-24 15:42:00 +00:00
|
|
|
self.update_tracks_from_context();
|
|
|
|
}
|
|
|
|
if new_index >= tracks_len {
|
Improved next/prev handling for queued tracks.
1) A queued track is removed once it has become the current track.
Note that the track doesn't need to actually play i.e. it could
have been immediately skipped over with 'next()'. This is
implemented in 'consume_queued_track()'.
2) Queued tracks are always positioned immediately after the current
track. 1) ensures this is true for 'next()' but 'prev()' requires
all the queued tracks are actually moved for this to remain the
case.
Also fixed the case where 'prev()' on the first track would incorrectly
wrap back around to the last track even when repeat was disabled. The
correct behaviour is to remain on the first track and just seek to the
start.
For example, with the following tracks and repeat enabled:
TrackA, TrackB, TrackC-Q, TrackD-Q, TrackE
^^^^^^
Here, the result of 'prev' changes the current track from TrackB to
TrackA and the queued tracks (TrackC, TrackD) move to the position
immediately after TrackA:
TrackA, TrackC-Q, TrackD-Q, TrackB, TrackE
^^^^^^
Calling 'prev' again results in the current track wrapping back around
to TrackE and the queued tracks moving after that same track:
TrackA, TrackB, TrackE, TrackC-Q, TrackD-Q
^^^^^^
2018-02-01 00:02:12 +00:00
|
|
|
new_index = 0; // Loop around back to start
|
|
|
|
continue_playing = self.state.get_repeat();
|
|
|
|
}
|
2017-02-23 11:05:32 +00:00
|
|
|
|
2020-01-23 08:05:10 +00:00
|
|
|
if tracks_len > 0 {
|
|
|
|
self.state.set_playing_track_index(new_index);
|
2020-01-31 21:41:11 +00:00
|
|
|
self.load_track(continue_playing, 0);
|
2020-01-28 18:19:18 +00:00
|
|
|
} else {
|
2020-01-23 08:05:10 +00:00
|
|
|
info!("Not playing next track because there are no more tracks left in queue.");
|
|
|
|
self.state.set_playing_track_index(0);
|
|
|
|
self.state.set_status(PlayStatus::kPlayStatusStop);
|
|
|
|
self.player.stop();
|
2020-01-31 21:41:11 +00:00
|
|
|
self.ensure_mixer_stopped();
|
|
|
|
self.play_status = SpircPlayStatus::Stopped;
|
2020-01-23 08:05:10 +00:00
|
|
|
}
|
2017-02-23 11:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_prev(&mut self) {
|
|
|
|
// Previous behaves differently based on the position
|
Improved next/prev handling for queued tracks.
1) A queued track is removed once it has become the current track.
Note that the track doesn't need to actually play i.e. it could
have been immediately skipped over with 'next()'. This is
implemented in 'consume_queued_track()'.
2) Queued tracks are always positioned immediately after the current
track. 1) ensures this is true for 'next()' but 'prev()' requires
all the queued tracks are actually moved for this to remain the
case.
Also fixed the case where 'prev()' on the first track would incorrectly
wrap back around to the last track even when repeat was disabled. The
correct behaviour is to remain on the first track and just seek to the
start.
For example, with the following tracks and repeat enabled:
TrackA, TrackB, TrackC-Q, TrackD-Q, TrackE
^^^^^^
Here, the result of 'prev' changes the current track from TrackB to
TrackA and the queued tracks (TrackC, TrackD) move to the position
immediately after TrackA:
TrackA, TrackC-Q, TrackD-Q, TrackB, TrackE
^^^^^^
Calling 'prev' again results in the current track wrapping back around
to TrackE and the queued tracks moving after that same track:
TrackA, TrackB, TrackE, TrackC-Q, TrackD-Q
^^^^^^
2018-02-01 00:02:12 +00:00
|
|
|
// Under 3s it goes to the previous song (starts playing)
|
|
|
|
// Over 3s it seeks to zero (retains previous play status)
|
2017-02-23 11:05:32 +00:00
|
|
|
if self.position() < 3000 {
|
Improved next/prev handling for queued tracks.
1) A queued track is removed once it has become the current track.
Note that the track doesn't need to actually play i.e. it could
have been immediately skipped over with 'next()'. This is
implemented in 'consume_queued_track()'.
2) Queued tracks are always positioned immediately after the current
track. 1) ensures this is true for 'next()' but 'prev()' requires
all the queued tracks are actually moved for this to remain the
case.
Also fixed the case where 'prev()' on the first track would incorrectly
wrap back around to the last track even when repeat was disabled. The
correct behaviour is to remain on the first track and just seek to the
start.
For example, with the following tracks and repeat enabled:
TrackA, TrackB, TrackC-Q, TrackD-Q, TrackE
^^^^^^
Here, the result of 'prev' changes the current track from TrackB to
TrackA and the queued tracks (TrackC, TrackD) move to the position
immediately after TrackA:
TrackA, TrackC-Q, TrackD-Q, TrackB, TrackE
^^^^^^
Calling 'prev' again results in the current track wrapping back around
to TrackE and the queued tracks moving after that same track:
TrackA, TrackB, TrackE, TrackC-Q, TrackD-Q
^^^^^^
2018-02-01 00:02:12 +00:00
|
|
|
// Queued tracks always follow the currently playing track.
|
|
|
|
// They should not be considered when calculating the previous
|
|
|
|
// track so extract them beforehand and reinsert them after it.
|
|
|
|
let mut queue_tracks = Vec::new();
|
|
|
|
{
|
|
|
|
let queue_index = self.consume_queued_track();
|
|
|
|
let tracks = self.state.mut_track();
|
|
|
|
while queue_index < tracks.len() && tracks[queue_index].get_queued() {
|
|
|
|
queue_tracks.push(tracks.remove(queue_index));
|
|
|
|
}
|
|
|
|
}
|
2017-02-23 11:05:32 +00:00
|
|
|
let current_index = self.state.get_playing_track_index();
|
Improved next/prev handling for queued tracks.
1) A queued track is removed once it has become the current track.
Note that the track doesn't need to actually play i.e. it could
have been immediately skipped over with 'next()'. This is
implemented in 'consume_queued_track()'.
2) Queued tracks are always positioned immediately after the current
track. 1) ensures this is true for 'next()' but 'prev()' requires
all the queued tracks are actually moved for this to remain the
case.
Also fixed the case where 'prev()' on the first track would incorrectly
wrap back around to the last track even when repeat was disabled. The
correct behaviour is to remain on the first track and just seek to the
start.
For example, with the following tracks and repeat enabled:
TrackA, TrackB, TrackC-Q, TrackD-Q, TrackE
^^^^^^
Here, the result of 'prev' changes the current track from TrackB to
TrackA and the queued tracks (TrackC, TrackD) move to the position
immediately after TrackA:
TrackA, TrackC-Q, TrackD-Q, TrackB, TrackE
^^^^^^
Calling 'prev' again results in the current track wrapping back around
to TrackE and the queued tracks moving after that same track:
TrackA, TrackB, TrackE, TrackC-Q, TrackD-Q
^^^^^^
2018-02-01 00:02:12 +00:00
|
|
|
let new_index = if current_index > 0 {
|
|
|
|
current_index - 1
|
|
|
|
} else if self.state.get_repeat() {
|
2017-02-23 11:05:32 +00:00
|
|
|
self.state.get_track().len() as u32 - 1
|
|
|
|
} else {
|
Improved next/prev handling for queued tracks.
1) A queued track is removed once it has become the current track.
Note that the track doesn't need to actually play i.e. it could
have been immediately skipped over with 'next()'. This is
implemented in 'consume_queued_track()'.
2) Queued tracks are always positioned immediately after the current
track. 1) ensures this is true for 'next()' but 'prev()' requires
all the queued tracks are actually moved for this to remain the
case.
Also fixed the case where 'prev()' on the first track would incorrectly
wrap back around to the last track even when repeat was disabled. The
correct behaviour is to remain on the first track and just seek to the
start.
For example, with the following tracks and repeat enabled:
TrackA, TrackB, TrackC-Q, TrackD-Q, TrackE
^^^^^^
Here, the result of 'prev' changes the current track from TrackB to
TrackA and the queued tracks (TrackC, TrackD) move to the position
immediately after TrackA:
TrackA, TrackC-Q, TrackD-Q, TrackB, TrackE
^^^^^^
Calling 'prev' again results in the current track wrapping back around
to TrackE and the queued tracks moving after that same track:
TrackA, TrackB, TrackE, TrackC-Q, TrackD-Q
^^^^^^
2018-02-01 00:02:12 +00:00
|
|
|
0
|
2017-02-23 11:05:32 +00:00
|
|
|
};
|
Improved next/prev handling for queued tracks.
1) A queued track is removed once it has become the current track.
Note that the track doesn't need to actually play i.e. it could
have been immediately skipped over with 'next()'. This is
implemented in 'consume_queued_track()'.
2) Queued tracks are always positioned immediately after the current
track. 1) ensures this is true for 'next()' but 'prev()' requires
all the queued tracks are actually moved for this to remain the
case.
Also fixed the case where 'prev()' on the first track would incorrectly
wrap back around to the last track even when repeat was disabled. The
correct behaviour is to remain on the first track and just seek to the
start.
For example, with the following tracks and repeat enabled:
TrackA, TrackB, TrackC-Q, TrackD-Q, TrackE
^^^^^^
Here, the result of 'prev' changes the current track from TrackB to
TrackA and the queued tracks (TrackC, TrackD) move to the position
immediately after TrackA:
TrackA, TrackC-Q, TrackD-Q, TrackB, TrackE
^^^^^^
Calling 'prev' again results in the current track wrapping back around
to TrackE and the queued tracks moving after that same track:
TrackA, TrackB, TrackE, TrackC-Q, TrackD-Q
^^^^^^
2018-02-01 00:02:12 +00:00
|
|
|
// Reinsert queued tracks after the new playing track.
|
|
|
|
let mut pos = (new_index + 1) as usize;
|
|
|
|
for track in queue_tracks.into_iter() {
|
|
|
|
self.state.mut_track().insert(pos, track);
|
|
|
|
pos += 1;
|
|
|
|
}
|
2017-02-23 11:05:32 +00:00
|
|
|
|
|
|
|
self.state.set_playing_track_index(new_index);
|
|
|
|
|
2020-01-31 21:41:11 +00:00
|
|
|
self.load_track(true, 0);
|
2017-02-23 11:05:32 +00:00
|
|
|
} else {
|
2020-01-31 21:41:11 +00:00
|
|
|
self.handle_seek(0);
|
2017-02-23 11:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_volume_up(&mut self) {
|
2018-01-25 22:37:28 +00:00
|
|
|
let mut volume: u32 = self.device.get_volume() as u32 + 4096;
|
2017-02-23 11:05:32 +00:00
|
|
|
if volume > 0xFFFF {
|
|
|
|
volume = 0xFFFF;
|
|
|
|
}
|
2018-05-17 01:15:17 +00:00
|
|
|
self.set_volume(volume as u16);
|
2017-02-23 11:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_volume_down(&mut self) {
|
2018-01-25 22:37:28 +00:00
|
|
|
let mut volume: i32 = self.device.get_volume() as i32 - 4096;
|
2017-02-23 11:05:32 +00:00
|
|
|
if volume < 0 {
|
|
|
|
volume = 0;
|
|
|
|
}
|
2018-05-17 01:15:17 +00:00
|
|
|
self.set_volume(volume as u16);
|
2017-02-23 11:05:32 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
fn handle_end_of_track(&mut self) {
|
2018-02-09 01:05:50 +00:00
|
|
|
self.handle_next();
|
2017-01-29 14:11:20 +00:00
|
|
|
self.notify(None);
|
2016-01-20 13:51:35 +00:00
|
|
|
}
|
2016-01-20 15:47:05 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
fn position(&mut self) -> u32 {
|
2020-01-31 21:41:11 +00:00
|
|
|
match self.play_status {
|
|
|
|
SpircPlayStatus::Stopped => 0,
|
|
|
|
SpircPlayStatus::LoadingPlay { position_ms }
|
|
|
|
| SpircPlayStatus::LoadingPause { position_ms }
|
2020-02-02 22:15:15 +00:00
|
|
|
| SpircPlayStatus::Paused { position_ms, .. } => position_ms,
|
|
|
|
SpircPlayStatus::Playing {
|
|
|
|
nominal_start_time, ..
|
|
|
|
} => (self.now_ms() - nominal_start_time) as u32,
|
2020-01-31 21:41:11 +00:00
|
|
|
}
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 09:31:18 +00:00
|
|
|
fn resolve_station(
|
|
|
|
&self,
|
|
|
|
uri: &str,
|
|
|
|
) -> Box<dyn Future<Item = serde_json::Value, Error = MercuryError>> {
|
2018-10-12 17:15:26 +00:00
|
|
|
let radio_uri = format!("hm://radio-apollo/v3/stations/{}", uri);
|
|
|
|
|
|
|
|
self.resolve_uri(&radio_uri)
|
|
|
|
}
|
|
|
|
|
2020-01-17 17:11:07 +00:00
|
|
|
fn resolve_autoplay_uri(
|
|
|
|
&self,
|
|
|
|
uri: &str,
|
|
|
|
) -> Box<dyn Future<Item = String, Error = MercuryError>> {
|
2019-03-24 15:42:00 +00:00
|
|
|
let query_uri = format!("hm://autoplay-enabled/query?uri={}", uri);
|
|
|
|
let request = self.session.mercury().get(query_uri);
|
|
|
|
Box::new(request.and_then(move |response| {
|
2020-01-07 11:13:49 +00:00
|
|
|
if response.status_code == 200 {
|
2020-01-17 17:11:07 +00:00
|
|
|
let data = response
|
|
|
|
.payload
|
|
|
|
.first()
|
|
|
|
.expect("Empty autoplay uri")
|
|
|
|
.to_vec();
|
2020-01-07 11:13:49 +00:00
|
|
|
let autoplay_uri = String::from_utf8(data).unwrap();
|
|
|
|
Ok(autoplay_uri)
|
|
|
|
} else {
|
|
|
|
warn!("No autoplay_uri found");
|
|
|
|
Err(MercuryError)
|
|
|
|
}
|
2019-03-24 15:42:00 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2020-01-17 17:11:07 +00:00
|
|
|
fn resolve_uri(
|
|
|
|
&self,
|
|
|
|
uri: &str,
|
|
|
|
) -> Box<dyn Future<Item = serde_json::Value, Error = MercuryError>> {
|
2018-10-12 17:15:26 +00:00
|
|
|
let request = self.session.mercury().get(uri);
|
|
|
|
|
|
|
|
Box::new(request.and_then(move |response| {
|
2019-10-08 09:31:18 +00:00
|
|
|
let data = response
|
|
|
|
.payload
|
|
|
|
.first()
|
|
|
|
.expect("Empty payload on context uri");
|
2018-10-12 17:15:26 +00:00
|
|
|
let response: serde_json::Value = serde_json::from_slice(&data).unwrap();
|
|
|
|
|
|
|
|
Ok(response)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_tracks_from_context(&mut self) {
|
|
|
|
if let Some(ref context) = self.context {
|
|
|
|
self.context_fut = self.resolve_uri(&context.next_page_url);
|
|
|
|
|
|
|
|
let new_tracks = &context.tracks;
|
2019-03-28 14:27:50 +00:00
|
|
|
debug!("Adding {:?} tracks from context to frame", new_tracks.len());
|
|
|
|
let mut track_vec = self.state.take_track().into_vec();
|
|
|
|
if let Some(head) = track_vec.len().checked_sub(CONTEXT_TRACKS_HISTORY) {
|
|
|
|
track_vec.drain(0..head);
|
|
|
|
}
|
|
|
|
track_vec.extend_from_slice(&new_tracks);
|
2019-10-08 09:31:18 +00:00
|
|
|
self.state
|
|
|
|
.set_track(protobuf::RepeatedField::from_vec(track_vec));
|
2019-03-28 14:27:50 +00:00
|
|
|
|
|
|
|
// Update playing index
|
|
|
|
if let Some(new_index) = self
|
|
|
|
.state
|
|
|
|
.get_playing_track_index()
|
|
|
|
.checked_sub(CONTEXT_TRACKS_HISTORY as u32)
|
2019-03-16 15:18:38 +00:00
|
|
|
{
|
2019-03-28 14:27:50 +00:00
|
|
|
self.state.set_playing_track_index(new_index);
|
2018-10-12 17:15:26 +00:00
|
|
|
}
|
2019-03-24 15:42:00 +00:00
|
|
|
} else {
|
|
|
|
warn!("No context to update from!");
|
2018-10-12 17:15:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 16:25:09 +00:00
|
|
|
fn update_tracks(&mut self, frame: &protocol::spirc::Frame) {
|
2019-10-09 17:49:13 +00:00
|
|
|
debug!("State: {:?}", frame.get_state());
|
2017-01-29 14:11:20 +00:00
|
|
|
let index = frame.get_state().get_playing_track_index();
|
2018-02-17 15:17:05 +00:00
|
|
|
let context_uri = frame.get_state().get_context_uri().to_owned();
|
2018-10-12 17:15:26 +00:00
|
|
|
let tracks = frame.get_state().get_track();
|
|
|
|
debug!("Frame has {:?} tracks", tracks.len());
|
2019-10-08 09:31:18 +00:00
|
|
|
if context_uri.starts_with("spotify:station:")
|
|
|
|
|| context_uri.starts_with("spotify:dailymix:")
|
|
|
|
{
|
2018-10-12 17:15:26 +00:00
|
|
|
self.context_fut = self.resolve_station(&context_uri);
|
2019-11-05 19:34:43 +00:00
|
|
|
} else if self.config.autoplay {
|
|
|
|
info!("Fetching autoplay context uri");
|
2019-03-24 15:42:00 +00:00
|
|
|
// Get autoplay_station_uri for regular playlists
|
|
|
|
self.autoplay_fut = self.resolve_autoplay_uri(&context_uri);
|
2018-10-12 17:15:26 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
|
|
|
self.state.set_playing_track_index(index);
|
2017-01-29 16:25:09 +00:00
|
|
|
self.state.set_track(tracks.into_iter().cloned().collect());
|
2018-02-17 15:17:05 +00:00
|
|
|
self.state.set_context_uri(context_uri);
|
2018-02-17 15:59:04 +00:00
|
|
|
self.state.set_repeat(frame.get_state().get_repeat());
|
|
|
|
self.state.set_shuffle(frame.get_state().get_shuffle());
|
2016-01-20 15:47:05 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 17:49:13 +00:00
|
|
|
// should this be a method of SpotifyId directly?
|
|
|
|
fn get_spotify_id_for_track(&self, track_ref: &TrackRef) -> Result<SpotifyId, SpotifyIdError> {
|
|
|
|
SpotifyId::from_raw(track_ref.get_gid()).or_else(|_| {
|
|
|
|
let uri = track_ref.get_uri();
|
|
|
|
debug!("Malformed or no gid, attempting to parse URI <{}>", uri);
|
|
|
|
SpotifyId::from_uri(uri)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-02 22:15:15 +00:00
|
|
|
fn get_track_id_to_play_from_playlist(&self, index: u32) -> Option<(SpotifyId, u32)> {
|
2019-10-09 17:49:13 +00:00
|
|
|
let tracks_len = self.state.get_track().len() as u32;
|
2020-02-02 22:15:15 +00:00
|
|
|
|
|
|
|
let mut new_playlist_index = index;
|
|
|
|
|
|
|
|
if new_playlist_index >= tracks_len {
|
|
|
|
new_playlist_index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let start_index = new_playlist_index;
|
|
|
|
|
2019-10-22 10:05:35 +00:00
|
|
|
// Cycle through all tracks, break if we don't find any playable tracks
|
|
|
|
// tracks in each frame either have a gid or uri (that may or may not be a valid track)
|
|
|
|
// E.g - context based frames sometimes contain tracks with <spotify:meta:page:>
|
2020-02-02 22:15:15 +00:00
|
|
|
|
2020-02-02 23:11:27 +00:00
|
|
|
let mut track_ref = self.state.get_track()[new_playlist_index as usize].clone();
|
2020-02-02 22:15:15 +00:00
|
|
|
let mut track_id = self.get_spotify_id_for_track(&track_ref);
|
|
|
|
while track_id.is_err() || track_id.unwrap().audio_type == SpotifyAudioType::NonPlayable {
|
|
|
|
warn!(
|
|
|
|
"Skipping track <{:?}> at position [{}] of {}",
|
|
|
|
track_ref.get_uri(),
|
|
|
|
new_playlist_index,
|
|
|
|
tracks_len
|
|
|
|
);
|
|
|
|
|
|
|
|
new_playlist_index += 1;
|
|
|
|
if new_playlist_index >= tracks_len {
|
|
|
|
new_playlist_index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if new_playlist_index == start_index {
|
|
|
|
warn!("No playable track found in state: {:?}", self.state);
|
|
|
|
return None;
|
2019-03-13 19:35:46 +00:00
|
|
|
}
|
2020-02-02 22:15:15 +00:00
|
|
|
track_ref = self.state.get_track()[index as usize].clone();
|
|
|
|
track_id = self.get_spotify_id_for_track(&track_ref);
|
2019-10-09 17:49:13 +00:00
|
|
|
}
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2020-02-02 22:15:15 +00:00
|
|
|
match track_id {
|
|
|
|
Ok(track_id) => Some((track_id, new_playlist_index)),
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
}
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2020-02-02 22:15:15 +00:00
|
|
|
fn load_track(&mut self, start_playing: bool, position_ms: u32) {
|
|
|
|
let index = self.state.get_playing_track_index();
|
|
|
|
|
|
|
|
match self.get_track_id_to_play_from_playlist(index) {
|
|
|
|
Some((track, index)) => {
|
|
|
|
self.state.set_playing_track_index(index);
|
|
|
|
|
|
|
|
self.play_request_id = Some(self.player.load(track, start_playing, position_ms));
|
|
|
|
|
|
|
|
self.update_state_position(position_ms);
|
|
|
|
self.state.set_status(PlayStatus::kPlayStatusLoading);
|
|
|
|
if start_playing {
|
|
|
|
self.play_status = SpircPlayStatus::LoadingPlay { position_ms };
|
|
|
|
} else {
|
|
|
|
self.play_status = SpircPlayStatus::LoadingPause { position_ms };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
self.state.set_status(PlayStatus::kPlayStatusStop);
|
|
|
|
self.player.stop();
|
|
|
|
self.ensure_mixer_stopped();
|
|
|
|
self.play_status = SpircPlayStatus::Stopped;
|
|
|
|
}
|
2017-01-29 16:25:09 +00:00
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
}
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2020-02-02 22:15:15 +00:00
|
|
|
// fn load_track(&mut self, start_playing: bool, position_ms: u32) {
|
|
|
|
// let context_uri = self.state.get_context_uri().to_owned();
|
|
|
|
// let mut index = self.state.get_playing_track_index();
|
|
|
|
// let start_index = index;
|
|
|
|
// let tracks_len = self.state.get_track().len() as u32;
|
|
|
|
// debug!(
|
|
|
|
// "Loading context: <{}> index: [{}] of {}",
|
|
|
|
// context_uri, index, tracks_len
|
|
|
|
// );
|
|
|
|
// // Cycle through all tracks, break if we don't find any playable tracks
|
|
|
|
// // TODO: This will panic if no playable tracks are found!
|
|
|
|
// // tracks in each frame either have a gid or uri (that may or may not be a valid track)
|
|
|
|
// // E.g - context based frames sometimes contain tracks with <spotify:meta:page:>
|
|
|
|
// let track = {
|
|
|
|
// let mut track_ref = self.state.get_track()[index as usize].clone();
|
|
|
|
// let mut track_id = self.get_spotify_id_for_track(&track_ref);
|
|
|
|
// while track_id.is_err() || track_id.unwrap().audio_type == SpotifyAudioType::NonPlayable
|
|
|
|
// {
|
|
|
|
// warn!(
|
|
|
|
// "Skipping track <{:?}> at position [{}] of {}",
|
|
|
|
// track_ref.get_uri(),
|
|
|
|
// index,
|
|
|
|
// tracks_len
|
|
|
|
// );
|
|
|
|
// index = if index + 1 < tracks_len { index + 1 } else { 0 };
|
|
|
|
// self.state.set_playing_track_index(index);
|
|
|
|
// if index == start_index {
|
|
|
|
// warn!("No playable track found in state: {:?}", self.state);
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// track_ref = self.state.get_track()[index as usize].clone();
|
|
|
|
// track_id = self.get_spotify_id_for_track(&track_ref);
|
|
|
|
// }
|
|
|
|
// track_id
|
|
|
|
// }
|
|
|
|
// .expect("Invalid SpotifyId");
|
|
|
|
//
|
|
|
|
// self.play_request_id = Some(self.player.load(track, start_playing, position_ms));
|
|
|
|
//
|
|
|
|
// self.update_state_position(position_ms);
|
|
|
|
// self.state.set_status(PlayStatus::kPlayStatusLoading);
|
|
|
|
// if start_playing {
|
|
|
|
// self.play_status = SpircPlayStatus::LoadingPlay { position_ms };
|
|
|
|
// } else {
|
|
|
|
// self.play_status = SpircPlayStatus::LoadingPause { position_ms };
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
fn hello(&mut self) {
|
|
|
|
CommandSender::new(self, MessageType::kMessageTypeHello).send();
|
|
|
|
}
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2017-01-29 14:11:20 +00:00
|
|
|
fn notify(&mut self, recipient: Option<&str>) {
|
2020-01-31 21:41:11 +00:00
|
|
|
let status_string = match self.state.get_status() {
|
|
|
|
PlayStatus::kPlayStatusLoading => "kPlayStatusLoading",
|
|
|
|
PlayStatus::kPlayStatusPause => "kPlayStatusPause",
|
|
|
|
PlayStatus::kPlayStatusStop => "kPlayStatusStop",
|
|
|
|
PlayStatus::kPlayStatusPlay => "kPlayStatusPlay",
|
|
|
|
};
|
|
|
|
trace!("Sending status to server: [{}]", status_string);
|
2017-01-29 14:11:20 +00:00
|
|
|
let mut cs = CommandSender::new(self, MessageType::kMessageTypeNotify);
|
|
|
|
if let Some(s) = recipient {
|
|
|
|
cs = cs.recipient(&s);
|
|
|
|
}
|
|
|
|
cs.send();
|
|
|
|
}
|
2018-05-17 01:15:17 +00:00
|
|
|
|
|
|
|
fn set_volume(&mut self, volume: u16) {
|
|
|
|
self.device.set_volume(volume as u32);
|
2019-11-05 19:34:43 +00:00
|
|
|
self.mixer
|
|
|
|
.set_volume(volume_to_mixer(volume, self.config.linear_volume));
|
2018-05-17 01:15:17 +00:00
|
|
|
if let Some(cache) = self.session.cache() {
|
|
|
|
cache.save_volume(Volume { volume })
|
|
|
|
}
|
2020-01-31 21:41:11 +00:00
|
|
|
self.player.emit_volume_set_event(volume);
|
2018-05-17 01:15:17 +00:00
|
|
|
}
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
2016-02-16 21:25:55 +00:00
|
|
|
|
2017-02-22 04:17:04 +00:00
|
|
|
impl Drop for SpircTask {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
debug!("drop Spirc[{}]", self.session.session_id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 14:24:40 +00:00
|
|
|
struct CommandSender<'a> {
|
2017-01-20 12:56:42 +00:00
|
|
|
spirc: &'a mut SpircTask,
|
2017-02-21 21:49:45 +00:00
|
|
|
frame: protocol::spirc::Frame,
|
2016-02-17 14:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> CommandSender<'a> {
|
2017-01-20 12:56:42 +00:00
|
|
|
fn new(spirc: &'a mut SpircTask, cmd: MessageType) -> CommandSender {
|
2018-02-17 09:15:09 +00:00
|
|
|
let mut frame = protocol::spirc::Frame::new();
|
|
|
|
frame.set_version(1);
|
|
|
|
frame.set_protocol_version(::std::convert::Into::into("2.0.0"));
|
|
|
|
frame.set_ident(spirc.ident.clone());
|
|
|
|
frame.set_seq_nr(spirc.sequence.get());
|
|
|
|
frame.set_typ(cmd);
|
|
|
|
frame.set_device_state(spirc.device.clone());
|
2019-03-24 14:15:14 +00:00
|
|
|
frame.set_state_update_id(spirc.now_ms());
|
2016-02-17 14:24:40 +00:00
|
|
|
CommandSender {
|
2017-01-20 12:56:42 +00:00
|
|
|
spirc: spirc,
|
2017-02-21 21:49:45 +00:00
|
|
|
frame: frame,
|
2016-02-16 21:25:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 21:49:45 +00:00
|
|
|
fn recipient(mut self, recipient: &'a str) -> CommandSender {
|
|
|
|
self.frame.mut_recipient().push(recipient.to_owned());
|
2016-02-17 14:24:40 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-02-21 22:46:19 +00:00
|
|
|
#[allow(dead_code)]
|
2017-02-21 21:49:45 +00:00
|
|
|
fn state(mut self, state: protocol::spirc::State) -> CommandSender<'a> {
|
|
|
|
self.frame.set_state(state);
|
2016-02-17 14:24:40 +00:00
|
|
|
self
|
|
|
|
}
|
2017-01-29 14:11:20 +00:00
|
|
|
|
2017-02-21 21:49:45 +00:00
|
|
|
fn send(mut self) {
|
2017-02-21 22:46:19 +00:00
|
|
|
if !self.frame.has_state() && self.spirc.device.get_is_active() {
|
|
|
|
self.frame.set_state(self.spirc.state.clone());
|
2016-02-17 14:24:40 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 22:46:19 +00:00
|
|
|
let send = self.spirc.sender.start_send(self.frame).unwrap();
|
|
|
|
assert!(send.is_ready());
|
2016-02-17 14:24:40 +00:00
|
|
|
}
|
2016-02-16 21:25:55 +00:00
|
|
|
}
|