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 <list> | 5 #include <list> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
11 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
12 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
13 #include "media/audio/audio_io.h" | 12 #include "media/audio/audio_io.h" |
14 #include "media/audio/simple_sources.h" | 13 #include "media/audio/simple_sources.h" |
15 #include "media/audio/virtual_audio_input_stream.h" | 14 #include "media/audio/virtual_audio_input_stream.h" |
16 #include "media/audio/virtual_audio_output_stream.h" | 15 #include "media/audio/virtual_audio_output_stream.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
19 | 18 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 } // namespace | 96 } // namespace |
98 | 97 |
99 class VirtualAudioInputStreamTest : public testing::TestWithParam<bool> { | 98 class VirtualAudioInputStreamTest : public testing::TestWithParam<bool> { |
100 public: | 99 public: |
101 VirtualAudioInputStreamTest() | 100 VirtualAudioInputStreamTest() |
102 : audio_thread_(new base::Thread("AudioThread")), | 101 : audio_thread_(new base::Thread("AudioThread")), |
103 worker_thread_(new base::Thread("AudioWorkerThread")), | 102 worker_thread_(new base::Thread("AudioWorkerThread")), |
104 stream_(NULL), | 103 stream_(NULL), |
105 closed_stream_(false, false) { | 104 closed_stream_(false, false) { |
106 audio_thread_->Start(); | 105 audio_thread_->Start(); |
107 audio_message_loop_ = audio_thread_->message_loop_proxy(); | 106 audio_task_runner_ = audio_thread_->message_loop_proxy(); |
108 } | 107 } |
109 | 108 |
110 virtual ~VirtualAudioInputStreamTest() { | 109 virtual ~VirtualAudioInputStreamTest() { |
111 SyncWithAudioThread(); | 110 SyncWithAudioThread(); |
112 | 111 |
113 DCHECK(output_streams_.empty()); | 112 DCHECK(output_streams_.empty()); |
114 DCHECK(stopped_output_streams_.empty()); | 113 DCHECK(stopped_output_streams_.empty()); |
115 } | 114 } |
116 | 115 |
117 void Create() { | 116 void Create() { |
118 const bool worker_is_separate_thread = GetParam(); | 117 const bool worker_is_separate_thread = GetParam(); |
119 stream_ = new VirtualAudioInputStream( | 118 stream_ = new VirtualAudioInputStream( |
120 kParams, GetWorkerLoop(worker_is_separate_thread), | 119 kParams, GetWorkerTaskRunner(worker_is_separate_thread), |
121 base::Bind(&base::DeletePointer<VirtualAudioInputStream>)); | 120 base::Bind(&base::DeletePointer<VirtualAudioInputStream>)); |
122 stream_->Open(); | 121 stream_->Open(); |
123 } | 122 } |
124 | 123 |
125 void Start() { | 124 void Start() { |
126 EXPECT_CALL(input_callback_, OnData(_, NotNull(), _, _, _)) | 125 EXPECT_CALL(input_callback_, OnData(_, NotNull(), _, _, _)) |
127 .Times(AtLeast(1)); | 126 .Times(AtLeast(1)); |
128 | 127 |
129 ASSERT_TRUE(!!stream_); | 128 ASSERT_TRUE(!!stream_); |
130 stream_->Start(&input_callback_); | 129 stream_->Start(&input_callback_); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 void RestartAllStoppedOutputStreams() { | 199 void RestartAllStoppedOutputStreams() { |
201 typedef std::list<AudioOutputStream*>::const_iterator ConstIter; | 200 typedef std::list<AudioOutputStream*>::const_iterator ConstIter; |
202 for (ConstIter it = stopped_output_streams_.begin(); | 201 for (ConstIter it = stopped_output_streams_.begin(); |
203 it != stopped_output_streams_.end(); ++it) { | 202 it != stopped_output_streams_.end(); ++it) { |
204 (*it)->Start(&source_); | 203 (*it)->Start(&source_); |
205 output_streams_.push_back(*it); | 204 output_streams_.push_back(*it); |
206 } | 205 } |
207 stopped_output_streams_.clear(); | 206 stopped_output_streams_.clear(); |
208 } | 207 } |
209 | 208 |
210 const scoped_refptr<base::MessageLoopProxy>& audio_message_loop() const { | 209 const scoped_refptr<base::SingleThreadTaskRunner>& audio_task_runner() const { |
211 return audio_message_loop_; | 210 return audio_task_runner_; |
212 } | 211 } |
213 | 212 |
214 const scoped_refptr<base::MessageLoopProxy>& GetWorkerLoop( | 213 const scoped_refptr<base::SingleThreadTaskRunner>& GetWorkerTaskRunner( |
215 bool worker_is_separate_thread) { | 214 bool worker_is_separate_thread) { |
216 if (worker_is_separate_thread) { | 215 if (worker_is_separate_thread) { |
217 if (!worker_thread_->IsRunning()) { | 216 if (!worker_thread_->IsRunning()) { |
218 worker_thread_->Start(); | 217 worker_thread_->Start(); |
219 worker_message_loop_ = worker_thread_->message_loop_proxy(); | 218 worker_task_runner_ = worker_thread_->message_loop_proxy(); |
220 } | 219 } |
221 return worker_message_loop_; | 220 return worker_task_runner_; |
222 } else { | 221 } else { |
223 return audio_message_loop_; | 222 return audio_task_runner_; |
224 } | 223 } |
225 } | 224 } |
226 | 225 |
227 private: | 226 private: |
228 void SyncWithAudioThread() { | 227 void SyncWithAudioThread() { |
229 base::WaitableEvent done(false, false); | 228 base::WaitableEvent done(false, false); |
230 audio_message_loop_->PostTask( | 229 audio_task_runner_->PostTask( |
231 FROM_HERE, | 230 FROM_HERE, |
232 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); | 231 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); |
233 done.Wait(); | 232 done.Wait(); |
234 } | 233 } |
235 | 234 |
236 scoped_ptr<base::Thread> audio_thread_; | 235 scoped_ptr<base::Thread> audio_thread_; |
237 scoped_refptr<base::MessageLoopProxy> audio_message_loop_; | 236 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner_; |
238 scoped_ptr<base::Thread> worker_thread_; | 237 scoped_ptr<base::Thread> worker_thread_; |
239 scoped_refptr<base::MessageLoopProxy> worker_message_loop_; | 238 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; |
240 | 239 |
241 VirtualAudioInputStream* stream_; | 240 VirtualAudioInputStream* stream_; |
242 MockInputCallback input_callback_; | 241 MockInputCallback input_callback_; |
243 base::WaitableEvent closed_stream_; | 242 base::WaitableEvent closed_stream_; |
244 | 243 |
245 std::list<AudioOutputStream*> output_streams_; | 244 std::list<AudioOutputStream*> output_streams_; |
246 std::list<AudioOutputStream*> stopped_output_streams_; | 245 std::list<AudioOutputStream*> stopped_output_streams_; |
247 TestAudioSource source_; | 246 TestAudioSource source_; |
248 | 247 |
249 DISALLOW_COPY_AND_ASSIGN(VirtualAudioInputStreamTest); | 248 DISALLOW_COPY_AND_ASSIGN(VirtualAudioInputStreamTest); |
250 }; | 249 }; |
251 | 250 |
252 #define RUN_ON_AUDIO_THREAD(method) \ | 251 #define RUN_ON_AUDIO_THREAD(method) \ |
253 audio_message_loop()->PostTask( \ | 252 audio_task_runner()->PostTask( \ |
254 FROM_HERE, base::Bind(&VirtualAudioInputStreamTest::method, \ | 253 FROM_HERE, base::Bind(&VirtualAudioInputStreamTest::method, \ |
255 base::Unretained(this))) | 254 base::Unretained(this))) |
256 | 255 |
257 TEST_P(VirtualAudioInputStreamTest, CreateAndClose) { | 256 TEST_P(VirtualAudioInputStreamTest, CreateAndClose) { |
258 RUN_ON_AUDIO_THREAD(Create); | 257 RUN_ON_AUDIO_THREAD(Create); |
259 RUN_ON_AUDIO_THREAD(Close); | 258 RUN_ON_AUDIO_THREAD(Close); |
260 WaitUntilClosed(); | 259 WaitUntilClosed(); |
261 } | 260 } |
262 | 261 |
263 TEST_P(VirtualAudioInputStreamTest, NoOutputs) { | 262 TEST_P(VirtualAudioInputStreamTest, NoOutputs) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 } | 346 } |
348 RUN_ON_AUDIO_THREAD(Close); | 347 RUN_ON_AUDIO_THREAD(Close); |
349 WaitUntilClosed(); | 348 WaitUntilClosed(); |
350 } | 349 } |
351 | 350 |
352 INSTANTIATE_TEST_CASE_P(SingleVersusMultithreaded, | 351 INSTANTIATE_TEST_CASE_P(SingleVersusMultithreaded, |
353 VirtualAudioInputStreamTest, | 352 VirtualAudioInputStreamTest, |
354 ::testing::Values(false, true)); | 353 ::testing::Values(false, true)); |
355 | 354 |
356 } // namespace media | 355 } // namespace media |
OLD | NEW |