# sipwise/rtpengine architecture

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

- Interactive diagram: https://gitdiagram.com/sipwise/rtpengine
- Repository: https://github.com/sipwise/rtpengine

## Overview

Based on our review of the file tree and README, the project is the Sipwise NGCP rtpengine—a specialized media proxy designed for handling RTP (and other UDP-based media) traffic for use with SIP proxies like Kamailio. It is a high-performance, real-time media forwarding and processing engine that incorporates both userspace (the daemon and recording processes) and kernel-level components (for offloaded packet forwarding).

##### Overall System View
- Project as a full media processing engine that sits between SIP proxy systems (like Kamailio) and the actual media endpoints.
- Two main operation paths: regular RTP/media handling via the primary daemon and call recording via the dedicated recording daemon.
- That there is support for both userspace operations and optional in-kernel packet forwarding via the kernel module.

##### Main Components
- Daemon Process:
– Represented by the files in the “daemon” directory.
– Responsible for core RTP processing, SDP rewriting, transcoding, bridging between IPv4 and IPv6 (and different network interfaces), and providing several protocol supports (including ICE, SRTP, and RTCP variants).
– Show submodules such as components for control (both TCP and UDP), logging, media socket handling, and network interfaces.

- Recording Daemon:
– Found in the “recording-daemon” folder.
– Handles call recording, media stream mixing, file handling, and logging for media recordings.

- Kernel Module:
– Located in “kernel-module”.
– This component provides in-kernel packet forwarding for efficient, lower latency, and CPU-efficient processing.

- Shared Libraries:
– Include “lib” and “include” directories, which offer common functionalities (e.g. helper functions, logging libraries, codec support, network abstractions) that are used across both daemon and recording processes.

- Packaging, Build, and Testing Tools:
– Represent artifacts from “debian”, “pkg”, “Makefile”, and “tests”, which manage the build, deployment, unit tests, and integration tests.
– Although not part of runtime, they indicate architectural concerns regarding portability, deployment, and correctness.

- External Interfaces:
– HTTP/WebSocket interfaces, as well as Janus and MQTT (in “daemon/mqtt.c” and “daemon/websocket.c”), which indicate how the rtpengine can be controlled and interfaced with other systems.
– Any indicated REST or control protocols such as the NGControl Protocol and TCP-NG Control Protocol.

##### Data Flow and External Relationships
- Incoming Media Traffic:
– Indicate how the main daemon receives and processes these packets.

- Processing Pipeline:
– Show the internal flow within the daemon, including packet parsing, SDP rewriting, codec/transcoding logic, and forwarding to media processing sub-components.
– Include a conditional branch where if the kernel module is available it intercepts or speeds up packet forwarding.

- Recording and Transcoding:
– Illustrate how the libraries support both real-time transmission and handling of recorded content.

- Control and Management:
– Mark control sockets (CLI, WebSocket, etc.) and external control interfaces.
– Indicate how configuration (files in “etc” and “debian/…” configuration scripts) plays into setting up the proper behaviors.

##### Architectural Patterns and Technologies
- Multi-threaded design used to achieve low latency.
- Plugin-like extensibility via shared libraries (from “lib” and “include”) and external modules (e.g., Janus interface support).
- Major communication patterns (point-to-point between modules, client-server interactions with control protocols).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Interfaces
    ext1("SIP Proxies (Kamailio)"):::external

    %% Userspace Components
    subgraph "Userspace Components"
        dp("Daemon Process (RTP & Media Handling)"):::core
        rd("Recording Daemon (Call Recording)"):::recording
        subgraph "Shared Libraries"
            lib("Lib (Common Functions)"):::shared
            inc("Include (Headers)"):::shared
        end
        subgraph "External Interfaces"
            mqtt("MQTT Interface"):::interface
            ws("WebSocket Interface"):::interface
            janus("Janus Interface"):::interface
        end
    end

    %% Kernel Offload
    subgraph "Kernel Offload"
        km("Kernel Module (Packet Forwarding)"):::kernel
    end

    %% Packaging, Build & Testing Tools
    subgraph "Packaging, Build & Testing Tools"
        deb("Debian Packaging"):::tools
        pkg("Pkg Packaging"):::tools
        mk("Makefile Build"):::tools
        tst("Tests (Unit/Integration)"):::tools
    end

    %% Data Flow Connections
    ext1 -->|"RTP/UDP Traffic"| dp
    dp -->|"accelerates"| km
    km -->|"fallback"| dp
    dp -->|"streams for recording"| rd
    lib --- dp
    lib --- rd
    inc --- dp
    inc --- rd
    dp -->|"control"| mqtt
    dp -->|"control"| ws
    dp -->|"control"| janus
    deb --- dp
    deb --- rd
    deb --- km
    pkg --- dp
    pkg --- rd
    pkg --- km
    mk --- dp
    mk --- rd
    mk --- km
    tst --- dp
    tst --- rd
    tst --- km

    %% Click Events for Component Mapping
    click dp "https://github.com/sipwise/rtpengine/tree/master/daemon/"
    click rd "https://github.com/sipwise/rtpengine/tree/master/recording-daemon/"
    click km "https://github.com/sipwise/rtpengine/tree/master/kernel-module/"
    click lib "https://github.com/sipwise/rtpengine/tree/master/lib/"
    click inc "https://github.com/sipwise/rtpengine/tree/master/include/"
    click deb "https://github.com/sipwise/rtpengine/tree/master/debian/"
    click pkg "https://github.com/sipwise/rtpengine/tree/master/pkg/"
    click mk "https://github.com/sipwise/rtpengine/tree/master/Makefile"
    click tst "https://github.com/sipwise/rtpengine/tree/master/tests/"
    click mqtt "https://github.com/sipwise/rtpengine/blob/master/daemon/mqtt.c"
    click ws "https://github.com/sipwise/rtpengine/blob/master/daemon/websocket.c"
    click janus "https://github.com/sipwise/rtpengine/blob/master/daemon/janus.c"

    %% Styles
    classDef external fill:#FFCC00,stroke:#333,stroke-width:2px;
    classDef core fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef recording fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef kernel fill:#F4A5A5,stroke:#333,stroke-width:2px;
    classDef shared fill:#E0FFE0,stroke:#333,stroke-width:2px;
    classDef tools fill:#E8E8E8,stroke:#333,stroke-width:2px;
    classDef interface fill:#D1E8E2,stroke:#333,stroke-width:2px;
```
