Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

browser(firefox): exclude frame from screencast video (#3372)

Yury Semikhatsky538daf33028a
diff --git a/browser_patches/firefox/BUILD_NUMBER b/browser_patches/firefox/BUILD_NUMBER
index a644ec8dc2..80a313e6ac 100644
--- a/browser_patches/firefox/BUILD_NUMBER
+++ b/browser_patches/firefox/BUILD_NUMBER
@@ -1,2 +1,2 @@
-1155
-Changed: [email protected] Fri Aug 7 15:25:51 PDT 2020
+1156
+Changed: [email protected] Mon Aug 10 20:28:44 GMTST 2020
diff --git a/browser_patches/firefox/juggler/screencast/ScreencastEncoder.cpp b/browser_patches/firefox/juggler/screencast/ScreencastEncoder.cpp
index cf6fca0f72..9989484824 100644
--- a/browser_patches/firefox/juggler/screencast/ScreencastEncoder.cpp
+++ b/browser_patches/firefox/juggler/screencast/ScreencastEncoder.cpp
@@ -105,10 +105,10 @@ void createImage(unsigned int width, unsigned int height,
class ScreencastEncoder::VPXFrame {
public:
- VPXFrame(rtc::scoped_refptr<webrtc::VideoFrameBuffer>&& buffer, Maybe<double> scale, int offsetTop)
+ VPXFrame(rtc::scoped_refptr<webrtc::VideoFrameBuffer>&& buffer, Maybe<double> scale, const gfx::IntMargin& margin)
: m_frameBuffer(std::move(buffer))
, m_scale(scale)
- , m_offsetTop(offsetTop)
+ , m_margin(margin)
{ }
void setDuration(int duration) { m_duration = duration; }
@@ -131,45 +131,45 @@ public:
uint8_t* v_data = image->planes[2];
if (m_scale) {
- int src_width = src->width();
+ int src_width = src->width() - m_margin.LeftRight();
double dst_width = src_width * m_scale.value();
if (dst_width > image->w) {
src_width *= image->w / dst_width;
dst_width = image->w;
}
- int src_height = src->height() - m_offsetTop;
+ int src_height = src->height() - m_margin.TopBottom();
double dst_height = src_height * m_scale.value();
if (dst_height > image->h) {
src_height *= image->h / dst_height;
dst_height = image->h;
}
- libyuv::I420Scale(src->DataY() + m_offsetTop * src->StrideY(), src->StrideY(),
- src->DataU() + (m_offsetTop + 1) / 2 * src->StrideU(), src->StrideU(),
- src->DataV() + (m_offsetTop + 1) / 2 * src->StrideV(), src->StrideV(),
- src_width, src_height,
- y_data, y_stride,
- u_data, uv_stride,
- v_data, uv_stride,
- dst_width, dst_height,
- libyuv::kFilterBilinear);
+ libyuv::I420Scale(src->DataY() + m_margin.top * src->StrideY() + m_margin.left, src->StrideY(),
+ src->DataU() + (m_margin.top * src->StrideU() + m_margin.left) / 2, src->StrideU(),
+ src->DataV() + (m_margin.top * src->StrideV() + m_margin.left) / 2, src->StrideV(),
+ src_width, src_height,
+ y_data, y_stride,
+ u_data, uv_stride,
+ v_data, uv_stride,
+ dst_width, dst_height,
+ libyuv::kFilterBilinear);
} else {
- int width = std::min<int>(image->w, src->width());
- int height = std::min<int>(image->h, src->height() - m_offsetTop);
-
- libyuv::I420Copy(src->DataY() + m_offsetTop * src->StrideY(), src->StrideY(),
- src->DataU() + (m_offsetTop + 1) / 2 * src->StrideU(), src->StrideU(),
- src->DataV() + (m_offsetTop + 1) / 2 * src->StrideV(), src->StrideV(),
- y_data, y_stride,
- u_data, uv_stride,
- v_data, uv_stride,
- width, height);
+ int width = std::min<int>(image->w, src->width() - m_margin.LeftRight());
+ int height = std::min<int>(image->h, src->height() - m_margin.TopBottom());
+
+ libyuv::I420Copy(src->DataY() + m_margin.top * src->StrideY() + m_margin.left, src->StrideY(),
+ src->DataU() + (m_margin.top * src->StrideU() + m_margin.left) / 2, src->StrideU(),
+ src->DataV() + (m_margin.top * src->StrideV() + m_margin.left) / 2, src->StrideV(),
+ y_data, y_stride,
+ u_data, uv_stride,
+ v_data, uv_stride,
+ width, height);
}
}
private:
rtc::scoped_refptr<webrtc::VideoFrameBuffer> m_frameBuffer;
Maybe<double> m_scale;
- int m_offsetTop = 0;
+ gfx::IntMargin m_margin;
int m_duration = 0;
};
@@ -264,10 +264,10 @@ private:
std::unique_ptr<vpx_image_t> m_image;
};
-ScreencastEncoder::ScreencastEncoder(std::unique_ptr<VPXCodec>&& vpxCodec, Maybe<double> scale, int offsetTop)
+ScreencastEncoder::ScreencastEncoder(std::unique_ptr<VPXCodec>&& vpxCodec, Maybe<double> scale, const gfx::IntMargin& margin)
: m_vpxCodec(std::move(vpxCodec))
, m_scale(scale)
- , m_offsetTop(offsetTop)
+ , m_margin(margin)
{
}
@@ -277,7 +277,7 @@ ScreencastEncoder::~ScreencastEncoder()
static constexpr int fps = 24;
-RefPtr<ScreencastEncoder> ScreencastEncoder::create(nsCString& errorString, const nsCString& filePath, int width, int height, Maybe<double> scale, int offsetTop)
+RefPtr<ScreencastEncoder> ScreencastEncoder::create(nsCString& errorString, const nsCString& filePath, int width, int height, Maybe<double> scale, const gfx::IntMargin& margin)
{
vpx_codec_iface_t* codec_interface = vpx_codec_vp8_cx();
if (!codec_interface) {
@@ -318,7 +318,7 @@ RefPtr<ScreencastEncoder> ScreencastEncoder::create(nsCString& errorString, cons
std::unique_ptr<VPXCodec> vpxCodec(new VPXCodec(codec, cfg, file));
fprintf(stderr, "ScreencastEncoder initialized with: %s\n", vpx_codec_iface_name(codec_interface));
- return new ScreencastEncoder(std::move(vpxCodec), scale, offsetTop);
+ return new ScreencastEncoder(std::move(vpxCodec), scale, margin);
}
void ScreencastEncoder::flushLastFrame()
@@ -342,7 +342,7 @@ void ScreencastEncoder::encodeFrame(const webrtc::VideoFrame& videoFrame)
fprintf(stderr, "ScreencastEncoder::encodeFrame\n");
flushLastFrame();
- m_lastFrame = std::make_unique<VPXFrame>(videoFrame.video_frame_buffer(), m_scale, m_offsetTop);
+ m_lastFrame = std::make_unique<VPXFrame>(videoFrame.video_frame_buffer(), m_scale, m_margin);
}
void ScreencastEncoder::finish(std::function<void()>&& callback)
diff --git a/browser_patches/firefox/juggler/screencast/ScreencastEncoder.h b/browser_patches/firefox/juggler/screencast/ScreencastEncoder.h
index 80208f0697..afdd1b284d 100644
--- a/browser_patches/firefox/juggler/screencast/ScreencastEncoder.h
+++ b/browser_patches/firefox/juggler/screencast/ScreencastEncoder.h
@@ -6,6 +6,7 @@
#include <functional>
#include <memory>
+#include "mozilla/gfx/Rect.h"
#include "mozilla/Maybe.h"
#include "mozilla/TimeStamp.h"
#include "nsISupportsImpl.h"
@@ -21,10 +22,10 @@ class ScreencastEncoder {
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ScreencastEncoder)
public:
- static RefPtr<ScreencastEncoder> create(nsCString& errorString, const nsCString& filePath, int width, int height, Maybe<double> scale, int offsetTop);
+ static RefPtr<ScreencastEncoder> create(nsCString& errorString, const nsCString& filePath, int width, int height, Maybe<double> scale, const gfx::IntMargin& margin);
class VPXCodec;
- ScreencastEncoder(std::unique_ptr<VPXCodec>&&, Maybe<double> scale, int offsetTop);
+ ScreencastEncoder(std::unique_ptr<VPXCodec>&&, Maybe<double> scale, const gfx::IntMargin& margin);
void encodeFrame(const webrtc::VideoFrame& videoFrame);
@@ -37,7 +38,7 @@ private:
std::unique_ptr<VPXCodec> m_vpxCodec;
Maybe<double> m_scale;
- int m_offsetTop;
+ gfx::IntMargin m_margin;
TimeStamp m_lastFrameTimestamp;
class VPXFrame;
std::unique_ptr<VPXFrame> m_lastFrame;
diff --git a/browser_patches/firefox/juggler/screencast/nsScreencastService.cpp b/browser_patches/firefox/juggler/screencast/nsScreencastService.cpp
index c30aa4c7e4..595a7cef5e 100644
--- a/browser_patches/firefox/juggler/screencast/nsScreencastService.cpp
+++ b/browser_patches/firefox/juggler/screencast/nsScreencastService.cpp
@@ -147,7 +147,15 @@ nsresult nsScreencastService::StartVideoRecording(nsIDocShell* aDocShell, const
Maybe<double> maybeScale;
if (scale)
maybeScale = Some(scale);
- RefPtr<ScreencastEncoder> encoder = ScreencastEncoder::create(error, PromiseFlatCString(aFileName), width, height, maybeScale, offsetTop);
+
+ auto bounds = widget->GetScreenBounds().ToUnknownRect();
+ auto clientBounds = widget->GetClientBounds().ToUnknownRect();
+ // Crop the image to exclude frame (if any).
+ gfx::IntMargin margin = bounds - clientBounds;
+ // Crop the image to exclude controls.
+ margin.top += offsetTop;
+
+ RefPtr<ScreencastEncoder> encoder = ScreencastEncoder::create(error, PromiseFlatCString(aFileName), width, height, maybeScale, margin);
if (!encoder) {
fprintf(stderr, "Failed to create ScreencastEncoder: %s\n", error.get());
return NS_ERROR_FAILURE;