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