scrcpy v5.0.1: Virtual displays, kernel UHID isolation and binary wire protocol
Offline binary and upstream source teardown of Genymobile/scrcpy v5.0.1: ADB tunnel bootstrap, Os.setuid(2000) privilege drop, --new-display / --flex-display VirtualDisplay flags, SurfaceEncoder 12B packet framing, AudioPlaybackCapture, API 35 UHID display binding, and 22 ControlMessage opcodes.
- ADB reverse + UID 2000 drop + CleanUp sidecar
- OWN_DISPLAY_GROUP + physical panel off + H.264/HEVC
- API 35 UHID display binding + 22 ControlMessages
Investigation question
How does scrcpy v5.0.1 bootstrap an unbundled Android app_process server, isolate virtual displays from the physical screen, route kernel /dev/uhid HID devices per display on Android 15+, capture playback audio, and frame its 22-opcode binary socket protocol?
Observed
Offline inspection of official SHA-256-verified Genymobile/scrcpy v5.0.1 release artifacts (commit c939183a7cdca4599cee337e60bc0e147b6c9828)—covering the 9.60 MB macOS arm64 host binary (statically linking SDL3, FFmpeg, and libusb against 23 Apple system frameworks) and the unbundled 734 KB scrcpy-server-v5.0.1 DEX payload (825 classes, minSdk 21, targetSdk 36)—establishes the exact client-server architecture. With zero APK installation, the host pushes scrcpy-server.jar to /data/local/tmp, allocates a 31-bit random scid, opens adb reverse (or adb forward) sockets, spawns app_process as com.genymobile.scrcpy.Server, and awaits a 1-byte 0x00 dummy handshake plus a 64-byte UTF-8 device header across up to three streams (video, audio, bidirectional control). Notably, Server.dropRootPrivileges() calls Os.setuid(2000) when started as UID 0 so Android binder IPCs such as ClipboardManager behave under shell UID 2000, while a persistent CleanUp sidecar restores display power and stay-awake flags on SIGKILL or USB disconnect. For headless displays, NewDisplayCapture combines OWN_DISPLAY_GROUP / DEVICE_DISPLAY_GROUP, ALWAYS_UNLOCKED, and OWN_FOCUS flags (API 33/34+) with --flex-display debounced resize (RESIZE_DISPLAY) and SET_DISPLAY_POWER(false) on physical display 0 (noting USE_ANDROID_15_DISPLAY_POWER remains disabled in v5.0.1 in favor of SurfaceControl). For input and audio, UhidManager opens kernel /dev/uhid (BUS_VIRTUAL) and calls InputManager.addUniqueIdAssociationByPort on API 35+ to isolate UHID keyboards/mice strictly to the target virtual display, while AudioPlaybackCapture / REMOTE_SUBMIX streams Opus/AAC/FLAC/raw audio.
Inferred
Because the host C client decouples ADB socket framing cleanly from SDL3 window rendering (with sc_video_regulator capping decoded queue depth at SC_BACKPRESSURE_THRESHOLD = 8), headless automation daemons can speak the exact 3-socket v5.0.1 protocol directly against official scrcpy-server-v5.0.1 without GUI dependencies or slow adb screencap PNG polling. Pairing coordinate-exact SDK touch (opcode 2) with SET_CLIPBOARD paste (opcode 9) covers standard taps, while UHID keyboard descriptors bypass flaky soft-IME text limits in WebViews and terminals—provided callers wait for display initialization before sending START_APP (opcode 16).
Unknown & limitations
This teardown is strictly offline static inspection of official v5.0.1 release binaries and upstream source; no live phone, ADB session, hardware MediaCodec encoder bitrate/latency curve (H.264 vs HEVC vs AV1), or OEM SurfaceControl.setDisplayPowerMode edge behavior on Android 17 was executed.
Architecture
A native C11 host client (SDL3, FFmpeg, libusb) communicates over ADB reverse/forward sockets (abstract UNIX scrcpy_<scid>) with an unbundled Dalvik/ART app_process server (com.genymobile.scrcpy.Server 5.0.1) running under UID 2000 alongside a pipe-monitored CleanUp restoration thread.
Domain & data models
Video packets carry a 12-byte header (64-bit PTS where bit 63 flags codec config and bit 62 flags keyframes, followed by 32-bit payload size) after an initial 4-byte codec tag (h264/h265/av1), width, and height; VirtualDisplay toggles PUBLIC, PRESENTATION, OWN_CONTENT_ONLY, SUPPORTS_TOUCH, ROTATES_WITH_CONTENT, TRUSTED, OWN_DISPLAY_GROUP, ALWAYS_UNLOCKED, and OWN_FOCUS.
Workflows & state machines
Startup executes push -> adb reverse -> app_process -> 1-byte dummy read -> 64B device header -> stream readers -> optional SET_DISPLAY_POWER(false) -> START_APP (+pkg force-stop or ?fuzzy query); UhidManager creates kernel HID devices via UHID_CREATE2 and forwards epoll UHID_OUTPUT LED/caps-lock reports back to host.
API & protocol surfaces
Big-endian ControlMessage socket framing with 22 opcodes (0..21: INJECT_KEYCODE, INJECT_TEXT, INJECT_TOUCH_EVENT, INJECT_SCROLL_EVENT, BACK_OR_SCREEN_ON, panel controls, GET/SET_CLIPBOARD, SET_DISPLAY_POWER, ROTATE_DEVICE, UHID_CREATE/INPUT/DESTROY, START_APP, RESET_VIDEO, RESIZE_DISPLAY, SCAN_FILE; bounded by SC_CONTROL_MSG_QUEUE_LIMIT=60+4) and 3 DeviceMessage responses (CLIPBOARD, ACK_CLIPBOARD, UHID_OUTPUT).