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.
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.
18 #include "media/audio/cras/cras_input.h"
22 using testing::AtLeast
;
24 using testing::InvokeWithoutArgs
;
25 using testing::StrictMock
;
29 class MockAudioInputCallback
: public AudioInputStream::AudioInputCallback
{
31 MOCK_METHOD5(OnData
, void(
32 AudioInputStream
*, const uint8
*, uint32
, uint32
, double));
33 MOCK_METHOD1(OnError
, void(AudioInputStream
*));
36 class MockAudioManagerCrasInput
: public AudioManagerCras
{
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 virtual void ReleaseInputStream(AudioInputStream
* stream
) OVERRIDE
{
50 FakeAudioLogFactory fake_audio_log_factory_
;
53 class CrasInputStreamTest
: public testing::Test
{
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 AudioParameters
params(kTestFormat
,
73 return new CrasInputStream(params
, mock_manager_
.get(),
74 AudioManagerBase::kDefaultDeviceId
);
77 void CaptureSomeFrames(const AudioParameters
¶ms
,
78 unsigned int duration_ms
) {
79 CrasInputStream
* test_stream
= new CrasInputStream(
80 params
, mock_manager_
.get(), AudioManagerBase::kDefaultDeviceId
);
82 ASSERT_TRUE(test_stream
->Open());
84 // Allow 8 frames variance for SRC in the callback. Different numbers of
85 // samples can be provided when doing non-integer SRC. For example
86 // converting from 192k to 44.1k is a ratio of 4.35 to 1.
87 MockAudioInputCallback mock_callback
;
88 unsigned int expected_size
= (kTestFramesPerPacket
- 8) *
90 params
.bits_per_sample() / 8;
92 base::WaitableEvent
event(false, false);
94 EXPECT_CALL(mock_callback
,
95 OnData(test_stream
, _
, Ge(expected_size
), _
, _
))
96 .WillOnce(InvokeWithoutArgs(&event
, &base::WaitableEvent::Signal
));
98 test_stream
->Start(&mock_callback
);
100 // Wait for samples to be captured.
101 EXPECT_TRUE(event
.TimedWait(TestTimeouts::action_timeout()));
104 test_stream
->Close();
107 static const unsigned int kTestBitsPerSample
;
108 static const unsigned int kTestCaptureDurationMs
;
109 static const ChannelLayout kTestChannelLayout
;
110 static const AudioParameters::Format kTestFormat
;
111 static const uint32 kTestFramesPerPacket
;
112 static const int kTestSampleRate
;
114 scoped_ptr
<StrictMock
<MockAudioManagerCrasInput
> > mock_manager_
;
117 DISALLOW_COPY_AND_ASSIGN(CrasInputStreamTest
);
120 const unsigned int CrasInputStreamTest::kTestBitsPerSample
= 16;
121 const unsigned int CrasInputStreamTest::kTestCaptureDurationMs
= 250;
122 const ChannelLayout
CrasInputStreamTest::kTestChannelLayout
=
123 CHANNEL_LAYOUT_STEREO
;
124 const AudioParameters::Format
CrasInputStreamTest::kTestFormat
=
125 AudioParameters::AUDIO_PCM_LINEAR
;
126 const uint32
CrasInputStreamTest::kTestFramesPerPacket
= 1000;
127 const int CrasInputStreamTest::kTestSampleRate
= 44100;
129 TEST_F(CrasInputStreamTest
, OpenMono
) {
130 CrasInputStream
* test_stream
= CreateStream(CHANNEL_LAYOUT_MONO
);
131 EXPECT_TRUE(test_stream
->Open());
132 test_stream
->Close();
135 TEST_F(CrasInputStreamTest
, OpenStereo
) {
136 CrasInputStream
* test_stream
= CreateStream(CHANNEL_LAYOUT_STEREO
);
137 EXPECT_TRUE(test_stream
->Open());
138 test_stream
->Close();
141 TEST_F(CrasInputStreamTest
, BadBitsPerSample
) {
142 AudioParameters
bad_bps_params(kTestFormat
,
145 kTestBitsPerSample
- 1,
146 kTestFramesPerPacket
);
147 CrasInputStream
* test_stream
= new CrasInputStream(
148 bad_bps_params
, mock_manager_
.get(), AudioManagerBase::kDefaultDeviceId
);
149 EXPECT_FALSE(test_stream
->Open());
150 test_stream
->Close();
153 TEST_F(CrasInputStreamTest
, BadFormat
) {
154 AudioParameters
bad_format_params(AudioParameters::AUDIO_LAST_FORMAT
,
158 kTestFramesPerPacket
);
159 CrasInputStream
* test_stream
= new CrasInputStream(
160 bad_format_params
, mock_manager_
.get(),
161 AudioManagerBase::kDefaultDeviceId
);
162 EXPECT_FALSE(test_stream
->Open());
163 test_stream
->Close();
166 TEST_F(CrasInputStreamTest
, BadSampleRate
) {
167 AudioParameters
bad_rate_params(kTestFormat
,
171 kTestFramesPerPacket
);
172 CrasInputStream
* test_stream
= new CrasInputStream(
173 bad_rate_params
, mock_manager_
.get(), AudioManagerBase::kDefaultDeviceId
);
174 EXPECT_FALSE(test_stream
->Open());
175 test_stream
->Close();
178 TEST_F(CrasInputStreamTest
, SetGetVolume
) {
179 CrasInputStream
* test_stream
= CreateStream(CHANNEL_LAYOUT_MONO
);
180 EXPECT_TRUE(test_stream
->Open());
182 double max_volume
= test_stream
->GetMaxVolume();
183 EXPECT_GE(max_volume
, 1.0);
185 test_stream
->SetVolume(max_volume
/ 2);
187 double new_volume
= test_stream
->GetVolume();
189 EXPECT_GE(new_volume
, 0.0);
190 EXPECT_LE(new_volume
, max_volume
);
192 test_stream
->Close();
195 TEST_F(CrasInputStreamTest
, CaptureFrames
) {
196 const unsigned int rates
[] =
197 {8000, 16000, 22050, 32000, 44100, 48000, 96000, 192000};
199 for (unsigned int i
= 0; i
< ARRAY_SIZE(rates
); i
++) {
200 SCOPED_TRACE(testing::Message() << "Mono " << rates
[i
] << "Hz");
201 AudioParameters
params_mono(kTestFormat
,
205 kTestFramesPerPacket
);
206 CaptureSomeFrames(params_mono
, kTestCaptureDurationMs
);
209 for (unsigned int i
= 0; i
< ARRAY_SIZE(rates
); i
++) {
210 SCOPED_TRACE(testing::Message() << "Stereo " << rates
[i
] << "Hz");
211 AudioParameters
params_stereo(kTestFormat
,
212 CHANNEL_LAYOUT_STEREO
,
215 kTestFramesPerPacket
);
216 CaptureSomeFrames(params_stereo
, kTestCaptureDurationMs
);