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