v4l2videodec: Don't lock the decoder when stopping task
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2videodec.c
index b5c2cb0..9de0b54 100644 (file)
 #include "config.h"
 #endif
 
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+
 #include "gstv4l2videodec.h"
 #include "v4l2_calls.h"
 
 #include <string.h>
 #include <gst/gst-i18n-plugin.h>
 
-#define DEFAULT_PROP_DEVICE "/dev/video8"
+#define DEFAULT_PROP_DEVICE "/dev/video0"
 
 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_video_dec_debug);
 #define GST_CAT_DEFAULT gst_v4l2_video_dec_debug
 
-#define parent_class gst_v4l2_video_dec_parent_class
-G_DEFINE_TYPE (GstV4l2VideoDec, gst_v4l2_video_dec, GST_TYPE_VIDEO_DECODER);
-
 static gboolean gst_v4l2_video_dec_flush (GstVideoDecoder * decoder);
 
+typedef struct
+{
+  gchar *device;
+  GstCaps *sink_caps;
+  GstCaps *src_caps;
+} GstV4l2VideoDecCData;
+
 enum
 {
   PROP_0,
@@ -46,6 +56,10 @@ enum
   PROP_CAPTURE_IO_MODE,
 };
 
+#define gst_v4l2_video_dec_parent_class parent_class
+G_DEFINE_ABSTRACT_TYPE (GstV4l2VideoDec, gst_v4l2_video_dec,
+    GST_TYPE_VIDEO_DECODER);
+
 static void
 gst_v4l2_video_dec_set_property (GObject * object,
     guint prop_id, const GValue * value, GParamSpec * pspec)
@@ -60,8 +74,8 @@ gst_v4l2_video_dec_set_property (GObject * object,
           pspec);
       break;
     case PROP_CAPTURE_IO_MODE:
-      gst_v4l2_object_set_property_helper (self->v4l2capture, prop_id, value,
-          pspec);
+      gst_v4l2_object_set_property_helper (self->v4l2capture, PROP_IO_MODE,
+          value, pspec);
       break;
 
     case PROP_DEVICE:
@@ -196,6 +210,16 @@ gst_v4l2_video_dec_stop (GstVideoDecoder * decoder)
 
   GST_DEBUG_OBJECT (self, "Stopping");
 
+  gst_v4l2_object_unlock (self->v4l2output);
+  gst_v4l2_object_unlock (self->v4l2capture);
+
+  /* Wait for capture thread to stop */
+  gst_pad_stop_task (decoder->srcpad);
+
+  GST_VIDEO_DECODER_STREAM_LOCK (decoder);
+  self->output_flow = GST_FLOW_OK;
+  GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
+
   /* Should have been flushed already */
   g_assert (g_atomic_int_get (&self->active) == FALSE);
   g_assert (g_atomic_int_get (&self->processing) == FALSE);
@@ -228,6 +252,7 @@ gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
       goto done;
     }
     gst_video_codec_state_unref (self->input_state);
+    self->input_state = NULL;
 
     /* FIXME we probably need to do more work if pools are active */
   }
@@ -249,14 +274,18 @@ gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
   GST_DEBUG_OBJECT (self, "Flushing");
 
   /* Wait for capture thread to stop */
+  GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
   gst_pad_stop_task (decoder->srcpad);
+  GST_VIDEO_DECODER_STREAM_LOCK (decoder);
   self->output_flow = GST_FLOW_OK;
 
-  gst_v4l2_buffer_pool_flush (GST_V4L2_BUFFER_POOL (self->v4l2output->pool));
-  gst_v4l2_buffer_pool_flush (GST_V4L2_BUFFER_POOL (self->v4l2capture->pool));
+  if (self->v4l2output->pool)
+    gst_v4l2_buffer_pool_stop_streaming (GST_V4L2_BUFFER_POOL
+        (self->v4l2output->pool));
 
-  /* Output will remain flushing until new frame comes in */
-  gst_v4l2_object_unlock_stop (self->v4l2capture);
+  if (self->v4l2capture->pool)
+    gst_v4l2_buffer_pool_stop_streaming (GST_V4L2_BUFFER_POOL
+        (self->v4l2capture->pool));
 
   return TRUE;
 }
@@ -274,7 +303,7 @@ gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
   GstFlowReturn ret = GST_FLOW_OK;
   GstBuffer *buffer;
 
-  if (!self->input_state)
+  if (!self->processing)
     goto done;
 
   GST_DEBUG_OBJECT (self, "Finishing decoding");
@@ -286,12 +315,17 @@ gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
     buffer = gst_buffer_new ();
     ret =
         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
-            v4l2output->pool), buffer);
+            v4l2output->pool), &buffer);
     gst_buffer_unref (buffer);
   }
   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
 
