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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #ifdef HAVE_VALGRIND_H
28 # include <valgrind/valgrind.h>
30 # define RUNNING_ON_VALGRIND FALSE
33 #include <gst/check/gstcheck.h>
35 /* test metadata for PTS/DTS and duration */
42 GstClockTime duration;
43 GstClockTime clock_rate;
46 static GType gst_meta_test_api_get_type (void);
47 #define GST_META_TEST_API_TYPE (gst_meta_test_api_get_type())
49 static const GstMetaInfo *gst_meta_test_get_info (void);
50 #define GST_META_TEST_INFO (gst_meta_test_get_info())
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))
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 */
60 gst_meta_test_init (GstMetaTest * meta)
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;
70 test_init_func (GstMeta * meta, gpointer params, GstBuffer * buffer)
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
80 test_free_func (GstMeta * meta, GstBuffer * buffer)
82 GST_DEBUG ("free called on buffer %p, meta %p", buffer, meta);
83 /* nothing to free really */
87 test_transform_func (GstBuffer * transbuf, GstMeta * meta,
88 GstBuffer * buffer, GQuark type, gpointer data)
90 GstMetaTest *test, *tmeta = (GstMetaTest *) meta;
92 GST_DEBUG ("transform %s called from buffer %p to %p, meta %p",
93 g_quark_to_string (type), buffer, transbuf, meta);
95 if (GST_META_TRANSFORM_IS_COPY (type)) {
96 GstMetaTransformCopy *copy_data = data;
98 test = GST_META_TEST_ADD (transbuf);
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;
109 fail_unless (gst_buffer_get_size (buffer) > copy_data->size);
111 test->duration = GST_CLOCK_TIME_NONE;
114 fail_unless (copy_data->region == TRUE);
119 test->clock_rate = tmeta->clock_rate;
125 gst_meta_test_api_get_type (void)
127 static volatile GType type;
128 static const gchar *tags[] = { "timing", NULL };
130 if (g_once_init_enter (&type)) {
131 GType _type = gst_meta_api_type_register ("GstMetaTestAPI", tags);
132 g_once_init_leave (&type, _type);
137 static const GstMetaInfo *
138 gst_meta_test_get_info (void)
140 static const GstMetaInfo *meta_test_info = NULL;
142 if (meta_test_info == NULL) {
143 meta_test_info = gst_meta_register (GST_META_TEST_API_TYPE,
145 sizeof (GstMetaTest),
146 test_init_func, test_free_func, test_transform_func);
148 return meta_test_info;
151 GST_START_TEST (test_meta_test)
153 GstBuffer *buffer, *copy, *subbuf;
157 buffer = gst_buffer_new_and_alloc (4);
158 fail_if (buffer == NULL);
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);
165 /* add some metadata */
166 meta = GST_META_TEST_ADD (buffer);
167 fail_if (meta == NULL);
168 /* fill some values */
171 meta->duration = 1000;
172 meta->clock_rate = 1000;
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);
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);
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);
208 gst_buffer_unref (buffer);
214 gst_buffermeta_suite (void)
216 Suite *s = suite_create ("GstMeta");
217 TCase *tc_chain = tcase_create ("general");
219 suite_add_tcase (s, tc_chain);
220 tcase_add_test (tc_chain, test_meta_test);
225 GST_CHECK_MAIN (gst_buffermeta);