# livekit/client-sdk-swift architecture

> Architecture diagram of the GitHub repository livekit/client-sdk-swift, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/livekit/client-sdk-swift
- Repository: https://github.com/livekit/client-sdk-swift

## Overview

##### Main Components
- Core/Session Management:
– Include the “Room” class and associated modules (found in Sources/LiveKit/Core) that handle the lifecycle of a connection. This module will be responsible for signaling (via SignalClient and RPC), session state, transport, and overall room management.
- Media Streams and Tracks:
– Show separate components for “Track” management (local vs remote), with LocalTrackPublication and RemoteTrackPublication (Sources/LiveKit/Track and Sources/LiveKit/TrackPublications).
– Separate the handling of audio (Sources/LiveKit/Audio) and video components (with VideoTrack classes and support in Sources/LiveKit/Views and Sources/LiveKit/SwiftUI).
- Data Streaming:
– Illustrate the “DataStream” module. This module (with its Incoming and Outgoing subdirectories) handles non‐media data exchange between participants.
- Broadcast and Screen Sharing:
– Represent the Broadcast module (Sources/LiveKit/Broadcast) that supports broadcasting functionality (including IPC, screen capturing, and associated codecs).
- End-to-End Encryption (E2EE):
– Include the E2EE module (Sources/LiveKit/E2EE) that handles encryption tasks like key provisioning and session state.
- Protocols and Extensions:
– Note the Extensions module long list of helper extensions which enrich standard types to support media and networking tasks.
- Support Utilities:
– Include a “Support” module (e.g., Async helpers, Logger, WebSocket wrapper, RingBuffer, and other utilities). This module supports the underlying operations across modules.
- Objective-C Interoperability:
– Add a “LKObjCHelpers” component indicating the Objective-C bridging code that enables mixed Swift/Objective-C usage.
- Integration with UI:
- External Dependencies & Build/Package Management:
– Optionally (if relevant) denote the package/podspec files (Package.swift, LiveKitClient.podspec) indicating that the SDK can be consumed via Swift Package Manager or CocoaPods.
- Testing & CI Infrastructure:
– Although not part of the runtime architecture, you might add a note for the extensive “Tests” suite and the GitHub workflows (under .github/workflows) that ensure module quality and regular integrations.

##### Relationships and Interactions
- The “Room” and core signaling component sits at the center, orchestrating communication between:
- The Data/Media Track modules for publishing and subscribing to audio/video tracks.
- The DataStream module for sending/receiving arbitrary payloads.
- The E2EE module for encrypting/decrypting media or data streams.
- Dependency relationships where modules like Audio and Broadcast rely on underlying system libraries (e.g., AVAudioSession, ReplayKit on macOS) and internal utility components from the “Support” module.

