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