tests/gst-launch: Fix sample memory leak
[platform/upstream/gstreamer.git] / tests / check / gst / gsttag.c
index 7efcf2c..75c84da 100644 (file)
@@ -13,8 +13,8 @@
  *
  * You should have received a copy of the GNU Library General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 #include <gst/check/gstcheck.h>
@@ -66,8 +66,8 @@ check_tags_empty (const GstTagList * list)
 
 #define NEW_LIST_FIXED(mode)                                    \
 G_STMT_START {                                                  \
-  if (list) gst_tag_list_free (list);                           \
-  list = gst_tag_list_new ();                                   \
+  if (list) gst_tag_list_unref (list);                          \
+  list = gst_tag_list_new_empty ();                             \
   gst_tag_list_add (list, mode, FTAG, FIXED1, FTAG, FIXED2,     \
                     FTAG, FIXED3, FTAG, FIXED4, NULL);          \
   mark_point();                                                 \
@@ -75,8 +75,8 @@ G_STMT_START {                                                  \
 
 #define NEW_LIST_UNFIXED(mode)                                  \
 G_STMT_START {                                                  \
-  if (list) gst_tag_list_free (list);                           \
-  list = gst_tag_list_new ();                                   \
+  if (list) gst_tag_list_unref (list);                          \
+  list = gst_tag_list_new_empty ();                             \
   gst_tag_list_add (list, mode, UTAG, UNFIXED1, UTAG, UNFIXED2, \
                     UTAG, UNFIXED3, UTAG, UNFIXED4, NULL);      \
   mark_point();                                                 \
@@ -84,56 +84,56 @@ G_STMT_START {                                                  \
 
 #define NEW_LISTS_FIXED(mode)                                   \
 G_STMT_START {                                                  \
-  if (list) gst_tag_list_free (list);                           \
-  list = gst_tag_list_new ();                                   \
+  if (list) gst_tag_list_unref (list);                          \
+  list = gst_tag_list_new_empty ();                             \
   gst_tag_list_add (list, GST_TAG_MERGE_APPEND, FTAG, FIXED1,   \
                     FTAG, FIXED2, NULL);                        \
-  if (list2) gst_tag_list_free (list2);                         \
-  list2 = gst_tag_list_new ();                                  \
+  if (list2) gst_tag_list_unref (list2);                        \
+  list2 = gst_tag_list_new_empty ();                            \
   gst_tag_list_add (list2, GST_TAG_MERGE_APPEND, FTAG, FIXED3,  \
                     FTAG, FIXED4, NULL);                        \
-  if (merge) gst_tag_list_free (merge);                         \
+  if (merge) gst_tag_list_unref (merge);                        \
   merge = gst_tag_list_merge (list, list2, mode);               \
   mark_point();                                                 \
 } G_STMT_END;
 
 #define NEW_LISTS_UNFIXED(mode)                                 \
 G_STMT_START {                                                  \
-  if (list) gst_tag_list_free (list);                           \
-  list = gst_tag_list_new ();                                   \
+  if (list) gst_tag_list_unref (list);                          \
+  list = gst_tag_list_new_empty ();                             \
   gst_tag_list_add (list, GST_TAG_MERGE_APPEND, UTAG, UNFIXED1, \
                     UTAG, UNFIXED2, NULL);                      \
-  if (list2) gst_tag_list_free (list2);                         \
-  list2 = gst_tag_list_new ();                                  \
+  if (list2) gst_tag_list_unref (list2);                        \
+  list2 = gst_tag_list_new_empty ();                            \
   gst_tag_list_add (list2, GST_TAG_MERGE_APPEND, UTAG, UNFIXED3,\
                     UTAG, UNFIXED4, NULL);                      \
-  if (merge) gst_tag_list_free (merge);                         \
+  if (merge) gst_tag_list_unref (merge);                        \
   merge = gst_tag_list_merge (list, list2, mode);               \
   mark_point();                                                 \
 } G_STMT_END;
 
 #define NEW_LISTS_EMPTY1(mode)                                  \
 G_STMT_START {                                                  \
-  if (list) gst_tag_list_free (list);                           \
+  if (list) gst_tag_list_unref (list);                          \
   list = NULL;                                                  \
-  if (list2) gst_tag_list_free (list2);                         \
-  list2 = gst_tag_list_new ();                                  \
+  if (list2) gst_tag_list_unref (list2);                        \
+  list2 = gst_tag_list_new_empty ();                            \
   gst_tag_list_add (list2, GST_TAG_MERGE_APPEND, FTAG, FIXED3,  \
                     FTAG, FIXED4, NULL);                        \
-  if (merge) gst_tag_list_free (merge);                         \
+  if (merge) gst_tag_list_unref (merge);                        \
   merge = gst_tag_list_merge (list, list2, mode);               \
   mark_point();                                                 \
 } G_STMT_END;
 
-#define NEW_LISTS_EMPTY2(mode)                                   \
+#define NEW_LISTS_EMPTY2(mode)                                  \
 G_STMT_START {                                                  \
-  if (list) gst_tag_list_free (list);                           \
-  list = gst_tag_list_new ();                                   \
+  if (list) gst_tag_list_unref (list);                          \
+  list = gst_tag_list_new_empty ();                             \
   gst_tag_list_add (list, GST_TAG_MERGE_APPEND, FTAG, FIXED1,   \
                     FTAG, FIXED2, NULL);                        \
-  if (list2) gst_tag_list_free (list2);                         \
+  if (list2) gst_tag_list_unref (list2);                        \
   list2 = NULL;                                                 \
-  if (merge) gst_tag_list_free (merge);                         \
+  if (merge) gst_tag_list_unref (merge);                        \
   merge = gst_tag_list_merge (list, list2, mode);               \
   mark_point();                                                 \
 } G_STMT_END;
@@ -185,7 +185,7 @@ GST_START_TEST (test_add)
 
   /* clean up */
   if (list)
-    gst_tag_list_free (list);
+    gst_tag_list_unref (list);
 }
 
 GST_END_TEST
@@ -256,11 +256,11 @@ GST_START_TEST (test_merge)
 
   /* clean up */
   if (list)
-    gst_tag_list_free (list);
+    gst_tag_list_unref (list);
   if (list2)
-    gst_tag_list_free (list2);
+    gst_tag_list_unref (list2);
   if (merge)
-    gst_tag_list_free (merge);
+    gst_tag_list_unref (merge);
 }
 
 GST_END_TEST
@@ -271,18 +271,18 @@ GST_START_TEST (test_date_tags)
   gchar *str;
 
   date = g_date_new_dmy (14, 10, 2005);
-  tag_list = gst_tag_list_new ();
+  tag_list = gst_tag_list_new_empty ();
   gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, GST_TAG_DATE, date, NULL);
 
-  str = gst_structure_to_string (tag_list);
+  str = gst_tag_list_to_string (tag_list);
   fail_if (str == NULL);
   fail_if (strstr (str, "2005-10-14") == NULL);
 
-  tag_list2 = gst_structure_from_string (str, NULL);
+  tag_list2 = gst_tag_list_new_from_string (str);
   fail_if (tag_list2 == NULL);
   fail_if (!gst_tag_list_get_date (tag_list2, GST_TAG_DATE, &date2));
   fail_unless (gst_tag_list_is_equal (tag_list2, tag_list));
-  gst_tag_list_free (tag_list2);
+  gst_tag_list_unref (tag_list2);
   g_free (str);
 
   fail_if (g_date_compare (date, date2) != 0);
@@ -294,7 +294,7 @@ GST_START_TEST (test_date_tags)
   fail_if (g_date_get_year (date2) != 2005);
   g_date_free (date2);
 
-  gst_tag_list_free (tag_list);
+  gst_tag_list_unref (tag_list);
   g_date_free (date);
 }
 
