# mira-screen-share/sharer architecture

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

- Interactive diagram: https://gitdiagram.com/mira-screen-share/sharer
- Repository: https://github.com/mira-screen-share/sharer

## Overview

##### Project Type and Overall Purpose
- This is a full-stack, multimedia streaming application focused on screen capture, encoding, and transmission using WebRTC.
- Its goal is to capture the screen and audio (using platform-specific capture APIs), encode the streams (with options like x264, NVENC, VP9, etc.), and send them to remote viewers via a P2P connection—with fallback to TURN relays if needed.
- The README indicates that the overall system is divided into at least three “logical” parts: the sharer client (this project), the viewer client, and the signalling server, with this repository covering the sharer component.

##### Analyzing the File Structure for Architectural Clues
- The src directory is clearly modular, containing subdirectories like:
- src/capture: Contains modules for screen capture techniques, separated further by platform (macos, wgc for Windows), audio capture, and even YUV conversion shaders.
- src/encoder: Integrates FFmpeg-based encoding and frame pooling, showing that encoding happens as a distinct and well-encapsulated stage.
- src/signaller: Manages establishing P2P connections and performing connection negotiation via WebSockets.
- src/outputs and src/inputs: Manage downstream output (file, WebRTC, or dummy no-op) and process input events (e.g., remote control signals).
- src/auth: Likely deals with authentication/authorization aspects.
- Additional directories (configs) provide various encoder configurations and are linked to the setup of encoding parameters.

##### Key Components
a. Subsystem:
- Modules: src/capture (with platform-specific folders for macos and Windows [wgc]), audio capture, and the YUV conversion modules.
- Role: Capture the screen (and audio) using APIs like ScreenCaptureKit on macOS or Windows.Graphics.Capture on Windows and preprocess frames.

b. Encoder:
- Modules: src/encoder and its supporting components (like frame_pool).
- Role: Compress raw frames into streaming-friendly formats (using FFmpeg libraries), guided by configuration options provided in the TOML files.

c. Output Subsystem:
- Modules: src/output (with file_output, webrtc_output, noop_output, etc.).
- Role: Deliver the encoded stream to a peer—either saving to a file, transmitting via WebRTC to remote viewers, or handling special fallback/diagnostics.

d. GUI:
- Modules: src/gui (components, themes, resources).
- Role: Provide a user interface for configuring the sharing session, possibly previewing the captured stream, and adjusting settings.

e. Signaller:
- Modules: src/signaller (especially via WebSockets).
- Role: Manage the signaling required by the WebRTC stack to negotiate P2P connections between sharer and viewer.

f. Additional Components:
- Auth (src/auth): Facilitates authentication if needed.
- Inputs (src/inputs): Processes remote input commands (keyboard/mouse events) to control the sharer’s OS.
- Config: The configuration file (config.toml and presets in configs/) dictates operational parameters across subsystems.

##### Relationships and Interactions
- Raw data flows into the Encoder which applies compression (using FFmpeg libraries); this step may use pre/post processing modules like YUV converters.
- Encoded data then routes to the Output layer that sends it over WebRTC.
- The Signaller operates in parallel to handle connection negotiation and peer discovery and provides input to determine routing when direct connections aren’t possible.
- The GUI ties into all these components by letting the user select configurations (which gate the encoder’s operation) and monitor the session.
- Dependence on configuration files (TOML) informs encoder settings and optionally selects between different codecs or capture methods.

