librespot/core/src/audio_key.rs

105 lines
3.1 KiB
Rust
Raw Normal View History

use std::{collections::HashMap, io::Write};
2017-01-18 20:39:46 +00:00
use byteorder::{BigEndian, ByteOrder, WriteBytesExt};
use bytes::Bytes;
use thiserror::Error;
use tokio::sync::oneshot;
2015-06-23 14:38:29 +00:00
use crate::{packet::PacketType, util::SeqGenerator, Error, FileId, SpotifyId};
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
#[derive(Debug, Error)]
pub enum AudioKeyError {
#[error("audio key error")]
AesKey,
#[error("other end of channel disconnected")]
Channel,
#[error("unexpected packet type {0}")]
Packet(u8),
#[error("sequence {0} not pending")]
Sequence(u32),
}
impl From<AudioKeyError> for Error {
fn from(err: AudioKeyError) -> Self {
match err {
AudioKeyError::AesKey => Error::unavailable(err),
AudioKeyError::Channel => Error::aborted(err),
AudioKeyError::Sequence(_) => Error::aborted(err),
AudioKeyError::Packet(_) => Error::unimplemented(err),
}
}
}
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),
pending: HashMap<u32, oneshot::Sender<Result<AudioKey, Error>>> = HashMap::new(),
2017-01-18 20:39:46 +00:00
}
2015-06-23 14:38:29 +00:00
}
impl AudioKeyManager {
pub(crate) fn dispatch(&self, cmd: PacketType, mut data: Bytes) -> Result<(), Error> {
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))
.ok_or(AudioKeyError::Sequence(seq))?;
2017-01-18 20:39:46 +00:00
match cmd {
PacketType::AesKey => {
let mut key = [0u8; 16];
key.copy_from_slice(data.as_ref());
sender
.send(Ok(AudioKey(key)))
.map_err(|_| AudioKeyError::Channel)?
}
PacketType::AesKeyError => {
error!(
"error audio key {:x} {:x}",
data.as_ref()[0],
data.as_ref()[1]
);
sender
.send(Err(AudioKeyError::AesKey.into()))
.map_err(|_| AudioKeyError::Channel)?
}
_ => {
trace!(
"Did not expect {:?} AES key packet with data {:#?}",
cmd,
data
);
return Err(AudioKeyError::Packet(cmd as u8).into());
2017-01-18 20:39:46 +00:00
}
}
Ok(())
2015-06-23 14:38:29 +00:00
}
pub async fn request(&self, track: SpotifyId, file: FileId) -> Result<AudioKey, Error> {
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
self.send_key_request(seq, track, file)?;
rx.await?
2017-01-18 20:39:46 +00:00
}
fn send_key_request(&self, seq: u32, track: SpotifyId, file: FileId) -> Result<(), Error> {
2016-01-02 15:48:44 +00:00
let mut data: Vec<u8> = Vec::new();
data.write_all(&file.0)?;
data.write_all(&track.to_raw())?;
data.write_u32::<BigEndian>(seq)?;
data.write_u16::<BigEndian>(0x0000)?;
2016-01-02 15:48:44 +00:00
self.session().send_packet(PacketType::RequestKey, data)
2015-06-23 14:38:29 +00:00
}
}