[go: nahoru, domu]

blob: 5b1e0664eb724f27ebc75c268c5a6a0893537564 [file] [log] [blame]
xhwangbe9da702014-08-23 21:44:551// 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
servolkf54f5c8f2015-02-24 20:32:395#ifndef MEDIA_RENDERERS_RENDERER_IMPL_H_
6#define MEDIA_RENDERERS_RENDERER_IMPL_H_
xhwangbe9da702014-08-23 21:44:557
servolkbff54a22017-01-11 01:39:008#include <list>
danakj4d43bc22016-04-26 03:36:049#include <memory>
dalecurtise92934582015-05-12 06:38:0010#include <vector>
11
dalecurtisc11e7bb2015-04-03 05:07:0812#include "base/cancelable_callback.h"
avia82b9b52015-12-19 04:27:0813#include "base/macros.h"
xhwangbe9da702014-08-23 21:44:5514#include "base/memory/ref_counted.h"
xhwangbe9da702014-08-23 21:44:5515#include "base/memory/weak_ptr.h"
16#include "base/synchronization/lock.h"
17#include "base/time/clock.h"
18#include "base/time/default_tick_clock.h"
19#include "base/time/time.h"
Chris Cunningham038548b2017-07-10 22:36:3020#include "media/base/audio_decoder_config.h"
xhwangbe9da702014-08-23 21:44:5521#include "media/base/buffering_state.h"
xhwang97de4202014-11-25 08:44:0122#include "media/base/decryptor.h"
alokp16bbeea2016-05-12 23:32:3623#include "media/base/demuxer_stream.h"
xhwangbe9da702014-08-23 21:44:5524#include "media/base/media_export.h"
25#include "media/base/pipeline_status.h"
26#include "media/base/renderer.h"
Chris Cunningham038548b2017-07-10 22:36:3027#include "media/base/video_decoder_config.h"
Xiaohan Wang640b41d2018-12-18 19:00:4628#include "media/base/waiting.h"
alokp5d86e9b2016-05-17 20:20:4129#include "ui/gfx/geometry/size.h"
xhwangbe9da702014-08-23 21:44:5530
31namespace base {
32class SingleThreadTaskRunner;
33}
34
35namespace media {
36
37class AudioRenderer;
servolkcfc91f152017-02-02 05:11:2338class MediaResource;
xhwangbe9da702014-08-23 21:44:5539class TimeSource;
40class VideoRenderer;
scherkusece89452014-09-09 23:13:3841class WallClockTimeSource;
xhwangbe9da702014-08-23 21:44:5542
43class MEDIA_EXPORT RendererImpl : public Renderer {
44 public:
xhwangabd95fd2014-10-03 07:10:0145 // Renders audio/video streams using |audio_renderer| and |video_renderer|
46 // provided. All methods except for GetMediaTime() run on the |task_runner|.
47 // GetMediaTime() runs on the render main thread because it's part of JS sync
48 // API.
xhwangbe9da702014-08-23 21:44:5549 RendererImpl(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
danakj4d43bc22016-04-26 03:36:0450 std::unique_ptr<AudioRenderer> audio_renderer,
51 std::unique_ptr<VideoRenderer> video_renderer);
xhwangbe9da702014-08-23 21:44:5552
xhwang97de4202014-11-25 08:44:0153 ~RendererImpl() final;
xhwangbe9da702014-08-23 21:44:5554
55 // Renderer implementation.
servolkcfc91f152017-02-02 05:11:2356 void Initialize(MediaResource* media_resource,
alokp16bbeea2016-05-12 23:32:3657 RendererClient* client,
58 const PipelineStatusCB& init_cb) final;
xhwang97de4202014-11-25 08:44:0159 void SetCdm(CdmContext* cdm_context,
60 const CdmAttachedCB& cdm_attached_cb) final;
Chris Mumford1ccf4512019-10-25 19:34:0561 void Flush(base::OnceClosure flush_cb) final;
xhwang97de4202014-11-25 08:44:0162 void StartPlayingFrom(base::TimeDelta time) final;
a.berwal338bf002015-04-22 11:14:5063 void SetPlaybackRate(double playback_rate) final;
xhwang97de4202014-11-25 08:44:0164 void SetVolume(float volume) final;
65 base::TimeDelta GetMediaTime() final;
Ted Meyerda82b602018-04-19 00:36:5666 void OnSelectedVideoTracksChanged(
67 const std::vector<DemuxerStream*>& enabled_tracks,
68 base::OnceClosure change_completed_cb) override;
69 void OnEnabledAudioTracksChanged(
70 const std::vector<DemuxerStream*>& enabled_tracks,
71 base::OnceClosure change_completed_cb) override;
xhwangbe9da702014-08-23 21:44:5572
73 // Helper functions for testing purposes. Must be called before Initialize().
74 void DisableUnderflowForTesting();
scherkusece89452014-09-09 23:13:3875 void EnableClocklessVideoPlaybackForTesting();
dalecurtisc11e7bb2015-04-03 05:07:0876 void set_time_source_for_testing(TimeSource* time_source) {
77 time_source_ = time_source;
78 }
79 void set_video_underflow_threshold_for_testing(base::TimeDelta threshold) {
80 video_underflow_threshold_ = threshold;
81 }
xhwangbe9da702014-08-23 21:44:5582
83 private:
alokp16bbeea2016-05-12 23:32:3684 class RendererClientInternal;
85
xhwangbe9da702014-08-23 21:44:5586 enum State {
87 STATE_UNINITIALIZED,
xhwanga935e442016-02-11 02:22:4588 STATE_INIT_PENDING_CDM, // Initialization is waiting for the CDM to be set.
89 STATE_INITIALIZING, // Initializing audio/video renderers.
servolka1597ec2017-04-06 19:49:1990 STATE_FLUSHING, // Flushing is in progress.
91 STATE_FLUSHED, // After initialization or after flush completed.
92 STATE_PLAYING, // After StartPlayingFrom has been called.
xhwangbe9da702014-08-23 21:44:5593 STATE_ERROR
94 };
95
dalecurtise92934582015-05-12 06:38:0096 bool GetWallClockTimes(const std::vector<base::TimeDelta>& media_timestamps,
97 std::vector<base::TimeTicks>* wall_clock_times);
xhwangbe9da702014-08-23 21:44:5598
xhwanga935e442016-02-11 02:22:4599 bool HasEncryptedStream();
100
101 void FinishInitialization(PipelineStatus status);
Dale Curtise130ef92018-09-20 00:17:13102 void FinishFlush();
xhwang97de4202014-11-25 08:44:01103
xhwangbe9da702014-08-23 21:44:55104 // Helper functions and callbacks for Initialize().
105 void InitializeAudioRenderer();
106 void OnAudioRendererInitializeDone(PipelineStatus status);
107 void InitializeVideoRenderer();
108 void OnVideoRendererInitializeDone(PipelineStatus status);
109
110 // Helper functions and callbacks for Flush().
servolk16e8bdf82017-04-11 17:00:39111 void FlushInternal();
xhwangbe9da702014-08-23 21:44:55112 void FlushAudioRenderer();
113 void OnAudioRendererFlushDone();
114 void FlushVideoRenderer();
115 void OnVideoRendererFlushDone();
116
servolk16e8bdf82017-04-11 17:00:39117 // Reinitialize audio/video renderer during a demuxer stream switching. The
118 // renderer must be flushed first, and when the re-init is completed the
119 // corresponding callback will be invoked to restart playback.
120 // The |stream| parameter specifies the new demuxer stream, and the |time|
121 // parameter specifies the time on media timeline where the switch occured.
Ted Meyerda82b602018-04-19 00:36:56122 void ReinitializeAudioRenderer(DemuxerStream* stream,
123 base::TimeDelta time,
124 base::OnceClosure reinitialize_completed_cb);
servolk16e8bdf82017-04-11 17:00:39125 void OnAudioRendererReinitialized(DemuxerStream* stream,
126 base::TimeDelta time,
Ted Meyerda82b602018-04-19 00:36:56127 base::OnceClosure reinitialize_completed_cb,
servolk16e8bdf82017-04-11 17:00:39128 PipelineStatus status);
Ted Meyerda82b602018-04-19 00:36:56129 void ReinitializeVideoRenderer(DemuxerStream* stream,
130 base::TimeDelta time,
131 base::OnceClosure restart_completed_cb);
servolk16e8bdf82017-04-11 17:00:39132 void OnVideoRendererReinitialized(DemuxerStream* stream,
133 base::TimeDelta time,
Ted Meyerda82b602018-04-19 00:36:56134 base::OnceClosure restart_completed_cb,
servolk16e8bdf82017-04-11 17:00:39135 PipelineStatus status);
136
137 // Restart audio/video renderer playback after a demuxer stream switch or
138 // after a demuxer stream has been disabled and re-enabled. The |stream|
139 // parameter specifies which stream needs to be restarted. The |time|
140 // parameter specifies the position on the media timeline where the playback
141 // needs to be restarted. It is necessary for demuxers with independent
142 // streams (e.g. MSE / ChunkDemuxer) to synchronize data reading between those
143 // streams.
Ted Meyerda82b602018-04-19 00:36:56144 void RestartAudioRenderer(DemuxerStream* stream,
145 base::TimeDelta time,
146 base::OnceClosure restart_completed_cb);
147 void RestartVideoRenderer(DemuxerStream* stream,
148 base::TimeDelta time,
149 base::OnceClosure restart_completed_cb);
150
151 // Fix state booleans after the stream switching is finished.
152 void CleanUpTrackChange(base::RepeatingClosure on_finished,
Ted Meyerda82b602018-04-19 00:36:56153 bool* ended,
154 bool* playing);
servolkf25ceed2016-07-01 03:44:38155
xhwangbe9da702014-08-23 21:44:55156 // Callback executed by filters to update statistics.
alokp16bbeea2016-05-12 23:32:36157 void OnStatisticsUpdate(const PipelineStatistics& stats);
xhwangbe9da702014-08-23 21:44:55158
159 // Collection of callback methods and helpers for tracking changes in
160 // buffering state and transition from paused/underflow states and playing
161 // states.
162 //
163 // While in the kPlaying state:
164 // - A waiting to non-waiting transition indicates preroll has completed
165 // and StartPlayback() should be called
166 // - A non-waiting to waiting transition indicates underflow has occurred
167 // and PausePlayback() should be called
alokp16bbeea2016-05-12 23:32:36168 void OnBufferingStateChange(DemuxerStream::Type type,
Chris Cunninghamfc0d67e2019-07-22 20:29:16169 BufferingState new_buffering_state,
170 BufferingStateChangeReason reason);
servolk16e8bdf82017-04-11 17:00:39171
servolkf25ceed2016-07-01 03:44:38172 // Handles the buffering notifications that we might get while an audio or a
173 // video stream is being restarted. In those cases we don't want to report
174 // underflows immediately and instead give decoders a chance to catch up with
175 // currently playing stream. Returns true if the buffering nofication has been
176 // handled and no further processing is necessary, returns false to indicate
177 // that we should fall back to the regular OnBufferingStateChange logic.
178 bool HandleRestartedStreamBufferingChanges(
179 DemuxerStream::Type type,
180 BufferingState new_buffering_state);
servolk16e8bdf82017-04-11 17:00:39181
xhwangbe9da702014-08-23 21:44:55182 bool WaitingForEnoughData() const;
183 void PausePlayback();
184 void StartPlayback();
185
xhwangbe9da702014-08-23 21:44:55186 // Callbacks executed when a renderer has ended.
alokp16bbeea2016-05-12 23:32:36187 void OnRendererEnded(DemuxerStream::Type type);
scherkusece89452014-09-09 23:13:38188 bool PlaybackHasEnded() const;
xhwangbe9da702014-08-23 21:44:55189 void RunEndedCallbackIfNeeded();
190
191 // Callback executed when a runtime error happens.
192 void OnError(PipelineStatus error);
Chris Cunningham038548b2017-07-10 22:36:30193
Xiaohan Wang640b41d2018-12-18 19:00:46194 void OnWaiting(WaitingReason reason);
alokp5d86e9b2016-05-17 20:20:41195 void OnVideoNaturalSizeChange(const gfx::Size& size);
Chris Cunningham038548b2017-07-10 22:36:30196 void OnAudioConfigChange(const AudioDecoderConfig& config);
197 void OnVideoConfigChange(const VideoDecoderConfig& config);
alokp5d86e9b2016-05-17 20:20:41198 void OnVideoOpacityChange(bool opaque);
xhwangbe9da702014-08-23 21:44:55199
servolkbff54a22017-01-11 01:39:00200 void OnStreamRestartCompleted();
201
xhwangbe9da702014-08-23 21:44:55202 State state_;
203
204 // Task runner used to execute pipeline tasks.
205 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
206
servolkcfc91f152017-02-02 05:11:23207 MediaResource* media_resource_;
alokp16bbeea2016-05-12 23:32:36208 RendererClient* client_;
xhwangbe9da702014-08-23 21:44:55209
210 // Temporary callback used for Initialize() and Flush().
dalecurtis47046112015-01-23 22:58:20211 PipelineStatusCB init_cb_;
Chris Mumford1ccf4512019-10-25 19:34:05212 base::OnceClosure flush_cb_;
xhwangbe9da702014-08-23 21:44:55213
alokp16bbeea2016-05-12 23:32:36214 std::unique_ptr<RendererClientInternal> audio_renderer_client_;
215 std::unique_ptr<RendererClientInternal> video_renderer_client_;
danakj4d43bc22016-04-26 03:36:04216 std::unique_ptr<AudioRenderer> audio_renderer_;
217 std::unique_ptr<VideoRenderer> video_renderer_;
xhwangbe9da702014-08-23 21:44:55218
servolk16e8bdf82017-04-11 17:00:39219 DemuxerStream* current_audio_stream_;
220 DemuxerStream* current_video_stream_;
221
xhwangbe9da702014-08-23 21:44:55222 // Renderer-provided time source used to control playback.
223 TimeSource* time_source_;
danakj4d43bc22016-04-26 03:36:04224 std::unique_ptr<WallClockTimeSource> wall_clock_time_source_;
scherkusece89452014-09-09 23:13:38225 bool time_ticking_;
a.berwal338bf002015-04-22 11:14:50226 double playback_rate_;
xhwangbe9da702014-08-23 21:44:55227
228 // The time to start playback from after starting/seeking has completed.
229 base::TimeDelta start_time_;
230
231 BufferingState audio_buffering_state_;
232 BufferingState video_buffering_state_;
233
234 // Whether we've received the audio/video ended events.
235 bool audio_ended_;
236 bool video_ended_;
Ted Meyerda82b602018-04-19 00:36:56237 bool audio_playing_;
238 bool video_playing_;
xhwangbe9da702014-08-23 21:44:55239
xhwang97de4202014-11-25 08:44:01240 CdmContext* cdm_context_;
xhwang97de4202014-11-25 08:44:01241
xhwangbe9da702014-08-23 21:44:55242 bool underflow_disabled_for_testing_;
scherkusece89452014-09-09 23:13:38243 bool clockless_video_playback_enabled_for_testing_;
xhwangbe9da702014-08-23 21:44:55244
dalecurtisc11e7bb2015-04-03 05:07:08245 // Used to defer underflow for video when audio is present.
servolkf25ceed2016-07-01 03:44:38246 base::CancelableClosure deferred_video_underflow_cb_;
247
248 // Used to defer underflow for audio when restarting audio playback.
249 base::CancelableClosure deferred_audio_restart_underflow_cb_;
dalecurtisc11e7bb2015-04-03 05:07:08250
251 // The amount of time to wait before declaring underflow if the video renderer
252 // runs out of data but the audio renderer still has enough.
253 base::TimeDelta video_underflow_threshold_;
254
servolk9dd6cc82017-05-24 05:39:02255 // Lock used to protect access to the |restarting_audio_| flag and
256 // |restarting_audio_time_|.
257 // TODO(servolk): Get rid of the lock and replace restarting_audio_ with
258 // std::atomic<bool> when atomics are unbanned in Chromium.
259 base::Lock restarting_audio_lock_;
Ted Meyerda82b602018-04-19 00:36:56260 bool pending_audio_track_change_ = false;
servolk9dd6cc82017-05-24 05:39:02261 base::TimeDelta restarting_audio_time_ = kNoTimestamp;
262
Ted Meyerda82b602018-04-19 00:36:56263 bool pending_video_track_change_ = false;
Dale Curtis3defa542017-11-07 00:52:32264
xhwangbe9da702014-08-23 21:44:55265 base::WeakPtr<RendererImpl> weak_this_;
Jeremy Roman32948392019-07-09 18:34:37266 base::WeakPtrFactory<RendererImpl> weak_factory_{this};
xhwangbe9da702014-08-23 21:44:55267
268 DISALLOW_COPY_AND_ASSIGN(RendererImpl);
269};
270
271} // namespace media
272
servolkf54f5c8f2015-02-24 20:32:39273#endif // MEDIA_RENDERERS_RENDERER_IMPL_H_