##### Architectural Patterns and Design Principles
- Modular design: Each subsystem (capture, encode, output, signaller, GUI) is isolated in its own module/directory.
- Platform abstraction: OS-specific implementations are kept inside their own folders (e.g., macos, wgc) under capture.
- Separation of concerns: Decoupling screen capturing, encoding, output streaming, and signaling allows for independent updates and testing.
- Configurability: External configuration (configs folder) allows swapping encoders or fine-tuning parameters without code changes.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Configuration Subsystem
    subgraph "Configuration"
        configPresets["Configuration Presets"]
        configSrc["Config Module"]
    end

    %% Capture Subsystem
    subgraph "Capture Subsystem"
        capMain["Capture Subsystem"]
        capMac["macOS Capture"]
        capWgc["Windows Capture"]
        capAudio["Audio Capture"]
        capYUV["YUV Conversion"]
    end

    %% Encoder Subsystem
    subgraph "Encoder (FFmpeg)"
        encMain["Encoder"]
        encFfmpeg["FFmpeg Encoder"]
        encPool["Frame Pool"]
    end

    %% Output Subsystem
    subgraph "Output Subsystem"
        outMain["Output Subsystem"]
        outFile["File Output"]
        outWebRTC["WebRTC Output"]
        outNoop["NOOP Output"]
        outPeer["WebRTC Peer"]
    end

    %% Signaller Subsystem
    subgraph "Signaller"
        signMain["Signalling Server"]
        signMod["Signaller Module"]
        signWS["WebSocket Signaller"]
    end

    %% GUI Subsystem
    subgraph "GUI"
        guiMain["GUI"]
        guiApp["App Component"]
        guiComp["GUI Components"]
        guiTheme["GUI Themes"]
        guiIcon["UI Icons"]
    end

    %% Auth Subsystem
    subgraph "Auth"
        authMain["Auth"]
    end

    %% Inputs Subsystem
    subgraph "Inputs"
        inpMain["Inputs"]
        inpMod["Input Module"]
        inpKey["Key Parsing"]
    end

    %% Interactions between major subsystems
    configPresets -->|"settings"| encMain
    configPresets -->|"settings"| capMain
    configSrc -->|"settings"| encMain
    configSrc -->|"settings"| capMain

    capMain -->|"raw_frames"| encMain
    encMain -->|"encoded_stream"| outMain

    signMain -->|"signaling"| encMain
    signMain -->|"connection_info"| outMain

    guiMain <--> capMain
    guiMain <--> encMain
    guiMain <--> outMain
    guiMain <--> signMain
    guiMain <--> authMain
    guiMain <--> inpMain

    %% Internal subsystem module breakdown
    capMain --> capMac
    capMain --> capWgc
    capMain --> capAudio
    capMain --> capYUV

    encMain --> encFfmpeg
    encMain --> encPool

    outMain --> outFile
    outMain --> outWebRTC
    outMain --> outNoop
    outMain --> outPeer

    signMain --> signMod
    signMain --> signWS

    inpMain --> inpMod
    inpMain --> inpKey

    guiMain --> guiApp
    guiMain --> guiComp
    guiMain --> guiTheme
    guiMain --> guiIcon

    %% Click Events for Component Mapping
    click capMain "https://github.com/mira-screen-share/sharer/tree/main/src/capture"
    click capMac "https://github.com/mira-screen-share/sharer/tree/main/src/capture/macos"
    click capWgc "https://github.com/mira-screen-share/sharer/tree/main/src/capture/wgc"
    click capAudio "https://github.com/mira-screen-share/sharer/tree/main/src/capture/audio"
    click capYUV "https://github.com/mira-screen-share/sharer/tree/main/src/capture/yuv_convert"
    
    click encMain "https://github.com/mira-screen-share/sharer/tree/main/src/encoder"
    click encFfmpeg "https://github.com/mira-screen-share/sharer/blob/main/src/encoder/ffmpeg.rs"
    click encPool "https://github.com/mira-screen-share/sharer/blob/main/src/encoder/frame_pool.rs"
    
    click outMain "https://github.com/mira-screen-share/sharer/tree/main/src/output"
    click outFile "https://github.com/mira-screen-share/sharer/blob/main/src/output/file_output.rs"
    click outWebRTC "https://github.com/mira-screen-share/sharer/blob/main/src/output/webrtc_output.rs"
    click outNoop "https://github.com/mira-screen-share/sharer/blob/main/src/output/noop_output.rs"
    click outPeer "https://github.com/mira-screen-share/sharer/blob/main/src/output/webrtc_peer.rs"
    
    click signMain "https://github.com/mira-screen-share/sharer/tree/main/src/signaller"
    click signMod "https://github.com/mira-screen-share/sharer/blob/main/src/signaller/mod.rs"
    click signWS "https://github.com/mira-screen-share/sharer/blob/main/src/signaller/websocket_signaller.rs"
    
    click guiMain "https://github.com/mira-screen-share/sharer/tree/main/src/gui"
    click guiApp "https://github.com/mira-screen-share/sharer/blob/main/src/gui/app.rs"
    click guiComp "https://github.com/mira-screen-share/sharer/tree/main/src/gui/component/"
    click guiTheme "https://github.com/mira-screen-share/sharer/tree/main/src/gui/theme/"
    click guiIcon "https://github.com/mira-screen-share/sharer/tree/main/resources/icons"
    
    click authMain "https://github.com/mira-screen-share/sharer/tree/main/src/auth"
    
    click inpMain "https://github.com/mira-screen-share/sharer/tree/main/src/inputs"
    click inpMod "https://github.com/mira-screen-share/sharer/blob/main/src/inputs/mod.rs"
    click inpKey "https://github.com/mira-screen-share/sharer/blob/main/src/inputs/parse_key.rs"
    
    click configPresets "https://github.com/mira-screen-share/sharer/tree/main/configs/"
    click configSrc "https://github.com/mira-screen-share/sharer/blob/main/src/config.rs"

    %% Styles and Classes
    classDef subsystem fill:#E0F7FA,stroke:#006064,stroke-width:2px;
    classDef module fill:#FFF9C4,stroke:#F57F17,stroke-width:2px;
    classDef ui fill:#FFECB3,stroke:#FFA000,stroke-width:2px;
    classDef config fill:#C8E6C9,stroke:#388E3C,stroke-width:2px;

    class capMain,encMain,outMain,signMain,guiMain,authMain,inpMain,configPresets,configSrc subsystem;
    class capMac,capWgc,capAudio,capYUV,encFfmpeg,encPool,outFile,outWebRTC,outNoop,outPeer,signMod,signWS,inpMod,inpKey module;
    class guiApp,guiComp,guiTheme,guiIcon ui;
    class configPresets,configSrc config;
```
