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