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 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 void DeliverRemainingAudio() { | 309 void DeliverRemainingAudio() { |
310 while (frames_remaining_in_buffer().value > 0) { | 310 while (frames_remaining_in_buffer().value > 0) { |
311 SatisfyPendingRead(InputFrames(256)); | 311 SatisfyPendingRead(InputFrames(256)); |
312 } | 312 } |
313 } | 313 } |
314 | 314 |
315 // Attempts to consume |requested_frames| frames from |renderer_|'s internal | 315 // Attempts to consume |requested_frames| frames from |renderer_|'s internal |
316 // buffer. Returns true if and only if all of |requested_frames| were able | 316 // buffer. Returns true if and only if all of |requested_frames| were able |
317 // to be consumed. | 317 // to be consumed. |
318 bool ConsumeBufferedData(OutputFrames requested_frames, | 318 bool ConsumeBufferedData(OutputFrames requested_frames, |
319 uint32_t frames_delayed) { | 319 base::TimeDelta delay) { |
320 std::unique_ptr<AudioBus> bus = | 320 std::unique_ptr<AudioBus> bus = |
321 AudioBus::Create(kChannels, requested_frames.value); | 321 AudioBus::Create(kChannels, requested_frames.value); |
322 int frames_read = 0; | 322 int frames_read = 0; |
323 EXPECT_TRUE(sink_->Render(bus.get(), frames_delayed, &frames_read)); | 323 EXPECT_TRUE(sink_->Render(bus.get(), delay, &frames_read)); |
324 return frames_read == requested_frames.value; | 324 return frames_read == requested_frames.value; |
325 } | 325 } |
326 | 326 |
327 bool ConsumeBufferedData(OutputFrames requested_frames) { | 327 bool ConsumeBufferedData(OutputFrames requested_frames) { |
328 return ConsumeBufferedData(requested_frames, 0); | 328 return ConsumeBufferedData(requested_frames, base::TimeDelta()); |
329 } | 329 } |
330 | 330 |
331 base::TimeTicks ConvertMediaTime(base::TimeDelta timestamp, | 331 base::TimeTicks ConvertMediaTime(base::TimeDelta timestamp, |
332 bool* is_time_moving) { | 332 bool* is_time_moving) { |
333 std::vector<base::TimeTicks> wall_clock_times; | 333 std::vector<base::TimeTicks> wall_clock_times; |
334 *is_time_moving = renderer_->GetWallClockTimes( | 334 *is_time_moving = renderer_->GetWallClockTimes( |
335 std::vector<base::TimeDelta>(1, timestamp), &wall_clock_times); | 335 std::vector<base::TimeDelta>(1, timestamp), &wall_clock_times); |
336 return wall_clock_times[0]; | 336 return wall_clock_times[0]; |
337 } | 337 } |
338 | 338 |
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
735 base::TimeDelta::FromSecondsD(hardware_params_.frames_per_buffer() * | 735 base::TimeDelta::FromSecondsD(hardware_params_.frames_per_buffer() * |
736 kBuffers / hardware_params_.sample_rate()); | 736 kBuffers / hardware_params_.sample_rate()); |
737 | 737 |
738 Preroll(base::TimeDelta(), first_timestamp, PIPELINE_OK); | 738 Preroll(base::TimeDelta(), first_timestamp, PIPELINE_OK); |
739 StartTicking(); | 739 StartTicking(); |
740 | 740 |
741 // Verify the first few buffers are silent. | 741 // Verify the first few buffers are silent. |
742 std::unique_ptr<AudioBus> bus = AudioBus::Create(hardware_params_); | 742 std::unique_ptr<AudioBus> bus = AudioBus::Create(hardware_params_); |
743 int frames_read = 0; | 743 int frames_read = 0; |
744 for (int i = 0; i < std::floor(kBuffers); ++i) { | 744 for (int i = 0; i < std::floor(kBuffers); ++i) { |
745 EXPECT_TRUE(sink_->Render(bus.get(), 0, &frames_read)); | 745 EXPECT_TRUE(sink_->Render(bus.get(), base::TimeDelta(), &frames_read)); |
746 EXPECT_EQ(frames_read, bus->frames()); | 746 EXPECT_EQ(frames_read, bus->frames()); |
747 for (int j = 0; j < bus->frames(); ++j) | 747 for (int j = 0; j < bus->frames(); ++j) |
748 ASSERT_FLOAT_EQ(0.0f, bus->channel(0)[j]); | 748 ASSERT_FLOAT_EQ(0.0f, bus->channel(0)[j]); |
749 WaitForPendingRead(); | 749 WaitForPendingRead(); |
750 DeliverRemainingAudio(); | 750 DeliverRemainingAudio(); |
751 } | 751 } |
752 | 752 |
753 // Verify the last buffer is half silence and half real data. | 753 // Verify the last buffer is half silence and half real data. |
754 EXPECT_TRUE(sink_->Render(bus.get(), 0, &frames_read)); | 754 EXPECT_TRUE(sink_->Render(bus.get(), base::TimeDelta(), &frames_read)); |
755 EXPECT_EQ(frames_read, bus->frames()); | 755 EXPECT_EQ(frames_read, bus->frames()); |
756 const int zero_frames = | 756 const int zero_frames = |
757 bus->frames() * (kBuffers - static_cast<int>(kBuffers)); | 757 bus->frames() * (kBuffers - static_cast<int>(kBuffers)); |
758 | 758 |
759 for (int i = 0; i < zero_frames; ++i) | 759 for (int i = 0; i < zero_frames; ++i) |
760 ASSERT_FLOAT_EQ(0.0f, bus->channel(0)[i]); | 760 ASSERT_FLOAT_EQ(0.0f, bus->channel(0)[i]); |
761 for (int i = zero_frames; i < bus->frames(); ++i) | 761 for (int i = zero_frames; i < bus->frames(); ++i) |
762 ASSERT_NE(0.0f, bus->channel(0)[i]); | 762 ASSERT_NE(0.0f, bus->channel(0)[i]); |
763 } | 763 } |
764 | 764 |
765 TEST_F(AudioRendererImplTest, RenderingDelayedForSuspend) { | 765 TEST_F(AudioRendererImplTest, RenderingDelayedForSuspend) { |
766 Initialize(); | 766 Initialize(); |
767 Preroll(base::TimeDelta(), base::TimeDelta(), PIPELINE_OK); | 767 Preroll(base::TimeDelta(), base::TimeDelta(), PIPELINE_OK); |
768 StartTicking(); | 768 StartTicking(); |
769 | 769 |
770 // Verify the first buffer is real data. | 770 // Verify the first buffer is real data. |
771 int frames_read = 0; | 771 int frames_read = 0; |
772 std::unique_ptr<AudioBus> bus = AudioBus::Create(hardware_params_); | 772 std::unique_ptr<AudioBus> bus = AudioBus::Create(hardware_params_); |
773 EXPECT_TRUE(sink_->Render(bus.get(), 0, &frames_read)); | 773 EXPECT_TRUE(sink_->Render(bus.get(), base::TimeDelta(), &frames_read)); |
774 EXPECT_NE(0, frames_read); | 774 EXPECT_NE(0, frames_read); |
775 for (int i = 0; i < bus->frames(); ++i) | 775 for (int i = 0; i < bus->frames(); ++i) |
776 ASSERT_NE(0.0f, bus->channel(0)[i]); | 776 ASSERT_NE(0.0f, bus->channel(0)[i]); |
777 | 777 |
778 // Verify after suspend we get silence. | 778 // Verify after suspend we get silence. |
779 renderer_->OnSuspend(); | 779 renderer_->OnSuspend(); |
780 EXPECT_TRUE(sink_->Render(bus.get(), 0, &frames_read)); | 780 EXPECT_TRUE(sink_->Render(bus.get(), base::TimeDelta(), &frames_read)); |
781 EXPECT_EQ(0, frames_read); | 781 EXPECT_EQ(0, frames_read); |
782 | 782 |
783 // Verify after resume we get audio. | 783 // Verify after resume we get audio. |
784 bus->Zero(); | 784 bus->Zero(); |
785 renderer_->OnResume(); | 785 renderer_->OnResume(); |
786 EXPECT_TRUE(sink_->Render(bus.get(), 0, &frames_read)); | 786 EXPECT_TRUE(sink_->Render(bus.get(), base::TimeDelta(), &frames_read)); |
787 EXPECT_NE(0, frames_read); | 787 EXPECT_NE(0, frames_read); |
788 for (int i = 0; i < bus->frames(); ++i) | 788 for (int i = 0; i < bus->frames(); ++i) |
789 ASSERT_NE(0.0f, bus->channel(0)[i]); | 789 ASSERT_NE(0.0f, bus->channel(0)[i]); |
790 } | 790 } |
791 | 791 |
792 TEST_F(AudioRendererImplTest, RenderingDelayDoesNotOverflow) { | 792 TEST_F(AudioRendererImplTest, RenderingDelayDoesNotOverflow) { |
793 Initialize(); | 793 Initialize(); |
794 | 794 |
795 // Choose a first timestamp as far into the future as possible. Without care | 795 // Choose a first timestamp as far into the future as possible. Without care |
796 // this can cause an overflow in rendering arithmetic. | 796 // this can cause an overflow in rendering arithmetic. |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
965 // Time should be stopped until the next render call. | 965 // Time should be stopped until the next render call. |
966 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving)); | 966 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving)); |
967 EXPECT_FALSE(is_time_moving); | 967 EXPECT_FALSE(is_time_moving); |
968 | 968 |
969 // Consume some buffered data with a small delay. | 969 // Consume some buffered data with a small delay. |
970 uint32_t delay_frames = 500; | 970 uint32_t delay_frames = 500; |
971 base::TimeDelta delay_time = base::TimeDelta::FromMicroseconds( | 971 base::TimeDelta delay_time = base::TimeDelta::FromMicroseconds( |
972 std::round(delay_frames * kOutputMicrosPerFrame)); | 972 std::round(delay_frames * kOutputMicrosPerFrame)); |
973 | 973 |
974 frames_to_consume.value = frames_buffered().value / 16; | 974 frames_to_consume.value = frames_buffered().value / 16; |
975 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume, delay_frames)); | 975 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume, delay_time)); |
976 | 976 |
977 // Verify time is adjusted for the current delay. | 977 // Verify time is adjusted for the current delay. |
978 current_time = tick_clock_->NowTicks() + delay_time; | 978 current_time = tick_clock_->NowTicks() + delay_time; |
979 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving)); | 979 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving)); |
980 EXPECT_TRUE(is_time_moving); | 980 EXPECT_TRUE(is_time_moving); |
981 EXPECT_EQ(current_time, | 981 EXPECT_EQ(current_time, |
982 ConvertMediaTime(renderer_->CurrentMediaTime(), &is_time_moving)); | 982 ConvertMediaTime(renderer_->CurrentMediaTime(), &is_time_moving)); |
983 EXPECT_TRUE(is_time_moving); | 983 EXPECT_TRUE(is_time_moving); |
984 | 984 |
985 tick_clock_->Advance(kOneSample); | 985 tick_clock_->Advance(kOneSample); |
986 renderer_->SetPlaybackRate(2); | 986 renderer_->SetPlaybackRate(2); |
987 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving)); | 987 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving)); |
988 EXPECT_TRUE(is_time_moving); | 988 EXPECT_TRUE(is_time_moving); |
989 EXPECT_EQ(current_time + kOneSample * 2, | 989 EXPECT_EQ(current_time + kOneSample * 2, |
990 ConvertMediaTime(renderer_->CurrentMediaTime(), &is_time_moving)); | 990 ConvertMediaTime(renderer_->CurrentMediaTime(), &is_time_moving)); |
991 EXPECT_TRUE(is_time_moving); | 991 EXPECT_TRUE(is_time_moving); |
992 | 992 |
993 // Advance far enough that we shouldn't be clamped to current time (tested | 993 // Advance far enough that we shouldn't be clamped to current time (tested |
994 // already above). | 994 // already above). |
995 tick_clock_->Advance(kOneSecond); | 995 tick_clock_->Advance(kOneSecond); |
996 EXPECT_EQ( | 996 EXPECT_EQ( |
997 current_time + timestamp_helper.GetFrameDuration(frames_to_consume.value), | 997 current_time + timestamp_helper.GetFrameDuration(frames_to_consume.value), |
998 CurrentMediaWallClockTime(&is_time_moving)); | 998 CurrentMediaWallClockTime(&is_time_moving)); |
999 EXPECT_TRUE(is_time_moving); | 999 EXPECT_TRUE(is_time_moving); |
1000 } | 1000 } |
1001 | 1001 |
1002 } // namespace media | 1002 } // namespace media |
OLD | NEW |