v4l2: Split flush in start/stop_streaming
[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   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
217   /* Wait for capture thread to stop */
218   gst_pad_stop_task (decoder->srcpad);
219   self->output_flow = GST_FLOW_OK;
220   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
221
222   /* Should have been flushed already */
223   g_assert (g_atomic_int_get (&self->active) == FALSE);
224   g_assert (g_atomic_int_get (&self->processing) == FALSE);
225
226   gst_v4l2_object_stop (self->v4l2output);
227   gst_v4l2_object_stop (self->v4l2capture);
228
229   if (self->input_state) {
230     gst_video_codec_state_unref (self->input_state);
231     self->input_state = NULL;
232   }
233
234   GST_DEBUG_OBJECT (self, "Stopped");
235
236   return TRUE;
237 }
238
239 static gboolean
240 gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
241     GstVideoCodecState * state)
242 {
243   gboolean ret = TRUE;
244   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
245
246   GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
247
248   if (self->input_state) {
249     if (gst_v4l2_object_caps_equal (self->v4l2output, state->caps)) {
250       GST_DEBUG_OBJECT (self, "Compatible caps");
251       goto done;
252     }
253     gst_video_codec_state_unref (self->input_state);
254     self->input_state = NULL;
255
256     /* FIXME we probably need to do more work if pools are active */
257   }
258
259   ret = gst_v4l2_object_set_format (self->v4l2output, state->caps);
260
261   if (ret)
262     self->input_state = gst_video_codec_state_ref (state);
263
264 done:
265   return ret;
266 }
267
268 static gboolean
269 gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
270 {
271   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
272
273   GST_DEBUG_OBJECT (self, "Flushing");
274
275   /* Wait for capture thread to stop */
276   gst_pad_stop_task (decoder->srcpad);
277   self->output_flow = GST_FLOW_OK;
278
279   if (self->v4l2output->pool)
280     gst_v4l2_buffer_pool_stop_streaming (GST_V4L2_BUFFER_POOL
281         (self->v4l2output->pool));
282
283   if (self->v4l2capture->pool)
284     gst_v4l2_buffer_pool_stop_streaming (GST_V4L2_BUFFER_POOL
285         (self->v4l2capture->pool));
286
287   return TRUE;
288 }
289
290 static gboolean
291 gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
292 {
293   return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
294 }
295
296 static GstFlowReturn
297 gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
298 {
299   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
300   GstFlowReturn ret = GST_FLOW_OK;
301   GstBuffer *buffer;
302
303   if (!self->processing)
304     goto done;
305
306   GST_DEBUG_OBJECT (self, "Finishing decoding");
307
308   /* Keep queuing empty buffers until the processing thread has stopped,
309    * _pool_process() will return FLUSHING when that happened */
310   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
311   while (ret == GST_FLOW_OK) {
312     buffer = gst_buffer_new ();
313     ret =
314         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
315             v4l2output->pool), &buffer);
316     gst_buffer_unref (buffer);
317   }
318   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
319
320   /* Ensure the processing thread has stopped */
321   if (self->processing) {
322     gst_v4l2_object_unlock (self->v4l2capture);
323     gst_pad_stop_task (decoder->srcpad);
324     g_assert (g_atomic_int_get (&self->processing) == FALSE);
325   }
326
327   if (ret == GST_FLOW_FLUSHING)
328     ret = self->output_flow;
329
330   GST_DEBUG_OBJECT (decoder, "Done draining buffers");
331
332 done:
333   return ret;
334 }
335
336 static GstVideoCodecFrame *
337 gst_v4l2_video_dec_get_oldest_frame (GstVideoDecoder * decoder)
338 {
339   GstVideoCodecFrame *frame = NULL;
340   GList *frames, *l;
341   gint count = 0;
342
343   frames = gst_video_decoder_get_frames (decoder);
344
345   for (l = frames; l != NULL; l = l->next) {
346     GstVideoCodecFrame *f = l->data;
347
348     if (!frame || frame->pts > f->pts)
349       frame = f;
350
351     count++;
352   }
353
354   if (frame) {
355     GST_LOG_OBJECT (decoder,
356         "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
357         frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
358     gst_video_codec_frame_ref (frame);
359   }
360
361   g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
362
363   return frame;
364 }
365
366 static void
367 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
368 {
369   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
370   GstBufferPool *pool;
371   GstVideoCodecFrame *frame;
372   GstBuffer *buffer = NULL;
373   GstFlowReturn ret;
374
375   GST_LOG_OBJECT (decoder, "Allocate output buffer");
376
377   /* We cannot use the base class allotate helper since it taking the internal
378    * stream lock. we know that the acquire may need to poll until more frames
379    * comes in and holding this lock would prevent that.
380    */
381   pool = gst_video_decoder_get_buffer_pool (decoder);
382
383   /* Pool may be NULL if we started going to READY state */
384   if (pool == NULL) {
385     ret = GST_FLOW_FLUSHING;
386     goto beach;
387   }
388
389   ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
390   g_object_unref (pool);
391
392   if (ret != GST_FLOW_OK)
393     goto beach;
394
395   GST_LOG_OBJECT (decoder, "Process output buffer");
396   ret =
397       gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
398           v4l2capture->pool), &buffer);
399
400   if (ret != GST_FLOW_OK)
401     goto beach;
402
403   frame = gst_v4l2_video_dec_get_oldest_frame (decoder);
404
405   if (frame) {
406     frame->output_buffer = buffer;
407     buffer = NULL;
408     ret = gst_video_decoder_finish_frame (decoder, frame);
409
410     if (ret != GST_FLOW_OK)
411       goto beach;
412   } else {
413     GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
414     gst_buffer_unref (buffer);
415   }
416
417   return;
418
419 beach:
420   GST_DEBUG_OBJECT (decoder, "Leaving output thread");
421
422   gst_buffer_replace (&buffer, NULL);
423   self->output_flow = ret;
424   g_atomic_int_set (&self->processing, FALSE);
425   gst_v4l2_object_unlock (self->v4l2output);
426   gst_pad_pause_task (decoder->srcpad);
427 }
428
429 static GstFlowReturn
430 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
431     GstVideoCodecFrame * frame)
432 {
433   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
434   GstFlowReturn ret = GST_FLOW_OK;
435
436   GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
437
438   if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
439     goto flushing;
440
441   if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
442     if (!self->input_state)
443       goto not_negotiated;
444     if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps))
445       goto not_negotiated;
446   }
447
448   if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
449     GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
450     GstVideoInfo info;
451     GstVideoCodecState *output_state;
452     GstBuffer *codec_data;
453
454     GST_DEBUG_OBJECT (self, "Sending header");
455
456     codec_data = self->input_state->codec_data;
457
458     /* We are running in byte-stream mode, so we don't know the headers, but
459      * we need to send something, otherwise the decoder will refuse to
460      * intialize.
461      */
462     if (codec_data) {
463       gst_buffer_ref (codec_data);
464     } else {
465       codec_data = frame->input_buffer;
466       frame->input_buffer = NULL;
467     }
468
469     /* Ensure input internal pool is active */
470     if (!gst_buffer_pool_is_active (pool)) {
471       GstStructure *config = gst_buffer_pool_get_config (pool);
472       gst_buffer_pool_config_set_params (config, self->input_state->caps,
473           self->v4l2output->info.size, 2, 2);
474
475       /* There is no reason to refuse this config */
476       if (!gst_buffer_pool_set_config (pool, config))
477         goto activate_failed;
478
479       if (!gst_buffer_pool_set_active (pool, TRUE))
480         goto activate_failed;
481     }
482
483     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
484     gst_v4l2_object_unlock_stop (self->v4l2output);
485     ret =
486         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
487             v4l2output->pool), &codec_data);
488     gst_v4l2_object_unlock (self->v4l2output);
489     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
490
491     gst_buffer_unref (codec_data);
492
493     if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
494       goto not_negotiated;
495
496     output_state = gst_video_decoder_set_output_state (decoder,
497         info.finfo->format, info.width, info.height, self->input_state);
498
499     /* Copy the rest of the information, there might be more in the future */
500     output_state->info.interlace_mode = info.interlace_mode;
501     gst_video_codec_state_unref (output_state);
502
503     if (!gst_video_decoder_negotiate (decoder)) {
504       if (GST_PAD_IS_FLUSHING (decoder->srcpad))
505         goto flushing;
506       else
507         goto not_negotiated;
508     }
509
510     /* Ensure our internal pool is activated */
511     if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
512             TRUE))
513       goto activate_failed;
514   }
515
516   if (g_atomic_int_get (&self->processing) == FALSE) {
517     /* It possible that the processing thread stopped due to an error */
518     if (self->output_flow != GST_FLOW_OK) {
519       GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
520       ret = self->output_flow;
521       goto drop;
522     }
523
524     GST_DEBUG_OBJECT (self, "Starting decoding thread");
525
526     /* Enable processing input */
527     gst_v4l2_buffer_pool_start_streaming (GST_V4L2_BUFFER_POOL
528         (self->v4l2capture->pool));
529     gst_v4l2_object_unlock_stop (self->v4l2output);
530     gst_v4l2_object_unlock_stop (self->v4l2capture);
531
532     /* Start the processing task, when it quits, the task will disable input
533      * processing to unlock input if draining, or prevent potential block */
534     g_atomic_int_set (&self->processing, TRUE);
535     gst_pad_start_task (decoder->srcpad,
536         (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL);
537   }
538
539   if (frame->input_buffer) {
540     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
541     ret =
542         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
543             pool), &frame->input_buffer);
544     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
545
546     if (ret == GST_FLOW_FLUSHING) {
547       if (g_atomic_int_get (&self->processing) == FALSE)
548         ret = self->output_flow;
549     }
550
551     /* No need to keep input arround */
552     gst_buffer_replace (&frame->input_buffer, NULL);
553   }
554
555   gst_video_codec_frame_unref (frame);
556   return ret;
557
558   /* ERRORS */
559 not_negotiated:
560   {
561     GST_ERROR_OBJECT (self, "not negotiated");
562     ret = GST_FLOW_NOT_NEGOTIATED;
563     goto drop;
564   }
565 activate_failed:
566   {
567     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
568         (_("Failed to allocate required memory.")),
569         ("Buffer pool activation failed"));
570     return GST_FLOW_ERROR;
571   }
572 flushing:
573   {
574     ret = GST_FLOW_FLUSHING;
575     goto drop;
576   }
577 drop:
578   {
579     gst_video_decoder_drop_frame (decoder, frame);
580     return ret;
581   }
582 }
583
584 static gboolean
585 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
586     GstQuery * query)
587 {
588   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
589   GstClockTime latency;
590   gboolean ret = FALSE;
591
592   if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
593     ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
594         query);
595
596   latency = self->v4l2capture->min_buffers_for_capture *
597       self->v4l2capture->duration;
598   gst_video_decoder_set_latency (decoder, latency, latency);
599
600   return ret;
601 }
602
603 static gboolean
604 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
605 {
606   gboolean ret = TRUE;
607   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
608
609   switch (GST_QUERY_TYPE (query)) {
610     case GST_QUERY_CAPS:{
611       GstCaps *filter, *result = NULL;
612       GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
613
614       gst_query_parse_caps (query, &filter);
615
616       if (self->probed_srccaps)
617         result = gst_caps_ref (self->probed_srccaps);
618       else
619         result = gst_pad_get_pad_template_caps (pad);
620
621       if (filter) {
622         GstCaps *tmp = result;
623         result =
624             gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
625         gst_caps_unref (tmp);
626       }
627
628       GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
629
630       gst_query_set_caps_result (query, result);
631       gst_caps_unref (result);
632       break;
633     }
634
635     default:
636       ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
637       break;
638   }
639
640   return ret;
641 }
642
643 static gboolean
644 gst_v4l2_video_dec_sink_query (GstVideoDecoder * decoder, GstQuery * query)
645 {
646   gboolean ret = TRUE;
647   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
648
649   switch (GST_QUERY_TYPE (query)) {
650     case GST_QUERY_CAPS:{
651       GstCaps *filter, *result = NULL;
652       GstPad *pad = GST_VIDEO_DECODER_SINK_PAD (decoder);
653       gst_query_parse_caps (query, &filter);
654
655       if (self->probed_sinkcaps)
656         result = gst_caps_ref (self->probed_sinkcaps);
657       else
658         result = gst_pad_get_pad_template_caps (pad);
659
660       if (filter) {
661         GstCaps *tmp = result;
662         result =
663             gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
664         gst_caps_unref (tmp);
665       }
666
667       GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
668
669       gst_query_set_caps_result (query, result);
670       gst_caps_unref (result);
671       break;
672     }
673
674     default:
675       ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_query (decoder, query);
676       break;
677   }
678
679   return ret;
680 }
681
682 static gboolean
683 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
684 {
685   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
686
687   switch (GST_EVENT_TYPE (event)) {
688     case GST_EVENT_FLUSH_START:
689       gst_v4l2_object_unlock (self->v4l2output);
690       gst_v4l2_object_unlock (self->v4l2capture);
691     default:
692       break;
693   }
694
695   return GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
696 }
697
698 static GstStateChangeReturn
699 gst_v4l2_video_dec_change_state (GstElement * element,
700     GstStateChange transition)
701 {
702   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
703
704   if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
705     g_atomic_int_set (&self->active, FALSE);
706     gst_v4l2_object_unlock (self->v4l2output);
707     gst_v4l2_object_unlock (self->v4l2capture);
708   }
709
710   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
711 }
712
713 static void
714 gst_v4l2_video_dec_dispose (GObject * object)
715 {
716   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
717
718   gst_caps_replace (&self->probed_sinkcaps, NULL);
719   gst_caps_replace (&self->probed_srccaps, NULL);
720
721   G_OBJECT_CLASS (parent_class)->dispose (object);
722 }
723
724 static void
725 gst_v4l2_video_dec_finalize (GObject * object)
726 {
727   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
728
729   gst_v4l2_object_destroy (self->v4l2capture);
730   gst_v4l2_object_destroy (self->v4l2output);
731
732   G_OBJECT_CLASS (parent_class)->finalize (object);
733 }
734
735 static void
736 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
737 {
738   /* V4L2 object are created in subinstance_init */
739 }
740
741 static void
742 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
743 {
744   GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
745   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
746   GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
747
748   gst_video_decoder_set_packetized (decoder, TRUE);
749
750   self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
751       V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
752       gst_v4l2_get_output, gst_v4l2_set_output, NULL);
753   self->v4l2output->no_initial_format = TRUE;
754   self->v4l2output->keep_aspect = FALSE;
755
756   self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
757       V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
758       gst_v4l2_get_input, gst_v4l2_set_input, NULL);
759   self->v4l2capture->no_initial_format = TRUE;
760   self->v4l2output->keep_aspect = FALSE;
761
762   g_object_set (self, "device", klass->default_device, NULL);
763 }
764
765 static void
766 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
767 {
768   GstElementClass *element_class;
769   GObjectClass *gobject_class;
770   GstVideoDecoderClass *video_decoder_class;
771
772   parent_class = g_type_class_peek_parent (klass);
773
774   element_class = (GstElementClass *) klass;
775   gobject_class = (GObjectClass *) klass;
776   video_decoder_class = (GstVideoDecoderClass *) klass;
777
778   GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
779       "V4L2 Video Decoder");
780
781   gst_element_class_set_static_metadata (element_class,
782       "V4L2 Video Decoder",
783       "Codec/Decoder/Video",
784       "Decode video streams via V4L2 API",
785       "Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>");
786
787   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
788   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
789   gobject_class->set_property =
790       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
791   gobject_class->get_property =
792       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
793
794   video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
795   video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
796   video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
797   video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
798   video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
799   video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
800   video_decoder_class->set_format =
801       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
802   video_decoder_class->negotiate =
803       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
804   video_decoder_class->decide_allocation =
805       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
806   /* FIXME propose_allocation or not ? */
807   video_decoder_class->handle_frame =
808       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
809   video_decoder_class->sink_query =
810       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_query);
811   video_decoder_class->src_query =
812       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
813   video_decoder_class->sink_event =
814       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
815
816   element_class->change_state =
817       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
818
819   gst_v4l2_object_install_properties_helper (gobject_class,
820       DEFAULT_PROP_DEVICE);
821
822   /**
823    * GstV4l2VideoDec:capture-io-mode
824    *
825    * Capture IO Mode
826    */
827   g_object_class_install_property (gobject_class, PROP_CAPTURE_IO_MODE,
828       g_param_spec_enum ("capture-io-mode", "Capture IO mode",
829           "Capture I/O mode",
830           GST_TYPE_V4L2_IO_MODE, GST_V4L2_IO_AUTO,
831           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
832 }
833
834 static void
835 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
836 {
837   GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
838   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
839   GstV4l2VideoDecCData *cdata = data;
840
841   klass->default_device = cdata->device;
842
843   /* Note: gst_pad_template_new() take the floating ref from the caps */
844   gst_element_class_add_pad_template (element_class,
845       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
846           cdata->sink_caps));
847   gst_element_class_add_pad_template (element_class,
848       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
849           cdata->src_caps));
850
851   g_free (cdata);
852 }
853
854 /* Probing functions */
855 gboolean
856 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
857 {
858   gboolean ret = FALSE;
859
860   if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
861       && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
862     ret = TRUE;
863
864   return ret;
865 }
866
867 gboolean
868 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
869     const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
870 {
871   GTypeQuery type_query;
872   GTypeInfo type_info = { 0, };
873   GType type, subtype;
874   gchar *type_name;
875   GstV4l2VideoDecCData *cdata;
876
877   cdata = g_new0 (GstV4l2VideoDecCData, 1);
878   cdata->device = g_strdup (device_path);
879   cdata->sink_caps = gst_caps_ref (sink_caps);
880   cdata->src_caps = gst_caps_ref (src_caps);
881
882   type = gst_v4l2_video_dec_get_type ();
883   g_type_query (type, &type_query);
884   memset (&type_info, 0, sizeof (type_info));
885   type_info.class_size = type_query.class_size;
886   type_info.instance_size = type_query.instance_size;
887   type_info.class_init = gst_v4l2_video_dec_subclass_init;
888   type_info.class_data = cdata;
889   type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
890
891   type_name = g_strdup_printf ("v4l2%sdec", basename);
892   subtype = g_type_register_static (type, type_name, &type_info, 0);
893
894   gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1, subtype);
895
896   g_free (type_name);
897
898   return TRUE;
899 }