3 * unit test for GstMeta
5 * Copyright (C) <2009> Wim Taymans <wim.taymans@gmail.com>
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.
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.
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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
27 #include <gst/check/gstcheck.h>
29 /* test metadata for PTS/DTS and duration */
36 GstClockTime duration;
37 GstClockTime clock_rate;
40 static GType gst_meta_test_api_get_type (void);
41 #define GST_META_TEST_API_TYPE (gst_meta_test_api_get_type())
43 static const GstMetaInfo *gst_meta_test_get_info (void);
44 #define GST_META_TEST_INFO (gst_meta_test_get_info())
46 #define GST_META_TEST_GET(buf) ((GstMetaTest *)gst_buffer_get_meta(buf,GST_META_TEST_API_TYPE))
47 #define GST_META_TEST_ADD(buf) ((GstMetaTest *)gst_buffer_add_meta(buf,GST_META_TEST_INFO,NULL))
50 /* unused currently. This is a user function to fill the metadata with default
51 * values. We don't call this from the init function because the user is mostly
52 * likely going to override the values immediately after */
54 gst_meta_test_init (GstMetaTest * meta)
56 meta->pts = GST_CLOCK_TIME_NONE;
57 meta->dts = GST_CLOCK_TIME_NONE;
58 meta->duration = GST_CLOCK_TIME_NONE;
59 meta->clock_rate = GST_SECOND;
64 test_init_func (GstMeta * meta, gpointer params, GstBuffer * buffer)
66 GST_DEBUG ("init called on buffer %p, meta %p", buffer, meta);
67 /* nothing to init really, the init function is mostly for allocating
68 * additional memory or doing special setup as part of adding the metadata to
74 test_free_func (GstMeta * meta, GstBuffer * buffer)
76 GST_DEBUG ("free called on buffer %p, meta %p", buffer, meta);
77 /* nothing to free really */
81 test_transform_func (GstBuffer * transbuf, GstMeta * meta,
82 GstBuffer * buffer, GQuark type, gpointer data)
84 GstMetaTest *test, *tmeta = (GstMetaTest *) meta;
86 GST_DEBUG ("transform %s called from buffer %p to %p, meta %p",
87 g_quark_to_string (type), buffer, transbuf, meta);
89 if (GST_META_TRANSFORM_IS_COPY (type)) {
90 GstMetaTransformCopy *copy_data = data;
92 test = GST_META_TEST_ADD (transbuf);
94 if (copy_data->offset == 0) {
95 /* same offset, copy timestamps */
96 test->pts = tmeta->pts;
97 test->dts = tmeta->dts;
98 if (!copy_data->region) {
99 fail_unless (gst_buffer_get_size (buffer) == copy_data->size);
100 /* same size, copy duration */
101 test->duration = tmeta->duration;
103 fail_unless (gst_buffer_get_size (buffer) > copy_data->size);
105 test->duration = GST_CLOCK_TIME_NONE;
108 fail_unless (copy_data->region == TRUE);
113 test->clock_rate = tmeta->clock_rate;
115 /* return FALSE, if transform type is not supported */
122 gst_meta_test_api_get_type (void)
124 static volatile GType type;
125 static const gchar *tags[] = { "timing", NULL };
127 if (g_once_init_enter (&type)) {
128 GType _type = gst_meta_api_type_register ("GstMetaTestAPI", tags);
129 g_once_init_leave (&type, _type);
134 static const GstMetaInfo *
135 gst_meta_test_get_info (void)
137 static const GstMetaInfo *meta_test_info = NULL;
139 if (g_once_init_enter (&meta_test_info)) {
140 const GstMetaInfo *mi = gst_meta_register (GST_META_TEST_API_TYPE,
142 sizeof (GstMetaTest),
143 test_init_func, test_free_func, test_transform_func);
144 g_once_init_leave (&meta_test_info, mi);
146 return meta_test_info;
149 GST_START_TEST (test_meta_test)
151 GstBuffer *buffer, *copy, *subbuf;
155 buffer = gst_buffer_new_and_alloc (4);
156 fail_if (buffer == NULL);
158 fail_unless (gst_buffer_map (buffer, &info, GST_MAP_WRITE));
159 fail_if (info.data == NULL);
160 memset (info.data, 0, 4);
161 gst_buffer_unmap (buffer, &info);
163 /* add some metadata */
164 meta = GST_META_TEST_ADD (buffer);
165 fail_if (meta == NULL);
166 /* fill some values */
169 meta->duration = 1000;
170 meta->clock_rate = 1000;
172 /* copy of the buffer */
173 copy = gst_buffer_copy (buffer);
174 /* get metadata of the buffer */
175 meta = GST_META_TEST_GET (copy);
176 fail_if (meta == NULL);
177 fail_if (meta->pts != 1000);
178 fail_if (meta->dts != 2000);
179 fail_if (meta->duration != 1000);
180 fail_if (meta->clock_rate != 1000);
181 gst_buffer_unref (copy);
184 subbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 0, 1);
185 /* get metadata of the buffer */
186 meta = GST_META_TEST_GET (subbuf);
187 fail_if (meta == NULL);
188 fail_if (meta->pts != 1000);
189 fail_if (meta->dts != 2000);
190 fail_if (meta->duration != -1);
191 fail_if (meta->clock_rate != 1000);
192 gst_buffer_unref (subbuf);
194 /* make another subbuffer */
195 subbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 1, 3);
196 /* get metadata of the buffer */
197 meta = GST_META_TEST_GET (subbuf);
198 fail_if (meta == NULL);
199 fail_if (meta->pts != -1);
200 fail_if (meta->dts != -1);
201 fail_if (meta->duration != -1);
202 fail_if (meta->clock_rate != 1000);
203 gst_buffer_unref (subbuf);
206 gst_buffer_unref (buffer);
212 foreach_meta (GstBuffer * buffer, GstMeta ** meta, gpointer user_data)
219 GST_START_TEST (test_meta_locked)
224 buffer = gst_buffer_new_and_alloc (4);
225 fail_if (buffer == NULL);
227 /* add some metadata */
228 meta = GST_META_TEST_ADD (buffer);
229 fail_if (meta == NULL);
230 GST_META_FLAG_SET (meta, GST_META_FLAG_LOCKED);
232 ASSERT_CRITICAL (gst_buffer_remove_meta (buffer, (GstMeta *) meta));
233 ASSERT_CRITICAL (gst_buffer_foreach_meta (buffer, foreach_meta, NULL));
235 GST_META_FLAG_UNSET (meta, GST_META_FLAG_LOCKED);
237 gst_buffer_remove_meta (buffer, (GstMeta *) meta);
240 gst_buffer_unref (buffer);
246 gst_buffermeta_suite (void)
248 Suite *s = suite_create ("GstMeta");
249 TCase *tc_chain = tcase_create ("general");
251 suite_add_tcase (s, tc_chain);
252 tcase_add_test (tc_chain, test_meta_test);
253 tcase_add_test (tc_chain, test_meta_locked);
258 GST_CHECK_MAIN (gst_buffermeta);