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

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

Issue 11413078: Tab Audio Capture: Browser-side connect/disconnect functionality. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add done closure to MirroringDestination::RemoveInput(). Created 8 years 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 "base/basictypes.h"
5 #include "base/bind.h" 6 #include "base/bind.h"
6 #include "base/environment.h" 7 #include "base/environment.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h" 11 #include "base/message_loop.h"
10 #include "base/synchronization/waitable_event.h" 12 #include "base/synchronization/waitable_event.h"
11 #include "media/audio/audio_output_controller.h" 13 #include "media/audio/audio_output_controller.h"
14 #include "media/audio/audio_parameters.h"
15 #include "media/base/audio_bus.h"
12 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
14 18
15 // TODO(vrk): These tests need to be rewritten! (crbug.com/112500)
16
17 using ::testing::_; 19 using ::testing::_;
18 using ::testing::AtLeast; 20 using ::testing::AtLeast;
19 using ::testing::DoAll; 21 using ::testing::DoAll;
20 using ::testing::Exactly;
21 using ::testing::InvokeWithoutArgs;
22 using ::testing::NotNull; 22 using ::testing::NotNull;
23 using ::testing::Return; 23 using ::testing::Return;
24 24
25 namespace media { 25 namespace media {
26 26
27 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; 27 static const int kSampleRate = AudioParameters::kAudioCDSampleRate;
28 static const int kBitsPerSample = 16; 28 static const int kBitsPerSample = 16;
29 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; 29 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO;
30 static const int kSamplesPerPacket = kSampleRate / 10; 30 static const int kSamplesPerPacket = kSampleRate / 100;
31 static const int kHardwareBufferSize = kSamplesPerPacket * 31 static const int kHardwareBufferSize = kSamplesPerPacket *
32 ChannelLayoutToChannelCount(kChannelLayout) * kBitsPerSample / 8; 32 ChannelLayoutToChannelCount(kChannelLayout) * kBitsPerSample / 8;
33 33
34 class MockAudioOutputControllerEventHandler 34 class MockAudioOutputControllerEventHandler
35 : public AudioOutputController::EventHandler { 35 : public AudioOutputController::EventHandler {
36 public: 36 public:
37 MockAudioOutputControllerEventHandler() {} 37 MockAudioOutputControllerEventHandler() {}
38 38
39 MOCK_METHOD1(OnCreated, void(AudioOutputController* controller)); 39 MOCK_METHOD1(OnCreated, void(AudioOutputController* controller));
40 MOCK_METHOD1(OnPlaying, void(AudioOutputController* controller)); 40 MOCK_METHOD1(OnPlaying, void(AudioOutputController* controller));
(...skipping 16 matching lines...) Expand all
57 MOCK_METHOD0(DataReady, bool()); 57 MOCK_METHOD0(DataReady, bool());
58 58
59 private: 59 private:
60 DISALLOW_COPY_AND_ASSIGN(MockAudioOutputControllerSyncReader); 60 DISALLOW_COPY_AND_ASSIGN(MockAudioOutputControllerSyncReader);
61 }; 61 };
62 62
63 ACTION_P(SignalEvent, event) { 63 ACTION_P(SignalEvent, event) {
64 event->Signal(); 64 event->Signal();
65 } 65 }
66 66
67 // Custom action to clear a memory buffer. 67 static const float kBufferZeroData = 0.0f;
68 ACTION(ClearBuffer) { 68 static const float kBufferNonZeroData = 1.0f;
69 arg1->Zero(); 69 ACTION_P(PopulateBuffer, value) {
70 } 70 // Note: To confirm the buffer will be populated in these tests, it's
71 71 // sufficient that only the first float in channel 0 is set to the value.
72 // Closes AudioOutputController synchronously. 72 arg1->channel(0)[0] = value;
73 static void CloseAudioController(AudioOutputController* controller) {
74 controller->Close(MessageLoop::QuitClosure());
75 MessageLoop::current()->Run();
76 } 73 }
77 74
78 class AudioOutputControllerTest : public testing::Test { 75 class AudioOutputControllerTest : public testing::Test {
79 public: 76 public:
80 AudioOutputControllerTest() {} 77 AudioOutputControllerTest()
81 virtual ~AudioOutputControllerTest() {} 78 : audio_manager_(AudioManager::Create()),
79 create_event_(false, false),
80 play_event_(false, false),
81 read_event_(false, false),
82 pause_event_(false, false),
83 diverted_callback_(NULL) {
84 }
85
86 virtual ~AudioOutputControllerTest() {
87 ASSERT_FALSE(diverted_callback_);
88 }
82 89
83 protected: 90 protected:
91 // Returns true if a controller was successfully created.
92 bool Create(int samples_per_packet) {
93 params_ = AudioParameters(
94 AudioParameters::AUDIO_FAKE, kChannelLayout,
95 kSampleRate, kBitsPerSample, samples_per_packet);
96 controller_ = AudioOutputController::Create(
97 audio_manager_.get(), &mock_event_handler_, params_,
98 &mock_sync_reader_);
99
100 if (controller_) {
101 EXPECT_FALSE(create_event_.IsSignaled());
102 EXPECT_CALL(mock_event_handler_, OnCreated(NotNull()))
103 .WillOnce(SignalEvent(&create_event_));
104 EXPECT_CALL(mock_sync_reader_, Close());
105 } else {
106 EXPECT_CALL(mock_event_handler_, OnCreated(NotNull()))
107 .Times(0);
108 EXPECT_CALL(mock_sync_reader_, Close())
109 .Times(0);
110 }
111
112 EXPECT_FALSE(play_event_.IsSignaled());
113 EXPECT_FALSE(read_event_.IsSignaled());
114 EXPECT_FALSE(pause_event_.IsSignaled());
115
116 return controller_ != NULL;
117 }
118
119 void Play() {
120 // Expect the event handler to receive one OnPlaying() call.
121 EXPECT_CALL(mock_event_handler_, OnPlaying(NotNull()))
122 .WillOnce(SignalEvent(&play_event_));
123
124 // During playback, the mock pretends to provide audio data rendered and
125 // sent from the render process.
126 EXPECT_CALL(mock_sync_reader_, UpdatePendingBytes(_))
127 .Times(AtLeast(2));
128 EXPECT_CALL(mock_sync_reader_, Read(_, _))
129 .WillRepeatedly(DoAll(PopulateBuffer(kBufferNonZeroData),
130 SignalEvent(&read_event_),
131 Return(params_.frames_per_buffer())));
132 EXPECT_CALL(mock_sync_reader_, DataReady())
133 .WillRepeatedly(Return(true));
134
135 controller_->Play();
136 }
137
138 void Pause() {
139 // Expect the event handler to receive one OnPaused() call.
140 EXPECT_CALL(mock_event_handler_, OnPaused(NotNull()))
141 .WillOnce(SignalEvent(&pause_event_));
142
143 controller_->Pause();
144 }
145
146 void ChangeDevice() {
147 // Expect the event handler to receive one OnPaying() call and no OnPaused()
148 // call.
149 EXPECT_CALL(mock_event_handler_, OnPlaying(NotNull()))
150 .WillOnce(SignalEvent(&play_event_));
151 EXPECT_CALL(mock_event_handler_, OnPaused(NotNull()))
152 .Times(0);
153
154 // Simulate a device change event to AudioOutputController from the
155 // AudioManager.
156 audio_manager_->GetMessageLoop()->PostTask(
157 FROM_HERE,
158 base::Bind(&AudioOutputController::OnDeviceChange, controller_));
159 }
160
161 void Divert() {
162 EXPECT_FALSE(diverted_callback_);
163 diverted_callback_ = controller_->Divert();
164 EXPECT_TRUE(diverted_callback_);
165
166 // Wait for one callback to ensure consumption of audio data was transferred
167 // (via the Glue::PassSoon() scheme) to diverted_callback_ before proceeding
168 // with testing.
169 base::WaitableEvent one_cb(false, false);
170 audio_manager_->GetMessageLoop()->PostTask(
171 FROM_HERE,
172 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&one_cb)));
173 one_cb.Wait();
174 }
175
176 // |expected_buffer_data| is used to differentiate between: 1) expecting to
177 // see audio data rendered from the render process; versus 2) expecting
178 // AudioOutputController to be filling-in zeros (i.e., it is not in a playback
179 // state).
180 void ReadDivertedAudioData(float expected_buffer_data) {
181 ASSERT_TRUE(diverted_callback_);
182 scoped_ptr<AudioBus> dest = AudioBus::Create(params_);
183 const int frames_read =
184 diverted_callback_->OnMoreData(dest.get(), AudioBuffersState());
185 EXPECT_EQ(dest->frames(), frames_read);
186 EXPECT_EQ(expected_buffer_data, dest->channel(0)[0]);
187 }
188
189 void Revert() {
190 EXPECT_TRUE(diverted_callback_);
191 // No OnPlaying() event expected because the divert/revert is invisible to
192 // the normal audio consumer.
193 EXPECT_CALL(mock_event_handler_, OnPlaying(NotNull()))
194 .Times(0);
195 controller_->Revert(diverted_callback_);
196 diverted_callback_ = NULL;
197 }
198
199 void Close() {
200 controller_->Close(MessageLoop::QuitClosure());
201 MessageLoop::current()->Run();
202 }
203
204 // These synchronize the main thread with key events taking place on other
205 // threads.
206 void WaitForCreate() { create_event_.Wait(); }
207 void WaitForPlay() { play_event_.Wait(); }
208 void WaitForReads() {
209 // Note: Arbitrarily chosen, but more iterations causes tests to take
210 // significantly more time.
211 static const int kNumIterations = 3;
212 for (int i = 0; i < kNumIterations; ++i) {
213 read_event_.Wait();
214 }
215 }
216 void WaitForPause() { pause_event_.Wait(); }
217
218 private:
84 MessageLoopForIO message_loop_; 219 MessageLoopForIO message_loop_;
85 220 scoped_ptr<AudioManager> audio_manager_;
86 private: 221 MockAudioOutputControllerEventHandler mock_event_handler_;
222 MockAudioOutputControllerSyncReader mock_sync_reader_;
223 base::WaitableEvent create_event_;
224 base::WaitableEvent play_event_;
225 base::WaitableEvent read_event_;
226 base::WaitableEvent pause_event_;
227 AudioParameters params_;
228 scoped_refptr<AudioOutputController> controller_;
229 AudioOutputStream::AudioSourceCallback* diverted_callback_;
230
87 DISALLOW_COPY_AND_ASSIGN(AudioOutputControllerTest); 231 DISALLOW_COPY_AND_ASSIGN(AudioOutputControllerTest);
88 }; 232 };
89 233
90 TEST_F(AudioOutputControllerTest, CreateAndClose) { 234 TEST_F(AudioOutputControllerTest, CreateAndClose) {
91 scoped_ptr<AudioManager> audio_manager(AudioManager::Create()); 235 ASSERT_TRUE(Create(kSamplesPerPacket));
92 if (!audio_manager->HasAudioOutputDevices()) 236 Close();
93 return; 237 }
94 238
95 MockAudioOutputControllerEventHandler event_handler; 239 TEST_F(AudioOutputControllerTest, HardwareBufferTooLarge) {
96 240 EXPECT_FALSE(Create(kSamplesPerPacket * 1000));
97 EXPECT_CALL(event_handler, OnCreated(NotNull())) 241 }
98 .Times(1); 242
99 243 TEST_F(AudioOutputControllerTest, PlayAndClose) {
100 MockAudioOutputControllerSyncReader sync_reader; 244 ASSERT_TRUE(Create(kSamplesPerPacket));
101 EXPECT_CALL(sync_reader, Close()); 245 WaitForCreate();
102 246 Play();
103 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, 247 WaitForPlay();
104 kSampleRate, kBitsPerSample, kSamplesPerPacket); 248 WaitForReads();
105 scoped_refptr<AudioOutputController> controller = 249 Close();
106 AudioOutputController::Create(
107 audio_manager.get(), &event_handler, params, &sync_reader);
108 ASSERT_TRUE(controller.get());
109
110 // Close the controller immediately.
111 CloseAudioController(controller);
112 } 250 }
113 251
114 TEST_F(AudioOutputControllerTest, PlayPauseClose) { 252 TEST_F(AudioOutputControllerTest, PlayPauseClose) {
115 scoped_ptr<AudioManager> audio_manager(AudioManager::Create()); 253 ASSERT_TRUE(Create(kSamplesPerPacket));
116 if (!audio_manager->HasAudioOutputDevices()) 254 WaitForCreate();
117 return; 255 Play();
118 256 WaitForPlay();
119 MockAudioOutputControllerEventHandler event_handler; 257 WaitForReads();
120 base::WaitableEvent event(false, false); 258 Pause();
121 base::WaitableEvent pause_event(false, false); 259 WaitForPause();
122 260 Close();
123 // If OnCreated is called then signal the event.
124 EXPECT_CALL(event_handler, OnCreated(NotNull()))
125 .WillOnce(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal));
126
127 // OnPlaying() will be called only once.
128 EXPECT_CALL(event_handler, OnPlaying(NotNull()));
129
130 MockAudioOutputControllerSyncReader sync_reader;
131 EXPECT_CALL(sync_reader, UpdatePendingBytes(_))
132 .Times(AtLeast(2));
133 EXPECT_CALL(sync_reader, Read(_, _))
134 .WillRepeatedly(DoAll(ClearBuffer(), SignalEvent(&event),
135 Return(4)));
136 EXPECT_CALL(sync_reader, DataReady())
137 .WillRepeatedly(Return(true));
138 EXPECT_CALL(event_handler, OnPaused(NotNull()))
139 .WillOnce(InvokeWithoutArgs(&pause_event, &base::WaitableEvent::Signal));
140 EXPECT_CALL(sync_reader, Close());
141
142 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout,
143 kSampleRate, kBitsPerSample, kSamplesPerPacket);
144 scoped_refptr<AudioOutputController> controller =
145 AudioOutputController::Create(
146 audio_manager.get(), &event_handler, params, &sync_reader);
147 ASSERT_TRUE(controller.get());
148
149 // Wait for OnCreated() to be called.
150 event.Wait();
151
152 ASSERT_FALSE(pause_event.IsSignaled());
153 controller->Play();
154 controller->Pause();
155 pause_event.Wait();
156
157 // Now stop the controller.
158 CloseAudioController(controller);
159 }
160
161 TEST_F(AudioOutputControllerTest, HardwareBufferTooLarge) {
162 scoped_ptr<AudioManager> audio_manager(AudioManager::Create());
163 if (!audio_manager->HasAudioOutputDevices())
164 return;
165
166 // Create an audio device with a very large hardware buffer size.
167 MockAudioOutputControllerEventHandler event_handler;
168
169 MockAudioOutputControllerSyncReader sync_reader;
170 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout,
171 kSampleRate, kBitsPerSample,
172 kSamplesPerPacket * 1000);
173 scoped_refptr<AudioOutputController> controller =
174 AudioOutputController::Create(
175 audio_manager.get(), &event_handler, params, &sync_reader);
176
177 // Use assert because we don't stop the device and assume we can't
178 // create one.
179 ASSERT_FALSE(controller);
180 } 261 }
181 262
182 TEST_F(AudioOutputControllerTest, PlayPausePlayClose) { 263 TEST_F(AudioOutputControllerTest, PlayPausePlayClose) {
183 scoped_ptr<AudioManager> audio_manager(AudioManager::Create()); 264 ASSERT_TRUE(Create(kSamplesPerPacket));
184 if (!audio_manager->HasAudioOutputDevices()) 265 WaitForCreate();
185 return; 266 Play();
186 267 WaitForPlay();
187 MockAudioOutputControllerEventHandler event_handler; 268 WaitForReads();
188 base::WaitableEvent event(false, false); 269 Pause();
189 EXPECT_CALL(event_handler, OnCreated(NotNull())) 270 WaitForPause();
190 .WillOnce(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal)); 271 Play();
191 272 WaitForPlay();
192 // OnPlaying() will be called only once. 273 Close();
193 base::WaitableEvent play_event(false, false); 274 }
194 EXPECT_CALL(event_handler, OnPlaying(NotNull())) 275
195 .WillOnce(InvokeWithoutArgs(&play_event, &base::WaitableEvent::Signal)); 276 TEST_F(AudioOutputControllerTest, PlayDeviceChangeClose) {
196 277 ASSERT_TRUE(Create(kSamplesPerPacket));
197 // OnPaused() should never be called since the pause during kStarting is 278 WaitForCreate();
198 // dropped when the second play comes in. 279 Play();
199 EXPECT_CALL(event_handler, OnPaused(NotNull())) 280 WaitForPlay();
200 .Times(0); 281 WaitForReads();
201 282 ChangeDevice();
202 MockAudioOutputControllerSyncReader sync_reader; 283 WaitForPlay();
203 EXPECT_CALL(sync_reader, UpdatePendingBytes(_)) 284 WaitForReads();
204 .Times(AtLeast(1)); 285 Close();
205 EXPECT_CALL(sync_reader, Read(_, _)) 286 }
206 .WillRepeatedly(DoAll(ClearBuffer(), SignalEvent(&event), Return(4))); 287
207 EXPECT_CALL(sync_reader, DataReady()) 288 TEST_F(AudioOutputControllerTest, PlayDivertRevertClose) {
208 .WillRepeatedly(Return(true)); 289 ASSERT_TRUE(Create(kSamplesPerPacket));
209 EXPECT_CALL(sync_reader, Close()); 290 WaitForCreate();
210 291 Play();
211 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, 292 WaitForPlay();
212 kSampleRate, kBitsPerSample, kSamplesPerPacket); 293 WaitForReads();
213 scoped_refptr<AudioOutputController> controller = 294 Divert();
214 AudioOutputController::Create( 295 ReadDivertedAudioData(kBufferNonZeroData);
215 audio_manager.get(), &event_handler, params, &sync_reader); 296 Revert();
216 ASSERT_TRUE(controller.get()); 297 WaitForReads();
217 298 Close();
218 // Wait for OnCreated() to be called. 299 }
219 event.Wait(); 300
220 301 TEST_F(AudioOutputControllerTest, DivertPlayPausePlayRevertClose) {
221 ASSERT_FALSE(play_event.IsSignaled()); 302 ASSERT_TRUE(Create(kSamplesPerPacket));
222 controller->Play(); 303 WaitForCreate();
223 controller->Pause(); 304 Divert();
224 controller->Play(); 305 // Read back zeros before playback starts.
225 play_event.Wait(); 306 ReadDivertedAudioData(kBufferZeroData);
226 307 Play();
227 // Now stop the controller. 308 WaitForPlay();
228 CloseAudioController(controller); 309 // Normal read of audio data from renderer.
229 } 310 ReadDivertedAudioData(kBufferNonZeroData);
230 311 Pause();
231 // Ensure state change events are handled. 312 WaitForPause();
232 TEST_F(AudioOutputControllerTest, PlayStateChangeClose) { 313 // Read back zeros while paused.
233 scoped_ptr<AudioManager> audio_manager(AudioManager::Create()); 314 ReadDivertedAudioData(kBufferZeroData);
234 if (!audio_manager->HasAudioOutputDevices()) 315 Play();
235 return; 316 WaitForPlay();
236 317 // Normal read of audio data from renderer again.
237 MockAudioOutputControllerEventHandler event_handler; 318 ReadDivertedAudioData(kBufferNonZeroData);
238 base::WaitableEvent event(false, false); 319 Revert();
239 EXPECT_CALL(event_handler, OnCreated(NotNull())) 320 WaitForReads();
240 .WillOnce(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal)); 321 Close();
241 322 }
242 // OnPlaying() will be called once normally and once after being recreated. 323
243 base::WaitableEvent play_event(false, false); 324 TEST_F(AudioOutputControllerTest, DivertRevertClose) {
244 EXPECT_CALL(event_handler, OnPlaying(NotNull())) 325 ASSERT_TRUE(Create(kSamplesPerPacket));
245 .Times(2) 326 WaitForCreate();
246 .WillRepeatedly(InvokeWithoutArgs( 327 Divert();
247 &play_event, &base::WaitableEvent::Signal)); 328 // Read back zeros since playback was not requested.
248 329 ReadDivertedAudioData(kBufferZeroData);
249 // OnPaused() should not be called during the state change event. 330 Revert();
250 EXPECT_CALL(event_handler, OnPaused(NotNull())) 331 Close();
251 .Times(0);
252
253 MockAudioOutputControllerSyncReader sync_reader;
254 EXPECT_CALL(sync_reader, UpdatePendingBytes(_))
255 .Times(AtLeast(1));
256 EXPECT_CALL(sync_reader, Read(_, _))
257 .WillRepeatedly(DoAll(ClearBuffer(), SignalEvent(&event), Return(4)));
258 EXPECT_CALL(sync_reader, DataReady())
259 .WillRepeatedly(Return(true));
260 EXPECT_CALL(sync_reader, Close());
261
262 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout,
263 kSampleRate, kBitsPerSample, kSamplesPerPacket);
264 scoped_refptr<AudioOutputController> controller =
265 AudioOutputController::Create(
266 audio_manager.get(), &event_handler, params, &sync_reader);
267 ASSERT_TRUE(controller.get());
268
269 // Wait for OnCreated() to be called.
270 event.Wait();
271
272 ASSERT_FALSE(play_event.IsSignaled());
273 controller->Play();
274 play_event.Wait();
275
276 // Force a state change and wait for the stream to come back to playing state.
277 play_event.Reset();
278 audio_manager->GetMessageLoop()->PostTask(FROM_HERE,
279 base::Bind(&AudioOutputController::OnDeviceChange, controller));
280 play_event.Wait();
281
282 // Now stop the controller.
283 CloseAudioController(controller);
284 } 332 }
285 333
286 } // namespace media 334 } // namespace media
OLDNEW
« media/audio/audio_output_controller.cc ('K') | « media/audio/audio_output_controller.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698