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; | |
32 | 31 |
33 // Mixer implementation. The mixer has one or more input queues; these can be | 32 // Mixer implementation. The mixer has one or more input queues; these can be |
34 // added/removed at any time. When an input source pushes frames to an input | 33 // added/removed at any time. When an input source pushes frames to an input |
35 // queue, the queue should call StreamMixerAlsa::WriteFrames(); this causes | 34 // queue, the queue should call StreamMixerAlsa::WriteFrames(); this causes |
36 // the mixer to attempt to mix and write out as many frames as possible. To do | 35 // the mixer to attempt to mix and write out as many frames as possible. To do |
37 // this, the mixer determines how many frames can be read from all inputs (ie, | 36 // this, the mixer determines how many frames can be read from all inputs (ie, |
38 // it gets the maximum number of frames that can be read from each input, and | 37 // it gets the maximum number of frames that can be read from each input, and |
39 // uses the minimum value). Assuming that all primary inputs have some data | 38 // uses the minimum value). Assuming that all primary inputs have some data |
40 // available, the calculated number of frames are pulled from each input (maybe | 39 // available, the calculated number of frames are pulled from each input (maybe |
41 // resampled, if the input's incoming sample rate is not equal to the mixer's | 40 // resampled, if the input's incoming sample rate is not equal to the mixer's |
(...skipping 19 matching lines...) Expand all Loading... |
61 | 60 |
62 // Returns the sample rate of this stream *before* data is resampled to | 61 // Returns the sample rate of this stream *before* data is resampled to |
63 // match the sample rate expected by the mixer. The returned value must be | 62 // match the sample rate expected by the mixer. The returned value must be |
64 // positive. | 63 // positive. |
65 virtual int input_samples_per_second() const = 0; | 64 virtual int input_samples_per_second() const = 0; |
66 | 65 |
67 // Returns true if the stream is primary. Primary streams will be given | 66 // Returns true if the stream is primary. Primary streams will be given |
68 // precedence for sample rates and will dictate when data is polled. | 67 // precedence for sample rates and will dictate when data is polled. |
69 virtual bool primary() const = 0; | 68 virtual bool primary() const = 0; |
70 | 69 |
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 device_id() const = 0; | |
75 | |
76 // Returns true if PrepareToDelete() has been called. | 70 // Returns true if PrepareToDelete() has been called. |
77 virtual bool IsDeleting() const = 0; | 71 virtual bool IsDeleting() const = 0; |
78 | 72 |
79 // Initializes the InputQueue after the mixer is set up. At this point the | 73 // Initializes the InputQueue after the mixer is set up. At this point the |
80 // input can correctly determine the mixer's output sample rate. | 74 // input can correctly determine the mixer's output sample rate. |
81 virtual void Initialize(const MediaPipelineBackendAlsa::RenderingDelay& | 75 virtual void Initialize(const MediaPipelineBackendAlsa::RenderingDelay& |
82 mixer_rendering_delay) = 0; | 76 mixer_rendering_delay) = 0; |
83 | 77 |
84 // Sets and gets the FilterGroup the InputQueue matches. | |
85 // This is determined at creation to save time matching the InputQueue | |
86 // to a FilterGroup every time it needs to be mixed. | |
87 virtual void set_filter_group(FilterGroup* filter_group) = 0; | |
88 virtual FilterGroup* filter_group() = 0; | |
89 | |
90 // Returns the maximum number of frames that can be read from this input | 78 // Returns the maximum number of frames that can be read from this input |
91 // stream without filling with zeros. This should return 0 if the queue is | 79 // stream without filling with zeros. This should return 0 if the queue is |
92 // empty and EOS has not been queued. | 80 // empty and EOS has not been queued. |
93 virtual int MaxReadSize() = 0; | 81 virtual int MaxReadSize() = 0; |
94 | 82 |
95 // Pulls data from the input stream. The input stream should populate |dest| | 83 // Pulls data from the input stream. The input stream should populate |dest| |
96 // with |frames| frames of data to be mixed. The mixer expects data to be | 84 // with |frames| frames of data to be mixed. The mixer expects data to be |
97 // at a sample rate of |output_samples_per_second()|, so each input stream | 85 // at a sample rate of |output_samples_per_second()|, so each input stream |
98 // should resample as necessary before returning. |frames| is guaranteed to | 86 // should resample as necessary before returning. |frames| is guaranteed to |
99 // be no larger than the value returned by the most recent call to | 87 // be no larger than the value returned by the most recent call to |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 // May be called on any thread. | 188 // May be called on any thread. |
201 void DeleteInputQueue(InputQueue* input); | 189 void DeleteInputQueue(InputQueue* input); |
202 // Runs on mixer thread to complete input queue deletion. | 190 // Runs on mixer thread to complete input queue deletion. |
203 void DeleteInputQueueInternal(InputQueue* input); | 191 void DeleteInputQueueInternal(InputQueue* input); |
204 // Called after a timeout period to close the PCM handle if no inputs are | 192 // Called after a timeout period to close the PCM handle if no inputs are |
205 // present. | 193 // present. |
206 void CheckClose(); | 194 void CheckClose(); |
207 | 195 |
208 void WriteFrames(); | 196 void WriteFrames(); |
209 bool TryWriteFrames(); | 197 bool TryWriteFrames(); |
210 void WriteMixedPcm(std::vector<uint8_t>* interleaved, int frames); | 198 void WriteMixedPcm(const ::media::AudioBus& mixed, int frames, |
| 199 bool is_silence); |
211 void UpdateRenderingDelay(int newly_pushed_frames); | 200 void UpdateRenderingDelay(int newly_pushed_frames); |
212 size_t InterleavedSize(int frames); | |
213 ssize_t BytesPerOutputFormatSample(); | 201 ssize_t BytesPerOutputFormatSample(); |
214 void ResizeBuffersIfNecessary(int chunk_size); | |
215 | 202 |
216 static bool single_threaded_for_test_; | 203 static bool single_threaded_for_test_; |
217 | 204 |
218 std::unique_ptr<AlsaWrapper> alsa_; | 205 std::unique_ptr<AlsaWrapper> alsa_; |
219 std::unique_ptr<base::Thread> mixer_thread_; | 206 std::unique_ptr<base::Thread> mixer_thread_; |
220 scoped_refptr<base::SingleThreadTaskRunner> mixer_task_runner_; | 207 scoped_refptr<base::SingleThreadTaskRunner> mixer_task_runner_; |
221 | 208 |
222 unsigned int fixed_output_samples_per_second_; | 209 unsigned int fixed_output_samples_per_second_; |
223 unsigned int low_sample_rate_cutoff_; | 210 unsigned int low_sample_rate_cutoff_; |
224 int requested_output_samples_per_second_; | 211 int requested_output_samples_per_second_; |
(...skipping 10 matching lines...) Expand all Loading... |
235 bool alsa_period_explicitly_set; | 222 bool alsa_period_explicitly_set; |
236 snd_pcm_uframes_t alsa_period_size_; | 223 snd_pcm_uframes_t alsa_period_size_; |
237 snd_pcm_uframes_t alsa_start_threshold_; | 224 snd_pcm_uframes_t alsa_start_threshold_; |
238 snd_pcm_uframes_t alsa_avail_min_; | 225 snd_pcm_uframes_t alsa_avail_min_; |
239 | 226 |
240 State state_; | 227 State state_; |
241 | 228 |
242 std::vector<std::unique_ptr<InputQueue>> inputs_; | 229 std::vector<std::unique_ptr<InputQueue>> inputs_; |
243 std::vector<std::unique_ptr<InputQueue>> ignored_inputs_; | 230 std::vector<std::unique_ptr<InputQueue>> ignored_inputs_; |
244 MediaPipelineBackendAlsa::RenderingDelay rendering_delay_; | 231 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_; |
245 | 240 |
246 std::unique_ptr<base::Timer> retry_write_frames_timer_; | 241 std::unique_ptr<base::Timer> retry_write_frames_timer_; |
247 | 242 |
248 int check_close_timeout_; | 243 int check_close_timeout_; |
249 std::unique_ptr<base::Timer> check_close_timer_; | 244 std::unique_ptr<base::Timer> check_close_timer_; |
250 | 245 |
251 std::vector<std::unique_ptr<FilterGroup>> filter_groups_; | |
252 std::vector<CastMediaShlib::LoopbackAudioObserver*> loopback_observers_; | 246 std::vector<CastMediaShlib::LoopbackAudioObserver*> loopback_observers_; |
253 | 247 |
| 248 std::unique_ptr<AudioFilterInterface> pre_loopback_filter_; |
| 249 std::unique_ptr<AudioFilterInterface> post_loopback_filter_; |
| 250 int silence_frames_filtered_ = 0; |
| 251 |
254 DISALLOW_COPY_AND_ASSIGN(StreamMixerAlsa); | 252 DISALLOW_COPY_AND_ASSIGN(StreamMixerAlsa); |
255 }; | 253 }; |
256 | 254 |
257 } // namespace media | 255 } // namespace media |
258 } // namespace chromecast | 256 } // namespace chromecast |
259 | 257 |
260 #endif // CHROMECAST_MEDIA_CMA_BACKEND_ALSA_STREAM_MIXER_ALSA_H_ | 258 #endif // CHROMECAST_MEDIA_CMA_BACKEND_ALSA_STREAM_MIXER_ALSA_H_ |
OLD | NEW |