This project implements a high-performance, ultra-low latency virtual display server for Linux environments running Wayland. By natively integrating with the XDG Desktop Portal via D-Bus and negotiating raw memory streams through PipeWire, the software circumvents the strict security boundaries of modern Wayland compositor architectures (e.g., Mutter) to provision headless virtual monitors entirely in user-space. The resulting architecture streams real-time Variable Bitrate (VBR) MJPEG over a custom-built, multithreaded TCP HTTP server.
Handling raw video buffers at 60 Frames Per Second (FPS) in user-space introduces critical memory bandwidth constraints. Let
For a standard 1080p stream at 60 FPS:
Processing
For libjpeg-turbo. This brings the algorithmic compression complexity down to
The system is decomposed into three highly concurrent subsystems: D-Bus Orchestration, PipeWire Media Graph, and TCP/HTTP Stream Synchronization.
graph TD
classDef compositor fill:#1e1e1e,stroke:#333,stroke-width:2px,color:#fff;
classDef daemon fill:#003366,stroke:#0055a4,stroke-width:2px,color:#fff;
classDef network fill:#005500,stroke:#00aa00,stroke-width:2px,color:#fff;
A[Mutter Compositor]:::compositor -->|Creates Headless Display| B(XDG Desktop Portal)
B:::daemon -.->|D-Bus Session Negotiation| C[wayland_capture.c]
A -->|Routes Video Buffers| D{PipeWire Server}:::daemon
C -->|FD Handoff| E[pipewire_capture.c]
D ==>|Zero-Copy DMA/MemFD| E
subgraph "Process Memory Space"
E -->|BGRA Format| F[SIMD TurboJPEG Compressor]
F -->|JPEG Byte Array| G((Shared Thread-Safe Frame Buffer))
G -->|Condition Variable & Mutex| H[mjpeg_stream.c TCP Socket]
end
H ==>|HTTP multipart/x-mixed-replace| I(Client Browser):::network
A naïve implementation of memory sharing between the GPU capturing thread and the TCP networking thread leads to race conditions, partial reads (screen tearing), and Thread Starvation.
To resolve this, the architecture employs POSIX Threads (pthreads) utilizing a Mutex-locked Deep Copy strategy:
-
State Mutation: The PipeWire thread acquires the Mutex lock:
pthread_mutex_lock(). - Buffer Swap: The compressed JPEG is written to the global heap, updating the active pointer.
-
Thread Waking:
pthread_cond_broadcast()is invoked, elevating the sleeping HTTP thread to an active execution state without busy-waiting (maintaining 0% CPU consumption while the screen is idle). -
Deep Copy Protocol: Upon awakening, the HTTP thread performs an atomic
memcpy()of the JPEG buffer into a localized networking heap, immediately releasing the Mutex in$O(1)$ time complexity ($t_{copy} < 100 \mu s$ ). -
Asynchronous I/O: The TCP socket invokes
send()iteratively with theMSG_NOSIGNALflag, effectively saturating the network interface queue without blocking the internal PipeWire DMA pipeline.
By manipulating the SelectSources schema of the org.freedesktop.portal.ScreenCast interface, the boolean mask for SourceType is shifted to 4 (Virtual Monitor). This implicitly forces the upstream Wayland compositor to provision a secondary frame buffer. In conjunction, the cursor_mode = 2 bitmask is requested, instructing the compositor to hardware-embed the pointer into the contiguous stream buffer, deprecating the need for client-side JavaScript coordinate emulation.
Video transport is conducted over a standardized HTTP/1.1 response implementing the multipart/x-mixed-replace specification. This topology forces standard WebKit/Blink rendering engines to overwrite the existing DOM image asset continually, establishing an ultra-low latency unidirectional socket without the initialization overhead of a WebRTC stack.
The build system relies on standard POSIX headers and explicit GNOME/FreeDesktop user-space libraries.
sudo pacman -S pipewire glib2 libjpeg-turbo gcc make pkgconfmake clean && make./second_screenUpon execution, the D-Bus abstraction layer will prompt a Wayland security dialog requesting authorization to instantiate and expose the virtual display. Once authenticated, the secondary stream is accessible via any web browser routing to http://localhost:8080/.
Provided "as-is" for educational and high-performance computing research within the Linux Display Server ecosystem. Dependencies (libpipewire, glib, libjpeg-turbo) remain subjects of their original licensing distributions.