# evercam/ex_nvr architecture

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

- Interactive diagram: https://gitdiagram.com/evercam/ex_nvr
- Repository: https://github.com/evercam/ex_nvr

## Overview

##### Main Components

a. Frontend Layer:
- Phoenix LiveView UI
- WebRTC Interface
- HLS Streaming Interface
- API Endpoints
- Swagger Documentation

b. Core Application Layer:
- Device Management
- Pipeline Management
- Recording Management
- User Management
- Event System

c. Media Processing Layer:
- RTSP Client (from rtsp project)
- Video Processing Pipeline
- Codec Support (H264/H265)
- Stream Management

d. Storage Layer:
- Local File System Storage
- SQLite Database
- Remote Storage Integration

##### Component Relationships

**[External Devices] → [RTSP/ONVIF Layer] → [Pipeline Management] → [Storage Layer]**
- ↓
**[Web Clients] → [Frontend Layer] → [Core Application Layer] → [Database]**

##### Detailed Component Breakdown

a. Device Integration:
**[IP Cameras] → [RTSP Client] → [Stream Handler] → [Pipeline Manager]**
**[ONVIF Discovery] ↔ [Device Manager]**

b. Pipeline Flow:
**[Video Input] → [Decoder (H264/H265)] → [Processing Elements] → [Multiple Outputs]**
- → [HLS]
- → [WebRTC]
- → [Storage]

##### Key Elements to Highlight

- WebRTC and HLS streaming capabilities
- ONVIF device discovery
- Multiple codec support (H264/H265)
- Storage management
- Authentication and authorization
- API endpoints
- Monitoring and metrics

##### Architectural Patterns

- Supervision Tree Structure
- Event-driven Communication
- Pipeline Pattern for Media Processing
- MVC Pattern for Web Interface

##### Integration Points

- External Storage Services
- Unix Socket Interface
- API Endpoints
- WebSocket Connections

##### Storage Layer (Bottom)

- Component types
- Connection types
- Important symbols

Keep the abstraction level high enough to show the overall architecture while including enough detail to understand the system's core functionality. Focus on showing how the video processing pipeline integrates with the web interface and storage systems, as this is the core functionality of the NVR system.

Remember to indicate the scalability aspects and how different components can be distributed or containerized, as evidenced by the Docker support in the project.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Define styles
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef media fill:#2ecc71,stroke:#27ae60,color:white
    classDef storage fill:#f1c40f,stroke:#f39c12,color:black
    classDef security fill:#e74c3c,stroke:#c0392b,color:white
    classDef integration fill:#9b59b6,stroke:#8e44ad,color:white
    classDef core fill:#34495e,stroke:#2c3e50,color:white

    %% External Components
    IPCam["IP Cameras"]:::integration
    WebClients["Web Clients"]:::frontend

    %% Frontend Layer
    subgraph Frontend
        LiveView["Phoenix LiveView UI"]:::frontend
        WebRTC["WebRTC Interface"]:::frontend
        HLS["HLS Streaming"]:::frontend
        API["API Endpoints"]:::frontend
        Swagger["Swagger Docs"]:::frontend
    end

    %% Core Application Layer
    subgraph Core
        DeviceMgmt["Device Management"]:::core
        PipelineMgmt["Pipeline Management"]:::core
        RecordMgmt["Recording Management"]:::core
        UserMgmt["User Management"]:::security
        EventSys["Event System"]:::core
        ONVIF["ONVIF Discovery"]:::integration
    end

    %% Media Processing Layer
    subgraph Media
        RTSPClient["RTSP Client"]:::media
        VideoProc["Video Processing"]:::media
        CodecH264["H264 Codec"]:::media
        CodecH265["H265 Codec"]:::media
        StreamMgmt["Stream Management"]:::media
    end

    %% Storage Layer
    subgraph Storage
        LocalFS["Local File System"]:::storage
        SQLite["SQLite Database"]:::storage
        RemoteStorage["Remote Storage"]:::storage
    end

    %% Relationships
    IPCam --> RTSPClient
    RTSPClient --> StreamMgmt
    StreamMgmt --> VideoProc
    VideoProc --> CodecH264 & CodecH265
    CodecH264 & CodecH265 --> PipelineMgmt
    PipelineMgmt --> HLS & WebRTC
    PipelineMgmt --> LocalFS & RemoteStorage
    
    WebClients --> LiveView
    LiveView --> API
    API --> DeviceMgmt & UserMgmt
    DeviceMgmt --> ONVIF
    EventSys --> RecordMgmt
    UserMgmt --> SQLite
    
    %% Click Events
    click LiveView "https://github.com/evercam/ex_nvr/tree/master/ui/lib/ex_nvr_web/live/"
    click WebRTC "https://github.com/evercam/ex_nvr/blob/master/ui/assets/js/webrtc.js"
    click HLS "https://github.com/evercam/ex_nvr/tree/master/ui/lib/ex_nvr/hls/"
    click API "https://github.com/evercam/ex_nvr/tree/master/ui/lib/ex_nvr_web/controllers/api/"
    click Swagger "https://github.com/evercam/ex_nvr/blob/master/docs/openapi.yaml"
    click DeviceMgmt "https://github.com/evercam/ex_nvr/tree/master/ui/lib/ex_nvr/devices/"
    click PipelineMgmt "https://github.com/evercam/ex_nvr/tree/master/ui/lib/ex_nvr/pipeline/"
    click RecordMgmt "https://github.com/evercam/ex_nvr/tree/master/ui/lib/ex_nvr/recordings/"
    click UserMgmt "https://github.com/evercam/ex_nvr/tree/master/ui/lib/ex_nvr/accounts/"
    click EventSys "https://github.com/evercam/ex_nvr/tree/master/ui/lib/ex_nvr/events/"
    click RTSPClient "https://github.com/evercam/ex_nvr/tree/master/rtsp/lib/rtsp/source/"
    click CodecH264 "https://github.com/evercam/ex_nvr/tree/master/rtsp/lib/rtsp/depayloader/h264/"
    click CodecH265 "https://github.com/evercam/ex_nvr/tree/master/rtsp/lib/rtsp/depayloader/h265/"
    click StreamMgmt "https://github.com/evercam/ex_nvr/blob/master/rtsp/lib/rtsp/source/stream_handler.ex"
    click RemoteStorage "https://github.com/evercam/ex_nvr/tree/master/ui/lib/ex_nvr/remote_storages/"
    click ONVIF "https://github.com/evercam/ex_nvr/tree/master/ui/lib/ex_nvr/onvif/"

    %% Legend
    subgraph Legend
        Frontend_L["Frontend"]:::frontend
        Media_L["Media Processing"]:::media
        Storage_L["Storage"]:::storage
        Security_L["Security"]:::security
        Integration_L["Integration"]:::integration
        Core_L["Core"]:::core
    end
```
