| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/run_loop.h" |
| 6 #include "base/test/test_message_loop.h" |
| 7 #include "content/renderer/media/webaudio_suspender.h" |
| 8 #include "media/base/fake_audio_render_callback.h" |
| 9 #include "media/base/mock_audio_renderer_sink.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 ACTION_P(RunClosure, closure) { |
| 16 closure.Run(); |
| 17 } |
| 18 |
| 19 class WebAudioSuspenderTest : public testing::Test { |
| 20 public: |
| 21 WebAudioSuspenderTest() |
| 22 : params_(media::AudioParameters::AUDIO_FAKE, |
| 23 media::CHANNEL_LAYOUT_MONO, |
| 24 44100, |
| 25 8, |
| 26 128), |
| 27 mock_sink_(new testing::StrictMock<media::MockAudioRendererSink>()), |
| 28 fake_callback_(0.1), |
| 29 temp_bus_(media::AudioBus::Create(params_)), |
| 30 suspender_(&fake_callback_, |
| 31 params_, |
| 32 mock_sink_, |
| 33 test_loop_.task_runner()) {} |
| 34 ~WebAudioSuspenderTest() override {} |
| 35 |
| 36 protected: |
| 37 base::TestMessageLoop test_loop_; |
| 38 const media::AudioParameters params_; |
| 39 scoped_refptr<testing::StrictMock<media::MockAudioRendererSink>> mock_sink_; |
| 40 media::FakeAudioRenderCallback fake_callback_; |
| 41 std::unique_ptr<media::AudioBus> temp_bus_; |
| 42 WebAudioSuspender suspender_; |
| 43 |
| 44 private: |
| 45 DISALLOW_COPY_AND_ASSIGN(WebAudioSuspenderTest); |
| 46 }; |
| 47 |
| 48 TEST_F(WebAudioSuspenderTest, BasicPassthough) { |
| 49 temp_bus_->Zero(); |
| 50 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 51 EXPECT_FALSE(temp_bus_->AreFramesZero()); |
| 52 } |
| 53 |
| 54 TEST_F(WebAudioSuspenderTest, SuspendResumeTriggered) { |
| 55 // Set a negative timeout so any silence will suspend immediately. |
| 56 suspender_.set_silent_timeout_for_testing(base::TimeDelta::FromSeconds(-1)); |
| 57 |
| 58 // Verify a normal Render() doesn't invoke suspend. |
| 59 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing()); |
| 60 temp_bus_->Zero(); |
| 61 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 62 EXPECT_FALSE(temp_bus_->AreFramesZero()); |
| 63 base::RunLoop().RunUntilIdle(); |
| 64 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing()); |
| 65 |
| 66 // Mute all audio generated by the callback, this should suspend immediately. |
| 67 fake_callback_.set_volume(0); |
| 68 temp_bus_->Zero(); |
| 69 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 70 EXPECT_TRUE(temp_bus_->AreFramesZero()); |
| 71 EXPECT_CALL(*mock_sink_, Pause()); |
| 72 base::RunLoop().RunUntilIdle(); |
| 73 EXPECT_TRUE(suspender_.is_using_fake_sink_for_testing()); |
| 74 |
| 75 // Unmute the audio, the FakeWorker inside |suspender_| should be running now, |
| 76 // so we don't need to manually invoke Render(). |
| 77 fake_callback_.reset(); |
| 78 fake_callback_.set_volume(1); |
| 79 base::RunLoop run_loop; |
| 80 EXPECT_CALL(*mock_sink_, Play()).WillOnce(RunClosure(run_loop.QuitClosure())); |
| 81 run_loop.Run(); |
| 82 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing()); |
| 83 |
| 84 // The first Render() after resume should return the first buffer which was |
| 85 // not silent. |
| 86 fake_callback_.reset(); |
| 87 std::unique_ptr<media::AudioBus> true_bus = media::AudioBus::Create(params_); |
| 88 fake_callback_.Render(true_bus.get(), 0, 0); |
| 89 EXPECT_FALSE(true_bus->AreFramesZero()); |
| 90 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 91 EXPECT_EQ(memcmp(temp_bus_->channel(0), true_bus->channel(0), |
| 92 temp_bus_->frames() * sizeof(float)), |
| 93 0); |
| 94 } |
| 95 |
| 96 TEST_F(WebAudioSuspenderTest, MultipleSuspend) { |
| 97 // Set a negative timeout so any silence will suspend immediately. |
| 98 suspender_.set_silent_timeout_for_testing(base::TimeDelta::FromSeconds(-1)); |
| 99 |
| 100 // Mute all audio generated by the callback, this should suspend immediately. |
| 101 fake_callback_.set_volume(0); |
| 102 temp_bus_->Zero(); |
| 103 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 104 EXPECT_TRUE(temp_bus_->AreFramesZero()); |
| 105 |
| 106 // A second render should only result in a single Pause() call. |
| 107 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 108 |
| 109 EXPECT_CALL(*mock_sink_, Pause()); |
| 110 base::RunLoop().RunUntilIdle(); |
| 111 EXPECT_TRUE(suspender_.is_using_fake_sink_for_testing()); |
| 112 } |
| 113 |
| 114 TEST_F(WebAudioSuspenderTest, MultipleResume) { |
| 115 // Set a negative timeout so any silence will suspend immediately. |
| 116 suspender_.set_silent_timeout_for_testing(base::TimeDelta::FromSeconds(-1)); |
| 117 |
| 118 // Mute all audio generated by the callback, this should suspend immediately. |
| 119 fake_callback_.set_volume(0); |
| 120 temp_bus_->Zero(); |
| 121 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 122 EXPECT_TRUE(temp_bus_->AreFramesZero()); |
| 123 EXPECT_CALL(*mock_sink_, Pause()); |
| 124 base::RunLoop().RunUntilIdle(); |
| 125 EXPECT_TRUE(suspender_.is_using_fake_sink_for_testing()); |
| 126 |
| 127 // Unmute the data. |
| 128 fake_callback_.set_volume(1); |
| 129 |
| 130 // Prepare our equality testers. |
| 131 fake_callback_.reset(); |
| 132 std::unique_ptr<media::AudioBus> true_bus1 = media::AudioBus::Create(params_); |
| 133 fake_callback_.Render(true_bus1.get(), 0, 0); |
| 134 std::unique_ptr<media::AudioBus> true_bus2 = media::AudioBus::Create(params_); |
| 135 fake_callback_.Render(true_bus2.get(), 0, 0); |
| 136 EXPECT_NE(memcmp(true_bus1->channel(0), true_bus2->channel(0), |
| 137 true_bus1->frames() * sizeof(float)), |
| 138 0); |
| 139 |
| 140 // Reset the fake callback data generation and force two Render() calls before |
| 141 // the sink can transition. |
| 142 fake_callback_.reset(); |
| 143 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(nullptr, 0, 0)); |
| 144 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(nullptr, 0, 0)); |
| 145 EXPECT_CALL(*mock_sink_, Play()); |
| 146 base::RunLoop().RunUntilIdle(); |
| 147 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing()); |
| 148 |
| 149 // Each render after resuming should return one of the non-silent bus. |
| 150 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 151 EXPECT_EQ(memcmp(temp_bus_->channel(0), true_bus1->channel(0), |
| 152 temp_bus_->frames() * sizeof(float)), |
| 153 0); |
| 154 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 155 EXPECT_EQ(memcmp(temp_bus_->channel(0), true_bus2->channel(0), |
| 156 temp_bus_->frames() * sizeof(float)), |
| 157 0); |
| 158 } |
| 159 |
| 160 } // namespace content |
| OLD | NEW |