• 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_types.mojom.h"
11 #include "ui/gfx/skbitmap_operations.h"
12 
13 namespace {
14 
15 // Helper to always call Done() at the end of OnFrameCaptured().
16 class ScopedVideoFrameDone {
17  public:
ScopedVideoFrameDone(mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks> callbacks)18   explicit ScopedVideoFrameDone(
19       mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
20           callbacks)
21       : callbacks_(std::move(callbacks)) {}
~ScopedVideoFrameDone()22   ~ScopedVideoFrameDone() { callbacks_->Done(); }
23 
24  private:
25   mojo::Remote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks> callbacks_;
26 };
27 
28 }  // namespace
29 
CefVideoConsumerOSR(CefRenderWidgetHostViewOSR * view)30 CefVideoConsumerOSR::CefVideoConsumerOSR(CefRenderWidgetHostViewOSR* view)
31     : view_(view), video_capturer_(view->CreateVideoCapturer()) {
32   video_capturer_->SetFormat(media::PIXEL_FORMAT_ARGB,
33                              gfx::ColorSpace::CreateREC709());
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);
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 base::Optional<gfx::Rect> & bounds_in_pixels)68 void CefVideoConsumerOSR::RequestRefreshFrame(
69     const base::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(base::ReadOnlySharedMemoryRegion data,::media::mojom::VideoFrameInfoPtr info,const gfx::Rect & content_rect,mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks> callbacks)82 void CefVideoConsumerOSR::OnFrameCaptured(
83     base::ReadOnlySharedMemoryRegion 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   if (!data.IsValid())
91     return;
92 
93   base::ReadOnlySharedMemoryMapping mapping = data.Map();
94   if (!mapping.IsValid()) {
95     DLOG(ERROR) << "Shared memory mapping failed.";
96     return;
97   }
98   if (mapping.size() <
99       media::VideoFrame::AllocationSize(info->pixel_format, info->coded_size)) {
100     DLOG(ERROR) << "Shared memory size was less than expected.";
101     return;
102   }
103 
104   // The SkBitmap's pixels will be marked as immutable, but the installPixels()
105   // API requires a non-const pointer. So, cast away the const.
106   void* const pixels = const_cast<void*>(mapping.memory());
107 
108   media::VideoFrameMetadata metadata = info->metadata;
109   gfx::Rect damage_rect;
110 
111   if (bounds_in_pixels_) {
112     // Use the bounds passed to RequestRefreshFrame().
113     damage_rect = gfx::Rect(info->coded_size);
114     damage_rect.Intersect(*bounds_in_pixels_);
115     bounds_in_pixels_ = base::nullopt;
116   } else {
117     // Retrieve the rectangular region of the frame that has changed since the
118     // frame with the directly preceding CAPTURE_COUNTER. If that frame was not
119     // received, typically because it was dropped during transport from the
120     // producer, clients must assume that the entire frame has changed.
121     // This rectangle is relative to the full frame data, i.e. [0, 0,
122     // coded_size.width(), coded_size.height()]. It does not have to be
123     // fully contained within visible_rect.
124     if (metadata.capture_update_rect) {
125       damage_rect = *metadata.capture_update_rect;
126     }
127     if (damage_rect.IsEmpty()) {
128       damage_rect = gfx::Rect(info->coded_size);
129     }
130   }
131 
132   view_->OnPaint(damage_rect, info->coded_size, pixels);
133 }
134 
OnStopped()135 void CefVideoConsumerOSR::OnStopped() {}
136 
OnLog(const std::string & message)137 void CefVideoConsumerOSR::OnLog(const std::string& message) {}
138