-  g_assert (g_atomic_int_get (&self->processing) == FALSE);
+  /* Ensure the processing thread has stopped */
+  if (self->processing) {
+    gst_v4l2_object_unlock (self->v4l2capture);
+    gst_pad_stop_task (decoder->srcpad);
+    g_assert (g_atomic_int_get (&self->processing) == FALSE);
+  }
 
   if (ret == GST_FLOW_FLUSHING)
     ret = self->output_flow;
@@ -348,20 +382,23 @@ gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
    * comes in and holding this lock would prevent that.
    */
   pool = gst_video_decoder_get_buffer_pool (decoder);
+
+  /* Pool may be NULL if we started going to READY state */
+  if (pool == NULL) {
+    ret = GST_FLOW_FLUSHING;
+    goto beach;
+  }
+
   ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
   g_object_unref (pool);
 
   if (ret != GST_FLOW_OK)
     goto beach;
 
-  /* Check if buffer isn't the last one */
-  if (gst_buffer_get_size (buffer) == 0)
-    goto beach;
-
   GST_LOG_OBJECT (decoder, "Process output buffer");
   ret =
       gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
-          v4l2capture->pool), buffer);
+          v4l2capture->pool), &buffer);
 
   if (ret != GST_FLOW_OK)
     goto beach;
@@ -405,11 +442,14 @@ gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
     goto flushing;
 
   if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
+    if (!self->input_state)
+      goto not_negotiated;
     if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps))
       goto not_negotiated;
   }
 
   if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
+    GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
     GstVideoInfo info;
     GstVideoCodecState *output_state;
     GstBuffer *codec_data;
@@ -429,17 +469,31 @@ gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
       frame->input_buffer = NULL;
     }
 
+    /* Ensure input internal pool is active */
+    if (!gst_buffer_pool_is_active (pool)) {
+      GstStructure *config = gst_buffer_pool_get_config (pool);
+      gst_buffer_pool_config_set_params (config, self->input_state->caps,
+          self->v4l2output->info.size, 2, 2);
+
+      /* There is no reason to refuse this config */
+      if (!gst_buffer_pool_set_config (pool, config))
+        goto activate_failed;
+
+      if (!gst_buffer_pool_set_active (pool, TRUE))
+        goto activate_failed;
+    }
+
     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
     gst_v4l2_object_unlock_stop (self->v4l2output);
     ret =
-        gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
-            pool), codec_data);
+        gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
+            v4l2output->pool), &codec_data);
     gst_v4l2_object_unlock (self->v4l2output);
     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
 
     gst_buffer_unref (codec_data);
 
-    if (!gst_v4l2_object_setup_format (self->v4l2capture, &info, &self->align))
+    if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
       goto not_negotiated;
 
     output_state = gst_video_decoder_set_output_state (decoder,
@@ -455,6 +509,11 @@ gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
       else
         goto not_negotiated;
     }
+
+    /* Ensure our internal pool is activated */
+    if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
+            TRUE))
+      goto activate_failed;
   }
 
   if (g_atomic_int_get (&self->processing) == FALSE) {
@@ -468,7 +527,10 @@ gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
     GST_DEBUG_OBJECT (self, "Starting decoding thread");
 
     /* Enable processing input */
+    gst_v4l2_buffer_pool_start_streaming (GST_V4L2_BUFFER_POOL
+        (self->v4l2capture->pool));
     gst_v4l2_object_unlock_stop (self->v4l2output);
+    gst_v4l2_object_unlock_stop (self->v4l2capture);
 
     /* Start the processing task, when it quits, the task will disable input
      * processing to unlock input if draining, or prevent potential block */
@@ -481,7 +543,7 @@ gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
     ret =
         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
-            pool), frame->input_buffer);
+            pool), &frame->input_buffer);
     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
 
     if (ret == GST_FLOW_FLUSHING) {
@@ -503,6 +565,13 @@ not_negotiated:
     ret = GST_FLOW_NOT_NEGOTIATED;
     goto drop;
   }
