A seek lands where its packets can say what time it is
Browse files at this commit5a98e1b8d29883e294f2cfa71400236b4e414a07Parent: 254c09a- fix
- a seek lands where its packets can say what time it is
1 changed file +34 -7
- crates/ec-ogg/src/demux.rs +34 -7
crates/ec-ogg/src/demux.rsmodified+34 -7
| @@ -422,18 +422,18 @@ impl<R: Read + Seek + Send> Demuxer for OggDemuxer<R> { | ||
| 422 | 422 | let mut hi = end; |
| 423 | 423 | // Last page start whose granule is at or before the target, and the |
| 424 | 424 | // first one at or after it — one bisection answers both. |
| 425 | - let mut before = self.data_start; | |
| 426 | - let mut after = None; | |
| 425 | + let mut before: Option<(u64, i64)> = None; | |
| 426 | + let mut after: Option<(u64, i64)> = None; | |
| 427 | 427 | while lo < hi { |
| 428 | 428 | let mid = lo + (hi - lo) / 2; |
| 429 | 429 | match self.page_at_or_after(mid, serial)? { |
| 430 | 430 | Some((offset, granule)) if granule != page::NO_GRANULE && granule <= target => { |
| 431 | - before = offset; | |
| 431 | + before = Some((offset, granule)); | |
| 432 | 432 | // Always make progress: the found page may start before mid. |
| 433 | 433 | lo = offset.max(mid) + 1; |
| 434 | 434 | } |
| 435 | 435 | Some((offset, granule)) if granule != page::NO_GRANULE => { |
| 436 | - after = Some(offset); | |
| 436 | + after = Some((offset, granule)); | |
| 437 | 437 | hi = match offset > mid { |
| 438 | 438 | true => mid, |
| 439 | 439 | false => offset, |
| @@ -445,12 +445,39 @@ impl<R: Read + Seek + Send> Demuxer for OggDemuxer<R> { | ||
| 445 | 445 | None => hi = mid, |
| 446 | 446 | } |
| 447 | 447 | } |
| 448 | - let landing = match mode { | |
| 449 | - SeekMode::SyncAfter => after.unwrap_or(before), | |
| 448 | + let found = match mode { | |
| 449 | + SeekMode::SyncAfter => after.or(before), | |
| 450 | + // No page ends at or before the target: it is inside the first one, | |
| 451 | + // and the stream's beginning is what "at or before" means there. | |
| 450 | 452 | _ => before, |
| 451 | 453 | }; |
| 452 | 454 | self.reset_state(); |
| 453 | - self.inner.seek(SeekFrom::Start(landing))?; | |
| 455 | + let granule = match found { | |
| 456 | + // A granule is where a page *ends*, and a Vorbis or FLAC packet | |
| 457 | + // states no duration of its own — so reading from the page the | |
| 458 | + // bisection found would hand out its packets with no timestamp at | |
| 459 | + // all, and a seek could not say where it landed. The landing is the | |
| 460 | + // page *after* it, whose first sample is that page's granule. | |
| 461 | + Some((offset, granule)) => { | |
| 462 | + self.inner.seek(SeekFrom::Start(offset))?; | |
| 463 | + read_page(&mut self.inner)?; | |
| 464 | + granule | |
| 465 | + } | |
| 466 | + // Nothing indexed the target: the stream's own beginning is the | |
| 467 | + // only honest answer, and every packet of it is still to come. | |
| 468 | + None => { | |
| 469 | + self.inner.seek(SeekFrom::Start(self.data_start))?; | |
| 470 | + 0 | |
| 471 | + } | |
| 472 | + }; | |
| 473 | + // Never resume inside the headers: they are not audio and must not be | |
| 474 | + // handed out as packets. | |
| 475 | + if self.inner.stream_position()? < self.data_start { | |
| 476 | + self.inner.seek(SeekFrom::Start(self.data_start))?; | |
| 477 | + } | |
| 478 | + let track = &mut self.tracks[index]; | |
| 479 | + track.pos = granule; | |
| 480 | + track.pos_known = true; | |
| 454 | 481 | Ok(()) |
| 455 | 482 | } |
| 456 | 483 | } |