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/channel_layout.h"
7 #include "media/base/mock_media_log.h"
8 #include "media/base/timestamp_constants.h"
9 #include "media/formats/webm/tracks_builder.h"
10 #include "media/formats/webm/webm_constants.h"
11 #include "media/formats/webm/webm_tracks_parser.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using ::testing::HasSubstr
;
16 using ::testing::InSequence
;
17 using ::testing::Return
;
18 using ::testing::StrictMock
;
23 static const double kDefaultTimecodeScaleInUs
= 1000.0; // 1 ms resolution
25 class WebMTracksParserTest
: public testing::Test
{
27 WebMTracksParserTest() : media_log_(new StrictMock
<MockMediaLog
>()) {}
30 void VerifyTextTrackInfo(const uint8
* buffer
,
33 const std::string
& name
,
34 const std::string
& language
) {
35 scoped_ptr
<WebMTracksParser
> parser(
36 new WebMTracksParser(media_log_
, false));
38 int result
= parser
->Parse(buffer
, buffer_size
);
40 EXPECT_EQ(result
, buffer_size
);
42 const WebMTracksParser::TextTracks
& text_tracks
= parser
->text_tracks();
43 EXPECT_EQ(text_tracks
.size(), WebMTracksParser::TextTracks::size_type(1));
45 const WebMTracksParser::TextTracks::const_iterator itr
=
47 EXPECT_EQ(itr
->first
, 1); // track num
49 const TextTrackConfig
& config
= itr
->second
;
50 EXPECT_EQ(config
.kind(), text_kind
);
51 EXPECT_TRUE(config
.label() == name
);
52 EXPECT_TRUE(config
.language() == language
);
55 scoped_refptr
<StrictMock
<MockMediaLog
>> media_log_
;
58 TEST_F(WebMTracksParserTest
, SubtitleNoNameNoLang
) {
62 tb
.AddTextTrack(1, 1, kWebMCodecSubtitles
, "", "");
64 const std::vector
<uint8
> buf
= tb
.Finish();
65 VerifyTextTrackInfo(&buf
[0], buf
.size(), kTextSubtitles
, "", "");
68 TEST_F(WebMTracksParserTest
, SubtitleYesNameNoLang
) {
72 tb
.AddTextTrack(1, 1, kWebMCodecSubtitles
, "Spock", "");
74 const std::vector
<uint8
> buf
= tb
.Finish();
75 VerifyTextTrackInfo(&buf
[0], buf
.size(), kTextSubtitles
, "Spock", "");
78 TEST_F(WebMTracksParserTest
, SubtitleNoNameYesLang
) {
82 tb
.AddTextTrack(1, 1, kWebMCodecSubtitles
, "", "eng");
84 const std::vector
<uint8
> buf
= tb
.Finish();
85 VerifyTextTrackInfo(&buf
[0], buf
.size(), kTextSubtitles
, "", "eng");
88 TEST_F(WebMTracksParserTest
, SubtitleYesNameYesLang
) {
92 tb
.AddTextTrack(1, 1, kWebMCodecSubtitles
, "Picard", "fre");
94 const std::vector
<uint8
> buf
= tb
.Finish();
95 VerifyTextTrackInfo(&buf
[0], buf
.size(), kTextSubtitles
, "Picard", "fre");
98 TEST_F(WebMTracksParserTest
, IgnoringTextTracks
) {
102 tb
.AddTextTrack(1, 1, kWebMCodecSubtitles
, "Subtitles", "fre");
103 tb
.AddTextTrack(2, 2, kWebMCodecSubtitles
, "Commentary", "fre");
105 const std::vector
<uint8
> buf
= tb
.Finish();
106 scoped_ptr
<WebMTracksParser
> parser(new WebMTracksParser(media_log_
, true));
108 EXPECT_MEDIA_LOG(HasSubstr("Ignoring text track 1"));
109 EXPECT_MEDIA_LOG(HasSubstr("Ignoring text track 2"));
111 int result
= parser
->Parse(&buf
[0], buf
.size());
112 EXPECT_GT(result
, 0);
113 EXPECT_EQ(result
, static_cast<int>(buf
.size()));
115 EXPECT_EQ(parser
->text_tracks().size(), 0u);
117 const std::set
<int64
>& ignored_tracks
= parser
->ignored_tracks();
118 EXPECT_TRUE(ignored_tracks
.find(1) != ignored_tracks
.end());
119 EXPECT_TRUE(ignored_tracks
.find(2) != ignored_tracks
.end());
121 // Test again w/o ignoring the test tracks.
122 parser
.reset(new WebMTracksParser(media_log_
, false));
124 result
= parser
->Parse(&buf
[0], buf
.size());
125 EXPECT_GT(result
, 0);
127 EXPECT_EQ(parser
->ignored_tracks().size(), 0u);
128 EXPECT_EQ(parser
->text_tracks().size(), 2u);
131 TEST_F(WebMTracksParserTest
, AudioVideoDefaultDurationUnset
) {
132 // Other audio/video decoder config fields are necessary in the test
133 // audio/video TrackEntry configurations. This method does only very minimal
134 // verification of their inclusion and parsing; the goal is to confirm
135 // TrackEntry DefaultDuration defaults to -1 if not included in audio or
138 tb
.AddAudioTrack(1, 1, "A_VORBIS", "audio", "", -1, 2, 8000);
139 tb
.AddVideoTrack(2, 2, "V_VP8", "video", "", -1, 320, 240);
140 const std::vector
<uint8
> buf
= tb
.Finish();
142 scoped_ptr
<WebMTracksParser
> parser(new WebMTracksParser(media_log_
, true));
143 int result
= parser
->Parse(&buf
[0], buf
.size());
144 EXPECT_LE(0, result
);
145 EXPECT_EQ(static_cast<int>(buf
.size()), result
);
147 EXPECT_EQ(kNoTimestamp(),
148 parser
->GetAudioDefaultDuration(kDefaultTimecodeScaleInUs
));
149 EXPECT_EQ(kNoTimestamp(),
150 parser
->GetVideoDefaultDuration(kDefaultTimecodeScaleInUs
));
152 const VideoDecoderConfig
& video_config
= parser
->video_decoder_config();
153 EXPECT_TRUE(video_config
.IsValidConfig());
154 EXPECT_EQ(320, video_config
.coded_size().width());
155 EXPECT_EQ(240, video_config
.coded_size().height());
157 const AudioDecoderConfig
& audio_config
= parser
->audio_decoder_config();
158 EXPECT_TRUE(audio_config
.IsValidConfig());
159 EXPECT_EQ(CHANNEL_LAYOUT_STEREO
, audio_config
.channel_layout());
160 EXPECT_EQ(8000, audio_config
.samples_per_second());
163 TEST_F(WebMTracksParserTest
, AudioVideoDefaultDurationSet
) {
164 // Confirm audio or video TrackEntry DefaultDuration values are parsed, if
167 tb
.AddAudioTrack(1, 1, "A_VORBIS", "audio", "", 12345678, 2, 8000);
168 tb
.AddVideoTrack(2, 2, "V_VP8", "video", "", 987654321, 320, 240);
169 const std::vector
<uint8
> buf
= tb
.Finish();
171 scoped_ptr
<WebMTracksParser
> parser(new WebMTracksParser(media_log_
, true));
172 int result
= parser
->Parse(&buf
[0], buf
.size());
173 EXPECT_LE(0, result
);
174 EXPECT_EQ(static_cast<int>(buf
.size()), result
);
176 EXPECT_EQ(base::TimeDelta::FromMicroseconds(12000),
177 parser
->GetAudioDefaultDuration(kDefaultTimecodeScaleInUs
));
178 EXPECT_EQ(base::TimeDelta::FromMicroseconds(985000),
179 parser
->GetVideoDefaultDuration(5000.0)); // 5 ms resolution
180 EXPECT_EQ(kNoTimestamp(), parser
->GetAudioDefaultDuration(12346.0));
181 EXPECT_EQ(base::TimeDelta::FromMicroseconds(12345),
182 parser
->GetAudioDefaultDuration(12345.0));
183 EXPECT_EQ(base::TimeDelta::FromMicroseconds(12003),
184 parser
->GetAudioDefaultDuration(1000.3)); // 1.0003 ms resolution
187 TEST_F(WebMTracksParserTest
, InvalidZeroDefaultDurationSet
) {
188 // Confirm parse error if TrackEntry DefaultDuration is present, but is 0ns.
189 TracksBuilder
tb(true);
190 tb
.AddAudioTrack(1, 1, "A_VORBIS", "audio", "", 0, 2, 8000);
191 const std::vector
<uint8
> buf
= tb
.Finish();
193 scoped_ptr
<WebMTracksParser
> parser(new WebMTracksParser(media_log_
, true));
195 EXPECT_MEDIA_LOG(HasSubstr("Illegal 0ns audio TrackEntry DefaultDuration"));
197 EXPECT_EQ(-1, parser
->Parse(&buf
[0], buf
.size()));
200 TEST_F(WebMTracksParserTest
, HighTrackUID
) {
201 // Confirm no parse error if TrackEntry TrackUID has MSb set
202 // (http://crbug.com/397067).
203 TracksBuilder
tb(true);
204 tb
.AddAudioTrack(1, 1ULL << 31, "A_VORBIS", "audio", "", 40, 2, 8000);
205 const std::vector
<uint8
> buf
= tb
.Finish();
207 scoped_ptr
<WebMTracksParser
> parser(new WebMTracksParser(media_log_
, true));
208 EXPECT_GT(parser
->Parse(&buf
[0], buf
.size()),0);