Skip to main content

Module Overview

The server module (server/) runs on the remote Android device and handles screen/audio capture, encoding, and input event injection. Package: org.server.scrcpy
Location: server/src/main/java/org/server/scrcpy/
Deployment: Packaged as APK, renamed to scrcpy-server.jar, executed via app_process

Entry Point: Server.java

The main entry point for the server process. File: Server.java (104 lines)

Main Method

The server automatically deletes its own JAR file after startup to prevent version conflicts on subsequent connections.

Command Line Arguments

The server accepts the following arguments from app_process:

Initialization Flow

Core Components

DroidConnection

Manages the TCP socket connection to the client. File: DroidConnection.java (93 lines)

Socket Setup

Control Event Reception

Reads 20-byte touch/key events from the client:

ScreenEncoder

Encodes screen content to H.264 video stream. File: ScreenEncoder.java (268 lines)

Encoding Configuration

Stream Pipeline

1

Send Device Resolution

First 16 bytes contain width and height:
2

Start Audio Capture

Launch audio encoder in separate thread:
3

Create MediaCodec

Configure H.264 encoder with screen dimensions:
4

Setup Screen Capture

Create input surface and start capture:
5

Encode Loop

Read encoded frames and send to client:

Encoding Loop

AudioEncoder

Encodes device audio to AAC stream. File: AudioEncoder.java (247 lines)

Audio Capture

Uses direct audio capture from system output:

Callback-Based Encoding

Uses MediaCodec.Callback for efficient asynchronous encoding (API 23+):

ScreenCapture

Captures screen content using Android’s display capture APIs. File: ScreenCapture.java (85 lines)

Virtual Display Creation

On Android 12+, secure displays cannot be created with shell permissions. The implementation handles this by checking SDK version and adapting the display creation strategy.

EventController

Injects touch and keyboard events into the Android system. File: EventController.java (270 lines)

Control Loop

Multi-Touch Support

Implements pointer state tracking for simultaneous touches:

Event Injection

System Service Wrappers

The server module includes wrappers for hidden Android system APIs:

Available Wrappers

ServiceManager

Access system services via reflection

DisplayManager

Manage displays and virtual displays

InputManager

Inject input events

SurfaceControl

Low-level surface manipulation

WindowManager

Window and display properties

PowerManager

Screen power state control
Location: server/src/main/java/org/server/scrcpy/wrappers/

Server Packaging and Deployment

Build Process

The server module uses a custom Gradle task to package the APK:

Deployment Flow

1

Build

Gradle builds the server module as a standard Android APK
2

Copy

APK is copied to client assets and renamed to .jar
3

Extract

Client extracts JAR from assets at runtime
4

Push

Client pushes JAR to /data/local/tmp/ via ADB
5

Execute

Client launches server using app_process:
6

Cleanup

Server deletes its own JAR on startup
The .jar extension is used even though the file is an APK because app_process can execute DEX code from any file, and this naming avoids confusion with installable APKs.

Rotation Handling

The server detects screen rotation and signals the client to reconfigure:

Performance Considerations

Frame Rate Control

  • Default: 60 fps
  • I-frame interval: 10 seconds
  • Repeat frame delay: 6 frames (100ms at 60fps)

Bitrate Settings

  • Video: User-configurable (typically 2-8 Mbps)
  • Audio: Fixed at 128 kbps

Memory Management

Packet size validation prevents memory exhaustion:

Client Module

Learn about decoding and UI components

Architecture Overview

Understand the complete system design