# satertek/moonlight-android architecture

> Architecture diagram of the GitHub repository satertek/moonlight-android, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/satertek/moonlight-android
- Repository: https://github.com/satertek/moonlight-android

## Overview

##### Main Components Overview
The system should be divided into these key components:

a) Client-Side Components (Android App):
- UI Layer (Activities and Views)
- Input Handling System
- Audio/Video Decoders
- Network Communication Layer
- Configuration Management

b) External Components:
- NVIDIA GameStream Server
- Sunshine Server
- Game Host PC

##### Detailed Component Breakdown

Frontend Layer:
- UI components (StreamView, VirtualController)
- Connection to preferences and settings management

Core Streaming Components:
- To sub-components:
- Video Pipeline (MediaCodecDecoderRenderer)
- Audio Pipeline (AndroidAudioRenderer)
- Input Management (ControllerHandler)
- Network Communication (NvHTTP)

Input Handling System:
- Multiple input sources:
- Touch Input
- Physical Controllers
- Virtual Controller
- Keyboard/Mouse
- To input processing components

##### Connection Flows

- Discovery flow (mDNS discovery)
- Authentication flow (pairing process)
- Streaming flow (video/audio/input data)
- Control flow (game launch/stop)

##### Component Relationships

Clear boundaries between:
- Java/Kotlin code
- Native code (JNI layer)
- External services
- System services

##### Technical Details

Annotations for:
- Protocol information (GameStream protocol)
- Codec details (H.264/HEVC)
- Input protocols
- Network requirements

##### Architectural Patterns

Highlight:
- MVC pattern in UI
- Service-based architecture for background operations
- Observer pattern for event handling
- Factory pattern for component creation

##### Android platform integration

Keep the abstraction level high enough to be understandable but detailed enough to show the sophisticated nature of the streaming system. Use clear boundaries between the Android app components and external systems while showing their interactions.

This will provide a clear understanding of how Moonlight Android works as a game streaming client while maintaining the complexity necessary for a principal software engineer to understand the system's architecture.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles
    classDef ui fill:#3498db,stroke:#2980b9,color:white
    classDef streaming fill:#2ecc71,stroke:#27ae60,color:white
    classDef input fill:#f1c40f,stroke:#f39c12,color:black
    classDef network fill:#e74c3c,stroke:#c0392b,color:white
    classDef external fill:#9b59b6,stroke:#8e44ad,color:white
    classDef config fill:#95a5a6,stroke:#7f8c8d,color:white

    %% Frontend Layer
    subgraph Frontend
        PcView["PcView"]:::ui
        AppView["AppView"]:::ui
        Game["Game Activity"]:::ui
        StreamView["StreamView"]:::ui
    end

    %% Core Streaming Components
    subgraph Streaming
        NvConnection["NvConnection"]:::streaming
        VideoDecoder["MediaCodecDecoderRenderer"]:::streaming
        AudioDecoder["AndroidAudioRenderer"]:::streaming
    end

    %% Input Handling System
    subgraph Input
        ControllerHandler["ControllerHandler"]:::input
        VirtualController["VirtualController"]:::input
        TouchContext["TouchContext"]:::input
        KeyboardTranslator["KeyboardTranslator"]:::input
    end

    %% Network and Discovery
    subgraph Network
        NvHTTP["NvHTTP"]:::network
        MdnsDiscovery["MdnsDiscoveryAgent"]:::network
        PairingManager["PairingManager"]:::network
        ComputerManager["ComputerManagerService"]:::network
    end

    %% Configuration
    subgraph Config
        PreferenceConfig["PreferenceConfiguration"]:::config
        StreamSettings["StreamSettings"]:::config
    end

    %% External Systems
    subgraph External
        GameStreamServer["NVIDIA GameStream Server"]:::external
        SunshineServer["Sunshine Server"]:::external
        HostPC["Game Host PC"]:::external
    end

    %% Relationships
    PcView --> AppView
    AppView --> Game
    Game --> StreamView
    StreamView --> NvConnection
    
    NvConnection --> VideoDecoder
    NvConnection --> AudioDecoder
    NvConnection --> NvHTTP
    
    ControllerHandler --> NvConnection
    VirtualController --> ControllerHandler
    TouchContext --> ControllerHandler
    KeyboardTranslator --> ControllerHandler
    
    NvHTTP --> GameStreamServer
    NvHTTP --> SunshineServer
    
    MdnsDiscovery --> ComputerManager
    PairingManager --> NvHTTP
    ComputerManager --> PcView
    
    PreferenceConfig --> NvConnection
    StreamSettings --> NvConnection
    
    GameStreamServer --> HostPC
    SunshineServer --> HostPC

    %% Click Events
    click PcView "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/PcView.java"
    click AppView "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/AppView.java"
    click Game "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/Game.java"
    click StreamView "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/ui/StreamView.java"
    click NvConnection "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/nvstream/NvConnection.java"
    click VideoDecoder "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/binding/video/MediaCodecDecoderRenderer.java"
    click AudioDecoder "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/binding/audio/AndroidAudioRenderer.java"
    click NvHTTP "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/nvstream/http/NvHTTP.java"
    click ControllerHandler "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/binding/input/ControllerHandler.java"
    click VirtualController "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/binding/input/virtual_controller/VirtualController.java"
    click TouchContext "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/binding/input/touch/TouchContext.java"
    click KeyboardTranslator "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/binding/input/KeyboardTranslator.java"
    click MdnsDiscovery "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/nvstream/mdns/MdnsDiscoveryAgent.java"
    click PairingManager "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/nvstream/http/PairingManager.java"
    click ComputerManager "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/computers/ComputerManagerService.java"
    click PreferenceConfig "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/preferences/PreferenceConfiguration.java"
    click StreamSettings "https://github.com/satertek/moonlight-android/blob/master/app/src/main/java/com/limelight/preferences/StreamSettings.java"
```
