Skip to content

Commit

Permalink
fix(playback): set pos correctly after seeking (#58)
Browse files Browse the repository at this point in the history
* fix(playback): set pos correctly after seeking

fixes 4de0f4e

* fix(playback): return correct `byte_len`
  • Loading branch information
snylonue authored Aug 30, 2024
1 parent 3b36d5b commit 9851c10
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions anni-playback/src/sources/cached_http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ pub struct CachedHttpSource {
#[allow(unused)]
buffer_signal: Arc<AtomicBool>,
duration: Option<u64>,
content_length: Option<u64>,
}

impl CachedHttpSource {
/// `cache_path` is the path to cache file.
pub fn new(
identifier: TrackIdentifier,
url: impl FnOnce() -> Option<(Url, Option<u64>)>,
url: impl FnOnce() -> Option<(Url, Option<u64>, Option<u64>)>,
cache_store: &CacheStore,
client: Client,
buffer_signal: Arc<AtomicBool>,
Expand All @@ -58,6 +59,7 @@ impl CachedHttpSource {
is_buffering: Arc::new(AtomicBool::new(false)),
buffer_signal,
duration: None,
content_length: Some(buf_len as u64),
});
}
Err(cache) => cache,
Expand All @@ -66,7 +68,7 @@ impl CachedHttpSource {
let buf_len = Arc::new(AtomicUsize::new(0));
let is_buffering = Arc::new(AtomicBool::new(true));

let (url, duration) = url().ok_or(OpenTrackError::NoAvailableAnnil)?;
let (url, duration, content_length) = url().ok_or(OpenTrackError::NoAvailableAnnil)?;

log::debug!("got duration {duration:?}");

Expand Down Expand Up @@ -124,6 +126,7 @@ impl CachedHttpSource {
is_buffering,
buffer_signal,
duration,
content_length,
})
}
}
Expand Down Expand Up @@ -157,7 +160,7 @@ impl Read for CachedHttpSource {
impl Seek for CachedHttpSource {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
let p = self.cache.seek(pos)?;
self.pos += p as usize;
self.pos = p as usize;
Ok(p)
}
}
Expand All @@ -168,9 +171,8 @@ impl MediaSource for CachedHttpSource {
}

fn byte_len(&self) -> Option<u64> {
let len = self.buf_len.load(Ordering::Acquire) as u64;
log::trace!("returning buf_len {len}");
Some(len)
log::trace!("returning byte len {:?}", self.content_length);
self.content_length
}
}

Expand Down Expand Up @@ -213,7 +215,7 @@ impl CachedAnnilSource {
content_length,
);
}
(url.clone(), duration)
(url.clone(), duration, r.content_length())
});

CachedHttpSource::new(track, || source.next(), cache_store, client, buffer_signal).map(Self)
Expand Down

0 comments on commit 9851c10

Please sign in to comment.