blob: ecd6cd200b139bc9af385683e791be0a02c996a1 [file] [log] [blame]
[email protected]85a37afd2013-05-30 22:51:151// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]891acc92009-04-27 19:56:412// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]ec9212f2008-12-18 21:40:364
acolwell9e0840d2014-09-06 19:01:325#include "media/blink/webmediaplayer_impl.h"
[email protected]8931c41a2009-07-07 17:31:496
[email protected]c2a45e242013-02-15 20:24:587#include <algorithm>
mateuszs3371ab02015-04-24 13:20:238#include <cmath>
[email protected]47b06ceb2010-08-04 22:41:119#include <limits>
Gyuyoung Kim62a5de42018-01-10 09:48:4210#include <memory>
guidouc7babef2015-10-22 00:42:3511#include <string>
dcheng652f5ff2015-12-27 08:54:0012#include <utility>
[email protected]47b06ceb2010-08-04 22:41:1113
[email protected]08273c7b2011-09-17 00:33:5114#include "base/bind.h"
sandersd1c0bba02016-03-04 23:14:0815#include "base/bind_helpers.h"
[email protected]2041cf342010-02-19 03:15:5916#include "base/callback.h"
[email protected]7502ef12014-01-25 01:19:2717#include "base/callback_helpers.h"
sandersd1e49fb62015-12-12 01:18:0618#include "base/command_line.h"
[email protected]ca04095f2014-02-07 10:23:5619#include "base/debug/alias.h"
[email protected]926f8fd2013-04-12 20:27:5320#include "base/debug/crash_logging.h"
fdoraydb3ef7d2016-06-09 15:42:3821#include "base/location.h"
Dale Curtis3899090ea2018-01-12 00:10:3522#include "base/metrics/histogram_functions.h"
asvitkine30330812016-08-30 04:01:0823#include "base/metrics/histogram_macros.h"
acolwellb4034942014-08-28 15:42:4324#include "base/single_thread_task_runner.h"
servolka233f832016-06-10 20:39:0425#include "base/strings/string_number_conversions.h"
Gabriel Charette44db1422018-08-06 11:19:3326#include "base/task/post_task.h"
wdzierzanowskifd4cd91c52015-12-02 23:50:2027#include "base/task_runner_util.h"
gabba14a442016-05-11 20:10:2028#include "base/threading/thread_task_runner_handle.h"
ssid9525f4672015-01-28 12:13:1529#include "base/trace_event/trace_event.h"
sandersd1e49fb62015-12-12 01:18:0630#include "build/build_config.h"
[email protected]21c3f7502013-03-23 03:29:5131#include "cc/layers/video_layer.h"
CJ DiMeglioc60a5cf2017-09-27 20:08:4132#include "components/viz/common/gpu/context_provider.h"
[email protected]e4fc09e2012-04-06 03:17:4433#include "media/audio/null_audio_sink.h"
[email protected]2eccbed2014-01-10 05:15:5334#include "media/base/bind_to_current_loop.h"
xhwang0ad11e512014-11-25 23:43:0935#include "media/base/cdm_context.h"
John Rummelldb5a7ef2018-05-16 00:28:0136#include "media/base/encryption_scheme.h"
[email protected]32da1002010-03-03 21:57:3537#include "media/base/limits.h"
zqzhang5d8eab72016-08-26 20:34:3038#include "media/base/media_content_type.h"
[email protected]090f7312011-08-05 23:26:4039#include "media/base/media_log.h"
sandersd1e49fb62015-12-12 01:18:0640#include "media/base/media_switches.h"
tguilbert25a4d112016-10-13 21:56:5141#include "media/base/media_url_demuxer.h"
[email protected]8a561062013-11-22 01:19:3142#include "media/base/text_renderer.h"
watk9f9dfdc92015-09-04 21:33:2943#include "media/base/timestamp_constants.h"
[email protected]e81283bb2010-08-31 18:01:2144#include "media/base/video_frame.h"
acolwell9e0840d2014-09-06 19:01:3245#include "media/blink/texttrack_impl.h"
John Delaneyb933391602018-10-17 21:50:4746#include "media/blink/url_index.h"
Chris Cunninghamd9df58e2017-08-29 00:04:2347#include "media/blink/video_decode_stats_reporter.h"
dalecurtis04bdb582016-08-17 22:15:2348#include "media/blink/watch_time_reporter.h"
xhwang97de4202014-11-25 08:44:0149#include "media/blink/webcontentdecryptionmodule_impl.h"
acolwell9e0840d2014-09-06 19:01:3250#include "media/blink/webinbandtexttrack_impl.h"
acolwell9e0840d2014-09-06 19:01:3251#include "media/blink/webmediasource_impl.h"
[email protected]efe7cd22012-09-12 23:55:0152#include "media/filters/chunk_demuxer.h"
[email protected]ddbc6ff2013-04-19 15:28:3353#include "media/filters/ffmpeg_demuxer.h"
Dale Curtis4841c712018-12-13 18:14:0554#include "media/filters/memory_data_source.h"
Scott Violeta35f9a42018-03-22 22:00:4455#include "media/media_buildflags.h"
Gyuyoung Kima040bc52019-10-30 01:14:3556#include "mojo/public/cpp/bindings/pending_remote.h"
Dale Curtis4841c712018-12-13 18:14:0557#include "net/base/data_url.h"
Blink Reformata30d4232018-04-07 15:31:0658#include "third_party/blink/public/platform/web_encrypted_media_types.h"
Blink Reformata30d4232018-04-07 15:31:0659#include "third_party/blink/public/platform/web_media_player_client.h"
60#include "third_party/blink/public/platform/web_media_player_encrypted_media_client.h"
61#include "third_party/blink/public/platform/web_media_player_source.h"
62#include "third_party/blink/public/platform/web_media_source.h"
63#include "third_party/blink/public/platform/web_rect.h"
64#include "third_party/blink/public/platform/web_runtime_features.h"
65#include "third_party/blink/public/platform/web_security_origin.h"
66#include "third_party/blink/public/platform/web_size.h"
67#include "third_party/blink/public/platform/web_string.h"
68#include "third_party/blink/public/platform/web_surface_layer_bridge.h"
69#include "third_party/blink/public/platform/web_url.h"
Antonio Gomes3a858b52019-05-31 02:47:5270#include "third_party/blink/public/platform/webaudiosourceprovider_impl.h"
Gyuyoung Kimf99f5f32019-09-26 03:45:1571#include "third_party/blink/public/strings/grit/blink_strings.h"
Antonio Gomesf01cfbd2019-07-12 08:53:1172#include "third_party/blink/public/web/modules/media/webmediaplayer_util.h"
Blink Reformata30d4232018-04-07 15:31:0673#include "third_party/blink/public/web/web_document.h"
74#include "third_party/blink/public/web/web_frame.h"
75#include "third_party/blink/public/web/web_local_frame.h"
Blink Reformata30d4232018-04-07 15:31:0676#include "third_party/blink/public/web/web_view.h"
[email protected]b3f2b912009-04-09 16:18:5277
dalecurtisea27a3ed2016-06-24 01:41:3078#if defined(OS_ANDROID)
79#include "media/base/android/media_codec_util.h"
80#endif
81
[email protected]180ef242013-11-07 06:50:4682using blink::WebMediaPlayer;
83using blink::WebRect;
84using blink::WebSize;
85using blink::WebString;
hubbed5f36882016-01-15 22:40:3786using gpu::gles2::GLES2Interface;
87
danakj365175c2016-02-06 00:37:3788#define STATIC_ASSERT_ENUM(a, b) \
89 static_assert(static_cast<int>(a) == static_cast<int>(b), \
90 "mismatching enums: " #a)
91
hubbed5f36882016-01-15 22:40:3792namespace media {
[email protected]ec9212f2008-12-18 21:40:3693
[email protected]8931c41a2009-07-07 17:31:4994namespace {
95
Dan Sanders6edfd782019-08-13 00:13:1896const char kWatchTimeHistogram[] = "Media.WebMediaPlayerImpl.WatchTime";
97
Xiaohan Wang48aaa192019-12-05 01:29:0098void RecordSimpleWatchTimeUMA(RendererFactoryType type) {
99 UMA_HISTOGRAM_ENUMERATION(kWatchTimeHistogram, type);
Dan Sanders6edfd782019-08-13 00:13:18100}
101
Antonio Gomes3a858b52019-05-31 02:47:52102void SetSinkIdOnMediaThread(
103 scoped_refptr<blink::WebAudioSourceProviderImpl> sink,
104 const std::string& device_id,
105 OutputDeviceStatusCB callback) {
Daniel Chengc1710b52018-10-24 03:12:28106 sink->SwitchOutputDevice(device_id, std::move(callback));
guidouc7babef2015-10-22 00:42:35107}
108
Sergey Volk8b09c2c52018-12-12 23:20:40109bool IsBackgroundSuspendEnabled(const WebMediaPlayerImpl* wmpi) {
Luke Halliwell7a8a8982018-07-25 01:07:05110 // TODO(crbug.com/867146): remove these switches.
111 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
112 switches::kDisableMediaSuspend))
113 return false;
114 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
115 switches::kEnableMediaSuspend))
116 return true;
117
Sergey Volk8b09c2c52018-12-12 23:20:40118 return wmpi->IsBackgroundMediaSuspendEnabled();
dalecurtis0431cbf2016-03-12 01:19:43119}
120
avayvod48a8be52016-08-04 19:52:50121bool IsResumeBackgroundVideosEnabled() {
122 return base::FeatureList::IsEnabled(kResumeBackgroundVideo);
123}
124
avayvod01201332017-04-14 00:27:15125bool IsBackgroundVideoPauseOptimizationEnabled() {
126 return base::FeatureList::IsEnabled(kBackgroundVideoPauseOptimization);
127}
128
sandersd50a635e2016-04-04 22:50:09129bool IsNetworkStateError(blink::WebMediaPlayer::NetworkState state) {
Blink Reformat1c4d759e2017-04-09 16:34:54130 bool result = state == blink::WebMediaPlayer::kNetworkStateFormatError ||
131 state == blink::WebMediaPlayer::kNetworkStateNetworkError ||
132 state == blink::WebMediaPlayer::kNetworkStateDecodeError;
133 DCHECK_EQ(state > blink::WebMediaPlayer::kNetworkStateLoaded, result);
sandersd50a635e2016-04-04 22:50:09134 return result;
135}
136
sandersd2c478422016-08-02 01:19:25137gfx::Size GetRotatedVideoSize(VideoRotation rotation, gfx::Size natural_size) {
138 if (rotation == VIDEO_ROTATION_90 || rotation == VIDEO_ROTATION_270)
139 return gfx::Size(natural_size.height(), natural_size.width());
140 return natural_size;
141}
142
Xiaohan Wangf63505d2017-10-21 08:00:53143void RecordEncryptedEvent(bool encrypted_event_fired) {
144 UMA_HISTOGRAM_BOOLEAN("Media.EME.EncryptedEvent", encrypted_event_fired);
145}
146
sandersd35d2c3f2017-01-14 02:04:42147// How much time must have elapsed since loading last progressed before we
148// assume that the decoder will have had time to complete preroll.
149constexpr base::TimeDelta kPrerollAttemptTimeout =
watkd026f792016-11-05 00:28:51150 base::TimeDelta::FromSeconds(3);
151
Matt Wolenetz6010f6c2017-10-18 00:44:36152// Maximum number, per-WMPI, of media logs of playback rate changes.
153constexpr int kMaxNumPlaybackRateLogs = 10;
154
Gyuyoung Kimf99f5f32019-09-26 03:45:15155int GetSwitchToLocalMessage(MediaObserverClient::ReasonToSwitchToLocal reason) {
Xiangjun Zhang5e20cba42018-01-10 19:54:56156 switch (reason) {
157 case MediaObserverClient::ReasonToSwitchToLocal::NORMAL:
Gyuyoung Kimf99f5f32019-09-26 03:45:15158 return IDS_MEDIA_REMOTING_STOP_TEXT;
Xiangjun Zhang5e20cba42018-01-10 19:54:56159 case MediaObserverClient::ReasonToSwitchToLocal::POOR_PLAYBACK_QUALITY:
Gyuyoung Kimf99f5f32019-09-26 03:45:15160 return IDS_MEDIA_REMOTING_STOP_BY_PLAYBACK_QUALITY_TEXT;
Xiangjun Zhang5e20cba42018-01-10 19:54:56161 case MediaObserverClient::ReasonToSwitchToLocal::PIPELINE_ERROR:
Gyuyoung Kimf99f5f32019-09-26 03:45:15162 return IDS_MEDIA_REMOTING_STOP_BY_ERROR_TEXT;
Xiangjun Zhang5e20cba42018-01-10 19:54:56163 case MediaObserverClient::ReasonToSwitchToLocal::ROUTE_TERMINATED:
Gyuyoung Kimf99f5f32019-09-26 03:45:15164 return blink::WebMediaPlayerClient::kMediaRemotingStopNoText;
Xiangjun Zhang5e20cba42018-01-10 19:54:56165 }
166 NOTREACHED();
167 // To suppress compiler warning on Windows.
Gyuyoung Kimf99f5f32019-09-26 03:45:15168 return blink::WebMediaPlayerClient::kMediaRemotingStopNoText;
Xiangjun Zhang5e20cba42018-01-10 19:54:56169}
170
John Rummelldb5a7ef2018-05-16 00:28:01171// These values are persisted to UMA. Entries should not be renumbered and
172// numeric values should never be reused.
Yuchen Liub33bfc12019-11-08 20:16:12173// TODO(crbug.com/825041): This should use EncryptionScheme when kUnencrypted
John Rummelldb5a7ef2018-05-16 00:28:01174// removed.
175enum class EncryptionSchemeUMA { kCenc = 0, kCbcs = 1, kCount };
176
177EncryptionSchemeUMA DetermineEncryptionSchemeUMAValue(
Yuchen Liub33bfc12019-11-08 20:16:12178 EncryptionScheme encryption_scheme) {
179 if (encryption_scheme == EncryptionScheme::kCbcs)
John Rummelldb5a7ef2018-05-16 00:28:01180 return EncryptionSchemeUMA::kCbcs;
181
Yuchen Liub33bfc12019-11-08 20:16:12182 DCHECK_EQ(encryption_scheme, EncryptionScheme::kCenc);
John Rummelldb5a7ef2018-05-16 00:28:01183 return EncryptionSchemeUMA::kCenc;
184}
185
Pavel Feldman023c3e62018-08-28 17:59:47186#if BUILDFLAG(ENABLE_FFMPEG)
Dale Curtisb8139f72018-08-27 23:28:48187// Returns true if |url| represents (or is likely to) a local file.
188bool IsLocalFile(const GURL& url) {
189 return url.SchemeIsFile() || url.SchemeIsFileSystem() ||
190 url.SchemeIs(url::kContentScheme) ||
191 url.SchemeIs(url::kContentIDScheme) ||
192 url.SchemeIs("chrome-extension");
193}
Pavel Feldman023c3e62018-08-28 17:59:47194#endif
Dale Curtisb8139f72018-08-27 23:28:48195
Dale Curtisf273f8f2018-12-13 23:40:33196// Handles destruction of media::Renderer dependent components after the
197// renderer has been destructed on the media thread.
198void DestructionHelper(
199 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
200 scoped_refptr<base::SingleThreadTaskRunner> vfc_task_runner,
201 std::unique_ptr<Demuxer> demuxer,
202 std::unique_ptr<DataSource> data_source,
203 std::unique_ptr<VideoFrameCompositor> compositor,
204 std::unique_ptr<CdmContextRef> cdm_context_1,
205 std::unique_ptr<CdmContextRef> cdm_context_2,
206 std::unique_ptr<MediaLog> media_log,
207 std::unique_ptr<RendererFactorySelector> renderer_factory_selector,
208 std::unique_ptr<blink::WebSurfaceLayerBridge> bridge,
209 bool is_chunk_demuxer) {
210 // We release |bridge| after pipeline stop to ensure layout tests receive
211 // painted video frames before test harness exit.
212 main_task_runner->DeleteSoon(FROM_HERE, std::move(bridge));
213
214 // Since the media::Renderer is gone we can now destroy the compositor and
215 // renderer factory selector.
216 vfc_task_runner->DeleteSoon(FROM_HERE, std::move(compositor));
217 main_task_runner->DeleteSoon(FROM_HERE, std::move(renderer_factory_selector));
218
219 // ChunkDemuxer can be deleted on any thread, but other demuxers are bound to
220 // the main thread and must be deleted there now that the renderer is gone.
221 if (!is_chunk_demuxer) {
222 main_task_runner->DeleteSoon(FROM_HERE, std::move(demuxer));
223 main_task_runner->DeleteSoon(FROM_HERE, std::move(data_source));
224 main_task_runner->DeleteSoon(FROM_HERE, std::move(cdm_context_1));
225 main_task_runner->DeleteSoon(FROM_HERE, std::move(cdm_context_2));
226 main_task_runner->DeleteSoon(FROM_HERE, std::move(media_log));
227 return;
228 }
229
230 // ChunkDemuxer's streams may contain much buffered, compressed media that
231 // may need to be paged back in during destruction. Paging delay may exceed
232 // the renderer hang monitor's threshold on at least Windows while also
233 // blocking other work on the renderer main thread, so we do the actual
234 // destruction in the background without blocking WMPI destruction or
235 // |task_runner|. On advice of task_scheduler OWNERS, MayBlock() is not
236 // used because virtual memory overhead is not considered blocking I/O; and
237 // CONTINUE_ON_SHUTDOWN is used to allow process termination to not block on
238 // completing the task.
Sami Kyostila127d6632019-08-06 21:32:25239 base::PostTask(
Dale Curtisf273f8f2018-12-13 23:40:33240 FROM_HERE,
Sami Kyostila127d6632019-08-06 21:32:25241 {base::ThreadPool(), base::TaskPriority::BEST_EFFORT,
Dale Curtisf273f8f2018-12-13 23:40:33242 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
243 base::BindOnce(
244 [](scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
245 std::unique_ptr<Demuxer> demuxer_to_destroy,
246 std::unique_ptr<CdmContextRef> cdm_context_1,
247 std::unique_ptr<CdmContextRef> cdm_context_2,
248 std::unique_ptr<MediaLog> media_log) {
249 SCOPED_UMA_HISTOGRAM_TIMER("Media.MSE.DemuxerDestructionTime");
250 demuxer_to_destroy.reset();
251 main_task_runner->DeleteSoon(FROM_HERE, std::move(cdm_context_1));
252 main_task_runner->DeleteSoon(FROM_HERE, std::move(cdm_context_2));
253 main_task_runner->DeleteSoon(FROM_HERE, std::move(media_log));
254 },
255 std::move(main_task_runner), std::move(demuxer),
256 std::move(cdm_context_1), std::move(cdm_context_2),
257 std::move(media_log)));
258}
259
Ted Meyer7f5b4e22019-11-21 03:21:19260std::string SanitizeUserStringProperty(blink::WebString value) {
Ted Meyera698afe2019-11-06 20:58:03261 std::string converted = value.Utf8();
Ted Meyer7f5b4e22019-11-21 03:21:19262 return base::IsStringUTF8(converted) ? converted : "[invalid property]";
Ted Meyera698afe2019-11-06 20:58:03263}
264
[email protected]8931c41a2009-07-07 17:31:49265} // namespace
266
[email protected]6683e1b2014-04-10 01:45:38267class BufferedDataSourceHostImpl;
268
Takashi Toyoshima2e01e692018-11-16 03:23:27269STATIC_ASSERT_ENUM(WebMediaPlayer::kCorsModeUnspecified,
danakj365175c2016-02-06 00:37:37270 UrlData::CORS_UNSPECIFIED);
Takashi Toyoshima2e01e692018-11-16 03:23:27271STATIC_ASSERT_ENUM(WebMediaPlayer::kCorsModeAnonymous, UrlData::CORS_ANONYMOUS);
272STATIC_ASSERT_ENUM(WebMediaPlayer::kCorsModeUseCredentials,
danakj365175c2016-02-06 00:37:37273 UrlData::CORS_USE_CREDENTIALS);
[email protected]a5a01102012-06-06 17:01:24274
[email protected]5b5bb9d2010-10-22 19:57:36275WebMediaPlayerImpl::WebMediaPlayerImpl(
[email protected]35b2a972014-04-04 15:50:22276 blink::WebLocalFrame* frame,
[email protected]180ef242013-11-07 06:50:46277 blink::WebMediaPlayerClient* client,
srirama.m26f864d02015-07-14 05:21:46278 blink::WebMediaPlayerEncryptedMediaClient* encrypted_client,
Antonio Gomes423df612019-07-11 12:40:07279 blink::WebMediaPlayerDelegate* delegate,
tguilbert70d2a00a2017-04-25 00:30:44280 std::unique_ptr<RendererFactorySelector> renderer_factory_selector,
Alok Priyadarshi7166d4d2017-07-29 04:04:25281 UrlIndex* url_index,
CJ DiMegliod7ac6772017-11-10 00:19:06282 std::unique_ptr<VideoFrameCompositor> compositor,
dalecurtis9cddc0b2017-04-19 21:23:38283 std::unique_ptr<WebMediaPlayerParams> params)
[email protected]f6af7592014-02-28 10:09:11284 : frame_(frame),
Alexander Timin310368112017-09-13 10:01:44285 main_task_runner_(
286 frame->GetTaskRunner(blink::TaskType::kMediaElementEvent)),
dalecurtis9cddc0b2017-04-19 21:23:38287 media_task_runner_(params->media_task_runner()),
288 worker_task_runner_(params->worker_task_runner()),
289 media_log_(params->take_media_log()),
[email protected]5badb082010-06-11 17:40:15290 client_(client),
srirama.m26f864d02015-07-14 05:21:46291 encrypted_client_(encrypted_client),
[email protected]baff4512011-10-19 18:21:07292 delegate_(delegate),
dalecurtis9cddc0b2017-04-19 21:23:38293 defer_load_cb_(params->defer_load_cb()),
dalecurtis9cddc0b2017-04-19 21:23:38294 adjust_allocated_memory_cb_(params->adjust_allocated_memory_cb()),
tzik2c963b872017-12-07 06:57:24295 tick_clock_(base::DefaultTickClock::GetInstance()),
hubbe5f0ad43b2015-12-14 20:57:26296 url_index_(url_index),
CJ DiMeglioc60a5cf2017-09-27 20:08:41297 context_provider_(params->context_provider()),
CJ DiMegliod7ac6772017-11-10 00:19:06298 vfc_task_runner_(params->video_frame_compositor_task_runner()),
299 compositor_(std::move(compositor)),
tguilbert70d2a00a2017-04-25 00:30:44300 renderer_factory_selector_(std::move(renderer_factory_selector)),
dalecurtis9cddc0b2017-04-19 21:23:38301 observer_(params->media_observer()),
servolkf94b4602017-01-31 16:44:27302 enable_instant_source_buffer_gc_(
dalecurtis9cddc0b2017-04-19 21:23:38303 params->enable_instant_source_buffer_gc()),
shaktisahu24189c12017-03-11 17:42:55304 embedded_media_experience_enabled_(
liberato2ff93ad2017-05-17 07:28:24305 params->embedded_media_experience_enabled()),
[email protected]69db58f2018-09-26 20:27:56306 surface_layer_mode_(params->use_surface_layer_for_video()),
CJ DiMeglio96c18b692018-07-04 03:39:06307 create_bridge_callback_(params->create_bridge_callback()),
liberato2ff93ad2017-05-17 07:28:24308 request_routing_token_cb_(params->request_routing_token_cb()),
Dale Curtis1adbe6a2017-08-02 02:09:13309 overlay_routing_token_(OverlayInfo::RoutingToken()),
Sergey Volk8b09c2c52018-12-12 23:20:40310 media_metrics_provider_(params->take_metrics_provider()),
311 is_background_suspend_enabled_(params->IsBackgroundSuspendEnabled()),
Junbo Kefba620b2019-01-16 02:54:36312 is_background_video_playback_enabled_(
313 params->IsBackgroundVideoPlaybackEnabled()),
Sergey Volk8b09c2c52018-12-12 23:20:40314 is_background_video_track_optimization_supported_(
Dan Sanders6edfd782019-08-13 00:13:18315 params->IsBackgroundVideoTrackOptimizationSupported()),
Xiaohan Wang48aaa192019-12-05 01:29:00316 reported_renderer_type_(RendererFactoryType::kDefault),
Dan Sanders6edfd782019-08-13 00:13:18317 simple_watch_timer_(
318 base::BindRepeating(&WebMediaPlayerImpl::OnSimpleWatchTimerTick,
319 base::Unretained(this)),
320 base::BindRepeating(&WebMediaPlayerImpl::GetCurrentTimeInternal,
[email protected]91fd3162019-09-05 23:51:32321 base::Unretained(this))),
[email protected]dc996312019-12-19 19:26:38322 will_play_helper_(nullptr),
323 power_status_helper_(params->TakePowerStatusHelper()) {
xhwang51139732017-02-24 19:36:08324 DVLOG(1) << __func__;
Dale Curtise25163812018-09-21 22:13:39325 DCHECK(adjust_allocated_memory_cb_);
tguilbert70d2a00a2017-04-25 00:30:44326 DCHECK(renderer_factory_selector_);
servolkef1e5ef2016-03-25 04:55:26327 DCHECK(client_);
tguilbert1bb1c782017-01-23 21:15:11328 DCHECK(delegate_);
dalecurtis83266c72015-10-29 18:43:20329
Antonio Gomes142f32a2019-05-15 23:32:56330 weak_this_ = weak_factory_.GetWeakPtr();
331
Xiaohan Wang2480de72019-11-15 01:44:47332 // Using base::Unretained(this) is safe because the |pipeline| is owned by
333 // |this| and the callback will always be made on the main task runner.
334 // Not using BindToCurrentLoop() because CreateRenderer() is a sync call.
335 auto pipeline = std::make_unique<PipelineImpl>(
336 media_task_runner_, main_task_runner_,
337 base::BindRepeating(&WebMediaPlayerImpl::CreateRenderer,
338 base::Unretained(this)),
339 media_log_.get());
340
Antonio Gomes81b56552019-05-15 22:15:28341 pipeline_controller_ = std::make_unique<PipelineController>(
Xiaohan Wang2480de72019-11-15 01:44:47342 std::move(pipeline),
Antonio Gomes142f32a2019-05-15 23:32:56343 base::BindRepeating(&WebMediaPlayerImpl::OnPipelineSeeked, weak_this_),
344 base::BindRepeating(&WebMediaPlayerImpl::OnPipelineSuspended, weak_this_),
Antonio Gomes81b56552019-05-15 22:15:28345 base::BindRepeating(&WebMediaPlayerImpl::OnBeforePipelineResume,
Antonio Gomes142f32a2019-05-15 23:32:56346 weak_this_),
347 base::BindRepeating(&WebMediaPlayerImpl::OnPipelineResumed, weak_this_),
348 base::BindRepeating(&WebMediaPlayerImpl::OnError, weak_this_));
Antonio Gomes81b56552019-05-15 22:15:28349
350 buffered_data_source_host_ = std::make_unique<BufferedDataSourceHostImpl>(
Antonio Gomes142f32a2019-05-15 23:32:56351 base::BindRepeating(&WebMediaPlayerImpl::OnProgress, weak_this_),
Antonio Gomes81b56552019-05-15 22:15:28352 tick_clock_);
353
[email protected]c8d574722017-08-30 20:53:43354 // If we're supposed to force video overlays, then make sure that they're
355 // enabled all the time.
356 always_enable_overlays_ = base::CommandLine::ForCurrentProcess()->HasSwitch(
tsunghungee562e92016-07-20 18:03:31357 switches::kForceVideoOverlays);
358
Jinsong Fan982342112019-11-07 23:45:45359 if (base::FeatureList::IsEnabled(kOverlayFullscreenVideo))
360 overlay_mode_ = OverlayMode::kUseAndroidOverlay;
361 else
liberato2ff93ad2017-05-17 07:28:24362 overlay_mode_ = OverlayMode::kNoOverlays;
ampea73f792017-01-19 04:05:39363
tguilbert1bb1c782017-01-23 21:15:11364 delegate_id_ = delegate_->AddObserver(this);
365 delegate_->SetIdle(delegate_id_, true);
sandersd1e49fb62015-12-12 01:18:06366
Ted Meyer380ca982020-01-12 01:00:29367 media_log_->AddLogRecord(media_log_->CreateCreatedEvent(
dalecurtis2cff7f372017-05-24 08:30:08368 url::Origin(frame_->GetSecurityOrigin()).GetURL().spec()));
Ted Meyera698afe2019-11-06 20:58:03369
Ted Meyer7f5b4e22019-11-21 03:21:19370 media_log_->SetProperty<MediaLogProperty::kFrameUrl>(
371 SanitizeUserStringProperty(frame_->GetDocument().Url().GetString()));
372 media_log_->SetProperty<MediaLogProperty::kFrameTitle>(
373 SanitizeUserStringProperty(frame_->GetDocument().Title()));
[email protected]52b0b212018-10-10 05:25:28374
dalecurtis9cddc0b2017-04-19 21:23:38375 if (params->initial_cdm())
Chris Cunninghamd4a00192019-03-21 20:02:49376 SetCdmInternal(params->initial_cdm());
xhwang0ad11e512014-11-25 23:43:09377
Xiaohan Wangf63505d2017-10-21 08:00:53378 // Report a false "EncrytpedEvent" here as a baseline.
379 RecordEncryptedEvent(false);
380
xhwangf94a634d2014-10-22 22:07:27381 // TODO(xhwang): When we use an external Renderer, many methods won't work,
xhwang6fa356202014-12-11 00:44:12382 // e.g. GetCurrentFrameFromCompositor(). See http://crbug.com/434861
Antonio Gomes3a858b52019-05-31 02:47:52383 audio_source_provider_ = new blink::WebAudioSourceProviderImpl(
dalecurtis9cddc0b2017-04-19 21:23:38384 params->audio_renderer_sink(), media_log_.get());
xjz4e5d4bf32017-02-15 21:26:35385
386 if (observer_)
387 observer_->SetClient(this);
Hajime Hoshi8bb37dc2017-12-19 09:35:10388
389 memory_usage_reporting_timer_.SetTaskRunner(
Hajime Hoshib5a26ee2018-05-14 12:47:51390 frame_->GetTaskRunner(blink::TaskType::kInternalMedia));
John Delaney6c8ecc1b2018-10-21 19:07:29391
Thomas Guilbert901808982019-07-03 20:38:18392#if defined(OS_ANDROID)
Thomas Guilbertf6d0702e2019-09-12 23:40:14393 renderer_factory_selector_->SetRemotePlayStateChangeCB(
394 BindToCurrentLoop(base::BindRepeating(
395 &WebMediaPlayerImpl::OnRemotePlayStateChange, weak_this_)));
Thomas Guilbert901808982019-07-03 20:38:18396#endif // defined (OS_ANDROID)
[email protected]ec9212f2008-12-18 21:40:36397}
398
[email protected]4e6be3f2009-05-07 02:24:44399WebMediaPlayerImpl::~WebMediaPlayerImpl() {
xhwang51139732017-02-24 19:36:08400 DVLOG(1) << __func__;
acolwellb4034942014-08-28 15:42:43401 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:53402
xhwang51139732017-02-24 19:36:08403 if (set_cdm_result_) {
Chris Cunninghamd4a00192019-03-21 20:02:49404 DVLOG(2)
405 << "Resolve pending SetCdmInternal() when media player is destroyed.";
Blink Reformat1c4d759e2017-04-09 16:34:54406 set_cdm_result_->Complete();
xhwang51139732017-02-24 19:36:08407 set_cdm_result_.reset();
408 }
409
alokp1116967f2016-06-11 17:30:56410 suppress_destruction_errors_ = true;
tguilbert1bb1c782017-01-23 21:15:11411
412 delegate_->PlayerGone(delegate_id_);
413 delegate_->RemoveObserver(delegate_id_);
[email protected]baff4512011-10-19 18:21:07414
dalecurtis04bdb582016-08-17 22:15:23415 // Finalize any watch time metrics before destroying the pipeline.
416 watch_time_reporter_.reset();
417
tguilbert350936ff2017-02-24 05:39:27418 // The underlying Pipeline must be stopped before it is destroyed.
Dale Curtisf273f8f2018-12-13 23:40:33419 //
420 // Note: This destruction happens synchronously on the media thread and
421 // |demuxer_|, |data_source_|, |compositor_|, and |media_log_| must outlive
422 // this process. They will be destructed by the DestructionHelper below
423 // after trampolining through the media thread.
Antonio Gomes81b56552019-05-15 22:15:28424 pipeline_controller_->Stop();
[email protected]f6af7592014-02-28 10:09:11425
dalecurtis83266c72015-10-29 18:43:20426 if (last_reported_memory_usage_)
427 adjust_allocated_memory_cb_.Run(-last_reported_memory_usage_);
428
dalecurtise1edb312016-06-22 02:33:21429 // Destruct compositor resources in the proper order.
danakj8d204a42018-05-18 18:05:35430 client_->SetCcLayer(nullptr);
CJ DiMeglio2302d202017-08-31 08:38:04431
Xiangjun Zhang5e20cba42018-01-10 19:54:56432 client_->MediaRemotingStopped(
Gyuyoung Kimf99f5f32019-09-26 03:45:15433 blink::WebMediaPlayerClient::kMediaRemotingStopNoText);
Xiangjun Zhang87299142017-09-13 20:35:03434
Dale Curtisf273f8f2018-12-13 23:40:33435 if (!surface_layer_for_video_enabled_ && video_layer_)
danakj25f030112018-05-11 18:26:54436 video_layer_->StopUsingProvider();
Matt Wolenetz95af6362018-01-04 20:23:42437
Dan Sanders6edfd782019-08-13 00:13:18438 simple_watch_timer_.Stop();
Ted Meyer257b682a2019-11-01 20:50:51439 media_log_->OnWebMediaPlayerDestroyed();
[email protected]ec9212f2008-12-18 21:40:36440
Dale Curtisf273f8f2018-12-13 23:40:33441 if (data_source_)
442 data_source_->Stop();
443
444 // Disconnect from the surface layer. We still preserve the |bridge_| until
445 // after pipeline shutdown to ensure any pending frames are painted for tests.
446 if (bridge_)
447 bridge_->ClearObserver();
448
Dale Curtisc96a3542018-12-17 22:15:23449 // Disconnect from the MediaObserver implementation since it's lifetime is
450 // tied to the RendererFactorySelector which can't be destroyed until after
451 // the Pipeline stops.
452 //
453 // Note: We can't use a WeakPtr with the RendererFactory because its methods
454 // are called on the media thread and this destruction takes place on the
455 // renderer thread.
456 if (observer_)
457 observer_->SetClient(nullptr);
458
[email protected]91fd3162019-09-05 23:51:32459 // If we're in the middle of an observation, then finish it.
460 will_play_helper_.CompleteObservationIfNeeded(learning::TargetValue(false));
461
Dale Curtisf273f8f2018-12-13 23:40:33462 // Handle destruction of things that need to be destructed after the pipeline
463 // completes stopping on the media thread.
464 media_task_runner_->PostTask(
Matt Wolenetz95af6362018-01-04 20:23:42465 FROM_HERE,
Dale Curtisf273f8f2018-12-13 23:40:33466 base::BindOnce(&DestructionHelper, std::move(main_task_runner_),
467 std::move(vfc_task_runner_), std::move(demuxer_),
468 std::move(data_source_), std::move(compositor_),
469 std::move(cdm_context_ref_),
470 std::move(pending_cdm_context_ref_), std::move(media_log_),
471 std::move(renderer_factory_selector_), std::move(bridge_),
472 !!chunk_demuxer_));
Matt Wolenetz95af6362018-01-04 20:23:42473}
474
chcunningham707b821e2018-05-29 08:42:19475WebMediaPlayer::LoadTiming WebMediaPlayerImpl::Load(
476 LoadType load_type,
477 const blink::WebMediaPlayerSource& source,
Takashi Toyoshima2e01e692018-11-16 03:23:27478 CorsMode cors_mode) {
guidou9bfe4e2f2016-04-09 08:31:19479 // Only URL or MSE blob URL is supported.
Blink Reformat1c4d759e2017-04-09 16:34:54480 DCHECK(source.IsURL());
481 blink::WebURL url = source.GetAsURL();
Xiaohan Wang81fd0022017-07-11 17:45:24482 DVLOG(1) << __func__ << "(" << load_type << ", " << GURL(url) << ", "
483 << cors_mode << ")";
chcunningham707b821e2018-05-29 08:42:19484
485 bool is_deferred = false;
486
Dale Curtise25163812018-09-21 22:13:39487 if (defer_load_cb_) {
chcunningham707b821e2018-05-29 08:42:19488 is_deferred = defer_load_cb_.Run(base::BindOnce(
Antonio Gomes142f32a2019-05-15 23:32:56489 &WebMediaPlayerImpl::DoLoad, weak_this_, load_type, url, cors_mode));
chcunningham707b821e2018-05-29 08:42:19490 } else {
491 DoLoad(load_type, url, cors_mode);
[email protected]d726eddc2013-07-02 22:25:55492 }
chcunningham707b821e2018-05-29 08:42:19493
494 return is_deferred ? LoadTiming::kDeferred : LoadTiming::kImmediate;
[email protected]62e5e682013-03-07 23:53:24495}
496
CJ DiMeglio013d4c472017-11-21 03:27:30497void WebMediaPlayerImpl::OnWebLayerUpdated() {}
498
danakj6a062b112018-05-17 16:25:45499void WebMediaPlayerImpl::RegisterContentsLayer(cc::Layer* layer) {
CJ DiMeglio2302d202017-08-31 08:38:04500 DCHECK(bridge_);
CJ DiMeglioa2b13fbc2018-06-27 00:50:59501 bridge_->SetContentsOpaque(opaque_);
danakj8d204a42018-05-18 18:05:35502 client_->SetCcLayer(layer);
CJ DiMeglio013d4c472017-11-21 03:27:30503}
504
danakj6a062b112018-05-17 16:25:45505void WebMediaPlayerImpl::UnregisterContentsLayer(cc::Layer* layer) {
506 // |client_| will unregister its cc::Layer if given a nullptr.
danakj8d204a42018-05-18 18:05:35507 client_->SetCcLayer(nullptr);
CJ DiMeglio2302d202017-08-31 08:38:04508}
509
Jennifer Apaciblec45fd052018-02-25 12:04:55510void WebMediaPlayerImpl::OnSurfaceIdUpdated(viz::SurfaceId surface_id) {
Jennifer Apaciblec45fd052018-02-25 12:04:55511 // TODO(726619): Handle the behavior when Picture-in-Picture mode is
512 // disabled.
Jennifer Apacible2b1dc5eb2018-04-27 16:23:28513 // The viz::SurfaceId may be updated when the video begins playback or when
514 // the size of the video changes.
Mounir Lamouri99ba5a62019-02-12 01:27:47515 if (client_)
516 client_->OnPictureInPictureStateChange();
Jennifer Apaciblec45fd052018-02-25 12:04:55517}
518
tsunghungee562e92016-07-20 18:03:31519void WebMediaPlayerImpl::EnableOverlay() {
520 overlay_enabled_ = true;
[email protected]f7df5b342018-07-13 20:22:13521 if (request_routing_token_cb_ &&
522 overlay_mode_ == OverlayMode::kUseAndroidOverlay) {
liberatofe8f9692017-06-08 19:17:36523 overlay_routing_token_is_pending_ = true;
liberato2ff93ad2017-05-17 07:28:24524 token_available_cb_.Reset(
Antonio Gomes142f32a2019-05-15 23:32:56525 base::Bind(&WebMediaPlayerImpl::OnOverlayRoutingToken, weak_this_));
liberato2ff93ad2017-05-17 07:28:24526 request_routing_token_cb_.Run(token_available_cb_.callback());
watkf835a792016-06-24 23:24:40527 }
tsunghungee562e92016-07-20 18:03:31528
liberato2ff93ad2017-05-17 07:28:24529 // We have requested (and maybe already have) overlay information. If the
530 // restarted decoder requests overlay information, then we'll defer providing
531 // it if it hasn't arrived yet. Otherwise, this would be a race, since we
532 // don't know if the request for overlay info or restart will complete first.
tsunghungee562e92016-07-20 18:03:31533 if (decoder_requires_restart_for_overlay_)
watkdee516f2016-02-18 02:22:19534 ScheduleRestart();
535}
536
tsunghungee562e92016-07-20 18:03:31537void WebMediaPlayerImpl::DisableOverlay() {
538 overlay_enabled_ = false;
Jinsong Fan982342112019-11-07 23:45:45539 if (overlay_mode_ == OverlayMode::kUseAndroidOverlay) {
liberato2ff93ad2017-05-17 07:28:24540 token_available_cb_.Cancel();
liberatofe8f9692017-06-08 19:17:36541 overlay_routing_token_is_pending_ = false;
542 overlay_routing_token_ = OverlayInfo::RoutingToken();
liberato2ff93ad2017-05-17 07:28:24543 }
tsunghungee562e92016-07-20 18:03:31544
545 if (decoder_requires_restart_for_overlay_)
watkdee516f2016-02-18 02:22:19546 ScheduleRestart();
liberato2ff93ad2017-05-17 07:28:24547 else
548 MaybeSendOverlayInfoToDecoder();
watkdee516f2016-02-18 02:22:19549}
550
Blink Reformat1c4d759e2017-04-09 16:34:54551void WebMediaPlayerImpl::EnteredFullscreen() {
liberatofe8f9692017-06-08 19:17:36552 overlay_info_.is_fullscreen = true;
553
[email protected]c8d574722017-08-30 20:53:43554 // |always_enable_overlays_| implies that we're already in overlay mode, so
555 // take no action here. Otherwise, switch to an overlay if it's allowed and
556 // if it will display properly.
557 if (!always_enable_overlays_ && overlay_mode_ != OverlayMode::kNoOverlays &&
liberato2fd111be2017-01-04 00:25:06558 DoesOverlaySupportMetadata()) {
tsunghungee562e92016-07-20 18:03:31559 EnableOverlay();
liberato2fd111be2017-01-04 00:25:06560 }
liberato2ff93ad2017-05-17 07:28:24561
liberatofe8f9692017-06-08 19:17:36562 // We send this only if we can send multiple calls. Otherwise, either (a)
563 // we already sent it and we don't have a callback anyway (we reset it when
564 // it's called in restart mode), or (b) we'll send this later when the surface
565 // actually arrives. GVD assumes that the first overlay info will have the
566 // routing information. Note that we set |is_fullscreen_| earlier, so that
567 // if EnableOverlay() can include fullscreen info in case it sends the overlay
568 // info before returning.
569 if (!decoder_requires_restart_for_overlay_)
570 MaybeSendOverlayInfoToDecoder();
[email protected]dc996312019-12-19 19:26:38571
572 if (power_status_helper_)
573 power_status_helper_->SetIsFullscreen(true);
tsunghungee562e92016-07-20 18:03:31574}
575
Blink Reformat1c4d759e2017-04-09 16:34:54576void WebMediaPlayerImpl::ExitedFullscreen() {
[email protected]dc996312019-12-19 19:26:38577 if (power_status_helper_)
578 power_status_helper_->SetIsFullscreen(false);
579
liberatofe8f9692017-06-08 19:17:36580 overlay_info_.is_fullscreen = false;
581
[email protected]c8d574722017-08-30 20:53:43582 // If we're in overlay mode, then exit it unless we're supposed to allow
583 // overlays all the time.
584 if (!always_enable_overlays_ && overlay_enabled_)
tsunghungee562e92016-07-20 18:03:31585 DisableOverlay();
liberato2ff93ad2017-05-17 07:28:24586
liberatofe8f9692017-06-08 19:17:36587 // See EnteredFullscreen for why we do this.
588 if (!decoder_requires_restart_for_overlay_)
589 MaybeSendOverlayInfoToDecoder();
tsunghungee562e92016-07-20 18:03:31590}
591
Chisoon Jeong81080c922019-09-03 23:40:18592void WebMediaPlayerImpl::BecameDominantVisibleContent(bool is_dominant) {
xjzcdbbe732016-12-03 20:47:42593 if (observer_)
Chisoon Jeong81080c922019-09-03 23:40:18594 observer_->OnBecameDominantVisibleContent(is_dominant);
xjzcdbbe732016-12-03 20:47:42595}
596
Blink Reformat1c4d759e2017-04-09 16:34:54597void WebMediaPlayerImpl::SetIsEffectivelyFullscreen(
François Beaufortad6c5232018-02-26 11:00:44598 blink::WebFullscreenVideoStatus fullscreen_video_status) {
599 delegate_->SetIsEffectivelyFullscreen(delegate_id_, fullscreen_video_status);
zqzhangabc08242017-03-02 16:07:14600}
601
Mounir Lamouri41a79c62017-06-06 12:53:16602void WebMediaPlayerImpl::OnHasNativeControlsChanged(bool has_native_controls) {
603 if (!watch_time_reporter_)
604 return;
605
606 if (has_native_controls)
607 watch_time_reporter_->OnNativeControlsEnabled();
608 else
609 watch_time_reporter_->OnNativeControlsDisabled();
610}
611
Mounir Lamourif9af74e72017-06-19 19:31:45612void WebMediaPlayerImpl::OnDisplayTypeChanged(
613 WebMediaPlayer::DisplayType display_type) {
Mounir Lamouri0484f40a2018-07-25 03:03:26614 if (surface_layer_for_video_enabled_) {
615 vfc_task_runner_->PostTask(
616 FROM_HERE,
617 base::BindOnce(
618 &VideoFrameCompositor::SetForceSubmit,
619 base::Unretained(compositor_.get()),
620 display_type == WebMediaPlayer::DisplayType::kPictureInPicture));
621 }
622
Mounir Lamourif9af74e72017-06-19 19:31:45623 if (!watch_time_reporter_)
624 return;
625
626 switch (display_type) {
627 case WebMediaPlayer::DisplayType::kInline:
628 watch_time_reporter_->OnDisplayTypeInline();
629 break;
630 case WebMediaPlayer::DisplayType::kFullscreen:
631 watch_time_reporter_->OnDisplayTypeFullscreen();
632 break;
633 case WebMediaPlayer::DisplayType::kPictureInPicture:
634 watch_time_reporter_->OnDisplayTypePictureInPicture();
François Beaufort3f62f382019-01-18 15:05:17635
636 // Resumes playback if it was paused when hidden.
637 if (paused_when_hidden_) {
638 paused_when_hidden_ = false;
639 OnPlay();
640 }
Mounir Lamourif9af74e72017-06-19 19:31:45641 break;
642 }
643}
644
[email protected]ef8394c2013-08-21 20:26:30645void WebMediaPlayerImpl::DoLoad(LoadType load_type,
[email protected]180ef242013-11-07 06:50:46646 const blink::WebURL& url,
Takashi Toyoshima2e01e692018-11-16 03:23:27647 CorsMode cors_mode) {
John Chen5b164a02017-11-01 00:36:09648 TRACE_EVENT1("media", "WebMediaPlayerImpl::DoLoad", "id", media_log_->id());
pkastingf5279482016-07-27 02:18:20649 DVLOG(1) << __func__;
acolwellb4034942014-08-28 15:42:43650 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d726eddc2013-07-02 22:25:55651
[email protected]91fd3162019-09-05 23:51:32652 // Start a new observation. If there was one before, then we didn't play it.
653 will_play_helper_.CompleteObservationIfNeeded(learning::TargetValue(false));
654 // For now, send in an empty set of features. We should fill some in here,
655 // and / or ask blink (via |client_|) for features from the DOM.
656 learning::FeatureDictionary dict;
657 will_play_helper_.BeginObservation(dict);
658
Thomas Guilbert55b48d92019-03-25 23:16:54659#if defined(OS_ANDROID)
660 // Only allow credentials if the crossorigin attribute is unspecified
661 // (kCorsModeUnspecified) or "use-credentials" (kCorsModeUseCredentials).
662 // This value is only used by the MediaPlayerRenderer.
663 // See https://crbug.com/936566.
664 //
665 // The credentials mode also has repercussions in WouldTaintOrigin(), but we
666 // access what we need from |mb_data_source_|->cors_mode() directly, instead
667 // of storing it here.
668 allow_media_player_renderer_credentials_ = cors_mode != kCorsModeAnonymous;
669#endif // defined(OS_ANDROID)
670
Dale Curtis4841c712018-12-13 18:14:05671 // Note: |url| may be very large, take care when making copies.
672 loaded_url_ = GURL(url);
673 load_type_ = load_type;
674
675 ReportMetrics(load_type, loaded_url_, *frame_, media_log_.get());
[email protected]62e5e682013-03-07 23:53:24676
Dale Curtis4841c712018-12-13 18:14:05677 // Set subresource URL for crash reporting; will be truncated to 256 bytes.
Robert Sesekc5e91df2017-12-12 21:11:03678 static base::debug::CrashKeyString* subresource_url =
679 base::debug::AllocateCrashKeyString("subresource_url",
680 base::debug::CrashKeySize::Size256);
Dale Curtis4841c712018-12-13 18:14:05681 base::debug::SetCrashKeyString(subresource_url, loaded_url_.spec());
[email protected]ef8394c2013-08-21 20:26:30682
Blink Reformat1c4d759e2017-04-09 16:34:54683 SetNetworkState(WebMediaPlayer::kNetworkStateLoading);
684 SetReadyState(WebMediaPlayer::kReadyStateHaveNothing);
Ted Meyer380ca982020-01-12 01:00:29685 media_log_->AddLogRecord(media_log_->CreateLoadEvent(url.GetString().Utf8()));
Dale Curtis3899090ea2018-01-12 00:10:35686 load_start_time_ = base::TimeTicks::Now();
[email protected]d726eddc2013-07-02 22:25:55687
Antonio Gomesf01cfbd2019-07-12 08:53:11688 media_metrics_provider_->Initialize(
689 load_type == kLoadTypeMediaSource,
690 load_type == kLoadTypeURL ? blink::GetMediaURLScheme(loaded_url_)
691 : mojom::MediaURLScheme::kUnknown);
Dale Curtis74612b72017-12-14 20:56:19692
[email protected]d726eddc2013-07-02 22:25:55693 // Media source pipelines can start immediately.
Blink Reformat1c4d759e2017-04-09 16:34:54694 if (load_type == kLoadTypeMediaSource) {
[email protected]ef8394c2013-08-21 20:26:30695 StartPipeline();
hubbe5f0ad43b2015-12-14 20:57:26696 } else {
Dale Curtis4841c712018-12-13 18:14:05697 // Short circuit the more complex loading path for data:// URLs. Sending
698 // them through the network based loading path just wastes memory and causes
699 // worse performance since reads become asynchronous.
700 if (loaded_url_.SchemeIs(url::kDataScheme)) {
701 std::string mime_type, charset, data;
Dale Curtis44eacb72019-06-10 20:41:28702 if (!net::DataURL::Parse(loaded_url_, &mime_type, &charset, &data) ||
703 data.empty()) {
Dale Curtis4841c712018-12-13 18:14:05704 DataSourceInitialized(false);
705 return;
706 }
707
708 // Replace |loaded_url_| with an empty data:// URL since it may be large.
709 loaded_url_ = GURL("data:,");
710
711 // Mark all the data as buffered.
Antonio Gomes81b56552019-05-15 22:15:28712 buffered_data_source_host_->SetTotalBytes(data.size());
713 buffered_data_source_host_->AddBufferedByteRange(0, data.size());
Dale Curtis4841c712018-12-13 18:14:05714
715 DCHECK(!mb_data_source_);
716 data_source_.reset(new MemoryDataSource(std::move(data)));
717 DataSourceInitialized(true);
718 return;
719 }
720
John Delaneyb933391602018-10-17 21:50:47721 auto url_data =
Takashi Toyoshima2e01e692018-11-16 03:23:27722 url_index_->GetByUrl(url, static_cast<UrlData::CorsMode>(cors_mode));
Dale Curtis4841c712018-12-13 18:14:05723 mb_data_source_ = new MultibufferDataSource(
John Delaneyb933391602018-10-17 21:50:47724 main_task_runner_, std::move(url_data), media_log_.get(),
Antonio Gomes81b56552019-05-15 22:15:28725 buffered_data_source_host_.get(),
Dale Curtis4841c712018-12-13 18:14:05726 base::BindRepeating(&WebMediaPlayerImpl::NotifyDownloading,
Antonio Gomes142f32a2019-05-15 23:32:56727 weak_this_));
Dale Curtis4841c712018-12-13 18:14:05728 data_source_.reset(mb_data_source_);
729 mb_data_source_->SetPreload(preload_);
730 mb_data_source_->SetIsClientAudioElement(client_->IsAudioElement());
731 mb_data_source_->Initialize(
Antonio Gomes142f32a2019-05-15 23:32:56732 base::Bind(&WebMediaPlayerImpl::DataSourceInitialized, weak_this_));
hubbe5f0ad43b2015-12-14 20:57:26733 }
[email protected]62e5e682013-03-07 23:53:24734}
735
Blink Reformat1c4d759e2017-04-09 16:34:54736void WebMediaPlayerImpl::Play() {
pkastingf5279482016-07-27 02:18:20737 DVLOG(1) << __func__;
acolwellb4034942014-08-28 15:42:43738 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:53739
avayvod65fad272017-02-24 01:00:48740 // User initiated play unlocks background video playback.
Mustaq Ahmed4baa9a6e82019-12-20 23:43:46741 if (frame_->HasTransientUserActivation())
avayvod65fad272017-02-24 01:00:48742 video_locked_when_paused_when_hidden_ = false;
743
sandersd35d2c3f2017-01-14 02:04:42744 // TODO(sandersd): Do we want to reset the idle timer here?
tguilbert1bb1c782017-01-23 21:15:11745 delegate_->SetIdle(delegate_id_, false);
[email protected]49480902009-07-14 20:23:43746 paused_ = false;
Antonio Gomes81b56552019-05-15 22:15:28747 pipeline_controller_->SetPlaybackRate(playback_rate_);
dalecurtis4619cd02016-09-22 21:39:10748 background_pause_timer_.Stop();
sandersd1c0bba02016-03-04 23:14:08749
xjz48a9cb72016-12-20 04:02:49750 if (observer_)
751 observer_->OnPlaying();
752
Mounir Lamouri89c0a1b2018-03-01 15:00:44753 watch_time_reporter_->SetAutoplayInitiated(client_->WasAutoplayInitiated());
754
Dale Curtis051fdf62017-08-05 00:21:13755 // If we're seeking we'll trigger the watch time reporter upon seek completed;
756 // we don't want to start it here since the seek time is unstable. E.g., when
757 // playing content with a positive start time we would have a zero seek time.
758 if (!Seeking()) {
759 DCHECK(watch_time_reporter_);
760 watch_time_reporter_->OnPlaying();
761 }
762
Chris Cunninghamd9df58e2017-08-29 00:04:23763 if (video_decode_stats_reporter_)
764 video_decode_stats_reporter_->OnPlaying();
765
Dan Sanders6edfd782019-08-13 00:13:18766 simple_watch_timer_.Start();
Ted Meyerd5885f82019-07-16 19:19:17767 media_metrics_provider_->SetHasPlayed();
Ted Meyer380ca982020-01-12 01:00:29768 media_log_->AddLogRecord(media_log_->CreateRecord(MediaLogRecord::PLAY));
Dale Curtis18ad391b2019-05-30 23:25:09769
770 MaybeUpdateBufferSizesForPlayback();
sandersd50a635e2016-04-04 22:50:09771 UpdatePlayState();
[email protected]91fd3162019-09-05 23:51:32772
773 // Notify the learning task, if needed.
774 will_play_helper_.CompleteObservationIfNeeded(learning::TargetValue(true));
[email protected]ec9212f2008-12-18 21:40:36775}
776
Blink Reformat1c4d759e2017-04-09 16:34:54777void WebMediaPlayerImpl::Pause() {
pkastingf5279482016-07-27 02:18:20778 DVLOG(1) << __func__;
acolwellb4034942014-08-28 15:42:43779 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:53780
sandersd50a635e2016-04-04 22:50:09781 // We update the paused state even when casting, since we expect pause() to be
782 // called when casting begins, and when we exit casting we should end up in a
783 // paused state.
[email protected]49480902009-07-14 20:23:43784 paused_ = true;
hubbed5f36882016-01-15 22:40:37785
avayvodeb9098d2017-01-07 00:33:03786 // No longer paused because it was hidden.
787 paused_when_hidden_ = false;
788
avayvod65fad272017-02-24 01:00:48789 // User initiated pause locks background videos.
Mustaq Ahmed4baa9a6e82019-12-20 23:43:46790 if (frame_->HasTransientUserActivation())
avayvod65fad272017-02-24 01:00:48791 video_locked_when_paused_when_hidden_ = true;
792
Antonio Gomes81b56552019-05-15 22:15:28793 pipeline_controller_->SetPlaybackRate(0.0);
Dale Curtis2fc01cc2019-05-20 22:53:52794
795 // For states <= kReadyStateHaveMetadata, we may not have a renderer yet.
796 if (highest_ready_state_ > WebMediaPlayer::kReadyStateHaveMetadata)
797 paused_time_ = pipeline_controller_->GetMediaTime();
[email protected]090f7312011-08-05 23:26:40798
xjz48a9cb72016-12-20 04:02:49799 if (observer_)
800 observer_->OnPaused();
801
dalecurtis04bdb582016-08-17 22:15:23802 DCHECK(watch_time_reporter_);
803 watch_time_reporter_->OnPaused();
Chris Cunninghamd9df58e2017-08-29 00:04:23804
805 if (video_decode_stats_reporter_)
806 video_decode_stats_reporter_->OnPaused();
807
Dan Sanders6edfd782019-08-13 00:13:18808 simple_watch_timer_.Stop();
Ted Meyer380ca982020-01-12 01:00:29809 media_log_->AddLogRecord(media_log_->CreateRecord(MediaLogRecord::PAUSE));
avayvod2135a642017-01-13 00:17:14810
Becca Hughes7a85bf22019-07-24 16:16:58811 // Paused changed so we should update media position state.
812 UpdateMediaPositionState();
813
sandersd50a635e2016-04-04 22:50:09814 UpdatePlayState();
[email protected]ec9212f2008-12-18 21:40:36815}
816
Blink Reformat1c4d759e2017-04-09 16:34:54817void WebMediaPlayerImpl::Seek(double seconds) {
pkastingf5279482016-07-27 02:18:20818 DVLOG(1) << __func__ << "(" << seconds << "s)";
acolwellb4034942014-08-28 15:42:43819 DCHECK(main_task_runner_->BelongsToCurrentThread());
Ted Meyer380ca982020-01-12 01:00:29820 media_log_->AddLogRecord(media_log_->CreateTimeEvent(MediaLogRecord::SEEK,
821 "seek_target", seconds));
sandersd1c0bba02016-03-04 23:14:08822 DoSeek(base::TimeDelta::FromSecondsD(seconds), true);
823}
824
825void WebMediaPlayerImpl::DoSeek(base::TimeDelta time, bool time_updated) {
826 DCHECK(main_task_runner_->BelongsToCurrentThread());
John Chen5b164a02017-11-01 00:36:09827 TRACE_EVENT2("media", "WebMediaPlayerImpl::DoSeek", "target",
828 time.InSecondsF(), "id", media_log_->id());
[email protected]d43ed912009-02-03 04:52:53829
srirama.mccf671812015-01-08 11:59:13830 ReadyState old_state = ready_state_;
Blink Reformat1c4d759e2017-04-09 16:34:54831 if (ready_state_ > WebMediaPlayer::kReadyStateHaveMetadata)
832 SetReadyState(WebMediaPlayer::kReadyStateHaveMetadata);
[email protected]1bb666802013-11-28 06:12:08833
Dale Curtis051fdf62017-08-05 00:21:13834 // When paused or ended, we know exactly what the current time is and can
835 // elide seeks to it. However, there are two cases that are not elided:
sandersd1c0bba02016-03-04 23:14:08836 // 1) When the pipeline state is not stable.
837 // In this case we just let |pipeline_controller_| decide what to do, as
838 // it has complete information.
839 // 2) For MSE.
840 // Because the buffers may have changed between seeks, MSE seeks are
841 // never elided.
Antonio Gomes81b56552019-05-15 22:15:28842 if (paused_ && pipeline_controller_->IsStable() &&
Dale Curtis051fdf62017-08-05 00:21:13843 (paused_time_ == time ||
844 (ended_ && time == base::TimeDelta::FromSecondsD(Duration()))) &&
sandersd1c0bba02016-03-04 23:14:08845 !chunk_demuxer_) {
846 // If the ready state was high enough before, we can indicate that the seek
847 // completed just by restoring it. Otherwise we will just wait for the real
848 // ready state change to eventually happen.
Blink Reformat1c4d759e2017-04-09 16:34:54849 if (old_state == kReadyStateHaveEnoughData) {
srirama.m8f4a37562014-12-13 08:16:18850 main_task_runner_->PostTask(
Dale Curtisee9be30012018-09-21 22:21:51851 FROM_HERE, base::BindOnce(&WebMediaPlayerImpl::OnBufferingStateChange,
Chris Cunninghamfc0d67e2019-07-22 20:29:16852 weak_this_, BUFFERING_HAVE_ENOUGH,
853 BUFFERING_CHANGE_REASON_UNKNOWN));
srirama.m36ab2682014-12-11 04:20:01854 }
sandersd1c0bba02016-03-04 23:14:08855 return;
srirama.m36ab2682014-12-11 04:20:01856 }
[email protected]44ff37c02009-10-24 01:03:03857
dalecurtis04bdb582016-08-17 22:15:23858 // Call this before setting |seeking_| so that the current media time can be
859 // recorded by the reporter.
860 if (watch_time_reporter_)
861 watch_time_reporter_->OnSeeking();
862
sandersd35d2c3f2017-01-14 02:04:42863 // TODO(sandersd): Move |seeking_| to PipelineController.
864 // TODO(sandersd): Do we want to reset the idle timer here?
tguilbert1bb1c782017-01-23 21:15:11865 delegate_->SetIdle(delegate_id_, false);
sandersd50a635e2016-04-04 22:50:09866 ended_ = false;
[email protected]b3766a22010-12-22 17:34:13867 seeking_ = true;
sandersd1c0bba02016-03-04 23:14:08868 seek_time_ = time;
869 if (paused_)
870 paused_time_ = time;
Antonio Gomes81b56552019-05-15 22:15:28871 pipeline_controller_->Seek(time, time_updated);
[email protected]b3766a22010-12-22 17:34:13872
sandersd50a635e2016-04-04 22:50:09873 // This needs to be called after Seek() so that if a resume is triggered, it
874 // is to the correct time.
875 UpdatePlayState();
Becca Hughes7a85bf22019-07-24 16:16:58876
877 // The seek time has changed so we should update the media position state.
878 UpdateMediaPositionState();
[email protected]ec9212f2008-12-18 21:40:36879}
880
Blink Reformat1c4d759e2017-04-09 16:34:54881void WebMediaPlayerImpl::SetRate(double rate) {
pkastingf5279482016-07-27 02:18:20882 DVLOG(1) << __func__ << "(" << rate << ")";
acolwellb4034942014-08-28 15:42:43883 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:53884
Matt Wolenetz6010f6c2017-10-18 00:44:36885 if (rate != playback_rate_) {
886 LIMITED_MEDIA_LOG(INFO, media_log_.get(), num_playback_rate_logs_,
887 kMaxNumPlaybackRateLogs)
888 << "Effective playback rate changed from " << playback_rate_ << " to "
889 << rate;
890 }
891
[email protected]49480902009-07-14 20:23:43892 playback_rate_ = rate;
Dale Curtis18ad391b2019-05-30 23:25:09893 if (!paused_)
Antonio Gomes81b56552019-05-15 22:15:28894 pipeline_controller_->SetPlaybackRate(rate);
Dale Curtis18ad391b2019-05-30 23:25:09895
896 MaybeUpdateBufferSizesForPlayback();
[email protected]ec9212f2008-12-18 21:40:36897}
898
Blink Reformat1c4d759e2017-04-09 16:34:54899void WebMediaPlayerImpl::SetVolume(double volume) {
pkastingf5279482016-07-27 02:18:20900 DVLOG(1) << __func__ << "(" << volume << ")";
acolwellb4034942014-08-28 15:42:43901 DCHECK(main_task_runner_->BelongsToCurrentThread());
dalecurtisbb3eaac2016-01-27 21:10:25902 volume_ = volume;
Antonio Gomes81b56552019-05-15 22:15:28903 pipeline_controller_->SetVolume(volume_ * volume_multiplier_);
dalecurtis04bdb582016-08-17 22:15:23904 if (watch_time_reporter_)
905 watch_time_reporter_->OnVolumeChange(volume);
Becca Hughes9f6fd4b82017-06-15 10:01:40906 delegate_->DidPlayerMutedStatusChange(delegate_id_, volume == 0.0);
mlamouri910111362016-11-04 11:28:24907
908 // The play state is updated because the player might have left the autoplay
909 // muted state.
910 UpdatePlayState();
[email protected]ec9212f2008-12-18 21:40:36911}
[email protected]f0a51fb52009-03-05 12:46:38912
Chris Cunningham8c2ef912019-11-15 23:10:41913void WebMediaPlayerImpl::SetLatencyHint(double seconds) {
914 DVLOG(1) << __func__ << "(" << seconds << ")";
Chris Cunninghame8c626332019-11-20 23:34:27915 DCHECK(main_task_runner_->BelongsToCurrentThread());
916 base::Optional<base::TimeDelta> latency_hint;
917 if (std::isfinite(seconds)) {
918 DCHECK_GE(seconds, 0);
919 latency_hint = base::TimeDelta::FromSecondsD(seconds);
920 }
921 pipeline_controller_->SetLatencyHint(latency_hint);
Chris Cunningham8c2ef912019-11-15 23:10:41922}
923
Mounir Lamouri967f8fb72019-02-13 02:34:22924void WebMediaPlayerImpl::OnRequestPictureInPicture() {
[email protected]69db58f2018-09-26 20:27:56925 if (!surface_layer_for_video_enabled_)
926 ActivateSurfaceLayerForVideo();
927
Mounir Lamourib642a3c2018-07-09 15:45:31928 DCHECK(bridge_);
Mounir Lamouri99ba5a62019-02-12 01:27:47929 DCHECK(bridge_->GetSurfaceId().is_valid());
Jennifer Apacible3f0489102018-01-19 20:10:31930}
931
Daniel Chengc1710b52018-10-24 03:12:28932void WebMediaPlayerImpl::SetSinkId(
933 const blink::WebString& sink_id,
Antonio Gomese7813f32019-04-02 06:11:03934 blink::WebSetSinkIdCompleteCallback completion_callback) {
guidou69223ce2015-06-16 10:36:19935 DCHECK(main_task_runner_->BelongsToCurrentThread());
pkastingf5279482016-07-27 02:18:20936 DVLOG(1) << __func__;
guidouc7babef2015-10-22 00:42:35937
Dale Curtisca1b78f2019-01-15 03:11:26938 OutputDeviceStatusCB callback =
Antonio Gomese7813f32019-04-02 06:11:03939 ConvertToOutputDeviceStatusCB(std::move(completion_callback));
guidouc7babef2015-10-22 00:42:35940 media_task_runner_->PostTask(
Dale Curtisee9be30012018-09-21 22:21:51941 FROM_HERE, base::BindOnce(&SetSinkIdOnMediaThread, audio_source_provider_,
Daniel Chengc1710b52018-10-24 03:12:28942 sink_id.Utf8(), std::move(callback)));
guidou69223ce2015-06-16 10:36:19943}
944
Blink Reformat1c4d759e2017-04-09 16:34:54945STATIC_ASSERT_ENUM(WebMediaPlayer::kPreloadNone, MultibufferDataSource::NONE);
946STATIC_ASSERT_ENUM(WebMediaPlayer::kPreloadMetaData,
dalecurtisb6e052f52016-08-25 00:35:55947 MultibufferDataSource::METADATA);
Blink Reformat1c4d759e2017-04-09 16:34:54948STATIC_ASSERT_ENUM(WebMediaPlayer::kPreloadAuto, MultibufferDataSource::AUTO);
[email protected]23a8b1d82011-04-05 16:28:20949
Blink Reformat1c4d759e2017-04-09 16:34:54950void WebMediaPlayerImpl::SetPreload(WebMediaPlayer::Preload preload) {
pkastingf5279482016-07-27 02:18:20951 DVLOG(1) << __func__ << "(" << preload << ")";
acolwellb4034942014-08-28 15:42:43952 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]4e6be3f2009-05-07 02:24:44953
dalecurtisb6e052f52016-08-25 00:35:55954 preload_ = static_cast<MultibufferDataSource::Preload>(preload);
Dale Curtis4841c712018-12-13 18:14:05955 if (mb_data_source_)
956 mb_data_source_->SetPreload(preload_);
[email protected]4e6be3f2009-05-07 02:24:44957}
958
Blink Reformat1c4d759e2017-04-09 16:34:54959bool WebMediaPlayerImpl::HasVideo() const {
acolwellb4034942014-08-28 15:42:43960 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:53961
[email protected]b8877772014-03-26 20:17:15962 return pipeline_metadata_.has_video;
[email protected]d43ed912009-02-03 04:52:53963}
964
Blink Reformat1c4d759e2017-04-09 16:34:54965bool WebMediaPlayerImpl::HasAudio() const {
acolwellb4034942014-08-28 15:42:43966 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]fc367af2009-08-14 23:06:35967
[email protected]b8877772014-03-26 20:17:15968 return pipeline_metadata_.has_audio;
[email protected]fc367af2009-08-14 23:06:35969}
970
Blink Reformat1c4d759e2017-04-09 16:34:54971void WebMediaPlayerImpl::EnabledAudioTracksChanged(
servolkf25ceed2016-07-01 03:44:38972 const blink::WebVector<blink::WebMediaPlayer::TrackId>& enabledTrackIds) {
973 DCHECK(main_task_runner_->BelongsToCurrentThread());
974
975 std::ostringstream logstr;
976 std::vector<MediaTrack::Id> enabledMediaTrackIds;
977 for (const auto& blinkTrackId : enabledTrackIds) {
Maciej Pawlowskif2556d122019-06-03 08:14:18978 const auto track_id = MediaTrack::Id(blinkTrackId.Utf8().data());
servolkf25ceed2016-07-01 03:44:38979 logstr << track_id << " ";
980 enabledMediaTrackIds.push_back(track_id);
981 }
dalecurtis9cddc0b2017-04-19 21:23:38982 MEDIA_LOG(INFO, media_log_.get())
983 << "Enabled audio tracks: [" << logstr.str() << "]";
Antonio Gomes81b56552019-05-15 22:15:28984 pipeline_controller_->OnEnabledAudioTracksChanged(enabledMediaTrackIds);
servolkf25ceed2016-07-01 03:44:38985}
986
Blink Reformat1c4d759e2017-04-09 16:34:54987void WebMediaPlayerImpl::SelectedVideoTrackChanged(
servolkf25ceed2016-07-01 03:44:38988 blink::WebMediaPlayer::TrackId* selectedTrackId) {
989 DCHECK(main_task_runner_->BelongsToCurrentThread());
990
servolk9bed6602017-02-24 01:20:11991 base::Optional<MediaTrack::Id> selected_video_track_id;
992 if (selectedTrackId && !video_track_disabled_)
Blink Reformat1c4d759e2017-04-09 16:34:54993 selected_video_track_id = MediaTrack::Id(selectedTrackId->Utf8().data());
dalecurtis9cddc0b2017-04-19 21:23:38994 MEDIA_LOG(INFO, media_log_.get())
Maciej Pawlowskif2556d122019-06-03 08:14:18995 << "Selected video track: ["
996 << selected_video_track_id.value_or(MediaTrack::Id()) << "]";
Antonio Gomes81b56552019-05-15 22:15:28997 pipeline_controller_->OnSelectedVideoTrackChanged(selected_video_track_id);
servolkf25ceed2016-07-01 03:44:38998}
999
Blink Reformat1c4d759e2017-04-09 16:34:541000blink::WebSize WebMediaPlayerImpl::NaturalSize() const {
acolwellb4034942014-08-28 15:42:431001 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:531002
[email protected]b8877772014-03-26 20:17:151003 return blink::WebSize(pipeline_metadata_.natural_size);
[email protected]d43ed912009-02-03 04:52:531004}
1005
Jiajia Qin82acdc02017-07-31 09:55:141006blink::WebSize WebMediaPlayerImpl::VisibleRect() const {
1007 DCHECK(main_task_runner_->BelongsToCurrentThread());
1008 scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor();
1009 if (!video_frame)
1010 return blink::WebSize();
1011
1012 const gfx::Rect& visible_rect = video_frame->visible_rect();
1013 return blink::WebSize(visible_rect.width(), visible_rect.height());
1014}
1015
Blink Reformat1c4d759e2017-04-09 16:34:541016bool WebMediaPlayerImpl::Paused() const {
acolwellb4034942014-08-28 15:42:431017 DCHECK(main_task_runner_->BelongsToCurrentThread());
Antonio Gomes81b56552019-05-15 22:15:281018 return pipeline_controller_->GetPlaybackRate() == 0.0f;
[email protected]d43ed912009-02-03 04:52:531019}
1020
François Beaufort184f6042019-01-25 05:50:051021bool WebMediaPlayerImpl::PausedWhenHidden() const {
1022 return paused_when_hidden_;
1023}
1024
Blink Reformat1c4d759e2017-04-09 16:34:541025bool WebMediaPlayerImpl::Seeking() const {
acolwellb4034942014-08-28 15:42:431026 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:531027
Blink Reformat1c4d759e2017-04-09 16:34:541028 if (ready_state_ == WebMediaPlayer::kReadyStateHaveNothing)
[email protected]0acebfa2009-08-21 22:45:401029 return false;
[email protected]67cd5052009-09-10 21:53:221030
[email protected]b3766a22010-12-22 17:34:131031 return seeking_;
[email protected]ec9212f2008-12-18 21:40:361032}
1033
Blink Reformat1c4d759e2017-04-09 16:34:541034double WebMediaPlayerImpl::Duration() const {
acolwellb4034942014-08-28 15:42:431035 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]39bdde32013-04-17 17:44:201036
Blink Reformat1c4d759e2017-04-09 16:34:541037 if (ready_state_ == WebMediaPlayer::kReadyStateHaveNothing)
[email protected]39bdde32013-04-17 17:44:201038 return std::numeric_limits<double>::quiet_NaN();
1039
chcunninghamb92d5062017-01-10 21:50:221040 // Use duration from ChunkDemuxer when present. MSE allows users to specify
1041 // duration as a double. This propagates to the rest of the pipeline as a
1042 // TimeDelta with potentially reduced precision (limited to Microseconds).
1043 // ChunkDemuxer returns the full-precision user-specified double. This ensures
1044 // users can "get" the exact duration they "set".
1045 if (chunk_demuxer_)
1046 return chunk_demuxer_->GetDuration();
1047
avayvodcc273dd2017-01-19 19:35:121048 base::TimeDelta pipeline_duration = GetPipelineMediaDuration();
chcunninghamb92d5062017-01-10 21:50:221049 return pipeline_duration == kInfiniteDuration
1050 ? std::numeric_limits<double>::infinity()
1051 : pipeline_duration.InSecondsF();
[email protected]d43ed912009-02-03 04:52:531052}
1053
[email protected]db66d0092014-04-16 07:15:121054double WebMediaPlayerImpl::timelineOffset() const {
acolwellb4034942014-08-28 15:42:431055 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]db66d0092014-04-16 07:15:121056
1057 if (pipeline_metadata_.timeline_offset.is_null())
1058 return std::numeric_limits<double>::quiet_NaN();
1059
1060 return pipeline_metadata_.timeline_offset.ToJsTime();
1061}
1062
Dale Curtis051fdf62017-08-05 00:21:131063base::TimeDelta WebMediaPlayerImpl::GetCurrentTimeInternal() const {
1064 DCHECK(main_task_runner_->BelongsToCurrentThread());
Dale Curtis051fdf62017-08-05 00:21:131065
1066 base::TimeDelta current_time;
1067 if (Seeking())
1068 current_time = seek_time_;
Dale Curtis051fdf62017-08-05 00:21:131069 else if (paused_)
1070 current_time = paused_time_;
1071 else
Antonio Gomes81b56552019-05-15 22:15:281072 current_time = pipeline_controller_->GetMediaTime();
Dale Curtis051fdf62017-08-05 00:21:131073
Dale Curtis77d6057b2019-05-03 01:31:561074 // It's possible for |current_time| to be kInfiniteDuration here if the page
1075 // seeks to kInfiniteDuration (2**64 - 1) when Duration() is infinite.
Dale Curtis051fdf62017-08-05 00:21:131076 DCHECK_GE(current_time, base::TimeDelta());
1077 return current_time;
1078}
1079
Blink Reformat1c4d759e2017-04-09 16:34:541080double WebMediaPlayerImpl::CurrentTime() const {
acolwellb4034942014-08-28 15:42:431081 DCHECK(main_task_runner_->BelongsToCurrentThread());
Blink Reformat1c4d759e2017-04-09 16:34:541082 DCHECK_NE(ready_state_, WebMediaPlayer::kReadyStateHaveNothing);
scherkusd2c745b2014-09-04 05:03:401083
1084 // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement,
1085 // see http://crbug.com/409280
Dale Curtis051fdf62017-08-05 00:21:131086 // Note: Duration() may be infinity.
josephlolak918863bc2017-11-15 08:54:331087 return (ended_ && !std::isinf(Duration()))
1088 ? Duration()
1089 : GetCurrentTimeInternal().InSecondsF();
[email protected]d43ed912009-02-03 04:52:531090}
1091
Blink Reformat1c4d759e2017-04-09 16:34:541092WebMediaPlayer::NetworkState WebMediaPlayerImpl::GetNetworkState() const {
acolwellb4034942014-08-28 15:42:431093 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]ddb1e5a2010-12-13 20:10:451094 return network_state_;
1095}
1096
Blink Reformat1c4d759e2017-04-09 16:34:541097WebMediaPlayer::ReadyState WebMediaPlayerImpl::GetReadyState() const {
acolwellb4034942014-08-28 15:42:431098 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]ddb1e5a2010-12-13 20:10:451099 return ready_state_;
1100}
1101
CJ DiMeglio89240472018-10-18 18:21:101102blink::WebMediaPlayer::SurfaceLayerMode
1103WebMediaPlayerImpl::GetVideoSurfaceLayerMode() const {
1104 return surface_layer_mode_;
1105}
1106
wolenetzed8e7092017-04-21 16:28:591107blink::WebString WebMediaPlayerImpl::GetErrorMessage() const {
wolenetz4d39cc02016-04-05 19:54:411108 DCHECK(main_task_runner_->BelongsToCurrentThread());
wolenetzed8e7092017-04-21 16:28:591109 return blink::WebString::FromUTF8(media_log_->GetErrorMessage());
wolenetz4d39cc02016-04-05 19:54:411110}
1111
Blink Reformat1c4d759e2017-04-09 16:34:541112blink::WebTimeRanges WebMediaPlayerImpl::Buffered() const {
acolwellb4034942014-08-28 15:42:431113 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]779a8322014-08-22 21:28:371114
acolwell9e0840d2014-09-06 19:01:321115 Ranges<base::TimeDelta> buffered_time_ranges =
Antonio Gomes81b56552019-05-15 22:15:281116 pipeline_controller_->GetBufferedTimeRanges();
[email protected]779a8322014-08-22 21:28:371117
avayvodcc273dd2017-01-19 19:35:121118 const base::TimeDelta duration = GetPipelineMediaDuration();
dalecurtis39a7f932016-07-19 18:34:591119 if (duration != kInfiniteDuration) {
Antonio Gomes81b56552019-05-15 22:15:281120 buffered_data_source_host_->AddBufferedTimeRanges(&buffered_time_ranges,
1121 duration);
[email protected]779a8322014-08-22 21:28:371122 }
Antonio Gomesf01cfbd2019-07-12 08:53:111123 return blink::ConvertToWebTimeRanges(buffered_time_ranges);
[email protected]02022fc2014-05-16 00:05:311124}
1125
Blink Reformat1c4d759e2017-04-09 16:34:541126blink::WebTimeRanges WebMediaPlayerImpl::Seekable() const {
acolwellb4034942014-08-28 15:42:431127 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]39bdde32013-04-17 17:44:201128
Blink Reformat1c4d759e2017-04-09 16:34:541129 if (ready_state_ < WebMediaPlayer::kReadyStateHaveMetadata)
philipjb0e6f3f2014-09-30 09:51:531130 return blink::WebTimeRanges();
1131
Blink Reformat1c4d759e2017-04-09 16:34:541132 const double seekable_end = Duration();
dalecurtis56359cb2014-10-28 00:06:291133
1134 // Allow a special exception for seeks to zero for streaming sources with a
1135 // finite duration; this allows looping to work.
Dale Curtis4841c712018-12-13 18:14:051136 const bool is_finite_stream = mb_data_source_ &&
1137 mb_data_source_->IsStreaming() &&
tguilbertade2bcb2017-01-07 02:57:451138 std::isfinite(seekable_end);
1139
tguilbert75e2bf62017-04-26 20:13:121140 // Do not change the seekable range when using the MediaPlayerRenderer. It
1141 // will take care of dropping invalid seeks.
1142 const bool force_seeks_to_zero =
1143 !using_media_player_renderer_ && is_finite_stream;
dalecurtis56359cb2014-10-28 00:06:291144
1145 // TODO(dalecurtis): Technically this allows seeking on media which return an
tguilbertade2bcb2017-01-07 02:57:451146 // infinite duration so long as DataSource::IsStreaming() is false. While not
dalecurtis56359cb2014-10-28 00:06:291147 // expected, disabling this breaks semi-live players, http://crbug.com/427412.
1148 const blink::WebTimeRange seekable_range(
tguilbertade2bcb2017-01-07 02:57:451149 0.0, force_seeks_to_zero ? 0.0 : seekable_end);
philipjb0e6f3f2014-09-30 09:51:531150 return blink::WebTimeRanges(&seekable_range, 1);
[email protected]ec9212f2008-12-18 21:40:361151}
1152
sandersd35d2c3f2017-01-14 02:04:421153bool WebMediaPlayerImpl::IsPrerollAttemptNeeded() {
1154 // TODO(sandersd): Replace with |highest_ready_state_since_seek_| if we need
1155 // to ensure that preroll always gets a chance to complete.
1156 // See http://crbug.com/671525.
Dale Curtis69e8f302019-05-22 07:36:581157 //
1158 // Note: Even though we get play/pause signals at kReadyStateHaveMetadata, we
1159 // must attempt to preroll until kReadyStateHaveFutureData so that the
1160 // canplaythrough event will be fired to the page (which may be waiting).
1161 //
1162 // TODO(dalecurtis): We should try signaling kReadyStateHaveFutureData upon
1163 // automatic-suspend of a non-playing element to avoid wasting resources.
1164 if (highest_ready_state_ >= ReadyState::kReadyStateHaveFutureData)
sandersd35d2c3f2017-01-14 02:04:421165 return false;
1166
Fredrik Hubinette4cfb4412017-08-23 00:03:071167 // To suspend before we reach kReadyStateHaveCurrentData is only ok
1168 // if we know we're going to get woken up when we get more data, which
1169 // will only happen if the network is in the "Loading" state.
1170 // This happens when the network is fast, but multiple videos are loading
1171 // and multiplexing gets held up waiting for available threads.
1172 if (highest_ready_state_ <= ReadyState::kReadyStateHaveMetadata &&
1173 network_state_ != WebMediaPlayer::kNetworkStateLoading) {
1174 return true;
1175 }
1176
sandersd35d2c3f2017-01-14 02:04:421177 if (preroll_attempt_pending_)
1178 return true;
1179
1180 // Freshly initialized; there has never been any loading progress. (Otherwise
1181 // |preroll_attempt_pending_| would be true when the start time is null.)
1182 if (preroll_attempt_start_time_.is_null())
1183 return false;
1184
1185 base::TimeDelta preroll_attempt_duration =
1186 tick_clock_->NowTicks() - preroll_attempt_start_time_;
1187 return preroll_attempt_duration < kPrerollAttemptTimeout;
1188}
1189
Blink Reformat1c4d759e2017-04-09 16:34:541190bool WebMediaPlayerImpl::DidLoadingProgress() {
acolwellb4034942014-08-28 15:42:431191 DCHECK(main_task_runner_->BelongsToCurrentThread());
dalecurtise7120dc2016-09-03 02:54:351192
1193 // Note: Separate variables used to ensure both methods are called every time.
Antonio Gomes81b56552019-05-15 22:15:281194 const bool pipeline_progress = pipeline_controller_->DidLoadingProgress();
1195 const bool data_progress = buffered_data_source_host_->DidLoadingProgress();
hubbeb2d3efd2017-05-05 23:26:381196 return pipeline_progress || data_progress;
[email protected]d43ed912009-02-03 04:52:531197}
1198
danakjff6a0262018-06-26 19:50:311199void WebMediaPlayerImpl::Paint(cc::PaintCanvas* canvas,
[email protected]dd5c7972014-08-21 15:00:371200 const blink::WebRect& rect,
Kai Ninomiya9e8ae29b2017-09-06 23:45:161201 cc::PaintFlags& flags,
1202 int already_uploaded_id,
1203 VideoFrameUploadMetadata* out_metadata) {
acolwellb4034942014-08-28 15:42:431204 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]dd061e12014-05-06 19:21:221205 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint");
[email protected]4e6be3f2009-05-07 02:24:441206
watkd16bb3e2017-04-25 01:18:311207 // We can't copy from protected frames.
Xiaohan Wang24cfe2c2018-01-22 23:16:001208 if (cdm_context_ref_)
xhwang80739452016-01-13 00:48:001209 return;
1210
mcasasf1236fc22015-05-29 22:38:561211 scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor();
[email protected]0fe5d43b2014-01-24 23:32:451212
[email protected]b49beeb2013-03-01 20:04:001213 gfx::Rect gfx_rect(rect);
mcasas265bdbf82015-06-12 18:44:071214 if (video_frame.get() && video_frame->HasTextures()) {
Antoine Labourfee4ae52019-04-24 23:53:571215 if (!context_provider_)
danakj53f7ec902016-05-21 01:30:101216 return; // Unable to get/create a shared main thread context.
Antoine Labourfee4ae52019-04-24 23:53:571217 if (!context_provider_->GrContext())
danakj53f7ec902016-05-21 01:30:101218 return; // The context has been lost since and can't setup a GrContext.
dongseong.hwang0c4e9d82015-01-08 20:11:131219 }
Kai Ninomiyaef8c9e02017-09-27 04:07:401220 if (out_metadata && video_frame) {
Kai Ninomiya9e8ae29b2017-09-06 23:45:161221 // WebGL last-uploaded-frame-metadata API enabled. https://crbug.com/639174
1222 ComputeFrameUploadMetadata(video_frame.get(), already_uploaded_id,
1223 out_metadata);
1224 if (out_metadata->skipped) {
1225 // Skip uploading this frame.
1226 return;
1227 }
1228 }
zhuoyu.qian4689dde22017-10-16 04:11:481229 video_renderer_.Paint(
Julien Isorce6c83d8de2017-10-12 13:11:291230 video_frame, canvas, gfx::RectF(gfx_rect), flags,
Ted Meyer4a427632019-05-01 19:05:271231 pipeline_metadata_.video_decoder_config.video_transformation(),
Antoine Labourfee4ae52019-04-24 23:53:571232 context_provider_.get());
[email protected]ec9212f2008-12-18 21:40:361233}
[email protected]5df51652009-01-17 00:03:001234
Yutaka Hirano657a0552018-11-09 00:52:551235bool WebMediaPlayerImpl::WouldTaintOrigin() const {
Thomas Guilbert153f84572018-07-19 05:03:581236 if (demuxer_found_hls_) {
1237 // HLS manifests might pull segments from a different origin. We can't know
1238 // for sure, so we conservatively say no here.
Yutaka Hiranoa9cbaa72018-10-10 08:35:221239 return true;
1240 }
1241
Dale Curtis4841c712018-12-13 18:14:051242 if (!mb_data_source_)
Yutaka Hirano657a0552018-11-09 00:52:551243 return false;
1244
1245 // When the resource is redirected to another origin we think it as
1246 // tainted. This is actually not specified, and is under discussion.
1247 // See https://github.com/whatwg/fetch/issues/737.
Dale Curtis4841c712018-12-13 18:14:051248 if (!mb_data_source_->HasSingleOrigin() &&
1249 mb_data_source_->cors_mode() == UrlData::CORS_UNSPECIFIED) {
Yutaka Hirano657a0552018-11-09 00:52:551250 return true;
1251 }
1252
Dale Curtis4841c712018-12-13 18:14:051253 return mb_data_source_->IsCorsCrossOrigin();
[email protected]3fe27112012-06-07 04:00:011254}
1255
Blink Reformat1c4d759e2017-04-09 16:34:541256double WebMediaPlayerImpl::MediaTimeForTimeValue(double timeValue) const {
qinminb4c39782015-08-10 18:43:241257 return base::TimeDelta::FromSecondsD(timeValue).InSecondsF();
[email protected]e06e16d82011-05-26 22:13:331258}
1259
Blink Reformat1c4d759e2017-04-09 16:34:541260unsigned WebMediaPlayerImpl::DecodedFrameCount() const {
acolwellb4034942014-08-28 15:42:431261 DCHECK(main_task_runner_->BelongsToCurrentThread());
Dale Curtisc7d2a7d22018-01-11 20:01:051262 return GetPipelineStatistics().video_frames_decoded;
[email protected]4c51bc662011-02-16 02:03:161263}
1264
Blink Reformat1c4d759e2017-04-09 16:34:541265unsigned WebMediaPlayerImpl::DroppedFrameCount() const {
acolwellb4034942014-08-28 15:42:431266 DCHECK(main_task_runner_->BelongsToCurrentThread());
Dale Curtisc7d2a7d22018-01-11 20:01:051267 return GetPipelineStatistics().video_frames_dropped;
[email protected]4c51bc662011-02-16 02:03:161268}
1269
Dave Tapuska6c7154912018-07-30 20:39:001270uint64_t WebMediaPlayerImpl::AudioDecodedByteCount() const {
acolwellb4034942014-08-28 15:42:431271 DCHECK(main_task_runner_->BelongsToCurrentThread());
Dale Curtisc7d2a7d22018-01-11 20:01:051272 return GetPipelineStatistics().audio_bytes_decoded;
[email protected]4c51bc662011-02-16 02:03:161273}
1274
Dave Tapuska6c7154912018-07-30 20:39:001275uint64_t WebMediaPlayerImpl::VideoDecodedByteCount() const {
acolwellb4034942014-08-28 15:42:431276 DCHECK(main_task_runner_->BelongsToCurrentThread());
Dale Curtisc7d2a7d22018-01-11 20:01:051277 return GetPipelineStatistics().video_bytes_decoded;
[email protected]4c51bc662011-02-16 02:03:161278}
1279
Dale Curtiscdfc6f22019-06-26 22:05:231280bool WebMediaPlayerImpl::HasAvailableVideoFrame() const {
1281 return has_first_frame_;
1282}
1283
Blink Reformat1c4d759e2017-04-09 16:34:541284bool WebMediaPlayerImpl::CopyVideoTextureToPlatformTexture(
danakj46070b02016-03-28 21:54:111285 gpu::gles2::GLES2Interface* gl,
jiajia.qinc2943162017-05-12 01:34:391286 unsigned int target,
zmo57d577a2015-10-30 18:28:591287 unsigned int texture,
kbr0986e622017-04-13 02:34:581288 unsigned internal_format,
1289 unsigned format,
1290 unsigned type,
jiajia.qinc2943162017-05-12 01:34:391291 int level,
zmo57d577a2015-10-30 18:28:591292 bool premultiply_alpha,
Kai Ninomiya9e8ae29b2017-09-06 23:45:161293 bool flip_y,
1294 int already_uploaded_id,
1295 VideoFrameUploadMetadata* out_metadata) {
xhwang213e50c2016-10-10 23:56:311296 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]bfc05f22013-10-19 17:55:161297 TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture");
1298
watkd16bb3e2017-04-25 01:18:311299 // We can't copy from protected frames.
Xiaohan Wang24cfe2c2018-01-22 23:16:001300 if (cdm_context_ref_)
xhwang213e50c2016-10-10 23:56:311301 return false;
[email protected]dd061e12014-05-06 19:21:221302
xhwang213e50c2016-10-10 23:56:311303 scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor();
jbauman581d041c2016-07-21 01:01:031304 if (!video_frame.get() || !video_frame->HasTextures()) {
[email protected]e56f88c72013-06-25 22:31:291305 return false;
dongseong.hwang0c4e9d82015-01-08 20:11:131306 }
Kai Ninomiya9e8ae29b2017-09-06 23:45:161307 if (out_metadata) {
1308 // WebGL last-uploaded-frame-metadata API is enabled.
1309 // https://crbug.com/639174
1310 ComputeFrameUploadMetadata(video_frame.get(), already_uploaded_id,
1311 out_metadata);
1312 if (out_metadata->skipped) {
1313 // Skip uploading this frame.
1314 return true;
1315 }
1316 }
[email protected]df41e252014-02-03 23:39:501317
zhuoyu.qian4689dde22017-10-16 04:11:481318 return video_renderer_.CopyVideoFrameTexturesToGLTexture(
Antoine Labourfee4ae52019-04-24 23:53:571319 context_provider_.get(), gl, video_frame.get(), target, texture,
Dan Sanders930cc1d2018-10-03 00:45:081320 internal_format, format, type, level, premultiply_alpha, flip_y);
[email protected]6523b242013-03-13 11:10:071321}
1322
Yan, Shaobo44b79ba2019-04-02 06:09:271323bool WebMediaPlayerImpl::PrepareVideoFrameForWebGL(
1324 gpu::gles2::GLES2Interface* gl,
1325 unsigned target,
1326 unsigned texture,
1327 int already_uploaded_id,
1328 WebMediaPlayer::VideoFrameUploadMetadata* out_metadata) {
1329 DCHECK(main_task_runner_->BelongsToCurrentThread());
1330 TRACE_EVENT0("media", "WebMediaPlayerImpl::PrepareVideoFrameForWebGL");
1331
1332 // TODO(crbug.com/776222): How to deal with protected frames.
1333 scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor();
1334 if (!video_frame.get() || !video_frame->HasTextures()) {
1335 return false;
1336 }
1337 if (out_metadata) {
1338 // WebGL last-uploaded-frame-metadata API is enabled.
1339 ComputeFrameUploadMetadata(video_frame.get(), already_uploaded_id,
1340 out_metadata);
1341 if (out_metadata->skipped) {
1342 // Skip uploading this frame.
1343 return true;
1344 }
1345 }
1346
Yan, Shaobo44b79ba2019-04-02 06:09:271347 return video_renderer_.PrepareVideoFrameForWebGL(
Antoine Labourfee4ae52019-04-24 23:53:571348 context_provider_.get(), gl, video_frame.get(), target, texture);
Yan, Shaobo44b79ba2019-04-02 06:09:271349}
1350
Matt Wolenetz95af6362018-01-04 20:23:421351// static
Kai Ninomiya9e8ae29b2017-09-06 23:45:161352void WebMediaPlayerImpl::ComputeFrameUploadMetadata(
1353 VideoFrame* frame,
1354 int already_uploaded_id,
1355 VideoFrameUploadMetadata* out_metadata) {
1356 DCHECK(out_metadata);
Kai Ninomiyaef8c9e02017-09-27 04:07:401357 DCHECK(frame);
Kai Ninomiya9e8ae29b2017-09-06 23:45:161358 out_metadata->frame_id = frame->unique_id();
1359 out_metadata->visible_rect = frame->visible_rect();
1360 out_metadata->timestamp = frame->timestamp();
Li, Hao0fab96a2019-10-31 02:46:471361 base::TimeDelta frame_duration;
1362 if (frame->metadata()->GetTimeDelta(media::VideoFrameMetadata::FRAME_DURATION,
1363 &frame_duration)) {
1364 out_metadata->expected_timestamp = frame->timestamp() + frame_duration;
1365 };
Kai Ninomiya9e8ae29b2017-09-06 23:45:161366 bool skip_possible = already_uploaded_id != -1;
1367 bool same_frame_id = frame->unique_id() == already_uploaded_id;
1368 out_metadata->skipped = skip_possible && same_frame_id;
1369}
1370
Blink Reformat1c4d759e2017-04-09 16:34:541371void WebMediaPlayerImpl::SetContentDecryptionModule(
[email protected]9ebc3b03f2014-08-13 04:01:231372 blink::WebContentDecryptionModule* cdm,
1373 blink::WebContentDecryptionModuleResult result) {
xhwang51139732017-02-24 19:36:081374 DVLOG(1) << __func__ << ": cdm = " << cdm;
acolwellb4034942014-08-28 15:42:431375 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]9ebc3b03f2014-08-13 04:01:231376
jrummell06f27072015-06-08 18:12:381377 // Once the CDM is set it can't be cleared as there may be frames being
1378 // decrypted on other threads. So fail this request.
1379 // http://crbug.com/462365#c7.
xhwang97de4202014-11-25 08:44:011380 if (!cdm) {
Blink Reformat1c4d759e2017-04-09 16:34:541381 result.CompleteWithError(
1382 blink::kWebContentDecryptionModuleExceptionInvalidStateError, 0,
xhwang79b193042016-12-13 18:52:431383 "The existing ContentDecryptionModule object cannot be removed at this "
1384 "time.");
xhwang97de4202014-11-25 08:44:011385 return;
1386 }
1387
jrummell89e61d82015-07-23 20:03:341388 // Create a local copy of |result| to avoid problems with the callback
1389 // getting passed to the media thread and causing |result| to be destructed
jrummell4acbec912016-02-27 02:45:031390 // on the wrong thread in some failure conditions. Blink should prevent
1391 // multiple simultaneous calls.
1392 DCHECK(!set_cdm_result_);
jrummell89e61d82015-07-23 20:03:341393 set_cdm_result_.reset(new blink::WebContentDecryptionModuleResult(result));
1394
Chris Cunninghamd4a00192019-03-21 20:02:491395 SetCdmInternal(cdm);
xhwang97de4202014-11-25 08:44:011396}
1397
xhwange8c4181a2014-12-06 08:10:011398void WebMediaPlayerImpl::OnEncryptedMediaInitData(
jrummellcf78967c2015-04-03 05:03:581399 EmeInitDataType init_data_type,
Avi Drissman97785ea2015-12-19 01:11:311400 const std::vector<uint8_t>& init_data) {
jrummellcf78967c2015-04-03 05:03:581401 DCHECK(init_data_type != EmeInitDataType::UNKNOWN);
xhwangbab66f52014-12-02 23:49:501402
Xiaohan Wangf63505d2017-10-21 08:00:531403 RecordEncryptedEvent(true);
xhwangbab66f52014-12-02 23:49:501404
dalecurtis04bdb582016-08-17 22:15:231405 // Recreate the watch time reporter if necessary.
1406 const bool was_encrypted = is_encrypted_;
1407 is_encrypted_ = true;
Dale Curtis3899090ea2018-01-12 00:10:351408 if (!was_encrypted) {
1409 media_metrics_provider_->SetIsEME();
1410 if (watch_time_reporter_)
1411 CreateWatchTimeReporter();
dalecurtis04bdb582016-08-17 22:15:231412
Chris Cunningham6c0ec292019-04-04 18:31:111413 // |was_encrypted| = false means we didn't have a CDM prior to observing
1414 // encrypted media init data. Reset the reporter until the CDM arrives. See
1415 // SetCdmInternal().
1416 DCHECK(!cdm_config_);
1417 video_decode_stats_reporter_.reset();
1418 }
Chris Cunninghamd9df58e2017-08-29 00:04:231419
Blink Reformat1c4d759e2017-04-09 16:34:541420 encrypted_client_->Encrypted(
Antonio Gomes89b70572019-07-13 00:44:211421 init_data_type, init_data.data(),
srirama.m26f864d02015-07-14 05:21:461422 base::saturated_cast<unsigned int>(init_data.size()));
xhwangbab66f52014-12-02 23:49:501423}
1424
servolk81e01e02016-03-05 03:29:151425void WebMediaPlayerImpl::OnFFmpegMediaTracksUpdated(
dcheng3076abbf2016-04-22 20:42:391426 std::unique_ptr<MediaTracks> tracks) {
servolk81e01e02016-03-05 03:29:151427 // For MSE/chunk_demuxer case the media track updates are handled by
1428 // WebSourceBufferImpl.
1429 DCHECK(demuxer_.get());
1430 DCHECK(!chunk_demuxer_);
servolkef1e5ef2016-03-25 04:55:261431
servolk16e8bdf82017-04-11 17:00:391432 // Report the media track information to blink. Only the first audio track and
1433 // the first video track are enabled by default to match blink logic.
1434 bool is_first_audio_track = true;
1435 bool is_first_video_track = true;
servolkef1e5ef2016-03-25 04:55:261436 for (const auto& track : tracks->tracks()) {
1437 if (track->type() == MediaTrack::Audio) {
Maciej Pawlowskif2556d122019-06-03 08:14:181438 client_->AddAudioTrack(
1439 blink::WebString::FromUTF8(track->id().value()),
1440 blink::WebMediaPlayerClient::kAudioTrackKindMain,
1441 blink::WebString::FromUTF8(track->label().value()),
1442 blink::WebString::FromUTF8(track->language().value()),
1443 is_first_audio_track);
servolk16e8bdf82017-04-11 17:00:391444 is_first_audio_track = false;
servolkef1e5ef2016-03-25 04:55:261445 } else if (track->type() == MediaTrack::Video) {
Maciej Pawlowskif2556d122019-06-03 08:14:181446 client_->AddVideoTrack(
1447 blink::WebString::FromUTF8(track->id().value()),
1448 blink::WebMediaPlayerClient::kVideoTrackKindMain,
1449 blink::WebString::FromUTF8(track->label().value()),
1450 blink::WebString::FromUTF8(track->language().value()),
1451 is_first_video_track);
servolk16e8bdf82017-04-11 17:00:391452 is_first_video_track = false;
servolkef1e5ef2016-03-25 04:55:261453 } else {
1454 // Text tracks are not supported through this code path yet.
1455 NOTREACHED();
1456 }
1457 }
servolk81e01e02016-03-05 03:29:151458}
1459
Chris Cunninghamd4a00192019-03-21 20:02:491460void WebMediaPlayerImpl::SetCdmInternal(
1461 blink::WebContentDecryptionModule* cdm) {
jrummelle616ee92016-10-08 02:15:441462 DCHECK(main_task_runner_->BelongsToCurrentThread());
1463 DCHECK(cdm);
Xiaohan Wang24cfe2c2018-01-22 23:16:001464
Chris Cunninghamd4a00192019-03-21 20:02:491465 const bool was_encrypted = is_encrypted_;
1466 is_encrypted_ = true;
1467
1468 // Recreate the watch time reporter if necessary.
1469 if (!was_encrypted) {
1470 media_metrics_provider_->SetIsEME();
1471 if (watch_time_reporter_)
1472 CreateWatchTimeReporter();
1473 }
1474
Chris Cunningham6c0ec292019-04-04 18:31:111475 WebContentDecryptionModuleImpl* web_cdm =
1476 ToWebContentDecryptionModuleImpl(cdm);
1477 auto cdm_context_ref = web_cdm->GetCdmContextRef();
Xiaohan Wang24cfe2c2018-01-22 23:16:001478 if (!cdm_context_ref) {
jrummelle616ee92016-10-08 02:15:441479 NOTREACHED();
1480 OnCdmAttached(false);
xhwang80739452016-01-13 00:48:001481 return;
1482 }
1483
Chris Cunningham6c0ec292019-04-04 18:31:111484 // Arrival of |cdm_config_| and |key_system_| unblocks recording of encrypted
1485 // stats. Attempt to create the stats reporter. Note, we do NOT guard this
1486 // within !was_encypted above because often the CDM arrives after the call to
1487 // OnEncryptedMediaInitData().
1488 cdm_config_ = web_cdm->GetCdmConfig();
1489 key_system_ = web_cdm->GetKeySystem();
1490 DCHECK(!key_system_.empty());
1491 CreateVideoDecodeStatsReporter();
1492
Xiaohan Wang24cfe2c2018-01-22 23:16:001493 CdmContext* cdm_context = cdm_context_ref->GetCdmContext();
1494 DCHECK(cdm_context);
jrummelle616ee92016-10-08 02:15:441495
1496 // Keep the reference to the CDM, as it shouldn't be destroyed until
1497 // after the pipeline is done with the |cdm_context|.
Xiaohan Wang24cfe2c2018-01-22 23:16:001498 pending_cdm_context_ref_ = std::move(cdm_context_ref);
Antonio Gomes81b56552019-05-15 22:15:281499 pipeline_controller_->SetCdm(
Antonio Gomes142f32a2019-05-15 23:32:561500 cdm_context, base::Bind(&WebMediaPlayerImpl::OnCdmAttached, weak_this_));
xhwang97de4202014-11-25 08:44:011501}
1502
jrummell89e61d82015-07-23 20:03:341503void WebMediaPlayerImpl::OnCdmAttached(bool success) {
xhwang51139732017-02-24 19:36:081504 DVLOG(1) << __func__ << ": success = " << success;
jrummelle616ee92016-10-08 02:15:441505 DCHECK(main_task_runner_->BelongsToCurrentThread());
Xiaohan Wang24cfe2c2018-01-22 23:16:001506 DCHECK(pending_cdm_context_ref_);
jrummelle616ee92016-10-08 02:15:441507
1508 // If the CDM is set from the constructor there is no promise
1509 // (|set_cdm_result_|) to fulfill.
xhwang97de4202014-11-25 08:44:011510 if (success) {
Ted Meyer7f5b4e22019-11-21 03:21:191511 media_log_->SetProperty<MediaLogProperty::kIsVideoEncrypted>(true);
xhwang29c5ad202017-04-14 07:02:191512
jrummelle616ee92016-10-08 02:15:441513 // This will release the previously attached CDM (if any).
Xiaohan Wang24cfe2c2018-01-22 23:16:001514 cdm_context_ref_ = std::move(pending_cdm_context_ref_);
jrummelle616ee92016-10-08 02:15:441515 if (set_cdm_result_) {
Blink Reformat1c4d759e2017-04-09 16:34:541516 set_cdm_result_->Complete();
jrummelle616ee92016-10-08 02:15:441517 set_cdm_result_.reset();
1518 }
1519
xhwang97de4202014-11-25 08:44:011520 return;
1521 }
1522
Xiaohan Wang24cfe2c2018-01-22 23:16:001523 pending_cdm_context_ref_.reset();
jrummelle616ee92016-10-08 02:15:441524 if (set_cdm_result_) {
Blink Reformat1c4d759e2017-04-09 16:34:541525 set_cdm_result_->CompleteWithError(
1526 blink::kWebContentDecryptionModuleExceptionNotSupportedError, 0,
xhwang79b193042016-12-13 18:52:431527 "Unable to set ContentDecryptionModule object");
jrummelle616ee92016-10-08 02:15:441528 set_cdm_result_.reset();
1529 }
[email protected]9ebc3b03f2014-08-13 04:01:231530}
1531
sandersd1c0bba02016-03-04 23:14:081532void WebMediaPlayerImpl::OnPipelineSeeked(bool time_updated) {
John Chen5b164a02017-11-01 00:36:091533 TRACE_EVENT2("media", "WebMediaPlayerImpl::OnPipelineSeeked", "target",
1534 seek_time_.InSecondsF(), "id", media_log_->id());
[email protected]5d11eff2011-09-15 00:06:061535 seeking_ = false;
wolenetz1b2ae7d2015-06-10 00:44:211536 seek_time_ = base::TimeDelta();
avayvod2135a642017-01-13 00:17:141537
hubbe5a2dec022016-03-17 01:14:231538 if (paused_) {
Antonio Gomes81b56552019-05-15 22:15:281539 paused_time_ = pipeline_controller_->GetMediaTime();
dalecurtis04bdb582016-08-17 22:15:231540 } else {
1541 DCHECK(watch_time_reporter_);
1542 watch_time_reporter_->OnPlaying();
hubbe5a2dec022016-03-17 01:14:231543 }
sandersd1c0bba02016-03-04 23:14:081544 if (time_updated)
1545 should_notify_time_changed_ = true;
dalecurtisaf34d8712016-09-20 04:32:261546
dalecurtis4f6d14d2017-02-22 17:42:221547 // Reset underflow duration upon seek; this prevents looping videos and user
1548 // actions from artificially inflating the duration.
1549 underflow_timer_.reset();
avayvod56e1f3942017-01-21 02:06:311550
1551 // Background video optimizations are delayed when shown/hidden if pipeline
1552 // is seeking.
1553 UpdateBackgroundVideoOptimizationState();
Dale Curtis2dc6089a2018-03-26 16:47:581554
Dale Curtisd71061f02019-05-21 21:33:541555 // If we successfully completed a suspended startup, we need to make a call to
1556 // UpdatePlayState() in case any events which should trigger a resume have
1557 // occurred during startup.
Dale Curtis2dc6089a2018-03-26 16:47:581558 if (attempting_suspended_start_ &&
Antonio Gomes81b56552019-05-15 22:15:281559 pipeline_controller_->IsPipelineSuspended()) {
Dale Curtisff576552018-03-30 02:32:441560 skip_metrics_due_to_startup_suspend_ = true;
Dale Curtisd71061f02019-05-21 21:33:541561
1562 // If we successfully completed a suspended startup, signal that we have
1563 // reached BUFFERING_HAVE_ENOUGH so that canplay and canplaythrough fire
1564 // correctly. We must unfortunately always do this because it's valid for
1565 // elements to play while not visible nor even in the DOM.
1566 //
1567 // Note: This call is dual purpose, it is also responsible for triggering an
1568 // UpdatePlayState() call which may need to resume the pipeline once Blink
1569 // has been told about the ReadyState change.
Chris Cunninghamfc0d67e2019-07-22 20:29:161570 OnBufferingStateChangeInternal(BUFFERING_HAVE_ENOUGH,
1571 BUFFERING_CHANGE_REASON_UNKNOWN, true);
Dale Curtisff576552018-03-30 02:32:441572
1573 // If |skip_metrics_due_to_startup_suspend_| is unset by a resume started by
1574 // the OnBufferingStateChangeInternal() call, record a histogram of it here.
1575 //
1576 // If the value is unset, that means we should not have suspended and we've
1577 // likely incurred some cost to TimeToFirstFrame and TimeToPlayReady which
1578 // will be reflected in those statistics.
1579 base::UmaHistogramBoolean(
1580 std::string("Media.PreloadMetadataSuspendWasIdeal.") +
1581 ((HasVideo() && HasAudio()) ? "AudioVideo"
1582 : (HasVideo() ? "Video" : "Audio")),
1583 skip_metrics_due_to_startup_suspend_);
Dale Curtis2dc6089a2018-03-26 16:47:581584 }
1585
1586 attempting_suspended_start_ = false;
Becca Hughes7a85bf22019-07-24 16:16:581587
1588 // The current time has changed so we should update the media position state.
1589 UpdateMediaPositionState();
[email protected]8931c41a2009-07-07 17:31:491590}
1591
sandersd1c0bba02016-03-04 23:14:081592void WebMediaPlayerImpl::OnPipelineSuspended() {
Dale Curtis83321152018-12-01 01:22:061593 // Add a log event so the player shows up as "SUSPENDED" in media-internals.
Ted Meyer380ca982020-01-12 01:00:291594 media_log_->AddLogRecord(media_log_->CreateRecord(MediaLogRecord::SUSPENDED));
Dale Curtis83321152018-12-01 01:22:061595
Dale Curtis04769ca2019-05-25 00:38:101596 if (attempting_suspended_start_) {
1597 DCHECK(pipeline_controller_->IsSuspended());
1598 did_lazy_load_ = !has_poster_ && HasVideo();
1599 }
1600
sandersd2f5bb6152017-03-29 22:57:531601 // Tell the data source we have enough data so that it may release the
Dale Curtis04769ca2019-05-25 00:38:101602 // connection (unless blink is waiting on us to signal play()).
1603 if (mb_data_source_ && !client_->CouldPlayIfEnoughData()) {
1604 // |attempting_suspended_start_| will be cleared by OnPipelineSeeked() which
1605 // will occur after this method during a suspended startup.
1606 if (attempting_suspended_start_ && did_lazy_load_) {
1607 DCHECK(!has_first_frame_);
1608 DCHECK(have_enough_after_lazy_load_cb_.IsCancelled());
1609
1610 // For lazy load, we won't know if the element is non-visible until a
1611 // layout completes, so to avoid unnecessarily tearing down the network
1612 // connection, briefly (250ms chosen arbitrarily) delay signaling "have
1613 // enough" to the MultiBufferDataSource.
1614 //
1615 // base::Unretained() is safe here since the base::CancelableOnceClosure
1616 // will cancel upon destruction of this class and |mb_data_source_| is
1617 // gauranteeed to outlive us.
1618 have_enough_after_lazy_load_cb_.Reset(
1619 base::BindOnce(&MultibufferDataSource::OnBufferingHaveEnough,
1620 base::Unretained(mb_data_source_), true));
1621 main_task_runner_->PostDelayedTask(
1622 FROM_HERE, have_enough_after_lazy_load_cb_.callback(),
1623 base::TimeDelta::FromMilliseconds(250));
1624 } else {
1625 have_enough_after_lazy_load_cb_.Cancel();
1626 mb_data_source_->OnBufferingHaveEnough(true);
1627 }
1628 }
dalecurtis37fe5862016-03-15 19:29:091629
sandersd50a635e2016-04-04 22:50:091630 ReportMemoryUsage();
1631
sandersd1c0bba02016-03-04 23:14:081632 if (pending_suspend_resume_cycle_) {
watkdee516f2016-02-18 02:22:191633 pending_suspend_resume_cycle_ = false;
sandersd50a635e2016-04-04 22:50:091634 UpdatePlayState();
dalecurtis0431cbf2016-03-12 01:19:431635 }
sandersd1c0bba02016-03-04 23:14:081636}
1637
avayvod2135a642017-01-13 00:17:141638void WebMediaPlayerImpl::OnBeforePipelineResume() {
Dale Curtis04769ca2019-05-25 00:38:101639 // Since we're resuming, cancel closing of the network connection.
1640 have_enough_after_lazy_load_cb_.Cancel();
1641
Dale Curtisff576552018-03-30 02:32:441642 // We went through suspended startup, so the player is only just now spooling
1643 // up for playback. As such adjust |load_start_time_| so it reports the same
1644 // metric as what would be reported if we had not suspended at startup.
1645 if (skip_metrics_due_to_startup_suspend_) {
1646 // In the event that the call to SetReadyState() initiated after pipeline
1647 // startup immediately tries to start playback, we should not update
1648 // |load_start_time_| to avoid losing visibility into the impact of a
1649 // suspended startup on the time until first frame / play ready for cases
1650 // where suspended startup was applied incorrectly.
1651 if (!attempting_suspended_start_)
1652 load_start_time_ = base::TimeTicks::Now() - time_to_metadata_;
1653 skip_metrics_due_to_startup_suspend_ = false;
1654 }
1655
avayvod2135a642017-01-13 00:17:141656 // Enable video track if we disabled it in the background - this way the new
1657 // renderer will attach its callbacks to the video stream properly.
1658 // TODO(avayvod): Remove this when disabling and enabling video tracks in
1659 // non-playing state works correctly. See https://crbug.com/678374.
1660 EnableVideoTrackIfNeeded();
1661 is_pipeline_resuming_ = true;
1662}
1663
1664void WebMediaPlayerImpl::OnPipelineResumed() {
1665 is_pipeline_resuming_ = false;
1666
avayvod56e1f3942017-01-21 02:06:311667 UpdateBackgroundVideoOptimizationState();
avayvod2135a642017-01-13 00:17:141668}
1669
alokp967c902452016-05-06 05:21:371670void WebMediaPlayerImpl::OnDemuxerOpened() {
1671 DCHECK(main_task_runner_->BelongsToCurrentThread());
dalecurtis9cddc0b2017-04-19 21:23:381672 client_->MediaSourceOpened(new WebMediaSourceImpl(chunk_demuxer_));
alokp967c902452016-05-06 05:21:371673}
1674
servolkf94b4602017-01-31 16:44:271675void WebMediaPlayerImpl::OnMemoryPressure(
1676 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
1677 DVLOG(2) << __func__ << " memory_pressure_level=" << memory_pressure_level;
1678 DCHECK(main_task_runner_->BelongsToCurrentThread());
1679 DCHECK(base::FeatureList::IsEnabled(kMemoryPressureBasedSourceBufferGC));
1680 DCHECK(chunk_demuxer_);
1681
1682 // The new value of |memory_pressure_level| will take effect on the next
1683 // garbage collection. Typically this means the next SourceBuffer append()
1684 // operation, since per MSE spec, the garbage collection must only occur
1685 // during SourceBuffer append(). But if memory pressure is critical it might
1686 // be better to perform GC immediately rather than wait for the next append
1687 // and potentially get killed due to out-of-memory.
1688 // So if this experiment is enabled and pressure level is critical, we'll pass
1689 // down force_instant_gc==true, which will force immediate GC on
1690 // SourceBufferStreams.
1691 bool force_instant_gc =
1692 (enable_instant_source_buffer_gc_ &&
1693 memory_pressure_level ==
1694 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
1695
Matt Wolenetz95af6362018-01-04 20:23:421696 // base::Unretained is safe, since |chunk_demuxer_| is actually owned by
1697 // |this| via this->demuxer_. Note the destruction of |chunk_demuxer_| is done
1698 // from ~WMPI by first hopping to |media_task_runner_| to prevent race with
1699 // this task.
servolkf94b4602017-01-31 16:44:271700 media_task_runner_->PostTask(
Dale Curtisee9be30012018-09-21 22:21:511701 FROM_HERE, base::BindOnce(&ChunkDemuxer::OnMemoryPressure,
1702 base::Unretained(chunk_demuxer_),
1703 base::TimeDelta::FromSecondsD(CurrentTime()),
1704 memory_pressure_level, force_instant_gc));
servolkf94b4602017-01-31 16:44:271705}
1706
alokp967c902452016-05-06 05:21:371707void WebMediaPlayerImpl::OnError(PipelineStatus status) {
Xiaohan Wang73b433f2019-11-05 20:13:011708 DVLOG(1) << __func__ << ": status=" << status;
alokp967c902452016-05-06 05:21:371709 DCHECK(main_task_runner_->BelongsToCurrentThread());
1710 DCHECK_NE(status, PIPELINE_OK);
1711
1712 if (suppress_destruction_errors_)
1713 return;
1714
Thomas Guilbert6b6be3d2017-08-18 03:17:271715#if defined(OS_ANDROID)
Dale Curtisf273f8f2018-12-13 23:40:331716 // |mb_data_source_| may be nullptr if someone passes in a m3u8 as a data://
1717 // URL, since MediaPlayer doesn't support data:// URLs, fail playback now.
Dan Sanders675250992019-05-11 00:07:491718 const bool found_hls = base::FeatureList::IsEnabled(kHlsPlayer) &&
1719 status == PipelineStatus::DEMUXER_ERROR_DETECTED_HLS;
Dale Curtisf273f8f2018-12-13 23:40:331720 if (found_hls && mb_data_source_) {
Dan Sandersae245e42019-03-07 23:25:291721 demuxer_found_hls_ = true;
1722
Dan Sandersd80b1842019-02-05 00:36:161723 UMA_HISTOGRAM_BOOLEAN("Media.WebMediaPlayerImpl.HLS.IsCorsCrossOrigin",
1724 mb_data_source_->IsCorsCrossOrigin());
Dan Sandersae245e42019-03-07 23:25:291725 if (mb_data_source_->IsCorsCrossOrigin()) {
1726 UMA_HISTOGRAM_BOOLEAN("Media.WebMediaPlayerImpl.HLS.HasAccessControl",
1727 mb_data_source_->HasAccessControl());
1728 }
1729
1730 // Note: Does not consider the full redirect chain, which could contain
1731 // undetected mixed content.
Dan Sandersd80b1842019-02-05 00:36:161732 bool frame_url_is_cryptographic = url::Origin(frame_->GetSecurityOrigin())
1733 .GetURL()
1734 .SchemeIsCryptographic();
1735 bool manifest_url_is_cryptographic =
1736 loaded_url_.SchemeIsCryptographic() &&
1737 mb_data_source_->GetUrlAfterRedirects().SchemeIsCryptographic();
1738 UMA_HISTOGRAM_BOOLEAN(
1739 "Media.WebMediaPlayerImpl.HLS.IsMixedContent",
1740 frame_url_is_cryptographic && !manifest_url_is_cryptographic);
Thomas Guilbert153f84572018-07-19 05:03:581741
Xiaohan Wang3684e0c2019-10-30 01:04:301742 renderer_factory_selector_->SetBaseFactoryType(
Xiaohan Wang48aaa192019-12-05 01:29:001743 RendererFactoryType::kMediaPlayer);
Thomas Guilbert6b6be3d2017-08-18 03:17:271744
Dale Curtisf273f8f2018-12-13 23:40:331745 loaded_url_ = mb_data_source_->GetUrlAfterRedirects();
1746 DCHECK(data_source_);
1747 data_source_->Stop();
1748 mb_data_source_ = nullptr;
1749
Antonio Gomes81b56552019-05-15 22:15:281750 pipeline_controller_->Stop();
Dale Curtis8a6281322017-08-31 00:35:531751 SetMemoryReportingState(false);
Thomas Guilbert6b6be3d2017-08-18 03:17:271752
Dale Curtisf273f8f2018-12-13 23:40:331753 // Trampoline through the media task runner to destruct the demuxer and
1754 // data source now that we're switching to HLS playback.
1755 media_task_runner_->PostTask(
Dale Curtisee9be30012018-09-21 22:21:511756 FROM_HERE,
Dale Curtisf273f8f2018-12-13 23:40:331757 BindToCurrentLoop(base::BindOnce(
1758 [](std::unique_ptr<Demuxer> demuxer,
1759 std::unique_ptr<DataSource> data_source,
1760 base::OnceClosure start_pipeline_cb) {
1761 // Release resources before starting HLS.
1762 demuxer.reset();
1763 data_source.reset();
1764
1765 std::move(start_pipeline_cb).Run();
1766 },
1767 std::move(demuxer_), std::move(data_source_),
Antonio Gomes142f32a2019-05-15 23:32:561768 base::BindOnce(&WebMediaPlayerImpl::StartPipeline, weak_this_))));
Dale Curtisf273f8f2018-12-13 23:40:331769
Thomas Guilbert6b6be3d2017-08-18 03:17:271770 return;
1771 }
Dale Curtisf273f8f2018-12-13 23:40:331772
1773 // We found hls in a data:// URL, fail immediately.
1774 if (found_hls)
1775 status = PIPELINE_ERROR_EXTERNAL_RENDERER_FAILED;
Thomas Guilbert6b6be3d2017-08-18 03:17:271776#endif
1777
Dale Curtis5bba03232018-08-30 17:57:381778 MaybeSetContainerName();
Dan Sanders6edfd782019-08-13 00:13:181779 simple_watch_timer_.Stop();
Ted Meyer380ca982020-01-12 01:00:291780 media_log_->AddLogRecord(media_log_->CreatePipelineErrorEvent(status));
Dale Curtis74612b72017-12-14 20:56:191781 media_metrics_provider_->OnError(status);
Dale Curtisccfd0cca2017-08-31 01:27:561782 if (watch_time_reporter_)
1783 watch_time_reporter_->OnError(status);
alokp967c902452016-05-06 05:21:371784
Blink Reformat1c4d759e2017-04-09 16:34:541785 if (ready_state_ == WebMediaPlayer::kReadyStateHaveNothing) {
alokp967c902452016-05-06 05:21:371786 // Any error that occurs before reaching ReadyStateHaveMetadata should
1787 // be considered a format error.
Blink Reformat1c4d759e2017-04-09 16:34:541788 SetNetworkState(WebMediaPlayer::kNetworkStateFormatError);
alokp967c902452016-05-06 05:21:371789 } else {
Antonio Gomesf01cfbd2019-07-12 08:53:111790 SetNetworkState(blink::PipelineErrorToNetworkState(status));
alokp967c902452016-05-06 05:21:371791 }
1792
Thomas Guilbert2e591392017-08-12 00:56:381793 // PipelineController::Stop() is idempotent.
Antonio Gomes81b56552019-05-15 22:15:281794 pipeline_controller_->Stop();
Thomas Guilbert2e591392017-08-12 00:56:381795
alokp967c902452016-05-06 05:21:371796 UpdatePlayState();
1797}
1798
1799void WebMediaPlayerImpl::OnEnded() {
John Chen5b164a02017-11-01 00:36:091800 TRACE_EVENT2("media", "WebMediaPlayerImpl::OnEnded", "duration", Duration(),
1801 "id", media_log_->id());
pkastingf5279482016-07-27 02:18:201802 DVLOG(1) << __func__;
acolwellb4034942014-08-28 15:42:431803 DCHECK(main_task_runner_->BelongsToCurrentThread());
scherkusd2c745b2014-09-04 05:03:401804
sandersd1c0bba02016-03-04 23:14:081805 // Ignore state changes until we've completed all outstanding operations.
Antonio Gomes81b56552019-05-15 22:15:281806 if (!pipeline_controller_->IsStable())
scherkusd2c745b2014-09-04 05:03:401807 return;
1808
1809 ended_ = true;
Blink Reformat1c4d759e2017-04-09 16:34:541810 client_->TimeChanged();
sandersd50a635e2016-04-04 22:50:091811
1812 // We don't actually want this to run until |client_| calls seek() or pause(),
1813 // but that should have already happened in timeChanged() and so this is
1814 // expected to be a no-op.
1815 UpdatePlayState();
[email protected]576537842009-08-12 23:52:051816}
1817
Chisoon Jeong0a8e4bc2019-05-11 00:06:141818void WebMediaPlayerImpl::OnMetadata(const PipelineMetadata& metadata) {
pkastingf5279482016-07-27 02:18:201819 DVLOG(1) << __func__;
acolwellb4034942014-08-28 15:42:431820 DCHECK(main_task_runner_->BelongsToCurrentThread());
Dale Curtisff576552018-03-30 02:32:441821
1822 // Cache the |time_to_metadata_| to use for adjusting the TimeToFirstFrame and
1823 // TimeToPlayReady metrics later if we end up doing a suspended startup.
1824 time_to_metadata_ = base::TimeTicks::Now() - load_start_time_;
1825 media_metrics_provider_->SetTimeToMetadata(time_to_metadata_);
1826 RecordTimingUMA("Media.TimeToMetadata", time_to_metadata_);
[email protected]a8e2cb82012-08-17 00:02:391827
Dale Curtis5bba03232018-08-30 17:57:381828 MaybeSetContainerName();
1829
[email protected]b8877772014-03-26 20:17:151830 pipeline_metadata_ = metadata;
[email protected]c7a918e2020-01-17 00:18:271831 if (power_status_helper_)
[email protected]dc996312019-12-19 19:26:381832 power_status_helper_->SetMetadata(metadata);
[email protected]739847c02014-01-16 00:12:251833
Ted Meyer4a427632019-05-01 19:05:271834 UMA_HISTOGRAM_ENUMERATION(
1835 "Media.VideoRotation",
1836 metadata.video_decoder_config.video_transformation().rotation,
1837 VIDEO_ROTATION_MAX + 1);
[email protected]21c3f7502013-03-23 03:29:511838
John Rummelldb5a7ef2018-05-16 00:28:011839 if (HasAudio()) {
Ted Meyerd5885f82019-07-16 19:19:171840 media_metrics_provider_->SetHasAudio(metadata.audio_decoder_config.codec());
John Rummelldb5a7ef2018-05-16 00:28:011841 RecordEncryptionScheme("Audio",
1842 metadata.audio_decoder_config.encryption_scheme());
1843 }
1844
Blink Reformat1c4d759e2017-04-09 16:34:541845 if (HasVideo()) {
Ted Meyerd5885f82019-07-16 19:19:171846 media_metrics_provider_->SetHasVideo(metadata.video_decoder_config.codec());
John Rummelldb5a7ef2018-05-16 00:28:011847 RecordEncryptionScheme("Video",
1848 metadata.video_decoder_config.encryption_scheme());
1849
liberato2fd111be2017-01-04 00:25:061850 if (overlay_enabled_) {
1851 // SurfaceView doesn't support rotated video, so transition back if
[email protected]c8d574722017-08-30 20:53:431852 // the video is now rotated. If |always_enable_overlays_|, we keep the
liberato2fd111be2017-01-04 00:25:061853 // overlay anyway so that the state machine keeps working.
[email protected]c8d574722017-08-30 20:53:431854 // TODO(liberato): verify if compositor feedback catches this. If so,
1855 // then we don't need this check.
1856 if (!always_enable_overlays_ && !DoesOverlaySupportMetadata())
liberato2fd111be2017-01-04 00:25:061857 DisableOverlay();
liberato2fd111be2017-01-04 00:25:061858 }
watkf835a792016-06-24 23:24:401859
[email protected]702d721d2018-10-25 21:55:271860 if (surface_layer_mode_ ==
Mounir Lamouri28d648102019-11-27 16:49:031861 blink::WebMediaPlayer::SurfaceLayerMode::kAlways) {
[email protected]702d721d2018-10-25 21:55:271862 ActivateSurfaceLayerForVideo();
1863 } else {
danakj6e669e782018-05-16 16:57:171864 DCHECK(!video_layer_);
Ted Meyer4a427632019-05-01 19:05:271865 // TODO(tmathmeyer) does this need support for reflections as well?
danakj25f030112018-05-11 18:26:541866 video_layer_ = cc::VideoLayer::Create(
Julien Isorce6c83d8de2017-10-12 13:11:291867 compositor_.get(),
Ted Meyer4a427632019-05-01 19:05:271868 pipeline_metadata_.video_decoder_config.video_transformation()
1869 .rotation);
danakj8bc61c72018-05-16 13:55:061870 video_layer_->SetContentsOpaque(opaque_);
danakj8d204a42018-05-18 18:05:351871 client_->SetCcLayer(video_layer_.get());
lethalantidote7f6009d2017-07-07 21:47:391872 }
[email protected]a8e2cb82012-08-17 00:02:391873 }
dalecurtis8e4dc682016-03-15 02:30:301874
xjzd3fe45a2016-10-12 18:26:371875 if (observer_)
xjz15b483f2017-01-12 00:21:361876 observer_->OnMetadataChanged(pipeline_metadata_);
xjzd3fe45a2016-10-12 18:26:371877
Dale Curtisc7d2a7d22018-01-11 20:01:051878 // TODO(dalecurtis): Don't create these until kReadyStateHaveFutureData; when
1879 // we create them early we just increase the chances of needing to throw them
1880 // away unnecessarily.
dalecurtis04bdb582016-08-17 22:15:231881 CreateWatchTimeReporter();
Chris Cunninghamd9df58e2017-08-29 00:04:231882 CreateVideoDecodeStatsReporter();
Dale Curtisc7d2a7d22018-01-11 20:01:051883
Dale Curtis1e7a02b42019-05-28 20:12:241884 // SetReadyState() may trigger all sorts of calls into this class (e.g.,
1885 // Play(), Pause(), etc) so do it last to avoid unexpected states during the
1886 // calls. An exception to this is UpdatePlayState(), which is safe to call and
1887 // needs to use the new ReadyState in its calculations.
Dale Curtis2fc01cc2019-05-20 22:53:521888 SetReadyState(WebMediaPlayer::kReadyStateHaveMetadata);
Dale Curtis1e7a02b42019-05-28 20:12:241889 UpdatePlayState();
[email protected]a8e2cb82012-08-17 00:02:391890}
1891
[email protected]69db58f2018-09-26 20:27:561892void WebMediaPlayerImpl::ActivateSurfaceLayerForVideo() {
1893 // Note that we might or might not already be in VideoLayer mode.
1894 DCHECK(!bridge_);
1895
1896 surface_layer_for_video_enabled_ = true;
1897
1898 // If we're in VideoLayer mode, then get rid of the layer.
1899 if (video_layer_) {
1900 client_->SetCcLayer(nullptr);
1901 video_layer_ = nullptr;
1902 }
1903
1904 bridge_ = std::move(create_bridge_callback_)
1905 .Run(this, compositor_->GetUpdateSubmissionStateCallback());
1906 bridge_->CreateSurfaceLayer();
1907
Ted Meyer4a427632019-05-01 19:05:271908 // TODO(tmathmeyer) does this need support for the reflection transformation
1909 // as well?
[email protected]69db58f2018-09-26 20:27:561910 vfc_task_runner_->PostTask(
1911 FROM_HERE,
Ted Meyer4a427632019-05-01 19:05:271912 base::BindOnce(
1913 &VideoFrameCompositor::EnableSubmission,
1914 base::Unretained(compositor_.get()), bridge_->GetSurfaceId(),
1915 bridge_->GetLocalSurfaceIdAllocationTime(),
1916 pipeline_metadata_.video_decoder_config.video_transformation()
1917 .rotation,
1918 IsInPictureInPicture()));
[email protected]69db58f2018-09-26 20:27:561919 bridge_->SetContentsOpaque(opaque_);
1920
1921 // If the element is already in Picture-in-Picture mode, it means that it
1922 // was set in this mode prior to this load, with a different
1923 // WebMediaPlayerImpl. The new player needs to send its id, size and
1924 // surface id to the browser process to make sure the states are properly
1925 // updated.
1926 // TODO(872056): the surface should be activated but for some reasons, it
1927 // does not. It is possible that this will no longer be needed after 872056
1928 // is fixed.
Dale Curtisca1b78f2019-01-15 03:11:261929 if (IsInPictureInPicture())
[email protected]69db58f2018-09-26 20:27:561930 OnSurfaceIdUpdated(bridge_->GetSurfaceId());
[email protected]69db58f2018-09-26 20:27:561931}
1932
Chris Cunninghamfc0d67e2019-07-22 20:29:161933void WebMediaPlayerImpl::OnBufferingStateChange(
1934 BufferingState state,
1935 BufferingStateChangeReason reason) {
1936 OnBufferingStateChangeInternal(state, reason, false);
Dale Curtis2dc6089a2018-03-26 16:47:581937}
1938
Chris Cunninghamd9df58e2017-08-29 00:04:231939void WebMediaPlayerImpl::CreateVideoDecodeStatsReporter() {
1940 // TODO(chcunningham): destroy reporter if we initially have video but the
1941 // track gets disabled. Currently not possible in default desktop Chrome.
1942 if (!HasVideo())
1943 return;
1944
Chris Cunningham89b4b762019-03-27 17:13:331945 // Only record stats from the local pipeline.
1946 if (is_flinging_ || is_remote_rendering_ || using_media_player_renderer_)
1947 return;
1948
Chris Cunninghamd9df58e2017-08-29 00:04:231949 // Stats reporter requires a valid config. We may not have one for HLS cases
1950 // where URL demuxer doesn't know details of the stream.
1951 if (!pipeline_metadata_.video_decoder_config.IsValidConfig())
1952 return;
1953
Chris Cunningham6c0ec292019-04-04 18:31:111954 // Profile must be known for use as index to save the reported stats.
Chris Cunningham5b1b67eb2019-03-23 03:24:411955 if (pipeline_metadata_.video_decoder_config.profile() ==
1956 VIDEO_CODEC_PROFILE_UNKNOWN) {
1957 return;
1958 }
1959
Chris Cunningham6c0ec292019-04-04 18:31:111960 // CdmConfig must be provided for use as index to save encrypted stats.
1961 if (is_encrypted_ && !cdm_config_) {
Chris Cunninghamd9df58e2017-08-29 00:04:231962 return;
Chris Cunningham6c0ec292019-04-04 18:31:111963 } else if (cdm_config_) {
1964 DCHECK(!key_system_.empty());
1965 }
Chris Cunninghamd9df58e2017-08-29 00:04:231966
Gyuyoung Kima040bc52019-10-30 01:14:351967 mojo::PendingRemote<mojom::VideoDecodeStatsRecorder> recorder;
Dale Curtis7e8a510d2017-12-14 19:19:481968 media_metrics_provider_->AcquireVideoDecodeStatsRecorder(
Gyuyoung Kima040bc52019-10-30 01:14:351969 recorder.InitWithNewPipeAndPassReceiver());
Chris Cunninghamc7c6a6d2017-11-23 14:06:451970
Chris Cunninghamd9df58e2017-08-29 00:04:231971 // Create capabilities reporter and synchronize its initial state.
1972 video_decode_stats_reporter_.reset(new VideoDecodeStatsReporter(
Chris Cunninghamc7c6a6d2017-11-23 14:06:451973 std::move(recorder),
Chris Cunninghamd9df58e2017-08-29 00:04:231974 base::Bind(&WebMediaPlayerImpl::GetPipelineStatistics,
1975 base::Unretained(this)),
Chris Cunningham5b1b67eb2019-03-23 03:24:411976 pipeline_metadata_.video_decoder_config.profile(),
Chris Cunningham6c0ec292019-04-04 18:31:111977 pipeline_metadata_.natural_size, key_system_, cdm_config_,
Hajime Hoshib5a26ee2018-05-14 12:47:511978 frame_->GetTaskRunner(blink::TaskType::kInternalMedia)));
Chris Cunninghamd9df58e2017-08-29 00:04:231979
1980 if (delegate_->IsFrameHidden())
1981 video_decode_stats_reporter_->OnHidden();
1982 else
1983 video_decode_stats_reporter_->OnShown();
1984
1985 if (paused_)
1986 video_decode_stats_reporter_->OnPaused();
1987 else
1988 video_decode_stats_reporter_->OnPlaying();
1989}
1990
hubbeb2d3efd2017-05-05 23:26:381991void WebMediaPlayerImpl::OnProgress() {
Chris Watkins1ffabc5f2017-05-10 20:52:091992 DVLOG(4) << __func__;
Dale Curtisad645f72019-09-06 23:28:161993
1994 // See IsPrerollAttemptNeeded() for more details. We can't use that method
1995 // here since it considers |preroll_attempt_start_time_| and for OnProgress()
1996 // events we must make the attempt -- since there may not be another event.
1997 if (highest_ready_state_ < ReadyState::kReadyStateHaveFutureData) {
hubbeb2d3efd2017-05-05 23:26:381998 // Reset the preroll attempt clock.
1999 preroll_attempt_pending_ = true;
2000 preroll_attempt_start_time_ = base::TimeTicks();
2001
2002 // Clear any 'stale' flag and give the pipeline a chance to resume. If we
2003 // are already resumed, this will cause |preroll_attempt_start_time_| to
2004 // be set.
2005 delegate_->ClearStaleFlag(delegate_id_);
2006 UpdatePlayState();
2007 } else if (ready_state_ == ReadyState::kReadyStateHaveFutureData &&
2008 CanPlayThrough()) {
2009 SetReadyState(WebMediaPlayer::kReadyStateHaveEnoughData);
2010 }
2011}
2012
2013bool WebMediaPlayerImpl::CanPlayThrough() {
2014 if (!base::FeatureList::IsEnabled(kSpecCompliantCanPlayThrough))
2015 return true;
2016 if (chunk_demuxer_)
2017 return true;
Dale Curtis4841c712018-12-13 18:14:052018 if (data_source_ && data_source_->AssumeFullyBuffered())
hubbeb2d3efd2017-05-05 23:26:382019 return true;
2020 // If we're not currently downloading, we have as much buffer as
2021 // we're ever going to get, which means we say we can play through.
2022 if (network_state_ == WebMediaPlayer::kNetworkStateIdle)
2023 return true;
Antonio Gomes81b56552019-05-15 22:15:282024 return buffered_data_source_host_->CanPlayThrough(
hubbeb2d3efd2017-05-05 23:26:382025 base::TimeDelta::FromSecondsD(CurrentTime()),
2026 base::TimeDelta::FromSecondsD(Duration()),
2027 playback_rate_ == 0.0 ? 1.0 : playback_rate_);
2028}
2029
Dale Curtisff576552018-03-30 02:32:442030void WebMediaPlayerImpl::OnBufferingStateChangeInternal(
2031 BufferingState state,
Chris Cunninghamfc0d67e2019-07-22 20:29:162032 BufferingStateChangeReason reason,
Dale Curtisff576552018-03-30 02:32:442033 bool for_suspended_start) {
Chris Cunninghamfc0d67e2019-07-22 20:29:162034 DVLOG(1) << __func__ << "(" << state << ", " << reason << ")";
alokp967c902452016-05-06 05:21:372035 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]b8877772014-03-26 20:17:152036
Ted Meyer742212b82018-08-22 17:21:422037 // Ignore buffering state changes caused by back-to-back seeking, so as not
2038 // to assume the second seek has finished when it was only the first seek.
Antonio Gomes81b56552019-05-15 22:15:282039 if (pipeline_controller_->IsPendingSeek())
[email protected]ba7d5f92014-06-24 05:37:402040 return;
[email protected]b8877772014-03-26 20:17:152041
Dale Curtisff576552018-03-30 02:32:442042 auto log_event = media_log_->CreateBufferingStateChangedEvent(
Chris Cunninghamfc0d67e2019-07-22 20:29:162043 "pipeline_buffering_state", state, reason);
Dale Curtisff576552018-03-30 02:32:442044 log_event->params.SetBoolean("for_suspended_start", for_suspended_start);
Ted Meyer380ca982020-01-12 01:00:292045 media_log_->AddLogRecord(std::move(log_event));
dalecurtis869bf2f2017-01-10 18:01:102046
Ted Meyerd5885f82019-07-16 19:19:172047 if (state == BUFFERING_HAVE_ENOUGH && !for_suspended_start)
2048 media_metrics_provider_->SetHaveEnough();
2049
chcunninghameb270c92016-07-15 01:00:452050 if (state == BUFFERING_HAVE_ENOUGH) {
John Chen5b164a02017-11-01 00:36:092051 TRACE_EVENT1("media", "WebMediaPlayerImpl::BufferingHaveEnough", "id",
2052 media_log_->id());
Dale Curtisff576552018-03-30 02:32:442053 // The SetReadyState() call below may clear
2054 // |skip_metrics_due_to_startup_suspend_| so report this first.
2055 if (!have_reported_time_to_play_ready_ &&
2056 !skip_metrics_due_to_startup_suspend_) {
2057 DCHECK(!for_suspended_start);
Dale Curtis3899090ea2018-01-12 00:10:352058 have_reported_time_to_play_ready_ = true;
2059 const base::TimeDelta elapsed = base::TimeTicks::Now() - load_start_time_;
2060 media_metrics_provider_->SetTimeToPlayReady(elapsed);
2061 RecordTimingUMA("Media.TimeToPlayReady", elapsed);
2062 }
[email protected]ba7d5f92014-06-24 05:37:402063
Dale Curtisff576552018-03-30 02:32:442064 // Warning: This call may be re-entrant.
2065 SetReadyState(CanPlayThrough() ? WebMediaPlayer::kReadyStateHaveEnoughData
2066 : WebMediaPlayer::kReadyStateHaveFutureData);
2067
Dale Curtis18ad391b2019-05-30 23:25:092068 // Let the DataSource know we have enough data -- this is the only function
2069 // during which we advance to (or past) the kReadyStateHaveEnoughData state.
2070 // It may use this information to update buffer sizes or release unused
2071 // network connections.
2072 MaybeUpdateBufferSizesForPlayback();
Dale Curtis04769ca2019-05-25 00:38:102073 if (mb_data_source_ && !client_->CouldPlayIfEnoughData()) {
2074 // For LazyLoad this will be handled during OnPipelineSuspended().
2075 if (for_suspended_start && did_lazy_load_)
2076 DCHECK(!have_enough_after_lazy_load_cb_.IsCancelled());
2077 else
2078 mb_data_source_->OnBufferingHaveEnough(false);
2079 }
dalecurtis849cf4b22015-03-27 18:35:452080
chcunninghameb270c92016-07-15 01:00:452081 // Blink expects a timeChanged() in response to a seek().
sandersd35d2c3f2017-01-14 02:04:422082 if (should_notify_time_changed_) {
2083 should_notify_time_changed_ = false;
Blink Reformat1c4d759e2017-04-09 16:34:542084 client_->TimeChanged();
sandersd35d2c3f2017-01-14 02:04:422085 }
dalecurtis0f0097a2015-12-01 17:40:472086
chcunninghameb270c92016-07-15 01:00:452087 // Once we have enough, start reporting the total memory usage. We'll also
2088 // report once playback starts.
2089 ReportMemoryUsage();
dalecurtis9d638a12016-08-30 06:20:552090
dalecurtis4f6d14d2017-02-22 17:42:222091 // Report the amount of time it took to leave the underflow state.
2092 if (underflow_timer_) {
Dale Curtisc9069622019-10-24 20:20:502093 auto elapsed = underflow_timer_->Elapsed();
2094 RecordUnderflowDuration(elapsed);
2095 watch_time_reporter_->OnUnderflowComplete(elapsed);
dalecurtis9d638a12016-08-30 06:20:552096 underflow_timer_.reset();
2097 }
chcunninghameb270c92016-07-15 01:00:452098 } else {
2099 // Buffering has underflowed.
2100 DCHECK_EQ(state, BUFFERING_HAVE_NOTHING);
dalecurtisacd77d62016-09-09 23:23:142101
dalecurtisd06eabc2017-02-24 23:43:292102 // Report the number of times we've entered the underflow state. Ensure we
2103 // only report the value when transitioning from HAVE_ENOUGH to
2104 // HAVE_NOTHING.
Dale Curtis6995b862017-05-31 22:20:082105 if (ready_state_ == WebMediaPlayer::kReadyStateHaveEnoughData &&
2106 !seeking_) {
dalecurtisacd77d62016-09-09 23:23:142107 underflow_timer_.reset(new base::ElapsedTimer());
Dale Curtis6995b862017-05-31 22:20:082108 watch_time_reporter_->OnUnderflow();
2109 }
dalecurtisacd77d62016-09-09 23:23:142110
chcunninghameb270c92016-07-15 01:00:452111 // It shouldn't be possible to underflow if we've not advanced past
2112 // HAVE_CURRENT_DATA.
Blink Reformat1c4d759e2017-04-09 16:34:542113 DCHECK_GT(highest_ready_state_, WebMediaPlayer::kReadyStateHaveCurrentData);
2114 SetReadyState(WebMediaPlayer::kReadyStateHaveCurrentData);
chcunninghameb270c92016-07-15 01:00:452115 }
sandersd50a635e2016-04-04 22:50:092116
2117 UpdatePlayState();
[email protected]b8877772014-03-26 20:17:152118}
2119
alokp967c902452016-05-06 05:21:372120void WebMediaPlayerImpl::OnDurationChange() {
acolwellb4034942014-08-28 15:42:432121 DCHECK(main_task_runner_->BelongsToCurrentThread());
alokp967c902452016-05-06 05:21:372122
John Delaney2371b452018-12-13 04:30:332123 if (frame_->IsAdSubframe()) {
2124 UMA_HISTOGRAM_CUSTOM_TIMES("Ads.Media.Duration", GetPipelineMediaDuration(),
2125 base::TimeDelta::FromMilliseconds(1),
2126 base::TimeDelta::FromDays(1),
2127 50 /* bucket_count */);
2128 }
2129
alokp967c902452016-05-06 05:21:372130 // TODO(sandersd): We should call delegate_->DidPlay() with the new duration,
2131 // especially if it changed from <5s to >5s.
Blink Reformat1c4d759e2017-04-09 16:34:542132 if (ready_state_ == WebMediaPlayer::kReadyStateHaveNothing)
alokp967c902452016-05-06 05:21:372133 return;
2134
Blink Reformat1c4d759e2017-04-09 16:34:542135 client_->DurationChanged();
Dale Curtisaebaeea2018-07-19 23:42:112136 if (watch_time_reporter_)
2137 watch_time_reporter_->OnDurationChanged(GetPipelineMediaDuration());
Becca Hughes7a85bf22019-07-24 16:16:582138
2139 // The duration has changed so we should update the media position state.
2140 UpdateMediaPositionState();
[email protected]81bb3322011-07-21 15:55:502141}
2142
alokp967c902452016-05-06 05:21:372143void WebMediaPlayerImpl::OnAddTextTrack(const TextTrackConfig& config,
Raymond Toy85ad6802019-12-17 23:55:192144 AddTextTrackDoneCB done_cb) {
acolwellb4034942014-08-28 15:42:432145 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]71537722013-05-23 06:47:532146
[email protected]8a561062013-11-22 01:19:312147 const WebInbandTextTrackImpl::Kind web_kind =
2148 static_cast<WebInbandTextTrackImpl::Kind>(config.kind());
Blink Reformat1c4d759e2017-04-09 16:34:542149 const blink::WebString web_label = blink::WebString::FromUTF8(config.label());
[email protected]8a561062013-11-22 01:19:312150 const blink::WebString web_language =
Blink Reformat1c4d759e2017-04-09 16:34:542151 blink::WebString::FromUTF8(config.language());
2152 const blink::WebString web_id = blink::WebString::FromUTF8(config.id());
[email protected]71537722013-05-23 06:47:532153
dcheng3076abbf2016-04-22 20:42:392154 std::unique_ptr<WebInbandTextTrackImpl> web_inband_text_track(
henriks9cce5fa2014-12-12 09:35:302155 new WebInbandTextTrackImpl(web_kind, web_label, web_language, web_id));
[email protected]8a561062013-11-22 01:19:312156
Dale Curtisca1b78f2019-01-15 03:11:262157 std::unique_ptr<TextTrack> text_track(new TextTrackImpl(
dcheng652f5ff2015-12-27 08:54:002158 main_task_runner_, client_, std::move(web_inband_text_track)));
[email protected]8a561062013-11-22 01:19:312159
Raymond Toy85ad6802019-12-17 23:55:192160 std::move(done_cb).Run(std::move(text_track));
[email protected]71537722013-05-23 06:47:532161}
2162
Xiaohan Wang640b41d2018-12-18 19:00:462163void WebMediaPlayerImpl::OnWaiting(WaitingReason reason) {
alokp967c902452016-05-06 05:21:372164 DCHECK(main_task_runner_->BelongsToCurrentThread());
2165
Xiaohan Wang640b41d2018-12-18 19:00:462166 switch (reason) {
Xiaohan Wangb54a75282019-10-04 21:34:432167 case WaitingReason::kNoCdm:
Xiaohan Wang640b41d2018-12-18 19:00:462168 case WaitingReason::kNoDecryptionKey:
2169 encrypted_client_->DidBlockPlaybackWaitingForKey();
2170 // TODO(jrummell): didResumePlaybackBlockedForKey() should only be called
2171 // when a key has been successfully added (e.g. OnSessionKeysChange() with
2172 // |has_additional_usable_key| = true). http://crbug.com/461903
2173 encrypted_client_->DidResumePlaybackBlockedForKey();
2174 return;
Xiaohan Wang640b41d2018-12-18 19:00:462175
Xiaohan Wanga7224d62019-01-04 22:22:302176 // Ideally this should be handled by PipelineController directly without
2177 // being proxied here. But currently Pipeline::Client (|this|) is passed to
2178 // PipelineImpl directly without going through |pipeline_controller_|,
2179 // making it difficult to do.
2180 // TODO(xhwang): Handle this in PipelineController when we have a clearer
2181 // picture on how to refactor WebMediaPlayerImpl, PipelineController and
2182 // PipelineImpl.
2183 case WaitingReason::kDecoderStateLost:
Antonio Gomes81b56552019-05-15 22:15:282184 pipeline_controller_->OnDecoderStateLost();
Xiaohan Wanga7224d62019-01-04 22:22:302185 return;
2186 }
alokp967c902452016-05-06 05:21:372187}
2188
alokp5d86e9b2016-05-17 20:20:412189void WebMediaPlayerImpl::OnVideoNaturalSizeChange(const gfx::Size& size) {
2190 DCHECK(main_task_runner_->BelongsToCurrentThread());
Blink Reformat1c4d759e2017-04-09 16:34:542191 DCHECK_NE(ready_state_, WebMediaPlayer::kReadyStateHaveNothing);
alokp5d86e9b2016-05-17 20:20:412192
Chris Cunningham038548b2017-07-10 22:36:302193 TRACE_EVENT0("media", "WebMediaPlayerImpl::OnVideoNaturalSizeChange");
xhwang60802652017-04-19 07:29:582194
xjz15b483f2017-01-12 00:21:362195 // The input |size| is from the decoded video frame, which is the original
2196 // natural size and need to be rotated accordingly.
Julien Isorce6c83d8de2017-10-12 13:11:292197 gfx::Size rotated_size = GetRotatedVideoSize(
Ted Meyer4a427632019-05-01 19:05:272198 pipeline_metadata_.video_decoder_config.video_transformation().rotation,
2199 size);
sandersd2c478422016-08-02 01:19:252200
xhwang60802652017-04-19 07:29:582201 RecordVideoNaturalSize(rotated_size);
2202
2203 gfx::Size old_size = pipeline_metadata_.natural_size;
2204 if (rotated_size == old_size)
alokp5d86e9b2016-05-17 20:20:412205 return;
2206
xjz516ef6d2017-01-07 00:23:062207 pipeline_metadata_.natural_size = rotated_size;
Dan Sanders7ef5d0f2019-05-03 18:30:112208
2209 if (using_media_player_renderer_ && old_size.IsEmpty()) {
2210 // If we are using MediaPlayerRenderer and this is the first size change, we
2211 // now know that there is a video track. This condition is paired with code
2212 // in CreateWatchTimeReporter() that guesses the existence of a video track.
2213 CreateWatchTimeReporter();
2214 } else {
Dan Sanders7ef5d0f2019-05-03 18:30:112215 UpdateSecondaryProperties();
2216 }
dalecurtis25405562017-04-14 23:35:112217
Chris Cunningham5b1b67eb2019-03-23 03:24:412218 if (video_decode_stats_reporter_ &&
2219 !video_decode_stats_reporter_->MatchesBucketedNaturalSize(
2220 pipeline_metadata_.natural_size)) {
2221 CreateVideoDecodeStatsReporter();
2222 }
Chris Cunninghamd9df58e2017-08-29 00:04:232223
Blink Reformat1c4d759e2017-04-09 16:34:542224 client_->SizeChanged();
xjz516ef6d2017-01-07 00:23:062225
xjz15b483f2017-01-12 00:21:362226 if (observer_)
2227 observer_->OnMetadataChanged(pipeline_metadata_);
peconn257951522017-06-09 18:24:592228
2229 delegate_->DidPlayerSizeChange(delegate_id_, NaturalSize());
alokp5d86e9b2016-05-17 20:20:412230}
2231
2232void WebMediaPlayerImpl::OnVideoOpacityChange(bool opaque) {
2233 DCHECK(main_task_runner_->BelongsToCurrentThread());
Blink Reformat1c4d759e2017-04-09 16:34:542234 DCHECK_NE(ready_state_, WebMediaPlayer::kReadyStateHaveNothing);
alokp5d86e9b2016-05-17 20:20:412235
2236 opaque_ = opaque;
Dale Curtis357630f22019-01-18 01:10:172237 if (!surface_layer_for_video_enabled_ && video_layer_)
2238 video_layer_->SetContentsOpaque(opaque_);
2239 else if (bridge_->GetCcLayer())
CJ DiMeglioa2b13fbc2018-06-27 00:50:592240 bridge_->SetContentsOpaque(opaque_);
alokp5d86e9b2016-05-17 20:20:412241}
2242
[email protected]218ac8de2020-01-03 23:41:432243void WebMediaPlayerImpl::OnVideoFrameRateChange(base::Optional<int> fps) {
2244 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]c7a918e2020-01-17 00:18:272245 if (power_status_helper_)
2246 power_status_helper_->SetAverageFrameRate(fps);
[email protected]218ac8de2020-01-03 23:41:432247}
2248
Chris Cunningham038548b2017-07-10 22:36:302249void WebMediaPlayerImpl::OnAudioConfigChange(const AudioDecoderConfig& config) {
2250 DCHECK(main_task_runner_->BelongsToCurrentThread());
2251 DCHECK_NE(ready_state_, WebMediaPlayer::kReadyStateHaveNothing);
2252
Dale Curtisccfd0cca2017-08-31 01:27:562253 const bool codec_change =
2254 pipeline_metadata_.audio_decoder_config.codec() != config.codec();
Dale Curtisf8afcef32020-01-18 06:23:262255 const bool codec_profile_change =
2256 pipeline_metadata_.audio_decoder_config.profile() != config.profile();
2257
Chris Cunningham038548b2017-07-10 22:36:302258 pipeline_metadata_.audio_decoder_config = config;
2259
2260 if (observer_)
2261 observer_->OnMetadataChanged(pipeline_metadata_);
Dale Curtisccfd0cca2017-08-31 01:27:562262
Dale Curtisf8afcef32020-01-18 06:23:262263 if (codec_change || codec_profile_change)
Dale Curtis96d6e9382018-07-18 18:01:072264 UpdateSecondaryProperties();
Chris Cunningham038548b2017-07-10 22:36:302265}
2266
2267void WebMediaPlayerImpl::OnVideoConfigChange(const VideoDecoderConfig& config) {
2268 DCHECK(main_task_runner_->BelongsToCurrentThread());
2269 DCHECK_NE(ready_state_, WebMediaPlayer::kReadyStateHaveNothing);
2270
Dale Curtisccfd0cca2017-08-31 01:27:562271 const bool codec_change =
2272 pipeline_metadata_.video_decoder_config.codec() != config.codec();
Chris Cunningham5b1b67eb2019-03-23 03:24:412273 const bool codec_profile_change =
2274 pipeline_metadata_.video_decoder_config.profile() != config.profile();
Dale Curtisccfd0cca2017-08-31 01:27:562275
Chris Cunningham038548b2017-07-10 22:36:302276 pipeline_metadata_.video_decoder_config = config;
2277
2278 if (observer_)
2279 observer_->OnMetadataChanged(pipeline_metadata_);
Chris Cunninghamd9df58e2017-08-29 00:04:232280
Dale Curtisf8afcef32020-01-18 06:23:262281 if (codec_change || codec_profile_change)
Dale Curtis96d6e9382018-07-18 18:01:072282 UpdateSecondaryProperties();
Chris Cunningham5b1b67eb2019-03-23 03:24:412283
2284 if (video_decode_stats_reporter_ && codec_profile_change)
2285 CreateVideoDecodeStatsReporter();
Chris Cunningham038548b2017-07-10 22:36:302286}
2287
avayvodeecec52c2017-02-14 01:25:092288void WebMediaPlayerImpl::OnVideoAverageKeyframeDistanceUpdate() {
2289 UpdateBackgroundVideoOptimizationState();
2290}
2291
Ted Meyerd5885f82019-07-16 19:19:172292void WebMediaPlayerImpl::OnAudioDecoderChange(const PipelineDecoderInfo& info) {
2293 media_metrics_provider_->SetAudioPipelineInfo(info);
2294 if (info.decoder_name == audio_decoder_name_)
Dale Curtisc7d2a7d22018-01-11 20:01:052295 return;
2296
Ted Meyerd5885f82019-07-16 19:19:172297 audio_decoder_name_ = info.decoder_name;
Dale Curtisc7d2a7d22018-01-11 20:01:052298
2299 // If there's no current reporter, there's nothing to be done.
2300 if (!watch_time_reporter_)
2301 return;
2302
Dale Curtis96d6e9382018-07-18 18:01:072303 UpdateSecondaryProperties();
Dale Curtisc7d2a7d22018-01-11 20:01:052304}
2305
Ted Meyerd5885f82019-07-16 19:19:172306void WebMediaPlayerImpl::OnVideoDecoderChange(const PipelineDecoderInfo& info) {
2307 media_metrics_provider_->SetVideoPipelineInfo(info);
2308 if (info.decoder_name == video_decoder_name_)
Dale Curtisc7d2a7d22018-01-11 20:01:052309 return;
2310
Ted Meyerd5885f82019-07-16 19:19:172311 video_decoder_name_ = info.decoder_name;
Dale Curtisc7d2a7d22018-01-11 20:01:052312
2313 // If there's no current reporter, there's nothing to be done.
2314 if (!watch_time_reporter_)
2315 return;
2316
Dale Curtis96d6e9382018-07-18 18:01:072317 UpdateSecondaryProperties();
Dale Curtisc7d2a7d22018-01-11 20:01:052318}
2319
sandersd35d2c3f2017-01-14 02:04:422320void WebMediaPlayerImpl::OnFrameHidden() {
sandersd1e49fb62015-12-12 01:18:062321 DCHECK(main_task_runner_->BelongsToCurrentThread());
avayvod39c102402016-11-23 21:43:132322
avayvod65fad272017-02-24 01:00:482323 // Backgrounding a video requires a user gesture to resume playback.
2324 if (IsHidden())
2325 video_locked_when_paused_when_hidden_ = true;
2326
dalecurtis04bdb582016-08-17 22:15:232327 if (watch_time_reporter_)
2328 watch_time_reporter_->OnHidden();
avayvod48a8be52016-08-04 19:52:502329
Chris Cunninghamd9df58e2017-08-29 00:04:232330 if (video_decode_stats_reporter_)
2331 video_decode_stats_reporter_->OnHidden();
2332
avayvod65fad272017-02-24 01:00:482333 UpdateBackgroundVideoOptimizationState();
sandersd50a635e2016-04-04 22:50:092334 UpdatePlayState();
dalecurtis8b8505e72016-06-10 21:59:172335
2336 // Schedule suspended playing media to be paused if the user doesn't come back
2337 // to it within some timeout period to avoid any autoplay surprises.
2338 ScheduleIdlePauseTimer();
Dale Curtisca1b78f2019-01-15 03:11:262339
2340 // Notify the compositor of our page visibility status.
2341 vfc_task_runner_->PostTask(
2342 FROM_HERE,
2343 base::BindOnce(&VideoFrameCompositor::SetIsPageVisible,
2344 base::Unretained(compositor_.get()), !IsHidden()));
sandersd1e49fb62015-12-12 01:18:062345}
2346
sandersd35d2c3f2017-01-14 02:04:422347void WebMediaPlayerImpl::OnFrameClosed() {
sandersd1e49fb62015-12-12 01:18:062348 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]28347e72017-06-27 17:30:112349
sandersd35d2c3f2017-01-14 02:04:422350 UpdatePlayState();
2351}
2352
2353void WebMediaPlayerImpl::OnFrameShown() {
2354 DCHECK(main_task_runner_->BelongsToCurrentThread());
2355 background_pause_timer_.Stop();
2356
avayvod65fad272017-02-24 01:00:482357 // Foreground videos don't require user gesture to continue playback.
2358 video_locked_when_paused_when_hidden_ = false;
2359
dalecurtis04bdb582016-08-17 22:15:232360 if (watch_time_reporter_)
2361 watch_time_reporter_->OnShown();
2362
Chris Cunninghamd9df58e2017-08-29 00:04:232363 if (video_decode_stats_reporter_)
2364 video_decode_stats_reporter_->OnShown();
2365
Dale Curtisca1b78f2019-01-15 03:11:262366 // Notify the compositor of our page visibility status.
2367 vfc_task_runner_->PostTask(
2368 FROM_HERE,
2369 base::BindOnce(&VideoFrameCompositor::SetIsPageVisible,
2370 base::Unretained(compositor_.get()), !IsHidden()));
2371
Dale Curtisdcbb81a2017-08-18 01:06:122372 UpdateBackgroundVideoOptimizationState();
avayvod65fad272017-02-24 01:00:482373
avayvod2135a642017-01-13 00:17:142374 if (paused_when_hidden_) {
2375 paused_when_hidden_ = false;
2376 OnPlay(); // Calls UpdatePlayState() so return afterwards.
2377 return;
2378 }
2379
sandersd50a635e2016-04-04 22:50:092380 UpdatePlayState();
sandersd1e49fb62015-12-12 01:18:062381}
2382
sandersd35d2c3f2017-01-14 02:04:422383void WebMediaPlayerImpl::OnIdleTimeout() {
dalecurtis0431cbf2016-03-12 01:19:432384 DCHECK(main_task_runner_->BelongsToCurrentThread());
2385
Dale Curtis99a9b482018-02-01 02:23:282386 // This should never be called when stale state testing overrides are used.
2387 DCHECK(!stale_state_override_for_testing_.has_value());
2388
sandersd35d2c3f2017-01-14 02:04:422389 // If we are attempting preroll, clear the stale flag.
2390 if (IsPrerollAttemptNeeded()) {
tguilbert1bb1c782017-01-23 21:15:112391 delegate_->ClearStaleFlag(delegate_id_);
sandersd35d2c3f2017-01-14 02:04:422392 return;
watkd026f792016-11-05 00:28:512393 }
sandersd50a635e2016-04-04 22:50:092394
sandersd35d2c3f2017-01-14 02:04:422395 UpdatePlayState();
dalecurtis0431cbf2016-03-12 01:19:432396}
2397
dalecurtisbb3eaac2016-01-27 21:10:252398void WebMediaPlayerImpl::OnPlay() {
Mounir Lamouri703106e2018-05-30 14:31:092399 client_->RequestPlay();
dalecurtisbb3eaac2016-01-27 21:10:252400}
2401
2402void WebMediaPlayerImpl::OnPause() {
Mounir Lamouri703106e2018-05-30 14:31:092403 client_->RequestPause();
dalecurtisbb3eaac2016-01-27 21:10:252404}
2405
François Beaufortb4fe7c62019-02-27 08:19:442406void WebMediaPlayerImpl::OnMuted(bool muted) {
2407 client_->RequestMuted(muted);
2408}
2409
Alec Douglas316cce42017-10-31 13:28:082410void WebMediaPlayerImpl::OnSeekForward(double seconds) {
2411 DCHECK_GE(seconds, 0) << "Attempted to seek by a negative number of seconds";
2412 client_->RequestSeek(CurrentTime() + seconds);
2413}
2414
2415void WebMediaPlayerImpl::OnSeekBackward(double seconds) {
2416 DCHECK_GE(seconds, 0) << "Attempted to seek by a negative number of seconds";
2417 client_->RequestSeek(CurrentTime() - seconds);
2418}
2419
dalecurtisbb3eaac2016-01-27 21:10:252420void WebMediaPlayerImpl::OnVolumeMultiplierUpdate(double multiplier) {
2421 volume_multiplier_ = multiplier;
Blink Reformat1c4d759e2017-04-09 16:34:542422 SetVolume(volume_);
dalecurtisbb3eaac2016-01-27 21:10:252423}
2424
zqzhang8ac49002017-03-16 21:51:352425void WebMediaPlayerImpl::OnBecamePersistentVideo(bool value) {
Blink Reformat1c4d759e2017-04-09 16:34:542426 client_->OnBecamePersistentVideo(value);
[email protected]6490e2d12019-01-14 19:18:372427 overlay_info_.is_persistent_video = value;
2428 MaybeSendOverlayInfoToDecoder();
zqzhang8ac49002017-03-16 21:51:352429}
2430
[email protected]201ce4ba2020-01-09 22:19:092431void WebMediaPlayerImpl::OnPowerExperimentState(bool state) {
2432 if (power_status_helper_)
2433 power_status_helper_->UpdatePowerExperimentState(state);
2434}
2435
watkdee516f2016-02-18 02:22:192436void WebMediaPlayerImpl::ScheduleRestart() {
sandersd50a635e2016-04-04 22:50:092437 // TODO(watk): All restart logic should be moved into PipelineController.
Antonio Gomes81b56552019-05-15 22:15:282438 if (pipeline_controller_->IsPipelineRunning() &&
2439 !pipeline_controller_->IsPipelineSuspended()) {
watkdee516f2016-02-18 02:22:192440 pending_suspend_resume_cycle_ = true;
sandersd50a635e2016-04-04 22:50:092441 UpdatePlayState();
watkdee516f2016-02-18 02:22:192442 }
2443}
2444
Blink Reformat1c4d759e2017-04-09 16:34:542445void WebMediaPlayerImpl::RequestRemotePlaybackDisabled(bool disabled) {
miu77f914c2016-11-19 23:56:182446 if (observer_)
2447 observer_->OnRemotePlaybackDisabled(disabled);
2448}
2449
Thomas Guilbert7350fdad2019-01-29 23:09:242450#if defined(OS_ANDROID)
Thomas Guilbertb341bae02018-05-09 00:02:132451void WebMediaPlayerImpl::FlingingStarted() {
2452 DCHECK(main_task_runner_->BelongsToCurrentThread());
2453 DCHECK(!disable_pipeline_auto_suspend_);
2454 disable_pipeline_auto_suspend_ = true;
2455
Thomas Guilbert29ae1a902018-10-20 01:53:382456 is_flinging_ = true;
2457
Thomas Guilbertb341bae02018-05-09 00:02:132458 // Capabilities reporting should only be performed for local playbacks.
2459 video_decode_stats_reporter_.reset();
2460
2461 // Requests to restart media pipeline. A flinging renderer will be created via
2462 // the |renderer_factory_selector_|.
2463 ScheduleRestart();
2464}
2465
2466void WebMediaPlayerImpl::FlingingStopped() {
2467 DCHECK(main_task_runner_->BelongsToCurrentThread());
2468 DCHECK(disable_pipeline_auto_suspend_);
2469 disable_pipeline_auto_suspend_ = false;
2470
Thomas Guilbert29ae1a902018-10-20 01:53:382471 is_flinging_ = false;
2472
Thomas Guilbertb341bae02018-05-09 00:02:132473 CreateVideoDecodeStatsReporter();
2474
2475 ScheduleRestart();
2476}
Thomas Guilbert901808982019-07-03 20:38:182477
2478void WebMediaPlayerImpl::OnRemotePlayStateChange(MediaStatus::State state) {
2479 DCHECK(is_flinging_);
Thomas Guilbertf6d0702e2019-09-12 23:40:142480 DCHECK(main_task_runner_->BelongsToCurrentThread());
Thomas Guilbert901808982019-07-03 20:38:182481
2482 if (state == MediaStatus::State::PLAYING && Paused()) {
2483 DVLOG(1) << __func__ << " requesting PLAY.";
2484 client_->RequestPlay();
2485 } else if (state == MediaStatus::State::PAUSED && !Paused()) {
2486 DVLOG(1) << __func__ << " requesting PAUSE.";
2487 client_->RequestPause();
2488 }
2489}
Thomas Guilbert7350fdad2019-01-29 23:09:242490#endif // defined(OS_ANDROID)
hubbee4027f92016-05-19 05:18:132491
Blink Reformat1c4d759e2017-04-09 16:34:542492void WebMediaPlayerImpl::SetPoster(const blink::WebURL& poster) {
Dan Sanderscd8981c2017-08-31 22:37:022493 has_poster_ = !poster.IsEmpty();
Dan Sanderscd8981c2017-08-31 22:37:022494}
xjzc102fd82017-01-04 20:13:532495
[email protected]fee8a902014-06-03 13:43:362496void WebMediaPlayerImpl::DataSourceInitialized(bool success) {
pkastingf5279482016-07-27 02:18:202497 DVLOG(1) << __func__;
acolwellb4034942014-08-28 15:42:432498 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]a9415292012-01-19 19:55:202499
Thomas Guilberta8a6e922019-02-01 00:02:472500 if (observer_ && mb_data_source_)
Dale Curtis4841c712018-12-13 18:14:052501 observer_->OnDataSourceInitialized(mb_data_source_->GetUrlAfterRedirects());
Anton Vayvod09fa66e2017-07-20 23:02:122502
[email protected]d250190da3b2012-07-23 22:57:302503 if (!success) {
Blink Reformat1c4d759e2017-04-09 16:34:542504 SetNetworkState(WebMediaPlayer::kNetworkStateFormatError);
Dale Curtis74612b72017-12-14 20:56:192505 media_metrics_provider_->OnError(PIPELINE_ERROR_NETWORK);
sandersd50a635e2016-04-04 22:50:092506
2507 // Not really necessary, since the pipeline was never started, but it at
2508 // least this makes sure that the error handling code is in sync.
2509 UpdatePlayState();
2510
[email protected]a9415292012-01-19 19:55:202511 return;
2512 }
2513
hubbee2cc88c092017-07-14 23:10:412514 // No point in preloading data as we'll probably just throw it away anyways.
Dale Curtis4841c712018-12-13 18:14:052515 if (IsStreaming() && preload_ > MultibufferDataSource::METADATA &&
2516 mb_data_source_) {
2517 mb_data_source_->SetPreload(MultibufferDataSource::METADATA);
hubbee2cc88c092017-07-14 23:10:412518 }
2519
[email protected]ef8394c2013-08-21 20:26:302520 StartPipeline();
[email protected]a9415292012-01-19 19:55:202521}
2522
[email protected]122f40252012-06-12 05:01:562523void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) {
hubbeb2d3efd2017-05-05 23:26:382524 DVLOG(1) << __func__ << "(" << is_downloading << ")";
Blink Reformat1c4d759e2017-04-09 16:34:542525 if (!is_downloading && network_state_ == WebMediaPlayer::kNetworkStateLoading)
2526 SetNetworkState(WebMediaPlayer::kNetworkStateIdle);
2527 else if (is_downloading &&
2528 network_state_ == WebMediaPlayer::kNetworkStateIdle)
2529 SetNetworkState(WebMediaPlayer::kNetworkStateLoading);
hubbeb2d3efd2017-05-05 23:26:382530 if (ready_state_ == ReadyState::kReadyStateHaveFutureData && !is_downloading)
2531 SetReadyState(WebMediaPlayer::kReadyStateHaveEnoughData);
[email protected]122f40252012-06-12 05:01:562532}
2533
liberato2ff93ad2017-05-17 07:28:242534void WebMediaPlayerImpl::OnOverlayRoutingToken(
2535 const base::UnguessableToken& token) {
2536 DCHECK(overlay_mode_ == OverlayMode::kUseAndroidOverlay);
liberatofe8f9692017-06-08 19:17:362537 // TODO(liberato): |token| should already be a RoutingToken.
2538 overlay_routing_token_is_pending_ = false;
2539 overlay_routing_token_ = OverlayInfo::RoutingToken(token);
liberato2ff93ad2017-05-17 07:28:242540 MaybeSendOverlayInfoToDecoder();
2541}
2542
2543void WebMediaPlayerImpl::OnOverlayInfoRequested(
dalecurtis4b632fce22016-11-10 00:52:172544 bool decoder_requires_restart_for_overlay,
liberato2ff93ad2017-05-17 07:28:242545 const ProvideOverlayInfoCB& provide_overlay_info_cb) {
watkdee516f2016-02-18 02:22:192546 DCHECK(main_task_runner_->BelongsToCurrentThread());
watkdee516f2016-02-18 02:22:192547
Chris Watkins557f84d2017-09-16 02:31:462548 // If we get a non-null cb, a decoder is initializing and requires overlay
2549 // info. If we get a null cb, a previously initialized decoder is
2550 // unregistering for overlay info updates.
Dale Curtise25163812018-09-21 22:13:392551 if (!provide_overlay_info_cb) {
tsunghungee562e92016-07-20 18:03:312552 decoder_requires_restart_for_overlay_ = false;
liberato2ff93ad2017-05-17 07:28:242553 provide_overlay_info_cb_.Reset();
watkdee516f2016-02-18 02:22:192554 return;
2555 }
2556
dalecurtis4b632fce22016-11-10 00:52:172557 // If |decoder_requires_restart_for_overlay| is true, we must restart the
2558 // pipeline for fullscreen transitions. The decoder is unable to switch
2559 // surfaces otherwise. If false, we simply need to tell the decoder about the
2560 // new surface and it will handle things seamlessly.
Chris Watkins557f84d2017-09-16 02:31:462561 // For encrypted video we pretend that the decoder doesn't require a restart
2562 // because it needs an overlay all the time anyway. We'll switch into
2563 // |always_enable_overlays_| mode below.
2564 decoder_requires_restart_for_overlay_ =
2565 (overlay_mode_ == OverlayMode::kUseAndroidOverlay && is_encrypted_)
2566 ? false
2567 : decoder_requires_restart_for_overlay;
liberato2ff93ad2017-05-17 07:28:242568 provide_overlay_info_cb_ = provide_overlay_info_cb;
dalecurtis4b632fce22016-11-10 00:52:172569
Chris Watkins557f84d2017-09-16 02:31:462570 // If the decoder doesn't require restarts for surface transitions, and we're
2571 // using AndroidOverlay mode, we can always enable the overlay and the decoder
2572 // can choose whether or not to use it. Otherwise, we'll restart the decoder
2573 // and enable the overlay on fullscreen transitions.
[email protected]77568482017-06-21 21:16:522574 if (overlay_mode_ == OverlayMode::kUseAndroidOverlay &&
Chris Watkins557f84d2017-09-16 02:31:462575 !decoder_requires_restart_for_overlay_) {
[email protected]c8d574722017-08-30 20:53:432576 always_enable_overlays_ = true;
[email protected]77568482017-06-21 21:16:522577 if (!overlay_enabled_)
2578 EnableOverlay();
2579 }
2580
Chris Watkins557f84d2017-09-16 02:31:462581 // Send the overlay info if we already have it. If not, it will be sent later.
liberato2ff93ad2017-05-17 07:28:242582 MaybeSendOverlayInfoToDecoder();
2583}
2584
2585void WebMediaPlayerImpl::MaybeSendOverlayInfoToDecoder() {
2586 // If the decoder didn't request overlay info, then don't send it.
2587 if (!provide_overlay_info_cb_)
dalecurtis4b632fce22016-11-10 00:52:172588 return;
2589
liberato2ff93ad2017-05-17 07:28:242590 // We should send the overlay info as long as we know it. This includes the
2591 // case where |!overlay_enabled_|, since we want to tell the decoder to avoid
2592 // using overlays. Assuming that the decoder has requested info, the only
2593 // case in which we don't want to send something is if we've requested the
2594 // info but not received it yet. Then, we should wait until we do.
liberatofe8f9692017-06-08 19:17:362595 //
2596 // Initialization requires this; AVDA should start with enough info to make an
2597 // overlay, so that (pre-M) the initial codec is created with the right output
2598 // surface; it can't switch later.
[email protected]f7df5b342018-07-13 20:22:132599 if (overlay_mode_ == OverlayMode::kUseAndroidOverlay) {
liberatofe8f9692017-06-08 19:17:362600 if (overlay_routing_token_is_pending_)
liberato2ff93ad2017-05-17 07:28:242601 return;
liberatofe8f9692017-06-08 19:17:362602
2603 overlay_info_.routing_token = overlay_routing_token_;
liberato2ff93ad2017-05-17 07:28:242604 }
2605
liberato2ff93ad2017-05-17 07:28:242606 // If restart is required, the callback is one-shot only.
2607 if (decoder_requires_restart_for_overlay_) {
Dale Curtise25163812018-09-21 22:13:392608 std::move(provide_overlay_info_cb_).Run(overlay_info_);
liberato2ff93ad2017-05-17 07:28:242609 } else {
liberatofe8f9692017-06-08 19:17:362610 provide_overlay_info_cb_.Run(overlay_info_);
liberato2ff93ad2017-05-17 07:28:242611 }
watkdee516f2016-02-18 02:22:192612}
2613
Xiaohan Wangcd4df0a2019-12-04 20:59:242614std::unique_ptr<Renderer> WebMediaPlayerImpl::CreateRenderer(
Xiaohan Wang48aaa192019-12-05 01:29:002615 base::Optional<RendererFactoryType> factory_type) {
dcheng37b415b92017-01-27 20:17:432616 DCHECK(main_task_runner_->BelongsToCurrentThread());
2617
[email protected]c8d574722017-08-30 20:53:432618 // Make sure that overlays are enabled if they're always allowed.
2619 if (always_enable_overlays_)
tsunghungee562e92016-07-20 18:03:312620 EnableOverlay();
2621
liberato2ff93ad2017-05-17 07:28:242622 RequestOverlayInfoCB request_overlay_info_cb;
watkdee516f2016-02-18 02:22:192623#if defined(OS_ANDROID)
liberato2ff93ad2017-05-17 07:28:242624 request_overlay_info_cb = BindToCurrentLoop(
Antonio Gomes142f32a2019-05-15 23:32:562625 base::Bind(&WebMediaPlayerImpl::OnOverlayInfoRequested, weak_this_));
watkdee516f2016-02-18 02:22:192626#endif
Xiaohan Wangcd4df0a2019-12-04 20:59:242627
2628 if (factory_type) {
Xiaohan Wang48aaa192019-12-05 01:29:002629 DVLOG(1) << __func__
2630 << ": factory_type=" << static_cast<int>(factory_type.value());
Xiaohan Wangcd4df0a2019-12-04 20:59:242631 renderer_factory_selector_->SetBaseFactoryType(factory_type.value());
2632 }
2633
Dan Sanders6edfd782019-08-13 00:13:182634 reported_renderer_type_ = renderer_factory_selector_->GetCurrentFactoryType();
Xiaohan Wang73b433f2019-11-05 20:13:012635
Xiaohan Wang2480de72019-11-15 01:44:472636 return renderer_factory_selector_->GetCurrentFactory()->CreateRenderer(
2637 media_task_runner_, worker_task_runner_, audio_source_provider_.get(),
2638 compositor_.get(), request_overlay_info_cb, client_->TargetColorSpace());
sandersd1e49fb62015-12-12 01:18:062639}
2640
[email protected]ef8394c2013-08-21 20:26:302641void WebMediaPlayerImpl::StartPipeline() {
acolwellb4034942014-08-28 15:42:432642 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]ddbc6ff2013-04-19 15:28:332643
xhwange8c4181a2014-12-06 08:10:012644 Demuxer::EncryptedMediaInitDataCB encrypted_media_init_data_cb =
dcheng37b415b92017-01-27 20:17:432645 BindToCurrentLoop(base::Bind(
Antonio Gomes142f32a2019-05-15 23:32:562646 &WebMediaPlayerImpl::OnEncryptedMediaInitData, weak_this_));
[email protected]2b57e2e2014-05-09 11:07:252647
Dale Curtis3899090ea2018-01-12 00:10:352648 vfc_task_runner_->PostTask(
2649 FROM_HERE,
2650 base::BindOnce(&VideoFrameCompositor::SetOnNewProcessedFrameCallback,
2651 base::Unretained(compositor_.get()),
2652 BindToCurrentLoop(base::BindOnce(
Antonio Gomes142f32a2019-05-15 23:32:562653 &WebMediaPlayerImpl::OnFirstFrame, weak_this_))));
Dale Curtis3899090ea2018-01-12 00:10:352654
Thomas Guilbert55b48d92019-03-25 23:16:542655#if defined(OS_ANDROID)
Dale Curtis65c151a2019-02-26 02:09:322656 if (demuxer_found_hls_ ||
2657 renderer_factory_selector_->GetCurrentFactory()
2658 ->GetRequiredMediaResourceType() == MediaResource::Type::URL) {
Thomas Guilbert8a42fcf2019-03-01 20:21:452659 // MediaPlayerRendererClientFactory is the only factory that a uses
2660 // MediaResource::Type::URL for the moment.
tguilbert75e2bf62017-04-26 20:13:122661 using_media_player_renderer_ = true;
2662
Chris Cunninghamd9df58e2017-08-29 00:04:232663 // MediaPlayerRenderer does not provide pipeline stats, so nuke capabilities
2664 // reporter.
2665 video_decode_stats_reporter_.reset();
2666
Thomas Guilbert55b48d92019-03-25 23:16:542667 demuxer_.reset(new MediaUrlDemuxer(
Maks Orlovichab27e242020-01-07 18:10:392668 media_task_runner_, loaded_url_,
2669 frame_->GetDocument().SiteForCookies().RepresentativeUrl(),
Christian Dullweberacff9bd32019-08-20 09:44:372670 frame_->GetDocument().TopFrameOrigin(),
Dan Sanders3ff9ffe2019-07-19 22:24:122671 allow_media_player_renderer_credentials_, demuxer_found_hls_));
Antonio Gomes81b56552019-05-15 22:15:282672 pipeline_controller_->Start(Pipeline::StartType::kNormal, demuxer_.get(),
2673 this, false, false);
tguilbert25a4d112016-10-13 21:56:512674 return;
2675 }
Thomas Guilbert55b48d92019-03-25 23:16:542676#endif // defined(OS_ANDROID)
tguilbert25a4d112016-10-13 21:56:512677
[email protected]ddbc6ff2013-04-19 15:28:332678 // Figure out which demuxer to use.
Blink Reformat1c4d759e2017-04-09 16:34:542679 if (load_type_ != kLoadTypeMediaSource) {
[email protected]ddbc6ff2013-04-19 15:28:332680 DCHECK(!chunk_demuxer_);
[email protected]f5443ef72013-04-22 04:03:382681 DCHECK(data_source_);
2682
Dale Curtisbcf523b2018-01-17 02:59:012683#if BUILDFLAG(ENABLE_FFMPEG)
servolk81e01e02016-03-05 03:29:152684 Demuxer::MediaTracksUpdatedCB media_tracks_updated_cb =
dcheng37b415b92017-01-27 20:17:432685 BindToCurrentLoop(base::Bind(
Antonio Gomes142f32a2019-05-15 23:32:562686 &WebMediaPlayerImpl::OnFFmpegMediaTracksUpdated, weak_this_));
servolk81e01e02016-03-05 03:29:152687
dalecurtis9cddc0b2017-04-19 21:23:382688 demuxer_.reset(new FFmpegDemuxer(
2689 media_task_runner_, data_source_.get(), encrypted_media_init_data_cb,
Dale Curtisb8139f72018-08-27 23:28:482690 media_tracks_updated_cb, media_log_.get(), IsLocalFile(loaded_url_)));
j.isorcef6778e652015-11-16 17:14:252691#else
alokp967c902452016-05-06 05:21:372692 OnError(PipelineStatus::DEMUXER_ERROR_COULD_NOT_OPEN);
j.isorcef6778e652015-11-16 17:14:252693 return;
2694#endif
[email protected]ddbc6ff2013-04-19 15:28:332695 } else {
[email protected]f5443ef72013-04-22 04:03:382696 DCHECK(!chunk_demuxer_);
2697 DCHECK(!data_source_);
2698
Antonio Gomes142f32a2019-05-15 23:32:562699 chunk_demuxer_ =
2700 new ChunkDemuxer(BindToCurrentLoop(base::Bind(
2701 &WebMediaPlayerImpl::OnDemuxerOpened, weak_this_)),
2702 BindToCurrentLoop(base::Bind(
2703 &WebMediaPlayerImpl::OnProgress, weak_this_)),
2704 encrypted_media_init_data_cb, media_log_.get());
[email protected]f5443ef72013-04-22 04:03:382705 demuxer_.reset(chunk_demuxer_);
servolkf94b4602017-01-31 16:44:272706
2707 if (base::FeatureList::IsEnabled(kMemoryPressureBasedSourceBufferGC)) {
2708 // base::Unretained is safe because |this| owns memory_pressure_listener_.
2709 memory_pressure_listener_ =
Gyuyoung Kim62a5de42018-01-10 09:48:422710 std::make_unique<base::MemoryPressureListener>(base::Bind(
servolkf94b4602017-01-31 16:44:272711 &WebMediaPlayerImpl::OnMemoryPressure, base::Unretained(this)));
2712 }
[email protected]ddbc6ff2013-04-19 15:28:332713 }
2714
sandersdb5e21462016-03-09 01:49:072715 // TODO(sandersd): FileSystem objects may also be non-static, but due to our
2716 // caching layer such situations are broken already. http://crbug.com/593159
2717 bool is_static = !chunk_demuxer_;
avayvod102cdb62017-01-07 03:11:092718 bool is_streaming = IsStreaming();
sandersdb8eb5f1d2016-11-19 04:04:022719 UMA_HISTOGRAM_BOOLEAN("Media.IsStreaming", is_streaming);
sandersdb5e21462016-03-09 01:49:072720
Dale Curtis2dc6089a2018-03-26 16:47:582721 // If possible attempt to avoid decoder spool up until playback starts.
2722 Pipeline::StartType start_type = Pipeline::StartType::kNormal;
Dale Curtis7c63f2e22018-09-19 21:06:272723 if (!chunk_demuxer_ && preload_ == MultibufferDataSource::METADATA &&
Dale Curtisd6ccd1992019-04-29 19:03:462724 !client_->CouldPlayIfEnoughData() && !IsStreaming()) {
Dale Curtis7c63f2e22018-09-19 21:06:272725 start_type =
2726 (has_poster_ || base::FeatureList::IsEnabled(kPreloadMetadataLazyLoad))
2727 ? Pipeline::StartType::kSuspendAfterMetadata
2728 : Pipeline::StartType::kSuspendAfterMetadataForAudioOnly;
Dale Curtis2dc6089a2018-03-26 16:47:582729 attempting_suspended_start_ = true;
2730 }
2731
[email protected]f5443ef72013-04-22 04:03:382732 // ... and we're ready to go!
sandersd1e49fb62015-12-12 01:18:062733 // TODO(sandersd): On Android, defer Start() if the tab is not visible.
sandersdb8eb5f1d2016-11-19 04:04:022734 seeking_ = true;
Antonio Gomes81b56552019-05-15 22:15:282735 pipeline_controller_->Start(start_type, demuxer_.get(), this, is_streaming,
2736 is_static);
[email protected]f5443ef72013-04-22 04:03:382737}
2738
2739void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state) {
pkastingf5279482016-07-27 02:18:202740 DVLOG(1) << __func__ << "(" << state << ")";
acolwellb4034942014-08-28 15:42:432741 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]f5443ef72013-04-22 04:03:382742 network_state_ = state;
2743 // Always notify to ensure client has the latest value.
Blink Reformat1c4d759e2017-04-09 16:34:542744 client_->NetworkStateChanged();
[email protected]f5443ef72013-04-22 04:03:382745}
2746
2747void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state) {
pkastingf5279482016-07-27 02:18:202748 DVLOG(1) << __func__ << "(" << state << ")";
acolwellb4034942014-08-28 15:42:432749 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]f5443ef72013-04-22 04:03:382750
Blink Reformat1c4d759e2017-04-09 16:34:542751 if (state == WebMediaPlayer::kReadyStateHaveEnoughData && data_source_ &&
Dale Curtis4841c712018-12-13 18:14:052752 data_source_->AssumeFullyBuffered() &&
2753 network_state_ == WebMediaPlayer::kNetworkStateLoading) {
Blink Reformat1c4d759e2017-04-09 16:34:542754 SetNetworkState(WebMediaPlayer::kNetworkStateLoaded);
Dale Curtis4841c712018-12-13 18:14:052755 }
[email protected]f5443ef72013-04-22 04:03:382756
2757 ready_state_ = state;
sandersd50a635e2016-04-04 22:50:092758 highest_ready_state_ = std::max(highest_ready_state_, ready_state_);
2759
[email protected]f5443ef72013-04-22 04:03:382760 // Always notify to ensure client has the latest value.
Blink Reformat1c4d759e2017-04-09 16:34:542761 client_->ReadyStateChanged();
[email protected]f5443ef72013-04-22 04:03:382762}
2763
Antonio Gomesa38bb7cb2019-05-31 03:02:522764scoped_refptr<blink::WebAudioSourceProviderImpl>
2765WebMediaPlayerImpl::GetAudioSourceProvider() {
2766 return audio_source_provider_;
[email protected]f5443ef72013-04-22 04:03:382767}
2768
Jiajia Qin82acdc02017-07-31 09:55:142769scoped_refptr<VideoFrame> WebMediaPlayerImpl::GetCurrentFrameFromCompositor()
2770 const {
xhwang213e50c2016-10-10 23:56:312771 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]dd061e12014-05-06 19:21:222772 TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor");
xhwang213e50c2016-10-10 23:56:312773
Thomas Guilberte90fe312019-11-07 22:27:562774 if (current_frame_override_)
2775 return current_frame_override_;
2776
Thomas Guilbertd85407412017-11-08 05:03:462777 // Can be null.
2778 scoped_refptr<VideoFrame> video_frame =
2779 compositor_->GetCurrentFrameOnAnyThread();
[email protected]dd061e12014-05-06 19:21:222780
Thomas Guilbertd85407412017-11-08 05:03:462781 // base::Unretained is safe here because |compositor_| is destroyed on
2782 // |vfc_task_runner_|. The destruction is queued from |this|' destructor,
2783 // which also runs on |main_task_runner_|, which makes it impossible for
2784 // UpdateCurrentFrameIfStale() to be queued after |compositor_|'s dtor.
CJ DiMeglio2302d202017-08-31 08:38:042785 vfc_task_runner_->PostTask(
Dale Curtisee9be30012018-09-21 22:21:512786 FROM_HERE,
2787 base::BindOnce(&VideoFrameCompositor::UpdateCurrentFrameIfStale,
2788 base::Unretained(compositor_.get())));
kainino36eeff82017-03-30 00:55:302789
[email protected]dd061e12014-05-06 19:21:222790 return video_frame;
2791}
2792
sandersd50a635e2016-04-04 22:50:092793void WebMediaPlayerImpl::UpdatePlayState() {
xhwang213e50c2016-10-10 23:56:312794 DCHECK(main_task_runner_->BelongsToCurrentThread());
hubbee2cc88c092017-07-14 23:10:412795 bool can_auto_suspend = !disable_pipeline_auto_suspend_;
2796 // For streaming videos, we only allow suspending at the very beginning of the
2797 // video, and only if we know the length of the video. (If we don't know
2798 // the length, it might be a dynamically generated video, and suspending
2799 // will not work at all.)
2800 if (IsStreaming()) {
2801 bool at_beginning =
2802 ready_state_ == WebMediaPlayer::kReadyStateHaveNothing ||
2803 CurrentTime() == 0.0;
2804 if (!at_beginning || GetPipelineMediaDuration() == kInfiniteDuration)
2805 can_auto_suspend = false;
2806 }
xhwang213e50c2016-10-10 23:56:312807
Antonio Gomes81b56552019-05-15 22:15:282808 bool is_suspended = pipeline_controller_->IsSuspended();
Sergey Volk8b09c2c52018-12-12 23:20:402809 bool is_backgrounded = IsBackgroundSuspendEnabled(this) && IsHidden();
sandersdaaff1a652016-11-17 01:47:252810 PlayState state = UpdatePlayState_ComputePlayState(
Thomas Guilbert7350fdad2019-01-29 23:09:242811 is_flinging_, can_auto_suspend, is_suspended, is_backgrounded);
sandersd35d2c3f2017-01-14 02:04:422812 SetDelegateState(state.delegate_state, state.is_idle);
sandersd50a635e2016-04-04 22:50:092813 SetMemoryReportingState(state.is_memory_reporting_enabled);
2814 SetSuspendState(state.is_suspended || pending_suspend_resume_cycle_);
[email protected]dc996312019-12-19 19:26:382815 if (power_status_helper_) {
2816 // Make sure that we're in something like steady-state before recording.
2817 power_status_helper_->SetIsPlaying(
2818 !paused_ && !seeking_ && !IsHidden() && !state.is_suspended &&
2819 ready_state_ == kReadyStateHaveEnoughData);
2820 }
sandersd50a635e2016-04-04 22:50:092821}
dalecurtis5bbc487e2016-02-27 04:15:052822
Becca Hughes7a85bf22019-07-24 16:16:582823void WebMediaPlayerImpl::UpdateMediaPositionState() {
2824 DCHECK(delegate_);
2825
2826 // When seeking the current time can go beyond the duration so we should
2827 // cap the current time at the duration.
2828 base::TimeDelta duration = GetPipelineMediaDuration();
2829 base::TimeDelta current_time = GetCurrentTimeInternal();
2830 if (current_time > duration)
2831 current_time = duration;
2832
2833 media_session::MediaPosition new_position(paused_ ? 0.0 : playback_rate_,
2834 duration, current_time);
2835
2836 if (media_position_state_ == new_position)
2837 return;
2838
2839 DVLOG(2) << __func__ << "(" << new_position.ToString() << ")";
2840 media_position_state_ = new_position;
2841 delegate_->DidPlayerMediaPositionStateChange(delegate_id_,
2842 media_position_state_);
2843}
2844
sandersd35d2c3f2017-01-14 02:04:422845void WebMediaPlayerImpl::SetDelegateState(DelegateState new_state,
2846 bool is_idle) {
tguilbert1bb1c782017-01-23 21:15:112847 DCHECK(delegate_);
Dale Curtis779ed842018-03-10 06:20:132848 DVLOG(2) << __func__ << "(" << static_cast<int>(new_state) << ", " << is_idle
2849 << ")";
dalecurtis5bbc487e2016-02-27 04:15:052850
sandersd35d2c3f2017-01-14 02:04:422851 // Prevent duplicate delegate calls.
2852 // TODO(sandersd): Move this deduplication into the delegate itself.
2853 // TODO(sandersd): WebContentsObserverSanityChecker does not allow sending the
2854 // 'playing' IPC more than once in a row, even if the metadata has changed.
2855 // Figure out whether it should.
Mounir Lamouri366dd8472018-06-19 17:20:042856 // Pretend that the media has no audio if it never played unmuted. This is to
2857 // avoid any action related to audible media such as taking audio focus or
2858 // showing a media notification. To preserve a consistent experience, it does
2859 // not apply if a media was audible so the system states do not flicker
2860 // depending on whether the user muted the player.
2861 bool has_audio = HasAudio() && !client_->WasAlwaysMuted();
sandersd35d2c3f2017-01-14 02:04:422862 if (delegate_state_ == new_state &&
2863 (delegate_state_ != DelegateState::PLAYING ||
2864 delegate_has_audio_ == has_audio)) {
2865 return;
mlamouri910111362016-11-04 11:28:242866 }
sandersd50a635e2016-04-04 22:50:092867 delegate_state_ = new_state;
sandersd35d2c3f2017-01-14 02:04:422868 delegate_has_audio_ = has_audio;
sandersd50a635e2016-04-04 22:50:092869
sandersd35d2c3f2017-01-14 02:04:422870 switch (new_state) {
sandersd50a635e2016-04-04 22:50:092871 case DelegateState::GONE:
2872 delegate_->PlayerGone(delegate_id_);
2873 break;
mlamouri910111362016-11-04 11:28:242874 case DelegateState::PLAYING: {
peconn257951522017-06-09 18:24:592875 if (HasVideo())
2876 delegate_->DidPlayerSizeChange(delegate_id_, NaturalSize());
zqzhang5d8eab72016-08-26 20:34:302877 delegate_->DidPlay(
Blink Reformat1c4d759e2017-04-09 16:34:542878 delegate_id_, HasVideo(), has_audio,
Dale Curtisca1b78f2019-01-15 03:11:262879 DurationToMediaContentType(GetPipelineMediaDuration()));
sandersd50a635e2016-04-04 22:50:092880 break;
mlamouri910111362016-11-04 11:28:242881 }
sandersd50a635e2016-04-04 22:50:092882 case DelegateState::PAUSED:
sandersd35d2c3f2017-01-14 02:04:422883 delegate_->DidPause(delegate_id_);
sandersd50a635e2016-04-04 22:50:092884 break;
dalecurtis0f0097a2015-12-01 17:40:472885 }
sandersd35d2c3f2017-01-14 02:04:422886
2887 delegate_->SetIdle(delegate_id_, is_idle);
dalecurtis0f0097a2015-12-01 17:40:472888}
2889
sandersd50a635e2016-04-04 22:50:092890void WebMediaPlayerImpl::SetMemoryReportingState(
2891 bool is_memory_reporting_enabled) {
2892 if (memory_usage_reporting_timer_.IsRunning() ==
2893 is_memory_reporting_enabled) {
hubbed5f36882016-01-15 22:40:372894 return;
sandersd50a635e2016-04-04 22:50:092895 }
sandersd1c0bba02016-03-04 23:14:082896
sandersd50a635e2016-04-04 22:50:092897 if (is_memory_reporting_enabled) {
2898 memory_usage_reporting_timer_.Start(FROM_HERE,
2899 base::TimeDelta::FromSeconds(2), this,
2900 &WebMediaPlayerImpl::ReportMemoryUsage);
2901 } else {
2902 memory_usage_reporting_timer_.Stop();
2903 ReportMemoryUsage();
2904 }
2905}
2906
2907void WebMediaPlayerImpl::SetSuspendState(bool is_suspended) {
xhwang213e50c2016-10-10 23:56:312908 DCHECK(main_task_runner_->BelongsToCurrentThread());
Dale Curtis779ed842018-03-10 06:20:132909 DVLOG(2) << __func__ << "(" << is_suspended << ")";
xhwang213e50c2016-10-10 23:56:312910
sandersd50a635e2016-04-04 22:50:092911 // Do not change the state after an error has occurred.
2912 // TODO(sandersd): Update PipelineController to remove the need for this.
2913 if (IsNetworkStateError(network_state_))
sandersd1c0bba02016-03-04 23:14:082914 return;
2915
jameswest451a5bb2017-01-27 03:59:392916 if (is_suspended) {
sandersd35d2c3f2017-01-14 02:04:422917 // If we were not resumed for long enough to satisfy the preroll attempt,
2918 // reset the clock.
2919 if (!preroll_attempt_pending_ && IsPrerollAttemptNeeded()) {
2920 preroll_attempt_pending_ = true;
2921 preroll_attempt_start_time_ = base::TimeTicks();
2922 }
Antonio Gomes81b56552019-05-15 22:15:282923 pipeline_controller_->Suspend();
sandersd50a635e2016-04-04 22:50:092924 } else {
sandersd35d2c3f2017-01-14 02:04:422925 // When resuming, start the preroll attempt clock.
2926 if (preroll_attempt_pending_) {
2927 preroll_attempt_pending_ = false;
2928 preroll_attempt_start_time_ = tick_clock_->NowTicks();
2929 }
Antonio Gomes81b56552019-05-15 22:15:282930 pipeline_controller_->Resume();
sandersd50a635e2016-04-04 22:50:092931 }
2932}
2933
2934WebMediaPlayerImpl::PlayState
Thomas Guilbert7350fdad2019-01-29 23:09:242935WebMediaPlayerImpl::UpdatePlayState_ComputePlayState(bool is_flinging,
xjz4e5d4bf32017-02-15 21:26:352936 bool can_auto_suspend,
dalecurtis8b8505e72016-06-10 21:59:172937 bool is_suspended,
sandersd50a635e2016-04-04 22:50:092938 bool is_backgrounded) {
2939 PlayState result;
2940
tguilbert1bb1c782017-01-23 21:15:112941 bool must_suspend = delegate_->IsFrameClosed();
Dale Curtis6438cf12018-03-29 02:34:012942 bool is_stale = delegate_->IsStale(delegate_id_);
2943
2944 if (stale_state_override_for_testing_.has_value() &&
2945 ready_state_ >= stale_state_override_for_testing_.value()) {
2946 is_stale = true;
2947 }
sandersd35d2c3f2017-01-14 02:04:422948
sandersd50a635e2016-04-04 22:50:092949 // This includes both data source (before pipeline startup) and pipeline
2950 // errors.
2951 bool has_error = IsNetworkStateError(network_state_);
2952
Dale Curtisc56158c2019-09-09 23:00:322953 // Note: Even though we get play/pause signals at kReadyStateHaveMetadata, we
2954 // must attempt to preroll until kReadyStateHaveFutureData so that the
2955 // canplaythrough event will be fired to the page (which may be waiting).
2956 bool have_future_data =
2957 highest_ready_state_ >= WebMediaPlayer::kReadyStateHaveFutureData;
sandersd50a635e2016-04-04 22:50:092958
avayvod65fad272017-02-24 01:00:482959 // Background suspend is only enabled for paused players.
2960 // In the case of players with audio the session should be kept.
2961 bool background_suspended =
Dale Curtisc56158c2019-09-09 23:00:322962 can_auto_suspend && is_backgrounded && paused_ && have_future_data;
sandersd50a635e2016-04-04 22:50:092963
Dale Curtisd71061f02019-05-21 21:33:542964 // Idle suspension is allowed prior to kReadyStateHaveMetadata since there
2965 // exist mechanisms to exit the idle state when the player is capable of
2966 // reaching the kReadyStateHaveMetadata state; see didLoadingProgress().
dalecurtiscc8baf72016-10-27 01:49:442967 //
sandersd50a635e2016-04-04 22:50:092968 // TODO(sandersd): Make the delegate suspend idle players immediately when
2969 // hidden.
Dale Curtis7c63f2e22018-09-19 21:06:272970 bool idle_suspended = can_auto_suspend && is_stale && paused_ && !seeking_ &&
2971 !overlay_enabled_ && !needs_first_frame_;
dalecurtise7120dc2016-09-03 02:54:352972
2973 // If we're already suspended, see if we can wait for user interaction. Prior
Dale Curtisd71061f02019-05-21 21:33:542974 // to kReadyStateHaveMetadata, we require |is_stale| to remain suspended.
2975 // |is_stale| will be cleared when we receive data which may take us to
2976 // kReadyStateHaveMetadata.
Dale Curtisc56158c2019-09-09 23:00:322977 bool can_stay_suspended = (is_stale || have_future_data) && is_suspended &&
Dale Curtis7c63f2e22018-09-19 21:06:272978 paused_ && !seeking_ && !needs_first_frame_;
sandersd50a635e2016-04-04 22:50:092979
2980 // Combined suspend state.
Thomas Guilbert7350fdad2019-01-29 23:09:242981 result.is_suspended = must_suspend || idle_suspended ||
avayvod65fad272017-02-24 01:00:482982 background_suspended || can_stay_suspended;
sandersd50a635e2016-04-04 22:50:092983
Thomas Guilbert7350fdad2019-01-29 23:09:242984 DVLOG(3) << __func__ << ": must_suspend=" << must_suspend
Dale Curtis779ed842018-03-10 06:20:132985 << ", idle_suspended=" << idle_suspended
2986 << ", background_suspended=" << background_suspended
2987 << ", can_stay_suspended=" << can_stay_suspended
Dale Curtisc56158c2019-09-09 23:00:322988 << ", is_stale=" << is_stale
2989 << ", have_future_data=" << have_future_data
Dale Curtis779ed842018-03-10 06:20:132990 << ", paused_=" << paused_ << ", seeking_=" << seeking_;
2991
sandersd50a635e2016-04-04 22:50:092992 // We do not treat |playback_rate_| == 0 as paused. For the media session,
2993 // being paused implies displaying a play button, which is incorrect in this
2994 // case. For memory usage reporting, we just use the same definition (but we
2995 // don't have to).
2996 //
2997 // Similarly, we don't consider |ended_| to be paused. Blink will immediately
2998 // call pause() or seek(), so |ended_| should not affect the computation.
2999 // Despite that, |ended_| does result in a separate paused state, to simplfy
3000 // the contract for SetDelegateState().
3001 //
avayvod65fad272017-02-24 01:00:483002 // |has_remote_controls| indicates if the player can be controlled outside the
3003 // page (e.g. via the notification controls or by audio focus events). Idle
sandersd50a635e2016-04-04 22:50:093004 // suspension does not destroy the media session, because we expect that the
avayvod5f34b642017-03-23 03:14:043005 // notification controls (and audio focus) remain. With some exceptions for
3006 // background videos, the player only needs to have audio to have controls
Dale Curtisd71061f02019-05-21 21:33:543007 // (requires |have_current_data|).
avayvod5f34b642017-03-23 03:14:043008 //
3009 // |alive| indicates if the player should be present (not |GONE|) to the
3010 // delegate, either paused or playing. The following must be true for the
3011 // player:
Dale Curtisd71061f02019-05-21 21:33:543012 // - |have_current_data|, since playback can't begin before that point, we
3013 // need to know whether we are paused to correctly configure the session,
3014 // and also because the tracks and duration are passed to DidPlay(),
Thomas Guilbert7350fdad2019-01-29 23:09:243015 // - |is_flinging| is false (RemotePlayback is not handled by the delegate)
avayvod5f34b642017-03-23 03:14:043016 // - |has_error| is false as player should have no errors,
3017 // - |background_suspended| is false, otherwise |has_remote_controls| must
3018 // be true.
sandersd50a635e2016-04-04 22:50:093019 //
avayvod65fad272017-02-24 01:00:483020 // TODO(sandersd): If Blink told us the paused state sooner, we could detect
3021 // if the remote controls are available sooner.
3022
3023 // Background videos with audio don't have remote controls if background
3024 // suspend is enabled and resuming background videos is not (original Android
3025 // behavior).
3026 bool backgrounded_video_has_no_remote_controls =
Sergey Volk8b09c2c52018-12-12 23:20:403027 IsBackgroundSuspendEnabled(this) && !IsResumeBackgroundVideosEnabled() &&
3028 is_backgrounded && HasVideo();
Dale Curtisd71061f02019-05-21 21:33:543029 bool have_current_data = highest_ready_state_ >= kReadyStateHaveCurrentData;
3030 bool can_play = !has_error && have_current_data;
avayvod5f34b642017-03-23 03:14:043031 bool has_remote_controls =
Blink Reformat1c4d759e2017-04-09 16:34:543032 HasAudio() && !backgrounded_video_has_no_remote_controls;
Thomas Guilbert7350fdad2019-01-29 23:09:243033 bool alive = can_play && !is_flinging && !must_suspend &&
avayvod5f34b642017-03-23 03:14:043034 (!background_suspended || has_remote_controls);
3035 if (!alive) {
Thomas Guilbert4c6feff2018-11-09 19:53:323036 // Do not mark players as idle when flinging.
sandersd50a635e2016-04-04 22:50:093037 result.delegate_state = DelegateState::GONE;
Thomas Guilbert4c6feff2018-11-09 19:53:323038 result.is_idle = delegate_->IsIdle(delegate_id_) && !is_flinging;
avayvod65fad272017-02-24 01:00:483039 } else if (paused_) {
sandersd35d2c3f2017-01-14 02:04:423040 // TODO(sandersd): Is it possible to have a suspended session, be ended,
3041 // and not be paused? If so we should be in a PLAYING state.
dalecurtise7120dc2016-09-03 02:54:353042 result.delegate_state =
sandersd35d2c3f2017-01-14 02:04:423043 ended_ ? DelegateState::GONE : DelegateState::PAUSED;
3044 result.is_idle = !seeking_;
sandersd50a635e2016-04-04 22:50:093045 } else {
3046 result.delegate_state = DelegateState::PLAYING;
sandersd35d2c3f2017-01-14 02:04:423047 result.is_idle = false;
sandersd50a635e2016-04-04 22:50:093048 }
3049
dalecurtis8b8505e72016-06-10 21:59:173050 // It's not critical if some cases where memory usage can change are missed,
sandersd50a635e2016-04-04 22:50:093051 // since media memory changes are usually gradual.
Thomas Guilbert7350fdad2019-01-29 23:09:243052 result.is_memory_reporting_enabled = !has_error && can_play && !is_flinging &&
3053 !result.is_suspended &&
3054 (!paused_ || seeking_);
sandersd50a635e2016-04-04 22:50:093055
3056 return result;
dalecurtis0f0097a2015-12-01 17:40:473057}
3058
dalecurtis83266c72015-10-29 18:43:203059void WebMediaPlayerImpl::ReportMemoryUsage() {
3060 DCHECK(main_task_runner_->BelongsToCurrentThread());
3061
wdzierzanowskifd4cd91c52015-12-02 23:50:203062 // About base::Unretained() usage below: We destroy |demuxer_| on the main
3063 // thread. Before that, however, ~WebMediaPlayerImpl() posts a task to the
3064 // media thread and waits for it to finish. Hence, the GetMemoryUsage() task
3065 // posted here must finish earlier.
Dale Curtis8a6281322017-08-31 00:35:533066 //
3067 // The exception to the above is when OnError() has been called. If we're in
3068 // the error state we've already shut down the pipeline and can't rely on it
3069 // to cycle the media thread before we destroy |demuxer_|. In this case skip
3070 // collection of the demuxer memory stats.
3071 if (demuxer_ && !IsNetworkStateError(network_state_)) {
wdzierzanowskifd4cd91c52015-12-02 23:50:203072 base::PostTaskAndReplyWithResult(
3073 media_task_runner_.get(), FROM_HERE,
3074 base::Bind(&Demuxer::GetMemoryUsage, base::Unretained(demuxer_.get())),
Antonio Gomes142f32a2019-05-15 23:32:563075 base::Bind(&WebMediaPlayerImpl::FinishMemoryUsageReport, weak_this_));
wdzierzanowskifd4cd91c52015-12-02 23:50:203076 } else {
3077 FinishMemoryUsageReport(0);
3078 }
3079}
3080
3081void WebMediaPlayerImpl::FinishMemoryUsageReport(int64_t demuxer_memory_usage) {
3082 DCHECK(main_task_runner_->BelongsToCurrentThread());
3083
avayvodc4bfb0e62017-01-13 01:07:013084 const PipelineStatistics stats = GetPipelineStatistics();
servolk639473e492016-12-15 04:14:203085 const int64_t data_source_memory_usage =
3086 data_source_ ? data_source_->GetMemoryUsage() : 0;
dalecurtisecc76612017-04-19 00:31:203087
Dale Curtisc2c5dcb12018-04-16 23:21:293088 // If we have video and no video memory usage and we've rendered the first
3089 // frame, assume the VideoFrameCompositor is holding onto the last frame after
3090 // we've suspended the pipeline; which thus reports zero memory usage from the
3091 // video renderer.
dalecurtisecc76612017-04-19 00:31:203092 //
3093 // Technically this should use the coded size, but that requires us to hop to
3094 // the compositor to get and byte-perfect accuracy isn't important here.
3095 const int64_t video_memory_usage =
3096 stats.video_memory_usage +
Dale Curtisc2c5dcb12018-04-16 23:21:293097 ((pipeline_metadata_.has_video && !stats.video_memory_usage &&
3098 has_first_frame_)
Miguel Casas9e7766022018-01-08 16:13:133099 ? VideoFrame::AllocationSize(PIXEL_FORMAT_I420,
dalecurtisecc76612017-04-19 00:31:203100 pipeline_metadata_.natural_size)
3101 : 0);
3102
dalecurtis83266c72015-10-29 18:43:203103 const int64_t current_memory_usage =
dalecurtisecc76612017-04-19 00:31:203104 stats.audio_memory_usage + video_memory_usage + data_source_memory_usage +
3105 demuxer_memory_usage;
dalecurtis83266c72015-10-29 18:43:203106
dalecurtisecc76612017-04-19 00:31:203107 DVLOG(2) << "Memory Usage -- Total: " << current_memory_usage
3108 << " Audio: " << stats.audio_memory_usage
3109 << ", Video: " << video_memory_usage
servolk639473e492016-12-15 04:14:203110 << ", DataSource: " << data_source_memory_usage
wdzierzanowskifd4cd91c52015-12-02 23:50:203111 << ", Demuxer: " << demuxer_memory_usage;
dalecurtis83266c72015-10-29 18:43:203112
3113 const int64_t delta = current_memory_usage - last_reported_memory_usage_;
3114 last_reported_memory_usage_ = current_memory_usage;
3115 adjust_allocated_memory_cb_.Run(delta);
servolk639473e492016-12-15 04:14:203116
Blink Reformat1c4d759e2017-04-09 16:34:543117 if (HasAudio()) {
servolk639473e492016-12-15 04:14:203118 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Audio",
3119 stats.audio_memory_usage / 1024);
3120 }
Blink Reformat1c4d759e2017-04-09 16:34:543121 if (HasVideo()) {
servolk639473e492016-12-15 04:14:203122 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Video",
dalecurtisecc76612017-04-19 00:31:203123 video_memory_usage / 1024);
servolk639473e492016-12-15 04:14:203124 }
3125 if (data_source_) {
3126 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.DataSource",
3127 data_source_memory_usage / 1024);
3128 }
3129 if (demuxer_) {
3130 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Demuxer",
3131 demuxer_memory_usage / 1024);
3132 }
dalecurtis83266c72015-10-29 18:43:203133}
3134
dalecurtis8b8505e72016-06-10 21:59:173135void WebMediaPlayerImpl::ScheduleIdlePauseTimer() {
avayvod65fad272017-02-24 01:00:483136 // Only schedule the pause timer if we're not paused or paused but going to
avayvod52efd282017-03-07 21:13:043137 // resume when foregrounded, and are suspended and have audio.
3138 if ((paused_ && !paused_when_hidden_) ||
Antonio Gomes81b56552019-05-15 22:15:283139 !pipeline_controller_->IsSuspended() || !HasAudio()) {
dalecurtis8b8505e72016-06-10 21:59:173140 return;
avayvod52efd282017-03-07 21:13:043141 }
dalecurtis8b8505e72016-06-10 21:59:173142
3143#if defined(OS_ANDROID)
Thomas Guilbert7350fdad2019-01-29 23:09:243144 // Don't pause videos casted as part of RemotePlayback.
3145 if (is_flinging_)
dalecurtis8b8505e72016-06-10 21:59:173146 return;
3147#endif
3148
3149 // Idle timeout chosen arbitrarily.
3150 background_pause_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(5),
3151 this, &WebMediaPlayerImpl::OnPause);
3152}
3153
dalecurtis04bdb582016-08-17 22:15:233154void WebMediaPlayerImpl::CreateWatchTimeReporter() {
wdzierzanowskia78fa9b2017-06-13 18:12:103155 if (!HasVideo() && !HasAudio())
3156 return;
3157
Dan Sanders7ef5d0f2019-05-03 18:30:113158 // MediaPlayerRenderer does not know about tracks until playback starts.
3159 // Assume audio-only unless the natural size has been detected.
3160 bool has_video = pipeline_metadata_.has_video;
3161 if (using_media_player_renderer_) {
3162 has_video = !pipeline_metadata_.natural_size.IsEmpty();
3163 }
3164
dalecurtis04bdb582016-08-17 22:15:233165 // Create the watch time reporter and synchronize its initial state.
Dale Curtis1adbe6a2017-08-02 02:09:133166 watch_time_reporter_.reset(new WatchTimeReporter(
Dan Sanders7ef5d0f2019-05-03 18:30:113167 mojom::PlaybackProperties::New(
3168 pipeline_metadata_.has_audio, has_video, false, false,
3169 !!chunk_demuxer_, is_encrypted_, embedded_media_experience_enabled_),
Dale Curtis96d6e9382018-07-18 18:01:073170 pipeline_metadata_.natural_size,
Dale Curtis051fdf62017-08-05 00:21:133171 base::BindRepeating(&WebMediaPlayerImpl::GetCurrentTimeInternal,
3172 base::Unretained(this)),
Dale Curtis0f8d7b32019-10-24 22:10:283173 base::BindRepeating(&WebMediaPlayerImpl::GetPipelineStatistics,
3174 base::Unretained(this)),
Hajime Hoshi8bb37dc2017-12-19 09:35:103175 media_metrics_provider_.get(),
Hajime Hoshib5a26ee2018-05-14 12:47:513176 frame_->GetTaskRunner(blink::TaskType::kInternalMedia)));
dalecurtis04bdb582016-08-17 22:15:233177 watch_time_reporter_->OnVolumeChange(volume_);
Dale Curtisaebaeea2018-07-19 23:42:113178 watch_time_reporter_->OnDurationChanged(GetPipelineMediaDuration());
Mounir Lamourif9af74e72017-06-19 19:31:453179
tguilbert1bb1c782017-01-23 21:15:113180 if (delegate_->IsFrameHidden())
dalecurtis04bdb582016-08-17 22:15:233181 watch_time_reporter_->OnHidden();
3182 else
3183 watch_time_reporter_->OnShown();
Mounir Lamourif9af74e72017-06-19 19:31:453184
Mounir Lamouri41a79c62017-06-06 12:53:163185 if (client_->HasNativeControls())
3186 watch_time_reporter_->OnNativeControlsEnabled();
3187 else
3188 watch_time_reporter_->OnNativeControlsDisabled();
Mounir Lamourif9af74e72017-06-19 19:31:453189
3190 switch (client_->DisplayType()) {
3191 case WebMediaPlayer::DisplayType::kInline:
3192 watch_time_reporter_->OnDisplayTypeInline();
3193 break;
3194 case WebMediaPlayer::DisplayType::kFullscreen:
3195 watch_time_reporter_->OnDisplayTypeFullscreen();
3196 break;
3197 case WebMediaPlayer::DisplayType::kPictureInPicture:
3198 watch_time_reporter_->OnDisplayTypePictureInPicture();
3199 break;
3200 }
Dale Curtis96d6e9382018-07-18 18:01:073201
3202 UpdateSecondaryProperties();
Dale Curtis7fd27c4b2018-07-30 22:14:213203
3204 // If the WatchTimeReporter was recreated in the middle of playback, we want
3205 // to resume playback here too since we won't get another play() call. When
3206 // seeking, the seek completion will restart it if necessary.
3207 if (!paused_ && !seeking_)
3208 watch_time_reporter_->OnPlaying();
Dale Curtis96d6e9382018-07-18 18:01:073209}
3210
3211void WebMediaPlayerImpl::UpdateSecondaryProperties() {
3212 watch_time_reporter_->UpdateSecondaryProperties(
3213 mojom::SecondaryPlaybackProperties::New(
3214 pipeline_metadata_.audio_decoder_config.codec(),
Dale Curtis7140b502019-10-23 20:34:423215 pipeline_metadata_.video_decoder_config.codec(),
Dale Curtisf8afcef32020-01-18 06:23:263216 pipeline_metadata_.audio_decoder_config.profile(),
Dale Curtis7140b502019-10-23 20:34:423217 pipeline_metadata_.video_decoder_config.profile(),
3218 audio_decoder_name_, video_decoder_name_,
Yuchen Liub33bfc12019-11-08 20:16:123219 pipeline_metadata_.audio_decoder_config.encryption_scheme(),
3220 pipeline_metadata_.video_decoder_config.encryption_scheme(),
John Rummelld30555352018-09-21 20:47:253221 pipeline_metadata_.natural_size));
dalecurtis04bdb582016-08-17 22:15:233222}
3223
avayvod39c102402016-11-23 21:43:133224bool WebMediaPlayerImpl::IsHidden() const {
3225 DCHECK(main_task_runner_->BelongsToCurrentThread());
3226
tguilbert1bb1c782017-01-23 21:15:113227 return delegate_->IsFrameHidden() && !delegate_->IsFrameClosed();
avayvod39c102402016-11-23 21:43:133228}
3229
avayvod102cdb62017-01-07 03:11:093230bool WebMediaPlayerImpl::IsStreaming() const {
3231 return data_source_ && data_source_->IsStreaming();
3232}
3233
liberato2fd111be2017-01-04 00:25:063234bool WebMediaPlayerImpl::DoesOverlaySupportMetadata() const {
Ted Meyer4a427632019-05-01 19:05:273235 return pipeline_metadata_.video_decoder_config.video_transformation() ==
3236 kNoTransformation;
liberato2fd111be2017-01-04 00:25:063237}
3238
xjzaf29d4182016-12-16 01:52:323239void WebMediaPlayerImpl::ActivateViewportIntersectionMonitoring(bool activate) {
3240 DCHECK(main_task_runner_->BelongsToCurrentThread());
3241
Blink Reformat1c4d759e2017-04-09 16:34:543242 client_->ActivateViewportIntersectionMonitoring(activate);
xjzaf29d4182016-12-16 01:52:323243}
3244
Anton Vayvod09fa66e2017-07-20 23:02:123245void WebMediaPlayerImpl::UpdateRemotePlaybackCompatibility(bool is_compatible) {
3246 DCHECK(main_task_runner_->BelongsToCurrentThread());
3247
3248 client_->RemotePlaybackCompatibilityChanged(loaded_url_, is_compatible);
3249}
3250
Dale Curtis6438cf12018-03-29 02:34:013251void WebMediaPlayerImpl::ForceStaleStateForTesting(ReadyState target_state) {
3252 stale_state_override_for_testing_.emplace(target_state);
Dale Curtis99a9b482018-02-01 02:23:283253 UpdatePlayState();
3254}
3255
3256bool WebMediaPlayerImpl::IsSuspendedForTesting() {
3257 // This intentionally uses IsPipelineSuspended since we need to know when the
3258 // pipeline has reached the suspended state, not when it's in suspending.
Antonio Gomes81b56552019-05-15 22:15:283259 return pipeline_controller_->IsPipelineSuspended();
Dale Curtis99a9b482018-02-01 02:23:283260}
3261
Dale Curtis7c63f2e22018-09-19 21:06:273262bool WebMediaPlayerImpl::DidLazyLoad() const {
3263 return did_lazy_load_;
3264}
3265
3266void WebMediaPlayerImpl::OnBecameVisible() {
Dale Curtis04769ca2019-05-25 00:38:103267 have_enough_after_lazy_load_cb_.Cancel();
Dale Curtis7c63f2e22018-09-19 21:06:273268 needs_first_frame_ = !has_first_frame_;
3269 UpdatePlayState();
3270}
3271
Miguel Casasfb63a5792018-12-04 23:50:413272bool WebMediaPlayerImpl::IsOpaque() const {
3273 return opaque_;
3274}
3275
Mounir Lamouri99ba5a62019-02-12 01:27:473276int WebMediaPlayerImpl::GetDelegateId() {
3277 return delegate_id_;
3278}
3279
3280base::Optional<viz::SurfaceId> WebMediaPlayerImpl::GetSurfaceId() {
3281 if (!surface_layer_for_video_enabled_)
3282 return base::nullopt;
3283 return bridge_->GetSurfaceId();
3284}
3285
Thomas Guilberte90fe312019-11-07 22:27:563286void WebMediaPlayerImpl::RequestAnimationFrame() {
3287 vfc_task_runner_->PostTask(
3288 FROM_HERE,
3289 base::BindOnce(&VideoFrameCompositor::SetOnFramePresentedCallback,
3290 base::Unretained(compositor_.get()),
3291 BindToCurrentLoop(base::BindOnce(
3292 &WebMediaPlayerImpl::OnNewFramePresentedCallback,
3293 weak_factory_.GetWeakPtr()))));
3294}
3295
3296void WebMediaPlayerImpl::OnNewFramePresentedCallback(
3297 scoped_refptr<VideoFrame> presented_frame,
3298 base::TimeTicks presentation_time,
3299 base::TimeTicks expected_presentation_time,
3300 uint32_t presentation_counter) {
3301 current_frame_override_ = std::move(presented_frame);
3302 client_->OnRequestAnimationFrame(
3303 presentation_time, expected_presentation_time, presentation_counter,
3304 *current_frame_override_);
3305 current_frame_override_.reset();
3306}
3307
Antonio Gomes142f32a2019-05-15 23:32:563308base::WeakPtr<blink::WebMediaPlayer> WebMediaPlayerImpl::AsWeakPtr() {
3309 return weak_this_;
3310}
3311
Yuchen Liue7813972019-04-12 22:34:243312bool WebMediaPlayerImpl::ShouldPausePlaybackWhenHidden() const {
3313 // Audio only stream is allowed to play when in background.
3314 // TODO: We should check IsBackgroundOptimizationCandidate here. But we need
3315 // to move the logic of checking video frames out of that function.
3316 if (!HasVideo())
3317 return false;
3318
Junbo Kefba620b2019-01-16 02:54:363319 if (!is_background_video_playback_enabled_)
3320 return true;
3321
avayvod65fad272017-02-24 01:00:483322 // If suspending background video, pause any video that's not remoted or
3323 // not unlocked to play in the background.
Sergey Volk8b09c2c52018-12-12 23:20:403324 if (IsBackgroundSuspendEnabled(this)) {
avayvod65fad272017-02-24 01:00:483325#if defined(OS_ANDROID)
Thomas Guilbert7350fdad2019-01-29 23:09:243326 if (is_flinging_)
avayvod65fad272017-02-24 01:00:483327 return false;
avayvodcc273dd2017-01-19 19:35:123328#endif
avayvodeb9098d2017-01-07 00:33:033329
Blink Reformat1c4d759e2017-04-09 16:34:543330 return !HasAudio() || (IsResumeBackgroundVideosEnabled() &&
3331 video_locked_when_paused_when_hidden_);
avayvod65fad272017-02-24 01:00:483332 }
3333
3334 // Otherwise only pause if the optimization is on and it's a video-only
3335 // optimization candidate.
avayvod01201332017-04-14 00:27:153336 return IsBackgroundVideoPauseOptimizationEnabled() && !HasAudio() &&
Thomas Guilbert29ae1a902018-10-20 01:53:383337 IsBackgroundOptimizationCandidate() && !is_flinging_;
avayvodeb9098d2017-01-07 00:33:033338}
3339
avayvod2135a642017-01-13 00:17:143340bool WebMediaPlayerImpl::ShouldDisableVideoWhenHidden() const {
Ted Meyera918e592018-09-08 00:16:203341 // This optimization is behind the flag on all platforms, only for non-mse
3342 // video. MSE video track switching on hide has gone through a field test.
3343 // TODO(tmathmeyer): Passing load_type_ won't be needed after src= field
3344 // testing is finished. see: http://crbug.com/709302
Ted Meyer0e105992019-05-04 01:47:213345 if (!is_background_video_track_optimization_supported_)
avayvodc4bfb0e62017-01-13 01:07:013346 return false;
avayvodc4bfb0e62017-01-13 01:07:013347
avayvodcc273dd2017-01-19 19:35:123348 // Disable video track only for players with audio that match the criteria for
3349 // being optimized.
Blink Reformat1c4d759e2017-04-09 16:34:543350 return HasAudio() && IsBackgroundOptimizationCandidate();
avayvodcc273dd2017-01-19 19:35:123351}
3352
3353bool WebMediaPlayerImpl::IsBackgroundOptimizationCandidate() const {
3354 DCHECK(main_task_runner_->BelongsToCurrentThread());
3355
François Beaufort664c3ca72018-04-13 07:24:513356 // Don't optimize Picture-in-Picture players.
Mounir Lamouri0484f40a2018-07-25 03:03:263357 if (IsInPictureInPicture())
François Beaufort664c3ca72018-04-13 07:24:513358 return false;
3359
Thomas Guilbert7350fdad2019-01-29 23:09:243360#if defined(OS_ANDROID)
3361 // Don't optimize videos casted as part of RemotePlayback.
3362 if (is_flinging_)
avayvodcc273dd2017-01-19 19:35:123363 return false;
Thomas Guilbert7350fdad2019-01-29 23:09:243364#endif
avayvodcc273dd2017-01-19 19:35:123365
3366 // Don't optimize audio-only or streaming players.
Blink Reformat1c4d759e2017-04-09 16:34:543367 if (!HasVideo() || IsStreaming())
avayvodcc273dd2017-01-19 19:35:123368 return false;
3369
Dale Curtis3f4935b2017-09-09 00:11:593370 // Video-only players are always optimized (paused).
3371 // Don't check the keyframe distance and duration.
3372 if (!HasAudio() && HasVideo())
3373 return true;
3374
avayvodcc273dd2017-01-19 19:35:123375 // Videos shorter than the maximum allowed keyframe distance can be optimized.
3376 base::TimeDelta duration = GetPipelineMediaDuration();
Ted Meyera918e592018-09-08 00:16:203377
Dale Curtis456808a2018-10-23 17:50:213378 constexpr base::TimeDelta kMaxKeyframeDistanceToDisableBackgroundVideo =
3379 base::TimeDelta::FromMilliseconds(
3380 kMaxKeyframeDistanceToDisableBackgroundVideoMs);
3381 if (duration < kMaxKeyframeDistanceToDisableBackgroundVideo)
avayvodcc273dd2017-01-19 19:35:123382 return true;
3383
3384 // Otherwise, only optimize videos with shorter average keyframe distance.
avayvodc4bfb0e62017-01-13 01:07:013385 PipelineStatistics stats = GetPipelineStatistics();
Dale Curtis456808a2018-10-23 17:50:213386 return stats.video_keyframe_distance_average <
3387 kMaxKeyframeDistanceToDisableBackgroundVideo;
avayvod2135a642017-01-13 00:17:143388}
3389
avayvod56e1f3942017-01-21 02:06:313390void WebMediaPlayerImpl::UpdateBackgroundVideoOptimizationState() {
Takumi Fujimotof2319684e2019-12-03 18:51:513391 if (IsHidden()) {
Yuchen Liue7813972019-04-12 22:34:243392 if (ShouldPausePlaybackWhenHidden()) {
avayvod56e1f3942017-01-21 02:06:313393 PauseVideoIfNeeded();
Dale Curtisa75a7892017-08-09 20:21:513394 } else if (update_background_status_cb_.IsCancelled()) {
3395 // Only trigger updates when we don't have one already scheduled.
3396 update_background_status_cb_.Reset(
3397 base::Bind(&WebMediaPlayerImpl::DisableVideoTrackIfNeeded,
3398 base::Unretained(this)));
3399
3400 // Defer disable track until we're sure the clip will be backgrounded for
3401 // some time. Resuming may take half a second, so frequent tab switches
3402 // will yield a poor user experience otherwise. http://crbug.com/709302
3403 // may also cause AV sync issues if disable/enable happens too fast.
3404 main_task_runner_->PostDelayedTask(
3405 FROM_HERE, update_background_status_cb_.callback(),
3406 base::TimeDelta::FromSeconds(10));
3407 }
avayvod56e1f3942017-01-21 02:06:313408 } else {
Dale Curtisa75a7892017-08-09 20:21:513409 update_background_status_cb_.Cancel();
avayvod56e1f3942017-01-21 02:06:313410 EnableVideoTrackIfNeeded();
3411 }
3412}
3413
3414void WebMediaPlayerImpl::PauseVideoIfNeeded() {
3415 DCHECK(IsHidden());
3416
3417 // Don't pause video while the pipeline is stopped, resuming or seeking.
3418 // Also if the video is paused already.
Antonio Gomes81b56552019-05-15 22:15:283419 if (!pipeline_controller_->IsPipelineRunning() || is_pipeline_resuming_ ||
tguilbert350936ff2017-02-24 05:39:273420 seeking_ || paused_)
avayvod56e1f3942017-01-21 02:06:313421 return;
3422
3423 // OnPause() will set |paused_when_hidden_| to false and call
3424 // UpdatePlayState(), so set the flag to true after and then return.
3425 OnPause();
3426 paused_when_hidden_ = true;
3427}
3428
avayvod2135a642017-01-13 00:17:143429void WebMediaPlayerImpl::EnableVideoTrackIfNeeded() {
avayvod56e1f3942017-01-21 02:06:313430 // Don't change video track while the pipeline is stopped, resuming or
3431 // seeking.
Antonio Gomes81b56552019-05-15 22:15:283432 if (!pipeline_controller_->IsPipelineRunning() || is_pipeline_resuming_ ||
tguilbert350936ff2017-02-24 05:39:273433 seeking_)
avayvod2135a642017-01-13 00:17:143434 return;
3435
3436 if (video_track_disabled_) {
3437 video_track_disabled_ = false;
Blink Reformat1c4d759e2017-04-09 16:34:543438 if (client_->HasSelectedVideoTrack()) {
3439 WebMediaPlayer::TrackId trackId = client_->GetSelectedVideoTrackId();
3440 SelectedVideoTrackChanged(&trackId);
avayvod2135a642017-01-13 00:17:143441 }
3442 }
3443}
3444
3445void WebMediaPlayerImpl::DisableVideoTrackIfNeeded() {
3446 DCHECK(IsHidden());
3447
3448 // Don't change video track while the pipeline is resuming or seeking.
3449 if (is_pipeline_resuming_ || seeking_)
3450 return;
3451
3452 if (!video_track_disabled_ && ShouldDisableVideoWhenHidden()) {
3453 video_track_disabled_ = true;
Blink Reformat1c4d759e2017-04-09 16:34:543454 SelectedVideoTrackChanged(nullptr);
avayvod2135a642017-01-13 00:17:143455 }
3456}
3457
avayvodc4bfb0e62017-01-13 01:07:013458void WebMediaPlayerImpl::SetPipelineStatisticsForTest(
3459 const PipelineStatistics& stats) {
3460 pipeline_statistics_for_test_ = base::make_optional(stats);
3461}
3462
3463PipelineStatistics WebMediaPlayerImpl::GetPipelineStatistics() const {
3464 DCHECK(main_task_runner_->BelongsToCurrentThread());
3465
tguilbert350936ff2017-02-24 05:39:273466 return pipeline_statistics_for_test_.value_or(
Antonio Gomes81b56552019-05-15 22:15:283467 pipeline_controller_->GetStatistics());
avayvodc4bfb0e62017-01-13 01:07:013468}
3469
avayvodcc273dd2017-01-19 19:35:123470void WebMediaPlayerImpl::SetPipelineMediaDurationForTest(
3471 base::TimeDelta duration) {
3472 pipeline_media_duration_for_test_ = base::make_optional(duration);
3473}
3474
3475base::TimeDelta WebMediaPlayerImpl::GetPipelineMediaDuration() const {
3476 DCHECK(main_task_runner_->BelongsToCurrentThread());
3477
3478 return pipeline_media_duration_for_test_.value_or(
Antonio Gomes81b56552019-05-15 22:15:283479 pipeline_controller_->GetMediaDuration());
avayvodcc273dd2017-01-19 19:35:123480}
3481
Xiangjun Zhangba8724f482017-08-03 06:43:253482void WebMediaPlayerImpl::SwitchToRemoteRenderer(
3483 const std::string& remote_device_friendly_name) {
xjz4e5d4bf32017-02-15 21:26:353484 DCHECK(main_task_runner_->BelongsToCurrentThread());
Chris Cunningham89b4b762019-03-27 17:13:333485
3486 DCHECK(!is_remote_rendering_);
3487 is_remote_rendering_ = true;
3488
Thomas Guilbertb341bae02018-05-09 00:02:133489 DCHECK(!disable_pipeline_auto_suspend_);
Xiangjun Zhangba8724f482017-08-03 06:43:253490 disable_pipeline_auto_suspend_ = true;
Chris Cunninghamd9df58e2017-08-29 00:04:233491
3492 // Capabilities reporting should only be performed for local playbacks.
3493 video_decode_stats_reporter_.reset();
3494
Xiangjun Zhangba8724f482017-08-03 06:43:253495 // Requests to restart media pipeline. A remote renderer will be created via
3496 // the |renderer_factory_selector_|.
xjz4e5d4bf32017-02-15 21:26:353497 ScheduleRestart();
xjzdf9b67e2017-04-13 23:28:253498 if (client_) {
Xiangjun Zhangba8724f482017-08-03 06:43:253499 client_->MediaRemotingStarted(
3500 WebString::FromUTF8(remote_device_friendly_name));
xjzdf9b67e2017-04-13 23:28:253501 }
xjz4e5d4bf32017-02-15 21:26:353502}
3503
Xiangjun Zhang5e20cba42018-01-10 19:54:563504void WebMediaPlayerImpl::SwitchToLocalRenderer(
3505 MediaObserverClient::ReasonToSwitchToLocal reason) {
Xiangjun Zhangba8724f482017-08-03 06:43:253506 DCHECK(main_task_runner_->BelongsToCurrentThread());
Chris Cunningham89b4b762019-03-27 17:13:333507 if (!is_remote_rendering_)
Xiangjun Zhang12f55272018-07-30 23:19:373508 return; // Is currently with local renderer.
Chris Cunningham89b4b762019-03-27 17:13:333509 is_remote_rendering_ = false;
3510
3511 DCHECK(disable_pipeline_auto_suspend_);
Xiangjun Zhangba8724f482017-08-03 06:43:253512 disable_pipeline_auto_suspend_ = false;
Chris Cunninghamd9df58e2017-08-29 00:04:233513
3514 // Capabilities reporting may resume now that playback is local.
3515 CreateVideoDecodeStatsReporter();
3516
Xiangjun Zhangba8724f482017-08-03 06:43:253517 // Requests to restart media pipeline. A local renderer will be created via
3518 // the |renderer_factory_selector_|.
3519 ScheduleRestart();
3520 if (client_)
Xiangjun Zhang5e20cba42018-01-10 19:54:563521 client_->MediaRemotingStopped(GetSwitchToLocalMessage(reason));
Xiangjun Zhangba8724f482017-08-03 06:43:253522}
3523
dalecurtis4f6d14d2017-02-22 17:42:223524void WebMediaPlayerImpl::RecordUnderflowDuration(base::TimeDelta duration) {
3525 DCHECK(data_source_ || chunk_demuxer_);
xhwang68b3fab82017-05-17 21:31:463526
dalecurtis4f6d14d2017-02-22 17:42:223527 if (data_source_)
Jennifer Apacible82e25c92017-08-07 18:15:273528 UMA_HISTOGRAM_TIMES("Media.UnderflowDuration2.SRC", duration);
dalecurtis4f6d14d2017-02-22 17:42:223529 else
Jennifer Apacible82e25c92017-08-07 18:15:273530 UMA_HISTOGRAM_TIMES("Media.UnderflowDuration2.MSE", duration);
xhwang68b3fab82017-05-17 21:31:463531
3532 if (is_encrypted_)
Jennifer Apacible82e25c92017-08-07 18:15:273533 UMA_HISTOGRAM_TIMES("Media.UnderflowDuration2.EME", duration);
dalecurtis4f6d14d2017-02-22 17:42:223534}
3535
xhwang60802652017-04-19 07:29:583536#define UMA_HISTOGRAM_VIDEO_HEIGHT(name, sample) \
3537 UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, 100, 10000, 50)
3538
3539void WebMediaPlayerImpl::RecordVideoNaturalSize(const gfx::Size& natural_size) {
3540 // Always report video natural size to MediaLog.
Ted Meyer380ca982020-01-12 01:00:293541 media_log_->AddLogRecord(media_log_->CreateVideoSizeSetEvent(
xhwang60802652017-04-19 07:29:583542 natural_size.width(), natural_size.height()));
Ted Meyer7f5b4e22019-11-21 03:21:193543 media_log_->SetProperty<MediaLogProperty::kResolution>(natural_size);
xhwang60802652017-04-19 07:29:583544
3545 if (initial_video_height_recorded_)
3546 return;
3547
3548 initial_video_height_recorded_ = true;
3549
3550 int height = natural_size.height();
3551
3552 if (load_type_ == kLoadTypeURL)
3553 UMA_HISTOGRAM_VIDEO_HEIGHT("Media.VideoHeight.Initial.SRC", height);
3554 else if (load_type_ == kLoadTypeMediaSource)
3555 UMA_HISTOGRAM_VIDEO_HEIGHT("Media.VideoHeight.Initial.MSE", height);
3556
3557 if (is_encrypted_)
3558 UMA_HISTOGRAM_VIDEO_HEIGHT("Media.VideoHeight.Initial.EME", height);
3559
3560 UMA_HISTOGRAM_VIDEO_HEIGHT("Media.VideoHeight.Initial.All", height);
3561}
3562
3563#undef UMA_HISTOGRAM_VIDEO_HEIGHT
3564
Greg Thompsonaa48ce8d2018-04-03 06:11:433565void WebMediaPlayerImpl::SetTickClockForTest(
3566 const base::TickClock* tick_clock) {
tzik2c963b872017-12-07 06:57:243567 tick_clock_ = tick_clock;
Antonio Gomes81b56552019-05-15 22:15:283568 buffered_data_source_host_->SetTickClockForTest(tick_clock);
hubbeb2d3efd2017-05-05 23:26:383569}
3570
Dale Curtis3899090ea2018-01-12 00:10:353571void WebMediaPlayerImpl::OnFirstFrame(base::TimeTicks frame_time) {
3572 DCHECK(!load_start_time_.is_null());
Dale Curtisff576552018-03-30 02:32:443573 DCHECK(!skip_metrics_due_to_startup_suspend_);
Dale Curtisc2c5dcb12018-04-16 23:21:293574 has_first_frame_ = true;
Dale Curtis7c63f2e22018-09-19 21:06:273575 needs_first_frame_ = false;
Dale Curtis3899090ea2018-01-12 00:10:353576 const base::TimeDelta elapsed = frame_time - load_start_time_;
3577 media_metrics_provider_->SetTimeToFirstFrame(elapsed);
3578 RecordTimingUMA("Media.TimeToFirstFrame", elapsed);
Dale Curtiscdfc6f22019-06-26 22:05:233579
3580 // Needed to signal HTMLVideoElement that it should remove the poster image.
Dale Curtis66790f32019-07-11 00:14:243581 if (client_ && has_poster_)
Dale Curtiscdfc6f22019-06-26 22:05:233582 client_->Repaint();
Dale Curtis3899090ea2018-01-12 00:10:353583}
3584
3585void WebMediaPlayerImpl::RecordTimingUMA(const std::string& key,
3586 base::TimeDelta elapsed) {
3587 if (chunk_demuxer_)
3588 base::UmaHistogramMediumTimes(key + ".MSE", elapsed);
3589 else
3590 base::UmaHistogramMediumTimes(key + ".SRC", elapsed);
3591 if (is_encrypted_)
3592 base::UmaHistogramMediumTimes(key + ".EME", elapsed);
3593}
3594
John Rummelldb5a7ef2018-05-16 00:28:013595void WebMediaPlayerImpl::RecordEncryptionScheme(
3596 const std::string& stream_name,
Yuchen Liub33bfc12019-11-08 20:16:123597 EncryptionScheme encryption_scheme) {
John Rummelldb5a7ef2018-05-16 00:28:013598 DCHECK(stream_name == "Audio" || stream_name == "Video");
3599
3600 // If the stream is not encrypted, don't record it.
Yuchen Liub33bfc12019-11-08 20:16:123601 if (encryption_scheme == EncryptionScheme::kUnencrypted)
John Rummelldb5a7ef2018-05-16 00:28:013602 return;
3603
3604 base::UmaHistogramEnumeration(
3605 "Media.EME.EncryptionScheme.Initial." + stream_name,
3606 DetermineEncryptionSchemeUMAValue(encryption_scheme),
3607 EncryptionSchemeUMA::kCount);
3608}
3609
Mounir Lamouri0484f40a2018-07-25 03:03:263610bool WebMediaPlayerImpl::IsInPictureInPicture() const {
3611 DCHECK(client_);
3612 return client_->DisplayType() ==
3613 WebMediaPlayer::DisplayType::kPictureInPicture;
3614}
3615
Dale Curtis5bba03232018-08-30 17:57:383616void WebMediaPlayerImpl::MaybeSetContainerName() {
Dale Curtisf01c8262018-09-04 23:50:433617 // MSE nor MediaPlayerRenderer provide container information.
3618 if (chunk_demuxer_ || using_media_player_renderer_)
Dale Curtis5bba03232018-08-30 17:57:383619 return;
3620
3621 // Pipeline startup failed before even getting a demuxer setup.
3622 if (!demuxer_)
3623 return;
3624
3625 // Container has already been set.
3626 if (highest_ready_state_ >= WebMediaPlayer::kReadyStateHaveMetadata)
3627 return;
3628
3629// If ffmpeg isn't enabled, we can't get the container name.
3630#if BUILDFLAG(ENABLE_FFMPEG)
3631 media_metrics_provider_->SetContainerName(
3632 static_cast<FFmpegDemuxer*>(demuxer_.get())->container());
3633#endif
3634}
3635
Dale Curtis18ad391b2019-05-30 23:25:093636void WebMediaPlayerImpl::MaybeUpdateBufferSizesForPlayback() {
3637 // Don't increase the MultiBufferDataSource buffer size until we've reached
3638 // kReadyStateHaveEnoughData. Otherwise we will unnecessarily slow down
3639 // playback startup -- it can instead be done for free after playback starts.
3640 if (!mb_data_source_ || highest_ready_state_ < kReadyStateHaveEnoughData)
3641 return;
3642
3643 mb_data_source_->MediaPlaybackRateChanged(playback_rate_);
3644 if (!paused_)
3645 mb_data_source_->MediaIsPlaying();
Becca Hughes7a85bf22019-07-24 16:16:583646
3647 // The playback rate has changed so we should rebuild the media position
3648 // state.
3649 UpdateMediaPositionState();
Dale Curtis18ad391b2019-05-30 23:25:093650}
3651
Dan Sanders6edfd782019-08-13 00:13:183652void WebMediaPlayerImpl::OnSimpleWatchTimerTick() {
3653 RecordSimpleWatchTimeUMA(reported_renderer_type_);
3654}
3655
[email protected]dbb3ff372019-10-15 19:09:423656GURL WebMediaPlayerImpl::GetSrcAfterRedirects() {
3657 return mb_data_source_ ? mb_data_source_->GetUrlAfterRedirects() : GURL();
3658}
3659
Thomas Guilberte90fe312019-11-07 22:27:563660void WebMediaPlayerImpl::SetCurrentFrameOverrideForTesting(
3661 scoped_refptr<VideoFrame> current_frame_override) {
3662 current_frame_override_ = current_frame_override;
3663}
3664
acolwell9e0840d2014-09-06 19:01:323665} // namespace media