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