1 // Copyright 2018 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_MIXER_MIXER_INPUT_H_
6 #define CHROMECAST_MEDIA_CMA_BACKEND_MIXER_MIXER_INPUT_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/macros.h"
13 #include "base/sequence_checker.h"
14 #include "chromecast/media/base/slew_volume.h"
15 #include "chromecast/public/media/media_pipeline_backend.h"
16 #include "chromecast/public/volume_control.h"
17 #include "media/base/channel_layout.h"
18 
19 namespace media {
20 class AudioBus;
21 class ChannelMixer;
22 class MultiChannelResampler;
23 }  // namespace media
24 
25 namespace chromecast {
26 namespace media {
27 
28 class AudioOutputRedirectorInput;
29 class FilterGroup;
30 
31 // Input stream to the mixer. Handles pulling data from the data source and
32 // resampling it to the mixer's output sample rate, as well as volume control.
33 // All methods must be called on the mixer thread.
34 class MixerInput {
35  public:
36   using RenderingDelay = MediaPipelineBackend::AudioDecoder::RenderingDelay;
37 
38   // Data source for the mixer. All methods are called on the mixer thread and
39   // must return promptly to avoid audio underruns. The source must remain valid
40   // until FinalizeAudioPlayback() is called.
41   class Source {
42    public:
43     enum class MixerError {
44       // This input is being ignored due to a sample rate change.
45       kInputIgnored,
46       // An internal mixer error occurred. The input is no longer usable.
47       kInternalError,
48     };
49 
50     virtual size_t num_channels() const = 0;
51     virtual ::media::ChannelLayout channel_layout() const = 0;
52     virtual int sample_rate() const = 0;
53     virtual bool primary() = 0;
54     virtual const std::string& device_id() = 0;
55     virtual AudioContentType content_type() = 0;
56     virtual AudioContentType focus_type() = 0;
57     virtual int desired_read_size() = 0;
58     virtual int playout_channel() = 0;
59     // Returns true if the source is currently providing audio to be mixed.
60     virtual bool active() = 0;
61 
62     // Called when the input has been added to the mixer, before any other
63     // calls are made. The |read_size| is the number of frames that will be
64     // requested for each call to FillAudioPlaybackFrames(). The
65     // |initial_rendering_delay| is the rendering delay estimate for the first
66     // call to FillAudioPlaybackFrames().
67     virtual void InitializeAudioPlayback(
68         int read_size,
69         RenderingDelay initial_rendering_delay) = 0;
70 
71     // Called to read more audio data from the source. The source must fill in
72     // |buffer| with up to |num_frames| of audio. The |rendering_delay|
73     // indicates when the first frame of the filled data will be played out.
74     // Returns the number of frames filled into |buffer|.
75     virtual int FillAudioPlaybackFrames(int num_frames,
76                                         RenderingDelay rendering_delay,
77                                         ::media::AudioBus* buffer) = 0;
78 
79     // Called when a mixer error occurs. No more data will be pulled from the
80     // source.
81     virtual void OnAudioPlaybackError(MixerError error) = 0;
82 
83     // Called when an underrun error occurs on mixer output.
OnOutputUnderrun()84     virtual void OnOutputUnderrun() {}
85 
86     // Called when the mixer has finished removing this input. The source may be
87     // deleted at this point.
88     virtual void FinalizeAudioPlayback() = 0;
89 
90    protected:
91     virtual ~Source() = default;
92   };
93 
94   MixerInput(Source* source, FilterGroup* filter_group);
95   ~MixerInput();
96 
97   void SetFilterGroup(FilterGroup* filter_group);
98 
source()99   Source* source() const { return source_; }
num_channels()100   int num_channels() const { return num_channels_; }
channel_layout()101   ::media::ChannelLayout channel_layout() const { return channel_layout_; }
input_samples_per_second()102   int input_samples_per_second() const { return input_samples_per_second_; }
output_samples_per_second()103   int output_samples_per_second() const { return output_samples_per_second_; }
primary()104   bool primary() const { return primary_; }
device_id()105   const std::string& device_id() const { return device_id_; }
content_type()106   AudioContentType content_type() const { return content_type_; }
focus_type()107   AudioContentType focus_type() const { return source_->focus_type(); }
108 
109   // Adds/removes an output redirector. When the mixer asks for more audio data,
110   // the lowest-ordered redirector (based on redirector->GetOrder()) is passed
111   // the audio data that would ordinarily have been mixed for local output;
112   // no audio from this MixerInput is passed to the mixer.
113   void AddAudioOutputRedirector(AudioOutputRedirectorInput* redirector);
114   void RemoveAudioOutputRedirector(AudioOutputRedirectorInput* redirector);
115 
116   // Reads data from the source. Returns the number of frames actually filled
117   // (<= |num_frames|).
118   int FillAudioData(int num_frames,
119                     RenderingDelay rendering_delay,
120                     ::media::AudioBus* dest);
121 
122   // Propagates |error| to the source.
123   void SignalError(Source::MixerError error);
124 
125   // Scales |frames| frames at |src| by the current volume (smoothing as
126   // needed). Adds the scaled result to |dest|.
127   // VolumeScaleAccumulate will be called once for each channel of audio
128   // present.
129   // |src| and |dest| should be 16-byte aligned.
130   void VolumeScaleAccumulate(const float* src, int frames, float* dest);
131 
132   // Sets the per-stream volume multiplier. If |multiplier| < 0, sets the
133   // volume multiplier to 0.
134   void SetVolumeMultiplier(float multiplier);
135 
136   // Sets the multiplier based on this stream's content type. The resulting
137   // output volume should be the content type volume * the per-stream volume
138   // multiplier.
139   void SetContentTypeVolume(float volume);
140 
141   // Sets min/max output volume for this stream (ie, limits the product of
142   // content type volume and per-stream volume multiplier). Note that mute
143   // and runtime output limits (for ducking) are applied after these limits.
144   void SetVolumeLimits(float volume_min, float volume_max);
145 
146   // Limits the output volume for this stream to below |limit|. Used for
147   // ducking. If |fade_ms| is >= 0, the resulting volume change should be
148   // faded over that many milliseconds; otherwise, the default fade time should
149   // be used.
150   void SetOutputLimit(float limit, int fade_ms);
151 
152   // Sets whether or not this stream should be muted.
153   void SetMuted(bool muted);
154 
155   // Returns the target volume multiplier of the stream. Fading in or out may
156   // cause this to be different from the actual multiplier applied in the last
157   // buffer. For the actual multiplier applied, use InstantaneousVolume().
158   float TargetVolume();
159 
160   // Returns the largest volume multiplier applied to the last buffer
161   // retrieved. This differs from TargetVolume() during transients.
162   float InstantaneousVolume();
163 
164  private:
165   int FillBuffer(int num_frames,
166                  RenderingDelay rendering_delay,
167                  ::media::AudioBus* dest);
168 
169   void ResamplerReadCallback(int frame_delay, ::media::AudioBus* output);
170 
171   Source* const source_;
172   const int num_channels_;
173   const ::media::ChannelLayout channel_layout_;
174   const int input_samples_per_second_;
175   const int output_samples_per_second_;
176   const bool primary_;
177   const std::string device_id_;
178   const AudioContentType content_type_;
179 
180   FilterGroup* filter_group_ = nullptr;
181   std::unique_ptr<::media::AudioBus> fill_buffer_;
182   std::unique_ptr<::media::ChannelMixer> channel_mixer_;
183 
184   float stream_volume_multiplier_ = 1.0f;
185   float type_volume_multiplier_ = 1.0f;
186   float volume_min_ = 0.0f;
187   float volume_max_ = 1.0f;
188   float output_volume_limit_ = 1.0f;
189   float mute_volume_multiplier_ = 1.0f;
190   SlewVolume slew_volume_;
191   // True if volume scale-accumulate has already been applied for at least
192   // one channel of the current buffer.
193   bool volume_applied_;
194   bool previous_ended_in_silence_;
195   bool first_buffer_;
196 
197   RenderingDelay mixer_rendering_delay_;
198   double resampler_buffered_frames_;
199   int filled_for_resampler_;
200   bool tried_to_fill_resampler_;
201   int resampled_silence_count_ = 0;
202   std::unique_ptr<::media::MultiChannelResampler> resampler_;
203 
204   std::vector<AudioOutputRedirectorInput*> audio_output_redirectors_;
205 
206   SEQUENCE_CHECKER(sequence_checker_);
207 
208   DISALLOW_COPY_AND_ASSIGN(MixerInput);
209 };
210 
211 }  // namespace media
212 }  // namespace chromecast
213 
214 #endif  // CHROMECAST_MEDIA_CMA_BACKEND_MIXER_MIXER_INPUT_H_
215