ca75bca667
* chore: bump chromium in DEPS to 520c02b46668fc608927e0fcd79b6a90885a48bf * chore: bump chromium in DEPS to 90.0.4414.0 * resolve chromium conflicts * resolve v8 conflicts * fix node gn files * 2673502: Remove RenderViewCreated use from ExtensionHost. https://chromium-review.googlesource.com/c/chromium/src/+/2673502 * 2676903: [mojo] Remove most legacy Binding classes. https://chromium-review.googlesource.com/c/chromium/src/+/2676903 * 2644847: Move self-deleting URLLoaderFactory base into //services/network. https://chromium-review.googlesource.com/c/chromium/src/+/2644847 * 2664006: Remove from mojo::DataPipe. https://chromium-review.googlesource.com/c/chromium/src/+/2664006 * 2674530: Remove CertVerifierService feature https://chromium-review.googlesource.com/c/chromium/src/+/2674530 * 2668748: Move OnSSLCertificateError to a new interface. https://chromium-review.googlesource.com/c/chromium/src/+/2668748 * 2672923: Remove RAPPOR reporting infrastructure. https://chromium-review.googlesource.com/c/chromium/src/+/2672923 * 2673502: Remove RenderViewCreated use from ExtensionHost. https://chromium-review.googlesource.com/c/chromium/src/+/2673502 * 2655126: Convert FrameHostMsg_ContextMenu and FrameMsg_ContextMenuClosed|CustomContextMenuAction to Mojo https://chromium-review.googlesource.com/c/chromium/src/+/2655126 * 2628705: Window Placement: Implement screen.isExtended and change event https://chromium-review.googlesource.com/c/chromium/src/+/2628705 * 2643161: Refactor storage::kFileSystem*Native* https://chromium-review.googlesource.com/c/chromium/src/+/2643161 * fix build * only remove the biggest subdir of //ios * chore: bump chromium in DEPS to 90.0.4415.0 * update patches * update sysroots * 2686147: Remove WebContentsObserver::RenderViewCreated(). https://chromium-review.googlesource.com/c/chromium/src/+/2686147 * 2596429: Fixing how extension's split and spanning modes affect OriginAccessList. https://chromium-review.googlesource.com/c/chromium/src/+/2596429 * 2686026: [mojo] Delete AssociatedInterfacePtr (replaced by AssociatedRemote) https://chromium-review.googlesource.com/c/chromium/src/+/2686026 * 2651705: Move ui/base/dragdrop/file_info to ui/base/clipboard https://chromium-review.googlesource.com/c/chromium/src/+/2651705 * 358217: drawBitmap is deprecated https://skia-review.googlesource.com/c/skia/+/358217 * fix gn check * 2678098: Use gen/front_end as input to generate_devtools_grd https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2678098 * 2674530: Remove CertVerifierService feature https://chromium-review.googlesource.com/c/chromium/src/+/2674530 * fixup 2664006: Remove from mojo::DataPipe. https://chromium-review.googlesource.com/c/chromium/src/+/2664006 * fixup build_add_electron_tracing_category.patch * 2673415: [base] Prepare CrashReporterClient for string16 switch https://chromium-review.googlesource.com/c/chromium/src/+/2673415 * 2673413: Add CursorFactoryWin to handle Cursors on Windows https://chromium-review.googlesource.com/c/chromium/src/+/2673413 * 2668748: Move OnSSLCertificateError to a new interface. https://chromium-review.googlesource.com/c/chromium/src/+/2668748 * fix mas gn check * update patch after merge * Update node for .mjs files * build: load v8_prof_processor dependencies as ESM * chore: add patch to fix linux 32bit Co-authored-by: Jeremy Rose <nornagon@nornagon.net> Co-authored-by: Jeremy Rose <jeremya@chromium.org> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com>
181 lines
6.1 KiB
C++
181 lines
6.1 KiB
C++
// Copyright (c) 2015 GitHub, Inc.
|
|
// Use of this source code is governed by the MIT license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "shell/browser/api/frame_subscriber.h"
|
|
|
|
#include <utility>
|
|
|
|
#include "content/public/browser/render_view_host.h"
|
|
#include "content/public/browser/render_widget_host.h"
|
|
#include "content/public/browser/render_widget_host_view.h"
|
|
#include "media/capture/mojom/video_capture_types.mojom.h"
|
|
#include "mojo/public/cpp/bindings/remote.h"
|
|
#include "ui/gfx/geometry/size_conversions.h"
|
|
#include "ui/gfx/image/image.h"
|
|
#include "ui/gfx/skbitmap_operations.h"
|
|
|
|
namespace electron {
|
|
|
|
namespace api {
|
|
|
|
constexpr static int kMaxFrameRate = 30;
|
|
|
|
FrameSubscriber::FrameSubscriber(content::WebContents* web_contents,
|
|
const FrameCaptureCallback& callback,
|
|
bool only_dirty)
|
|
: content::WebContentsObserver(web_contents),
|
|
callback_(callback),
|
|
only_dirty_(only_dirty) {
|
|
content::RenderViewHost* rvh = web_contents->GetRenderViewHost();
|
|
if (rvh)
|
|
AttachToHost(rvh->GetWidget());
|
|
}
|
|
|
|
FrameSubscriber::~FrameSubscriber() = default;
|
|
|
|
void FrameSubscriber::AttachToHost(content::RenderWidgetHost* host) {
|
|
host_ = host;
|
|
|
|
// The view can be null if the renderer process has crashed.
|
|
// (https://crbug.com/847363)
|
|
if (!host_->GetView())
|
|
return;
|
|
|
|
// Create and configure the video capturer.
|
|
gfx::Size size = GetRenderViewSize();
|
|
video_capturer_ = host_->GetView()->CreateVideoCapturer();
|
|
video_capturer_->SetResolutionConstraints(size, size, true);
|
|
video_capturer_->SetAutoThrottlingEnabled(false);
|
|
video_capturer_->SetMinSizeChangePeriod(base::TimeDelta());
|
|
video_capturer_->SetFormat(media::PIXEL_FORMAT_ARGB,
|
|
gfx::ColorSpace::CreateREC709());
|
|
video_capturer_->SetMinCapturePeriod(base::TimeDelta::FromSeconds(1) /
|
|
kMaxFrameRate);
|
|
video_capturer_->Start(this);
|
|
}
|
|
|
|
void FrameSubscriber::DetachFromHost() {
|
|
if (!host_)
|
|
return;
|
|
video_capturer_.reset();
|
|
host_ = nullptr;
|
|
}
|
|
|
|
void FrameSubscriber::RenderFrameCreated(
|
|
content::RenderFrameHost* render_frame_host) {
|
|
if (!host_)
|
|
AttachToHost(render_frame_host->GetRenderWidgetHost());
|
|
}
|
|
|
|
void FrameSubscriber::RenderViewDeleted(content::RenderViewHost* host) {
|
|
if (host->GetWidget() == host_) {
|
|
DetachFromHost();
|
|
}
|
|
}
|
|
|
|
void FrameSubscriber::RenderViewHostChanged(content::RenderViewHost* old_host,
|
|
content::RenderViewHost* new_host) {
|
|
if ((old_host && old_host->GetWidget() == host_) || (!old_host && !host_)) {
|
|
DetachFromHost();
|
|
AttachToHost(new_host->GetWidget());
|
|
}
|
|
}
|
|
|
|
void FrameSubscriber::OnFrameCaptured(
|
|
base::ReadOnlySharedMemoryRegion data,
|
|
::media::mojom::VideoFrameInfoPtr info,
|
|
const gfx::Rect& content_rect,
|
|
mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
|
|
callbacks) {
|
|
gfx::Size size = GetRenderViewSize();
|
|
if (size != content_rect.size()) {
|
|
video_capturer_->SetResolutionConstraints(size, size, true);
|
|
video_capturer_->RequestRefreshFrame();
|
|
return;
|
|
}
|
|
|
|
mojo::Remote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
|
|
callbacks_remote(std::move(callbacks));
|
|
if (!data.IsValid()) {
|
|
callbacks_remote->Done();
|
|
return;
|
|
}
|
|
base::ReadOnlySharedMemoryMapping mapping = data.Map();
|
|
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());
|
|
|
|
// 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::Remote<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), std::move(callbacks_remote)});
|
|
bitmap.setImmutable();
|
|
|
|
Done(content_rect, bitmap);
|
|
}
|
|
|
|
void FrameSubscriber::OnStopped() {}
|
|
|
|
void FrameSubscriber::OnLog(const std::string& message) {}
|
|
|
|
void FrameSubscriber::Done(const gfx::Rect& damage, const SkBitmap& frame) {
|
|
if (frame.drawsNothing())
|
|
return;
|
|
|
|
const SkBitmap& bitmap = only_dirty_ ? SkBitmapOperations::CreateTiledBitmap(
|
|
frame, damage.x(), damage.y(),
|
|
damage.width(), damage.height())
|
|
: frame;
|
|
|
|
// Copying SkBitmap does not copy the internal pixels, we have to manually
|
|
// allocate and write pixels otherwise crash may happen when the original
|
|
// frame is modified.
|
|
SkBitmap copy;
|
|
copy.allocPixels(SkImageInfo::Make(bitmap.width(), bitmap.height(),
|
|
kN32_SkColorType, kPremul_SkAlphaType));
|
|
SkPixmap pixmap;
|
|
bool success = bitmap.peekPixels(&pixmap) && copy.writePixels(pixmap, 0, 0);
|
|
CHECK(success);
|
|
|
|
callback_.Run(gfx::Image::CreateFrom1xBitmap(copy), damage);
|
|
}
|
|
|
|
gfx::Size FrameSubscriber::GetRenderViewSize() const {
|
|
content::RenderWidgetHostView* view = host_->GetView();
|
|
gfx::Size size = view->GetViewBounds().size();
|
|
return gfx::ToRoundedSize(
|
|
gfx::ScaleSize(gfx::SizeF(size), view->GetDeviceScaleFactor()));
|
|
}
|
|
|
|
} // namespace api
|
|
|
|
} // namespace electron
|