pbutils: descriptions: Allow smart codec tag handling
authorEdward Hervey <edward@collabora.com>
Fri, 14 Jun 2013 05:23:40 +0000 (07:23 +0200)
committerEdward Hervey <edward@collabora.com>
Fri, 28 Jun 2013 05:09:53 +0000 (07:09 +0200)
We already have internally the information on what type of stream (audio,
video, container, subtitle, ...) a certain caps is.
Instead of forcing callers to specify which CODEC_TAG category a certain
caps is, use that information to make a smart choice.

Does not break previous behaviour of gst_pb_utils_add_codec_description_to_tag_list
(if tag is specified it will be used, if caps is invalid it will be rejected,
...).

https://bugzilla.gnome.org/show_bug.cgi?id=702215

gst-libs/gst/pbutils/descriptions.c
tests/check/libs/pbutils.c

index 7b9f9b6..ac27e7d 100644 (file)
@@ -949,8 +949,9 @@ gst_pb_utils_get_element_description (const gchar * factory_name)
 /**
  * gst_pb_utils_add_codec_description_to_tag_list:
  * @taglist: a #GstTagList
- * @codec_tag: a GStreamer codec tag such as #GST_TAG_AUDIO_CODEC,
- *             #GST_TAG_VIDEO_CODEC or #GST_TAG_CODEC
+ * @codec_tag: (allow-none): a GStreamer codec tag such as #GST_TAG_AUDIO_CODEC,
+ *             #GST_TAG_VIDEO_CODEC or #GST_TAG_CODEC. If none is specified,
+ *             the function will attempt to detect the appropriate category.
  * @caps: the (fixed) #GstCaps for which a codec tag should be added.
  *
  * Adds a codec tag describing the format specified by @caps to @taglist.
@@ -966,9 +967,8 @@ gst_pb_utils_add_codec_description_to_tag_list (GstTagList * taglist,
 
   g_return_val_if_fail (taglist != NULL, FALSE);
   g_return_val_if_fail (GST_IS_TAG_LIST (taglist), FALSE);
-  g_return_val_if_fail (codec_tag != NULL, FALSE);
-  g_return_val_if_fail (gst_tag_exists (codec_tag), FALSE);
-  g_return_val_if_fail (gst_tag_get_type (codec_tag) == G_TYPE_STRING, FALSE);
+  g_return_val_if_fail (codec_tag == NULL || (gst_tag_exists (codec_tag)
+          && gst_tag_get_type (codec_tag) == G_TYPE_STRING), FALSE);
   g_return_val_if_fail (caps != NULL, FALSE);
   g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
 
@@ -976,6 +976,20 @@ gst_pb_utils_add_codec_description_to_tag_list (GstTagList * taglist,
   if (info == NULL)
     return FALSE;
 
+  /* Attempt to find tag classification */
+  if (codec_tag == NULL) {
+    if (info->flags & FLAG_CONTAINER)
+      codec_tag = GST_TAG_CONTAINER_FORMAT;
+    else if (info->flags & FLAG_AUDIO)
+      codec_tag = GST_TAG_AUDIO_CODEC;
+    else if (info->flags & FLAG_VIDEO)
+      codec_tag = GST_TAG_VIDEO_CODEC;
+    else if (info->flags & FLAG_SUB)
+      codec_tag = GST_TAG_SUBTITLE_CODEC;
+    else
+      codec_tag = GST_TAG_CODEC;
+  }
+
   desc = format_info_get_desc (info, caps);
   gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, codec_tag, desc, NULL);
   g_free (desc);
