blob: 5b924bb471d552f6714b9f08888c7d9fd3f08c42 [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>
guidouc7babef2015-10-22 00:42:3510#include <string>
dcheng652f5ff2015-12-27 08:54:0011#include <utility>
[email protected]47b06ceb2010-08-04 22:41:1112
[email protected]08273c7b2011-09-17 00:33:5113#include "base/bind.h"
sandersd1c0bba02016-03-04 23:14:0814#include "base/bind_helpers.h"
[email protected]2041cf342010-02-19 03:15:5915#include "base/callback.h"
[email protected]7502ef12014-01-25 01:19:2716#include "base/callback_helpers.h"
sandersd1e49fb62015-12-12 01:18:0617#include "base/command_line.h"
[email protected]ca04095f2014-02-07 10:23:5618#include "base/debug/alias.h"
[email protected]926f8fd2013-04-12 20:27:5319#include "base/debug/crash_logging.h"
[email protected]b0b258f2011-11-08 00:34:2320#include "base/metrics/histogram.h"
acolwellb4034942014-08-28 15:42:4321#include "base/single_thread_task_runner.h"
[email protected]cf02541b2012-04-11 08:02:1722#include "base/synchronization/waitable_event.h"
wdzierzanowskifd4cd91c52015-12-02 23:50:2023#include "base/task_runner_util.h"
anujk.sharma2fa37a02015-04-30 05:51:3224#include "base/thread_task_runner_handle.h"
ssid9525f4672015-01-28 12:13:1525#include "base/trace_event/trace_event.h"
sandersd1e49fb62015-12-12 01:18:0626#include "build/build_config.h"
[email protected]38564622014-08-19 02:47:1827#include "cc/blink/web_layer_impl.h"
[email protected]21c3f7502013-03-23 03:29:5128#include "cc/layers/video_layer.h"
dongseong.hwang0c4e9d82015-01-08 20:11:1329#include "gpu/blink/webgraphicscontext3d_impl.h"
[email protected]e4fc09e2012-04-06 03:17:4430#include "media/audio/null_audio_sink.h"
[email protected]2eccbed2014-01-10 05:15:5331#include "media/base/bind_to_current_loop.h"
xhwang0ad11e512014-11-25 23:43:0932#include "media/base/cdm_context.h"
[email protected]32da1002010-03-03 21:57:3533#include "media/base/limits.h"
[email protected]090f7312011-08-05 23:26:4034#include "media/base/media_log.h"
sandersd1e49fb62015-12-12 01:18:0635#include "media/base/media_switches.h"
[email protected]8a561062013-11-22 01:19:3136#include "media/base/text_renderer.h"
watk9f9dfdc92015-09-04 21:33:2937#include "media/base/timestamp_constants.h"
[email protected]e81283bb2010-08-31 18:01:2138#include "media/base/video_frame.h"
acolwell9e0840d2014-09-06 19:01:3239#include "media/blink/texttrack_impl.h"
40#include "media/blink/webaudiosourceprovider_impl.h"
xhwang97de4202014-11-25 08:44:0141#include "media/blink/webcontentdecryptionmodule_impl.h"
acolwell9e0840d2014-09-06 19:01:3242#include "media/blink/webinbandtexttrack_impl.h"
43#include "media/blink/webmediaplayer_delegate.h"
acolwell9e0840d2014-09-06 19:01:3244#include "media/blink/webmediaplayer_util.h"
45#include "media/blink/webmediasource_impl.h"
[email protected]efe7cd22012-09-12 23:55:0146#include "media/filters/chunk_demuxer.h"
[email protected]ddbc6ff2013-04-19 15:28:3347#include "media/filters/ffmpeg_demuxer.h"
jrummellc9d8e532015-02-26 18:38:1948#include "third_party/WebKit/public/platform/WebEncryptedMediaTypes.h"
srirama.m26f864d02015-07-14 05:21:4649#include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
50#include "third_party/WebKit/public/platform/WebMediaPlayerEncryptedMediaClient.h"
guidou9bfe4e2f2016-04-09 08:31:1951#include "third_party/WebKit/public/platform/WebMediaPlayerSource.h"
[email protected]745746d2013-08-23 02:09:1652#include "third_party/WebKit/public/platform/WebMediaSource.h"
[email protected]c10884462013-05-30 00:22:0953#include "third_party/WebKit/public/platform/WebRect.h"
mek966863c2016-02-04 23:39:0554#include "third_party/WebKit/public/platform/WebSecurityOrigin.h"
[email protected]c10884462013-05-30 00:22:0955#include "third_party/WebKit/public/platform/WebSize.h"
56#include "third_party/WebKit/public/platform/WebString.h"
57#include "third_party/WebKit/public/platform/WebURL.h"
xhwang0acca44b2015-06-18 00:43:3158#include "third_party/WebKit/public/web/WebFrame.h"
[email protected]80504652014-04-18 04:41:5059#include "third_party/WebKit/public/web/WebLocalFrame.h"
[email protected]2255a9332013-06-17 05:12:3160#include "third_party/WebKit/public/web/WebView.h"
[email protected]b3f2b912009-04-09 16:18:5261
[email protected]180ef242013-11-07 06:50:4662using blink::WebCanvas;
63using blink::WebMediaPlayer;
64using blink::WebRect;
65using blink::WebSize;
66using blink::WebString;
hubbed5f36882016-01-15 22:40:3767using gpu::gles2::GLES2Interface;
68
danakj365175c2016-02-06 00:37:3769#define STATIC_ASSERT_ENUM(a, b) \
70 static_assert(static_cast<int>(a) == static_cast<int>(b), \
71 "mismatching enums: " #a)
72
hubbed5f36882016-01-15 22:40:3773namespace media {
[email protected]ec9212f2008-12-18 21:40:3674
[email protected]8931c41a2009-07-07 17:31:4975namespace {
76
[email protected]378f0b72009-08-11 17:11:4277// Limits the range of playback rate.
78//
79// TODO(kylep): Revisit these.
80//
81// Vista has substantially lower performance than XP or Windows7. If you speed
82// up a video too much, it can't keep up, and rendering stops updating except on
83// the time bar. For really high speeds, audio becomes a bottleneck and we just
84// use up the data we have, which may not achieve the speed requested, but will
85// not crash the tab.
86//
87// A very slow speed, ie 0.00000001x, causes the machine to lock up. (It seems
88// like a busy loop). It gets unresponsive, although its not completely dead.
89//
90// Also our timers are not very accurate (especially for ogg), which becomes
91// evident at low speeds and on Vista. Since other speeds are risky and outside
92// the norms, we think 1/16x to 16x is a safe and useful range for now.
[email protected]39bdde32013-04-17 17:44:2093const double kMinRate = 0.0625;
94const double kMaxRate = 16.0;
[email protected]378f0b72009-08-11 17:11:4295
hubbed5f36882016-01-15 22:40:3796void SetSinkIdOnMediaThread(scoped_refptr<WebAudioSourceProviderImpl> sink,
97 const std::string& device_id,
98 const url::Origin& security_origin,
olka68b69392016-04-01 11:42:1299 const OutputDeviceStatusCB& callback) {
100 sink->SwitchOutputDevice(device_id, security_origin, callback);
guidouc7babef2015-10-22 00:42:35101}
102
sandersd50a635e2016-04-04 22:50:09103bool IsBackgroundedSuspendEnabled() {
dalecurtis0431cbf2016-03-12 01:19:43104#if !defined(OS_ANDROID)
105 // Suspend/Resume is only enabled by default on Android.
106 return base::CommandLine::ForCurrentProcess()->HasSwitch(
107 switches::kEnableMediaSuspend);
108#else
109 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
110 switches::kDisableMediaSuspend);
111#endif
112}
113
sandersd50a635e2016-04-04 22:50:09114bool IsNetworkStateError(blink::WebMediaPlayer::NetworkState state) {
115 bool result = state == blink::WebMediaPlayer::NetworkStateFormatError ||
116 state == blink::WebMediaPlayer::NetworkStateNetworkError ||
117 state == blink::WebMediaPlayer::NetworkStateDecodeError;
118 DCHECK_EQ(state > blink::WebMediaPlayer::NetworkStateLoaded, result);
119 return result;
120}
121
[email protected]8931c41a2009-07-07 17:31:49122} // namespace
123
[email protected]6683e1b2014-04-10 01:45:38124class BufferedDataSourceHostImpl;
125
danakj365175c2016-02-06 00:37:37126STATIC_ASSERT_ENUM(WebMediaPlayer::CORSModeUnspecified,
127 UrlData::CORS_UNSPECIFIED);
128STATIC_ASSERT_ENUM(WebMediaPlayer::CORSModeAnonymous, UrlData::CORS_ANONYMOUS);
129STATIC_ASSERT_ENUM(WebMediaPlayer::CORSModeUseCredentials,
130 UrlData::CORS_USE_CREDENTIALS);
[email protected]a5a01102012-06-06 17:01:24131
[email protected]2c539b82012-08-18 04:10:19132#define BIND_TO_RENDER_LOOP(function) \
acolwellb4034942014-08-28 15:42:43133 (DCHECK(main_task_runner_->BelongsToCurrentThread()), \
acolwell9e0840d2014-09-06 19:01:32134 BindToCurrentLoop(base::Bind(function, AsWeakPtr())))
[email protected]5983adb2012-10-24 00:12:00135
[email protected]4e982192014-06-21 13:35:45136#define BIND_TO_RENDER_LOOP1(function, arg1) \
acolwellb4034942014-08-28 15:42:43137 (DCHECK(main_task_runner_->BelongsToCurrentThread()), \
acolwell9e0840d2014-09-06 19:01:32138 BindToCurrentLoop(base::Bind(function, AsWeakPtr(), arg1)))
[email protected]4e982192014-06-21 13:35:45139
[email protected]5b5bb9d2010-10-22 19:57:36140WebMediaPlayerImpl::WebMediaPlayerImpl(
[email protected]35b2a972014-04-04 15:50:22141 blink::WebLocalFrame* frame,
[email protected]180ef242013-11-07 06:50:46142 blink::WebMediaPlayerClient* client,
srirama.m26f864d02015-07-14 05:21:46143 blink::WebMediaPlayerEncryptedMediaClient* encrypted_client,
[email protected]b8976652011-10-26 23:46:55144 base::WeakPtr<WebMediaPlayerDelegate> delegate,
dcheng3076abbf2016-04-22 20:42:39145 std::unique_ptr<RendererFactory> renderer_factory,
hubbe5f0ad43b2015-12-14 20:57:26146 linked_ptr<UrlIndex> url_index,
[email protected]e82b2bd2013-01-02 17:47:57147 const WebMediaPlayerParams& params)
[email protected]f6af7592014-02-28 10:09:11148 : frame_(frame),
sandersd50a635e2016-04-04 22:50:09149 delegate_state_(DelegateState::GONE),
150 is_idle_(false),
151 must_suspend_(false),
[email protected]ef405f66b2012-04-18 02:39:55152 network_state_(WebMediaPlayer::NetworkStateEmpty),
153 ready_state_(WebMediaPlayer::ReadyStateHaveNothing),
sandersd50a635e2016-04-04 22:50:09154 highest_ready_state_(WebMediaPlayer::ReadyStateHaveNothing),
acolwella5081a42014-08-28 23:42:52155 preload_(BufferedDataSource::AUTO),
sandersdc6ab163a2015-12-12 03:56:13156 buffering_strategy_(
157 BufferedDataSourceInterface::BUFFERING_STRATEGY_NORMAL),
anujk.sharma2fa37a02015-04-30 05:51:32158 main_task_runner_(base::ThreadTaskRunnerHandle::Get()),
acolwell755d12d2014-08-30 01:09:19159 media_task_runner_(params.media_task_runner()),
dcastagna617d086b2015-08-20 01:39:30160 worker_task_runner_(params.worker_task_runner()),
acolwell755d12d2014-08-30 01:09:19161 media_log_(params.media_log()),
acolwellb4034942014-08-28 15:42:43162 pipeline_(media_task_runner_, media_log_.get()),
sandersd1c0bba02016-03-04 23:14:08163 pipeline_controller_(
164 &pipeline_,
165 base::Bind(&WebMediaPlayerImpl::CreateRenderer,
166 base::Unretained(this)),
167 base::Bind(&WebMediaPlayerImpl::OnPipelineSeeked, AsWeakPtr()),
168 base::Bind(&WebMediaPlayerImpl::OnPipelineSuspended, AsWeakPtr()),
sandersd1c0bba02016-03-04 23:14:08169 base::Bind(&WebMediaPlayerImpl::OnPipelineError, AsWeakPtr())),
[email protected]f988d9b2014-07-25 00:35:43170 load_type_(LoadTypeURL),
[email protected]75e145a2014-04-15 17:44:32171 opaque_(false),
wolenetz1b2ae7d2015-06-10 00:44:21172 playback_rate_(0.0),
[email protected]49480902009-07-14 20:23:43173 paused_(true),
[email protected]b3766a22010-12-22 17:34:13174 seeking_(false),
watkdee516f2016-02-18 02:22:19175 pending_suspend_resume_cycle_(false),
scherkusd2c745b2014-09-04 05:03:40176 ended_(false),
yoichio863bebf2016-03-04 07:56:58177 should_notify_time_changed_(false),
watkdee516f2016-02-18 02:22:19178 fullscreen_(false),
179 decoder_requires_restart_for_fullscreen_(false),
[email protected]5badb082010-06-11 17:40:15180 client_(client),
srirama.m26f864d02015-07-14 05:21:46181 encrypted_client_(encrypted_client),
[email protected]baff4512011-10-19 18:21:07182 delegate_(delegate),
dalecurtisbb3eaac2016-01-27 21:10:25183 delegate_id_(0),
[email protected]d726eddc2013-07-02 22:25:55184 defer_load_cb_(params.defer_load_cb()),
dongseong.hwang0c4e9d82015-01-08 20:11:13185 context_3d_cb_(params.context_3d_cb()),
dalecurtis83266c72015-10-29 18:43:20186 adjust_allocated_memory_cb_(params.adjust_allocated_memory_cb()),
187 last_reported_memory_usage_(0),
[email protected]132dd57c2012-08-10 23:24:34188 supports_save_(true),
[email protected]f5443ef72013-04-22 04:03:38189 chunk_demuxer_(NULL),
hubbe5f0ad43b2015-12-14 20:57:26190 url_index_(url_index),
Bartosz Fabianowski85a823812015-04-16 10:27:51191 // Threaded compositing isn't enabled universally yet.
192 compositor_task_runner_(
193 params.compositor_task_runner()
194 ? params.compositor_task_runner()
195 : base::MessageLoop::current()->task_runner()),
[email protected]dd061e12014-05-06 19:21:22196 compositor_(new VideoFrameCompositor(
Bartosz Fabianowski85a823812015-04-16 10:27:51197 compositor_task_runner_,
[email protected]75e145a2014-04-15 17:44:32198 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNaturalSizeChanged),
[email protected]dd061e12014-05-06 19:21:22199 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnOpacityChanged))),
xhwang80739452016-01-13 00:48:00200 is_cdm_attached_(false),
hubbed5f36882016-01-15 22:40:37201#if defined(OS_ANDROID) // WMPI_CAST
dalecurtisbb3eaac2016-01-27 21:10:25202 cast_impl_(this, client_, params.context_3d_cb()),
hubbed5f36882016-01-15 22:40:37203#endif
dalecurtisbb3eaac2016-01-27 21:10:25204 volume_(1.0),
205 volume_multiplier_(1.0),
watkdee516f2016-02-18 02:22:19206 renderer_factory_(std::move(renderer_factory)),
dalecurtis2ff781da2016-03-03 01:52:13207 surface_manager_(params.surface_manager()),
dalecurtis7f366b2242016-04-13 01:16:17208 suppress_destruction_errors_(false),
209 can_suspend_state_(CanSuspendState::UNKNOWN) {
dalecurtis83266c72015-10-29 18:43:20210 DCHECK(!adjust_allocated_memory_cb_.is_null());
xhwang59d4175a2016-01-14 03:19:30211 DCHECK(renderer_factory_);
servolkef1e5ef2016-03-25 04:55:26212 DCHECK(client_);
dalecurtis83266c72015-10-29 18:43:20213
dalecurtisbb3eaac2016-01-27 21:10:25214 if (delegate_)
215 delegate_id_ = delegate_->AddObserver(this);
sandersd1e49fb62015-12-12 01:18:06216
[email protected]c93eb0a62011-08-09 22:47:24217 media_log_->AddEvent(
acolwell9e0840d2014-09-06 19:01:32218 media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_CREATED));
[email protected]4e6be3f2009-05-07 02:24:44219
xhwang0ad11e512014-11-25 23:43:09220 if (params.initial_cdm()) {
xhwang9bd8c732015-04-13 23:27:53221 SetCdm(base::Bind(&IgnoreCdmAttached),
222 ToWebContentDecryptionModuleImpl(params.initial_cdm())
223 ->GetCdmContext());
xhwang0ad11e512014-11-25 23:43:09224 }
225
xhwangf94a634d2014-10-22 22:07:27226 // TODO(xhwang): When we use an external Renderer, many methods won't work,
xhwang6fa356202014-12-11 00:44:12227 // e.g. GetCurrentFrameFromCompositor(). See http://crbug.com/434861
[email protected]c50edb962013-10-19 18:05:07228
[email protected]ddbc6ff2013-04-19 15:28:33229 // Use the null sink if no sink was provided.
[email protected]4a914882013-01-10 00:43:48230 audio_source_provider_ = new WebAudioSourceProviderImpl(
[email protected]ff875be52013-06-02 23:47:38231 params.audio_renderer_sink().get()
232 ? params.audio_renderer_sink()
acolwell9e0840d2014-09-06 19:01:32233 : new NullAudioSink(media_task_runner_));
[email protected]ec9212f2008-12-18 21:40:36234}
235
[email protected]4e6be3f2009-05-07 02:24:44236WebMediaPlayerImpl::~WebMediaPlayerImpl() {
[email protected]ce70c982013-12-20 17:04:32237 client_->setWebLayer(NULL);
[email protected]21c3f7502013-03-23 03:29:51238
acolwellb4034942014-08-28 15:42:43239 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:53240
sandersd1e49fb62015-12-12 01:18:06241 if (delegate_) {
dalecurtisbb3eaac2016-01-27 21:10:25242 delegate_->PlayerGone(delegate_id_);
243 delegate_->RemoveObserver(delegate_id_);
sandersd1e49fb62015-12-12 01:18:06244 }
[email protected]baff4512011-10-19 18:21:07245
[email protected]f6af7592014-02-28 10:09:11246 // Abort any pending IO so stopping the pipeline doesn't get blocked.
dalecurtis2ff781da2016-03-03 01:52:13247 suppress_destruction_errors_ = true;
[email protected]f6af7592014-02-28 10:09:11248 if (data_source_)
249 data_source_->Abort();
250 if (chunk_demuxer_) {
251 chunk_demuxer_->Shutdown();
sandersd1c0bba02016-03-04 23:14:08252 chunk_demuxer_ = nullptr;
[email protected]f6af7592014-02-28 10:09:11253 }
254
xhwang6fa356202014-12-11 00:44:12255 renderer_factory_.reset();
[email protected]f6af7592014-02-28 10:09:11256
257 // Make sure to kill the pipeline so there's no more media threads running.
258 // Note: stopping the pipeline might block for a long time.
259 base::WaitableEvent waiter(false, false);
260 pipeline_.Stop(
261 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&waiter)));
262 waiter.Wait();
263
dalecurtis83266c72015-10-29 18:43:20264 if (last_reported_memory_usage_)
265 adjust_allocated_memory_cb_.Run(-last_reported_memory_usage_);
266
[email protected]dd061e12014-05-06 19:21:22267 compositor_task_runner_->DeleteSoon(FROM_HERE, compositor_);
xhwangea8bb3562015-06-08 21:18:37268
269 media_log_->AddEvent(
270 media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_DESTROYED));
[email protected]ec9212f2008-12-18 21:40:36271}
272
guidou9bfe4e2f2016-04-09 08:31:19273void WebMediaPlayerImpl::load(LoadType load_type,
274 const blink::WebMediaPlayerSource& source,
[email protected]62e5e682013-03-07 23:53:24275 CORSMode cors_mode) {
guidou9bfe4e2f2016-04-09 08:31:19276 // Only URL or MSE blob URL is supported.
277 DCHECK(source.isURL());
278 blink::WebURL url = source.getAsURL();
[email protected]2a06ca62014-06-04 13:59:52279 DVLOG(1) << __FUNCTION__ << "(" << load_type << ", " << url << ", "
280 << cors_mode << ")";
[email protected]d726eddc2013-07-02 22:25:55281 if (!defer_load_cb_.is_null()) {
282 defer_load_cb_.Run(base::Bind(
[email protected]ef8394c2013-08-21 20:26:30283 &WebMediaPlayerImpl::DoLoad, AsWeakPtr(), load_type, url, cors_mode));
[email protected]d726eddc2013-07-02 22:25:55284 return;
285 }
[email protected]ef8394c2013-08-21 20:26:30286 DoLoad(load_type, url, cors_mode);
[email protected]62e5e682013-03-07 23:53:24287}
288
watkdee516f2016-02-18 02:22:19289void WebMediaPlayerImpl::enteredFullscreen() {
290 fullscreen_ = true;
291 if (decoder_requires_restart_for_fullscreen_)
292 ScheduleRestart();
293}
294
295void WebMediaPlayerImpl::exitedFullscreen() {
296 fullscreen_ = false;
297 if (decoder_requires_restart_for_fullscreen_)
298 ScheduleRestart();
299}
300
[email protected]ef8394c2013-08-21 20:26:30301void WebMediaPlayerImpl::DoLoad(LoadType load_type,
[email protected]180ef242013-11-07 06:50:46302 const blink::WebURL& url,
[email protected]d726eddc2013-07-02 22:25:55303 CORSMode cors_mode) {
hubbed82bed52015-12-15 23:07:16304 DVLOG(1) << __FUNCTION__;
acolwellb4034942014-08-28 15:42:43305 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d726eddc2013-07-02 22:25:55306
[email protected]62e5e682013-03-07 23:53:24307 GURL gurl(url);
xhwangbffbf452016-04-01 05:26:45308 ReportMetrics(load_type, gurl, frame_->getSecurityOrigin());
[email protected]62e5e682013-03-07 23:53:24309
[email protected]926f8fd2013-04-12 20:27:53310 // Set subresource URL for crash reporting.
311 base::debug::SetCrashKeyValue("subresource_url", gurl.spec());
312
[email protected]ef8394c2013-08-21 20:26:30313 load_type_ = load_type;
314
[email protected]62e5e682013-03-07 23:53:24315 SetNetworkState(WebMediaPlayer::NetworkStateLoading);
316 SetReadyState(WebMediaPlayer::ReadyStateHaveNothing);
kinukof19cde72015-12-18 23:15:25317 media_log_->AddEvent(media_log_->CreateLoadEvent(url.string().utf8()));
[email protected]d726eddc2013-07-02 22:25:55318
319 // Media source pipelines can start immediately.
[email protected]ef8394c2013-08-21 20:26:30320 if (load_type == LoadTypeMediaSource) {
[email protected]d726eddc2013-07-02 22:25:55321 supports_save_ = false;
[email protected]ef8394c2013-08-21 20:26:30322 StartPipeline();
hubbe5f0ad43b2015-12-14 20:57:26323 } else {
avayvode46d7bef2016-03-30 23:18:26324 // TODO(hubbe): This experiment is temporary and should be removed once
325 // we have enough data to support the primacy of the new media cache.
326 // See http://crbug.com/514719 for details.
327 // Otherwise it's a regular request which requires resolving the URL first.
328 if (base::FeatureList::IsEnabled(kUseNewMediaCache)) {
329 // Remove this when MultiBufferDataSource becomes default.
330 LOG(WARNING) << "Using MultibufferDataSource";
331 data_source_.reset(new MultibufferDataSource(
332 url, static_cast<UrlData::CORSMode>(cors_mode), main_task_runner_,
333 url_index_, frame_, media_log_.get(), &buffered_data_source_host_,
334 base::Bind(&WebMediaPlayerImpl::NotifyDownloading, AsWeakPtr())));
335 } else {
336 data_source_.reset(new BufferedDataSource(
337 url, static_cast<BufferedResourceLoader::CORSMode>(cors_mode),
338 main_task_runner_, frame_, media_log_.get(),
339 &buffered_data_source_host_,
340 base::Bind(&WebMediaPlayerImpl::NotifyDownloading, AsWeakPtr())));
341 }
342 data_source_->SetPreload(preload_);
343 data_source_->SetBufferingStrategy(buffering_strategy_);
344 data_source_->Initialize(
345 base::Bind(&WebMediaPlayerImpl::DataSourceInitialized, AsWeakPtr()));
hubbe5f0ad43b2015-12-14 20:57:26346 }
hubbed5f36882016-01-15 22:40:37347
348#if defined(OS_ANDROID) // WMPI_CAST
dalecurtisbb3eaac2016-01-27 21:10:25349 cast_impl_.Initialize(url, frame_, delegate_id_);
hubbed5f36882016-01-15 22:40:37350#endif
[email protected]62e5e682013-03-07 23:53:24351}
352
[email protected]4e6be3f2009-05-07 02:24:44353void WebMediaPlayerImpl::play() {
[email protected]2a06ca62014-06-04 13:59:52354 DVLOG(1) << __FUNCTION__;
acolwellb4034942014-08-28 15:42:43355 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:53356
hubbed5f36882016-01-15 22:40:37357#if defined(OS_ANDROID) // WMPI_CAST
358 if (isRemote()) {
dalecurtis6e2716d2016-01-21 04:49:15359 cast_impl_.play();
hubbed5f36882016-01-15 22:40:37360 return;
361 }
362#endif
363
[email protected]49480902009-07-14 20:23:43364 paused_ = false;
sandersd50a635e2016-04-04 22:50:09365 is_idle_ = false;
yoichio863bebf2016-03-04 07:56:58366 pipeline_.SetPlaybackRate(playback_rate_);
sandersd1c0bba02016-03-04 23:14:08367
[email protected]039b7542013-10-17 22:06:25368 if (data_source_)
369 data_source_->MediaIsPlaying();
[email protected]090f7312011-08-05 23:26:40370
acolwell9e0840d2014-09-06 19:01:32371 media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PLAY));
sandersd50a635e2016-04-04 22:50:09372 UpdatePlayState();
[email protected]ec9212f2008-12-18 21:40:36373}
374
[email protected]4e6be3f2009-05-07 02:24:44375void WebMediaPlayerImpl::pause() {
[email protected]2a06ca62014-06-04 13:59:52376 DVLOG(1) << __FUNCTION__;
acolwellb4034942014-08-28 15:42:43377 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:53378
sandersd50a635e2016-04-04 22:50:09379 // We update the paused state even when casting, since we expect pause() to be
380 // called when casting begins, and when we exit casting we should end up in a
381 // paused state.
[email protected]49480902009-07-14 20:23:43382 paused_ = true;
hubbed5f36882016-01-15 22:40:37383
384#if defined(OS_ANDROID) // WMPI_CAST
385 if (isRemote()) {
dalecurtis6e2716d2016-01-21 04:49:15386 cast_impl_.pause();
hubbed5f36882016-01-15 22:40:37387 return;
388 }
389#endif
390
a.berwal338bf002015-04-22 11:14:50391 pipeline_.SetPlaybackRate(0.0);
sandersd1c0bba02016-03-04 23:14:08392
393 // pause() may be called after playback has ended and the HTMLMediaElement
394 // requires that currentTime() == duration() after ending. We want to ensure
395 // |paused_time_| matches currentTime() in this case or a future seek() may
396 // incorrectly discard what it thinks is a seek to the existing time.
397 paused_time_ =
398 ended_ ? pipeline_.GetMediaDuration() : pipeline_.GetMediaTime();
[email protected]090f7312011-08-05 23:26:40399
acolwell9e0840d2014-09-06 19:01:32400 media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PAUSE));
sandersd50a635e2016-04-04 22:50:09401 UpdatePlayState();
[email protected]ec9212f2008-12-18 21:40:36402}
403
[email protected]574a1d62009-07-17 03:23:46404bool WebMediaPlayerImpl::supportsSave() const {
acolwellb4034942014-08-28 15:42:43405 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]132dd57c2012-08-10 23:24:34406 return supports_save_;
[email protected]574a1d62009-07-17 03:23:46407}
408
[email protected]39bdde32013-04-17 17:44:20409void WebMediaPlayerImpl::seek(double seconds) {
xhwang12d8d042014-12-01 21:48:57410 DVLOG(1) << __FUNCTION__ << "(" << seconds << "s)";
acolwellb4034942014-08-28 15:42:43411 DCHECK(main_task_runner_->BelongsToCurrentThread());
sandersd1c0bba02016-03-04 23:14:08412 DoSeek(base::TimeDelta::FromSecondsD(seconds), true);
413}
414
415void WebMediaPlayerImpl::DoSeek(base::TimeDelta time, bool time_updated) {
416 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:53417
hubbed5f36882016-01-15 22:40:37418#if defined(OS_ANDROID) // WMPI_CAST
419 if (isRemote()) {
sandersd1c0bba02016-03-04 23:14:08420 cast_impl_.seek(time);
hubbed5f36882016-01-15 22:40:37421 return;
422 }
423#endif
424
srirama.mccf671812015-01-08 11:59:13425 ReadyState old_state = ready_state_;
[email protected]1bb666802013-11-28 06:12:08426 if (ready_state_ > WebMediaPlayer::ReadyStateHaveMetadata)
427 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
428
sandersd1c0bba02016-03-04 23:14:08429 // When paused, we know exactly what the current time is and can elide seeks
430 // to it. However, there are two cases that are not elided:
431 // 1) When the pipeline state is not stable.
432 // In this case we just let |pipeline_controller_| decide what to do, as
433 // it has complete information.
434 // 2) For MSE.
435 // Because the buffers may have changed between seeks, MSE seeks are
436 // never elided.
437 if (paused_ && pipeline_controller_.IsStable() && paused_time_ == time &&
438 !chunk_demuxer_) {
439 // If the ready state was high enough before, we can indicate that the seek
440 // completed just by restoring it. Otherwise we will just wait for the real
441 // ready state change to eventually happen.
442 if (old_state == ReadyStateHaveEnoughData) {
srirama.m8f4a37562014-12-13 08:16:18443 main_task_runner_->PostTask(
srirama.m36ab2682014-12-11 04:20:01444 FROM_HERE,
445 base::Bind(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged,
srirama.m8f4a37562014-12-13 08:16:18446 AsWeakPtr(), BUFFERING_HAVE_ENOUGH));
srirama.m36ab2682014-12-11 04:20:01447 }
sandersd1c0bba02016-03-04 23:14:08448 return;
srirama.m36ab2682014-12-11 04:20:01449 }
[email protected]44ff37c02009-10-24 01:03:03450
sandersd50a635e2016-04-04 22:50:09451 // TODO(sandersd): Ideally we would not clear the idle state if
452 // |pipeline_controller_| can elide the seek.
453 is_idle_ = false;
454 ended_ = false;
455
[email protected]b3766a22010-12-22 17:34:13456 seeking_ = true;
sandersd1c0bba02016-03-04 23:14:08457 seek_time_ = time;
458 if (paused_)
459 paused_time_ = time;
460 pipeline_controller_.Seek(time, time_updated);
[email protected]b3766a22010-12-22 17:34:13461
sandersd50a635e2016-04-04 22:50:09462 // This needs to be called after Seek() so that if a resume is triggered, it
463 // is to the correct time.
464 UpdatePlayState();
[email protected]ec9212f2008-12-18 21:40:36465}
466
[email protected]39bdde32013-04-17 17:44:20467void WebMediaPlayerImpl::setRate(double rate) {
[email protected]2a06ca62014-06-04 13:59:52468 DVLOG(1) << __FUNCTION__ << "(" << rate << ")";
acolwellb4034942014-08-28 15:42:43469 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:53470
[email protected]378f0b72009-08-11 17:11:42471 // TODO(kylep): Remove when support for negatives is added. Also, modify the
472 // following checks so rewind uses reasonable values also.
[email protected]39bdde32013-04-17 17:44:20473 if (rate < 0.0)
[email protected]378f0b72009-08-11 17:11:42474 return;
475
476 // Limit rates to reasonable values by clamping.
[email protected]39bdde32013-04-17 17:44:20477 if (rate != 0.0) {
[email protected]378f0b72009-08-11 17:11:42478 if (rate < kMinRate)
479 rate = kMinRate;
480 else if (rate > kMaxRate)
481 rate = kMaxRate;
482 }
483
[email protected]49480902009-07-14 20:23:43484 playback_rate_ = rate;
485 if (!paused_) {
[email protected]f6af7592014-02-28 10:09:11486 pipeline_.SetPlaybackRate(rate);
[email protected]039b7542013-10-17 22:06:25487 if (data_source_)
488 data_source_->MediaPlaybackRateChanged(rate);
[email protected]49480902009-07-14 20:23:43489 }
[email protected]ec9212f2008-12-18 21:40:36490}
491
[email protected]39bdde32013-04-17 17:44:20492void WebMediaPlayerImpl::setVolume(double volume) {
[email protected]2a06ca62014-06-04 13:59:52493 DVLOG(1) << __FUNCTION__ << "(" << volume << ")";
acolwellb4034942014-08-28 15:42:43494 DCHECK(main_task_runner_->BelongsToCurrentThread());
dalecurtisbb3eaac2016-01-27 21:10:25495 volume_ = volume;
496 pipeline_.SetVolume(volume_ * volume_multiplier_);
[email protected]ec9212f2008-12-18 21:40:36497}
[email protected]f0a51fb52009-03-05 12:46:38498
guidouc7babef2015-10-22 00:42:35499void WebMediaPlayerImpl::setSinkId(
500 const blink::WebString& sink_id,
501 const blink::WebSecurityOrigin& security_origin,
502 blink::WebSetSinkIdCallbacks* web_callback) {
guidou69223ce2015-06-16 10:36:19503 DCHECK(main_task_runner_->BelongsToCurrentThread());
guidou5754e612015-07-31 08:09:41504 DVLOG(1) << __FUNCTION__;
guidouc7babef2015-10-22 00:42:35505
olka68b69392016-04-01 11:42:12506 media::OutputDeviceStatusCB callback =
507 media::ConvertToOutputDeviceStatusCB(web_callback);
guidouc7babef2015-10-22 00:42:35508 media_task_runner_->PostTask(
509 FROM_HERE,
510 base::Bind(&SetSinkIdOnMediaThread, audio_source_provider_,
511 sink_id.utf8(), static_cast<url::Origin>(security_origin),
512 callback));
guidou69223ce2015-06-16 10:36:19513}
514
danakj365175c2016-02-06 00:37:37515STATIC_ASSERT_ENUM(WebMediaPlayer::PreloadNone, BufferedDataSource::NONE);
516STATIC_ASSERT_ENUM(WebMediaPlayer::PreloadMetaData,
517 BufferedDataSource::METADATA);
518STATIC_ASSERT_ENUM(WebMediaPlayer::PreloadAuto, BufferedDataSource::AUTO);
[email protected]23a8b1d82011-04-05 16:28:20519
[email protected]ef405f66b2012-04-18 02:39:55520void WebMediaPlayerImpl::setPreload(WebMediaPlayer::Preload preload) {
[email protected]2a06ca62014-06-04 13:59:52521 DVLOG(1) << __FUNCTION__ << "(" << preload << ")";
acolwellb4034942014-08-28 15:42:43522 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]4e6be3f2009-05-07 02:24:44523
acolwella5081a42014-08-28 23:42:52524 preload_ = static_cast<BufferedDataSource::Preload>(preload);
[email protected]b49beeb2013-03-01 20:04:00525 if (data_source_)
[email protected]09c60222014-08-07 16:42:31526 data_source_->SetPreload(preload_);
[email protected]4e6be3f2009-05-07 02:24:44527}
528
danakj365175c2016-02-06 00:37:37529STATIC_ASSERT_ENUM(WebMediaPlayer::BufferingStrategy::Normal,
530 BufferedDataSource::BUFFERING_STRATEGY_NORMAL);
531STATIC_ASSERT_ENUM(WebMediaPlayer::BufferingStrategy::Aggressive,
532 BufferedDataSource::BUFFERING_STRATEGY_AGGRESSIVE);
sandersdc6ab163a2015-12-12 03:56:13533
534void WebMediaPlayerImpl::setBufferingStrategy(
535 WebMediaPlayer::BufferingStrategy buffering_strategy) {
536 DVLOG(1) << __FUNCTION__;
537 DCHECK(main_task_runner_->BelongsToCurrentThread());
538
dalecurtis37fe5862016-03-15 19:29:09539#if defined(OS_ANDROID)
540 // We disallow aggressive buffering on Android since it matches the behavior
541 // of the platform media player and may have data usage penalties.
542 // TODO(dalecurtis, hubbe): We should probably stop using "pause-and-buffer"
543 // everywhere. See http://crbug.com/594669 for more details.
544 buffering_strategy_ = BufferedDataSource::BUFFERING_STRATEGY_NORMAL;
545#else
sandersdc6ab163a2015-12-12 03:56:13546 buffering_strategy_ =
547 static_cast<BufferedDataSource::BufferingStrategy>(buffering_strategy);
dalecurtis37fe5862016-03-15 19:29:09548#endif
549
sandersdc6ab163a2015-12-12 03:56:13550 if (data_source_)
551 data_source_->SetBufferingStrategy(buffering_strategy_);
552}
553
[email protected]4e6be3f2009-05-07 02:24:44554bool WebMediaPlayerImpl::hasVideo() const {
acolwellb4034942014-08-28 15:42:43555 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:53556
[email protected]b8877772014-03-26 20:17:15557 return pipeline_metadata_.has_video;
[email protected]d43ed912009-02-03 04:52:53558}
559
[email protected]fc367af2009-08-14 23:06:35560bool WebMediaPlayerImpl::hasAudio() const {
acolwellb4034942014-08-28 15:42:43561 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]fc367af2009-08-14 23:06:35562
[email protected]b8877772014-03-26 20:17:15563 return pipeline_metadata_.has_audio;
[email protected]fc367af2009-08-14 23:06:35564}
565
[email protected]180ef242013-11-07 06:50:46566blink::WebSize WebMediaPlayerImpl::naturalSize() const {
acolwellb4034942014-08-28 15:42:43567 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:53568
[email protected]b8877772014-03-26 20:17:15569 return blink::WebSize(pipeline_metadata_.natural_size);
[email protected]d43ed912009-02-03 04:52:53570}
571
[email protected]4e6be3f2009-05-07 02:24:44572bool WebMediaPlayerImpl::paused() const {
acolwellb4034942014-08-28 15:42:43573 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:53574
hubbed5f36882016-01-15 22:40:37575#if defined(OS_ANDROID) // WMPI_CAST
576 if (isRemote())
577 return cast_impl_.paused();
578#endif
sandersd50a635e2016-04-04 22:50:09579
[email protected]f6af7592014-02-28 10:09:11580 return pipeline_.GetPlaybackRate() == 0.0f;
[email protected]d43ed912009-02-03 04:52:53581}
582
[email protected]4e6be3f2009-05-07 02:24:44583bool WebMediaPlayerImpl::seeking() const {
acolwellb4034942014-08-28 15:42:43584 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]d43ed912009-02-03 04:52:53585
[email protected]ef405f66b2012-04-18 02:39:55586 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
[email protected]0acebfa2009-08-21 22:45:40587 return false;
[email protected]67cd5052009-09-10 21:53:22588
[email protected]b3766a22010-12-22 17:34:13589 return seeking_;
[email protected]ec9212f2008-12-18 21:40:36590}
591
[email protected]39bdde32013-04-17 17:44:20592double WebMediaPlayerImpl::duration() const {
acolwellb4034942014-08-28 15:42:43593 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]39bdde32013-04-17 17:44:20594
595 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
596 return std::numeric_limits<double>::quiet_NaN();
597
[email protected]39bdde32013-04-17 17:44:20598 return GetPipelineDuration();
[email protected]d43ed912009-02-03 04:52:53599}
600
[email protected]db66d0092014-04-16 07:15:12601double WebMediaPlayerImpl::timelineOffset() const {
acolwellb4034942014-08-28 15:42:43602 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]db66d0092014-04-16 07:15:12603
604 if (pipeline_metadata_.timeline_offset.is_null())
605 return std::numeric_limits<double>::quiet_NaN();
606
607 return pipeline_metadata_.timeline_offset.ToJsTime();
608}
609
[email protected]39bdde32013-04-17 17:44:20610double WebMediaPlayerImpl::currentTime() const {
acolwellb4034942014-08-28 15:42:43611 DCHECK(main_task_runner_->BelongsToCurrentThread());
scherkusd2c745b2014-09-04 05:03:40612 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
613
614 // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement,
615 // see http://crbug.com/409280
616 if (ended_)
617 return duration();
618
sandersd1c0bba02016-03-04 23:14:08619 if (seeking())
620 return seek_time_.InSecondsF();
wolenetz1b2ae7d2015-06-10 00:44:21621
hubbed5f36882016-01-15 22:40:37622#if defined(OS_ANDROID) // WMPI_CAST
sandersd1c0bba02016-03-04 23:14:08623 if (isRemote())
hubbed5f36882016-01-15 22:40:37624 return cast_impl_.currentTime();
hubbed5f36882016-01-15 22:40:37625#endif
626
sandersd1c0bba02016-03-04 23:14:08627 if (paused_)
hubbed5f36882016-01-15 22:40:37628 return paused_time_.InSecondsF();
hubbed5f36882016-01-15 22:40:37629
630 return pipeline_.GetMediaTime().InSecondsF();
[email protected]d43ed912009-02-03 04:52:53631}
632
danakj13afe0362016-02-27 01:22:50633WebMediaPlayer::NetworkState WebMediaPlayerImpl::getNetworkState() const {
acolwellb4034942014-08-28 15:42:43634 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]ddb1e5a2010-12-13 20:10:45635 return network_state_;
636}
637
danakj13afe0362016-02-27 01:22:50638WebMediaPlayer::ReadyState WebMediaPlayerImpl::getReadyState() const {
acolwellb4034942014-08-28 15:42:43639 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]ddb1e5a2010-12-13 20:10:45640 return ready_state_;
641}
642
wolenetz4d39cc02016-04-05 19:54:41643blink::WebString WebMediaPlayerImpl::getErrorMessage() {
644 DCHECK(main_task_runner_->BelongsToCurrentThread());
645 return blink::WebString::fromUTF8(media_log_->GetLastErrorMessage());
646}
647
[email protected]02022fc2014-05-16 00:05:31648blink::WebTimeRanges WebMediaPlayerImpl::buffered() const {
acolwellb4034942014-08-28 15:42:43649 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]779a8322014-08-22 21:28:37650
acolwell9e0840d2014-09-06 19:01:32651 Ranges<base::TimeDelta> buffered_time_ranges =
[email protected]02022fc2014-05-16 00:05:31652 pipeline_.GetBufferedTimeRanges();
[email protected]779a8322014-08-22 21:28:37653
654 const base::TimeDelta duration = pipeline_.GetMediaDuration();
acolwell9e0840d2014-09-06 19:01:32655 if (duration != kInfiniteDuration()) {
[email protected]779a8322014-08-22 21:28:37656 buffered_data_source_host_.AddBufferedTimeRanges(
657 &buffered_time_ranges, duration);
658 }
[email protected]02022fc2014-05-16 00:05:31659 return ConvertToWebTimeRanges(buffered_time_ranges);
660}
661
philipjb0e6f3f2014-09-30 09:51:53662blink::WebTimeRanges WebMediaPlayerImpl::seekable() const {
acolwellb4034942014-08-28 15:42:43663 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]39bdde32013-04-17 17:44:20664
dalecurtis56359cb2014-10-28 00:06:29665 if (ready_state_ < WebMediaPlayer::ReadyStateHaveMetadata)
philipjb0e6f3f2014-09-30 09:51:53666 return blink::WebTimeRanges();
667
dalecurtis56359cb2014-10-28 00:06:29668 const double seekable_end = duration();
669
670 // Allow a special exception for seeks to zero for streaming sources with a
671 // finite duration; this allows looping to work.
672 const bool allow_seek_to_zero = data_source_ && data_source_->IsStreaming() &&
mateuszs3371ab02015-04-24 13:20:23673 std::isfinite(seekable_end);
dalecurtis56359cb2014-10-28 00:06:29674
675 // TODO(dalecurtis): Technically this allows seeking on media which return an
676 // infinite duration so long as DataSource::IsStreaming() is false. While not
677 // expected, disabling this breaks semi-live players, http://crbug.com/427412.
678 const blink::WebTimeRange seekable_range(
679 0.0, allow_seek_to_zero ? 0.0 : seekable_end);
philipjb0e6f3f2014-09-30 09:51:53680 return blink::WebTimeRanges(&seekable_range, 1);
[email protected]ec9212f2008-12-18 21:40:36681}
682
[email protected]5d2b3e4c2014-05-12 23:27:30683bool WebMediaPlayerImpl::didLoadingProgress() {
acolwellb4034942014-08-28 15:42:43684 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]6683e1b2014-04-10 01:45:38685 bool pipeline_progress = pipeline_.DidLoadingProgress();
686 bool data_progress = buffered_data_source_host_.DidLoadingProgress();
687 return pipeline_progress || data_progress;
[email protected]d43ed912009-02-03 04:52:53688}
689
[email protected]dd5c7972014-08-21 15:00:37690void WebMediaPlayerImpl::paint(blink::WebCanvas* canvas,
691 const blink::WebRect& rect,
[email protected]dd5c7972014-08-21 15:00:37692 unsigned char alpha,
693 SkXfermode::Mode mode) {
acolwellb4034942014-08-28 15:42:43694 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]dd061e12014-05-06 19:21:22695 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint");
[email protected]4e6be3f2009-05-07 02:24:44696
xhwang80739452016-01-13 00:48:00697 if (is_cdm_attached_)
698 return;
699
mcasasf1236fc22015-05-29 22:38:56700 scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor();
[email protected]0fe5d43b2014-01-24 23:32:45701
[email protected]b49beeb2013-03-01 20:04:00702 gfx::Rect gfx_rect(rect);
dongseong.hwang0c4e9d82015-01-08 20:11:13703 Context3D context_3d;
mcasas265bdbf82015-06-12 18:44:07704 if (video_frame.get() && video_frame->HasTextures()) {
mcasasf1236fc22015-05-29 22:38:56705 if (!context_3d_cb_.is_null())
dongseong.hwang0c4e9d82015-01-08 20:11:13706 context_3d = context_3d_cb_.Run();
dongseong.hwang0c4e9d82015-01-08 20:11:13707 // GPU Process crashed.
708 if (!context_3d.gl)
709 return;
710 }
danakj795f1732015-08-31 23:40:22711 skcanvas_video_renderer_.Paint(video_frame, canvas, gfx::RectF(gfx_rect),
712 alpha, mode, pipeline_metadata_.video_rotation,
713 context_3d);
[email protected]ec9212f2008-12-18 21:40:36714}
[email protected]5df51652009-01-17 00:03:00715
[email protected]38259a7a82009-07-29 21:49:49716bool WebMediaPlayerImpl::hasSingleSecurityOrigin() const {
[email protected]b49beeb2013-03-01 20:04:00717 if (data_source_)
718 return data_source_->HasSingleOrigin();
[email protected]fcdb7462009-10-21 21:05:11719 return true;
[email protected]38259a7a82009-07-29 21:49:49720}
721
[email protected]3fe27112012-06-07 04:00:01722bool WebMediaPlayerImpl::didPassCORSAccessCheck() const {
[email protected]b49beeb2013-03-01 20:04:00723 if (data_source_)
724 return data_source_->DidPassCORSAccessCheck();
725 return false;
[email protected]3fe27112012-06-07 04:00:01726}
727
[email protected]39bdde32013-04-17 17:44:20728double WebMediaPlayerImpl::mediaTimeForTimeValue(double timeValue) const {
qinminb4c39782015-08-10 18:43:24729 return base::TimeDelta::FromSecondsD(timeValue).InSecondsF();
[email protected]e06e16d82011-05-26 22:13:33730}
731
[email protected]d82b18ae2011-03-23 21:28:59732unsigned WebMediaPlayerImpl::decodedFrameCount() const {
acolwellb4034942014-08-28 15:42:43733 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]4c51bc662011-02-16 02:03:16734
acolwell9e0840d2014-09-06 19:01:32735 PipelineStatistics stats = pipeline_.GetStatistics();
[email protected]4c51bc662011-02-16 02:03:16736 return stats.video_frames_decoded;
737}
738
[email protected]d82b18ae2011-03-23 21:28:59739unsigned WebMediaPlayerImpl::droppedFrameCount() const {
acolwellb4034942014-08-28 15:42:43740 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]4c51bc662011-02-16 02:03:16741
acolwell9e0840d2014-09-06 19:01:32742 PipelineStatistics stats = pipeline_.GetStatistics();
[email protected]dd061e12014-05-06 19:21:22743 return stats.video_frames_dropped;
[email protected]4c51bc662011-02-16 02:03:16744}
745
chakshu.a94326b72016-03-08 05:11:44746size_t WebMediaPlayerImpl::audioDecodedByteCount() const {
acolwellb4034942014-08-28 15:42:43747 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]4c51bc662011-02-16 02:03:16748
acolwell9e0840d2014-09-06 19:01:32749 PipelineStatistics stats = pipeline_.GetStatistics();
[email protected]4c51bc662011-02-16 02:03:16750 return stats.audio_bytes_decoded;
751}
752
chakshu.a94326b72016-03-08 05:11:44753size_t WebMediaPlayerImpl::videoDecodedByteCount() const {
acolwellb4034942014-08-28 15:42:43754 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]4c51bc662011-02-16 02:03:16755
acolwell9e0840d2014-09-06 19:01:32756 PipelineStatistics stats = pipeline_.GetStatistics();
[email protected]4c51bc662011-02-16 02:03:16757 return stats.video_bytes_decoded;
758}
759
[email protected]6523b242013-03-13 11:10:07760bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture(
danakj46070b02016-03-28 21:54:11761 gpu::gles2::GLES2Interface* gl,
zmo57d577a2015-10-30 18:28:59762 unsigned int texture,
763 unsigned int internal_format,
764 unsigned int type,
765 bool premultiply_alpha,
766 bool flip_y) {
[email protected]bfc05f22013-10-19 17:55:16767 TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture");
768
mcasasf1236fc22015-05-29 22:38:56769 scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor();
[email protected]dd061e12014-05-06 19:21:22770
mcasas265bdbf82015-06-12 18:44:07771 if (!video_frame.get() || !video_frame->HasTextures() ||
dcastagna31a65d432015-06-11 19:01:00772 media::VideoFrame::NumPlanes(video_frame->format()) != 1) {
[email protected]e56f88c72013-06-25 22:31:29773 return false;
dongseong.hwang0c4e9d82015-01-08 20:11:13774 }
[email protected]df41e252014-02-03 23:39:50775
dcastagna31a65d432015-06-11 19:01:00776 SkCanvasVideoRenderer::CopyVideoFrameSingleTextureToGLTexture(
zmo57d577a2015-10-30 18:28:59777 gl, video_frame.get(), texture, internal_format, type, premultiply_alpha,
778 flip_y);
[email protected]e56f88c72013-06-25 22:31:29779 return true;
[email protected]6523b242013-03-13 11:10:07780}
781
[email protected]7bce1832014-01-09 00:01:22782void WebMediaPlayerImpl::setContentDecryptionModule(
[email protected]9ebc3b03f2014-08-13 04:01:23783 blink::WebContentDecryptionModule* cdm,
784 blink::WebContentDecryptionModuleResult result) {
acolwellb4034942014-08-28 15:42:43785 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]9ebc3b03f2014-08-13 04:01:23786
jrummell06f27072015-06-08 18:12:38787 // Once the CDM is set it can't be cleared as there may be frames being
788 // decrypted on other threads. So fail this request.
789 // http://crbug.com/462365#c7.
xhwang97de4202014-11-25 08:44:01790 if (!cdm) {
791 result.completeWithError(
jrummell06f27072015-06-08 18:12:38792 blink::WebContentDecryptionModuleExceptionInvalidStateError, 0,
793 "The existing MediaKeys object cannot be removed at this time.");
xhwang97de4202014-11-25 08:44:01794 return;
795 }
796
jrummell89e61d82015-07-23 20:03:34797 // Create a local copy of |result| to avoid problems with the callback
798 // getting passed to the media thread and causing |result| to be destructed
jrummell4acbec912016-02-27 02:45:03799 // on the wrong thread in some failure conditions. Blink should prevent
800 // multiple simultaneous calls.
801 DCHECK(!set_cdm_result_);
jrummell89e61d82015-07-23 20:03:34802 set_cdm_result_.reset(new blink::WebContentDecryptionModuleResult(result));
803
804 SetCdm(BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnCdmAttached),
xhwang9bd8c732015-04-13 23:27:53805 ToWebContentDecryptionModuleImpl(cdm)->GetCdmContext());
xhwang97de4202014-11-25 08:44:01806}
807
xhwange8c4181a2014-12-06 08:10:01808void WebMediaPlayerImpl::OnEncryptedMediaInitData(
jrummellcf78967c2015-04-03 05:03:58809 EmeInitDataType init_data_type,
Avi Drissman97785ea2015-12-19 01:11:31810 const std::vector<uint8_t>& init_data) {
jrummellcf78967c2015-04-03 05:03:58811 DCHECK(init_data_type != EmeInitDataType::UNKNOWN);
xhwangbab66f52014-12-02 23:49:50812
ddorwin301ccdb2016-02-25 02:39:17813 // TODO(xhwang): Update this UMA name. https://crbug.com/589251
xhwangbab66f52014-12-02 23:49:50814 UMA_HISTOGRAM_COUNTS("Media.EME.NeedKey", 1);
815
srirama.m26f864d02015-07-14 05:21:46816 encrypted_client_->encrypted(
davidbenb50f00c2015-12-01 00:01:50817 ConvertToWebInitDataType(init_data_type), init_data.data(),
srirama.m26f864d02015-07-14 05:21:46818 base::saturated_cast<unsigned int>(init_data.size()));
xhwangbab66f52014-12-02 23:49:50819}
820
servolk81e01e02016-03-05 03:29:15821void WebMediaPlayerImpl::OnFFmpegMediaTracksUpdated(
dcheng3076abbf2016-04-22 20:42:39822 std::unique_ptr<MediaTracks> tracks) {
servolk81e01e02016-03-05 03:29:15823 // For MSE/chunk_demuxer case the media track updates are handled by
824 // WebSourceBufferImpl.
825 DCHECK(demuxer_.get());
826 DCHECK(!chunk_demuxer_);
servolkef1e5ef2016-03-25 04:55:26827
828 // Report the media track information to blink.
829 for (const auto& track : tracks->tracks()) {
830 if (track->type() == MediaTrack::Audio) {
831 auto track_id = client_->addAudioTrack(
832 blink::WebString::fromUTF8(track->id()),
833 blink::WebMediaPlayerClient::AudioTrackKindMain,
834 blink::WebString::fromUTF8(track->label()),
835 blink::WebString::fromUTF8(track->language()),
836 /*enabled*/ true);
Per515ae3172016-04-18 10:29:35837 (void)track_id;
servolkef1e5ef2016-03-25 04:55:26838 } else if (track->type() == MediaTrack::Video) {
839 auto track_id = client_->addVideoTrack(
840 blink::WebString::fromUTF8(track->id()),
841 blink::WebMediaPlayerClient::VideoTrackKindMain,
842 blink::WebString::fromUTF8(track->label()),
843 blink::WebString::fromUTF8(track->language()),
844 /*selected*/ true);
Per515ae3172016-04-18 10:29:35845 (void)track_id;
servolkef1e5ef2016-03-25 04:55:26846 } else {
847 // Text tracks are not supported through this code path yet.
848 NOTREACHED();
849 }
850 }
servolk81e01e02016-03-05 03:29:15851}
852
jrummell74fc4f942015-03-02 22:48:27853void WebMediaPlayerImpl::OnWaitingForDecryptionKey() {
srirama.m26f864d02015-07-14 05:21:46854 encrypted_client_->didBlockPlaybackWaitingForKey();
jrummell74fc4f942015-03-02 22:48:27855
856 // TODO(jrummell): didResumePlaybackBlockedForKey() should only be called
857 // when a key has been successfully added (e.g. OnSessionKeysChange() with
858 // |has_additional_usable_key| = true). http://crbug.com/461903
srirama.m26f864d02015-07-14 05:21:46859 encrypted_client_->didResumePlaybackBlockedForKey();
jrummell74fc4f942015-03-02 22:48:27860}
861
xhwang9bd8c732015-04-13 23:27:53862void WebMediaPlayerImpl::SetCdm(const CdmAttachedCB& cdm_attached_cb,
863 CdmContext* cdm_context) {
xhwang80739452016-01-13 00:48:00864 if (!cdm_context) {
865 cdm_attached_cb.Run(false);
866 return;
867 }
868
jrummell87a2db52015-05-05 22:27:18869 // If CDM initialization succeeded, tell the pipeline about it.
xhwang80739452016-01-13 00:48:00870 pipeline_.SetCdm(cdm_context, cdm_attached_cb);
xhwang97de4202014-11-25 08:44:01871}
872
jrummell89e61d82015-07-23 20:03:34873void WebMediaPlayerImpl::OnCdmAttached(bool success) {
xhwang97de4202014-11-25 08:44:01874 if (success) {
jrummell89e61d82015-07-23 20:03:34875 set_cdm_result_->complete();
876 set_cdm_result_.reset();
xhwang80739452016-01-13 00:48:00877 is_cdm_attached_ = true;
xhwang97de4202014-11-25 08:44:01878 return;
879 }
880
jrummell89e61d82015-07-23 20:03:34881 set_cdm_result_->completeWithError(
xhwang97de4202014-11-25 08:44:01882 blink::WebContentDecryptionModuleExceptionNotSupportedError, 0,
883 "Unable to set MediaKeys object");
jrummell89e61d82015-07-23 20:03:34884 set_cdm_result_.reset();
[email protected]9ebc3b03f2014-08-13 04:01:23885}
886
sandersd1c0bba02016-03-04 23:14:08887void WebMediaPlayerImpl::OnPipelineSeeked(bool time_updated) {
[email protected]5d11eff2011-09-15 00:06:06888 seeking_ = false;
wolenetz1b2ae7d2015-06-10 00:44:21889 seek_time_ = base::TimeDelta();
hubbe5a2dec022016-03-17 01:14:23890 if (paused_) {
891#if defined(OS_ANDROID) // WMPI_CAST
892 if (isRemote()) {
893 paused_time_ = base::TimeDelta::FromSecondsD(cast_impl_.currentTime());
894 } else {
895 paused_time_ = pipeline_.GetMediaTime();
896 }
897#else
sandersd1c0bba02016-03-04 23:14:08898 paused_time_ = pipeline_.GetMediaTime();
hubbe5a2dec022016-03-17 01:14:23899#endif
900 }
sandersd1c0bba02016-03-04 23:14:08901 if (time_updated)
902 should_notify_time_changed_ = true;
[email protected]8931c41a2009-07-07 17:31:49903}
904
sandersd1c0bba02016-03-04 23:14:08905void WebMediaPlayerImpl::OnPipelineSuspended() {
hubbed5f36882016-01-15 22:40:37906#if defined(OS_ANDROID)
907 if (isRemote()) {
908 scoped_refptr<VideoFrame> frame = cast_impl_.GetCastingBanner();
sandersd50a635e2016-04-04 22:50:09909 if (frame)
hubbed5f36882016-01-15 22:40:37910 compositor_->PaintFrameUsingOldRenderingPath(frame);
hubbed5f36882016-01-15 22:40:37911 }
912#endif
913
dalecurtis37fe5862016-03-15 19:29:09914 // If we're not in an aggressive buffering state, tell the data source we have
915 // enough data so that it may release the connection.
916 if (buffering_strategy_ !=
917 BufferedDataSource::BUFFERING_STRATEGY_AGGRESSIVE) {
liberato3f9f32b2016-03-16 16:54:51918 if (data_source_)
919 data_source_->OnBufferingHaveEnough(true);
dalecurtis37fe5862016-03-15 19:29:09920 }
921
sandersd50a635e2016-04-04 22:50:09922 ReportMemoryUsage();
923
sandersd1c0bba02016-03-04 23:14:08924 if (pending_suspend_resume_cycle_) {
watkdee516f2016-02-18 02:22:19925 pending_suspend_resume_cycle_ = false;
sandersd50a635e2016-04-04 22:50:09926 UpdatePlayState();
dalecurtis0431cbf2016-03-12 01:19:43927 }
sandersd1c0bba02016-03-04 23:14:08928}
929
[email protected]6954fe12013-01-03 03:22:32930void WebMediaPlayerImpl::OnPipelineEnded() {
[email protected]2a06ca62014-06-04 13:59:52931 DVLOG(1) << __FUNCTION__;
acolwellb4034942014-08-28 15:42:43932 DCHECK(main_task_runner_->BelongsToCurrentThread());
scherkusd2c745b2014-09-04 05:03:40933
sandersd1c0bba02016-03-04 23:14:08934 // Ignore state changes until we've completed all outstanding operations.
935 if (!pipeline_controller_.IsStable())
scherkusd2c745b2014-09-04 05:03:40936 return;
937
938 ended_ = true;
[email protected]ce70c982013-12-20 17:04:32939 client_->timeChanged();
sandersd50a635e2016-04-04 22:50:09940
941 // We don't actually want this to run until |client_| calls seek() or pause(),
942 // but that should have already happened in timeChanged() and so this is
943 // expected to be a no-op.
944 UpdatePlayState();
[email protected]576537842009-08-12 23:52:05945}
946
[email protected]a9590c22011-03-16 16:57:02947void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error) {
hubbed82bed52015-12-15 23:07:16948 DVLOG(1) << __FUNCTION__;
acolwellb4034942014-08-28 15:42:43949 DCHECK(main_task_runner_->BelongsToCurrentThread());
acolwell9e0840d2014-09-06 19:01:32950 DCHECK_NE(error, PIPELINE_OK);
[email protected]a8e2cb82012-08-17 00:02:39951
dalecurtis2ff781da2016-03-03 01:52:13952 if (suppress_destruction_errors_)
953 return;
954
955 media_log_->AddEvent(media_log_->CreatePipelineErrorEvent(error));
956
[email protected]a8e2cb82012-08-17 00:02:39957 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) {
958 // Any error that occurs before reaching ReadyStateHaveMetadata should
959 // be considered a format error.
960 SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
sandersd50a635e2016-04-04 22:50:09961 } else {
962 SetNetworkState(PipelineErrorToNetworkState(error));
[email protected]a8e2cb82012-08-17 00:02:39963 }
964
sandersd50a635e2016-04-04 22:50:09965 UpdatePlayState();
[email protected]db190487d2009-07-30 18:51:52966}
967
[email protected]b8877772014-03-26 20:17:15968void WebMediaPlayerImpl::OnPipelineMetadata(
acolwell9e0840d2014-09-06 19:01:32969 PipelineMetadata metadata) {
[email protected]2a06ca62014-06-04 13:59:52970 DVLOG(1) << __FUNCTION__;
[email protected]a8e2cb82012-08-17 00:02:39971
[email protected]b8877772014-03-26 20:17:15972 pipeline_metadata_ = metadata;
[email protected]739847c02014-01-16 00:12:25973
dalecurtis849cf4b22015-03-27 18:35:45974 UMA_HISTOGRAM_ENUMERATION("Media.VideoRotation", metadata.video_rotation,
acolwell9e0840d2014-09-06 19:01:32975 VIDEO_ROTATION_MAX + 1);
[email protected]b8877772014-03-26 20:17:15976 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
[email protected]21c3f7502013-03-23 03:29:51977
[email protected]b8877772014-03-26 20:17:15978 if (hasVideo()) {
979 DCHECK(!video_weblayer_);
loyso0940d412016-03-14 01:30:31980 scoped_refptr<cc::VideoLayer> layer =
981 cc::VideoLayer::Create(compositor_, pipeline_metadata_.video_rotation);
[email protected]f78c3e82014-08-08 01:24:47982
acolwell9e0840d2014-09-06 19:01:32983 if (pipeline_metadata_.video_rotation == VIDEO_ROTATION_90 ||
984 pipeline_metadata_.video_rotation == VIDEO_ROTATION_270) {
[email protected]f78c3e82014-08-08 01:24:47985 gfx::Size size = pipeline_metadata_.natural_size;
986 pipeline_metadata_.natural_size = gfx::Size(size.height(), size.width());
987 }
988
[email protected]38564622014-08-19 02:47:18989 video_weblayer_.reset(new cc_blink::WebLayerImpl(layer));
jbauman952274d2015-09-10 23:23:36990 video_weblayer_->layer()->SetContentsOpaque(opaque_);
991 video_weblayer_->SetContentsOpaqueIsFixed(true);
[email protected]a7c4f582014-04-16 18:44:49992 client_->setWebLayer(video_weblayer_.get());
[email protected]a8e2cb82012-08-17 00:02:39993 }
dalecurtis8e4dc682016-03-15 02:30:30994
sandersd50a635e2016-04-04 22:50:09995 UpdatePlayState();
[email protected]a8e2cb82012-08-17 00:02:39996}
997
[email protected]ba7d5f92014-06-24 05:37:40998void WebMediaPlayerImpl::OnPipelineBufferingStateChanged(
acolwell9e0840d2014-09-06 19:01:32999 BufferingState buffering_state) {
[email protected]ba7d5f92014-06-24 05:37:401000 DVLOG(1) << __FUNCTION__ << "(" << buffering_state << ")";
[email protected]b8877772014-03-26 20:17:151001
sandersd1c0bba02016-03-04 23:14:081002 // Ignore buffering state changes until we've completed all outstanding
1003 // operations.
1004 if (!pipeline_controller_.IsStable())
[email protected]ba7d5f92014-06-24 05:37:401005 return;
[email protected]b8877772014-03-26 20:17:151006
[email protected]ba7d5f92014-06-24 05:37:401007 // TODO(scherkus): Handle other buffering states when Pipeline starts using
1008 // them and translate them ready state changes http://crbug.com/144683
acolwell9e0840d2014-09-06 19:01:321009 DCHECK_EQ(buffering_state, BUFFERING_HAVE_ENOUGH);
[email protected]ba7d5f92014-06-24 05:37:401010 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
1011
dalecurtis849cf4b22015-03-27 18:35:451012 // Let the DataSource know we have enough data. It may use this information to
1013 // release unused network connections.
1014 if (data_source_)
dalecurtis37fe5862016-03-15 19:29:091015 data_source_->OnBufferingHaveEnough(false);
dalecurtis849cf4b22015-03-27 18:35:451016
[email protected]ba7d5f92014-06-24 05:37:401017 // Blink expects a timeChanged() in response to a seek().
1018 if (should_notify_time_changed_)
1019 client_->timeChanged();
dalecurtis0f0097a2015-12-01 17:40:471020
1021 // Once we have enough, start reporting the total memory usage. We'll also
1022 // report once playback starts.
1023 ReportMemoryUsage();
sandersd50a635e2016-04-04 22:50:091024
1025 UpdatePlayState();
[email protected]b8877772014-03-26 20:17:151026}
1027
[email protected]ef8394c2013-08-21 20:26:301028void WebMediaPlayerImpl::OnDemuxerOpened() {
acolwellb4034942014-08-28 15:42:431029 DCHECK(main_task_runner_->BelongsToCurrentThread());
wolenetz1fb319d2015-07-14 17:49:041030 client_->mediaSourceOpened(
1031 new WebMediaSourceImpl(chunk_demuxer_, media_log_));
[email protected]81bb3322011-07-21 15:55:501032}
1033
[email protected]8a561062013-11-22 01:19:311034void WebMediaPlayerImpl::OnAddTextTrack(
acolwell9e0840d2014-09-06 19:01:321035 const TextTrackConfig& config,
1036 const AddTextTrackDoneCB& done_cb) {
acolwellb4034942014-08-28 15:42:431037 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]71537722013-05-23 06:47:531038
[email protected]8a561062013-11-22 01:19:311039 const WebInbandTextTrackImpl::Kind web_kind =
1040 static_cast<WebInbandTextTrackImpl::Kind>(config.kind());
1041 const blink::WebString web_label =
1042 blink::WebString::fromUTF8(config.label());
1043 const blink::WebString web_language =
1044 blink::WebString::fromUTF8(config.language());
[email protected]427ff102013-11-26 23:45:401045 const blink::WebString web_id =
1046 blink::WebString::fromUTF8(config.id());
[email protected]71537722013-05-23 06:47:531047
dcheng3076abbf2016-04-22 20:42:391048 std::unique_ptr<WebInbandTextTrackImpl> web_inband_text_track(
henriks9cce5fa2014-12-12 09:35:301049 new WebInbandTextTrackImpl(web_kind, web_label, web_language, web_id));
[email protected]8a561062013-11-22 01:19:311050
dcheng3076abbf2016-04-22 20:42:391051 std::unique_ptr<media::TextTrack> text_track(new TextTrackImpl(
dcheng652f5ff2015-12-27 08:54:001052 main_task_runner_, client_, std::move(web_inband_text_track)));
[email protected]8a561062013-11-22 01:19:311053
dcheng652f5ff2015-12-27 08:54:001054 done_cb.Run(std::move(text_track));
[email protected]71537722013-05-23 06:47:531055}
1056
dalecurtis0431cbf2016-03-12 01:19:431057void WebMediaPlayerImpl::OnHidden() {
sandersd1e49fb62015-12-12 01:18:061058 DCHECK(main_task_runner_->BelongsToCurrentThread());
sandersd50a635e2016-04-04 22:50:091059 UpdatePlayState();
sandersd1e49fb62015-12-12 01:18:061060}
1061
1062void WebMediaPlayerImpl::OnShown() {
1063 DCHECK(main_task_runner_->BelongsToCurrentThread());
sandersd50a635e2016-04-04 22:50:091064 must_suspend_ = false;
1065 UpdatePlayState();
sandersd1e49fb62015-12-12 01:18:061066}
1067
dalecurtis0431cbf2016-03-12 01:19:431068void WebMediaPlayerImpl::OnSuspendRequested(bool must_suspend) {
1069 DCHECK(main_task_runner_->BelongsToCurrentThread());
1070
sandersd50a635e2016-04-04 22:50:091071 if (must_suspend) {
1072 must_suspend_ = true;
1073 } else {
watk81727bf2016-04-12 00:25:131074 // TODO(sandersd): Remove this when idleness is separate from play state.
1075 if (delegate_state_ == DelegateState::PAUSED_BUT_NOT_IDLE)
sandersd5ea2ba4c2016-04-05 22:40:561076 return;
sandersd50a635e2016-04-04 22:50:091077 is_idle_ = true;
1078 }
1079
1080 UpdatePlayState();
dalecurtis0431cbf2016-03-12 01:19:431081}
1082
dalecurtisbb3eaac2016-01-27 21:10:251083void WebMediaPlayerImpl::OnPlay() {
1084 play();
1085 client_->playbackStateChanged();
1086}
1087
1088void WebMediaPlayerImpl::OnPause() {
1089 pause();
1090 client_->playbackStateChanged();
1091}
1092
1093void WebMediaPlayerImpl::OnVolumeMultiplierUpdate(double multiplier) {
1094 volume_multiplier_ = multiplier;
1095 setVolume(volume_);
1096}
1097
watkdee516f2016-02-18 02:22:191098void WebMediaPlayerImpl::ScheduleRestart() {
sandersd50a635e2016-04-04 22:50:091099 // TODO(watk): All restart logic should be moved into PipelineController.
1100 if (pipeline_.IsRunning() && !pipeline_controller_.IsPipelineSuspended()) {
watkdee516f2016-02-18 02:22:191101 pending_suspend_resume_cycle_ = true;
sandersd50a635e2016-04-04 22:50:091102 UpdatePlayState();
watkdee516f2016-02-18 02:22:191103 }
1104}
1105
hubbed5f36882016-01-15 22:40:371106#if defined(OS_ANDROID) // WMPI_CAST
hubbed5f36882016-01-15 22:40:371107bool WebMediaPlayerImpl::isRemote() const {
1108 return cast_impl_.isRemote();
1109}
1110
1111void WebMediaPlayerImpl::SetMediaPlayerManager(
1112 RendererMediaPlayerManagerInterface* media_player_manager) {
1113 cast_impl_.SetMediaPlayerManager(media_player_manager);
1114}
1115
1116void WebMediaPlayerImpl::requestRemotePlayback() {
1117 cast_impl_.requestRemotePlayback();
1118}
1119
1120void WebMediaPlayerImpl::requestRemotePlaybackControl() {
1121 cast_impl_.requestRemotePlaybackControl();
1122}
1123
1124void WebMediaPlayerImpl::OnRemotePlaybackEnded() {
1125 DVLOG(1) << __FUNCTION__;
1126 DCHECK(main_task_runner_->BelongsToCurrentThread());
1127
1128 ended_ = true;
1129 client_->timeChanged();
1130}
1131
1132void WebMediaPlayerImpl::OnDisconnectedFromRemoteDevice(double t) {
sandersd1c0bba02016-03-04 23:14:081133 DoSeek(base::TimeDelta::FromSecondsD(t), false);
hubbed5f36882016-01-15 22:40:371134
hubbed5f36882016-01-15 22:40:371135 // We already told the delegate we're paused when remoting started.
1136 client_->playbackStateChanged();
1137 client_->disconnectedFromRemoteDevice();
sandersd50a635e2016-04-04 22:50:091138
1139 UpdatePlayState();
hubbed5f36882016-01-15 22:40:371140}
1141
1142void WebMediaPlayerImpl::SuspendForRemote() {
sandersd50a635e2016-04-04 22:50:091143 if (pipeline_controller_.IsPipelineSuspended()) {
hubbed5f36882016-01-15 22:40:371144 scoped_refptr<VideoFrame> frame = cast_impl_.GetCastingBanner();
sandersd50a635e2016-04-04 22:50:091145 if (frame)
hubbed5f36882016-01-15 22:40:371146 compositor_->PaintFrameUsingOldRenderingPath(frame);
hubbed5f36882016-01-15 22:40:371147 }
sandersd50a635e2016-04-04 22:50:091148
1149 UpdatePlayState();
hubbed5f36882016-01-15 22:40:371150}
1151
1152gfx::Size WebMediaPlayerImpl::GetCanvasSize() const {
1153 if (!video_weblayer_)
1154 return pipeline_metadata_.natural_size;
1155
1156 return video_weblayer_->bounds();
1157}
1158
1159void WebMediaPlayerImpl::SetDeviceScaleFactor(float scale_factor) {
1160 cast_impl_.SetDeviceScaleFactor(scale_factor);
1161}
1162#endif // defined(OS_ANDROID) // WMPI_CAST
1163
[email protected]fee8a902014-06-03 13:43:361164void WebMediaPlayerImpl::DataSourceInitialized(bool success) {
hubbed82bed52015-12-15 23:07:161165 DVLOG(1) << __FUNCTION__;
acolwellb4034942014-08-28 15:42:431166 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]a9415292012-01-19 19:55:201167
[email protected]d250190da3b2012-07-23 22:57:301168 if (!success) {
[email protected]ef405f66b2012-04-18 02:39:551169 SetNetworkState(WebMediaPlayer::NetworkStateFormatError);
sandersd50a635e2016-04-04 22:50:091170
1171 // Not really necessary, since the pipeline was never started, but it at
1172 // least this makes sure that the error handling code is in sync.
1173 UpdatePlayState();
1174
[email protected]a9415292012-01-19 19:55:201175 return;
1176 }
1177
[email protected]ef8394c2013-08-21 20:26:301178 StartPipeline();
[email protected]a9415292012-01-19 19:55:201179}
1180
[email protected]122f40252012-06-12 05:01:561181void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) {
hubbed82bed52015-12-15 23:07:161182 DVLOG(1) << __FUNCTION__;
[email protected]122f40252012-06-12 05:01:561183 if (!is_downloading && network_state_ == WebMediaPlayer::NetworkStateLoading)
1184 SetNetworkState(WebMediaPlayer::NetworkStateIdle);
1185 else if (is_downloading && network_state_ == WebMediaPlayer::NetworkStateIdle)
1186 SetNetworkState(WebMediaPlayer::NetworkStateLoading);
1187 media_log_->AddEvent(
1188 media_log_->CreateBooleanEvent(
acolwell9e0840d2014-09-06 19:01:321189 MediaLogEvent::NETWORK_ACTIVITY_SET,
[email protected]122f40252012-06-12 05:01:561190 "is_downloading_data", is_downloading));
1191}
1192
watkdee516f2016-02-18 02:22:191193// TODO(watk): Move this state management out of WMPI.
1194void WebMediaPlayerImpl::OnSurfaceRequested(
1195 const SurfaceCreatedCB& surface_created_cb) {
1196 DCHECK(main_task_runner_->BelongsToCurrentThread());
1197 DCHECK(surface_manager_);
1198
1199 // A null callback indicates that the decoder is going away.
1200 if (surface_created_cb.is_null()) {
1201 decoder_requires_restart_for_fullscreen_ = false;
1202 return;
1203 }
1204
1205 // If we're getting a surface request it means GVD is initializing, so until
1206 // we get a null surface request, GVD is the active decoder. While that's the
1207 // case we should restart the pipeline on fullscreen transitions so that when
1208 // we create a new GVD it will request a surface again and get the right kind
1209 // of surface for the fullscreen state.
1210 // TODO(watk): Don't require a pipeline restart to switch surfaces for
1211 // cases where it isn't necessary.
1212 decoder_requires_restart_for_fullscreen_ = true;
1213 if (fullscreen_) {
1214 surface_manager_->CreateFullscreenSurface(pipeline_metadata_.natural_size,
1215 surface_created_cb);
1216 } else {
1217 // Tell the decoder to create its own surface.
1218 surface_created_cb.Run(SurfaceManager::kNoSurfaceID);
1219 }
1220}
1221
dcheng3076abbf2016-04-22 20:42:391222std::unique_ptr<Renderer> WebMediaPlayerImpl::CreateRenderer() {
watkdee516f2016-02-18 02:22:191223 RequestSurfaceCB request_surface_cb;
1224#if defined(OS_ANDROID)
1225 request_surface_cb =
1226 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnSurfaceRequested);
1227#endif
sandersd1e49fb62015-12-12 01:18:061228 return renderer_factory_->CreateRenderer(
1229 media_task_runner_, worker_task_runner_, audio_source_provider_.get(),
watkdee516f2016-02-18 02:22:191230 compositor_, request_surface_cb);
sandersd1e49fb62015-12-12 01:18:061231}
1232
[email protected]ef8394c2013-08-21 20:26:301233void WebMediaPlayerImpl::StartPipeline() {
acolwellb4034942014-08-28 15:42:431234 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]ddbc6ff2013-04-19 15:28:331235
xhwange8c4181a2014-12-06 08:10:011236 Demuxer::EncryptedMediaInitDataCB encrypted_media_init_data_cb =
1237 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnEncryptedMediaInitData);
[email protected]2b57e2e2014-05-09 11:07:251238
[email protected]ddbc6ff2013-04-19 15:28:331239 // Figure out which demuxer to use.
[email protected]ef8394c2013-08-21 20:26:301240 if (load_type_ != LoadTypeMediaSource) {
[email protected]ddbc6ff2013-04-19 15:28:331241 DCHECK(!chunk_demuxer_);
[email protected]f5443ef72013-04-22 04:03:381242 DCHECK(data_source_);
1243
j.isorcef6778e652015-11-16 17:14:251244#if !defined(MEDIA_DISABLE_FFMPEG)
servolk81e01e02016-03-05 03:29:151245 Demuxer::MediaTracksUpdatedCB media_tracks_updated_cb =
servolkef1e5ef2016-03-25 04:55:261246 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnFFmpegMediaTracksUpdated);
servolk81e01e02016-03-05 03:29:151247
xhwange8c4181a2014-12-06 08:10:011248 demuxer_.reset(new FFmpegDemuxer(media_task_runner_, data_source_.get(),
servolk81e01e02016-03-05 03:29:151249 encrypted_media_init_data_cb,
1250 media_tracks_updated_cb, media_log_));
j.isorcef6778e652015-11-16 17:14:251251#else
1252 OnPipelineError(PipelineStatus::DEMUXER_ERROR_COULD_NOT_OPEN);
1253 return;
1254#endif
[email protected]ddbc6ff2013-04-19 15:28:331255 } else {
[email protected]f5443ef72013-04-22 04:03:381256 DCHECK(!chunk_demuxer_);
1257 DCHECK(!data_source_);
1258
acolwell9e0840d2014-09-06 19:01:321259 chunk_demuxer_ = new ChunkDemuxer(
[email protected]ef8394c2013-08-21 20:26:301260 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDemuxerOpened),
wolenetz1fb319d2015-07-14 17:49:041261 encrypted_media_init_data_cb, media_log_, true);
[email protected]f5443ef72013-04-22 04:03:381262 demuxer_.reset(chunk_demuxer_);
[email protected]ddbc6ff2013-04-19 15:28:331263 }
1264
sandersdb5e21462016-03-09 01:49:071265 // TODO(sandersd): FileSystem objects may also be non-static, but due to our
1266 // caching layer such situations are broken already. http://crbug.com/593159
1267 bool is_static = !chunk_demuxer_;
1268
[email protected]f5443ef72013-04-22 04:03:381269 // ... and we're ready to go!
[email protected]d228aeec2014-06-20 19:16:491270 seeking_ = true;
xhwangf94a634d2014-10-22 22:07:271271
sandersd1e49fb62015-12-12 01:18:061272 // TODO(sandersd): On Android, defer Start() if the tab is not visible.
sandersd1c0bba02016-03-04 23:14:081273 bool is_streaming = (data_source_ && data_source_->IsStreaming());
1274 pipeline_controller_.Start(
sandersdb5e21462016-03-09 01:49:071275 demuxer_.get(), is_streaming, is_static,
[email protected]f5443ef72013-04-22 04:03:381276 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineEnded),
[email protected]b8877772014-03-26 20:17:151277 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineMetadata),
[email protected]ba7d5f92014-06-24 05:37:401278 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged),
xhwang59805902014-08-29 01:44:151279 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDurationChanged),
jrummell74fc4f942015-03-02 22:48:271280 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnAddTextTrack),
1281 BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnWaitingForDecryptionKey));
[email protected]f5443ef72013-04-22 04:03:381282}
1283
1284void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state) {
[email protected]2a06ca62014-06-04 13:59:521285 DVLOG(1) << __FUNCTION__ << "(" << state << ")";
acolwellb4034942014-08-28 15:42:431286 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]f5443ef72013-04-22 04:03:381287 network_state_ = state;
1288 // Always notify to ensure client has the latest value.
[email protected]ce70c982013-12-20 17:04:321289 client_->networkStateChanged();
[email protected]f5443ef72013-04-22 04:03:381290}
1291
1292void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state) {
[email protected]2a06ca62014-06-04 13:59:521293 DVLOG(1) << __FUNCTION__ << "(" << state << ")";
acolwellb4034942014-08-28 15:42:431294 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]f5443ef72013-04-22 04:03:381295
[email protected]fee8a902014-06-03 13:43:361296 if (state == WebMediaPlayer::ReadyStateHaveEnoughData && data_source_ &&
1297 data_source_->assume_fully_buffered() &&
[email protected]f5443ef72013-04-22 04:03:381298 network_state_ == WebMediaPlayer::NetworkStateLoading)
1299 SetNetworkState(WebMediaPlayer::NetworkStateLoaded);
1300
1301 ready_state_ = state;
sandersd50a635e2016-04-04 22:50:091302 highest_ready_state_ = std::max(highest_ready_state_, ready_state_);
1303
[email protected]f5443ef72013-04-22 04:03:381304 // Always notify to ensure client has the latest value.
[email protected]ce70c982013-12-20 17:04:321305 client_->readyStateChanged();
[email protected]f5443ef72013-04-22 04:03:381306}
1307
Dana Jansens71331252016-03-09 20:57:221308blink::WebAudioSourceProvider* WebMediaPlayerImpl::getAudioSourceProvider() {
[email protected]ff875be52013-06-02 23:47:381309 return audio_source_provider_.get();
[email protected]f5443ef72013-04-22 04:03:381310}
1311
[email protected]f5443ef72013-04-22 04:03:381312double WebMediaPlayerImpl::GetPipelineDuration() const {
[email protected]f6af7592014-02-28 10:09:111313 base::TimeDelta duration = pipeline_.GetMediaDuration();
[email protected]f5443ef72013-04-22 04:03:381314
1315 // Return positive infinity if the resource is unbounded.
1316 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
acolwell9e0840d2014-09-06 19:01:321317 if (duration == kInfiniteDuration())
[email protected]f5443ef72013-04-22 04:03:381318 return std::numeric_limits<double>::infinity();
1319
1320 return duration.InSecondsF();
1321}
1322
[email protected]75e145a2014-04-15 17:44:321323void WebMediaPlayerImpl::OnDurationChanged() {
sandersd50a635e2016-04-04 22:50:091324 // TODO(sandersd): We should call delegate_->DidPlay() with the new duration,
1325 // especially if it changed from <5s to >5s.
[email protected]f5443ef72013-04-22 04:03:381326 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
1327 return;
1328
[email protected]ce70c982013-12-20 17:04:321329 client_->durationChanged();
[email protected]f5443ef72013-04-22 04:03:381330}
1331
[email protected]75e145a2014-04-15 17:44:321332void WebMediaPlayerImpl::OnNaturalSizeChanged(gfx::Size size) {
acolwellb4034942014-08-28 15:42:431333 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]739847c02014-01-16 00:12:251334 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
[email protected]739847c02014-01-16 00:12:251335
alokp810dee42016-04-22 20:19:531336 if (size == pipeline_metadata_.natural_size)
1337 return;
1338
1339 TRACE_EVENT0("media", "WebMediaPlayerImpl::OnNaturalSizeChanged");
[email protected]739847c02014-01-16 00:12:251340 media_log_->AddEvent(
1341 media_log_->CreateVideoSizeSetEvent(size.width(), size.height()));
[email protected]739847c02014-01-16 00:12:251342
alokp810dee42016-04-22 20:19:531343 if (fullscreen_ && surface_manager_)
watkdee516f2016-02-18 02:22:191344 surface_manager_->NaturalSizeChanged(size);
watkdee516f2016-02-18 02:22:191345
1346 pipeline_metadata_.natural_size = size;
[email protected]739847c02014-01-16 00:12:251347 client_->sizeChanged();
1348}
1349
[email protected]75e145a2014-04-15 17:44:321350void WebMediaPlayerImpl::OnOpacityChanged(bool opaque) {
acolwellb4034942014-08-28 15:42:431351 DCHECK(main_task_runner_->BelongsToCurrentThread());
[email protected]75e145a2014-04-15 17:44:321352 DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing);
1353
1354 opaque_ = opaque;
jbauman952274d2015-09-10 23:23:361355 // Modify content opaqueness of cc::Layer directly so that
1356 // SetContentsOpaqueIsFixed is ignored.
[email protected]75e145a2014-04-15 17:44:321357 if (video_weblayer_)
jbauman952274d2015-09-10 23:23:361358 video_weblayer_->layer()->SetContentsOpaque(opaque_);
[email protected]75e145a2014-04-15 17:44:321359}
1360
[email protected]dd061e12014-05-06 19:21:221361static void GetCurrentFrameAndSignal(
1362 VideoFrameCompositor* compositor,
acolwell9e0840d2014-09-06 19:01:321363 scoped_refptr<VideoFrame>* video_frame_out,
[email protected]dd061e12014-05-06 19:21:221364 base::WaitableEvent* event) {
1365 TRACE_EVENT0("media", "GetCurrentFrameAndSignal");
dalecurtis44ce4de2015-05-11 18:42:071366 *video_frame_out = compositor->GetCurrentFrameAndUpdateIfStale();
[email protected]dd061e12014-05-06 19:21:221367 event->Signal();
1368}
1369
acolwell9e0840d2014-09-06 19:01:321370scoped_refptr<VideoFrame>
[email protected]dd061e12014-05-06 19:21:221371WebMediaPlayerImpl::GetCurrentFrameFromCompositor() {
1372 TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor");
1373 if (compositor_task_runner_->BelongsToCurrentThread())
dalecurtis44ce4de2015-05-11 18:42:071374 return compositor_->GetCurrentFrameAndUpdateIfStale();
[email protected]dd061e12014-05-06 19:21:221375
1376 // Use a posted task and waitable event instead of a lock otherwise
1377 // WebGL/Canvas can see different content than what the compositor is seeing.
acolwell9e0840d2014-09-06 19:01:321378 scoped_refptr<VideoFrame> video_frame;
[email protected]dd061e12014-05-06 19:21:221379 base::WaitableEvent event(false, false);
1380 compositor_task_runner_->PostTask(FROM_HERE,
1381 base::Bind(&GetCurrentFrameAndSignal,
1382 base::Unretained(compositor_),
1383 &video_frame,
1384 &event));
1385 event.Wait();
1386 return video_frame;
1387}
1388
sandersd50a635e2016-04-04 22:50:091389void WebMediaPlayerImpl::UpdatePlayState() {
hubbed5f36882016-01-15 22:40:371390#if defined(OS_ANDROID) // WMPI_CAST
sandersd50a635e2016-04-04 22:50:091391 bool is_remote = isRemote();
1392#else
1393 bool is_remote = false;
hubbed5f36882016-01-15 22:40:371394#endif
sandersd50a635e2016-04-04 22:50:091395 bool is_backgrounded =
1396 IsBackgroundedSuspendEnabled() && delegate_ && delegate_->IsHidden();
1397 PlayState state =
1398 UpdatePlayState_ComputePlayState(is_remote, is_backgrounded);
1399 SetDelegateState(state.delegate_state);
1400 SetMemoryReportingState(state.is_memory_reporting_enabled);
1401 SetSuspendState(state.is_suspended || pending_suspend_resume_cycle_);
1402}
dalecurtis5bbc487e2016-02-27 04:15:051403
sandersd50a635e2016-04-04 22:50:091404void WebMediaPlayerImpl::SetDelegateState(DelegateState new_state) {
1405 if (!delegate_ || delegate_state_ == new_state)
dalecurtis5bbc487e2016-02-27 04:15:051406 return;
1407
sandersd50a635e2016-04-04 22:50:091408 delegate_state_ = new_state;
1409
1410 switch (delegate_state_) {
1411 case DelegateState::GONE:
1412 delegate_->PlayerGone(delegate_id_);
1413 break;
1414 case DelegateState::PLAYING:
1415 delegate_->DidPlay(delegate_id_, hasVideo(), hasAudio(), false,
1416 pipeline_.GetMediaDuration());
1417 break;
1418 case DelegateState::PAUSED:
1419 delegate_->DidPause(delegate_id_, false);
1420 break;
watk81727bf2016-04-12 00:25:131421 case DelegateState::PAUSED_BUT_NOT_IDLE:
sandersd5ea2ba4c2016-04-05 22:40:561422 // It doesn't really matter what happens when we enter this state, only
1423 // that we reset the idle timer when leaving it.
1424 //
watk81727bf2016-04-12 00:25:131425 // TODO(sandersd): Ideally the delegate would consider idleness and play
1426 // state as orthogonal properties so that we could avoid this.
sandersd5ea2ba4c2016-04-05 22:40:561427 delegate_->DidPause(delegate_id_, false);
1428 break;
sandersd50a635e2016-04-04 22:50:091429 case DelegateState::ENDED:
1430 delegate_->DidPause(delegate_id_, true);
1431 break;
dalecurtis0f0097a2015-12-01 17:40:471432 }
1433}
1434
sandersd50a635e2016-04-04 22:50:091435void WebMediaPlayerImpl::SetMemoryReportingState(
1436 bool is_memory_reporting_enabled) {
1437 if (memory_usage_reporting_timer_.IsRunning() ==
1438 is_memory_reporting_enabled) {
hubbed5f36882016-01-15 22:40:371439 return;
sandersd50a635e2016-04-04 22:50:091440 }
sandersd1c0bba02016-03-04 23:14:081441
sandersd50a635e2016-04-04 22:50:091442 if (is_memory_reporting_enabled) {
1443 memory_usage_reporting_timer_.Start(FROM_HERE,
1444 base::TimeDelta::FromSeconds(2), this,
1445 &WebMediaPlayerImpl::ReportMemoryUsage);
1446 } else {
1447 memory_usage_reporting_timer_.Stop();
1448 ReportMemoryUsage();
1449 }
1450}
1451
1452void WebMediaPlayerImpl::SetSuspendState(bool is_suspended) {
1453 // Do not change the state after an error has occurred.
1454 // TODO(sandersd): Update PipelineController to remove the need for this.
1455 if (IsNetworkStateError(network_state_))
sandersd1c0bba02016-03-04 23:14:081456 return;
1457
dalecurtis7f366b2242016-04-13 01:16:171458#if defined(OS_MACOSX) || defined(OS_WIN)
1459 // TODO(sandersd): Idle suspend is disabled on OSX and Windows for hardware
1460 // decoding / opaque video frames since these frames are owned by the decoder
1461 // in the GPU process. http://crbug.com/595716 and http://crbug.com/602708
1462 if (can_suspend_state_ == CanSuspendState::UNKNOWN) {
1463 scoped_refptr<VideoFrame> frame = GetCurrentFrameFromCompositor();
1464 if (frame) {
1465 can_suspend_state_ =
1466 frame->metadata()->IsTrue(VideoFrameMetadata::DECODER_OWNS_FRAME)
1467 ? CanSuspendState::NO
1468 : CanSuspendState::YES;
1469 }
1470 }
1471#else
1472 can_suspend_state_ = CanSuspendState::YES;
1473#endif
1474
1475 if (can_suspend_state_ == CanSuspendState::NO)
1476 return;
1477
sandersd50a635e2016-04-04 22:50:091478 if (is_suspended) {
1479 pipeline_controller_.Suspend();
1480 } else {
1481 pipeline_controller_.Resume();
1482 }
1483}
1484
1485WebMediaPlayerImpl::PlayState
1486WebMediaPlayerImpl::UpdatePlayState_ComputePlayState(bool is_remote,
1487 bool is_backgrounded) {
1488 PlayState result;
1489
1490 // This includes both data source (before pipeline startup) and pipeline
1491 // errors.
1492 bool has_error = IsNetworkStateError(network_state_);
1493
1494 // After HaveMetadata, we know which tracks are present and the duration.
1495 bool have_metadata = ready_state_ >= WebMediaPlayer::ReadyStateHaveMetadata;
1496
1497 // After HaveFutureData, Blink will call play() if the state is not paused.
1498 bool have_future_data =
1499 highest_ready_state_ >= WebMediaPlayer::ReadyStateHaveFutureData;
1500
1501 // Background suspend is not enabled for audio-only players.
1502 bool background_suspended = is_backgrounded && have_metadata && hasVideo();
1503
1504 // Idle suspend is enabled once there is future data. We don't want to idle
1505 // suspend before that because play() may never be triggered to leave the idle
1506 // state. There could be other theoretical problems if the page is waiting for
1507 // other events before actually calling play(), but at least we don't break
1508 // Blink.
1509 //
1510 // TODO(sandersd): Make the delegate suspend idle players immediately when
1511 // hidden.
1512 // TODO(sandersd): If Blink told us the paused state sooner, we could
1513 // idle suspend sooner.
1514 bool idle_suspended = is_idle_ && have_future_data;
1515
1516 // Combined suspend state.
1517 result.is_suspended =
1518 is_remote || must_suspend_ || idle_suspended || background_suspended;
1519
1520 // We do not treat |playback_rate_| == 0 as paused. For the media session,
1521 // being paused implies displaying a play button, which is incorrect in this
1522 // case. For memory usage reporting, we just use the same definition (but we
1523 // don't have to).
1524 //
1525 // Similarly, we don't consider |ended_| to be paused. Blink will immediately
1526 // call pause() or seek(), so |ended_| should not affect the computation.
1527 // Despite that, |ended_| does result in a separate paused state, to simplfy
1528 // the contract for SetDelegateState().
1529 //
1530 // |has_session| is used to decide when to create a media session. Idle
1531 // suspension does not destroy the media session, because we expect that the
1532 // notification controls (and audio focus) remain. We also require:
1533 // - |have_metadata|, since the tracks and duration are passed to DidPlay().
1534 // - |have_future_data|, since we need to know whether we are paused to
1535 // correctly configure the session.
1536 //
1537 // TODO(sandersd): If Blink told us the paused state sooner, we could create
1538 // the media session sooner.
1539 bool can_play = !has_error && !is_remote && have_future_data;
1540 bool has_session = can_play && !must_suspend_ && !background_suspended;
1541
1542 if (!has_session) {
1543 result.delegate_state = DelegateState::GONE;
1544 } else if (paused_) {
watk81727bf2016-04-12 00:25:131545 if (seeking() || fullscreen_) {
1546 result.delegate_state = DelegateState::PAUSED_BUT_NOT_IDLE;
sandersd5ea2ba4c2016-04-05 22:40:561547 } else if (ended_) {
1548 result.delegate_state = DelegateState::ENDED;
1549 } else {
1550 result.delegate_state = DelegateState::PAUSED;
1551 }
sandersd50a635e2016-04-04 22:50:091552 } else {
1553 result.delegate_state = DelegateState::PLAYING;
1554 }
1555
1556 // It's not critical if some cases where memory usage can change are missed,
1557 // since media memory changes are usually gradual.
1558 result.is_memory_reporting_enabled =
1559 can_play && !result.is_suspended && !paused_;
1560
1561 return result;
dalecurtis0f0097a2015-12-01 17:40:471562}
1563
dalecurtis83266c72015-10-29 18:43:201564void WebMediaPlayerImpl::ReportMemoryUsage() {
1565 DCHECK(main_task_runner_->BelongsToCurrentThread());
1566
wdzierzanowskifd4cd91c52015-12-02 23:50:201567 // About base::Unretained() usage below: We destroy |demuxer_| on the main
1568 // thread. Before that, however, ~WebMediaPlayerImpl() posts a task to the
1569 // media thread and waits for it to finish. Hence, the GetMemoryUsage() task
1570 // posted here must finish earlier.
1571
1572 if (demuxer_) {
1573 base::PostTaskAndReplyWithResult(
1574 media_task_runner_.get(), FROM_HERE,
1575 base::Bind(&Demuxer::GetMemoryUsage, base::Unretained(demuxer_.get())),
1576 base::Bind(&WebMediaPlayerImpl::FinishMemoryUsageReport, AsWeakPtr()));
1577 } else {
1578 FinishMemoryUsageReport(0);
1579 }
1580}
1581
1582void WebMediaPlayerImpl::FinishMemoryUsageReport(int64_t demuxer_memory_usage) {
1583 DCHECK(main_task_runner_->BelongsToCurrentThread());
1584
dalecurtis83266c72015-10-29 18:43:201585 const PipelineStatistics stats = pipeline_.GetStatistics();
1586 const int64_t current_memory_usage =
1587 stats.audio_memory_usage + stats.video_memory_usage +
1588 (data_source_ ? data_source_->GetMemoryUsage() : 0) +
wdzierzanowskifd4cd91c52015-12-02 23:50:201589 demuxer_memory_usage;
dalecurtis83266c72015-10-29 18:43:201590
dalecurtis3a7d38f42016-03-07 21:17:131591 // Note, this isn't entirely accurate, there may be VideoFrames held by the
1592 // compositor or other resources that we're unaware of.
1593
dalecurtis83266c72015-10-29 18:43:201594 DVLOG(2) << "Memory Usage -- Audio: " << stats.audio_memory_usage
1595 << ", Video: " << stats.video_memory_usage << ", DataSource: "
1596 << (data_source_ ? data_source_->GetMemoryUsage() : 0)
wdzierzanowskifd4cd91c52015-12-02 23:50:201597 << ", Demuxer: " << demuxer_memory_usage;
dalecurtis83266c72015-10-29 18:43:201598
1599 const int64_t delta = current_memory_usage - last_reported_memory_usage_;
1600 last_reported_memory_usage_ = current_memory_usage;
1601 adjust_allocated_memory_cb_.Run(delta);
1602}
1603
acolwell9e0840d2014-09-06 19:01:321604} // namespace media