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.
6 #include "media/base/audio_decoder_config.h"
7 #include "media/base/decoder_buffer.h"
8 #include "media/base/stream_parser_buffer.h"
9 #include "media/base/test_data_util.h"
10 #include "media/base/text_track_config.h"
11 #include "media/base/video_decoder_config.h"
12 #include "media/formats/mp3/mp3_stream_parser.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 class MP3StreamParserTest : public testing::Test {
19 MP3StreamParserTest() {}
22 MP3StreamParser parser_;
23 std::stringstream results_stream_;
25 bool AppendData(const uint8* data, size_t length) {
26 return parser_.Parse(data, length);
29 bool AppendDataInPieces(const uint8* data, size_t length, size_t piece_size) {
30 const uint8* start = data;
31 const uint8* end = data + length;
34 std::min(piece_size, static_cast<size_t>(end - start));
35 if (!AppendData(start, append_size))
42 void OnInitDone(bool success, base::TimeDelta duration) {
43 DVLOG(1) << __FUNCTION__ << "(" << success << ", "
44 << duration.InMilliseconds() << ")";
47 bool OnNewConfig(const AudioDecoderConfig& audio_config,
48 const VideoDecoderConfig& video_config,
49 const StreamParser::TextTrackConfigMap& text_config) {
50 DVLOG(1) << __FUNCTION__ << "(" << audio_config.IsValidConfig() << ", "
51 << video_config.IsValidConfig() << ")";
52 EXPECT_TRUE(audio_config.IsValidConfig());
53 EXPECT_FALSE(video_config.IsValidConfig());
57 std::string BufferQueueToString(const StreamParser::BufferQueue& buffers) {
61 for (StreamParser::BufferQueue::const_iterator itr = buffers.begin();
64 ss << " " << (*itr)->timestamp().InMilliseconds();
65 if ((*itr)->IsKeyframe())
73 bool OnNewBuffers(const StreamParser::BufferQueue& audio_buffers,
74 const StreamParser::BufferQueue& video_buffers,
75 const StreamParser::TextBufferQueueMap& text_map) {
76 EXPECT_FALSE(audio_buffers.empty());
77 EXPECT_TRUE(video_buffers.empty());
79 // TODO(wolenetz/acolwell): Add text track support to more MSE parsers. See
80 // http://crbug.com/336926.
81 EXPECT_TRUE(text_map.empty());
83 std::string buffers_str = BufferQueueToString(audio_buffers);
84 DVLOG(1) << __FUNCTION__ << " : " << buffers_str;
85 results_stream_ << buffers_str;
89 void OnKeyNeeded(const std::string& type,
90 const std::vector<uint8>& init_data) {
91 DVLOG(1) << __FUNCTION__ << "(" << type << ", " << init_data.size() << ")";
95 DVLOG(1) << __FUNCTION__;
96 results_stream_ << "NewSegment";
99 void OnEndOfSegment() {
100 DVLOG(1) << __FUNCTION__;
101 results_stream_ << "EndOfSegment";
104 void InitializeParser() {
106 base::Bind(&MP3StreamParserTest::OnInitDone, base::Unretained(this)),
107 base::Bind(&MP3StreamParserTest::OnNewConfig, base::Unretained(this)),
108 base::Bind(&MP3StreamParserTest::OnNewBuffers, base::Unretained(this)),
110 base::Bind(&MP3StreamParserTest::OnKeyNeeded, base::Unretained(this)),
111 base::Bind(&MP3StreamParserTest::OnNewSegment, base::Unretained(this)),
112 base::Bind(&MP3StreamParserTest::OnEndOfSegment,
113 base::Unretained(this)),
117 std::string ParseFile(const std::string& filename, int append_bytes) {
118 results_stream_.clear();
121 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(filename);
123 AppendDataInPieces(buffer->data(), buffer->data_size(), append_bytes));
124 return results_stream_.str();
128 // Test parsing with small prime sized chunks to smoke out "power of
129 // 2" field size assumptions.
130 TEST_F(MP3StreamParserTest, UnalignedAppend) {
131 std::string expected =
151 EXPECT_EQ(expected, ParseFile("sfx.mp3", 17));
154 // Test parsing with a larger piece size to verify that multiple buffers
155 // are passed to |new_buffer_cb_|.
156 TEST_F(MP3StreamParserTest, UnalignedAppend512) {
157 std::string expected =
160 "{ 26K 52K 78K 104K }"
164 "{ 208K 235K 261K 287K }"
167 EXPECT_EQ(expected, ParseFile("sfx.mp3", 512));