OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_BLINK_VIDEO_FRAME_COMPOSITOR_H_ | 5 #ifndef MEDIA_BLINK_VIDEO_FRAME_COMPOSITOR_H_ |
6 #define MEDIA_BLINK_VIDEO_FRAME_COMPOSITOR_H_ | 6 #define MEDIA_BLINK_VIDEO_FRAME_COMPOSITOR_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/ref_counted.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" | 10 #include "cc/layers/video_frame_provider.h" |
13 #include "media/base/media_export.h" | 11 #include "media/base/media_export.h" |
14 #include "media/base/video_renderer_sink.h" | |
15 #include "ui/gfx/geometry/size.h" | 12 #include "ui/gfx/geometry/size.h" |
16 | 13 |
17 namespace media { | 14 namespace media { |
18 class VideoFrame; | 15 class VideoFrame; |
19 | 16 |
20 // VideoFrameCompositor acts as a bridge between the media and cc layers for | 17 // VideoFrameCompositor handles incoming frames by notifying the compositor and |
21 // rendering video frames. I.e. a media::VideoRenderer will talk to this class | 18 // dispatching callbacks when detecting changes in video frames. |
22 // from the media side, while a cc::VideoFrameProvider::Client will talk to it | |
23 // from the cc side. | |
24 // | 19 // |
25 // This class is responsible for requesting new frames from a video renderer in | 20 // Typical usage is to deliver ready-to-be-displayed video frames to |
26 // response to requests from the VFP::Client. Since the VFP::Client may stop | 21 // UpdateCurrentFrame() so that VideoFrameCompositor can take care of tracking |
27 // issuing requests in response to visibility changes it is also responsible for | 22 // changes in video frames and firing callbacks as needed. |
28 // ensuring the "freshness" of the current frame for programmatic frame | |
29 // requests; e.g., Canvas.drawImage() requests | |
30 // | 23 // |
31 // This class is also responsible for detecting frames dropped by the compositor | 24 // VideoFrameCompositor must live on the same thread as 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. | |
35 // | |
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. | |
39 // | |
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 | 25 class MEDIA_EXPORT VideoFrameCompositor |
43 : public VideoRendererSink, | 26 : NON_EXPORTED_BASE(public cc::VideoFrameProvider) { |
44 NON_EXPORTED_BASE(public cc::VideoFrameProvider) { | |
45 public: | 27 public: |
46 // |compositor_task_runner| is the task runner on which this class will live, | |
47 // though it may be constructed on any thread. | |
48 // | |
49 // |natural_size_changed_cb| is run with the new natural size of the video | 28 // |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 | 29 // 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 | 30 // first time UpdateCurrentFrame() is called. Run on the same thread as the |
52 // caller of UpdateCurrentFrame(). | 31 // caller of UpdateCurrentFrame(). |
53 // | 32 // |
54 // |opacity_changed_cb| is run when a change in opacity is detected. It *is* | 33 // |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 | 34 // called the first time UpdateCurrentFrame() is called. Run on the same |
56 // thread as the caller of UpdateCurrentFrame(). | 35 // thread as the caller of UpdateCurrentFrame(). |
57 // | 36 // |
58 // TODO(dalecurtis): Investigate the inconsistency between the callbacks with | 37 // TODO(scherkus): Investigate the inconsistency between the callbacks with |
59 // respect to why we don't call |natural_size_changed_cb| on the first frame. | 38 // 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. | 39 // I suspect it was for historical reasons that no longer make sense. |
61 VideoFrameCompositor( | 40 VideoFrameCompositor( |
62 const scoped_refptr<base::SingleThreadTaskRunner>& compositor_task_runner, | |
63 const base::Callback<void(gfx::Size)>& natural_size_changed_cb, | 41 const base::Callback<void(gfx::Size)>& natural_size_changed_cb, |
64 const base::Callback<void(bool)>& opacity_changed_cb); | 42 const base::Callback<void(bool)>& opacity_changed_cb); |
65 | |
66 // Destruction must happen on the compositor thread; Stop() must have been | |
67 // called before destruction starts. | |
68 ~VideoFrameCompositor() override; | 43 ~VideoFrameCompositor() override; |
69 | 44 |
70 // Returns |current_frame_| if it was refreshed recently; otherwise, if | 45 // cc::VideoFrameProvider implementation. |
71 // |callback_| is available, requests a new frame and returns that one. | |
72 // | |
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(); | |
77 | |
78 // cc::VideoFrameProvider implementation. These methods must be called on the | |
79 // |compositor_task_runner_|. | |
80 void SetVideoFrameProviderClient( | 46 void SetVideoFrameProviderClient( |
81 cc::VideoFrameProvider::Client* client) override; | 47 cc::VideoFrameProvider::Client* client) override; |
82 bool UpdateCurrentFrame(base::TimeTicks deadline_min, | |
83 base::TimeTicks deadline_max) override; | |
84 scoped_refptr<VideoFrame> GetCurrentFrame() override; | 48 scoped_refptr<VideoFrame> GetCurrentFrame() override; |
85 void PutCurrentFrame() override; | 49 void PutCurrentFrame(const scoped_refptr<VideoFrame>& frame) override; |
86 | 50 |
87 // VideoRendererSink implementation. These methods must be called from the | 51 // Updates the current frame and notifies the compositor. |
88 // same thread (typically the media thread). | 52 void UpdateCurrentFrame(const scoped_refptr<VideoFrame>& frame); |
89 void Start(RenderCallback* callback) override; | |
90 void Stop() override; | |
91 void PaintFrameUsingOldRenderingPath( | |
92 const scoped_refptr<VideoFrame>& frame) override; | |
93 | 53 |
94 private: | 54 private: |
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(); | |
98 | |
99 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_; | |
100 | |
101 // These callbacks are executed on the compositor thread. | |
102 base::Callback<void(gfx::Size)> natural_size_changed_cb_; | 55 base::Callback<void(gfx::Size)> natural_size_changed_cb_; |
103 base::Callback<void(bool)> opacity_changed_cb_; | 56 base::Callback<void(bool)> opacity_changed_cb_; |
104 | 57 |
105 // These values are only set and read on the compositor thread. | |
106 cc::VideoFrameProvider::Client* client_; | 58 cc::VideoFrameProvider::Client* client_; |
| 59 |
107 scoped_refptr<VideoFrame> current_frame_; | 60 scoped_refptr<VideoFrame> current_frame_; |
108 | 61 |
109 // These values are updated and read from the media and compositor threads. | |
110 base::Lock lock_; | |
111 bool rendering_; | |
112 VideoRendererSink::RenderCallback* callback_; | |
113 | |
114 DISALLOW_COPY_AND_ASSIGN(VideoFrameCompositor); | 62 DISALLOW_COPY_AND_ASSIGN(VideoFrameCompositor); |
115 }; | 63 }; |
116 | 64 |
117 } // namespace media | 65 } // namespace media |
118 | 66 |
119 #endif // MEDIA_BLINK_VIDEO_FRAME_COMPOSITOR_H_ | 67 #endif // MEDIA_BLINK_VIDEO_FRAME_COMPOSITOR_H_ |
OLD | NEW |