electron/shell/browser/osr/osr_video_consumer.cc
electron-roller[bot] d32e6cc252
chore: bump chromium to 105.0.5173.0 (main) (#34770)
* chore: bump chromium in DEPS to 105.0.5147.0

* chore: update chromium/can_create_window.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3642216

fix minor code shear in patch

* chore: update chromium/port_autofill_colors_to_the_color_pipeline.patch

Xref: chromium/port_autofill_colors_to_the_color_pipeline.patch

fix minor code shear in patch

* chore: remove chromium/posix_replace_doubleforkandexec_with_forkandspawn.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3727368

Removing upstreamed patch

* chore: update patches

* chore: update patches

* chore: add new enum kOffscreenDocument to switch statement

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3722498

* chore: add stub for new parent virtual method OnNewCropVersion()

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3717305

* chore: remove download_schedule arg from DownloadTargetCallback invocation

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3711096

Clean up DownloadLater flags and prefs

* chore: add stub for new parent virtual method OnNewCropVersion()

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3717305

* chore: use base::List for extensions::Event

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3718366

Remove Event constructor overloads that take vector<base::Value>

* refactor: replace ClearStorageDataOptions.origin with .storage_key

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3702946

Refactor ClearData to take StorageKey

* chore: bump chromium in DEPS to 105.0.5149.0

* chore: update patches

* refactor: migrate InspectableWebContents to base::Value::List

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3726326

Migrate DevToolsEmbedderMessageDispatcher to base::Value::List.

* refactor: update electron_api_clipboard_mac Clipboard::ReadFindText()

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3721398

Mac: Remove unused FindPasteboard C++ interface

* chore: bump chromium in DEPS to 105.0.5151.0

* chore: fix code shear in chromium/build_do_not_depend_on_packed_resource_integrity.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3714995

Move Cart DB proto to //components/commerce

* Revert "chore: fix code shear in chromium/build_do_not_depend_on_packed_resource_integrity.patch"

This reverts commit f8de4605eb3d35152b186646fefd8e88d1df836b.

* chore: fix code shear in chromium/build_do_not_depend_on_packed_resource_integrity.patch

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3714995

Move Cart DB proto to //components/commerce

* chore: update patches

* chore: update ElectronAccessibilityUIMessageHandler to use base::Value::List

Xref: https://chromium-review.googlesource.com/c/chromium/src/+/3733367

Convert /chrome/browser/accessibility away from WebUI::RegisterDeprecatedMessageCallback

* chore: bump chromium in DEPS to 105.0.5153.0

* chore: bump chromium in DEPS to 105.0.5155.0

* chore: bump chromium in DEPS to 105.0.5157.0

* chore: bump chromium in DEPS to 105.0.5159.0

* chore: update patches

* chore: update CL reference

* Replace ContentMainDelegate::InvokedIn with a variant

https://chromium-review.googlesource.com/c/chromium/src/+/3705957

* Switch devtools_frontend.mojom to mojom.DictValue.

https://chromium-review.googlesource.com/c/chromium/src/+/3726425

* webhid: Bind HID service with service workers

https://chromium-review.googlesource.com/c/chromium/src/+/3680562

* chore: fix lint

* Don't fire load-complete AX notification on macOS for unfocused windows

https://chromium-review.googlesource.com/c/chromium/src/+/3695403

* chore: update patches

* chore: bump chromium in DEPS to 105.0.5165.0

* chore: update patches

* chore: bump chromium in DEPS to 105.0.5167.0

* chore: bump chromium in DEPS to 105.0.5169.0

* chore: bump chromium in DEPS to 105.0.5171.0

* chore: update patches

* chore: bump chromium in DEPS to 105.0.5173.0

* chore: update patches

* 3743346: Stop generating unsupported policies on macOS

https://chromium-review.googlesource.com/c/chromium/src/+/3743346

* 3727612: [Bluetooth][Win] Refactor pairing prompt code

https://chromium-review.googlesource.com/c/chromium/src/+/3727612

* 3737325: system-extensions: Move IsEnabled() out of SystemExtensionsProvider.

https://chromium-review.googlesource.com/c/chromium/src/+/3737325

* 3748635: Remove RenderView from the public API.

https://chromium-review.googlesource.com/c/chromium/src/+/3748635

* fixup: 3743346: Stop generating unsupported policies on macOS

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Co-authored-by: deepak1556 <hop2deep@gmail.com>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
2022-07-13 17:26:16 -04:00

162 lines
5.4 KiB
C++

// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/osr/osr_video_consumer.h"
#include <utility>
#include "media/base/limits.h"
#include "media/base/video_frame_metadata.h"
#include "media/capture/mojom/video_capture_buffer.mojom.h"
#include "media/capture/mojom/video_capture_types.mojom.h"
#include "services/viz/privileged/mojom/compositing/frame_sink_video_capture.mojom-shared.h"
#include "shell/browser/osr/osr_render_widget_host_view.h"
#include "ui/gfx/skbitmap_operations.h"
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
namespace electron {
OffScreenVideoConsumer::OffScreenVideoConsumer(
OffScreenRenderWidgetHostView* view,
OnPaintCallback callback)
: callback_(callback),
view_(view),
video_capturer_(view->CreateVideoCapturer()) {
video_capturer_->SetAutoThrottlingEnabled(false);
video_capturer_->SetMinSizeChangePeriod(base::TimeDelta());
video_capturer_->SetFormat(media::PIXEL_FORMAT_ARGB);
SizeChanged(view_->SizeInPixels());
SetFrameRate(view_->GetFrameRate());
}
OffScreenVideoConsumer::~OffScreenVideoConsumer() = default;
void OffScreenVideoConsumer::SetActive(bool active) {
if (active) {
video_capturer_->Start(this, viz::mojom::BufferFormatPreference::kDefault);
} else {
video_capturer_->Stop();
}
}
void OffScreenVideoConsumer::SetFrameRate(int frame_rate) {
video_capturer_->SetMinCapturePeriod(base::Seconds(1) / frame_rate);
}
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);
video_capturer_->RequestRefreshFrame();
}
void OffScreenVideoConsumer::OnFrameCaptured(
::media::mojom::VideoBufferHandlePtr data,
::media::mojom::VideoFrameInfoPtr info,
const gfx::Rect& content_rect,
mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
callbacks) {
auto& data_region = data->get_read_only_shmem_region();
if (!CheckContentRect(content_rect)) {
SizeChanged(view_->SizeInPixels());
return;
}
mojo::Remote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
callbacks_remote(std::move(callbacks));
if (!data_region.IsValid()) {
callbacks_remote->Done();
return;
}
base::ReadOnlySharedMemoryMapping mapping = data_region.Map();
if (!mapping.IsValid()) {
DLOG(ERROR) << "Shared memory mapping failed.";
callbacks_remote->Done();
return;
}
if (mapping.size() <
media::VideoFrame::AllocationSize(info->pixel_format, info->coded_size)) {
DLOG(ERROR) << "Shared memory size was less than expected.";
callbacks_remote->Done();
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_|.
mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
releaser;
};
SkBitmap bitmap;
bitmap.installPixels(
SkImageInfo::MakeN32(content_rect.width(), content_rect.height(),
kPremul_SkAlphaType),
pixels,
media::VideoFrame::RowBytes(media::VideoFrame::kARGBPlane,
info->pixel_format, info->coded_size.width()),
[](void* addr, void* context) {
delete static_cast<FramePinner*>(context);
},
new FramePinner{std::move(mapping), callbacks_remote.Unbind()});
bitmap.setImmutable();
absl::optional<gfx::Rect> update_rect = info->metadata.capture_update_rect;
if (!update_rect.has_value() || update_rect->IsEmpty()) {
update_rect = content_rect;
}
callback_.Run(*update_rect, bitmap);
}
void OffScreenVideoConsumer::OnNewCropVersion(uint32_t crop_version) {}
void OffScreenVideoConsumer::OnFrameWithEmptyRegionCapture() {}
void OffScreenVideoConsumer::OnStopped() {}
void OffScreenVideoConsumer::OnLog(const std::string& message) {}
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;
}
} // namespace electron