2015-09-18 07:57:43 +00: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.
|
|
|
|
|
|
|
|
#include "atom/browser/api/frame_subscriber.h"
|
|
|
|
|
2016-06-21 00:15:39 +00:00
|
|
|
#include "atom/common/native_mate_converters/gfx_converter.h"
|
2018-04-18 00:45:15 +00:00
|
|
|
#include "components/viz/host/host_frame_sink_manager.h"
|
|
|
|
#include "content/browser/compositor/surface_utils.h"
|
|
|
|
#include "content/browser/renderer_host/render_widget_host_view_base.h"
|
2016-02-25 22:48:58 +00:00
|
|
|
|
2016-09-06 08:24:37 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
|
2015-09-18 07:57:43 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
FrameSubscriber::FrameSubscriber(v8::Isolate* isolate,
|
2016-02-18 00:15:00 +00:00
|
|
|
content::RenderWidgetHostView* view,
|
2018-04-18 00:45:15 +00:00
|
|
|
const FrameCaptureCallback& callback)
|
2016-06-26 02:46:40 +00:00
|
|
|
: isolate_(isolate),
|
|
|
|
view_(view),
|
|
|
|
callback_(callback),
|
2018-04-18 00:45:15 +00:00
|
|
|
video_consumer_binding_(this),
|
|
|
|
weak_factory_(this) {
|
|
|
|
video_capturer_ = CreateVideoCapturer();
|
|
|
|
video_capturer_->SetResolutionConstraints(
|
|
|
|
view_->GetViewBounds().size(),
|
|
|
|
view_->GetViewBounds().size(), true);
|
|
|
|
video_capturer_->SetFormat(media::PIXEL_FORMAT_ARGB,
|
|
|
|
media::COLOR_SPACE_UNSPECIFIED);
|
|
|
|
|
|
|
|
viz::mojom::FrameSinkVideoConsumerPtr consumer;
|
|
|
|
video_consumer_binding_.Bind(mojo::MakeRequest(&consumer));
|
|
|
|
video_capturer_->Start(std::move(consumer));
|
2017-04-13 10:21:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 00:45:15 +00:00
|
|
|
FrameSubscriber::~FrameSubscriber() = default;
|
2015-09-18 07:57:43 +00:00
|
|
|
|
2018-04-18 00:45:15 +00:00
|
|
|
void FrameSubscriber::OnFrameCaptured(
|
|
|
|
mojo::ScopedSharedBufferHandle buffer,
|
|
|
|
uint32_t buffer_size,
|
|
|
|
::media::mojom::VideoFrameInfoPtr info,
|
|
|
|
const gfx::Rect& update_rect,
|
|
|
|
const gfx::Rect& content_rect,
|
|
|
|
viz::mojom::FrameSinkVideoConsumerFrameCallbacksPtr callbacks) {
|
2015-11-14 22:59:38 +00:00
|
|
|
v8::Locker locker(isolate_);
|
|
|
|
v8::HandleScope handle_scope(isolate_);
|
|
|
|
|
2018-04-18 00:45:15 +00:00
|
|
|
gfx::Size view_size = view_->GetViewBounds().size();
|
|
|
|
if (view_size != content_rect.size()) {
|
|
|
|
video_capturer_->SetResolutionConstraints(view_size, view_size, true);
|
|
|
|
video_capturer_->RequestRefreshFrame();
|
|
|
|
return;
|
|
|
|
}
|
2017-07-28 09:57:41 +00:00
|
|
|
|
2018-04-18 00:45:15 +00:00
|
|
|
if (!buffer.is_valid()) {
|
|
|
|
callbacks->Done();
|
|
|
|
return;
|
|
|
|
}
|
2017-07-28 09:57:41 +00:00
|
|
|
|
2018-04-18 00:45:15 +00:00
|
|
|
mojo::ScopedSharedBufferMapping mapping = buffer->Map(buffer_size);
|
|
|
|
if (!mapping) {
|
2015-09-18 07:57:43 +00:00
|
|
|
return;
|
2018-04-18 00:45:15 +00:00
|
|
|
}
|
2015-09-18 07:57:43 +00:00
|
|
|
|
2018-04-18 00:45:15 +00:00
|
|
|
SkImageInfo image_info = SkImageInfo::MakeN32(
|
|
|
|
content_rect.width(), content_rect.height(), kPremul_SkAlphaType);
|
|
|
|
SkPixmap pixmap(image_info, mapping.get(),
|
|
|
|
media::VideoFrame::RowBytes(media::VideoFrame::kARGBPlane,
|
|
|
|
info->pixel_format,
|
|
|
|
info->coded_size.width()));
|
|
|
|
frame_.installPixels(pixmap);
|
|
|
|
size_t rgb_row_size = frame_.width() * frame_.bytesPerPixel();
|
|
|
|
v8::MaybeLocal<v8::Object> node_buffer =
|
|
|
|
node::Buffer::New(isolate_, rgb_row_size * frame_.height());
|
|
|
|
auto local_buffer = node_buffer.ToLocalChecked();
|
2017-07-28 09:57:41 +00:00
|
|
|
|
|
|
|
{
|
2018-04-18 00:45:15 +00:00
|
|
|
auto* source = static_cast<const unsigned char*>(frame_.getPixels());
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* target = node::Buffer::Data(local_buffer);
|
2017-07-28 09:57:41 +00:00
|
|
|
|
2018-04-18 00:45:15 +00:00
|
|
|
for (int y = 0; y < frame_.height(); ++y) {
|
2017-07-28 09:57:41 +00:00
|
|
|
memcpy(target, source, rgb_row_size);
|
2018-04-18 00:45:15 +00:00
|
|
|
source += frame_.rowBytes();
|
2017-07-28 09:57:41 +00:00
|
|
|
target += rgb_row_size;
|
|
|
|
}
|
|
|
|
}
|
2015-09-18 07:57:43 +00:00
|
|
|
|
2016-06-21 00:15:39 +00:00
|
|
|
v8::Local<v8::Value> damage =
|
2018-04-18 00:45:15 +00:00
|
|
|
mate::Converter<gfx::Rect>::ToV8(isolate_, update_rect);
|
2016-06-21 00:15:39 +00:00
|
|
|
|
2017-07-28 09:57:41 +00:00
|
|
|
callback_.Run(local_buffer, damage);
|
2018-04-18 00:45:15 +00:00
|
|
|
|
|
|
|
shared_memory_mapping_ = std::move(mapping);
|
|
|
|
shared_memory_releaser_ = std::move(callbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrameSubscriber::OnTargetLost(const viz::FrameSinkId& frame_sink_id) {}
|
|
|
|
|
|
|
|
void FrameSubscriber::OnStopped() {}
|
|
|
|
|
|
|
|
viz::mojom::FrameSinkVideoCapturerPtr FrameSubscriber::CreateVideoCapturer() {
|
|
|
|
auto* view_base = static_cast<content::RenderWidgetHostViewBase*>(view_);
|
|
|
|
viz::mojom::FrameSinkVideoCapturerPtr video_capturer;
|
|
|
|
content::GetHostFrameSinkManager()->CreateVideoCapturer(
|
|
|
|
mojo::MakeRequest(&video_capturer));
|
|
|
|
video_capturer->ChangeTarget(view_base->GetFrameSinkId());
|
|
|
|
return video_capturer;
|
2015-09-18 07:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|