How mira-screen-share/sharer works
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.