Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(490)

Side by Side Diff: media/audio/audio_input_unittest.cc

Issue 131503006: Initialization of audio manager for Android is now done on the audio thread (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Restored DCHECKs and disabled unit test in content_unittessts instead Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 kSamplesPerPacket = kSamplingRate / 20;
18
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()
24 : callback_count_(0), 22 : callback_count_(0),
25 had_error_(0), 23 had_error_(0) {
26 max_data_bytes_(max_data_bytes) {
27 } 24 }
28 virtual void OnData(AudioInputStream* stream, 25 virtual void OnData(AudioInputStream* stream,
29 const uint8* data, 26 const uint8* data,
30 uint32 size, 27 uint32 size,
31 uint32 hardware_delay_bytes, 28 uint32 hardware_delay_bytes,
32 double volume) OVERRIDE { 29 double volume) OVERRIDE {
33 ++callback_count_; 30 ++callback_count_;
34 // Read the first byte to make sure memory is good.
35 if (size) {
36 ASSERT_LE(static_cast<int>(size), max_data_bytes_);
37 int value = data[0];
38 EXPECT_GE(value, 0);
39 }
40 } 31 }
41 virtual void OnError(AudioInputStream* stream) OVERRIDE { 32 virtual void OnError(AudioInputStream* stream) OVERRIDE {
42 ++had_error_; 33 ++had_error_;
43 } 34 }
44 // Returns how many times OnData() has been called. 35 // Returns how many times OnData() has been called.
45 int callback_count() const { 36 int callback_count() const {
46 return callback_count_; 37 return callback_count_;
47 } 38 }
48 // Returns how many times the OnError callback was called. 39 // Returns how many times the OnError callback was called.
49 int had_error() const { 40 int had_error() const {
50 return had_error_; 41 return had_error_;
51 } 42 }
52 43
53 private: 44 private:
54 int callback_count_; 45 int callback_count_;
55 int had_error_; 46 int had_error_;
56 int max_data_bytes_;
57 }; 47 };
58 48
59 static bool CanRunAudioTests(AudioManager* audio_man) { 49 class AudioInputTest : public testing::Test {
60 bool has_input = audio_man->HasAudioInputDevices(); 50 public:
51 AudioInputTest() :
52 message_loop_(base::MessageLoop::TYPE_UI),
53 audio_manager_(AudioManager::CreateForTesting()),
54 audio_input_stream_(NULL) {
55 }
61 56
62 if (!has_input) 57 virtual ~AudioInputTest() {
tommi (sloooow) - chröme 2014/02/21 16:10:30 nit: {} (since you're already using that style for
henrika (OOO until Aug 14) 2014/02/21 16:21:29 Done.
63 LOG(WARNING) << "No input devices detected"; 58 }
64 59
65 return has_input; 60 protected:
66 } 61 AudioManager* audio_manager() { return audio_manager_.get(); }
67 62
68 static AudioInputStream* CreateTestAudioInputStream(AudioManager* audio_man) { 63 bool CanRunAudioTests() {
69 AudioInputStream* ais = audio_man->MakeAudioInputStream( 64 bool has_input = audio_manager()->HasAudioInputDevices();
70 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, 65 if (!has_input)
71 kSamplingRate, 16, kSamplesPerPacket), 66 LOG(WARNING) << "No input devices detected";
tommi (sloooow) - chröme 2014/02/21 16:10:30 nit: use LOG_IF(WARNING, !has_input)
henrika (OOO until Aug 14) 2014/02/21 16:21:29 Done.
72 AudioManagerBase::kDefaultDeviceId); 67 return has_input;
73 EXPECT_TRUE(NULL != ais); 68 }
74 return ais;
75 }
76 69
77 // Test that AudioInputStream rejects out of range parameters. 70 void MakeAudioInputStreamOnAudioThread() {
78 TEST(AudioInputTest, SanityOnMakeParams) { 71 RunOnAudioThread(
79 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); 72 base::Bind(&AudioInputTest::MakeAudioInputStream,
80 if (!CanRunAudioTests(audio_man.get())) 73 base::Unretained(this)));
81 return; 74 }
82 75
83 AudioParameters::Format fmt = AudioParameters::AUDIO_PCM_LINEAR; 76 void CloseAudioInputStreamOnAudioThread() {
84 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( 77 RunOnAudioThread(
85 AudioParameters(fmt, CHANNEL_LAYOUT_7_1, 8000, 16, 78 base::Bind(&AudioInputStream::Close,
86 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); 79 base::Unretained(audio_input_stream_)));
87 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( 80 audio_input_stream_ = NULL;
88 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 1024 * 1024, 16, 81 }
89 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); 82
90 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( 83 void OpenAndCloseAudioInputStreamOnAudioThread() {
91 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 80, 84 RunOnAudioThread(
92 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); 85 base::Bind(&AudioInputTest::OpenAndClose,
93 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( 86 base::Unretained(this)));
94 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 80, 87 }
95 1000 * kSamplesPerPacket), 88
96 AudioManagerBase::kDefaultDeviceId)); 89 void OpenStopAndCloseAudioInputStreamOnAudioThread() {
97 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( 90 RunOnAudioThread(
98 AudioParameters(fmt, CHANNEL_LAYOUT_UNSUPPORTED, 8000, 16, 91 base::Bind(&AudioInputTest::OpenStopAndClose,
99 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); 92 base::Unretained(this)));
100 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( 93 }
101 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, -8000, 16, 94
102 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); 95 void OpenAndStartAudioInputStreamOnAudioThread(
103 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( 96 AudioInputStream::AudioInputCallback* sink) {
104 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, -16, 97 RunOnAudioThread(
105 kSamplesPerPacket), AudioManagerBase::kDefaultDeviceId)); 98 base::Bind(&AudioInputTest::OpenAndStart,
106 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream( 99 base::Unretained(this),
107 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 16, -1024), 100 sink));
108 AudioManagerBase::kDefaultDeviceId)); 101 }
109 } 102
103 void StopAndCloseAudioInputStreamOnAudioThread() {
104 RunOnAudioThread(
105 base::Bind(&AudioInputTest::StopAndClose,
106 base::Unretained(this)));
107 }
108
109 void MakeAudioInputStream() {
110 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
111 AudioParameters params = audio_manager()->GetInputStreamParameters(
112 AudioManagerBase::kDefaultDeviceId);
113 audio_input_stream_ = audio_manager()->MakeAudioInputStream(params,
114 AudioManagerBase::kDefaultDeviceId);
115 EXPECT_TRUE(audio_input_stream_);
116 }
117
118 void OpenAndClose() {
119 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
120 EXPECT_TRUE(audio_input_stream_->Open());
121 audio_input_stream_->Close();
122 audio_input_stream_ = NULL;
123 }
124
125 void OpenAndStart(AudioInputStream::AudioInputCallback* sink) {
126 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
127 EXPECT_TRUE(audio_input_stream_->Open());
128 audio_input_stream_->Start(sink);
129 }
130
131 void OpenStopAndClose() {
132 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
133 EXPECT_TRUE(audio_input_stream_->Open());
134 audio_input_stream_->Stop();
135 audio_input_stream_->Close();
136 audio_input_stream_ = NULL;
137 }
138
139 void StopAndClose() {
140 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
141 audio_input_stream_->Stop();
142 audio_input_stream_->Close();
143 audio_input_stream_ = NULL;
144 }
145
146 // Synchronously runs the provided callback/closure on the audio thread.
147 void RunOnAudioThread(const base::Closure& closure) {
148 if (!audio_manager()->GetTaskRunner()->BelongsToCurrentThread()) {
149 base::WaitableEvent event(false, false);
150 audio_manager()->GetTaskRunner()->PostTask(
151 FROM_HERE,
152 base::Bind(&AudioInputTest::RunOnAudioThreadImpl,
153 base::Unretained(this),
154 closure,
155 &event));
156 event.Wait();
157 } else {
158 closure.Run();
159 }
160 }
161
162 void RunOnAudioThreadImpl(const base::Closure& closure,
163 base::WaitableEvent* event) {
164 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
165 closure.Run();
166 event->Signal();
167 }
168
169 base::MessageLoop message_loop_;
170 scoped_ptr<AudioManager> audio_manager_;
171 AudioInputStream* audio_input_stream_;
172
173 private:
174 DISALLOW_COPY_AND_ASSIGN(AudioInputTest);
175 };
110 176
111 // Test create and close of an AudioInputStream without recording audio. 177 // Test create and close of an AudioInputStream without recording audio.
112 TEST(AudioInputTest, CreateAndClose) { 178 TEST_F(AudioInputTest, CreateAndClose) {
113 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); 179 if (!CanRunAudioTests())
114 if (!CanRunAudioTests(audio_man.get()))
115 return; 180 return;
116 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); 181 MakeAudioInputStreamOnAudioThread();
117 ais->Close(); 182 CloseAudioInputStreamOnAudioThread();
118 } 183 }
119 184
120 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY) 185 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
121 // This test is failing on ARM linux: http://crbug.com/238490 186 // This test is failing on ARM linux: http://crbug.com/238490
122 #define MAYBE_OpenAndClose DISABLED_OpenAndClose 187 #define MAYBE_OpenAndClose DISABLED_OpenAndClose
123 #else 188 #else
124 #define MAYBE_OpenAndClose OpenAndClose 189 #define MAYBE_OpenAndClose OpenAndClose
125 #endif 190 #endif
126 // Test create, open and close of an AudioInputStream without recording audio. 191 // Test create, open and close of an AudioInputStream without recording audio.
127 TEST(AudioInputTest, MAYBE_OpenAndClose) { 192 TEST_F(AudioInputTest, MAYBE_OpenAndClose) {
128 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); 193 if (!CanRunAudioTests())
129 if (!CanRunAudioTests(audio_man.get()))
130 return; 194 return;
131 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); 195 MakeAudioInputStreamOnAudioThread();
132 EXPECT_TRUE(ais->Open()); 196 OpenAndCloseAudioInputStreamOnAudioThread();
133 ais->Close();
134 } 197 }
135 198
136 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY) 199 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
137 // This test is failing on ARM linux: http://crbug.com/238490 200 // This test is failing on ARM linux: http://crbug.com/238490
138 #define MAYBE_OpenStopAndClose DISABLED_OpenStopAndClose 201 #define MAYBE_OpenStopAndClose DISABLED_OpenStopAndClose
139 #else 202 #else
140 #define MAYBE_OpenStopAndClose OpenStopAndClose 203 #define MAYBE_OpenStopAndClose OpenStopAndClose
141 #endif 204 #endif
142 // Test create, open, stop and close of an AudioInputStream without recording. 205 // Test create, open, stop and close of an AudioInputStream without recording.
143 TEST(AudioInputTest, MAYBE_OpenStopAndClose) { 206 TEST_F(AudioInputTest, MAYBE_OpenStopAndClose) {
144 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); 207 if (!CanRunAudioTests())
145 if (!CanRunAudioTests(audio_man.get()))
146 return; 208 return;
147 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get()); 209 MakeAudioInputStreamOnAudioThread();
148 EXPECT_TRUE(ais->Open()); 210 OpenStopAndCloseAudioInputStreamOnAudioThread();
149 ais->Stop();
150 ais->Close();
151 } 211 }
152 212
153 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY) 213 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
154 // This test is failing on ARM linux: http://crbug.com/238490 214 // This test is failing on ARM linux: http://crbug.com/238490
155 #define MAYBE_Record DISABLED_Record 215 #define MAYBE_Record DISABLED_Record
156 #else 216 #else
157 #define MAYBE_Record Record 217 #define MAYBE_Record Record
158 #endif 218 #endif
159 // Test a normal recording sequence using an AudioInputStream. 219 // Test a normal recording sequence using an AudioInputStream.
160 TEST(AudioInputTest, MAYBE_Record) { 220 // Very simple test which starts capturing during half a second and verifies
161 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); 221 // that recording starts.
162 if (!CanRunAudioTests(audio_man.get())) 222 TEST_F(AudioInputTest, MAYBE_Record) {
223 if (!CanRunAudioTests())
163 return; 224 return;
164 base::MessageLoop message_loop; 225 MakeAudioInputStreamOnAudioThread();
165 AudioInputStream* ais = CreateTestAudioInputStream(audio_man.get());
166 EXPECT_TRUE(ais->Open());
167 226
168 TestInputCallback test_callback(kSamplesPerPacket * 4); 227 TestInputCallback test_callback;
169 ais->Start(&test_callback); 228 OpenAndStartAudioInputStreamOnAudioThread(&test_callback);
170 // Verify at least 500ms worth of audio was recorded, after giving sufficient 229
171 // extra time. 230 message_loop_.PostDelayedTask(
172 message_loop.PostDelayedTask(
173 FROM_HERE, 231 FROM_HERE,
174 base::MessageLoop::QuitClosure(), 232 base::MessageLoop::QuitClosure(),
175 base::TimeDelta::FromMilliseconds(690)); 233 base::TimeDelta::FromMilliseconds(500));
176 message_loop.Run(); 234 message_loop_.Run();
177 EXPECT_GE(test_callback.callback_count(), 1); 235 EXPECT_GE(test_callback.callback_count(), 2);
178 EXPECT_FALSE(test_callback.had_error()); 236 EXPECT_FALSE(test_callback.had_error());
179 237
180 ais->Stop(); 238 StopAndCloseAudioInputStreamOnAudioThread();
181 ais->Close();
182 } 239 }
183 240
184 } // namespace media 241 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698