From 901ea4b08786f87e86f90d5b21395fd76723d0ab Mon Sep 17 00:00:00 2001 From: tilpner Date: Fri, 21 Apr 2023 13:32:48 +0200 Subject: [PATCH] fix(format/io): implement AVSEEK_SIZE seeking on stable --- Cargo.toml | 1 - src/format/io.rs | 21 ++++++++++++++++----- src/lib.rs | 1 - 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 16a7dc5..b3d5ace 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,6 @@ keywords = ["audio", "video"] [features] default = ["codec", "device", "filter", "format", "resampling", "software-scaling"] -unstable = [] serde = ["serde_", "serde_derive", "ffmpeg-sys/serde"] static = ["ffmpeg-sys/static"] diff --git a/src/format/io.rs b/src/format/io.rs index 1a12f35..f8d4231 100644 --- a/src/format/io.rs +++ b/src/format/io.rs @@ -90,18 +90,29 @@ unsafe extern "C" fn read_packet(opaque: *mut c_void, buf: *mut u8, size: c_int) result.try_into().unwrap() } +// copy of unstable `Seek::stream_len` +// https://github.com/rust-lang/rust/issues/59359 +fn stream_len(mut seek: impl Seek) -> io::Result { + let old_pos = seek.stream_position()?; + let len = seek.seek(SeekFrom::End(0))?; + + // Avoid seeking a third time when we were already at the end of the + // stream. The branch is usually way cheaper than a seek operation. + if old_pos != len { + seek.seek(SeekFrom::Start(old_pos))?; + } + + Ok(len) +} + unsafe extern "C" fn seek(opaque: *mut c_void, offset: i64, whence: c_int) -> i64 { let mut proxy = Box::::from_raw(opaque.cast()); let result: i64 = if whence == AVSEEK_SIZE { - #[cfg(feature = "unstable")] - match proxy.as_seek().stream_len() { + match stream_len(proxy.as_seek()) { Ok(size) => size.try_into().unwrap(), Err(err) => as_error(err).into(), } - - #[cfg(not(feature = "unstable"))] - 0 } else { let whence = match whence { diff --git a/src/lib.rs b/src/lib.rs index 1c8fcb3..7d37de1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,4 @@ #![allow(non_camel_case_types)] -#![cfg_attr(feature = "unstable", feature(seek_stream_len))] #![cfg_attr(feature = "cargo-clippy", allow(inline_always))] #[macro_use]