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