@@ -304,24 +304,20 @@ GST_START_TEST (test_type)
 {
   GstTagList *taglist;
 
-  taglist = gst_tag_list_new ();
+  taglist = gst_tag_list_new_empty ();
   fail_unless (GST_IS_TAG_LIST (taglist));
-  fail_unless (gst_is_tag_list (taglist));
-  gst_tag_list_free (taglist);
+  gst_tag_list_unref (taglist);
 
-  /* this isn't okay */
-  ASSERT_CRITICAL (fail_if (gst_is_tag_list (NULL)));
-
-  /* this however should be fine */
+  /* this should be fine */
   fail_if (GST_IS_TAG_LIST (NULL));
 
   /* check gst_tag_list_is_empty */
   ASSERT_CRITICAL (gst_tag_list_is_empty (NULL));
-  taglist = gst_tag_list_new ();
+  taglist = gst_tag_list_new_empty ();
   fail_unless (gst_tag_list_is_empty (taglist));
   gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "JD", NULL);
   fail_if (gst_tag_list_is_empty (taglist));
-  gst_tag_list_free (taglist);
+  gst_tag_list_unref (taglist);
 }
 
 GST_END_TEST;
@@ -331,7 +327,7 @@ GST_START_TEST (test_set_non_utf8_string)
   GstTagList *taglist;
   guint8 foobar[2] = { 0xff, 0x00 };    /* not UTF-8 */
 
