# taojhlwkl/zlmediakit architecture

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

- Interactive diagram: https://gitdiagram.com/taojhlwkl/zlmediakit
- Repository: https://github.com/taojhlwkl/zlmediakit

## Overview

##### Project type and purpose
- ZLMediaKit is a cross-platform, high-performance C++11-based media streaming framework. It acts both as
- A server (RTSP/RTMP/HLS/HTTP-FLV/WebSocket-FLV) supporting protocol conversion and media distribution
- An SDK/C API library for embedding into applications (Android, iOS, Linux, Windows, macOS)
- It provides:
- Core media modules (codec, RTP/RTMP/RTSP/HLS pipelines, HTTP/WebSocket I/O, recording, playback, pushing)
- A C API wrapper (under api/) to expose functionality to other languages
- Example server app (under server/) and Android demo (under Android/)

##### Directory-level architecture
- 3rdpart/
- ZLToolKit – asynchronous network IO, event loop, thread pool
- jsoncpp – JSON parser for config and API
- src/ – core C++ modules arranged by feature area:
- Codec (AAC/H264/H265)
- Common utilities (config, media source/sink, parser, time stamping)
- Http (client, server, WebSocket)
- Player, Pusher, Record, Rtmp, Rtp, Rtsp, Shell interfaces
- Extension – protocol adapters, frame factories
- api/ – C wrapper: include/ (headers), source/ (implementation), tests/
- server/ – sample MediaServer executable using core modules
- Android/ – Android Studio project demonstrating JNI bindings to native library
- cmake/, build scripts, Dockerfiles – build and deployment support
- conf/ – configuration file templates

##### Key architectural patterns
- Modular pipeline: each protocol (RTSP, RTMP, HLS, HTTP-FLV, WebSocket-FLV) is implemented as a session/sessionMuxer layer that plugs into the event loop
- Asynchronous network IO with epoll (or equivalent) + thread-pool model provided by ZLToolKit
- Separation of concerns:
- Core media logic (decoding, encoding, muxing, demuxing)
- Network transport (TCP/UDP/HTTP/WebSocket)
- API boundary (C API wrappers, JNI for Android)
- Plugin-like extension points for new codecs, protocols, or transports

