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

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

Issue 1323403005: Allow AudioOutputDevice objects to be initialized with a specific hardware output device and store … (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: dalecurtis' comments Created 5 years, 3 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
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/command_line.h" 6 #include "base/command_line.h"
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/sync_socket.h" 9 #include "base/sync_socket.h"
10 #include "content/browser/media/capture/audio_mirroring_manager.h" 10 #include "content/browser/media/capture/audio_mirroring_manager.h"
(...skipping 14 matching lines...) Expand all
25 using ::testing::_; 25 using ::testing::_;
26 using ::testing::Assign; 26 using ::testing::Assign;
27 using ::testing::DoAll; 27 using ::testing::DoAll;
28 using ::testing::NotNull; 28 using ::testing::NotNull;
29 29
30 namespace { 30 namespace {
31 const int kRenderProcessId = 1; 31 const int kRenderProcessId = 1;
32 const int kRenderFrameId = 5; 32 const int kRenderFrameId = 5;
33 const int kStreamId = 50; 33 const int kStreamId = 50;
34 const int kBadStreamId = 99; 34 const int kBadStreamId = 99;
35 const int kSwitchOutputDeviceRequestId = 1;
36 const GURL kSecurityOrigin("http://localhost"); 35 const GURL kSecurityOrigin("http://localhost");
37 const std::string kDefaultDeviceID = ""; 36 const GURL kDefaultSecurityOrigin;
37 const std::string kDefaultDeviceID;
38 const std::string kBadDeviceID = "bad-device-id"; 38 const std::string kBadDeviceID = "bad-device-id";
39 } // namespace 39 } // namespace
40 40
41 namespace content { 41 namespace content {
42 42
43 class MockAudioMirroringManager : public AudioMirroringManager { 43 class MockAudioMirroringManager : public AudioMirroringManager {
44 public: 44 public:
45 MockAudioMirroringManager() {} 45 MockAudioMirroringManager() {}
46 virtual ~MockAudioMirroringManager() {} 46 virtual ~MockAudioMirroringManager() {}
47 47
(...skipping 16 matching lines...) Expand all
64 const ResourceContext::SaltCallback& salt_callback) 64 const ResourceContext::SaltCallback& salt_callback)
65 : AudioRendererHost(kRenderProcessId, 65 : AudioRendererHost(kRenderProcessId,
66 audio_manager, 66 audio_manager,
67 mirroring_manager, 67 mirroring_manager,
68 media_internals, 68 media_internals,
69 media_stream_manager, 69 media_stream_manager,
70 salt_callback), 70 salt_callback),
71 shared_memory_length_(0) {} 71 shared_memory_length_(0) {}
72 72
73 // A list of mock methods. 73 // A list of mock methods.
74 MOCK_METHOD3(OnDeviceAuthorized,
75 void(int stream_id,
76 bool success,
77 const media::AudioParameters& output_params));
74 MOCK_METHOD2(OnStreamCreated, void(int stream_id, int length)); 78 MOCK_METHOD2(OnStreamCreated, void(int stream_id, int length));
75 MOCK_METHOD1(OnStreamPlaying, void(int stream_id)); 79 MOCK_METHOD1(OnStreamPlaying, void(int stream_id));
76 MOCK_METHOD1(OnStreamPaused, void(int stream_id)); 80 MOCK_METHOD1(OnStreamPaused, void(int stream_id));
77 MOCK_METHOD1(OnStreamError, void(int stream_id)); 81 MOCK_METHOD1(OnStreamError, void(int stream_id));
78 MOCK_METHOD3(OnOutputDeviceSwitched, 82 MOCK_METHOD2(OnOutputDeviceSwitched,
79 void(int stream_id, 83 void(int stream_id, media::SwitchOutputDeviceResult result));
80 int request_id,
81 media::SwitchOutputDeviceResult result));
82 84
83 private: 85 private:
84 virtual ~MockAudioRendererHost() { 86 virtual ~MockAudioRendererHost() {
85 // Make sure all audio streams have been deleted. 87 // Make sure all audio streams have been deleted.
86 EXPECT_TRUE(audio_entries_.empty()); 88 EXPECT_TRUE(audio_entries_.empty());
87 } 89 }
88 90
89 // This method is used to dispatch IPC messages to the renderer. We intercept 91 // This method is used to dispatch IPC messages to the renderer. We intercept
90 // these messages here and dispatch to our mock methods to verify the 92 // these messages here and dispatch to our mock methods to verify the
91 // conversation between this object and the renderer. 93 // conversation between this object and the renderer.
92 virtual bool Send(IPC::Message* message) { 94 virtual bool Send(IPC::Message* message) {
93 CHECK(message); 95 CHECK(message);
94 96
95 // In this method we dispatch the messages to the according handlers as if 97 // In this method we dispatch the messages to the according handlers as if
96 // we are the renderer. 98 // we are the renderer.
97 bool handled = true; 99 bool handled = true;
98 IPC_BEGIN_MESSAGE_MAP(MockAudioRendererHost, *message) 100 IPC_BEGIN_MESSAGE_MAP(MockAudioRendererHost, *message)
101 IPC_MESSAGE_HANDLER(AudioMsg_NotifyDeviceAuthorized,
102 OnNotifyDeviceAuthorized)
99 IPC_MESSAGE_HANDLER(AudioMsg_NotifyStreamCreated, 103 IPC_MESSAGE_HANDLER(AudioMsg_NotifyStreamCreated,
100 OnNotifyStreamCreated) 104 OnNotifyStreamCreated)
101 IPC_MESSAGE_HANDLER(AudioMsg_NotifyStreamStateChanged, 105 IPC_MESSAGE_HANDLER(AudioMsg_NotifyStreamStateChanged,
102 OnNotifyStreamStateChanged) 106 OnNotifyStreamStateChanged)
103 IPC_MESSAGE_HANDLER(AudioMsg_NotifyOutputDeviceSwitched, 107 IPC_MESSAGE_HANDLER(AudioMsg_NotifyOutputDeviceSwitched,
104 OnNotifyOutputDeviceSwitched) 108 OnNotifyOutputDeviceSwitched)
105 IPC_MESSAGE_UNHANDLED(handled = false) 109 IPC_MESSAGE_UNHANDLED(handled = false)
106 IPC_END_MESSAGE_MAP() 110 IPC_END_MESSAGE_MAP()
107 EXPECT_TRUE(handled); 111 EXPECT_TRUE(handled);
108 112
109 delete message; 113 delete message;
110 return true; 114 return true;
111 } 115 }
112 116
117 void OnNotifyDeviceAuthorized(int stream_id,
118 bool success,
119 const media::AudioParameters& output_params) {
120 OnDeviceAuthorized(stream_id, success, output_params);
121 }
122
113 void OnNotifyStreamCreated( 123 void OnNotifyStreamCreated(
114 int stream_id, base::SharedMemoryHandle handle, 124 int stream_id, base::SharedMemoryHandle handle,
115 base::SyncSocket::TransitDescriptor socket_descriptor, uint32 length) { 125 base::SyncSocket::TransitDescriptor socket_descriptor, uint32 length) {
116 // Maps the shared memory. 126 // Maps the shared memory.
117 shared_memory_.reset(new base::SharedMemory(handle, false)); 127 shared_memory_.reset(new base::SharedMemory(handle, false));
118 CHECK(shared_memory_->Map(length)); 128 CHECK(shared_memory_->Map(length));
119 CHECK(shared_memory_->memory()); 129 CHECK(shared_memory_->memory());
120 shared_memory_length_ = length; 130 shared_memory_length_ = length;
121 131
122 // Create the SyncSocket using the handle. 132 // Create the SyncSocket using the handle.
(...skipping 17 matching lines...) Expand all
140 case media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR: 150 case media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR:
141 OnStreamError(stream_id); 151 OnStreamError(stream_id);
142 break; 152 break;
143 default: 153 default:
144 FAIL() << "Unknown stream state"; 154 FAIL() << "Unknown stream state";
145 break; 155 break;
146 } 156 }
147 } 157 }
148 158
149 void OnNotifyOutputDeviceSwitched(int stream_id, 159 void OnNotifyOutputDeviceSwitched(int stream_id,
150 int request_id,
151 media::SwitchOutputDeviceResult result) { 160 media::SwitchOutputDeviceResult result) {
152 switch (result) { 161 switch (result) {
153 case media::SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS: 162 case media::SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS:
154 case media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_FOUND: 163 case media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_FOUND:
155 case media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_AUTHORIZED: 164 case media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_AUTHORIZED:
156 case media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE: 165 case media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_INTERNAL:
157 case media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED: 166 OnOutputDeviceSwitched(stream_id, result);
158 OnOutputDeviceSwitched(stream_id, request_id, result);
159 break; 167 break;
160 default: 168 default:
161 FAIL() << "Unknown SwitchOutputDevice result"; 169 FAIL() << "Unknown SwitchOutputDevice result";
162 break; 170 break;
163 } 171 }
164 } 172 }
165 173
166 scoped_ptr<base::SharedMemory> shared_memory_; 174 scoped_ptr<base::SharedMemory> shared_memory_;
167 scoped_ptr<base::SyncSocket> sync_socket_; 175 scoped_ptr<base::SyncSocket> sync_socket_;
168 uint32 shared_memory_length_; 176 uint32 shared_memory_length_;
169 177
170 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost); 178 DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost);
171 }; 179 };
172 180
173 namespace { 181 namespace {
174 std::string ReturnMockSalt() { 182 std::string ReturnMockSalt() {
175 return std::string(); 183 return std::string();
176 } 184 }
177 185
178 ResourceContext::SaltCallback GetMockSaltCallback() { 186 ResourceContext::SaltCallback GetMockSaltCallback() {
179 return base::Bind(&ReturnMockSalt); 187 return base::Bind(&ReturnMockSalt);
180 } 188 }
189
190 void WaitForEnumeration(base::RunLoop* loop,
191 const AudioOutputDeviceEnumeration& e) {
192 loop->Quit();
181 } 193 }
194 } // namespace
182 195
183 class AudioRendererHostTest : public testing::Test { 196 class AudioRendererHostTest : public testing::Test {
184 public: 197 public:
185 AudioRendererHostTest() { 198 AudioRendererHostTest() {
186 audio_manager_.reset(media::AudioManager::CreateForTesting()); 199 audio_manager_.reset(media::AudioManager::CreateForTesting());
187 base::CommandLine::ForCurrentProcess()->AppendSwitch( 200 base::CommandLine::ForCurrentProcess()->AppendSwitch(
188 switches::kUseFakeDeviceForMediaStream); 201 switches::kUseFakeDeviceForMediaStream);
189 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); 202 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get()));
203
204 // Enable caching to make enumerations run in a single thread
205 media_stream_manager_->audio_output_device_enumerator()->SetCachePolicy(
206 AudioOutputDeviceEnumerator::CACHE_POLICY_MANUAL_INVALIDATION);
207 base::RunLoop().RunUntilIdle();
208 base::RunLoop run_loop;
209 media_stream_manager_->audio_output_device_enumerator()->Enumerate(
210 base::Bind(&WaitForEnumeration, &run_loop));
211 run_loop.Run();
212
190 host_ = new MockAudioRendererHost(audio_manager_.get(), &mirroring_manager_, 213 host_ = new MockAudioRendererHost(audio_manager_.get(), &mirroring_manager_,
191 MediaInternals::GetInstance(), 214 MediaInternals::GetInstance(),
192 media_stream_manager_.get(), 215 media_stream_manager_.get(),
193 GetMockSaltCallback()); 216 GetMockSaltCallback());
194 217
195 // Simulate IPC channel connected. 218 // Simulate IPC channel connected.
196 host_->set_peer_process_for_testing(base::Process::Current()); 219 host_->set_peer_process_for_testing(base::Process::Current());
197 } 220 }
198 221
199 ~AudioRendererHostTest() override { 222 ~AudioRendererHostTest() override {
200 // Simulate closing the IPC channel and give the audio thread time to close 223 // Simulate closing the IPC channel and give the audio thread time to close
201 // the underlying streams. 224 // the underlying streams.
202 host_->OnChannelClosing(); 225 host_->OnChannelClosing();
203 SyncWithAudioThread(); 226 SyncWithAudioThread();
204 227
205 // Release the reference to the mock object. The object will be destructed 228 // Release the reference to the mock object. The object will be destructed
206 // on message_loop_. 229 // on message_loop_.
207 host_ = NULL; 230 host_ = NULL;
208 } 231 }
209 232
210 protected: 233 protected:
211 void Create(bool unified_stream) { 234 void Create(bool unified_stream) {
235 EXPECT_CALL(*host_.get(), OnDeviceAuthorized(kStreamId, true, _));
212 EXPECT_CALL(*host_.get(), OnStreamCreated(kStreamId, _)); 236 EXPECT_CALL(*host_.get(), OnStreamCreated(kStreamId, _));
213 237
214 EXPECT_CALL(mirroring_manager_, 238 EXPECT_CALL(mirroring_manager_,
215 AddDiverter(kRenderProcessId, kRenderFrameId, NotNull())) 239 AddDiverter(kRenderProcessId, kRenderFrameId, NotNull()))
216 .RetiresOnSaturation(); 240 .RetiresOnSaturation();
217 241
218 // Send a create stream message to the audio output stream and wait until 242 // Send a create stream message to the audio output stream and wait until
219 // we receive the created message. 243 // we receive the created message.
220 media::AudioParameters params( 244 media::AudioParameters params(
221 media::AudioParameters::AUDIO_FAKE, media::CHANNEL_LAYOUT_STEREO, 245 media::AudioParameters::AUDIO_FAKE, media::CHANNEL_LAYOUT_STEREO,
222 media::AudioParameters::kAudioCDSampleRate, 16, 246 media::AudioParameters::kAudioCDSampleRate, 16,
223 media::AudioParameters::kAudioCDSampleRate / 10); 247 media::AudioParameters::kAudioCDSampleRate / 10);
224 int session_id = 0; 248 int session_id = 0;
225 if (unified_stream) { 249 if (unified_stream) {
226 // Use AudioInputDeviceManager::kFakeOpenSessionId as the session id to 250 // Use AudioInputDeviceManager::kFakeOpenSessionId as the session id to
227 // pass the permission check. 251 // pass the permission check.
228 session_id = AudioInputDeviceManager::kFakeOpenSessionId; 252 session_id = AudioInputDeviceManager::kFakeOpenSessionId;
229 } 253 }
230 host_->OnCreateStream(kStreamId, kRenderFrameId, session_id, params); 254 host_->OnRequestDeviceAuthorization(kStreamId, kRenderFrameId, session_id,
255 kDefaultDeviceID,
256 kDefaultSecurityOrigin);
257 host_->OnCreateStream(kStreamId, kRenderFrameId, params);
231 258
232 // At some point in the future, a corresponding RemoveDiverter() call must 259 // At some point in the future, a corresponding RemoveDiverter() call must
233 // be made. 260 // be made.
234 EXPECT_CALL(mirroring_manager_, RemoveDiverter(NotNull())) 261 EXPECT_CALL(mirroring_manager_, RemoveDiverter(NotNull()))
235 .RetiresOnSaturation(); 262 .RetiresOnSaturation();
236 SyncWithAudioThread(); 263 SyncWithAudioThread();
237 } 264 }
238 265
239 void Close() { 266 void Close() {
240 // Send a message to AudioRendererHost to tell it we want to close the 267 // Send a message to AudioRendererHost to tell it we want to close the
(...skipping 13 matching lines...) Expand all
254 host_->OnPauseStream(kStreamId); 281 host_->OnPauseStream(kStreamId);
255 SyncWithAudioThread(); 282 SyncWithAudioThread();
256 } 283 }
257 284
258 void SetVolume(double volume) { 285 void SetVolume(double volume) {
259 host_->OnSetVolume(kStreamId, volume); 286 host_->OnSetVolume(kStreamId, volume);
260 SyncWithAudioThread(); 287 SyncWithAudioThread();
261 } 288 }
262 289
263 void SwitchOutputDevice(int stream_id, 290 void SwitchOutputDevice(int stream_id,
264 std::string device_id, 291 const std::string& device_id,
292 const GURL& security_origin,
265 media::SwitchOutputDeviceResult expected_result) { 293 media::SwitchOutputDeviceResult expected_result) {
266 EXPECT_CALL(*host_.get(), 294 EXPECT_CALL(*host_.get(),
267 OnOutputDeviceSwitched(stream_id, kSwitchOutputDeviceRequestId, 295 OnOutputDeviceSwitched(stream_id, expected_result));
268 expected_result));
269 host_->OnSwitchOutputDevice(stream_id, kRenderFrameId, device_id, 296 host_->OnSwitchOutputDevice(stream_id, kRenderFrameId, device_id,
270 kSecurityOrigin, kSwitchOutputDeviceRequestId); 297 security_origin);
271 SyncWithAudioThread(); 298 SyncWithAudioThread();
272 } 299 }
273 300
274 void SimulateError() { 301 void SimulateError() {
275 EXPECT_EQ(1u, host_->audio_entries_.size()) 302 EXPECT_EQ(1u, host_->audio_entries_.size())
276 << "Calls Create() before calling this method"; 303 << "Calls Create() before calling this method";
277 304
278 // Expect an error signal sent through IPC. 305 // Expect an error signal sent through IPC.
279 EXPECT_CALL(*host_.get(), OnStreamError(kStreamId)); 306 EXPECT_CALL(*host_.get(), OnStreamError(kStreamId));
280 307
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 TEST_F(AudioRendererHostTest, SetVolume) { 364 TEST_F(AudioRendererHostTest, SetVolume) {
338 Create(false); 365 Create(false);
339 SetVolume(0.5); 366 SetVolume(0.5);
340 Play(); 367 Play();
341 Pause(); 368 Pause();
342 Close(); 369 Close();
343 } 370 }
344 371
345 TEST_F(AudioRendererHostTest, SwitchOutputDevice) { 372 TEST_F(AudioRendererHostTest, SwitchOutputDevice) {
346 Create(false); 373 Create(false);
347 SwitchOutputDevice(kStreamId, kDefaultDeviceID, 374 SwitchOutputDevice(kStreamId, kDefaultDeviceID, kDefaultSecurityOrigin,
348 media::SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS); 375 media::SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS);
349 Close(); 376 Close();
350 } 377 }
351 378
352 TEST_F(AudioRendererHostTest, SwitchOutputDeviceNotAuthorized) { 379 TEST_F(AudioRendererHostTest, SwitchOutputDeviceNotAuthorized) {
353 Create(false); 380 Create(false);
354 SwitchOutputDevice(kStreamId, kBadDeviceID, 381 SwitchOutputDevice(kStreamId, kBadDeviceID, kSecurityOrigin,
355 media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_AUTHORIZED); 382 media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_AUTHORIZED);
356 Close(); 383 Close();
357 } 384 }
358 385
359 TEST_F(AudioRendererHostTest, SwitchOutputDeviceNoStream) { 386 TEST_F(AudioRendererHostTest, SwitchOutputDeviceNoStream) {
360 Create(false); 387 Create(false);
361 SwitchOutputDevice(kBadStreamId, kDefaultDeviceID, 388 SwitchOutputDevice(kBadStreamId, kDefaultDeviceID, kDefaultSecurityOrigin,
362 media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE); 389 media::SWITCH_OUTPUT_DEVICE_RESULT_ERROR_INTERNAL);
363 Close(); 390 Close();
364 } 391 }
365 392
366 // Simulate the case where a stream is not properly closed. 393 // Simulate the case where a stream is not properly closed.
367 TEST_F(AudioRendererHostTest, CreatePlayAndShutdown) { 394 TEST_F(AudioRendererHostTest, CreatePlayAndShutdown) {
368 Create(false); 395 Create(false);
369 Play(); 396 Play();
370 } 397 }
371 398
372 // Simulate the case where a stream is not properly closed. 399 // Simulate the case where a stream is not properly closed.
(...skipping 20 matching lines...) Expand all
393 } 420 }
394 421
395 TEST_F(AudioRendererHostTest, CreateUnifiedStreamAndClose) { 422 TEST_F(AudioRendererHostTest, CreateUnifiedStreamAndClose) {
396 Create(true); 423 Create(true);
397 Close(); 424 Close();
398 } 425 }
399 426
400 // TODO(hclam): Add tests for data conversation in low latency mode. 427 // TODO(hclam): Add tests for data conversation in low latency mode.
401 428
402 } // namespace content 429 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698