2017-01-18 20:39:46 +00:00
|
|
|
use byteorder::{BigEndian, ByteOrder, WriteBytesExt};
|
|
|
|
use futures::sync::oneshot;
|
2017-01-19 12:56:49 +00:00
|
|
|
use futures::{Async, Future, Poll};
|
2015-09-01 11:20:37 +00:00
|
|
|
use std::collections::HashMap;
|
2017-01-18 20:39:46 +00:00
|
|
|
use std::io::Write;
|
2017-01-29 17:54:32 +00:00
|
|
|
use tokio_core::io::EasyBuf;
|
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 20:39:46 +00:00
|
|
|
component! {
|
|
|
|
AudioKeyManager : AudioKeyManagerInner {
|
|
|
|
sequence: SeqGenerator<u32> = SeqGenerator::new(0),
|
2017-01-19 12:56:49 +00:00
|
|
|
pending: HashMap<u32, oneshot::Sender<Result<AudioKey, AudioKeyError>>> = HashMap::new(),
|
2017-01-18 20:39:46 +00:00
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioKeyManager {
|
2017-01-29 17:54:32 +00:00
|
|
|
pub fn dispatch(&self, cmd: u8, mut data: EasyBuf) {
|
|
|
|
let seq = BigEndian::read_u32(data.drain_to(4).as_ref());
|
2017-01-18 20:39:46 +00:00
|
|
|
|
|
|
|
let sender = self.lock(|inner| inner.pending.remove(&seq));
|
|
|
|
|
|
|
|
if let Some(sender) = sender {
|
|
|
|
match cmd {
|
|
|
|
0xd => {
|
|
|
|
let mut key = [0u8; 16];
|
2017-01-29 17:54:32 +00:00
|
|
|
key.copy_from_slice(data.as_ref());
|
2017-01-18 20:39:46 +00:00
|
|
|
sender.complete(Ok(AudioKey(key)));
|
|
|
|
}
|
|
|
|
0xe => {
|
2017-01-29 17:54:32 +00:00
|
|
|
warn!("error audio key {:x} {:x}", data.as_ref()[0], data.as_ref()[1]);
|
2017-01-18 20:39:46 +00:00
|
|
|
sender.complete(Err(AudioKeyError));
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
2015-07-02 17:24:25 +00:00
|
|
|
}
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 16:25:09 +00:00
|
|
|
pub fn request(&self, track: SpotifyId, file: FileId) -> AudioKeyFuture<AudioKey> {
|
2017-01-18 20:39:46 +00:00
|
|
|
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);
|
2017-01-19 12:56:49 +00:00
|
|
|
AudioKeyFuture(rx)
|
2017-01-18 20:39:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 16:25:09 +00:00
|
|
|
fn send_key_request(&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
|
|
|
}
|
|
|
|
}
|
2017-01-19 12:56:49 +00:00
|
|
|
|
|
|
|
pub struct AudioKeyFuture<T>(oneshot::Receiver<Result<T, AudioKeyError>>);
|
|
|
|
impl <T> Future for AudioKeyFuture<T> {
|
|
|
|
type Item = T;
|
|
|
|
type Error = AudioKeyError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<T, AudioKeyError> {
|
|
|
|
match self.0.poll() {
|
|
|
|
Ok(Async::Ready(Ok(value))) => Ok(Async::Ready(value)),
|
|
|
|
Ok(Async::Ready(Err(err))) => Err(err),
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
|
|
Err(oneshot::Canceled) => Err(AudioKeyError),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|