mirror of
https://github.com/narrrl/proton-stream.git
synced 2026-11-04 16:57:00 +01:00
perf(stream,player,app): start a resumed episode at its place, with that block already coming
Resuming loaded the file from the start and sought on FileLoaded. By then mpv's demuxer had begun reading ahead from the beginning, so the blocks the seek wanted queued behind ones fetched to be thrown away. Player::play_from sets mpv's start option instead, and puts it back once the file has loaded. VideoStream::warm_resume fetches, beside the two edge blocks, the block the resume position falls in, estimated from the running time saved with the watch state. A video's bitrate is uneven, so it can land a block off; then it is one block for nothing, against a round trip saved when it lands.
This commit is contained in:
1 parent
47c482498b
commit
8d795c29e6
4 files changed
+121
-18
No files matched your search
@@ -223,20 +223,48 @@ impl VideoStream {
|
||||
/// seek, and a seek cancels read-ahead — which here would cancel the very
|
||||
/// fetch it was meant to have started.
|
||||
pub fn warm_edges(&self) {
|
||||
let Ok(runtime) = tokio::runtime::Handle::try_current() else {
|
||||
return;
|
||||
};
|
||||
let count = self.inner.map.len();
|
||||
let edges: &[usize] = match count {
|
||||
0 => &[],
|
||||
1 => &[0],
|
||||
_ => &[0, count - 1],
|
||||
};
|
||||
for &index in edges {
|
||||
self.warm(edges);
|
||||
}
|
||||
|
||||
/// [`Self::warm_edges`], and the block `fraction` of the way through the
|
||||
/// file: where a resumed episode seeks once its index is read.
|
||||
///
|
||||
/// An estimate, from the bytes being spread evenly over the running time.
|
||||
/// A video's bitrate is not even, so it can land a block off the one the
|
||||
/// seek wants. Then it is one block fetched for nothing, against a round
|
||||
/// trip saved, after the two the edges cost, when it lands.
|
||||
pub fn warm_resume(&self, fraction: f64) {
|
||||
self.warm_edges();
|
||||
if !(0.0..1.0).contains(&fraction) {
|
||||
return;
|
||||
}
|
||||
let offset = (self.inner.map.size() as f64 * fraction) as u64;
|
||||
let count = self.inner.map.len();
|
||||
if let Some(index) = self.inner.map.block_at(offset)
|
||||
&& index != 0
|
||||
&& index + 1 != count
|
||||
{
|
||||
self.warm(&[index]);
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetch these blocks in the background. Detached: see
|
||||
/// [`Self::warm_edges`] for why not read-ahead.
|
||||
fn warm(&self, indices: &[usize]) {
|
||||
let Ok(runtime) = tokio::runtime::Handle::try_current() else {
|
||||
return;
|
||||
};
|
||||
for &index in indices {
|
||||
let inner = Arc::clone(&self.inner);
|
||||
runtime.spawn(async move {
|
||||
if let Err(error) = block_at(inner, index).await {
|
||||
tracing::debug!(index, %error, "warming an edge block failed");
|
||||
tracing::debug!(index, %error, "warming a block failed");
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1037,6 +1065,21 @@ mod tests {
|
||||
assert_eq!(fake.fetches(), 2);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn warming_a_resume_fetches_the_block_that_far_in_besides_the_edges() {
|
||||
let fake = Fake::new(&[1000; 10]);
|
||||
let stream = stream_over(Arc::clone(&fake), 0);
|
||||
|
||||
stream.warm_resume(0.45);
|
||||
assert!(
|
||||
eventually(|| {
|
||||
fake.fetches_of(0) == 1 && fake.fetches_of(4) == 1 && fake.fetches_of(9) == 1
|
||||
})
|
||||
.await
|
||||
);
|
||||
assert_eq!(fake.fetches(), 3);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_fetched_block_reaches_the_disk_cache_without_the_read_waiting_for_it() {
|
||||
let disk = disk_in("fill").await;
|
||||
|
||||
Reference in new issue
Block a user