[go: nahoru, domu]

blob: c33d290bfc9001c5f6b0c704396736098a83d543 [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
xhwang97de4202014-11-25 08:44:0154 ~RendererImpl() final;
xhwangbe9da702014-08-23 21:44:5555
56 // Renderer implementation.
servolkcfc91f152017-02-02 05:11:2357 void Initialize(MediaResource* media_resource,
alokp16bbeea2016-05-12 23:32:3658 RendererClient* client,
Chris Mumford84f095f2019-10-25 23:19:4459 PipelineStatusCallback init_cb) final;
Takashi Sakamotocfb66262019-10-30 09:19:0960 void SetCdm(CdmContext* cdm_context, CdmAttachedCB cdm_attached_cb) final;
Chris Cunninghame8c62632019-11-20 23:34:2761 void SetLatencyHint(base::Optional<base::TimeDelta> latency_hint) final;
Thomas Guilbertdd0e3e62020-06-24 23:53:4262 void SetPreservesPitch(bool preserves_pitch) final;
Chris Mumford1ccf4512019-10-25 19:34:0563 void Flush(base::OnceClosure flush_cb) final;
xhwang97de4202014-11-25 08:44:0164 void StartPlayingFrom(base::TimeDelta time) final;
a.berwal338bf002015-04-22 11:14:5065 void SetPlaybackRate(double playback_rate) final;
xhwang97de4202014-11-25 08:44:0166 void SetVolume(float volume) final;
67 base::TimeDelta GetMediaTime() final;
Ted Meyerda82b602018-04-19 00:36:5668 void OnSelectedVideoTracksChanged(
69 const std::vector<DemuxerStream*>& enabled_tracks,
70 base::OnceClosure change_completed_cb) override;
71 void OnEnabledAudioTracksChanged(
72 const std::vector<DemuxerStream*>& enabled_tracks,
73 base::OnceClosure change_completed_cb) override;
xhwangbe9da702014-08-23 21:44:5574
75 // Helper functions for testing purposes. Must be called before Initialize().
76 void DisableUnderflowForTesting();
scherkusece89452014-09-09 23:13:3877 void EnableClocklessVideoPlaybackForTesting();
dalecurtisc11e7bb2015-04-03 05:07:0878 void set_time_source_for_testing(TimeSource* time_source) {
79 time_source_ = time_source;
80 }
81 void set_video_underflow_threshold_for_testing(base::TimeDelta threshold) {
liberato@chromium.org8705ffc2020-07-28 20:11:5582 video_underflow_threshold_.set_for_testing(threshold);
dalecurtisc11e7bb2015-04-03 05:07:0883 }
xhwangbe9da702014-08-23 21:44:5584
85 private:
alokp16bbeea2016-05-12 23:32:3686 class RendererClientInternal;
87
xhwangbe9da702014-08-23 21:44:5588 enum State {
89 STATE_UNINITIALIZED,
xhwanga935e442016-02-11 02:22:4590 STATE_INIT_PENDING_CDM, // Initialization is waiting for the CDM to be set.
91 STATE_INITIALIZING, // Initializing audio/video renderers.
servolka1597ec2017-04-06 19:49:1992 STATE_FLUSHING, // Flushing is in progress.
93 STATE_FLUSHED, // After initialization or after flush completed.
94 STATE_PLAYING, // After StartPlayingFrom has been called.
xhwangbe9da702014-08-23 21:44:5595 STATE_ERROR
96 };
97
dalecurtise92934582015-05-12 06:38:0098 bool GetWallClockTimes(const std::vector<base::TimeDelta>& media_timestamps,
99 std::vector<base::TimeTicks>* wall_clock_times);
xhwangbe9da702014-08-23 21:44:55100
xhwanga935e442016-02-11 02:22:45101 bool HasEncryptedStream();
102
103 void FinishInitialization(PipelineStatus status);
Dale Curtise130ef92018-09-20 00:17:13104 void FinishFlush();
xhwang97de4202014-11-25 08:44:01105
xhwangbe9da702014-08-23 21:44:55106 // Helper functions and callbacks for Initialize().
107 void InitializeAudioRenderer();
108 void OnAudioRendererInitializeDone(PipelineStatus status);
109 void InitializeVideoRenderer();
110 void OnVideoRendererInitializeDone(PipelineStatus status);
111
112 // Helper functions and callbacks for Flush().
servolk16e8bdf82017-04-11 17:00:39113 void FlushInternal();
xhwangbe9da702014-08-23 21:44:55114 void FlushAudioRenderer();
115 void OnAudioRendererFlushDone();
116 void FlushVideoRenderer();
117 void OnVideoRendererFlushDone();
118
servolk16e8bdf82017-04-11 17:00:39119 // Reinitialize audio/video renderer during a demuxer stream switching. The
120 // renderer must be flushed first, and when the re-init is completed the
121 // corresponding callback will be invoked to restart playback.
122 // The |stream| parameter specifies the new demuxer stream, and the |time|
123 // parameter specifies the time on media timeline where the switch occured.
Ted Meyerda82b602018-04-19 00:36:56124 void ReinitializeAudioRenderer(DemuxerStream* stream,
125 base::TimeDelta time,
126 base::OnceClosure reinitialize_completed_cb);
servolk16e8bdf82017-04-11 17:00:39127 void OnAudioRendererReinitialized(DemuxerStream* stream,
128 base::TimeDelta time,
Ted Meyerda82b602018-04-19 00:36:56129 base::OnceClosure reinitialize_completed_cb,
servolk16e8bdf82017-04-11 17:00:39130 PipelineStatus status);
Ted Meyerda82b602018-04-19 00:36:56131 void ReinitializeVideoRenderer(DemuxerStream* stream,
132 base::TimeDelta time,
133 base::OnceClosure restart_completed_cb);
servolk16e8bdf82017-04-11 17:00:39134 void OnVideoRendererReinitialized(DemuxerStream* stream,
135 base::TimeDelta time,
Ted Meyerda82b602018-04-19 00:36:56136 base::OnceClosure restart_completed_cb,
servolk16e8bdf82017-04-11 17:00:39137 PipelineStatus status);
138
139 // Restart audio/video renderer playback after a demuxer stream switch or
140 // after a demuxer stream has been disabled and re-enabled. The |stream|
141 // parameter specifies which stream needs to be restarted. The |time|
142 // parameter specifies the position on the media timeline where the playback
143 // needs to be restarted. It is necessary for demuxers with independent
144 // streams (e.g. MSE / ChunkDemuxer) to synchronize data reading between those
145 // streams.
Ted Meyerda82b602018-04-19 00:36:56146 void RestartAudioRenderer(DemuxerStream* stream,
147 base::TimeDelta time,
148 base::OnceClosure restart_completed_cb);
149 void RestartVideoRenderer(DemuxerStream* stream,
150 base::TimeDelta time,
151 base::OnceClosure restart_completed_cb);
152
153 // Fix state booleans after the stream switching is finished.
154 void CleanUpTrackChange(base::RepeatingClosure on_finished,
Ted Meyerda82b602018-04-19 00:36:56155 bool* ended,
156 bool* playing);
servolkf25ceed2016-07-01 03:44:38157
xhwangbe9da702014-08-23 21:44:55158 // Callback executed by filters to update statistics.
alokp16bbeea2016-05-12 23:32:36159 void OnStatisticsUpdate(const PipelineStatistics& stats);
xhwangbe9da702014-08-23 21:44:55160
161 // Collection of callback methods and helpers for tracking changes in
162 // buffering state and transition from paused/underflow states and playing
163 // states.
164 //
165 // While in the kPlaying state:
166 // - A waiting to non-waiting transition indicates preroll has completed
167 // and StartPlayback() should be called
168 // - A non-waiting to waiting transition indicates underflow has occurred
169 // and PausePlayback() should be called
alokp16bbeea2016-05-12 23:32:36170 void OnBufferingStateChange(DemuxerStream::Type type,
Chris Cunninghamfc0d67e2019-07-22 20:29:16171 BufferingState new_buffering_state,
172 BufferingStateChangeReason reason);
servolk16e8bdf82017-04-11 17:00:39173
servolkf25ceed2016-07-01 03:44:38174 // Handles the buffering notifications that we might get while an audio or a
175 // video stream is being restarted. In those cases we don't want to report
176 // underflows immediately and instead give decoders a chance to catch up with
177 // currently playing stream. Returns true if the buffering nofication has been
178 // handled and no further processing is necessary, returns false to indicate
179 // that we should fall back to the regular OnBufferingStateChange logic.
180 bool HandleRestartedStreamBufferingChanges(
181 DemuxerStream::Type type,
182 BufferingState new_buffering_state);
servolk16e8bdf82017-04-11 17:00:39183
xhwangbe9da702014-08-23 21:44:55184 bool WaitingForEnoughData() const;
185 void PausePlayback();
186 void StartPlayback();
187
xhwangbe9da702014-08-23 21:44:55188 // Callbacks executed when a renderer has ended.
alokp16bbeea2016-05-12 23:32:36189 void OnRendererEnded(DemuxerStream::Type type);
scherkusece89452014-09-09 23:13:38190 bool PlaybackHasEnded() const;
xhwangbe9da702014-08-23 21:44:55191 void RunEndedCallbackIfNeeded();
192
193 // Callback executed when a runtime error happens.
194 void OnError(PipelineStatus error);
Chris Cunningham038548b2017-07-10 22:36:30195
Xiaohan Wang640b41d2018-12-18 19:00:46196 void OnWaiting(WaitingReason reason);
alokp5d86e9b2016-05-17 20:20:41197 void OnVideoNaturalSizeChange(const gfx::Size& size);
Chris Cunningham038548b2017-07-10 22:36:30198 void OnAudioConfigChange(const AudioDecoderConfig& config);
199 void OnVideoConfigChange(const VideoDecoderConfig& config);
alokp5d86e9b2016-05-17 20:20:41200 void OnVideoOpacityChange(bool opaque);
liberato@chromium.org218ac8de2020-01-03 23:41:43201 void OnVideoFrameRateChange(base::Optional<int> fps);
xhwangbe9da702014-08-23 21:44:55202
servolkbff54a22017-01-11 01:39:00203 void OnStreamRestartCompleted();
204
xhwangbe9da702014-08-23 21:44:55205 State state_;
206
207 // Task runner used to execute pipeline tasks.
208 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
209
servolkcfc91f152017-02-02 05:11:23210 MediaResource* media_resource_;
alokp16bbeea2016-05-12 23:32:36211 RendererClient* client_;
xhwangbe9da702014-08-23 21:44:55212
213 // Temporary callback used for Initialize() and Flush().
Chris Mumford84f095f2019-10-25 23:19:44214 PipelineStatusCallback init_cb_;
Chris Mumford1ccf4512019-10-25 19:34:05215 base::OnceClosure flush_cb_;
xhwangbe9da702014-08-23 21:44:55216
alokp16bbeea2016-05-12 23:32:36217 std::unique_ptr<RendererClientInternal> audio_renderer_client_;
218 std::unique_ptr<RendererClientInternal> video_renderer_client_;
danakj4d43bc22016-04-26 03:36:04219 std::unique_ptr<AudioRenderer> audio_renderer_;
220 std::unique_ptr<VideoRenderer> video_renderer_;
xhwangbe9da702014-08-23 21:44:55221
servolk16e8bdf82017-04-11 17:00:39222 DemuxerStream* current_audio_stream_;
223 DemuxerStream* current_video_stream_;
224
xhwangbe9da702014-08-23 21:44:55225 // Renderer-provided time source used to control playback.
226 TimeSource* time_source_;
danakj4d43bc22016-04-26 03:36:04227 std::unique_ptr<WallClockTimeSource> wall_clock_time_source_;
scherkusece89452014-09-09 23:13:38228 bool time_ticking_;
a.berwal338bf002015-04-22 11:14:50229 double playback_rate_;
xhwangbe9da702014-08-23 21:44:55230
231 // The time to start playback from after starting/seeking has completed.
232 base::TimeDelta start_time_;
233
234 BufferingState audio_buffering_state_;
235 BufferingState video_buffering_state_;
236
237 // Whether we've received the audio/video ended events.
238 bool audio_ended_;
239 bool video_ended_;
Ted Meyerda82b602018-04-19 00:36:56240 bool audio_playing_;
241 bool video_playing_;
xhwangbe9da702014-08-23 21:44:55242
xhwang97de4202014-11-25 08:44:01243 CdmContext* cdm_context_;
xhwang97de4202014-11-25 08:44:01244
xhwangbe9da702014-08-23 21:44:55245 bool underflow_disabled_for_testing_;
scherkusece89452014-09-09 23:13:38246 bool clockless_video_playback_enabled_for_testing_;
xhwangbe9da702014-08-23 21:44:55247
dalecurtisc11e7bb2015-04-03 05:07:08248 // Used to defer underflow for video when audio is present.
servolkf25ceed2016-07-01 03:44:38249 base::CancelableClosure deferred_video_underflow_cb_;
250
251 // Used to defer underflow for audio when restarting audio playback.
252 base::CancelableClosure deferred_audio_restart_underflow_cb_;
dalecurtisc11e7bb2015-04-03 05:07:08253
254 // The amount of time to wait before declaring underflow if the video renderer
255 // runs out of data but the audio renderer still has enough.
liberato@chromium.org8705ffc2020-07-28 20:11:55256 Tuneable<base::TimeDelta> video_underflow_threshold_ = {
257 "MediaVideoUnderflowThreshold", base::TimeDelta::FromMilliseconds(1000),
258 base::TimeDelta::FromMilliseconds(3000),
259 base::TimeDelta::FromMilliseconds(8000)};
dalecurtisc11e7bb2015-04-03 05:07:08260
servolk9dd6cc82017-05-24 05:39:02261 // Lock used to protect access to the |restarting_audio_| flag and
262 // |restarting_audio_time_|.
263 // TODO(servolk): Get rid of the lock and replace restarting_audio_ with
264 // std::atomic<bool> when atomics are unbanned in Chromium.
265 base::Lock restarting_audio_lock_;
Ted Meyerda82b602018-04-19 00:36:56266 bool pending_audio_track_change_ = false;
servolk9dd6cc82017-05-24 05:39:02267 base::TimeDelta restarting_audio_time_ = kNoTimestamp;
268
Ted Meyerda82b602018-04-19 00:36:56269 bool pending_video_track_change_ = false;
Dale Curtis3defa542017-11-07 00:52:32270
xhwangbe9da702014-08-23 21:44:55271 base::WeakPtr<RendererImpl> weak_this_;
Jeremy Roman32948392019-07-09 18:34:37272 base::WeakPtrFactory<RendererImpl> weak_factory_{this};
xhwangbe9da702014-08-23 21:44:55273
274 DISALLOW_COPY_AND_ASSIGN(RendererImpl);
275};
276
277} // namespace media
278
servolkf54f5c8f2015-02-24 20:32:39279#endif // MEDIA_RENDERERS_RENDERER_IMPL_H_