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 "gstv4l2object.h"
35 #include "gstv4l2videoenc.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_CAPTURE_IO_MODE:
68 if (!gst_v4l2_object_set_property_helper (self->v4l2capture,
69 prop_id, value, pspec)) {
70 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
74 /* By default, only set on output */
76 if (!gst_v4l2_object_set_property_helper (self->v4l2output,
77 prop_id, value, pspec)) {
78 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
85 gst_v4l2_video_enc_get_property (GObject * object,
86 guint prop_id, GValue * value, GParamSpec * pspec)
88 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (object);
91 case PROP_CAPTURE_IO_MODE:
92 if (!gst_v4l2_object_get_property_helper (self->v4l2capture,
93 prop_id, value, pspec)) {
94 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
98 /* By default read from output */
100 if (!gst_v4l2_object_get_property_helper (self->v4l2output,
101 prop_id, value, pspec)) {
102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109 gst_v4l2_video_enc_open (GstVideoEncoder * encoder)
111 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
114 GST_DEBUG_OBJECT (self, "Opening");
116 if (!gst_v4l2_object_open (self->v4l2output))
119 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
122 self->probed_sinkcaps = gst_v4l2_object_probe_caps (self->v4l2output,
123 gst_v4l2_object_get_raw_caps ());
125 if (gst_caps_is_empty (self->probed_sinkcaps))
128 codec_caps = gst_pad_get_pad_template_caps (encoder->srcpad);
129 self->probed_srccaps = gst_v4l2_object_probe_caps (self->v4l2capture,
131 gst_caps_unref (codec_caps);
133 if (gst_caps_is_empty (self->probed_srccaps))
134 goto no_encoded_format;
139 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
140 (_("Encoder on device %s has no supported output format"),
141 self->v4l2output->videodev), (NULL));
146 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
147 (_("Encoder on device %s has no supported input format"),
148 self->v4l2output->videodev), (NULL));
152 if (GST_V4L2_IS_OPEN (self->v4l2output))
153 gst_v4l2_object_close (self->v4l2output);
155 if (GST_V4L2_IS_OPEN (self->v4l2capture))
156 gst_v4l2_object_close (self->v4l2capture);
158 gst_caps_replace (&self->probed_srccaps, NULL);
159 gst_caps_replace (&self->probed_sinkcaps, NULL);
165 gst_v4l2_video_enc_close (GstVideoEncoder * encoder)
167 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
169 GST_DEBUG_OBJECT (self, "Closing");
171 gst_v4l2_object_close (self->v4l2output);
172 gst_v4l2_object_close (self->v4l2capture);
173 gst_caps_replace (&self->probed_srccaps, NULL);
174 gst_caps_replace (&self->probed_sinkcaps, NULL);
180 gst_v4l2_video_enc_start (GstVideoEncoder * encoder)
182 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
184 GST_DEBUG_OBJECT (self, "Starting");
186 gst_v4l2_object_unlock (self->v4l2output);
187 g_atomic_int_set (&self->active, TRUE);
188 self->output_flow = GST_FLOW_OK;
194 gst_v4l2_video_enc_stop (GstVideoEncoder * encoder)
196 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
198 GST_DEBUG_OBJECT (self, "Stopping");
200 gst_v4l2_object_unlock (self->v4l2output);
201 gst_v4l2_object_unlock (self->v4l2capture);
203 /* Wait for capture thread to stop */
204 gst_pad_stop_task (encoder->srcpad);
206 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
207 self->output_flow = GST_FLOW_OK;
208 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
210 /* Should have been flushed already */
211 g_assert (g_atomic_int_get (&self->active) == FALSE);
212 g_assert (g_atomic_int_get (&self->processing) == FALSE);
214 gst_v4l2_object_stop (self->v4l2output);
215 gst_v4l2_object_stop (self->v4l2capture);
217 if (self->input_state) {
218 gst_video_codec_state_unref (self->input_state);
219 self->input_state = NULL;
222 GST_DEBUG_OBJECT (self, "Stopped");
228 gst_v4l2_encoder_cmd (GstV4l2Object * v4l2object, guint cmd, guint flags)
230 struct v4l2_encoder_cmd ecmd = { 0, };
232 GST_DEBUG_OBJECT (v4l2object->element,
233 "sending v4l2 encoder command %u with flags %u", cmd, flags);
235 if (!GST_V4L2_IS_OPEN (v4l2object))
240 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_ENCODER_CMD, &ecmd) < 0)
246 if (errno == ENOTTY) {
247 GST_INFO_OBJECT (v4l2object->element,
248 "Failed to send encoder command %u with flags %u for '%s'. (%s)",
249 cmd, flags, v4l2object->videodev, g_strerror (errno));
251 GST_ERROR_OBJECT (v4l2object->element,
252 "Failed to send encoder command %u with flags %u for '%s'. (%s)",
253 cmd, flags, v4l2object->videodev, g_strerror (errno));
259 gst_v4l2_video_enc_finish (GstVideoEncoder * encoder)
261 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
262 GstFlowReturn ret = GST_FLOW_OK;
264 if (gst_pad_get_task_state (encoder->srcpad) != GST_TASK_STARTED)
267 GST_DEBUG_OBJECT (self, "Finishing encoding");
269 /* drop the stream lock while draining, so remaining buffers can be
270 * pushed from the src pad task thread */
271 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
273 if (gst_v4l2_encoder_cmd (self->v4l2capture, V4L2_ENC_CMD_STOP, 0)) {
274 GstTask *task = encoder->srcpad->task;
276 /* Wait for the task to be drained */
277 GST_OBJECT_LOCK (task);
278 while (GST_TASK_STATE (task) == GST_TASK_STARTED)
279 GST_TASK_WAIT (task);
280 GST_OBJECT_UNLOCK (task);
281 ret = GST_FLOW_FLUSHING;
284 /* and ensure the processing thread has stopped in case another error
286 gst_v4l2_object_unlock (self->v4l2capture);
287 gst_pad_stop_task (encoder->srcpad);
288 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
290 if (ret == GST_FLOW_FLUSHING)
291 ret = self->output_flow;
293 GST_DEBUG_OBJECT (encoder, "Done draining buffers");
300 gst_v4l2_video_enc_set_format (GstVideoEncoder * encoder,
301 GstVideoCodecState * state)
304 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
305 GstV4l2Error error = GST_V4L2_ERROR_INIT;
307 GstVideoCodecState *output;
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_v4l2_object_stop (self->v4l2output);
321 gst_v4l2_object_stop (self->v4l2capture);
323 gst_video_codec_state_unref (self->input_state);
324 self->input_state = NULL;
327 outcaps = gst_pad_get_pad_template_caps (encoder->srcpad);
328 outcaps = gst_caps_make_writable (outcaps);
329 output = gst_video_encoder_set_output_state (encoder, outcaps, state);
330 gst_video_codec_state_unref (output);
332 if (!gst_video_encoder_negotiate (encoder))
335 if (!gst_v4l2_object_set_format (self->v4l2output, state->caps, &error)) {
336 gst_v4l2_error (self, &error);
340 self->input_state = gst_video_codec_state_ref (state);
342 GST_DEBUG_OBJECT (self, "output caps: %" GST_PTR_FORMAT, state->caps);
348 gst_v4l2_video_enc_flush (GstVideoEncoder * encoder)
350 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
352 GST_DEBUG_OBJECT (self, "Flushing");
354 /* Ensure the processing thread has stopped for the reverse playback
356 if (g_atomic_int_get (&self->processing)) {
357 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
359 gst_v4l2_object_unlock_stop (self->v4l2output);
360 gst_v4l2_object_unlock_stop (self->v4l2capture);
361 gst_pad_stop_task (encoder->srcpad);
363 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
367 self->output_flow = GST_FLOW_OK;
369 gst_v4l2_object_unlock_stop (self->v4l2output);
370 gst_v4l2_object_unlock_stop (self->v4l2capture);
375 struct ProfileLevelCtx
377 GstV4l2VideoEnc *self;
378 const gchar *profile;
383 get_string_list (GstStructure * s, const gchar * field, GQueue * queue)
387 value = gst_structure_get_value (s, field);
392 if (GST_VALUE_HOLDS_LIST (value)) {
395 if (gst_value_list_get_size (value) == 0)
398 for (i = 0; i < gst_value_list_get_size (value); i++) {
399 const GValue *item = gst_value_list_get_value (value, i);
401 if (G_VALUE_HOLDS_STRING (item))
402 g_queue_push_tail (queue, g_value_dup_string (item));
404 } else if (G_VALUE_HOLDS_STRING (value)) {
405 g_queue_push_tail (queue, g_value_dup_string (value));
412 negotiate_profile_and_level (GstCapsFeatures * features, GstStructure * s,
415 struct ProfileLevelCtx *ctx = user_data;
416 GstV4l2VideoEncClass *klass = GST_V4L2_VIDEO_ENC_GET_CLASS (ctx->self);
417 GstV4l2Object *v4l2object = GST_V4L2_VIDEO_ENC (ctx->self)->v4l2output;
418 GQueue profiles = G_QUEUE_INIT;
419 GQueue levels = G_QUEUE_INIT;
420 gboolean failed = FALSE;
422 if (klass->profile_cid && get_string_list (s, "profile", &profiles)) {
425 for (l = profiles.head; l; l = l->next) {
426 struct v4l2_control control = { 0, };
428 const gchar *profile = l->data;
430 GST_TRACE_OBJECT (ctx->self, "Trying profile %s", profile);
432 control.id = klass->profile_cid;
433 control.value = v4l2_profile = klass->profile_from_string (profile);
435 if (control.value < 0)
438 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0) {
439 GST_WARNING_OBJECT (ctx->self, "Failed to set %s profile: '%s'",
440 klass->codec_name, g_strerror (errno));
444 profile = klass->profile_to_string (control.value);
446 if (control.value == v4l2_profile) {
447 ctx->profile = profile;
451 if (g_list_find_custom (l, profile, g_str_equal)) {
452 ctx->profile = profile;
457 if (profiles.length && !ctx->profile)
460 g_queue_foreach (&profiles, (GFunc) g_free, NULL);
461 g_queue_clear (&profiles);
464 if (!failed && klass->level_cid && get_string_list (s, "level", &levels)) {
467 for (l = levels.head; l; l = l->next) {
468 struct v4l2_control control = { 0, };
470 const gchar *level = l->data;
472 GST_TRACE_OBJECT (ctx->self, "Trying level %s", level);
474 control.id = klass->level_cid;
475 control.value = v4l2_level = klass->level_from_string (level);
477 if (control.value < 0)
480 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_S_CTRL, &control) < 0) {
481 GST_WARNING_OBJECT (ctx->self, "Failed to set %s level: '%s'",
482 klass->codec_name, g_strerror (errno));
486 level = klass->level_to_string (control.value);
488 if (control.value == v4l2_level) {
493 if (g_list_find_custom (l, level, g_str_equal)) {
499 if (levels.length && !ctx->level)
502 g_queue_foreach (&levels, (GFunc) g_free, NULL);
503 g_queue_clear (&levels);
506 /* If it failed, we continue */
511 gst_v4l2_video_enc_negotiate (GstVideoEncoder * encoder)
513 GstV4l2VideoEncClass *klass = GST_V4L2_VIDEO_ENC_GET_CLASS (encoder);
514 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
515 GstV4l2Object *v4l2object = self->v4l2output;
516 GstCaps *allowed_caps;
517 struct ProfileLevelCtx ctx = { self, NULL, NULL };
518 GstVideoCodecState *state;
521 GST_DEBUG_OBJECT (self, "Negotiating %s profile and level.",
524 /* Only renegotiate on upstream changes */
525 if (self->input_state)
528 allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
532 if (gst_caps_is_empty (allowed_caps))
535 allowed_caps = gst_caps_make_writable (allowed_caps);
537 /* negotiate_profile_and_level() will return TRUE on failure to keep
538 * iterating, if gst_caps_foreach() returns TRUE it means there was no
539 * compatible profile and level in any of the structure */
540 if (gst_caps_foreach (allowed_caps, negotiate_profile_and_level, &ctx)) {
541 goto no_profile_level;
545 if (klass->profile_cid && !ctx.profile) {
546 struct v4l2_control control = { 0, };
548 control.id = klass->profile_cid;
550 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
553 ctx.profile = klass->profile_to_string (control.value);
556 if (klass->level_cid && !ctx.level) {
557 struct v4l2_control control = { 0, };
559 control.id = klass->level_cid;
561 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) < 0)
564 ctx.level = klass->level_to_string (control.value);
567 GST_DEBUG_OBJECT (self, "Selected %s profile %s at level %s",
568 klass->codec_name, ctx.profile, ctx.level);
570 state = gst_video_encoder_get_output_state (encoder);
571 s = gst_caps_get_structure (state->caps, 0);
573 if (klass->profile_cid)
574 gst_structure_set (s, "profile", G_TYPE_STRING, ctx.profile, NULL);
576 if (klass->level_cid)
577 gst_structure_set (s, "level", G_TYPE_STRING, ctx.level, NULL);
579 if (!GST_VIDEO_ENCODER_CLASS (parent_class)->negotiate (encoder))
585 GST_WARNING_OBJECT (self, "Failed to get %s profile and level: '%s'",
586 klass->codec_name, g_strerror (errno));
590 GST_WARNING_OBJECT (self, "No compatible level and profile in caps: %"
591 GST_PTR_FORMAT, allowed_caps);
596 gst_caps_unref (allowed_caps);
600 static GstVideoCodecFrame *
601 gst_v4l2_video_enc_get_oldest_frame (GstVideoEncoder * encoder)
603 GstVideoCodecFrame *frame = NULL;
607 frames = gst_video_encoder_get_frames (encoder);
609 for (l = frames; l != NULL; l = l->next) {
610 GstVideoCodecFrame *f = l->data;
612 if (!frame || frame->pts > f->pts)
619 GST_LOG_OBJECT (encoder,
620 "Oldest frame is %d %" GST_TIME_FORMAT
621 " and %d frames left",
622 frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
623 gst_video_codec_frame_ref (frame);
626 g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
632 gst_v4l2_video_enc_loop (GstVideoEncoder * encoder)
634 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
635 GstVideoCodecFrame *frame;
636 GstBuffer *buffer = NULL;
639 GST_LOG_OBJECT (encoder, "Allocate output buffer");
641 buffer = gst_video_encoder_allocate_output_buffer (encoder,
642 self->v4l2capture->info.size);
644 if (NULL == buffer) {
645 ret = GST_FLOW_FLUSHING;
650 /* FIXME Check if buffer isn't the last one here */
652 GST_LOG_OBJECT (encoder, "Process output buffer");
654 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL
655 (self->v4l2capture->pool), &buffer);
657 if (ret != GST_FLOW_OK)
660 frame = gst_v4l2_video_enc_get_oldest_frame (encoder);
663 frame->output_buffer = buffer;
665 ret = gst_video_encoder_finish_frame (encoder, frame);
667 if (ret != GST_FLOW_OK)
670 GST_WARNING_OBJECT (encoder, "Encoder is producing too many buffers");
671 gst_buffer_unref (buffer);
677 GST_DEBUG_OBJECT (encoder, "Leaving output thread");
679 gst_buffer_replace (&buffer, NULL);
680 self->output_flow = ret;
681 g_atomic_int_set (&self->processing, FALSE);
682 gst_v4l2_object_unlock (self->v4l2output);
683 gst_pad_pause_task (encoder->srcpad);
687 gst_v4l2_video_enc_loop_stopped (GstV4l2VideoEnc * self)
689 if (g_atomic_int_get (&self->processing)) {
690 GST_DEBUG_OBJECT (self, "Early stop of encoding thread");
691 self->output_flow = GST_FLOW_FLUSHING;
692 g_atomic_int_set (&self->processing, FALSE);
695 GST_DEBUG_OBJECT (self, "Encoding task destroyed: %s",
696 gst_flow_get_name (self->output_flow));
701 gst_v4l2_video_enc_handle_frame (GstVideoEncoder * encoder,
702 GstVideoCodecFrame * frame)
704 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
705 GstFlowReturn ret = GST_FLOW_OK;
706 GstTaskState task_state;
708 GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
710 if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
713 task_state = gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self));
714 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED) {
715 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
717 /* It possible that the processing thread stopped due to an error */
718 if (self->output_flow != GST_FLOW_OK &&
719 self->output_flow != GST_FLOW_FLUSHING) {
720 GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
721 ret = self->output_flow;
725 /* Ensure input internal pool is active */
726 if (!gst_buffer_pool_is_active (pool)) {
727 GstStructure *config = gst_buffer_pool_get_config (pool);
728 guint min = MAX (self->v4l2output->min_buffers, GST_V4L2_MIN_BUFFERS);
730 gst_buffer_pool_config_set_params (config, self->input_state->caps,
731 self->v4l2output->info.size, min, min);
733 /* There is no reason to refuse this config */
734 if (!gst_buffer_pool_set_config (pool, config))
735 goto activate_failed;
737 if (!gst_buffer_pool_set_active (pool, TRUE))
738 goto activate_failed;
741 if (!gst_buffer_pool_set_active
742 (GST_BUFFER_POOL (self->v4l2capture->pool), TRUE)) {
743 GST_WARNING_OBJECT (self, "Could not activate capture buffer pool.");
744 goto activate_failed;
747 GST_DEBUG_OBJECT (self, "Starting encoding thread");
749 /* Start the processing task, when it quits, the task will disable input
750 * processing to unlock input if draining, or prevent potential block */
751 if (!gst_pad_start_task (encoder->srcpad,
752 (GstTaskFunction) gst_v4l2_video_enc_loop, self,
753 (GDestroyNotify) gst_v4l2_video_enc_loop_stopped))
754 goto start_task_failed;
757 if (frame->input_buffer) {
758 GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
760 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL
761 (self->v4l2output->pool), &frame->input_buffer);
762 GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
764 if (ret == GST_FLOW_FLUSHING) {
765 if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) !=
767 ret = self->output_flow;
769 } else if (ret != GST_FLOW_OK) {
774 gst_video_codec_frame_unref (frame);
780 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
781 (_("Failed to allocate required memory.")),
782 ("Buffer pool activation failed"));
783 return GST_FLOW_ERROR;
788 ret = GST_FLOW_FLUSHING;
793 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
794 (_("Failed to start encoding thread.")), (NULL));
795 g_atomic_int_set (&self->processing, FALSE);
796 ret = GST_FLOW_ERROR;
801 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
802 (_("Failed to process frame.")),
803 ("Maybe be due to not enough memory or failing driver"));
804 ret = GST_FLOW_ERROR;
809 gst_video_encoder_finish_frame (encoder, frame);
815 gst_v4l2_video_enc_decide_allocation (GstVideoEncoder *
816 encoder, GstQuery * query)
818 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
819 GstVideoCodecState *state = gst_video_encoder_get_output_state (encoder);
821 GstV4l2Error error = GST_V4L2_ERROR_INIT;
822 GstClockTime latency;
823 gboolean ret = FALSE;
825 /* We need to set the format here, since this is called right after
826 * GstVideoEncoder have set the width, height and framerate into the state
827 * caps. These are needed by the driver to calculate the buffer size and to
828 * implement bitrate adaptation. */
829 caps = gst_caps_copy (state->caps);
830 gst_structure_remove_field (gst_caps_get_structure (caps, 0), "colorimetry");
831 if (!gst_v4l2_object_set_format (self->v4l2capture, caps, &error)) {
832 gst_v4l2_error (self, &error);
833 gst_caps_unref (caps);
837 gst_caps_unref (caps);
839 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query)) {
840 GstVideoEncoderClass *enc_class = GST_VIDEO_ENCODER_CLASS (parent_class);
841 ret = enc_class->decide_allocation (encoder, query);
844 /* FIXME This may not be entirely correct, as encoder may keep some
845 * observation withouth delaying the encoding. Linux Media API need some
846 * more work to explicitly expressed the decoder / encoder latency. This
847 * value will then become max latency, and the reported driver latency would
848 * become the min latency. */
849 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
850 gst_video_encoder_set_latency (encoder, latency, latency);
853 gst_video_codec_state_unref (state);
858 gst_v4l2_video_enc_propose_allocation (GstVideoEncoder *
859 encoder, GstQuery * query)
861 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
862 gboolean ret = FALSE;
864 GST_DEBUG_OBJECT (self, "called");
869 ret = gst_v4l2_object_propose_allocation (self->v4l2output, query);
872 ret = GST_VIDEO_ENCODER_CLASS (parent_class)->propose_allocation (encoder,
879 gst_v4l2_video_enc_src_query (GstVideoEncoder * encoder, GstQuery * query)
882 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
883 switch (GST_QUERY_TYPE (query)) {
884 case GST_QUERY_CAPS:{
885 GstCaps *filter, *result = NULL;
886 GstPad *pad = GST_VIDEO_ENCODER_SRC_PAD (encoder);
888 gst_query_parse_caps (query, &filter);
890 /* FIXME Try and not probe the entire encoder, but only the implement
892 if (self->probed_srccaps) {
893 GstCaps *tmpl = gst_pad_get_pad_template_caps (pad);
894 result = gst_caps_intersect (tmpl, self->probed_srccaps);
895 gst_caps_unref (tmpl);
897 result = gst_pad_get_pad_template_caps (pad);
900 GstCaps *tmp = result;
902 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
903 gst_caps_unref (tmp);
906 GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
908 gst_query_set_caps_result (query, result);
909 gst_caps_unref (result);
914 ret = GST_VIDEO_ENCODER_CLASS (parent_class)->src_query (encoder, query);
922 gst_v4l2_video_enc_sink_query (GstVideoEncoder * encoder, GstQuery * query)
925 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
927 switch (GST_QUERY_TYPE (query)) {
928 case GST_QUERY_CAPS:{
929 GstCaps *filter, *result = NULL;
930 GstPad *pad = GST_VIDEO_ENCODER_SINK_PAD (encoder);
932 gst_query_parse_caps (query, &filter);
934 if (self->probed_sinkcaps)
935 result = gst_caps_ref (self->probed_sinkcaps);
937 result = gst_pad_get_pad_template_caps (pad);
940 GstCaps *tmp = result;
942 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
943 gst_caps_unref (tmp);
946 GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
948 gst_query_set_caps_result (query, result);
949 gst_caps_unref (result);
954 ret = GST_VIDEO_ENCODER_CLASS (parent_class)->sink_query (encoder, query);
962 gst_v4l2_video_enc_sink_event (GstVideoEncoder * encoder, GstEvent * event)
964 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
967 switch (GST_EVENT_TYPE (event)) {
968 case GST_EVENT_FLUSH_START:
969 GST_DEBUG_OBJECT (self, "flush start");
970 gst_v4l2_object_unlock (self->v4l2output);
971 gst_v4l2_object_unlock (self->v4l2capture);
977 ret = GST_VIDEO_ENCODER_CLASS (parent_class)->sink_event (encoder, event);
979 switch (GST_EVENT_TYPE (event)) {
980 case GST_EVENT_FLUSH_START:
981 gst_pad_stop_task (encoder->srcpad);
982 GST_DEBUG_OBJECT (self, "flush start done");
990 static GstStateChangeReturn
991 gst_v4l2_video_enc_change_state (GstElement * element,
992 GstStateChange transition)
994 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (element);
996 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
997 g_atomic_int_set (&self->active, FALSE);
998 gst_v4l2_object_unlock (self->v4l2output);
999 gst_v4l2_object_unlock (self->v4l2capture);
1002 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1007 gst_v4l2_video_enc_dispose (GObject * object)
1009 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (object);
1011 gst_caps_replace (&self->probed_sinkcaps, NULL);
1012 gst_caps_replace (&self->probed_srccaps, NULL);
1014 G_OBJECT_CLASS (parent_class)->dispose (object);
1018 gst_v4l2_video_enc_finalize (GObject * object)
1020 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (object);
1022 gst_v4l2_object_destroy (self->v4l2capture);
1023 gst_v4l2_object_destroy (self->v4l2output);
1025 G_OBJECT_CLASS (parent_class)->finalize (object);
1030 gst_v4l2_video_enc_init (GstV4l2VideoEnc * self)
1032 /* V4L2 object are created in subinstance_init */
1036 gst_v4l2_video_enc_subinstance_init (GTypeInstance * instance, gpointer g_class)
1038 GstV4l2VideoEncClass *klass = GST_V4L2_VIDEO_ENC_CLASS (g_class);
1039 GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (instance);
1041 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
1042 GST_OBJECT (GST_VIDEO_ENCODER_SINK_PAD (self)),
1043 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1044 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
1045 self->v4l2output->no_initial_format = TRUE;
1046 self->v4l2output->keep_aspect = FALSE;
1048 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
1049 GST_OBJECT (GST_VIDEO_ENCODER_SRC_PAD (self)),
1050 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1051 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
1055 gst_v4l2_video_enc_class_init (GstV4l2VideoEncClass * klass)
1057 GstElementClass *element_class;
1058 GObjectClass *gobject_class;
1059 GstVideoEncoderClass *video_encoder_class;
1061 parent_class = g_type_class_peek_parent (klass);
1063 element_class = (GstElementClass *) klass;
1064 gobject_class = (GObjectClass *) klass;
1065 video_encoder_class = (GstVideoEncoderClass *) klass;
1067 GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_enc_debug, "v4l2videoenc", 0,
1068 "V4L2 Video Encoder");
1070 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_dispose);
1071 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_finalize);
1072 gobject_class->set_property =
1073 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_set_property);
1074 gobject_class->get_property =
1075 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_get_property);
1077 video_encoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_open);
1078 video_encoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_close);
1079 video_encoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_start);
1080 video_encoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_stop);
1081 video_encoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_finish);
1082 video_encoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_flush);
1083 video_encoder_class->set_format =
1084 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_set_format);
1085 video_encoder_class->negotiate =
1086 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_negotiate);
1087 video_encoder_class->decide_allocation =
1088 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_decide_allocation);
1089 video_encoder_class->propose_allocation =
1090 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_propose_allocation);
1091 video_encoder_class->sink_query =
1092 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_sink_query);
1093 video_encoder_class->src_query =
1094 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_src_query);
1095 video_encoder_class->sink_event =
1096 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_sink_event);
1097 video_encoder_class->handle_frame =
1098 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_handle_frame);
1100 element_class->change_state =
1101 GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_change_state);
1103 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
1107 gst_v4l2_video_enc_subclass_init (gpointer g_class, gpointer data)
1109 GstV4l2VideoEncClass *klass = GST_V4L2_VIDEO_ENC_CLASS (g_class);
1110 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1111 GstV4l2VideoEncCData *cdata = data;
1113 klass->default_device = cdata->device;
1115 /* Note: gst_pad_template_new() take the floating ref from the caps */
1116 gst_element_class_add_pad_template (element_class,
1117 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1119 gst_element_class_add_pad_template (element_class,
1120 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1123 gst_caps_unref (cdata->sink_caps);
1124 gst_caps_unref (cdata->src_caps);
1128 /* Probing functions */
1130 gst_v4l2_is_video_enc (GstCaps * sink_caps, GstCaps * src_caps,
1131 GstCaps * codec_caps)
1133 gboolean ret = FALSE;
1134 gboolean (*check_caps) (const GstCaps *, const GstCaps *);
1137 check_caps = gst_caps_can_intersect;
1139 codec_caps = gst_v4l2_object_get_codec_caps ();
1140 check_caps = gst_caps_is_subset;
1143 if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_raw_caps ())
1144 && check_caps (src_caps, codec_caps))
1151 gst_v4l2_video_enc_register (GstPlugin * plugin, GType type,
1152 const char *codec, const gchar * basename, const gchar * device_path,
1153 GstCaps * sink_caps, GstCaps * codec_caps, GstCaps * src_caps)
1155 GstCaps *filtered_caps;
1156 GTypeQuery type_query;
1157 GTypeInfo type_info = { 0, };
1160 GstV4l2VideoEncCData *cdata;
1162 filtered_caps = gst_caps_intersect (src_caps, codec_caps);
1164 cdata = g_new0 (GstV4l2VideoEncCData, 1);
1165 cdata->device = g_strdup (device_path);
1166 cdata->sink_caps = gst_caps_ref (sink_caps);
1167 cdata->src_caps = gst_caps_ref (filtered_caps);
1169 g_type_query (type, &type_query);
1170 memset (&type_info, 0, sizeof (type_info));
1171 type_info.class_size = type_query.class_size;
1172 type_info.instance_size = type_query.instance_size;
1173 type_info.class_init = gst_v4l2_video_enc_subclass_init;
1174 type_info.class_data = cdata;
1175 type_info.instance_init = gst_v4l2_video_enc_subinstance_init;
1177 /* The first encoder to be registered should use a constant name, like
1178 * v4l2h264enc, for any additional encoders, we create unique names. Encoder
1179 * names may change between boots, so this should help gain stable names for
1180 * the most common use cases. */
1181 type_name = g_strdup_printf ("v4l2%senc", codec);
1183 if (g_type_from_name (type_name) != 0) {
1185 type_name = g_strdup_printf ("v4l2%s%senc", basename, codec);
1188 subtype = g_type_register_static (type, type_name, &type_info, 0);
1190 if (!gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1, subtype))
1191 GST_WARNING ("Failed to register plugin '%s'", type_name);