[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 | |
[email protected] | 5e35a8d | 2013-07-10 19:37:21 | [diff] [blame] | 5 | #include "content/renderer/media/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] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 14 | #include "base/command_line.h" |
[email protected] | 926f8fd | 2013-04-12 20:27:53 | [diff] [blame] | 15 | #include "base/debug/crash_logging.h" |
[email protected] | 7ccb707 | 2013-06-10 20:56:28 | [diff] [blame] | 16 | #include "base/message_loop/message_loop_proxy.h" |
[email protected] | b0b258f | 2011-11-08 00:34:23 | [diff] [blame] | 17 | #include "base/metrics/histogram.h" |
[email protected] | 4ce532f | 2013-03-27 22:03:40 | [diff] [blame] | 18 | #include "base/strings/string_number_conversions.h" |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 19 | #include "base/synchronization/waitable_event.h" |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 20 | #include "cc/layers/video_layer.h" |
[email protected] | ca73adc7 | 2013-07-11 11:37:02 | [diff] [blame] | 21 | #include "content/public/common/content_switches.h" |
[email protected] | 5e35a8d | 2013-07-10 19:37:21 | [diff] [blame] | 22 | #include "content/renderer/media/buffered_data_source.h" |
[email protected] | 61582e8c | 2013-07-14 00:38:33 | [diff] [blame] | 23 | #include "content/renderer/media/crypto/key_systems.h" |
[email protected] | 5e35a8d | 2013-07-10 19:37:21 | [diff] [blame] | 24 | #include "content/renderer/media/texttrack_impl.h" |
| 25 | #include "content/renderer/media/webaudiosourceprovider_impl.h" |
| 26 | #include "content/renderer/media/webinbandtexttrack_impl.h" |
| 27 | #include "content/renderer/media/webmediaplayer_delegate.h" |
| 28 | #include "content/renderer/media/webmediaplayer_params.h" |
| 29 | #include "content/renderer/media/webmediaplayer_util.h" |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame^] | 30 | #include "content/renderer/media/webmediasource_impl.h" |
[email protected] | adab233 | 2013-07-25 18:04:32 | [diff] [blame] | 31 | #include "content/renderer/pepper/pepper_webplugin_impl.h" |
[email protected] | 6523b24 | 2013-03-13 11:10:07 | [diff] [blame] | 32 | #include "gpu/GLES2/gl2extchromium.h" |
[email protected] | e4fc09e | 2012-04-06 03:17:44 | [diff] [blame] | 33 | #include "media/audio/null_audio_sink.h" |
[email protected] | 2c539b8 | 2012-08-18 04:10:19 | [diff] [blame] | 34 | #include "media/base/bind_to_loop.h" |
[email protected] | f8db813 | 2010-12-03 00:27:49 | [diff] [blame] | 35 | #include "media/base/filter_collection.h" |
[email protected] | 32da100 | 2010-03-03 21:57:35 | [diff] [blame] | 36 | #include "media/base/limits.h" |
[email protected] | 090f731 | 2011-08-05 23:26:40 | [diff] [blame] | 37 | #include "media/base/media_log.h" |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 38 | #include "media/base/media_switches.h" |
[email protected] | d399866c | 2012-01-18 09:30:32 | [diff] [blame] | 39 | #include "media/base/pipeline.h" |
[email protected] | e81283bb | 2010-08-31 18:01:21 | [diff] [blame] | 40 | #include "media/base/video_frame.h" |
[email protected] | 37136d16 | 2012-04-09 23:39:19 | [diff] [blame] | 41 | #include "media/filters/audio_renderer_impl.h" |
[email protected] | efe7cd2 | 2012-09-12 23:55:01 | [diff] [blame] | 42 | #include "media/filters/chunk_demuxer.h" |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 43 | #include "media/filters/ffmpeg_audio_decoder.h" |
| 44 | #include "media/filters/ffmpeg_demuxer.h" |
| 45 | #include "media/filters/ffmpeg_video_decoder.h" |
[email protected] | 1cad880 | 2013-08-13 16:54:32 | [diff] [blame] | 46 | #include "media/filters/gpu_video_accelerator_factories.h" |
[email protected] | b9a407e | 2013-07-24 07:30:02 | [diff] [blame] | 47 | #include "media/filters/gpu_video_decoder.h" |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 48 | #include "media/filters/opus_audio_decoder.h" |
[email protected] | d4833b7 | 2012-01-13 22:16:17 | [diff] [blame] | 49 | #include "media/filters/video_renderer_base.h" |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 50 | #include "media/filters/vpx_video_decoder.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" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 55 | #include "third_party/WebKit/public/web/WebMediaSource.h" |
| 56 | #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" |
| 57 | #include "third_party/WebKit/public/web/WebView.h" |
[email protected] | 38aefc31 | 2011-09-20 05:06:37 | [diff] [blame] | 58 | #include "v8/include/v8.h" |
[email protected] | 799fd73 | 2013-05-15 21:18:52 | [diff] [blame] | 59 | #include "webkit/renderer/compositor_bindings/web_layer_impl.h" |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 60 | |
[email protected] | df143bdc | 2009-06-16 17:34:19 | [diff] [blame] | 61 | using WebKit::WebCanvas; |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 62 | using WebKit::WebMediaPlayer; |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 63 | using WebKit::WebRect; |
| 64 | using WebKit::WebSize; |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 65 | using WebKit::WebString; |
[email protected] | a9590c2 | 2011-03-16 16:57:02 | [diff] [blame] | 66 | using media::PipelineStatus; |
[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] | 38aefc31 | 2011-09-20 05:06:37 | [diff] [blame] | 70 | // Amount of extra memory used by each player instance reported to V8. |
| 71 | // It is not exact number -- first, it differs on different platforms, |
| 72 | // and second, it is very hard to calculate. Instead, use some arbitrary |
| 73 | // value that will cause garbage collection from time to time. We don't want |
| 74 | // it to happen on every allocation, but don't want 5k players to sit in memory |
| 75 | // either. Looks that chosen constant achieves both goals, at least for audio |
| 76 | // objects. (Do not worry about video objects yet, JS programs do not create |
| 77 | // thousands of them...) |
| 78 | const int kPlayerExtraMemory = 1024 * 1024; |
| 79 | |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 80 | // Limits the range of playback rate. |
| 81 | // |
| 82 | // TODO(kylep): Revisit these. |
| 83 | // |
| 84 | // Vista has substantially lower performance than XP or Windows7. If you speed |
| 85 | // up a video too much, it can't keep up, and rendering stops updating except on |
| 86 | // the time bar. For really high speeds, audio becomes a bottleneck and we just |
| 87 | // use up the data we have, which may not achieve the speed requested, but will |
| 88 | // not crash the tab. |
| 89 | // |
| 90 | // A very slow speed, ie 0.00000001x, causes the machine to lock up. (It seems |
| 91 | // like a busy loop). It gets unresponsive, although its not completely dead. |
| 92 | // |
| 93 | // Also our timers are not very accurate (especially for ogg), which becomes |
| 94 | // evident at low speeds and on Vista. Since other speeds are risky and outside |
| 95 | // 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] | 96 | const double kMinRate = 0.0625; |
| 97 | const double kMaxRate = 16.0; |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 98 | |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 99 | // Prefix for histograms related to Encrypted Media Extensions. |
| 100 | const char* kMediaEme = "Media.EME."; |
[email protected] | 61582e8c | 2013-07-14 00:38:33 | [diff] [blame] | 101 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 102 | } // namespace |
| 103 | |
[email protected] | 5e35a8d | 2013-07-10 19:37:21 | [diff] [blame] | 104 | namespace content { |
[email protected] | add5177 | 2009-06-11 18:25:17 | [diff] [blame] | 105 | |
[email protected] | a888463 | 2012-06-29 03:05:34 | [diff] [blame] | 106 | #define COMPILE_ASSERT_MATCHING_ENUM(name) \ |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 107 | COMPILE_ASSERT(static_cast<int>(WebMediaPlayer::CORSMode ## name) == \ |
[email protected] | a888463 | 2012-06-29 03:05:34 | [diff] [blame] | 108 | static_cast<int>(BufferedResourceLoader::k ## name), \ |
[email protected] | a5a0110 | 2012-06-06 17:01:24 | [diff] [blame] | 109 | mismatching_enums) |
| 110 | COMPILE_ASSERT_MATCHING_ENUM(Unspecified); |
| 111 | COMPILE_ASSERT_MATCHING_ENUM(Anonymous); |
| 112 | COMPILE_ASSERT_MATCHING_ENUM(UseCredentials); |
| 113 | #undef COMPILE_ASSERT_MATCHING_ENUM |
| 114 | |
[email protected] | 2c539b8 | 2012-08-18 04:10:19 | [diff] [blame] | 115 | #define BIND_TO_RENDER_LOOP(function) \ |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 116 | media::BindToLoop(main_loop_, base::Bind(function, AsWeakPtr())) |
[email protected] | 2c539b8 | 2012-08-18 04:10:19 | [diff] [blame] | 117 | |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 118 | #define BIND_TO_RENDER_LOOP_1(function, arg1) \ |
| 119 | media::BindToLoop(main_loop_, base::Bind(function, AsWeakPtr(), arg1)) |
| 120 | |
[email protected] | 5983adb | 2012-10-24 00:12:00 | [diff] [blame] | 121 | #define BIND_TO_RENDER_LOOP_2(function, arg1, arg2) \ |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 122 | media::BindToLoop(main_loop_, base::Bind(function, AsWeakPtr(), arg1, arg2)) |
[email protected] | 5983adb | 2012-10-24 00:12:00 | [diff] [blame] | 123 | |
[email protected] | 2ef9960 | 2012-12-08 04:22:21 | [diff] [blame] | 124 | static void LogMediaSourceError(const scoped_refptr<media::MediaLog>& media_log, |
| 125 | const std::string& error) { |
| 126 | media_log->AddEvent(media_log->CreateMediaSourceErrorEvent(error)); |
| 127 | } |
| 128 | |
[email protected] | 5b5bb9d | 2010-10-22 19:57:36 | [diff] [blame] | 129 | WebMediaPlayerImpl::WebMediaPlayerImpl( |
[email protected] | da952fd | 2012-01-13 03:49:25 | [diff] [blame] | 130 | WebKit::WebFrame* frame, |
[email protected] | 5b5bb9d | 2010-10-22 19:57:36 | [diff] [blame] | 131 | WebKit::WebMediaPlayerClient* client, |
[email protected] | b897665 | 2011-10-26 23:46:55 | [diff] [blame] | 132 | base::WeakPtr<WebMediaPlayerDelegate> delegate, |
[email protected] | e82b2bd | 2013-01-02 17:47:57 | [diff] [blame] | 133 | const WebMediaPlayerParams& params) |
[email protected] | a941529 | 2012-01-19 19:55:20 | [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), |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 137 | main_loop_(base::MessageLoopProxy::current()), |
[email protected] | c1330c8 | 2013-06-06 02:23:25 | [diff] [blame] | 138 | media_loop_(params.message_loop_proxy()), |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 139 | paused_(true), |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 140 | seeking_(false), |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 141 | playback_rate_(0.0f), |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 142 | pending_seek_(false), |
[email protected] | b1a975e | 2011-12-21 00:12:39 | [diff] [blame] | 143 | pending_seek_seconds_(0.0f), |
[email protected] | 5badb08 | 2010-06-11 17:40:15 | [diff] [blame] | 144 | client_(client), |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 145 | delegate_(delegate), |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 146 | defer_load_cb_(params.defer_load_cb()), |
[email protected] | e82b2bd | 2013-01-02 17:47:57 | [diff] [blame] | 147 | media_log_(params.media_log()), |
[email protected] | c5b1b64 | 2012-03-26 19:40:51 | [diff] [blame] | 148 | accelerated_compositing_reported_(false), |
[email protected] | 9e2269d | 2012-01-07 00:06:21 | [diff] [blame] | 149 | incremented_externally_allocated_memory_(false), |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 150 | gpu_factories_(params.gpu_factories()), |
[email protected] | a888463 | 2012-06-29 03:05:34 | [diff] [blame] | 151 | is_local_source_(false), |
[email protected] | 132dd57c | 2012-08-10 23:24:34 | [diff] [blame] | 152 | supports_save_(true), |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 153 | starting_(false), |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 154 | chunk_demuxer_(NULL), |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 155 | pending_repaint_(false), |
[email protected] | 29037ce5 | 2013-05-06 22:44:16 | [diff] [blame] | 156 | pending_size_change_(false), |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 157 | video_frame_provider_client_(NULL), |
| 158 | text_track_index_(0) { |
[email protected] | c93eb0a6 | 2011-08-09 22:47:24 | [diff] [blame] | 159 | media_log_->AddEvent( |
| 160 | media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_CREATED)); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 161 | |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 162 | pipeline_.reset(new media::Pipeline(media_loop_, media_log_.get())); |
[email protected] | db190487d | 2009-07-30 18:51:52 | [diff] [blame] | 163 | |
[email protected] | e82b2bd | 2013-01-02 17:47:57 | [diff] [blame] | 164 | // Let V8 know we started new thread if we did not do it yet. |
[email protected] | da952fd | 2012-01-13 03:49:25 | [diff] [blame] | 165 | // Made separate task to avoid deletion of player currently being created. |
| 166 | // Also, delaying GC until after player starts gets rid of starting lag -- |
| 167 | // collection happens in parallel with playing. |
| 168 | // |
| 169 | // TODO(enal): remove when we get rid of per-audio-stream thread. |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 170 | main_loop_->PostTask( |
[email protected] | da952fd | 2012-01-13 03:49:25 | [diff] [blame] | 171 | FROM_HERE, |
| 172 | base::Bind(&WebMediaPlayerImpl::IncrementExternallyAllocatedMemory, |
| 173 | AsWeakPtr())); |
| 174 | |
[email protected] | da952fd | 2012-01-13 03:49:25 | [diff] [blame] | 175 | // Also we want to be notified of |main_loop_| destruction. |
[email protected] | fb5af23 | 2013-04-22 22:40:03 | [diff] [blame] | 176 | base::MessageLoop::current()->AddDestructionObserver(this); |
[email protected] | da952fd | 2012-01-13 03:49:25 | [diff] [blame] | 177 | |
[email protected] | 16c242d | 2013-05-31 23:56:47 | [diff] [blame] | 178 | if (WebKit::WebRuntimeFeatures::isLegacyEncryptedMediaEnabled()) { |
[email protected] | abd36b3af | 2012-12-22 03:42:02 | [diff] [blame] | 179 | decryptor_.reset(new ProxyDecryptor( |
[email protected] | 04af4991 | 2013-06-21 22:09:01 | [diff] [blame] | 180 | #if defined(ENABLE_PEPPER_CDMS) |
[email protected] | abd36b3af | 2012-12-22 03:42:02 | [diff] [blame] | 181 | client, |
| 182 | frame, |
[email protected] | 04af4991 | 2013-06-21 22:09:01 | [diff] [blame] | 183 | #endif |
[email protected] | abd36b3af | 2012-12-22 03:42:02 | [diff] [blame] | 184 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnKeyAdded), |
| 185 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnKeyError), |
[email protected] | 7e06e6ac | 2013-06-20 21:34:15 | [diff] [blame] | 186 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnKeyMessage))); |
[email protected] | 47ebb0e | 2012-11-30 08:28:29 | [diff] [blame] | 187 | } |
[email protected] | 893f2828 | 2012-12-14 22:51:35 | [diff] [blame] | 188 | |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 189 | // Use the null sink if no sink was provided. |
[email protected] | 4a91488 | 2013-01-10 00:43:48 | [diff] [blame] | 190 | audio_source_provider_ = new WebAudioSourceProviderImpl( |
[email protected] | ff875be5 | 2013-06-02 23:47:38 | [diff] [blame] | 191 | params.audio_renderer_sink().get() |
| 192 | ? params.audio_renderer_sink() |
[email protected] | c1330c8 | 2013-06-06 02:23:25 | [diff] [blame] | 193 | : new media::NullAudioSink(media_loop_)); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 194 | } |
| 195 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 196 | WebMediaPlayerImpl::~WebMediaPlayerImpl() { |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 197 | SetVideoFrameProviderClient(NULL); |
| 198 | GetClient()->setWebLayer(NULL); |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 199 | |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 200 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | c93eb0a6 | 2011-08-09 22:47:24 | [diff] [blame] | 201 | media_log_->AddEvent( |
| 202 | media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_DESTROYED)); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 203 | |
[email protected] | 9b55d80 | 2013-06-04 02:39:46 | [diff] [blame] | 204 | if (delegate_.get()) |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 205 | delegate_->PlayerGone(this); |
| 206 | |
[email protected] | a3702c5 | 2013-05-09 05:40:30 | [diff] [blame] | 207 | Destroy(); |
| 208 | |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 209 | // Remove destruction observer if we're being destroyed but the main thread is |
| 210 | // still running. |
[email protected] | fb5af23 | 2013-04-22 22:40:03 | [diff] [blame] | 211 | if (base::MessageLoop::current()) |
| 212 | base::MessageLoop::current()->RemoveDestructionObserver(this); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 213 | } |
| 214 | |
[email protected] | daeae72 | 2011-11-15 23:00:58 | [diff] [blame] | 215 | namespace { |
| 216 | |
| 217 | // Helper enum for reporting scheme histograms. |
| 218 | enum URLSchemeForHistogram { |
| 219 | kUnknownURLScheme, |
| 220 | kMissingURLScheme, |
| 221 | kHttpURLScheme, |
| 222 | kHttpsURLScheme, |
| 223 | kFtpURLScheme, |
| 224 | kChromeExtensionURLScheme, |
| 225 | kJavascriptURLScheme, |
| 226 | kFileURLScheme, |
| 227 | kBlobURLScheme, |
| 228 | kDataURLScheme, |
[email protected] | aacd4a4 | 2012-04-11 02:34:48 | [diff] [blame] | 229 | kFileSystemScheme, |
| 230 | kMaxURLScheme = kFileSystemScheme // Must be equal to highest enum value. |
[email protected] | daeae72 | 2011-11-15 23:00:58 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | URLSchemeForHistogram URLScheme(const GURL& url) { |
| 234 | if (!url.has_scheme()) return kMissingURLScheme; |
| 235 | if (url.SchemeIs("http")) return kHttpURLScheme; |
| 236 | if (url.SchemeIs("https")) return kHttpsURLScheme; |
| 237 | if (url.SchemeIs("ftp")) return kFtpURLScheme; |
| 238 | if (url.SchemeIs("chrome-extension")) return kChromeExtensionURLScheme; |
| 239 | if (url.SchemeIs("javascript")) return kJavascriptURLScheme; |
| 240 | if (url.SchemeIs("file")) return kFileURLScheme; |
| 241 | if (url.SchemeIs("blob")) return kBlobURLScheme; |
| 242 | if (url.SchemeIs("data")) return kDataURLScheme; |
[email protected] | aacd4a4 | 2012-04-11 02:34:48 | [diff] [blame] | 243 | if (url.SchemeIs("filesystem")) return kFileSystemScheme; |
[email protected] | daeae72 | 2011-11-15 23:00:58 | [diff] [blame] | 244 | return kUnknownURLScheme; |
| 245 | } |
| 246 | |
| 247 | } // anonymous namespace |
| 248 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame^] | 249 | void WebMediaPlayerImpl::load(LoadType load_type, const WebKit::WebURL& url, |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 250 | CORSMode cors_mode) { |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 251 | if (!defer_load_cb_.is_null()) { |
| 252 | defer_load_cb_.Run(base::Bind( |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame^] | 253 | &WebMediaPlayerImpl::DoLoad, AsWeakPtr(), load_type, url, cors_mode)); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 254 | return; |
| 255 | } |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame^] | 256 | DoLoad(load_type, url, cors_mode); |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 257 | } |
| 258 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame^] | 259 | void WebMediaPlayerImpl::DoLoad(LoadType load_type, |
| 260 | const WebKit::WebURL& url, |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 261 | CORSMode cors_mode) { |
| 262 | DCHECK(main_loop_->BelongsToCurrentThread()); |
| 263 | |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 264 | GURL gurl(url); |
| 265 | UMA_HISTOGRAM_ENUMERATION("Media.URLScheme", URLScheme(gurl), kMaxURLScheme); |
| 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 | // Handle any volume/preload changes that occurred before load(). |
| 273 | setVolume(GetClient()->volume()); |
| 274 | setPreload(GetClient()->preload()); |
| 275 | |
| 276 | SetNetworkState(WebMediaPlayer::NetworkStateLoading); |
| 277 | SetReadyState(WebMediaPlayer::ReadyStateHaveNothing); |
| 278 | media_log_->AddEvent(media_log_->CreateLoadEvent(url.spec())); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 279 | |
| 280 | // Media source pipelines can start immediately. |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame^] | 281 | if (load_type == LoadTypeMediaSource) { |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 282 | supports_save_ = false; |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame^] | 283 | StartPipeline(); |
[email protected] | d726eddc | 2013-07-02 22:25:55 | [diff] [blame] | 284 | return; |
| 285 | } |
| 286 | |
| 287 | // Otherwise it's a regular request which requires resolving the URL first. |
| 288 | data_source_.reset(new BufferedDataSource( |
| 289 | main_loop_, |
| 290 | frame_, |
| 291 | media_log_.get(), |
| 292 | base::Bind(&WebMediaPlayerImpl::NotifyDownloading, AsWeakPtr()))); |
| 293 | data_source_->Initialize( |
| 294 | url, static_cast<BufferedResourceLoader::CORSMode>(cors_mode), |
| 295 | base::Bind( |
| 296 | &WebMediaPlayerImpl::DataSourceInitialized, |
| 297 | AsWeakPtr(), gurl)); |
| 298 | |
| 299 | is_local_source_ = !gurl.SchemeIs("http") && !gurl.SchemeIs("https"); |
[email protected] | 62e5e68 | 2013-03-07 23:53:24 | [diff] [blame] | 300 | } |
| 301 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 302 | void WebMediaPlayerImpl::play() { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 303 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 304 | |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 305 | paused_ = false; |
| 306 | pipeline_->SetPlaybackRate(playback_rate_); |
[email protected] | 090f731 | 2011-08-05 23:26:40 | [diff] [blame] | 307 | |
[email protected] | 24d75c7 | 2011-08-09 17:53:45 | [diff] [blame] | 308 | media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PLAY)); |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 309 | |
[email protected] | 9b55d80 | 2013-06-04 02:39:46 | [diff] [blame] | 310 | if (delegate_.get()) |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 311 | delegate_->DidPlay(this); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 312 | } |
| 313 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 314 | void WebMediaPlayerImpl::pause() { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 315 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 316 | |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 317 | paused_ = true; |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 318 | pipeline_->SetPlaybackRate(0.0f); |
[email protected] | f7c0ada0 | 2012-07-23 08:42:50 | [diff] [blame] | 319 | paused_time_ = pipeline_->GetMediaTime(); |
[email protected] | 090f731 | 2011-08-05 23:26:40 | [diff] [blame] | 320 | |
[email protected] | 24d75c7 | 2011-08-09 17:53:45 | [diff] [blame] | 321 | media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PAUSE)); |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 322 | |
[email protected] | 9b55d80 | 2013-06-04 02:39:46 | [diff] [blame] | 323 | if (delegate_.get()) |
[email protected] | baff451 | 2011-10-19 18:21:07 | [diff] [blame] | 324 | delegate_->DidPause(this); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 325 | } |
| 326 | |
[email protected] | 574a1d6 | 2009-07-17 03:23:46 | [diff] [blame] | 327 | bool WebMediaPlayerImpl::supportsFullscreen() const { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 328 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | 574a1d6 | 2009-07-17 03:23:46 | [diff] [blame] | 329 | return true; |
| 330 | } |
| 331 | |
| 332 | bool WebMediaPlayerImpl::supportsSave() const { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 333 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | 132dd57c | 2012-08-10 23:24:34 | [diff] [blame] | 334 | return supports_save_; |
[email protected] | 574a1d6 | 2009-07-17 03:23:46 | [diff] [blame] | 335 | } |
| 336 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 337 | void WebMediaPlayerImpl::seek(double seconds) { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 338 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 339 | |
[email protected] | bb8a70b | 2013-06-26 06:23:35 | [diff] [blame] | 340 | base::TimeDelta seek_time = ConvertSecondsToTimestamp(seconds); |
| 341 | |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 342 | if (starting_ || seeking_) { |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 343 | pending_seek_ = true; |
| 344 | pending_seek_seconds_ = seconds; |
[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_->CancelPendingSeek(seek_time); |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 347 | return; |
| 348 | } |
| 349 | |
[email protected] | c93eb0a6 | 2011-08-09 22:47:24 | [diff] [blame] | 350 | media_log_->AddEvent(media_log_->CreateSeekEvent(seconds)); |
| 351 | |
[email protected] | 44ff37c0 | 2009-10-24 01:03:03 | [diff] [blame] | 352 | // Update our paused time. |
[email protected] | 7b268b7a | 2011-11-18 19:52:08 | [diff] [blame] | 353 | if (paused_) |
[email protected] | 44ff37c0 | 2009-10-24 01:03:03 | [diff] [blame] | 354 | paused_time_ = seek_time; |
[email protected] | 44ff37c0 | 2009-10-24 01:03:03 | [diff] [blame] | 355 | |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 356 | seeking_ = true; |
| 357 | |
[email protected] | efe7cd2 | 2012-09-12 23:55:01 | [diff] [blame] | 358 | if (chunk_demuxer_) |
[email protected] | bb8a70b | 2013-06-26 06:23:35 | [diff] [blame] | 359 | chunk_demuxer_->StartWaitingForSeek(seek_time); |
[email protected] | 020fba3 | 2011-06-29 16:37:46 | [diff] [blame] | 360 | |
[email protected] | 44ff37c0 | 2009-10-24 01:03:03 | [diff] [blame] | 361 | // Kick off the asynchronous seek! |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 362 | pipeline_->Seek( |
[email protected] | 44ff37c0 | 2009-10-24 01:03:03 | [diff] [blame] | 363 | seek_time, |
[email protected] | 2c539b8 | 2012-08-18 04:10:19 | [diff] [blame] | 364 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineSeek)); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 365 | } |
| 366 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 367 | void WebMediaPlayerImpl::setRate(double rate) { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 368 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 369 | |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 370 | // TODO(kylep): Remove when support for negatives is added. Also, modify the |
| 371 | // following checks so rewind uses reasonable values also. |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 372 | if (rate < 0.0) |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 373 | return; |
| 374 | |
| 375 | // Limit rates to reasonable values by clamping. |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 376 | if (rate != 0.0) { |
[email protected] | 378f0b7 | 2009-08-11 17:11:42 | [diff] [blame] | 377 | if (rate < kMinRate) |
| 378 | rate = kMinRate; |
| 379 | else if (rate > kMaxRate) |
| 380 | rate = kMaxRate; |
| 381 | } |
| 382 | |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame] | 383 | playback_rate_ = rate; |
| 384 | if (!paused_) { |
| 385 | pipeline_->SetPlaybackRate(rate); |
| 386 | } |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 387 | } |
| 388 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 389 | void WebMediaPlayerImpl::setVolume(double volume) { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 390 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 391 | |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 392 | pipeline_->SetVolume(volume); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 393 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 394 | |
[email protected] | 23a8b1d8 | 2011-04-05 16:28:20 | [diff] [blame] | 395 | #define COMPILE_ASSERT_MATCHING_ENUM(webkit_name, chromium_name) \ |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 396 | COMPILE_ASSERT(static_cast<int>(WebMediaPlayer::webkit_name) == \ |
[email protected] | 5e35a8d | 2013-07-10 19:37:21 | [diff] [blame] | 397 | static_cast<int>(content::chromium_name), \ |
[email protected] | 474c37a | 2011-08-23 20:24:52 | [diff] [blame] | 398 | mismatching_enums) |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 399 | COMPILE_ASSERT_MATCHING_ENUM(PreloadNone, NONE); |
| 400 | COMPILE_ASSERT_MATCHING_ENUM(PreloadMetaData, METADATA); |
| 401 | COMPILE_ASSERT_MATCHING_ENUM(PreloadAuto, AUTO); |
[email protected] | a5a0110 | 2012-06-06 17:01:24 | [diff] [blame] | 402 | #undef COMPILE_ASSERT_MATCHING_ENUM |
[email protected] | 23a8b1d8 | 2011-04-05 16:28:20 | [diff] [blame] | 403 | |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 404 | void WebMediaPlayerImpl::setPreload(WebMediaPlayer::Preload preload) { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 405 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 406 | |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 407 | if (data_source_) |
[email protected] | 5e35a8d | 2013-07-10 19:37:21 | [diff] [blame] | 408 | data_source_->SetPreload(static_cast<content::Preload>(preload)); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 409 | } |
| 410 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 411 | bool WebMediaPlayerImpl::hasVideo() const { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 412 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 413 | |
[email protected] | cee3234 | 2011-03-09 15:58:16 | [diff] [blame] | 414 | return pipeline_->HasVideo(); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 415 | } |
| 416 | |
[email protected] | fc367af | 2009-08-14 23:06:35 | [diff] [blame] | 417 | bool WebMediaPlayerImpl::hasAudio() const { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 418 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | fc367af | 2009-08-14 23:06:35 | [diff] [blame] | 419 | |
[email protected] | cee3234 | 2011-03-09 15:58:16 | [diff] [blame] | 420 | return pipeline_->HasAudio(); |
[email protected] | fc367af | 2009-08-14 23:06:35 | [diff] [blame] | 421 | } |
| 422 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 423 | WebKit::WebSize WebMediaPlayerImpl::naturalSize() const { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 424 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 425 | |
[email protected] | 2975fa53 | 2011-09-19 20:18:33 | [diff] [blame] | 426 | gfx::Size size; |
| 427 | pipeline_->GetNaturalVideoSize(&size); |
| 428 | return WebKit::WebSize(size); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 429 | } |
| 430 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 431 | bool WebMediaPlayerImpl::paused() const { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 432 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 433 | |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 434 | return pipeline_->GetPlaybackRate() == 0.0f; |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 435 | } |
| 436 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 437 | bool WebMediaPlayerImpl::seeking() const { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 438 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 439 | |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 440 | if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) |
[email protected] | 0acebfa | 2009-08-21 22:45:40 | [diff] [blame] | 441 | return false; |
[email protected] | 67cd505 | 2009-09-10 21:53:22 | [diff] [blame] | 442 | |
[email protected] | b3766a2 | 2010-12-22 17:34:13 | [diff] [blame] | 443 | return seeking_; |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 444 | } |
| 445 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 446 | double WebMediaPlayerImpl::duration() const { |
| 447 | DCHECK(main_loop_->BelongsToCurrentThread()); |
| 448 | |
| 449 | if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) |
| 450 | return std::numeric_limits<double>::quiet_NaN(); |
| 451 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 452 | return GetPipelineDuration(); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 453 | } |
| 454 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 455 | double WebMediaPlayerImpl::currentTime() const { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 456 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 457 | return (paused_ ? paused_time_ : pipeline_->GetMediaTime()).InSecondsF(); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 458 | } |
| 459 | |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 460 | WebMediaPlayer::NetworkState WebMediaPlayerImpl::networkState() const { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 461 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 462 | return network_state_; |
| 463 | } |
| 464 | |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 465 | WebMediaPlayer::ReadyState WebMediaPlayerImpl::readyState() const { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 466 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 467 | return ready_state_; |
| 468 | } |
| 469 | |
[email protected] | f70f8e1 | 2010-05-25 21:30:39 | [diff] [blame] | 470 | const WebKit::WebTimeRanges& WebMediaPlayerImpl::buffered() { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 471 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | c566cb79 | 2012-06-20 00:51:52 | [diff] [blame] | 472 | WebKit::WebTimeRanges web_ranges( |
| 473 | ConvertToWebTimeRanges(pipeline_->GetBufferedTimeRanges())); |
[email protected] | 49f7b77 | 2012-05-10 00:45:35 | [diff] [blame] | 474 | buffered_.swap(web_ranges); |
[email protected] | f70f8e1 | 2010-05-25 21:30:39 | [diff] [blame] | 475 | return buffered_; |
| 476 | } |
| 477 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 478 | double WebMediaPlayerImpl::maxTimeSeekable() const { |
| 479 | DCHECK(main_loop_->BelongsToCurrentThread()); |
| 480 | |
| 481 | // If we haven't even gotten to ReadyStateHaveMetadata yet then just |
| 482 | // return 0 so that the seekable range is empty. |
| 483 | if (ready_state_ < WebMediaPlayer::ReadyStateHaveMetadata) |
| 484 | return 0.0; |
| 485 | |
| 486 | // We don't support seeking in streaming media. |
| 487 | if (data_source_ && data_source_->IsStreaming()) |
| 488 | return 0.0; |
[email protected] | 62b5897c | 2013-01-30 22:53:35 | [diff] [blame] | 489 | return duration(); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 490 | } |
| 491 | |
[email protected] | 7ed21be | 2012-05-30 08:09:15 | [diff] [blame] | 492 | bool WebMediaPlayerImpl::didLoadingProgress() const { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 493 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | 7ed21be | 2012-05-30 08:09:15 | [diff] [blame] | 494 | return pipeline_->DidLoadingProgress(); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 495 | } |
| 496 | |
[email protected] | df143bdc | 2009-06-16 17:34:19 | [diff] [blame] | 497 | void WebMediaPlayerImpl::paint(WebCanvas* canvas, |
[email protected] | 4dc43be | 2012-03-27 14:05:07 | [diff] [blame] | 498 | const WebRect& rect, |
[email protected] | 98f60ed | 2013-05-24 23:20:54 | [diff] [blame] | 499 | unsigned char alpha) { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 500 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 501 | |
[email protected] | eaf7d9a | 2012-05-19 06:14:33 | [diff] [blame] | 502 | if (!accelerated_compositing_reported_) { |
| 503 | accelerated_compositing_reported_ = true; |
[email protected] | 3dc8f1b | 2012-05-21 22:54:32 | [diff] [blame] | 504 | // Normally paint() is only called in non-accelerated rendering, but there |
| 505 | // are exceptions such as webgl where compositing is used in the WebView but |
| 506 | // video frames are still rendered to a canvas. |
| 507 | UMA_HISTOGRAM_BOOLEAN( |
| 508 | "Media.AcceleratedCompositingActive", |
| 509 | frame_->view()->isAcceleratedCompositingActive()); |
[email protected] | eaf7d9a | 2012-05-19 06:14:33 | [diff] [blame] | 510 | } |
| 511 | |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 512 | // Avoid locking and potentially blocking the video rendering thread while |
| 513 | // painting in software. |
| 514 | scoped_refptr<media::VideoFrame> video_frame; |
| 515 | { |
| 516 | base::AutoLock auto_lock(lock_); |
| 517 | video_frame = current_frame_; |
| 518 | } |
| 519 | gfx::Rect gfx_rect(rect); |
[email protected] | ff875be5 | 2013-06-02 23:47:38 | [diff] [blame] | 520 | skcanvas_video_renderer_.Paint(video_frame.get(), canvas, gfx_rect, alpha); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 521 | } |
[email protected] | 5df5165 | 2009-01-17 00:03:00 | [diff] [blame] | 522 | |
[email protected] | 38259a7a8 | 2009-07-29 21:49:49 | [diff] [blame] | 523 | bool WebMediaPlayerImpl::hasSingleSecurityOrigin() const { |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 524 | if (data_source_) |
| 525 | return data_source_->HasSingleOrigin(); |
[email protected] | fcdb746 | 2009-10-21 21:05:11 | [diff] [blame] | 526 | return true; |
[email protected] | 38259a7a8 | 2009-07-29 21:49:49 | [diff] [blame] | 527 | } |
| 528 | |
[email protected] | 3fe2711 | 2012-06-07 04:00:01 | [diff] [blame] | 529 | bool WebMediaPlayerImpl::didPassCORSAccessCheck() const { |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 530 | if (data_source_) |
| 531 | return data_source_->DidPassCORSAccessCheck(); |
| 532 | return false; |
[email protected] | 3fe2711 | 2012-06-07 04:00:01 | [diff] [blame] | 533 | } |
| 534 | |
[email protected] | 39bdde3 | 2013-04-17 17:44:20 | [diff] [blame] | 535 | double WebMediaPlayerImpl::mediaTimeForTimeValue(double timeValue) const { |
[email protected] | e06e16d8 | 2011-05-26 22:13:33 | [diff] [blame] | 536 | return ConvertSecondsToTimestamp(timeValue).InSecondsF(); |
| 537 | } |
| 538 | |
[email protected] | d82b18ae | 2011-03-23 21:28:59 | [diff] [blame] | 539 | unsigned WebMediaPlayerImpl::decodedFrameCount() const { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 540 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 541 | |
| 542 | media::PipelineStatistics stats = pipeline_->GetStatistics(); |
| 543 | return stats.video_frames_decoded; |
| 544 | } |
| 545 | |
[email protected] | d82b18ae | 2011-03-23 21:28:59 | [diff] [blame] | 546 | unsigned WebMediaPlayerImpl::droppedFrameCount() const { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 547 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 548 | |
| 549 | media::PipelineStatistics stats = pipeline_->GetStatistics(); |
| 550 | return stats.video_frames_dropped; |
| 551 | } |
| 552 | |
[email protected] | d82b18ae | 2011-03-23 21:28:59 | [diff] [blame] | 553 | unsigned WebMediaPlayerImpl::audioDecodedByteCount() const { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 554 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 555 | |
| 556 | media::PipelineStatistics stats = pipeline_->GetStatistics(); |
| 557 | return stats.audio_bytes_decoded; |
| 558 | } |
| 559 | |
[email protected] | d82b18ae | 2011-03-23 21:28:59 | [diff] [blame] | 560 | unsigned WebMediaPlayerImpl::videoDecodedByteCount() const { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 561 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | 4c51bc66 | 2011-02-16 02:03:16 | [diff] [blame] | 562 | |
| 563 | media::PipelineStatistics stats = pipeline_->GetStatistics(); |
| 564 | return stats.video_bytes_decoded; |
| 565 | } |
| 566 | |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 567 | void WebMediaPlayerImpl::SetVideoFrameProviderClient( |
| 568 | cc::VideoFrameProvider::Client* client) { |
| 569 | // This is called from both the main renderer thread and the compositor |
| 570 | // thread (when the main thread is blocked). |
| 571 | if (video_frame_provider_client_) |
| 572 | video_frame_provider_client_->StopUsingProvider(); |
| 573 | video_frame_provider_client_ = client; |
| 574 | } |
| 575 | |
| 576 | scoped_refptr<media::VideoFrame> WebMediaPlayerImpl::GetCurrentFrame() { |
| 577 | base::AutoLock auto_lock(lock_); |
| 578 | return current_frame_; |
| 579 | } |
| 580 | |
| 581 | void WebMediaPlayerImpl::PutCurrentFrame( |
| 582 | const scoped_refptr<media::VideoFrame>& frame) { |
| 583 | if (!accelerated_compositing_reported_) { |
| 584 | accelerated_compositing_reported_ = true; |
| 585 | DCHECK(frame_->view()->isAcceleratedCompositingActive()); |
| 586 | UMA_HISTOGRAM_BOOLEAN("Media.AcceleratedCompositingActive", true); |
| 587 | } |
| 588 | } |
[email protected] | e81283bb | 2010-08-31 18:01:21 | [diff] [blame] | 589 | |
[email protected] | 6523b24 | 2013-03-13 11:10:07 | [diff] [blame] | 590 | bool WebMediaPlayerImpl::copyVideoTextureToPlatformTexture( |
| 591 | WebKit::WebGraphicsContext3D* web_graphics_context, |
| 592 | unsigned int texture, |
| 593 | unsigned int level, |
| 594 | unsigned int internal_format, |
[email protected] | 9aa2b278 | 2013-05-14 00:13:31 | [diff] [blame] | 595 | unsigned int type, |
[email protected] | 6523b24 | 2013-03-13 11:10:07 | [diff] [blame] | 596 | bool premultiply_alpha, |
| 597 | bool flip_y) { |
| 598 | scoped_refptr<media::VideoFrame> video_frame; |
| 599 | { |
| 600 | base::AutoLock auto_lock(lock_); |
| 601 | video_frame = current_frame_; |
| 602 | } |
[email protected] | e56f88c7 | 2013-06-25 22:31:29 | [diff] [blame] | 603 | |
| 604 | if (!video_frame.get()) |
| 605 | return false; |
| 606 | if (video_frame->format() != media::VideoFrame::NATIVE_TEXTURE) |
| 607 | return false; |
| 608 | if (video_frame->texture_target() != GL_TEXTURE_2D) |
| 609 | return false; |
| 610 | |
| 611 | scoped_refptr<media::VideoFrame::MailboxHolder> mailbox_holder = |
| 612 | video_frame->texture_mailbox(); |
| 613 | |
| 614 | uint32 source_texture = web_graphics_context->createTexture(); |
| 615 | |
| 616 | web_graphics_context->waitSyncPoint(mailbox_holder->sync_point()); |
| 617 | web_graphics_context->bindTexture(GL_TEXTURE_2D, source_texture); |
| 618 | web_graphics_context->consumeTextureCHROMIUM(GL_TEXTURE_2D, |
| 619 | mailbox_holder->mailbox().name); |
| 620 | |
| 621 | // The video is stored in a unmultiplied format, so premultiply |
| 622 | // if necessary. |
| 623 | web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM, |
| 624 | premultiply_alpha); |
| 625 | // Application itself needs to take care of setting the right flip_y |
| 626 | // value down to get the expected result. |
| 627 | // flip_y==true means to reverse the video orientation while |
| 628 | // flip_y==false means to keep the intrinsic orientation. |
| 629 | web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, flip_y); |
| 630 | web_graphics_context->copyTextureCHROMIUM(GL_TEXTURE_2D, |
| 631 | source_texture, |
| 632 | texture, |
| 633 | level, |
| 634 | internal_format, |
| 635 | type); |
| 636 | web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, false); |
| 637 | web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM, |
| 638 | false); |
| 639 | |
| 640 | web_graphics_context->deleteTexture(source_texture); |
| 641 | |
| 642 | // The flush() operation is not necessary here. It is kept since the |
| 643 | // performance will be better when it is added than not. |
| 644 | web_graphics_context->flush(); |
| 645 | return true; |
[email protected] | 6523b24 | 2013-03-13 11:10:07 | [diff] [blame] | 646 | } |
| 647 | |
[email protected] | 49a097e | 2013-04-02 19:31:51 | [diff] [blame] | 648 | // Helper functions to report media EME related stats to UMA. They follow the |
| 649 | // convention of more commonly used macros UMA_HISTOGRAM_ENUMERATION and |
| 650 | // UMA_HISTOGRAM_COUNTS. The reason that we cannot use those macros directly is |
| 651 | // that UMA_* macros require the names to be constant throughout the process' |
| 652 | // lifetime. |
[email protected] | f3a1d40d | 2013-06-25 22:12:50 | [diff] [blame] | 653 | static void EmeUMAHistogramEnumeration(const WebKit::WebString& key_system, |
[email protected] | 49a097e | 2013-04-02 19:31:51 | [diff] [blame] | 654 | const std::string& method, |
| 655 | int sample, |
| 656 | int boundary_value) { |
| 657 | base::LinearHistogram::FactoryGet( |
| 658 | kMediaEme + KeySystemNameForUMA(key_system) + "." + method, |
| 659 | 1, boundary_value, boundary_value + 1, |
| 660 | base::Histogram::kUmaTargetedHistogramFlag)->Add(sample); |
| 661 | } |
| 662 | |
[email protected] | f3a1d40d | 2013-06-25 22:12:50 | [diff] [blame] | 663 | static void EmeUMAHistogramCounts(const WebKit::WebString& key_system, |
[email protected] | 49a097e | 2013-04-02 19:31:51 | [diff] [blame] | 664 | const std::string& method, |
| 665 | int sample) { |
| 666 | // Use the same parameters as UMA_HISTOGRAM_COUNTS. |
| 667 | base::Histogram::FactoryGet( |
| 668 | kMediaEme + KeySystemNameForUMA(key_system) + "." + method, |
| 669 | 1, 1000000, 50, base::Histogram::kUmaTargetedHistogramFlag)->Add(sample); |
| 670 | } |
| 671 | |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 672 | // Helper enum for reporting generateKeyRequest/addKey histograms. |
| 673 | enum MediaKeyException { |
| 674 | kUnknownResultId, |
| 675 | kSuccess, |
| 676 | kKeySystemNotSupported, |
| 677 | kInvalidPlayerState, |
| 678 | kMaxMediaKeyException |
| 679 | }; |
| 680 | |
| 681 | static MediaKeyException MediaKeyExceptionForUMA( |
| 682 | WebMediaPlayer::MediaKeyException e) { |
| 683 | switch (e) { |
| 684 | case WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported: |
| 685 | return kKeySystemNotSupported; |
| 686 | case WebMediaPlayer::MediaKeyExceptionInvalidPlayerState: |
| 687 | return kInvalidPlayerState; |
| 688 | case WebMediaPlayer::MediaKeyExceptionNoError: |
| 689 | return kSuccess; |
| 690 | default: |
| 691 | return kUnknownResultId; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | // Helper for converting |key_system| name and exception |e| to a pair of enum |
| 696 | // values from above, for reporting to UMA. |
| 697 | static void ReportMediaKeyExceptionToUMA( |
| 698 | const std::string& method, |
| 699 | const WebString& key_system, |
| 700 | WebMediaPlayer::MediaKeyException e) { |
| 701 | MediaKeyException result_id = MediaKeyExceptionForUMA(e); |
| 702 | DCHECK_NE(result_id, kUnknownResultId) << e; |
[email protected] | 49a097e | 2013-04-02 19:31:51 | [diff] [blame] | 703 | EmeUMAHistogramEnumeration( |
[email protected] | f3a1d40d | 2013-06-25 22:12:50 | [diff] [blame] | 704 | key_system, method, result_id, kMaxMediaKeyException); |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | WebMediaPlayer::MediaKeyException |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 708 | WebMediaPlayerImpl::generateKeyRequest(const WebString& key_system, |
| 709 | const unsigned char* init_data, |
| 710 | unsigned init_data_length) { |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 711 | WebMediaPlayer::MediaKeyException e = |
| 712 | GenerateKeyRequestInternal(key_system, init_data, init_data_length); |
| 713 | ReportMediaKeyExceptionToUMA("generateKeyRequest", key_system, e); |
| 714 | return e; |
| 715 | } |
| 716 | |
| 717 | WebMediaPlayer::MediaKeyException |
| 718 | WebMediaPlayerImpl::GenerateKeyRequestInternal( |
| 719 | const WebString& key_system, |
| 720 | const unsigned char* init_data, |
| 721 | unsigned init_data_length) { |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 722 | DVLOG(1) << "generateKeyRequest: " << key_system.utf8().data() << ": " |
| 723 | << std::string(reinterpret_cast<const char*>(init_data), |
[email protected] | cbdc8c2b | 2012-06-16 01:27:36 | [diff] [blame] | 724 | static_cast<size_t>(init_data_length)); |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 725 | |
[email protected] | fecb5fe | 2013-06-19 00:52:13 | [diff] [blame] | 726 | if (!IsSupportedKeySystem(key_system)) |
| 727 | return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported; |
| 728 | |
| 729 | // We do not support run-time switching between key systems for now. |
| 730 | if (current_key_system_.isEmpty()) { |
| 731 | if (!decryptor_->InitializeCDM(key_system.utf8())) |
| 732 | return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported; |
| 733 | current_key_system_ = key_system; |
| 734 | } |
| 735 | else if (key_system != current_key_system_) { |
| 736 | return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState; |
| 737 | } |
| 738 | |
[email protected] | c8d4028 | 2012-10-27 07:23:13 | [diff] [blame] | 739 | // TODO(xhwang): We assume all streams are from the same container (thus have |
| 740 | // the same "type") for now. In the future, the "type" should be passed down |
| 741 | // from the application. |
[email protected] | fecb5fe | 2013-06-19 00:52:13 | [diff] [blame] | 742 | if (!decryptor_->GenerateKeyRequest(init_data_type_, |
[email protected] | 83fdd99 | 2012-12-21 19:57:49 | [diff] [blame] | 743 | init_data, init_data_length)) { |
[email protected] | 6a20673 | 2012-08-29 01:14:40 | [diff] [blame] | 744 | current_key_system_.reset(); |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 745 | return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported; |
[email protected] | 6a20673 | 2012-08-29 01:14:40 | [diff] [blame] | 746 | } |
| 747 | |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 748 | return WebMediaPlayer::MediaKeyExceptionNoError; |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 749 | } |
| 750 | |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 751 | WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::addKey( |
| 752 | const WebString& key_system, |
| 753 | const unsigned char* key, |
| 754 | unsigned key_length, |
| 755 | const unsigned char* init_data, |
| 756 | unsigned init_data_length, |
| 757 | const WebString& session_id) { |
| 758 | WebMediaPlayer::MediaKeyException e = AddKeyInternal( |
| 759 | key_system, key, key_length, init_data, init_data_length, session_id); |
| 760 | ReportMediaKeyExceptionToUMA("addKey", key_system, e); |
| 761 | return e; |
| 762 | } |
| 763 | |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 764 | WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::AddKeyInternal( |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 765 | const WebString& key_system, |
| 766 | const unsigned char* key, |
| 767 | unsigned key_length, |
| 768 | const unsigned char* init_data, |
| 769 | unsigned init_data_length, |
| 770 | const WebString& session_id) { |
[email protected] | a014908 | 2012-04-19 17:33:41 | [diff] [blame] | 771 | DCHECK(key); |
| 772 | DCHECK_GT(key_length, 0u); |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 773 | DVLOG(1) << "addKey: " << key_system.utf8().data() << ": " |
| 774 | << std::string(reinterpret_cast<const char*>(key), |
| 775 | static_cast<size_t>(key_length)) << ", " |
| 776 | << std::string(reinterpret_cast<const char*>(init_data), |
| 777 | static_cast<size_t>(init_data_length)) |
| 778 | << " [" << session_id.utf8().data() << "]"; |
| 779 | |
[email protected] | fecb5fe | 2013-06-19 00:52:13 | [diff] [blame] | 780 | |
| 781 | if (!IsSupportedKeySystem(key_system)) |
| 782 | return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported; |
| 783 | |
| 784 | if (current_key_system_.isEmpty() || key_system != current_key_system_) |
| 785 | return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState; |
| 786 | |
| 787 | decryptor_->AddKey(key, key_length, |
[email protected] | 83fdd99 | 2012-12-21 19:57:49 | [diff] [blame] | 788 | init_data, init_data_length, session_id.utf8()); |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 789 | return WebMediaPlayer::MediaKeyExceptionNoError; |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 790 | } |
| 791 | |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 792 | WebMediaPlayer::MediaKeyException WebMediaPlayerImpl::cancelKeyRequest( |
| 793 | const WebString& key_system, |
| 794 | const WebString& session_id) { |
| 795 | WebMediaPlayer::MediaKeyException e = |
| 796 | CancelKeyRequestInternal(key_system, session_id); |
| 797 | ReportMediaKeyExceptionToUMA("cancelKeyRequest", key_system, e); |
| 798 | return e; |
| 799 | } |
| 800 | |
| 801 | WebMediaPlayer::MediaKeyException |
| 802 | WebMediaPlayerImpl::CancelKeyRequestInternal( |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 803 | const WebString& key_system, |
| 804 | const WebString& session_id) { |
| 805 | if (!IsSupportedKeySystem(key_system)) |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 806 | return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported; |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 807 | |
[email protected] | a888463 | 2012-06-29 03:05:34 | [diff] [blame] | 808 | if (current_key_system_.isEmpty() || key_system != current_key_system_) |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 809 | return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState; |
[email protected] | a888463 | 2012-06-29 03:05:34 | [diff] [blame] | 810 | |
[email protected] | fecb5fe | 2013-06-19 00:52:13 | [diff] [blame] | 811 | decryptor_->CancelKeyRequest(session_id.utf8()); |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 812 | return WebMediaPlayer::MediaKeyExceptionNoError; |
[email protected] | f7a6b99 | 2012-04-19 09:45:56 | [diff] [blame] | 813 | } |
| 814 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 815 | void WebMediaPlayerImpl::WillDestroyCurrentMessageLoop() { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 816 | Destroy(); |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | void WebMediaPlayerImpl::Repaint() { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 820 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | b49beeb | 2013-03-01 20:04:00 | [diff] [blame] | 821 | |
[email protected] | 29037ce5 | 2013-05-06 22:44:16 | [diff] [blame] | 822 | bool size_changed = false; |
| 823 | { |
| 824 | base::AutoLock auto_lock(lock_); |
| 825 | std::swap(pending_size_change_, size_changed); |
| 826 | pending_repaint_ = false; |
| 827 | } |
| 828 | |
| 829 | if (size_changed) |
| 830 | GetClient()->sizeChanged(); |
| 831 | |
| 832 | GetClient()->repaint(); |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 833 | } |
| 834 | |
[email protected] | a9590c2 | 2011-03-16 16:57:02 | [diff] [blame] | 835 | void WebMediaPlayerImpl::OnPipelineSeek(PipelineStatus status) { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 836 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 837 | starting_ = false; |
[email protected] | 5d11eff | 2011-09-15 00:06:06 | [diff] [blame] | 838 | seeking_ = false; |
| 839 | if (pending_seek_) { |
| 840 | pending_seek_ = false; |
| 841 | seek(pending_seek_seconds_); |
| 842 | return; |
| 843 | } |
| 844 | |
[email protected] | 10a64d9 | 2012-04-10 21:08:02 | [diff] [blame] | 845 | if (status != media::PIPELINE_OK) { |
| 846 | OnPipelineError(status); |
| 847 | return; |
[email protected] | 9670691 | 2009-07-15 17:18:05 | [diff] [blame] | 848 | } |
[email protected] | 10a64d9 | 2012-04-10 21:08:02 | [diff] [blame] | 849 | |
| 850 | // Update our paused time. |
| 851 | if (paused_) |
[email protected] | f7c0ada0 | 2012-07-23 08:42:50 | [diff] [blame] | 852 | paused_time_ = pipeline_->GetMediaTime(); |
[email protected] | 10a64d9 | 2012-04-10 21:08:02 | [diff] [blame] | 853 | |
| 854 | GetClient()->timeChanged(); |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 855 | } |
| 856 | |
[email protected] | 6954fe1 | 2013-01-03 03:22:32 | [diff] [blame] | 857 | void WebMediaPlayerImpl::OnPipelineEnded() { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 858 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | 10a64d9 | 2012-04-10 21:08:02 | [diff] [blame] | 859 | GetClient()->timeChanged(); |
[email protected] | 57653784 | 2009-08-12 23:52:05 | [diff] [blame] | 860 | } |
| 861 | |
[email protected] | a9590c2 | 2011-03-16 16:57:02 | [diff] [blame] | 862 | void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error) { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 863 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | be57ee5 | 2013-05-28 22:27:27 | [diff] [blame] | 864 | DCHECK_NE(error, media::PIPELINE_OK); |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 865 | |
| 866 | if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) { |
| 867 | // Any error that occurs before reaching ReadyStateHaveMetadata should |
| 868 | // be considered a format error. |
| 869 | SetNetworkState(WebMediaPlayer::NetworkStateFormatError); |
| 870 | Repaint(); |
| 871 | return; |
| 872 | } |
| 873 | |
[email protected] | be57ee5 | 2013-05-28 22:27:27 | [diff] [blame] | 874 | SetNetworkState(PipelineErrorToNetworkState(error)); |
[email protected] | a9590c2 | 2011-03-16 16:57:02 | [diff] [blame] | 875 | |
[email protected] | be57ee5 | 2013-05-28 22:27:27 | [diff] [blame] | 876 | if (error == media::PIPELINE_ERROR_DECRYPT) |
[email protected] | f3a1d40d | 2013-06-25 22:12:50 | [diff] [blame] | 877 | EmeUMAHistogramCounts(current_key_system_, "DecryptError", 1); |
[email protected] | bb2c1f3 | 2009-08-14 04:14:50 | [diff] [blame] | 878 | |
| 879 | // Repaint to trigger UI update. |
| 880 | Repaint(); |
[email protected] | db190487d | 2009-07-30 18:51:52 | [diff] [blame] | 881 | } |
| 882 | |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 883 | void WebMediaPlayerImpl::OnPipelineBufferingState( |
| 884 | media::Pipeline::BufferingState buffering_state) { |
| 885 | DVLOG(1) << "OnPipelineBufferingState(" << buffering_state << ")"; |
| 886 | |
| 887 | switch (buffering_state) { |
| 888 | case media::Pipeline::kHaveMetadata: |
| 889 | SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata); |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 890 | |
[email protected] | 21c3f750 | 2013-03-23 03:29:51 | [diff] [blame] | 891 | if (hasVideo() && GetClient()->needsWebLayerForVideo()) { |
| 892 | DCHECK(!video_weblayer_); |
| 893 | video_weblayer_.reset( |
| 894 | new webkit::WebLayerImpl(cc::VideoLayer::Create(this))); |
| 895 | GetClient()->setWebLayer(video_weblayer_.get()); |
| 896 | } |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 897 | break; |
| 898 | case media::Pipeline::kPrerollCompleted: |
[email protected] | a06d9e97 | 2013-06-20 08:43:47 | [diff] [blame] | 899 | // Only transition to ReadyStateHaveEnoughData if we don't have |
| 900 | // any pending seeks because the transition can cause Blink to |
| 901 | // report that the most recent seek has completed. |
| 902 | if (!pending_seek_) |
| 903 | SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData); |
[email protected] | a8e2cb8 | 2012-08-17 00:02:39 | [diff] [blame] | 904 | break; |
| 905 | } |
| 906 | |
| 907 | // Repaint to trigger UI update. |
| 908 | Repaint(); |
| 909 | } |
| 910 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame^] | 911 | void WebMediaPlayerImpl::OnDemuxerOpened() { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 912 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame^] | 913 | GetClient()->mediaSourceOpened(new WebMediaSourceImpl( |
[email protected] | 5f811a2d | 2013-03-15 23:54:51 | [diff] [blame] | 914 | chunk_demuxer_, base::Bind(&LogMediaSourceError, media_log_))); |
[email protected] | 81bb332 | 2011-07-21 15:55:50 | [diff] [blame] | 915 | } |
| 916 | |
[email protected] | fecb5fe | 2013-06-19 00:52:13 | [diff] [blame] | 917 | void WebMediaPlayerImpl::OnKeyAdded(const std::string& session_id) { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 918 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | f3a1d40d | 2013-06-25 22:12:50 | [diff] [blame] | 919 | EmeUMAHistogramCounts(current_key_system_, "KeyAdded", 1); |
[email protected] | fecb5fe | 2013-06-19 00:52:13 | [diff] [blame] | 920 | GetClient()->keyAdded(current_key_system_, |
[email protected] | cbdc8c2b | 2012-06-16 01:27:36 | [diff] [blame] | 921 | WebString::fromUTF8(session_id)); |
| 922 | } |
| 923 | |
[email protected] | fecb5fe | 2013-06-19 00:52:13 | [diff] [blame] | 924 | void WebMediaPlayerImpl::OnNeedKey(const std::string& session_id, |
[email protected] | c8d4028 | 2012-10-27 07:23:13 | [diff] [blame] | 925 | const std::string& type, |
[email protected] | 9ec0f9c | 2013-04-07 16:10:47 | [diff] [blame] | 926 | scoped_ptr<uint8[]> init_data, |
[email protected] | cbdc8c2b | 2012-06-16 01:27:36 | [diff] [blame] | 927 | int init_data_size) { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 928 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | cbdc8c2b | 2012-06-16 01:27:36 | [diff] [blame] | 929 | |
[email protected] | 47ebb0e | 2012-11-30 08:28:29 | [diff] [blame] | 930 | // Do not fire NeedKey event if encrypted media is not enabled. |
| 931 | if (!decryptor_) |
| 932 | return; |
| 933 | |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 934 | UMA_HISTOGRAM_COUNTS(kMediaEme + std::string("NeedKey"), 1); |
| 935 | |
[email protected] | c8d4028 | 2012-10-27 07:23:13 | [diff] [blame] | 936 | DCHECK(init_data_type_.empty() || type.empty() || type == init_data_type_); |
| 937 | if (init_data_type_.empty()) |
| 938 | init_data_type_ = type; |
| 939 | |
[email protected] | fecb5fe | 2013-06-19 00:52:13 | [diff] [blame] | 940 | GetClient()->keyNeeded(WebString(), |
[email protected] | cbdc8c2b | 2012-06-16 01:27:36 | [diff] [blame] | 941 | WebString::fromUTF8(session_id), |
| 942 | init_data.get(), |
| 943 | init_data_size); |
| 944 | } |
| 945 | |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 946 | scoped_ptr<media::TextTrack> |
| 947 | WebMediaPlayerImpl::OnTextTrack(media::TextKind kind, |
| 948 | const std::string& label, |
| 949 | const std::string& language) { |
| 950 | typedef WebInbandTextTrackImpl::Kind webkind_t; |
| 951 | const webkind_t webkind = static_cast<webkind_t>(kind); |
| 952 | const WebKit::WebString weblabel = WebKit::WebString::fromUTF8(label); |
| 953 | const WebKit::WebString weblanguage = WebKit::WebString::fromUTF8(language); |
| 954 | |
| 955 | WebInbandTextTrackImpl* const text_track = |
| 956 | new WebInbandTextTrackImpl(webkind, weblabel, weblanguage, |
| 957 | text_track_index_++); |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 958 | |
[email protected] | 2fb8aa2dd | 2013-07-08 21:23:35 | [diff] [blame] | 959 | return scoped_ptr<media::TextTrack>(new TextTrackImpl(GetClient(), |
| 960 | text_track)); |
[email protected] | 7153772 | 2013-05-23 06:47:53 | [diff] [blame] | 961 | } |
| 962 | |
[email protected] | fecb5fe | 2013-06-19 00:52:13 | [diff] [blame] | 963 | void WebMediaPlayerImpl::OnKeyError(const std::string& session_id, |
[email protected] | 15b05a7 | 2013-05-30 00:40:57 | [diff] [blame] | 964 | media::MediaKeys::KeyError error_code, |
[email protected] | cbdc8c2b | 2012-06-16 01:27:36 | [diff] [blame] | 965 | int system_code) { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 966 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | cbdc8c2b | 2012-06-16 01:27:36 | [diff] [blame] | 967 | |
[email protected] | f3a1d40d | 2013-06-25 22:12:50 | [diff] [blame] | 968 | EmeUMAHistogramEnumeration(current_key_system_, "KeyError", |
[email protected] | fecb5fe | 2013-06-19 00:52:13 | [diff] [blame] | 969 | error_code, media::MediaKeys::kMaxKeyError); |
[email protected] | 2ca7d5c | 2012-10-23 07:30:54 | [diff] [blame] | 970 | |
[email protected] | cbdc8c2b | 2012-06-16 01:27:36 | [diff] [blame] | 971 | GetClient()->keyError( |
[email protected] | fecb5fe | 2013-06-19 00:52:13 | [diff] [blame] | 972 | current_key_system_, |
[email protected] | cbdc8c2b | 2012-06-16 01:27:36 | [diff] [blame] | 973 | WebString::fromUTF8(session_id), |
| 974 | static_cast<WebKit::WebMediaPlayerClient::MediaKeyErrorCode>(error_code), |
| 975 | system_code); |
| 976 | } |
| 977 | |
[email protected] | fecb5fe | 2013-06-19 00:52:13 | [diff] [blame] | 978 | void WebMediaPlayerImpl::OnKeyMessage(const std::string& session_id, |
[email protected] | 0a81bdb | 2013-06-29 23:18:06 | [diff] [blame] | 979 | const std::vector<uint8>& message, |
[email protected] | abd36b3af | 2012-12-22 03:42:02 | [diff] [blame] | 980 | const std::string& default_url) { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 981 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | cbdc8c2b | 2012-06-16 01:27:36 | [diff] [blame] | 982 | |
[email protected] | abd36b3af | 2012-12-22 03:42:02 | [diff] [blame] | 983 | const GURL default_url_gurl(default_url); |
| 984 | DLOG_IF(WARNING, !default_url.empty() && !default_url_gurl.is_valid()) |
| 985 | << "Invalid URL in default_url: " << default_url; |
| 986 | |
[email protected] | fecb5fe | 2013-06-19 00:52:13 | [diff] [blame] | 987 | GetClient()->keyMessage(current_key_system_, |
[email protected] | cbdc8c2b | 2012-06-16 01:27:36 | [diff] [blame] | 988 | WebString::fromUTF8(session_id), |
[email protected] | 33325ff | 2013-07-01 07:49:36 | [diff] [blame] | 989 | message.empty() ? NULL : &message[0], |
[email protected] | 65a8c10 | 2012-12-15 00:56:57 | [diff] [blame] | 990 | message.size(), |
[email protected] | abd36b3af | 2012-12-22 03:42:02 | [diff] [blame] | 991 | default_url_gurl); |
[email protected] | 4205f0aab | 2012-04-26 04:33:06 | [diff] [blame] | 992 | } |
| 993 | |
[email protected] | 5bb69ef | 2011-12-23 16:29:09 | [diff] [blame] | 994 | void WebMediaPlayerImpl::SetOpaque(bool opaque) { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 995 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | 5bb69ef | 2011-12-23 16:29:09 | [diff] [blame] | 996 | |
| 997 | GetClient()->setOpaque(opaque); |
| 998 | } |
| 999 | |
[email protected] | d250190da3b | 2012-07-23 22:57:30 | [diff] [blame] | 1000 | void WebMediaPlayerImpl::DataSourceInitialized(const GURL& gurl, bool success) { |
[email protected] | e82c0e35 | 2013-03-01 01:53:46 | [diff] [blame] | 1001 | DCHECK(main_loop_->BelongsToCurrentThread()); |
[email protected] | a941529 | 2012-01-19 19:55:20 | [diff] [blame] | 1002 | |
[email protected] | d250190da3b | 2012-07-23 22:57:30 | [diff] [blame] | 1003 | if (!success) { |
[email protected] | ef405f66b | 2012-04-18 02:39:55 | [diff] [blame] | 1004 | SetNetworkState(WebMediaPlayer::NetworkStateFormatError); |
[email protected] | a941529 | 2012-01-19 19:55:20 | [diff] [blame] | 1005 | Repaint(); |
| 1006 | return; |
| 1007 | } |
| 1008 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame^] | 1009 | StartPipeline(); |
[email protected] | a941529 | 2012-01-19 19:55:20 | [diff] [blame] | 1010 | } |
| 1011 | |
[email protected] | 122f4025 | 2012-06-12 05:01:56 | [diff] [blame] | 1012 | void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) { |
| 1013 | if (!is_downloading && network_state_ == WebMediaPlayer::NetworkStateLoading) |
| 1014 | SetNetworkState(WebMediaPlayer::NetworkStateIdle); |
| 1015 | else if (is_downloading && network_state_ == WebMediaPlayer::NetworkStateIdle) |
| 1016 | SetNetworkState(WebMediaPlayer::NetworkStateLoading); |
| 1017 | media_log_->AddEvent( |
| 1018 | media_log_->CreateBooleanEvent( |
| 1019 | media::MediaLogEvent::NETWORK_ACTIVITY_SET, |
| 1020 | "is_downloading_data", is_downloading)); |
| 1021 | } |
| 1022 | |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame^] | 1023 | void WebMediaPlayerImpl::StartPipeline() { |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1024 | const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
[email protected] | b573f95 | 2013-06-04 10:50:09 | [diff] [blame] | 1025 | bool increase_preroll_on_underflow = true; |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1026 | |
[email protected] | 0814ac6 | 2013-06-07 21:01:29 | [diff] [blame] | 1027 | // Keep track if this is a MSE or non-MSE playback. |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame^] | 1028 | UMA_HISTOGRAM_BOOLEAN("Media.MSE.Playback", |
| 1029 | (load_type_ == LoadTypeMediaSource)); |
[email protected] | 0814ac6 | 2013-06-07 21:01:29 | [diff] [blame] | 1030 | |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1031 | // Figure out which demuxer to use. |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame^] | 1032 | if (load_type_ != LoadTypeMediaSource) { |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1033 | DCHECK(!chunk_demuxer_); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1034 | DCHECK(data_source_); |
| 1035 | |
| 1036 | demuxer_.reset(new media::FFmpegDemuxer( |
[email protected] | c1330c8 | 2013-06-06 02:23:25 | [diff] [blame] | 1037 | media_loop_, data_source_.get(), |
[email protected] | 1e931f6 | 2013-07-31 23:25:52 | [diff] [blame] | 1038 | BIND_TO_RENDER_LOOP_1(&WebMediaPlayerImpl::OnNeedKey, ""), |
| 1039 | media_log_)); |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1040 | } else { |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1041 | DCHECK(!chunk_demuxer_); |
| 1042 | DCHECK(!data_source_); |
| 1043 | |
[email protected] | 7afb1bb3 | 2013-06-29 14:27:10 | [diff] [blame] | 1044 | media::AddTextTrackCB add_text_track_cb; |
| 1045 | |
| 1046 | if (cmd_line->HasSwitch(switches::kEnableInbandTextTracks)) { |
| 1047 | add_text_track_cb = |
| 1048 | base::Bind(&WebMediaPlayerImpl::OnTextTrack, base::Unretained(this)); |
| 1049 | } |
| 1050 | |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1051 | chunk_demuxer_ = new media::ChunkDemuxer( |
[email protected] | ef8394c | 2013-08-21 20:26:30 | [diff] [blame^] | 1052 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDemuxerOpened), |
[email protected] | fecb5fe | 2013-06-19 00:52:13 | [diff] [blame] | 1053 | BIND_TO_RENDER_LOOP_1(&WebMediaPlayerImpl::OnNeedKey, ""), |
[email protected] | 7afb1bb3 | 2013-06-29 14:27:10 | [diff] [blame] | 1054 | add_text_track_cb, |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1055 | base::Bind(&LogMediaSourceError, media_log_)); |
| 1056 | demuxer_.reset(chunk_demuxer_); |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1057 | |
[email protected] | 6314f1b | 2013-08-12 11:14:01 | [diff] [blame] | 1058 | #if !defined(OS_CHROMEOS) |
| 1059 | // Disable GpuVideoDecoder creation on platforms other than CrOS until |
[email protected] | 6790829 | 2013-08-03 12:17:15 | [diff] [blame] | 1060 | // they support codec config changes. |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1061 | // TODO(acolwell): Remove this once http://crbug.com/151045 is fixed. |
| 1062 | gpu_factories_ = NULL; |
[email protected] | 6790829 | 2013-08-03 12:17:15 | [diff] [blame] | 1063 | #endif |
[email protected] | b573f95 | 2013-06-04 10:50:09 | [diff] [blame] | 1064 | |
| 1065 | // Disable preroll increases on underflow since the web application has no |
| 1066 | // way to detect that this is happening and runs the risk of triggering |
| 1067 | // unwanted garbage collection if it is to aggressive about appending data. |
| 1068 | // TODO(acolwell): Remove this once http://crbug.com/144683 is fixed. |
| 1069 | increase_preroll_on_underflow = false; |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1070 | } |
| 1071 | |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1072 | scoped_ptr<media::FilterCollection> filter_collection( |
| 1073 | new media::FilterCollection()); |
| 1074 | filter_collection->SetDemuxer(demuxer_.get()); |
| 1075 | |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1076 | // Figure out if EME is enabled. |
| 1077 | media::SetDecryptorReadyCB set_decryptor_ready_cb; |
| 1078 | if (decryptor_) { |
| 1079 | set_decryptor_ready_cb = base::Bind(&ProxyDecryptor::SetDecryptorReadyCB, |
| 1080 | base::Unretained(decryptor_.get())); |
| 1081 | } |
| 1082 | |
| 1083 | // Create our audio decoders and renderer. |
| 1084 | ScopedVector<media::AudioDecoder> audio_decoders; |
[email protected] | c1330c8 | 2013-06-06 02:23:25 | [diff] [blame] | 1085 | audio_decoders.push_back(new media::FFmpegAudioDecoder(media_loop_)); |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1086 | if (cmd_line->HasSwitch(switches::kEnableOpusPlayback)) { |
[email protected] | c1330c8 | 2013-06-06 02:23:25 | [diff] [blame] | 1087 | audio_decoders.push_back(new media::OpusAudioDecoder(media_loop_)); |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | scoped_ptr<media::AudioRenderer> audio_renderer( |
[email protected] | c1330c8 | 2013-06-06 02:23:25 | [diff] [blame] | 1091 | new media::AudioRendererImpl(media_loop_, |
[email protected] | ff875be5 | 2013-06-02 23:47:38 | [diff] [blame] | 1092 | audio_source_provider_.get(), |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1093 | audio_decoders.Pass(), |
[email protected] | b573f95 | 2013-06-04 10:50:09 | [diff] [blame] | 1094 | set_decryptor_ready_cb, |
| 1095 | increase_preroll_on_underflow)); |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1096 | filter_collection->SetAudioRenderer(audio_renderer.Pass()); |
| 1097 | |
| 1098 | // Create our video decoders and renderer. |
| 1099 | ScopedVector<media::VideoDecoder> video_decoders; |
| 1100 | |
[email protected] | 96cb47d0 | 2013-08-16 07:34:52 | [diff] [blame] | 1101 | if (gpu_factories_.get()) { |
| 1102 | video_decoders.push_back( |
| 1103 | new media::GpuVideoDecoder(gpu_factories_, media_log_)); |
| 1104 | } |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1105 | |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1106 | // TODO(phajdan.jr): Remove ifdefs when libvpx with vp9 support is released |
| 1107 | // (http://crbug.com/174287) . |
| 1108 | #if !defined(MEDIA_DISABLE_LIBVPX) |
[email protected] | b150b553 | 2013-06-15 00:50:54 | [diff] [blame] | 1109 | video_decoders.push_back(new media::VpxVideoDecoder(media_loop_)); |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1110 | #endif // !defined(MEDIA_DISABLE_LIBVPX) |
| 1111 | |
[email protected] | c1330c8 | 2013-06-06 02:23:25 | [diff] [blame] | 1112 | video_decoders.push_back(new media::FFmpegVideoDecoder(media_loop_)); |
[email protected] | 6ac955b | 2013-04-19 23:43:32 | [diff] [blame] | 1113 | |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1114 | scoped_ptr<media::VideoRenderer> video_renderer( |
| 1115 | new media::VideoRendererBase( |
[email protected] | c1330c8 | 2013-06-06 02:23:25 | [diff] [blame] | 1116 | media_loop_, |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1117 | video_decoders.Pass(), |
| 1118 | set_decryptor_ready_cb, |
| 1119 | base::Bind(&WebMediaPlayerImpl::FrameReady, base::Unretained(this)), |
| 1120 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::SetOpaque), |
| 1121 | true)); |
| 1122 | filter_collection->SetVideoRenderer(video_renderer.Pass()); |
| 1123 | |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1124 | // ... and we're ready to go! |
| 1125 | starting_ = true; |
| 1126 | pipeline_->Start( |
| 1127 | filter_collection.Pass(), |
| 1128 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineEnded), |
| 1129 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineError), |
| 1130 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineSeek), |
| 1131 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnPipelineBufferingState), |
| 1132 | BIND_TO_RENDER_LOOP(&WebMediaPlayerImpl::OnDurationChange)); |
| 1133 | } |
| 1134 | |
| 1135 | void WebMediaPlayerImpl::SetNetworkState(WebMediaPlayer::NetworkState state) { |
| 1136 | DCHECK(main_loop_->BelongsToCurrentThread()); |
| 1137 | DVLOG(1) << "SetNetworkState: " << state; |
| 1138 | network_state_ = state; |
| 1139 | // Always notify to ensure client has the latest value. |
| 1140 | GetClient()->networkStateChanged(); |
| 1141 | } |
| 1142 | |
| 1143 | void WebMediaPlayerImpl::SetReadyState(WebMediaPlayer::ReadyState state) { |
| 1144 | DCHECK(main_loop_->BelongsToCurrentThread()); |
| 1145 | DVLOG(1) << "SetReadyState: " << state; |
| 1146 | |
| 1147 | if (state == WebMediaPlayer::ReadyStateHaveEnoughData && |
| 1148 | is_local_source_ && |
| 1149 | network_state_ == WebMediaPlayer::NetworkStateLoading) |
| 1150 | SetNetworkState(WebMediaPlayer::NetworkStateLoaded); |
| 1151 | |
| 1152 | ready_state_ = state; |
| 1153 | // Always notify to ensure client has the latest value. |
| 1154 | GetClient()->readyStateChanged(); |
| 1155 | } |
| 1156 | |
| 1157 | void WebMediaPlayerImpl::Destroy() { |
| 1158 | DCHECK(main_loop_->BelongsToCurrentThread()); |
| 1159 | |
| 1160 | // Abort any pending IO so stopping the pipeline doesn't get blocked. |
| 1161 | if (data_source_) |
| 1162 | data_source_->Abort(); |
| 1163 | if (chunk_demuxer_) { |
| 1164 | chunk_demuxer_->Shutdown(); |
| 1165 | chunk_demuxer_ = NULL; |
| 1166 | } |
| 1167 | |
[email protected] | ff875be5 | 2013-06-02 23:47:38 | [diff] [blame] | 1168 | if (gpu_factories_.get()) { |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1169 | gpu_factories_->Abort(); |
| 1170 | gpu_factories_ = NULL; |
| 1171 | } |
| 1172 | |
| 1173 | // Make sure to kill the pipeline so there's no more media threads running. |
| 1174 | // Note: stopping the pipeline might block for a long time. |
| 1175 | base::WaitableEvent waiter(false, false); |
| 1176 | pipeline_->Stop(base::Bind( |
| 1177 | &base::WaitableEvent::Signal, base::Unretained(&waiter))); |
| 1178 | waiter.Wait(); |
| 1179 | |
| 1180 | // Let V8 know we are not using extra resources anymore. |
| 1181 | if (incremented_externally_allocated_memory_) { |
| 1182 | v8::V8::AdjustAmountOfExternalAllocatedMemory(-kPlayerExtraMemory); |
| 1183 | incremented_externally_allocated_memory_ = false; |
| 1184 | } |
| 1185 | |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1186 | // Release any final references now that everything has stopped. |
[email protected] | a3702c5 | 2013-05-09 05:40:30 | [diff] [blame] | 1187 | pipeline_.reset(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1188 | demuxer_.reset(); |
[email protected] | cccbd0f6 | 2013-04-26 18:39:59 | [diff] [blame] | 1189 | data_source_.reset(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | WebKit::WebMediaPlayerClient* WebMediaPlayerImpl::GetClient() { |
| 1193 | DCHECK(main_loop_->BelongsToCurrentThread()); |
| 1194 | DCHECK(client_); |
| 1195 | return client_; |
| 1196 | } |
| 1197 | |
| 1198 | WebKit::WebAudioSourceProvider* WebMediaPlayerImpl::audioSourceProvider() { |
[email protected] | ff875be5 | 2013-06-02 23:47:38 | [diff] [blame] | 1199 | return audio_source_provider_.get(); |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | void WebMediaPlayerImpl::IncrementExternallyAllocatedMemory() { |
| 1203 | DCHECK(main_loop_->BelongsToCurrentThread()); |
| 1204 | incremented_externally_allocated_memory_ = true; |
| 1205 | v8::V8::AdjustAmountOfExternalAllocatedMemory(kPlayerExtraMemory); |
| 1206 | } |
| 1207 | |
| 1208 | double WebMediaPlayerImpl::GetPipelineDuration() const { |
| 1209 | base::TimeDelta duration = pipeline_->GetMediaDuration(); |
| 1210 | |
| 1211 | // Return positive infinity if the resource is unbounded. |
| 1212 | // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration |
| 1213 | if (duration == media::kInfiniteDuration()) |
| 1214 | return std::numeric_limits<double>::infinity(); |
| 1215 | |
| 1216 | return duration.InSecondsF(); |
| 1217 | } |
| 1218 | |
| 1219 | void WebMediaPlayerImpl::OnDurationChange() { |
| 1220 | if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing) |
| 1221 | return; |
| 1222 | |
| 1223 | GetClient()->durationChanged(); |
| 1224 | } |
| 1225 | |
| 1226 | void WebMediaPlayerImpl::FrameReady( |
| 1227 | const scoped_refptr<media::VideoFrame>& frame) { |
| 1228 | base::AutoLock auto_lock(lock_); |
[email protected] | 29037ce5 | 2013-05-06 22:44:16 | [diff] [blame] | 1229 | |
[email protected] | ff875be5 | 2013-06-02 23:47:38 | [diff] [blame] | 1230 | if (current_frame_.get() && |
[email protected] | 29037ce5 | 2013-05-06 22:44:16 | [diff] [blame] | 1231 | current_frame_->natural_size() != frame->natural_size() && |
| 1232 | !pending_size_change_) { |
| 1233 | pending_size_change_ = true; |
| 1234 | } |
| 1235 | |
[email protected] | f5443ef7 | 2013-04-22 04:03:38 | [diff] [blame] | 1236 | current_frame_ = frame; |
| 1237 | |
| 1238 | if (pending_repaint_) |
| 1239 | return; |
| 1240 | |
| 1241 | pending_repaint_ = true; |
| 1242 | main_loop_->PostTask(FROM_HERE, base::Bind( |
| 1243 | &WebMediaPlayerImpl::Repaint, AsWeakPtr())); |
[email protected] | ddbc6ff | 2013-04-19 15:28:33 | [diff] [blame] | 1244 | } |
| 1245 | |
[email protected] | 5e35a8d | 2013-07-10 19:37:21 | [diff] [blame] | 1246 | } // namespace content |