2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2019-05-25 08:11:06 +00:00
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use std::{mem, ptr};
|
|
|
|
|
|
|
|
use glib::{prelude::*, translate::*};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
utils::HasStreamLock,
|
|
|
|
video_codec_state::{InNegotiation, Readable, VideoCodecState, VideoCodecStateContext},
|
|
|
|
VideoCodecFrame, VideoEncoder,
|
|
|
|
};
|
2019-05-25 08:11:06 +00:00
|
|
|
|
|
|
|
pub trait VideoEncoderExtManual: 'static {
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_video_encoder_allocate_output_frame")]
|
2019-05-25 08:11:06 +00:00
|
|
|
fn allocate_output_frame(
|
|
|
|
&self,
|
2019-12-18 16:42:40 +00:00
|
|
|
frame: &mut VideoCodecFrame,
|
2019-05-25 08:11:06 +00:00
|
|
|
size: usize,
|
|
|
|
) -> Result<gst::FlowSuccess, gst::FlowError>;
|
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_frame")]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_video_encoder_get_frame")]
|
2021-04-20 10:23:24 +00:00
|
|
|
fn frame(&self, frame_number: i32) -> Option<VideoCodecFrame>;
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_frames")]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_video_encoder_get_frames")]
|
2021-04-11 19:39:50 +00:00
|
|
|
fn frames(&self) -> Vec<VideoCodecFrame>;
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_oldest_frame")]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_video_encoder_get_oldest_frame")]
|
2021-04-11 19:39:50 +00:00
|
|
|
fn oldest_frame(&self) -> Option<VideoCodecFrame>;
|
2019-05-25 08:11:06 +00:00
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_allocator")]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_video_encoder_get_allocator")]
|
2021-04-11 19:39:50 +00:00
|
|
|
fn allocator(&self) -> (Option<gst::Allocator>, gst::AllocationParams);
|
2019-09-13 15:12:49 +00:00
|
|
|
|
2020-11-27 13:37:49 +00:00
|
|
|
#[cfg(any(feature = "v1_18", feature = "dox"))]
|
|
|
|
#[cfg_attr(feature = "dox", doc(cfg(feature = "v1_18")))]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_video_encoder_finish_subframe")]
|
2020-05-03 12:26:36 +00:00
|
|
|
fn finish_subframe(&self, frame: &VideoCodecFrame) -> Result<gst::FlowSuccess, gst::FlowError>;
|
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_latency")]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_video_encoder_get_latency")]
|
2021-04-28 22:29:13 +00:00
|
|
|
fn latency(&self) -> (gst::ClockTime, Option<gst::ClockTime>);
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_video_encoder_set_latency")]
|
2021-04-28 22:29:13 +00:00
|
|
|
fn set_latency(
|
|
|
|
&self,
|
|
|
|
min_latency: gst::ClockTime,
|
|
|
|
max_latency: impl Into<Option<gst::ClockTime>>,
|
|
|
|
);
|
2019-05-25 08:11:06 +00:00
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_output_state")]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_video_encoder_get_output_state")]
|
2021-04-11 19:39:50 +00:00
|
|
|
fn output_state(&self) -> Option<VideoCodecState<'static, Readable>>;
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_video_encoder_set_output_state")]
|
2019-05-25 08:11:06 +00:00
|
|
|
fn set_output_state(
|
|
|
|
&self,
|
|
|
|
caps: gst::Caps,
|
|
|
|
reference: Option<&VideoCodecState<Readable>>,
|
|
|
|
) -> Result<VideoCodecState<InNegotiation>, gst::FlowError>;
|
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_video_encoder_negotiate")]
|
2019-05-25 08:11:06 +00:00
|
|
|
fn negotiate<'a>(
|
|
|
|
&'a self,
|
|
|
|
output_state: VideoCodecState<'a, InNegotiation<'a>>,
|
|
|
|
) -> Result<(), gst::FlowError>;
|
2022-05-03 22:04:08 +00:00
|
|
|
|
2023-01-03 19:06:38 +00:00
|
|
|
#[doc(alias = "gst_video_encoder_set_headers")]
|
|
|
|
fn set_headers(&self, headers: impl IntoIterator<Item = gst::Buffer>);
|
|
|
|
|
2022-05-05 10:24:46 +00:00
|
|
|
fn sink_pad(&self) -> &gst::Pad;
|
2022-05-03 22:04:08 +00:00
|
|
|
|
2022-05-05 10:24:46 +00:00
|
|
|
fn src_pad(&self) -> &gst::Pad;
|
2019-05-25 08:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<O: IsA<VideoEncoder>> VideoEncoderExtManual for O {
|
|
|
|
fn allocate_output_frame(
|
|
|
|
&self,
|
2019-12-18 16:42:40 +00:00
|
|
|
frame: &mut VideoCodecFrame,
|
2019-05-25 08:11:06 +00:00
|
|
|
size: usize,
|
|
|
|
) -> Result<gst::FlowSuccess, gst::FlowError> {
|
2021-04-28 12:34:56 +00:00
|
|
|
unsafe {
|
2021-05-13 12:54:41 +00:00
|
|
|
try_from_glib(ffi::gst_video_encoder_allocate_output_frame(
|
2019-05-25 08:11:06 +00:00
|
|
|
self.as_ref().to_glib_none().0,
|
|
|
|
frame.to_glib_none().0,
|
|
|
|
size,
|
|
|
|
))
|
2021-04-28 12:34:56 +00:00
|
|
|
}
|
2019-05-25 08:11:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 19:39:50 +00:00
|
|
|
fn allocator(&self) -> (Option<gst::Allocator>, gst::AllocationParams) {
|
2019-09-13 15:12:49 +00:00
|
|
|
unsafe {
|
|
|
|
let mut allocator = ptr::null_mut();
|
2023-01-06 12:11:45 +00:00
|
|
|
let mut params = mem::MaybeUninit::uninit();
|
2020-11-22 09:53:17 +00:00
|
|
|
ffi::gst_video_encoder_get_allocator(
|
2019-09-13 15:12:49 +00:00
|
|
|
self.as_ref().to_glib_none().0,
|
|
|
|
&mut allocator,
|
2023-01-06 12:11:45 +00:00
|
|
|
params.as_mut_ptr(),
|
2019-09-13 15:12:49 +00:00
|
|
|
);
|
2023-01-06 12:11:45 +00:00
|
|
|
(from_glib_full(allocator), params.assume_init().into())
|
2019-09-13 15:12:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 13:37:49 +00:00
|
|
|
#[cfg(any(feature = "v1_18", feature = "dox"))]
|
|
|
|
#[cfg_attr(feature = "dox", doc(cfg(feature = "v1_18")))]
|
2020-05-03 12:26:36 +00:00
|
|
|
fn finish_subframe(&self, frame: &VideoCodecFrame) -> Result<gst::FlowSuccess, gst::FlowError> {
|
2021-04-28 12:34:56 +00:00
|
|
|
unsafe {
|
2021-05-13 12:54:41 +00:00
|
|
|
try_from_glib(ffi::gst_video_encoder_finish_subframe(
|
2020-05-03 12:26:36 +00:00
|
|
|
self.as_ref().to_glib_none().0,
|
|
|
|
frame.to_glib_none().0,
|
|
|
|
))
|
2021-04-28 12:34:56 +00:00
|
|
|
}
|
2020-05-03 12:26:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 22:29:13 +00:00
|
|
|
fn latency(&self) -> (gst::ClockTime, Option<gst::ClockTime>) {
|
2020-11-22 09:53:17 +00:00
|
|
|
let mut min_latency = gst::ffi::GST_CLOCK_TIME_NONE;
|
|
|
|
let mut max_latency = gst::ffi::GST_CLOCK_TIME_NONE;
|
2019-05-25 08:11:06 +00:00
|
|
|
|
|
|
|
unsafe {
|
2020-11-22 09:53:17 +00:00
|
|
|
ffi::gst_video_encoder_get_latency(
|
2019-05-25 08:11:06 +00:00
|
|
|
self.as_ref().to_glib_none().0,
|
|
|
|
&mut min_latency,
|
|
|
|
&mut max_latency,
|
|
|
|
);
|
|
|
|
|
2021-04-28 22:29:13 +00:00
|
|
|
(
|
|
|
|
try_from_glib(min_latency).expect("undefined min_latency"),
|
|
|
|
from_glib(max_latency),
|
|
|
|
)
|
2019-05-25 08:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 22:29:13 +00:00
|
|
|
fn set_latency(
|
|
|
|
&self,
|
|
|
|
min_latency: gst::ClockTime,
|
|
|
|
max_latency: impl Into<Option<gst::ClockTime>>,
|
|
|
|
) {
|
2019-05-25 08:11:06 +00:00
|
|
|
unsafe {
|
2020-11-22 09:53:17 +00:00
|
|
|
ffi::gst_video_encoder_set_latency(
|
2019-05-25 08:11:06 +00:00
|
|
|
self.as_ref().to_glib_none().0,
|
2021-04-27 15:15:46 +00:00
|
|
|
min_latency.into_glib(),
|
2021-04-28 22:29:13 +00:00
|
|
|
max_latency.into().into_glib(),
|
2019-05-25 08:11:06 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 10:23:24 +00:00
|
|
|
fn frame(&self, frame_number: i32) -> Option<VideoCodecFrame> {
|
2019-05-25 08:11:06 +00:00
|
|
|
let frame = unsafe {
|
2020-11-22 09:53:17 +00:00
|
|
|
ffi::gst_video_encoder_get_frame(self.as_ref().to_glib_none().0, frame_number)
|
2019-05-25 08:11:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if frame.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
unsafe { Some(VideoCodecFrame::new(frame, self.as_ref())) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 19:39:50 +00:00
|
|
|
fn frames(&self) -> Vec<VideoCodecFrame> {
|
2019-05-25 08:11:06 +00:00
|
|
|
unsafe {
|
2020-11-22 09:53:17 +00:00
|
|
|
let frames = ffi::gst_video_encoder_get_frames(self.as_ref().to_glib_none().0);
|
|
|
|
let mut iter: *const glib::ffi::GList = frames;
|
2019-05-25 08:11:06 +00:00
|
|
|
let mut vec = Vec::new();
|
|
|
|
|
|
|
|
while !iter.is_null() {
|
|
|
|
let frame_ptr = Ptr::from((*iter).data);
|
|
|
|
/* transfer ownership of the frame */
|
|
|
|
let frame = VideoCodecFrame::new(frame_ptr, self.as_ref());
|
|
|
|
vec.push(frame);
|
|
|
|
iter = (*iter).next;
|
|
|
|
}
|
|
|
|
|
2020-11-22 09:53:17 +00:00
|
|
|
glib::ffi::g_list_free(frames);
|
2019-05-25 08:11:06 +00:00
|
|
|
vec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 19:39:50 +00:00
|
|
|
fn oldest_frame(&self) -> Option<VideoCodecFrame> {
|
2020-11-22 09:53:17 +00:00
|
|
|
let frame =
|
|
|
|
unsafe { ffi::gst_video_encoder_get_oldest_frame(self.as_ref().to_glib_none().0) };
|
2019-05-25 08:11:06 +00:00
|
|
|
|
|
|
|
if frame.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
unsafe { Some(VideoCodecFrame::new(frame, self.as_ref())) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 19:39:50 +00:00
|
|
|
fn output_state(&self) -> Option<VideoCodecState<'static, Readable>> {
|
2020-11-22 09:53:17 +00:00
|
|
|
let state =
|
|
|
|
unsafe { ffi::gst_video_encoder_get_output_state(self.as_ref().to_glib_none().0) };
|
2019-05-25 08:11:06 +00:00
|
|
|
|
|
|
|
if state.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
unsafe { Some(VideoCodecState::<Readable>::new(state)) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_output_state(
|
|
|
|
&self,
|
|
|
|
caps: gst::Caps,
|
|
|
|
reference: Option<&VideoCodecState<Readable>>,
|
|
|
|
) -> Result<VideoCodecState<InNegotiation>, gst::FlowError> {
|
|
|
|
let state = unsafe {
|
|
|
|
let reference = match reference {
|
|
|
|
Some(reference) => reference.as_mut_ptr(),
|
|
|
|
None => ptr::null_mut(),
|
|
|
|
};
|
2020-11-22 09:53:17 +00:00
|
|
|
ffi::gst_video_encoder_set_output_state(
|
2019-05-25 08:11:06 +00:00
|
|
|
self.as_ref().to_glib_none().0,
|
2022-05-06 19:41:15 +00:00
|
|
|
caps.into_glib_ptr(),
|
2019-05-25 08:11:06 +00:00
|
|
|
reference,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
if state.is_null() {
|
2019-09-13 19:19:53 +00:00
|
|
|
Err(gst::FlowError::NotNegotiated)
|
2019-05-25 08:11:06 +00:00
|
|
|
} else {
|
|
|
|
unsafe { Ok(VideoCodecState::<InNegotiation>::new(state, self.as_ref())) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn negotiate<'a>(
|
|
|
|
&'a self,
|
|
|
|
output_state: VideoCodecState<'a, InNegotiation<'a>>,
|
|
|
|
) -> Result<(), gst::FlowError> {
|
|
|
|
// Consume output_state so user won't be able to modify it anymore
|
2020-11-22 09:53:17 +00:00
|
|
|
let self_ptr = self.to_glib_none().0 as *const gst::ffi::GstElement;
|
2021-04-11 19:39:50 +00:00
|
|
|
assert_eq!(output_state.context.element_as_ptr(), self_ptr);
|
2019-05-25 08:11:06 +00:00
|
|
|
|
|
|
|
let ret = unsafe {
|
2020-11-22 09:53:17 +00:00
|
|
|
from_glib(ffi::gst_video_encoder_negotiate(
|
2019-05-25 08:11:06 +00:00
|
|
|
self.as_ref().to_glib_none().0,
|
|
|
|
))
|
|
|
|
};
|
|
|
|
if ret {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(gst::FlowError::NotNegotiated)
|
|
|
|
}
|
|
|
|
}
|
2022-05-03 22:04:08 +00:00
|
|
|
|
2023-01-03 19:06:38 +00:00
|
|
|
fn set_headers(&self, headers: impl IntoIterator<Item = gst::Buffer>) {
|
|
|
|
unsafe {
|
|
|
|
ffi::gst_video_encoder_set_headers(
|
|
|
|
self.as_ref().to_glib_none().0,
|
|
|
|
headers
|
|
|
|
.into_iter()
|
|
|
|
.collect::<glib::List<_>>()
|
|
|
|
.into_glib_ptr(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 10:24:46 +00:00
|
|
|
fn sink_pad(&self) -> &gst::Pad {
|
2022-05-03 22:04:08 +00:00
|
|
|
unsafe {
|
2022-05-05 10:24:46 +00:00
|
|
|
let elt = &*(self.as_ptr() as *const ffi::GstVideoEncoder);
|
|
|
|
&*(&elt.sinkpad as *const *mut gst::ffi::GstPad as *const gst::Pad)
|
2022-05-03 22:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 10:24:46 +00:00
|
|
|
fn src_pad(&self) -> &gst::Pad {
|
2022-05-03 22:04:08 +00:00
|
|
|
unsafe {
|
2022-05-05 10:24:46 +00:00
|
|
|
let elt = &*(self.as_ptr() as *const ffi::GstVideoEncoder);
|
|
|
|
&*(&elt.srcpad as *const *mut gst::ffi::GstPad as *const gst::Pad)
|
2022-05-03 22:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-25 08:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HasStreamLock for VideoEncoder {
|
2021-04-11 19:39:50 +00:00
|
|
|
fn stream_lock(&self) -> *mut glib::ffi::GRecMutex {
|
2020-11-22 09:53:17 +00:00
|
|
|
let encoder_sys: *const ffi::GstVideoEncoder = self.to_glib_none().0;
|
2019-05-25 08:11:06 +00:00
|
|
|
unsafe { &(*encoder_sys).stream_lock as *const _ as usize as *mut _ }
|
|
|
|
}
|
|
|
|
|
2021-04-11 19:39:50 +00:00
|
|
|
fn element_as_ptr(&self) -> *const gst::ffi::GstElement {
|
2020-11-22 09:53:17 +00:00
|
|
|
let encoder_sys: *const ffi::GstVideoEncoder = self.to_glib_none().0;
|
|
|
|
encoder_sys as *const gst::ffi::GstElement
|
2019-05-25 08:11:06 +00:00
|
|
|
}
|
|
|
|
}
|