2019-07-16 22:09:04 +02:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/osr/video_consumer_osr.h"
|
2019-07-16 22:09:04 +02:00
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/osr/render_widget_host_view_osr.h"
|
2019-07-16 22:09:04 +02:00
|
|
|
#include "media/base/video_frame_metadata.h"
|
2021-07-23 18:40:13 +02:00
|
|
|
#include "media/capture/mojom/video_capture_buffer.mojom.h"
|
2019-07-16 22:09:04 +02:00
|
|
|
#include "media/capture/mojom/video_capture_types.mojom.h"
|
|
|
|
#include "ui/gfx/skbitmap_operations.h"
|
|
|
|
|
2024-03-08 13:44:56 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
|
|
#include "ipc/service/gpu_memory_buffer_factory_dxgi.h"
|
|
|
|
#elif BUILDFLAG(IS_APPLE)
|
|
|
|
#include "ipc/service/gpu_memory_buffer_factory_io_surface.h"
|
|
|
|
#endif
|
|
|
|
|
2020-01-31 23:54:26 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Helper to always call Done() at the end of OnFrameCaptured().
|
|
|
|
class ScopedVideoFrameDone {
|
|
|
|
public:
|
|
|
|
explicit ScopedVideoFrameDone(
|
|
|
|
mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
|
|
|
|
callbacks)
|
|
|
|
: callbacks_(std::move(callbacks)) {}
|
|
|
|
~ScopedVideoFrameDone() { callbacks_->Done(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
mojo::Remote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks> callbacks_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2024-03-08 13:44:56 +01:00
|
|
|
CefVideoConsumerOSR::CefVideoConsumerOSR(CefRenderWidgetHostViewOSR* view,
|
|
|
|
bool use_shared_texture)
|
|
|
|
: use_shared_texture_(use_shared_texture),
|
|
|
|
view_(view),
|
|
|
|
video_capturer_(view->CreateVideoCapturer()) {
|
2021-12-16 23:35:54 +01:00
|
|
|
video_capturer_->SetFormat(media::PIXEL_FORMAT_ARGB);
|
2020-01-31 23:54:26 +01:00
|
|
|
|
|
|
|
// Always use the highest resolution within constraints that doesn't exceed
|
|
|
|
// the source size.
|
|
|
|
video_capturer_->SetAutoThrottlingEnabled(false);
|
|
|
|
video_capturer_->SetMinSizeChangePeriod(base::TimeDelta());
|
|
|
|
|
|
|
|
SizeChanged(view_->SizeInPixels());
|
|
|
|
SetActive(true);
|
2019-07-16 22:09:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefVideoConsumerOSR::~CefVideoConsumerOSR() = default;
|
|
|
|
|
|
|
|
void CefVideoConsumerOSR::SetActive(bool active) {
|
|
|
|
if (active) {
|
2024-03-08 13:44:56 +01:00
|
|
|
video_capturer_->Start(
|
|
|
|
this, use_shared_texture_
|
|
|
|
? viz::mojom::BufferFormatPreference::kPreferGpuMemoryBuffer
|
|
|
|
: viz::mojom::BufferFormatPreference::kDefault);
|
2019-07-16 22:09:04 +02:00
|
|
|
} else {
|
|
|
|
video_capturer_->Stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefVideoConsumerOSR::SetFrameRate(base::TimeDelta frame_rate) {
|
|
|
|
video_capturer_->SetMinCapturePeriod(frame_rate);
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:54:26 +01:00
|
|
|
void CefVideoConsumerOSR::SizeChanged(const gfx::Size& size_in_pixels) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (size_in_pixels_ == size_in_pixels) {
|
2020-01-31 23:54:26 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-01-31 23:54:26 +01:00
|
|
|
size_in_pixels_ = size_in_pixels;
|
|
|
|
|
|
|
|
// Capture resolution will be held constant.
|
|
|
|
video_capturer_->SetResolutionConstraints(size_in_pixels, size_in_pixels,
|
|
|
|
true /* use_fixed_aspect_ratio */);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefVideoConsumerOSR::RequestRefreshFrame(
|
2024-03-19 22:11:42 +01:00
|
|
|
const std::optional<gfx::Rect>& bounds_in_pixels) {
|
2020-01-31 23:54:26 +01:00
|
|
|
bounds_in_pixels_ = bounds_in_pixels;
|
2019-07-16 22:09:04 +02:00
|
|
|
video_capturer_->RequestRefreshFrame();
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:54:26 +01:00
|
|
|
// Frame size values are as follows:
|
|
|
|
// info->coded_size = Width and height of the video frame. Not all pixels in
|
|
|
|
// this region are valid.
|
|
|
|
// info->visible_rect = Region of coded_size that contains image data, also
|
|
|
|
// known as the clean aperture.
|
|
|
|
// content_rect = Region of the frame that contains the captured content, with
|
|
|
|
// the rest of the frame having been letterboxed to adhere to resolution
|
|
|
|
// constraints.
|
2019-07-16 22:09:04 +02:00
|
|
|
void CefVideoConsumerOSR::OnFrameCaptured(
|
2021-12-16 23:35:54 +01:00
|
|
|
media::mojom::VideoBufferHandlePtr data,
|
|
|
|
media::mojom::VideoFrameInfoPtr info,
|
2019-07-16 22:09:04 +02:00
|
|
|
const gfx::Rect& content_rect,
|
2020-02-10 18:10:17 +01:00
|
|
|
mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
|
|
|
|
callbacks) {
|
2020-01-31 23:54:26 +01:00
|
|
|
ScopedVideoFrameDone scoped_done(std::move(callbacks));
|
2019-07-16 22:09:04 +02:00
|
|
|
|
2024-03-08 13:44:56 +01:00
|
|
|
// If it is GPU Texture OSR.
|
|
|
|
if (use_shared_texture_) {
|
|
|
|
CHECK(data->is_gpu_memory_buffer_handle() &&
|
|
|
|
(info->pixel_format == media::PIXEL_FORMAT_ARGB ||
|
|
|
|
info->pixel_format == media::PIXEL_FORMAT_ABGR));
|
|
|
|
|
|
|
|
// The info->pixel_format will tell if the texture is RGBA or BGRA
|
|
|
|
// On Linux, X11 lacks support for RGBA_8888 so it might be BGRA.
|
|
|
|
// On Windows and macOS, it should always be RGBA.
|
|
|
|
auto pixel_format = info->pixel_format == media::PIXEL_FORMAT_ABGR
|
|
|
|
? CEF_COLOR_TYPE_RGBA_8888
|
|
|
|
: CEF_COLOR_TYPE_BGRA_8888;
|
|
|
|
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
|
|
auto& gmb_handle = data->get_gpu_memory_buffer_handle();
|
|
|
|
cef_accelerated_paint_info_t paint_info;
|
|
|
|
paint_info.shared_texture_handle = gmb_handle.dxgi_handle.Get();
|
|
|
|
paint_info.format = pixel_format;
|
|
|
|
view_->OnAcceleratedPaint(content_rect, info->coded_size, paint_info);
|
|
|
|
#elif BUILDFLAG(IS_APPLE)
|
|
|
|
auto& gmb_handle = data->get_gpu_memory_buffer_handle();
|
|
|
|
cef_accelerated_paint_info_t paint_info;
|
|
|
|
paint_info.shared_texture_io_surface = gmb_handle.io_surface.get();
|
|
|
|
paint_info.format = pixel_format;
|
|
|
|
view_->OnAcceleratedPaint(content_rect, info->coded_size, paint_info);
|
|
|
|
#elif BUILDFLAG(IS_LINUX)
|
|
|
|
auto& gmb_handle = data->get_gpu_memory_buffer_handle();
|
|
|
|
auto& native_pixmap = gmb_handle.native_pixmap_handle;
|
|
|
|
CHECK(native_pixmap.planes.size() <= kAcceleratedPaintMaxPlanes);
|
|
|
|
|
|
|
|
cef_accelerated_paint_info_t paint_info;
|
|
|
|
paint_info.plane_count = native_pixmap.planes.size();
|
|
|
|
paint_info.modifier = native_pixmap.modifier;
|
|
|
|
paint_info.format = pixel_format;
|
|
|
|
|
|
|
|
auto cef_plain_index = 0;
|
|
|
|
for (const auto& plane : native_pixmap.planes) {
|
|
|
|
cef_accelerated_paint_native_pixmap_plane_t cef_plane;
|
|
|
|
cef_plane.stride = plane.stride;
|
|
|
|
cef_plane.offset = plane.offset;
|
|
|
|
cef_plane.size = plane.size;
|
|
|
|
cef_plane.fd = plane.fd.get();
|
|
|
|
paint_info.planes[cef_plain_index++] = cef_plane;
|
|
|
|
}
|
|
|
|
view_->OnAcceleratedPaint(content_rect, info->coded_size, paint_info);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-05 18:14:58 +01:00
|
|
|
if (info->pixel_format != media::PIXEL_FORMAT_ARGB) {
|
|
|
|
DLOG(ERROR) << "Unsupported pixel format " << info->pixel_format;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-16 23:35:54 +01:00
|
|
|
CHECK(data->is_read_only_shmem_region());
|
|
|
|
base::ReadOnlySharedMemoryRegion& shmem_region =
|
|
|
|
data->get_read_only_shmem_region();
|
|
|
|
|
|
|
|
// The |data| parameter is not nullable and mojo type mapping for
|
|
|
|
// `base::ReadOnlySharedMemoryRegion` defines that nullable version of it is
|
|
|
|
// the same type, with null check being equivalent to IsValid() check. Given
|
|
|
|
// the above, we should never be able to receive a read only shmem region that
|
|
|
|
// is not valid - mojo will enforce it for us.
|
|
|
|
DCHECK(shmem_region.IsValid());
|
2020-01-31 23:54:26 +01:00
|
|
|
|
2021-12-16 23:35:54 +01:00
|
|
|
base::ReadOnlySharedMemoryMapping mapping = shmem_region.Map();
|
2019-07-16 22:09:04 +02:00
|
|
|
if (!mapping.IsValid()) {
|
|
|
|
DLOG(ERROR) << "Shared memory mapping failed.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mapping.size() <
|
|
|
|
media::VideoFrame::AllocationSize(info->pixel_format, info->coded_size)) {
|
|
|
|
DLOG(ERROR) << "Shared memory size was less than expected.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The SkBitmap's pixels will be marked as immutable, but the installPixels()
|
|
|
|
// API requires a non-const pointer. So, cast away the const.
|
|
|
|
void* const pixels = const_cast<void*>(mapping.memory());
|
|
|
|
|
2020-07-08 19:23:29 +02:00
|
|
|
media::VideoFrameMetadata metadata = info->metadata;
|
2019-07-16 22:09:04 +02:00
|
|
|
gfx::Rect damage_rect;
|
|
|
|
|
2020-01-31 23:54:26 +01:00
|
|
|
if (bounds_in_pixels_) {
|
|
|
|
// Use the bounds passed to RequestRefreshFrame().
|
|
|
|
damage_rect = gfx::Rect(info->coded_size);
|
|
|
|
damage_rect.Intersect(*bounds_in_pixels_);
|
2024-03-19 22:11:42 +01:00
|
|
|
bounds_in_pixels_ = std::nullopt;
|
2020-01-31 23:54:26 +01:00
|
|
|
} else {
|
|
|
|
// Retrieve the rectangular region of the frame that has changed since the
|
|
|
|
// frame with the directly preceding CAPTURE_COUNTER. If that frame was not
|
|
|
|
// received, typically because it was dropped during transport from the
|
|
|
|
// producer, clients must assume that the entire frame has changed.
|
|
|
|
// This rectangle is relative to the full frame data, i.e. [0, 0,
|
|
|
|
// coded_size.width(), coded_size.height()]. It does not have to be
|
|
|
|
// fully contained within visible_rect.
|
2020-07-08 19:23:29 +02:00
|
|
|
if (metadata.capture_update_rect) {
|
|
|
|
damage_rect = *metadata.capture_update_rect;
|
|
|
|
}
|
|
|
|
if (damage_rect.IsEmpty()) {
|
2020-01-31 23:54:26 +01:00
|
|
|
damage_rect = gfx::Rect(info->coded_size);
|
|
|
|
}
|
2019-07-16 22:09:04 +02:00
|
|
|
}
|
|
|
|
|
2019-11-26 19:29:27 +01:00
|
|
|
view_->OnPaint(damage_rect, info->coded_size, pixels);
|
2019-07-16 22:09:04 +02:00
|
|
|
}
|