2015-09-01 11:20:37 +00:00
|
|
|
use eventual::Async;
|
2015-07-08 19:50:44 +00:00
|
|
|
use protobuf::{self, Message};
|
2015-07-09 21:04:19 +00:00
|
|
|
use std::sync::{mpsc, MutexGuard};
|
2015-07-08 19:50:44 +00:00
|
|
|
|
|
|
|
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};
|
|
|
|
|
|
|
|
use librespot_protocol as protocol;
|
2015-07-09 20:08:14 +00:00
|
|
|
pub use librespot_protocol::spirc::PlayStatus;
|
2015-07-08 19:50:44 +00:00
|
|
|
|
2015-07-09 20:08:14 +00:00
|
|
|
pub struct SpircManager<'s, D: SpircDelegate> {
|
|
|
|
delegate: D,
|
2015-07-08 19:50:44 +00:00
|
|
|
session: &'s Session,
|
|
|
|
|
|
|
|
state_update_id: i64,
|
|
|
|
seq_nr: u32,
|
|
|
|
|
|
|
|
name: String,
|
|
|
|
ident: String,
|
|
|
|
device_type: u8,
|
|
|
|
can_play: bool,
|
|
|
|
|
|
|
|
repeat: bool,
|
|
|
|
shuffle: bool,
|
|
|
|
volume: u16,
|
|
|
|
|
|
|
|
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>,
|
|
|
|
index: u32
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 20:08:14 +00:00
|
|
|
pub trait SpircDelegate {
|
|
|
|
type State : SpircState;
|
|
|
|
|
|
|
|
fn load(&self, track: SpotifyId,
|
|
|
|
start_playing: bool, position_ms: u32);
|
|
|
|
fn play(&self);
|
|
|
|
fn pause(&self);
|
|
|
|
fn seek(&self, position_ms: u32);
|
|
|
|
fn stop(&self);
|
|
|
|
|
|
|
|
fn state(&self) -> MutexGuard<Self::State>;
|
2015-07-09 21:04:19 +00:00
|
|
|
fn updates(&self) -> mpsc::Receiver<i64>;
|
2015-07-09 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait SpircState {
|
|
|
|
fn status(&self) -> PlayStatus;
|
|
|
|
fn position(&self) -> (u32, i64);
|
|
|
|
fn update_time(&self) -> i64;
|
2015-07-09 22:09:40 +00:00
|
|
|
fn end_of_track(&self) -> bool;
|
2015-07-09 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl <'s, D: SpircDelegate> SpircManager<'s, D> {
|
|
|
|
pub fn new(session: &'s Session, delegate: D,
|
2015-12-30 18:50:23 +00:00
|
|
|
name: String) -> SpircManager<'s, D> {
|
2015-07-08 19:50:44 +00:00
|
|
|
SpircManager {
|
2015-07-09 20:08:14 +00:00
|
|
|
delegate: delegate,
|
2015-07-08 19:50:44 +00:00
|
|
|
session: &session,
|
|
|
|
|
|
|
|
state_update_id: 0,
|
|
|
|
seq_nr: 0,
|
|
|
|
|
|
|
|
name: name,
|
2015-09-01 11:20:37 +00:00
|
|
|
ident: session.0.config.device_id.clone(),
|
2015-07-08 19:50:44 +00:00
|
|
|
device_type: 5,
|
|
|
|
can_play: true,
|
|
|
|
|
|
|
|
repeat: false,
|
|
|
|
shuffle: false,
|
|
|
|
volume: 0x8000,
|
|
|
|
|
|
|
|
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(),
|
|
|
|
index: 0
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run(&mut self) {
|
2015-12-30 18:50:23 +00:00
|
|
|
let rx = self.session.mercury_sub(format!("hm://remote/3/user/{}/",
|
|
|
|
self.session.0.data.read().unwrap().canonical_username.clone()));
|
2015-07-09 21:04:19 +00:00
|
|
|
let updates = self.delegate.updates();
|
2015-07-08 19:50:44 +00:00
|
|
|
|
|
|
|
loop {
|
2015-07-09 21:04:19 +00:00
|
|
|
select! {
|
|
|
|
pkt = rx.recv() => {
|
|
|
|
let frame = protobuf::parse_from_bytes::<protocol::spirc::Frame>(
|
2015-09-01 11:20:37 +00:00
|
|
|
pkt.unwrap().payload.first().unwrap()).unwrap();
|
|
|
|
|
2015-07-09 21:04:19 +00:00
|
|
|
println!("{:?} {} {} {} {}",
|
|
|
|
frame.get_typ(),
|
|
|
|
frame.get_device_state().get_name(),
|
|
|
|
frame.get_ident(),
|
|
|
|
frame.get_seq_nr(),
|
|
|
|
frame.get_state_update_id());
|
|
|
|
if frame.get_ident() != self.ident &&
|
|
|
|
(frame.get_recipient().len() == 0 ||
|
|
|
|
frame.get_recipient().contains(&self.ident)) {
|
|
|
|
self.handle(frame);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
update_time = updates.recv() => {
|
2015-07-09 22:09:40 +00:00
|
|
|
let end_of_track = self.delegate.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.delegate.load(track, true, 0);
|
|
|
|
} else {
|
|
|
|
self.state_update_id = update_time.unwrap();
|
|
|
|
self.notify(None);
|
|
|
|
}
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle(&mut self, frame: protocol::spirc::Frame) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
match frame.get_typ() {
|
|
|
|
protocol::spirc::MessageType::kMessageTypeHello => {
|
|
|
|
self.notify(Some(frame.get_ident()));
|
|
|
|
}
|
|
|
|
protocol::spirc::MessageType::kMessageTypeLoad => {
|
|
|
|
if !self.is_active {
|
|
|
|
self.is_active = true;
|
|
|
|
self.became_active_at = util::now_ms();
|
|
|
|
}
|
|
|
|
|
2015-07-09 22:09:40 +00:00
|
|
|
|
|
|
|
self.index = frame.get_state().get_playing_track_index();
|
|
|
|
self.tracks = frame.get_state().get_track().iter()
|
|
|
|
.map(|track| SpotifyId::from_raw(track.get_gid()))
|
|
|
|
.collect();
|
|
|
|
|
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();
|
|
|
|
self.delegate.load(track, play, position);
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
protocol::spirc::MessageType::kMessageTypePlay => {
|
2015-07-09 20:08:14 +00:00
|
|
|
self.delegate.play();
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
protocol::spirc::MessageType::kMessageTypePause => {
|
2015-07-09 20:08:14 +00:00
|
|
|
self.delegate.pause();
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
protocol::spirc::MessageType::kMessageTypeSeek => {
|
2015-07-09 20:08:14 +00:00
|
|
|
self.delegate.seek(frame.get_position());
|
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;
|
2015-07-09 20:08:14 +00:00
|
|
|
self.delegate.stop();
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn notify(&mut self, recipient: Option<&str>) {
|
|
|
|
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 },
|
|
|
|
typ: protocol::spirc::MessageType::kMessageTypeNotify,
|
|
|
|
device_state: self.device_state(),
|
|
|
|
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
|
|
|
),
|
2015-07-09 21:04:19 +00:00
|
|
|
state_update_id: self.state_update_id as i64
|
2015-07-08 19:50:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if self.is_active {
|
2015-07-09 20:08:14 +00:00
|
|
|
pkt.set_state(self.spirc_state());
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.session.mercury(MercuryRequest{
|
|
|
|
method: MercuryMethod::SEND,
|
2015-12-30 18:50:23 +00:00
|
|
|
uri: format!("hm://remote/user/{}",
|
|
|
|
self.session.0.data.read().unwrap().canonical_username.clone()),
|
2015-07-08 19:50:44 +00:00
|
|
|
content_type: None,
|
|
|
|
payload: vec![ pkt.write_to_bytes().unwrap() ]
|
2015-09-01 11:20:37 +00:00
|
|
|
}).await().unwrap();
|
2015-07-08 19:50:44 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 20:08:14 +00:00
|
|
|
fn spirc_state(&self) -> protocol::spirc::State {
|
|
|
|
let state = self.delegate.state();
|
|
|
|
let (position_ms, position_measured_at) = state.position();
|
2015-07-08 19:50:44 +00:00
|
|
|
|
|
|
|
protobuf_init!(protocol::spirc::State::new(), {
|
2015-07-09 20:08:14 +00:00
|
|
|
status: state.status(),
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-09 20:08:14 +00:00
|
|
|
fn device_state(&self) -> 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,
|
|
|
|
volume: self.volume as u32,
|
|
|
|
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
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|