##### Main components and interactions
- ZLToolKit (Network & Event Loop)
- Provides: TCP/UDP servers, client connectors, epoll, thread pool
- Depended on by all session modules
- Core Media Modules
- Codec (H264/H265/AAC)
- Parser, Muxer, Demuxer, Recorder, Recorder Imp
- MediaSource/MediaSink abstractions
- Protocol Sessions
- RTSPSession, RTMPSession, HttpSession, WebSocketSession
- RTP/RTMP/RTSP muxers and demuxers
- HLS file server and segmenter
- C API Layer (api/)
- Wraps core modules into C-callable functions
- Used by external applications or bindings
- Sample Server App (server/)
- Instantiates TcpServer on configured ports
- Registers session classes (RtspSession, RtmpSession, HttpSession, HttpsSession)
- Android Demo (Android/)
- JNI layer (ZLMediaKit.java) calls into C API library compiled for multiple ABIs
- Presents UI and calls native-lib.cpp to start player/pusher

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Networking Layer
    subgraph "Networking Layer" 
        direction TB
        ZLToolKit["ZLToolKit\n(Network & Event Loop)"]:::network
        JSONParser["JSON Parser"]:::network
    end

    %% Protocol Sessions Layer
    subgraph "Protocol Sessions" 
        direction TB
        RTPSession["RTP Session"]:::session
        RTMPSession["RTMP Session"]:::session
        RTSPSession["RTSP Session"]:::session
        HttpSession["HTTP-FLV Session"]:::session
        WebSocketSession["WebSocket-FLV Session"]:::session
        ShellSession["Shell CLI Session"]:::session
    end

    %% Core Media Logic Layer
    subgraph "Core Media Logic" 
        direction TB
        CodecModules["Codec Modules\n(H264/H265/AAC)"]:::core
        CommonUtils["Common Utilities"]:::core
        HttpWSIO["HTTP & WebSocket I/O"]:::core
        Player["MediaPlayer"]:::core
        Pusher["MediaPusher"]:::core
        RecordingMuxing["Recording & Muxing"]:::core
        Extension["Extensions & Plugins"]:::core
    end

    %% API & JNI Layer
    subgraph "API & JNI Boundary" 
        direction TB
        CAPI["C API Layer"]:::api
        CAPITests["C API Tests"]:::api
        JNI["JNI Layer"]:::api
    end

    %% Applications Layer
    subgraph "Applications" 
        direction TB
        MediaServer["Sample MediaServer App"]:::app
        AndroidDemo["Android Demo"]:::app
    end

    %% Dependencies and Data Flow
    ZLToolKit -->|I/O, events| RTPSession
    ZLToolKit -->|I/O, events| RTMPSession
    ZLToolKit -->|I/O, events| RTSPSession
    ZLToolKit -->|I/O, events| HttpSession
    ZLToolKit -->|I/O, events| WebSocketSession
    ZLToolKit -->|I/O, events| ShellSession

    RTPSession <-->|"media frames, control"| CodecModules
    RTMPSession <-->|"media frames, control"| CodecModules
    RTSPSession <-->|"media frames, control"| CodecModules
    HttpSession <-->|"media frames, control"| HttpWSIO
    WebSocketSession <-->|"media frames, control"| HttpWSIO
    ShellSession <-->|"control commands"| CommonUtils

    CodecModules -->|"parsed data"| CommonUtils
    HttpWSIO --> CommonUtils
    CommonUtils --> RecordingMuxing
    RecordingMuxing -->|"HLS segments, files"| conf["File System\n(conf/config.ini)"]:::app

    %% API interactions
    CAPI -->|"calls, callbacks"| CodecModules
    CAPI --> CommonUtils
    JNI -->|"JNI calls"| CAPI

    %% Applications use layers
    MediaServer -->|register sessions| RTSPSession
    MediaServer -->|register sessions| RTMPSession
    MediaServer -->|register sessions| HttpSession
    MediaServer -->|register sessions| WebSocketSession

    AndroidDemo -->|UI calls| JNI

    %% Click Events
    click ZLToolKit "https://github.com/taojhlwkl/zlmediakit/tree/master/3rdpart/ZLToolKit"
    click JSONParser "https://github.com/taojhlwkl/zlmediakit/tree/master/3rdpart/jsoncpp"
    click CodecModules "https://github.com/taojhlwkl/zlmediakit/tree/master/src/Codec"
    click CommonUtils "https://github.com/taojhlwkl/zlmediakit/tree/master/src/Common"
    click HttpWSIO "https://github.com/taojhlwkl/zlmediakit/tree/master/src/Http"
    click Player "https://github.com/taojhlwkl/zlmediakit/tree/master/src/Player"
    click Pusher "https://github.com/taojhlwkl/zlmediakit/tree/master/src/Pusher"
    click RecordingMuxing "https://github.com/taojhlwkl/zlmediakit/tree/master/src/Record"
    click RTMPSession "https://github.com/taojhlwkl/zlmediakit/tree/master/src/Rtmp"
    click RTPSession "https://github.com/taojhlwkl/zlmediakit/tree/master/src/Rtp"
    click RTSPSession "https://github.com/taojhlwkl/zlmediakit/tree/master/src/Rtsp"
    click ShellSession "https://github.com/taojhlwkl/zlmediakit/tree/master/src/Shell"
    click Extension "https://github.com/taojhlwkl/zlmediakit/tree/master/src/Extension"
    click CAPI "https://github.com/taojhlwkl/zlmediakit/tree/master/api/include"
    click CAPITests "https://github.com/taojhlwkl/zlmediakit/tree/master/api/tests"
    click MediaServer "https://github.com/taojhlwkl/zlmediakit/tree/master/server"
    click AndroidDemo "https://github.com/taojhlwkl/zlmediakit/tree/master/Android"
    click conf "https://github.com/taojhlwkl/zlmediakit/blob/master/conf/config.ini"

    %% Styles
    classDef network fill:#D0E6FF,stroke:#0B69A3,stroke-width:2px;
    classDef core fill:#DFFFD0,stroke:#4E8C00,stroke-width:2px;
    classDef session fill:#FFE3B8,stroke:#D27F00,stroke-width:2px;
    classDef api fill:#E8D0FF,stroke:#6A0DAD,stroke-width:2px;
    classDef app fill:#F0F0F0,stroke:#555555,stroke-width:2px;
```
