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" |
10 #include "cc/layers/video_frame_provider.h" | 12 #include "cc/layers/video_frame_provider.h" |
11 #include "media/base/media_export.h" | 13 #include "media/base/media_export.h" |
| 14 #include "media/base/video_renderer_sink.h" |
12 #include "ui/gfx/geometry/size.h" | 15 #include "ui/gfx/geometry/size.h" |
13 | 16 |
14 namespace media { | 17 namespace media { |
15 class VideoFrame; | 18 class VideoFrame; |
16 | 19 |
17 // VideoFrameCompositor handles incoming frames by notifying the compositor and | 20 // VideoFrameCompositor acts as a bridge between the media and cc layers for |
18 // dispatching callbacks when detecting changes in video frames. | 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 |
| 23 // from the cc side. |
19 // | 24 // |
20 // Typical usage is to deliver ready-to-be-displayed video frames to | 25 // This class is responsible for requesting new frames from a video renderer in |
21 // UpdateCurrentFrame() so that VideoFrameCompositor can take care of tracking | 26 // response to requests from the VFP::Client. Since the VFP::Client may stop |
22 // changes in video frames and firing callbacks as needed. | 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 |
23 // | 30 // |
24 // VideoFrameCompositor must live on the same thread as the compositor. | 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. |
| 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. |
25 class MEDIA_EXPORT VideoFrameCompositor | 42 class MEDIA_EXPORT VideoFrameCompositor |
26 : NON_EXPORTED_BASE(public cc::VideoFrameProvider) { | 43 : public VideoRendererSink, |
| 44 NON_EXPORTED_BASE(public cc::VideoFrameProvider) { |
27 public: | 45 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 // |
28 // |natural_size_changed_cb| is run with the new natural size of the video | 49 // |natural_size_changed_cb| is run with the new natural size of the video |
29 // frame whenever a change in natural size is detected. It is not called the | 50 // frame whenever a change in natural size is detected. It is not called the |
30 // first time UpdateCurrentFrame() is called. Run on the same thread as the | 51 // first time UpdateCurrentFrame() is called. Run on the same thread as the |
31 // caller of UpdateCurrentFrame(). | 52 // caller of UpdateCurrentFrame(). |
32 // | 53 // |
33 // |opacity_changed_cb| is run when a change in opacity is detected. It *is* | 54 // |opacity_changed_cb| is run when a change in opacity is detected. It *is* |
34 // called the first time UpdateCurrentFrame() is called. Run on the same | 55 // called the first time UpdateCurrentFrame() is called. Run on the same |
35 // thread as the caller of UpdateCurrentFrame(). | 56 // thread as the caller of UpdateCurrentFrame(). |
36 // | 57 // |
37 // TODO(scherkus): Investigate the inconsistency between the callbacks with | 58 // TODO(dalecurtis): Investigate the inconsistency between the callbacks with |
38 // respect to why we don't call |natural_size_changed_cb| on the first frame. | 59 // respect to why we don't call |natural_size_changed_cb| on the first frame. |
39 // I suspect it was for historical reasons that no longer make sense. | 60 // I suspect it was for historical reasons that no longer make sense. |
40 VideoFrameCompositor( | 61 VideoFrameCompositor( |
| 62 const scoped_refptr<base::SingleThreadTaskRunner>& compositor_task_runner, |
41 const base::Callback<void(gfx::Size)>& natural_size_changed_cb, | 63 const base::Callback<void(gfx::Size)>& natural_size_changed_cb, |
42 const base::Callback<void(bool)>& opacity_changed_cb); | 64 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. |
43 ~VideoFrameCompositor() override; | 68 ~VideoFrameCompositor() override; |
44 | 69 |
45 // cc::VideoFrameProvider implementation. | 70 // Returns |current_frame_| if it was refreshed recently; otherwise, if |
| 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_|. |
46 void SetVideoFrameProviderClient( | 80 void SetVideoFrameProviderClient( |
47 cc::VideoFrameProvider::Client* client) override; | 81 cc::VideoFrameProvider::Client* client) override; |
| 82 bool UpdateCurrentFrame(base::TimeTicks deadline_min, |
| 83 base::TimeTicks deadline_max) override; |
48 scoped_refptr<VideoFrame> GetCurrentFrame() override; | 84 scoped_refptr<VideoFrame> GetCurrentFrame() override; |
49 void PutCurrentFrame(const scoped_refptr<VideoFrame>& frame) override; | 85 void PutCurrentFrame() override; |
50 | 86 |
51 // Updates the current frame and notifies the compositor. | 87 // VideoRendererSink implementation. These methods must be called from the |
52 void UpdateCurrentFrame(const scoped_refptr<VideoFrame>& frame); | 88 // same thread (typically the media thread). |
| 89 void Start(RenderCallback* callback) override; |
| 90 void Stop() override; |
| 91 void PaintFrameUsingOldRenderingPath( |
| 92 const scoped_refptr<VideoFrame>& frame) override; |
53 | 93 |
54 private: | 94 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. |
55 base::Callback<void(gfx::Size)> natural_size_changed_cb_; | 102 base::Callback<void(gfx::Size)> natural_size_changed_cb_; |
56 base::Callback<void(bool)> opacity_changed_cb_; | 103 base::Callback<void(bool)> opacity_changed_cb_; |
57 | 104 |
| 105 // These values are only set and read on the compositor thread. |
58 cc::VideoFrameProvider::Client* client_; | 106 cc::VideoFrameProvider::Client* client_; |
| 107 scoped_refptr<VideoFrame> current_frame_; |
59 | 108 |
60 scoped_refptr<VideoFrame> current_frame_; | 109 // These values are updated and read from the media and compositor threads. |
| 110 base::Lock lock_; |
| 111 bool rendering_; |
| 112 VideoRendererSink::RenderCallback* callback_; |
61 | 113 |
62 DISALLOW_COPY_AND_ASSIGN(VideoFrameCompositor); | 114 DISALLOW_COPY_AND_ASSIGN(VideoFrameCompositor); |
63 }; | 115 }; |
64 | 116 |
65 } // namespace media | 117 } // namespace media |
66 | 118 |
67 #endif // MEDIA_BLINK_VIDEO_FRAME_COMPOSITOR_H_ | 119 #endif // MEDIA_BLINK_VIDEO_FRAME_COMPOSITOR_H_ |
OLD | NEW |