[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" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 14 | #include "base/callback.h" |
[email protected] | 7502ef1 | 2014-01-25 01:19:27 | [diff] [blame] | 15 | #include "base/callback_helpers.h" |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 16 | #include "base/command_line.h" |
[email protected] | ca04095f | 2014-02-07 10:23:56 | [diff] [blame] | 17 | #include "base/debug/alias.h" |
[email protected] | 926f8fd | 2013-04-12 20:27:53 | [diff] [blame] | 18 | #include "base/debug/crash_logging.h" |
[email protected] | b0b258f | 2011-11-08 00:34:23 | [diff] [blame] | 19 | #include "base/metrics/histogram.h" |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 20 | #include "base/single_thread_task_runner.h" |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 21 | #include "base/synchronization/waitable_event.h" |
wdzierzanowski | fd4cd91c5 | 2015-12-02 23:50:20 | [diff] [blame] | 22 | #include "base/task_runner_util.h" |
anujk.sharma | 2fa37a0 | 2015-04-30 05:51:32 | [diff] [blame] | 23 | #include "base/thread_task_runner_handle.h" |
ssid | 9525f467 | 2015-01-28 12:13:15 | [diff] [blame] | 24 | #include "base/trace_event/trace_event.h" |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 25 | #include "build/build_config.h" |
[email protected] | 3856462 | 2014-08-19 02:47:18 | [diff] [blame] | 26 | #include "cc/blink/web_layer_impl.h" |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 27 | #include "cc/layers/video_layer.h" |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 28 | #include "gpu/blink/webgraphicscontext3d_impl.h" |
[email protected] | e4fc09e | 2012-04-06 03:17:44 | [diff] [blame] | 29 | #include "media/audio/null_audio_sink.h" |
[email protected] | 2eccbed | 2014-01-10 05:15:53 | [diff] [blame] | 30 | #include "media/base/bind_to_current_loop.h" |
xhwang | 0ad11e51 | 2014-11-25 23:43:09 | [diff] [blame] | 31 | #include "media/base/cdm_context.h" |
[email protected] | 32da100 | 2010-03-03 21:57:35 | [diff] [blame] | 32 | #include "media/base/limits.h" |
[email protected] | 090f731 | 2011-08-05 23:26:40 | [diff] [blame] | 33 | #include "media/base/media_log.h" |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 34 | #include "media/base/media_switches.h" |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 35 | #include "media/base/text_renderer.h" |
watk | 9f9dfdc9 | 2015-09-04 21:33:29 | [diff] [blame] | 36 | #include "media/base/timestamp_constants.h" |
[email protected] | e81283bb | 2010-08-31 18:01:21 | [diff] [blame] | 37 | #include "media/base/video_frame.h" |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 38 | #include "media/blink/texttrack_impl.h" |
| 39 | #include "media/blink/webaudiosourceprovider_impl.h" |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 40 | #include "media/blink/webcontentdecryptionmodule_impl.h" |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 41 | #include "media/blink/webinbandtexttrack_impl.h" |
| 42 | #include "media/blink/webmediaplayer_delegate.h" |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 43 | #include "media/blink/webmediaplayer_util.h" |
| 44 | #include "media/blink/webmediasource_impl.h" |
[email protected] | efe7cd2 | 2012-09-12 23:55:01 | [diff] [blame] | 45 | #include "media/filters/chunk_demuxer.h" |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 46 | #include "media/filters/ffmpeg_demuxer.h" |
jrummell | c9d8e53 | 2015-02-26 18:38:19 | [diff] [blame] | 47 | #include "third_party/WebKit/public/platform/WebEncryptedMediaTypes.h" |
srirama.m | 26f864d0 | 2015-07-14 05:21:46 | [diff] [blame] | 48 | #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h" |
| 49 | #include "third_party/WebKit/public/platform/WebMediaPlayerEncryptedMediaClient.h" |
[email protected] | 745746d | 2013-08-23 02:09:16 | [diff] [blame] | 50 | #include "third_party/WebKit/public/platform/WebMediaSource.h" |
[email protected] | c1088446 | 2013-05-30 00:22:09 | [diff] [blame] | 51 | #include "third_party/WebKit/public/platform/WebRect.h" |
| 52 | #include "third_party/WebKit/public/platform/WebSize.h" |
| 53 | #include "third_party/WebKit/public/platform/WebString.h" |
| 54 | #include "third_party/WebKit/public/platform/WebURL.h" |
xhwang | 0acca44b | 2015-06-18 00:43:31 | [diff] [blame] | 55 | #include "third_party/WebKit/public/web/WebDocument.h" |
| 56 | #include "third_party/WebKit/public/web/WebFrame.h" |
[email protected] | 8050465 | 2014-04-18 04:41:50 | [diff] [blame] | 57 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
xhwang | bab66f5 | 2014-12-02 23:49:50 | [diff] [blame] | 58 | #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" |
[email protected] | 65b8528 | 2014-03-31 23:32:31 | [diff] [blame] | 59 | #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 60 | #include "third_party/WebKit/public/web/WebView.h" |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 61 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 62 | using blink::WebCanvas; |
| 63 | using blink::WebMediaPlayer; |
| 64 | using blink::WebRect; |
| 65 | using blink::WebSize; |
| 66 | using blink::WebString; |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 67 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 68 | namespace { |
| 69 | |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 70 | // Limits the range of playback rate. |
| 71 | // |
| 72 | // TODO(kylep): Revisit these. |
| 73 | // |
| 74 | // Vista has substantially lower performance than XP or Windows7. If you speed |
| 75 | // up a video too much, it can't keep up, and rendering stops updating except on |
| 76 | // the time bar. For really high speeds, audio becomes a bottleneck and we just |
| 77 | // use up the data we have, which may not achieve the speed requested, but will |
| 78 | // not crash the tab. |
| 79 | // |
| 80 | // A very slow speed, ie 0.00000001x, causes the machine to lock up. (It seems |
| 81 | // like a busy loop). It gets unresponsive, although its not completely dead. |
| 82 | // |
| 83 | // Also our timers are not very accurate (especially for ogg), which becomes |
| 84 | // evident at low speeds and on Vista. Since other speeds are risky and outside |
| 85 | // 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] | 86 | const double kMinRate = 0.0625; |
| 87 | const double kMaxRate = 16.0; |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 88 | |
guidou | c7babef | 2015-10-22 00:42:35 | [diff] [blame] | 89 | void SetSinkIdOnMediaThread( |
| 90 | scoped_refptr<media::WebAudioSourceProviderImpl> sink, |
| 91 | const std::string& device_id, |
| 92 | const url::Origin& security_origin, |
| 93 | const media::SwitchOutputDeviceCB& callback) { |
| 94 | if (sink->GetOutputDevice()) { |
| 95 | sink->GetOutputDevice()->SwitchOutputDevice(device_id, security_origin, |
| 96 | callback); |
| 97 | } else { |
| 98 | callback.Run(media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); |
| 99 | } |
| 100 | } |
| 101 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 102 | } // namespace |
| 103 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 104 | namespace media { |
[email protected] | add5177 | 2009-06-11 18:25:17 | [diff] [blame] | 105 | |
[email protected] | 6683e1b | 2014-04-10 01:45:38 | [diff] [blame] | 106 | class BufferedDataSourceHostImpl; |
| 107 | |
hubbe | 5f0ad43b | 2015-12-14 20:57:26 | [diff] [blame] | 108 | #define STATIC_ASSERT_MATCHING_ENUM(name, name2) \ |
| 109 | static_assert(static_cast<int>(WebMediaPlayer::CORSMode##name) == \ |
| 110 | static_cast<int>(UrlData::name2), \ |
mostynb | 502b00b | 2015-01-05 23:43:40 | [diff] [blame] | 111 | "mismatching enum values: " #name) |
hubbe | 5f0ad43b | 2015-12-14 20:57:26 | [diff] [blame] | 112 | STATIC_ASSERT_MATCHING_ENUM(Unspecified, CORS_UNSPECIFIED); |
| 113 | STATIC_ASSERT_MATCHING_ENUM(Anonymous, CORS_ANONYMOUS); |
| 114 | STATIC_ASSERT_MATCHING_ENUM(UseCredentials, CORS_USE_CREDENTIALS); |
mostynb | 502b00b | 2015-01-05 23:43:40 | [diff] [blame] | 115 | #undef STATIC_ASSERT_MATCHING_ENUM |
[email protected] | a5a0110 | 2012-06-06 17:01:24 | [diff] [blame] | 116 | |
[email protected] | 2c539b8 | 2012-08-18 04:10:19 | [diff] [blame] | 117 | #define BIND_TO_RENDER_LOOP(function) \ |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 118 | (DCHECK(main_task_runner_->BelongsToCurrentThread()), \ |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 119 | BindToCurrentLoop(base::Bind(function, AsWeakPtr()))) |
[email protected] | 5983adb | 2012-10-24 00:12:00 | [diff] [blame] | 120 | |
[email protected] | 4e98219 | 2014-06-21 13:35:45 | [diff] [blame] | 121 | #define BIND_TO_RENDER_LOOP1(function, arg1) \ |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 122 | (DCHECK(main_task_runner_->BelongsToCurrentThread()), \ |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 123 | BindToCurrentLoop(base::Bind(function, AsWeakPtr(), arg1))) |
[email protected] | 4e98219 | 2014-06-21 13:35:45 | [diff] [blame] | 124 | |
[email protected] | 5b5bb9d | 2010-10-22 19:57:36 | [diff] [blame] | 125 | WebMediaPlayerImpl::WebMediaPlayerImpl( |
[email protected] | 35b2a97 | 2014-04-04 15:50:22 | [diff] [blame] | 126 | blink::WebLocalFrame* frame, |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 127 | blink::WebMediaPlayerClient* client, |
srirama.m | 26f864d0 | 2015-07-14 05:21:46 | [diff] [blame] | 128 | blink::WebMediaPlayerEncryptedMediaClient* encrypted_client, |
[email protected] | b897665 | 2011-10-26 23:46:55 | [diff] [blame] | 129 | base::WeakPtr<WebMediaPlayerDelegate> delegate, |
xhwang | 6fa35620 | 2014-12-11 00:44:12 | [diff] [blame] | 130 | scoped_ptr<RendererFactory> renderer_factory, |
xhwang | d718083 | 2015-04-03 05:38:15 | [diff] [blame] | 131 | CdmFactory* cdm_factory, |
hubbe | 5f0ad43b | 2015-12-14 20:57:26 | [diff] [blame] | 132 | linked_ptr<UrlIndex> url_index, |
[email protected] | e82b2bd | 2013-01-02 17:47:57 | [diff] [blame] | 133 | const WebMediaPlayerParams& params) |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 134 | : frame_(frame), |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 135 | network_state_(WebMediaPlayer::NetworkStateEmpty), |
| 136 | ready_state_(WebMediaPlayer::ReadyStateHaveNothing), |
acolwell | a5081a4 | 2014-08-28 23:42:52 | [diff] [blame] | 137 | preload_(BufferedDataSource::AUTO), |
sandersd | c6ab163a | 2015-12-12 03:56:13 | [diff] [blame] | 138 | buffering_strategy_( |
| 139 | BufferedDataSourceInterface::BUFFERING_STRATEGY_NORMAL), |
anujk.sharma | 2fa37a0 | 2015-04-30 05:51:32 | [diff] [blame] | 140 | main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
acolwell | 755d12d | 2014-08-30 01:09:19 | [diff] [blame] | 141 | media_task_runner_(params.media_task_runner()), |
dcastagna | 617d086b | 2015-08-20 01:39:30 | [diff] [blame] | 142 | worker_task_runner_(params.worker_task_runner()), |
acolwell | 755d12d | 2014-08-30 01:09:19 | [diff] [blame] | 143 | media_log_(params.media_log()), |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 144 | pipeline_(media_task_runner_, media_log_.get()), |
[email protected] | f988d9b | 2014-07-25 00:35:43 | [diff] [blame] | 145 | load_type_(LoadTypeURL), |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 146 | opaque_(false), |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 147 | playback_rate_(0.0), |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 148 | paused_(true), |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 149 | seeking_(false), |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 150 | pending_suspend_(false), |
| 151 | pending_time_change_(false), |
| 152 | pending_resume_(false), |
| 153 | suspending_(false), |
| 154 | suspended_(false), |
| 155 | resuming_(false), |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 156 | ended_(false), |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 157 | pending_seek_(false), |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 158 | should_notify_time_changed_(false), |
[email protected] | 5badb08 | 2010-06-11 17:40:15 | [diff] [blame] | 159 | client_(client), |
srirama.m | 26f864d0 | 2015-07-14 05:21:46 | [diff] [blame] | 160 | encrypted_client_(encrypted_client), |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 161 | delegate_(delegate), |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 162 | defer_load_cb_(params.defer_load_cb()), |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 163 | context_3d_cb_(params.context_3d_cb()), |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 164 | adjust_allocated_memory_cb_(params.adjust_allocated_memory_cb()), |
| 165 | last_reported_memory_usage_(0), |
[email protected] | 132dd57c | 2012-08-10 23:24:34 | [diff] [blame] | 166 | supports_save_(true), |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 167 | chunk_demuxer_(NULL), |
hubbe | 5f0ad43b | 2015-12-14 20:57:26 | [diff] [blame] | 168 | url_index_(url_index), |
Bartosz Fabianowski | 85a82381 | 2015-04-16 10:27:51 | [diff] [blame] | 169 | // Threaded compositing isn't enabled universally yet. |
| 170 | compositor_task_runner_( |
| 171 | params.compositor_task_runner() |
| 172 | ? params.compositor_task_runner() |
| 173 | : base::MessageLoop::current()->task_runner()), |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 174 | compositor_(new VideoFrameCompositor( |
Bartosz Fabianowski | 85a82381 | 2015-04-16 10:27:51 | [diff] [blame] | 175 | compositor_task_runner_, |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 176 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNaturalSizeChanged), |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 177 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnOpacityChanged))), |
xhwang | 9bd8c73 | 2015-04-13 23:27:53 | [diff] [blame] | 178 | encrypted_media_support_(cdm_factory, |
srirama.m | 26f864d0 | 2015-07-14 05:21:46 | [diff] [blame] | 179 | encrypted_client, |
xhwang | 9bd8c73 | 2015-04-13 23:27:53 | [diff] [blame] | 180 | params.media_permission(), |
| 181 | base::Bind(&WebMediaPlayerImpl::SetCdm, |
| 182 | AsWeakPtr(), |
| 183 | base::Bind(&IgnoreCdmAttached))), |
dcheng | 652f5ff | 2015-12-27 08:54:00 | [diff] [blame^] | 184 | renderer_factory_(std::move(renderer_factory)) { |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 185 | DCHECK(!adjust_allocated_memory_cb_.is_null()); |
| 186 | |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 187 | if (delegate) |
| 188 | delegate->AddObserver(this); |
| 189 | |
[email protected] | c93eb0a6 | 2011-08-09 22:47:24 | [diff] [blame] | 190 | media_log_->AddEvent( |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 191 | media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_CREATED)); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 192 | |
xhwang | 0ad11e51 | 2014-11-25 23:43:09 | [diff] [blame] | 193 | if (params.initial_cdm()) { |
xhwang | 9bd8c73 | 2015-04-13 23:27:53 | [diff] [blame] | 194 | SetCdm(base::Bind(&IgnoreCdmAttached), |
| 195 | ToWebContentDecryptionModuleImpl(params.initial_cdm()) |
| 196 | ->GetCdmContext()); |
xhwang | 0ad11e51 | 2014-11-25 23:43:09 | [diff] [blame] | 197 | } |
| 198 | |
xhwang | f94a634d | 2014-10-22 22:07:27 | [diff] [blame] | 199 | // TODO(xhwang): When we use an external Renderer, many methods won't work, |
xhwang | 6fa35620 | 2014-12-11 00:44:12 | [diff] [blame] | 200 | // e.g. GetCurrentFrameFromCompositor(). See http://crbug.com/434861 |
[email protected] | c50edb96 | 2013-10-19 18:05:07 | [diff] [blame] | 201 | |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 202 | // Use the null sink if no sink was provided. |
[email protected] | 4a91488 | 2013-01-10 00:43:48 | [diff] [blame] | 203 | audio_source_provider_ = new WebAudioSourceProviderImpl( |
[email protected] | ff875be5 | 2013-06-02 23:47:38 | [diff] [blame] | 204 | params.audio_renderer_sink().get() |
| 205 | ? params.audio_renderer_sink() |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 206 | : new NullAudioSink(media_task_runner_)); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 207 | } |
| 208 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 209 | WebMediaPlayerImpl::~WebMediaPlayerImpl() { |
[email protected] | ce70c98 | 2013-12-20 17:04:32 | [diff] [blame] | 210 | client_->setWebLayer(NULL); |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 211 | |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 212 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 213 | |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 214 | if (delegate_) { |
| 215 | delegate_->RemoveObserver(this); |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 216 | delegate_->PlayerGone(this); |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 217 | } |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 218 | |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 219 | // Abort any pending IO so stopping the pipeline doesn't get blocked. |
| 220 | if (data_source_) |
| 221 | data_source_->Abort(); |
| 222 | if (chunk_demuxer_) { |
| 223 | chunk_demuxer_->Shutdown(); |
| 224 | chunk_demuxer_ = NULL; |
| 225 | } |
| 226 | |
xhwang | 6fa35620 | 2014-12-11 00:44:12 | [diff] [blame] | 227 | renderer_factory_.reset(); |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 228 | |
| 229 | // Make sure to kill the pipeline so there's no more media threads running. |
| 230 | // Note: stopping the pipeline might block for a long time. |
| 231 | base::WaitableEvent waiter(false, false); |
| 232 | pipeline_.Stop( |
| 233 | base::Bind(&base::WaitableEvent::Signal, base::Unretained(&waiter))); |
| 234 | waiter.Wait(); |
| 235 | |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 236 | if (last_reported_memory_usage_) |
| 237 | adjust_allocated_memory_cb_.Run(-last_reported_memory_usage_); |
| 238 | |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 239 | compositor_task_runner_->DeleteSoon(FROM_HERE, compositor_); |
xhwang | ea8bb356 | 2015-06-08 21:18:37 | [diff] [blame] | 240 | |
| 241 | media_log_->AddEvent( |
| 242 | media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_DESTROYED)); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 243 | } |
| 244 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 245 | void WebMediaPlayerImpl::load(LoadType load_type, const blink::WebURL& url, |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 246 | CORSMode cors_mode) { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 247 | DVLOG(1) << __FUNCTION__ << "(" << load_type << ", " << url << ", " |
| 248 | << cors_mode << ")"; |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 249 | if (!defer_load_cb_.is_null()) { |
| 250 | defer_load_cb_.Run(base::Bind( |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 251 | &WebMediaPlayerImpl::DoLoad, AsWeakPtr(), load_type, url, cors_mode)); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 252 | return; |
| 253 | } |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 254 | DoLoad(load_type, url, cors_mode); |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 255 | } |
| 256 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 257 | void WebMediaPlayerImpl::DoLoad(LoadType load_type, |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 258 | const blink::WebURL& url, |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 259 | CORSMode cors_mode) { |
hubbe | d82bed5 | 2015-12-15 23:07:16 | [diff] [blame] | 260 | DVLOG(1) << __FUNCTION__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 261 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 262 | |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 263 | GURL gurl(url); |
xhwang | 0acca44b | 2015-06-18 00:43:31 | [diff] [blame] | 264 | ReportMetrics(load_type, gurl, |
| 265 | GURL(frame_->document().securityOrigin().toString())); |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 266 | |
[email protected] | 926f8fd | 2013-04-12 20:27:53 | [diff] [blame] | 267 | // Set subresource URL for crash reporting. |
| 268 | base::debug::SetCrashKeyValue("subresource_url", gurl.spec()); |
| 269 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 270 | load_type_ = load_type; |
| 271 | |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 272 | SetNetworkState(WebMediaPlayer::NetworkStateLoading); |
| 273 | SetReadyState(WebMediaPlayer::ReadyStateHaveNothing); |
kinuko | f19cde7 | 2015-12-18 23:15:25 | [diff] [blame] | 274 | media_log_->AddEvent(media_log_->CreateLoadEvent(url.string().utf8())); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 275 | |
| 276 | // Media source pipelines can start immediately. |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 277 | if (load_type == LoadTypeMediaSource) { |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 278 | supports_save_ = false; |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 279 | StartPipeline(); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 280 | return; |
| 281 | } |
| 282 | |
| 283 | // Otherwise it's a regular request which requires resolving the URL first. |
hubbe | 5f0ad43b | 2015-12-14 20:57:26 | [diff] [blame] | 284 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 285 | switches::kUseNewMediaCache)) { |
| 286 | // Remove this when MultiBufferDataSource becomes default. |
| 287 | LOG(WARNING) << "Using MultibufferDataSource"; |
| 288 | data_source_.reset(new MultibufferDataSource( |
| 289 | url, static_cast<UrlData::CORSMode>(cors_mode), main_task_runner_, |
| 290 | url_index_, frame_, media_log_.get(), &buffered_data_source_host_, |
| 291 | base::Bind(&WebMediaPlayerImpl::NotifyDownloading, AsWeakPtr()))); |
| 292 | } else { |
| 293 | data_source_.reset(new BufferedDataSource( |
| 294 | url, static_cast<BufferedResourceLoader::CORSMode>(cors_mode), |
| 295 | main_task_runner_, frame_, media_log_.get(), |
| 296 | &buffered_data_source_host_, |
| 297 | base::Bind(&WebMediaPlayerImpl::NotifyDownloading, AsWeakPtr()))); |
| 298 | } |
dalecurtis | 9157fc9e | 2015-01-16 01:09:11 | [diff] [blame] | 299 | data_source_->SetPreload(preload_); |
sandersd | c6ab163a | 2015-12-12 03:56:13 | [diff] [blame] | 300 | data_source_->SetBufferingStrategy(buffering_strategy_); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 301 | data_source_->Initialize( |
[email protected] | fee8a90 | 2014-06-03 13:43:36 | [diff] [blame] | 302 | base::Bind(&WebMediaPlayerImpl::DataSourceInitialized, AsWeakPtr())); |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 303 | } |
| 304 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 305 | void WebMediaPlayerImpl::play() { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 306 | DVLOG(1) << __FUNCTION__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 307 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 308 | |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 309 | paused_ = false; |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 310 | pipeline_.SetPlaybackRate(playback_rate_); |
[email protected] | 039b754 | 2013-10-17 22:06:25 | [diff] [blame] | 311 | if (data_source_) |
| 312 | data_source_->MediaIsPlaying(); |
[email protected] | 090f731 | 2011-08-05 23:26:40 | [diff] [blame] | 313 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 314 | media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PLAY)); |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 315 | |
dalecurtis | 6d269ed | 2014-09-11 21:18:13 | [diff] [blame] | 316 | if (delegate_ && playback_rate_ > 0) |
dalecurtis | 0f0097a | 2015-12-01 17:40:47 | [diff] [blame] | 317 | NotifyPlaybackStarted(); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 318 | } |
| 319 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 320 | void WebMediaPlayerImpl::pause() { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 321 | DVLOG(1) << __FUNCTION__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 322 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 323 | |
dalecurtis | 6d269ed | 2014-09-11 21:18:13 | [diff] [blame] | 324 | const bool was_already_paused = paused_ || playback_rate_ == 0; |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 325 | paused_ = true; |
a.berwal | 338bf00 | 2015-04-22 11:14:50 | [diff] [blame] | 326 | pipeline_.SetPlaybackRate(0.0); |
dalecurtis | 2e50e6f6 | 2015-01-09 02:42:52 | [diff] [blame] | 327 | UpdatePausedTime(); |
[email protected] | 090f731 | 2011-08-05 23:26:40 | [diff] [blame] | 328 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 329 | media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PAUSE)); |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 330 | |
dalecurtis | 6d269ed | 2014-09-11 21:18:13 | [diff] [blame] | 331 | if (!was_already_paused && delegate_) |
dalecurtis | 0f0097a | 2015-12-01 17:40:47 | [diff] [blame] | 332 | NotifyPlaybackPaused(); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 333 | } |
| 334 | |
[email protected] | 574a1d6 | 2009-07-17 03:23:46 | [diff] [blame] | 335 | bool WebMediaPlayerImpl::supportsSave() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 336 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 132dd57c | 2012-08-10 23:24:34 | [diff] [blame] | 337 | return supports_save_; |
[email protected] | 574a1d6 | 2009-07-17 03:23:46 | [diff] [blame] | 338 | } |
| 339 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 340 | void WebMediaPlayerImpl::seek(double seconds) { |
xhwang | 12d8d04 | 2014-12-01 21:48:57 | [diff] [blame] | 341 | DVLOG(1) << __FUNCTION__ << "(" << seconds << "s)"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 342 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 343 | |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 344 | ended_ = false; |
| 345 | |
srirama.m | ccf67181 | 2015-01-08 11:59:13 | [diff] [blame] | 346 | ReadyState old_state = ready_state_; |
[email protected] | 1bb66680 | 2013-11-28 06:12:08 | [diff] [blame] | 347 | if (ready_state_ > WebMediaPlayer::ReadyStateHaveMetadata) |
| 348 | SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata); |
| 349 | |
qinmin | b4c3978 | 2015-08-10 18:43:24 | [diff] [blame] | 350 | base::TimeDelta new_seek_time = base::TimeDelta::FromSecondsD(seconds); |
[email protected] | bb8a70b | 2013-06-26 06:23:35 | [diff] [blame] | 351 | |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 352 | if (seeking_ || suspended_) { |
| 353 | // Once resuming, it's too late to change the resume time and so the |
| 354 | // implementation is a little different. |
| 355 | bool is_suspended = suspended_ && !resuming_; |
| 356 | |
| 357 | // If we are currently seeking or resuming to |new_seek_time|, skip the |
| 358 | // seek (except for MSE, which always seeks). |
| 359 | if (!is_suspended && new_seek_time == seek_time_) { |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 360 | if (chunk_demuxer_) { |
wolenetz | a609797 | 2015-09-01 05:37:56 | [diff] [blame] | 361 | // Don't suppress any redundant in-progress MSE seek. There could have |
| 362 | // been changes to the underlying buffers after seeking the demuxer and |
| 363 | // before receiving OnPipelineSeeked() for the currently in-progress |
| 364 | // seek. |
| 365 | MEDIA_LOG(DEBUG, media_log_) |
| 366 | << "Detected MediaSource seek to same time as in-progress seek to " |
| 367 | << seek_time_ << "."; |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 368 | } else { |
| 369 | // Suppress all redundant seeks if unrestricted by media source demuxer |
| 370 | // API. |
| 371 | pending_seek_ = false; |
| 372 | pending_seek_time_ = base::TimeDelta(); |
| 373 | return; |
| 374 | } |
| 375 | } |
| 376 | |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 377 | // If |chunk_demuxer_| is already seeking, cancel that seek and schedule the |
| 378 | // new one. |
| 379 | if (!is_suspended && chunk_demuxer_) |
| 380 | chunk_demuxer_->CancelPendingSeek(new_seek_time); |
| 381 | |
| 382 | // Schedule a seek once the current suspend or seek finishes. |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 383 | pending_seek_ = true; |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 384 | pending_seek_time_ = new_seek_time; |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 385 | |
| 386 | // In the case of seeking while suspended, the seek is considered to have |
| 387 | // started immediately (but won't complete until the pipeline is resumed). |
| 388 | if (is_suspended) { |
| 389 | seeking_ = true; |
| 390 | seek_time_ = new_seek_time; |
| 391 | } |
| 392 | |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 393 | return; |
| 394 | } |
| 395 | |
[email protected] | c93eb0a6 | 2011-08-09 22:47:24 | [diff] [blame] | 396 | media_log_->AddEvent(media_log_->CreateSeekEvent(seconds)); |
| 397 | |
[email protected] | 44ff37c0 | 2009-10-24 01:03:03 | [diff] [blame] | 398 | // Update our paused time. |
wolenetz | a609797 | 2015-09-01 05:37:56 | [diff] [blame] | 399 | // For non-MSE playbacks, in paused state ignore the seek operations to |
| 400 | // current time if the loading is completed and generate |
| 401 | // OnPipelineBufferingStateChanged event to eventually fire seeking and seeked |
| 402 | // events. We don't short-circuit MSE seeks in this logic because the |
| 403 | // underlying buffers around the seek time might have changed (or even been |
| 404 | // removed) since previous seek/preroll/pause action, and the pipeline might |
| 405 | // need to flush so the new buffers are decoded and rendered instead of the |
| 406 | // old ones. |
srirama.m | 36ab268 | 2014-12-11 04:20:01 | [diff] [blame] | 407 | if (paused_) { |
wolenetz | a609797 | 2015-09-01 05:37:56 | [diff] [blame] | 408 | if (paused_time_ != new_seek_time || chunk_demuxer_) { |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 409 | paused_time_ = new_seek_time; |
srirama.m | ccf67181 | 2015-01-08 11:59:13 | [diff] [blame] | 410 | } else if (old_state == ReadyStateHaveEnoughData) { |
srirama.m | 8f4a3756 | 2014-12-13 08:16:18 | [diff] [blame] | 411 | main_task_runner_->PostTask( |
srirama.m | 36ab268 | 2014-12-11 04:20:01 | [diff] [blame] | 412 | FROM_HERE, |
| 413 | base::Bind(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged, |
srirama.m | 8f4a3756 | 2014-12-13 08:16:18 | [diff] [blame] | 414 | AsWeakPtr(), BUFFERING_HAVE_ENOUGH)); |
srirama.m | 36ab268 | 2014-12-11 04:20:01 | [diff] [blame] | 415 | return; |
| 416 | } |
| 417 | } |
[email protected] | 44ff37c0 | 2009-10-24 01:03:03 | [diff] [blame] | 418 | |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 419 | seeking_ = true; |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 420 | seek_time_ = new_seek_time; |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 421 | |
[email protected] | efe7cd2 | 2012-09-12 23:55:01 | [diff] [blame] | 422 | if (chunk_demuxer_) |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 423 | chunk_demuxer_->StartWaitingForSeek(seek_time_); |
[email protected] | 020fba3 | 2011-06-29 16:37:46 | [diff] [blame] | 424 | |
[email protected] | 44ff37c0 | 2009-10-24 01:03:03 | [diff] [blame] | 425 | // Kick off the asynchronous seek! |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 426 | pipeline_.Seek(seek_time_, BIND_TO_RENDER_LOOP1( |
| 427 | &WebMediaPlayerImpl::OnPipelineSeeked, true)); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 428 | } |
| 429 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 430 | void WebMediaPlayerImpl::setRate(double rate) { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 431 | DVLOG(1) << __FUNCTION__ << "(" << rate << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 432 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 433 | |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 434 | // TODO(kylep): Remove when support for negatives is added. Also, modify the |
| 435 | // following checks so rewind uses reasonable values also. |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 436 | if (rate < 0.0) |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 437 | return; |
| 438 | |
| 439 | // Limit rates to reasonable values by clamping. |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 440 | if (rate != 0.0) { |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 441 | if (rate < kMinRate) |
| 442 | rate = kMinRate; |
| 443 | else if (rate > kMaxRate) |
| 444 | rate = kMaxRate; |
dalecurtis | bb761e1 | 2014-09-12 01:16:35 | [diff] [blame] | 445 | if (playback_rate_ == 0 && !paused_ && delegate_) |
dalecurtis | 0f0097a | 2015-12-01 17:40:47 | [diff] [blame] | 446 | NotifyPlaybackStarted(); |
dalecurtis | bb761e1 | 2014-09-12 01:16:35 | [diff] [blame] | 447 | } else if (playback_rate_ != 0 && !paused_ && delegate_) { |
dalecurtis | 0f0097a | 2015-12-01 17:40:47 | [diff] [blame] | 448 | NotifyPlaybackPaused(); |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 449 | } |
| 450 | |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 451 | playback_rate_ = rate; |
| 452 | if (!paused_) { |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 453 | pipeline_.SetPlaybackRate(rate); |
[email protected] | 039b754 | 2013-10-17 22:06:25 | [diff] [blame] | 454 | if (data_source_) |
| 455 | data_source_->MediaPlaybackRateChanged(rate); |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 456 | } |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 457 | } |
| 458 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 459 | void WebMediaPlayerImpl::setVolume(double volume) { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 460 | DVLOG(1) << __FUNCTION__ << "(" << volume << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 461 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 462 | |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 463 | pipeline_.SetVolume(volume); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 464 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 465 | |
guidou | c7babef | 2015-10-22 00:42:35 | [diff] [blame] | 466 | void WebMediaPlayerImpl::setSinkId( |
| 467 | const blink::WebString& sink_id, |
| 468 | const blink::WebSecurityOrigin& security_origin, |
| 469 | blink::WebSetSinkIdCallbacks* web_callback) { |
guidou | 69223ce | 2015-06-16 10:36:19 | [diff] [blame] | 470 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
guidou | 5754e61 | 2015-07-31 08:09:41 | [diff] [blame] | 471 | DVLOG(1) << __FUNCTION__; |
guidou | c7babef | 2015-10-22 00:42:35 | [diff] [blame] | 472 | |
guidou | 5754e61 | 2015-07-31 08:09:41 | [diff] [blame] | 473 | media::SwitchOutputDeviceCB callback = |
| 474 | media::ConvertToSwitchOutputDeviceCB(web_callback); |
guidou | c7babef | 2015-10-22 00:42:35 | [diff] [blame] | 475 | media_task_runner_->PostTask( |
| 476 | FROM_HERE, |
| 477 | base::Bind(&SetSinkIdOnMediaThread, audio_source_provider_, |
| 478 | sink_id.utf8(), static_cast<url::Origin>(security_origin), |
| 479 | callback)); |
guidou | 69223ce | 2015-06-16 10:36:19 | [diff] [blame] | 480 | } |
| 481 | |
mostynb | 502b00b | 2015-01-05 23:43:40 | [diff] [blame] | 482 | #define STATIC_ASSERT_MATCHING_ENUM(webkit_name, chromium_name) \ |
| 483 | static_assert(static_cast<int>(WebMediaPlayer::webkit_name) == \ |
| 484 | static_cast<int>(BufferedDataSource::chromium_name), \ |
| 485 | "mismatching enum values: " #webkit_name) |
| 486 | STATIC_ASSERT_MATCHING_ENUM(PreloadNone, NONE); |
| 487 | STATIC_ASSERT_MATCHING_ENUM(PreloadMetaData, METADATA); |
| 488 | STATIC_ASSERT_MATCHING_ENUM(PreloadAuto, AUTO); |
| 489 | #undef STATIC_ASSERT_MATCHING_ENUM |
[email protected] | 23a8b1d8 | 2011-04-05 16:28:20 | [diff] [blame] | 490 | |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 491 | void WebMediaPlayerImpl::setPreload(WebMediaPlayer::Preload preload) { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 492 | DVLOG(1) << __FUNCTION__ << "(" << preload << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 493 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 494 | |
acolwell | a5081a4 | 2014-08-28 23:42:52 | [diff] [blame] | 495 | preload_ = static_cast<BufferedDataSource::Preload>(preload); |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 496 | if (data_source_) |
[email protected] | 09c6022 | 2014-08-07 16:42:31 | [diff] [blame] | 497 | data_source_->SetPreload(preload_); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 498 | } |
| 499 | |
sandersd | c6ab163a | 2015-12-12 03:56:13 | [diff] [blame] | 500 | #define STATIC_ASSERT_MATCHING_ENUM(webkit_name, chromium_name) \ |
| 501 | static_assert(static_cast<int>(WebMediaPlayer::webkit_name) == \ |
| 502 | static_cast<int>(BufferedDataSource::chromium_name), \ |
| 503 | "mismatching enum values: " #webkit_name) |
| 504 | STATIC_ASSERT_MATCHING_ENUM(BufferingStrategy::Normal, |
| 505 | BUFFERING_STRATEGY_NORMAL); |
| 506 | STATIC_ASSERT_MATCHING_ENUM(BufferingStrategy::Aggressive, |
| 507 | BUFFERING_STRATEGY_AGGRESSIVE); |
| 508 | #undef STATIC_ASSERT_MATCHING_ENUM |
| 509 | |
| 510 | void WebMediaPlayerImpl::setBufferingStrategy( |
| 511 | WebMediaPlayer::BufferingStrategy buffering_strategy) { |
| 512 | DVLOG(1) << __FUNCTION__; |
| 513 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 514 | |
| 515 | buffering_strategy_ = |
| 516 | static_cast<BufferedDataSource::BufferingStrategy>(buffering_strategy); |
| 517 | if (data_source_) |
| 518 | data_source_->SetBufferingStrategy(buffering_strategy_); |
| 519 | } |
| 520 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 521 | bool WebMediaPlayerImpl::hasVideo() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 522 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 523 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 524 | return pipeline_metadata_.has_video; |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 525 | } |
| 526 | |
[email protected] | fc367af | 2009-08-14 23:06:35 | [diff] [blame] | 527 | bool WebMediaPlayerImpl::hasAudio() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 528 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | fc367af | 2009-08-14 23:06:35 | [diff] [blame] | 529 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 530 | return pipeline_metadata_.has_audio; |
[email protected] | fc367af | 2009-08-14 23:06:35 | [diff] [blame] | 531 | } |
| 532 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 533 | blink::WebSize WebMediaPlayerImpl::naturalSize() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 534 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 535 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 536 | return blink::WebSize(pipeline_metadata_.natural_size); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 537 | } |
| 538 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 539 | bool WebMediaPlayerImpl::paused() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 540 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 541 | |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 542 | return pipeline_.GetPlaybackRate() == 0.0f; |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 543 | } |
| 544 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 545 | bool WebMediaPlayerImpl::seeking() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 546 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 547 | |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 548 | if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) |
[email protected] | 0acebfa | 2009-08-21 22:45:40 | [diff] [blame] | 549 | return false; |
[email protected] | 67cd505 | 2009-09-10 21:53:22 | [diff] [blame] | 550 | |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 551 | return seeking_; |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 552 | } |
| 553 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 554 | double WebMediaPlayerImpl::duration() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 555 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 556 | |
| 557 | if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) |
| 558 | return std::numeric_limits<double>::quiet_NaN(); |
| 559 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 560 | return GetPipelineDuration(); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 561 | } |
| 562 | |
[email protected] | db66d009 | 2014-04-16 07:15:12 | [diff] [blame] | 563 | double WebMediaPlayerImpl::timelineOffset() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 564 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | db66d009 | 2014-04-16 07:15:12 | [diff] [blame] | 565 | |
| 566 | if (pipeline_metadata_.timeline_offset.is_null()) |
| 567 | return std::numeric_limits<double>::quiet_NaN(); |
| 568 | |
| 569 | return pipeline_metadata_.timeline_offset.ToJsTime(); |
| 570 | } |
| 571 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 572 | double WebMediaPlayerImpl::currentTime() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 573 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 574 | DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing); |
| 575 | |
| 576 | // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement, |
| 577 | // see http://crbug.com/409280 |
| 578 | if (ended_) |
| 579 | return duration(); |
| 580 | |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 581 | // We know the current seek time better than pipeline: pipeline may processing |
| 582 | // an earlier seek before a pending seek has been started, or it might not yet |
| 583 | // have the current seek time returnable via GetMediaTime(). |
| 584 | if (seeking()) { |
| 585 | return pending_seek_ ? pending_seek_time_.InSecondsF() |
| 586 | : seek_time_.InSecondsF(); |
| 587 | } |
| 588 | |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 589 | return (paused_ ? paused_time_ : pipeline_.GetMediaTime()).InSecondsF(); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 590 | } |
| 591 | |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 592 | WebMediaPlayer::NetworkState WebMediaPlayerImpl::networkState() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 593 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 594 | return network_state_; |
| 595 | } |
| 596 | |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 597 | WebMediaPlayer::ReadyState WebMediaPlayerImpl::readyState() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 598 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 599 | return ready_state_; |
| 600 | } |
| 601 | |
[email protected] | 02022fc | 2014-05-16 00:05:31 | [diff] [blame] | 602 | blink::WebTimeRanges WebMediaPlayerImpl::buffered() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 603 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 779a832 | 2014-08-22 21:28:37 | [diff] [blame] | 604 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 605 | Ranges<base::TimeDelta> buffered_time_ranges = |
[email protected] | 02022fc | 2014-05-16 00:05:31 | [diff] [blame] | 606 | pipeline_.GetBufferedTimeRanges(); |
[email protected] | 779a832 | 2014-08-22 21:28:37 | [diff] [blame] | 607 | |
| 608 | const base::TimeDelta duration = pipeline_.GetMediaDuration(); |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 609 | if (duration != kInfiniteDuration()) { |
[email protected] | 779a832 | 2014-08-22 21:28:37 | [diff] [blame] | 610 | buffered_data_source_host_.AddBufferedTimeRanges( |
| 611 | &buffered_time_ranges, duration); |
| 612 | } |
[email protected] | 02022fc | 2014-05-16 00:05:31 | [diff] [blame] | 613 | return ConvertToWebTimeRanges(buffered_time_ranges); |
| 614 | } |
| 615 | |
philipj | b0e6f3f | 2014-09-30 09:51:53 | [diff] [blame] | 616 | blink::WebTimeRanges WebMediaPlayerImpl::seekable() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 617 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 618 | |
dalecurtis | 56359cb | 2014-10-28 00:06:29 | [diff] [blame] | 619 | if (ready_state_ < WebMediaPlayer::ReadyStateHaveMetadata) |
philipj | b0e6f3f | 2014-09-30 09:51:53 | [diff] [blame] | 620 | return blink::WebTimeRanges(); |
| 621 | |
dalecurtis | 56359cb | 2014-10-28 00:06:29 | [diff] [blame] | 622 | const double seekable_end = duration(); |
| 623 | |
| 624 | // Allow a special exception for seeks to zero for streaming sources with a |
| 625 | // finite duration; this allows looping to work. |
| 626 | const bool allow_seek_to_zero = data_source_ && data_source_->IsStreaming() && |
mateuszs | 3371ab0 | 2015-04-24 13:20:23 | [diff] [blame] | 627 | std::isfinite(seekable_end); |
dalecurtis | 56359cb | 2014-10-28 00:06:29 | [diff] [blame] | 628 | |
| 629 | // TODO(dalecurtis): Technically this allows seeking on media which return an |
| 630 | // infinite duration so long as DataSource::IsStreaming() is false. While not |
| 631 | // expected, disabling this breaks semi-live players, http://crbug.com/427412. |
| 632 | const blink::WebTimeRange seekable_range( |
| 633 | 0.0, allow_seek_to_zero ? 0.0 : seekable_end); |
philipj | b0e6f3f | 2014-09-30 09:51:53 | [diff] [blame] | 634 | return blink::WebTimeRanges(&seekable_range, 1); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 635 | } |
| 636 | |
[email protected] | 5d2b3e4c | 2014-05-12 23:27:30 | [diff] [blame] | 637 | bool WebMediaPlayerImpl::didLoadingProgress() { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 638 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 6683e1b | 2014-04-10 01:45:38 | [diff] [blame] | 639 | bool pipeline_progress = pipeline_.DidLoadingProgress(); |
| 640 | bool data_progress = buffered_data_source_host_.DidLoadingProgress(); |
| 641 | return pipeline_progress || data_progress; |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 642 | } |
| 643 | |
[email protected] | dd5c797 | 2014-08-21 15:00:37 | [diff] [blame] | 644 | void WebMediaPlayerImpl::paint(blink::WebCanvas* canvas, |
| 645 | const blink::WebRect& rect, |
[email protected] | dd5c797 | 2014-08-21 15:00:37 | [diff] [blame] | 646 | unsigned char alpha, |
| 647 | SkXfermode::Mode mode) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 648 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 649 | TRACE_EVENT0("media", "WebMediaPlayerImpl:paint"); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 650 | |
[email protected] | 5e1502e | 2014-03-24 22:48:44 | [diff] [blame] | 651 | // TODO(scherkus): Clarify paint() API contract to better understand when and |
| 652 | // why it's being called. For example, today paint() is called when: |
| 653 | // - We haven't reached HAVE_CURRENT_DATA and need to paint black |
| 654 | // - We're painting to a canvas |
| 655 | // See http://crbug.com/341225 http://crbug.com/342621 for details. |
mcasas | f1236fc2 | 2015-05-29 22:38:56 | [diff] [blame] | 656 | scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor(); |
[email protected] | 0fe5d43b | 2014-01-24 23:32:45 | [diff] [blame] | 657 | |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 658 | gfx::Rect gfx_rect(rect); |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 659 | Context3D context_3d; |
mcasas | 265bdbf8 | 2015-06-12 18:44:07 | [diff] [blame] | 660 | if (video_frame.get() && video_frame->HasTextures()) { |
mcasas | f1236fc2 | 2015-05-29 22:38:56 | [diff] [blame] | 661 | if (!context_3d_cb_.is_null()) |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 662 | context_3d = context_3d_cb_.Run(); |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 663 | // GPU Process crashed. |
| 664 | if (!context_3d.gl) |
| 665 | return; |
| 666 | } |
danakj | 795f173 | 2015-08-31 23:40:22 | [diff] [blame] | 667 | skcanvas_video_renderer_.Paint(video_frame, canvas, gfx::RectF(gfx_rect), |
| 668 | alpha, mode, pipeline_metadata_.video_rotation, |
| 669 | context_3d); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 670 | } |
[email protected] | 5df5165 | 2009-01-17 00:03:00 | [diff] [blame] | 671 | |
[email protected] | 38259a7a8 | 2009-07-29 21:49:49 | [diff] [blame] | 672 | bool WebMediaPlayerImpl::hasSingleSecurityOrigin() const { |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 673 | if (data_source_) |
| 674 | return data_source_->HasSingleOrigin(); |
[email protected] | fcdb746 | 2009-10-21 21:05:11 | [diff] [blame] | 675 | return true; |
[email protected] | 38259a7a8 | 2009-07-29 21:49:49 | [diff] [blame] | 676 | } |
| 677 | |
[email protected] | 3fe2711 | 2012-06-07 04:00:01 | [diff] [blame] | 678 | bool WebMediaPlayerImpl::didPassCORSAccessCheck() const { |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 679 | if (data_source_) |
| 680 | return data_source_->DidPassCORSAccessCheck(); |
| 681 | return false; |
[email protected] | 3fe2711 | 2012-06-07 04:00:01 | [diff] [blame] | 682 | } |
| 683 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 684 | double WebMediaPlayerImpl::mediaTimeForTimeValue(double timeValue) const { |
qinmin | b4c3978 | 2015-08-10 18:43:24 | [diff] [blame] | 685 | return base::TimeDelta::FromSecondsD(timeValue).InSecondsF(); |
[email protected] | e06e16d8 | 2011-05-26 22:13:33 | [diff] [blame] | 686 | } |
| 687 | |
[email protected] | d82b18ae | 2011-03-23 21:28:59 | [diff] [blame] | 688 | unsigned WebMediaPlayerImpl::decodedFrameCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 689 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 690 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 691 | PipelineStatistics stats = pipeline_.GetStatistics(); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 692 | return stats.video_frames_decoded; |
| 693 | } |
| 694 | |
[email protected] | d82b18ae | 2011-03-23 21:28:59 | [diff] [blame] | 695 | unsigned WebMediaPlayerImpl::droppedFrameCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 696 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 697 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 698 | PipelineStatistics stats = pipeline_.GetStatistics(); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 699 | return stats.video_frames_dropped; |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 700 | } |
| 701 | |
[email protected] | d82b18ae | 2011-03-23 21:28:59 | [diff] [blame] | 702 | unsigned WebMediaPlayerImpl::audioDecodedByteCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 703 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 704 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 705 | PipelineStatistics stats = pipeline_.GetStatistics(); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 706 | return stats.audio_bytes_decoded; |
| 707 | } |
| 708 | |
[email protected] | d82b18ae | 2011-03-23 21:28:59 | [diff] [blame] | 709 | unsigned WebMediaPlayerImpl::videoDecodedByteCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 710 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 711 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 712 | PipelineStatistics stats = pipeline_.GetStatistics(); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 713 | return stats.video_bytes_decoded; |
| 714 | } |
| 715 | |
[email protected] | 6523b24 | 2013-03-13 11:10:07 | [diff] [blame] | 716 | bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture( |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 717 | blink::WebGraphicsContext3D* web_graphics_context, |
zmo | 57d577a | 2015-10-30 18:28:59 | [diff] [blame] | 718 | unsigned int texture, |
| 719 | unsigned int internal_format, |
| 720 | unsigned int type, |
| 721 | bool premultiply_alpha, |
| 722 | bool flip_y) { |
[email protected] | bfc05f2 | 2013-10-19 17:55:16 | [diff] [blame] | 723 | TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture"); |
| 724 | |
mcasas | f1236fc2 | 2015-05-29 22:38:56 | [diff] [blame] | 725 | scoped_refptr<VideoFrame> video_frame = GetCurrentFrameFromCompositor(); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 726 | |
mcasas | 265bdbf8 | 2015-06-12 18:44:07 | [diff] [blame] | 727 | if (!video_frame.get() || !video_frame->HasTextures() || |
dcastagna | 31a65d43 | 2015-06-11 19:01:00 | [diff] [blame] | 728 | media::VideoFrame::NumPlanes(video_frame->format()) != 1) { |
[email protected] | e56f88c7 | 2013-06-25 22:31:29 | [diff] [blame] | 729 | return false; |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 730 | } |
[email protected] | df41e25 | 2014-02-03 23:39:50 | [diff] [blame] | 731 | |
dongseong.hwang | 0c4e9d8 | 2015-01-08 20:11:13 | [diff] [blame] | 732 | // TODO(dshwang): need more elegant way to convert WebGraphicsContext3D to |
| 733 | // GLES2Interface. |
| 734 | gpu::gles2::GLES2Interface* gl = |
| 735 | static_cast<gpu_blink::WebGraphicsContext3DImpl*>(web_graphics_context) |
| 736 | ->GetGLInterface(); |
dcastagna | 31a65d43 | 2015-06-11 19:01:00 | [diff] [blame] | 737 | SkCanvasVideoRenderer::CopyVideoFrameSingleTextureToGLTexture( |
zmo | 57d577a | 2015-10-30 18:28:59 | [diff] [blame] | 738 | gl, video_frame.get(), texture, internal_format, type, premultiply_alpha, |
| 739 | flip_y); |
[email protected] | e56f88c7 | 2013-06-25 22:31:29 | [diff] [blame] | 740 | return true; |
[email protected] | 6523b24 | 2013-03-13 11:10:07 | [diff] [blame] | 741 | } |
| 742 | |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 743 | WebMediaPlayer::MediaKeyException |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 744 | WebMediaPlayerImpl::generateKeyRequest(const WebString& key_system, |
| 745 | const unsigned char* init_data, |
| 746 | unsigned init_data_length) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 747 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 7502ef1 | 2014-01-25 01:19:27 | [diff] [blame] | 748 | |
xhwang | 16ff136 | 2014-11-19 00:16:34 | [diff] [blame] | 749 | return encrypted_media_support_.GenerateKeyRequest( |
acolwell | e2ea518 | 2014-08-25 23:01:07 | [diff] [blame] | 750 | frame_, key_system, init_data, init_data_length); |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 751 | } |
| 752 | |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 753 | WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::addKey( |
| 754 | const WebString& key_system, |
| 755 | const unsigned char* key, |
| 756 | unsigned key_length, |
| 757 | const unsigned char* init_data, |
| 758 | unsigned init_data_length, |
| 759 | const WebString& session_id) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 760 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 761 | |
xhwang | 16ff136 | 2014-11-19 00:16:34 | [diff] [blame] | 762 | return encrypted_media_support_.AddKey( |
acolwell | e2ea518 | 2014-08-25 23:01:07 | [diff] [blame] | 763 | key_system, key, key_length, init_data, init_data_length, session_id); |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 764 | } |
| 765 | |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 766 | WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::cancelKeyRequest( |
| 767 | const WebString& key_system, |
| 768 | const WebString& session_id) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 769 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 0b9999d | 2014-02-20 02:16:11 | [diff] [blame] | 770 | |
xhwang | 16ff136 | 2014-11-19 00:16:34 | [diff] [blame] | 771 | return encrypted_media_support_.CancelKeyRequest(key_system, session_id); |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 772 | } |
| 773 | |
[email protected] | 7bce183 | 2014-01-09 00:01:22 | [diff] [blame] | 774 | void WebMediaPlayerImpl::setContentDecryptionModule( |
[email protected] | 9ebc3b03f | 2014-08-13 04:01:23 | [diff] [blame] | 775 | blink::WebContentDecryptionModule* cdm, |
| 776 | blink::WebContentDecryptionModuleResult result) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 777 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 9ebc3b03f | 2014-08-13 04:01:23 | [diff] [blame] | 778 | |
jrummell | 06f2707 | 2015-06-08 18:12:38 | [diff] [blame] | 779 | // Once the CDM is set it can't be cleared as there may be frames being |
| 780 | // decrypted on other threads. So fail this request. |
| 781 | // http://crbug.com/462365#c7. |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 782 | if (!cdm) { |
| 783 | result.completeWithError( |
jrummell | 06f2707 | 2015-06-08 18:12:38 | [diff] [blame] | 784 | blink::WebContentDecryptionModuleExceptionInvalidStateError, 0, |
| 785 | "The existing MediaKeys object cannot be removed at this time."); |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 786 | return; |
| 787 | } |
| 788 | |
jrummell | 89e61d8 | 2015-07-23 20:03:34 | [diff] [blame] | 789 | // Although unlikely, it is possible that multiple calls happen |
| 790 | // simultaneously, so fail this call if there is already one pending. |
| 791 | if (set_cdm_result_) { |
| 792 | result.completeWithError( |
| 793 | blink::WebContentDecryptionModuleExceptionInvalidStateError, 0, |
| 794 | "Unable to set MediaKeys object at this time."); |
| 795 | return; |
| 796 | } |
| 797 | |
| 798 | // Create a local copy of |result| to avoid problems with the callback |
| 799 | // getting passed to the media thread and causing |result| to be destructed |
| 800 | // on the wrong thread in some failure conditions. |
| 801 | set_cdm_result_.reset(new blink::WebContentDecryptionModuleResult(result)); |
| 802 | |
| 803 | SetCdm(BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnCdmAttached), |
xhwang | 9bd8c73 | 2015-04-13 23:27:53 | [diff] [blame] | 804 | ToWebContentDecryptionModuleImpl(cdm)->GetCdmContext()); |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 805 | } |
| 806 | |
xhwang | e8c4181a | 2014-12-06 08:10:01 | [diff] [blame] | 807 | void WebMediaPlayerImpl::OnEncryptedMediaInitData( |
jrummell | cf78967c | 2015-04-03 05:03:58 | [diff] [blame] | 808 | EmeInitDataType init_data_type, |
Avi Drissman | 97785ea | 2015-12-19 01:11:31 | [diff] [blame] | 809 | const std::vector<uint8_t>& init_data) { |
jrummell | cf78967c | 2015-04-03 05:03:58 | [diff] [blame] | 810 | DCHECK(init_data_type != EmeInitDataType::UNKNOWN); |
xhwang | bab66f5 | 2014-12-02 23:49:50 | [diff] [blame] | 811 | |
xhwang | e8c4181a | 2014-12-06 08:10:01 | [diff] [blame] | 812 | // Do not fire "encrypted" event if encrypted media is not enabled. |
xhwang | bab66f5 | 2014-12-02 23:49:50 | [diff] [blame] | 813 | // TODO(xhwang): Handle this in |client_|. |
| 814 | if (!blink::WebRuntimeFeatures::isPrefixedEncryptedMediaEnabled() && |
| 815 | !blink::WebRuntimeFeatures::isEncryptedMediaEnabled()) { |
| 816 | return; |
| 817 | } |
| 818 | |
xhwang | e8c4181a | 2014-12-06 08:10:01 | [diff] [blame] | 819 | // TODO(xhwang): Update this UMA name. |
xhwang | bab66f5 | 2014-12-02 23:49:50 | [diff] [blame] | 820 | UMA_HISTOGRAM_COUNTS("Media.EME.NeedKey", 1); |
| 821 | |
| 822 | encrypted_media_support_.SetInitDataType(init_data_type); |
| 823 | |
srirama.m | 26f864d0 | 2015-07-14 05:21:46 | [diff] [blame] | 824 | encrypted_client_->encrypted( |
davidben | b50f00c | 2015-12-01 00:01:50 | [diff] [blame] | 825 | ConvertToWebInitDataType(init_data_type), init_data.data(), |
srirama.m | 26f864d0 | 2015-07-14 05:21:46 | [diff] [blame] | 826 | base::saturated_cast<unsigned int>(init_data.size())); |
xhwang | bab66f5 | 2014-12-02 23:49:50 | [diff] [blame] | 827 | } |
| 828 | |
jrummell | 74fc4f94 | 2015-03-02 22:48:27 | [diff] [blame] | 829 | void WebMediaPlayerImpl::OnWaitingForDecryptionKey() { |
srirama.m | 26f864d0 | 2015-07-14 05:21:46 | [diff] [blame] | 830 | encrypted_client_->didBlockPlaybackWaitingForKey(); |
jrummell | 74fc4f94 | 2015-03-02 22:48:27 | [diff] [blame] | 831 | |
| 832 | // TODO(jrummell): didResumePlaybackBlockedForKey() should only be called |
| 833 | // when a key has been successfully added (e.g. OnSessionKeysChange() with |
| 834 | // |has_additional_usable_key| = true). http://crbug.com/461903 |
srirama.m | 26f864d0 | 2015-07-14 05:21:46 | [diff] [blame] | 835 | encrypted_client_->didResumePlaybackBlockedForKey(); |
jrummell | 74fc4f94 | 2015-03-02 22:48:27 | [diff] [blame] | 836 | } |
| 837 | |
xhwang | 9bd8c73 | 2015-04-13 23:27:53 | [diff] [blame] | 838 | void WebMediaPlayerImpl::SetCdm(const CdmAttachedCB& cdm_attached_cb, |
| 839 | CdmContext* cdm_context) { |
jrummell | 87a2db5 | 2015-05-05 22:27:18 | [diff] [blame] | 840 | // If CDM initialization succeeded, tell the pipeline about it. |
| 841 | if (cdm_context) |
| 842 | pipeline_.SetCdm(cdm_context, cdm_attached_cb); |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 843 | } |
| 844 | |
jrummell | 89e61d8 | 2015-07-23 20:03:34 | [diff] [blame] | 845 | void WebMediaPlayerImpl::OnCdmAttached(bool success) { |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 846 | if (success) { |
jrummell | 89e61d8 | 2015-07-23 20:03:34 | [diff] [blame] | 847 | set_cdm_result_->complete(); |
| 848 | set_cdm_result_.reset(); |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 849 | return; |
| 850 | } |
| 851 | |
jrummell | 89e61d8 | 2015-07-23 20:03:34 | [diff] [blame] | 852 | set_cdm_result_->completeWithError( |
xhwang | 97de420 | 2014-11-25 08:44:01 | [diff] [blame] | 853 | blink::WebContentDecryptionModuleExceptionNotSupportedError, 0, |
| 854 | "Unable to set MediaKeys object"); |
jrummell | 89e61d8 | 2015-07-23 20:03:34 | [diff] [blame] | 855 | set_cdm_result_.reset(); |
[email protected] | 9ebc3b03f | 2014-08-13 04:01:23 | [diff] [blame] | 856 | } |
| 857 | |
[email protected] | 4e98219 | 2014-06-21 13:35:45 | [diff] [blame] | 858 | void WebMediaPlayerImpl::OnPipelineSeeked(bool time_changed, |
| 859 | PipelineStatus status) { |
| 860 | DVLOG(1) << __FUNCTION__ << "(" << time_changed << ", " << status << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 861 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 862 | |
| 863 | if (status != PIPELINE_OK) { |
| 864 | OnPipelineError(status); |
| 865 | return; |
| 866 | } |
| 867 | |
| 868 | // Whether or not the seek was caused by a resume, we're not suspended now. |
| 869 | resuming_ = false; |
| 870 | suspended_ = false; |
| 871 | |
| 872 | // If there is a pending suspend, the seek does not complete until after the |
| 873 | // next resume. |
| 874 | if (pending_suspend_) { |
| 875 | pending_suspend_ = false; |
| 876 | pending_time_change_ = time_changed; |
| 877 | Suspend(); |
| 878 | return; |
| 879 | } |
| 880 | |
| 881 | // Clear seek state. Note that if the seek was caused by a resume, then |
| 882 | // |seek_time_| is always set but |seeking_| is only set if there was a |
| 883 | // pending seek at the time. |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 884 | seeking_ = false; |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 885 | seek_time_ = base::TimeDelta(); |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 886 | |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 887 | if (pending_seek_) { |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 888 | double pending_seek_seconds = pending_seek_time_.InSecondsF(); |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 889 | pending_seek_ = false; |
wolenetz | 1b2ae7d | 2015-06-10 00:44:21 | [diff] [blame] | 890 | pending_seek_time_ = base::TimeDelta(); |
| 891 | seek(pending_seek_seconds); |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 892 | return; |
| 893 | } |
| 894 | |
[email protected] | 10a64d9 | 2012-04-10 21:08:02 | [diff] [blame] | 895 | // Update our paused time. |
| 896 | if (paused_) |
dalecurtis | 2e50e6f6 | 2015-01-09 02:42:52 | [diff] [blame] | 897 | UpdatePausedTime(); |
[email protected] | 10a64d9 | 2012-04-10 21:08:02 | [diff] [blame] | 898 | |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 899 | should_notify_time_changed_ = time_changed; |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 900 | } |
| 901 | |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 902 | void WebMediaPlayerImpl::OnPipelineSuspended(PipelineStatus status) { |
| 903 | DVLOG(1) << __FUNCTION__ << "(" << status << ")"; |
| 904 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 905 | |
| 906 | if (status != PIPELINE_OK) { |
| 907 | OnPipelineError(status); |
| 908 | return; |
| 909 | } |
| 910 | |
| 911 | suspending_ = false; |
| 912 | |
| 913 | if (pending_resume_) { |
| 914 | pending_resume_ = false; |
| 915 | Resume(); |
| 916 | return; |
| 917 | } |
| 918 | } |
| 919 | |
[email protected] | 6954fe1 | 2013-01-03 03:22:32 | [diff] [blame] | 920 | void WebMediaPlayerImpl::OnPipelineEnded() { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 921 | DVLOG(1) << __FUNCTION__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 922 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 923 | |
| 924 | // Ignore state changes until we've completed all outstanding seeks. |
| 925 | if (seeking_ || pending_seek_) |
| 926 | return; |
| 927 | |
| 928 | ended_ = true; |
[email protected] | ce70c98 | 2013-12-20 17:04:32 | [diff] [blame] | 929 | client_->timeChanged(); |
[email protected] | 57653784 | 2009-08-12 23:52:05 | [diff] [blame] | 930 | } |
| 931 | |
[email protected] | a9590c2 | 2011-03-16 16:57:02 | [diff] [blame] | 932 | void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error) { |
hubbe | d82bed5 | 2015-12-15 23:07:16 | [diff] [blame] | 933 | DVLOG(1) << __FUNCTION__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 934 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 935 | DCHECK_NE(error, PIPELINE_OK); |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 936 | |
| 937 | if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) { |
| 938 | // Any error that occurs before reaching ReadyStateHaveMetadata should |
| 939 | // be considered a format error. |
| 940 | SetNetworkState(WebMediaPlayer::NetworkStateFormatError); |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 941 | return; |
| 942 | } |
| 943 | |
[email protected] | be57ee5 | 2013-05-28 22:27:27 | [diff] [blame] | 944 | SetNetworkState(PipelineErrorToNetworkState(error)); |
[email protected] | db190487d | 2009-07-30 18:51:52 | [diff] [blame] | 945 | } |
| 946 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 947 | void WebMediaPlayerImpl::OnPipelineMetadata( |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 948 | PipelineMetadata metadata) { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 949 | DVLOG(1) << __FUNCTION__; |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 950 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 951 | pipeline_metadata_ = metadata; |
[email protected] | 739847c0 | 2014-01-16 00:12:25 | [diff] [blame] | 952 | |
dalecurtis | 849cf4b2 | 2015-03-27 18:35:45 | [diff] [blame] | 953 | UMA_HISTOGRAM_ENUMERATION("Media.VideoRotation", metadata.video_rotation, |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 954 | VIDEO_ROTATION_MAX + 1); |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 955 | SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata); |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 956 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 957 | if (hasVideo()) { |
| 958 | DCHECK(!video_weblayer_); |
[email protected] | f78c3e8 | 2014-08-08 01:24:47 | [diff] [blame] | 959 | scoped_refptr<cc::VideoLayer> layer = |
loyso | a6edaaff | 2015-05-25 03:26:44 | [diff] [blame] | 960 | cc::VideoLayer::Create(cc_blink::WebLayerImpl::LayerSettings(), |
| 961 | compositor_, pipeline_metadata_.video_rotation); |
[email protected] | f78c3e8 | 2014-08-08 01:24:47 | [diff] [blame] | 962 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 963 | if (pipeline_metadata_.video_rotation == VIDEO_ROTATION_90 || |
| 964 | pipeline_metadata_.video_rotation == VIDEO_ROTATION_270) { |
[email protected] | f78c3e8 | 2014-08-08 01:24:47 | [diff] [blame] | 965 | gfx::Size size = pipeline_metadata_.natural_size; |
| 966 | pipeline_metadata_.natural_size = gfx::Size(size.height(), size.width()); |
| 967 | } |
| 968 | |
[email protected] | 3856462 | 2014-08-19 02:47:18 | [diff] [blame] | 969 | video_weblayer_.reset(new cc_blink::WebLayerImpl(layer)); |
jbauman | 952274d | 2015-09-10 23:23:36 | [diff] [blame] | 970 | video_weblayer_->layer()->SetContentsOpaque(opaque_); |
| 971 | video_weblayer_->SetContentsOpaqueIsFixed(true); |
[email protected] | a7c4f58 | 2014-04-16 18:44:49 | [diff] [blame] | 972 | client_->setWebLayer(video_weblayer_.get()); |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 973 | } |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 974 | } |
| 975 | |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 976 | void WebMediaPlayerImpl::OnPipelineBufferingStateChanged( |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 977 | BufferingState buffering_state) { |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 978 | DVLOG(1) << __FUNCTION__ << "(" << buffering_state << ")"; |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 979 | |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 980 | // Ignore buffering state changes until we've completed all outstanding seeks. |
| 981 | if (seeking_ || pending_seek_) |
| 982 | return; |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 983 | |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 984 | // TODO(scherkus): Handle other buffering states when Pipeline starts using |
| 985 | // them and translate them ready state changes http://crbug.com/144683 |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 986 | DCHECK_EQ(buffering_state, BUFFERING_HAVE_ENOUGH); |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 987 | SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData); |
| 988 | |
dalecurtis | 849cf4b2 | 2015-03-27 18:35:45 | [diff] [blame] | 989 | // Let the DataSource know we have enough data. It may use this information to |
| 990 | // release unused network connections. |
| 991 | if (data_source_) |
| 992 | data_source_->OnBufferingHaveEnough(); |
| 993 | |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 994 | // Blink expects a timeChanged() in response to a seek(). |
| 995 | if (should_notify_time_changed_) |
| 996 | client_->timeChanged(); |
dalecurtis | 0f0097a | 2015-12-01 17:40:47 | [diff] [blame] | 997 | |
| 998 | // Once we have enough, start reporting the total memory usage. We'll also |
| 999 | // report once playback starts. |
| 1000 | ReportMemoryUsage(); |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 1001 | } |
| 1002 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 1003 | void WebMediaPlayerImpl::OnDemuxerOpened() { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1004 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
wolenetz | 1fb319d | 2015-07-14 17:49:04 | [diff] [blame] | 1005 | client_->mediaSourceOpened( |
| 1006 | new WebMediaSourceImpl(chunk_demuxer_, media_log_)); |
[email protected] | 81bb332 | 2011-07-21 15:55:50 | [diff] [blame] | 1007 | } |
| 1008 | |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 1009 | void WebMediaPlayerImpl::OnAddTextTrack( |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1010 | const TextTrackConfig& config, |
| 1011 | const AddTextTrackDoneCB& done_cb) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1012 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 1013 | |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 1014 | const WebInbandTextTrackImpl::Kind web_kind = |
| 1015 | static_cast<WebInbandTextTrackImpl::Kind>(config.kind()); |
| 1016 | const blink::WebString web_label = |
| 1017 | blink::WebString::fromUTF8(config.label()); |
| 1018 | const blink::WebString web_language = |
| 1019 | blink::WebString::fromUTF8(config.language()); |
[email protected] | 427ff10 | 2013-11-26 23:45:40 | [diff] [blame] | 1020 | const blink::WebString web_id = |
| 1021 | blink::WebString::fromUTF8(config.id()); |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 1022 | |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 1023 | scoped_ptr<WebInbandTextTrackImpl> web_inband_text_track( |
henriks | 9cce5fa | 2014-12-12 09:35:30 | [diff] [blame] | 1024 | new WebInbandTextTrackImpl(web_kind, web_label, web_language, web_id)); |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 1025 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1026 | scoped_ptr<TextTrack> text_track(new TextTrackImpl( |
dcheng | 652f5ff | 2015-12-27 08:54:00 | [diff] [blame^] | 1027 | main_task_runner_, client_, std::move(web_inband_text_track))); |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 1028 | |
dcheng | 652f5ff | 2015-12-27 08:54:00 | [diff] [blame^] | 1029 | done_cb.Run(std::move(text_track)); |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 1030 | } |
| 1031 | |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 1032 | void WebMediaPlayerImpl::OnHidden() { |
| 1033 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1034 | |
| 1035 | #if !defined(OS_ANDROID) |
| 1036 | // Suspend/Resume is enabled by default on Android. |
| 1037 | if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 1038 | switches::kEnableMediaSuspend)) { |
| 1039 | return; |
| 1040 | } |
| 1041 | #endif // !defined(OS_ANDROID) |
| 1042 | |
| 1043 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 1044 | switches::kDisableMediaSuspend)) { |
| 1045 | return; |
| 1046 | } |
| 1047 | |
| 1048 | if (!pipeline_.IsRunning()) |
| 1049 | return; |
| 1050 | |
| 1051 | if (resuming_ || seeking_) { |
| 1052 | pending_suspend_ = true; |
| 1053 | return; |
| 1054 | } |
| 1055 | |
| 1056 | if (pending_resume_) { |
| 1057 | pending_resume_ = false; |
| 1058 | return; |
| 1059 | } |
| 1060 | |
| 1061 | Suspend(); |
| 1062 | } |
| 1063 | |
| 1064 | void WebMediaPlayerImpl::Suspend() { |
| 1065 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1066 | CHECK(!suspended_); |
| 1067 | suspended_ = true; |
| 1068 | suspending_ = true; |
| 1069 | pipeline_.Suspend( |
| 1070 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineSuspended)); |
| 1071 | } |
| 1072 | |
| 1073 | void WebMediaPlayerImpl::OnShown() { |
| 1074 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1075 | |
| 1076 | #if !defined(OS_ANDROID) |
| 1077 | // Suspend/Resume is enabled by default on Android. |
| 1078 | if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 1079 | switches::kEnableMediaSuspend)) { |
| 1080 | return; |
| 1081 | } |
| 1082 | #endif // !defined(OS_ANDROID) |
| 1083 | |
| 1084 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 1085 | switches::kDisableMediaSuspend)) { |
| 1086 | return; |
| 1087 | } |
| 1088 | |
| 1089 | if (!pipeline_.IsRunning()) |
| 1090 | return; |
| 1091 | |
| 1092 | if (suspending_) { |
| 1093 | pending_resume_ = true; |
| 1094 | return; |
| 1095 | } |
| 1096 | |
| 1097 | if (pending_suspend_) { |
| 1098 | pending_suspend_ = false; |
| 1099 | return; |
| 1100 | } |
| 1101 | |
| 1102 | // We may not be suspended if we were not yet subscribed or the pipeline was |
| 1103 | // not yet started when OnHidden() fired. |
| 1104 | if (!suspended_) |
| 1105 | return; |
| 1106 | |
| 1107 | Resume(); |
| 1108 | } |
| 1109 | |
| 1110 | void WebMediaPlayerImpl::Resume() { |
| 1111 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1112 | CHECK(suspended_); |
| 1113 | CHECK(!resuming_); |
| 1114 | |
| 1115 | // If there was a time change pending when we suspended (which can happen when |
| 1116 | // we suspend immediately after a seek), surface it after resuming. |
| 1117 | bool time_changed = pending_time_change_; |
| 1118 | pending_time_change_ = false; |
| 1119 | |
| 1120 | if (seeking_ || pending_seek_) { |
| 1121 | if (pending_seek_) { |
| 1122 | seek_time_ = pending_seek_time_; |
| 1123 | pending_seek_ = false; |
| 1124 | pending_seek_time_ = base::TimeDelta(); |
| 1125 | } |
| 1126 | time_changed = true; |
| 1127 | } else { |
| 1128 | // It is safe to call GetCurrentFrameTimestamp() because VFC is stopped |
| 1129 | // during Suspend(). It won't be started again until after Resume() is |
| 1130 | // called. |
| 1131 | seek_time_ = compositor_->GetCurrentFrameTimestamp(); |
| 1132 | } |
| 1133 | |
| 1134 | if (chunk_demuxer_) |
| 1135 | chunk_demuxer_->StartWaitingForSeek(seek_time_); |
| 1136 | |
| 1137 | resuming_ = true; |
| 1138 | pipeline_.Resume(CreateRenderer(), seek_time_, |
| 1139 | BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked, |
| 1140 | time_changed)); |
| 1141 | } |
| 1142 | |
[email protected] | fee8a90 | 2014-06-03 13:43:36 | [diff] [blame] | 1143 | void WebMediaPlayerImpl::DataSourceInitialized(bool success) { |
hubbe | d82bed5 | 2015-12-15 23:07:16 | [diff] [blame] | 1144 | DVLOG(1) << __FUNCTION__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1145 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | a941529 | 2012-01-19 19:55:20 | [diff] [blame] | 1146 | |
[email protected] | d250190da3b | 2012-07-23 22:57:30 | [diff] [blame] | 1147 | if (!success) { |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 1148 | SetNetworkState(WebMediaPlayer::NetworkStateFormatError); |
[email protected] | a941529 | 2012-01-19 19:55:20 | [diff] [blame] | 1149 | return; |
| 1150 | } |
| 1151 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 1152 | StartPipeline(); |
[email protected] | a941529 | 2012-01-19 19:55:20 | [diff] [blame] | 1153 | } |
| 1154 | |
[email protected] | 122f4025 | 2012-06-12 05:01:56 | [diff] [blame] | 1155 | void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) { |
hubbe | d82bed5 | 2015-12-15 23:07:16 | [diff] [blame] | 1156 | DVLOG(1) << __FUNCTION__; |
[email protected] | 122f4025 | 2012-06-12 05:01:56 | [diff] [blame] | 1157 | if (!is_downloading && network_state_ == WebMediaPlayer::NetworkStateLoading) |
| 1158 | SetNetworkState(WebMediaPlayer::NetworkStateIdle); |
| 1159 | else if (is_downloading && network_state_ == WebMediaPlayer::NetworkStateIdle) |
| 1160 | SetNetworkState(WebMediaPlayer::NetworkStateLoading); |
| 1161 | media_log_->AddEvent( |
| 1162 | media_log_->CreateBooleanEvent( |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1163 | MediaLogEvent::NETWORK_ACTIVITY_SET, |
[email protected] | 122f4025 | 2012-06-12 05:01:56 | [diff] [blame] | 1164 | "is_downloading_data", is_downloading)); |
| 1165 | } |
| 1166 | |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 1167 | scoped_ptr<Renderer> WebMediaPlayerImpl::CreateRenderer() { |
| 1168 | return renderer_factory_->CreateRenderer( |
| 1169 | media_task_runner_, worker_task_runner_, audio_source_provider_.get(), |
| 1170 | compositor_); |
| 1171 | } |
| 1172 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 1173 | void WebMediaPlayerImpl::StartPipeline() { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1174 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1175 | |
xhwang | e8c4181a | 2014-12-06 08:10:01 | [diff] [blame] | 1176 | Demuxer::EncryptedMediaInitDataCB encrypted_media_init_data_cb = |
| 1177 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnEncryptedMediaInitData); |
[email protected] | 2b57e2e | 2014-05-09 11:07:25 | [diff] [blame] | 1178 | |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1179 | // Figure out which demuxer to use. |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 1180 | if (load_type_ != LoadTypeMediaSource) { |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1181 | DCHECK(!chunk_demuxer_); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1182 | DCHECK(data_source_); |
| 1183 | |
j.isorce | f6778e65 | 2015-11-16 17:14:25 | [diff] [blame] | 1184 | #if !defined(MEDIA_DISABLE_FFMPEG) |
xhwang | e8c4181a | 2014-12-06 08:10:01 | [diff] [blame] | 1185 | demuxer_.reset(new FFmpegDemuxer(media_task_runner_, data_source_.get(), |
| 1186 | encrypted_media_init_data_cb, media_log_)); |
j.isorce | f6778e65 | 2015-11-16 17:14:25 | [diff] [blame] | 1187 | #else |
| 1188 | OnPipelineError(PipelineStatus::DEMUXER_ERROR_COULD_NOT_OPEN); |
| 1189 | return; |
| 1190 | #endif |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1191 | } else { |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1192 | DCHECK(!chunk_demuxer_); |
| 1193 | DCHECK(!data_source_); |
| 1194 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1195 | chunk_demuxer_ = new ChunkDemuxer( |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 1196 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDemuxerOpened), |
wolenetz | 1fb319d | 2015-07-14 17:49:04 | [diff] [blame] | 1197 | encrypted_media_init_data_cb, media_log_, true); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1198 | demuxer_.reset(chunk_demuxer_); |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1199 | } |
| 1200 | |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1201 | // ... and we're ready to go! |
[email protected] | d228aeec | 2014-06-20 19:16:49 | [diff] [blame] | 1202 | seeking_ = true; |
xhwang | f94a634d | 2014-10-22 22:07:27 | [diff] [blame] | 1203 | |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 1204 | // TODO(sandersd): On Android, defer Start() if the tab is not visible. |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 1205 | pipeline_.Start( |
sandersd | 1e49fb6 | 2015-12-12 01:18:06 | [diff] [blame] | 1206 | demuxer_.get(), CreateRenderer(), |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1207 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineEnded), |
| 1208 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineError), |
[email protected] | 4e98219 | 2014-06-21 13:35:45 | [diff] [blame] | 1209 | BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked, false), |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 1210 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineMetadata), |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 1211 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged), |
xhwang | 5980590 | 2014-08-29 01:44:15 | [diff] [blame] | 1212 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDurationChanged), |
jrummell | 74fc4f94 | 2015-03-02 22:48:27 | [diff] [blame] | 1213 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnAddTextTrack), |
| 1214 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnWaitingForDecryptionKey)); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state) { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 1218 | DVLOG(1) << __FUNCTION__ << "(" << state << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1219 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1220 | network_state_ = state; |
| 1221 | // Always notify to ensure client has the latest value. |
[email protected] | ce70c98 | 2013-12-20 17:04:32 | [diff] [blame] | 1222 | client_->networkStateChanged(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state) { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 1226 | DVLOG(1) << __FUNCTION__ << "(" << state << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1227 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1228 | |
[email protected] | fee8a90 | 2014-06-03 13:43:36 | [diff] [blame] | 1229 | if (state == WebMediaPlayer::ReadyStateHaveEnoughData && data_source_ && |
| 1230 | data_source_->assume_fully_buffered() && |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1231 | network_state_ == WebMediaPlayer::NetworkStateLoading) |
| 1232 | SetNetworkState(WebMediaPlayer::NetworkStateLoaded); |
| 1233 | |
| 1234 | ready_state_ = state; |
| 1235 | // Always notify to ensure client has the latest value. |
[email protected] | ce70c98 | 2013-12-20 17:04:32 | [diff] [blame] | 1236 | client_->readyStateChanged(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1237 | } |
| 1238 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 1239 | blink::WebAudioSourceProvider* WebMediaPlayerImpl::audioSourceProvider() { |
[email protected] | ff875be5 | 2013-06-02 23:47:38 | [diff] [blame] | 1240 | return audio_source_provider_.get(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1241 | } |
| 1242 | |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1243 | double WebMediaPlayerImpl::GetPipelineDuration() const { |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 1244 | base::TimeDelta duration = pipeline_.GetMediaDuration(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1245 | |
| 1246 | // Return positive infinity if the resource is unbounded. |
| 1247 | // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1248 | if (duration == kInfiniteDuration()) |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1249 | return std::numeric_limits<double>::infinity(); |
| 1250 | |
| 1251 | return duration.InSecondsF(); |
| 1252 | } |
| 1253 | |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 1254 | void WebMediaPlayerImpl::OnDurationChanged() { |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1255 | if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) |
| 1256 | return; |
| 1257 | |
[email protected] | ce70c98 | 2013-12-20 17:04:32 | [diff] [blame] | 1258 | client_->durationChanged(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1259 | } |
| 1260 | |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 1261 | void WebMediaPlayerImpl::OnNaturalSizeChanged(gfx::Size size) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1262 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 739847c0 | 2014-01-16 00:12:25 | [diff] [blame] | 1263 | DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing); |
| 1264 | TRACE_EVENT0("media", "WebMediaPlayerImpl::OnNaturalSizeChanged"); |
| 1265 | |
| 1266 | media_log_->AddEvent( |
| 1267 | media_log_->CreateVideoSizeSetEvent(size.width(), size.height())); |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 1268 | pipeline_metadata_.natural_size = size; |
[email protected] | 739847c0 | 2014-01-16 00:12:25 | [diff] [blame] | 1269 | |
| 1270 | client_->sizeChanged(); |
| 1271 | } |
| 1272 | |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 1273 | void WebMediaPlayerImpl::OnOpacityChanged(bool opaque) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 1274 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 1275 | DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing); |
| 1276 | |
| 1277 | opaque_ = opaque; |
jbauman | 952274d | 2015-09-10 23:23:36 | [diff] [blame] | 1278 | // Modify content opaqueness of cc::Layer directly so that |
| 1279 | // SetContentsOpaqueIsFixed is ignored. |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 1280 | if (video_weblayer_) |
jbauman | 952274d | 2015-09-10 23:23:36 | [diff] [blame] | 1281 | video_weblayer_->layer()->SetContentsOpaque(opaque_); |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 1282 | } |
| 1283 | |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1284 | static void GetCurrentFrameAndSignal( |
| 1285 | VideoFrameCompositor* compositor, |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1286 | scoped_refptr<VideoFrame>* video_frame_out, |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1287 | base::WaitableEvent* event) { |
| 1288 | TRACE_EVENT0("media", "GetCurrentFrameAndSignal"); |
dalecurtis | 44ce4de | 2015-05-11 18:42:07 | [diff] [blame] | 1289 | *video_frame_out = compositor->GetCurrentFrameAndUpdateIfStale(); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1290 | event->Signal(); |
| 1291 | } |
| 1292 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1293 | scoped_refptr<VideoFrame> |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1294 | WebMediaPlayerImpl::GetCurrentFrameFromCompositor() { |
| 1295 | TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor"); |
| 1296 | if (compositor_task_runner_->BelongsToCurrentThread()) |
dalecurtis | 44ce4de | 2015-05-11 18:42:07 | [diff] [blame] | 1297 | return compositor_->GetCurrentFrameAndUpdateIfStale(); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1298 | |
| 1299 | // Use a posted task and waitable event instead of a lock otherwise |
| 1300 | // WebGL/Canvas can see different content than what the compositor is seeing. |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1301 | scoped_refptr<VideoFrame> video_frame; |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1302 | base::WaitableEvent event(false, false); |
| 1303 | compositor_task_runner_->PostTask(FROM_HERE, |
| 1304 | base::Bind(&GetCurrentFrameAndSignal, |
| 1305 | base::Unretained(compositor_), |
| 1306 | &video_frame, |
| 1307 | &event)); |
| 1308 | event.Wait(); |
| 1309 | return video_frame; |
| 1310 | } |
| 1311 | |
dalecurtis | 2e50e6f6 | 2015-01-09 02:42:52 | [diff] [blame] | 1312 | void WebMediaPlayerImpl::UpdatePausedTime() { |
| 1313 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1314 | |
| 1315 | // pause() may be called after playback has ended and the HTMLMediaElement |
| 1316 | // requires that currentTime() == duration() after ending. We want to ensure |
| 1317 | // |paused_time_| matches currentTime() in this case or a future seek() may |
| 1318 | // incorrectly discard what it thinks is a seek to the existing time. |
| 1319 | paused_time_ = |
| 1320 | ended_ ? pipeline_.GetMediaDuration() : pipeline_.GetMediaTime(); |
| 1321 | } |
| 1322 | |
dalecurtis | 0f0097a | 2015-12-01 17:40:47 | [diff] [blame] | 1323 | void WebMediaPlayerImpl::NotifyPlaybackStarted() { |
| 1324 | if (delegate_) |
| 1325 | delegate_->DidPlay(this); |
| 1326 | if (!memory_usage_reporting_timer_.IsRunning()) { |
| 1327 | memory_usage_reporting_timer_.Start(FROM_HERE, |
| 1328 | base::TimeDelta::FromSeconds(2), this, |
| 1329 | &WebMediaPlayerImpl::ReportMemoryUsage); |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | void WebMediaPlayerImpl::NotifyPlaybackPaused() { |
| 1334 | if (delegate_) |
| 1335 | delegate_->DidPause(this); |
| 1336 | memory_usage_reporting_timer_.Stop(); |
| 1337 | ReportMemoryUsage(); |
| 1338 | } |
| 1339 | |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 1340 | void WebMediaPlayerImpl::ReportMemoryUsage() { |
| 1341 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1342 | |
wdzierzanowski | fd4cd91c5 | 2015-12-02 23:50:20 | [diff] [blame] | 1343 | // About base::Unretained() usage below: We destroy |demuxer_| on the main |
| 1344 | // thread. Before that, however, ~WebMediaPlayerImpl() posts a task to the |
| 1345 | // media thread and waits for it to finish. Hence, the GetMemoryUsage() task |
| 1346 | // posted here must finish earlier. |
| 1347 | |
| 1348 | if (demuxer_) { |
| 1349 | base::PostTaskAndReplyWithResult( |
| 1350 | media_task_runner_.get(), FROM_HERE, |
| 1351 | base::Bind(&Demuxer::GetMemoryUsage, base::Unretained(demuxer_.get())), |
| 1352 | base::Bind(&WebMediaPlayerImpl::FinishMemoryUsageReport, AsWeakPtr())); |
| 1353 | } else { |
| 1354 | FinishMemoryUsageReport(0); |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | void WebMediaPlayerImpl::FinishMemoryUsageReport(int64_t demuxer_memory_usage) { |
| 1359 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1360 | |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 1361 | const PipelineStatistics stats = pipeline_.GetStatistics(); |
| 1362 | const int64_t current_memory_usage = |
| 1363 | stats.audio_memory_usage + stats.video_memory_usage + |
| 1364 | (data_source_ ? data_source_->GetMemoryUsage() : 0) + |
wdzierzanowski | fd4cd91c5 | 2015-12-02 23:50:20 | [diff] [blame] | 1365 | demuxer_memory_usage; |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 1366 | |
| 1367 | DVLOG(2) << "Memory Usage -- Audio: " << stats.audio_memory_usage |
| 1368 | << ", Video: " << stats.video_memory_usage << ", DataSource: " |
| 1369 | << (data_source_ ? data_source_->GetMemoryUsage() : 0) |
wdzierzanowski | fd4cd91c5 | 2015-12-02 23:50:20 | [diff] [blame] | 1370 | << ", Demuxer: " << demuxer_memory_usage; |
dalecurtis | 83266c7 | 2015-10-29 18:43:20 | [diff] [blame] | 1371 | |
| 1372 | const int64_t delta = current_memory_usage - last_reported_memory_usage_; |
| 1373 | last_reported_memory_usage_ = current_memory_usage; |
| 1374 | adjust_allocated_memory_cb_.Run(delta); |
| 1375 | } |
| 1376 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1377 | } // namespace media |