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

Side by Side Diff: content/browser/renderer_host/media/audio_renderer_host_unittest.cc

Issue 11413078: Tab Audio Capture: Browser-side connect/disconnect functionality. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove WCAudioInputStream changes (split into another change). Created 8 years 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 | Annotate | Revision Log
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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/environment.h" 6 #include "base/environment.h"
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/process_util.h" 9 #include "base/process_util.h"
10 #include "base/sync_socket.h" 10 #include "base/sync_socket.h"
11 #include "content/browser/browser_thread_impl.h" 11 #include "content/browser/browser_thread_impl.h"
12 #include "content/browser/renderer_host/media/audio_renderer_host.h" 12 #include "content/browser/renderer_host/media/audio_renderer_host.h"
13 #include "content/browser/renderer_host/media/mock_media_observer.h" 13 #include "content/browser/renderer_host/media/mock_media_observer.h"
14 #include "content/common/media/audio_messages.h" 14 #include "content/common/media/audio_messages.h"
15 #include "ipc/ipc_message_utils.h" 15 #include "ipc/ipc_message_utils.h"
16 #include "media/audio/audio_manager.h" 16 #include "media/audio/audio_manager.h"
17 #include "media/audio/fake_audio_output_stream.h" 17 #include "media/audio/fake_audio_output_stream.h"
18 #include "net/url_request/url_request_context.h" 18 #include "net/url_request/url_request_context.h"
19 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 using ::testing::_; 22 using ::testing::_;
23 using ::testing::AtLeast; 23 using ::testing::AtLeast;
24 using ::testing::DoAll; 24 using ::testing::DoAll;
25 using ::testing::InSequence; 25 using ::testing::InSequence;
26 using ::testing::InvokeWithoutArgs; 26 using ::testing::InvokeWithoutArgs;
27 using ::testing::NotNull;
27 using ::testing::Return; 28 using ::testing::Return;
28 using ::testing::SaveArg; 29 using ::testing::SaveArg;
29 using ::testing::SetArgumentPointee; 30 using ::testing::SetArgumentPointee;
30 31
31 namespace content { 32 namespace content {
32 33
34 static const int kRenderProcessId = 1;
35 static const int kRenderViewId = 4;
33 static const int kStreamId = 50; 36 static const int kStreamId = 50;
34 37
35 static bool IsRunningHeadless() { 38 static bool IsRunningHeadless() {
36 scoped_ptr<base::Environment> env(base::Environment::Create()); 39 scoped_ptr<base::Environment> env(base::Environment::Create());
37 if (env->HasVar("CHROME_HEADLESS")) 40 if (env->HasVar("CHROME_HEADLESS"))
38 return true; 41 return true;
39 return false; 42 return false;
40 } 43 }
41 44
42 class MockAudioRendererHost : public AudioRendererHost { 45 class MockAudioRendererHost : public AudioRendererHost {
43 public: 46 public:
44 explicit MockAudioRendererHost( 47 explicit MockAudioRendererHost(
45 media::AudioManager* audio_manager, 48 media::AudioManager* audio_manager,
46 MediaObserver* media_observer) 49 MediaObserver* media_observer)
47 : AudioRendererHost(audio_manager, media_observer), 50 : AudioRendererHost(kRenderProcessId, audio_manager, media_observer),
48 shared_memory_length_(0) { 51 shared_memory_length_(0) {
49 } 52 }
50 53
51 // A list of mock methods. 54 // A list of mock methods.
52 MOCK_METHOD2(OnStreamCreated, 55 MOCK_METHOD2(OnStreamCreated,
53 void(int stream_id, int length)); 56 void(int stream_id, int length));
54 MOCK_METHOD1(OnStreamPlaying, void(int stream_id)); 57 MOCK_METHOD1(OnStreamPlaying, void(int stream_id));
55 MOCK_METHOD1(OnStreamPaused, void(int stream_id)); 58 MOCK_METHOD1(OnStreamPaused, void(int stream_id));
56 MOCK_METHOD1(OnStreamError, void(int stream_id)); 59 MOCK_METHOD1(OnStreamError, void(int stream_id));
57 MOCK_METHOD2(OnStreamVolume, void(int stream_id, double volume)); 60 MOCK_METHOD2(OnStreamVolume, void(int stream_id, double volume));
58 61
59 private: 62 private:
60 virtual ~MockAudioRendererHost() { 63 virtual ~MockAudioRendererHost() {
61 // Make sure all audio streams have been deleted. 64 // Make sure all audio streams have been deleted.
62 EXPECT_TRUE(audio_entries_.empty()); 65 EXPECT_TRUE(audio_entries_.empty());
66 // Make sure no mirroring sessions are active.
67 EXPECT_TRUE(mirror_sessions_.empty());
63 } 68 }
64 69
65 // This method is used to dispatch IPC messages to the renderer. We intercept 70 // This method is used to dispatch IPC messages to the renderer. We intercept
66 // these messages here and dispatch to our mock methods to verify the 71 // these messages here and dispatch to our mock methods to verify the
67 // conversation between this object and the renderer. 72 // conversation between this object and the renderer.
68 virtual bool Send(IPC::Message* message) { 73 virtual bool Send(IPC::Message* message) {
69 CHECK(message); 74 CHECK(message);
70 75
71 // In this method we dispatch the messages to the according handlers as if 76 // In this method we dispatch the messages to the according handlers as if
72 // we are the renderer. 77 // we are the renderer.
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 } 134 }
130 } 135 }
131 136
132 scoped_ptr<base::SharedMemory> shared_memory_; 137 scoped_ptr<base::SharedMemory> shared_memory_;
133 scoped_ptr<base::SyncSocket> sync_socket_; 138 scoped_ptr<base::SyncSocket> sync_socket_;
134 uint32 shared_memory_length_; 139 uint32 shared_memory_length_;
135 140
136 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); 141 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost);
137 }; 142 };
138 143
144 class MockMirroringDestination
145 : public AudioRendererHost::MirroringDestination {
146 public:
147 MockMirroringDestination() {}
148
149 MOCK_METHOD2(AddInput,
150 void(media::AudioOutputStream::AudioSourceCallback* callback,
151 const media::AudioParameters& params));
152 MOCK_METHOD1(RemoveInput,
153 void(media::AudioOutputStream::AudioSourceCallback* callback));
154
155 private:
156 virtual ~MockMirroringDestination() {}
157
158 DISALLOW_COPY_AND_ASSIGN(MockMirroringDestination);
159 };
160
139 ACTION_P(QuitMessageLoop, message_loop) { 161 ACTION_P(QuitMessageLoop, message_loop) {
140 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 162 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure());
141 } 163 }
142 164
143 class AudioRendererHostTest : public testing::Test { 165 class AudioRendererHostTest : public testing::Test {
144 public: 166 public:
145 AudioRendererHostTest() 167 AudioRendererHostTest()
146 : mock_stream_(true) { 168 : mock_stream_(true) {
147 } 169 }
148 170
149 protected: 171 protected:
150 virtual void SetUp() { 172 virtual void SetUp() {
151 // Create a message loop so AudioRendererHost can use it. 173 // Create a message loop so AudioRendererHost can use it.
152 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); 174 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO));
153 175
154 // Claim to be on both the UI and IO threads to pass all the DCHECKS. 176 // Claim to be on both the UI and IO threads to pass all the DCHECKS.
155 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, 177 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO,
156 message_loop_.get())); 178 message_loop_.get()));
157 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI, 179 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI,
158 message_loop_.get())); 180 message_loop_.get()));
159 audio_manager_.reset(media::AudioManager::Create()); 181 audio_manager_.reset(media::AudioManager::Create());
160 observer_.reset(new MockMediaObserver()); 182 observer_.reset(new MockMediaObserver());
161 host_ = new MockAudioRendererHost(audio_manager_.get(), observer_.get()); 183 host_ = new MockAudioRendererHost(audio_manager_.get(), observer_.get());
162 184 destination_ = new MockMirroringDestination();
163 // Expect the audio stream will be deleted. 185 another_destination_ = new MockMirroringDestination();
164 EXPECT_CALL(*observer_, OnDeleteAudioStream(_, kStreamId));
165 186
166 // Simulate IPC channel connected. 187 // Simulate IPC channel connected.
167 host_->OnChannelConnected(base::GetCurrentProcId()); 188 host_->OnChannelConnected(base::GetCurrentProcId());
189
190 if (!IsRunningHeadless())
191 EnableRealDevice();
168 } 192 }
169 193
170 virtual void TearDown() { 194 virtual void TearDown() {
171 // Simulate closing the IPC channel. 195 // Simulate closing the IPC channel.
172 host_->OnChannelClosing(); 196 host_->OnChannelClosing();
173 197
174 // Release the reference to the mock object. The object will be destructed 198 // Release the reference to the mock objects. The MockAudioRendererHost
175 // on message_loop_. 199 // will be destroyed on message_loop_.
200 another_destination_ = NULL;
201 destination_ = NULL;
176 host_ = NULL; 202 host_ = NULL;
177 203
178 // We need to continue running message_loop_ to complete all destructions. 204 // We need to continue running message_loop_ to complete all destructions.
179 SyncWithAudioThread(); 205 SyncWithAudioThread();
180 206
181 audio_manager_.reset(); 207 audio_manager_.reset();
182 208
183 io_thread_.reset(); 209 io_thread_.reset();
184 ui_thread_.reset(); 210 ui_thread_.reset();
185 } 211 }
(...skipping 15 matching lines...) Expand all
201 227
202 media::AudioParameters params( 228 media::AudioParameters params(
203 format, media::CHANNEL_LAYOUT_STEREO, 229 format, media::CHANNEL_LAYOUT_STEREO,
204 media::AudioParameters::kAudioCDSampleRate, 16, 230 media::AudioParameters::kAudioCDSampleRate, 16,
205 media::AudioParameters::kAudioCDSampleRate / 10); 231 media::AudioParameters::kAudioCDSampleRate / 10);
206 232
207 // Send a create stream message to the audio output stream and wait until 233 // Send a create stream message to the audio output stream and wait until
208 // we receive the created message. 234 // we receive the created message.
209 host_->OnCreateStream(kStreamId, params, 0); 235 host_->OnCreateStream(kStreamId, params, 0);
210 message_loop_->Run(); 236 message_loop_->Run();
237
238 // Simulate the renderer process associating a stream with a render view.
239 host_->OnAssociateStreamWithProducer(kStreamId, kRenderViewId);
240 message_loop_->RunUntilIdle();
241
242 // Expect the audio stream will be deleted at some later point.
243 EXPECT_CALL(*observer_, OnDeleteAudioStream(_, kStreamId));
211 } 244 }
212 245
213 void Close() { 246 void Close() {
214 EXPECT_CALL(*observer_, 247 EXPECT_CALL(*observer_,
215 OnSetAudioStreamStatus(_, kStreamId, "closed")); 248 OnSetAudioStreamStatus(_, kStreamId, "closed"));
216 249
217 // Send a message to AudioRendererHost to tell it we want to close the 250 // Send a message to AudioRendererHost to tell it we want to close the
218 // stream. 251 // stream.
219 host_->OnCloseStream(kStreamId); 252 host_->OnCloseStream(kStreamId);
220 message_loop_->RunUntilIdle(); 253 message_loop_->RunUntilIdle();
(...skipping 20 matching lines...) Expand all
241 } 274 }
242 275
243 void SetVolume(double volume) { 276 void SetVolume(double volume) {
244 EXPECT_CALL(*observer_, 277 EXPECT_CALL(*observer_,
245 OnSetAudioStreamVolume(_, kStreamId, volume)); 278 OnSetAudioStreamVolume(_, kStreamId, volume));
246 279
247 host_->OnSetVolume(kStreamId, volume); 280 host_->OnSetVolume(kStreamId, volume);
248 message_loop_->RunUntilIdle(); 281 message_loop_->RunUntilIdle();
249 } 282 }
250 283
284 void StartMirroringTo(MockMirroringDestination* dest,
285 int expected_inputs_seen) {
286 EXPECT_CALL(*dest, AddInput(NotNull(), _)).Times(expected_inputs_seen);
287 EXPECT_CALL(*dest, RemoveInput(NotNull())).Times(expected_inputs_seen);
288
289 AudioRendererHost::StartMirroring(kRenderProcessId, kRenderViewId, dest);
290 message_loop_->RunUntilIdle();
291 }
292
293 void StopMirroringTo(MockMirroringDestination* dest) {
294 AudioRendererHost::StopMirroring(kRenderProcessId, kRenderViewId, dest);
295 message_loop_->RunUntilIdle();
296 }
297
251 void SimulateError() { 298 void SimulateError() {
252 EXPECT_CALL(*observer_, 299 EXPECT_CALL(*observer_,
253 OnSetAudioStreamStatus(_, kStreamId, "error")); 300 OnSetAudioStreamStatus(_, kStreamId, "error"));
254 // Find the first AudioOutputController in the AudioRendererHost. 301 // Find the first AudioOutputController in the AudioRendererHost.
255 CHECK(host_->audio_entries_.size()) 302 CHECK(host_->audio_entries_.size())
256 << "Calls Create() before calling this method"; 303 << "Calls Create() before calling this method";
257 media::AudioOutputController* controller = 304 media::AudioOutputController* controller =
258 host_->LookupControllerByIdForTesting(kStreamId); 305 host_->LookupControllerByIdForTesting(kStreamId);
259 CHECK(controller) << "AudioOutputController not found"; 306 CHECK(controller) << "AudioOutputController not found";
260 307
(...skipping 29 matching lines...) Expand all
290 // to the thread that itself owns. 337 // to the thread that itself owns.
291 message_loop_->PostTask( 338 message_loop_->PostTask(
292 FROM_HERE, base::Bind(&PostQuitOnAudioThread, 339 FROM_HERE, base::Bind(&PostQuitOnAudioThread,
293 base::Unretained(audio_manager_.get()), 340 base::Unretained(audio_manager_.get()),
294 message_loop_.get())); 341 message_loop_.get()));
295 message_loop_->Run(); 342 message_loop_->Run();
296 } 343 }
297 344
298 void EnableRealDevice() { mock_stream_ = false; } 345 void EnableRealDevice() { mock_stream_ = false; }
299 346
347 protected:
348 // Accessors for testing mirroring to one or two destinations.
349 MockMirroringDestination* destination() const {
350 return destination_;
351 }
352 MockMirroringDestination* another_destination() const {
353 return another_destination_;
354 }
355
300 private: 356 private:
301 bool mock_stream_; 357 bool mock_stream_;
302 scoped_ptr<MockMediaObserver> observer_; 358 scoped_ptr<MockMediaObserver> observer_;
303 scoped_refptr<MockAudioRendererHost> host_; 359 scoped_refptr<MockAudioRendererHost> host_;
360 scoped_refptr<MockMirroringDestination> destination_;
361 scoped_refptr<MockMirroringDestination> another_destination_;
304 scoped_ptr<MessageLoop> message_loop_; 362 scoped_ptr<MessageLoop> message_loop_;
305 scoped_ptr<BrowserThreadImpl> io_thread_; 363 scoped_ptr<BrowserThreadImpl> io_thread_;
306 scoped_ptr<BrowserThreadImpl> ui_thread_; 364 scoped_ptr<BrowserThreadImpl> ui_thread_;
307 scoped_ptr<media::AudioManager> audio_manager_; 365 scoped_ptr<media::AudioManager> audio_manager_;
308 366
309 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest); 367 DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest);
310 }; 368 };
311 369
312 TEST_F(AudioRendererHostTest, CreateAndClose) { 370 TEST_F(AudioRendererHostTest, CreateAndClose) {
313 if (!IsRunningHeadless())
314 EnableRealDevice();
315
316 Create(); 371 Create();
317 Close(); 372 Close();
318 } 373 }
319 374
320 // Simulate the case where a stream is not properly closed. 375 // Simulate the case where a stream is not properly closed.
321 TEST_F(AudioRendererHostTest, CreateAndShutdown) { 376 TEST_F(AudioRendererHostTest, CreateAndShutdown) {
322 if (!IsRunningHeadless())
323 EnableRealDevice();
324
325 Create(); 377 Create();
326 } 378 }
327 379
328 TEST_F(AudioRendererHostTest, CreatePlayAndClose) { 380 TEST_F(AudioRendererHostTest, CreatePlayAndClose) {
329 if (!IsRunningHeadless())
330 EnableRealDevice();
331
332 Create(); 381 Create();
333 Play(); 382 Play();
334 Close(); 383 Close();
335 } 384 }
336 385
337 TEST_F(AudioRendererHostTest, CreatePlayPauseAndClose) { 386 TEST_F(AudioRendererHostTest, CreatePlayPauseAndClose) {
338 if (!IsRunningHeadless())
339 EnableRealDevice();
340
341 Create(); 387 Create();
342 Play(); 388 Play();
343 Pause(); 389 Pause();
344 Close(); 390 Close();
345 } 391 }
346 392
347 TEST_F(AudioRendererHostTest, SetVolume) { 393 TEST_F(AudioRendererHostTest, SetVolume) {
348 if (!IsRunningHeadless())
349 EnableRealDevice();
350
351 Create(); 394 Create();
352 SetVolume(0.5); 395 SetVolume(0.5);
353 Play(); 396 Play();
354 Pause(); 397 Pause();
355 Close(); 398 Close();
356 } 399 }
357 400
358 // Simulate the case where a stream is not properly closed. 401 // Simulate the case where a stream is not properly closed.
359 TEST_F(AudioRendererHostTest, CreatePlayAndShutdown) { 402 TEST_F(AudioRendererHostTest, CreatePlayAndShutdown) {
360 if (!IsRunningHeadless())
361 EnableRealDevice();
362
363 Create(); 403 Create();
364 Play(); 404 Play();
365 } 405 }
366 406
367 // Simulate the case where a stream is not properly closed. 407 // Simulate the case where a stream is not properly closed.
368 TEST_F(AudioRendererHostTest, CreatePlayPauseAndShutdown) { 408 TEST_F(AudioRendererHostTest, CreatePlayPauseAndShutdown) {
369 if (!IsRunningHeadless())
370 EnableRealDevice();
371
372 Create(); 409 Create();
373 Play(); 410 Play();
374 Pause(); 411 Pause();
375 } 412 }
376 413
414 TEST_F(AudioRendererHostTest, MirroringOfNothing) {
415 StartMirroringTo(destination(), 0);
416 StopMirroringTo(destination());
417 }
418
419 TEST_F(AudioRendererHostTest, StreamLifetimeAroundMirroringSession) {
420 Create();
421 Play();
422 StartMirroringTo(destination(), 1);
423 StopMirroringTo(destination());
424 Close();
425 }
426
427 TEST_F(AudioRendererHostTest, PauseStreamDuringMirroringSession) {
428 Create();
429 Play();
430 StartMirroringTo(destination(), 1);
431 Pause();
432 StopMirroringTo(destination());
433 Close();
434 }
435
436 TEST_F(AudioRendererHostTest, StreamLifetimeWithinMirroringSession) {
437 StartMirroringTo(destination(), 1);
438 Create();
439 Play();
440 Close();
441 StopMirroringTo(destination());
442 }
443
444 TEST_F(AudioRendererHostTest, StreamLifetimeAroundTwoMirroringSessions) {
445 Create();
446 Play();
447 StartMirroringTo(destination(), 1);
448 StopMirroringTo(destination());
449 StartMirroringTo(another_destination(), 1);
450 StopMirroringTo(another_destination());
451 Close();
452 }
453
454 TEST_F(AudioRendererHostTest, StreamLifetimeWithinTwoMirroringSessions) {
455 StartMirroringTo(destination(), 1);
456 Create();
457 Play();
458 StopMirroringTo(destination());
459 StartMirroringTo(another_destination(), 1);
460 Close();
461 StopMirroringTo(another_destination());
462 }
463
464 TEST_F(AudioRendererHostTest, StreamSwitchesMirroringSessions) {
465 StartMirroringTo(destination(), 1);
466 Create();
467 Play();
468 StartMirroringTo(another_destination(), 1);
469 Close();
470 StopMirroringTo(another_destination());
471 StopMirroringTo(destination()); // This should be a no-op.
472 }
473
474 TEST_F(AudioRendererHostTest, SwitchMirroringDestinationNoStreams) {
475 StartMirroringTo(destination(), 0);
476 StartMirroringTo(another_destination(), 0);
477 StopMirroringTo(another_destination());
478 }
479
480 // Simulate the case where mirroring was not stopped before shutdown.
481 TEST_F(AudioRendererHostTest, StartMirroringNothingAndShutdown) {
482 StartMirroringTo(destination(), 0);
483 }
484
485 // Simulate the case where mirroring was not stopped before shutdown, and a
486 // stream was being mirrored.
487 TEST_F(AudioRendererHostTest, StartMirroringSomethingAndShutdown) {
488 StartMirroringTo(destination(), 1);
489 Create();
490 Play();
491 }
492
377 TEST_F(AudioRendererHostTest, SimulateError) { 493 TEST_F(AudioRendererHostTest, SimulateError) {
378 if (!IsRunningHeadless())
379 EnableRealDevice();
380
381 Create(); 494 Create();
382 Play(); 495 Play();
383 SimulateError(); 496 SimulateError();
384 } 497 }
385 498
386 // Simulate the case when an error is generated on the browser process, 499 // Simulate the case when an error is generated on the browser process,
387 // the audio device is closed but the render process try to close the 500 // the audio device is closed but the render process try to close the
388 // audio stream again. 501 // audio stream again.
389 TEST_F(AudioRendererHostTest, SimulateErrorAndClose) { 502 TEST_F(AudioRendererHostTest, SimulateErrorAndClose) {
390 if (!IsRunningHeadless())
391 EnableRealDevice();
392
393 Create(); 503 Create();
394 Play(); 504 Play();
395 SimulateError(); 505 SimulateError();
396 Close(); 506 Close();
397 } 507 }
398 508
399 // TODO(hclam): Add tests for data conversation in low latency mode. 509 // TODO(hclam): Add tests for data conversation in low latency mode.
400 510
401 } // namespace content 511 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698