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.
25 #include <gst/check/gstcheck.h>
26 #include <gst/base/gstadapter.h>
27 #include <gst/check/gstharness.h>
29 #define AC3_CAPS_STRING "audio/x-ac3, " \
30 "channels = (int) 1, " \
32 #define VORBIS_TMPL_CAPS_STRING "audio/x-vorbis, " \
33 "channels = (int) 1, " \
34 "rate = (int) 8000, " \
35 "streamheader=(buffer)<10, 2020, 303030>"
38 setup_matroskamux_harness (const gchar * src_pad_str)
42 h = gst_harness_new_with_padnames ("matroskamux", "audio_%u", "src");
43 gst_harness_set_src_caps_str (h, src_pad_str);
44 gst_harness_set_sink_caps_str (h, "video/x-matroska; audio/x-matroska");
50 seekable_sinkpad_query (GstPad * pad, GstObject * parent, GstQuery * query)
54 if (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING) {
55 gst_query_set_seeking (query, GST_FORMAT_BYTES, TRUE, 0, -1);
62 #define compare_buffer_to_data(buffer, data, data_size) \
64 fail_unless_equals_int (data_size, gst_buffer_get_size (buffer)); \
65 fail_unless (gst_buffer_memcmp (buffer, 0, data, data_size) == 0); \
69 test_ebml_header_with_version (gint version,
70 gconstpointer data, gsize data_size)
73 GstBuffer *inbuffer, *outbuffer;
75 h = setup_matroskamux_harness (AC3_CAPS_STRING);
76 g_object_set (h->element, "version", version, NULL);
78 inbuffer = gst_harness_create_buffer (h, 1);
79 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
80 fail_unless_equals_int (2, gst_harness_buffers_received (h));
82 outbuffer = gst_harness_pull (h);
83 compare_buffer_to_data (outbuffer, data, data_size);
84 gst_buffer_unref (outbuffer);
86 gst_harness_teardown (h);
89 GST_START_TEST (test_ebml_header_v1)
92 0x1a, 0x45, 0xdf, 0xa3, /* master ID */
93 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14,
94 0x42, 0x82, /* doctype */
96 0x6d, 0x61, 0x74, 0x72, 0x6f, 0x73, 0x6b, 0x61, 0x00, /* "matroska" */
97 0x42, 0x87, /* doctypeversion */
100 0x42, 0x85, /* doctypereadversion */
105 test_ebml_header_with_version (1, data_v1, sizeof (data_v1));
110 GST_START_TEST (test_ebml_header_v2)
113 0x1a, 0x45, 0xdf, 0xa3, /* master ID */
114 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14,
115 0x42, 0x82, /* doctype */
117 0x6d, 0x61, 0x74, 0x72, 0x6f, 0x73, 0x6b, 0x61, 0x00, /* "matroska" */
118 0x42, 0x87, /* doctypeversion */
121 0x42, 0x85, /* doctypereadversion */
126 test_ebml_header_with_version (2, data_v2, sizeof (data_v2));
132 GST_START_TEST (test_vorbis_header)
135 GstBuffer *inbuffer, *outbuffer;
136 gboolean vorbis_header_found = FALSE;
140 { 0x63, 0xa2, 0x89, 0x02, 0x01, 0x02, 0x10, 0x20, 0x20, 0x30, 0x30,
144 h = setup_matroskamux_harness (VORBIS_TMPL_CAPS_STRING);
146 inbuffer = gst_harness_create_buffer (h, 1);
147 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
149 outbuffer = gst_harness_pull (h);
150 while (outbuffer != NULL) {
151 buffer_size = gst_buffer_get_size (outbuffer);
153 if (!vorbis_header_found && buffer_size >= sizeof (data)) {
154 for (j = 0; j <= buffer_size - sizeof (data); j++) {
155 if (gst_buffer_memcmp (outbuffer, j, data, sizeof (data)) == 0) {
156 vorbis_header_found = TRUE;
162 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
163 gst_buffer_unref (outbuffer);
165 outbuffer = gst_harness_try_pull (h);
168 fail_unless (vorbis_header_found);
170 gst_harness_teardown (h);
177 test_block_group_with_version (gint version,
178 gconstpointer data0, gsize data0_size)
181 GstBuffer *inbuffer, *outbuffer;
182 guint8 data1[] = { 0x42 };
184 h = setup_matroskamux_harness (AC3_CAPS_STRING);
185 g_object_set (h->element, "version", version, NULL);
187 /* Generate the header */
188 inbuffer = gst_harness_create_buffer (h, 1);
189 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
190 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
191 fail_unless_equals_int (5, gst_harness_buffers_received (h));
193 outbuffer = gst_harness_pull (h);
194 fail_unless (outbuffer != NULL);
195 while (outbuffer != NULL) {
196 gst_buffer_unref (outbuffer);
197 outbuffer = gst_harness_try_pull (h);
200 /* Now push a buffer */
201 inbuffer = gst_harness_create_buffer (h, 1);
202 gst_buffer_fill (inbuffer, 0, data1, sizeof (data1));
203 GST_BUFFER_TIMESTAMP (inbuffer) = 1000000;
205 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
207 outbuffer = gst_harness_pull (h);
208 compare_buffer_to_data (outbuffer, data0, data0_size);
209 gst_buffer_unref (outbuffer);
211 outbuffer = gst_harness_pull (h);
212 compare_buffer_to_data (outbuffer, data1, sizeof (data1));
213 gst_buffer_unref (outbuffer);
215 gst_harness_teardown (h);
218 GST_START_TEST (test_block_group_v1)
220 guint8 data0_v1[] = { 0xa0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
222 0x81, 0x00, 0x01, 0x00
225 test_block_group_with_version (1, data0_v1, sizeof (data0_v1));
230 GST_START_TEST (test_block_group_v2)
232 guint8 data0_v2[] = { 0xa3, 0x85, 0x81, 0x00, 0x01, 0x00 };
234 test_block_group_with_version (2, data0_v2, sizeof (data0_v2));
239 GST_START_TEST (test_reset)
243 GstBuffer *outbuffer;
245 h = setup_matroskamux_harness (AC3_CAPS_STRING);
247 inbuffer = gst_harness_create_buffer (h, 1);
248 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
249 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
250 fail_unless_equals_int (5, gst_harness_buffers_received (h));
252 outbuffer = gst_harness_pull (h);
253 fail_unless (outbuffer != NULL);
254 while (outbuffer != NULL) {
255 gst_buffer_unref (outbuffer);
256 outbuffer = gst_harness_try_pull (h);
259 fail_unless_equals_int (GST_STATE_CHANGE_SUCCESS,
260 gst_element_set_state (h->element, GST_STATE_NULL));
262 gst_harness_play (h);
264 inbuffer = gst_harness_create_buffer (h, 1);
265 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
266 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
268 outbuffer = gst_harness_pull (h);
269 fail_unless (outbuffer != NULL);
270 while (outbuffer != NULL) {
271 gst_buffer_unref (outbuffer);
272 outbuffer = gst_harness_try_pull (h);
275 gst_harness_teardown (h);
280 GST_START_TEST (test_link_webmmux_webm_sink)
284 h = gst_harness_new_with_padnames ("webmmux", "audio_%u", "src");
285 fail_unless (h != NULL);
287 gst_harness_set_sink_caps_str (h, "video/webm; audio/webm");
289 gst_harness_play (h);
291 fail_unless_equals_int (GST_STATE_CHANGE_SUCCESS,
292 gst_element_set_state (h->element, GST_STATE_NULL));
294 gst_harness_teardown (h);
299 static gint64 timecodescales[] = {
305 /* FAILS: ? GST_MSECOND * 500, a bug? */
308 GST_START_TEST (test_timecodescale)
310 GstBuffer *inbuffer, *outbuffer;
312 0xa3, 0x85, 0x81, 0x00, 0x00, 0x00,
315 0xa3, 0x85, 0x81, 0x00, 0x01, 0x00,
318 GstHarness *h = setup_matroskamux_harness (AC3_CAPS_STRING);
319 gint64 timecodescale = timecodescales[__i__];
321 g_object_set (h->element, "timecodescale", timecodescale, NULL);
322 g_object_set (h->element, "version", 2, NULL);
325 inbuffer = gst_harness_create_buffer (h, 1);
326 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
327 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
329 /* pull out headers */
330 gst_buffer_unref (gst_harness_pull (h));
331 gst_buffer_unref (gst_harness_pull (h));
332 gst_buffer_unref (gst_harness_pull (h));
334 /* verify header and drop the data */
335 outbuffer = gst_harness_pull (h);
336 compare_buffer_to_data (outbuffer, data_h0, sizeof (data_h0));
337 gst_buffer_unref (outbuffer);
338 gst_buffer_unref (gst_harness_pull (h));
341 inbuffer = gst_harness_create_buffer (h, 1);
342 GST_BUFFER_TIMESTAMP (inbuffer) = timecodescale;
343 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
345 /* verify header and drop the data */
346 outbuffer = gst_harness_pull (h);
347 compare_buffer_to_data (outbuffer, data_h1, sizeof (data_h1));
348 gst_buffer_unref (outbuffer);
349 gst_buffer_unref (gst_harness_pull (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);
598 fail_unless (check_id (info, index, chapter_language,
599 sizeof (chapter_language), &len));
600 /* TODO: define language - always "und" ATM */
604 /* TODO: add remaining fields (not used in current matroska-mux) */
606 g_snprintf (s_uid, sizeof (s_uid), "%" G_GINT64_FORMAT, uid);
607 internal_chapter = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, s_uid);
608 gst_toc_entry_append_sub_entry (internal_toc_entry, internal_chapter);
610 cur_sub_chap = gst_toc_entry_get_sub_entries (toc_entry);
611 while (cur_sub_chap != NULL && *index < last_offset) {
612 check_chapter (cur_sub_chap->data, internal_chapter, info,
614 cur_sub_chap = cur_sub_chap->next;
617 fail_unless (cur_sub_chap == NULL);
620 /* Check the reference toc against the muxed buffer */
622 check_toc (GstToc * ref_toc, GstToc * internal_toc,
623 GstMapInfo * info, gsize * index)
625 guint64 len, value, uid;
628 GList *cur_entry, *cur_chapter;
629 GstTocEntry *internal_edition;
631 guint8 edition_entry[] = { 0x45, 0xb9 };
632 guint8 edition_uid[] = { 0x45, 0xbc };
633 guint8 edition_flag_hidden[] = { 0x45, 0xbd };
634 guint8 edition_flag_default[] = { 0x45, 0xdb };
635 guint8 edition_flag_ordered[] = { 0x45, 0xdd };
638 fail_unless (check_id (info, index, edition_entry,
639 sizeof (edition_entry), &len));
640 last_offset = *index + (gint) len;
642 cur_entry = gst_toc_get_entries (ref_toc);
643 while (cur_entry != NULL && *index < last_offset) {
645 check_id_read_int (info, index, edition_uid, sizeof (edition_uid), &uid);
646 g_snprintf (s_uid, sizeof (s_uid), "%" G_GINT64_FORMAT, uid);
647 internal_edition = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, s_uid);
648 gst_toc_append_entry (internal_toc, internal_edition);
650 fail_unless (check_id_read_int (info, index, edition_flag_hidden,
651 sizeof (edition_flag_hidden), &value));
653 fail_unless (check_id_read_int (info, index, edition_flag_default,
654 sizeof (edition_flag_default), &value));
657 check_id_read_int (info, index, edition_flag_ordered,
658 sizeof (edition_flag_ordered), &value);
660 cur_chapter = gst_toc_entry_get_sub_entries (cur_entry->data);
661 while (cur_chapter != NULL && *index < last_offset) {
662 check_chapter (cur_chapter->data, internal_edition, info,
664 cur_chapter = cur_chapter->next;
666 fail_unless (cur_chapter == NULL);
668 cur_entry = cur_entry->next;
671 fail_unless (cur_entry == NULL && *index == last_offset);
675 find_toc_entry (GstTocEntry * ref_toc_entry, GstTocEntry * internal_toc_entry,
678 GList *cur_ref_entry, *cur_internal_entry;
679 guint64 internal_uid;
680 GstTocEntry *result = NULL;
682 internal_uid = g_ascii_strtoull (gst_toc_entry_get_uid (internal_toc_entry),
684 if (uid == internal_uid) {
685 result = ref_toc_entry;
687 cur_ref_entry = gst_toc_entry_get_sub_entries (ref_toc_entry);
688 cur_internal_entry = gst_toc_entry_get_sub_entries (internal_toc_entry);
689 while (cur_ref_entry != NULL && cur_internal_entry != NULL) {
690 result = find_toc_entry (cur_ref_entry->data, cur_internal_entry->data,
693 if (result != NULL) {
697 cur_ref_entry = cur_ref_entry->next;
698 cur_internal_entry = cur_internal_entry->next;
706 find_and_check_tags (GstToc * ref_toc, GstToc * internal_toc, GstMapInfo * info,
707 guint64 uid, gchar * tag_name, gchar * tag_string)
709 GList *cur_ref_entry, *cur_internal_entry;
710 GstTocEntry *ref_toc_entry = NULL;
712 const gchar *tag_type;
713 gchar *cur_tag_string;
715 /* find the reference toc entry matching the UID */
716 cur_ref_entry = gst_toc_get_entries (ref_toc);
717 cur_internal_entry = gst_toc_get_entries (internal_toc);
718 while (cur_ref_entry != NULL && cur_internal_entry != NULL) {
719 ref_toc_entry = find_toc_entry (cur_ref_entry->data,
720 cur_internal_entry->data, uid);
722 if (ref_toc_entry != NULL) {
726 cur_ref_entry = cur_ref_entry->next;
727 cur_internal_entry = cur_internal_entry->next;
730 fail_unless (ref_toc_entry != NULL);
732 if (g_strcmp0 (tag_name, "ARTIST") == 0) {
733 tag_type = GST_TAG_ARTIST;
734 } else if (g_strcmp0 (tag_name, "COMMENTS") == 0) {
735 tag_type = GST_TAG_COMMENT;
740 fail_unless (tag_type != NULL);
742 tags = gst_toc_entry_get_tags (ref_toc_entry);
743 fail_unless (gst_tag_list_get_tag_size (tags, tag_type) > 0);
744 gst_tag_list_get_string_index (tags, tag_type, 0, &cur_tag_string);
745 fail_unless (g_strcmp0 (cur_tag_string, tag_string) == 0);
749 check_tags (GstToc * ref_toc, GstToc * internal_toc,
750 GstMapInfo * info, gsize * index)
752 gboolean found_tags = FALSE, must_check_tag = FALSE;
753 guint64 len, value, uid;
754 gsize last_offset, next_tag;
755 gchar *tag_name_str, *tag_string_str;
756 guint8 tags[] = { 0x12, 0x54, 0xc3, 0x67 };
757 guint8 tag[] = { 0x73, 0x73 };
758 guint8 tag_targets[] = { 0x63, 0xc0 };
759 guint8 tag_target_type_value[] = { 0x68, 0xca };
760 guint8 tag_target_type[] = { 0x63, 0xca };
761 guint8 tag_edition_uid[] = { 0x63, 0xc9 };
762 guint8 tag_chapter_uid[] = { 0x63, 0xc4 };
763 guint8 simple_tag[] = { 0x67, 0xc8 };
764 guint8 tag_name[] = { 0x45, 0xa3 };
765 guint8 tag_string[] = { 0x44, 0x87 };
767 if (info->size > *index + sizeof (tags)) {
768 for (; *index < info->size - sizeof (tags); ++(*index)) {
769 if (memcmp (info->data + *index, tags, sizeof (tags)) == 0) {
770 *index += sizeof (tags);
772 len = read_length (info, index);
773 last_offset = *index + len;
781 fail_unless (found_tags);
783 while (*index < last_offset) {
784 fail_unless (check_id (info, index, tag, sizeof (tag), &len));
785 next_tag = *index + len;
787 fail_unless (check_id (info, index, tag_targets,
788 sizeof (tag_targets), &len));
790 must_check_tag = FALSE;
791 check_id_read_int (info, index, tag_target_type_value,
792 sizeof (tag_target_type_value), &value);
794 if (check_id (info, index, tag_target_type, sizeof (tag_target_type), &len)) {
798 if (check_id_read_int (info, index, tag_chapter_uid,
799 sizeof (tag_chapter_uid), &uid)) {
800 must_check_tag = TRUE;
801 } else if (check_id_read_int (info, index, tag_edition_uid,
802 sizeof (tag_edition_uid), &uid)) {
803 must_check_tag = TRUE;
806 if (must_check_tag) {
807 fail_unless (check_id (info, index, simple_tag,
808 sizeof (simple_tag), &len));
810 fail_unless (check_id (info, index, tag_name, sizeof (tag_name), &len));
811 tag_name_str = g_strndup ((gchar *) info->data + *index, len);
814 fail_unless (check_id (info, index, tag_string, sizeof (tag_string),
816 tag_string_str = g_strndup ((gchar *) info->data + *index, len);
819 find_and_check_tags (ref_toc, internal_toc, info, uid,
820 tag_name_str, tag_string_str);
822 g_free (tag_name_str);
823 g_free (tag_string_str);
831 check_segment (GstToc * ref_toc, GstToc * internal_toc,
832 GstMapInfo * info, gsize * index)
834 guint8 matroska_segment[] = { 0x18, 0x53, 0x80, 0x67 };
835 guint8 matroska_seek_id_chapters[] = { 0x53, 0xab, 0x84,
836 0x10, 0x43, 0xA7, 0x70
838 guint8 matroska_seek_id_tags[] = { 0x53, 0xab, 0x84,
839 0x12, 0x54, 0xc3, 0x67
841 guint8 matroska_seek_pos[] = { 0x53, 0xac };
842 guint8 matroska_chapters[] = { 0x10, 0x43, 0xA7, 0x70 };
844 guint64 len, value, segment_offset, chapters_offset, tags_offset;
845 gboolean found_chapters_declaration = FALSE, found_tags_declaration = FALSE;
848 fail_unless (info->size > sizeof (matroska_segment));
849 fail_unless (check_id (info, index, matroska_segment,
850 sizeof (matroska_segment), &len));
852 segment_offset = *index;
854 /* Search chapter declaration in seek head */
855 for (; *index < len - sizeof (matroska_seek_id_chapters); ++(*index)) {
856 if (memcmp (info->data + *index, matroska_seek_id_chapters,
857 sizeof (matroska_seek_id_chapters)) == 0) {
858 *index += sizeof (matroska_seek_id_chapters);
860 if (check_id_read_int (info, index, matroska_seek_pos,
861 sizeof (matroska_seek_pos), &value)) {
862 /* found chapter declaration */
863 found_chapters_declaration = TRUE;
864 chapters_offset = segment_offset + value;
870 fail_unless (found_chapters_declaration);
872 *index = chapters_offset;
873 if (check_id (info, index, matroska_chapters,
874 sizeof (matroska_chapters), &len)) {
875 check_toc (ref_toc, internal_toc, info, index);
878 /* Search tags declaration in seek head */
879 for (*index = segment_offset; *index < len - sizeof (matroska_seek_id_tags);
881 if (memcmp (info->data + *index, matroska_seek_id_tags,
882 sizeof (matroska_seek_id_tags)) == 0) {
883 *index += sizeof (matroska_seek_id_tags);
885 if (check_id_read_int (info, index, matroska_seek_pos,
886 sizeof (matroska_seek_pos), &value)) {
887 /* found tags declaration */
888 found_tags_declaration = TRUE;
889 tags_offset = segment_offset + value;
895 fail_unless (found_tags_declaration);
897 *index = tags_offset;
898 check_tags (ref_toc, internal_toc, info, index);
902 test_toc (gboolean with_edition)
905 GstBuffer *inbuffer, *outbuffer, *merged_buffer;
909 GstTocSetter *toc_setter;
910 GstToc *test_toc, *ref_toc, *internal_toc;
912 guint8 ebml_header[] = { 0x1a, 0x45, 0xdf, 0xa3 };
914 h = setup_matroskamux_harness (AC3_CAPS_STRING);
916 /* Make element seekable */
917 gst_pad_set_query_function (h->sinkpad, seekable_sinkpad_query);
919 toc_setter = GST_TOC_SETTER (h->element);
920 fail_unless (toc_setter != NULL);
923 test_toc = new_reference_toc ();
925 test_toc = new_no_edition_toc ();
927 gst_toc_setter_set_toc (toc_setter, test_toc);
928 gst_toc_unref (test_toc);
930 inbuffer = gst_harness_create_buffer (h, 1);
931 gst_buffer_memset (inbuffer, 0, 0, 1);
932 GST_BUFFER_TIMESTAMP (inbuffer) = 0;
933 GST_BUFFER_DURATION (inbuffer) = 1 * GST_MSECOND;
934 fail_unless_equals_int (GST_FLOW_OK, gst_harness_push (h, inbuffer));
936 /* send eos to ensure everything is written */
937 fail_unless (gst_harness_push_event (h, gst_event_new_eos ()));
939 outbuffer = gst_harness_pull (h);
940 fail_unless (outbuffer != NULL);
943 merged_buffer = gst_buffer_new ();
944 while (outbuffer != NULL) {
945 if (outbuffer->offset == gst_buffer_get_size (merged_buffer)) {
946 gst_buffer_append_memory (merged_buffer,
947 gst_buffer_get_all_memory (outbuffer));
949 fail_unless (gst_buffer_map (outbuffer, &info, GST_MAP_READ));
950 gst_buffer_fill (merged_buffer, outbuffer->offset, info.data, info.size);
951 gst_buffer_unmap (outbuffer, &info);
954 gst_buffer_unref (outbuffer);
955 outbuffer = gst_harness_try_pull (h);
958 fail_unless (gst_buffer_map (merged_buffer, &info, GST_MAP_READ));
961 fail_unless (check_id (&info, &index, ebml_header,
962 sizeof (ebml_header), &len));
966 ref_toc = new_reference_toc ();
967 internal_toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
968 check_segment (ref_toc, internal_toc, &info, &index);
969 gst_toc_unref (internal_toc);
970 gst_toc_unref (ref_toc);
972 gst_buffer_unmap (merged_buffer, &info);
973 gst_harness_teardown (h);
976 GST_START_TEST (test_toc_with_edition)
983 GST_START_TEST (test_toc_without_edition)
991 matroskamux_suite (void)
993 Suite *s = suite_create ("matroskamux");
994 TCase *tc_chain = tcase_create ("general");
996 suite_add_tcase (s, tc_chain);
997 tcase_add_test (tc_chain, test_ebml_header_v1);
998 tcase_add_test (tc_chain, test_ebml_header_v2);
999 tcase_add_test (tc_chain, test_vorbis_header);
1000 tcase_add_test (tc_chain, test_block_group_v1);
1001 tcase_add_test (tc_chain, test_block_group_v2);
1003 tcase_add_test (tc_chain, test_reset);
1004 tcase_add_test (tc_chain, test_link_webmmux_webm_sink);
1005 tcase_add_loop_test (tc_chain, test_timecodescale,
1006 0, G_N_ELEMENTS (timecodescales));
1008 tcase_add_test (tc_chain, test_toc_with_edition);
1009 tcase_add_test (tc_chain, test_toc_without_edition);
1013 GST_CHECK_MAIN (matroskamux);