fix: support Path type in input/open functions

This commit is contained in:
Kornel
2021-03-11 13:49:34 +01:00
committed by GitHub
parent 86ec6dec44
commit 3d08dae9be
+36 -23
View File
@@ -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<str>, format: &Format) -> Result<Context, Error> {
pub fn open<P: AsRef<OsStr>>(path_or_url: P, format: &Format) -> Result<Context, Error> {
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<str>, format: &Format) -> Result<Context, Error> {
}
}
pub fn open_with(
path: impl AsRef<str>,
pub fn open_with<P: AsRef<OsStr>>(
path_or_url: P,
format: &Format,
options: Dictionary,
) -> Result<Context, Error> {
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<str>) -> Result<context::Input, Error> {
pub fn input<P: AsRef<OsStr>>(path_or_url: P) -> Result<context::Input, Error> {
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<str>) -> Result<context::Input, Error> {
}
}
pub fn input_with_dictionary(
path: impl AsRef<str>,
pub fn input_with_dictionary<P: AsRef<OsStr>>(
path_or_url: P,
options: Dictionary,
) -> Result<context::Input, Error> {
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<str>,
pub fn input_with_interrupt<P: AsRef<OsStr>>(
path_or_url: P,
closure: impl FnMut() -> bool,
) -> Result<context::Input, Error> {
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<str>) -> Result<context::Output, Error> {
pub fn output<P: AsRef<OsStr>>(path_or_url: P) -> Result<context::Output, Error> {
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<str>) -> Result<context::Output, Error> {
}
}
pub fn output_with(path: impl AsRef<str>, options: Dictionary) -> Result<context::Output, Error> {
pub fn output_with<P: AsRef<OsStr>>(path_or_url: P, options: Dictionary) -> Result<context::Output, Error> {
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<str>, options: Dictionary) -> Result<context
}
}
pub fn output_as(path: impl AsRef<str>, format: &str) -> Result<context::Output, Error> {
pub fn output_as<P: AsRef<OsStr>>(path_or_url: P, format: &str) -> Result<context::Output, Error> {
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<str>, format: &str) -> Result<context::Output,
}
}
pub fn output_as_with(
path: impl AsRef<str>,
pub fn output_as_with<P: AsRef<OsStr>>(
path_or_url: P,
format: &str,
options: Dictionary,
) -> Result<context::Output, Error> {
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<OsStr>) -> 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<OsStr>) -> CString {
CString::new(path_or_url.as_ref().to_str().unwrap()).unwrap()
}