1 /* GStreamer
2 *
3 * unit test for matroskamux
4 *
5 * Copyright (C) <2005> Michal Benes <michal.benes@xeris.cz>
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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #include <gst/check/gstcheck.h>
24 #include <gst/base/gstadapter.h>
25 #include <gst/check/gstharness.h>
26
27 #define AC3_CAPS_STRING "audio/x-ac3, " \
28 "channels = (int) 1, " \
29 "rate = (int) 8000"
30 #define VORBIS_TMPL_CAPS_STRING "audio/x-vorbis, " \
31 "channels = (int) 1, " \
32 "rate = (int) 8000, " \
33 "streamheader=(buffer)<10, 2020, 303030>"
34
35 static GstHarness *
setup_matroskamux_harness(const gchar * src_pad_str)36 setup_matroskamux_harness (const gchar * src_pad_str)
37 {
38 GstHarness *h;
39
40 h = gst_harness_new_with_padnames ("matroskamux", "audio_%u", "src");
41 gst_harness_set_src_caps_str (h, src_pad_str);
42 gst_harness_set_sink_caps_str (h, "video/x-matroska; audio/x-matroska");
43
44 return h;
45 }
46
47 static gboolean
seekable_sinkpad_query(GstPad * pad,GstObject * parent,GstQuery * query)48 seekable_sinkpad_query (GstPad * pad, GstObject * parent, GstQuery * query)
49 {
50 gboolean ret = FALSE;
51
52 if (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING) {
53 gst_query_set_seeking (query, GST_FORMAT_BYTES, TRUE, 0, -1);
54 ret = TRUE;
55 }
56
57 return ret;
58 }
59
60 #define compare_buffer_to_data(buffer, data, data_size) \
61 G_STMT_START { \
62 fail_unless_equals_int (data_size, gst_buffer_get_size (buffer)); \
63 fail_unless (gst_buffer_memcmp (buffer, 0, data, data_size) == 0); \
64 } G_STMT_END
65
66 static void
test_ebml_header_with_version(gint version,gconstpointer data,gsize data_size)67 test_ebml_header_with_version (gint version,
68 gconstpointer data, gsize data_size)
69 {
70 GstHarness *h;
71 GstBuffer *inbuffer, *outbuffer;
72
73 h = setup_matroskamux_harness (AC3_CAPS_STRING);
74 g_object_set (h->element, "version", version, NULL);
75
76 inbuffer = gst_harness_create_buffer (h, 1);
77 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
78 fail_unless_equals_int (2, gst_harness_buffers_received (h));
79
80 outbuffer = gst_harness_pull (h);
81 compare_buffer_to_data (outbuffer, data, data_size);
82 gst_buffer_unref (outbuffer);
83
84 gst_harness_teardown (h);
85 }
86
GST_START_TEST(test_ebml_header_v1)87 GST_START_TEST (test_ebml_header_v1)
88 {
89 guint8 data_v1[] = {
90 0x1a, 0x45, 0xdf, 0xa3, /* master ID */
91 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14,
92 0x42, 0x82, /* doctype */
93 0x89, /* 9 bytes */
94 0x6d, 0x61, 0x74, 0x72, 0x6f, 0x73, 0x6b, 0x61, 0x00, /* "matroska" */
95 0x42, 0x87, /* doctypeversion */
96 0x81, /* 1 byte */
97 0x01, /* 1 */
98 0x42, 0x85, /* doctypereadversion */
99 0x81, /* 1 byte */
100 0x01, /* 1 */
101 };
102
103 test_ebml_header_with_version (1, data_v1, sizeof (data_v1));
104 }
105
106 GST_END_TEST;
107
GST_START_TEST(test_ebml_header_v2)108 GST_START_TEST (test_ebml_header_v2)
109 {
110 guint8 data_v2[] = {
111 0x1a, 0x45, 0xdf, 0xa3, /* master ID */
112 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14,
113 0x42, 0x82, /* doctype */
114 0x89, /* 9 bytes */
115 0x6d, 0x61, 0x74, 0x72, 0x6f, 0x73, 0x6b, 0x61, 0x00, /* "matroska" */
116 0x42, 0x87, /* doctypeversion */
117 0x81, /* 1 byte */
118 0x02, /* 2 */
119 0x42, 0x85, /* doctypereadversion */
120 0x81, /* 1 byte */
121 0x02, /* 2 */
122 };
123
124 test_ebml_header_with_version (2, data_v2, sizeof (data_v2));
125 }
126
127 GST_END_TEST;
128
129
GST_START_TEST(test_vorbis_header)130 GST_START_TEST (test_vorbis_header)
131 {
132 GstHarness *h;
133 GstBuffer *inbuffer, *outbuffer;
134 gboolean vorbis_header_found = FALSE;
135 gint j;
136 gsize buffer_size;
137 guint8 data[] =
138 { 0x63, 0xa2, 0x89, 0x02, 0x01, 0x02, 0x10, 0x20, 0x20, 0x30, 0x30,
139 0x30
140 };
141
142 h = setup_matroskamux_harness (VORBIS_TMPL_CAPS_STRING);
143
144 inbuffer = gst_harness_create_buffer (h, 1);
145 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
146
147 outbuffer = gst_harness_pull (h);
148 while (outbuffer != NULL) {
149 buffer_size = gst_buffer_get_size (outbuffer);
150
151 if (!vorbis_header_found && buffer_size >= sizeof (data)) {
152 for (j = 0; j <= buffer_size - sizeof (data); j++) {
153 if (gst_buffer_memcmp (outbuffer, j, data, sizeof (data)) == 0) {
154 vorbis_header_found = TRUE;
155 break;
156 }
157 }
158 }
159
160 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
161 gst_buffer_unref (outbuffer);
162
163 outbuffer = gst_harness_try_pull (h);
164 }
165
166 fail_unless (vorbis_header_found);
167
168 gst_harness_teardown (h);
169 }
170
171 GST_END_TEST;
172
173
174 static void
test_block_group_with_version(gint version,gconstpointer data0,gsize data0_size)175 test_block_group_with_version (gint version,
176 gconstpointer data0, gsize data0_size)
177 {
178 GstHarness *h;
179 GstBuffer *inbuffer, *outbuffer;
180 guint8 data1[] = { 0x42 };
181
182 h = setup_matroskamux_harness (AC3_CAPS_STRING);
183 g_object_set (h->element, "version", version, NULL);
184
185 /* Generate the header */
186 inbuffer = gst_harness_create_buffer (h, 1);
187 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
188 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
189 fail_unless_equals_int (5, gst_harness_buffers_received (h));
190
191 outbuffer = gst_harness_pull (h);
192 fail_unless (outbuffer != NULL);
193 while (outbuffer != NULL) {
194 gst_buffer_unref (outbuffer);
195 outbuffer = gst_harness_try_pull (h);
196 }
197
198 /* Now push a buffer */
199 inbuffer = gst_harness_create_buffer (h, 1);
200 gst_buffer_fill (inbuffer, 0, data1, sizeof (data1));
201 GST_BUFFER_TIMESTAMP (inbuffer) = 1000000;
202
203 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
204
205 outbuffer = gst_harness_pull (h);
206 compare_buffer_to_data (outbuffer, data0, data0_size);
207 gst_buffer_unref (outbuffer);
208
209 outbuffer = gst_harness_pull (h);
210 compare_buffer_to_data (outbuffer, data1, sizeof (data1));
211 gst_buffer_unref (outbuffer);
212
213 gst_harness_teardown (h);
214 }
215
GST_START_TEST(test_block_group_v1)216 GST_START_TEST (test_block_group_v1)
217 {
218 guint8 data0_v1[] = { 0xa0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
219 0xa1, 0x85,
220 0x81, 0x00, 0x01, 0x00
221 };
222
223 test_block_group_with_version (1, data0_v1, sizeof (data0_v1));
224 }
225
226 GST_END_TEST;
227
GST_START_TEST(test_block_group_v2)228 GST_START_TEST (test_block_group_v2)
229 {
230 guint8 data0_v2[] = { 0xa3, 0x85, 0x81, 0x00, 0x01, 0x00 };
231
232 test_block_group_with_version (2, data0_v2, sizeof (data0_v2));
233 }
234
235 GST_END_TEST;
236
GST_START_TEST(test_reset)237 GST_START_TEST (test_reset)
238 {
239 GstHarness *h;
240 GstBuffer *inbuffer;
241 GstBuffer *outbuffer;
242
243 h = setup_matroskamux_harness (AC3_CAPS_STRING);
244
245 inbuffer = gst_harness_create_buffer (h, 1);
246 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
247 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
248 fail_unless_equals_int (5, gst_harness_buffers_received (h));
249
250 outbuffer = gst_harness_pull (h);
251 fail_unless (outbuffer != NULL);
252 while (outbuffer != NULL) {
253 gst_buffer_unref (outbuffer);
254 outbuffer = gst_harness_try_pull (h);
255 }
256
257 fail_unless_equals_int (GST_STATE_CHANGE_SUCCESS,
258 gst_element_set_state (h->element, GST_STATE_NULL));
259
260 gst_harness_play (h);
261
262 inbuffer = gst_harness_create_buffer (h, 1);
263 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
264 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
265
266 outbuffer = gst_harness_pull (h);
267 fail_unless (outbuffer != NULL);
268 while (outbuffer != NULL) {
269 gst_buffer_unref (outbuffer);
270 outbuffer = gst_harness_try_pull (h);
271 }
272
273 gst_harness_teardown (h);
274 }
275
276 GST_END_TEST;
277
GST_START_TEST(test_link_webmmux_webm_sink)278 GST_START_TEST (test_link_webmmux_webm_sink)
279 {
280 GstHarness *h;
281
282 h = gst_harness_new_with_padnames ("webmmux", "audio_%u", "src");
283 fail_unless (h != NULL);
284
285 gst_harness_set_sink_caps_str (h, "video/webm; audio/webm");
286
287 gst_harness_play (h);
288
289 fail_unless_equals_int (GST_STATE_CHANGE_SUCCESS,
290 gst_element_set_state (h->element, GST_STATE_NULL));
291
292 gst_harness_teardown (h);
293 }
294
295 GST_END_TEST;
296
297 static gint64 timecodescales[] = {
298 GST_USECOND,
299 GST_MSECOND,
300 GST_MSECOND * 10,
301 GST_MSECOND * 100,
302 GST_MSECOND * 400,
303 /* FAILS: ? GST_MSECOND * 500, a bug? */
304 };
305
GST_START_TEST(test_timecodescale)306 GST_START_TEST (test_timecodescale)
307 {
308 GstBuffer *inbuffer, *outbuffer;
309 guint8 data_h0[] = {
310 0xa3, 0x85, 0x81, 0x00, 0x00, 0x00,
311 };
312 guint8 data_h1[] = {
313 0xa3, 0x85, 0x81, 0x00, 0x01, 0x00,
314 };
315
316 GstHarness *h = setup_matroskamux_harness (AC3_CAPS_STRING);
317 gint64 timecodescale = timecodescales[__i__];
318
319 g_object_set (h->element, "timecodescale", timecodescale, NULL);
320 g_object_set (h->element, "version", 2, NULL);
321
322 /* Buffer 0 */
323 inbuffer = gst_harness_create_buffer (h, 1);
324 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
325 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
326
327 /* pull out headers */
328 gst_buffer_unref (gst_harness_pull (h));
329 gst_buffer_unref (gst_harness_pull (h));
330 gst_buffer_unref (gst_harness_pull (h));
331
332 /* verify header and drop the data */
333 outbuffer = gst_harness_pull (h);
334 compare_buffer_to_data (outbuffer, data_h0, sizeof (data_h0));
335 gst_buffer_unref (outbuffer);
336 gst_buffer_unref (gst_harness_pull (h));
337
338 /* Buffer 1 */
339 inbuffer = gst_harness_create_buffer (h, 1);
340 GST_BUFFER_TIMESTAMP (inbuffer) = timecodescale;
341 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
342
343 /* verify header and drop the data */
344 outbuffer = gst_harness_pull (h);
345 compare_buffer_to_data (outbuffer, data_h1, sizeof (data_h1));
346 gst_buffer_unref (outbuffer);
347 gst_buffer_unref (gst_harness_pull (h));
348
349 gst_harness_teardown (h);
350 }
351
352 GST_END_TEST;
353
354 /* Create a new chapter */
355 static GstTocEntry *
new_chapter(const guint chapter_nb,const gint64 start,const gint64 stop)356 new_chapter (const guint chapter_nb, const gint64 start, const gint64 stop)
357 {
358 GstTocEntry *toc_entry, *toc_sub_entry;
359 GstTagList *tags;
360 gchar title[32];
361 gchar artist[32];
362 gchar str_uid[32];
363
364 g_snprintf (str_uid, sizeof (str_uid), "uid.%d", chapter_nb);
365 toc_entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, str_uid);
366 gst_toc_entry_set_start_stop_times (toc_entry, start, stop);
367
368 g_snprintf (title, sizeof (title), "chap.%d", chapter_nb);
369 g_snprintf (artist, sizeof (artist), "art.%d", chapter_nb);
370 tags = gst_tag_list_new (GST_TAG_TITLE, title, GST_TAG_ARTIST, artist, NULL);
371 gst_toc_entry_set_tags (toc_entry, tags);
372
373 g_snprintf (str_uid, sizeof (str_uid), "uid.%d.1", chapter_nb);
374 toc_sub_entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, str_uid);
375 gst_toc_entry_set_start_stop_times (toc_sub_entry, start, (start + stop) / 2);
376
377 g_snprintf (title, sizeof (title), "nested.%d.1", chapter_nb);
378 g_snprintf (artist, sizeof (artist), "art.%d.1", chapter_nb);
379 tags = gst_tag_list_new (GST_TAG_TITLE, title, GST_TAG_ARTIST, artist, NULL);
380 gst_toc_entry_set_tags (toc_sub_entry, tags);
381
382 gst_toc_entry_append_sub_entry (toc_entry, toc_sub_entry);
383
384 g_snprintf (str_uid, sizeof (str_uid), "uid.%d.2", chapter_nb);
385 toc_sub_entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, str_uid);
386 gst_toc_entry_set_start_stop_times (toc_sub_entry, (start + stop) / 2, stop);
387
388 g_snprintf (title, sizeof (title), "nested/%d.2", chapter_nb);
389 g_snprintf (artist, sizeof (artist), "art.%d.2", chapter_nb);
390 tags = gst_tag_list_new (GST_TAG_TITLE, title, GST_TAG_ARTIST, artist, NULL);
391 gst_toc_entry_set_tags (toc_sub_entry, tags);
392
393 gst_toc_entry_append_sub_entry (toc_entry, toc_sub_entry);
394
395 return toc_entry;
396 }
397
398 /* Create a reference toc which includes a master edition entry */
399 static GstToc *
new_reference_toc(void)400 new_reference_toc (void)
401 {
402 GstToc *ref_toc;
403 GstTocEntry *toc_edition_entry, *toc_entry;
404 GstTagList *tags;
405
406 ref_toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
407
408 toc_edition_entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, "00");
409 tags = gst_tag_list_new (GST_TAG_COMMENT, "Ed", NULL);
410 gst_toc_entry_set_tags (toc_edition_entry, tags);
411
412 toc_entry = new_chapter (1, 0 * GST_MSECOND, 2 * GST_MSECOND);
413 gst_toc_entry_append_sub_entry (toc_edition_entry, toc_entry);
414
415 toc_entry = new_chapter (2, 2 * GST_MSECOND, 4 * GST_MSECOND);
416 gst_toc_entry_append_sub_entry (toc_edition_entry, toc_entry);
417
418 gst_toc_append_entry (ref_toc, toc_edition_entry);
419
420 return ref_toc;
421 }
422
423 /* Create a toc which includes chapters without edition entry */
424 static GstToc *
new_no_edition_toc(void)425 new_no_edition_toc (void)
426 {
427 GstToc *ref_toc;
428 GstTocEntry *toc_entry;
429
430 ref_toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
431
432 toc_entry = new_chapter (1, 0 * GST_MSECOND, 2 * GST_MSECOND);
433 gst_toc_append_entry (ref_toc, toc_entry);
434
435 toc_entry = new_chapter (2, 2 * GST_MSECOND, 4 * GST_MSECOND);
436 gst_toc_append_entry (ref_toc, toc_entry);
437
438 return ref_toc;
439 }
440
441 static guint64
read_integer(GstMapInfo * info,gsize * index,guint64 len)442 read_integer (GstMapInfo * info, gsize * index, guint64 len)
443 {
444 guint64 total = 0;
445
446 for (; len > 0; --len) {
447 total = (total << 8) | GST_READ_UINT8 (info->data + *index);
448 ++(*index);
449 }
450
451 return total;
452 }
453
454 static guint64
read_length(GstMapInfo * info,gsize * index)455 read_length (GstMapInfo * info, gsize * index)
456 {
457 gint len_mask = 0x80, read = 1;
458 guint64 total;
459 guint8 b;
460
461 b = GST_READ_UINT8 (info->data + *index);
462 ++(*index);
463 total = (guint64) b;
464 while (read <= 8 && !(total & len_mask)) {
465 read++;
466 len_mask >>= 1;
467 }
468 total &= (len_mask - 1);
469
470 for (; read > 1; --read) {
471 total = (total << 8) | GST_READ_UINT8 (info->data + *index);
472 ++(*index);
473 }
474
475 return total;
476 }
477
478 static gboolean
check_id(GstMapInfo * info,gsize * index,guint8 * tag,gint tag_len,guint64 * len)479 check_id (GstMapInfo * info, gsize * index,
480 guint8 * tag, gint tag_len, guint64 * len)
481 {
482 if (memcmp (info->data + *index, tag, tag_len) == 0) {
483 *index += tag_len;
484 *len = read_length (info, index);
485 return TRUE;
486 } else {
487 return FALSE;
488 }
489 }
490
491 static gboolean
check_id_read_int(GstMapInfo * info,gsize * index,guint8 * tag,gint tag_len,guint64 * value)492 check_id_read_int (GstMapInfo * info, gsize * index,
493 guint8 * tag, gint tag_len, guint64 * value)
494 {
495 guint64 len;
496
497 if (check_id (info, index, tag, tag_len, &len)) {
498 *value = read_integer (info, index, len);
499 return TRUE;
500 } else {
501 return FALSE;
502 }
503 }
504
505 /* Check the toc entry against the muxed buffer
506 * Returns the internal UID */
507 static void
check_chapter(GstTocEntry * toc_entry,GstTocEntry * internal_toc_entry,GstMapInfo * info,gsize * index,gint last_offset)508 check_chapter (GstTocEntry * toc_entry, GstTocEntry * internal_toc_entry,
509 GstMapInfo * info, gsize * index, gint last_offset)
510 {
511 guint64 len, value, uid;
512 gint64 start_ref, end_ref;
513 gchar s_uid[32];
514 const gchar *str_uid;
515 GstTocEntry *internal_chapter;
516 GList *cur_sub_chap;
517 GstTagList *tags;
518 gchar *title;
519
520 guint8 chapter_atom[] = { 0xb6 };
521 guint8 chapter_uid[] = { 0x73, 0xc4 };
522 guint8 chapter_str_uid[] = { 0x56, 0x54 };
523 guint8 chapter_start[] = { 0x91 };
524 guint8 chapter_end[] = { 0x92 };
525 guint8 chapter_flag_hidden[] = { 0x98 };
526 guint8 chapter_flag_enabled[] = { 0x45, 0x98 };
527 guint8 chapter_segment_uid[] = { 0x6e, 0x67 };
528 guint8 chapter_segment_edition_uid[] = { 0x6e, 0xbc };
529 guint8 chapter_physical_equiv[] = { 0x63, 0xc3 };
530 guint8 chapter_track[] = { 0x8f };
531 guint8 chapter_track_nb[] = { 0x89 };
532 guint8 chapter_display[] = { 0x80 };
533 guint8 chapter_string[] = { 0x85 };
534 guint8 chapter_language[] = { 0x43, 0x7c };
535
536 fail_unless (check_id (info, index, chapter_atom,
537 sizeof (chapter_atom), &len));
538
539 fail_unless (check_id_read_int (info, index, chapter_uid,
540 sizeof (chapter_uid), &uid));
541
542 /* optional StringUID */
543 if (check_id (info, index, chapter_str_uid, sizeof (chapter_str_uid), &len)) {
544 str_uid = gst_toc_entry_get_uid (toc_entry);
545 fail_unless (memcmp (info->data + *index, str_uid, strlen (str_uid)) == 0);
546 *index += len;
547 }
548
549 gst_toc_entry_get_start_stop_times (toc_entry, &start_ref, &end_ref);
550
551 fail_unless (check_id_read_int (info, index, chapter_start,
552 sizeof (chapter_start), &value));
553 fail_unless_equals_int (start_ref, value);
554
555 /* optional chapter end */
556 if (check_id_read_int (info, index, chapter_end,
557 sizeof (chapter_end), &value)) {
558 fail_unless_equals_int (end_ref, value);
559 }
560
561 fail_unless (check_id_read_int (info, index, chapter_flag_hidden,
562 sizeof (chapter_flag_hidden), &value));
563
564 fail_unless (check_id_read_int (info, index, chapter_flag_enabled,
565 sizeof (chapter_flag_enabled), &value));
566
567 /* optional segment UID */
568 check_id_read_int (info, index, chapter_segment_uid,
569 sizeof (chapter_segment_uid), &value);
570
571 /* optional segment edition UID */
572 check_id_read_int (info, index, chapter_segment_edition_uid,
573 sizeof (chapter_segment_edition_uid), &value);
574
575 /* optional physical equiv */
576 check_id_read_int (info, index, chapter_physical_equiv,
577 sizeof (chapter_physical_equiv), &value);
578
579 /* optional chapter track */
580 if (check_id (info, index, chapter_track, sizeof (chapter_track), &len)) {
581 fail_unless (check_id_read_int (info, index, chapter_track_nb,
582 sizeof (chapter_track_nb), &value));
583 }
584
585 /* FIXME: there can be several chapter displays */
586 if (check_id (info, index, chapter_display, sizeof (chapter_display), &len)) {
587 /* chapter display */
588 fail_unless (check_id (info, index, chapter_string,
589 sizeof (chapter_string), &len));
590
591 tags = gst_toc_entry_get_tags (toc_entry);
592 if (gst_tag_list_get_tag_size (tags, GST_TAG_TITLE) > 0) {
593 gst_tag_list_get_string_index (tags, GST_TAG_TITLE, 0, &title);
594 fail_unless (memcmp (info->data + *index, title, strlen (title)) == 0);
595 g_free (title);
596 }
597 *index += len;
598
599 fail_unless (check_id (info, index, chapter_language,
600 sizeof (chapter_language), &len));
601 /* TODO: define language - always "und" ATM */
602 *index += len;
603 }
604
605 /* TODO: add remaining fields (not used in current matroska-mux) */
606
607 g_snprintf (s_uid, sizeof (s_uid), "%" G_GINT64_FORMAT, uid);
608 internal_chapter = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, s_uid);
609 gst_toc_entry_append_sub_entry (internal_toc_entry, internal_chapter);
610
611 cur_sub_chap = gst_toc_entry_get_sub_entries (toc_entry);
612 while (cur_sub_chap != NULL && *index < last_offset) {
613 check_chapter (cur_sub_chap->data, internal_chapter, info,
614 index, last_offset);
615 cur_sub_chap = cur_sub_chap->next;
616 }
617
618 fail_unless (cur_sub_chap == NULL);
619 }
620
621 /* Check the reference toc against the muxed buffer */
622 static void
check_toc(GstToc * ref_toc,GstToc * internal_toc,GstMapInfo * info,gsize * index)623 check_toc (GstToc * ref_toc, GstToc * internal_toc,
624 GstMapInfo * info, gsize * index)
625 {
626 guint64 len, value, uid;
627 gchar s_uid[32];
628 gint last_offset;
629 GList *cur_entry, *cur_chapter;
630 GstTocEntry *internal_edition;
631
632 guint8 edition_entry[] = { 0x45, 0xb9 };
633 guint8 edition_uid[] = { 0x45, 0xbc };
634 guint8 edition_flag_hidden[] = { 0x45, 0xbd };
635 guint8 edition_flag_default[] = { 0x45, 0xdb };
636 guint8 edition_flag_ordered[] = { 0x45, 0xdd };
637
638 /* edition entry */
639 fail_unless (check_id (info, index, edition_entry,
640 sizeof (edition_entry), &len));
641 last_offset = *index + (gint) len;
642
643 cur_entry = gst_toc_get_entries (ref_toc);
644 while (cur_entry != NULL && *index < last_offset) {
645 uid = 0;
646 check_id_read_int (info, index, edition_uid, sizeof (edition_uid), &uid);
647 g_snprintf (s_uid, sizeof (s_uid), "%" G_GINT64_FORMAT, uid);
648 internal_edition = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, s_uid);
649 gst_toc_append_entry (internal_toc, internal_edition);
650
651 fail_unless (check_id_read_int (info, index, edition_flag_hidden,
652 sizeof (edition_flag_hidden), &value));
653
654 fail_unless (check_id_read_int (info, index, edition_flag_default,
655 sizeof (edition_flag_default), &value));
656
657 /* optional */
658 check_id_read_int (info, index, edition_flag_ordered,
659 sizeof (edition_flag_ordered), &value);
660
661 cur_chapter = gst_toc_entry_get_sub_entries (cur_entry->data);
662 while (cur_chapter != NULL && *index < last_offset) {
663 check_chapter (cur_chapter->data, internal_edition, info,
664 index, last_offset);
665 cur_chapter = cur_chapter->next;
666 }
667 fail_unless (cur_chapter == NULL);
668
669 cur_entry = cur_entry->next;
670 }
671
672 fail_unless (cur_entry == NULL && *index == last_offset);
673 }
674
675 static GstTocEntry *
find_toc_entry(GstTocEntry * ref_toc_entry,GstTocEntry * internal_toc_entry,guint64 uid)676 find_toc_entry (GstTocEntry * ref_toc_entry, GstTocEntry * internal_toc_entry,
677 guint64 uid)
678 {
679 GList *cur_ref_entry, *cur_internal_entry;
680 guint64 internal_uid;
681 GstTocEntry *result = NULL;
682
683 internal_uid = g_ascii_strtoull (gst_toc_entry_get_uid (internal_toc_entry),
684 NULL, 10);
685 if (uid == internal_uid) {
686 result = ref_toc_entry;
687 } else {
688 cur_ref_entry = gst_toc_entry_get_sub_entries (ref_toc_entry);
689 cur_internal_entry = gst_toc_entry_get_sub_entries (internal_toc_entry);
690 while (cur_ref_entry != NULL && cur_internal_entry != NULL) {
691 result = find_toc_entry (cur_ref_entry->data, cur_internal_entry->data,
692 uid);
693
694 if (result != NULL) {
695 break;
696 }
697
698 cur_ref_entry = cur_ref_entry->next;
699 cur_internal_entry = cur_internal_entry->next;
700 }
701 }
702
703 return result;
704 }
705
706 static void
find_and_check_tags(GstToc * ref_toc,GstToc * internal_toc,GstMapInfo * info,guint64 uid,gchar * tag_name,gchar * tag_string)707 find_and_check_tags (GstToc * ref_toc, GstToc * internal_toc, GstMapInfo * info,
708 guint64 uid, gchar * tag_name, gchar * tag_string)
709 {
710 GList *cur_ref_entry, *cur_internal_entry;
711 GstTocEntry *ref_toc_entry = NULL;
712 GstTagList *tags;
713 const gchar *tag_type;
714 gchar *cur_tag_string;
715
716 /* find the reference toc entry matching the UID */
717 cur_ref_entry = gst_toc_get_entries (ref_toc);
718 cur_internal_entry = gst_toc_get_entries (internal_toc);
719 while (cur_ref_entry != NULL && cur_internal_entry != NULL) {
720 ref_toc_entry = find_toc_entry (cur_ref_entry->data,
721 cur_internal_entry->data, uid);
722
723 if (ref_toc_entry != NULL) {
724 break;
725 }
726
727 cur_ref_entry = cur_ref_entry->next;
728 cur_internal_entry = cur_internal_entry->next;
729 }
730
731 fail_unless (ref_toc_entry != NULL);
732
733 if (g_strcmp0 (tag_name, "ARTIST") == 0) {
734 tag_type = GST_TAG_ARTIST;
735 } else if (g_strcmp0 (tag_name, "COMMENTS") == 0) {
736 tag_type = GST_TAG_COMMENT;
737 } else {
738 tag_type = NULL;
739 }
740
741 fail_unless (tag_type != NULL);
742
743 tags = gst_toc_entry_get_tags (ref_toc_entry);
744 fail_unless (gst_tag_list_get_tag_size (tags, tag_type) > 0);
745 gst_tag_list_get_string_index (tags, tag_type, 0, &cur_tag_string);
746 fail_unless (g_strcmp0 (cur_tag_string, tag_string) == 0);
747 g_free (cur_tag_string);
748 }
749
750 static void
check_tags(GstToc * ref_toc,GstToc * internal_toc,GstMapInfo * info,gsize * index)751 check_tags (GstToc * ref_toc, GstToc * internal_toc,
752 GstMapInfo * info, gsize * index)
753 {
754 gboolean found_tags = FALSE, must_check_tag = FALSE;
755 guint64 len, value, uid;
756 gsize last_offset = 0;
757 gsize next_tag;
758 gchar *tag_name_str, *tag_string_str;
759 guint8 tags[] = { 0x12, 0x54, 0xc3, 0x67 };
760 guint8 tag[] = { 0x73, 0x73 };
761 guint8 tag_targets[] = { 0x63, 0xc0 };
762 guint8 tag_target_type_value[] = { 0x68, 0xca };
763 guint8 tag_target_type[] = { 0x63, 0xca };
764 guint8 tag_edition_uid[] = { 0x63, 0xc9 };
765 guint8 tag_chapter_uid[] = { 0x63, 0xc4 };
766 guint8 simple_tag[] = { 0x67, 0xc8 };
767 guint8 tag_name[] = { 0x45, 0xa3 };
768 guint8 tag_string[] = { 0x44, 0x87 };
769
770 if (info->size > *index + sizeof (tags)) {
771 for (; *index < info->size - sizeof (tags); ++(*index)) {
772 if (memcmp (info->data + *index, tags, sizeof (tags)) == 0) {
773 *index += sizeof (tags);
774
775 len = read_length (info, index);
776 last_offset = *index + len;
777
778 found_tags = TRUE;
779 break;
780 }
781 }
782 }
783
784 fail_unless (found_tags);
785
786 while (*index < last_offset) {
787 fail_unless (check_id (info, index, tag, sizeof (tag), &len));
788 next_tag = *index + len;
789
790 fail_unless (check_id (info, index, tag_targets,
791 sizeof (tag_targets), &len));
792
793 must_check_tag = FALSE;
794 check_id_read_int (info, index, tag_target_type_value,
795 sizeof (tag_target_type_value), &value);
796
797 if (check_id (info, index, tag_target_type, sizeof (tag_target_type), &len)) {
798 *index += len;
799 }
800
801 if (check_id_read_int (info, index, tag_chapter_uid,
802 sizeof (tag_chapter_uid), &uid)) {
803 must_check_tag = TRUE;
804 } else if (check_id_read_int (info, index, tag_edition_uid,
805 sizeof (tag_edition_uid), &uid)) {
806 must_check_tag = TRUE;
807 }
808
809 if (must_check_tag) {
810 fail_unless (check_id (info, index, simple_tag,
811 sizeof (simple_tag), &len));
812
813 fail_unless (check_id (info, index, tag_name, sizeof (tag_name), &len));
814 tag_name_str = g_strndup ((gchar *) info->data + *index, len);
815 *index += len;
816
817 fail_unless (check_id (info, index, tag_string, sizeof (tag_string),
818 &len));
819 tag_string_str = g_strndup ((gchar *) info->data + *index, len);
820 *index += len;
821
822 find_and_check_tags (ref_toc, internal_toc, info, uid,
823 tag_name_str, tag_string_str);
824
825 g_free (tag_name_str);
826 g_free (tag_string_str);
827 }
828
829 *index = next_tag;
830 }
831 }
832
833 static void
check_segment(GstToc * ref_toc,GstToc * internal_toc,GstMapInfo * info,gsize * index)834 check_segment (GstToc * ref_toc, GstToc * internal_toc,
835 GstMapInfo * info, gsize * index)
836 {
837 guint8 matroska_segment[] = { 0x18, 0x53, 0x80, 0x67 };
838 guint8 matroska_seek_id_chapters[] = { 0x53, 0xab, 0x84,
839 0x10, 0x43, 0xA7, 0x70
840 };
841 guint8 matroska_seek_id_tags[] = { 0x53, 0xab, 0x84,
842 0x12, 0x54, 0xc3, 0x67
843 };
844 guint8 matroska_seek_pos[] = { 0x53, 0xac };
845 guint8 matroska_chapters[] = { 0x10, 0x43, 0xA7, 0x70 };
846
847 guint64 len, value, segment_offset;
848 guint64 tags_offset = 0;
849 guint64 chapters_offset = 0;
850 gboolean found_chapters_declaration = FALSE, found_tags_declaration = FALSE;
851
852 /* Segment */
853 fail_unless (info->size > sizeof (matroska_segment));
854 fail_unless (check_id (info, index, matroska_segment,
855 sizeof (matroska_segment), &len));
856
857 segment_offset = *index;
858
859 /* Search chapter declaration in seek head */
860 for (; *index < len - sizeof (matroska_seek_id_chapters); ++(*index)) {
861 if (memcmp (info->data + *index, matroska_seek_id_chapters,
862 sizeof (matroska_seek_id_chapters)) == 0) {
863 *index += sizeof (matroska_seek_id_chapters);
864
865 if (check_id_read_int (info, index, matroska_seek_pos,
866 sizeof (matroska_seek_pos), &value)) {
867 /* found chapter declaration */
868 found_chapters_declaration = TRUE;
869 chapters_offset = segment_offset + value;
870 break;
871 }
872 }
873 }
874
875 fail_unless (found_chapters_declaration);
876
877 *index = chapters_offset;
878 if (check_id (info, index, matroska_chapters,
879 sizeof (matroska_chapters), &len)) {
880 check_toc (ref_toc, internal_toc, info, index);
881 }
882
883 /* Search tags declaration in seek head */
884 for (*index = segment_offset; *index < len - sizeof (matroska_seek_id_tags);
885 ++(*index)) {
886 if (memcmp (info->data + *index, matroska_seek_id_tags,
887 sizeof (matroska_seek_id_tags)) == 0) {
888 *index += sizeof (matroska_seek_id_tags);
889
890 if (check_id_read_int (info, index, matroska_seek_pos,
891 sizeof (matroska_seek_pos), &value)) {
892 /* found tags declaration */
893 found_tags_declaration = TRUE;
894 tags_offset = segment_offset + value;
895 break;
896 }
897 }
898 }
899
900 fail_unless (found_tags_declaration);
901
902 *index = tags_offset;
903 check_tags (ref_toc, internal_toc, info, index);
904 }
905
906 static void
test_toc(gboolean with_edition)907 test_toc (gboolean with_edition)
908 {
909 GstHarness *h;
910 GstBuffer *inbuffer, *outbuffer, *merged_buffer;
911 GstMapInfo info;
912 guint64 len;
913 gsize index;
914 GstTocSetter *toc_setter;
915 GstToc *test_toc, *ref_toc, *internal_toc;
916
917 guint8 ebml_header[] = { 0x1a, 0x45, 0xdf, 0xa3 };
918
919 h = setup_matroskamux_harness (AC3_CAPS_STRING);
920
921 /* Make element seekable */
922 gst_pad_set_query_function (h->sinkpad, seekable_sinkpad_query);
923
924 toc_setter = GST_TOC_SETTER (h->element);
925 fail_unless (toc_setter != NULL);
926
927 if (with_edition) {
928 test_toc = new_reference_toc ();
929 } else {
930 test_toc = new_no_edition_toc ();
931 }
932 gst_toc_setter_set_toc (toc_setter, test_toc);
933 gst_toc_unref (test_toc);
934
935 inbuffer = gst_harness_create_buffer (h, 1);
936 gst_buffer_memset (inbuffer, 0, 0, 1);
937 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
938 GST_BUFFER_DURATION (inbuffer) = 1 * GST_MSECOND;
939 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
940
941 /* send eos to ensure everything is written */
942 fail_unless (gst_harness_push_event (h, gst_event_new_eos ()));
943 ASSERT_MINI_OBJECT_REFCOUNT (test_toc, "test_toc", 1);
944
945 outbuffer = gst_harness_pull (h);
946 fail_unless (outbuffer != NULL);
947
948 /* Merge buffers */
949 merged_buffer = gst_buffer_new ();
950 while (outbuffer != NULL) {
951 if (outbuffer->offset == gst_buffer_get_size (merged_buffer)) {
952 gst_buffer_append_memory (merged_buffer,
953 gst_buffer_get_all_memory (outbuffer));
954 } else {
955 fail_unless (gst_buffer_map (outbuffer, &info, GST_MAP_READ));
956 gst_buffer_fill (merged_buffer, outbuffer->offset, info.data, info.size);
957 gst_buffer_unmap (outbuffer, &info);
958 }
959
960 gst_buffer_unref (outbuffer);
961 outbuffer = gst_harness_try_pull (h);
962 }
963
964 fail_unless (gst_buffer_map (merged_buffer, &info, GST_MAP_READ));
965 index = 0;
966
967 fail_unless (check_id (&info, &index, ebml_header,
968 sizeof (ebml_header), &len));
969 /* skip header */
970 index += len;
971
972 ref_toc = new_reference_toc ();
973 internal_toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
974 check_segment (ref_toc, internal_toc, &info, &index);
975 gst_toc_unref (internal_toc);
976 gst_toc_unref (ref_toc);
977
978 gst_buffer_unmap (merged_buffer, &info);
979 gst_buffer_unref (merged_buffer);
980 gst_harness_teardown (h);
981 }
982
GST_START_TEST(test_toc_with_edition)983 GST_START_TEST (test_toc_with_edition)
984 {
985 test_toc (TRUE);
986 }
987
988 GST_END_TEST;
989
GST_START_TEST(test_toc_without_edition)990 GST_START_TEST (test_toc_without_edition)
991 {
992 test_toc (FALSE);
993 }
994
995 GST_END_TEST;
996
997 static Suite *
matroskamux_suite(void)998 matroskamux_suite (void)
999 {
1000 Suite *s = suite_create ("matroskamux");
1001 TCase *tc_chain = tcase_create ("general");
1002
1003 suite_add_tcase (s, tc_chain);
1004 tcase_add_test (tc_chain, test_ebml_header_v1);
1005 tcase_add_test (tc_chain, test_ebml_header_v2);
1006 tcase_add_test (tc_chain, test_vorbis_header);
1007 tcase_add_test (tc_chain, test_block_group_v1);
1008 tcase_add_test (tc_chain, test_block_group_v2);
1009
1010 tcase_add_test (tc_chain, test_reset);
1011 tcase_add_test (tc_chain, test_link_webmmux_webm_sink);
1012 tcase_add_loop_test (tc_chain, test_timecodescale,
1013 0, G_N_ELEMENTS (timecodescales));
1014
1015 tcase_add_test (tc_chain, test_toc_with_edition);
1016 tcase_add_test (tc_chain, test_toc_without_edition);
1017 return s;
1018 }
1019
1020 GST_CHECK_MAIN (matroskamux);
1021