libmedia_transfer_protocol 架构文档

媒体协议层 RTMP/RTP/RTSP/HLS/RTCP/HTTP-FLV/WebRTC/GB28181

libmedia_transfer_protocol 架构文档

📖 概述

本文档详细描述 libmedia_transfer_protocol 库的整体架构设计、模块划分、数据流和协议栈。

🏗️ 整体架构

系统分层架构

┌─────────────────────────────────────────────────────────────┐
│                    应用层 (Application Layer)                │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐  ┌──────────┐   │
│  │ RTSP     │  │ HTTP-FLV │  │ HLS      │  │ GB28181  │   │
│  │ Server   │  │ Server   │  │ Muxer    │  │ Server   │   │
│  └────┬─────┘  └────┬─────┘  └────┬─────┘  └────┬─────┘   │
└───────┼──────────────┼──────────────┼──────────────┼────────┘
        │              │              │              │
┌───────┼──────────────┼──────────────┼──────────────┼────────┐
│       │              │              │              │        │
│ ┌─────▼─────┐  ┌─────▼─────┐  ┌─────▼─────┐  ┌─────▼─────┐│
│ │  RTP/RTCP │  │  HTTP     │  │  MPEG-TS  │  │  SIP      ││
│ │  Protocol │  │  Protocol │  │  Protocol │  │  Protocol ││
│ └─────┬─────┘  └─────┬─────┘  └─────┬─────┘  └─────┬─────┘│
│       │              │              │              │        │
│ ┌─────▼─────────────────────────────────────────────▼─────┐│
│ │             网络层 (Network Layer)                       ││
│ │  ┌──────────┐  ┌──────────┐  ┌──────────┐             ││
│ │  │ TCP      │  │ UDP      │  │ Connection│            ││
│ │  │ Server   │  │ Server   │  │ Manager   │            ││
│ │  └──────────┘  └──────────┘  └──────────┘             ││
│ └─────────────────────────────────────────────────────────┘│
│                                                             │
│ ┌─────────────────────────────────────────────────────────┐│
│ │        拥塞控制和码率估计 (QoS Control)                  ││
│ │  ┌──────────┐  ┌──────────┐  ┌──────────┐             ││
│ │  │Congestion│  │ Pacing   │  │Bitrate   │             ││
│ │  │Control   │  │ Control  │  │Estimator │             ││
│ │  └──────────┘  └──────────┘  └──────────┘             ││
│ └─────────────────────────────────────────────────────────┘│
└─────────────────────────────────────────────────────────────┘

📦 核心模块架构

1. 网络层架构 (libnetwork)

┌─────────────────────────────────────────────────────────┐
│                 网络层架构 (libnetwork)                   │
├─────────────────────────────────────────────────────────┤
│                                                          │
│  ┌──────────────┐         ┌──────────────┐            │
│  │  TcpServer   │────────▶│ TcpSession   │            │
│  │              │         │              │            │
│  │ - Listen     │         │ - OnRead     │            │
│  │ - Accept     │         │ - OnWrite    │            │
│  │ - Close      │         │ - Send       │            │
│  └──────┬───────┘         └──────┬───────┘            │
│         │                        │                     │
│         │                        ▼                     │
│         │              ┌──────────────┐               │
│         └──────────────▶│ Connection   │               │
│                        │              │               │
│                        │ - SendQueue  │               │
│                        │ - Buffer     │               │
│                        │ - State      │               │
│                        └──────────────┘               │
│                                                          │
│  ┌──────────────┐                                      │
│  │  UdpServer   │                                      │
│  │              │                                      │
│  │ - Bind       │                                      │
│  │ - Recv       │                                      │
│  │ - Send       │                                      │
│  └──────────────┘                                      │
│                                                          │
└─────────────────────────────────────────────────────────┘

2. HTTP 协议架构 (libhttp)

