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 | |
danakj | 4d43bc2 | 2016-04-26 03:36:04 | [diff] [blame] | 8 | #include <memory> |
dalecurtis | e9293458 | 2015-05-12 06:38:00 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
dalecurtis | c11e7bb | 2015-04-03 05:07:08 | [diff] [blame] | 11 | #include "base/cancelable_callback.h" |
avi | a82b9b5 | 2015-12-19 04:27:08 | [diff] [blame] | 12 | #include "base/macros.h" |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
| 15 | #include "base/synchronization/lock.h" |
| 16 | #include "base/time/clock.h" |
| 17 | #include "base/time/default_tick_clock.h" |
| 18 | #include "base/time/time.h" |
| 19 | #include "media/base/buffering_state.h" |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 20 | #include "media/base/decryptor.h" |
alokp | 16bbeea | 2016-05-12 23:32:36 | [diff] [blame^] | 21 | #include "media/base/demuxer_stream.h" |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 22 | #include "media/base/media_export.h" |
| 23 | #include "media/base/pipeline_status.h" |
| 24 | #include "media/base/renderer.h" |
| 25 | |
| 26 | namespace base { |
| 27 | class SingleThreadTaskRunner; |
| 28 | } |
| 29 | |
| 30 | namespace media { |
| 31 | |
| 32 | class AudioRenderer; |
xhwang | 1c5668e | 2014-09-10 02:42:04 | [diff] [blame] | 33 | class DemuxerStreamProvider; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 34 | class TimeSource; |
| 35 | class VideoRenderer; |
scherkus | ece8945 | 2014-09-09 23:13:38 | [diff] [blame] | 36 | class WallClockTimeSource; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 37 | |
| 38 | class MEDIA_EXPORT RendererImpl : public Renderer { |
| 39 | public: |
xhwang | abd95fd | 2014-10-03 07:10:01 | [diff] [blame] | 40 | // Renders audio/video streams using |audio_renderer| and |video_renderer| |
| 41 | // provided. All methods except for GetMediaTime() run on the |task_runner|. |
| 42 | // GetMediaTime() runs on the render main thread because it's part of JS sync |
| 43 | // API. |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 44 | RendererImpl(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
danakj | 4d43bc2 | 2016-04-26 03:36:04 | [diff] [blame] | 45 | std::unique_ptr<AudioRenderer> audio_renderer, |
| 46 | std::unique_ptr<VideoRenderer> video_renderer); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 47 | |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 48 | ~RendererImpl() final; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 49 | |
| 50 | // Renderer implementation. |
dcheng | c245654 | 2014-10-21 12:23:27 | [diff] [blame] | 51 | void Initialize(DemuxerStreamProvider* demuxer_stream_provider, |
alokp | 16bbeea | 2016-05-12 23:32:36 | [diff] [blame^] | 52 | RendererClient* client, |
| 53 | const PipelineStatusCB& init_cb) final; |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 54 | void SetCdm(CdmContext* cdm_context, |
| 55 | const CdmAttachedCB& cdm_attached_cb) final; |
| 56 | void Flush(const base::Closure& flush_cb) final; |
| 57 | void StartPlayingFrom(base::TimeDelta time) final; |
a.berwal | 338bf00 | 2015-04-22 11:14:50 | [diff] [blame] | 58 | void SetPlaybackRate(double playback_rate) final; |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 59 | void SetVolume(float volume) final; |
| 60 | base::TimeDelta GetMediaTime() final; |
| 61 | bool HasAudio() final; |
| 62 | bool HasVideo() final; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 63 | |
| 64 | // Helper functions for testing purposes. Must be called before Initialize(). |
| 65 | void DisableUnderflowForTesting(); |
scherkus | ece8945 | 2014-09-09 23:13:38 | [diff] [blame] | 66 | void EnableClocklessVideoPlaybackForTesting(); |
dalecurtis | c11e7bb | 2015-04-03 05:07:08 | [diff] [blame] | 67 | void set_time_source_for_testing(TimeSource* time_source) { |
| 68 | time_source_ = time_source; |
| 69 | } |
| 70 | void set_video_underflow_threshold_for_testing(base::TimeDelta threshold) { |
| 71 | video_underflow_threshold_ = threshold; |
| 72 | } |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 73 | |
| 74 | private: |
alokp | 16bbeea | 2016-05-12 23:32:36 | [diff] [blame^] | 75 | class RendererClientInternal; |
| 76 | |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 77 | enum State { |
| 78 | STATE_UNINITIALIZED, |
xhwang | a935e44 | 2016-02-11 02:22:45 | [diff] [blame] | 79 | STATE_INIT_PENDING_CDM, // Initialization is waiting for the CDM to be set. |
| 80 | STATE_INITIALIZING, // Initializing audio/video renderers. |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 81 | STATE_FLUSHING, |
| 82 | STATE_PLAYING, |
| 83 | STATE_ERROR |
| 84 | }; |
| 85 | |
dalecurtis | e9293458 | 2015-05-12 06:38:00 | [diff] [blame] | 86 | bool GetWallClockTimes(const std::vector<base::TimeDelta>& media_timestamps, |
| 87 | std::vector<base::TimeTicks>* wall_clock_times); |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 88 | |
xhwang | a935e44 | 2016-02-11 02:22:45 | [diff] [blame] | 89 | bool HasEncryptedStream(); |
| 90 | |
| 91 | void FinishInitialization(PipelineStatus status); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 92 | |
| 93 | // Helper functions and callbacks for Initialize(). |
| 94 | void InitializeAudioRenderer(); |
| 95 | void OnAudioRendererInitializeDone(PipelineStatus status); |
| 96 | void InitializeVideoRenderer(); |
| 97 | void OnVideoRendererInitializeDone(PipelineStatus status); |
| 98 | |
| 99 | // Helper functions and callbacks for Flush(). |
| 100 | void FlushAudioRenderer(); |
| 101 | void OnAudioRendererFlushDone(); |
| 102 | void FlushVideoRenderer(); |
| 103 | void OnVideoRendererFlushDone(); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 104 | |
| 105 | // Callback executed by filters to update statistics. |
alokp | 16bbeea | 2016-05-12 23:32:36 | [diff] [blame^] | 106 | void OnStatisticsUpdate(const PipelineStatistics& stats); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 107 | |
| 108 | // Collection of callback methods and helpers for tracking changes in |
| 109 | // buffering state and transition from paused/underflow states and playing |
| 110 | // states. |
| 111 | // |
| 112 | // While in the kPlaying state: |
| 113 | // - A waiting to non-waiting transition indicates preroll has completed |
| 114 | // and StartPlayback() should be called |
| 115 | // - A non-waiting to waiting transition indicates underflow has occurred |
| 116 | // and PausePlayback() should be called |
alokp | 16bbeea | 2016-05-12 23:32:36 | [diff] [blame^] | 117 | void OnBufferingStateChange(DemuxerStream::Type type, |
| 118 | BufferingState new_buffering_state); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 119 | bool WaitingForEnoughData() const; |
| 120 | void PausePlayback(); |
| 121 | void StartPlayback(); |
| 122 | |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 123 | // Callbacks executed when a renderer has ended. |
alokp | 16bbeea | 2016-05-12 23:32:36 | [diff] [blame^] | 124 | void OnRendererEnded(DemuxerStream::Type type); |
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); |
alokp | 16bbeea | 2016-05-12 23:32:36 | [diff] [blame^] | 130 | void OnWaitingForDecryptionKey(); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 131 | |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 132 | State state_; |
| 133 | |
| 134 | // Task runner used to execute pipeline tasks. |
| 135 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 136 | |
xhwang | 1c5668e | 2014-09-10 02:42:04 | [diff] [blame] | 137 | DemuxerStreamProvider* demuxer_stream_provider_; |
alokp | 16bbeea | 2016-05-12 23:32:36 | [diff] [blame^] | 138 | RendererClient* client_; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 139 | |
| 140 | // Temporary callback used for Initialize() and Flush(). |
dalecurtis | 4704611 | 2015-01-23 22:58:20 | [diff] [blame] | 141 | PipelineStatusCB init_cb_; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 142 | base::Closure flush_cb_; |
| 143 | |
alokp | 16bbeea | 2016-05-12 23:32:36 | [diff] [blame^] | 144 | std::unique_ptr<RendererClientInternal> audio_renderer_client_; |
| 145 | std::unique_ptr<RendererClientInternal> video_renderer_client_; |
danakj | 4d43bc2 | 2016-04-26 03:36:04 | [diff] [blame] | 146 | std::unique_ptr<AudioRenderer> audio_renderer_; |
| 147 | std::unique_ptr<VideoRenderer> video_renderer_; |
servolk | 06e3a9f | 2016-04-16 02:20:10 | [diff] [blame] | 148 | |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 149 | // Renderer-provided time source used to control playback. |
| 150 | TimeSource* time_source_; |
danakj | 4d43bc2 | 2016-04-26 03:36:04 | [diff] [blame] | 151 | std::unique_ptr<WallClockTimeSource> wall_clock_time_source_; |
scherkus | ece8945 | 2014-09-09 23:13:38 | [diff] [blame] | 152 | bool time_ticking_; |
a.berwal | 338bf00 | 2015-04-22 11:14:50 | [diff] [blame] | 153 | double playback_rate_; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 154 | |
| 155 | // The time to start playback from after starting/seeking has completed. |
| 156 | base::TimeDelta start_time_; |
| 157 | |
| 158 | BufferingState audio_buffering_state_; |
| 159 | BufferingState video_buffering_state_; |
| 160 | |
| 161 | // Whether we've received the audio/video ended events. |
| 162 | bool audio_ended_; |
| 163 | bool video_ended_; |
| 164 | |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 165 | CdmContext* cdm_context_; |
xhwang | a935e44 | 2016-02-11 02:22:45 | [diff] [blame] | 166 | CdmAttachedCB pending_cdm_attached_cb_; |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 167 | |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 168 | bool underflow_disabled_for_testing_; |
scherkus | ece8945 | 2014-09-09 23:13:38 | [diff] [blame] | 169 | bool clockless_video_playback_enabled_for_testing_; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 170 | |
dalecurtis | c11e7bb | 2015-04-03 05:07:08 | [diff] [blame] | 171 | // Used to defer underflow for video when audio is present. |
| 172 | base::CancelableClosure deferred_underflow_cb_; |
| 173 | |
| 174 | // The amount of time to wait before declaring underflow if the video renderer |
| 175 | // runs out of data but the audio renderer still has enough. |
| 176 | base::TimeDelta video_underflow_threshold_; |
| 177 | |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 178 | base::WeakPtr<RendererImpl> weak_this_; |
dmichael | 284c5e9 | 2014-12-09 20:02:47 | [diff] [blame] | 179 | base::WeakPtrFactory<RendererImpl> weak_factory_; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 180 | |
| 181 | DISALLOW_COPY_AND_ASSIGN(RendererImpl); |
| 182 | }; |
| 183 | |
| 184 | } // namespace media |
| 185 | |
servolk | f54f5c8f | 2015-02-24 20:32:39 | [diff] [blame] | 186 | #endif // MEDIA_RENDERERS_RENDERER_IMPL_H_ |