Make screen sharing on wayland (sway) work!

Note: xdg-desktop-portal-wlr is on arch’s community repository.

From i3 to Sway

my new desktop Reddit post on /r/unixporn

Few months ago I’ve updated my linux stack and decided to switch to a wayland-based infrastructure (see also: Wayland vs Xorg). I use i3 for some years now, the natural way further is Sway. I had to find alternatives to my previous tools, here’s a list:

X11 Wayland Description
Rofi Wofi App launcher
notify-osd mako Notify server
arandr wdisplays Monitor manager
xsel, xclip wl-clipboard-x11 Clipboard manager
scrot grim slurp Screenshot manager
i3bar waybar Bars

You can find all the packages I use daily in my dotfiles repository. Here are some of my favourite:

These are my only graphical interfaced applications, for the rest I’m living in tmux itself running on alacritty. Note that if you install gvim instead of vim, you’ll get that famous +clipboard in vim! About everything else is handled by my dotfiles, the most recent change was to use bat as pager in man files.

Desktop sharing

In France, we’re now at the 36th day of the stay-at-home directive drived by the gouvernment. In that era, communication over the Internet is more important then ever. We need to use tools like Zoom, Google Meet or less corporate-owned alternatives like Jitsi. I’m looking for a more decentralized alternative if you know one! By the way, Framasoft may help working on live streaming with peertube (source).

The week before the confinment, I had to give an online course about reactive programming applied to Angular and redux. They chose to use a self-hosted jitsi instance and asked me to do a few tests. I noticed that I couldn’t share my screen, indeed I switched on wayland where our browsers use X-based windows to make screen sharing works. Thanks to chrome being able to share a chrome tab, I was able to make something work by sharing my tmux session in a web page. For the first session I used gotty, but damn that didn’t worked so well with my vim inside tmux. I had many complains that it was laggy / not readable (I type fast). I also had to make sure I focused the right tab. For the next session, I used tmate. This was better as it works seamlessly with tmux, and I can share a hosted URL so that people can open my tmux in their own browser.

Still, none of these solutions were as good as full screen sharing but I was in a hurry and didn’t got the time to investigate. About a month after, I’ve done my research and guess what it works:

screen sharing sway

Many thanks to emersion for the xdg-desktop-portal-wlr (he works on so many wayland-related tools, sponsored by sourcehut.org a set of web tools, worth the check if you don’t know what it is) and danshick he made the PR that makes things work!

How does this work?

Now because wayland is so different then X (see Wayland vs Xorg) many things change. GNOME is one of the most popular desktop environment to switch to Wayland. For desktop audio and video management, they decided to introduce a new multimedia framework called PipeWire (links to the archlinux documentation, the website is pipewire.org). This framework differs in using a PolKit-like security model asking Wayland for permission to record screen or audio instead of relying on user groups (audio and video).

Pipewire will become the new way to communicate with the desktop applications, at least on linux systems. In Chromium, you can enable it using the enable-webrtc-pipewire-capturer flag (in chrome://flags). On firefox today you need this patch, available as fedora-firefox-wayland-bin in the AUR.

Now this pipewire needs some additional packages to support pulseaudio (pipewire-pulse) or say wlroots-based window manager like sway. For this to work, you need “portals” using xdg-desktop-portal with a “backend”, which is one of:

For sway, we’ll use the later. Let’s install everything from the AUR, note that for this process I use aurman which isn’t publicly maintained, if you don’t really know what you do in archlinux use yay.

Note: xdg-desktop-portal-wlr is on arch’s community repository which was not the case at the time of writing.

aurman -S pipewire-git xdg-desktop-portal-git xdg-desktop-portal-wlr-git

I forked all these and builded them manually, indeed xdg-desktop-portal-wlr wasn’t aliased to xdg-desktop-portal-impl and I had a conflict with the gtk backend. I think that this is fixed now.

From there, let’s run all these (following these debugging instructions):

tmux xdg

Now you can test these via this webrtc test page. On Chromium it works very well. On Firefox it needs the patch discussed earlier.

I hope that this can help you setup screen sharing on your Wayland-based window manager.