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