# gtxaspec/prudynt-t architecture

> Architecture diagram of the GitHub repository gtxaspec/prudynt-t, 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/gtxaspec/prudynt-t
- Repository: https://github.com/gtxaspec/prudynt-t

## Overview

##### Project type and purpose
- prudynt-t is an embedded video server daemon for Ingenic-based cameras (e.g., Wyze v3 and Thingino hardware).
- It captures raw video and audio from the camera SoC, encodes streams (H264/H265 for video, AAC/PCMU for audio), and exposes them over RTSP (for video+audio playback) and a WebSocket backchannel (for two-way audio/control).

##### File structure
- Top-level build and CI: Dockerfile, Makefile, build scripts (build.sh, buildroot_dev.sh), `.github/workflows/pru.yaml`.
- Resources and patches: `res/` directory contains library patches and logos.
- Dependency builders: `scripts/` for building libhelixaac, libwebsockets, opus, etc.
- Core code in `src/`: organized into
- Hardware abstraction & system init: `IMPSystem`, `Config`, `ConfigWatcher`
- Media sources: `IMPDeviceSource`, `IMPFrameSource`, `AudioReframer`
- Encoders & formats: `AACEncoder`, `Opus`, `JPEGWorker`, `Motion` (on-the-fly motion detection)
- Worker threads: `VideoWorker`, `AudioWorker`, `BackchannelWorker`, each using a `RingBuffer` and `WorkerUtils`
- Sinks and RTSP glue: `AACSink`, `BackchannelSink`, `BackchannelServerMediaSubsession`, `IMP*ServerMediaSubsession`, `RTSP`
- Networking & control: `WS` (WebSocket handling), `MsgChannel`
- Utilities: `Logger`, `OSD` (on-screen display overlays)
- Entry point: `main.cpp`

##### Extract architectural insights from README
- Describes two-way audio, video codecs, Thingino integration
- Build instructions show dependency on Buildroot or Docker isolation

a. Main components
1. Hardware & ISA: Ingenic SoC running Linux + libimp_control
2. Device Source Layer: `IMPDeviceSource` / `IMPFramesource`
3. Processing Pipelines:
- Video Pipeline: `VideoWorker` → `Motion` & `JPEGWorker` (optional) → H264/H265 encoder (via internal IMPEncoder)
- Audio Pipeline: `AudioWorker` → `AudioReframer` → AACEncoder / Opus
4. Buffering: `RingBuffer` between producers and consumers
5. Sink & Streaming Layer:
- `AACSink` / `BackchannelSink` → RTSP Server subsessions (`ServerMediaSubsession`)
- Backchannel (two-way): WebSocket server (`WS`) + `BackchannelServerMediaSubsession`
6. Control & Config: `ConfigWatcher` / `Config` reloads settings at runtime, `WS` for control messages
7. Utilities: Logging, OSD
8. External Clients: RTSP clients (VLC, etc.), WebSocket clients (mobile/JS UIs)

b. Relationships and interactions
- Device Source → ring buffers → Worker threads
- Workers invoke encoders, feed encoded frames to sinks
- Sinks push to RTSP server subsessions (video+audio multiplexed) or to WebSocket stream
- ConfigWatcher notifies workers and sinks on config changes
- Logger is invoked by every layer for debug/status output

c. Architectural patterns & principles
- Modular pipeline stages connected by thread-safe queues (producer/consumer)
- Separation of concerns: sources, processing, sinks, network layers
- Plugin-like subsessions for RTSP streaming (Live555)
- Hot-reloadable config via watcher
- Hardware abstraction (IMPSystem, libimp_control)

d. Key technologies & libraries
- Live555 (RTSP stack)
- libimp_control (Ingenic hardware interface)
- AAC (libhelix-aac), Opus, WebSockets (libwebsockets)
- Buildroot/Docker for cross-compilation

