# xiqi/react-native-live-audio-stream architecture

> Architecture diagram of the GitHub repository xiqi/react-native-live-audio-stream, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/xiqi/react-native-live-audio-stream
- Repository: https://github.com/xiqi/react-native-live-audio-stream

## Overview

##### Main Components
- JavaScript Interface: At the heart of the project is the JS API (index.js / index.d.ts) which provides functions like init, start, stop, and event listeners to receive live audio data.
- Native Bridge – Android Module: Located in android/src/main/java/... (files such as RNLiveAudioStreamModule.java and RNLiveAudioStreamPackage.java) that implement the Android native side of the module, handling the audio recording using Android’s AudioRecord API.
- Native Bridge – iOS Module: Found in the ios directory (RNLiveAudioStream.h, RNLiveAudioStream.m, and Xcode project files) where the iOS portion utilizes Audio Queues (as referenced in the README) to capture live audio.
- React Native Bridge: The communication mechanism that connects the JS code to the respective native modules on both platforms.

##### Component Interactions and Relationships
- The JS layer (React Native component) calls the exposed API methods (init, start, stop). These calls are bridged to the native modules based on the platform.
- On the Android side, the native module intercepts these calls (using classes in the Java directory) and interacts with Android’s low-level audio APIs.
- On the iOS side, similar functionalities are implemented to use the Audio Queues for capturing audio.
- Both native modules emit asynchronous events (audio data) that are sent back through the React Native bridge to the JS layer, which can handle these events (e.g., decoding the base64 data).

##### Architectural Patterns and Design Principles
- Separation of Concerns: The codebase is clearly divided between the cross-platform JS layer and the platform-specific native implementations.
- Bridging/Adapter Pattern: The native modules act as adapters between the audio capture functionalities provided by the underlying operating system and the React Native JavaScript API.
- Event-driven Architecture: The system leverages events to communicate live audio stream data from native layers to the JS layer, ensuring real-time processing.

##### Technologies, Frameworks, and Libraries
- React Native: The overall framework for integrating the module into a mobile app.
- Android APIs (e.g., AudioRecord) and iOS Audio Queues: Underlying native technologies to capture audio.
- Node Package (package.json): Indicates that the module is distributed as an NPM package.
- Podspec and CocoaPods (for iOS): Used for managing iOS dependencies and integration.
- Build configurations: Included in android/build.gradle and Xcode project files.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% JavaScript Interface Layer
    subgraph "JavaScript Interface"
        J1["JS API"]:::js
        J2["Type Declarations"]:::js
        J3["Package Config"]:::js
        J1 --> J2
        J2 --> J3
    end

    %% Native Modules Layer
    subgraph "Native Modules"
        %% Android Native Module
        subgraph "Android Native Module"
            A1["RNLiveAudioStreamModule"]:::android
            A2["RNLiveAudioStreamPackage"]:::android
            A1 --> A2
        end

        %% iOS Native Module
        subgraph "iOS Native Module"
            I1["RNLiveAudioStream.h"]:::ios
            I2["RNLiveAudioStream.m"]:::ios
            I3["RNLiveAudioStream XcodeProj"]:::ios
            I1 --> I2
            I2 --> I3
        end
    end

    %% JS API calls native modules via the React Native Bridge (implicit)
    J1 -->|"bridgeCall"| A1
    J1 -->|"bridgeCall"| I1

    %% Native modules emit audio data events back to JS API
    A2 -->|"audioEvent"| J1
    I3 -->|"audioEvent"| J1

    %% Click Events for JavaScript Interface
    click J1 "https://github.com/xiqi/react-native-live-audio-stream/blob/master/index.js"
    click J2 "https://github.com/xiqi/react-native-live-audio-stream/blob/master/index.d.ts"
    click J3 "https://github.com/xiqi/react-native-live-audio-stream/blob/master/package.json"

    %% Click Events for Android Native Module
    click A1 "https://github.com/xiqi/react-native-live-audio-stream/blob/master/android/src/main/java/com/imxiqi/rnliveaudiostream/RNLiveAudioStreamModule.java"
    click A2 "https://github.com/xiqi/react-native-live-audio-stream/blob/master/android/src/main/java/com/imxiqi/rnliveaudiostream/RNLiveAudioStreamPackage.java"

    %% Click Events for iOS Native Module
    click I1 "https://github.com/xiqi/react-native-live-audio-stream/blob/master/ios/RNLiveAudioStream.h"
    click I2 "https://github.com/xiqi/react-native-live-audio-stream/blob/master/ios/RNLiveAudioStream.m"
    click I3 "https://github.com/xiqi/react-native-live-audio-stream/blob/master/ios/RNLiveAudioStream.xcodeproj"

    %% Style Classes
    classDef js fill:#cce5ff,stroke:#003366,stroke-width:2px;
    classDef android fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef ios fill:#f8d7da,stroke:#721c24,stroke-width:2px;
```
