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

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

Powered by Google App Engine
This is Rietveld 408576698