| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Audio rendering unit utilizing an AudioRendererSink to output data. | |
| 6 // | |
| 7 // This class lives inside three threads during it's lifetime, namely: | |
| 8 // 1. Render thread. | |
| 9 // This object is created on the render thread. | |
| 10 // 2. Pipeline thread | |
| 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. | |
| 19 | |
| 20 #ifndef MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ | |
| 21 #define MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ | |
| 22 | |
| 23 #include <deque> | |
| 24 | |
| 25 #include "base/synchronization/lock.h" | |
| 26 #include "media/base/audio_decoder.h" | |
| 27 #include "media/base/audio_renderer_sink.h" | |
| 28 #include "media/base/buffers.h" | |
| 29 #include "media/base/filters.h" | |
| 30 #include "media/filters/audio_renderer_algorithm_base.h" | |
| 31 | |
| 32 namespace media { | |
| 33 | |
| 34 class MEDIA_EXPORT AudioRendererBase | |
| 35 : public AudioRenderer, | |
| 36 NON_EXPORTED_BASE(public media::AudioRendererSink::RenderCallback) { | |
| 37 public: | |
| 38 // Methods called on Render thread ------------------------------------------ | |
| 39 // An AudioRendererSink is used as the destination for the rendered audio. | |
| 40 explicit AudioRendererBase(media::AudioRendererSink* sink); | |
| 41 virtual ~AudioRendererBase(); | |
| 42 | |
| 43 // Methods called on pipeline thread ---------------------------------------- | |
| 44 // Filter implementation. | |
| 45 virtual void Play(const base::Closure& callback) OVERRIDE; | |
| 46 virtual void Pause(const base::Closure& callback) OVERRIDE; | |
| 47 virtual void Flush(const base::Closure& callback) OVERRIDE; | |
| 48 virtual void Stop(const base::Closure& callback) OVERRIDE; | |
| 49 virtual void SetPlaybackRate(float rate) OVERRIDE; | |
| 50 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE; | |
| 51 | |
| 52 // AudioRenderer implementation. | |
| 53 virtual void Initialize(const scoped_refptr<AudioDecoder>& decoder, | |
| 54 const PipelineStatusCB& init_cb, | |
| 55 const base::Closure& underflow_cb, | |
| 56 const TimeCB& time_cb) OVERRIDE; | |
| 57 virtual bool HasEnded() OVERRIDE; | |
| 58 virtual void ResumeAfterUnderflow(bool buffer_more_audio) OVERRIDE; | |
| 59 virtual void SetVolume(float volume) OVERRIDE; | |
| 60 | |
| 61 private: | |
| 62 friend class AudioRendererBaseTest; | |
| 63 FRIEND_TEST_ALL_PREFIXES(AudioRendererBaseTest, EndOfStream); | |
| 64 FRIEND_TEST_ALL_PREFIXES(AudioRendererBaseTest, Underflow_EndOfStream); | |
| 65 | |
| 66 // Callback from the audio decoder delivering decoded audio samples. | |
| 67 void DecodedAudioReady(scoped_refptr<Buffer> buffer); | |
| 68 | |
| 69 // Fills the given buffer with audio data by delegating to its |algorithm_|. | |
| 70 // FillBuffer() also takes care of updating the clock. Returns the number of | |
| 71 // frames copied into |dest|, which may be less than or equal to | |
| 72 // |requested_frames|. | |
| 73 // | |
| 74 // If this method returns fewer frames than |requested_frames|, it could | |
| 75 // be a sign that the pipeline is stalled or unable to stream the data fast | |
| 76 // enough. In such scenarios, the callee should zero out unused portions | |
| 77 // of their buffer to playback silence. | |
| 78 // | |
| 79 // FillBuffer() updates the pipeline's playback timestamp. If FillBuffer() is | |
| 80 // not called at the same rate as audio samples are played, then the reported | |
| 81 // timestamp in the pipeline will be ahead of the actual audio playback. In | |
| 82 // this case |playback_delay| should be used to indicate when in the future | |
| 83 // should the filled buffer be played. If FillBuffer() is called as the audio | |
| 84 // hardware plays the buffer, then |playback_delay| should be zero. | |
| 85 // | |
| 86 // FillBuffer() calls SignalEndOfStream() when it reaches end of stream. | |
| 87 // | |
| 88 // Safe to call on any thread. | |
| 89 uint32 FillBuffer(uint8* dest, | |
| 90 uint32 requested_frames, | |
| 91 const base::TimeDelta& playback_delay); | |
| 92 | |
| 93 // Called at the end of stream when all the hardware buffers become empty | |
| 94 // (i.e. when all the data written to the device has been played). | |
| 95 void SignalEndOfStream(); | |
| 96 | |
| 97 // Get the playback rate of |algorithm_|. | |
| 98 float GetPlaybackRate(); | |
| 99 | |
| 100 // Convert number of bytes to duration of time using information about the | |
| 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; | |
| 119 | |
| 120 // Helper method that schedules an asynchronous read from the decoder and | |
| 121 // increments |pending_reads_|. | |
| 122 // | |
| 123 // Safe to call from any thread. | |
| 124 void ScheduleRead_Locked(); | |
| 125 | |
| 126 // Returns true if the data in the buffer is all before | |
| 127 // |seek_timestamp_|. This can only return true while | |
| 128 // in the kSeeking state. | |
| 129 bool IsBeforeSeekTime(const scoped_refptr<Buffer>& buffer); | |
| 130 | |
| 131 // Audio decoder. | |
| 132 scoped_refptr<AudioDecoder> decoder_; | |
| 133 | |
| 134 // Algorithm for scaling audio. | |
| 135 scoped_ptr<AudioRendererAlgorithmBase> algorithm_; | |
| 136 | |
| 137 base::Lock lock_; | |
| 138 | |
| 139 // Simple state tracking variable. | |
| 140 enum State { | |
| 141 kUninitialized, | |
| 142 kPaused, | |
| 143 kSeeking, | |
| 144 kPlaying, | |
| 145 kStopped, | |
| 146 kUnderflow, | |
| 147 kRebuffering, | |
| 148 }; | |
| 149 State state_; | |
| 150 | |
| 151 // Keep track of our outstanding read to |decoder_|. | |
| 152 bool pending_read_; | |
| 153 | |
| 154 // Keeps track of whether we received and rendered the end of stream buffer. | |
| 155 bool received_end_of_stream_; | |
| 156 bool rendered_end_of_stream_; | |
| 157 | |
| 158 // The timestamp of the last frame (i.e. furthest in the future) buffered. | |
| 159 // TODO(ralphl): Update this value after seeking. | |
| 160 base::TimeDelta audio_time_buffered_; | |
| 161 | |
| 162 // Filter callbacks. | |
| 163 base::Closure pause_cb_; | |
| 164 PipelineStatusCB seek_cb_; | |
| 165 | |
| 166 base::Closure underflow_cb_; | |
| 167 | |
| 168 TimeCB time_cb_; | |
| 169 | |
| 170 base::TimeDelta seek_timestamp_; | |
| 171 | |
| 172 uint32 bytes_per_frame_; | |
| 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 | |
| 204 AudioDecoder::ReadCB read_cb_; | |
| 205 | |
| 206 DISALLOW_COPY_AND_ASSIGN(AudioRendererBase); | |
| 207 }; | |
| 208 | |
| 209 } // namespace media | |
| 210 | |
| 211 #endif // MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ | |
| OLD | NEW |