From 3d08dae9be521cd5bb0728d914c03624f95fa2ec Mon Sep 17 00:00:00 2001 From: Kornel Date: Thu, 11 Mar 2021 12:49:34 +0000 Subject: [PATCH] fix: support Path type in input/open functions --- src/format/mod.rs | 59 +++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/src/format/mod.rs b/src/format/mod.rs index 89a0690..46432e1 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -15,7 +15,7 @@ pub use self::format::{flag, list, Flags, Input, Output}; pub mod network; use std::{ - ffi::{CStr, CString}, + ffi::{CStr, CString, OsStr}, ptr, str::from_utf8_unchecked, }; @@ -52,10 +52,10 @@ pub fn license() -> &'static str { unsafe { from_utf8_unchecked(CStr::from_ptr(avformat_license()).to_bytes()) } } -pub fn open(path: impl AsRef, format: &Format) -> Result { +pub fn open>(path_or_url: P, format: &Format) -> Result { unsafe { let mut ps = ptr::null_mut(); - let path = CString::new(path.as_ref()).unwrap(); + let path = from_os_str(path_or_url); match *format { Format::Input(ref format) => match avformat_open_input( @@ -89,14 +89,14 @@ pub fn open(path: impl AsRef, format: &Format) -> Result { } } -pub fn open_with( - path: impl AsRef, +pub fn open_with>( + path_or_url: P, format: &Format, options: Dictionary, ) -> Result { unsafe { let mut ps = ptr::null_mut(); - let path = CString::new(path.as_ref()).unwrap(); + let path = from_os_str(path_or_url); let mut opts = options.disown(); match *format { @@ -132,10 +132,10 @@ pub fn open_with( } } -pub fn input(path: impl AsRef) -> Result { +pub fn input>(path_or_url: P) -> Result { unsafe { let mut ps = ptr::null_mut(); - let path = CString::new(path.as_ref()).unwrap(); + let path = from_os_str(path_or_url); match avformat_open_input(&mut ps, path.as_ptr(), ptr::null_mut(), ptr::null_mut()) { 0 => match avformat_find_stream_info(ps, ptr::null_mut()) { @@ -151,13 +151,13 @@ pub fn input(path: impl AsRef) -> Result { } } -pub fn input_with_dictionary( - path: impl AsRef, +pub fn input_with_dictionary>( + path_or_url: P, options: Dictionary, ) -> Result { unsafe { let mut ps = ptr::null_mut(); - let path = CString::new(path.as_ref()).unwrap(); + let path = from_os_str(path_or_url); let mut opts = options.disown(); let res = avformat_open_input(&mut ps, path.as_ptr(), ptr::null_mut(), &mut opts); @@ -177,13 +177,13 @@ pub fn input_with_dictionary( } } -pub fn input_with_interrupt( - path: impl AsRef, +pub fn input_with_interrupt>( + path_or_url: P, closure: impl FnMut() -> bool, ) -> Result { unsafe { let mut ps = avformat_alloc_context(); - let path = CString::new(path.as_ref()).unwrap(); + let path = from_os_str(path_or_url); (*ps).interrupt_callback = interrupt::new(Box::new(closure)).interrupt; match avformat_open_input(&mut ps, path.as_ptr(), ptr::null_mut(), ptr::null_mut()) { @@ -200,10 +200,10 @@ pub fn input_with_interrupt( } } -pub fn output(path: impl AsRef) -> Result { +pub fn output>(path_or_url: P) -> Result { unsafe { let mut ps = ptr::null_mut(); - let path = CString::new(path.as_ref()).unwrap(); + let path = from_os_str(path_or_url); match avformat_alloc_output_context2(&mut ps, ptr::null_mut(), ptr::null(), path.as_ptr()) { 0 => match avio_open(&mut (*ps).pb, path.as_ptr(), AVIO_FLAG_WRITE) { @@ -216,10 +216,10 @@ pub fn output(path: impl AsRef) -> Result { } } -pub fn output_with(path: impl AsRef, options: Dictionary) -> Result { +pub fn output_with>(path_or_url: P, options: Dictionary) -> Result { unsafe { let mut ps = ptr::null_mut(); - let path = CString::new(path.as_ref()).unwrap(); + let path = from_os_str(path_or_url); let mut opts = options.disown(); match avformat_alloc_output_context2(&mut ps, ptr::null_mut(), ptr::null(), path.as_ptr()) { @@ -245,10 +245,10 @@ pub fn output_with(path: impl AsRef, options: Dictionary) -> Result, format: &str) -> Result { +pub fn output_as>(path_or_url: P, format: &str) -> Result { unsafe { let mut ps = ptr::null_mut(); - let path = CString::new(path.as_ref()).unwrap(); + let path = from_os_str(path_or_url); let format = CString::new(format).unwrap(); match avformat_alloc_output_context2(&mut ps, ptr::null_mut(), format.as_ptr(), path.as_ptr()) { @@ -262,14 +262,14 @@ pub fn output_as(path: impl AsRef, format: &str) -> Result, +pub fn output_as_with>( + path_or_url: P, format: &str, options: Dictionary, ) -> Result { unsafe { let mut ps = ptr::null_mut(); - let path = CString::new(path.as_ref()).unwrap(); + let path = from_os_str(path_or_url); let format = CString::new(format).unwrap(); let mut opts = options.disown(); @@ -295,3 +295,16 @@ pub fn output_as_with( } } } + + +#[cfg(unix)] +fn from_os_str(path_or_url: impl AsRef) -> CString { + use std::os::unix::ffi::OsStrExt; + + CString::new(path_or_url.as_ref().as_bytes()).unwrap() +} + +#[cfg(not(unix))] +fn from_os_str(path_or_url: impl AsRef) -> CString { + CString::new(path_or_url.as_ref().to_str().unwrap()).unwrap() +}