1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
media / audio / alsa / alsa_output_unittest.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
#include <memory>
#include "base/logging.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/test/test_message_loop.h"
#include "base/time/time.h"
#include "media/audio/alsa/alsa_output.h"
#include "media/audio/alsa/alsa_wrapper.h"
#include "media/audio/alsa/audio_manager_alsa.h"
#include "media/audio/alsa/mock_alsa_wrapper.h"
#include "media/audio/fake_audio_log_factory.h"
#include "media/audio/mock_audio_source_callback.h"
#include "media/audio/test_audio_thread.h"
#include "media/base/audio_timestamp_helper.h"
#include "media/base/channel_layout.h"
#include "media/base/data_buffer.h"
#include "media/base/seekable_buffer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::AllOf;
using testing::AtLeast;
using testing::DoAll;
using testing::Field;
using testing::InSequence;
using testing::Invoke;
using testing::InvokeWithoutArgs;
using testing::Mock;
using testing::MockFunction;
using testing::Return;
using testing::SetArgPointee;
using testing::StrictMock;
using testing::StrEq;
using testing::Unused;
namespace media {
namespace {
constexpr ChannelLayout kTestChannelLayout = CHANNEL_LAYOUT_STEREO;
constexpr int kTestSampleRate = AudioParameters::kAudioCDSampleRate;
constexpr size_t kTestBitsPerSample = 16;
constexpr AudioParameters::Format kTestFormat =
AudioParameters::AUDIO_PCM_LINEAR;
constexpr char kTestDeviceName[] = "TestDevice";
constexpr char kDummyMessage[] = "dummy";
constexpr uint32_t kTestFramesPerPacket = 1000;
constexpr int kTestFailedErrno = -EACCES;
const size_t kTestBytesPerFrame =
kTestBitsPerSample / 8 * ChannelLayoutToChannelCount(kTestChannelLayout);
const size_t kTestPacketSize = kTestFramesPerPacket * kTestBytesPerFrame;
// The APIs that use handles are not const-correct and require this annoying
// conversion.
snd_pcm_t* GetFakeHandle() {
return const_cast<snd_pcm_t*>(reinterpret_cast<snd_pcm_t*>(1));
}
} // namespace
class MockAudioManagerAlsa : public AudioManagerAlsa {
public:
MockAudioManagerAlsa()
: AudioManagerAlsa(std::make_unique<TestAudioThread>(),
&fake_audio_log_factory_) {}
MOCK_METHOD0(Init, void());
MOCK_METHOD0(HasAudioOutputDevices, bool());
MOCK_METHOD0(HasAudioInputDevices, bool());
MOCK_METHOD2(MakeLinearOutputStream,
AudioOutputStream*(const AudioParameters& params,
const LogCallback& log_callback));
MOCK_METHOD3(MakeLowLatencyOutputStream,
AudioOutputStream*(const AudioParameters& params,
const std::string& device_id,
const LogCallback& log_callback));
MOCK_METHOD3(MakeLowLatencyInputStream,
AudioInputStream*(const AudioParameters& params,
const std::string& device_id,
const LogCallback& log_callback));
// We need to override this function in order to skip the checking the number
// of active output streams. It is because the number of active streams
// is managed inside MakeAudioOutputStream, and we don't use
// MakeAudioOutputStream to create the stream in the tests.
void ReleaseOutputStream(AudioOutputStream* stream) override {
DCHECK(stream);
delete stream;
}
private:
FakeAudioLogFactory fake_audio_log_factory_;
};
class AlsaPcmOutputStreamTest : public testing::Test {
public:
AlsaPcmOutputStreamTest(const AlsaPcmOutputStreamTest&) = delete;
AlsaPcmOutputStreamTest& operator=(const AlsaPcmOutputStreamTest&) = delete;
protected:
AlsaPcmOutputStreamTest() {
mock_manager_ = std::make_unique<StrictMock<MockAudioManagerAlsa>>();
}
~AlsaPcmOutputStreamTest() override { mock_manager_->Shutdown(); }
AlsaPcmOutputStream* CreateStream(ChannelLayout layout) {
return CreateStream(layout, kTestFramesPerPacket);
}
AlsaPcmOutputStream* CreateStream(ChannelLayout layout,
int32_t samples_per_packet) {
AudioParameters params(
kTestFormat,
ChannelLayoutConfig(layout, ChannelLayoutToChannelCount(layout)),
kTestSampleRate, samples_per_packet);
return new AlsaPcmOutputStream(kTestDeviceName,
params,
&mock_alsa_wrapper_,
mock_manager_.get());
}
// Helper function to malloc the string returned by DeviceNameHint for NAME.
static char* EchoHint(const void* name, Unused) {
return strdup(static_cast<const char*>(name));
}
// Helper function to malloc the string returned by DeviceNameHint for IOID.
static char* OutputHint(Unused, Unused) {
return strdup("Output");
}
// Helper function to initialize |test_stream->buffer_|. Must be called
// in all tests that use buffer_ without opening the stream.
void InitBuffer(AlsaPcmOutputStream* test_stream) {
DCHECK(test_stream);
packet_ = base::MakeRefCounted<DataBuffer>(kTestPacketSize);
packet_->set_size(kTestPacketSize);
test_stream->buffer_ = std::make_unique<SeekableBuffer>(0, kTestPacketSize);
test_stream->buffer_->Append(packet_.get());
}
base::TestMessageLoop message_loop_;
StrictMock<MockAlsaWrapper> mock_alsa_wrapper_;
std::unique_ptr<StrictMock<MockAudioManagerAlsa>> mock_manager_;
scoped_refptr<DataBuffer> packet_;
};
// Custom action to clear a memory buffer.
ACTION(ClearBuffer) {
arg3->Zero();
}
TEST_F(AlsaPcmOutputStreamTest, ConstructedState) {
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
EXPECT_EQ(AlsaPcmOutputStream::kCreated, test_stream->state());
test_stream->Close();
// Should support mono.
test_stream = CreateStream(CHANNEL_LAYOUT_MONO);
EXPECT_EQ(AlsaPcmOutputStream::kCreated, test_stream->state());
test_stream->Close();
// Should support multi-channel.
test_stream = CreateStream(CHANNEL_LAYOUT_SURROUND);
EXPECT_EQ(AlsaPcmOutputStream::kCreated, test_stream->state());
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, LatencyFloor) {
constexpr const double kMicrosPerFrame =
static_cast<double>(1000000) / kTestSampleRate;
const double kPacketFramesInMinLatency =
AlsaPcmOutputStream::kMinLatencyMicros / kMicrosPerFrame / 2.0;
// Test that packets which would cause a latency under less than
// AlsaPcmOutputStream::kMinLatencyMicros will get clipped to
// AlsaPcmOutputStream::kMinLatencyMicros,
EXPECT_CALL(mock_alsa_wrapper_, PcmOpen(_, _, _, _))
.WillOnce(DoAll(SetArgPointee<0>(GetFakeHandle()), Return(0)));
EXPECT_CALL(mock_alsa_wrapper_,
PcmSetParams(_, _, _, _, _, _,
AlsaPcmOutputStream::kMinLatencyMicros))
.WillOnce(Return(0));
EXPECT_CALL(mock_alsa_wrapper_, PcmGetParams(_, _, _))
.WillOnce(DoAll(SetArgPointee<1>(kTestFramesPerPacket),
SetArgPointee<2>(kTestFramesPerPacket / 2), Return(0)));
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout,
kPacketFramesInMinLatency);
ASSERT_TRUE(test_stream->Open());
// Now close it and test that everything was released.
EXPECT_CALL(mock_alsa_wrapper_, PcmClose(GetFakeHandle()))
.WillOnce(Return(0));
EXPECT_CALL(mock_alsa_wrapper_, PcmName(GetFakeHandle()))
.WillOnce(Return(kTestDeviceName));
test_stream->Close();
Mock::VerifyAndClear(&mock_alsa_wrapper_);
Mock::VerifyAndClear(mock_manager_.get());
// Test that having more packets ends up with a latency based on packet size.
const int kOverMinLatencyPacketSize = kPacketFramesInMinLatency + 1;
int64_t expected_micros = AudioTimestampHelper::FramesToTime(
kOverMinLatencyPacketSize * 2, kTestSampleRate)
.InMicroseconds();
EXPECT_CALL(mock_alsa_wrapper_, PcmOpen(_, _, _, _))
.WillOnce(DoAll(SetArgPointee<0>(GetFakeHandle()), Return(0)));
EXPECT_CALL(mock_alsa_wrapper_,
PcmSetParams(_, _, _, _, _, _, expected_micros))
.WillOnce(Return(0));
EXPECT_CALL(mock_alsa_wrapper_, PcmGetParams(_, _, _))
.WillOnce(DoAll(SetArgPointee<1>(kTestFramesPerPacket),
SetArgPointee<2>(kTestFramesPerPacket / 2), Return(0)));
test_stream = CreateStream(kTestChannelLayout,
kOverMinLatencyPacketSize);
ASSERT_TRUE(test_stream->Open());
// Now close it and test that everything was released.
EXPECT_CALL(mock_alsa_wrapper_, PcmClose(GetFakeHandle()))
.WillOnce(Return(0));
EXPECT_CALL(mock_alsa_wrapper_, PcmName(GetFakeHandle()))
.WillOnce(Return(kTestDeviceName));
test_stream->Close();
Mock::VerifyAndClear(&mock_alsa_wrapper_);
Mock::VerifyAndClear(mock_manager_.get());
}
TEST_F(AlsaPcmOutputStreamTest, OpenClose) {
int64_t expected_micros = AudioTimestampHelper::FramesToTime(
2 * kTestFramesPerPacket, kTestSampleRate)
.InMicroseconds();
// Open() call opens the playback device, sets the parameters, posts a task
// with the resulting configuration data, and transitions the object state to
// kIsOpened.
EXPECT_CALL(mock_alsa_wrapper_,
PcmOpen(_, StrEq(kTestDeviceName), SND_PCM_STREAM_PLAYBACK,
SND_PCM_NONBLOCK))
.WillOnce(DoAll(SetArgPointee<0>(GetFakeHandle()), Return(0)));
EXPECT_CALL(mock_alsa_wrapper_,
PcmSetParams(GetFakeHandle(), SND_PCM_FORMAT_S16_LE,
SND_PCM_ACCESS_RW_INTERLEAVED,
ChannelLayoutToChannelCount(kTestChannelLayout),
kTestSampleRate, 1, expected_micros))
.WillOnce(Return(0));
EXPECT_CALL(mock_alsa_wrapper_, PcmGetParams(GetFakeHandle(), _, _))
.WillOnce(DoAll(SetArgPointee<1>(kTestFramesPerPacket),
SetArgPointee<2>(kTestFramesPerPacket / 2), Return(0)));
// Open the stream.
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
ASSERT_TRUE(test_stream->Open());
EXPECT_EQ(AlsaPcmOutputStream::kIsOpened, test_stream->state());
EXPECT_EQ(GetFakeHandle(), test_stream->playback_handle_);
EXPECT_EQ(kTestFramesPerPacket, test_stream->frames_per_packet_);
EXPECT_TRUE(test_stream->buffer_.get());
EXPECT_FALSE(test_stream->stop_stream_);
// Now close it and test that everything was released.
EXPECT_CALL(mock_alsa_wrapper_, PcmClose(GetFakeHandle()))
.WillOnce(Return(0));
EXPECT_CALL(mock_alsa_wrapper_, PcmName(GetFakeHandle()))
.WillOnce(Return(kTestDeviceName));
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, PcmOpenFailed) {
EXPECT_CALL(mock_alsa_wrapper_, PcmOpen(_, _, _, _))
.WillOnce(Return(kTestFailedErrno));
EXPECT_CALL(mock_alsa_wrapper_, StrError(kTestFailedErrno))
.WillOnce(Return(kDummyMessage));
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
ASSERT_FALSE(test_stream->Open());
ASSERT_EQ(AlsaPcmOutputStream::kInError, test_stream->state());
// Ensure internal state is set for a no-op stream if PcmOpen() fails.
EXPECT_TRUE(test_stream->stop_stream_);
EXPECT_FALSE(test_stream->playback_handle_);
EXPECT_FALSE(test_stream->buffer_.get());
// Close the stream since we opened it to make destruction happy.
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, PcmSetParamsFailed) {
EXPECT_CALL(mock_alsa_wrapper_, PcmOpen(_, _, _, _))
.WillOnce(DoAll(SetArgPointee<0>(GetFakeHandle()), Return(0)));
EXPECT_CALL(mock_alsa_wrapper_, PcmSetParams(_, _, _, _, _, _, _))
.WillOnce(Return(kTestFailedErrno));
EXPECT_CALL(mock_alsa_wrapper_, PcmClose(GetFakeHandle()))
.WillOnce(Return(0));
EXPECT_CALL(mock_alsa_wrapper_, PcmName(GetFakeHandle()))
.WillOnce(Return(kTestDeviceName));
EXPECT_CALL(mock_alsa_wrapper_, StrError(kTestFailedErrno))
.WillOnce(Return(kDummyMessage));
// If open fails, the stream stays in kCreated because it has effectively had
// no changes.
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
ASSERT_FALSE(test_stream->Open());
EXPECT_EQ(AlsaPcmOutputStream::kInError, test_stream->state());
// Ensure internal state is set for a no-op stream if PcmSetParams() fails.
EXPECT_TRUE(test_stream->stop_stream_);
EXPECT_FALSE(test_stream->playback_handle_);
EXPECT_FALSE(test_stream->buffer_.get());
// Close the stream since we opened it to make destruction happy.
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, StartStop) {
// Open() call opens the playback device, sets the parameters, posts a task
// with the resulting configuration data, and transitions the object state to
// kIsOpened.
EXPECT_CALL(mock_alsa_wrapper_, PcmOpen(_, _, _, _))
.WillOnce(DoAll(SetArgPointee<0>(GetFakeHandle()), Return(0)));
EXPECT_CALL(mock_alsa_wrapper_, PcmSetParams(_, _, _, _, _, _, _))
.WillOnce(Return(0));
EXPECT_CALL(mock_alsa_wrapper_, PcmGetParams(_, _, _))
.WillOnce(DoAll(SetArgPointee<1>(kTestFramesPerPacket),
SetArgPointee<2>(kTestFramesPerPacket / 2), Return(0)));
// Open the stream.
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
ASSERT_TRUE(test_stream->Open());
base::SimpleTestTickClock tick_clock;
tick_clock.SetNowTicks(base::TimeTicks::Now());
test_stream->SetTickClockForTesting(&tick_clock);
// Expect Device setup.
EXPECT_CALL(mock_alsa_wrapper_, PcmDrop(GetFakeHandle())).WillOnce(Return(0));
EXPECT_CALL(mock_alsa_wrapper_, PcmPrepare(GetFakeHandle()))
.WillOnce(Return(0));
// Expect the pre-roll.
MockAudioSourceCallback mock_callback;
EXPECT_CALL(mock_alsa_wrapper_, PcmState(GetFakeHandle()))
.WillRepeatedly(Return(SND_PCM_STATE_RUNNING));
EXPECT_CALL(mock_alsa_wrapper_, PcmDelay(GetFakeHandle(), _))
.WillRepeatedly(DoAll(SetArgPointee<1>(0), Return(0)));
EXPECT_CALL(mock_callback,
OnMoreData(base::TimeDelta(), tick_clock.NowTicks(),
AudioGlitchInfo(), _))
.WillRepeatedly(DoAll(ClearBuffer(), Return(kTestFramesPerPacket)));
EXPECT_CALL(mock_alsa_wrapper_, PcmWritei(GetFakeHandle(), _, _))
.WillRepeatedly(Return(kTestFramesPerPacket));
// Expect scheduling.
EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(GetFakeHandle()))
.Times(AtLeast(2))
.WillRepeatedly(Return(kTestFramesPerPacket));
test_stream->Start(&mock_callback);
// Start() will issue a WriteTask() directly and then schedule the next one,
// call Stop() immediately after to ensure we don't run the message loop
// forever.
test_stream->Stop();
base::RunLoop().RunUntilIdle();
EXPECT_CALL(mock_alsa_wrapper_, PcmClose(GetFakeHandle()))
.WillOnce(Return(0));
EXPECT_CALL(mock_alsa_wrapper_, PcmName(GetFakeHandle()))
.WillOnce(Return(kTestDeviceName));
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, WritePacket_FinishedPacket) {
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
InitBuffer(test_stream);
test_stream->TransitionTo(AlsaPcmOutputStream::kIsOpened);
test_stream->TransitionTo(AlsaPcmOutputStream::kIsPlaying);
// Nothing should happen. Don't set any expectations and Our strict mocks
// should verify most of this.
// Test empty buffer.
test_stream->buffer_->Clear();
test_stream->WritePacket();
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, WritePacket_NormalPacket) {
// We need to open the stream before writing data to ALSA.
EXPECT_CALL(mock_alsa_wrapper_, PcmOpen(_, _, _, _))
.WillOnce(DoAll(SetArgPointee<0>(GetFakeHandle()), Return(0)));
EXPECT_CALL(mock_alsa_wrapper_, PcmSetParams(_, _, _, _, _, _, _))
.WillOnce(Return(0));
EXPECT_CALL(mock_alsa_wrapper_, PcmGetParams(_, _, _))
.WillOnce(DoAll(SetArgPointee<1>(kTestFramesPerPacket),
SetArgPointee<2>(kTestFramesPerPacket / 2), Return(0)));
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
ASSERT_TRUE(test_stream->Open());
InitBuffer(test_stream);
test_stream->TransitionTo(AlsaPcmOutputStream::kIsPlaying);
// Write a little less than half the data.
size_t written = packet_->size() / kTestBytesPerFrame / 2 - 1;
EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(GetFakeHandle()))
.WillOnce(Return(static_cast<snd_pcm_sframes_t>(written)));
EXPECT_CALL(mock_alsa_wrapper_,
PcmWritei(GetFakeHandle(), packet_->data().data(), _))
.WillOnce(Return(static_cast<snd_pcm_sframes_t>(written)));
test_stream->WritePacket();
ASSERT_EQ(static_cast<size_t>(test_stream->buffer_->forward_bytes()),
packet_->size() - written * kTestBytesPerFrame);
// Write the rest.
EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(GetFakeHandle()))
.WillOnce(Return(
static_cast<snd_pcm_sframes_t>(kTestFramesPerPacket - written)));
EXPECT_CALL(
mock_alsa_wrapper_,
PcmWritei(GetFakeHandle(),
packet_->data().subspan(written * kTestBytesPerFrame).data(),
_))
.WillOnce(Return(static_cast<snd_pcm_sframes_t>(
packet_->size() / kTestBytesPerFrame - written)));
test_stream->WritePacket();
EXPECT_EQ(0u, test_stream->buffer_->forward_bytes());
// Now close it and test that everything was released.
EXPECT_CALL(mock_alsa_wrapper_, PcmClose(GetFakeHandle()))
.WillOnce(Return(0));
EXPECT_CALL(mock_alsa_wrapper_, PcmName(GetFakeHandle()))
.WillOnce(Return(kTestDeviceName));
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, WritePacket_WriteFails) {
// We need to open the stream before writing data to ALSA.
EXPECT_CALL(mock_alsa_wrapper_, PcmOpen(_, _, _, _))
.WillOnce(DoAll(SetArgPointee<0>(GetFakeHandle()), Return(0)));
EXPECT_CALL(mock_alsa_wrapper_, PcmSetParams(_, _, _, _, _, _, _))
.WillOnce(Return(0));
EXPECT_CALL(mock_alsa_wrapper_, PcmGetParams(_, _, _))
.WillOnce(DoAll(SetArgPointee<1>(kTestFramesPerPacket),
SetArgPointee<2>(kTestFramesPerPacket / 2), Return(0)));
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
ASSERT_TRUE(test_stream->Open());
InitBuffer(test_stream);
test_stream->TransitionTo(AlsaPcmOutputStream::kIsPlaying);
// Fail due to a recoverable error and see that PcmRecover code path
// continues normally.
EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(GetFakeHandle()))
.WillOnce(Return(kTestFramesPerPacket));
EXPECT_CALL(mock_alsa_wrapper_, PcmWritei(GetFakeHandle(), _, _))
.WillOnce(Return(-EINTR));
EXPECT_CALL(mock_alsa_wrapper_, PcmRecover(GetFakeHandle(), _, _))
.WillOnce(Return(0));
test_stream->WritePacket();
ASSERT_EQ(test_stream->buffer_->forward_bytes(), packet_->size());
// Fail the next write, and see that stop_stream_ is set.
EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(GetFakeHandle()))
.WillOnce(Return(kTestFramesPerPacket));
EXPECT_CALL(mock_alsa_wrapper_, PcmWritei(GetFakeHandle(), _, _))
.WillOnce(Return(kTestFailedErrno));
EXPECT_CALL(mock_alsa_wrapper_, PcmRecover(GetFakeHandle(), _, _))
.WillOnce(Return(kTestFailedErrno));
EXPECT_CALL(mock_alsa_wrapper_, StrError(kTestFailedErrno))
.WillOnce(Return(kDummyMessage));
test_stream->WritePacket();
EXPECT_EQ(test_stream->buffer_->forward_bytes(), packet_->size());
EXPECT_TRUE(test_stream->stop_stream_);
// Now close it and test that everything was released.
EXPECT_CALL(mock_alsa_wrapper_, PcmClose(GetFakeHandle()))
.WillOnce(Return(0));
EXPECT_CALL(mock_alsa_wrapper_, PcmName(GetFakeHandle()))
.WillOnce(Return(kTestDeviceName));
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, WritePacket_StopStream) {
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
InitBuffer(test_stream);
test_stream->TransitionTo(AlsaPcmOutputStream::kIsOpened);
test_stream->TransitionTo(AlsaPcmOutputStream::kIsPlaying);
// No expectations set on the strict mock because nothing should be called.
test_stream->stop_stream_ = true;
test_stream->WritePacket();
EXPECT_EQ(0u, test_stream->buffer_->forward_bytes());
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, BufferPacket) {
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
base::SimpleTestTickClock tick_clock;
tick_clock.SetNowTicks(base::TimeTicks::Now());
test_stream->SetTickClockForTesting(&tick_clock);
InitBuffer(test_stream);
test_stream->buffer_->Clear();
MockAudioSourceCallback mock_callback;
EXPECT_CALL(mock_alsa_wrapper_, PcmState(_))
.WillOnce(Return(SND_PCM_STATE_RUNNING));
EXPECT_CALL(mock_alsa_wrapper_, PcmDelay(_, _))
.WillOnce(DoAll(SetArgPointee<1>(1), Return(0)));
EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(_))
.WillRepeatedly(Return(0)); // Buffer is full.
// Return a partially filled packet.
EXPECT_CALL(mock_callback,
OnMoreData(base::TimeDelta(), tick_clock.NowTicks(),
AudioGlitchInfo(), _))
.WillOnce(DoAll(ClearBuffer(), Return(kTestFramesPerPacket / 2)));
bool source_exhausted;
test_stream->set_source_callback(&mock_callback);
test_stream->packet_size_ = kTestPacketSize;
test_stream->BufferPacket(&source_exhausted);
EXPECT_EQ(kTestPacketSize / 2, test_stream->buffer_->forward_bytes());
EXPECT_FALSE(source_exhausted);
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, BufferPacket_Negative) {
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
base::SimpleTestTickClock tick_clock;
tick_clock.SetNowTicks(base::TimeTicks::Now());
test_stream->SetTickClockForTesting(&tick_clock);
InitBuffer(test_stream);
test_stream->buffer_->Clear();
// Simulate where the underrun has occurred right after checking the delay.
MockAudioSourceCallback mock_callback;
EXPECT_CALL(mock_alsa_wrapper_, PcmState(_))
.WillOnce(Return(SND_PCM_STATE_RUNNING));
EXPECT_CALL(mock_alsa_wrapper_, PcmDelay(_, _))
.WillOnce(DoAll(SetArgPointee<1>(-1), Return(0)));
EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(_))
.WillRepeatedly(Return(0)); // Buffer is full.
EXPECT_CALL(mock_callback,
OnMoreData(base::TimeDelta(), tick_clock.NowTicks(),
AudioGlitchInfo(), _))
.WillOnce(DoAll(ClearBuffer(), Return(kTestFramesPerPacket / 2)));
bool source_exhausted;
test_stream->set_source_callback(&mock_callback);
test_stream->packet_size_ = kTestPacketSize;
test_stream->BufferPacket(&source_exhausted);
EXPECT_EQ(kTestPacketSize / 2, test_stream->buffer_->forward_bytes());
EXPECT_FALSE(source_exhausted);
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, BufferPacket_Underrun) {
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
base::SimpleTestTickClock tick_clock;
tick_clock.SetNowTicks(base::TimeTicks::Now());
test_stream->SetTickClockForTesting(&tick_clock);
InitBuffer(test_stream);
test_stream->buffer_->Clear();
// If ALSA has underrun then we should assume a delay of zero.
MockAudioSourceCallback mock_callback;
EXPECT_CALL(mock_alsa_wrapper_, PcmState(_))
.WillOnce(Return(SND_PCM_STATE_XRUN));
EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(_))
.WillRepeatedly(Return(0)); // Buffer is full.
EXPECT_CALL(mock_callback,
OnMoreData(base::TimeDelta(), tick_clock.NowTicks(),
AudioGlitchInfo(), _))
.WillOnce(DoAll(ClearBuffer(), Return(kTestFramesPerPacket / 2)));
bool source_exhausted;
test_stream->set_source_callback(&mock_callback);
test_stream->packet_size_ = kTestPacketSize;
test_stream->BufferPacket(&source_exhausted);
EXPECT_EQ(kTestPacketSize / 2, test_stream->buffer_->forward_bytes());
EXPECT_FALSE(source_exhausted);
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, BufferPacket_FullBuffer) {
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
InitBuffer(test_stream);
// No expectations set on the strict mock because nothing should be called.
bool source_exhausted;
test_stream->packet_size_ = kTestPacketSize;
test_stream->BufferPacket(&source_exhausted);
EXPECT_EQ(kTestPacketSize, test_stream->buffer_->forward_bytes());
EXPECT_FALSE(source_exhausted);
test_stream->Close();
}
constexpr const char kSurround40[] = "surround40:CARD=foo,DEV=0";
constexpr const char kSurround41[] = "surround41:CARD=foo,DEV=0";
constexpr const char kSurround50[] = "surround50:CARD=foo,DEV=0";
constexpr const char kSurround51[] = "surround51:CARD=foo,DEV=0";
constexpr const char kSurround70[] = "surround70:CARD=foo,DEV=0";
constexpr const char kSurround71[] = "surround71:CARD=foo,DEV=0";
constexpr const char kGenericSurround50[] = "surround50";
void** GetFakeHints() {
static const char* kFakeHints[] = {kSurround40, kSurround41, kSurround50,
kSurround51, kSurround70, kSurround71,
nullptr};
return const_cast<void**>(
reinterpret_cast<const void** const>(std::data(kFakeHints)));
}
TEST_F(AlsaPcmOutputStreamTest, AutoSelectDevice_DeviceSelect) {
// Try channels from 1 -> 9. and see that we get the more specific surroundXX
// device opened for channels 4-8. For all other channels, the device should
// default to |AlsaPcmOutputStream::kDefaultDevice|. We should also not
// downmix any channel in this case because downmixing is only defined for
// channels 4-8, which we are guaranteeing to work.
//
// Note that the loop starts at "1", so the first parameter is ignored in
// these arrays.
constexpr std::array<const char*, 9> kExpectedDeviceName{
{nullptr, AlsaPcmOutputStream::kDefaultDevice,
AlsaPcmOutputStream::kDefaultDevice, AlsaPcmOutputStream::kDefaultDevice,
kSurround40, kSurround50, kSurround51, kSurround70, kSurround71}};
constexpr std::array<ChannelLayout, 9> kExpectedLayouts{
{CHANNEL_LAYOUT_NONE, CHANNEL_LAYOUT_MONO, CHANNEL_LAYOUT_STEREO,
CHANNEL_LAYOUT_SURROUND, CHANNEL_LAYOUT_4_0, CHANNEL_LAYOUT_5_0,
CHANNEL_LAYOUT_5_1, CHANNEL_LAYOUT_7_0, CHANNEL_LAYOUT_7_1}};
for (int i = 1; i < 9; ++i) {
if (i == 3 || i == 4 || i == 5) // invalid number of channels
continue;
SCOPED_TRACE(base::StringPrintf("Attempting %d Channel", i));
// Hints will only be grabbed for channel numbers that have non-default
// devices associated with them.
if (kExpectedDeviceName[i] != AlsaPcmOutputStream::kDefaultDevice) {
// The DeviceNameHint and DeviceNameFreeHint need to be paired to avoid a
// memory leak.
EXPECT_CALL(mock_alsa_wrapper_, DeviceNameHint(_, _, _))
.WillOnce(DoAll(SetArgPointee<2>(GetFakeHints()), Return(0)));
EXPECT_CALL(mock_alsa_wrapper_, DeviceNameFreeHint(GetFakeHints()))
.Times(1);
}
EXPECT_CALL(mock_alsa_wrapper_,
PcmOpen(_, StrEq(kExpectedDeviceName[i]), _, _))
.WillOnce(DoAll(SetArgPointee<0>(GetFakeHandle()), Return(0)));
EXPECT_CALL(mock_alsa_wrapper_,
PcmSetParams(GetFakeHandle(), _, _, i, _, _, _))
.WillOnce(Return(0));
// The parameters are specified by ALSA documentation, and are in constants
// in the implementation files.
EXPECT_CALL(mock_alsa_wrapper_, DeviceNameGetHint(_, StrEq("IOID")))
.WillRepeatedly(Invoke(OutputHint));
EXPECT_CALL(mock_alsa_wrapper_, DeviceNameGetHint(_, StrEq("NAME")))
.WillRepeatedly(Invoke(EchoHint));
AlsaPcmOutputStream* test_stream = CreateStream(kExpectedLayouts[i]);
EXPECT_TRUE(test_stream->AutoSelectDevice(i));
// Expected downmix is only true for the fifth iteration.
EXPECT_EQ(i == 5, static_cast<bool>(test_stream->channel_mixer_));
Mock::VerifyAndClearExpectations(&mock_alsa_wrapper_);
Mock::VerifyAndClearExpectations(mock_manager_.get());
test_stream->Close();
}
}
TEST_F(AlsaPcmOutputStreamTest, AutoSelectDevice_FallbackDevices) {
// If there are problems opening a multi-channel device, it the fallbacks
// operations should be as follows. Assume the multi-channel device name is
// surround50:
//
// 1) Try open "surround50:CARD=foo,DEV=0"
// 2) Try open "plug:surround50:CARD=foo,DEV=0".
// 3) Try open "plug:surround50".
// 4) Try open "default".
// 5) Try open "plug:default".
// 6) Give up trying to open.
//
const std::array<std::string, 5> kTries{
{kSurround50, std::string(AlsaPcmOutputStream::kPlugPrefix) + kSurround50,
std::string(AlsaPcmOutputStream::kPlugPrefix) + kGenericSurround50,
AlsaPcmOutputStream::kDefaultDevice,
std::string(AlsaPcmOutputStream::kPlugPrefix) +
AlsaPcmOutputStream::kDefaultDevice}};
EXPECT_CALL(mock_alsa_wrapper_, DeviceNameHint(_, _, _))
.WillOnce(DoAll(SetArgPointee<2>(GetFakeHints()), Return(0)));
EXPECT_CALL(mock_alsa_wrapper_, DeviceNameFreeHint(GetFakeHints())).Times(1);
EXPECT_CALL(mock_alsa_wrapper_, DeviceNameGetHint(_, StrEq("IOID")))
.WillRepeatedly(Invoke(OutputHint));
EXPECT_CALL(mock_alsa_wrapper_, DeviceNameGetHint(_, StrEq("NAME")))
.WillRepeatedly(Invoke(EchoHint));
EXPECT_CALL(mock_alsa_wrapper_, StrError(kTestFailedErrno))
.WillRepeatedly(Return(kDummyMessage));
InSequence s;
EXPECT_CALL(mock_alsa_wrapper_, PcmOpen(_, StrEq(kTries[0].c_str()), _, _))
.WillOnce(Return(kTestFailedErrno));
EXPECT_CALL(mock_alsa_wrapper_, PcmOpen(_, StrEq(kTries[1].c_str()), _, _))
.WillOnce(Return(kTestFailedErrno));
EXPECT_CALL(mock_alsa_wrapper_, PcmOpen(_, StrEq(kTries[2].c_str()), _, _))
.WillOnce(Return(kTestFailedErrno));
EXPECT_CALL(mock_alsa_wrapper_, PcmOpen(_, StrEq(kTries[3].c_str()), _, _))
.WillOnce(Return(kTestFailedErrno));
EXPECT_CALL(mock_alsa_wrapper_, PcmOpen(_, StrEq(kTries[4].c_str()), _, _))
.WillOnce(Return(kTestFailedErrno));
AlsaPcmOutputStream* test_stream = CreateStream(CHANNEL_LAYOUT_5_0);
EXPECT_FALSE(test_stream->AutoSelectDevice(5));
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, AutoSelectDevice_HintFail) {
// Should get |kDefaultDevice|, and force a 2-channel downmix on a failure to
// enumerate devices.
EXPECT_CALL(mock_alsa_wrapper_, DeviceNameHint(_, _, _))
.WillRepeatedly(Return(kTestFailedErrno));
EXPECT_CALL(mock_alsa_wrapper_,
PcmOpen(_, StrEq(AlsaPcmOutputStream::kDefaultDevice), _, _))
.WillOnce(DoAll(SetArgPointee<0>(GetFakeHandle()), Return(0)));
EXPECT_CALL(mock_alsa_wrapper_,
PcmSetParams(GetFakeHandle(), _, _, 2, _, _, _))
.WillOnce(Return(0));
EXPECT_CALL(mock_alsa_wrapper_, StrError(kTestFailedErrno))
.WillOnce(Return(kDummyMessage));
AlsaPcmOutputStream* test_stream = CreateStream(CHANNEL_LAYOUT_5_0);
EXPECT_TRUE(test_stream->AutoSelectDevice(5));
EXPECT_TRUE(test_stream->channel_mixer_);
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, BufferPacket_StopStream) {
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
InitBuffer(test_stream);
test_stream->stop_stream_ = true;
bool source_exhausted;
test_stream->BufferPacket(&source_exhausted);
EXPECT_EQ(0u, test_stream->buffer_->forward_bytes());
EXPECT_TRUE(source_exhausted);
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, ScheduleNextWrite) {
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
test_stream->TransitionTo(AlsaPcmOutputStream::kIsOpened);
test_stream->TransitionTo(AlsaPcmOutputStream::kIsPlaying);
InitBuffer(test_stream);
DVLOG(1) << test_stream->state();
EXPECT_CALL(mock_alsa_wrapper_, PcmAvailUpdate(_))
.WillOnce(Return(10));
test_stream->ScheduleNextWrite(false);
DVLOG(1) << test_stream->state();
// TODO(sergeyu): Figure out how to check that the task has been added to the
// message loop.
// Cleanup the message queue. Currently ~MessageQueue() doesn't free pending
// tasks unless running on valgrind. The code below is needed to keep
// heapcheck happy.
test_stream->stop_stream_ = true;
DVLOG(1) << test_stream->state();
test_stream->TransitionTo(AlsaPcmOutputStream::kIsClosed);
DVLOG(1) << test_stream->state();
test_stream->Close();
}
TEST_F(AlsaPcmOutputStreamTest, ScheduleNextWrite_StopStream) {
AlsaPcmOutputStream* test_stream = CreateStream(kTestChannelLayout);
test_stream->TransitionTo(AlsaPcmOutputStream::kIsOpened);
test_stream->TransitionTo(AlsaPcmOutputStream::kIsPlaying);
InitBuffer(test_stream);
test_stream->stop_stream_ = true;
test_stream->ScheduleNextWrite(true);
// TODO(ajwong): Find a way to test whether or not another task has been
// posted so we can verify that the Alsa code will indeed break the task
// posting loop.
test_stream->TransitionTo(AlsaPcmOutputStream::kIsClosed);
test_stream->Close();
}
} // namespace media