Module Overview
The client module (app/) is an Android application that provides the user interface and handles video/audio decoding, user input collection, and ADB communication.
Package: org.client.scrcpyLocation:
app/src/main/java/org/client/scrcpy/
Core Components
MainActivity
The main activity manages the application lifecycle and coordinates between UI and streaming service. File:MainActivity.java (837 lines)
Key Responsibilities
Connection Management
Connection Management
Handles server connection setup, including:
- Reading user preferences (resolution, bitrate, delay)
- Validating server address
- Deploying server JAR via ADB
- Establishing socket connection
MainActivity.java:738-791:Lifecycle Management
Lifecycle Management
Manages activity states with service binding:Key lifecycle methods:
onCreate(): Initialize UI and restore stateonPause(): Pause streaming and optionally disconnectonResume(): Resume streaming or reconnectonSaveInstanceState(): Persist configuration
Display and Touch Handling
Display and Touch Handling
Calculates aspect ratio and sets up touch event handling (
MainActivity.java:362-433):Rotation Handling
Rotation Handling
Supports dynamic orientation changes:
Scrcpy Service
Background service that manages the streaming connection and coordinates decoders. File:Scrcpy.java (486 lines)
Architecture
Key Methods
Connection Loop (Scrcpy.java:342-470):
Scrcpy.java:136-182):
VideoDecoder
Decodes H.264 video stream using Android’s MediaCodec API. File:decoder/VideoDecoder.java (131 lines)
Implementation Details
AudioDecoder
Decodes AAC audio stream and plays back using AudioTrack. File:decoder/AudioDecoder.java (172 lines)
Key Features
- Format: AAC (audio/mp4a-latm)
- Sample Rate: 48 kHz
- Channels: Stereo (2 channels)
- Bit Rate: 128 kbps
Playback Pipeline
SendCommands
Handles ADB command execution for server deployment. File:SendCommands.java (117 lines)
Server Deployment Process
Data Models
Packet Structures
VideoPacket and AudioPacket share a common base structure:Threading Model
Main Thread
- UI rendering
- Activity lifecycle
- User input collection
Service Thread
- Socket I/O loop
- Packet routing
- Event queue management
VideoDecoder Worker
- MediaCodec input feeding
- Output buffer management
- Surface rendering
AudioDecoder Worker
- MediaCodec audio decoding
- AudioTrack playback
- PCM buffer management
Error Handling
The client implements retry logic and error recovery:Performance Optimizations
Frame Dropping
The service implements intelligent frame dropping based on delay threshold:Multi-touch Support
Supports multiple simultaneous touch points with pointer ID tracking (Scrcpy.java:161-170):
Related Documentation
Server Module
Learn about screen capture and encoding
Architecture Overview
Understand the complete system design
