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