OLD | NEW |
---|---|
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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 ACTION_P2(SendPendingBytes, socket, pending_bytes) { | 104 ACTION_P2(SendPendingBytes, socket, pending_bytes) { |
105 socket->Send(&pending_bytes, sizeof(pending_bytes)); | 105 socket->Send(&pending_bytes, sizeof(pending_bytes)); |
106 } | 106 } |
107 | 107 |
108 // Used to terminate a loop from a different thread than the loop belongs to. | 108 // Used to terminate a loop from a different thread than the loop belongs to. |
109 // |loop| should be a MessageLoopProxy. | 109 // |loop| should be a MessageLoopProxy. |
110 ACTION_P(QuitLoop, loop) { | 110 ACTION_P(QuitLoop, loop) { |
111 loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 111 loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
112 } | 112 } |
113 | 113 |
114 // Zeros out all the buffers in the |audio_data| vector. | |
115 void FillData(const std::vector<float*>& audio_data, | |
116 int number_of_frames, | |
henrika (OOO until Aug 14)
2012/07/23 11:17:07
Odd indentation. int is not aligned with const.
tommi (sloooow) - chröme
2012/07/23 12:23:10
Done.
| |
117 int audio_delay_milliseconds) { | |
118 std::vector<float*>::const_iterator it = audio_data.begin(); | |
119 for (; it != audio_data.end(); ++it) { | |
120 float* channel = *it; | |
121 for (int j = 0; j < number_of_frames; ++j) { | |
122 channel[j] = 0.0f; | |
123 } | |
124 } | |
125 } | |
126 | |
114 } // namespace. | 127 } // namespace. |
115 | 128 |
116 class AudioDeviceTest : public testing::Test { | 129 class AudioDeviceTest : public testing::Test { |
117 public: | 130 public: |
118 AudioDeviceTest() | 131 AudioDeviceTest() |
119 : default_audio_parameters_(media::AudioParameters::AUDIO_PCM_LINEAR, | 132 : default_audio_parameters_(media::AudioParameters::AUDIO_PCM_LINEAR, |
120 CHANNEL_LAYOUT_STEREO, | 133 CHANNEL_LAYOUT_STEREO, |
121 48000, 16, 1024), | 134 48000, 16, 1024), |
122 stream_id_(-1) { | 135 stream_id_(-1) { |
123 } | 136 } |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 // stream id. | 203 // stream id. |
191 ASSERT_NE(stream_id_, -1); | 204 ASSERT_NE(stream_id_, -1); |
192 | 205 |
193 // This is where it gets a bit hacky. The shared memory contract between | 206 // This is where it gets a bit hacky. The shared memory contract between |
194 // AudioDevice and its browser side counter part includes a bit more than | 207 // AudioDevice and its browser side counter part includes a bit more than |
195 // just the audio data, so we must call TotalSharedMemorySizeInBytes() to get | 208 // just the audio data, so we must call TotalSharedMemorySizeInBytes() to get |
196 // the actual size needed to fit the audio data plus the extra data. | 209 // the actual size needed to fit the audio data plus the extra data. |
197 int memory_size = media::TotalSharedMemorySizeInBytes( | 210 int memory_size = media::TotalSharedMemorySizeInBytes( |
198 default_audio_parameters_.GetBytesPerBuffer()); | 211 default_audio_parameters_.GetBytesPerBuffer()); |
199 SharedMemory shared_memory; | 212 SharedMemory shared_memory; |
200 ASSERT_TRUE(shared_memory.CreateAnonymous(memory_size)); | 213 ASSERT_TRUE(shared_memory.CreateAndMapAnonymous(memory_size)); |
214 // Initialize the memory. | |
215 memset(shared_memory.memory(), 0xff, memory_size); | |
201 | 216 |
202 CancelableSyncSocket browser_socket, renderer_socket; | 217 CancelableSyncSocket browser_socket, renderer_socket; |
203 ASSERT_TRUE(CancelableSyncSocket::CreatePair(&browser_socket, | 218 ASSERT_TRUE(CancelableSyncSocket::CreatePair(&browser_socket, |
204 &renderer_socket)); | 219 &renderer_socket)); |
205 | 220 |
206 // Create duplicates of the handles we pass to AudioDevice since ownership | 221 // Create duplicates of the handles we pass to AudioDevice since ownership |
207 // will be transferred and AudioDevice is responsible for freeing. | 222 // will be transferred and AudioDevice is responsible for freeing. |
208 SyncSocket::Handle audio_device_socket = SyncSocket::kInvalidHandle; | 223 SyncSocket::Handle audio_device_socket = SyncSocket::kInvalidHandle; |
209 ASSERT_TRUE(DuplicateSocketHandle(renderer_socket.handle(), | 224 ASSERT_TRUE(DuplicateSocketHandle(renderer_socket.handle(), |
210 &audio_device_socket)); | 225 &audio_device_socket)); |
(...skipping 10 matching lines...) Expand all Loading... | |
221 | 236 |
222 // We expect calls to our audio renderer callback, which returns the number | 237 // We expect calls to our audio renderer callback, which returns the number |
223 // of frames written to the memory section. | 238 // of frames written to the memory section. |
224 // Here's the second place where it gets hacky: There's no way for us to | 239 // Here's the second place where it gets hacky: There's no way for us to |
225 // know (without using a sleep loop!) when the AudioDevice has finished | 240 // know (without using a sleep loop!) when the AudioDevice has finished |
226 // writing the interleaved audio data into the shared memory section. | 241 // writing the interleaved audio data into the shared memory section. |
227 // So, for the sake of this test, we consider the call to Render a sign | 242 // So, for the sake of this test, we consider the call to Render a sign |
228 // of success and quit the loop. | 243 // of success and quit the loop. |
229 EXPECT_CALL(callback_, Render(_, _, _)) | 244 EXPECT_CALL(callback_, Render(_, _, _)) |
230 .WillOnce(DoAll( | 245 .WillOnce(DoAll( |
246 WithArgs<0, 1, 2>(Invoke(&FillData)), | |
henrika (OOO until Aug 14)
2012/07/23 11:17:07
Mind adding a comment?
tommi (sloooow) - chröme
2012/07/23 12:23:10
Done.
| |
231 QuitLoop(io_loop_.message_loop_proxy()), | 247 QuitLoop(io_loop_.message_loop_proxy()), |
232 Return(1))); | 248 Return(1))); |
henrika (OOO until Aug 14)
2012/07/23 11:17:07
Why 1 here?
| |
233 | 249 |
234 audio_device->OnStreamCreated(duplicated_memory_handle, audio_device_socket, | 250 audio_device->OnStreamCreated(duplicated_memory_handle, audio_device_socket, |
235 memory_size); | 251 memory_size); |
236 | 252 |
237 io_loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), | 253 io_loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), |
238 TestTimeouts::action_timeout()); | 254 TestTimeouts::action_timeout()); |
239 io_loop_.Run(); | 255 io_loop_.Run(); |
240 | 256 |
241 // Close the stream sequence. | 257 // Close the stream sequence. |
242 | 258 |
243 EXPECT_CALL(*audio_message_filter_, OnCloseStream(stream_id_)); | 259 EXPECT_CALL(*audio_message_filter_, OnCloseStream(stream_id_)); |
244 | 260 |
245 audio_device->Stop(); | 261 audio_device->Stop(); |
246 io_loop_.RunAllPending(); | 262 io_loop_.RunAllPending(); |
247 } | 263 } |
OLD | NEW |