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

Side by Side Diff: media/audio/audio_output_device_unittest.cc

Issue 12383016: Merge AssociateStreamWithProducer message into CreateStream message for both audio output and input. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 9 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) 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/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/process_util.h" 9 #include "base/process_util.h"
10 #include "base/shared_memory.h" 10 #include "base/shared_memory.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 AudioBus* dest, 42 AudioBus* dest,
43 int audio_delay_milliseconds)); 43 int audio_delay_milliseconds));
44 MOCK_METHOD0(OnRenderError, void()); 44 MOCK_METHOD0(OnRenderError, void());
45 }; 45 };
46 46
47 class MockAudioOutputIPC : public AudioOutputIPC { 47 class MockAudioOutputIPC : public AudioOutputIPC {
48 public: 48 public:
49 MockAudioOutputIPC() {} 49 MockAudioOutputIPC() {}
50 virtual ~MockAudioOutputIPC() {} 50 virtual ~MockAudioOutputIPC() {}
51 51
52 MOCK_METHOD1(AddDelegate, int(AudioOutputIPCDelegate* delegate)); 52 MOCK_METHOD2(CreateStream, void(AudioOutputIPCDelegate* delegate,
53 MOCK_METHOD1(RemoveDelegate, void(int stream_id)); 53 const AudioParameters& params));
54 54 MOCK_METHOD0(PlayStream, void());
55 MOCK_METHOD2(CreateStream, 55 MOCK_METHOD0(PauseStream, void());
56 void(int stream_id, const AudioParameters& params)); 56 MOCK_METHOD0(FlushStream, void());
57 MOCK_METHOD1(PlayStream, void(int stream_id)); 57 MOCK_METHOD0(CloseStream, void());
58 MOCK_METHOD1(CloseStream, void(int stream_id)); 58 MOCK_METHOD1(SetVolume, void(double volume));
59 MOCK_METHOD2(SetVolume, void(int stream_id, double volume));
60 MOCK_METHOD1(PauseStream, void(int stream_id));
61 MOCK_METHOD1(FlushStream, void(int stream_id));
62 }; 59 };
63 60
64 // Creates a copy of a SyncSocket handle that we can give to AudioOutputDevice. 61 // Creates a copy of a SyncSocket handle that we can give to AudioOutputDevice.
65 // On Windows this means duplicating the pipe handle so that AudioOutputDevice 62 // On Windows this means duplicating the pipe handle so that AudioOutputDevice
66 // can call CloseHandle() (since ownership has been transferred), but on other 63 // can call CloseHandle() (since ownership has been transferred), but on other
67 // platforms, we just copy the same socket handle since AudioOutputDevice on 64 // platforms, we just copy the same socket handle since AudioOutputDevice on
68 // those platforms won't actually own the socket (FileDescriptor.auto_close is 65 // those platforms won't actually own the socket (FileDescriptor.auto_close is
69 // false). 66 // false).
70 bool DuplicateSocketHandle(SyncSocket::Handle socket_handle, 67 bool DuplicateSocketHandle(SyncSocket::Handle socket_handle,
71 SyncSocket::Handle* copy) { 68 SyncSocket::Handle* copy) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 void WaitUntilRenderCallback(); 102 void WaitUntilRenderCallback();
106 void StopAudioDevice(); 103 void StopAudioDevice();
107 104
108 protected: 105 protected:
109 // Used to clean up TLS pointers that the test(s) will initialize. 106 // Used to clean up TLS pointers that the test(s) will initialize.
110 // Must remain the first member of this class. 107 // Must remain the first member of this class.
111 base::ShadowingAtExitManager at_exit_manager_; 108 base::ShadowingAtExitManager at_exit_manager_;
112 MessageLoopForIO io_loop_; 109 MessageLoopForIO io_loop_;
113 AudioParameters default_audio_parameters_; 110 AudioParameters default_audio_parameters_;
114 StrictMock<MockRenderCallback> callback_; 111 StrictMock<MockRenderCallback> callback_;
115 StrictMock<MockAudioOutputIPC> audio_output_ipc_; 112 MockAudioOutputIPC* audio_output_ipc_; // owned by audio_device_
116 scoped_refptr<AudioOutputDevice> audio_device_; 113 scoped_refptr<AudioOutputDevice> audio_device_;
117 114
118 private: 115 private:
119 int CalculateMemorySize(); 116 int CalculateMemorySize();
120 117
121 const bool synchronized_io_; 118 const bool synchronized_io_;
122 const int input_channels_; 119 const int input_channels_;
123 SharedMemory shared_memory_; 120 SharedMemory shared_memory_;
124 CancelableSyncSocket browser_socket_; 121 CancelableSyncSocket browser_socket_;
125 CancelableSyncSocket renderer_socket_; 122 CancelableSyncSocket renderer_socket_;
(...skipping 22 matching lines...) Expand all
148 } 145 }
149 146
150 AudioOutputDeviceTest::AudioOutputDeviceTest() 147 AudioOutputDeviceTest::AudioOutputDeviceTest()
151 : synchronized_io_(GetParam()), 148 : synchronized_io_(GetParam()),
152 input_channels_(synchronized_io_ ? 2 : 0) { 149 input_channels_(synchronized_io_ ? 2 : 0) {
153 default_audio_parameters_.Reset( 150 default_audio_parameters_.Reset(
154 AudioParameters::AUDIO_PCM_LINEAR, 151 AudioParameters::AUDIO_PCM_LINEAR,
155 CHANNEL_LAYOUT_STEREO, input_channels_, 152 CHANNEL_LAYOUT_STEREO, input_channels_,
156 48000, 16, 1024); 153 48000, 16, 1024);
157 154
155 audio_output_ipc_ = new MockAudioOutputIPC();
158 audio_device_ = new AudioOutputDevice( 156 audio_device_ = new AudioOutputDevice(
159 &audio_output_ipc_, io_loop_.message_loop_proxy()); 157 scoped_ptr<AudioOutputIPC>(audio_output_ipc_),
158 io_loop_.message_loop_proxy());
160 159
161 audio_device_->Initialize(default_audio_parameters_, 160 audio_device_->Initialize(default_audio_parameters_,
162 &callback_); 161 &callback_);
163 162
164 io_loop_.RunUntilIdle(); 163 io_loop_.RunUntilIdle();
165 } 164 }
166 165
167 AudioOutputDeviceTest::~AudioOutputDeviceTest() { 166 AudioOutputDeviceTest::~AudioOutputDeviceTest() {
168 audio_device_ = NULL; 167 audio_device_ = NULL;
169 } 168 }
170 169
171 void AudioOutputDeviceTest::StartAudioDevice() { 170 void AudioOutputDeviceTest::StartAudioDevice() {
172 audio_device_->Start(); 171 audio_device_->Start();
173 172
174 EXPECT_CALL(audio_output_ipc_, AddDelegate(_)).WillOnce(Return(kStreamId)); 173 EXPECT_CALL(*audio_output_ipc_, CreateStream(audio_device_.get(), _));
175 EXPECT_CALL(audio_output_ipc_, CreateStream(kStreamId, _));
176 174
177 io_loop_.RunUntilIdle(); 175 io_loop_.RunUntilIdle();
178 } 176 }
179 177
180 void AudioOutputDeviceTest::CreateStream() { 178 void AudioOutputDeviceTest::CreateStream() {
181 const int kMemorySize = CalculateMemorySize(); 179 const int kMemorySize = CalculateMemorySize();
182 180
183 ASSERT_TRUE(shared_memory_.CreateAndMapAnonymous(kMemorySize)); 181 ASSERT_TRUE(shared_memory_.CreateAndMapAnonymous(kMemorySize));
184 memset(shared_memory_.memory(), 0xff, kMemorySize); 182 memset(shared_memory_.memory(), 0xff, kMemorySize);
185 183
(...skipping 15 matching lines...) Expand all
201 io_loop_.RunUntilIdle(); 199 io_loop_.RunUntilIdle();
202 } 200 }
203 201
204 void AudioOutputDeviceTest::ExpectRenderCallback() { 202 void AudioOutputDeviceTest::ExpectRenderCallback() {
205 // We should get a 'play' notification when we call OnStreamCreated(). 203 // We should get a 'play' notification when we call OnStreamCreated().
206 // Respond by asking for some audio data. This should ask our callback 204 // Respond by asking for some audio data. This should ask our callback
207 // to provide some audio data that AudioOutputDevice then writes into the 205 // to provide some audio data that AudioOutputDevice then writes into the
208 // shared memory section. 206 // shared memory section.
209 const int kMemorySize = CalculateMemorySize(); 207 const int kMemorySize = CalculateMemorySize();
210 208
211 EXPECT_CALL(audio_output_ipc_, PlayStream(kStreamId)) 209 EXPECT_CALL(*audio_output_ipc_, PlayStream())
212 .WillOnce(SendPendingBytes(&browser_socket_, kMemorySize)); 210 .WillOnce(SendPendingBytes(&browser_socket_, kMemorySize));
213 211
214 // We expect calls to our audio renderer callback, which returns the number 212 // We expect calls to our audio renderer callback, which returns the number
215 // of frames written to the memory section. 213 // of frames written to the memory section.
216 // Here's the second place where it gets hacky: There's no way for us to 214 // Here's the second place where it gets hacky: There's no way for us to
217 // know (without using a sleep loop!) when the AudioOutputDevice has finished 215 // know (without using a sleep loop!) when the AudioOutputDevice has finished
218 // writing the interleaved audio data into the shared memory section. 216 // writing the interleaved audio data into the shared memory section.
219 // So, for the sake of this test, we consider the call to Render a sign 217 // So, for the sake of this test, we consider the call to Render a sign
220 // of success and quit the loop. 218 // of success and quit the loop.
221 if (synchronized_io_) { 219 if (synchronized_io_) {
(...skipping 13 matching lines...) Expand all
235 void AudioOutputDeviceTest::WaitUntilRenderCallback() { 233 void AudioOutputDeviceTest::WaitUntilRenderCallback() {
236 // Don't hang the test if we never get the Render() callback. 234 // Don't hang the test if we never get the Render() callback.
237 io_loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), 235 io_loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(),
238 TestTimeouts::action_timeout()); 236 TestTimeouts::action_timeout());
239 io_loop_.Run(); 237 io_loop_.Run();
240 } 238 }
241 239
242 void AudioOutputDeviceTest::StopAudioDevice() { 240 void AudioOutputDeviceTest::StopAudioDevice() {
243 audio_device_->Stop(); 241 audio_device_->Stop();
244 242
245 EXPECT_CALL(audio_output_ipc_, CloseStream(kStreamId)); 243 EXPECT_CALL(*audio_output_ipc_, CloseStream());
246 EXPECT_CALL(audio_output_ipc_, RemoveDelegate(kStreamId));
247 244
248 io_loop_.RunUntilIdle(); 245 io_loop_.RunUntilIdle();
249 } 246 }
250 247
251 TEST_P(AudioOutputDeviceTest, Initialize) { 248 TEST_P(AudioOutputDeviceTest, Initialize) {
252 // Tests that the object can be constructed, initialized and destructed 249 // Tests that the object can be constructed, initialized and destructed
253 // without having ever been started/stopped. 250 // without having ever been started/stopped.
254 } 251 }
255 252
256 // Calls Start() followed by an immediate Stop() and check for the basic message 253 // Calls Start() followed by an immediate Stop() and check for the basic message
(...skipping 14 matching lines...) Expand all
271 // Simulate receiving OnStreamCreated() prior to processing ShutDownOnIOThread() 268 // Simulate receiving OnStreamCreated() prior to processing ShutDownOnIOThread()
272 // on the IO loop. 269 // on the IO loop.
273 TEST_P(AudioOutputDeviceTest, StopBeforeRender) { 270 TEST_P(AudioOutputDeviceTest, StopBeforeRender) {
274 StartAudioDevice(); 271 StartAudioDevice();
275 272
276 // Call Stop() but don't run the IO loop yet. 273 // Call Stop() but don't run the IO loop yet.
277 audio_device_->Stop(); 274 audio_device_->Stop();
278 275
279 // Expect us to shutdown IPC but not to render anything despite the stream 276 // Expect us to shutdown IPC but not to render anything despite the stream
280 // getting created. 277 // getting created.
281 EXPECT_CALL(audio_output_ipc_, CloseStream(kStreamId)); 278 EXPECT_CALL(*audio_output_ipc_, CloseStream());
282 EXPECT_CALL(audio_output_ipc_, RemoveDelegate(kStreamId));
283 CreateStream(); 279 CreateStream();
284 } 280 }
285 281
286 // Full test with output only. 282 // Full test with output only.
287 TEST_P(AudioOutputDeviceTest, CreateStream) { 283 TEST_P(AudioOutputDeviceTest, CreateStream) {
288 StartAudioDevice(); 284 StartAudioDevice();
289 ExpectRenderCallback(); 285 ExpectRenderCallback();
290 CreateStream(); 286 CreateStream();
291 WaitUntilRenderCallback(); 287 WaitUntilRenderCallback();
292 StopAudioDevice(); 288 StopAudioDevice();
293 } 289 }
294 290
295 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); 291 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false));
296 INSTANTIATE_TEST_CASE_P(RenderIO, AudioOutputDeviceTest, Values(true)); 292 INSTANTIATE_TEST_CASE_P(RenderIO, AudioOutputDeviceTest, Values(true));
297 293
298 } // namespace media. 294 } // namespace media.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698