[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> |
Gyuyoung Kim | 62a5de4 | 2018-01-10 09:48:42 | [diff] [blame] | 10 | #include <memory> |
guidou | c7babef | 2015-10-22 00:42:35 | [diff] [blame] | 11 | #include <string> |
dcheng | 652f5ff | 2015-12-27 08:54:00 | [diff] [blame] | 12 | #include <utility> |
[email protected] | 47b06ceb | 2010-08-04 22:41:11 | [diff] [blame] | 13 | |
[email protected] | 08273c7b | 2011-09-17 00:33:51 | [diff] [blame] | 14 | #include "base/bind.h" |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 15 | #include "base/bind_helpers.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 16 | #include "base/callback.h" |
[email protected] | 7502ef1 | 2014-01-25 01:19:27 | [diff] [blame] | 17 | #include "base/callback_helpers.h" |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 18 | #include "base/command_line.h" |
[email protected] | ca04095f | 2014-02-07 10:23:56 | [diff] [blame] | 19 | #include "base/debug/alias.h" |
[email protected] | 926f8fd | 2013-04-12 20:27:53 | [diff] [blame] | 20 | #include "base/debug/crash_logging.h" |
fdoray | db3ef7d | 2016-06-09 15:42:38 | [diff] [blame] | 21 | #include "base/location.h" |
Dale Curtis | 3899090ea | 2018-01-12 00:10:35 | [diff] [blame] | 22 | #include "base/metrics/histogram_functions.h" |
asvitkine | 3033081 | 2016-08-30 04:01:08 | [diff] [blame] | 23 | #include "base/metrics/histogram_macros.h" |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 24 | #include "base/single_thread_task_runner.h" |
servolk | a233f83 | 2016-06-10 20:39:04 | [diff] [blame] | 25 | #include "base/strings/string_number_conversions.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 26 | #include "base/task/post_task.h" |
wdzierzanowski | fd4cd91c5 | 2015-12-02 23:50:20 | [diff] [blame] | 27 | #include "base/task_runner_util.h" |
gab | ba14a44 | 2016-05-11 20:10:20 | [diff] [blame] | 28 | #include "base/threading/thread_task_runner_handle.h" |
ssid | 9525f467 | 2015-01-28 12:13:15 | [diff] [blame] | 29 | #include "base/trace_event/trace_event.h" |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 30 | #include "build/build_config.h" |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 31 | #include "cc/layers/video_layer.h" |
CJ DiMeglio | c60a5cf | 2017-09-27 20:08:41 | [diff] [blame] | 32 | #include "components/viz/common/gpu/context_provider.h" |
[email protected] | e4fc09e | 2012-04-06 03:17:44 | [diff] [blame] | 33 | #include "media/audio/null_audio_sink.h" |
[email protected] | 2eccbed | 2014-01-10 05:15:53 | [diff] [blame] | 34 | #include "media/base/bind_to_current_loop.h" |
xhwang | 0ad11e51 | 2014-11-25 23:43:09 | [diff] [blame] | 35 | #include "media/base/cdm_context.h" |
John Rummell | db5a7ef | 2018-05-16 00:28:01 | [diff] [blame] | 36 | #include "media/base/encryption_scheme.h" |
[email protected] | 32da100 | 2010-03-03 21:57:35 | [diff] [blame] | 37 | #include "media/base/limits.h" |
zqzhang | 5d8eab7 | 2016-08-26 20:34:30 | [diff] [blame] | 38 | #include "media/base/media_content_type.h" |
[email protected] | 090f731 | 2011-08-05 23:26:40 | [diff] [blame] | 39 | #include "media/base/media_log.h" |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 40 | #include "media/base/media_switches.h" |
tguilbert | 25a4d11 | 2016-10-13 21:56:51 | [diff] [blame] | 41 | #include "media/base/media_url_demuxer.h" |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 42 | #include "media/base/text_renderer.h" |
watk | 9f9dfdc9 | 2015-09-04 21:33:29 | [diff] [blame] | 43 | #include "media/base/timestamp_constants.h" |
[email protected] | e81283bb | 2010-08-31 18:01:21 | [diff] [blame] | 44 | #include "media/base/video_frame.h" |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 45 | #include "media/blink/texttrack_impl.h" |
John Delaney | b93339160 | 2018-10-17 21:50:47 | [diff] [blame] | 46 | #include "media/blink/url_index.h" |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 47 | #include "media/blink/video_decode_stats_reporter.h" |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 48 | #include "media/blink/watch_time_reporter.h" |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 49 | #include "media/blink/webaudiosourceprovider_impl.h" |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 50 | #include "media/blink/webcontentdecryptionmodule_impl.h" |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 51 | #include "media/blink/webinbandtexttrack_impl.h" |
| 52 | #include "media/blink/webmediaplayer_delegate.h" |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 53 | #include "media/blink/webmediaplayer_util.h" |
| 54 | #include "media/blink/webmediasource_impl.h" |
[email protected] | efe7cd2 | 2012-09-12 23:55:01 | [diff] [blame] | 55 | #include "media/filters/chunk_demuxer.h" |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 56 | #include "media/filters/ffmpeg_demuxer.h" |
Scott Violet | a35f9a4 | 2018-03-22 22:00:44 | [diff] [blame] | 57 | #include "media/media_buildflags.h" |
sawtelle | e19d11aa | 2018-08-10 03:47:50 | [diff] [blame] | 58 | #include "third_party/blink/public/common/picture_in_picture/picture_in_picture_control_info.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 59 | #include "third_party/blink/public/platform/web_encrypted_media_types.h" |
| 60 | #include "third_party/blink/public/platform/web_localized_string.h" |
| 61 | #include "third_party/blink/public/platform/web_media_player_client.h" |
| 62 | #include "third_party/blink/public/platform/web_media_player_encrypted_media_client.h" |
| 63 | #include "third_party/blink/public/platform/web_media_player_source.h" |
| 64 | #include "third_party/blink/public/platform/web_media_source.h" |
| 65 | #include "third_party/blink/public/platform/web_rect.h" |
| 66 | #include "third_party/blink/public/platform/web_runtime_features.h" |
| 67 | #include "third_party/blink/public/platform/web_security_origin.h" |
| 68 | #include "third_party/blink/public/platform/web_size.h" |
| 69 | #include "third_party/blink/public/platform/web_string.h" |
| 70 | #include "third_party/blink/public/platform/web_surface_layer_bridge.h" |
| 71 | #include "third_party/blink/public/platform/web_url.h" |
| 72 | #include "third_party/blink/public/web/web_document.h" |
| 73 | #include "third_party/blink/public/web/web_frame.h" |
| 74 | #include "third_party/blink/public/web/web_local_frame.h" |
| 75 | #include "third_party/blink/public/web/web_user_gesture_indicator.h" |
| 76 | #include "third_party/blink/public/web/web_view.h" |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 77 | |
dalecurtis | ea27a3ed | 2016-06-24 01:41:30 | [diff] [blame] | 78 | #if defined(OS_ANDROID) |
| 79 | #include "media/base/android/media_codec_util.h" |
| 80 | #endif |
| 81 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 82 | using blink::WebMediaPlayer; |
| 83 | using blink::WebRect; |
| 84 | using blink::WebSize; |
| 85 | using blink::WebString; |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 86 | using gpu::gles2::GLES2Interface; |
| 87 | |
danakj | 365175c | 2016-02-06 00:37:37 | [diff] [blame] | 88 | #define STATIC_ASSERT_ENUM(a, b) \ |
| 89 | static_assert(static_cast<int>(a) == static_cast<int>(b), \ |
| 90 | "mismatching enums: " #a) |
| 91 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 92 | namespace media { |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 93 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 94 | namespace { |
| 95 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 96 | void SetSinkIdOnMediaThread(scoped_refptr<WebAudioSourceProviderImpl> sink, |
| 97 | const std::string& device_id, |
Daniel Cheng | c1710b5 | 2018-10-24 03:12:28 | [diff] [blame^] | 98 | OutputDeviceStatusCB callback) { |
| 99 | sink->SwitchOutputDevice(device_id, std::move(callback)); |
guidou | c7babef | 2015-10-22 00:42:35 | [diff] [blame] | 100 | } |
| 101 | |
Luke Halliwell | 7a8a898 | 2018-07-25 01:07:05 | [diff] [blame] | 102 | bool IsBackgroundSuspendEnabled(WebMediaPlayerDelegate* delegate) { |
| 103 | // TODO(crbug.com/867146): remove these switches. |
| 104 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 105 | switches::kDisableMediaSuspend)) |
| 106 | return false; |
| 107 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 108 | switches::kEnableMediaSuspend)) |
| 109 | return true; |
| 110 | |
| 111 | return delegate->IsBackgroundMediaSuspendEnabled(); |
dalecurtis | 0431cbf | 2016-03-12 01:19:43 | [diff] [blame] | 112 | } |
| 113 | |
avayvod | 48a8be5 | 2016-08-04 19:52:50 | [diff] [blame] | 114 | bool IsResumeBackgroundVideosEnabled() { |
| 115 | return base::FeatureList::IsEnabled(kResumeBackgroundVideo); |
| 116 | } |
| 117 | |
Ted Meyer | a918e59 | 2018-09-08 00:16:20 | [diff] [blame] | 118 | bool IsBackgroundVideoTrackOptimizationEnabled( |
| 119 | WebMediaPlayer::LoadType load_type) { |
| 120 | // Background video track optimization is always enabled for MSE videos. |
| 121 | return load_type == WebMediaPlayer::LoadType::kLoadTypeMediaSource || |
| 122 | base::FeatureList::IsEnabled(kBackgroundSrcVideoTrackOptimization); |
avayvod | 39c10240 | 2016-11-23 21:43:13 | [diff] [blame] | 123 | } |
| 124 | |
avayvod | 0120133 | 2017-04-14 00:27:15 | [diff] [blame] | 125 | bool IsBackgroundVideoPauseOptimizationEnabled() { |
| 126 | return base::FeatureList::IsEnabled(kBackgroundVideoPauseOptimization); |
| 127 | } |
| 128 | |
avayvod | 8272927 | 2017-05-29 21:58:39 | [diff] [blame] | 129 | bool IsNewRemotePlaybackPipelineEnabled() { |
| 130 | return base::FeatureList::IsEnabled(kNewRemotePlaybackPipeline); |
| 131 | } |
| 132 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 133 | bool IsNetworkStateError(blink::WebMediaPlayer::NetworkState state) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 134 | bool result = state == blink::WebMediaPlayer::kNetworkStateFormatError || |
| 135 | state == blink::WebMediaPlayer::kNetworkStateNetworkError || |
| 136 | state == blink::WebMediaPlayer::kNetworkStateDecodeError; |
| 137 | DCHECK_EQ(state > blink::WebMediaPlayer::kNetworkStateLoaded, result); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 138 | return result; |
| 139 | } |
| 140 | |
sandersd | 2c47842 | 2016-08-02 01:19:25 | [diff] [blame] | 141 | gfx::Size GetRotatedVideoSize(VideoRotation rotation, gfx::Size natural_size) { |
| 142 | if (rotation == VIDEO_ROTATION_90 || rotation == VIDEO_ROTATION_270) |
| 143 | return gfx::Size(natural_size.height(), natural_size.width()); |
| 144 | return natural_size; |
| 145 | } |
| 146 | |
Xiaohan Wang | f63505d | 2017-10-21 08:00:53 | [diff] [blame] | 147 | void RecordEncryptedEvent(bool encrypted_event_fired) { |
| 148 | UMA_HISTOGRAM_BOOLEAN("Media.EME.EncryptedEvent", encrypted_event_fired); |
| 149 | } |
| 150 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 151 | // How much time must have elapsed since loading last progressed before we |
| 152 | // assume that the decoder will have had time to complete preroll. |
| 153 | constexpr base::TimeDelta kPrerollAttemptTimeout = |
watk | d026f79 | 2016-11-05 00:28:51 | [diff] [blame] | 154 | base::TimeDelta::FromSeconds(3); |
| 155 | |
Matt Wolenetz | 6010f6c | 2017-10-18 00:44:36 | [diff] [blame] | 156 | // Maximum number, per-WMPI, of media logs of playback rate changes. |
| 157 | constexpr int kMaxNumPlaybackRateLogs = 10; |
| 158 | |
Xiangjun Zhang | 5e20cba4 | 2018-01-10 19:54:56 | [diff] [blame] | 159 | blink::WebLocalizedString::Name GetSwitchToLocalMessage( |
| 160 | MediaObserverClient::ReasonToSwitchToLocal reason) { |
| 161 | switch (reason) { |
| 162 | case MediaObserverClient::ReasonToSwitchToLocal::NORMAL: |
| 163 | return blink::WebLocalizedString::kMediaRemotingStopText; |
| 164 | case MediaObserverClient::ReasonToSwitchToLocal::POOR_PLAYBACK_QUALITY: |
| 165 | return blink::WebLocalizedString::kMediaRemotingStopByPlaybackQualityText; |
| 166 | case MediaObserverClient::ReasonToSwitchToLocal::PIPELINE_ERROR: |
| 167 | return blink::WebLocalizedString::kMediaRemotingStopByErrorText; |
| 168 | case MediaObserverClient::ReasonToSwitchToLocal::ROUTE_TERMINATED: |
| 169 | return blink::WebLocalizedString::kMediaRemotingStopNoText; |
| 170 | } |
| 171 | NOTREACHED(); |
| 172 | // To suppress compiler warning on Windows. |
| 173 | return blink::WebLocalizedString::kMediaRemotingStopNoText; |
| 174 | } |
| 175 | |
John Rummell | db5a7ef | 2018-05-16 00:28:01 | [diff] [blame] | 176 | // These values are persisted to UMA. Entries should not be renumbered and |
| 177 | // numeric values should never be reused. |
| 178 | // TODO(crbug.com/825041): This should use EncryptionMode when kUnencrypted |
| 179 | // removed. |
| 180 | enum class EncryptionSchemeUMA { kCenc = 0, kCbcs = 1, kCount }; |
| 181 | |
| 182 | EncryptionSchemeUMA DetermineEncryptionSchemeUMAValue( |
| 183 | const EncryptionScheme& encryption_scheme) { |
| 184 | if (encryption_scheme.mode() == EncryptionScheme::CIPHER_MODE_AES_CBC) |
| 185 | return EncryptionSchemeUMA::kCbcs; |
| 186 | |
| 187 | DCHECK_EQ(encryption_scheme.mode(), EncryptionScheme::CIPHER_MODE_AES_CTR); |
| 188 | return EncryptionSchemeUMA::kCenc; |
| 189 | } |
| 190 | |
John Rummell | d3055535 | 2018-09-21 20:47:25 | [diff] [blame] | 191 | EncryptionMode DetermineEncryptionMode( |
| 192 | const EncryptionScheme& encryption_scheme) { |
| 193 | switch (encryption_scheme.mode()) { |
| 194 | case EncryptionScheme::CIPHER_MODE_UNENCRYPTED: |
| 195 | return EncryptionMode::kUnencrypted; |
| 196 | case EncryptionScheme::CIPHER_MODE_AES_CTR: |
| 197 | return EncryptionMode::kCenc; |
| 198 | case EncryptionScheme::CIPHER_MODE_AES_CBC: |
| 199 | return EncryptionMode::kCbcs; |
| 200 | } |
| 201 | } |
| 202 | |
Pavel Feldman | 023c3e6 | 2018-08-28 17:59:47 | [diff] [blame] | 203 | #if BUILDFLAG(ENABLE_FFMPEG) |
Dale Curtis | b8139f7 | 2018-08-27 23:28:48 | [diff] [blame] | 204 | // Returns true if |url| represents (or is likely to) a local file. |
| 205 | bool IsLocalFile(const GURL& url) { |
| 206 | return url.SchemeIsFile() || url.SchemeIsFileSystem() || |
| 207 | url.SchemeIs(url::kContentScheme) || |
| 208 | url.SchemeIs(url::kContentIDScheme) || |
| 209 | url.SchemeIs("chrome-extension"); |
| 210 | } |
Pavel Feldman | 023c3e6 | 2018-08-28 17:59:47 | [diff] [blame] | 211 | #endif |
Dale Curtis | b8139f7 | 2018-08-27 23:28:48 | [diff] [blame] | 212 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 213 | } // namespace |
| 214 | |
[email protected] | 6683e1b | 2014-04-10 01:45:38 | [diff] [blame] | 215 | class BufferedDataSourceHostImpl; |
| 216 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 217 | STATIC_ASSERT_ENUM(WebMediaPlayer::kCORSModeUnspecified, |
danakj | 365175c | 2016-02-06 00:37:37 | [diff] [blame] | 218 | UrlData::CORS_UNSPECIFIED); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 219 | STATIC_ASSERT_ENUM(WebMediaPlayer::kCORSModeAnonymous, UrlData::CORS_ANONYMOUS); |
| 220 | STATIC_ASSERT_ENUM(WebMediaPlayer::kCORSModeUseCredentials, |
danakj | 365175c | 2016-02-06 00:37:37 | [diff] [blame] | 221 | UrlData::CORS_USE_CREDENTIALS); |
[email protected] | a5a0110 | 2012-06-06 17:01:24 | [diff] [blame] | 222 | |
[email protected] | 5b5bb9d | 2010-10-22 19:57:36 | [diff] [blame] | 223 | WebMediaPlayerImpl::WebMediaPlayerImpl( |
[email protected] | 35b2a97 | 2014-04-04 15:50:22 | [diff] [blame] | 224 | blink::WebLocalFrame* frame, |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 225 | blink::WebMediaPlayerClient* client, |
srirama.m | 26f864d0 | 2015-07-14 05:21:46 | [diff] [blame] | 226 | blink::WebMediaPlayerEncryptedMediaClient* encrypted_client, |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 227 | WebMediaPlayerDelegate* delegate, |
tguilbert | 70d2a00a | 2017-04-25 00:30:44 | [diff] [blame] | 228 | std::unique_ptr<RendererFactorySelector> renderer_factory_selector, |
Alok Priyadarshi | 7166d4d | 2017-07-29 04:04:25 | [diff] [blame] | 229 | UrlIndex* url_index, |
CJ DiMeglio | d7ac677 | 2017-11-10 00:19:06 | [diff] [blame] | 230 | std::unique_ptr<VideoFrameCompositor> compositor, |
dalecurtis | 9cddc0b | 2017-04-19 21:23:38 | [diff] [blame] | 231 | std::unique_ptr<WebMediaPlayerParams> params) |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 232 | : frame_(frame), |
Alexander Timin | 31036811 | 2017-09-13 10:01:44 | [diff] [blame] | 233 | main_task_runner_( |
| 234 | frame->GetTaskRunner(blink::TaskType::kMediaElementEvent)), |
dalecurtis | 9cddc0b | 2017-04-19 21:23:38 | [diff] [blame] | 235 | media_task_runner_(params->media_task_runner()), |
| 236 | worker_task_runner_(params->worker_task_runner()), |
| 237 | media_log_(params->take_media_log()), |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 238 | pipeline_controller_( |
Hajime Hoshi | db0761a4 | 2018-01-18 08:10:19 | [diff] [blame] | 239 | std::make_unique<PipelineImpl>(media_task_runner_, |
| 240 | main_task_runner_, |
| 241 | media_log_.get()), |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 242 | base::Bind(&WebMediaPlayerImpl::CreateRenderer, |
| 243 | base::Unretained(this)), |
| 244 | base::Bind(&WebMediaPlayerImpl::OnPipelineSeeked, AsWeakPtr()), |
| 245 | base::Bind(&WebMediaPlayerImpl::OnPipelineSuspended, AsWeakPtr()), |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 246 | base::Bind(&WebMediaPlayerImpl::OnBeforePipelineResume, AsWeakPtr()), |
| 247 | base::Bind(&WebMediaPlayerImpl::OnPipelineResumed, AsWeakPtr()), |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 248 | base::Bind(&WebMediaPlayerImpl::OnError, AsWeakPtr())), |
[email protected] | 5badb08 | 2010-06-11 17:40:15 | [diff] [blame] | 249 | client_(client), |
srirama.m | 26f864d0 | 2015-07-14 05:21:46 | [diff] [blame] | 250 | encrypted_client_(encrypted_client), |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 251 | delegate_(delegate), |
dalecurtis | 9cddc0b | 2017-04-19 21:23:38 | [diff] [blame] | 252 | defer_load_cb_(params->defer_load_cb()), |
dalecurtis | 9cddc0b | 2017-04-19 21:23:38 | [diff] [blame] | 253 | adjust_allocated_memory_cb_(params->adjust_allocated_memory_cb()), |
tzik | 2c963b87 | 2017-12-07 06:57:24 | [diff] [blame] | 254 | tick_clock_(base::DefaultTickClock::GetInstance()), |
hubbe | b2d3efd | 2017-05-05 23:26:38 | [diff] [blame] | 255 | buffered_data_source_host_( |
| 256 | base::Bind(&WebMediaPlayerImpl::OnProgress, AsWeakPtr()), |
tzik | 2c963b87 | 2017-12-07 06:57:24 | [diff] [blame] | 257 | tick_clock_), |
hubbe | 5f0ad43b | 2015-12-14 20:57:26 | [diff] [blame] | 258 | url_index_(url_index), |
CJ DiMeglio | c60a5cf | 2017-09-27 20:08:41 | [diff] [blame] | 259 | context_provider_(params->context_provider()), |
CJ DiMeglio | d7ac677 | 2017-11-10 00:19:06 | [diff] [blame] | 260 | vfc_task_runner_(params->video_frame_compositor_task_runner()), |
| 261 | compositor_(std::move(compositor)), |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 262 | #if defined(OS_ANDROID) // WMPI_CAST |
CJ DiMeglio | c60a5cf | 2017-09-27 20:08:41 | [diff] [blame] | 263 | cast_impl_(this, client_, params->context_provider()), |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 264 | #endif |
tguilbert | 70d2a00a | 2017-04-25 00:30:44 | [diff] [blame] | 265 | renderer_factory_selector_(std::move(renderer_factory_selector)), |
dalecurtis | 9cddc0b | 2017-04-19 21:23:38 | [diff] [blame] | 266 | observer_(params->media_observer()), |
servolk | f94b460 | 2017-01-31 16:44:27 | [diff] [blame] | 267 | enable_instant_source_buffer_gc_( |
dalecurtis | 9cddc0b | 2017-04-19 21:23:38 | [diff] [blame] | 268 | params->enable_instant_source_buffer_gc()), |
shaktisahu | 24189c1 | 2017-03-11 17:42:55 | [diff] [blame] | 269 | embedded_media_experience_enabled_( |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 270 | params->embedded_media_experience_enabled()), |
[email protected] | 69db58f | 2018-09-26 20:27:56 | [diff] [blame] | 271 | surface_layer_mode_(params->use_surface_layer_for_video()), |
CJ DiMeglio | 96c18b69 | 2018-07-04 03:39:06 | [diff] [blame] | 272 | create_bridge_callback_(params->create_bridge_callback()), |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 273 | request_routing_token_cb_(params->request_routing_token_cb()), |
Dale Curtis | 1adbe6a | 2017-08-02 02:09:13 | [diff] [blame] | 274 | overlay_routing_token_(OverlayInfo::RoutingToken()), |
Mounir Lamouri | fe756e3 | 2018-05-22 23:19:08 | [diff] [blame] | 275 | media_metrics_provider_(params->take_metrics_provider()) { |
xhwang | 5113973 | 2017-02-24 19:36:08 | [diff] [blame] | 276 | DVLOG(1) << __func__; |
Dale Curtis | e2516381 | 2018-09-21 22:13:39 | [diff] [blame] | 277 | DCHECK(adjust_allocated_memory_cb_); |
tguilbert | 70d2a00a | 2017-04-25 00:30:44 | [diff] [blame] | 278 | DCHECK(renderer_factory_selector_); |
servolk | ef1e5ef | 2016-03-25 04:55:26 | [diff] [blame] | 279 | DCHECK(client_); |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 280 | DCHECK(delegate_); |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 281 | |
[email protected] | c8d57472 | 2017-08-30 20:53:43 | [diff] [blame] | 282 | // If we're supposed to force video overlays, then make sure that they're |
| 283 | // enabled all the time. |
| 284 | always_enable_overlays_ = base::CommandLine::ForCurrentProcess()->HasSwitch( |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 285 | switches::kForceVideoOverlays); |
| 286 | |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 287 | if (base::FeatureList::IsEnabled(media::kOverlayFullscreenVideo)) { |
| 288 | bool use_android_overlay = |
| 289 | base::FeatureList::IsEnabled(media::kUseAndroidOverlay); |
| 290 | overlay_mode_ = use_android_overlay ? OverlayMode::kUseAndroidOverlay |
| 291 | : OverlayMode::kUseContentVideoView; |
| 292 | } else { |
| 293 | overlay_mode_ = OverlayMode::kNoOverlays; |
| 294 | } |
amp | ea73f79 | 2017-01-19 04:05:39 | [diff] [blame] | 295 | |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 296 | delegate_id_ = delegate_->AddObserver(this); |
| 297 | delegate_->SetIdle(delegate_id_, true); |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 298 | |
dalecurtis | 2cff7f37 | 2017-05-24 08:30:08 | [diff] [blame] | 299 | media_log_->AddEvent(media_log_->CreateCreatedEvent( |
| 300 | url::Origin(frame_->GetSecurityOrigin()).GetURL().spec())); |
sandersd | 72683f25 | 2017-07-07 23:20:46 | [diff] [blame] | 301 | media_log_->SetStringProperty("frame_url", |
| 302 | frame_->GetDocument().Url().GetString().Utf8()); |
| 303 | media_log_->SetStringProperty("frame_title", |
| 304 | frame_->GetDocument().Title().Utf8()); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 305 | |
[email protected] | 52b0b21 | 2018-10-10 05:25:28 | [diff] [blame] | 306 | // To make manual testing easier, include |surface_layer_mode_| in the log. |
| 307 | // TODO(liberato): Move this into media_factory.cc, so that it can be shared |
| 308 | // with the MediaStream startup. |
| 309 | const char* surface_layer_mode_name = "(unset)"; |
| 310 | switch (surface_layer_mode_) { |
CJ DiMeglio | 8924047 | 2018-10-18 18:21:10 | [diff] [blame] | 311 | case SurfaceLayerMode::kAlways: |
[email protected] | 52b0b21 | 2018-10-10 05:25:28 | [diff] [blame] | 312 | surface_layer_mode_name = "kAlways"; |
| 313 | break; |
CJ DiMeglio | 8924047 | 2018-10-18 18:21:10 | [diff] [blame] | 314 | case SurfaceLayerMode::kOnDemand: |
[email protected] | 52b0b21 | 2018-10-10 05:25:28 | [diff] [blame] | 315 | surface_layer_mode_name = "kOnDemand"; |
| 316 | break; |
CJ DiMeglio | 8924047 | 2018-10-18 18:21:10 | [diff] [blame] | 317 | case SurfaceLayerMode::kNever: |
[email protected] | 52b0b21 | 2018-10-10 05:25:28 | [diff] [blame] | 318 | surface_layer_mode_name = "kNever"; |
| 319 | break; |
| 320 | } |
| 321 | media_log_->SetStringProperty("surface_layer_mode", surface_layer_mode_name); |
| 322 | |
dalecurtis | 9cddc0b | 2017-04-19 21:23:38 | [diff] [blame] | 323 | if (params->initial_cdm()) |
| 324 | SetCdm(params->initial_cdm()); |
xhwang | 0ad11e51 | 2014-11-25 23:43:09 | [diff] [blame] | 325 | |
Xiaohan Wang | f63505d | 2017-10-21 08:00:53 | [diff] [blame] | 326 | // Report a false "EncrytpedEvent" here as a baseline. |
| 327 | RecordEncryptedEvent(false); |
| 328 | |
xhwang | f94a634d | 2014-10-22 22:07:27 | [diff] [blame] | 329 | // TODO(xhwang): When we use an external Renderer, many methods won't work, |
xhwang | 6fa35620 | 2014-12-11 00:44:12 | [diff] [blame] | 330 | // e.g. GetCurrentFrameFromCompositor(). See http://crbug.com/434861 |
dalecurtis | 9cddc0b | 2017-04-19 21:23:38 | [diff] [blame] | 331 | audio_source_provider_ = new WebAudioSourceProviderImpl( |
| 332 | params->audio_renderer_sink(), media_log_.get()); |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 333 | |
| 334 | if (observer_) |
| 335 | observer_->SetClient(this); |
Hajime Hoshi | 8bb37dc | 2017-12-19 09:35:10 | [diff] [blame] | 336 | |
| 337 | memory_usage_reporting_timer_.SetTaskRunner( |
Hajime Hoshi | b5a26ee | 2018-05-14 12:47:51 | [diff] [blame] | 338 | frame_->GetTaskRunner(blink::TaskType::kInternalMedia)); |
John Delaney | 6c8ecc1b | 2018-10-21 19:07:29 | [diff] [blame] | 339 | |
| 340 | if (frame_->IsAdSubframe()) |
| 341 | media_metrics_provider_->SetIsAdMedia(); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 342 | } |
| 343 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 344 | WebMediaPlayerImpl::~WebMediaPlayerImpl() { |
xhwang | 5113973 | 2017-02-24 19:36:08 | [diff] [blame] | 345 | DVLOG(1) << __func__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 346 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 347 | |
xhwang | 5113973 | 2017-02-24 19:36:08 | [diff] [blame] | 348 | if (set_cdm_result_) { |
| 349 | DVLOG(2) << "Resolve pending SetCdm() when media player is destroyed."; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 350 | set_cdm_result_->Complete(); |
xhwang | 5113973 | 2017-02-24 19:36:08 | [diff] [blame] | 351 | set_cdm_result_.reset(); |
| 352 | } |
| 353 | |
alokp | 1116967f | 2016-06-11 17:30:56 | [diff] [blame] | 354 | suppress_destruction_errors_ = true; |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 355 | |
| 356 | delegate_->PlayerGone(delegate_id_); |
| 357 | delegate_->RemoveObserver(delegate_id_); |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 358 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 359 | // Finalize any watch time metrics before destroying the pipeline. |
| 360 | watch_time_reporter_.reset(); |
| 361 | |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 362 | // The underlying Pipeline must be stopped before it is destroyed. |
| 363 | pipeline_controller_.Stop(); |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 364 | |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 365 | if (last_reported_memory_usage_) |
| 366 | adjust_allocated_memory_cb_.Run(-last_reported_memory_usage_); |
| 367 | |
dalecurtis | e1edb31 | 2016-06-22 02:33:21 | [diff] [blame] | 368 | // Destruct compositor resources in the proper order. |
danakj | 8d204a4 | 2018-05-18 18:05:35 | [diff] [blame] | 369 | client_->SetCcLayer(nullptr); |
CJ DiMeglio | 2302d20 | 2017-08-31 08:38:04 | [diff] [blame] | 370 | |
Xiangjun Zhang | 5e20cba4 | 2018-01-10 19:54:56 | [diff] [blame] | 371 | client_->MediaRemotingStopped( |
| 372 | blink::WebLocalizedString::kMediaRemotingStopNoText); |
Xiangjun Zhang | 8729914 | 2017-09-13 20:35:03 | [diff] [blame] | 373 | |
danakj | 25f03011 | 2018-05-11 18:26:54 | [diff] [blame] | 374 | if (!surface_layer_for_video_enabled_ && video_layer_) { |
| 375 | video_layer_->StopUsingProvider(); |
lethalantidote | 7f6009d | 2017-07-07 21:47:39 | [diff] [blame] | 376 | } |
CJ DiMeglio | 2302d20 | 2017-08-31 08:38:04 | [diff] [blame] | 377 | |
| 378 | vfc_task_runner_->DeleteSoon(FROM_HERE, std::move(compositor_)); |
xhwang | ea8bb356 | 2015-06-08 21:18:37 | [diff] [blame] | 379 | |
Matt Wolenetz | 95af636 | 2018-01-04 20:23:42 | [diff] [blame] | 380 | if (chunk_demuxer_) { |
| 381 | // Continue destruction of |chunk_demuxer_| on the |media_task_runner_| to |
| 382 | // avoid racing other pending tasks on |chunk_demuxer_| on that runner while |
| 383 | // not further blocking |main_task_runner_| to perform the destruction. |
| 384 | media_task_runner_->PostTask( |
| 385 | FROM_HERE, base::BindOnce(&WebMediaPlayerImpl::DemuxerDestructionHelper, |
| 386 | media_task_runner_, std::move(demuxer_))); |
| 387 | } |
| 388 | |
xhwang | ea8bb356 | 2015-06-08 21:18:37 | [diff] [blame] | 389 | media_log_->AddEvent( |
| 390 | media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_DESTROYED)); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 391 | } |
| 392 | |
Matt Wolenetz | 95af636 | 2018-01-04 20:23:42 | [diff] [blame] | 393 | // static |
| 394 | void WebMediaPlayerImpl::DemuxerDestructionHelper( |
| 395 | scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 396 | std::unique_ptr<Demuxer> demuxer) { |
| 397 | DCHECK(task_runner->BelongsToCurrentThread()); |
| 398 | // ChunkDemuxer's streams may contain much buffered, compressed media that may |
| 399 | // need to be paged back in during destruction. Paging delay may exceed the |
| 400 | // renderer hang monitor's threshold on at least Windows while also blocking |
| 401 | // other work on the renderer main thread, so we do the actual destruction in |
| 402 | // the background without blocking WMPI destruction or |task_runner|. On |
| 403 | // advice of task_scheduler OWNERS, MayBlock() is not used because virtual |
| 404 | // memory overhead is not considered blocking I/O; and CONTINUE_ON_SHUTDOWN is |
| 405 | // used to allow process termination to not block on completing the task. |
| 406 | base::PostTaskWithTraits( |
| 407 | FROM_HERE, |
Gabriel Charette | b10aeebc | 2018-07-26 20:15:00 | [diff] [blame] | 408 | {base::TaskPriority::BEST_EFFORT, |
Matt Wolenetz | 95af636 | 2018-01-04 20:23:42 | [diff] [blame] | 409 | base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
| 410 | base::BindOnce( |
| 411 | [](std::unique_ptr<Demuxer> demuxer_to_destroy) { |
| 412 | SCOPED_UMA_HISTOGRAM_TIMER("Media.MSE.DemuxerDestructionTime"); |
| 413 | demuxer_to_destroy.reset(); |
| 414 | }, |
| 415 | std::move(demuxer))); |
| 416 | } |
| 417 | |
chcunningham | 707b821e | 2018-05-29 08:42:19 | [diff] [blame] | 418 | WebMediaPlayer::LoadTiming WebMediaPlayerImpl::Load( |
| 419 | LoadType load_type, |
| 420 | const blink::WebMediaPlayerSource& source, |
| 421 | CORSMode cors_mode) { |
xhwang | 5113973 | 2017-02-24 19:36:08 | [diff] [blame] | 422 | DVLOG(1) << __func__; |
guidou | 9bfe4e2f | 2016-04-09 08:31:19 | [diff] [blame] | 423 | // Only URL or MSE blob URL is supported. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 424 | DCHECK(source.IsURL()); |
| 425 | blink::WebURL url = source.GetAsURL(); |
Xiaohan Wang | 81fd002 | 2017-07-11 17:45:24 | [diff] [blame] | 426 | DVLOG(1) << __func__ << "(" << load_type << ", " << GURL(url) << ", " |
| 427 | << cors_mode << ")"; |
chcunningham | 707b821e | 2018-05-29 08:42:19 | [diff] [blame] | 428 | |
| 429 | bool is_deferred = false; |
| 430 | |
Dale Curtis | e2516381 | 2018-09-21 22:13:39 | [diff] [blame] | 431 | if (defer_load_cb_) { |
chcunningham | 707b821e | 2018-05-29 08:42:19 | [diff] [blame] | 432 | is_deferred = defer_load_cb_.Run(base::BindOnce( |
| 433 | &WebMediaPlayerImpl::DoLoad, AsWeakPtr(), load_type, url, cors_mode)); |
| 434 | } else { |
| 435 | DoLoad(load_type, url, cors_mode); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 436 | } |
chcunningham | 707b821e | 2018-05-29 08:42:19 | [diff] [blame] | 437 | |
| 438 | return is_deferred ? LoadTiming::kDeferred : LoadTiming::kImmediate; |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 439 | } |
| 440 | |
CJ DiMeglio | 013d4c47 | 2017-11-21 03:27:30 | [diff] [blame] | 441 | void WebMediaPlayerImpl::OnWebLayerUpdated() {} |
| 442 | |
danakj | 6a062b11 | 2018-05-17 16:25:45 | [diff] [blame] | 443 | void WebMediaPlayerImpl::RegisterContentsLayer(cc::Layer* layer) { |
CJ DiMeglio | 2302d20 | 2017-08-31 08:38:04 | [diff] [blame] | 444 | DCHECK(bridge_); |
CJ DiMeglio | a2b13fbc | 2018-06-27 00:50:59 | [diff] [blame] | 445 | bridge_->SetContentsOpaque(opaque_); |
danakj | 8d204a4 | 2018-05-18 18:05:35 | [diff] [blame] | 446 | client_->SetCcLayer(layer); |
CJ DiMeglio | 013d4c47 | 2017-11-21 03:27:30 | [diff] [blame] | 447 | } |
| 448 | |
danakj | 6a062b11 | 2018-05-17 16:25:45 | [diff] [blame] | 449 | void WebMediaPlayerImpl::UnregisterContentsLayer(cc::Layer* layer) { |
| 450 | // |client_| will unregister its cc::Layer if given a nullptr. |
danakj | 8d204a4 | 2018-05-18 18:05:35 | [diff] [blame] | 451 | client_->SetCcLayer(nullptr); |
CJ DiMeglio | 2302d20 | 2017-08-31 08:38:04 | [diff] [blame] | 452 | } |
| 453 | |
Jennifer Apacible | c45fd05 | 2018-02-25 12:04:55 | [diff] [blame] | 454 | void WebMediaPlayerImpl::OnSurfaceIdUpdated(viz::SurfaceId surface_id) { |
Jennifer Apacible | c45fd05 | 2018-02-25 12:04:55 | [diff] [blame] | 455 | // TODO(726619): Handle the behavior when Picture-in-Picture mode is |
| 456 | // disabled. |
Jennifer Apacible | 2b1dc5eb | 2018-04-27 16:23:28 | [diff] [blame] | 457 | // The viz::SurfaceId may be updated when the video begins playback or when |
| 458 | // the size of the video changes. |
Mounir Lamouri | 0484f40a | 2018-07-25 03:03:26 | [diff] [blame] | 459 | if (client_ && IsInPictureInPicture() && !client_->IsInAutoPIP()) { |
Mounir Lamouri | fe756e3 | 2018-05-22 23:19:08 | [diff] [blame] | 460 | delegate_->DidPictureInPictureSurfaceChange( |
François Beaufort | a22026d | 2018-09-25 08:51:11 | [diff] [blame] | 461 | delegate_id_, surface_id, pipeline_metadata_.natural_size, |
| 462 | true /* show_play_pause_button */); |
Mounir Lamouri | fe756e3 | 2018-05-22 23:19:08 | [diff] [blame] | 463 | } |
Jennifer Apacible | c45fd05 | 2018-02-25 12:04:55 | [diff] [blame] | 464 | } |
| 465 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 466 | bool WebMediaPlayerImpl::SupportsOverlayFullscreenVideo() { |
watk | 9c87c6fa | 2016-05-06 20:36:51 | [diff] [blame] | 467 | #if defined(OS_ANDROID) |
[email protected] | 68ec57f | 2017-06-27 22:10:05 | [diff] [blame] | 468 | return !using_media_player_renderer_ && |
| 469 | overlay_mode_ == OverlayMode::kUseContentVideoView; |
watk | 9c87c6fa | 2016-05-06 20:36:51 | [diff] [blame] | 470 | #else |
| 471 | return false; |
| 472 | #endif |
| 473 | } |
| 474 | |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 475 | void WebMediaPlayerImpl::EnableOverlay() { |
| 476 | overlay_enabled_ = true; |
[email protected] | f7df5b34 | 2018-07-13 20:22:13 | [diff] [blame] | 477 | if (request_routing_token_cb_ && |
| 478 | overlay_mode_ == OverlayMode::kUseAndroidOverlay) { |
liberato | fe8f969 | 2017-06-08 19:17:36 | [diff] [blame] | 479 | overlay_routing_token_is_pending_ = true; |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 480 | token_available_cb_.Reset( |
| 481 | base::Bind(&WebMediaPlayerImpl::OnOverlayRoutingToken, AsWeakPtr())); |
| 482 | request_routing_token_cb_.Run(token_available_cb_.callback()); |
watk | f835a79 | 2016-06-24 23:24:40 | [diff] [blame] | 483 | } |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 484 | |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 485 | // We have requested (and maybe already have) overlay information. If the |
| 486 | // restarted decoder requests overlay information, then we'll defer providing |
| 487 | // it if it hasn't arrived yet. Otherwise, this would be a race, since we |
| 488 | // don't know if the request for overlay info or restart will complete first. |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 489 | if (decoder_requires_restart_for_overlay_) |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 490 | ScheduleRestart(); |
| 491 | } |
| 492 | |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 493 | void WebMediaPlayerImpl::DisableOverlay() { |
| 494 | overlay_enabled_ = false; |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 495 | if (overlay_mode_ == OverlayMode::kUseContentVideoView) { |
| 496 | surface_created_cb_.Cancel(); |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 497 | } else if (overlay_mode_ == OverlayMode::kUseAndroidOverlay) { |
| 498 | token_available_cb_.Cancel(); |
liberato | fe8f969 | 2017-06-08 19:17:36 | [diff] [blame] | 499 | overlay_routing_token_is_pending_ = false; |
| 500 | overlay_routing_token_ = OverlayInfo::RoutingToken(); |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 501 | } |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 502 | |
| 503 | if (decoder_requires_restart_for_overlay_) |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 504 | ScheduleRestart(); |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 505 | else |
| 506 | MaybeSendOverlayInfoToDecoder(); |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 507 | } |
| 508 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 509 | void WebMediaPlayerImpl::EnteredFullscreen() { |
liberato | fe8f969 | 2017-06-08 19:17:36 | [diff] [blame] | 510 | overlay_info_.is_fullscreen = true; |
| 511 | |
[email protected] | c8d57472 | 2017-08-30 20:53:43 | [diff] [blame] | 512 | // |always_enable_overlays_| implies that we're already in overlay mode, so |
| 513 | // take no action here. Otherwise, switch to an overlay if it's allowed and |
| 514 | // if it will display properly. |
| 515 | if (!always_enable_overlays_ && overlay_mode_ != OverlayMode::kNoOverlays && |
liberato | 2fd111be | 2017-01-04 00:25:06 | [diff] [blame] | 516 | DoesOverlaySupportMetadata()) { |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 517 | EnableOverlay(); |
liberato | 2fd111be | 2017-01-04 00:25:06 | [diff] [blame] | 518 | } |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 519 | |
liberato | fe8f969 | 2017-06-08 19:17:36 | [diff] [blame] | 520 | // We send this only if we can send multiple calls. Otherwise, either (a) |
| 521 | // we already sent it and we don't have a callback anyway (we reset it when |
| 522 | // it's called in restart mode), or (b) we'll send this later when the surface |
| 523 | // actually arrives. GVD assumes that the first overlay info will have the |
| 524 | // routing information. Note that we set |is_fullscreen_| earlier, so that |
| 525 | // if EnableOverlay() can include fullscreen info in case it sends the overlay |
| 526 | // info before returning. |
| 527 | if (!decoder_requires_restart_for_overlay_) |
| 528 | MaybeSendOverlayInfoToDecoder(); |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 529 | } |
| 530 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 531 | void WebMediaPlayerImpl::ExitedFullscreen() { |
liberato | fe8f969 | 2017-06-08 19:17:36 | [diff] [blame] | 532 | overlay_info_.is_fullscreen = false; |
| 533 | |
[email protected] | c8d57472 | 2017-08-30 20:53:43 | [diff] [blame] | 534 | // If we're in overlay mode, then exit it unless we're supposed to allow |
| 535 | // overlays all the time. |
| 536 | if (!always_enable_overlays_ && overlay_enabled_) |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 537 | DisableOverlay(); |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 538 | |
liberato | fe8f969 | 2017-06-08 19:17:36 | [diff] [blame] | 539 | // See EnteredFullscreen for why we do this. |
| 540 | if (!decoder_requires_restart_for_overlay_) |
| 541 | MaybeSendOverlayInfoToDecoder(); |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 542 | } |
| 543 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 544 | void WebMediaPlayerImpl::BecameDominantVisibleContent(bool isDominant) { |
xjz | cdbbe73 | 2016-12-03 20:47:42 | [diff] [blame] | 545 | if (observer_) |
| 546 | observer_->OnBecameDominantVisibleContent(isDominant); |
| 547 | } |
| 548 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 549 | void WebMediaPlayerImpl::SetIsEffectivelyFullscreen( |
François Beaufort | ad6c523 | 2018-02-26 11:00:44 | [diff] [blame] | 550 | blink::WebFullscreenVideoStatus fullscreen_video_status) { |
| 551 | delegate_->SetIsEffectivelyFullscreen(delegate_id_, fullscreen_video_status); |
zqzhang | abc0824 | 2017-03-02 16:07:14 | [diff] [blame] | 552 | } |
| 553 | |
Mounir Lamouri | 41a79c6 | 2017-06-06 12:53:16 | [diff] [blame] | 554 | void WebMediaPlayerImpl::OnHasNativeControlsChanged(bool has_native_controls) { |
| 555 | if (!watch_time_reporter_) |
| 556 | return; |
| 557 | |
| 558 | if (has_native_controls) |
| 559 | watch_time_reporter_->OnNativeControlsEnabled(); |
| 560 | else |
| 561 | watch_time_reporter_->OnNativeControlsDisabled(); |
| 562 | } |
| 563 | |
Mounir Lamouri | f9af74e7 | 2017-06-19 19:31:45 | [diff] [blame] | 564 | void WebMediaPlayerImpl::OnDisplayTypeChanged( |
| 565 | WebMediaPlayer::DisplayType display_type) { |
Mounir Lamouri | 0484f40a | 2018-07-25 03:03:26 | [diff] [blame] | 566 | if (surface_layer_for_video_enabled_) { |
| 567 | vfc_task_runner_->PostTask( |
| 568 | FROM_HERE, |
| 569 | base::BindOnce( |
| 570 | &VideoFrameCompositor::SetForceSubmit, |
| 571 | base::Unretained(compositor_.get()), |
| 572 | display_type == WebMediaPlayer::DisplayType::kPictureInPicture)); |
| 573 | } |
| 574 | |
Mounir Lamouri | f9af74e7 | 2017-06-19 19:31:45 | [diff] [blame] | 575 | if (!watch_time_reporter_) |
| 576 | return; |
| 577 | |
| 578 | switch (display_type) { |
| 579 | case WebMediaPlayer::DisplayType::kInline: |
| 580 | watch_time_reporter_->OnDisplayTypeInline(); |
| 581 | break; |
| 582 | case WebMediaPlayer::DisplayType::kFullscreen: |
| 583 | watch_time_reporter_->OnDisplayTypeFullscreen(); |
| 584 | break; |
| 585 | case WebMediaPlayer::DisplayType::kPictureInPicture: |
| 586 | watch_time_reporter_->OnDisplayTypePictureInPicture(); |
| 587 | break; |
| 588 | } |
| 589 | } |
| 590 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 591 | void WebMediaPlayerImpl::DoLoad(LoadType load_type, |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 592 | const blink::WebURL& url, |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 593 | CORSMode cors_mode) { |
John Chen | 5b164a0 | 2017-11-01 00:36:09 | [diff] [blame] | 594 | TRACE_EVENT1("media", "WebMediaPlayerImpl::DoLoad", "id", media_log_->id()); |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 595 | DVLOG(1) << __func__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 596 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 597 | |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 598 | GURL gurl(url); |
John Delaney | bdcfc211 | 2018-09-18 16:20:23 | [diff] [blame] | 599 | ReportMetrics(load_type, gurl, *frame_, media_log_.get()); |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 600 | |
Dan Sanders | cd8981c | 2017-08-31 22:37:02 | [diff] [blame] | 601 | // Report poster availability for SRC=. |
| 602 | if (load_type == kLoadTypeURL) { |
| 603 | if (preload_ == MultibufferDataSource::METADATA) { |
| 604 | UMA_HISTOGRAM_BOOLEAN("Media.SRC.PreloadMetaDataHasPoster", has_poster_); |
| 605 | } else if (preload_ == MultibufferDataSource::AUTO) { |
| 606 | UMA_HISTOGRAM_BOOLEAN("Media.SRC.PreloadAutoHasPoster", has_poster_); |
| 607 | } |
| 608 | } |
| 609 | |
[email protected] | 926f8fd | 2013-04-12 20:27:53 | [diff] [blame] | 610 | // Set subresource URL for crash reporting. |
Robert Sesek | c5e91df | 2017-12-12 21:11:03 | [diff] [blame] | 611 | static base::debug::CrashKeyString* subresource_url = |
| 612 | base::debug::AllocateCrashKeyString("subresource_url", |
| 613 | base::debug::CrashKeySize::Size256); |
| 614 | base::debug::SetCrashKeyString(subresource_url, gurl.spec()); |
[email protected] | 926f8fd | 2013-04-12 20:27:53 | [diff] [blame] | 615 | |
tguilbert | 75e2bf6 | 2017-04-26 20:13:12 | [diff] [blame] | 616 | // Used for HLS playback. |
| 617 | loaded_url_ = gurl; |
tguilbert | 25a4d11 | 2016-10-13 21:56:51 | [diff] [blame] | 618 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 619 | load_type_ = load_type; |
| 620 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 621 | SetNetworkState(WebMediaPlayer::kNetworkStateLoading); |
| 622 | SetReadyState(WebMediaPlayer::kReadyStateHaveNothing); |
| 623 | media_log_->AddEvent(media_log_->CreateLoadEvent(url.GetString().Utf8())); |
Dale Curtis | 3899090ea | 2018-01-12 00:10:35 | [diff] [blame] | 624 | load_start_time_ = base::TimeTicks::Now(); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 625 | |
Dale Curtis | d131da1e | 2018-08-30 02:09:53 | [diff] [blame] | 626 | media_metrics_provider_->Initialize(load_type == kLoadTypeMediaSource, |
| 627 | load_type == kLoadTypeURL |
| 628 | ? GetMediaURLScheme(loaded_url_) |
| 629 | : mojom::MediaURLScheme::kUnknown); |
Dale Curtis | 74612b7 | 2017-12-14 20:56:19 | [diff] [blame] | 630 | |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 631 | // Media source pipelines can start immediately. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 632 | if (load_type == kLoadTypeMediaSource) { |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 633 | StartPipeline(); |
hubbe | 5f0ad43b | 2015-12-14 20:57:26 | [diff] [blame] | 634 | } else { |
John Delaney | b93339160 | 2018-10-17 21:50:47 | [diff] [blame] | 635 | auto url_data = |
| 636 | url_index_->GetByUrl(url, static_cast<UrlData::CORSMode>(cors_mode)); |
| 637 | // Notify |this| of bytes received by the network. |
| 638 | url_data->AddBytesReceivedCallback(BindToCurrentLoop(base::BindRepeating( |
| 639 | &WebMediaPlayerImpl::OnBytesReceived, AsWeakPtr()))); |
dalecurtis | b6e052f5 | 2016-08-25 00:35:55 | [diff] [blame] | 640 | data_source_.reset(new MultibufferDataSource( |
John Delaney | b93339160 | 2018-10-17 21:50:47 | [diff] [blame] | 641 | main_task_runner_, std::move(url_data), media_log_.get(), |
| 642 | &buffered_data_source_host_, |
dalecurtis | b6e052f5 | 2016-08-25 00:35:55 | [diff] [blame] | 643 | base::Bind(&WebMediaPlayerImpl::NotifyDownloading, AsWeakPtr()))); |
avayvod | e46d7bef | 2016-03-30 23:18:26 | [diff] [blame] | 644 | data_source_->SetPreload(preload_); |
Yoav Weiss | c34fb0d | 2018-01-08 05:37:27 | [diff] [blame] | 645 | data_source_->SetIsClientAudioElement(client_->IsAudioElement()); |
avayvod | e46d7bef | 2016-03-30 23:18:26 | [diff] [blame] | 646 | data_source_->Initialize( |
| 647 | base::Bind(&WebMediaPlayerImpl::DataSourceInitialized, AsWeakPtr())); |
hubbe | 5f0ad43b | 2015-12-14 20:57:26 | [diff] [blame] | 648 | } |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 649 | |
| 650 | #if defined(OS_ANDROID) // WMPI_CAST |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 651 | cast_impl_.Initialize(url, frame_, delegate_id_); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 652 | #endif |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 653 | } |
| 654 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 655 | void WebMediaPlayerImpl::Play() { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 656 | DVLOG(1) << __func__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 657 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 658 | |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 659 | // User initiated play unlocks background video playback. |
Mustaq Ahmed | e473e435 | 2017-11-04 01:04:25 | [diff] [blame] | 660 | if (blink::WebUserGestureIndicator::IsProcessingUserGesture(frame_)) |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 661 | video_locked_when_paused_when_hidden_ = false; |
| 662 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 663 | #if defined(OS_ANDROID) // WMPI_CAST |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 664 | if (IsRemote()) { |
dalecurtis | 6e2716d | 2016-01-21 04:49:15 | [diff] [blame] | 665 | cast_impl_.play(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 666 | return; |
| 667 | } |
| 668 | #endif |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 669 | // TODO(sandersd): Do we want to reset the idle timer here? |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 670 | delegate_->SetIdle(delegate_id_, false); |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 671 | paused_ = false; |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 672 | pipeline_controller_.SetPlaybackRate(playback_rate_); |
dalecurtis | 4619cd0 | 2016-09-22 21:39:10 | [diff] [blame] | 673 | background_pause_timer_.Stop(); |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 674 | |
[email protected] | 039b754 | 2013-10-17 22:06:25 | [diff] [blame] | 675 | if (data_source_) |
| 676 | data_source_->MediaIsPlaying(); |
[email protected] | 090f731 | 2011-08-05 23:26:40 | [diff] [blame] | 677 | |
xjz | 48a9cb7 | 2016-12-20 04:02:49 | [diff] [blame] | 678 | if (observer_) |
| 679 | observer_->OnPlaying(); |
| 680 | |
Mounir Lamouri | 89c0a1b | 2018-03-01 15:00:44 | [diff] [blame] | 681 | watch_time_reporter_->SetAutoplayInitiated(client_->WasAutoplayInitiated()); |
| 682 | |
Dale Curtis | 051fdf6 | 2017-08-05 00:21:13 | [diff] [blame] | 683 | // If we're seeking we'll trigger the watch time reporter upon seek completed; |
| 684 | // we don't want to start it here since the seek time is unstable. E.g., when |
| 685 | // playing content with a positive start time we would have a zero seek time. |
| 686 | if (!Seeking()) { |
| 687 | DCHECK(watch_time_reporter_); |
| 688 | watch_time_reporter_->OnPlaying(); |
| 689 | } |
| 690 | |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 691 | if (video_decode_stats_reporter_) |
| 692 | video_decode_stats_reporter_->OnPlaying(); |
| 693 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 694 | media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PLAY)); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 695 | UpdatePlayState(); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 696 | } |
| 697 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 698 | void WebMediaPlayerImpl::Pause() { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 699 | DVLOG(1) << __func__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 700 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 701 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 702 | // We update the paused state even when casting, since we expect pause() to be |
| 703 | // called when casting begins, and when we exit casting we should end up in a |
| 704 | // paused state. |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 705 | paused_ = true; |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 706 | |
avayvod | eb9098d | 2017-01-07 00:33:03 | [diff] [blame] | 707 | // No longer paused because it was hidden. |
| 708 | paused_when_hidden_ = false; |
| 709 | |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 710 | // User initiated pause locks background videos. |
Mustaq Ahmed | e473e435 | 2017-11-04 01:04:25 | [diff] [blame] | 711 | if (blink::WebUserGestureIndicator::IsProcessingUserGesture(frame_)) |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 712 | video_locked_when_paused_when_hidden_ = true; |
| 713 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 714 | #if defined(OS_ANDROID) // WMPI_CAST |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 715 | if (IsRemote()) { |
dalecurtis | 6e2716d | 2016-01-21 04:49:15 | [diff] [blame] | 716 | cast_impl_.pause(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 717 | return; |
| 718 | } |
| 719 | #endif |
| 720 | |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 721 | pipeline_controller_.SetPlaybackRate(0.0); |
Dale Curtis | dc5d47c | 2018-02-15 11:15:19 | [diff] [blame] | 722 | paused_time_ = pipeline_controller_.GetMediaTime(); |
[email protected] | 090f731 | 2011-08-05 23:26:40 | [diff] [blame] | 723 | |
xjz | 48a9cb7 | 2016-12-20 04:02:49 | [diff] [blame] | 724 | if (observer_) |
| 725 | observer_->OnPaused(); |
| 726 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 727 | DCHECK(watch_time_reporter_); |
| 728 | watch_time_reporter_->OnPaused(); |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 729 | |
| 730 | if (video_decode_stats_reporter_) |
| 731 | video_decode_stats_reporter_->OnPaused(); |
| 732 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 733 | media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PAUSE)); |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 734 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 735 | UpdatePlayState(); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 736 | } |
| 737 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 738 | void WebMediaPlayerImpl::Seek(double seconds) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 739 | DVLOG(1) << __func__ << "(" << seconds << "s)"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 740 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
servolk | 86b3d88fb | 2017-03-18 02:50:28 | [diff] [blame] | 741 | media_log_->AddEvent(media_log_->CreateSeekEvent(seconds)); |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 742 | DoSeek(base::TimeDelta::FromSecondsD(seconds), true); |
| 743 | } |
| 744 | |
| 745 | void WebMediaPlayerImpl::DoSeek(base::TimeDelta time, bool time_updated) { |
| 746 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
John Chen | 5b164a0 | 2017-11-01 00:36:09 | [diff] [blame] | 747 | TRACE_EVENT2("media", "WebMediaPlayerImpl::DoSeek", "target", |
| 748 | time.InSecondsF(), "id", media_log_->id()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 749 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 750 | #if defined(OS_ANDROID) // WMPI_CAST |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 751 | if (IsRemote()) { |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 752 | cast_impl_.seek(time); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 753 | return; |
| 754 | } |
| 755 | #endif |
| 756 | |
srirama.m | ccf67181 | 2015-01-08 11:59:13 | [diff] [blame] | 757 | ReadyState old_state = ready_state_; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 758 | if (ready_state_ > WebMediaPlayer::kReadyStateHaveMetadata) |
| 759 | SetReadyState(WebMediaPlayer::kReadyStateHaveMetadata); |
[email protected] | 1bb66680 | 2013-11-28 06:12:08 | [diff] [blame] | 760 | |
Dale Curtis | 051fdf6 | 2017-08-05 00:21:13 | [diff] [blame] | 761 | // When paused or ended, we know exactly what the current time is and can |
| 762 | // elide seeks to it. However, there are two cases that are not elided: |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 763 | // 1) When the pipeline state is not stable. |
| 764 | // In this case we just let |pipeline_controller_| decide what to do, as |
| 765 | // it has complete information. |
| 766 | // 2) For MSE. |
| 767 | // Because the buffers may have changed between seeks, MSE seeks are |
| 768 | // never elided. |
Dale Curtis | 051fdf6 | 2017-08-05 00:21:13 | [diff] [blame] | 769 | if (paused_ && pipeline_controller_.IsStable() && |
| 770 | (paused_time_ == time || |
| 771 | (ended_ && time == base::TimeDelta::FromSecondsD(Duration()))) && |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 772 | !chunk_demuxer_) { |
| 773 | // If the ready state was high enough before, we can indicate that the seek |
| 774 | // completed just by restoring it. Otherwise we will just wait for the real |
| 775 | // ready state change to eventually happen. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 776 | if (old_state == kReadyStateHaveEnoughData) { |
srirama.m | 8f4a3756 | 2014-12-13 08:16:18 | [diff] [blame] | 777 | main_task_runner_->PostTask( |
Dale Curtis | ee9be3001 | 2018-09-21 22:21:51 | [diff] [blame] | 778 | FROM_HERE, base::BindOnce(&WebMediaPlayerImpl::OnBufferingStateChange, |
| 779 | AsWeakPtr(), BUFFERING_HAVE_ENOUGH)); |
srirama.m | 36ab268 | 2014-12-11 04:20:01 | [diff] [blame] | 780 | } |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 781 | return; |
srirama.m | 36ab268 | 2014-12-11 04:20:01 | [diff] [blame] | 782 | } |
[email protected] | 44ff37c0 | 2009-10-24 01:03:03 | [diff] [blame] | 783 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 784 | // Call this before setting |seeking_| so that the current media time can be |
| 785 | // recorded by the reporter. |
| 786 | if (watch_time_reporter_) |
| 787 | watch_time_reporter_->OnSeeking(); |
| 788 | |
dalecurtis | 1af3c1a | 2017-04-11 00:53:49 | [diff] [blame] | 789 | // Clear any new frame processed callbacks on seek; otherwise we'll end up |
| 790 | // logging a time long after the seek completes. |
| 791 | frame_time_report_cb_.Cancel(); |
| 792 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 793 | // TODO(sandersd): Move |seeking_| to PipelineController. |
| 794 | // TODO(sandersd): Do we want to reset the idle timer here? |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 795 | delegate_->SetIdle(delegate_id_, false); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 796 | ended_ = false; |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 797 | seeking_ = true; |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 798 | seek_time_ = time; |
| 799 | if (paused_) |
| 800 | paused_time_ = time; |
| 801 | pipeline_controller_.Seek(time, time_updated); |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 802 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 803 | // This needs to be called after Seek() so that if a resume is triggered, it |
| 804 | // is to the correct time. |
| 805 | UpdatePlayState(); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 806 | } |
| 807 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 808 | void WebMediaPlayerImpl::SetRate(double rate) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 809 | DVLOG(1) << __func__ << "(" << rate << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 810 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 811 | |
Matt Wolenetz | 6010f6c | 2017-10-18 00:44:36 | [diff] [blame] | 812 | if (rate != playback_rate_) { |
| 813 | LIMITED_MEDIA_LOG(INFO, media_log_.get(), num_playback_rate_logs_, |
| 814 | kMaxNumPlaybackRateLogs) |
| 815 | << "Effective playback rate changed from " << playback_rate_ << " to " |
| 816 | << rate; |
| 817 | } |
| 818 | |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 819 | playback_rate_ = rate; |
| 820 | if (!paused_) { |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 821 | pipeline_controller_.SetPlaybackRate(rate); |
[email protected] | 039b754 | 2013-10-17 22:06:25 | [diff] [blame] | 822 | if (data_source_) |
| 823 | data_source_->MediaPlaybackRateChanged(rate); |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 824 | } |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 825 | } |
| 826 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 827 | void WebMediaPlayerImpl::SetVolume(double volume) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 828 | DVLOG(1) << __func__ << "(" << volume << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 829 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 830 | volume_ = volume; |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 831 | pipeline_controller_.SetVolume(volume_ * volume_multiplier_); |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 832 | if (watch_time_reporter_) |
| 833 | watch_time_reporter_->OnVolumeChange(volume); |
Becca Hughes | 9f6fd4b8 | 2017-06-15 10:01:40 | [diff] [blame] | 834 | delegate_->DidPlayerMutedStatusChange(delegate_id_, volume == 0.0); |
mlamouri | 91011136 | 2016-11-04 11:28:24 | [diff] [blame] | 835 | |
| 836 | // The play state is updated because the player might have left the autoplay |
| 837 | // muted state. |
| 838 | UpdatePlayState(); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 839 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 840 | |
François Beaufort | 9290aab | 2018-05-07 21:18:47 | [diff] [blame] | 841 | void WebMediaPlayerImpl::EnterPictureInPicture( |
Mounir Lamouri | 917143af | 2018-05-24 21:19:57 | [diff] [blame] | 842 | blink::WebMediaPlayer::PipWindowOpenedCallback callback) { |
[email protected] | 69db58f | 2018-09-26 20:27:56 | [diff] [blame] | 843 | if (!surface_layer_for_video_enabled_) |
| 844 | ActivateSurfaceLayerForVideo(); |
| 845 | |
Mounir Lamouri | b642a3c | 2018-07-09 15:45:31 | [diff] [blame] | 846 | DCHECK(bridge_); |
Mounir Lamouri | 37e8ae8 | 2018-05-31 02:38:46 | [diff] [blame] | 847 | |
Mounir Lamouri | b642a3c | 2018-07-09 15:45:31 | [diff] [blame] | 848 | const viz::SurfaceId& surface_id = bridge_->GetSurfaceId(); |
| 849 | DCHECK(surface_id.is_valid()); |
Jennifer Apacible | c45fd05 | 2018-02-25 12:04:55 | [diff] [blame] | 850 | |
Mounir Lamouri | 11e9ef4 | 2018-05-22 03:10:16 | [diff] [blame] | 851 | // Notifies the browser process that the player should now be in |
| 852 | // Picture-in-Picture mode. |
François Beaufort | a22026d | 2018-09-25 08:51:11 | [diff] [blame] | 853 | delegate_->DidPictureInPictureModeStart( |
| 854 | delegate_id_, surface_id, pipeline_metadata_.natural_size, |
| 855 | std::move(callback), true /* show_play_pause_button */); |
Jennifer Apacible | 3f048910 | 2018-01-19 20:10:31 | [diff] [blame] | 856 | } |
| 857 | |
François Beaufort | 9290aab | 2018-05-07 21:18:47 | [diff] [blame] | 858 | void WebMediaPlayerImpl::ExitPictureInPicture( |
| 859 | blink::WebMediaPlayer::PipWindowClosedCallback callback) { |
Mounir Lamouri | 6d759e1 | 2018-05-16 20:01:30 | [diff] [blame] | 860 | // Notifies the browser process that Picture-in-Picture has ended. It will |
| 861 | // clear out the states and close the window. |
| 862 | delegate_->DidPictureInPictureModeEnd(delegate_id_, std::move(callback)); |
Jennifer Apacible | 02897b7 | 2018-04-01 01:00:49 | [diff] [blame] | 863 | |
Mounir Lamouri | 6d759e1 | 2018-05-16 20:01:30 | [diff] [blame] | 864 | // Internal cleanups. |
Jennifer Apacible | 5cd9a7e | 2018-04-17 03:08:06 | [diff] [blame] | 865 | OnPictureInPictureModeEnded(); |
Jennifer Apacible | 02897b7 | 2018-04-01 01:00:49 | [diff] [blame] | 866 | } |
| 867 | |
sawtelle | e19d11aa | 2018-08-10 03:47:50 | [diff] [blame] | 868 | void WebMediaPlayerImpl::SetPictureInPictureCustomControls( |
| 869 | const std::vector<blink::PictureInPictureControlInfo>& controls) { |
| 870 | delegate_->DidSetPictureInPictureCustomControls(delegate_id_, controls); |
| 871 | } |
| 872 | |
Mounir Lamouri | 917143af | 2018-05-24 21:19:57 | [diff] [blame] | 873 | void WebMediaPlayerImpl::RegisterPictureInPictureWindowResizeCallback( |
| 874 | blink::WebMediaPlayer::PipWindowResizedCallback callback) { |
Mounir Lamouri | 0484f40a | 2018-07-25 03:03:26 | [diff] [blame] | 875 | DCHECK(IsInPictureInPicture() && !client_->IsInAutoPIP()); |
Mounir Lamouri | 917143af | 2018-05-24 21:19:57 | [diff] [blame] | 876 | |
| 877 | delegate_->RegisterPictureInPictureWindowResizeCallback(delegate_id_, |
| 878 | std::move(callback)); |
| 879 | } |
| 880 | |
Daniel Cheng | c1710b5 | 2018-10-24 03:12:28 | [diff] [blame^] | 881 | void WebMediaPlayerImpl::SetSinkId( |
| 882 | const blink::WebString& sink_id, |
| 883 | std::unique_ptr<blink::WebSetSinkIdCallbacks> web_callback) { |
guidou | 69223ce | 2015-06-16 10:36:19 | [diff] [blame] | 884 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 885 | DVLOG(1) << __func__; |
guidou | c7babef | 2015-10-22 00:42:35 | [diff] [blame] | 886 | |
olka | 68b6939 | 2016-04-01 11:42:12 | [diff] [blame] | 887 | media::OutputDeviceStatusCB callback = |
Daniel Cheng | c1710b5 | 2018-10-24 03:12:28 | [diff] [blame^] | 888 | media::ConvertToOutputDeviceStatusCB(std::move(web_callback)); |
guidou | c7babef | 2015-10-22 00:42:35 | [diff] [blame] | 889 | media_task_runner_->PostTask( |
Dale Curtis | ee9be3001 | 2018-09-21 22:21:51 | [diff] [blame] | 890 | FROM_HERE, base::BindOnce(&SetSinkIdOnMediaThread, audio_source_provider_, |
Daniel Cheng | c1710b5 | 2018-10-24 03:12:28 | [diff] [blame^] | 891 | sink_id.Utf8(), std::move(callback))); |
guidou | 69223ce | 2015-06-16 10:36:19 | [diff] [blame] | 892 | } |
| 893 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 894 | STATIC_ASSERT_ENUM(WebMediaPlayer::kPreloadNone, MultibufferDataSource::NONE); |
| 895 | STATIC_ASSERT_ENUM(WebMediaPlayer::kPreloadMetaData, |
dalecurtis | b6e052f5 | 2016-08-25 00:35:55 | [diff] [blame] | 896 | MultibufferDataSource::METADATA); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 897 | STATIC_ASSERT_ENUM(WebMediaPlayer::kPreloadAuto, MultibufferDataSource::AUTO); |
[email protected] | 23a8b1d8 | 2011-04-05 16:28:20 | [diff] [blame] | 898 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 899 | void WebMediaPlayerImpl::SetPreload(WebMediaPlayer::Preload preload) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 900 | DVLOG(1) << __func__ << "(" << preload << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 901 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 902 | |
dalecurtis | b6e052f5 | 2016-08-25 00:35:55 | [diff] [blame] | 903 | preload_ = static_cast<MultibufferDataSource::Preload>(preload); |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 904 | if (data_source_) |
[email protected] | 09c6022 | 2014-08-07 16:42:31 | [diff] [blame] | 905 | data_source_->SetPreload(preload_); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 906 | } |
| 907 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 908 | bool WebMediaPlayerImpl::HasVideo() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 909 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 910 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 911 | return pipeline_metadata_.has_video; |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 912 | } |
| 913 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 914 | bool WebMediaPlayerImpl::HasAudio() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 915 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | fc367af | 2009-08-14 23:06:35 | [diff] [blame] | 916 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 917 | return pipeline_metadata_.has_audio; |
[email protected] | fc367af | 2009-08-14 23:06:35 | [diff] [blame] | 918 | } |
| 919 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 920 | void WebMediaPlayerImpl::EnabledAudioTracksChanged( |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 921 | const blink::WebVector<blink::WebMediaPlayer::TrackId>& enabledTrackIds) { |
| 922 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 923 | |
| 924 | std::ostringstream logstr; |
| 925 | std::vector<MediaTrack::Id> enabledMediaTrackIds; |
| 926 | for (const auto& blinkTrackId : enabledTrackIds) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 927 | MediaTrack::Id track_id = blinkTrackId.Utf8().data(); |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 928 | logstr << track_id << " "; |
| 929 | enabledMediaTrackIds.push_back(track_id); |
| 930 | } |
dalecurtis | 9cddc0b | 2017-04-19 21:23:38 | [diff] [blame] | 931 | MEDIA_LOG(INFO, media_log_.get()) |
| 932 | << "Enabled audio tracks: [" << logstr.str() << "]"; |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 933 | pipeline_controller_.OnEnabledAudioTracksChanged(enabledMediaTrackIds); |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 934 | } |
| 935 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 936 | void WebMediaPlayerImpl::SelectedVideoTrackChanged( |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 937 | blink::WebMediaPlayer::TrackId* selectedTrackId) { |
| 938 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 939 | |
servolk | 9bed660 | 2017-02-24 01:20:11 | [diff] [blame] | 940 | base::Optional<MediaTrack::Id> selected_video_track_id; |
| 941 | if (selectedTrackId && !video_track_disabled_) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 942 | selected_video_track_id = MediaTrack::Id(selectedTrackId->Utf8().data()); |
dalecurtis | 9cddc0b | 2017-04-19 21:23:38 | [diff] [blame] | 943 | MEDIA_LOG(INFO, media_log_.get()) |
| 944 | << "Selected video track: [" << selected_video_track_id.value_or("") |
| 945 | << "]"; |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 946 | pipeline_controller_.OnSelectedVideoTrackChanged(selected_video_track_id); |
servolk | f25ceed | 2016-07-01 03:44:38 | [diff] [blame] | 947 | } |
| 948 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 949 | blink::WebSize WebMediaPlayerImpl::NaturalSize() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 950 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 951 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 952 | return blink::WebSize(pipeline_metadata_.natural_size); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 953 | } |
| 954 | |
Jiajia Qin | 82acdc0 | 2017-07-31 09:55:14 | [diff] [blame] | 955 | blink::WebSize WebMediaPlayerImpl::VisibleRect() const { |
| 956 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 957 | scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor(); |
| 958 | if (!video_frame) |
| 959 | return blink::WebSize(); |
| 960 | |
| 961 | const gfx::Rect& visible_rect = video_frame->visible_rect(); |
| 962 | return blink::WebSize(visible_rect.width(), visible_rect.height()); |
| 963 | } |
| 964 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 965 | bool WebMediaPlayerImpl::Paused() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 966 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 967 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 968 | #if defined(OS_ANDROID) // WMPI_CAST |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 969 | if (IsRemote()) |
danakj | 4f1fd6a | 2017-01-06 21:15:17 | [diff] [blame] | 970 | return cast_impl_.IsPaused(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 971 | #endif |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 972 | |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 973 | return pipeline_controller_.GetPlaybackRate() == 0.0f; |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 974 | } |
| 975 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 976 | bool WebMediaPlayerImpl::Seeking() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 977 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 978 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 979 | if (ready_state_ == WebMediaPlayer::kReadyStateHaveNothing) |
[email protected] | 0acebfa | 2009-08-21 22:45:40 | [diff] [blame] | 980 | return false; |
[email protected] | 67cd505 | 2009-09-10 21:53:22 | [diff] [blame] | 981 | |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 982 | return seeking_; |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 983 | } |
| 984 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 985 | double WebMediaPlayerImpl::Duration() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 986 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 987 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 988 | if (ready_state_ == WebMediaPlayer::kReadyStateHaveNothing) |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 989 | return std::numeric_limits<double>::quiet_NaN(); |
| 990 | |
chcunningham | b92d506 | 2017-01-10 21:50:22 | [diff] [blame] | 991 | // Use duration from ChunkDemuxer when present. MSE allows users to specify |
| 992 | // duration as a double. This propagates to the rest of the pipeline as a |
| 993 | // TimeDelta with potentially reduced precision (limited to Microseconds). |
| 994 | // ChunkDemuxer returns the full-precision user-specified double. This ensures |
| 995 | // users can "get" the exact duration they "set". |
| 996 | if (chunk_demuxer_) |
| 997 | return chunk_demuxer_->GetDuration(); |
| 998 | |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 999 | base::TimeDelta pipeline_duration = GetPipelineMediaDuration(); |
chcunningham | b92d506 | 2017-01-10 21:50:22 | [diff] [blame] | 1000 | return pipeline_duration == kInfiniteDuration |
| 1001 | ? std::numeric_limits<double>::infinity() |
| 1002 | : pipeline_duration.InSecondsF(); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 1003 | } |
| 1004 | |
[email protected] | db66d009 | 2014-04-16 07:15:12 | [diff] [blame] | 1005 | double WebMediaPlayerImpl::timelineOffset() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1006 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | db66d009 | 2014-04-16 07:15:12 | [diff] [blame] | 1007 | |
| 1008 | if (pipeline_metadata_.timeline_offset.is_null()) |
| 1009 | return std::numeric_limits<double>::quiet_NaN(); |
| 1010 | |
| 1011 | return pipeline_metadata_.timeline_offset.ToJsTime(); |
| 1012 | } |
| 1013 | |
Dale Curtis | 051fdf6 | 2017-08-05 00:21:13 | [diff] [blame] | 1014 | base::TimeDelta WebMediaPlayerImpl::GetCurrentTimeInternal() const { |
| 1015 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1016 | DCHECK_NE(ready_state_, WebMediaPlayer::kReadyStateHaveNothing); |
| 1017 | |
| 1018 | base::TimeDelta current_time; |
| 1019 | if (Seeking()) |
| 1020 | current_time = seek_time_; |
| 1021 | #if defined(OS_ANDROID) // WMPI_CAST |
| 1022 | else if (IsRemote()) |
| 1023 | current_time = cast_impl_.currentTime(); |
| 1024 | #endif |
| 1025 | else if (paused_) |
| 1026 | current_time = paused_time_; |
| 1027 | else |
| 1028 | current_time = pipeline_controller_.GetMediaTime(); |
| 1029 | |
| 1030 | DCHECK_NE(current_time, kInfiniteDuration); |
| 1031 | DCHECK_GE(current_time, base::TimeDelta()); |
| 1032 | return current_time; |
| 1033 | } |
| 1034 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1035 | double WebMediaPlayerImpl::CurrentTime() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1036 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1037 | DCHECK_NE(ready_state_, WebMediaPlayer::kReadyStateHaveNothing); |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 1038 | |
| 1039 | // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement, |
| 1040 | // see http://crbug.com/409280 |
Dale Curtis | 051fdf6 | 2017-08-05 00:21:13 | [diff] [blame] | 1041 | // Note: Duration() may be infinity. |
josephlolak | 918863bc | 2017-11-15 08:54:33 | [diff] [blame] | 1042 | return (ended_ && !std::isinf(Duration())) |
| 1043 | ? Duration() |
| 1044 | : GetCurrentTimeInternal().InSecondsF(); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 1045 | } |
| 1046 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1047 | WebMediaPlayer::NetworkState WebMediaPlayerImpl::GetNetworkState() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1048 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 1049 | return network_state_; |
| 1050 | } |
| 1051 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1052 | WebMediaPlayer::ReadyState WebMediaPlayerImpl::GetReadyState() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1053 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 1054 | return ready_state_; |
| 1055 | } |
| 1056 | |
CJ DiMeglio | 8924047 | 2018-10-18 18:21:10 | [diff] [blame] | 1057 | blink::WebMediaPlayer::SurfaceLayerMode |
| 1058 | WebMediaPlayerImpl::GetVideoSurfaceLayerMode() const { |
| 1059 | return surface_layer_mode_; |
| 1060 | } |
| 1061 | |
wolenetz | ed8e709 | 2017-04-21 16:28:59 | [diff] [blame] | 1062 | blink::WebString WebMediaPlayerImpl::GetErrorMessage() const { |
wolenetz | 4d39cc0 | 2016-04-05 19:54:41 | [diff] [blame] | 1063 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
wolenetz | ed8e709 | 2017-04-21 16:28:59 | [diff] [blame] | 1064 | return blink::WebString::FromUTF8(media_log_->GetErrorMessage()); |
wolenetz | 4d39cc0 | 2016-04-05 19:54:41 | [diff] [blame] | 1065 | } |
| 1066 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1067 | blink::WebTimeRanges WebMediaPlayerImpl::Buffered() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1068 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 779a832 | 2014-08-22 21:28:37 | [diff] [blame] | 1069 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1070 | Ranges<base::TimeDelta> buffered_time_ranges = |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 1071 | pipeline_controller_.GetBufferedTimeRanges(); |
[email protected] | 779a832 | 2014-08-22 21:28:37 | [diff] [blame] | 1072 | |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 1073 | const base::TimeDelta duration = GetPipelineMediaDuration(); |
dalecurtis | 39a7f93 | 2016-07-19 18:34:59 | [diff] [blame] | 1074 | if (duration != kInfiniteDuration) { |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 1075 | buffered_data_source_host_.AddBufferedTimeRanges(&buffered_time_ranges, |
| 1076 | duration); |
[email protected] | 779a832 | 2014-08-22 21:28:37 | [diff] [blame] | 1077 | } |
[email protected] | 02022fc | 2014-05-16 00:05:31 | [diff] [blame] | 1078 | return ConvertToWebTimeRanges(buffered_time_ranges); |
| 1079 | } |
| 1080 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1081 | blink::WebTimeRanges WebMediaPlayerImpl::Seekable() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1082 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 1083 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1084 | if (ready_state_ < WebMediaPlayer::kReadyStateHaveMetadata) |
philipj | b0e6f3f | 2014-09-30 09:51:53 | [diff] [blame] | 1085 | return blink::WebTimeRanges(); |
| 1086 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1087 | const double seekable_end = Duration(); |
dalecurtis | 56359cb | 2014-10-28 00:06:29 | [diff] [blame] | 1088 | |
| 1089 | // Allow a special exception for seeks to zero for streaming sources with a |
| 1090 | // finite duration; this allows looping to work. |
tguilbert | ade2bcb | 2017-01-07 02:57:45 | [diff] [blame] | 1091 | const bool is_finite_stream = data_source_ && data_source_->IsStreaming() && |
| 1092 | std::isfinite(seekable_end); |
| 1093 | |
tguilbert | 75e2bf6 | 2017-04-26 20:13:12 | [diff] [blame] | 1094 | // Do not change the seekable range when using the MediaPlayerRenderer. It |
| 1095 | // will take care of dropping invalid seeks. |
| 1096 | const bool force_seeks_to_zero = |
| 1097 | !using_media_player_renderer_ && is_finite_stream; |
dalecurtis | 56359cb | 2014-10-28 00:06:29 | [diff] [blame] | 1098 | |
| 1099 | // TODO(dalecurtis): Technically this allows seeking on media which return an |
tguilbert | ade2bcb | 2017-01-07 02:57:45 | [diff] [blame] | 1100 | // infinite duration so long as DataSource::IsStreaming() is false. While not |
dalecurtis | 56359cb | 2014-10-28 00:06:29 | [diff] [blame] | 1101 | // expected, disabling this breaks semi-live players, http://crbug.com/427412. |
| 1102 | const blink::WebTimeRange seekable_range( |
tguilbert | ade2bcb | 2017-01-07 02:57:45 | [diff] [blame] | 1103 | 0.0, force_seeks_to_zero ? 0.0 : seekable_end); |
philipj | b0e6f3f | 2014-09-30 09:51:53 | [diff] [blame] | 1104 | return blink::WebTimeRanges(&seekable_range, 1); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 1105 | } |
| 1106 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1107 | bool WebMediaPlayerImpl::IsPrerollAttemptNeeded() { |
| 1108 | // TODO(sandersd): Replace with |highest_ready_state_since_seek_| if we need |
| 1109 | // to ensure that preroll always gets a chance to complete. |
| 1110 | // See http://crbug.com/671525. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1111 | if (highest_ready_state_ >= ReadyState::kReadyStateHaveFutureData) |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1112 | return false; |
| 1113 | |
Fredrik Hubinette | 4cfb441 | 2017-08-23 00:03:07 | [diff] [blame] | 1114 | // To suspend before we reach kReadyStateHaveCurrentData is only ok |
| 1115 | // if we know we're going to get woken up when we get more data, which |
| 1116 | // will only happen if the network is in the "Loading" state. |
| 1117 | // This happens when the network is fast, but multiple videos are loading |
| 1118 | // and multiplexing gets held up waiting for available threads. |
| 1119 | if (highest_ready_state_ <= ReadyState::kReadyStateHaveMetadata && |
| 1120 | network_state_ != WebMediaPlayer::kNetworkStateLoading) { |
| 1121 | return true; |
| 1122 | } |
| 1123 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1124 | if (preroll_attempt_pending_) |
| 1125 | return true; |
| 1126 | |
| 1127 | // Freshly initialized; there has never been any loading progress. (Otherwise |
| 1128 | // |preroll_attempt_pending_| would be true when the start time is null.) |
| 1129 | if (preroll_attempt_start_time_.is_null()) |
| 1130 | return false; |
| 1131 | |
| 1132 | base::TimeDelta preroll_attempt_duration = |
| 1133 | tick_clock_->NowTicks() - preroll_attempt_start_time_; |
| 1134 | return preroll_attempt_duration < kPrerollAttemptTimeout; |
| 1135 | } |
| 1136 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1137 | bool WebMediaPlayerImpl::DidLoadingProgress() { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1138 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
dalecurtis | e7120dc | 2016-09-03 02:54:35 | [diff] [blame] | 1139 | |
| 1140 | // Note: Separate variables used to ensure both methods are called every time. |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 1141 | const bool pipeline_progress = pipeline_controller_.DidLoadingProgress(); |
dalecurtis | e7120dc | 2016-09-03 02:54:35 | [diff] [blame] | 1142 | const bool data_progress = buffered_data_source_host_.DidLoadingProgress(); |
hubbe | b2d3efd | 2017-05-05 23:26:38 | [diff] [blame] | 1143 | return pipeline_progress || data_progress; |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 1144 | } |
| 1145 | |
danakj | ff6a026 | 2018-06-26 19:50:31 | [diff] [blame] | 1146 | void WebMediaPlayerImpl::Paint(cc::PaintCanvas* canvas, |
[email protected] | dd5c797 | 2014-08-21 15:00:37 | [diff] [blame] | 1147 | const blink::WebRect& rect, |
Kai Ninomiya | 9e8ae29b | 2017-09-06 23:45:16 | [diff] [blame] | 1148 | cc::PaintFlags& flags, |
| 1149 | int already_uploaded_id, |
| 1150 | VideoFrameUploadMetadata* out_metadata) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1151 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1152 | TRACE_EVENT0("media", "WebMediaPlayerImpl:paint"); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 1153 | |
watk | d16bb3e | 2017-04-25 01:18:31 | [diff] [blame] | 1154 | // We can't copy from protected frames. |
Xiaohan Wang | 24cfe2c | 2018-01-22 23:16:00 | [diff] [blame] | 1155 | if (cdm_context_ref_) |
xhwang | 8073945 | 2016-01-13 00:48:00 | [diff] [blame] | 1156 | return; |
| 1157 | |
mcasas | f1236fc2 | 2015-05-29 22:38:56 | [diff] [blame] | 1158 | scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor(); |
[email protected] | 0fe5d43b | 2014-01-24 23:32:45 | [diff] [blame] | 1159 | |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 1160 | gfx::Rect gfx_rect(rect); |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 1161 | Context3D context_3d; |
Dan Sanders | 930cc1d | 2018-10-03 00:45:08 | [diff] [blame] | 1162 | gpu::ContextSupport* context_support = nullptr; |
mcasas | 265bdbf8 | 2015-06-12 18:44:07 | [diff] [blame] | 1163 | if (video_frame.get() && video_frame->HasTextures()) { |
CJ DiMeglio | c60a5cf | 2017-09-27 20:08:41 | [diff] [blame] | 1164 | if (context_provider_) { |
| 1165 | context_3d = Context3D(context_provider_->ContextGL(), |
| 1166 | context_provider_->GrContext()); |
Dan Sanders | 930cc1d | 2018-10-03 00:45:08 | [diff] [blame] | 1167 | context_support = context_provider_->ContextSupport(); |
CJ DiMeglio | c60a5cf | 2017-09-27 20:08:41 | [diff] [blame] | 1168 | } |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 1169 | if (!context_3d.gl) |
danakj | 53f7ec90 | 2016-05-21 01:30:10 | [diff] [blame] | 1170 | return; // Unable to get/create a shared main thread context. |
| 1171 | if (!context_3d.gr_context) |
| 1172 | return; // The context has been lost since and can't setup a GrContext. |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 1173 | } |
Kai Ninomiya | ef8c9e0 | 2017-09-27 04:07:40 | [diff] [blame] | 1174 | if (out_metadata && video_frame) { |
Kai Ninomiya | 9e8ae29b | 2017-09-06 23:45:16 | [diff] [blame] | 1175 | // WebGL last-uploaded-frame-metadata API enabled. https://crbug.com/639174 |
| 1176 | ComputeFrameUploadMetadata(video_frame.get(), already_uploaded_id, |
| 1177 | out_metadata); |
| 1178 | if (out_metadata->skipped) { |
| 1179 | // Skip uploading this frame. |
| 1180 | return; |
| 1181 | } |
| 1182 | } |
zhuoyu.qian | 4689dde2 | 2017-10-16 04:11:48 | [diff] [blame] | 1183 | video_renderer_.Paint( |
Julien Isorce | 6c83d8de | 2017-10-12 13:11:29 | [diff] [blame] | 1184 | video_frame, canvas, gfx::RectF(gfx_rect), flags, |
Dan Sanders | 930cc1d | 2018-10-03 00:45:08 | [diff] [blame] | 1185 | pipeline_metadata_.video_decoder_config.video_rotation(), context_3d, |
| 1186 | context_support); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 1187 | } |
[email protected] | 5df5165 | 2009-01-17 00:03:00 | [diff] [blame] | 1188 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1189 | bool WebMediaPlayerImpl::HasSingleSecurityOrigin() const { |
Thomas Guilbert | 153f8457 | 2018-07-19 05:03:58 | [diff] [blame] | 1190 | if (demuxer_found_hls_) { |
| 1191 | // HLS manifests might pull segments from a different origin. We can't know |
| 1192 | // for sure, so we conservatively say no here. |
| 1193 | return false; |
| 1194 | } |
| 1195 | |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 1196 | if (data_source_) |
| 1197 | return data_source_->HasSingleOrigin(); |
[email protected] | fcdb746 | 2009-10-21 21:05:11 | [diff] [blame] | 1198 | return true; |
[email protected] | 38259a7a8 | 2009-07-29 21:49:49 | [diff] [blame] | 1199 | } |
| 1200 | |
Yutaka Hirano | a9cbaa7 | 2018-10-10 08:35:22 | [diff] [blame] | 1201 | bool WebMediaPlayerImpl::WouldTaintOrigin() const { |
| 1202 | if (!HasSingleSecurityOrigin()) { |
| 1203 | // When the resource is redirected to another origin we think it as |
| 1204 | // tainted. This is actually not specified, and is under discussion. |
| 1205 | // See https://github.com/whatwg/fetch/issues/737. |
| 1206 | return true; |
| 1207 | } |
| 1208 | |
| 1209 | return data_source_ && data_source_->IsCorsCrossOrigin(); |
[email protected] | 3fe2711 | 2012-06-07 04:00:01 | [diff] [blame] | 1210 | } |
| 1211 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1212 | double WebMediaPlayerImpl::MediaTimeForTimeValue(double timeValue) const { |
qinmin | b4c3978 | 2015-08-10 18:43:24 | [diff] [blame] | 1213 | return base::TimeDelta::FromSecondsD(timeValue).InSecondsF(); |
[email protected] | e06e16d8 | 2011-05-26 22:13:33 | [diff] [blame] | 1214 | } |
| 1215 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1216 | unsigned WebMediaPlayerImpl::DecodedFrameCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1217 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Dale Curtis | c7d2a7d2 | 2018-01-11 20:01:05 | [diff] [blame] | 1218 | return GetPipelineStatistics().video_frames_decoded; |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 1219 | } |
| 1220 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1221 | unsigned WebMediaPlayerImpl::DroppedFrameCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1222 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Dale Curtis | c7d2a7d2 | 2018-01-11 20:01:05 | [diff] [blame] | 1223 | return GetPipelineStatistics().video_frames_dropped; |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 1224 | } |
| 1225 | |
Dave Tapuska | 6c715491 | 2018-07-30 20:39:00 | [diff] [blame] | 1226 | uint64_t WebMediaPlayerImpl::AudioDecodedByteCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1227 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Dale Curtis | c7d2a7d2 | 2018-01-11 20:01:05 | [diff] [blame] | 1228 | return GetPipelineStatistics().audio_bytes_decoded; |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 1229 | } |
| 1230 | |
Dave Tapuska | 6c715491 | 2018-07-30 20:39:00 | [diff] [blame] | 1231 | uint64_t WebMediaPlayerImpl::VideoDecodedByteCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1232 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Dale Curtis | c7d2a7d2 | 2018-01-11 20:01:05 | [diff] [blame] | 1233 | return GetPipelineStatistics().video_bytes_decoded; |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 1234 | } |
| 1235 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1236 | bool WebMediaPlayerImpl::CopyVideoTextureToPlatformTexture( |
danakj | 46070b0 | 2016-03-28 21:54:11 | [diff] [blame] | 1237 | gpu::gles2::GLES2Interface* gl, |
jiajia.qin | c294316 | 2017-05-12 01:34:39 | [diff] [blame] | 1238 | unsigned int target, |
zmo | 57d577a | 2015-10-30 18:28:59 | [diff] [blame] | 1239 | unsigned int texture, |
kbr | 0986e62 | 2017-04-13 02:34:58 | [diff] [blame] | 1240 | unsigned internal_format, |
| 1241 | unsigned format, |
| 1242 | unsigned type, |
jiajia.qin | c294316 | 2017-05-12 01:34:39 | [diff] [blame] | 1243 | int level, |
zmo | 57d577a | 2015-10-30 18:28:59 | [diff] [blame] | 1244 | bool premultiply_alpha, |
Kai Ninomiya | 9e8ae29b | 2017-09-06 23:45:16 | [diff] [blame] | 1245 | bool flip_y, |
| 1246 | int already_uploaded_id, |
| 1247 | VideoFrameUploadMetadata* out_metadata) { |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 1248 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | bfc05f2 | 2013-10-19 17:55:16 | [diff] [blame] | 1249 | TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture"); |
| 1250 | |
watk | d16bb3e | 2017-04-25 01:18:31 | [diff] [blame] | 1251 | // We can't copy from protected frames. |
Xiaohan Wang | 24cfe2c | 2018-01-22 23:16:00 | [diff] [blame] | 1252 | if (cdm_context_ref_) |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 1253 | return false; |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1254 | |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 1255 | scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor(); |
jbauman | 581d041c | 2016-07-21 01:01:03 | [diff] [blame] | 1256 | if (!video_frame.get() || !video_frame->HasTextures()) { |
[email protected] | e56f88c7 | 2013-06-25 22:31:29 | [diff] [blame] | 1257 | return false; |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 1258 | } |
Kai Ninomiya | 9e8ae29b | 2017-09-06 23:45:16 | [diff] [blame] | 1259 | if (out_metadata) { |
| 1260 | // WebGL last-uploaded-frame-metadata API is enabled. |
| 1261 | // https://crbug.com/639174 |
| 1262 | ComputeFrameUploadMetadata(video_frame.get(), already_uploaded_id, |
| 1263 | out_metadata); |
| 1264 | if (out_metadata->skipped) { |
| 1265 | // Skip uploading this frame. |
| 1266 | return true; |
| 1267 | } |
| 1268 | } |
[email protected] | df41e25 | 2014-02-03 23:39:50 | [diff] [blame] | 1269 | |
jbauman | 581d041c | 2016-07-21 01:01:03 | [diff] [blame] | 1270 | Context3D context_3d; |
Dan Sanders | 930cc1d | 2018-10-03 00:45:08 | [diff] [blame] | 1271 | gpu::ContextSupport* context_support = nullptr; |
CJ DiMeglio | c60a5cf | 2017-09-27 20:08:41 | [diff] [blame] | 1272 | if (context_provider_) { |
| 1273 | context_3d = Context3D(context_provider_->ContextGL(), |
| 1274 | context_provider_->GrContext()); |
Dan Sanders | 930cc1d | 2018-10-03 00:45:08 | [diff] [blame] | 1275 | context_support = context_provider_->ContextSupport(); |
CJ DiMeglio | c60a5cf | 2017-09-27 20:08:41 | [diff] [blame] | 1276 | } |
zhuoyu.qian | 4689dde2 | 2017-10-16 04:11:48 | [diff] [blame] | 1277 | return video_renderer_.CopyVideoFrameTexturesToGLTexture( |
Dan Sanders | 930cc1d | 2018-10-03 00:45:08 | [diff] [blame] | 1278 | context_3d, context_support, gl, video_frame.get(), target, texture, |
| 1279 | internal_format, format, type, level, premultiply_alpha, flip_y); |
[email protected] | 6523b24 | 2013-03-13 11:10:07 | [diff] [blame] | 1280 | } |
| 1281 | |
Matt Wolenetz | 95af636 | 2018-01-04 20:23:42 | [diff] [blame] | 1282 | // static |
Kai Ninomiya | 9e8ae29b | 2017-09-06 23:45:16 | [diff] [blame] | 1283 | void WebMediaPlayerImpl::ComputeFrameUploadMetadata( |
| 1284 | VideoFrame* frame, |
| 1285 | int already_uploaded_id, |
| 1286 | VideoFrameUploadMetadata* out_metadata) { |
| 1287 | DCHECK(out_metadata); |
Kai Ninomiya | ef8c9e0 | 2017-09-27 04:07:40 | [diff] [blame] | 1288 | DCHECK(frame); |
Kai Ninomiya | 9e8ae29b | 2017-09-06 23:45:16 | [diff] [blame] | 1289 | out_metadata->frame_id = frame->unique_id(); |
| 1290 | out_metadata->visible_rect = frame->visible_rect(); |
| 1291 | out_metadata->timestamp = frame->timestamp(); |
| 1292 | bool skip_possible = already_uploaded_id != -1; |
| 1293 | bool same_frame_id = frame->unique_id() == already_uploaded_id; |
| 1294 | out_metadata->skipped = skip_possible && same_frame_id; |
| 1295 | } |
| 1296 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1297 | void WebMediaPlayerImpl::SetContentDecryptionModule( |
[email protected] | 9ebc3b03f | 2014-08-13 04:01:23 | [diff] [blame] | 1298 | blink::WebContentDecryptionModule* cdm, |
| 1299 | blink::WebContentDecryptionModuleResult result) { |
xhwang | 5113973 | 2017-02-24 19:36:08 | [diff] [blame] | 1300 | DVLOG(1) << __func__ << ": cdm = " << cdm; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1301 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 9ebc3b03f | 2014-08-13 04:01:23 | [diff] [blame] | 1302 | |
jrummell | 06f2707 | 2015-06-08 18:12:38 | [diff] [blame] | 1303 | // Once the CDM is set it can't be cleared as there may be frames being |
| 1304 | // decrypted on other threads. So fail this request. |
| 1305 | // http://crbug.com/462365#c7. |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 1306 | if (!cdm) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1307 | result.CompleteWithError( |
| 1308 | blink::kWebContentDecryptionModuleExceptionInvalidStateError, 0, |
xhwang | 79b19304 | 2016-12-13 18:52:43 | [diff] [blame] | 1309 | "The existing ContentDecryptionModule object cannot be removed at this " |
| 1310 | "time."); |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 1311 | return; |
| 1312 | } |
| 1313 | |
jrummell | 89e61d8 | 2015-07-23 20:03:34 | [diff] [blame] | 1314 | // Create a local copy of |result| to avoid problems with the callback |
| 1315 | // getting passed to the media thread and causing |result| to be destructed |
jrummell | 4acbec91 | 2016-02-27 02:45:03 | [diff] [blame] | 1316 | // on the wrong thread in some failure conditions. Blink should prevent |
| 1317 | // multiple simultaneous calls. |
| 1318 | DCHECK(!set_cdm_result_); |
jrummell | 89e61d8 | 2015-07-23 20:03:34 | [diff] [blame] | 1319 | set_cdm_result_.reset(new blink::WebContentDecryptionModuleResult(result)); |
| 1320 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 1321 | // Recreate the watch time reporter if necessary. |
| 1322 | const bool was_encrypted = is_encrypted_; |
| 1323 | is_encrypted_ = true; |
Dale Curtis | 3899090ea | 2018-01-12 00:10:35 | [diff] [blame] | 1324 | if (!was_encrypted) { |
| 1325 | media_metrics_provider_->SetIsEME(); |
| 1326 | if (watch_time_reporter_) |
| 1327 | CreateWatchTimeReporter(); |
| 1328 | } |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 1329 | |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 1330 | // For now MediaCapabilities only handles clear content. |
| 1331 | video_decode_stats_reporter_.reset(); |
| 1332 | |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1333 | SetCdm(cdm); |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 1334 | } |
| 1335 | |
xhwang | e8c4181a | 2014-12-06 08:10:01 | [diff] [blame] | 1336 | void WebMediaPlayerImpl::OnEncryptedMediaInitData( |
jrummell | cf78967c | 2015-04-03 05:03:58 | [diff] [blame] | 1337 | EmeInitDataType init_data_type, |
Avi Drissman | 97785ea | 2015-12-19 01:11:31 | [diff] [blame] | 1338 | const std::vector<uint8_t>& init_data) { |
jrummell | cf78967c | 2015-04-03 05:03:58 | [diff] [blame] | 1339 | DCHECK(init_data_type != EmeInitDataType::UNKNOWN); |
xhwang | bab66f5 | 2014-12-02 23:49:50 | [diff] [blame] | 1340 | |
Xiaohan Wang | f63505d | 2017-10-21 08:00:53 | [diff] [blame] | 1341 | RecordEncryptedEvent(true); |
xhwang | bab66f5 | 2014-12-02 23:49:50 | [diff] [blame] | 1342 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 1343 | // Recreate the watch time reporter if necessary. |
| 1344 | const bool was_encrypted = is_encrypted_; |
| 1345 | is_encrypted_ = true; |
Dale Curtis | 3899090ea | 2018-01-12 00:10:35 | [diff] [blame] | 1346 | if (!was_encrypted) { |
| 1347 | media_metrics_provider_->SetIsEME(); |
| 1348 | if (watch_time_reporter_) |
| 1349 | CreateWatchTimeReporter(); |
| 1350 | } |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 1351 | |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 1352 | // For now MediaCapabilities only handles clear content. |
| 1353 | video_decode_stats_reporter_.reset(); |
| 1354 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1355 | encrypted_client_->Encrypted( |
davidben | b50f00c | 2015-12-01 00:01:50 | [diff] [blame] | 1356 | ConvertToWebInitDataType(init_data_type), init_data.data(), |
srirama.m | 26f864d0 | 2015-07-14 05:21:46 | [diff] [blame] | 1357 | base::saturated_cast<unsigned int>(init_data.size())); |
xhwang | bab66f5 | 2014-12-02 23:49:50 | [diff] [blame] | 1358 | } |
| 1359 | |
servolk | 81e01e0 | 2016-03-05 03:29:15 | [diff] [blame] | 1360 | void WebMediaPlayerImpl::OnFFmpegMediaTracksUpdated( |
dcheng | 3076abbf | 2016-04-22 20:42:39 | [diff] [blame] | 1361 | std::unique_ptr<MediaTracks> tracks) { |
servolk | 81e01e0 | 2016-03-05 03:29:15 | [diff] [blame] | 1362 | // For MSE/chunk_demuxer case the media track updates are handled by |
| 1363 | // WebSourceBufferImpl. |
| 1364 | DCHECK(demuxer_.get()); |
| 1365 | DCHECK(!chunk_demuxer_); |
servolk | ef1e5ef | 2016-03-25 04:55:26 | [diff] [blame] | 1366 | |
servolk | 16e8bdf8 | 2017-04-11 17:00:39 | [diff] [blame] | 1367 | // Report the media track information to blink. Only the first audio track and |
| 1368 | // the first video track are enabled by default to match blink logic. |
| 1369 | bool is_first_audio_track = true; |
| 1370 | bool is_first_video_track = true; |
servolk | ef1e5ef | 2016-03-25 04:55:26 | [diff] [blame] | 1371 | for (const auto& track : tracks->tracks()) { |
| 1372 | if (track->type() == MediaTrack::Audio) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1373 | client_->AddAudioTrack(blink::WebString::FromUTF8(track->id()), |
| 1374 | blink::WebMediaPlayerClient::kAudioTrackKindMain, |
| 1375 | blink::WebString::FromUTF8(track->label()), |
| 1376 | blink::WebString::FromUTF8(track->language()), |
servolk | 16e8bdf8 | 2017-04-11 17:00:39 | [diff] [blame] | 1377 | is_first_audio_track); |
| 1378 | is_first_audio_track = false; |
servolk | ef1e5ef | 2016-03-25 04:55:26 | [diff] [blame] | 1379 | } else if (track->type() == MediaTrack::Video) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1380 | client_->AddVideoTrack(blink::WebString::FromUTF8(track->id()), |
| 1381 | blink::WebMediaPlayerClient::kVideoTrackKindMain, |
| 1382 | blink::WebString::FromUTF8(track->label()), |
| 1383 | blink::WebString::FromUTF8(track->language()), |
servolk | 16e8bdf8 | 2017-04-11 17:00:39 | [diff] [blame] | 1384 | is_first_video_track); |
| 1385 | is_first_video_track = false; |
servolk | ef1e5ef | 2016-03-25 04:55:26 | [diff] [blame] | 1386 | } else { |
| 1387 | // Text tracks are not supported through this code path yet. |
| 1388 | NOTREACHED(); |
| 1389 | } |
| 1390 | } |
servolk | 81e01e0 | 2016-03-05 03:29:15 | [diff] [blame] | 1391 | } |
| 1392 | |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1393 | void WebMediaPlayerImpl::SetCdm(blink::WebContentDecryptionModule* cdm) { |
| 1394 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1395 | DCHECK(cdm); |
Xiaohan Wang | 24cfe2c | 2018-01-22 23:16:00 | [diff] [blame] | 1396 | |
| 1397 | auto cdm_context_ref = |
| 1398 | ToWebContentDecryptionModuleImpl(cdm)->GetCdmContextRef(); |
| 1399 | if (!cdm_context_ref) { |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1400 | NOTREACHED(); |
| 1401 | OnCdmAttached(false); |
xhwang | 8073945 | 2016-01-13 00:48:00 | [diff] [blame] | 1402 | return; |
| 1403 | } |
| 1404 | |
Xiaohan Wang | 24cfe2c | 2018-01-22 23:16:00 | [diff] [blame] | 1405 | CdmContext* cdm_context = cdm_context_ref->GetCdmContext(); |
| 1406 | DCHECK(cdm_context); |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1407 | |
| 1408 | // Keep the reference to the CDM, as it shouldn't be destroyed until |
| 1409 | // after the pipeline is done with the |cdm_context|. |
Xiaohan Wang | 24cfe2c | 2018-01-22 23:16:00 | [diff] [blame] | 1410 | pending_cdm_context_ref_ = std::move(cdm_context_ref); |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 1411 | pipeline_controller_.SetCdm( |
| 1412 | cdm_context, base::Bind(&WebMediaPlayerImpl::OnCdmAttached, AsWeakPtr())); |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 1413 | } |
| 1414 | |
jrummell | 89e61d8 | 2015-07-23 20:03:34 | [diff] [blame] | 1415 | void WebMediaPlayerImpl::OnCdmAttached(bool success) { |
xhwang | 5113973 | 2017-02-24 19:36:08 | [diff] [blame] | 1416 | DVLOG(1) << __func__ << ": success = " << success; |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1417 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Xiaohan Wang | 24cfe2c | 2018-01-22 23:16:00 | [diff] [blame] | 1418 | DCHECK(pending_cdm_context_ref_); |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1419 | |
| 1420 | // If the CDM is set from the constructor there is no promise |
| 1421 | // (|set_cdm_result_|) to fulfill. |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 1422 | if (success) { |
xhwang | 29c5ad20 | 2017-04-14 07:02:19 | [diff] [blame] | 1423 | media_log_->SetBooleanProperty("has_cdm", true); |
| 1424 | |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1425 | // This will release the previously attached CDM (if any). |
Xiaohan Wang | 24cfe2c | 2018-01-22 23:16:00 | [diff] [blame] | 1426 | cdm_context_ref_ = std::move(pending_cdm_context_ref_); |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1427 | if (set_cdm_result_) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1428 | set_cdm_result_->Complete(); |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1429 | set_cdm_result_.reset(); |
| 1430 | } |
| 1431 | |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 1432 | return; |
| 1433 | } |
| 1434 | |
Xiaohan Wang | 24cfe2c | 2018-01-22 23:16:00 | [diff] [blame] | 1435 | pending_cdm_context_ref_.reset(); |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1436 | if (set_cdm_result_) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1437 | set_cdm_result_->CompleteWithError( |
| 1438 | blink::kWebContentDecryptionModuleExceptionNotSupportedError, 0, |
xhwang | 79b19304 | 2016-12-13 18:52:43 | [diff] [blame] | 1439 | "Unable to set ContentDecryptionModule object"); |
jrummell | e616ee9 | 2016-10-08 02:15:44 | [diff] [blame] | 1440 | set_cdm_result_.reset(); |
| 1441 | } |
[email protected] | 9ebc3b03f | 2014-08-13 04:01:23 | [diff] [blame] | 1442 | } |
| 1443 | |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1444 | void WebMediaPlayerImpl::OnPipelineSeeked(bool time_updated) { |
John Chen | 5b164a0 | 2017-11-01 00:36:09 | [diff] [blame] | 1445 | TRACE_EVENT2("media", "WebMediaPlayerImpl::OnPipelineSeeked", "target", |
| 1446 | seek_time_.InSecondsF(), "id", media_log_->id()); |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 1447 | seeking_ = false; |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 1448 | seek_time_ = base::TimeDelta(); |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 1449 | |
hubbe | 5a2dec02 | 2016-03-17 01:14:23 | [diff] [blame] | 1450 | if (paused_) { |
| 1451 | #if defined(OS_ANDROID) // WMPI_CAST |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1452 | if (IsRemote()) { |
Dale Curtis | 051fdf6 | 2017-08-05 00:21:13 | [diff] [blame] | 1453 | paused_time_ = cast_impl_.currentTime(); |
hubbe | 5a2dec02 | 2016-03-17 01:14:23 | [diff] [blame] | 1454 | } else { |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 1455 | paused_time_ = pipeline_controller_.GetMediaTime(); |
hubbe | 5a2dec02 | 2016-03-17 01:14:23 | [diff] [blame] | 1456 | } |
| 1457 | #else |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 1458 | paused_time_ = pipeline_controller_.GetMediaTime(); |
hubbe | 5a2dec02 | 2016-03-17 01:14:23 | [diff] [blame] | 1459 | #endif |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 1460 | } else { |
| 1461 | DCHECK(watch_time_reporter_); |
| 1462 | watch_time_reporter_->OnPlaying(); |
hubbe | 5a2dec02 | 2016-03-17 01:14:23 | [diff] [blame] | 1463 | } |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1464 | if (time_updated) |
| 1465 | should_notify_time_changed_ = true; |
dalecurtis | af34d871 | 2016-09-20 04:32:26 | [diff] [blame] | 1466 | |
dalecurtis | 4f6d14d | 2017-02-22 17:42:22 | [diff] [blame] | 1467 | // Reset underflow duration upon seek; this prevents looping videos and user |
| 1468 | // actions from artificially inflating the duration. |
| 1469 | underflow_timer_.reset(); |
avayvod | 56e1f394 | 2017-01-21 02:06:31 | [diff] [blame] | 1470 | |
| 1471 | // Background video optimizations are delayed when shown/hidden if pipeline |
| 1472 | // is seeking. |
| 1473 | UpdateBackgroundVideoOptimizationState(); |
Dale Curtis | 2dc6089a | 2018-03-26 16:47:58 | [diff] [blame] | 1474 | |
| 1475 | // If we successfully completed a suspended startup, lie about our buffering |
| 1476 | // state for the time being. While ultimately we want to avoid lying about the |
| 1477 | // buffering state, for the initial test of true preload=metadata, signal |
| 1478 | // BUFFERING_HAVE_ENOUGH so that canplay and canplaythrough fire correctly. |
| 1479 | // |
| 1480 | // Later we can experiment with the impact of removing this lie; initial data |
| 1481 | // suggests high disruption since we've also made preload=metadata the |
| 1482 | // default. Most sites are not prepared for a lack of canplay; even many of |
| 1483 | // our own tests don't function correctly. See https://crbug.com/694855. |
| 1484 | // |
| 1485 | // Note: This call is dual purpose, it is also responsible for triggering an |
| 1486 | // UpdatePlayState() call which may need to resume the pipeline once Blink |
| 1487 | // has been told about the ReadyState change. |
| 1488 | if (attempting_suspended_start_ && |
| 1489 | pipeline_controller_.IsPipelineSuspended()) { |
Dale Curtis | 7c63f2e2 | 2018-09-19 21:06:27 | [diff] [blame] | 1490 | did_lazy_load_ = !has_poster_ && HasVideo(); |
| 1491 | if (did_lazy_load_) |
| 1492 | DCHECK(base::FeatureList::IsEnabled(kPreloadMetadataLazyLoad)); |
| 1493 | |
Dale Curtis | ff57655 | 2018-03-30 02:32:44 | [diff] [blame] | 1494 | skip_metrics_due_to_startup_suspend_ = true; |
Dale Curtis | 2dc6089a | 2018-03-26 16:47:58 | [diff] [blame] | 1495 | OnBufferingStateChangeInternal(BUFFERING_HAVE_ENOUGH, true); |
Dale Curtis | ff57655 | 2018-03-30 02:32:44 | [diff] [blame] | 1496 | |
| 1497 | // If |skip_metrics_due_to_startup_suspend_| is unset by a resume started by |
| 1498 | // the OnBufferingStateChangeInternal() call, record a histogram of it here. |
| 1499 | // |
| 1500 | // If the value is unset, that means we should not have suspended and we've |
| 1501 | // likely incurred some cost to TimeToFirstFrame and TimeToPlayReady which |
| 1502 | // will be reflected in those statistics. |
| 1503 | base::UmaHistogramBoolean( |
| 1504 | std::string("Media.PreloadMetadataSuspendWasIdeal.") + |
| 1505 | ((HasVideo() && HasAudio()) ? "AudioVideo" |
| 1506 | : (HasVideo() ? "Video" : "Audio")), |
| 1507 | skip_metrics_due_to_startup_suspend_); |
Dale Curtis | 2dc6089a | 2018-03-26 16:47:58 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | attempting_suspended_start_ = false; |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 1511 | } |
| 1512 | |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1513 | void WebMediaPlayerImpl::OnPipelineSuspended() { |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1514 | #if defined(OS_ANDROID) |
avayvod | 8272927 | 2017-05-29 21:58:39 | [diff] [blame] | 1515 | if (IsRemote() && !IsNewRemotePlaybackPipelineEnabled()) { |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1516 | scoped_refptr<VideoFrame> frame = cast_impl_.GetCastingBanner(); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1517 | if (frame) |
dalecurtis | e9c89e9 | 2016-05-20 19:38:00 | [diff] [blame] | 1518 | compositor_->PaintSingleFrame(frame); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 1519 | } |
| 1520 | #endif |
| 1521 | |
sandersd | 2f5bb615 | 2017-03-29 22:57:53 | [diff] [blame] | 1522 | // Tell the data source we have enough data so that it may release the |
| 1523 | // connection. |
| 1524 | if (data_source_) |
| 1525 | data_source_->OnBufferingHaveEnough(true); |
dalecurtis | 37fe586 | 2016-03-15 19:29:09 | [diff] [blame] | 1526 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1527 | ReportMemoryUsage(); |
| 1528 | |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1529 | if (pending_suspend_resume_cycle_) { |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 1530 | pending_suspend_resume_cycle_ = false; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1531 | UpdatePlayState(); |
dalecurtis | 0431cbf | 2016-03-12 01:19:43 | [diff] [blame] | 1532 | } |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1533 | } |
| 1534 | |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 1535 | void WebMediaPlayerImpl::OnBeforePipelineResume() { |
Dale Curtis | ff57655 | 2018-03-30 02:32:44 | [diff] [blame] | 1536 | // We went through suspended startup, so the player is only just now spooling |
| 1537 | // up for playback. As such adjust |load_start_time_| so it reports the same |
| 1538 | // metric as what would be reported if we had not suspended at startup. |
| 1539 | if (skip_metrics_due_to_startup_suspend_) { |
| 1540 | // In the event that the call to SetReadyState() initiated after pipeline |
| 1541 | // startup immediately tries to start playback, we should not update |
| 1542 | // |load_start_time_| to avoid losing visibility into the impact of a |
| 1543 | // suspended startup on the time until first frame / play ready for cases |
| 1544 | // where suspended startup was applied incorrectly. |
| 1545 | if (!attempting_suspended_start_) |
| 1546 | load_start_time_ = base::TimeTicks::Now() - time_to_metadata_; |
| 1547 | skip_metrics_due_to_startup_suspend_ = false; |
| 1548 | } |
| 1549 | |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 1550 | // Enable video track if we disabled it in the background - this way the new |
| 1551 | // renderer will attach its callbacks to the video stream properly. |
| 1552 | // TODO(avayvod): Remove this when disabling and enabling video tracks in |
| 1553 | // non-playing state works correctly. See https://crbug.com/678374. |
| 1554 | EnableVideoTrackIfNeeded(); |
| 1555 | is_pipeline_resuming_ = true; |
| 1556 | } |
| 1557 | |
| 1558 | void WebMediaPlayerImpl::OnPipelineResumed() { |
| 1559 | is_pipeline_resuming_ = false; |
| 1560 | |
avayvod | 56e1f394 | 2017-01-21 02:06:31 | [diff] [blame] | 1561 | UpdateBackgroundVideoOptimizationState(); |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 1562 | } |
| 1563 | |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1564 | void WebMediaPlayerImpl::OnDemuxerOpened() { |
| 1565 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
dalecurtis | 9cddc0b | 2017-04-19 21:23:38 | [diff] [blame] | 1566 | client_->MediaSourceOpened(new WebMediaSourceImpl(chunk_demuxer_)); |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1567 | } |
| 1568 | |
servolk | f94b460 | 2017-01-31 16:44:27 | [diff] [blame] | 1569 | void WebMediaPlayerImpl::OnMemoryPressure( |
| 1570 | base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
| 1571 | DVLOG(2) << __func__ << " memory_pressure_level=" << memory_pressure_level; |
| 1572 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1573 | DCHECK(base::FeatureList::IsEnabled(kMemoryPressureBasedSourceBufferGC)); |
| 1574 | DCHECK(chunk_demuxer_); |
| 1575 | |
| 1576 | // The new value of |memory_pressure_level| will take effect on the next |
| 1577 | // garbage collection. Typically this means the next SourceBuffer append() |
| 1578 | // operation, since per MSE spec, the garbage collection must only occur |
| 1579 | // during SourceBuffer append(). But if memory pressure is critical it might |
| 1580 | // be better to perform GC immediately rather than wait for the next append |
| 1581 | // and potentially get killed due to out-of-memory. |
| 1582 | // So if this experiment is enabled and pressure level is critical, we'll pass |
| 1583 | // down force_instant_gc==true, which will force immediate GC on |
| 1584 | // SourceBufferStreams. |
| 1585 | bool force_instant_gc = |
| 1586 | (enable_instant_source_buffer_gc_ && |
| 1587 | memory_pressure_level == |
| 1588 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 1589 | |
Matt Wolenetz | 95af636 | 2018-01-04 20:23:42 | [diff] [blame] | 1590 | // base::Unretained is safe, since |chunk_demuxer_| is actually owned by |
| 1591 | // |this| via this->demuxer_. Note the destruction of |chunk_demuxer_| is done |
| 1592 | // from ~WMPI by first hopping to |media_task_runner_| to prevent race with |
| 1593 | // this task. |
servolk | f94b460 | 2017-01-31 16:44:27 | [diff] [blame] | 1594 | media_task_runner_->PostTask( |
Dale Curtis | ee9be3001 | 2018-09-21 22:21:51 | [diff] [blame] | 1595 | FROM_HERE, base::BindOnce(&ChunkDemuxer::OnMemoryPressure, |
| 1596 | base::Unretained(chunk_demuxer_), |
| 1597 | base::TimeDelta::FromSecondsD(CurrentTime()), |
| 1598 | memory_pressure_level, force_instant_gc)); |
servolk | f94b460 | 2017-01-31 16:44:27 | [diff] [blame] | 1599 | } |
| 1600 | |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1601 | void WebMediaPlayerImpl::OnError(PipelineStatus status) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 1602 | DVLOG(1) << __func__; |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1603 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1604 | DCHECK_NE(status, PIPELINE_OK); |
| 1605 | |
| 1606 | if (suppress_destruction_errors_) |
| 1607 | return; |
| 1608 | |
Thomas Guilbert | 6b6be3d | 2017-08-18 03:17:27 | [diff] [blame] | 1609 | #if defined(OS_ANDROID) |
| 1610 | if (status == PipelineStatus::DEMUXER_ERROR_DETECTED_HLS) { |
Thomas Guilbert | 153f8457 | 2018-07-19 05:03:58 | [diff] [blame] | 1611 | demuxer_found_hls_ = true; |
| 1612 | |
Thomas Guilbert | 6b6be3d | 2017-08-18 03:17:27 | [diff] [blame] | 1613 | renderer_factory_selector_->SetUseMediaPlayer(true); |
| 1614 | |
| 1615 | pipeline_controller_.Stop(); |
Dale Curtis | 8a628132 | 2017-08-31 00:35:53 | [diff] [blame] | 1616 | SetMemoryReportingState(false); |
Thomas Guilbert | 6b6be3d | 2017-08-18 03:17:27 | [diff] [blame] | 1617 | |
| 1618 | main_task_runner_->PostTask( |
Dale Curtis | ee9be3001 | 2018-09-21 22:21:51 | [diff] [blame] | 1619 | FROM_HERE, |
| 1620 | base::BindOnce(&WebMediaPlayerImpl::StartPipeline, AsWeakPtr())); |
Thomas Guilbert | 6b6be3d | 2017-08-18 03:17:27 | [diff] [blame] | 1621 | return; |
| 1622 | } |
| 1623 | #endif |
| 1624 | |
Dale Curtis | 5bba0323 | 2018-08-30 17:57:38 | [diff] [blame] | 1625 | MaybeSetContainerName(); |
dalecurtis | 9cddc0b | 2017-04-19 21:23:38 | [diff] [blame] | 1626 | ReportPipelineError(load_type_, status, media_log_.get()); |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1627 | media_log_->AddEvent(media_log_->CreatePipelineErrorEvent(status)); |
Dale Curtis | 74612b7 | 2017-12-14 20:56:19 | [diff] [blame] | 1628 | media_metrics_provider_->OnError(status); |
Dale Curtis | ccfd0cca | 2017-08-31 01:27:56 | [diff] [blame] | 1629 | if (watch_time_reporter_) |
| 1630 | watch_time_reporter_->OnError(status); |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1631 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1632 | if (ready_state_ == WebMediaPlayer::kReadyStateHaveNothing) { |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1633 | // Any error that occurs before reaching ReadyStateHaveMetadata should |
| 1634 | // be considered a format error. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1635 | SetNetworkState(WebMediaPlayer::kNetworkStateFormatError); |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1636 | } else { |
| 1637 | SetNetworkState(PipelineErrorToNetworkState(status)); |
| 1638 | } |
| 1639 | |
Thomas Guilbert | 2e59139 | 2017-08-12 00:56:38 | [diff] [blame] | 1640 | // PipelineController::Stop() is idempotent. |
| 1641 | pipeline_controller_.Stop(); |
| 1642 | |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1643 | UpdatePlayState(); |
| 1644 | } |
| 1645 | |
| 1646 | void WebMediaPlayerImpl::OnEnded() { |
John Chen | 5b164a0 | 2017-11-01 00:36:09 | [diff] [blame] | 1647 | TRACE_EVENT2("media", "WebMediaPlayerImpl::OnEnded", "duration", Duration(), |
| 1648 | "id", media_log_->id()); |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 1649 | DVLOG(1) << __func__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1650 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 1651 | |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 1652 | // Ignore state changes until we've completed all outstanding operations. |
| 1653 | if (!pipeline_controller_.IsStable()) |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 1654 | return; |
| 1655 | |
| 1656 | ended_ = true; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1657 | client_->TimeChanged(); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1658 | |
dalecurtis | 1af3c1a | 2017-04-11 00:53:49 | [diff] [blame] | 1659 | // Clear any new frame processed callbacks on end; otherwise we'll end up |
| 1660 | // logging a time long after playback ends. |
| 1661 | frame_time_report_cb_.Cancel(); |
| 1662 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1663 | // We don't actually want this to run until |client_| calls seek() or pause(), |
| 1664 | // but that should have already happened in timeChanged() and so this is |
| 1665 | // expected to be a no-op. |
| 1666 | UpdatePlayState(); |
[email protected] | 57653784 | 2009-08-12 23:52:05 | [diff] [blame] | 1667 | } |
| 1668 | |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1669 | void WebMediaPlayerImpl::OnMetadata(PipelineMetadata metadata) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 1670 | DVLOG(1) << __func__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1671 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Dale Curtis | ff57655 | 2018-03-30 02:32:44 | [diff] [blame] | 1672 | |
| 1673 | // Cache the |time_to_metadata_| to use for adjusting the TimeToFirstFrame and |
| 1674 | // TimeToPlayReady metrics later if we end up doing a suspended startup. |
| 1675 | time_to_metadata_ = base::TimeTicks::Now() - load_start_time_; |
| 1676 | media_metrics_provider_->SetTimeToMetadata(time_to_metadata_); |
| 1677 | RecordTimingUMA("Media.TimeToMetadata", time_to_metadata_); |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 1678 | |
Dale Curtis | 5bba0323 | 2018-08-30 17:57:38 | [diff] [blame] | 1679 | MaybeSetContainerName(); |
| 1680 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 1681 | pipeline_metadata_ = metadata; |
[email protected] | 739847c0 | 2014-01-16 00:12:25 | [diff] [blame] | 1682 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1683 | SetReadyState(WebMediaPlayer::kReadyStateHaveMetadata); |
Julien Isorce | 6c83d8de | 2017-10-12 13:11:29 | [diff] [blame] | 1684 | UMA_HISTOGRAM_ENUMERATION("Media.VideoRotation", |
| 1685 | metadata.video_decoder_config.video_rotation(), |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1686 | VIDEO_ROTATION_MAX + 1); |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 1687 | |
John Rummell | db5a7ef | 2018-05-16 00:28:01 | [diff] [blame] | 1688 | if (HasAudio()) { |
| 1689 | RecordEncryptionScheme("Audio", |
| 1690 | metadata.audio_decoder_config.encryption_scheme()); |
| 1691 | } |
| 1692 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1693 | if (HasVideo()) { |
John Rummell | db5a7ef | 2018-05-16 00:28:01 | [diff] [blame] | 1694 | RecordEncryptionScheme("Video", |
| 1695 | metadata.video_decoder_config.encryption_scheme()); |
| 1696 | |
liberato | 2fd111be | 2017-01-04 00:25:06 | [diff] [blame] | 1697 | if (overlay_enabled_) { |
| 1698 | // SurfaceView doesn't support rotated video, so transition back if |
[email protected] | c8d57472 | 2017-08-30 20:53:43 | [diff] [blame] | 1699 | // the video is now rotated. If |always_enable_overlays_|, we keep the |
liberato | 2fd111be | 2017-01-04 00:25:06 | [diff] [blame] | 1700 | // overlay anyway so that the state machine keeps working. |
[email protected] | c8d57472 | 2017-08-30 20:53:43 | [diff] [blame] | 1701 | // TODO(liberato): verify if compositor feedback catches this. If so, |
| 1702 | // then we don't need this check. |
| 1703 | if (!always_enable_overlays_ && !DoesOverlaySupportMetadata()) |
liberato | 2fd111be | 2017-01-04 00:25:06 | [diff] [blame] | 1704 | DisableOverlay(); |
liberato | 2fd111be | 2017-01-04 00:25:06 | [diff] [blame] | 1705 | } |
watk | f835a79 | 2016-06-24 23:24:40 | [diff] [blame] | 1706 | |
[email protected] | 69db58f | 2018-09-26 20:27:56 | [diff] [blame] | 1707 | if (surface_layer_mode_ != |
CJ DiMeglio | 8924047 | 2018-10-18 18:21:10 | [diff] [blame] | 1708 | blink::WebMediaPlayer::SurfaceLayerMode::kAlways) { |
danakj | 6e669e78 | 2018-05-16 16:57:17 | [diff] [blame] | 1709 | DCHECK(!video_layer_); |
danakj | 25f03011 | 2018-05-11 18:26:54 | [diff] [blame] | 1710 | video_layer_ = cc::VideoLayer::Create( |
Julien Isorce | 6c83d8de | 2017-10-12 13:11:29 | [diff] [blame] | 1711 | compositor_.get(), |
danakj | 25f03011 | 2018-05-11 18:26:54 | [diff] [blame] | 1712 | pipeline_metadata_.video_decoder_config.video_rotation()); |
danakj | 8bc61c7 | 2018-05-16 13:55:06 | [diff] [blame] | 1713 | video_layer_->SetContentsOpaque(opaque_); |
danakj | 8d204a4 | 2018-05-18 18:05:35 | [diff] [blame] | 1714 | client_->SetCcLayer(video_layer_.get()); |
CJ DiMeglio | 7b8b5b6e | 2018-02-07 01:58:42 | [diff] [blame] | 1715 | } else { |
[email protected] | 69db58f | 2018-09-26 20:27:56 | [diff] [blame] | 1716 | ActivateSurfaceLayerForVideo(); |
lethalantidote | 7f6009d | 2017-07-07 21:47:39 | [diff] [blame] | 1717 | } |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 1718 | } |
dalecurtis | 8e4dc68 | 2016-03-15 02:30:30 | [diff] [blame] | 1719 | |
xjz | d3fe45a | 2016-10-12 18:26:37 | [diff] [blame] | 1720 | if (observer_) |
xjz | 15b483f | 2017-01-12 00:21:36 | [diff] [blame] | 1721 | observer_->OnMetadataChanged(pipeline_metadata_); |
xjz | d3fe45a | 2016-10-12 18:26:37 | [diff] [blame] | 1722 | |
Dale Curtis | c7d2a7d2 | 2018-01-11 20:01:05 | [diff] [blame] | 1723 | // TODO(dalecurtis): Don't create these until kReadyStateHaveFutureData; when |
| 1724 | // we create them early we just increase the chances of needing to throw them |
| 1725 | // away unnecessarily. |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 1726 | CreateWatchTimeReporter(); |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 1727 | CreateVideoDecodeStatsReporter(); |
Dale Curtis | c7d2a7d2 | 2018-01-11 20:01:05 | [diff] [blame] | 1728 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1729 | UpdatePlayState(); |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 1730 | } |
| 1731 | |
[email protected] | 69db58f | 2018-09-26 20:27:56 | [diff] [blame] | 1732 | void WebMediaPlayerImpl::ActivateSurfaceLayerForVideo() { |
| 1733 | // Note that we might or might not already be in VideoLayer mode. |
| 1734 | DCHECK(!bridge_); |
| 1735 | |
| 1736 | surface_layer_for_video_enabled_ = true; |
| 1737 | |
| 1738 | // If we're in VideoLayer mode, then get rid of the layer. |
| 1739 | if (video_layer_) { |
| 1740 | client_->SetCcLayer(nullptr); |
| 1741 | video_layer_ = nullptr; |
| 1742 | } |
| 1743 | |
| 1744 | bridge_ = std::move(create_bridge_callback_) |
| 1745 | .Run(this, compositor_->GetUpdateSubmissionStateCallback()); |
| 1746 | bridge_->CreateSurfaceLayer(); |
| 1747 | |
| 1748 | vfc_task_runner_->PostTask( |
| 1749 | FROM_HERE, |
| 1750 | base::BindOnce( |
| 1751 | &VideoFrameCompositor::EnableSubmission, |
| 1752 | base::Unretained(compositor_.get()), bridge_->GetSurfaceId(), |
jonross | 180dc48 | 2018-10-23 21:22:09 | [diff] [blame] | 1753 | bridge_->GetLocalSurfaceIdAllocationTime(), |
[email protected] | 69db58f | 2018-09-26 20:27:56 | [diff] [blame] | 1754 | pipeline_metadata_.video_decoder_config.video_rotation(), |
| 1755 | IsInPictureInPicture(), opaque_, |
| 1756 | BindToCurrentLoop(base::BindRepeating( |
| 1757 | &WebMediaPlayerImpl::OnFrameSinkDestroyed, AsWeakPtr())))); |
| 1758 | bridge_->SetContentsOpaque(opaque_); |
| 1759 | |
| 1760 | // If the element is already in Picture-in-Picture mode, it means that it |
| 1761 | // was set in this mode prior to this load, with a different |
| 1762 | // WebMediaPlayerImpl. The new player needs to send its id, size and |
| 1763 | // surface id to the browser process to make sure the states are properly |
| 1764 | // updated. |
| 1765 | // TODO(872056): the surface should be activated but for some reasons, it |
| 1766 | // does not. It is possible that this will no longer be needed after 872056 |
| 1767 | // is fixed. |
| 1768 | if (client_->DisplayType() == |
| 1769 | WebMediaPlayer::DisplayType::kPictureInPicture) { |
| 1770 | OnSurfaceIdUpdated(bridge_->GetSurfaceId()); |
| 1771 | } |
| 1772 | } |
| 1773 | |
CJ DiMeglio | df92bfe | 2018-05-11 20:11:00 | [diff] [blame] | 1774 | void WebMediaPlayerImpl::OnFrameSinkDestroyed() { |
| 1775 | bridge_->ClearSurfaceId(); |
| 1776 | } |
| 1777 | |
Dale Curtis | 2dc6089a | 2018-03-26 16:47:58 | [diff] [blame] | 1778 | void WebMediaPlayerImpl::OnBufferingStateChange(BufferingState state) { |
| 1779 | OnBufferingStateChangeInternal(state, false); |
| 1780 | } |
| 1781 | |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 1782 | void WebMediaPlayerImpl::CreateVideoDecodeStatsReporter() { |
| 1783 | // TODO(chcunningham): destroy reporter if we initially have video but the |
| 1784 | // track gets disabled. Currently not possible in default desktop Chrome. |
| 1785 | if (!HasVideo()) |
| 1786 | return; |
| 1787 | |
| 1788 | // Stats reporter requires a valid config. We may not have one for HLS cases |
| 1789 | // where URL demuxer doesn't know details of the stream. |
| 1790 | if (!pipeline_metadata_.video_decoder_config.IsValidConfig()) |
| 1791 | return; |
| 1792 | |
| 1793 | // For now MediaCapabilities only handles clear content. |
| 1794 | // TODO(chcunningham): Report encrypted stats. |
| 1795 | if (is_encrypted_) |
| 1796 | return; |
| 1797 | |
Dale Curtis | 7e8a510d | 2017-12-14 19:19:48 | [diff] [blame] | 1798 | mojom::VideoDecodeStatsRecorderPtr recorder; |
| 1799 | media_metrics_provider_->AcquireVideoDecodeStatsRecorder( |
Dale Curtis | 7e8a510d | 2017-12-14 19:19:48 | [diff] [blame] | 1800 | mojo::MakeRequest(&recorder)); |
Chris Cunningham | c7c6a6d | 2017-11-23 14:06:45 | [diff] [blame] | 1801 | |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 1802 | // Create capabilities reporter and synchronize its initial state. |
| 1803 | video_decode_stats_reporter_.reset(new VideoDecodeStatsReporter( |
Chris Cunningham | c7c6a6d | 2017-11-23 14:06:45 | [diff] [blame] | 1804 | std::move(recorder), |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 1805 | base::Bind(&WebMediaPlayerImpl::GetPipelineStatistics, |
| 1806 | base::Unretained(this)), |
Hajime Hoshi | 6c3194b5 | 2017-12-15 03:02:11 | [diff] [blame] | 1807 | pipeline_metadata_.video_decoder_config, |
Hajime Hoshi | b5a26ee | 2018-05-14 12:47:51 | [diff] [blame] | 1808 | frame_->GetTaskRunner(blink::TaskType::kInternalMedia))); |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 1809 | |
| 1810 | if (delegate_->IsFrameHidden()) |
| 1811 | video_decode_stats_reporter_->OnHidden(); |
| 1812 | else |
| 1813 | video_decode_stats_reporter_->OnShown(); |
| 1814 | |
| 1815 | if (paused_) |
| 1816 | video_decode_stats_reporter_->OnPaused(); |
| 1817 | else |
| 1818 | video_decode_stats_reporter_->OnPlaying(); |
| 1819 | } |
| 1820 | |
hubbe | b2d3efd | 2017-05-05 23:26:38 | [diff] [blame] | 1821 | void WebMediaPlayerImpl::OnProgress() { |
Chris Watkins | 1ffabc5f | 2017-05-10 20:52:09 | [diff] [blame] | 1822 | DVLOG(4) << __func__; |
hubbe | b2d3efd | 2017-05-05 23:26:38 | [diff] [blame] | 1823 | if (highest_ready_state_ < ReadyState::kReadyStateHaveFutureData) { |
| 1824 | // Reset the preroll attempt clock. |
| 1825 | preroll_attempt_pending_ = true; |
| 1826 | preroll_attempt_start_time_ = base::TimeTicks(); |
| 1827 | |
| 1828 | // Clear any 'stale' flag and give the pipeline a chance to resume. If we |
| 1829 | // are already resumed, this will cause |preroll_attempt_start_time_| to |
| 1830 | // be set. |
| 1831 | delegate_->ClearStaleFlag(delegate_id_); |
| 1832 | UpdatePlayState(); |
| 1833 | } else if (ready_state_ == ReadyState::kReadyStateHaveFutureData && |
| 1834 | CanPlayThrough()) { |
| 1835 | SetReadyState(WebMediaPlayer::kReadyStateHaveEnoughData); |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | bool WebMediaPlayerImpl::CanPlayThrough() { |
| 1840 | if (!base::FeatureList::IsEnabled(kSpecCompliantCanPlayThrough)) |
| 1841 | return true; |
| 1842 | if (chunk_demuxer_) |
| 1843 | return true; |
| 1844 | if (data_source_ && data_source_->assume_fully_buffered()) |
| 1845 | return true; |
| 1846 | // If we're not currently downloading, we have as much buffer as |
| 1847 | // we're ever going to get, which means we say we can play through. |
| 1848 | if (network_state_ == WebMediaPlayer::kNetworkStateIdle) |
| 1849 | return true; |
| 1850 | return buffered_data_source_host_.CanPlayThrough( |
| 1851 | base::TimeDelta::FromSecondsD(CurrentTime()), |
| 1852 | base::TimeDelta::FromSecondsD(Duration()), |
| 1853 | playback_rate_ == 0.0 ? 1.0 : playback_rate_); |
| 1854 | } |
| 1855 | |
Dale Curtis | ff57655 | 2018-03-30 02:32:44 | [diff] [blame] | 1856 | void WebMediaPlayerImpl::OnBufferingStateChangeInternal( |
| 1857 | BufferingState state, |
| 1858 | bool for_suspended_start) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 1859 | DVLOG(1) << __func__ << "(" << state << ")"; |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1860 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 1861 | |
Ted Meyer | 742212b8 | 2018-08-22 17:21:42 | [diff] [blame] | 1862 | // Ignore buffering state changes caused by back-to-back seeking, so as not |
| 1863 | // to assume the second seek has finished when it was only the first seek. |
| 1864 | if (pipeline_controller_.IsPendingSeek()) |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 1865 | return; |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 1866 | |
Dale Curtis | ff57655 | 2018-03-30 02:32:44 | [diff] [blame] | 1867 | auto log_event = media_log_->CreateBufferingStateChangedEvent( |
| 1868 | "pipeline_buffering_state", state); |
| 1869 | log_event->params.SetBoolean("for_suspended_start", for_suspended_start); |
| 1870 | media_log_->AddEvent(std::move(log_event)); |
dalecurtis | 869bf2f | 2017-01-10 18:01:10 | [diff] [blame] | 1871 | |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1872 | if (state == BUFFERING_HAVE_ENOUGH) { |
John Chen | 5b164a0 | 2017-11-01 00:36:09 | [diff] [blame] | 1873 | TRACE_EVENT1("media", "WebMediaPlayerImpl::BufferingHaveEnough", "id", |
| 1874 | media_log_->id()); |
Dale Curtis | ff57655 | 2018-03-30 02:32:44 | [diff] [blame] | 1875 | // The SetReadyState() call below may clear |
| 1876 | // |skip_metrics_due_to_startup_suspend_| so report this first. |
| 1877 | if (!have_reported_time_to_play_ready_ && |
| 1878 | !skip_metrics_due_to_startup_suspend_) { |
| 1879 | DCHECK(!for_suspended_start); |
Dale Curtis | 3899090ea | 2018-01-12 00:10:35 | [diff] [blame] | 1880 | have_reported_time_to_play_ready_ = true; |
| 1881 | const base::TimeDelta elapsed = base::TimeTicks::Now() - load_start_time_; |
| 1882 | media_metrics_provider_->SetTimeToPlayReady(elapsed); |
| 1883 | RecordTimingUMA("Media.TimeToPlayReady", elapsed); |
| 1884 | } |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 1885 | |
Dale Curtis | ff57655 | 2018-03-30 02:32:44 | [diff] [blame] | 1886 | // Warning: This call may be re-entrant. |
| 1887 | SetReadyState(CanPlayThrough() ? WebMediaPlayer::kReadyStateHaveEnoughData |
| 1888 | : WebMediaPlayer::kReadyStateHaveFutureData); |
| 1889 | |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1890 | // Let the DataSource know we have enough data. It may use this information |
| 1891 | // to release unused network connections. |
Fredrik Hubinette | 6dd3f322 | 2018-05-24 01:00:51 | [diff] [blame] | 1892 | if (data_source_ && !client_->CouldPlayIfEnoughData()) |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1893 | data_source_->OnBufferingHaveEnough(false); |
dalecurtis | 849cf4b2 | 2015-03-27 18:35:45 | [diff] [blame] | 1894 | |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1895 | // Blink expects a timeChanged() in response to a seek(). |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1896 | if (should_notify_time_changed_) { |
| 1897 | should_notify_time_changed_ = false; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1898 | client_->TimeChanged(); |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 1899 | } |
dalecurtis | 0f0097a | 2015-12-01 17:40:47 | [diff] [blame] | 1900 | |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1901 | // Once we have enough, start reporting the total memory usage. We'll also |
| 1902 | // report once playback starts. |
| 1903 | ReportMemoryUsage(); |
dalecurtis | 9d638a1 | 2016-08-30 06:20:55 | [diff] [blame] | 1904 | |
dalecurtis | 4f6d14d | 2017-02-22 17:42:22 | [diff] [blame] | 1905 | // Report the amount of time it took to leave the underflow state. |
| 1906 | if (underflow_timer_) { |
| 1907 | RecordUnderflowDuration(underflow_timer_->Elapsed()); |
dalecurtis | 9d638a1 | 2016-08-30 06:20:55 | [diff] [blame] | 1908 | underflow_timer_.reset(); |
| 1909 | } |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1910 | } else { |
| 1911 | // Buffering has underflowed. |
| 1912 | DCHECK_EQ(state, BUFFERING_HAVE_NOTHING); |
dalecurtis | acd77d6 | 2016-09-09 23:23:14 | [diff] [blame] | 1913 | |
dalecurtis | d06eabc | 2017-02-24 23:43:29 | [diff] [blame] | 1914 | // Report the number of times we've entered the underflow state. Ensure we |
| 1915 | // only report the value when transitioning from HAVE_ENOUGH to |
| 1916 | // HAVE_NOTHING. |
Dale Curtis | 6995b86 | 2017-05-31 22:20:08 | [diff] [blame] | 1917 | if (ready_state_ == WebMediaPlayer::kReadyStateHaveEnoughData && |
| 1918 | !seeking_) { |
dalecurtis | acd77d6 | 2016-09-09 23:23:14 | [diff] [blame] | 1919 | underflow_timer_.reset(new base::ElapsedTimer()); |
Dale Curtis | 6995b86 | 2017-05-31 22:20:08 | [diff] [blame] | 1920 | watch_time_reporter_->OnUnderflow(); |
| 1921 | } |
dalecurtis | acd77d6 | 2016-09-09 23:23:14 | [diff] [blame] | 1922 | |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1923 | // It shouldn't be possible to underflow if we've not advanced past |
| 1924 | // HAVE_CURRENT_DATA. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1925 | DCHECK_GT(highest_ready_state_, WebMediaPlayer::kReadyStateHaveCurrentData); |
| 1926 | SetReadyState(WebMediaPlayer::kReadyStateHaveCurrentData); |
chcunningham | eb270c9 | 2016-07-15 01:00:45 | [diff] [blame] | 1927 | } |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 1928 | |
| 1929 | UpdatePlayState(); |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 1930 | } |
| 1931 | |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1932 | void WebMediaPlayerImpl::OnDurationChange() { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1933 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1934 | |
| 1935 | // TODO(sandersd): We should call delegate_->DidPlay() with the new duration, |
| 1936 | // especially if it changed from <5s to >5s. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1937 | if (ready_state_ == WebMediaPlayer::kReadyStateHaveNothing) |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1938 | return; |
| 1939 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1940 | client_->DurationChanged(); |
Dale Curtis | aebaeea | 2018-07-19 23:42:11 | [diff] [blame] | 1941 | if (watch_time_reporter_) |
| 1942 | watch_time_reporter_->OnDurationChanged(GetPipelineMediaDuration()); |
[email protected] | 81bb332 | 2011-07-21 15:55:50 | [diff] [blame] | 1943 | } |
| 1944 | |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1945 | void WebMediaPlayerImpl::OnAddTextTrack(const TextTrackConfig& config, |
| 1946 | const AddTextTrackDoneCB& done_cb) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1947 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 1948 | |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 1949 | const WebInbandTextTrackImpl::Kind web_kind = |
| 1950 | static_cast<WebInbandTextTrackImpl::Kind>(config.kind()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1951 | const blink::WebString web_label = blink::WebString::FromUTF8(config.label()); |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 1952 | const blink::WebString web_language = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1953 | blink::WebString::FromUTF8(config.language()); |
| 1954 | const blink::WebString web_id = blink::WebString::FromUTF8(config.id()); |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 1955 | |
dcheng | 3076abbf | 2016-04-22 20:42:39 | [diff] [blame] | 1956 | std::unique_ptr<WebInbandTextTrackImpl> web_inband_text_track( |
henriks | 9cce5fa | 2014-12-12 09:35:30 | [diff] [blame] | 1957 | new WebInbandTextTrackImpl(web_kind, web_label, web_language, web_id)); |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 1958 | |
dcheng | 3076abbf | 2016-04-22 20:42:39 | [diff] [blame] | 1959 | std::unique_ptr<media::TextTrack> text_track(new TextTrackImpl( |
dcheng | 652f5ff | 2015-12-27 08:54:00 | [diff] [blame] | 1960 | main_task_runner_, client_, std::move(web_inband_text_track))); |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 1961 | |
dcheng | 652f5ff | 2015-12-27 08:54:00 | [diff] [blame] | 1962 | done_cb.Run(std::move(text_track)); |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 1963 | } |
| 1964 | |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1965 | void WebMediaPlayerImpl::OnWaitingForDecryptionKey() { |
| 1966 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1967 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1968 | encrypted_client_->DidBlockPlaybackWaitingForKey(); |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1969 | // TODO(jrummell): didResumePlaybackBlockedForKey() should only be called |
| 1970 | // when a key has been successfully added (e.g. OnSessionKeysChange() with |
| 1971 | // |has_additional_usable_key| = true). http://crbug.com/461903 |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1972 | encrypted_client_->DidResumePlaybackBlockedForKey(); |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 1973 | } |
| 1974 | |
alokp | 5d86e9b | 2016-05-17 20:20:41 | [diff] [blame] | 1975 | void WebMediaPlayerImpl::OnVideoNaturalSizeChange(const gfx::Size& size) { |
| 1976 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1977 | DCHECK_NE(ready_state_, WebMediaPlayer::kReadyStateHaveNothing); |
alokp | 5d86e9b | 2016-05-17 20:20:41 | [diff] [blame] | 1978 | |
Chris Cunningham | 038548b | 2017-07-10 22:36:30 | [diff] [blame] | 1979 | TRACE_EVENT0("media", "WebMediaPlayerImpl::OnVideoNaturalSizeChange"); |
xhwang | 6080265 | 2017-04-19 07:29:58 | [diff] [blame] | 1980 | |
xjz | 15b483f | 2017-01-12 00:21:36 | [diff] [blame] | 1981 | // The input |size| is from the decoded video frame, which is the original |
| 1982 | // natural size and need to be rotated accordingly. |
Julien Isorce | 6c83d8de | 2017-10-12 13:11:29 | [diff] [blame] | 1983 | gfx::Size rotated_size = GetRotatedVideoSize( |
| 1984 | pipeline_metadata_.video_decoder_config.video_rotation(), size); |
sandersd | 2c47842 | 2016-08-02 01:19:25 | [diff] [blame] | 1985 | |
xhwang | 6080265 | 2017-04-19 07:29:58 | [diff] [blame] | 1986 | RecordVideoNaturalSize(rotated_size); |
| 1987 | |
| 1988 | gfx::Size old_size = pipeline_metadata_.natural_size; |
| 1989 | if (rotated_size == old_size) |
alokp | 5d86e9b | 2016-05-17 20:20:41 | [diff] [blame] | 1990 | return; |
| 1991 | |
xjz | 516ef6d | 2017-01-07 00:23:06 | [diff] [blame] | 1992 | pipeline_metadata_.natural_size = rotated_size; |
Dale Curtis | 96d6e938 | 2018-07-18 18:01:07 | [diff] [blame] | 1993 | UpdateSecondaryProperties(); |
dalecurtis | 2540556 | 2017-04-14 23:35:11 | [diff] [blame] | 1994 | |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 1995 | if (video_decode_stats_reporter_) |
| 1996 | video_decode_stats_reporter_->OnNaturalSizeChanged(rotated_size); |
| 1997 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 1998 | client_->SizeChanged(); |
xjz | 516ef6d | 2017-01-07 00:23:06 | [diff] [blame] | 1999 | |
xjz | 15b483f | 2017-01-12 00:21:36 | [diff] [blame] | 2000 | if (observer_) |
| 2001 | observer_->OnMetadataChanged(pipeline_metadata_); |
peconn | 25795152 | 2017-06-09 18:24:59 | [diff] [blame] | 2002 | |
| 2003 | delegate_->DidPlayerSizeChange(delegate_id_, NaturalSize()); |
alokp | 5d86e9b | 2016-05-17 20:20:41 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | void WebMediaPlayerImpl::OnVideoOpacityChange(bool opaque) { |
| 2007 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2008 | DCHECK_NE(ready_state_, WebMediaPlayer::kReadyStateHaveNothing); |
alokp | 5d86e9b | 2016-05-17 20:20:41 | [diff] [blame] | 2009 | |
| 2010 | opaque_ = opaque; |
lethalantidote | 7f6009d | 2017-07-07 21:47:39 | [diff] [blame] | 2011 | if (!surface_layer_for_video_enabled_) { |
danakj | 8bc61c7 | 2018-05-16 13:55:06 | [diff] [blame] | 2012 | if (video_layer_) |
| 2013 | video_layer_->SetContentsOpaque(opaque_); |
danakj | 8d204a4 | 2018-05-18 18:05:35 | [diff] [blame] | 2014 | } else if (bridge_->GetCcLayer()) { |
CJ DiMeglio | a2b13fbc | 2018-06-27 00:50:59 | [diff] [blame] | 2015 | bridge_->SetContentsOpaque(opaque_); |
CJ DiMeglio | 2721137 | 2018-07-27 23:17:44 | [diff] [blame] | 2016 | vfc_task_runner_->PostTask( |
| 2017 | FROM_HERE, |
| 2018 | base::BindOnce(&VideoFrameCompositor::UpdateIsOpaque, |
| 2019 | base::Unretained(compositor_.get()), opaque_)); |
lethalantidote | 7f6009d | 2017-07-07 21:47:39 | [diff] [blame] | 2020 | } |
alokp | 5d86e9b | 2016-05-17 20:20:41 | [diff] [blame] | 2021 | } |
| 2022 | |
Chris Cunningham | 038548b | 2017-07-10 22:36:30 | [diff] [blame] | 2023 | void WebMediaPlayerImpl::OnAudioConfigChange(const AudioDecoderConfig& config) { |
| 2024 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2025 | DCHECK_NE(ready_state_, WebMediaPlayer::kReadyStateHaveNothing); |
| 2026 | |
Dale Curtis | ccfd0cca | 2017-08-31 01:27:56 | [diff] [blame] | 2027 | const bool codec_change = |
| 2028 | pipeline_metadata_.audio_decoder_config.codec() != config.codec(); |
Chris Cunningham | 038548b | 2017-07-10 22:36:30 | [diff] [blame] | 2029 | pipeline_metadata_.audio_decoder_config = config; |
| 2030 | |
| 2031 | if (observer_) |
| 2032 | observer_->OnMetadataChanged(pipeline_metadata_); |
Dale Curtis | ccfd0cca | 2017-08-31 01:27:56 | [diff] [blame] | 2033 | |
| 2034 | if (codec_change) |
Dale Curtis | 96d6e938 | 2018-07-18 18:01:07 | [diff] [blame] | 2035 | UpdateSecondaryProperties(); |
Chris Cunningham | 038548b | 2017-07-10 22:36:30 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | void WebMediaPlayerImpl::OnVideoConfigChange(const VideoDecoderConfig& config) { |
| 2039 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2040 | DCHECK_NE(ready_state_, WebMediaPlayer::kReadyStateHaveNothing); |
| 2041 | |
Dale Curtis | ccfd0cca | 2017-08-31 01:27:56 | [diff] [blame] | 2042 | const bool codec_change = |
| 2043 | pipeline_metadata_.video_decoder_config.codec() != config.codec(); |
| 2044 | |
Chris Cunningham | 038548b | 2017-07-10 22:36:30 | [diff] [blame] | 2045 | // TODO(chcunningham): Observe changes to video codec profile to signal |
| 2046 | // beginning of a new Media Capabilities playback report. |
| 2047 | pipeline_metadata_.video_decoder_config = config; |
| 2048 | |
| 2049 | if (observer_) |
| 2050 | observer_->OnMetadataChanged(pipeline_metadata_); |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 2051 | |
| 2052 | if (video_decode_stats_reporter_) |
| 2053 | video_decode_stats_reporter_->OnVideoConfigChanged(config); |
Dale Curtis | ccfd0cca | 2017-08-31 01:27:56 | [diff] [blame] | 2054 | |
| 2055 | if (codec_change) |
Dale Curtis | 96d6e938 | 2018-07-18 18:01:07 | [diff] [blame] | 2056 | UpdateSecondaryProperties(); |
Chris Cunningham | 038548b | 2017-07-10 22:36:30 | [diff] [blame] | 2057 | } |
| 2058 | |
avayvod | eecec52c | 2017-02-14 01:25:09 | [diff] [blame] | 2059 | void WebMediaPlayerImpl::OnVideoAverageKeyframeDistanceUpdate() { |
| 2060 | UpdateBackgroundVideoOptimizationState(); |
| 2061 | } |
| 2062 | |
Dale Curtis | c7d2a7d2 | 2018-01-11 20:01:05 | [diff] [blame] | 2063 | void WebMediaPlayerImpl::OnAudioDecoderChange(const std::string& name) { |
| 2064 | if (name == audio_decoder_name_) |
| 2065 | return; |
| 2066 | |
Dale Curtis | c7d2a7d2 | 2018-01-11 20:01:05 | [diff] [blame] | 2067 | audio_decoder_name_ = name; |
| 2068 | |
| 2069 | // If there's no current reporter, there's nothing to be done. |
| 2070 | if (!watch_time_reporter_) |
| 2071 | return; |
| 2072 | |
Dale Curtis | 96d6e938 | 2018-07-18 18:01:07 | [diff] [blame] | 2073 | UpdateSecondaryProperties(); |
Dale Curtis | c7d2a7d2 | 2018-01-11 20:01:05 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | void WebMediaPlayerImpl::OnVideoDecoderChange(const std::string& name) { |
| 2077 | if (name == video_decoder_name_) |
| 2078 | return; |
| 2079 | |
Dale Curtis | c7d2a7d2 | 2018-01-11 20:01:05 | [diff] [blame] | 2080 | video_decoder_name_ = name; |
| 2081 | |
| 2082 | // If there's no current reporter, there's nothing to be done. |
| 2083 | if (!watch_time_reporter_) |
| 2084 | return; |
| 2085 | |
Dale Curtis | 96d6e938 | 2018-07-18 18:01:07 | [diff] [blame] | 2086 | UpdateSecondaryProperties(); |
Dale Curtis | c7d2a7d2 | 2018-01-11 20:01:05 | [diff] [blame] | 2087 | } |
| 2088 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2089 | void WebMediaPlayerImpl::OnFrameHidden() { |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 2090 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
avayvod | 39c10240 | 2016-11-23 21:43:13 | [diff] [blame] | 2091 | |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2092 | // Backgrounding a video requires a user gesture to resume playback. |
| 2093 | if (IsHidden()) |
| 2094 | video_locked_when_paused_when_hidden_ = true; |
| 2095 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 2096 | if (watch_time_reporter_) |
| 2097 | watch_time_reporter_->OnHidden(); |
avayvod | 48a8be5 | 2016-08-04 19:52:50 | [diff] [blame] | 2098 | |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 2099 | if (video_decode_stats_reporter_) |
| 2100 | video_decode_stats_reporter_->OnHidden(); |
| 2101 | |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2102 | UpdateBackgroundVideoOptimizationState(); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2103 | UpdatePlayState(); |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 2104 | |
| 2105 | // Schedule suspended playing media to be paused if the user doesn't come back |
| 2106 | // to it within some timeout period to avoid any autoplay surprises. |
| 2107 | ScheduleIdlePauseTimer(); |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 2108 | } |
| 2109 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2110 | void WebMediaPlayerImpl::OnFrameClosed() { |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 2111 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 28347e7 | 2017-06-27 17:30:11 | [diff] [blame] | 2112 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2113 | UpdatePlayState(); |
| 2114 | } |
| 2115 | |
| 2116 | void WebMediaPlayerImpl::OnFrameShown() { |
| 2117 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2118 | background_pause_timer_.Stop(); |
| 2119 | |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2120 | // Foreground videos don't require user gesture to continue playback. |
| 2121 | video_locked_when_paused_when_hidden_ = false; |
| 2122 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 2123 | if (watch_time_reporter_) |
| 2124 | watch_time_reporter_->OnShown(); |
| 2125 | |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 2126 | if (video_decode_stats_reporter_) |
| 2127 | video_decode_stats_reporter_->OnShown(); |
| 2128 | |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2129 | // Only track the time to the first frame if playing or about to play because |
| 2130 | // of being shown and only for videos we would optimize background playback |
| 2131 | // for. |
| 2132 | if ((!paused_ && IsBackgroundOptimizationCandidate()) || |
| 2133 | paused_when_hidden_) { |
Dale Curtis | 3899090ea | 2018-01-12 00:10:35 | [diff] [blame] | 2134 | frame_time_report_cb_.Reset(base::BindOnce( |
| 2135 | &WebMediaPlayerImpl::ReportTimeFromForegroundToFirstFrame, AsWeakPtr(), |
| 2136 | base::TimeTicks::Now())); |
CJ DiMeglio | 2302d20 | 2017-08-31 08:38:04 | [diff] [blame] | 2137 | vfc_task_runner_->PostTask( |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2138 | FROM_HERE, |
Dale Curtis | 3899090ea | 2018-01-12 00:10:35 | [diff] [blame] | 2139 | base::BindOnce(&VideoFrameCompositor::SetOnNewProcessedFrameCallback, |
| 2140 | base::Unretained(compositor_.get()), |
| 2141 | BindToCurrentLoop(frame_time_report_cb_.callback()))); |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2142 | } |
avayvod | ac607d65 | 2017-01-06 03:16:43 | [diff] [blame] | 2143 | |
Dale Curtis | dcbb81a | 2017-08-18 01:06:12 | [diff] [blame] | 2144 | UpdateBackgroundVideoOptimizationState(); |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2145 | |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 2146 | if (paused_when_hidden_) { |
| 2147 | paused_when_hidden_ = false; |
| 2148 | OnPlay(); // Calls UpdatePlayState() so return afterwards. |
| 2149 | return; |
| 2150 | } |
| 2151 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2152 | UpdatePlayState(); |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 2153 | } |
| 2154 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2155 | void WebMediaPlayerImpl::OnIdleTimeout() { |
dalecurtis | 0431cbf | 2016-03-12 01:19:43 | [diff] [blame] | 2156 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2157 | |
Dale Curtis | 99a9b48 | 2018-02-01 02:23:28 | [diff] [blame] | 2158 | // This should never be called when stale state testing overrides are used. |
| 2159 | DCHECK(!stale_state_override_for_testing_.has_value()); |
| 2160 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2161 | // If we are attempting preroll, clear the stale flag. |
| 2162 | if (IsPrerollAttemptNeeded()) { |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 2163 | delegate_->ClearStaleFlag(delegate_id_); |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2164 | return; |
watk | d026f79 | 2016-11-05 00:28:51 | [diff] [blame] | 2165 | } |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2166 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2167 | UpdatePlayState(); |
dalecurtis | 0431cbf | 2016-03-12 01:19:43 | [diff] [blame] | 2168 | } |
| 2169 | |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 2170 | void WebMediaPlayerImpl::OnPlay() { |
Mounir Lamouri | 703106e | 2018-05-30 14:31:09 | [diff] [blame] | 2171 | client_->RequestPlay(); |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | void WebMediaPlayerImpl::OnPause() { |
Mounir Lamouri | 703106e | 2018-05-30 14:31:09 | [diff] [blame] | 2175 | client_->RequestPause(); |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 2176 | } |
| 2177 | |
Alec Douglas | 316cce4 | 2017-10-31 13:28:08 | [diff] [blame] | 2178 | void WebMediaPlayerImpl::OnSeekForward(double seconds) { |
| 2179 | DCHECK_GE(seconds, 0) << "Attempted to seek by a negative number of seconds"; |
| 2180 | client_->RequestSeek(CurrentTime() + seconds); |
| 2181 | } |
| 2182 | |
| 2183 | void WebMediaPlayerImpl::OnSeekBackward(double seconds) { |
| 2184 | DCHECK_GE(seconds, 0) << "Attempted to seek by a negative number of seconds"; |
| 2185 | client_->RequestSeek(CurrentTime() - seconds); |
| 2186 | } |
| 2187 | |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 2188 | void WebMediaPlayerImpl::OnVolumeMultiplierUpdate(double multiplier) { |
| 2189 | volume_multiplier_ = multiplier; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2190 | SetVolume(volume_); |
dalecurtis | bb3eaac | 2016-01-27 21:10:25 | [diff] [blame] | 2191 | } |
| 2192 | |
zqzhang | 8ac4900 | 2017-03-16 21:51:35 | [diff] [blame] | 2193 | void WebMediaPlayerImpl::OnBecamePersistentVideo(bool value) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2194 | client_->OnBecamePersistentVideo(value); |
zqzhang | 8ac4900 | 2017-03-16 21:51:35 | [diff] [blame] | 2195 | } |
| 2196 | |
Jennifer Apacible | 009f2984 | 2018-04-16 23:07:37 | [diff] [blame] | 2197 | void WebMediaPlayerImpl::OnPictureInPictureModeEnded() { |
Mounir Lamouri | b642a3c | 2018-07-09 15:45:31 | [diff] [blame] | 2198 | // It is possible for this method to be called when the player is no longer in |
| 2199 | // Picture-in-Picture mode. |
Mounir Lamouri | 0484f40a | 2018-07-25 03:03:26 | [diff] [blame] | 2200 | if (!client_ || !IsInPictureInPicture()) |
Mounir Lamouri | b642a3c | 2018-07-09 15:45:31 | [diff] [blame] | 2201 | return; |
Mounir Lamouri | b642a3c | 2018-07-09 15:45:31 | [diff] [blame] | 2202 | |
| 2203 | client_->PictureInPictureStopped(); |
Jennifer Apacible | 009f2984 | 2018-04-16 23:07:37 | [diff] [blame] | 2204 | } |
| 2205 | |
sawtelle | 82e4b75 | 2018-07-19 04:33:12 | [diff] [blame] | 2206 | void WebMediaPlayerImpl::OnPictureInPictureControlClicked( |
| 2207 | const std::string& control_id) { |
Mounir Lamouri | 0484f40a | 2018-07-25 03:03:26 | [diff] [blame] | 2208 | if (client_ && IsInPictureInPicture()) { |
sawtelle | 82e4b75 | 2018-07-19 04:33:12 | [diff] [blame] | 2209 | client_->PictureInPictureControlClicked( |
| 2210 | blink::WebString::FromUTF8(control_id)); |
sawtelle | 42eac6a | 2018-06-26 03:22:01 | [diff] [blame] | 2211 | } |
| 2212 | } |
| 2213 | |
John Delaney | b93339160 | 2018-10-17 21:50:47 | [diff] [blame] | 2214 | void WebMediaPlayerImpl::OnBytesReceived(uint64_t data_length) { |
| 2215 | media_metrics_provider_->AddBytesReceived(data_length); |
| 2216 | } |
| 2217 | |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 2218 | void WebMediaPlayerImpl::ScheduleRestart() { |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2219 | // TODO(watk): All restart logic should be moved into PipelineController. |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 2220 | if (pipeline_controller_.IsPipelineRunning() && |
| 2221 | !pipeline_controller_.IsPipelineSuspended()) { |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 2222 | pending_suspend_resume_cycle_ = true; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2223 | UpdatePlayState(); |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 2224 | } |
| 2225 | } |
| 2226 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2227 | void WebMediaPlayerImpl::RequestRemotePlaybackDisabled(bool disabled) { |
miu | 77f914c | 2016-11-19 23:56:18 | [diff] [blame] | 2228 | if (observer_) |
| 2229 | observer_->OnRemotePlaybackDisabled(disabled); |
| 2230 | } |
| 2231 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2232 | #if defined(OS_ANDROID) // WMPI_CAST |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2233 | bool WebMediaPlayerImpl::IsRemote() const { |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2234 | return cast_impl_.isRemote(); |
| 2235 | } |
| 2236 | |
| 2237 | void WebMediaPlayerImpl::SetMediaPlayerManager( |
| 2238 | RendererMediaPlayerManagerInterface* media_player_manager) { |
| 2239 | cast_impl_.SetMediaPlayerManager(media_player_manager); |
| 2240 | } |
| 2241 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2242 | void WebMediaPlayerImpl::RequestRemotePlayback() { |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2243 | cast_impl_.requestRemotePlayback(); |
| 2244 | } |
| 2245 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2246 | void WebMediaPlayerImpl::RequestRemotePlaybackControl() { |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2247 | cast_impl_.requestRemotePlaybackControl(); |
| 2248 | } |
| 2249 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2250 | void WebMediaPlayerImpl::RequestRemotePlaybackStop() { |
avayvod | 8d8c53b | 2016-11-04 16:10:30 | [diff] [blame] | 2251 | cast_impl_.requestRemotePlaybackStop(); |
| 2252 | } |
| 2253 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2254 | void WebMediaPlayerImpl::OnRemotePlaybackEnded() { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 2255 | DVLOG(1) << __func__; |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2256 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2257 | |
| 2258 | ended_ = true; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2259 | client_->TimeChanged(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2260 | } |
| 2261 | |
Thomas Guilbert | b341bae0 | 2018-05-09 00:02:13 | [diff] [blame] | 2262 | void WebMediaPlayerImpl::FlingingStarted() { |
| 2263 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2264 | DCHECK(!disable_pipeline_auto_suspend_); |
| 2265 | disable_pipeline_auto_suspend_ = true; |
| 2266 | |
Thomas Guilbert | 29ae1a90 | 2018-10-20 01:53:38 | [diff] [blame] | 2267 | is_flinging_ = true; |
| 2268 | |
Thomas Guilbert | b341bae0 | 2018-05-09 00:02:13 | [diff] [blame] | 2269 | // Capabilities reporting should only be performed for local playbacks. |
| 2270 | video_decode_stats_reporter_.reset(); |
| 2271 | |
| 2272 | // Requests to restart media pipeline. A flinging renderer will be created via |
| 2273 | // the |renderer_factory_selector_|. |
| 2274 | ScheduleRestart(); |
| 2275 | } |
| 2276 | |
| 2277 | void WebMediaPlayerImpl::FlingingStopped() { |
| 2278 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2279 | DCHECK(disable_pipeline_auto_suspend_); |
| 2280 | disable_pipeline_auto_suspend_ = false; |
| 2281 | |
Thomas Guilbert | 29ae1a90 | 2018-10-20 01:53:38 | [diff] [blame] | 2282 | is_flinging_ = false; |
| 2283 | |
Thomas Guilbert | b341bae0 | 2018-05-09 00:02:13 | [diff] [blame] | 2284 | CreateVideoDecodeStatsReporter(); |
| 2285 | |
| 2286 | ScheduleRestart(); |
| 2287 | } |
| 2288 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2289 | void WebMediaPlayerImpl::OnDisconnectedFromRemoteDevice(double t) { |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 2290 | DoSeek(base::TimeDelta::FromSecondsD(t), false); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2291 | |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 2292 | // Capabilities reporting can resume now that playback is local. |
| 2293 | CreateVideoDecodeStatsReporter(); |
| 2294 | |
Anton Vayvod | fad2f3ea | 2017-07-19 21:45:27 | [diff] [blame] | 2295 | // |client_| might destroy us in methods below. |
| 2296 | UpdatePlayState(); |
| 2297 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2298 | // We already told the delegate we're paused when remoting started. |
Mounir Lamouri | 703106e | 2018-05-30 14:31:09 | [diff] [blame] | 2299 | client_->RequestPause(); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2300 | client_->DisconnectedFromRemoteDevice(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | void WebMediaPlayerImpl::SuspendForRemote() { |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 2304 | // Capabilities reporting should only be performed for local playbacks. |
| 2305 | video_decode_stats_reporter_.reset(); |
| 2306 | |
avayvod | 8272927 | 2017-05-29 21:58:39 | [diff] [blame] | 2307 | if (pipeline_controller_.IsPipelineSuspended() && |
| 2308 | !IsNewRemotePlaybackPipelineEnabled()) { |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2309 | scoped_refptr<VideoFrame> frame = cast_impl_.GetCastingBanner(); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2310 | if (frame) |
dalecurtis | e9c89e9 | 2016-05-20 19:38:00 | [diff] [blame] | 2311 | compositor_->PaintSingleFrame(frame); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2312 | } |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2313 | |
| 2314 | UpdatePlayState(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | gfx::Size WebMediaPlayerImpl::GetCanvasSize() const { |
lethalantidote | 7f6009d | 2017-07-07 21:47:39 | [diff] [blame] | 2318 | if (!surface_layer_for_video_enabled_) { |
danakj | 6e669e78 | 2018-05-16 16:57:17 | [diff] [blame] | 2319 | if (!video_layer_) |
lethalantidote | 7f6009d | 2017-07-07 21:47:39 | [diff] [blame] | 2320 | return pipeline_metadata_.natural_size; |
| 2321 | |
danakj | 6e669e78 | 2018-05-16 16:57:17 | [diff] [blame] | 2322 | return video_layer_->bounds(); |
lethalantidote | 7f6009d | 2017-07-07 21:47:39 | [diff] [blame] | 2323 | } |
danakj | 8d204a4 | 2018-05-18 18:05:35 | [diff] [blame] | 2324 | if (!bridge_->GetCcLayer()) |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2325 | return pipeline_metadata_.natural_size; |
| 2326 | |
danakj | 8d204a4 | 2018-05-18 18:05:35 | [diff] [blame] | 2327 | return bridge_->GetCcLayer()->bounds(); |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | void WebMediaPlayerImpl::SetDeviceScaleFactor(float scale_factor) { |
| 2331 | cast_impl_.SetDeviceScaleFactor(scale_factor); |
| 2332 | } |
Dan Sanders | cd8981c | 2017-08-31 22:37:02 | [diff] [blame] | 2333 | #endif // defined(OS_ANDROID) // WMPI_CAST |
hubbe | e4027f9 | 2016-05-19 05:18:13 | [diff] [blame] | 2334 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2335 | void WebMediaPlayerImpl::SetPoster(const blink::WebURL& poster) { |
Dan Sanders | cd8981c | 2017-08-31 22:37:02 | [diff] [blame] | 2336 | has_poster_ = !poster.IsEmpty(); |
| 2337 | #if defined(OS_ANDROID) // WMPI_CAST |
xjz | c102fd8 | 2017-01-04 20:13:53 | [diff] [blame] | 2338 | cast_impl_.setPoster(poster); |
xjz | 2504c4da | 2017-04-18 18:50:14 | [diff] [blame] | 2339 | #endif // defined(OS_ANDROID) // WMPI_CAST |
Dan Sanders | cd8981c | 2017-08-31 22:37:02 | [diff] [blame] | 2340 | } |
xjz | c102fd8 | 2017-01-04 20:13:53 | [diff] [blame] | 2341 | |
[email protected] | fee8a90 | 2014-06-03 13:43:36 | [diff] [blame] | 2342 | void WebMediaPlayerImpl::DataSourceInitialized(bool success) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 2343 | DVLOG(1) << __func__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 2344 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | a941529 | 2012-01-19 19:55:20 | [diff] [blame] | 2345 | |
Anton Vayvod | 09fa66e | 2017-07-20 23:02:12 | [diff] [blame] | 2346 | if (observer_ && IsNewRemotePlaybackPipelineEnabled() && data_source_) |
| 2347 | observer_->OnDataSourceInitialized(data_source_->GetUrlAfterRedirects()); |
| 2348 | |
[email protected] | d250190da3b | 2012-07-23 22:57:30 | [diff] [blame] | 2349 | if (!success) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2350 | SetNetworkState(WebMediaPlayer::kNetworkStateFormatError); |
Dale Curtis | 74612b7 | 2017-12-14 20:56:19 | [diff] [blame] | 2351 | media_metrics_provider_->OnError(PIPELINE_ERROR_NETWORK); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2352 | |
| 2353 | // Not really necessary, since the pipeline was never started, but it at |
| 2354 | // least this makes sure that the error handling code is in sync. |
| 2355 | UpdatePlayState(); |
| 2356 | |
[email protected] | a941529 | 2012-01-19 19:55:20 | [diff] [blame] | 2357 | return; |
| 2358 | } |
| 2359 | |
hubbe | e2cc88c09 | 2017-07-14 23:10:41 | [diff] [blame] | 2360 | // No point in preloading data as we'll probably just throw it away anyways. |
| 2361 | if (IsStreaming() && preload_ > MultibufferDataSource::METADATA) { |
| 2362 | data_source_->SetPreload(MultibufferDataSource::METADATA); |
| 2363 | } |
| 2364 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 2365 | StartPipeline(); |
[email protected] | a941529 | 2012-01-19 19:55:20 | [diff] [blame] | 2366 | } |
| 2367 | |
[email protected] | 122f4025 | 2012-06-12 05:01:56 | [diff] [blame] | 2368 | void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) { |
hubbe | b2d3efd | 2017-05-05 23:26:38 | [diff] [blame] | 2369 | DVLOG(1) << __func__ << "(" << is_downloading << ")"; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2370 | if (!is_downloading && network_state_ == WebMediaPlayer::kNetworkStateLoading) |
| 2371 | SetNetworkState(WebMediaPlayer::kNetworkStateIdle); |
| 2372 | else if (is_downloading && |
| 2373 | network_state_ == WebMediaPlayer::kNetworkStateIdle) |
| 2374 | SetNetworkState(WebMediaPlayer::kNetworkStateLoading); |
hubbe | b2d3efd | 2017-05-05 23:26:38 | [diff] [blame] | 2375 | if (ready_state_ == ReadyState::kReadyStateHaveFutureData && !is_downloading) |
| 2376 | SetReadyState(WebMediaPlayer::kReadyStateHaveEnoughData); |
[email protected] | 122f4025 | 2012-06-12 05:01:56 | [diff] [blame] | 2377 | } |
| 2378 | |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 2379 | void WebMediaPlayerImpl::OnOverlayRoutingToken( |
| 2380 | const base::UnguessableToken& token) { |
| 2381 | DCHECK(overlay_mode_ == OverlayMode::kUseAndroidOverlay); |
liberato | fe8f969 | 2017-06-08 19:17:36 | [diff] [blame] | 2382 | // TODO(liberato): |token| should already be a RoutingToken. |
| 2383 | overlay_routing_token_is_pending_ = false; |
| 2384 | overlay_routing_token_ = OverlayInfo::RoutingToken(token); |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 2385 | MaybeSendOverlayInfoToDecoder(); |
| 2386 | } |
| 2387 | |
| 2388 | void WebMediaPlayerImpl::OnOverlayInfoRequested( |
dalecurtis | 4b632fce2 | 2016-11-10 00:52:17 | [diff] [blame] | 2389 | bool decoder_requires_restart_for_overlay, |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 2390 | const ProvideOverlayInfoCB& provide_overlay_info_cb) { |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 2391 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 2392 | |
Chris Watkins | 557f84d | 2017-09-16 02:31:46 | [diff] [blame] | 2393 | // If we get a non-null cb, a decoder is initializing and requires overlay |
| 2394 | // info. If we get a null cb, a previously initialized decoder is |
| 2395 | // unregistering for overlay info updates. |
Dale Curtis | e2516381 | 2018-09-21 22:13:39 | [diff] [blame] | 2396 | if (!provide_overlay_info_cb) { |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 2397 | decoder_requires_restart_for_overlay_ = false; |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 2398 | provide_overlay_info_cb_.Reset(); |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 2399 | return; |
| 2400 | } |
| 2401 | |
dalecurtis | 4b632fce2 | 2016-11-10 00:52:17 | [diff] [blame] | 2402 | // If |decoder_requires_restart_for_overlay| is true, we must restart the |
| 2403 | // pipeline for fullscreen transitions. The decoder is unable to switch |
| 2404 | // surfaces otherwise. If false, we simply need to tell the decoder about the |
| 2405 | // new surface and it will handle things seamlessly. |
Chris Watkins | 557f84d | 2017-09-16 02:31:46 | [diff] [blame] | 2406 | // For encrypted video we pretend that the decoder doesn't require a restart |
| 2407 | // because it needs an overlay all the time anyway. We'll switch into |
| 2408 | // |always_enable_overlays_| mode below. |
| 2409 | decoder_requires_restart_for_overlay_ = |
| 2410 | (overlay_mode_ == OverlayMode::kUseAndroidOverlay && is_encrypted_) |
| 2411 | ? false |
| 2412 | : decoder_requires_restart_for_overlay; |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 2413 | provide_overlay_info_cb_ = provide_overlay_info_cb; |
dalecurtis | 4b632fce2 | 2016-11-10 00:52:17 | [diff] [blame] | 2414 | |
Chris Watkins | 557f84d | 2017-09-16 02:31:46 | [diff] [blame] | 2415 | // If the decoder doesn't require restarts for surface transitions, and we're |
| 2416 | // using AndroidOverlay mode, we can always enable the overlay and the decoder |
| 2417 | // can choose whether or not to use it. Otherwise, we'll restart the decoder |
| 2418 | // and enable the overlay on fullscreen transitions. |
[email protected] | 7756848 | 2017-06-21 21:16:52 | [diff] [blame] | 2419 | if (overlay_mode_ == OverlayMode::kUseAndroidOverlay && |
Chris Watkins | 557f84d | 2017-09-16 02:31:46 | [diff] [blame] | 2420 | !decoder_requires_restart_for_overlay_) { |
[email protected] | c8d57472 | 2017-08-30 20:53:43 | [diff] [blame] | 2421 | always_enable_overlays_ = true; |
[email protected] | 7756848 | 2017-06-21 21:16:52 | [diff] [blame] | 2422 | if (!overlay_enabled_) |
| 2423 | EnableOverlay(); |
| 2424 | } |
| 2425 | |
Chris Watkins | 557f84d | 2017-09-16 02:31:46 | [diff] [blame] | 2426 | // Send the overlay info if we already have it. If not, it will be sent later. |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 2427 | MaybeSendOverlayInfoToDecoder(); |
| 2428 | } |
| 2429 | |
| 2430 | void WebMediaPlayerImpl::MaybeSendOverlayInfoToDecoder() { |
| 2431 | // If the decoder didn't request overlay info, then don't send it. |
| 2432 | if (!provide_overlay_info_cb_) |
dalecurtis | 4b632fce2 | 2016-11-10 00:52:17 | [diff] [blame] | 2433 | return; |
| 2434 | |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 2435 | // We should send the overlay info as long as we know it. This includes the |
| 2436 | // case where |!overlay_enabled_|, since we want to tell the decoder to avoid |
| 2437 | // using overlays. Assuming that the decoder has requested info, the only |
| 2438 | // case in which we don't want to send something is if we've requested the |
| 2439 | // info but not received it yet. Then, we should wait until we do. |
liberato | fe8f969 | 2017-06-08 19:17:36 | [diff] [blame] | 2440 | // |
| 2441 | // Initialization requires this; AVDA should start with enough info to make an |
| 2442 | // overlay, so that (pre-M) the initial codec is created with the right output |
| 2443 | // surface; it can't switch later. |
[email protected] | f7df5b34 | 2018-07-13 20:22:13 | [diff] [blame] | 2444 | if (overlay_mode_ == OverlayMode::kUseAndroidOverlay) { |
liberato | fe8f969 | 2017-06-08 19:17:36 | [diff] [blame] | 2445 | if (overlay_routing_token_is_pending_) |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 2446 | return; |
liberato | fe8f969 | 2017-06-08 19:17:36 | [diff] [blame] | 2447 | |
| 2448 | overlay_info_.routing_token = overlay_routing_token_; |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 2449 | } |
| 2450 | |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 2451 | // If restart is required, the callback is one-shot only. |
| 2452 | if (decoder_requires_restart_for_overlay_) { |
Dale Curtis | e2516381 | 2018-09-21 22:13:39 | [diff] [blame] | 2453 | std::move(provide_overlay_info_cb_).Run(overlay_info_); |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 2454 | } else { |
liberato | fe8f969 | 2017-06-08 19:17:36 | [diff] [blame] | 2455 | provide_overlay_info_cb_.Run(overlay_info_); |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 2456 | } |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 2457 | } |
| 2458 | |
dcheng | 3076abbf | 2016-04-22 20:42:39 | [diff] [blame] | 2459 | std::unique_ptr<Renderer> WebMediaPlayerImpl::CreateRenderer() { |
dcheng | 37b415b9 | 2017-01-27 20:17:43 | [diff] [blame] | 2460 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2461 | |
[email protected] | c8d57472 | 2017-08-30 20:53:43 | [diff] [blame] | 2462 | // Make sure that overlays are enabled if they're always allowed. |
| 2463 | if (always_enable_overlays_) |
tsunghung | ee562e9 | 2016-07-20 18:03:31 | [diff] [blame] | 2464 | EnableOverlay(); |
| 2465 | |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 2466 | RequestOverlayInfoCB request_overlay_info_cb; |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 2467 | #if defined(OS_ANDROID) |
liberato | 2ff93ad | 2017-05-17 07:28:24 | [diff] [blame] | 2468 | request_overlay_info_cb = BindToCurrentLoop( |
| 2469 | base::Bind(&WebMediaPlayerImpl::OnOverlayInfoRequested, AsWeakPtr())); |
watk | dee516f | 2016-02-18 02:22:19 | [diff] [blame] | 2470 | #endif |
tguilbert | 70d2a00a | 2017-04-25 00:30:44 | [diff] [blame] | 2471 | return renderer_factory_selector_->GetCurrentFactory()->CreateRenderer( |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 2472 | media_task_runner_, worker_task_runner_, audio_source_provider_.get(), |
CJ DiMeglio | 2302d20 | 2017-08-31 08:38:04 | [diff] [blame] | 2473 | compositor_.get(), request_overlay_info_cb, client_->TargetColorSpace()); |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 2474 | } |
| 2475 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 2476 | void WebMediaPlayerImpl::StartPipeline() { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 2477 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 2478 | |
xhwang | e8c4181a | 2014-12-06 08:10:01 | [diff] [blame] | 2479 | Demuxer::EncryptedMediaInitDataCB encrypted_media_init_data_cb = |
dcheng | 37b415b9 | 2017-01-27 20:17:43 | [diff] [blame] | 2480 | BindToCurrentLoop(base::Bind( |
| 2481 | &WebMediaPlayerImpl::OnEncryptedMediaInitData, AsWeakPtr())); |
[email protected] | 2b57e2e | 2014-05-09 11:07:25 | [diff] [blame] | 2482 | |
Dale Curtis | 3899090ea | 2018-01-12 00:10:35 | [diff] [blame] | 2483 | vfc_task_runner_->PostTask( |
| 2484 | FROM_HERE, |
| 2485 | base::BindOnce(&VideoFrameCompositor::SetOnNewProcessedFrameCallback, |
| 2486 | base::Unretained(compositor_.get()), |
| 2487 | BindToCurrentLoop(base::BindOnce( |
| 2488 | &WebMediaPlayerImpl::OnFirstFrame, AsWeakPtr())))); |
| 2489 | |
tguilbert | 75e2bf6 | 2017-04-26 20:13:12 | [diff] [blame] | 2490 | if (renderer_factory_selector_->GetCurrentFactory() |
| 2491 | ->GetRequiredMediaResourceType() == MediaResource::Type::URL) { |
| 2492 | if (data_source_) |
| 2493 | loaded_url_ = data_source_->GetUrlAfterRedirects(); |
| 2494 | |
| 2495 | // MediaPlayerRendererClient factory is the only factory that a |
| 2496 | // MediaResource::Type::URL for the moment. This might no longer be true |
| 2497 | // when we remove WebMediaPlayerCast. |
| 2498 | // |
| 2499 | // TODO(tguilbert/avayvod): Update this flag when removing |cast_impl_|. |
| 2500 | using_media_player_renderer_ = true; |
| 2501 | |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 2502 | // MediaPlayerRenderer does not provide pipeline stats, so nuke capabilities |
| 2503 | // reporter. |
| 2504 | video_decode_stats_reporter_.reset(); |
| 2505 | |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 2506 | demuxer_.reset(new MediaUrlDemuxer(media_task_runner_, loaded_url_, |
| 2507 | frame_->GetDocument().SiteForCookies())); |
Dale Curtis | c00d5648 | 2018-02-09 20:55:06 | [diff] [blame] | 2508 | pipeline_controller_.Start(Pipeline::StartType::kNormal, demuxer_.get(), |
| 2509 | this, false, false); |
tguilbert | 25a4d11 | 2016-10-13 21:56:51 | [diff] [blame] | 2510 | return; |
| 2511 | } |
| 2512 | |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 2513 | // Figure out which demuxer to use. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2514 | if (load_type_ != kLoadTypeMediaSource) { |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 2515 | DCHECK(!chunk_demuxer_); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 2516 | DCHECK(data_source_); |
| 2517 | |
Dale Curtis | bcf523b | 2018-01-17 02:59:01 | [diff] [blame] | 2518 | #if BUILDFLAG(ENABLE_FFMPEG) |
servolk | 81e01e0 | 2016-03-05 03:29:15 | [diff] [blame] | 2519 | Demuxer::MediaTracksUpdatedCB media_tracks_updated_cb = |
dcheng | 37b415b9 | 2017-01-27 20:17:43 | [diff] [blame] | 2520 | BindToCurrentLoop(base::Bind( |
| 2521 | &WebMediaPlayerImpl::OnFFmpegMediaTracksUpdated, AsWeakPtr())); |
servolk | 81e01e0 | 2016-03-05 03:29:15 | [diff] [blame] | 2522 | |
dalecurtis | 9cddc0b | 2017-04-19 21:23:38 | [diff] [blame] | 2523 | demuxer_.reset(new FFmpegDemuxer( |
| 2524 | media_task_runner_, data_source_.get(), encrypted_media_init_data_cb, |
Dale Curtis | b8139f7 | 2018-08-27 23:28:48 | [diff] [blame] | 2525 | media_tracks_updated_cb, media_log_.get(), IsLocalFile(loaded_url_))); |
j.isorce | f6778e65 | 2015-11-16 17:14:25 | [diff] [blame] | 2526 | #else |
alokp | 967c90245 | 2016-05-06 05:21:37 | [diff] [blame] | 2527 | OnError(PipelineStatus::DEMUXER_ERROR_COULD_NOT_OPEN); |
j.isorce | f6778e65 | 2015-11-16 17:14:25 | [diff] [blame] | 2528 | return; |
| 2529 | #endif |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 2530 | } else { |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 2531 | DCHECK(!chunk_demuxer_); |
| 2532 | DCHECK(!data_source_); |
| 2533 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 2534 | chunk_demuxer_ = new ChunkDemuxer( |
dcheng | 37b415b9 | 2017-01-27 20:17:43 | [diff] [blame] | 2535 | BindToCurrentLoop( |
| 2536 | base::Bind(&WebMediaPlayerImpl::OnDemuxerOpened, AsWeakPtr())), |
Dan Sanders | 42311b5 | 2017-08-10 23:41:03 | [diff] [blame] | 2537 | BindToCurrentLoop( |
| 2538 | base::Bind(&WebMediaPlayerImpl::OnProgress, AsWeakPtr())), |
dalecurtis | 9cddc0b | 2017-04-19 21:23:38 | [diff] [blame] | 2539 | encrypted_media_init_data_cb, media_log_.get()); |
John Delaney | b93339160 | 2018-10-17 21:50:47 | [diff] [blame] | 2540 | // Notify |this| of bytes that are received via MSE. |
| 2541 | chunk_demuxer_->AddBytesReceivedCallback( |
| 2542 | BindToCurrentLoop(base::BindRepeating( |
| 2543 | &WebMediaPlayerImpl::OnBytesReceived, AsWeakPtr()))); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 2544 | demuxer_.reset(chunk_demuxer_); |
servolk | f94b460 | 2017-01-31 16:44:27 | [diff] [blame] | 2545 | |
| 2546 | if (base::FeatureList::IsEnabled(kMemoryPressureBasedSourceBufferGC)) { |
| 2547 | // base::Unretained is safe because |this| owns memory_pressure_listener_. |
| 2548 | memory_pressure_listener_ = |
Gyuyoung Kim | 62a5de4 | 2018-01-10 09:48:42 | [diff] [blame] | 2549 | std::make_unique<base::MemoryPressureListener>(base::Bind( |
servolk | f94b460 | 2017-01-31 16:44:27 | [diff] [blame] | 2550 | &WebMediaPlayerImpl::OnMemoryPressure, base::Unretained(this))); |
| 2551 | } |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 2552 | } |
| 2553 | |
sandersd | b5e2146 | 2016-03-09 01:49:07 | [diff] [blame] | 2554 | // TODO(sandersd): FileSystem objects may also be non-static, but due to our |
| 2555 | // caching layer such situations are broken already. http://crbug.com/593159 |
| 2556 | bool is_static = !chunk_demuxer_; |
avayvod | 102cdb6 | 2017-01-07 03:11:09 | [diff] [blame] | 2557 | bool is_streaming = IsStreaming(); |
sandersd | b8eb5f1d | 2016-11-19 04:04:02 | [diff] [blame] | 2558 | UMA_HISTOGRAM_BOOLEAN("Media.IsStreaming", is_streaming); |
sandersd | b5e2146 | 2016-03-09 01:49:07 | [diff] [blame] | 2559 | |
Dale Curtis | 2dc6089a | 2018-03-26 16:47:58 | [diff] [blame] | 2560 | // If possible attempt to avoid decoder spool up until playback starts. |
| 2561 | Pipeline::StartType start_type = Pipeline::StartType::kNormal; |
Dale Curtis | 7c63f2e2 | 2018-09-19 21:06:27 | [diff] [blame] | 2562 | if (!chunk_demuxer_ && preload_ == MultibufferDataSource::METADATA && |
Dale Curtis | f5a7c921 | 2018-04-10 21:57:34 | [diff] [blame] | 2563 | !client_->CouldPlayIfEnoughData()) { |
Dale Curtis | 7c63f2e2 | 2018-09-19 21:06:27 | [diff] [blame] | 2564 | start_type = |
| 2565 | (has_poster_ || base::FeatureList::IsEnabled(kPreloadMetadataLazyLoad)) |
| 2566 | ? Pipeline::StartType::kSuspendAfterMetadata |
| 2567 | : Pipeline::StartType::kSuspendAfterMetadataForAudioOnly; |
Dale Curtis | 2dc6089a | 2018-03-26 16:47:58 | [diff] [blame] | 2568 | attempting_suspended_start_ = true; |
| 2569 | } |
| 2570 | |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 2571 | // ... and we're ready to go! |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 2572 | // TODO(sandersd): On Android, defer Start() if the tab is not visible. |
sandersd | b8eb5f1d | 2016-11-19 04:04:02 | [diff] [blame] | 2573 | seeking_ = true; |
Dale Curtis | 2dc6089a | 2018-03-26 16:47:58 | [diff] [blame] | 2574 | pipeline_controller_.Start(start_type, demuxer_.get(), this, is_streaming, |
| 2575 | is_static); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 2579 | DVLOG(1) << __func__ << "(" << state << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 2580 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 2581 | network_state_ = state; |
| 2582 | // Always notify to ensure client has the latest value. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2583 | client_->NetworkStateChanged(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state) { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 2587 | DVLOG(1) << __func__ << "(" << state << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 2588 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 2589 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2590 | if (state == WebMediaPlayer::kReadyStateHaveEnoughData && data_source_ && |
[email protected] | fee8a90 | 2014-06-03 13:43:36 | [diff] [blame] | 2591 | data_source_->assume_fully_buffered() && |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2592 | network_state_ == WebMediaPlayer::kNetworkStateLoading) |
| 2593 | SetNetworkState(WebMediaPlayer::kNetworkStateLoaded); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 2594 | |
| 2595 | ready_state_ = state; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2596 | highest_ready_state_ = std::max(highest_ready_state_, ready_state_); |
| 2597 | |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 2598 | // Always notify to ensure client has the latest value. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2599 | client_->ReadyStateChanged(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 2600 | } |
| 2601 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2602 | blink::WebAudioSourceProvider* WebMediaPlayerImpl::GetAudioSourceProvider() { |
[email protected] | ff875be5 | 2013-06-02 23:47:38 | [diff] [blame] | 2603 | return audio_source_provider_.get(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 2604 | } |
| 2605 | |
Jiajia Qin | 82acdc0 | 2017-07-31 09:55:14 | [diff] [blame] | 2606 | scoped_refptr<VideoFrame> WebMediaPlayerImpl::GetCurrentFrameFromCompositor() |
| 2607 | const { |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 2608 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 2609 | TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor"); |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 2610 | |
Thomas Guilbert | d8540741 | 2017-11-08 05:03:46 | [diff] [blame] | 2611 | // Can be null. |
| 2612 | scoped_refptr<VideoFrame> video_frame = |
| 2613 | compositor_->GetCurrentFrameOnAnyThread(); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 2614 | |
Thomas Guilbert | d8540741 | 2017-11-08 05:03:46 | [diff] [blame] | 2615 | // base::Unretained is safe here because |compositor_| is destroyed on |
| 2616 | // |vfc_task_runner_|. The destruction is queued from |this|' destructor, |
| 2617 | // which also runs on |main_task_runner_|, which makes it impossible for |
| 2618 | // UpdateCurrentFrameIfStale() to be queued after |compositor_|'s dtor. |
CJ DiMeglio | 2302d20 | 2017-08-31 08:38:04 | [diff] [blame] | 2619 | vfc_task_runner_->PostTask( |
Dale Curtis | ee9be3001 | 2018-09-21 22:21:51 | [diff] [blame] | 2620 | FROM_HERE, |
| 2621 | base::BindOnce(&VideoFrameCompositor::UpdateCurrentFrameIfStale, |
| 2622 | base::Unretained(compositor_.get()))); |
kainino | 36eeff8 | 2017-03-30 00:55:30 | [diff] [blame] | 2623 | |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 2624 | return video_frame; |
| 2625 | } |
| 2626 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2627 | void WebMediaPlayerImpl::UpdatePlayState() { |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 2628 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2629 | |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2630 | #if defined(OS_ANDROID) // WMPI_CAST |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2631 | bool is_remote = IsRemote(); |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 2632 | bool can_auto_suspend = true; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2633 | #else |
| 2634 | bool is_remote = false; |
hubbe | e2cc88c09 | 2017-07-14 23:10:41 | [diff] [blame] | 2635 | bool can_auto_suspend = !disable_pipeline_auto_suspend_; |
| 2636 | // For streaming videos, we only allow suspending at the very beginning of the |
| 2637 | // video, and only if we know the length of the video. (If we don't know |
| 2638 | // the length, it might be a dynamically generated video, and suspending |
| 2639 | // will not work at all.) |
| 2640 | if (IsStreaming()) { |
| 2641 | bool at_beginning = |
| 2642 | ready_state_ == WebMediaPlayer::kReadyStateHaveNothing || |
| 2643 | CurrentTime() == 0.0; |
| 2644 | if (!at_beginning || GetPipelineMediaDuration() == kInfiniteDuration) |
| 2645 | can_auto_suspend = false; |
| 2646 | } |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2647 | #endif |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 2648 | |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 2649 | bool is_suspended = pipeline_controller_.IsSuspended(); |
Luke Halliwell | 7a8a898 | 2018-07-25 01:07:05 | [diff] [blame] | 2650 | bool is_backgrounded = IsBackgroundSuspendEnabled(delegate_) && IsHidden(); |
sandersd | aaff1a65 | 2016-11-17 01:47:25 | [diff] [blame] | 2651 | PlayState state = UpdatePlayState_ComputePlayState( |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 2652 | is_remote, can_auto_suspend, is_suspended, is_backgrounded); |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2653 | SetDelegateState(state.delegate_state, state.is_idle); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2654 | SetMemoryReportingState(state.is_memory_reporting_enabled); |
| 2655 | SetSuspendState(state.is_suspended || pending_suspend_resume_cycle_); |
| 2656 | } |
dalecurtis | 5bbc487e | 2016-02-27 04:15:05 | [diff] [blame] | 2657 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2658 | void WebMediaPlayerImpl::SetDelegateState(DelegateState new_state, |
| 2659 | bool is_idle) { |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 2660 | DCHECK(delegate_); |
Dale Curtis | 779ed84 | 2018-03-10 06:20:13 | [diff] [blame] | 2661 | DVLOG(2) << __func__ << "(" << static_cast<int>(new_state) << ", " << is_idle |
| 2662 | << ")"; |
dalecurtis | 5bbc487e | 2016-02-27 04:15:05 | [diff] [blame] | 2663 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2664 | // Prevent duplicate delegate calls. |
| 2665 | // TODO(sandersd): Move this deduplication into the delegate itself. |
| 2666 | // TODO(sandersd): WebContentsObserverSanityChecker does not allow sending the |
| 2667 | // 'playing' IPC more than once in a row, even if the metadata has changed. |
| 2668 | // Figure out whether it should. |
Mounir Lamouri | 366dd847 | 2018-06-19 17:20:04 | [diff] [blame] | 2669 | // Pretend that the media has no audio if it never played unmuted. This is to |
| 2670 | // avoid any action related to audible media such as taking audio focus or |
| 2671 | // showing a media notification. To preserve a consistent experience, it does |
| 2672 | // not apply if a media was audible so the system states do not flicker |
| 2673 | // depending on whether the user muted the player. |
| 2674 | bool has_audio = HasAudio() && !client_->WasAlwaysMuted(); |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2675 | if (delegate_state_ == new_state && |
| 2676 | (delegate_state_ != DelegateState::PLAYING || |
| 2677 | delegate_has_audio_ == has_audio)) { |
| 2678 | return; |
mlamouri | 91011136 | 2016-11-04 11:28:24 | [diff] [blame] | 2679 | } |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2680 | delegate_state_ = new_state; |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2681 | delegate_has_audio_ = has_audio; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2682 | |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2683 | switch (new_state) { |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2684 | case DelegateState::GONE: |
| 2685 | delegate_->PlayerGone(delegate_id_); |
| 2686 | break; |
mlamouri | 91011136 | 2016-11-04 11:28:24 | [diff] [blame] | 2687 | case DelegateState::PLAYING: { |
peconn | 25795152 | 2017-06-09 18:24:59 | [diff] [blame] | 2688 | if (HasVideo()) |
| 2689 | delegate_->DidPlayerSizeChange(delegate_id_, NaturalSize()); |
zqzhang | 5d8eab7 | 2016-08-26 20:34:30 | [diff] [blame] | 2690 | delegate_->DidPlay( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2691 | delegate_id_, HasVideo(), has_audio, |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 2692 | media::DurationToMediaContentType(GetPipelineMediaDuration())); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2693 | break; |
mlamouri | 91011136 | 2016-11-04 11:28:24 | [diff] [blame] | 2694 | } |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2695 | case DelegateState::PAUSED: |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2696 | delegate_->DidPause(delegate_id_); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2697 | break; |
dalecurtis | 0f0097a | 2015-12-01 17:40:47 | [diff] [blame] | 2698 | } |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2699 | |
| 2700 | delegate_->SetIdle(delegate_id_, is_idle); |
dalecurtis | 0f0097a | 2015-12-01 17:40:47 | [diff] [blame] | 2701 | } |
| 2702 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2703 | void WebMediaPlayerImpl::SetMemoryReportingState( |
| 2704 | bool is_memory_reporting_enabled) { |
| 2705 | if (memory_usage_reporting_timer_.IsRunning() == |
| 2706 | is_memory_reporting_enabled) { |
hubbe | d5f3688 | 2016-01-15 22:40:37 | [diff] [blame] | 2707 | return; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2708 | } |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 2709 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2710 | if (is_memory_reporting_enabled) { |
| 2711 | memory_usage_reporting_timer_.Start(FROM_HERE, |
| 2712 | base::TimeDelta::FromSeconds(2), this, |
| 2713 | &WebMediaPlayerImpl::ReportMemoryUsage); |
| 2714 | } else { |
| 2715 | memory_usage_reporting_timer_.Stop(); |
| 2716 | ReportMemoryUsage(); |
| 2717 | } |
| 2718 | } |
| 2719 | |
| 2720 | void WebMediaPlayerImpl::SetSuspendState(bool is_suspended) { |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 2721 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Dale Curtis | 779ed84 | 2018-03-10 06:20:13 | [diff] [blame] | 2722 | DVLOG(2) << __func__ << "(" << is_suspended << ")"; |
xhwang | 213e50c | 2016-10-10 23:56:31 | [diff] [blame] | 2723 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2724 | // Do not change the state after an error has occurred. |
| 2725 | // TODO(sandersd): Update PipelineController to remove the need for this. |
| 2726 | if (IsNetworkStateError(network_state_)) |
sandersd | 1c0bba0 | 2016-03-04 23:14:08 | [diff] [blame] | 2727 | return; |
| 2728 | |
jameswest | 451a5bb | 2017-01-27 03:59:39 | [diff] [blame] | 2729 | if (is_suspended) { |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2730 | // If we were not resumed for long enough to satisfy the preroll attempt, |
| 2731 | // reset the clock. |
| 2732 | if (!preroll_attempt_pending_ && IsPrerollAttemptNeeded()) { |
| 2733 | preroll_attempt_pending_ = true; |
| 2734 | preroll_attempt_start_time_ = base::TimeTicks(); |
| 2735 | } |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2736 | pipeline_controller_.Suspend(); |
| 2737 | } else { |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2738 | // When resuming, start the preroll attempt clock. |
| 2739 | if (preroll_attempt_pending_) { |
| 2740 | preroll_attempt_pending_ = false; |
| 2741 | preroll_attempt_start_time_ = tick_clock_->NowTicks(); |
| 2742 | } |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2743 | pipeline_controller_.Resume(); |
| 2744 | } |
| 2745 | } |
| 2746 | |
| 2747 | WebMediaPlayerImpl::PlayState |
| 2748 | WebMediaPlayerImpl::UpdatePlayState_ComputePlayState(bool is_remote, |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 2749 | bool can_auto_suspend, |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 2750 | bool is_suspended, |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2751 | bool is_backgrounded) { |
| 2752 | PlayState result; |
| 2753 | |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 2754 | bool must_suspend = delegate_->IsFrameClosed(); |
Dale Curtis | 6438cf1 | 2018-03-29 02:34:01 | [diff] [blame] | 2755 | bool is_stale = delegate_->IsStale(delegate_id_); |
| 2756 | |
| 2757 | if (stale_state_override_for_testing_.has_value() && |
| 2758 | ready_state_ >= stale_state_override_for_testing_.value()) { |
| 2759 | is_stale = true; |
| 2760 | } |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2761 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2762 | // This includes both data source (before pipeline startup) and pipeline |
| 2763 | // errors. |
| 2764 | bool has_error = IsNetworkStateError(network_state_); |
| 2765 | |
dalecurtis | cc8baf7 | 2016-10-27 01:49:44 | [diff] [blame] | 2766 | // After HaveFutureData, Blink will call play() if the state is not paused; |
| 2767 | // prior to this point |paused_| is not accurate. |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2768 | bool have_future_data = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2769 | highest_ready_state_ >= WebMediaPlayer::kReadyStateHaveFutureData; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2770 | |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2771 | // Background suspend is only enabled for paused players. |
| 2772 | // In the case of players with audio the session should be kept. |
| 2773 | bool background_suspended = |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 2774 | can_auto_suspend && is_backgrounded && paused_ && have_future_data; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2775 | |
dalecurtis | cc8baf7 | 2016-10-27 01:49:44 | [diff] [blame] | 2776 | // Idle suspension is allowed prior to have future data since there exist |
| 2777 | // mechanisms to exit the idle state when the player is capable of reaching |
| 2778 | // the have future data state; see didLoadingProgress(). |
| 2779 | // |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2780 | // TODO(sandersd): Make the delegate suspend idle players immediately when |
| 2781 | // hidden. |
Dale Curtis | 7c63f2e2 | 2018-09-19 21:06:27 | [diff] [blame] | 2782 | bool idle_suspended = can_auto_suspend && is_stale && paused_ && !seeking_ && |
| 2783 | !overlay_enabled_ && !needs_first_frame_; |
dalecurtis | e7120dc | 2016-09-03 02:54:35 | [diff] [blame] | 2784 | |
| 2785 | // If we're already suspended, see if we can wait for user interaction. Prior |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2786 | // to HaveFutureData, we require |is_stale| to remain suspended. |is_stale| |
dalecurtis | e7120dc | 2016-09-03 02:54:35 | [diff] [blame] | 2787 | // will be cleared when we receive data which may take us to HaveFutureData. |
Dale Curtis | 7c63f2e2 | 2018-09-19 21:06:27 | [diff] [blame] | 2788 | bool can_stay_suspended = (is_stale || have_future_data) && is_suspended && |
| 2789 | paused_ && !seeking_ && !needs_first_frame_; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2790 | |
| 2791 | // Combined suspend state. |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2792 | result.is_suspended = is_remote || must_suspend || idle_suspended || |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2793 | background_suspended || can_stay_suspended; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2794 | |
Dale Curtis | 779ed84 | 2018-03-10 06:20:13 | [diff] [blame] | 2795 | DVLOG(3) << __func__ << ": is_remote=" << is_remote |
| 2796 | << ", must_suspend=" << must_suspend |
| 2797 | << ", idle_suspended=" << idle_suspended |
| 2798 | << ", background_suspended=" << background_suspended |
| 2799 | << ", can_stay_suspended=" << can_stay_suspended |
| 2800 | << ", is_stale=" << is_stale |
| 2801 | << ", have_future_data=" << have_future_data |
| 2802 | << ", paused_=" << paused_ << ", seeking_=" << seeking_; |
| 2803 | |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2804 | // We do not treat |playback_rate_| == 0 as paused. For the media session, |
| 2805 | // being paused implies displaying a play button, which is incorrect in this |
| 2806 | // case. For memory usage reporting, we just use the same definition (but we |
| 2807 | // don't have to). |
| 2808 | // |
| 2809 | // Similarly, we don't consider |ended_| to be paused. Blink will immediately |
| 2810 | // call pause() or seek(), so |ended_| should not affect the computation. |
| 2811 | // Despite that, |ended_| does result in a separate paused state, to simplfy |
| 2812 | // the contract for SetDelegateState(). |
| 2813 | // |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2814 | // |has_remote_controls| indicates if the player can be controlled outside the |
| 2815 | // page (e.g. via the notification controls or by audio focus events). Idle |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2816 | // suspension does not destroy the media session, because we expect that the |
avayvod | 5f34b64 | 2017-03-23 03:14:04 | [diff] [blame] | 2817 | // notification controls (and audio focus) remain. With some exceptions for |
| 2818 | // background videos, the player only needs to have audio to have controls |
| 2819 | // (requires |have_future_data|). |
| 2820 | // |
| 2821 | // |alive| indicates if the player should be present (not |GONE|) to the |
| 2822 | // delegate, either paused or playing. The following must be true for the |
| 2823 | // player: |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2824 | // - |have_future_data|, since we need to know whether we are paused to |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2825 | // correctly configure the session and also because the tracks and |
avayvod | 5f34b64 | 2017-03-23 03:14:04 | [diff] [blame] | 2826 | // duration are passed to DidPlay(), |
| 2827 | // - |is_remote| is false as remote playback is not handled by the delegate, |
| 2828 | // - |has_error| is false as player should have no errors, |
| 2829 | // - |background_suspended| is false, otherwise |has_remote_controls| must |
| 2830 | // be true. |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2831 | // |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2832 | // TODO(sandersd): If Blink told us the paused state sooner, we could detect |
| 2833 | // if the remote controls are available sooner. |
| 2834 | |
| 2835 | // Background videos with audio don't have remote controls if background |
| 2836 | // suspend is enabled and resuming background videos is not (original Android |
| 2837 | // behavior). |
| 2838 | bool backgrounded_video_has_no_remote_controls = |
Luke Halliwell | 7a8a898 | 2018-07-25 01:07:05 | [diff] [blame] | 2839 | IsBackgroundSuspendEnabled(delegate_) && |
| 2840 | !IsResumeBackgroundVideosEnabled() && is_backgrounded && HasVideo(); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2841 | bool can_play = !has_error && !is_remote && have_future_data; |
avayvod | 5f34b64 | 2017-03-23 03:14:04 | [diff] [blame] | 2842 | bool has_remote_controls = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2843 | HasAudio() && !backgrounded_video_has_no_remote_controls; |
avayvod | 5f34b64 | 2017-03-23 03:14:04 | [diff] [blame] | 2844 | bool alive = can_play && !must_suspend && |
| 2845 | (!background_suspended || has_remote_controls); |
| 2846 | if (!alive) { |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2847 | result.delegate_state = DelegateState::GONE; |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 2848 | result.is_idle = delegate_->IsIdle(delegate_id_); |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2849 | } else if (paused_) { |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2850 | // TODO(sandersd): Is it possible to have a suspended session, be ended, |
| 2851 | // and not be paused? If so we should be in a PLAYING state. |
dalecurtis | e7120dc | 2016-09-03 02:54:35 | [diff] [blame] | 2852 | result.delegate_state = |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2853 | ended_ ? DelegateState::GONE : DelegateState::PAUSED; |
| 2854 | result.is_idle = !seeking_; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2855 | } else { |
| 2856 | result.delegate_state = DelegateState::PLAYING; |
sandersd | 35d2c3f | 2017-01-14 02:04:42 | [diff] [blame] | 2857 | result.is_idle = false; |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2858 | } |
| 2859 | |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 2860 | // It's not critical if some cases where memory usage can change are missed, |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2861 | // since media memory changes are usually gradual. |
| 2862 | result.is_memory_reporting_enabled = |
Dale Curtis | 8a628132 | 2017-08-31 00:35:53 | [diff] [blame] | 2863 | !has_error && can_play && !result.is_suspended && (!paused_ || seeking_); |
sandersd | 50a635e | 2016-04-04 22:50:09 | [diff] [blame] | 2864 | |
| 2865 | return result; |
dalecurtis | 0f0097a | 2015-12-01 17:40:47 | [diff] [blame] | 2866 | } |
| 2867 | |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 2868 | void WebMediaPlayerImpl::ReportMemoryUsage() { |
| 2869 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2870 | |
wdzierzanowski | fd4cd91c5 | 2015-12-02 23:50:20 | [diff] [blame] | 2871 | // About base::Unretained() usage below: We destroy |demuxer_| on the main |
| 2872 | // thread. Before that, however, ~WebMediaPlayerImpl() posts a task to the |
| 2873 | // media thread and waits for it to finish. Hence, the GetMemoryUsage() task |
| 2874 | // posted here must finish earlier. |
Dale Curtis | 8a628132 | 2017-08-31 00:35:53 | [diff] [blame] | 2875 | // |
| 2876 | // The exception to the above is when OnError() has been called. If we're in |
| 2877 | // the error state we've already shut down the pipeline and can't rely on it |
| 2878 | // to cycle the media thread before we destroy |demuxer_|. In this case skip |
| 2879 | // collection of the demuxer memory stats. |
| 2880 | if (demuxer_ && !IsNetworkStateError(network_state_)) { |
wdzierzanowski | fd4cd91c5 | 2015-12-02 23:50:20 | [diff] [blame] | 2881 | base::PostTaskAndReplyWithResult( |
| 2882 | media_task_runner_.get(), FROM_HERE, |
| 2883 | base::Bind(&Demuxer::GetMemoryUsage, base::Unretained(demuxer_.get())), |
| 2884 | base::Bind(&WebMediaPlayerImpl::FinishMemoryUsageReport, AsWeakPtr())); |
| 2885 | } else { |
| 2886 | FinishMemoryUsageReport(0); |
| 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | void WebMediaPlayerImpl::FinishMemoryUsageReport(int64_t demuxer_memory_usage) { |
| 2891 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2892 | |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 2893 | const PipelineStatistics stats = GetPipelineStatistics(); |
servolk | 639473e49 | 2016-12-15 04:14:20 | [diff] [blame] | 2894 | const int64_t data_source_memory_usage = |
| 2895 | data_source_ ? data_source_->GetMemoryUsage() : 0; |
dalecurtis | ecc7661 | 2017-04-19 00:31:20 | [diff] [blame] | 2896 | |
Dale Curtis | c2c5dcb1 | 2018-04-16 23:21:29 | [diff] [blame] | 2897 | // If we have video and no video memory usage and we've rendered the first |
| 2898 | // frame, assume the VideoFrameCompositor is holding onto the last frame after |
| 2899 | // we've suspended the pipeline; which thus reports zero memory usage from the |
| 2900 | // video renderer. |
dalecurtis | ecc7661 | 2017-04-19 00:31:20 | [diff] [blame] | 2901 | // |
| 2902 | // Technically this should use the coded size, but that requires us to hop to |
| 2903 | // the compositor to get and byte-perfect accuracy isn't important here. |
| 2904 | const int64_t video_memory_usage = |
| 2905 | stats.video_memory_usage + |
Dale Curtis | c2c5dcb1 | 2018-04-16 23:21:29 | [diff] [blame] | 2906 | ((pipeline_metadata_.has_video && !stats.video_memory_usage && |
| 2907 | has_first_frame_) |
Miguel Casas | 9e776602 | 2018-01-08 16:13:13 | [diff] [blame] | 2908 | ? VideoFrame::AllocationSize(PIXEL_FORMAT_I420, |
dalecurtis | ecc7661 | 2017-04-19 00:31:20 | [diff] [blame] | 2909 | pipeline_metadata_.natural_size) |
| 2910 | : 0); |
| 2911 | |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 2912 | const int64_t current_memory_usage = |
dalecurtis | ecc7661 | 2017-04-19 00:31:20 | [diff] [blame] | 2913 | stats.audio_memory_usage + video_memory_usage + data_source_memory_usage + |
| 2914 | demuxer_memory_usage; |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 2915 | |
dalecurtis | ecc7661 | 2017-04-19 00:31:20 | [diff] [blame] | 2916 | DVLOG(2) << "Memory Usage -- Total: " << current_memory_usage |
| 2917 | << " Audio: " << stats.audio_memory_usage |
| 2918 | << ", Video: " << video_memory_usage |
servolk | 639473e49 | 2016-12-15 04:14:20 | [diff] [blame] | 2919 | << ", DataSource: " << data_source_memory_usage |
wdzierzanowski | fd4cd91c5 | 2015-12-02 23:50:20 | [diff] [blame] | 2920 | << ", Demuxer: " << demuxer_memory_usage; |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 2921 | |
| 2922 | const int64_t delta = current_memory_usage - last_reported_memory_usage_; |
| 2923 | last_reported_memory_usage_ = current_memory_usage; |
| 2924 | adjust_allocated_memory_cb_.Run(delta); |
servolk | 639473e49 | 2016-12-15 04:14:20 | [diff] [blame] | 2925 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2926 | if (HasAudio()) { |
servolk | 639473e49 | 2016-12-15 04:14:20 | [diff] [blame] | 2927 | UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Audio", |
| 2928 | stats.audio_memory_usage / 1024); |
| 2929 | } |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2930 | if (HasVideo()) { |
servolk | 639473e49 | 2016-12-15 04:14:20 | [diff] [blame] | 2931 | UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Video", |
dalecurtis | ecc7661 | 2017-04-19 00:31:20 | [diff] [blame] | 2932 | video_memory_usage / 1024); |
servolk | 639473e49 | 2016-12-15 04:14:20 | [diff] [blame] | 2933 | } |
| 2934 | if (data_source_) { |
| 2935 | UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.DataSource", |
| 2936 | data_source_memory_usage / 1024); |
| 2937 | } |
| 2938 | if (demuxer_) { |
| 2939 | UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Demuxer", |
| 2940 | demuxer_memory_usage / 1024); |
| 2941 | } |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 2942 | } |
| 2943 | |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 2944 | void WebMediaPlayerImpl::ScheduleIdlePauseTimer() { |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 2945 | // 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] | 2946 | // resume when foregrounded, and are suspended and have audio. |
| 2947 | if ((paused_ && !paused_when_hidden_) || |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2948 | !pipeline_controller_.IsSuspended() || !HasAudio()) { |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 2949 | return; |
avayvod | 52efd28 | 2017-03-07 21:13:04 | [diff] [blame] | 2950 | } |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 2951 | |
| 2952 | #if defined(OS_ANDROID) |
| 2953 | // Remote players will be suspended and locally paused. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 2954 | if (IsRemote()) |
dalecurtis | 8b8505e7 | 2016-06-10 21:59:17 | [diff] [blame] | 2955 | return; |
| 2956 | #endif |
| 2957 | |
| 2958 | // Idle timeout chosen arbitrarily. |
| 2959 | background_pause_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(5), |
| 2960 | this, &WebMediaPlayerImpl::OnPause); |
| 2961 | } |
| 2962 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 2963 | void WebMediaPlayerImpl::CreateWatchTimeReporter() { |
wdzierzanowski | a78fa9b | 2017-06-13 18:12:10 | [diff] [blame] | 2964 | if (!HasVideo() && !HasAudio()) |
| 2965 | return; |
| 2966 | |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 2967 | // Create the watch time reporter and synchronize its initial state. |
Dale Curtis | 1adbe6a | 2017-08-02 02:09:13 | [diff] [blame] | 2968 | watch_time_reporter_.reset(new WatchTimeReporter( |
Dale Curtis | 96d6e938 | 2018-07-18 18:01:07 | [diff] [blame] | 2969 | mojom::PlaybackProperties::New(pipeline_metadata_.has_audio, |
| 2970 | pipeline_metadata_.has_video, false, false, |
| 2971 | !!chunk_demuxer_, is_encrypted_, |
| 2972 | embedded_media_experience_enabled_), |
| 2973 | pipeline_metadata_.natural_size, |
Dale Curtis | 051fdf6 | 2017-08-05 00:21:13 | [diff] [blame] | 2974 | base::BindRepeating(&WebMediaPlayerImpl::GetCurrentTimeInternal, |
| 2975 | base::Unretained(this)), |
Hajime Hoshi | 8bb37dc | 2017-12-19 09:35:10 | [diff] [blame] | 2976 | media_metrics_provider_.get(), |
Hajime Hoshi | b5a26ee | 2018-05-14 12:47:51 | [diff] [blame] | 2977 | frame_->GetTaskRunner(blink::TaskType::kInternalMedia))); |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 2978 | watch_time_reporter_->OnVolumeChange(volume_); |
Dale Curtis | aebaeea | 2018-07-19 23:42:11 | [diff] [blame] | 2979 | watch_time_reporter_->OnDurationChanged(GetPipelineMediaDuration()); |
Mounir Lamouri | f9af74e7 | 2017-06-19 19:31:45 | [diff] [blame] | 2980 | |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 2981 | if (delegate_->IsFrameHidden()) |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 2982 | watch_time_reporter_->OnHidden(); |
| 2983 | else |
| 2984 | watch_time_reporter_->OnShown(); |
Mounir Lamouri | f9af74e7 | 2017-06-19 19:31:45 | [diff] [blame] | 2985 | |
Mounir Lamouri | 41a79c6 | 2017-06-06 12:53:16 | [diff] [blame] | 2986 | if (client_->HasNativeControls()) |
| 2987 | watch_time_reporter_->OnNativeControlsEnabled(); |
| 2988 | else |
| 2989 | watch_time_reporter_->OnNativeControlsDisabled(); |
Mounir Lamouri | f9af74e7 | 2017-06-19 19:31:45 | [diff] [blame] | 2990 | |
| 2991 | switch (client_->DisplayType()) { |
| 2992 | case WebMediaPlayer::DisplayType::kInline: |
| 2993 | watch_time_reporter_->OnDisplayTypeInline(); |
| 2994 | break; |
| 2995 | case WebMediaPlayer::DisplayType::kFullscreen: |
| 2996 | watch_time_reporter_->OnDisplayTypeFullscreen(); |
| 2997 | break; |
| 2998 | case WebMediaPlayer::DisplayType::kPictureInPicture: |
| 2999 | watch_time_reporter_->OnDisplayTypePictureInPicture(); |
| 3000 | break; |
| 3001 | } |
Dale Curtis | 96d6e938 | 2018-07-18 18:01:07 | [diff] [blame] | 3002 | |
| 3003 | UpdateSecondaryProperties(); |
Dale Curtis | 7fd27c4b | 2018-07-30 22:14:21 | [diff] [blame] | 3004 | |
| 3005 | // If the WatchTimeReporter was recreated in the middle of playback, we want |
| 3006 | // to resume playback here too since we won't get another play() call. When |
| 3007 | // seeking, the seek completion will restart it if necessary. |
| 3008 | if (!paused_ && !seeking_) |
| 3009 | watch_time_reporter_->OnPlaying(); |
Dale Curtis | 96d6e938 | 2018-07-18 18:01:07 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | void WebMediaPlayerImpl::UpdateSecondaryProperties() { |
| 3013 | watch_time_reporter_->UpdateSecondaryProperties( |
| 3014 | mojom::SecondaryPlaybackProperties::New( |
| 3015 | pipeline_metadata_.audio_decoder_config.codec(), |
| 3016 | pipeline_metadata_.video_decoder_config.codec(), audio_decoder_name_, |
John Rummell | d3055535 | 2018-09-21 20:47:25 | [diff] [blame] | 3017 | video_decoder_name_, |
| 3018 | DetermineEncryptionMode( |
| 3019 | pipeline_metadata_.audio_decoder_config.encryption_scheme()), |
| 3020 | DetermineEncryptionMode( |
| 3021 | pipeline_metadata_.video_decoder_config.encryption_scheme()), |
| 3022 | pipeline_metadata_.natural_size)); |
dalecurtis | 04bdb58 | 2016-08-17 22:15:23 | [diff] [blame] | 3023 | } |
| 3024 | |
avayvod | 39c10240 | 2016-11-23 21:43:13 | [diff] [blame] | 3025 | bool WebMediaPlayerImpl::IsHidden() const { |
| 3026 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 3027 | |
tguilbert | 1bb1c78 | 2017-01-23 21:15:11 | [diff] [blame] | 3028 | return delegate_->IsFrameHidden() && !delegate_->IsFrameClosed(); |
avayvod | 39c10240 | 2016-11-23 21:43:13 | [diff] [blame] | 3029 | } |
| 3030 | |
avayvod | 102cdb6 | 2017-01-07 03:11:09 | [diff] [blame] | 3031 | bool WebMediaPlayerImpl::IsStreaming() const { |
| 3032 | return data_source_ && data_source_->IsStreaming(); |
| 3033 | } |
| 3034 | |
liberato | 2fd111be | 2017-01-04 00:25:06 | [diff] [blame] | 3035 | bool WebMediaPlayerImpl::DoesOverlaySupportMetadata() const { |
Julien Isorce | 6c83d8de | 2017-10-12 13:11:29 | [diff] [blame] | 3036 | return pipeline_metadata_.video_decoder_config.video_rotation() == |
| 3037 | VIDEO_ROTATION_0; |
liberato | 2fd111be | 2017-01-04 00:25:06 | [diff] [blame] | 3038 | } |
| 3039 | |
xjz | af29d418 | 2016-12-16 01:52:32 | [diff] [blame] | 3040 | void WebMediaPlayerImpl::ActivateViewportIntersectionMonitoring(bool activate) { |
| 3041 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 3042 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 3043 | client_->ActivateViewportIntersectionMonitoring(activate); |
xjz | af29d418 | 2016-12-16 01:52:32 | [diff] [blame] | 3044 | } |
| 3045 | |
Anton Vayvod | 09fa66e | 2017-07-20 23:02:12 | [diff] [blame] | 3046 | void WebMediaPlayerImpl::UpdateRemotePlaybackCompatibility(bool is_compatible) { |
| 3047 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 3048 | |
| 3049 | client_->RemotePlaybackCompatibilityChanged(loaded_url_, is_compatible); |
| 3050 | } |
| 3051 | |
Dale Curtis | 6438cf1 | 2018-03-29 02:34:01 | [diff] [blame] | 3052 | void WebMediaPlayerImpl::ForceStaleStateForTesting(ReadyState target_state) { |
| 3053 | stale_state_override_for_testing_.emplace(target_state); |
Dale Curtis | 99a9b48 | 2018-02-01 02:23:28 | [diff] [blame] | 3054 | UpdatePlayState(); |
| 3055 | } |
| 3056 | |
| 3057 | bool WebMediaPlayerImpl::IsSuspendedForTesting() { |
| 3058 | // This intentionally uses IsPipelineSuspended since we need to know when the |
| 3059 | // pipeline has reached the suspended state, not when it's in suspending. |
| 3060 | return pipeline_controller_.IsPipelineSuspended(); |
| 3061 | } |
| 3062 | |
Dale Curtis | 7c63f2e2 | 2018-09-19 21:06:27 | [diff] [blame] | 3063 | bool WebMediaPlayerImpl::DidLazyLoad() const { |
| 3064 | return did_lazy_load_; |
| 3065 | } |
| 3066 | |
| 3067 | void WebMediaPlayerImpl::OnBecameVisible() { |
| 3068 | needs_first_frame_ = !has_first_frame_; |
| 3069 | UpdatePlayState(); |
| 3070 | } |
| 3071 | |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 3072 | bool WebMediaPlayerImpl::ShouldPauseVideoWhenHidden() const { |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 3073 | // If suspending background video, pause any video that's not remoted or |
| 3074 | // not unlocked to play in the background. |
Luke Halliwell | 7a8a898 | 2018-07-25 01:07:05 | [diff] [blame] | 3075 | if (IsBackgroundSuspendEnabled(delegate_)) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 3076 | if (!HasVideo()) |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 3077 | return false; |
| 3078 | |
| 3079 | #if defined(OS_ANDROID) |
Thomas Guilbert | 29ae1a90 | 2018-10-20 01:53:38 | [diff] [blame] | 3080 | if (IsRemote() || is_flinging_) |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 3081 | return false; |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 3082 | #endif |
avayvod | eb9098d | 2017-01-07 00:33:03 | [diff] [blame] | 3083 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 3084 | return !HasAudio() || (IsResumeBackgroundVideosEnabled() && |
| 3085 | video_locked_when_paused_when_hidden_); |
avayvod | 65fad27 | 2017-02-24 01:00:48 | [diff] [blame] | 3086 | } |
| 3087 | |
| 3088 | // Otherwise only pause if the optimization is on and it's a video-only |
| 3089 | // optimization candidate. |
avayvod | 0120133 | 2017-04-14 00:27:15 | [diff] [blame] | 3090 | return IsBackgroundVideoPauseOptimizationEnabled() && !HasAudio() && |
Thomas Guilbert | 29ae1a90 | 2018-10-20 01:53:38 | [diff] [blame] | 3091 | IsBackgroundOptimizationCandidate() && !is_flinging_; |
avayvod | eb9098d | 2017-01-07 00:33:03 | [diff] [blame] | 3092 | } |
| 3093 | |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 3094 | bool WebMediaPlayerImpl::ShouldDisableVideoWhenHidden() const { |
Ted Meyer | a918e59 | 2018-09-08 00:16:20 | [diff] [blame] | 3095 | // This optimization is behind the flag on all platforms, only for non-mse |
| 3096 | // video. MSE video track switching on hide has gone through a field test. |
| 3097 | // TODO(tmathmeyer): Passing load_type_ won't be needed after src= field |
| 3098 | // testing is finished. see: http://crbug.com/709302 |
| 3099 | if (!IsBackgroundVideoTrackOptimizationEnabled(load_type_)) |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 3100 | return false; |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 3101 | |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 3102 | // Disable video track only for players with audio that match the criteria for |
| 3103 | // being optimized. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 3104 | return HasAudio() && IsBackgroundOptimizationCandidate(); |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 3105 | } |
| 3106 | |
| 3107 | bool WebMediaPlayerImpl::IsBackgroundOptimizationCandidate() const { |
| 3108 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 3109 | |
François Beaufort | 664c3ca7 | 2018-04-13 07:24:51 | [diff] [blame] | 3110 | // Don't optimize Picture-in-Picture players. |
Mounir Lamouri | 0484f40a | 2018-07-25 03:03:26 | [diff] [blame] | 3111 | if (IsInPictureInPicture()) |
François Beaufort | 664c3ca7 | 2018-04-13 07:24:51 | [diff] [blame] | 3112 | return false; |
| 3113 | |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 3114 | #if defined(OS_ANDROID) // WMPI_CAST |
avayvod | ac1a852 | 2017-01-20 19:02:50 | [diff] [blame] | 3115 | // Don't optimize players being Cast. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 3116 | if (IsRemote()) |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 3117 | return false; |
| 3118 | #endif // defined(OS_ANDROID) |
| 3119 | |
| 3120 | // Don't optimize audio-only or streaming players. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 3121 | if (!HasVideo() || IsStreaming()) |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 3122 | return false; |
| 3123 | |
Dale Curtis | 3f4935b | 2017-09-09 00:11:59 | [diff] [blame] | 3124 | // Video-only players are always optimized (paused). |
| 3125 | // Don't check the keyframe distance and duration. |
| 3126 | if (!HasAudio() && HasVideo()) |
| 3127 | return true; |
| 3128 | |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 3129 | // Videos shorter than the maximum allowed keyframe distance can be optimized. |
| 3130 | base::TimeDelta duration = GetPipelineMediaDuration(); |
Ted Meyer | a918e59 | 2018-09-08 00:16:20 | [diff] [blame] | 3131 | |
Dale Curtis | 456808a | 2018-10-23 17:50:21 | [diff] [blame] | 3132 | constexpr base::TimeDelta kMaxKeyframeDistanceToDisableBackgroundVideo = |
| 3133 | base::TimeDelta::FromMilliseconds( |
| 3134 | kMaxKeyframeDistanceToDisableBackgroundVideoMs); |
| 3135 | if (duration < kMaxKeyframeDistanceToDisableBackgroundVideo) |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 3136 | return true; |
| 3137 | |
| 3138 | // Otherwise, only optimize videos with shorter average keyframe distance. |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 3139 | PipelineStatistics stats = GetPipelineStatistics(); |
Dale Curtis | 456808a | 2018-10-23 17:50:21 | [diff] [blame] | 3140 | return stats.video_keyframe_distance_average < |
| 3141 | kMaxKeyframeDistanceToDisableBackgroundVideo; |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 3142 | } |
| 3143 | |
avayvod | 56e1f394 | 2017-01-21 02:06:31 | [diff] [blame] | 3144 | void WebMediaPlayerImpl::UpdateBackgroundVideoOptimizationState() { |
| 3145 | if (IsHidden()) { |
Dale Curtis | a75a789 | 2017-08-09 20:21:51 | [diff] [blame] | 3146 | if (ShouldPauseVideoWhenHidden()) { |
avayvod | 56e1f394 | 2017-01-21 02:06:31 | [diff] [blame] | 3147 | PauseVideoIfNeeded(); |
Dale Curtis | a75a789 | 2017-08-09 20:21:51 | [diff] [blame] | 3148 | } else if (update_background_status_cb_.IsCancelled()) { |
| 3149 | // Only trigger updates when we don't have one already scheduled. |
| 3150 | update_background_status_cb_.Reset( |
| 3151 | base::Bind(&WebMediaPlayerImpl::DisableVideoTrackIfNeeded, |
| 3152 | base::Unretained(this))); |
| 3153 | |
| 3154 | // Defer disable track until we're sure the clip will be backgrounded for |
| 3155 | // some time. Resuming may take half a second, so frequent tab switches |
| 3156 | // will yield a poor user experience otherwise. http://crbug.com/709302 |
| 3157 | // may also cause AV sync issues if disable/enable happens too fast. |
| 3158 | main_task_runner_->PostDelayedTask( |
| 3159 | FROM_HERE, update_background_status_cb_.callback(), |
| 3160 | base::TimeDelta::FromSeconds(10)); |
| 3161 | } |
avayvod | 56e1f394 | 2017-01-21 02:06:31 | [diff] [blame] | 3162 | } else { |
Dale Curtis | a75a789 | 2017-08-09 20:21:51 | [diff] [blame] | 3163 | update_background_status_cb_.Cancel(); |
avayvod | 56e1f394 | 2017-01-21 02:06:31 | [diff] [blame] | 3164 | EnableVideoTrackIfNeeded(); |
| 3165 | } |
| 3166 | } |
| 3167 | |
| 3168 | void WebMediaPlayerImpl::PauseVideoIfNeeded() { |
| 3169 | DCHECK(IsHidden()); |
| 3170 | |
| 3171 | // Don't pause video while the pipeline is stopped, resuming or seeking. |
| 3172 | // Also if the video is paused already. |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 3173 | if (!pipeline_controller_.IsPipelineRunning() || is_pipeline_resuming_ || |
| 3174 | seeking_ || paused_) |
avayvod | 56e1f394 | 2017-01-21 02:06:31 | [diff] [blame] | 3175 | return; |
| 3176 | |
| 3177 | // OnPause() will set |paused_when_hidden_| to false and call |
| 3178 | // UpdatePlayState(), so set the flag to true after and then return. |
| 3179 | OnPause(); |
| 3180 | paused_when_hidden_ = true; |
| 3181 | } |
| 3182 | |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 3183 | void WebMediaPlayerImpl::EnableVideoTrackIfNeeded() { |
avayvod | 56e1f394 | 2017-01-21 02:06:31 | [diff] [blame] | 3184 | // Don't change video track while the pipeline is stopped, resuming or |
| 3185 | // seeking. |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 3186 | if (!pipeline_controller_.IsPipelineRunning() || is_pipeline_resuming_ || |
| 3187 | seeking_) |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 3188 | return; |
| 3189 | |
| 3190 | if (video_track_disabled_) { |
| 3191 | video_track_disabled_ = false; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 3192 | if (client_->HasSelectedVideoTrack()) { |
| 3193 | WebMediaPlayer::TrackId trackId = client_->GetSelectedVideoTrackId(); |
| 3194 | SelectedVideoTrackChanged(&trackId); |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 3195 | } |
| 3196 | } |
| 3197 | } |
| 3198 | |
| 3199 | void WebMediaPlayerImpl::DisableVideoTrackIfNeeded() { |
| 3200 | DCHECK(IsHidden()); |
| 3201 | |
| 3202 | // Don't change video track while the pipeline is resuming or seeking. |
| 3203 | if (is_pipeline_resuming_ || seeking_) |
| 3204 | return; |
| 3205 | |
| 3206 | if (!video_track_disabled_ && ShouldDisableVideoWhenHidden()) { |
| 3207 | video_track_disabled_ = true; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 3208 | SelectedVideoTrackChanged(nullptr); |
avayvod | 2135a64 | 2017-01-13 00:17:14 | [diff] [blame] | 3209 | } |
| 3210 | } |
| 3211 | |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 3212 | void WebMediaPlayerImpl::SetPipelineStatisticsForTest( |
| 3213 | const PipelineStatistics& stats) { |
| 3214 | pipeline_statistics_for_test_ = base::make_optional(stats); |
| 3215 | } |
| 3216 | |
| 3217 | PipelineStatistics WebMediaPlayerImpl::GetPipelineStatistics() const { |
| 3218 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 3219 | |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 3220 | return pipeline_statistics_for_test_.value_or( |
| 3221 | pipeline_controller_.GetStatistics()); |
avayvod | c4bfb0e6 | 2017-01-13 01:07:01 | [diff] [blame] | 3222 | } |
| 3223 | |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 3224 | void WebMediaPlayerImpl::SetPipelineMediaDurationForTest( |
| 3225 | base::TimeDelta duration) { |
| 3226 | pipeline_media_duration_for_test_ = base::make_optional(duration); |
| 3227 | } |
| 3228 | |
| 3229 | base::TimeDelta WebMediaPlayerImpl::GetPipelineMediaDuration() const { |
| 3230 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 3231 | |
| 3232 | return pipeline_media_duration_for_test_.value_or( |
tguilbert | 350936ff | 2017-02-24 05:39:27 | [diff] [blame] | 3233 | pipeline_controller_.GetMediaDuration()); |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | void WebMediaPlayerImpl::ReportTimeFromForegroundToFirstFrame( |
| 3237 | base::TimeTicks foreground_time, |
| 3238 | base::TimeTicks new_frame_time) { |
| 3239 | base::TimeDelta time_to_first_frame = new_frame_time - foreground_time; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 3240 | if (HasAudio()) { |
avayvod | cc273dd | 2017-01-19 19:35:12 | [diff] [blame] | 3241 | UMA_HISTOGRAM_TIMES( |
| 3242 | "Media.Video.TimeFromForegroundToFirstFrame.DisableTrack", |
| 3243 | time_to_first_frame); |
| 3244 | } else { |
| 3245 | UMA_HISTOGRAM_TIMES("Media.Video.TimeFromForegroundToFirstFrame.Paused", |
| 3246 | time_to_first_frame); |
| 3247 | } |
| 3248 | } |
Xiangjun Zhang | ba8724f48 | 2017-08-03 06:43:25 | [diff] [blame] | 3249 | |
| 3250 | void WebMediaPlayerImpl::SwitchToRemoteRenderer( |
| 3251 | const std::string& remote_device_friendly_name) { |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 3252 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Thomas Guilbert | b341bae0 | 2018-05-09 00:02:13 | [diff] [blame] | 3253 | DCHECK(!disable_pipeline_auto_suspend_); |
Xiangjun Zhang | ba8724f48 | 2017-08-03 06:43:25 | [diff] [blame] | 3254 | disable_pipeline_auto_suspend_ = true; |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 3255 | |
| 3256 | // Capabilities reporting should only be performed for local playbacks. |
| 3257 | video_decode_stats_reporter_.reset(); |
| 3258 | |
Xiangjun Zhang | ba8724f48 | 2017-08-03 06:43:25 | [diff] [blame] | 3259 | // Requests to restart media pipeline. A remote renderer will be created via |
| 3260 | // the |renderer_factory_selector_|. |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 3261 | ScheduleRestart(); |
xjz | df9b67e | 2017-04-13 23:28:25 | [diff] [blame] | 3262 | if (client_) { |
Xiangjun Zhang | ba8724f48 | 2017-08-03 06:43:25 | [diff] [blame] | 3263 | client_->MediaRemotingStarted( |
| 3264 | WebString::FromUTF8(remote_device_friendly_name)); |
xjz | df9b67e | 2017-04-13 23:28:25 | [diff] [blame] | 3265 | } |
xjz | 4e5d4bf3 | 2017-02-15 21:26:35 | [diff] [blame] | 3266 | } |
| 3267 | |
Xiangjun Zhang | 5e20cba4 | 2018-01-10 19:54:56 | [diff] [blame] | 3268 | void WebMediaPlayerImpl::SwitchToLocalRenderer( |
| 3269 | MediaObserverClient::ReasonToSwitchToLocal reason) { |
Xiangjun Zhang | ba8724f48 | 2017-08-03 06:43:25 | [diff] [blame] | 3270 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
Xiangjun Zhang | 12f5527 | 2018-07-30 23:19:37 | [diff] [blame] | 3271 | if (!disable_pipeline_auto_suspend_) |
| 3272 | return; // Is currently with local renderer. |
Xiangjun Zhang | ba8724f48 | 2017-08-03 06:43:25 | [diff] [blame] | 3273 | disable_pipeline_auto_suspend_ = false; |
Chris Cunningham | d9df58e | 2017-08-29 00:04:23 | [diff] [blame] | 3274 | |
| 3275 | // Capabilities reporting may resume now that playback is local. |
| 3276 | CreateVideoDecodeStatsReporter(); |
| 3277 | |
Xiangjun Zhang | ba8724f48 | 2017-08-03 06:43:25 | [diff] [blame] | 3278 | // Requests to restart media pipeline. A local renderer will be created via |
| 3279 | // the |renderer_factory_selector_|. |
| 3280 | ScheduleRestart(); |
| 3281 | if (client_) |
Xiangjun Zhang | 5e20cba4 | 2018-01-10 19:54:56 | [diff] [blame] | 3282 | client_->MediaRemotingStopped(GetSwitchToLocalMessage(reason)); |
Xiangjun Zhang | ba8724f48 | 2017-08-03 06:43:25 | [diff] [blame] | 3283 | } |
| 3284 | |
dalecurtis | 4f6d14d | 2017-02-22 17:42:22 | [diff] [blame] | 3285 | void WebMediaPlayerImpl::RecordUnderflowDuration(base::TimeDelta duration) { |
| 3286 | DCHECK(data_source_ || chunk_demuxer_); |
xhwang | 68b3fab8 | 2017-05-17 21:31:46 | [diff] [blame] | 3287 | |
dalecurtis | 4f6d14d | 2017-02-22 17:42:22 | [diff] [blame] | 3288 | if (data_source_) |
Jennifer Apacible | 82e25c9 | 2017-08-07 18:15:27 | [diff] [blame] | 3289 | UMA_HISTOGRAM_TIMES("Media.UnderflowDuration2.SRC", duration); |
dalecurtis | 4f6d14d | 2017-02-22 17:42:22 | [diff] [blame] | 3290 | else |
Jennifer Apacible | 82e25c9 | 2017-08-07 18:15:27 | [diff] [blame] | 3291 | UMA_HISTOGRAM_TIMES("Media.UnderflowDuration2.MSE", duration); |
xhwang | 68b3fab8 | 2017-05-17 21:31:46 | [diff] [blame] | 3292 | |
| 3293 | if (is_encrypted_) |
Jennifer Apacible | 82e25c9 | 2017-08-07 18:15:27 | [diff] [blame] | 3294 | UMA_HISTOGRAM_TIMES("Media.UnderflowDuration2.EME", duration); |
dalecurtis | 4f6d14d | 2017-02-22 17:42:22 | [diff] [blame] | 3295 | } |
| 3296 | |
xhwang | 6080265 | 2017-04-19 07:29:58 | [diff] [blame] | 3297 | #define UMA_HISTOGRAM_VIDEO_HEIGHT(name, sample) \ |
| 3298 | UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, 100, 10000, 50) |
| 3299 | |
| 3300 | void WebMediaPlayerImpl::RecordVideoNaturalSize(const gfx::Size& natural_size) { |
| 3301 | // Always report video natural size to MediaLog. |
| 3302 | media_log_->AddEvent(media_log_->CreateVideoSizeSetEvent( |
| 3303 | natural_size.width(), natural_size.height())); |
| 3304 | |
| 3305 | if (initial_video_height_recorded_) |
| 3306 | return; |
| 3307 | |
| 3308 | initial_video_height_recorded_ = true; |
| 3309 | |
| 3310 | int height = natural_size.height(); |
| 3311 | |
| 3312 | if (load_type_ == kLoadTypeURL) |
| 3313 | UMA_HISTOGRAM_VIDEO_HEIGHT("Media.VideoHeight.Initial.SRC", height); |
| 3314 | else if (load_type_ == kLoadTypeMediaSource) |
| 3315 | UMA_HISTOGRAM_VIDEO_HEIGHT("Media.VideoHeight.Initial.MSE", height); |
| 3316 | |
| 3317 | if (is_encrypted_) |
| 3318 | UMA_HISTOGRAM_VIDEO_HEIGHT("Media.VideoHeight.Initial.EME", height); |
| 3319 | |
| 3320 | UMA_HISTOGRAM_VIDEO_HEIGHT("Media.VideoHeight.Initial.All", height); |
| 3321 | } |
| 3322 | |
| 3323 | #undef UMA_HISTOGRAM_VIDEO_HEIGHT |
| 3324 | |
Greg Thompson | aa48ce8d | 2018-04-03 06:11:43 | [diff] [blame] | 3325 | void WebMediaPlayerImpl::SetTickClockForTest( |
| 3326 | const base::TickClock* tick_clock) { |
tzik | 2c963b87 | 2017-12-07 06:57:24 | [diff] [blame] | 3327 | tick_clock_ = tick_clock; |
hubbe | b2d3efd | 2017-05-05 23:26:38 | [diff] [blame] | 3328 | buffered_data_source_host_.SetTickClockForTest(tick_clock); |
| 3329 | } |
| 3330 | |
Dale Curtis | 3899090ea | 2018-01-12 00:10:35 | [diff] [blame] | 3331 | void WebMediaPlayerImpl::OnFirstFrame(base::TimeTicks frame_time) { |
| 3332 | DCHECK(!load_start_time_.is_null()); |
Dale Curtis | ff57655 | 2018-03-30 02:32:44 | [diff] [blame] | 3333 | DCHECK(!skip_metrics_due_to_startup_suspend_); |
Dale Curtis | c2c5dcb1 | 2018-04-16 23:21:29 | [diff] [blame] | 3334 | has_first_frame_ = true; |
Dale Curtis | 7c63f2e2 | 2018-09-19 21:06:27 | [diff] [blame] | 3335 | needs_first_frame_ = false; |
Dale Curtis | 3899090ea | 2018-01-12 00:10:35 | [diff] [blame] | 3336 | const base::TimeDelta elapsed = frame_time - load_start_time_; |
| 3337 | media_metrics_provider_->SetTimeToFirstFrame(elapsed); |
| 3338 | RecordTimingUMA("Media.TimeToFirstFrame", elapsed); |
| 3339 | } |
| 3340 | |
| 3341 | void WebMediaPlayerImpl::RecordTimingUMA(const std::string& key, |
| 3342 | base::TimeDelta elapsed) { |
| 3343 | if (chunk_demuxer_) |
| 3344 | base::UmaHistogramMediumTimes(key + ".MSE", elapsed); |
| 3345 | else |
| 3346 | base::UmaHistogramMediumTimes(key + ".SRC", elapsed); |
| 3347 | if (is_encrypted_) |
| 3348 | base::UmaHistogramMediumTimes(key + ".EME", elapsed); |
| 3349 | } |
| 3350 | |
John Rummell | db5a7ef | 2018-05-16 00:28:01 | [diff] [blame] | 3351 | void WebMediaPlayerImpl::RecordEncryptionScheme( |
| 3352 | const std::string& stream_name, |
| 3353 | const EncryptionScheme& encryption_scheme) { |
| 3354 | DCHECK(stream_name == "Audio" || stream_name == "Video"); |
| 3355 | |
| 3356 | // If the stream is not encrypted, don't record it. |
| 3357 | if (encryption_scheme.mode() == EncryptionScheme::CIPHER_MODE_UNENCRYPTED) |
| 3358 | return; |
| 3359 | |
| 3360 | base::UmaHistogramEnumeration( |
| 3361 | "Media.EME.EncryptionScheme.Initial." + stream_name, |
| 3362 | DetermineEncryptionSchemeUMAValue(encryption_scheme), |
| 3363 | EncryptionSchemeUMA::kCount); |
| 3364 | } |
| 3365 | |
Mounir Lamouri | 0484f40a | 2018-07-25 03:03:26 | [diff] [blame] | 3366 | bool WebMediaPlayerImpl::IsInPictureInPicture() const { |
| 3367 | DCHECK(client_); |
| 3368 | return client_->DisplayType() == |
| 3369 | WebMediaPlayer::DisplayType::kPictureInPicture; |
| 3370 | } |
| 3371 | |
Dale Curtis | 5bba0323 | 2018-08-30 17:57:38 | [diff] [blame] | 3372 | void WebMediaPlayerImpl::MaybeSetContainerName() { |
Dale Curtis | f01c826 | 2018-09-04 23:50:43 | [diff] [blame] | 3373 | // MSE nor MediaPlayerRenderer provide container information. |
| 3374 | if (chunk_demuxer_ || using_media_player_renderer_) |
Dale Curtis | 5bba0323 | 2018-08-30 17:57:38 | [diff] [blame] | 3375 | return; |
| 3376 | |
| 3377 | // Pipeline startup failed before even getting a demuxer setup. |
| 3378 | if (!demuxer_) |
| 3379 | return; |
| 3380 | |
| 3381 | // Container has already been set. |
| 3382 | if (highest_ready_state_ >= WebMediaPlayer::kReadyStateHaveMetadata) |
| 3383 | return; |
| 3384 | |
| 3385 | // If ffmpeg isn't enabled, we can't get the container name. |
| 3386 | #if BUILDFLAG(ENABLE_FFMPEG) |
| 3387 | media_metrics_provider_->SetContainerName( |
| 3388 | static_cast<FFmpegDemuxer*>(demuxer_.get())->container()); |
| 3389 | #endif |
| 3390 | } |
| 3391 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 3392 | } // namespace media |