2015-09-01 11:20:37 +00:00
|
|
|
use eventual::Async;
|
2016-02-17 14:24:40 +00:00
|
|
|
use protobuf::{self, Message, RepeatedField};
|
2017-01-31 20:38:52 +00:00
|
|
|
use std::sync::{mpsc, Mutex, Arc};
|
2016-02-13 01:09:15 +00:00
|
|
|
use std::collections::HashMap;
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2017-02-03 09:06:09 +00:00
|
|
|
use mercury::{MercuryRequest, MercuryMethod};
|
|
|
|
use messaging::{SpircMessage, MercuryResponseSender, UpdateMessageSender};
|
2016-03-17 03:31:57 +00:00
|
|
|
use player::{Player, PlayerState};
|
2017-01-25 21:09:03 +00:00
|
|
|
use mixer::Mixer;
|
2016-03-17 03:31:57 +00:00
|
|
|
use session::Session;
|
|
|
|
use util;
|
|
|
|
use util::SpotifyId;
|
|
|
|
use version;
|
|
|
|
|
2016-01-27 10:40:00 +00:00
|
|
|
use protocol;
|
2016-02-16 21:25:55 +00:00
|
|
|
pub use protocol::spirc::{PlayStatus, MessageType};
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2016-02-17 19:47:55 +00:00
|
|
|
#[derive(Clone)]
|
2016-01-20 15:47:05 +00:00
|
|
|
pub struct SpircManager(Arc<Mutex<SpircInternal>>);
|
|
|
|
|
|
|
|
struct SpircInternal {
|
2016-01-20 14:11:49 +00:00
|
|
|
player: Player,
|
2016-01-01 23:16:12 +00:00
|
|
|
session: Session,
|
2017-01-25 21:09:03 +00:00
|
|
|
mixer: Box<Mixer + Send>,
|
2015-07-08 19:50:44 +00:00
|
|
|
|
|
|
|
seq_nr: u32,
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
devices: HashMap<String, String>,
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 21:09:03 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct State {
|
|
|
|
pub status: PlayStatus,
|
|
|
|
pub position_ms: u32,
|
|
|
|
pub position_measured_at: i64,
|
|
|
|
pub update_time: i64,
|
|
|
|
pub volume: u16,
|
|
|
|
pub track: Option<SpotifyId>,
|
|
|
|
pub end_of_track: bool,
|
|
|
|
}
|
|
|
|
|
2017-02-03 12:20:49 +00:00
|
|
|
impl State {
|
|
|
|
pub fn new() -> State {
|
|
|
|
let state = State {
|
|
|
|
status: PlayStatus::kPlayStatusStop,
|
|
|
|
position_ms: 0,
|
|
|
|
position_measured_at: 0,
|
|
|
|
update_time: 0,
|
|
|
|
volume: 0,
|
|
|
|
track: None,
|
|
|
|
end_of_track: false,
|
|
|
|
};
|
|
|
|
state.update_time()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_from_player(mut self, player: &Player) -> State {
|
|
|
|
let player_state = player.state();
|
|
|
|
let (position_ms, position_measured_at) = player_state.position();
|
|
|
|
self.status = player_state.status();
|
|
|
|
self.position_ms = position_ms;
|
|
|
|
self.position_measured_at = position_measured_at;
|
|
|
|
self.track = player_state.track;
|
|
|
|
self.end_of_track = player_state.end_of_track();
|
|
|
|
self.update_time()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_from_mixer(mut self, mixer: &Box<Mixer + Send>) -> State {
|
|
|
|
self.volume = mixer.volume();
|
|
|
|
self.update_time()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_time(mut self) -> State {
|
|
|
|
self.update_time = util::now_ms();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 14:11:49 +00:00
|
|
|
impl SpircManager {
|
2017-01-25 21:09:03 +00:00
|
|
|
pub fn new(session: Session, player: Player, mixer: Box<Mixer + Send>) -> SpircManager {
|
2016-03-17 03:06:56 +00:00
|
|
|
let ident = session.device_id().to_owned();
|
2016-01-26 22:34:57 +00:00
|
|
|
let name = session.config().device_name.clone();
|
2016-01-01 23:16:12 +00:00
|
|
|
|
2016-01-20 15:47:05 +00:00
|
|
|
SpircManager(Arc::new(Mutex::new(SpircInternal {
|
2016-01-20 14:11:49 +00:00
|
|
|
player: player,
|
2016-01-01 23:16:12 +00:00
|
|
|
session: session,
|
2017-01-25 21:09:03 +00:00
|
|
|
mixer: mixer,
|
2015-07-08 19:50:44 +00:00
|
|
|
|
|
|
|
seq_nr: 0,
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
devices: HashMap::new(),
|
2016-01-20 15:47:05 +00:00
|
|
|
})))
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
2016-02-19 00:03:08 +00:00
|
|
|
pub fn run(&self) {
|
2016-01-20 15:47:05 +00:00
|
|
|
let rx = {
|
|
|
|
let mut internal = self.0.lock().unwrap();
|
2017-01-31 20:38:52 +00:00
|
|
|
let (tx, rx) = mpsc::channel::<SpircMessage>();
|
2016-01-20 15:47:05 +00:00
|
|
|
|
2017-01-31 20:38:52 +00:00
|
|
|
let mercury_response_sender = MercuryResponseSender::create(tx.clone());
|
|
|
|
|
|
|
|
internal.session.mercury_sub(internal.uri(), mercury_response_sender);
|
2016-01-20 15:47:05 +00:00
|
|
|
|
2017-02-01 20:32:14 +00:00
|
|
|
let update_message_sender = UpdateMessageSender::create(tx.clone());
|
|
|
|
|
|
|
|
internal.mixer.init(update_message_sender);
|
|
|
|
|
2016-01-20 15:47:05 +00:00
|
|
|
internal.notify(true, None);
|
|
|
|
|
|
|
|
// Use a weak pointer to avoid creating an Rc cycle between the player and the
|
|
|
|
// SpircManager
|
|
|
|
let _self = Arc::downgrade(&self.0);
|
|
|
|
internal.player.add_observer(Box::new(move |state| {
|
|
|
|
if let Some(_self) = _self.upgrade() {
|
|
|
|
let mut internal = _self.lock().unwrap();
|
|
|
|
internal.on_update(state);
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
2016-01-20 15:47:05 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
rx
|
|
|
|
};
|
|
|
|
|
2017-01-31 20:38:52 +00:00
|
|
|
for msg in rx {
|
|
|
|
match msg {
|
2017-02-01 20:32:14 +00:00
|
|
|
SpircMessage::MercuryMsg(pkt) => {
|
|
|
|
let data = pkt.payload.first().unwrap();
|
|
|
|
let frame = protobuf::parse_from_bytes::<protocol::spirc::Frame>(data).unwrap();
|
|
|
|
|
|
|
|
debug!("{:?} {:?} {} {} {}",
|
|
|
|
frame.get_typ(),
|
|
|
|
frame.get_device_state().get_name(),
|
|
|
|
frame.get_ident(),
|
|
|
|
frame.get_seq_nr(),
|
|
|
|
frame.get_state_update_id());
|
|
|
|
|
|
|
|
self.0.lock().unwrap().handle(frame);
|
|
|
|
}
|
|
|
|
SpircMessage::UpdateMsg(_) => {
|
|
|
|
self.0.lock().unwrap().notify(false, None);
|
|
|
|
}
|
2017-01-31 20:38:52 +00:00
|
|
|
}
|
2016-01-20 15:47:05 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-16 21:26:51 +00:00
|
|
|
|
|
|
|
pub fn devices(&self) -> HashMap<String, String> {
|
|
|
|
self.0.lock().unwrap().devices.clone()
|
|
|
|
}
|
2016-02-16 21:52:55 +00:00
|
|
|
|
2016-02-19 00:03:08 +00:00
|
|
|
pub fn send_play(&self, recipient: &str) {
|
2016-02-16 21:52:55 +00:00
|
|
|
let mut internal = self.0.lock().unwrap();
|
2016-02-18 23:02:41 +00:00
|
|
|
CommandSender::new(&mut *internal, MessageType::kMessageTypePlay)
|
2016-02-17 14:24:40 +00:00
|
|
|
.recipient(recipient)
|
|
|
|
.send();
|
2016-02-16 21:52:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-19 00:03:08 +00:00
|
|
|
pub fn send_pause(&self, recipient: &str) {
|
2016-02-16 21:52:55 +00:00
|
|
|
let mut internal = self.0.lock().unwrap();
|
2016-02-18 23:02:41 +00:00
|
|
|
CommandSender::new(&mut *internal, MessageType::kMessageTypePause)
|
2016-02-17 14:24:40 +00:00
|
|
|
.recipient(recipient)
|
|
|
|
.send();
|
2016-02-16 21:52:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-19 00:03:08 +00:00
|
|
|
pub fn send_prev(&self, recipient: &str) {
|
2016-02-16 21:52:55 +00:00
|
|
|
let mut internal = self.0.lock().unwrap();
|
2016-02-18 23:02:41 +00:00
|
|
|
CommandSender::new(&mut *internal, MessageType::kMessageTypePrev)
|
2016-02-17 14:24:40 +00:00
|
|
|
.recipient(recipient)
|
|
|
|
.send();
|
2016-02-16 21:52:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-19 00:03:08 +00:00
|
|
|
pub fn send_next(&self, recipient: &str) {
|
2016-02-16 21:52:55 +00:00
|
|
|
let mut internal = self.0.lock().unwrap();
|
2016-02-18 23:02:41 +00:00
|
|
|
CommandSender::new(&mut *internal, MessageType::kMessageTypeNext)
|
2016-02-17 14:24:40 +00:00
|
|
|
.recipient(recipient)
|
|
|
|
.send();
|
|
|
|
}
|
|
|
|
|
2016-02-17 19:35:31 +00:00
|
|
|
pub fn send_replace_tracks<I: Iterator<Item = SpotifyId>>(&mut self,
|
|
|
|
recipient: &str,
|
|
|
|
track_ids: I) {
|
|
|
|
let state = track_ids_to_state(track_ids);
|
2016-02-17 14:24:40 +00:00
|
|
|
let mut internal = self.0.lock().unwrap();
|
2016-02-18 23:02:41 +00:00
|
|
|
CommandSender::new(&mut *internal, MessageType::kMessageTypeReplace)
|
2016-02-17 14:24:40 +00:00
|
|
|
.recipient(recipient)
|
|
|
|
.state(state)
|
|
|
|
.send();
|
2016-02-16 21:52:55 +00:00
|
|
|
}
|
2016-02-17 19:35:31 +00:00
|
|
|
|
|
|
|
pub fn send_load_tracks<I: Iterator<Item = SpotifyId>>(&mut self,
|
|
|
|
recipient: &str,
|
|
|
|
track_ids: I) {
|
|
|
|
let state = track_ids_to_state(track_ids);
|
|
|
|
let mut internal = self.0.lock().unwrap();
|
2016-02-18 23:02:41 +00:00
|
|
|
CommandSender::new(&mut *internal, MessageType::kMessageTypeLoad)
|
2016-02-17 19:35:31 +00:00
|
|
|
.recipient(recipient)
|
|
|
|
.state(state)
|
|
|
|
.send();
|
|
|
|
}
|
2016-02-17 19:35:52 +00:00
|
|
|
|
2016-09-20 18:59:41 +00:00
|
|
|
pub fn send_goodbye(&self) {
|
|
|
|
let mut internal = self.0.lock().unwrap();
|
|
|
|
CommandSender::new(&mut *internal, MessageType::kMessageTypeGoodbye)
|
|
|
|
.send();
|
|
|
|
}
|
|
|
|
|
2016-02-17 19:35:52 +00:00
|
|
|
pub fn get_queue(&self) -> Vec<SpotifyId> {
|
|
|
|
self.0.lock().unwrap().tracks.clone()
|
|
|
|
}
|
2016-01-20 15:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SpircInternal {
|
|
|
|
fn on_update(&mut self, player_state: &PlayerState) {
|
|
|
|
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-25 21:09:03 +00:00
|
|
|
self.notify(false, None);
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 23:02:41 +00:00
|
|
|
fn handle(&mut self, frame: protocol::spirc::Frame) {
|
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
|
|
|
|
|
|
|
if frame.has_ident() && !frame.has_goodbye() && frame.has_device_state() {
|
2016-02-18 23:02:41 +00:00
|
|
|
self.devices.insert(frame.get_ident().into(),
|
|
|
|
frame.get_device_state().get_name().into());
|
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;
|
|
|
|
self.became_active_at = util::now_ms();
|
|
|
|
}
|
|
|
|
|
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();
|
2017-02-03 12:30:30 +00:00
|
|
|
self.mixer.start();
|
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();
|
2017-02-03 12:30:30 +00:00
|
|
|
self.mixer.stop();
|
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();
|
2017-02-03 12:30:30 +00:00
|
|
|
self.mixer.stop();
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypeVolume => {
|
2017-01-25 21:09:03 +00:00
|
|
|
self.mixer.set_volume(frame.get_volume() as u16);
|
2016-01-14 12:12:01 +00:00
|
|
|
}
|
2016-02-18 23:02:41 +00:00
|
|
|
MessageType::kMessageTypeGoodbye => {
|
|
|
|
if frame.has_ident() {
|
|
|
|
self.devices.remove(frame.get_ident());
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2017-01-25 21:09:03 +00:00
|
|
|
fn spirc_state(&self, state: &State) -> protocol::spirc::State {
|
2015-07-08 19:50:44 +00:00
|
|
|
protobuf_init!(protocol::spirc::State::new(), {
|
2017-01-25 21:09:03 +00:00
|
|
|
status: state.status,
|
|
|
|
position_ms: state.position_ms,
|
|
|
|
position_measured_at: state.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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:09:03 +00:00
|
|
|
fn device_state(&self, state: &State) -> 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,
|
2017-01-25 21:09:03 +00:00
|
|
|
volume: 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,
|
2017-01-30 04:13:18 +00:00
|
|
|
intValue => [64]
|
2015-07-08 19:50:44 +00:00
|
|
|
},
|
|
|
|
@{
|
|
|
|
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-01-20 15:47:05 +00:00
|
|
|
|
|
|
|
fn uri(&self) -> String {
|
2016-01-26 22:34:57 +00:00
|
|
|
format!("hm://remote/user/{}", self.session.username())
|
2016-01-20 15:47:05 +00:00
|
|
|
}
|
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> {
|
|
|
|
spirc_internal: &'a mut SpircInternal,
|
2016-02-18 23:02:41 +00:00
|
|
|
cmd: MessageType,
|
2016-02-17 14:24:40 +00:00
|
|
|
recipient: Option<&'a str>,
|
|
|
|
state: Option<protocol::spirc::State>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> CommandSender<'a> {
|
2016-02-18 23:02:41 +00:00
|
|
|
fn new(spirc_internal: &'a mut SpircInternal, cmd: MessageType) -> CommandSender {
|
2016-02-17 14:24:40 +00:00
|
|
|
CommandSender {
|
|
|
|
spirc_internal: spirc_internal,
|
|
|
|
cmd: cmd,
|
|
|
|
recipient: 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 state(mut self, s: protocol::spirc::State) -> CommandSender<'a> {
|
|
|
|
self.state = Some(s);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send(self) {
|
2017-02-03 12:20:49 +00:00
|
|
|
let state = State::new()
|
|
|
|
.update_from_player(&self.spirc_internal.player)
|
|
|
|
.update_from_mixer(&self.spirc_internal.mixer);
|
2016-02-18 23:02:41 +00:00
|
|
|
|
2016-02-17 14:24:40 +00:00
|
|
|
let mut pkt = protobuf_init!(protocol::spirc::Frame::new(), {
|
|
|
|
version: 1,
|
|
|
|
ident: self.spirc_internal.ident.clone(),
|
2016-02-22 05:48:58 +00:00
|
|
|
protocol_version: "2.0.0",
|
2016-02-17 14:24:40 +00:00
|
|
|
seq_nr: { self.spirc_internal.seq_nr += 1; self.spirc_internal.seq_nr },
|
|
|
|
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![])
|
|
|
|
),
|
2016-02-22 05:41:37 +00:00
|
|
|
device_state: self.spirc_internal.device_state(&state),
|
2017-01-25 21:09:03 +00:00
|
|
|
state_update_id: state.update_time
|
2016-02-17 14:24:40 +00:00
|
|
|
});
|
|
|
|
|
2016-02-18 23:02:41 +00:00
|
|
|
if self.spirc_internal.is_active {
|
2016-02-22 05:41:37 +00:00
|
|
|
pkt.set_state(self.spirc_internal.spirc_state(&state));
|
2016-02-17 14:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.spirc_internal
|
|
|
|
.session
|
|
|
|
.mercury(MercuryRequest {
|
|
|
|
method: MercuryMethod::SEND,
|
|
|
|
uri: self.spirc_internal.uri(),
|
|
|
|
content_type: None,
|
|
|
|
payload: vec![pkt.write_to_bytes().unwrap()],
|
|
|
|
})
|
|
|
|
.fire();
|
|
|
|
}
|
2016-02-16 21:25:55 +00:00
|
|
|
}
|
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)
|
|
|
|
})
|
|
|
|
}
|