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