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