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

Side by Side Diff: media/base/audio_splicer_unittest.cc

Issue 156783003: Enhance AudioSplicer to crossfade marked splice frames. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments. Created 6 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « media/base/audio_splicer.cc ('k') | media/base/audio_timestamp_helper.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "media/base/audio_buffer.h" 6 #include "media/base/audio_buffer.h"
7 #include "media/base/audio_bus.h" 7 #include "media/base/audio_bus.h"
8 #include "media/base/audio_splicer.h" 8 #include "media/base/audio_splicer.h"
9 #include "media/base/audio_timestamp_helper.h" 9 #include "media/base/audio_timestamp_helper.h"
10 #include "media/base/buffers.h" 10 #include "media/base/buffers.h"
11 #include "media/base/test_helpers.h" 11 #include "media/base/test_helpers.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 namespace media { 14 namespace media {
15 15
16 // Do not change this format. AddInput() and GetValue() only work with float.
16 static const SampleFormat kSampleFormat = kSampleFormatF32; 17 static const SampleFormat kSampleFormat = kSampleFormatF32;
18 COMPILE_ASSERT(kSampleFormat == kSampleFormatF32, invalid_splice_format);
19
17 static const int kChannels = 1; 20 static const int kChannels = 1;
18 static const int kDefaultSampleRate = 44100; 21 static const int kDefaultSampleRate = 44100;
19 static const int kDefaultBufferSize = 100; 22 static const int kDefaultBufferSize = 100;
20 23
21 class AudioSplicerTest : public ::testing::Test { 24 class AudioSplicerTest : public ::testing::Test {
22 public: 25 public:
23 AudioSplicerTest() 26 AudioSplicerTest()
24 : splicer_(kDefaultSampleRate), 27 : splicer_(kDefaultSampleRate),
25 input_timestamp_helper_(kDefaultSampleRate) { 28 input_timestamp_helper_(kDefaultSampleRate) {
26 input_timestamp_helper_.SetBaseTimestamp(base::TimeDelta()); 29 input_timestamp_helper_.SetBaseTimestamp(base::TimeDelta());
27 } 30 }
28 31
29 scoped_refptr<AudioBuffer> GetNextInputBuffer(float value) { 32 scoped_refptr<AudioBuffer> GetNextInputBuffer(float value) {
30 return GetNextInputBuffer(value, kDefaultBufferSize); 33 return GetNextInputBuffer(value, kDefaultBufferSize);
31 } 34 }
32 35
33 scoped_refptr<AudioBuffer> GetNextInputBuffer(float value, int frame_size) { 36 scoped_refptr<AudioBuffer> GetNextInputBuffer(float value, int frame_size) {
34 scoped_refptr<AudioBuffer> buffer = MakeInterleavedAudioBuffer<float>( 37 scoped_refptr<AudioBuffer> buffer = MakeInterleavedAudioBuffer<float>(
35 kSampleFormat, 38 kSampleFormat,
36 kChannels, 39 kChannels,
37 value, 40 value,
38 0.0f, 41 0.0f,
39 frame_size, 42 frame_size,
40 input_timestamp_helper_.GetTimestamp(), 43 input_timestamp_helper_.GetTimestamp(),
41 input_timestamp_helper_.GetFrameDuration(frame_size)); 44 input_timestamp_helper_.GetFrameDuration(frame_size));
42 input_timestamp_helper_.AddFrames(frame_size); 45 input_timestamp_helper_.AddFrames(frame_size);
43 return buffer; 46 return buffer;
44 } 47 }
45 48
46 bool VerifyData(scoped_refptr<AudioBuffer> buffer, float value) { 49 float GetValue(const scoped_refptr<AudioBuffer>& buffer) {
50 return reinterpret_cast<const float*>(buffer->channel_data()[0])[0];
51 }
52
53 bool VerifyData(const scoped_refptr<AudioBuffer>& buffer, float value) {
47 int frames = buffer->frame_count(); 54 int frames = buffer->frame_count();
48 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, frames); 55 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, frames);
49 buffer->ReadFrames(frames, 0, 0, bus.get()); 56 buffer->ReadFrames(frames, 0, 0, bus.get());
50 for (int i = 0; i < frames; ++i) { 57 for (int ch = 0; ch < buffer->channel_count(); ++ch) {
51 if (bus->channel(0)[i] != value) 58 for (int i = 0; i < frames; ++i) {
52 return false; 59 if (bus->channel(ch)[i] != value)
60 return false;
61 }
53 } 62 }
54 return true; 63 return true;
55 } 64 }
56 65
66 void VerifyNextBuffer(const scoped_refptr<AudioBuffer>& input) {
67 ASSERT_TRUE(splicer_.HasNextBuffer());
68 scoped_refptr<AudioBuffer> output = splicer_.GetNextBuffer();
69 EXPECT_EQ(input->timestamp(), output->timestamp());
70 EXPECT_EQ(input->duration(), output->duration());
71 EXPECT_EQ(input->frame_count(), output->frame_count());
72 EXPECT_TRUE(VerifyData(output, GetValue(input)));
73 }
74
75 void VerifyPreSpliceOutput(
76 const scoped_refptr<AudioBuffer>& overlapped_buffer,
77 const scoped_refptr<AudioBuffer>& overlapping_buffer,
78 int expected_pre_splice_size,
79 base::TimeDelta expected_pre_splice_duration) {
80 ASSERT_TRUE(splicer_.HasNextBuffer());
81 scoped_refptr<AudioBuffer> pre_splice_output = splicer_.GetNextBuffer();
82 EXPECT_EQ(overlapped_buffer->timestamp(), pre_splice_output->timestamp());
83 EXPECT_EQ(expected_pre_splice_size, pre_splice_output->frame_count());
84 EXPECT_EQ(expected_pre_splice_duration, pre_splice_output->duration());
85 EXPECT_TRUE(VerifyData(pre_splice_output, GetValue(overlapped_buffer)));
86 }
87
88 void VerifyCrossfadeOutput(
89 const scoped_refptr<AudioBuffer>& overlapped_buffer,
90 const scoped_refptr<AudioBuffer>& overlapping_buffer,
91 int expected_crossfade_size,
92 base::TimeDelta expected_crossfade_duration) {
93 ASSERT_TRUE(splicer_.HasNextBuffer());
94
95 scoped_refptr<AudioBuffer> crossfade_output = splicer_.GetNextBuffer();
96 EXPECT_EQ(expected_crossfade_size, crossfade_output->frame_count());
97 EXPECT_EQ(expected_crossfade_duration, crossfade_output->duration());
98
99 // The splice timestamp may be adjusted by a microsecond.
100 EXPECT_NEAR(overlapping_buffer->timestamp().InMicroseconds(),
101 crossfade_output->timestamp().InMicroseconds(),
102 1);
103
104 // Verify the actual crossfade.
105 const int frames = crossfade_output->frame_count();
106 const float overlapped_value = GetValue(overlapped_buffer);
107 const float overlapping_value = GetValue(overlapping_buffer);
108 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, frames);
109 crossfade_output->ReadFrames(frames, 0, 0, bus.get());
110 for (int ch = 0; ch < crossfade_output->channel_count(); ++ch) {
111 float cf_ratio = 0;
112 const float cf_increment = 1.0f / frames;
113 for (int i = 0; i < frames; ++i, cf_ratio += cf_increment) {
114 const float actual = bus->channel(ch)[i];
115 const float expected =
116 (1.0f - cf_ratio) * overlapped_value + cf_ratio * overlapping_value;
117 ASSERT_FLOAT_EQ(expected, actual) << "i=" << i;
118 }
119 }
120 }
121
122 bool AddInput(const scoped_refptr<AudioBuffer>& input) {
123 // Since the splicer doesn't make copies it's working directly on the input
124 // buffers. We must make a copy before adding to ensure the original buffer
125 // is not modified in unexpected ways.
126 scoped_refptr<AudioBuffer> buffer_copy =
127 input->end_of_stream()
128 ? AudioBuffer::CreateEOSBuffer()
129 : AudioBuffer::CopyFrom(kSampleFormat,
130 input->channel_count(),
131 input->frame_count(),
132 &input->channel_data()[0],
133 input->timestamp(),
134 input->duration());
135 return splicer_.AddInput(buffer_copy);
136 }
137
138 base::TimeDelta max_crossfade_duration() {
139 return splicer_.max_crossfade_duration_;
140 }
141
57 protected: 142 protected:
58 AudioSplicer splicer_; 143 AudioSplicer splicer_;
59 AudioTimestampHelper input_timestamp_helper_; 144 AudioTimestampHelper input_timestamp_helper_;
60 145
61 DISALLOW_COPY_AND_ASSIGN(AudioSplicerTest); 146 DISALLOW_COPY_AND_ASSIGN(AudioSplicerTest);
62 }; 147 };
63 148
64 TEST_F(AudioSplicerTest, PassThru) { 149 TEST_F(AudioSplicerTest, PassThru) {
65 EXPECT_FALSE(splicer_.HasNextBuffer()); 150 EXPECT_FALSE(splicer_.HasNextBuffer());
66 151
67 // Test single buffer pass-thru behavior. 152 // Test single buffer pass-thru behavior.
68 scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f); 153 scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f);
69 EXPECT_TRUE(splicer_.AddInput(input_1)); 154 EXPECT_TRUE(AddInput(input_1));
70 EXPECT_TRUE(splicer_.HasNextBuffer()); 155 VerifyNextBuffer(input_1);
71
72 scoped_refptr<AudioBuffer> output_1 = splicer_.GetNextBuffer();
73 EXPECT_FALSE(splicer_.HasNextBuffer()); 156 EXPECT_FALSE(splicer_.HasNextBuffer());
74 EXPECT_EQ(input_1->timestamp(), output_1->timestamp());
75 EXPECT_EQ(input_1->duration(), output_1->duration());
76 EXPECT_EQ(input_1->frame_count(), output_1->frame_count());
77 EXPECT_TRUE(VerifyData(output_1, 0.1f));
78 157
79 // Test that multiple buffers can be queued in the splicer. 158 // Test that multiple buffers can be queued in the splicer.
80 scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f); 159 scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f);
81 scoped_refptr<AudioBuffer> input_3 = GetNextInputBuffer(0.3f); 160 scoped_refptr<AudioBuffer> input_3 = GetNextInputBuffer(0.3f);
82 EXPECT_TRUE(splicer_.AddInput(input_2)); 161 EXPECT_TRUE(AddInput(input_2));
83 EXPECT_TRUE(splicer_.AddInput(input_3)); 162 EXPECT_TRUE(AddInput(input_3));
84 EXPECT_TRUE(splicer_.HasNextBuffer()); 163 VerifyNextBuffer(input_2);
85 164 VerifyNextBuffer(input_3);
86 scoped_refptr<AudioBuffer> output_2 = splicer_.GetNextBuffer();
87 EXPECT_TRUE(splicer_.HasNextBuffer());
88 EXPECT_EQ(input_2->timestamp(), output_2->timestamp());
89 EXPECT_EQ(input_2->duration(), output_2->duration());
90 EXPECT_EQ(input_2->frame_count(), output_2->frame_count());
91
92 scoped_refptr<AudioBuffer> output_3 = splicer_.GetNextBuffer();
93 EXPECT_FALSE(splicer_.HasNextBuffer()); 165 EXPECT_FALSE(splicer_.HasNextBuffer());
94 EXPECT_EQ(input_3->timestamp(), output_3->timestamp());
95 EXPECT_EQ(input_3->duration(), output_3->duration());
96 EXPECT_EQ(input_3->frame_count(), output_3->frame_count());
97 } 166 }
98 167
99 TEST_F(AudioSplicerTest, Reset) { 168 TEST_F(AudioSplicerTest, Reset) {
100 scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f); 169 scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f);
101 EXPECT_TRUE(splicer_.AddInput(input_1)); 170 EXPECT_TRUE(AddInput(input_1));
102 EXPECT_TRUE(splicer_.HasNextBuffer()); 171 ASSERT_TRUE(splicer_.HasNextBuffer());
103 172
104 splicer_.Reset(); 173 splicer_.Reset();
105 EXPECT_FALSE(splicer_.HasNextBuffer()); 174 EXPECT_FALSE(splicer_.HasNextBuffer());
106 175
107 // Add some bytes to the timestamp helper so that the 176 // Add some bytes to the timestamp helper so that the
108 // next buffer starts many frames beyond the end of 177 // next buffer starts many frames beyond the end of
109 // |input_1|. This is to make sure that Reset() actually 178 // |input_1|. This is to make sure that Reset() actually
110 // clears its state and doesn't try to insert a gap. 179 // clears its state and doesn't try to insert a gap.
111 input_timestamp_helper_.AddFrames(100); 180 input_timestamp_helper_.AddFrames(100);
112 181
113 // Verify that a new input buffer passes through as expected. 182 // Verify that a new input buffer passes through as expected.
114 scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f); 183 scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f);
115 EXPECT_TRUE(splicer_.AddInput(input_2)); 184 EXPECT_TRUE(AddInput(input_2));
116 EXPECT_TRUE(splicer_.HasNextBuffer()); 185 ASSERT_TRUE(splicer_.HasNextBuffer());
117 186 VerifyNextBuffer(input_2);
118 scoped_refptr<AudioBuffer> output_2 = splicer_.GetNextBuffer();
119 EXPECT_FALSE(splicer_.HasNextBuffer()); 187 EXPECT_FALSE(splicer_.HasNextBuffer());
120 EXPECT_EQ(input_2->timestamp(), output_2->timestamp());
121 EXPECT_EQ(input_2->duration(), output_2->duration());
122 EXPECT_EQ(input_2->frame_count(), output_2->frame_count());
123 } 188 }
124 189
125 TEST_F(AudioSplicerTest, EndOfStream) { 190 TEST_F(AudioSplicerTest, EndOfStream) {
126 scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f); 191 scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f);
127 scoped_refptr<AudioBuffer> input_2 = AudioBuffer::CreateEOSBuffer(); 192 scoped_refptr<AudioBuffer> input_2 = AudioBuffer::CreateEOSBuffer();
128 scoped_refptr<AudioBuffer> input_3 = GetNextInputBuffer(0.2f); 193 scoped_refptr<AudioBuffer> input_3 = GetNextInputBuffer(0.2f);
129 EXPECT_TRUE(input_2->end_of_stream()); 194 EXPECT_TRUE(input_2->end_of_stream());
130 195
131 EXPECT_TRUE(splicer_.AddInput(input_1)); 196 EXPECT_TRUE(AddInput(input_1));
132 EXPECT_TRUE(splicer_.AddInput(input_2)); 197 EXPECT_TRUE(AddInput(input_2));
133 EXPECT_TRUE(splicer_.HasNextBuffer());
134 198
135 scoped_refptr<AudioBuffer> output_1 = splicer_.GetNextBuffer(); 199 VerifyNextBuffer(input_1);
200
136 scoped_refptr<AudioBuffer> output_2 = splicer_.GetNextBuffer(); 201 scoped_refptr<AudioBuffer> output_2 = splicer_.GetNextBuffer();
137 EXPECT_FALSE(splicer_.HasNextBuffer()); 202 EXPECT_FALSE(splicer_.HasNextBuffer());
138 EXPECT_EQ(input_1->timestamp(), output_1->timestamp());
139 EXPECT_EQ(input_1->duration(), output_1->duration());
140 EXPECT_EQ(input_1->frame_count(), output_1->frame_count());
141
142 EXPECT_TRUE(output_2->end_of_stream()); 203 EXPECT_TRUE(output_2->end_of_stream());
143 204
144 // Verify that buffers can be added again after Reset(). 205 // Verify that buffers can be added again after Reset().
145 splicer_.Reset(); 206 splicer_.Reset();
146 EXPECT_TRUE(splicer_.AddInput(input_3)); 207 EXPECT_TRUE(AddInput(input_3));
147 scoped_refptr<AudioBuffer> output_3 = splicer_.GetNextBuffer(); 208 VerifyNextBuffer(input_3);
148 EXPECT_FALSE(splicer_.HasNextBuffer()); 209 EXPECT_FALSE(splicer_.HasNextBuffer());
149 EXPECT_EQ(input_3->timestamp(), output_3->timestamp());
150 EXPECT_EQ(input_3->duration(), output_3->duration());
151 EXPECT_EQ(input_3->frame_count(), output_3->frame_count());
152 } 210 }
153 211
154
155 // Test the gap insertion code. 212 // Test the gap insertion code.
156 // +--------------+ +--------------+ 213 // +--------------+ +--------------+
157 // |11111111111111| |22222222222222| 214 // |11111111111111| |22222222222222|
158 // +--------------+ +--------------+ 215 // +--------------+ +--------------+
159 // Results in: 216 // Results in:
160 // +--------------+----+--------------+ 217 // +--------------+----+--------------+
161 // |11111111111111|0000|22222222222222| 218 // |11111111111111|0000|22222222222222|
162 // +--------------+----+--------------+ 219 // +--------------+----+--------------+
163 TEST_F(AudioSplicerTest, GapInsertion) { 220 TEST_F(AudioSplicerTest, GapInsertion) {
164 scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f); 221 scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f);
165 222
166 // Add bytes to the timestamp helper so that the next buffer 223 // Add bytes to the timestamp helper so that the next buffer
167 // will have a starting timestamp that indicates a gap is 224 // will have a starting timestamp that indicates a gap is
168 // present. 225 // present.
169 const int kGapSize = 7; 226 const int kGapSize = 7;
170 input_timestamp_helper_.AddFrames(kGapSize); 227 input_timestamp_helper_.AddFrames(kGapSize);
171 scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f); 228 scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f);
172 229
173 EXPECT_TRUE(splicer_.AddInput(input_1)); 230 EXPECT_TRUE(AddInput(input_1));
174 EXPECT_TRUE(splicer_.AddInput(input_2)); 231 EXPECT_TRUE(AddInput(input_2));
175
176 // Verify that a gap buffer is generated.
177 EXPECT_TRUE(splicer_.HasNextBuffer());
178 scoped_refptr<AudioBuffer> output_1 = splicer_.GetNextBuffer();
179 scoped_refptr<AudioBuffer> output_2 = splicer_.GetNextBuffer();
180 scoped_refptr<AudioBuffer> output_3 = splicer_.GetNextBuffer();
181 EXPECT_FALSE(splicer_.HasNextBuffer());
182 232
183 // Verify that the first input buffer passed through unmodified. 233 // Verify that the first input buffer passed through unmodified.
184 EXPECT_EQ(input_1->timestamp(), output_1->timestamp()); 234 VerifyNextBuffer(input_1);
185 EXPECT_EQ(input_1->duration(), output_1->duration());
186 EXPECT_EQ(input_1->frame_count(), output_1->frame_count());
187 EXPECT_TRUE(VerifyData(output_1, 0.1f));
188 235
189 // Verify the contents of the gap buffer. 236 // Verify the contents of the gap buffer.
237 scoped_refptr<AudioBuffer> output_2 = splicer_.GetNextBuffer();
190 base::TimeDelta gap_timestamp = 238 base::TimeDelta gap_timestamp =
191 input_1->timestamp() + input_1->duration(); 239 input_1->timestamp() + input_1->duration();
192 base::TimeDelta gap_duration = input_2->timestamp() - gap_timestamp; 240 base::TimeDelta gap_duration = input_2->timestamp() - gap_timestamp;
193 EXPECT_GT(gap_duration, base::TimeDelta()); 241 EXPECT_GT(gap_duration, base::TimeDelta());
194 EXPECT_EQ(gap_timestamp, output_2->timestamp()); 242 EXPECT_EQ(gap_timestamp, output_2->timestamp());
195 EXPECT_EQ(gap_duration, output_2->duration()); 243 EXPECT_EQ(gap_duration, output_2->duration());
196 EXPECT_EQ(kGapSize, output_2->frame_count()); 244 EXPECT_EQ(kGapSize, output_2->frame_count());
197 EXPECT_TRUE(VerifyData(output_2, 0.0f)); 245 EXPECT_TRUE(VerifyData(output_2, 0.0f));
198 246
199 // Verify that the second input buffer passed through unmodified. 247 // Verify that the second input buffer passed through unmodified.
200 EXPECT_EQ(input_2->timestamp(), output_3->timestamp()); 248 VerifyNextBuffer(input_2);
201 EXPECT_EQ(input_2->duration(), output_3->duration()); 249 EXPECT_FALSE(splicer_.HasNextBuffer());
202 EXPECT_EQ(input_2->frame_count(), output_3->frame_count());
203 EXPECT_TRUE(VerifyData(output_3, 0.2f));
204 } 250 }
205 251
206
207 // Test that an error is signalled when the gap between input buffers is 252 // Test that an error is signalled when the gap between input buffers is
208 // too large. 253 // too large.
209 TEST_F(AudioSplicerTest, GapTooLarge) { 254 TEST_F(AudioSplicerTest, GapTooLarge) {
210 scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f); 255 scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f);
211 256
212 // Add a seconds worth of bytes so that an unacceptably large 257 // Add a seconds worth of bytes so that an unacceptably large
213 // gap exists between |input_1| and |input_2|. 258 // gap exists between |input_1| and |input_2|.
214 const int kGapSize = kDefaultSampleRate; 259 const int kGapSize = kDefaultSampleRate;
215 input_timestamp_helper_.AddFrames(kGapSize); 260 input_timestamp_helper_.AddFrames(kGapSize);
216 scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f); 261 scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f);
217 262
218 EXPECT_TRUE(splicer_.AddInput(input_1)); 263 EXPECT_TRUE(AddInput(input_1));
219 EXPECT_FALSE(splicer_.AddInput(input_2)); 264 EXPECT_FALSE(AddInput(input_2));
220 265
221 EXPECT_TRUE(splicer_.HasNextBuffer()); 266 VerifyNextBuffer(input_1);
222 scoped_refptr<AudioBuffer> output_1 = splicer_.GetNextBuffer();
223
224 // Verify that the first input buffer passed through unmodified.
225 EXPECT_EQ(input_1->timestamp(), output_1->timestamp());
226 EXPECT_EQ(input_1->duration(), output_1->duration());
227 EXPECT_EQ(input_1->frame_count(), output_1->frame_count());
228 EXPECT_TRUE(VerifyData(output_1, 0.1f));
229 267
230 // Verify that the second buffer is not available. 268 // Verify that the second buffer is not available.
231 EXPECT_FALSE(splicer_.HasNextBuffer()); 269 EXPECT_FALSE(splicer_.HasNextBuffer());
232 270
233 // Reset the timestamp helper so it can generate a buffer that is 271 // Reset the timestamp helper so it can generate a buffer that is
234 // right after |input_1|. 272 // right after |input_1|.
235 input_timestamp_helper_.SetBaseTimestamp( 273 input_timestamp_helper_.SetBaseTimestamp(
236 input_1->timestamp() + input_1->duration()); 274 input_1->timestamp() + input_1->duration());
237 275
238 // Verify that valid buffers are still accepted. 276 // Verify that valid buffers are still accepted.
239 scoped_refptr<AudioBuffer> input_3 = GetNextInputBuffer(0.3f); 277 scoped_refptr<AudioBuffer> input_3 = GetNextInputBuffer(0.3f);
240 EXPECT_TRUE(splicer_.AddInput(input_3)); 278 EXPECT_TRUE(AddInput(input_3));
241 EXPECT_TRUE(splicer_.HasNextBuffer()); 279 VerifyNextBuffer(input_3);
242 scoped_refptr<AudioBuffer> output_2 = splicer_.GetNextBuffer();
243 EXPECT_FALSE(splicer_.HasNextBuffer()); 280 EXPECT_FALSE(splicer_.HasNextBuffer());
244 EXPECT_EQ(input_3->timestamp(), output_2->timestamp());
245 EXPECT_EQ(input_3->duration(), output_2->duration());
246 EXPECT_EQ(input_3->frame_count(), output_2->frame_count());
247 EXPECT_TRUE(VerifyData(output_2, 0.3f));
248 } 281 }
249 282
250
251 // Verifies that an error is signalled if AddInput() is called 283 // Verifies that an error is signalled if AddInput() is called
252 // with a timestamp that is earlier than the first buffer added. 284 // with a timestamp that is earlier than the first buffer added.
253 TEST_F(AudioSplicerTest, BufferAddedBeforeBase) { 285 TEST_F(AudioSplicerTest, BufferAddedBeforeBase) {
254 input_timestamp_helper_.SetBaseTimestamp( 286 input_timestamp_helper_.SetBaseTimestamp(
255 base::TimeDelta::FromMicroseconds(10)); 287 base::TimeDelta::FromMicroseconds(10));
256 scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f); 288 scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f);
257 289
258 // Reset the timestamp helper so the next buffer will have a timestamp earlier 290 // Reset the timestamp helper so the next buffer will have a timestamp earlier
259 // than |input_1|. 291 // than |input_1|.
260 input_timestamp_helper_.SetBaseTimestamp(base::TimeDelta::FromSeconds(0)); 292 input_timestamp_helper_.SetBaseTimestamp(base::TimeDelta::FromSeconds(0));
261 scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.1f); 293 scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.1f);
262 294
263 EXPECT_GT(input_1->timestamp(), input_2->timestamp()); 295 EXPECT_GT(input_1->timestamp(), input_2->timestamp());
264 EXPECT_TRUE(splicer_.AddInput(input_1)); 296 EXPECT_TRUE(AddInput(input_1));
265 EXPECT_FALSE(splicer_.AddInput(input_2)); 297 EXPECT_FALSE(AddInput(input_2));
266 } 298 }
267 299
268
269 // Test when one buffer partially overlaps another. 300 // Test when one buffer partially overlaps another.
270 // +--------------+ 301 // +--------------+
271 // |11111111111111| 302 // |11111111111111|
272 // +--------------+ 303 // +--------------+
273 // +--------------+ 304 // +--------------+
274 // |22222222222222| 305 // |22222222222222|
275 // +--------------+ 306 // +--------------+
276 // Results in: 307 // Results in:
277 // +--------------+----------+ 308 // +--------------+----------+
278 // |11111111111111|2222222222| 309 // |11111111111111|2222222222|
279 // +--------------+----------+ 310 // +--------------+----------+
280 TEST_F(AudioSplicerTest, PartialOverlap) { 311 TEST_F(AudioSplicerTest, PartialOverlap) {
281 scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f); 312 scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f);
282 313
283 // Reset timestamp helper so that the next buffer will have a 314 // Reset timestamp helper so that the next buffer will have a
284 // timestamp that starts in the middle of |input_1|. 315 // timestamp that starts in the middle of |input_1|.
285 const int kOverlapSize = input_1->frame_count() / 4; 316 const int kOverlapSize = input_1->frame_count() / 4;
286 input_timestamp_helper_.SetBaseTimestamp(input_1->timestamp()); 317 input_timestamp_helper_.SetBaseTimestamp(input_1->timestamp());
287 input_timestamp_helper_.AddFrames(input_1->frame_count() - kOverlapSize); 318 input_timestamp_helper_.AddFrames(input_1->frame_count() - kOverlapSize);
288 319
289 scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f); 320 scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f);
290 321
291 EXPECT_TRUE(splicer_.AddInput(input_1)); 322 EXPECT_TRUE(AddInput(input_1));
292 EXPECT_TRUE(splicer_.AddInput(input_2)); 323 EXPECT_TRUE(AddInput(input_2));
293 324
294 EXPECT_TRUE(splicer_.HasNextBuffer()); 325 // Verify that the first input buffer passed through unmodified.
295 scoped_refptr<AudioBuffer> output_1 = splicer_.GetNextBuffer(); 326 VerifyNextBuffer(input_1);
327 ASSERT_TRUE(splicer_.HasNextBuffer());
328
296 scoped_refptr<AudioBuffer> output_2 = splicer_.GetNextBuffer(); 329 scoped_refptr<AudioBuffer> output_2 = splicer_.GetNextBuffer();
297 EXPECT_FALSE(splicer_.HasNextBuffer()); 330 EXPECT_FALSE(splicer_.HasNextBuffer());
298 331
299 // Verify that the first input buffer passed through unmodified.
300 EXPECT_EQ(input_1->timestamp(), output_1->timestamp());
301 EXPECT_EQ(input_1->duration(), output_1->duration());
302 EXPECT_EQ(input_1->frame_count(), output_1->frame_count());
303 EXPECT_TRUE(VerifyData(output_1, 0.1f));
304
305 // Verify that the second input buffer was truncated to only contain 332 // Verify that the second input buffer was truncated to only contain
306 // the samples that are after the end of |input_1|. Note that data is not 333 // the samples that are after the end of |input_1|.
307 // copied, so |input_2|'s values are modified.
308 base::TimeDelta expected_timestamp = 334 base::TimeDelta expected_timestamp =
309 input_1->timestamp() + input_1->duration(); 335 input_1->timestamp() + input_1->duration();
310 base::TimeDelta expected_duration = 336 base::TimeDelta expected_duration =
311 (input_2->timestamp() + input_2->duration()) - expected_timestamp; 337 (input_2->timestamp() + input_2->duration()) - expected_timestamp;
312 EXPECT_EQ(expected_timestamp, output_2->timestamp()); 338 EXPECT_EQ(expected_timestamp, output_2->timestamp());
313 EXPECT_EQ(expected_duration, output_2->duration()); 339 EXPECT_EQ(expected_duration, output_2->duration());
314 EXPECT_TRUE(VerifyData(output_2, 0.2f)); 340 EXPECT_TRUE(VerifyData(output_2, GetValue(input_2)));
315 } 341 }
316 342
317
318 // Test that an input buffer that is completely overlapped by a buffer 343 // Test that an input buffer that is completely overlapped by a buffer
319 // that was already added is dropped. 344 // that was already added is dropped.
320 // +--------------+ 345 // +--------------+
321 // |11111111111111| 346 // |11111111111111|
322 // +--------------+ 347 // +--------------+
323 // +-----+ 348 // +-----+
324 // |22222| 349 // |22222|
325 // +-----+ 350 // +-----+
326 // +-------------+ 351 // +-------------+
327 // |3333333333333| 352 // |3333333333333|
(...skipping 13 matching lines...) Expand all
341 input_timestamp_helper_.AddFrames(kOverlapOffset); 366 input_timestamp_helper_.AddFrames(kOverlapOffset);
342 367
343 scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f, kOverlapSize); 368 scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f, kOverlapSize);
344 369
345 // Reset the timestamp helper so the next buffer will be right after 370 // Reset the timestamp helper so the next buffer will be right after
346 // |input_1|. 371 // |input_1|.
347 input_timestamp_helper_.SetBaseTimestamp(input_1->timestamp()); 372 input_timestamp_helper_.SetBaseTimestamp(input_1->timestamp());
348 input_timestamp_helper_.AddFrames(input_1->frame_count()); 373 input_timestamp_helper_.AddFrames(input_1->frame_count());
349 scoped_refptr<AudioBuffer> input_3 = GetNextInputBuffer(0.3f); 374 scoped_refptr<AudioBuffer> input_3 = GetNextInputBuffer(0.3f);
350 375
351 EXPECT_TRUE(splicer_.AddInput(input_1)); 376 EXPECT_TRUE(AddInput(input_1));
352 EXPECT_TRUE(splicer_.AddInput(input_2)); 377 EXPECT_TRUE(AddInput(input_2));
353 EXPECT_TRUE(splicer_.AddInput(input_3)); 378 EXPECT_TRUE(AddInput(input_3));
354 379
355 EXPECT_TRUE(splicer_.HasNextBuffer()); 380 VerifyNextBuffer(input_1);
356 scoped_refptr<AudioBuffer> output_1 = splicer_.GetNextBuffer(); 381 VerifyNextBuffer(input_3);
357 scoped_refptr<AudioBuffer> output_2 = splicer_.GetNextBuffer(); 382 EXPECT_FALSE(splicer_.HasNextBuffer());
358 EXPECT_FALSE(splicer_.HasNextBuffer()); 383 }
359 384
360 // Verify that the first input buffer passed through unmodified. 385 // Test crossfade when one buffer partially overlaps another.
361 EXPECT_EQ(input_1->timestamp(), output_1->timestamp()); 386 // +--------------+
362 EXPECT_EQ(input_1->duration(), output_1->duration()); 387 // |11111111111111|
363 EXPECT_EQ(input_1->frame_count(), output_1->frame_count()); 388 // +--------------+
364 EXPECT_TRUE(VerifyData(output_1, 0.1f)); 389 // +--------------+
365 390 // |22222222222222|
366 // Verify that the second output buffer only contains 391 // +--------------+
367 // the samples that are in |input_3|. 392 // Results in:
368 EXPECT_EQ(input_3->timestamp(), output_2->timestamp()); 393 // +----------+----+----------+
369 EXPECT_EQ(input_3->duration(), output_2->duration()); 394 // |1111111111|xxxx|2222222222|
370 EXPECT_EQ(input_3->frame_count(), output_2->frame_count()); 395 // +----------+----+----------+
371 EXPECT_TRUE(VerifyData(output_2, 0.3f)); 396 // Where "xxxx" represents the crossfaded portion of the signal.
397 TEST_F(AudioSplicerTest, PartialOverlapCrossfade) {
398 const int kCrossfadeSize =
399 input_timestamp_helper_.GetFramesToTarget(max_crossfade_duration());
400 const int kBufferSize = kCrossfadeSize * 2;
401
402 scoped_refptr<AudioBuffer> extra_pre_splice_buffer =
403 GetNextInputBuffer(0.2f, kBufferSize);
404 scoped_refptr<AudioBuffer> overlapped_buffer =
405 GetNextInputBuffer(1.0f, kBufferSize);
406
407 // Reset timestamp helper so that the next buffer will have a timestamp that
408 // starts in the middle of |overlapped_buffer|.
409 input_timestamp_helper_.SetBaseTimestamp(overlapped_buffer->timestamp());
410 input_timestamp_helper_.AddFrames(overlapped_buffer->frame_count() -
411 kCrossfadeSize);
412 splicer_.SetSpliceTimestamp(input_timestamp_helper_.GetTimestamp());
413 scoped_refptr<AudioBuffer> overlapping_buffer =
414 GetNextInputBuffer(0.0f, kBufferSize);
415
416 // |extra_pre_splice_buffer| is entirely before the splice and should be ready
417 // for output.
418 EXPECT_TRUE(AddInput(extra_pre_splice_buffer));
419 VerifyNextBuffer(extra_pre_splice_buffer);
420
421 // The splicer should be internally queuing input since |overlapped_buffer| is
422 // part of the splice.
423 EXPECT_TRUE(AddInput(overlapped_buffer));
424 EXPECT_FALSE(splicer_.HasNextBuffer());
425
426 // |overlapping_buffer| should complete the splice, so ensure output is now
427 // available.
428 EXPECT_TRUE(AddInput(overlapping_buffer));
429 ASSERT_TRUE(splicer_.HasNextBuffer());
430
431 // Add one more buffer to make sure it's passed through untouched.
432 scoped_refptr<AudioBuffer> extra_post_splice_buffer =
433 GetNextInputBuffer(0.5f, kBufferSize);
434 EXPECT_TRUE(AddInput(extra_post_splice_buffer));
435
436 VerifyPreSpliceOutput(overlapped_buffer,
437 overlapping_buffer,
438 221,
439 base::TimeDelta::FromMicroseconds(5012));
440
441 // Due to rounding the crossfade size may vary by up to a frame.
442 const int kExpectedCrossfadeSize = 220;
443 EXPECT_NEAR(kExpectedCrossfadeSize, kCrossfadeSize, 1);
444
445 VerifyCrossfadeOutput(overlapped_buffer,
446 overlapping_buffer,
447 kExpectedCrossfadeSize,
448 base::TimeDelta::FromMicroseconds(4988));
449
450 // Retrieve the remaining portion after crossfade.
451 ASSERT_TRUE(splicer_.HasNextBuffer());
452 scoped_refptr<AudioBuffer> post_splice_output = splicer_.GetNextBuffer();
453 EXPECT_EQ(base::TimeDelta::FromMicroseconds(20022),
454 post_splice_output->timestamp());
455 EXPECT_EQ(overlapping_buffer->frame_count() - kExpectedCrossfadeSize,
456 post_splice_output->frame_count());
457 EXPECT_EQ(base::TimeDelta::FromMicroseconds(5034),
458 post_splice_output->duration());
459
460 EXPECT_TRUE(VerifyData(post_splice_output, GetValue(overlapping_buffer)));
461
462 VerifyNextBuffer(extra_post_splice_buffer);
463 EXPECT_FALSE(splicer_.HasNextBuffer());
464 }
465
466 // Test crossfade when one buffer partially overlaps another, but an end of
467 // stream buffer is received before the crossfade duration is reached.
468 // +--------------+
469 // |11111111111111|
470 // +--------------+
471 // +---------++---+
472 // |222222222||EOS|
473 // +---------++---+
474 // Results in:
475 // +----------+----+----++---+
476 // |1111111111|xxxx|2222||EOS|
477 // +----------+----+----++---+
478 // Where "x" represents the crossfaded portion of the signal.
479 TEST_F(AudioSplicerTest, PartialOverlapCrossfadeEndOfStream) {
480 const int kCrossfadeSize =
481 input_timestamp_helper_.GetFramesToTarget(max_crossfade_duration());
482
483 scoped_refptr<AudioBuffer> overlapped_buffer =
484 GetNextInputBuffer(1.0f, kCrossfadeSize * 2);
485
486 // Reset timestamp helper so that the next buffer will have a timestamp that
487 // starts 3/4 of the way into |overlapped_buffer|.
488 input_timestamp_helper_.SetBaseTimestamp(overlapped_buffer->timestamp());
489 input_timestamp_helper_.AddFrames(3 * overlapped_buffer->frame_count() / 4);
490 splicer_.SetSpliceTimestamp(input_timestamp_helper_.GetTimestamp());
491 scoped_refptr<AudioBuffer> overlapping_buffer =
492 GetNextInputBuffer(0.0f, kCrossfadeSize / 3);
493
494 // The splicer should be internally queuing input since |overlapped_buffer| is
495 // part of the splice.
496 EXPECT_TRUE(AddInput(overlapped_buffer));
497 EXPECT_FALSE(splicer_.HasNextBuffer());
498
499 // |overlapping_buffer| should not have enough data to complete the splice, so
500 // ensure output is not available.
501 EXPECT_TRUE(AddInput(overlapping_buffer));
502 EXPECT_FALSE(splicer_.HasNextBuffer());
503
504 // Now add an EOS buffer which should complete the splice.
505 EXPECT_TRUE(AddInput(AudioBuffer::CreateEOSBuffer()));
506 ASSERT_TRUE(splicer_.HasNextBuffer());
507
508 VerifyPreSpliceOutput(overlapped_buffer,
509 overlapping_buffer,
510 331,
511 base::TimeDelta::FromMicroseconds(7505));
512 VerifyCrossfadeOutput(overlapped_buffer,
513 overlapping_buffer,
514 overlapping_buffer->frame_count(),
515 overlapping_buffer->duration());
516
517 // Ensure the last buffer is an EOS buffer.
518 ASSERT_TRUE(splicer_.HasNextBuffer());
519 scoped_refptr<AudioBuffer> post_splice_output = splicer_.GetNextBuffer();
520 EXPECT_TRUE(post_splice_output->end_of_stream());
521
522 EXPECT_FALSE(splicer_.HasNextBuffer());
523 }
524
525 // Test crossfade when one buffer partially overlaps another, but the amount of
526 // overlapped data is less than the crossfade duration.
527 // +------------+
528 // |111111111111|
529 // +------------+
530 // +--------------+
531 // |22222222222222|
532 // +--------------+
533 // Results in:
534 // +----------+-+------------+
535 // |1111111111|x|222222222222|
536 // +----------+-+------------+
537 // Where "x" represents the crossfaded portion of the signal.
538 TEST_F(AudioSplicerTest, PartialOverlapCrossfadeShortPreSplice) {
539 const int kCrossfadeSize =
540 input_timestamp_helper_.GetFramesToTarget(max_crossfade_duration());
541
542 scoped_refptr<AudioBuffer> overlapped_buffer =
543 GetNextInputBuffer(1.0f, kCrossfadeSize / 2);
544
545 // Reset timestamp helper so that the next buffer will have a timestamp that
546 // starts in the middle of |overlapped_buffer|.
547 input_timestamp_helper_.SetBaseTimestamp(overlapped_buffer->timestamp());
548 input_timestamp_helper_.AddFrames(overlapped_buffer->frame_count() / 2);
549 splicer_.SetSpliceTimestamp(input_timestamp_helper_.GetTimestamp());
550 scoped_refptr<AudioBuffer> overlapping_buffer =
551 GetNextInputBuffer(0.0f, kCrossfadeSize * 2);
552
553 // The splicer should be internally queuing input since |overlapped_buffer| is
554 // part of the splice.
555 EXPECT_TRUE(AddInput(overlapped_buffer));
556 EXPECT_FALSE(splicer_.HasNextBuffer());
557
558 // |overlapping_buffer| should complete the splice, so ensure output is now
559 // available.
560 EXPECT_TRUE(AddInput(overlapping_buffer));
561 ASSERT_TRUE(splicer_.HasNextBuffer());
562
563 const int kExpectedPreSpliceSize = 55;
564 const base::TimeDelta kExpectedPreSpliceDuration =
565 base::TimeDelta::FromMicroseconds(1247);
566 VerifyPreSpliceOutput(overlapped_buffer,
567 overlapping_buffer,
568 kExpectedPreSpliceSize,
569 kExpectedPreSpliceDuration);
570 VerifyCrossfadeOutput(overlapped_buffer,
571 overlapping_buffer,
572 kExpectedPreSpliceSize,
573 kExpectedPreSpliceDuration);
574
575 // Retrieve the remaining portion after crossfade.
576 ASSERT_TRUE(splicer_.HasNextBuffer());
577 scoped_refptr<AudioBuffer> post_splice_output = splicer_.GetNextBuffer();
578 EXPECT_EQ(overlapping_buffer->timestamp() + kExpectedPreSpliceDuration,
579 post_splice_output->timestamp());
580 EXPECT_EQ(overlapping_buffer->frame_count() - kExpectedPreSpliceSize,
581 post_splice_output->frame_count());
582 EXPECT_EQ(overlapping_buffer->duration() - kExpectedPreSpliceDuration,
583 post_splice_output->duration());
584
585 EXPECT_TRUE(VerifyData(post_splice_output, GetValue(overlapping_buffer)));
586
587 EXPECT_FALSE(splicer_.HasNextBuffer());
372 } 588 }
373 589
374 } // namespace media 590 } // namespace media
OLDNEW
« no previous file with comments | « media/base/audio_splicer.cc ('k') | media/base/audio_timestamp_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698