-
Notifications
You must be signed in to change notification settings - Fork 8
Runtime Main Path
This page provides the end-to-end reading path for a remote-control session. Session State and Events and Session Configuration and Connection Lifecycle own the detailed state model and configuration ownership rules.
flowchart TD
A["SessionData"] --> B["ScrcpyOptions"]
B --> C["SessionManager.start"]
C --> D["Session"]
D --> E["ConnectionLifecycle.connect"]
E --> F["ADB candidate race and verification"]
F --> M{"Compatibility mode?"}
M -->|Yes| N["ADB screenshot stream and ADB input"]
M -->|No| G["Clean old scrcpy resources"]
G --> H["Prepare tunnel and push server"]
H --> I["Start scrcpy server"]
I --> J["Connect video → optional audio → control"]
J --> K["Validate video dummy byte and read metadata"]
K --> L["Create streams, health monitor, decoders and controller"]
SessionManager currently keeps one active Session. The session owns its configuration snapshot, ADB reference, runtime state, event queue, and monitoring bus. ConnectionLifecycle orchestrates one connect or disconnect operation under a mutex.
-
SessionData.toScrcpyOptions()creates the runtime snapshot, which must contain at least oneConnectionCandidate. -
SessionManager.start()stops any old session before creating a newSession. -
ConnectionLifecycleraces candidates and obtains a verifiedAdbConnection. The winning object becomes the device fact used by this run. - Old sockets, forwards, and the server for the previous SCID are cleaned before a new connection is prepared.
- The attempt receives a new SCID and socket name. The lifecycle prepares either a
DIRECT_ADBorADB_FORWARDtunnel and makes the server asset available. - Any required remote codec detection must finish before the final server command reads the launch configuration.
- The scrcpy server starts, then video, optional audio, and control sockets connect in strict protocol order.
- The client validates the dummy byte on the video socket and fully reads device and media metadata. Audio codec id
0lets the server explicitly disable audio. - Video and audio streams are created. The final negotiated result controls health monitoring, after which the client starts decoders, rendering, audio playback, and the controller.
Compatibility mode branches after ADB verification. It skips SCID, tunnel, scrcpy-server, sockets, metadata, MediaCodec, audio, and the scrcpy control channel. CompatibilityModeController opens the bundled dadb helper's JPEG screenshot stream, publishes the source dimensions and latest frame, and serializes ADB input commands for taps, swipes, keys, and text. Capture startup and interruption retry up to three consecutive failures with bounded delay; stopping closes the helper stream and capture job.
The scrcpy server assigns roles by accept() order, so the client must use:
audio enabled: video -> audio -> control
audio disabled: video -> control
The sockets must never be connected concurrently. After video connects, the client must also avoid blocking the whole sequence before the remaining required sockets connect. ConnectionSocketManager implements this invariant and ConnectionSocketOrderTest protects it.
- ADB connected: only the device channel is available.
- Server started: the remote process is ready for socket setup.
-
SessionState.Connected: all required sockets are connected and the video dummy byte is confirmed. - Media usable: metadata must also succeed, streams must exist, and the corresponding decoder must start.
- Compatibility frame usable: ADB is connected and the first helper screenshot has arrived.
SessionState.Connectedalone does not remove the loading overlay in this mode.
Do not collapse these phases into one connected boolean. Management features may require only ADB; the remote screen requires the full scrcpy and media path.
- User exit follows explicit cleanup: cancel pending reconnection, move the session to
Idle, stop media and monitoring, disconnect scrcpy resources, and stop the session. - A failed connection attempt cleans resources it has already created. Coroutine cancellation preserves cancellation semantics while completing necessary cleanup in a non-cancellable region.
- Transport, socket, or decoder failures may request reconnection. Events enter one session serially to prevent duplicate reconnect jobs.
- A fixed decoder size failure or server capture/encoding failure publishes a user-confirmation request instead of silently changing configuration. Approved recovery reconnects the current run through
1920 → 1080 → 720, with at most three attempts and without persistingmaxSize; automatic reconnect pauses while confirmation is pending. -
ConnectionLifecycle.disconnect()stops monitoring, closes sockets and the shell stream, removes a forward when present, terminates the server for the current SCID, and clears the active connection snapshot. - A healthy ADB connection may remain cached by the ADB layer. The ADB layer validates and removes failed connections; scrcpy cleanup does not automatically destroy the transport.
| Concern | Entry point |
|---|---|
| Active session |
infrastructure/scrcpy/session/SessionManager.kt, Session.kt
|
| Connection and cleanup orchestration | infrastructure/scrcpy/connection/ConnectionLifecycle.kt |
| ADB candidate race | infrastructure/adb/connection/AdbConnectionRace.kt |
| Socket order and dummy byte | infrastructure/scrcpy/connection/ConnectionSocketManager.kt |
| Metadata and stream creation | infrastructure/scrcpy/connection/ConnectionMetadataReader.kt |
| Event reduction | infrastructure/scrcpy/session/internal/SessionRuntimeEvents.kt |
| Compatibility capture and ADB input | infrastructure/scrcpy/client/CompatibilityModeController.kt |
-
ConnectionSocketOrderTest: protocol connection order. -
AdbConnectionRacePolicyTest: candidate-race decisions. -
ConnectionFailureClassifierTest: closure signals and error classification. -
ScrcpyStreamProtocolTestandScrcpyAudioPacketTest: media protocol boundaries. -
SessionCodecFallbackTest: codec fallback for the current run. -
CompatibilityCaptureSettingsTest,RemoteConnectionOverlayStateTest: compatibility capture policy and first-frame readiness. -
VideoCaptureRecoveryPolicyTest: user-confirmed temporary size tiers.
- Overview / 总览
- Quick Start / 快速开始
- Connection & Pairing / 连接设备与配对
- Session Management / 会话管理
- Remote Control / 远程控制
- Settings / 设置
- Management Features / 管理功能
- FAQ / 常见问题
- Overview / 总览
- Technical Index / 技术文档索引
- Audit Checklist / 初始化与更新清单
- Conventions / 文档维护约定
- Architecture / 架构
- Module Map / 模块地图与边界
- Runtime Path / 运行时主链路
- Session State / 会话状态与事件
- Session Lifecycle / 会话配置与连接生命周期
- ADB, USB & Wireless / ADB、USB 与无线
- ADB Lifecycle / ADB 连接生命周期
- Pairing / 设备配对与无线调试
- Codecs / 编码与解码
- Remote Interaction / 远程交互性能与悬浮菜单
- Clipboard / 剪贴板同步
- Management / 管理功能
- Events & Shell / 事件、监控与 Shell
- Logging / 日志系统与维护
- Troubleshooting / 分层排障方法
- UI Design / UI 设计系统
- Bilingual Copy / 双语与文案
- Remote UI Analysis / 远程 UI 布局分析
- Engineering Rules / 工程与验证规则
- External Research / 外部研究与取舍
Technical topics appear here only after their page-level audit is complete. / 技术主题仅在完成逐页审核后加入侧边栏。