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

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

Issue 7157001: Implements AudioMessageFilter as member in RenderThread (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Fixed nits in AudioRenderImpl unit test Created 9 years, 5 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/environment.h" 5 #include "base/environment.h"
6 #include "base/memory/scoped_ptr.h" 6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/process_util.h" 8 #include "base/process_util.h"
9 #include "base/sync_socket.h" 9 #include "base/sync_socket.h"
10 #include "content/browser/browser_thread.h" 10 #include "content/browser/browser_thread.h"
11 #include "content/browser/mock_resource_context.h" 11 #include "content/browser/mock_resource_context.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 "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 20
21 using ::testing::_; 21 using ::testing::_;
22 using ::testing::AtLeast; 22 using ::testing::AtLeast;
23 using ::testing::DoAll; 23 using ::testing::DoAll;
24 using ::testing::InSequence; 24 using ::testing::InSequence;
25 using ::testing::InvokeWithoutArgs; 25 using ::testing::InvokeWithoutArgs;
26 using ::testing::Return; 26 using ::testing::Return;
27 using ::testing::SaveArg; 27 using ::testing::SaveArg;
28 using ::testing::SetArgumentPointee; 28 using ::testing::SetArgumentPointee;
29 29
30 static const int kInvalidId = -1;
31 static const int kRouteId = 200;
32 static const int kStreamId = 50; 30 static const int kStreamId = 50;
33 31
34 static bool IsRunningHeadless() { 32 static bool IsRunningHeadless() {
35 scoped_ptr<base::Environment> env(base::Environment::Create()); 33 scoped_ptr<base::Environment> env(base::Environment::Create());
36 if (env->HasVar("CHROME_HEADLESS")) 34 if (env->HasVar("CHROME_HEADLESS"))
37 return true; 35 return true;
38 return false; 36 return false;
39 } 37 }
40 38
41 class MockAudioRendererHost : public AudioRendererHost { 39 class MockAudioRendererHost : public AudioRendererHost {
42 public: 40 public:
43 MockAudioRendererHost(const content::ResourceContext* resource_context) 41 MockAudioRendererHost(const content::ResourceContext* resource_context)
44 : AudioRendererHost(resource_context), 42 : AudioRendererHost(resource_context),
45 shared_memory_length_(0) { 43 shared_memory_length_(0) {
46 } 44 }
47 45
48 virtual ~MockAudioRendererHost() { 46 virtual ~MockAudioRendererHost() {
49 } 47 }
50 48
51 // A list of mock methods. 49 // A list of mock methods.
52 MOCK_METHOD3(OnRequestPacket, 50 MOCK_METHOD2(OnRequestPacket,
53 void(int routing_id, int stream_id, 51 void(int stream_id, AudioBuffersState buffers_state));
54 AudioBuffersState buffers_state)); 52 MOCK_METHOD2(OnStreamCreated,
55 MOCK_METHOD3(OnStreamCreated, 53 void(int stream_id, int length));
56 void(int routing_id, int stream_id, int length)); 54 MOCK_METHOD2(OnLowLatencyStreamCreated,
57 MOCK_METHOD3(OnLowLatencyStreamCreated, 55 void(int stream_id, int length));
58 void(int routing_id, int stream_id, int length)); 56 MOCK_METHOD1(OnStreamPlaying, void(int stream_id));
59 MOCK_METHOD2(OnStreamPlaying, void(int routing_id, int stream_id)); 57 MOCK_METHOD1(OnStreamPaused, void(int stream_id));
60 MOCK_METHOD2(OnStreamPaused, void(int routing_id, int stream_id)); 58 MOCK_METHOD1(OnStreamError, void(int stream_id));
61 MOCK_METHOD2(OnStreamError, void(int routing_id, int stream_id)); 59 MOCK_METHOD2(OnStreamVolume, void(int stream_id, double volume));
62 MOCK_METHOD3(OnStreamVolume,
63 void(int routing_id, int stream_id, double volume));
64 60
65 base::SharedMemory* shared_memory() { return shared_memory_.get(); } 61 base::SharedMemory* shared_memory() { return shared_memory_.get(); }
66 uint32 shared_memory_length() { return shared_memory_length_; } 62 uint32 shared_memory_length() { return shared_memory_length_; }
67 63
68 base::SyncSocket* sync_socket() { return sync_socket_.get(); } 64 base::SyncSocket* sync_socket() { return sync_socket_.get(); }
69 65
70 private: 66 private:
71 // This method is used to dispatch IPC messages to the renderer. We intercept 67 // This method is used to dispatch IPC messages to the renderer. We intercept
72 // these messages here and dispatch to our mock methods to verify the 68 // these messages here and dispatch to our mock methods to verify the
73 // conversation between this object and the renderer. 69 // conversation between this object and the renderer.
(...skipping 15 matching lines...) Expand all
89 IPC_END_MESSAGE_MAP() 85 IPC_END_MESSAGE_MAP()
90 EXPECT_TRUE(handled); 86 EXPECT_TRUE(handled);
91 87
92 delete message; 88 delete message;
93 return true; 89 return true;
94 } 90 }
95 91
96 // These handler methods do minimal things and delegate to the mock methods. 92 // These handler methods do minimal things and delegate to the mock methods.
97 void OnRequestPacket(const IPC::Message& msg, int stream_id, 93 void OnRequestPacket(const IPC::Message& msg, int stream_id,
98 AudioBuffersState buffers_state) { 94 AudioBuffersState buffers_state) {
99 OnRequestPacket(msg.routing_id(), stream_id, buffers_state); 95 OnRequestPacket(stream_id, buffers_state);
100 } 96 }
101 97
102 void OnStreamCreated(const IPC::Message& msg, int stream_id, 98 void OnStreamCreated(const IPC::Message& msg, int stream_id,
103 base::SharedMemoryHandle handle, uint32 length) { 99 base::SharedMemoryHandle handle, uint32 length) {
104 // Maps the shared memory. 100 // Maps the shared memory.
105 shared_memory_.reset(new base::SharedMemory(handle, false)); 101 shared_memory_.reset(new base::SharedMemory(handle, false));
106 ASSERT_TRUE(shared_memory_->Map(length)); 102 ASSERT_TRUE(shared_memory_->Map(length));
107 ASSERT_TRUE(shared_memory_->memory()); 103 ASSERT_TRUE(shared_memory_->memory());
108 shared_memory_length_ = length; 104 shared_memory_length_ = length;
109 105
110 // And then delegate the call to the mock method. 106 // And then delegate the call to the mock method.
111 OnStreamCreated(msg.routing_id(), stream_id, length); 107 OnStreamCreated(stream_id, length);
112 } 108 }
113 109
114 void OnLowLatencyStreamCreated(const IPC::Message& msg, int stream_id, 110 void OnLowLatencyStreamCreated(const IPC::Message& msg, int stream_id,
115 base::SharedMemoryHandle handle, 111 base::SharedMemoryHandle handle,
116 #if defined(OS_WIN) 112 #if defined(OS_WIN)
117 base::SyncSocket::Handle socket_handle, 113 base::SyncSocket::Handle socket_handle,
118 #else 114 #else
119 base::FileDescriptor socket_descriptor, 115 base::FileDescriptor socket_descriptor,
120 #endif 116 #endif
121 uint32 length) { 117 uint32 length) {
122 // Maps the shared memory. 118 // Maps the shared memory.
123 shared_memory_.reset(new base::SharedMemory(handle, false)); 119 shared_memory_.reset(new base::SharedMemory(handle, false));
124 CHECK(shared_memory_->Map(length)); 120 CHECK(shared_memory_->Map(length));
125 CHECK(shared_memory_->memory()); 121 CHECK(shared_memory_->memory());
126 shared_memory_length_ = length; 122 shared_memory_length_ = length;
127 123
128 // Create the SyncSocket using the handle. 124 // Create the SyncSocket using the handle.
129 base::SyncSocket::Handle sync_socket_handle; 125 base::SyncSocket::Handle sync_socket_handle;
130 #if defined(OS_WIN) 126 #if defined(OS_WIN)
131 sync_socket_handle = socket_handle; 127 sync_socket_handle = socket_handle;
132 #else 128 #else
133 sync_socket_handle = socket_descriptor.fd; 129 sync_socket_handle = socket_descriptor.fd;
134 #endif 130 #endif
135 sync_socket_.reset(new base::SyncSocket(sync_socket_handle)); 131 sync_socket_.reset(new base::SyncSocket(sync_socket_handle));
136 132
137 // And then delegate the call to the mock method. 133 // And then delegate the call to the mock method.
138 OnLowLatencyStreamCreated(msg.routing_id(), stream_id, length); 134 OnLowLatencyStreamCreated(stream_id, length);
139 } 135 }
140 136
141 void OnStreamStateChanged(const IPC::Message& msg, int stream_id, 137 void OnStreamStateChanged(const IPC::Message& msg, int stream_id,
142 AudioStreamState state) { 138 AudioStreamState state) {
143 if (state == kAudioStreamPlaying) { 139 if (state == kAudioStreamPlaying) {
144 OnStreamPlaying(msg.routing_id(), stream_id); 140 OnStreamPlaying(stream_id);
145 } else if (state == kAudioStreamPaused) { 141 } else if (state == kAudioStreamPaused) {
146 OnStreamPaused(msg.routing_id(), stream_id); 142 OnStreamPaused(stream_id);
147 } else if (state == kAudioStreamError) { 143 } else if (state == kAudioStreamError) {
148 OnStreamError(msg.routing_id(), stream_id); 144 OnStreamError(stream_id);
149 } else { 145 } else {
150 FAIL() << "Unknown stream state"; 146 FAIL() << "Unknown stream state";
151 } 147 }
152 } 148 }
153 149
154 void OnStreamVolume(const IPC::Message& msg, int stream_id, double volume) { 150 void OnStreamVolume(const IPC::Message& msg, int stream_id, double volume) {
155 OnStreamVolume(msg.routing_id(), stream_id, volume); 151 OnStreamVolume(stream_id, volume);
156 } 152 }
157 153
158 scoped_ptr<base::SharedMemory> shared_memory_; 154 scoped_ptr<base::SharedMemory> shared_memory_;
159 scoped_ptr<base::SyncSocket> sync_socket_; 155 scoped_ptr<base::SyncSocket> sync_socket_;
160 uint32 shared_memory_length_; 156 uint32 shared_memory_length_;
161 157
162 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); 158 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost);
163 }; 159 };
164 160
165 ACTION_P(QuitMessageLoop, message_loop) { 161 ACTION_P(QuitMessageLoop, message_loop) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 197
202 // We need to continue running message_loop_ to complete all destructions. 198 // We need to continue running message_loop_ to complete all destructions.
203 SyncWithAudioThread(); 199 SyncWithAudioThread();
204 200
205 io_thread_.reset(); 201 io_thread_.reset();
206 ui_thread_.reset(); 202 ui_thread_.reset();
207 } 203 }
208 204
209 void Create() { 205 void Create() {
210 EXPECT_CALL(*observer_, 206 EXPECT_CALL(*observer_,
211 OnSetAudioStreamStatus(_, kRouteId, kStreamId, "created")); 207 OnSetAudioStreamStatus(_, kStreamId, "created"));
212 EXPECT_CALL(*observer_, OnDeleteAudioStream(_, kRouteId, kStreamId)); 208 EXPECT_CALL(*observer_, OnDeleteAudioStream(_, kStreamId));
213 209
214 InSequence s; 210 InSequence s;
215 // 1. We will first receive a OnStreamCreated() signal. 211 // 1. We will first receive a OnStreamCreated() signal.
216 EXPECT_CALL(*host_, 212 EXPECT_CALL(*host_, OnStreamCreated(kStreamId, _));
217 OnStreamCreated(kRouteId, kStreamId, _));
218 213
219 // 2. First packet request will arrive. 214 // 2. First packet request will arrive.
220 EXPECT_CALL(*host_, OnRequestPacket(kRouteId, kStreamId, _)) 215 EXPECT_CALL(*host_, OnRequestPacket(kStreamId, _))
221 .WillOnce(QuitMessageLoop(message_loop_.get())); 216 .WillOnce(QuitMessageLoop(message_loop_.get()));
222 217
223 IPC::Message msg;
224 msg.set_routing_id(kRouteId);
225
226 AudioParameters params; 218 AudioParameters params;
227 if (mock_stream_) 219 if (mock_stream_)
228 params.format = AudioParameters::AUDIO_MOCK; 220 params.format = AudioParameters::AUDIO_MOCK;
229 else 221 else
230 params.format = AudioParameters::AUDIO_PCM_LINEAR; 222 params.format = AudioParameters::AUDIO_PCM_LINEAR;
231 params.channels = 2; 223 params.channels = 2;
232 params.sample_rate = AudioParameters::kAudioCDSampleRate; 224 params.sample_rate = AudioParameters::kAudioCDSampleRate;
233 params.bits_per_sample = 16; 225 params.bits_per_sample = 16;
234 params.samples_per_packet = 0; 226 params.samples_per_packet = 0;
235 227
236 // Send a create stream message to the audio output stream and wait until 228 // Send a create stream message to the audio output stream and wait until
237 // we receive the created message. 229 // we receive the created message.
238 host_->OnCreateStream(msg, kStreamId, params, false); 230 host_->OnCreateStream(kStreamId, params, false);
239 message_loop_->Run(); 231 message_loop_->Run();
240 } 232 }
241 233
242 void CreateLowLatency() { 234 void CreateLowLatency() {
243 EXPECT_CALL(*observer_, 235 EXPECT_CALL(*observer_,
244 OnSetAudioStreamStatus(_, kRouteId, kStreamId, "created")); 236 OnSetAudioStreamStatus(_, kStreamId, "created"));
245 EXPECT_CALL(*observer_, OnDeleteAudioStream(_, kRouteId, kStreamId)); 237 EXPECT_CALL(*observer_, OnDeleteAudioStream(_, kStreamId));
246 238
247 InSequence s; 239 InSequence s;
248 // We will first receive a OnLowLatencyStreamCreated() signal. 240 // We will first receive a OnLowLatencyStreamCreated() signal.
249 EXPECT_CALL(*host_, 241 EXPECT_CALL(*host_,
250 OnLowLatencyStreamCreated(kRouteId, kStreamId, _)) 242 OnLowLatencyStreamCreated(kStreamId, _))
251 .WillOnce(QuitMessageLoop(message_loop_.get())); 243 .WillOnce(QuitMessageLoop(message_loop_.get()));
252 244
253 IPC::Message msg;
254 msg.set_routing_id(kRouteId);
255
256 AudioParameters params; 245 AudioParameters params;
257 if (mock_stream_) 246 if (mock_stream_)
258 params.format = AudioParameters::AUDIO_MOCK; 247 params.format = AudioParameters::AUDIO_MOCK;
259 else 248 else
260 params.format = AudioParameters::AUDIO_PCM_LINEAR; 249 params.format = AudioParameters::AUDIO_PCM_LINEAR;
261 params.channels = 2; 250 params.channels = 2;
262 params.sample_rate = AudioParameters::kAudioCDSampleRate; 251 params.sample_rate = AudioParameters::kAudioCDSampleRate;
263 params.bits_per_sample = 16; 252 params.bits_per_sample = 16;
264 params.samples_per_packet = 0; 253 params.samples_per_packet = 0;
265 254
266 // Send a create stream message to the audio output stream and wait until 255 // Send a create stream message to the audio output stream and wait until
267 // we receive the created message. 256 // we receive the created message.
268 host_->OnCreateStream(msg, kStreamId, params, true); 257 host_->OnCreateStream(kStreamId, params, true);
269 message_loop_->Run(); 258 message_loop_->Run();
270 } 259 }
271 260
272 void Close() { 261 void Close() {
273 EXPECT_CALL(*observer_, 262 EXPECT_CALL(*observer_,
274 OnSetAudioStreamStatus(_, kRouteId, kStreamId, "closed")); 263 OnSetAudioStreamStatus(_, kStreamId, "closed"));
275 264
276 // Send a message to AudioRendererHost to tell it we want to close the 265 // Send a message to AudioRendererHost to tell it we want to close the
277 // stream. 266 // stream.
278 IPC::Message msg; 267 host_->OnCloseStream(kStreamId);
279 msg.set_routing_id(kRouteId);
280 host_->OnCloseStream(msg, kStreamId);
281 message_loop_->RunAllPending(); 268 message_loop_->RunAllPending();
282 } 269 }
283 270
284 void Play() { 271 void Play() {
285 EXPECT_CALL(*observer_, 272 EXPECT_CALL(*observer_,
286 OnSetAudioStreamPlaying(_, kRouteId, kStreamId, true)); 273 OnSetAudioStreamPlaying(_, kStreamId, true));
287 EXPECT_CALL(*host_, OnStreamPlaying(kRouteId, kStreamId)) 274 EXPECT_CALL(*host_, OnStreamPlaying(kStreamId))
288 .WillOnce(QuitMessageLoop(message_loop_.get())); 275 .WillOnce(QuitMessageLoop(message_loop_.get()));
289 276
290 IPC::Message msg; 277 host_->OnPlayStream(kStreamId);
291 msg.set_routing_id(kRouteId);
292 host_->OnPlayStream(msg, kStreamId);
293 message_loop_->Run(); 278 message_loop_->Run();
294 } 279 }
295 280
296 void Pause() { 281 void Pause() {
297 EXPECT_CALL(*observer_, 282 EXPECT_CALL(*observer_,
298 OnSetAudioStreamPlaying(_, kRouteId, kStreamId, false)); 283 OnSetAudioStreamPlaying(_, kStreamId, false));
299 EXPECT_CALL(*host_, OnStreamPaused(kRouteId, kStreamId)) 284 EXPECT_CALL(*host_, OnStreamPaused(kStreamId))
300 .WillOnce(QuitMessageLoop(message_loop_.get())); 285 .WillOnce(QuitMessageLoop(message_loop_.get()));
301 286
302 IPC::Message msg; 287 host_->OnPauseStream(kStreamId);
303 msg.set_routing_id(kRouteId);
304 host_->OnPauseStream(msg, kStreamId);
305 message_loop_->Run(); 288 message_loop_->Run();
306 } 289 }
307 290
308 void SetVolume(double volume) { 291 void SetVolume(double volume) {
309 EXPECT_CALL(*observer_, 292 EXPECT_CALL(*observer_,
310 OnSetAudioStreamVolume(_, kRouteId, kStreamId, volume)); 293 OnSetAudioStreamVolume(_, kStreamId, volume));
311 IPC::Message msg; 294
312 msg.set_routing_id(kRouteId); 295 host_->OnSetVolume(kStreamId, volume);
313 host_->OnSetVolume(msg, kStreamId, volume);
314 message_loop_->RunAllPending(); 296 message_loop_->RunAllPending();
315 } 297 }
316 298
317 void NotifyPacketReady() { 299 void NotifyPacketReady() {
318 EXPECT_CALL(*host_, OnRequestPacket(kRouteId, kStreamId, _)) 300 EXPECT_CALL(*host_, OnRequestPacket(kStreamId, _))
319 .WillOnce(QuitMessageLoop(message_loop_.get())); 301 .WillOnce(QuitMessageLoop(message_loop_.get()));
320 302
321 IPC::Message msg;
322 msg.set_routing_id(kRouteId);
323 memset(host_->shared_memory()->memory(), 0, host_->shared_memory_length()); 303 memset(host_->shared_memory()->memory(), 0, host_->shared_memory_length());
324 host_->OnNotifyPacketReady(msg, kStreamId, 304 host_->OnNotifyPacketReady(kStreamId, host_->shared_memory_length());
325 host_->shared_memory_length());
326 message_loop_->Run(); 305 message_loop_->Run();
327 } 306 }
328 307
329 void SimulateError() { 308 void SimulateError() {
330 EXPECT_CALL(*observer_, 309 EXPECT_CALL(*observer_,
331 OnSetAudioStreamStatus(_, kRouteId, kStreamId, "error")); 310 OnSetAudioStreamStatus(_, kStreamId, "error"));
332 // Find the first AudioOutputController in the AudioRendererHost. 311 // Find the first AudioOutputController in the AudioRendererHost.
333 CHECK(host_->audio_entries_.size()) 312 CHECK(host_->audio_entries_.size())
334 << "Calls Create() before calling this method"; 313 << "Calls Create() before calling this method";
335 media::AudioOutputController* controller = 314 media::AudioOutputController* controller =
336 host_->audio_entries_.begin()->second->controller; 315 host_->audio_entries_.begin()->second->controller;
337 CHECK(controller) << "AudioOutputController not found"; 316 CHECK(controller) << "AudioOutputController not found";
338 317
339 // Expect an error signal sent through IPC. 318 // Expect an error signal sent through IPC.
340 EXPECT_CALL(*host_, OnStreamError(kRouteId, kStreamId)); 319 EXPECT_CALL(*host_, OnStreamError(kStreamId));
341 320
342 // Simulate an error sent from the audio device. 321 // Simulate an error sent from the audio device.
343 host_->OnError(controller, 0); 322 host_->OnError(controller, 0);
344 SyncWithAudioThread(); 323 SyncWithAudioThread();
345 324
346 // Expect the audio stream record is removed. 325 // Expect the audio stream record is removed.
347 EXPECT_EQ(0u, host_->audio_entries_.size()); 326 EXPECT_EQ(0u, host_->audio_entries_.size());
348 } 327 }
349 328
350 // Called on the audio thread. 329 // Called on the audio thread.
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 492
514 // Simulate the case where a stream is not properly closed. 493 // Simulate the case where a stream is not properly closed.
515 TEST_F(AudioRendererHostTest, CreateLowLatencyAndShutdown) { 494 TEST_F(AudioRendererHostTest, CreateLowLatencyAndShutdown) {
516 if (!IsRunningHeadless()) 495 if (!IsRunningHeadless())
517 EnableRealDevice(); 496 EnableRealDevice();
518 497
519 CreateLowLatency(); 498 CreateLowLatency();
520 } 499 }
521 500
522 // TODO(hclam): Add tests for data conversation in low latency mode. 501 // TODO(hclam): Add tests for data conversation in low latency mode.
OLDNEW
« no previous file with comments | « content/browser/renderer_host/media/audio_renderer_host.cc ('k') | content/browser/renderer_host/media/media_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698