From fa5b1be6f3d6631c0404097b69ea81b4a1eab7f7 Mon Sep 17 00:00:00 2001 From: Athul Iddya Date: Wed, 26 Jul 2023 01:40:19 -0700 Subject: [PATCH] fix: delete desktop capturers when they're not needed (#39194) * fix: delete desktop capturers when they're not needed Delete desktop capturer objects by resetting the DesktopMediaList objects that own them after the sources have been collected. Capturers that are not delegated are already being reset via a patch on NativeDesktopMediaList. That is not safe for delegated capturers as thumbnail generation depends on user events. Deleting the DesktopMediaList operation is safe for all capturers and releases OS capture resources as soon as possible. * fix: add a patch to clean up PipeWire resources Adding a patch to workaround a Chromium issue: https://bugs.chromium.org/p/chromium/issues/detail?id=1467060 The patch can be removed when the issue is resolved. --- patches/webrtc/.patches | 1 + ...er_as_failed_after_session_is_closed.patch | 26 +++++++++++++++++++ .../api/electron_api_desktop_capturer.cc | 6 +++++ 3 files changed, 33 insertions(+) create mode 100644 patches/webrtc/fix_mark_pipewire_capturer_as_failed_after_session_is_closed.patch diff --git a/patches/webrtc/.patches b/patches/webrtc/.patches index 495c944656..0cf4d364bb 100644 --- a/patches/webrtc/.patches +++ b/patches/webrtc/.patches @@ -1 +1,2 @@ fix_fallback_to_x11_capturer_on_wayland.patch +fix_mark_pipewire_capturer_as_failed_after_session_is_closed.patch diff --git a/patches/webrtc/fix_mark_pipewire_capturer_as_failed_after_session_is_closed.patch b/patches/webrtc/fix_mark_pipewire_capturer_as_failed_after_session_is_closed.patch new file mode 100644 index 0000000000..3032706205 --- /dev/null +++ b/patches/webrtc/fix_mark_pipewire_capturer_as_failed_after_session_is_closed.patch @@ -0,0 +1,26 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Athul Iddya +Date: Sat, 22 Jul 2023 11:11:01 -0700 +Subject: fix: mark PipeWire capturer as failed after session is closed + +After a PipeWire screencast session is successfully started, the +consumer is expected to keep calling CaptureFrame() from the +DesktopCapturer interface in a loop to pull frames. A PipeWire +screencast session can be closed by the server on user action. Once the +session is closed, there's no point in calling CaptureFrame() again as +the capture has to be restarted. Inform the caller of the failure by +returning Result::ERROR_PERMANENT on the next invocation of +CaptureFrame(). + +diff --git a/modules/desktop_capture/linux/wayland/base_capturer_pipewire.cc b/modules/desktop_capture/linux/wayland/base_capturer_pipewire.cc +index 4ef00e68ab58333648b8acaf4256f4b57a42734d..5a67c18c1d1f62aa5e3162d9778ca665bac4a1bb 100644 +--- a/modules/desktop_capture/linux/wayland/base_capturer_pipewire.cc ++++ b/modules/desktop_capture/linux/wayland/base_capturer_pipewire.cc +@@ -109,6 +109,7 @@ void BaseCapturerPipeWire::OnScreenCastRequestResult(RequestResponse result, + void BaseCapturerPipeWire::OnScreenCastSessionClosed() { + if (!capturer_failed_) { + options_.screencast_stream()->StopScreenCastStream(); ++ capturer_failed_ = true; + } + } + diff --git a/shell/browser/api/electron_api_desktop_capturer.cc b/shell/browser/api/electron_api_desktop_capturer.cc index 5cfe0fedb9..74530402e9 100644 --- a/shell/browser/api/electron_api_desktop_capturer.cc +++ b/shell/browser/api/electron_api_desktop_capturer.cc @@ -399,6 +399,9 @@ void DesktopCapturer::UpdateSourcesList(DesktopMediaList* list) { v8::HandleScope scope(isolate); gin_helper::CallMethod(this, "_onfinished", captured_sources_); + screen_capturer_.reset(); + window_capturer_.reset(); + Unpin(); } } @@ -408,6 +411,9 @@ void DesktopCapturer::HandleFailure() { v8::HandleScope scope(isolate); gin_helper::CallMethod(this, "_onerror", "Failed to get sources."); + screen_capturer_.reset(); + window_capturer_.reset(); + Unpin(); }