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 10 matching lines...) Expand all Loading... |
21 #include "chromecast/media/cma/backend/alsa/stream_mixer_alsa_input.h" | 21 #include "chromecast/media/cma/backend/alsa/stream_mixer_alsa_input.h" |
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 class FilterGroup; |
31 | 32 |
32 // Mixer implementation. The mixer has one or more input queues; these can be | 33 // 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 | 34 // added/removed at any time. When an input source pushes frames to an input |
34 // queue, the queue should call StreamMixerAlsa::WriteFrames(); this causes | 35 // 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 | 36 // 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, | 37 // 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 | 38 // 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 | 39 // 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 | 40 // 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 | 41 // resampled, if the input's incoming sample rate is not equal to the mixer's |
(...skipping 19 matching lines...) Expand all Loading... |
60 | 61 |
61 // Returns the sample rate of this stream *before* data is resampled to | 62 // 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 | 63 // match the sample rate expected by the mixer. The returned value must be |
63 // positive. | 64 // positive. |
64 virtual int input_samples_per_second() const = 0; | 65 virtual int input_samples_per_second() const = 0; |
65 | 66 |
66 // Returns true if the stream is primary. Primary streams will be given | 67 // Returns true if the stream is primary. Primary streams will be given |
67 // precedence for sample rates and will dictate when data is polled. | 68 // precedence for sample rates and will dictate when data is polled. |
68 virtual bool primary() const = 0; | 69 virtual bool primary() const = 0; |
69 | 70 |
| 71 // Returns a string describing the content type class. |
| 72 // Should be from chromecast/media/base/audio_device_ids.h |
| 73 // or media/audio/audio_device_description.h |
| 74 virtual std::string name() const = 0; |
| 75 |
70 // Returns true if PrepareToDelete() has been called. | 76 // Returns true if PrepareToDelete() has been called. |
71 virtual bool IsDeleting() const = 0; | 77 virtual bool IsDeleting() const = 0; |
72 | 78 |
73 // Initializes the InputQueue after the mixer is set up. At this point the | 79 // Initializes the InputQueue after the mixer is set up. At this point the |
74 // input can correctly determine the mixer's output sample rate. | 80 // input can correctly determine the mixer's output sample rate. |
75 virtual void Initialize(const MediaPipelineBackendAlsa::RenderingDelay& | 81 virtual void Initialize(const MediaPipelineBackendAlsa::RenderingDelay& |
76 mixer_rendering_delay) = 0; | 82 mixer_rendering_delay) = 0; |
77 | 83 |
78 // Returns the maximum number of frames that can be read from this input | 84 // 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 | 85 // 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. | 194 // May be called on any thread. |
189 void DeleteInputQueue(InputQueue* input); | 195 void DeleteInputQueue(InputQueue* input); |
190 // Runs on mixer thread to complete input queue deletion. | 196 // Runs on mixer thread to complete input queue deletion. |
191 void DeleteInputQueueInternal(InputQueue* input); | 197 void DeleteInputQueueInternal(InputQueue* input); |
192 // Called after a timeout period to close the PCM handle if no inputs are | 198 // Called after a timeout period to close the PCM handle if no inputs are |
193 // present. | 199 // present. |
194 void CheckClose(); | 200 void CheckClose(); |
195 | 201 |
196 void WriteFrames(); | 202 void WriteFrames(); |
197 bool TryWriteFrames(); | 203 bool TryWriteFrames(); |
198 void WriteMixedPcm(const ::media::AudioBus& mixed, int frames, | 204 void WriteMixedPcm(std::vector<uint8_t>* interleaved, int frames); |
199 bool is_silence); | |
200 void UpdateRenderingDelay(int newly_pushed_frames); | 205 void UpdateRenderingDelay(int newly_pushed_frames); |
| 206 size_t InterleavedSize(int frames); |
201 ssize_t BytesPerOutputFormatSample(); | 207 ssize_t BytesPerOutputFormatSample(); |
| 208 void ResizeBuffersIfNecessary(int chunk_size); |
202 | 209 |
203 static bool single_threaded_for_test_; | 210 static bool single_threaded_for_test_; |
204 | 211 |
205 std::unique_ptr<AlsaWrapper> alsa_; | 212 std::unique_ptr<AlsaWrapper> alsa_; |
206 std::unique_ptr<base::Thread> mixer_thread_; | 213 std::unique_ptr<base::Thread> mixer_thread_; |
207 scoped_refptr<base::SingleThreadTaskRunner> mixer_task_runner_; | 214 scoped_refptr<base::SingleThreadTaskRunner> mixer_task_runner_; |
208 | 215 |
209 unsigned int fixed_output_samples_per_second_; | 216 unsigned int fixed_output_samples_per_second_; |
210 unsigned int low_sample_rate_cutoff_; | 217 unsigned int low_sample_rate_cutoff_; |
211 int requested_output_samples_per_second_; | 218 int requested_output_samples_per_second_; |
(...skipping 10 matching lines...) Expand all Loading... |
222 bool alsa_period_explicitly_set; | 229 bool alsa_period_explicitly_set; |
223 snd_pcm_uframes_t alsa_period_size_; | 230 snd_pcm_uframes_t alsa_period_size_; |
224 snd_pcm_uframes_t alsa_start_threshold_; | 231 snd_pcm_uframes_t alsa_start_threshold_; |
225 snd_pcm_uframes_t alsa_avail_min_; | 232 snd_pcm_uframes_t alsa_avail_min_; |
226 | 233 |
227 State state_; | 234 State state_; |
228 | 235 |
229 std::vector<std::unique_ptr<InputQueue>> inputs_; | 236 std::vector<std::unique_ptr<InputQueue>> inputs_; |
230 std::vector<std::unique_ptr<InputQueue>> ignored_inputs_; | 237 std::vector<std::unique_ptr<InputQueue>> ignored_inputs_; |
231 MediaPipelineBackendAlsa::RenderingDelay rendering_delay_; | 238 MediaPipelineBackendAlsa::RenderingDelay rendering_delay_; |
232 // Buffer to write final interleaved data before sending to snd_pcm_writei(). | |
233 std::vector<uint8_t> interleaved_; | |
234 | |
235 // 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 | |
237 // allocations. | |
238 std::unique_ptr<::media::AudioBus> temp_; | |
239 std::unique_ptr<::media::AudioBus> mixed_; | |
240 | 239 |
241 std::unique_ptr<base::Timer> retry_write_frames_timer_; | 240 std::unique_ptr<base::Timer> retry_write_frames_timer_; |
242 | 241 |
243 int check_close_timeout_; | 242 int check_close_timeout_; |
244 std::unique_ptr<base::Timer> check_close_timer_; | 243 std::unique_ptr<base::Timer> check_close_timer_; |
245 | 244 |
| 245 std::vector<std::unique_ptr<FilterGroup>> filter_groups_; |
246 std::vector<CastMediaShlib::LoopbackAudioObserver*> loopback_observers_; | 246 std::vector<CastMediaShlib::LoopbackAudioObserver*> loopback_observers_; |
247 | 247 |
248 std::unique_ptr<AudioFilterInterface> pre_loopback_filter_; | |
249 std::unique_ptr<AudioFilterInterface> post_loopback_filter_; | |
250 int silence_frames_filtered_ = 0; | |
251 | |
252 DISALLOW_COPY_AND_ASSIGN(StreamMixerAlsa); | 248 DISALLOW_COPY_AND_ASSIGN(StreamMixerAlsa); |
253 }; | 249 }; |
254 | 250 |
255 } // namespace media | 251 } // namespace media |
256 } // namespace chromecast | 252 } // namespace chromecast |
257 | 253 |
258 #endif // CHROMECAST_MEDIA_CMA_BACKEND_ALSA_STREAM_MIXER_ALSA_H_ | 254 #endif // CHROMECAST_MEDIA_CMA_BACKEND_ALSA_STREAM_MIXER_ALSA_H_ |
OLD | NEW |