2019-04-17 21:10:04 +00:00
|
|
|
// Copyright (c) 2019 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/osr/osr_video_consumer.h"
|
2019-04-17 21:10:04 +00:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
2022-05-05 13:53:39 +00:00
|
|
|
#include "media/base/limits.h"
|
2019-04-17 21:10:04 +00:00
|
|
|
#include "media/base/video_frame_metadata.h"
|
2021-06-22 19:17:16 +00:00
|
|
|
#include "media/capture/mojom/video_capture_buffer.mojom.h"
|
2019-04-17 21:10:04 +00:00
|
|
|
#include "media/capture/mojom/video_capture_types.mojom.h"
|
2022-01-10 22:31:39 +00:00
|
|
|
#include "services/viz/privileged/mojom/compositing/frame_sink_video_capture.mojom-shared.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/osr/osr_render_widget_host_view.h"
|
2024-07-25 09:25:45 +00:00
|
|
|
#include "third_party/skia/include/core/SkImageInfo.h"
|
|
|
|
#include "third_party/skia/include/core/SkRegion.h"
|
2019-04-17 21:10:04 +00:00
|
|
|
#include "ui/gfx/skbitmap_operations.h"
|
|
|
|
|
2022-05-05 13:53:39 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool IsValidMinAndMaxFrameSize(gfx::Size min_frame_size,
|
|
|
|
gfx::Size max_frame_size) {
|
|
|
|
// Returns true if
|
|
|
|
// 0 < |min_frame_size| <= |max_frame_size| <= media::limits::kMaxDimension.
|
|
|
|
return 0 < min_frame_size.width() && 0 < min_frame_size.height() &&
|
|
|
|
min_frame_size.width() <= max_frame_size.width() &&
|
|
|
|
min_frame_size.height() <= max_frame_size.height() &&
|
|
|
|
max_frame_size.width() <= media::limits::kMaxDimension &&
|
|
|
|
max_frame_size.height() <= media::limits::kMaxDimension;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2019-04-17 21:10:04 +00:00
|
|
|
|
|
|
|
OffScreenVideoConsumer::OffScreenVideoConsumer(
|
|
|
|
OffScreenRenderWidgetHostView* view,
|
|
|
|
OnPaintCallback callback)
|
|
|
|
: callback_(callback),
|
|
|
|
view_(view),
|
2021-01-26 18:16:21 +00:00
|
|
|
video_capturer_(view->CreateVideoCapturer()) {
|
2019-04-17 21:10:04 +00:00
|
|
|
video_capturer_->SetAutoThrottlingEnabled(false);
|
|
|
|
video_capturer_->SetMinSizeChangePeriod(base::TimeDelta());
|
2022-01-10 22:31:39 +00:00
|
|
|
video_capturer_->SetFormat(media::PIXEL_FORMAT_ARGB);
|
2022-05-05 13:53:39 +00:00
|
|
|
|
|
|
|
SizeChanged(view_->SizeInPixels());
|
2024-01-30 02:43:28 +00:00
|
|
|
SetFrameRate(view_->frame_rate());
|
2019-04-17 21:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OffScreenVideoConsumer::~OffScreenVideoConsumer() = default;
|
|
|
|
|
|
|
|
void OffScreenVideoConsumer::SetActive(bool active) {
|
|
|
|
if (active) {
|
2022-01-10 22:31:39 +00:00
|
|
|
video_capturer_->Start(this, viz::mojom::BufferFormatPreference::kDefault);
|
2019-04-17 21:10:04 +00:00
|
|
|
} else {
|
|
|
|
video_capturer_->Stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OffScreenVideoConsumer::SetFrameRate(int frame_rate) {
|
2021-11-24 08:45:59 +00:00
|
|
|
video_capturer_->SetMinCapturePeriod(base::Seconds(1) / frame_rate);
|
2019-04-17 21:10:04 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 13:53:39 +00:00
|
|
|
void OffScreenVideoConsumer::SizeChanged(const gfx::Size& size_in_pixels) {
|
|
|
|
DCHECK(IsValidMinAndMaxFrameSize(size_in_pixels, size_in_pixels));
|
|
|
|
video_capturer_->SetResolutionConstraints(size_in_pixels, size_in_pixels,
|
|
|
|
true);
|
2019-04-17 21:10:04 +00:00
|
|
|
video_capturer_->RequestRefreshFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OffScreenVideoConsumer::OnFrameCaptured(
|
2022-01-10 22:31:39 +00:00
|
|
|
::media::mojom::VideoBufferHandlePtr data,
|
2019-04-17 21:10:04 +00:00
|
|
|
::media::mojom::VideoFrameInfoPtr info,
|
|
|
|
const gfx::Rect& content_rect,
|
2020-02-27 19:00:07 +00:00
|
|
|
mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
|
|
|
|
callbacks) {
|
2022-01-10 22:31:39 +00:00
|
|
|
auto& data_region = data->get_read_only_shmem_region();
|
|
|
|
|
2019-04-17 21:10:04 +00:00
|
|
|
if (!CheckContentRect(content_rect)) {
|
2022-05-05 13:53:39 +00:00
|
|
|
SizeChanged(view_->SizeInPixels());
|
2019-04-17 21:10:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-27 19:00:07 +00:00
|
|
|
mojo::Remote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
|
|
|
|
callbacks_remote(std::move(callbacks));
|
|
|
|
|
2022-01-10 22:31:39 +00:00
|
|
|
if (!data_region.IsValid()) {
|
2020-02-27 19:00:07 +00:00
|
|
|
callbacks_remote->Done();
|
2019-04-17 21:10:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-01-10 22:31:39 +00:00
|
|
|
base::ReadOnlySharedMemoryMapping mapping = data_region.Map();
|
2019-04-17 21:10:04 +00:00
|
|
|
if (!mapping.IsValid()) {
|
|
|
|
DLOG(ERROR) << "Shared memory mapping failed.";
|
2020-02-27 19:00:07 +00:00
|
|
|
callbacks_remote->Done();
|
2019-04-17 21:10:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mapping.size() <
|
|
|
|
media::VideoFrame::AllocationSize(info->pixel_format, info->coded_size)) {
|
|
|
|
DLOG(ERROR) << "Shared memory size was less than expected.";
|
2020-02-27 19:00:07 +00:00
|
|
|
callbacks_remote->Done();
|
2019-04-17 21:10:04 +00:00
|
|
|
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());
|
|
|
|
|
|
|
|
// Call installPixels() with a |releaseProc| that: 1) notifies the capturer
|
|
|
|
// that this consumer has finished with the frame, and 2) releases the shared
|
|
|
|
// memory mapping.
|
|
|
|
struct FramePinner {
|
|
|
|
// Keeps the shared memory that backs |frame_| mapped.
|
|
|
|
base::ReadOnlySharedMemoryMapping mapping;
|
|
|
|
// Prevents FrameSinkVideoCapturer from recycling the shared memory that
|
|
|
|
// backs |frame_|.
|
2020-02-27 19:00:07 +00:00
|
|
|
mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
|
|
|
|
releaser;
|
2019-04-17 21:10:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SkBitmap bitmap;
|
|
|
|
bitmap.installPixels(
|
|
|
|
SkImageInfo::MakeN32(content_rect.width(), content_rect.height(),
|
|
|
|
kPremul_SkAlphaType),
|
|
|
|
pixels,
|
2024-06-07 21:18:35 +00:00
|
|
|
media::VideoFrame::RowBytes(media::VideoFrame::Plane::kARGB,
|
2019-04-17 21:10:04 +00:00
|
|
|
info->pixel_format, info->coded_size.width()),
|
|
|
|
[](void* addr, void* context) {
|
|
|
|
delete static_cast<FramePinner*>(context);
|
|
|
|
},
|
2020-02-27 19:00:07 +00:00
|
|
|
new FramePinner{std::move(mapping), callbacks_remote.Unbind()});
|
2019-04-17 21:10:04 +00:00
|
|
|
bitmap.setImmutable();
|
|
|
|
|
2024-01-10 22:23:35 +00:00
|
|
|
std::optional<gfx::Rect> update_rect = info->metadata.capture_update_rect;
|
2020-06-22 17:35:10 +00:00
|
|
|
if (!update_rect.has_value() || update_rect->IsEmpty()) {
|
|
|
|
update_rect = content_rect;
|
2019-04-17 21:10:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-22 17:35:10 +00:00
|
|
|
callback_.Run(*update_rect, bitmap);
|
2019-04-17 21:10:04 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 14:02:12 +00:00
|
|
|
void OffScreenVideoConsumer::OnNewSubCaptureTargetVersion(
|
|
|
|
uint32_t crop_version) {}
|
2022-07-13 21:26:16 +00:00
|
|
|
|
2022-02-25 18:17:35 +00:00
|
|
|
void OffScreenVideoConsumer::OnFrameWithEmptyRegionCapture() {}
|
|
|
|
|
2019-04-17 21:10:04 +00:00
|
|
|
void OffScreenVideoConsumer::OnStopped() {}
|
|
|
|
|
2020-04-13 23:39:26 +00:00
|
|
|
void OffScreenVideoConsumer::OnLog(const std::string& message) {}
|
|
|
|
|
2019-04-17 21:10:04 +00:00
|
|
|
bool OffScreenVideoConsumer::CheckContentRect(const gfx::Rect& content_rect) {
|
|
|
|
gfx::Size view_size = view_->SizeInPixels();
|
|
|
|
gfx::Size content_size = content_rect.size();
|
|
|
|
|
|
|
|
if (std::abs(view_size.width() - content_size.width()) > 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::abs(view_size.height() - content_size.height()) > 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|