• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2015 GitHub, Inc.
2 // Use of this source code is governed by the MIT license that can be
3 // found in the LICENSE file.
4 
5 #include "libcef/browser/osr/video_consumer_osr.h"
6 
7 #include "libcef/browser/osr/render_widget_host_view_osr.h"
8 
9 #include "media/base/video_frame_metadata.h"
10 #include "media/capture/mojom/video_capture_buffer.mojom.h"
11 #include "media/capture/mojom/video_capture_types.mojom.h"
12 #include "ui/gfx/skbitmap_operations.h"
13 
14 namespace {
15 
16 // Helper to always call Done() at the end of OnFrameCaptured().
17 class ScopedVideoFrameDone {
18  public:
ScopedVideoFrameDone(mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks> callbacks)19   explicit ScopedVideoFrameDone(
20       mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
21           callbacks)
22       : callbacks_(std::move(callbacks)) {}
~ScopedVideoFrameDone()23   ~ScopedVideoFrameDone() { callbacks_->Done(); }
24 
25  private:
26   mojo::Remote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks> callbacks_;
27 };
28 
29 }  // namespace
30 
CefVideoConsumerOSR(CefRenderWidgetHostViewOSR * view)31 CefVideoConsumerOSR::CefVideoConsumerOSR(CefRenderWidgetHostViewOSR* view)
32     : view_(view), video_capturer_(view->CreateVideoCapturer()) {
33   video_capturer_->SetFormat(media::PIXEL_FORMAT_ARGB);
34 
35   // Always use the highest resolution within constraints that doesn't exceed
36   // the source size.
37   video_capturer_->SetAutoThrottlingEnabled(false);
38   video_capturer_->SetMinSizeChangePeriod(base::TimeDelta());
39 
40   SizeChanged(view_->SizeInPixels());
41   SetActive(true);
42 }
43 
44 CefVideoConsumerOSR::~CefVideoConsumerOSR() = default;
45 
SetActive(bool active)46 void CefVideoConsumerOSR::SetActive(bool active) {
47   if (active) {
48     video_capturer_->Start(this, viz::mojom::BufferFormatPreference::kDefault);
49   } else {
50     video_capturer_->Stop();
51   }
52 }
53 
SetFrameRate(base::TimeDelta frame_rate)54 void CefVideoConsumerOSR::SetFrameRate(base::TimeDelta frame_rate) {
55   video_capturer_->SetMinCapturePeriod(frame_rate);
56 }
57 
SizeChanged(const gfx::Size & size_in_pixels)58 void CefVideoConsumerOSR::SizeChanged(const gfx::Size& size_in_pixels) {
59   if (size_in_pixels_ == size_in_pixels)
60     return;
61   size_in_pixels_ = size_in_pixels;
62 
63   // Capture resolution will be held constant.
64   video_capturer_->SetResolutionConstraints(size_in_pixels, size_in_pixels,
65                                             true /* use_fixed_aspect_ratio */);
66 }
67 
RequestRefreshFrame(const absl::optional<gfx::Rect> & bounds_in_pixels)68 void CefVideoConsumerOSR::RequestRefreshFrame(
69     const absl::optional<gfx::Rect>& bounds_in_pixels) {
70   bounds_in_pixels_ = bounds_in_pixels;
71   video_capturer_->RequestRefreshFrame();
72 }
73 
74 // Frame size values are as follows:
75 //   info->coded_size = Width and height of the video frame. Not all pixels in
76 //   this region are valid.
77 //   info->visible_rect = Region of coded_size that contains image data, also
78 //   known as the clean aperture.
79 //   content_rect = Region of the frame that contains the captured content, with
80 //   the rest of the frame having been letterboxed to adhere to resolution
81 //   constraints.
OnFrameCaptured(media::mojom::VideoBufferHandlePtr data,media::mojom::VideoFrameInfoPtr info,const gfx::Rect & content_rect,mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks> callbacks)82 void CefVideoConsumerOSR::OnFrameCaptured(
83     media::mojom::VideoBufferHandlePtr data,
84     media::mojom::VideoFrameInfoPtr info,
85     const gfx::Rect& content_rect,
86     mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
87         callbacks) {
88   ScopedVideoFrameDone scoped_done(std::move(callbacks));
89 
90   CHECK(data->is_read_only_shmem_region());
91   base::ReadOnlySharedMemoryRegion& shmem_region =
92       data->get_read_only_shmem_region();
93 
94   // The |data| parameter is not nullable and mojo type mapping for
95   // `base::ReadOnlySharedMemoryRegion` defines that nullable version of it is
96   // the same type, with null check being equivalent to IsValid() check. Given
97   // the above, we should never be able to receive a read only shmem region that
98   // is not valid - mojo will enforce it for us.
99   DCHECK(shmem_region.IsValid());
100 
101   base::ReadOnlySharedMemoryMapping mapping = shmem_region.Map();
102   if (!mapping.IsValid()) {
103     DLOG(ERROR) << "Shared memory mapping failed.";
104     return;
105   }
106   if (mapping.size() <
107       media::VideoFrame::AllocationSize(info->pixel_format, info->coded_size)) {
108     DLOG(ERROR) << "Shared memory size was less than expected.";
109     return;
110   }
111 
112   // The SkBitmap's pixels will be marked as immutable, but the installPixels()
113   // API requires a non-const pointer. So, cast away the const.
114   void* const pixels = const_cast<void*>(mapping.memory());
115 
116   media::VideoFrameMetadata metadata = info->metadata;
117   gfx::Rect damage_rect;
118 
119   if (bounds_in_pixels_) {
120     // Use the bounds passed to RequestRefreshFrame().
121     damage_rect = gfx::Rect(info->coded_size);
122     damage_rect.Intersect(*bounds_in_pixels_);
123     bounds_in_pixels_ = absl::nullopt;
124   } else {
125     // Retrieve the rectangular region of the frame that has changed since the
126     // frame with the directly preceding CAPTURE_COUNTER. If that frame was not
127     // received, typically because it was dropped during transport from the
128     // producer, clients must assume that the entire frame has changed.
129     // This rectangle is relative to the full frame data, i.e. [0, 0,
130     // coded_size.width(), coded_size.height()]. It does not have to be
131     // fully contained within visible_rect.
132     if (metadata.capture_update_rect) {
133       damage_rect = *metadata.capture_update_rect;
134     }
135     if (damage_rect.IsEmpty()) {
136       damage_rect = gfx::Rect(info->coded_size);
137     }
138   }
139 
140   view_->OnPaint(damage_rect, info->coded_size, pixels);
141 }
142 
OnStopped()143 void CefVideoConsumerOSR::OnStopped() {}
144 
OnLog(const std::string & message)145 void CefVideoConsumerOSR::OnLog(const std::string& message) {}
146