meson: add abi configuration for meson build
[platform/upstream/gstreamer.git] / tools / gst-discoverer.c
index 16468ae..5bd27ac 100644 (file)
@@ -27,6 +27,7 @@
 #include <glib.h>
 #include <gst/gst.h>
 #include <gst/pbutils/pbutils.h>
+#include <gst/audio/audio.h>
 
 #define MAX_INDENT 40
 
@@ -45,6 +46,37 @@ typedef struct
   char **argv;
 } PrivStruct;
 
+static gboolean
+structure_remove_buffers_ip (GQuark field_id, GValue * value,
+    gpointer user_data)
+{
+  if (G_VALUE_HOLDS (value, GST_TYPE_BUFFER))
+    return FALSE;
+
+  if (GST_VALUE_HOLDS_ARRAY (value)) {
+    gint i;
+
+    for (i = 0; i < gst_value_array_get_size (value); i++) {
+      if (structure_remove_buffers_ip (0,
+              (GValue *) gst_value_array_get_value (value, i), user_data))
+        return TRUE;
+    }
+
+    return FALSE;
+  }
+  return TRUE;
+}
+
+static gboolean
+caps_remove_buffers_ip (GstCapsFeatures * features, GstStructure * structure,
+    gpointer user_data)
+{
+  gst_structure_filter_and_map_in_place (structure,
+      structure_remove_buffers_ip, NULL);
+
+  return TRUE;
+}
+
 static void
 my_g_string_append_printf (GString * str, int depth, const gchar * format, ...)
 {
@@ -59,29 +91,54 @@ my_g_string_append_printf (GString * str, int depth, const gchar * format, ...)
   va_end (args);
 }
 
