1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use ffi::{GstGLBaseSrc, GstGLMemory};

use crate::prelude::*;

use glib::translate::*;

use gst::{result_from_gboolean, LoggableError, CAT_RUST};
use gst_base::subclass::prelude::*;

use crate::{GLBaseSrc, GLMemory, GLAPI};

pub trait GLBaseSrcImpl: GLBaseSrcImplExt + PushSrcImpl {
    const SUPPORTED_GL_API: GLAPI;

    fn gl_start(&self, element: &Self::Type) -> Result<(), LoggableError> {
        self.parent_gl_start(element)
    }

    fn gl_stop(&self, element: &Self::Type) {
        self.parent_gl_stop(element)
    }

    fn fill_gl_memory(&self, element: &Self::Type, memory: &GLMemory) -> Result<(), LoggableError> {
        self.parent_fill_gl_memory(element, memory)
    }
}

pub trait GLBaseSrcImplExt: ObjectSubclass {
    fn parent_gl_start(&self, element: &Self::Type) -> Result<(), LoggableError>;

    fn parent_gl_stop(&self, element: &Self::Type);

    fn parent_fill_gl_memory(
        &self,
        element: &Self::Type,
        memory: &GLMemory,
    ) -> Result<(), LoggableError>;
}

impl<T: GLBaseSrcImpl> GLBaseSrcImplExt for T {
    fn parent_gl_start(&self, element: &Self::Type) -> Result<(), LoggableError> {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstGLBaseSrcClass;

            (*parent_class)
                .gl_start
                .map(|f| {
                    result_from_gboolean!(
                        f(element.unsafe_cast_ref::<GLBaseSrc>().to_glib_none().0),
                        CAT_RUST,
                        "Parent function `gl_start` failed",
                    )
                })
                .unwrap_or(Ok(()))
        }
    }

    fn parent_gl_stop(&self, element: &Self::Type) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstGLBaseSrcClass;

            if let Some(f) = (*parent_class).gl_stop {
                f(element.unsafe_cast_ref::<GLBaseSrc>().to_glib_none().0)
            }
        }
    }

    fn parent_fill_gl_memory(
        &self,
        element: &Self::Type,
        memory: &GLMemory,
    ) -> Result<(), LoggableError> {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstGLBaseSrcClass;

            (*parent_class)
                .fill_gl_memory
                .map(|f| {
                    result_from_gboolean!(
                        f(
                            element.unsafe_cast_ref::<GLBaseSrc>().to_glib_none().0,
                            mut_override(memory.to_glib_none().0),
                        ),
                        CAT_RUST,
                        "Parent function `fill_gl_memory` failed",
                    )
                })
                .unwrap_or(Ok(()))
        }
    }
}

unsafe impl<T: GLBaseSrcImpl> IsSubclassable<T> for GLBaseSrc {
    fn class_init(klass: &mut glib::Class<Self>) {
        <gst_base::PushSrc as IsSubclassable<T>>::class_init(klass);
        let klass = klass.as_mut();
        klass.supported_gl_api = T::SUPPORTED_GL_API.into_glib();
        klass.gl_start = Some(gl_start::<T>);
        klass.gl_stop = Some(gl_stop::<T>);
        klass.fill_gl_memory = Some(fill_gl_memory::<T>);
    }

    fn instance_init(instance: &mut glib::subclass::InitializingObject<T>) {
        <gst_base::PushSrc as IsSubclassable<T>>::instance_init(instance)
    }
}

unsafe extern "C" fn gl_start<T: GLBaseSrcImpl>(ptr: *mut GstGLBaseSrc) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.impl_();
    let wrap: Borrowed<GLBaseSrc> = from_glib_borrow(ptr);

    gst::panic_to_error!(&wrap, &imp.panicked(), false, {
        match imp.gl_start(wrap.unsafe_cast_ref()) {
            Ok(()) => true,
            Err(err) => {
                err.log_with_object(&*wrap);
                false
            }
        }
    })
    .into_glib()
}

unsafe extern "C" fn gl_stop<T: GLBaseSrcImpl>(ptr: *mut GstGLBaseSrc) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.impl_();
    let wrap: Borrowed<GLBaseSrc> = from_glib_borrow(ptr);

    gst::panic_to_error!(&wrap, &imp.panicked(), (), {
        imp.gl_stop(wrap.unsafe_cast_ref())
    })
}

unsafe extern "C" fn fill_gl_memory<T: GLBaseSrcImpl>(
    ptr: *mut GstGLBaseSrc,
    memory: *mut GstGLMemory,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.impl_();
    let wrap: Borrowed<GLBaseSrc> = from_glib_borrow(ptr);

    gst::panic_to_error!(&wrap, &imp.panicked(), false, {
        match imp.fill_gl_memory(wrap.unsafe_cast_ref(), &from_glib_borrow(memory)) {
            Ok(()) => true,
            Err(err) => {
                err.log_with_object(&*wrap);
                false
            }
        }
    })
    .into_glib()
}