1 // Copyright 2014 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 #ifndef MEDIA_BLINK_VIDEO_FRAME_COMPOSITOR_H_
6 #define MEDIA_BLINK_VIDEO_FRAME_COMPOSITOR_H_
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/synchronization/lock.h"
12 #include "cc/layers/video_frame_provider.h"
13 #include "media/base/media_export.h"
14 #include "media/base/video_renderer_sink.h"
15 #include "ui/gfx/geometry/size.h"
20 // VideoFrameCompositor acts as a bridge between the media and cc layers for
21 // rendering video frames. I.e. a media::VideoRenderer will talk to this class
22 // from the media side, while a cc::VideoFrameProvider::Client will talk to it
25 // This class is responsible for requesting new frames from a video renderer in
26 // response to requests from the VFP::Client. Since the VFP::Client may stop
27 // issuing requests in response to visibility changes it is also responsible for
28 // ensuring the "freshness" of the current frame for programmatic frame
29 // requests; e.g., Canvas.drawImage() requests
31 // This class is also responsible for detecting frames dropped by the compositor
32 // after rendering and signaling that information to a RenderCallback. It
33 // detects frames not dropped by verifying each GetCurrentFrame() is followed
34 // by a PutCurrentFrame() before the next UpdateCurrentFrame() call.
36 // VideoRenderSink::RenderCallback implementations must call Start() and Stop()
37 // once new frames are expected or are no longer expected to be ready; this data
38 // is relayed to the compositor to avoid extraneous callbacks.
40 // VideoFrameCompositor must live on the same thread as the compositor, though
41 // it may be constructed on any thread.
42 class MEDIA_EXPORT VideoFrameCompositor
43 : public VideoRendererSink
,
44 NON_EXPORTED_BASE(public cc::VideoFrameProvider
) {
46 // |compositor_task_runner| is the task runner on which this class will live,
47 // though it may be constructed on any thread.
49 // |natural_size_changed_cb| is run with the new natural size of the video
50 // frame whenever a change in natural size is detected. It is not called the
51 // first time UpdateCurrentFrame() is called. Run on the same thread as the
52 // caller of UpdateCurrentFrame().
54 // |opacity_changed_cb| is run when a change in opacity is detected. It *is*
55 // called the first time UpdateCurrentFrame() is called. Run on the same
56 // thread as the caller of UpdateCurrentFrame().
58 // TODO(dalecurtis): Investigate the inconsistency between the callbacks with
59 // respect to why we don't call |natural_size_changed_cb| on the first frame.
60 // I suspect it was for historical reasons that no longer make sense.
62 const scoped_refptr
<base::SingleThreadTaskRunner
>& compositor_task_runner
,
63 const base::Callback
<void(gfx::Size
)>& natural_size_changed_cb
,
64 const base::Callback
<void(bool)>& opacity_changed_cb
);
66 // Destruction must happen on the compositor thread; Stop() must have been
67 // called before destruction starts.
68 ~VideoFrameCompositor() override
;
70 // Returns |current_frame_| if it was refreshed recently; otherwise, if
71 // |callback_| is available, requests a new frame and returns that one.
73 // This is required for programmatic frame requests where the compositor may
74 // have stopped issuing UpdateCurrentFrame() callbacks in response to
75 // visibility changes in the output layer.
76 scoped_refptr
<VideoFrame
> GetCurrentFrameAndUpdateIfStale();
78 // cc::VideoFrameProvider implementation. These methods must be called on the
79 // |compositor_task_runner_|.
80 void SetVideoFrameProviderClient(
81 cc::VideoFrameProvider::Client
* client
) override
;
82 bool UpdateCurrentFrame(base::TimeTicks deadline_min
,
83 base::TimeTicks deadline_max
) override
;
84 scoped_refptr
<VideoFrame
> GetCurrentFrame() override
;
85 void PutCurrentFrame() override
;
87 // VideoRendererSink implementation. These methods must be called from the
88 // same thread (typically the media thread).
89 void Start(RenderCallback
* callback
) override
;
91 void PaintFrameUsingOldRenderingPath(
92 const scoped_refptr
<VideoFrame
>& frame
) override
;
95 // Called on the compositor thread to start or stop rendering if rendering was
96 // previously started or stopped before we had a |callback_|.
97 void OnRendererStateUpdate();
99 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_task_runner_
;
101 // These callbacks are executed on the compositor thread.
102 base::Callback
<void(gfx::Size
)> natural_size_changed_cb_
;
103 base::Callback
<void(bool)> opacity_changed_cb_
;
105 // These values are only set and read on the compositor thread.
106 cc::VideoFrameProvider::Client
* client_
;
107 scoped_refptr
<VideoFrame
> current_frame_
;
109 // These values are updated and read from the media and compositor threads.
112 VideoRendererSink::RenderCallback
* callback_
;
114 DISALLOW_COPY_AND_ASSIGN(VideoFrameCompositor
);
119 #endif // MEDIA_BLINK_VIDEO_FRAME_COMPOSITOR_H_