┌─────────────────────────────────────────────────────────┐
│                 HTTP 协议架构 (libhttp)                   │
├─────────────────────────────────────────────────────────┤
│                                                          │
│  ┌──────────────┐         ┌──────────────┐            │
│  │ HttpServer   │────────▶│ HttpSession  │            │
│  │              │         │              │            │
│  │ - Start      │         │ - OnRequest  │            │
│  │ - Stop       │         │ - SendResponse│           │
│  │ - Register   │         │ - ParseRequest│           │
│  └──────┬───────┘         └──────┬───────┘            │
│         │                        │                     │
│         │                        ▼                     │
│         │              ┌──────────────┐               │
│         └──────────────▶│ HttpRequest  │               │
│                        │              │               │
│                        │ - Method     │               │
│                        │ - Path       │               │
│                        │ - Headers    │               │
│                        │ - Body       │               │
│                        │ - GetParam   │               │
│                        └──────────────┘               │
│                                                          │
└─────────────────────────────────────────────────────────┘

3. RTSP 协议架构 (librtsp)

┌─────────────────────────────────────────────────────────┐
│                 RTSP 协议架构 (librtsp)                   │
├─────────────────────────────────────────────────────────┤
│                                                          │
│  客户端                          服务器                   │
│  ┌──────┐                      ┌──────┐                │
│  │Client│────────OPTIONS───────▶│Server│                │
│  │      │◄──────200 OK─────────│      │                │
│  │      │                      │      │                │
│  │      │────────DESCRIBE──────▶│      │                │
│  │      │◄──────200 OK─────────│      │                │
│  │      │   (SDP)              │      │                │
│  │      │                      │      │                │
│  │      │────────SETUP─────────▶│Session│               │
│  │      │◄──────200 OK─────────│      │                │
│  │      │   (Session ID)       │      │                │
│  │      │                      │      │                │
│  │      │────────PLAY──────────▶│      │                │
│  │      │◄──────200 OK─────────│      │                │
│  │      │                      │      │                │
│  │      │◄──────RTP Data───────│      │                │
│  │      │◄──────RTCP Feedback──│      │                │
│  │      │                      │      │                │
│  │      │────────TEARDOWN──────▶│      │                │
│  │      │◄──────200 OK─────────│      │                │
│  └──────┘                      └──────┘                │
│                                                          │
└─────────────────────────────────────────────────────────┘

RTSP 数据流:

RTSP Request/Response (TCP Port 554)
    │
    ▼
RTP Media Stream (UDP/TCP)
    │
    ├── Video RTP Packets
    │   └── H.264/H.265 NALU
    │
    └── Audio RTP Packets
        └── AAC/MP3/G.711

RTCP Control Packets (UDP/TCP)
    │
    ├── SR (Sender Report)
    ├── RR (Receiver Report)
    └── NACK (Negative Acknowledgment)

4. RTP/RTCP 架构 (librtp/librtcp)

┌─────────────────────────────────────────────────────────┐
│              RTP/RTCP 架构 (librtp/librtcp)              │
├─────────────────────────────────────────────────────────┤
│                                                          │
│  发送端 (Sender)                                        │
│  ┌──────────────────────────────────────────┐          │
│  │  RTP Packetizer                          │          │
│  │  ┌──────────┐  ┌──────────┐            │          │
│  │  │ Video    │  │ Audio    │            │          │
│  │  │Packetizer│  │Packetizer│            │          │
│  │  └────┬─────┘  └────┬─────┘            │          │
│  │       │             │                   │          │
│  │       └──────┬──────┘                   │          │
│  │              ▼                          │          │
│  │        RTP Packet                       │          │
│  │        - Header                         │          │
│  │        - Payload                        │          │
│  └──────────────┬──────────────────────────┘          │
│                 │                                       │
│                 ▼                                       │
│          Network (UDP/TCP)                             │
│                 │                                       │
│                 ▼                                       │
│  接收端 (Receiver)                                      │
│  ┌──────────────────────────────────────────┐          │
│  │  RTP Depacketizer                        │          │
│  │  ┌──────────┐  ┌──────────┐            │          │
│  │  │Frame     │  │Video     │            │          │
│  │  │Assembler │  │Frame     │            │          │
│  │  └────┬─────┘  └────┬─────┘            │          │
│  │       │             │                   │          │
│  │       └──────┬──────┘                   │          │
│  │              ▼                          │          │
│  │        Media Frame                      │          │
│  │        (H.264/H.265/AAC)                │          │
│  └──────────────────────────────────────────┘          │
│                                                          │
│  ┌──────────────────────────────────────────┐          │
│  │  RTCP Feedback                          │          │
│  │  ┌──────────┐  ┌──────────┐            │          │
│  │  │ SR/RR    │  │ NACK     │            │          │
│  │  │ (Quality)│  │ (Loss)   │            │          │
│  │  └──────────┘  └──────────┘            │          │
│  └──────────────────────────────────────────┘          │
│                                                          │
└─────────────────────────────────────────────────────────┘

