Skip to main content

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.scrcpy
Location: 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

Handles server connection setup, including:
  • Reading user preferences (resolution, bitrate, delay)
  • Validating server address
  • Deploying server JAR via ADB
  • Establishing socket connection
From MainActivity.java:738-791:
Manages activity states with service binding:
Key lifecycle methods:
  • onCreate(): Initialize UI and restore state
  • onPause(): Pause streaming and optionally disconnect
  • onResume(): Resume streaming or reconnect
  • onSaveInstanceState(): Persist configuration
Calculates aspect ratio and sets up touch event handling (MainActivity.java:362-433):
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):
Touch Event Processing (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

1

Connect to Device

2

Push Server JAR

3

Setup Port Forwarding

4

Launch Server

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):

Server Module

Learn about screen capture and encoding

Architecture Overview

Understand the complete system design