2016-02-17 14:24:40 +00:00
|
|
|
use protobuf::{self, Message, RepeatedField};
|
2016-02-22 05:41:37 +00:00
|
|
|
use std::borrow::Cow;
|
2017-01-20 12:56:42 +00:00
|
|
|
use futures::{Future, Stream, Sink, Async, Poll};
|
|
|
|
use futures::stream::BoxStream;
|
|
|
|
use futures::sink::BoxSink;
|
|
|
|
use futures::sync::mpsc;
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
use mercury::MercuryError;
|
2016-03-17 03:31:57 +00:00
|
|
|
use player::{Player, PlayerState};
|
|
|
|
use session::Session;
|
2017-01-20 12:56:42 +00:00
|
|
|
use util::{now_ms, SpotifyId, SeqGenerator};
|
2016-03-17 03:31:57 +00:00
|
|
|
use version;
|
|
|
|
|
2016-01-27 10:40:00 +00:00
|
|
|
use protocol;
|
2017-01-20 12:56:42 +00:00
|
|
|
pub use protocol::spirc::{PlayStatus, MessageType, Frame};
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
pub struct SpircTask {
|
2016-01-20 14:11:49 +00:00
|
|
|
player: Player,
|
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
|
|
|
|
|
|
|
name: String,
|
|
|
|
ident: String,
|
|
|
|
device_type: u8,
|
|
|
|
can_play: bool,
|
|
|
|
|
|
|
|
repeat: bool,
|
|
|
|
shuffle: bool,
|
|
|
|
|
|
|
|
is_active: bool,
|
|
|
|
became_active_at: i64,
|
|
|
|
|
|
|
|
last_command_ident: String,
|
|
|
|
last_command_msgid: u32,
|
|
|
|
|
2015-07-09 22:09:40 +00:00
|
|
|
tracks: Vec<SpotifyId>,
|
2016-01-02 15:19:39 +00:00
|
|
|
index: u32,
|
2016-02-13 01:09:15 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
subscription: BoxStream<Frame, MercuryError>,
|
|
|
|
sender: BoxSink<Frame, MercuryError>,
|
|
|
|
|
|
|
|
updates: mpsc::UnboundedReceiver<PlayerState>,
|
|
|
|
commands: mpsc::UnboundedReceiver<SpircCommand>,
|
|
|
|
|
|
|
|
shutdown: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum SpircCommand {
|
|
|
|
Shutdown
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Spirc {
|
|
|
|
commands: mpsc::UnboundedSender<SpircCommand>,
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
impl Spirc {
|
|
|
|
pub fn new(session: Session, player: Player) -> (Spirc, SpircTask) {
|
2016-03-17 03:06:56 +00:00
|
|
|
let ident = session.device_id().to_owned();
|
2017-01-18 18:41:22 +00:00
|
|
|
let name = session.config().name.clone();
|
2016-01-01 23:16:12 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
let uri = format!("hm://remote/user/{}", session.username());
|
|
|
|
|
|
|
|
let subscription = session.mercury().subscribe(&uri as &str);
|
|
|
|
let subscription = subscription.map(|stream| stream.map_err(|_| MercuryError)).flatten_stream();
|
|
|
|
let subscription = subscription.map(|response| -> Frame {
|
|
|
|
let data = response.payload.first().unwrap();
|
|
|
|
protobuf::parse_from_bytes(data).unwrap()
|
|
|
|
}).boxed();
|
|
|
|
|
|
|
|
let sender = Box::new(session.mercury().sender(uri).with(|frame: Frame| {
|
|
|
|
Ok(frame.write_to_bytes().unwrap())
|
|
|
|
}));
|
|
|
|
|
|
|
|
let updates = player.observe();
|
|
|
|
|
|
|
|
let (cmd_tx, cmd_rx) = mpsc::unbounded();
|
|
|
|
|
|
|
|
let mut task = SpircTask {
|
2016-01-20 14:11:49 +00:00
|
|
|
player: player,
|
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
|
|
|
|
|
|
|
name: name,
|
2016-01-01 23:16:12 +00:00
|
|
|
ident: ident,
|
2015-07-08 19:50:44 +00:00
|
|
|
device_type: 5,
|
|
|
|
can_play: true,
|
|
|
|
|
|
|
|
repeat: false,
|
|
|
|
shuffle: false,
|
|
|
|
|
|
|
|
is_active: false,
|
|
|
|
became_active_at: 0,
|
|
|
|
|
|
|
|
last_command_ident: String::new(),
|
|
|
|
last_command_msgid: 0,
|
|
|
|
|
2015-07-09 22:09:40 +00:00
|
|
|
tracks: Vec::new(),
|
2016-01-02 15:19:39 +00:00
|
|
|
index: 0,
|
2016-02-13 01:09:15 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
subscription: subscription,
|
|
|
|
sender: sender,
|
|
|
|
updates: updates,
|
|
|
|
commands: cmd_rx,
|
2016-01-20 15:47:05 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
shutdown: false,
|
2016-01-20 15:47:05 +00:00
|
|
|
};
|
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
let spirc = Spirc {
|
|
|
|
commands: cmd_tx,
|
|
|
|
};
|
2016-01-20 15:47:05 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
task.notify(true, None);
|
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-01-20 12:56:42 +00:00
|
|
|
pub fn shutdown(&mut self) {
|
|
|
|
mpsc::UnboundedSender::send(&mut self.commands, SpircCommand::Shutdown).unwrap();
|
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;
|
|
|
|
|
|
|
|
if !self.shutdown {
|
|
|
|
match self.subscription.poll().unwrap() {
|
|
|
|
Async::Ready(Some(frame)) => {
|
|
|
|
progress = true;
|
|
|
|
self.handle_frame(frame);
|
|
|
|
}
|
|
|
|
Async::Ready(None) => panic!("subscription terminated"),
|
|
|
|
Async::NotReady => (),
|
|
|
|
}
|
2016-02-16 21:52:55 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
match self.updates.poll().unwrap() {
|
|
|
|
Async::Ready(Some(state)) => {
|
|
|
|
progress = true;
|
|
|
|
self.handle_update(state);
|
|
|
|
}
|
|
|
|
Async::Ready(None) => panic!("player terminated"),
|
|
|
|
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 => (),
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2016-09-20 18:59:41 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
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 {
|
|
|
|
fn handle_update(&mut self, player_state: PlayerState) {
|
2016-01-20 15:47:05 +00:00
|
|
|
let end_of_track = player_state.end_of_track();
|
|
|
|
if end_of_track {
|
|
|
|
self.index = (self.index + 1) % self.tracks.len() as u32;
|
|
|
|
let track = self.tracks[self.index as usize];
|
|
|
|
self.player.load(track, true, 0);
|
|
|
|
} else {
|
2017-01-20 12:56:42 +00:00
|
|
|
self.notify_with_player_state(false, None, &player_state);
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
fn handle_command(&mut self, cmd: SpircCommand) {
|
|
|
|
match cmd {
|
|
|
|
SpircCommand::Shutdown => {
|
|
|
|
CommandSender::new(self, MessageType::kMessageTypeGoodbye).send();
|
|
|
|
self.shutdown = true;
|
|
|
|
self.commands.close();
|
|
|
|
self.updates.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_frame(&mut self, frame: Frame) {
|
|
|
|
debug!("{:?} {:?} {} {} {}",
|
|
|
|
frame.get_typ(),
|
|
|
|
frame.get_device_state().get_name(),
|
|
|
|
frame.get_ident(),
|
|
|
|
frame.get_seq_nr(),
|
|
|
|
frame.get_state_update_id());
|
|
|
|
|
2016-01-20 15:47:05 +00:00
|
|
|
if frame.get_ident() == self.ident ||
|
|
|
|
(frame.get_recipient().len() > 0 && !frame.get_recipient().contains(&self.ident)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-08 19:50:44 +00:00
|
|
|
if frame.get_recipient().len() > 0 {
|
2015-09-01 11:20:37 +00:00
|
|
|
self.last_command_ident = frame.get_ident().to_owned();
|
2015-07-08 19:50:44 +00:00
|
|
|
self.last_command_msgid = frame.get_seq_nr();
|
|
|
|
}
|
2016-02-13 01:09:15 +00:00
|
|
|
|
2015-07-08 19:50:44 +00:00
|
|
|
match frame.get_typ() {
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypeHello => {
|
2016-01-01 23:16:12 +00:00
|
|
|
self.notify(false, Some(frame.get_ident()));
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypeLoad => {
|
2015-07-08 19:50:44 +00:00
|
|
|
if !self.is_active {
|
|
|
|
self.is_active = true;
|
2017-01-20 12:56:42 +00:00
|
|
|
self.became_active_at = now_ms();
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 01:15:24 +00:00
|
|
|
self.reload_tracks(&frame);
|
2016-08-23 21:43:55 +00:00
|
|
|
if self.tracks.len() > 0 {
|
|
|
|
let play = frame.get_state().get_status() == PlayStatus::kPlayStatusPlay;
|
|
|
|
let track = self.tracks[self.index as usize];
|
|
|
|
let position = frame.get_state().get_position_ms();
|
|
|
|
self.player.load(track, play, position);
|
|
|
|
} else {
|
|
|
|
self.notify(false, Some(frame.get_ident()));
|
|
|
|
}
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypePlay => {
|
2016-01-20 14:11:49 +00:00
|
|
|
self.player.play();
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypePause => {
|
2016-01-20 14:11:49 +00:00
|
|
|
self.player.pause();
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypeNext => {
|
2016-01-20 13:51:35 +00:00
|
|
|
self.index = (self.index + 1) % self.tracks.len() as u32;
|
2016-01-14 03:27:34 +00:00
|
|
|
let track = self.tracks[self.index as usize];
|
2016-01-20 14:11:49 +00:00
|
|
|
self.player.load(track, true, 0);
|
2016-01-14 03:27:34 +00:00
|
|
|
}
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypePrev => {
|
2016-01-20 13:51:35 +00:00
|
|
|
self.index = (self.index - 1) % self.tracks.len() as u32;
|
2016-01-14 03:27:34 +00:00
|
|
|
let track = self.tracks[self.index as usize];
|
2016-01-20 14:11:49 +00:00
|
|
|
self.player.load(track, true, 0);
|
2016-01-14 03:27:34 +00:00
|
|
|
}
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypeSeek => {
|
2016-01-20 14:11:49 +00:00
|
|
|
self.player.seek(frame.get_position());
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypeReplace => {
|
2016-01-16 01:15:24 +00:00
|
|
|
self.reload_tracks(&frame);
|
|
|
|
}
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypeNotify => {
|
2015-07-08 19:50:44 +00:00
|
|
|
if self.is_active && frame.get_device_state().get_is_active() {
|
|
|
|
self.is_active = false;
|
2016-01-20 14:11:49 +00:00
|
|
|
self.player.stop();
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypeVolume => {
|
2016-01-20 14:11:49 +00:00
|
|
|
self.player.volume(frame.get_volume() as u16);
|
2016-01-14 12:12:01 +00:00
|
|
|
}
|
2017-01-20 12:56:42 +00:00
|
|
|
MessageType::kMessageTypeGoodbye => (),
|
2016-01-02 15:19:39 +00:00
|
|
|
_ => (),
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 13:51:35 +00:00
|
|
|
fn reload_tracks(&mut self, ref frame: &protocol::spirc::Frame) {
|
|
|
|
self.index = frame.get_state().get_playing_track_index();
|
2016-01-16 01:15:24 +00:00
|
|
|
self.tracks = frame.get_state()
|
2016-01-20 13:51:35 +00:00
|
|
|
.get_track()
|
|
|
|
.iter()
|
|
|
|
.filter(|track| track.has_gid())
|
|
|
|
.map(|track| SpotifyId::from_raw(track.get_gid()))
|
|
|
|
.collect();
|
|
|
|
}
|
2016-01-20 15:47:05 +00:00
|
|
|
|
2016-01-01 23:16:12 +00:00
|
|
|
fn notify(&mut self, hello: bool, recipient: Option<&str>) {
|
2016-02-18 23:02:41 +00:00
|
|
|
let mut cs = CommandSender::new(self,
|
|
|
|
if hello {
|
|
|
|
MessageType::kMessageTypeHello
|
|
|
|
} else {
|
|
|
|
MessageType::kMessageTypeNotify
|
|
|
|
});
|
2016-02-17 14:24:40 +00:00
|
|
|
if let Some(s) = recipient {
|
2016-02-18 23:02:41 +00:00
|
|
|
cs = cs.recipient(&s);
|
2016-02-17 14:24:40 +00:00
|
|
|
}
|
2016-02-18 23:02:41 +00:00
|
|
|
cs.send();
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 15:47:05 +00:00
|
|
|
fn notify_with_player_state(&mut self,
|
|
|
|
hello: bool,
|
|
|
|
recipient: Option<&str>,
|
|
|
|
player_state: &PlayerState) {
|
2016-02-18 23:02:41 +00:00
|
|
|
let mut cs = CommandSender::new(self,
|
|
|
|
if hello {
|
|
|
|
MessageType::kMessageTypeHello
|
|
|
|
} else {
|
|
|
|
MessageType::kMessageTypeNotify
|
|
|
|
})
|
|
|
|
.player_state(player_state);
|
2016-02-17 14:24:40 +00:00
|
|
|
if let Some(s) = recipient {
|
2016-02-18 23:02:41 +00:00
|
|
|
cs = cs.recipient(&s);
|
2016-02-17 14:24:40 +00:00
|
|
|
}
|
2016-02-18 23:02:41 +00:00
|
|
|
cs.send();
|
2016-01-20 15:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn spirc_state(&self, player_state: &PlayerState) -> protocol::spirc::State {
|
|
|
|
let (position_ms, position_measured_at) = player_state.position();
|
2015-07-08 19:50:44 +00:00
|
|
|
|
|
|
|
protobuf_init!(protocol::spirc::State::new(), {
|
2016-01-20 15:47:05 +00:00
|
|
|
status: player_state.status(),
|
2015-07-09 20:08:14 +00:00
|
|
|
position_ms: position_ms,
|
|
|
|
position_measured_at: position_measured_at as u64,
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2015-07-09 22:09:40 +00:00
|
|
|
playing_track_index: self.index,
|
|
|
|
track: self.tracks.iter().map(|track| {
|
|
|
|
protobuf_init!(protocol::spirc::TrackRef::new(), {
|
|
|
|
gid: track.to_raw().to_vec()
|
|
|
|
})
|
|
|
|
}).collect(),
|
2015-07-08 19:50:44 +00:00
|
|
|
|
|
|
|
shuffle: self.shuffle,
|
|
|
|
repeat: self.repeat,
|
|
|
|
|
|
|
|
playing_from_fallback: true,
|
|
|
|
|
|
|
|
last_command_ident: self.last_command_ident.clone(),
|
|
|
|
last_command_msgid: self.last_command_msgid
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:47:05 +00:00
|
|
|
fn device_state(&self, player_state: &PlayerState) -> protocol::spirc::DeviceState {
|
2015-07-08 19:50:44 +00:00
|
|
|
protobuf_init!(protocol::spirc::DeviceState::new(), {
|
2016-03-17 03:31:57 +00:00
|
|
|
sw_version: version::version_string(),
|
2015-07-08 19:50:44 +00:00
|
|
|
is_active: self.is_active,
|
|
|
|
can_play: self.can_play,
|
2016-01-20 15:47:05 +00:00
|
|
|
volume: player_state.volume() as u32,
|
2015-07-08 19:50:44 +00:00
|
|
|
name: self.name.clone(),
|
|
|
|
error_code: 0,
|
2015-07-09 21:04:19 +00:00
|
|
|
became_active_at: if self.is_active { self.became_active_at as i64 } else { 0 },
|
2015-07-08 19:50:44 +00:00
|
|
|
capabilities => [
|
|
|
|
@{
|
|
|
|
typ: protocol::spirc::CapabilityType::kCanBePlayer,
|
|
|
|
intValue => [0]
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
typ: protocol::spirc::CapabilityType::kDeviceType,
|
|
|
|
intValue => [ self.device_type as i64 ]
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
typ: protocol::spirc::CapabilityType::kGaiaEqConnectId,
|
|
|
|
intValue => [1]
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
typ: protocol::spirc::CapabilityType::kSupportsLogout,
|
|
|
|
intValue => [0]
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
typ: protocol::spirc::CapabilityType::kIsObservable,
|
|
|
|
intValue => [1]
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
typ: protocol::spirc::CapabilityType::kVolumeSteps,
|
|
|
|
intValue => [10]
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
typ: protocol::spirc::CapabilityType::kSupportedContexts,
|
|
|
|
stringValue => [
|
2016-02-22 05:48:58 +00:00
|
|
|
"album",
|
|
|
|
"playlist",
|
|
|
|
"search",
|
|
|
|
"inbox",
|
|
|
|
"toplist",
|
|
|
|
"starred",
|
|
|
|
"publishedstarred",
|
|
|
|
"track",
|
2015-07-08 19:50:44 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
typ: protocol::spirc::CapabilityType::kSupportedTypes,
|
|
|
|
stringValue => [
|
2016-02-22 05:48:58 +00:00
|
|
|
"audio/local",
|
|
|
|
"audio/track",
|
|
|
|
"local",
|
|
|
|
"track",
|
2015-07-08 19:50:44 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-02-16 21:25:55 +00:00
|
|
|
|
2016-02-17 14:24:40 +00:00
|
|
|
struct CommandSender<'a> {
|
2017-01-20 12:56:42 +00:00
|
|
|
spirc: &'a mut SpircTask,
|
2016-02-18 23:02:41 +00:00
|
|
|
cmd: MessageType,
|
2016-02-17 14:24:40 +00:00
|
|
|
recipient: Option<&'a str>,
|
|
|
|
player_state: Option<&'a PlayerState>,
|
|
|
|
state: Option<protocol::spirc::State>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> CommandSender<'a> {
|
2017-01-20 12:56:42 +00:00
|
|
|
fn new(spirc: &'a mut SpircTask, cmd: MessageType) -> CommandSender {
|
2016-02-17 14:24:40 +00:00
|
|
|
CommandSender {
|
2017-01-20 12:56:42 +00:00
|
|
|
spirc: spirc,
|
2016-02-17 14:24:40 +00:00
|
|
|
cmd: cmd,
|
|
|
|
recipient: None,
|
|
|
|
player_state: None,
|
|
|
|
state: None,
|
2016-02-16 21:25:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 14:24:40 +00:00
|
|
|
fn recipient(mut self, r: &'a str) -> CommandSender {
|
|
|
|
self.recipient = Some(r);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn player_state(mut self, s: &'a PlayerState) -> CommandSender {
|
|
|
|
self.player_state = Some(s);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
#[allow(dead_code)]
|
2016-02-17 14:24:40 +00:00
|
|
|
fn state(mut self, s: protocol::spirc::State) -> CommandSender<'a> {
|
|
|
|
self.state = Some(s);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send(self) {
|
2016-02-22 05:41:37 +00:00
|
|
|
let state = self.player_state.map_or_else(|| {
|
2017-01-20 12:56:42 +00:00
|
|
|
Cow::Owned(self.spirc.player.state())
|
2016-02-22 05:41:37 +00:00
|
|
|
}, |s| {
|
|
|
|
Cow::Borrowed(s)
|
|
|
|
});
|
2016-02-18 23:02:41 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
let mut frame = protobuf_init!(Frame::new(), {
|
2016-02-17 14:24:40 +00:00
|
|
|
version: 1,
|
2017-01-20 12:56:42 +00:00
|
|
|
ident: self.spirc.ident.clone(),
|
2016-02-22 05:48:58 +00:00
|
|
|
protocol_version: "2.0.0",
|
2017-01-20 12:56:42 +00:00
|
|
|
seq_nr: self.spirc.sequence.get(),
|
2016-02-17 14:24:40 +00:00
|
|
|
typ: self.cmd,
|
2016-02-18 23:02:41 +00:00
|
|
|
recipient: RepeatedField::from_vec(
|
2016-02-17 14:24:40 +00:00
|
|
|
self.recipient.map(|r| vec![r.to_owned()] ).unwrap_or(vec![])
|
|
|
|
),
|
2017-01-20 12:56:42 +00:00
|
|
|
device_state: self.spirc.device_state(&state),
|
2016-02-22 05:41:37 +00:00
|
|
|
state_update_id: state.update_time()
|
2016-02-17 14:24:40 +00:00
|
|
|
});
|
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
if self.spirc.is_active {
|
|
|
|
frame.set_state(self.spirc.spirc_state(&state));
|
2016-02-17 14:24:40 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
let ready = self.spirc.sender.start_send(frame).unwrap().is_ready();
|
|
|
|
assert!(ready);
|
2016-02-17 14:24:40 +00:00
|
|
|
}
|
2016-02-16 21:25:55 +00:00
|
|
|
}
|
2016-02-17 19:35:31 +00:00
|
|
|
|
2017-01-20 12:56:42 +00:00
|
|
|
#[allow(dead_code)]
|
2016-02-17 19:35:31 +00:00
|
|
|
fn track_ids_to_state<I: Iterator<Item = SpotifyId>>(track_ids: I) -> protocol::spirc::State {
|
|
|
|
let tracks: Vec<protocol::spirc::TrackRef> =
|
|
|
|
track_ids.map(|i| {
|
|
|
|
protobuf_init!(protocol::spirc::TrackRef::new(), { gid: i.to_raw().to_vec()})
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
protobuf_init!(protocol::spirc::State::new(), {
|
|
|
|
track: RepeatedField::from_vec(tracks)
|
|
|
|
})
|
|
|
|
}
|