A desktop app, written in Go
Your phone's camera on your PC. And your PC's camera on your phone.
Start the app, pick a camera and scan a QR code with your phone. The two cameras then show on each other’s screen over WebRTC. The app serves the phone’s page itself, so there is no app to install on the phone and no service in between.
# Build it and start it
$ go build -o desktop-mirror ./cmd/desktop-mirror
$ ./desktop-mirror
# 1. Pick a PC camera in the window
# 2. Scan the QR code with your phone, and accept the certificate warning once
# 3. Allow the camera on the phone. Both feeds start
The window has the camera picker, a preview of the PC camera, the QR code, and the feed of the phone beside them.
3 steps
pick a camera, scan the QR code, allow the camera on the phone
0 apps
to install on the phone. Its side is a plain web page
128 bits
of random token in each QR code. It is valid for 10 minutes and for one phone
720p
at about 30 frames a second is what both cameras are asked for
Pair with a QR code
The window shows a QR code with the address of the app’s own page and a one-time token. Scan it, allow the camera, and both feeds start.
It stays on your network
The app serves the page and the signaling itself, over HTTPS on your LAN. Nothing leaves your network unless you configure a STUN server.
No app on the phone
The phone’s side is a web page in plain JavaScript, with no build step. The video is the WebRTC of the phone’s browser.
What the phone opens
The link of the QR code opens one small page. A button starts the camera, because a browser wants a tap before it asks for one. After that the page shows the PC’s camera, with the phone’s own camera in a corner.
- Start camera asks for the permission and starts both feeds
- Flip camera changes between the front and the back camera, without a new pairing
- Stop ends the session
<main id="stage" hidden>
<video id="remote" autoplay playsinline muted></video>
<video id="local" autoplay playsinline muted></video>
<div id="toolbar">
<button id="flip" type="button">Flip camera</button>
<button id="stop" type="button" class="danger">Stop</button>
</div>
</main>
<section id="intro">
<h1>Desktop Mirror</h1>
<p id="message">Share this phone's camera with your computer
and see the computer's camera here.</p>
<button id="start" type="button">Start camera</button>
</section>
How the two sides find each other
There is no account and no server of somebody else. The desktop app is the web server, the signaling server and one of the two WebRTC peers.
Pairing
A token that works once
The QR code holds the address of the page, with a token of 128 random bits in its path. The first phone that connects uses the token up.
- A token is valid for 10 minutes while the app waits
- A second phone is refused while a session is active
- After every session the token changes, and the window shows a new QR code
- The window shows the user agent of the phone that connected, so a pairing that you did not make is visible
https://<ip>:8443/s/<token>/ the page
wss://<ip>:8443/s/<token>/ws the signaling
HTTPS
A certificate that the app makes itself
A mobile browser gives a page the camera only in a secure context, and http://192.168.x.x is not one. So the app makes a self-signed certificate for the LAN addresses of the PC and keeps it in its config directory. The phone shows a certificate warning once, and you accept it.
- The certificate covers every LAN address, so another network interface does not need a new one
- The server listens only on the LAN address that you selected
- A WebSocket whose
Origindoes not match is rejected
# Another HTTPS port than 8443.
# A free port is used if it is taken.
./desktop-mirror -port 9443
# Keep the WebRTC media in a range of UDP ports,
# so that you can write a firewall rule.
./desktop-mirror -udp-min 50000 -udp-max 50100
# Another folder for the certificate.
./desktop-mirror -config-dir ~/mirror-cert
# A STUN server. Without the flag there is none.
./desktop-mirror -stun stun:stun.l.google.com:19302
Video
Both directions over one connection
The desktop app uses pion for WebRTC, and the phone uses its browser. The app makes the offer with one video transceiver that sends and receives, so one negotiation carries both feeds. The codec is VP8, which Chrome, Firefox and Safari support.
- A change of camera on either side swaps the source and keeps the session
- When the phone asks for a keyframe, the encoder makes one
- Frames of the PC camera that are wider than 1280 pixels are made smaller before they are encoded
// Encoder compresses raw frames into one codec access unit per call.
type Encoder interface {
// Encode compresses a frame. The result may be empty if the codec
// buffered the frame. forceKeyframe requests an intra frame.
Encode(ctx context.Context, f I420Frame, forceKeyframe bool) ([]byte, error)
SetBitrate(bps int) error
Close() error
}
// Decoder turns one codec access unit into a raw frame.
type Decoder interface {
// Decode returns ok=false when the access unit produced no picture yet.
Decode(ctx context.Context, au []byte) (frame I420Frame, ok bool, err error)
Close() error
}
Build
Go everywhere, except the codec
The interface is built on Glutter, and the camera is read from Video4Linux2 with ioctls and memory-mapped buffers. Both are pure Go and need no development files. Only the layer over FFmpeg’s libavcodec, which encodes and decodes VP8, needs a C toolchain.
- The camera is asked for the mode closest to 1280x720 at about 30 frames a second
- The list of cameras is checked every 2 seconds for one that was plugged or unplugged
- At run time it needs a Wayland or X11 session with OpenGL ES 3.0 or Vulkan
# Are the FFmpeg libraries found?
pkg-config --modversion libavcodec
# Does your FFmpeg have the VP8 encoder?
ffmpeg -hide_banner -encoders | grep libvpx
Tests
Tested without a camera and without a display
The tests of the interface run on Glutter’s fake platform, and the camera test skips when there is no camera. The codecs sit behind two small interfaces, and the end-to-end test puts stub codecs in their place.
- An end-to-end test with a real HTTPS and WebSocket server and real pion peers
- A round trip through the real VP8 codec at VGA, 720p and portrait
- Unit tests for the tokens, the messages, the certificate and the choice of the network interface
# No display, no camera and no phone are needed.
go test -race ./...
Six messages make a pairing
The phone and the app talk over one WebSocket, in JSON, only to agree on the WebRTC connection. The video itself goes over UDP on your LAN.
| Message | From | What it means |
|---|---|---|
hello | the phone | The page is ready, and the camera was allowed. It carries the user agent |
offer | the app | The SDP offer |
answer | the phone | The SDP answer |
candidate | both | An ICE candidate. An empty one ends the list |
bye | both | A clean end of the session |
error | the app | For example session_busy, when another phone is connected |
The design and its reasons are in docs/SPEC.md.
Built on Glutter
The window of Desktop Mirror is drawn by my own GUI toolkit, and its errors are made with my errors package.
What Desktop Mirror does not do
Better to read it here than to find it out.
- It has not been run against a real phone browser yet. The script of the page is only checked for its syntax, and a test phone that is written in Go speaks the same protocol.
- Video only. There is no audio.
- Both devices must be on the same network. There is no TURN, and STUN is off unless you give a server.
- One phone at a time, and no recording.
- Linux only. The camera is read through Video4Linux2, and there are no builds for Windows or macOS.
- The bitrate is fixed at 1.5 Mbit/s, and the VP8 encoder runs on the CPU.
- If you reload the page on the phone during a session, you scan the QR code again.
- A camera that delivers MJPEG without Huffman tables is not handled.
In three steps#
- Start the desktop app and pick a PC camera.
- Scan the QR code with your phone and accept the one-time certificate warning.
- Allow camera access on the phone. Both feeds start.
The app serves the phone’s page itself over HTTPS on your LAN, so both devices must be on the same network. Nothing leaves your network unless you configure a STUN server.
Under the hood#
One process does everything. A session goes from waiting for the phone, to negotiating, to connected, and back to waiting with a new token when the phone leaves. Frames of the PC camera are converted to I420, encoded as VP8 and sent with pion. Frames of the phone are decoded and drawn as an image in the Glutter window.
Only the package of the interface imports Glutter. The rest is plain Go that the tests run without a display.
- Kind
- Application
- Language
- Go
- License
- MIT
Build it on Linux
You need Go 1.27 or newer, a C toolchain, pkg-config, and the development files of an FFmpeg that was built with libvpx. Allow the HTTPS port and the UDP range through your firewall if the phone cannot connect.
# The build dependencies on Fedora
$ sudo dnf install golang gcc pkgconf-pkg-config \
libavcodec-free-devel libavutil-free-devel libvpx
# Get the source, build and start
$ git clone https://github.com/rm4n0s/desktop-mirror
$ cd desktop-mirror
$ go build -o desktop-mirror ./cmd/desktop-mirror
$ ./desktop-mirror