| 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; | |
| 29 class TimeSource; | 28 class TimeSource; |
| 30 class VideoRenderer; | 29 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. |get_duration_cb| is used to get the duration of the | 37 // part of JS sync API. |
| 38 // stream. | |
| 39 RendererImpl(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 38 RendererImpl(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 40 Demuxer* demuxer, | 39 Demuxer* demuxer, |
| 41 scoped_ptr<AudioRenderer> audio_renderer, | 40 scoped_ptr<AudioRenderer> audio_renderer, |
| 42 scoped_ptr<VideoRenderer> video_renderer); | 41 scoped_ptr<VideoRenderer> video_renderer); |
| 43 | 42 |
| 44 virtual ~RendererImpl(); | 43 virtual ~RendererImpl(); |
| 45 | 44 |
| 46 // Renderer implementation. | 45 // Renderer implementation. |
| 47 virtual void Initialize(const base::Closure& init_cb, | 46 virtual void Initialize(const base::Closure& init_cb, |
| 48 const StatisticsCB& statistics_cb, | 47 const StatisticsCB& statistics_cb, |
| 49 const base::Closure& ended_cb, | 48 const base::Closure& ended_cb, |
| 50 const PipelineStatusCB& error_cb, | 49 const PipelineStatusCB& error_cb, |
| 51 const BufferingStateCB& buffering_state_cb, | 50 const BufferingStateCB& buffering_state_cb) OVERRIDE; |
| 52 const TimeDeltaCB& get_duration_cb) OVERRIDE; | |
| 53 virtual void Flush(const base::Closure& flush_cb) OVERRIDE; | 51 virtual void Flush(const base::Closure& flush_cb) OVERRIDE; |
| 54 virtual void StartPlayingFrom(base::TimeDelta time) OVERRIDE; | 52 virtual void StartPlayingFrom(base::TimeDelta time) OVERRIDE; |
| 55 virtual void SetPlaybackRate(float playback_rate) OVERRIDE; | 53 virtual void SetPlaybackRate(float playback_rate) OVERRIDE; |
| 56 virtual void SetVolume(float volume) OVERRIDE; | 54 virtual void SetVolume(float volume) OVERRIDE; |
| 57 virtual base::TimeDelta GetMediaTime() OVERRIDE; | 55 virtual base::TimeDelta GetMediaTime() OVERRIDE; |
| 58 virtual bool HasAudio() OVERRIDE; | 56 virtual bool HasAudio() OVERRIDE; |
| 59 virtual bool HasVideo() OVERRIDE; | 57 virtual bool HasVideo() OVERRIDE; |
| 60 virtual void SetCdm(MediaKeys* cdm) OVERRIDE; | 58 virtual void SetCdm(MediaKeys* cdm) OVERRIDE; |
| 61 | 59 |
| 62 // Helper functions for testing purposes. Must be called before Initialize(). | 60 // Helper functions for testing purposes. Must be called before Initialize(). |
| 63 void DisableUnderflowForTesting(); | 61 void DisableUnderflowForTesting(); |
| 64 void SetTimeDeltaInterpolatorForTesting(TimeDeltaInterpolator* interpolator); | 62 void EnableClocklessVideoPlaybackForTesting(); |
| 65 | 63 |
| 66 private: | 64 private: |
| 67 enum State { | 65 enum State { |
| 68 STATE_UNINITIALIZED, | 66 STATE_UNINITIALIZED, |
| 69 STATE_INITIALIZING, | 67 STATE_INITIALIZING, |
| 70 STATE_FLUSHING, | 68 STATE_FLUSHING, |
| 71 STATE_PLAYING, | 69 STATE_PLAYING, |
| 72 STATE_ERROR | 70 STATE_ERROR |
| 73 }; | 71 }; |
| 74 | 72 |
| 75 base::TimeDelta GetMediaDuration(); | 73 base::TimeDelta GetMediaTimeForSyncingVideo(); |
| 76 | 74 |
| 77 // Helper functions and callbacks for Initialize(). | 75 // Helper functions and callbacks for Initialize(). |
| 78 void InitializeAudioRenderer(); | 76 void InitializeAudioRenderer(); |
| 79 void OnAudioRendererInitializeDone(PipelineStatus status); | 77 void OnAudioRendererInitializeDone(PipelineStatus status); |
| 80 void InitializeVideoRenderer(); | 78 void InitializeVideoRenderer(); |
| 81 void OnVideoRendererInitializeDone(PipelineStatus status); | 79 void OnVideoRendererInitializeDone(PipelineStatus status); |
| 82 | 80 |
| 83 // Helper functions and callbacks for Flush(). | 81 // Helper functions and callbacks for Flush(). |
| 84 void FlushAudioRenderer(); | 82 void FlushAudioRenderer(); |
| 85 void OnAudioRendererFlushDone(); | 83 void OnAudioRendererFlushDone(); |
| 86 void FlushVideoRenderer(); | 84 void FlushVideoRenderer(); |
| 87 void OnVideoRendererFlushDone(); | 85 void OnVideoRendererFlushDone(); |
| 88 | 86 |
| 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 | |
| 95 // Callback executed by filters to update statistics. | 87 // Callback executed by filters to update statistics. |
| 96 void OnUpdateStatistics(const PipelineStatistics& stats); | 88 void OnUpdateStatistics(const PipelineStatistics& stats); |
| 97 | 89 |
| 98 // Collection of callback methods and helpers for tracking changes in | 90 // Collection of callback methods and helpers for tracking changes in |
| 99 // buffering state and transition from paused/underflow states and playing | 91 // buffering state and transition from paused/underflow states and playing |
| 100 // states. | 92 // states. |
| 101 // | 93 // |
| 102 // While in the kPlaying state: | 94 // While in the kPlaying state: |
| 103 // - A waiting to non-waiting transition indicates preroll has completed | 95 // - A waiting to non-waiting transition indicates preroll has completed |
| 104 // and StartPlayback() should be called | 96 // and StartPlayback() should be called |
| 105 // - A non-waiting to waiting transition indicates underflow has occurred | 97 // - A non-waiting to waiting transition indicates underflow has occurred |
| 106 // and PausePlayback() should be called | 98 // and PausePlayback() should be called |
| 107 void OnBufferingStateChanged(BufferingState* buffering_state, | 99 void OnBufferingStateChanged(BufferingState* buffering_state, |
| 108 BufferingState new_buffering_state); | 100 BufferingState new_buffering_state); |
| 109 bool WaitingForEnoughData() const; | 101 bool WaitingForEnoughData() const; |
| 110 void PausePlayback(); | 102 void PausePlayback(); |
| 111 void StartPlayback(); | 103 void StartPlayback(); |
| 112 | 104 |
| 113 void PauseClockAndStopTicking_Locked(); | |
| 114 void StartClockIfWaitingForTimeUpdate_Locked(); | |
| 115 | |
| 116 // Callbacks executed when a renderer has ended. | 105 // Callbacks executed when a renderer has ended. |
| 117 void OnAudioRendererEnded(); | 106 void OnAudioRendererEnded(); |
| 118 void OnVideoRendererEnded(); | 107 void OnVideoRendererEnded(); |
| 108 bool PlaybackHasEnded() const; |
| 119 void RunEndedCallbackIfNeeded(); | 109 void RunEndedCallbackIfNeeded(); |
| 120 | 110 |
| 121 // Callback executed when a runtime error happens. | 111 // Callback executed when a runtime error happens. |
| 122 void OnError(PipelineStatus error); | 112 void OnError(PipelineStatus error); |
| 123 | 113 |
| 124 void FireAllPendingCallbacks(); | 114 void FireAllPendingCallbacks(); |
| 125 | 115 |
| 126 State state_; | 116 State state_; |
| 127 | 117 |
| 128 // Task runner used to execute pipeline tasks. | 118 // Task runner used to execute pipeline tasks. |
| 129 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 119 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 130 | 120 |
| 131 Demuxer* demuxer_; | 121 Demuxer* demuxer_; |
| 132 | 122 |
| 133 // Permanent callback to get the media duration. | |
| 134 TimeDeltaCB get_duration_cb_; | |
| 135 | |
| 136 // Permanent callbacks to notify various renderer states/stats. | 123 // Permanent callbacks to notify various renderer states/stats. |
| 137 StatisticsCB statistics_cb_; | 124 StatisticsCB statistics_cb_; |
| 138 base::Closure ended_cb_; | 125 base::Closure ended_cb_; |
| 139 PipelineStatusCB error_cb_; | 126 PipelineStatusCB error_cb_; |
| 140 BufferingStateCB buffering_state_cb_; | 127 BufferingStateCB buffering_state_cb_; |
| 141 | 128 |
| 142 // Temporary callback used for Initialize() and Flush(). | 129 // Temporary callback used for Initialize() and Flush(). |
| 143 base::Closure init_cb_; | 130 base::Closure init_cb_; |
| 144 base::Closure flush_cb_; | 131 base::Closure flush_cb_; |
| 145 | 132 |
| 146 scoped_ptr<AudioRenderer> audio_renderer_; | 133 scoped_ptr<AudioRenderer> audio_renderer_; |
| 147 scoped_ptr<VideoRenderer> video_renderer_; | 134 scoped_ptr<VideoRenderer> video_renderer_; |
| 148 | 135 |
| 149 // Renderer-provided time source used to control playback. | 136 // Renderer-provided time source used to control playback. |
| 150 TimeSource* time_source_; | 137 TimeSource* time_source_; |
| 138 scoped_ptr<WallClockTimeSource> wall_clock_time_source_; |
| 139 bool time_ticking_; |
| 151 | 140 |
| 152 // The time to start playback from after starting/seeking has completed. | 141 // The time to start playback from after starting/seeking has completed. |
| 153 base::TimeDelta start_time_; | 142 base::TimeDelta start_time_; |
| 154 | 143 |
| 155 BufferingState audio_buffering_state_; | 144 BufferingState audio_buffering_state_; |
| 156 BufferingState video_buffering_state_; | 145 BufferingState video_buffering_state_; |
| 157 | 146 |
| 158 // Whether we've received the audio/video ended events. | 147 // Whether we've received the audio/video ended events. |
| 159 bool audio_ended_; | 148 bool audio_ended_; |
| 160 bool video_ended_; | 149 bool video_ended_; |
| 161 | 150 |
| 162 bool underflow_disabled_for_testing_; | 151 bool underflow_disabled_for_testing_; |
| 163 | 152 bool clockless_video_playback_enabled_for_testing_; |
| 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_; | |
| 189 | 153 |
| 190 // NOTE: Weak pointers must be invalidated before all other member variables. | 154 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 191 base::WeakPtrFactory<RendererImpl> weak_factory_; | 155 base::WeakPtrFactory<RendererImpl> weak_factory_; |
| 192 base::WeakPtr<RendererImpl> weak_this_; | 156 base::WeakPtr<RendererImpl> weak_this_; |
| 193 | 157 |
| 194 DISALLOW_COPY_AND_ASSIGN(RendererImpl); | 158 DISALLOW_COPY_AND_ASSIGN(RendererImpl); |
| 195 }; | 159 }; |
| 196 | 160 |
| 197 } // namespace media | 161 } // namespace media |
| 198 | 162 |
| 199 #endif // MEDIA_FILTERS_RENDERER_IMPL_H_ | 163 #endif // MEDIA_FILTERS_RENDERER_IMPL_H_ |
| OLD | NEW |