| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/environment.h" | 6 #include "base/environment.h" |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/task.h" | 9 #include "base/task.h" |
| 10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| 11 #include "media/audio/audio_output_controller.h" | 11 #include "media/audio/audio_output_controller.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 using ::testing::_; | 15 using ::testing::_; |
| 16 using ::testing::AtLeast; | 16 using ::testing::AtLeast; |
| 17 using ::testing::DoAll; |
| 17 using ::testing::Exactly; | 18 using ::testing::Exactly; |
| 18 using ::testing::InvokeWithoutArgs; | 19 using ::testing::InvokeWithoutArgs; |
| 19 using ::testing::NotNull; | 20 using ::testing::NotNull; |
| 20 using ::testing::Return; | 21 using ::testing::Return; |
| 21 | 22 |
| 22 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; | 23 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; |
| 23 static const int kBitsPerSample = 16; | 24 static const int kBitsPerSample = 16; |
| 24 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; | 25 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; |
| 25 static const int kSamplesPerPacket = kSampleRate / 10; | 26 static const int kSamplesPerPacket = kSampleRate / 10; |
| 26 static const int kHardwareBufferSize = kSamplesPerPacket * | 27 static const int kHardwareBufferSize = kSamplesPerPacket * |
| (...skipping 20 matching lines...) Expand all Loading... |
| 47 }; | 48 }; |
| 48 | 49 |
| 49 class MockAudioOutputControllerSyncReader | 50 class MockAudioOutputControllerSyncReader |
| 50 : public AudioOutputController::SyncReader { | 51 : public AudioOutputController::SyncReader { |
| 51 public: | 52 public: |
| 52 MockAudioOutputControllerSyncReader() {} | 53 MockAudioOutputControllerSyncReader() {} |
| 53 | 54 |
| 54 MOCK_METHOD1(UpdatePendingBytes, void(uint32 bytes)); | 55 MOCK_METHOD1(UpdatePendingBytes, void(uint32 bytes)); |
| 55 MOCK_METHOD2(Read, uint32(void* data, uint32 size)); | 56 MOCK_METHOD2(Read, uint32(void* data, uint32 size)); |
| 56 MOCK_METHOD0(Close, void()); | 57 MOCK_METHOD0(Close, void()); |
| 58 MOCK_METHOD0(DataReady, bool()); |
| 57 | 59 |
| 58 private: | 60 private: |
| 59 DISALLOW_COPY_AND_ASSIGN(MockAudioOutputControllerSyncReader); | 61 DISALLOW_COPY_AND_ASSIGN(MockAudioOutputControllerSyncReader); |
| 60 }; | 62 }; |
| 61 | 63 |
| 62 static bool HasAudioOutputDevices() { | 64 static bool HasAudioOutputDevices() { |
| 63 AudioManager* audio_man = AudioManager::GetAudioManager(); | 65 AudioManager* audio_man = AudioManager::GetAudioManager(); |
| 64 CHECK(audio_man); | 66 CHECK(audio_man); |
| 65 return audio_man->HasAudioOutputDevices(); | 67 return audio_man->HasAudioOutputDevices(); |
| 66 } | 68 } |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 for (int i = 0; i < 10; i++) { | 145 for (int i = 0; i < 10; i++) { |
| 144 event.Wait(); | 146 event.Wait(); |
| 145 uint8 buf[1]; | 147 uint8 buf[1]; |
| 146 controller->EnqueueData(buf, 0); | 148 controller->EnqueueData(buf, 0); |
| 147 } | 149 } |
| 148 | 150 |
| 149 // Now stop the controller. | 151 // Now stop the controller. |
| 150 CloseAudioController(controller); | 152 CloseAudioController(controller); |
| 151 } | 153 } |
| 152 | 154 |
| 155 TEST(AudioOutputControllerTest, PlayAndCloseLowLatency) { |
| 156 if (!HasAudioOutputDevices() || IsRunningHeadless()) |
| 157 return; |
| 158 |
| 159 MockAudioOutputControllerEventHandler event_handler; |
| 160 base::WaitableEvent event(false, false); |
| 161 |
| 162 // If OnCreated is called then signal the event. |
| 163 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
| 164 .WillOnce(SignalEvent(&event)); |
| 165 |
| 166 // OnPlaying() will be called only once. |
| 167 EXPECT_CALL(event_handler, OnPlaying(NotNull())) |
| 168 .Times(Exactly(1)); |
| 169 |
| 170 MockAudioOutputControllerSyncReader sync_reader; |
| 171 EXPECT_CALL(sync_reader, UpdatePendingBytes(_)) |
| 172 .Times(AtLeast(10)); |
| 173 EXPECT_CALL(sync_reader, DataReady()) |
| 174 .WillOnce(Return(false)) |
| 175 .WillOnce(Return(false)) |
| 176 .WillRepeatedly(Return(true)); |
| 177 EXPECT_CALL(sync_reader, Read(_, kHardwareBufferSize)) |
| 178 .Times(AtLeast(10)) |
| 179 .WillRepeatedly(DoAll(SignalEvent(&event), |
| 180 Return(1))); |
| 181 EXPECT_CALL(sync_reader, Close()); |
| 182 |
| 183 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, |
| 184 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
| 185 scoped_refptr<AudioOutputController> controller = |
| 186 AudioOutputController::CreateLowLatency(&event_handler, |
| 187 params, |
| 188 &sync_reader); |
| 189 ASSERT_TRUE(controller.get()); |
| 190 |
| 191 // Wait for OnCreated() to be called. |
| 192 event.Wait(); |
| 193 |
| 194 controller->Play(); |
| 195 |
| 196 // Wait until the date is requested at least 10 times. |
| 197 for (int i = 0; i < 10; i++) { |
| 198 event.Wait(); |
| 199 uint8 buf[1]; |
| 200 controller->EnqueueData(buf, 0); |
| 201 } |
| 202 |
| 203 // Now stop the controller. |
| 204 CloseAudioController(controller); |
| 205 } |
| 206 |
| 153 TEST(AudioOutputControllerTest, PlayPauseClose) { | 207 TEST(AudioOutputControllerTest, PlayPauseClose) { |
| 154 if (!HasAudioOutputDevices() || IsRunningHeadless()) | 208 if (!HasAudioOutputDevices() || IsRunningHeadless()) |
| 155 return; | 209 return; |
| 156 | 210 |
| 157 MockAudioOutputControllerEventHandler event_handler; | 211 MockAudioOutputControllerEventHandler event_handler; |
| 158 base::WaitableEvent event(false, false); | 212 base::WaitableEvent event(false, false); |
| 159 base::WaitableEvent pause_event(false, false); | 213 base::WaitableEvent pause_event(false, false); |
| 160 | 214 |
| 161 // If OnCreated is called then signal the event. | 215 // If OnCreated is called then signal the event. |
| 162 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 216 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 controller->Close(base::Bind(&SignalClosedEvent, &closed_event_1)); | 378 controller->Close(base::Bind(&SignalClosedEvent, &closed_event_1)); |
| 325 | 379 |
| 326 base::WaitableEvent closed_event_2(true, false); | 380 base::WaitableEvent closed_event_2(true, false); |
| 327 controller->Close(base::Bind(&SignalClosedEvent, &closed_event_2)); | 381 controller->Close(base::Bind(&SignalClosedEvent, &closed_event_2)); |
| 328 | 382 |
| 329 closed_event_1.Wait(); | 383 closed_event_1.Wait(); |
| 330 closed_event_2.Wait(); | 384 closed_event_2.Wait(); |
| 331 } | 385 } |
| 332 | 386 |
| 333 } // namespace media | 387 } // namespace media |
| OLD | NEW |