Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / media / formats / mp3 / mp3_stream_parser_unittest.cc
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.
4
5 #include "base/bind.h"
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"
14
15 namespace media {
16
17 class MP3StreamParserTest : public testing::Test {
18  public:
19   MP3StreamParserTest() {}
20
21  protected:
22   MP3StreamParser parser_;
23   std::stringstream results_stream_;
24
25   bool AppendData(const uint8* data, size_t length) {
26     return parser_.Parse(data, length);
27   }
28
29   bool AppendDataInPieces(const uint8* data, size_t length, size_t piece_size) {
30     const uint8* start = data;
31     const uint8* end = data + length;
32     while (start < end) {
33       size_t append_size =
34           std::min(piece_size, static_cast<size_t>(end - start));
35       if (!AppendData(start, append_size))
36         return false;
37       start += append_size;
38     }
39     return true;
40   }
41
42   void OnInitDone(bool success, base::TimeDelta duration) {
43     DVLOG(1) << __FUNCTION__ << "(" << success << ", "
44              << duration.InMilliseconds() << ")";
45   }
46
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());
54     return true;
55   }
56
57   std::string BufferQueueToString(const StreamParser::BufferQueue& buffers) {
58     std::stringstream ss;
59
60     ss << "{";
61     for (StreamParser::BufferQueue::const_iterator itr = buffers.begin();
62          itr != buffers.end();
63          ++itr) {
64       ss << " " << (*itr)->timestamp().InMilliseconds();
65       if ((*itr)->IsKeyframe())
66         ss << "K";
67     }
68     ss << " }";
69
70     return ss.str();
71   }
72
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());
78
79     // TODO(wolenetz/acolwell): Add text track support to more MSE parsers. See
80     // http://crbug.com/336926.
81     EXPECT_TRUE(text_map.empty());
82
83     std::string buffers_str = BufferQueueToString(audio_buffers);
84     DVLOG(1) << __FUNCTION__ << " : " << buffers_str;
85     results_stream_ << buffers_str;
86     return true;
87   }
88
89   void OnKeyNeeded(const std::string& type,
90                    const std::vector<uint8>& init_data) {
91     DVLOG(1) << __FUNCTION__ << "(" << type << ", " << init_data.size() << ")";
92   }
93
94   void OnNewSegment() {
95     DVLOG(1) << __FUNCTION__;
96     results_stream_ << "NewSegment";
97   }
98
99   void OnEndOfSegment() {
100     DVLOG(1) << __FUNCTION__;
101     results_stream_ << "EndOfSegment";
102   }
103
104   void InitializeParser() {
105     parser_.Init(
106         base::Bind(&MP3StreamParserTest::OnInitDone, base::Unretained(this)),
107         base::Bind(&MP3StreamParserTest::OnNewConfig, base::Unretained(this)),
108         base::Bind(&MP3StreamParserTest::OnNewBuffers, base::Unretained(this)),
109         true,
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)),
114         LogCB());
115   }
116
117   std::string ParseFile(const std::string& filename, int append_bytes) {
118     results_stream_.clear();
119     InitializeParser();
120
121     scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(filename);
122     EXPECT_TRUE(
123         AppendDataInPieces(buffer->data(), buffer->data_size(), append_bytes));
124     return results_stream_.str();
125   }
126 };
127
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 =
132       "NewSegment"
133       "{ 0K }"
134       "{ 26K }"
135       "{ 52K }"
136       "{ 78K }"
137       "{ 104K }"
138       "{ 130K }"
139       "{ 156K }"
140       "{ 182K }"
141       "EndOfSegment"
142       "NewSegment"
143       "{ 208K }"
144       "{ 235K }"
145       "{ 261K }"
146       "EndOfSegment"
147       "NewSegment"
148       "{ 287K }"
149       "{ 313K }"
150       "EndOfSegment";
151   EXPECT_EQ(expected, ParseFile("sfx.mp3", 17));
152 }
153
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 =
158       "NewSegment"
159       "{ 0K }"
160       "{ 26K 52K 78K 104K }"
161       "EndOfSegment"
162       "NewSegment"
163       "{ 130K 156K 182K }"
164       "{ 208K 235K 261K 287K }"
165       "{ 313K }"
166       "EndOfSegment";
167   EXPECT_EQ(expected, ParseFile("sfx.mp3", 512));
168 }
169
170 }  // namespace media