| 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.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
| 10 #include "media/audio/audio_io.h" | 10 #include "media/audio/audio_io.h" |
| 11 #include "media/audio/audio_manager_base.h" | 11 #include "media/audio/audio_manager_base.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 #if defined(OS_ANDROID) | |
| 15 #include "base/android/jni_android.h" | |
| 16 #include "media/audio/audio_manager_base.h" | |
| 17 #endif | |
| 18 | |
| 19 namespace media { | 14 namespace media { |
| 20 | 15 |
| 21 static const int kSamplingRate = 8000; | 16 static const int kSamplingRate = 8000; |
| 22 static const int kSamplesPerPacket = kSamplingRate / 20; | 17 static const int kSamplesPerPacket = kSamplingRate / 20; |
| 23 | 18 |
| 24 // This class allows to find out if the callbacks are occurring as | 19 // This class allows to find out if the callbacks are occurring as |
| 25 // expected and if any error has been reported. | 20 // expected and if any error has been reported. |
| 26 class TestInputCallback : public AudioInputStream::AudioInputCallback { | 21 class TestInputCallback : public AudioInputStream::AudioInputCallback { |
| 27 public: | 22 public: |
| 28 explicit TestInputCallback(int max_data_bytes) | 23 explicit TestInputCallback(int max_data_bytes) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 static bool CanRunAudioTests(AudioManager* audio_man) { | 60 static bool CanRunAudioTests(AudioManager* audio_man) { |
| 66 bool has_input = audio_man->HasAudioInputDevices(); | 61 bool has_input = audio_man->HasAudioInputDevices(); |
| 67 | 62 |
| 68 if (!has_input) | 63 if (!has_input) |
| 69 LOG(WARNING) << "No input devices detected"; | 64 LOG(WARNING) << "No input devices detected"; |
| 70 | 65 |
| 71 return has_input; | 66 return has_input; |
| 72 } | 67 } |
| 73 | 68 |
| 74 static AudioInputStream* CreateTestAudioInputStream(AudioManager* audio_man) { | 69 static AudioInputStream* CreateTestAudioInputStream(AudioManager* audio_man) { |
| 75 #if defined(OS_ANDROID) | |
| 76 bool ret = media::AudioManagerBase::RegisterAudioManager( | |
| 77 base::android::AttachCurrentThread()); | |
| 78 EXPECT_TRUE(ret); | |
| 79 #endif | |
| 80 AudioInputStream* ais = audio_man->MakeAudioInputStream( | 70 AudioInputStream* ais = audio_man->MakeAudioInputStream( |
| 81 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, | 71 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, |
| 82 kSamplingRate, 16, kSamplesPerPacket), | 72 kSamplingRate, 16, kSamplesPerPacket), |
| 83 AudioManagerBase::kDefaultDeviceId); | 73 AudioManagerBase::kDefaultDeviceId); |
| 84 EXPECT_TRUE(NULL != ais); | 74 EXPECT_TRUE(NULL != ais); |
| 85 return ais; | 75 return ais; |
| 86 } | 76 } |
| 87 | 77 |
| 88 // Test that AudioInputStream rejects out of range parameters. | 78 // Test that AudioInputStream rejects out of range parameters. |
| 89 TEST(AudioInputTest, SanityOnMakeParams) { | 79 TEST(AudioInputTest, SanityOnMakeParams) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 113 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); | 103 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); |
| 114 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 104 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( |
| 115 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, -16, | 105 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, -16, |
| 116 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); | 106 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); |
| 117 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 107 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( |
| 118 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 16, -1024), | 108 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 16, -1024), |
| 119 AudioManagerBase::kDefaultDeviceId)); | 109 AudioManagerBase::kDefaultDeviceId)); |
| 120 } | 110 } |
| 121 | 111 |
| 122 // Test create and close of an AudioInputStream without recording audio. | 112 // Test create and close of an AudioInputStream without recording audio. |
| 123 TEST(AudioInputTest, CreateAndClose) { | 113 #if defined(OS_ANDROID) |
| 114 #define MAYBE_CreateAndClose DISABLED_CreateAndClose |
| 115 #else |
| 116 #define MAYBE_CreateAndClose CreateAndClose |
| 117 #endif |
| 118 TEST(AudioInputTest, MAYBE_CreateAndClose) { |
| 124 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); | 119 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); |
| 125 if (!CanRunAudioTests(audio_man.get())) | 120 if (!CanRunAudioTests(audio_man.get())) |
| 126 return; | 121 return; |
| 127 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); | 122 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); |
| 128 ais->Close(); | 123 ais->Close(); |
| 129 } | 124 } |
| 130 | 125 |
| 131 // Test create, open and close of an AudioInputStream without recording audio. | 126 // Test create, open and close of an AudioInputStream without recording audio. |
| 132 TEST(AudioInputTest, OpenAndClose) { | 127 // TODO(leozwang): Because java calls were introduced in audio_manager_base, |
| 128 // unit test has to register jni first, else it will crash. |
| 129 #if defined(OS_ANDROID) |
| 130 #define MAYBE_OpenAndClose DISABLED_OpenAndClose |
| 131 #else |
| 132 #define MAYBE_OpenAndClose OpenAndClose |
| 133 #endif |
| 134 TEST(AudioInputTest, MAYBE_OpenAndClose) { |
| 133 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); | 135 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); |
| 134 if (!CanRunAudioTests(audio_man.get())) | 136 if (!CanRunAudioTests(audio_man.get())) |
| 135 return; | 137 return; |
| 136 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); | 138 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); |
| 137 EXPECT_TRUE(ais->Open()); | 139 EXPECT_TRUE(ais->Open()); |
| 138 ais->Close(); | 140 ais->Close(); |
| 139 } | 141 } |
| 140 | 142 |
| 141 // Test create, open, stop and close of an AudioInputStream without recording. | 143 // Test create, open, stop and close of an AudioInputStream without recording. |
| 142 TEST(AudioInputTest, OpenStopAndClose) { | 144 #if defined(OS_ANDROID) |
| 145 #define MAYBE_OpenStopAndClose DISABLED_OpenStopAndClose |
| 146 #else |
| 147 #define MAYBE_OpenStopAndClose OpenStopAndClose |
| 148 #endif |
| 149 TEST(AudioInputTest, MAYBE_OpenStopAndClose) { |
| 143 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); | 150 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); |
| 144 if (!CanRunAudioTests(audio_man.get())) | 151 if (!CanRunAudioTests(audio_man.get())) |
| 145 return; | 152 return; |
| 146 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); | 153 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); |
| 147 EXPECT_TRUE(ais->Open()); | 154 EXPECT_TRUE(ais->Open()); |
| 148 ais->Stop(); | 155 ais->Stop(); |
| 149 ais->Close(); | 156 ais->Close(); |
| 150 } | 157 } |
| 151 | 158 |
| 152 // Test a normal recording sequence using an AudioInputStream. | 159 // Test a normal recording sequence using an AudioInputStream. |
| 153 TEST(AudioInputTest, Record) { | 160 #if defined(OS_ANDROID) |
| 161 #define MAYBE_Record DISABLED_Record |
| 162 #else |
| 163 #define MAYBE_Record Record |
| 164 #endif |
| 165 TEST(AudioInputTest, MAYBE_Record) { |
| 154 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); | 166 scoped_ptr<AudioManager> audio_man(AudioManager::Create()); |
| 155 if (!CanRunAudioTests(audio_man.get())) | 167 if (!CanRunAudioTests(audio_man.get())) |
| 156 return; | 168 return; |
| 157 MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); | 169 MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
| 158 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); | 170 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); |
| 159 EXPECT_TRUE(ais->Open()); | 171 EXPECT_TRUE(ais->Open()); |
| 160 | 172 |
| 161 TestInputCallback test_callback(kSamplesPerPacket * 4); | 173 TestInputCallback test_callback(kSamplesPerPacket * 4); |
| 162 ais->Start(&test_callback); | 174 ais->Start(&test_callback); |
| 163 // Verify at least 500ms worth of audio was recorded, after giving sufficient | 175 // Verify at least 500ms worth of audio was recorded, after giving sufficient |
| 164 // extra time. | 176 // extra time. |
| 165 message_loop.PostDelayedTask( | 177 message_loop.PostDelayedTask( |
| 166 FROM_HERE, | 178 FROM_HERE, |
| 167 MessageLoop::QuitClosure(), | 179 MessageLoop::QuitClosure(), |
| 168 base::TimeDelta::FromMilliseconds(690)); | 180 base::TimeDelta::FromMilliseconds(690)); |
| 169 message_loop.Run(); | 181 message_loop.Run(); |
| 170 EXPECT_GE(test_callback.callback_count(), 1); | 182 EXPECT_GE(test_callback.callback_count(), 1); |
| 171 EXPECT_FALSE(test_callback.had_error()); | 183 EXPECT_FALSE(test_callback.had_error()); |
| 172 | 184 |
| 173 ais->Stop(); | 185 ais->Stop(); |
| 174 ais->Close(); | 186 ais->Close(); |
| 175 } | 187 } |
| 176 | 188 |
| 177 } // namespace media | 189 } // namespace media |
| OLD | NEW |