2015-09-01 11:20:37 +00:00
|
|
|
use eventual::Async;
|
2015-07-08 19:50:44 +00:00
|
|
|
use protobuf::{self, Message};
|
|
|
|
|
|
|
|
use util;
|
2015-07-09 20:08:14 +00:00
|
|
|
use session::Session;
|
2015-07-08 19:50:44 +00:00
|
|
|
use util::SpotifyId;
|
|
|
|
use util::version::version_string;
|
|
|
|
use mercury::{MercuryRequest, MercuryMethod};
|
2016-01-20 15:47:05 +00:00
|
|
|
use player::{Player, PlayerState};
|
|
|
|
|
|
|
|
use std::sync::{Mutex, Arc};
|
2016-02-13 01:09:15 +00:00
|
|
|
use std::collections::HashMap;
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2016-01-27 10:40:00 +00:00
|
|
|
use protocol;
|
|
|
|
pub use protocol::spirc::PlayStatus;
|
2015-07-08 19:50:44 +00:00
|
|
|
|
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,
|
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
|
|
|
}
|
|
|
|
|
2016-01-20 14:11:49 +00:00
|
|
|
impl SpircManager {
|
|
|
|
pub fn new(session: Session, player: Player) -> SpircManager {
|
2016-01-26 22:34:57 +00:00
|
|
|
let ident = session.device_id();
|
|
|
|
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,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(&mut self) {
|
2016-01-20 15:47:05 +00:00
|
|
|
let rx = {
|
|
|
|
let mut internal = self.0.lock().unwrap();
|
|
|
|
|
|
|
|
let rx = internal.session.mercury_sub(internal.uri());
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
for pkt in rx {
|
|
|
|
let data = pkt.payload.first().unwrap();
|
|
|
|
let frame = protobuf::parse_from_bytes::<protocol::spirc::Frame>(data).unwrap();
|
|
|
|
|
|
|
|
println!("{:?} {} {} {} {}",
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
self.notify_with_player_state(false, None, player_state);
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-13 01:09:15 +00:00
|
|
|
fn handle(&mut self, mut 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() {
|
|
|
|
self.devices.insert(frame.take_ident(), frame.take_device_state().take_name());
|
|
|
|
}
|
|
|
|
|
2015-07-08 19:50:44 +00:00
|
|
|
match frame.get_typ() {
|
|
|
|
protocol::spirc::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
|
|
|
}
|
|
|
|
protocol::spirc::MessageType::kMessageTypeLoad => {
|
|
|
|
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);
|
2015-07-09 22:09:40 +00:00
|
|
|
|
2015-07-09 20:08:14 +00:00
|
|
|
let play = frame.get_state().get_status() == PlayStatus::kPlayStatusPlay;
|
2015-07-09 22:09:40 +00:00
|
|
|
let track = self.tracks[self.index as usize];
|
|
|
|
let position = frame.get_state().get_position_ms();
|
2016-01-20 14:11:49 +00:00
|
|
|
self.player.load(track, play, position);
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
protocol::spirc::MessageType::kMessageTypePlay => {
|
2016-01-20 14:11:49 +00:00
|
|
|
self.player.play();
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
protocol::spirc::MessageType::kMessageTypePause => {
|
2016-01-20 14:11:49 +00:00
|
|
|
self.player.pause();
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
2016-01-14 03:27:34 +00:00
|
|
|
protocol::spirc::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
|
|
|
}
|
|
|
|
protocol::spirc::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
|
|
|
}
|
2015-07-08 19:50:44 +00:00
|
|
|
protocol::spirc::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-01-16 01:15:24 +00:00
|
|
|
protocol::spirc::MessageType::kMessageTypeReplace => {
|
|
|
|
self.reload_tracks(&frame);
|
|
|
|
}
|
2015-07-08 19:50:44 +00:00
|
|
|
protocol::spirc::MessageType::kMessageTypeNotify => {
|
|
|
|
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-13 01:09:15 +00:00
|
|
|
|
|
|
|
if frame.has_ident() && frame.has_goodbye() {
|
|
|
|
self.devices.remove(&frame.take_ident());
|
|
|
|
}
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
2016-01-20 13:51:35 +00:00
|
|
|
protocol::spirc::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
|
|
|
}
|
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
|
|
|
|
|
|
|
// FIXME: this entire function is duplicated in notify_with_player_state, but the borrow
|
|
|
|
// checker makes it hard to refactor
|
2016-01-01 23:16:12 +00:00
|
|
|
fn notify(&mut self, hello: bool, recipient: Option<&str>) {
|
2016-01-20 15:47:05 +00:00
|
|
|
let player_state = self.player.state();
|
|
|
|
|
2015-07-08 19:50:44 +00:00
|
|
|
let mut pkt = protobuf_init!(protocol::spirc::Frame::new(), {
|
|
|
|
version: 1,
|
|
|
|
ident: self.ident.clone(),
|
2015-09-01 11:20:37 +00:00
|
|
|
protocol_version: "2.0.0".to_owned(),
|
2015-07-08 19:50:44 +00:00
|
|
|
seq_nr: { self.seq_nr += 1; self.seq_nr },
|
2016-01-01 23:16:12 +00:00
|
|
|
typ: if hello {
|
|
|
|
protocol::spirc::MessageType::kMessageTypeHello
|
|
|
|
} else {
|
|
|
|
protocol::spirc::MessageType::kMessageTypeNotify
|
|
|
|
},
|
|
|
|
|
2016-01-20 15:47:05 +00:00
|
|
|
device_state: self.device_state(&player_state),
|
2015-07-08 19:50:44 +00:00
|
|
|
recipient: protobuf::RepeatedField::from_vec(
|
2015-09-01 11:20:37 +00:00
|
|
|
recipient.map(|r| vec![r.to_owned()] ).unwrap_or(vec![])
|
2015-07-08 19:50:44 +00:00
|
|
|
),
|
2016-01-20 15:47:05 +00:00
|
|
|
state_update_id: player_state.update_time() as i64
|
2015-07-08 19:50:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if self.is_active {
|
2016-01-20 15:47:05 +00:00
|
|
|
pkt.set_state(self.spirc_state(&player_state));
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
2016-01-02 15:19:39 +00:00
|
|
|
self.session
|
|
|
|
.mercury(MercuryRequest {
|
|
|
|
method: MercuryMethod::SEND,
|
2016-01-20 15:47:05 +00:00
|
|
|
uri: self.uri(),
|
2016-01-02 15:19:39 +00:00
|
|
|
content_type: None,
|
|
|
|
payload: vec![pkt.write_to_bytes().unwrap()],
|
|
|
|
})
|
|
|
|
.await()
|
|
|
|
.unwrap();
|
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) {
|
|
|
|
let mut pkt = protobuf_init!(protocol::spirc::Frame::new(), {
|
|
|
|
version: 1,
|
|
|
|
ident: self.ident.clone(),
|
|
|
|
protocol_version: "2.0.0".to_owned(),
|
|
|
|
seq_nr: { self.seq_nr += 1; self.seq_nr },
|
|
|
|
typ: if hello {
|
|
|
|
protocol::spirc::MessageType::kMessageTypeHello
|
|
|
|
} else {
|
|
|
|
protocol::spirc::MessageType::kMessageTypeNotify
|
|
|
|
},
|
|
|
|
|
|
|
|
device_state: self.device_state(&player_state),
|
|
|
|
recipient: protobuf::RepeatedField::from_vec(
|
|
|
|
recipient.map(|r| vec![r.to_owned()] ).unwrap_or(vec![])
|
|
|
|
),
|
|
|
|
state_update_id: player_state.update_time() as i64
|
|
|
|
});
|
|
|
|
|
|
|
|
if self.is_active {
|
|
|
|
pkt.set_state(self.spirc_state(&player_state));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.session
|
|
|
|
.mercury(MercuryRequest {
|
|
|
|
method: MercuryMethod::SEND,
|
|
|
|
uri: self.uri(),
|
|
|
|
content_type: None,
|
|
|
|
payload: vec![pkt.write_to_bytes().unwrap()],
|
|
|
|
})
|
|
|
|
.fire();
|
|
|
|
}
|
|
|
|
|
|
|
|
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(), {
|
|
|
|
sw_version: version_string(),
|
|
|
|
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 => [
|
2015-09-01 11:20:37 +00:00
|
|
|
"album".to_owned(),
|
|
|
|
"playlist".to_owned(),
|
|
|
|
"search".to_owned(),
|
|
|
|
"inbox".to_owned(),
|
|
|
|
"toplist".to_owned(),
|
|
|
|
"starred".to_owned(),
|
|
|
|
"publishedstarred".to_owned(),
|
|
|
|
"track".to_owned(),
|
2015-07-08 19:50:44 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
typ: protocol::spirc::CapabilityType::kSupportedTypes,
|
|
|
|
stringValue => [
|
2015-09-01 11:20:37 +00:00
|
|
|
"audio/local".to_owned(),
|
|
|
|
"audio/track".to_owned(),
|
|
|
|
"local".to_owned(),
|
|
|
|
"track".to_owned(),
|
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
|
|
|
}
|