OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // AudioRendererBase takes care of the tricky queuing work and provides simple | 5 // Audio rendering unit utilizing an AudioRendererSink to output data. |
6 // methods for subclasses to peek and poke at audio data. In addition to | |
7 // AudioRenderer interface methods this classes doesn't implement, subclasses | |
8 // must also implement the following methods: | |
9 // OnInitialized | |
10 // OnStop | |
11 // OnRenderEndOfStream | |
12 // | 6 // |
13 // The general assumption is that subclasses start a callback-based audio thread | 7 // This class lives inside three threads during it's lifetime, namely: |
14 // which needs to be filled with decoded audio data. AudioDecoderBase provides | 8 // 1. Render thread. |
15 // FillBuffer which handles filling the provided buffer, dequeuing items, | 9 // This object is created on the render thread. |
16 // scheduling additional reads and updating the clock. In a sense, | 10 // 2. Pipeline thread |
17 // AudioRendererBase is the producer and the subclass is the consumer. | 11 // Initialize() is called here with the audio format. |
| 12 // Play/Pause/Seek also happens here. |
| 13 // 3. Audio thread created by the AudioRendererSink. |
| 14 // Render() is called here where audio data is decoded into raw PCM data. |
| 15 // |
| 16 // AudioRendererBase talks to an AudioRendererAlgorithmBase that takes care of |
| 17 // queueing audio data and stretching/shrinking audio data when playback rate != |
| 18 // 1.0 or 0.0. |
18 | 19 |
19 #ifndef MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ | 20 #ifndef MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ |
20 #define MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ | 21 #define MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ |
21 | 22 |
22 #include <deque> | 23 #include <deque> |
23 | 24 |
24 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
25 #include "media/base/audio_decoder.h" | 26 #include "media/base/audio_decoder.h" |
| 27 #include "media/base/audio_renderer_sink.h" |
26 #include "media/base/buffers.h" | 28 #include "media/base/buffers.h" |
27 #include "media/base/filters.h" | 29 #include "media/base/filters.h" |
28 #include "media/filters/audio_renderer_algorithm_base.h" | 30 #include "media/filters/audio_renderer_algorithm_base.h" |
29 | 31 |
30 namespace media { | 32 namespace media { |
31 | 33 |
32 class MEDIA_EXPORT AudioRendererBase : public AudioRenderer { | 34 class MEDIA_EXPORT AudioRendererBase |
| 35 : public AudioRenderer, |
| 36 NON_EXPORTED_BASE(public media::AudioRendererSink::RenderCallback) { |
33 public: | 37 public: |
34 AudioRendererBase(); | 38 // Methods called on Render thread ------------------------------------------ |
| 39 // An AudioRendererSink is used as the destination for the rendered audio. |
| 40 explicit AudioRendererBase(media::AudioRendererSink* sink); |
35 virtual ~AudioRendererBase(); | 41 virtual ~AudioRendererBase(); |
36 | 42 |
| 43 // Methods called on pipeline thread ---------------------------------------- |
37 // Filter implementation. | 44 // Filter implementation. |
38 virtual void Play(const base::Closure& callback) OVERRIDE; | 45 virtual void Play(const base::Closure& callback) OVERRIDE; |
39 virtual void Pause(const base::Closure& callback) OVERRIDE; | 46 virtual void Pause(const base::Closure& callback) OVERRIDE; |
40 virtual void Flush(const base::Closure& callback) OVERRIDE; | 47 virtual void Flush(const base::Closure& callback) OVERRIDE; |
41 virtual void Stop(const base::Closure& callback) OVERRIDE; | 48 virtual void Stop(const base::Closure& callback) OVERRIDE; |
| 49 virtual void SetPlaybackRate(float rate) OVERRIDE; |
42 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE; | 50 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE; |
43 | 51 |
44 // AudioRenderer implementation. | 52 // AudioRenderer implementation. |
45 virtual void Initialize(const scoped_refptr<AudioDecoder>& decoder, | 53 virtual void Initialize(const scoped_refptr<AudioDecoder>& decoder, |
46 const PipelineStatusCB& init_cb, | 54 const PipelineStatusCB& init_cb, |
47 const base::Closure& underflow_cb, | 55 const base::Closure& underflow_cb, |
48 const TimeCB& time_cb) OVERRIDE; | 56 const TimeCB& time_cb) OVERRIDE; |
49 virtual bool HasEnded() OVERRIDE; | 57 virtual bool HasEnded() OVERRIDE; |
50 virtual void ResumeAfterUnderflow(bool buffer_more_audio) OVERRIDE; | 58 virtual void ResumeAfterUnderflow(bool buffer_more_audio) OVERRIDE; |
| 59 virtual void SetVolume(float volume) OVERRIDE; |
51 | 60 |
52 protected: | 61 private: |
| 62 friend class AudioRendererBaseTest; |
53 FRIEND_TEST_ALL_PREFIXES(AudioRendererBaseTest, EndOfStream); | 63 FRIEND_TEST_ALL_PREFIXES(AudioRendererBaseTest, EndOfStream); |
54 FRIEND_TEST_ALL_PREFIXES(AudioRendererBaseTest, Underflow_EndOfStream); | 64 FRIEND_TEST_ALL_PREFIXES(AudioRendererBaseTest, Underflow_EndOfStream); |
55 | 65 |
56 // Subclasses should return true if they were able to initialize, false | |
57 // otherwise. | |
58 virtual bool OnInitialize(int bits_per_channel, | |
59 ChannelLayout channel_layout, | |
60 int sample_rate) = 0; | |
61 | |
62 // Called by Stop(). Subclasses should perform any necessary cleanup during | |
63 // this time, such as stopping any running threads. | |
64 virtual void OnStop() = 0; | |
65 | |
66 // Method called by FillBuffer() when it finds that it reached end of stream. | |
67 // FillBuffer() cannot immediately signal end of stream event because browser | |
68 // may have buffered data. | |
69 virtual void OnRenderEndOfStream() = 0; | |
70 | |
71 // Callback from the audio decoder delivering decoded audio samples. | 66 // Callback from the audio decoder delivering decoded audio samples. |
72 void DecodedAudioReady(scoped_refptr<Buffer> buffer); | 67 void DecodedAudioReady(scoped_refptr<Buffer> buffer); |
73 | 68 |
74 // Fills the given buffer with audio data by delegating to its |algorithm_|. | 69 // Fills the given buffer with audio data by delegating to its |algorithm_|. |
75 // FillBuffer() also takes care of updating the clock. Returns the number of | 70 // FillBuffer() also takes care of updating the clock. Returns the number of |
76 // frames copied into |dest|, which may be less than or equal to | 71 // frames copied into |dest|, which may be less than or equal to |
77 // |requested_frames|. | 72 // |requested_frames|. |
78 // | 73 // |
79 // If this method returns fewer frames than |requested_frames|, it could | 74 // If this method returns fewer frames than |requested_frames|, it could |
80 // be a sign that the pipeline is stalled or unable to stream the data fast | 75 // be a sign that the pipeline is stalled or unable to stream the data fast |
81 // enough. In such scenarios, the callee should zero out unused portions | 76 // enough. In such scenarios, the callee should zero out unused portions |
82 // of their buffer to playback silence. | 77 // of their buffer to playback silence. |
83 // | 78 // |
84 // FillBuffer() updates the pipeline's playback timestamp. If FillBuffer() is | 79 // FillBuffer() updates the pipeline's playback timestamp. If FillBuffer() is |
85 // not called at the same rate as audio samples are played, then the reported | 80 // not called at the same rate as audio samples are played, then the reported |
86 // timestamp in the pipeline will be ahead of the actual audio playback. In | 81 // timestamp in the pipeline will be ahead of the actual audio playback. In |
87 // this case |playback_delay| should be used to indicate when in the future | 82 // this case |playback_delay| should be used to indicate when in the future |
88 // should the filled buffer be played. If FillBuffer() is called as the audio | 83 // should the filled buffer be played. If FillBuffer() is called as the audio |
89 // hardware plays the buffer, then |playback_delay| should be zero. | 84 // hardware plays the buffer, then |playback_delay| should be zero. |
90 // | 85 // |
91 // FillBuffer() calls OnRenderEndOfStream() when it reaches end of stream. | 86 // FillBuffer() calls SignalEndOfStream() when it reaches end of stream. |
92 // It is responsibility of derived class to provide implementation of | |
93 // OnRenderEndOfStream() that calls SignalEndOfStream() when all the hardware | |
94 // buffers become empty (i.e. when all the data written to the device has | |
95 // been played). | |
96 // | 87 // |
97 // Safe to call on any thread. | 88 // Safe to call on any thread. |
98 uint32 FillBuffer(uint8* dest, | 89 uint32 FillBuffer(uint8* dest, |
99 uint32 requested_frames, | 90 uint32 requested_frames, |
100 const base::TimeDelta& playback_delay); | 91 const base::TimeDelta& playback_delay); |
101 | 92 |
102 // Called by OnRenderEndOfStream() or some callback scheduled by derived class | 93 // Called at the end of stream when all the hardware buffers become empty |
103 // to signal end of stream. | 94 // (i.e. when all the data written to the device has been played). |
104 void SignalEndOfStream(); | 95 void SignalEndOfStream(); |
105 | 96 |
106 // Get/Set the playback rate of |algorithm_|. | 97 // Get the playback rate of |algorithm_|. |
107 virtual void SetPlaybackRate(float playback_rate) OVERRIDE; | 98 float GetPlaybackRate(); |
108 virtual float GetPlaybackRate(); | |
109 | 99 |
110 private: | 100 // Convert number of bytes to duration of time using information about the |
111 friend class AudioRendererBaseTest; | 101 // number of channels, sample rate and sample bits. |
| 102 base::TimeDelta ConvertToDuration(int bytes); |
| 103 |
| 104 // Estimate earliest time when current buffer can stop playing. |
| 105 void UpdateEarliestEndTime(int bytes_filled, |
| 106 base::TimeDelta request_delay, |
| 107 base::Time time_now); |
| 108 |
| 109 // Methods called on pipeline thread ---------------------------------------- |
| 110 void DoPlay(); |
| 111 void DoPause(); |
| 112 void DoSeek(); |
| 113 |
| 114 // media::AudioRendererSink::RenderCallback implementation. |
| 115 virtual int Render(const std::vector<float*>& audio_data, |
| 116 int number_of_frames, |
| 117 int audio_delay_milliseconds) OVERRIDE; |
| 118 virtual void OnRenderError() OVERRIDE; |
112 | 119 |
113 // Helper method that schedules an asynchronous read from the decoder and | 120 // Helper method that schedules an asynchronous read from the decoder and |
114 // increments |pending_reads_|. | 121 // increments |pending_reads_|. |
115 // | 122 // |
116 // Safe to call from any thread. | 123 // Safe to call from any thread. |
117 void ScheduleRead_Locked(); | 124 void ScheduleRead_Locked(); |
118 | 125 |
119 // Returns true if the data in the buffer is all before | 126 // Returns true if the data in the buffer is all before |
120 // |seek_timestamp_|. This can only return true while | 127 // |seek_timestamp_|. This can only return true while |
121 // in the kSeeking state. | 128 // in the kSeeking state. |
(...skipping 19 matching lines...) Expand all Loading... |
141 }; | 148 }; |
142 State state_; | 149 State state_; |
143 | 150 |
144 // Keep track of our outstanding read to |decoder_|. | 151 // Keep track of our outstanding read to |decoder_|. |
145 bool pending_read_; | 152 bool pending_read_; |
146 | 153 |
147 // Keeps track of whether we received and rendered the end of stream buffer. | 154 // Keeps track of whether we received and rendered the end of stream buffer. |
148 bool received_end_of_stream_; | 155 bool received_end_of_stream_; |
149 bool rendered_end_of_stream_; | 156 bool rendered_end_of_stream_; |
150 | 157 |
151 // Audio time at end of last call to FillBuffer(). | 158 // The timestamp of the last frame (i.e. furthest in the future) buffered. |
152 // TODO(ralphl): Update this value after seeking. | 159 // TODO(ralphl): Update this value after seeking. |
153 base::TimeDelta last_fill_buffer_time_; | 160 base::TimeDelta audio_time_buffered_; |
154 | 161 |
155 // Filter callbacks. | 162 // Filter callbacks. |
156 base::Closure pause_cb_; | 163 base::Closure pause_cb_; |
157 PipelineStatusCB seek_cb_; | 164 PipelineStatusCB seek_cb_; |
158 | 165 |
159 base::Closure underflow_cb_; | 166 base::Closure underflow_cb_; |
160 | 167 |
161 TimeCB time_cb_; | 168 TimeCB time_cb_; |
162 | 169 |
163 base::TimeDelta seek_timestamp_; | 170 base::TimeDelta seek_timestamp_; |
164 | 171 |
165 uint32 bytes_per_frame_; | 172 uint32 bytes_per_frame_; |
166 | 173 |
| 174 // Used to calculate audio delay given bytes. |
| 175 uint32 bytes_per_second_; |
| 176 |
| 177 // A flag that indicates this filter is called to stop. |
| 178 bool stopped_; |
| 179 |
| 180 // The sink (destination) for rendered audio. |
| 181 scoped_refptr<media::AudioRendererSink> sink_; |
| 182 |
| 183 // Set to true when OnInitialize() is called. |
| 184 bool is_initialized_; |
| 185 |
| 186 // We're supposed to know amount of audio data OS or hardware buffered, but |
| 187 // that is not always so -- on my Linux box |
| 188 // AudioBuffersState::hardware_delay_bytes never reaches 0. |
| 189 // |
| 190 // As a result we cannot use it to find when stream ends. If we just ignore |
| 191 // buffered data we will notify host that stream ended before it is actually |
| 192 // did so, I've seen it done ~140ms too early when playing ~150ms file. |
| 193 // |
| 194 // Instead of trying to invent OS-specific solution for each and every OS we |
| 195 // are supporting, use simple workaround: every time we fill the buffer we |
| 196 // remember when it should stop playing, and do not assume that buffer is |
| 197 // empty till that time. Workaround is not bulletproof, as we don't exactly |
| 198 // know when that particular data would start playing, but it is much better |
| 199 // than nothing. |
| 200 base::Time earliest_end_time_; |
| 201 |
| 202 AudioParameters audio_parameters_; |
| 203 |
167 AudioDecoder::ReadCB read_cb_; | 204 AudioDecoder::ReadCB read_cb_; |
168 | 205 |
169 DISALLOW_COPY_AND_ASSIGN(AudioRendererBase); | 206 DISALLOW_COPY_AND_ASSIGN(AudioRendererBase); |
170 }; | 207 }; |
171 | 208 |
172 } // namespace media | 209 } // namespace media |
173 | 210 |
174 #endif // MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ | 211 #endif // MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ |
OLD | NEW |