[email protected] | 85a37afd | 2013-05-30 22:51:15 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | 891acc9 | 2009-04-27 19:56:41 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 4 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 5 | #include "media/blink/webmediaplayer_impl.h" |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 6 | |
[email protected] | c2a45e24 | 2013-02-15 20:24:58 | [diff] [blame] | 7 | #include <algorithm> |
mateuszs | 3371ab0 | 2015-04-24 13:20:23 | [diff] [blame] | 8 | #include <cmath> |
[email protected] | 47b06ceb | 2010-08-04 22:41:11 | [diff] [blame] | 9 | #include <limits> |
guidou | c7babef | 2015-10-22 00:42:35 | [diff] [blame] | 10 | #include <string> |
dcheng | 652f5ff | 2015-12-27 08:54:00 | [diff] [blame] | 11 | #include <utility> |
[email protected] | 47b06ceb | 2010-08-04 22:41:11 | [diff] [blame] | 12 | |
[email protected] | 08273c7b | 2011-09-17 00:33:51 | [diff] [blame] | 13 | #include "base/bind.h" |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 14 | #include "base/bind_helpers.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 15 | #include "base/callback.h" |
[email protected] | 7502ef1 | 2014-01-25 01:19:27 | [diff] [blame] | 16 | #include "base/callback_helpers.h" |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 17 | #include "base/command_line.h" |
[email protected] | ca04095f | 2014-02-07 10:23:56 | [diff] [blame] | 18 | #include "base/debug/alias.h" |
[email protected] | 926f8fd | 2013-04-12 20:27:53 | [diff] [blame] | 19 | #include "base/debug/crash_logging.h" |
fdoray | db3ef7d | 2016-06-09 15:42:38 | [diff] [blame] | 20 | #include "base/location.h" |
servolk | f94b460 | 2017-01-31 16:44:27 | [diff] [blame] | 21 | #include "base/memory/ptr_util.h" |
asvitkine | 3033081 | 2016-08-30 04:01:08 | [diff] [blame] | 22 | #include "base/metrics/histogram_macros.h" |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 23 | #include "base/single_thread_task_runner.h" |
servolk | a233f83 | 2016-06-10 20:39:04 | [diff] [blame] | 24 | #include "base/strings/string_number_conversions.h" |
wdzierzanowski | fd4cd91c5 | 2015-12-02 23:50:20 | [diff] [blame] | 25 | #include "base/task_runner_util.h" |
gab | ba14a44 | 2016-05-11 20:10:20 | [diff] [blame] | 26 | #include "base/threading/thread_task_runner_handle.h" |
ssid | 9525f467 | 2015-01-28 12:13:15 | [diff] [blame] | 27 | #include "base/trace_event/trace_event.h" |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 28 | #include "build/build_config.h" |
[email protected] | 3856462 | 2014-08-19 02:47:18 | [diff] [blame] | 29 | #include "cc/blink/web_layer_impl.h" |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 30 | #include "cc/layers/video_layer.h" |
[email protected] | e4fc09e | 2012-04-06 03:17:44 | [diff] [blame] | 31 | #include "media/audio/null_audio_sink.h" |
[email protected] | 2eccbed | 2014-01-10 05:15:53 | [diff] [blame] | 32 | #include "media/base/bind_to_current_loop.h" |
xhwang | 0ad11e51 | 2014-11-25 23:43:09 | [diff] [blame] | 33 | #include "media/base/cdm_context.h" |
xhwang | 79b19304 | 2016-12-13 18:52:43 | [diff] [blame] | 34 | #include "media/base/content_decryption_module.h" |
[email protected] | 32da100 | 2010-03-03 21:57:35 | [diff] [blame] | 35 | #include "media/base/limits.h" |
zqzhang | 5d8eab7 | 2016-08-26 20:34:30 | [diff] [blame] | 36 | #include "media/base/media_content_type.h" |
[email protected] | 090f731 | 2011-08-05 23:26:40 | [diff] [blame] | 37 | #include "media/base/media_log.h" |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 38 | #include "media/base/media_switches.h" |
tguilbert | 25a4d11 | 2016-10-13 21:56:51 | [diff] [blame] | 39 | #include "media/base/media_url_demuxer.h" |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 40 | #include "media/base/text_renderer.h" |
watk | 9f9dfdc9 | 2015-09-04 21:33:29 | [diff] [blame] | 41 | #include "media/base/timestamp_constants.h" |
[email protected] | e81283bb | 2010-08-31 18:01:21 | [diff] [blame] | 42 | #include "media/base/video_frame.h" |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 43 | #include "media/blink/texttrack_impl.h" |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 44 | #include "media/blink/watch_time_reporter.h" |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 45 | #include "media/blink/webaudiosourceprovider_impl.h" |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 46 | #include "media/blink/webcontentdecryptionmodule_impl.h" |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 47 | #include "media/blink/webinbandtexttrack_impl.h" |
| 48 | #include "media/blink/webmediaplayer_delegate.h" |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 49 | #include "media/blink/webmediaplayer_util.h" |
| 50 | #include "media/blink/webmediasource_impl.h" |
[email protected] | efe7cd2 | 2012-09-12 23:55:01 | [diff] [blame] | 51 | #include "media/filters/chunk_demuxer.h" |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 52 | #include "media/filters/ffmpeg_demuxer.h" |
jrummell | c9d8e53 | 2015-02-26 18:38:19 | [diff] [blame] | 53 | #include "third_party/WebKit/public/platform/WebEncryptedMediaTypes.h" |
srirama.m | 26f864d0 | 2015-07-14 05:21:46 | [diff] [blame] | 54 | #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h" |
| 55 | #include "third_party/WebKit/public/platform/WebMediaPlayerEncryptedMediaClient.h" |
guidou | 9bfe4e2f | 2016-04-09 08:31:19 | [diff] [blame] | 56 | #include "third_party/WebKit/public/platform/WebMediaPlayerSource.h" |
[email protected] | 745746d | 2013-08-23 02:09:16 | [diff] [blame] | 57 | #include "third_party/WebKit/public/platform/WebMediaSource.h" |
[email protected] | c1088446 | 2013-05-30 00:22:09 | [diff] [blame] | 58 | #include "third_party/WebKit/public/platform/WebRect.h" |
mek | 966863c | 2016-02-04 23:39:05 | [diff] [blame] | 59 | #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" |
[email protected] | c1088446 | 2013-05-30 00:22:09 | [diff] [blame] | 60 | #include "third_party/WebKit/public/platform/WebSize.h" |
| 61 | #include "third_party/WebKit/public/platform/WebString.h" |
| 62 | #include "third_party/WebKit/public/platform/WebURL.h" |
tguilbert | 9881bc2 | 2016-10-27 03:13:41 | [diff] [blame] | 63 | #include "third_party/WebKit/public/web/WebDocument.h" |
xhwang | 0acca44b | 2015-06-18 00:43:31 | [diff] [blame] | 64 | #include "third_party/WebKit/public/web/WebFrame.h" |
[email protected] | 8050465 | 2014-04-18 04:41:50 | [diff] [blame] | 65 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 66 | #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 67 | #include "third_party/WebKit/public/web/WebView.h" |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 68 | |
dalecurtis | ea27a3ed | 2016-06-24 01:41:30 | [diff] [blame] | 69 | #if defined(OS_ANDROID) |
| 70 | #include "media/base/android/media_codec_util.h" |
| 71 | #endif |
| 72 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 73 | using blink::WebCanvas; |
| 74 | using blink::WebMediaPlayer; |
| 75 | using blink::WebRect; |
| 76 | using blink::WebSize; |
| 77 | using blink::WebString; |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 78 | using gpu::gles2::GLES2Interface; |
| 79 | |
danakj | 365175c | 2016-02-06 00:37:37 | [diff] [blame] | 80 | #define STATIC_ASSERT_ENUM(a, b) \ |
| 81 | static_assert(static_cast<int>(a) == static_cast<int>(b), \ |
| 82 | "mismatching enums: " #a) |
| 83 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 84 | namespace media { |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 85 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 86 | namespace { |
| 87 | |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 88 | // Limits the range of playback rate. |
| 89 | // |
| 90 | // TODO(kylep): Revisit these. |
| 91 | // |
| 92 | // Vista has substantially lower performance than XP or Windows7. If you speed |
| 93 | // up a video too much, it can't keep up, and rendering stops updating except on |
| 94 | // the time bar. For really high speeds, audio becomes a bottleneck and we just |
| 95 | // use up the data we have, which may not achieve the speed requested, but will |
| 96 | // not crash the tab. |
| 97 | // |
| 98 | // A very slow speed, ie 0.00000001x, causes the machine to lock up. (It seems |
| 99 | // like a busy loop). It gets unresponsive, although its not completely dead. |
| 100 | // |
| 101 | // Also our timers are not very accurate (especially for ogg), which becomes |
| 102 | // evident at low speeds and on Vista. Since other speeds are risky and outside |
| 103 | // the norms, we think 1/16x to 16x is a safe and useful range for now. |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 104 | const double kMinRate = 0.0625; |
| 105 | const double kMaxRate = 16.0; |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 106 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 107 | void SetSinkIdOnMediaThread(scoped_refptr<WebAudioSourceProviderImpl> sink, |
| 108 | const std::string& device_id, |
| 109 | const url::Origin& security_origin, |
olka | 68b6939 | 2016-04-01 11:42:12 | [diff] [blame] | 110 | const OutputDeviceStatusCB& callback) { |
| 111 | sink->SwitchOutputDevice(device_id, security_origin, callback); |
guidou | c7babef | 2015-10-22 00:42:35 | [diff] [blame] | 112 | } |
| 113 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 114 | bool IsBackgroundedSuspendEnabled() { |
dalecurtis | 0431cbf | 2016-03-12 01:19:43 | [diff] [blame] | 115 | #if !defined(OS_ANDROID) |
| 116 | // Suspend/Resume is only enabled by default on Android. |
| 117 | return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 118 | switches::kEnableMediaSuspend); |
| 119 | #else |
| 120 | return !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 121 | switches::kDisableMediaSuspend); |
| 122 | #endif |
| 123 | } |
| 124 | |
avayvod | 48a8be5 | 2016-08-04 19:52:50 | [diff] [blame] | 125 | bool IsResumeBackgroundVideosEnabled() { |
| 126 | return base::FeatureList::IsEnabled(kResumeBackgroundVideo); |
| 127 | } |
| 128 | |
avayvod | 39c10240 | 2016-11-23 21:43:13 | [diff] [blame] | 129 | bool IsBackgroundVideoTrackOptimizationEnabled() { |
| 130 | return base::FeatureList::IsEnabled(kBackgroundVideoTrackOptimization); |
| 131 | } |
| 132 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 133 | bool IsNetworkStateError(blink::WebMediaPlayer::NetworkState state) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 134 | bool result = state == blink::WebMediaPlayer::kNetworkStateFormatError || |
| 135 | state == blink::WebMediaPlayer::kNetworkStateNetworkError || |
| 136 | state == blink::WebMediaPlayer::kNetworkStateDecodeError; |
| 137 | DCHECK_EQ(state > blink::WebMediaPlayer::kNetworkStateLoaded, result); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 138 | return result; |
| 139 | } |
| 140 | |
sandersd | 2c47842 | 2016-08-02 01:19:25 | [diff] [blame] | 141 | gfx::Size GetRotatedVideoSize(VideoRotation rotation, gfx::Size natural_size) { |
| 142 | if (rotation == VIDEO_ROTATION_90 || rotation == VIDEO_ROTATION_270) |
| 143 | return gfx::Size(natural_size.height(), natural_size.width()); |
| 144 | return natural_size; |
| 145 | } |
| 146 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 147 | base::TimeDelta GetCurrentTimeInternal(WebMediaPlayerImpl* p_this) { |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 148 | // We wrap currentTime() instead of using pipeline_controller_.GetMediaTime() |
| 149 | // since there are a variety of cases in which that time is not accurate; |
| 150 | // e.g., while remoting and during a pause or seek. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 151 | return base::TimeDelta::FromSecondsD(p_this->CurrentTime()); |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 152 | } |
| 153 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 154 | // How much time must have elapsed since loading last progressed before we |
| 155 | // assume that the decoder will have had time to complete preroll. |
| 156 | constexpr base::TimeDelta kPrerollAttemptTimeout = |
watk | d026f79 | 2016-11-05 00:28:51 | [diff] [blame] | 157 | base::TimeDelta::FromSeconds(3); |
| 158 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 159 | } // namespace |
| 160 | |
[email protected] | 6683e1b | 2014-04-10 01:45:38 | [diff] [blame] | 161 | class BufferedDataSourceHostImpl; |
| 162 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 163 | STATIC_ASSERT_ENUM(WebMediaPlayer::kCORSModeUnspecified, |
danakj | 365175c | 2016-02-06 00:37:37 | [diff] [blame] | 164 | UrlData::CORS_UNSPECIFIED); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 165 | STATIC_ASSERT_ENUM(WebMediaPlayer::kCORSModeAnonymous, UrlData::CORS_ANONYMOUS); |
| 166 | STATIC_ASSERT_ENUM(WebMediaPlayer::kCORSModeUseCredentials, |
danakj | 365175c | 2016-02-06 00:37:37 | [diff] [blame] | 167 | UrlData::CORS_USE_CREDENTIALS); |
[email protected] | a5a0110 | 2012-06-06 17:01:24 | [diff] [blame] | 168 | |
[email protected] | 5b5bb9d | 2010-10-22 19:57:36 | [diff] [blame] | 169 | WebMediaPlayerImpl::WebMediaPlayerImpl( |
[email protected] | 35b2a97 | 2014-04-04 15:50:22 | [diff] [blame] | 170 | blink::WebLocalFrame* frame, |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 171 | blink::WebMediaPlayerClient* client, |
srirama.m | 26f864d0 | 2015-07-14 05:21:46 | [diff] [blame] | 172 | blink::WebMediaPlayerEncryptedMediaClient* encrypted_client, |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 173 | WebMediaPlayerDelegate* delegate, |
dcheng | 3076abbf | 2016-04-22 20:42:39 | [diff] [blame] | 174 | std::unique_ptr<RendererFactory> renderer_factory, |
hubbe | 5f0ad43b | 2015-12-14 20:57:26 | [diff] [blame] | 175 | linked_ptr<UrlIndex> url_index, |
[email protected] | e82b2bd | 2013-01-02 17:47:57 | [diff] [blame] | 176 | const WebMediaPlayerParams& params) |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 177 | : frame_(frame), |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 178 | delegate_state_(DelegateState::GONE), |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 179 | delegate_has_audio_(false), |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 180 | network_state_(WebMediaPlayer::kNetworkStateEmpty), |
| 181 | ready_state_(WebMediaPlayer::kReadyStateHaveNothing), |
| 182 | highest_ready_state_(WebMediaPlayer::kReadyStateHaveNothing), |
dalecurtis | b6e052f5 | 2016-08-25 00:35:55 | [diff] [blame] | 183 | preload_(MultibufferDataSource::AUTO), |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 184 | main_task_runner_(frame->LoadingTaskRunner()), |
acolwell | 755d12d | 2014-08-30 01:09:19 | [diff] [blame] | 185 | media_task_runner_(params.media_task_runner()), |
dcastagna | 617d086b | 2015-08-20 01:39:30 | [diff] [blame] | 186 | worker_task_runner_(params.worker_task_runner()), |
acolwell | 755d12d | 2014-08-30 01:09:19 | [diff] [blame] | 187 | media_log_(params.media_log()), |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 188 | pipeline_controller_( |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 189 | base::MakeUnique<PipelineImpl>(media_task_runner_, media_log_.get()), |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 190 | base::Bind(&WebMediaPlayerImpl::CreateRenderer, |
| 191 | base::Unretained(this)), |
| 192 | base::Bind(&WebMediaPlayerImpl::OnPipelineSeeked, AsWeakPtr()), |
| 193 | base::Bind(&WebMediaPlayerImpl::OnPipelineSuspended, AsWeakPtr()), |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 194 | base::Bind(&WebMediaPlayerImpl::OnBeforePipelineResume, AsWeakPtr()), |
| 195 | base::Bind(&WebMediaPlayerImpl::OnPipelineResumed, AsWeakPtr()), |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 196 | base::Bind(&WebMediaPlayerImpl::OnError, AsWeakPtr())), |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 197 | load_type_(kLoadTypeURL), |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 198 | opaque_(false), |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 199 | playback_rate_(0.0), |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 200 | paused_(true), |
avayvod | eb9098d | 2017-01-07 00:33:03 | [diff] [blame] | 201 | paused_when_hidden_(false), |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 202 | seeking_(false), |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 203 | pending_suspend_resume_cycle_(false), |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 204 | ended_(false), |
yoichio | 863bebf | 2016-03-04 07:56:58 | [diff] [blame] | 205 | should_notify_time_changed_(false), |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 206 | overlay_enabled_(false), |
| 207 | decoder_requires_restart_for_overlay_(false), |
[email protected] | 5badb08 | 2010-06-11 17:40:15 | [diff] [blame] | 208 | client_(client), |
srirama.m | 26f864d0 | 2015-07-14 05:21:46 | [diff] [blame] | 209 | encrypted_client_(encrypted_client), |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 210 | delegate_(delegate), |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 211 | delegate_id_(0), |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 212 | defer_load_cb_(params.defer_load_cb()), |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 213 | context_3d_cb_(params.context_3d_cb()), |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 214 | adjust_allocated_memory_cb_(params.adjust_allocated_memory_cb()), |
| 215 | last_reported_memory_usage_(0), |
[email protected] | 132dd57c | 2012-08-10 23:24:34 | [diff] [blame] | 216 | supports_save_(true), |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 217 | chunk_demuxer_(NULL), |
hubbe | 5f0ad43b | 2015-12-14 20:57:26 | [diff] [blame] | 218 | url_index_(url_index), |
Bartosz Fabianowski | 85a82381 | 2015-04-16 10:27:51 | [diff] [blame] | 219 | // Threaded compositing isn't enabled universally yet. |
fdoray | db3ef7d | 2016-06-09 15:42:38 | [diff] [blame] | 220 | compositor_task_runner_(params.compositor_task_runner() |
| 221 | ? params.compositor_task_runner() |
| 222 | : base::ThreadTaskRunnerHandle::Get()), |
alokp | 5d86e9b | 2016-05-17 20:20:41 | [diff] [blame] | 223 | compositor_(new VideoFrameCompositor(compositor_task_runner_)), |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 224 | #if defined(OS_ANDROID) // WMPI_CAST |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 225 | cast_impl_(this, client_, params.context_3d_cb()), |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 226 | #endif |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 227 | volume_(1.0), |
| 228 | volume_multiplier_(1.0), |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 229 | renderer_factory_(std::move(renderer_factory)), |
dalecurtis | 2ff781da | 2016-03-03 01:52:13 | [diff] [blame] | 230 | surface_manager_(params.surface_manager()), |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 231 | overlay_surface_id_(SurfaceManager::kNoSurfaceID), |
dalecurtis | 7f366b224 | 2016-04-13 01:16:17 | [diff] [blame] | 232 | suppress_destruction_errors_(false), |
jameswest | 451a5bb | 2017-01-27 03:59:39 | [diff] [blame] | 233 | suspend_enabled_(params.allow_suspend()), |
tguilbert | 25a4d11 | 2016-10-13 21:56:51 | [diff] [blame] | 234 | use_fallback_path_(false), |
dalecurtis | 9d638a1 | 2016-08-30 06:20:55 | [diff] [blame] | 235 | is_encrypted_(false), |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 236 | preroll_attempt_pending_(false), |
avayvod | 590011e3 | 2017-01-20 02:01:00 | [diff] [blame] | 237 | observer_(params.media_observer()), |
| 238 | max_keyframe_distance_to_disable_background_video_( |
servolk | f94b460 | 2017-01-31 16:44:27 | [diff] [blame] | 239 | params.max_keyframe_distance_to_disable_background_video()), |
| 240 | enable_instant_source_buffer_gc_( |
shaktisahu | 24189c1 | 2017-03-11 17:42:55 | [diff] [blame] | 241 | params.enable_instant_source_buffer_gc()), |
| 242 | embedded_media_experience_enabled_( |
| 243 | params.embedded_media_experience_enabled()) { |
xhwang | 5113973 | 2017-02-24 19:36:08 | [diff] [blame] | 244 | DVLOG(1) << __func__; |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 245 | DCHECK(!adjust_allocated_memory_cb_.is_null()); |
xhwang | 59d4175a | 2016-01-14 03:19:30 | [diff] [blame] | 246 | DCHECK(renderer_factory_); |
servolk | ef1e5ef | 2016-03-25 04:55:26 | [diff] [blame] | 247 | DCHECK(client_); |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 248 | DCHECK(delegate_); |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 249 | |
watk | d026f79 | 2016-11-05 00:28:51 | [diff] [blame] | 250 | tick_clock_.reset(new base::DefaultTickClock()); |
| 251 | |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 252 | force_video_overlays_ = base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 253 | switches::kForceVideoOverlays); |
| 254 | |
amp | ea73f79 | 2017-01-19 04:05:39 | [diff] [blame] | 255 | enable_fullscreen_video_overlays_ = |
| 256 | base::FeatureList::IsEnabled(media::kOverlayFullscreenVideo); |
| 257 | |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 258 | delegate_id_ = delegate_->AddObserver(this); |
| 259 | delegate_->SetIdle(delegate_id_, true); |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 260 | |
[email protected] | c93eb0a6 | 2011-08-09 22:47:24 | [diff] [blame] | 261 | media_log_->AddEvent( |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 262 | media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_CREATED)); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 263 | |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 264 | if (params.initial_cdm()) |
| 265 | SetCdm(params.initial_cdm()); |
xhwang | 0ad11e51 | 2014-11-25 23:43:09 | [diff] [blame] | 266 | |
xhwang | f94a634d | 2014-10-22 22:07:27 | [diff] [blame] | 267 | // TODO(xhwang): When we use an external Renderer, many methods won't work, |
xhwang | 6fa35620 | 2014-12-11 00:44:12 | [diff] [blame] | 268 | // e.g. GetCurrentFrameFromCompositor(). See http://crbug.com/434861 |
olka | e4ba2ed8 | 2016-12-06 16:34:51 | [diff] [blame] | 269 | audio_source_provider_ = |
| 270 | new WebAudioSourceProviderImpl(params.audio_renderer_sink(), media_log_); |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 271 | |
| 272 | if (observer_) |
| 273 | observer_->SetClient(this); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 274 | } |
| 275 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 276 | WebMediaPlayerImpl::~WebMediaPlayerImpl() { |
xhwang | 5113973 | 2017-02-24 19:36:08 | [diff] [blame] | 277 | DVLOG(1) << __func__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 278 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 279 | |
xhwang | 5113973 | 2017-02-24 19:36:08 | [diff] [blame] | 280 | if (set_cdm_result_) { |
| 281 | DVLOG(2) << "Resolve pending SetCdm() when media player is destroyed."; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 282 | set_cdm_result_->Complete(); |
xhwang | 5113973 | 2017-02-24 19:36:08 | [diff] [blame] | 283 | set_cdm_result_.reset(); |
| 284 | } |
| 285 | |
alokp | 1116967f | 2016-06-11 17:30:56 | [diff] [blame] | 286 | suppress_destruction_errors_ = true; |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 287 | |
| 288 | delegate_->PlayerGone(delegate_id_); |
| 289 | delegate_->RemoveObserver(delegate_id_); |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 290 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 291 | // Finalize any watch time metrics before destroying the pipeline. |
| 292 | watch_time_reporter_.reset(); |
| 293 | |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 294 | // The underlying Pipeline must be stopped before it is destroyed. |
| 295 | pipeline_controller_.Stop(); |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 296 | |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 297 | if (last_reported_memory_usage_) |
| 298 | adjust_allocated_memory_cb_.Run(-last_reported_memory_usage_); |
| 299 | |
dalecurtis | e1edb31 | 2016-06-22 02:33:21 | [diff] [blame] | 300 | // Destruct compositor resources in the proper order. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 301 | client_->SetWebLayer(nullptr); |
dalecurtis | e1edb31 | 2016-06-22 02:33:21 | [diff] [blame] | 302 | if (video_weblayer_) |
| 303 | static_cast<cc::VideoLayer*>(video_weblayer_->layer())->StopUsingProvider(); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 304 | compositor_task_runner_->DeleteSoon(FROM_HERE, compositor_); |
xhwang | ea8bb356 | 2015-06-08 21:18:37 | [diff] [blame] | 305 | |
| 306 | media_log_->AddEvent( |
| 307 | media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_DESTROYED)); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 308 | } |
| 309 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 310 | void WebMediaPlayerImpl::Load(LoadType load_type, |
guidou | 9bfe4e2f | 2016-04-09 08:31:19 | [diff] [blame] | 311 | const blink::WebMediaPlayerSource& source, |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 312 | CORSMode cors_mode) { |
xhwang | 5113973 | 2017-02-24 19:36:08 | [diff] [blame] | 313 | DVLOG(1) << __func__; |
guidou | 9bfe4e2f | 2016-04-09 08:31:19 | [diff] [blame] | 314 | // Only URL or MSE blob URL is supported. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 315 | DCHECK(source.IsURL()); |
| 316 | blink::WebURL url = source.GetAsURL(); |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 317 | DVLOG(1) << __func__ << "(" << load_type << ", " << url << ", " << cors_mode |
| 318 | << ")"; |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 319 | if (!defer_load_cb_.is_null()) { |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 320 | defer_load_cb_.Run(base::Bind(&WebMediaPlayerImpl::DoLoad, AsWeakPtr(), |
| 321 | load_type, url, cors_mode)); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 322 | return; |
| 323 | } |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 324 | DoLoad(load_type, url, cors_mode); |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 325 | } |
| 326 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 327 | bool WebMediaPlayerImpl::SupportsOverlayFullscreenVideo() { |
watk | 9c87c6fa | 2016-05-06 20:36:51 | [diff] [blame] | 328 | #if defined(OS_ANDROID) |
watk | f835a79 | 2016-06-24 23:24:40 | [diff] [blame] | 329 | return true; |
watk | 9c87c6fa | 2016-05-06 20:36:51 | [diff] [blame] | 330 | #else |
| 331 | return false; |
| 332 | #endif |
| 333 | } |
| 334 | |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 335 | void WebMediaPlayerImpl::EnableOverlay() { |
| 336 | overlay_enabled_ = true; |
watk | f835a79 | 2016-06-24 23:24:40 | [diff] [blame] | 337 | if (surface_manager_) { |
| 338 | surface_created_cb_.Reset( |
| 339 | base::Bind(&WebMediaPlayerImpl::OnSurfaceCreated, AsWeakPtr())); |
| 340 | surface_manager_->CreateFullscreenSurface(pipeline_metadata_.natural_size, |
| 341 | surface_created_cb_.callback()); |
| 342 | } |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 343 | |
| 344 | if (decoder_requires_restart_for_overlay_) |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 345 | ScheduleRestart(); |
| 346 | } |
| 347 | |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 348 | void WebMediaPlayerImpl::DisableOverlay() { |
| 349 | overlay_enabled_ = false; |
watk | f835a79 | 2016-06-24 23:24:40 | [diff] [blame] | 350 | surface_created_cb_.Cancel(); |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 351 | overlay_surface_id_ = SurfaceManager::kNoSurfaceID; |
| 352 | |
| 353 | if (decoder_requires_restart_for_overlay_) |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 354 | ScheduleRestart(); |
dalecurtis | 4b632fce2 | 2016-11-10 00:52:17 | [diff] [blame] | 355 | else if (!set_surface_cb_.is_null()) |
| 356 | set_surface_cb_.Run(overlay_surface_id_); |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 357 | } |
| 358 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 359 | void WebMediaPlayerImpl::EnteredFullscreen() { |
liberato | 2fd111be | 2017-01-04 00:25:06 | [diff] [blame] | 360 | // |force_video_overlays_| implies that we're already in overlay mode, so take |
| 361 | // no action here. Otherwise, switch to an overlay if it's allowed and if |
| 362 | // it will display properly. |
| 363 | if (!force_video_overlays_ && enable_fullscreen_video_overlays_ && |
| 364 | DoesOverlaySupportMetadata()) { |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 365 | EnableOverlay(); |
liberato | 2fd111be | 2017-01-04 00:25:06 | [diff] [blame] | 366 | } |
xjz | d3fe45a | 2016-10-12 18:26:37 | [diff] [blame] | 367 | if (observer_) |
| 368 | observer_->OnEnteredFullscreen(); |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 369 | } |
| 370 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 371 | void WebMediaPlayerImpl::ExitedFullscreen() { |
liberato | 2fd111be | 2017-01-04 00:25:06 | [diff] [blame] | 372 | // If we're in overlay mode, then exit it unless we're supposed to be in |
| 373 | // overlay mode all the time. |
| 374 | if (!force_video_overlays_ && overlay_enabled_) |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 375 | DisableOverlay(); |
xjz | d3fe45a | 2016-10-12 18:26:37 | [diff] [blame] | 376 | if (observer_) |
| 377 | observer_->OnExitedFullscreen(); |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 378 | } |
| 379 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 380 | void WebMediaPlayerImpl::BecameDominantVisibleContent(bool isDominant) { |
xjz | cdbbe73 | 2016-12-03 20:47:42 | [diff] [blame] | 381 | if (observer_) |
| 382 | observer_->OnBecameDominantVisibleContent(isDominant); |
| 383 | } |
| 384 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 385 | void WebMediaPlayerImpl::SetIsEffectivelyFullscreen( |
zqzhang | abc0824 | 2017-03-02 16:07:14 | [diff] [blame] | 386 | bool isEffectivelyFullscreen) { |
| 387 | delegate_->SetIsEffectivelyFullscreen(delegate_id_, isEffectivelyFullscreen); |
| 388 | } |
| 389 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 390 | void WebMediaPlayerImpl::DoLoad(LoadType load_type, |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 391 | const blink::WebURL& url, |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 392 | CORSMode cors_mode) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 393 | DVLOG(1) << __func__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 394 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 395 | |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 396 | GURL gurl(url); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 397 | ReportMetrics(load_type, gurl, frame_->GetSecurityOrigin(), media_log_); |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 398 | |
[email protected] | 926f8fd | 2013-04-12 20:27:53 | [diff] [blame] | 399 | // Set subresource URL for crash reporting. |
| 400 | base::debug::SetCrashKeyValue("subresource_url", gurl.spec()); |
| 401 | |
tguilbert | 25a4d11 | 2016-10-13 21:56:51 | [diff] [blame] | 402 | if (use_fallback_path_) |
| 403 | fallback_url_ = gurl; |
| 404 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 405 | load_type_ = load_type; |
| 406 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 407 | SetNetworkState(WebMediaPlayer::kNetworkStateLoading); |
| 408 | SetReadyState(WebMediaPlayer::kReadyStateHaveNothing); |
| 409 | media_log_->AddEvent(media_log_->CreateLoadEvent(url.GetString().Utf8())); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 410 | |
| 411 | // Media source pipelines can start immediately. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 412 | if (load_type == kLoadTypeMediaSource) { |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 413 | supports_save_ = false; |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 414 | StartPipeline(); |
hubbe | 5f0ad43b | 2015-12-14 20:57:26 | [diff] [blame] | 415 | } else { |
dalecurtis | b6e052f5 | 2016-08-25 00:35:55 | [diff] [blame] | 416 | data_source_.reset(new MultibufferDataSource( |
| 417 | url, static_cast<UrlData::CORSMode>(cors_mode), main_task_runner_, |
| 418 | url_index_, frame_, media_log_.get(), &buffered_data_source_host_, |
| 419 | base::Bind(&WebMediaPlayerImpl::NotifyDownloading, AsWeakPtr()))); |
avayvod | e46d7bef | 2016-03-30 23:18:26 | [diff] [blame] | 420 | data_source_->SetPreload(preload_); |
avayvod | e46d7bef | 2016-03-30 23:18:26 | [diff] [blame] | 421 | data_source_->Initialize( |
| 422 | base::Bind(&WebMediaPlayerImpl::DataSourceInitialized, AsWeakPtr())); |
hubbe | 5f0ad43b | 2015-12-14 20:57:26 | [diff] [blame] | 423 | } |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 424 | |
| 425 | #if defined(OS_ANDROID) // WMPI_CAST |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 426 | cast_impl_.Initialize(url, frame_, delegate_id_); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 427 | #endif |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 428 | } |
| 429 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 430 | void WebMediaPlayerImpl::Play() { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 431 | DVLOG(1) << __func__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 432 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 433 | |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 434 | // User initiated play unlocks background video playback. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 435 | if (blink::WebUserGestureIndicator::IsProcessingUserGesture()) |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 436 | video_locked_when_paused_when_hidden_ = false; |
| 437 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 438 | #if defined(OS_ANDROID) // WMPI_CAST |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 439 | if (IsRemote()) { |
dalecurtis | 6e2716d | 2016-01-21 04:49:15 | [diff] [blame] | 440 | cast_impl_.play(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 441 | return; |
| 442 | } |
| 443 | #endif |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 444 | // TODO(sandersd): Do we want to reset the idle timer here? |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 445 | delegate_->SetIdle(delegate_id_, false); |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 446 | paused_ = false; |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 447 | pipeline_controller_.SetPlaybackRate(playback_rate_); |
dalecurtis | 4619cd0 | 2016-09-22 21:39:10 | [diff] [blame] | 448 | background_pause_timer_.Stop(); |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 449 | |
[email protected] | 039b754 | 2013-10-17 22:06:25 | [diff] [blame] | 450 | if (data_source_) |
| 451 | data_source_->MediaIsPlaying(); |
[email protected] | 090f731 | 2011-08-05 23:26:40 | [diff] [blame] | 452 | |
xjz | 48a9cb7 | 2016-12-20 04:02:49 | [diff] [blame] | 453 | if (observer_) |
| 454 | observer_->OnPlaying(); |
| 455 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 456 | DCHECK(watch_time_reporter_); |
| 457 | watch_time_reporter_->OnPlaying(); |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 458 | media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PLAY)); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 459 | UpdatePlayState(); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 460 | } |
| 461 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 462 | void WebMediaPlayerImpl::Pause() { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 463 | DVLOG(1) << __func__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 464 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 465 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 466 | // We update the paused state even when casting, since we expect pause() to be |
| 467 | // called when casting begins, and when we exit casting we should end up in a |
| 468 | // paused state. |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 469 | paused_ = true; |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 470 | |
avayvod | eb9098d | 2017-01-07 00:33:03 | [diff] [blame] | 471 | // No longer paused because it was hidden. |
| 472 | paused_when_hidden_ = false; |
| 473 | |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 474 | // User initiated pause locks background videos. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 475 | if (blink::WebUserGestureIndicator::IsProcessingUserGesture()) |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 476 | video_locked_when_paused_when_hidden_ = true; |
| 477 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 478 | #if defined(OS_ANDROID) // WMPI_CAST |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 479 | if (IsRemote()) { |
dalecurtis | 6e2716d | 2016-01-21 04:49:15 | [diff] [blame] | 480 | cast_impl_.pause(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 481 | return; |
| 482 | } |
| 483 | #endif |
| 484 | |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 485 | pipeline_controller_.SetPlaybackRate(0.0); |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 486 | |
| 487 | // pause() may be called after playback has ended and the HTMLMediaElement |
| 488 | // requires that currentTime() == duration() after ending. We want to ensure |
| 489 | // |paused_time_| matches currentTime() in this case or a future seek() may |
| 490 | // incorrectly discard what it thinks is a seek to the existing time. |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 491 | paused_time_ = |
| 492 | ended_ ? GetPipelineMediaDuration() : pipeline_controller_.GetMediaTime(); |
[email protected] | 090f731 | 2011-08-05 23:26:40 | [diff] [blame] | 493 | |
xjz | 48a9cb7 | 2016-12-20 04:02:49 | [diff] [blame] | 494 | if (observer_) |
| 495 | observer_->OnPaused(); |
| 496 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 497 | DCHECK(watch_time_reporter_); |
| 498 | watch_time_reporter_->OnPaused(); |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 499 | media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PAUSE)); |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 500 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 501 | UpdatePlayState(); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 502 | } |
| 503 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 504 | bool WebMediaPlayerImpl::SupportsSave() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 505 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 132dd57c | 2012-08-10 23:24:34 | [diff] [blame] | 506 | return supports_save_; |
[email protected] | 574a1d6 | 2009-07-17 03:23:46 | [diff] [blame] | 507 | } |
| 508 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 509 | void WebMediaPlayerImpl::Seek(double seconds) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 510 | DVLOG(1) << __func__ << "(" << seconds << "s)"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 511 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
servolk | 86b3d88fb | 2017-03-18 02:50:28 | [diff] [blame] | 512 | media_log_->AddEvent(media_log_->CreateSeekEvent(seconds)); |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 513 | DoSeek(base::TimeDelta::FromSecondsD(seconds), true); |
| 514 | } |
| 515 | |
| 516 | void WebMediaPlayerImpl::DoSeek(base::TimeDelta time, bool time_updated) { |
| 517 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 518 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 519 | #if defined(OS_ANDROID) // WMPI_CAST |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 520 | if (IsRemote()) { |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 521 | cast_impl_.seek(time); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 522 | return; |
| 523 | } |
| 524 | #endif |
| 525 | |
srirama.m | ccf67181 | 2015-01-08 11:59:13 | [diff] [blame] | 526 | ReadyState old_state = ready_state_; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 527 | if (ready_state_ > WebMediaPlayer::kReadyStateHaveMetadata) |
| 528 | SetReadyState(WebMediaPlayer::kReadyStateHaveMetadata); |
[email protected] | 1bb66680 | 2013-11-28 06:12:08 | [diff] [blame] | 529 | |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 530 | // When paused, we know exactly what the current time is and can elide seeks |
| 531 | // to it. However, there are two cases that are not elided: |
| 532 | // 1) When the pipeline state is not stable. |
| 533 | // In this case we just let |pipeline_controller_| decide what to do, as |
| 534 | // it has complete information. |
| 535 | // 2) For MSE. |
| 536 | // Because the buffers may have changed between seeks, MSE seeks are |
| 537 | // never elided. |
| 538 | if (paused_ && pipeline_controller_.IsStable() && paused_time_ == time && |
| 539 | !chunk_demuxer_) { |
| 540 | // If the ready state was high enough before, we can indicate that the seek |
| 541 | // completed just by restoring it. Otherwise we will just wait for the real |
| 542 | // ready state change to eventually happen. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 543 | if (old_state == kReadyStateHaveEnoughData) { |
srirama.m | 8f4a3756 | 2014-12-13 08:16:18 | [diff] [blame] | 544 | main_task_runner_->PostTask( |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 545 | FROM_HERE, base::Bind(&WebMediaPlayerImpl::OnBufferingStateChange, |
| 546 | AsWeakPtr(), BUFFERING_HAVE_ENOUGH)); |
srirama.m | 36ab268 | 2014-12-11 04:20:01 | [diff] [blame] | 547 | } |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 548 | return; |
srirama.m | 36ab268 | 2014-12-11 04:20:01 | [diff] [blame] | 549 | } |
[email protected] | 44ff37c0 | 2009-10-24 01:03:03 | [diff] [blame] | 550 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 551 | // Call this before setting |seeking_| so that the current media time can be |
| 552 | // recorded by the reporter. |
| 553 | if (watch_time_reporter_) |
| 554 | watch_time_reporter_->OnSeeking(); |
| 555 | |
dalecurtis | 1af3c1a | 2017-04-11 00:53:49 | [diff] [blame] | 556 | // Clear any new frame processed callbacks on seek; otherwise we'll end up |
| 557 | // logging a time long after the seek completes. |
| 558 | frame_time_report_cb_.Cancel(); |
| 559 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 560 | // TODO(sandersd): Move |seeking_| to PipelineController. |
| 561 | // TODO(sandersd): Do we want to reset the idle timer here? |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 562 | delegate_->SetIdle(delegate_id_, false); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 563 | ended_ = false; |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 564 | seeking_ = true; |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 565 | seek_time_ = time; |
| 566 | if (paused_) |
| 567 | paused_time_ = time; |
| 568 | pipeline_controller_.Seek(time, time_updated); |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 569 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 570 | // This needs to be called after Seek() so that if a resume is triggered, it |
| 571 | // is to the correct time. |
| 572 | UpdatePlayState(); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 573 | } |
| 574 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 575 | void WebMediaPlayerImpl::SetRate(double rate) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 576 | DVLOG(1) << __func__ << "(" << rate << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 577 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 578 | |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 579 | // TODO(kylep): Remove when support for negatives is added. Also, modify the |
| 580 | // following checks so rewind uses reasonable values also. |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 581 | if (rate < 0.0) |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 582 | return; |
| 583 | |
| 584 | // Limit rates to reasonable values by clamping. |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 585 | if (rate != 0.0) { |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 586 | if (rate < kMinRate) |
| 587 | rate = kMinRate; |
| 588 | else if (rate > kMaxRate) |
| 589 | rate = kMaxRate; |
| 590 | } |
| 591 | |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 592 | playback_rate_ = rate; |
| 593 | if (!paused_) { |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 594 | pipeline_controller_.SetPlaybackRate(rate); |
[email protected] | 039b754 | 2013-10-17 22:06:25 | [diff] [blame] | 595 | if (data_source_) |
| 596 | data_source_->MediaPlaybackRateChanged(rate); |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 597 | } |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 598 | } |
| 599 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 600 | void WebMediaPlayerImpl::SetVolume(double volume) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 601 | DVLOG(1) << __func__ << "(" << volume << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 602 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 603 | volume_ = volume; |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 604 | pipeline_controller_.SetVolume(volume_ * volume_multiplier_); |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 605 | if (watch_time_reporter_) |
| 606 | watch_time_reporter_->OnVolumeChange(volume); |
mlamouri | 91011136 | 2016-11-04 11:28:24 | [diff] [blame] | 607 | |
| 608 | // The play state is updated because the player might have left the autoplay |
| 609 | // muted state. |
| 610 | UpdatePlayState(); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 611 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 612 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 613 | void WebMediaPlayerImpl::SetSinkId( |
guidou | c7babef | 2015-10-22 00:42:35 | [diff] [blame] | 614 | const blink::WebString& sink_id, |
| 615 | const blink::WebSecurityOrigin& security_origin, |
| 616 | blink::WebSetSinkIdCallbacks* web_callback) { |
guidou | 69223ce | 2015-06-16 10:36:19 | [diff] [blame] | 617 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 618 | DVLOG(1) << __func__; |
guidou | c7babef | 2015-10-22 00:42:35 | [diff] [blame] | 619 | |
olka | 68b6939 | 2016-04-01 11:42:12 | [diff] [blame] | 620 | media::OutputDeviceStatusCB callback = |
| 621 | media::ConvertToOutputDeviceStatusCB(web_callback); |
guidou | c7babef | 2015-10-22 00:42:35 | [diff] [blame] | 622 | media_task_runner_->PostTask( |
| 623 | FROM_HERE, |
| 624 | base::Bind(&SetSinkIdOnMediaThread, audio_source_provider_, |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 625 | sink_id.Utf8(), static_cast<url::Origin>(security_origin), |
guidou | c7babef | 2015-10-22 00:42:35 | [diff] [blame] | 626 | callback)); |
guidou | 69223ce | 2015-06-16 10:36:19 | [diff] [blame] | 627 | } |
| 628 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 629 | STATIC_ASSERT_ENUM(WebMediaPlayer::kPreloadNone, MultibufferDataSource::NONE); |
| 630 | STATIC_ASSERT_ENUM(WebMediaPlayer::kPreloadMetaData, |
dalecurtis | b6e052f5 | 2016-08-25 00:35:55 | [diff] [blame] | 631 | MultibufferDataSource::METADATA); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 632 | STATIC_ASSERT_ENUM(WebMediaPlayer::kPreloadAuto, MultibufferDataSource::AUTO); |
[email protected] | 23a8b1d8 | 2011-04-05 16:28:20 | [diff] [blame] | 633 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 634 | void WebMediaPlayerImpl::SetPreload(WebMediaPlayer::Preload preload) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 635 | DVLOG(1) << __func__ << "(" << preload << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 636 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 637 | |
dalecurtis | b6e052f5 | 2016-08-25 00:35:55 | [diff] [blame] | 638 | preload_ = static_cast<MultibufferDataSource::Preload>(preload); |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 639 | if (data_source_) |
[email protected] | 09c6022 | 2014-08-07 16:42:31 | [diff] [blame] | 640 | data_source_->SetPreload(preload_); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 641 | } |
| 642 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 643 | bool WebMediaPlayerImpl::HasVideo() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 644 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 645 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 646 | return pipeline_metadata_.has_video; |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 647 | } |
| 648 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 649 | bool WebMediaPlayerImpl::HasAudio() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 650 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | fc367af | 2009-08-14 23:06:35 | [diff] [blame] | 651 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 652 | return pipeline_metadata_.has_audio; |
[email protected] | fc367af | 2009-08-14 23:06:35 | [diff] [blame] | 653 | } |
| 654 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 655 | void WebMediaPlayerImpl::EnabledAudioTracksChanged( |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 656 | const blink::WebVector<blink::WebMediaPlayer::TrackId>& enabledTrackIds) { |
| 657 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 658 | |
| 659 | std::ostringstream logstr; |
| 660 | std::vector<MediaTrack::Id> enabledMediaTrackIds; |
| 661 | for (const auto& blinkTrackId : enabledTrackIds) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 662 | MediaTrack::Id track_id = blinkTrackId.Utf8().data(); |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 663 | logstr << track_id << " "; |
| 664 | enabledMediaTrackIds.push_back(track_id); |
| 665 | } |
| 666 | MEDIA_LOG(INFO, media_log_) << "Enabled audio tracks: [" << logstr.str() |
| 667 | << "]"; |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 668 | pipeline_controller_.OnEnabledAudioTracksChanged(enabledMediaTrackIds); |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 669 | } |
| 670 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 671 | void WebMediaPlayerImpl::SelectedVideoTrackChanged( |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 672 | blink::WebMediaPlayer::TrackId* selectedTrackId) { |
| 673 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 674 | |
servolk | 9bed660 | 2017-02-24 01:20:11 | [diff] [blame] | 675 | base::Optional<MediaTrack::Id> selected_video_track_id; |
| 676 | if (selectedTrackId && !video_track_disabled_) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 677 | selected_video_track_id = MediaTrack::Id(selectedTrackId->Utf8().data()); |
servolk | 9bed660 | 2017-02-24 01:20:11 | [diff] [blame] | 678 | MEDIA_LOG(INFO, media_log_) << "Selected video track: [" |
| 679 | << selected_video_track_id.value_or("") << "]"; |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 680 | pipeline_controller_.OnSelectedVideoTrackChanged(selected_video_track_id); |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 681 | } |
| 682 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 683 | bool WebMediaPlayerImpl::GetLastUploadedFrameInfo(unsigned* width, |
kainino | 36eeff8 | 2017-03-30 00:55:30 | [diff] [blame] | 684 | unsigned* height, |
| 685 | double* timestamp) { |
| 686 | *width = last_uploaded_frame_size_.width(); |
| 687 | *height = last_uploaded_frame_size_.height(); |
| 688 | *timestamp = last_uploaded_frame_timestamp_.InSecondsF(); |
| 689 | return true; |
| 690 | } |
| 691 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 692 | blink::WebSize WebMediaPlayerImpl::NaturalSize() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 693 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 694 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 695 | return blink::WebSize(pipeline_metadata_.natural_size); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 696 | } |
| 697 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 698 | bool WebMediaPlayerImpl::Paused() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 699 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 700 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 701 | #if defined(OS_ANDROID) // WMPI_CAST |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 702 | if (IsRemote()) |
danakj | 4f1fd6a | 2017-01-06 21:15:17 | [diff] [blame] | 703 | return cast_impl_.IsPaused(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 704 | #endif |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 705 | |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 706 | return pipeline_controller_.GetPlaybackRate() == 0.0f; |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 707 | } |
| 708 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 709 | bool WebMediaPlayerImpl::Seeking() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 710 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 711 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 712 | if (ready_state_ == WebMediaPlayer::kReadyStateHaveNothing) |
[email protected] | 0acebfa | 2009-08-21 22:45:40 | [diff] [blame] | 713 | return false; |
[email protected] | 67cd505 | 2009-09-10 21:53:22 | [diff] [blame] | 714 | |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 715 | return seeking_; |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 716 | } |
| 717 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 718 | double WebMediaPlayerImpl::Duration() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 719 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 720 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 721 | if (ready_state_ == WebMediaPlayer::kReadyStateHaveNothing) |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 722 | return std::numeric_limits<double>::quiet_NaN(); |
| 723 | |
chcunningham | b92d506 | 2017-01-10 21:50:22 | [diff] [blame] | 724 | // Use duration from ChunkDemuxer when present. MSE allows users to specify |
| 725 | // duration as a double. This propagates to the rest of the pipeline as a |
| 726 | // TimeDelta with potentially reduced precision (limited to Microseconds). |
| 727 | // ChunkDemuxer returns the full-precision user-specified double. This ensures |
| 728 | // users can "get" the exact duration they "set". |
| 729 | if (chunk_demuxer_) |
| 730 | return chunk_demuxer_->GetDuration(); |
| 731 | |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 732 | base::TimeDelta pipeline_duration = GetPipelineMediaDuration(); |
chcunningham | b92d506 | 2017-01-10 21:50:22 | [diff] [blame] | 733 | return pipeline_duration == kInfiniteDuration |
| 734 | ? std::numeric_limits<double>::infinity() |
| 735 | : pipeline_duration.InSecondsF(); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 736 | } |
| 737 | |
[email protected] | db66d009 | 2014-04-16 07:15:12 | [diff] [blame] | 738 | double WebMediaPlayerImpl::timelineOffset() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 739 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | db66d009 | 2014-04-16 07:15:12 | [diff] [blame] | 740 | |
| 741 | if (pipeline_metadata_.timeline_offset.is_null()) |
| 742 | return std::numeric_limits<double>::quiet_NaN(); |
| 743 | |
| 744 | return pipeline_metadata_.timeline_offset.ToJsTime(); |
| 745 | } |
| 746 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 747 | double WebMediaPlayerImpl::CurrentTime() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 748 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 749 | DCHECK_NE(ready_state_, WebMediaPlayer::kReadyStateHaveNothing); |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 750 | |
| 751 | // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement, |
| 752 | // see http://crbug.com/409280 |
| 753 | if (ended_) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 754 | return Duration(); |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 755 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 756 | if (Seeking()) |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 757 | return seek_time_.InSecondsF(); |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 758 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 759 | #if defined(OS_ANDROID) // WMPI_CAST |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 760 | if (IsRemote()) |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 761 | return cast_impl_.currentTime(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 762 | #endif |
| 763 | |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 764 | if (paused_) |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 765 | return paused_time_.InSecondsF(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 766 | |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 767 | return pipeline_controller_.GetMediaTime().InSecondsF(); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 768 | } |
| 769 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 770 | WebMediaPlayer::NetworkState WebMediaPlayerImpl::GetNetworkState() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 771 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 772 | return network_state_; |
| 773 | } |
| 774 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 775 | WebMediaPlayer::ReadyState WebMediaPlayerImpl::GetReadyState() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 776 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 777 | return ready_state_; |
| 778 | } |
| 779 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 780 | blink::WebString WebMediaPlayerImpl::GetErrorMessage() { |
wolenetz | 4d39cc0 | 2016-04-05 19:54:41 | [diff] [blame] | 781 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 782 | return blink::WebString::FromUTF8(media_log_->GetLastErrorMessage()); |
wolenetz | 4d39cc0 | 2016-04-05 19:54:41 | [diff] [blame] | 783 | } |
| 784 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 785 | blink::WebTimeRanges WebMediaPlayerImpl::Buffered() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 786 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 779a832 | 2014-08-22 21:28:37 | [diff] [blame] | 787 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 788 | Ranges<base::TimeDelta> buffered_time_ranges = |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 789 | pipeline_controller_.GetBufferedTimeRanges(); |
[email protected] | 779a832 | 2014-08-22 21:28:37 | [diff] [blame] | 790 | |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 791 | const base::TimeDelta duration = GetPipelineMediaDuration(); |
dalecurtis | 39a7f93 | 2016-07-19 18:34:59 | [diff] [blame] | 792 | if (duration != kInfiniteDuration) { |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 793 | buffered_data_source_host_.AddBufferedTimeRanges(&buffered_time_ranges, |
| 794 | duration); |
[email protected] | 779a832 | 2014-08-22 21:28:37 | [diff] [blame] | 795 | } |
[email protected] | 02022fc | 2014-05-16 00:05:31 | [diff] [blame] | 796 | return ConvertToWebTimeRanges(buffered_time_ranges); |
| 797 | } |
| 798 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 799 | blink::WebTimeRanges WebMediaPlayerImpl::Seekable() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 800 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 801 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 802 | if (ready_state_ < WebMediaPlayer::kReadyStateHaveMetadata) |
philipj | b0e6f3f | 2014-09-30 09:51:53 | [diff] [blame] | 803 | return blink::WebTimeRanges(); |
| 804 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 805 | const double seekable_end = Duration(); |
dalecurtis | 56359cb | 2014-10-28 00:06:29 | [diff] [blame] | 806 | |
| 807 | // Allow a special exception for seeks to zero for streaming sources with a |
| 808 | // finite duration; this allows looping to work. |
tguilbert | ade2bcb | 2017-01-07 02:57:45 | [diff] [blame] | 809 | const bool is_finite_stream = data_source_ && data_source_->IsStreaming() && |
| 810 | std::isfinite(seekable_end); |
| 811 | |
| 812 | // Do not change the seekable range when using the fallback path. |
| 813 | // The MediaPlayerRenderer will take care of dropping invalid seeks. |
| 814 | const bool force_seeks_to_zero = !use_fallback_path_ && is_finite_stream; |
dalecurtis | 56359cb | 2014-10-28 00:06:29 | [diff] [blame] | 815 | |
| 816 | // TODO(dalecurtis): Technically this allows seeking on media which return an |
tguilbert | ade2bcb | 2017-01-07 02:57:45 | [diff] [blame] | 817 | // infinite duration so long as DataSource::IsStreaming() is false. While not |
dalecurtis | 56359cb | 2014-10-28 00:06:29 | [diff] [blame] | 818 | // expected, disabling this breaks semi-live players, http://crbug.com/427412. |
| 819 | const blink::WebTimeRange seekable_range( |
tguilbert | ade2bcb | 2017-01-07 02:57:45 | [diff] [blame] | 820 | 0.0, force_seeks_to_zero ? 0.0 : seekable_end); |
philipj | b0e6f3f | 2014-09-30 09:51:53 | [diff] [blame] | 821 | return blink::WebTimeRanges(&seekable_range, 1); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 822 | } |
| 823 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 824 | bool WebMediaPlayerImpl::IsPrerollAttemptNeeded() { |
| 825 | // TODO(sandersd): Replace with |highest_ready_state_since_seek_| if we need |
| 826 | // to ensure that preroll always gets a chance to complete. |
| 827 | // See http://crbug.com/671525. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 828 | if (highest_ready_state_ >= ReadyState::kReadyStateHaveFutureData) |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 829 | return false; |
| 830 | |
| 831 | if (preroll_attempt_pending_) |
| 832 | return true; |
| 833 | |
| 834 | // Freshly initialized; there has never been any loading progress. (Otherwise |
| 835 | // |preroll_attempt_pending_| would be true when the start time is null.) |
| 836 | if (preroll_attempt_start_time_.is_null()) |
| 837 | return false; |
| 838 | |
| 839 | base::TimeDelta preroll_attempt_duration = |
| 840 | tick_clock_->NowTicks() - preroll_attempt_start_time_; |
| 841 | return preroll_attempt_duration < kPrerollAttemptTimeout; |
| 842 | } |
| 843 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 844 | bool WebMediaPlayerImpl::DidLoadingProgress() { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 845 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
dalecurtis | e7120dc | 2016-09-03 02:54:35 | [diff] [blame] | 846 | |
| 847 | // Note: Separate variables used to ensure both methods are called every time. |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 848 | const bool pipeline_progress = pipeline_controller_.DidLoadingProgress(); |
dalecurtis | e7120dc | 2016-09-03 02:54:35 | [diff] [blame] | 849 | const bool data_progress = buffered_data_source_host_.DidLoadingProgress(); |
| 850 | const bool did_loading_progress = pipeline_progress || data_progress; |
| 851 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 852 | if (did_loading_progress && |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 853 | highest_ready_state_ < ReadyState::kReadyStateHaveFutureData) { |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 854 | // Reset the preroll attempt clock. |
| 855 | preroll_attempt_pending_ = true; |
| 856 | preroll_attempt_start_time_ = base::TimeTicks(); |
| 857 | |
| 858 | // Clear any 'stale' flag and give the pipeline a chance to resume. If we |
| 859 | // are already resumed, this will cause |preroll_attempt_start_time_| to be |
| 860 | // set. |
| 861 | // TODO(sandersd): Should this be on the same stack? It might be surprising |
| 862 | // that didLoadingProgress() can synchronously change state. |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 863 | delegate_->ClearStaleFlag(delegate_id_); |
dalecurtis | e7120dc | 2016-09-03 02:54:35 | [diff] [blame] | 864 | UpdatePlayState(); |
| 865 | } |
| 866 | |
| 867 | return did_loading_progress; |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 868 | } |
| 869 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 870 | void WebMediaPlayerImpl::Paint(blink::WebCanvas* canvas, |
[email protected] | dd5c797 | 2014-08-21 15:00:37 | [diff] [blame] | 871 | const blink::WebRect& rect, |
enne | 11266b8 | 2017-03-02 01:43:47 | [diff] [blame] | 872 | cc::PaintFlags& flags) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 873 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 874 | TRACE_EVENT0("media", "WebMediaPlayerImpl:paint"); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 875 | |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 876 | // TODO(sandersd): Move this check into GetCurrentFrameFromCompositor() when |
| 877 | // we have other ways to check if decoder owns video frame. |
| 878 | // See http://crbug.com/595716 and http://crbug.com/602708 |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 879 | if (cdm_) |
xhwang | 8073945 | 2016-01-13 00:48:00 | [diff] [blame] | 880 | return; |
| 881 | |
mcasas | f1236fc2 | 2015-05-29 22:38:56 | [diff] [blame] | 882 | scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor(); |
[email protected] | 0fe5d43b | 2014-01-24 23:32:45 | [diff] [blame] | 883 | |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 884 | gfx::Rect gfx_rect(rect); |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 885 | Context3D context_3d; |
mcasas | 265bdbf8 | 2015-06-12 18:44:07 | [diff] [blame] | 886 | if (video_frame.get() && video_frame->HasTextures()) { |
mcasas | f1236fc2 | 2015-05-29 22:38:56 | [diff] [blame] | 887 | if (!context_3d_cb_.is_null()) |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 888 | context_3d = context_3d_cb_.Run(); |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 889 | if (!context_3d.gl) |
danakj | 53f7ec90 | 2016-05-21 01:30:10 | [diff] [blame] | 890 | return; // Unable to get/create a shared main thread context. |
| 891 | if (!context_3d.gr_context) |
| 892 | return; // The context has been lost since and can't setup a GrContext. |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 893 | } |
danakj | 795f173 | 2015-08-31 23:40:22 | [diff] [blame] | 894 | skcanvas_video_renderer_.Paint(video_frame, canvas, gfx::RectF(gfx_rect), |
enne | 11266b8 | 2017-03-02 01:43:47 | [diff] [blame] | 895 | flags, pipeline_metadata_.video_rotation, |
danakj | 795f173 | 2015-08-31 23:40:22 | [diff] [blame] | 896 | context_3d); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 897 | } |
[email protected] | 5df5165 | 2009-01-17 00:03:00 | [diff] [blame] | 898 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 899 | bool WebMediaPlayerImpl::HasSingleSecurityOrigin() const { |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 900 | if (data_source_) |
| 901 | return data_source_->HasSingleOrigin(); |
[email protected] | fcdb746 | 2009-10-21 21:05:11 | [diff] [blame] | 902 | return true; |
[email protected] | 38259a7a8 | 2009-07-29 21:49:49 | [diff] [blame] | 903 | } |
| 904 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 905 | bool WebMediaPlayerImpl::DidPassCORSAccessCheck() const { |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 906 | if (data_source_) |
| 907 | return data_source_->DidPassCORSAccessCheck(); |
| 908 | return false; |
[email protected] | 3fe2711 | 2012-06-07 04:00:01 | [diff] [blame] | 909 | } |
| 910 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 911 | double WebMediaPlayerImpl::MediaTimeForTimeValue(double timeValue) const { |
qinmin | b4c3978 | 2015-08-10 18:43:24 | [diff] [blame] | 912 | return base::TimeDelta::FromSecondsD(timeValue).InSecondsF(); |
[email protected] | e06e16d8 | 2011-05-26 22:13:33 | [diff] [blame] | 913 | } |
| 914 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 915 | unsigned WebMediaPlayerImpl::DecodedFrameCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 916 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 917 | |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 918 | PipelineStatistics stats = GetPipelineStatistics(); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 919 | return stats.video_frames_decoded; |
| 920 | } |
| 921 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 922 | unsigned WebMediaPlayerImpl::DroppedFrameCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 923 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 924 | |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 925 | PipelineStatistics stats = GetPipelineStatistics(); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 926 | return stats.video_frames_dropped; |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 927 | } |
| 928 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 929 | size_t WebMediaPlayerImpl::AudioDecodedByteCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 930 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 931 | |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 932 | PipelineStatistics stats = GetPipelineStatistics(); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 933 | return stats.audio_bytes_decoded; |
| 934 | } |
| 935 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 936 | size_t WebMediaPlayerImpl::VideoDecodedByteCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 937 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 938 | |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 939 | PipelineStatistics stats = GetPipelineStatistics(); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 940 | return stats.video_bytes_decoded; |
| 941 | } |
| 942 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 943 | bool WebMediaPlayerImpl::CopyVideoTextureToPlatformTexture( |
danakj | 46070b0 | 2016-03-28 21:54:11 | [diff] [blame] | 944 | gpu::gles2::GLES2Interface* gl, |
zmo | 57d577a | 2015-10-30 18:28:59 | [diff] [blame] | 945 | unsigned int texture, |
zmo | 57d577a | 2015-10-30 18:28:59 | [diff] [blame] | 946 | bool premultiply_alpha, |
| 947 | bool flip_y) { |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 948 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | bfc05f2 | 2013-10-19 17:55:16 | [diff] [blame] | 949 | TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture"); |
| 950 | |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 951 | // TODO(sandersd): Move this check into GetCurrentFrameFromCompositor() when |
| 952 | // we have other ways to check if decoder owns video frame. |
| 953 | // See http://crbug.com/595716 and http://crbug.com/602708 |
| 954 | if (cdm_) |
| 955 | return false; |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 956 | |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 957 | scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor(); |
jbauman | 581d041c | 2016-07-21 01:01:03 | [diff] [blame] | 958 | if (!video_frame.get() || !video_frame->HasTextures()) { |
[email protected] | e56f88c7 | 2013-06-25 22:31:29 | [diff] [blame] | 959 | return false; |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 960 | } |
[email protected] | df41e25 | 2014-02-03 23:39:50 | [diff] [blame] | 961 | |
jbauman | 581d041c | 2016-07-21 01:01:03 | [diff] [blame] | 962 | Context3D context_3d; |
| 963 | if (!context_3d_cb_.is_null()) |
| 964 | context_3d = context_3d_cb_.Run(); |
| 965 | return skcanvas_video_renderer_.CopyVideoFrameTexturesToGLTexture( |
kbr | 4910ae5 | 2017-01-11 00:52:28 | [diff] [blame] | 966 | context_3d, gl, video_frame.get(), texture, premultiply_alpha, flip_y); |
[email protected] | 6523b24 | 2013-03-13 11:10:07 | [diff] [blame] | 967 | } |
| 968 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 969 | void WebMediaPlayerImpl::SetContentDecryptionModule( |
[email protected] | 9ebc3b03f | 2014-08-13 04:01:23 | [diff] [blame] | 970 | blink::WebContentDecryptionModule* cdm, |
| 971 | blink::WebContentDecryptionModuleResult result) { |
xhwang | 5113973 | 2017-02-24 19:36:08 | [diff] [blame] | 972 | DVLOG(1) << __func__ << ": cdm = " << cdm; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 973 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 9ebc3b03f | 2014-08-13 04:01:23 | [diff] [blame] | 974 | |
jrummell | 06f2707 | 2015-06-08 18:12:38 | [diff] [blame] | 975 | // Once the CDM is set it can't be cleared as there may be frames being |
| 976 | // decrypted on other threads. So fail this request. |
| 977 | // http://crbug.com/462365#c7. |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 978 | if (!cdm) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 979 | result.CompleteWithError( |
| 980 | blink::kWebContentDecryptionModuleExceptionInvalidStateError, 0, |
xhwang | 79b19304 | 2016-12-13 18:52:43 | [diff] [blame] | 981 | "The existing ContentDecryptionModule object cannot be removed at this " |
| 982 | "time."); |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 983 | return; |
| 984 | } |
| 985 | |
jrummell | 89e61d8 | 2015-07-23 20:03:34 | [diff] [blame] | 986 | // Create a local copy of |result| to avoid problems with the callback |
| 987 | // getting passed to the media thread and causing |result| to be destructed |
jrummell | 4acbec91 | 2016-02-27 02:45:03 | [diff] [blame] | 988 | // on the wrong thread in some failure conditions. Blink should prevent |
| 989 | // multiple simultaneous calls. |
| 990 | DCHECK(!set_cdm_result_); |
jrummell | 89e61d8 | 2015-07-23 20:03:34 | [diff] [blame] | 991 | set_cdm_result_.reset(new blink::WebContentDecryptionModuleResult(result)); |
| 992 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 993 | // Recreate the watch time reporter if necessary. |
| 994 | const bool was_encrypted = is_encrypted_; |
| 995 | is_encrypted_ = true; |
| 996 | if (!was_encrypted && watch_time_reporter_) |
| 997 | CreateWatchTimeReporter(); |
| 998 | |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 999 | SetCdm(cdm); |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 1000 | } |
| 1001 | |
xhwang | e8c4181a | 2014-12-06 08:10:01 | [diff] [blame] | 1002 | void WebMediaPlayerImpl::OnEncryptedMediaInitData( |
jrummell | cf78967c | 2015-04-03 05:03:58 | [diff] [blame] | 1003 | EmeInitDataType init_data_type, |
Avi Drissman | 97785ea | 2015-12-19 01:11:31 | [diff] [blame] | 1004 | const std::vector<uint8_t>& init_data) { |
jrummell | cf78967c | 2015-04-03 05:03:58 | [diff] [blame] | 1005 | DCHECK(init_data_type != EmeInitDataType::UNKNOWN); |
xhwang | bab66f5 | 2014-12-02 23:49:50 | [diff] [blame] | 1006 | |
ddorwin | 301ccdb | 2016-02-25 02:39:17 | [diff] [blame] | 1007 | // TODO(xhwang): Update this UMA name. https://crbug.com/589251 |
xhwang | bab66f5 | 2014-12-02 23:49:50 | [diff] [blame] | 1008 | UMA_HISTOGRAM_COUNTS("Media.EME.NeedKey", 1); |
| 1009 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 1010 | // Recreate the watch time reporter if necessary. |
| 1011 | const bool was_encrypted = is_encrypted_; |
| 1012 | is_encrypted_ = true; |
| 1013 | if (!was_encrypted && watch_time_reporter_) |
| 1014 | CreateWatchTimeReporter(); |
| 1015 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1016 | encrypted_client_->Encrypted( |
davidben | b50f00c | 2015-12-01 00:01:50 | [diff] [blame] | 1017 | ConvertToWebInitDataType(init_data_type), init_data.data(), |
srirama.m | 26f864d0 | 2015-07-14 05:21:46 | [diff] [blame] | 1018 | base::saturated_cast<unsigned int>(init_data.size())); |
xhwang | bab66f5 | 2014-12-02 23:49:50 | [diff] [blame] | 1019 | } |
| 1020 | |
servolk | 81e01e0 | 2016-03-05 03:29:15 | [diff] [blame] | 1021 | void WebMediaPlayerImpl::OnFFmpegMediaTracksUpdated( |
dcheng | 3076abbf | 2016-04-22 20:42:39 | [diff] [blame] | 1022 | std::unique_ptr<MediaTracks> tracks) { |
servolk | 81e01e0 | 2016-03-05 03:29:15 | [diff] [blame] | 1023 | // For MSE/chunk_demuxer case the media track updates are handled by |
| 1024 | // WebSourceBufferImpl. |
| 1025 | DCHECK(demuxer_.get()); |
| 1026 | DCHECK(!chunk_demuxer_); |
servolk | ef1e5ef | 2016-03-25 04:55:26 | [diff] [blame] | 1027 | |
servolk | 16e8bdf8 | 2017-04-11 17:00:39 | [diff] [blame^] | 1028 | // Report the media track information to blink. Only the first audio track and |
| 1029 | // the first video track are enabled by default to match blink logic. |
| 1030 | bool is_first_audio_track = true; |
| 1031 | bool is_first_video_track = true; |
servolk | ef1e5ef | 2016-03-25 04:55:26 | [diff] [blame] | 1032 | for (const auto& track : tracks->tracks()) { |
| 1033 | if (track->type() == MediaTrack::Audio) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1034 | client_->AddAudioTrack(blink::WebString::FromUTF8(track->id()), |
| 1035 | blink::WebMediaPlayerClient::kAudioTrackKindMain, |
| 1036 | blink::WebString::FromUTF8(track->label()), |
| 1037 | blink::WebString::FromUTF8(track->language()), |
servolk | 16e8bdf8 | 2017-04-11 17:00:39 | [diff] [blame^] | 1038 | is_first_audio_track); |
| 1039 | is_first_audio_track = false; |
servolk | ef1e5ef | 2016-03-25 04:55:26 | [diff] [blame] | 1040 | } else if (track->type() == MediaTrack::Video) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1041 | client_->AddVideoTrack(blink::WebString::FromUTF8(track->id()), |
| 1042 | blink::WebMediaPlayerClient::kVideoTrackKindMain, |
| 1043 | blink::WebString::FromUTF8(track->label()), |
| 1044 | blink::WebString::FromUTF8(track->language()), |
servolk | 16e8bdf8 | 2017-04-11 17:00:39 | [diff] [blame^] | 1045 | is_first_video_track); |
| 1046 | is_first_video_track = false; |
servolk | ef1e5ef | 2016-03-25 04:55:26 | [diff] [blame] | 1047 | } else { |
| 1048 | // Text tracks are not supported through this code path yet. |
| 1049 | NOTREACHED(); |
| 1050 | } |
| 1051 | } |
servolk | 81e01e0 | 2016-03-05 03:29:15 | [diff] [blame] | 1052 | } |
| 1053 | |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1054 | void WebMediaPlayerImpl::SetCdm(blink::WebContentDecryptionModule* cdm) { |
| 1055 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1056 | DCHECK(cdm); |
xhwang | 79b19304 | 2016-12-13 18:52:43 | [diff] [blame] | 1057 | scoped_refptr<ContentDecryptionModule> cdm_reference = |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1058 | ToWebContentDecryptionModuleImpl(cdm)->GetCdm(); |
| 1059 | if (!cdm_reference) { |
| 1060 | NOTREACHED(); |
| 1061 | OnCdmAttached(false); |
xhwang | 8073945 | 2016-01-13 00:48:00 | [diff] [blame] | 1062 | return; |
| 1063 | } |
| 1064 | |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1065 | CdmContext* cdm_context = cdm_reference->GetCdmContext(); |
| 1066 | if (!cdm_context) { |
| 1067 | OnCdmAttached(false); |
| 1068 | return; |
| 1069 | } |
| 1070 | |
xjz | d3fe45a | 2016-10-12 18:26:37 | [diff] [blame] | 1071 | if (observer_) |
| 1072 | observer_->OnSetCdm(cdm_context); |
| 1073 | |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1074 | // Keep the reference to the CDM, as it shouldn't be destroyed until |
| 1075 | // after the pipeline is done with the |cdm_context|. |
| 1076 | pending_cdm_ = std::move(cdm_reference); |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 1077 | pipeline_controller_.SetCdm( |
| 1078 | cdm_context, base::Bind(&WebMediaPlayerImpl::OnCdmAttached, AsWeakPtr())); |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 1079 | } |
| 1080 | |
jrummell | 89e61d8 | 2015-07-23 20:03:34 | [diff] [blame] | 1081 | void WebMediaPlayerImpl::OnCdmAttached(bool success) { |
xhwang | 5113973 | 2017-02-24 19:36:08 | [diff] [blame] | 1082 | DVLOG(1) << __func__ << ": success = " << success; |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1083 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1084 | DCHECK(pending_cdm_); |
| 1085 | |
| 1086 | // If the CDM is set from the constructor there is no promise |
| 1087 | // (|set_cdm_result_|) to fulfill. |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 1088 | if (success) { |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1089 | // This will release the previously attached CDM (if any). |
| 1090 | cdm_ = std::move(pending_cdm_); |
| 1091 | if (set_cdm_result_) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1092 | set_cdm_result_->Complete(); |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1093 | set_cdm_result_.reset(); |
| 1094 | } |
| 1095 | |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 1096 | return; |
| 1097 | } |
| 1098 | |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1099 | pending_cdm_ = nullptr; |
| 1100 | if (set_cdm_result_) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1101 | set_cdm_result_->CompleteWithError( |
| 1102 | blink::kWebContentDecryptionModuleExceptionNotSupportedError, 0, |
xhwang | 79b19304 | 2016-12-13 18:52:43 | [diff] [blame] | 1103 | "Unable to set ContentDecryptionModule object"); |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1104 | set_cdm_result_.reset(); |
| 1105 | } |
[email protected] | 9ebc3b03f | 2014-08-13 04:01:23 | [diff] [blame] | 1106 | } |
| 1107 | |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1108 | void WebMediaPlayerImpl::OnPipelineSeeked(bool time_updated) { |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 1109 | seeking_ = false; |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 1110 | seek_time_ = base::TimeDelta(); |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 1111 | |
hubbe | 5a2dec02 | 2016-03-17 01:14:23 | [diff] [blame] | 1112 | if (paused_) { |
| 1113 | #if defined(OS_ANDROID) // WMPI_CAST |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1114 | if (IsRemote()) { |
hubbe | 5a2dec02 | 2016-03-17 01:14:23 | [diff] [blame] | 1115 | paused_time_ = base::TimeDelta::FromSecondsD(cast_impl_.currentTime()); |
| 1116 | } else { |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 1117 | paused_time_ = pipeline_controller_.GetMediaTime(); |
hubbe | 5a2dec02 | 2016-03-17 01:14:23 | [diff] [blame] | 1118 | } |
| 1119 | #else |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 1120 | paused_time_ = pipeline_controller_.GetMediaTime(); |
hubbe | 5a2dec02 | 2016-03-17 01:14:23 | [diff] [blame] | 1121 | #endif |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 1122 | } else { |
| 1123 | DCHECK(watch_time_reporter_); |
| 1124 | watch_time_reporter_->OnPlaying(); |
hubbe | 5a2dec02 | 2016-03-17 01:14:23 | [diff] [blame] | 1125 | } |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1126 | if (time_updated) |
| 1127 | should_notify_time_changed_ = true; |
dalecurtis | af34d871 | 2016-09-20 04:32:26 | [diff] [blame] | 1128 | |
dalecurtis | 4f6d14d | 2017-02-22 17:42:22 | [diff] [blame] | 1129 | // Reset underflow duration upon seek; this prevents looping videos and user |
| 1130 | // actions from artificially inflating the duration. |
| 1131 | underflow_timer_.reset(); |
avayvod | 56e1f394 | 2017-01-21 02:06:31 | [diff] [blame] | 1132 | |
| 1133 | // Background video optimizations are delayed when shown/hidden if pipeline |
| 1134 | // is seeking. |
| 1135 | UpdateBackgroundVideoOptimizationState(); |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 1136 | } |
| 1137 | |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1138 | void WebMediaPlayerImpl::OnPipelineSuspended() { |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1139 | #if defined(OS_ANDROID) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1140 | if (IsRemote()) { |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1141 | scoped_refptr<VideoFrame> frame = cast_impl_.GetCastingBanner(); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1142 | if (frame) |
dalecurtis | e9c89e9 | 2016-05-20 19:38:00 | [diff] [blame] | 1143 | compositor_->PaintSingleFrame(frame); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1144 | } |
| 1145 | #endif |
| 1146 | |
sandersd | 2f5bb615 | 2017-03-29 22:57:53 | [diff] [blame] | 1147 | // Tell the data source we have enough data so that it may release the |
| 1148 | // connection. |
| 1149 | if (data_source_) |
| 1150 | data_source_->OnBufferingHaveEnough(true); |
dalecurtis | 37fe586 | 2016-03-15 19:29:09 | [diff] [blame] | 1151 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1152 | ReportMemoryUsage(); |
| 1153 | |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1154 | if (pending_suspend_resume_cycle_) { |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 1155 | pending_suspend_resume_cycle_ = false; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1156 | UpdatePlayState(); |
dalecurtis | 0431cbf | 2016-03-12 01:19:43 | [diff] [blame] | 1157 | } |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1158 | } |
| 1159 | |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 1160 | void WebMediaPlayerImpl::OnBeforePipelineResume() { |
| 1161 | // Enable video track if we disabled it in the background - this way the new |
| 1162 | // renderer will attach its callbacks to the video stream properly. |
| 1163 | // TODO(avayvod): Remove this when disabling and enabling video tracks in |
| 1164 | // non-playing state works correctly. See https://crbug.com/678374. |
| 1165 | EnableVideoTrackIfNeeded(); |
| 1166 | is_pipeline_resuming_ = true; |
| 1167 | } |
| 1168 | |
| 1169 | void WebMediaPlayerImpl::OnPipelineResumed() { |
| 1170 | is_pipeline_resuming_ = false; |
| 1171 | |
avayvod | 56e1f394 | 2017-01-21 02:06:31 | [diff] [blame] | 1172 | UpdateBackgroundVideoOptimizationState(); |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 1173 | } |
| 1174 | |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1175 | void WebMediaPlayerImpl::OnDemuxerOpened() { |
| 1176 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1177 | client_->MediaSourceOpened( |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1178 | new WebMediaSourceImpl(chunk_demuxer_, media_log_)); |
| 1179 | } |
| 1180 | |
servolk | f94b460 | 2017-01-31 16:44:27 | [diff] [blame] | 1181 | void WebMediaPlayerImpl::OnMemoryPressure( |
| 1182 | base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
| 1183 | DVLOG(2) << __func__ << " memory_pressure_level=" << memory_pressure_level; |
| 1184 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1185 | DCHECK(base::FeatureList::IsEnabled(kMemoryPressureBasedSourceBufferGC)); |
| 1186 | DCHECK(chunk_demuxer_); |
| 1187 | |
| 1188 | // The new value of |memory_pressure_level| will take effect on the next |
| 1189 | // garbage collection. Typically this means the next SourceBuffer append() |
| 1190 | // operation, since per MSE spec, the garbage collection must only occur |
| 1191 | // during SourceBuffer append(). But if memory pressure is critical it might |
| 1192 | // be better to perform GC immediately rather than wait for the next append |
| 1193 | // and potentially get killed due to out-of-memory. |
| 1194 | // So if this experiment is enabled and pressure level is critical, we'll pass |
| 1195 | // down force_instant_gc==true, which will force immediate GC on |
| 1196 | // SourceBufferStreams. |
| 1197 | bool force_instant_gc = |
| 1198 | (enable_instant_source_buffer_gc_ && |
| 1199 | memory_pressure_level == |
| 1200 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 1201 | |
| 1202 | // base::Unretained is safe, since chunk_demuxer_ is actually owned by |
| 1203 | // |this| via this->demuxer_. |
| 1204 | media_task_runner_->PostTask( |
| 1205 | FROM_HERE, base::Bind(&ChunkDemuxer::OnMemoryPressure, |
| 1206 | base::Unretained(chunk_demuxer_), |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1207 | base::TimeDelta::FromSecondsD(CurrentTime()), |
servolk | f94b460 | 2017-01-31 16:44:27 | [diff] [blame] | 1208 | memory_pressure_level, force_instant_gc)); |
| 1209 | } |
| 1210 | |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1211 | void WebMediaPlayerImpl::OnError(PipelineStatus status) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 1212 | DVLOG(1) << __func__; |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1213 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1214 | DCHECK_NE(status, PIPELINE_OK); |
| 1215 | |
| 1216 | if (suppress_destruction_errors_) |
| 1217 | return; |
| 1218 | |
chcunningham | 8aeee0b5 | 2017-04-07 22:41:40 | [diff] [blame] | 1219 | ReportPipelineError(load_type_, status, media_log_); |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1220 | media_log_->AddEvent(media_log_->CreatePipelineErrorEvent(status)); |
| 1221 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1222 | if (ready_state_ == WebMediaPlayer::kReadyStateHaveNothing) { |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1223 | // Any error that occurs before reaching ReadyStateHaveMetadata should |
| 1224 | // be considered a format error. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1225 | SetNetworkState(WebMediaPlayer::kNetworkStateFormatError); |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1226 | } else { |
| 1227 | SetNetworkState(PipelineErrorToNetworkState(status)); |
| 1228 | } |
| 1229 | |
| 1230 | UpdatePlayState(); |
| 1231 | } |
| 1232 | |
| 1233 | void WebMediaPlayerImpl::OnEnded() { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 1234 | DVLOG(1) << __func__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1235 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 1236 | |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1237 | // Ignore state changes until we've completed all outstanding operations. |
| 1238 | if (!pipeline_controller_.IsStable()) |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 1239 | return; |
| 1240 | |
| 1241 | ended_ = true; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1242 | client_->TimeChanged(); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1243 | |
dalecurtis | 1af3c1a | 2017-04-11 00:53:49 | [diff] [blame] | 1244 | // Clear any new frame processed callbacks on end; otherwise we'll end up |
| 1245 | // logging a time long after playback ends. |
| 1246 | frame_time_report_cb_.Cancel(); |
| 1247 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1248 | // We don't actually want this to run until |client_| calls seek() or pause(), |
| 1249 | // but that should have already happened in timeChanged() and so this is |
| 1250 | // expected to be a no-op. |
| 1251 | UpdatePlayState(); |
[email protected] | 57653784 | 2009-08-12 23:52:05 | [diff] [blame] | 1252 | } |
| 1253 | |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1254 | void WebMediaPlayerImpl::OnMetadata(PipelineMetadata metadata) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 1255 | DVLOG(1) << __func__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1256 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 1257 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 1258 | pipeline_metadata_ = metadata; |
[email protected] | 739847c0 | 2014-01-16 00:12:25 | [diff] [blame] | 1259 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1260 | SetReadyState(WebMediaPlayer::kReadyStateHaveMetadata); |
dalecurtis | 849cf4b2 | 2015-03-27 18:35:45 | [diff] [blame] | 1261 | UMA_HISTOGRAM_ENUMERATION("Media.VideoRotation", metadata.video_rotation, |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1262 | VIDEO_ROTATION_MAX + 1); |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 1263 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1264 | if (HasVideo()) { |
liberato | 2fd111be | 2017-01-04 00:25:06 | [diff] [blame] | 1265 | if (overlay_enabled_) { |
| 1266 | // SurfaceView doesn't support rotated video, so transition back if |
| 1267 | // the video is now rotated. If |force_video_overlays_|, we keep the |
| 1268 | // overlay anyway so that the state machine keeps working. |
| 1269 | if (!force_video_overlays_ && !DoesOverlaySupportMetadata()) |
| 1270 | DisableOverlay(); |
| 1271 | else if (surface_manager_) |
| 1272 | surface_manager_->NaturalSizeChanged(pipeline_metadata_.natural_size); |
| 1273 | } |
watk | f835a79 | 2016-06-24 23:24:40 | [diff] [blame] | 1274 | |
| 1275 | DCHECK(!video_weblayer_); |
dalecurtis | e1edb31 | 2016-06-22 02:33:21 | [diff] [blame] | 1276 | video_weblayer_.reset(new cc_blink::WebLayerImpl(cc::VideoLayer::Create( |
| 1277 | compositor_, pipeline_metadata_.video_rotation))); |
jbauman | 952274d | 2015-09-10 23:23:36 | [diff] [blame] | 1278 | video_weblayer_->layer()->SetContentsOpaque(opaque_); |
| 1279 | video_weblayer_->SetContentsOpaqueIsFixed(true); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1280 | client_->SetWebLayer(video_weblayer_.get()); |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 1281 | } |
dalecurtis | 8e4dc68 | 2016-03-15 02:30:30 | [diff] [blame] | 1282 | |
xjz | d3fe45a | 2016-10-12 18:26:37 | [diff] [blame] | 1283 | if (observer_) |
xjz | 15b483f | 2017-01-12 00:21:36 | [diff] [blame] | 1284 | observer_->OnMetadataChanged(pipeline_metadata_); |
xjz | d3fe45a | 2016-10-12 18:26:37 | [diff] [blame] | 1285 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 1286 | CreateWatchTimeReporter(); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1287 | UpdatePlayState(); |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 1288 | } |
| 1289 | |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1290 | void WebMediaPlayerImpl::OnBufferingStateChange(BufferingState state) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 1291 | DVLOG(1) << __func__ << "(" << state << ")"; |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1292 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 1293 | |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1294 | // Ignore buffering state changes until we've completed all outstanding |
| 1295 | // operations. |
| 1296 | if (!pipeline_controller_.IsStable()) |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 1297 | return; |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 1298 | |
dalecurtis | 869bf2f | 2017-01-10 18:01:10 | [diff] [blame] | 1299 | media_log_->AddEvent(media_log_->CreateBufferingStateChangedEvent( |
| 1300 | "pipeline_buffering_state", state)); |
| 1301 | |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1302 | if (state == BUFFERING_HAVE_ENOUGH) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1303 | if (highest_ready_state_ < WebMediaPlayer::kReadyStateHaveEnoughData) { |
dalecurtis | 4f6d14d | 2017-02-22 17:42:22 | [diff] [blame] | 1304 | // Record a zero value for underflow histogram so that the histogram |
dalecurtis | 821e803f | 2016-09-01 18:44:15 | [diff] [blame] | 1305 | // includes playbacks which never encounter an underflow event. |
dalecurtis | 4f6d14d | 2017-02-22 17:42:22 | [diff] [blame] | 1306 | RecordUnderflowDuration(base::TimeDelta()); |
dalecurtis | 821e803f | 2016-09-01 18:44:15 | [diff] [blame] | 1307 | } |
| 1308 | |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1309 | // TODO(chcunningham): Monitor playback position vs buffered. Potentially |
| 1310 | // transition to HAVE_FUTURE_DATA here if not enough is buffered. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1311 | SetReadyState(WebMediaPlayer::kReadyStateHaveEnoughData); |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 1312 | |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1313 | // Let the DataSource know we have enough data. It may use this information |
| 1314 | // to release unused network connections. |
| 1315 | if (data_source_) |
| 1316 | data_source_->OnBufferingHaveEnough(false); |
dalecurtis | 849cf4b2 | 2015-03-27 18:35:45 | [diff] [blame] | 1317 | |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1318 | // Blink expects a timeChanged() in response to a seek(). |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1319 | if (should_notify_time_changed_) { |
| 1320 | should_notify_time_changed_ = false; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1321 | client_->TimeChanged(); |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1322 | } |
dalecurtis | 0f0097a | 2015-12-01 17:40:47 | [diff] [blame] | 1323 | |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1324 | // Once we have enough, start reporting the total memory usage. We'll also |
| 1325 | // report once playback starts. |
| 1326 | ReportMemoryUsage(); |
dalecurtis | 9d638a1 | 2016-08-30 06:20:55 | [diff] [blame] | 1327 | |
dalecurtis | 4f6d14d | 2017-02-22 17:42:22 | [diff] [blame] | 1328 | // Report the amount of time it took to leave the underflow state. |
| 1329 | if (underflow_timer_) { |
| 1330 | RecordUnderflowDuration(underflow_timer_->Elapsed()); |
dalecurtis | 9d638a1 | 2016-08-30 06:20:55 | [diff] [blame] | 1331 | underflow_timer_.reset(); |
| 1332 | } |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1333 | } else { |
| 1334 | // Buffering has underflowed. |
| 1335 | DCHECK_EQ(state, BUFFERING_HAVE_NOTHING); |
dalecurtis | acd77d6 | 2016-09-09 23:23:14 | [diff] [blame] | 1336 | |
dalecurtis | d06eabc | 2017-02-24 23:43:29 | [diff] [blame] | 1337 | // Report the number of times we've entered the underflow state. Ensure we |
| 1338 | // only report the value when transitioning from HAVE_ENOUGH to |
| 1339 | // HAVE_NOTHING. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1340 | if (ready_state_ == WebMediaPlayer::kReadyStateHaveEnoughData) |
dalecurtis | acd77d6 | 2016-09-09 23:23:14 | [diff] [blame] | 1341 | underflow_timer_.reset(new base::ElapsedTimer()); |
dalecurtis | acd77d6 | 2016-09-09 23:23:14 | [diff] [blame] | 1342 | |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1343 | // It shouldn't be possible to underflow if we've not advanced past |
| 1344 | // HAVE_CURRENT_DATA. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1345 | DCHECK_GT(highest_ready_state_, WebMediaPlayer::kReadyStateHaveCurrentData); |
| 1346 | SetReadyState(WebMediaPlayer::kReadyStateHaveCurrentData); |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1347 | } |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1348 | |
| 1349 | UpdatePlayState(); |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 1350 | } |
| 1351 | |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1352 | void WebMediaPlayerImpl::OnDurationChange() { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1353 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1354 | |
| 1355 | // TODO(sandersd): We should call delegate_->DidPlay() with the new duration, |
| 1356 | // especially if it changed from <5s to >5s. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1357 | if (ready_state_ == WebMediaPlayer::kReadyStateHaveNothing) |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1358 | return; |
| 1359 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1360 | client_->DurationChanged(); |
[email protected] | 81bb332 | 2011-07-21 15:55:50 | [diff] [blame] | 1361 | } |
| 1362 | |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1363 | void WebMediaPlayerImpl::OnAddTextTrack(const TextTrackConfig& config, |
| 1364 | const AddTextTrackDoneCB& done_cb) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1365 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 1366 | |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 1367 | const WebInbandTextTrackImpl::Kind web_kind = |
| 1368 | static_cast<WebInbandTextTrackImpl::Kind>(config.kind()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1369 | const blink::WebString web_label = blink::WebString::FromUTF8(config.label()); |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 1370 | const blink::WebString web_language = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1371 | blink::WebString::FromUTF8(config.language()); |
| 1372 | const blink::WebString web_id = blink::WebString::FromUTF8(config.id()); |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 1373 | |
dcheng | 3076abbf | 2016-04-22 20:42:39 | [diff] [blame] | 1374 | std::unique_ptr<WebInbandTextTrackImpl> web_inband_text_track( |
henriks | 9cce5fa | 2014-12-12 09:35:30 | [diff] [blame] | 1375 | new WebInbandTextTrackImpl(web_kind, web_label, web_language, web_id)); |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 1376 | |
dcheng | 3076abbf | 2016-04-22 20:42:39 | [diff] [blame] | 1377 | std::unique_ptr<media::TextTrack> text_track(new TextTrackImpl( |
dcheng | 652f5ff | 2015-12-27 08:54:00 | [diff] [blame] | 1378 | main_task_runner_, client_, std::move(web_inband_text_track))); |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 1379 | |
dcheng | 652f5ff | 2015-12-27 08:54:00 | [diff] [blame] | 1380 | done_cb.Run(std::move(text_track)); |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 1381 | } |
| 1382 | |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1383 | void WebMediaPlayerImpl::OnWaitingForDecryptionKey() { |
| 1384 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1385 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1386 | encrypted_client_->DidBlockPlaybackWaitingForKey(); |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1387 | // TODO(jrummell): didResumePlaybackBlockedForKey() should only be called |
| 1388 | // when a key has been successfully added (e.g. OnSessionKeysChange() with |
| 1389 | // |has_additional_usable_key| = true). http://crbug.com/461903 |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1390 | encrypted_client_->DidResumePlaybackBlockedForKey(); |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1391 | } |
| 1392 | |
alokp | 5d86e9b | 2016-05-17 20:20:41 | [diff] [blame] | 1393 | void WebMediaPlayerImpl::OnVideoNaturalSizeChange(const gfx::Size& size) { |
| 1394 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1395 | DCHECK_NE(ready_state_, WebMediaPlayer::kReadyStateHaveNothing); |
alokp | 5d86e9b | 2016-05-17 20:20:41 | [diff] [blame] | 1396 | |
xjz | 15b483f | 2017-01-12 00:21:36 | [diff] [blame] | 1397 | // The input |size| is from the decoded video frame, which is the original |
| 1398 | // natural size and need to be rotated accordingly. |
sandersd | 2c47842 | 2016-08-02 01:19:25 | [diff] [blame] | 1399 | gfx::Size rotated_size = |
| 1400 | GetRotatedVideoSize(pipeline_metadata_.video_rotation, size); |
| 1401 | |
| 1402 | if (rotated_size == pipeline_metadata_.natural_size) |
alokp | 5d86e9b | 2016-05-17 20:20:41 | [diff] [blame] | 1403 | return; |
| 1404 | |
| 1405 | TRACE_EVENT0("media", "WebMediaPlayerImpl::OnNaturalSizeChanged"); |
sandersd | 2c47842 | 2016-08-02 01:19:25 | [diff] [blame] | 1406 | media_log_->AddEvent(media_log_->CreateVideoSizeSetEvent( |
| 1407 | rotated_size.width(), rotated_size.height())); |
alokp | 5d86e9b | 2016-05-17 20:20:41 | [diff] [blame] | 1408 | |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 1409 | if (overlay_enabled_ && surface_manager_) |
sandersd | 2c47842 | 2016-08-02 01:19:25 | [diff] [blame] | 1410 | surface_manager_->NaturalSizeChanged(rotated_size); |
alokp | 5d86e9b | 2016-05-17 20:20:41 | [diff] [blame] | 1411 | |
xjz | 516ef6d | 2017-01-07 00:23:06 | [diff] [blame] | 1412 | gfx::Size old_size = pipeline_metadata_.natural_size; |
| 1413 | pipeline_metadata_.natural_size = rotated_size; |
| 1414 | if (old_size.IsEmpty()) { |
tguilbert | 796a40e | 2016-11-09 01:11:42 | [diff] [blame] | 1415 | // WatchTimeReporter doesn't report metrics for empty videos. Re-create |
| 1416 | // |watch_time_reporter_| if we didn't originally know the video size. |
| 1417 | CreateWatchTimeReporter(); |
| 1418 | } |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1419 | client_->SizeChanged(); |
xjz | 516ef6d | 2017-01-07 00:23:06 | [diff] [blame] | 1420 | |
xjz | 15b483f | 2017-01-12 00:21:36 | [diff] [blame] | 1421 | if (observer_) |
| 1422 | observer_->OnMetadataChanged(pipeline_metadata_); |
alokp | 5d86e9b | 2016-05-17 20:20:41 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | void WebMediaPlayerImpl::OnVideoOpacityChange(bool opaque) { |
| 1426 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1427 | DCHECK_NE(ready_state_, WebMediaPlayer::kReadyStateHaveNothing); |
alokp | 5d86e9b | 2016-05-17 20:20:41 | [diff] [blame] | 1428 | |
| 1429 | opaque_ = opaque; |
| 1430 | // Modify content opaqueness of cc::Layer directly so that |
| 1431 | // SetContentsOpaqueIsFixed is ignored. |
| 1432 | if (video_weblayer_) |
| 1433 | video_weblayer_->layer()->SetContentsOpaque(opaque_); |
| 1434 | } |
| 1435 | |
avayvod | eecec52c | 2017-02-14 01:25:09 | [diff] [blame] | 1436 | void WebMediaPlayerImpl::OnVideoAverageKeyframeDistanceUpdate() { |
| 1437 | UpdateBackgroundVideoOptimizationState(); |
| 1438 | } |
| 1439 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1440 | void WebMediaPlayerImpl::OnFrameHidden() { |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 1441 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
avayvod | 39c10240 | 2016-11-23 21:43:13 | [diff] [blame] | 1442 | |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 1443 | // Backgrounding a video requires a user gesture to resume playback. |
| 1444 | if (IsHidden()) |
| 1445 | video_locked_when_paused_when_hidden_ = true; |
| 1446 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 1447 | if (watch_time_reporter_) |
| 1448 | watch_time_reporter_->OnHidden(); |
avayvod | 48a8be5 | 2016-08-04 19:52:50 | [diff] [blame] | 1449 | |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 1450 | UpdateBackgroundVideoOptimizationState(); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1451 | UpdatePlayState(); |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 1452 | |
| 1453 | // Schedule suspended playing media to be paused if the user doesn't come back |
| 1454 | // to it within some timeout period to avoid any autoplay surprises. |
| 1455 | ScheduleIdlePauseTimer(); |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 1456 | } |
| 1457 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1458 | void WebMediaPlayerImpl::OnFrameClosed() { |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 1459 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1460 | UpdatePlayState(); |
| 1461 | } |
| 1462 | |
| 1463 | void WebMediaPlayerImpl::OnFrameShown() { |
| 1464 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1465 | background_pause_timer_.Stop(); |
| 1466 | |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 1467 | // Foreground videos don't require user gesture to continue playback. |
| 1468 | video_locked_when_paused_when_hidden_ = false; |
| 1469 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 1470 | if (watch_time_reporter_) |
| 1471 | watch_time_reporter_->OnShown(); |
| 1472 | |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 1473 | // Only track the time to the first frame if playing or about to play because |
| 1474 | // of being shown and only for videos we would optimize background playback |
| 1475 | // for. |
| 1476 | if ((!paused_ && IsBackgroundOptimizationCandidate()) || |
| 1477 | paused_when_hidden_) { |
dalecurtis | 1af3c1a | 2017-04-11 00:53:49 | [diff] [blame] | 1478 | frame_time_report_cb_.Reset( |
| 1479 | base::Bind(&WebMediaPlayerImpl::ReportTimeFromForegroundToFirstFrame, |
| 1480 | AsWeakPtr(), base::TimeTicks::Now())); |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 1481 | compositor_task_runner_->PostTask( |
| 1482 | FROM_HERE, |
| 1483 | base::Bind(&VideoFrameCompositor::SetOnNewProcessedFrameCallback, |
dalecurtis | 1af3c1a | 2017-04-11 00:53:49 | [diff] [blame] | 1484 | base::Unretained(compositor_), |
| 1485 | BindToCurrentLoop(frame_time_report_cb_.callback()))); |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 1486 | } |
avayvod | ac607d65 | 2017-01-06 03:16:43 | [diff] [blame] | 1487 | |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 1488 | EnableVideoTrackIfNeeded(); |
| 1489 | |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 1490 | if (paused_when_hidden_) { |
| 1491 | paused_when_hidden_ = false; |
| 1492 | OnPlay(); // Calls UpdatePlayState() so return afterwards. |
| 1493 | return; |
| 1494 | } |
| 1495 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1496 | UpdatePlayState(); |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 1497 | } |
| 1498 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1499 | void WebMediaPlayerImpl::OnIdleTimeout() { |
dalecurtis | 0431cbf | 2016-03-12 01:19:43 | [diff] [blame] | 1500 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1501 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1502 | // If we are attempting preroll, clear the stale flag. |
| 1503 | if (IsPrerollAttemptNeeded()) { |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 1504 | delegate_->ClearStaleFlag(delegate_id_); |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1505 | return; |
watk | d026f79 | 2016-11-05 00:28:51 | [diff] [blame] | 1506 | } |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1507 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1508 | UpdatePlayState(); |
dalecurtis | 0431cbf | 2016-03-12 01:19:43 | [diff] [blame] | 1509 | } |
| 1510 | |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 1511 | void WebMediaPlayerImpl::OnPlay() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1512 | Play(); |
| 1513 | client_->PlaybackStateChanged(); |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | void WebMediaPlayerImpl::OnPause() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1517 | Pause(); |
| 1518 | client_->PlaybackStateChanged(); |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | void WebMediaPlayerImpl::OnVolumeMultiplierUpdate(double multiplier) { |
| 1522 | volume_multiplier_ = multiplier; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1523 | SetVolume(volume_); |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 1524 | } |
| 1525 | |
zqzhang | 8ac4900 | 2017-03-16 21:51:35 | [diff] [blame] | 1526 | void WebMediaPlayerImpl::OnBecamePersistentVideo(bool value) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1527 | client_->OnBecamePersistentVideo(value); |
zqzhang | 8ac4900 | 2017-03-16 21:51:35 | [diff] [blame] | 1528 | } |
| 1529 | |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 1530 | void WebMediaPlayerImpl::ScheduleRestart() { |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1531 | // TODO(watk): All restart logic should be moved into PipelineController. |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 1532 | if (pipeline_controller_.IsPipelineRunning() && |
| 1533 | !pipeline_controller_.IsPipelineSuspended()) { |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 1534 | pending_suspend_resume_cycle_ = true; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1535 | UpdatePlayState(); |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 1536 | } |
| 1537 | } |
| 1538 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1539 | void WebMediaPlayerImpl::RequestRemotePlaybackDisabled(bool disabled) { |
miu | 77f914c | 2016-11-19 23:56:18 | [diff] [blame] | 1540 | if (observer_) |
| 1541 | observer_->OnRemotePlaybackDisabled(disabled); |
| 1542 | } |
| 1543 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1544 | #if defined(OS_ANDROID) // WMPI_CAST |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1545 | bool WebMediaPlayerImpl::IsRemote() const { |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1546 | return cast_impl_.isRemote(); |
| 1547 | } |
| 1548 | |
| 1549 | void WebMediaPlayerImpl::SetMediaPlayerManager( |
| 1550 | RendererMediaPlayerManagerInterface* media_player_manager) { |
| 1551 | cast_impl_.SetMediaPlayerManager(media_player_manager); |
| 1552 | } |
| 1553 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1554 | void WebMediaPlayerImpl::RequestRemotePlayback() { |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1555 | cast_impl_.requestRemotePlayback(); |
| 1556 | } |
| 1557 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1558 | void WebMediaPlayerImpl::RequestRemotePlaybackControl() { |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1559 | cast_impl_.requestRemotePlaybackControl(); |
| 1560 | } |
| 1561 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1562 | void WebMediaPlayerImpl::RequestRemotePlaybackStop() { |
avayvod | 8d8c53b | 2016-11-04 16:10:30 | [diff] [blame] | 1563 | cast_impl_.requestRemotePlaybackStop(); |
| 1564 | } |
| 1565 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1566 | void WebMediaPlayerImpl::OnRemotePlaybackEnded() { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 1567 | DVLOG(1) << __func__; |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1568 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1569 | |
| 1570 | ended_ = true; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1571 | client_->TimeChanged(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | void WebMediaPlayerImpl::OnDisconnectedFromRemoteDevice(double t) { |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1575 | DoSeek(base::TimeDelta::FromSecondsD(t), false); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1576 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1577 | // We already told the delegate we're paused when remoting started. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1578 | client_->PlaybackStateChanged(); |
| 1579 | client_->DisconnectedFromRemoteDevice(); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1580 | |
| 1581 | UpdatePlayState(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | void WebMediaPlayerImpl::SuspendForRemote() { |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1585 | if (pipeline_controller_.IsPipelineSuspended()) { |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1586 | scoped_refptr<VideoFrame> frame = cast_impl_.GetCastingBanner(); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1587 | if (frame) |
dalecurtis | e9c89e9 | 2016-05-20 19:38:00 | [diff] [blame] | 1588 | compositor_->PaintSingleFrame(frame); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1589 | } |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1590 | |
| 1591 | UpdatePlayState(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | gfx::Size WebMediaPlayerImpl::GetCanvasSize() const { |
| 1595 | if (!video_weblayer_) |
| 1596 | return pipeline_metadata_.natural_size; |
| 1597 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1598 | return video_weblayer_->Bounds(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | void WebMediaPlayerImpl::SetDeviceScaleFactor(float scale_factor) { |
| 1602 | cast_impl_.SetDeviceScaleFactor(scale_factor); |
| 1603 | } |
hubbe | e4027f9 | 2016-05-19 05:18:13 | [diff] [blame] | 1604 | |
tguilbert | 25a4d11 | 2016-10-13 21:56:51 | [diff] [blame] | 1605 | void WebMediaPlayerImpl::SetUseFallbackPath(bool use_fallback_path) { |
| 1606 | use_fallback_path_ = use_fallback_path; |
| 1607 | } |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1608 | #endif // defined(OS_ANDROID) // WMPI_CAST |
| 1609 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1610 | void WebMediaPlayerImpl::SetPoster(const blink::WebURL& poster) { |
xjz | c102fd8 | 2017-01-04 20:13:53 | [diff] [blame] | 1611 | #if defined(OS_ANDROID) |
| 1612 | cast_impl_.setPoster(poster); |
| 1613 | #endif |
| 1614 | |
| 1615 | if (observer_) |
| 1616 | observer_->OnSetPoster(poster); |
| 1617 | } |
| 1618 | |
[email protected] | fee8a90 | 2014-06-03 13:43:36 | [diff] [blame] | 1619 | void WebMediaPlayerImpl::DataSourceInitialized(bool success) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 1620 | DVLOG(1) << __func__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1621 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | a941529 | 2012-01-19 19:55:20 | [diff] [blame] | 1622 | |
dalecurtis | ea27a3ed | 2016-06-24 01:41:30 | [diff] [blame] | 1623 | #if defined(OS_ANDROID) |
| 1624 | // We can't play HLS URLs with WebMediaPlayerImpl, so in cases where they are |
| 1625 | // encountered, instruct the HTML media element to create a new WebMediaPlayer |
tguilbert | d7e079c | 2017-02-07 01:16:24 | [diff] [blame] | 1626 | // instance with the correct URL to trigger the creation of WMPI with a |
| 1627 | // MediaPlayerRendererFactory instead. |
dalecurtis | ea27a3ed | 2016-06-24 01:41:30 | [diff] [blame] | 1628 | // |
tguilbert | d7e079c | 2017-02-07 01:16:24 | [diff] [blame] | 1629 | // TODO(tguilbert): Allow 'hotswapping' renderer factories to prevent reloads |
| 1630 | // and/or rely on demuxer extracted MediaContainerNames. See crbug.com/663503. |
tguilbert | 25a4d11 | 2016-10-13 21:56:51 | [diff] [blame] | 1631 | if (data_source_ && !use_fallback_path_) { |
dalecurtis | ea27a3ed | 2016-06-24 01:41:30 | [diff] [blame] | 1632 | const GURL url_after_redirects = data_source_->GetUrlAfterRedirects(); |
qinmin | 0d952127 | 2016-10-10 20:43:19 | [diff] [blame] | 1633 | if (MediaCodecUtil::IsHLSURL(url_after_redirects)) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1634 | client_->RequestReload(url_after_redirects); |
dalecurtis | ea27a3ed | 2016-06-24 01:41:30 | [diff] [blame] | 1635 | // |this| may be destructed, do nothing after this. |
| 1636 | return; |
| 1637 | } |
| 1638 | } |
| 1639 | #endif |
| 1640 | |
[email protected] | d250190da3b | 2012-07-23 22:57:30 | [diff] [blame] | 1641 | if (!success) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1642 | SetNetworkState(WebMediaPlayer::kNetworkStateFormatError); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1643 | |
| 1644 | // Not really necessary, since the pipeline was never started, but it at |
| 1645 | // least this makes sure that the error handling code is in sync. |
| 1646 | UpdatePlayState(); |
| 1647 | |
[email protected] | a941529 | 2012-01-19 19:55:20 | [diff] [blame] | 1648 | return; |
| 1649 | } |
| 1650 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 1651 | StartPipeline(); |
[email protected] | a941529 | 2012-01-19 19:55:20 | [diff] [blame] | 1652 | } |
| 1653 | |
[email protected] | 122f4025 | 2012-06-12 05:01:56 | [diff] [blame] | 1654 | void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 1655 | DVLOG(1) << __func__; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1656 | if (!is_downloading && network_state_ == WebMediaPlayer::kNetworkStateLoading) |
| 1657 | SetNetworkState(WebMediaPlayer::kNetworkStateIdle); |
| 1658 | else if (is_downloading && |
| 1659 | network_state_ == WebMediaPlayer::kNetworkStateIdle) |
| 1660 | SetNetworkState(WebMediaPlayer::kNetworkStateLoading); |
[email protected] | 122f4025 | 2012-06-12 05:01:56 | [diff] [blame] | 1661 | media_log_->AddEvent( |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 1662 | media_log_->CreateBooleanEvent(MediaLogEvent::NETWORK_ACTIVITY_SET, |
| 1663 | "is_downloading_data", is_downloading)); |
[email protected] | 122f4025 | 2012-06-12 05:01:56 | [diff] [blame] | 1664 | } |
| 1665 | |
watk | f835a79 | 2016-06-24 23:24:40 | [diff] [blame] | 1666 | void WebMediaPlayerImpl::OnSurfaceCreated(int surface_id) { |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 1667 | overlay_surface_id_ = surface_id; |
dalecurtis | 4b632fce2 | 2016-11-10 00:52:17 | [diff] [blame] | 1668 | if (!set_surface_cb_.is_null()) { |
| 1669 | // If restart is required, the callback is one-shot only. |
| 1670 | if (decoder_requires_restart_for_overlay_) |
| 1671 | base::ResetAndReturn(&set_surface_cb_).Run(surface_id); |
| 1672 | else |
| 1673 | set_surface_cb_.Run(surface_id); |
| 1674 | } |
watk | f835a79 | 2016-06-24 23:24:40 | [diff] [blame] | 1675 | } |
| 1676 | |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 1677 | void WebMediaPlayerImpl::OnSurfaceRequested( |
dalecurtis | 4b632fce2 | 2016-11-10 00:52:17 | [diff] [blame] | 1678 | bool decoder_requires_restart_for_overlay, |
| 1679 | const SurfaceCreatedCB& set_surface_cb) { |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 1680 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1681 | DCHECK(surface_manager_); |
tguilbert | 25a4d11 | 2016-10-13 21:56:51 | [diff] [blame] | 1682 | DCHECK(!use_fallback_path_); |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 1683 | |
| 1684 | // A null callback indicates that the decoder is going away. |
dalecurtis | 4b632fce2 | 2016-11-10 00:52:17 | [diff] [blame] | 1685 | if (set_surface_cb.is_null()) { |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 1686 | decoder_requires_restart_for_overlay_ = false; |
dalecurtis | 4b632fce2 | 2016-11-10 00:52:17 | [diff] [blame] | 1687 | set_surface_cb_.Reset(); |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 1688 | return; |
| 1689 | } |
| 1690 | |
dalecurtis | 4b632fce2 | 2016-11-10 00:52:17 | [diff] [blame] | 1691 | // If we get a surface request it means GpuVideoDecoder is initializing, so |
| 1692 | // until we get a null surface request, GVD is the active decoder. |
| 1693 | // |
| 1694 | // If |decoder_requires_restart_for_overlay| is true, we must restart the |
| 1695 | // pipeline for fullscreen transitions. The decoder is unable to switch |
| 1696 | // surfaces otherwise. If false, we simply need to tell the decoder about the |
| 1697 | // new surface and it will handle things seamlessly. |
| 1698 | decoder_requires_restart_for_overlay_ = decoder_requires_restart_for_overlay; |
| 1699 | set_surface_cb_ = set_surface_cb; |
| 1700 | |
| 1701 | // If we're waiting for the surface to arrive, OnSurfaceCreated() will be |
| 1702 | // called later when it arrives; so do nothing for now. |
| 1703 | if (overlay_enabled_ && overlay_surface_id_ == SurfaceManager::kNoSurfaceID) |
| 1704 | return; |
| 1705 | |
| 1706 | OnSurfaceCreated(overlay_surface_id_); |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 1707 | } |
| 1708 | |
dcheng | 3076abbf | 2016-04-22 20:42:39 | [diff] [blame] | 1709 | std::unique_ptr<Renderer> WebMediaPlayerImpl::CreateRenderer() { |
dcheng | 37b415b9 | 2017-01-27 20:17:43 | [diff] [blame] | 1710 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1711 | |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 1712 | if (force_video_overlays_) |
| 1713 | EnableOverlay(); |
| 1714 | |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 1715 | RequestSurfaceCB request_surface_cb; |
| 1716 | #if defined(OS_ANDROID) |
dcheng | 37b415b9 | 2017-01-27 20:17:43 | [diff] [blame] | 1717 | request_surface_cb = BindToCurrentLoop( |
| 1718 | base::Bind(&WebMediaPlayerImpl::OnSurfaceRequested, AsWeakPtr())); |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 1719 | #endif |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 1720 | return renderer_factory_->CreateRenderer( |
| 1721 | media_task_runner_, worker_task_runner_, audio_source_provider_.get(), |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 1722 | compositor_, request_surface_cb); |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 1723 | } |
| 1724 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 1725 | void WebMediaPlayerImpl::StartPipeline() { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1726 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1727 | |
xhwang | e8c4181a | 2014-12-06 08:10:01 | [diff] [blame] | 1728 | Demuxer::EncryptedMediaInitDataCB encrypted_media_init_data_cb = |
dcheng | 37b415b9 | 2017-01-27 20:17:43 | [diff] [blame] | 1729 | BindToCurrentLoop(base::Bind( |
| 1730 | &WebMediaPlayerImpl::OnEncryptedMediaInitData, AsWeakPtr())); |
[email protected] | 2b57e2e | 2014-05-09 11:07:25 | [diff] [blame] | 1731 | |
tguilbert | 25a4d11 | 2016-10-13 21:56:51 | [diff] [blame] | 1732 | if (use_fallback_path_) { |
tguilbert | 9881bc2 | 2016-10-27 03:13:41 | [diff] [blame] | 1733 | demuxer_.reset( |
| 1734 | new MediaUrlDemuxer(media_task_runner_, fallback_url_, |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1735 | frame_->GetDocument().FirstPartyForCookies())); |
tguilbert | 25a4d11 | 2016-10-13 21:56:51 | [diff] [blame] | 1736 | pipeline_controller_.Start(demuxer_.get(), this, false, false); |
| 1737 | return; |
| 1738 | } |
| 1739 | |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1740 | // Figure out which demuxer to use. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1741 | if (load_type_ != kLoadTypeMediaSource) { |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1742 | DCHECK(!chunk_demuxer_); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1743 | DCHECK(data_source_); |
| 1744 | |
j.isorce | f6778e65 | 2015-11-16 17:14:25 | [diff] [blame] | 1745 | #if !defined(MEDIA_DISABLE_FFMPEG) |
servolk | 81e01e0 | 2016-03-05 03:29:15 | [diff] [blame] | 1746 | Demuxer::MediaTracksUpdatedCB media_tracks_updated_cb = |
dcheng | 37b415b9 | 2017-01-27 20:17:43 | [diff] [blame] | 1747 | BindToCurrentLoop(base::Bind( |
| 1748 | &WebMediaPlayerImpl::OnFFmpegMediaTracksUpdated, AsWeakPtr())); |
servolk | 81e01e0 | 2016-03-05 03:29:15 | [diff] [blame] | 1749 | |
xhwang | e8c4181a | 2014-12-06 08:10:01 | [diff] [blame] | 1750 | demuxer_.reset(new FFmpegDemuxer(media_task_runner_, data_source_.get(), |
servolk | 81e01e0 | 2016-03-05 03:29:15 | [diff] [blame] | 1751 | encrypted_media_init_data_cb, |
| 1752 | media_tracks_updated_cb, media_log_)); |
j.isorce | f6778e65 | 2015-11-16 17:14:25 | [diff] [blame] | 1753 | #else |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1754 | OnError(PipelineStatus::DEMUXER_ERROR_COULD_NOT_OPEN); |
j.isorce | f6778e65 | 2015-11-16 17:14:25 | [diff] [blame] | 1755 | return; |
| 1756 | #endif |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1757 | } else { |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1758 | DCHECK(!chunk_demuxer_); |
| 1759 | DCHECK(!data_source_); |
| 1760 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1761 | chunk_demuxer_ = new ChunkDemuxer( |
dcheng | 37b415b9 | 2017-01-27 20:17:43 | [diff] [blame] | 1762 | BindToCurrentLoop( |
| 1763 | base::Bind(&WebMediaPlayerImpl::OnDemuxerOpened, AsWeakPtr())), |
chcunningham | 967db2f | 2016-11-02 20:47:17 | [diff] [blame] | 1764 | encrypted_media_init_data_cb, media_log_); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1765 | demuxer_.reset(chunk_demuxer_); |
servolk | f94b460 | 2017-01-31 16:44:27 | [diff] [blame] | 1766 | |
| 1767 | if (base::FeatureList::IsEnabled(kMemoryPressureBasedSourceBufferGC)) { |
| 1768 | // base::Unretained is safe because |this| owns memory_pressure_listener_. |
| 1769 | memory_pressure_listener_ = |
| 1770 | base::MakeUnique<base::MemoryPressureListener>(base::Bind( |
| 1771 | &WebMediaPlayerImpl::OnMemoryPressure, base::Unretained(this))); |
| 1772 | } |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1773 | } |
| 1774 | |
sandersd | b5e2146 | 2016-03-09 01:49:07 | [diff] [blame] | 1775 | // TODO(sandersd): FileSystem objects may also be non-static, but due to our |
| 1776 | // caching layer such situations are broken already. http://crbug.com/593159 |
| 1777 | bool is_static = !chunk_demuxer_; |
avayvod | 102cdb6 | 2017-01-07 03:11:09 | [diff] [blame] | 1778 | bool is_streaming = IsStreaming(); |
sandersd | b8eb5f1d | 2016-11-19 04:04:02 | [diff] [blame] | 1779 | UMA_HISTOGRAM_BOOLEAN("Media.IsStreaming", is_streaming); |
sandersd | b5e2146 | 2016-03-09 01:49:07 | [diff] [blame] | 1780 | |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1781 | // ... and we're ready to go! |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 1782 | // TODO(sandersd): On Android, defer Start() if the tab is not visible. |
sandersd | b8eb5f1d | 2016-11-19 04:04:02 | [diff] [blame] | 1783 | seeking_ = true; |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1784 | pipeline_controller_.Start(demuxer_.get(), this, is_streaming, is_static); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 1788 | DVLOG(1) << __func__ << "(" << state << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1789 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1790 | network_state_ = state; |
| 1791 | // Always notify to ensure client has the latest value. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1792 | client_->NetworkStateChanged(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 1796 | DVLOG(1) << __func__ << "(" << state << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1797 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1798 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1799 | if (state == WebMediaPlayer::kReadyStateHaveEnoughData && data_source_ && |
[email protected] | fee8a90 | 2014-06-03 13:43:36 | [diff] [blame] | 1800 | data_source_->assume_fully_buffered() && |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1801 | network_state_ == WebMediaPlayer::kNetworkStateLoading) |
| 1802 | SetNetworkState(WebMediaPlayer::kNetworkStateLoaded); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1803 | |
| 1804 | ready_state_ = state; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1805 | highest_ready_state_ = std::max(highest_ready_state_, ready_state_); |
| 1806 | |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1807 | // Always notify to ensure client has the latest value. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1808 | client_->ReadyStateChanged(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1809 | } |
| 1810 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1811 | blink::WebAudioSourceProvider* WebMediaPlayerImpl::GetAudioSourceProvider() { |
[email protected] | ff875be5 | 2013-06-02 23:47:38 | [diff] [blame] | 1812 | return audio_source_provider_.get(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1813 | } |
| 1814 | |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 1815 | static void GetCurrentFrameAndSignal(VideoFrameCompositor* compositor, |
| 1816 | scoped_refptr<VideoFrame>* video_frame_out, |
| 1817 | base::WaitableEvent* event) { |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1818 | TRACE_EVENT0("media", "GetCurrentFrameAndSignal"); |
dalecurtis | 44ce4de | 2015-05-11 18:42:07 | [diff] [blame] | 1819 | *video_frame_out = compositor->GetCurrentFrameAndUpdateIfStale(); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1820 | event->Signal(); |
| 1821 | } |
| 1822 | |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 1823 | scoped_refptr<VideoFrame> WebMediaPlayerImpl::GetCurrentFrameFromCompositor() { |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 1824 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1825 | TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor"); |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 1826 | |
| 1827 | // Needed when the |main_task_runner_| and |compositor_task_runner_| are the |
| 1828 | // same to avoid deadlock in the Wait() below. |
kainino | 36eeff8 | 2017-03-30 00:55:30 | [diff] [blame] | 1829 | if (compositor_task_runner_->BelongsToCurrentThread()) { |
| 1830 | scoped_refptr<VideoFrame> video_frame = |
| 1831 | compositor_->GetCurrentFrameAndUpdateIfStale(); |
| 1832 | if (!video_frame) { |
| 1833 | return nullptr; |
| 1834 | } |
| 1835 | last_uploaded_frame_size_ = video_frame->natural_size(); |
| 1836 | last_uploaded_frame_timestamp_ = video_frame->timestamp(); |
| 1837 | return video_frame; |
| 1838 | } |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1839 | |
| 1840 | // Use a posted task and waitable event instead of a lock otherwise |
| 1841 | // WebGL/Canvas can see different content than what the compositor is seeing. |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1842 | scoped_refptr<VideoFrame> video_frame; |
gab | 0d77c7cb | 2016-06-02 00:00:23 | [diff] [blame] | 1843 | base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 1844 | base::WaitableEvent::InitialState::NOT_SIGNALED); |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 1845 | compositor_task_runner_->PostTask( |
| 1846 | FROM_HERE, |
| 1847 | base::Bind(&GetCurrentFrameAndSignal, base::Unretained(compositor_), |
| 1848 | &video_frame, &event)); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1849 | event.Wait(); |
kainino | 36eeff8 | 2017-03-30 00:55:30 | [diff] [blame] | 1850 | |
| 1851 | if (!video_frame) { |
| 1852 | return nullptr; |
| 1853 | } |
| 1854 | last_uploaded_frame_size_ = video_frame->natural_size(); |
| 1855 | last_uploaded_frame_timestamp_ = video_frame->timestamp(); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1856 | return video_frame; |
| 1857 | } |
| 1858 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1859 | void WebMediaPlayerImpl::UpdatePlayState() { |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 1860 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1861 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1862 | #if defined(OS_ANDROID) // WMPI_CAST |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1863 | bool is_remote = IsRemote(); |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 1864 | bool can_auto_suspend = true; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1865 | #else |
| 1866 | bool is_remote = false; |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 1867 | bool can_auto_suspend = !disable_pipeline_auto_suspend_ && !IsStreaming(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1868 | #endif |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 1869 | |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 1870 | bool is_suspended = pipeline_controller_.IsSuspended(); |
avayvod | 39c10240 | 2016-11-23 21:43:13 | [diff] [blame] | 1871 | bool is_backgrounded = IsBackgroundedSuspendEnabled() && IsHidden(); |
sandersd | aaff1a65 | 2016-11-17 01:47:25 | [diff] [blame] | 1872 | PlayState state = UpdatePlayState_ComputePlayState( |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 1873 | is_remote, can_auto_suspend, is_suspended, is_backgrounded); |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1874 | SetDelegateState(state.delegate_state, state.is_idle); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1875 | SetMemoryReportingState(state.is_memory_reporting_enabled); |
| 1876 | SetSuspendState(state.is_suspended || pending_suspend_resume_cycle_); |
| 1877 | } |
dalecurtis | 5bbc487e | 2016-02-27 04:15:05 | [diff] [blame] | 1878 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1879 | void WebMediaPlayerImpl::SetDelegateState(DelegateState new_state, |
| 1880 | bool is_idle) { |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 1881 | DCHECK(delegate_); |
dalecurtis | 5bbc487e | 2016-02-27 04:15:05 | [diff] [blame] | 1882 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1883 | // Prevent duplicate delegate calls. |
| 1884 | // TODO(sandersd): Move this deduplication into the delegate itself. |
| 1885 | // TODO(sandersd): WebContentsObserverSanityChecker does not allow sending the |
| 1886 | // 'playing' IPC more than once in a row, even if the metadata has changed. |
| 1887 | // Figure out whether it should. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1888 | bool has_audio = HasAudio() && !client_->IsAutoplayingMuted(); |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1889 | if (delegate_state_ == new_state && |
| 1890 | (delegate_state_ != DelegateState::PLAYING || |
| 1891 | delegate_has_audio_ == has_audio)) { |
| 1892 | return; |
mlamouri | 91011136 | 2016-11-04 11:28:24 | [diff] [blame] | 1893 | } |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1894 | delegate_state_ = new_state; |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1895 | delegate_has_audio_ = has_audio; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1896 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1897 | switch (new_state) { |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1898 | case DelegateState::GONE: |
| 1899 | delegate_->PlayerGone(delegate_id_); |
| 1900 | break; |
mlamouri | 91011136 | 2016-11-04 11:28:24 | [diff] [blame] | 1901 | case DelegateState::PLAYING: { |
zqzhang | 5d8eab7 | 2016-08-26 20:34:30 | [diff] [blame] | 1902 | delegate_->DidPlay( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1903 | delegate_id_, HasVideo(), has_audio, |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 1904 | media::DurationToMediaContentType(GetPipelineMediaDuration())); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1905 | break; |
mlamouri | 91011136 | 2016-11-04 11:28:24 | [diff] [blame] | 1906 | } |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1907 | case DelegateState::PAUSED: |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1908 | delegate_->DidPause(delegate_id_); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1909 | break; |
dalecurtis | 0f0097a | 2015-12-01 17:40:47 | [diff] [blame] | 1910 | } |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1911 | |
| 1912 | delegate_->SetIdle(delegate_id_, is_idle); |
dalecurtis | 0f0097a | 2015-12-01 17:40:47 | [diff] [blame] | 1913 | } |
| 1914 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1915 | void WebMediaPlayerImpl::SetMemoryReportingState( |
| 1916 | bool is_memory_reporting_enabled) { |
| 1917 | if (memory_usage_reporting_timer_.IsRunning() == |
| 1918 | is_memory_reporting_enabled) { |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1919 | return; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1920 | } |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1921 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1922 | if (is_memory_reporting_enabled) { |
| 1923 | memory_usage_reporting_timer_.Start(FROM_HERE, |
| 1924 | base::TimeDelta::FromSeconds(2), this, |
| 1925 | &WebMediaPlayerImpl::ReportMemoryUsage); |
| 1926 | } else { |
| 1927 | memory_usage_reporting_timer_.Stop(); |
| 1928 | ReportMemoryUsage(); |
| 1929 | } |
| 1930 | } |
| 1931 | |
| 1932 | void WebMediaPlayerImpl::SetSuspendState(bool is_suspended) { |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 1933 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
jameswest | 451a5bb | 2017-01-27 03:59:39 | [diff] [blame] | 1934 | if (!suspend_enabled_) { |
| 1935 | DCHECK(!pipeline_controller_.IsSuspended()); |
| 1936 | return; |
| 1937 | } |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 1938 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1939 | // Do not change the state after an error has occurred. |
| 1940 | // TODO(sandersd): Update PipelineController to remove the need for this. |
| 1941 | if (IsNetworkStateError(network_state_)) |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1942 | return; |
| 1943 | |
jameswest | 451a5bb | 2017-01-27 03:59:39 | [diff] [blame] | 1944 | if (is_suspended) { |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1945 | // If we were not resumed for long enough to satisfy the preroll attempt, |
| 1946 | // reset the clock. |
| 1947 | if (!preroll_attempt_pending_ && IsPrerollAttemptNeeded()) { |
| 1948 | preroll_attempt_pending_ = true; |
| 1949 | preroll_attempt_start_time_ = base::TimeTicks(); |
| 1950 | } |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1951 | pipeline_controller_.Suspend(); |
| 1952 | } else { |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1953 | // When resuming, start the preroll attempt clock. |
| 1954 | if (preroll_attempt_pending_) { |
| 1955 | preroll_attempt_pending_ = false; |
| 1956 | preroll_attempt_start_time_ = tick_clock_->NowTicks(); |
| 1957 | } |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1958 | pipeline_controller_.Resume(); |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | WebMediaPlayerImpl::PlayState |
| 1963 | WebMediaPlayerImpl::UpdatePlayState_ComputePlayState(bool is_remote, |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 1964 | bool can_auto_suspend, |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 1965 | bool is_suspended, |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1966 | bool is_backgrounded) { |
| 1967 | PlayState result; |
| 1968 | |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 1969 | bool must_suspend = delegate_->IsFrameClosed(); |
| 1970 | bool is_stale = delegate_->IsStale(delegate_id_); |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1971 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1972 | // This includes both data source (before pipeline startup) and pipeline |
| 1973 | // errors. |
| 1974 | bool has_error = IsNetworkStateError(network_state_); |
| 1975 | |
dalecurtis | cc8baf7 | 2016-10-27 01:49:44 | [diff] [blame] | 1976 | // After HaveFutureData, Blink will call play() if the state is not paused; |
| 1977 | // prior to this point |paused_| is not accurate. |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1978 | bool have_future_data = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1979 | highest_ready_state_ >= WebMediaPlayer::kReadyStateHaveFutureData; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1980 | |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 1981 | // Background suspend is only enabled for paused players. |
| 1982 | // In the case of players with audio the session should be kept. |
| 1983 | bool background_suspended = |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 1984 | can_auto_suspend && is_backgrounded && paused_ && have_future_data; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1985 | |
dalecurtis | cc8baf7 | 2016-10-27 01:49:44 | [diff] [blame] | 1986 | // Idle suspension is allowed prior to have future data since there exist |
| 1987 | // mechanisms to exit the idle state when the player is capable of reaching |
| 1988 | // the have future data state; see didLoadingProgress(). |
| 1989 | // |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1990 | // TODO(sandersd): Make the delegate suspend idle players immediately when |
| 1991 | // hidden. |
sandersd | aaff1a65 | 2016-11-17 01:47:25 | [diff] [blame] | 1992 | bool idle_suspended = |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 1993 | can_auto_suspend && is_stale && paused_ && !seeking_ && !overlay_enabled_; |
dalecurtis | e7120dc | 2016-09-03 02:54:35 | [diff] [blame] | 1994 | |
| 1995 | // If we're already suspended, see if we can wait for user interaction. Prior |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1996 | // to HaveFutureData, we require |is_stale| to remain suspended. |is_stale| |
dalecurtis | e7120dc | 2016-09-03 02:54:35 | [diff] [blame] | 1997 | // will be cleared when we receive data which may take us to HaveFutureData. |
| 1998 | bool can_stay_suspended = |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1999 | (is_stale || have_future_data) && is_suspended && paused_ && !seeking_; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2000 | |
| 2001 | // Combined suspend state. |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2002 | result.is_suspended = is_remote || must_suspend || idle_suspended || |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2003 | background_suspended || can_stay_suspended; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2004 | |
| 2005 | // We do not treat |playback_rate_| == 0 as paused. For the media session, |
| 2006 | // being paused implies displaying a play button, which is incorrect in this |
| 2007 | // case. For memory usage reporting, we just use the same definition (but we |
| 2008 | // don't have to). |
| 2009 | // |
| 2010 | // Similarly, we don't consider |ended_| to be paused. Blink will immediately |
| 2011 | // call pause() or seek(), so |ended_| should not affect the computation. |
| 2012 | // Despite that, |ended_| does result in a separate paused state, to simplfy |
| 2013 | // the contract for SetDelegateState(). |
| 2014 | // |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2015 | // |has_remote_controls| indicates if the player can be controlled outside the |
| 2016 | // page (e.g. via the notification controls or by audio focus events). Idle |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2017 | // suspension does not destroy the media session, because we expect that the |
avayvod | 5f34b64 | 2017-03-23 03:14:04 | [diff] [blame] | 2018 | // notification controls (and audio focus) remain. With some exceptions for |
| 2019 | // background videos, the player only needs to have audio to have controls |
| 2020 | // (requires |have_future_data|). |
| 2021 | // |
| 2022 | // |alive| indicates if the player should be present (not |GONE|) to the |
| 2023 | // delegate, either paused or playing. The following must be true for the |
| 2024 | // player: |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2025 | // - |have_future_data|, since we need to know whether we are paused to |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2026 | // correctly configure the session and also because the tracks and |
avayvod | 5f34b64 | 2017-03-23 03:14:04 | [diff] [blame] | 2027 | // duration are passed to DidPlay(), |
| 2028 | // - |is_remote| is false as remote playback is not handled by the delegate, |
| 2029 | // - |has_error| is false as player should have no errors, |
| 2030 | // - |background_suspended| is false, otherwise |has_remote_controls| must |
| 2031 | // be true. |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2032 | // |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2033 | // TODO(sandersd): If Blink told us the paused state sooner, we could detect |
| 2034 | // if the remote controls are available sooner. |
| 2035 | |
| 2036 | // Background videos with audio don't have remote controls if background |
| 2037 | // suspend is enabled and resuming background videos is not (original Android |
| 2038 | // behavior). |
| 2039 | bool backgrounded_video_has_no_remote_controls = |
| 2040 | IsBackgroundedSuspendEnabled() && !IsResumeBackgroundVideosEnabled() && |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2041 | is_backgrounded && HasVideo(); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2042 | bool can_play = !has_error && !is_remote && have_future_data; |
avayvod | 5f34b64 | 2017-03-23 03:14:04 | [diff] [blame] | 2043 | bool has_remote_controls = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2044 | HasAudio() && !backgrounded_video_has_no_remote_controls; |
avayvod | 5f34b64 | 2017-03-23 03:14:04 | [diff] [blame] | 2045 | bool alive = can_play && !must_suspend && |
| 2046 | (!background_suspended || has_remote_controls); |
| 2047 | if (!alive) { |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2048 | result.delegate_state = DelegateState::GONE; |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 2049 | result.is_idle = delegate_->IsIdle(delegate_id_); |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2050 | } else if (paused_) { |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2051 | // TODO(sandersd): Is it possible to have a suspended session, be ended, |
| 2052 | // and not be paused? If so we should be in a PLAYING state. |
dalecurtis | e7120dc | 2016-09-03 02:54:35 | [diff] [blame] | 2053 | result.delegate_state = |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2054 | ended_ ? DelegateState::GONE : DelegateState::PAUSED; |
| 2055 | result.is_idle = !seeking_; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2056 | } else { |
| 2057 | result.delegate_state = DelegateState::PLAYING; |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2058 | result.is_idle = false; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2059 | } |
| 2060 | |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 2061 | // It's not critical if some cases where memory usage can change are missed, |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2062 | // since media memory changes are usually gradual. |
| 2063 | result.is_memory_reporting_enabled = |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2064 | can_play && !result.is_suspended && (!paused_ || seeking_); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2065 | |
| 2066 | return result; |
dalecurtis | 0f0097a | 2015-12-01 17:40:47 | [diff] [blame] | 2067 | } |
| 2068 | |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 2069 | void WebMediaPlayerImpl::ReportMemoryUsage() { |
| 2070 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2071 | |
wdzierzanowski | fd4cd91c5 | 2015-12-02 23:50:20 | [diff] [blame] | 2072 | // About base::Unretained() usage below: We destroy |demuxer_| on the main |
| 2073 | // thread. Before that, however, ~WebMediaPlayerImpl() posts a task to the |
| 2074 | // media thread and waits for it to finish. Hence, the GetMemoryUsage() task |
| 2075 | // posted here must finish earlier. |
| 2076 | |
| 2077 | if (demuxer_) { |
| 2078 | base::PostTaskAndReplyWithResult( |
| 2079 | media_task_runner_.get(), FROM_HERE, |
| 2080 | base::Bind(&Demuxer::GetMemoryUsage, base::Unretained(demuxer_.get())), |
| 2081 | base::Bind(&WebMediaPlayerImpl::FinishMemoryUsageReport, AsWeakPtr())); |
| 2082 | } else { |
| 2083 | FinishMemoryUsageReport(0); |
| 2084 | } |
| 2085 | } |
| 2086 | |
| 2087 | void WebMediaPlayerImpl::FinishMemoryUsageReport(int64_t demuxer_memory_usage) { |
| 2088 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2089 | |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 2090 | const PipelineStatistics stats = GetPipelineStatistics(); |
servolk | 639473e49 | 2016-12-15 04:14:20 | [diff] [blame] | 2091 | const int64_t data_source_memory_usage = |
| 2092 | data_source_ ? data_source_->GetMemoryUsage() : 0; |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 2093 | const int64_t current_memory_usage = |
| 2094 | stats.audio_memory_usage + stats.video_memory_usage + |
servolk | 639473e49 | 2016-12-15 04:14:20 | [diff] [blame] | 2095 | data_source_memory_usage + demuxer_memory_usage; |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 2096 | |
dalecurtis | 3a7d38f4 | 2016-03-07 21:17:13 | [diff] [blame] | 2097 | // Note, this isn't entirely accurate, there may be VideoFrames held by the |
| 2098 | // compositor or other resources that we're unaware of. |
| 2099 | |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 2100 | DVLOG(2) << "Memory Usage -- Audio: " << stats.audio_memory_usage |
servolk | 639473e49 | 2016-12-15 04:14:20 | [diff] [blame] | 2101 | << ", Video: " << stats.video_memory_usage |
| 2102 | << ", DataSource: " << data_source_memory_usage |
wdzierzanowski | fd4cd91c5 | 2015-12-02 23:50:20 | [diff] [blame] | 2103 | << ", Demuxer: " << demuxer_memory_usage; |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 2104 | |
| 2105 | const int64_t delta = current_memory_usage - last_reported_memory_usage_; |
| 2106 | last_reported_memory_usage_ = current_memory_usage; |
| 2107 | adjust_allocated_memory_cb_.Run(delta); |
servolk | 639473e49 | 2016-12-15 04:14:20 | [diff] [blame] | 2108 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2109 | if (HasAudio()) { |
servolk | 639473e49 | 2016-12-15 04:14:20 | [diff] [blame] | 2110 | UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Audio", |
| 2111 | stats.audio_memory_usage / 1024); |
| 2112 | } |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2113 | if (HasVideo()) { |
servolk | 639473e49 | 2016-12-15 04:14:20 | [diff] [blame] | 2114 | UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Video", |
| 2115 | stats.video_memory_usage / 1024); |
| 2116 | } |
| 2117 | if (data_source_) { |
| 2118 | UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.DataSource", |
| 2119 | data_source_memory_usage / 1024); |
| 2120 | } |
| 2121 | if (demuxer_) { |
| 2122 | UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Demuxer", |
| 2123 | demuxer_memory_usage / 1024); |
| 2124 | } |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 2125 | } |
| 2126 | |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 2127 | void WebMediaPlayerImpl::ScheduleIdlePauseTimer() { |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2128 | // Only schedule the pause timer if we're not paused or paused but going to |
avayvod | 52efd28 | 2017-03-07 21:13:04 | [diff] [blame] | 2129 | // resume when foregrounded, and are suspended and have audio. |
| 2130 | if ((paused_ && !paused_when_hidden_) || |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2131 | !pipeline_controller_.IsSuspended() || !HasAudio()) { |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 2132 | return; |
avayvod | 52efd28 | 2017-03-07 21:13:04 | [diff] [blame] | 2133 | } |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 2134 | |
| 2135 | #if defined(OS_ANDROID) |
| 2136 | // Remote players will be suspended and locally paused. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2137 | if (IsRemote()) |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 2138 | return; |
| 2139 | #endif |
| 2140 | |
| 2141 | // Idle timeout chosen arbitrarily. |
| 2142 | background_pause_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(5), |
| 2143 | this, &WebMediaPlayerImpl::OnPause); |
| 2144 | } |
| 2145 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 2146 | void WebMediaPlayerImpl::CreateWatchTimeReporter() { |
| 2147 | // Create the watch time reporter and synchronize its initial state. |
shaktisahu | 24189c1 | 2017-03-11 17:42:55 | [diff] [blame] | 2148 | watch_time_reporter_.reset( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2149 | new WatchTimeReporter(HasAudio(), HasVideo(), !!chunk_demuxer_, |
shaktisahu | 24189c1 | 2017-03-11 17:42:55 | [diff] [blame] | 2150 | is_encrypted_, embedded_media_experience_enabled_, |
| 2151 | media_log_, pipeline_metadata_.natural_size, |
| 2152 | base::Bind(&GetCurrentTimeInternal, this))); |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 2153 | watch_time_reporter_->OnVolumeChange(volume_); |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 2154 | if (delegate_->IsFrameHidden()) |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 2155 | watch_time_reporter_->OnHidden(); |
| 2156 | else |
| 2157 | watch_time_reporter_->OnShown(); |
| 2158 | } |
| 2159 | |
avayvod | 39c10240 | 2016-11-23 21:43:13 | [diff] [blame] | 2160 | bool WebMediaPlayerImpl::IsHidden() const { |
| 2161 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2162 | |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 2163 | return delegate_->IsFrameHidden() && !delegate_->IsFrameClosed(); |
avayvod | 39c10240 | 2016-11-23 21:43:13 | [diff] [blame] | 2164 | } |
| 2165 | |
avayvod | 102cdb6 | 2017-01-07 03:11:09 | [diff] [blame] | 2166 | bool WebMediaPlayerImpl::IsStreaming() const { |
| 2167 | return data_source_ && data_source_->IsStreaming(); |
| 2168 | } |
| 2169 | |
liberato | 2fd111be | 2017-01-04 00:25:06 | [diff] [blame] | 2170 | bool WebMediaPlayerImpl::DoesOverlaySupportMetadata() const { |
| 2171 | return pipeline_metadata_.video_rotation == VIDEO_ROTATION_0; |
| 2172 | } |
| 2173 | |
xjz | af29d418 | 2016-12-16 01:52:32 | [diff] [blame] | 2174 | void WebMediaPlayerImpl::ActivateViewportIntersectionMonitoring(bool activate) { |
| 2175 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2176 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2177 | client_->ActivateViewportIntersectionMonitoring(activate); |
xjz | af29d418 | 2016-12-16 01:52:32 | [diff] [blame] | 2178 | } |
| 2179 | |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2180 | bool WebMediaPlayerImpl::ShouldPauseVideoWhenHidden() const { |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2181 | // If suspending background video, pause any video that's not remoted or |
| 2182 | // not unlocked to play in the background. |
| 2183 | if (IsBackgroundedSuspendEnabled()) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2184 | if (!HasVideo()) |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2185 | return false; |
| 2186 | |
| 2187 | #if defined(OS_ANDROID) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2188 | if (IsRemote()) |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2189 | return false; |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2190 | #endif |
avayvod | eb9098d | 2017-01-07 00:33:03 | [diff] [blame] | 2191 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2192 | return !HasAudio() || (IsResumeBackgroundVideosEnabled() && |
| 2193 | video_locked_when_paused_when_hidden_); |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | // Otherwise only pause if the optimization is on and it's a video-only |
| 2197 | // optimization candidate. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2198 | return IsBackgroundVideoTrackOptimizationEnabled() && !HasAudio() && |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2199 | IsBackgroundOptimizationCandidate(); |
avayvod | eb9098d | 2017-01-07 00:33:03 | [diff] [blame] | 2200 | } |
| 2201 | |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 2202 | bool WebMediaPlayerImpl::ShouldDisableVideoWhenHidden() const { |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2203 | // This optimization is behind the flag on all platforms. |
| 2204 | if (!IsBackgroundVideoTrackOptimizationEnabled()) |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 2205 | return false; |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 2206 | |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2207 | // Disable video track only for players with audio that match the criteria for |
| 2208 | // being optimized. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2209 | return HasAudio() && IsBackgroundOptimizationCandidate(); |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | bool WebMediaPlayerImpl::IsBackgroundOptimizationCandidate() const { |
| 2213 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2214 | |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2215 | #if defined(OS_ANDROID) // WMPI_CAST |
avayvod | ac1a852 | 2017-01-20 19:02:50 | [diff] [blame] | 2216 | // Don't optimize players being Cast. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2217 | if (IsRemote()) |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2218 | return false; |
avayvod | ac1a852 | 2017-01-20 19:02:50 | [diff] [blame] | 2219 | |
| 2220 | // Video-only players are always optimized (paused) on Android. |
| 2221 | // Don't check the keyframe distance and duration. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2222 | if (!HasAudio() && HasVideo()) |
avayvod | ac1a852 | 2017-01-20 19:02:50 | [diff] [blame] | 2223 | return true; |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2224 | #endif // defined(OS_ANDROID) |
| 2225 | |
| 2226 | // Don't optimize audio-only or streaming players. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2227 | if (!HasVideo() || IsStreaming()) |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2228 | return false; |
| 2229 | |
| 2230 | // Videos shorter than the maximum allowed keyframe distance can be optimized. |
| 2231 | base::TimeDelta duration = GetPipelineMediaDuration(); |
| 2232 | if (duration < max_keyframe_distance_to_disable_background_video_) |
| 2233 | return true; |
| 2234 | |
| 2235 | // Otherwise, only optimize videos with shorter average keyframe distance. |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 2236 | PipelineStatistics stats = GetPipelineStatistics(); |
| 2237 | return stats.video_keyframe_distance_average < |
| 2238 | max_keyframe_distance_to_disable_background_video_; |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 2239 | } |
| 2240 | |
avayvod | 56e1f394 | 2017-01-21 02:06:31 | [diff] [blame] | 2241 | void WebMediaPlayerImpl::UpdateBackgroundVideoOptimizationState() { |
| 2242 | if (IsHidden()) { |
| 2243 | if (ShouldPauseVideoWhenHidden()) |
| 2244 | PauseVideoIfNeeded(); |
| 2245 | else |
| 2246 | DisableVideoTrackIfNeeded(); |
| 2247 | } else { |
| 2248 | EnableVideoTrackIfNeeded(); |
| 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | void WebMediaPlayerImpl::PauseVideoIfNeeded() { |
| 2253 | DCHECK(IsHidden()); |
| 2254 | |
| 2255 | // Don't pause video while the pipeline is stopped, resuming or seeking. |
| 2256 | // Also if the video is paused already. |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 2257 | if (!pipeline_controller_.IsPipelineRunning() || is_pipeline_resuming_ || |
| 2258 | seeking_ || paused_) |
avayvod | 56e1f394 | 2017-01-21 02:06:31 | [diff] [blame] | 2259 | return; |
| 2260 | |
| 2261 | // OnPause() will set |paused_when_hidden_| to false and call |
| 2262 | // UpdatePlayState(), so set the flag to true after and then return. |
| 2263 | OnPause(); |
| 2264 | paused_when_hidden_ = true; |
| 2265 | } |
| 2266 | |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 2267 | void WebMediaPlayerImpl::EnableVideoTrackIfNeeded() { |
avayvod | 56e1f394 | 2017-01-21 02:06:31 | [diff] [blame] | 2268 | // Don't change video track while the pipeline is stopped, resuming or |
| 2269 | // seeking. |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 2270 | if (!pipeline_controller_.IsPipelineRunning() || is_pipeline_resuming_ || |
| 2271 | seeking_) |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 2272 | return; |
| 2273 | |
| 2274 | if (video_track_disabled_) { |
| 2275 | video_track_disabled_ = false; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2276 | if (client_->HasSelectedVideoTrack()) { |
| 2277 | WebMediaPlayer::TrackId trackId = client_->GetSelectedVideoTrackId(); |
| 2278 | SelectedVideoTrackChanged(&trackId); |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 2279 | } |
| 2280 | } |
| 2281 | } |
| 2282 | |
| 2283 | void WebMediaPlayerImpl::DisableVideoTrackIfNeeded() { |
| 2284 | DCHECK(IsHidden()); |
| 2285 | |
| 2286 | // Don't change video track while the pipeline is resuming or seeking. |
| 2287 | if (is_pipeline_resuming_ || seeking_) |
| 2288 | return; |
| 2289 | |
| 2290 | if (!video_track_disabled_ && ShouldDisableVideoWhenHidden()) { |
| 2291 | video_track_disabled_ = true; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2292 | SelectedVideoTrackChanged(nullptr); |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 2293 | } |
| 2294 | } |
| 2295 | |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 2296 | void WebMediaPlayerImpl::SetPipelineStatisticsForTest( |
| 2297 | const PipelineStatistics& stats) { |
| 2298 | pipeline_statistics_for_test_ = base::make_optional(stats); |
| 2299 | } |
| 2300 | |
| 2301 | PipelineStatistics WebMediaPlayerImpl::GetPipelineStatistics() const { |
| 2302 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2303 | |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 2304 | return pipeline_statistics_for_test_.value_or( |
| 2305 | pipeline_controller_.GetStatistics()); |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 2306 | } |
| 2307 | |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2308 | void WebMediaPlayerImpl::SetPipelineMediaDurationForTest( |
| 2309 | base::TimeDelta duration) { |
| 2310 | pipeline_media_duration_for_test_ = base::make_optional(duration); |
| 2311 | } |
| 2312 | |
| 2313 | base::TimeDelta WebMediaPlayerImpl::GetPipelineMediaDuration() const { |
| 2314 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2315 | |
| 2316 | return pipeline_media_duration_for_test_.value_or( |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 2317 | pipeline_controller_.GetMediaDuration()); |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | void WebMediaPlayerImpl::ReportTimeFromForegroundToFirstFrame( |
| 2321 | base::TimeTicks foreground_time, |
| 2322 | base::TimeTicks new_frame_time) { |
| 2323 | base::TimeDelta time_to_first_frame = new_frame_time - foreground_time; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2324 | if (HasAudio()) { |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2325 | UMA_HISTOGRAM_TIMES( |
| 2326 | "Media.Video.TimeFromForegroundToFirstFrame.DisableTrack", |
| 2327 | time_to_first_frame); |
| 2328 | } else { |
| 2329 | UMA_HISTOGRAM_TIMES("Media.Video.TimeFromForegroundToFirstFrame.Paused", |
| 2330 | time_to_first_frame); |
| 2331 | } |
| 2332 | } |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 2333 | void WebMediaPlayerImpl::SwitchRenderer(bool disable_pipeline_auto_suspend) { |
| 2334 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2335 | disable_pipeline_auto_suspend_ = disable_pipeline_auto_suspend; |
| 2336 | ScheduleRestart(); |
| 2337 | } |
| 2338 | |
dalecurtis | 4f6d14d | 2017-02-22 17:42:22 | [diff] [blame] | 2339 | void WebMediaPlayerImpl::RecordUnderflowDuration(base::TimeDelta duration) { |
| 2340 | DCHECK(data_source_ || chunk_demuxer_); |
| 2341 | if (data_source_) |
| 2342 | UMA_HISTOGRAM_TIMES("Media.UnderflowDuration", duration); |
| 2343 | else |
| 2344 | UMA_HISTOGRAM_TIMES("Media.UnderflowDuration.MSE", duration); |
| 2345 | } |
| 2346 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 2347 | } // namespace media |