# xroier/sharprtmp architecture

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

- Interactive diagram: https://gitdiagram.com/xroier/sharprtmp
- Repository: https://github.com/xroier/sharprtmp

## Overview

##### Project Type and Purpose
- Project Type: It is a framework/library for building RTMP servers with extension points for handling streaming protocols, controller logic (for live and record streams), and integration with WebSocket for FLV streaming.
- Purpose: The core engine supports receiving video/audio streams (e.g., via ffmpeg pushing to an RTMP endpoint) and distributing them over protocols (RTMP for ffplay and WebSocket/FLV for web players like flv.js). It also provides extensibility (via Startup and DI with Autofac) so developers can register their custom controllers and services.

##### File Structure
- Top-level separation:
- SharpRtmp: Contains the core framework code including subdirectories for Buffers, Controllers (with separate folders for Living, Record, etc.), Hosting (server setup, options, startup interfaces), Networking (divided further among AMF processing, FLV handling, RTMP messaging, and WebSocket sessions), Rpc, and Service layers.
- SharpRtmp.Example: A demonstration application that shows how to configure and run the server (using Program.cs and Startup.cs).
- SharpRtmp.UnitTests: Contains tests for AMF readers/writers, buffer management, etc.
- docs and samples: Provide API documentation and sample media files for testing AMF serialization.
- Architectural Clues:
- There’s an obvious layering: Hosting (server bootstrapping), Networking (protocol management and message processing), Controllers (handling application logic for streams), and Services (e.g., record and publisher session management).
- Use of Dependency Injection (via Autofac) in Startup and an explicit chainable RtmpServerBuilder indicating a pluggable and modular architecture.

