1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROMECAST_MEDIA_CMA_BACKEND_AUDIO_DECODER_FOR_MIXER_H_
6 #define CHROMECAST_MEDIA_CMA_BACKEND_AUDIO_DECODER_FOR_MIXER_H_
7 
8 #include <cstdint>
9 #include <memory>
10 
11 #include "base/bind.h"
12 #include "base/location.h"
13 #include "chromecast/media/api/cast_audio_decoder.h"
14 #include "chromecast/media/audio/mixer_service/output_stream_connection.h"
15 #include "chromecast/public/media/decoder_config.h"
16 #include "chromecast/public/media/media_pipeline_backend.h"
17 #include "chromecast/public/media/media_pipeline_device_params.h"
18 #include "media/base/audio_buffer.h"
19 
20 namespace base {
21 class SingleThreadTaskRunner;
22 }  // namespace base
23 
24 namespace chromecast {
25 class IOBufferPool;
26 
27 namespace media {
28 class DecoderBufferBase;
29 class MediaPipelineBackendForMixer;
30 
31 // AudioDecoder implementation that streams decoded stream to the StreamMixer.
32 class AudioDecoderForMixer
33     : public MediaPipelineBackend::AudioDecoder,
34       public mixer_service::OutputStreamConnection::Delegate {
35  public:
36   using BufferStatus = MediaPipelineBackend::BufferStatus;
37 
38   explicit AudioDecoderForMixer(MediaPipelineBackendForMixer* backend);
39   ~AudioDecoderForMixer() override;
40 
41   virtual void Initialize();
42   virtual bool Start(int64_t pts, bool start_playback_asap);
43   void StartPlaybackAt(int64_t timestamp);
44   virtual void Stop();
45   virtual bool Pause();
46   virtual bool Resume();
47   virtual float SetPlaybackRate(float rate);
48   virtual bool GetTimestampedPts(int64_t* timestamp, int64_t* pts) const;
49   virtual int64_t GetCurrentPts() const;
50 
51   // MediaPipelineBackend::AudioDecoder implementation:
52   void SetDelegate(MediaPipelineBackend::Decoder::Delegate* delegate) override;
53   BufferStatus PushBuffer(CastDecoderBuffer* buffer) override;
54   void GetStatistics(Statistics* statistics) override;
55   bool SetConfig(const AudioConfig& config) override;
56   bool SetVolume(float multiplier) override;
57   RenderingDelay GetRenderingDelay() override;
58 
59   // This allows for very small changes in the rate of audio playback that are
60   // (supposedly) imperceptible.
61   double SetAvSyncPlaybackRate(double rate);
62   void RestartPlaybackAt(int64_t pts, int64_t timestamp);
63 
64   RenderingDelay GetMixerRenderingDelay();
65 
66  private:
67   friend class MockAudioDecoderForMixer;
68   friend class AvSyncTest;
69 
70   // mixer_service::OutputStreamConnection::Delegate implementation:
71   void FillNextBuffer(void* buffer,
72                       int frames,
73                       int64_t playout_timestamp) override;
74   void OnAudioReadyForPlayback(int64_t mixer_delay) override;
75   void OnEosPlayed() override;
76   void OnMixerError() override;
77 
78   void CreateBufferPool(const AudioConfig& config, int frame_count);
79   void CreateMixerInput(const AudioConfig& config, bool start_playback_asap);
80   void CleanUpPcm();
81   void ResetMixerInputForNewConfig(const AudioConfig& config);
82   void CreateDecoder();
83 
84   void OnBufferDecoded(uint64_t input_bytes,
85                        bool has_config,
86                        CastAudioDecoder::Status status,
87                        const AudioConfig& config,
88                        scoped_refptr<DecoderBufferBase> decoded);
89   void CheckBufferComplete();
90   void WritePcm(scoped_refptr<DecoderBufferBase> buffer);
91   bool BypassDecoder() const;
92   void UpdateStatistics(Statistics delta);
93 
94   MediaPipelineBackendForMixer* const backend_;
95   const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
96   MediaPipelineBackend::Decoder::Delegate* delegate_ = nullptr;
97 
98   Statistics stats_;
99 
100   int buffer_pool_frames_ = 0;
101   bool pending_buffer_complete_ = false;
102   bool mixer_error_ = false;
103   bool paused_ = false;
104   float playback_rate_ = 1.0f;
105   bool reported_ready_for_playback_ = false;
106   RenderingDelay mixer_delay_;
107 
108   AudioConfig input_config_;
109   AudioConfig decoded_config_;
110   std::unique_ptr<CastAudioDecoder> decoder_;
111 
112   double av_sync_clock_rate_ = 1.0;
113 
114   std::unique_ptr<mixer_service::OutputStreamConnection> mixer_input_;
115 
116   RenderingDelay next_buffer_delay_;
117   int64_t pending_output_frames_ = -1;
118   float volume_multiplier_ = 1.0f;
119 
120   int64_t last_push_pts_ = INT64_MIN;
121   int64_t last_push_playout_timestamp_ = INT64_MIN;
122 
123   scoped_refptr<::media::AudioBufferMemoryPool> pool_;
124   scoped_refptr<IOBufferPool> buffer_pool_;
125 
126   int64_t playback_start_pts_ = 0;
127   bool start_playback_asap_ = false;
128 
129   base::WeakPtrFactory<AudioDecoderForMixer> weak_factory_;
130 
131   DISALLOW_COPY_AND_ASSIGN(AudioDecoderForMixer);
132 };
133 
134 }  // namespace media
135 }  // namespace chromecast
136 
137 #endif  // CHROMECAST_MEDIA_CMA_BACKEND_AUDIO_DECODER_FOR_MIXER_H_
138