OLD | NEW |
1 // Copyright (c) 2010 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/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.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.h" | 11 #include "media/audio/audio_manager.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace { | 14 static const int kSamplingRate = 8000; |
15 | 15 static const int kSamplesPerPacket = kSamplingRate / 20; |
16 const int kSamplingRate = 8000; | |
17 const int kSamplesPerPacket = kSamplingRate / 20; | |
18 | 16 |
19 // This class allows to find out if the callbacks are occurring as | 17 // This class allows to find out if the callbacks are occurring as |
20 // expected and if any error has been reported. | 18 // expected and if any error has been reported. |
21 class TestInputCallback : public AudioInputStream::AudioInputCallback { | 19 class TestInputCallback : public AudioInputStream::AudioInputCallback { |
22 public: | 20 public: |
23 explicit TestInputCallback(int max_data_bytes) | 21 explicit TestInputCallback(int max_data_bytes) |
24 : callback_count_(0), | 22 : callback_count_(0), |
25 had_error_(0), | 23 had_error_(0), |
26 was_closed_(0), | 24 was_closed_(0), |
27 max_data_bytes_(max_data_bytes) { | 25 max_data_bytes_(max_data_bytes) { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 TestInputCallback::OnData(stream, data, size); | 80 TestInputCallback::OnData(stream, data, size); |
83 if (callback_count() > block_after_callback_) | 81 if (callback_count() > block_after_callback_) |
84 base::PlatformThread::Sleep(block_for_ms_); | 82 base::PlatformThread::Sleep(block_for_ms_); |
85 } | 83 } |
86 | 84 |
87 private: | 85 private: |
88 int block_after_callback_; | 86 int block_after_callback_; |
89 int block_for_ms_; | 87 int block_for_ms_; |
90 }; | 88 }; |
91 | 89 |
92 bool CanRunAudioTests() { | 90 static bool CanRunAudioTests() { |
93 scoped_ptr<base::Environment> env(base::Environment::Create()); | 91 scoped_ptr<base::Environment> env(base::Environment::Create()); |
94 if (env->HasVar("CHROME_HEADLESS")) | 92 if (env->HasVar("CHROME_HEADLESS")) |
95 return false; | 93 return false; |
96 | 94 |
97 AudioManager* audio_man = AudioManager::GetAudioManager(); | 95 AudioManager* audio_man = AudioManager::GetAudioManager(); |
98 if (NULL == audio_man) | 96 if (NULL == audio_man) |
99 return false; | 97 return false; |
100 | 98 |
101 return audio_man->HasAudioInputDevices(); | 99 return audio_man->HasAudioInputDevices(); |
102 } | 100 } |
103 | 101 |
104 AudioInputStream* CreateTestAudioInputStream() { | 102 static AudioInputStream* CreateTestAudioInputStream() { |
105 AudioManager* audio_man = AudioManager::GetAudioManager(); | 103 AudioManager* audio_man = AudioManager::GetAudioManager(); |
106 AudioInputStream* ais = audio_man->MakeAudioInputStream( | 104 AudioInputStream* ais = audio_man->MakeAudioInputStream( |
107 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, 2, kSamplingRate, | 105 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, 2, kSamplingRate, |
108 16, kSamplesPerPacket)); | 106 16, kSamplesPerPacket)); |
109 EXPECT_TRUE(NULL != ais); | 107 EXPECT_TRUE(NULL != ais); |
110 return ais; | 108 return ais; |
111 } | 109 } |
112 | 110 |
113 } // namespace. | |
114 | |
115 // Test that AudioInputStream rejects out of range parameters. | 111 // Test that AudioInputStream rejects out of range parameters. |
116 TEST(AudioInputTest, SanityOnMakeParams) { | 112 TEST(AudioInputTest, SanityOnMakeParams) { |
117 if (!CanRunAudioTests()) | 113 if (!CanRunAudioTests()) |
118 return; | 114 return; |
119 AudioManager* audio_man = AudioManager::GetAudioManager(); | 115 AudioManager* audio_man = AudioManager::GetAudioManager(); |
120 AudioParameters::Format fmt = AudioParameters::AUDIO_PCM_LINEAR; | 116 AudioParameters::Format fmt = AudioParameters::AUDIO_PCM_LINEAR; |
121 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 117 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( |
122 AudioParameters(fmt, 8, 8000, 16, kSamplesPerPacket))); | 118 AudioParameters(fmt, 8, 8000, 16, kSamplesPerPacket))); |
123 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( | 119 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( |
124 AudioParameters(fmt, 1, 1024 * 1024, 16, kSamplesPerPacket))); | 120 AudioParameters(fmt, 1, 1024 * 1024, 16, kSamplesPerPacket))); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 // Verify at least 500ms worth of audio was recorded, after giving sufficient | 195 // Verify at least 500ms worth of audio was recorded, after giving sufficient |
200 // extra time. | 196 // extra time. |
201 message_loop.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(), 590); | 197 message_loop.PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(), 590); |
202 message_loop.Run(); | 198 message_loop.Run(); |
203 EXPECT_GE(test_callback.callback_count(), 10); | 199 EXPECT_GE(test_callback.callback_count(), 10); |
204 EXPECT_FALSE(test_callback.had_error()); | 200 EXPECT_FALSE(test_callback.had_error()); |
205 | 201 |
206 ais->Stop(); | 202 ais->Stop(); |
207 ais->Close(); | 203 ais->Close(); |
208 } | 204 } |
OLD | NEW |