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.
9 #include "base/synchronization/waitable_event.h"
10 #include "base/test/test_timeouts.h"
11 #include "base/time.h"
12 #include "media/audio/cras/audio_manager_cras.h"
13 #include "media/audio/cras/cras_input.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
18 using testing::AtLeast
;
20 using testing::InvokeWithoutArgs
;
21 using testing::StrictMock
;
25 class MockAudioInputCallback
: public AudioInputStream::AudioInputCallback
{
27 MOCK_METHOD5(OnData
, void(
28 AudioInputStream
*, const uint8
*, uint32
, uint32
, double));
29 MOCK_METHOD1(OnError
, void(AudioInputStream
*));
30 MOCK_METHOD1(OnClose
, void(AudioInputStream
*));
33 class MockAudioManagerCrasInput
: public AudioManagerCras
{
35 // We need to override this function in order to skip checking the number
36 // of active output streams. It is because the number of active streams
37 // is managed inside MakeAudioInputStream, and we don't use
38 // MakeAudioInputStream to create the stream in the tests.
39 virtual void ReleaseInputStream(AudioInputStream
* stream
) OVERRIDE
{
45 class CrasInputStreamTest
: public testing::Test
{
47 CrasInputStreamTest() {
48 mock_manager_
.reset(new StrictMock
<MockAudioManagerCrasInput
>());
51 virtual ~CrasInputStreamTest() {
54 CrasInputStream
* CreateStream(ChannelLayout layout
) {
55 return CreateStream(layout
, kTestFramesPerPacket
);
58 CrasInputStream
* CreateStream(ChannelLayout layout
,
59 int32 samples_per_packet
) {
60 AudioParameters
params(kTestFormat
,
65 return new CrasInputStream(params
, mock_manager_
.get());
68 void CaptureSomeFrames(const AudioParameters
¶ms
,
69 unsigned int duration_ms
) {
70 CrasInputStream
* test_stream
= new CrasInputStream(params
,
73 ASSERT_TRUE(test_stream
->Open());
75 // Allow 8 frames variance for SRC in the callback. Different numbers of
76 // samples can be provided when doing non-integer SRC. For example
77 // converting from 192k to 44.1k is a ratio of 4.35 to 1.
78 MockAudioInputCallback mock_callback
;
79 unsigned int expected_size
= (kTestFramesPerPacket
- 8) *
81 params
.bits_per_sample() / 8;
83 base::WaitableEvent
event(false, false);
85 EXPECT_CALL(mock_callback
,
86 OnData(test_stream
, _
, Ge(expected_size
), _
, _
))
87 .WillOnce(InvokeWithoutArgs(&event
, &base::WaitableEvent::Signal
));
89 test_stream
->Start(&mock_callback
);
91 // Wait for samples to be captured.
92 EXPECT_TRUE(event
.TimedWait(TestTimeouts::action_timeout()));
96 EXPECT_CALL(mock_callback
, OnClose(test_stream
)).Times(1);
100 static const unsigned int kTestBitsPerSample
;
101 static const unsigned int kTestCaptureDurationMs
;
102 static const ChannelLayout kTestChannelLayout
;
103 static const AudioParameters::Format kTestFormat
;
104 static const uint32 kTestFramesPerPacket
;
105 static const int kTestSampleRate
;
107 scoped_ptr
<StrictMock
<MockAudioManagerCrasInput
> > mock_manager_
;
110 DISALLOW_COPY_AND_ASSIGN(CrasInputStreamTest
);
113 const unsigned int CrasInputStreamTest::kTestBitsPerSample
= 16;
114 const unsigned int CrasInputStreamTest::kTestCaptureDurationMs
= 250;
115 const ChannelLayout
CrasInputStreamTest::kTestChannelLayout
=
116 CHANNEL_LAYOUT_STEREO
;
117 const AudioParameters::Format
CrasInputStreamTest::kTestFormat
=
118 AudioParameters::AUDIO_PCM_LINEAR
;
119 const uint32
CrasInputStreamTest::kTestFramesPerPacket
= 1000;
120 const int CrasInputStreamTest::kTestSampleRate
= 44100;
122 TEST_F(CrasInputStreamTest
, OpenMono
) {
123 CrasInputStream
* test_stream
= CreateStream(CHANNEL_LAYOUT_MONO
);
124 EXPECT_TRUE(test_stream
->Open());
125 test_stream
->Close();
128 TEST_F(CrasInputStreamTest
, OpenStereo
) {
129 CrasInputStream
* test_stream
= CreateStream(CHANNEL_LAYOUT_STEREO
);
130 EXPECT_TRUE(test_stream
->Open());
131 test_stream
->Close();
134 TEST_F(CrasInputStreamTest
, BadBitsPerSample
) {
135 AudioParameters
bad_bps_params(kTestFormat
,
138 kTestBitsPerSample
- 1,
139 kTestFramesPerPacket
);
140 CrasInputStream
* test_stream
=
141 new CrasInputStream(bad_bps_params
, mock_manager_
.get());
142 EXPECT_FALSE(test_stream
->Open());
143 test_stream
->Close();
146 TEST_F(CrasInputStreamTest
, BadFormat
) {
147 AudioParameters
bad_format_params(AudioParameters::AUDIO_LAST_FORMAT
,
151 kTestFramesPerPacket
);
152 CrasInputStream
* test_stream
=
153 new CrasInputStream(bad_format_params
, mock_manager_
.get());
154 EXPECT_FALSE(test_stream
->Open());
155 test_stream
->Close();
158 TEST_F(CrasInputStreamTest
, BadSampleRate
) {
159 AudioParameters
bad_rate_params(kTestFormat
,
163 kTestFramesPerPacket
);
164 CrasInputStream
* test_stream
=
165 new CrasInputStream(bad_rate_params
, mock_manager_
.get());
166 EXPECT_FALSE(test_stream
->Open());
167 test_stream
->Close();
170 TEST_F(CrasInputStreamTest
, SetGetVolume
) {
171 CrasInputStream
* test_stream
= CreateStream(CHANNEL_LAYOUT_MONO
);
172 EXPECT_TRUE(test_stream
->Open());
174 double max_volume
= test_stream
->GetMaxVolume();
175 EXPECT_GE(max_volume
, 1.0);
177 test_stream
->SetVolume(max_volume
/ 2);
179 double new_volume
= test_stream
->GetVolume();
181 EXPECT_GE(new_volume
, 0.0);
182 EXPECT_LE(new_volume
, max_volume
);
184 test_stream
->Close();
187 TEST_F(CrasInputStreamTest
, CaptureFrames
) {
188 const unsigned int rates
[] =
189 {8000, 16000, 22050, 32000, 44100, 48000, 96000, 192000};
191 for (unsigned int i
= 0; i
< ARRAY_SIZE(rates
); i
++) {
192 SCOPED_TRACE(testing::Message() << "Mono " << rates
[i
] << "Hz");
193 AudioParameters
params_mono(kTestFormat
,
197 kTestFramesPerPacket
);
198 CaptureSomeFrames(params_mono
, kTestCaptureDurationMs
);
201 for (unsigned int i
= 0; i
< ARRAY_SIZE(rates
); i
++) {
202 SCOPED_TRACE(testing::Message() << "Stereo " << rates
[i
] << "Hz");
203 AudioParameters
params_stereo(kTestFormat
,
204 CHANNEL_LAYOUT_STEREO
,
207 kTestFramesPerPacket
);
208 CaptureSomeFrames(params_stereo
, kTestCaptureDurationMs
);