Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: media/audio/audio_input_controller_unittest.cc

Issue 2941773002: Added initial muted state to stream creation callback, to avoid races. (Closed)
Patch Set: Added mute information to DoCompleteCreation log message. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/audio/audio_input_controller.cc ('k') | media/audio/audio_input_device.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/audio/audio_input_controller.h" 5 #include "media/audio/audio_input_controller.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 static void CloseAudioController(AudioInputController* controller) { 54 static void CloseAudioController(AudioInputController* controller) {
55 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); 55 controller->Close(base::MessageLoop::QuitWhenIdleClosure());
56 base::RunLoop().Run(); 56 base::RunLoop().Run();
57 } 57 }
58 58
59 class MockAudioInputControllerEventHandler 59 class MockAudioInputControllerEventHandler
60 : public AudioInputController::EventHandler { 60 : public AudioInputController::EventHandler {
61 public: 61 public:
62 MockAudioInputControllerEventHandler() {} 62 MockAudioInputControllerEventHandler() {}
63 63
64 MOCK_METHOD1(OnCreated, void(AudioInputController* controller)); 64 MOCK_METHOD2(OnCreated,
65 void(AudioInputController* controller, bool initially_muted));
65 MOCK_METHOD2(OnError, void(AudioInputController* controller, 66 MOCK_METHOD2(OnError, void(AudioInputController* controller,
66 AudioInputController::ErrorCode error_code)); 67 AudioInputController::ErrorCode error_code));
67 MOCK_METHOD2(OnLog, 68 MOCK_METHOD2(OnLog,
68 void(AudioInputController* controller, 69 void(AudioInputController* controller,
69 const std::string& message)); 70 const std::string& message));
70 MOCK_METHOD2(OnMuted, void(AudioInputController* controller, bool is_muted)); 71 MOCK_METHOD2(OnMuted, void(AudioInputController* controller, bool is_muted));
71 72
72 private: 73 private:
73 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler); 74 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler);
74 }; 75 };
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 scoped_refptr<AudioInputController> controller; 130 scoped_refptr<AudioInputController> controller;
130 131
131 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, 132 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout,
132 kSampleRate, kBitsPerSample, kSamplesPerPacket); 133 kSampleRate, kBitsPerSample, kSamplesPerPacket);
133 134
134 SuspendAudioThread(); 135 SuspendAudioThread();
135 controller = AudioInputController::Create( 136 controller = AudioInputController::Create(
136 audio_manager_.get(), &event_handler, &sync_writer, nullptr, params, 137 audio_manager_.get(), &event_handler, &sync_writer, nullptr, params,
137 AudioDeviceDescription::kDefaultDeviceId, false); 138 AudioDeviceDescription::kDefaultDeviceId, false);
138 ASSERT_TRUE(controller.get()); 139 ASSERT_TRUE(controller.get());
139 EXPECT_CALL(event_handler, OnCreated(controller.get())).Times(Exactly(1)); 140 EXPECT_CALL(event_handler, OnCreated(controller.get(), _)).Times(Exactly(1));
140 EXPECT_CALL(event_handler, OnLog(controller.get(), _)).Times(Exactly(3)); 141 EXPECT_CALL(event_handler, OnLog(controller.get(), _)).Times(Exactly(3));
141 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1)); 142 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1));
142 ResumeAudioThread(); 143 ResumeAudioThread();
143 144
144 CloseAudioController(controller.get()); 145 CloseAudioController(controller.get());
145 } 146 }
146 147
147 // Test a normal call sequence of create, record and close. 148 // Test a normal call sequence of create, record and close.
148 TEST_F(AudioInputControllerTest, RecordAndClose) { 149 TEST_F(AudioInputControllerTest, RecordAndClose) {
149 MockAudioInputControllerEventHandler event_handler; 150 MockAudioInputControllerEventHandler event_handler;
150 MockSyncWriter sync_writer; 151 MockSyncWriter sync_writer;
151 int count = 0; 152 int count = 0;
152 153
153 // OnCreated() will be called once. 154 // OnCreated() will be called once.
154 EXPECT_CALL(event_handler, OnCreated(NotNull())) 155 EXPECT_CALL(event_handler, OnCreated(NotNull(), _))
155 .Times(Exactly(1)); 156 .Times(Exactly(1));
156 157
157 // Write() should be called ten times. 158 // Write() should be called ten times.
158 EXPECT_CALL(sync_writer, Write(NotNull(), _, _, _)) 159 EXPECT_CALL(sync_writer, Write(NotNull(), _, _, _))
159 .Times(AtLeast(10)) 160 .Times(AtLeast(10))
160 .WillRepeatedly( 161 .WillRepeatedly(
161 CheckCountAndPostQuitTask(&count, 10, message_loop_.task_runner())); 162 CheckCountAndPostQuitTask(&count, 10, message_loop_.task_runner()));
162 163
163 EXPECT_CALL(event_handler, OnLog(_, _)).Times(AnyNumber()); 164 EXPECT_CALL(event_handler, OnLog(_, _)).Times(AnyNumber());
164 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1)); 165 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1));
(...skipping 14 matching lines...) Expand all
179 CloseAudioController(controller.get()); 180 CloseAudioController(controller.get());
180 } 181 }
181 182
182 // Test that AudioInputController rejects insanely large packet sizes. 183 // Test that AudioInputController rejects insanely large packet sizes.
183 TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) { 184 TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) {
184 // Create an audio device with a very large packet size. 185 // Create an audio device with a very large packet size.
185 MockAudioInputControllerEventHandler event_handler; 186 MockAudioInputControllerEventHandler event_handler;
186 MockSyncWriter sync_writer; 187 MockSyncWriter sync_writer;
187 188
188 // OnCreated() shall not be called in this test. 189 // OnCreated() shall not be called in this test.
189 EXPECT_CALL(event_handler, OnCreated(NotNull())).Times(Exactly(0)); 190 EXPECT_CALL(event_handler, OnCreated(NotNull(), _)).Times(Exactly(0));
190 191
191 AudioParameters params(AudioParameters::AUDIO_FAKE, 192 AudioParameters params(AudioParameters::AUDIO_FAKE,
192 kChannelLayout, 193 kChannelLayout,
193 kSampleRate, 194 kSampleRate,
194 kBitsPerSample, 195 kBitsPerSample,
195 kSamplesPerPacket * 1000); 196 kSamplesPerPacket * 1000);
196 scoped_refptr<AudioInputController> controller = AudioInputController::Create( 197 scoped_refptr<AudioInputController> controller = AudioInputController::Create(
197 audio_manager_.get(), &event_handler, &sync_writer, nullptr, params, 198 audio_manager_.get(), &event_handler, &sync_writer, nullptr, params,
198 AudioDeviceDescription::kDefaultDeviceId, false); 199 AudioDeviceDescription::kDefaultDeviceId, false);
199 ASSERT_FALSE(controller.get()); 200 ASSERT_FALSE(controller.get());
200 } 201 }
201 202
202 // Test calling AudioInputController::Close multiple times. 203 // Test calling AudioInputController::Close multiple times.
203 TEST_F(AudioInputControllerTest, CloseTwice) { 204 TEST_F(AudioInputControllerTest, CloseTwice) {
204 MockAudioInputControllerEventHandler event_handler; 205 MockAudioInputControllerEventHandler event_handler;
205 MockSyncWriter sync_writer; 206 MockSyncWriter sync_writer;
206 207
207 // OnCreated() will be called only once. 208 // OnCreated() will be called only once.
208 EXPECT_CALL(event_handler, OnCreated(NotNull())).Times(Exactly(1)); 209 EXPECT_CALL(event_handler, OnCreated(NotNull(), _)).Times(Exactly(1));
209 EXPECT_CALL(event_handler, OnLog(_, _)).Times(AnyNumber()); 210 EXPECT_CALL(event_handler, OnLog(_, _)).Times(AnyNumber());
210 // This callback should still only be called once. 211 // This callback should still only be called once.
211 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1)); 212 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1));
212 213
213 AudioParameters params(AudioParameters::AUDIO_FAKE, 214 AudioParameters params(AudioParameters::AUDIO_FAKE,
214 kChannelLayout, 215 kChannelLayout,
215 kSampleRate, 216 kSampleRate,
216 kBitsPerSample, 217 kBitsPerSample,
217 kSamplesPerPacket); 218 kSamplesPerPacket);
218 scoped_refptr<AudioInputController> controller = AudioInputController::Create( 219 scoped_refptr<AudioInputController> controller = AudioInputController::Create(
(...skipping 26 matching lines...) Expand all
245 scoped_refptr<AudioInputController> controller; 246 scoped_refptr<AudioInputController> controller;
246 WaitableEvent callback_event(WaitableEvent::ResetPolicy::AUTOMATIC, 247 WaitableEvent callback_event(WaitableEvent::ResetPolicy::AUTOMATIC,
247 WaitableEvent::InitialState::NOT_SIGNALED); 248 WaitableEvent::InitialState::NOT_SIGNALED);
248 249
249 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, 250 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout,
250 kSampleRate, kBitsPerSample, kSamplesPerPacket); 251 kSampleRate, kBitsPerSample, kSamplesPerPacket);
251 252
252 base::RunLoop unmute_run_loop; 253 base::RunLoop unmute_run_loop;
253 base::RunLoop mute_run_loop; 254 base::RunLoop mute_run_loop;
254 base::RunLoop setup_run_loop; 255 base::RunLoop setup_run_loop;
255 EXPECT_CALL(event_handler, OnCreated(_)).Times(Exactly(1)); 256 EXPECT_CALL(event_handler, OnCreated(_, false))
257 .WillOnce(InvokeWithoutArgs([&] { setup_run_loop.QuitWhenIdle(); }));
256 EXPECT_CALL(event_handler, OnLog(_, _)).Times(Exactly(3)); 258 EXPECT_CALL(event_handler, OnLog(_, _)).Times(Exactly(3));
257 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1)); 259 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1));
258 EXPECT_CALL(event_handler, OnMuted(_, true)) 260 EXPECT_CALL(event_handler, OnMuted(_, true))
259 .WillOnce(InvokeWithoutArgs([&] { mute_run_loop.Quit(); })); 261 .WillOnce(InvokeWithoutArgs([&] { mute_run_loop.Quit(); }));
260 EXPECT_CALL(event_handler, OnMuted(_, false)) 262 EXPECT_CALL(event_handler, OnMuted(_, false))
261 .WillOnce(InvokeWithoutArgs([&] { unmute_run_loop.Quit(); })); 263 .WillOnce(InvokeWithoutArgs([&] { unmute_run_loop.Quit(); }));
262 264
263 FakeAudioInputStream::SetGlobalMutedState(false); 265 FakeAudioInputStream::SetGlobalMutedState(false);
264 controller = AudioInputController::Create( 266 controller = AudioInputController::Create(
265 audio_manager_.get(), &event_handler, &sync_writer, nullptr, params, 267 audio_manager_.get(), &event_handler, &sync_writer, nullptr, params,
266 AudioDeviceDescription::kDefaultDeviceId, false); 268 AudioDeviceDescription::kDefaultDeviceId, false);
267 ASSERT_TRUE(controller.get()); 269 ASSERT_TRUE(controller.get());
268 setup_run_loop.RunUntilIdle(); 270 RunLoopWithTimeout(&setup_run_loop, timeout);
269 271
270 FakeAudioInputStream::SetGlobalMutedState(true); 272 FakeAudioInputStream::SetGlobalMutedState(true);
271 RunLoopWithTimeout(&mute_run_loop, timeout); 273 RunLoopWithTimeout(&mute_run_loop, timeout);
272 FakeAudioInputStream::SetGlobalMutedState(false); 274 FakeAudioInputStream::SetGlobalMutedState(false);
273 RunLoopWithTimeout(&unmute_run_loop, timeout); 275 RunLoopWithTimeout(&unmute_run_loop, timeout);
274 276
275 CloseAudioController(controller.get()); 277 CloseAudioController(controller.get());
276 } 278 }
277 279
278 TEST_F(AudioInputControllerTest, TestOnmutedCallbackInitiallyMuted) { 280 TEST_F(AudioInputControllerTest, TestOnmutedCallbackInitiallyMuted) {
279 const auto timeout = base::TimeDelta::FromMilliseconds(kOnMuteWaitTimeoutMs); 281 const auto timeout = base::TimeDelta::FromMilliseconds(kOnMuteWaitTimeoutMs);
280 MockAudioInputControllerEventHandler event_handler; 282 MockAudioInputControllerEventHandler event_handler;
281 MockSyncWriter sync_writer; 283 MockSyncWriter sync_writer;
282 scoped_refptr<AudioInputController> controller; 284 scoped_refptr<AudioInputController> controller;
283 WaitableEvent callback_event(WaitableEvent::ResetPolicy::AUTOMATIC, 285 WaitableEvent callback_event(WaitableEvent::ResetPolicy::AUTOMATIC,
284 WaitableEvent::InitialState::NOT_SIGNALED); 286 WaitableEvent::InitialState::NOT_SIGNALED);
285 287
286 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, 288 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout,
287 kSampleRate, kBitsPerSample, kSamplesPerPacket); 289 kSampleRate, kBitsPerSample, kSamplesPerPacket);
288 290
289 base::RunLoop unmute_run_loop; 291 base::RunLoop unmute_run_loop;
290 base::RunLoop setup_run_loop; 292 base::RunLoop setup_run_loop;
291 EXPECT_CALL(event_handler, OnCreated(_)).Times(Exactly(1)); 293 EXPECT_CALL(event_handler, OnCreated(_, true))
294 .WillOnce(InvokeWithoutArgs([&] { setup_run_loop.QuitWhenIdle(); }));
292 EXPECT_CALL(event_handler, OnLog(_, _)).Times(Exactly(3)); 295 EXPECT_CALL(event_handler, OnLog(_, _)).Times(Exactly(3));
293 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1)); 296 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1));
294 EXPECT_CALL(event_handler, OnMuted(_, true))
295 .WillOnce(InvokeWithoutArgs([&] { setup_run_loop.QuitWhenIdle(); }));
296 EXPECT_CALL(event_handler, OnMuted(_, false)) 297 EXPECT_CALL(event_handler, OnMuted(_, false))
297 .WillOnce(InvokeWithoutArgs([&] { unmute_run_loop.Quit(); })); 298 .WillOnce(InvokeWithoutArgs([&] { unmute_run_loop.Quit(); }));
298 299
299 FakeAudioInputStream::SetGlobalMutedState(true); 300 FakeAudioInputStream::SetGlobalMutedState(true);
300 controller = AudioInputController::Create( 301 controller = AudioInputController::Create(
301 audio_manager_.get(), &event_handler, &sync_writer, nullptr, params, 302 audio_manager_.get(), &event_handler, &sync_writer, nullptr, params,
302 AudioDeviceDescription::kDefaultDeviceId, false); 303 AudioDeviceDescription::kDefaultDeviceId, false);
303 ASSERT_TRUE(controller.get()); 304 ASSERT_TRUE(controller.get());
304 RunLoopWithTimeout(&setup_run_loop, timeout); 305 RunLoopWithTimeout(&setup_run_loop, timeout);
305 306
306 FakeAudioInputStream::SetGlobalMutedState(false); 307 FakeAudioInputStream::SetGlobalMutedState(false);
307 RunLoopWithTimeout(&unmute_run_loop, timeout); 308 RunLoopWithTimeout(&unmute_run_loop, timeout);
308 309
309 CloseAudioController(controller.get()); 310 CloseAudioController(controller.get());
310 } 311 }
311 312
312 } // namespace media 313 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/audio_input_controller.cc ('k') | media/audio/audio_input_device.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698