2 * Copyright (C) 2014-2017 SUMOMO Computer Association
3 * Authors Ayaka <ayaka@soulik.info>
4 * Copyright (C) 2017 Collabora Ltd.
5 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
34 #include "gstv4l2videoenc.h"
35 #include "v4l2_calls.h"
38 #include <gst/gst-i18n-plugin.h>
40 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_video_enc_debug);
41 #define GST_CAT_DEFAULT gst_v4l2_video_enc_debug
48 } GstV4l2VideoEncCData;
53 V4L2_STD_OBJECT_PROPS,
56 #define gst_v4l2_video_enc_parent_class parent_class
57 G_DEFINE_ABSTRACT_TYPE (GstV4l2VideoEnc, gst_v4l2_video_enc,
58 GST_TYPE_VIDEO_ENCODER);
61 gst_v4l2_video_enc_set_property (GObject * object,
62 guint prop_id, const GValue * value, GParamSpec * pspec)
64 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (object);
67 case PROP_OUTPUT_IO_MODE:
68 gst_v4l2_object_set_property_helper (self->v4l2output,
69 prop_id, value, pspec);
71 case PROP_CAPTURE_IO_MODE:
72 gst_v4l2_object_set_property_helper (self->v4l2capture,
73 prop_id, value, pspec);
76 /* By default, only set on output */
78 if (!gst_v4l2_object_set_property_helper (self->v4l2output,
79 prop_id, value, pspec)) {
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87 gst_v4l2_video_enc_get_property (GObject * object,
88 guint prop_id, GValue * value, GParamSpec * pspec)
90 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (object);
93 case PROP_OUTPUT_IO_MODE:
94 gst_v4l2_object_get_property_helper (self->v4l2output,
95 prop_id, value, pspec);
97 case PROP_CAPTURE_IO_MODE:
98 gst_v4l2_object_get_property_helper (self->v4l2output,
99 PROP_IO_MODE, value, pspec);
102 /* By default read from output */
104 if (!gst_v4l2_object_get_property_helper (self->v4l2output,
105 prop_id, value, pspec)) {
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113 gst_v4l2_video_enc_open (GstVideoEncoder * encoder)
115 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
117 GST_DEBUG_OBJECT (self, "Opening");
119 if (!gst_v4l2_object_open (self->v4l2output))
122 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
125 self->probed_sinkcaps = gst_v4l2_object_get_caps (self->v4l2output,
126 gst_v4l2_object_get_raw_caps ());
128 if (gst_caps_is_empty (self->probed_sinkcaps))
131 self->probed_srccaps = gst_v4l2_object_get_caps (self->v4l2capture,
132 gst_v4l2_object_get_codec_caps ());
134 if (gst_caps_is_empty (self->probed_srccaps))
135 goto no_encoded_format;
140 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
141 (_("Encoder on device %s has no supported output format"),
142 self->v4l2output->videodev), (NULL));
147 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
148 (_("Encoder on device %s has no supported input format"),
149 self->v4l2output->videodev), (NULL));
153 if (GST_V4L2_IS_OPEN (self->v4l2output))
154 gst_v4l2_object_close (self->v4l2output);
156 if (GST_V4L2_IS_OPEN (self->v4l2capture))
157 gst_v4l2_object_close (self->v4l2capture);
159 gst_caps_replace (&self->probed_srccaps, NULL);
160 gst_caps_replace (&self->probed_sinkcaps, NULL);
166 gst_v4l2_video_enc_close (GstVideoEncoder * encoder)
168 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
170 GST_DEBUG_OBJECT (self, "Closing");
172 gst_v4l2_object_close (self->v4l2output);
173 gst_v4l2_object_close (self->v4l2capture);
174 gst_caps_replace (&self->probed_srccaps, NULL);
175 gst_caps_replace (&self->probed_sinkcaps, NULL);
181 gst_v4l2_video_enc_start (GstVideoEncoder * encoder)
183 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
185 GST_DEBUG_OBJECT (self, "Starting");
187 gst_v4l2_object_unlock (self->v4l2output);
188 g_atomic_int_set (&self->active, TRUE);
189 self->output_flow = GST_FLOW_OK;
195 gst_v4l2_video_enc_stop (GstVideoEncoder * encoder)
197 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
199 GST_DEBUG_OBJECT (self, "Stopping");
201 gst_v4l2_object_unlock (self->v4l2output);
202 gst_v4l2_object_unlock (self->v4l2capture);
204 /* Wait for capture thread to stop */
205 gst_pad_stop_task (encoder->srcpad);
207 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
208 self->output_flow = GST_FLOW_OK;
209 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
211 /* Should have been flushed already */
212 g_assert (g_atomic_int_get (&self->active) == FALSE);
213 g_assert (g_atomic_int_get (&self->processing) == FALSE);
215 gst_v4l2_object_stop (self->v4l2output);
216 gst_v4l2_object_stop (self->v4l2capture);
218 if (self->input_state) {
219 gst_video_codec_state_unref (self->input_state);
220 self->input_state = NULL;
223 GST_DEBUG_OBJECT (self, "Stopped");
229 gst_v4l2_encoder_cmd (GstV4l2Object * v4l2object, guint cmd, guint flags)
231 struct v4l2_encoder_cmd ecmd = { 0, };
233 GST_DEBUG_OBJECT (v4l2object->element,
234 "sending v4l2 encoder command %u with flags %u", cmd, flags);
236 if (!GST_V4L2_IS_OPEN (v4l2object))
241 if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENCODER_CMD, &ecmd) < 0)
247 if (errno == ENOTTY) {
248 GST_INFO_OBJECT (v4l2object->element,
249 "Failed to send encoder command %u with flags %u for '%s'. (%s)",
250 cmd, flags, v4l2object->videodev, g_strerror (errno));
252 GST_ERROR_OBJECT (v4l2object->element,
253 "Failed to send encoder command %u with flags %u for '%s'. (%s)",
254 cmd, flags, v4l2object->videodev, g_strerror (errno));
260 gst_v4l2_video_enc_finish (GstVideoEncoder * encoder)
262 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
263 GstFlowReturn ret = GST_FLOW_OK;
265 if (gst_pad_get_task_state (encoder->srcpad) != GST_TASK_STARTED)
268 GST_DEBUG_OBJECT (self, "Finishing encoding");
270 /* drop the stream lock while draining, so remaining buffers can be
271 * pushed from the src pad task thread */
272 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
274 if (gst_v4l2_encoder_cmd (self->v4l2capture, V4L2_ENC_CMD_STOP, 0)) {
275 GstTask *task = encoder->srcpad->task;
277 /* Wait for the task to be drained */
278 GST_OBJECT_LOCK (task);
279 while (GST_TASK_STATE (task) == GST_TASK_STARTED)
280 GST_TASK_WAIT (task);
281 GST_OBJECT_UNLOCK (task);
282 ret = GST_FLOW_FLUSHING;
285 /* and ensure the processing thread has stopped in case another error
287 gst_v4l2_object_unlock (self->v4l2capture);
288 gst_pad_stop_task (encoder->srcpad);
289 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
291 if (ret == GST_FLOW_FLUSHING)
292 ret = self->output_flow;
294 GST_DEBUG_OBJECT (encoder, "Done draining buffers");
301 gst_v4l2_video_enc_set_format (GstVideoEncoder * encoder,
302 GstVideoCodecState * state)
305 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
306 GstV4l2Error error = GST_V4L2_ERROR_INIT;
309 GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
311 if (self->input_state) {
312 if (gst_v4l2_object_caps_equal (self->v4l2output, state->caps)) {
313 GST_DEBUG_OBJECT (self, "Compatible caps");
317 if (gst_v4l2_video_enc_finish (encoder) != GST_FLOW_OK)
320 gst_video_codec_state_unref (self->input_state);
321 self->input_state = NULL;
324 outcaps = gst_pad_get_pad_template_caps (encoder->srcpad);
325 outcaps = gst_caps_make_writable (outcaps);
326 gst_video_encoder_set_output_state (encoder, outcaps, state);
328 if (!gst_video_encoder_negotiate (encoder))
331 if (!gst_v4l2_object_set_format (self->v4l2output, state->caps, &error)) {
332 gst_v4l2_error (self, &error);
336 self->input_state = gst_video_codec_state_ref (state);
338 GST_DEBUG_OBJECT (self, "output caps: %" GST_PTR_FORMAT, state->caps);
344 gst_v4l2_video_enc_flush (GstVideoEncoder * encoder)
346 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
348 GST_DEBUG_OBJECT (self, "Flushing");
350 /* Ensure the processing thread has stopped for the reverse playback
352 if (g_atomic_int_get (&self->processing)) {
353 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
355 gst_v4l2_object_unlock_stop (self->v4l2output);
356 gst_v4l2_object_unlock_stop (self->v4l2capture);
357 gst_pad_stop_task (encoder->srcpad);
359 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
363 self->output_flow = GST_FLOW_OK;
365 gst_v4l2_object_unlock_stop (self->v4l2output);
366 gst_v4l2_object_unlock_stop (self->v4l2capture);
372 gst_v4l2_video_enc_negotiate (GstVideoEncoder * encoder)
374 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
377 ret = GST_VIDEO_ENCODER_CLASS (parent_class)->negotiate (encoder);
379 if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
381 GST_WARNING_OBJECT (self, "Could not activate capture buffer pool.");
388 static GstVideoCodecFrame *
389 gst_v4l2_video_enc_get_oldest_frame (GstVideoEncoder * encoder)
391 GstVideoCodecFrame *frame = NULL;
395 frames = gst_video_encoder_get_frames (encoder);
397 for (l = frames; l != NULL; l = l->next) {
398 GstVideoCodecFrame *f = l->data;
400 if (!frame || frame->pts > f->pts)
407 GST_LOG_OBJECT (encoder,
408 "Oldest frame is %d %" GST_TIME_FORMAT
409 " and %d frames left",
410 frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
411 gst_video_codec_frame_ref (frame);
414 g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
420 gst_v4l2_video_enc_loop (GstVideoEncoder * encoder)
422 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
423 GstVideoCodecFrame *frame;
424 GstBuffer *buffer = NULL;
427 GST_LOG_OBJECT (encoder, "Allocate output buffer");
429 buffer = gst_video_encoder_allocate_output_buffer (encoder,
430 self->v4l2capture->info.size);
432 if (NULL == buffer) {
433 ret = GST_FLOW_FLUSHING;
438 /* FIXME Check if buffer isn't the last one here */
440 GST_LOG_OBJECT (encoder, "Process output buffer");
442 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL
443 (self->v4l2capture->pool), &buffer);
445 if (ret != GST_FLOW_OK)
448 frame = gst_v4l2_video_enc_get_oldest_frame (encoder);
451 frame->output_buffer = buffer;
453 ret = gst_video_encoder_finish_frame (encoder, frame);
455 if (ret != GST_FLOW_OK)
458 GST_WARNING_OBJECT (encoder, "Encoder is producing too many buffers");
459 gst_buffer_unref (buffer);
465 GST_DEBUG_OBJECT (encoder, "Leaving output thread");
467 gst_buffer_replace (&buffer, NULL);
468 self->output_flow = ret;
469 g_atomic_int_set (&self->processing, FALSE);
470 gst_v4l2_object_unlock (self->v4l2output);
471 gst_pad_pause_task (encoder->srcpad);
475 gst_v4l2_video_enc_loop_stopped (GstV4l2VideoEnc * self)
477 if (g_atomic_int_get (&self->processing)) {
478 GST_DEBUG_OBJECT (self, "Early stop of encoding thread");
479 self->output_flow = GST_FLOW_FLUSHING;
480 g_atomic_int_set (&self->processing, FALSE);
483 GST_DEBUG_OBJECT (self, "Encoding task destroyed: %s",
484 gst_flow_get_name (self->output_flow));
489 gst_v4l2_video_enc_handle_frame (GstVideoEncoder * encoder,
490 GstVideoCodecFrame * frame)
492 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
493 GstFlowReturn ret = GST_FLOW_OK;
495 GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
497 if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
500 if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) ==
502 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
504 /* It possible that the processing thread stopped due to an error */
505 if (self->output_flow != GST_FLOW_OK &&
506 self->output_flow != GST_FLOW_FLUSHING) {
507 GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
508 ret = self->output_flow;
512 /* Ensure input internal pool is active */
513 if (!gst_buffer_pool_is_active (pool)) {
514 GstStructure *config = gst_buffer_pool_get_config (pool);
515 gst_buffer_pool_config_set_params (config, self->input_state->caps,
516 self->v4l2output->info.size, self->v4l2output->min_buffers,
517 self->v4l2output->min_buffers);
519 /* There is no reason to refuse this config */
520 if (!gst_buffer_pool_set_config (pool, config))
521 goto activate_failed;
523 if (!gst_buffer_pool_set_active (pool, TRUE))
524 goto activate_failed;
527 GST_DEBUG_OBJECT (self, "Starting encoding thread");
529 /* Start the processing task, when it quits, the task will disable input
530 * processing to unlock input if draining, or prevent potential block */
531 if (!gst_pad_start_task (encoder->srcpad,
532 (GstTaskFunction) gst_v4l2_video_enc_loop, self,
533 (GDestroyNotify) gst_v4l2_video_enc_loop_stopped))
534 goto start_task_failed;
537 if (frame->input_buffer) {
538 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
540 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL
541 (self->v4l2output->pool), &frame->input_buffer);
542 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
544 if (ret == GST_FLOW_FLUSHING) {
545 if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) !=
547 ret = self->output_flow;
549 } else if (ret != GST_FLOW_OK) {
554 gst_video_codec_frame_unref (frame);
560 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
561 (_("Failed to allocate required memory.")),
562 ("Buffer pool activation failed"));
563 return GST_FLOW_ERROR;
568 ret = GST_FLOW_FLUSHING;
573 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
574 (_("Failed to start encoding thread.")), (NULL));
575 g_atomic_int_set (&self->processing, FALSE);
576 ret = GST_FLOW_ERROR;
581 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
582 (_("Failed to process frame.")),
583 ("Maybe be due to not enough memory or failing driver"));
584 ret = GST_FLOW_ERROR;
589 gst_video_encoder_finish_frame (encoder, frame);
595 gst_v4l2_video_enc_decide_allocation (GstVideoEncoder *
596 encoder, GstQuery * query)
598 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
599 GstVideoCodecState *state = gst_video_encoder_get_output_state (encoder);
600 GstV4l2Error error = GST_V4L2_ERROR_INIT;
601 GstClockTime latency;
602 gboolean ret = FALSE;
604 /* We need to set the format here, since this is called right after
605 * GstVideoEncoder have set the width, height and framerate into the state
606 * caps. These are needed by the driver to calculate the buffer size and to
607 * implement bitrate adaptation. */
608 if (!gst_v4l2_object_set_format (self->v4l2capture, state->caps, &error)) {
609 gst_v4l2_error (self, &error);
614 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query)) {
615 GstVideoEncoderClass *enc_class = GST_VIDEO_ENCODER_CLASS (parent_class);
616 ret = enc_class->decide_allocation (encoder, query);
619 /* FIXME This may not be entirely correct, as encoder may keep some
620 * observation withouth delaying the encoding. Linux Media API need some
621 * more work to explicitly expressed the decoder / encoder latency. This
622 * value will then become max latency, and the reported driver latency would
623 * become the min latency. */
624 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
625 gst_video_encoder_set_latency (encoder, latency, latency);
628 gst_video_codec_state_unref (state);
633 gst_v4l2_video_enc_propose_allocation (GstVideoEncoder *
634 encoder, GstQuery * query)
636 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
637 gboolean ret = FALSE;
639 GST_DEBUG_OBJECT (self, "called");
644 ret = gst_v4l2_object_propose_allocation (self->v4l2output, query);
647 ret = GST_VIDEO_ENCODER_CLASS (parent_class)->propose_allocation (encoder,
654 gst_v4l2_video_enc_src_query (GstVideoEncoder * encoder, GstQuery * query)
657 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
658 switch (GST_QUERY_TYPE (query)) {
659 case GST_QUERY_CAPS:{
660 GstCaps *filter, *result = NULL;
661 GstPad *pad = GST_VIDEO_ENCODER_SRC_PAD (encoder);
663 gst_query_parse_caps (query, &filter);
665 /* FIXME Try and not probe the entire encoder, but only the implement
667 if (self->probed_srccaps) {
668 GstCaps *tmpl = gst_pad_get_pad_template_caps (pad);
669 result = gst_caps_intersect (tmpl, self->probed_srccaps);
670 gst_caps_unref (tmpl);
672 result = gst_pad_get_pad_template_caps (pad);
675 GstCaps *tmp = result;
677 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
678 gst_caps_unref (tmp);
681 GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
683 gst_query_set_caps_result (query, result);
684 gst_caps_unref (result);
689 ret = GST_VIDEO_ENCODER_CLASS (parent_class)->src_query (encoder, query);
697 gst_v4l2_video_enc_sink_query (GstVideoEncoder * encoder, GstQuery * query)
700 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
702 switch (GST_QUERY_TYPE (query)) {
703 case GST_QUERY_CAPS:{
704 GstCaps *filter, *result = NULL;
705 GstPad *pad = GST_VIDEO_ENCODER_SINK_PAD (encoder);
707 gst_query_parse_caps (query, &filter);
709 if (self->probed_sinkcaps)
710 result = gst_caps_ref (self->probed_sinkcaps);
712 result = gst_pad_get_pad_template_caps (pad);
715 GstCaps *tmp = result;
717 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
718 gst_caps_unref (tmp);
721 GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
723 gst_query_set_caps_result (query, result);
724 gst_caps_unref (result);
729 ret = GST_VIDEO_ENCODER_CLASS (parent_class)->sink_query (encoder, query);
737 gst_v4l2_video_enc_sink_event (GstVideoEncoder * encoder, GstEvent * event)
739 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
742 switch (GST_EVENT_TYPE (event)) {
743 case GST_EVENT_FLUSH_START:
744 GST_DEBUG_OBJECT (self, "flush start");
745 gst_v4l2_object_unlock (self->v4l2output);
746 gst_v4l2_object_unlock (self->v4l2capture);
752 ret = GST_VIDEO_ENCODER_CLASS (parent_class)->sink_event (encoder, event);
754 switch (GST_EVENT_TYPE (event)) {
755 case GST_EVENT_FLUSH_START:
756 gst_pad_stop_task (encoder->srcpad);
757 GST_DEBUG_OBJECT (self, "flush start done");
765 static GstStateChangeReturn
766 gst_v4l2_video_enc_change_state (GstElement * element,
767 GstStateChange transition)
769 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (element);
771 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
772 g_atomic_int_set (&self->active, FALSE);
773 gst_v4l2_object_unlock (self->v4l2output);
774 gst_v4l2_object_unlock (self->v4l2capture);
777 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
781 gst_v4l2_video_enc_dispose (GObject * object)
783 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (object);
785 gst_caps_replace (&self->probed_sinkcaps, NULL);
786 gst_caps_replace (&self->probed_srccaps, NULL);
788 G_OBJECT_CLASS (parent_class)->dispose (object);
792 gst_v4l2_video_enc_finalize (GObject * object)
794 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (object);
796 gst_v4l2_object_destroy (self->v4l2capture);
797 gst_v4l2_object_destroy (self->v4l2output);
799 G_OBJECT_CLASS (parent_class)->finalize (object);
803 gst_v4l2_video_enc_init (GstV4l2VideoEnc * self)
805 /* V4L2 object are created in subinstance_init */
809 gst_v4l2_video_enc_subinstance_init (GTypeInstance * instance, gpointer g_class)
811 GstV4l2VideoEncClass *klass = GST_V4L2_VIDEO_ENC_CLASS (g_class);
812 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (instance);
814 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
815 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
816 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
817 self->v4l2output->no_initial_format = TRUE;
818 self->v4l2output->keep_aspect = FALSE;
820 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
821 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
822 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
823 self->v4l2capture->no_initial_format = TRUE;
824 self->v4l2output->keep_aspect = FALSE;
828 gst_v4l2_video_enc_class_init (GstV4l2VideoEncClass * klass)
830 GstElementClass *element_class;
831 GObjectClass *gobject_class;
832 GstVideoEncoderClass *video_encoder_class;
834 parent_class = g_type_class_peek_parent (klass);
836 element_class = (GstElementClass *) klass;
837 gobject_class = (GObjectClass *) klass;
838 video_encoder_class = (GstVideoEncoderClass *) klass;
840 GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_enc_debug, "v4l2videoenc", 0,
841 "V4L2 Video Encoder");
843 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_dispose);
844 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_finalize);
845 gobject_class->set_property =
846 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_set_property);
847 gobject_class->get_property =
848 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_get_property);
850 video_encoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_open);
851 video_encoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_close);
852 video_encoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_start);
853 video_encoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_stop);
854 video_encoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_finish);
855 video_encoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_flush);
856 video_encoder_class->set_format =
857 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_set_format);
858 video_encoder_class->negotiate =
859 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_negotiate);
860 video_encoder_class->decide_allocation =
861 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_decide_allocation);
862 video_encoder_class->propose_allocation =
863 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_propose_allocation);
864 video_encoder_class->sink_query =
865 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_sink_query);
866 video_encoder_class->src_query =
867 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_src_query);
868 video_encoder_class->sink_event =
869 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_sink_event);
870 video_encoder_class->handle_frame =
871 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_handle_frame);
873 element_class->change_state =
874 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_change_state);
876 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
880 gst_v4l2_video_enc_subclass_init (gpointer g_class, gpointer data)
882 GstV4l2VideoEncClass *klass = GST_V4L2_VIDEO_ENC_CLASS (g_class);
883 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
884 GstV4l2VideoEncCData *cdata = data;
886 klass->default_device = cdata->device;
888 /* Note: gst_pad_template_new() take the floating ref from the caps */
889 gst_element_class_add_pad_template (element_class,
890 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
892 gst_element_class_add_pad_template (element_class,
893 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
899 /* Probing functions */
901 gst_v4l2_video_enc_register (GstPlugin * plugin, GType type,
902 const char *codec, const gchar * basename, const gchar * device_path,
903 GstCaps * sink_caps, GstCaps * codec_caps, GstCaps * src_caps)
905 GstCaps *filtered_caps;
906 GTypeQuery type_query;
907 GTypeInfo type_info = { 0, };
910 GstV4l2VideoEncCData *cdata;
912 filtered_caps = gst_caps_intersect (src_caps, codec_caps);
914 cdata = g_new0 (GstV4l2VideoEncCData, 1);
915 cdata->device = g_strdup (device_path);
916 cdata->sink_caps = gst_caps_ref (sink_caps);
917 cdata->src_caps = gst_caps_ref (filtered_caps);
919 g_type_query (type, &type_query);
920 memset (&type_info, 0, sizeof (type_info));
921 type_info.class_size = type_query.class_size;
922 type_info.instance_size = type_query.instance_size;
923 type_info.class_init = gst_v4l2_video_enc_subclass_init;
924 type_info.class_data = cdata;
925 type_info.instance_init = gst_v4l2_video_enc_subinstance_init;
927 /* The first encoder to be registered should use a constant name, like
928 * v4l2h264enc, for any additional encoders, we create unique names. Encoder
929 * names may change between boots, so this should help gain stable names for
930 * the most common use cases. */
931 type_name = g_strdup_printf ("v4l2%senc", codec);
933 if (g_type_from_name (type_name) != 0) {
935 type_name = g_strdup_printf ("v4l2%s%senc", basename, codec);
938 subtype = g_type_register_static (type, type_name, &type_info, 0);
939 gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1, subtype);