[email protected] | 891acc9 | 2009-04-27 19:56:41 | [diff] [blame] | 1 | // Copyright (c) 2008-2009 The Chromium Authors. All rights reserved. |
| 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] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 5 | #include "webkit/glue/webmediaplayer_impl.h" |
| 6 | |
[email protected] | 891acc9 | 2009-04-27 19:56:41 | [diff] [blame] | 7 | #include "base/command_line.h" |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 8 | #include "googleurl/src/gurl.h" |
[email protected] | 4c616fa5 | 2009-03-30 20:10:07 | [diff] [blame] | 9 | #include "media/filters/ffmpeg_audio_decoder.h" |
[email protected] | 3f06d0ef | 2009-03-19 01:35:36 | [diff] [blame] | 10 | #include "media/filters/ffmpeg_demuxer.h" |
[email protected] | 720ac73 | 2009-04-02 23:55:44 | [diff] [blame] | 11 | #include "media/filters/ffmpeg_video_decoder.h" |
[email protected] | 891acc9 | 2009-04-27 19:56:41 | [diff] [blame] | 12 | #include "media/filters/null_audio_renderer.h" |
[email protected] | afdcf5c | 2009-05-10 20:30:41 | [diff] [blame] | 13 | #include "webkit/api/public/WebRect.h" |
| 14 | #include "webkit/api/public/WebSize.h" |
| 15 | #include "webkit/api/public/WebURL.h" |
[email protected] | add5177 | 2009-06-11 18:25:17 | [diff] [blame] | 16 | #include "webkit/glue/media/video_renderer_impl.h" |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 17 | |
[email protected] | df143bdc | 2009-06-16 17:34:19 | [diff] [blame] | 18 | using WebKit::WebCanvas; |
[email protected] | b3f2b91 | 2009-04-09 16:18:52 | [diff] [blame] | 19 | using WebKit::WebRect; |
| 20 | using WebKit::WebSize; |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 21 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | // Limits the maximum outstanding repaints posted on render thread. |
| 25 | // This number of 50 is a guess, it does not take too much memory on the task |
| 26 | // queue but gives up a pretty good latency on repaint. |
| 27 | const int kMaxOutstandingRepaints = 50; |
| 28 | |
| 29 | } // namespace |
| 30 | |
[email protected] | add5177 | 2009-06-11 18:25:17 | [diff] [blame] | 31 | namespace webkit_glue { |
| 32 | |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 33 | ///////////////////////////////////////////////////////////////////////////// |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 34 | // WebMediaPlayerImpl::Proxy implementation |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 35 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 36 | WebMediaPlayerImpl::Proxy::Proxy(MessageLoop* render_loop, |
| 37 | WebMediaPlayerImpl* webmediaplayer) |
| 38 | : render_loop_(render_loop), |
| 39 | webmediaplayer_(webmediaplayer), |
| 40 | outstanding_repaints_(0) { |
| 41 | DCHECK(render_loop_); |
| 42 | DCHECK(webmediaplayer_); |
| 43 | } |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 44 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 45 | WebMediaPlayerImpl::Proxy::~Proxy() { |
| 46 | Detach(); |
| 47 | } |
| 48 | |
| 49 | void WebMediaPlayerImpl::Proxy::Repaint() { |
| 50 | AutoLock auto_lock(lock_); |
| 51 | if (outstanding_repaints_ < kMaxOutstandingRepaints) { |
| 52 | ++outstanding_repaints_; |
| 53 | |
| 54 | render_loop_->PostTask(FROM_HERE, |
| 55 | NewRunnableMethod(this, &WebMediaPlayerImpl::Proxy::RepaintTask)); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 56 | } |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 57 | } |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 58 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 59 | void WebMediaPlayerImpl::Proxy::TimeChanged() { |
| 60 | render_loop_->PostTask(FROM_HERE, |
| 61 | NewRunnableMethod(this, &WebMediaPlayerImpl::Proxy::TimeChangedTask)); |
| 62 | } |
| 63 | |
| 64 | void WebMediaPlayerImpl::Proxy::NetworkStateChanged( |
| 65 | WebKit::WebMediaPlayer::NetworkState state) { |
| 66 | render_loop_->PostTask(FROM_HERE, |
| 67 | NewRunnableMethod(this, |
| 68 | &WebMediaPlayerImpl::Proxy::NetworkStateChangedTask, |
| 69 | state)); |
| 70 | } |
| 71 | |
| 72 | void WebMediaPlayerImpl::Proxy::ReadyStateChanged( |
| 73 | WebKit::WebMediaPlayer::ReadyState state) { |
| 74 | render_loop_->PostTask(FROM_HERE, |
| 75 | NewRunnableMethod(this, |
| 76 | &WebMediaPlayerImpl::Proxy::ReadyStateChangedTask, |
| 77 | state)); |
| 78 | } |
| 79 | |
| 80 | void WebMediaPlayerImpl::Proxy::RepaintTask() { |
| 81 | DCHECK(MessageLoop::current() == render_loop_); |
| 82 | { |
| 83 | AutoLock auto_lock(lock_); |
| 84 | --outstanding_repaints_; |
| 85 | DCHECK_GE(outstanding_repaints_, 0); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 86 | } |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 87 | if (webmediaplayer_) |
| 88 | webmediaplayer_->Repaint(); |
| 89 | } |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 90 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 91 | void WebMediaPlayerImpl::Proxy::TimeChangedTask() { |
| 92 | DCHECK(MessageLoop::current() == render_loop_); |
| 93 | if (webmediaplayer_) |
| 94 | webmediaplayer_->TimeChanged(); |
| 95 | } |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 96 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 97 | void WebMediaPlayerImpl::Proxy::NetworkStateChangedTask( |
| 98 | WebKit::WebMediaPlayer::NetworkState state) { |
| 99 | DCHECK(MessageLoop::current() == render_loop_); |
| 100 | if (webmediaplayer_) |
| 101 | webmediaplayer_->SetNetworkState(state); |
| 102 | } |
| 103 | |
| 104 | void WebMediaPlayerImpl::Proxy::ReadyStateChangedTask( |
| 105 | WebKit::WebMediaPlayer::ReadyState state) { |
| 106 | DCHECK(MessageLoop::current() == render_loop_); |
| 107 | if (webmediaplayer_) |
| 108 | webmediaplayer_->SetReadyState(state); |
| 109 | } |
| 110 | |
| 111 | void WebMediaPlayerImpl::Proxy::SetVideoRenderer( |
| 112 | VideoRendererImpl* video_renderer) { |
| 113 | video_renderer_ = video_renderer; |
| 114 | } |
| 115 | |
| 116 | void WebMediaPlayerImpl::Proxy::Paint(skia::PlatformCanvas* canvas, |
| 117 | const gfx::Rect& dest_rect) { |
| 118 | DCHECK(MessageLoop::current() == render_loop_); |
| 119 | if (video_renderer_) { |
| 120 | video_renderer_->Paint(canvas, dest_rect); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void WebMediaPlayerImpl::Proxy::SetSize(const gfx::Rect& rect) { |
| 125 | DCHECK(MessageLoop::current() == render_loop_); |
| 126 | if (video_renderer_) { |
| 127 | video_renderer_->SetRect(rect); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void WebMediaPlayerImpl::Proxy::Detach() { |
| 132 | DCHECK(MessageLoop::current() == render_loop_); |
| 133 | webmediaplayer_ = NULL; |
| 134 | video_renderer_ = NULL; |
| 135 | } |
| 136 | |
| 137 | void WebMediaPlayerImpl::Proxy::PipelineInitializationCallback(bool success) { |
| 138 | if (success) { |
| 139 | // Since we have initialized the pipeline, say we have everything. |
| 140 | // TODO(hclam): change this to report the correct status. Should also post |
| 141 | // a task to call to |webmediaplayer_|. |
| 142 | ReadyStateChanged(WebKit::WebMediaPlayer::HaveMetadata); |
| 143 | ReadyStateChanged(WebKit::WebMediaPlayer::HaveEnoughData); |
| 144 | NetworkStateChanged(WebKit::WebMediaPlayer::Loaded); |
| 145 | } else { |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 146 | // TODO(hclam): should use pipeline_->GetError() to determine the state |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 147 | // properly and reports error using MediaError. |
| 148 | // WebKit uses FormatError to indicate an error for bogus URL or bad file. |
| 149 | // Since we are at the initialization stage we can safely treat every error |
| 150 | // as format error. Should post a task to call to |webmediaplayer_|. |
| 151 | NetworkStateChanged(WebKit::WebMediaPlayer::FormatError); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void WebMediaPlayerImpl::Proxy::PipelineSeekCallback(bool success) { |
| 156 | if (success) |
| 157 | TimeChanged(); |
| 158 | } |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 159 | |
| 160 | ///////////////////////////////////////////////////////////////////////////// |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 161 | // WebMediaPlayerImpl implementation |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 162 | |
[email protected] | add5177 | 2009-06-11 18:25:17 | [diff] [blame] | 163 | WebMediaPlayerImpl::WebMediaPlayerImpl(WebKit::WebMediaPlayerClient* client, |
| 164 | media::FilterFactoryCollection* factory) |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 165 | : network_state_(WebKit::WebMediaPlayer::Empty), |
| 166 | ready_state_(WebKit::WebMediaPlayer::HaveNothing), |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 167 | main_loop_(NULL), |
[email protected] | add5177 | 2009-06-11 18:25:17 | [diff] [blame] | 168 | filter_factory_(factory), |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 169 | pipeline_thread_("PipelineThread"), |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame^] | 170 | paused_(true), |
| 171 | playback_rate_(0.0f), |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 172 | client_(client) { |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 173 | // Saves the current message loop. |
| 174 | DCHECK(!main_loop_); |
| 175 | main_loop_ = MessageLoop::current(); |
| 176 | |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 177 | // Create the pipeline and its thread. |
| 178 | if (!pipeline_thread_.Start()) { |
| 179 | NOTREACHED() << "Could not start PipelineThread"; |
| 180 | } else { |
| 181 | pipeline_.reset(new media::PipelineImpl(pipeline_thread_.message_loop())); |
| 182 | } |
| 183 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 184 | // Also we want to be notified of |main_loop_| destruction. |
| 185 | main_loop_->AddDestructionObserver(this); |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 186 | |
| 187 | // Creates the proxy. |
| 188 | proxy_ = new Proxy(main_loop_, this); |
| 189 | |
| 190 | // Add in the default filter factories. |
| 191 | filter_factory_->AddFactory(media::FFmpegDemuxer::CreateFilterFactory()); |
| 192 | filter_factory_->AddFactory(media::FFmpegAudioDecoder::CreateFactory()); |
| 193 | filter_factory_->AddFactory(media::FFmpegVideoDecoder::CreateFactory()); |
| 194 | filter_factory_->AddFactory(media::NullAudioRenderer::CreateFilterFactory()); |
| 195 | filter_factory_->AddFactory(VideoRendererImpl::CreateFactory(proxy_)); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 196 | } |
| 197 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 198 | WebMediaPlayerImpl::~WebMediaPlayerImpl() { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 199 | Destroy(); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 200 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 201 | // Finally tell the |main_loop_| we don't want to be notified of destruction |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 202 | // event. |
| 203 | if (main_loop_) { |
| 204 | main_loop_->RemoveDestructionObserver(this); |
| 205 | } |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 206 | } |
| 207 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 208 | void WebMediaPlayerImpl::load(const WebKit::WebURL& url) { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 209 | DCHECK(MessageLoop::current() == main_loop_); |
| 210 | DCHECK(proxy_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 211 | |
[email protected] | a728102 | 2009-06-17 17:58:04 | [diff] [blame] | 212 | // Initialize the pipeline. |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 213 | SetNetworkState(WebKit::WebMediaPlayer::Loading); |
| 214 | SetReadyState(WebKit::WebMediaPlayer::HaveNothing); |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 215 | pipeline_->Start( |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 216 | filter_factory_.get(), |
| 217 | url.spec(), |
| 218 | NewCallback(proxy_.get(), |
| 219 | &WebMediaPlayerImpl::Proxy::PipelineInitializationCallback)); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 220 | } |
| 221 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 222 | void WebMediaPlayerImpl::cancelLoad() { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 223 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 224 | } |
| 225 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 226 | void WebMediaPlayerImpl::play() { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 227 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 228 | |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame^] | 229 | paused_ = false; |
| 230 | pipeline_->SetPlaybackRate(playback_rate_); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 231 | } |
| 232 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 233 | void WebMediaPlayerImpl::pause() { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 234 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 235 | |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame^] | 236 | paused_ = true; |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 237 | pipeline_->SetPlaybackRate(0.0f); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 238 | } |
| 239 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 240 | void WebMediaPlayerImpl::seek(float seconds) { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 241 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 242 | |
[email protected] | b9490b9c | 2009-05-12 01:01:52 | [diff] [blame] | 243 | // Try to preserve as much accuracy as possible. |
| 244 | float microseconds = seconds * base::Time::kMicrosecondsPerSecond; |
[email protected] | add5177 | 2009-06-11 18:25:17 | [diff] [blame] | 245 | if (seconds != 0) |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 246 | pipeline_->Seek( |
[email protected] | b9490b9c | 2009-05-12 01:01:52 | [diff] [blame] | 247 | base::TimeDelta::FromMicroseconds(static_cast<int64>(microseconds)), |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 248 | NewCallback(proxy_.get(), |
| 249 | &WebMediaPlayerImpl::Proxy::PipelineSeekCallback)); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 250 | } |
| 251 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 252 | void WebMediaPlayerImpl::setEndTime(float seconds) { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 253 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 254 | |
| 255 | // TODO(hclam): add method call when it has been implemented. |
| 256 | return; |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 257 | } |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 258 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 259 | void WebMediaPlayerImpl::setRate(float rate) { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 260 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 261 | |
[email protected] | 4948090 | 2009-07-14 20:23:43 | [diff] [blame^] | 262 | playback_rate_ = rate; |
| 263 | if (!paused_) { |
| 264 | pipeline_->SetPlaybackRate(rate); |
| 265 | } |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 266 | } |
| 267 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 268 | void WebMediaPlayerImpl::setVolume(float volume) { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 269 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 270 | |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 271 | pipeline_->SetVolume(volume); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 272 | } |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 273 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 274 | void WebMediaPlayerImpl::setVisible(bool visible) { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 275 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 276 | |
| 277 | // TODO(hclam): add appropriate method call when pipeline has it implemented. |
| 278 | return; |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 279 | } |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 280 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 281 | bool WebMediaPlayerImpl::setAutoBuffer(bool autoBuffer) { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 282 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 283 | |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | bool WebMediaPlayerImpl::totalBytesKnown() { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 288 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 289 | |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 290 | return pipeline_->GetTotalBytes() != 0; |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 291 | } |
| 292 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 293 | bool WebMediaPlayerImpl::hasVideo() const { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 294 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 295 | |
| 296 | size_t width, height; |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 297 | pipeline_->GetVideoSize(&width, &height); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 298 | return width != 0 && height != 0; |
| 299 | } |
| 300 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 301 | WebKit::WebSize WebMediaPlayerImpl::naturalSize() const { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 302 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 303 | |
| 304 | size_t width, height; |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 305 | pipeline_->GetVideoSize(&width, &height); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 306 | return WebKit::WebSize(width, height); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 307 | } |
| 308 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 309 | bool WebMediaPlayerImpl::paused() const { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 310 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 311 | |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 312 | return pipeline_->GetPlaybackRate() == 0.0f; |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 315 | bool WebMediaPlayerImpl::seeking() const { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 316 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 317 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 318 | return false; |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 319 | } |
| 320 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 321 | float WebMediaPlayerImpl::duration() const { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 322 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 323 | |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 324 | return static_cast<float>(pipeline_->GetDuration().InSecondsF()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 325 | } |
| 326 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 327 | float WebMediaPlayerImpl::currentTime() const { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 328 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 329 | |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 330 | return static_cast<float>(pipeline_->GetTime().InSecondsF()); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 331 | } |
| 332 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 333 | int WebMediaPlayerImpl::dataRate() const { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 334 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 335 | |
| 336 | // TODO(hclam): Add this method call if pipeline has it in the interface. |
| 337 | return 0; |
| 338 | } |
| 339 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 340 | float WebMediaPlayerImpl::maxTimeBuffered() const { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 341 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 342 | |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 343 | return static_cast<float>(pipeline_->GetBufferedTime().InSecondsF()); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 344 | } |
| 345 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 346 | float WebMediaPlayerImpl::maxTimeSeekable() const { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 347 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 348 | |
[email protected] | 7329360 | 2009-04-29 00:30:22 | [diff] [blame] | 349 | // TODO(scherkus): move this logic down into the pipeline. |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 350 | if (pipeline_->GetTotalBytes() == 0) { |
[email protected] | 7329360 | 2009-04-29 00:30:22 | [diff] [blame] | 351 | return 0.0f; |
| 352 | } |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 353 | double total_bytes = static_cast<double>(pipeline_->GetTotalBytes()); |
| 354 | double buffered_bytes = static_cast<double>(pipeline_->GetBufferedBytes()); |
| 355 | double duration = static_cast<double>(pipeline_->GetDuration().InSecondsF()); |
[email protected] | 7329360 | 2009-04-29 00:30:22 | [diff] [blame] | 356 | return static_cast<float>(duration * (buffered_bytes / total_bytes)); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 357 | } |
| 358 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 359 | unsigned long long WebMediaPlayerImpl::bytesLoaded() const { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 360 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 361 | |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 362 | return pipeline_->GetBufferedBytes(); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 363 | } |
| 364 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 365 | unsigned long long WebMediaPlayerImpl::totalBytes() const { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 366 | DCHECK(MessageLoop::current() == main_loop_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 367 | |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 368 | return pipeline_->GetTotalBytes(); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 369 | } |
| 370 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 371 | void WebMediaPlayerImpl::setSize(const WebSize& size) { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 372 | DCHECK(MessageLoop::current() == main_loop_); |
| 373 | DCHECK(proxy_); |
[email protected] | d43ed91 | 2009-02-03 04:52:53 | [diff] [blame] | 374 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 375 | proxy_->SetSize(gfx::Rect(0, 0, size.width, size.height)); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 376 | } |
| 377 | |
[email protected] | df143bdc | 2009-06-16 17:34:19 | [diff] [blame] | 378 | void WebMediaPlayerImpl::paint(WebCanvas* canvas, |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 379 | const WebRect& rect) { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 380 | DCHECK(MessageLoop::current() == main_loop_); |
| 381 | DCHECK(proxy_); |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 382 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 383 | proxy_->Paint(canvas, rect); |
[email protected] | ec9212f | 2008-12-18 21:40:36 | [diff] [blame] | 384 | } |
[email protected] | 5df5165 | 2009-01-17 00:03:00 | [diff] [blame] | 385 | |
[email protected] | 4e6be3f | 2009-05-07 02:24:44 | [diff] [blame] | 386 | void WebMediaPlayerImpl::WillDestroyCurrentMessageLoop() { |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 387 | Destroy(); |
| 388 | main_loop_ = NULL; |
| 389 | } |
| 390 | |
| 391 | void WebMediaPlayerImpl::Repaint() { |
| 392 | DCHECK(MessageLoop::current() == main_loop_); |
| 393 | GetClient()->repaint(); |
| 394 | } |
| 395 | |
| 396 | void WebMediaPlayerImpl::TimeChanged() { |
| 397 | DCHECK(MessageLoop::current() == main_loop_); |
| 398 | GetClient()->timeChanged(); |
| 399 | } |
| 400 | |
| 401 | void WebMediaPlayerImpl::SetNetworkState( |
| 402 | WebKit::WebMediaPlayer::NetworkState state) { |
| 403 | DCHECK(MessageLoop::current() == main_loop_); |
| 404 | if (network_state_ != state) { |
| 405 | network_state_ = state; |
| 406 | GetClient()->networkStateChanged(); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | void WebMediaPlayerImpl::SetReadyState( |
| 411 | WebKit::WebMediaPlayer::ReadyState state) { |
| 412 | DCHECK(MessageLoop::current() == main_loop_); |
| 413 | if (ready_state_ != state) { |
| 414 | ready_state_ = state; |
| 415 | GetClient()->readyStateChanged(); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void WebMediaPlayerImpl::Destroy() { |
| 420 | DCHECK(MessageLoop::current() == main_loop_); |
| 421 | |
| 422 | // Make sure to kill the pipeline so there's no more media threads running. |
[email protected] | 3b4cbbf | 2009-07-11 00:16:19 | [diff] [blame] | 423 | // TODO(hclam): stopping the pipeline might block for a long time. |
| 424 | pipeline_->Stop(NULL); |
| 425 | pipeline_thread_.Stop(); |
[email protected] | 5df5165 | 2009-01-17 00:03:00 | [diff] [blame] | 426 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 427 | // And then detach the proxy, it may live on the render thread for a little |
| 428 | // longer until all the tasks are finished. |
| 429 | if (proxy_) { |
| 430 | proxy_->Detach(); |
| 431 | proxy_ = NULL; |
[email protected] | a728102 | 2009-06-17 17:58:04 | [diff] [blame] | 432 | } |
[email protected] | 5df5165 | 2009-01-17 00:03:00 | [diff] [blame] | 433 | } |
| 434 | |
[email protected] | 8931c41a | 2009-07-07 17:31:49 | [diff] [blame] | 435 | WebKit::WebMediaPlayerClient* WebMediaPlayerImpl::GetClient() { |
| 436 | DCHECK(MessageLoop::current() == main_loop_); |
| 437 | DCHECK(client_); |
| 438 | return client_; |
[email protected] | 5df5165 | 2009-01-17 00:03:00 | [diff] [blame] | 439 | } |
[email protected] | add5177 | 2009-06-11 18:25:17 | [diff] [blame] | 440 | |
| 441 | } // namespace webkit_glue |