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