index a6bc6b0..e39e7f4 100644 (file)
@@ -425,7 +425,8 @@ GST_END_TEST;
 GST_START_TEST (test_pb_utils_taglist_add_codec_info)
 {
   GstTagList *list;
-  GstCaps *caps;
+  GstCaps *caps, *bogus_caps;
+  gchar *res;
 
   gst_pb_utils_init ();
   list = gst_tag_list_new_empty ();
@@ -434,8 +435,6 @@ GST_START_TEST (test_pb_utils_taglist_add_codec_info)
       (gst_pb_utils_add_codec_description_to_tag_list (NULL,
               GST_TAG_VIDEO_CODEC, caps)));
   ASSERT_CRITICAL (fail_if
-      (gst_pb_utils_add_codec_description_to_tag_list (list, NULL, caps)));
-  ASSERT_CRITICAL (fail_if
       (gst_pb_utils_add_codec_description_to_tag_list (list, "asdfa", caps)));
   ASSERT_CRITICAL (fail_if
       (gst_pb_utils_add_codec_description_to_tag_list (list,
@@ -443,10 +442,68 @@ GST_START_TEST (test_pb_utils_taglist_add_codec_info)
   ASSERT_CRITICAL (fail_if
       (gst_pb_utils_add_codec_description_to_tag_list (list,
               GST_TAG_VIDEO_CODEC, NULL)));
-  /* FIXME: do something here */
+
+  /* Try adding bogus caps (should fail) */
+  bogus_caps = gst_caps_new_empty_simple ("bogus/format");
+  fail_if (gst_pb_utils_add_codec_description_to_tag_list (list,
+          GST_TAG_VIDEO_CODEC, bogus_caps));
+  gst_caps_unref (bogus_caps);
+
+  /* Try adding valid caps with known tag */
   fail_unless (gst_pb_utils_add_codec_description_to_tag_list (list,
           GST_TAG_VIDEO_CODEC, caps));
   fail_if (gst_tag_list_is_empty (list));
+  fail_unless (gst_tag_list_get_string (list, GST_TAG_VIDEO_CODEC, &res));
+  g_free (res);
+  gst_tag_list_unref (list);
+
+  /* Try adding valid caps with auto-tag (for video, audio, subtitle, generic) */
+  list = gst_tag_list_new_empty ();
+  fail_unless (gst_pb_utils_add_codec_description_to_tag_list (list, NULL,
+          caps));
+  fail_if (gst_tag_list_is_empty (list));
+  fail_unless (gst_tag_list_get_string (list, GST_TAG_VIDEO_CODEC, &res));
+  g_free (res);
+  gst_tag_list_unref (list);
+  gst_caps_unref (caps);
+
+  list = gst_tag_list_new_empty ();
+  caps = gst_caps_new_empty_simple ("audio/x-vorbis");
+  fail_unless (gst_pb_utils_add_codec_description_to_tag_list (list, NULL,
+          caps));
+  fail_if (gst_tag_list_is_empty (list));
+  fail_unless (gst_tag_list_get_string (list, GST_TAG_AUDIO_CODEC, &res));
+  g_free (res);
+  gst_tag_list_unref (list);
+  gst_caps_unref (caps);
+
+  list = gst_tag_list_new_empty ();
+  caps = gst_caps_new_empty_simple ("subtitle/x-kate");
+  fail_unless (gst_pb_utils_add_codec_description_to_tag_list (list, NULL,
+          caps));
+  fail_if (gst_tag_list_is_empty (list));
+  fail_unless (gst_tag_list_get_string (list, GST_TAG_SUBTITLE_CODEC, &res));
+  g_free (res);
+  gst_tag_list_unref (list);
+  gst_caps_unref (caps);
+
+  list = gst_tag_list_new_empty ();
+  caps = gst_caps_new_empty_simple ("application/ogg");
+  fail_unless (gst_pb_utils_add_codec_description_to_tag_list (list, NULL,
+          caps));
+  fail_if (gst_tag_list_is_empty (list));
+  fail_unless (gst_tag_list_get_string (list, GST_TAG_CONTAINER_FORMAT, &res));
+  g_free (res);
+  gst_tag_list_unref (list);
+  gst_caps_unref (caps);
+
+  list = gst_tag_list_new_empty ();
+  caps = gst_caps_new_empty_simple ("image/bmp");
+  fail_unless (gst_pb_utils_add_codec_description_to_tag_list (list, NULL,
+          caps));
+  fail_if (gst_tag_list_is_empty (list));
+  fail_unless (gst_tag_list_get_string (list, GST_TAG_CODEC, &res));
+  g_free (res);
   gst_tag_list_unref (list);
   gst_caps_unref (caps);
 }