2017-01-18 20:39:46 +00:00
|
|
|
use byteorder::{BigEndian, ByteOrder, WriteBytesExt};
|
|
|
|
use futures::sync::oneshot;
|
2015-09-01 11:20:37 +00:00
|
|
|
use std::collections::HashMap;
|
2017-01-18 20:39:46 +00:00
|
|
|
use std::io::Write;
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2017-01-18 20:39:46 +00:00
|
|
|
use util::SeqGenerator;
|
2015-09-01 11:20:37 +00:00
|
|
|
use util::{SpotifyId, FileId};
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-12-29 22:12:02 +00:00
|
|
|
#[derive(Debug,Hash,PartialEq,Eq,Copy,Clone)]
|
2017-01-18 20:39:46 +00:00
|
|
|
pub struct AudioKey(pub [u8; 16]);
|
2015-07-02 21:05:47 +00:00
|
|
|
|
2016-03-16 04:07:04 +00:00
|
|
|
#[derive(Debug,Hash,PartialEq,Eq,Copy,Clone)]
|
2017-01-18 20:39:46 +00:00
|
|
|
pub struct AudioKeyError;
|
2015-07-02 21:05:47 +00:00
|
|
|
|
2017-01-18 21:33:52 +00:00
|
|
|
pub type Result = ::std::result::Result<AudioKey, AudioKeyError>;
|
2017-01-18 20:39:46 +00:00
|
|
|
|
|
|
|
component! {
|
|
|
|
AudioKeyManager : AudioKeyManagerInner {
|
|
|
|
sequence: SeqGenerator<u32> = SeqGenerator::new(0),
|
|
|
|
pending: HashMap<u32, oneshot::Sender<Result>> = HashMap::new(),
|
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioKeyManager {
|
2017-01-18 20:39:46 +00:00
|
|
|
pub fn dispatch(&self, cmd: u8, data: Vec<u8>) {
|
|
|
|
let seq = BigEndian::read_u32(&data[..4]);
|
|
|
|
|
|
|
|
let sender = self.lock(|inner| inner.pending.remove(&seq));
|
|
|
|
|
|
|
|
if let Some(sender) = sender {
|
|
|
|
match cmd {
|
|
|
|
0xd => {
|
|
|
|
let mut key = [0u8; 16];
|
|
|
|
key.copy_from_slice(&data[4..20]);
|
|
|
|
sender.complete(Ok(AudioKey(key)));
|
|
|
|
}
|
|
|
|
0xe => {
|
|
|
|
warn!("error audio key {:x} {:x}", data[4], data[5]);
|
|
|
|
sender.complete(Err(AudioKeyError));
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
2015-07-02 17:24:25 +00:00
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 20:39:46 +00:00
|
|
|
pub fn request<'a>(&self, track: SpotifyId, file: FileId) -> oneshot::Receiver<Result> {
|
|
|
|
let (tx, rx) = oneshot::channel();
|
|
|
|
|
|
|
|
let seq = self.lock(move |inner| {
|
|
|
|
let seq = inner.sequence.get();
|
|
|
|
inner.pending.insert(seq, tx);
|
|
|
|
seq
|
|
|
|
});
|
2016-01-02 15:48:44 +00:00
|
|
|
|
2017-01-18 20:39:46 +00:00
|
|
|
self.send_key_request(seq, track, file);
|
|
|
|
rx
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_key_request<'a>(&self, seq: u32, track: SpotifyId, file: FileId) {
|
2016-01-02 15:48:44 +00:00
|
|
|
let mut data: Vec<u8> = Vec::new();
|
|
|
|
data.write(&file.0).unwrap();
|
|
|
|
data.write(&track.to_raw()).unwrap();
|
|
|
|
data.write_u32::<BigEndian>(seq).unwrap();
|
|
|
|
data.write_u16::<BigEndian>(0x0000).unwrap();
|
|
|
|
|
2017-01-18 20:39:46 +00:00
|
|
|
self.session().send_packet(0xc, data)
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
}
|