+activate_failed:
+  {
+    GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
+        (_("Failed to allocate required memory.")),
+        ("Buffer pool activation failed"));
+    return GST_FLOW_ERROR;
+  }
 flushing:
   {
     ret = GST_FLOW_FLUSHING;
@@ -543,12 +612,14 @@ gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
   switch (GST_QUERY_TYPE (query)) {
     case GST_QUERY_CAPS:{
       GstCaps *filter, *result = NULL;
+      GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
+
       gst_query_parse_caps (query, &filter);
 
       if (self->probed_srccaps)
         result = gst_caps_ref (self->probed_srccaps);
       else
-        result = gst_v4l2_object_get_raw_caps ();
+        result = gst_pad_get_pad_template_caps (pad);
 
       if (filter) {
         GstCaps *tmp = result;
@@ -581,12 +652,13 @@ gst_v4l2_video_dec_sink_query (GstVideoDecoder * decoder, GstQuery * query)
   switch (GST_QUERY_TYPE (query)) {
     case GST_QUERY_CAPS:{
       GstCaps *filter, *result = NULL;
+      GstPad *pad = GST_VIDEO_DECODER_SINK_PAD (decoder);
       gst_query_parse_caps (query, &filter);
 
       if (self->probed_sinkcaps)
         result = gst_caps_ref (self->probed_sinkcaps);
       else
-        result = gst_v4l2_object_get_codec_caps ();
+        result = gst_pad_get_pad_template_caps (pad);
 
       if (filter) {
         GstCaps *tmp = result;
@@ -666,23 +738,31 @@ gst_v4l2_video_dec_finalize (GObject * object)
 static void
 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
 {
-  GstVideoDecoder *decoder = (GstVideoDecoder *) self;
+  /* V4L2 object are created in subinstance_init */
+}
+
+static void
+gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
+{
+  GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
+  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
+  GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
 
   gst_video_decoder_set_packetized (decoder, TRUE);
 
   self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
-      V4L2_BUF_TYPE_VIDEO_OUTPUT, DEFAULT_PROP_DEVICE,
+      V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
       gst_v4l2_get_output, gst_v4l2_set_output, NULL);
   self->v4l2output->no_initial_format = TRUE;
   self->v4l2output->keep_aspect = FALSE;
 
   self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
-      V4L2_BUF_TYPE_VIDEO_CAPTURE, DEFAULT_PROP_DEVICE,
+      V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
       gst_v4l2_get_input, gst_v4l2_set_input, NULL);
   self->v4l2capture->no_initial_format = TRUE;
   self->v4l2output->keep_aspect = FALSE;
 
-  g_object_set (self, "device", DEFAULT_PROP_DEVICE, NULL);
+  g_object_set (self, "device", klass->default_device, NULL);
 }
 
 static void
@@ -692,17 +772,14 @@ gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
   GObjectClass *gobject_class;
   GstVideoDecoderClass *video_decoder_class;
 
+  parent_class = g_type_class_peek_parent (klass);
+
   element_class = (GstElementClass *) klass;
   gobject_class = (GObjectClass *) klass;
   video_decoder_class = (GstVideoDecoderClass *) klass;
 
-  gst_element_class_add_pad_template (element_class,
-      gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
-          gst_v4l2_object_get_codec_caps ()));
-
-  gst_element_class_add_pad_template (element_class,
-      gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
-          gst_v4l2_object_get_raw_caps ()));
+  GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
+      "V4L2 Video Decoder");
 
   gst_element_class_set_static_metadata (element_class,
       "V4L2 Video Decoder",
@@ -742,7 +819,6 @@ gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
   element_class->change_state =
       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
 
-
   gst_v4l2_object_install_properties_helper (gobject_class,
       DEFAULT_PROP_DEVICE);
 
@@ -751,12 +827,76 @@ gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
    *
    * Capture IO Mode
    */
-  g_object_class_install_property (gobject_class, PROP_IO_MODE,
+  g_object_class_install_property (gobject_class, PROP_CAPTURE_IO_MODE,
       g_param_spec_enum ("capture-io-mode", "Capture IO mode",
           "Capture I/O mode",
           GST_TYPE_V4L2_IO_MODE, GST_V4L2_IO_AUTO,
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+}
 
-  GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
-      "V4L2 Video Decoder");
+static void
+gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
+{
+  GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
+  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+  GstV4l2VideoDecCData *cdata = data;
+
+  klass->default_device = cdata->device;
+
+  /* Note: gst_pad_template_new() take the floating ref from the caps */
+  gst_element_class_add_pad_template (element_class,
+      gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
+          cdata->sink_caps));
+  gst_element_class_add_pad_template (element_class,
+      gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
+          cdata->src_caps));
+
+  g_free (cdata);
+}
+
+/* Probing functions */
+gboolean
+gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
+{
+  gboolean ret = FALSE;
+
+  if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
+      && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
+    ret = TRUE;
+
+  return ret;
+}
+
+gboolean
+gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
+    const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
+{
+  GTypeQuery type_query;
+  GTypeInfo type_info = { 0, };
+  GType type, subtype;
+  gchar *type_name;
+  GstV4l2VideoDecCData *cdata;
+
+  cdata = g_new0 (GstV4l2VideoDecCData, 1);
+  cdata->device = g_strdup (device_path);
+  cdata->sink_caps = gst_caps_ref (sink_caps);
+  cdata->src_caps = gst_caps_ref (src_caps);
+
+  type = gst_v4l2_video_dec_get_type ();
+  g_type_query (type, &type_query);
+  memset (&type_info, 0, sizeof (type_info));
+  type_info.class_size = type_query.class_size;
+  type_info.instance_size = type_query.instance_size;
+  type_info.class_init = gst_v4l2_video_dec_subclass_init;
+  type_info.class_data = cdata;
+  type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
+
+  type_name = g_strdup_printf ("v4l2%sdec", basename);
+  subtype = g_type_register_static (type, type_name, &type_info, 0);
+
+  gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1, subtype);
+
+  g_free (type_name);
+
+  return TRUE;
 }