1 /* GStreamer
2 *
3 * unit test for the taglib-based apev2mux element
4 *
5 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
6 * Copyright (C) 2006 Sebastian Dröge <slomo@circular-chaos.org>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #include <gst/check/gstcheck.h>
25
26 #include <gst/gst.h>
27 #include <string.h>
28
29 #define TEST_ARTIST "Ar T\303\255st"
30 #define TEST_TITLE "M\303\274llermilch!"
31 #define TEST_ALBUM "Boom"
32 #define TEST_DATE g_date_new_dmy(1,1,2006)
33 #define TEST_TRACK_NUMBER 7
34 #define TEST_TRACK_COUNT 19
35 #define TEST_TRACK_GAIN 1.45
36 #define TEST_ALBUM_GAIN 0.78
37
38 /* for dummy mp3 frame sized MP3_FRAME_SIZE bytes,
39 * start: ff fb b0 44 00 00 08 00 00 4b 00 00 00 00 00 00 */
40 static const guint8 mp3_dummyhdr[] = { 0xff, 0xfb, 0xb0, 0x44, 0x00, 0x00,
41 0x08, 0x00, 0x4b, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00
42 };
43
44 #define MP3_FRAME_SIZE 626
45
46 static GstTagList *
test_taglib_apev2mux_create_tags(guint32 mask)47 test_taglib_apev2mux_create_tags (guint32 mask)
48 {
49 GstTagList *tags;
50
51 tags = gst_tag_list_new_empty ();
52
53 if (mask & (1 << 0)) {
54 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
55 GST_TAG_ARTIST, TEST_ARTIST, NULL);
56 }
57 if (mask & (1 << 1)) {
58 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
59 GST_TAG_TITLE, TEST_TITLE, NULL);
60 }
61 if (mask & (1 << 2)) {
62 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
63 GST_TAG_ALBUM, TEST_ALBUM, NULL);
64 }
65 if (mask & (1 << 3)) {
66 GDate *date;
67
68 date = TEST_DATE;
69 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP, GST_TAG_DATE, date, NULL);
70 g_date_free (date);
71 }
72 if (mask & (1 << 4)) {
73 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
74 GST_TAG_TRACK_NUMBER, TEST_TRACK_NUMBER, NULL);
75 }
76 if (mask & (1 << 5)) {
77 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
78 GST_TAG_TRACK_COUNT, TEST_TRACK_COUNT, NULL);
79 }
80 if (mask & (1 << 6)) {
81 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
82 GST_TAG_TRACK_GAIN, TEST_TRACK_GAIN, NULL);
83 }
84 if (mask & (1 << 7)) {
85 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
86 GST_TAG_ALBUM_GAIN, TEST_ALBUM_GAIN, NULL);
87 }
88 if (mask & (1 << 8)) {
89 }
90 if (mask & (1 << 9)) {
91 }
92 if (mask & (1 << 10)) {
93 }
94 if (mask & (1 << 11)) {
95 }
96 if (mask & (1 << 12)) {
97 }
98 if (mask & (1 << 13)) {
99 }
100 return tags;
101 }
102
103 static void
test_taglib_apev2mux_check_tags(GstTagList * tags,guint32 mask)104 test_taglib_apev2mux_check_tags (GstTagList * tags, guint32 mask)
105 {
106 if (mask & (1 << 0)) {
107 gchar *s = NULL;
108
109 fail_unless (gst_tag_list_get_string (tags, GST_TAG_ARTIST, &s));
110 fail_unless (g_str_equal (s, TEST_ARTIST));
111 g_free (s);
112 }
113 if (mask & (1 << 1)) {
114 gchar *s = NULL;
115
116 fail_unless (gst_tag_list_get_string (tags, GST_TAG_TITLE, &s));
117 fail_unless (g_str_equal (s, TEST_TITLE));
118 g_free (s);
119 }
120 if (mask & (1 << 2)) {
121 gchar *s = NULL;
122
123 fail_unless (gst_tag_list_get_string (tags, GST_TAG_ALBUM, &s));
124 fail_unless (g_str_equal (s, TEST_ALBUM));
125 g_free (s);
126 }
127 if (mask & (1 << 3)) {
128 GDate *shouldbe, *date = NULL;
129
130 shouldbe = TEST_DATE;
131 fail_unless (gst_tag_list_get_date (tags, GST_TAG_DATE, &date));
132 fail_unless (g_date_compare (shouldbe, date) == 0);
133 g_date_free (shouldbe);
134 g_date_free (date);
135 }
136 if (mask & (1 << 4)) {
137 guint num;
138
139 fail_unless (gst_tag_list_get_uint (tags, GST_TAG_TRACK_NUMBER, &num));
140 fail_unless (num == TEST_TRACK_NUMBER);
141 }
142 if (mask & (1 << 5)) {
143 guint count;
144
145 fail_unless (gst_tag_list_get_uint (tags, GST_TAG_TRACK_COUNT, &count));
146 fail_unless (count == TEST_TRACK_COUNT);
147 }
148 if (mask & (1 << 6)) {
149 gdouble gain;
150
151 fail_unless (gst_tag_list_get_double (tags, GST_TAG_TRACK_GAIN, &gain));
152 fail_unless (gain == TEST_TRACK_GAIN);
153 }
154 if (mask & (1 << 7)) {
155 gdouble gain;
156
157 fail_unless (gst_tag_list_get_double (tags, GST_TAG_ALBUM_GAIN, &gain));
158 fail_unless (gain == TEST_ALBUM_GAIN);
159 }
160 if (mask & (1 << 8)) {
161 }
162 if (mask & (1 << 9)) {
163 }
164 if (mask & (1 << 10)) {
165 }
166 if (mask & (1 << 11)) {
167 }
168 if (mask & (1 << 12)) {
169 }
170 if (mask & (1 << 13)) {
171 }
172 }
173
174 static void
fill_mp3_buffer(GstElement * fakesrc,GstBuffer * buf,GstPad * pad,guint64 * p_offset)175 fill_mp3_buffer (GstElement * fakesrc, GstBuffer * buf, GstPad * pad,
176 guint64 * p_offset)
177 {
178 gsize size;
179
180 size = gst_buffer_get_size (buf);
181
182 fail_unless (size == MP3_FRAME_SIZE);
183
184 GST_LOG ("filling buffer with fake mp3 data, offset = %" G_GUINT64_FORMAT,
185 *p_offset);
186
187 gst_buffer_fill (buf, 0, mp3_dummyhdr, sizeof (mp3_dummyhdr));
188
189 #if 0
190 /* can't use gst_buffer_set_caps() here because the metadata isn't writable
191 * because of the extra refcounts taken by the signal emission mechanism;
192 * we know it's fine to use GST_BUFFER_CAPS() here though */
193 GST_BUFFER_CAPS (buf) = gst_caps_new_simple ("audio/mpeg", "mpegversion",
194 G_TYPE_INT, 1, "layer", G_TYPE_INT, 3, NULL);
195 #endif
196
197 GST_BUFFER_OFFSET (buf) = *p_offset;
198 *p_offset += size;
199 }
200
201 static void
got_buffer(GstElement * fakesink,GstBuffer * buf,GstPad * pad,GstBuffer ** p_buf)202 got_buffer (GstElement * fakesink, GstBuffer * buf, GstPad * pad,
203 GstBuffer ** p_buf)
204 {
205 gint64 off;
206 GstMapInfo map;
207
208 off = GST_BUFFER_OFFSET (buf);
209
210 gst_buffer_map (buf, &map, GST_MAP_READ);
211
212 GST_LOG ("size=%" G_GSIZE_FORMAT ", offset=%" G_GINT64_FORMAT, map.size, off);
213
214 fail_unless (GST_BUFFER_OFFSET_IS_VALID (buf));
215
216 if (*p_buf == NULL || (off + map.size) > gst_buffer_get_size (*p_buf)) {
217 GstBuffer *newbuf;
218
219 /* not very elegant, but who cares */
220 newbuf = gst_buffer_new_and_alloc (off + map.size);
221 if (*p_buf) {
222 GstMapInfo pmap;
223
224 gst_buffer_map (*p_buf, &pmap, GST_MAP_READ);
225 gst_buffer_fill (newbuf, 0, pmap.data, pmap.size);
226 gst_buffer_unmap (*p_buf, &pmap);
227 }
228 gst_buffer_fill (newbuf, off, map.data, map.size);
229 if (*p_buf)
230 gst_buffer_unref (*p_buf);
231 *p_buf = newbuf;
232 } else {
233 gst_buffer_fill (*p_buf, off, map.data, map.size);
234 }
235 gst_buffer_unmap (buf, &map);
236 }
237
238 static void
test_taglib_apev2mux_check_output_buffer(GstBuffer * buf)239 test_taglib_apev2mux_check_output_buffer (GstBuffer * buf)
240 {
241 GstMapInfo map;
242 guint off;
243
244 gst_buffer_map (buf, &map, GST_MAP_READ);
245 g_assert (map.size % MP3_FRAME_SIZE == 0);
246
247 for (off = 0; off < map.size; off += MP3_FRAME_SIZE) {
248 fail_unless (memcmp (map.data + off, mp3_dummyhdr,
249 sizeof (mp3_dummyhdr)) == 0);
250 }
251 gst_buffer_unmap (buf, &map);
252 }
253
254 static void
test_taglib_apev2mux_with_tags(GstTagList * tags,guint32 mask)255 test_taglib_apev2mux_with_tags (GstTagList * tags, guint32 mask)
256 {
257 GstMessage *msg;
258 GstTagList *tags_read = NULL;
259 GstElement *pipeline, *apev2mux, *apedemux, *fakesrc, *fakesink;
260 GstBus *bus;
261 guint64 offset;
262 GstBuffer *outbuf = NULL;
263
264 pipeline = gst_pipeline_new ("pipeline");
265 g_assert (pipeline != NULL);
266
267 fakesrc = gst_element_factory_make ("fakesrc", "fakesrc");
268 g_assert (fakesrc != NULL);
269
270 apev2mux = gst_element_factory_make ("apev2mux", "apev2mux");
271 g_assert (apev2mux != NULL);
272
273 apedemux = gst_element_factory_make ("apedemux", "apedemux");
274 g_assert (apedemux != NULL);
275
276 fakesink = gst_element_factory_make ("fakesink", "fakesink");
277 g_assert (fakesink != NULL);
278
279 /* set up sink */
280 outbuf = NULL;
281 g_object_set (fakesink, "signal-handoffs", TRUE, NULL);
282 g_signal_connect (fakesink, "handoff", G_CALLBACK (got_buffer), &outbuf);
283
284 gst_bin_add (GST_BIN (pipeline), fakesrc);
285 gst_bin_add (GST_BIN (pipeline), apev2mux);
286 gst_bin_add (GST_BIN (pipeline), apedemux);
287 gst_bin_add (GST_BIN (pipeline), fakesink);
288
289 gst_tag_setter_merge_tags (GST_TAG_SETTER (apev2mux), tags,
290 GST_TAG_MERGE_APPEND);
291
292 gst_element_link_many (fakesrc, apev2mux, apedemux, fakesink, NULL);
293
294 /* set up source */
295 g_object_set (fakesrc, "signal-handoffs", TRUE, "can-activate-pull", FALSE,
296 "filltype", 2, "sizetype", 2, "sizemax", MP3_FRAME_SIZE,
297 "num-buffers", 16, NULL);
298
299 offset = 0;
300 g_signal_connect (fakesrc, "handoff", G_CALLBACK (fill_mp3_buffer), &offset);
301
302 gst_element_set_state (pipeline, GST_STATE_PLAYING);
303 fail_unless (gst_element_get_state (pipeline, NULL, NULL,
304 -1) == GST_STATE_CHANGE_SUCCESS);
305
306 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
307
308 GST_LOG ("Waiting for tag ...");
309 msg =
310 gst_bus_poll (bus, GST_MESSAGE_TAG | GST_MESSAGE_EOS | GST_MESSAGE_ERROR,
311 -1);
312 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
313 GError *err;
314 gchar *dbg;
315
316 gst_message_parse_error (msg, &err, &dbg);
317 g_printerr ("ERROR from element %s: %s\n%s\n",
318 GST_OBJECT_NAME (msg->src), err->message, GST_STR_NULL (dbg));
319 g_error_free (err);
320 g_free (dbg);
321 } else if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS) {
322 g_printerr ("EOS message, but were waiting for TAGS!\n");
323 }
324 fail_unless (msg->type == GST_MESSAGE_TAG);
325
326 gst_message_parse_tag (msg, &tags_read);
327 gst_message_unref (msg);
328
329 GST_LOG ("Got tags: %" GST_PTR_FORMAT, tags_read);
330 test_taglib_apev2mux_check_tags (tags_read, mask);
331 gst_tag_list_unref (tags_read);
332
333 GST_LOG ("Waiting for EOS ...");
334 msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
335 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
336 GError *err;
337 gchar *dbg;
338
339 gst_message_parse_error (msg, &err, &dbg);
340 g_printerr ("ERROR from element %s: %s\n%s\n",
341 GST_OBJECT_NAME (msg->src), err->message, GST_STR_NULL (dbg));
342 g_error_free (err);
343 g_free (dbg);
344 }
345 fail_unless (msg->type == GST_MESSAGE_EOS);
346 gst_message_unref (msg);
347
348 gst_object_unref (bus);
349
350 GST_LOG ("Got EOS, shutting down ...");
351 gst_element_set_state (pipeline, GST_STATE_NULL);
352 gst_object_unref (pipeline);
353
354 test_taglib_apev2mux_check_output_buffer (outbuf);
355 gst_buffer_unref (outbuf);
356
357 GST_LOG ("Done");
358 }
359
GST_START_TEST(test_apev2mux)360 GST_START_TEST (test_apev2mux)
361 {
362 GstTagList *tags;
363 gint i;
364
365 g_random_set_seed (247166295);
366
367 /* internal consistency check */
368 tags = test_taglib_apev2mux_create_tags (0xFFFFFFFF);
369 test_taglib_apev2mux_check_tags (tags, 0xFFFFFFFF);
370 gst_tag_list_unref (tags);
371
372 /* now the real tests */
373 for (i = 0; i < 50; ++i) {
374 guint32 mask;
375
376 mask = g_random_int ();
377 GST_LOG ("tag mask = %08x (i=%d)", mask, i);
378
379 if (mask == 0)
380 continue;
381
382 /* create tags */
383 tags = test_taglib_apev2mux_create_tags (mask);
384 GST_LOG ("tags for mask %08x = %" GST_PTR_FORMAT, mask, tags);
385
386 /* double-check for internal consistency */
387 test_taglib_apev2mux_check_tags (tags, mask);
388
389 /* test with pipeline */
390 test_taglib_apev2mux_with_tags (tags, mask);
391
392 /* free tags */
393 gst_tag_list_unref (tags);
394 }
395 }
396
397 GST_END_TEST;
398
399 static Suite *
apev2mux_suite(void)400 apev2mux_suite (void)
401 {
402 Suite *s = suite_create ("apev2mux");
403 TCase *tc_chain = tcase_create ("general");
404
405 suite_add_tcase (s, tc_chain);
406 tcase_add_test (tc_chain, test_apev2mux);
407
408 return s;
409 }
410
411 GST_CHECK_MAIN (apev2mux);
412