##### Detail
- Shared resources (e.g., ring buffers) explicitly.
- Third-party libraries at their integration points.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Hardware Layer
    Hardware["Ingenic SoC\n(libimp_control)"]:::source
    click Hardware "https://github.com/gtxaspec/prudynt-t/blob/master/src/IMPSystem.cpp"

    %% Device Source Layer
    subgraph "Device Source" 
        direction TB
        IMPDeviceSource["IMPDeviceSource"]:::source
        click IMPDeviceSource "https://github.com/gtxaspec/prudynt-t/blob/master/src/IMPDeviceSource.cpp"
        IMPFrameSource["IMPFrameSource"]:::source
        click IMPFrameSource "https://github.com/gtxaspec/prudynt-t/blob/master/src/IMPFramesource.cpp"
    end

    %% Buffering
    RingBuffer["RingBuffer"]:::util
    click RingBuffer "https://github.com/gtxaspec/prudynt-t/blob/master/src/RingBuffer.hpp"

    %% Video Processing Pipeline
    subgraph "Video Pipeline"
        direction TB
        VideoWorker["VideoWorker\n(thread)"]:::process
        click VideoWorker "https://github.com/gtxaspec/prudynt-t/blob/master/src/VideoWorker.cpp"
        Motion["Motion\n(Motion Detection)"]:::process
        click Motion "https://github.com/gtxaspec/prudynt-t/blob/master/src/Motion.cpp"
        JPEGWorker["JPEGWorker"]:::process
        click JPEGWorker "https://github.com/gtxaspec/prudynt-t/blob/master/src/JPEGWorker.cpp"
        IMPEncoder["IMPEncoder\n(H264/H265)"]:::process
        click IMPEncoder "https://github.com/gtxaspec/prudynt-t/blob/master/src/IMPEncoder.cpp"
    end

    %% Audio Processing Pipeline
    subgraph "Audio Pipeline"
        direction TB
        AudioWorker["AudioWorker\n(thread)"]:::process
        click AudioWorker "https://github.com/gtxaspec/prudynt-t/blob/master/src/AudioWorker.cpp"
        AudioReframer["AudioReframer"]:::process
        click AudioReframer "https://github.com/gtxaspec/prudynt-t/blob/master/src/AudioReframer.cpp"
        AACEncoder["AACEncoder"]:::process
        click AACEncoder "https://github.com/gtxaspec/prudynt-t/blob/master/src/AACEncoder.cpp"
        OpusEncoder["OpusEncoder"]:::process
        click OpusEncoder "https://github.com/gtxaspec/prudynt-t/blob/master/src/Opus.cpp"
    end

    %% Sinks & Streaming
    subgraph "Streaming & Sinks"
        direction TB
        AACSink["AACSink"]:::network
        click AACSink "https://github.com/gtxaspec/prudynt-t/blob/master/src/AACSink.cpp"
        BackchannelSink["BackchannelSink"]:::network
        click BackchannelSink "https://github.com/gtxaspec/prudynt-t/blob/master/src/BackchannelSink.cpp"
        RTSPServer["RTSP Server\n(Live555)"]:::network
        click RTSPServer "https://github.com/gtxaspec/prudynt-t/blob/master/src/RTSP.cpp"
        IMPServerSub["IMPServerMediaSubsession"]:::network
        click IMPServerSub "https://github.com/gtxaspec/prudynt-t/blob/master/src/IMPServerMediaSubsession.cpp"
        IMPAudioSub["IMPAudioServerMediaSubsession"]:::network
        click IMPAudioSub "https://github.com/gtxaspec/prudynt-t/blob/master/src/IMPAudioServerMediaSubsession.cpp"
        BackchannelSub["BackchannelServerMediaSubsession"]:::network
        click BackchannelSub "https://github.com/gtxaspec/prudynt-t/blob/master/src/BackchannelServerMediaSubsession.cpp"
    end

    %% Backchannel Control & Messaging
    subgraph "Backchannel Control"
        direction TB
        WS["WS (WebSocket)"]:::network
        click WS "https://github.com/gtxaspec/prudynt-t/blob/master/src/WS.cpp"
        MsgChannel["MsgChannel"]:::util
        click MsgChannel "https://github.com/gtxaspec/prudynt-t/blob/master/src/MsgChannel.hpp"
        StreamState["BackchannelStreamState"]:::util
        click StreamState "https://github.com/gtxaspec/prudynt-t/blob/master/src/BackchannelStreamState.cpp"
        IMPBackchannel["IMPBackchannel"]:::util
        click IMPBackchannel "https://github.com/gtxaspec/prudynt-t/blob/master/src/IMPBackchannel.cpp"
    end

    %% Config & Utilities
    subgraph "Config & Utilities"
        direction TB
        Config["Config"]:::util
        click Config "https://github.com/gtxaspec/prudynt-t/blob/master/src/Config.cpp"
        ConfigWatcher["ConfigWatcher"]:::util
        click ConfigWatcher "https://github.com/gtxaspec/prudynt-t/blob/master/src/ConfigWatcher.cpp"
        Logger["Logger"]:::util
        click Logger "https://github.com/gtxaspec/prudynt-t/blob/master/src/Logger.cpp"
        OSD["OSD"]:::util
        click OSD "https://github.com/gtxaspec/prudynt-t/blob/master/src/OSD.cpp"
        WorkerUtils["WorkerUtils"]:::util
        click WorkerUtils "https://github.com/gtxaspec/prudynt-t/blob/master/src/WorkerUtils.cpp"
    end

    %% Entry Point
    Main["main.cpp\n(Application Entry)"]:::util
    click Main "https://github.com/gtxaspec/prudynt-t/blob/master/src/main.cpp"

    %% External Clients
    RTSPClient["RTSP Clients\n(VLC, etc.)"]:::external
    WSClient["WebSocket Clients\n(UI)"]:::external

    %% Data Flows
    Hardware --> IMPDeviceSource
    Hardware --> IMPFrameSource

    IMPDeviceSource --> RingBuffer
    IMPFrameSource --> RingBuffer

    RingBuffer --> VideoWorker
    RingBuffer --> AudioWorker

    VideoWorker --> Motion
    VideoWorker --> JPEGWorker
    Motion --> IMPEncoder
    JPEGWorker --> IMPEncoder
    IMPEncoder --> AACSink
    IMPEncoder --> RTSPServer

    AudioWorker --> AudioReframer
    AudioReframer --> AACEncoder
    AACEncoder --> AACSink
    OpusEncoder --> AACSink

    AACSink --> RTSPServer
    RTSPServer --> IMPServerSub
    RTSPServer --> IMPAudioSub
    IMPServerSub --> RTSPClient
    IMPAudioSub --> RTSPClient

    WSClient --> WS
    WS --> MsgChannel
    MsgChannel --> StreamState
    StreamState --> BackchannelSink
    BackchannelSink --> BackchannelSub
    BackchannelSub --> RTSPServer
    BackchannelSub --> WSClient

    Config --> ConfigWatcher
    ConfigWatcher --> VideoWorker
    ConfigWatcher --> AudioWorker
    ConfigWatcher --> BackchannelSink

    Logger --> VideoWorker
    Logger --> AudioWorker
    Logger --> BackchannelSink
    Logger --> WS
    Logger --> RTSPServer
    Logger --> ConfigWatcher

    OSD --> VideoWorker

    Main --> Hardware
    Main --> Config
    Main --> Logger

    %% Styles
    classDef source fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef process fill:#d4edda,stroke:#333,stroke-width:1px
    classDef network fill:#ffe5b4,stroke:#333,stroke-width:1px
    classDef util fill:#e2e3e5,stroke:#333,stroke-width:1px
    classDef external fill:#f5c6cb,stroke:#333,stroke-width:1px
```
