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