-  taglist = gst_tag_list_new ();
+  taglist = gst_tag_list_new_empty ();
   fail_unless (taglist != NULL);
 
   ASSERT_WARNING (gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND,
@@ -340,7 +336,7 @@ GST_START_TEST (test_set_non_utf8_string)
   /* That string field with a non-UTF8 string should not have been added */
   fail_unless (gst_tag_list_is_empty (taglist));
 
-  gst_tag_list_free (taglist);
+  gst_tag_list_unref (taglist);
 }
 
 GST_END_TEST;
@@ -349,38 +345,54 @@ GST_START_TEST (test_buffer_tags)
 {
   GstTagList *tags;
   GstBuffer *buf1, *buf2;
+  GstSample *s1, *s2;
 
-  tags = gst_tag_list_new ();
-  buf1 = gst_buffer_new_and_alloc (222);
-  buf2 = gst_buffer_new_and_alloc (100);
-  gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_IMAGE, buf1,
-      GST_TAG_PREVIEW_IMAGE, buf2, NULL);
-  gst_buffer_unref (buf1);
-  gst_buffer_unref (buf2);
+  tags = gst_tag_list_new_empty ();
 
-  buf1 = buf2 = NULL;
-  fail_if (!gst_tag_list_get_buffer (tags, GST_TAG_IMAGE, &buf1));
+  buf1 = gst_buffer_new_and_alloc (222);
+  s1 = gst_sample_new (buf1, NULL, NULL, NULL);
   gst_buffer_unref (buf1);
-  fail_if (!gst_tag_list_get_buffer (tags, GST_TAG_PREVIEW_IMAGE, &buf2));
-  gst_buffer_unref (buf2);
 
-  fail_if (gst_tag_list_get_buffer_index (tags, GST_TAG_IMAGE, 1, &buf1));
-  fail_if (gst_tag_list_get_buffer_index (tags, GST_TAG_IMAGE, 2, &buf1));
-  fail_if (gst_tag_list_get_buffer_index (tags, GST_TAG_PREVIEW_IMAGE, 1,
-          &buf1));
-  fail_if (gst_tag_list_get_buffer_index (tags, GST_TAG_PREVIEW_IMAGE, 2,
-          &buf1));
-
-  fail_if (!gst_tag_list_get_buffer_index (tags, GST_TAG_IMAGE, 0, &buf1));
-  fail_if (!gst_tag_list_get_buffer_index (tags, GST_TAG_PREVIEW_IMAGE, 0,
-          &buf2));
-  fail_unless_equals_int (GST_BUFFER_SIZE (buf1), 222);
-  fail_unless_equals_int (GST_BUFFER_SIZE (buf2), 100);
-
-  gst_buffer_unref (buf1);
+  buf2 = gst_buffer_new_and_alloc (100);
+  s2 = gst_sample_new (buf2, NULL, NULL, NULL);
   gst_buffer_unref (buf2);
 
