From 8f3d49be315f7631284205f84cc20729672ff557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Fri, 13 May 2016 18:53:35 +0300 Subject: [PATCH] Move everything into the FileSrc implementation --- src/rsfilesrc.rs | 80 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/src/rsfilesrc.rs b/src/rsfilesrc.rs index 68ffde19..a14cae9e 100644 --- a/src/rsfilesrc.rs +++ b/src/rsfilesrc.rs @@ -1,14 +1,9 @@ use libc::{c_char}; use std::ffi::{CStr, CString}; -use std::{ptr, mem}; +use std::ptr; use std::u64; use std::slice; -#[derive(Debug)] -pub struct FileSrc { - location: Option, -} - #[repr(C)] pub enum GstFlowReturn { Ok = 0, @@ -25,10 +20,59 @@ pub enum GBoolean { 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, +} + impl FileSrc { fn new() -> FileSrc { FileSrc { location: None } } + + fn set_location(&mut self, location: &Option) { + self.location = location.clone(); + } + + fn get_location(&self) -> &Option { + &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 { @@ -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 }; if location_ptr.is_null() { - filesrc.location = None; + filesrc.set_location(&None) } else { 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 { let filesrc: &mut FileSrc = unsafe { &mut *ptr }; - match filesrc.location { + match *filesrc.get_location() { Some(ref location) => CString::new(location.clone().into_bytes()).unwrap().into_raw(), None => @@ -78,42 +122,34 @@ pub extern "C" fn filesrc_fill(ptr: *mut FileSrc, data_ptr: *mut u8, data_len: u println!("{:?}", filesrc); let mut data = unsafe { slice::from_raw_parts_mut(data_ptr, data_len) }; - - for i in 0..data.len() - 1 { - data[i] = 1; - } - - return GstFlowReturn::Ok; + return filesrc.fill(data); } #[no_mangle] pub extern "C" fn filesrc_get_size(ptr: *mut FileSrc) -> u64 { let filesrc: &mut FileSrc = unsafe { &mut *ptr }; - return u64::MAX; + return filesrc.get_size(); } #[no_mangle] pub extern "C" fn filesrc_start(ptr: *mut FileSrc) -> GBoolean { let filesrc: &mut FileSrc = unsafe { &mut *ptr }; - match filesrc.location { - None => GBoolean::False, - Some(_) => GBoolean::True - } + GBoolean::from_bool(filesrc.start()) } #[no_mangle] pub extern "C" fn filesrc_stop(ptr: *mut FileSrc) -> GBoolean { let filesrc: &mut FileSrc = unsafe { &mut *ptr }; - return GBoolean::True; + GBoolean::from_bool(filesrc.stop()) } #[no_mangle] pub extern "C" fn filesrc_is_seekable(ptr: *mut FileSrc) -> GBoolean { let filesrc: &mut FileSrc = unsafe { &mut *ptr }; - return GBoolean::True; + GBoolean::from_bool(filesrc.is_seekable()) }