crazy linker: convert relocation unpacking to Android style.
[chromium-blink-merge.git] / media / audio / cras / cras_input_unittest.cc
blob4352b58a773805d8480e98761cac9760954f0d22
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.
5 #include <string>
7 #include "base/synchronization/waitable_event.h"
8 #include "base/test/test_timeouts.h"
9 #include "base/time/time.h"
10 #include "media/audio/cras/audio_manager_cras.h"
11 #include "media/audio/fake_audio_log_factory.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 // cras_util.h defines custom min/max macros which break compilation, so ensure
16 // it's not included until last. #if avoids presubmit errors.
17 #if defined(USE_CRAS)
18 #include "media/audio/cras/cras_input.h"
19 #endif
21 using testing::_;
22 using testing::AtLeast;
23 using testing::Ge;
24 using testing::InvokeWithoutArgs;
25 using testing::StrictMock;
27 namespace media {
29 class MockAudioInputCallback : public AudioInputStream::AudioInputCallback {
30 public:
31 MOCK_METHOD4(OnData,
32 void(AudioInputStream*, const AudioBus*, uint32, double));
33 MOCK_METHOD1(OnError, void(AudioInputStream*));
36 class MockAudioManagerCrasInput : public AudioManagerCras {
37 public:
38 MockAudioManagerCrasInput() : AudioManagerCras(&fake_audio_log_factory_) {}
40 // We need to override this function in order to skip checking the number
41 // of active output streams. It is because the number of active streams
42 // is managed inside MakeAudioInputStream, and we don't use
43 // MakeAudioInputStream to create the stream in the tests.
44 void ReleaseInputStream(AudioInputStream* stream) override {
45 DCHECK(stream);
46 delete stream;
49 private:
50 FakeAudioLogFactory fake_audio_log_factory_;
53 class CrasInputStreamTest : public testing::Test {
54 protected:
55 CrasInputStreamTest() {
56 mock_manager_.reset(new StrictMock<MockAudioManagerCrasInput>());
59 virtual ~CrasInputStreamTest() {
62 CrasInputStream* CreateStream(ChannelLayout layout) {
63 return CreateStream(layout, kTestFramesPerPacket);
66 CrasInputStream* CreateStream(ChannelLayout layout,
67 int32 samples_per_packet) {
68 return CreateStream(layout, samples_per_packet,
69 AudioManagerBase::kDefaultDeviceId);
72 CrasInputStream* CreateStream(ChannelLayout layout,
73 int32 samples_per_packet,
74 const std::string& device_id) {
75 AudioParameters params(kTestFormat,
76 layout,
77 kTestSampleRate,
78 kTestBitsPerSample,
79 samples_per_packet);
80 return new CrasInputStream(params, mock_manager_.get(), device_id);
83 void CaptureSomeFrames(const AudioParameters &params,
84 unsigned int duration_ms) {
85 CrasInputStream* test_stream = new CrasInputStream(
86 params, mock_manager_.get(), AudioManagerBase::kDefaultDeviceId);
88 ASSERT_TRUE(test_stream->Open());
90 // Allow 8 frames variance for SRC in the callback. Different numbers of
91 // samples can be provided when doing non-integer SRC. For example
92 // converting from 192k to 44.1k is a ratio of 4.35 to 1.
93 MockAudioInputCallback mock_callback;
94 base::WaitableEvent event(false, false);
96 EXPECT_CALL(mock_callback, OnData(test_stream, _, _, _))
97 .WillOnce(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal));
99 test_stream->Start(&mock_callback);
101 // Wait for samples to be captured.
102 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_timeout()));
104 test_stream->Stop();
105 test_stream->Close();
108 static const unsigned int kTestBitsPerSample;
109 static const unsigned int kTestCaptureDurationMs;
110 static const ChannelLayout kTestChannelLayout;
111 static const AudioParameters::Format kTestFormat;
112 static const uint32 kTestFramesPerPacket;
113 static const int kTestSampleRate;
115 scoped_ptr<StrictMock<MockAudioManagerCrasInput> > mock_manager_;
117 private:
118 DISALLOW_COPY_AND_ASSIGN(CrasInputStreamTest);
121 const unsigned int CrasInputStreamTest::kTestBitsPerSample = 16;
122 const unsigned int CrasInputStreamTest::kTestCaptureDurationMs = 250;
123 const ChannelLayout CrasInputStreamTest::kTestChannelLayout =
124 CHANNEL_LAYOUT_STEREO;
125 const AudioParameters::Format CrasInputStreamTest::kTestFormat =
126 AudioParameters::AUDIO_PCM_LINEAR;
127 const uint32 CrasInputStreamTest::kTestFramesPerPacket = 1000;
128 const int CrasInputStreamTest::kTestSampleRate = 44100;
130 TEST_F(CrasInputStreamTest, OpenMono) {
131 CrasInputStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO);
132 EXPECT_TRUE(test_stream->Open());
133 test_stream->Close();
136 TEST_F(CrasInputStreamTest, OpenStereo) {
137 CrasInputStream* test_stream = CreateStream(CHANNEL_LAYOUT_STEREO);
138 EXPECT_TRUE(test_stream->Open());
139 test_stream->Close();
142 TEST_F(CrasInputStreamTest, BadBitsPerSample) {
143 AudioParameters bad_bps_params(kTestFormat,
144 kTestChannelLayout,
145 kTestSampleRate,
146 kTestBitsPerSample - 1,
147 kTestFramesPerPacket);
148 CrasInputStream* test_stream = new CrasInputStream(
149 bad_bps_params, mock_manager_.get(), AudioManagerBase::kDefaultDeviceId);
150 EXPECT_FALSE(test_stream->Open());
151 test_stream->Close();
154 TEST_F(CrasInputStreamTest, BadFormat) {
155 AudioParameters bad_format_params(AudioParameters::AUDIO_LAST_FORMAT,
156 kTestChannelLayout,
157 kTestSampleRate,
158 kTestBitsPerSample,
159 kTestFramesPerPacket);
160 CrasInputStream* test_stream = new CrasInputStream(
161 bad_format_params, mock_manager_.get(),
162 AudioManagerBase::kDefaultDeviceId);
163 EXPECT_FALSE(test_stream->Open());
164 test_stream->Close();
167 TEST_F(CrasInputStreamTest, BadSampleRate) {
168 AudioParameters bad_rate_params(kTestFormat,
169 kTestChannelLayout,
171 kTestBitsPerSample,
172 kTestFramesPerPacket);
173 CrasInputStream* test_stream = new CrasInputStream(
174 bad_rate_params, mock_manager_.get(), AudioManagerBase::kDefaultDeviceId);
175 EXPECT_FALSE(test_stream->Open());
176 test_stream->Close();
179 TEST_F(CrasInputStreamTest, SetGetVolume) {
180 CrasInputStream* test_stream = CreateStream(CHANNEL_LAYOUT_MONO);
181 EXPECT_TRUE(test_stream->Open());
183 double max_volume = test_stream->GetMaxVolume();
184 EXPECT_GE(max_volume, 1.0);
186 test_stream->SetVolume(max_volume / 2);
188 double new_volume = test_stream->GetVolume();
190 EXPECT_GE(new_volume, 0.0);
191 EXPECT_LE(new_volume, max_volume);
193 test_stream->Close();
196 TEST_F(CrasInputStreamTest, CaptureFrames) {
197 const unsigned int rates[] =
198 {8000, 16000, 22050, 32000, 44100, 48000, 96000, 192000};
200 for (unsigned int i = 0; i < ARRAY_SIZE(rates); i++) {
201 SCOPED_TRACE(testing::Message() << "Mono " << rates[i] << "Hz");
202 AudioParameters params_mono(kTestFormat,
203 CHANNEL_LAYOUT_MONO,
204 rates[i],
205 kTestBitsPerSample,
206 kTestFramesPerPacket);
207 CaptureSomeFrames(params_mono, kTestCaptureDurationMs);
210 for (unsigned int i = 0; i < ARRAY_SIZE(rates); i++) {
211 SCOPED_TRACE(testing::Message() << "Stereo " << rates[i] << "Hz");
212 AudioParameters params_stereo(kTestFormat,
213 CHANNEL_LAYOUT_STEREO,
214 rates[i],
215 kTestBitsPerSample,
216 kTestFramesPerPacket);
217 CaptureSomeFrames(params_stereo, kTestCaptureDurationMs);
221 TEST_F(CrasInputStreamTest, CaptureLoopback) {
222 CrasInputStream* test_stream = CreateStream(
223 CHANNEL_LAYOUT_STEREO,
224 kTestFramesPerPacket,
225 AudioManagerBase::kLoopbackInputDeviceId);
226 EXPECT_TRUE(test_stream->Open());
227 test_stream->Close();
230 } // namespace media