v4l2codecs: Factor out plugin registration
authorNicolas Dufresne <nicolas.dufresne@collabora.com>
Tue, 28 Apr 2020 00:26:54 +0000 (20:26 -0400)
committerGStreamer Merge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Tue, 28 Apr 2020 22:53:11 +0000 (22:53 +0000)
This introduce a common place for generic functions and factor out the plugin
registration code. This code is nearly identical between implementation.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/merge_requests/1216>

sys/v4l2codecs/gstv4l2codech264dec.c
sys/v4l2codecs/gstv4l2codecvp8dec.c
sys/v4l2codecs/gstv4l2decoder.c
sys/v4l2codecs/gstv4l2decoder.h
sys/v4l2codecs/meson.build

index 7cdaf27..93b4d93 100644 (file)
@@ -978,39 +978,8 @@ void
 gst_v4l2_codec_h264_dec_register (GstPlugin * plugin,
     GstV4l2CodecDevice * device, guint rank)
 {
-  GTypeQuery type_query;
-  GTypeInfo type_info = { 0, };
-  GType subtype;
-  gchar *type_name;
-
-  g_type_query (GST_TYPE_V4L2_CODEC_H264_DEC, &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 = (GClassInitFunc) gst_v4l2_codec_h264_dec_subclass_init;
-  type_info.class_data = gst_mini_object_ref (GST_MINI_OBJECT (device));
-  type_info.instance_init = (GInstanceInitFunc) gst_v4l2_codec_h264_dec_subinit;
-  GST_MINI_OBJECT_FLAG_SET (device, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
-
-  /* The first decoder to be registered should use a constant name, like
-   * v4l2slh264dec, for any additional decoders, we create unique names. Decoder
-   * names may change between boots, so this should help gain stable names for
-   * the most common use cases. SL stands for state-less, we differentiate
-   * with v4l2h264dec as this element may not have the same properties */
-  type_name = g_strdup ("v4l2slh264dec");
-
-  if (g_type_from_name (type_name) != 0) {
-    gchar *basename = g_path_get_basename (device->video_device_path);
-    g_free (type_name);
-    type_name = g_strdup_printf ("v4l2sl%sh264dec", basename);
-    g_free (basename);
-  }
-
-  subtype = g_type_register_static (GST_TYPE_V4L2_CODEC_H264_DEC, type_name,
-      &type_info, 0);
-
-  if (!gst_element_register (plugin, type_name, rank, subtype))
-    GST_WARNING ("Failed to register plugin '%s'", type_name);
-
-  g_free (type_name);
+  gst_v4l2_decoder_register (plugin, GST_TYPE_V4L2_CODEC_H264_DEC,
+      (GClassInitFunc) gst_v4l2_codec_h264_dec_subclass_init,
+      (GInstanceInitFunc) gst_v4l2_codec_h264_dec_subinit,
+      "v4l2sl%sh264dec", device, rank);
 }
index 475e700..8e7edcb 100644 (file)
@@ -22,8 +22,8 @@
 #endif
 
 #include "gstv4l2codecallocator.h"
-#include "gstv4l2codecvp8dec.h"
 #include "gstv4l2codecpool.h"
+#include "gstv4l2codecvp8dec.h"
 #include "linux/vp8-ctrls.h"
 
 GST_DEBUG_CATEGORY_STATIC (v4l2_vp8dec_debug);
@@ -871,39 +871,8 @@ void
 gst_v4l2_codec_vp8_dec_register (GstPlugin * plugin,
     GstV4l2CodecDevice * device, guint rank)
 {
-  GTypeQuery type_query;
-  GTypeInfo type_info = { 0, };
-  GType subtype;
-  gchar *type_name;
-
-  g_type_query (GST_TYPE_V4L2_CODEC_VP8_DEC, &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 = (GClassInitFunc) gst_v4l2_codec_vp8_dec_subclass_init;
-  type_info.class_data = gst_mini_object_ref (GST_MINI_OBJECT (device));
-  type_info.instance_init = (GInstanceInitFunc) gst_v4l2_codec_vp8_dec_subinit;
-  GST_MINI_OBJECT_FLAG_SET (device, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
-
-  /* The first decoder to be registered should use a constant name, like
-   * v4l2slvp8dec, for any additional decoders, we create unique names. Decoder
-   * names may change between boots, so this should help gain stable names for
-   * the most common use cases. SL stands for state-less, we differentiate
-   * with v4l2vp8dec as this element may not have the same properties */
-  type_name = g_strdup ("v4l2slvp8dec");
-
-  if (g_type_from_name (type_name) != 0) {
-    gchar *basename = g_path_get_basename (device->video_device_path);
-    g_free (type_name);
-    type_name = g_strdup_printf ("v4l2sl%svp8dec", basename);
-    g_free (basename);
-  }
-
-  subtype = g_type_register_static (GST_TYPE_V4L2_CODEC_VP8_DEC, type_name,
-      &type_info, 0);
-
-  if (!gst_element_register (plugin, type_name, rank, subtype))
-    GST_WARNING ("Failed to register plugin '%s'", type_name);
-
-  g_free (type_name);
+  gst_v4l2_decoder_register (plugin, GST_TYPE_V4L2_CODEC_VP8_DEC,
+      (GClassInitFunc) gst_v4l2_codec_vp8_dec_subclass_init,
+      (GInstanceInitFunc) gst_v4l2_codec_vp8_dec_subinit,
+      "v4l2sl%svp8dec", device, rank);
 }
index 6423e60..43581c8 100644 (file)
@@ -643,6 +643,47 @@ gst_v4l2_decoder_get_property (GObject * object, guint prop_id,
   }
 }
 
+void
+gst_v4l2_decoder_register (GstPlugin * plugin,
+    GType dec_type, GClassInitFunc class_init, GInstanceInitFunc instance_init,
+    const gchar * element_name_tmpl, GstV4l2CodecDevice * device, guint rank)
+{
+  GTypeQuery type_query;
+  GTypeInfo type_info = { 0, };
+  GType subtype;
+  gchar *type_name;
+
+  g_type_query (dec_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 = class_init;
+  type_info.class_data = gst_mini_object_ref (GST_MINI_OBJECT (device));
+  type_info.instance_init = instance_init;
+  GST_MINI_OBJECT_FLAG_SET (device, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
+
+  /* The first decoder to be registered should use a constant name, like
+   * v4l2slvp8dec, for any additional decoders, we create unique names. Decoder
+   * names may change between boots, so this should help gain stable names for
+   * the most common use cases. SL stands for state-less, we differentiate
+   * with v4l2vp8dec as this element may not have the same properties */
+  type_name = g_strdup_printf (element_name_tmpl, "");
+
+  if (g_type_from_name (type_name) != 0) {
+    gchar *basename = g_path_get_basename (device->video_device_path);
+    g_free (type_name);
+    type_name = g_strdup_printf (element_name_tmpl, basename);
+    g_free (basename);
+  }
+
+  subtype = g_type_register_static (dec_type, type_name, &type_info, 0);
+
+  if (!gst_element_register (plugin, type_name, rank, subtype))
+    GST_WARNING ("Failed to register plugin '%s'", type_name);
+
+  g_free (type_name);
+}
+
 GstV4l2Request *
 gst_v4l2_decoder_alloc_request (GstV4l2Decoder * self)
 {
index 72ecb23..d5c9a54 100644 (file)
@@ -101,6 +101,14 @@ void              gst_v4l2_decoder_set_property (GObject * object, guint prop_id
 void              gst_v4l2_decoder_get_property (GObject * object, guint prop_id,
                                                  GValue * value, GParamSpec * pspec);
 
+void              gst_v4l2_decoder_register (GstPlugin * plugin,
+                                             GType dec_type,
+                                             GClassInitFunc class_init,
+                                             GInstanceInitFunc instance_init,
+                                             const gchar *element_name_tmpl,
+                                             GstV4l2CodecDevice * device,
+                                             guint rank);
+
 GstV4l2Request   *gst_v4l2_decoder_alloc_request (GstV4l2Decoder * self);
 
 void              gst_v4l2_request_free (GstV4l2Request * request);
index 155710b..d876285 100644 (file)
@@ -3,8 +3,8 @@ v4l2codecs_sources = [
   'gstv4l2codecallocator.c',
   'gstv4l2codecdevice.c',
   'gstv4l2codech264dec.c',
-  'gstv4l2codecvp8dec.c',
   'gstv4l2codecpool.c',
+  'gstv4l2codecvp8dec.c',
   'gstv4l2decoder.c',
   'gstv4l2format.c',
 ]