RTP 包结构:

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |V=2|P|X|  CC   |M|     PT      |       sequence number         |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                           timestamp                           |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |           synchronization source (SSRC) identifier            |
   +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
   |            contributing source (CSRC) identifiers             |
   |                             ....                              |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                            Payload                            |
   |                             ....                              |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

5. HLS 架构 (libhls)

┌─────────────────────────────────────────────────────────┐
│                  HLS 架构 (libhls)                       │
├─────────────────────────────────────────────────────────┤
│                                                          │
│  输入流 (Input Stream)                                   │
│  ┌──────────────────────────────────────────┐          │
│  │  Media Packets (Video/Audio)             │          │
│  └──────────────┬───────────────────────────┘          │
│                 │                                       │
│                 ▼                                       │
│  ┌──────────────────────────────────────────┐          │
│  │  HLSMuxer                                 │          │
│  │  ┌──────────────────────────────┐       │          │
│  │  │ Parse Codec                  │       │          │
│  │  │ - H.264/H.265                │       │          │
│  │  │ - AAC/MP3                    │       │          │
│  │  └──────────┬───────────────────┘       │          │
│  │             │                            │          │
│  │             ▼                            │          │
│  │  ┌──────────────────────────────┐       │          │
│  │  │ Fragment Creation            │       │          │
│  │  │ - On Keyframe                │       │          │
│  │  │ - Min/Max Duration           │       │          │
│  │  └──────────┬───────────────────┘       │          │
│  │             │                            │          │
│  │             ▼                            │          │
│  │  ┌──────────────────────────────┐       │          │
│  │  │ TS Encoder                   │       │          │
│  │  │ - PAT/PMT                    │       │          │
│  │  │ - PES                        │       │          │
│  │  └──────────┬───────────────────┘       │          │
│  │             │                            │          │
│  │             ▼                            │          │
│  │  ┌──────────────────────────────┐       │          │
│  │  │ FragmentWindow               │       │          │
│  │  │ - Add Fragment               │       │          │
│  │  │ - Shrink Window              │       │          │
│  │  │ - Generate M3U8              │       │          │
│  │  └──────────┬───────────────────┘       │          │
│  └─────────────┼────────────────────────────┘          │
│                 │                                       │
│                 ├──────────────┐                       │
│                 ▼              ▼                       │
│         ┌──────────┐    ┌──────────┐                  │
│         │ M3U8     │    │ TS       │                  │
│         │ Playlist │    │ Fragments│                  │
│         └──────────┘    └──────────┘                  │
│                                                          │
└─────────────────────────────────────────────────────────┘

HLS 工作流程:

Media Stream
    │
    ▼
[Fragment Creation]
    │
    │  ┌─────────────┐
    ├─▶│ Fragment 0  │ (segment_0.ts)
    │  └─────────────┘
    │  ┌─────────────┐
    ├─▶│ Fragment 1  │ (segment_1.ts)
    │  └─────────────┘
    │  ┌─────────────┐
    ├─▶│ Fragment 2  │ (segment_2.ts)
    │  └─────────────┘
    │  ┌─────────────┐
    ├─▶│ Fragment N  │ (segment_N.ts)
    │  └─────────────┘
    │
    ▼
[Sliding Window]
    │
    │  Window Size = 3
    │  ┌──────┐ ┌──────┐ ┌──────┐
    │  │ Seg0 │ │ Seg1 │ │ Seg2 │
    │  └──────┘ └──────┘ └──────┘
    │    Oldest              Newest
    │
    ▼
[M3U8 Playlist]
    │
    └─▶ #EXTM3U
        #EXT-X-VERSION:3
        #EXT-X-TARGETDURATION:10
        #EXTINF:10.0,
        segment_0.ts
        #EXTINF:10.0,
        segment_1.ts
        #EXTINF:10.0,
        segment_2.ts
        #EXT-X-ENDLIST (for VOD)

6. FLV 架构 (libflv)

