3 * unit test for matroskamux
5 * Copyright (C) <2005> Michal Benes <michal.benes@xeris.cz>
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.
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.
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.
23 #include <gst/check/gstcheck.h>
24 #include <gst/base/gstadapter.h>
25 #include <gst/check/gstharness.h>
27 #define AC3_CAPS_STRING "audio/x-ac3, " \
28 "channels = (int) 1, " \
30 #define VORBIS_TMPL_CAPS_STRING "audio/x-vorbis, " \
31 "channels = (int) 1, " \
32 "rate = (int) 8000, " \
33 "streamheader=(buffer)<10, 2020, 303030>"
36 setup_matroskamux_harness (const gchar * src_pad_str)
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");
48 seekable_sinkpad_query (GstPad * pad, GstObject * parent, GstQuery * query)
52 if (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING) {
53 gst_query_set_seeking (query, GST_FORMAT_BYTES, TRUE, 0, -1);
60 #define compare_buffer_to_data(buffer, data, data_size) \
62 fail_unless_equals_int (data_size, gst_buffer_get_size (buffer)); \
63 fail_unless (gst_buffer_memcmp (buffer, 0, data, data_size) == 0); \
67 test_ebml_header_with_version (gint version,
68 gconstpointer data, gsize data_size)
71 GstBuffer *inbuffer, *outbuffer;
73 h = setup_matroskamux_harness (AC3_CAPS_STRING);
74 g_object_set (h->element, "version", version, NULL);
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));
80 outbuffer = gst_harness_pull (h);
81 compare_buffer_to_data (outbuffer, data, data_size);
82 gst_buffer_unref (outbuffer);
84 gst_harness_teardown (h);
87 GST_START_TEST (test_ebml_header_v1)
90 0x1a, 0x45, 0xdf, 0xa3, /* master ID */
91 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14,
92 0x42, 0x82, /* doctype */
94 0x6d, 0x61, 0x74, 0x72, 0x6f, 0x73, 0x6b, 0x61, 0x00, /* "matroska" */
95 0x42, 0x87, /* doctypeversion */
98 0x42, 0x85, /* doctypereadversion */
103 test_ebml_header_with_version (1, data_v1, sizeof (data_v1));
108 GST_START_TEST (test_ebml_header_v2)
111 0x1a, 0x45, 0xdf, 0xa3, /* master ID */
112 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14,
113 0x42, 0x82, /* doctype */
115 0x6d, 0x61, 0x74, 0x72, 0x6f, 0x73, 0x6b, 0x61, 0x00, /* "matroska" */
116 0x42, 0x87, /* doctypeversion */
119 0x42, 0x85, /* doctypereadversion */
124 test_ebml_header_with_version (2, data_v2, sizeof (data_v2));
130 GST_START_TEST (test_vorbis_header)
133 GstBuffer *inbuffer, *outbuffer;
134 gboolean vorbis_header_found = FALSE;
138 { 0x63, 0xa2, 0x89, 0x02, 0x01, 0x02, 0x10, 0x20, 0x20, 0x30, 0x30,
142 h = setup_matroskamux_harness (VORBIS_TMPL_CAPS_STRING);
144 inbuffer = gst_harness_create_buffer (h, 1);
145 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
147 outbuffer = gst_harness_pull (h);
148 while (outbuffer != NULL) {
149 buffer_size = gst_buffer_get_size (outbuffer);
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;
160 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
161 gst_buffer_unref (outbuffer);
163 outbuffer = gst_harness_try_pull (h);
166 fail_unless (vorbis_header_found);
168 gst_harness_teardown (h);
175 test_block_group_with_version (gint version,
176 gconstpointer data0, gsize data0_size)
179 GstBuffer *inbuffer, *outbuffer;
180 guint8 data1[] = { 0x42 };
182 h = setup_matroskamux_harness (AC3_CAPS_STRING);
183 g_object_set (h->element, "version", version, NULL);
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));
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);
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;
203 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
205 outbuffer = gst_harness_pull (h);
206 compare_buffer_to_data (outbuffer, data0, data0_size);
207 gst_buffer_unref (outbuffer);
209 outbuffer = gst_harness_pull (h);
210 compare_buffer_to_data (outbuffer, data1, sizeof (data1));
211 gst_buffer_unref (outbuffer);
213 gst_harness_teardown (h);
216 GST_START_TEST (test_block_group_v1)
218 guint8 data0_v1[] = { 0xa0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
220 0x81, 0x00, 0x01, 0x00
223 test_block_group_with_version (1, data0_v1, sizeof (data0_v1));
228 GST_START_TEST (test_block_group_v2)
230 guint8 data0_v2[] = { 0xa3, 0x85, 0x81, 0x00, 0x01, 0x00 };
232 test_block_group_with_version (2, data0_v2, sizeof (data0_v2));
237 GST_START_TEST (test_reset)
241 GstBuffer *outbuffer;
243 h = setup_matroskamux_harness (AC3_CAPS_STRING);
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));
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);
257 fail_unless_equals_int (GST_STATE_CHANGE_SUCCESS,
258 gst_element_set_state (h->element, GST_STATE_NULL));
260 gst_harness_play (h);
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));
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);
273 gst_harness_teardown (h);
278 GST_START_TEST (test_link_webmmux_webm_sink)
282 h = gst_harness_new_with_padnames ("webmmux", "audio_%u", "src");
283 fail_unless (h != NULL);
285 gst_harness_set_sink_caps_str (h, "video/webm; audio/webm");
287 gst_harness_play (h);
289 fail_unless_equals_int (GST_STATE_CHANGE_SUCCESS,
290 gst_element_set_state (h->element, GST_STATE_NULL));
292 gst_harness_teardown (h);
297 static gint64 timecodescales[] = {
303 /* FAILS: ? GST_MSECOND * 500, a bug? */
306 GST_START_TEST (test_timecodescale)
308 GstBuffer *inbuffer, *outbuffer;
310 0xa3, 0x85, 0x81, 0x00, 0x00, 0x00,
313 0xa3, 0x85, 0x81, 0x00, 0x01, 0x00,
316 GstHarness *h = setup_matroskamux_harness (AC3_CAPS_STRING);
317 gint64 timecodescale = timecodescales[__i__];
319 g_object_set (h->element, "timecodescale", timecodescale, NULL);
320 g_object_set (h->element, "version", 2, NULL);
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));
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));
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));
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));
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));
349 gst_harness_teardown (h);
354 /* Create a new chapter */
356 new_chapter (const guint chapter_nb, const gint64 start, const gint64 stop)
358 GstTocEntry *toc_entry, *toc_sub_entry;
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);
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);
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);
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);
382 gst_toc_entry_append_sub_entry (toc_entry, toc_sub_entry);
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);
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);
393 gst_toc_entry_append_sub_entry (toc_entry, toc_sub_entry);
398 /* Create a reference toc which includes a master edition entry */
400 new_reference_toc (void)
403 GstTocEntry *toc_edition_entry, *toc_entry;
406 ref_toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
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);
412 toc_entry = new_chapter (1, 0 * GST_MSECOND, 2 * GST_MSECOND);
413 gst_toc_entry_append_sub_entry (toc_edition_entry, toc_entry);
415 toc_entry = new_chapter (2, 2 * GST_MSECOND, 4 * GST_MSECOND);
416 gst_toc_entry_append_sub_entry (toc_edition_entry, toc_entry);
418 gst_toc_append_entry (ref_toc, toc_edition_entry);
423 /* Create a toc which includes chapters without edition entry */
425 new_no_edition_toc (void)
428 GstTocEntry *toc_entry;
430 ref_toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
432 toc_entry = new_chapter (1, 0 * GST_MSECOND, 2 * GST_MSECOND);
433 gst_toc_append_entry (ref_toc, toc_entry);
435 toc_entry = new_chapter (2, 2 * GST_MSECOND, 4 * GST_MSECOND);
436 gst_toc_append_entry (ref_toc, toc_entry);
442 read_integer (GstMapInfo * info, gsize * index, guint64 len)
446 for (; len > 0; --len) {
447 total = (total << 8) | GST_READ_UINT8 (info->data + *index);
455 read_length (GstMapInfo * info, gsize * index)
457 gint len_mask = 0x80, read = 1;
461 b = GST_READ_UINT8 (info->data + *index);
464 while (read <= 8 && !(total & len_mask)) {
468 total &= (len_mask - 1);
470 for (; read > 1; --read) {
471 total = (total << 8) | GST_READ_UINT8 (info->data + *index);
479 check_id (GstMapInfo * info, gsize * index,
480 guint8 * tag, gint tag_len, guint64 * len)
482 if (memcmp (info->data + *index, tag, tag_len) == 0) {
484 *len = read_length (info, index);
492 check_id_read_int (GstMapInfo * info, gsize * index,
493 guint8 * tag, gint tag_len, guint64 * value)
497 if (check_id (info, index, tag, tag_len, &len)) {
498 *value = read_integer (info, index, len);
505 /* Check the toc entry against the muxed buffer
506 * Returns the internal UID */
508 check_chapter (GstTocEntry * toc_entry, GstTocEntry * internal_toc_entry,
509 GstMapInfo * info, gsize * index, gint last_offset)
511 guint64 len, value, uid;
512 gint64 start_ref, end_ref;
514 const gchar *str_uid;
515 GstTocEntry *internal_chapter;
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 };
536 fail_unless (check_id (info, index, chapter_atom,
537 sizeof (chapter_atom), &len));
539 fail_unless (check_id_read_int (info, index, chapter_uid,
540 sizeof (chapter_uid), &uid));
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);
549 gst_toc_entry_get_start_stop_times (toc_entry, &start_ref, &end_ref);
551 fail_unless (check_id_read_int (info, index, chapter_start,
552 sizeof (chapter_start), &value));
553 fail_unless_equals_int (start_ref, value);
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);
561 fail_unless (check_id_read_int (info, index, chapter_flag_hidden,
562 sizeof (chapter_flag_hidden), &value));
564 fail_unless (check_id_read_int (info, index, chapter_flag_enabled,
565 sizeof (chapter_flag_enabled), &value));
567 /* optional segment UID */
568 check_id_read_int (info, index, chapter_segment_uid,
569 sizeof (chapter_segment_uid), &value);
571 /* optional segment edition UID */
572 check_id_read_int (info, index, chapter_segment_edition_uid,
573 sizeof (chapter_segment_edition_uid), &value);
575 /* optional physical equiv */
576 check_id_read_int (info, index, chapter_physical_equiv,
577 sizeof (chapter_physical_equiv), &value);
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));
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));
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);
599 fail_unless (check_id (info, index, chapter_language,
600 sizeof (chapter_language), &len));
601 /* TODO: define language - always "und" ATM */
605 /* TODO: add remaining fields (not used in current matroska-mux) */
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);
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,
615 cur_sub_chap = cur_sub_chap->next;
618 fail_unless (cur_sub_chap == NULL);
621 /* Check the reference toc against the muxed buffer */
623 check_toc (GstToc * ref_toc, GstToc * internal_toc,
624 GstMapInfo * info, gsize * index)
626 guint64 len, value, uid;
629 GList *cur_entry, *cur_chapter;
630 GstTocEntry *internal_edition;
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 };
639 fail_unless (check_id (info, index, edition_entry,
640 sizeof (edition_entry), &len));
641 last_offset = *index + (gint) len;
643 cur_entry = gst_toc_get_entries (ref_toc);
644 while (cur_entry != NULL && *index < last_offset) {
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);
651 fail_unless (check_id_read_int (info, index, edition_flag_hidden,
652 sizeof (edition_flag_hidden), &value));
654 fail_unless (check_id_read_int (info, index, edition_flag_default,
655 sizeof (edition_flag_default), &value));
658 check_id_read_int (info, index, edition_flag_ordered,
659 sizeof (edition_flag_ordered), &value);
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,
665 cur_chapter = cur_chapter->next;
667 fail_unless (cur_chapter == NULL);
669 cur_entry = cur_entry->next;
672 fail_unless (cur_entry == NULL && *index == last_offset);
676 find_toc_entry (GstTocEntry * ref_toc_entry, GstTocEntry * internal_toc_entry,
679 GList *cur_ref_entry, *cur_internal_entry;
680 guint64 internal_uid;
681 GstTocEntry *result = NULL;
683 internal_uid = g_ascii_strtoull (gst_toc_entry_get_uid (internal_toc_entry),
685 if (uid == internal_uid) {
686 result = ref_toc_entry;
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,
694 if (result != NULL) {
698 cur_ref_entry = cur_ref_entry->next;
699 cur_internal_entry = cur_internal_entry->next;
707 find_and_check_tags (GstToc * ref_toc, GstToc * internal_toc, GstMapInfo * info,
708 guint64 uid, gchar * tag_name, gchar * tag_string)
710 GList *cur_ref_entry, *cur_internal_entry;
711 GstTocEntry *ref_toc_entry = NULL;
713 const gchar *tag_type;
714 gchar *cur_tag_string;
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);
723 if (ref_toc_entry != NULL) {
727 cur_ref_entry = cur_ref_entry->next;
728 cur_internal_entry = cur_internal_entry->next;
731 fail_unless (ref_toc_entry != NULL);
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;
741 fail_unless (tag_type != NULL);
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);
751 check_tags (GstToc * ref_toc, GstToc * internal_toc,
752 GstMapInfo * info, gsize * index)
754 gboolean found_tags = FALSE, must_check_tag = FALSE;
755 guint64 len, value, uid;
756 gsize last_offset = 0;
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 };
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);
775 len = read_length (info, index);
776 last_offset = *index + len;
784 fail_unless (found_tags);
786 while (*index < last_offset) {
787 fail_unless (check_id (info, index, tag, sizeof (tag), &len));
788 next_tag = *index + len;
790 fail_unless (check_id (info, index, tag_targets,
791 sizeof (tag_targets), &len));
793 must_check_tag = FALSE;
794 check_id_read_int (info, index, tag_target_type_value,
795 sizeof (tag_target_type_value), &value);
797 if (check_id (info, index, tag_target_type, sizeof (tag_target_type), &len)) {
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;
809 if (must_check_tag) {
810 fail_unless (check_id (info, index, simple_tag,
811 sizeof (simple_tag), &len));
813 fail_unless (check_id (info, index, tag_name, sizeof (tag_name), &len));
814 tag_name_str = g_strndup ((gchar *) info->data + *index, len);
817 fail_unless (check_id (info, index, tag_string, sizeof (tag_string),
819 tag_string_str = g_strndup ((gchar *) info->data + *index, len);
822 find_and_check_tags (ref_toc, internal_toc, info, uid,
823 tag_name_str, tag_string_str);
825 g_free (tag_name_str);
826 g_free (tag_string_str);
834 check_segment (GstToc * ref_toc, GstToc * internal_toc,
835 GstMapInfo * info, gsize * index)
837 guint8 matroska_segment[] = { 0x18, 0x53, 0x80, 0x67 };
838 guint8 matroska_seek_id_chapters[] = { 0x53, 0xab, 0x84,
839 0x10, 0x43, 0xA7, 0x70
841 guint8 matroska_seek_id_tags[] = { 0x53, 0xab, 0x84,
842 0x12, 0x54, 0xc3, 0x67
844 guint8 matroska_seek_pos[] = { 0x53, 0xac };
845 guint8 matroska_chapters[] = { 0x10, 0x43, 0xA7, 0x70 };
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;
853 fail_unless (info->size > sizeof (matroska_segment));
854 fail_unless (check_id (info, index, matroska_segment,
855 sizeof (matroska_segment), &len));
857 segment_offset = *index;
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);
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;
875 fail_unless (found_chapters_declaration);
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);
883 /* Search tags declaration in seek head */
884 for (*index = segment_offset; *index < len - sizeof (matroska_seek_id_tags);
886 if (memcmp (info->data + *index, matroska_seek_id_tags,
887 sizeof (matroska_seek_id_tags)) == 0) {
888 *index += sizeof (matroska_seek_id_tags);
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;
900 fail_unless (found_tags_declaration);
902 *index = tags_offset;
903 check_tags (ref_toc, internal_toc, info, index);
907 test_toc (gboolean with_edition)
910 GstBuffer *inbuffer, *outbuffer, *merged_buffer;
914 GstTocSetter *toc_setter;
915 GstToc *test_toc, *ref_toc, *internal_toc;
917 guint8 ebml_header[] = { 0x1a, 0x45, 0xdf, 0xa3 };
919 h = setup_matroskamux_harness (AC3_CAPS_STRING);
921 /* Make element seekable */
922 gst_pad_set_query_function (h->sinkpad, seekable_sinkpad_query);
924 toc_setter = GST_TOC_SETTER (h->element);
925 fail_unless (toc_setter != NULL);
928 test_toc = new_reference_toc ();
930 test_toc = new_no_edition_toc ();
932 gst_toc_setter_set_toc (toc_setter, test_toc);
933 gst_toc_unref (test_toc);
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));
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);
945 outbuffer = gst_harness_pull (h);
946 fail_unless (outbuffer != NULL);
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));
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);
960 gst_buffer_unref (outbuffer);
961 outbuffer = gst_harness_try_pull (h);
964 fail_unless (gst_buffer_map (merged_buffer, &info, GST_MAP_READ));
967 fail_unless (check_id (&info, &index, ebml_header,
968 sizeof (ebml_header), &len));
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);
978 gst_buffer_unmap (merged_buffer, &info);
979 gst_buffer_unref (merged_buffer);
980 gst_harness_teardown (h);
983 GST_START_TEST (test_toc_with_edition)
990 GST_START_TEST (test_toc_without_edition)
998 matroskamux_suite (void)
1000 Suite *s = suite_create ("matroskamux");
1001 TCase *tc_chain = tcase_create ("general");
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);
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));
1015 tcase_add_test (tc_chain, test_toc_with_edition);
1016 tcase_add_test (tc_chain, test_toc_without_edition);
1020 GST_CHECK_MAIN (matroskamux);