Move everything into the FileSrc implementation

This commit is contained in:
Sebastian Dröge 2016-05-13 18:53:35 +03:00
parent 76a5226f79
commit 8f3d49be31

View file

@ -1,14 +1,9 @@
use libc::{c_char}; use libc::{c_char};
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
use std::{ptr, mem}; use std::ptr;
use std::u64; use std::u64;
use std::slice; use std::slice;
#[derive(Debug)]
pub struct FileSrc {
location: Option<String>,
}
#[repr(C)] #[repr(C)]
pub enum GstFlowReturn { pub enum GstFlowReturn {
Ok = 0, Ok = 0,
@ -25,10 +20,59 @@ pub enum GBoolean {
True = 1, True = 1,
} }
impl GBoolean {
fn from_bool(v: bool) -> GBoolean {
match v {
true => GBoolean::True,
false => GBoolean::False,
}
}
}
#[derive(Debug)]
pub struct FileSrc {
location: Option<String>,
}
impl FileSrc { impl FileSrc {
fn new() -> FileSrc { fn new() -> FileSrc {
FileSrc { location: None } FileSrc { location: None }
} }
fn set_location(&mut self, location: &Option<String>) {
self.location = location.clone();
}
fn get_location(&self) -> &Option<String> {
&self.location
}
fn is_seekable(&self) -> bool {
true
}
fn get_size(&self) -> u64 {
u64::MAX
}
fn start(&mut self) -> bool {
match self.location {
None => false,
Some(_) => true
}
}
fn stop(&mut self) -> bool {
true
}
fn fill(&mut self, data: &mut [u8]) -> GstFlowReturn {
for i in 0..data.len() - 1 {
data[i] = 1;
}
return GstFlowReturn::Ok;
}
} }
impl Drop for FileSrc { impl Drop for FileSrc {
@ -53,10 +97,10 @@ pub extern "C" fn filesrc_set_location(ptr: *mut FileSrc, location_ptr: *const c
let filesrc: &mut FileSrc = unsafe { &mut *ptr }; let filesrc: &mut FileSrc = unsafe { &mut *ptr };
if location_ptr.is_null() { if location_ptr.is_null() {
filesrc.location = None; filesrc.set_location(&None)
} else { } else {
let location = unsafe { CStr::from_ptr(location_ptr) }; let location = unsafe { CStr::from_ptr(location_ptr) };
filesrc.location = Some(String::from(location.to_str().unwrap())); filesrc.set_location(&Some(String::from(location.to_str().unwrap())));
} }
} }
@ -64,7 +108,7 @@ pub extern "C" fn filesrc_set_location(ptr: *mut FileSrc, location_ptr: *const c
pub extern "C" fn filesrc_get_location(ptr: *mut FileSrc) -> *mut c_char { pub extern "C" fn filesrc_get_location(ptr: *mut FileSrc) -> *mut c_char {
let filesrc: &mut FileSrc = unsafe { &mut *ptr }; let filesrc: &mut FileSrc = unsafe { &mut *ptr };
match filesrc.location { match *filesrc.get_location() {
Some(ref location) => Some(ref location) =>
CString::new(location.clone().into_bytes()).unwrap().into_raw(), CString::new(location.clone().into_bytes()).unwrap().into_raw(),
None => None =>
@ -78,42 +122,34 @@ pub extern "C" fn filesrc_fill(ptr: *mut FileSrc, data_ptr: *mut u8, data_len: u
println!("{:?}", filesrc); println!("{:?}", filesrc);
let mut data = unsafe { slice::from_raw_parts_mut(data_ptr, data_len) }; let mut data = unsafe { slice::from_raw_parts_mut(data_ptr, data_len) };
return filesrc.fill(data);
for i in 0..data.len() - 1 {
data[i] = 1;
}
return GstFlowReturn::Ok;
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn filesrc_get_size(ptr: *mut FileSrc) -> u64 { pub extern "C" fn filesrc_get_size(ptr: *mut FileSrc) -> u64 {
let filesrc: &mut FileSrc = unsafe { &mut *ptr }; let filesrc: &mut FileSrc = unsafe { &mut *ptr };
return u64::MAX; return filesrc.get_size();
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn filesrc_start(ptr: *mut FileSrc) -> GBoolean { pub extern "C" fn filesrc_start(ptr: *mut FileSrc) -> GBoolean {
let filesrc: &mut FileSrc = unsafe { &mut *ptr }; let filesrc: &mut FileSrc = unsafe { &mut *ptr };
match filesrc.location { GBoolean::from_bool(filesrc.start())
None => GBoolean::False,
Some(_) => GBoolean::True
}
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn filesrc_stop(ptr: *mut FileSrc) -> GBoolean { pub extern "C" fn filesrc_stop(ptr: *mut FileSrc) -> GBoolean {
let filesrc: &mut FileSrc = unsafe { &mut *ptr }; let filesrc: &mut FileSrc = unsafe { &mut *ptr };
return GBoolean::True; GBoolean::from_bool(filesrc.stop())
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn filesrc_is_seekable(ptr: *mut FileSrc) -> GBoolean { pub extern "C" fn filesrc_is_seekable(ptr: *mut FileSrc) -> GBoolean {
let filesrc: &mut FileSrc = unsafe { &mut *ptr }; let filesrc: &mut FileSrc = unsafe { &mut *ptr };
return GBoolean::True; GBoolean::from_bool(filesrc.is_seekable())
} }