| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/run_loop.h" | 5 #include "base/run_loop.h" |
| 6 #include "base/test/test_message_loop.h" | 6 #include "base/test/test_message_loop.h" |
| 7 #include "media/base/fake_audio_render_callback.h" | 7 #include "media/base/fake_audio_render_callback.h" |
| 8 #include "media/base/mock_audio_renderer_sink.h" | 8 #include "media/base/mock_audio_renderer_sink.h" |
| 9 #include "media/base/silent_sink_suspender.h" | 9 #include "media/base/silent_sink_suspender.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 FakeAudioRenderCallback fake_callback_; | 42 FakeAudioRenderCallback fake_callback_; |
| 43 std::unique_ptr<AudioBus> temp_bus_; | 43 std::unique_ptr<AudioBus> temp_bus_; |
| 44 SilentSinkSuspender suspender_; | 44 SilentSinkSuspender suspender_; |
| 45 | 45 |
| 46 private: | 46 private: |
| 47 DISALLOW_COPY_AND_ASSIGN(SilentSinkSuspenderTest); | 47 DISALLOW_COPY_AND_ASSIGN(SilentSinkSuspenderTest); |
| 48 }; | 48 }; |
| 49 | 49 |
| 50 TEST_F(SilentSinkSuspenderTest, BasicPassthough) { | 50 TEST_F(SilentSinkSuspenderTest, BasicPassthough) { |
| 51 temp_bus_->Zero(); | 51 temp_bus_->Zero(); |
| 52 EXPECT_EQ(temp_bus_->frames(), | 52 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 53 suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0, | |
| 54 temp_bus_.get())); | |
| 55 EXPECT_FALSE(temp_bus_->AreFramesZero()); | 53 EXPECT_FALSE(temp_bus_->AreFramesZero()); |
| 56 } | 54 } |
| 57 | 55 |
| 58 TEST_F(SilentSinkSuspenderTest, SuspendResumeTriggered) { | 56 TEST_F(SilentSinkSuspenderTest, SuspendResumeTriggered) { |
| 59 // Verify a normal Render() doesn't invoke suspend. | 57 // Verify a normal Render() doesn't invoke suspend. |
| 60 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing()); | 58 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing()); |
| 61 temp_bus_->Zero(); | 59 temp_bus_->Zero(); |
| 62 EXPECT_EQ(temp_bus_->frames(), | 60 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 63 suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0, | |
| 64 temp_bus_.get())); | |
| 65 EXPECT_FALSE(temp_bus_->AreFramesZero()); | 61 EXPECT_FALSE(temp_bus_->AreFramesZero()); |
| 66 base::RunLoop().RunUntilIdle(); | 62 base::RunLoop().RunUntilIdle(); |
| 67 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing()); | 63 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing()); |
| 68 | 64 |
| 69 // Mute all audio generated by the callback, this should suspend immediately. | 65 // Mute all audio generated by the callback, this should suspend immediately. |
| 70 fake_callback_.set_volume(0); | 66 fake_callback_.set_volume(0); |
| 71 temp_bus_->Zero(); | 67 temp_bus_->Zero(); |
| 72 EXPECT_EQ(temp_bus_->frames(), | 68 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 73 suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0, | |
| 74 temp_bus_.get())); | |
| 75 EXPECT_TRUE(temp_bus_->AreFramesZero()); | 69 EXPECT_TRUE(temp_bus_->AreFramesZero()); |
| 76 { | 70 { |
| 77 base::RunLoop run_loop; | 71 base::RunLoop run_loop; |
| 78 EXPECT_CALL(*mock_sink_, Pause()) | 72 EXPECT_CALL(*mock_sink_, Pause()) |
| 79 .WillOnce(RunClosure(run_loop.QuitClosure())); | 73 .WillOnce(RunClosure(run_loop.QuitClosure())); |
| 80 run_loop.Run(); | 74 run_loop.Run(); |
| 81 EXPECT_TRUE(suspender_.is_using_fake_sink_for_testing()); | 75 EXPECT_TRUE(suspender_.is_using_fake_sink_for_testing()); |
| 82 } | 76 } |
| 83 | 77 |
| 84 // Unmute the audio, the FakeWorker inside |suspender_| should be running now, | 78 // Unmute the audio, the FakeWorker inside |suspender_| should be running now, |
| 85 // so we don't need to manually invoke Render(). | 79 // so we don't need to manually invoke Render(). |
| 86 fake_callback_.reset(); | 80 fake_callback_.reset(); |
| 87 fake_callback_.set_volume(1); | 81 fake_callback_.set_volume(1); |
| 88 { | 82 { |
| 89 base::RunLoop run_loop; | 83 base::RunLoop run_loop; |
| 90 EXPECT_CALL(*mock_sink_, Play()) | 84 EXPECT_CALL(*mock_sink_, Play()) |
| 91 .WillOnce(RunClosure(run_loop.QuitClosure())); | 85 .WillOnce(RunClosure(run_loop.QuitClosure())); |
| 92 run_loop.Run(); | 86 run_loop.Run(); |
| 93 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing()); | 87 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing()); |
| 94 } | 88 } |
| 95 | 89 |
| 96 // The first Render() after resume should return the first buffer which was | 90 // The first Render() after resume should return the first buffer which was |
| 97 // not silent. | 91 // not silent. |
| 98 fake_callback_.reset(); | 92 fake_callback_.reset(); |
| 99 std::unique_ptr<AudioBus> true_bus = AudioBus::Create(params_); | 93 std::unique_ptr<AudioBus> true_bus = AudioBus::Create(params_); |
| 100 fake_callback_.Render(base::TimeDelta(), base::TimeTicks(), 0, | 94 fake_callback_.Render(true_bus.get(), 0, 0); |
| 101 true_bus.get()); | |
| 102 EXPECT_FALSE(true_bus->AreFramesZero()); | 95 EXPECT_FALSE(true_bus->AreFramesZero()); |
| 103 EXPECT_EQ(temp_bus_->frames(), | 96 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 104 suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0, | |
| 105 temp_bus_.get())); | |
| 106 EXPECT_EQ(memcmp(temp_bus_->channel(0), true_bus->channel(0), | 97 EXPECT_EQ(memcmp(temp_bus_->channel(0), true_bus->channel(0), |
| 107 temp_bus_->frames() * sizeof(float)), | 98 temp_bus_->frames() * sizeof(float)), |
| 108 0); | 99 0); |
| 109 } | 100 } |
| 110 | 101 |
| 111 TEST_F(SilentSinkSuspenderTest, MultipleSuspend) { | 102 TEST_F(SilentSinkSuspenderTest, MultipleSuspend) { |
| 112 // Mute all audio generated by the callback, this should suspend immediately. | 103 // Mute all audio generated by the callback, this should suspend immediately. |
| 113 fake_callback_.set_volume(0); | 104 fake_callback_.set_volume(0); |
| 114 temp_bus_->Zero(); | 105 temp_bus_->Zero(); |
| 115 EXPECT_EQ(temp_bus_->frames(), | 106 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 116 suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0, | |
| 117 temp_bus_.get())); | |
| 118 EXPECT_TRUE(temp_bus_->AreFramesZero()); | 107 EXPECT_TRUE(temp_bus_->AreFramesZero()); |
| 119 | 108 |
| 120 // A second render should only result in a single Pause() call. | 109 // A second render should only result in a single Pause() call. |
| 121 EXPECT_EQ(temp_bus_->frames(), | 110 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 122 suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0, | |
| 123 temp_bus_.get())); | |
| 124 | 111 |
| 125 EXPECT_CALL(*mock_sink_, Pause()); | 112 EXPECT_CALL(*mock_sink_, Pause()); |
| 126 base::RunLoop().RunUntilIdle(); | 113 base::RunLoop().RunUntilIdle(); |
| 127 EXPECT_TRUE(suspender_.is_using_fake_sink_for_testing()); | 114 EXPECT_TRUE(suspender_.is_using_fake_sink_for_testing()); |
| 128 } | 115 } |
| 129 | 116 |
| 130 TEST_F(SilentSinkSuspenderTest, MultipleResume) { | 117 TEST_F(SilentSinkSuspenderTest, MultipleResume) { |
| 131 // Mute all audio generated by the callback, this should suspend immediately. | 118 // Mute all audio generated by the callback, this should suspend immediately. |
| 132 fake_callback_.set_volume(0); | 119 fake_callback_.set_volume(0); |
| 133 temp_bus_->Zero(); | 120 temp_bus_->Zero(); |
| 134 EXPECT_EQ(temp_bus_->frames(), | 121 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 135 suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0, | |
| 136 temp_bus_.get())); | |
| 137 EXPECT_TRUE(temp_bus_->AreFramesZero()); | 122 EXPECT_TRUE(temp_bus_->AreFramesZero()); |
| 138 EXPECT_CALL(*mock_sink_, Pause()); | 123 EXPECT_CALL(*mock_sink_, Pause()); |
| 139 base::RunLoop().RunUntilIdle(); | 124 base::RunLoop().RunUntilIdle(); |
| 140 EXPECT_TRUE(suspender_.is_using_fake_sink_for_testing()); | 125 EXPECT_TRUE(suspender_.is_using_fake_sink_for_testing()); |
| 141 | 126 |
| 142 // Unmute the data. | 127 // Unmute the data. |
| 143 fake_callback_.set_volume(1); | 128 fake_callback_.set_volume(1); |
| 144 | 129 |
| 145 // Prepare our equality testers. | 130 // Prepare our equality testers. |
| 146 fake_callback_.reset(); | 131 fake_callback_.reset(); |
| 147 std::unique_ptr<AudioBus> true_bus1 = AudioBus::Create(params_); | 132 std::unique_ptr<AudioBus> true_bus1 = AudioBus::Create(params_); |
| 148 fake_callback_.Render(base::TimeDelta(), base::TimeTicks(), 0, | 133 fake_callback_.Render(true_bus1.get(), 0, 0); |
| 149 true_bus1.get()); | |
| 150 std::unique_ptr<AudioBus> true_bus2 = AudioBus::Create(params_); | 134 std::unique_ptr<AudioBus> true_bus2 = AudioBus::Create(params_); |
| 151 fake_callback_.Render(base::TimeDelta(), base::TimeTicks(), 0, | 135 fake_callback_.Render(true_bus2.get(), 0, 0); |
| 152 true_bus2.get()); | |
| 153 EXPECT_NE(memcmp(true_bus1->channel(0), true_bus2->channel(0), | 136 EXPECT_NE(memcmp(true_bus1->channel(0), true_bus2->channel(0), |
| 154 true_bus1->frames() * sizeof(float)), | 137 true_bus1->frames() * sizeof(float)), |
| 155 0); | 138 0); |
| 156 | 139 |
| 157 // Reset the fake callback data generation and force two Render() calls before | 140 // Reset the fake callback data generation and force two Render() calls before |
| 158 // the sink can transition. | 141 // the sink can transition. |
| 159 fake_callback_.reset(); | 142 fake_callback_.reset(); |
| 160 EXPECT_EQ( | 143 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(nullptr, 0, 0)); |
| 161 temp_bus_->frames(), | 144 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(nullptr, 0, 0)); |
| 162 suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0, nullptr)); | |
| 163 EXPECT_EQ( | |
| 164 temp_bus_->frames(), | |
| 165 suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0, nullptr)); | |
| 166 EXPECT_CALL(*mock_sink_, Play()); | 145 EXPECT_CALL(*mock_sink_, Play()); |
| 167 base::RunLoop().RunUntilIdle(); | 146 base::RunLoop().RunUntilIdle(); |
| 168 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing()); | 147 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing()); |
| 169 | 148 |
| 170 // Each render after resuming should return one of the non-silent bus. | 149 // Each render after resuming should return one of the non-silent bus. |
| 171 EXPECT_EQ(temp_bus_->frames(), | 150 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 172 suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0, | |
| 173 temp_bus_.get())); | |
| 174 EXPECT_EQ(memcmp(temp_bus_->channel(0), true_bus1->channel(0), | 151 EXPECT_EQ(memcmp(temp_bus_->channel(0), true_bus1->channel(0), |
| 175 temp_bus_->frames() * sizeof(float)), | 152 temp_bus_->frames() * sizeof(float)), |
| 176 0); | 153 0); |
| 177 EXPECT_EQ(temp_bus_->frames(), | 154 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0)); |
| 178 suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0, | |
| 179 temp_bus_.get())); | |
| 180 EXPECT_EQ(memcmp(temp_bus_->channel(0), true_bus2->channel(0), | 155 EXPECT_EQ(memcmp(temp_bus_->channel(0), true_bus2->channel(0), |
| 181 temp_bus_->frames() * sizeof(float)), | 156 temp_bus_->frames() * sizeof(float)), |
| 182 0); | 157 0); |
| 183 } | 158 } |
| 184 | 159 |
| 185 } // namespace content | 160 } // namespace content |
| OLD | NEW |