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