Add a Rust struct and store/use it in the C code

This commit is contained in:
Sebastian Dröge 2016-05-13 17:35:09 +03:00
parent 9bb2d32c75
commit 6a3a42717f
3 changed files with 49 additions and 5 deletions

View file

@ -1,5 +1,10 @@
#include "rsfilesrc.h"
/* Declarations for Rust code */
extern void * filesrc_new (void);
extern void filesrc_drop (void * filesrc);
extern int filesrc_fill (void * filesrc);
GST_DEBUG_CATEGORY_STATIC (gst_rsfile_src_debug);
#define GST_CAT_DEFAULT gst_rsfile_src_debug
@ -74,6 +79,8 @@ static void
gst_rsfile_src_init (GstRsfileSrc * src)
{
gst_base_src_set_blocksize (GST_BASE_SRC (src), 4096);
src->instance = filesrc_new ();
}
static void
@ -81,6 +88,7 @@ gst_rsfile_src_finalize (GObject * object)
{
GstRsfileSrc *src = GST_RSFILE_SRC (object);
filesrc_drop (src->instance);
g_free (src->location);
G_OBJECT_CLASS (parent_class)->finalize (object);
@ -120,15 +128,14 @@ gst_rsfile_src_get_property (GObject * object, guint prop_id, GValue * value,
}
}
extern void foo(void);
static GstFlowReturn
gst_rsfile_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
GstBuffer * buf)
{
GstRsfileSrc *src = GST_RSFILE_SRC (basesrc);
foo();
g_print ("foo %p\n", src->instance);
filesrc_fill (src->instance);
gst_buffer_memset (buf, 0, 0, gst_buffer_get_size (buf));

View file

@ -25,6 +25,7 @@ struct _GstRsfileSrc {
GstBaseSrc element;
gchar *location;
gpointer instance;
};
struct _GstRsfileSrcClass {

View file

@ -1,4 +1,40 @@
use std::mem;
#[no_mangle]
pub extern fn foo() {
print!("foo\n");
pub extern "C" fn filesrc_new() -> *mut FileSrc {
let mut instance = Box::new(FileSrc::new());
return &mut *instance;
}
#[no_mangle]
pub extern "C" fn filesrc_drop(ptr: *mut FileSrc) {
let filesrc: &mut FileSrc = unsafe { &mut *ptr };
println!("drop");
drop(filesrc);
}
#[no_mangle]
pub extern "C" fn filesrc_fill(ptr: *mut FileSrc) {
let filesrc: &mut FileSrc = unsafe { &mut *ptr };
println!("fill");
filesrc.location = Some(String::from("bla"));
}
#[derive(Debug)]
pub struct FileSrc {
location: Option<String>,
}
impl FileSrc {
fn new() -> FileSrc {
FileSrc { location: None }
}
}
impl Drop for FileSrc {
fn drop(&mut self) {
println!("drop");
}
}