frame_subscriber.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/api/frame_subscriber.h"
  5. #include <utility>
  6. #include "content/public/browser/render_view_host.h"
  7. #include "content/public/browser/render_widget_host.h"
  8. #include "content/public/browser/render_widget_host_view.h"
  9. #include "media/capture/mojom/video_capture_buffer.mojom.h"
  10. #include "media/capture/mojom/video_capture_types.mojom.h"
  11. #include "mojo/public/cpp/bindings/remote.h"
  12. #include "services/viz/privileged/mojom/compositing/frame_sink_video_capture.mojom-shared.h"
  13. #include "ui/gfx/geometry/size_conversions.h"
  14. #include "ui/gfx/image/image.h"
  15. #include "ui/gfx/skbitmap_operations.h"
  16. namespace electron::api {
  17. constexpr static int kMaxFrameRate = 30;
  18. FrameSubscriber::FrameSubscriber(content::WebContents* web_contents,
  19. const FrameCaptureCallback& callback,
  20. bool only_dirty)
  21. : content::WebContentsObserver(web_contents),
  22. callback_(callback),
  23. only_dirty_(only_dirty) {
  24. AttachToHost(web_contents->GetPrimaryMainFrame()->GetRenderWidgetHost());
  25. }
  26. FrameSubscriber::~FrameSubscriber() = default;
  27. void FrameSubscriber::AttachToHost(content::RenderWidgetHost* host) {
  28. host_ = host;
  29. // The view can be null if the renderer process has crashed.
  30. // (https://crbug.com/847363)
  31. if (!host_->GetView())
  32. return;
  33. // Create and configure the video capturer.
  34. gfx::Size size = GetRenderViewSize();
  35. video_capturer_ = host_->GetView()->CreateVideoCapturer();
  36. video_capturer_->SetResolutionConstraints(size, size, true);
  37. video_capturer_->SetAutoThrottlingEnabled(false);
  38. video_capturer_->SetMinSizeChangePeriod(base::TimeDelta());
  39. video_capturer_->SetFormat(media::PIXEL_FORMAT_ARGB);
  40. video_capturer_->SetMinCapturePeriod(base::Seconds(1) / kMaxFrameRate);
  41. video_capturer_->Start(this, viz::mojom::BufferFormatPreference::kDefault);
  42. }
  43. void FrameSubscriber::DetachFromHost() {
  44. if (!host_)
  45. return;
  46. video_capturer_.reset();
  47. host_ = nullptr;
  48. }
  49. void FrameSubscriber::RenderFrameCreated(
  50. content::RenderFrameHost* render_frame_host) {
  51. if (!host_)
  52. AttachToHost(render_frame_host->GetRenderWidgetHost());
  53. }
  54. void FrameSubscriber::RenderViewDeleted(content::RenderViewHost* host) {
  55. if (host->GetWidget() == host_) {
  56. DetachFromHost();
  57. }
  58. }
  59. void FrameSubscriber::PrimaryPageChanged(content::Page& page) {
  60. if (auto* host = page.GetMainDocument().GetMainFrame()->GetRenderWidgetHost();
  61. host_ != host) {
  62. DetachFromHost();
  63. AttachToHost(host);
  64. }
  65. }
  66. void FrameSubscriber::OnFrameCaptured(
  67. ::media::mojom::VideoBufferHandlePtr data,
  68. ::media::mojom::VideoFrameInfoPtr info,
  69. const gfx::Rect& content_rect,
  70. mojo::PendingRemote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
  71. callbacks) {
  72. auto& data_region = data->get_read_only_shmem_region();
  73. gfx::Size size = GetRenderViewSize();
  74. if (size != content_rect.size()) {
  75. video_capturer_->SetResolutionConstraints(size, size, true);
  76. video_capturer_->RequestRefreshFrame();
  77. return;
  78. }
  79. mojo::Remote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks>
  80. callbacks_remote(std::move(callbacks));
  81. if (!data_region.IsValid()) {
  82. callbacks_remote->Done();
  83. return;
  84. }
  85. base::ReadOnlySharedMemoryMapping mapping = data_region.Map();
  86. if (!mapping.IsValid()) {
  87. DLOG(ERROR) << "Shared memory mapping failed.";
  88. return;
  89. }
  90. if (mapping.size() <
  91. media::VideoFrame::AllocationSize(info->pixel_format, info->coded_size)) {
  92. DLOG(ERROR) << "Shared memory size was less than expected.";
  93. return;
  94. }
  95. // The SkBitmap's pixels will be marked as immutable, but the installPixels()
  96. // API requires a non-const pointer. So, cast away the const.
  97. void* const pixels = const_cast<void*>(mapping.memory());
  98. // Call installPixels() with a |releaseProc| that: 1) notifies the capturer
  99. // that this consumer has finished with the frame, and 2) releases the shared
  100. // memory mapping.
  101. struct FramePinner {
  102. // Keeps the shared memory that backs |frame_| mapped.
  103. base::ReadOnlySharedMemoryMapping mapping;
  104. // Prevents FrameSinkVideoCapturer from recycling the shared memory that
  105. // backs |frame_|.
  106. mojo::Remote<viz::mojom::FrameSinkVideoConsumerFrameCallbacks> releaser;
  107. };
  108. SkBitmap bitmap;
  109. bitmap.installPixels(
  110. SkImageInfo::MakeN32(content_rect.width(), content_rect.height(),
  111. kPremul_SkAlphaType),
  112. pixels,
  113. media::VideoFrame::RowBytes(media::VideoFrame::kARGBPlane,
  114. info->pixel_format, info->coded_size.width()),
  115. [](void* addr, void* context) {
  116. delete static_cast<FramePinner*>(context);
  117. },
  118. new FramePinner{std::move(mapping), std::move(callbacks_remote)});
  119. bitmap.setImmutable();
  120. Done(content_rect, bitmap);
  121. }
  122. void FrameSubscriber::OnNewCropVersion(uint32_t crop_version) {}
  123. void FrameSubscriber::OnFrameWithEmptyRegionCapture() {}
  124. void FrameSubscriber::OnStopped() {}
  125. void FrameSubscriber::OnLog(const std::string& message) {}
  126. void FrameSubscriber::Done(const gfx::Rect& damage, const SkBitmap& frame) {
  127. if (frame.drawsNothing())
  128. return;
  129. const SkBitmap& bitmap = only_dirty_ ? SkBitmapOperations::CreateTiledBitmap(
  130. frame, damage.x(), damage.y(),
  131. damage.width(), damage.height())
  132. : frame;
  133. // Copying SkBitmap does not copy the internal pixels, we have to manually
  134. // allocate and write pixels otherwise crash may happen when the original
  135. // frame is modified.
  136. SkBitmap copy;
  137. copy.allocPixels(SkImageInfo::Make(bitmap.width(), bitmap.height(),
  138. kN32_SkColorType, kPremul_SkAlphaType));
  139. SkPixmap pixmap;
  140. bool success = bitmap.peekPixels(&pixmap) && copy.writePixels(pixmap, 0, 0);
  141. CHECK(success);
  142. callback_.Run(gfx::Image::CreateFrom1xBitmap(copy), damage);
  143. }
  144. gfx::Size FrameSubscriber::GetRenderViewSize() const {
  145. content::RenderWidgetHostView* view = host_->GetView();
  146. gfx::Size size = view->GetViewBounds().size();
  147. return gfx::ToRoundedSize(
  148. gfx::ScaleSize(gfx::SizeF(size), view->GetDeviceScaleFactor()));
  149. }
  150. } // namespace electron::api