2022-03-21 17:59:25 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use std::{mem, ptr};
|
2022-03-21 17:59:25 +00:00
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use glib::translate::*;
|
|
|
|
use gst_base::{prelude::*, subclass::prelude::*};
|
2022-03-21 17:59:25 +00:00
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use crate::{subclass::AggregateFramesToken, VideoAggregator, VideoAggregatorPad};
|
2022-03-21 17:59:25 +00:00
|
|
|
|
|
|
|
pub trait VideoAggregatorPadImpl: VideoAggregatorPadImplExt + AggregatorPadImpl {
|
2022-10-08 18:50:06 +00:00
|
|
|
fn update_conversion_info(&self) {
|
|
|
|
self.parent_update_conversion_info()
|
2022-03-21 17:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn prepare_frame(
|
|
|
|
&self,
|
|
|
|
aggregator: &crate::VideoAggregator,
|
|
|
|
token: &AggregateFramesToken,
|
|
|
|
buffer: &gst::Buffer,
|
|
|
|
) -> Option<crate::VideoFrame<crate::video_frame::Readable>> {
|
2022-10-08 18:50:06 +00:00
|
|
|
self.parent_prepare_frame(aggregator, token, buffer)
|
2022-03-21 17:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn clean_frame(
|
|
|
|
&self,
|
|
|
|
aggregator: &crate::VideoAggregator,
|
|
|
|
token: &AggregateFramesToken,
|
|
|
|
frame: Option<crate::VideoFrame<crate::video_frame::Readable>>,
|
|
|
|
) {
|
2022-10-08 18:50:06 +00:00
|
|
|
self.parent_clean_frame(aggregator, token, frame)
|
2022-03-21 17:59:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait VideoAggregatorPadImplExt: ObjectSubclass {
|
2022-10-08 18:50:06 +00:00
|
|
|
fn parent_update_conversion_info(&self);
|
2022-03-21 17:59:25 +00:00
|
|
|
|
|
|
|
fn parent_prepare_frame(
|
|
|
|
&self,
|
|
|
|
aggregator: &crate::VideoAggregator,
|
|
|
|
token: &AggregateFramesToken,
|
|
|
|
buffer: &gst::Buffer,
|
|
|
|
) -> Option<crate::VideoFrame<crate::video_frame::Readable>>;
|
|
|
|
|
|
|
|
fn parent_clean_frame(
|
|
|
|
&self,
|
|
|
|
aggregator: &crate::VideoAggregator,
|
|
|
|
token: &AggregateFramesToken,
|
|
|
|
frame: Option<crate::VideoFrame<crate::video_frame::Readable>>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: VideoAggregatorPadImpl> VideoAggregatorPadImplExt for T {
|
2022-10-08 18:50:06 +00:00
|
|
|
fn parent_update_conversion_info(&self) {
|
2022-03-21 17:59:25 +00:00
|
|
|
unsafe {
|
|
|
|
let data = Self::type_data();
|
|
|
|
let parent_class = data.as_ref().parent_class() as *mut ffi::GstVideoAggregatorPadClass;
|
|
|
|
if let Some(f) = (*parent_class).update_conversion_info {
|
2022-10-08 18:50:06 +00:00
|
|
|
f(self
|
2022-10-23 19:59:23 +00:00
|
|
|
.obj()
|
2022-10-08 18:50:06 +00:00
|
|
|
.unsafe_cast_ref::<VideoAggregatorPad>()
|
|
|
|
.to_glib_none()
|
|
|
|
.0);
|
2022-03-21 17:59:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parent_prepare_frame(
|
|
|
|
&self,
|
|
|
|
aggregator: &crate::VideoAggregator,
|
|
|
|
token: &AggregateFramesToken,
|
|
|
|
buffer: &gst::Buffer,
|
|
|
|
) -> Option<crate::VideoFrame<crate::video_frame::Readable>> {
|
|
|
|
assert_eq!(
|
|
|
|
aggregator.as_ptr() as *mut ffi::GstVideoAggregator,
|
|
|
|
token.0.as_ptr() as *mut ffi::GstVideoAggregator
|
|
|
|
);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let data = Self::type_data();
|
|
|
|
let parent_class = data.as_ref().parent_class() as *mut ffi::GstVideoAggregatorPadClass;
|
|
|
|
if let Some(f) = (*parent_class).prepare_frame {
|
|
|
|
let mut prepared_frame = mem::MaybeUninit::zeroed();
|
|
|
|
|
|
|
|
f(
|
2022-10-23 19:59:23 +00:00
|
|
|
self.obj()
|
2022-10-08 18:50:06 +00:00
|
|
|
.unsafe_cast_ref::<VideoAggregatorPad>()
|
|
|
|
.to_glib_none()
|
|
|
|
.0,
|
2022-03-21 17:59:25 +00:00
|
|
|
aggregator.to_glib_none().0,
|
|
|
|
buffer.as_mut_ptr(),
|
|
|
|
prepared_frame.as_mut_ptr(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let prepared_frame = prepared_frame.assume_init();
|
|
|
|
if prepared_frame.buffer.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(crate::VideoFrame::from_glib_full(prepared_frame))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parent_clean_frame(
|
|
|
|
&self,
|
|
|
|
aggregator: &crate::VideoAggregator,
|
|
|
|
token: &AggregateFramesToken,
|
|
|
|
frame: Option<crate::VideoFrame<crate::video_frame::Readable>>,
|
|
|
|
) {
|
|
|
|
assert_eq!(
|
|
|
|
aggregator.as_ptr() as *mut ffi::GstVideoAggregator,
|
|
|
|
token.0.as_ptr() as *mut ffi::GstVideoAggregator
|
|
|
|
);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let data = Self::type_data();
|
|
|
|
let parent_class = data.as_ref().parent_class() as *mut ffi::GstVideoAggregatorPadClass;
|
|
|
|
if let Some(f) = (*parent_class).clean_frame {
|
|
|
|
let mut prepared_frame = if let Some(frame) = frame {
|
|
|
|
frame.into_raw()
|
|
|
|
} else {
|
|
|
|
mem::zeroed()
|
|
|
|
};
|
|
|
|
|
|
|
|
f(
|
2022-10-23 19:59:23 +00:00
|
|
|
self.obj()
|
2022-10-08 18:50:06 +00:00
|
|
|
.unsafe_cast_ref::<VideoAggregatorPad>()
|
|
|
|
.to_glib_none()
|
|
|
|
.0,
|
2022-03-21 17:59:25 +00:00
|
|
|
aggregator.to_glib_none().0,
|
|
|
|
&mut prepared_frame,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T: VideoAggregatorPadImpl> IsSubclassable<T> for VideoAggregatorPad {
|
|
|
|
fn class_init(klass: &mut glib::Class<Self>) {
|
|
|
|
Self::parent_class_init::<T>(klass);
|
|
|
|
|
|
|
|
let klass = klass.as_mut();
|
|
|
|
klass.update_conversion_info = Some(video_aggregator_pad_update_conversion_info::<T>);
|
|
|
|
klass.prepare_frame = Some(video_aggregator_pad_prepare_frame::<T>);
|
|
|
|
klass.clean_frame = Some(video_aggregator_pad_clean_frame::<T>);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn video_aggregator_pad_update_conversion_info<T: VideoAggregatorPadImpl>(
|
|
|
|
ptr: *mut ffi::GstVideoAggregatorPad,
|
|
|
|
) {
|
|
|
|
let instance = &*(ptr as *mut T::Instance);
|
|
|
|
let imp = instance.imp();
|
|
|
|
|
2022-10-08 18:50:06 +00:00
|
|
|
imp.update_conversion_info();
|
2022-03-21 17:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn video_aggregator_pad_prepare_frame<T: VideoAggregatorPadImpl>(
|
|
|
|
ptr: *mut ffi::GstVideoAggregatorPad,
|
|
|
|
aggregator: *mut ffi::GstVideoAggregator,
|
|
|
|
buffer: *mut gst::ffi::GstBuffer,
|
|
|
|
prepared_frame: *mut ffi::GstVideoFrame,
|
|
|
|
) -> glib::ffi::gboolean {
|
|
|
|
let instance = &*(ptr as *mut T::Instance);
|
|
|
|
let imp = instance.imp();
|
|
|
|
let aggregator: Borrowed<VideoAggregator> = from_glib_borrow(aggregator);
|
|
|
|
|
2022-11-01 09:10:57 +00:00
|
|
|
let token = AggregateFramesToken(&aggregator);
|
2022-03-21 17:59:25 +00:00
|
|
|
|
2022-10-08 18:50:06 +00:00
|
|
|
match imp.prepare_frame(&aggregator, &token, &from_glib_borrow(buffer)) {
|
2022-03-21 17:59:25 +00:00
|
|
|
Some(frame) => {
|
|
|
|
*prepared_frame = frame.into_raw();
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
ptr::write(prepared_frame, mem::zeroed());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
glib::ffi::GTRUE
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn video_aggregator_pad_clean_frame<T: VideoAggregatorPadImpl>(
|
|
|
|
ptr: *mut ffi::GstVideoAggregatorPad,
|
|
|
|
aggregator: *mut ffi::GstVideoAggregator,
|
|
|
|
prepared_frame: *mut ffi::GstVideoFrame,
|
|
|
|
) {
|
|
|
|
let instance = &*(ptr as *mut T::Instance);
|
|
|
|
let imp = instance.imp();
|
|
|
|
let aggregator: Borrowed<VideoAggregator> = from_glib_borrow(aggregator);
|
|
|
|
|
2022-11-01 09:10:57 +00:00
|
|
|
let token = AggregateFramesToken(&aggregator);
|
2022-03-21 17:59:25 +00:00
|
|
|
|
|
|
|
let frame = if (*prepared_frame).buffer.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let frame = crate::VideoFrame::from_glib_full(*prepared_frame);
|
|
|
|
ptr::write(prepared_frame, mem::zeroed());
|
|
|
|
Some(frame)
|
|
|
|
};
|
|
|
|
|
2022-10-08 18:50:06 +00:00
|
|
|
imp.clean_frame(&aggregator, &token, frame);
|
2022-03-21 17:59:25 +00:00
|
|
|
}
|