silent_sink_suspender_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #include "media/base/silent_sink_suspender.h"
  5. #include "base/run_loop.h"
  6. #include "base/test/gmock_callback_support.h"
  7. #include "base/test/test_message_loop.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. using base::test::RunClosure;
  13. namespace media {
  14. class SilentSinkSuspenderTest : public testing::Test {
  15. public:
  16. SilentSinkSuspenderTest()
  17. : params_(AudioParameters::AUDIO_FAKE, CHANNEL_LAYOUT_MONO, 44100, 128),
  18. mock_sink_(new testing::StrictMock<MockAudioRendererSink>()),
  19. fake_callback_(0.1, params_.sample_rate()),
  20. temp_bus_(AudioBus::Create(params_)),
  21. // Set a negative timeout so any silence will suspend immediately.
  22. suspender_(&fake_callback_,
  23. base::Seconds(-1),
  24. params_,
  25. mock_sink_,
  26. test_loop_.task_runner()) {}
  27. SilentSinkSuspenderTest(const SilentSinkSuspenderTest&) = delete;
  28. SilentSinkSuspenderTest& operator=(const SilentSinkSuspenderTest&) = delete;
  29. ~SilentSinkSuspenderTest() override = default;
  30. protected:
  31. base::TestMessageLoop test_loop_;
  32. const AudioParameters params_;
  33. scoped_refptr<testing::StrictMock<MockAudioRendererSink>> mock_sink_;
  34. FakeAudioRenderCallback fake_callback_;
  35. std::unique_ptr<AudioBus> temp_bus_;
  36. SilentSinkSuspender suspender_;
  37. };
  38. TEST_F(SilentSinkSuspenderTest, BasicPassthough) {
  39. temp_bus_->Zero();
  40. auto delay = base::Milliseconds(20);
  41. EXPECT_EQ(temp_bus_->frames(),
  42. suspender_.Render(delay, base::TimeTicks(), 0, temp_bus_.get()));
  43. // Delay should remain.
  44. EXPECT_EQ(delay, fake_callback_.last_delay());
  45. EXPECT_FALSE(temp_bus_->AreFramesZero());
  46. }
  47. TEST_F(SilentSinkSuspenderTest, SuspendResumeTriggered) {
  48. // Verify a normal Render() doesn't invoke suspend.
  49. EXPECT_FALSE(suspender_.IsUsingFakeSinkForTesting());
  50. temp_bus_->Zero();
  51. EXPECT_EQ(temp_bus_->frames(),
  52. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  53. temp_bus_.get()));
  54. EXPECT_FALSE(temp_bus_->AreFramesZero());
  55. base::RunLoop().RunUntilIdle();
  56. EXPECT_FALSE(suspender_.IsUsingFakeSinkForTesting());
  57. // Mute all audio generated by the callback, this should suspend immediately.
  58. fake_callback_.set_volume(0);
  59. temp_bus_->Zero();
  60. EXPECT_EQ(temp_bus_->frames(),
  61. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  62. temp_bus_.get()));
  63. EXPECT_TRUE(temp_bus_->AreFramesZero());
  64. {
  65. base::RunLoop run_loop;
  66. EXPECT_CALL(*mock_sink_, Pause())
  67. .WillOnce(RunClosure(run_loop.QuitClosure()));
  68. run_loop.Run();
  69. EXPECT_TRUE(suspender_.IsUsingFakeSinkForTesting());
  70. }
  71. // Unmute the audio, the FakeWorker inside |suspender_| should be running now,
  72. // so we don't need to manually invoke Render().
  73. fake_callback_.reset();
  74. fake_callback_.set_volume(1);
  75. {
  76. base::RunLoop run_loop;
  77. EXPECT_CALL(*mock_sink_, Play())
  78. .WillOnce(RunClosure(run_loop.QuitClosure()));
  79. run_loop.Run();
  80. EXPECT_FALSE(suspender_.IsUsingFakeSinkForTesting());
  81. }
  82. // The first Render() after resume should return the first buffer which was
  83. // not silent.
  84. fake_callback_.reset();
  85. std::unique_ptr<AudioBus> true_bus = AudioBus::Create(params_);
  86. fake_callback_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  87. true_bus.get());
  88. EXPECT_FALSE(true_bus->AreFramesZero());
  89. EXPECT_EQ(temp_bus_->frames(),
  90. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  91. temp_bus_.get()));
  92. EXPECT_EQ(memcmp(temp_bus_->channel(0), true_bus->channel(0),
  93. temp_bus_->frames() * sizeof(float)),
  94. 0);
  95. }
  96. TEST_F(SilentSinkSuspenderTest, MultipleSuspend) {
  97. // Mute all audio generated by the callback, this should suspend immediately.
  98. fake_callback_.set_volume(0);
  99. temp_bus_->Zero();
  100. EXPECT_EQ(temp_bus_->frames(),
  101. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  102. temp_bus_.get()));
  103. EXPECT_TRUE(temp_bus_->AreFramesZero());
  104. // A second render should only result in a single Pause() call.
  105. EXPECT_EQ(temp_bus_->frames(),
  106. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  107. temp_bus_.get()));
  108. EXPECT_CALL(*mock_sink_, Pause());
  109. base::RunLoop().RunUntilIdle();
  110. EXPECT_TRUE(suspender_.IsUsingFakeSinkForTesting());
  111. }
  112. TEST_F(SilentSinkSuspenderTest, MultipleResume) {
  113. // Mute all audio generated by the callback, this should suspend immediately.
  114. fake_callback_.set_volume(0);
  115. temp_bus_->Zero();
  116. EXPECT_EQ(temp_bus_->frames(),
  117. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  118. temp_bus_.get()));
  119. EXPECT_TRUE(temp_bus_->AreFramesZero());
  120. EXPECT_CALL(*mock_sink_, Pause());
  121. base::RunLoop().RunUntilIdle();
  122. EXPECT_TRUE(suspender_.IsUsingFakeSinkForTesting());
  123. // Unmute the data.
  124. fake_callback_.set_volume(1);
  125. // Prepare our equality testers.
  126. fake_callback_.reset();
  127. std::unique_ptr<AudioBus> true_bus1 = AudioBus::Create(params_);
  128. fake_callback_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  129. true_bus1.get());
  130. std::unique_ptr<AudioBus> true_bus2 = AudioBus::Create(params_);
  131. fake_callback_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  132. true_bus2.get());
  133. EXPECT_NE(memcmp(true_bus1->channel(0), true_bus2->channel(0),
  134. true_bus1->frames() * sizeof(float)),
  135. 0);
  136. // Reset the fake callback data generation and force two Render() calls before
  137. // the sink can transition.
  138. fake_callback_.reset();
  139. EXPECT_EQ(
  140. temp_bus_->frames(),
  141. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0, nullptr));
  142. EXPECT_EQ(
  143. temp_bus_->frames(),
  144. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0, nullptr));
  145. EXPECT_CALL(*mock_sink_, Play());
  146. base::RunLoop().RunUntilIdle();
  147. EXPECT_FALSE(suspender_.IsUsingFakeSinkForTesting());
  148. // Each render after resuming should return one of the non-silent bus.
  149. EXPECT_EQ(temp_bus_->frames(),
  150. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  151. temp_bus_.get()));
  152. EXPECT_EQ(memcmp(temp_bus_->channel(0), true_bus1->channel(0),
  153. temp_bus_->frames() * sizeof(float)),
  154. 0);
  155. EXPECT_EQ(temp_bus_->frames(),
  156. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  157. temp_bus_.get()));
  158. EXPECT_EQ(memcmp(temp_bus_->channel(0), true_bus2->channel(0),
  159. temp_bus_->frames() * sizeof(float)),
  160. 0);
  161. }
  162. TEST_F(SilentSinkSuspenderTest, SetDetectSilence) {
  163. // Verify a normal Render() doesn't invoke suspend.
  164. EXPECT_FALSE(suspender_.IsUsingFakeSinkForTesting());
  165. temp_bus_->Zero();
  166. EXPECT_EQ(temp_bus_->frames(),
  167. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  168. temp_bus_.get()));
  169. EXPECT_FALSE(temp_bus_->AreFramesZero());
  170. base::RunLoop().RunUntilIdle();
  171. EXPECT_FALSE(suspender_.IsUsingFakeSinkForTesting());
  172. // Mute all audio generated by the callback, this should suspend immediately.
  173. fake_callback_.set_volume(0);
  174. temp_bus_->Zero();
  175. EXPECT_EQ(temp_bus_->frames(),
  176. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  177. temp_bus_.get()));
  178. EXPECT_TRUE(temp_bus_->AreFramesZero());
  179. {
  180. base::RunLoop run_loop;
  181. EXPECT_CALL(*mock_sink_, Pause())
  182. .WillOnce(RunClosure(run_loop.QuitClosure()));
  183. run_loop.Run();
  184. EXPECT_TRUE(suspender_.IsUsingFakeSinkForTesting());
  185. }
  186. // Disable silence detection, but don't unmute the audio, the FakeWorker
  187. // inside |suspender_| should be running now, so we don't need to manually
  188. // invoke Render().
  189. {
  190. base::RunLoop run_loop;
  191. EXPECT_CALL(*mock_sink_, Play())
  192. .WillOnce(RunClosure(run_loop.QuitClosure()));
  193. // Disable silence detection which should put us back on the real sink.
  194. suspender_.SetDetectSilence(false);
  195. run_loop.Run();
  196. EXPECT_FALSE(suspender_.IsUsingFakeSinkForTesting());
  197. }
  198. // Run one more Render() to ensure we stay on the real sink even w/ silent
  199. // audio.
  200. temp_bus_->Zero();
  201. EXPECT_EQ(temp_bus_->frames(),
  202. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  203. temp_bus_.get()));
  204. base::RunLoop().RunUntilIdle();
  205. EXPECT_FALSE(suspender_.IsUsingFakeSinkForTesting());
  206. // Enable silence detection which should transition to the fake sink on the
  207. // next Render().
  208. suspender_.SetDetectSilence(true);
  209. temp_bus_->Zero();
  210. EXPECT_EQ(temp_bus_->frames(),
  211. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  212. temp_bus_.get()));
  213. EXPECT_TRUE(temp_bus_->AreFramesZero());
  214. {
  215. base::RunLoop run_loop;
  216. EXPECT_CALL(*mock_sink_, Pause())
  217. .WillOnce(RunClosure(run_loop.QuitClosure()));
  218. run_loop.Run();
  219. EXPECT_TRUE(suspender_.IsUsingFakeSinkForTesting());
  220. }
  221. }
  222. TEST_F(SilentSinkSuspenderTest, OnPaused) {
  223. // Verify a normal Render() doesn't invoke suspend.
  224. EXPECT_FALSE(suspender_.IsUsingFakeSinkForTesting());
  225. temp_bus_->Zero();
  226. EXPECT_EQ(temp_bus_->frames(),
  227. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  228. temp_bus_.get()));
  229. EXPECT_FALSE(temp_bus_->AreFramesZero());
  230. base::RunLoop().RunUntilIdle();
  231. EXPECT_FALSE(suspender_.IsUsingFakeSinkForTesting());
  232. // OnPaused() at this point should do nothing, since we're on the real sink.
  233. suspender_.OnPaused();
  234. // Mute all audio generated by the callback, this should suspend immediately.
  235. fake_callback_.set_volume(0);
  236. temp_bus_->Zero();
  237. EXPECT_EQ(temp_bus_->frames(),
  238. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  239. temp_bus_.get()));
  240. EXPECT_TRUE(temp_bus_->AreFramesZero());
  241. {
  242. base::RunLoop run_loop;
  243. EXPECT_CALL(*mock_sink_, Pause())
  244. .WillOnce(RunClosure(run_loop.QuitClosure()));
  245. run_loop.Run();
  246. EXPECT_TRUE(suspender_.IsUsingFakeSinkForTesting());
  247. }
  248. // Call OnPaused(), which should disable the fake sink, but won't call Play().
  249. suspender_.OnPaused();
  250. base::RunLoop().RunUntilIdle();
  251. EXPECT_FALSE(suspender_.IsUsingFakeSinkForTesting());
  252. // Render silence again, which should attempt to transition to the fake sink.
  253. temp_bus_->Zero();
  254. EXPECT_EQ(temp_bus_->frames(),
  255. suspender_.Render(base::TimeDelta(), base::TimeTicks(), 0,
  256. temp_bus_.get()));
  257. // OnPaused() should cancel any pending transitions.
  258. suspender_.OnPaused();
  259. // We should not transition to the fake sink now.
  260. base::RunLoop().RunUntilIdle();
  261. EXPECT_FALSE(suspender_.IsUsingFakeSinkForTesting());
  262. }
  263. } // namespace media