##### Architectural Patterns
- Protocol-Driven Design – Highlight the role of Swift protocols to abstract component interactions, enabling easier testing and extensibility.
- Asynchronous and Thread-Safe Design – Note that many tasks (networking, audio/video processing) work on background threads with UI updates dispatched on the main thread (as mentioned in the README).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core/Session Management
    subgraph "Core/Session Management"
        Room["Room/Session Manager"]:::core
        SignalClient["SignalClient"]:::core
        Transport["Transport"]:::core
    end

    %% Media Streams and Tracks
    subgraph "Media Streams and Tracks"
        subgraph "Track Management"
            LocalTrack["LocalTrackPublication / LocalTrack"]:::media
            RemoteTrack["RemoteTrackPublication / RemoteTrack"]:::media
        end
        AudioModule["Audio Module"]:::media
        VideoModule["Video Module"]:::media
    end

    %% Data Streaming
    subgraph "Data Streaming"
        DataStream["Data Stream Manager"]:::data
    end

    %% Broadcast and Screen Sharing
    subgraph "Broadcast & Screen Sharing"
        BroadcastManager["Broadcast Manager"]:::broadcast
        ScreenShare["Screen Sharing (ScreenCapturer)"]:::broadcast
    end

    %% End-to-End Encryption (E2EE)
    Encryption["Encryption Engine (E2EE)"]:::encryption

    %% Protocols and Extensions
    subgraph "Protocols & Extensions"
        Protocols["Protocols (RoomDelegate, TrackDelegate, etc.)"]:::protocols
        Extensions["Extensions"]:::protocols
    end

    %% Support Utilities
    Support["Support Module"]:::support

    %% Objective-C Interoperability
    ObjC["LKObjCHelpers"]:::objc

    %% Integration with UI
    subgraph "UI Integration"
        UIKit["UIKit VideoView"]:::ui
        SwiftUI["SwiftUI Components"]:::ui
    end

    %% External Dependencies & Build/Package Management
    subgraph "External Dependencies"
        Package["Package.swift"]:::external
        Podspec["LiveKitClient.podspec"]:::external
    end

    %% Testing & CI Infrastructure
    subgraph "Testing & CI"
        Tests["Tests Suite"]:::testing
        CI["CI Workflows"]:::testing
    end

    %% Relationships and Interactions
    Room -->|"signals_via"| SignalClient
    Room -->|"controls"| Transport
    Room -->|"manages"| LocalTrack
    Room -->|"manages"| RemoteTrack
    Room -->|"communicates_with"| DataStream
    Room -->|"secures_via"| Encryption
    LocalTrack -->|"renders_in"| UIKit
    RemoteTrack -->|"renders_in"| UIKit
    LocalTrack -->|"renders_in"| SwiftUI
    RemoteTrack -->|"renders_in"| SwiftUI
    AudioModule -->|"feeds_to"| Room
    VideoModule -->|"integrates_with"| Room
    BroadcastManager -->|"initiates_broadcast_for"| Room
    ScreenShare -->|"captures_for"| BroadcastManager
    Protocols --- Room
    Protocols --- LocalTrack
    Protocols --- RemoteTrack
    Support -.-> Room
    Support -.-> AudioModule
    Support -.-> VideoModule
    ObjC -.-> Room
    ObjC -.-> AudioModule

    %% Click Events
    click Room "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LiveKit/Core"
    click SignalClient "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LiveKit/Core"
    click Transport "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LiveKit/Core"
    click LocalTrack "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LiveKit/Track"
    click RemoteTrack "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LiveKit/TrackPublications"
    click AudioModule "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LiveKit/Audio"
    click UIKit "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LiveKit/Views"
    click SwiftUI "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LiveKit/SwiftUI"
    click DataStream "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LiveKit/DataStream"
    click BroadcastManager "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LiveKit/Broadcast"
    click ScreenShare "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LiveKit/Broadcast"
    click Encryption "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LiveKit/E2EE"
    click Protocols "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LiveKit/Protocols"
    click Extensions "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LiveKit/Extensions"
    click Support "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LiveKit/Support"
    click ObjC "https://github.com/livekit/client-sdk-swift/tree/main/Sources/LKObjCHelpers"
    click Package "https://github.com/livekit/client-sdk-swift/blob/main/Package.swift"
    click Podspec "https://github.com/livekit/client-sdk-swift/blob/main/LiveKitClient.podspec"
    click Tests "https://github.com/livekit/client-sdk-swift/tree/main/Tests"
    click CI "https://github.com/livekit/client-sdk-swift/tree/main/.github/workflows"

    %% Styles
    classDef core fill:#ffcccc,stroke:#800000,stroke-width:2px;
    classDef media fill:#ccffcc,stroke:#228B22,stroke-width:2px;
    classDef data fill:#ccccff,stroke:#00008b,stroke-width:2px;
    classDef broadcast fill:#ffe6cc,stroke:#cc6600,stroke-width:2px;
    classDef encryption fill:#e6ccff,stroke:#6600cc,stroke-width:2px;
    classDef protocols fill:#ffffcc,stroke:#cccc00,stroke-width:2px;
    classDef support fill:#dcdcdc,stroke:#808080,stroke-width:2px;
    classDef objc fill:#ffebcc,stroke:#e69500,stroke-width:2px;
    classDef ui fill:#ccffff,stroke:#008080,stroke-width:2px;
    classDef external fill:#f0e68c,stroke:#b8860b,stroke-width:2px;
    classDef testing fill:#ffccff,stroke:#ff00ff,stroke-width:2px;
```
