v4l2videodec: Handle start_streaming error
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2videodec.c
1 /*
2  * Copyright (C) 2014 Collabora Ltd.
3  *     Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <string.h>
31
32 #include "gstv4l2videodec.h"
33 #include "v4l2_calls.h"
34
35 #include <string.h>
36 #include <gst/gst-i18n-plugin.h>
37
38 #define DEFAULT_PROP_DEVICE "/dev/video0"
39
40 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_video_dec_debug);
41 #define GST_CAT_DEFAULT gst_v4l2_video_dec_debug
42
43 static gboolean gst_v4l2_video_dec_flush (GstVideoDecoder * decoder);
44
45 typedef struct
46 {
47   gchar *device;
48   GstCaps *sink_caps;
49   GstCaps *src_caps;
50 } GstV4l2VideoDecCData;
51
52 enum
53 {
54   PROP_0,
55   V4L2_STD_OBJECT_PROPS,
56   PROP_CAPTURE_IO_MODE,
57 };
58
59 #define gst_v4l2_video_dec_parent_class parent_class
60 G_DEFINE_ABSTRACT_TYPE (GstV4l2VideoDec, gst_v4l2_video_dec,
61     GST_TYPE_VIDEO_DECODER);
62
63 static void
64 gst_v4l2_video_dec_set_property (GObject * object,
65     guint prop_id, const GValue * value, GParamSpec * pspec)
66 {
67   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
68
69   switch (prop_id) {
70       /* Split IO mode so output is configure through 'io-mode' and capture
71        * through 'capture-io-mode' */
72     case PROP_IO_MODE:
73       gst_v4l2_object_set_property_helper (self->v4l2output, prop_id, value,
74           pspec);
75       break;
76     case PROP_CAPTURE_IO_MODE:
77       gst_v4l2_object_set_property_helper (self->v4l2capture, PROP_IO_MODE,
78           value, pspec);
79       break;
80
81     case PROP_DEVICE:
82       gst_v4l2_object_set_property_helper (self->v4l2output, prop_id, value,
83           pspec);
84       gst_v4l2_object_set_property_helper (self->v4l2capture, prop_id, value,
85           pspec);
86       break;
87
88       /* By default, only set on output */
89     default:
90       if (!gst_v4l2_object_set_property_helper (self->v4l2output,
91               prop_id, value, pspec)) {
92         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93       }
94       break;
95   }
96 }
97
98 static void
99 gst_v4l2_video_dec_get_property (GObject * object,
100     guint prop_id, GValue * value, GParamSpec * pspec)
101 {
102   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
103
104   switch (prop_id) {
105     case PROP_IO_MODE:
106       gst_v4l2_object_get_property_helper (self->v4l2output, prop_id, value,
107           pspec);
108       break;
109     case PROP_CAPTURE_IO_MODE:
110       gst_v4l2_object_get_property_helper (self->v4l2output, PROP_IO_MODE,
111           value, pspec);
112       break;
113
114       /* By default read from output */
115     default:
116       if (!gst_v4l2_object_get_property_helper (self->v4l2output,
117               prop_id, value, pspec)) {
118         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119       }
120       break;
121   }
122 }
123
124 static gboolean
125 gst_v4l2_video_dec_open (GstVideoDecoder * decoder)
126 {
127   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
128
129   GST_DEBUG_OBJECT (self, "Opening");
130
131   if (!gst_v4l2_object_open (self->v4l2output))
132     goto failure;
133
134   if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
135     goto failure;
136
137   self->probed_sinkcaps = gst_v4l2_object_get_caps (self->v4l2output,
138       gst_v4l2_object_get_codec_caps ());
139
140   if (gst_caps_is_empty (self->probed_sinkcaps))
141     goto no_encoded_format;
142
143   self->probed_srccaps = gst_v4l2_object_get_caps (self->v4l2capture,
144       gst_v4l2_object_get_raw_caps ());
145
146   if (gst_caps_is_empty (self->probed_srccaps))
147     goto no_raw_format;
148
149   return TRUE;
150
151 no_encoded_format:
152   GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
153       (_("Encoder on device %s has no supported input format"),
154           self->v4l2output->videodev), (NULL));
155   goto failure;
156
157
158 no_raw_format:
159   GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
160       (_("Encoder on device %s has no supported output format"),
161           self->v4l2output->videodev), (NULL));
162   goto failure;
163
164 failure:
165   if (GST_V4L2_IS_OPEN (self->v4l2output))
166     gst_v4l2_object_close (self->v4l2output);
167
168   if (GST_V4L2_IS_OPEN (self->v4l2capture))
169     gst_v4l2_object_close (self->v4l2capture);
170
171   gst_caps_replace (&self->probed_srccaps, NULL);
172   gst_caps_replace (&self->probed_sinkcaps, NULL);
173
174   return FALSE;
175 }
176
177 static gboolean
178 gst_v4l2_video_dec_close (GstVideoDecoder * decoder)
179 {
180   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
181
182   GST_DEBUG_OBJECT (self, "Closing");
183
184   gst_v4l2_object_close (self->v4l2output);
185   gst_v4l2_object_close (self->v4l2capture);
186   gst_caps_replace (&self->probed_srccaps, NULL);
187   gst_caps_replace (&self->probed_sinkcaps, NULL);
188
189   return TRUE;
190 }
191
192 static gboolean
193 gst_v4l2_video_dec_start (GstVideoDecoder * decoder)
194 {
195   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
196
197   GST_DEBUG_OBJECT (self, "Starting");
198
199   gst_v4l2_object_unlock (self->v4l2output);
200   g_atomic_int_set (&self->active, TRUE);
201   self->output_flow = GST_FLOW_OK;
202
203   return TRUE;
204 }
205
206 static gboolean
207 gst_v4l2_video_dec_stop (GstVideoDecoder * decoder)
208 {
209   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
210
211   GST_DEBUG_OBJECT (self, "Stopping");
212
213   gst_v4l2_object_unlock (self->v4l2output);
214   gst_v4l2_object_unlock (self->v4l2capture);
215
216   /* Wait for capture thread to stop */
217   gst_pad_stop_task (decoder->srcpad);
218
219   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
220   self->output_flow = GST_FLOW_OK;
221   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
222
223   /* Should have been flushed already */
224   g_assert (g_atomic_int_get (&self->active) == FALSE);
225   g_assert (g_atomic_int_get (&self->processing) == FALSE);
226
227   gst_v4l2_object_stop (self->v4l2output);
228   gst_v4l2_object_stop (self->v4l2capture);
229
230   if (self->input_state) {
231     gst_video_codec_state_unref (self->input_state);
232     self->input_state = NULL;
233   }
234
235   GST_DEBUG_OBJECT (self, "Stopped");
236
237   return TRUE;
238 }
239
240 static gboolean
241 gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
242     GstVideoCodecState * state)
243 {
244   gboolean ret = TRUE;
245   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
246
247   GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
248
249   if (self->input_state) {
250     if (gst_v4l2_object_caps_equal (self->v4l2output, state->caps)) {
251       GST_DEBUG_OBJECT (self, "Compatible caps");
252       goto done;
253     }
254     gst_video_codec_state_unref (self->input_state);
255     self->input_state = NULL;
256
257     /* FIXME we probably need to do more work if pools are active */
258   }
259
260   ret = gst_v4l2_object_set_format (self->v4l2output, state->caps);
261
262   if (ret)
263     self->input_state = gst_video_codec_state_ref (state);
264
265 done:
266   return ret;
267 }
268
269 static gboolean
270 gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
271 {
272   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
273
274   GST_DEBUG_OBJECT (self, "Flushing");
275
276   /* Wait for capture thread to stop */
277   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
278   gst_pad_stop_task (decoder->srcpad);
279   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
280   self->output_flow = GST_FLOW_OK;
281
282   if (self->v4l2output->pool)
283     gst_v4l2_buffer_pool_stop_streaming (GST_V4L2_BUFFER_POOL
284         (self->v4l2output->pool));
285
286   if (self->v4l2capture->pool)
287     gst_v4l2_buffer_pool_stop_streaming (GST_V4L2_BUFFER_POOL
288         (self->v4l2capture->pool));
289
290   return TRUE;
291 }
292
293 static gboolean
294 gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
295 {
296   return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
297 }
298
299 static GstFlowReturn
300 gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
301 {
302   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
303   GstFlowReturn ret = GST_FLOW_OK;
304   GstBuffer *buffer;
305
306   if (!self->processing)
307     goto done;
308
309   GST_DEBUG_OBJECT (self, "Finishing decoding");
310
311   /* Keep queuing empty buffers until the processing thread has stopped,
312    * _pool_process() will return FLUSHING when that happened */
313   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
314   while (ret == GST_FLOW_OK) {
315     buffer = gst_buffer_new ();
316     ret =
317         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
318             v4l2output->pool), &buffer);
319     gst_buffer_unref (buffer);
320   }
321   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
322
323   /* Ensure the processing thread has stopped */
324   if (self->processing) {
325     gst_v4l2_object_unlock (self->v4l2capture);
326     gst_pad_stop_task (decoder->srcpad);
327     g_assert (g_atomic_int_get (&self->processing) == FALSE);
328   }
329
330   if (ret == GST_FLOW_FLUSHING)
331     ret = self->output_flow;
332
333   GST_DEBUG_OBJECT (decoder, "Done draining buffers");
334
335 done:
336   return ret;
337 }
338
339 static GstVideoCodecFrame *
340 gst_v4l2_video_dec_get_oldest_frame (GstVideoDecoder * decoder)
341 {
342   GstVideoCodecFrame *frame = NULL;
343   GList *frames, *l;
344   gint count = 0;
345
346   frames = gst_video_decoder_get_frames (decoder);
347
348   for (l = frames; l != NULL; l = l->next) {
349     GstVideoCodecFrame *f = l->data;
350
351     if (!frame || frame->pts > f->pts)
352       frame = f;
353
354     count++;
355   }
356
357   if (frame) {
358     GST_LOG_OBJECT (decoder,
359         "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
360         frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
361     gst_video_codec_frame_ref (frame);
362   }
363
364   g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
365
366   return frame;
367 }
368
369 static void
370 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
371 {
372   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
373   GstBufferPool *pool;
374   GstVideoCodecFrame *frame;
375   GstBuffer *buffer = NULL;
376   GstFlowReturn ret;
377
378   GST_LOG_OBJECT (decoder, "Allocate output buffer");
379
380   /* We cannot use the base class allotate helper since it taking the internal
381    * stream lock. we know that the acquire may need to poll until more frames
382    * comes in and holding this lock would prevent that.
383    */
384   pool = gst_video_decoder_get_buffer_pool (decoder);
385
386   /* Pool may be NULL if we started going to READY state */
387   if (pool == NULL) {
388     ret = GST_FLOW_FLUSHING;
389     goto beach;
390   }
391
392   ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
393   g_object_unref (pool);
394
395   if (ret != GST_FLOW_OK)
396     goto beach;
397
398   GST_LOG_OBJECT (decoder, "Process output buffer");
399   ret =
400       gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
401           v4l2capture->pool), &buffer);
402
403   if (ret != GST_FLOW_OK)
404     goto beach;
405
406   frame = gst_v4l2_video_dec_get_oldest_frame (decoder);
407
408   if (frame) {
409     frame->output_buffer = buffer;
410     buffer = NULL;
411     ret = gst_video_decoder_finish_frame (decoder, frame);
412
413     if (ret != GST_FLOW_OK)
414       goto beach;
415   } else {
416     GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
417     gst_buffer_unref (buffer);
418   }
419
420   return;
421
422 beach:
423   GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
424       gst_flow_get_name (ret));
425
426   gst_buffer_replace (&buffer, NULL);
427   self->output_flow = ret;
428   g_atomic_int_set (&self->processing, FALSE);
429   gst_v4l2_object_unlock (self->v4l2output);
430   gst_pad_pause_task (decoder->srcpad);
431 }
432
433 static GstFlowReturn
434 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
435     GstVideoCodecFrame * frame)
436 {
437   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
438   GstFlowReturn ret = GST_FLOW_OK;
439
440   GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
441
442   if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
443     goto flushing;
444
445   if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
446     if (!self->input_state)
447       goto not_negotiated;
448     if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps))
449       goto not_negotiated;
450   }
451
452   if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
453     GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
454     GstVideoInfo info;
455     GstVideoCodecState *output_state;
456     GstBuffer *codec_data;
457
458     GST_DEBUG_OBJECT (self, "Sending header");
459
460     codec_data = self->input_state->codec_data;
461
462     /* We are running in byte-stream mode, so we don't know the headers, but
463      * we need to send something, otherwise the decoder will refuse to
464      * intialize.
465      */
466     if (codec_data) {
467       gst_buffer_ref (codec_data);
468     } else {
469       codec_data = frame->input_buffer;
470       frame->input_buffer = NULL;
471     }
472
473     /* Ensure input internal pool is active */
474     if (!gst_buffer_pool_is_active (pool)) {
475       GstStructure *config = gst_buffer_pool_get_config (pool);
476       gst_buffer_pool_config_set_params (config, self->input_state->caps,
477           self->v4l2output->info.size, 2, 2);
478
479       /* There is no reason to refuse this config */
480       if (!gst_buffer_pool_set_config (pool, config))
481         goto activate_failed;
482
483       if (!gst_buffer_pool_set_active (pool, TRUE))
484         goto activate_failed;
485     }
486
487     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
488     gst_v4l2_object_unlock_stop (self->v4l2output);
489     ret =
490         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
491             v4l2output->pool), &codec_data);
492     gst_v4l2_object_unlock (self->v4l2output);
493     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
494
495     gst_buffer_unref (codec_data);
496
497     if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
498       goto not_negotiated;
499
500     output_state = gst_video_decoder_set_output_state (decoder,
501         info.finfo->format, info.width, info.height, self->input_state);
502
503     /* Copy the rest of the information, there might be more in the future */
504     output_state->info.interlace_mode = info.interlace_mode;
505     gst_video_codec_state_unref (output_state);
506
507     if (!gst_video_decoder_negotiate (decoder)) {
508       if (GST_PAD_IS_FLUSHING (decoder->srcpad))
509         goto flushing;
510       else
511         goto not_negotiated;
512     }
513
514     /* Ensure our internal pool is activated */
515     if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
516             TRUE))
517       goto activate_failed;
518   }
519
520   if (g_atomic_int_get (&self->processing) == FALSE) {
521     /* It possible that the processing thread stopped due to an error */
522     if (self->output_flow != GST_FLOW_OK) {
523       GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
524       ret = self->output_flow;
525       goto drop;
526     }
527
528     GST_DEBUG_OBJECT (self, "Starting decoding thread");
529
530     /* Enable processing input */
531     if (!gst_v4l2_buffer_pool_start_streaming (GST_V4L2_BUFFER_POOL
532             (self->v4l2capture->pool)))
533       goto start_streaming_failed;
534
535     gst_v4l2_object_unlock_stop (self->v4l2output);
536     gst_v4l2_object_unlock_stop (self->v4l2capture);
537
538     /* Start the processing task, when it quits, the task will disable input
539      * processing to unlock input if draining, or prevent potential block */
540     g_atomic_int_set (&self->processing, TRUE);
541     gst_pad_start_task (decoder->srcpad,
542         (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL);
543   }
544
545   if (frame->input_buffer) {
546     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
547     ret =
548         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
549             pool), &frame->input_buffer);
550     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
551
552     if (ret == GST_FLOW_FLUSHING) {
553       if (g_atomic_int_get (&self->processing) == FALSE)
554         ret = self->output_flow;
555     }
556
557     /* No need to keep input arround */
558     gst_buffer_replace (&frame->input_buffer, NULL);
559   }
560
561   gst_video_codec_frame_unref (frame);
562   return ret;
563
564   /* ERRORS */
565 not_negotiated:
566   {
567     GST_ERROR_OBJECT (self, "not negotiated");
568     ret = GST_FLOW_NOT_NEGOTIATED;
569     goto drop;
570   }
571 start_streaming_failed:
572   {
573     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
574         (_("Failed to re-enabled decoder.")),
575         ("Could not re-enqueue and start streaming on decide."));
576     return GST_FLOW_ERROR;
577   }
578 activate_failed:
579   {
580     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
581         (_("Failed to allocate required memory.")),
582         ("Buffer pool activation failed"));
583     return GST_FLOW_ERROR;
584   }
585 flushing:
586   {
587     ret = GST_FLOW_FLUSHING;
588     goto drop;
589   }
590 drop:
591   {
592     gst_video_decoder_drop_frame (decoder, frame);
593     return ret;
594   }
595 }
596
597 static gboolean
598 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
599     GstQuery * query)
600 {
601   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
602   GstClockTime latency;
603   gboolean ret = FALSE;
604
605   if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
606     ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
607         query);
608
609   latency = self->v4l2capture->min_buffers_for_capture *
610       self->v4l2capture->duration;
611   gst_video_decoder_set_latency (decoder, latency, latency);
612
613   return ret;
614 }
615
616 static gboolean
617 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
618 {
619   gboolean ret = TRUE;
620   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
621
622   switch (GST_QUERY_TYPE (query)) {
623     case GST_QUERY_CAPS:{
624       GstCaps *filter, *result = NULL;
625       GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
626
627       gst_query_parse_caps (query, &filter);
628
629       if (self->probed_srccaps)
630         result = gst_caps_ref (self->probed_srccaps);
631       else
632         result = gst_pad_get_pad_template_caps (pad);
633
634       if (filter) {
635         GstCaps *tmp = result;
636         result =
637             gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
638         gst_caps_unref (tmp);
639       }
640
641       GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
642
643       gst_query_set_caps_result (query, result);
644       gst_caps_unref (result);
645       break;
646     }
647
648     default:
649       ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
650       break;
651   }
652
653   return ret;
654 }
655
656 static gboolean
657 gst_v4l2_video_dec_sink_query (GstVideoDecoder * decoder, GstQuery * query)
658 {
659   gboolean ret = TRUE;
660   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
661
662   switch (GST_QUERY_TYPE (query)) {
663     case GST_QUERY_CAPS:{
664       GstCaps *filter, *result = NULL;
665       GstPad *pad = GST_VIDEO_DECODER_SINK_PAD (decoder);
666       gst_query_parse_caps (query, &filter);
667
668       if (self->probed_sinkcaps)
669         result = gst_caps_ref (self->probed_sinkcaps);
670       else
671         result = gst_pad_get_pad_template_caps (pad);
672
673       if (filter) {
674         GstCaps *tmp = result;
675         result =
676             gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
677         gst_caps_unref (tmp);
678       }
679
680       GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
681
682       gst_query_set_caps_result (query, result);
683       gst_caps_unref (result);
684       break;
685     }
686
687     default:
688       ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_query (decoder, query);
689       break;
690   }
691
692   return ret;
693 }
694
695 static gboolean
696 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
697 {
698   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
699
700   switch (GST_EVENT_TYPE (event)) {
701     case GST_EVENT_FLUSH_START:
702       gst_v4l2_object_unlock (self->v4l2output);
703       gst_v4l2_object_unlock (self->v4l2capture);
704     default:
705       break;
706   }
707
708   return GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
709 }
710
711 static GstStateChangeReturn
712 gst_v4l2_video_dec_change_state (GstElement * element,
713     GstStateChange transition)
714 {
715   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
716
717   if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
718     g_atomic_int_set (&self->active, FALSE);
719     gst_v4l2_object_unlock (self->v4l2output);
720     gst_v4l2_object_unlock (self->v4l2capture);
721   }
722
723   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
724 }
725
726 static void
727 gst_v4l2_video_dec_dispose (GObject * object)
728 {
729   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
730
731   gst_caps_replace (&self->probed_sinkcaps, NULL);
732   gst_caps_replace (&self->probed_srccaps, NULL);
733
734   G_OBJECT_CLASS (parent_class)->dispose (object);
735 }
736
737 static void
738 gst_v4l2_video_dec_finalize (GObject * object)
739 {
740   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
741
742   gst_v4l2_object_destroy (self->v4l2capture);
743   gst_v4l2_object_destroy (self->v4l2output);
744
745   G_OBJECT_CLASS (parent_class)->finalize (object);
746 }
747
748 static void
749 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
750 {
751   /* V4L2 object are created in subinstance_init */
752 }
753
754 static void
755 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
756 {
757   GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
758   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
759   GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
760
761   gst_video_decoder_set_packetized (decoder, TRUE);
762
763   self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
764       V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
765       gst_v4l2_get_output, gst_v4l2_set_output, NULL);
766   self->v4l2output->no_initial_format = TRUE;
767   self->v4l2output->keep_aspect = FALSE;
768
769   self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
770       V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
771       gst_v4l2_get_input, gst_v4l2_set_input, NULL);
772   self->v4l2capture->no_initial_format = TRUE;
773   self->v4l2output->keep_aspect = FALSE;
774
775   g_object_set (self, "device", klass->default_device, NULL);
776 }
777
778 static void
779 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
780 {
781   GstElementClass *element_class;
782   GObjectClass *gobject_class;
783   GstVideoDecoderClass *video_decoder_class;
784
785   parent_class = g_type_class_peek_parent (klass);
786
787   element_class = (GstElementClass *) klass;
788   gobject_class = (GObjectClass *) klass;
789   video_decoder_class = (GstVideoDecoderClass *) klass;
790
791   GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
792       "V4L2 Video Decoder");
793
794   gst_element_class_set_static_metadata (element_class,
795       "V4L2 Video Decoder",
796       "Codec/Decoder/Video",
797       "Decode video streams via V4L2 API",
798       "Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>");
799
800   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
801   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
802   gobject_class->set_property =
803       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
804   gobject_class->get_property =
805       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
806
807   video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
808   video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
809   video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
810   video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
811   video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
812   video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
813   video_decoder_class->set_format =
814       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
815   video_decoder_class->negotiate =
816       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
817   video_decoder_class->decide_allocation =
818       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
819   /* FIXME propose_allocation or not ? */
820   video_decoder_class->handle_frame =
821       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
822   video_decoder_class->sink_query =
823       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_query);
824   video_decoder_class->src_query =
825       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
826   video_decoder_class->sink_event =
827       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
828
829   element_class->change_state =
830       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
831
832   gst_v4l2_object_install_properties_helper (gobject_class,
833       DEFAULT_PROP_DEVICE);
834
835   /**
836    * GstV4l2VideoDec:capture-io-mode
837    *
838    * Capture IO Mode
839    */
840   g_object_class_install_property (gobject_class, PROP_CAPTURE_IO_MODE,
841       g_param_spec_enum ("capture-io-mode", "Capture IO mode",
842           "Capture I/O mode",
843           GST_TYPE_V4L2_IO_MODE, GST_V4L2_IO_AUTO,
844           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
845 }
846
847 static void
848 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
849 {
850   GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
851   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
852   GstV4l2VideoDecCData *cdata = data;
853
854   klass->default_device = cdata->device;
855
856   /* Note: gst_pad_template_new() take the floating ref from the caps */
857   gst_element_class_add_pad_template (element_class,
858       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
859           cdata->sink_caps));
860   gst_element_class_add_pad_template (element_class,
861       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
862           cdata->src_caps));
863
864   g_free (cdata);
865 }
866
867 /* Probing functions */
868 gboolean
869 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
870 {
871   gboolean ret = FALSE;
872
873   if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
874       && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
875     ret = TRUE;
876
877   return ret;
878 }
879
880 gboolean
881 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
882     const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
883 {
884   GTypeQuery type_query;
885   GTypeInfo type_info = { 0, };
886   GType type, subtype;
887   gchar *type_name;
888   GstV4l2VideoDecCData *cdata;
889
890   cdata = g_new0 (GstV4l2VideoDecCData, 1);
891   cdata->device = g_strdup (device_path);
892   cdata->sink_caps = gst_caps_ref (sink_caps);
893   cdata->src_caps = gst_caps_ref (src_caps);
894
895   type = gst_v4l2_video_dec_get_type ();
896   g_type_query (type, &type_query);
897   memset (&type_info, 0, sizeof (type_info));
898   type_info.class_size = type_query.class_size;
899   type_info.instance_size = type_query.instance_size;
900   type_info.class_init = gst_v4l2_video_dec_subclass_init;
901   type_info.class_data = cdata;
902   type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
903
904   type_name = g_strdup_printf ("v4l2%sdec", basename);
905   subtype = g_type_register_static (type, type_name, &type_info, 0);
906
907   gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1, subtype);
908
909   g_free (type_name);
910
911   return TRUE;
912 }