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

Side by Side Diff: media/audio/win/audio_input_win_unittest.cc

Issue 2966005: Add recording capability to AudioManager, and implemented on windows using the WaveIn APIs. (Closed)
Patch Set: Patch Created 10 years, 5 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
« no previous file with comments | « media/audio/openbsd/audio_manager_openbsd.cc ('k') | media/audio/win/audio_manager_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <windows.h>
6
7 #include "base/basictypes.h"
8 #include "media/audio/audio_io.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace {
12
13 // This class allows to find out if the callbacks are occurring as
14 // expected and if any error has been reported.
15 class TestInputCallback : public AudioInputStream::AudioInputCallback {
16 public:
17 explicit TestInputCallback(int max_data_bytes)
18 : callback_count_(0),
19 had_error_(0),
20 was_closed_(0),
21 max_data_bytes_(max_data_bytes) {
22 }
23 virtual void OnData(AudioInputStream* stream, const uint8* data,
24 uint32 size) {
25 ++callback_count_;
26 // Read the first byte to make sure memory is good.
27 if (size) {
28 ASSERT_LE(static_cast<int>(size), max_data_bytes_);
29 EXPECT_TRUE(data[0] >= 0);
30 }
31 }
32 virtual void OnClose(AudioInputStream* stream) {
33 ++was_closed_;
34 }
35 virtual void OnError(AudioInputStream* stream, int code) {
36 ++had_error_;
37 }
38 // Returns how many times OnData() has been called.
39 int callback_count() const {
40 return callback_count_;
41 }
42 // Returns how many times the OnError callback was called.
43 int had_error() const {
44 return had_error_;
45 }
46
47 void set_error(bool error) {
48 had_error_ += error ? 1 : 0;
49 }
50 // Returns how many times the OnClose callback was called.
51 int was_closed() const {
52 return was_closed_;
53 }
54
55 private:
56 int callback_count_;
57 int had_error_;
58 int was_closed_;
59 int max_data_bytes_;
60 };
61
62 // Specializes TestInputCallback to simulate a sink that blocks for some time
63 // in the OnData callback.
64 class TestInputCallbackBlocking : public TestInputCallback {
65 public:
66 TestInputCallbackBlocking(int max_data_bytes, int block_after_callback,
67 int block_for_ms)
68 : TestInputCallback(max_data_bytes),
69 block_after_callback_(block_after_callback),
70 block_for_ms_(block_for_ms) {
71 }
72 virtual void OnData(AudioInputStream* stream, const uint8* data,
73 uint32 size) {
74 // Call the base, which increments the callback_count_.
75 TestInputCallback::OnData(stream, data, size);
76 if (callback_count() > block_after_callback_)
77 ::Sleep(block_for_ms_);
78 }
79
80 private:
81 int block_after_callback_;
82 int block_for_ms_;
83 };
84
85 bool CanRunAudioTests() {
86 AudioManager* audio_man = AudioManager::GetAudioManager();
87 if (NULL == audio_man)
88 return false;
89 return (audio_man->HasAudioInputDevices() &&
90 ::GetEnvironmentVariableW(L"CHROME_HEADLESS", NULL, 0) == 0);
91 }
92
93 } // namespace.
94
95 // Test that AudioInputStream rejects out of range parameters.
96 TEST(WinAudioInputTest, SanityOnMakeParams) {
97 if (!CanRunAudioTests())
98 return;
99 AudioManager* audio_man = AudioManager::GetAudioManager();
100 AudioManager::Format fmt = AudioManager::AUDIO_PCM_LINEAR;
101 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(fmt, 8, 8000, 16, 0));
102 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(fmt, 1, 1024 * 1024, 16,
103 0));
104 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(fmt, 2, 8000, 80, 0));
105 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(fmt, 2, 8000, 80,
106 1024 * 4096));
107 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(fmt, -2, 8000, 16, 0));
108 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(fmt, 2, -8000, 16, 0));
109 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(fmt, 2, 8000, -16, 0));
110 EXPECT_TRUE(NULL == audio_man->MakeAudioInputStream(fmt, 2, 8000, 16, -1024));
111 }
112
113 // Test create and close of an AudioInputStream without recording audio.
114 TEST(WinAudioInputTest, CreateAndClose) {
115 if (!CanRunAudioTests())
116 return;
117 AudioManager* audio_man = AudioManager::GetAudioManager();
118 AudioInputStream* ais = audio_man->MakeAudioInputStream(
119 AudioManager::AUDIO_PCM_LINEAR, 2, 8000, 16, 0);
120 ASSERT_TRUE(NULL != ais);
121 ais->Close();
122 }
123
124 // Test create, open and close of an AudioInputStream without recording audio.
125 TEST(WinAudioInputTest, OpenAndClose) {
126 if (!CanRunAudioTests())
127 return;
128 AudioManager* audio_man = AudioManager::GetAudioManager();
129 AudioInputStream* ais = audio_man->MakeAudioInputStream(
130 AudioManager::AUDIO_PCM_LINEAR, 2, 8000, 16, 0);
131 ASSERT_TRUE(NULL != ais);
132 EXPECT_TRUE(ais->Open());
133 ais->Close();
134 }
135
136 // Test a normal recording sequence using an AudioInputStream.
137 TEST(WinAudioInputTest, Record) {
138 if (!CanRunAudioTests())
139 return;
140 AudioManager* audio_man = AudioManager::GetAudioManager();
141 const int kSamplingRate = 8000;
142 const int kSamplesPerPacket = kSamplingRate / 20;
143 AudioInputStream* ais = audio_man->MakeAudioInputStream(
144 AudioManager::AUDIO_PCM_LINEAR, 2, kSamplingRate, 16, kSamplesPerPacket);
145 ASSERT_TRUE(NULL != ais);
146 EXPECT_TRUE(ais->Open());
147
148 TestInputCallback test_callback(kSamplesPerPacket * 4);
149 ais->Start(&test_callback);
150 // Verify at least 500ms worth of audio was recorded, after giving sufficient
151 // extra time.
152 Sleep(590);
153 EXPECT_GE(test_callback.callback_count(), 10);
154 EXPECT_FALSE(test_callback.had_error());
155
156 ais->Stop();
157 ais->Close();
158 }
159
160 // Test a recording sequence with delays in the audio callback.
161 TEST(WinAudioInputTest, RecordWithSlowSink) {
162 if (!CanRunAudioTests())
163 return;
164 AudioManager* audio_man = AudioManager::GetAudioManager();
165 const int kSamplingRate = 8000;
166 const int kSamplesPerPacket = kSamplingRate / 20;
167 AudioInputStream* ais = audio_man->MakeAudioInputStream(
168 AudioManager::AUDIO_PCM_LINEAR, 2, kSamplingRate, 16, kSamplesPerPacket);
169 ASSERT_TRUE(NULL != ais);
170 EXPECT_TRUE(ais->Open());
171
172 // We should normally get a callback every 50ms, and a 20ms delay inside each
173 // callback should not change this sequence.
174 TestInputCallbackBlocking test_callback(kSamplesPerPacket * 4, 0, 20);
175 ais->Start(&test_callback);
176 // Verify at least 500ms worth of audio was recorded, after giving sufficient
177 // extra time.
178 Sleep(590);
179 EXPECT_GE(test_callback.callback_count(), 10);
180 EXPECT_FALSE(test_callback.had_error());
181
182 ais->Stop();
183 ais->Close();
184 }
OLDNEW
« no previous file with comments | « media/audio/openbsd/audio_manager_openbsd.cc ('k') | media/audio/win/audio_manager_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698