Merge branch 'master' into 0.11
[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 const GstMetaInfo *gst_meta_test_get_info (void);
47 #define GST_META_TEST_INFO (gst_meta_test_get_info())
48
49 #define GST_META_TEST_GET(buf) ((GstMetaTest *)gst_buffer_get_meta(buf,GST_META_TEST_INFO))
50 #define GST_META_TEST_ADD(buf) ((GstMetaTest *)gst_buffer_add_meta(buf,GST_META_TEST_INFO,NULL))
51
52 #if 0
53 /* unused currently. This is a user function to fill the metadata with default
54  * values. We don't call this from the init function because the user is mostly
55  * likely going to override the values immediately after */
56 static void
57 gst_meta_test_init (GstMetaTest * meta)
58 {
59   meta->pts = GST_CLOCK_TIME_NONE;
60   meta->dts = GST_CLOCK_TIME_NONE;
61   meta->duration = GST_CLOCK_TIME_NONE;
62   meta->clock_rate = GST_SECOND;
63 }
64 #endif
65
66 static void
67 test_init_func (GstMetaTest * meta, GstBuffer * buffer)
68 {
69   GST_DEBUG ("init called on buffer %p, meta %p", buffer, meta);
70   /* nothing to init really, the init function is mostly for allocating
71    * additional memory or doing special setup as part of adding the metadata to
72    * the buffer*/
73 }
74
75 static void
76 test_free_func (GstMetaTest * meta, GstBuffer * buffer)
77 {
78   GST_DEBUG ("free called on buffer %p, meta %p", buffer, meta);
79   /* nothing to free really */
80 }
81
82 static void
83 test_copy_func (GstBuffer * copybuf, GstMetaTest * meta,
84     GstBuffer * buffer, gsize offset, gsize size)
85 {
86   GstMetaTest *test;
87
88   GST_DEBUG ("copy called from buffer %p to %p, meta %p, %u-%u", buffer,
89       copybuf, meta, offset, size);
90
91   test = GST_META_TEST_ADD (copybuf);
92   if (offset == 0) {
93     /* same offset, copy timestamps */
94     test->pts = meta->pts;
95     test->dts = meta->dts;
96     if (size == gst_buffer_get_size (buffer)) {
97       /* same size, copy duration */
98       test->duration = meta->duration;
99     } else {
100       /* else clear */
101       test->duration = GST_CLOCK_TIME_NONE;
102     }
103   } else {
104     test->pts = -1;
105     test->dts = -1;
106     test->duration = -1;
107   }
108   test->clock_rate = meta->clock_rate;
109 }
110
111 static const GstMetaInfo *
112 gst_meta_test_get_info (void)
113 {
114   static const GstMetaInfo *meta_test_info = NULL;
115
116   if (meta_test_info == NULL) {
117     meta_test_info = gst_meta_register ("GstMetaTest", "GstMetaTest",
118         sizeof (GstMetaTest),
119         (GstMetaInitFunction) test_init_func,
120         (GstMetaFreeFunction) test_free_func,
121         (GstMetaCopyFunction) test_copy_func,
122         (GstMetaTransformFunction) NULL, NULL, NULL);
123   }
124   return meta_test_info;
125 }
126
127 GST_START_TEST (test_meta_test)
128 {
129   GstBuffer *buffer, *copy, *subbuf;
130   GstMetaTest *meta;
131   gpointer data;
132
133   buffer = gst_buffer_new_and_alloc (4);
134
135   data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_WRITE);
136   memset (data, 0, 4);
137   gst_buffer_unmap (buffer, data, 4);
138
139   /* add some metadata */
140   meta = GST_META_TEST_ADD (buffer);
141   fail_if (meta == NULL);
142   /* fill some values */
143   meta->pts = 1000;
144   meta->dts = 2000;
145   meta->duration = 1000;
146   meta->clock_rate = 1000;
147
148   /* copy of the buffer */
149   copy = gst_buffer_copy (buffer);
150   /* get metadata of the buffer */
151   meta = GST_META_TEST_GET (copy);
152   fail_if (meta == NULL);
153   fail_if (meta->pts != 1000);
154   fail_if (meta->dts != 2000);
155   fail_if (meta->duration != 1000);
156   fail_if (meta->clock_rate != 1000);
157   gst_buffer_unref (copy);
158
159   /* make subbuffer */
160   subbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 0, 1);
161   /* get metadata of the buffer */
162   meta = GST_META_TEST_GET (subbuf);
163   fail_if (meta == NULL);
164   fail_if (meta->pts != 1000);
165   fail_if (meta->dts != 2000);
166   fail_if (meta->duration != -1);
167   fail_if (meta->clock_rate != 1000);
168   gst_buffer_unref (subbuf);
169
170   /* make another subbuffer */
171   subbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 1, 3);
172   /* get metadata of the buffer */
173   meta = GST_META_TEST_GET (subbuf);
174   fail_if (meta == NULL);
175   fail_if (meta->pts != -1);
176   fail_if (meta->dts != -1);
177   fail_if (meta->duration != -1);
178   fail_if (meta->clock_rate != 1000);
179   gst_buffer_unref (subbuf);
180
181   /* clean up */
182   gst_buffer_unref (buffer);
183 }
184
185 GST_END_TEST;
186
187 static Suite *
188 gst_buffermeta_suite (void)
189 {
190   Suite *s = suite_create ("GstMeta");
191   TCase *tc_chain = tcase_create ("general");
192
193   suite_add_tcase (s, tc_chain);
194   tcase_add_test (tc_chain, test_meta_test);
195
196   return s;
197 }
198
199 GST_CHECK_MAIN (gst_buffermeta);