MP3: Fix hang (#52)

This commit is contained in:
Serial 2022-05-16 10:43:59 -04:00
parent 5e354e9cda
commit 9c021659fc
No known key found for this signature in database
GPG key ID: DA95198DC17C4568
2 changed files with 12 additions and 9 deletions

View file

@ -49,15 +49,17 @@ where
// Unlike `search_for_frame_sync`, since this has the `Seek` bound, it will seek the reader
// back to the start of the header.
const REV_FRAME_SEARCH_BOUNDS: u64 = 1024;
pub(super) fn rev_search_for_frame_sync<R>(input: &mut R) -> std::io::Result<Option<u64>>
pub(super) fn rev_search_for_frame_sync<R>(
input: &mut R,
pos: &mut u64,
) -> std::io::Result<Option<u64>>
where
R: Read + Seek,
{
let mut pos = input.stream_position()?;
let search_bounds = std::cmp::min(pos, REV_FRAME_SEARCH_BOUNDS);
let search_bounds = std::cmp::min(*pos, REV_FRAME_SEARCH_BOUNDS);
pos -= search_bounds;
input.seek(SeekFrom::Start(pos))?;
*pos -= search_bounds;
input.seek(SeekFrom::Start(*pos))?;
let ret = search_for_frame_sync(&mut input.take(search_bounds));
if let Ok(Some(_)) = ret {
@ -379,9 +381,9 @@ mod tests {
fn rev_search_for_frame_sync() {
fn test<R: Read + Seek>(reader: &mut R, expected_result: Option<u64>) {
// We have to start these at the end to do a reverse search, of course :)
reader.seek(SeekFrom::End(0)).unwrap();
let mut pos = reader.seek(SeekFrom::End(0)).unwrap();
let ret = super::rev_search_for_frame_sync(reader).unwrap();
let ret = super::rev_search_for_frame_sync(reader, &mut pos).unwrap();
assert_eq!(ret, expected_result);
}

View file

@ -150,8 +150,9 @@ where
reader.seek(SeekFrom::Start(last_frame_offset))?;
let mut last_frame = None;
while last_frame_offset > 0 {
match rev_search_for_frame_sync(reader) {
let mut pos = reader.stream_position()?;
while pos > 0 {
match rev_search_for_frame_sync(reader, &mut pos) {
// Found a frame sync, attempt to read a header
Ok(Some(_)) => {
// Move `last_frame_offset` back to the actual position