gstreamer-rs/gstreamer/src/typefind.rs

313 lines
8.6 KiB
Rust
Raw Normal View History

2020-12-15 10:53:31 +00:00
// Take a look at the license at the top of the repository in the LICENSE file.
use crate::Caps;
use crate::Plugin;
use crate::Rank;
use crate::TypeFindFactory;
use crate::TypeFindProbability;
2018-04-01 08:30:03 +00:00
use glib::translate::*;
use std::ptr;
use std::slice;
#[repr(transparent)]
#[derive(Debug)]
#[doc(alias = "GstTypeFind")]
pub struct TypeFind(ffi::GstTypeFind);
pub trait TypeFindImpl {
fn peek(&mut self, offset: i64, size: u32) -> Option<&[u8]>;
fn suggest(&mut self, probability: TypeFindProbability, caps: &Caps);
#[doc(alias = "get_length")]
2021-04-11 19:39:50 +00:00
fn length(&self) -> Option<u64> {
None
}
}
impl TypeFind {
#[doc(alias = "gst_type_find_register")]
pub fn register<F>(
plugin: Option<&Plugin>,
name: &str,
rank: Rank,
extensions: Option<&str>,
possible_caps: Option<&Caps>,
func: F,
) -> Result<(), glib::error::BoolError>
where
F: Fn(&mut TypeFind) + Send + Sync + 'static,
{
2020-03-22 14:18:47 +00:00
assert_initialized_main_thread!();
unsafe {
let func: Box<F> = Box::new(func);
let func = Box::into_raw(func);
let res = ffi::gst_type_find_register(
plugin.to_glib_none().0,
name.to_glib_none().0,
rank.into_glib() as u32,
Some(type_find_trampoline::<F>),
extensions.to_glib_none().0,
possible_caps.to_glib_none().0,
func as *mut _,
Some(type_find_closure_drop::<F>),
);
glib::result_from_gboolean!(res, "Failed to register typefind factory")
}
}
#[doc(alias = "gst_type_find_peek")]
pub fn peek(&mut self, offset: i64, size: u32) -> Option<&[u8]> {
unsafe {
let data = ffi::gst_type_find_peek(&mut self.0, offset, size);
if data.is_null() {
None
} else {
Some(slice::from_raw_parts(data, size as usize))
}
}
}
#[doc(alias = "gst_type_find_suggest")]
pub fn suggest(&mut self, probability: TypeFindProbability, caps: &Caps) {
unsafe {
ffi::gst_type_find_suggest(
&mut self.0,
probability.into_glib() as u32,
caps.to_glib_none().0,
);
}
}
#[doc(alias = "get_length")]
#[doc(alias = "gst_type_find_get_length")]
2021-04-11 19:39:50 +00:00
pub fn length(&mut self) -> Option<u64> {
unsafe {
let len = ffi::gst_type_find_get_length(&mut self.0);
if len == 0 {
None
} else {
Some(len)
}
}
}
}
impl TypeFindFactory {
#[doc(alias = "gst_type_find_factory_call_function")]
pub fn call_function(&self, find: &mut dyn TypeFindImpl) {
unsafe {
let find_ptr = &find as *const &mut dyn TypeFindImpl as glib::ffi::gpointer;
let mut find = ffi::GstTypeFind {
peek: Some(type_find_peek),
suggest: Some(type_find_suggest),
data: find_ptr,
get_length: Some(type_find_get_length),
_gst_reserved: [ptr::null_mut(); 4],
};
ffi::gst_type_find_factory_call_function(self.to_glib_none().0, &mut find)
}
}
}
unsafe extern "C" fn type_find_trampoline<F: Fn(&mut TypeFind) + Send + Sync + 'static>(
find: *mut ffi::GstTypeFind,
user_data: glib::ffi::gpointer,
) {
2019-02-21 17:30:36 +00:00
let func: &F = &*(user_data as *const F);
func(&mut *(find as *mut TypeFind));
}
unsafe extern "C" fn type_find_closure_drop<F: Fn(&mut TypeFind) + Send + Sync + 'static>(
data: glib::ffi::gpointer,
) {
Box::<F>::from_raw(data as *mut _);
}
unsafe extern "C" fn type_find_peek(
data: glib::ffi::gpointer,
offset: i64,
size: u32,
) -> *const u8 {
let find: &mut &mut dyn TypeFindImpl = &mut *(data as *mut &mut dyn TypeFindImpl);
match find.peek(offset, size) {
None => ptr::null(),
Some(data) => data.as_ptr(),
}
}
unsafe extern "C" fn type_find_suggest(
data: glib::ffi::gpointer,
probability: u32,
caps: *mut ffi::GstCaps,
) {
let find: &mut &mut dyn TypeFindImpl = &mut *(data as *mut &mut dyn TypeFindImpl);
find.suggest(from_glib(probability as i32), &from_glib_borrow(caps));
}
unsafe extern "C" fn type_find_get_length(data: glib::ffi::gpointer) -> u64 {
use std::u64;
let find: &mut &mut dyn TypeFindImpl = &mut *(data as *mut &mut dyn TypeFindImpl);
2021-04-11 19:39:50 +00:00
find.length().unwrap_or(u64::MAX)
}
#[derive(Debug)]
pub struct SliceTypeFind<T: AsRef<[u8]>> {
pub probability: Option<TypeFindProbability>,
pub caps: Option<Caps>,
data: T,
}
impl<T: AsRef<[u8]>> SliceTypeFind<T> {
pub fn new(data: T) -> SliceTypeFind<T> {
2020-03-22 14:18:47 +00:00
assert_initialized_main_thread!();
SliceTypeFind {
probability: None,
caps: None,
data,
}
}
pub fn run(&mut self) {
2021-04-20 10:24:17 +00:00
let factories = TypeFindFactory::list();
for factory in factories {
factory.call_function(self);
if let Some(prob) = self.probability {
if prob >= TypeFindProbability::Maximum {
break;
}
}
}
}
pub fn type_find(data: T) -> (TypeFindProbability, Option<Caps>) {
2020-03-22 14:18:47 +00:00
assert_initialized_main_thread!();
let mut t = SliceTypeFind {
probability: None,
caps: None,
data,
};
t.run();
(t.probability.unwrap_or(TypeFindProbability::None), t.caps)
}
}
impl<T: AsRef<[u8]>> TypeFindImpl for SliceTypeFind<T> {
fn peek(&mut self, offset: i64, size: u32) -> Option<&[u8]> {
let data = self.data.as_ref();
let len = data.len();
let offset = if offset >= 0 {
offset as usize
} else {
if len < offset.abs() as usize {
return None;
}
len - (offset.abs() as usize)
};
let size = size as usize;
if offset + size <= len {
Some(&data[offset..(offset + size)])
} else {
None
}
}
fn suggest(&mut self, probability: TypeFindProbability, caps: &Caps) {
match self.probability {
None => {
self.probability = Some(probability);
self.caps = Some(caps.clone());
}
Some(old_probability) if old_probability < probability => {
self.probability = Some(probability);
self.caps = Some(caps.clone());
}
_ => (),
}
}
2021-04-11 19:39:50 +00:00
fn length(&self) -> Option<u64> {
Some(self.data.as_ref().len() as u64)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_typefind_call_function() {
crate::init().unwrap();
2021-04-20 10:24:17 +00:00
let xml_factory = TypeFindFactory::list()
.iter()
.cloned()
.find(|f| {
2021-04-11 19:39:50 +00:00
f.caps()
2018-07-27 10:36:40 +00:00
.map(|c| {
2021-04-20 10:24:17 +00:00
c.structure(0)
2021-04-11 19:39:50 +00:00
.map(|s| s.name() == "application/xml")
2018-07-27 10:36:40 +00:00
.unwrap_or(false)
2018-10-08 12:02:23 +00:00
})
.unwrap_or(false)
})
.unwrap();
let data = b"<?xml version=\"1.0\"?><test>test</test>";
let data = &data[..];
let mut typefind = SliceTypeFind::new(&data);
xml_factory.call_function(&mut typefind);
assert_eq!(
typefind.caps,
Some(Caps::builder("application/xml").build())
);
assert_eq!(typefind.probability, Some(TypeFindProbability::Minimum));
}
#[test]
fn test_typefind_register() {
crate::init().unwrap();
TypeFind::register(
None,
"test_typefind",
crate::Rank::Primary,
None,
Some(&Caps::builder("test/test").build()),
|typefind| {
2021-04-11 19:39:50 +00:00
assert_eq!(typefind.length(), Some(8));
let mut found = false;
if let Some(data) = typefind.peek(0, 8) {
if data == b"abcdefgh" {
found = true;
}
}
if found {
typefind.suggest(
TypeFindProbability::Likely,
&Caps::builder("test/test").build(),
);
}
},
)
.unwrap();
let data = b"abcdefgh";
let data = &data[..];
let (probability, caps) = SliceTypeFind::type_find(&data);
assert_eq!(caps, Some(Caps::builder("test/test").build()));
assert_eq!(probability, TypeFindProbability::Likely);
}
}