FlvEncoder
    │
    ├── Audio Tag
    │   ├── SoundFormat
    │   └── Audio Data
    │
    └── PreviousTagSize
│
▼ HTTP Server (Port 8080)
│
▼ HTTP-FLV Client ```

7. MPEG-TS 架构 (libmpeg)

┌─────────────────────────────────────────────────────────┐
│              MPEG-TS 架构 (libmpeg)                      │
├─────────────────────────────────────────────────────────┤
│                                                          │
│  输入媒体流 (Input Media Stream)                          │
│  ┌──────────────────────────────────────────┐          │
│  │  Video: H.264/H.265 NALU                 │          │
│  │  Audio: AAC/MP3/G.711                    │          │
│  └──────────────┬───────────────────────────┘          │
│                 │                                       │
│                 ▼                                       │
│  ┌──────────────────────────────────────────┐          │
│  │  PSI Table Generator                     │          │
│  │  ┌──────────┐  ┌──────────┐            │          │
│  │  │ PAT      │  │ PMT      │            │          │
│  │  │ Writer   │  │ Writer   │            │          │
│  │  └────┬─────┘  └────┬─────┘            │          │
│  │       │             │                   │          │
│  │       └──────┬──────┘                   │          │
│  │              ▼                          │          │
│  │        PSI Section                      │          │
│  │        (PAT/PMT)                        │          │
│  └──────────────┬──────────────────────────┘          │
│                 │                                       │
│                 ▼                                       │
│  ┌──────────────────────────────────────────┐          │
│  │  PES Encoder                             │          │
│  │  ┌──────────┐  ┌──────────┐            │          │
│  │  │ Video    │  │ Audio    │            │          │
│  │  │ Encoder  │  │ Encoder  │            │          │
│  │  └────┬─────┘  └────┬─────┘            │          │
│  │       │             │                   │          │
│  │       ├── PTS/DTS   ├── PTS            │          │
│  │       │             │                   │          │
│  │       └──────┬──────┘                   │          │
│  │              ▼                          │          │
│  │        PES Packet                       │          │
│  │        - Header                         │          │
│  │        - Payload                        │          │
│  └──────────────┬──────────────────────────┘          │
│                 │                                       │
│                 ▼                                       │
│  ┌──────────────────────────────────────────┐          │
│  │  TS Packetizer                           │          │
│  │  ┌──────────────────────────────┐       │          │
│  │  │ Fragment PES to TS           │       │          │
│  │  │ - 188 bytes per packet       │       │          │
│  │  │ - Continuity Counter         │       │          │
│  │  │ - Adaptation Field           │       │          │
│  │  └──────────┬───────────────────┘       │          │
│  │             │                            │          │
│  │             ▼                            │          │
│  │  ┌──────────────────────────────┐       │          │
│  │  │ TS Packet (188 bytes)        │       │          │
│  │  │ ┌──────────┬──────────────┐ │       │          │
│  │  │ │ Header   │ Payload/    │ │       │          │
│  │  │ │ (4 bytes)│ Adaptation  │ │       │          │
│  │  │ │          │ Field       │ │       │          │
│  │  │ └──────────┴──────────────┘ │       │          │
│  │  └──────────────────────────────┘       │          │
│  └──────────────┬──────────────────────────┘          │
│                 │                                       │
│                 ▼                                       │
│          TS Stream                                     │
│                                                          │
└─────────────────────────────────────────────────────────┘

MPEG-TS 数据结构:

TS Packet Structure (188 bytes):
    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   | sync_byte |TE|P |  Priority  |        PID (13 bits)          |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |TSC |AFC |   Continuity Counter (4 bits)                       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Adaptation Field (optional)                |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                         Payload (184 bytes)                   |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

PSI Section Structure (PAT/PMT):
    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   | table_id | section_syntax_indicator | reserved | section_length|
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   | transport_stream_id                                            |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   | reserved | version | current_next | section_number | last_sec  |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Section Data                                |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                            CRC_32                              |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

PES Packet Structure:
    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   | packet_start_code_prefix (0x000001)                           |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   | stream_id      | PES_packet_length                            |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   | PES_scrambling | PES_priority | data_alignment | copyright    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   | PTS_DTS_flags  | ESCR_flag | ES_rate_flag | ...               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   | PES_header_data_length                                        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   | PTS (33 bits, optional)                                       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   | DTS (33 bits, optional)                                       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                         Payload Data                           |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

8. GB28181 架构 (libgb28181)

┌─────────────────────────────────────────────────────────┐
│              GB28181 架构 (libgb28181)                   │
├─────────────────────────────────────────────────────────┤
│                                                          │
│  设备端 (Device)               平台端 (Platform)          │
│  ┌──────────────┐              ┌──────────────┐          │
│  │ SIP Client   │◄────────────►│ SIP Server   │          │
│  │              │   SIP Signal │              │          │
│  └──────┬───────┘   (UDP/TCP)  └──────┬───────┘          │
│         │                              │                  │
│         │                              │                  │
│         │                              │                  │
│         │   1. REGISTER                │                  │
│         │      ────────────────────────►                  │
│         │                              │                  │
│         │   2. 200 OK                  │                  │
│         │◄─────────────────────────────                  │
│         │                              │                  │
│         │   3. MESSAGE (Catalog)       │                  │
│         │      ────────────────────────►                  │
│         │                              │                  │
│         │   4. 200 OK                  │                  │
│         │◄─────────────────────────────                  │
│         │                              │                  │
│         │   5. INVITE (Play)           │                  │
│         │      ────────────────────────►                  │
│         │                              │                  │
│         │   6. 200 OK (SDP)            │                  │
│         │◄─────────────────────────────                  │
│         │                              │                  │
│         │   7. ACK                     │                  │
│         │      ────────────────────────►                  │
│         │                              │                  │
│         └──────────┬───────────────────┘                  │
│                    │                                      │
│                    ▼                                      │
│         ┌─────────────────────┐                          │
│         │ RTP Media Stream    │                          │
│         │ (TCP/UDP)           │                          │
│         │                     │                          │
│         │ - Video RTP         │                          │
│         │ - Audio RTP         │                          │
│         │ - RTCP Feedback     │                          │
│         └─────────────────────┘                          │
│                                                          │
└─────────────────────────────────────────────────────────┘

GB28181 消息流程:

1. 设备注册 (Device Registration)
   设备 ──REGISTER──► 平台
   设备 ◄─200 OK──── 平台

2. 目录查询 (Catalog Query)
   平台 ──MESSAGE (Catalog)──► 设备
   平台 ◄─200 OK (XML)─────── 设备

3. 视频点播 (Video Play)
   平台 ──INVITE (Play)──► 设备
   平台 ◄─200 OK (SDP)──── 设备
   平台 ──ACK────────────► 设备
   
   [RTP 媒体传输]
   
   平台 ──BYE────────────► 设备
   平台 ◄─200 OK────────── 设备

9. 拥塞控制架构 (congestion_controller)

┌─────────────────────────────────────────────────────────┐
│          拥塞控制架构 (congestion_controller)             │
├─────────────────────────────────────────────────────────┤
│                                                          │
│  ┌──────────────────────────────────────────┐          │
│  │  Network Monitor                         │          │
│  │  ┌──────────┐  ┌──────────┐            │          │
│  │  │ Packet   │  │ Delay    │            │          │
│  │  │ Loss     │  │ Gradient │            │          │
│  │  │ Detector │  │ Detector │            │          │
│  │  └────┬─────┘  └────┬─────┘            │          │
│  │       │             │                   │          │
│  │       └──────┬──────┘                   │          │
│  │              ▼                          │          │
│  │        Network State                    │          │
│  │        - Loss Rate                      │          │
│  │        - RTT                            │          │
│  │        - Throughput                     │          │
│  └──────────────┬──────────────────────────┘          │
│                 │                                       │
│                 ▼                                       │
│  ┌──────────────────────────────────────────┐          │
│  │  Congestion Control Algorithm            │          │
│  │  ┌──────────┐  ┌──────────┐            │          │
│  │  │ GCC      │  │ BBR      │            │          │
│  │  │ (Google) │  │ (Google) │            │          │
│  │  └────┬─────┘  └────┬─────┘            │          │
│  │       │             │                   │          │
│  │       └──────┬──────┘                   │          │
│  │              ▼                          │          │
│  │        Target Bitrate                   │          │
│  │        - Min Bitrate                    │          │
│  │        - Max Bitrate                    │          │
│  │        - Current Bitrate                │          │
│  └──────────────┬──────────────────────────┘          │
│                 │                                       │
│                 ▼                                       │
│  ┌──────────────────────────────────────────┐          │
│  │  Bitrate Controller                      │          │
│  │  ┌──────────┐  ┌──────────┐            │          │
│  │  │ Encoder  │  │ Pacing   │            │          │
│  │  │ Bitrate  │  │ Control  │            │          │
│  │  │ Adjust   │  │          │            │          │
│  │  └──────────┘  └──────────┘            │          │
│  └──────────────────────────────────────────┘          │
│                                                          │
└─────────────────────────────────────────────────────────┘

10. 数据包调度架构 (pacing)

┌─────────────────────────────────────────────────────────┐
│            数据包调度架构 (pacing)                        │
├─────────────────────────────────────────────────────────┤
│                                                          │
│  媒体编码器 (Media Encoder)                               │
│    │                                                      │
│    ▼                                                      │
│  ┌──────────────────────────────────────────┐          │
│  │  Packet Queue                            │          │
│  │  ┌──────┐ ┌──────┐ ┌──────┐            │          │
│  │  │ Pkt0 │ │ Pkt1 │ │ Pkt2 │ ...        │          │
│  │  └──────┘ └──────┘ └──────┘            │          │
│  └──────────────┬───────────────────────────┘          │
│                 │                                       │
│                 ▼                                       │
│  ┌──────────────────────────────────────────┐          │
│  │  Pacing Controller                       │          │
│  │  ┌──────────────────────────────┐       │          │
│  │  │ Calculate Send Interval      │       │          │
│  │  │ - Based on Bitrate           │       │          │
│  │  │ - Based on Packet Size       │       │          │
│  │  │ - Smooth Transmission        │       │          │
│  │  └──────────┬───────────────────┘       │          │
│  │             │                            │          │
│  │             ▼                            │          │
│  │  ┌──────────────────────────────┐       │          │
│  │  │ Timer/Event                  │       │          │
│  │  │ - Schedule Next Send         │       │          │
│  │  └──────────┬───────────────────┘       │          │
│  └─────────────┼────────────────────────────┘          │
│                 │                                       │
│                 ▼                                       │
│          Network Send (UDP/TCP)                        │
│                                                          │
└─────────────────────────────────────────────────────────┘

🔄 数据流图

完整的流媒体推拉流程

``` ┌─────────────────────────────────────────────────────────────┐ │ 流媒体推拉完整流程 │ ├─────────────────────────────────────────────────────────────┤ │ │ │ 推流端 (Push) │ │ ┌──────────────────────────────────────────┐ │ │ │ Media Source │ │ │ │ - Camera/Microphone │ │ │ │ - File │ │ │ │ - Screen Capture │ │ │ └──────────────┬───────────────────────────┘ │ │ │ │ │ ▼ │ │ ┌──────────────────────────────────────────┐ │ │ │ Encoder │ │ │ │ - H.264/H.265 Video │ │ │ │ - AAC/MP3 Audio │ │ │ └──────────────┬───────────────────────────┘ │ │ │ │ │ ├──────────────┐ │ │ │ │ │ │ ▼ ▼ │ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │ │ RTSP │ │ HTTP-FLV │ │ HLS │ │ │ │ Push │ │ Push │ │ Muxer │ │ │ └────┬─────┘ └────┬─────┘ └────┬─────┘ │ │ │ │ │ │ │ │ │ │ │ │ ▼ ▼ ▼ │ │ ┌──────────────────────────────────────────┐ │ │ │ Media Server │ │ │ │ ┌──────────┐ ┌──────────┐ │ │ │ │ │ RTSP │ │ HTTP │ │ │ │ │ │ Server │ │ Server │ │ │ │ │ └────┬─────┘ └────┬─────┘ │ │ │ │ │ │ │ │ │ │ └──────┬──────┘ │ │ │ │ ▼ │ │ │ │ Stream Manager │ │ │ │ - Session Management │ │ │ │ - Stream Distribution │ │ │ └──────────────┬──────────────────────────┘ │ │ │ │ │ ├──────────────┐ │ │ │ │ │ │ ▼ ▼ │ │ ┌──────────┐ ┌──────────┐ │ │ │ RTSP │ │ HTTP-FLV │ │ │ │ Client