# nmelihsensoy/streamviewer architecture

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

- Interactive diagram: https://gitdiagram.com/nmelihsensoy/streamviewer
- Repository: https://github.com/nmelihsensoy/streamviewer

## Overview

##### Main components
- Android Frontend: Represented by the UI layer (MainActivity and associated Android resources/layouts).
- Streaming Engine: The integration with GStreamer both in Java (org.freedesktop.gstreamer and androidmedia packages) and in the native layer (native-lib.cpp and gstreamer_android.c) that performs video decoding and processing.
- HLS Streaming Component: The HLS server functionality as implemented in HlsServer.java, which restreams the current TCP source to an HLS feed.
- Frame Capture & YOLO Inference Module: This comprises components like ModelHelper.java and assets such as the yolov5su.onnx file, which are used to capture frames, run inference, and display output.
- External Dependencies/Configurations: Include the externally configured SDK paths from gradle.properties (paths to GStreamer and OpenCV SDKs).

##### Relationships and interactions
- The Android Frontend (MainActivity) interacts with the Streaming Engine to initiate the TCP stream connection and display video.
- The Streaming Engine, implemented partly in Java and partly in C/C++ (JNI), handles GStreamer’s stream processing.
- The HLS Restreaming component, via HlsServer, creates an endpoint (port 8181) making the stream accessible over HTTP.
- When a frame is captured, the UI triggers the Frame Capture module; the captured frame is saved to local storage and then passed to the YOLO Inference module, which processes the image and logs the detection results.
- Configurations in gradle.properties set up critical paths for external libraries (GStreamer, OpenCV), which the native and Java components utilize at runtime.

##### Architectural patterns and design principles
- Separation of concerns: Clean segregation between the frontend (UI), stream processing, native integration (for performance-critical tasks), and inference logic.
- Use of JNI for performance: The native C/C++ code is used to interface with GStreamer (likely for optimized streaming processing).
- Modularity: The modular folder structure (src/main/java for Android components, src/main/cpp for the native code) reinforces a modular design.
- Integration of external services: The HLS server exposes a network endpoint, illustrating inter-process / network communication.

##### Relevant technologies/frameworks
- Android SDK for UI.
- GStreamer (both Java and native C/C++ libraries) for media stream processing.
- ONNX model (YOLO inference) and potentially OpenCV for image manipulation.
- Gradle as the build system and CMake for native build configurations.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Android Frontend
    subgraph "Android Frontend"
        UI1["MainActivity.java"]:::frontend
        UI2["activity_main.xml"]:::frontend
    end

    %% Streaming Engine - Java Layer
    subgraph "Streaming Engine (Java)"
        JST1["GStreamer.java"]:::stream
        JST2["androidmedia (Directory)"]:::stream
    end

    %% Streaming Engine - Native (JNI Bridge)
    subgraph "Streaming Engine (Native / JNI)"
        N1["native-lib.cpp"]:::native
        N2["gstreamer_android.c"]:::native
        N3["CMakeLists.txt"]:::native
    end

    %% HLS Streaming Component
    subgraph "HLS Streaming Component"
        HLS["HlsServer.java"]:::server
    end

    %% Frame Capture & YOLO Inference Module
    subgraph "Frame Capture & YOLO Inference"
        FC1["ModelHelper.java"]:::inference
        FC2["yolov5su.onnx"]:::inference
    end

    %% External Dependencies/Configurations
    EX["gradle.properties"]:::external

    %% Relationships
    UI1 -->|"initStream"| JST1
    UI1 -->|"startHLS"| HLS
    UI1 -->|"captureFrame"| FC1

    JST1 -->|"callsJNI"| N1
    JST1 -->|"restreamFeed"| HLS

    EX -->|"externConf"| JST1
    EX -->|"externConf"| N1

    N1 -->|"decodedVideo"| UI1

    FC1 -->|"processFrame"| FC2
    FC1 -->|"displayInference"| UI1

    %% Click Events
    click UI1 "https://github.com/nmelihsensoy/streamviewer/blob/main/app/src/main/java/org/nmelihsensoy/streamviewer/MainActivity.java"
    click UI2 "https://github.com/nmelihsensoy/streamviewer/blob/main/app/src/main/res/layout/activity_main.xml"
    click JST1 "https://github.com/nmelihsensoy/streamviewer/blob/main/app/src/main/java/org/freedesktop/gstreamer/GStreamer.java"
    click JST2 "https://github.com/nmelihsensoy/streamviewer/tree/main/app/src/main/java/org/freedesktop/gstreamer/androidmedia"
    click N1 "https://github.com/nmelihsensoy/streamviewer/blob/main/app/src/main/cpp/native-lib.cpp"
    click N2 "https://github.com/nmelihsensoy/streamviewer/blob/main/app/src/main/cpp/gstreamer_android.c"
    click N3 "https://github.com/nmelihsensoy/streamviewer/blob/main/app/src/main/cpp/CMakeLists.txt"
    click HLS "https://github.com/nmelihsensoy/streamviewer/blob/main/app/src/main/java/org/nmelihsensoy/streamviewer/HlsServer.java"
    click FC1 "https://github.com/nmelihsensoy/streamviewer/blob/main/app/src/main/java/org/nmelihsensoy/streamviewer/ModelHelper.java"
    click FC2 "https://github.com/nmelihsensoy/streamviewer/blob/main/app/src/main/assets/yolov5su.onnx"
    click EX "https://github.com/nmelihsensoy/streamviewer/blob/main/gradle.properties"

    %% Styles
    classDef frontend fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef stream fill:#ccffcc,stroke:#009900,stroke-width:2px;
    classDef native fill:#ccccff,stroke:#0000cc,stroke-width:2px;
    classDef server fill:#ffffcc,stroke:#cccc00,stroke-width:2px;
    classDef inference fill:#ffccff,stroke:#cc00cc,stroke-width:2px;
    classDef external fill:#e0e0e0,stroke:#808080,stroke-width:2px;
```
