1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/logging.h"
6 #include "media/base/buffers.h"
7 #include "media/base/channel_layout.h"
8 #include "media/formats/webm/tracks_builder.h"
9 #include "media/formats/webm/webm_constants.h"
10 #include "media/formats/webm/webm_tracks_parser.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using ::testing::InSequence
;
15 using ::testing::Return
;
20 static const double kDefaultTimecodeScaleInUs
= 1000.0; // 1 ms resolution
22 class WebMTracksParserTest
: public testing::Test
{
24 WebMTracksParserTest() {}
27 static void VerifyTextTrackInfo(const uint8
* buffer
,
30 const std::string
& name
,
31 const std::string
& language
) {
32 scoped_ptr
<WebMTracksParser
> parser(
33 new WebMTracksParser(new MediaLog(), false));
35 int result
= parser
->Parse(buffer
, buffer_size
);
37 EXPECT_EQ(result
, buffer_size
);
39 const WebMTracksParser::TextTracks
& text_tracks
= parser
->text_tracks();
40 EXPECT_EQ(text_tracks
.size(), WebMTracksParser::TextTracks::size_type(1));
42 const WebMTracksParser::TextTracks::const_iterator itr
= text_tracks
.begin();
43 EXPECT_EQ(itr
->first
, 1); // track num
45 const TextTrackConfig
& config
= itr
->second
;
46 EXPECT_EQ(config
.kind(), text_kind
);
47 EXPECT_TRUE(config
.label() == name
);
48 EXPECT_TRUE(config
.language() == language
);
51 TEST_F(WebMTracksParserTest
, SubtitleNoNameNoLang
) {
55 tb
.AddTextTrack(1, 1, kWebMCodecSubtitles
, "", "");
57 const std::vector
<uint8
> buf
= tb
.Finish();
58 VerifyTextTrackInfo(&buf
[0], buf
.size(), kTextSubtitles
, "", "");
61 TEST_F(WebMTracksParserTest
, SubtitleYesNameNoLang
) {
65 tb
.AddTextTrack(1, 1, kWebMCodecSubtitles
, "Spock", "");
67 const std::vector
<uint8
> buf
= tb
.Finish();
68 VerifyTextTrackInfo(&buf
[0], buf
.size(), kTextSubtitles
, "Spock", "");
71 TEST_F(WebMTracksParserTest
, SubtitleNoNameYesLang
) {
75 tb
.AddTextTrack(1, 1, kWebMCodecSubtitles
, "", "eng");
77 const std::vector
<uint8
> buf
= tb
.Finish();
78 VerifyTextTrackInfo(&buf
[0], buf
.size(), kTextSubtitles
, "", "eng");
81 TEST_F(WebMTracksParserTest
, SubtitleYesNameYesLang
) {
85 tb
.AddTextTrack(1, 1, kWebMCodecSubtitles
, "Picard", "fre");
87 const std::vector
<uint8
> buf
= tb
.Finish();
88 VerifyTextTrackInfo(&buf
[0], buf
.size(), kTextSubtitles
, "Picard", "fre");
91 TEST_F(WebMTracksParserTest
, IgnoringTextTracks
) {
95 tb
.AddTextTrack(1, 1, kWebMCodecSubtitles
, "Subtitles", "fre");
96 tb
.AddTextTrack(2, 2, kWebMCodecSubtitles
, "Commentary", "fre");
98 const std::vector
<uint8
> buf
= tb
.Finish();
99 scoped_ptr
<WebMTracksParser
> parser(
100 new WebMTracksParser(new MediaLog(), true));
102 int result
= parser
->Parse(&buf
[0], buf
.size());
103 EXPECT_GT(result
, 0);
104 EXPECT_EQ(result
, static_cast<int>(buf
.size()));
106 EXPECT_EQ(parser
->text_tracks().size(), 0u);
108 const std::set
<int64
>& ignored_tracks
= parser
->ignored_tracks();
109 EXPECT_TRUE(ignored_tracks
.find(1) != ignored_tracks
.end());
110 EXPECT_TRUE(ignored_tracks
.find(2) != ignored_tracks
.end());
112 // Test again w/o ignoring the test tracks.
113 parser
.reset(new WebMTracksParser(new MediaLog(), false));
115 result
= parser
->Parse(&buf
[0], buf
.size());
116 EXPECT_GT(result
, 0);
118 EXPECT_EQ(parser
->ignored_tracks().size(), 0u);
119 EXPECT_EQ(parser
->text_tracks().size(), 2u);
122 TEST_F(WebMTracksParserTest
, AudioVideoDefaultDurationUnset
) {
123 // Other audio/video decoder config fields are necessary in the test
124 // audio/video TrackEntry configurations. This method does only very minimal
125 // verification of their inclusion and parsing; the goal is to confirm
126 // TrackEntry DefaultDuration defaults to -1 if not included in audio or
129 tb
.AddAudioTrack(1, 1, "A_VORBIS", "audio", "", -1, 2, 8000);
130 tb
.AddVideoTrack(2, 2, "V_VP8", "video", "", -1, 320, 240);
131 const std::vector
<uint8
> buf
= tb
.Finish();
133 scoped_ptr
<WebMTracksParser
> parser(
134 new WebMTracksParser(new MediaLog(), true));
135 int result
= parser
->Parse(&buf
[0], buf
.size());
136 EXPECT_LE(0, result
);
137 EXPECT_EQ(static_cast<int>(buf
.size()), result
);
139 EXPECT_EQ(kNoTimestamp(),
140 parser
->GetAudioDefaultDuration(kDefaultTimecodeScaleInUs
));
141 EXPECT_EQ(kNoTimestamp(),
142 parser
->GetVideoDefaultDuration(kDefaultTimecodeScaleInUs
));
144 const VideoDecoderConfig
& video_config
= parser
->video_decoder_config();
145 EXPECT_TRUE(video_config
.IsValidConfig());
146 EXPECT_EQ(320, video_config
.coded_size().width());
147 EXPECT_EQ(240, video_config
.coded_size().height());
149 const AudioDecoderConfig
& audio_config
= parser
->audio_decoder_config();
150 EXPECT_TRUE(audio_config
.IsValidConfig());
151 EXPECT_EQ(CHANNEL_LAYOUT_STEREO
, audio_config
.channel_layout());
152 EXPECT_EQ(8000, audio_config
.samples_per_second());
155 TEST_F(WebMTracksParserTest
, AudioVideoDefaultDurationSet
) {
156 // Confirm audio or video TrackEntry DefaultDuration values are parsed, if
159 tb
.AddAudioTrack(1, 1, "A_VORBIS", "audio", "", 12345678, 2, 8000);
160 tb
.AddVideoTrack(2, 2, "V_VP8", "video", "", 987654321, 320, 240);
161 const std::vector
<uint8
> buf
= tb
.Finish();
163 scoped_ptr
<WebMTracksParser
> parser(
164 new WebMTracksParser(new MediaLog(), true));
165 int result
= parser
->Parse(&buf
[0], buf
.size());
166 EXPECT_LE(0, result
);
167 EXPECT_EQ(static_cast<int>(buf
.size()), result
);
169 EXPECT_EQ(base::TimeDelta::FromMicroseconds(12000),
170 parser
->GetAudioDefaultDuration(kDefaultTimecodeScaleInUs
));
171 EXPECT_EQ(base::TimeDelta::FromMicroseconds(985000),
172 parser
->GetVideoDefaultDuration(5000.0)); // 5 ms resolution
173 EXPECT_EQ(kNoTimestamp(), parser
->GetAudioDefaultDuration(12346.0));
174 EXPECT_EQ(base::TimeDelta::FromMicroseconds(12345),
175 parser
->GetAudioDefaultDuration(12345.0));
176 EXPECT_EQ(base::TimeDelta::FromMicroseconds(12003),
177 parser
->GetAudioDefaultDuration(1000.3)); // 1.0003 ms resolution
180 TEST_F(WebMTracksParserTest
, InvalidZeroDefaultDurationSet
) {
181 // Confirm parse error if TrackEntry DefaultDuration is present, but is 0ns.
182 TracksBuilder
tb(true);
183 tb
.AddAudioTrack(1, 1, "A_VORBIS", "audio", "", 0, 2, 8000);
184 const std::vector
<uint8
> buf
= tb
.Finish();
186 scoped_ptr
<WebMTracksParser
> parser(
187 new WebMTracksParser(new MediaLog(), true));
188 EXPECT_EQ(-1, parser
->Parse(&buf
[0], buf
.size()));
191 TEST_F(WebMTracksParserTest
, HighTrackUID
) {
192 // Confirm no parse error if TrackEntry TrackUID has MSb set
193 // (http://crbug.com/397067).
194 TracksBuilder
tb(true);
195 tb
.AddAudioTrack(1, 1ULL << 31, "A_VORBIS", "audio", "", 40, 2, 8000);
196 const std::vector
<uint8
> buf
= tb
.Finish();
198 scoped_ptr
<WebMTracksParser
> parser(
199 new WebMTracksParser(new MediaLog(), true));
200 EXPECT_GT(parser
->Parse(&buf
[0], buf
.size()),0);