# theelixzammuto/moonlight-xbox architecture

> Architecture diagram of the GitHub repository theelixzammuto/moonlight-xbox, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/theelixzammuto/moonlight-xbox
- Repository: https://github.com/theelixzammuto/moonlight-xbox

## Overview

##### Main Components
- UI & Presentation Layer:
- XAML Pages: Located in the Pages directory (e.g., AppPage.xaml, HostSelectorPage.xaml, StreamPage.xaml, etc.) and additional controls like KeyboardControl.xaml.
- Application Shell: The App.xaml, which initializes the application and ties the UI with backend logic.
- Graphics and Rendering Subsystem:
- Common Components: The Common directory contains DirectXHelper, DeviceResources, and a timer module (StepTimer) that manage core DirectX functionality.
- Shaders: The Assets/Shader directory holds HLSL shader code (both vertex and pixel shaders) used for video rendering and effects.
- Streaming Engine:
- Streaming Module: The Streaming directory includes modules for decoding (FFmpegDecoder), audio (AudioPlayer), and video rendering (VideoRenderer) plus utility components such as StatsRenderer and LogRenderer.
- External Dependencies: Third-party libraries (like h264bitstream, miniaudio, and libgamestream located under libgamestream and third_party) that help with multimedia processing and protocol handling.
- Input Handling:
- Keyboard Module: The Keyboard directory, containing various keyboard layouts, dead key definitions, and source files for processing keyboard input, ensuring smooth text entry and in-game controls.
- Application State and Host Communication:
- State Management: The State directory handles the application’s state, including the stream configuration, HDMI display modes, MDNS handling for network discovery, and host/client states.
- External Tooling & Build/Deployment:
- Build Scripts & Configuration: Files like the generate-thirdparty-projects.bat, Package.appxmanifest, and various Visual Studio project files (.sln, .vcxproj) denote the build and packaging process.
- Dependency Management: VCPKG integration (vcpkg.json, bootstrap scripting, etc.) and submodule initialization for managing third-party dependencies.

##### Relationships and Data Flow
- The UI layer interacts with the Application State to trigger streaming actions (start/stop streams, select host, adjust settings).
- The Application State component maintains user and host configurations that feed into the Streaming Engine.
- The Streaming Engine uses multimedia libraries (e.g., FFmpeg in FFmpegDecoder and DirectX components) to decode and render video/audio, with shaders in the rendering pipeline applied to video frames.
- Input modules (Keyboard and possibly Gamepad input logic) propagate user interactions to both UI updates and streaming commands.
- The build and deployment pipeline (Visual Studio build scripts, VCPKG, and submodules) supports all these components by ensuring the correct libraries and dependencies are integrated.

##### Architectural Patterns & Design Principles
- Layered/MVC-like separation: The project separates presentation (XAML UI) from business logic (Application State and Streaming Engine) and low-level rendering (DirectX, shaders).
- Modularity: Each directory (e.g., Streaming, Keyboard, State) encapsulates a specific functionality which enhances maintainability.

