1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/renderers/audio_renderer_impl.h"
11 #include "base/format_macros.h"
12 #include "base/functional/bind.h"
13 #include "base/functional/callback_helpers.h"
14 #include "base/memory/scoped_refptr.h"
15 #include "base/run_loop.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/task/single_thread_task_runner.h"
18 #include "base/test/gmock_callback_support.h"
19 #include "base/test/scoped_feature_list.h"
20 #include "base/test/simple_test_tick_clock.h"
21 #include "base/test/task_environment.h"
22 #include "base/time/time.h"
23 #include "build/build_config.h"
24 #include "media/base/audio_buffer_converter.h"
25 #include "media/base/audio_bus.h"
26 #include "media/base/fake_audio_renderer_sink.h"
27 #include "media/base/media_client.h"
28 #include "media/base/media_switches.h"
29 #include "media/base/media_util.h"
30 #include "media/base/mock_audio_renderer_sink.h"
31 #include "media/base/mock_filters.h"
32 #include "media/base/mock_media_log.h"
33 #include "media/base/speech_recognition_client.h"
34 #include "media/base/test_helpers.h"
35 #include "testing/gmock/include/gmock/gmock.h"
36 #include "testing/gtest/include/gtest/gtest.h"
38 using ::base::TimeDelta;
39 using ::base::test::RunCallback;
40 using ::base::test::RunOnceCallback;
42 using ::testing::DoAll;
43 using ::testing::Return;
44 using ::testing::SaveArg;
50 // Since AudioBufferConverter is used due to different input/output sample
51 // rates, define some helper types to differentiate between the two.
53 explicit InputFrames(int value) : value(value) {}
58 explicit OutputFrames(int value) : value(value) {}
64 // Constants to specify the type of audio data used.
65 constexpr AudioCodec kCodec = AudioCodec::kVorbis;
66 constexpr SampleFormat kSampleFormat = kSampleFormatPlanarF32;
67 constexpr ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO;
68 constexpr int kChannels = 2;
70 // Use a different output sample rate so the AudioBufferConverter is invoked.
71 constexpr int kInputSamplesPerSecond = 5000;
72 constexpr int kOutputSamplesPerSecond = 10000;
73 constexpr double kOutputMicrosPerFrame =
74 static_cast<double>(base::Time::kMicrosecondsPerSecond) /
75 kOutputSamplesPerSecond;
77 // Arbitrarily chosen frame count for a typical input audio buffer.
78 // NOTE: Do not assume that N InputFrames in translates to N OutputFrames.
79 // Format differences between "in" vs "out" (reconciled by AudioBufferConverter)
80 // will cause the N InputFrames to generate M OutputFrames, such that N and M
81 // may be off by a significant factor.
82 constexpr int kInputFramesChunk = 256;
84 ACTION_P(EnterPendingDecoderInitStateAction, test) {
85 test->EnterPendingDecoderInitState(std::move(arg2));
88 ACTION_P(AssertNotYetEnded, test) {
89 ASSERT_FALSE(test->ended());
92 class AudioRendererImplTest : public ::testing::Test,
93 public RendererClient,
94 public SpeechRecognitionClient {
96 std::vector<std::unique_ptr<AudioDecoder>> CreateAudioDecoderForTest() {
97 auto decoder = std::make_unique<MockAudioDecoder>();
98 if (!enter_pending_decoder_init_) {
99 EXPECT_CALL(*decoder, Initialize_(_, _, _, _, _))
101 DoAll(SaveArg<3>(&output_cb_),
102 RunOnceCallback<2>(expected_init_result_
103 ? DecoderStatus::Codes::kOk
104 : DecoderStatus::Codes::kFailed)));
106 EXPECT_CALL(*decoder, Initialize_(_, _, _, _, _))
107 .WillOnce(EnterPendingDecoderInitStateAction(this));
109 EXPECT_CALL(*decoder, Decode(_, _))
110 .WillRepeatedly(Invoke(this, &AudioRendererImplTest::DecodeDecoder));
111 EXPECT_CALL(*decoder, Reset_(_))
112 .WillRepeatedly(Invoke(this, &AudioRendererImplTest::ResetDecoder));
113 std::vector<std::unique_ptr<AudioDecoder>> decoders;
114 decoders.push_back(std::move(decoder));
118 // Give the decoder some non-garbage media properties.
119 AudioRendererImplTest()
120 : hardware_params_(AudioParameters::AUDIO_PCM_LOW_LATENCY,
121 ChannelLayoutConfig::FromLayout<kChannelLayout>(),
122 kOutputSamplesPerSecond,
124 main_thread_task_runner_(
125 base::SingleThreadTaskRunner::GetCurrentDefault()),
126 sink_(new FakeAudioRendererSink(hardware_params_)),
127 demuxer_stream_(DemuxerStream::AUDIO),
128 expected_init_result_(true),
129 enter_pending_decoder_init_(false),
131 AudioDecoderConfig audio_config(kCodec, kSampleFormat, kChannelLayout,
132 kInputSamplesPerSecond, EmptyExtraData(),
133 EncryptionScheme::kUnencrypted);
134 demuxer_stream_.set_audio_decoder_config(audio_config);
136 ConfigureDemuxerStream(true);
138 AudioParameters out_params(
139 AudioParameters::AUDIO_PCM_LOW_LATENCY,
140 ChannelLayoutConfig::FromLayout<kChannelLayout>(),
141 kOutputSamplesPerSecond, 512);
142 renderer_ = std::make_unique<AudioRendererImpl>(
143 main_thread_task_runner_, sink_.get(),
144 base::BindRepeating(&AudioRendererImplTest::CreateAudioDecoderForTest,
145 base::Unretained(this)),
146 &media_log_, 0, this);
147 renderer_->tick_clock_ = &tick_clock_;
148 tick_clock_.Advance(base::Seconds(1));
151 AudioRendererImplTest(const AudioRendererImplTest&) = delete;
152 AudioRendererImplTest& operator=(const AudioRendererImplTest&) = delete;
154 ~AudioRendererImplTest() override {
155 SCOPED_TRACE("~AudioRendererImplTest()");
158 // Mock out demuxer reads.
159 void ConfigureDemuxerStream(bool supports_config_changes) {
160 EXPECT_CALL(demuxer_stream_, OnRead(_))
161 .WillRepeatedly(Invoke(this, &AudioRendererImplTest::OnDemuxerRead));
162 EXPECT_CALL(demuxer_stream_, SupportsConfigChanges())
163 .WillRepeatedly(Return(supports_config_changes));
166 void OnDemuxerRead(DemuxerStream::ReadCB& read_cb) {
167 if (simulate_demuxer_stall_) {
168 simulate_demuxer_stall_ = false;
169 stalled_demixer_read_cb_ = std::move(read_cb);
172 scoped_refptr<DecoderBuffer> decoder_buffer(new DecoderBuffer(0));
173 std::move(read_cb).Run(DemuxerStream::kOk, {std::move(decoder_buffer)});
176 bool IsDemuxerStalled() { return !!stalled_demixer_read_cb_; }
178 void UnstallDemuxer() {
179 EXPECT_TRUE(IsDemuxerStalled());
180 OnDemuxerRead(stalled_demixer_read_cb_);
183 // Reconfigures a renderer without config change support using given params.
184 void ConfigureBasicRenderer(const AudioParameters& params) {
185 hardware_params_ = params;
186 sink_ = base::MakeRefCounted<FakeAudioRendererSink>(hardware_params_);
187 renderer_ = std::make_unique<AudioRendererImpl>(
188 main_thread_task_runner_, sink_.get(),
189 base::BindRepeating(&AudioRendererImplTest::CreateAudioDecoderForTest,
190 base::Unretained(this)),
191 &media_log_, 0, nullptr);
192 testing::Mock::VerifyAndClearExpectations(&demuxer_stream_);
193 ConfigureDemuxerStream(false);
196 // Reconfigures a renderer with config change support using given params.
197 void ConfigureConfigChangeRenderer(const AudioParameters& params,
198 const AudioParameters& hardware_params) {
199 hardware_params_ = hardware_params;
200 sink_ = base::MakeRefCounted<FakeAudioRendererSink>(hardware_params_);
201 renderer_ = std::make_unique<AudioRendererImpl>(
202 main_thread_task_runner_, sink_.get(),
203 base::BindRepeating(&AudioRendererImplTest::CreateAudioDecoderForTest,
204 base::Unretained(this)),
205 &media_log_, 0, nullptr);
206 testing::Mock::VerifyAndClearExpectations(&demuxer_stream_);
207 ConfigureDemuxerStream(true);
210 void ConfigureWithMockSink(const AudioParameters& params) {
211 mock_sink_ = base::MakeRefCounted<MockAudioRendererSink>();
212 renderer_ = std::make_unique<AudioRendererImpl>(
213 main_thread_task_runner_, mock_sink_.get(),
214 base::BindRepeating(&AudioRendererImplTest::CreateAudioDecoderForTest,
215 base::Unretained(this)),
216 &media_log_, 0, nullptr);
217 testing::Mock::VerifyAndClearExpectations(&demuxer_stream_);
218 ConfigureDemuxerStream(true);
221 void ConfigureWithMockMediaLog() {
222 sink_ = base::MakeRefCounted<FakeAudioRendererSink>(hardware_params_);
223 renderer_ = std::make_unique<AudioRendererImpl>(
224 main_thread_task_runner_, sink_.get(),
225 base::BindRepeating(&AudioRendererImplTest::CreateAudioDecoderForTest,
226 base::Unretained(this)),
227 &mock_media_log_, 0, nullptr);
228 testing::Mock::VerifyAndClearExpectations(&demuxer_stream_);
229 ConfigureDemuxerStream(true);
232 void EnableSpeechRecognition() { renderer_->EnableSpeechRecognition(); }
234 // RendererClient implementation.
235 MOCK_METHOD1(OnError, void(PipelineStatus));
236 void OnFallback(PipelineStatus status) override { NOTREACHED(); }
237 void OnEnded() override {
241 void OnStatisticsUpdate(const PipelineStatistics& stats) override {
242 last_statistics_.audio_memory_usage += stats.audio_memory_usage;
244 MOCK_METHOD2(OnBufferingStateChange,
245 void(BufferingState, BufferingStateChangeReason));
246 MOCK_METHOD1(OnWaiting, void(WaitingReason));
247 MOCK_METHOD1(OnAudioConfigChange, void(const AudioDecoderConfig&));
248 MOCK_METHOD1(OnVideoConfigChange, void(const VideoDecoderConfig&));
249 MOCK_METHOD1(OnVideoNaturalSizeChange, void(const gfx::Size&));
250 MOCK_METHOD1(OnVideoOpacityChange, void(bool));
251 MOCK_METHOD1(OnVideoFrameRateChange, void(absl::optional<int>));
252 MOCK_METHOD1(OnDurationChange, void(base::TimeDelta));
253 MOCK_METHOD1(OnRemotePlayStateChange, void(MediaStatus::State state));
254 MOCK_METHOD1(TranscribeAudio, void(scoped_refptr<AudioBuffer>));
256 // SpeechRecognitionClient implementation.
257 MOCK_METHOD1(AddAudio, void(scoped_refptr<AudioBuffer>));
258 MOCK_METHOD3(AddAudioBusOnMainSequence,
259 void(std::unique_ptr<AudioBus>, int, ChannelLayout));
260 MOCK_METHOD0(IsSpeechRecognitionAvailable, bool());
261 MOCK_METHOD1(SetOnReadyCallback, void(OnReadyCallback));
262 MOCK_METHOD1(AddAudio, void(const media::AudioBus&));
263 MOCK_METHOD1(Reconfigure, void(const media::AudioParameters&));
265 void InitializeRenderer(DemuxerStream* demuxer_stream,
266 PipelineStatusCallback pipeline_status_cb) {
267 EXPECT_CALL(*this, OnWaiting(_)).Times(0);
268 EXPECT_CALL(*this, OnVideoNaturalSizeChange(_)).Times(0);
269 EXPECT_CALL(*this, OnVideoOpacityChange(_)).Times(0);
270 EXPECT_CALL(*this, OnVideoConfigChange(_)).Times(0);
271 renderer_->Initialize(demuxer_stream, nullptr, this,
272 std::move(pipeline_status_cb));
276 InitializeWithStatus(PIPELINE_OK);
278 std::make_unique<AudioTimestampHelper>(kInputSamplesPerSecond);
281 void InitializeBitstreamFormat() {
282 EXPECT_CALL(media_client_, IsSupportedBitstreamAudioCodec(_))
283 .WillRepeatedly(Return(true));
284 SetMediaClient(&media_client_);
286 hardware_params_.Reset(AudioParameters::AUDIO_BITSTREAM_EAC3,
287 ChannelLayoutConfig::FromLayout<kChannelLayout>(),
288 kOutputSamplesPerSecond, 512);
289 sink_ = base::MakeRefCounted<FakeAudioRendererSink>(hardware_params_);
290 AudioDecoderConfig audio_config(AudioCodec::kAC3, kSampleFormatEac3,
291 kChannelLayout, kInputSamplesPerSecond,
293 EncryptionScheme::kUnencrypted);
294 demuxer_stream_.set_audio_decoder_config(audio_config);
296 ConfigureDemuxerStream(true);
298 renderer_ = std::make_unique<AudioRendererImpl>(
299 main_thread_task_runner_, sink_.get(),
300 base::BindRepeating(&AudioRendererImplTest::CreateAudioDecoderForTest,
301 base::Unretained(this)),
302 &media_log_, 0, this);
307 void InitializeWithStatus(PipelineStatus expected) {
309 base::StringPrintf("InitializeWithStatus(%d)", expected.code()));
311 WaitableMessageLoopEvent event;
312 InitializeRenderer(&demuxer_stream_, event.GetPipelineStatusCB());
313 event.RunAndWaitForStatus(expected);
315 // We should have no reads.
316 EXPECT_TRUE(!decode_cb_);
319 void InitializeAndDestroy() {
320 WaitableMessageLoopEvent event;
321 InitializeRenderer(&demuxer_stream_, event.GetPipelineStatusCB());
323 // Destroy the |renderer_| before we let the MessageLoop run, this simulates
324 // an interleaving in which we end up destroying the |renderer_| while the
325 // OnDecoderSelected callback is in flight.
327 event.RunAndWaitForStatus(PIPELINE_ERROR_ABORT);
330 void InitializeAndDestroyDuringDecoderInit() {
331 enter_pending_decoder_init_ = true;
333 WaitableMessageLoopEvent event;
334 InitializeRenderer(&demuxer_stream_, event.GetPipelineStatusCB());
335 base::RunLoop().RunUntilIdle();
336 DCHECK(init_decoder_cb_);
339 event.RunAndWaitForStatus(PIPELINE_ERROR_ABORT);
342 void EnterPendingDecoderInitState(AudioDecoder::InitCB cb) {
343 init_decoder_cb_ = std::move(cb);
346 void FlushDuringPendingRead() {
347 SCOPED_TRACE("FlushDuringPendingRead()");
348 WaitableMessageLoopEvent flush_event;
349 renderer_->Flush(flush_event.GetClosure());
350 SatisfyPendingRead(InputFrames(kInputFramesChunk));
351 flush_event.RunAndWait();
353 EXPECT_FALSE(IsDecodePending());
356 void Preroll() { Preroll(base::TimeDelta(), base::TimeDelta(), PIPELINE_OK); }
358 void Preroll(base::TimeDelta start_timestamp,
359 base::TimeDelta first_timestamp,
360 PipelineStatus expected) {
361 SCOPED_TRACE(base::StringPrintf("Preroll(%" PRId64 ", %d)",
362 first_timestamp.InMilliseconds(),
364 next_timestamp_->SetBaseTimestamp(first_timestamp);
366 // Fill entire buffer to complete prerolling.
367 renderer_->SetMediaTime(start_timestamp);
368 renderer_->StartPlaying();
369 WaitForPendingRead();
370 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH,
371 BUFFERING_CHANGE_REASON_UNKNOWN));
372 DeliverRemainingAudio();
375 void StartTicking() {
376 renderer_->StartTicking();
377 renderer_->SetPlaybackRate(1.0);
380 void StopTicking() { renderer_->StopTicking(); }
382 bool IsDecodePending() const { return !!decode_cb_; }
384 void WaitForPendingRead() {
385 SCOPED_TRACE("WaitForPendingRead()");
389 DCHECK(!wait_for_pending_decode_cb_);
391 WaitableMessageLoopEvent event;
392 wait_for_pending_decode_cb_ = event.GetClosure();
396 DCHECK(!wait_for_pending_decode_cb_);
399 // Delivers decoded frames to |renderer_|.
400 void SatisfyPendingRead(InputFrames frames) {
401 CHECK_GT(frames.value, 0);
404 scoped_refptr<AudioBuffer> buffer;
405 if (hardware_params_.IsBitstreamFormat()) {
406 buffer = MakeBitstreamAudioBuffer(kSampleFormatEac3, kChannelLayout,
407 kChannels, kInputSamplesPerSecond, 1, 0,
408 frames.value, frames.value / 2,
409 next_timestamp_->GetTimestamp());
411 buffer = MakeAudioBuffer<float>(
412 kSampleFormat, kChannelLayout, kChannels, kInputSamplesPerSecond,
413 1.0f, 0.0f, frames.value, next_timestamp_->GetTimestamp());
415 next_timestamp_->AddFrames(frames.value);
417 DeliverBuffer(DecoderStatus::Codes::kOk, std::move(buffer));
420 void DeliverEndOfStream() {
423 // Return EOS buffer to trigger EOS frame.
424 DemuxerStream::DecoderBufferVector buffers;
425 buffers.emplace_back(DecoderBuffer::CreateEOSBuffer());
426 EXPECT_CALL(demuxer_stream_, OnRead(_))
427 .WillOnce(RunOnceCallback<0>(DemuxerStream::kOk, buffers));
429 // Satify pending |decode_cb_| to trigger a new DemuxerStream::Read().
430 main_thread_task_runner_->PostTask(
432 base::BindOnce(std::move(decode_cb_), DecoderStatus::Codes::kOk));
434 WaitForPendingRead();
436 main_thread_task_runner_->PostTask(
438 base::BindOnce(std::move(decode_cb_), DecoderStatus::Codes::kOk));
440 base::RunLoop().RunUntilIdle();
441 EXPECT_EQ(last_statistics_.audio_memory_usage,
442 renderer_->algorithm_->GetMemoryUsage());
445 // Delivers frames until |renderer_|'s internal buffer is full and no longer
446 // has pending reads.
447 void DeliverRemainingAudio() {
448 // NOTE: !IsDecodePending() -> frames_remaining_in_buffer() == 0... but the
449 // arrow is unidirectional! DecoderStream does its own buffering of decoded
450 // output such that it generally triggers reads even after the renderer's
451 // buffer is full. Hence, the loop below must check both of the conditions
452 // to ensure no pending reads exist after the function returns.
453 while (frames_remaining_in_buffer().value > 0 || IsDecodePending()) {
454 SatisfyPendingRead(InputFrames(kInputFramesChunk));
458 // Consumes data from the buffer until what remains drops below the buffer's
459 // capacity. Note that the buffer is often over-filled, such that consuming
460 // a fixed amount of data cannot guarantee we fall bellow the full line.
461 // Precondition: the buffer must be full when called.
462 bool ConsumeBufferedDataUntilNotFull() {
463 int buffered = frames_buffered().value;
464 int capacity = buffer_capacity().value;
465 DCHECK(buffered >= capacity);
467 int overfill = buffered > capacity ? buffered - capacity : 0;
468 int quarter_buffer = capacity / 4;
470 // Leaves the buffer 3/4 full.
471 return ConsumeBufferedData(OutputFrames(overfill + quarter_buffer));
474 // Attempts to consume |requested_frames| frames from |renderer_|'s internal
475 // buffer. Returns true if and only if all of |requested_frames| were able
477 bool ConsumeBufferedData(OutputFrames requested_frames,
478 base::TimeDelta delay) {
479 std::unique_ptr<AudioBus> bus =
480 AudioBus::Create(kChannels, requested_frames.value);
482 EXPECT_TRUE(sink_->Render(bus.get(), delay, &frames_read));
483 return frames_read == requested_frames.value;
486 bool ConsumeBufferedData(OutputFrames requested_frames) {
487 return ConsumeBufferedData(requested_frames, base::TimeDelta());
490 bool ConsumeBitstreamBufferedData(OutputFrames requested_frames,
491 base::TimeDelta delay = base::TimeDelta()) {
492 std::unique_ptr<AudioBus> bus =
493 AudioBus::Create(kChannels, requested_frames.value);
494 int total_frames_read = 0;
495 while (total_frames_read != requested_frames.value) {
497 EXPECT_TRUE(sink_->Render(bus.get(), delay, &frames_read));
499 if (frames_read <= 0)
501 total_frames_read += frames_read;
504 return total_frames_read == requested_frames.value;
507 base::TimeTicks ConvertMediaTime(base::TimeDelta timestamp,
508 bool* is_time_moving) {
509 std::vector<base::TimeTicks> wall_clock_times;
510 *is_time_moving = renderer_->GetWallClockTimes(
511 std::vector<base::TimeDelta>(1, timestamp), &wall_clock_times);
512 return wall_clock_times[0];
515 base::TimeTicks CurrentMediaWallClockTime(bool* is_time_moving) {
516 std::vector<base::TimeTicks> wall_clock_times;
517 *is_time_moving = renderer_->GetWallClockTimes(
518 std::vector<base::TimeDelta>(), &wall_clock_times);
519 return wall_clock_times[0];
522 OutputFrames frames_buffered() {
523 return OutputFrames(renderer_->algorithm_->BufferedFrames());
526 OutputFrames buffer_playback_threshold() {
527 return OutputFrames(renderer_->algorithm_->QueuePlaybackThreshold());
530 OutputFrames buffer_capacity() {
531 return OutputFrames(renderer_->algorithm_->QueueCapacity());
534 OutputFrames frames_remaining_in_buffer() {
535 // This can happen if too much data was delivered, in which case the buffer
536 // will accept the data but not increase capacity.
537 if (frames_buffered().value > buffer_capacity().value) {
538 return OutputFrames(0);
540 return OutputFrames(buffer_capacity().value - frames_buffered().value);
543 bool is_buffer_full() { return renderer_->algorithm_->IsQueueFull(); }
545 void force_config_change(const AudioDecoderConfig& config) {
546 renderer_->OnConfigChange(config);
549 InputFrames converter_input_frames_left() const {
551 renderer_->buffer_converter_->input_frames_left_for_testing());
554 base::TimeDelta CurrentMediaTime() { return renderer_->CurrentMediaTime(); }
556 std::vector<bool> channel_mask() const {
557 CHECK(renderer_->algorithm_);
558 return renderer_->algorithm_->channel_mask_for_testing();
561 bool ended() const { return ended_; }
563 void DecodeDecoder(scoped_refptr<DecoderBuffer> buffer,
564 AudioDecoder::DecodeCB decode_cb) {
565 // TODO(scherkus): Make this a DCHECK after threading semantics are fixed.
566 if (!main_thread_task_runner_->BelongsToCurrentThread()) {
567 main_thread_task_runner_->PostTask(
569 base::BindOnce(&AudioRendererImplTest::DecodeDecoder,
570 base::Unretained(this), buffer, std::move(decode_cb)));
574 CHECK(!decode_cb_) << "Overlapping decodes are not permitted";
575 decode_cb_ = std::move(decode_cb);
577 // Wake up WaitForPendingRead() if needed.
578 if (wait_for_pending_decode_cb_)
579 std::move(wait_for_pending_decode_cb_).Run();
582 void ResetDecoder(base::OnceClosure& reset_cb) {
584 // |reset_cb| will be called in DeliverBuffer(), after the decoder is
586 reset_cb_ = std::move(reset_cb);
590 main_thread_task_runner_->PostTask(FROM_HERE, std::move(reset_cb));
593 void DeliverBuffer(DecoderStatus status, scoped_refptr<AudioBuffer> buffer) {
596 if (buffer.get() && !buffer->end_of_stream())
597 output_cb_.Run(std::move(buffer));
598 std::move(decode_cb_).Run(status);
601 std::move(reset_cb_).Run();
603 base::RunLoop().RunUntilIdle();
607 AudioParameters hardware_params_;
608 base::test::TaskEnvironment task_environment_;
609 const scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
610 NullMediaLog media_log_;
611 MockMediaLog mock_media_log_;
612 std::unique_ptr<AudioRendererImpl> renderer_;
613 scoped_refptr<FakeAudioRendererSink> sink_;
614 scoped_refptr<MockAudioRendererSink> mock_sink_;
615 base::SimpleTestTickClock tick_clock_;
616 PipelineStatistics last_statistics_;
618 MockDemuxerStream demuxer_stream_;
619 MockMediaClient media_client_;
621 // When |simulate_demuxer_stall_| is set OnDemuxerRead() will put the callback
622 // in |stalled_demixer_read_cb_| instead of calling it.
623 bool simulate_demuxer_stall_ = false;
624 DemuxerStream::ReadCB stalled_demixer_read_cb_;
626 // Used for satisfying reads.
627 AudioDecoder::OutputCB output_cb_;
628 AudioDecoder::DecodeCB decode_cb_;
629 base::OnceClosure reset_cb_;
630 std::unique_ptr<AudioTimestampHelper> next_timestamp_;
632 // Run during DecodeDecoder() to unblock WaitForPendingRead().
633 base::OnceClosure wait_for_pending_decode_cb_;
635 AudioDecoder::InitCB init_decoder_cb_;
636 bool expected_init_result_;
637 bool enter_pending_decoder_init_;
641 TEST_F(AudioRendererImplTest, Initialize_Successful) {
645 TEST_F(AudioRendererImplTest, Initialize_DecoderInitFailure) {
646 expected_init_result_ = false;
647 InitializeWithStatus(DECODER_ERROR_NOT_SUPPORTED);
650 TEST_F(AudioRendererImplTest, ReinitializeForDifferentStream) {
651 // Initialize and start playback
655 EXPECT_TRUE(ConsumeBufferedDataUntilNotFull());
656 WaitForPendingRead();
658 // Stop playback and flush
660 EXPECT_TRUE(IsDecodePending());
661 // Flush and expect to be notified that we have nothing.
662 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
663 FlushDuringPendingRead();
665 // Prepare a new demuxer stream.
666 MockDemuxerStream new_stream(DemuxerStream::AUDIO);
667 EXPECT_CALL(new_stream, SupportsConfigChanges()).WillOnce(Return(false));
668 AudioDecoderConfig audio_config(kCodec, kSampleFormat, kChannelLayout,
669 kInputSamplesPerSecond, EmptyExtraData(),
670 EncryptionScheme::kUnencrypted);
671 new_stream.set_audio_decoder_config(audio_config);
673 // The renderer is now in the flushed state and can be reinitialized.
674 WaitableMessageLoopEvent event;
675 InitializeRenderer(&new_stream, event.GetPipelineStatusCB());
676 event.RunAndWaitForStatus(PIPELINE_OK);
679 TEST_F(AudioRendererImplTest, SignalConfigChange) {
680 // Initialize and start playback.
684 EXPECT_TRUE(ConsumeBufferedData(OutputFrames(256)));
685 // Note: no need to WaitForPendingRead() here or below. Historically this test
686 // seemed to wait, but really the read was already pending because preroll
687 // didn't satisfy the final read from decoder stream.
689 // Force config change to simulate detected change from decoder stream. Expect
690 // that RendererClient to be signaled with the new config.
691 const AudioDecoderConfig kValidAudioConfig(
692 AudioCodec::kVorbis, kSampleFormatPlanarF32, CHANNEL_LAYOUT_STEREO, 44100,
693 EmptyExtraData(), EncryptionScheme::kUnencrypted);
694 EXPECT_TRUE(kValidAudioConfig.IsValidConfig());
695 EXPECT_CALL(*this, OnAudioConfigChange(DecoderConfigEq(kValidAudioConfig)));
696 force_config_change(kValidAudioConfig);
698 // Verify rendering can continue after config change.
699 EXPECT_TRUE(ConsumeBufferedData(OutputFrames(256)));
701 // Force a config change with an invalid dummy config. This is occasionally
702 // done to reset internal state and should not bubble to the RendererClient.
703 EXPECT_CALL(*this, OnAudioConfigChange(_)).Times(0);
704 const AudioDecoderConfig kInvalidConfig;
705 EXPECT_FALSE(kInvalidConfig.IsValidConfig());
706 force_config_change(kInvalidConfig);
709 TEST_F(AudioRendererImplTest, Preroll) {
714 TEST_F(AudioRendererImplTest, StartTicking) {
719 // Drain internal buffer, we should have a pending read.
720 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
721 WaitForPendingRead();
724 TEST_F(AudioRendererImplTest, EndOfStream) {
729 // Drain internal buffer, we should have a pending read.
730 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
731 WaitForPendingRead();
733 // Forcefully trigger underflow.
734 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
735 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
737 // Fulfill the read with an end-of-stream buffer. Doing so should change our
738 // buffering state so playback resumes.
739 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH,
740 BUFFERING_CHANGE_REASON_UNKNOWN));
741 DeliverEndOfStream();
743 // Consume all remaining data. We shouldn't have signal ended yet.
744 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
745 base::RunLoop().RunUntilIdle();
746 EXPECT_FALSE(ended());
748 // Ended should trigger on next render call.
749 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
750 base::RunLoop().RunUntilIdle();
751 EXPECT_TRUE(ended());
754 TEST_F(AudioRendererImplTest, DecoderUnderflow) {
759 // Drain internal buffer, we should have a pending read.
760 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
761 WaitForPendingRead();
763 // Verify the next FillBuffer() call triggers a buffering state change
764 // update. Expect a decoder underflow flag because demuxer is not blocked on a
767 *this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, DECODER_UNDERFLOW));
768 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
770 // Verify we're still not getting audio data.
771 EXPECT_EQ(0, frames_buffered().value);
772 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
774 // Deliver enough data to have enough for buffering.
775 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH,
776 BUFFERING_CHANGE_REASON_UNKNOWN));
777 DeliverRemainingAudio();
779 // Verify we're getting audio data.
780 EXPECT_TRUE(ConsumeBufferedData(OutputFrames(1)));
783 TEST_F(AudioRendererImplTest, DemuxerUnderflow) {
788 // Drain internal buffer, we should have a pending read.
789 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
790 WaitForPendingRead();
792 // Verify the next FillBuffer() call triggers a buffering state change
793 // update. Expect a demuxer underflow flag because demuxer is blocked on a
796 *this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, DEMUXER_UNDERFLOW));
797 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
799 // Verify we're still not getting audio data.
800 EXPECT_EQ(0, frames_buffered().value);
801 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
803 // Deliver enough data to have enough for buffering.
804 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH,
805 BUFFERING_CHANGE_REASON_UNKNOWN));
807 // Stall the demuxer to trigger underflow.
808 simulate_demuxer_stall_ = true;
809 SatisfyPendingRead(InputFrames(kInputFramesChunk));
812 DeliverRemainingAudio();
814 // Verify we're getting audio data.
815 EXPECT_TRUE(ConsumeBufferedData(OutputFrames(1)));
818 TEST_F(AudioRendererImplTest, Underflow_CapacityResetsAfterFlush) {
823 // Drain internal buffer, we should have a pending read.
824 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
825 WaitForPendingRead();
827 // Verify the next FillBuffer() call triggers the underflow callback
828 // since the decoder hasn't delivered any data after it was drained.
829 OutputFrames initial_capacity = buffer_capacity();
830 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
831 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
833 // Verify that the buffer capacity increased as a result of underflowing.
834 EXPECT_GT(buffer_capacity().value, initial_capacity.value);
836 // Verify that the buffer capacity is restored to the |initial_capacity|.
838 FlushDuringPendingRead();
839 EXPECT_EQ(buffer_capacity().value, initial_capacity.value);
842 TEST_F(AudioRendererImplTest, Underflow_CapacityIncreasesBeforeHaveNothing) {
847 // Verify the next FillBuffer() call triggers the underflow callback
848 // since the decoder hasn't delivered any data after it was drained.
849 OutputFrames initial_capacity = buffer_capacity();
851 // Drain internal buffer.
852 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(frames_buffered().value + 1)));
854 // Verify that the buffer capacity increased despite not sending have nothing.
855 EXPECT_GT(buffer_capacity().value, initial_capacity.value);
858 TEST_F(AudioRendererImplTest, Underflow_OneCapacityIncreasePerUnderflow) {
863 OutputFrames prev_capacity = buffer_capacity();
865 // Consume more than is available (partial read) to trigger underflow.
866 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
867 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(frames_buffered().value + 1)));
869 // Verify first underflow triggers an increase to buffer capacity and
870 // signals HAVE_NOTHING.
871 EXPECT_GT(buffer_capacity().value, prev_capacity.value);
872 prev_capacity = buffer_capacity();
873 // Give HAVE_NOTHING a chance to post.
874 base::RunLoop().RunUntilIdle();
875 testing::Mock::VerifyAndClearExpectations(this);
877 // Try reading again, this time with the queue totally empty. We should expect
878 // NO additional HAVE_NOTHING and NO increase to capacity because we still
879 // haven't refilled the queue since the previous underflow.
880 EXPECT_EQ(0, frames_buffered().value);
881 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _))
883 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
884 EXPECT_EQ(buffer_capacity().value, prev_capacity.value);
885 // Give HAVE_NOTHING a chance to NOT post.
886 base::RunLoop().RunUntilIdle();
887 testing::Mock::VerifyAndClearExpectations(this);
889 // Fill the buffer back up.
890 WaitForPendingRead();
891 DeliverRemainingAudio();
892 EXPECT_GT(frames_buffered().value, 0);
894 // Consume all available data without underflowing. Expect no buffer state
895 // change and no change to capacity.
896 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _))
898 EXPECT_TRUE(ConsumeBufferedData(OutputFrames(frames_buffered().value)));
899 EXPECT_EQ(buffer_capacity().value, prev_capacity.value);
900 // Give HAVE_NOTHING a chance to NOT post.
901 base::RunLoop().RunUntilIdle();
902 testing::Mock::VerifyAndClearExpectations(this);
904 // Now empty, trigger underflow attempting to read one frame. This should
905 // signal buffering state change and increase capacity.
906 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
907 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
908 EXPECT_GT(buffer_capacity().value, prev_capacity.value);
909 // Give HAVE_NOTHING a chance to NOT post.
910 base::RunLoop().RunUntilIdle();
911 testing::Mock::VerifyAndClearExpectations(this);
914 // Verify that the proper reduced search space is configured for playback rate
915 // changes when upmixing is applied to the input.
916 TEST_F(AudioRendererImplTest, ChannelMask) {
917 AudioParameters hw_params(
918 AudioParameters::AUDIO_PCM_LOW_LATENCY,
919 ChannelLayoutConfig::FromLayout<CHANNEL_LAYOUT_7_1>(),
920 kOutputSamplesPerSecond, 1024);
921 ConfigureConfigChangeRenderer(
922 AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
923 ChannelLayoutConfig::Stereo(), kOutputSamplesPerSecond,
927 std::vector<bool> mask = channel_mask();
928 EXPECT_FALSE(mask.empty());
929 ASSERT_EQ(mask.size(), static_cast<size_t>(hw_params.channels()));
930 for (int ch = 0; ch < hw_params.channels(); ++ch) {
932 ASSERT_FALSE(mask[ch]);
934 ASSERT_TRUE(mask[ch]);
937 renderer_->SetMediaTime(base::TimeDelta());
938 renderer_->StartPlaying();
939 WaitForPendingRead();
941 // Force a channel configuration change.
942 scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<float>(
943 kSampleFormat, hw_params.channel_layout(), hw_params.channels(),
944 kInputSamplesPerSecond, 1.0f, 0.0f, kInputFramesChunk, base::TimeDelta());
945 DeliverBuffer(DecoderStatus::Codes::kOk, std::move(buffer));
947 // All channels should now be enabled.
948 mask = channel_mask();
949 EXPECT_FALSE(mask.empty());
950 ASSERT_EQ(mask.size(), static_cast<size_t>(hw_params.channels()));
951 for (int ch = 0; ch < hw_params.channels(); ++ch)
952 ASSERT_TRUE(mask[ch]);
955 // Verify that the proper channel mask is configured when downmixing is applied
956 // to the input with discrete layout. The default hardware layout is stereo.
957 TEST_F(AudioRendererImplTest, ChannelMask_DownmixDiscreteLayout) {
958 int audio_channels = 9;
960 AudioDecoderConfig audio_config(
961 AudioCodec::kOpus, kSampleFormat, CHANNEL_LAYOUT_DISCRETE,
962 kInputSamplesPerSecond, EmptyExtraData(), EncryptionScheme::kUnencrypted);
963 audio_config.SetChannelsForDiscrete(audio_channels);
964 demuxer_stream_.set_audio_decoder_config(audio_config);
965 ConfigureDemuxerStream(true);
967 // Fake an attached webaudio client.
968 sink_->SetIsOptimizedForHardwareParameters(false);
971 std::vector<bool> mask = channel_mask();
972 EXPECT_FALSE(mask.empty());
973 ASSERT_EQ(mask.size(), static_cast<size_t>(audio_channels));
974 for (int ch = 0; ch < audio_channels; ++ch)
975 ASSERT_TRUE(mask[ch]);
978 TEST_F(AudioRendererImplTest, Underflow_Flush) {
984 EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
985 WaitForPendingRead();
986 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
987 EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
988 WaitForPendingRead();
991 // We shouldn't expect another buffering state change when flushing.
992 FlushDuringPendingRead();
995 TEST_F(AudioRendererImplTest, PendingRead_Flush) {
1001 // Partially drain internal buffer so we get a pending read.
1002 EXPECT_TRUE(ConsumeBufferedDataUntilNotFull());
1003 WaitForPendingRead();
1007 EXPECT_TRUE(IsDecodePending());
1009 // Flush and expect to be notified that we have nothing.
1010 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
1011 FlushDuringPendingRead();
1013 // Preroll again to a different timestamp and verify it completed normally.
1014 const base::TimeDelta seek_timestamp = base::Milliseconds(1000);
1015 Preroll(seek_timestamp, seek_timestamp, PIPELINE_OK);
1018 TEST_F(AudioRendererImplTest, PendingRead_Destroy) {
1024 // Partially drain internal buffer so we get a pending read.
1025 EXPECT_TRUE(ConsumeBufferedDataUntilNotFull());
1026 WaitForPendingRead();
1030 EXPECT_TRUE(IsDecodePending());
1035 TEST_F(AudioRendererImplTest, PendingFlush_Destroy) {
1041 // Partially drain internal buffer so we get a pending read.
1042 EXPECT_TRUE(ConsumeBufferedDataUntilNotFull());
1043 WaitForPendingRead();
1047 EXPECT_TRUE(IsDecodePending());
1050 WaitableMessageLoopEvent flush_event;
1051 renderer_->Flush(flush_event.GetClosure());
1053 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
1054 SatisfyPendingRead(InputFrames(kInputFramesChunk));
1059 TEST_F(AudioRendererImplTest, InitializeThenDestroy) {
1060 InitializeAndDestroy();
1063 TEST_F(AudioRendererImplTest, InitializeThenDestroyDuringDecoderInit) {
1064 InitializeAndDestroyDuringDecoderInit();
1067 TEST_F(AudioRendererImplTest, CurrentMediaTimeBehavior) {
1072 AudioTimestampHelper timestamp_helper(kOutputSamplesPerSecond);
1073 timestamp_helper.SetBaseTimestamp(base::TimeDelta());
1075 // Time should be the starting timestamp as nothing has been consumed yet.
1076 EXPECT_EQ(timestamp_helper.GetTimestamp(), CurrentMediaTime());
1078 const OutputFrames frames_to_consume(frames_buffered().value / 3);
1079 const base::TimeDelta kConsumptionDuration =
1080 timestamp_helper.GetFrameDuration(frames_to_consume.value);
1082 // Render() has not be called yet, thus no data has been consumed, so
1083 // advancing tick clock must not change the media time.
1084 tick_clock_.Advance(kConsumptionDuration);
1085 EXPECT_EQ(timestamp_helper.GetTimestamp(), CurrentMediaTime());
1087 // Consume some audio data.
1088 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume));
1089 // No need to wait for a pending read here. It may or may not happen depending
1090 // on how over-filled the buffer is. Either way, not important for this test.
1092 // Time shouldn't change just yet because we've only sent the initial audio
1093 // data to the hardware.
1094 EXPECT_EQ(timestamp_helper.GetTimestamp(), CurrentMediaTime());
1096 // Advancing the tick clock now should result in an estimated media time.
1097 tick_clock_.Advance(kConsumptionDuration);
1098 EXPECT_EQ(timestamp_helper.GetTimestamp() + kConsumptionDuration,
1099 CurrentMediaTime());
1101 // Consume some more audio data.
1102 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume));
1104 // Time should change now that Render() has been called a second time.
1105 timestamp_helper.AddFrames(frames_to_consume.value);
1106 EXPECT_EQ(timestamp_helper.GetTimestamp(), CurrentMediaTime());
1108 // Advance current time well past all played audio to simulate an irregular or
1109 // delayed OS callback. The value should be clamped to whats been rendered.
1110 timestamp_helper.AddFrames(frames_to_consume.value);
1111 tick_clock_.Advance(kConsumptionDuration * 2);
1112 EXPECT_EQ(timestamp_helper.GetTimestamp(), CurrentMediaTime());
1114 // Consume some more audio data.
1115 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume));
1117 // Stop ticking, the media time should be clamped to what's been rendered.
1119 EXPECT_EQ(timestamp_helper.GetTimestamp(), CurrentMediaTime());
1120 tick_clock_.Advance(kConsumptionDuration * 2);
1121 timestamp_helper.AddFrames(frames_to_consume.value);
1122 EXPECT_EQ(timestamp_helper.GetTimestamp(), CurrentMediaTime());
1125 TEST_F(AudioRendererImplTest, RenderingDelayedForEarlyStartTime) {
1128 // Choose a first timestamp a few buffers into the future, which ends halfway
1129 // through the desired output buffer; this allows for maximum test coverage.
1130 const double kBuffers = 4.5;
1131 const base::TimeDelta first_timestamp =
1132 base::Seconds(hardware_params_.frames_per_buffer() * kBuffers /
1133 hardware_params_.sample_rate());
1135 Preroll(base::TimeDelta(), first_timestamp, PIPELINE_OK);
1138 // Verify the first few buffers are silent.
1139 std::unique_ptr<AudioBus> bus = AudioBus::Create(hardware_params_);
1140 int frames_read = 0;
1141 for (int i = 0; i < std::floor(kBuffers); ++i) {
1142 EXPECT_TRUE(sink_->Render(bus.get(), base::TimeDelta(), &frames_read));
1143 EXPECT_EQ(frames_read, bus->frames());
1144 for (int j = 0; j < bus->frames(); ++j)
1145 ASSERT_FLOAT_EQ(0.0f, bus->channel(0)[j]);
1147 // Buffer may have been previously over-filled. Only expect new reads when
1148 // we drop below "full".
1149 if (!is_buffer_full()) {
1150 WaitForPendingRead();
1151 DeliverRemainingAudio();
1155 // Verify the last buffer is half silence and half real data.
1156 EXPECT_TRUE(sink_->Render(bus.get(), base::TimeDelta(), &frames_read));
1157 EXPECT_EQ(frames_read, bus->frames());
1158 const int zero_frames =
1159 bus->frames() * (kBuffers - static_cast<int>(kBuffers));
1161 for (int i = 0; i < zero_frames; ++i)
1162 ASSERT_FLOAT_EQ(0.0f, bus->channel(0)[i]);
1163 for (int i = zero_frames; i < bus->frames(); ++i)
1164 ASSERT_NE(0.0f, bus->channel(0)[i]);
1167 TEST_F(AudioRendererImplTest, RenderingDelayedForSuspend) {
1169 Preroll(base::TimeDelta(), base::TimeDelta(), PIPELINE_OK);
1172 // Verify the first buffer is real data.
1173 int frames_read = 0;
1174 std::unique_ptr<AudioBus> bus = AudioBus::Create(hardware_params_);
1175 EXPECT_TRUE(sink_->Render(bus.get(), base::TimeDelta(), &frames_read));
1176 EXPECT_NE(0, frames_read);
1177 for (int i = 0; i < bus->frames(); ++i)
1178 ASSERT_NE(0.0f, bus->channel(0)[i]);
1180 // Verify after suspend we get silence.
1181 renderer_->OnSuspend();
1182 EXPECT_TRUE(sink_->Render(bus.get(), base::TimeDelta(), &frames_read));
1183 EXPECT_EQ(0, frames_read);
1185 // Verify after resume we get audio.
1187 renderer_->OnResume();
1188 EXPECT_TRUE(sink_->Render(bus.get(), base::TimeDelta(), &frames_read));
1189 EXPECT_NE(0, frames_read);
1190 for (int i = 0; i < bus->frames(); ++i)
1191 ASSERT_NE(0.0f, bus->channel(0)[i]);
1194 TEST_F(AudioRendererImplTest, AbsurdRenderingDelayLog) {
1195 ConfigureWithMockMediaLog();
1197 Preroll(base::TimeDelta(), base::TimeDelta(), PIPELINE_OK);
1200 // Verify the first buffer is real data.
1201 int frames_read = 0;
1202 std::unique_ptr<AudioBus> bus = AudioBus::Create(hardware_params_);
1204 EXPECT_MEDIA_LOG_ON(mock_media_log_,
1205 testing::HasSubstr("Large rendering delay"));
1206 EXPECT_TRUE(sink_->Render(bus.get(), base::Seconds(5), &frames_read));
1209 TEST_F(AudioRendererImplTest, RenderingDelayDoesNotOverflow) {
1212 // Choose a first timestamp as far into the future as possible. Without care
1213 // this can cause an overflow in rendering arithmetic.
1214 Preroll(base::TimeDelta(), base::TimeDelta::Max(), PIPELINE_OK);
1216 EXPECT_TRUE(ConsumeBufferedData(OutputFrames(1)));
1219 TEST_F(AudioRendererImplTest, ImmediateEndOfStream) {
1222 renderer_->SetMediaTime(base::TimeDelta());
1223 renderer_->StartPlaying();
1224 WaitForPendingRead();
1226 // The buffering state change must occur before the ended signal.
1227 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH,
1228 BUFFERING_CHANGE_REASON_UNKNOWN))
1229 .WillOnce(AssertNotYetEnded(this));
1230 DeliverEndOfStream();
1232 EXPECT_TRUE(ended());
1235 TEST_F(AudioRendererImplTest, OnRenderErrorCausesDecodeError) {
1240 EXPECT_CALL(*this, OnError(HasStatusCode(AUDIO_RENDERER_ERROR)));
1241 sink_->OnRenderError();
1242 base::RunLoop().RunUntilIdle();
1245 // Test for AudioRendererImpl calling Pause()/Play() on the sink when the
1246 // playback rate is set to zero and non-zero.
1247 TEST_F(AudioRendererImplTest, SetPlaybackRate) {
1251 // Rendering hasn't started. Sink should always be paused.
1252 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
1253 renderer_->SetPlaybackRate(0.0);
1254 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
1255 renderer_->SetPlaybackRate(1.0);
1256 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
1258 // Rendering has started with non-zero rate. Rate changes will affect sink
1260 renderer_->StartTicking();
1261 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
1262 renderer_->SetPlaybackRate(0.0);
1263 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
1264 renderer_->SetPlaybackRate(1.0);
1265 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
1267 // Rendering has stopped. Sink should be paused.
1268 renderer_->StopTicking();
1269 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
1271 // Start rendering with zero playback rate. Sink should be paused until
1272 // non-zero rate is set.
1273 renderer_->SetPlaybackRate(0.0);
1274 renderer_->StartTicking();
1275 EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
1276 renderer_->SetPlaybackRate(1.0);
1277 EXPECT_EQ(FakeAudioRendererSink::kPlaying, sink_->state());
1280 TEST_F(AudioRendererImplTest, TimeSourceBehavior) {
1284 AudioTimestampHelper timestamp_helper(kOutputSamplesPerSecond);
1285 timestamp_helper.SetBaseTimestamp(base::TimeDelta());
1287 // Prior to start, time should be shown as not moving.
1288 bool is_time_moving = false;
1289 EXPECT_EQ(base::TimeTicks(),
1290 ConvertMediaTime(base::TimeDelta(), &is_time_moving));
1291 EXPECT_FALSE(is_time_moving);
1293 EXPECT_EQ(base::TimeTicks(), CurrentMediaWallClockTime(&is_time_moving));
1294 EXPECT_FALSE(is_time_moving);
1296 // Start ticking, but use a zero playback rate, time should still be stopped
1297 // until a positive playback rate is set and the first Render() is called.
1298 renderer_->SetPlaybackRate(0.0);
1300 EXPECT_EQ(base::TimeTicks(), CurrentMediaWallClockTime(&is_time_moving));
1301 EXPECT_FALSE(is_time_moving);
1302 renderer_->SetPlaybackRate(1.0);
1303 EXPECT_EQ(base::TimeTicks(), CurrentMediaWallClockTime(&is_time_moving));
1304 EXPECT_FALSE(is_time_moving);
1305 renderer_->SetPlaybackRate(1.0);
1307 // Issue the first render call to start time moving.
1308 OutputFrames frames_to_consume(frames_buffered().value / 2);
1309 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume));
1310 WaitForPendingRead();
1312 // Time shouldn't change just yet because we've only sent the initial audio
1313 // data to the hardware.
1314 EXPECT_EQ(tick_clock_.NowTicks(),
1315 ConvertMediaTime(base::TimeDelta(), &is_time_moving));
1316 EXPECT_TRUE(is_time_moving);
1318 // A system suspend should freeze the time state and resume restart it.
1319 renderer_->OnSuspend();
1320 EXPECT_EQ(tick_clock_.NowTicks(),
1321 ConvertMediaTime(base::TimeDelta(), &is_time_moving));
1322 EXPECT_FALSE(is_time_moving);
1323 renderer_->OnResume();
1324 EXPECT_EQ(tick_clock_.NowTicks(),
1325 ConvertMediaTime(base::TimeDelta(), &is_time_moving));
1326 EXPECT_TRUE(is_time_moving);
1328 // Consume some more audio data.
1329 frames_to_consume = frames_buffered();
1330 tick_clock_.Advance(base::Seconds(1.0 / kOutputSamplesPerSecond));
1331 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume));
1333 // Time should change now that the audio hardware has called back.
1334 const base::TimeTicks wall_clock_time_zero =
1335 tick_clock_.NowTicks() -
1336 timestamp_helper.GetFrameDuration(frames_to_consume.value);
1337 EXPECT_EQ(wall_clock_time_zero,
1338 ConvertMediaTime(base::TimeDelta(), &is_time_moving));
1339 EXPECT_TRUE(is_time_moving);
1341 // Store current media time before advancing the tick clock since the call is
1342 // compensated based on TimeTicks::Now().
1343 const base::TimeDelta current_media_time = renderer_->CurrentMediaTime();
1345 // The current wall clock time should change as our tick clock advances, up
1346 // until we've reached the end of played out frames.
1347 const int kSteps = 4;
1348 const base::TimeDelta kAdvanceDelta =
1349 timestamp_helper.GetFrameDuration(frames_to_consume.value) / kSteps;
1351 for (int i = 0; i < kSteps; ++i) {
1352 tick_clock_.Advance(kAdvanceDelta);
1353 EXPECT_EQ(tick_clock_.NowTicks(),
1354 CurrentMediaWallClockTime(&is_time_moving));
1355 EXPECT_TRUE(is_time_moving);
1358 // Converting the current media time should be relative to wall clock zero.
1359 EXPECT_EQ(wall_clock_time_zero + kSteps * kAdvanceDelta,
1360 ConvertMediaTime(current_media_time, &is_time_moving));
1361 EXPECT_TRUE(is_time_moving);
1363 // Advancing once more will exceed the amount of played out frames finally.
1364 const base::TimeDelta kOneSample =
1365 base::Seconds(1.0 / kOutputSamplesPerSecond);
1366 base::TimeTicks current_time = tick_clock_.NowTicks();
1367 tick_clock_.Advance(kOneSample);
1368 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving));
1369 EXPECT_TRUE(is_time_moving);
1372 DeliverRemainingAudio();
1374 // Elapse a lot of time between StopTicking() and the next Render() call.
1375 const base::TimeDelta kOneSecond = base::Seconds(1);
1376 tick_clock_.Advance(kOneSecond);
1379 // Time should be stopped until the next render call.
1380 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving));
1381 EXPECT_FALSE(is_time_moving);
1383 // Consume some buffered data with a small delay.
1384 uint32_t delay_frames = 500;
1385 base::TimeDelta delay_time =
1386 base::Microseconds(std::round(delay_frames * kOutputMicrosPerFrame));
1388 frames_to_consume.value = frames_buffered().value / 16;
1389 EXPECT_TRUE(ConsumeBufferedData(frames_to_consume, delay_time));
1391 // Verify time is adjusted for the current delay.
1392 current_time = tick_clock_.NowTicks() + delay_time;
1393 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving));
1394 EXPECT_TRUE(is_time_moving);
1395 EXPECT_EQ(current_time,
1396 ConvertMediaTime(renderer_->CurrentMediaTime(), &is_time_moving));
1397 EXPECT_TRUE(is_time_moving);
1399 tick_clock_.Advance(kOneSample);
1400 renderer_->SetPlaybackRate(2);
1401 EXPECT_EQ(current_time, CurrentMediaWallClockTime(&is_time_moving));
1402 EXPECT_TRUE(is_time_moving);
1403 EXPECT_EQ(current_time + kOneSample * 2,
1404 ConvertMediaTime(renderer_->CurrentMediaTime(), &is_time_moving));
1405 EXPECT_TRUE(is_time_moving);
1407 // Advance far enough that we shouldn't be clamped to current time (tested
1409 tick_clock_.Advance(kOneSecond);
1411 current_time + timestamp_helper.GetFrameDuration(frames_to_consume.value),
1412 CurrentMediaWallClockTime(&is_time_moving));
1413 EXPECT_TRUE(is_time_moving);
1416 TEST_F(AudioRendererImplTest, BitstreamEndOfStream) {
1417 // NOTE: bitstream formats are pass-through to sink, so input size == output
1418 // NOTE: bitstream audio buffers must always consume a whole number of
1419 // buffers (i.e. N*kOutputFramesChunk).
1420 const int kOutputFramesChunk = kInputFramesChunk;
1422 InitializeBitstreamFormat();
1426 // Drain past the internal buffer, triggering underflow and a pending read.
1427 EXPECT_FALSE(ConsumeBitstreamBufferedData(
1428 OutputFrames(frames_buffered().value + kOutputFramesChunk)));
1429 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
1430 WaitForPendingRead();
1432 // Fulfill the read with an end-of-stream buffer. Doing so should change our
1433 // buffering state so playback resumes.
1434 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH,
1435 BUFFERING_CHANGE_REASON_UNKNOWN));
1436 DeliverEndOfStream();
1438 // Consume all remaining data. We shouldn't have signal ended yet.
1439 if (frames_buffered().value != 0)
1440 EXPECT_TRUE(ConsumeBitstreamBufferedData(frames_buffered()));
1441 base::RunLoop().RunUntilIdle();
1442 EXPECT_FALSE(ended());
1444 // Ended should trigger on next render call.
1445 EXPECT_FALSE(ConsumeBitstreamBufferedData(OutputFrames(1)));
1446 base::RunLoop().RunUntilIdle();
1447 EXPECT_TRUE(ended());
1449 // Clear the use of |media_client_|, which was set in
1450 // InitializeBitstreamFormat().
1451 SetMediaClient(nullptr);
1454 TEST_F(AudioRendererImplTest, MutedPlaybackBadDeviceInfo) {
1455 base::test::ScopedFeatureList scoped_feature_list_;
1456 scoped_feature_list_.InitAndEnableFeature(kSuspendMutedAudio);
1458 mock_sink_ = base::MakeRefCounted<MockAudioRendererSink>(
1459 std::string(), OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED,
1461 renderer_ = std::make_unique<AudioRendererImpl>(
1462 main_thread_task_runner_, mock_sink_.get(),
1463 base::BindRepeating(&AudioRendererImplTest::CreateAudioDecoderForTest,
1464 base::Unretained(this)),
1465 &media_log_, 0, nullptr);
1466 testing::Mock::VerifyAndClearExpectations(&demuxer_stream_);
1467 ConfigureDemuxerStream(true);
1469 EXPECT_CALL(*mock_sink_, SetVolume(0)).Times(0);
1470 renderer_->SetVolume(0);
1472 // Playback startup should use never touch our passed in sink, since an
1473 // internal NullAudioSink is always used for bad device info.
1474 EXPECT_CALL(*mock_sink_, Start()).Times(0);
1479 EXPECT_CALL(*mock_sink_, Pause()).Times(0);
1481 EXPECT_CALL(*mock_sink_, Play()).Times(0);
1483 testing::Mock::VerifyAndClearExpectations(mock_sink_.get());
1485 EXPECT_CALL(*mock_sink_, SetVolume(1)).Times(0);
1486 EXPECT_CALL(*mock_sink_, Start()).Times(0);
1487 EXPECT_CALL(*mock_sink_, Play()).Times(0);
1488 renderer_->SetVolume(1);
1490 EXPECT_CALL(*mock_sink_, Pause()).Times(0);
1492 EXPECT_CALL(*mock_sink_, Stop()).Times(0);
1495 TEST_F(AudioRendererImplTest, BasicMutedPlayback) {
1496 base::test::ScopedFeatureList scoped_feature_list_;
1497 scoped_feature_list_.InitAndEnableFeature(kSuspendMutedAudio);
1498 ConfigureWithMockSink(hardware_params_);
1500 EXPECT_CALL(*mock_sink_, SetVolume(0));
1501 renderer_->SetVolume(0);
1503 // Playback startup shouldn't start the real sink.
1504 EXPECT_CALL(*mock_sink_, Start()).Times(0);
1509 // Play pause should all function as normal on the muted sink.
1510 EXPECT_CALL(*mock_sink_, Pause()).Times(0);
1512 EXPECT_CALL(*mock_sink_, Play()).Times(0);
1514 testing::Mock::VerifyAndClearExpectations(mock_sink_.get());
1516 // First unmute should start and play the real sink.
1517 EXPECT_CALL(*mock_sink_, SetVolume(1));
1518 EXPECT_CALL(*mock_sink_, Start());
1519 EXPECT_CALL(*mock_sink_, Play());
1520 renderer_->SetVolume(1);
1522 // Play pause should all function as normal on the normal sink.
1523 EXPECT_CALL(*mock_sink_, Pause());
1525 EXPECT_CALL(*mock_sink_, Play());
1527 testing::Mock::VerifyAndClearExpectations(mock_sink_.get());
1529 // Muting again should pause the real sink.
1530 EXPECT_CALL(*mock_sink_, SetVolume(0));
1531 EXPECT_CALL(*mock_sink_, Pause());
1532 renderer_->SetVolume(0);
1533 testing::Mock::VerifyAndClearExpectations(mock_sink_.get());
1535 // Second unmuted play shouldn't try to Start() again.
1536 EXPECT_CALL(*mock_sink_, SetVolume(0.5f));
1537 EXPECT_CALL(*mock_sink_, Play());
1538 renderer_->SetVolume(0.5f);
1539 testing::Mock::VerifyAndClearExpectations(mock_sink_.get());
1541 EXPECT_CALL(*mock_sink_, Pause());
1543 EXPECT_CALL(*mock_sink_, Stop());
1546 TEST_F(AudioRendererImplTest, SinkIsFlushed) {
1547 ConfigureWithMockSink(
1548 AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
1549 ChannelLayoutConfig::FromLayout<kChannelLayout>(),
1550 kOutputSamplesPerSecond, 1024 * 15));
1556 // Verify renderer Flush() triggers sink Flush().
1557 EXPECT_CALL(*mock_sink_, Flush());
1558 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
1559 WaitableMessageLoopEvent flush_event;
1560 renderer_->Flush(flush_event.GetClosure());
1561 flush_event.RunAndWait();
1564 TEST_F(AudioRendererImplTest, LowLatencyHint) {
1565 // Frames per buffer chosen to be small enough that we will have some room to
1566 // decrease the algorithm buffer below its default value of 200ms.
1567 int kFramesPerBuffer = 100;
1568 // Use a basic setup that avoids buffer conversion and sample rate mismatch.
1569 // This simplifies passing frames to the algorithm and verification of
1570 // frames-to-time logic.
1571 ConfigureBasicRenderer(
1572 AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
1573 ChannelLayoutConfig::FromLayout<kChannelLayout>(),
1574 kInputSamplesPerSecond, kFramesPerBuffer));
1577 // Setup renderer for playback.
1578 next_timestamp_->SetBaseTimestamp(base::TimeDelta());
1579 renderer_->SetMediaTime(base::TimeDelta());
1580 renderer_->StartPlaying();
1582 WaitForPendingRead();
1584 // With no latency hint set, the default playback threshold should equal
1585 // the buffer's total capacity.
1586 const int default_buffer_playback_threshold =
1587 buffer_playback_threshold().value;
1588 const int default_buffer_capacity = buffer_capacity().value;
1589 EXPECT_EQ(default_buffer_playback_threshold, default_buffer_capacity);
1591 // Fill the buffer to the playback threshold. Verify HAVE_ENOUGH is reached.
1592 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH,
1593 BUFFERING_CHANGE_REASON_UNKNOWN));
1594 SatisfyPendingRead(InputFrames(default_buffer_playback_threshold));
1595 EXPECT_EQ(frames_buffered().value, default_buffer_playback_threshold);
1596 base::RunLoop().RunUntilIdle(); // Let HAVE_ENOUGH post.
1597 testing::Mock::VerifyAndClearExpectations(this);
1599 // Force underflow by reading 1 frame past the buffered amount.
1600 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
1601 ConsumeBitstreamBufferedData(OutputFrames(frames_buffered().value + 1));
1602 base::RunLoop().RunUntilIdle(); // Let HAVE_NOTHING post.
1603 testing::Mock::VerifyAndClearExpectations(this);
1605 // Underflow should trigger a capacity increase *when no latency hint is set*.
1606 // Playback threshold should also increase, still matching capacity.
1607 EXPECT_GT(buffer_capacity().value, default_buffer_capacity);
1608 EXPECT_EQ(buffer_playback_threshold().value, buffer_capacity().value);
1610 // Set a *LATENCY HINT* that reduces the playback buffering threshold by half.
1611 base::TimeDelta default_buffering_latency =
1612 AudioTimestampHelper::FramesToTime(default_buffer_playback_threshold,
1613 kInputSamplesPerSecond);
1614 base::TimeDelta low_latency = default_buffering_latency / 2;
1615 renderer_->SetLatencyHint(low_latency);
1617 // Verify playback threshold now reflects the lower latency target.
1618 int low_latency_playback_threshold = buffer_playback_threshold().value;
1619 EXPECT_EQ(AudioTimestampHelper::FramesToTime(low_latency_playback_threshold,
1620 kInputSamplesPerSecond),
1623 // Verify total buffer capacity is unchanged, leaving it higher than the
1624 // playback threshold.
1625 EXPECT_EQ(buffer_capacity().value, default_buffer_capacity);
1626 EXPECT_GT(buffer_capacity().value, low_latency_playback_threshold);
1628 // Verify HAVE_ENOUGH is reached when filled to this lower threshold value.
1629 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH,
1630 BUFFERING_CHANGE_REASON_UNKNOWN));
1631 SatisfyPendingRead(InputFrames(low_latency_playback_threshold));
1632 EXPECT_EQ(frames_buffered().value, low_latency_playback_threshold);
1633 base::RunLoop().RunUntilIdle(); // Let HAVE_ENOUGH post.
1634 testing::Mock::VerifyAndClearExpectations(this);
1636 // Verify the buffer will happily continue filling, exceeding the playback
1637 // threshold, until it becomes "full";
1638 DeliverRemainingAudio();
1639 EXPECT_GE(frames_buffered().value, buffer_capacity().value);
1641 // Again force underflow by reading 1 frame past the buffered amount.
1642 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
1643 ConsumeBitstreamBufferedData(OutputFrames(frames_buffered().value + 1));
1644 base::RunLoop().RunUntilIdle(); // Let HAVE_NOTHING post.
1645 testing::Mock::VerifyAndClearExpectations(this);
1647 // With latency hint set, this underflow should NOT trigger a capacity
1648 // increase, nor a change to the playback threshold.
1649 EXPECT_EQ(buffer_capacity().value, default_buffer_capacity);
1650 EXPECT_EQ(buffer_playback_threshold().value, low_latency_playback_threshold);
1653 TEST_F(AudioRendererImplTest, HighLatencyHint) {
1654 // Frames per buffer chosen to be small enough that we will have some room to
1655 // decrease the algorithm buffer below its default value of 200ms.
1656 int kFramesPerBuffer = 100;
1657 // Use a basic setup that avoids buffer conversion and sample rate mismatch.
1658 // This simplifies passing frames to the algorithm and verification of
1659 // frames-to-time logic.
1660 ConfigureBasicRenderer(
1661 AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
1662 ChannelLayoutConfig::FromLayout<kChannelLayout>(),
1663 kInputSamplesPerSecond, kFramesPerBuffer));
1666 // Setup renderer for playback.
1667 next_timestamp_->SetBaseTimestamp(base::TimeDelta());
1668 renderer_->SetMediaTime(base::TimeDelta());
1669 renderer_->StartPlaying();
1671 WaitForPendingRead();
1673 // With no latency hint set, the default playback threshold should equal
1674 // the buffer's total capacity.
1675 const int default_buffer_playback_threshold =
1676 buffer_playback_threshold().value;
1677 const int default_buffer_capacity = buffer_capacity().value;
1678 EXPECT_EQ(default_buffer_playback_threshold, default_buffer_capacity);
1680 // Fill the buffer to the playback threshold. Verify HAVE_ENOUGH is reached.
1681 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH,
1682 BUFFERING_CHANGE_REASON_UNKNOWN));
1683 SatisfyPendingRead(InputFrames(default_buffer_playback_threshold));
1684 EXPECT_EQ(frames_buffered().value, default_buffer_playback_threshold);
1685 base::RunLoop().RunUntilIdle(); // Let HAVE_ENOUGH post.
1686 testing::Mock::VerifyAndClearExpectations(this);
1688 // Force underflow by reading 1 frame past the buffered amount.
1689 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
1690 ConsumeBitstreamBufferedData(OutputFrames(frames_buffered().value + 1));
1691 base::RunLoop().RunUntilIdle(); // Let HAVE_NOTHING post.
1692 testing::Mock::VerifyAndClearExpectations(this);
1694 // Underflow should trigger a capacity increase *when no latency hint is set*.
1695 // Playback threshold should also increase, still matching capacity.
1696 EXPECT_GT(buffer_capacity().value, default_buffer_capacity);
1697 EXPECT_EQ(buffer_playback_threshold().value, buffer_capacity().value);
1699 // Set a *LATENCY HINT* that increases the playback buffering threshold by 2x.
1700 base::TimeDelta default_buffering_latency =
1701 AudioTimestampHelper::FramesToTime(default_buffer_playback_threshold,
1702 kInputSamplesPerSecond);
1703 base::TimeDelta high_latency = default_buffering_latency * 2;
1704 renderer_->SetLatencyHint(high_latency);
1706 // Verify playback threshold now reflects the higher latency target.
1707 int high_latency_playback_threshold = buffer_playback_threshold().value;
1708 EXPECT_EQ(AudioTimestampHelper::FramesToTime(high_latency_playback_threshold,
1709 kInputSamplesPerSecond),
1712 // Verify total buffer capacity is also increased by the same amount.
1713 EXPECT_GT(buffer_capacity().value, default_buffer_capacity);
1714 EXPECT_EQ(buffer_capacity().value, high_latency_playback_threshold);
1716 // Verify HAVE_ENOUGH is reached when filled to this higher threshold value.
1717 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH,
1718 BUFFERING_CHANGE_REASON_UNKNOWN));
1719 SatisfyPendingRead(InputFrames(high_latency_playback_threshold));
1720 EXPECT_EQ(frames_buffered().value, high_latency_playback_threshold);
1721 base::RunLoop().RunUntilIdle(); // Let HAVE_ENOUGH post.
1722 testing::Mock::VerifyAndClearExpectations(this);
1724 // Verify the buffer is also considered "full" when saturated to this higher
1726 EXPECT_GE(frames_buffered().value, buffer_capacity().value);
1728 // Again force underflow by reading 1 frame past the buffered amount.
1729 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
1730 ConsumeBitstreamBufferedData(OutputFrames(frames_buffered().value + 1));
1731 base::RunLoop().RunUntilIdle(); // Let HAVE_NOTHING post.
1732 testing::Mock::VerifyAndClearExpectations(this);
1734 // With a latency hint set, this underflow should NOT trigger a capacity
1735 // increase, nor a change to the playback threshold.
1736 EXPECT_EQ(buffer_capacity().value, high_latency_playback_threshold);
1737 EXPECT_EQ(buffer_playback_threshold().value, high_latency_playback_threshold);
1740 TEST_F(AudioRendererImplTest, PlayUnmuted) {
1741 // Setting the volume to a non-zero value does not count as unmuted until the
1743 EXPECT_EQ(renderer_->was_unmuted_for_testing(), 0);
1744 renderer_->SetVolume(1);
1745 EXPECT_EQ(renderer_->was_unmuted_for_testing(), 0);
1750 EXPECT_EQ(renderer_->was_unmuted_for_testing(), 1);
1753 TEST_F(AudioRendererImplTest, UnmuteWhilePlaying) {
1754 ConfigureWithMockSink(hardware_params_);
1755 EXPECT_CALL(*mock_sink_, SetVolume(0));
1756 renderer_->SetVolume(0);
1757 EXPECT_EQ(renderer_->was_unmuted_for_testing(), 0);
1759 EXPECT_CALL(*mock_sink_, Start());
1760 EXPECT_CALL(*mock_sink_, Play());
1764 EXPECT_EQ(renderer_->was_unmuted_for_testing(), 0);
1766 EXPECT_CALL(*mock_sink_, SetVolume(1));
1767 renderer_->SetVolume(1);
1768 EXPECT_EQ(renderer_->was_unmuted_for_testing(), 1);
1770 // Muting should pause the sink.
1771 EXPECT_CALL(*mock_sink_, SetVolume(0));
1772 EXPECT_CALL(*mock_sink_, Pause());
1773 renderer_->SetVolume(0);
1774 EXPECT_EQ(renderer_->was_unmuted_for_testing(), 1);
1777 EXPECT_CALL(*mock_sink_, Stop());
1780 TEST_F(AudioRendererImplTest, DecodeAudioReadyPreemptsFlush) {
1785 EXPECT_TRUE(ConsumeBufferedDataUntilNotFull());
1786 WaitForPendingRead();
1787 EXPECT_CALL(*this, OnError(HasStatusCode(PIPELINE_ERROR_DECODE)));
1789 EXPECT_TRUE(IsDecodePending());
1791 // Imitate the behavior of a pending seek as flush_cb_;
1792 renderer_->Flush(base::BindOnce(&AudioRendererImpl::StartPlaying,
1793 base::Unretained(renderer_.get())));
1795 // This shouldn't cause a deadlock.
1796 renderer_->decoded_audio_ready_for_testing();
1799 #if !BUILDFLAG(IS_ANDROID)
1800 TEST_F(AudioRendererImplTest,
1801 TranscribeAudioCallback_SpeechRecognitionDisabled) {
1802 EXPECT_CALL(*this, SetOnReadyCallback(_));
1805 EXPECT_CALL(*this, AddAudio(testing::An<scoped_refptr<AudioBuffer>>()))
1812 TEST_F(AudioRendererImplTest,
1813 TranscribeAudioCallback_Muted_WithoutUserActivation) {
1814 EnableSpeechRecognition();
1815 EXPECT_CALL(*this, SetOnReadyCallback(_));
1818 EXPECT_CALL(*this, AddAudio(testing::An<scoped_refptr<AudioBuffer>>()))
1825 TEST_F(AudioRendererImplTest,
1826 TranscribeAudioCallback_Unmuted_WithoutUserActivation) {
1827 EnableSpeechRecognition();
1829 EXPECT_CALL(*this, SetOnReadyCallback(_));
1832 EXPECT_CALL(*this, AddAudio(testing::An<scoped_refptr<AudioBuffer>>()))
1834 next_timestamp_->SetBaseTimestamp(base::TimeDelta());
1835 renderer_->SetMediaTime(base::TimeDelta());
1836 renderer_->StartPlaying();
1837 renderer_->SetVolume(1);
1838 WaitForPendingRead();
1840 EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH,
1841 BUFFERING_CHANGE_REASON_UNKNOWN));
1842 DeliverRemainingAudio();
1845 EXPECT_EQ(renderer_->was_unmuted_for_testing(), 1);
1848 TEST_F(AudioRendererImplTest,
1849 TranscribeAudioCallback_Muted_WithUserActivation) {
1850 EnableSpeechRecognition();
1851 renderer_->SetWasPlayedWithUserActivation(true);
1853 EXPECT_CALL(*this, SetOnReadyCallback(_));
1856 EXPECT_CALL(*this, AddAudio(testing::An<scoped_refptr<AudioBuffer>>()))
1861 EXPECT_EQ(renderer_->was_unmuted_for_testing(), 1);
1865 } // namespace media