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