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