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