1 // Copyright (c) 2012 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.
5 #include "webkit/media/webmediaplayer_proxy.h"
8 #include "base/logging.h"
9 #include "base/message_loop_proxy.h"
10 #include "media/base/pipeline_status.h"
11 #include "media/filters/video_renderer_base.h"
12 #include "webkit/media/webmediaplayer_impl.h"
14 using media::PipelineStatus
;
16 namespace webkit_media
{
18 // Limits the maximum outstanding repaints posted on render thread.
19 // This number of 50 is a guess, it does not take too much memory on the task
20 // queue but gives up a pretty good latency on repaint.
21 static const int kMaxOutstandingRepaints
= 50;
23 WebMediaPlayerProxy::WebMediaPlayerProxy(
24 const scoped_refptr
<base::MessageLoopProxy
>& render_loop
,
25 WebMediaPlayerImpl
* webmediaplayer
)
26 : render_loop_(render_loop
),
27 webmediaplayer_(webmediaplayer
),
28 outstanding_repaints_(0) {
30 DCHECK(webmediaplayer_
);
33 WebMediaPlayerProxy::~WebMediaPlayerProxy() {
37 void WebMediaPlayerProxy::FrameReady(
38 const scoped_refptr
<media::VideoFrame
>& frame
) {
39 base::AutoLock
auto_lock(lock_
);
40 current_frame_
= frame
;
42 if (outstanding_repaints_
< kMaxOutstandingRepaints
) {
43 ++outstanding_repaints_
;
45 render_loop_
->PostTask(FROM_HERE
, base::Bind(
46 &WebMediaPlayerProxy::RepaintTask
, this));
50 void WebMediaPlayerProxy::Paint(SkCanvas
* canvas
,
51 const gfx::Rect
& dest_rect
,
53 DCHECK(render_loop_
->BelongsToCurrentThread());
55 // Use GetCurrentFrame() to avoid locking while painting in software.
56 scoped_refptr
<media::VideoFrame
> video_frame
;
57 GetCurrentFrame(&video_frame
);
58 video_renderer_
.Paint(video_frame
, canvas
, dest_rect
, alpha
);
61 bool WebMediaPlayerProxy::HasSingleOrigin() {
62 DCHECK(render_loop_
->BelongsToCurrentThread());
64 return data_source_
->HasSingleOrigin();
68 bool WebMediaPlayerProxy::DidPassCORSAccessCheck() const {
69 DCHECK(render_loop_
->BelongsToCurrentThread());
71 return data_source_
->DidPassCORSAccessCheck();
75 void WebMediaPlayerProxy::AbortDataSource() {
76 DCHECK(render_loop_
->BelongsToCurrentThread());
78 data_source_
->Abort();
81 void WebMediaPlayerProxy::Detach() {
82 DCHECK(render_loop_
->BelongsToCurrentThread());
83 webmediaplayer_
= NULL
;
87 void WebMediaPlayerProxy::RepaintTask() {
88 DCHECK(render_loop_
->BelongsToCurrentThread());
90 base::AutoLock
auto_lock(lock_
);
91 --outstanding_repaints_
;
92 DCHECK_GE(outstanding_repaints_
, 0);
94 if (webmediaplayer_
) {
95 webmediaplayer_
->Repaint();
99 void WebMediaPlayerProxy::GetCurrentFrame(
100 scoped_refptr
<media::VideoFrame
>* frame_out
) {
101 base::AutoLock
auto_lock(lock_
);
102 *frame_out
= current_frame_
;
105 } // namespace webkit_media