| 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 "media/renderers/audio_renderer_impl.h" | 5 #include "media/renderers/audio_renderer_impl.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 void DeliverRemainingAudio() { | 310 void DeliverRemainingAudio() { |
| 311 while (frames_remaining_in_buffer().value > 0) { | 311 while (frames_remaining_in_buffer().value > 0) { |
| 312 SatisfyPendingRead(InputFrames(256)); | 312 SatisfyPendingRead(InputFrames(256)); |
| 313 } | 313 } |
| 314 } | 314 } |
| 315 | 315 |
| 316 // Attempts to consume |requested_frames| frames from |renderer_|'s internal | 316 // Attempts to consume |requested_frames| frames from |renderer_|'s internal |
| 317 // buffer. Returns true if and only if all of |requested_frames| were able | 317 // buffer. Returns true if and only if all of |requested_frames| were able |
| 318 // to be consumed. | 318 // to be consumed. |
| 319 bool ConsumeBufferedData(OutputFrames requested_frames, | 319 bool ConsumeBufferedData(OutputFrames requested_frames, |
| 320 uint32_t frames_delayed) { | 320 base::TimeDelta delay) { |
| 321 std::unique_ptr<AudioBus> bus = | 321 std::unique_ptr<AudioBus> bus = |
| 322 AudioBus::Create(kChannels, requested_frames.value); | 322 AudioBus::Create(kChannels, requested_frames.value); |
| 323 int frames_read = 0; | 323 int frames_read = 0; |
| 324 EXPECT_TRUE(sink_->Render(bus.get(), frames_delayed, &frames_read)); | 324 EXPECT_TRUE(sink_->Render(bus.get(), delay, &frames_read)); |
| 325 return frames_read == requested_frames.value; | 325 return frames_read == requested_frames.value; |
| 326 } | 326 } |
| 327 | 327 |
| 328 bool ConsumeBufferedData(OutputFrames requested_frames) { | 328 bool ConsumeBufferedData(OutputFrames requested_frames) { |
| 329 return ConsumeBufferedData(requested_frames, 0); | 329 return ConsumeBufferedData(requested_frames, base::TimeDelta()); |
| 330 } | 330 } |
| 331 | 331 |
| 332 base::TimeTicks ConvertMediaTime(base::TimeDelta timestamp, | 332 base::TimeTicks ConvertMediaTime(base::TimeDelta timestamp, |
| 333 bool* is_time_moving) { | 333 bool* is_time_moving) { |
| 334 std::vector<base::TimeTicks> wall_clock_times; | 334 std::vector<base::TimeTicks> wall_clock_times; |
| 335 *is_time_moving = renderer_->GetWallClockTimes( | 335 *is_time_moving = renderer_->GetWallClockTimes( |
| 336 std::vector<base::TimeDelta>(1, timestamp), &wall_clock_times); | 336 std::vector<base::TimeDelta>(1, timestamp), &wall_clock_times); |
| 337 return wall_clock_times[0]; | 337 return wall_clock_times[0]; |
| 338 } | 338 } |
| 339 | 339 |
| (...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 768 base::TimeDelta::FromSecondsD(hardware_params_.frames_per_buffer() * | 768 base::TimeDelta::FromSecondsD(hardware_params_.frames_per_buffer() * |
| 769 kBuffers / hardware_params_.sample_rate()); | 769 kBuffers / hardware_params_.sample_rate()); |
| 770 | 770 |
| 771 Preroll(base::TimeDelta(), first_timestamp, PIPELINE_OK); | 771 Preroll(base::TimeDelta(), first_timestamp, PIPELINE_OK); |
| 772 StartTicking(); | 772 StartTicking(); |
| 773 | 773 |
| 774 // Verify the first few buffers are silent. | 774 // Verify the first few buffers are silent. |
| 775 std::unique_ptr<AudioBus> bus = AudioBus::Create(hardware_params_); | 775 std::unique_ptr<AudioBus> bus = AudioBus::Create(hardware_params_); |
| 776 int frames_read = 0; | 776 int frames_read = 0; |
| 777 for (int i = 0; i < std::floor(kBuffers); ++i) { | 777 for (int i = 0; i < std::floor(kBuffers); ++i) { |
| 778 EXPECT_TRUE(sink_->Render(bus.get(), 0, &frames_read)); | 778 EXPECT_TRUE(sink_->Render(bus.get(), base::TimeDelta(), &frames_read)); |
| 779 EXPECT_EQ(frames_read, bus->frames()); | 779 EXPECT_EQ(frames_read, bus->frames()); |
| 780 for (int j = 0; j < bus->frames(); ++j) | 780 for (int j = 0; j < bus->frames(); ++j) |
| 781 ASSERT_FLOAT_EQ(0.0f, bus->channel(0)[j]); | 781 ASSERT_FLOAT_EQ(0.0f, bus->channel(0)[j]); |
| 782 WaitForPendingRead(); | 782 WaitForPendingRead(); |
| 783 DeliverRemainingAudio(); | 783 DeliverRemainingAudio(); |
| 784 } | 784 } |
| 785 | 785 |
| 786 // Verify the last buffer is half silence and half real data. | 786 // Verify the last buffer is half silence and half real data. |
| 787 EXPECT_TRUE(sink_->Render(bus.get(), 0, &frames_read)); | 787 EXPECT_TRUE(sink_->Render(bus.get(), base::TimeDelta(), &frames_read)); |
| 788 EXPECT_EQ(frames_read, bus->frames()); | 788 EXPECT_EQ(frames_read, bus->frames()); |
| 789 const int zero_frames = | 789 const int zero_frames = |
| 790 bus->frames() * (kBuffers - static_cast<int>(kBuffers)); | 790 bus->frames() * (kBuffers - static_cast<int>(kBuffers)); |
| 791 | 791 |
| 792 for (int i = 0; i < zero_frames; ++i) | 792 for (int i = 0; i < zero_frames; ++i) |
| 793 ASSERT_FLOAT_EQ(0.0f, bus->channel(0)[i]); | 793 ASSERT_FLOAT_EQ(0.0f, bus->channel(0)[i]); |
| 794 for (int i = zero_frames; i < bus->frames(); ++i) | 794 for (int i = zero_frames; i < bus->frames(); ++i) |
| 795 ASSERT_NE(0.0f, bus->channel(0)[i]); | 795 ASSERT_NE(0.0f, bus->channel(0)[i]); |
| 796 } | 796 } |
| 797 | 797 |
| 798 TEST_F(AudioRendererImplTest, RenderingDelayedForSuspend) { | 798 TEST_F(AudioRendererImplTest, RenderingDelayedForSuspend) { |
| 799 Initialize(); | 799 Initialize(); |
| 800 Preroll(base::TimeDelta(), base::TimeDelta(), PIPELINE_OK); | 800 Preroll(base::TimeDelta(), base::TimeDelta(), PIPELINE_OK); |
| 801 StartTicking(); | 801 StartTicking(); |
| 802 | 802 |
| 803 // Verify the first buffer is real data. | 803 // Verify the first buffer is real data. |
| 804 int frames_read = 0; | 804 int frames_read = 0; |
| 805 std::unique_ptr<AudioBus> bus = AudioBus::Create(hardware_params_); | 805 std::unique_ptr<AudioBus> bus = AudioBus::Create(hardware_params_); |
| 806 EXPECT_TRUE(sink_->Render(bus.get(), 0, &frames_read)); | 806 EXPECT_TRUE(sink_->Render(bus.get(), base::TimeDelta(), &frames_read)); |
| 807 EXPECT_NE(0, frames_read); | 807 EXPECT_NE(0, frames_read); |
| 808 for (int i = 0; i < bus->frames(); ++i) | 808 for (int i = 0; i < bus->frames(); ++i) |
| 809 ASSERT_NE(0.0f, bus->channel(0)[i]); | 809 ASSERT_NE(0.0f, bus->channel(0)[i]); |
| 810 | 810 |
| 811 // Verify after suspend we get silence. | 811 // Verify after suspend we get silence. |
| 812 renderer_->OnSuspend(); | 812 renderer_->OnSuspend(); |
| 813 EXPECT_TRUE(sink_->Render(bus.get(), 0, &frames_read)); | 813 EXPECT_TRUE(sink_->Render(bus.get(), base::TimeDelta(), &frames_read)); |
| 814 EXPECT_EQ(0, frames_read); | 814 EXPECT_EQ(0, frames_read); |
| 815 | 815 |
| 816 // Verify after resume we get audio. | 816 // Verify after resume we get audio. |
| 817 bus->Zero(); | 817 bus->Zero(); |
| 818 renderer_->OnResume(); | 818 renderer_->OnResume(); |
| 819 EXPECT_TRUE(sink_->Render(bus.get(), 0, &frames_read)); | 819 EXPECT_TRUE(sink_->Render(bus.get(), base::TimeDelta(), &frames_read)); |
| 820 EXPECT_NE(0, frames_read); | 820 EXPECT_NE(0, frames_read); |
| 821 for (int i = 0; i < bus->frames(); ++i) | 821 for (int i = 0; i < bus->frames(); ++i) |
| 822 ASSERT_NE(0.0f, bus->channel(0)[i]); | 822 ASSERT_NE(0.0f, bus->channel(0)[i]); |
| 823 } | 823 } |
| 824 | 824 |
| 825 TEST_F(AudioRendererImplTest, RenderingDelayDoesNotOverflow) { | 825 TEST_F(AudioRendererImplTest, RenderingDelayDoesNotOverflow) { |
| 826 Initialize(); | 826 Initialize(); |
| 827 | 827 |
| 828 // Choose a first timestamp as far into the future as possible. Without care | 828 // Choose a first timestamp as far into the future as possible. Without care |
| 829 // this can cause an overflow in rendering arithmetic. | 829 // this can cause an overflow in rendering arithmetic. |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 998 // Time should be stopped until the next render call. | 998 // Time should be stopped until the next render call. |
| 999 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving)); | 999 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving)); |
| 1000 EXPECT_FALSE(is_time_moving); | 1000 EXPECT_FALSE(is_time_moving); |
| 1001 | 1001 |
| 1002 // Consume some buffered data with a small delay. | 1002 // Consume some buffered data with a small delay. |
| 1003 uint32_t delay_frames = 500; | 1003 uint32_t delay_frames = 500; |
| 1004 base::TimeDelta delay_time = base::TimeDelta::FromMicroseconds( | 1004 base::TimeDelta delay_time = base::TimeDelta::FromMicroseconds( |
| 1005 std::round(delay_frames * kOutputMicrosPerFrame)); | 1005 std::round(delay_frames * kOutputMicrosPerFrame)); |
| 1006 | 1006 |
| 1007 frames_to_consume.value = frames_buffered().value / 16; | 1007 frames_to_consume.value = frames_buffered().value / 16; |
| 1008 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume, delay_frames)); | 1008 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume, delay_time)); |
| 1009 | 1009 |
| 1010 // Verify time is adjusted for the current delay. | 1010 // Verify time is adjusted for the current delay. |
| 1011 current_time = tick_clock_->NowTicks() + delay_time; | 1011 current_time = tick_clock_->NowTicks() + delay_time; |
| 1012 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving)); | 1012 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving)); |
| 1013 EXPECT_TRUE(is_time_moving); | 1013 EXPECT_TRUE(is_time_moving); |
| 1014 EXPECT_EQ(current_time, | 1014 EXPECT_EQ(current_time, |
| 1015 ConvertMediaTime(renderer_->CurrentMediaTime(), &is_time_moving)); | 1015 ConvertMediaTime(renderer_->CurrentMediaTime(), &is_time_moving)); |
| 1016 EXPECT_TRUE(is_time_moving); | 1016 EXPECT_TRUE(is_time_moving); |
| 1017 | 1017 |
| 1018 tick_clock_->Advance(kOneSample); | 1018 tick_clock_->Advance(kOneSample); |
| 1019 renderer_->SetPlaybackRate(2); | 1019 renderer_->SetPlaybackRate(2); |
| 1020 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving)); | 1020 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving)); |
| 1021 EXPECT_TRUE(is_time_moving); | 1021 EXPECT_TRUE(is_time_moving); |
| 1022 EXPECT_EQ(current_time + kOneSample * 2, | 1022 EXPECT_EQ(current_time + kOneSample * 2, |
| 1023 ConvertMediaTime(renderer_->CurrentMediaTime(), &is_time_moving)); | 1023 ConvertMediaTime(renderer_->CurrentMediaTime(), &is_time_moving)); |
| 1024 EXPECT_TRUE(is_time_moving); | 1024 EXPECT_TRUE(is_time_moving); |
| 1025 | 1025 |
| 1026 // Advance far enough that we shouldn't be clamped to current time (tested | 1026 // Advance far enough that we shouldn't be clamped to current time (tested |
| 1027 // already above). | 1027 // already above). |
| 1028 tick_clock_->Advance(kOneSecond); | 1028 tick_clock_->Advance(kOneSecond); |
| 1029 EXPECT_EQ( | 1029 EXPECT_EQ( |
| 1030 current_time + timestamp_helper.GetFrameDuration(frames_to_consume.value), | 1030 current_time + timestamp_helper.GetFrameDuration(frames_to_consume.value), |
| 1031 CurrentMediaWallClockTime(&is_time_moving)); | 1031 CurrentMediaWallClockTime(&is_time_moving)); |
| 1032 EXPECT_TRUE(is_time_moving); | 1032 EXPECT_TRUE(is_time_moving); |
| 1033 } | 1033 } |
| 1034 | 1034 |
| 1035 } // namespace media | 1035 } // namespace media |
| OLD | NEW |