feat: lossless (FLAC) streaming
Adds a Lossless streaming quality that asks /get-file-info for FLAC and decrypts its encraw stream in memory. FLAC and FLAC-in-MP4 decode through symphonia. When Yandex grants no FLAC, its lossy reply is played as is, except HE-AAC, which falls back to the MP3 path. The player bar badge shows the codec actually served and turns accent-coloured for FLAC. Adds a read-only stream_probe example.
This commit is contained in:
1 parent
c85f5785fd
commit
24ff874d34
8 files changed
+175
-24
No files matched your search
@@ -7,7 +7,7 @@ use crate::library::{
|
||||
};
|
||||
use crate::edit::{LikedIds, Lyrics, QueueContext};
|
||||
use crate::discover::{ChartEntry, Genre, Home, SearchKind, SearchResults};
|
||||
use crate::models::{Stream, Track, best_variant};
|
||||
use crate::models::{Stream, Track, best_variant, file_info_stream};
|
||||
use crate::wave::{PlayReport, Station, WaveBatch, WaveEvent, WaveSettings, iso_millis, numeric_id};
|
||||
|
||||
use crate::{Error, Result};
|
||||
@@ -145,16 +145,37 @@ impl YmClient {
|
||||
.ok_or_else(|| Error::Api(format!("no playable variant for track {track_id}")))?;
|
||||
let url = variant.direct_link(self.inner.transport()).await?;
|
||||
Ok(Stream {
|
||||
url,
|
||||
urls: vec![url],
|
||||
codec: variant.codec.clone().unwrap_or_default(),
|
||||
bitrate_kbps: variant.bitrate_in_kbps.unwrap_or(0).max(0) as u32,
|
||||
preview: variant.preview.unwrap_or(false),
|
||||
key: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Downloads a whole stream into memory (a 320 kbps track is about 10 MB).
|
||||
/// FLAC when Yandex grants it for this track and account. Otherwise Yandex
|
||||
/// answers with the best lossy file it allows (often AAC 192), which is played
|
||||
/// as is unless it's HE-AAC, which the decoder can't do justice; then the MP3
|
||||
/// path, as [`stream`](Self::stream) with no cap.
|
||||
pub async fn lossless_stream(&self, track_id: &str) -> Result<Stream> {
|
||||
match self.inner.track_lossless_info(track_id, "lossless").await.map_err(Error::from) {
|
||||
Ok(info) => {
|
||||
if let Some(stream) = file_info_stream(info) {
|
||||
return Ok(stream);
|
||||
}
|
||||
}
|
||||
Err(e @ (Error::Unauthorized(_) | Error::Network(_))) => return Err(e),
|
||||
// An API refusal (region, rights) still leaves the MP3 path.
|
||||
Err(_) => {}
|
||||
}
|
||||
self.stream(track_id, None).await
|
||||
}
|
||||
|
||||
/// Downloads a whole stream into memory, decrypting it when it is encrypted
|
||||
/// (MP3 320 is about 10 MB a track, FLAC 30–60 MB).
|
||||
pub async fn fetch(&self, stream: &Stream) -> Result<Arc<[u8]>> {
|
||||
let bytes = yamuse::download::Downloader::new(self.inner.transport(), [stream.url.clone()])
|
||||
let bytes = yamuse::download::Downloader::new(self.inner.transport(), stream.urls.iter().cloned())
|
||||
.maybe_encraw_key(stream.key.clone())
|
||||
.to_bytes()
|
||||
.await?;
|
||||
Ok(bytes.into())
|
||||
|
||||
Reference in new issue
Block a user