react-native-twilio-video-webrtc

repository·master·Indexed 20 days ago

https://github.com/blackuy/react-native-twilio-video-webrtc

A React Native wrapper for Twilio Video (WebRTC) that enables video and audio communication on iOS and Android. It provides core components including TwilioVideo for session control, TwilioVideoLocalView for local feeds, and TwilioVideoParticipantView for remote participants. The library supports Expo via a config plugin and includes specific implementations for Android (CustomTwilioVideoView, TwilioVideoPreview, TwilioRemotePreview) and iOS (TwilioVideo, TwilioVideoLocalView, TwilioVideoParticipantView).

Tokens
7.1K
Snippets
23
Records
28
Agent score
71%

What's inside react-native-twilio-video-webrtc

  1. Core components of react-native-twilio-video-webrtc

    master

    The library is built around three primary components that handle different aspects of the WebRTC session:

    • TwilioVideo: The core controller. It is responsible for connecting to rooms, managing the connection lifecycle, delivering events, and handling camera/audio hardware.
    • TwilioVideoLocalView: A UI component used to render the local user's camera feed.
    • TwilioVideoParticipantView: A UI component used to render the camera feed of remote participants.
    import {
      TwilioVideo,
      TwilioVideoLocalView,
      TwilioVideoParticipantView,
    } from "react-native-twilio-video-webrtc";
  2. Configure react-native-twilio-video-webrtc with Expo

    master

    To use this library with Expo, use the provided config plugin. You must also install expo-build-properties.

    Config Plugin Properties

    • cameraPermission: The text shown when requesting camera access.
    • microphonePermission: The text shown when requesting microphone access.
    {
      "name": "my app",
      "plugins": [
        [
          "react-native-twilio-video-webrtc",
          {
            "cameraPermission": "Allow $(PRODUCT_NAME) to access your camera",
            "microphonePermission": "Allow $(PRODUCT_NAME) to access your microphone"
          }
        ]
      ]
    }
    npx expo install expo-build-properties
  3. Configure iOS installation and permissions

    master
    1. Add the package to your Podfile: pod 'react-native-twilio-video-webrtc', path: '../node_modules/react-native-twilio-video-webrtc'
    2. Run pod install.

    Post-install Requirements

    • Set iOS Deployment Target to at least iOS 11.
    • Ensure your Podfile contains platform :ios, '11.0'.

    Permissions

    Add the following to your Info.plist to enable camera and microphone usage:

    • NSCameraUsageDescription
    • NSMicrophoneUsageDescription
    # Podfile
    pod 'react-native-twilio-video-webrtc', path: '../node_modules/react-native-twilio-video-webrtc'
    <!-- Info.plist -->
    <key>NSCameraUsageDescription</key>
    <string>Your message to user when the camera is accessed for the first time</string>
    <key>NSMicrophoneUsageDescription</key>
    <string>Your message to user when the microphone is accessed for the first time</string>
  4. Configure Android installation and permissions

    master

    Installation

    1. Add the library to settings.gradle: include ':react-native-twilio-video-webrtc' project(':react-native-twilio-video-webrtc').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-twilio-video-webrtc/android')
    2. Add the dependency to android/app/build.gradle: implementation project(':react-native-twilio-video-webrtc')
    3. Ensure Java 8 compatibility in android/app/build.gradle: sourceCompatibility 1.8 and targetCompatibility 1.8.
    4. Register the package in MainApplication.java:
      • Import com.twiliorn.library.TwilioPackage.
      • Add new TwilioPackage() to the getPackages() list.

    Permissions

    Add these to AndroidManifest.xml:

    • android.permission.CAMERA
    • android.permission.MODIFY_AUDIO_SETTINGS
    • android.permission.RECORD_AUDIO
    • Hardware features for camera, camera.autofocus, and microphone (set required="false").

    Note: For newer Android versions, use PermissionsAndroid from react-native to request CAMERA and RECORD_AUDIO at runtime.

    // android/app/build.gradle
    dependencies {
        implementation project(':react-native-twilio-video-webrtc')
    }
    
    android {
        compileOptions {
            sourceCompatibility 1.8
            targetCompatibility 1.8
        }
    }
    // MainApplication.java
    import com.twiliorn.library.TwilioPackage;
    
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
            ...
            new TwilioPackage()
        );
    }
    <!-- AndroidManifest.xml -->
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-feature android:name="android.hardware.camera" android:required="false" />
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
    <uses-feature android:name="android.hardware.microphone" android:required="false" />
  5. Run the Example Application

    master

    To run the provided example project, follow these steps:

    1. Navigate to the Example directory: cd Example
    2. Install Node dependencies: yarn install
    3. Install iOS dependencies: cd ios && pod install
    4. Open the workspace and run: open Example.xcworkspace
    cd Example && yarn install && cd ios && pod install && open Example.xcworkspace
  6. Migrate from 1.x to 2.x

    master

    If you are upgrading from version 1.x to 2.x, ensure the following changes are applied:

    1. Update Pod Dependencies: Ensure your pod dependencies are updated. If manually specifying a version, use: s.dependency 'TwilioVideo', '~> 2.2.0'

    2. Identifier Changes: The trackId field has been removed. Use trackSid instead.

      • Participants and tracks are now uniquely identified by their sid and trackSid fields.
      • TwilioVideoParticipantView now expects participantSid and videoTrackSid keys in the trackIdentity prop (replacing the old identity and trackId).
    3. Event Listeners: Switch from listening to onParticipant{Enabled/Disabled}Track to onParticipant{Added/Removed}VideoTrack.

    s.dependency 'TwilioVideo', '~> 2.2.0'
  7. Install react-native-twilio-video-webrtc

    master

    Install the package using your preferred package manager. Note the version requirements based on your React Native version:

    • For react-native >= 0.40.0: install react-native-twilio-video-webrtc@1.0.1
    • For react-native < 0.40.0: install react-native-twilio-video-webrtc@1.0.0
    # Using yarn
    yarn add react-native-twilio-video-webrtc
    
    # Using npm
    npm install react-native-twilio-video-webrtc
  8. Troubleshoot Android build issues (DexIndexOverflow and Proguard)

    master

    DexIndexOverflowException

    If your Android build fails due to symbol thresholds, enable jumboMode in app/build.gradle:

    android {
      dexOptions {
        jumboMode true
      }
    }

    Proguard Configuration

    If using Proguard, prevent the library symbols from being stripped by adding these rules to proguard-rules.pro:

    -keep class com.twilio.** { *; }
    -keep class tvi.webrtc.** { *; }
  9. Use TwilioVideo components in a React Native application

    master

    To build a video calling interface, use TwilioVideo as a controller (typically via a ref) to trigger connection actions like connect() and disconnect(). Listen to lifecycle events on TwilioVideo to manage your application state. Use TwilioVideoLocalView for the user's own video and TwilioVideoParticipantView to display remote participants by passing their track identifiers.

    import React, { Component, useRef, useState } from "react";
    import {
      TwilioVideoLocalView,
      TwilioVideoParticipantView,
      TwilioVideo,
    } from "react-native-twilio-video-webrtc";
    
    const Example = (props) => {
      const [status, setStatus] = useState("disconnected");
      const [videoTracks, setVideoTracks] = useState(new Map());
      const [token, setToken] = useState("");
      const twilioRef = useRef(null);
    
      const _onConnectButtonPress = () => {
        twilioRef.current.connect({ accessToken: token });
        setStatus("connecting");
      };
    
      const _onEndButtonPress = () => {
        twilioRef.current.disconnect();
      };
    
      const _onMuteButtonPress = () => {
        twilioRef.current
          .setLocalAudioEnabled(!isAudioEnabled)
          .then((isEnabled) => setIsAudioEnabled(isEnabled));
      };
    
      const _onFlipButtonPress = () => {
        twilioRef.current.flipCamera();
      };
    
      const _onRoomDidConnect = ({ roomName, error }) => {
        setStatus("connected");
      };
    
      const _onParticipantAddedVideoTrack = ({ participant, track }) => {
        setVideoTracks((originalVideoTracks) => {
          originalVideoTracks.set(track.trackSid, {
            participantSid: participant.sid,
            videoTrackSid: track.trackSid,
          });
          return new Map(originalVideoTracks);
        });
      };
    
      return (
        <View>
          {/* ... UI Logic ... */}
          <TwilioVideo
            ref={twilioRef}
            onRoomDidConnect={_onRoomDidConnect}
            onRoomDidDisconnect={_onRoomDidDisconnect}
            onRoomDidFailToConnect={_onRoomDidFailToConnect}
            onParticipantAddedVideoTrack={_onParticipantAddedVideoTrack}
            onParticipantRemovedVideoTrack={_onParticipantRemovedVideoTrack}
          />
        </View>
      );
    };
  10. Use CustomTwilioVideoView (Android)

    master

    The CustomTwilioVideoView component is used on Android to manage room connections and participant events. It provides callbacks for room lifecycle events (connect, disconnect, fail), participant events (added/removed audio/video/data tracks), and media state changes (camera switched, video/audio toggled).

    // Example of available props for CustomTwilioVideoView on Android
    <CustomTwilioVideoView
      onRoomDidConnect={({ roomName, participants, localParticipant }) => { ... }}
      onParticipantAddedVideoTrack={({ participant, track, enabled }) => { ... }}
      onDominantSpeakerDidChange={({ participant, room }) => { ... }}
      // ... other callbacks
    />