2015-07-08 18:28:17 +00:00
|
|
|
#![feature(plugin,scoped)]
|
2015-07-02 17:24:25 +00:00
|
|
|
#![allow(deprecated)]
|
2015-07-07 21:40:31 +00:00
|
|
|
//#![allow(unused_imports,dead_code)]
|
2015-04-25 20:32:07 +00:00
|
|
|
|
|
|
|
#![plugin(protobuf_macros)]
|
|
|
|
#[macro_use] extern crate lazy_static;
|
|
|
|
|
2015-07-01 18:47:51 +00:00
|
|
|
|
2015-04-25 20:32:07 +00:00
|
|
|
extern crate byteorder;
|
|
|
|
extern crate crypto;
|
|
|
|
extern crate gmp;
|
|
|
|
extern crate num;
|
2015-06-23 14:38:29 +00:00
|
|
|
extern crate portaudio;
|
2015-04-25 20:32:07 +00:00
|
|
|
extern crate protobuf;
|
2015-05-09 10:07:24 +00:00
|
|
|
extern crate shannon;
|
2015-04-25 20:32:07 +00:00
|
|
|
extern crate rand;
|
2015-05-09 10:07:24 +00:00
|
|
|
extern crate readall;
|
2015-06-23 14:38:29 +00:00
|
|
|
extern crate vorbis;
|
2015-05-09 10:07:24 +00:00
|
|
|
|
|
|
|
extern crate librespot_protocol;
|
2015-07-08 18:28:17 +00:00
|
|
|
#[macro_use] extern crate librespot;
|
2015-04-25 20:32:07 +00:00
|
|
|
|
2015-06-23 14:38:29 +00:00
|
|
|
use std::clone::Clone;
|
2015-05-09 10:07:24 +00:00
|
|
|
use std::fs::File;
|
2015-06-23 14:38:29 +00:00
|
|
|
use std::io::{Read, Write};
|
2015-05-09 10:07:24 +00:00
|
|
|
use std::path::Path;
|
2015-07-01 22:40:38 +00:00
|
|
|
use protobuf::core::Message;
|
2015-07-02 17:24:25 +00:00
|
|
|
use std::thread;
|
2015-07-07 21:40:31 +00:00
|
|
|
use std::path::PathBuf;
|
2015-05-09 10:07:24 +00:00
|
|
|
|
2015-07-08 18:28:17 +00:00
|
|
|
use librespot::util;
|
|
|
|
use librespot::metadata::{AlbumRef, ArtistRef, TrackRef};
|
|
|
|
use librespot::session::{Config, Session};
|
|
|
|
use librespot::util::SpotifyId;
|
|
|
|
use librespot::util::version::version_string;
|
|
|
|
use librespot::player::{Player, PlayerCommand};
|
|
|
|
use librespot::mercury::{MercuryRequest, MercuryMethod};
|
|
|
|
|
2015-07-01 18:47:51 +00:00
|
|
|
use librespot_protocol as protocol;
|
2015-07-02 11:54:34 +00:00
|
|
|
use librespot_protocol::spirc::PlayStatus;
|
2015-04-25 20:32:07 +00:00
|
|
|
|
|
|
|
fn main() {
|
2015-05-09 10:07:24 +00:00
|
|
|
let mut args = std::env::args().skip(1);
|
|
|
|
let mut appkey_file = File::open(Path::new(&args.next().unwrap())).unwrap();
|
|
|
|
let username = args.next().unwrap();
|
|
|
|
let password = args.next().unwrap();
|
2015-07-07 21:40:31 +00:00
|
|
|
let cache_location = args.next().unwrap();
|
2015-07-01 23:27:19 +00:00
|
|
|
let name = args.next().unwrap();
|
2015-05-09 10:07:24 +00:00
|
|
|
|
|
|
|
let mut appkey = Vec::new();
|
|
|
|
appkey_file.read_to_end(&mut appkey).unwrap();
|
|
|
|
|
|
|
|
let config = Config {
|
|
|
|
application_key: appkey,
|
2015-07-01 23:27:19 +00:00
|
|
|
user_agent: version_string(),
|
2015-07-07 21:40:31 +00:00
|
|
|
device_id: name.clone(),
|
|
|
|
cache_location: PathBuf::from(cache_location)
|
2015-05-09 10:07:24 +00:00
|
|
|
};
|
2015-06-23 14:38:29 +00:00
|
|
|
let session = Session::new(config);
|
2015-07-01 23:27:19 +00:00
|
|
|
session.login(username.clone(), password);
|
2015-06-23 14:38:29 +00:00
|
|
|
session.poll();
|
2015-05-09 10:07:24 +00:00
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
let poll_thread = thread::scoped(|| {
|
|
|
|
loop {
|
|
|
|
session.poll();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-07-02 19:42:49 +00:00
|
|
|
let player = Player::new(&session);
|
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
SpircManager {
|
|
|
|
session: &session,
|
2015-07-02 19:42:49 +00:00
|
|
|
player: &player,
|
|
|
|
|
2015-07-01 23:27:19 +00:00
|
|
|
username: username.clone(),
|
2015-07-02 19:42:49 +00:00
|
|
|
state_update_id: 0,
|
|
|
|
seq_nr: 0,
|
|
|
|
|
2015-07-07 21:40:31 +00:00
|
|
|
name: name,
|
2015-07-02 17:24:25 +00:00
|
|
|
ident: session.config.device_id.clone(),
|
2015-07-01 23:27:19 +00:00
|
|
|
device_type: 5,
|
2015-07-02 19:42:49 +00:00
|
|
|
can_play: true,
|
2015-07-01 23:27:19 +00:00
|
|
|
|
2015-07-02 19:42:49 +00:00
|
|
|
repeat: false,
|
|
|
|
shuffle: false,
|
2015-07-01 22:40:38 +00:00
|
|
|
volume: 0x8000,
|
2015-07-02 19:42:49 +00:00
|
|
|
|
2015-07-01 22:40:38 +00:00
|
|
|
is_active: false,
|
|
|
|
became_active_at: 0,
|
2015-07-02 11:54:34 +00:00
|
|
|
|
2015-07-02 19:42:49 +00:00
|
|
|
last_command_ident: String::new(),
|
|
|
|
last_command_msgid: 0,
|
|
|
|
|
|
|
|
track: None
|
2015-07-01 22:40:38 +00:00
|
|
|
}.run();
|
2015-07-01 17:49:03 +00:00
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
poll_thread.join();
|
2015-07-01 17:49:03 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
fn print_track(session: &Session, track_id: SpotifyId) {
|
|
|
|
let track : TrackRef = session.metadata(track_id);
|
2015-06-23 14:38:29 +00:00
|
|
|
|
|
|
|
let album : AlbumRef = {
|
|
|
|
let handle = track.wait();
|
|
|
|
let data = handle.unwrap();
|
|
|
|
eprintln!("{}", data.name);
|
2015-07-02 17:24:25 +00:00
|
|
|
session.metadata(data.album)
|
2015-06-23 14:38:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let artists : Vec<ArtistRef> = {
|
|
|
|
let handle = album.wait();
|
|
|
|
let data = handle.unwrap();
|
|
|
|
eprintln!("{}", data.name);
|
|
|
|
data.artists.iter().map(|id| {
|
2015-07-02 17:24:25 +00:00
|
|
|
session.metadata(*id)
|
2015-06-23 14:38:29 +00:00
|
|
|
}).collect()
|
|
|
|
};
|
|
|
|
|
|
|
|
for artist in artists {
|
|
|
|
let handle = artist.wait();
|
|
|
|
let data = handle.unwrap();
|
|
|
|
eprintln!("{}", data.name);
|
|
|
|
}
|
2015-04-25 20:32:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
struct SpircManager<'s> {
|
2015-07-02 19:42:49 +00:00
|
|
|
player: &'s Player<'s>,
|
2015-07-02 17:24:25 +00:00
|
|
|
session: &'s Session,
|
2015-07-02 19:42:49 +00:00
|
|
|
|
2015-07-01 23:27:19 +00:00
|
|
|
username: String,
|
2015-07-01 22:40:38 +00:00
|
|
|
state_update_id: i64,
|
|
|
|
seq_nr: u32,
|
|
|
|
|
|
|
|
name: String,
|
|
|
|
ident: String,
|
|
|
|
device_type: u8,
|
2015-07-02 19:42:49 +00:00
|
|
|
can_play: bool,
|
2015-07-01 22:40:38 +00:00
|
|
|
|
2015-07-02 19:42:49 +00:00
|
|
|
repeat: bool,
|
|
|
|
shuffle: bool,
|
2015-07-01 22:40:38 +00:00
|
|
|
volume: u16,
|
2015-07-02 19:42:49 +00:00
|
|
|
|
2015-07-01 22:40:38 +00:00
|
|
|
is_active: bool,
|
|
|
|
became_active_at: i64,
|
2015-07-02 11:54:34 +00:00
|
|
|
|
2015-07-02 19:42:49 +00:00
|
|
|
last_command_ident: String,
|
|
|
|
last_command_msgid: u32,
|
|
|
|
|
|
|
|
track: Option<SpotifyId>
|
2015-07-01 22:40:38 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
impl <'s> SpircManager<'s> {
|
2015-07-01 22:40:38 +00:00
|
|
|
fn run(&mut self) {
|
2015-07-02 19:42:49 +00:00
|
|
|
let rx = self.session.mercury_sub(format!("hm://remote/user/{}/v23", self.username));
|
2015-07-01 22:40:38 +00:00
|
|
|
|
|
|
|
self.notify(None);
|
|
|
|
|
2015-07-02 19:42:49 +00:00
|
|
|
loop {
|
|
|
|
if let Ok(pkt) = rx.try_recv() {
|
|
|
|
let frame = protobuf::parse_from_bytes::<protocol::spirc::Frame>(
|
|
|
|
pkt.payload.front().unwrap()).unwrap();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let h = self.player.state.0.lock().unwrap();
|
|
|
|
if h.update_time > self.state_update_id {
|
|
|
|
self.state_update_id = util::now_ms();
|
|
|
|
drop(h);
|
|
|
|
self.notify(None);
|
2015-07-02 11:54:34 +00:00
|
|
|
}
|
2015-07-01 22:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle(&mut self, frame: protocol::spirc::Frame) {
|
2015-07-02 19:42:49 +00:00
|
|
|
if frame.get_recipient().len() > 0 {
|
|
|
|
self.last_command_ident = frame.get_ident().to_string();
|
|
|
|
self.last_command_msgid = frame.get_seq_nr();
|
|
|
|
}
|
2015-07-01 22:40:38 +00:00
|
|
|
match frame.get_typ() {
|
|
|
|
protocol::spirc::MessageType::kMessageTypeHello => {
|
|
|
|
self.notify(Some(frame.get_ident()));
|
|
|
|
}
|
|
|
|
protocol::spirc::MessageType::kMessageTypeLoad => {
|
2015-07-02 19:42:49 +00:00
|
|
|
if !self.is_active {
|
|
|
|
self.is_active = true;
|
|
|
|
self.became_active_at = util::now_ms();
|
|
|
|
}
|
2015-07-02 11:54:34 +00:00
|
|
|
|
2015-07-02 19:42:49 +00:00
|
|
|
let index = frame.get_state().get_playing_track_index() as usize;
|
|
|
|
let track = SpotifyId::from_raw(frame.get_state().get_track()[index].get_gid());
|
|
|
|
self.track = Some(track);
|
|
|
|
self.player.command(PlayerCommand::Load(track,
|
|
|
|
frame.get_state().get_status() == PlayStatus::kPlayStatusPlay,
|
|
|
|
frame.get_state().get_position_ms()));
|
2015-07-02 11:54:34 +00:00
|
|
|
}
|
|
|
|
protocol::spirc::MessageType::kMessageTypePlay => {
|
2015-07-02 19:42:49 +00:00
|
|
|
self.player.command(PlayerCommand::Play);
|
2015-07-02 11:54:34 +00:00
|
|
|
}
|
|
|
|
protocol::spirc::MessageType::kMessageTypePause => {
|
2015-07-02 19:42:49 +00:00
|
|
|
self.player.command(PlayerCommand::Pause);
|
2015-07-02 11:54:34 +00:00
|
|
|
}
|
|
|
|
protocol::spirc::MessageType::kMessageTypeSeek => {
|
2015-07-02 19:42:49 +00:00
|
|
|
self.player.command(PlayerCommand::Seek(frame.get_position()));
|
2015-07-02 11:54:34 +00:00
|
|
|
}
|
|
|
|
protocol::spirc::MessageType::kMessageTypeNotify => {
|
2015-07-02 19:42:49 +00:00
|
|
|
if self.is_active && frame.get_device_state().get_is_active() {
|
|
|
|
self.is_active = false;
|
|
|
|
self.player.command(PlayerCommand::Stop);
|
2015-07-02 11:54:34 +00:00
|
|
|
}
|
2015-07-01 22:40:38 +00:00
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn notify(&mut self, recipient: Option<&str>) {
|
2015-07-02 11:54:34 +00:00
|
|
|
let mut pkt = protobuf_init!(protocol::spirc::Frame::new(), {
|
|
|
|
version: 1,
|
|
|
|
ident: self.ident.clone(),
|
|
|
|
protocol_version: "2.0.0".to_string(),
|
|
|
|
seq_nr: { self.seq_nr += 1; self.seq_nr },
|
|
|
|
typ: protocol::spirc::MessageType::kMessageTypeNotify,
|
|
|
|
device_state: self.device_state(),
|
|
|
|
recipient: protobuf::RepeatedField::from_vec(
|
|
|
|
recipient.map(|r| vec![r.to_string()] ).unwrap_or(vec![])
|
|
|
|
),
|
|
|
|
state_update_id: self.state_update_id
|
|
|
|
});
|
|
|
|
|
|
|
|
if self.is_active {
|
2015-07-02 19:42:49 +00:00
|
|
|
pkt.set_state(self.state());
|
2015-07-02 11:54:34 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
self.session.mercury(MercuryRequest{
|
2015-07-01 22:40:38 +00:00
|
|
|
method: MercuryMethod::SEND,
|
2015-07-02 11:54:34 +00:00
|
|
|
uri: format!("hm://remote/user/{}", self.username),
|
2015-07-01 22:40:38 +00:00
|
|
|
content_type: None,
|
2015-07-02 11:54:34 +00:00
|
|
|
payload: vec![ pkt.write_to_bytes().unwrap() ]
|
2015-07-02 17:24:25 +00:00
|
|
|
});
|
2015-07-01 22:40:38 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 19:42:49 +00:00
|
|
|
fn state(&mut self) -> protocol::spirc::State {
|
|
|
|
let state = self.player.state.0.lock().unwrap();
|
|
|
|
|
|
|
|
protobuf_init!(protocol::spirc::State::new(), {
|
|
|
|
status: state.status,
|
|
|
|
position_ms: state.position_ms,
|
|
|
|
position_measured_at: state.position_measured_at as u64,
|
|
|
|
|
|
|
|
playing_track_index: 0,
|
|
|
|
track => [
|
|
|
|
@{
|
|
|
|
gid: self.track.unwrap().to_raw().to_vec()
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
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-01 22:40:38 +00:00
|
|
|
fn device_state(&mut self) -> protocol::spirc::DeviceState {
|
|
|
|
protobuf_init!(protocol::spirc::DeviceState::new(), {
|
2015-07-01 23:27:19 +00:00
|
|
|
sw_version: version_string(),
|
2015-07-01 22:40:38 +00:00
|
|
|
is_active: self.is_active,
|
|
|
|
can_play: self.can_play,
|
|
|
|
volume: self.volume as u32,
|
|
|
|
name: self.name.clone(),
|
|
|
|
error_code: 0,
|
|
|
|
became_active_at: if self.is_active { self.became_active_at } else { 0 },
|
|
|
|
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 => [
|
|
|
|
"album".to_string(),
|
|
|
|
"playlist".to_string(),
|
|
|
|
"search".to_string(),
|
|
|
|
"inbox".to_string(),
|
|
|
|
"toplist".to_string(),
|
|
|
|
"starred".to_string(),
|
|
|
|
"publishedstarred".to_string(),
|
|
|
|
"track".to_string(),
|
|
|
|
]
|
|
|
|
},
|
|
|
|
@{
|
|
|
|
typ: protocol::spirc::CapabilityType::kSupportedTypes,
|
|
|
|
stringValue => [
|
|
|
|
"audio/local".to_string(),
|
|
|
|
"audio/track".to_string(),
|
|
|
|
"local".to_string(),
|
|
|
|
"track".to_string(),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|