-
Notifications
You must be signed in to change notification settings - Fork 8
Session Configuration and Connection Lifecycle
This page defines ownership for persisted configuration, device capabilities, connection candidates, the active session, and resources from one scrcpy connection attempt. Its goal is to prevent field-by-field mirrors, preferred addresses overriding the actual connection, and partially cleaned runtimes.
| Data | Owner | Lifetime |
|---|---|---|
| Saved session record |
SessionData / SessionRepository
|
Persistent |
| User-editable scrcpy configuration | ScrcpyConfig |
Persistent session |
| Automatically detected codec capability | DeviceCapabilityCache |
Cache invalidated by device identity |
| Connection strategy | List<ConnectionCandidate> |
Persisted, with priority adjusted from results |
| Current runtime snapshot | ScrcpyOptions |
Active session |
| Active session | SessionManager.activeSession |
From start through cleanup |
| Mutable runtime state and reconnect budget | SessionRuntimeState |
Internal to Session
|
| Winning ADB object | Session.adbConnection |
Active session |
| Attempt tying SCID, tunnel, and sockets together | ActiveScrcpyConnection |
One connection attempt |
SessionData.toScrcpyOptions() reuses the complete ScrcpyConfig, capability cache, and candidate list instead of maintaining a second field-by-field configuration. ScrcpyOptions requires at least one candidate. After ADB wins, Session.deviceIdentifier comes from the actual connection; only before that does it fall back to the preferred candidate identity.
-
ScrcpyConfigexpresses user intent only, including display, media, control, andScrcpyTunnelMode. -
DeviceCapabilityCacherepresents remote encoders and final automatic selections only; changing the device serial resets it as a whole. - Explicit user choices outrank automatic cache values. The cache is used only when the choice is empty.
- Codec detection and temporary decoder fallback must not silently rewrite user selections.
- Profiles should apply complete configuration semantics instead of a partial field map that can drift.
Tunnel modes:
-
DIRECT_ADB: open the remote localabstract stream directly and adapt it as a socket. -
ADB_FORWARD: create an ADB forward from local TCP to the remote localabstract socket.
raceAdbConnections may attempt different ADB candidates concurrently. This is separate from the rule that scrcpy sockets must connect sequentially.
- The race layer owns candidate deduplication, the USB preference window, winner selection, and loser cleanup.
-
AdbConnectionManagerowns per-device locking, the connection pool, and TCP/mDNS/USB routing. - The connector returns the actual
AdbConnection; server startup, monitoring, control, and cleanup all use that same winning object. - Persisted candidates may record success time and failure count to influence later ordering, but a “preferred candidate” must never masquerade as the connected device for this run.
ActiveScrcpyConnection freezes the sessionId, actual adbConnection, local port, SCID, socket name, and tunnel mode. Cleanup must use this attempt snapshot instead of re-deriving resource identity from configuration that may have changed.
See Runtime Main Path for the full sequence. The sequential video → optional audio → control socket order is an invariant of the protocol.
- The session's single event Channel serializes reconnect requests. Duplicates are ignored while state is
ReconnectingorFailed. - Reconnect budget exists only in
SessionRuntimeState. Its limit comes fromScrcpyConstants.MAX_RECONNECT_ATTEMPTS(currently 3), not a UI-local counter. - Entering
SessionState.Connectedimmediately resets the budget, so a later outage starts from attempt one. - Reconnection rebuilds the scrcpy server, tunnel, sockets, and media path. The ADB object is reused only while healthy.
- Temporary decoder-size recovery changes
maxSizeonly for the current session and does not persist it.
Explicit stop and failure-triggered reconnection have different semantics:
- Cancel pending reconnect or connection work.
- Process
RequestCleanupso the session deterministically reachesIdleand clears component state. - Stop media, health monitoring, and the controller.
- Under the lifecycle mutex, close sockets and shell resources, remove the forward, and terminate the server for the current SCID.
- Stop session monitoring and event processing and release session references.
When connection work is cancelled, created scrcpy resources are reclaimed in a NonCancellable region before CancellationException continues upward. Do not record user cancellation as an ordinary failure, and do not require a generated device id before cleanup can run.
EOF, socket/stream closed, connection reset, broken pipe, closed channel, and classified ADB-close exceptions are disconnection signals. They should produce a concise reason and enter the unified policy. Protocol corruption, malformed packets, MediaCodec failures, and unknown I/O errors should retain complete diagnostics.
- The winning candidate object is the one used by the session, server, monitoring, and control.
- Exiting during connection leaves no session, server, forward, or sockets and does not start background reconnection.
- Reconnect budget resets after every successful recovery; the next outage starts at attempt one.
- Each rebuild creates a new SCID and preserves protocol socket order.
- Disconnection events caused by explicit closure do not start another reconnect.
- Tests for configuration saving, candidate policy, racing, socket order, close classification, and codec fallback remain green.
- 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. / 技术主题仅在完成逐页审核后加入侧边栏。