[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> |
[email protected] | 47b06ceb | 2010-08-04 22:41:11 | [diff] [blame] | 8 | #include <limits> |
[email protected] | d82b18ae | 2011-03-23 21:28:59 | [diff] [blame] | 9 | #include <string> |
[email protected] | cbdc8c2b | 2012-06-16 01:27:36 | [diff] [blame] | 10 | #include <vector> |
[email protected] | 47b06ceb | 2010-08-04 22:41:11 | [diff] [blame] | 11 | |
[email protected] | 08273c7b | 2011-09-17 00:33:51 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 13 | #include "base/callback.h" |
[email protected] | 7502ef1 | 2014-01-25 01:19:27 | [diff] [blame] | 14 | #include "base/callback_helpers.h" |
[email protected] | ca04095f | 2014-02-07 10:23:56 | [diff] [blame] | 15 | #include "base/debug/alias.h" |
[email protected] | 926f8fd | 2013-04-12 20:27:53 | [diff] [blame] | 16 | #include "base/debug/crash_logging.h" |
[email protected] | bfc05f2 | 2013-10-19 17:55:16 | [diff] [blame] | 17 | #include "base/debug/trace_event.h" |
dalecurtis | dfe0527f2 | 2014-10-07 20:52:01 | [diff] [blame] | 18 | #include "base/float_util.h" |
[email protected] | 7ccb707 | 2013-06-10 20:56:28 | [diff] [blame] | 19 | #include "base/message_loop/message_loop_proxy.h" |
[email protected] | b0b258f | 2011-11-08 00:34:23 | [diff] [blame] | 20 | #include "base/metrics/histogram.h" |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 21 | #include "base/single_thread_task_runner.h" |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 22 | #include "base/synchronization/waitable_event.h" |
[email protected] | 3856462 | 2014-08-19 02:47:18 | [diff] [blame] | 23 | #include "cc/blink/web_layer_impl.h" |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 24 | #include "cc/layers/video_layer.h" |
[email protected] | 6523b24 | 2013-03-13 11:10:07 | [diff] [blame] | 25 | #include "gpu/GLES2/gl2extchromium.h" |
[email protected] | df41e25 | 2014-02-03 23:39:50 | [diff] [blame] | 26 | #include "gpu/command_buffer/common/mailbox_holder.h" |
[email protected] | e4fc09e | 2012-04-06 03:17:44 | [diff] [blame] | 27 | #include "media/audio/null_audio_sink.h" |
[email protected] | f9a42874 | 2014-03-28 04:20:26 | [diff] [blame] | 28 | #include "media/base/audio_hardware_config.h" |
[email protected] | 2eccbed | 2014-01-10 05:15:53 | [diff] [blame] | 29 | #include "media/base/bind_to_current_loop.h" |
[email protected] | 32da100 | 2010-03-03 21:57:35 | [diff] [blame] | 30 | #include "media/base/limits.h" |
[email protected] | 090f731 | 2011-08-05 23:26:40 | [diff] [blame] | 31 | #include "media/base/media_log.h" |
[email protected] | d399866c | 2012-01-18 09:30:32 | [diff] [blame] | 32 | #include "media/base/pipeline.h" |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 33 | #include "media/base/text_renderer.h" |
[email protected] | e81283bb | 2010-08-31 18:01:21 | [diff] [blame] | 34 | #include "media/base/video_frame.h" |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 35 | #include "media/blink/buffered_data_source.h" |
| 36 | #include "media/blink/encrypted_media_player_support.h" |
| 37 | #include "media/blink/texttrack_impl.h" |
| 38 | #include "media/blink/webaudiosourceprovider_impl.h" |
| 39 | #include "media/blink/webinbandtexttrack_impl.h" |
| 40 | #include "media/blink/webmediaplayer_delegate.h" |
| 41 | #include "media/blink/webmediaplayer_params.h" |
| 42 | #include "media/blink/webmediaplayer_util.h" |
| 43 | #include "media/blink/webmediasource_impl.h" |
[email protected] | 37136d16 | 2012-04-09 23:39:19 | [diff] [blame] | 44 | #include "media/filters/audio_renderer_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_audio_decoder.h" |
| 47 | #include "media/filters/ffmpeg_demuxer.h" |
| 48 | #include "media/filters/ffmpeg_video_decoder.h" |
[email protected] | 1cad880 | 2013-08-13 16:54:32 | [diff] [blame] | 49 | #include "media/filters/gpu_video_accelerator_factories.h" |
[email protected] | b9a407e | 2013-07-24 07:30:02 | [diff] [blame] | 50 | #include "media/filters/gpu_video_decoder.h" |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 51 | #include "media/filters/opus_audio_decoder.h" |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 52 | #include "media/filters/renderer_impl.h" |
[email protected] | cc3933d | 2013-11-26 03:41:02 | [diff] [blame] | 53 | #include "media/filters/video_renderer_impl.h" |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 54 | #include "media/filters/vpx_video_decoder.h" |
[email protected] | 745746d | 2013-08-23 02:09:16 | [diff] [blame] | 55 | #include "third_party/WebKit/public/platform/WebMediaSource.h" |
[email protected] | c1088446 | 2013-05-30 00:22:09 | [diff] [blame] | 56 | #include "third_party/WebKit/public/platform/WebRect.h" |
| 57 | #include "third_party/WebKit/public/platform/WebSize.h" |
| 58 | #include "third_party/WebKit/public/platform/WebString.h" |
| 59 | #include "third_party/WebKit/public/platform/WebURL.h" |
[email protected] | 8050465 | 2014-04-18 04:41:50 | [diff] [blame] | 60 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | 65b8528 | 2014-03-31 23:32:31 | [diff] [blame] | 61 | #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 62 | #include "third_party/WebKit/public/web/WebView.h" |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 63 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 64 | using blink::WebCanvas; |
| 65 | using blink::WebMediaPlayer; |
| 66 | using blink::WebRect; |
| 67 | using blink::WebSize; |
| 68 | using blink::WebString; |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 69 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 70 | namespace { |
| 71 | |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 72 | // Limits the range of playback rate. |
| 73 | // |
| 74 | // TODO(kylep): Revisit these. |
| 75 | // |
| 76 | // Vista has substantially lower performance than XP or Windows7. If you speed |
| 77 | // up a video too much, it can't keep up, and rendering stops updating except on |
| 78 | // the time bar. For really high speeds, audio becomes a bottleneck and we just |
| 79 | // use up the data we have, which may not achieve the speed requested, but will |
| 80 | // not crash the tab. |
| 81 | // |
| 82 | // A very slow speed, ie 0.00000001x, causes the machine to lock up. (It seems |
| 83 | // like a busy loop). It gets unresponsive, although its not completely dead. |
| 84 | // |
| 85 | // Also our timers are not very accurate (especially for ogg), which becomes |
| 86 | // evident at low speeds and on Vista. Since other speeds are risky and outside |
| 87 | // 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] | 88 | const double kMinRate = 0.0625; |
| 89 | const double kMaxRate = 16.0; |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 90 | |
[email protected] | 47c345b | 2014-07-22 10:31:57 | [diff] [blame] | 91 | class SyncPointClientImpl : public media::VideoFrame::SyncPointClient { |
| 92 | public: |
| 93 | explicit SyncPointClientImpl( |
| 94 | blink::WebGraphicsContext3D* web_graphics_context) |
| 95 | : web_graphics_context_(web_graphics_context) {} |
dcheng | c24565478f | 2014-10-21 12:23:27 | [diff] [blame] | 96 | ~SyncPointClientImpl() override {} |
| 97 | uint32 InsertSyncPoint() override { |
[email protected] | 47c345b | 2014-07-22 10:31:57 | [diff] [blame] | 98 | return web_graphics_context_->insertSyncPoint(); |
| 99 | } |
dcheng | c24565478f | 2014-10-21 12:23:27 | [diff] [blame] | 100 | void WaitSyncPoint(uint32 sync_point) override { |
[email protected] | 47c345b | 2014-07-22 10:31:57 | [diff] [blame] | 101 | web_graphics_context_->waitSyncPoint(sync_point); |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | blink::WebGraphicsContext3D* web_graphics_context_; |
| 106 | }; |
| 107 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 108 | } // namespace |
| 109 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 110 | namespace media { |
[email protected] | add5177 | 2009-06-11 18:25:17 | [diff] [blame] | 111 | |
[email protected] | 6683e1b | 2014-04-10 01:45:38 | [diff] [blame] | 112 | class BufferedDataSourceHostImpl; |
| 113 | |
[email protected] | a888463 | 2012-06-29 03:05:34 | [diff] [blame] | 114 | #define COMPILE_ASSERT_MATCHING_ENUM(name) \ |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 115 | COMPILE_ASSERT(static_cast<int>(WebMediaPlayer::CORSMode ## name) == \ |
[email protected] | a888463 | 2012-06-29 03:05:34 | [diff] [blame] | 116 | static_cast<int>(BufferedResourceLoader::k ## name), \ |
[email protected] | a5a0110 | 2012-06-06 17:01:24 | [diff] [blame] | 117 | mismatching_enums) |
| 118 | COMPILE_ASSERT_MATCHING_ENUM(Unspecified); |
| 119 | COMPILE_ASSERT_MATCHING_ENUM(Anonymous); |
| 120 | COMPILE_ASSERT_MATCHING_ENUM(UseCredentials); |
| 121 | #undef COMPILE_ASSERT_MATCHING_ENUM |
| 122 | |
[email protected] | 2c539b8 | 2012-08-18 04:10:19 | [diff] [blame] | 123 | #define BIND_TO_RENDER_LOOP(function) \ |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 124 | (DCHECK(main_task_runner_->BelongsToCurrentThread()), \ |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 125 | BindToCurrentLoop(base::Bind(function, AsWeakPtr()))) |
[email protected] | 5983adb | 2012-10-24 00:12:00 | [diff] [blame] | 126 | |
[email protected] | 4e98219 | 2014-06-21 13:35:45 | [diff] [blame] | 127 | #define BIND_TO_RENDER_LOOP1(function, arg1) \ |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 128 | (DCHECK(main_task_runner_->BelongsToCurrentThread()), \ |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 129 | BindToCurrentLoop(base::Bind(function, AsWeakPtr(), arg1))) |
[email protected] | 4e98219 | 2014-06-21 13:35:45 | [diff] [blame] | 130 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 131 | static void LogMediaSourceError(const scoped_refptr<MediaLog>& media_log, |
[email protected] | 2ef9960 | 2012-12-08 04:22:21 | [diff] [blame] | 132 | const std::string& error) { |
| 133 | media_log->AddEvent(media_log->CreateMediaSourceErrorEvent(error)); |
| 134 | } |
| 135 | |
[email protected] | 5b5bb9d | 2010-10-22 19:57:36 | [diff] [blame] | 136 | WebMediaPlayerImpl::WebMediaPlayerImpl( |
[email protected] | 35b2a97 | 2014-04-04 15:50:22 | [diff] [blame] | 137 | blink::WebLocalFrame* frame, |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 138 | blink::WebMediaPlayerClient* client, |
[email protected] | b897665 | 2011-10-26 23:46:55 | [diff] [blame] | 139 | base::WeakPtr<WebMediaPlayerDelegate> delegate, |
xhwang | f94a634d | 2014-10-22 22:07:27 | [diff] [blame] | 140 | scoped_ptr<Renderer> renderer, |
[email protected] | e82b2bd | 2013-01-02 17:47:57 | [diff] [blame] | 141 | const WebMediaPlayerParams& params) |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 142 | : frame_(frame), |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 143 | network_state_(WebMediaPlayer::NetworkStateEmpty), |
| 144 | ready_state_(WebMediaPlayer::ReadyStateHaveNothing), |
acolwell | a5081a4 | 2014-08-28 23:42:52 | [diff] [blame] | 145 | preload_(BufferedDataSource::AUTO), |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 146 | main_task_runner_(base::MessageLoopProxy::current()), |
acolwell | 755d12d | 2014-08-30 01:09:19 | [diff] [blame] | 147 | media_task_runner_(params.media_task_runner()), |
| 148 | media_log_(params.media_log()), |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 149 | pipeline_(media_task_runner_, media_log_.get()), |
[email protected] | f988d9b | 2014-07-25 00:35:43 | [diff] [blame] | 150 | load_type_(LoadTypeURL), |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 151 | opaque_(false), |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 152 | paused_(true), |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 153 | seeking_(false), |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 154 | playback_rate_(0.0f), |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 155 | ended_(false), |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 156 | pending_seek_(false), |
[email protected] | b1a975e | 2011-12-21 00:12:39 | [diff] [blame] | 157 | pending_seek_seconds_(0.0f), |
[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), |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 160 | delegate_(delegate), |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 161 | defer_load_cb_(params.defer_load_cb()), |
acolwell | 755d12d | 2014-08-30 01:09:19 | [diff] [blame] | 162 | gpu_factories_(params.gpu_factories()), |
[email protected] | 132dd57c | 2012-08-10 23:24:34 | [diff] [blame] | 163 | supports_save_(true), |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 164 | chunk_demuxer_(NULL), |
acolwell | 755d12d | 2014-08-30 01:09:19 | [diff] [blame] | 165 | compositor_task_runner_(params.compositor_task_runner()), |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 166 | compositor_(new VideoFrameCompositor( |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 167 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnNaturalSizeChanged), |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 168 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnOpacityChanged))), |
[email protected] | 7bce183 | 2014-01-09 00:01:22 | [diff] [blame] | 169 | text_track_index_(0), |
acolwell | 755d12d | 2014-08-30 01:09:19 | [diff] [blame] | 170 | encrypted_media_support_( |
| 171 | params.CreateEncryptedMediaPlayerSupport(client)), |
xhwang | f94a634d | 2014-10-22 22:07:27 | [diff] [blame] | 172 | audio_hardware_config_(params.audio_hardware_config()), |
| 173 | renderer_(renderer.Pass()) { |
acolwell | e2ea518 | 2014-08-25 23:01:07 | [diff] [blame] | 174 | DCHECK(encrypted_media_support_); |
| 175 | |
acolwell | 755d12d | 2014-08-30 01:09:19 | [diff] [blame] | 176 | // Threaded compositing isn't enabled universally yet. |
dcheng | d3c6060 | 2014-09-03 17:14:37 | [diff] [blame] | 177 | if (!compositor_task_runner_.get()) |
acolwell | 755d12d | 2014-08-30 01:09:19 | [diff] [blame] | 178 | compositor_task_runner_ = base::MessageLoopProxy::current(); |
| 179 | |
[email protected] | c93eb0a6 | 2011-08-09 22:47:24 | [diff] [blame] | 180 | media_log_->AddEvent( |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 181 | media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_CREATED)); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 182 | |
xhwang | f94a634d | 2014-10-22 22:07:27 | [diff] [blame] | 183 | // TODO(xhwang): When we use an external Renderer, many methods won't work, |
| 184 | // e.g. GetCurrentFrameFromCompositor(). Fix this in a future CL. |
| 185 | if (renderer_) |
| 186 | return; |
| 187 | |
[email protected] | c50edb96 | 2013-10-19 18:05:07 | [diff] [blame] | 188 | // |gpu_factories_| requires that its entry points be called on its |
[email protected] | b639a83 | 2013-12-20 21:12:39 | [diff] [blame] | 189 | // |GetTaskRunner()|. Since |pipeline_| will own decoders created from the |
[email protected] | c50edb96 | 2013-10-19 18:05:07 | [diff] [blame] | 190 | // factories, require that their message loops are identical. |
dcheng | f70d95e | 2014-08-27 02:06:28 | [diff] [blame] | 191 | DCHECK(!gpu_factories_.get() || |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 192 | (gpu_factories_->GetTaskRunner() == media_task_runner_.get())); |
[email protected] | c50edb96 | 2013-10-19 18:05:07 | [diff] [blame] | 193 | |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 194 | // Use the null sink if no sink was provided. |
[email protected] | 4a91488 | 2013-01-10 00:43:48 | [diff] [blame] | 195 | audio_source_provider_ = new WebAudioSourceProviderImpl( |
[email protected] | ff875be5 | 2013-06-02 23:47:38 | [diff] [blame] | 196 | params.audio_renderer_sink().get() |
| 197 | ? params.audio_renderer_sink() |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 198 | : new NullAudioSink(media_task_runner_)); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 201 | WebMediaPlayerImpl::~WebMediaPlayerImpl() { |
[email protected] | ce70c98 | 2013-12-20 17:04:32 | [diff] [blame] | 202 | client_->setWebLayer(NULL); |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 203 | |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 204 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | c93eb0a6 | 2011-08-09 22:47:24 | [diff] [blame] | 205 | media_log_->AddEvent( |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 206 | media_log_->CreateEvent(MediaLogEvent::WEBMEDIAPLAYER_DESTROYED)); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 207 | |
dalecurtis | 6d269ed | 2014-09-11 21:18:13 | [diff] [blame] | 208 | if (delegate_) |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 209 | delegate_->PlayerGone(this); |
| 210 | |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 211 | // Abort any pending IO so stopping the pipeline doesn't get blocked. |
| 212 | if (data_source_) |
| 213 | data_source_->Abort(); |
| 214 | if (chunk_demuxer_) { |
| 215 | chunk_demuxer_->Shutdown(); |
| 216 | chunk_demuxer_ = NULL; |
| 217 | } |
| 218 | |
| 219 | gpu_factories_ = NULL; |
| 220 | |
| 221 | // Make sure to kill the pipeline so there's no more media threads running. |
| 222 | // Note: stopping the pipeline might block for a long time. |
| 223 | base::WaitableEvent waiter(false, false); |
| 224 | pipeline_.Stop( |
| 225 | base::Bind(&base::WaitableEvent::Signal, base::Unretained(&waiter))); |
| 226 | waiter.Wait(); |
| 227 | |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 228 | compositor_task_runner_->DeleteSoon(FROM_HERE, compositor_); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 229 | } |
| 230 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 231 | void WebMediaPlayerImpl::load(LoadType load_type, const blink::WebURL& url, |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 232 | CORSMode cors_mode) { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 233 | DVLOG(1) << __FUNCTION__ << "(" << load_type << ", " << url << ", " |
| 234 | << cors_mode << ")"; |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 235 | if (!defer_load_cb_.is_null()) { |
| 236 | defer_load_cb_.Run(base::Bind( |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 237 | &WebMediaPlayerImpl::DoLoad, AsWeakPtr(), load_type, url, cors_mode)); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 238 | return; |
| 239 | } |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 240 | DoLoad(load_type, url, cors_mode); |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 241 | } |
| 242 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 243 | void WebMediaPlayerImpl::DoLoad(LoadType load_type, |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 244 | const blink::WebURL& url, |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 245 | CORSMode cors_mode) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 246 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 247 | |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 248 | GURL gurl(url); |
[email protected] | e2f6bba | 2014-06-09 20:08:07 | [diff] [blame] | 249 | ReportMediaSchemeUma(gurl); |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 250 | |
[email protected] | 926f8fd | 2013-04-12 20:27:53 | [diff] [blame] | 251 | // Set subresource URL for crash reporting. |
| 252 | base::debug::SetCrashKeyValue("subresource_url", gurl.spec()); |
| 253 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 254 | load_type_ = load_type; |
| 255 | |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 256 | SetNetworkState(WebMediaPlayer::NetworkStateLoading); |
| 257 | SetReadyState(WebMediaPlayer::ReadyStateHaveNothing); |
| 258 | media_log_->AddEvent(media_log_->CreateLoadEvent(url.spec())); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 259 | |
| 260 | // Media source pipelines can start immediately. |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 261 | if (load_type == LoadTypeMediaSource) { |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 262 | supports_save_ = false; |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 263 | StartPipeline(); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 264 | return; |
| 265 | } |
| 266 | |
| 267 | // Otherwise it's a regular request which requires resolving the URL first. |
| 268 | data_source_.reset(new BufferedDataSource( |
[email protected] | fee8a90 | 2014-06-03 13:43:36 | [diff] [blame] | 269 | url, |
| 270 | static_cast<BufferedResourceLoader::CORSMode>(cors_mode), |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 271 | main_task_runner_, |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 272 | frame_, |
| 273 | media_log_.get(), |
[email protected] | 6683e1b | 2014-04-10 01:45:38 | [diff] [blame] | 274 | &buffered_data_source_host_, |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 275 | base::Bind(&WebMediaPlayerImpl::NotifyDownloading, AsWeakPtr()))); |
| 276 | data_source_->Initialize( |
[email protected] | fee8a90 | 2014-06-03 13:43:36 | [diff] [blame] | 277 | base::Bind(&WebMediaPlayerImpl::DataSourceInitialized, AsWeakPtr())); |
[email protected] | 09c6022 | 2014-08-07 16:42:31 | [diff] [blame] | 278 | data_source_->SetPreload(preload_); |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 281 | void WebMediaPlayerImpl::play() { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 282 | DVLOG(1) << __FUNCTION__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 283 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 284 | |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 285 | paused_ = false; |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 286 | pipeline_.SetPlaybackRate(playback_rate_); |
[email protected] | 039b754 | 2013-10-17 22:06:25 | [diff] [blame] | 287 | if (data_source_) |
| 288 | data_source_->MediaIsPlaying(); |
[email protected] | 090f731 | 2011-08-05 23:26:40 | [diff] [blame] | 289 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 290 | media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PLAY)); |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 291 | |
dalecurtis | 6d269ed | 2014-09-11 21:18:13 | [diff] [blame] | 292 | if (delegate_ && playback_rate_ > 0) |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 293 | delegate_->DidPlay(this); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 294 | } |
| 295 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 296 | void WebMediaPlayerImpl::pause() { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 297 | DVLOG(1) << __FUNCTION__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 298 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 299 | |
dalecurtis | 6d269ed | 2014-09-11 21:18:13 | [diff] [blame] | 300 | const bool was_already_paused = paused_ || playback_rate_ == 0; |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 301 | paused_ = true; |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 302 | pipeline_.SetPlaybackRate(0.0f); |
[email protected] | 039b754 | 2013-10-17 22:06:25 | [diff] [blame] | 303 | if (data_source_) |
| 304 | data_source_->MediaIsPaused(); |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 305 | paused_time_ = pipeline_.GetMediaTime(); |
[email protected] | 090f731 | 2011-08-05 23:26:40 | [diff] [blame] | 306 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 307 | media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PAUSE)); |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 308 | |
dalecurtis | 6d269ed | 2014-09-11 21:18:13 | [diff] [blame] | 309 | if (!was_already_paused && delegate_) |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 310 | delegate_->DidPause(this); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 311 | } |
| 312 | |
[email protected] | 574a1d6 | 2009-07-17 03:23:46 | [diff] [blame] | 313 | bool WebMediaPlayerImpl::supportsSave() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 314 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 132dd57c | 2012-08-10 23:24:34 | [diff] [blame] | 315 | return supports_save_; |
[email protected] | 574a1d6 | 2009-07-17 03:23:46 | [diff] [blame] | 316 | } |
| 317 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 318 | void WebMediaPlayerImpl::seek(double seconds) { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 319 | DVLOG(1) << __FUNCTION__ << "(" << seconds << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 320 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 321 | |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 322 | ended_ = false; |
| 323 | |
[email protected] | 1bb66680 | 2013-11-28 06:12:08 | [diff] [blame] | 324 | if (ready_state_ > WebMediaPlayer::ReadyStateHaveMetadata) |
| 325 | SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata); |
| 326 | |
[email protected] | bb8a70b | 2013-06-26 06:23:35 | [diff] [blame] | 327 | base::TimeDelta seek_time = ConvertSecondsToTimestamp(seconds); |
| 328 | |
[email protected] | d228aeec | 2014-06-20 19:16:49 | [diff] [blame] | 329 | if (seeking_) { |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 330 | pending_seek_ = true; |
| 331 | pending_seek_seconds_ = seconds; |
[email protected] | efe7cd2 | 2012-09-12 23:55:01 | [diff] [blame] | 332 | if (chunk_demuxer_) |
[email protected] | bb8a70b | 2013-06-26 06:23:35 | [diff] [blame] | 333 | chunk_demuxer_->CancelPendingSeek(seek_time); |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 334 | return; |
| 335 | } |
| 336 | |
[email protected] | c93eb0a6 | 2011-08-09 22:47:24 | [diff] [blame] | 337 | media_log_->AddEvent(media_log_->CreateSeekEvent(seconds)); |
| 338 | |
[email protected] | 44ff37c0 | 2009-10-24 01:03:03 | [diff] [blame] | 339 | // Update our paused time. |
Bartosz Fabianowski | 36c6832 | 2014-11-13 13:48:37 | [diff] [blame] | 340 | if (paused_) |
| 341 | paused_time_ = seek_time; |
[email protected] | 44ff37c0 | 2009-10-24 01:03:03 | [diff] [blame] | 342 | |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 343 | seeking_ = true; |
| 344 | |
[email protected] | efe7cd2 | 2012-09-12 23:55:01 | [diff] [blame] | 345 | if (chunk_demuxer_) |
[email protected] | bb8a70b | 2013-06-26 06:23:35 | [diff] [blame] | 346 | chunk_demuxer_->StartWaitingForSeek(seek_time); |
[email protected] | 020fba3 | 2011-06-29 16:37:46 | [diff] [blame] | 347 | |
[email protected] | 44ff37c0 | 2009-10-24 01:03:03 | [diff] [blame] | 348 | // Kick off the asynchronous seek! |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 349 | pipeline_.Seek( |
[email protected] | 44ff37c0 | 2009-10-24 01:03:03 | [diff] [blame] | 350 | seek_time, |
[email protected] | 4e98219 | 2014-06-21 13:35:45 | [diff] [blame] | 351 | BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked, true)); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 352 | } |
| 353 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 354 | void WebMediaPlayerImpl::setRate(double rate) { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 355 | DVLOG(1) << __FUNCTION__ << "(" << rate << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 356 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 357 | |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 358 | // TODO(kylep): Remove when support for negatives is added. Also, modify the |
| 359 | // following checks so rewind uses reasonable values also. |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 360 | if (rate < 0.0) |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 361 | return; |
| 362 | |
| 363 | // Limit rates to reasonable values by clamping. |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 364 | if (rate != 0.0) { |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 365 | if (rate < kMinRate) |
| 366 | rate = kMinRate; |
| 367 | else if (rate > kMaxRate) |
| 368 | rate = kMaxRate; |
dalecurtis | bb761e1 | 2014-09-12 01:16:35 | [diff] [blame] | 369 | if (playback_rate_ == 0 && !paused_ && delegate_) |
dalecurtis | 6d269ed | 2014-09-11 21:18:13 | [diff] [blame] | 370 | delegate_->DidPlay(this); |
dalecurtis | bb761e1 | 2014-09-12 01:16:35 | [diff] [blame] | 371 | } else if (playback_rate_ != 0 && !paused_ && delegate_) { |
dalecurtis | 6d269ed | 2014-09-11 21:18:13 | [diff] [blame] | 372 | delegate_->DidPause(this); |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 373 | } |
| 374 | |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 375 | playback_rate_ = rate; |
| 376 | if (!paused_) { |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 377 | pipeline_.SetPlaybackRate(rate); |
[email protected] | 039b754 | 2013-10-17 22:06:25 | [diff] [blame] | 378 | if (data_source_) |
| 379 | data_source_->MediaPlaybackRateChanged(rate); |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 380 | } |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 381 | } |
| 382 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 383 | void WebMediaPlayerImpl::setVolume(double volume) { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 384 | DVLOG(1) << __FUNCTION__ << "(" << volume << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 385 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 386 | |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 387 | pipeline_.SetVolume(volume); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 388 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 389 | |
[email protected] | 23a8b1d8 | 2011-04-05 16:28:20 | [diff] [blame] | 390 | #define COMPILE_ASSERT_MATCHING_ENUM(webkit_name, chromium_name) \ |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 391 | COMPILE_ASSERT(static_cast<int>(WebMediaPlayer::webkit_name) == \ |
acolwell | a5081a4 | 2014-08-28 23:42:52 | [diff] [blame] | 392 | static_cast<int>(BufferedDataSource::chromium_name), \ |
[email protected] | 474c37a | 2011-08-23 20:24:52 | [diff] [blame] | 393 | mismatching_enums) |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 394 | COMPILE_ASSERT_MATCHING_ENUM(PreloadNone, NONE); |
| 395 | COMPILE_ASSERT_MATCHING_ENUM(PreloadMetaData, METADATA); |
| 396 | COMPILE_ASSERT_MATCHING_ENUM(PreloadAuto, AUTO); |
[email protected] | a5a0110 | 2012-06-06 17:01:24 | [diff] [blame] | 397 | #undef COMPILE_ASSERT_MATCHING_ENUM |
[email protected] | 23a8b1d8 | 2011-04-05 16:28:20 | [diff] [blame] | 398 | |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 399 | void WebMediaPlayerImpl::setPreload(WebMediaPlayer::Preload preload) { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 400 | DVLOG(1) << __FUNCTION__ << "(" << preload << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 401 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 402 | |
acolwell | a5081a4 | 2014-08-28 23:42:52 | [diff] [blame] | 403 | preload_ = static_cast<BufferedDataSource::Preload>(preload); |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 404 | if (data_source_) |
[email protected] | 09c6022 | 2014-08-07 16:42:31 | [diff] [blame] | 405 | data_source_->SetPreload(preload_); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 406 | } |
| 407 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 408 | bool WebMediaPlayerImpl::hasVideo() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 409 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 410 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 411 | return pipeline_metadata_.has_video; |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 412 | } |
| 413 | |
[email protected] | fc367af | 2009-08-14 23:06:35 | [diff] [blame] | 414 | bool WebMediaPlayerImpl::hasAudio() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 415 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | fc367af | 2009-08-14 23:06:35 | [diff] [blame] | 416 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 417 | return pipeline_metadata_.has_audio; |
[email protected] | fc367af | 2009-08-14 23:06:35 | [diff] [blame] | 418 | } |
| 419 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 420 | blink::WebSize WebMediaPlayerImpl::naturalSize() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 421 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 422 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 423 | return blink::WebSize(pipeline_metadata_.natural_size); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 424 | } |
| 425 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 426 | bool WebMediaPlayerImpl::paused() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 427 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 428 | |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 429 | return pipeline_.GetPlaybackRate() == 0.0f; |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 430 | } |
| 431 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 432 | bool WebMediaPlayerImpl::seeking() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 433 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 434 | |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 435 | if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) |
[email protected] | 0acebfa | 2009-08-21 22:45:40 | [diff] [blame] | 436 | return false; |
[email protected] | 67cd505 | 2009-09-10 21:53:22 | [diff] [blame] | 437 | |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 438 | return seeking_; |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 439 | } |
| 440 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 441 | double WebMediaPlayerImpl::duration() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 442 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 443 | |
| 444 | if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) |
| 445 | return std::numeric_limits<double>::quiet_NaN(); |
| 446 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 447 | return GetPipelineDuration(); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | db66d009 | 2014-04-16 07:15:12 | [diff] [blame] | 450 | double WebMediaPlayerImpl::timelineOffset() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 451 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | db66d009 | 2014-04-16 07:15:12 | [diff] [blame] | 452 | |
| 453 | if (pipeline_metadata_.timeline_offset.is_null()) |
| 454 | return std::numeric_limits<double>::quiet_NaN(); |
| 455 | |
| 456 | return pipeline_metadata_.timeline_offset.ToJsTime(); |
| 457 | } |
| 458 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 459 | double WebMediaPlayerImpl::currentTime() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 460 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 461 | DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing); |
| 462 | |
| 463 | // TODO(scherkus): Replace with an explicit ended signal to HTMLMediaElement, |
| 464 | // see http://crbug.com/409280 |
| 465 | if (ended_) |
| 466 | return duration(); |
| 467 | |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 468 | return (paused_ ? paused_time_ : pipeline_.GetMediaTime()).InSecondsF(); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 469 | } |
| 470 | |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 471 | WebMediaPlayer::NetworkState WebMediaPlayerImpl::networkState() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 472 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 473 | return network_state_; |
| 474 | } |
| 475 | |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 476 | WebMediaPlayer::ReadyState WebMediaPlayerImpl::readyState() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 477 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 478 | return ready_state_; |
| 479 | } |
| 480 | |
[email protected] | 02022fc | 2014-05-16 00:05:31 | [diff] [blame] | 481 | blink::WebTimeRanges WebMediaPlayerImpl::buffered() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 482 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 779a832 | 2014-08-22 21:28:37 | [diff] [blame] | 483 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 484 | Ranges<base::TimeDelta> buffered_time_ranges = |
[email protected] | 02022fc | 2014-05-16 00:05:31 | [diff] [blame] | 485 | pipeline_.GetBufferedTimeRanges(); |
[email protected] | 779a832 | 2014-08-22 21:28:37 | [diff] [blame] | 486 | |
| 487 | const base::TimeDelta duration = pipeline_.GetMediaDuration(); |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 488 | if (duration != kInfiniteDuration()) { |
[email protected] | 779a832 | 2014-08-22 21:28:37 | [diff] [blame] | 489 | buffered_data_source_host_.AddBufferedTimeRanges( |
| 490 | &buffered_time_ranges, duration); |
| 491 | } |
[email protected] | 02022fc | 2014-05-16 00:05:31 | [diff] [blame] | 492 | return ConvertToWebTimeRanges(buffered_time_ranges); |
| 493 | } |
| 494 | |
philipj | b0e6f3f | 2014-09-30 09:51:53 | [diff] [blame] | 495 | blink::WebTimeRanges WebMediaPlayerImpl::seekable() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 496 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 497 | |
dalecurtis | 56359cb | 2014-10-28 00:06:29 | [diff] [blame] | 498 | if (ready_state_ < WebMediaPlayer::ReadyStateHaveMetadata) |
philipj | b0e6f3f | 2014-09-30 09:51:53 | [diff] [blame] | 499 | return blink::WebTimeRanges(); |
| 500 | |
dalecurtis | 56359cb | 2014-10-28 00:06:29 | [diff] [blame] | 501 | const double seekable_end = duration(); |
| 502 | |
| 503 | // Allow a special exception for seeks to zero for streaming sources with a |
| 504 | // finite duration; this allows looping to work. |
| 505 | const bool allow_seek_to_zero = data_source_ && data_source_->IsStreaming() && |
| 506 | base::IsFinite(seekable_end); |
| 507 | |
| 508 | // TODO(dalecurtis): Technically this allows seeking on media which return an |
| 509 | // infinite duration so long as DataSource::IsStreaming() is false. While not |
| 510 | // expected, disabling this breaks semi-live players, http://crbug.com/427412. |
| 511 | const blink::WebTimeRange seekable_range( |
| 512 | 0.0, allow_seek_to_zero ? 0.0 : seekable_end); |
philipj | b0e6f3f | 2014-09-30 09:51:53 | [diff] [blame] | 513 | return blink::WebTimeRanges(&seekable_range, 1); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 514 | } |
| 515 | |
[email protected] | 5d2b3e4c | 2014-05-12 23:27:30 | [diff] [blame] | 516 | bool WebMediaPlayerImpl::didLoadingProgress() { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 517 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 6683e1b | 2014-04-10 01:45:38 | [diff] [blame] | 518 | bool pipeline_progress = pipeline_.DidLoadingProgress(); |
| 519 | bool data_progress = buffered_data_source_host_.DidLoadingProgress(); |
| 520 | return pipeline_progress || data_progress; |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 521 | } |
| 522 | |
[email protected] | dd5c797 | 2014-08-21 15:00:37 | [diff] [blame] | 523 | void WebMediaPlayerImpl::paint(blink::WebCanvas* canvas, |
| 524 | const blink::WebRect& rect, |
[email protected] | dd5c797 | 2014-08-21 15:00:37 | [diff] [blame] | 525 | unsigned char alpha, |
| 526 | SkXfermode::Mode mode) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 527 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 528 | TRACE_EVENT0("media", "WebMediaPlayerImpl:paint"); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 529 | |
[email protected] | 5e1502e | 2014-03-24 22:48:44 | [diff] [blame] | 530 | // TODO(scherkus): Clarify paint() API contract to better understand when and |
| 531 | // why it's being called. For example, today paint() is called when: |
| 532 | // - We haven't reached HAVE_CURRENT_DATA and need to paint black |
| 533 | // - We're painting to a canvas |
| 534 | // See http://crbug.com/341225 http://crbug.com/342621 for details. |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 535 | scoped_refptr<VideoFrame> video_frame = |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 536 | GetCurrentFrameFromCompositor(); |
[email protected] | 0fe5d43b | 2014-01-24 23:32:45 | [diff] [blame] | 537 | |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 538 | gfx::Rect gfx_rect(rect); |
[email protected] | f78c3e8 | 2014-08-08 01:24:47 | [diff] [blame] | 539 | |
rileya | 54b8c174 | 2014-09-16 02:20:42 | [diff] [blame] | 540 | skcanvas_video_renderer_.Paint(video_frame, |
[email protected] | 8bad182 | 2014-08-09 05:52:38 | [diff] [blame] | 541 | canvas, |
| 542 | gfx_rect, |
| 543 | alpha, |
[email protected] | dd5c797 | 2014-08-21 15:00:37 | [diff] [blame] | 544 | mode, |
[email protected] | 8bad182 | 2014-08-09 05:52:38 | [diff] [blame] | 545 | pipeline_metadata_.video_rotation); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 546 | } |
[email protected] | 5df5165 | 2009-01-17 00:03:00 | [diff] [blame] | 547 | |
[email protected] | 38259a7a8 | 2009-07-29 21:49:49 | [diff] [blame] | 548 | bool WebMediaPlayerImpl::hasSingleSecurityOrigin() const { |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 549 | if (data_source_) |
| 550 | return data_source_->HasSingleOrigin(); |
[email protected] | fcdb746 | 2009-10-21 21:05:11 | [diff] [blame] | 551 | return true; |
[email protected] | 38259a7a8 | 2009-07-29 21:49:49 | [diff] [blame] | 552 | } |
| 553 | |
[email protected] | 3fe2711 | 2012-06-07 04:00:01 | [diff] [blame] | 554 | bool WebMediaPlayerImpl::didPassCORSAccessCheck() const { |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 555 | if (data_source_) |
| 556 | return data_source_->DidPassCORSAccessCheck(); |
| 557 | return false; |
[email protected] | 3fe2711 | 2012-06-07 04:00:01 | [diff] [blame] | 558 | } |
| 559 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 560 | double WebMediaPlayerImpl::mediaTimeForTimeValue(double timeValue) const { |
[email protected] | e06e16d8 | 2011-05-26 22:13:33 | [diff] [blame] | 561 | return ConvertSecondsToTimestamp(timeValue).InSecondsF(); |
| 562 | } |
| 563 | |
[email protected] | d82b18ae | 2011-03-23 21:28:59 | [diff] [blame] | 564 | unsigned WebMediaPlayerImpl::decodedFrameCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 565 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 566 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 567 | PipelineStatistics stats = pipeline_.GetStatistics(); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 568 | return stats.video_frames_decoded; |
| 569 | } |
| 570 | |
[email protected] | d82b18ae | 2011-03-23 21:28:59 | [diff] [blame] | 571 | unsigned WebMediaPlayerImpl::droppedFrameCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 572 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 573 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 574 | PipelineStatistics stats = pipeline_.GetStatistics(); |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 575 | return stats.video_frames_dropped; |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 576 | } |
| 577 | |
[email protected] | d82b18ae | 2011-03-23 21:28:59 | [diff] [blame] | 578 | unsigned WebMediaPlayerImpl::audioDecodedByteCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 579 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 580 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 581 | PipelineStatistics stats = pipeline_.GetStatistics(); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 582 | return stats.audio_bytes_decoded; |
| 583 | } |
| 584 | |
[email protected] | d82b18ae | 2011-03-23 21:28:59 | [diff] [blame] | 585 | unsigned WebMediaPlayerImpl::videoDecodedByteCount() const { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 586 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 587 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 588 | PipelineStatistics stats = pipeline_.GetStatistics(); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 589 | return stats.video_bytes_decoded; |
| 590 | } |
| 591 | |
[email protected] | 6523b24 | 2013-03-13 11:10:07 | [diff] [blame] | 592 | bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture( |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 593 | blink::WebGraphicsContext3D* web_graphics_context, |
[email protected] | 6523b24 | 2013-03-13 11:10:07 | [diff] [blame] | 594 | unsigned int texture, |
| 595 | unsigned int level, |
| 596 | unsigned int internal_format, |
[email protected] | 9aa2b278 | 2013-05-14 00:13:31 | [diff] [blame] | 597 | unsigned int type, |
[email protected] | 6523b24 | 2013-03-13 11:10:07 | [diff] [blame] | 598 | bool premultiply_alpha, |
| 599 | bool flip_y) { |
[email protected] | bfc05f2 | 2013-10-19 17:55:16 | [diff] [blame] | 600 | TRACE_EVENT0("media", "WebMediaPlayerImpl:copyVideoTextureToPlatformTexture"); |
| 601 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 602 | scoped_refptr<VideoFrame> video_frame = |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 603 | GetCurrentFrameFromCompositor(); |
| 604 | |
dcheng | f70d95e | 2014-08-27 02:06:28 | [diff] [blame] | 605 | if (!video_frame.get()) |
[email protected] | e56f88c7 | 2013-06-25 22:31:29 | [diff] [blame] | 606 | return false; |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 607 | if (video_frame->format() != VideoFrame::NATIVE_TEXTURE) |
[email protected] | e56f88c7 | 2013-06-25 22:31:29 | [diff] [blame] | 608 | return false; |
[email protected] | df41e25 | 2014-02-03 23:39:50 | [diff] [blame] | 609 | |
[email protected] | c37a4cc | 2014-04-30 19:15:28 | [diff] [blame] | 610 | const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder(); |
[email protected] | df41e25 | 2014-02-03 23:39:50 | [diff] [blame] | 611 | if (mailbox_holder->texture_target != GL_TEXTURE_2D) |
[email protected] | e56f88c7 | 2013-06-25 22:31:29 | [diff] [blame] | 612 | return false; |
| 613 | |
[email protected] | df41e25 | 2014-02-03 23:39:50 | [diff] [blame] | 614 | web_graphics_context->waitSyncPoint(mailbox_holder->sync_point); |
[email protected] | c77f272 | 2014-07-22 09:04:32 | [diff] [blame] | 615 | uint32 source_texture = web_graphics_context->createAndConsumeTextureCHROMIUM( |
| 616 | GL_TEXTURE_2D, mailbox_holder->mailbox.name); |
[email protected] | e56f88c7 | 2013-06-25 22:31:29 | [diff] [blame] | 617 | |
| 618 | // The video is stored in a unmultiplied format, so premultiply |
| 619 | // if necessary. |
| 620 | web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM, |
| 621 | premultiply_alpha); |
| 622 | // Application itself needs to take care of setting the right flip_y |
| 623 | // value down to get the expected result. |
| 624 | // flip_y==true means to reverse the video orientation while |
| 625 | // flip_y==false means to keep the intrinsic orientation. |
| 626 | web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, flip_y); |
| 627 | web_graphics_context->copyTextureCHROMIUM(GL_TEXTURE_2D, |
| 628 | source_texture, |
| 629 | texture, |
| 630 | level, |
| 631 | internal_format, |
| 632 | type); |
| 633 | web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, false); |
| 634 | web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM, |
| 635 | false); |
| 636 | |
| 637 | web_graphics_context->deleteTexture(source_texture); |
[email protected] | e56f88c7 | 2013-06-25 22:31:29 | [diff] [blame] | 638 | web_graphics_context->flush(); |
[email protected] | 47c345b | 2014-07-22 10:31:57 | [diff] [blame] | 639 | |
| 640 | SyncPointClientImpl client(web_graphics_context); |
| 641 | video_frame->UpdateReleaseSyncPoint(&client); |
[email protected] | e56f88c7 | 2013-06-25 22:31:29 | [diff] [blame] | 642 | return true; |
[email protected] | 6523b24 | 2013-03-13 11:10:07 | [diff] [blame] | 643 | } |
| 644 | |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 645 | WebMediaPlayer::MediaKeyException |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 646 | WebMediaPlayerImpl::generateKeyRequest(const WebString& key_system, |
| 647 | const unsigned char* init_data, |
| 648 | unsigned init_data_length) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 649 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 7502ef1 | 2014-01-25 01:19:27 | [diff] [blame] | 650 | |
acolwell | e2ea518 | 2014-08-25 23:01:07 | [diff] [blame] | 651 | return encrypted_media_support_->GenerateKeyRequest( |
| 652 | frame_, key_system, init_data, init_data_length); |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 653 | } |
| 654 | |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 655 | WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::addKey( |
| 656 | const WebString& key_system, |
| 657 | const unsigned char* key, |
| 658 | unsigned key_length, |
| 659 | const unsigned char* init_data, |
| 660 | unsigned init_data_length, |
| 661 | const WebString& session_id) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 662 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 663 | |
acolwell | e2ea518 | 2014-08-25 23:01:07 | [diff] [blame] | 664 | return encrypted_media_support_->AddKey( |
| 665 | key_system, key, key_length, init_data, init_data_length, session_id); |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 666 | } |
| 667 | |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 668 | WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::cancelKeyRequest( |
| 669 | const WebString& key_system, |
| 670 | const WebString& session_id) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 671 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 0b9999d | 2014-02-20 02:16:11 | [diff] [blame] | 672 | |
acolwell | e2ea518 | 2014-08-25 23:01:07 | [diff] [blame] | 673 | return encrypted_media_support_->CancelKeyRequest(key_system, session_id); |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 674 | } |
| 675 | |
[email protected] | 7bce183 | 2014-01-09 00:01:22 | [diff] [blame] | 676 | void WebMediaPlayerImpl::setContentDecryptionModule( |
| 677 | blink::WebContentDecryptionModule* cdm) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 678 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 7502ef1 | 2014-01-25 01:19:27 | [diff] [blame] | 679 | |
acolwell | e2ea518 | 2014-08-25 23:01:07 | [diff] [blame] | 680 | encrypted_media_support_->SetContentDecryptionModule(cdm); |
[email protected] | 9ebc3b03f | 2014-08-13 04:01:23 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | void WebMediaPlayerImpl::setContentDecryptionModule( |
| 684 | blink::WebContentDecryptionModule* cdm, |
| 685 | blink::WebContentDecryptionModuleResult result) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 686 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 9ebc3b03f | 2014-08-13 04:01:23 | [diff] [blame] | 687 | |
acolwell | e2ea518 | 2014-08-25 23:01:07 | [diff] [blame] | 688 | encrypted_media_support_->SetContentDecryptionModule(cdm, result); |
[email protected] | 9ebc3b03f | 2014-08-13 04:01:23 | [diff] [blame] | 689 | } |
| 690 | |
[email protected] | 4e98219 | 2014-06-21 13:35:45 | [diff] [blame] | 691 | void WebMediaPlayerImpl::OnPipelineSeeked(bool time_changed, |
| 692 | PipelineStatus status) { |
| 693 | DVLOG(1) << __FUNCTION__ << "(" << time_changed << ", " << status << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 694 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 695 | seeking_ = false; |
| 696 | if (pending_seek_) { |
| 697 | pending_seek_ = false; |
| 698 | seek(pending_seek_seconds_); |
| 699 | return; |
| 700 | } |
| 701 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 702 | if (status != PIPELINE_OK) { |
[email protected] | 10a64d9 | 2012-04-10 21:08:02 | [diff] [blame] | 703 | OnPipelineError(status); |
| 704 | return; |
[email protected] | 9670691 | 2009-07-15 17:18:05 | [diff] [blame] | 705 | } |
[email protected] | 10a64d9 | 2012-04-10 21:08:02 | [diff] [blame] | 706 | |
| 707 | // Update our paused time. |
| 708 | if (paused_) |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 709 | paused_time_ = pipeline_.GetMediaTime(); |
[email protected] | 10a64d9 | 2012-04-10 21:08:02 | [diff] [blame] | 710 | |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 711 | should_notify_time_changed_ = time_changed; |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 712 | } |
| 713 | |
[email protected] | 6954fe1 | 2013-01-03 03:22:32 | [diff] [blame] | 714 | void WebMediaPlayerImpl::OnPipelineEnded() { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 715 | DVLOG(1) << __FUNCTION__; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 716 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
scherkus | d2c745b | 2014-09-04 05:03:40 | [diff] [blame] | 717 | |
| 718 | // Ignore state changes until we've completed all outstanding seeks. |
| 719 | if (seeking_ || pending_seek_) |
| 720 | return; |
| 721 | |
| 722 | ended_ = true; |
[email protected] | ce70c98 | 2013-12-20 17:04:32 | [diff] [blame] | 723 | client_->timeChanged(); |
[email protected] | 57653784 | 2009-08-12 23:52:05 | [diff] [blame] | 724 | } |
| 725 | |
[email protected] | a9590c2 | 2011-03-16 16:57:02 | [diff] [blame] | 726 | void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 727 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 728 | DCHECK_NE(error, PIPELINE_OK); |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 729 | |
| 730 | if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) { |
| 731 | // Any error that occurs before reaching ReadyStateHaveMetadata should |
| 732 | // be considered a format error. |
| 733 | SetNetworkState(WebMediaPlayer::NetworkStateFormatError); |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 734 | return; |
| 735 | } |
| 736 | |
[email protected] | be57ee5 | 2013-05-28 22:27:27 | [diff] [blame] | 737 | SetNetworkState(PipelineErrorToNetworkState(error)); |
[email protected] | a9590c2 | 2011-03-16 16:57:02 | [diff] [blame] | 738 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 739 | if (error == PIPELINE_ERROR_DECRYPT) |
acolwell | e2ea518 | 2014-08-25 23:01:07 | [diff] [blame] | 740 | encrypted_media_support_->OnPipelineDecryptError(); |
[email protected] | db190487d | 2009-07-30 18:51:52 | [diff] [blame] | 741 | } |
| 742 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 743 | void WebMediaPlayerImpl::OnPipelineMetadata( |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 744 | PipelineMetadata metadata) { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 745 | DVLOG(1) << __FUNCTION__; |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 746 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 747 | pipeline_metadata_ = metadata; |
[email protected] | 739847c0 | 2014-01-16 00:12:25 | [diff] [blame] | 748 | |
[email protected] | eddfef9 | 2014-07-18 01:26:47 | [diff] [blame] | 749 | UMA_HISTOGRAM_ENUMERATION("Media.VideoRotation", |
| 750 | metadata.video_rotation, |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 751 | VIDEO_ROTATION_MAX + 1); |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 752 | SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata); |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 753 | |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 754 | if (hasVideo()) { |
| 755 | DCHECK(!video_weblayer_); |
[email protected] | f78c3e8 | 2014-08-08 01:24:47 | [diff] [blame] | 756 | scoped_refptr<cc::VideoLayer> layer = |
| 757 | cc::VideoLayer::Create(compositor_, pipeline_metadata_.video_rotation); |
| 758 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 759 | if (pipeline_metadata_.video_rotation == VIDEO_ROTATION_90 || |
| 760 | pipeline_metadata_.video_rotation == VIDEO_ROTATION_270) { |
[email protected] | f78c3e8 | 2014-08-08 01:24:47 | [diff] [blame] | 761 | gfx::Size size = pipeline_metadata_.natural_size; |
| 762 | pipeline_metadata_.natural_size = gfx::Size(size.height(), size.width()); |
| 763 | } |
| 764 | |
[email protected] | 3856462 | 2014-08-19 02:47:18 | [diff] [blame] | 765 | video_weblayer_.reset(new cc_blink::WebLayerImpl(layer)); |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 766 | video_weblayer_->setOpaque(opaque_); |
[email protected] | a7c4f58 | 2014-04-16 18:44:49 | [diff] [blame] | 767 | client_->setWebLayer(video_weblayer_.get()); |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 768 | } |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 769 | } |
| 770 | |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 771 | void WebMediaPlayerImpl::OnPipelineBufferingStateChanged( |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 772 | BufferingState buffering_state) { |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 773 | DVLOG(1) << __FUNCTION__ << "(" << buffering_state << ")"; |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 774 | |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 775 | // Ignore buffering state changes until we've completed all outstanding seeks. |
| 776 | if (seeking_ || pending_seek_) |
| 777 | return; |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 778 | |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 779 | // TODO(scherkus): Handle other buffering states when Pipeline starts using |
| 780 | // them and translate them ready state changes http://crbug.com/144683 |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 781 | DCHECK_EQ(buffering_state, BUFFERING_HAVE_ENOUGH); |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 782 | SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData); |
| 783 | |
| 784 | // Blink expects a timeChanged() in response to a seek(). |
| 785 | if (should_notify_time_changed_) |
| 786 | client_->timeChanged(); |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 787 | } |
| 788 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 789 | void WebMediaPlayerImpl::OnDemuxerOpened() { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 790 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | ce70c98 | 2013-12-20 17:04:32 | [diff] [blame] | 791 | client_->mediaSourceOpened(new WebMediaSourceImpl( |
[email protected] | 5f811a2d | 2013-03-15 23:54:51 | [diff] [blame] | 792 | chunk_demuxer_, base::Bind(&LogMediaSourceError, media_log_))); |
[email protected] | 81bb332 | 2011-07-21 15:55:50 | [diff] [blame] | 793 | } |
| 794 | |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 795 | void WebMediaPlayerImpl::OnAddTextTrack( |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 796 | const TextTrackConfig& config, |
| 797 | const AddTextTrackDoneCB& done_cb) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 798 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 799 | |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 800 | const WebInbandTextTrackImpl::Kind web_kind = |
| 801 | static_cast<WebInbandTextTrackImpl::Kind>(config.kind()); |
| 802 | const blink::WebString web_label = |
| 803 | blink::WebString::fromUTF8(config.label()); |
| 804 | const blink::WebString web_language = |
| 805 | blink::WebString::fromUTF8(config.language()); |
[email protected] | 427ff10 | 2013-11-26 23:45:40 | [diff] [blame] | 806 | const blink::WebString web_id = |
| 807 | blink::WebString::fromUTF8(config.id()); |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 808 | |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 809 | scoped_ptr<WebInbandTextTrackImpl> web_inband_text_track( |
[email protected] | 427ff10 | 2013-11-26 23:45:40 | [diff] [blame] | 810 | new WebInbandTextTrackImpl(web_kind, web_label, web_language, web_id, |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 811 | text_track_index_++)); |
| 812 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 813 | scoped_ptr<TextTrack> text_track(new TextTrackImpl( |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 814 | main_task_runner_, client_, web_inband_text_track.Pass())); |
[email protected] | 8a56106 | 2013-11-22 01:19:31 | [diff] [blame] | 815 | |
| 816 | done_cb.Run(text_track.Pass()); |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 817 | } |
| 818 | |
[email protected] | fee8a90 | 2014-06-03 13:43:36 | [diff] [blame] | 819 | void WebMediaPlayerImpl::DataSourceInitialized(bool success) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 820 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | a941529 | 2012-01-19 19:55:20 | [diff] [blame] | 821 | |
[email protected] | d250190da3b | 2012-07-23 22:57:30 | [diff] [blame] | 822 | if (!success) { |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 823 | SetNetworkState(WebMediaPlayer::NetworkStateFormatError); |
[email protected] | a941529 | 2012-01-19 19:55:20 | [diff] [blame] | 824 | return; |
| 825 | } |
| 826 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 827 | StartPipeline(); |
[email protected] | a941529 | 2012-01-19 19:55:20 | [diff] [blame] | 828 | } |
| 829 | |
[email protected] | 122f4025 | 2012-06-12 05:01:56 | [diff] [blame] | 830 | void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) { |
| 831 | if (!is_downloading && network_state_ == WebMediaPlayer::NetworkStateLoading) |
| 832 | SetNetworkState(WebMediaPlayer::NetworkStateIdle); |
| 833 | else if (is_downloading && network_state_ == WebMediaPlayer::NetworkStateIdle) |
| 834 | SetNetworkState(WebMediaPlayer::NetworkStateLoading); |
| 835 | media_log_->AddEvent( |
| 836 | media_log_->CreateBooleanEvent( |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 837 | MediaLogEvent::NETWORK_ACTIVITY_SET, |
[email protected] | 122f4025 | 2012-06-12 05:01:56 | [diff] [blame] | 838 | "is_downloading_data", is_downloading)); |
| 839 | } |
| 840 | |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 841 | // TODO(xhwang): Move this to a factory class so that we can create different |
| 842 | // renderers. |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 843 | scoped_ptr<Renderer> WebMediaPlayerImpl::CreateRenderer() { |
| 844 | SetDecryptorReadyCB set_decryptor_ready_cb = |
acolwell | e2ea518 | 2014-08-25 23:01:07 | [diff] [blame] | 845 | encrypted_media_support_->CreateSetDecryptorReadyCB(); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 846 | |
| 847 | // Create our audio decoders and renderer. |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 848 | ScopedVector<AudioDecoder> audio_decoders; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 849 | |
xhwang | ff459d4e | 2014-09-09 04:55:47 | [diff] [blame] | 850 | audio_decoders.push_back(new media::FFmpegAudioDecoder( |
| 851 | media_task_runner_, base::Bind(&LogMediaSourceError, media_log_))); |
| 852 | audio_decoders.push_back(new media::OpusAudioDecoder(media_task_runner_)); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 853 | |
xhwang | ff459d4e | 2014-09-09 04:55:47 | [diff] [blame] | 854 | scoped_ptr<AudioRenderer> audio_renderer( |
| 855 | new AudioRendererImpl(media_task_runner_, |
| 856 | audio_source_provider_.get(), |
| 857 | audio_decoders.Pass(), |
| 858 | set_decryptor_ready_cb, |
| 859 | audio_hardware_config_, |
| 860 | media_log_)); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 861 | |
| 862 | // Create our video decoders and renderer. |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 863 | ScopedVector<VideoDecoder> video_decoders; |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 864 | |
xhwang | ff459d4e | 2014-09-09 04:55:47 | [diff] [blame] | 865 | if (gpu_factories_.get()) |
| 866 | video_decoders.push_back(new GpuVideoDecoder(gpu_factories_)); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 867 | |
| 868 | #if !defined(MEDIA_DISABLE_LIBVPX) |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 869 | video_decoders.push_back(new VpxVideoDecoder(media_task_runner_)); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 870 | #endif // !defined(MEDIA_DISABLE_LIBVPX) |
| 871 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 872 | video_decoders.push_back(new FFmpegVideoDecoder(media_task_runner_)); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 873 | |
xhwang | ff459d4e | 2014-09-09 04:55:47 | [diff] [blame] | 874 | scoped_ptr<VideoRenderer> video_renderer(new VideoRendererImpl( |
| 875 | media_task_runner_, |
| 876 | video_decoders.Pass(), |
| 877 | set_decryptor_ready_cb, |
xhwang | ff459d4e | 2014-09-09 04:55:47 | [diff] [blame] | 878 | true, |
| 879 | media_log_)); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 880 | |
| 881 | // Create renderer. |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 882 | return scoped_ptr<Renderer>(new RendererImpl( |
xhwang | abd95fd | 2014-10-03 07:10:01 | [diff] [blame] | 883 | media_task_runner_, audio_renderer.Pass(), video_renderer.Pass())); |
xhwang | be9da70 | 2014-08-23 21:44:55 | [diff] [blame] | 884 | } |
| 885 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 886 | void WebMediaPlayerImpl::StartPipeline() { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 887 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 888 | |
[email protected] | 0814ac6 | 2013-06-07 21:01:29 | [diff] [blame] | 889 | // Keep track if this is a MSE or non-MSE playback. |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 890 | UMA_HISTOGRAM_BOOLEAN("Media.MSE.Playback", |
| 891 | (load_type_ == LoadTypeMediaSource)); |
[email protected] | 0814ac6 | 2013-06-07 21:01:29 | [diff] [blame] | 892 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 893 | LogCB mse_log_cb; |
| 894 | Demuxer::NeedKeyCB need_key_cb = |
acolwell | e2ea518 | 2014-08-25 23:01:07 | [diff] [blame] | 895 | encrypted_media_support_->CreateNeedKeyCB(); |
[email protected] | 2b57e2e | 2014-05-09 11:07:25 | [diff] [blame] | 896 | |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 897 | // Figure out which demuxer to use. |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 898 | if (load_type_ != LoadTypeMediaSource) { |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 899 | DCHECK(!chunk_demuxer_); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 900 | DCHECK(data_source_); |
| 901 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 902 | demuxer_.reset(new FFmpegDemuxer( |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 903 | media_task_runner_, data_source_.get(), |
acolwell | e2ea518 | 2014-08-25 23:01:07 | [diff] [blame] | 904 | need_key_cb, |
[email protected] | 1e931f6 | 2013-07-31 23:25:52 | [diff] [blame] | 905 | media_log_)); |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 906 | } else { |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 907 | DCHECK(!chunk_demuxer_); |
| 908 | DCHECK(!data_source_); |
| 909 | |
[email protected] | 2b57e2e | 2014-05-09 11:07:25 | [diff] [blame] | 910 | mse_log_cb = base::Bind(&LogMediaSourceError, media_log_); |
| 911 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 912 | chunk_demuxer_ = new ChunkDemuxer( |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame] | 913 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDemuxerOpened), |
acolwell | e2ea518 | 2014-08-25 23:01:07 | [diff] [blame] | 914 | need_key_cb, |
[email protected] | 2b57e2e | 2014-05-09 11:07:25 | [diff] [blame] | 915 | mse_log_cb, |
[email protected] | ae3c42e | 2014-04-30 05:48:05 | [diff] [blame] | 916 | true); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 917 | demuxer_.reset(chunk_demuxer_); |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 918 | } |
| 919 | |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 920 | // ... and we're ready to go! |
[email protected] | d228aeec | 2014-06-20 19:16:49 | [diff] [blame] | 921 | seeking_ = true; |
xhwang | f94a634d | 2014-10-22 22:07:27 | [diff] [blame] | 922 | |
| 923 | if (!renderer_) |
| 924 | renderer_ = CreateRenderer(); |
| 925 | |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 926 | pipeline_.Start( |
xhwang | 5980590 | 2014-08-29 01:44:15 | [diff] [blame] | 927 | demuxer_.get(), |
xhwang | f94a634d | 2014-10-22 22:07:27 | [diff] [blame] | 928 | renderer_.Pass(), |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 929 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineEnded), |
| 930 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineError), |
[email protected] | 4e98219 | 2014-06-21 13:35:45 | [diff] [blame] | 931 | BIND_TO_RENDER_LOOP1(&WebMediaPlayerImpl::OnPipelineSeeked, false), |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 932 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineMetadata), |
[email protected] | ba7d5f9 | 2014-06-24 05:37:40 | [diff] [blame] | 933 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineBufferingStateChanged), |
xhwang | 94c0bd3 | 2014-11-13 20:49:31 | [diff] [blame^] | 934 | base::Bind(&WebMediaPlayerImpl::FrameReady, base::Unretained(this)), |
xhwang | 5980590 | 2014-08-29 01:44:15 | [diff] [blame] | 935 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDurationChanged), |
| 936 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnAddTextTrack)); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state) { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 940 | DVLOG(1) << __FUNCTION__ << "(" << state << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 941 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 942 | network_state_ = state; |
| 943 | // Always notify to ensure client has the latest value. |
[email protected] | ce70c98 | 2013-12-20 17:04:32 | [diff] [blame] | 944 | client_->networkStateChanged(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state) { |
[email protected] | 2a06ca6 | 2014-06-04 13:59:52 | [diff] [blame] | 948 | DVLOG(1) << __FUNCTION__ << "(" << state << ")"; |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 949 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 950 | |
[email protected] | fee8a90 | 2014-06-03 13:43:36 | [diff] [blame] | 951 | if (state == WebMediaPlayer::ReadyStateHaveEnoughData && data_source_ && |
| 952 | data_source_->assume_fully_buffered() && |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 953 | network_state_ == WebMediaPlayer::NetworkStateLoading) |
| 954 | SetNetworkState(WebMediaPlayer::NetworkStateLoaded); |
| 955 | |
| 956 | ready_state_ = state; |
| 957 | // Always notify to ensure client has the latest value. |
[email protected] | ce70c98 | 2013-12-20 17:04:32 | [diff] [blame] | 958 | client_->readyStateChanged(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 959 | } |
| 960 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 961 | blink::WebAudioSourceProvider* WebMediaPlayerImpl::audioSourceProvider() { |
[email protected] | ff875be5 | 2013-06-02 23:47:38 | [diff] [blame] | 962 | return audio_source_provider_.get(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 963 | } |
| 964 | |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 965 | double WebMediaPlayerImpl::GetPipelineDuration() const { |
[email protected] | f6af759 | 2014-02-28 10:09:11 | [diff] [blame] | 966 | base::TimeDelta duration = pipeline_.GetMediaDuration(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 967 | |
| 968 | // Return positive infinity if the resource is unbounded. |
| 969 | // 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] | 970 | if (duration == kInfiniteDuration()) |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 971 | return std::numeric_limits<double>::infinity(); |
| 972 | |
| 973 | return duration.InSecondsF(); |
| 974 | } |
| 975 | |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 976 | void WebMediaPlayerImpl::OnDurationChanged() { |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 977 | if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) |
| 978 | return; |
| 979 | |
[email protected] | ce70c98 | 2013-12-20 17:04:32 | [diff] [blame] | 980 | client_->durationChanged(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 981 | } |
| 982 | |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 983 | void WebMediaPlayerImpl::OnNaturalSizeChanged(gfx::Size size) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 984 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 739847c0 | 2014-01-16 00:12:25 | [diff] [blame] | 985 | DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing); |
| 986 | TRACE_EVENT0("media", "WebMediaPlayerImpl::OnNaturalSizeChanged"); |
| 987 | |
| 988 | media_log_->AddEvent( |
| 989 | media_log_->CreateVideoSizeSetEvent(size.width(), size.height())); |
[email protected] | b887777 | 2014-03-26 20:17:15 | [diff] [blame] | 990 | pipeline_metadata_.natural_size = size; |
[email protected] | 739847c0 | 2014-01-16 00:12:25 | [diff] [blame] | 991 | |
| 992 | client_->sizeChanged(); |
| 993 | } |
| 994 | |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 995 | void WebMediaPlayerImpl::OnOpacityChanged(bool opaque) { |
acolwell | b403494 | 2014-08-28 15:42:43 | [diff] [blame] | 996 | DCHECK(main_task_runner_->BelongsToCurrentThread()); |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 997 | DCHECK_NE(ready_state_, WebMediaPlayer::ReadyStateHaveNothing); |
| 998 | |
| 999 | opaque_ = opaque; |
[email protected] | 75e145a | 2014-04-15 17:44:32 | [diff] [blame] | 1000 | if (video_weblayer_) |
| 1001 | video_weblayer_->setOpaque(opaque_); |
| 1002 | } |
| 1003 | |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1004 | void WebMediaPlayerImpl::FrameReady( |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1005 | const scoped_refptr<VideoFrame>& frame) { |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1006 | compositor_task_runner_->PostTask( |
| 1007 | FROM_HERE, |
| 1008 | base::Bind(&VideoFrameCompositor::UpdateCurrentFrame, |
| 1009 | base::Unretained(compositor_), |
| 1010 | frame)); |
[email protected] | bfc05f2 | 2013-10-19 17:55:16 | [diff] [blame] | 1011 | } |
| 1012 | |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1013 | static void GetCurrentFrameAndSignal( |
| 1014 | VideoFrameCompositor* compositor, |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1015 | scoped_refptr<VideoFrame>* video_frame_out, |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1016 | base::WaitableEvent* event) { |
| 1017 | TRACE_EVENT0("media", "GetCurrentFrameAndSignal"); |
| 1018 | *video_frame_out = compositor->GetCurrentFrame(); |
| 1019 | event->Signal(); |
| 1020 | } |
| 1021 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1022 | scoped_refptr<VideoFrame> |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1023 | WebMediaPlayerImpl::GetCurrentFrameFromCompositor() { |
| 1024 | TRACE_EVENT0("media", "WebMediaPlayerImpl::GetCurrentFrameFromCompositor"); |
| 1025 | if (compositor_task_runner_->BelongsToCurrentThread()) |
| 1026 | return compositor_->GetCurrentFrame(); |
| 1027 | |
| 1028 | // Use a posted task and waitable event instead of a lock otherwise |
| 1029 | // WebGL/Canvas can see different content than what the compositor is seeing. |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1030 | scoped_refptr<VideoFrame> video_frame; |
[email protected] | dd061e1 | 2014-05-06 19:21:22 | [diff] [blame] | 1031 | base::WaitableEvent event(false, false); |
| 1032 | compositor_task_runner_->PostTask(FROM_HERE, |
| 1033 | base::Bind(&GetCurrentFrameAndSignal, |
| 1034 | base::Unretained(compositor_), |
| 1035 | &video_frame, |
| 1036 | &event)); |
| 1037 | event.Wait(); |
| 1038 | return video_frame; |
| 1039 | } |
| 1040 | |
acolwell | 9e0840d | 2014-09-06 19:01:32 | [diff] [blame] | 1041 | } // namespace media |