xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 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 | |
servolk | f54f5c8f | 2015-02-24 20:32:39 | [diff] [blame] | 5 | #ifndef MEDIA_RENDERERS_RENDERER_IMPL_H_ |
| 6 | #define MEDIA_RENDERERS_RENDERER_IMPL_H_ |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 7 | |
dalecurtis | c11e7bb | 2015-04-03 05:07:08 | [diff] [blame^] | 8 | #include "base/cancelable_callback.h" |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 9 | #include "base/memory/ref_counted.h" |
| 10 | #include "base/memory/scoped_ptr.h" |
| 11 | #include "base/memory/weak_ptr.h" |
| 12 | #include "base/synchronization/lock.h" |
| 13 | #include "base/time/clock.h" |
| 14 | #include "base/time/default_tick_clock.h" |
| 15 | #include "base/time/time.h" |
| 16 | #include "media/base/buffering_state.h" |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 17 | #include "media/base/decryptor.h" |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 18 | #include "media/base/media_export.h" |
| 19 | #include "media/base/pipeline_status.h" |
| 20 | #include "media/base/renderer.h" |
| 21 | |
| 22 | namespace base { |
| 23 | class SingleThreadTaskRunner; |
| 24 | } |
| 25 | |
| 26 | namespace media { |
| 27 | |
| 28 | class AudioRenderer; |
xhwang | 1c5668e | 2014-09-10 02:42:04 | [diff] [blame] | 29 | class DemuxerStreamProvider; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 30 | class TimeSource; |
| 31 | class VideoRenderer; |
scherkus | ece8945 | 2014-09-09 23:13:38 | [diff] [blame] | 32 | class WallClockTimeSource; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 33 | |
| 34 | class MEDIA_EXPORT RendererImpl : public Renderer { |
| 35 | public: |
xhwang | abd95fd | 2014-10-03 07:10:01 | [diff] [blame] | 36 | // Renders audio/video streams using |audio_renderer| and |video_renderer| |
| 37 | // provided. All methods except for GetMediaTime() run on the |task_runner|. |
| 38 | // GetMediaTime() runs on the render main thread because it's part of JS sync |
| 39 | // API. |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 40 | RendererImpl(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 41 | scoped_ptr<AudioRenderer> audio_renderer, |
| 42 | scoped_ptr<VideoRenderer> video_renderer); |
| 43 | |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 44 | ~RendererImpl() final; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 45 | |
| 46 | // Renderer implementation. |
dcheng | c245654 | 2014-10-21 12:23:27 | [diff] [blame] | 47 | void Initialize(DemuxerStreamProvider* demuxer_stream_provider, |
dalecurtis | 4704611 | 2015-01-23 22:58:20 | [diff] [blame] | 48 | const PipelineStatusCB& init_cb, |
dcheng | c245654 | 2014-10-21 12:23:27 | [diff] [blame] | 49 | const StatisticsCB& statistics_cb, |
xhwang | 94c0bd3 | 2014-11-13 20:49:31 | [diff] [blame] | 50 | const BufferingStateCB& buffering_state_cb, |
| 51 | const PaintCB& paint_cb, |
dcheng | c245654 | 2014-10-21 12:23:27 | [diff] [blame] | 52 | const base::Closure& ended_cb, |
jrummell | 74fc4f94 | 2015-03-02 22:48:27 | [diff] [blame] | 53 | const PipelineStatusCB& error_cb, |
| 54 | const base::Closure& waiting_for_decryption_key_cb) final; |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 55 | void SetCdm(CdmContext* cdm_context, |
| 56 | const CdmAttachedCB& cdm_attached_cb) final; |
| 57 | void Flush(const base::Closure& flush_cb) final; |
| 58 | void StartPlayingFrom(base::TimeDelta time) final; |
| 59 | void SetPlaybackRate(float playback_rate) final; |
| 60 | void SetVolume(float volume) final; |
| 61 | base::TimeDelta GetMediaTime() final; |
| 62 | bool HasAudio() final; |
| 63 | bool HasVideo() final; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 64 | |
| 65 | // Helper functions for testing purposes. Must be called before Initialize(). |
| 66 | void DisableUnderflowForTesting(); |
scherkus | ece8945 | 2014-09-09 23:13:38 | [diff] [blame] | 67 | void EnableClocklessVideoPlaybackForTesting(); |
dalecurtis | c11e7bb | 2015-04-03 05:07:08 | [diff] [blame^] | 68 | void set_time_source_for_testing(TimeSource* time_source) { |
| 69 | time_source_ = time_source; |
| 70 | } |
| 71 | void set_video_underflow_threshold_for_testing(base::TimeDelta threshold) { |
| 72 | video_underflow_threshold_ = threshold; |
| 73 | } |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 74 | |
| 75 | private: |
| 76 | enum State { |
| 77 | STATE_UNINITIALIZED, |
| 78 | STATE_INITIALIZING, |
| 79 | STATE_FLUSHING, |
| 80 | STATE_PLAYING, |
| 81 | STATE_ERROR |
| 82 | }; |
| 83 | |
dalecurtis | 5755042 | 2015-03-26 20:31:22 | [diff] [blame] | 84 | base::TimeTicks GetWallClockTime(base::TimeDelta time); |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 85 | |
| 86 | // Requests that this object notifies when a decryptor is ready through the |
| 87 | // |decryptor_ready_cb| provided. |
| 88 | // If |decryptor_ready_cb| is null, the existing callback will be fired with |
| 89 | // nullptr immediately and reset. |
| 90 | void SetDecryptorReadyCallback(const DecryptorReadyCB& decryptor_ready_cb); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 91 | |
| 92 | // Helper functions and callbacks for Initialize(). |
| 93 | void InitializeAudioRenderer(); |
| 94 | void OnAudioRendererInitializeDone(PipelineStatus status); |
| 95 | void InitializeVideoRenderer(); |
| 96 | void OnVideoRendererInitializeDone(PipelineStatus status); |
| 97 | |
| 98 | // Helper functions and callbacks for Flush(). |
| 99 | void FlushAudioRenderer(); |
| 100 | void OnAudioRendererFlushDone(); |
| 101 | void FlushVideoRenderer(); |
| 102 | void OnVideoRendererFlushDone(); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 103 | |
| 104 | // Callback executed by filters to update statistics. |
| 105 | void OnUpdateStatistics(const PipelineStatistics& stats); |
| 106 | |
| 107 | // Collection of callback methods and helpers for tracking changes in |
| 108 | // buffering state and transition from paused/underflow states and playing |
| 109 | // states. |
| 110 | // |
| 111 | // While in the kPlaying state: |
| 112 | // - A waiting to non-waiting transition indicates preroll has completed |
| 113 | // and StartPlayback() should be called |
| 114 | // - A non-waiting to waiting transition indicates underflow has occurred |
| 115 | // and PausePlayback() should be called |
| 116 | void OnBufferingStateChanged(BufferingState* buffering_state, |
| 117 | BufferingState new_buffering_state); |
| 118 | bool WaitingForEnoughData() const; |
| 119 | void PausePlayback(); |
| 120 | void StartPlayback(); |
| 121 | |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 122 | // Callbacks executed when a renderer has ended. |
| 123 | void OnAudioRendererEnded(); |
| 124 | void OnVideoRendererEnded(); |
scherkus | ece8945 | 2014-09-09 23:13:38 | [diff] [blame] | 125 | bool PlaybackHasEnded() const; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 126 | void RunEndedCallbackIfNeeded(); |
| 127 | |
| 128 | // Callback executed when a runtime error happens. |
| 129 | void OnError(PipelineStatus error); |
| 130 | |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 131 | State state_; |
| 132 | |
| 133 | // Task runner used to execute pipeline tasks. |
| 134 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 135 | |
xhwang | 1c5668e | 2014-09-10 02:42:04 | [diff] [blame] | 136 | DemuxerStreamProvider* demuxer_stream_provider_; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 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_; |
xhwang | 94c0bd3 | 2014-11-13 20:49:31 | [diff] [blame] | 143 | PaintCB paint_cb_; |
jrummell | 74fc4f94 | 2015-03-02 22:48:27 | [diff] [blame] | 144 | base::Closure waiting_for_decryption_key_cb_; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 145 | |
| 146 | // Temporary callback used for Initialize() and Flush(). |
dalecurtis | 4704611 | 2015-01-23 22:58:20 | [diff] [blame] | 147 | PipelineStatusCB init_cb_; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 148 | base::Closure flush_cb_; |
| 149 | |
| 150 | scoped_ptr<AudioRenderer> audio_renderer_; |
| 151 | scoped_ptr<VideoRenderer> video_renderer_; |
| 152 | |
| 153 | // Renderer-provided time source used to control playback. |
| 154 | TimeSource* time_source_; |
scherkus | ece8945 | 2014-09-09 23:13:38 | [diff] [blame] | 155 | scoped_ptr<WallClockTimeSource> wall_clock_time_source_; |
| 156 | bool time_ticking_; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 157 | |
| 158 | // The time to start playback from after starting/seeking has completed. |
| 159 | base::TimeDelta start_time_; |
| 160 | |
| 161 | BufferingState audio_buffering_state_; |
| 162 | BufferingState video_buffering_state_; |
| 163 | |
| 164 | // Whether we've received the audio/video ended events. |
| 165 | bool audio_ended_; |
| 166 | bool video_ended_; |
| 167 | |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 168 | CdmContext* cdm_context_; |
| 169 | |
| 170 | // Callback registered by filters (decoder or demuxer) to be informed of a |
| 171 | // Decryptor. |
| 172 | // Note: We could have multiple filters registering this callback. One |
| 173 | // callback is okay because: |
| 174 | // 1, We always initialize filters in sequence. |
| 175 | // 2, Filter initialization will not finish until this callback is satisfied. |
| 176 | DecryptorReadyCB decryptor_ready_cb_; |
| 177 | |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 178 | bool underflow_disabled_for_testing_; |
scherkus | ece8945 | 2014-09-09 23:13:38 | [diff] [blame] | 179 | bool clockless_video_playback_enabled_for_testing_; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 180 | |
dalecurtis | c11e7bb | 2015-04-03 05:07:08 | [diff] [blame^] | 181 | // Used to defer underflow for video when audio is present. |
| 182 | base::CancelableClosure deferred_underflow_cb_; |
| 183 | |
| 184 | // The amount of time to wait before declaring underflow if the video renderer |
| 185 | // runs out of data but the audio renderer still has enough. |
| 186 | base::TimeDelta video_underflow_threshold_; |
| 187 | |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 188 | base::WeakPtr<RendererImpl> weak_this_; |
dmichael | 284c5e9 | 2014-12-09 20:02:47 | [diff] [blame] | 189 | base::WeakPtrFactory<RendererImpl> weak_factory_; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 190 | |
| 191 | DISALLOW_COPY_AND_ASSIGN(RendererImpl); |
| 192 | }; |
| 193 | |
| 194 | } // namespace media |
| 195 | |
servolk | f54f5c8f | 2015-02-24 20:32:39 | [diff] [blame] | 196 | #endif // MEDIA_RENDERERS_RENDERER_IMPL_H_ |