diff --git a/.gitignore b/.gitignore index 1e5b40d..9380bc8 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ DerivedData *.hmap *.ipa *.xcuserstate +ios/.xcode.env.local project.xcworkspace # Android/IJ @@ -40,6 +41,9 @@ project.xcworkspace local.properties android.iml *.hprof +.cxx/ +*.keystore +!debug.keystore # Cocoapods # @@ -67,3 +71,6 @@ yalc.lock # generated by bob lib/ + +# Temporary files created by Metro to check the health of the file watcher +.metro-health-check* \ No newline at end of file diff --git a/.node-version b/.node-version new file mode 100644 index 0000000..25bf17f --- /dev/null +++ b/.node-version @@ -0,0 +1 @@ +18 \ No newline at end of file diff --git a/README.md b/README.md index f906ce7..ae6e7ae 100644 --- a/README.md +++ b/README.md @@ -15,19 +15,69 @@ ### NPM ```sh -npm install @livekit/react-native react-native-webrtc +npm install @livekit/react-native @livekit/react-native-webrtc ``` ### Yarn ```sh -yarn add @livekit/react-native react-native-webrtc +yarn add @livekit/react-native @livekit/react-native-webrtc ``` -This library depends on `react-native-webrtc`, which has additional installation instructions found here: +This library depends on `@livekit/react-native-webrtc`, which has additional installation instructions found here: -- [iOS Installation Guide](https://github.com/react-native-webrtc/react-native-webrtc/blob/master/Documentation/iOSInstallation.md) -- [Android Installation Guide](https://github.com/react-native-webrtc/react-native-webrtc/blob/master/Documentation/AndroidInstallation.md) +- [iOS Installation Guide](https://github.com/livekit/react-native-webrtc/blob/master/Documentation/iOSInstallation.md) +- [Android Installation Guide](https://github.com/livekit/react-native-webrtc/blob/master/Documentation/AndroidInstallation.md) + +---- + +Once the `@livekit/react-native-webrtc` dependency is installed, one last step is needed to finish the installation: + +### Android + +In your [MainApplication.java](https://github.com/livekit/client-sdk-react-native/blob/main/example/android/app/src/main/java/com/example/livekitreactnative/MainApplication.java) file: + +``` +import com.livekit.reactnative.video.SimulcastVideoEncoderFactoryWrapper; +import com.oney.WebRTCModule.WebRTCModuleOptions; +import com.oney.WebRTCModule.webrtcutils.H264AndSoftwareVideoDecoderFactory; + +import org.webrtc.*; + +public class MainApplication extends Application implements ReactApplication { + + @Override + public void onCreate() { + // Place this above any other RN related initialization + WebRTCModuleOptions options = WebRTCModuleOptions.getInstance(); + options.videoEncoderFactory = new SimulcastVideoEncoderFactoryWrapper(null, true, true); + options.videoDecoderFactory = new H264AndSoftwareVideoDecoderFactory(null); + // ... + } +} +``` + +### iOS + +In your [AppDelegate.m](https://github.com/livekit/client-sdk-react-native/blob/main/example/ios/LivekitReactNativeExample/AppDelegate.mm) file: + +``` +#import "WebRTCModule.h" +#import "WebRTCModuleOptions.h" + +@implementation AppDelegate + +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions +{ + // Place this above any other RN related initialization + RTCDefaultVideoEncoderFactory *videoEncoderFactory = [[RTCDefaultVideoEncoderFactory alloc] init]; + RTCVideoEncoderFactorySimulcast *simulcastVideoEncoderFactory = + [[RTCVideoEncoderFactorySimulcast alloc] initWithPrimary:videoEncoderFactory fallback:videoEncoderFactory]; + WebRTCModuleOptions *options = [WebRTCModuleOptions sharedInstance]; + options.videoEncoderFactory = simulcastVideoEncoderFactory; + //... +} +``` ### Expo diff --git a/android/build.gradle b/android/build.gradle index 538b58e..95df44f 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -9,7 +9,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.2.1' + classpath("com.android.tools.build:gradle:7.3.1") // noinspection DifferentKotlinGradleVersion classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } @@ -28,14 +28,15 @@ def getExtOrIntegerDefault(name) { android { compileSdkVersion getExtOrIntegerDefault('compileSdkVersion') + namespace "com.livekit.reactnative" defaultConfig { minSdkVersion 16 targetSdkVersion getExtOrIntegerDefault('targetSdkVersion') versionCode 1 versionName "1.0" - + } - + buildTypes { release { minifyEnabled false @@ -129,5 +130,6 @@ dependencies { api 'com.facebook.react:react-native:+' implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" api 'com.github.davidliu:audioswitch:c498d866c57f1d88056d5e7e7a78d622e3b0c046' + api 'io.github.webrtc-sdk:android:104.5112.09' implementation "androidx.annotation:annotation:1.4.0" } diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index da9702f..8fad3f5 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/android/gradlew b/android/gradlew index 4f906e0..a58591e 100755 --- a/android/gradlew +++ b/android/gradlew @@ -1,7 +1,7 @@ -#!/usr/bin/env sh +#!/bin/sh # -# Copyright 2015 the original author or authors. +# Copyright © 2015-2021 the original authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,67 +17,101 @@ # ############################################################################## -## -## Gradle start up script for UN*X -## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# ############################################################################## # Attempt to set APP_HOME + # Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` +APP_BASE_NAME=${0##*/} # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" +MAX_FD=maximum warn () { echo "$*" -} +} >&2 die () { echo echo "$*" echo exit 1 -} +} >&2 # OS specific support (must be 'true' or 'false'). cygwin=false msys=false darwin=false nonstop=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MINGW* ) - msys=true - ;; - NONSTOP* ) - nonstop=true - ;; +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; esac CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar @@ -87,9 +121,9 @@ CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar if [ -n "$JAVA_HOME" ] ; then if [ -x "$JAVA_HOME/jre/sh/java" ] ; then # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACMD=$JAVA_HOME/jre/sh/java else - JAVACMD="$JAVA_HOME/bin/java" + JAVACMD=$JAVA_HOME/bin/java fi if [ ! -x "$JAVACMD" ] ; then die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME @@ -98,7 +132,7 @@ Please set the JAVA_HOME variable in your environment to match the location of your Java installation." fi else - JAVACMD="java" + JAVACMD=java which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the @@ -106,80 +140,95 @@ location of your Java installation." fi # Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac fi -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. # For Cygwin or MSYS, switch paths to Windows format before running java -if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - - JAVACMD=`cygpath --unix "$JAVACMD"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) fi - i=`expr $i + 1` + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg done - case $i in - 0) set -- ;; - 1) set -- "$args0" ;; - 2) set -- "$args0" "$args1" ;; - 3) set -- "$args0" "$args1" "$args2" ;; - 4) set -- "$args0" "$args1" "$args2" "$args3" ;; - 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac fi -# Escape application args -save () { - for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done - echo " " -} -APP_ARGS=`save "$@"` +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# -# Collect all arguments for the java command, following the shell quoting and substitution rules -eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' -exec "$JAVACMD" "$@" +exec "$JAVACMD" "$@" \ No newline at end of file diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index 20b6ebc..86bb90e 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1,5 +1,4 @@ - + diff --git a/android/src/main/java/com/livekit/reactnative/video/SimulcastVideoEncoderFactoryWrapper.kt b/android/src/main/java/com/livekit/reactnative/video/SimulcastVideoEncoderFactoryWrapper.kt new file mode 100644 index 0000000..ef10024 --- /dev/null +++ b/android/src/main/java/com/livekit/reactnative/video/SimulcastVideoEncoderFactoryWrapper.kt @@ -0,0 +1,249 @@ +package com.livekit.reactnative.video + +import android.util.Log +import org.webrtc.EglBase +import org.webrtc.HardwareVideoEncoderFactory +import org.webrtc.SimulcastVideoEncoderFactory +import org.webrtc.SoftwareVideoEncoderFactory +import org.webrtc.VideoCodecInfo +import org.webrtc.VideoCodecStatus +import org.webrtc.VideoEncoder +import org.webrtc.VideoEncoderFactory +import org.webrtc.VideoEncoderFallback +import org.webrtc.VideoFrame +import org.webrtc.WrappedNativeVideoEncoder +import java.util.concurrent.Callable +import java.util.concurrent.ExecutorService +import java.util.concurrent.Executors + +/* +Copyright 2017, Lyo Kato (Original Author) +Copyright 2017-2021, Shiguredo Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + */ +open class SimulcastVideoEncoderFactoryWrapper( + sharedContext: EglBase.Context?, + enableIntelVp8Encoder: Boolean, + enableH264HighProfile: Boolean +) : VideoEncoderFactory { + + /** + * Factory that prioritizes software encoder. + * + * When the selected codec can't be handled by the software encoder, + * it uses the hardware encoder as a fallback. However, this class is + * primarily used to address an issue in libwebrtc, and does not have + * purposeful usecase itself. + * + * To use simulcast in libwebrtc, SimulcastEncoderAdapter is used. + * SimulcastEncoderAdapter takes in a primary and fallback encoder. + * If HardwareVideoEncoderFactory and SoftwareVideoEncoderFactory are + * passed in directly as primary and fallback, when H.264 is used, + * libwebrtc will crash. + * + * This is because SoftwareVideoEncoderFactory does not handle H.264, + * so [SoftwareVideoEncoderFactory.createEncoder] returns null, and + * the libwebrtc side does not handle nulls, regardless of whether the + * fallback is actually used or not. + * + * To avoid nulls, we simply pass responsibility over to the HardwareVideoEncoderFactory. + * This results in HardwareVideoEncoderFactory being both the primary and fallback, + * but there aren't any specific problems in doing so. + */ + private class FallbackFactory(private val hardwareVideoEncoderFactory: VideoEncoderFactory) : + VideoEncoderFactory { + + private val softwareVideoEncoderFactory: VideoEncoderFactory = SoftwareVideoEncoderFactory() + + override fun createEncoder(info: VideoCodecInfo): VideoEncoder? { + val softwareEncoder = softwareVideoEncoderFactory.createEncoder(info) + val hardwareEncoder = hardwareVideoEncoderFactory.createEncoder(info) + return if (hardwareEncoder != null && softwareEncoder != null) { + VideoEncoderFallback(hardwareEncoder, softwareEncoder) + } else { + softwareEncoder ?: hardwareEncoder + } + } + + override fun getSupportedCodecs(): Array { + val supportedCodecInfos: MutableList = mutableListOf() + supportedCodecInfos.addAll(softwareVideoEncoderFactory.supportedCodecs) + supportedCodecInfos.addAll(hardwareVideoEncoderFactory.supportedCodecs) + return supportedCodecInfos.toTypedArray() + } + + } + + /** + * Wraps each stream encoder and performs the following: + * - Starts up a single thread + * - When the width/height from [initEncode] doesn't match the frame buffer's, + * scales the frame prior to encoding. + * - Always calls the encoder on the thread. + */ + private class StreamEncoderWrapper(private val encoder: VideoEncoder) : VideoEncoder { + + val executor: ExecutorService = Executors.newSingleThreadExecutor() + var streamSettings: VideoEncoder.Settings? = null + + override fun initEncode( + settings: VideoEncoder.Settings, + callback: VideoEncoder.Callback? + ): VideoCodecStatus { + streamSettings = settings + val future = executor.submit(Callable { + Log.i( + "SimulVideoEncoderFac", + """initEncode() thread=${Thread.currentThread().name} [${Thread.currentThread().id}] + | encoder=${encoder.implementationName} + | streamSettings: + | numberOfCores=${settings.numberOfCores} + | width=${settings.width} + | height=${settings.height} + | startBitrate=${settings.startBitrate} + | maxFramerate=${settings.maxFramerate} + | automaticResizeOn=${settings.automaticResizeOn} + | numberOfSimulcastStreams=${settings.numberOfSimulcastStreams} + | lossNotification=${settings.capabilities.lossNotification} + """.trimMargin() + ) + return@Callable encoder.initEncode(settings, callback) + }) + return future.get() + } + + override fun release(): VideoCodecStatus { + val future = executor.submit(Callable { return@Callable encoder.release() }) + return future.get() + } + + override fun encode( + frame: VideoFrame, + encodeInfo: VideoEncoder.EncodeInfo? + ): VideoCodecStatus { + val future = executor.submit(Callable { + //LKLog.d { "encode() buffer=${frame.buffer}, thread=${Thread.currentThread().name} " + + // "[${Thread.currentThread().id}]" } + if (streamSettings == null) { + return@Callable encoder.encode(frame, encodeInfo) + } else if (frame.buffer.width == streamSettings!!.width) { + return@Callable encoder.encode(frame, encodeInfo) + } else { + // The incoming buffer is different than the streamSettings received in initEncode() + // Need to scale. + val originalBuffer = frame.buffer + // TODO: Do we need to handle when the scale factor is weird? + val adaptedBuffer = originalBuffer.cropAndScale( + 0, 0, originalBuffer.width, originalBuffer.height, + streamSettings!!.width, streamSettings!!.height + ) + val adaptedFrame = VideoFrame(adaptedBuffer, frame.rotation, frame.timestampNs) + val result = encoder.encode(adaptedFrame, encodeInfo) + adaptedBuffer.release() + return@Callable result + } + }) + return future.get() + } + + override fun setRateAllocation( + allocation: VideoEncoder.BitrateAllocation?, + frameRate: Int + ): VideoCodecStatus { + val future = executor.submit(Callable { + return@Callable encoder.setRateAllocation( + allocation, + frameRate + ) + }) + return future.get() + } + + override fun getScalingSettings(): VideoEncoder.ScalingSettings { + val future = executor.submit(Callable { return@Callable encoder.scalingSettings }) + return future.get() + } + + override fun getImplementationName(): String { + val future = executor.submit(Callable { return@Callable encoder.implementationName }) + return future.get() + } + + override fun createNativeVideoEncoder(): Long { + val future = executor.submit(Callable { return@Callable encoder.createNativeVideoEncoder() }) + return future.get() + } + + override fun isHardwareEncoder(): Boolean { + val future = executor.submit(Callable { return@Callable encoder.isHardwareEncoder }) + return future.get() + } + + override fun setRates(rcParameters: VideoEncoder.RateControlParameters?): VideoCodecStatus { + val future = executor.submit(Callable { return@Callable encoder.setRates(rcParameters) }) + return future.get() + } + + override fun getResolutionBitrateLimits(): Array { + val future = executor.submit(Callable { return@Callable encoder.resolutionBitrateLimits }) + return future.get() + } + + override fun getEncoderInfo(): VideoEncoder.EncoderInfo { + val future = executor.submit(Callable { return@Callable encoder.encoderInfo }) + return future.get() + } + } + + private class StreamEncoderWrapperFactory(private val factory: VideoEncoderFactory) : + VideoEncoderFactory { + override fun createEncoder(videoCodecInfo: VideoCodecInfo?): VideoEncoder? { + val encoder = factory.createEncoder(videoCodecInfo) + if (encoder == null) { + return null + } + if (encoder is WrappedNativeVideoEncoder) { + return encoder + } + return StreamEncoderWrapper(encoder) + } + + override fun getSupportedCodecs(): Array { + return factory.supportedCodecs + } + } + + + private val primary: VideoEncoderFactory + private val fallback: VideoEncoderFactory + private val native: SimulcastVideoEncoderFactory + + init { + val hardwareVideoEncoderFactory = HardwareVideoEncoderFactory( + sharedContext, enableIntelVp8Encoder, enableH264HighProfile + ) + primary = StreamEncoderWrapperFactory(hardwareVideoEncoderFactory) + fallback = StreamEncoderWrapperFactory(FallbackFactory(primary)) + native = SimulcastVideoEncoderFactory(primary, fallback) + } + + override fun createEncoder(info: VideoCodecInfo?): VideoEncoder? { + return native.createEncoder(info) + } + + override fun getSupportedCodecs(): Array { + return native.supportedCodecs + } + +} \ No newline at end of file diff --git a/docs/assets/highlight.css b/docs/assets/highlight.css index 0568466..32acdf8 100644 --- a/docs/assets/highlight.css +++ b/docs/assets/highlight.css @@ -5,28 +5,28 @@ --dark-hl-1: #C586C0; --light-hl-2: #001080; --dark-hl-2: #9CDCFE; - --light-hl-3: #A31515; - --dark-hl-3: #CE9178; + --light-hl-3: #0000FF; + --dark-hl-3: #569CD6; --light-hl-4: #008000; --dark-hl-4: #6A9955; - --light-hl-5: #795E26; - --dark-hl-5: #DCDCAA; - --light-hl-6: #0000FF; - --dark-hl-6: #569CD6; - --light-hl-7: #0070C1; - --dark-hl-7: #4FC1FF; - --light-hl-8: #098658; - --dark-hl-8: #B5CEA8; - --light-hl-9: #800000; - --dark-hl-9: #808080; - --light-hl-10: #267F99; - --dark-hl-10: #4EC9B0; - --light-hl-11: #FF0000; - --dark-hl-11: #9CDCFE; - --light-hl-12: #000000FF; - --dark-hl-12: #D4D4D4; - --light-hl-13: #000000; - --dark-hl-13: #C8C8C8; + --light-hl-5: #A31515; + --dark-hl-5: #CE9178; + --light-hl-6: #0070C1; + --dark-hl-6: #4FC1FF; + --light-hl-7: #000000; + --dark-hl-7: #C8C8C8; + --light-hl-8: #795E26; + --dark-hl-8: #DCDCAA; + --light-hl-9: #098658; + --dark-hl-9: #B5CEA8; + --light-hl-10: #800000; + --dark-hl-10: #808080; + --light-hl-11: #267F99; + --dark-hl-11: #4EC9B0; + --light-hl-12: #FF0000; + --dark-hl-12: #9CDCFE; + --light-hl-13: #000000FF; + --dark-hl-13: #D4D4D4; --light-code-background: #FFFFFF; --dark-code-background: #1E1E1E; } diff --git a/docs/classes/AudioSession.html b/docs/classes/AudioSession.html index 1a26b4a..98dc27a 100644 --- a/docs/classes/AudioSession.html +++ b/docs/classes/AudioSession.html @@ -1,16 +1,16 @@ -AudioSession | LiveKit React Native Client SDK - v1.0.1
+AudioSession | LiveKit React Native Client SDK - v1.1.0
+
  • The search index is not available
  • LiveKit React Native Client SDK - v1.1.0
    @@ -18,7 +18,7 @@

    Hierarchy

    • AudioSession
    +
  • Defined in audio/AudioSession.ts:50
  • @@ -62,7 +62,7 @@

    Parameters

    config: AudioConfiguration

    Returns Promise<void>

    +
  • Defined in audio/AudioSession.ts:56
    • @@ -94,7 +94,7 @@

      Returns

      the available audio output types

    Returns Promise<string[]>

    +
  • Defined in audio/AudioSession.ts:101
    • @@ -112,7 +112,7 @@
      deviceId: string

    Returns Promise<void>

    +
  • Defined in audio/AudioSession.ts:118
  • +
  • Defined in audio/AudioSession.ts:127
  • +
  • Defined in audio/AudioSession.ts:63
  • +
  • Defined in audio/AudioSession.ts:70
  • +
  • The search index is not available
  • LiveKit React Native Client SDK - v1.1.0
    @@ -24,7 +24,7 @@

    Parameters

    __namedParameters: Props

    Returns Element

    +
  • Defined in components/VideoView.tsx:24
  • +
  • The search index is not available
  • LiveKit React Native Client SDK - v1.1.0
    @@ -22,7 +22,7 @@

    Function registerGlobals

    Returns void

    +
  • Defined in index.tsx:13
  • +
  • The search index is not available
  • LiveKit React Native Client SDK - v1.1.0
    @@ -35,7 +35,7 @@
    participants: defaultOptional localParticipant: default

    Returns void

    +
  • Defined in useRoom.ts:111
  • +
  • The search index is not available
  • LiveKit React Native Client SDK - v1.1.0
    @@ -24,7 +24,7 @@

    Parameters

    participant: default

    Returns ParticipantState

    +
  • Defined in useParticipant.ts:23
  • +
  • The search index is not available
  • LiveKit React Native Client SDK - v1.1.0
    @@ -26,7 +26,7 @@
    room: Room
    Optional options: RoomOptions

    Returns RoomState

    +
  • Defined in useRoom.ts:26
  • +
  • The search index is not available
  • LiveKit React Native Client SDK - v1.1.0
    -

    LiveKit React Native Client SDK - v1.0.1

    +

    LiveKit React Native Client SDK - v1.1.0

    @@ -32,19 +32,35 @@

    Installation

    NPM

    -
    npm install @livekit/react-native react-native-webrtc
    +
    npm install @livekit/react-native @livekit/react-native-webrtc
     

    Yarn

    -
    yarn add @livekit/react-native react-native-webrtc
    +
    yarn add @livekit/react-native @livekit/react-native-webrtc
     
    -

    This library depends on react-native-webrtc, which has additional installation instructions found here:

    +

    This library depends on @livekit/react-native-webrtc, which has additional installation instructions found here:

    +
    +

    Once the @livekit/react-native-webrtc dependency is installed, one last step is needed to finish the installation:

    + + +

    Android

    +
    +

    In your MainApplication.java file:

    +
    import com.livekit.reactnative.video.SimulcastVideoEncoderFactoryWrapper;
    import com.oney.WebRTCModule.WebRTCModuleOptions;
    import com.oney.WebRTCModule.webrtcutils.H264AndSoftwareVideoDecoderFactory;

    import org.webrtc.*;

    public class MainApplication extends Application implements ReactApplication {

    @Override
    public void onCreate() {
    // Place this above any other RN related initialization
    WebRTCModuleOptions options = WebRTCModuleOptions.getInstance();
    options.videoEncoderFactory = new SimulcastVideoEncoderFactoryWrapper(null, true, true);
    options.videoDecoderFactory = new H264AndSoftwareVideoDecoderFactory(null);
    // ...
    }
    } +
    + + +

    iOS

    +
    +

    In your AppDelegate.m file:

    +
    #import "WebRTCModule.h"
    #import "WebRTCModuleOptions.h"

    @implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    // Place this above any other RN related initialization
    RTCDefaultVideoEncoderFactory *videoEncoderFactory = [[RTCDefaultVideoEncoderFactory alloc] init];
    RTCVideoEncoderFactorySimulcast *simulcastVideoEncoderFactory =
    [[RTCVideoEncoderFactorySimulcast alloc] initWithPrimary:videoEncoderFactory fallback:videoEncoderFactory];
    WebRTCModuleOptions *options = [WebRTCModuleOptions sharedInstance];
    options.videoEncoderFactory = simulcastVideoEncoderFactory;
    //...
    } +

    Expo

    @@ -61,10 +77,10 @@

    Usage

    In your index.js file, setup the LiveKit SDK by calling registerGlobals(). This sets up the required WebRTC libraries for use in Javascript, and is needed for LiveKit to work.

    -
    import { registerGlobals } from '@livekit/react-native';

    // ...

    registerGlobals(); +
    import { registerGlobals } from '@livekit/react-native';

    // ...

    registerGlobals();

    A Room object can then be created and connected to.

    -
    import { Participant, Room, Track } from 'livekit-client';
    import { useRoom, AudioSession, VideoView } from '@livekit/react-native';

    /*...*/

    // Create a room state
    const [room] = useState(() => new Room());

    // Get the participants from the room
    const { participants } = useRoom(room);

    useEffect(() => {
    AudioSession.startAudioSession();
    room.connect(url, token, {});
    return () => {
    room.disconnect();
    AudioSession.stopAudioSession();
    };
    }, [url, token, room]);

    const videoView = participants.length > 0 && (
    <VideoView
    style={{ flex: 1, width: '100%' }}
    videoTrack={participants[0].getTrack(Track.Source.Camera)?.videoTrack}
    />
    ); +
    import { Participant, Room, Track } from 'livekit-client';
    import { useRoom, AudioSession, VideoView } from '@livekit/react-native';

    /*...*/

    // Create a room state
    const [room] = useState(() => new Room());

    // Get the participants from the room
    const { participants } = useRoom(room);

    useEffect(() => {
    AudioSession.startAudioSession();
    room.connect(url, token, {});
    return () => {
    room.disconnect();
    AudioSession.stopAudioSession();
    };
    }, [url, token, room]);

    const videoView = participants.length > 0 && (
    <VideoView
    style={{ flex: 1, width: '100%' }}
    videoTrack={participants[0].getTrack(Track.Source.Camera)?.videoTrack}
    />
    );

    API documentation is located here.

    Additional documentation for the LiveKit SDK can be found at https://docs.livekit.io/references/client-sdks/

    @@ -74,17 +90,17 @@

    Screenshare

    Enabling screenshare requires extra installation steps:

    - +

    Android

    Android screenshare requires a foreground service with type mediaProjection to be present.

    The example app uses @voximplant/react-native-foreground-service for this. Ensure that the service is labelled a mediaProjection service like so:

    -
    <service android:name="com.voximplant.foregroundservice.VIForegroundService"
    android:foregroundServiceType="mediaProjection" /> +
    <service android:name="com.voximplant.foregroundservice.VIForegroundService"
    android:foregroundServiceType="mediaProjection" />

    Once setup, start the foreground service prior to using screenshare.

    - +

    iOS

    iOS screenshare requires adding a Broadcast Extension to your iOS project. Follow the integration instructions here:

    @@ -96,7 +112,7 @@

    iOS

    Due to background app processing limitations, screen recording may be interrupted if the app is restricted in the background. Registering with CallKit allows the app to continue processing for the duration of the call.

    Once setup, iOS screenshare can be initiated like so:

    -
    const screenCaptureRef = React.useRef(null);
    const screenCapturePickerView = Platform.OS === 'ios' && (
    <ScreenCapturePickerView ref={screenCaptureRef} />
    );
    const startBroadcast = async () => {
    if (Platform.OS === 'ios') {
    const reactTag = findNodeHandle(screenCaptureRef.current);
    await NativeModules.ScreenCapturePickerViewManager.show(reactTag);
    room.localParticipant.setScreenShareEnabled(true);
    } else {
    room.localParticipant.setScreenShareEnabled(true);
    }
    };

    return (
    <View style={styles.container}>
    /*...*/ // Make sure the ScreenCapturePickerView exists in the view tree.
    {screenCapturePickerView}
    </View>
    ); +
    const screenCaptureRef = React.useRef(null);
    const screenCapturePickerView = Platform.OS === 'ios' && (
    <ScreenCapturePickerView ref={screenCaptureRef} />
    );
    const startBroadcast = async () => {
    if (Platform.OS === 'ios') {
    const reactTag = findNodeHandle(screenCaptureRef.current);
    await NativeModules.ScreenCapturePickerViewManager.show(reactTag);
    room.localParticipant.setScreenShareEnabled(true);
    } else {
    room.localParticipant.setScreenShareEnabled(true);
    }
    };

    return (
    <View style={styles.container}>
    /*...*/ // Make sure the ScreenCapturePickerView exists in the view tree.
    {screenCapturePickerView}
    </View>
    );
    @@ -140,7 +156,7 @@

    Theme

    • Preparing search index...
    • -
    • The search index is not available
    LiveKit React Native Client SDK - v1.0.1
    +
  • The search index is not available
  • LiveKit React Native Client SDK - v1.1.0
    @@ -18,7 +18,7 @@

    Hierarchy

    • ParticipantState
    +
  • Defined in useParticipant.ts:11
  • @@ -42,47 +42,47 @@

    Properties

    cameraPublication?: TrackPublication
    +
  • Defined in useParticipant.ts:18
  • connectionQuality: ConnectionQuality
    +
  • Defined in useParticipant.ts:13
  • isLocal: boolean
    +
  • Defined in useParticipant.ts:14
  • isSpeaking: boolean
    +
  • Defined in useParticipant.ts:12
  • metadata?: string
    +
  • Defined in useParticipant.ts:15
  • microphonePublication?: TrackPublication
    +
  • Defined in useParticipant.ts:19
  • publications: TrackPublication[]
    +
  • Defined in useParticipant.ts:16
  • screenSharePublication?: TrackPublication
    +
  • Defined in useParticipant.ts:20
  • subscribedTracks: TrackPublication[]
    +
  • Defined in useParticipant.ts:17
  • +
  • The search index is not available
  • LiveKit React Native Client SDK - v1.1.0
    @@ -18,7 +18,7 @@

    Hierarchy

    • RoomOptions
    +
  • Defined in useRoom.ts:22
  • @@ -47,7 +47,7 @@

    Parameters

    participants: default[]

    Returns void

    +
  • Defined in useRoom.ts:23
  • +
  • The search index is not available
  • LiveKit React Native Client SDK - v1.1.0
    @@ -18,7 +18,7 @@

    Hierarchy

    • RoomState
    +
  • Defined in useRoom.ts:13
  • @@ -37,22 +37,22 @@

    Properties

    audioTracks: AudioTrack[]
    +
  • Defined in useRoom.ts:18
  • error?: Error
    +
  • Defined in useRoom.ts:19
  • participants: default[]
    +
  • Defined in useRoom.ts:16
  • room?: Room
    +
  • Defined in useRoom.ts:14
  • +
  • The search index is not available
  • LiveKit React Native Client SDK - v1.1.0
    -

    LiveKit React Native Client SDK - v1.0.1

    +

    LiveKit React Native Client SDK - v1.1.0

    Index

    @@ -52,7 +52,7 @@

    Theme

    • Preparing search index...
    • -
    • The search index is not available
    LiveKit React Native Client SDK - v1.0.1 +
  • The search index is not available
  • LiveKit React Native Client SDK - v1.1.0
    AudioConfiguration: { android: { preferredOutputList: ("speaker" | "earpiece" | "headset" | "bluetooth")[] }; ios: { defaultOutput: "speaker" | "earpiece" } }
    @@ -51,7 +51,7 @@
    ios:
    defaultOutput: "speaker" | "earpiece"
    +
  • Defined in audio/AudioSession.ts:41
  • +
  • The search index is not available
  • LiveKit React Native Client SDK - v1.1.0
    Props: { mirror?: boolean; objectFit?: "cover" | "contain"; style?: ViewStyle; videoTrack?: VideoTrack; zOrder?: number }
    @@ -28,7 +28,7 @@
    Optional videoTrack
    Optional zOrder?: number
    +
  • Defined in components/VideoView.tsx:16