v4l2videodec: Don't lock the decoder when stopping task
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2videodec.c
index 125978e..9de0b54 100644 (file)
@@ -37,9 +37,6 @@
 
 #define DEFAULT_PROP_DEVICE "/dev/video0"
 
-#define V4L2_VIDEO_DEC_QUARK \
-       g_quark_from_static_string("gst-v4l2-video-dec-info")
-
 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_video_dec_debug);
 #define GST_CAT_DEFAULT gst_v4l2_video_dec_debug
 
@@ -50,7 +47,7 @@ typedef struct
   gchar *device;
   GstCaps *sink_caps;
   GstCaps *src_caps;
-} Gstv4l2VideoDecQData;
+} GstV4l2VideoDecCData;
 
 enum
 {
@@ -59,39 +56,9 @@ enum
   PROP_CAPTURE_IO_MODE,
 };
 
-static void gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass);
-static void gst_v4l2_video_dec_init (GstV4l2VideoDec * self, gpointer g_class);
-static void gst_v4l2_video_dec_base_init (gpointer g_class);
-
-static GstVideoDecoderClass *parent_class = NULL;
-
-GType
-gst_v4l2_video_dec_get_type (void)
-{
-  static volatile gsize type = 0;
-
-  if (g_once_init_enter (&type)) {
-    GType _type;
-    static const GTypeInfo info = {
-      sizeof (GstV4l2VideoDecClass),
-      gst_v4l2_video_dec_base_init,
-      NULL,
-      (GClassInitFunc) gst_v4l2_video_dec_class_init,
-      NULL,
-      NULL,
-      sizeof (GstV4l2VideoDec),
-      0,
-      (GInstanceInitFunc) gst_v4l2_video_dec_init,
-      NULL
-    };
-
-    _type = g_type_register_static (GST_TYPE_VIDEO_DECODER, "GstV4l2VideoDec",
-        &info, 0);
-
-    g_once_init_leave (&type, _type);
-  }
-  return type;
-}
+#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,
@@ -107,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:
@@ -243,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);
@@ -275,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 */
   }
@@ -296,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;
 }
@@ -321,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");
@@ -333,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;
@@ -395,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;
@@ -452,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;
@@ -476,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);
+            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,
@@ -502,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) {
@@ -515,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 */
@@ -528,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) {
@@ -550,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;
@@ -590,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;
@@ -628,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;
@@ -711,52 +736,33 @@ gst_v4l2_video_dec_finalize (GObject * object)
 }
 
 static void
-gst_v4l2_video_dec_base_init (gpointer g_class)
+gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
 {
-  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
-  Gstv4l2VideoDecQData *qdata;
-  GstPadTemplate *templ;
-
-  qdata = g_type_get_qdata (G_TYPE_FROM_CLASS (g_class), V4L2_VIDEO_DEC_QUARK);
-  if (!qdata)
-    return;
-
-  templ =
-      gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
-      qdata->sink_caps);
-  gst_element_class_add_pad_template (element_class, templ);
-
-  templ =
-      gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
-      qdata->src_caps);
-  gst_element_class_add_pad_template (element_class, templ);
+  /* V4L2 object are created in subinstance_init */
 }
 
 static void
-gst_v4l2_video_dec_init (GstV4l2VideoDec * self, gpointer g_class)
+gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
 {
-  GstVideoDecoder *decoder = (GstVideoDecoder *) self;
-  Gstv4l2VideoDecQData *qdata;
-
-  qdata = g_type_get_qdata (G_TYPE_FROM_CLASS (g_class), V4L2_VIDEO_DEC_QUARK);
-  if (!qdata)
-    return;
+  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, qdata->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, qdata->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", qdata->device, NULL);
+  g_object_set (self, "device", klass->default_device, NULL);
 }
 
 static void
@@ -772,6 +778,9 @@ gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
   gobject_class = (GObjectClass *) klass;
   video_decoder_class = (GstVideoDecoderClass *) klass;
 
+  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",
       "Codec/Decoder/Video",
@@ -818,126 +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));
 }
 
