| OLD | NEW |
| 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" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 scoped_refptr<AudioBuffer> GetNextInputBuffer(float value, int frame_size) { | 37 scoped_refptr<AudioBuffer> GetNextInputBuffer(float value, int frame_size) { |
| 38 scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<float>( | 38 scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<float>( |
| 39 kSampleFormat, | 39 kSampleFormat, |
| 40 kChannelLayout, | 40 kChannelLayout, |
| 41 kChannels, | 41 kChannels, |
| 42 kDefaultSampleRate, | 42 kDefaultSampleRate, |
| 43 value, | 43 value, |
| 44 0.0f, | 44 0.0f, |
| 45 frame_size, | 45 frame_size, |
| 46 input_timestamp_helper_.GetTimestamp(), | 46 input_timestamp_helper_.GetTimestamp()); |
| 47 input_timestamp_helper_.GetFrameDuration(frame_size)); | |
| 48 input_timestamp_helper_.AddFrames(frame_size); | 47 input_timestamp_helper_.AddFrames(frame_size); |
| 49 return buffer; | 48 return buffer; |
| 50 } | 49 } |
| 51 | 50 |
| 52 float GetValue(const scoped_refptr<AudioBuffer>& buffer) { | 51 float GetValue(const scoped_refptr<AudioBuffer>& buffer) { |
| 53 return reinterpret_cast<const float*>(buffer->channel_data()[0])[0]; | 52 return reinterpret_cast<const float*>(buffer->channel_data()[0])[0]; |
| 54 } | 53 } |
| 55 | 54 |
| 56 bool VerifyData(const scoped_refptr<AudioBuffer>& buffer, float value) { | 55 bool VerifyData(const scoped_refptr<AudioBuffer>& buffer, float value) { |
| 57 int frames = buffer->frame_count(); | 56 int frames = buffer->frame_count(); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 // is not modified in unexpected ways. | 131 // is not modified in unexpected ways. |
| 133 scoped_refptr<AudioBuffer> buffer_copy = | 132 scoped_refptr<AudioBuffer> buffer_copy = |
| 134 input->end_of_stream() | 133 input->end_of_stream() |
| 135 ? AudioBuffer::CreateEOSBuffer() | 134 ? AudioBuffer::CreateEOSBuffer() |
| 136 : AudioBuffer::CopyFrom(kSampleFormat, | 135 : AudioBuffer::CopyFrom(kSampleFormat, |
| 137 input->channel_layout(), | 136 input->channel_layout(), |
| 138 input->channel_count(), | 137 input->channel_count(), |
| 139 input->sample_rate(), | 138 input->sample_rate(), |
| 140 input->frame_count(), | 139 input->frame_count(), |
| 141 &input->channel_data()[0], | 140 &input->channel_data()[0], |
| 142 input->timestamp(), | 141 input->timestamp()); |
| 143 input->duration()); | |
| 144 return splicer_.AddInput(buffer_copy); | 142 return splicer_.AddInput(buffer_copy); |
| 145 } | 143 } |
| 146 | 144 |
| 147 base::TimeDelta max_crossfade_duration() { | 145 base::TimeDelta max_crossfade_duration() { |
| 148 return splicer_.max_crossfade_duration_; | 146 return splicer_.max_crossfade_duration_; |
| 149 } | 147 } |
| 150 | 148 |
| 151 protected: | 149 protected: |
| 152 AudioSplicer splicer_; | 150 AudioSplicer splicer_; |
| 153 AudioTimestampHelper input_timestamp_helper_; | 151 AudioTimestampHelper input_timestamp_helper_; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 // Verify that the first input buffer passed through unmodified. | 239 // Verify that the first input buffer passed through unmodified. |
| 242 VerifyNextBuffer(input_1); | 240 VerifyNextBuffer(input_1); |
| 243 | 241 |
| 244 // Verify the contents of the gap buffer. | 242 // Verify the contents of the gap buffer. |
| 245 scoped_refptr<AudioBuffer> output_2 = splicer_.GetNextBuffer(); | 243 scoped_refptr<AudioBuffer> output_2 = splicer_.GetNextBuffer(); |
| 246 base::TimeDelta gap_timestamp = | 244 base::TimeDelta gap_timestamp = |
| 247 input_1->timestamp() + input_1->duration(); | 245 input_1->timestamp() + input_1->duration(); |
| 248 base::TimeDelta gap_duration = input_2->timestamp() - gap_timestamp; | 246 base::TimeDelta gap_duration = input_2->timestamp() - gap_timestamp; |
| 249 EXPECT_GT(gap_duration, base::TimeDelta()); | 247 EXPECT_GT(gap_duration, base::TimeDelta()); |
| 250 EXPECT_EQ(gap_timestamp, output_2->timestamp()); | 248 EXPECT_EQ(gap_timestamp, output_2->timestamp()); |
| 251 EXPECT_EQ(gap_duration, output_2->duration()); | 249 EXPECT_NEAR( |
| 250 gap_duration.InMicroseconds(), output_2->duration().InMicroseconds(), 1); |
| 252 EXPECT_EQ(kGapSize, output_2->frame_count()); | 251 EXPECT_EQ(kGapSize, output_2->frame_count()); |
| 253 EXPECT_TRUE(VerifyData(output_2, 0.0f)); | 252 EXPECT_TRUE(VerifyData(output_2, 0.0f)); |
| 254 | 253 |
| 255 // Verify that the second input buffer passed through unmodified. | 254 // Verify that the second input buffer passed through unmodified. |
| 256 VerifyNextBuffer(input_2); | 255 VerifyNextBuffer(input_2); |
| 257 EXPECT_FALSE(splicer_.HasNextBuffer()); | 256 EXPECT_FALSE(splicer_.HasNextBuffer()); |
| 258 } | 257 } |
| 259 | 258 |
| 260 // Test that an error is signalled when the gap between input buffers is | 259 // Test that an error is signalled when the gap between input buffers is |
| 261 // too large. | 260 // too large. |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 ASSERT_TRUE(splicer_.HasNextBuffer()); | 436 ASSERT_TRUE(splicer_.HasNextBuffer()); |
| 438 | 437 |
| 439 // Add one more buffer to make sure it's passed through untouched. | 438 // Add one more buffer to make sure it's passed through untouched. |
| 440 scoped_refptr<AudioBuffer> extra_post_splice_buffer = | 439 scoped_refptr<AudioBuffer> extra_post_splice_buffer = |
| 441 GetNextInputBuffer(0.5f, kBufferSize); | 440 GetNextInputBuffer(0.5f, kBufferSize); |
| 442 EXPECT_TRUE(AddInput(extra_post_splice_buffer)); | 441 EXPECT_TRUE(AddInput(extra_post_splice_buffer)); |
| 443 | 442 |
| 444 VerifyPreSpliceOutput(overlapped_buffer, | 443 VerifyPreSpliceOutput(overlapped_buffer, |
| 445 overlapping_buffer, | 444 overlapping_buffer, |
| 446 221, | 445 221, |
| 447 base::TimeDelta::FromMicroseconds(5012)); | 446 base::TimeDelta::FromMicroseconds(5011)); |
| 448 | 447 |
| 449 // Due to rounding the crossfade size may vary by up to a frame. | 448 // Due to rounding the crossfade size may vary by up to a frame. |
| 450 const int kExpectedCrossfadeSize = 220; | 449 const int kExpectedCrossfadeSize = 220; |
| 451 EXPECT_NEAR(kExpectedCrossfadeSize, kCrossfadeSize, 1); | 450 EXPECT_NEAR(kExpectedCrossfadeSize, kCrossfadeSize, 1); |
| 452 | 451 |
| 453 VerifyCrossfadeOutput(overlapped_buffer, | 452 VerifyCrossfadeOutput(overlapped_buffer, |
| 454 NULL, | 453 NULL, |
| 455 overlapping_buffer, | 454 overlapping_buffer, |
| 456 0, | 455 0, |
| 457 kExpectedCrossfadeSize, | 456 kExpectedCrossfadeSize, |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 // |1111111111|22222222222222| | 611 // |1111111111|22222222222222| |
| 613 // +----------+--------------+ | 612 // +----------+--------------+ |
| 614 TEST_F(AudioSplicerTest, IncorrectlyMarkedSplice) { | 613 TEST_F(AudioSplicerTest, IncorrectlyMarkedSplice) { |
| 615 const int kBufferSize = | 614 const int kBufferSize = |
| 616 input_timestamp_helper_.GetFramesToTarget(max_crossfade_duration()) * 2; | 615 input_timestamp_helper_.GetFramesToTarget(max_crossfade_duration()) * 2; |
| 617 | 616 |
| 618 scoped_refptr<AudioBuffer> first_buffer = | 617 scoped_refptr<AudioBuffer> first_buffer = |
| 619 GetNextInputBuffer(1.0f, kBufferSize); | 618 GetNextInputBuffer(1.0f, kBufferSize); |
| 620 // Fuzz the duration slightly so that the buffer overlaps the splice timestamp | 619 // Fuzz the duration slightly so that the buffer overlaps the splice timestamp |
| 621 // by a microsecond, which is not enough to crossfade. | 620 // by a microsecond, which is not enough to crossfade. |
| 622 first_buffer->set_duration(first_buffer->duration() + | 621 const base::TimeDelta kSpliceTimestamp = |
| 623 base::TimeDelta::FromMicroseconds(1)); | 622 input_timestamp_helper_.GetTimestamp() - |
| 624 splicer_.SetSpliceTimestamp(input_timestamp_helper_.GetTimestamp()); | 623 base::TimeDelta::FromMicroseconds(1); |
| 624 splicer_.SetSpliceTimestamp(kSpliceTimestamp); |
| 625 scoped_refptr<AudioBuffer> second_buffer = | 625 scoped_refptr<AudioBuffer> second_buffer = |
| 626 GetNextInputBuffer(0.0f, kBufferSize); | 626 GetNextInputBuffer(0.0f, kBufferSize); |
| 627 second_buffer->set_timestamp(kSpliceTimestamp); |
| 627 | 628 |
| 628 // The splicer should be internally queuing input since |first_buffer| is part | 629 // The splicer should be internally queuing input since |first_buffer| is part |
| 629 // of the supposed splice. | 630 // of the supposed splice. |
| 630 EXPECT_TRUE(AddInput(first_buffer)); | 631 EXPECT_TRUE(AddInput(first_buffer)); |
| 631 EXPECT_FALSE(splicer_.HasNextBuffer()); | 632 EXPECT_FALSE(splicer_.HasNextBuffer()); |
| 632 | 633 |
| 633 // |second_buffer| should complete the supposed splice, so ensure output is | 634 // |second_buffer| should complete the supposed splice, so ensure output is |
| 634 // now available. | 635 // now available. |
| 635 splicer_.SetSpliceTimestamp(kNoTimestamp()); | 636 splicer_.SetSpliceTimestamp(kNoTimestamp()); |
| 636 EXPECT_TRUE(AddInput(second_buffer)); | 637 EXPECT_TRUE(AddInput(second_buffer)); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 // |second_buffer| will complete the supposed splice. | 676 // |second_buffer| will complete the supposed splice. |
| 676 splicer_.SetSpliceTimestamp(kNoTimestamp()); | 677 splicer_.SetSpliceTimestamp(kNoTimestamp()); |
| 677 EXPECT_TRUE(AddInput(second_buffer)); | 678 EXPECT_TRUE(AddInput(second_buffer)); |
| 678 | 679 |
| 679 VerifyNextBuffer(gap_buffer); | 680 VerifyNextBuffer(gap_buffer); |
| 680 VerifyNextBuffer(second_buffer); | 681 VerifyNextBuffer(second_buffer); |
| 681 EXPECT_FALSE(splicer_.HasNextBuffer()); | 682 EXPECT_FALSE(splicer_.HasNextBuffer()); |
| 682 } | 683 } |
| 683 | 684 |
| 684 } // namespace media | 685 } // namespace media |
| OLD | NEW |