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