Merge remote-tracking branch 'origin/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, (GstMetaTransformFunction) NULL);
122   }
123   return meta_test_info;
124 }
125
126 GST_START_TEST (test_meta_test)
127 {
128   GstBuffer *buffer, *copy, *subbuf;
129   GstMetaTest *meta;
130   gpointer data;
131
132   buffer = gst_buffer_new_and_alloc (4);
133   fail_if (buffer == NULL);
134
135   data = gst_buffer_map (buffer, NULL, NULL, GST_MAP_WRITE);
136   fail_if (data == NULL);
137   memset (data, 0, 4);
138   gst_buffer_unmap (buffer, data, 4);
139
140   /* add some metadata */
141   meta = GST_META_TEST_ADD (buffer);
142   fail_if (meta == NULL);
143   /* fill some values */
144   meta->pts = 1000;
145   meta->dts = 2000;
146   meta->duration = 1000;
147   meta->clock_rate = 1000;
148
149   /* copy of the buffer */
150   copy = gst_buffer_copy (buffer);
151   /* get metadata of the buffer */
152   meta = GST_META_TEST_GET (copy);
153   fail_if (meta == NULL);
154   fail_if (meta->pts != 1000);
155   fail_if (meta->dts != 2000);
156   fail_if (meta->duration != 1000);
157   fail_if (meta->clock_rate != 1000);
158   gst_buffer_unref (copy);
159
160   /* make subbuffer */
161   subbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 0, 1);
162   /* get metadata of the buffer */
163   meta = GST_META_TEST_GET (subbuf);
164   fail_if (meta == NULL);
165   fail_if (meta->pts != 1000);
166   fail_if (meta->dts != 2000);
167   fail_if (meta->duration != -1);
168   fail_if (meta->clock_rate != 1000);
169   gst_buffer_unref (subbuf);
170
171   /* make another subbuffer */
172   subbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 1, 3);
173   /* get metadata of the buffer */
174   meta = GST_META_TEST_GET (subbuf);
175   fail_if (meta == NULL);
176   fail_if (meta->pts != -1);
177   fail_if (meta->dts != -1);
178   fail_if (meta->duration != -1);
179   fail_if (meta->clock_rate != 1000);
180   gst_buffer_unref (subbuf);
181
182   /* clean up */
183   gst_buffer_unref (buffer);
184 }
185
186 GST_END_TEST;
187
188 static Suite *
189 gst_buffermeta_suite (void)
190 {
191   Suite *s = suite_create ("GstMeta");
192   TCase *tc_chain = tcase_create ("general");
193
194   suite_add_tcase (s, tc_chain);
195   tcase_add_test (tc_chain, test_meta_test);
196
197   return s;
198 }
199
200 GST_CHECK_MAIN (gst_buffermeta);