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