Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROMECAST_MEDIA_CMA_BACKEND_ALSA_STREAM_MIXER_ALSA_H_ | 5 #ifndef CHROMECAST_MEDIA_CMA_BACKEND_ALSA_STREAM_MIXER_ALSA_H_ |
| 6 #define CHROMECAST_MEDIA_CMA_BACKEND_ALSA_STREAM_MIXER_ALSA_H_ | 6 #define CHROMECAST_MEDIA_CMA_BACKEND_ALSA_STREAM_MIXER_ALSA_H_ |
| 7 | 7 |
| 8 #include <alsa/asoundlib.h> | 8 #include <alsa/asoundlib.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 #include "chromecast/public/cast_media_shlib.h" | 22 #include "chromecast/public/cast_media_shlib.h" |
| 23 | 23 |
| 24 namespace media { | 24 namespace media { |
| 25 class AudioBus; | 25 class AudioBus; |
| 26 } // namespace media | 26 } // namespace media |
| 27 | 27 |
| 28 namespace chromecast { | 28 namespace chromecast { |
| 29 namespace media { | 29 namespace media { |
| 30 class AlsaWrapper; | 30 class AlsaWrapper; |
| 31 | 31 |
| 32 const int kNumFilterGroups = 2; | |
| 33 | |
| 32 // Mixer implementation. The mixer has one or more input queues; these can be | 34 // Mixer implementation. The mixer has one or more input queues; these can be |
| 33 // added/removed at any time. When an input source pushes frames to an input | 35 // added/removed at any time. When an input source pushes frames to an input |
| 34 // queue, the queue should call StreamMixerAlsa::WriteFrames(); this causes | 36 // queue, the queue should call StreamMixerAlsa::WriteFrames(); this causes |
| 35 // the mixer to attempt to mix and write out as many frames as possible. To do | 37 // the mixer to attempt to mix and write out as many frames as possible. To do |
| 36 // this, the mixer determines how many frames can be read from all inputs (ie, | 38 // this, the mixer determines how many frames can be read from all inputs (ie, |
| 37 // it gets the maximum number of frames that can be read from each input, and | 39 // it gets the maximum number of frames that can be read from each input, and |
| 38 // uses the minimum value). Assuming that all primary inputs have some data | 40 // uses the minimum value). Assuming that all primary inputs have some data |
| 39 // available, the calculated number of frames are pulled from each input (maybe | 41 // available, the calculated number of frames are pulled from each input (maybe |
| 40 // resampled, if the input's incoming sample rate is not equal to the mixer's | 42 // resampled, if the input's incoming sample rate is not equal to the mixer's |
| 41 // output sample rate) and written to the ALSA stack. | 43 // output sample rate), mixed and written to the ALSA stack. |
| 44 // | |
| 45 // MIXER ARCHITECTURE | |
| 46 // The mixer allows multiple streams to be grouped together for post-processing | |
| 47 // purposes. Each group has a single AudioFilterInterface instance that | |
| 48 // is used to post-process the data before a final mixing stage. | |
| 49 // | |
| 50 // Stream Stream Stream Stream Stream <- Input streams | |
| 51 // | | | | | | |
| 52 // --------------------- ---------------- | |
| 53 // | Media Mixer | | System Mixer | <- Intermediate mix | |
| 54 // --------------------- ---------------- | |
| 55 // | | | |
| 56 // --------------------- ----------------- | |
| 57 // | Media Filter | | System Filter | | |
| 58 // --------------------- ----------------- | |
| 59 // \ / | |
| 60 // ----------------- | |
| 61 // | Final Mixer | | |
| 62 // ----------------- | |
| 63 // | | |
| 64 // ALSA | |
| 42 // | 65 // |
| 43 // The rendering delay is recalculated after every successful write to the ALSA | 66 // The rendering delay is recalculated after every successful write to the ALSA |
| 44 // stack. This delay is passed up to the input sources whenever some new data | 67 // stack. This delay is passed up to the input sources whenever some new data |
| 45 // is sucessfully added to the input queue (which happens whenever the amount | 68 // is sucessfully added to the input queue (which happens whenever the amount |
| 46 // of data in the queue drops below the maximum limit, if data is pending). Note | 69 // of data in the queue drops below the maximum limit, if data is pending). Note |
| 47 // that the rendering delay is not accurate while the mixer is gathering frames | 70 // that the rendering delay is not accurate while the mixer is gathering frames |
| 48 // to write, so the rendering delay and the queue size for each input must be | 71 // to write, so the rendering delay and the queue size for each input must be |
| 49 // updated atomically after each write is complete (ie, in AfterWriteFrames()). | 72 // updated atomically after each write is complete (ie, in AfterWriteFrames()). |
| 50 class StreamMixerAlsa { | 73 class StreamMixerAlsa { |
| 51 public: | 74 public: |
| 52 // This mixer will pull data from InputQueues which are added to it, mix the | 75 // This mixer will pull data from InputQueues which are added to it, mix the |
| 53 // data from the streams, and write the mixed data as a single stream to ALSA. | 76 // data from the streams, and write the mixed data as a single stream to ALSA. |
| 54 // These methods will be called on the mixer thread only. | 77 // These methods will be called on the mixer thread only. |
| 55 class InputQueue { | 78 class InputQueue { |
| 56 public: | 79 public: |
| 57 using OnReadyToDeleteCb = base::Callback<void(InputQueue*)>; | 80 using OnReadyToDeleteCb = base::Callback<void(InputQueue*)>; |
| 58 | 81 |
| 59 virtual ~InputQueue() {} | 82 virtual ~InputQueue() {} |
| 60 | 83 |
| 61 // Returns the sample rate of this stream *before* data is resampled to | 84 // Returns the sample rate of this stream *before* data is resampled to |
| 62 // match the sample rate expected by the mixer. The returned value must be | 85 // match the sample rate expected by the mixer. The returned value must be |
| 63 // positive. | 86 // positive. |
| 64 virtual int input_samples_per_second() const = 0; | 87 virtual int input_samples_per_second() const = 0; |
| 65 | 88 |
| 66 // Returns true if the stream is primary. Primary streams will be given | 89 // Returns true if the stream is primary. Primary streams will be given |
| 67 // precedence for sample rates and will dictate when data is polled. | 90 // precedence for sample rates and will dictate when data is polled. |
| 68 virtual bool primary() const = 0; | 91 virtual bool primary() const = 0; |
| 69 | 92 |
| 93 // Returns a string describing the content type class. | |
| 94 // Should be from chromecast/public/media/audio_device_ids.h | |
| 95 // or media/audio/audio_device_description.h | |
| 96 virtual std::string name() const = 0; | |
| 97 | |
| 70 // Returns true if PrepareToDelete() has been called. | 98 // Returns true if PrepareToDelete() has been called. |
| 71 virtual bool IsDeleting() const = 0; | 99 virtual bool IsDeleting() const = 0; |
| 72 | 100 |
| 73 // Initializes the InputQueue after the mixer is set up. At this point the | 101 // Initializes the InputQueue after the mixer is set up. At this point the |
| 74 // input can correctly determine the mixer's output sample rate. | 102 // input can correctly determine the mixer's output sample rate. |
| 75 virtual void Initialize(const MediaPipelineBackendAlsa::RenderingDelay& | 103 virtual void Initialize(const MediaPipelineBackendAlsa::RenderingDelay& |
| 76 mixer_rendering_delay) = 0; | 104 mixer_rendering_delay) = 0; |
| 77 | 105 |
| 78 // Returns the maximum number of frames that can be read from this input | 106 // Returns the maximum number of frames that can be read from this input |
| 79 // stream without filling with zeros. This should return 0 if the queue is | 107 // stream without filling with zeros. This should return 0 if the queue is |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 // May be called on any thread. | 216 // May be called on any thread. |
| 189 void DeleteInputQueue(InputQueue* input); | 217 void DeleteInputQueue(InputQueue* input); |
| 190 // Runs on mixer thread to complete input queue deletion. | 218 // Runs on mixer thread to complete input queue deletion. |
| 191 void DeleteInputQueueInternal(InputQueue* input); | 219 void DeleteInputQueueInternal(InputQueue* input); |
| 192 // Called after a timeout period to close the PCM handle if no inputs are | 220 // Called after a timeout period to close the PCM handle if no inputs are |
| 193 // present. | 221 // present. |
| 194 void CheckClose(); | 222 void CheckClose(); |
| 195 | 223 |
| 196 void WriteFrames(); | 224 void WriteFrames(); |
| 197 bool TryWriteFrames(); | 225 bool TryWriteFrames(); |
| 198 void WriteMixedPcm(const ::media::AudioBus& mixed, int frames, | 226 |
| 199 bool is_silence); | 227 // Mixes |active_inputs| and processes the resulting buffer |
| 228 // with pre_loopback_filter_[|filter|]. | |
| 229 // If |accumulate| is false, |interleaved_| will be overwritten | |
| 230 // (rather than summed). | |
| 231 // Returns |true| if non-zero data was written to |interleaved_|. | |
| 232 bool MixAndFilterGroup(const std::vector<InputQueue*>& active_inputs, | |
| 233 int filter, | |
| 234 int frames, | |
| 235 bool accumulate); | |
| 236 void WriteMixedPcm(int frames); | |
| 200 void UpdateRenderingDelay(int newly_pushed_frames); | 237 void UpdateRenderingDelay(int newly_pushed_frames); |
| 201 ssize_t BytesPerOutputFormatSample(); | 238 ssize_t BytesPerOutputFormatSample(); |
| 239 void ResizeBuffersIfNecessary(int chunk_size); | |
| 202 | 240 |
| 203 static bool single_threaded_for_test_; | 241 static bool single_threaded_for_test_; |
| 204 | 242 |
| 205 std::unique_ptr<AlsaWrapper> alsa_; | 243 std::unique_ptr<AlsaWrapper> alsa_; |
| 206 std::unique_ptr<base::Thread> mixer_thread_; | 244 std::unique_ptr<base::Thread> mixer_thread_; |
| 207 scoped_refptr<base::SingleThreadTaskRunner> mixer_task_runner_; | 245 scoped_refptr<base::SingleThreadTaskRunner> mixer_task_runner_; |
| 208 | 246 |
| 209 unsigned int fixed_output_samples_per_second_; | 247 unsigned int fixed_output_samples_per_second_; |
| 210 unsigned int low_sample_rate_cutoff_; | 248 unsigned int low_sample_rate_cutoff_; |
| 211 int requested_output_samples_per_second_; | 249 int requested_output_samples_per_second_; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 222 bool alsa_period_explicitly_set; | 260 bool alsa_period_explicitly_set; |
| 223 snd_pcm_uframes_t alsa_period_size_; | 261 snd_pcm_uframes_t alsa_period_size_; |
| 224 snd_pcm_uframes_t alsa_start_threshold_; | 262 snd_pcm_uframes_t alsa_start_threshold_; |
| 225 snd_pcm_uframes_t alsa_avail_min_; | 263 snd_pcm_uframes_t alsa_avail_min_; |
| 226 | 264 |
| 227 State state_; | 265 State state_; |
| 228 | 266 |
| 229 std::vector<std::unique_ptr<InputQueue>> inputs_; | 267 std::vector<std::unique_ptr<InputQueue>> inputs_; |
| 230 std::vector<std::unique_ptr<InputQueue>> ignored_inputs_; | 268 std::vector<std::unique_ptr<InputQueue>> ignored_inputs_; |
| 231 MediaPipelineBackendAlsa::RenderingDelay rendering_delay_; | 269 MediaPipelineBackendAlsa::RenderingDelay rendering_delay_; |
| 270 // Buffer to write intermediate mixes during post-processing. | |
| 271 std::vector<uint8_t> interleaved_intermediate_; | |
| 232 // Buffer to write final interleaved data before sending to snd_pcm_writei(). | 272 // Buffer to write final interleaved data before sending to snd_pcm_writei(). |
| 233 std::vector<uint8_t> interleaved_; | 273 std::vector<uint8_t> interleaved_; |
| 234 | 274 |
| 235 // Buffers that hold audio data while it is mixed, before it is passed to the | 275 // Buffers that hold audio data while it is mixed, before it is passed to the |
| 236 // ALSA layer. These are kept as members of this class to minimize copies and | 276 // ALSA layer. These are kept as members of this class to minimize copies and |
| 237 // allocations. | 277 // allocations. |
| 238 std::unique_ptr<::media::AudioBus> temp_; | 278 std::unique_ptr<::media::AudioBus> temp_; |
| 239 std::unique_ptr<::media::AudioBus> mixed_; | 279 std::unique_ptr<::media::AudioBus> mixed_; |
| 240 | 280 |
| 241 std::unique_ptr<base::Timer> retry_write_frames_timer_; | 281 std::unique_ptr<base::Timer> retry_write_frames_timer_; |
| 242 | 282 |
| 243 int check_close_timeout_; | 283 int check_close_timeout_; |
| 244 std::unique_ptr<base::Timer> check_close_timer_; | 284 std::unique_ptr<base::Timer> check_close_timer_; |
| 245 | 285 |
| 246 std::vector<CastMediaShlib::LoopbackAudioObserver*> loopback_observers_; | 286 std::vector<CastMediaShlib::LoopbackAudioObserver*> loopback_observers_; |
| 247 | 287 |
| 248 std::unique_ptr<AudioFilterInterface> pre_loopback_filter_; | 288 std::unique_ptr<AudioFilterInterface> pre_loopback_filter_[kNumFilterGroups]; |
|
tianyuwang1
2017/02/18 00:17:08
Since there's no post_loopback_filter_ anymore, ju
bshaya
2017/02/21 23:30:14
Done.
| |
| 249 std::unique_ptr<AudioFilterInterface> post_loopback_filter_; | 289 int silence_frames_filtered_[kNumFilterGroups]; |
| 250 int silence_frames_filtered_ = 0; | |
| 251 | 290 |
| 252 DISALLOW_COPY_AND_ASSIGN(StreamMixerAlsa); | 291 DISALLOW_COPY_AND_ASSIGN(StreamMixerAlsa); |
| 253 }; | 292 }; |
| 254 | 293 |
| 255 } // namespace media | 294 } // namespace media |
| 256 } // namespace chromecast | 295 } // namespace chromecast |
| 257 | 296 |
| 258 #endif // CHROMECAST_MEDIA_CMA_BACKEND_ALSA_STREAM_MIXER_ALSA_H_ | 297 #endif // CHROMECAST_MEDIA_CMA_BACKEND_ALSA_STREAM_MIXER_ALSA_H_ |
| OLD | NEW |