v4l2videoenc: Remove unused function
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2videoenc.c
1 /*
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>
6  *
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.
11  *
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.
16  *
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.
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <string.h>
33
34 #include "gstv4l2videoenc.h"
35 #include "v4l2_calls.h"
36
37 #include <string.h>
38 #include <gst/gst-i18n-plugin.h>
39
40 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_video_enc_debug);
41 #define GST_CAT_DEFAULT gst_v4l2_video_enc_debug
42
43 typedef struct
44 {
45   gchar *device;
46   GstCaps *sink_caps;
47   GstCaps *src_caps;
48 } GstV4l2VideoEncCData;
49
50 enum
51 {
52   PROP_0,
53   V4L2_STD_OBJECT_PROPS,
54 };
55
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);
59
60 static void
61 gst_v4l2_video_enc_set_property (GObject * object,
62     guint prop_id, const GValue * value, GParamSpec * pspec)
63 {
64   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (object);
65
66   switch (prop_id) {
67     case PROP_OUTPUT_IO_MODE:
68       gst_v4l2_object_set_property_helper (self->v4l2output,
69           prop_id, value, pspec);
70       break;
71     case PROP_CAPTURE_IO_MODE:
72       gst_v4l2_object_set_property_helper (self->v4l2capture,
73           prop_id, value, pspec);
74       break;
75
76       /* By default, only set on output */
77     default:
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);
81       }
82       break;
83   }
84 }
85
86 static void
87 gst_v4l2_video_enc_get_property (GObject * object,
88     guint prop_id, GValue * value, GParamSpec * pspec)
89 {
90   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (object);
91
92   switch (prop_id) {
93     case PROP_OUTPUT_IO_MODE:
94       gst_v4l2_object_get_property_helper (self->v4l2output,
95           prop_id, value, pspec);
96       break;
97     case PROP_CAPTURE_IO_MODE:
98       gst_v4l2_object_get_property_helper (self->v4l2output,
99           PROP_IO_MODE, value, pspec);
100       break;
101
102       /* By default read from output */
103     default:
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);
107       }
108       break;
109   }
110 }
111
112 static gboolean
113 gst_v4l2_video_enc_open (GstVideoEncoder * encoder)
114 {
115   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
116
117   GST_DEBUG_OBJECT (self, "Opening");
118
119   if (!gst_v4l2_object_open (self->v4l2output))
120     goto failure;
121
122   if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
123     goto failure;
124
125   self->probed_sinkcaps = gst_v4l2_object_get_caps (self->v4l2output,
126       gst_v4l2_object_get_raw_caps ());
127
128   if (gst_caps_is_empty (self->probed_sinkcaps))
129     goto no_raw_format;
130
131   self->probed_srccaps = gst_v4l2_object_get_caps (self->v4l2capture,
132       gst_v4l2_object_get_codec_caps ());
133
134   if (gst_caps_is_empty (self->probed_srccaps))
135     goto no_encoded_format;
136
137   return TRUE;
138
139 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));
143   goto failure;
144
145
146 no_raw_format:
147   GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
148       (_("Encoder on device %s has no supported input format"),
149           self->v4l2output->videodev), (NULL));
150   goto failure;
151
152 failure:
153   if (GST_V4L2_IS_OPEN (self->v4l2output))
154     gst_v4l2_object_close (self->v4l2output);
155
156   if (GST_V4L2_IS_OPEN (self->v4l2capture))
157     gst_v4l2_object_close (self->v4l2capture);
158
159   gst_caps_replace (&self->probed_srccaps, NULL);
160   gst_caps_replace (&self->probed_sinkcaps, NULL);
161
162   return FALSE;
163 }
164
165 static gboolean
166 gst_v4l2_video_enc_close (GstVideoEncoder * encoder)
167 {
168   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
169
170   GST_DEBUG_OBJECT (self, "Closing");
171
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);
176
177   return TRUE;
178 }
179
180 static gboolean
181 gst_v4l2_video_enc_start (GstVideoEncoder * encoder)
182 {
183   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
184
185   GST_DEBUG_OBJECT (self, "Starting");
186
187   gst_v4l2_object_unlock (self->v4l2output);
188   g_atomic_int_set (&self->active, TRUE);
189   self->output_flow = GST_FLOW_OK;
190
191   return TRUE;
192 }
193
194 static gboolean
195 gst_v4l2_video_enc_stop (GstVideoEncoder * encoder)
196 {
197   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
198
199   GST_DEBUG_OBJECT (self, "Stopping");
200
201   gst_v4l2_object_unlock (self->v4l2output);
202   gst_v4l2_object_unlock (self->v4l2capture);
203
204   /* Wait for capture thread to stop */
205   gst_pad_stop_task (encoder->srcpad);
206
207   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
208   self->output_flow = GST_FLOW_OK;
209   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
210
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);
214
215   gst_v4l2_object_stop (self->v4l2output);
216   gst_v4l2_object_stop (self->v4l2capture);
217
218   if (self->input_state) {
219     gst_video_codec_state_unref (self->input_state);
220     self->input_state = NULL;
221   }
222
223   GST_DEBUG_OBJECT (self, "Stopped");
224
225   return TRUE;
226 }
227
228 static gboolean
229 gst_v4l2_encoder_cmd (GstV4l2Object * v4l2object, guint cmd, guint flags)
230 {
231   struct v4l2_encoder_cmd ecmd = { 0, };
232
233   GST_DEBUG_OBJECT (v4l2object->element,
234       "sending v4l2 encoder command %u with flags %u", cmd, flags);
235
236   if (!GST_V4L2_IS_OPEN (v4l2object))
237     return FALSE;
238
239   ecmd.cmd = cmd;
240   ecmd.flags = flags;
241   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENCODER_CMD, &ecmd) < 0)
242     goto ecmd_failed;
243
244   return TRUE;
245
246 ecmd_failed:
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));
251   } else {
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));
255   }
256   return FALSE;
257 }
258
259 static GstFlowReturn
260 gst_v4l2_video_enc_finish (GstVideoEncoder * encoder)
261 {
262   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
263   GstFlowReturn ret = GST_FLOW_OK;
264
265   if (gst_pad_get_task_state (encoder->srcpad) != GST_TASK_STARTED)
266     goto done;
267
268   GST_DEBUG_OBJECT (self, "Finishing encoding");
269
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);
273
274   if (gst_v4l2_encoder_cmd (self->v4l2capture, V4L2_ENC_CMD_STOP, 0)) {
275     GstTask *task = encoder->srcpad->task;
276
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;
283   }
284
285   /* and ensure the processing thread has stopped in case another error
286    * occured. */
287   gst_v4l2_object_unlock (self->v4l2capture);
288   gst_pad_stop_task (encoder->srcpad);
289   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
290
291   if (ret == GST_FLOW_FLUSHING)
292     ret = self->output_flow;
293
294   GST_DEBUG_OBJECT (encoder, "Done draining buffers");
295
296 done:
297   return ret;
298 }
299
300 static gboolean
301 gst_v4l2_video_enc_set_format (GstVideoEncoder * encoder,
302     GstVideoCodecState * state)
303 {
304   gboolean ret = TRUE;
305   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
306   GstV4l2Error error = GST_V4L2_ERROR_INIT;
307   GstCaps *outcaps;
308
309   GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
310
311   if (self->input_state) {
312     if (gst_v4l2_object_caps_equal (self->v4l2output, state->caps)) {
313       GST_DEBUG_OBJECT (self, "Compatible caps");
314       return TRUE;
315     }
316
317     if (gst_v4l2_video_enc_finish (encoder) != GST_FLOW_OK)
318       return FALSE;
319
320     gst_video_codec_state_unref (self->input_state);
321     self->input_state = NULL;
322   }
323
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);
327
328   if (!gst_video_encoder_negotiate (encoder))
329     return FALSE;
330
331   if (!gst_v4l2_object_set_format (self->v4l2output, state->caps, &error)) {
332     gst_v4l2_error (self, &error);
333     return FALSE;
334   }
335
336   self->input_state = gst_video_codec_state_ref (state);
337
338   GST_DEBUG_OBJECT (self, "output caps: %" GST_PTR_FORMAT, state->caps);
339
340   return ret;
341 }
342
343 static gboolean
344 gst_v4l2_video_enc_flush (GstVideoEncoder * encoder)
345 {
346   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
347
348   GST_DEBUG_OBJECT (self, "Flushing");
349
350   /* Ensure the processing thread has stopped for the reverse playback
351    * iscount case */
352   if (g_atomic_int_get (&self->processing)) {
353     GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
354
355     gst_v4l2_object_unlock_stop (self->v4l2output);
356     gst_v4l2_object_unlock_stop (self->v4l2capture);
357     gst_pad_stop_task (encoder->srcpad);
358
359     GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
360
361   }
362
363   self->output_flow = GST_FLOW_OK;
364
365   gst_v4l2_object_unlock_stop (self->v4l2output);
366   gst_v4l2_object_unlock_stop (self->v4l2capture);
367
368   return TRUE;
369 }
370
371 static gboolean
372 gst_v4l2_video_enc_negotiate (GstVideoEncoder * encoder)
373 {
374   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
375   gboolean ret;
376
377   ret = GST_VIDEO_ENCODER_CLASS (parent_class)->negotiate (encoder);
378
379   if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
380           TRUE)) {
381     GST_WARNING_OBJECT (self, "Could not activate capture buffer pool.");
382     ret = FALSE;
383   }
384
385   return ret;
386 }
387
388 static GstVideoCodecFrame *
389 gst_v4l2_video_enc_get_oldest_frame (GstVideoEncoder * encoder)
390 {
391   GstVideoCodecFrame *frame = NULL;
392   GList *frames, *l;
393   gint count = 0;
394
395   frames = gst_video_encoder_get_frames (encoder);
396
397   for (l = frames; l != NULL; l = l->next) {
398     GstVideoCodecFrame *f = l->data;
399
400     if (!frame || frame->pts > f->pts)
401       frame = f;
402
403     count++;
404   }
405
406   if (frame) {
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);
412   }
413
414   g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
415
416   return frame;
417 }
418
419 static void
420 gst_v4l2_video_enc_loop (GstVideoEncoder * encoder)
421 {
422   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
423   GstVideoCodecFrame *frame;
424   GstBuffer *buffer = NULL;
425   GstFlowReturn ret;
426
427   GST_LOG_OBJECT (encoder, "Allocate output buffer");
428
429   buffer = gst_video_encoder_allocate_output_buffer (encoder,
430       self->v4l2capture->info.size);
431
432   if (NULL == buffer) {
433     ret = GST_FLOW_FLUSHING;
434     goto beach;
435   }
436
437
438   /* FIXME Check if buffer isn't the last one here */
439
440   GST_LOG_OBJECT (encoder, "Process output buffer");
441   ret =
442       gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL
443       (self->v4l2capture->pool), &buffer);
444
445   if (ret != GST_FLOW_OK)
446     goto beach;
447
448   frame = gst_v4l2_video_enc_get_oldest_frame (encoder);
449
450   if (frame) {
451     frame->output_buffer = buffer;
452     buffer = NULL;
453     ret = gst_video_encoder_finish_frame (encoder, frame);
454
455     if (ret != GST_FLOW_OK)
456       goto beach;
457   } else {
458     GST_WARNING_OBJECT (encoder, "Encoder is producing too many buffers");
459     gst_buffer_unref (buffer);
460   }
461
462   return;
463
464 beach:
465   GST_DEBUG_OBJECT (encoder, "Leaving output thread");
466
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);
472 }
473
474 static void
475 gst_v4l2_video_enc_loop_stopped (GstV4l2VideoEnc * self)
476 {
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);
481   }
482
483   GST_DEBUG_OBJECT (self, "Encoding task destroyed: %s",
484       gst_flow_get_name (self->output_flow));
485
486 }
487
488 static GstFlowReturn
489 gst_v4l2_video_enc_handle_frame (GstVideoEncoder * encoder,
490     GstVideoCodecFrame * frame)
491 {
492   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
493   GstFlowReturn ret = GST_FLOW_OK;
494
495   GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
496
497   if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
498     goto flushing;
499
500   if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) ==
501       GST_TASK_STOPPED) {
502     GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
503
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;
509       goto drop;
510     }
511
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);
518
519       /* There is no reason to refuse this config */
520       if (!gst_buffer_pool_set_config (pool, config))
521         goto activate_failed;
522
523       if (!gst_buffer_pool_set_active (pool, TRUE))
524         goto activate_failed;
525     }
526
527     GST_DEBUG_OBJECT (self, "Starting encoding thread");
528
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;
535   }
536
537   if (frame->input_buffer) {
538     GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
539     ret =
540         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL
541         (self->v4l2output->pool), &frame->input_buffer);
542     GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
543
544     if (ret == GST_FLOW_FLUSHING) {
545       if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) !=
546           GST_TASK_STARTED)
547         ret = self->output_flow;
548       goto drop;
549     } else if (ret != GST_FLOW_OK) {
550       goto process_failed;
551     }
552   }
553
554   gst_video_codec_frame_unref (frame);
555   return ret;
556
557   /* ERRORS */
558 activate_failed:
559   {
560     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
561         (_("Failed to allocate required memory.")),
562         ("Buffer pool activation failed"));
563     return GST_FLOW_ERROR;
564
565   }
566 flushing:
567   {
568     ret = GST_FLOW_FLUSHING;
569     goto drop;
570   }
571 start_task_failed:
572   {
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;
577     goto drop;
578   }
579 process_failed:
580   {
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;
585     goto drop;
586   }
587 drop:
588   {
589     gst_video_encoder_finish_frame (encoder, frame);
590     return ret;
591   }
592 }
593
594 static gboolean
595 gst_v4l2_video_enc_decide_allocation (GstVideoEncoder *
596     encoder, GstQuery * query)
597 {
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;
603
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);
610     ret = FALSE;
611     goto done;
612   }
613
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);
617   }
618
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);
626
627 done:
628   gst_video_codec_state_unref (state);
629   return ret;
630 }
631
632 static gboolean
633 gst_v4l2_video_enc_propose_allocation (GstVideoEncoder *
634     encoder, GstQuery * query)
635 {
636   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
637   gboolean ret = FALSE;
638
639   GST_DEBUG_OBJECT (self, "called");
640
641   if (query == NULL)
642     ret = TRUE;
643   else
644     ret = gst_v4l2_object_propose_allocation (self->v4l2output, query);
645
646   if (ret)
647     ret = GST_VIDEO_ENCODER_CLASS (parent_class)->propose_allocation (encoder,
648         query);
649
650   return ret;
651 }
652
653 static gboolean
654 gst_v4l2_video_enc_src_query (GstVideoEncoder * encoder, GstQuery * query)
655 {
656   gboolean ret = TRUE;
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);
662
663       gst_query_parse_caps (query, &filter);
664
665       /* FIXME Try and not probe the entire encoder, but only the implement
666        * subclass format */
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);
671       } else
672         result = gst_pad_get_pad_template_caps (pad);
673
674       if (filter) {
675         GstCaps *tmp = result;
676         result =
677             gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
678         gst_caps_unref (tmp);
679       }
680
681       GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
682
683       gst_query_set_caps_result (query, result);
684       gst_caps_unref (result);
685       break;
686     }
687
688     default:
689       ret = GST_VIDEO_ENCODER_CLASS (parent_class)->src_query (encoder, query);
690       break;
691   }
692
693   return ret;
694 }
695
696 static gboolean
697 gst_v4l2_video_enc_sink_query (GstVideoEncoder * encoder, GstQuery * query)
698 {
699   gboolean ret = TRUE;
700   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
701
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);
706
707       gst_query_parse_caps (query, &filter);
708
709       if (self->probed_sinkcaps)
710         result = gst_caps_ref (self->probed_sinkcaps);
711       else
712         result = gst_pad_get_pad_template_caps (pad);
713
714       if (filter) {
715         GstCaps *tmp = result;
716         result =
717             gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
718         gst_caps_unref (tmp);
719       }
720
721       GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
722
723       gst_query_set_caps_result (query, result);
724       gst_caps_unref (result);
725       break;
726     }
727
728     default:
729       ret = GST_VIDEO_ENCODER_CLASS (parent_class)->sink_query (encoder, query);
730       break;
731   }
732
733   return ret;
734 }
735
736 static gboolean
737 gst_v4l2_video_enc_sink_event (GstVideoEncoder * encoder, GstEvent * event)
738 {
739   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
740   gboolean ret;
741
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);
747       break;
748     default:
749       break;
750   }
751
752   ret = GST_VIDEO_ENCODER_CLASS (parent_class)->sink_event (encoder, event);
753
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");
758     default:
759       break;
760   }
761
762   return ret;
763 }
764
765 static GstStateChangeReturn
766 gst_v4l2_video_enc_change_state (GstElement * element,
767     GstStateChange transition)
768 {
769   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (element);
770
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);
775   }
776
777   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
778 }
779
780 static void
781 gst_v4l2_video_enc_dispose (GObject * object)
782 {
783   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (object);
784
785   gst_caps_replace (&self->probed_sinkcaps, NULL);
786   gst_caps_replace (&self->probed_srccaps, NULL);
787
788   G_OBJECT_CLASS (parent_class)->dispose (object);
789 }
790
791 static void
792 gst_v4l2_video_enc_finalize (GObject * object)
793 {
794   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (object);
795
796   gst_v4l2_object_destroy (self->v4l2capture);
797   gst_v4l2_object_destroy (self->v4l2output);
798
799   G_OBJECT_CLASS (parent_class)->finalize (object);
800 }
801
802 static void
803 gst_v4l2_video_enc_init (GstV4l2VideoEnc * self)
804 {
805   /* V4L2 object are created in subinstance_init */
806 }
807
808 static void
809 gst_v4l2_video_enc_subinstance_init (GTypeInstance * instance, gpointer g_class)
810 {
811   GstV4l2VideoEncClass *klass = GST_V4L2_VIDEO_ENC_CLASS (g_class);
812   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (instance);
813
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;
819
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;
825 }
826
827 static void
828 gst_v4l2_video_enc_class_init (GstV4l2VideoEncClass * klass)
829 {
830   GstElementClass *element_class;
831   GObjectClass *gobject_class;
832   GstVideoEncoderClass *video_encoder_class;
833
834   parent_class = g_type_class_peek_parent (klass);
835
836   element_class = (GstElementClass *) klass;
837   gobject_class = (GObjectClass *) klass;
838   video_encoder_class = (GstVideoEncoderClass *) klass;
839
840   GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_enc_debug, "v4l2videoenc", 0,
841       "V4L2 Video Encoder");
842
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);
849
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);
872
873   element_class->change_state =
874       GST_DEBUG_FUNCPTR (gst_v4l2_video_enc_change_state);
875
876   gst_v4l2_object_install_m2m_properties_helper (gobject_class);
877 }
878
879 static void
880 gst_v4l2_video_enc_subclass_init (gpointer g_class, gpointer data)
881 {
882   GstV4l2VideoEncClass *klass = GST_V4L2_VIDEO_ENC_CLASS (g_class);
883   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
884   GstV4l2VideoEncCData *cdata = data;
885
886   klass->default_device = cdata->device;
887
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,
891           cdata->sink_caps));
892   gst_element_class_add_pad_template (element_class,
893       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
894           cdata->src_caps));
895
896   g_free (cdata);
897 }
898
899 /* Probing functions */
900 gboolean
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)
904 {
905   GstCaps *filtered_caps;
906   GTypeQuery type_query;
907   GTypeInfo type_info = { 0, };
908   GType subtype;
909   gchar *type_name;
910   GstV4l2VideoEncCData *cdata;
911
912   filtered_caps = gst_caps_intersect (src_caps, codec_caps);
913
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);
918
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;
926
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);
932
933   if (g_type_from_name (type_name) != 0) {
934     g_free (type_name);
935     type_name = g_strdup_printf ("v4l2%s%senc", basename, codec);
936   }
937
938   subtype = g_type_register_static (type, type_name, &type_info, 0);
939   gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1, subtype);
940
941   g_free (type_name);
942
943   return TRUE;
944 }