2017-01-18 20:39:46 +00:00
|
|
|
use byteorder::{BigEndian, ByteOrder, WriteBytesExt};
|
2018-01-21 20:52:31 +00:00
|
|
|
use bytes::Bytes;
|
2017-01-19 12:56:49 +00:00
|
|
|
use futures::{Async, Future, Poll};
|
2018-02-11 11:37:08 +00:00
|
|
|
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
|
|
|
|
2018-02-11 11:37:08 +00:00
|
|
|
use util::{FileId, SpotifyId};
|
2017-01-18 20:39:46 +00:00
|
|
|
use util::SeqGenerator;
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2018-02-11 11:37:08 +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
|
|
|
|
2018-02-11 11:37:08 +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 {
|
2018-02-09 20:06:16 +00:00
|
|
|
pub(crate) fn dispatch(&self, cmd: u8, mut data: Bytes) {
|
2018-01-21 20:52:31 +00:00
|
|
|
let seq = BigEndian::read_u32(data.split_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());
|
2018-01-21 18:51:48 +00:00
|
|
|
let _ = sender.send(Ok(AudioKey(key)));
|
2017-01-18 20:39:46 +00:00
|
|
|
}
|
|
|
|
0xe => {
|
2018-02-11 11:37:08 +00:00
|
|
|
warn!(
|
|
|
|
"error audio key {:x} {:x}",
|
|
|
|
data.as_ref()[0],
|
|
|
|
data.as_ref()[1]
|
|
|
|
);
|
2018-01-21 18:51:48 +00:00
|
|
|
let _ = sender.send(Err(AudioKeyError));
|
2017-01-18 20:39:46 +00:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
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>>);
|
2018-02-11 11:37:08 +00:00
|
|
|
impl<T> Future for AudioKeyFuture<T> {
|
2017-01-19 12:56:49 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|