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/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" |
(...skipping 13 matching lines...) Expand all Loading... |
24 was_closed_(0), | 24 was_closed_(0), |
25 max_data_bytes_(max_data_bytes) { | 25 max_data_bytes_(max_data_bytes) { |
26 } | 26 } |
27 virtual void OnData(AudioInputStream* stream, const uint8* data, | 27 virtual void OnData(AudioInputStream* stream, const uint8* data, |
28 uint32 size, uint32 hardware_delay_bytes) { | 28 uint32 size, uint32 hardware_delay_bytes) { |
29 ++callback_count_; | 29 ++callback_count_; |
30 // Read the first byte to make sure memory is good. | 30 // Read the first byte to make sure memory is good. |
31 if (size) { | 31 if (size) { |
32 ASSERT_LE(static_cast<int>(size), max_data_bytes_); | 32 ASSERT_LE(static_cast<int>(size), max_data_bytes_); |
33 int value = data[0]; | 33 int value = data[0]; |
34 EXPECT_TRUE(value >= 0); | 34 EXPECT_GE(value, 0); |
35 } | 35 } |
36 } | 36 } |
37 virtual void OnClose(AudioInputStream* stream) { | 37 virtual void OnClose(AudioInputStream* stream) { |
38 ++was_closed_; | 38 ++was_closed_; |
39 } | 39 } |
40 virtual void OnError(AudioInputStream* stream, int code) { | 40 virtual void OnError(AudioInputStream* stream, int code) { |
41 ++had_error_; | 41 ++had_error_; |
42 } | 42 } |
43 // Returns how many times OnData() has been called. | 43 // Returns how many times OnData() has been called. |
44 int callback_count() const { | 44 int callback_count() const { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 TestInputCallback::OnData(stream, data, size, hardware_delay_bytes); | 80 TestInputCallback::OnData(stream, data, size, hardware_delay_bytes); |
81 if (callback_count() > block_after_callback_) | 81 if (callback_count() > block_after_callback_) |
82 base::PlatformThread::Sleep(block_for_ms_); | 82 base::PlatformThread::Sleep(block_for_ms_); |
83 } | 83 } |
84 | 84 |
85 private: | 85 private: |
86 int block_after_callback_; | 86 int block_after_callback_; |
87 int block_for_ms_; | 87 int block_for_ms_; |
88 }; | 88 }; |
89 | 89 |
90 static bool CanRunAudioTests() { | 90 static bool CanRunAudioTests(AudioManager* audio_man) { |
| 91 if (NULL == audio_man) |
| 92 return false; |
| 93 |
91 scoped_ptr<base::Environment> env(base::Environment::Create()); | 94 scoped_ptr<base::Environment> env(base::Environment::Create()); |
92 if (env->HasVar("CHROME_HEADLESS")) | 95 if (env->HasVar("CHROME_HEADLESS")) |
93 return false; | 96 return false; |
94 | 97 |
95 AudioManager* audio_man = AudioManager::GetAudioManager(); | |
96 if (NULL == audio_man) | |
97 return false; | |
98 | |
99 return audio_man->HasAudioInputDevices(); | 98 return audio_man->HasAudioInputDevices(); |
100 } | 99 } |
101 | 100 |
102 static AudioInputStream* CreateTestAudioInputStream() { | 101 static AudioInputStream* CreateTestAudioInputStream(AudioManager* audio_man) { |
103 AudioManager* audio_man = AudioManager::GetAudioManager(); | |
104 AudioInputStream* ais = audio_man->MakeAudioInputStream( | 102 AudioInputStream* ais = audio_man->MakeAudioInputStream( |
105 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, | 103 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, |
106 kSamplingRate, 16, kSamplesPerPacket), | 104 kSamplingRate, 16, kSamplesPerPacket), |
107 AudioManagerBase::kDefaultDeviceId); | 105 AudioManagerBase::kDefaultDeviceId); |
108 EXPECT_TRUE(NULL != ais); | 106 EXPECT_TRUE(NULL != ais); |
109 return ais; | 107 return ais; |
110 } | 108 } |
111 | 109 |
112 // Test that AudioInputStream rejects out of range parameters. | 110 // Test that AudioInputStream rejects out of range parameters. |
113 TEST(AudioInputTest, SanityOnMakeParams) { | 111 TEST(AudioInputTest, SanityOnMakeParams) { |
114 if (!CanRunAudioTests()) | 112 scoped_refptr<AudioManager> audio_man(AudioManager::Create()); |
| 113 if (!CanRunAudioTests(audio_man.get())) |
115 return; | 114 return; |
116 AudioManager* audio_man = AudioManager::GetAudioManager(); | 115 |
117 AudioParameters::Format fmt = AudioParameters::AUDIO_PCM_LINEAR; | 116 AudioParameters::Format fmt = AudioParameters::AUDIO_PCM_LINEAR; |
118 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 117 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( |
119 AudioParameters(fmt, CHANNEL_LAYOUT_7POINT1, 8000, 16, | 118 AudioParameters(fmt, CHANNEL_LAYOUT_7POINT1, 8000, 16, |
120 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); | 119 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); |
121 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 120 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( |
122 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 1024 * 1024, 16, | 121 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 1024 * 1024, 16, |
123 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); | 122 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); |
124 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 123 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( |
125 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 80, | 124 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 80, |
126 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); | 125 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); |
(...skipping 10 matching lines...) Expand all Loading... |
137 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 136 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( |
138 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, -16, | 137 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, -16, |
139 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); | 138 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); |
140 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 139 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( |
141 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 16, -1024), | 140 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 16, -1024), |
142 AudioManagerBase::kDefaultDeviceId)); | 141 AudioManagerBase::kDefaultDeviceId)); |
143 } | 142 } |
144 | 143 |
145 // Test create and close of an AudioInputStream without recording audio. | 144 // Test create and close of an AudioInputStream without recording audio. |
146 TEST(AudioInputTest, CreateAndClose) { | 145 TEST(AudioInputTest, CreateAndClose) { |
147 if (!CanRunAudioTests()) | 146 scoped_refptr<AudioManager> audio_man(AudioManager::Create()); |
| 147 if (!CanRunAudioTests(audio_man.get())) |
148 return; | 148 return; |
149 AudioInputStream* ais = CreateTestAudioInputStream(); | 149 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); |
150 ais->Close(); | 150 ais->Close(); |
151 } | 151 } |
152 | 152 |
153 // Test create, open and close of an AudioInputStream without recording audio. | 153 // Test create, open and close of an AudioInputStream without recording audio. |
154 TEST(AudioInputTest, OpenAndClose) { | 154 TEST(AudioInputTest, OpenAndClose) { |
155 if (!CanRunAudioTests()) | 155 scoped_refptr<AudioManager> audio_man(AudioManager::Create()); |
| 156 if (!CanRunAudioTests(audio_man.get())) |
156 return; | 157 return; |
157 AudioInputStream* ais = CreateTestAudioInputStream(); | 158 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); |
158 EXPECT_TRUE(ais->Open()); | 159 EXPECT_TRUE(ais->Open()); |
159 ais->Close(); | 160 ais->Close(); |
160 } | 161 } |
161 | 162 |
162 // Test create, open, stop and close of an AudioInputStream without recording. | 163 // Test create, open, stop and close of an AudioInputStream without recording. |
163 TEST(AudioInputTest, OpenStopAndClose) { | 164 TEST(AudioInputTest, OpenStopAndClose) { |
164 if (!CanRunAudioTests()) | 165 scoped_refptr<AudioManager> audio_man(AudioManager::Create()); |
| 166 if (!CanRunAudioTests(audio_man.get())) |
165 return; | 167 return; |
166 AudioInputStream* ais = CreateTestAudioInputStream(); | 168 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); |
167 EXPECT_TRUE(ais->Open()); | 169 EXPECT_TRUE(ais->Open()); |
168 ais->Stop(); | 170 ais->Stop(); |
169 ais->Close(); | 171 ais->Close(); |
170 } | 172 } |
171 | 173 |
172 // Test a normal recording sequence using an AudioInputStream. | 174 // Test a normal recording sequence using an AudioInputStream. |
173 TEST(AudioInputTest, Record) { | 175 TEST(AudioInputTest, Record) { |
174 if (!CanRunAudioTests()) | 176 scoped_refptr<AudioManager> audio_man(AudioManager::Create()); |
| 177 if (!CanRunAudioTests(audio_man.get())) |
175 return; | 178 return; |
176 MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); | 179 MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
177 AudioInputStream* ais = CreateTestAudioInputStream(); | 180 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); |
178 EXPECT_TRUE(ais->Open()); | 181 EXPECT_TRUE(ais->Open()); |
179 | 182 |
180 TestInputCallback test_callback(kSamplesPerPacket * 4); | 183 TestInputCallback test_callback(kSamplesPerPacket * 4); |
181 ais->Start(&test_callback); | 184 ais->Start(&test_callback); |
182 // Verify at least 500ms worth of audio was recorded, after giving sufficient | 185 // Verify at least 500ms worth of audio was recorded, after giving sufficient |
183 // extra time. | 186 // extra time. |
184 message_loop.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), 590); | 187 message_loop.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), 590); |
185 message_loop.Run(); | 188 message_loop.Run(); |
186 EXPECT_GE(test_callback.callback_count(), 10); | 189 EXPECT_GE(test_callback.callback_count(), 10); |
187 EXPECT_FALSE(test_callback.had_error()); | 190 EXPECT_FALSE(test_callback.had_error()); |
188 | 191 |
189 ais->Stop(); | 192 ais->Stop(); |
190 ais->Close(); | 193 ais->Close(); |
191 } | 194 } |
192 | 195 |
193 // Test a recording sequence with delays in the audio callback. | 196 // Test a recording sequence with delays in the audio callback. |
194 TEST(AudioInputTest, RecordWithSlowSink) { | 197 TEST(AudioInputTest, RecordWithSlowSink) { |
195 if (!CanRunAudioTests()) | 198 scoped_refptr<AudioManager> audio_man(AudioManager::Create()); |
| 199 if (!CanRunAudioTests(audio_man.get())) |
196 return; | 200 return; |
197 MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); | 201 MessageLoop message_loop(MessageLoop::TYPE_DEFAULT); |
198 AudioInputStream* ais = CreateTestAudioInputStream(); | 202 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); |
199 EXPECT_TRUE(ais->Open()); | 203 EXPECT_TRUE(ais->Open()); |
200 | 204 |
201 // We should normally get a callback every 50ms, and a 20ms delay inside each | 205 // We should normally get a callback every 50ms, and a 20ms delay inside each |
202 // callback should not change this sequence. | 206 // callback should not change this sequence. |
203 TestInputCallbackBlocking test_callback(kSamplesPerPacket * 4, 0, 20); | 207 TestInputCallbackBlocking test_callback(kSamplesPerPacket * 4, 0, 20); |
204 ais->Start(&test_callback); | 208 ais->Start(&test_callback); |
205 // Verify at least 500ms worth of audio was recorded, after giving sufficient | 209 // Verify at least 500ms worth of audio was recorded, after giving sufficient |
206 // extra time. | 210 // extra time. |
207 message_loop.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), 590); | 211 message_loop.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), 590); |
208 message_loop.Run(); | 212 message_loop.Run(); |
209 EXPECT_GE(test_callback.callback_count(), 10); | 213 EXPECT_GE(test_callback.callback_count(), 10); |
210 EXPECT_FALSE(test_callback.had_error()); | 214 EXPECT_FALSE(test_callback.had_error()); |
211 | 215 |
212 ais->Stop(); | 216 ais->Stop(); |
213 ais->Close(); | 217 ais->Close(); |
214 } | 218 } |
OLD | NEW |