Search code examples
rustgstreamerglibgstreamer-1.0

How do I properly subclass with glib using Rust?


Im following this tutorial, because I want to create a gstreamer plugin. I think the tutorial I a bit outdated, but the Cargo.toml files don't specify which versions they're using for the tutorial. I'm having some problems with glib, and I was hoping someone with more experience with glib could help me.

The tutorial prompts me to write the following lines. I added the errors I'm getting, and the things I've already tried in the comments:

mod rgb2gray/imp

#[derive(Default)]
pub struct Rgb2Gray {}

impl Rgb2Gray {}

// This macro doesn't seem to exist 
// I tried putting "glib::glib_object_subclass!();" inside the struct (???)
// after that I get: "the trait `glib::subclass::object::ObjectImpl` is not implemented for `rgb2gray::imp::Rgb2Gray`"
#[glib::object_subclass] 
impl ObjectSubclass for Rgb2Gray {
    const NAME: &'static str = "RsRgb2Gray";
    type Type = super::Rgb2Gray; // TYPE is not a variable in ObjectSubclass
    type ParentType = gst_base::BaseTransform;
}

mod rgb2gray

mod imp;

// Again, this macro doesn't seem to exist
// I tried glib::glib_wrapper
glib::wrapper! {
    // The following line doesn't accept ObjectSubclass, it does accept Boxed, or Object, or Shared.
    //  I tried Object, but then it wants more information in the angle brackets... And I'm not sure what kind of information.
    pub struct Rgb2Gray(ObjectSubclass<imp::Rgb2Gray>) @extends gst_base::BaseTransform, gst::Element, gst::Object;
}

// more code...

Solution

  • Did you compare it to the code at https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/tree/master/tutorial ? That one builds fine, so I assume that somewhere the code and the markdown are not in sync.

    Judging from the error you wrote on above, you are using the latest release of glib and not the git version. See the Cargo.toml in there for how to select the git version.

    If you're using the latest release you need the code/markdown from the 0.6 branch here: https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/tree/0.6/tutorial