| 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/environment.h" | 6 #include "base/environment.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/synchronization/waitable_event.h" |
| 9 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
| 10 #include "media/audio/audio_io.h" | 11 #include "media/audio/audio_io.h" |
| 11 #include "media/audio/audio_manager_base.h" | 12 #include "media/audio/audio_manager_base.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 namespace media { | 15 namespace media { |
| 15 | 16 |
| 16 static const int kSamplingRate = 8000; | 17 static const int kSamplingRate = 8000; |
| 17 static const int kSamplesPerPacket = kSamplingRate / 20; | 18 static const int kSamplesPerPacket = kSamplingRate / 20; |
| 18 | 19 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 49 int had_error() const { | 50 int had_error() const { |
| 50 return had_error_; | 51 return had_error_; |
| 51 } | 52 } |
| 52 | 53 |
| 53 private: | 54 private: |
| 54 int callback_count_; | 55 int callback_count_; |
| 55 int had_error_; | 56 int had_error_; |
| 56 int max_data_bytes_; | 57 int max_data_bytes_; |
| 57 }; | 58 }; |
| 58 | 59 |
| 59 static bool CanRunAudioTests(AudioManager* audio_man) { | 60 class AudioInputTest : public testing::Test { |
| 60 bool has_input = audio_man->HasAudioInputDevices(); | 61 public: |
| 62 AudioInputTest() : |
| 63 message_loop_(base::MessageLoop::TYPE_UI), |
| 64 audio_manager_(AudioManager::CreateForTesting()), |
| 65 audio_input_stream_(NULL) { |
| 66 } |
| 61 | 67 |
| 62 if (!has_input) | 68 virtual ~AudioInputTest() { |
| 63 LOG(WARNING) << "No input devices detected"; | 69 } |
| 64 | 70 |
| 65 return has_input; | 71 protected: |
| 66 } | 72 AudioManager* audio_manager() { return audio_manager_.get(); } |
| 67 | 73 |
| 68 static AudioInputStream* CreateTestAudioInputStream(AudioManager* audio_man) { | 74 bool CanRunAudioTests() { |
| 69 AudioInputStream* ais = audio_man->MakeAudioInputStream( | 75 bool has_input = audio_manager()->HasAudioInputDevices(); |
| 70 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, | 76 if (!has_input) |
| 71 kSamplingRate, 16, kSamplesPerPacket), | 77 LOG(WARNING) << "No input devices detected"; |
| 72 AudioManagerBase::kDefaultDeviceId); | 78 return has_input; |
| 73 EXPECT_TRUE(NULL != ais); | 79 } |
| 74 return ais; | |
| 75 } | |
| 76 | 80 |
| 77 // Test that AudioInputStream rejects out of range parameters. | 81 void MakeAudioInputStreamOnAudioThread() { |
| 78 TEST(AudioInputTest, SanityOnMakeParams) { | 82 RunOnAudioThread( |
| 79 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 83 base::Bind(&AudioInputTest::MakeAudioInputStream, |
| 80 if (!CanRunAudioTests(audio_man.get())) | 84 base::Unretained(this))); |
| 81 return; | 85 } |
| 82 | 86 |
| 83 AudioParameters::Format fmt = AudioParameters::AUDIO_PCM_LINEAR; | 87 void CloseAudioInputStreamOnAudioThread() { |
| 84 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 88 RunOnAudioThread( |
| 85 AudioParameters(fmt, CHANNEL_LAYOUT_7_1, 8000, 16, | 89 base::Bind(&AudioInputStream::Close, |
| 86 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); | 90 base::Unretained(audio_input_stream_))); |
| 87 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 91 audio_input_stream_ = NULL; |
| 88 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 1024 * 1024, 16, | 92 } |
| 89 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); | 93 |
| 90 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 94 void OpenAndCloseAudioInputStreamOnAudioThread() { |
| 91 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 80, | 95 RunOnAudioThread( |
| 92 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); | 96 base::Bind(&AudioInputTest::OpenAndClose, |
| 93 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 97 base::Unretained(this))); |
| 94 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 80, | 98 } |
| 95 1000 * kSamplesPerPacket), | 99 |
| 96 AudioManagerBase::kDefaultDeviceId)); | 100 void OpenStopAndCloseAudioInputStreamOnAudioThread() { |
| 97 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 101 RunOnAudioThread( |
| 98 AudioParameters(fmt, CHANNEL_LAYOUT_UNSUPPORTED, 8000, 16, | 102 base::Bind(&AudioInputTest::OpenStopAndClose, |
| 99 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); | 103 base::Unretained(this))); |
| 100 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 104 } |
| 101 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, -8000, 16, | 105 |
| 102 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); | 106 void MakeAudioInputStream() { |
| 103 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 107 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); |
| 104 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, -16, | 108 AudioParameters params = audio_manager()->GetInputStreamParameters( |
| 105 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); | 109 AudioManagerBase::kDefaultDeviceId); |
| 106 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 110 audio_input_stream_ = audio_manager()->MakeAudioInputStream(params, |
| 107 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 16, -1024), | 111 AudioManagerBase::kDefaultDeviceId); |
| 108 AudioManagerBase::kDefaultDeviceId)); | 112 EXPECT_TRUE(audio_input_stream_); |
| 109 } | 113 } |
| 114 |
| 115 void OpenAndClose() { |
| 116 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); |
| 117 EXPECT_TRUE(audio_input_stream_->Open()); |
| 118 audio_input_stream_->Close(); |
| 119 audio_input_stream_ = NULL; |
| 120 } |
| 121 |
| 122 void OpenStopAndClose() { |
| 123 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); |
| 124 EXPECT_TRUE(audio_input_stream_->Open()); |
| 125 audio_input_stream_->Stop(); |
| 126 audio_input_stream_->Close(); |
| 127 audio_input_stream_ = NULL; |
| 128 } |
| 129 |
| 130 // Synchronously runs the provided callback/closure on the audio thread. |
| 131 void RunOnAudioThread(const base::Closure& closure) { |
| 132 if (!audio_manager()->GetTaskRunner()->BelongsToCurrentThread()) { |
| 133 base::WaitableEvent event(false, false); |
| 134 audio_manager()->GetTaskRunner()->PostTask( |
| 135 FROM_HERE, |
| 136 base::Bind(&AudioInputTest::RunOnAudioThreadImpl, |
| 137 base::Unretained(this), |
| 138 closure, |
| 139 &event)); |
| 140 event.Wait(); |
| 141 } else { |
| 142 closure.Run(); |
| 143 } |
| 144 } |
| 145 |
| 146 void RunOnAudioThreadImpl(const base::Closure& closure, |
| 147 base::WaitableEvent* event) { |
| 148 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); |
| 149 closure.Run(); |
| 150 event->Signal(); |
| 151 } |
| 152 |
| 153 base::MessageLoop message_loop_; |
| 154 scoped_ptr<AudioManager> audio_manager_; |
| 155 AudioInputStream* audio_input_stream_; |
| 156 |
| 157 private: |
| 158 DISALLOW_COPY_AND_ASSIGN(AudioInputTest); |
| 159 }; |
| 110 | 160 |
| 111 // Test create and close of an AudioInputStream without recording audio. | 161 // Test create and close of an AudioInputStream without recording audio. |
| 112 TEST(AudioInputTest, CreateAndClose) { | 162 TEST_F(AudioInputTest, CreateAndClose) { |
| 113 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 163 if (!CanRunAudioTests()) |
| 114 if (!CanRunAudioTests(audio_man.get())) | |
| 115 return; | 164 return; |
| 116 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); | 165 MakeAudioInputStreamOnAudioThread(); |
| 117 ais->Close(); | 166 CloseAudioInputStreamOnAudioThread(); |
| 118 } | 167 } |
| 119 | 168 |
| 120 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY) | 169 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY) |
| 121 // This test is failing on ARM linux: http://crbug.com/238490 | 170 // This test is failing on ARM linux: http://crbug.com/238490 |
| 122 #define MAYBE_OpenAndClose DISABLED_OpenAndClose | 171 #define MAYBE_OpenAndClose DISABLED_OpenAndClose |
| 123 #else | 172 #else |
| 124 #define MAYBE_OpenAndClose OpenAndClose | 173 #define MAYBE_OpenAndClose OpenAndClose |
| 125 #endif | 174 #endif |
| 126 // Test create, open and close of an AudioInputStream without recording audio. | 175 // Test create, open and close of an AudioInputStream without recording audio. |
| 127 TEST(AudioInputTest, MAYBE_OpenAndClose) { | 176 TEST_F(AudioInputTest, MAYBE_OpenAndClose) { |
| 128 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 177 if (!CanRunAudioTests()) |
| 129 if (!CanRunAudioTests(audio_man.get())) | |
| 130 return; | 178 return; |
| 131 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); | 179 MakeAudioInputStreamOnAudioThread(); |
| 132 EXPECT_TRUE(ais->Open()); | 180 OpenAndCloseAudioInputStreamOnAudioThread(); |
| 133 ais->Close(); | |
| 134 } | 181 } |
| 135 | 182 |
| 136 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY) | 183 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY) |
| 137 // This test is failing on ARM linux: http://crbug.com/238490 | 184 // This test is failing on ARM linux: http://crbug.com/238490 |
| 138 #define MAYBE_OpenStopAndClose DISABLED_OpenStopAndClose | 185 #define MAYBE_OpenStopAndClose DISABLED_OpenStopAndClose |
| 139 #else | 186 #else |
| 140 #define MAYBE_OpenStopAndClose OpenStopAndClose | 187 #define MAYBE_OpenStopAndClose OpenStopAndClose |
| 141 #endif | 188 #endif |
| 142 // Test create, open, stop and close of an AudioInputStream without recording. | 189 // Test create, open, stop and close of an AudioInputStream without recording. |
| 143 TEST(AudioInputTest, MAYBE_OpenStopAndClose) { | 190 TEST_F(AudioInputTest, MAYBE_OpenStopAndClose) { |
| 144 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 191 if (!CanRunAudioTests()) |
| 145 if (!CanRunAudioTests(audio_man.get())) | |
| 146 return; | 192 return; |
| 147 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); | 193 MakeAudioInputStreamOnAudioThread(); |
| 148 EXPECT_TRUE(ais->Open()); | 194 OpenStopAndCloseAudioInputStreamOnAudioThread(); |
| 149 ais->Stop(); | |
| 150 ais->Close(); | |
| 151 } | 195 } |
| 152 | 196 |
| 153 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY) | 197 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY) |
| 154 // This test is failing on ARM linux: http://crbug.com/238490 | 198 // This test is failing on ARM linux: http://crbug.com/238490 |
| 155 #define MAYBE_Record DISABLED_Record | 199 #define MAYBE_Record DISABLED_Record |
| 156 #else | 200 #else |
| 157 #define MAYBE_Record Record | 201 // TODO(henrika): remove DISABLED_. |
| 202 #define MAYBE_Record DISABLED_Record |
| 158 #endif | 203 #endif |
| 159 // Test a normal recording sequence using an AudioInputStream. | 204 // Test a normal recording sequence using an AudioInputStream. |
| 160 TEST(AudioInputTest, MAYBE_Record) { | 205 TEST_F(AudioInputTest, MAYBE_Record) { |
| 161 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 206 if (!CanRunAudioTests()) |
| 162 if (!CanRunAudioTests(audio_man.get())) | |
| 163 return; | 207 return; |
| 164 base::MessageLoop message_loop; | 208 MakeAudioInputStreamOnAudioThread(); |
| 165 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); | 209 EXPECT_TRUE(audio_input_stream_->Open()); |
| 166 EXPECT_TRUE(ais->Open()); | |
| 167 | 210 |
| 168 TestInputCallback test_callback(kSamplesPerPacket * 4); | 211 TestInputCallback test_callback(kSamplesPerPacket * 4); |
| 169 ais->Start(&test_callback); | 212 audio_input_stream_->Start(&test_callback); |
| 170 // Verify at least 500ms worth of audio was recorded, after giving sufficient | 213 // Verify at least 500ms worth of audio was recorded, after giving sufficient |
| 171 // extra time. | 214 // extra time. |
| 172 message_loop.PostDelayedTask( | 215 message_loop_.PostDelayedTask( |
| 173 FROM_HERE, | 216 FROM_HERE, |
| 174 base::MessageLoop::QuitClosure(), | 217 base::MessageLoop::QuitClosure(), |
| 175 base::TimeDelta::FromMilliseconds(690)); | 218 base::TimeDelta::FromMilliseconds(690)); |
| 176 message_loop.Run(); | 219 message_loop_.Run(); |
| 177 EXPECT_GE(test_callback.callback_count(), 1); | 220 EXPECT_GE(test_callback.callback_count(), 1); |
| 178 EXPECT_FALSE(test_callback.had_error()); | 221 EXPECT_FALSE(test_callback.had_error()); |
| 179 | 222 |
| 180 ais->Stop(); | 223 audio_input_stream_->Stop(); |
| 181 ais->Close(); | 224 audio_input_stream_->Close(); |
| 182 } | 225 } |
| 183 | 226 |
| 184 } // namespace media | 227 } // namespace media |
| OLD | NEW |