Skip to content

Instantly share code, notes, and snippets.

@progandy
Last active September 26, 2023 08:27
Show Gist options
  • Save progandy/bff675311aa2c3b777a37abe81aa4b4d to your computer and use it in GitHub Desktop.
Save progandy/bff675311aa2c3b777a37abe81aa4b4d to your computer and use it in GitHub Desktop.
Sway output mirror and screencast

Sway output mirror and screencast

Mirror

wf-recorder

With wf-recorder it is possible to record one output. When ffmpeg is compiled with sdl support, then it is possible to use "sdl" as the muxer and replay the recorded video instead of writing it to a file.

SDL_VIDEODRIVER=wayland wf-recorder -c rawvideo -m sdl -f pipe:wayland-mirror

As of version 0.2.1, closing the window will not stop wf-recorder. This has been fixed in the git version with PR#94`

To terminate wf-recorder when the window is closed, you can use the following script. It requires jq in addition to swaymsg and wf-recorder.

#!/bin/bash
# sway-mirror OPTIONS
#
# Examples:
#
# SDL_VIDEODRIVER=wayland sway-mirror
#
# SDL_VIDEODRIVER=x11 sway-mirror
#
# sway-mirror --output LVDS-1
#
instance=sway-mirror$$
wf-recorder "$@" -c rawvideo -m sdl -f pipe:$instance &
sleep 1
if kill -0 $! 2>/dev/null && swaymsg "[title=\"^pipe:$instance\$\"] nop" >/dev/null; then
	while read type ; read name ; do 
		if [[ $type == '"close"' && $name == "\"pipe:$instance\"" ]]; then
			break
		fi
	done < <(swaymsg -mt subscribe '["window"]' | jq --unbuffered .change,.container.name)
	#while jobs '%1' >/dev/null && swaymsg "[title=\"^pipe:wf-mirror$$\$\"] nop" >/dev/null; do
	#	sleep 1
	#done
fi
kill -INT '%1'
exit 0

wdomirror

wdomirror is an experimental program to mirror an output with as little overhead as possible using the dmabuf and viewporter protocols.

Screencast

Share a v4l2 camera

For applications that support sharing a v4l2 camera, it is possible to set up a virtual camera with wf-recorder and v4l2loopback. An alternative to v4l2loopback is akvcam.

Share an X11 window

For application that can share X11 windows, it is possible to mirror an output to a X11 window with wf-recorder just as described in the mirror section. Creating an X11 window instead of a wayland window is done by setting the environment variable SDL_VIDEODRIVER=x11

SDL_VIDEODRIVER=x11 wf-recorder -c rawvideo -m sdl -f pipe:xwayland-mirror

This window can be moved to a virtual output created with swaymsg create_output (as of sway 1.5)

Share with xdg-desktop-portal

For applications that support screencasting with xdg-desktop-portal, it should be possible to use xdg-desktop-portal-wlr WebRTC support in Firefox and Chromium falls in this category, but Firefox has to be patched (version 84 might not need patches anymore)

Gnome Screenshot DBus API

A few applications like Zoom can take screenshots every second using the Gnome Screenshot API, that can be replicated with gnome-dbus-emulation-wlr

OBS

For OBS there is the wlrobs plugin. A plugin for xdg-desktop-portal exists as well, but wlrobs is preferred.

VNC

An output can also be shared or mirrored by starting a vnc server with wayvnc and connecting to it with a vnc client. This allows the remote client to interact with the output, i.e. send keyboard and mouse events.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment