1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/environment.h"
#include "base/message_loop.h"
#include "base/process_util.h"
#include "base/scoped_ptr.h"
#include "base/sync_socket.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/browser/renderer_host/audio_renderer_host.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/render_messages_params.h"
#include "ipc/ipc_message_utils.h"
#include "media/audio/audio_manager.h"
#include "media/audio/fake_audio_output_stream.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::InvokeWithoutArgs;
using ::testing::Return;
using ::testing::SaveArg;
using ::testing::SetArgumentPointee;
static const int kInvalidId = -1;
static const int kRouteId = 200;
static const int kStreamId = 50;
static bool IsRunningHeadless() {
scoped_ptr<base::Environment> env(base::Environment::Create());
if (env->HasVar("CHROME_HEADLESS"))
return true;
return false;
}
class MockAudioRendererHost : public AudioRendererHost {
public:
MockAudioRendererHost() : shared_memory_length_(0) {
}
virtual ~MockAudioRendererHost() {
}
// A list of mock methods.
MOCK_METHOD3(OnRequestPacket,
void(int routing_id, int stream_id,
AudioBuffersState buffers_state));
MOCK_METHOD3(OnStreamCreated,
void(int routing_id, int stream_id, int length));
MOCK_METHOD3(OnLowLatencyStreamCreated,
void(int routing_id, int stream_id, int length));
MOCK_METHOD2(OnStreamPlaying, void(int routing_id, int stream_id));
MOCK_METHOD2(OnStreamPaused, void(int routing_id, int stream_id));
MOCK_METHOD2(OnStreamError, void(int routing_id, int stream_id));
MOCK_METHOD3(OnStreamVolume,
void(int routing_id, int stream_id, double volume));
base::SharedMemory* shared_memory() { return shared_memory_.get(); }
uint32 shared_memory_length() { return shared_memory_length_; }
base::SyncSocket* sync_socket() { return sync_socket_.get(); }
private:
// This method is used to dispatch IPC messages to the renderer. We intercept
// these messages here and dispatch to our mock methods to verify the
// conversation between this object and the renderer.
virtual void SendMessage(IPC::Message* message) {
CHECK(message);
// In this method we dispatch the messages to the according handlers as if
// we are the renderer.
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(MockAudioRendererHost, *message)
IPC_MESSAGE_HANDLER(ViewMsg_RequestAudioPacket, OnRequestPacket)
IPC_MESSAGE_HANDLER(ViewMsg_NotifyAudioStreamCreated, OnStreamCreated)
IPC_MESSAGE_HANDLER(ViewMsg_NotifyLowLatencyAudioStreamCreated,
OnLowLatencyStreamCreated)
IPC_MESSAGE_HANDLER(ViewMsg_NotifyAudioStreamStateChanged,
OnStreamStateChanged)
IPC_MESSAGE_HANDLER(ViewMsg_NotifyAudioStreamVolume, OnStreamVolume)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
EXPECT_TRUE(handled);
delete message;
}
// These handler methods do minimal things and delegate to the mock methods.
void OnRequestPacket(const IPC::Message& msg, int stream_id,
AudioBuffersState buffers_state) {
OnRequestPacket(msg.routing_id(), stream_id, buffers_state);
}
void OnStreamCreated(const IPC::Message& msg, int stream_id,
base::SharedMemoryHandle handle, uint32 length) {
// Maps the shared memory.
shared_memory_.reset(new base::SharedMemory(handle, false));
ASSERT_TRUE(shared_memory_->Map(length));
ASSERT_TRUE(shared_memory_->memory());
shared_memory_length_ = length;
// And then delegate the call to the mock method.
OnStreamCreated(msg.routing_id(), stream_id, length);
}
void OnLowLatencyStreamCreated(const IPC::Message& msg, int stream_id,
base::SharedMemoryHandle handle,
#if defined(OS_WIN)
base::SyncSocket::Handle socket_handle,
#else
base::FileDescriptor socket_descriptor,
#endif
uint32 length) {
// Maps the shared memory.
shared_memory_.reset(new base::SharedMemory(handle, false));
CHECK(shared_memory_->Map(length));
CHECK(shared_memory_->memory());
shared_memory_length_ = length;
// Create the SyncSocket using the handle.
base::SyncSocket::Handle sync_socket_handle;
#if defined(OS_WIN)
sync_socket_handle = socket_handle;
#else
sync_socket_handle = socket_descriptor.fd;
#endif
sync_socket_.reset(new base::SyncSocket(sync_socket_handle));
// And then delegate the call to the mock method.
OnLowLatencyStreamCreated(msg.routing_id(), stream_id, length);
}
void OnStreamStateChanged(const IPC::Message& msg, int stream_id,
const ViewMsg_AudioStreamState_Params& params) {
if (params.state == ViewMsg_AudioStreamState_Params::kPlaying) {
OnStreamPlaying(msg.routing_id(), stream_id);
} else if (params.state == ViewMsg_AudioStreamState_Params::kPaused) {
OnStreamPaused(msg.routing_id(), stream_id);
} else if (params.state == ViewMsg_AudioStreamState_Params::kError) {
OnStreamError(msg.routing_id(), stream_id);
} else {
FAIL() << "Unknown stream state";
}
}
void OnStreamVolume(const IPC::Message& msg, int stream_id, double volume) {
OnStreamVolume(msg.routing_id(), stream_id, volume);
}
scoped_ptr<base::SharedMemory> shared_memory_;
scoped_ptr<base::SyncSocket> sync_socket_;
uint32 shared_memory_length_;
DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost);
};
ACTION_P(QuitMessageLoop, message_loop) {
message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
}
class AudioRendererHostTest : public testing::Test {
public:
AudioRendererHostTest()
: mock_stream_(true) {
}
protected:
virtual void SetUp() {
// Create a message loop so AudioRendererHost can use it.
message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO));
io_thread_.reset(new BrowserThread(BrowserThread::IO, message_loop_.get()));
host_ = new MockAudioRendererHost();
// Simulate IPC channel connected.
host_->IPCChannelConnected(base::GetCurrentProcId(),
base::GetCurrentProcessHandle(),
NULL);
}
virtual void TearDown() {
// Simulate closing the IPC channel.
host_->IPCChannelClosing();
// This task post a task to message_loop_ to do internal destruction on
// message_loop_.
host_->Destroy();
// Release the reference to the mock object.
host_ = NULL;
// We need to continue running message_loop_ to complete all destructions.
SyncWithAudioThread();
io_thread_.reset();
}
void Create() {
InSequence s;
// 1. We will first receive a OnStreamCreated() signal.
EXPECT_CALL(*host_,
OnStreamCreated(kRouteId, kStreamId, _));
// 2. First packet request will arrive.
EXPECT_CALL(*host_, OnRequestPacket(kRouteId, kStreamId, _))
.WillOnce(QuitMessageLoop(message_loop_.get()));
IPC::Message msg;
msg.set_routing_id(kRouteId);
ViewHostMsg_Audio_CreateStream_Params params;
if (mock_stream_)
params.params.format = AudioParameters::AUDIO_MOCK;
else
params.params.format = AudioParameters::AUDIO_PCM_LINEAR;
params.params.channels = 2;
params.params.sample_rate = AudioParameters::kAudioCDSampleRate;
params.params.bits_per_sample = 16;
params.params.samples_per_packet = 0;
// Send a create stream message to the audio output stream and wait until
// we receive the created message.
host_->OnCreateStream(msg, kStreamId, params, false);
message_loop_->Run();
}
void CreateLowLatency() {
InSequence s;
// We will first receive a OnLowLatencyStreamCreated() signal.
EXPECT_CALL(*host_,
OnLowLatencyStreamCreated(kRouteId, kStreamId, _))
.WillOnce(QuitMessageLoop(message_loop_.get()));
IPC::Message msg;
msg.set_routing_id(kRouteId);
ViewHostMsg_Audio_CreateStream_Params params;
if (mock_stream_)
params.params.format = AudioParameters::AUDIO_MOCK;
else
params.params.format = AudioParameters::AUDIO_PCM_LINEAR;
params.params.channels = 2;
params.params.sample_rate = AudioParameters::kAudioCDSampleRate;
params.params.bits_per_sample = 16;
params.params.samples_per_packet = 0;
// Send a create stream message to the audio output stream and wait until
// we receive the created message.
host_->OnCreateStream(msg, kStreamId, params, true);
message_loop_->Run();
}
void Close() {
// Send a message to AudioRendererHost to tell it we want to close the
// stream.
IPC::Message msg;
msg.set_routing_id(kRouteId);
host_->OnCloseStream(msg, kStreamId);
message_loop_->RunAllPending();
}
void Play() {
EXPECT_CALL(*host_, OnStreamPlaying(kRouteId, kStreamId))
.WillOnce(QuitMessageLoop(message_loop_.get()));
IPC::Message msg;
msg.set_routing_id(kRouteId);
host_->OnPlayStream(msg, kStreamId);
message_loop_->Run();
}
void Pause() {
EXPECT_CALL(*host_, OnStreamPaused(kRouteId, kStreamId))
.WillOnce(QuitMessageLoop(message_loop_.get()));
IPC::Message msg;
msg.set_routing_id(kRouteId);
host_->OnPauseStream(msg, kStreamId);
message_loop_->Run();
}
void SetVolume(double volume) {
IPC::Message msg;
msg.set_routing_id(kRouteId);
host_->OnSetVolume(msg, kStreamId, volume);
message_loop_->RunAllPending();
}
void NotifyPacketReady() {
EXPECT_CALL(*host_, OnRequestPacket(kRouteId, kStreamId, _))
.WillOnce(QuitMessageLoop(message_loop_.get()));
IPC::Message msg;
msg.set_routing_id(kRouteId);
memset(host_->shared_memory()->memory(), 0, host_->shared_memory_length());
host_->OnNotifyPacketReady(msg, kStreamId,
host_->shared_memory_length());
message_loop_->Run();
}
void SimulateError() {
// Find the first AudioOutputController in the AudioRendererHost.
CHECK(host_->audio_entries_.size())
<< "Calls Create() before calling this method";
media::AudioOutputController* controller =
host_->audio_entries_.begin()->second->controller;
CHECK(controller) << "AudioOutputController not found";
// Expect an error signal sent through IPC.
EXPECT_CALL(*host_, OnStreamError(kRouteId, kStreamId));
// Simulate an error sent from the audio device.
host_->OnError(controller, 0);
SyncWithAudioThread();
// Expect the audio stream record is removed.
EXPECT_EQ(0u, host_->audio_entries_.size());
}
// Called on the audio thread.
static void PostQuitMessageLoop(MessageLoop* message_loop) {
message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask());
}
// Called on the main thread.
static void PostQuitOnAudioThread(MessageLoop* message_loop) {
AudioManager::GetAudioManager()->GetMessageLoop()->PostTask(
FROM_HERE, NewRunnableFunction(&PostQuitMessageLoop, message_loop));
}
// SyncWithAudioThread() waits until all pending tasks on the audio thread
// are executed while also processing pending task in message_loop_ on the
// current thread. It is used to synchronize with the audio thread when we are
// closing an audio stream.
void SyncWithAudioThread() {
message_loop_->PostTask(
FROM_HERE, NewRunnableFunction(&PostQuitOnAudioThread,
message_loop_.get()));
message_loop_->Run();
}
MessageLoop* message_loop() { return message_loop_.get(); }
MockAudioRendererHost* host() { return host_; }
void EnableRealDevice() { mock_stream_ = false; }
private:
bool mock_stream_;
scoped_refptr<MockAudioRendererHost> host_;
scoped_ptr<MessageLoop> message_loop_;
scoped_ptr<BrowserThread> io_thread_;
DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest);
};
TEST_F(AudioRendererHostTest, CreateAndClose) {
if (!IsRunningHeadless())
EnableRealDevice();
Create();
Close();
}
TEST_F(AudioRendererHostTest, CreatePlayAndClose) {
if (!IsRunningHeadless())
EnableRealDevice();
Create();
Play();
Close();
}
TEST_F(AudioRendererHostTest, CreatePlayPauseAndClose) {
if (!IsRunningHeadless())
EnableRealDevice();
Create();
Play();
Pause();
Close();
}
TEST_F(AudioRendererHostTest, SetVolume) {
if (!IsRunningHeadless())
EnableRealDevice();
Create();
SetVolume(0.5);
Play();
Pause();
Close();
// Expect the volume is set.
if (IsRunningHeadless()) {
EXPECT_EQ(0.5, FakeAudioOutputStream::GetLastFakeStream()->volume());
}
}
// Simulate the case where a stream is not properly closed.
TEST_F(AudioRendererHostTest, CreateAndShutdown) {
if (!IsRunningHeadless())
EnableRealDevice();
Create();
}
// Simulate the case where a stream is not properly closed.
TEST_F(AudioRendererHostTest, CreatePlayAndShutdown) {
if (!IsRunningHeadless())
EnableRealDevice();
Create();
Play();
}
// Simulate the case where a stream is not properly closed.
TEST_F(AudioRendererHostTest, CreatePlayPauseAndShutdown) {
if (!IsRunningHeadless())
EnableRealDevice();
Create();
Play();
Pause();
}
TEST_F(AudioRendererHostTest, DataConversationMockStream) {
Create();
// Note that we only do notify three times because the buffer capacity is
// triple of one packet size.
NotifyPacketReady();
NotifyPacketReady();
NotifyPacketReady();
Close();
}
TEST_F(AudioRendererHostTest, DataConversationRealStream) {
if (IsRunningHeadless())
return;
EnableRealDevice();
Create();
Play();
// If this is a real audio device, the data conversation is not limited
// to the buffer capacity of AudioOutputController. So we do 5 exchanges
// before we close the device.
for (int i = 0; i < 5; ++i) {
NotifyPacketReady();
}
Close();
}
TEST_F(AudioRendererHostTest, SimulateError) {
if (!IsRunningHeadless())
EnableRealDevice();
Create();
Play();
SimulateError();
}
// Simulate the case when an error is generated on the browser process,
// the audio device is closed but the render process try to close the
// audio stream again.
TEST_F(AudioRendererHostTest, SimulateErrorAndClose) {
if (!IsRunningHeadless())
EnableRealDevice();
Create();
Play();
SimulateError();
Close();
}
TEST_F(AudioRendererHostTest, CreateLowLatencyAndClose) {
if (!IsRunningHeadless())
EnableRealDevice();
CreateLowLatency();
Close();
}
// Simulate the case where a stream is not properly closed.
TEST_F(AudioRendererHostTest, CreateLowLatencyAndShutdown) {
if (!IsRunningHeadless())
EnableRealDevice();
CreateLowLatency();
}
// TODO(hclam): Add tests for data conversation in low latency mode.
|