Upload upstream chromium 120.0.6099.5
[platform/framework/web/chromium-efl.git] / media / renderers / audio_renderer_impl_unittest.cc
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.
4
5 #include "media/renderers/audio_renderer_impl.h"
6
7 #include <memory>
8 #include <utility>
9 #include <vector>
10
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"
37
38 using ::base::TimeDelta;
39 using ::base::test::RunCallback;
40 using ::base::test::RunOnceCallback;
41 using ::testing::_;
42 using ::testing::DoAll;
43 using ::testing::Return;
44 using ::testing::SaveArg;
45
46 namespace media {
47
48 namespace {
49
50 // Since AudioBufferConverter is used due to different input/output sample
51 // rates, define some helper types to differentiate between the two.
52 struct InputFrames {
53   explicit InputFrames(int value) : value(value) {}
54   int value;
55 };
56
57 struct OutputFrames {
58   explicit OutputFrames(int value) : value(value) {}
59   int value;
60 };
61
62 }  // namespace
63
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;
69
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;
76
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;
83
84 ACTION_P(EnterPendingDecoderInitStateAction, test) {
85   test->EnterPendingDecoderInitState(std::move(arg2));
86 }
87
88 ACTION_P(AssertNotYetEnded, test) {
89   ASSERT_FALSE(test->ended());
90 }
91
92 class AudioRendererImplTest : public ::testing::Test,
93                               public RendererClient,
94                               public SpeechRecognitionClient {
95  public:
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_(_, _, _, _, _))
100           .WillOnce(
101               DoAll(SaveArg<3>(&output_cb_),
102                     RunOnceCallback<2>(expected_init_result_
103                                            ? DecoderStatus::Codes::kOk
104                                            : DecoderStatus::Codes::kFailed)));
105     } else {
106       EXPECT_CALL(*decoder, Initialize_(_, _, _, _, _))
107           .WillOnce(EnterPendingDecoderInitStateAction(this));
108     }
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));
115     return decoders;
116   }
117
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,
123                          512),
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),
130         ended_(false) {
131     AudioDecoderConfig audio_config(kCodec, kSampleFormat, kChannelLayout,
132                                     kInputSamplesPerSecond, EmptyExtraData(),
133                                     EncryptionScheme::kUnencrypted);
134     demuxer_stream_.set_audio_decoder_config(audio_config);
135
136     ConfigureDemuxerStream(true);
137
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));
149   }
150
151   AudioRendererImplTest(const AudioRendererImplTest&) = delete;
152   AudioRendererImplTest& operator=(const AudioRendererImplTest&) = delete;
153
154   ~AudioRendererImplTest() override {
155     SCOPED_TRACE("~AudioRendererImplTest()");
156   }
157
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));
164   }
165
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);
170       return;
171     }
172     scoped_refptr<DecoderBuffer> decoder_buffer(new DecoderBuffer(0));
173     std::move(read_cb).Run(DemuxerStream::kOk, {std::move(decoder_buffer)});
174   }
175
176   bool IsDemuxerStalled() { return !!stalled_demixer_read_cb_; }
177
178   void UnstallDemuxer() {
179     EXPECT_TRUE(IsDemuxerStalled());
180     OnDemuxerRead(stalled_demixer_read_cb_);
181   }
182
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);
194   }
195
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);
208   }
209
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);
219   }
220
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);
230   }
231
232   void EnableSpeechRecognition() { renderer_->EnableSpeechRecognition(); }
233
234   // RendererClient implementation.
235   MOCK_METHOD1(OnError, void(PipelineStatus));
236   void OnFallback(PipelineStatus status) override { NOTREACHED(); }
237   void OnEnded() override {
238     CHECK(!ended_);
239     ended_ = true;
240   }
241   void OnStatisticsUpdate(const PipelineStatistics& stats) override {
242     last_statistics_.audio_memory_usage += stats.audio_memory_usage;
243   }
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>));
255
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&));
264
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));
273   }
274
275   void Initialize() {
276     InitializeWithStatus(PIPELINE_OK);
277     next_timestamp_ =
278         std::make_unique<AudioTimestampHelper>(kInputSamplesPerSecond);
279   }
280
281   void InitializeBitstreamFormat() {
282     EXPECT_CALL(media_client_, IsSupportedBitstreamAudioCodec(_))
283         .WillRepeatedly(Return(true));
284     SetMediaClient(&media_client_);
285
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,
292                                     EmptyExtraData(),
293                                     EncryptionScheme::kUnencrypted);
294     demuxer_stream_.set_audio_decoder_config(audio_config);
295
296     ConfigureDemuxerStream(true);
297
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);
303
304     Initialize();
305   }
306
307   void InitializeWithStatus(PipelineStatus expected) {
308     SCOPED_TRACE(
309         base::StringPrintf("InitializeWithStatus(%d)", expected.code()));
310
311     WaitableMessageLoopEvent event;
312     InitializeRenderer(&demuxer_stream_, event.GetPipelineStatusCB());
313     event.RunAndWaitForStatus(expected);
314
315     // We should have no reads.
316     EXPECT_TRUE(!decode_cb_);
317   }
318
319   void InitializeAndDestroy() {
320     WaitableMessageLoopEvent event;
321     InitializeRenderer(&demuxer_stream_, event.GetPipelineStatusCB());
322
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.
326     renderer_.reset();
327     event.RunAndWaitForStatus(PIPELINE_ERROR_ABORT);
328   }
329
330   void InitializeAndDestroyDuringDecoderInit() {
331     enter_pending_decoder_init_ = true;
332
333     WaitableMessageLoopEvent event;
334     InitializeRenderer(&demuxer_stream_, event.GetPipelineStatusCB());
335     base::RunLoop().RunUntilIdle();
336     DCHECK(init_decoder_cb_);
337
338     renderer_.reset();
339     event.RunAndWaitForStatus(PIPELINE_ERROR_ABORT);
340   }
341
342   void EnterPendingDecoderInitState(AudioDecoder::InitCB cb) {
343     init_decoder_cb_ = std::move(cb);
344   }
345
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();
352
353     EXPECT_FALSE(IsDecodePending());
354   }
355
356   void Preroll() { Preroll(base::TimeDelta(), base::TimeDelta(), PIPELINE_OK); }
357
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(),
363                                     expected.code()));
364     next_timestamp_->SetBaseTimestamp(first_timestamp);
365
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();
373   }
374
375   void StartTicking() {
376     renderer_->StartTicking();
377     renderer_->SetPlaybackRate(1.0);
378   }
379
380   void StopTicking() { renderer_->StopTicking(); }
381
382   bool IsDecodePending() const { return !!decode_cb_; }
383
384   void WaitForPendingRead() {
385     SCOPED_TRACE("WaitForPendingRead()");
386     if (decode_cb_)
387       return;
388
389     DCHECK(!wait_for_pending_decode_cb_);
390
391     WaitableMessageLoopEvent event;
392     wait_for_pending_decode_cb_ = event.GetClosure();
393     event.RunAndWait();
394
395     DCHECK(decode_cb_);
396     DCHECK(!wait_for_pending_decode_cb_);
397   }
398
399   // Delivers decoded frames to |renderer_|.
400   void SatisfyPendingRead(InputFrames frames) {
401     CHECK_GT(frames.value, 0);
402     CHECK(decode_cb_);
403
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());
410     } else {
411       buffer = MakeAudioBuffer<float>(
412           kSampleFormat, kChannelLayout, kChannels, kInputSamplesPerSecond,
413           1.0f, 0.0f, frames.value, next_timestamp_->GetTimestamp());
414     }
415     next_timestamp_->AddFrames(frames.value);
416
417     DeliverBuffer(DecoderStatus::Codes::kOk, std::move(buffer));
418   }
419
420   void DeliverEndOfStream() {
421     DCHECK(decode_cb_);
422
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));
428
429     // Satify pending |decode_cb_| to trigger a new DemuxerStream::Read().
430     main_thread_task_runner_->PostTask(
431         FROM_HERE,
432         base::BindOnce(std::move(decode_cb_), DecoderStatus::Codes::kOk));
433
434     WaitForPendingRead();
435
436     main_thread_task_runner_->PostTask(
437         FROM_HERE,
438         base::BindOnce(std::move(decode_cb_), DecoderStatus::Codes::kOk));
439
440     base::RunLoop().RunUntilIdle();
441     EXPECT_EQ(last_statistics_.audio_memory_usage,
442               renderer_->algorithm_->GetMemoryUsage());
443   }
444
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));
455     }
456   }
457
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);
466
467     int overfill = buffered > capacity ? buffered - capacity : 0;
468     int quarter_buffer = capacity / 4;
469
470     // Leaves the buffer 3/4 full.
471     return ConsumeBufferedData(OutputFrames(overfill + quarter_buffer));
472   }
473
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
476   // to be consumed.
477   bool ConsumeBufferedData(OutputFrames requested_frames,
478                            base::TimeDelta delay) {
479     std::unique_ptr<AudioBus> bus =
480         AudioBus::Create(kChannels, requested_frames.value);
481     int frames_read = 0;
482     EXPECT_TRUE(sink_->Render(bus.get(), delay, &frames_read));
483     return frames_read == requested_frames.value;
484   }
485
486   bool ConsumeBufferedData(OutputFrames requested_frames) {
487     return ConsumeBufferedData(requested_frames, base::TimeDelta());
488   }
489
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) {
496       int frames_read = 0;
497       EXPECT_TRUE(sink_->Render(bus.get(), delay, &frames_read));
498
499       if (frames_read <= 0)
500         break;
501       total_frames_read += frames_read;
502     }
503
504     return total_frames_read == requested_frames.value;
505   }
506
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];
513   }
514
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];
520   }
521
522   OutputFrames frames_buffered() {
523     return OutputFrames(renderer_->algorithm_->BufferedFrames());
524   }
525
526   OutputFrames buffer_playback_threshold() {
527     return OutputFrames(renderer_->algorithm_->QueuePlaybackThreshold());
528   }
529
530   OutputFrames buffer_capacity() {
531     return OutputFrames(renderer_->algorithm_->QueueCapacity());
532   }
533
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);
539     }
540     return OutputFrames(buffer_capacity().value - frames_buffered().value);
541   }
542
543   bool is_buffer_full() { return renderer_->algorithm_->IsQueueFull(); }
544
545   void force_config_change(const AudioDecoderConfig& config) {
546     renderer_->OnConfigChange(config);
547   }
548
549   InputFrames converter_input_frames_left() const {
550     return InputFrames(
551         renderer_->buffer_converter_->input_frames_left_for_testing());
552   }
553
554   base::TimeDelta CurrentMediaTime() { return renderer_->CurrentMediaTime(); }
555
556   std::vector<bool> channel_mask() const {
557     CHECK(renderer_->algorithm_);
558     return renderer_->algorithm_->channel_mask_for_testing();
559   }
560
561   bool ended() const { return ended_; }
562
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(
568           FROM_HERE,
569           base::BindOnce(&AudioRendererImplTest::DecodeDecoder,
570                          base::Unretained(this), buffer, std::move(decode_cb)));
571       return;
572     }
573
574     CHECK(!decode_cb_) << "Overlapping decodes are not permitted";
575     decode_cb_ = std::move(decode_cb);
576
577     // Wake up WaitForPendingRead() if needed.
578     if (wait_for_pending_decode_cb_)
579       std::move(wait_for_pending_decode_cb_).Run();
580   }
581
582   void ResetDecoder(base::OnceClosure& reset_cb) {
583     if (decode_cb_) {
584       // |reset_cb| will be called in DeliverBuffer(), after the decoder is
585       // flushed.
586       reset_cb_ = std::move(reset_cb);
587       return;
588     }
589
590     main_thread_task_runner_->PostTask(FROM_HERE, std::move(reset_cb));
591   }
592
593   void DeliverBuffer(DecoderStatus status, scoped_refptr<AudioBuffer> buffer) {
594     CHECK(decode_cb_);
595
596     if (buffer.get() && !buffer->end_of_stream())
597       output_cb_.Run(std::move(buffer));
598     std::move(decode_cb_).Run(status);
599
600     if (reset_cb_)
601       std::move(reset_cb_).Run();
602
603     base::RunLoop().RunUntilIdle();
604   }
605
606   // Fixture members.
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_;
617
618   MockDemuxerStream demuxer_stream_;
619   MockMediaClient media_client_;
620
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_;
625
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_;
631
632   // Run during DecodeDecoder() to unblock WaitForPendingRead().
633   base::OnceClosure wait_for_pending_decode_cb_;
634
635   AudioDecoder::InitCB init_decoder_cb_;
636   bool expected_init_result_;
637   bool enter_pending_decoder_init_;
638   bool ended_;
639 };
640
641 TEST_F(AudioRendererImplTest, Initialize_Successful) {
642   Initialize();
643 }
644
645 TEST_F(AudioRendererImplTest, Initialize_DecoderInitFailure) {
646   expected_init_result_ = false;
647   InitializeWithStatus(DECODER_ERROR_NOT_SUPPORTED);
648 }
649
650 TEST_F(AudioRendererImplTest, ReinitializeForDifferentStream) {
651   // Initialize and start playback
652   Initialize();
653   Preroll();
654   StartTicking();
655   EXPECT_TRUE(ConsumeBufferedDataUntilNotFull());
656   WaitForPendingRead();
657
658   // Stop playback and flush
659   StopTicking();
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();
664
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);
672
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);
677 }
678
679 TEST_F(AudioRendererImplTest, SignalConfigChange) {
680   // Initialize and start playback.
681   Initialize();
682   Preroll();
683   StartTicking();
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.
688
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);
697
698   // Verify rendering can continue after config change.
699   EXPECT_TRUE(ConsumeBufferedData(OutputFrames(256)));
700
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);
707 }
708
709 TEST_F(AudioRendererImplTest, Preroll) {
710   Initialize();
711   Preroll();
712 }
713
714 TEST_F(AudioRendererImplTest, StartTicking) {
715   Initialize();
716   Preroll();
717   StartTicking();
718
719   // Drain internal buffer, we should have a pending read.
720   EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
721   WaitForPendingRead();
722 }
723
724 TEST_F(AudioRendererImplTest, EndOfStream) {
725   Initialize();
726   Preroll();
727   StartTicking();
728
729   // Drain internal buffer, we should have a pending read.
730   EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
731   WaitForPendingRead();
732
733   // Forcefully trigger underflow.
734   EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
735   EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
736
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();
742
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());
747
748   // Ended should trigger on next render call.
749   EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
750   base::RunLoop().RunUntilIdle();
751   EXPECT_TRUE(ended());
752 }
753
754 TEST_F(AudioRendererImplTest, DecoderUnderflow) {
755   Initialize();
756   Preroll();
757   StartTicking();
758
759   // Drain internal buffer, we should have a pending read.
760   EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
761   WaitForPendingRead();
762
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
765   // pending read.
766   EXPECT_CALL(
767       *this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, DECODER_UNDERFLOW));
768   EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
769
770   // Verify we're still not getting audio data.
771   EXPECT_EQ(0, frames_buffered().value);
772   EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
773
774   // Deliver enough data to have enough for buffering.
775   EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH,
776                                             BUFFERING_CHANGE_REASON_UNKNOWN));
777   DeliverRemainingAudio();
778
779   // Verify we're getting audio data.
780   EXPECT_TRUE(ConsumeBufferedData(OutputFrames(1)));
781 }
782
783 TEST_F(AudioRendererImplTest, DemuxerUnderflow) {
784   Initialize();
785   Preroll();
786   StartTicking();
787
788   // Drain internal buffer, we should have a pending read.
789   EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
790   WaitForPendingRead();
791
792   // Verify the next FillBuffer() call triggers a buffering state change
793   // update. Expect a demuxer underflow flag because demuxer is blocked on a
794   // pending read.
795   EXPECT_CALL(
796       *this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, DEMUXER_UNDERFLOW));
797   EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
798
799   // Verify we're still not getting audio data.
800   EXPECT_EQ(0, frames_buffered().value);
801   EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
802
803   // Deliver enough data to have enough for buffering.
804   EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH,
805                                             BUFFERING_CHANGE_REASON_UNKNOWN));
806
807   // Stall the demuxer to trigger underflow.
808   simulate_demuxer_stall_ = true;
809   SatisfyPendingRead(InputFrames(kInputFramesChunk));
810   UnstallDemuxer();
811
812   DeliverRemainingAudio();
813
814   // Verify we're getting audio data.
815   EXPECT_TRUE(ConsumeBufferedData(OutputFrames(1)));
816 }
817
818 TEST_F(AudioRendererImplTest, Underflow_CapacityResetsAfterFlush) {
819   Initialize();
820   Preroll();
821   StartTicking();
822
823   // Drain internal buffer, we should have a pending read.
824   EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
825   WaitForPendingRead();
826
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)));
832
833   // Verify that the buffer capacity increased as a result of underflowing.
834   EXPECT_GT(buffer_capacity().value, initial_capacity.value);
835
836   // Verify that the buffer capacity is restored to the |initial_capacity|.
837   StopTicking();
838   FlushDuringPendingRead();
839   EXPECT_EQ(buffer_capacity().value, initial_capacity.value);
840 }
841
842 TEST_F(AudioRendererImplTest, Underflow_CapacityIncreasesBeforeHaveNothing) {
843   Initialize();
844   Preroll();
845   StartTicking();
846
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();
850
851   // Drain internal buffer.
852   EXPECT_FALSE(ConsumeBufferedData(OutputFrames(frames_buffered().value + 1)));
853
854   // Verify that the buffer capacity increased despite not sending have nothing.
855   EXPECT_GT(buffer_capacity().value, initial_capacity.value);
856 }
857
858 TEST_F(AudioRendererImplTest, Underflow_OneCapacityIncreasePerUnderflow) {
859   Initialize();
860   Preroll();
861   StartTicking();
862
863   OutputFrames prev_capacity = buffer_capacity();
864
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)));
868
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);
876
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, _))
882       .Times(0);
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);
888
889   // Fill the buffer back up.
890   WaitForPendingRead();
891   DeliverRemainingAudio();
892   EXPECT_GT(frames_buffered().value, 0);
893
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, _))
897       .Times(0);
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);
903
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);
912 }
913
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,
924                       1024),
925       hw_params);
926   Initialize();
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) {
931     if (ch > 1)
932       ASSERT_FALSE(mask[ch]);
933     else
934       ASSERT_TRUE(mask[ch]);
935   }
936
937   renderer_->SetMediaTime(base::TimeDelta());
938   renderer_->StartPlaying();
939   WaitForPendingRead();
940
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));
946
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]);
953 }
954
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;
959
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);
966
967   // Fake an attached webaudio client.
968   sink_->SetIsOptimizedForHardwareParameters(false);
969
970   Initialize();
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]);
976 }
977
978 TEST_F(AudioRendererImplTest, Underflow_Flush) {
979   Initialize();
980   Preroll();
981   StartTicking();
982
983   // Force underflow.
984   EXPECT_TRUE(ConsumeBufferedData(frames_buffered()));
985   WaitForPendingRead();
986   EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
987   EXPECT_FALSE(ConsumeBufferedData(OutputFrames(1)));
988   WaitForPendingRead();
989   StopTicking();
990
991   // We shouldn't expect another buffering state change when flushing.
992   FlushDuringPendingRead();
993 }
994
995 TEST_F(AudioRendererImplTest, PendingRead_Flush) {
996   Initialize();
997
998   Preroll();
999   StartTicking();
1000
1001   // Partially drain internal buffer so we get a pending read.
1002   EXPECT_TRUE(ConsumeBufferedDataUntilNotFull());
1003   WaitForPendingRead();
1004
1005   StopTicking();
1006
1007   EXPECT_TRUE(IsDecodePending());
1008
1009   // Flush and expect to be notified that we have nothing.
1010   EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
1011   FlushDuringPendingRead();
1012
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);
1016 }
1017
1018 TEST_F(AudioRendererImplTest, PendingRead_Destroy) {
1019   Initialize();
1020
1021   Preroll();
1022   StartTicking();
1023
1024   // Partially drain internal buffer so we get a pending read.
1025   EXPECT_TRUE(ConsumeBufferedDataUntilNotFull());
1026   WaitForPendingRead();
1027
1028   StopTicking();
1029
1030   EXPECT_TRUE(IsDecodePending());
1031
1032   renderer_.reset();
1033 }
1034
1035 TEST_F(AudioRendererImplTest, PendingFlush_Destroy) {
1036   Initialize();
1037
1038   Preroll();
1039   StartTicking();
1040
1041   // Partially drain internal buffer so we get a pending read.
1042   EXPECT_TRUE(ConsumeBufferedDataUntilNotFull());
1043   WaitForPendingRead();
1044
1045   StopTicking();
1046
1047   EXPECT_TRUE(IsDecodePending());
1048
1049   // Start flushing.
1050   WaitableMessageLoopEvent flush_event;
1051   renderer_->Flush(flush_event.GetClosure());
1052
1053   EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_NOTHING, _));
1054   SatisfyPendingRead(InputFrames(kInputFramesChunk));
1055
1056   renderer_.reset();
1057 }
1058
1059 TEST_F(AudioRendererImplTest, InitializeThenDestroy) {
1060   InitializeAndDestroy();
1061 }
1062
1063 TEST_F(AudioRendererImplTest, InitializeThenDestroyDuringDecoderInit) {
1064   InitializeAndDestroyDuringDecoderInit();
1065 }
1066
1067 TEST_F(AudioRendererImplTest, CurrentMediaTimeBehavior) {
1068   Initialize();
1069   Preroll();
1070   StartTicking();
1071
1072   AudioTimestampHelper timestamp_helper(kOutputSamplesPerSecond);
1073   timestamp_helper.SetBaseTimestamp(base::TimeDelta());
1074
1075   // Time should be the starting timestamp as nothing has been consumed yet.
1076   EXPECT_EQ(timestamp_helper.GetTimestamp(), CurrentMediaTime());
1077
1078   const OutputFrames frames_to_consume(frames_buffered().value / 3);
1079   const base::TimeDelta kConsumptionDuration =
1080       timestamp_helper.GetFrameDuration(frames_to_consume.value);
1081
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());
1086
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.
1091
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());
1095
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());
1100
1101   // Consume some more audio data.
1102   EXPECT_TRUE(ConsumeBufferedData(frames_to_consume));
1103
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());
1107
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());
1113
1114   // Consume some more audio data.
1115   EXPECT_TRUE(ConsumeBufferedData(frames_to_consume));
1116
1117   // Stop ticking, the media time should be clamped to what's been rendered.
1118   StopTicking();
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());
1123 }
1124
1125 TEST_F(AudioRendererImplTest, RenderingDelayedForEarlyStartTime) {
1126   Initialize();
1127
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());
1134
1135   Preroll(base::TimeDelta(), first_timestamp, PIPELINE_OK);
1136   StartTicking();
1137
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]);
1146
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();
1152     }
1153   }
1154
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));
1160
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]);
1165 }
1166
1167 TEST_F(AudioRendererImplTest, RenderingDelayedForSuspend) {
1168   Initialize();
1169   Preroll(base::TimeDelta(), base::TimeDelta(), PIPELINE_OK);
1170   StartTicking();
1171
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]);
1179
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);
1184
1185   // Verify after resume we get audio.
1186   bus->Zero();
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]);
1192 }
1193
1194 TEST_F(AudioRendererImplTest, AbsurdRenderingDelayLog) {
1195   ConfigureWithMockMediaLog();
1196   Initialize();
1197   Preroll(base::TimeDelta(), base::TimeDelta(), PIPELINE_OK);
1198   StartTicking();
1199
1200   // Verify the first buffer is real data.
1201   int frames_read = 0;
1202   std::unique_ptr<AudioBus> bus = AudioBus::Create(hardware_params_);
1203
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));
1207 }
1208
1209 TEST_F(AudioRendererImplTest, RenderingDelayDoesNotOverflow) {
1210   Initialize();
1211
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);
1215   StartTicking();
1216   EXPECT_TRUE(ConsumeBufferedData(OutputFrames(1)));
1217 }
1218
1219 TEST_F(AudioRendererImplTest, ImmediateEndOfStream) {
1220   Initialize();
1221
1222   renderer_->SetMediaTime(base::TimeDelta());
1223   renderer_->StartPlaying();
1224   WaitForPendingRead();
1225
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();
1231
1232   EXPECT_TRUE(ended());
1233 }
1234
1235 TEST_F(AudioRendererImplTest, OnRenderErrorCausesDecodeError) {
1236   Initialize();
1237   Preroll();
1238   StartTicking();
1239
1240   EXPECT_CALL(*this, OnError(HasStatusCode(AUDIO_RENDERER_ERROR)));
1241   sink_->OnRenderError();
1242   base::RunLoop().RunUntilIdle();
1243 }
1244
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) {
1248   Initialize();
1249   Preroll();
1250
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());
1257
1258   // Rendering has started with non-zero rate. Rate changes will affect sink
1259   // state.
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());
1266
1267   // Rendering has stopped. Sink should be paused.
1268   renderer_->StopTicking();
1269   EXPECT_EQ(FakeAudioRendererSink::kPaused, sink_->state());
1270
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());
1278 }
1279
1280 TEST_F(AudioRendererImplTest, TimeSourceBehavior) {
1281   Initialize();
1282   Preroll();
1283
1284   AudioTimestampHelper timestamp_helper(kOutputSamplesPerSecond);
1285   timestamp_helper.SetBaseTimestamp(base::TimeDelta());
1286
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);
1292
1293   EXPECT_EQ(base::TimeTicks(), CurrentMediaWallClockTime(&is_time_moving));
1294   EXPECT_FALSE(is_time_moving);
1295
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);
1299   StartTicking();
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);
1306
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();
1311
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);
1317
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);
1327
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));
1332
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);
1340
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();
1344
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;
1350
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);
1356   }
1357
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);
1362
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);
1370
1371   StopTicking();
1372   DeliverRemainingAudio();
1373
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);
1377   StartTicking();
1378
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);
1382
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));
1387
1388   frames_to_consume.value = frames_buffered().value / 16;
1389   EXPECT_TRUE(ConsumeBufferedData(frames_to_consume, delay_time));
1390
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);
1398
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);
1406
1407   // Advance far enough that we shouldn't be clamped to current time (tested
1408   // already above).
1409   tick_clock_.Advance(kOneSecond);
1410   EXPECT_EQ(
1411       current_time + timestamp_helper.GetFrameDuration(frames_to_consume.value),
1412       CurrentMediaWallClockTime(&is_time_moving));
1413   EXPECT_TRUE(is_time_moving);
1414 }
1415
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;
1421
1422   InitializeBitstreamFormat();
1423   Preroll();
1424   StartTicking();
1425
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();
1431
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();
1437
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());
1443
1444   // Ended should trigger on next render call.
1445   EXPECT_FALSE(ConsumeBitstreamBufferedData(OutputFrames(1)));
1446   base::RunLoop().RunUntilIdle();
1447   EXPECT_TRUE(ended());
1448
1449   // Clear the use of |media_client_|, which was set in
1450   // InitializeBitstreamFormat().
1451   SetMediaClient(nullptr);
1452 }
1453
1454 TEST_F(AudioRendererImplTest, MutedPlaybackBadDeviceInfo) {
1455   base::test::ScopedFeatureList scoped_feature_list_;
1456   scoped_feature_list_.InitAndEnableFeature(kSuspendMutedAudio);
1457
1458   mock_sink_ = base::MakeRefCounted<MockAudioRendererSink>(
1459       std::string(), OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED,
1460       AudioParameters());
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);
1468
1469   EXPECT_CALL(*mock_sink_, SetVolume(0)).Times(0);
1470   renderer_->SetVolume(0);
1471
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);
1475   Initialize();
1476   Preroll();
1477   StartTicking();
1478
1479   EXPECT_CALL(*mock_sink_, Pause()).Times(0);
1480   StopTicking();
1481   EXPECT_CALL(*mock_sink_, Play()).Times(0);
1482   StartTicking();
1483   testing::Mock::VerifyAndClearExpectations(mock_sink_.get());
1484
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);
1489
1490   EXPECT_CALL(*mock_sink_, Pause()).Times(0);
1491   StopTicking();
1492   EXPECT_CALL(*mock_sink_, Stop()).Times(0);
1493 }
1494
1495 TEST_F(AudioRendererImplTest, BasicMutedPlayback) {
1496   base::test::ScopedFeatureList scoped_feature_list_;
1497   scoped_feature_list_.InitAndEnableFeature(kSuspendMutedAudio);
1498   ConfigureWithMockSink(hardware_params_);
1499
1500   EXPECT_CALL(*mock_sink_, SetVolume(0));
1501   renderer_->SetVolume(0);
1502
1503   // Playback startup shouldn't start the real sink.
1504   EXPECT_CALL(*mock_sink_, Start()).Times(0);
1505   Initialize();
1506   Preroll();
1507   StartTicking();
1508
1509   // Play pause should all function as normal on the muted sink.
1510   EXPECT_CALL(*mock_sink_, Pause()).Times(0);
1511   StopTicking();
1512   EXPECT_CALL(*mock_sink_, Play()).Times(0);
1513   StartTicking();
1514   testing::Mock::VerifyAndClearExpectations(mock_sink_.get());
1515
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);
1521
1522   // Play pause should all function as normal on the normal sink.
1523   EXPECT_CALL(*mock_sink_, Pause());
1524   StopTicking();
1525   EXPECT_CALL(*mock_sink_, Play());
1526   StartTicking();
1527   testing::Mock::VerifyAndClearExpectations(mock_sink_.get());
1528
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());
1534
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());
1540
1541   EXPECT_CALL(*mock_sink_, Pause());
1542   StopTicking();
1543   EXPECT_CALL(*mock_sink_, Stop());
1544 }
1545
1546 TEST_F(AudioRendererImplTest, SinkIsFlushed) {
1547   ConfigureWithMockSink(
1548       AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
1549                       ChannelLayoutConfig::FromLayout<kChannelLayout>(),
1550                       kOutputSamplesPerSecond, 1024 * 15));
1551   Initialize();
1552   Preroll();
1553   StartTicking();
1554   StopTicking();
1555
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();
1562 }
1563
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));
1575   Initialize();
1576
1577   // Setup renderer for playback.
1578   next_timestamp_->SetBaseTimestamp(base::TimeDelta());
1579   renderer_->SetMediaTime(base::TimeDelta());
1580   renderer_->StartPlaying();
1581   StartTicking();
1582   WaitForPendingRead();
1583
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);
1590
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);
1598
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);
1604
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);
1609
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);
1616
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),
1621             low_latency);
1622
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);
1627
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);
1635
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);
1640
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);
1646
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);
1651 }
1652
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));
1664   Initialize();
1665
1666   // Setup renderer for playback.
1667   next_timestamp_->SetBaseTimestamp(base::TimeDelta());
1668   renderer_->SetMediaTime(base::TimeDelta());
1669   renderer_->StartPlaying();
1670   StartTicking();
1671   WaitForPendingRead();
1672
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);
1679
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);
1687
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);
1693
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);
1698
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);
1705
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),
1710             high_latency);
1711
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);
1715
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);
1723
1724   // Verify the buffer is also considered "full" when saturated to this higher
1725   // threshold.
1726   EXPECT_GE(frames_buffered().value, buffer_capacity().value);
1727
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);
1733
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);
1738 }
1739
1740 TEST_F(AudioRendererImplTest, PlayUnmuted) {
1741   // Setting the volume to a non-zero value does not count as unmuted until the
1742   // audio is played.
1743   EXPECT_EQ(renderer_->was_unmuted_for_testing(), 0);
1744   renderer_->SetVolume(1);
1745   EXPECT_EQ(renderer_->was_unmuted_for_testing(), 0);
1746
1747   Initialize();
1748   Preroll();
1749   StartTicking();
1750   EXPECT_EQ(renderer_->was_unmuted_for_testing(), 1);
1751 }
1752
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);
1758
1759   EXPECT_CALL(*mock_sink_, Start());
1760   EXPECT_CALL(*mock_sink_, Play());
1761   Initialize();
1762   Preroll();
1763   StartTicking();
1764   EXPECT_EQ(renderer_->was_unmuted_for_testing(), 0);
1765
1766   EXPECT_CALL(*mock_sink_, SetVolume(1));
1767   renderer_->SetVolume(1);
1768   EXPECT_EQ(renderer_->was_unmuted_for_testing(), 1);
1769
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);
1775
1776   StopTicking();
1777   EXPECT_CALL(*mock_sink_, Stop());
1778 }
1779
1780 TEST_F(AudioRendererImplTest, DecodeAudioReadyPreemptsFlush) {
1781   Initialize();
1782
1783   Preroll();
1784   StartTicking();
1785   EXPECT_TRUE(ConsumeBufferedDataUntilNotFull());
1786   WaitForPendingRead();
1787   EXPECT_CALL(*this, OnError(HasStatusCode(PIPELINE_ERROR_DECODE)));
1788   StopTicking();
1789   EXPECT_TRUE(IsDecodePending());
1790
1791   // Imitate the behavior of a pending seek as flush_cb_;
1792   renderer_->Flush(base::BindOnce(&AudioRendererImpl::StartPlaying,
1793                                   base::Unretained(renderer_.get())));
1794
1795   // This shouldn't cause a deadlock.
1796   renderer_->decoded_audio_ready_for_testing();
1797 }
1798
1799 #if !BUILDFLAG(IS_ANDROID)
1800 TEST_F(AudioRendererImplTest,
1801        TranscribeAudioCallback_SpeechRecognitionDisabled) {
1802   EXPECT_CALL(*this, SetOnReadyCallback(_));
1803   Initialize();
1804
1805   EXPECT_CALL(*this, AddAudio(testing::An<scoped_refptr<AudioBuffer>>()))
1806       .Times(0);
1807   Preroll();
1808
1809   StartTicking();
1810 }
1811
1812 TEST_F(AudioRendererImplTest,
1813        TranscribeAudioCallback_Muted_WithoutUserActivation) {
1814   EnableSpeechRecognition();
1815   EXPECT_CALL(*this, SetOnReadyCallback(_));
1816   Initialize();
1817
1818   EXPECT_CALL(*this, AddAudio(testing::An<scoped_refptr<AudioBuffer>>()))
1819       .Times(0);
1820   Preroll();
1821
1822   StartTicking();
1823 }
1824
1825 TEST_F(AudioRendererImplTest,
1826        TranscribeAudioCallback_Unmuted_WithoutUserActivation) {
1827   EnableSpeechRecognition();
1828
1829   EXPECT_CALL(*this, SetOnReadyCallback(_));
1830   Initialize();
1831
1832   EXPECT_CALL(*this, AddAudio(testing::An<scoped_refptr<AudioBuffer>>()))
1833       .Times(3);
1834   next_timestamp_->SetBaseTimestamp(base::TimeDelta());
1835   renderer_->SetMediaTime(base::TimeDelta());
1836   renderer_->StartPlaying();
1837   renderer_->SetVolume(1);
1838   WaitForPendingRead();
1839
1840   EXPECT_CALL(*this, OnBufferingStateChange(BUFFERING_HAVE_ENOUGH,
1841                                             BUFFERING_CHANGE_REASON_UNKNOWN));
1842   DeliverRemainingAudio();
1843   StartTicking();
1844
1845   EXPECT_EQ(renderer_->was_unmuted_for_testing(), 1);
1846 }
1847
1848 TEST_F(AudioRendererImplTest,
1849        TranscribeAudioCallback_Muted_WithUserActivation) {
1850   EnableSpeechRecognition();
1851   renderer_->SetWasPlayedWithUserActivation(true);
1852
1853   EXPECT_CALL(*this, SetOnReadyCallback(_));
1854   Initialize();
1855
1856   EXPECT_CALL(*this, AddAudio(testing::An<scoped_refptr<AudioBuffer>>()))
1857       .Times(3);
1858   Preroll();
1859
1860   StartTicking();
1861   EXPECT_EQ(renderer_->was_unmuted_for_testing(), 1);
1862 }
1863 #endif
1864
1865 }  // namespace media