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