| 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 // Audio rendering unit utilizing an AudioRendererSink to output data. | 5 // Audio rendering unit utilizing an AudioRendererSink to output data. |
| 6 // | 6 // |
| 7 // This class lives inside three threads during it's lifetime, namely: | 7 // This class lives inside three threads during it's lifetime, namely: |
| 8 // 1. Render thread | 8 // 1. Render thread |
| 9 // Where the object is created. | 9 // Where the object is created. |
| 10 // 2. Media thread (provided via constructor) | 10 // 2. Media thread (provided via constructor) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
| 27 #include "media/base/audio_decoder.h" | 27 #include "media/base/audio_decoder.h" |
| 28 #include "media/base/audio_renderer.h" | 28 #include "media/base/audio_renderer.h" |
| 29 #include "media/base/audio_renderer_sink.h" | 29 #include "media/base/audio_renderer_sink.h" |
| 30 #include "media/base/decryptor.h" | 30 #include "media/base/decryptor.h" |
| 31 #include "media/filters/audio_renderer_algorithm.h" | 31 #include "media/filters/audio_renderer_algorithm.h" |
| 32 #include "media/filters/decoder_stream.h" | 32 #include "media/filters/decoder_stream.h" |
| 33 | 33 |
| 34 namespace base { | 34 namespace base { |
| 35 class SingleThreadTaskRunner; | 35 class SingleThreadTaskRunner; |
| 36 class TickClock; |
| 36 } | 37 } |
| 37 | 38 |
| 38 namespace media { | 39 namespace media { |
| 39 | 40 |
| 40 class AudioBufferConverter; | 41 class AudioBufferConverter; |
| 41 class AudioBus; | 42 class AudioBus; |
| 42 class AudioClock; | 43 class AudioClock; |
| 43 class AudioHardwareConfig; | 44 class AudioHardwareConfig; |
| 44 class AudioSplicer; | 45 class AudioSplicer; |
| 45 class DecryptingDemuxerStream; | 46 class DecryptingDemuxerStream; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 73 const base::Closure& ended_cb, | 74 const base::Closure& ended_cb, |
| 74 const PipelineStatusCB& error_cb) OVERRIDE; | 75 const PipelineStatusCB& error_cb) OVERRIDE; |
| 75 virtual void StartRendering() OVERRIDE; | 76 virtual void StartRendering() OVERRIDE; |
| 76 virtual void StopRendering() OVERRIDE; | 77 virtual void StopRendering() OVERRIDE; |
| 77 virtual void Flush(const base::Closure& callback) OVERRIDE; | 78 virtual void Flush(const base::Closure& callback) OVERRIDE; |
| 78 virtual void Stop(const base::Closure& callback) OVERRIDE; | 79 virtual void Stop(const base::Closure& callback) OVERRIDE; |
| 79 virtual void SetPlaybackRate(float rate) OVERRIDE; | 80 virtual void SetPlaybackRate(float rate) OVERRIDE; |
| 80 virtual void StartPlayingFrom(base::TimeDelta timestamp) OVERRIDE; | 81 virtual void StartPlayingFrom(base::TimeDelta timestamp) OVERRIDE; |
| 81 virtual void SetVolume(float volume) OVERRIDE; | 82 virtual void SetVolume(float volume) OVERRIDE; |
| 82 | 83 |
| 83 // Allows injection of a custom time callback for non-realtime testing. | 84 void SetTickClockForTesting(scoped_ptr<base::TickClock> tick_clock); |
| 84 typedef base::Callback<base::TimeTicks()> NowCB; | |
| 85 void set_now_cb_for_testing(const NowCB& now_cb) { | |
| 86 now_cb_ = now_cb; | |
| 87 } | |
| 88 | 85 |
| 89 private: | 86 private: |
| 90 friend class AudioRendererImplTest; | 87 friend class AudioRendererImplTest; |
| 91 | 88 |
| 92 // Important detail: being in kPlaying doesn't imply that audio is being | 89 // Important detail: being in kPlaying doesn't imply that audio is being |
| 93 // rendered. Rather, it means that the renderer is ready to go. The actual | 90 // rendered. Rather, it means that the renderer is ready to go. The actual |
| 94 // rendering of audio is controlled via Start/StopRendering(). | 91 // rendering of audio is controlled via Start/StopRendering(). |
| 95 // | 92 // |
| 96 // kUninitialized | 93 // kUninitialized |
| 97 // | Initialize() | 94 // | Initialize() |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 // | 143 // |
| 147 // Render() updates the pipeline's playback timestamp. If Render() is | 144 // Render() updates the pipeline's playback timestamp. If Render() is |
| 148 // not called at the same rate as audio samples are played, then the reported | 145 // not called at the same rate as audio samples are played, then the reported |
| 149 // timestamp in the pipeline will be ahead of the actual audio playback. In | 146 // timestamp in the pipeline will be ahead of the actual audio playback. In |
| 150 // this case |audio_delay_milliseconds| should be used to indicate when in the | 147 // this case |audio_delay_milliseconds| should be used to indicate when in the |
| 151 // future should the filled buffer be played. | 148 // future should the filled buffer be played. |
| 152 virtual int Render(AudioBus* audio_bus, | 149 virtual int Render(AudioBus* audio_bus, |
| 153 int audio_delay_milliseconds) OVERRIDE; | 150 int audio_delay_milliseconds) OVERRIDE; |
| 154 virtual void OnRenderError() OVERRIDE; | 151 virtual void OnRenderError() OVERRIDE; |
| 155 | 152 |
| 153 // Contains result of the last call to Render(). Used to update |audio_clock_| |
| 154 // and changes in buffering state. |
| 155 struct RenderResult { |
| 156 RenderResult(); |
| 157 |
| 158 base::TimeTicks ticks; |
| 159 int requested_frames; |
| 160 int delay_frames; |
| 161 int frames_written; |
| 162 float playback_rate; |
| 163 base::TimeDelta endpoint_timestamp; |
| 164 }; |
| 165 void DidRender(RenderResult result); |
| 166 |
| 156 // Helper methods that schedule an asynchronous read from the decoder as long | 167 // Helper methods that schedule an asynchronous read from the decoder as long |
| 157 // as there isn't a pending read. | 168 // as there isn't a pending read. |
| 158 // | 169 // |
| 159 // Must be called on |task_runner_|. | 170 // Must be called on |task_runner_|. |
| 160 void AttemptRead(); | |
| 161 void AttemptRead_Locked(); | 171 void AttemptRead_Locked(); |
| 162 bool CanRead_Locked(); | |
| 163 void ChangeState_Locked(State new_state); | 172 void ChangeState_Locked(State new_state); |
| 164 | 173 |
| 165 // Returns true if the data in the buffer is all before |start_timestamp_|. | 174 // Returns true if the data in the buffer is all before |start_timestamp_|. |
| 166 // This can only return true while in the kPlaying state. | 175 // This can only return true while in the kPlaying state. |
| 167 bool IsBeforeStartTime(const scoped_refptr<AudioBuffer>& buffer); | 176 bool IsBeforeStartTime(const scoped_refptr<AudioBuffer>& buffer); |
| 168 | 177 |
| 169 // Called upon AudioBufferStream initialization, or failure thereof (indicated | 178 // Called upon AudioBufferStream initialization, or failure thereof (indicated |
| 170 // by the value of |success|). | 179 // by the value of |success|). |
| 171 void OnAudioBufferStreamInitialized(bool succes); | 180 void OnAudioBufferStreamInitialized(bool succes); |
| 172 | 181 |
| 173 // Used to initiate the flush operation once all pending reads have | 182 // Used to initiate the flush operation once all pending reads have |
| 174 // completed. | 183 // completed. |
| 175 void DoFlush_Locked(); | 184 void DoFlush_Locked(); |
| 176 | 185 |
| 177 // Calls |decoder_|.Reset() and arranges for ResetDecoderDone() to get | 186 // Calls |decoder_|.Reset() and arranges for ResetDecoderDone() to get |
| 178 // called when the reset completes. | 187 // called when the reset completes. |
| 179 void ResetDecoder(); | 188 void ResetDecoder(); |
| 180 | 189 |
| 181 // Called when the |decoder_|.Reset() has completed. | 190 // Called when the |decoder_|.Reset() has completed. |
| 182 void ResetDecoderDone(); | 191 void ResetDecoderDone(); |
| 183 | 192 |
| 184 // Called by the AudioBufferStream when a splice buffer is demuxed. | 193 // Called by the AudioBufferStream when a splice buffer is demuxed. |
| 185 void OnNewSpliceBuffer(base::TimeDelta); | 194 void OnNewSpliceBuffer(base::TimeDelta); |
| 186 | 195 |
| 187 // Called by the AudioBufferStream when a config change occurs. | 196 // Called by the AudioBufferStream when a config change occurs. |
| 188 void OnConfigChange(); | 197 void OnConfigChange(); |
| 189 | 198 |
| 190 // Updates |buffering_state_| and fires |buffering_state_cb_|. | 199 // Updates |buffering_state_| and fires |buffering_state_cb_|. |
| 191 void SetBufferingState_Locked(BufferingState buffering_state); | 200 void SetBufferingState(BufferingState buffering_state); |
| 192 | 201 |
| 193 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 202 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 194 | 203 |
| 195 scoped_ptr<AudioSplicer> splicer_; | 204 scoped_ptr<AudioSplicer> splicer_; |
| 196 scoped_ptr<AudioBufferConverter> buffer_converter_; | 205 scoped_ptr<AudioBufferConverter> buffer_converter_; |
| 197 | 206 |
| 198 // Whether or not we expect to handle config changes. | 207 // Whether or not we expect to handle config changes. |
| 199 bool expecting_config_changes_; | 208 bool expecting_config_changes_; |
| 200 | 209 |
| 201 // The sink (destination) for rendered audio. |sink_| must only be accessed | 210 // The sink (destination) for rendered audio. |sink_| must only be accessed |
| (...skipping 12 matching lines...) Expand all Loading... |
| 214 // Callbacks provided during Initialize(). | 223 // Callbacks provided during Initialize(). |
| 215 PipelineStatusCB init_cb_; | 224 PipelineStatusCB init_cb_; |
| 216 TimeCB time_cb_; | 225 TimeCB time_cb_; |
| 217 BufferingStateCB buffering_state_cb_; | 226 BufferingStateCB buffering_state_cb_; |
| 218 base::Closure ended_cb_; | 227 base::Closure ended_cb_; |
| 219 PipelineStatusCB error_cb_; | 228 PipelineStatusCB error_cb_; |
| 220 | 229 |
| 221 // Callback provided to Flush(). | 230 // Callback provided to Flush(). |
| 222 base::Closure flush_cb_; | 231 base::Closure flush_cb_; |
| 223 | 232 |
| 224 // Typically calls base::TimeTicks::Now() but can be overridden by a test. | 233 scoped_ptr<base::TickClock> tick_clock_; |
| 225 NowCB now_cb_; | |
| 226 | 234 |
| 227 // After Initialize() has completed, all variables below must be accessed | 235 // After Initialize() has completed, all variables below must be accessed |
| 228 // under |lock_|. ------------------------------------------------------------ | 236 // under |lock_|. ------------------------------------------------------------ |
| 229 base::Lock lock_; | 237 base::Lock lock_; |
| 230 | 238 |
| 231 // Algorithm for scaling audio. | 239 // Algorithm for scaling audio. |
| 232 scoped_ptr<AudioRendererAlgorithm> algorithm_; | 240 scoped_ptr<AudioRendererAlgorithm> algorithm_; |
| 233 | 241 |
| 234 // Simple state tracking variable. | 242 // Simple state tracking variable. |
| 235 State state_; | 243 State state_; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 256 | 264 |
| 257 // NOTE: Weak pointers must be invalidated before all other member variables. | 265 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 258 base::WeakPtrFactory<AudioRendererImpl> weak_factory_; | 266 base::WeakPtrFactory<AudioRendererImpl> weak_factory_; |
| 259 | 267 |
| 260 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); | 268 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); |
| 261 }; | 269 }; |
| 262 | 270 |
| 263 } // namespace media | 271 } // namespace media |
| 264 | 272 |
| 265 #endif // MEDIA_FILTERS_AUDIO_RENDERER_IMPL_H_ | 273 #endif // MEDIA_FILTERS_AUDIO_RENDERER_IMPL_H_ |
| OLD | NEW |