browser(webkit): do not leak vpx codecs (#16032)

This commit is contained in:
Yury Semikhatsky 2022-07-28 13:16:20 -07:00 коммит произвёл GitHub
Родитель 3d89506704
Коммит 4b7da07b0f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 26 добавлений и 14 удалений

Просмотреть файл

@ -1,2 +1,2 @@
1691
Changed: yurys@chromium.org Tue 26 Jul 2022 12:38:57 PM PDT
1692
Changed: yurys@chromium.org Thu 28 Jul 2022 01:15:17 PM PDT

Просмотреть файл

@ -13168,10 +13168,10 @@ index 0000000000000000000000000000000000000000..d28dde452275f18739e3b1c8d709185c
+} // namespace WebKit
diff --git a/Source/WebKit/UIProcess/Inspector/Agents/ScreencastEncoder.cpp b/Source/WebKit/UIProcess/Inspector/Agents/ScreencastEncoder.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..489fb9a4a98de71526a6c3d6746e8b7192bac9b8
index 0000000000000000000000000000000000000000..5f82314ec035268b1a90568935d427e0909eae05
--- /dev/null
+++ b/Source/WebKit/UIProcess/Inspector/Agents/ScreencastEncoder.cpp
@@ -0,0 +1,379 @@
@@ -0,0 +1,391 @@
+/*
+ * Copyright (c) 2010, The WebM Project authors. All rights reserved.
+ * Copyright (c) 2013 The Chromium Authors. All rights reserved.
@ -13223,6 +13223,18 @@ index 0000000000000000000000000000000000000000..489fb9a4a98de71526a6c3d6746e8b71
+
+namespace {
+
+struct VpxCodecDeleter {
+ void operator()(vpx_codec_ctx_t* codec) {
+ if (codec) {
+ vpx_codec_err_t ret = vpx_codec_destroy(codec);
+ if (ret != VPX_CODEC_OK)
+ fprintf(stderr, "Failed to encode frame: %s\n", vpx_codec_error(codec));
+ }
+ }
+};
+
+using ScopedVpxCodec = std::unique_ptr<vpx_codec_ctx_t, VpxCodecDeleter>;
+
+// Number of timebase unints per one frame.
+constexpr int timeScale = 1000;
+
@ -13343,9 +13355,9 @@ index 0000000000000000000000000000000000000000..489fb9a4a98de71526a6c3d6746e8b71
+
+class ScreencastEncoder::VPXCodec {
+public:
+ VPXCodec(vpx_codec_ctx_t codec, vpx_codec_enc_cfg_t cfg, FILE* file)
+ VPXCodec(ScopedVpxCodec codec, vpx_codec_enc_cfg_t cfg, FILE* file)
+ : m_encoderQueue(WorkQueue::create("Screencast encoder"))
+ , m_codec(codec)
+ , m_codec(WTFMove(codec))
+ , m_cfg(cfg)
+ , m_file(file)
+ , m_writer(new WebMFileWriter(file, &m_cfg))
@ -13381,14 +13393,14 @@ index 0000000000000000000000000000000000000000..489fb9a4a98de71526a6c3d6746e8b71
+ vpx_codec_iter_t iter = nullptr;
+ const vpx_codec_cx_pkt_t *pkt = nullptr;
+ int flags = 0;
+ const vpx_codec_err_t res = vpx_codec_encode(&m_codec, img, m_pts, duration, flags, VPX_DL_REALTIME);
+ const vpx_codec_err_t res = vpx_codec_encode(m_codec.get(), img, m_pts, duration, flags, VPX_DL_REALTIME);
+ if (res != VPX_CODEC_OK) {
+ fprintf(stderr, "Failed to encode frame: %s\n", vpx_codec_error(&m_codec));
+ fprintf(stderr, "Failed to encode frame: %s\n", vpx_codec_error(m_codec.get()));
+ return false;
+ }
+
+ bool gotPkts = false;
+ while ((pkt = vpx_codec_get_cx_data(&m_codec, &iter)) != nullptr) {
+ while ((pkt = vpx_codec_get_cx_data(m_codec.get(), &iter)) != nullptr) {
+ gotPkts = true;
+
+ if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
@ -13415,7 +13427,7 @@ index 0000000000000000000000000000000000000000..489fb9a4a98de71526a6c3d6746e8b71
+ }
+
+ Ref<WorkQueue> m_encoderQueue;
+ vpx_codec_ctx_t m_codec;
+ ScopedVpxCodec m_codec;
+ vpx_codec_enc_cfg_t m_cfg;
+ FILE* m_file { nullptr };
+ std::unique_ptr<WebMFileWriter> m_writer;
@ -13463,9 +13475,9 @@ index 0000000000000000000000000000000000000000..489fb9a4a98de71526a6c3d6746e8b71
+ cfg.g_timebase.den = fps * timeScale;
+ cfg.g_error_resilient = VPX_ERROR_RESILIENT_DEFAULT;
+
+ vpx_codec_ctx_t codec;
+ if (vpx_codec_enc_init(&codec, codec_interface, &cfg, 0)) {
+ errorString = makeString("Failed to initialize encoder: "_s, vpx_codec_error(&codec));
+ ScopedVpxCodec codec(new vpx_codec_ctx_t);
+ if (vpx_codec_enc_init(codec.get(), codec_interface, &cfg, 0)) {
+ errorString = makeString("Failed to initialize encoder: "_s, vpx_codec_error(codec.get()));
+ return nullptr;
+ }
+
@ -13475,7 +13487,7 @@ index 0000000000000000000000000000000000000000..489fb9a4a98de71526a6c3d6746e8b71
+ return nullptr;
+ }
+
+ std::unique_ptr<VPXCodec> vpxCodec(new VPXCodec(codec, cfg, file));
+ std::unique_ptr<VPXCodec> vpxCodec(new VPXCodec(WTFMove(codec), cfg, file));
+ return adoptRef(new ScreencastEncoder(WTFMove(vpxCodec), size));
+}
+