Repository: electron/electron (language: cpp)

Issue:
[Bug]: DesktopCapturer doesn't work in Ubuntu 22.04.2 LTS
### Preflight Checklist

- [X] I have read the [Contributing Guidelines](https://github.com/electron/electron/blob/main/CONTRIBUTING.md) for this project.
- [X] I agree to follow the [Code of Conduct](https://github.com/electron/electron/blob/main/CODE_OF_CONDUCT.md) that this project adheres to.
- [X] I have searched the [issue tracker](https://www.github.com/electron/electron/issues) for a bug report that matches the one I want to file, without success.

### Electron Version

23.1.1

### What operating system are you using?

Ubuntu

### Operating System Version

22.04.2

### What arch are you using?

x64

### Last Known Working Electron version

_No response_

### Expected Behavior

Get access to screens for capture recording using desktopCapturer in electron Api

### Actual Behavior

There are difficulties to report the problem because no specific error is received in the terminal, only the message below

```bash
tiago@tghpereira:~/Área de Trabalho/electron/test$ yarn start
yarn run v1.22.19
$ electron .
/home/tiago/Área de Trabalho/electron/test/node_modules/electron/dist/electron exited with signal SIGSEGV
error Command failed with exit code 1.
```

I followed the official Electronjs documentation step by step and tried several approaches mentioned in forums to find ways to solve the problem, but without success.
Please help us with this issue which is greatly affecting many packages.

### Testcase Gist URL

https://gist.github.com/68498fa7437a78b9daf435f3413c9a43

### Additional Information

_No response_


Discussion / hints:
Hello @tghpereira. Thanks for reporting this and helping to make Electron better!

Would it be possible for you to make a standalone testcase with only the code necessary to reproduce the issue? For example, [Electron Fiddle](https://www.electronjs.org/fiddle) is a great tool for making small test cases and makes it easy to publish your test case to a [gist](https://gist.github.com) that Electron maintainers can use.

Stand-alone test cases make fixing issues go more smoothly: it ensure everyone's looking at the same issue, it removes all unnecessary variables from the equation, and it can also provide the basis for automated regression tests.

Now adding the `blocked/need-repro` label for this reason. After you make a test case, please link to it in a followup comment. This issue will be closed in 10 days if the above is not addressed.
@tghpereira I have the same problem.  try installing a previous version of electron. 
hello @vlkalashnikov, thanks for the suggestion but I tried all the possibilities none worked, I left a test code created through Electron Fiddle, when running the project it just returned the message "Electron exited with signal SIGSEGV." and then the process shuts down.

@VerteDinde follow the gist link as requested

https://gist.github.com/68498fa7437a78b9daf435f3413c9a43
@tghpereira Thanks for the gist, very appreciated! 

I think I have a lead on the issue - if you switch to Xorg, does the crash still occur? I can reproduce this on Ubuntu 20.04 when using Wayland, 20.04 using Xorg works as expected, so I suspect the issue is Wayland-specific. Will try to find a fix.

Bisecting, it looks like this issue started in 23.0.0-nightly20221118 (23.0.0-nightly20221117 works as expected). I'm guessing this Chrome roll caused the regression: https://github.com/electron/electron/pull/36186/files
@VerteDinde  We in the community thank you for the attention of the Electron team.

I made 2 attempts as suggested, one in which I just deactivated Wayland and the other using Xorg, both still persist with the error with the same characteristics mentioned above

Fix patch:
diff --git a/patches/config.json b/patches/config.json
index bac8c8b9a4586..703821a3e6dcd 100644
--- a/patches/config.json
+++ b/patches/config.json
@@ -21,5 +21,7 @@
 
   "src/electron/patches/Mantle": "src/third_party/squirrel.mac/vendor/Mantle",
 
-  "src/electron/patches/ReactiveObjC": "src/third_party/squirrel.mac/vendor/ReactiveObjC"
+  "src/electron/patches/ReactiveObjC": "src/third_party/squirrel.mac/vendor/ReactiveObjC",
+
+  "src/electron/patches/webrtc": "src/third_party/webrtc"
 }
diff --git a/patches/webrtc/.patches b/patches/webrtc/.patches
index e69de29bb2d1d..495c94465691c 100644
--- a/patches/webrtc/.patches
+++ b/patches/webrtc/.patches
@@ -0,0 +1,1 @@
+fix_fallback_to_x11_capturer_on_wayland.patch
diff --git a/patches/webrtc/fix_fallback_to_x11_capturer_on_wayland.patch b/patches/webrtc/fix_fallback_to_x11_capturer_on_wayland.patch
new file mode 100644
index 0000000000000..7db9b2393ad0a
--- /dev/null
+++ b/patches/webrtc/fix_fallback_to_x11_capturer_on_wayland.patch
@@ -0,0 +1,58 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: VerteDinde <keeleymhammond@gmail.com>
+Date: Sun, 5 Mar 2023 21:04:37 -0800
+Subject: fix: fallback to X11 capturer on Wayland
+
+CL: https://webrtc-review.googlesource.com/c/src/+/279163
+
+Desktop Capturer behaves inconsistently on Wayland. PipeWire does not
+always successfully start; if it does not, we return a nullptr rather
+than falling back on the X11 capturer, crashing the application.
+
+If the X11 capturer is enabled, we should at minimum try to fallback
+to X11 for desktop capturer. This patch re-enables that fallback,
+which was previously default behavior.
+
+This patch can be removed when 1) this fix is upstreamed, or 2) the
+stability of PipeWire initialization is improved upstream.
+
+Patch_Filename: fix_fallback_to_x11_desktop_capturer_wayland.patch
+
+diff --git a/modules/desktop_capture/screen_capturer_linux.cc b/modules/desktop_capture/screen_capturer_linux.cc
+index 44993837e8bbd84a11ec9d187349a95f83258910..cd9f8b0be6a19d057fe9150382fb72bc4032b343 100644
+--- a/modules/desktop_capture/screen_capturer_linux.cc
++++ b/modules/desktop_capture/screen_capturer_linux.cc
+@@ -34,11 +34,10 @@ std::unique_ptr<DesktopCapturer> DesktopCapturer::CreateRawScreenCapturer(
+ #endif  // defined(WEBRTC_USE_PIPEWIRE)
+ 
+ #if defined(WEBRTC_USE_X11)
+-  if (!DesktopCapturer::IsRunningUnderWayland())
+-    return ScreenCapturerX11::CreateRawScreenCapturer(options);
+-#endif  // defined(WEBRTC_USE_X11)
+-
++  return ScreenCapturerX11::CreateRawScreenCapturer(options);
++#else
+   return nullptr;
++#endif  // defined(WEBRTC_USE_X11)
+ }
+ 
+ }  // namespace webrtc
+diff --git a/modules/desktop_capture/window_capturer_linux.cc b/modules/desktop_capture/window_capturer_linux.cc
+index 4205bf9bc0eede48cdc39353c77ceb6e7529fd51..785dc01a1911fd027401b1461223668333e05558 100644
+--- a/modules/desktop_capture/window_capturer_linux.cc
++++ b/modules/desktop_capture/window_capturer_linux.cc
+@@ -34,11 +34,10 @@ std::unique_ptr<DesktopCapturer> DesktopCapturer::CreateRawWindowCapturer(
+ #endif  // defined(WEBRTC_USE_PIPEWIRE)
+ 
+ #if defined(WEBRTC_USE_X11)
+-  if (!DesktopCapturer::IsRunningUnderWayland())
+-    return WindowCapturerX11::CreateRawWindowCapturer(options);
+-#endif  // defined(WEBRTC_USE_X11)
+-
++  return WindowCapturerX11::CreateRawWindowCapturer(options);
++#else
+   return nullptr;
++#endif  // defined(WEBRTC_USE_X11)
+ }
+ 
+ }  // namespace webrtc
diff --git a/shell/browser/api/electron_api_desktop_capturer.cc b/shell/browser/api/electron_api_desktop_capturer.cc
index 8af09cdfe9c9f..0ea7096150742 100644
--- a/shell/browser/api/electron_api_desktop_capturer.cc
+++ b/shell/browser/api/electron_api_desktop_capturer.cc
@@ -207,27 +207,31 @@ void DesktopCapturer::StartHandling(bool capture_window,
     // Initialize the source list.
     // Apply the new thumbnail size and restart capture.
     if (capture_window) {
-      window_capturer_ = std::make_unique<NativeDesktopMediaList>(
-          DesktopMediaList::Type::kWindow,
-          content::desktop_capture::CreateWindowCapturer());
-      window_capturer_->SetThumbnailSize(thumbnail_size);
-      window_capturer_->Update(
-          base::BindOnce(&DesktopCapturer::UpdateSourcesList,
-                         weak_ptr_factory_.GetWeakPtr(),
-                         window_capturer_.get()),
-          /* refresh_thumbnails = */ true);
+      if (auto capturer = content::desktop_capture::CreateWindowCapturer();
+          capturer) {
+        window_capturer_ = std::make_unique<NativeDesktopMediaList>(
+            DesktopMediaList::Type::kWindow, std::move(capturer));
+        window_capturer_->SetThumbnailSize(thumbnail_size);
+        window_capturer_->Update(
+            base::BindOnce(&DesktopCapturer::UpdateSourcesList,
+                           weak_ptr_factory_.GetWeakPtr(),
+                           window_capturer_.get()),
+            /* refresh_thumbnails = */ true);
+      }
     }
 
     if (capture_screen) {
-      screen_capturer_ = std::make_unique<NativeDesktopMediaList>(
-          DesktopMediaList::Type::kScreen,
-          content::desktop_capture::CreateScreenCapturer());
-      screen_capturer_->SetThumbnailSize(thumbnail_size);
-      screen_capturer_->Update(
-          base::BindOnce(&DesktopCapturer::UpdateSourcesList,
-                         weak_ptr_factory_.GetWeakPtr(),
-                         screen_capturer_.get()),
-          /* refresh_thumbnails = */ true);
+      if (auto capturer = content::desktop_capture::CreateScreenCapturer();
+          capturer) {
+        screen_capturer_ = std::make_unique<NativeDesktopMediaList>(
+            DesktopMediaList::Type::kScreen, std::move(capturer));
+        screen_capturer_->SetThumbnailSize(thumbnail_size);
+        screen_capturer_->Update(
+            base::BindOnce(&DesktopCapturer::UpdateSourcesList,
+                           weak_ptr_factory_.GetWeakPtr(),
+                           screen_capturer_.get()),
+            /* refresh_thumbnails = */ true);
+      }
     }
   }
 }


Test patch: