Improve prefetch for mixed playlists
- Look ahead 15 tracks instead of 3 to find Qobuz tracks past local ones - Prefetch up to 3 Qobuz tracks regardless of local track positions - Count cached/fetching tracks toward limit to avoid over-prefetching Example: [Local, Local, Qobuz, Qobuz, Local x4, Qobuz] Before: Only prefetched tracks 3,4 (next 3 total) After: Prefetches tracks 3,4,9 (next 3 Qobuz)
This commit is contained in:
1 parent
94d7d78a22
commit
2bdde3d592
1 file changed
+19
-5
@@ -194,24 +194,35 @@ async fn download_audio(url: &str) -> Result<Vec<u8>, String> {
|
|||||||
Ok(bytes.to_vec())
|
Ok(bytes.to_vec())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Number of tracks to prefetch ahead
|
/// Number of Qobuz tracks to prefetch (not total tracks, just Qobuz)
|
||||||
const PREFETCH_COUNT: usize = 3;
|
const QOBUZ_PREFETCH_COUNT: usize = 3;
|
||||||
|
|
||||||
/// Spawn background tasks to prefetch upcoming tracks
|
/// How far ahead to look for tracks to prefetch (to handle mixed playlists)
|
||||||
|
const PREFETCH_LOOKAHEAD: usize = 15;
|
||||||
|
|
||||||
|
/// Spawn background tasks to prefetch upcoming Qobuz tracks
|
||||||
|
/// For mixed playlists, we look further ahead to find Qobuz tracks past local ones
|
||||||
fn spawn_prefetch(
|
fn spawn_prefetch(
|
||||||
client: Arc<Mutex<QobuzClient>>,
|
client: Arc<Mutex<QobuzClient>>,
|
||||||
cache: Arc<AudioCache>,
|
cache: Arc<AudioCache>,
|
||||||
queue: &QueueManager,
|
queue: &QueueManager,
|
||||||
) {
|
) {
|
||||||
// Get upcoming tracks from queue
|
// Look further ahead to find Qobuz tracks in mixed playlists
|
||||||
let upcoming_tracks = queue.peek_upcoming(PREFETCH_COUNT);
|
let upcoming_tracks = queue.peek_upcoming(PREFETCH_LOOKAHEAD);
|
||||||
|
|
||||||
if upcoming_tracks.is_empty() {
|
if upcoming_tracks.is_empty() {
|
||||||
log::debug!("No upcoming tracks to prefetch");
|
log::debug!("No upcoming tracks to prefetch");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut qobuz_prefetched = 0;
|
||||||
|
|
||||||
for track in upcoming_tracks {
|
for track in upcoming_tracks {
|
||||||
|
// Stop once we've prefetched enough Qobuz tracks
|
||||||
|
if qobuz_prefetched >= QOBUZ_PREFETCH_COUNT {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
let track_id = track.id;
|
let track_id = track.id;
|
||||||
let track_title = track.title.clone();
|
let track_title = track.title.clone();
|
||||||
|
|
||||||
@@ -224,16 +235,19 @@ fn spawn_prefetch(
|
|||||||
// Check if already cached or being fetched
|
// Check if already cached or being fetched
|
||||||
if cache.contains(track_id) {
|
if cache.contains(track_id) {
|
||||||
log::debug!("Track {} already cached", track_id);
|
log::debug!("Track {} already cached", track_id);
|
||||||
|
qobuz_prefetched += 1; // Count as "handled"
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if cache.is_fetching(track_id) {
|
if cache.is_fetching(track_id) {
|
||||||
log::debug!("Track {} already being fetched", track_id);
|
log::debug!("Track {} already being fetched", track_id);
|
||||||
|
qobuz_prefetched += 1; // Count as "handled"
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark as fetching
|
// Mark as fetching
|
||||||
cache.mark_fetching(track_id);
|
cache.mark_fetching(track_id);
|
||||||
|
qobuz_prefetched += 1;
|
||||||
|
|
||||||
let client_clone = client.clone();
|
let client_clone = client.clone();
|
||||||
let cache_clone = cache.clone();
|
let cache_clone = cache.clone();
|
||||||
|
|||||||
Reference in new issue
Block a user