-  gst_tag_list_free (tags);
+  gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_IMAGE, s1,
+      GST_TAG_PREVIEW_IMAGE, s2, NULL);
+
+  gst_sample_unref (s1);
+  gst_sample_unref (s2);
+  s1 = s2 = NULL;
+
+  fail_if (!gst_tag_list_get_sample (tags, GST_TAG_IMAGE, &s1));
+  fail_unless (gst_sample_get_buffer (s1) == buf1);
+  gst_sample_unref (s1);
+
+  fail_if (!gst_tag_list_get_sample (tags, GST_TAG_PREVIEW_IMAGE, &s2));
+  fail_unless (gst_sample_get_buffer (s2) == buf2);
+  gst_sample_unref (s2);
+
+  fail_if (gst_tag_list_get_sample_index (tags, GST_TAG_IMAGE, 1, &s1));
+  gst_sample_unref (s1);
+  fail_if (gst_tag_list_get_sample_index (tags, GST_TAG_IMAGE, 2, &s1));
+  gst_sample_unref (s1);
+  fail_if (gst_tag_list_get_sample_index (tags, GST_TAG_PREVIEW_IMAGE, 1, &s1));
+  gst_sample_unref (s1);
+  fail_if (gst_tag_list_get_sample_index (tags, GST_TAG_PREVIEW_IMAGE, 2, &s1));
+  gst_sample_unref (s1);
+
+  fail_if (!gst_tag_list_get_sample_index (tags, GST_TAG_IMAGE, 0, &s1));
+  fail_if (!gst_tag_list_get_sample_index (tags, GST_TAG_PREVIEW_IMAGE, 0,
+          &s2));
+  buf1 = gst_sample_get_buffer (s1);
+  fail_unless_equals_int (gst_buffer_get_size (buf1), 222);
+  buf2 = gst_sample_get_buffer (s2);
+  fail_unless_equals_int (gst_buffer_get_size (buf2), 100);
+
+  gst_sample_unref (s1);
+  gst_sample_unref (s2);
+
+  gst_tag_list_unref (tags);
 }
 
 GST_END_TEST;
@@ -393,13 +405,13 @@ GST_START_TEST (test_empty_tags)
   if (GST_VERSION_NANO != 1)
     return;
 
-  tags = gst_tag_list_new ();
+  tags = gst_tag_list_new_empty ();
   ASSERT_WARNING (gst_tag_list_add (tags, GST_TAG_MERGE_APPEND,
           GST_TAG_ARTIST, NULL, NULL));
   ASSERT_WARNING (gst_tag_list_add (tags, GST_TAG_MERGE_APPEND,
           GST_TAG_ARTIST, "", NULL));
   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "xyz", NULL);
-  gst_tag_list_free (tags);
+  gst_tag_list_unref (tags);
 }
 
 GST_END_TEST;
@@ -411,7 +423,7 @@ GST_START_TEST (test_new_full)
   gdouble track_gain;
   guint track_num;
 
-  tags = gst_tag_list_new_full (GST_TAG_ARTIST, "Arty Ist",
+  tags = gst_tag_list_new (GST_TAG_ARTIST, "Arty Ist",
       GST_TAG_TRACK_NUMBER, 9, GST_TAG_TRACK_GAIN, 4.242, GST_TAG_TITLE,
       "Title!", NULL);
 
@@ -425,7 +437,7 @@ GST_START_TEST (test_new_full)
   fail_unless_equals_float (track_gain, 4.242);
   fail_unless (tags != NULL);
 
-  gst_tag_list_free (tags);
+  gst_tag_list_unref (tags);
   g_free (artist);
   g_free (title);
 }