##### Final Recommendations
- Provide as much detail as possible by breaking down the components into their submodules (for example, splitting the Streaming Engine into Audio and Video pipelines).
- Element is clearly labeled and organized logically (for example, group the shader and DirectX functionalities together).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "UI Layer"
        A["AppPage (XAML Page)"]
        B["Moonlight Welcome (XAML Page)"]
        C["Keyboard Control (XAML Control)"]
        D["Application Shell (App.xaml)"]
    end

    subgraph "Application State & Host Communication"
        Q["Application State"]
    end

    subgraph "Streaming Engine"
        I["FFmpeg Decoder"]
        J["Audio Player"]
        K["Video Renderer"]
        L["Stats Renderer"]
        M["Log Renderer"]
        N["YUVRGB Pixel Shader"]
        O["YUVRGB Vertex Shader"]
    end

    subgraph "Graphics & Rendering"
        E["Device Resources"]
        F["DirectX Helper"]
        G["Step Timer"]
        H["Shader Assets"]
    end

    subgraph "Input Handling"
        P["Keyboard Module"]
    end

    subgraph "Build & Deployment"
        R["Build Script"]
        S["App Manifest"]
        T["VS Projects"]
        U["Dependency Config"]
        V["External Libraries"]
    end

    %% Relationships
    A --> Q
    B --> Q
    C --> Q
    D --> Q
    P --> Q

    Q --> I
    Q --> J
    Q --> K
    Q --> L
    Q --> M
    Q --> N
    Q --> O

    I --> K

    K --> E
    K --> F
    K --> G
    K --> H

    H --> N
    H --> O

    V --> I
    V --> K

    R --> Q
    S --> Q
    T --> Q
    U --> Q
    R --> I
    S --> I
    T --> I
    U --> I

    %% Click Events for UI & Presentation Layer
    click A "https://github.com/theelixzammuto/moonlight-xbox/blob/master/Pages/AppPage.xaml"
    click B "https://github.com/theelixzammuto/moonlight-xbox/blob/master/MoonlightWelcome.xaml"
    click C "https://github.com/theelixzammuto/moonlight-xbox/blob/master/KeyboardControl.xaml"
    click D "https://github.com/theelixzammuto/moonlight-xbox/blob/master/App.xaml"

    %% Click Events for Graphics and Rendering Subsystem
    click E "https://github.com/theelixzammuto/moonlight-xbox/blob/master/Common/DeviceResources.h"
    click F "https://github.com/theelixzammuto/moonlight-xbox/blob/master/Common/DirectXHelper.h"
    click G "https://github.com/theelixzammuto/moonlight-xbox/blob/master/Common/StepTimer.h"
    click H "https://github.com/theelixzammuto/moonlight-xbox/tree/master/Assets/Shader"

    %% Click Events for Streaming Engine
    click I "https://github.com/theelixzammuto/moonlight-xbox/blob/master/Streaming/FFmpegDecoder.*"
    click J "https://github.com/theelixzammuto/moonlight-xbox/blob/master/Streaming/AudioPlayer.*"
    click K "https://github.com/theelixzammuto/moonlight-xbox/blob/master/Streaming/VideoRenderer.*"
    click L "https://github.com/theelixzammuto/moonlight-xbox/blob/master/Streaming/StatsRenderer.*"
    click M "https://github.com/theelixzammuto/moonlight-xbox/blob/master/Streaming/LogRenderer.*"
    click N "https://github.com/theelixzammuto/moonlight-xbox/blob/master/Streaming/YUVRGBPixelShader.hlsl"
    click O "https://github.com/theelixzammuto/moonlight-xbox/blob/master/Streaming/YUVRGBVertexShader.hlsl"

    %% Click Event for Input Handling
    click P "https://github.com/theelixzammuto/moonlight-xbox/tree/master/Keyboard"

    %% Click Event for Application State
    click Q "https://github.com/theelixzammuto/moonlight-xbox/tree/master/State"

    %% Click Events for Build & Deployment
    click R "https://github.com/theelixzammuto/moonlight-xbox/blob/master/generate-thirdparty-projects.bat"
    click S "https://github.com/theelixzammuto/moonlight-xbox/blob/master/Package.appxmanifest"
    click T "https://github.com/theelixzammuto/moonlight-xbox/blob/master/moonlight-xbox-dx.sln"
    click U "https://github.com/theelixzammuto/moonlight-xbox/blob/master/packages.config"
    click V "https://github.com/theelixzammuto/moonlight-xbox/tree/master/libgamestream"

    %% Styles
    classDef ui fill:#f9e79f,stroke:#e67e22,stroke-width:2px;
    classDef graphics fill:#aed6f1,stroke:#3498db,stroke-width:2px;
    classDef streaming fill:#d5f5e3,stroke:#28b463,stroke-width:2px;
    classDef state fill:#fadbd8,stroke:#c0392b,stroke-width:2px;
    classDef input fill:#d6eaf8,stroke:#5dade2,stroke-width:2px;
    classDef build fill:#fcf3cf,stroke:#f1c40f,stroke-width:2px;

    class A,B,C,D ui;
    class E,F,G,H graphics;
    class I,J,K,L,M,N,O streaming;
    class P input;
    class Q state;
    class R,S,T,U,V build;
```
