Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.
Kotlin alternativeandroidandroid-applicationclientglassmorphisminnertubelibrespotliquid-glassspotifyspotify-connectspotifyapiyoutube-musicyoutube-playerytdlnis
use std::{
collections::HashMap,
io::Write,
sync::{
Mutex, OnceLock,
atomic::{AtomicBool, Ordering},
},
time::{Duration, Instant},
};
use byteorder::{BigEndian, ByteOrder, WriteBytesExt};
use bytes::Bytes;
use thiserror::Error;
use tokio::sync::oneshot;
use crate::{Error, FileId, SpotifyId, packet::PacketType, util::SeqGenerator};
#[derive(Debug, Hash, PartialEq, Eq, Copy, Clone)]
pub struct AudioKey(pub [u8; 16]);
#[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),
#[error("audio key response timeout")]
Timeout,
}
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),
AudioKeyError::Timeout => Error::aborted(err),
}
}
}
// LOCAL PATCH: `cool_off_until` is when the next key may be asked for, and
// `cool_off` how long the wait becomes if that one is refused too. The macro
// takes no doc comments on its fields, hence this note; see
// [AudioKeyManager::request] for what they are for.
component! {
AudioKeyManager : AudioKeyManagerInner {
sequence: SeqGenerator<u32> = SeqGenerator::new(0),
pending: HashMap<u32, oneshot::Sender<Result<AudioKey, Error>>> = HashMap::new(),
cool_off_until: Option<Instant> = None,
cool_off: Duration = FIRST_COOL_OFF,
known: Vec<((SpotifyId, FileId), AudioKey)> = Vec::new(),
}
}
/// LOCAL PATCH: how many keys are remembered for the life of the session.
///
/// A key belongs to a file of a track and does not change, so a track played
/// again asks for nothing. Worth remembering because the thing that runs into
/// Spotify's limit is a listener going back and forth over the same handful of
/// songs: without this, every pass spent another request, and the refusals
/// that followed held up the song they actually stopped on.
///
/// Sixty-four of them is a few kilobytes and further back than anybody skips.
const KEYS_REMEMBERED: usize = 64;
/// LOCAL PATCH: how long to leave the key service alone after it refuses one.
///
/// Spotify meters these: a burst of requests — a run of skips, a preload
/// landing on top of a load, a failed load being tried again — is answered by
/// refusing everything for around a minute, and every retry inside that minute
/// makes it worse rather than better. Measured on this app: thirty-nine
/// requests in seventy seconds, four different tracks, none of them playable,
/// and everything fine on either side of the window.
///
/// So a refusal puts the whole session on hold, doubling until the cap, and one
/// key that comes back clears it.
/// LOCAL PATCH: when playback last asked for a key.
///
/// Spotify limits audio keys per session, and a download queue asking for one
/// spends the same allowance a track starting needs. Without somewhere to look
/// this up, a library being downloaded quietly starved playback: the queue took
/// the refusals, the session-wide hold that follows them applied to everything,
/// and pressing play did nothing for half a minute.
///
/// Only playback writes here. Downloads read it and wait; see `downloads.rs`.
static PLAYBACK_AT: Mutex<Option<Instant>> = Mutex::new(None);
/// Called by the player before it asks for a key.
pub fn note_playback_request() {
if let Ok(mut at) = PLAYBACK_AT.lock() {
*at = Some(Instant::now());
}
}
/// How long ago playback last asked, or `None` if it never has.
pub fn since_playback_request() -> Option<Duration> {
PLAYBACK_AT
.lock()
.ok()?
.map(|at| Instant::now().saturating_duration_since(at))
}
/// LOCAL PATCH: told when a key playback asked for is refused, and when one
/// comes back after that.
///
/// So the app can say why nothing is starting. Without it a refused song sits
/// at the start with no time on it and nothing else happens, which to the
/// listener is the app broken rather than Spotify saying no. Downloads refused
/// on their own, with playback quiet, are not reported: nobody is waiting on a
/// song then, and the download queue shows its own wait.
///
/// The hook is given a [Refusal] as a number: 0 over, 1 refused, 2 refused to
/// the account.
static ON_PLAYBACK_REFUSAL: OnceLock<fn(i64)> = OnceLock::new();
static PLAYBACK_REFUSED: AtomicBool = AtomicBool::new(false);
static ACCOUNT_REPORTED: AtomicBool = AtomicBool::new(false);
static EVER_GRANTED: AtomicBool = AtomicBool::new(false);
static FIRST_REFUSED: Mutex<Option<Instant>> = Mutex::new(None);
/// What a refusal is reported as.
///
/// Spotify refuses these keys for two reasons that look the same on the wire.
/// A burst of requests is refused for a minute or so, and waiting fixes it.
/// Some accounts, mostly newer ones, are refused every key, all the time,
/// whatever device they are on (librespot#1649), and waiting fixes nothing:
/// telling them to try again in a few minutes sends them round in circles. An
/// account that has not had a single key since the app started, over a few
/// minutes of asking, is taken to be the second.
#[repr(i64)]
enum Refusal {
Over = 0,
Refused = 1,
Account = 2,
}
/// How recent a playback request has to be for a refusal to be its.
///
/// Longer than one load's own retries take: three refusals with the hold
/// doubling between them, plus the player trying the load again, run past a
/// minute, and at sixty seconds the last refusals of a load stopped counting.
const PLAYBACK_WAITING: Duration = Duration::from_secs(90);
/// How long refusals with no key in between make it the account's.
///
/// A burst of requests is refused for about a minute. At two minutes, the
/// refusals one press of play produces never got that far, so an account that
/// was refused every key was told for several minutes to wait and try again,
/// which could not help.
const ACCOUNT_AFTER: Duration = Duration::from_secs(70);
/// Sets where refusals are reported; see [ON_PLAYBACK_REFUSAL]. Only the first
/// call counts.
pub fn on_playback_refusal(hook: fn(i64)) {
let _ = ON_PLAYBACK_REFUSAL.set(hook);
}
fn report_refusal(refused: bool) {
let report = |what: Refusal| {
if let Some(hook) = ON_PLAYBACK_REFUSAL.get() {
hook(what as i64);
}
};
if !refused {
EVER_GRANTED.store(true, Ordering::SeqCst);
ACCOUNT_REPORTED.store(false, Ordering::SeqCst);
if let Ok(mut first) = FIRST_REFUSED.lock() {
*first = None;
}
if PLAYBACK_REFUSED.swap(false, Ordering::SeqCst) {
report(Refusal::Over);
}
return;
}
if !since_playback_request().is_some_and(|ago| ago < PLAYBACK_WAITING) {
return;
}
let refused_for = FIRST_REFUSED
.lock()
.map(|mut first| first.get_or_insert_with(Instant::now).elapsed())
.unwrap_or_default();
if !EVER_GRANTED.load(Ordering::SeqCst)
&& refused_for >= ACCOUNT_AFTER
&& !ACCOUNT_REPORTED.swap(true, Ordering::SeqCst)
{
PLAYBACK_REFUSED.store(true, Ordering::SeqCst);
report(Refusal::Account);
} else if !PLAYBACK_REFUSED.swap(true, Ordering::SeqCst) {
report(Refusal::Refused);
}
}
const FIRST_COOL_OFF: Duration = Duration::from_secs(3);
const MAX_COOL_OFF: Duration = Duration::from_secs(30);
impl AudioKeyManager {
pub(crate) fn dispatch(&self, cmd: PacketType, mut data: Bytes) -> Result<(), Error> {
// LOCAL PATCH: `split_to` and `read_u32` panic under four bytes; see
// the key below.
if data.len() < 4 {
return Err(AudioKeyError::Packet(cmd as u8).into());
}
let seq = BigEndian::read_u32(data.split_to(4).as_ref());
let sender = self
.lock(|inner| inner.pending.remove(&seq))
.ok_or(AudioKeyError::Sequence(seq))?;
match cmd {
PacketType::AesKey => {
// LOCAL PATCH: a key of the wrong length is refused rather than
// copied. `copy_from_slice` panics on a short packet, and the
// panic is on the task that reads every packet of the session,
// so one bad answer took the whole connection down with it.
// After go-librespot 287a478, which found the same packet
// being taken as a zero-padded, wrong key.
let Ok(key) = <[u8; 16]>::try_from(data.as_ref()) else {
error!("audio key of {} bytes, expected 16", data.len());
let _ = sender.send(Err(AudioKeyError::AesKey.into()));
return Ok(());
};
sender
.send(Ok(AudioKey(key)))
.map_err(|_| AudioKeyError::Channel)?
}
PacketType::AesKeyError => {
// LOCAL PATCH: read with get(), for the same reason as above.
let code = |at: usize| data.get(at).copied().unwrap_or_default();
error!("error audio key {:x} {:x}", code(0), code(1));
sender
.send(Err(AudioKeyError::AesKey.into()))
.map_err(|_| AudioKeyError::Channel)?
}
_ => {
trace!("Did not expect {cmd:?} AES key packet with data {data:#?}");
return Err(AudioKeyError::Packet(cmd as u8).into());
}
}
Ok(())
}
/// LOCAL PATCH: retries a refused or timed-out key before giving up.
///
/// Skipping quickly fires a burst of key requests, and Spotify answers some
/// of them with `AesKeyError`. Upstream gives up on the first refusal, and
/// the player then goes on to decode the still-encrypted bytes: the decoder
/// dies on the garbage, the track ends immediately, the engine advances,
/// and the next one is refused too. From the listener's side a run of skips
/// turns into silence with the queue still moving.
///
/// The refusal is rate limiting rather than a verdict on the track, so a
/// short wait usually clears it. Only the transient failures are retried;
/// a broken channel or an unexpected packet is structural and repeating it
/// would just add latency to an error that is not going to change.
pub async fn request(&self, track: SpotifyId, file: FileId) -> Result<AudioKey, Error> {
// LOCAL PATCH: asked for before, and the answer does not change.
if let Some(key) = self.lock(|inner| {
inner
.known
.iter()
.find(|((id, of), _)| *id == track && *of == file)
.map(|(_, key)| *key)
}) {
return Ok(key);
}
const ATTEMPTS: usize = 3;
let mut last = None;
for attempt in 0..ATTEMPTS {
// Whatever is left of the hold from the last refusal, whoever it
// was that ran into it.
if let Some(wait) = self.lock(|inner| {
inner
.cool_off_until
.and_then(|until| until.checked_duration_since(Instant::now()))
}) {
debug!("waiting {wait:?} before asking for another audio key");
tokio::time::sleep(wait).await;
}
match self.request_once(track, file).await {
Ok(key) => {
self.lock(|inner| {
// LOCAL PATCH: remembered, so the next pass over this
// track costs nothing; see KEYS_REMEMBERED.
inner.known.retain(|((id, of), _)| !(*id == track && *of == file));
inner.known.push(((track, file), key));
if inner.known.len() > KEYS_REMEMBERED {
inner.known.remove(0);
}
inner.cool_off_until = None;
inner.cool_off = FIRST_COOL_OFF;
});
report_refusal(false);
return Ok(key);
}
Err(e) => {
// Through the boxed cause, not the wrapper. This `Error` is
// librespot's own: a `kind` plus the error it was built
// from, and only the latter says which failure this was.
let transient = matches!(
e.error.downcast_ref::<AudioKeyError>(),
Some(AudioKeyError::AesKey) | Some(AudioKeyError::Timeout)
);
if transient {
// The hold belongs to the session, not to this request:
// the next track is about to ask for one too, and it
// will be refused for the same reason.
self.lock(|inner| {
let wait = inner.cool_off;
inner.cool_off_until = Some(Instant::now() + wait);
inner.cool_off = (wait * 2).min(MAX_COOL_OFF);
warn!("audio key refused, holding off for {wait:?}");
});
report_refusal(true);
}
if !transient || attempt + 1 == ATTEMPTS {
return Err(e);
}
debug!("audio key attempt {} failed, retrying", attempt + 1);
last = Some(e);
}
}
}
Err(last.unwrap_or_else(|| AudioKeyError::Timeout.into()))
}
async fn request_once(&self, track: SpotifyId, file: FileId) -> Result<AudioKey, Error> {
let (tx, rx) = oneshot::channel();
let seq = self.lock(move |inner| {
let seq = inner.sequence.get();
inner.pending.insert(seq, tx);
seq
});
self.send_key_request(seq, track, file)?;
const KEY_RESPONSE_TIMEOUT: Duration = Duration::from_millis(1500);
match tokio::time::timeout(KEY_RESPONSE_TIMEOUT, rx).await {
Err(_) => {
error!("Audio key response timeout");
// Upstream leaves the entry behind, so a burst of timeouts
// grows the map for the life of the session.
self.lock(|inner| inner.pending.remove(&seq));
Err(AudioKeyError::Timeout.into())
}
Ok(k) => k?,
}
}
fn send_key_request(&self, seq: u32, track: SpotifyId, file: FileId) -> Result<(), Error> {
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)?;
self.session().send_packet(PacketType::RequestKey, data)
}
}