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

Side by Side Diff: media/audio/audio_output_device_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: Revert DCHECK in destructor 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 <vector> 5 #include <vector>
6 6
7 #include "base/at_exit.h" 7 #include "base/at_exit.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/memory/shared_memory.h" 10 #include "base/memory/shared_memory.h"
(...skipping 17 matching lines...) Expand all
28 using testing::Invoke; 28 using testing::Invoke;
29 using testing::Return; 29 using testing::Return;
30 using testing::WithArgs; 30 using testing::WithArgs;
31 using testing::StrictMock; 31 using testing::StrictMock;
32 using testing::Values; 32 using testing::Values;
33 33
34 namespace media { 34 namespace media {
35 35
36 namespace { 36 namespace {
37 37
38 const std::string kNonDefaultDeviceId("fake-device-id");
39
38 class MockRenderCallback : public AudioRendererSink::RenderCallback { 40 class MockRenderCallback : public AudioRendererSink::RenderCallback {
39 public: 41 public:
40 MockRenderCallback() {} 42 MockRenderCallback() {}
41 virtual ~MockRenderCallback() {} 43 virtual ~MockRenderCallback() {}
42 44
43 MOCK_METHOD2(Render, int(AudioBus* dest, int audio_delay_milliseconds)); 45 MOCK_METHOD2(Render, int(AudioBus* dest, int audio_delay_milliseconds));
44 MOCK_METHOD0(OnRenderError, void()); 46 MOCK_METHOD0(OnRenderError, void());
45 }; 47 };
46 48
47 class MockAudioOutputIPC : public AudioOutputIPC { 49 class MockAudioOutputIPC : public AudioOutputIPC {
48 public: 50 public:
49 MockAudioOutputIPC() {} 51 MockAudioOutputIPC() {}
50 virtual ~MockAudioOutputIPC() {} 52 virtual ~MockAudioOutputIPC() {}
51 53
52 MOCK_METHOD3(CreateStream, void(AudioOutputIPCDelegate* delegate, 54 MOCK_METHOD4(RequestDeviceAuthorization,
53 const AudioParameters& params, 55 void(AudioOutputIPCDelegate* delegate,
54 int session_id)); 56 int session_id,
57 const std::string& device_id,
58 const GURL& security_origin));
59 MOCK_METHOD2(CreateStream,
60 void(AudioOutputIPCDelegate* delegate,
61 const AudioParameters& params));
55 MOCK_METHOD0(PlayStream, void()); 62 MOCK_METHOD0(PlayStream, void());
56 MOCK_METHOD0(PauseStream, void()); 63 MOCK_METHOD0(PauseStream, void());
57 MOCK_METHOD0(CloseStream, void()); 64 MOCK_METHOD0(CloseStream, void());
58 MOCK_METHOD1(SetVolume, void(double volume)); 65 MOCK_METHOD1(SetVolume, void(double volume));
59 MOCK_METHOD3(SwitchOutputDevice, 66 MOCK_METHOD2(SwitchOutputDevice,
60 void(const std::string& device_id, 67 void(const std::string& device_id, const GURL& security_origin));
61 const GURL& security_origin,
62 int request_id));
63 }; 68 };
64 69
65 class MockSwitchOutputDeviceCallback { 70 class MockSwitchOutputDeviceCallback {
66 public: 71 public:
67 MOCK_METHOD1(Callback, void(media::SwitchOutputDeviceResult result)); 72 MOCK_METHOD1(Callback, void(media::SwitchOutputDeviceResult result));
68 }; 73 };
69 74
70 ACTION_P2(SendPendingBytes, socket, pending_bytes) { 75 ACTION_P2(SendPendingBytes, socket, pending_bytes) {
71 socket->Send(&pending_bytes, sizeof(pending_bytes)); 76 socket->Send(&pending_bytes, sizeof(pending_bytes));
72 } 77 }
73 78
74 // Used to terminate a loop from a different thread than the loop belongs to. 79 // Used to terminate a loop from a different thread than the loop belongs to.
75 // |task_runner| should be a SingleThreadTaskRunner. 80 // |task_runner| should be a SingleThreadTaskRunner.
76 ACTION_P(QuitLoop, task_runner) { 81 ACTION_P(QuitLoop, task_runner) {
77 task_runner->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); 82 task_runner->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
78 } 83 }
79 84
80 } // namespace. 85 } // namespace.
81 86
82 class AudioOutputDeviceTest 87 class AudioOutputDeviceTest
83 : public testing::Test, 88 : public testing::Test,
84 public testing::WithParamInterface<bool> { 89 public testing::WithParamInterface<bool> {
85 public: 90 public:
86 AudioOutputDeviceTest(); 91 AudioOutputDeviceTest();
87 ~AudioOutputDeviceTest(); 92 ~AudioOutputDeviceTest();
88 93
94 void ReceiveAuthorization();
89 void StartAudioDevice(); 95 void StartAudioDevice();
90 void CreateStream(); 96 void CreateStream();
91 void ExpectRenderCallback(); 97 void ExpectRenderCallback();
92 void WaitUntilRenderCallback(); 98 void WaitUntilRenderCallback();
93 void StopAudioDevice(); 99 void StopAudioDevice();
94 void SwitchOutputDevice(); 100 void SwitchOutputDevice();
101 void SetDevice(const std::string& device_id);
95 102
96 protected: 103 protected:
97 // Used to clean up TLS pointers that the test(s) will initialize. 104 // Used to clean up TLS pointers that the test(s) will initialize.
98 // Must remain the first member of this class. 105 // Must remain the first member of this class.
99 base::ShadowingAtExitManager at_exit_manager_; 106 base::ShadowingAtExitManager at_exit_manager_;
100 base::MessageLoopForIO io_loop_; 107 base::MessageLoopForIO io_loop_;
101 AudioParameters default_audio_parameters_; 108 AudioParameters default_audio_parameters_;
102 StrictMock<MockRenderCallback> callback_; 109 StrictMock<MockRenderCallback> callback_;
103 MockAudioOutputIPC* audio_output_ipc_; // owned by audio_device_ 110 MockAudioOutputIPC* audio_output_ipc_; // owned by audio_device_
104 scoped_refptr<AudioOutputDevice> audio_device_; 111 scoped_refptr<AudioOutputDevice> audio_device_;
(...skipping 11 matching lines...) Expand all
116 }; 123 };
117 124
118 int AudioOutputDeviceTest::CalculateMemorySize() { 125 int AudioOutputDeviceTest::CalculateMemorySize() {
119 // Calculate output memory size. 126 // Calculate output memory size.
120 return AudioBus::CalculateMemorySize(default_audio_parameters_); 127 return AudioBus::CalculateMemorySize(default_audio_parameters_);
121 } 128 }
122 129
123 AudioOutputDeviceTest::AudioOutputDeviceTest() { 130 AudioOutputDeviceTest::AudioOutputDeviceTest() {
124 default_audio_parameters_.Reset(AudioParameters::AUDIO_PCM_LINEAR, 131 default_audio_parameters_.Reset(AudioParameters::AUDIO_PCM_LINEAR,
125 CHANNEL_LAYOUT_STEREO, 48000, 16, 1024); 132 CHANNEL_LAYOUT_STEREO, 48000, 16, 1024);
126 133 SetDevice(std::string()); // Use default device
127 audio_output_ipc_ = new MockAudioOutputIPC();
128 audio_device_ = new AudioOutputDevice(
129 scoped_ptr<AudioOutputIPC>(audio_output_ipc_),
130 io_loop_.task_runner());
131
132 audio_device_->Initialize(default_audio_parameters_,
133 &callback_);
134
135 io_loop_.RunUntilIdle();
136 } 134 }
137 135
138 AudioOutputDeviceTest::~AudioOutputDeviceTest() { 136 AudioOutputDeviceTest::~AudioOutputDeviceTest() {
139 audio_device_ = NULL; 137 audio_device_ = NULL;
140 } 138 }
141 139
140 void AudioOutputDeviceTest::SetDevice(const std::string& device_id) {
141 audio_output_ipc_ = new MockAudioOutputIPC();
142 audio_device_ =
143 new AudioOutputDevice(scoped_ptr<AudioOutputIPC>(audio_output_ipc_),
144 io_loop_.task_runner(), 0, device_id, GURL());
145 audio_device_->RequestDeviceAuthorization();
146 EXPECT_CALL(
147 *audio_output_ipc_,
148 RequestDeviceAuthorization(audio_device_.get(), 0, device_id, GURL()));
149 io_loop_.RunUntilIdle();
150
151 // Simulate response from browser
152 ReceiveAuthorization();
153
154 audio_device_->Initialize(default_audio_parameters_,
155 &callback_);
156 }
157
158 void AudioOutputDeviceTest::ReceiveAuthorization() {
159 audio_device_->OnDeviceAuthorized(true, default_audio_parameters_);
160 io_loop_.RunUntilIdle();
161 }
162
142 void AudioOutputDeviceTest::StartAudioDevice() { 163 void AudioOutputDeviceTest::StartAudioDevice() {
143 audio_device_->Start(); 164 audio_device_->Start();
144 165 EXPECT_CALL(*audio_output_ipc_, CreateStream(audio_device_.get(), _));
145 EXPECT_CALL(*audio_output_ipc_, CreateStream(audio_device_.get(), _, 0));
146 166
147 io_loop_.RunUntilIdle(); 167 io_loop_.RunUntilIdle();
148 } 168 }
149 169
150 void AudioOutputDeviceTest::CreateStream() { 170 void AudioOutputDeviceTest::CreateStream() {
151 const int kMemorySize = CalculateMemorySize(); 171 const int kMemorySize = CalculateMemorySize();
152 172
153 ASSERT_TRUE(shared_memory_.CreateAndMapAnonymous(kMemorySize)); 173 ASSERT_TRUE(shared_memory_.CreateAndMapAnonymous(kMemorySize));
154 memset(shared_memory_.memory(), 0xff, kMemorySize); 174 memset(shared_memory_.memory(), 0xff, kMemorySize);
155 175
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 225
206 void AudioOutputDeviceTest::StopAudioDevice() { 226 void AudioOutputDeviceTest::StopAudioDevice() {
207 audio_device_->Stop(); 227 audio_device_->Stop();
208 228
209 EXPECT_CALL(*audio_output_ipc_, CloseStream()); 229 EXPECT_CALL(*audio_output_ipc_, CloseStream());
210 230
211 io_loop_.RunUntilIdle(); 231 io_loop_.RunUntilIdle();
212 } 232 }
213 233
214 void AudioOutputDeviceTest::SwitchOutputDevice() { 234 void AudioOutputDeviceTest::SwitchOutputDevice() {
215 const GURL security_origin("http://localhost");
216 const std::string device_id;
217 const int request_id = 1;
218
219 // Switch the output device and check that the IPC message is sent 235 // Switch the output device and check that the IPC message is sent
220 EXPECT_CALL(*audio_output_ipc_, 236 EXPECT_CALL(*audio_output_ipc_,
221 SwitchOutputDevice(device_id, security_origin, request_id)); 237 SwitchOutputDevice(kNonDefaultDeviceId, GURL()));
222 audio_device_->SwitchOutputDevice( 238 audio_device_->SwitchOutputDevice(
223 device_id, security_origin, 239 kNonDefaultDeviceId, GURL(),
224 base::Bind(&MockSwitchOutputDeviceCallback::Callback, 240 base::Bind(&MockSwitchOutputDeviceCallback::Callback,
225 base::Unretained(&switch_output_device_callback_))); 241 base::Unretained(&switch_output_device_callback_)));
226 io_loop_.RunUntilIdle(); 242 io_loop_.RunUntilIdle();
227 243
228 // Simulate the reception of a successful response from the browser 244 // Simulate the reception of a successful response from the browser
229 EXPECT_CALL(switch_output_device_callback_, 245 EXPECT_CALL(switch_output_device_callback_,
230 Callback(SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS)); 246 Callback(SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS));
231 audio_device_->OnOutputDeviceSwitched(request_id, 247 audio_device_->OnOutputDeviceSwitched(SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS);
232 SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS);
233 io_loop_.RunUntilIdle(); 248 io_loop_.RunUntilIdle();
234 } 249 }
235 250
236 TEST_P(AudioOutputDeviceTest, Initialize) { 251 TEST_P(AudioOutputDeviceTest, Initialize) {
237 // Tests that the object can be constructed, initialized and destructed 252 // Tests that the object can be constructed, initialized and destructed
238 // without having ever been started/stopped. 253 // without having ever been started.
254 StopAudioDevice();
239 } 255 }
240 256
241 // Calls Start() followed by an immediate Stop() and check for the basic message 257 // Calls Start() followed by an immediate Stop() and check for the basic message
242 // filter messages being sent in that case. 258 // filter messages being sent in that case.
243 TEST_P(AudioOutputDeviceTest, StartStop) { 259 TEST_P(AudioOutputDeviceTest, StartStop) {
244 StartAudioDevice(); 260 StartAudioDevice();
245 StopAudioDevice(); 261 StopAudioDevice();
246 } 262 }
247 263
248 // AudioOutputDevice supports multiple start/stop sequences. 264 // AudioOutputDevice supports multiple start/stop sequences.
(...skipping 27 matching lines...) Expand all
276 StopAudioDevice(); 292 StopAudioDevice();
277 } 293 }
278 294
279 // Switch the output device 295 // Switch the output device
280 TEST_P(AudioOutputDeviceTest, SwitchOutputDevice) { 296 TEST_P(AudioOutputDeviceTest, SwitchOutputDevice) {
281 StartAudioDevice(); 297 StartAudioDevice();
282 SwitchOutputDevice(); 298 SwitchOutputDevice();
283 StopAudioDevice(); 299 StopAudioDevice();
284 } 300 }
285 301
302 // Full test with output only with nondefault device.
303 TEST_P(AudioOutputDeviceTest, NonDefaultCreateStream) {
304 SetDevice(kNonDefaultDeviceId);
305 StartAudioDevice();
306 ExpectRenderCallback();
307 CreateStream();
308 WaitUntilRenderCallback();
309 StopAudioDevice();
310 }
311
312 // Multiple start/stop with nondefault device
313 TEST_P(AudioOutputDeviceTest, NonDefaultStartStopStartStop) {
314 SetDevice(kNonDefaultDeviceId);
315 StartAudioDevice();
316 StopAudioDevice();
317
318 EXPECT_CALL(*audio_output_ipc_,
319 RequestDeviceAuthorization(audio_device_.get(), 0, _, _));
320 StartAudioDevice();
321 // Simulate reply from browser
322 ReceiveAuthorization();
323
324 StopAudioDevice();
325 }
326
286 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); 327 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false));
287 328
288 } // namespace media. 329 } // namespace media.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698