# langhuihui/monibuca architecture

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

- Interactive diagram: https://gitdiagram.com/langhuihui/monibuca
- Repository: https://github.com/langhuihui/monibuca

## Overview

##### Project Type and Purpose
- Monibuca is a streaming server framework. It is designed for high-throughput, low-latency media streaming and supports various protocols (RTMP, RTSP, HTTP-FLV, WS-FLV, HLS, WebRTC, GB28181, ONVIF, SRT, etc.).
- The framework highlights modularity and extensibility through a plugin system, distributed deployment (cascade, room management), media processing (recording, transcoding, snapshot, etc.), integrated gRPC interfaces, and built-in debug and monitoring features.

##### Analysis of the File Structure
- Top-Level Components:
⁃ Root files such as server.go, publisher.go, subscriber.go, and recoder.go represent the core server components orchestrating stream flow.
⁃ The “pkg” directory contains essential libraries and utilities for media parsing (avframe, codec, track), configuration management, database connectors (SQLite, MySQL, Postgres, DuckDB), task scheduling, logging, and low-level utilities.
⁃ The “plugin” directory is extensive and broken down into multiple subdirectories corresponding to different streaming protocols and functionalities (e.g., rtmp, rtsp, hls, gb28181, onvif, webrtc, crypto, debug, logrotate, monitor, transcode, etc.). This reflects a plugin-based modular architecture.
⁃ The “pb” directory contains protobuf definitions and generated code for gRPC and REST gateways, signifying an important interface layer for remote communication and API access.
⁃ The “doc” and “doc_CN” directories include detailed architectural documentation, with files such as arch.md, arch/admin.md, arch/config.md, etc., which can provide further insights into the design decisions.

##### Architecture Components
a. Core Server Engine:
- Represented by files such as server.go, publisher.go, subscriber.go, and puller/pusher proxies.
- This engine is responsible for managing connections, data flow, and dispatching streams.
b. Plugin System:
- Plugin manager as a central module that loads and interfaces with various protocol handlers and extensions.
- Graphically separate plugins (submodules for RTMP, HLS, RTSP, GB28181, etc.) and indicate that they plug into the core via defined interfaces/APIs.
c. Communication & API Layer:
- Incorporate the gRPC/REST APIs represented in the “pb” directory.
- How external services and clients (or monitoring tools like Prometheus) interact with the server.
d. Database & Configuration:
- Components from pkg/config and pkg/db, outlining support for different backends (SQLite, MySQL, etc.).
- Configuration management that supports dynamic adjustments for streaming, resource allocation, and cluster coordination.
e. Media Processing Modules:
- Dedicated modules for transcoding, recording, screenshot, and stream alias management (files in pkg/codec, pkg/event, pkg/task, etc.) that interact with the core server.
f. Task Scheduling & Logging:

##### Relationships and Interactions
- How plugins communicate with the core through well-defined interfaces; for example, the hls, rtmp, and rtsp plugins feed processed media back into the main engine.
- Integration of a monitoring/metrics module (exposed at /api/metrics for Prometheus) and how configurations are loaded from files (using example configuration directories) and possibly updated at runtime.

##### Architectural Patterns and Key Design Aspects
- Modular and extensible design: The clear separation of plugins and core functionalities allows for “load on demand” enhancements.
- High performance and low latency: Highlight components (like lock-free structures in pkg/util and ring buffers) that ensure minimal waiting.
- Distributed deployment: Include cascade and room management (see plugin/cascade and plugin/room) as part of the design, and indicate external node interactions if schematic requires.
- Multi-protocol support: Use grouped segments for different streaming protocols and show how the API/gRPC interface supports cross-language integration.

##### Elements to be Included
- Clear labels for each major component (Core Engine, Plugin Manager, Protocol Plugins, API/GRPC Interfaces, Database/Config Management, Monitoring & Logging).
- Optionally, separate clusters or groups for protocol-specific plugins (RTMP, RTSP, HLS, etc.) and highlight their interaction with the media processing pipeline.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Layer
    EC["Streaming Clients"]:::ext

    %% Core Layer
    CSE["Core Server Engine"]:::core

    %% Plugin System Layer
    PS["Plugin System"]:::plugin
    subgraph "Protocol Plugins"
        RTP["RTMP Plugin"]:::plugin
        RSP["RTSP Plugin"]:::plugin
        HLS["HLS Plugin"]:::plugin
        GBP["GB28181 Plugin"]:::plugin
    end

    %% Communication & API Layer
    API["API Interface (gRPC/REST)"]:::api

    %% Database & Configuration Layer
    subgraph "Database & Config Module"
        CONFIG["Configuration"]:::db
        DB["Database"]:::db
    end

    %% Media Processing Layer
    MP["Media Processing Modules"]:::media

    %% Task Scheduling & Logging Layer
    subgraph "Task Scheduling & Logging"
        TS["Task Scheduler"]:::task
        LM["Log Management"]:::task
        LR["Log Rotator"]:::task
    end

    %% Data Flow Connections
    EC -->|"ingests stream"| CSE
    CSE -->|"dispatches to"| PS
    PS -->|"sends processed data"| CSE
    CSE -->|"exposes control"| API
    API -->|"commands"| CSE
    CSE -->|"loads settings"| CONFIG
    CSE -->|"persists state"| DB
    CSE -->|"triggers media processing"| MP
    MP -->|"outputs processed media"| CSE
    CSE -->|"schedules tasks"| TS
    TS -->|"executes periodic jobs"| CSE
    CSE -->|"logs events"| LM
    LM -->|"manages logs"| LR

    %% Click Events
    click CSE "https://github.com/langhuihui/monibuca/blob/v5/server.go"
    click PS "https://github.com/langhuihui/monibuca/tree/v5/plugin"
    click API "https://github.com/langhuihui/monibuca/tree/v5/pb"
    click CONFIG "https://github.com/langhuihui/monibuca/blob/v5/pkg/config/config.go"
    click DB "https://github.com/langhuihui/monibuca/blob/v5/pkg/db/mysql.go"
    click MP "https://github.com/langhuihui/monibuca/tree/v5/pkg/codec"
    click TS "https://github.com/langhuihui/monibuca/tree/v5/pkg/task"
    click LM "https://github.com/langhuihui/monibuca/blob/v5/pkg/log.go"
    click LR "https://github.com/langhuihui/monibuca/tree/v5/plugin/logrotate"

    %% Styles
    classDef core fill:#ffcccc,stroke:#aa0000,stroke-width:2px;
    classDef plugin fill:#ccffcc,stroke:#008800,stroke-width:2px;
    classDef api fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef db fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef media fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef task fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
    classDef ext fill:#daf7a6,stroke:#7fba00,stroke-width:2px;
```
