[go: nahoru, domu]

blob: e48a4b3f066d2e18b64eabeb930c625a48181a9f [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"
liberato@chromium.org8705ffc2020-07-28 20:11:5527#include "media/base/tuneable.h"
Chris Cunningham038548b2017-07-10 22:36:3028#include "media/base/video_decoder_config.h"
Xiaohan Wang640b41d2018-12-18 19:00:4629#include "media/base/waiting.h"
alokp5d86e9b2016-05-17 20:20:4130#include "ui/gfx/geometry/size.h"
xhwangbe9da702014-08-23 21:44:5531
32namespace base {
33class SingleThreadTaskRunner;
34}
35
36namespace media {
37
38class AudioRenderer;
servolkcfc91f152017-02-02 05:11:2339class MediaResource;
xhwangbe9da702014-08-23 21:44:5540class TimeSource;
41class VideoRenderer;
scherkusece89452014-09-09 23:13:3842class WallClockTimeSource;
xhwangbe9da702014-08-23 21:44:5543
Zentaro Kavanagh19d10592020-11-03 23:26:1444class MEDIA_EXPORT RendererImpl final : public Renderer {
xhwangbe9da702014-08-23 21:44:5545 public:
xhwangabd95fd2014-10-03 07:10:0146 // Renders audio/video streams using |audio_renderer| and |video_renderer|
47 // provided. All methods except for GetMediaTime() run on the |task_runner|.
48 // GetMediaTime() runs on the render main thread because it's part of JS sync
49 // API.
xhwangbe9da702014-08-23 21:44:5550 RendererImpl(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
danakj4d43bc22016-04-26 03:36:0451 std::unique_ptr<AudioRenderer> audio_renderer,
52 std::unique_ptr<VideoRenderer> video_renderer);
xhwangbe9da702014-08-23 21:44:5553
Peter Boström4c42559f2021-09-22 20:24:3454 RendererImpl(const RendererImpl&) = delete;
55 RendererImpl& operator=(const RendererImpl&) = delete;
56
xhwang97de4202014-11-25 08:44:0157 ~RendererImpl() final;
xhwangbe9da702014-08-23 21:44:5558
59 // Renderer implementation.
servolkcfc91f152017-02-02 05:11:2360 void Initialize(MediaResource* media_resource,
alokp16bbeea2016-05-12 23:32:3661 RendererClient* client,
Chris Mumford84f095f2019-10-25 23:19:4462 PipelineStatusCallback init_cb) final;
Takashi Sakamotocfb66262019-10-30 09:19:0963 void SetCdm(CdmContext* cdm_context, CdmAttachedCB cdm_attached_cb) final;
Anton Bikineev60ef3812021-05-15 18:05:0364 void SetLatencyHint(absl::optional<base::TimeDelta> latency_hint) final;
Thomas Guilbertdd0e3e62020-06-24 23:53:4265 void SetPreservesPitch(bool preserves_pitch) final;
evliu76eefae82021-01-14 10:10:0466 void SetAutoplayInitiated(bool autoplay_initiated) final;
Chris Mumford1ccf4512019-10-25 19:34:0567 void Flush(base::OnceClosure flush_cb) final;
xhwang97de4202014-11-25 08:44:0168 void StartPlayingFrom(base::TimeDelta time) final;
a.berwal338bf002015-04-22 11:14:5069 void SetPlaybackRate(double playback_rate) final;
xhwang97de4202014-11-25 08:44:0170 void SetVolume(float volume) final;
71 base::TimeDelta GetMediaTime() final;
Ted Meyerda82b602018-04-19 00:36:5672 void OnSelectedVideoTracksChanged(
73 const std::vector<DemuxerStream*>& enabled_tracks,
74 base::OnceClosure change_completed_cb) override;
75 void OnEnabledAudioTracksChanged(
76 const std::vector<DemuxerStream*>& enabled_tracks,
77 base::OnceClosure change_completed_cb) override;
xhwangbe9da702014-08-23 21:44:5578
79 // Helper functions for testing purposes. Must be called before Initialize().
80 void DisableUnderflowForTesting();
scherkusece89452014-09-09 23:13:3881 void EnableClocklessVideoPlaybackForTesting();
dalecurtisc11e7bb2015-04-03 05:07:0882 void set_time_source_for_testing(TimeSource* time_source) {
83 time_source_ = time_source;
84 }
85 void set_video_underflow_threshold_for_testing(base::TimeDelta threshold) {
liberato@chromium.org8705ffc2020-07-28 20:11:5586 video_underflow_threshold_.set_for_testing(threshold);
dalecurtisc11e7bb2015-04-03 05:07:0887 }
xhwangbe9da702014-08-23 21:44:5588
89 private:
alokp16bbeea2016-05-12 23:32:3690 class RendererClientInternal;
91
xhwangbe9da702014-08-23 21:44:5592 enum State {
93 STATE_UNINITIALIZED,
xhwanga935e442016-02-11 02:22:4594 STATE_INIT_PENDING_CDM, // Initialization is waiting for the CDM to be set.
95 STATE_INITIALIZING, // Initializing audio/video renderers.
servolka1597ec2017-04-06 19:49:1996 STATE_FLUSHING, // Flushing is in progress.
97 STATE_FLUSHED, // After initialization or after flush completed.
98 STATE_PLAYING, // After StartPlayingFrom has been called.
xhwangbe9da702014-08-23 21:44:5599 STATE_ERROR
100 };
101
dalecurtise92934582015-05-12 06:38:00102 bool GetWallClockTimes(const std::vector<base::TimeDelta>& media_timestamps,
103 std::vector<base::TimeTicks>* wall_clock_times);
xhwangbe9da702014-08-23 21:44:55104
xhwanga935e442016-02-11 02:22:45105 bool HasEncryptedStream();
106
107 void FinishInitialization(PipelineStatus status);
Dale Curtise130ef92018-09-20 00:17:13108 void FinishFlush();
xhwang97de4202014-11-25 08:44:01109
xhwangbe9da702014-08-23 21:44:55110 // Helper functions and callbacks for Initialize().
111 void InitializeAudioRenderer();
112 void OnAudioRendererInitializeDone(PipelineStatus status);
113 void InitializeVideoRenderer();
114 void OnVideoRendererInitializeDone(PipelineStatus status);
115
116 // Helper functions and callbacks for Flush().
servolk16e8bdf82017-04-11 17:00:39117 void FlushInternal();
xhwangbe9da702014-08-23 21:44:55118 void FlushAudioRenderer();
119 void OnAudioRendererFlushDone();
120 void FlushVideoRenderer();
121 void OnVideoRendererFlushDone();
122
servolk16e8bdf82017-04-11 17:00:39123 // Reinitialize audio/video renderer during a demuxer stream switching. The
124 // renderer must be flushed first, and when the re-init is completed the
125 // corresponding callback will be invoked to restart playback.
126 // The |stream| parameter specifies the new demuxer stream, and the |time|
127 // parameter specifies the time on media timeline where the switch occured.
Ted Meyerda82b602018-04-19 00:36:56128 void ReinitializeAudioRenderer(DemuxerStream* stream,
129 base::TimeDelta time,
130 base::OnceClosure reinitialize_completed_cb);
servolk16e8bdf82017-04-11 17:00:39131 void OnAudioRendererReinitialized(DemuxerStream* stream,
132 base::TimeDelta time,
Ted Meyerda82b602018-04-19 00:36:56133 base::OnceClosure reinitialize_completed_cb,
servolk16e8bdf82017-04-11 17:00:39134 PipelineStatus status);
Ted Meyerda82b602018-04-19 00:36:56135 void ReinitializeVideoRenderer(DemuxerStream* stream,
136 base::TimeDelta time,
137 base::OnceClosure restart_completed_cb);
servolk16e8bdf82017-04-11 17:00:39138 void OnVideoRendererReinitialized(DemuxerStream* stream,
139 base::TimeDelta time,
Ted Meyerda82b602018-04-19 00:36:56140 base::OnceClosure restart_completed_cb,
servolk16e8bdf82017-04-11 17:00:39141 PipelineStatus status);
142
143 // Restart audio/video renderer playback after a demuxer stream switch or
144 // after a demuxer stream has been disabled and re-enabled. The |stream|
145 // parameter specifies which stream needs to be restarted. The |time|
146 // parameter specifies the position on the media timeline where the playback
147 // needs to be restarted. It is necessary for demuxers with independent
148 // streams (e.g. MSE / ChunkDemuxer) to synchronize data reading between those
149 // streams.
Ted Meyerda82b602018-04-19 00:36:56150 void RestartAudioRenderer(DemuxerStream* stream,
151 base::TimeDelta time,
152 base::OnceClosure restart_completed_cb);
153 void RestartVideoRenderer(DemuxerStream* stream,
154 base::TimeDelta time,
155 base::OnceClosure restart_completed_cb);
156
157 // Fix state booleans after the stream switching is finished.
Daniel Cheng868ab272021-02-24 17:07:15158 void CleanUpTrackChange(base::OnceClosure on_finished,
Ted Meyerda82b602018-04-19 00:36:56159 bool* ended,
160 bool* playing);
servolkf25ceed2016-07-01 03:44:38161
xhwangbe9da702014-08-23 21:44:55162 // Callback executed by filters to update statistics.
alokp16bbeea2016-05-12 23:32:36163 void OnStatisticsUpdate(const PipelineStatistics& stats);
xhwangbe9da702014-08-23 21:44:55164
165 // Collection of callback methods and helpers for tracking changes in
166 // buffering state and transition from paused/underflow states and playing
167 // states.
168 //
169 // While in the kPlaying state:
170 // - A waiting to non-waiting transition indicates preroll has completed
171 // and StartPlayback() should be called
172 // - A non-waiting to waiting transition indicates underflow has occurred
173 // and PausePlayback() should be called
alokp16bbeea2016-05-12 23:32:36174 void OnBufferingStateChange(DemuxerStream::Type type,
Chris Cunninghamfc0d67e2019-07-22 20:29:16175 BufferingState new_buffering_state,
176 BufferingStateChangeReason reason);
servolk16e8bdf82017-04-11 17:00:39177
servolkf25ceed2016-07-01 03:44:38178 // Handles the buffering notifications that we might get while an audio or a
179 // video stream is being restarted. In those cases we don't want to report
180 // underflows immediately and instead give decoders a chance to catch up with
181 // currently playing stream. Returns true if the buffering nofication has been
182 // handled and no further processing is necessary, returns false to indicate
183 // that we should fall back to the regular OnBufferingStateChange logic.
184 bool HandleRestartedStreamBufferingChanges(
185 DemuxerStream::Type type,
186 BufferingState new_buffering_state);
servolk16e8bdf82017-04-11 17:00:39187
xhwangbe9da702014-08-23 21:44:55188 bool WaitingForEnoughData() const;
189 void PausePlayback();
190 void StartPlayback();
191
xhwangbe9da702014-08-23 21:44:55192 // Callbacks executed when a renderer has ended.
alokp16bbeea2016-05-12 23:32:36193 void OnRendererEnded(DemuxerStream::Type type);
scherkusece89452014-09-09 23:13:38194 bool PlaybackHasEnded() const;
xhwangbe9da702014-08-23 21:44:55195 void RunEndedCallbackIfNeeded();
196
197 // Callback executed when a runtime error happens.
198 void OnError(PipelineStatus error);
Chris Cunningham038548b2017-07-10 22:36:30199
Xiaohan Wang640b41d2018-12-18 19:00:46200 void OnWaiting(WaitingReason reason);
alokp5d86e9b2016-05-17 20:20:41201 void OnVideoNaturalSizeChange(const gfx::Size& size);
Chris Cunningham038548b2017-07-10 22:36:30202 void OnAudioConfigChange(const AudioDecoderConfig& config);
203 void OnVideoConfigChange(const VideoDecoderConfig& config);
alokp5d86e9b2016-05-17 20:20:41204 void OnVideoOpacityChange(bool opaque);
Anton Bikineev60ef3812021-05-15 18:05:03205 void OnVideoFrameRateChange(absl::optional<int> fps);
xhwangbe9da702014-08-23 21:44:55206
servolkbff54a22017-01-11 01:39:00207 void OnStreamRestartCompleted();
208
xhwangbe9da702014-08-23 21:44:55209 State state_;
210
211 // Task runner used to execute pipeline tasks.
212 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
213
servolkcfc91f152017-02-02 05:11:23214 MediaResource* media_resource_;
alokp16bbeea2016-05-12 23:32:36215 RendererClient* client_;
xhwangbe9da702014-08-23 21:44:55216
217 // Temporary callback used for Initialize() and Flush().
Chris Mumford84f095f2019-10-25 23:19:44218 PipelineStatusCallback init_cb_;
Chris Mumford1ccf4512019-10-25 19:34:05219 base::OnceClosure flush_cb_;
xhwangbe9da702014-08-23 21:44:55220
alokp16bbeea2016-05-12 23:32:36221 std::unique_ptr<RendererClientInternal> audio_renderer_client_;
222 std::unique_ptr<RendererClientInternal> video_renderer_client_;
danakj4d43bc22016-04-26 03:36:04223 std::unique_ptr<AudioRenderer> audio_renderer_;
224 std::unique_ptr<VideoRenderer> video_renderer_;
xhwangbe9da702014-08-23 21:44:55225
servolk16e8bdf82017-04-11 17:00:39226 DemuxerStream* current_audio_stream_;
227 DemuxerStream* current_video_stream_;
228
xhwangbe9da702014-08-23 21:44:55229 // Renderer-provided time source used to control playback.
230 TimeSource* time_source_;
danakj4d43bc22016-04-26 03:36:04231 std::unique_ptr<WallClockTimeSource> wall_clock_time_source_;
scherkusece89452014-09-09 23:13:38232 bool time_ticking_;
a.berwal338bf002015-04-22 11:14:50233 double playback_rate_;
xhwangbe9da702014-08-23 21:44:55234
235 // The time to start playback from after starting/seeking has completed.
236 base::TimeDelta start_time_;
237
238 BufferingState audio_buffering_state_;
239 BufferingState video_buffering_state_;
240
241 // Whether we've received the audio/video ended events.
242 bool audio_ended_;
243 bool video_ended_;
Ted Meyerda82b602018-04-19 00:36:56244 bool audio_playing_;
245 bool video_playing_;
xhwangbe9da702014-08-23 21:44:55246
xhwang97de4202014-11-25 08:44:01247 CdmContext* cdm_context_;
xhwang97de4202014-11-25 08:44:01248
xhwangbe9da702014-08-23 21:44:55249 bool underflow_disabled_for_testing_;
scherkusece89452014-09-09 23:13:38250 bool clockless_video_playback_enabled_for_testing_;
xhwangbe9da702014-08-23 21:44:55251
dalecurtisc11e7bb2015-04-03 05:07:08252 // Used to defer underflow for video when audio is present.
Alex Turner3ae74622020-11-25 15:42:24253 base::CancelableOnceClosure deferred_video_underflow_cb_;
servolkf25ceed2016-07-01 03:44:38254
Alex Turner3ae74622020-11-25 15:42:24255 // We cannot use `!deferred_video_underflow_cb_.IsCancelled()` as that changes
256 // when the callback is run, even if not explicitly cancelled.
257 bool has_deferred_buffering_state_change_ = false;
dalecurtisc11e7bb2015-04-03 05:07:08258
259 // The amount of time to wait before declaring underflow if the video renderer
260 // runs out of data but the audio renderer still has enough.
liberato@chromium.org8705ffc2020-07-28 20:11:55261 Tuneable<base::TimeDelta> video_underflow_threshold_ = {
Peter Kastinge5a38ed2021-10-02 03:06:35262 "MediaVideoUnderflowThreshold", base::Milliseconds(1000),
263 base::Milliseconds(3000), base::Milliseconds(8000)};
dalecurtisc11e7bb2015-04-03 05:07:08264
servolk9dd6cc82017-05-24 05:39:02265 // Lock used to protect access to the |restarting_audio_| flag and
266 // |restarting_audio_time_|.
267 // TODO(servolk): Get rid of the lock and replace restarting_audio_ with
268 // std::atomic<bool> when atomics are unbanned in Chromium.
269 base::Lock restarting_audio_lock_;
Ted Meyerda82b602018-04-19 00:36:56270 bool pending_audio_track_change_ = false;
servolk9dd6cc82017-05-24 05:39:02271 base::TimeDelta restarting_audio_time_ = kNoTimestamp;
272
Ted Meyerda82b602018-04-19 00:36:56273 bool pending_video_track_change_ = false;
Dale Curtis3defa542017-11-07 00:52:32274
xhwangbe9da702014-08-23 21:44:55275 base::WeakPtr<RendererImpl> weak_this_;
Jeremy Roman32948392019-07-09 18:34:37276 base::WeakPtrFactory<RendererImpl> weak_factory_{this};
xhwangbe9da702014-08-23 21:44:55277};
278
279} // namespace media
280
servolkf54f5c8f2015-02-24 20:32:39281#endif // MEDIA_RENDERERS_RENDERER_IMPL_H_