blob: 0a47f1d8382a7c4b795fe592c9824feb514760e5 [file] [log] [blame]
Olivier Naudan988c6f02012-04-16 07:16:25 -04001/* GStreamer
2 *
3 * unit test for the taglib-based id3v2mux element
4 *
5 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
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
Sebastian Dröge5f0bab02013-07-14 11:42:29 +020019 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
Olivier Naudan988c6f02012-04-16 07:16:25 -040021 */
22
23#include <gst/check/gstcheck.h>
24
25#include <gst/gst.h>
26#include <string.h>
27
28#define TEST_ARTIST "Ar T\303\255st"
29#define TEST_TITLE "M\303\274llermilch!"
30#define TEST_ALBUM "Boom"
31#define TEST_DATE g_date_new_dmy(1,1,2006)
32#define TEST_TRACK_NUMBER 7
33#define TEST_TRACK_COUNT 19
34#define TEST_VOLUME_NUMBER 2
35#define TEST_VOLUME_COUNT 3
36#define TEST_TRACK_GAIN 1.45
37#define TEST_ALBUM_GAIN 0.78
38#define TEST_TRACK_PEAK 0.83
39#define TEST_ALBUM_PEAK 0.18
40#define TEST_BPM 113.0
41
42/* for dummy mp3 frame sized MP3_FRAME_SIZE bytes,
43 * start: ff fb b0 44 00 00 08 00 00 4b 00 00 00 00 00 00 */
44static const guint8 mp3_dummyhdr[] = { 0xff, 0xfb, 0xb0, 0x44, 0x00, 0x00,
45 0x08, 0x00, 0x4b, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00
46};
47
48#define MP3_FRAME_SIZE 626
49
50/* the peak and gain values are stored pretty roughly, so check that they're
51 * within 2% of the expected value.
52 */
53#define fail_unless_sorta_equals_float(a, b) \
54G_STMT_START { \
55 double first = a; \
56 double second = b; \
57 fail_unless(fabs (first - second) < (0.02 * fabs (first)), \
58 "'" #a "' (%g) is not equal to '" #b "' (%g)", first, second); \
59} G_STMT_END;
60
61
62static GstTagList *
63test_taglib_id3mux_create_tags (guint32 mask)
64{
65 GstTagList *tags;
66
67 tags = gst_tag_list_new_empty ();
68
69 if (mask & (1 << 0)) {
70 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
71 GST_TAG_ARTIST, TEST_ARTIST, NULL);
72 }
73 if (mask & (1 << 1)) {
74 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
75 GST_TAG_TITLE, TEST_TITLE, NULL);
76 }
77 if (mask & (1 << 2)) {
78 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
79 GST_TAG_ALBUM, TEST_ALBUM, NULL);
80 }
81 if (mask & (1 << 3)) {
82 GDate *date;
83
84 date = TEST_DATE;
85 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP, GST_TAG_DATE, date, NULL);
86 g_date_free (date);
87 }
88 if (mask & (1 << 4)) {
89 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
90 GST_TAG_TRACK_NUMBER, TEST_TRACK_NUMBER, NULL);
91 }
92 if (mask & (1 << 5)) {
93 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
94 GST_TAG_TRACK_COUNT, TEST_TRACK_COUNT, NULL);
95 }
96 if (mask & (1 << 6)) {
97 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
98 GST_TAG_ALBUM_VOLUME_NUMBER, TEST_VOLUME_NUMBER, NULL);
99 }
100 if (mask & (1 << 7)) {
101 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
102 GST_TAG_ALBUM_VOLUME_COUNT, TEST_VOLUME_COUNT, NULL);
103 }
104 if (mask & (1 << 8)) {
105 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
106 GST_TAG_TRACK_GAIN, TEST_TRACK_GAIN, NULL);
107 }
108 if (mask & (1 << 9)) {
109 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
110 GST_TAG_ALBUM_GAIN, TEST_ALBUM_GAIN, NULL);
111 }
112 if (mask & (1 << 10)) {
113 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
114 GST_TAG_TRACK_PEAK, TEST_TRACK_PEAK, NULL);
115 }
116 if (mask & (1 << 11)) {
117 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
118 GST_TAG_ALBUM_PEAK, TEST_ALBUM_PEAK, NULL);
119 }
120 if (mask & (1 << 12)) {
121 gst_tag_list_add (tags, GST_TAG_MERGE_KEEP,
122 GST_TAG_BEATS_PER_MINUTE, TEST_BPM, NULL);
123 }
124 if (mask & (1 << 13)) {
125 }
126 return tags;
127}
128
129static gboolean
130utf8_string_in_buf (GstBuffer * buf, const gchar * s)
131{
132 gint i, len;
133 GstMapInfo map;
134
135 len = strlen (s);
136 gst_buffer_map (buf, &map, GST_MAP_READ);
137 for (i = 0; i < (map.size - len); ++i) {
138 if (memcmp (map.data + i, s, len) == 0) {
139 gst_buffer_unmap (buf, &map);
140 return TRUE;
141 }
142 }
143 gst_buffer_unmap (buf, &map);
144
145 return FALSE;
146}
147
148static void
149test_taglib_id3mux_check_tag_buffer (GstBuffer * buf, guint32 mask)
150{
151 /* make sure our UTF-8 string hasn't been put into the tag as ISO-8859-1 */
152 if (mask & (1 << 0)) {
153 fail_unless (utf8_string_in_buf (buf, TEST_ARTIST));
154 }
155 /* make sure our UTF-8 string hasn't been put into the tag as ISO-8859-1 */
156 if (mask & (1 << 1)) {
157 fail_unless (utf8_string_in_buf (buf, TEST_TITLE));
158 }
159 /* make sure our UTF-8 string hasn't been put into the tag as ISO-8859-1 */
160 if (mask & (1 << 2)) {
161 fail_unless (utf8_string_in_buf (buf, TEST_ALBUM));
162 }
163}
164
165static void
166test_taglib_id3mux_check_tags (GstTagList * tags, guint32 mask)
167{
168 if (mask & (1 << 0)) {
169 gchar *s = NULL;
170
171 fail_unless (gst_tag_list_get_string (tags, GST_TAG_ARTIST, &s));
172 fail_unless (g_str_equal (s, TEST_ARTIST));
173 g_free (s);
174 }
175 if (mask & (1 << 1)) {
176 gchar *s = NULL;
177
178 fail_unless (gst_tag_list_get_string (tags, GST_TAG_TITLE, &s));
179 fail_unless (g_str_equal (s, TEST_TITLE));
180 g_free (s);
181 }
182 if (mask & (1 << 2)) {
183 gchar *s = NULL;
184
185 fail_unless (gst_tag_list_get_string (tags, GST_TAG_ALBUM, &s));
186 fail_unless (g_str_equal (s, TEST_ALBUM));
187 g_free (s);
188 }
189 if (mask & (1 << 3)) {
190 GDate *shouldbe, *date = NULL;
191
192 shouldbe = TEST_DATE;
193 fail_unless (gst_tag_list_get_date (tags, GST_TAG_DATE, &date));
194 fail_unless (g_date_compare (shouldbe, date) == 0);
195 g_date_free (shouldbe);
196 g_date_free (date);
197 }
198 if (mask & (1 << 4)) {
199 guint num;
200
201 fail_unless (gst_tag_list_get_uint (tags, GST_TAG_TRACK_NUMBER, &num));
202 fail_unless (num == TEST_TRACK_NUMBER);
203 }
204 if (mask & (1 << 5)) {
205 guint count;
206
207 fail_unless (gst_tag_list_get_uint (tags, GST_TAG_TRACK_COUNT, &count));
208 fail_unless (count == TEST_TRACK_COUNT);
209 }
210 if (mask & (1 << 6)) {
211 guint num;
212
213 fail_unless (gst_tag_list_get_uint (tags, GST_TAG_ALBUM_VOLUME_NUMBER,
214 &num));
215 fail_unless (num == TEST_VOLUME_NUMBER);
216 }
217 if (mask & (1 << 7)) {
218 guint count;
219
220 fail_unless (gst_tag_list_get_uint (tags, GST_TAG_ALBUM_VOLUME_COUNT,
221 &count));
222 fail_unless (count == TEST_VOLUME_COUNT);
223 }
224 if (mask & (1 << 8)) {
225 gdouble gain;
226
227 fail_unless (gst_tag_list_get_double (tags, GST_TAG_TRACK_GAIN, &gain));
228 fail_unless_sorta_equals_float (gain, TEST_TRACK_GAIN);
229 }
230 if (mask & (1 << 9)) {
231 gdouble gain;
232
233 fail_unless (gst_tag_list_get_double (tags, GST_TAG_ALBUM_GAIN, &gain));
234 fail_unless_sorta_equals_float (gain, TEST_ALBUM_GAIN);
235 }
236 if (mask & (1 << 10)) {
237 gdouble peak;
238
239 fail_unless (gst_tag_list_get_double (tags, GST_TAG_TRACK_PEAK, &peak));
240 fail_unless_sorta_equals_float (peak, TEST_TRACK_PEAK);
241 }
242 if (mask & (1 << 11)) {
243 gdouble peak;
244
245 fail_unless (gst_tag_list_get_double (tags, GST_TAG_ALBUM_PEAK, &peak));
246 fail_unless_sorta_equals_float (peak, TEST_ALBUM_PEAK);
247 }
248 if (mask & (1 << 12)) {
249 gdouble bpm;
250
251 fail_unless (gst_tag_list_get_double (tags, GST_TAG_BEATS_PER_MINUTE,
252 &bpm));
253 fail_unless_sorta_equals_float (bpm, TEST_BPM);
254 }
255 if (mask & (1 << 13)) {
256 }
257}
258
259static void
260fill_mp3_buffer (GstElement * fakesrc, GstBuffer * buf, GstPad * pad,
261 guint64 * p_offset)
262{
263 gsize size;
264
265 size = gst_buffer_get_size (buf);
266
267 fail_unless (size == MP3_FRAME_SIZE);
268
269 GST_LOG ("filling buffer with fake mp3 data, offset = %" G_GUINT64_FORMAT,
270 *p_offset);
271
272 gst_buffer_fill (buf, 0, mp3_dummyhdr, sizeof (mp3_dummyhdr));
273
274#if 0
275 /* can't use gst_buffer_set_caps() here because the metadata isn't writable
276 * because of the extra refcounts taken by the signal emission mechanism;
277 * we know it's fine to use GST_BUFFER_CAPS() here though */
278 GST_BUFFER_CAPS (buf) = gst_caps_new_simple ("audio/mpeg", "mpegversion",
279 G_TYPE_INT, 1, "layer", G_TYPE_INT, 3, NULL);
280#endif
281
282 GST_BUFFER_OFFSET (buf) = *p_offset;
283 *p_offset += size;
284}
285
286static void
287got_buffer (GstElement * fakesink, GstBuffer * buf, GstPad * pad,
288 GstBuffer ** p_buf)
289{
290 gint64 off;
291 GstMapInfo map;
292
293 off = GST_BUFFER_OFFSET (buf);
294
295 gst_buffer_map (buf, &map, GST_MAP_READ);
296
Sebastian Dröge5f0bab02013-07-14 11:42:29 +0200297 GST_LOG ("size=%" G_GSIZE_FORMAT ", offset=%" G_GINT64_FORMAT, map.size, off);
Olivier Naudan988c6f02012-04-16 07:16:25 -0400298
299 fail_unless (GST_BUFFER_OFFSET_IS_VALID (buf));
300
301 if (*p_buf == NULL || (off + map.size) > gst_buffer_get_size (*p_buf)) {
302 GstBuffer *newbuf;
303
304 /* not very elegant, but who cares */
305 newbuf = gst_buffer_new_and_alloc (off + map.size);
306 if (*p_buf) {
307 GstMapInfo pmap;
308
309 gst_buffer_map (*p_buf, &pmap, GST_MAP_READ);
310 gst_buffer_fill (newbuf, 0, pmap.data, pmap.size);
311 gst_buffer_unmap (*p_buf, &pmap);
312 }
313 gst_buffer_fill (newbuf, off, map.data, map.size);
314
315 if (*p_buf)
316 gst_buffer_unref (*p_buf);
317 *p_buf = newbuf;
318 } else {
319 gst_buffer_fill (*p_buf, off, map.data, map.size);
320 }
321 gst_buffer_unmap (buf, &map);
322}
323
324static void
325test_taglib_id3mux_check_output_buffer (GstBuffer * buf)
326{
327 GstMapInfo map;
328 guint off;
329
330 gst_buffer_map (buf, &map, GST_MAP_READ);
331 g_assert (map.size % MP3_FRAME_SIZE == 0);
332
333 for (off = 0; off < map.size; off += MP3_FRAME_SIZE) {
334 fail_unless (memcmp (map.data + off, mp3_dummyhdr,
335 sizeof (mp3_dummyhdr)) == 0);
336 }
337 gst_buffer_unmap (buf, &map);
338}
339
340static void
341identity_cb (GstElement * identity, GstBuffer * buf, GstBuffer ** p_tagbuf)
342{
343 if (*p_tagbuf == NULL) {
344 *p_tagbuf = gst_buffer_ref (buf);
345 }
346}
347
348static void
349test_taglib_id3mux_with_tags (GstTagList * tags, guint32 mask)
350{
351 GstMessage *msg;
352 GstTagList *tags_read = NULL;
353 GstElement *pipeline, *id3mux, *id3demux, *fakesrc, *identity, *fakesink;
354 GstBus *bus;
355 guint64 offset;
356 GstBuffer *outbuf = NULL;
357 GstBuffer *tagbuf = NULL;
358 GstStateChangeReturn state_result;
359
360 pipeline = gst_pipeline_new ("pipeline");
361 g_assert (pipeline != NULL);
362
363 fakesrc = gst_element_factory_make ("fakesrc", "fakesrc");
364 g_assert (fakesrc != NULL);
365
366 id3mux = gst_element_factory_make ("id3v2mux", "id3v2mux");
367 g_assert (id3mux != NULL);
368
369 identity = gst_element_factory_make ("identity", "identity");
370 g_assert (identity != NULL);
371
372 id3demux = gst_element_factory_make ("id3demux", "id3demux");
373 g_assert (id3demux != NULL);
374
375 fakesink = gst_element_factory_make ("fakesink", "fakesink");
376 g_assert (fakesink != NULL);
377
378 /* set up sink */
379 outbuf = NULL;
380 g_object_set (fakesink, "signal-handoffs", TRUE, NULL);
381 g_signal_connect (fakesink, "handoff", G_CALLBACK (got_buffer), &outbuf);
382
383 gst_bin_add (GST_BIN (pipeline), fakesrc);
384 gst_bin_add (GST_BIN (pipeline), id3mux);
385 gst_bin_add (GST_BIN (pipeline), identity);
386 gst_bin_add (GST_BIN (pipeline), id3demux);
387 gst_bin_add (GST_BIN (pipeline), fakesink);
388
389 gst_tag_setter_merge_tags (GST_TAG_SETTER (id3mux), tags,
390 GST_TAG_MERGE_APPEND);
391
392 gst_element_link_many (fakesrc, id3mux, identity, id3demux, fakesink, NULL);
393
394 /* set up source */
395 g_object_set (fakesrc, "signal-handoffs", TRUE, "can-activate-pull", FALSE,
396 "filltype", 2, "sizetype", 2, "sizemax", MP3_FRAME_SIZE,
397 "num-buffers", 16, NULL);
398
399 offset = 0;
400 g_signal_connect (fakesrc, "handoff", G_CALLBACK (fill_mp3_buffer), &offset);
401
402 /* set up identity to catch tag buffer */
403 g_signal_connect (identity, "handoff", G_CALLBACK (identity_cb), &tagbuf);
404
405 GST_LOG ("setting and getting state ...");
406 gst_element_set_state (pipeline, GST_STATE_PLAYING);
407 state_result = gst_element_get_state (pipeline, NULL, NULL, -1);
408 fail_unless (state_result == GST_STATE_CHANGE_SUCCESS,
409 "Unexpected result from get_state(). Expected success, got %d",
410 state_result);
411
412 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
413
414 GST_LOG ("Waiting for tag ...");
415 msg =
416 gst_bus_poll (bus, GST_MESSAGE_TAG | GST_MESSAGE_EOS | GST_MESSAGE_ERROR,
417 -1);
418 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
419 GError *err;
420 gchar *dbg;
421
422 gst_message_parse_error (msg, &err, &dbg);
423 g_printerr ("ERROR from element %s: %s\n%s\n",
424 GST_OBJECT_NAME (msg->src), err->message, GST_STR_NULL (dbg));
425 g_error_free (err);
426 g_free (dbg);
427 } else if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS) {
428 g_printerr ("EOS message, but were waiting for TAGS!\n");
429 }
430 fail_unless (msg->type == GST_MESSAGE_TAG);
431
432 gst_message_parse_tag (msg, &tags_read);
433 gst_message_unref (msg);
434
435 GST_LOG ("Got tags: %" GST_PTR_FORMAT, tags_read);
436 test_taglib_id3mux_check_tags (tags_read, mask);
Sebastian Drögecabd2262012-08-09 10:22:17 +0200437 gst_tag_list_unref (tags_read);
Olivier Naudan988c6f02012-04-16 07:16:25 -0400438
439 fail_unless (tagbuf != NULL);
440 test_taglib_id3mux_check_tag_buffer (tagbuf, mask);
441 gst_buffer_unref (tagbuf);
442
443 GST_LOG ("Waiting for EOS ...");
444 msg = gst_bus_poll (bus, GST_MESSAGE_EOS | GST_MESSAGE_ERROR, -1);
445 if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
446 GError *err;
447 gchar *dbg;
448
449 gst_message_parse_error (msg, &err, &dbg);
450 g_printerr ("ERROR from element %s: %s\n%s\n",
451 GST_OBJECT_NAME (msg->src), err->message, GST_STR_NULL (dbg));
452 g_error_free (err);
453 g_free (dbg);
454 }
455 fail_unless (msg->type == GST_MESSAGE_EOS);
456 gst_message_unref (msg);
457
458 gst_object_unref (bus);
459
460 GST_LOG ("Got EOS, shutting down ...");
461 gst_element_set_state (pipeline, GST_STATE_NULL);
462 gst_object_unref (pipeline);
463
464 test_taglib_id3mux_check_output_buffer (outbuf);
465 gst_buffer_unref (outbuf);
466
467 GST_LOG ("Done");
468}
469
470GST_START_TEST (test_id3v2mux)
471{
472 GstTagList *tags;
473 gint i;
474
475 g_random_set_seed (247166295);
476
477 /* internal consistency check */
478 tags = test_taglib_id3mux_create_tags (0xFFFFFFFF);
479 test_taglib_id3mux_check_tags (tags, 0xFFFFFFFF);
Sebastian Drögecabd2262012-08-09 10:22:17 +0200480 gst_tag_list_unref (tags);
Olivier Naudan988c6f02012-04-16 07:16:25 -0400481
482 /* now the real tests */
483 for (i = 0; i < 50; ++i) {
484 guint32 mask;
485
486 mask = g_random_int ();
487 GST_LOG ("tag mask = %08x (i=%d)", mask, i);
488
489 if (mask == 0)
490 continue;
491
492 /* create tags */
493 tags = test_taglib_id3mux_create_tags (mask);
494 GST_LOG ("tags for mask %08x = %" GST_PTR_FORMAT, mask, tags);
495
496 /* double-check for internal consistency */
497 test_taglib_id3mux_check_tags (tags, mask);
498
499 /* test with pipeline */
500 test_taglib_id3mux_with_tags (tags, mask);
501
502 /* free tags */
Sebastian Drögecabd2262012-08-09 10:22:17 +0200503 gst_tag_list_unref (tags);
Olivier Naudan988c6f02012-04-16 07:16:25 -0400504 }
505}
506
507GST_END_TEST;
508
509static Suite *
510id3v2mux_suite (void)
511{
512 Suite *s = suite_create ("id3v2mux");
513 TCase *tc_chain = tcase_create ("general");
514
515 suite_add_tcase (s, tc_chain);
516 tcase_add_test (tc_chain, test_id3v2mux);
517
518 return s;
519}
520
Sebastian Dröge42ff2402015-06-07 10:56:31 +0200521GST_CHECK_MAIN (id3v2mux);