+static gchar *
+caps_to_string (GstCaps * caps)
+{
+  gchar *res = NULL;
+
+  if (verbose) {
+    res = gst_caps_to_string (caps);
+    goto done;
+  }
+
+  caps = gst_caps_make_writable (caps);
+
+  gst_caps_map_in_place (caps, caps_remove_buffers_ip, NULL);
+  res = gst_caps_to_string (caps);
+
+done:
+  gst_caps_unref (caps);
+  return res;
+}
+
 static void
 gst_stream_information_to_string (GstDiscovererStreamInfo * info, GString * s,
     guint depth)
 {
   gchar *tmp;
   GstCaps *caps;
+#ifndef GST_DISABLE_DEPRECATED
   const GstStructure *misc;
+#endif
 
-  my_g_string_append_printf (s, depth, "Codec:\n");
-  caps = gst_discoverer_stream_info_get_caps (info);
-  tmp = gst_caps_to_string (caps);
-  gst_caps_unref (caps);
-  my_g_string_append_printf (s, depth, "  %s\n", tmp);
-  g_free (tmp);
-
-  my_g_string_append_printf (s, depth, "Additional info:\n");
-  if ((misc = gst_discoverer_stream_info_get_misc (info))) {
-    tmp = gst_structure_to_string (misc);
+  if (verbose) {
+    my_g_string_append_printf (s, depth, "Codec:\n");
+    caps = gst_discoverer_stream_info_get_caps (info);
+    tmp = caps_to_string (caps);
     my_g_string_append_printf (s, depth, "  %s\n", tmp);
     g_free (tmp);
-  } else {
-    my_g_string_append_printf (s, depth, "  None\n");
   }
+#ifndef GST_DISABLE_DEPRECATED
+  if (verbose) {
+    misc = gst_discoverer_stream_info_get_misc (info);
+    if (misc) {
+      my_g_string_append_printf (s, depth, "Additional info:\n");
+      tmp = gst_structure_to_string (misc);
+      my_g_string_append_printf (s, depth, "  %s\n", tmp);
+      g_free (tmp);
+    }
+  }
+#endif
 
   my_g_string_append_printf (s, depth, "Stream ID: %s\n",
       gst_discoverer_stream_info_get_stream_id (info));
@@ -109,7 +166,7 @@ print_tag_foreach (const GstTagList * tags, const gchar * tag,
       if (caps) {
         gchar *caps_str;
 
-        caps_str = gst_caps_to_string (caps);
+        caps_str = caps_to_string (gst_caps_ref (caps));
         str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes, "
             "type: %s", gst_buffer_get_size (img), caps_str);
         g_free (caps_str);
@@ -133,6 +190,9 @@ print_tag_foreach (const GstTagList * tags, const gchar * tag,
 static void
 print_tags_topology (guint depth, const GstTagList * tags)
 {
+  if (!verbose)
+    return;
+
   g_print ("%*sTags:\n", 2 * depth, " ");
   if (tags) {
     gst_tag_list_foreach (tags, print_tag_foreach,
@@ -140,8 +200,40 @@ print_tags_topology (guint depth, const GstTagList * tags)
   } else {
     g_print ("%*sNone\n", 2 * (depth + 1), " ");
   }
-  if (verbose)
-    g_print ("%*s\n", 2 * depth, " ");
+  g_print ("%*s\n", 2 * depth, " ");
+}
+
+static gchar *
+format_channel_mask (GstDiscovererAudioInfo * ainfo)
+{
+  GString *s = g_string_sized_new (32);
+  GstAudioChannelPosition position[64];
+  guint channels = gst_discoverer_audio_info_get_channels (ainfo);
+  GEnumClass *enum_class = g_type_class_ref (GST_TYPE_AUDIO_CHANNEL_POSITION);
+  guint i;
+  guint64 channel_mask;
+
+  if (channels == 0)
+    goto done;
+
+  channel_mask = gst_discoverer_audio_info_get_channel_mask (ainfo);
+
+  if (channel_mask != 0) {
+    gst_audio_channel_positions_from_mask (channels, channel_mask, position);
+
+    for (i = 0; i < channels; i++) {
+      GEnumValue *value = g_enum_get_value (enum_class, position[i]);
+      my_g_string_append_printf (s, 0, "%s%s", value->value_nick,
+          i + 1 == channels ? "" : ", ");
+    }
+  } else {
+    g_string_append (s, "unknown layout");
+  }
+
+  g_type_class_unref (enum_class);
+
+done:
+  return g_string_free (s, FALSE);
 }
 
 static gchar *
@@ -153,6 +245,7 @@ gst_stream_audio_information_to_string (GstDiscovererStreamInfo * info,
   const gchar *ctmp;
   int len = 400;
   const GstTagList *tags;
+  gchar *channel_positions;
 
   g_return_val_if_fail (info != NULL, NULL);
 
@@ -164,8 +257,12 @@ gst_stream_audio_information_to_string (GstDiscovererStreamInfo * info,
   ctmp = gst_discoverer_audio_info_get_language (audio_info);
   my_g_string_append_printf (s, depth, "Language: %s\n",
       ctmp ? ctmp : "<unknown>");
-  my_g_string_append_printf (s, depth, "Channels: %u\n",
-      gst_discoverer_audio_info_get_channels (audio_info));
+
+  channel_positions = format_channel_mask (audio_info);
+  my_g_string_append_printf (s, depth, "Channels: %u (%s)\n",
+      gst_discoverer_audio_info_get_channels (audio_info), channel_positions);
+  g_free (channel_positions);
+
   my_g_string_append_printf (s, depth, "Sample rate: %u\n",
       gst_discoverer_audio_info_get_sample_rate (audio_info));
   my_g_string_append_printf (s, depth, "Depth: %u\n",
@@ -266,35 +363,34 @@ print_stream_info (GstDiscovererStreamInfo * info, void *depth)
     if (gst_caps_is_fixed (caps) && !verbose)
       desc = gst_pb_utils_get_codec_description (caps);
     else
-      desc = gst_caps_to_string (caps);
+      desc = caps_to_string (gst_caps_ref (caps));
     gst_caps_unref (caps);
   }
 
   g_print ("%*s%s: %s\n", 2 * GPOINTER_TO_INT (depth), " ",
-      gst_discoverer_stream_info_get_stream_type_nick (info), desc);
+      gst_discoverer_stream_info_get_stream_type_nick (info),
+      GST_STR_NULL (desc));
 
   if (desc) {
     g_free (desc);
     desc = NULL;
   }
 
-  if (verbose) {
-    if (GST_IS_DISCOVERER_AUDIO_INFO (info))
-      desc =
-          gst_stream_audio_information_to_string (info,
-          GPOINTER_TO_INT (depth) + 1);
-    else if (GST_IS_DISCOVERER_VIDEO_INFO (info))
-      desc =
-          gst_stream_video_information_to_string (info,
-          GPOINTER_TO_INT (depth) + 1);
-    else if (GST_IS_DISCOVERER_SUBTITLE_INFO (info))
-      desc =
-          gst_stream_subtitle_information_to_string (info,
-          GPOINTER_TO_INT (depth) + 1);
-    if (desc) {
-      g_print ("%s", desc);
-      g_free (desc);
-    }
+  if (GST_IS_DISCOVERER_AUDIO_INFO (info))
+    desc =
+        gst_stream_audio_information_to_string (info,
+        GPOINTER_TO_INT (depth) + 1);
+  else if (GST_IS_DISCOVERER_VIDEO_INFO (info))
+    desc =
+        gst_stream_video_information_to_string (info,
+        GPOINTER_TO_INT (depth) + 1);
+  else if (GST_IS_DISCOVERER_SUBTITLE_INFO (info))
+    desc =
+        gst_stream_subtitle_information_to_string (info,
+        GPOINTER_TO_INT (depth) + 1);
+  if (desc) {
+    g_print ("%s", desc);
+    g_free (desc);
   }
 }
 
@@ -365,7 +461,9 @@ print_properties (GstDiscovererInfo * info, gint tab)
       GST_TIME_ARGS (gst_discoverer_info_get_duration (info)));
   g_print ("%*sSeekable: %s\n", tab + 1, " ",
       (gst_discoverer_info_get_seekable (info) ? "yes" : "no"));
-  if ((tags = gst_discoverer_info_get_tags (info))) {
+  g_print ("%*sLive: %s\n", tab + 1, " ",
+      (gst_discoverer_info_get_live (info) ? "yes" : "no"));
+  if (verbose && (tags = gst_discoverer_info_get_tags (info))) {
     g_print ("%*sTags: \n", tab + 1, " ");
     gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (tab + 2));
   }
@@ -420,27 +518,25 @@ print_info (GstDiscovererInfo * info, GError * err)
     }
     case GST_DISCOVERER_MISSING_PLUGINS:
     {
+      gint i = 0;
+      const gchar **installer_details =
+          gst_discoverer_info_get_missing_elements_installer_details (info);
+
       g_print ("Missing plugins\n");
-      if (verbose) {
-        gint i = 0;
-        const gchar **installer_details =
-            gst_discoverer_info_get_missing_elements_installer_details (info);
 
-        while (installer_details[i]) {
-          g_print (" (%s)\n", installer_details[i]);
+      while (installer_details[i]) {
+        g_print (" (%s)\n", installer_details[i]);
 
-          i++;
-        }
+        i++;
       }
       break;
     }
   }
 
   if ((sinfo = gst_discoverer_info_get_stream_info (info))) {
-    g_print ("\nTopology:\n");
-    print_topology (sinfo, 1);
     g_print ("\nProperties:\n");
     print_properties (info, 1);
+    print_topology (sinfo, 1);
     gst_discoverer_stream_info_unref (sinfo);
   }
 
@@ -487,7 +583,7 @@ process_file (GstDiscoverer * dc, const gchar * filename)
 
     if (err) {
       g_warning ("Couldn't convert filename to URI: %s\n", err->message);
-      g_error_free (err);
+      g_clear_error (&err);
       return;
     }
   } else {
@@ -498,9 +594,9 @@ process_file (GstDiscoverer * dc, const gchar * filename)
     g_print ("Analyzing %s\n", uri);
     info = gst_discoverer_discover_uri (dc, uri, &err);
     print_info (info, err);
-    if (err)
-      g_error_free (err);
-    gst_discoverer_info_unref (info);
+    g_clear_error (&err);
+    if (info)
+      gst_discoverer_info_unref (info);
   } else {
     gst_discoverer_discover_uri_async (dc, uri);
   }
@@ -537,9 +633,14 @@ main (int argc, char **argv)
   GError *err = NULL;
   GstDiscoverer *dc;
   gint timeout = 10;
+  gboolean use_cache = FALSE, print_cache_dir = FALSE;
   GOptionEntry options[] = {
     {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
         "Run asynchronously", NULL},
+    {"use-cache", 0, 0, G_OPTION_ARG_NONE, &use_cache,
+        "Use GstDiscovererInfo from our cache.", NULL},
+    {"print-cache-dir", 0, 0, G_OPTION_ARG_NONE, &print_cache_dir,
+        "Print the directory of the discoverer cache.", NULL},
     {"timeout", 't', 0, G_OPTION_ARG_INT, &timeout,
         "Specify timeout (in seconds, default 10)", "T"},
     /* {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek, */
@@ -574,6 +675,15 @@ main (int argc, char **argv)
     exit (-1);
   }
 
+  if (print_cache_dir) {
+    gchar *cache_dir =
+        g_build_filename (g_get_user_cache_dir (), "gstreamer-" GST_API_VERSION,
+        "discoverer", NULL);
+    g_print ("\nGstDiscoverer cache directory:\n\n    %s\n\n", cache_dir);
+    g_free (cache_dir);
+    exit (0);
+  }
+
   dc = gst_discoverer_new (timeout * GST_SECOND, &err);
   if (G_UNLIKELY (dc == NULL)) {
     g_print ("Error initializing: %s\n", err->message);
@@ -581,6 +691,8 @@ main (int argc, char **argv)
     exit (1);
   }
 
+  g_object_set (dc, "use-cache", use_cache, NULL);
+
   if (!async) {
     gint i;
     for (i = 1; i < argc; i++)