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

Side by Side Diff: media/audio/linux/cras_input_unittest.cc

Issue 10592014: media/audio/linux: Add CrasInputStream. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: allow repeated starts Created 8 years, 6 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <unistd.h>
6
7 #include <string>
8
9 #include "base/time.h"
10 #include "media/audio/linux/audio_manager_linux.h"
11 #include "media/audio/linux/cras_input.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 using testing::_;
16 using testing::AtLeast;
17 using testing::Ge;
18 using testing::StrictMock;
19
20 namespace media {
21
22 class MockAudioInputCallback : public AudioInputStream::AudioInputCallback {
23 public:
24 MOCK_METHOD5(OnData, void(
25 AudioInputStream*, const uint8*, uint32, uint32, double));
26 MOCK_METHOD2(OnError, void(AudioInputStream*, int));
27 MOCK_METHOD1(OnClose, void(AudioInputStream*));
28 };
29
30 class MockAudioManagerLinuxInput : public AudioManagerLinux {
31 public:
32 // We need to override this function in order to skip the checking the number
33 // of active output streams. It is because the number of active streams
34 // is managed inside MakeAudioInputStream, and we don't use
35 // MakeAudioInputStream to create the stream in the tests.
36 virtual void ReleaseInputStream(AudioInputStream* stream) OVERRIDE {
37 DCHECK(stream);
38 delete stream;
39 }
40 };
41
42 class CrasInputStreamTest : public testing::Test {
43 protected:
44 CrasInputStreamTest() {
45 mock_manager_.reset(new StrictMock<MockAudioManagerLinuxInput>());
46 }
47
48 virtual ~CrasInputStreamTest() {
49 }
50
51 CrasInputStream* CreateStream(ChannelLayout layout) {
52 return CreateStream(layout, kTestFramesPerPacket);
53 }
54
55 CrasInputStream* CreateStream(ChannelLayout layout,
56 int32 samples_per_packet) {
57 AudioParameters params(kTestFormat,
58 layout,
59 kTestSampleRate,
60 kTestBitsPerSample,
61 samples_per_packet);
62 return new CrasInputStream(params, mock_manager_.get());
63 }
64
65 void CaptureSomeFrames(const AudioParameters &params,
66 unsigned int duration_ms) {
67 CrasInputStream* test_stream = new CrasInputStream(params,
68 mock_manager_.get());
69
70 ASSERT_TRUE(test_stream->Open());
71
72 // Allow 8 frames variance for SRC in the callback. Different numbers of
73 // samples can be provided when doing non-integer SRC. For example
74 // converting from 192k to 44.1k is a ratio of 4.35 to 1.
75 MockAudioInputCallback mock_callback;
76 unsigned int expected_size = (kTestFramesPerPacket - 8) *
77 params.channels() *
78 params.bits_per_sample() / 8;
79 EXPECT_CALL(mock_callback,
80 OnData(test_stream, _, Ge(expected_size), _, _))
81 .Times(AtLeast(1));
82
83 test_stream->Start(&mock_callback);
84
85 usleep(duration_ms * base::Time::kMillisecondsPerSecond);
scherkus (not reviewing) 2012/06/26 22:10:10 when running on build bots if OnData() isn't calle
dgreid 2012/06/27 07:58:07 Slick, Since being called once is sufficient, this
86
87 EXPECT_CALL(mock_callback, OnClose(test_stream)).Times(1);
88 test_stream->Stop();
89
90 test_stream->Close();
91 }
92
93 static const unsigned int kTestBitsPerSample;
94 static const unsigned int kTestCaptureDurationMs;
95 static const ChannelLayout kTestChannelLayout;
96 static const AudioParameters::Format kTestFormat;
97 static const uint32 kTestFramesPerPacket;
98 static const int kTestSampleRate;
99
100 scoped_ptr<StrictMock<MockAudioManagerLinuxInput> > mock_manager_;
101
102 private:
103 DISALLOW_COPY_AND_ASSIGN(CrasInputStreamTest);
104 };
105
106 const unsigned int CrasInputStreamTest::kTestBitsPerSample = 16;
107 const unsigned int CrasInputStreamTest::kTestCaptureDurationMs = 250;
108 const ChannelLayout CrasInputStreamTest::kTestChannelLayout =
109 CHANNEL_LAYOUT_STEREO;
110 const AudioParameters::Format CrasInputStreamTest::kTestFormat =
111 AudioParameters::AUDIO_PCM_LINEAR;
112 const uint32 CrasInputStreamTest::kTestFramesPerPacket = 1000;
113 const int CrasInputStreamTest::kTestSampleRate = 44100;
114
115 TEST_F(CrasInputStreamTest, OpenMono) {
116 CrasInputStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO);
117 EXPECT_TRUE(test_stream->Open());
118 test_stream->Close();
119 }
120
121 TEST_F(CrasInputStreamTest, OpenStereo) {
122 CrasInputStream* test_stream = CreateStream(CHANNEL_LAYOUT_STEREO);
123 EXPECT_TRUE(test_stream->Open());
124 test_stream->Close();
125 }
126
127 TEST_F(CrasInputStreamTest, BadBitsPerSample) {
128 AudioParameters bad_bps_params(kTestFormat,
129 kTestChannelLayout,
130 kTestSampleRate,
131 kTestBitsPerSample - 1,
132 kTestFramesPerPacket);
133 CrasInputStream* test_stream =
134 new CrasInputStream(bad_bps_params, mock_manager_.get());
135 EXPECT_FALSE(test_stream->Open());
136 test_stream->Close();
137 }
138
139 TEST_F(CrasInputStreamTest, BadFormat) {
140 AudioParameters bad_format_params(AudioParameters::AUDIO_LAST_FORMAT,
141 kTestChannelLayout,
142 kTestSampleRate,
143 kTestBitsPerSample,
144 kTestFramesPerPacket);
145 CrasInputStream* test_stream =
146 new CrasInputStream(bad_format_params, mock_manager_.get());
147 EXPECT_FALSE(test_stream->Open());
148 test_stream->Close();
149 }
150
151 TEST_F(CrasInputStreamTest, BadSampleRate) {
152 AudioParameters bad_rate_params(kTestFormat,
153 kTestChannelLayout,
154 0,
155 kTestBitsPerSample,
156 kTestFramesPerPacket);
157 CrasInputStream* test_stream =
158 new CrasInputStream(bad_rate_params, mock_manager_.get());
159 EXPECT_FALSE(test_stream->Open());
160 test_stream->Close();
161 }
162
163 TEST_F(CrasInputStreamTest, DoubleStart) {
164 AudioParameters bad_rate_params(kTestFormat,
165 kTestChannelLayout,
166 kTestSampleRate,
167 kTestBitsPerSample,
168 kTestFramesPerPacket);
169 CrasInputStream* test_stream =
170 new CrasInputStream(bad_rate_params, mock_manager_.get());
171 ASSERT_TRUE(test_stream->Open());
172
173 // Start the stream with one callback.
174 MockAudioInputCallback mock_callback;
175 EXPECT_CALL(mock_callback, OnData(test_stream, _, _, _, _))
176 .Times(AtLeast(1));
177 test_stream->Start(&mock_callback);
178
179 usleep(kTestCaptureDurationMs * base::Time::kMillisecondsPerSecond);
180
181 // Then re-start the stream with a seconds callback.
scherkus (not reviewing) 2012/06/26 22:10:10 s/seconds/second
dgreid 2012/06/27 07:58:07 removed.
182 MockAudioInputCallback mock_callback2;
183 EXPECT_CALL(mock_callback, OnClose(test_stream)).Times(1);
184 EXPECT_CALL(mock_callback2, OnData(test_stream, _, _, _, _))
185 .Times(AtLeast(1));
186 test_stream->Start(&mock_callback2);
187
188 usleep(kTestCaptureDurationMs * base::Time::kMillisecondsPerSecond);
189
190 EXPECT_CALL(mock_callback2, OnClose(test_stream)).Times(1);
191 test_stream->Stop();
192
193 test_stream->Close();
194 }
195
196 TEST_F(CrasInputStreamTest, CaptureFrames) {
197 const unsigned int rates[] =
198 {8000, 16000, 22050, 32000, 44100, 48000, 96000, 192000};
199
200 for (unsigned int i = 0; i < ARRAY_SIZE(rates); i++) {
201 AudioParameters params_mono(kTestFormat,
202 CHANNEL_LAYOUT_MONO,
203 rates[i],
204 kTestBitsPerSample,
205 kTestFramesPerPacket);
206 CaptureSomeFrames(params_mono, kTestCaptureDurationMs);
207
208 AudioParameters params_stereo(kTestFormat,
209 CHANNEL_LAYOUT_STEREO,
210 rates[i],
211 kTestBitsPerSample,
212 kTestFramesPerPacket);
213 CaptureSomeFrames(params_stereo, kTestCaptureDurationMs);
214 }
215 }
216
217 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698