gstreamer_video/auto/video_encoder.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// from gst-gir-files (https://gitlab.freedesktop.org/gstreamer/gir-files-rs.git)
4// DO NOT EDIT
5
6use crate::{VideoCodecFrame, ffi};
7use glib::{
8 prelude::*,
9 signal::{SignalHandlerId, connect_raw},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 /// This base class is for video encoders turning raw video into
16 /// encoded video data.
17 ///
18 /// GstVideoEncoder and subclass should cooperate as follows.
19 ///
20 /// ## Configuration
21 ///
22 /// * Initially, GstVideoEncoder calls `start` when the encoder element
23 /// is activated, which allows subclass to perform any global setup.
24 /// * GstVideoEncoder calls `set_format` to inform subclass of the format
25 /// of input video data that it is about to receive. Subclass should
26 /// setup for encoding and configure base class as appropriate
27 /// (e.g. latency). While unlikely, it might be called more than once,
28 /// if changing input parameters require reconfiguration. Baseclass
29 /// will ensure that processing of current configuration is finished.
30 /// * GstVideoEncoder calls `stop` at end of all processing.
31 ///
32 /// ## Data processing
33 ///
34 /// * Base class collects input data and metadata into a frame and hands
35 /// this to subclass' `handle_frame`.
36 ///
37 /// * If codec processing results in encoded data, subclass should call
38 /// [`VideoEncoderExt::finish_frame()`][crate::prelude::VideoEncoderExt::finish_frame()] to have encoded data pushed
39 /// downstream.
40 ///
41 /// * If implemented, baseclass calls subclass `pre_push` just prior to
42 /// pushing to allow subclasses to modify some metadata on the buffer.
43 /// If it returns GST_FLOW_OK, the buffer is pushed downstream.
44 ///
45 /// * GstVideoEncoderClass will handle both srcpad and sinkpad events.
46 /// Sink events will be passed to subclass if `event` callback has been
47 /// provided.
48 ///
49 /// ## Shutdown phase
50 ///
51 /// * GstVideoEncoder class calls `stop` to inform the subclass that data
52 /// parsing will be stopped.
53 ///
54 /// Subclass is responsible for providing pad template caps for
55 /// source and sink pads. The pads need to be named "sink" and "src". It should
56 /// also be able to provide fixed src pad caps in `getcaps` by the time it calls
57 /// [`VideoEncoderExt::finish_frame()`][crate::prelude::VideoEncoderExt::finish_frame()].
58 ///
59 /// Things that subclass need to take care of:
60 ///
61 /// * Provide pad templates
62 /// * Provide source pad caps before pushing the first buffer
63 /// * Accept data in `handle_frame` and provide encoded results to
64 /// [`VideoEncoderExt::finish_frame()`][crate::prelude::VideoEncoderExt::finish_frame()].
65 ///
66 ///
67 /// ## Adaptive Presets
68 ///
69 /// [`VideoEncoder`][crate::VideoEncoder] supports adaptive presets, where property values in a
70 /// preset file can vary based on the input video resolution and framerate.
71 /// This is done using GKeyFile's locale string syntax to define alternative
72 /// values for properties.
73 ///
74 /// For example, a preset file can specify different bitrates for different
75 /// resolutions:
76 ///
77 /// **⚠️ The following code is in ini ⚠️**
78 ///
79 /// ``` ini
80 /// [Profile YouTube]
81 /// bitrate=2500
82 /// bitrate[1920x1080]=8000
83 /// bitrate[1920x1080@48]=12000
84 /// bitrate[3840x2160]=40000
85 /// bitrate[3840x2160@48]=60000
86 /// ```
87 ///
88 /// When a preset with alternatives is loaded, the best matching
89 /// alternative is applied each time the input format changes, before
90 /// the subclass `set_format` method runs.
91 ///
92 /// Alternative keys use the format `WxH` (matches when the actual pixel
93 /// count >= W*H) or `WxH`FPS`` (additionally requires actual fps >= FPS).
94 /// The most specific match wins (highest pixel count, then highest fps
95 /// threshold). If no alternative matches, the base property value set by
96 /// `gst_preset_load_preset()` remains.
97 ///
98 /// The [`qos`][struct@crate::VideoEncoder#qos] property will enable the Quality-of-Service
99 /// features of the encoder which gather statistics about the real-time
100 /// performance of the downstream elements. If enabled, subclasses can
101 /// use [`VideoEncoderExt::max_encode_time()`][crate::prelude::VideoEncoderExt::max_encode_time()] to check if input frames
102 /// are already late and drop them right away to give a chance to the
103 /// pipeline to catch up.
104 ///
105 /// This is an Abstract Base Class, you cannot instantiate it.
106 ///
107 /// ## Properties
108 ///
109 ///
110 /// #### `min-force-key-unit-interval`
111 /// Minimum interval between force-keyunit requests in nanoseconds. See
112 /// [`VideoEncoderExt::set_min_force_key_unit_interval()`][crate::prelude::VideoEncoderExt::set_min_force_key_unit_interval()] for more details.
113 ///
114 /// Readable | Writeable
115 ///
116 ///
117 /// #### `qos`
118 /// Readable | Writeable
119 /// <details><summary><h4>Object</h4></summary>
120 ///
121 ///
122 /// #### `name`
123 /// Readable | Writeable | Construct
124 ///
125 ///
126 /// #### `parent`
127 /// The parent of the object. Please note, that when changing the 'parent'
128 /// property, we don't emit [`notify`][struct@crate::glib::Object#notify] and [`deep-notify`][struct@crate::gst::Object#deep-notify]
129 /// signals due to locking issues. In some cases one can use
130 /// `GstBin::element-added` or `GstBin::element-removed` signals on the parent to
131 /// achieve a similar effect.
132 ///
133 /// Readable | Writeable
134 /// </details>
135 ///
136 /// # Implements
137 ///
138 /// [`VideoEncoderExt`][trait@crate::prelude::VideoEncoderExt], [`trait@gst::prelude::ElementExt`], [`trait@gst::prelude::ObjectExt`], [`trait@glib::ObjectExt`], [`VideoEncoderExtManual`][trait@crate::prelude::VideoEncoderExtManual]
139 #[doc(alias = "GstVideoEncoder")]
140 pub struct VideoEncoder(Object<ffi::GstVideoEncoder, ffi::GstVideoEncoderClass>) @extends gst::Element, gst::Object;
141
142 match fn {
143 type_ => || ffi::gst_video_encoder_get_type(),
144 }
145}
146
147impl VideoEncoder {
148 pub const NONE: Option<&'static VideoEncoder> = None;
149}
150
151unsafe impl Send for VideoEncoder {}
152unsafe impl Sync for VideoEncoder {}
153
154/// Trait containing all [`struct@VideoEncoder`] methods.
155///
156/// # Implementors
157///
158/// [`VideoEncoder`][struct@crate::VideoEncoder]
159pub trait VideoEncoderExt: IsA<VideoEncoder> + 'static {
160 /// Helper function that allocates a buffer to hold an encoded video frame
161 /// for `self`'s current [`VideoCodecState`][crate::VideoCodecState].
162 /// ## `size`
163 /// size of the buffer
164 ///
165 /// # Returns
166 ///
167 /// allocated buffer
168 #[doc(alias = "gst_video_encoder_allocate_output_buffer")]
169 fn allocate_output_buffer(&self, size: usize) -> gst::Buffer {
170 unsafe {
171 from_glib_full(ffi::gst_video_encoder_allocate_output_buffer(
172 self.as_ref().to_glib_none().0,
173 size,
174 ))
175 }
176 }
177
178 /// Removes `frame` from the list of pending frames, releases it
179 /// and posts a QoS message with the frame's details on the bus.
180 /// Similar to calling [`finish_frame()`][Self::finish_frame()] without a buffer
181 /// attached to `frame`, but this function additionally stores events
182 /// from `frame` as pending, to be pushed out alongside the next frame
183 /// submitted via [`finish_frame()`][Self::finish_frame()].
184 /// ## `frame`
185 /// a [`VideoCodecFrame`][crate::VideoCodecFrame]
186 #[cfg(feature = "v1_26")]
187 #[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
188 #[doc(alias = "gst_video_encoder_drop_frame")]
189 fn drop_frame(&self, frame: VideoCodecFrame) {
190 unsafe {
191 ffi::gst_video_encoder_drop_frame(
192 self.as_ref().to_glib_none().0,
193 frame.into_glib_ptr(),
194 );
195 }
196 }
197
198 /// `frame` must have a valid encoded data buffer, whose metadata fields
199 /// are then appropriately set according to frame data or no buffer at
200 /// all if the frame should be dropped.
201 /// It is subsequently pushed downstream or provided to `pre_push`.
202 /// In any case, the frame is considered finished and released.
203 ///
204 /// If `frame` does not have a buffer attached, it will be dropped, and
205 /// a QoS message will be posted on the bus. Events from `frame` will be
206 /// pushed out immediately.
207 ///
208 /// After calling this function the output buffer of the frame is to be
209 /// considered read-only. This function will also change the metadata
210 /// of the buffer.
211 /// ## `frame`
212 /// an encoded [`VideoCodecFrame`][crate::VideoCodecFrame]
213 ///
214 /// # Returns
215 ///
216 /// a [`gst::FlowReturn`][crate::gst::FlowReturn] resulting from sending data downstream
217 #[doc(alias = "gst_video_encoder_finish_frame")]
218 fn finish_frame(&self, frame: VideoCodecFrame) -> Result<gst::FlowSuccess, gst::FlowError> {
219 unsafe {
220 try_from_glib(ffi::gst_video_encoder_finish_frame(
221 self.as_ref().to_glib_none().0,
222 frame.into_glib_ptr(),
223 ))
224 }
225 }
226
227 /// Determines maximum possible encoding time for `frame` that will
228 /// allow it to encode and arrive in time (as determined by QoS events).
229 /// In particular, a negative result means encoding in time is no longer possible
230 /// and should therefore occur as soon/skippy as possible.
231 ///
232 /// If no QoS events have been received from downstream, or if
233 /// [`qos`][struct@crate::VideoEncoder#qos] is disabled this function returns `G_MAXINT64`.
234 /// ## `frame`
235 /// a [`VideoCodecFrame`][crate::VideoCodecFrame]
236 ///
237 /// # Returns
238 ///
239 /// max decoding time.
240 #[doc(alias = "gst_video_encoder_get_max_encode_time")]
241 #[doc(alias = "get_max_encode_time")]
242 fn max_encode_time(&self, frame: &VideoCodecFrame) -> gst::ClockTimeDiff {
243 unsafe {
244 ffi::gst_video_encoder_get_max_encode_time(
245 self.as_ref().to_glib_none().0,
246 frame.to_glib_none().0,
247 )
248 }
249 }
250
251 /// Returns the minimum force-keyunit interval, see [`set_min_force_key_unit_interval()`][Self::set_min_force_key_unit_interval()]
252 /// for more details.
253 ///
254 /// # Returns
255 ///
256 /// the minimum force-keyunit interval
257 #[cfg(feature = "v1_18")]
258 #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
259 #[doc(alias = "gst_video_encoder_get_min_force_key_unit_interval")]
260 #[doc(alias = "get_min_force_key_unit_interval")]
261 #[doc(alias = "min-force-key-unit-interval")]
262 fn min_force_key_unit_interval(&self) -> Option<gst::ClockTime> {
263 unsafe {
264 from_glib(ffi::gst_video_encoder_get_min_force_key_unit_interval(
265 self.as_ref().to_glib_none().0,
266 ))
267 }
268 }
269
270 /// Checks if `self` is currently configured to handle Quality-of-Service
271 /// events from downstream.
272 ///
273 /// # Returns
274 ///
275 /// [`true`] if the encoder is configured to perform Quality-of-Service.
276 #[doc(alias = "gst_video_encoder_is_qos_enabled")]
277 fn is_qos_enabled(&self) -> bool {
278 unsafe {
279 from_glib(ffi::gst_video_encoder_is_qos_enabled(
280 self.as_ref().to_glib_none().0,
281 ))
282 }
283 }
284
285 /// Sets the video encoder tags and how they should be merged with any
286 /// upstream stream tags. This will override any tags previously-set
287 /// with [`merge_tags()`][Self::merge_tags()].
288 ///
289 /// Note that this is provided for convenience, and the subclass is
290 /// not required to use this and can still do tag handling on its own.
291 ///
292 /// MT safe.
293 /// ## `tags`
294 /// a [`gst::TagList`][crate::gst::TagList] to merge, or NULL to unset
295 /// previously-set tags
296 /// ## `mode`
297 /// the [`gst::TagMergeMode`][crate::gst::TagMergeMode] to use, usually [`gst::TagMergeMode::Replace`][crate::gst::TagMergeMode::Replace]
298 #[doc(alias = "gst_video_encoder_merge_tags")]
299 fn merge_tags(&self, tags: Option<&gst::TagList>, mode: gst::TagMergeMode) {
300 unsafe {
301 ffi::gst_video_encoder_merge_tags(
302 self.as_ref().to_glib_none().0,
303 tags.to_glib_none().0,
304 mode.into_glib(),
305 );
306 }
307 }
308
309 /// Returns caps that express `caps` (or sink template caps if `caps` == NULL)
310 /// restricted to resolution/format/... combinations supported by downstream
311 /// elements (e.g. muxers).
312 /// ## `caps`
313 /// initial caps
314 /// ## `filter`
315 /// filter caps
316 ///
317 /// # Returns
318 ///
319 /// a [`gst::Caps`][crate::gst::Caps] owned by caller
320 #[doc(alias = "gst_video_encoder_proxy_getcaps")]
321 fn proxy_getcaps(&self, caps: Option<&gst::Caps>, filter: Option<&gst::Caps>) -> gst::Caps {
322 unsafe {
323 from_glib_full(ffi::gst_video_encoder_proxy_getcaps(
324 self.as_ref().to_glib_none().0,
325 caps.to_glib_none().0,
326 filter.to_glib_none().0,
327 ))
328 }
329 }
330
331 /// Removes `frame` from list of pending frames and releases it, similar
332 /// to calling [`finish_frame()`][Self::finish_frame()] without a buffer attached
333 /// to the frame, but does not post a QoS message or do any additional
334 /// processing. Events from `frame` are moved to the pending events list.
335 /// ## `frame`
336 /// a [`VideoCodecFrame`][crate::VideoCodecFrame]
337 #[cfg(feature = "v1_26")]
338 #[cfg_attr(docsrs, doc(cfg(feature = "v1_26")))]
339 #[doc(alias = "gst_video_encoder_release_frame")]
340 fn release_frame(&self, frame: VideoCodecFrame) {
341 unsafe {
342 ffi::gst_video_encoder_release_frame(
343 self.as_ref().to_glib_none().0,
344 frame.into_glib_ptr(),
345 );
346 }
347 }
348
349 /// Allows [`VideoEncoder`][crate::VideoEncoder] sub-classes to set the memory `allocator` and
350 /// its `params`.
351 /// ## `allocator`
352 /// the [`gst::Allocator`][crate::gst::Allocator]
353 /// ## `params`
354 /// the [`gst::AllocationParams`][crate::gst::AllocationParams] of `allocator`
355 #[cfg(feature = "v1_30")]
356 #[cfg_attr(docsrs, doc(cfg(feature = "v1_30")))]
357 #[doc(alias = "gst_video_encoder_set_allocator")]
358 fn set_allocator(
359 &self,
360 allocator: Option<impl IsA<gst::Allocator>>,
361 params: Option<&gst::AllocationParams>,
362 ) {
363 unsafe {
364 ffi::gst_video_encoder_set_allocator(
365 self.as_ref().to_glib_none().0,
366 allocator.map(|p| p.upcast()).into_glib_ptr(),
367 params.to_glib_none().0,
368 );
369 }
370 }
371
372 /// Sets the minimum interval for requesting keyframes based on force-keyunit
373 /// events. Setting this to 0 will allow to handle every event, setting this to
374 /// `GST_CLOCK_TIME_NONE` causes force-keyunit events to be ignored.
375 /// ## `interval`
376 /// minimum interval
377 #[cfg(feature = "v1_18")]
378 #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
379 #[doc(alias = "gst_video_encoder_set_min_force_key_unit_interval")]
380 #[doc(alias = "min-force-key-unit-interval")]
381 fn set_min_force_key_unit_interval(&self, interval: impl Into<Option<gst::ClockTime>>) {
382 unsafe {
383 ffi::gst_video_encoder_set_min_force_key_unit_interval(
384 self.as_ref().to_glib_none().0,
385 interval.into().into_glib(),
386 );
387 }
388 }
389
390 /// Request minimal value for PTS passed to handle_frame.
391 ///
392 /// For streams with reordered frames this can be used to ensure that there
393 /// is enough time to accommodate first DTS, which may be less than first PTS
394 /// ## `min_pts`
395 /// minimal PTS that will be passed to handle_frame
396 #[doc(alias = "gst_video_encoder_set_min_pts")]
397 fn set_min_pts(&self, min_pts: impl Into<Option<gst::ClockTime>>) {
398 unsafe {
399 ffi::gst_video_encoder_set_min_pts(
400 self.as_ref().to_glib_none().0,
401 min_pts.into().into_glib(),
402 );
403 }
404 }
405
406 /// Configures `self` to handle Quality-of-Service events from downstream.
407 /// ## `enabled`
408 /// the new qos value.
409 #[doc(alias = "gst_video_encoder_set_qos_enabled")]
410 fn set_qos_enabled(&self, enabled: bool) {
411 unsafe {
412 ffi::gst_video_encoder_set_qos_enabled(
413 self.as_ref().to_glib_none().0,
414 enabled.into_glib(),
415 );
416 }
417 }
418
419 fn is_qos(&self) -> bool {
420 ObjectExt::property(self.as_ref(), "qos")
421 }
422
423 fn set_qos(&self, qos: bool) {
424 ObjectExt::set_property(self.as_ref(), "qos", qos)
425 }
426
427 #[cfg(feature = "v1_18")]
428 #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
429 #[doc(alias = "min-force-key-unit-interval")]
430 fn connect_min_force_key_unit_interval_notify<F: Fn(&Self) + Send + Sync + 'static>(
431 &self,
432 f: F,
433 ) -> SignalHandlerId {
434 unsafe extern "C" fn notify_min_force_key_unit_interval_trampoline<
435 P: IsA<VideoEncoder>,
436 F: Fn(&P) + Send + Sync + 'static,
437 >(
438 this: *mut ffi::GstVideoEncoder,
439 _param_spec: glib::ffi::gpointer,
440 f: glib::ffi::gpointer,
441 ) {
442 unsafe {
443 let f: &F = &*(f as *const F);
444 f(VideoEncoder::from_glib_borrow(this).unsafe_cast_ref())
445 }
446 }
447 unsafe {
448 let f: Box_<F> = Box_::new(f);
449 connect_raw(
450 self.as_ptr() as *mut _,
451 c"notify::min-force-key-unit-interval".as_ptr(),
452 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
453 notify_min_force_key_unit_interval_trampoline::<Self, F> as *const (),
454 )),
455 Box_::into_raw(f),
456 )
457 }
458 }
459
460 #[doc(alias = "qos")]
461 fn connect_qos_notify<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
462 unsafe extern "C" fn notify_qos_trampoline<
463 P: IsA<VideoEncoder>,
464 F: Fn(&P) + Send + Sync + 'static,
465 >(
466 this: *mut ffi::GstVideoEncoder,
467 _param_spec: glib::ffi::gpointer,
468 f: glib::ffi::gpointer,
469 ) {
470 unsafe {
471 let f: &F = &*(f as *const F);
472 f(VideoEncoder::from_glib_borrow(this).unsafe_cast_ref())
473 }
474 }
475 unsafe {
476 let f: Box_<F> = Box_::new(f);
477 connect_raw(
478 self.as_ptr() as *mut _,
479 c"notify::qos".as_ptr(),
480 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
481 notify_qos_trampoline::<Self, F> as *const (),
482 )),
483 Box_::into_raw(f),
484 )
485 }
486 }
487}
488
489impl<O: IsA<VideoEncoder>> VideoEncoderExt for O {}