##### Elements to Include (for clarity and granularity)
- Use clear labels for each module/component such as “Hosting (RtmpServer, RtmpServerBuilder, Options)”, “Networking – RTMP (Handshake, Message Streams, ChunkStream)”, “Networking – AMF Serialization (AMF0, AMF3)” and so on.
- Optionally, include annotations that capture design patterns like the builder pattern, the layered architecture, and the use of dependency injection.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Initialization/Configuration Layer
    subgraph "Initialization/Configuration Layer"
        A["Program.cs"]:::init
        B["Startup.cs"]:::init
    end

    %% Hosting Module
    subgraph "Hosting Module"
        C["RtmpServerBuilder.cs"]:::hosting
        D["RtmpServer.cs"]:::hosting
        E["RtmpServerOptions.cs"]:::hosting
        F["WebSocketOptions.cs"]:::hosting
    end

    %% Networking Layer
    subgraph "Networking Layer"
        %% RTMP Networking
        subgraph "RTMP Networking"
            G["RtmpSession.cs"]:::networking
            H["RtmpMessageStream.cs"]:::networking
            I["ChunkStreamContext.cs"]:::networking
        end
        %% AMF Serialization
        subgraph "AMF Serialization"
            subgraph "AMF0"
                J["Amf0Reader.cs"]:::networking
                K["Amf0Writer.cs"]:::networking
                L["SerializationContext.cs (AMF0)"]:::networking
            end
            subgraph "AMF3"
                M["Amf3Reader.cs"]:::networking
                N["Amf3Writer.cs"]:::networking
                O["SerializationContext.cs (AMF3)"]:::networking
            end
        end
        %% FLV Handling
        subgraph "FLV Handling"
            P["FlvDemuxer.cs"]:::networking
            Q["FlvMuxer.cs"]:::networking
        end
        %% WebSocket
        subgraph "WebSocket"
            R["WebSocketSession.cs"]:::networking
        end
    end

    %% Controller Layer
    subgraph "Controller Layer"
        S["LivingController.cs"]:::controller
        T["LivingStream.cs"]:::controller
        U["RecordController.cs"]:::controller
        V["RecordStream.cs"]:::controller
        W["RtmpController.cs"]:::controller
        X["WebSocketController.cs"]:::controller
        Y["WebSocketPlayController.cs"]:::controller
    end

    %% Services Layer
    subgraph "Services Layer"
        Z["PublisherSessionService.cs"]:::services
        AA["RecordService.cs"]:::services
        AB["RecordServiceConfiguration.cs"]:::services
    end

    %% Utility Modules
    subgraph "Utility Modules"
        AC["ByteBuffer.cs"]:::utilities
        AD["NetworkBitConverter.cs"]:::utilities
        AE["StreamHelper.cs"]:::utilities
    end

    %% External Clients
    AF["ffmpeg(RTMP_Source)"]:::external
    AG["ffplay(RTMP_Consumer)"]:::external
    AH["WebBrowser(flv.js)"]:::external

    %% Connections: Initialization -> Hosting
    A --> B
    B --> C

    %% Hosting connections
    C --> D
    C --> E
    C --> F

    %% Hosting -> Networking
    D --> G
    D --> R

    %% Networking RTMP details and external streaming
    G --> H
    H --> I
    AF --> G
    G --> AG

    %% Networking -> AMF Serialization (processing RTMP messages)
    G --> J
    G --> M

    %% FLV Handling chain
    I --> P
    P --> Q
    Q --> R

    %% Networking WebSocket -> External client (web browser)
    R --> AH

    %% Controllers: invoked from Networking RTMP and WebSocket
    G --> W
    G --> S
    G --> U
    R --> X
    R --> Y

    %% Controllers -> Services
    S --> Z
    U --> AA
    W --> AA
    V --> AB

    %% Utilities used in Networking
    AC --> G
    AD --> G
    AE --> G

    %% Styles
    classDef init fill:#f9e6ff,stroke:#8B008B,stroke-width:2px;
    classDef hosting fill:#ffebcd,stroke:#CD853F,stroke-width:2px;
    classDef networking fill:#e0ffff,stroke:#008B8B,stroke-width:2px;
    classDef controller fill:#e6e6fa,stroke:#6A5ACD,stroke-width:2px;
    classDef services fill:#ffe4e1,stroke:#FF69B4,stroke-width:2px;
    classDef utilities fill:#ffffe0,stroke:#DAA520,stroke-width:2px;
    classDef external fill:#f5f5dc,stroke:#A0522D,stroke-width:2px;

    %% Click Events
    click A "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp.Example/Program.cs"
    click B "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp.Example/Startup.cs"
    click C "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Hosting/RtmpServerBuilder.cs"
    click D "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Hosting/RtmpServer.cs"
    click E "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Hosting/RtmpServerOptions.cs"
    click F "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Hosting/WebSocketOptions.cs"

    click G "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Networking/Rtmp/RtmpSession.cs"
    click H "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Networking/Rtmp/RtmpMessageStream.cs"
    click I "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Networking/Rtmp/ChunkStreamContext.cs"

    click J "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Networking/Amf/Serialization/Amf0/Amf0Reader.cs"
    click K "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Networking/Amf/Serialization/Amf0/Amf0Writer.cs"
    click L "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Networking/Amf/Serialization/Amf0/SerializationContext.cs"
    click M "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Networking/Amf/Serialization/Amf3/Amf3Reader.cs"
    click N "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Networking/Amf/Serialization/Amf3/Amf3Writer.cs"
    click O "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Networking/Amf/Serialization/Amf3/SerializationContext.cs"

    click P "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Networking/Flv/FlvDemuxer.cs"
    click Q "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Networking/Flv/FlvMuxer.cs"

    click R "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Networking/WebSocket/WebSocketSession.cs"

    click S "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Controllers/Living/LivingController.cs"
    click T "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Controllers/Living/LivingStream.cs"
    click U "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Controllers/Record/RecordController.cs"
    click V "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Controllers/Record/RecordStream.cs"
    click W "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Controllers/RtmpController.cs"
    click X "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Controllers/WebSocketController.cs"
    click Y "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Controllers/WebSocketPlayController.cs"

    click Z "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Service/PublisherSessionService.cs"
    click AA "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Service/RecordService.cs"
    click AB "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Service/RecordServiceConfiguration.cs"

    click AC "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Buffers/ByteBuffer.cs"
    click AD "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Networking/Utils/NetworkBitConverter.cs"
    click AE "https://github.com/xroier/sharprtmp/blob/main/SharpRtmp/Networking/Utils/StreamHelper.cs"
```
