OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 #ifndef MEDIA_FILTERS_RENDERER_IMPL_H_ |
| 6 #define MEDIA_FILTERS_RENDERER_IMPL_H_ |
| 7 |
| 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/synchronization/lock.h" |
| 12 #include "base/time/clock.h" |
| 13 #include "base/time/default_tick_clock.h" |
| 14 #include "base/time/time.h" |
| 15 #include "media/base/buffering_state.h" |
| 16 #include "media/base/media_export.h" |
| 17 #include "media/base/pipeline_status.h" |
| 18 #include "media/base/renderer.h" |
| 19 |
| 20 namespace base { |
| 21 class SingleThreadTaskRunner; |
| 22 } |
| 23 |
| 24 namespace media { |
| 25 |
| 26 class AudioRenderer; |
| 27 class Demuxer; |
| 28 class FilterCollection; |
| 29 class TimeDeltaInterpolator; |
| 30 class TimeSource; |
| 31 class VideoRenderer; |
| 32 |
| 33 class MEDIA_EXPORT RendererImpl : public Renderer { |
| 34 public: |
| 35 typedef base::Callback<base::TimeDelta()> TimeDeltaCB; |
| 36 |
| 37 // Renders audio/video streams in |demuxer| using AudioRenderer and |
| 38 // VideoRenderer provided in |filter_collection|. All methods except for |
| 39 // GetMediaTime() run on the |task_runner|. GetMediaTime() runs on the render |
| 40 // main thread because it's part of JS sync API. |
| 41 // |get_duration_cb| is used to get the duration of the stream. |
| 42 RendererImpl(Demuxer* demuxer, |
| 43 scoped_ptr<FilterCollection> filter_collection, |
| 44 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 45 const TimeDeltaCB& get_duration_cb); |
| 46 |
| 47 // Stops rendering and fires all pending callbacks. |
| 48 virtual ~RendererImpl(); |
| 49 |
| 50 // Renderer implementation. |
| 51 virtual void Initialize(const PipelineStatusCB& init_cb, |
| 52 const StatisticsCB& statistics_cb, |
| 53 const base::Closure& ended_cb, |
| 54 const PipelineStatusCB& error_cb, |
| 55 const BufferingStateCB& buffering_state_cb) OVERRIDE; |
| 56 virtual void Flush(const base::Closure& flush_cb) OVERRIDE; |
| 57 virtual void StartPlayingFrom(base::TimeDelta timestamp) OVERRIDE; |
| 58 virtual void SetPlaybackRate(float playback_rate) OVERRIDE; |
| 59 virtual void SetVolume(float volume) OVERRIDE; |
| 60 virtual base::TimeDelta GetMediaTime() const OVERRIDE; |
| 61 virtual bool HasAudio() const OVERRIDE; |
| 62 virtual bool HasVideo() const OVERRIDE; |
| 63 virtual void SetCdm(MediaKeys* cdm) OVERRIDE; |
| 64 virtual void DisableUnderflowForTesting() OVERRIDE; |
| 65 virtual void SetTimeDeltaInterpolatorForTesting( |
| 66 TimeDeltaInterpolator* interpolator) OVERRIDE; |
| 67 |
| 68 private: |
| 69 enum State { |
| 70 kUninitialized, |
| 71 kInitializing, |
| 72 kFlushing, |
| 73 kPlaying, |
| 74 kError |
| 75 }; |
| 76 |
| 77 base::TimeDelta GetMediaDuration(); |
| 78 |
| 79 // Helper functions and callbacks for Initialize(). |
| 80 void InitializeAudioRenderer(); |
| 81 void OnAudioRendererInitializeDone(PipelineStatus status); |
| 82 void InitializeVideoRenderer(); |
| 83 void OnVideoRendererInitializeDone(PipelineStatus status); |
| 84 |
| 85 // Helper functions and callbacks for Flush(). |
| 86 void FlushAudioRenderer(); |
| 87 void OnAudioRendererFlushDone(); |
| 88 void FlushVideoRenderer(); |
| 89 void OnVideoRendererFlushDone(); |
| 90 |
| 91 // Callback executed by audio renderer to update clock time. |
| 92 void OnAudioTimeUpdate(base::TimeDelta time, base::TimeDelta max_time); |
| 93 |
| 94 // Callback executed by video renderer to update clock time. |
| 95 void OnVideoTimeUpdate(base::TimeDelta max_time); |
| 96 |
| 97 // Callback executed by filters to update statistics. |
| 98 void OnUpdateStatistics(const PipelineStatistics& stats); |
| 99 |
| 100 // Collection of callback methods and helpers for tracking changes in |
| 101 // buffering state and transition from paused/underflow states and playing |
| 102 // states. |
| 103 // |
| 104 // While in the kPlaying state: |
| 105 // - A waiting to non-waiting transition indicates preroll has completed |
| 106 // and StartPlayback() should be called |
| 107 // - A non-waiting to waiting transition indicates underflow has occurred |
| 108 // and PausePlayback() should be called |
| 109 void OnBufferingStateChanged(BufferingState* buffering_state, |
| 110 BufferingState new_buffering_state); |
| 111 bool WaitingForEnoughData() const; |
| 112 void PausePlayback(); |
| 113 void StartPlayback(); |
| 114 |
| 115 void PauseClockAndStopTicking_Locked(); |
| 116 void StartClockIfWaitingForTimeUpdate_Locked(); |
| 117 |
| 118 // Callbacks executed when a renderer has ended. |
| 119 void OnAudioRendererEnded(); |
| 120 void OnVideoRendererEnded(); |
| 121 void RunEndedCallbackIfNeeded(); |
| 122 |
| 123 // Callback executed when a runtime error happens. |
| 124 void OnError(PipelineStatus error); |
| 125 |
| 126 State state_; |
| 127 |
| 128 // Task runner used to execute pipeline tasks. |
| 129 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 130 |
| 131 scoped_ptr<FilterCollection> filter_collection_; |
| 132 Demuxer* demuxer_; |
| 133 |
| 134 // Permanent callback to get the media duration. |
| 135 TimeDeltaCB get_duration_cb_; |
| 136 |
| 137 // Permanent callbacks to notify various renderer states/stats. |
| 138 StatisticsCB statistics_cb_; |
| 139 base::Closure ended_cb_; |
| 140 PipelineStatusCB error_cb_; |
| 141 BufferingStateCB buffering_state_cb_; |
| 142 |
| 143 // Temporary callback used for Initialize() and Flush(). |
| 144 PipelineStatusCB init_cb_; |
| 145 base::Closure flush_cb_; |
| 146 |
| 147 scoped_ptr<AudioRenderer> audio_renderer_; |
| 148 scoped_ptr<VideoRenderer> video_renderer_; |
| 149 |
| 150 // Renderer-provided time source used to control playback. |
| 151 TimeSource* time_source_; |
| 152 |
| 153 // The timestamp to start playback from after starting/seeking has completed. |
| 154 base::TimeDelta start_timestamp_; |
| 155 |
| 156 BufferingState audio_buffering_state_; |
| 157 BufferingState video_buffering_state_; |
| 158 |
| 159 // Whether we've received the audio/video ended events. |
| 160 bool audio_ended_; |
| 161 bool video_ended_; |
| 162 |
| 163 bool underflow_disabled_for_testing_; |
| 164 |
| 165 // Protects time interpolation related member variables, i.e. |interpolator_|, |
| 166 // |default_tick_clock_| and |interpolation_state_|. This is because |
| 167 // |interpolator_| can be used on different threads (see GetMediaTime()). |
| 168 mutable base::Lock interpolator_lock_; |
| 169 |
| 170 // Tracks the most recent media time update and provides interpolated values |
| 171 // as playback progresses. |
| 172 scoped_ptr<TimeDeltaInterpolator> interpolator_; |
| 173 |
| 174 // base::TickClock used by |interpolator_|. |
| 175 // TODO(xhwang): This can be TimeDeltaInterpolator's implementation detail. |
| 176 base::DefaultTickClock default_tick_clock_; |
| 177 |
| 178 enum InterpolationState { |
| 179 // Audio (if present) is not rendering. Time isn't being interpolated. |
| 180 INTERPOLATION_STOPPED, |
| 181 |
| 182 // Audio (if present) is rendering. Time isn't being interpolated. |
| 183 INTERPOLATION_WAITING_FOR_AUDIO_TIME_UPDATE, |
| 184 |
| 185 // Audio (if present) is rendering. Time is being interpolated. |
| 186 INTERPOLATION_STARTED, |
| 187 }; |
| 188 |
| 189 InterpolationState interpolation_state_; |
| 190 |
| 191 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 192 base::WeakPtrFactory<RendererImpl> weak_factory_; |
| 193 base::WeakPtr<RendererImpl> weak_this_; |
| 194 |
| 195 DISALLOW_COPY_AND_ASSIGN(RendererImpl); |
| 196 }; |
| 197 |
| 198 } // namespace media |
| 199 |
| 200 #endif // MEDIA_FILTERS_RENDERER_IMPL_H_ |
OLD | NEW |