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