Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: media/filters/audio_file_reader_unittest.cc

Issue 2655783004: Decode entire in-memory file for WebAudio (Closed)
Patch Set: Use int, not size_t. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/filters/audio_file_reader.h" 5 #include "media/filters/audio_file_reader.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
(...skipping 17 matching lines...) Expand all
28 28
29 void Initialize(const char* filename) { 29 void Initialize(const char* filename) {
30 data_ = ReadTestDataFile(filename); 30 data_ = ReadTestDataFile(filename);
31 protocol_.reset( 31 protocol_.reset(
32 new InMemoryUrlProtocol(data_->data(), data_->data_size(), false)); 32 new InMemoryUrlProtocol(data_->data(), data_->data_size(), false));
33 reader_.reset(new AudioFileReader(protocol_.get())); 33 reader_.reset(new AudioFileReader(protocol_.get()));
34 } 34 }
35 35
36 // Reads and the entire file provided to Initialize(). 36 // Reads and the entire file provided to Initialize().
37 void ReadAndVerify(const char* expected_audio_hash, int expected_frames) { 37 void ReadAndVerify(const char* expected_audio_hash, int expected_frames) {
38 std::vector<std::unique_ptr<AudioBus>> decoded_audio_packets;
39 int actual_frames = reader_->Read(&decoded_audio_packets);
38 std::unique_ptr<AudioBus> decoded_audio_data = 40 std::unique_ptr<AudioBus> decoded_audio_data =
39 AudioBus::Create(reader_->channels(), reader_->GetNumberOfFrames()); 41 AudioBus::Create(reader_->channels(), actual_frames);
40 int actual_frames = reader_->Read(decoded_audio_data.get()); 42 int dest_start_frame = 0;
43 for (size_t k = 0; k < decoded_audio_packets.size(); ++k) {
44 const AudioBus* packet = decoded_audio_packets[k].get();
45 int frame_count = packet->frames();
46 packet->CopyPartialFramesTo(0, frame_count, dest_start_frame,
47 decoded_audio_data.get());
48 dest_start_frame += frame_count;
49 }
41 ASSERT_LE(actual_frames, decoded_audio_data->frames()); 50 ASSERT_LE(actual_frames, decoded_audio_data->frames());
42 ASSERT_EQ(expected_frames, actual_frames); 51 ASSERT_EQ(expected_frames, actual_frames);
43 52
44 AudioHash audio_hash; 53 AudioHash audio_hash;
45 audio_hash.Update(decoded_audio_data.get(), actual_frames); 54 audio_hash.Update(decoded_audio_data.get(), actual_frames);
46 EXPECT_EQ(expected_audio_hash, audio_hash.ToString()); 55 EXPECT_EQ(expected_audio_hash, audio_hash.ToString());
47 } 56 }
48 57
49 // Verify packets are consistent across demuxer runs. Reads the first few 58 // Verify packets are consistent across demuxer runs. Reads the first few
50 // packets and then seeks back to the start timestamp and verifies that the 59 // packets and then seeks back to the start timestamp and verifies that the
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 ASSERT_TRUE(reader_->SeekForTesting(start_timestamp)); 91 ASSERT_TRUE(reader_->SeekForTesting(start_timestamp));
83 } 92 }
84 } 93 }
85 94
86 void RunTest(const char* fn, 95 void RunTest(const char* fn,
87 const char* hash, 96 const char* hash,
88 int channels, 97 int channels,
89 int sample_rate, 98 int sample_rate,
90 base::TimeDelta duration, 99 base::TimeDelta duration,
91 int frames, 100 int frames,
92 int trimmed_frames) { 101 int expected_frames) {
93 Initialize(fn); 102 Initialize(fn);
94 ASSERT_TRUE(reader_->Open()); 103 ASSERT_TRUE(reader_->Open());
95 EXPECT_EQ(channels, reader_->channels()); 104 EXPECT_EQ(channels, reader_->channels());
96 EXPECT_EQ(sample_rate, reader_->sample_rate()); 105 EXPECT_EQ(sample_rate, reader_->sample_rate());
97 EXPECT_EQ(duration.InMicroseconds(), 106 if (frames >= 0) {
98 reader_->GetDuration().InMicroseconds()); 107 EXPECT_EQ(duration.InMicroseconds(),
99 EXPECT_EQ(frames, reader_->GetNumberOfFrames()); 108 reader_->GetDuration().InMicroseconds());
109 EXPECT_EQ(frames, reader_->GetNumberOfFrames());
110 EXPECT_EQ(reader_->HasKnownDuration(), true);
111 } else {
112 EXPECT_EQ(reader_->HasKnownDuration(), false);
113 }
100 if (!packet_verification_disabled_) 114 if (!packet_verification_disabled_)
101 ASSERT_NO_FATAL_FAILURE(VerifyPackets()); 115 ASSERT_NO_FATAL_FAILURE(VerifyPackets());
102 ReadAndVerify(hash, trimmed_frames); 116 ReadAndVerify(hash, expected_frames);
103 } 117 }
104 118
105 void RunTestFailingDemux(const char* fn) { 119 void RunTestFailingDemux(const char* fn) {
106 Initialize(fn); 120 Initialize(fn);
107 EXPECT_FALSE(reader_->Open()); 121 EXPECT_FALSE(reader_->Open());
108 } 122 }
109 123
110 void RunTestFailingDecode(const char* fn) { 124 void RunTestFailingDecode(const char* fn) {
111 Initialize(fn); 125 Initialize(fn);
112 EXPECT_TRUE(reader_->Open()); 126 EXPECT_TRUE(reader_->Open());
113 std::unique_ptr<AudioBus> decoded_audio_data = 127 std::vector<std::unique_ptr<AudioBus>> decoded_audio_packets;
114 AudioBus::Create(reader_->channels(), reader_->GetNumberOfFrames()); 128 EXPECT_EQ(reader_->Read(&decoded_audio_packets), 0);
115 EXPECT_EQ(reader_->Read(decoded_audio_data.get()), 0);
116 } 129 }
117 130
118 void disable_packet_verification() { 131 void disable_packet_verification() {
119 packet_verification_disabled_ = true; 132 packet_verification_disabled_ = true;
120 } 133 }
121 134
122 protected: 135 protected:
123 scoped_refptr<DecoderBuffer> data_; 136 scoped_refptr<DecoderBuffer> data_;
124 std::unique_ptr<InMemoryUrlProtocol> protocol_; 137 std::unique_ptr<InMemoryUrlProtocol> protocol_;
125 std::unique_ptr<AudioFileReader> reader_; 138 std::unique_ptr<AudioFileReader> reader_;
126 bool packet_verification_disabled_; 139 bool packet_verification_disabled_;
127 140
128 DISALLOW_COPY_AND_ASSIGN(AudioFileReaderTest); 141 DISALLOW_COPY_AND_ASSIGN(AudioFileReaderTest);
129 }; 142 };
130 143
131 TEST_F(AudioFileReaderTest, WithoutOpen) { 144 TEST_F(AudioFileReaderTest, WithoutOpen) {
132 Initialize("bear.ogv"); 145 Initialize("bear.ogv");
133 } 146 }
134 147
135 TEST_F(AudioFileReaderTest, InvalidFile) { 148 TEST_F(AudioFileReaderTest, InvalidFile) {
136 RunTestFailingDemux("ten_byte_file"); 149 RunTestFailingDemux("ten_byte_file");
137 } 150 }
138 151
139 TEST_F(AudioFileReaderTest, InfiniteDuration) { 152 TEST_F(AudioFileReaderTest, UnknownDuration) {
140 RunTestFailingDemux("bear-320x240-live.webm"); 153 RunTest("bear-320x240-live.webm", "-3.59,-2.06,-0.43,2.15,0.77,-0.95,", 2,
154 44100, base::TimeDelta::FromMicroseconds(-1), -1, 121024);
141 } 155 }
142 156
143 TEST_F(AudioFileReaderTest, WithVideo) { 157 TEST_F(AudioFileReaderTest, WithVideo) {
144 RunTest("bear.ogv", 158 RunTest("bear.ogv", "-0.73,0.92,0.48,-0.07,-0.92,-0.88,", 2, 44100,
145 "-2.49,-0.75,0.38,1.60,0.70,-1.22,", 159 base::TimeDelta::FromMicroseconds(1011520), 44609, 45632);
146 2,
147 44100,
148 base::TimeDelta::FromMicroseconds(1011520),
149 44609,
150 44609);
151 } 160 }
152 161
153 TEST_F(AudioFileReaderTest, Vorbis) { 162 TEST_F(AudioFileReaderTest, Vorbis) {
154 RunTest("sfx.ogg", 163 RunTest("sfx.ogg", "2.17,3.31,5.15,6.33,5.97,4.35,", 1, 44100,
155 "4.36,4.81,4.84,4.45,4.61,4.63,", 164 base::TimeDelta::FromMicroseconds(350001), 15436, 15936);
156 1,
157 44100,
158 base::TimeDelta::FromMicroseconds(350001),
159 15436,
160 15436);
161 } 165 }
162 166
163 TEST_F(AudioFileReaderTest, WaveU8) { 167 TEST_F(AudioFileReaderTest, WaveU8) {
164 RunTest("sfx_u8.wav", 168 RunTest("sfx_u8.wav",
165 "-1.23,-1.57,-1.14,-0.91,-0.87,-0.07,", 169 "-1.23,-1.57,-1.14,-0.91,-0.87,-0.07,",
166 1, 170 1,
167 44100, 171 44100,
168 base::TimeDelta::FromMicroseconds(288414), 172 base::TimeDelta::FromMicroseconds(288414),
169 12720, 173 12720,
170 12719); 174 12719);
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 RunTest("4ch.wav", 246 RunTest("4ch.wav",
243 "131.71,38.02,130.31,44.89,135.98,42.52,", 247 "131.71,38.02,130.31,44.89,135.98,42.52,",
244 4, 248 4,
245 44100, 249 44100,
246 base::TimeDelta::FromMicroseconds(100001), 250 base::TimeDelta::FromMicroseconds(100001),
247 4411, 251 4411,
248 4410); 252 4410);
249 } 253 }
250 254
251 } // namespace media 255 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698