# rtspmediastream/raspberrypi5-rtsp-server architecture

> Architecture diagram of the GitHub repository rtspmediastream/raspberrypi5-rtsp-server, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rtspmediastream/raspberrypi5-rtsp-server
- Repository: https://github.com/rtspmediastream/raspberrypi5-rtsp-server

## Overview

##### Project Type and Purpose
This is a specialized streaming media server application that implements the RTSP (Real-Time Streaming Protocol) protocol, supporting audio, video, and camera streaming capabilities.

##### Main Components

A. Core Server Components:
- RTSPServer (Central component)
- TCP/UDP Handlers
- Request Handler
- Client Session Manager
- Media Stream Handler

B. Media Processing Components:
- Audio Capture System
- Video Processing System
- Camera Integration System
- Encoders (H264, Opus, FFmpeg)

C. Protocol Components:
- RTP Packet Handler
- RTCP Packet Handler
- Stream Management

**┌─────────────────── Client Layer ───────────────────┐**
- VLC Player / FFmpeg / Other RTSP Clients          │
- ┬───────────────────────────┘
- RTSP/RTP/RTCP
**┌───────────────────────▼───────────────────────────┐**
- Network Layer                      │
- ┌─────────────┐    ┌─────────────┐              │
- TCP Handler │    │ UDP Handler │              │
- ┘    └─────────────┘              │
- ┬───────────────────────────┘

**┌───────────────────────▼───────────────────────────┐**
- Protocol Layer                       │
- ┌─────────────┐    ┌─────────────┐              │
- RTSP Server  │    │Request      │              │
- Handler      │              │
- ┘    └─────────────┘              │
- ┌─────────────┐    ┌─────────────┐              │
- RTP Packet   │    │RTCP Packet  │              │
- Handler      │    │Handler      │              │
- ┘    └─────────────┘              │
- ┬───────────────────────────┘

**┌───────────────────────▼───────────────────────────┐**
- Media Layer                         │
- ┌─────────────┐    ┌─────────────┐              │
- Audio        │    │Video        │              │
- Capture      │    │Processing   │              │
- ┘    └─────────────┘              │
- ┌─────────────┐    ┌─────────────┐              │
- Camera       │    │Media Stream │              │
- Integration  │    │Handler      │              │
- ┘    └─────────────┘              │
- ┬───────────────────────────┘

**┌───────────────────────▼───────────────────────────┐**
- Encoder Layer                       │
- ┌─────────────┐    ┌─────────────┐              │
- H264 Encoder │    │Opus Encoder │              │
- ┘    └─────────────┘              │
- ┌─────────────┐                                 │
- FFmpeg       │                                 │
- Encoder      │                                 │
- ┘                                 │
- ┘

##### Implementation Details
- Separation between the three main server types (Audio, Video, Camera)
- Use of external libraries (libopus, libavcodec, etc.)
- Main entry points for each server type

Remember to maintain a balance between showing enough detail to understand the system's architecture while keeping it clean and readable. The focus should be on the main components and their interactions rather than implementation details.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles
    classDef client fill:#f0f0f0,stroke:#333
    classDef network fill:#b3d9ff,stroke:#333
    classDef protocol fill:#ffd9b3,stroke:#333
    classDef media fill:#b3ffb3,stroke:#333
    classDef encoder fill:#e6b3ff,stroke:#333

    %% Client Layer
    subgraph ClientLayer
        Clients["RTSP Clients<br/>(VLC, FFmpeg, etc.)"]:::client
    end

    %% Network Layer
    subgraph NetworkLayer
        TCP["TCP Handler"]:::network
        UDP["UDP Handler"]:::network
    end

    %% Protocol Layer
    subgraph ProtocolLayer
        RTSP["RTSP Server"]:::protocol
        ReqHandler["Request Handler"]:::protocol
        RTP["RTP Packet Handler"]:::protocol
        RTCP["RTCP Packet Handler"]:::protocol
        Session["Client Session Manager"]:::protocol
    end

    %% Media Layer
    subgraph MediaLayer
        Audio["Audio Capture System"]:::media
        Video["Video Processing"]:::media
        Camera["Camera Integration"]:::media
        MediaStream["Media Stream Handler"]:::media
        DataCap["Data Capture"]:::media
    end

    %% Encoder Layer
    subgraph EncoderLayer
        H264["H264 Encoder"]:::encoder
        Opus["Opus Encoder"]:::encoder
        FFmpeg["FFmpeg Encoder"]:::encoder
    end

    %% Connections
    Clients <--> TCP & UDP
    TCP & UDP --> RTSP
    RTSP --> ReqHandler
    RTSP --> Session
    Session --> RTP & RTCP
    RTP & RTCP --> MediaStream
    MediaStream --> Audio & Video & Camera
    Audio --> Opus
    Video & Camera --> H264
    Camera --> FFmpeg
    DataCap --> MediaStream

    %% Click Events
    click RTSP "https://github.com/rtspMediaStream/raspberrypi5-rtsp-server/blob/main/inc/RTSPServer.h"
    click TCP "https://github.com/rtspMediaStream/raspberrypi5-rtsp-server/blob/main/inc/TCPHandler.h"
    click UDP "https://github.com/rtspMediaStream/raspberrypi5-rtsp-server/blob/main/inc/UDPHandler.h"
    click ReqHandler "https://github.com/rtspMediaStream/raspberrypi5-rtsp-server/blob/main/inc/RequestHandler.h"
    click Session "https://github.com/rtspMediaStream/raspberrypi5-rtsp-server/blob/main/inc/ClientSession.h"
    click MediaStream "https://github.com/rtspMediaStream/raspberrypi5-rtsp-server/blob/main/inc/MediaStreamHandler.h"
    click Audio "https://github.com/rtspMediaStream/raspberrypi5-rtsp-server/blob/main/example/AudioServer/AudioCapture.h"
    click Camera "https://github.com/rtspMediaStream/raspberrypi5-rtsp-server/blob/main/example/CameraServer/main.cpp"
    click H264 "https://github.com/rtspMediaStream/raspberrypi5-rtsp-server/blob/main/inc/H264Encoder.h"
    click Opus "https://github.com/rtspMediaStream/raspberrypi5-rtsp-server/blob/main/inc/OpusEncoder.h"
    click FFmpeg "https://github.com/rtspMediaStream/raspberrypi5-rtsp-server/blob/main/example/CameraServer/FFmpegEncoder.h"
    click RTP "https://github.com/rtspMediaStream/raspberrypi5-rtsp-server/blob/main/inc/RTPPacket.hpp"
    click RTCP "https://github.com/rtspMediaStream/raspberrypi5-rtsp-server/blob/main/inc/RTCPPacket.hpp"
    click DataCap "https://github.com/rtspMediaStream/raspberrypi5-rtsp-server/blob/main/inc/DataCapture.h"
```
