About screen mirroring, app windows and the webcam
The phone’s screen, single phone apps, the phone’s audio and its camera reach the desktop through the shell helper, a program that the daemon starts on the phone through adb. All four features therefore belong to the enhanced tier, and none of them has a normal-tier variant. About privilege tiers and adb explains the helper and why it is needed. The webcam comes with the next release, 0.5.0.
For the steps, see Mirror the screen and open phone apps and Use the phone as a webcam.
The parts
phone desktop
┌─────────────────────────┐ ┌─────────────────────────────────┐
│ shell helper │ control │ tetherd │
│ │◄──────────►│ starts and supervises helper │
│ display ─► encoder ────┼──video────►│──► fd ──► Tether app: window │
│ input injector ◄───────┼──input─────│◄── fd ◄── mouse and keyboard │
│ audio capture ─► Opus ─┼──audio────►│ decode ─► desktop sound server │
│ camera ─► encoder ─────┼──video────►│ decode ─► virtual camera │
└─────────────────────────┘ adb tunnel └─────────────────────────────────┘
The helper keeps one control connection to the daemon for small requests and answers, and opens a separate connection for every stream: one per phone screen or app window, one for audio, one for the camera. All of them run through the adb tunnel, over USB or Wi-Fi, and each must present the one-time token of this helper start. The daemon gives the helper that token on its standard input when it starts it.
For the phone screen and app windows, the daemon does not sit in the path. It sets the stream up and then passes its end of the connection to the desktop app as a file descriptor. Video goes from the helper to the window, and input from the window to the helper on the same connection, without a detour through the daemon or D-Bus. The record format is in the wire protocol reference.
Capture and encoding on the phone
For the phone screen, the helper asks Android for a display that mirrors the main one and hands it the input surface of a hardware video encoder. Android composes the screen straight into the encoder, and the helper never touches pixels. The codec is H.264, at 8 Mbit/s unless the desktop asks for something else, and at the phone’s own resolution unless a maximum size is set.
When the phone rotates, the helper restarts the encoder and announces the new size before the first new frame, and the window on the desktop resizes to match.
Two options ride along with a mirror. Screen off switches the phone’s panel off while the picture continues on the desktop. The helper restores the panel when the last window that asked for it closes. If the helper is killed, a small detached process on the phone restores it, so the phone is not left dark. Stay awake keeps the phone from going to sleep while it is mirrored.
Android itself composes screens that an app marks as protected as black. The helper cannot change that.
Virtual displays for app windows
An app window is not a cropped view of the phone’s screen. For each app window the helper creates a new display on the phone, with the size of the window, starts the app on it and mirrors that display. The phone’s own screen is free for something else, and several apps can run side by side. The limit is eight windows per phone.
The display follows the window. When you resize the window, the desktop waits until the size has settled and then asks the helper to resize the display, and the app is laid out again as it would be on a phone or tablet of that size. The density is chosen so that one Android layout unit is one desktop pixel, which keeps text about as large as on the desktop.
Such a display has no status bar and no navigation bar, so the window supplies its own Back button. The phone’s on-screen keyboard is kept away from it, and the desktop keyboard types.
The display lives exactly as long as the stream. Closing the window destroys the display and closes the app on it. There is no option yet to move the app to the phone’s screen. In the other direction, the helper notices when no app is visible on the display any more, for example because you closed it in the phone’s Recents, and ends the stream, which closes the window.
Decoding and display on the desktop
The window decodes with GStreamer and draws into a GTK widget through
OpenGL. GStreamer was chosen because it is packaged on every target
distribution and finds the hardware decoder by itself. Linking against
ffmpeg directly was rejected because its library versions differ between the
distributions that one .deb has to serve. The GTK sink is linked into the
desktop app statically, because it is packaged on only one of the three
target distributions. ADR-0018
records these choices.
The decoder is chosen when the stream starts: hardware decoders first (VA-API, then NVIDIA, then V4L2 stateless decoders), software decoders after them. If a decoder fails while running, the window takes the next one and asks the phone for a new key frame. A switch in the preferences skips the hardware decoders for machines where they misbehave.
The pipeline is tuned to show the newest picture and may skip older ones. It does not synchronise to timestamps, so a frame is shown when it is decoded. A newer decoded frame replaces an older one that has not been drawn. If encoded frames pile up in front of the decoder for more than a moment, the window throws the backlog away and asks for a key frame.
Input
The mouse acts as one finger. The window maps the pointer position into the coordinates of the video frame, allowing for the bars that appear when window and picture have different shapes. Each event also names the frame size it refers to. The helper scales the position to the real display and discards events that refer to a frame size from before a rotation, so a click does not land in the wrong place after the screen has turned.
Keys take one of two routes. Characters go through the desktop’s input
method first, so that dead keys, compose sequences and input methods for
other scripts work, and arrive on the phone as finished text. Keys with
Ctrl or Alt, and keys that produce no character, are sent as Android key
presses with their modifiers, so that shortcuts inside Android apps behave
as on a keyboard plugged into the phone. If a character has no key on
Android’s virtual keyboard, the helper pastes it and then puts the previous
clipboard content back.
Pointer motion is limited to about 125 events per second. Everything that is held down is released when the window loses focus or the screen rotates, so that no finger stays stuck on the phone.
Audio capture
Phone audio is independent of the windows. The helper captures what the phone plays, encodes it as Opus in packets of 20 ms and sends it on its own connection. The daemon decodes and plays it, so the sound continues when no window is open. It appears in the desktop’s mixer as a stream of its own.
Android offers the shell more than one way to capture, and they differ in what the phone itself still plays:
| Source | What is captured | The phone itself |
|---|---|---|
output | everything | silent |
playback | media, games, navigation and similar; not ringtones, alarms, notification sounds or calls | silent for what is captured |
playback_dup | as playback | keeps playing too |
The helper reports which of these work on the phone, and the daemon takes the first available one in that order unless configured otherwise.
Capture stops while a call exists on the phone and resumes afterwards. Call audio travels over Bluetooth by a different route (see About calls and call audio), and the two must not compete.
The webcam, and why it needs a kernel module
For the webcam the helper opens the phone’s camera and, as with the screen, lets it render straight into a video encoder. No app is visible on the phone and no notification is shown. The phone’s own camera indicator is the sign that the camera is on. The helper is the only way the camera reaches the desktop, so there is no webcam without the enhanced tier.
The hard part is on the desktop. Video call apps and the default browser
builds on the target distributions list cameras from one source only: the
kernel’s video device interface, V4L2. A camera offered through PipeWire
would need no special rights, but those apps do not show it. A V4L2 device
without hardware behind it exists only through the kernel module
v4l2loopback, which is not part of the standard kernel, and loading a
kernel module needs administrator rights.
Tetheron takes this route and keeps the privileged part small:
- The module is not installed with the package and never loaded silently. Loading it from the package’s install script was rejected because it would surprise people who never use the webcam, and fail noisily under Secure Boot.
- The daemon has no administrator rights. One small script with a fixed set of three actions creates the device, started through the system’s authorisation prompt.
- Two failures can be explained to you but not repaired for you: a module that no longer builds after a kernel update, and Secure Boot refusing a module whose signing key is not enrolled.
The daemon owns the device. It decodes the camera stream, rotates and flips the picture as you set it, scales it into a fixed frame and writes it to the device. The format of the device never changes while the daemon holds it. Changing camera, rotating or a short interruption changes only the picture inside the frame, with black bars or black frames where needed, so a call app never sees the format change under it.
Orientation is a setting. The helper does not track how the phone is held, because that would have needed more hidden parts of Android for something the desktop does in one step.
The webcam is a switch and does not start when a call app opens the camera, because the module version on the target distributions does not tell the daemon when an app starts reading. The phone’s camera is therefore on for as long as the switch is on. And because the device is created so that apps see it only while it is fed, the switch must be on before the call app looks for cameras.
The shell user has a low priority for the camera. Any app on the phone that opens the camera takes it away, and the webcam picture stops until that app lets go.
ADR-0019 records the webcam design.
Latency
The design aims at a delay of no more than about a tenth of a second between the phone’s screen and the window. scrcpy reports 35 to 70 ms with the same approach of an encoder on the phone and a stream through adb. Tetheron’s own delay has not been measured yet.
Four things keep the delay short. The display or the camera feeds the encoder directly, so nothing is copied on the phone. Video and input for windows bypass the daemon. The desktop shows frames as they arrive and drops late ones. Hardware decoding is used where the system has it.
Other things lengthen it:
- All streams of one phone share the single adb connection, so a burst on one stream can hold up the others.
- Software decoding adds delay on machines without a usable hardware decoder.
- The first start of a hardware decoder can take several seconds. Later starts are fast.
- Audio is buffered for a few hundredths of a second on the desktop to avoid crackling. Audio and video are not synchronised with each other.
Limits
- Protected screens are black.
- Closing an app window closes the app on that display.
- The helper depends on hidden parts of Android that change between releases. A feature can disappear after a phone update until Tetheron follows. The helper’s probing keeps the other features working.
- Hardware decoding has been tested with NVIDIA on X11 only. Supported platforms lists what has been tested.