caps: improve _do_simplify
[platform/upstream/gstreamer.git] / tests / check / gst / gstmeta.c
1 /* GStreamer
2  *
3  * unit test for GstMeta
4  *
5  * Copyright (C) <2009> Wim Taymans <wim.taymans@gmail.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #ifdef HAVE_VALGRIND_H
28 # include <valgrind/valgrind.h>
29 #else
30 # define RUNNING_ON_VALGRIND FALSE
31 #endif
32
33 #include <gst/check/gstcheck.h>
34
35 /* test metadata for PTS/DTS and duration */
36 typedef struct
37 {
38   GstMeta meta;
39
40   GstClockTime pts;
41   GstClockTime dts;
42   GstClockTime duration;
43   GstClockTime clock_rate;
44 } GstMetaTest;
45
46 static GType gst_meta_test_api_get_type (void);
47 #define GST_META_TEST_API_TYPE (gst_meta_test_api_get_type())
48
49 static const GstMetaInfo *gst_meta_test_get_info (void);
50 #define GST_META_TEST_INFO (gst_meta_test_get_info())
51
52 #define GST_META_TEST_GET(buf) ((GstMetaTest *)gst_buffer_get_meta(buf,GST_META_TEST_API_TYPE))
53 #define GST_META_TEST_ADD(buf) ((GstMetaTest *)gst_buffer_add_meta(buf,GST_META_TEST_INFO,NULL))
54
55 #if 0
56 /* unused currently. This is a user function to fill the metadata with default
57  * values. We don't call this from the init function because the user is mostly
58  * likely going to override the values immediately after */
59 static void
60 gst_meta_test_init (GstMetaTest * meta)
61 {
62   meta->pts = GST_CLOCK_TIME_NONE;
63   meta->dts = GST_CLOCK_TIME_NONE;
64   meta->duration = GST_CLOCK_TIME_NONE;
65   meta->clock_rate = GST_SECOND;
66 }
67 #endif
68
69 static gboolean
70 test_init_func (GstMeta * meta, gpointer params, GstBuffer * buffer)
71 {
72   GST_DEBUG ("init called on buffer %p, meta %p", buffer, meta);
73   /* nothing to init really, the init function is mostly for allocating
74    * additional memory or doing special setup as part of adding the metadata to
75    * the buffer*/
76   return TRUE;
77 }
78
79 static void
80 test_free_func (GstMeta * meta, GstBuffer * buffer)
81 {
82   GST_DEBUG ("free called on buffer %p, meta %p", buffer, meta);
83   /* nothing to free really */
84 }
85
86 static gboolean
87 test_transform_func (GstBuffer * transbuf, GstMeta * meta,
88     GstBuffer * buffer, GQuark type, gpointer data)
89 {
90   GstMetaTest *test, *tmeta = (GstMetaTest *) meta;
91
92   GST_DEBUG ("transform %s called from buffer %p to %p, meta %p",
93       g_quark_to_string (type), buffer, transbuf, meta);
94
95   if (GST_META_TRANSFORM_IS_COPY (type)) {
96     GstMetaTransformCopy *copy_data = data;
97
98     test = GST_META_TEST_ADD (transbuf);
99
100     if (copy_data->offset == 0) {
101       /* same offset, copy timestamps */
102       test->pts = tmeta->pts;
103       test->dts = tmeta->dts;
104       if (!copy_data->region) {
105         fail_unless (gst_buffer_get_size (buffer) == copy_data->size);
106         /* same size, copy duration */
107         test->duration = tmeta->duration;
108       } else {
109         fail_unless (gst_buffer_get_size (buffer) > copy_data->size);
110         /* else clear */
111         test->duration = GST_CLOCK_TIME_NONE;
112       }
113     } else {
114       fail_unless (copy_data->region == TRUE);
115       test->pts = -1;
116       test->dts = -1;
117       test->duration = -1;
118     }
119     test->clock_rate = tmeta->clock_rate;
120   }
121   return TRUE;
122 }
123
124 static GType
125 gst_meta_test_api_get_type (void)
126 {
127   static volatile GType type;
128   static const gchar *tags[] = { "timing", NULL };
129
130   if (g_once_init_enter (&type)) {
131     GType _type = gst_meta_api_type_register ("GstMetaTestAPI", tags);
132     g_once_init_leave (&type, _type);
133   }
134   return type;
135 }
136
137 static const GstMetaInfo *
138 gst_meta_test_get_info (void)
139 {
140   static const GstMetaInfo *meta_test_info = NULL;
141
142   if (meta_test_info == NULL) {
143     meta_test_info = gst_meta_register (GST_META_TEST_API_TYPE,
144         "GstMetaTest",
145         sizeof (GstMetaTest),
146         test_init_func, test_free_func, test_transform_func);
147   }
148   return meta_test_info;
149 }
150
151 GST_START_TEST (test_meta_test)
152 {
153   GstBuffer *buffer, *copy, *subbuf;
154   GstMetaTest *meta;
155   GstMapInfo info;
156
157   buffer = gst_buffer_new_and_alloc (4);
158   fail_if (buffer == NULL);
159
160   fail_unless (gst_buffer_map (buffer, &info, GST_MAP_WRITE));
161   fail_if (info.data == NULL);
162   memset (info.data, 0, 4);
163   gst_buffer_unmap (buffer, &info);
164
165   /* add some metadata */
166   meta = GST_META_TEST_ADD (buffer);
167   fail_if (meta == NULL);
168   /* fill some values */
169   meta->pts = 1000;
170   meta->dts = 2000;
171   meta->duration = 1000;
172   meta->clock_rate = 1000;
173
174   /* copy of the buffer */
175   copy = gst_buffer_copy (buffer);
176   /* get metadata of the buffer */
177   meta = GST_META_TEST_GET (copy);
178   fail_if (meta == NULL);
179   fail_if (meta->pts != 1000);
180   fail_if (meta->dts != 2000);
181   fail_if (meta->duration != 1000);
182   fail_if (meta->clock_rate != 1000);
183   gst_buffer_unref (copy);
184
185   /* make subbuffer */
186   subbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 0, 1);
187   /* get metadata of the buffer */
188   meta = GST_META_TEST_GET (subbuf);
189   fail_if (meta == NULL);
190   fail_if (meta->pts != 1000);
191   fail_if (meta->dts != 2000);
192   fail_if (meta->duration != -1);
193   fail_if (meta->clock_rate != 1000);
194   gst_buffer_unref (subbuf);
195
196   /* make another subbuffer */
197   subbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 1, 3);
198   /* get metadata of the buffer */
199   meta = GST_META_TEST_GET (subbuf);
200   fail_if (meta == NULL);
201   fail_if (meta->pts != -1);
202   fail_if (meta->dts != -1);
203   fail_if (meta->duration != -1);
204   fail_if (meta->clock_rate != 1000);
205   gst_buffer_unref (subbuf);
206
207   /* clean up */
208   gst_buffer_unref (buffer);
209 }
210
211 GST_END_TEST;
212
213 static Suite *
214 gst_buffermeta_suite (void)
215 {
216   Suite *s = suite_create ("GstMeta");
217   TCase *tc_chain = tcase_create ("general");
218
219   suite_add_tcase (s, tc_chain);
220   tcase_add_test (tc_chain, test_meta_test);
221
222   return s;
223 }
224
225 GST_CHECK_MAIN (gst_buffermeta);