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