# limitcool/bilistream architecture

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

- Interactive diagram: https://gitdiagram.com/limitcool/bilistream
- Repository: https://github.com/limitcool/bilistream

## Overview

##### Project Type and Purpose
- bilistream is a standalone streaming relay tool built in Rust. Its purpose is to automatically “re-broadcast” live streams from platforms like Twitch and YouTube to Bilibili.
- The README and file tree emphasize Docker-based deployment, a configuration file (config.yaml), and clearly defined plugins for different platforms, underlining that the project is a command-line, container-ready tool with a plugin-based architecture.

##### File Structure
- Top-level Files: Files like Cargo.toml, Cargo.lock, and main.rs in the src directory indicate a Rust project. The presence of Dockerfile, docker-compose.yaml, and .dockerignore shows that containerization and deployment automation are important.
- Directories:
- src: contains the code.
- config.rs – handles configuration.
- main.rs – entry point.
- push.rs – likely responsible for initiating the streaming/push process.
- plugins folder – includes submodules such as live.rs, twitch.rs, youtube.rs, response.rs, and a module file (mod.rs) which provides an extensible plugin interface for supporting multiple platforms.
- This layout suggests a modular design where the core functionality is separated into submodules for configuration, streaming logic (push), and plugin handlers for each live-streaming service.

##### README for Additional Insights
- The README explains both Docker and binary deployment, showing that the system is built to run in a container environment as well as on traditional systems.
- It provides detailed instructions on Docker volume mounting (for config.yaml and cookies.txt) and environment setups (like specifying proxies), emphasizing the importance of external configuration.

a. Main Components to Include:
- Configuration Component:
- Represents the config.yaml file loaded at startup (via Docker volume or local file system).
- Shows environment variables (such as TZ, proxy settings) that influence runtime behavior.
- Core Application Modules:
- Main Module (main.rs): The entry point that initializes the application.
- Config Module (config.rs): Reads and validates configuration.
- Push Module (push.rs): Handles the stream pushing logic.
- Plugin Manager and Plugins (in src/plugins): Includes individual plugins for Twitch, YouTube, and live functionalities (like live.rs, twitch.rs, youtube.rs, and possibly response handling).
- External Dependency Components:
- Docker Container Environment: Represent the Docker container and the docker-compose orchestration.
- External Streaming Platforms: Twitch, YouTube, and Bilibili endpoints (for RTP streaming).
- External Tools: ffmpeg (and possibly yt-dlp for stream handling/ingestion) as outlined in the binary deployment instructions.
- Proxy/Network Services: If configured, a proxy server may be needed for streaming (FfmpegProxy).
- Notification Services: Gotify integration for push notifications.

b. Relationships and Interactions:
- The Main Module initializes the Plugin Manager using plugins from the src/plugins folder. Each plugin encapsulates logic for interacting with its respective streaming source.
- The Push Module works as a service that takes the processed streaming data from one of the plugins and then uses ffmpeg (or a similar process) to push the stream to Bilibili.
- Docker Deployment and Environment: Show that the entire application is containerized; with Docker Compose, volumes mount config and cookie files. Environment variables (TZ, proxies) propagate into the container.
- External APIs: Indicate API calls where applicable (e.g., YouTube API, as mentioned in the README) and dependencies like yt-dlp for additional stream extraction.

c. Architectural Patterns/Design Principles:
- Modular and Plugin-Based Architecture: Separation of concerns is clear with individual plugins for different services.
- Configuration-Driven: The use of an external YAML file for both platform-specific configurations and global settings.
- Containerization: Emphasize Docker as a deployment pattern.

d. Technologies, Frameworks, and Libraries:
- Rust as the primary programming language.
- Docker and Docker Compose for deployment.
- ffmpeg and yt-dlp as external tools used for media processing.
- Gotify for notifications, if enabled.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Deployment Layer
    subgraph "Deployment Environment"
        A1["Dockerfile"]:::container
        A2["docker-compose"]:::container
        A3["Dockerfile.full"]:::container
    end

    %% External Configuration
    B["Configuration File (config.yaml)"]:::external

    %% Core Application Layer
    subgraph "Core Application"
        C["Config Module (config.rs)"]:::config
        D["Main Module (main.rs)"]:::core
        E["Plugin Manager (plugins)"]:::core
        G["Push Module (push.rs)"]:::core
    end

    %% Plugins Layer
    subgraph "Plugins"
        F1["Twitch Plugin"]:::plugin
        F2["YouTube Plugin"]:::plugin
        F3["Live Plugin"]:::plugin
        F4["Plugin Response Handling"]:::plugin
    end

    %% External Tools and Endpoints
    H["ffmpeg/yt-dlp Tools"]:::external
    I["Bilibili Endpoint"]:::external
    J["Twitch Endpoint"]:::external
    K["YouTube API"]:::external
    L["Gotify Notification Service"]:::external

    %% Data Flow Connections
    A1 ---|"Volume Mount"| B
    A2 ---|"Volume Mount"| B
    A3 ---|"Volume Mount"| B

    B -->|"LoadConfig"| C
    C -->|"Initialize"| D
    D -->|"StartManager"| E
    E -->|"LoadPlugins"| F1
    E --> F2
    E --> F3
    E --> F4
    E -->|"SendStreamData"| G

    %% Plugin External Streams
    F1 -->|"FetchStream"| J
    F2 -->|"FetchStream"| K

    %% Stream Processing and Output
    G -->|"InvokeTools"| H
    H -->|"PushStream"| I
    G -->|"Notify"| L

    %% Styling Classes
    classDef container fill:#fff2cc,stroke:#bf9000,stroke-width:2px;
    classDef external fill:#d9ead3,stroke:#38761d,stroke-width:2px;
    classDef config fill:#f4cccc,stroke:#cc0000,stroke-width:2px;
    classDef core fill:#cfe2f3,stroke:#0b5394,stroke-width:2px;
    classDef plugin fill:#ead1dc,stroke:#b45f06,stroke-width:2px;

    %% Click Events for Mapped Components
    click C "https://github.com/limitcool/bilistream/blob/main/src/config.rs"
    click D "https://github.com/limitcool/bilistream/blob/main/src/main.rs"
    click G "https://github.com/limitcool/bilistream/blob/main/src/push.rs"
    click E "https://github.com/limitcool/bilistream/blob/main/src/plugins/mod.rs"
    click F1 "https://github.com/limitcool/bilistream/blob/main/src/plugins/twitch.rs"
    click F2 "https://github.com/limitcool/bilistream/blob/main/src/plugins/youtube.rs"
    click F3 "https://github.com/limitcool/bilistream/blob/main/src/plugins/live.rs"
    click F4 "https://github.com/limitcool/bilistream/blob/main/src/plugins/response.rs"
    click A1 "https://github.com/limitcool/bilistream/tree/main/Dockerfile"
    click A2 "https://github.com/limitcool/bilistream/blob/main/docker-compose.yaml"
    click A3 "https://github.com/limitcool/bilistream/blob/main/Dockerfile.full"
```