@@ -437,7 +449,7 @@ GST_START_TEST (test_merge_strings_with_comma)
   GstTagList *tags;
   gchar *artists = NULL;
 
-  tags = gst_tag_list_new ();
+  tags = gst_tag_list_new_empty ();
   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Foo", NULL);
   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Bar", NULL);
   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Yay", NULL);
@@ -448,7 +460,7 @@ GST_START_TEST (test_merge_strings_with_comma)
   fail_unless (strstr (artists, "Bar") != NULL);
   fail_unless (strstr (artists, "Yay") != NULL);
   g_free (artists);
-  gst_tag_list_free (tags);
+  gst_tag_list_unref (tags);
 }
 
 GST_END_TEST;
@@ -456,13 +468,15 @@ GST_END_TEST;
 GST_START_TEST (test_equal)
 {
   GstTagList *tags, *tags2;
+  GstSample *sample1, *sample2, *sample11;
+  GstBuffer *buf;
 
-  tags = gst_tag_list_new ();
+  tags = gst_tag_list_new_empty ();
   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Foo", NULL);
   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Bar", NULL);
   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Yay", NULL);
 
-  tags2 = gst_tag_list_new ();
+  tags2 = gst_tag_list_new_empty ();
   fail_unless (!gst_tag_list_is_equal (tags2, tags));
   gst_tag_list_add (tags2, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Yay", NULL);
   fail_unless (!gst_tag_list_is_equal (tags2, tags));
@@ -484,12 +498,139 @@ GST_START_TEST (test_equal)
       9.87654310, NULL);
   fail_unless (!gst_tag_list_is_equal (tags2, tags));
 
-  gst_tag_list_free (tags);
-  gst_tag_list_free (tags2);
+  gst_tag_list_unref (tags);
+  gst_tag_list_unref (tags2);
+
+  /* samples */
+  buf = gst_buffer_new_wrapped (g_strdup ("test 1-2-3"), 10);
+  sample1 = gst_sample_new (buf, NULL, NULL, NULL);
+  gst_buffer_unref (buf);
+
+  buf = gst_buffer_new_wrapped (g_strdup ("test 1-2-3"), 10);
+  sample11 = gst_sample_new (buf, NULL, NULL, NULL);
+  gst_buffer_unref (buf);
+
+  buf = gst_buffer_new_wrapped (g_strdup ("test 2-3-4-5"), 12);
+  sample2 = gst_sample_new (buf, NULL, NULL, NULL);
+  gst_buffer_unref (buf);
+
+  tags = gst_tag_list_new_empty ();
+  gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_IMAGE, sample1, NULL);
+
+  tags2 = gst_tag_list_new_empty ();
+  fail_unless (!gst_tag_list_is_equal (tags2, tags));
+
+  /* sample sample, should be very equal now */
+  gst_tag_list_add (tags2, GST_TAG_MERGE_APPEND, GST_TAG_IMAGE, sample1, NULL);
+  fail_unless (gst_tag_list_is_equal (tags2, tags));
+  gst_tag_list_unref (tags2);
+
+  /* same buffer content, but different buffer instances, still rather equal */
+  tags2 = gst_tag_list_new_empty ();
+  gst_tag_list_add (tags2, GST_TAG_MERGE_APPEND, GST_TAG_IMAGE, sample11, NULL);
+  fail_unless (gst_tag_list_is_equal (tags2, tags));
+  gst_tag_list_unref (tags2);
+
+  /* different buffer content, should not be equal */
+  tags2 = gst_tag_list_new_empty ();
+  gst_tag_list_add (tags2, GST_TAG_MERGE_APPEND, GST_TAG_IMAGE, sample2, NULL);
+  fail_unless (!gst_tag_list_is_equal (tags2, tags));
+  gst_tag_list_unref (tags2);
+
+  gst_tag_list_unref (tags);
+
+  gst_sample_unref (sample1);
+  gst_sample_unref (sample11);
+  gst_sample_unref (sample2);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_writability)
+{
+  GstTagList *tags, *wtags;
+
+  tags = gst_tag_list_new_empty ();
+  gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Foo", NULL);
+
+  /* take ref, should no longer be writable */
+  gst_tag_list_ref (tags);
+  ASSERT_CRITICAL (gst_tag_list_add (tags, GST_TAG_MERGE_APPEND,
+          GST_TAG_ARTIST, "Bar", NULL));
+
+  wtags = gst_tag_list_make_writable (tags);
+  /* should be ok now */
+  gst_tag_list_add (wtags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Bar", NULL);
+  gst_tag_list_unref (wtags);
+
+  /* this too, since we dropped the second ref in make_writable */
+  gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Bar", NULL);
+  gst_tag_list_unref (tags);
+}
+
+GST_END_TEST;
+
+/* this tests GstSample serialisation/deserialisation, esp. with multiple
+ * samples in a tag list */
+GST_START_TEST (test_serialization)
+{
+  GstTagList *tags, *tags2;
+  GstBuffer *b1, *b2;
+  GstSample *s1, *s2;
+  GstCaps *c2;
+  gchar *s;
+
+  b1 = gst_buffer_new_allocate (NULL, 1, NULL);
+  gst_buffer_memset (b1, 0, 0xb3, -1);
+  s1 = gst_sample_new (b1, NULL, NULL, NULL);
+  gst_buffer_unref (b1);
+
+  b2 = gst_buffer_new_allocate (NULL, 8, NULL);
+  gst_buffer_memset (b2, 0, 0x2f, -1);
+  c2 = gst_caps_new_empty_simple ("foo/bar");
+  s2 = gst_sample_new (b2, c2, NULL, NULL);
+  gst_buffer_unref (b2);
+  gst_caps_unref (c2);
+  c2 = NULL;
+
+  tags = gst_tag_list_new (GST_TAG_ATTACHMENT, s1, NULL);
+  gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ATTACHMENT, s2, NULL);
+  GST_INFO ("tags: %" GST_PTR_FORMAT, tags);
+
+  s = gst_tag_list_to_string (tags);
+  GST_INFO ("taglist -> string: %s", s);
+  tags2 = gst_tag_list_new_from_string (s);
+  GST_INFO ("string -> taglist: %" GST_PTR_FORMAT, tags2);
+  fail_unless (gst_tag_list_is_equal (tags, tags2));
+  gst_tag_list_unref (tags2);
+  g_free (s);
+
+  gst_sample_unref (s1);
+  gst_sample_unref (s2);
+  gst_tag_list_unref (tags);
 }
 
 GST_END_TEST;
 
+GST_START_TEST (test_empty_taglist_serialization)
+{
+  GstTagList *taglist, *taglist2;
+  gchar *str;
+
+  taglist = gst_tag_list_new_empty ();
+  str = gst_tag_list_to_string (taglist);
+  taglist2 = gst_tag_list_new_from_string (str);
+  fail_if (taglist2 == NULL);
+  fail_unless (gst_tag_list_is_equal (taglist, taglist2));
+
+  gst_tag_list_unref (taglist);
+  gst_tag_list_unref (taglist2);
+  g_free (str);
+}
+
+GST_END_TEST;
+
+
 static Suite *
 gst_tag_suite (void)
 {
@@ -508,6 +649,9 @@ gst_tag_suite (void)
   tcase_add_test (tc_chain, test_empty_tags);
   tcase_add_test (tc_chain, test_new_full);
   tcase_add_test (tc_chain, test_equal);
+  tcase_add_test (tc_chain, test_writability);
+  tcase_add_test (tc_chain, test_serialization);
+  tcase_add_test (tc_chain, test_empty_taglist_serialization);
 
   return s;
 }