1 // Copyright 2013 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 "base/strings/stringprintf.h"
6 #include "media/audio/alsa/alsa_output.h"
7 #include "media/audio/alsa/alsa_wrapper.h"
8 #include "media/audio/alsa/audio_manager_alsa.h"
9 #include "media/audio/fake_audio_log_factory.h"
10 #include "media/audio/mock_audio_source_callback.h"
11 #include "media/base/data_buffer.h"
12 #include "media/base/seekable_buffer.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 using testing::AtLeast
;
21 using testing::InSequence
;
22 using testing::Invoke
;
23 using testing::InvokeWithoutArgs
;
25 using testing::MockFunction
;
26 using testing::Return
;
27 using testing::SetArgumentPointee
;
28 using testing::StrictMock
;
30 using testing::Unused
;
34 class MockAlsaWrapper
: public AlsaWrapper
{
36 MOCK_METHOD3(DeviceNameHint
, int(int card
,
39 MOCK_METHOD2(DeviceNameGetHint
, char*(const void* hint
, const char* id
));
40 MOCK_METHOD1(DeviceNameFreeHint
, int(void** hints
));
42 MOCK_METHOD4(PcmOpen
, int(snd_pcm_t
** handle
, const char* name
,
43 snd_pcm_stream_t stream
, int mode
));
44 MOCK_METHOD1(PcmClose
, int(snd_pcm_t
* handle
));
45 MOCK_METHOD1(PcmPrepare
, int(snd_pcm_t
* handle
));
46 MOCK_METHOD1(PcmDrop
, int(snd_pcm_t
* handle
));
47 MOCK_METHOD2(PcmDelay
, int(snd_pcm_t
* handle
, snd_pcm_sframes_t
* delay
));
48 MOCK_METHOD3(PcmWritei
, snd_pcm_sframes_t(snd_pcm_t
* handle
,
50 snd_pcm_uframes_t size
));
51 MOCK_METHOD3(PcmReadi
, snd_pcm_sframes_t(snd_pcm_t
* handle
,
53 snd_pcm_uframes_t size
));
54 MOCK_METHOD3(PcmRecover
, int(snd_pcm_t
* handle
, int err
, int silent
));
55 MOCK_METHOD7(PcmSetParams
, int(snd_pcm_t
* handle
, snd_pcm_format_t format
,
56 snd_pcm_access_t access
, unsigned int channels
,
57 unsigned int rate
, int soft_resample
,
58 unsigned int latency
));
59 MOCK_METHOD3(PcmGetParams
, int(snd_pcm_t
* handle
,
60 snd_pcm_uframes_t
* buffer_size
,
61 snd_pcm_uframes_t
* period_size
));
62 MOCK_METHOD1(PcmName
, const char*(snd_pcm_t
* handle
));
63 MOCK_METHOD1(PcmAvailUpdate
, snd_pcm_sframes_t(snd_pcm_t
* handle
));
64 MOCK_METHOD1(PcmState
, snd_pcm_state_t(snd_pcm_t
* handle
));
65 MOCK_METHOD1(PcmStart
, int(snd_pcm_t
* handle
));
67 MOCK_METHOD1(StrError
, const char*(int errnum
));
70 class MockAudioManagerAlsa
: public AudioManagerAlsa
{
72 MockAudioManagerAlsa() : AudioManagerAlsa(&fake_audio_log_factory_
) {}
73 MOCK_METHOD0(Init
, void());
74 MOCK_METHOD0(HasAudioOutputDevices
, bool());
75 MOCK_METHOD0(HasAudioInputDevices
, bool());
76 MOCK_METHOD1(MakeLinearOutputStream
, AudioOutputStream
*(
77 const AudioParameters
& params
));
78 MOCK_METHOD2(MakeLowLatencyOutputStream
, AudioOutputStream
*(
79 const AudioParameters
& params
,
80 const std::string
& device_id
));
81 MOCK_METHOD2(MakeLowLatencyInputStream
, AudioInputStream
*(
82 const AudioParameters
& params
, const std::string
& device_id
));
84 // We need to override this function in order to skip the checking the number
85 // of active output streams. It is because the number of active streams
86 // is managed inside MakeAudioOutputStream, and we don't use
87 // MakeAudioOutputStream to create the stream in the tests.
88 virtual void ReleaseOutputStream(AudioOutputStream
* stream
) override
{
93 // We don't mock this method since all tests will do the same thing
94 // and use the current task runner.
95 virtual scoped_refptr
<base::SingleThreadTaskRunner
> GetTaskRunner() override
{
96 return base::MessageLoop::current()->message_loop_proxy();
100 FakeAudioLogFactory fake_audio_log_factory_
;
103 class AlsaPcmOutputStreamTest
: public testing::Test
{
105 AlsaPcmOutputStreamTest() {
106 mock_manager_
.reset(new StrictMock
<MockAudioManagerAlsa
>());
109 virtual ~AlsaPcmOutputStreamTest() {
112 AlsaPcmOutputStream
* CreateStream(ChannelLayout layout
) {
113 return CreateStream(layout
, kTestFramesPerPacket
);
116 AlsaPcmOutputStream
* CreateStream(ChannelLayout layout
,
117 int32 samples_per_packet
) {
118 AudioParameters
params(kTestFormat
, layout
, kTestSampleRate
,
119 kTestBitsPerSample
, samples_per_packet
);
120 return new AlsaPcmOutputStream(kTestDeviceName
,
123 mock_manager_
.get());
126 // Helper function to malloc the string returned by DeviceNameHint for NAME.
127 static char* EchoHint(const void* name
, Unused
) {
128 return strdup(static_cast<const char*>(name
));
131 // Helper function to malloc the string returned by DeviceNameHint for IOID.
132 static char* OutputHint(Unused
, Unused
) {
133 return strdup("Output");
136 // Helper function to initialize |test_stream->buffer_|. Must be called
137 // in all tests that use buffer_ without opening the stream.
138 void InitBuffer(AlsaPcmOutputStream
* test_stream
) {
140 packet_
= new media::DataBuffer(kTestPacketSize
);
141 packet_
->set_data_size(kTestPacketSize
);
142 test_stream
->buffer_
.reset(new media::SeekableBuffer(0, kTestPacketSize
));
143 test_stream
->buffer_
->Append(packet_
.get());
146 static const ChannelLayout kTestChannelLayout
;
147 static const int kTestSampleRate
;
148 static const int kTestBitsPerSample
;
149 static const int kTestBytesPerFrame
;
150 static const AudioParameters::Format kTestFormat
;
151 static const char kTestDeviceName
[];
152 static const char kDummyMessage
[];
153 static const uint32 kTestFramesPerPacket
;
154 static const int kTestPacketSize
;
155 static const int kTestFailedErrno
;
156 static snd_pcm_t
* const kFakeHandle
;
158 // Used to simulate DeviceNameHint.
159 static char kSurround40
[];
160 static char kSurround41
[];
161 static char kSurround50
[];
162 static char kSurround51
[];
163 static char kSurround70
[];
164 static char kSurround71
[];
165 static void* kFakeHints
[];
166 static char kGenericSurround50
[];
168 StrictMock
<MockAlsaWrapper
> mock_alsa_wrapper_
;
169 scoped_ptr
<StrictMock
<MockAudioManagerAlsa
> > mock_manager_
;
170 base::MessageLoop message_loop_
;
171 scoped_refptr
<media::DataBuffer
> packet_
;
174 DISALLOW_COPY_AND_ASSIGN(AlsaPcmOutputStreamTest
);
177 const ChannelLayout
AlsaPcmOutputStreamTest::kTestChannelLayout
=
178 CHANNEL_LAYOUT_STEREO
;
179 const int AlsaPcmOutputStreamTest::kTestSampleRate
=
180 AudioParameters::kAudioCDSampleRate
;
181 const int AlsaPcmOutputStreamTest::kTestBitsPerSample
= 8;
182 const int AlsaPcmOutputStreamTest::kTestBytesPerFrame
=
183 AlsaPcmOutputStreamTest::kTestBitsPerSample
/ 8 *
184 ChannelLayoutToChannelCount(AlsaPcmOutputStreamTest::kTestChannelLayout
);
185 const AudioParameters::Format
AlsaPcmOutputStreamTest::kTestFormat
=
186 AudioParameters::AUDIO_PCM_LINEAR
;
187 const char AlsaPcmOutputStreamTest::kTestDeviceName
[] = "TestDevice";
188 const char AlsaPcmOutputStreamTest::kDummyMessage
[] = "dummy";
189 const uint32
AlsaPcmOutputStreamTest::kTestFramesPerPacket
= 1000;
190 const int AlsaPcmOutputStreamTest::kTestPacketSize
=
191 AlsaPcmOutputStreamTest::kTestFramesPerPacket
*
192 AlsaPcmOutputStreamTest::kTestBytesPerFrame
;
193 const int AlsaPcmOutputStreamTest::kTestFailedErrno
= -EACCES
;
194 snd_pcm_t
* const AlsaPcmOutputStreamTest::kFakeHandle
=
195 reinterpret_cast<snd_pcm_t
*>(1);
197 char AlsaPcmOutputStreamTest::kSurround40
[] = "surround40:CARD=foo,DEV=0";
198 char AlsaPcmOutputStreamTest::kSurround41
[] = "surround41:CARD=foo,DEV=0";
199 char AlsaPcmOutputStreamTest::kSurround50
[] = "surround50:CARD=foo,DEV=0";
200 char AlsaPcmOutputStreamTest::kSurround51
[] = "surround51:CARD=foo,DEV=0";
201 char AlsaPcmOutputStreamTest::kSurround70
[] = "surround70:CARD=foo,DEV=0";
202 char AlsaPcmOutputStreamTest::kSurround71
[] = "surround71:CARD=foo,DEV=0";
203 void* AlsaPcmOutputStreamTest::kFakeHints
[] = {
204 kSurround40
, kSurround41
, kSurround50
, kSurround51
,
205 kSurround70
, kSurround71
, NULL
};
206 char AlsaPcmOutputStreamTest::kGenericSurround50
[] = "surround50";
208 // Custom action to clear a memory buffer.
209 ACTION(ClearBuffer
) {
213 TEST_F(AlsaPcmOutputStreamTest
, ConstructedState
) {
214 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
215 EXPECT_EQ(AlsaPcmOutputStream::kCreated
, test_stream
->state());
216 test_stream
->Close();
218 // Should support mono.
219 test_stream
= CreateStream(CHANNEL_LAYOUT_MONO
);
220 EXPECT_EQ(AlsaPcmOutputStream::kCreated
, test_stream
->state());
221 test_stream
->Close();
223 // Should support multi-channel.
224 test_stream
= CreateStream(CHANNEL_LAYOUT_SURROUND
);
225 EXPECT_EQ(AlsaPcmOutputStream::kCreated
, test_stream
->state());
226 test_stream
->Close();
228 // Bad bits per sample.
229 AudioParameters
bad_bps_params(kTestFormat
, kTestChannelLayout
,
230 kTestSampleRate
, kTestBitsPerSample
- 1,
231 kTestFramesPerPacket
);
232 test_stream
= new AlsaPcmOutputStream(kTestDeviceName
,
235 mock_manager_
.get());
236 EXPECT_EQ(AlsaPcmOutputStream::kInError
, test_stream
->state());
237 test_stream
->Close();
240 AudioParameters
bad_format_params(
241 AudioParameters::AUDIO_LAST_FORMAT
, kTestChannelLayout
, kTestSampleRate
,
242 kTestBitsPerSample
, kTestFramesPerPacket
);
243 test_stream
= new AlsaPcmOutputStream(kTestDeviceName
,
246 mock_manager_
.get());
247 EXPECT_EQ(AlsaPcmOutputStream::kInError
, test_stream
->state());
248 test_stream
->Close();
251 TEST_F(AlsaPcmOutputStreamTest
, LatencyFloor
) {
252 const double kMicrosPerFrame
=
253 static_cast<double>(1000000) / kTestSampleRate
;
254 const double kPacketFramesInMinLatency
=
255 AlsaPcmOutputStream::kMinLatencyMicros
/ kMicrosPerFrame
/ 2.0;
257 // Test that packets which would cause a latency under less than
258 // AlsaPcmOutputStream::kMinLatencyMicros will get clipped to
259 // AlsaPcmOutputStream::kMinLatencyMicros,
260 EXPECT_CALL(mock_alsa_wrapper_
, PcmOpen(_
, _
, _
, _
))
261 .WillOnce(DoAll(SetArgumentPointee
<0>(kFakeHandle
),
263 EXPECT_CALL(mock_alsa_wrapper_
,
264 PcmSetParams(_
, _
, _
, _
, _
, _
,
265 AlsaPcmOutputStream::kMinLatencyMicros
))
266 .WillOnce(Return(0));
267 EXPECT_CALL(mock_alsa_wrapper_
, PcmGetParams(_
, _
, _
))
268 .WillOnce(DoAll(SetArgumentPointee
<1>(kTestFramesPerPacket
),
269 SetArgumentPointee
<2>(kTestFramesPerPacket
/ 2),
272 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
,
273 kPacketFramesInMinLatency
);
274 ASSERT_TRUE(test_stream
->Open());
276 // Now close it and test that everything was released.
277 EXPECT_CALL(mock_alsa_wrapper_
, PcmClose(kFakeHandle
)).WillOnce(Return(0));
278 EXPECT_CALL(mock_alsa_wrapper_
, PcmName(kFakeHandle
))
279 .WillOnce(Return(kTestDeviceName
));
280 test_stream
->Close();
282 Mock::VerifyAndClear(&mock_alsa_wrapper_
);
283 Mock::VerifyAndClear(mock_manager_
.get());
285 // Test that having more packets ends up with a latency based on packet size.
286 const int kOverMinLatencyPacketSize
= kPacketFramesInMinLatency
+ 1;
287 int64 expected_micros
= AlsaPcmOutputStream::FramesToTimeDelta(
288 kOverMinLatencyPacketSize
* 2, kTestSampleRate
).InMicroseconds();
290 EXPECT_CALL(mock_alsa_wrapper_
, PcmOpen(_
, _
, _
, _
))
291 .WillOnce(DoAll(SetArgumentPointee
<0>(kFakeHandle
), Return(0)));
292 EXPECT_CALL(mock_alsa_wrapper_
,
293 PcmSetParams(_
, _
, _
, _
, _
, _
, expected_micros
))
294 .WillOnce(Return(0));
295 EXPECT_CALL(mock_alsa_wrapper_
, PcmGetParams(_
, _
, _
))
296 .WillOnce(DoAll(SetArgumentPointee
<1>(kTestFramesPerPacket
),
297 SetArgumentPointee
<2>(kTestFramesPerPacket
/ 2),
300 test_stream
= CreateStream(kTestChannelLayout
,
301 kOverMinLatencyPacketSize
);
302 ASSERT_TRUE(test_stream
->Open());
304 // Now close it and test that everything was released.
305 EXPECT_CALL(mock_alsa_wrapper_
, PcmClose(kFakeHandle
))
306 .WillOnce(Return(0));
307 EXPECT_CALL(mock_alsa_wrapper_
, PcmName(kFakeHandle
))
308 .WillOnce(Return(kTestDeviceName
));
309 test_stream
->Close();
311 Mock::VerifyAndClear(&mock_alsa_wrapper_
);
312 Mock::VerifyAndClear(mock_manager_
.get());
315 TEST_F(AlsaPcmOutputStreamTest
, OpenClose
) {
316 int64 expected_micros
= AlsaPcmOutputStream::FramesToTimeDelta(
317 2 * kTestFramesPerPacket
, kTestSampleRate
).InMicroseconds();
319 // Open() call opens the playback device, sets the parameters, posts a task
320 // with the resulting configuration data, and transitions the object state to
322 EXPECT_CALL(mock_alsa_wrapper_
,
323 PcmOpen(_
, StrEq(kTestDeviceName
),
324 SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
))
325 .WillOnce(DoAll(SetArgumentPointee
<0>(kFakeHandle
),
327 EXPECT_CALL(mock_alsa_wrapper_
,
328 PcmSetParams(kFakeHandle
,
330 SND_PCM_ACCESS_RW_INTERLEAVED
,
331 ChannelLayoutToChannelCount(kTestChannelLayout
),
335 .WillOnce(Return(0));
336 EXPECT_CALL(mock_alsa_wrapper_
, PcmGetParams(kFakeHandle
, _
, _
))
337 .WillOnce(DoAll(SetArgumentPointee
<1>(kTestFramesPerPacket
),
338 SetArgumentPointee
<2>(kTestFramesPerPacket
/ 2),
342 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
343 ASSERT_TRUE(test_stream
->Open());
345 EXPECT_EQ(AlsaPcmOutputStream::kIsOpened
, test_stream
->state());
346 EXPECT_EQ(kFakeHandle
, test_stream
->playback_handle_
);
347 EXPECT_EQ(kTestFramesPerPacket
, test_stream
->frames_per_packet_
);
348 EXPECT_TRUE(test_stream
->buffer_
.get());
349 EXPECT_FALSE(test_stream
->stop_stream_
);
351 // Now close it and test that everything was released.
352 EXPECT_CALL(mock_alsa_wrapper_
, PcmClose(kFakeHandle
))
353 .WillOnce(Return(0));
354 EXPECT_CALL(mock_alsa_wrapper_
, PcmName(kFakeHandle
))
355 .WillOnce(Return(kTestDeviceName
));
356 test_stream
->Close();
359 TEST_F(AlsaPcmOutputStreamTest
, PcmOpenFailed
) {
360 EXPECT_CALL(mock_alsa_wrapper_
, PcmOpen(_
, _
, _
, _
))
361 .WillOnce(Return(kTestFailedErrno
));
362 EXPECT_CALL(mock_alsa_wrapper_
, StrError(kTestFailedErrno
))
363 .WillOnce(Return(kDummyMessage
));
365 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
366 ASSERT_FALSE(test_stream
->Open());
367 ASSERT_EQ(AlsaPcmOutputStream::kInError
, test_stream
->state());
369 // Ensure internal state is set for a no-op stream if PcmOpen() failes.
370 EXPECT_TRUE(test_stream
->stop_stream_
);
371 EXPECT_TRUE(test_stream
->playback_handle_
== NULL
);
372 EXPECT_FALSE(test_stream
->buffer_
.get());
374 // Close the stream since we opened it to make destruction happy.
375 test_stream
->Close();
378 TEST_F(AlsaPcmOutputStreamTest
, PcmSetParamsFailed
) {
379 EXPECT_CALL(mock_alsa_wrapper_
, PcmOpen(_
, _
, _
, _
))
380 .WillOnce(DoAll(SetArgumentPointee
<0>(kFakeHandle
),
382 EXPECT_CALL(mock_alsa_wrapper_
, PcmSetParams(_
, _
, _
, _
, _
, _
, _
))
383 .WillOnce(Return(kTestFailedErrno
));
384 EXPECT_CALL(mock_alsa_wrapper_
, PcmClose(kFakeHandle
))
385 .WillOnce(Return(0));
386 EXPECT_CALL(mock_alsa_wrapper_
, PcmName(kFakeHandle
))
387 .WillOnce(Return(kTestDeviceName
));
388 EXPECT_CALL(mock_alsa_wrapper_
, StrError(kTestFailedErrno
))
389 .WillOnce(Return(kDummyMessage
));
391 // If open fails, the stream stays in kCreated because it has effectively had
393 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
394 ASSERT_FALSE(test_stream
->Open());
395 EXPECT_EQ(AlsaPcmOutputStream::kInError
, test_stream
->state());
397 // Ensure internal state is set for a no-op stream if PcmSetParams() failes.
398 EXPECT_TRUE(test_stream
->stop_stream_
);
399 EXPECT_TRUE(test_stream
->playback_handle_
== NULL
);
400 EXPECT_FALSE(test_stream
->buffer_
.get());
402 // Close the stream since we opened it to make destruction happy.
403 test_stream
->Close();
406 TEST_F(AlsaPcmOutputStreamTest
, StartStop
) {
407 // Open() call opens the playback device, sets the parameters, posts a task
408 // with the resulting configuration data, and transitions the object state to
410 EXPECT_CALL(mock_alsa_wrapper_
, PcmOpen(_
, _
, _
, _
))
411 .WillOnce(DoAll(SetArgumentPointee
<0>(kFakeHandle
),
413 EXPECT_CALL(mock_alsa_wrapper_
, PcmSetParams(_
, _
, _
, _
, _
, _
, _
))
414 .WillOnce(Return(0));
415 EXPECT_CALL(mock_alsa_wrapper_
, PcmGetParams(_
, _
, _
))
416 .WillOnce(DoAll(SetArgumentPointee
<1>(kTestFramesPerPacket
),
417 SetArgumentPointee
<2>(kTestFramesPerPacket
/ 2),
421 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
422 ASSERT_TRUE(test_stream
->Open());
424 // Expect Device setup.
425 EXPECT_CALL(mock_alsa_wrapper_
, PcmDrop(kFakeHandle
))
426 .WillOnce(Return(0));
427 EXPECT_CALL(mock_alsa_wrapper_
, PcmPrepare(kFakeHandle
))
428 .WillOnce(Return(0));
430 // Expect the pre-roll.
431 MockAudioSourceCallback mock_callback
;
432 EXPECT_CALL(mock_alsa_wrapper_
, PcmState(kFakeHandle
))
433 .WillRepeatedly(Return(SND_PCM_STATE_RUNNING
));
434 EXPECT_CALL(mock_alsa_wrapper_
, PcmDelay(kFakeHandle
, _
))
435 .WillRepeatedly(DoAll(SetArgumentPointee
<1>(0), Return(0)));
436 EXPECT_CALL(mock_callback
, OnMoreData(_
, _
))
437 .WillRepeatedly(DoAll(ClearBuffer(), Return(kTestFramesPerPacket
)));
438 EXPECT_CALL(mock_alsa_wrapper_
, PcmWritei(kFakeHandle
, _
, _
))
439 .WillRepeatedly(Return(kTestFramesPerPacket
));
441 // Expect scheduling.
442 EXPECT_CALL(mock_alsa_wrapper_
, PcmAvailUpdate(kFakeHandle
))
444 .WillRepeatedly(Return(kTestFramesPerPacket
));
446 test_stream
->Start(&mock_callback
);
447 // Start() will issue a WriteTask() directly and then schedule the next one,
448 // call Stop() immediately after to ensure we don't run the message loop
451 message_loop_
.RunUntilIdle();
453 EXPECT_CALL(mock_alsa_wrapper_
, PcmClose(kFakeHandle
))
454 .WillOnce(Return(0));
455 EXPECT_CALL(mock_alsa_wrapper_
, PcmName(kFakeHandle
))
456 .WillOnce(Return(kTestDeviceName
));
457 test_stream
->Close();
460 TEST_F(AlsaPcmOutputStreamTest
, WritePacket_FinishedPacket
) {
461 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
462 InitBuffer(test_stream
);
463 test_stream
->TransitionTo(AlsaPcmOutputStream::kIsOpened
);
464 test_stream
->TransitionTo(AlsaPcmOutputStream::kIsPlaying
);
466 // Nothing should happen. Don't set any expectations and Our strict mocks
467 // should verify most of this.
469 // Test empty buffer.
470 test_stream
->buffer_
->Clear();
471 test_stream
->WritePacket();
472 test_stream
->Close();
475 TEST_F(AlsaPcmOutputStreamTest
, WritePacket_NormalPacket
) {
476 // We need to open the stream before writing data to ALSA.
477 EXPECT_CALL(mock_alsa_wrapper_
, PcmOpen(_
, _
, _
, _
))
478 .WillOnce(DoAll(SetArgumentPointee
<0>(kFakeHandle
),
480 EXPECT_CALL(mock_alsa_wrapper_
, PcmSetParams(_
, _
, _
, _
, _
, _
, _
))
481 .WillOnce(Return(0));
482 EXPECT_CALL(mock_alsa_wrapper_
, PcmGetParams(_
, _
, _
))
483 .WillOnce(DoAll(SetArgumentPointee
<1>(kTestFramesPerPacket
),
484 SetArgumentPointee
<2>(kTestFramesPerPacket
/ 2),
486 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
487 ASSERT_TRUE(test_stream
->Open());
488 InitBuffer(test_stream
);
489 test_stream
->TransitionTo(AlsaPcmOutputStream::kIsPlaying
);
491 // Write a little less than half the data.
492 int written
= packet_
->data_size() / kTestBytesPerFrame
/ 2 - 1;
493 EXPECT_CALL(mock_alsa_wrapper_
, PcmAvailUpdate(kFakeHandle
))
494 .WillOnce(Return(written
));
495 EXPECT_CALL(mock_alsa_wrapper_
, PcmWritei(kFakeHandle
, packet_
->data(), _
))
496 .WillOnce(Return(written
));
498 test_stream
->WritePacket();
500 ASSERT_EQ(test_stream
->buffer_
->forward_bytes(),
501 packet_
->data_size() - written
* kTestBytesPerFrame
);
504 EXPECT_CALL(mock_alsa_wrapper_
, PcmAvailUpdate(kFakeHandle
))
505 .WillOnce(Return(kTestFramesPerPacket
- written
));
506 EXPECT_CALL(mock_alsa_wrapper_
,
507 PcmWritei(kFakeHandle
,
508 packet_
->data() + written
* kTestBytesPerFrame
,
510 .WillOnce(Return(packet_
->data_size() / kTestBytesPerFrame
- written
));
511 test_stream
->WritePacket();
512 EXPECT_EQ(0, test_stream
->buffer_
->forward_bytes());
514 // Now close it and test that everything was released.
515 EXPECT_CALL(mock_alsa_wrapper_
, PcmClose(kFakeHandle
))
516 .WillOnce(Return(0));
517 EXPECT_CALL(mock_alsa_wrapper_
, PcmName(kFakeHandle
))
518 .WillOnce(Return(kTestDeviceName
));
519 test_stream
->Close();
522 TEST_F(AlsaPcmOutputStreamTest
, WritePacket_WriteFails
) {
523 // We need to open the stream before writing data to ALSA.
524 EXPECT_CALL(mock_alsa_wrapper_
, PcmOpen(_
, _
, _
, _
))
525 .WillOnce(DoAll(SetArgumentPointee
<0>(kFakeHandle
),
527 EXPECT_CALL(mock_alsa_wrapper_
, PcmSetParams(_
, _
, _
, _
, _
, _
, _
))
528 .WillOnce(Return(0));
529 EXPECT_CALL(mock_alsa_wrapper_
, PcmGetParams(_
, _
, _
))
530 .WillOnce(DoAll(SetArgumentPointee
<1>(kTestFramesPerPacket
),
531 SetArgumentPointee
<2>(kTestFramesPerPacket
/ 2),
533 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
534 ASSERT_TRUE(test_stream
->Open());
535 InitBuffer(test_stream
);
536 test_stream
->TransitionTo(AlsaPcmOutputStream::kIsPlaying
);
538 // Fail due to a recoverable error and see that PcmRecover code path
539 // continues normally.
540 EXPECT_CALL(mock_alsa_wrapper_
, PcmAvailUpdate(kFakeHandle
))
541 .WillOnce(Return(kTestFramesPerPacket
));
542 EXPECT_CALL(mock_alsa_wrapper_
, PcmWritei(kFakeHandle
, _
, _
))
543 .WillOnce(Return(-EINTR
));
544 EXPECT_CALL(mock_alsa_wrapper_
, PcmRecover(kFakeHandle
, _
, _
))
545 .WillOnce(Return(0));
547 test_stream
->WritePacket();
549 ASSERT_EQ(test_stream
->buffer_
->forward_bytes(), packet_
->data_size());
551 // Fail the next write, and see that stop_stream_ is set.
552 EXPECT_CALL(mock_alsa_wrapper_
, PcmAvailUpdate(kFakeHandle
))
553 .WillOnce(Return(kTestFramesPerPacket
));
554 EXPECT_CALL(mock_alsa_wrapper_
, PcmWritei(kFakeHandle
, _
, _
))
555 .WillOnce(Return(kTestFailedErrno
));
556 EXPECT_CALL(mock_alsa_wrapper_
, PcmRecover(kFakeHandle
, _
, _
))
557 .WillOnce(Return(kTestFailedErrno
));
558 EXPECT_CALL(mock_alsa_wrapper_
, StrError(kTestFailedErrno
))
559 .WillOnce(Return(kDummyMessage
));
560 test_stream
->WritePacket();
561 EXPECT_EQ(test_stream
->buffer_
->forward_bytes(), packet_
->data_size());
562 EXPECT_TRUE(test_stream
->stop_stream_
);
564 // Now close it and test that everything was released.
565 EXPECT_CALL(mock_alsa_wrapper_
, PcmClose(kFakeHandle
))
566 .WillOnce(Return(0));
567 EXPECT_CALL(mock_alsa_wrapper_
, PcmName(kFakeHandle
))
568 .WillOnce(Return(kTestDeviceName
));
569 test_stream
->Close();
572 TEST_F(AlsaPcmOutputStreamTest
, WritePacket_StopStream
) {
573 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
574 InitBuffer(test_stream
);
575 test_stream
->TransitionTo(AlsaPcmOutputStream::kIsOpened
);
576 test_stream
->TransitionTo(AlsaPcmOutputStream::kIsPlaying
);
578 // No expectations set on the strict mock because nothing should be called.
579 test_stream
->stop_stream_
= true;
580 test_stream
->WritePacket();
581 EXPECT_EQ(0, test_stream
->buffer_
->forward_bytes());
582 test_stream
->Close();
585 TEST_F(AlsaPcmOutputStreamTest
, BufferPacket
) {
586 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
587 InitBuffer(test_stream
);
588 test_stream
->buffer_
->Clear();
590 MockAudioSourceCallback mock_callback
;
591 EXPECT_CALL(mock_alsa_wrapper_
, PcmState(_
))
592 .WillOnce(Return(SND_PCM_STATE_RUNNING
));
593 EXPECT_CALL(mock_alsa_wrapper_
, PcmDelay(_
, _
))
594 .WillOnce(DoAll(SetArgumentPointee
<1>(1), Return(0)));
595 EXPECT_CALL(mock_alsa_wrapper_
, PcmAvailUpdate(_
))
596 .WillRepeatedly(Return(0)); // Buffer is full.
598 // Return a partially filled packet.
599 EXPECT_CALL(mock_callback
, OnMoreData(_
, _
))
600 .WillOnce(DoAll(ClearBuffer(), Return(kTestFramesPerPacket
/ 2)));
602 bool source_exhausted
;
603 test_stream
->set_source_callback(&mock_callback
);
604 test_stream
->packet_size_
= kTestPacketSize
;
605 test_stream
->BufferPacket(&source_exhausted
);
607 EXPECT_EQ(kTestPacketSize
/ 2, test_stream
->buffer_
->forward_bytes());
608 EXPECT_FALSE(source_exhausted
);
609 test_stream
->Close();
612 TEST_F(AlsaPcmOutputStreamTest
, BufferPacket_Negative
) {
613 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
614 InitBuffer(test_stream
);
615 test_stream
->buffer_
->Clear();
617 // Simulate where the underrun has occurred right after checking the delay.
618 MockAudioSourceCallback mock_callback
;
619 EXPECT_CALL(mock_alsa_wrapper_
, PcmState(_
))
620 .WillOnce(Return(SND_PCM_STATE_RUNNING
));
621 EXPECT_CALL(mock_alsa_wrapper_
, PcmDelay(_
, _
))
622 .WillOnce(DoAll(SetArgumentPointee
<1>(-1), Return(0)));
623 EXPECT_CALL(mock_alsa_wrapper_
, PcmAvailUpdate(_
))
624 .WillRepeatedly(Return(0)); // Buffer is full.
625 EXPECT_CALL(mock_callback
, OnMoreData(_
, _
))
626 .WillOnce(DoAll(ClearBuffer(), Return(kTestFramesPerPacket
/ 2)));
628 bool source_exhausted
;
629 test_stream
->set_source_callback(&mock_callback
);
630 test_stream
->packet_size_
= kTestPacketSize
;
631 test_stream
->BufferPacket(&source_exhausted
);
633 EXPECT_EQ(kTestPacketSize
/ 2, test_stream
->buffer_
->forward_bytes());
634 EXPECT_FALSE(source_exhausted
);
635 test_stream
->Close();
638 TEST_F(AlsaPcmOutputStreamTest
, BufferPacket_Underrun
) {
639 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
640 InitBuffer(test_stream
);
641 test_stream
->buffer_
->Clear();
643 // If ALSA has underrun then we should assume a delay of zero.
644 MockAudioSourceCallback mock_callback
;
645 EXPECT_CALL(mock_alsa_wrapper_
, PcmState(_
))
646 .WillOnce(Return(SND_PCM_STATE_XRUN
));
647 EXPECT_CALL(mock_alsa_wrapper_
, PcmAvailUpdate(_
))
648 .WillRepeatedly(Return(0)); // Buffer is full.
649 EXPECT_CALL(mock_callback
, OnMoreData(_
, 0))
650 .WillOnce(DoAll(ClearBuffer(), Return(kTestFramesPerPacket
/ 2)));
652 bool source_exhausted
;
653 test_stream
->set_source_callback(&mock_callback
);
654 test_stream
->packet_size_
= kTestPacketSize
;
655 test_stream
->BufferPacket(&source_exhausted
);
657 EXPECT_EQ(kTestPacketSize
/ 2, test_stream
->buffer_
->forward_bytes());
658 EXPECT_FALSE(source_exhausted
);
659 test_stream
->Close();
662 TEST_F(AlsaPcmOutputStreamTest
, BufferPacket_FullBuffer
) {
663 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
664 InitBuffer(test_stream
);
665 // No expectations set on the strict mock because nothing should be called.
666 bool source_exhausted
;
667 test_stream
->packet_size_
= kTestPacketSize
;
668 test_stream
->BufferPacket(&source_exhausted
);
669 EXPECT_EQ(kTestPacketSize
, test_stream
->buffer_
->forward_bytes());
670 EXPECT_FALSE(source_exhausted
);
671 test_stream
->Close();
674 TEST_F(AlsaPcmOutputStreamTest
, AutoSelectDevice_DeviceSelect
) {
675 // Try channels from 1 -> 9. and see that we get the more specific surroundXX
676 // device opened for channels 4-8. For all other channels, the device should
677 // default to |AlsaPcmOutputStream::kDefaultDevice|. We should also not
678 // downmix any channel in this case because downmixing is only defined for
679 // channels 4-8, which we are guaranteeing to work.
681 // Note that the loop starts at "1", so the first parameter is ignored in
683 const char* kExpectedDeviceName
[] = { NULL
,
684 AlsaPcmOutputStream::kDefaultDevice
,
685 AlsaPcmOutputStream::kDefaultDevice
,
686 AlsaPcmOutputStream::kDefaultDevice
,
687 kSurround40
, kSurround50
, kSurround51
,
688 kSurround70
, kSurround71
,
689 AlsaPcmOutputStream::kDefaultDevice
};
690 bool kExpectedDownmix
[] = { false, false, false, false, false, true,
691 false, false, false, false };
692 ChannelLayout kExpectedLayouts
[] = { CHANNEL_LAYOUT_NONE
,
694 CHANNEL_LAYOUT_STEREO
,
695 CHANNEL_LAYOUT_SURROUND
,
700 CHANNEL_LAYOUT_7_1
};
703 for (int i
= 1; i
< 9; ++i
) {
704 if (i
== 3 || i
== 4 || i
== 5) // invalid number of channels
706 SCOPED_TRACE(base::StringPrintf("Attempting %d Channel", i
));
708 // Hints will only be grabbed for channel numbers that have non-default
709 // devices associated with them.
710 if (kExpectedDeviceName
[i
] != AlsaPcmOutputStream::kDefaultDevice
) {
711 // The DeviceNameHint and DeviceNameFreeHint need to be paired to avoid a
713 EXPECT_CALL(mock_alsa_wrapper_
, DeviceNameHint(_
, _
, _
))
714 .WillOnce(DoAll(SetArgumentPointee
<2>(&kFakeHints
[0]), Return(0)));
715 EXPECT_CALL(mock_alsa_wrapper_
, DeviceNameFreeHint(&kFakeHints
[0]))
719 EXPECT_CALL(mock_alsa_wrapper_
,
720 PcmOpen(_
, StrEq(kExpectedDeviceName
[i
]), _
, _
))
721 .WillOnce(DoAll(SetArgumentPointee
<0>(kFakeHandle
), Return(0)));
722 EXPECT_CALL(mock_alsa_wrapper_
,
723 PcmSetParams(kFakeHandle
, _
, _
, i
, _
, _
, _
))
724 .WillOnce(Return(0));
726 // The parameters are specified by ALSA documentation, and are in constants
727 // in the implementation files.
728 EXPECT_CALL(mock_alsa_wrapper_
, DeviceNameGetHint(_
, StrEq("IOID")))
729 .WillRepeatedly(Invoke(OutputHint
));
730 EXPECT_CALL(mock_alsa_wrapper_
, DeviceNameGetHint(_
, StrEq("NAME")))
731 .WillRepeatedly(Invoke(EchoHint
));
733 AlsaPcmOutputStream
* test_stream
= CreateStream(kExpectedLayouts
[i
]);
734 EXPECT_TRUE(test_stream
->AutoSelectDevice(i
));
735 EXPECT_EQ(kExpectedDownmix
[i
],
736 static_cast<bool>(test_stream
->channel_mixer_
));
738 Mock::VerifyAndClearExpectations(&mock_alsa_wrapper_
);
739 Mock::VerifyAndClearExpectations(mock_manager_
.get());
740 test_stream
->Close();
744 TEST_F(AlsaPcmOutputStreamTest
, AutoSelectDevice_FallbackDevices
) {
747 // If there are problems opening a multi-channel device, it the fallbacks
748 // operations should be as follows. Assume the multi-channel device name is
751 // 1) Try open "surround50:CARD=foo,DEV=0"
752 // 2) Try open "plug:surround50:CARD=foo,DEV=0".
753 // 3) Try open "plug:surround50".
754 // 4) Try open "default".
755 // 5) Try open "plug:default".
756 // 6) Give up trying to open.
758 const string first_try
= kSurround50
;
759 const string second_try
= string(AlsaPcmOutputStream::kPlugPrefix
) +
761 const string third_try
= string(AlsaPcmOutputStream::kPlugPrefix
) +
763 const string fourth_try
= AlsaPcmOutputStream::kDefaultDevice
;
764 const string fifth_try
= string(AlsaPcmOutputStream::kPlugPrefix
) +
765 AlsaPcmOutputStream::kDefaultDevice
;
767 EXPECT_CALL(mock_alsa_wrapper_
, DeviceNameHint(_
, _
, _
))
768 .WillOnce(DoAll(SetArgumentPointee
<2>(&kFakeHints
[0]), Return(0)));
769 EXPECT_CALL(mock_alsa_wrapper_
, DeviceNameFreeHint(&kFakeHints
[0]))
771 EXPECT_CALL(mock_alsa_wrapper_
, DeviceNameGetHint(_
, StrEq("IOID")))
772 .WillRepeatedly(Invoke(OutputHint
));
773 EXPECT_CALL(mock_alsa_wrapper_
, DeviceNameGetHint(_
, StrEq("NAME")))
774 .WillRepeatedly(Invoke(EchoHint
));
775 EXPECT_CALL(mock_alsa_wrapper_
, StrError(kTestFailedErrno
))
776 .WillRepeatedly(Return(kDummyMessage
));
779 EXPECT_CALL(mock_alsa_wrapper_
, PcmOpen(_
, StrEq(first_try
.c_str()), _
, _
))
780 .WillOnce(Return(kTestFailedErrno
));
781 EXPECT_CALL(mock_alsa_wrapper_
, PcmOpen(_
, StrEq(second_try
.c_str()), _
, _
))
782 .WillOnce(Return(kTestFailedErrno
));
783 EXPECT_CALL(mock_alsa_wrapper_
, PcmOpen(_
, StrEq(third_try
.c_str()), _
, _
))
784 .WillOnce(Return(kTestFailedErrno
));
785 EXPECT_CALL(mock_alsa_wrapper_
, PcmOpen(_
, StrEq(fourth_try
.c_str()), _
, _
))
786 .WillOnce(Return(kTestFailedErrno
));
787 EXPECT_CALL(mock_alsa_wrapper_
, PcmOpen(_
, StrEq(fifth_try
.c_str()), _
, _
))
788 .WillOnce(Return(kTestFailedErrno
));
790 AlsaPcmOutputStream
* test_stream
= CreateStream(CHANNEL_LAYOUT_5_0
);
791 EXPECT_FALSE(test_stream
->AutoSelectDevice(5));
792 test_stream
->Close();
795 TEST_F(AlsaPcmOutputStreamTest
, AutoSelectDevice_HintFail
) {
796 // Should get |kDefaultDevice|, and force a 2-channel downmix on a failure to
797 // enumerate devices.
798 EXPECT_CALL(mock_alsa_wrapper_
, DeviceNameHint(_
, _
, _
))
799 .WillRepeatedly(Return(kTestFailedErrno
));
800 EXPECT_CALL(mock_alsa_wrapper_
,
801 PcmOpen(_
, StrEq(AlsaPcmOutputStream::kDefaultDevice
), _
, _
))
802 .WillOnce(DoAll(SetArgumentPointee
<0>(kFakeHandle
), Return(0)));
803 EXPECT_CALL(mock_alsa_wrapper_
,
804 PcmSetParams(kFakeHandle
, _
, _
, 2, _
, _
, _
))
805 .WillOnce(Return(0));
806 EXPECT_CALL(mock_alsa_wrapper_
, StrError(kTestFailedErrno
))
807 .WillOnce(Return(kDummyMessage
));
809 AlsaPcmOutputStream
* test_stream
= CreateStream(CHANNEL_LAYOUT_5_0
);
810 EXPECT_TRUE(test_stream
->AutoSelectDevice(5));
811 EXPECT_TRUE(test_stream
->channel_mixer_
);
812 test_stream
->Close();
815 TEST_F(AlsaPcmOutputStreamTest
, BufferPacket_StopStream
) {
816 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
817 InitBuffer(test_stream
);
818 test_stream
->stop_stream_
= true;
819 bool source_exhausted
;
820 test_stream
->BufferPacket(&source_exhausted
);
821 EXPECT_EQ(0, test_stream
->buffer_
->forward_bytes());
822 EXPECT_TRUE(source_exhausted
);
823 test_stream
->Close();
826 TEST_F(AlsaPcmOutputStreamTest
, ScheduleNextWrite
) {
827 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
828 test_stream
->TransitionTo(AlsaPcmOutputStream::kIsOpened
);
829 test_stream
->TransitionTo(AlsaPcmOutputStream::kIsPlaying
);
830 InitBuffer(test_stream
);
831 DVLOG(1) << test_stream
->state();
832 EXPECT_CALL(mock_alsa_wrapper_
, PcmAvailUpdate(_
))
833 .WillOnce(Return(10));
834 test_stream
->ScheduleNextWrite(false);
835 DVLOG(1) << test_stream
->state();
836 // TODO(sergeyu): Figure out how to check that the task has been added to the
839 // Cleanup the message queue. Currently ~MessageQueue() doesn't free pending
840 // tasks unless running on valgrind. The code below is needed to keep
843 test_stream
->stop_stream_
= true;
844 DVLOG(1) << test_stream
->state();
845 test_stream
->TransitionTo(AlsaPcmOutputStream::kIsClosed
);
846 DVLOG(1) << test_stream
->state();
847 test_stream
->Close();
850 TEST_F(AlsaPcmOutputStreamTest
, ScheduleNextWrite_StopStream
) {
851 AlsaPcmOutputStream
* test_stream
= CreateStream(kTestChannelLayout
);
852 test_stream
->TransitionTo(AlsaPcmOutputStream::kIsOpened
);
853 test_stream
->TransitionTo(AlsaPcmOutputStream::kIsPlaying
);
855 InitBuffer(test_stream
);
857 test_stream
->stop_stream_
= true;
858 test_stream
->ScheduleNextWrite(true);
860 // TODO(ajwong): Find a way to test whether or not another task has been
861 // posted so we can verify that the Alsa code will indeed break the task
864 test_stream
->TransitionTo(AlsaPcmOutputStream::kIsClosed
);
865 test_stream
->Close();