-/* Probing functions */
-static GstCaps *
-gst_v4l2_video_dec_probe_caps (gchar * device, gint video_fd,
-    enum v4l2_buf_type type, GstCaps * filter)
+static void
+gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
 {
-  gint n;
-  struct v4l2_fmtdesc format;
-  GstCaps *ret, *caps;
-
-  GST_DEBUG ("Getting %s format enumerations", device);
-  caps = gst_caps_new_empty ();
-
-  for (n = 0;; n++) {
-    GstStructure *template;
-
-    format.index = n;
-    format.type = type;
-
-    if (v4l2_ioctl (video_fd, VIDIOC_ENUM_FMT, &format) < 0)
-      break;                    /* end of enumeration */
+  GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
+  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+  GstV4l2VideoDecCData *cdata = data;
 
-    GST_LOG ("index:       %u", format.index);
-    GST_LOG ("type:        %d", format.type);
-    GST_LOG ("flags:       %08x", format.flags);
-    GST_LOG ("description: '%s'", format.description);
-    GST_LOG ("pixelformat: %" GST_FOURCC_FORMAT,
-        GST_FOURCC_ARGS (format.pixelformat));
+  klass->default_device = cdata->device;
 
-    template = gst_v4l2_object_v4l2fourcc_to_structure (format.pixelformat);
+  /* 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));
 
-    if (template)
-      gst_caps_append_structure (caps, template);
-  }
+  g_free (cdata);
+}
 
-  caps = gst_caps_simplify (caps);
+/* Probing functions */
+gboolean
+gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
+{
+  gboolean ret = FALSE;
 
-  ret = gst_caps_intersect (filter, caps);
-  gst_caps_unref (filter);
-  gst_caps_unref (caps);
+  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)
+gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
+    const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
 {
-  gint i = -1;
-  gchar *device = NULL;
-
-  GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
-      "V4L2 Video Decoder");
-
-  while (TRUE) {
-    GstCaps *src_caps, *sink_caps;
-    gint video_fd;
-
-    g_free (device);
-    device = g_strdup_printf ("/dev/video%d", ++i);
-
-    if (!g_file_test (device, G_FILE_TEST_EXISTS))
-      break;
-
-    video_fd = open (device, O_RDWR);
-    if (video_fd == -1) {
-      GST_WARNING ("Failed to open %s", device);
-      continue;
-    }
-
-    /* get sink supported format (no MPLANE for codec) */
-    sink_caps = gst_v4l2_video_dec_probe_caps (device, video_fd,
-        V4L2_BUF_TYPE_VIDEO_OUTPUT, gst_v4l2_object_get_codec_caps ());
-
-    /* get src supported format */
-    src_caps = gst_caps_merge (gst_v4l2_video_dec_probe_caps (device, video_fd,
-            V4L2_BUF_TYPE_VIDEO_CAPTURE, gst_v4l2_object_get_raw_caps ()),
-        gst_v4l2_video_dec_probe_caps (device, video_fd,
-            V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
-            gst_v4l2_object_get_raw_caps ()));
-
-    if (!gst_caps_is_empty (sink_caps) && !gst_caps_is_empty (src_caps)) {
-      GTypeQuery type_query;
-      GTypeInfo type_info = { 0, };
-      GType type, subtype;
-      gchar *type_name;
-      Gstv4l2VideoDecQData *qdata;
-
-      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_name = g_strdup_printf ("v4l2video%ddec", i);
-      subtype = g_type_register_static (type, type_name, &type_info, 0);
-
-      qdata = g_new0 (Gstv4l2VideoDecQData, 1);
-      qdata->device = g_strdup (device);
-      qdata->sink_caps = gst_caps_ref (sink_caps);
-      qdata->src_caps = gst_caps_ref (src_caps);
-
-      g_type_set_qdata (subtype, V4L2_VIDEO_DEC_QUARK, qdata);
-
-      gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1, subtype);
-
-      g_free (type_name);
-    }
-
-    close (video_fd);
-    gst_caps_unref (src_caps);
-    gst_caps_unref (sink_caps);
-  }
-
-  g_free (device);
+  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;
 }