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