diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index beaebe9..8a44c38 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -51,16 +51,26 @@ jobs: - name: Set up Android NDK if: matrix.platform == 'android' + id: setup-ndk uses: nttld/setup-ndk@v1 with: - ndk-version: r26d + ndk-version: r27d + + - name: Export NDK path + if: matrix.platform == 'android' + run: echo "ANDROID_NDK_ROOT=${{ steps.setup-ndk.outputs.ndk-path }}" >> "$GITHUB_ENV" + + - name: Install Android SDK build dependencies + if: matrix.platform == 'android' + run: sudo apt-get update && sudo apt-get install -y ninja-build - name: Cache dependencies uses: actions/cache@v4 with: path: | godot-cpp - key: deps-${{ matrix.platform }}-${{ matrix.arch || matrix.platform }}-${{ hashFiles('build.sh') }} + livekit-sdk + key: deps-${{ matrix.platform }}-${{ matrix.arch || matrix.platform }}-${{ hashFiles('build.sh', 'patches/**') }} - name: Build GDExtension shell: bash @@ -180,22 +190,131 @@ jobs: - name: Run tests run: | - # Join tokens for the integration tests, signed with the dev server's key. - token() { - python3 - "$1" <<'PY' - import base64, hashlib, hmac, json, sys, time - def b64(b): return base64.urlsafe_b64encode(b).rstrip(b"=").decode() - now = int(time.time()) - claims = {"iss": "devkey", "sub": sys.argv[1], "iat": now, "nbf": now, "exp": now + 3600, - "video": {"room": "web-integration", "roomJoin": True, "canPublish": True, - "canSubscribe": True, "canPublishData": True}} - signing = b64(json.dumps({"alg": "HS256", "typ": "JWT"}).encode()) + "." + b64(json.dumps(claims).encode()) - print(signing + "." + b64(hmac.new(b"secret", signing.encode(), hashlib.sha256).digest())) - PY - } export LIVEKIT_TEST_URL=ws://127.0.0.1:7880 - export LIVEKIT_TOKEN_1=$(token web-user-1) - export LIVEKIT_TOKEN_2=$(token web-user-2) + export LIVEKIT_TOKEN_1=$(python3 test/dev_token.py web-user-1 web-integration) + export LIVEKIT_TOKEN_2=$(python3 test/dev_token.py web-user-2 web-integration) export RUN_E2E=1 export LIVEKIT_E2E_URL=ws://127.0.0.1:7880 ./test_web.sh + + android: + name: Test on Android (x86_64 emulator) + runs-on: ubuntu-latest + env: + GODOT_VERSION: "4.5.2" + LIVEKIT_SERVER_VERSION: "1.13.7" + # The emulator reaches the host's localhost at 10.0.2.2. + LIVEKIT_TEST_URL: ws://10.0.2.2:7880 + LIVEKIT_E2E_URL: ws://10.0.2.2:7880 + RUN_E2E: "1" + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.x" + + - name: Install SCons + run: pip install scons + + - name: Set up Android NDK + id: setup-ndk + uses: nttld/setup-ndk@v1 + with: + ndk-version: r27d + + - name: Export NDK path + run: echo "ANDROID_NDK_ROOT=${{ steps.setup-ndk.outputs.ndk-path }}" >> "$GITHUB_ENV" + + - name: Install Android SDK build dependencies + run: sudo apt-get update && sudo apt-get install -y ninja-build + + - name: Cache dependencies + uses: actions/cache@v4 + with: + path: | + godot-cpp + livekit-sdk + key: deps-android-x86_64-${{ hashFiles('build.sh', 'patches/**') }} + + # x86_64, since GitHub's runners can only emulate their own architecture at a usable speed. + - name: Build GDExtension + run: ./build.sh android x86_64 + + - name: Set up Java + uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: "17" + + - name: Cache Godot and export templates + id: cache-godot + uses: actions/cache@v4 + with: + path: | + godot + ~/.local/share/godot/export_templates + key: godot-${{ env.GODOT_VERSION }}-android-templates + + - name: Download Godot and Android export templates + if: steps.cache-godot.outputs.cache-hit != 'true' + run: | + wget -q https://github.com/godotengine/godot/releases/download/${GODOT_VERSION}-stable/Godot_v${GODOT_VERSION}-stable_linux.x86_64.zip + unzip -q Godot_v${GODOT_VERSION}-stable_linux.x86_64.zip + mv Godot_v${GODOT_VERSION}-stable_linux.x86_64 godot + chmod +x godot + wget -q https://github.com/godotengine/godot/releases/download/${GODOT_VERSION}-stable/Godot_v${GODOT_VERSION}-stable_export_templates.tpz + templates=~/.local/share/godot/export_templates/${GODOT_VERSION}.stable + mkdir -p "$templates" + unzip -q -j Godot_v${GODOT_VERSION}-stable_export_templates.tpz 'templates/android_*' 'templates/version.txt' -d "$templates" + + - name: Cache Gradle + uses: actions/cache@v4 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: gradle-godot-${{ env.GODOT_VERSION }} + + - name: Install GUT + run: | + git clone --depth 1 --branch v9.5.0 https://github.com/bitwes/Gut.git /tmp/gut + mkdir -p addons + cp -r /tmp/gut/addons/gut addons/gut + + - name: Start LiveKit server + run: | + wget -q https://github.com/livekit/livekit/releases/download/v${LIVEKIT_SERVER_VERSION}/livekit_${LIVEKIT_SERVER_VERSION}_linux_amd64.tar.gz + tar -xzf livekit_${LIVEKIT_SERVER_VERSION}_linux_amd64.tar.gz livekit-server + ./livekit-server --dev > livekit-server.log 2>&1 & + + - name: Create join tokens + run: | + echo "LIVEKIT_TOKEN_1=$(python3 test/dev_token.py android-user-1 android-integration)" >> "$GITHUB_ENV" + echo "LIVEKIT_TOKEN_2=$(python3 test/dev_token.py android-user-2 android-integration)" >> "$GITHUB_ENV" + + # Before starting the emulator, since both need a lot of CPU. + - name: Export test app + run: ./test_android.sh --export-only build/android-tests.apk --arch x86_64 + + - name: Enable KVM + run: | + echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules + sudo udevadm control --reload-rules + sudo udevadm trigger --name-match=kvm + + - name: Run tests + uses: reactivecircus/android-emulator-runner@v2 + with: + api-level: 34 + target: google_apis + arch: x86_64 + cores: 4 + ram-size: 4096M + # The action's defaults, but with audio, which the tests play. + emulator-options: -no-window -gpu swiftshader_indirect -no-snapshot -no-boot-anim + disable-animations: true + script: ./test_android.sh --apk build/android-tests.apk diff --git a/.github/workflows/build_release.yml b/.github/workflows/build_release.yml index fc9c425..ab0e43b 100644 --- a/.github/workflows/build_release.yml +++ b/.github/workflows/build_release.yml @@ -68,7 +68,11 @@ jobs: id: setup-ndk uses: nttld/setup-ndk@v1 with: - ndk-version: r26d + ndk-version: r27d + + - name: Install Android SDK build dependencies + if: matrix.platform == 'android' + run: sudo apt-get update && sudo apt-get install -y ninja-build - name: Set up Emscripten if: matrix.platform == 'web' @@ -86,7 +90,8 @@ jobs: with: path: | godot-cpp - key: deps-${{ matrix.platform }}-${{ matrix.arch || matrix.platform }}-${{ hashFiles('build.sh') }} + livekit-sdk + key: deps-${{ matrix.platform }}-${{ matrix.arch || matrix.platform }}-${{ hashFiles('build.sh', 'patches/**') }} - name: Build GDExtension shell: bash diff --git a/.gitignore b/.gitignore index 0742525..63235ec 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ export_presets.cfg # Build artifacts and downloaded SDKs godot-cpp/ livekit-sdk/ +livekit-sdk-build/ livekit-js/ src/web/gen/ *.wasm @@ -38,3 +39,6 @@ test_results_*.xml # Godot UID files *.uid + +# Android build template, installed by test_android.sh +/android/ diff --git a/README.md b/README.md index c93a784..5bc71fb 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,8 @@ If you have [GUT (Godot Unit Test)](https://github.com/bitwes/Gut) installed in `./test_web.sh` runs the tests that apply to the web build in headless Chrome. It requires a web build, Godot's web export templates, and `google-chrome`. +`./test_android.sh` runs the tests on a connected Android device or emulator. It requires an Android build for the device (`./build.sh android x86_64` for an x86_64 emulator), Godot's Android export templates, the Android SDK (`ANDROID_HOME`) and a JDK (`JAVA_HOME`). + ## Continuous Integration The project is configured with GitHub Actions to automatically build releases for Linux, Windows, and macOS whenever a new tag (e.g., `v1.0.0`) is pushed. Check the `.github/workflows` directory for details. diff --git a/SConstruct b/SConstruct index 78f363d..0a0f042 100644 --- a/SConstruct +++ b/SConstruct @@ -48,7 +48,7 @@ if is_android: Exit(1) ndk_bin = toolchain_bins[0] ndk_sysroot = os.path.join(os.path.dirname(ndk_bin), 'sysroot') - android_target = 'aarch64-linux-android24' + android_target = 'x86_64-linux-android24' if arch == 'x86_64' else 'aarch64-linux-android24' env = Environment() env.Replace(CC=os.path.join(ndk_bin, 'clang')) env.Replace(CXX=os.path.join(ndk_bin, 'clang++')) @@ -95,7 +95,9 @@ if enable_screen_capture: env.Append(LIBPATH=libpath) if is_android: - env.Append(CCFLAGS=[f'--target={android_target}', f'--sysroot={ndk_sysroot}', '-fPIC', '-march=armv8-a']) + env.Append(CCFLAGS=[f'--target={android_target}', f'--sysroot={ndk_sysroot}', '-fPIC']) + if arch == 'arm64': + env.Append(CCFLAGS=['-march=armv8-a']) env.Append(CXXFLAGS=['-std=c++20']) env.Append(LINKFLAGS=[f'--target={android_target}', f'--sysroot={ndk_sysroot}']) env.Append(CPPDEFINES=['ANDROID_ENABLED', 'UNIX_ENABLED']) @@ -226,6 +228,9 @@ else: # linux, android # file-extension suffix (which causes "should have exactly one target # with the suffix: .dll"). lib_name = f"{env['SHLIBPREFIX']}godot-livekit.{platform}.{arch}{env['SHLIBSUFFIX']}" +if platform == 'windows': + src_files.append('src/windows_pin.cpp') + library = env.SharedLibrary(target=lib_name, source=src_files) installed_library = env.Install('addons/godot-livekit/bin', library) Default(installed_library) \ No newline at end of file diff --git a/addons/godot-livekit/godot-livekit.gdextension b/addons/godot-livekit/godot-livekit.gdextension index d5ef6f3..6549f95 100644 --- a/addons/godot-livekit/godot-livekit.gdextension +++ b/addons/godot-livekit/godot-livekit.gdextension @@ -8,6 +8,7 @@ windows.x86_64 = "res://addons/godot-livekit/bin/libgodot-livekit.windows.x86_64 macos.arm64 = "res://addons/godot-livekit/bin/libgodot-livekit.macos.arm64.dylib" macos.x86_64 = "res://addons/godot-livekit/bin/libgodot-livekit.macos.x86_64.dylib" android.arm64 = "res://addons/godot-livekit/bin/libgodot-livekit.android.arm64.so" +android.x86_64 = "res://addons/godot-livekit/bin/libgodot-livekit.android.x86_64.so" web.wasm32.nothreads = "res://addons/godot-livekit/bin/libgodot-livekit.web.wasm32.nothreads.wasm" [dependencies] @@ -16,3 +17,4 @@ windows.x86_64 = {"res://addons/godot-livekit/bin/livekit_ffi.dll": "", "res://a macos.arm64 = {"res://addons/godot-livekit/bin/macos-arm64/liblivekit_ffi.dylib": "", "res://addons/godot-livekit/bin/macos-arm64/liblivekit.dylib": ""} macos.x86_64 = {"res://addons/godot-livekit/bin/macos-x86_64/liblivekit_ffi.dylib": "", "res://addons/godot-livekit/bin/macos-x86_64/liblivekit.dylib": ""} android.arm64 = {"res://addons/godot-livekit/bin/android-arm64/liblivekit_ffi.so": "", "res://addons/godot-livekit/bin/android-arm64/liblivekit.so": ""} +android.x86_64 = {"res://addons/godot-livekit/bin/android-x86_64/liblivekit_ffi.so": "", "res://addons/godot-livekit/bin/android-x86_64/liblivekit.so": ""} diff --git a/build.sh b/build.sh index a1be71e..bad2a42 100755 --- a/build.sh +++ b/build.sh @@ -15,13 +15,24 @@ NC='\033[0m' # No Color PLATFORM="" ARCH="" SCONS_FLAGS="" -LIVEKIT_VERSION="0.3.1" +LIVEKIT_VERSION="1.13.1" +# LiveKit publishes no Android build of the C++ SDK, so Android builds it from the same release with this patch. +LIVEKIT_ANDROID_PATCH="patches/client-sdk-cpp-android.patch" +# Cross-compiling the SDK needs a protoc for the build machine that matches its vendored protobuf. +PROTOC_VERSION="25.3" +# protoc runs on the build machine, so there's one download per host. +PROTOC_SHA256_LINUX_X86_64="f853e691868d0557425ea290bf7ba6384eef2fa9b04c323afab49a770ba9da80" +PROTOC_SHA256_LINUX_AARCH64="9eae1f20f70cccc912d1c318c3929b86aebf5afd4b0f32c196ef682c222ed5ae" +PROTOC_SHA256_MACOS="c06b6c5527c9b11a30a9c103400ee59eb6882f1766ea786fe4bdbb7f55ee5e6f" +# webrtc-sys needs NDK r27's libc++. +MIN_ANDROID_NDK_MAJOR=27 GODOT_CPP_VERSION="godot-4.5-stable" FRAMETAP_VERSION="0.1.3" LIVEKIT_JS_VERSION="2.24.0" LIVEKIT_JS_SHA256="88b41eb0905a66dd3f46009c9a024aa72503b53398ae9a70c1f60258d246d262" SKIP_FRAMETAP=false SKIP_LIVEKIT_SDK=false +BUILD_LIVEKIT_SDK=false # Verify a downloaded file's SHA-256 hash (if a hash is provided). verify_checksum() { @@ -57,7 +68,7 @@ show_usage() { echo " linux: Build for Linux (x86_64)" echo " macos [arm64|x86_64]: Build for macOS (defaults to host arch)" echo " windows: Build for Windows (x86_64, cross-compile)" - echo " android [arm64]: Build for Android (arm64, requires ANDROID_NDK_ROOT)" + echo " android [arm64|x86_64]: Build for Android (defaults to arm64, requires ANDROID_NDK_ROOT)" echo " web: Build for the web (wasm32, single-threaded, requires emcc from emsdk on PATH)" echo " --debug: Build debug variant (template_debug) instead of release" exit 1 @@ -68,8 +79,8 @@ setup_linux() { PLATFORM="linux" ARCH="x86_64" SCONS_FLAGS="platform=linux arch=x86_64" - LIVEKIT_ARCHIVE="livekit-sdk-linux-x64-${LIVEKIT_VERSION}.tar.gz" - LIVEKIT_URL="https://github.com/krazyjakee/client-sdk-cpp/releases/download/v${LIVEKIT_VERSION}/${LIVEKIT_ARCHIVE}" + LIVEKIT_ARCHIVE="livekit-sdk-ubuntu-22.04-x64-${LIVEKIT_VERSION}.tar.gz" + LIVEKIT_URL="https://github.com/livekit/client-sdk-cpp/releases/download/v${LIVEKIT_VERSION}/${LIVEKIT_ARCHIVE}" echo -e "${BLUE}=== Godot-LiveKit Local Build Script (Linux) ===${NC}" } @@ -89,7 +100,7 @@ setup_macos() { local sdk_arch="arm64" [ "$ARCH" = "x86_64" ] && sdk_arch="x64" LIVEKIT_ARCHIVE="livekit-sdk-macos-${sdk_arch}-${LIVEKIT_VERSION}.tar.gz" - LIVEKIT_URL="https://github.com/krazyjakee/client-sdk-cpp/releases/download/v${LIVEKIT_VERSION}/${LIVEKIT_ARCHIVE}" + LIVEKIT_URL="https://github.com/livekit/client-sdk-cpp/releases/download/v${LIVEKIT_VERSION}/${LIVEKIT_ARCHIVE}" echo -e "${BLUE}=== Godot-LiveKit Local Build Script (macOS ${ARCH}) ===${NC}" } @@ -98,18 +109,17 @@ setup_windows() { ARCH="x86_64" SCONS_FLAGS="platform=windows arch=x86_64" LIVEKIT_ARCHIVE="livekit-sdk-windows-x64-${LIVEKIT_VERSION}.zip" - LIVEKIT_URL="https://github.com/krazyjakee/client-sdk-cpp/releases/download/v${LIVEKIT_VERSION}/${LIVEKIT_ARCHIVE}" + LIVEKIT_URL="https://github.com/livekit/client-sdk-cpp/releases/download/v${LIVEKIT_VERSION}/${LIVEKIT_ARCHIVE}" echo -e "${BLUE}=== Godot-LiveKit Local Build Script (Windows Native) ===${NC}" } setup_android() { PLATFORM="android" - ARCH="arm64" - SCONS_FLAGS="platform=android arch=arm64 screen_capture=no" - LIVEKIT_ARCHIVE="livekit-sdk-android-arm64-${LIVEKIT_VERSION}.tar.gz" - LIVEKIT_URL="https://github.com/krazyjakee/client-sdk-cpp/releases/download/v${LIVEKIT_VERSION}/${LIVEKIT_ARCHIVE}" + ARCH="${1:-arm64}" + SCONS_FLAGS="platform=android arch=${ARCH} screen_capture=no" + BUILD_LIVEKIT_SDK=true SKIP_FRAMETAP=true - echo -e "${BLUE}=== Godot-LiveKit Local Build Script (Android arm64) ===${NC}" + echo -e "${BLUE}=== Godot-LiveKit Local Build Script (Android ${ARCH}) ===${NC}" } setup_web() { @@ -133,6 +143,12 @@ check_godotcpp_cache() { return 1 fi + # A build for another architecture or target doesn't have the library this build links. + if [ -n "$ARCH" ] && ! ls godot-cpp/bin/*godot-cpp.${PLATFORM}.${BUILD_TARGET}*.${ARCH}.* &> /dev/null; then + echo -e "${RED}Cache miss: godot-cpp not built for ${PLATFORM} ${ARCH} (${BUILD_TARGET})${NC}" + return 1 + fi + # Verify version matches if [ -f "godot-cpp/.version" ] && [ "$(cat godot-cpp/.version)" = "$GODOT_CPP_VERSION" ]; then echo -e "${GREEN}godot-cpp cache is valid!${NC}" @@ -218,12 +234,76 @@ fetch_livekit() { fi fi - local version_key="$LIVEKIT_VERSION" - [ "$PLATFORM" == "macos" ] && version_key="${LIVEKIT_VERSION}-${ARCH}" - echo "$version_key" > livekit-sdk/.version + livekit_version_key > livekit-sdk/.version echo -e "${GREEN}LiveKit SDK downloaded and extracted successfully!${NC}" } +# Function to build the LiveKit C++ SDK for Android from its release, with our Android patch +build_livekit_android() { + echo -e "${YELLOW}Building LiveKit C++ SDK ${LIVEKIT_VERSION} for Android (${ARCH})...${NC}" + local ndk_root="${ANDROID_NDK_ROOT:-$ANDROID_NDK_HOME}" + local work_dir="$(pwd)/livekit-sdk-build" + local src_dir="${work_dir}/client-sdk-cpp" + local android_abi="arm64-v8a" + local rust_target="aarch64-linux-android" + if [ "$ARCH" = "x86_64" ]; then + android_abi="x86_64" + rust_target="x86_64-linux-android" + fi + + rm -rf livekit-sdk "$work_dir" + mkdir -p "$work_dir" + git clone --quiet --depth 1 --branch "v${LIVEKIT_VERSION}" --recurse-submodules --shallow-submodules \ + https://github.com/livekit/client-sdk-cpp.git "$src_dir" + git -C "$src_dir" apply "$(pwd)/${LIVEKIT_ANDROID_PATCH}" + + local protoc_host protoc_sha256 + case "$(uname -s)-$(uname -m)" in + Linux-x86_64) protoc_host="linux-x86_64"; protoc_sha256="$PROTOC_SHA256_LINUX_X86_64" ;; + Linux-aarch64 | Linux-arm64) protoc_host="linux-aarch_64"; protoc_sha256="$PROTOC_SHA256_LINUX_AARCH64" ;; + Darwin-*) protoc_host="osx-universal_binary"; protoc_sha256="$PROTOC_SHA256_MACOS" ;; + *) + echo -e "${RED}Building the LiveKit SDK for Android isn't supported on $(uname -s) $(uname -m)${NC}" + exit 1 + ;; + esac + local protoc_zip="${work_dir}/protoc.zip" + curl -sL "https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-${protoc_host}.zip" \ + -o "$protoc_zip" + verify_checksum "$protoc_zip" "$protoc_sha256" + unzip -q "$protoc_zip" -d "${work_dir}/protoc" + + # The SDK pins its Rust release in client-sdk-rust, so add the target to that toolchain. + (cd "${src_dir}/client-sdk-rust" && rustup target add "$rust_target") + + # The SDK reports errors by throwing, and the extension can only catch them if both share one C++ runtime. + # With the NDK's default static runtime, each library gets its own exception types, and the app aborts instead. + cmake -S "$src_dir" -B "${work_dir}/build" -G Ninja \ + -DCMAKE_TOOLCHAIN_FILE="${ndk_root}/build/cmake/android.toolchain.cmake" \ + -DANDROID_ABI="$android_abi" \ + -DANDROID_PLATFORM=android-24 \ + -DANDROID_STL=c++_shared \ + -DCMAKE_BUILD_TYPE=Release \ + -DLIVEKIT_VERSION="$LIVEKIT_VERSION" \ + -DProtobuf_PROTOC_EXECUTABLE="${work_dir}/protoc/bin/protoc" + cmake --build "${work_dir}/build" -j "$(getconf _NPROCESSORS_ONLN)" + cmake --install "${work_dir}/build" --prefix "$(pwd)/livekit-sdk" + # Apps also need WebRTC's Java classes, which come with the WebRTC build that the SDK's build downloads. + local webrtc_jar + # The SDK's build puts Cargo's output in CARGO_TARGET_DIR when it's set, relative to client-sdk-rust. + webrtc_jar=$(cd "${src_dir}/client-sdk-rust" && find "$(cd "${CARGO_TARGET_DIR:-target}" && pwd)" -path "*/out/livekit_webrtc/*" -name libwebrtc.jar | head -1) + if [ -z "$webrtc_jar" ]; then + echo -e "${RED}libwebrtc.jar not found in the LiveKit SDK build${NC}" + exit 1 + fi + mkdir -p livekit-sdk/java + cp "$webrtc_jar" livekit-sdk/java/ + + livekit_version_key > livekit-sdk/.version + rm -rf "$work_dir" + echo -e "${GREEN}LiveKit SDK built successfully!${NC}" +} + # Function to download livekit-client, which the web build embeds fetch_livekit_js() { if [ -f "livekit-js/.version" ] && [ "$(cat livekit-js/.version)" = "$LIVEKIT_JS_VERSION" ] && [ -f "livekit-js/livekit-client.e2ee.worker.js" ]; then @@ -310,9 +390,7 @@ check_livekit_cache() { return 1 fi - local version_key="$LIVEKIT_VERSION" - [ "$PLATFORM" == "macos" ] && version_key="${LIVEKIT_VERSION}-${ARCH}" - if [ -f "livekit-sdk/.version" ] && [ "$(cat livekit-sdk/.version)" = "$version_key" ]; then + if [ -f "livekit-sdk/.version" ] && [ "$(cat livekit-sdk/.version)" = "$(livekit_version_key)" ]; then echo -e "${GREEN}livekit SDK cache is valid!${NC}" return 0 fi @@ -401,8 +479,15 @@ install_dependencies() { echo -e "${RED}ANDROID_NDK_ROOT or ANDROID_NDK_HOME must be set and point to a valid NDK installation${NC}" exit 1 fi + local ndk_major + ndk_major=$(sed -n 's/^Pkg.Revision *= *\([0-9]*\).*/\1/p' "${ndk_root}/source.properties" 2>/dev/null) + if [ -z "$ndk_major" ] || [ "$ndk_major" -lt "$MIN_ANDROID_NDK_MAJOR" ]; then + echo -e "${RED}Android builds need NDK r${MIN_ANDROID_NDK_MAJOR} or newer, but ${ndk_root} is ${ndk_major:-unknown}${NC}" + exit 1 + fi - local required_tools=("scons" "curl" "tar") + # Building the LiveKit SDK also needs git, CMake, Ninja and Rust (rustup). + local required_tools=("scons" "curl" "tar" "unzip" "git" "cmake" "ninja" "rustup" "cargo") local missing_tools=() for tool in "${required_tools[@]}"; do @@ -429,7 +514,7 @@ build_main_project() { # Copy shared libraries next to the Godot GDExtension library so they can be loaded echo -e "${YELLOW}Copying LiveKit shared libraries to bin...${NC}" if [ "$PLATFORM" == "android" ]; then - local dep_dir="addons/godot-livekit/bin/android-arm64" + local dep_dir="addons/godot-livekit/bin/android-${ARCH}" mkdir -p "$dep_dir" cp livekit-sdk/lib/*.so "$dep_dir/" || true elif [ "$PLATFORM" == "web" ]; then @@ -456,6 +541,18 @@ build_main_project() { fi } +# Prints the key that identifies the LiveKit SDK build in livekit-sdk/.version. +livekit_version_key() { + if [ "$PLATFORM" == "macos" ]; then + echo "${LIVEKIT_VERSION}-${ARCH}" + elif [ "$BUILD_LIVEKIT_SDK" = true ]; then + # A changed patch needs a rebuild. + echo "${LIVEKIT_VERSION}-${ARCH}-$(git hash-object "$LIVEKIT_ANDROID_PATCH" | cut -c1-12)" + else + echo "$LIVEKIT_VERSION" + fi +} + # --- Main Execution --- main() { # Parse command-line arguments @@ -497,7 +594,7 @@ main() { setup_windows ;; android) - setup_android + setup_android "$arch_arg" ;; web) setup_web @@ -519,10 +616,12 @@ main() { if [ "$SKIP_LIVEKIT_SDK" = true ]; then fetch_livekit_js - elif ! check_livekit_cache; then - fetch_livekit - else + elif check_livekit_cache; then echo -e "${GREEN}Using cached livekit SDK${NC}" + elif [ "$BUILD_LIVEKIT_SDK" = true ]; then + build_livekit_android + else + fetch_livekit fi if ! check_godotcpp_cache; then diff --git a/patches/client-sdk-cpp-android.patch b/patches/client-sdk-cpp-android.patch new file mode 100644 index 0000000..bad1802 --- /dev/null +++ b/patches/client-sdk-cpp-android.patch @@ -0,0 +1,284 @@ +diff --git a/CMakeLists.txt b/CMakeLists.txt +index 00d4bd3..230df50 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -211,6 +211,15 @@ if(_lk_capture_features) + list(JOIN _lk_capture_features "," LIVEKIT_CARGO_FEATURES) + set(LIVEKIT_CAPTURE_ENABLED 1) + endif() ++# Android keeps its CA certificates where rustls-native-certs doesn't look, so use the bundled webpki roots there, as ++# LiveKit's own Android FFI builds do. ++set(LIVEKIT_CARGO_NO_DEFAULT_FEATURES OFF) ++if(ANDROID) ++ set(LIVEKIT_CARGO_NO_DEFAULT_FEATURES ON) ++ set(_lk_cargo_features ${_lk_capture_features} room-apis rustls-tls-webpki-roots) ++ list(SORT _lk_cargo_features) ++ list(JOIN _lk_cargo_features "," LIVEKIT_CARGO_FEATURES) ++endif() + message(STATUS "LIVEKIT_CAPTURE='${LIVEKIT_CAPTURE}' -> cargo features: '${LIVEKIT_CARGO_FEATURES}'") + + set(GIT_COMMIT "unknown") +@@ -261,14 +270,55 @@ configure_file( + + find_program(CARGO_EXECUTABLE NAMES cargo REQUIRED) + +-# Determine Rust target triple for cross-compilation on macOS ++# Determine Rust target triple for cross-compilation on macOS and Android + set(RUST_TARGET_TRIPLE "") ++set(LIVEKIT_ANDROID_NDK "") ++set(LIVEKIT_ANDROID_CC "") + if(APPLE AND CMAKE_OSX_ARCHITECTURES) + if(CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64") + set(RUST_TARGET_TRIPLE "x86_64-apple-darwin") + elseif(CMAKE_OSX_ARCHITECTURES STREQUAL "arm64") + set(RUST_TARGET_TRIPLE "aarch64-apple-darwin") + endif() ++elseif(ANDROID) ++ # The NDK names its clang wrappers after the target triple, which differs from Rust's for 32-bit ARM. ++ if(CMAKE_ANDROID_ARCH_ABI STREQUAL "arm64-v8a") ++ set(RUST_TARGET_TRIPLE "aarch64-linux-android") ++ set(_lk_android_clang_triple "aarch64-linux-android") ++ elseif(CMAKE_ANDROID_ARCH_ABI STREQUAL "armeabi-v7a") ++ set(RUST_TARGET_TRIPLE "armv7-linux-androideabi") ++ set(_lk_android_clang_triple "armv7a-linux-androideabi") ++ elseif(CMAKE_ANDROID_ARCH_ABI STREQUAL "x86_64") ++ set(RUST_TARGET_TRIPLE "x86_64-linux-android") ++ set(_lk_android_clang_triple "x86_64-linux-android") ++ else() ++ message(FATAL_ERROR "Unsupported Android ABI: '${CMAKE_ANDROID_ARCH_ABI}'") ++ endif() ++ set(LIVEKIT_ANDROID_NDK "${CMAKE_ANDROID_NDK}") ++ # Cargo links the FFI itself, so it needs the NDK's compiler for the same ABI and API level as this build. ++ # The NDK's toolchain file sets ANDROID_PLATFORM_LEVEL; CMake's own Android support uses CMAKE_SYSTEM_VERSION. ++ if(DEFINED ANDROID_PLATFORM_LEVEL) ++ set(_lk_android_api "${ANDROID_PLATFORM_LEVEL}") ++ else() ++ set(_lk_android_api "${CMAKE_SYSTEM_VERSION}") ++ endif() ++ get_filename_component(_lk_android_toolchain_bin "${CMAKE_C_COMPILER}" DIRECTORY) ++ set(LIVEKIT_ANDROID_CC ++ "${_lk_android_toolchain_bin}/${_lk_android_clang_triple}${_lk_android_api}-clang") ++ if(NOT EXISTS "${LIVEKIT_ANDROID_CC}") ++ message(FATAL_ERROR "Android NDK compiler not found: ${LIVEKIT_ANDROID_CC}") ++ endif() ++ # The SDK throws exceptions to its callers. With a static C++ runtime, liblivekit and the app's libraries each get ++ # their own copy, so the app can't catch them and aborts instead. ++ if(DEFINED ANDROID_STL) ++ set(_lk_android_stl "${ANDROID_STL}") ++ else() ++ set(_lk_android_stl "${CMAKE_ANDROID_STL_TYPE}") ++ endif() ++ if(NOT _lk_android_stl STREQUAL "c++_shared") ++ message(WARNING "Building for Android with ANDROID_STL='${_lk_android_stl}'. Use c++_shared, or apps won't be " ++ "able to catch the SDK's exceptions.") ++ endif() + endif() + + # Cargo artifacts cannot be shared between capture-enabled and disabled SDKs. +@@ -293,7 +343,8 @@ endif() + # This is needed when using a non-system GCC (e.g. GCC 14) on an older distro whose + # system libstdc++ lacks symbols like __cxa_call_terminate. + set(GCC_LIB_DIR "") +-if(NOT WIN32) ++# The NDK's compiler is clang, and the libraries beside it are for the build machine, not the target. ++if(NOT WIN32 AND NOT ANDROID) + get_filename_component(_gcc_bin_dir "${CMAKE_C_COMPILER}" DIRECTORY) + get_filename_component(_gcc_root "${_gcc_bin_dir}" DIRECTORY) + if(IS_DIRECTORY "${_gcc_root}/lib64") +@@ -397,8 +448,12 @@ add_custom_command( + -DCARGO=${CARGO_EXECUTABLE} + -DPROTOC_PATH=${Protobuf_PROTOC_EXECUTABLE} + -DRUST_TARGET=${RUST_TARGET_TRIPLE} ++ -DANDROID_NDK=${LIVEKIT_ANDROID_NDK} ++ -DANDROID_CC=${LIVEKIT_ANDROID_CC} ++ -DANDROID_AR=${CMAKE_AR} + -DGCC_LIB_DIR=${GCC_LIB_DIR} + -DCARGO_FEATURES=${LIVEKIT_CARGO_FEATURES} ++ -DCARGO_NO_DEFAULT_FEATURES=${LIVEKIT_CARGO_NO_DEFAULT_FEATURES} + -DCARGO_TARGET_DIR=${LIVEKIT_CARGO_TARGET_DIR} + -P "${RUN_CARGO_SCRIPT}" + WORKING_DIRECTORY "${RUST_ROOT}" +@@ -533,7 +588,7 @@ target_link_libraries(livekit + + if(WIN32) + target_link_libraries(livekit PRIVATE winhttp) +-else() ++elseif(NOT ANDROID) + find_package(CURL REQUIRED) + target_link_libraries(livekit PRIVATE CURL::libcurl) + endif() +@@ -684,7 +739,7 @@ if(WIN32) + ) + endif() + +-if(UNIX AND NOT APPLE) ++if(UNIX AND NOT APPLE AND NOT ANDROID) + find_package(OpenSSL REQUIRED) + target_link_libraries(livekit PRIVATE OpenSSL::SSL OpenSSL::Crypto) + endif() +diff --git a/cmake/protobuf.cmake b/cmake/protobuf.cmake +index 3e0fa33..33cfa2a 100644 +--- a/cmake/protobuf.cmake ++++ b/cmake/protobuf.cmake +@@ -149,7 +149,11 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON CACHE BOOL "" FORCE) + set(protobuf_BUILD_TESTS OFF CACHE BOOL "" FORCE) + set(protobuf_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) + set(protobuf_BUILD_CONFORMANCE OFF CACHE BOOL "" FORCE) +-set(protobuf_BUILD_PROTOC_BINARIES ON CACHE BOOL "" FORCE) ++if(CMAKE_CROSSCOMPILING) ++ set(protobuf_BUILD_PROTOC_BINARIES OFF CACHE BOOL "" FORCE) ++else() ++ set(protobuf_BUILD_PROTOC_BINARIES ON CACHE BOOL "" FORCE) ++endif() + set(protobuf_WITH_ZLIB OFF CACHE BOOL "" FORCE) + + set(protobuf_ABSL_PROVIDER "package" CACHE STRING "" FORCE) +@@ -198,7 +202,21 @@ foreach(_livekit_protobuf_target IN LISTS _livekit_protobuf_targets) + endforeach() + + # Protobuf targets: modern protobuf exports protobuf::protoc etc. +-if(TARGET protobuf::protoc) ++if(CMAKE_CROSSCOMPILING) ++ # A protoc built for the target can't run during the build, so use one for the build machine. Its version must ++ # match the vendored runtime, which only accepts code generated by the same release. ++ find_program(Protobuf_PROTOC_EXECUTABLE NAMES protoc REQUIRED) ++ execute_process( ++ COMMAND "${Protobuf_PROTOC_EXECUTABLE}" --version ++ OUTPUT_VARIABLE _livekit_host_protoc_version ++ OUTPUT_STRIP_TRAILING_WHITESPACE ++ ) ++ if(NOT _livekit_host_protoc_version STREQUAL "libprotoc ${LIVEKIT_PROTOBUF_VERSION}") ++ message(FATAL_ERROR ++ "Cross-compiling needs protoc ${LIVEKIT_PROTOBUF_VERSION} for the build machine, but " ++ "${Protobuf_PROTOC_EXECUTABLE} is '${_livekit_host_protoc_version}'. Set Protobuf_PROTOC_EXECUTABLE.") ++ endif() ++elseif(TARGET protobuf::protoc) + set(Protobuf_PROTOC_EXECUTABLE "$" CACHE STRING "protoc (vendored)" FORCE) + elseif(TARGET protoc) + set(Protobuf_PROTOC_EXECUTABLE "$" CACHE STRING "protoc (vendored)" FORCE) +diff --git a/cmake/run_cargo.cmake b/cmake/run_cargo.cmake +index 06be61f..69bdf61 100644 +--- a/cmake/run_cargo.cmake ++++ b/cmake/run_cargo.cmake +@@ -27,6 +27,10 @@ if(NOT CFG STREQUAL "Debug") + list(APPEND ARGS --release) + endif() + ++if(CARGO_NO_DEFAULT_FEATURES) ++ list(APPEND ARGS --no-default-features) ++endif() ++ + if(DEFINED CARGO_FEATURES AND NOT CARGO_FEATURES STREQUAL "") + list(APPEND ARGS --features "${CARGO_FEATURES}") + message(STATUS "[run_cargo.cmake] Cargo features: ${CARGO_FEATURES}") +@@ -49,6 +53,22 @@ if(DEFINED GCC_LIB_DIR AND NOT GCC_LIB_DIR STREQUAL "") + set(ENV{LD_LIBRARY_PATH} "${GCC_LIB_DIR}:$ENV{LD_LIBRARY_PATH}") + endif() + ++# Point cargo, the cc crate and bindgen at the NDK's toolchain for the target, as cargo-ndk does. webrtc-sys ++# finds the NDK through ANDROID_NDK_HOME. ++if(DEFINED ANDROID_NDK AND NOT ANDROID_NDK STREQUAL "") ++ string(REPLACE "-" "_" _target_env "${RUST_TARGET}") ++ string(TOUPPER "${_target_env}" _target_env_upper) ++ get_filename_component(_toolchain_bin "${ANDROID_CC}" DIRECTORY) ++ get_filename_component(_toolchain_root "${_toolchain_bin}" DIRECTORY) ++ set(ENV{ANDROID_NDK_HOME} "${ANDROID_NDK}") ++ set(ENV{CC_${_target_env}} "${ANDROID_CC}") ++ set(ENV{CXX_${_target_env}} "${ANDROID_CC}++") ++ set(ENV{AR_${_target_env}} "${ANDROID_AR}") ++ set(ENV{CARGO_TARGET_${_target_env_upper}_LINKER} "${ANDROID_CC}") ++ set(ENV{BINDGEN_EXTRA_CLANG_ARGS_${_target_env}} "--sysroot=${_toolchain_root}/sysroot") ++ message(STATUS "[run_cargo.cmake] Android NDK=${ANDROID_NDK} CC=${ANDROID_CC}") ++endif() ++ + execute_process( + COMMAND "${CARGO}" ${ARGS} + WORKING_DIRECTORY "${RUST_ROOT}" +diff --git a/cmake/uniffi_cpp.cmake b/cmake/uniffi_cpp.cmake +index f5d5f1a..0edc902 100644 +--- a/cmake/uniffi_cpp.cmake ++++ b/cmake/uniffi_cpp.cmake +@@ -27,8 +27,14 @@ set(LIVEKIT_UNIFFI_CPP_SCAFFOLDING_HEADER + if(UNIX AND NOT APPLE) + set(LIVEKIT_UNIFFI_METADATA_TARGET_DIR + "${LIVEKIT_CARGO_TARGET_DIR}/uniffi-cpp-metadata") +- set(LIVEKIT_UNIFFI_METADATA_LIBRARY +- "${LIVEKIT_UNIFFI_METADATA_TARGET_DIR}/debug/liblivekit_ffi.so") ++ # Android builds it for the target too, so cross-compiling doesn't also need a native toolchain for the host. ++ if(RUST_TARGET_TRIPLE) ++ set(LIVEKIT_UNIFFI_METADATA_LIBRARY ++ "${LIVEKIT_UNIFFI_METADATA_TARGET_DIR}/${RUST_TARGET_TRIPLE}/debug/liblivekit_ffi.so") ++ else() ++ set(LIVEKIT_UNIFFI_METADATA_LIBRARY ++ "${LIVEKIT_UNIFFI_METADATA_TARGET_DIR}/debug/liblivekit_ffi.so") ++ endif() + endif() + + file(GLOB_RECURSE LIVEKIT_UNIFFI_BINDGEN_SOURCES CONFIGURE_DEPENDS +@@ -44,9 +50,14 @@ if(UNIX AND NOT APPLE) + -DRUST_ROOT=${RUST_ROOT} + -DCARGO=${CARGO_EXECUTABLE} + -DPROTOC_PATH=${Protobuf_PROTOC_EXECUTABLE} ++ -DRUST_TARGET=${RUST_TARGET_TRIPLE} ++ -DANDROID_NDK=${LIVEKIT_ANDROID_NDK} ++ -DANDROID_CC=${LIVEKIT_ANDROID_CC} ++ -DANDROID_AR=${CMAKE_AR} + -DGCC_LIB_DIR=${GCC_LIB_DIR} + -DCARGO_TARGET_DIR=${LIVEKIT_UNIFFI_METADATA_TARGET_DIR} + -DCARGO_FEATURES=${LIVEKIT_CARGO_FEATURES} ++ -DCARGO_NO_DEFAULT_FEATURES=${LIVEKIT_CARGO_NO_DEFAULT_FEATURES} + -P "${RUN_CARGO_SCRIPT}" + WORKING_DIRECTORY "${RUST_ROOT}" + DEPENDS ${RUST_SOURCES} "${RUN_CARGO_SCRIPT}" +diff --git a/src/token_source_http.cpp b/src/token_source_http.cpp +index 470e85d..99254be 100644 +--- a/src/token_source_http.cpp ++++ b/src/token_source_http.cpp +@@ -27,14 +27,14 @@ + #endif + #include + #include +-#else ++#elif !defined(__ANDROID__) + #include + #endif + + namespace livekit { + namespace { + +-#if !defined(_WIN32) ++#if !defined(_WIN32) && !defined(__ANDROID__) + size_t curlWriteCallback(char* contents, size_t size, size_t nmemb, void* user_data) { + const size_t total_size = size * nmemb; + auto* response = static_cast(user_data); +@@ -43,6 +43,7 @@ size_t curlWriteCallback(char* contents, size_t size, size_t nmemb, void* user_d + } + #endif + ++#if !defined(__ANDROID__) + std::string normalizeHttpMethod(std::string method) { + if (method.empty()) { + return "POST"; +@@ -61,6 +62,7 @@ std::int64_t effectiveTimeoutMs(std::chrono::milliseconds timeout) { + const std::int64_t count = timeout.count(); + return count > 0 ? count : kDefaultTimeoutMs; + } ++#endif + + #if defined(_WIN32) + std::wstring toWide(const std::string& value) { +@@ -198,6 +200,14 @@ Result tokenSourceHttpRequest(const std::string& metho + std::chrono::milliseconds timeout) { + #if defined(_WIN32) + return winHttpRequest(method, url, headers, json_body, timeout); ++#elif defined(__ANDROID__) ++ // Android has no system libcurl, and bundling one would mean bundling a TLS library too. ++ static_cast(method); ++ static_cast(url); ++ static_cast(headers); ++ static_cast(json_body); ++ static_cast(timeout); ++ return Result::failure("HTTP token sources aren't supported on Android"); + #else + CURL* curl = curl_easy_init(); + if (curl == nullptr) { diff --git a/project.godot b/project.godot index 55c581b..35c5cd6 100644 --- a/project.godot +++ b/project.godot @@ -17,3 +17,7 @@ config/features=PackedStringArray("4.5") [editor_plugins] enabled=PackedStringArray("res://addons/godot-livekit/plugin.cfg", "res://addons/gut/plugin.cfg") + +[rendering] + +textures/vram_compression/import_etc2_astc=true diff --git a/src/android_jni_init.cpp b/src/android_jni_init.cpp index 620dc32..87c647d 100644 --- a/src/android_jni_init.cpp +++ b/src/android_jni_init.cpp @@ -156,7 +156,11 @@ void livekit_android_jni_init() { // registration), then this library (AV1 stubs). Both calls are idempotent // for already-Java-loaded libraries. bool ok = java_load_library(env, "livekit_ffi"); +#if defined(__x86_64__) + ok = java_load_library(env, "godot-livekit.android.x86_64") && ok; +#else ok = java_load_library(env, "godot-livekit.android.arm64") && ok; +#endif jni_initialized = ok; } diff --git a/src/livekit_audio_stream.cpp b/src/livekit_audio_stream.cpp index 2fa616c..d2b7b17 100644 --- a/src/livekit_audio_stream.cpp +++ b/src/livekit_audio_stream.cpp @@ -19,9 +19,7 @@ void LiveKitAudioStream::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::INT, "num_channels"), "", "get_num_channels"); } -LiveKitAudioStream::LiveKitAudioStream() { - ring_.resize(kMaxAudioBufferSamples, 0.0f); -} +LiveKitAudioStream::LiveKitAudioStream() {} LiveKitAudioStream::~LiveKitAudioStream() { alive_->store(false); @@ -46,10 +44,10 @@ void LiveKitAudioStream::_reader_loop() { const auto &frame = event.frame; const auto &pcm_data = frame.data(); - int channels = frame.num_channels(); + int channels = frame.numChannels(); // Update sample rate/channels from actual frame data - sample_rate_ = frame.sample_rate(); + sample_rate_ = frame.sampleRate(); num_channels_ = channels; // Convert int16 interleaved PCM to float32 using persistent buffer. @@ -94,6 +92,8 @@ Ref LiveKitAudioStream::from_track(const Ref & Ref stream; stream.instantiate(); stream->stream_ = native_stream; + // Allocated here rather than in the constructor, since only streams that read need it. + stream->ring_.resize(kMaxAudioBufferSamples, 0.0f); stream->running_.store(true); // Capture the alive sentinel so the thread can detect if the object is freed. auto alive = stream->alive_; @@ -105,7 +105,8 @@ Ref LiveKitAudioStream::from_track(const Ref & } Ref LiveKitAudioStream::from_participant(const Ref &participant, int source) { - if (participant.is_null() || !participant->get_native_remote_participant()) { + auto native_participant = participant.is_valid() ? participant->get_native_remote_participant() : nullptr; + if (!native_participant) { UtilityFunctions::push_error("LiveKitAudioStream::from_participant: invalid participant"); return Ref(); } @@ -113,7 +114,7 @@ Ref LiveKitAudioStream::from_participant(const Refget_native_remote_participant(), + *native_participant, (livekit::TrackSource)source, opts); if (!native_stream) { @@ -124,6 +125,8 @@ Ref LiveKitAudioStream::from_participant(const Ref stream; stream.instantiate(); stream->stream_ = native_stream; + // Allocated here rather than in the constructor, since only streams that read need it. + stream->ring_.resize(kMaxAudioBufferSamples, 0.0f); stream->running_.store(true); auto alive = stream->alive_; stream->reader_thread_.start([raw = stream.ptr(), alive]() { @@ -151,7 +154,8 @@ int LiveKitAudioStream::poll(const Ref &playback) std::unique_lock lock(audio_mutex_, std::try_to_lock); if (!lock.owns_lock()) { uint32_t count = lock_contention_count_.fetch_add(1) + 1; - if (count == 1 || (count % 100) == 0) { + // An occasional skipped poll is harmless, since the next one picks up what the reader added. + if ((count % 100) == 0) { UtilityFunctions::push_warning("LiveKitAudioStream::poll: lock contention (", count, " skipped frames)"); } return 0; diff --git a/src/livekit_audio_stream.h b/src/livekit_audio_stream.h index ac9be6e..a698e9c 100644 --- a/src/livekit_audio_stream.h +++ b/src/livekit_audio_stream.h @@ -27,7 +27,7 @@ class LiveKitAudioStream : public RefCounted { std::shared_ptr stream_; std::mutex audio_mutex_; - // Ring buffer: pre-allocated, O(1) write, no memmove on overflow. + // Ring buffer: allocated once when reading starts, O(1) write, no memmove on overflow. static constexpr size_t kMaxAudioBufferSamples = 48000 * 2 * 5; // ~5 seconds stereo at 48kHz std::vector ring_; size_t ring_head_{0}; // read position diff --git a/src/livekit_e2ee.cpp b/src/livekit_e2ee.cpp index cc31c36..eecd6b6 100644 --- a/src/livekit_e2ee.cpp +++ b/src/livekit_e2ee.cpp @@ -1,4 +1,5 @@ #include "livekit_e2ee.h" +#include "sdk_call.h" #include #include @@ -118,26 +119,29 @@ LiveKitKeyProvider::LiveKitKeyProvider() { LiveKitKeyProvider::~LiveKitKeyProvider() { } -void LiveKitKeyProvider::bind_key_provider(livekit::E2EEManager::KeyProvider *kp) { +void LiveKitKeyProvider::bind_key_provider(const std::weak_ptr &kp) { key_provider_ = kp; } void LiveKitKeyProvider::set_shared_key(const PackedByteArray &key, int key_index) { - if (!key_provider_) { + auto kp = key_provider_.lock(); + if (!kp) { UtilityFunctions::push_error("LiveKitKeyProvider::set_shared_key: not bound"); return; } std::vector k(key.ptr(), key.ptr() + key.size()); - key_provider_->setSharedKey(k, key_index); + call_sdk("LiveKitKeyProvider::set_shared_key", [&] { kp->setSharedKey(k, key_index); }); } PackedByteArray LiveKitKeyProvider::get_shared_key(int key_index) const { PackedByteArray result; - if (!key_provider_) { + auto kp = key_provider_.lock(); + if (!kp) { UtilityFunctions::push_error("LiveKitKeyProvider::get_shared_key: not bound"); return result; } - auto k = key_provider_->exportSharedKey(key_index); + std::vector k; + call_sdk("LiveKitKeyProvider::get_shared_key", [&] { k = kp->exportSharedKey(key_index); }); result.resize(k.size()); memcpy(result.ptrw(), k.data(), k.size()); return result; @@ -145,34 +149,39 @@ PackedByteArray LiveKitKeyProvider::get_shared_key(int key_index) const { PackedByteArray LiveKitKeyProvider::ratchet_shared_key(int key_index) { PackedByteArray result; - if (!key_provider_) { + auto kp = key_provider_.lock(); + if (!kp) { UtilityFunctions::push_error("LiveKitKeyProvider::ratchet_shared_key: not bound"); return result; } - auto k = key_provider_->ratchetSharedKey(key_index); + std::vector k; + call_sdk("LiveKitKeyProvider::ratchet_shared_key", [&] { k = kp->ratchetSharedKey(key_index); }); result.resize(k.size()); memcpy(result.ptrw(), k.data(), k.size()); return result; } void LiveKitKeyProvider::set_key(const String &participant_identity, const PackedByteArray &key, int key_index) { - if (!key_provider_) { + auto kp = key_provider_.lock(); + if (!kp) { UtilityFunctions::push_error("LiveKitKeyProvider::set_key: not bound"); return; } std::string identity = participant_identity.utf8().get_data(); std::vector k(key.ptr(), key.ptr() + key.size()); - key_provider_->setKey(identity, k, key_index); + call_sdk("LiveKitKeyProvider::set_key", [&] { kp->setKey(identity, k, key_index); }); } PackedByteArray LiveKitKeyProvider::get_key(const String &participant_identity, int key_index) const { PackedByteArray result; - if (!key_provider_) { + auto kp = key_provider_.lock(); + if (!kp) { UtilityFunctions::push_error("LiveKitKeyProvider::get_key: not bound"); return result; } std::string identity = participant_identity.utf8().get_data(); - auto k = key_provider_->exportKey(identity, key_index); + std::vector k; + call_sdk("LiveKitKeyProvider::get_key", [&] { k = kp->exportKey(identity, key_index); }); result.resize(k.size()); memcpy(result.ptrw(), k.data(), k.size()); return result; @@ -180,12 +189,14 @@ PackedByteArray LiveKitKeyProvider::get_key(const String &participant_identity, PackedByteArray LiveKitKeyProvider::ratchet_key(const String &participant_identity, int key_index) { PackedByteArray result; - if (!key_provider_) { + auto kp = key_provider_.lock(); + if (!kp) { UtilityFunctions::push_error("LiveKitKeyProvider::ratchet_key: not bound"); return result; } std::string identity = participant_identity.utf8().get_data(); - auto k = key_provider_->ratchetKey(identity, key_index); + std::vector k; + call_sdk("LiveKitKeyProvider::ratchet_key", [&] { k = kp->ratchetKey(identity, key_index); }); result.resize(k.size()); memcpy(result.ptrw(), k.data(), k.size()); return result; @@ -243,8 +254,9 @@ void LiveKitFrameCryptor::set_enabled(bool enabled) { UtilityFunctions::push_error("LiveKitFrameCryptor::set_enabled: not bound"); return; } - frame_cryptor_->setEnabled(enabled); - enabled_ = enabled; + // client-sdk-cpp 1.13.1's FrameCryptor doesn't send its track's sid, and the FFI aborts the + // process when it's missing (livekit/client-sdk-cpp#311). + UtilityFunctions::push_error("LiveKitFrameCryptor::set_enabled: not supported by the LiveKit C++ SDK yet"); } void LiveKitFrameCryptor::set_key_index(int key_index) { @@ -252,8 +264,9 @@ void LiveKitFrameCryptor::set_key_index(int key_index) { UtilityFunctions::push_error("LiveKitFrameCryptor::set_key_index: not bound"); return; } - frame_cryptor_->setKeyIndex(key_index); - key_index_ = key_index; + // client-sdk-cpp 1.13.1's FrameCryptor doesn't send its track's sid, and the FFI aborts the + // process when it's missing (livekit/client-sdk-cpp#311). + UtilityFunctions::push_error("LiveKitFrameCryptor::set_key_index: not supported by the LiveKit C++ SDK yet"); } // --- LiveKitE2eeManager --- @@ -274,33 +287,36 @@ LiveKitE2eeManager::LiveKitE2eeManager() { LiveKitE2eeManager::~LiveKitE2eeManager() { } -void LiveKitE2eeManager::bind_manager(livekit::E2EEManager *mgr) { +void LiveKitE2eeManager::bind_manager(const std::weak_ptr &mgr) { e2ee_manager_ = mgr; key_provider_.unref(); } bool LiveKitE2eeManager::get_enabled() const { - if (!e2ee_manager_) { + auto mgr = e2ee_manager_.lock(); + if (!mgr) { return false; } - return e2ee_manager_->enabled(); + return mgr->enabled(); } void LiveKitE2eeManager::set_enabled(bool enabled) { - if (!e2ee_manager_) { + auto mgr = e2ee_manager_.lock(); + if (!mgr) { UtilityFunctions::push_error("LiveKitE2eeManager::set_enabled: not bound"); return; } - e2ee_manager_->setEnabled(enabled); + call_sdk("LiveKitE2eeManager::set_enabled", [&] { mgr->setEnabled(enabled); }); } Ref LiveKitE2eeManager::get_key_provider() { - if (!e2ee_manager_) { + auto mgr = e2ee_manager_.lock(); + if (!mgr) { return Ref(); } - livekit::E2EEManager::KeyProvider *kp = e2ee_manager_->keyProvider(); - if (!kp) { + auto kp = mgr->keyProvider(); + if (kp.expired()) { return Ref(); } @@ -313,11 +329,15 @@ Ref LiveKitE2eeManager::get_key_provider() { Array LiveKitE2eeManager::get_frame_cryptors() const { Array result; - if (!e2ee_manager_) { + auto mgr = e2ee_manager_.lock(); + if (!mgr) { return result; } - auto cryptors = e2ee_manager_->frameCryptors(); + std::vector cryptors; + if (!call_sdk("LiveKitE2eeManager::get_frame_cryptors", [&] { cryptors = mgr->frameCryptors(); })) { + return result; + } for (auto &fc : cryptors) { Ref godot_fc; godot_fc.instantiate(); diff --git a/src/livekit_e2ee.h b/src/livekit_e2ee.h index 6b8a1c9..0fc4810 100644 --- a/src/livekit_e2ee.h +++ b/src/livekit_e2ee.h @@ -59,7 +59,7 @@ class LiveKitKeyProvider : public RefCounted { GDCLASS(LiveKitKeyProvider, RefCounted) private: - livekit::E2EEManager::KeyProvider *key_provider_ = nullptr; + std::weak_ptr key_provider_; protected: static void _bind_methods(); @@ -68,7 +68,7 @@ class LiveKitKeyProvider : public RefCounted { LiveKitKeyProvider(); ~LiveKitKeyProvider(); - void bind_key_provider(livekit::E2EEManager::KeyProvider *kp); + void bind_key_provider(const std::weak_ptr &kp); void set_shared_key(const PackedByteArray &key, int key_index); PackedByteArray get_shared_key(int key_index) const; @@ -111,7 +111,8 @@ class LiveKitE2eeManager : public RefCounted { GDCLASS(LiveKitE2eeManager, RefCounted) private: - livekit::E2EEManager *e2ee_manager_ = nullptr; + // Weak, since the room owns its manager and frees it on disconnect. + std::weak_ptr e2ee_manager_; Ref key_provider_; protected: @@ -121,7 +122,7 @@ class LiveKitE2eeManager : public RefCounted { LiveKitE2eeManager(); ~LiveKitE2eeManager(); - void bind_manager(livekit::E2EEManager *mgr); + void bind_manager(const std::weak_ptr &mgr); bool get_enabled() const; void set_enabled(bool enabled); diff --git a/src/livekit_participant.cpp b/src/livekit_participant.cpp index 9d2df03..4aa5039 100644 --- a/src/livekit_participant.cpp +++ b/src/livekit_participant.cpp @@ -1,7 +1,9 @@ #include "livekit_participant.h" #include "livekit_track.h" #include "livekit_track_publication.h" +#include "sdk_call.h" +#include #include #include @@ -40,42 +42,40 @@ LiveKitParticipant::LiveKitParticipant() { LiveKitParticipant::~LiveKitParticipant() { } -void LiveKitParticipant::bind_participant(livekit::Participant *p_participant) { +void LiveKitParticipant::bind_participant(const std::weak_ptr &p_participant, const std::string &p_identity) { participant_ = p_participant; + identity_ = p_identity; } String LiveKitParticipant::get_sid() const { - if (participant_) { - return String(participant_->sid().c_str()); + if (auto participant = participant_.lock()) { + return String(participant->sid().c_str()); } return String(); } String LiveKitParticipant::get_name() const { - if (participant_) { - return String(participant_->name().c_str()); + if (auto participant = participant_.lock()) { + return String(participant->name().c_str()); } return String(); } String LiveKitParticipant::get_identity() const { - if (participant_) { - return String(participant_->identity().c_str()); - } - return String(); + return String(identity_.c_str()); } String LiveKitParticipant::get_metadata() const { - if (participant_) { - return String(participant_->metadata().c_str()); + if (auto participant = participant_.lock()) { + return String(participant->metadata().c_str()); } return String(); } Dictionary LiveKitParticipant::get_attributes() const { Dictionary result; - if (participant_) { - const auto &attrs = participant_->attributes(); + if (auto participant = participant_.lock()) { + const auto &attrs = participant->attributes(); for (const auto &pair : attrs) { result[String(pair.first.c_str())] = String(pair.second.c_str()); } @@ -84,8 +84,8 @@ Dictionary LiveKitParticipant::get_attributes() const { } int LiveKitParticipant::get_kind() const { - if (participant_) { - return (int)participant_->kind(); + if (auto participant = participant_.lock()) { + return (int)participant->kind(); } return KIND_STANDARD; } @@ -132,22 +132,16 @@ LiveKitLocalParticipant::LiveKitLocalParticipant() { LiveKitLocalParticipant::~LiveKitLocalParticipant() { // Wait for any in-flight RPC thread. rpc_thread_.join_or_detach(2000); - - // Unregister all RPC methods so the SDK doesn't invoke callbacks - // into a freed object. - if (local_participant_) { - for (const auto &method : registered_rpc_methods_) { - local_participant_->unregisterRpcMethod(method); - } - } + unregister_rpc_methods(); } -void LiveKitLocalParticipant::bind_local_participant(livekit::LocalParticipant *p) { - local_participant_ = p; +std::shared_ptr LiveKitLocalParticipant::get_native_local_participant() const { + return std::static_pointer_cast(participant_.lock()); } void LiveKitLocalParticipant::publish_data(const PackedByteArray &data, bool reliable, const PackedStringArray &destination_identities, const String &topic) { - if (!local_participant_) { + auto lp = get_native_local_participant(); + if (!lp) { UtilityFunctions::push_error("LiveKitLocalParticipant::publish_data: not bound"); return; } @@ -159,27 +153,30 @@ void LiveKitLocalParticipant::publish_data(const PackedByteArray &data, bool rel } std::string topic_str = topic.utf8().get_data(); - local_participant_->publishData(payload, reliable, identities, topic_str); + call_sdk("LiveKitLocalParticipant::publish_data", [&] { lp->publishData(payload, reliable, identities, topic_str); }); } void LiveKitLocalParticipant::set_metadata(const String &metadata) { - if (!local_participant_) { + auto lp = get_native_local_participant(); + if (!lp) { UtilityFunctions::push_error("LiveKitLocalParticipant::set_metadata: not bound"); return; } - local_participant_->setMetadata(std::string(metadata.utf8().get_data())); + call_sdk("LiveKitLocalParticipant::set_metadata", [&] { lp->setMetadata(std::string(metadata.utf8().get_data())); }); } void LiveKitLocalParticipant::set_name(const String &name) { - if (!local_participant_) { + auto lp = get_native_local_participant(); + if (!lp) { UtilityFunctions::push_error("LiveKitLocalParticipant::set_name: not bound"); return; } - local_participant_->setName(std::string(name.utf8().get_data())); + call_sdk("LiveKitLocalParticipant::set_name", [&] { lp->setName(std::string(name.utf8().get_data())); }); } void LiveKitLocalParticipant::set_attributes(const Dictionary &attributes) { - if (!local_participant_) { + auto lp = get_native_local_participant(); + if (!lp) { UtilityFunctions::push_error("LiveKitLocalParticipant::set_attributes: not bound"); return; } @@ -191,16 +188,17 @@ void LiveKitLocalParticipant::set_attributes(const Dictionary &attributes) { String value = attributes[key]; attrs[std::string(key.utf8().get_data())] = std::string(value.utf8().get_data()); } - local_participant_->setAttributes(attrs); + call_sdk("LiveKitLocalParticipant::set_attributes", [&] { lp->setAttributes(attrs); }); } Dictionary LiveKitLocalParticipant::get_track_publications() const { Dictionary result; - if (!local_participant_) { + auto lp = get_native_local_participant(); + if (!lp) { return result; } - const auto &pubs = local_participant_->trackPublications(); + const auto &pubs = lp->trackPublications(); for (const auto &pair : pubs) { Ref pub; pub.instantiate(); @@ -211,7 +209,8 @@ Dictionary LiveKitLocalParticipant::get_track_publications() const { } Ref LiveKitLocalParticipant::publish_track(const Ref &track, const Dictionary &options) { - if (!local_participant_ || track.is_null()) { + auto lp = get_native_local_participant(); + if (!lp || track.is_null()) { UtilityFunctions::push_error("LiveKitLocalParticipant::publish_track: invalid arguments"); return Ref(); } @@ -238,12 +237,25 @@ Ref LiveKitLocalParticipant::publish_track(const R } // video_codec is a VideoCodec enum in the SDK - not easily set from string - auto native_pub = local_participant_->publishTrack(native_track, pub_options); + if (!call_sdk("LiveKitLocalParticipant::publish_track", [&] { lp->publishTrack(native_track, pub_options); })) { + return Ref(); + } + + std::shared_ptr native_pub; + if (auto audio = std::dynamic_pointer_cast(native_track)) { + native_pub = audio->publication(); + } else if (auto video = std::dynamic_pointer_cast(native_track)) { + native_pub = video->publication(); + } if (!native_pub) { UtilityFunctions::push_error("LiveKitLocalParticipant::publish_track: publish failed"); return Ref(); } + if (std::find(published_tracks_.begin(), published_tracks_.end(), native_track) == published_tracks_.end()) { + published_tracks_.push_back(native_track); + } + Ref pub; pub.instantiate(); pub->bind_publication(native_pub); @@ -251,17 +263,35 @@ Ref LiveKitLocalParticipant::publish_track(const R } void LiveKitLocalParticipant::unpublish_track(const String &track_sid) { - if (!local_participant_) { + auto lp = get_native_local_participant(); + if (!lp) { UtilityFunctions::push_error("LiveKitLocalParticipant::unpublish_track: not bound"); return; } - local_participant_->unpublishTrack(std::string(track_sid.utf8().get_data())); + std::string sid = track_sid.utf8().get_data(); + if (!call_sdk("LiveKitLocalParticipant::unpublish_track", [&] { lp->unpublishTrack(sid); })) { + return; + } + published_tracks_.erase( + std::remove_if(published_tracks_.begin(), published_tracks_.end(), + [&sid](const std::shared_ptr &track) { return track->sid() == sid; }), + published_tracks_.end()); +} + +void LiveKitLocalParticipant::unbind() { + // Unbinding stops the destructor from reaching the SDK to unregister these. + unregister_rpc_methods(); + // The SDK's local participant methods wait for a reply without a timeout, and a room being torn + // down may never send one, so calls made after this fail rather than risk blocking forever. + participant_.reset(); + published_tracks_.clear(); } // RPC void LiveKitLocalParticipant::perform_rpc(const String &destination, const String &method, const String &payload, double timeout) { - if (!local_participant_) { + auto lp = get_native_local_participant(); + if (!lp) { UtilityFunctions::push_error("LiveKitLocalParticipant::perform_rpc: not bound"); return; } @@ -280,47 +310,67 @@ void LiveKitLocalParticipant::perform_rpc(const String &destination, const Strin // back to the main thread via signal (call_deferred is thread-safe). // Wait for any previous RPC thread to finish before starting a new one. rpc_thread_.join_or_detach(2000); - rpc_thread_.start([prevent_free, dest_str, method_str, payload_str, timeout, method_for_signal]() { + rpc_thread_.start([prevent_free, lp, dest_str, method_str, payload_str, timeout, method_for_signal]() { try { - std::string result = prevent_free->local_participant_->performRpc( - dest_str, method_str, payload_str, timeout); + std::string result = lp->performRpc(dest_str, method_str, payload_str, timeout); prevent_free->call_deferred("emit_signal", "rpc_response_received", method_for_signal, String(result.c_str())); } catch (const livekit::RpcError &e) { prevent_free->call_deferred("emit_signal", "rpc_error", method_for_signal, String(e.message().c_str())); + } catch (const std::exception &e) { + // E.g. the room disconnected, invalidating the participant's handle. + prevent_free->call_deferred("emit_signal", "rpc_error", + method_for_signal, String(e.what())); } }); } void LiveKitLocalParticipant::register_rpc_method(const String &method) { - if (!local_participant_) { + auto lp = get_native_local_participant(); + if (!lp) { UtilityFunctions::push_error("LiveKitLocalParticipant::register_rpc_method: not bound"); return; } std::string method_name = method.utf8().get_data(); - registered_rpc_methods_.push_back(method_name); - local_participant_->registerRpcMethod(method_name, - [this, method](const livekit::RpcInvocationData &data) -> std::optional { - call_deferred("emit_signal", "rpc_method_invoked", - method, - String(data.request_id.c_str()), - String(data.caller_identity.c_str()), - String(data.payload.c_str()), - data.response_timeout_sec); - // Replies aren't supported yet (see _bind_methods), so the caller gets an empty response. - return std::nullopt; - }); + // The SDK calls handlers on its own thread, and may still be running one when this participant is + // freed, so the handler only holds its ID and emits from the main thread if it's still around. + const uint64_t self_id = get_instance_id(); + bool registered = call_sdk("LiveKitLocalParticipant::register_rpc_method", [&] { + lp->registerRpcMethod(method_name, + [self_id, method](const livekit::RpcInvocationData &data) -> std::optional { + callable_mp_static(&LiveKitLocalParticipant::emit_rpc_method_invoked) + .call_deferred(self_id, method, + String(data.request_id.c_str()), + String(data.caller_identity.c_str()), + String(data.payload.c_str()), + data.response_timeout_sec); + // Replies aren't supported yet (see _bind_methods), so the caller gets an empty response. + return std::nullopt; + }); + }); + if (registered) { + registered_rpc_methods_.push_back(method_name); + } +} + +void LiveKitLocalParticipant::emit_rpc_method_invoked(uint64_t p_participant_id, const String &p_method, + const String &p_request_id, const String &p_caller_identity, const String &p_payload, double p_response_timeout) { + if (auto *participant = Object::cast_to(ObjectDB::get_instance(p_participant_id))) { + participant->emit_signal("rpc_method_invoked", p_method, p_request_id, p_caller_identity, p_payload, + p_response_timeout); + } } void LiveKitLocalParticipant::unregister_rpc_method(const String &method) { - if (!local_participant_) { + auto lp = get_native_local_participant(); + if (!lp) { UtilityFunctions::push_error("LiveKitLocalParticipant::unregister_rpc_method: not bound"); return; } std::string method_name = method.utf8().get_data(); - local_participant_->unregisterRpcMethod(method_name); + call_sdk("LiveKitLocalParticipant::unregister_rpc_method", [&] { lp->unregisterRpcMethod(method_name); }); registered_rpc_methods_.erase( std::remove(registered_rpc_methods_.begin(), registered_rpc_methods_.end(), method_name), registered_rpc_methods_.end()); @@ -339,6 +389,19 @@ void LiveKitLocalParticipant::respond_to_rpc_error(const String &request_id, int // LiveKitRemoteParticipant +void LiveKitLocalParticipant::unregister_rpc_methods() { + if (auto lp = get_native_local_participant()) { + for (const auto &method : registered_rpc_methods_) { + try { + lp->unregisterRpcMethod(method); + } catch (const std::exception &) { + // The room is going away, so it won't invoke the method anyway. + } + } + } + registered_rpc_methods_.clear(); +} + void LiveKitRemoteParticipant::_bind_methods() { ClassDB::bind_method(D_METHOD("get_track_publications"), &LiveKitRemoteParticipant::get_track_publications); } @@ -349,17 +412,18 @@ LiveKitRemoteParticipant::LiveKitRemoteParticipant() { LiveKitRemoteParticipant::~LiveKitRemoteParticipant() { } -void LiveKitRemoteParticipant::bind_remote_participant(livekit::RemoteParticipant *p) { - remote_participant_ = p; +std::shared_ptr LiveKitRemoteParticipant::get_native_remote_participant() const { + return std::static_pointer_cast(participant_.lock()); } Dictionary LiveKitRemoteParticipant::get_track_publications() const { Dictionary result; - if (!remote_participant_) { + auto rp = get_native_remote_participant(); + if (!rp) { return result; } - const auto &pubs = remote_participant_->trackPublications(); + const auto &pubs = rp->trackPublications(); for (const auto &pair : pubs) { Ref pub; pub.instantiate(); diff --git a/src/livekit_participant.h b/src/livekit_participant.h index f8d374b..5b786a7 100644 --- a/src/livekit_participant.h +++ b/src/livekit_participant.h @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -35,13 +36,17 @@ class LiveKitParticipant : public RefCounted { protected: static void _bind_methods(); - livekit::Participant *participant_ = nullptr; + // Weak, since the room owns its participants and frees them once they disconnect. + std::weak_ptr participant_; + // Kept, since it never changes, and handlers for a participant leaving (e.g. participant_disconnected) + // often run after the SDK has freed it, but still need to know who left. + std::string identity_; public: LiveKitParticipant(); ~LiveKitParticipant(); - virtual void bind_participant(livekit::Participant *p_participant); + void bind_participant(const std::weak_ptr &p_participant, const std::string &p_identity); String get_sid() const; String get_name() const; @@ -55,10 +60,17 @@ class LiveKitLocalParticipant : public LiveKitParticipant { GDCLASS(LiveKitLocalParticipant, LiveKitParticipant) private: - livekit::LocalParticipant *local_participant_ = nullptr; + // The SDK only holds published tracks weakly, and forgets their publications and events once + // they're freed, so these keep them alive until they're unpublished. + std::vector> published_tracks_; std::vector registered_rpc_methods_; DetachableThread rpc_thread_; + /// Emits rpc_method_invoked from the participant with this ID, if it hasn't been freed. + static void emit_rpc_method_invoked(uint64_t p_participant_id, const String &p_method, const String &p_request_id, + const String &p_caller_identity, const String &p_payload, double p_response_timeout); + void unregister_rpc_methods(); + protected: static void _bind_methods(); @@ -66,8 +78,7 @@ class LiveKitLocalParticipant : public LiveKitParticipant { LiveKitLocalParticipant(); ~LiveKitLocalParticipant(); - void bind_local_participant(livekit::LocalParticipant *p); - livekit::LocalParticipant *get_native_local_participant() const { return local_participant_; } + std::shared_ptr get_native_local_participant() const; void publish_data(const PackedByteArray &data, bool reliable, const PackedStringArray &destination_identities, const String &topic); void set_metadata(const String &metadata); @@ -77,6 +88,9 @@ class LiveKitLocalParticipant : public LiveKitParticipant { Dictionary get_track_publications() const; Ref publish_track(const Ref &track, const Dictionary &options); void unpublish_track(const String &track_sid); + /// Unbinds this participant and lets go of the tracks it published, for the room to call when its + /// connection ends, since apps may keep the participant afterwards. + void unbind(); // RPC (async — result delivered via rpc_response_received / rpc_error signals) void perform_rpc(const String &destination, const String &method, const String &payload, double timeout); @@ -89,9 +103,6 @@ class LiveKitLocalParticipant : public LiveKitParticipant { class LiveKitRemoteParticipant : public LiveKitParticipant { GDCLASS(LiveKitRemoteParticipant, LiveKitParticipant) -private: - livekit::RemoteParticipant *remote_participant_ = nullptr; - protected: static void _bind_methods(); @@ -99,8 +110,7 @@ class LiveKitRemoteParticipant : public LiveKitParticipant { LiveKitRemoteParticipant(); ~LiveKitRemoteParticipant(); - void bind_remote_participant(livekit::RemoteParticipant *p); - livekit::RemoteParticipant *get_native_remote_participant() const { return remote_participant_; } + std::shared_ptr get_native_remote_participant() const; Dictionary get_track_publications() const; }; diff --git a/src/livekit_room.cpp b/src/livekit_room.cpp index dec4b42..b01f777 100644 --- a/src/livekit_room.cpp +++ b/src/livekit_room.cpp @@ -141,11 +141,11 @@ LiveKitRoom::LiveKitRoom() { LiveKitRoom::~LiveKitRoom() { LiveKitPoller::instance().unregister_room(this); - // Tell any detached connect thread not to touch `this` after Connect() + // Tell any detached connect thread not to touch `this` after connect() // returns (event_mutex_, pending_events_ will be destroyed). alive_->store(false); - // Detach delegate early — this may unblock a pending Connect() by + // Detach delegate early — this may unblock a pending connect() by // preventing the SDK from waiting on delegate callbacks. if (room) { room->setDelegate(nullptr); @@ -163,9 +163,9 @@ LiveKitRoom::~LiveKitRoom() { disconnect_thread_.join_or_detach(5000); // If the connect thread finished, join it and destroy the Room normally. - // If it's still running (Connect() stuck), move the native Room into a - // detached cleanup thread so it stays alive until Connect() returns — - // destroying the Room while Connect() is in progress is use-after-free. + // If it's still running (connect() stuck), move the native Room into a + // detached cleanup thread so it stays alive until connect() returns — + // destroying the Room while connect() is in progress is use-after-free. if (connect_thread_.joinable() && !connect_thread_.finished()) { auto orphan_room = std::move(room); auto orphan_delegate = std::move(delegate); @@ -213,6 +213,9 @@ bool LiveKitRoom::connect_to_room(const String &url, const String &token, const } // Clear participant state from the old connection. + if (local_participant.is_valid()) { + local_participant->unbind(); + } local_participant.unref(); { std::lock_guard plock(participants_mutex_); @@ -239,7 +242,7 @@ bool LiveKitRoom::connect_to_room(const String &url, const String &token, const // Move the old connect thread out so the cleanup lambda can join it. std::thread old_ct = connect_thread_.release(); - // Background the old room destruction (may block waiting for Connect()). + // Background the old room destruction (may block waiting for connect()). disconnect_thread_.start([old_room = std::move(old_room), old_delegate = std::move(old_delegate), old_ct = std::move(old_ct)]() mutable { if (old_ct.joinable()) { old_ct.join(); @@ -261,14 +264,14 @@ bool LiveKitRoom::connect_to_room(const String &url, const String &token, const std::string url_str(url.utf8().get_data()); std::string token_str(token.utf8().get_data()); - // Run the blocking Connect() on a background thread so the main + // Run the blocking connect() on a background thread so the main // thread (and Godot's rendering/input loop) stays responsive. auto alive = alive_; livekit::Room *room_ptr = room.get(); connect_thread_.start([this, alive, room_ptr, url_str, token_str, room_options]() { bool success = false; try { - success = room_ptr->Connect(url_str.c_str(), token_str.c_str(), room_options); + success = room_ptr->connect(url_str, token_str, room_options); } catch (const std::exception &e) { UtilityFunctions::push_error("LiveKitRoom::connect_to_room: connection failed: ", String(e.what())); } catch (...) { @@ -304,28 +307,29 @@ void LiveKitRoom::_finalize_connection(bool success) { connection_state = STATE_CONNECTED; // Initialize local participant - livekit::LocalParticipant *lp = room->localParticipant(); - if (lp) { + if (auto lp = room->localParticipant().lock()) { local_participant.instantiate(); - local_participant->bind_participant(lp); - local_participant->bind_local_participant(lp); + local_participant->bind_participant(lp, lp->identity()); } // Initialize remote participants auto remotes = room->remoteParticipants(); for (auto const &r : remotes) { + auto remote = r.lock(); + if (!remote) { + continue; + } Ref p; p.instantiate(); - p->bind_participant(r.get()); - p->bind_remote_participant(static_cast(r.get())); + p->bind_participant(r, remote->identity()); std::lock_guard plock(participants_mutex_); remote_participants[p->get_identity()] = p; } #ifdef LIVEKIT_E2EE_SUPPORTED // Initialize E2EE manager if available - livekit::E2EEManager *mgr = room->e2eeManager(); - if (mgr) { + auto mgr = room->e2eeManager(); + if (!mgr.expired()) { e2ee_manager_.instantiate(); e2ee_manager_->bind_manager(mgr); } @@ -339,7 +343,7 @@ void LiveKitRoom::_finalize_connection(bool success) { } void LiveKitRoom::poll_events() { - // Check for connection timeout while Connect() is still running. + // Check for connection timeout while connect() is still running. if (connecting_async_.load() && connect_timeout_sec_ > 0.0) { uint64_t now = Time::get_singleton()->get_ticks_msec(); double elapsed_sec = (now - connect_start_ms_) / 1000.0; @@ -354,7 +358,7 @@ void LiveKitRoom::poll_events() { emit_signal("connection_failed", String("connection timed out after %s seconds") .replace("%s", String::num(connect_timeout_sec_, 1))); - // Swap to a fresh Room so the stuck Connect() thread can't + // Swap to a fresh Room so the stuck connect() thread can't // produce a ghost connection on the old native Room. disconnect_from_room(); return; @@ -386,6 +390,9 @@ void LiveKitRoom::disconnect_from_room() { disconnect_thread_.detach(); } + if (local_participant.is_valid()) { + local_participant->unbind(); + } local_participant.unref(); { std::lock_guard plock(participants_mutex_); @@ -410,7 +417,7 @@ void LiveKitRoom::disconnect_from_room() { room->setDelegate(delegate.get()); // Extract the raw connect thread so the disconnect lambda can wait for - // Connect() to finish before deleting the room. + // connect() to finish before deleting the room. std::thread ct = connect_thread_.release(); // SDK has no explicit disconnect; destroying the Room disconnects. @@ -440,7 +447,7 @@ String LiveKitRoom::get_sid() const { if (connecting_async_.load() || connection_state.load() != STATE_CONNECTED) return String(); if (room) { - auto info = room->room_info(); + auto info = room->roomInfo(); if (info.sid.has_value()) { return String(info.sid.value().c_str()); } @@ -452,7 +459,7 @@ String LiveKitRoom::get_name() const { if (connecting_async_.load() || connection_state.load() != STATE_CONNECTED) return String(); if (room) { - auto info = room->room_info(); + auto info = room->roomInfo(); return String(info.name.c_str()); } return String(); @@ -462,7 +469,7 @@ String LiveKitRoom::get_metadata() const { if (connecting_async_.load() || connection_state.load() != STATE_CONNECTED) return String(); if (room) { - auto info = room->room_info(); + auto info = room->roomInfo(); return String(info.metadata.c_str()); } return String(); @@ -519,16 +526,17 @@ Ref LiveKitRoom::_find_or_create_participant_by_identity(con // Godot wrappers / emits signals. void LiveKitRoom::GodotRoomDelegate::onParticipantConnected(livekit::Room &r, const livekit::ParticipantConnectedEvent &e) { - auto *rp = static_cast(e.participant); + // The event only carries a raw pointer, which the main thread may see after the room has freed it. + std::string identity(e.participant->identity()); + std::weak_ptr rp = r.remoteParticipant(identity); std::lock_guard lock(room->event_mutex_); - room->pending_events_.push_back([this, rp]() { + room->pending_events_.push_back([this, identity, rp]() { Ref p; p.instantiate(); - p->bind_participant(rp); - p->bind_remote_participant(rp); + p->bind_participant(rp, identity); { std::lock_guard plock(room->participants_mutex_); - room->remote_participants[p->get_identity()] = p; + room->remote_participants[String(identity.c_str())] = p; } room->emit_signal("participant_connected", p); }); diff --git a/src/livekit_room.h b/src/livekit_room.h index f99ac27..b9cc2b2 100644 --- a/src/livekit_room.h +++ b/src/livekit_room.h @@ -91,7 +91,7 @@ class LiveKitRoom : public RefCounted { std::atomic disconnected_emitted_{false}; // Connection timeout: poll_events() checks elapsed time and forces - // failure when Connect() takes longer than this (seconds, 0 = no limit). + // failure when connect() takes longer than this (seconds, 0 = no limit). double connect_timeout_sec_{15.0}; uint64_t connect_start_ms_{0}; diff --git a/src/livekit_track.cpp b/src/livekit_track.cpp index cf54dd1..79d6606 100644 --- a/src/livekit_track.cpp +++ b/src/livekit_track.cpp @@ -327,7 +327,7 @@ bool LiveKitTrack::get_muted() const { int LiveKitTrack::get_stream_state() const { if (track_) { - return (int)track_->stream_state(); + return (int)track_->streamState(); } return STATE_UNKNOWN; } diff --git a/src/livekit_video_stream.cpp b/src/livekit_video_stream.cpp index 4f565e8..cb1c21b 100644 --- a/src/livekit_video_stream.cpp +++ b/src/livekit_video_stream.cpp @@ -77,7 +77,8 @@ Ref LiveKitVideoStream::from_track(const Ref & } Ref LiveKitVideoStream::from_participant(const Ref &participant, int source) { - if (participant.is_null() || !participant->get_native_remote_participant()) { + auto native_participant = participant.is_valid() ? participant->get_native_remote_participant() : nullptr; + if (!native_participant) { UtilityFunctions::push_error("LiveKitVideoStream::from_participant: invalid participant"); return Ref(); } @@ -86,7 +87,7 @@ Ref LiveKitVideoStream::from_participant(const Refget_native_remote_participant(), + *native_participant, (livekit::TrackSource)source, opts); if (!native_stream) { @@ -131,7 +132,8 @@ bool LiveKitVideoStream::poll() { std::unique_lock lock(frame_mutex_, std::try_to_lock); if (!lock.owns_lock()) { uint32_t count = lock_contention_count_.fetch_add(1) + 1; - if (count == 1 || (count % 100) == 0) { + // An occasional skipped poll is harmless, since the next one picks up what the reader added. + if ((count % 100) == 0) { UtilityFunctions::push_warning("LiveKitVideoStream::poll: lock contention (", count, " skipped frames)"); } return false; diff --git a/src/register_types.cpp b/src/register_types.cpp index 5ebf0d5..70639bf 100644 --- a/src/register_types.cpp +++ b/src/register_types.cpp @@ -13,6 +13,9 @@ #endif #include "livekit_poller.h" #include "thread_pool.h" +#ifdef _WIN32 +#include "windows_pin.h" +#endif #ifdef ANDROID_ENABLED #include "android_jni_init.h" #endif @@ -43,6 +46,10 @@ void initialize_livekit_module(ModuleInitializationLevel p_level) { livekit_android_jni_init(); #endif +#ifdef _WIN32 + livekit_pin_modules(); +#endif + livekit::initialize(); // Room diff --git a/src/sdk_call.h b/src/sdk_call.h new file mode 100644 index 0000000..71c6243 --- /dev/null +++ b/src/sdk_call.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +#include + +/// Calls into the LiveKit SDK, reporting its exceptions with push_error() and returning false +/// instead. (It throws when the room has disconnected or is disconnecting.) +template +bool call_sdk(const char *method, F &&call) { + try { + call(); + return true; + } catch (const std::exception &e) { + godot::UtilityFunctions::push_error(method, ": ", godot::String(e.what())); + return false; + } +} diff --git a/src/web/README.md b/src/web/README.md index 73b15fa..f186c81 100644 --- a/src/web/README.md +++ b/src/web/README.md @@ -17,7 +17,7 @@ The web build is meant to behave like the native one, and the shared tests in `t ## Rooms and participants -- **Participants belong to their connection.** A participant object kept from an earlier connection reports "participant is no longer connected" when used, rather than acting on a later connection's participant. (Natively, the SDK has freed those participants.) +- **Participants belong to their connection.** A participant object kept from an earlier connection reports "participant is no longer connected" when used, rather than acting on a later connection's participant. (Natively, it reports that it isn't bound, and its getters return empty values.) - **Publishing is asynchronous.** The publication `publish_track()` returns starts with an empty sid, which fills in once `local_track_published` fires. From then on, `get_track_publications()` includes it. ## Audio diff --git a/src/windows_pin.cpp b/src/windows_pin.cpp new file mode 100644 index 0000000..a8003a6 --- /dev/null +++ b/src/windows_pin.cpp @@ -0,0 +1,16 @@ +#include "windows_pin.h" + +#define WIN32_LEAN_AND_MEAN +#include + +// LiveKit's runtime threads (behind subscriptions) can outlive livekit::shutdown(). When Godot +// unloads the extension at exit, Windows unmaps livekit_ffi.dll under them and the process +// crashes on its way out. Pinned modules are never unloaded, so those threads just end with the +// process. (Linux keeps such libraries mapped anyway.) +void livekit_pin_modules() { + HMODULE module = nullptr; + GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, L"livekit_ffi.dll", &module); + GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, L"livekit.dll", &module); + GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, + reinterpret_cast(&livekit_pin_modules), &module); +} diff --git a/src/windows_pin.h b/src/windows_pin.h new file mode 100644 index 0000000..d0c8e51 --- /dev/null +++ b/src/windows_pin.h @@ -0,0 +1,4 @@ +#pragma once + +// Keeps this library and LiveKit's DLLs loaded until the process ends (Windows only). +void livekit_pin_modules(); diff --git a/test/android/android_runner.gd b/test/android/android_runner.gd new file mode 100644 index 0000000..e56d13b --- /dev/null +++ b/test/android/android_runner.gd @@ -0,0 +1,38 @@ +extends Node +## Runs GUT inside an Android export, where tests can't be passed on a command line. test_android.sh +## bakes the tests to run, and env vars read by the test helpers, into the export as user arguments +## (`tests=res://test/unit/test_room.gd,... RUN_E2E=1 ...`). +## Prints `GUT_ANDROID_STARTED` once it's running and `GUT_ANDROID_RESULT tests=N failing=N pending=N` +## when done, for test_android.sh to parse, and quits. + +const GutConfig := preload("res://addons/gut/gut_config.gd") +const GutRunner := preload("res://addons/gut/gui/GutRunner.tscn") + + +func _ready() -> void: + print("GUT_ANDROID_STARTED") + var tests := PackedStringArray() + for arg in OS.get_cmdline_user_args(): + var parts := arg.split("=", true, 1) + if parts.size() != 2: + continue + if parts[0] == "tests": + tests = parts[1].split(",", false) + else: + OS.set_environment(parts[0], parts[1]) + var config := GutConfig.new() + config.options.tests = Array(tests) + config.options.should_exit = false + config.options.log_level = 1 + var runner := GutRunner.instantiate() + runner.set_gut_config(config) + add_child(runner) + var gut = runner.get_gut() + gut.end_run.connect(func() -> void: + print("GUT_ANDROID_RESULT tests=%d failing=%d pending=%d" % [ + gut.get_test_count(), gut.get_fail_count(), gut.get_pending_count() + ]) + # Quitting flushes the log file, and tells test_android.sh the run is over. + get_tree().quit() + ) + runner.run_tests(false) diff --git a/test/android/android_runner.tscn b/test/android/android_runner.tscn new file mode 100644 index 0000000..18dc1cb --- /dev/null +++ b/test/android/android_runner.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/android/android_runner.gd" id="1"] + +[node name="AndroidRunner" type="Node"] +script = ExtResource("1") diff --git a/test/dev_token.py b/test/dev_token.py new file mode 100644 index 0000000..46779a5 --- /dev/null +++ b/test/dev_token.py @@ -0,0 +1,37 @@ +"""Prints a join token for the integration tests, signed with `livekit-server --dev`'s key. + +Usage: python3 test/dev_token.py +""" + +import base64 +import hashlib +import hmac +import json +import sys +import time + +_API_KEY = "devkey" +_API_SECRET = b"secret" + + +def main() -> None: + identity, room = sys.argv[1:3] + now = int(time.time()) + claims = { + "iss": _API_KEY, + "sub": identity, + "iat": now, + "nbf": now, + "exp": now + 3600, + "video": {"room": room, "roomJoin": True, "canPublish": True, "canSubscribe": True, "canPublishData": True}, + } + signing = _b64(json.dumps({"alg": "HS256", "typ": "JWT"}).encode()) + "." + _b64(json.dumps(claims).encode()) + print(signing + "." + _b64(hmac.new(_API_SECRET, signing.encode(), hashlib.sha256).digest())) + + +def _b64(data: bytes) -> str: + return base64.urlsafe_b64encode(data).rstrip(b"=").decode() + + +if __name__ == "__main__": + main() diff --git a/test/e2e/test_e2ee.gd b/test/e2e/test_e2ee.gd index 15246a2..202c878 100644 --- a/test/e2e/test_e2ee.gd +++ b/test/e2e/test_e2ee.gd @@ -7,7 +7,7 @@ const KEY := "correct horse battery staple" func test_encrypted_media_and_data_reach_room_with_same_key(): - if _skip_if_unsupported(): + if _skip_if_encrypted_video_may_not_start(): return assert_true(await _connect_both_rooms(_e2ee_options(KEY), _e2ee_options(KEY)), "Both rooms should connect") var statuses := {} @@ -30,7 +30,7 @@ func test_encrypted_media_and_data_reach_room_with_same_key(): func test_room_with_a_different_key_cannot_decode(): - if _skip_if_unsupported(): + if _skip_if_no_server(): return assert_true(await _connect_both_rooms(_e2ee_options(KEY), _e2ee_options("wrong key")), "Both rooms should connect") var received := [] @@ -45,7 +45,7 @@ func test_room_with_a_different_key_cannot_decode(): func test_rotated_key_is_used_once_switched_to(): - if _skip_if_unsupported(): + if _skip_without_cryptor_key_index(): return assert_true(await _connect_both_rooms(_e2ee_options(KEY), _e2ee_options(KEY)), "Both rooms should connect") var video := _publish_red_video() @@ -75,7 +75,7 @@ func test_rotated_key_is_used_once_switched_to(): func test_data_sent_right_after_switching_keys_uses_the_new_key(): - if _skip_if_unsupported(): + if _skip_without_cryptor_key_index(): return # Room2 can only decrypt the key room1 switches to. var next_key := "next key".to_utf8_buffer() @@ -98,7 +98,7 @@ func test_data_sent_right_after_switching_keys_uses_the_new_key(): func test_data_sent_just_before_switching_keys_is_delivered(): - if _skip_if_unsupported(): + if _skip_without_cryptor_key_index(): return assert_true(await _connect_both_rooms(_e2ee_options(KEY), _e2ee_options(KEY)), "Both rooms should connect") var next_key := "next key".to_utf8_buffer() @@ -120,7 +120,7 @@ func test_data_sent_just_before_switching_keys_is_delivered(): func test_key_changes_requested_together_each_take_effect(): - if _skip_if_unsupported(): + if _skip_without_cryptor_key_index(): return assert_true(await _connect_both_rooms(_e2ee_options(KEY), _e2ee_options(KEY)), "Both rooms should connect") # Frame cryptors (which switch keys) only exist for tracks. @@ -155,7 +155,7 @@ func _await_key_change(key_provider: LiveKitKeyProvider, key_index: int, old_key func test_receiver_follows_a_ratcheted_key(): - if _skip_if_unsupported(): + if _skip_if_encrypted_video_may_not_start(): return assert_true(await _connect_both_rooms(_e2ee_options(KEY), _e2ee_options(KEY)), "Both rooms should connect") var video := _publish_red_video() @@ -179,7 +179,7 @@ func test_receiver_follows_a_ratcheted_key(): func test_data_only_receiver_follows_a_ratcheted_key(): - if _skip_if_unsupported(): + if _skip_if_no_server(): return assert_true(await _connect_both_rooms(_e2ee_options(KEY), _e2ee_options(KEY)), "Both rooms should connect") var received := [] @@ -198,7 +198,7 @@ func test_data_only_receiver_follows_a_ratcheted_key(): func test_receiver_records_a_participant_key_it_ratcheted(): - if _skip_if_unsupported(): + if _skip_if_encrypted_video_may_not_start(): return var no_shared_key := {"e2ee": LiveKitE2eeOptions.new()} assert_true(await _connect_both_rooms(no_shared_key, {"e2ee": LiveKitE2eeOptions.new()}), "Both rooms should connect") @@ -224,9 +224,13 @@ func test_receiver_records_a_participant_key_it_ratcheted(): func test_encryption_turned_off_and_on_again_stays_on(): - if _skip_if_unsupported(): + if _skip_if_no_server(): return - assert_true(await _connect_both_rooms(_e2ee_options(KEY), _e2ee_options("wrong key")), "Both rooms should connect") + var room2_options := _e2ee_options("wrong key") + # Natively, a receiver that's ratcheting a key it can't decrypt with puts that key back when it + # gives up, even if set_shared_key() replaced it in the meantime. + room2_options.e2ee.ratchet_window_size = 0 + assert_true(await _connect_both_rooms(_e2ee_options(KEY), room2_options), "Both rooms should connect") var manager := _room.get_e2ee_manager() manager.set_enabled(false) manager.set_enabled(true) @@ -245,7 +249,7 @@ func test_encryption_turned_off_and_on_again_stays_on(): func test_video_flows_unencrypted_once_encryption_is_turned_off(): - if _skip_if_unsupported(): + if _skip_if_no_server(): return assert_true(await _connect_both_rooms(_e2ee_options(KEY), _e2ee_options("wrong key")), "Both rooms should connect") var video := _publish_red_video() @@ -254,6 +258,8 @@ func test_video_flows_unencrypted_once_encryption_is_turned_off(): assert_true(await _poll_until(_room, func(): return not local.get_track_publications().is_empty(), 10.0), "video published") _room.get_e2ee_manager().set_enabled(false) + # Natively, a receiver keeps trying to decrypt until it turns encryption off too. + _room2.get_e2ee_manager().set_enabled(false) # Room2 can only decode the video if it isn't encrypted. var frames := await _count_red_frames(video, 5, 15.0) @@ -262,7 +268,7 @@ func test_video_flows_unencrypted_once_encryption_is_turned_off(): func test_data_sent_as_encryption_is_turned_back_on_is_delivered(): - if _skip_if_unsupported(): + if _skip_if_no_server(): return assert_true(await _connect_both_rooms(_e2ee_options(KEY), _e2ee_options(KEY)), "Both rooms should connect") var manager := _room.get_e2ee_manager() @@ -289,7 +295,7 @@ func test_correct_participant_key_is_used_after_a_wrong_one_exceeds_the_failure_ ## Asserts that room2 decodes room1's video once it's given the right key, after failing to with a ## wrong one. func _assert_recovers_from_wrong_key(shared: bool) -> void: - if _skip_if_unsupported(): + if _skip_if_no_server(): return var key := KEY.to_utf8_buffer() var options := _intolerant_options(key if shared else PackedByteArray()) @@ -312,6 +318,28 @@ func _assert_recovers_from_wrong_key(shared: bool) -> void: _close(video) +## Skips tests that need encrypted video to start when the rooms connect, which on Android it +## sometimes never does (NodotProject/godot-livekit#19). +func _skip_if_encrypted_video_may_not_start() -> bool: + if _skip_if_no_server(): + return true + if OS.has_feature("android"): + pending("Encrypted video sometimes never starts on Android (NodotProject/godot-livekit#19)") + return true + return false + + +func _skip_without_cryptor_key_index() -> bool: + if _skip_if_no_server(): + return true + if not OS.has_feature("web"): + # client-sdk-cpp's FrameCryptor doesn't say which track it's for, which the FFI requires + # (livekit/client-sdk-cpp#311). + pending("Native LiveKitFrameCryptor.set_key_index() isn't supported with client-sdk-cpp 1.13.1") + return true + return false + + ## Options that drop a key after one decryption failure, with a shared key unless it's empty. func _intolerant_options(shared_key: PackedByteArray) -> Dictionary: var options := LiveKitE2eeOptions.new() @@ -322,16 +350,6 @@ func _intolerant_options(shared_key: PackedByteArray) -> Dictionary: return {"e2ee": options} -func _skip_if_unsupported() -> bool: - if _skip_if_no_server(): - return true - if not OS.has_feature("web"): - # client-sdk-cpp v0.3.1 builds its E2EEManager from a room handle that isn't set yet. - pending("Native E2EE crashes on connect with client-sdk-cpp v0.3.1") - return true - return false - - func _e2ee_options(key: String) -> Dictionary: var options := LiveKitE2eeOptions.new() options.shared_key = key.to_utf8_buffer() diff --git a/test/e2e/test_native_lifetimes.gd b/test/e2e/test_native_lifetimes.gd new file mode 100644 index 0000000..3d1f0a1 --- /dev/null +++ b/test/e2e/test_native_lifetimes.gd @@ -0,0 +1,76 @@ +extends "res://test/e2e/e2e_helper.gd" +## E2E: using objects after the native SDK has freed them. (test/web/test_room_lifecycle.gd covers +## the web build, where participants outlive their connection.) + + +func test_remote_participant_kept_after_it_leaves_is_unbound_but_keeps_its_identity(): + if _skip_on_web(): + return + assert_true(await _connect_both_rooms(), "Both rooms should connect") + var departed := [] + _room.participant_disconnected.connect(func(p): departed.append(p)) + + _room2.disconnect_from_room() + await _poll_until(_room, func(): return not departed.is_empty(), 10.0) + await _poll_until(_room, func(): return false, 1.0) + + var p: LiveKitRemoteParticipant = departed[0] + assert_eq([p.get_identity(), p.get_attributes(), p.get_track_publications()], ["e2e-user-2", {}, {}]) + + +func test_local_participant_kept_after_disconnecting_is_unbound_but_keeps_its_identity(): + if _skip_on_web(): + return + assert_true(await _connect_both_rooms(), "Both rooms should connect") + var local := _room.get_local_participant() + + _room.disconnect_from_room() + # Right away, while the SDK is still tearing the room down. + local.publish_data("stale".to_utf8_buffer(), true) + + assert_eq( + [local.get_identity(), local.get_attributes(), local.get_track_publications(), _take_errors()], + ["e2e-user-1", {}, {}, ["LiveKitLocalParticipant::publish_data: not bound"]], + ) + + +func test_e2ee_manager_kept_after_disconnecting_is_unbound(): + if _skip_on_web(): + return + var options := LiveKitE2eeOptions.new() + options.shared_key = "key".to_utf8_buffer() + assert_true(await _connect_both_rooms({"e2ee": options}, {"e2ee": options}), "Both rooms should connect") + var manager := _room.get_e2ee_manager() + var key_provider := manager.get_key_provider() + + _room.disconnect_from_room() + # Used while the SDK is still tearing the room down, they may fail, but mustn't crash. + key_provider.set_shared_key("new key".to_utf8_buffer(), 0) + manager.set_enabled(false) + await get_tree().create_timer(2.0).timeout + _take_errors() + key_provider.set_shared_key("new key".to_utf8_buffer(), 0) + + assert_eq( + [manager.get_enabled(), manager.get_key_provider() == null, _take_errors()], + [false, true, ["LiveKitKeyProvider::set_shared_key: not bound"]], + ) + + +func _skip_on_web() -> bool: + if _skip_if_no_server(): + return true + if OS.has_feature("web"): + pending("Native only") + return true + return false + + +## Returns the messages of the errors pushed so far in this test, so that they don't fail it. +func _take_errors() -> Array: + var messages := [] + for error in get_errors(): + if not error.handled and error.is_push_error(): + error.handled = true + messages.append(error.code) + return messages diff --git a/test/e2e/test_track_lifecycle.gd b/test/e2e/test_track_lifecycle.gd index e2d8b9d..eaa3130 100644 --- a/test/e2e/test_track_lifecycle.gd +++ b/test/e2e/test_track_lifecycle.gd @@ -114,6 +114,32 @@ func test_publish_unpublish_cycle(): assert_true(remote_gone, "Room2 should see track removed") + +func test_track_published_without_keeping_it_stays_published(): + if _skip_if_no_server(): + return + assert_true(await _connect_both_rooms(), "Both rooms should connect") + var lp = _room.get_local_participant() + var published_sids := [] + _room.local_track_published.connect(func(publication, _track): published_sids.append(publication.get_sid())) + + _publish_unkept_audio_track(lp) + + assert_true(await _poll_until(_room, func(): return not published_sids.is_empty(), 10.0), + "Room should publish the track") + # Natively, the SDK forgets a track's publication once the track is freed (but leaves it + # published), so give it a moment to be freed. + await _poll_both(func(): return false, 2.0) + assert_eq([lp.get_track_publications().keys(), _get_remote_track_publications(_room2).map(func(p): return p.get_sid())], + [published_sids, published_sids]) + + +## Publishes a track that nothing else refers to once this returns (unlike a temporary in the +## caller, which GDScript keeps until the caller returns). +func _publish_unkept_audio_track(lp: LiveKitLocalParticipant) -> void: + lp.publish_track(LiveKitLocalAudioTrack.create("e2e_unkept", LiveKitAudioSource.create(48000, 1, 0)), {}) + + func test_multiple_tracks(): if _skip_if_no_server(): return diff --git a/test/unit/test_platform.gd b/test/unit/test_platform.gd index 7d59625..330e3f6 100644 --- a/test/unit/test_platform.gd +++ b/test/unit/test_platform.gd @@ -36,6 +36,9 @@ func test_all_classes_registered(): if OS.has_feature("web"): # Browsers apply their own audio processing to captured audio. classes.erase("LiveKitAudioProcessingModule") + if OS.has_feature("android"): + # Android builds leave out screen capture (see build.sh). + classes.erase("LiveKitScreenCapture") for cls in classes: assert_true(ClassDB.class_exists(cls), diff --git a/test/unit/test_screen_capture_threading.gd b/test/unit/test_screen_capture_threading.gd new file mode 100644 index 0000000..ccd9a98 --- /dev/null +++ b/test/unit/test_screen_capture_threading.gd @@ -0,0 +1,74 @@ +extends GutTest +## Threading stress tests for LiveKitScreenCapture, kept apart from test_threading_stress.gd +## because builds without screen capture (e.g. Android) can't compile them. Like those, they use a +## 500ms frame budget to detect main-thread blocking. + +const FRAME_BUDGET_MS := 500 + + +# --------------------------------------------------------------------------- +# 1. ScreenCapture start/stop rapid cycles (display-dependent) +# --------------------------------------------------------------------------- + +func test_screen_capture_rapid_start_stop(): + var perms = LiveKitScreenCapture.check_permissions() + if perms.get("status", 2) == LiveKitScreenCapture.PERMISSION_ERROR: + pending("No display available — skipping") + return + var capture = LiveKitScreenCapture.create() + if capture == null: + pending("create() returned null — capture not available") + return + var before := Time.get_ticks_msec() + for i in range(5): + capture.start() + capture.stop() + var elapsed := Time.get_ticks_msec() - before + assert_lt(elapsed, FRAME_BUDGET_MS, + "Rapid start/stop should not crash (took %d ms)" % elapsed) + capture.close() + + +# --------------------------------------------------------------------------- +# 2. ScreenCapture close during active capture (display-dependent) +# --------------------------------------------------------------------------- + +func test_screen_capture_close_during_capture(): + var perms = LiveKitScreenCapture.check_permissions() + if perms.get("status", 2) == LiveKitScreenCapture.PERMISSION_ERROR: + pending("No display available — skipping") + return + var capture = LiveKitScreenCapture.create() + if capture == null: + pending("create() returned null — capture not available") + return + capture.start() + # Close immediately without stop — close() should call stop() internally + var before := Time.get_ticks_msec() + capture.close() + var elapsed := Time.get_ticks_msec() - before + assert_lt(elapsed, FRAME_BUDGET_MS, + "Close during active capture should not block (took %d ms)" % elapsed) + + +# --------------------------------------------------------------------------- +# 3. ScreenCapture double close +# --------------------------------------------------------------------------- + +func test_screen_capture_double_close(): + var capture := LiveKitScreenCapture.new() + capture.close() + capture.close() + assert_true(true, "ScreenCapture double close should not crash") + + +# --------------------------------------------------------------------------- +# 4. ScreenCapture poll after close +# --------------------------------------------------------------------------- + +func test_screen_capture_poll_after_close(): + var capture := LiveKitScreenCapture.new() + capture.close() + var result = capture.poll() + assert_false(result, + "poll() after close should return false without crash") diff --git a/test/unit/test_threading_stress.gd b/test/unit/test_threading_stress.gd index 9a61112..55fdef6 100644 --- a/test/unit/test_threading_stress.gd +++ b/test/unit/test_threading_stress.gd @@ -184,75 +184,7 @@ func test_audio_stream_poll_after_close(): # --------------------------------------------------------------------------- -# 11. ScreenCapture start/stop rapid cycles (display-dependent) -# --------------------------------------------------------------------------- - -func test_screen_capture_rapid_start_stop(): - var perms = LiveKitScreenCapture.check_permissions() - if perms.get("status", 2) == LiveKitScreenCapture.PERMISSION_ERROR: - pending("No display available — skipping") - return - var capture = LiveKitScreenCapture.create() - if capture == null: - pending("create() returned null — capture not available") - return - var before := Time.get_ticks_msec() - for i in range(5): - capture.start() - capture.stop() - var elapsed := Time.get_ticks_msec() - before - assert_lt(elapsed, FRAME_BUDGET_MS, - "Rapid start/stop should not crash (took %d ms)" % elapsed) - capture.close() - - -# --------------------------------------------------------------------------- -# 12. ScreenCapture close during active capture (display-dependent) -# --------------------------------------------------------------------------- - -func test_screen_capture_close_during_capture(): - var perms = LiveKitScreenCapture.check_permissions() - if perms.get("status", 2) == LiveKitScreenCapture.PERMISSION_ERROR: - pending("No display available — skipping") - return - var capture = LiveKitScreenCapture.create() - if capture == null: - pending("create() returned null — capture not available") - return - capture.start() - # Close immediately without stop — close() should call stop() internally - var before := Time.get_ticks_msec() - capture.close() - var elapsed := Time.get_ticks_msec() - before - assert_lt(elapsed, FRAME_BUDGET_MS, - "Close during active capture should not block (took %d ms)" % elapsed) - - -# --------------------------------------------------------------------------- -# 13. ScreenCapture double close -# --------------------------------------------------------------------------- - -func test_screen_capture_double_close(): - var capture := LiveKitScreenCapture.new() - capture.close() - capture.close() - assert_true(true, "ScreenCapture double close should not crash") - - -# --------------------------------------------------------------------------- -# 14. ScreenCapture poll after close -# --------------------------------------------------------------------------- - -func test_screen_capture_poll_after_close(): - var capture := LiveKitScreenCapture.new() - capture.close() - var result = capture.poll() - assert_false(result, - "poll() after close should return false without crash") - - -# --------------------------------------------------------------------------- -# 15. Room connect/disconnect interleaved with property access stress +# 11. Room connect/disconnect interleaved with property access stress # --------------------------------------------------------------------------- func test_connect_disconnect_property_stress(): @@ -269,7 +201,7 @@ func test_connect_disconnect_property_stress(): # --------------------------------------------------------------------------- -# 16. Multiple rooms with interleaved poll_events +# 12. Multiple rooms with interleaved poll_events # --------------------------------------------------------------------------- func test_multiple_rooms_interleaved_poll(): @@ -292,7 +224,7 @@ func test_multiple_rooms_interleaved_poll(): # --------------------------------------------------------------------------- -# 17. Room disconnect then immediate reconnect (rapid reuse) +# 13. Room disconnect then immediate reconnect (rapid reuse) # --------------------------------------------------------------------------- func test_rapid_disconnect_reconnect(): @@ -310,7 +242,7 @@ func test_rapid_disconnect_reconnect(): # --------------------------------------------------------------------------- -# 18. VideoStream create and immediately null (GC) +# 14. VideoStream create and immediately null (GC) # --------------------------------------------------------------------------- func test_video_stream_immediate_gc(): @@ -324,7 +256,7 @@ func test_video_stream_immediate_gc(): # --------------------------------------------------------------------------- -# 19. AudioStream create and immediately null (GC) +# 15. AudioStream create and immediately null (GC) # --------------------------------------------------------------------------- func test_audio_stream_immediate_gc(): @@ -338,7 +270,7 @@ func test_audio_stream_immediate_gc(): # --------------------------------------------------------------------------- -# 20. Room connect with poll in tight loop then disconnect +# 16. Room connect with poll in tight loop then disconnect # --------------------------------------------------------------------------- func test_poll_tight_loop_during_connect(): diff --git a/test_android.sh b/test_android.sh new file mode 100755 index 0000000..c87fa37 --- /dev/null +++ b/test_android.sh @@ -0,0 +1,337 @@ +#!/bin/bash + +# Runs GUT tests on an Android device or emulator. +# +# Requires: an Android build for the device's architecture (./build.sh android [arm64|x86_64]), +# Godot with its Android export templates installed, a device or emulator connected to adb, the +# Android SDK (ANDROID_HOME) and a JDK (JAVA_HOME) for Godot's Gradle build. Unit tests run by +# default; integration and e2e tests are added when LIVEKIT_TEST_URL / RUN_E2E are set, as in +# test.sh. On an emulator, the host's localhost is 10.0.2.2. +# +# Exporting and running can also be done separately, e.g. to export before starting an emulator, +# since both need a lot of CPU. The tests to run and the env vars are fixed when exporting. +# +# Usage: ./test_android.sh [--arch arm64|x86_64] [res://test/path/to/test.gd ...] +# ./test_android.sh --export-only tests.apk --arch arm64|x86_64 [res://test/path/to/test.gd ...] +# ./test_android.sh --apk tests.apk + +set -euo pipefail + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# Android builds leave out screen capture, so these scripts don't compile there. +EXCLUDED_TESTS=( + res://test/unit/test_screen_capture.gd + res://test/unit/test_screen_capture_threading.gd + res://test/e2e/test_screen_capture.gd +) +PASSTHROUGH_ENV=(LIVEKIT_TEST_URL LIVEKIT_TOKEN_1 LIVEKIT_TOKEN_2 RUN_E2E LIVEKIT_E2E_URL LIVEKIT_E2E_ROOM LIVEKIT_API_KEY LIVEKIT_API_SECRET) +PACKAGE="org.godotlivekit.tests" +# Godot's log in the app's user:// folder. +APP_LOG="test.log" +TIMEOUT_SEC="${ANDROID_TEST_TIMEOUT:-1800}" +# Homebrew's coreutils names it gtimeout on macOS. +TIMEOUT_CMD="timeout" +if ! command -v timeout &>/dev/null && command -v gtimeout &>/dev/null; then + TIMEOUT_CMD="gtimeout" +fi + +if [ -f "./godot" ]; then + GODOT_CMD="./godot" +else + GODOT_CMD="${GODOT:-godot}" +fi +if command -v adb &> /dev/null; then + ADB="adb" +else + ADB="${ANDROID_HOME:-}/platform-tools/adb" +fi + +arch="" +export_only="" +apk="" +tests=() +while [ $# -gt 0 ]; do + case "$1" in + --arch) arch="$2"; shift 2 ;; + --export-only) export_only="$2"; shift 2 ;; + --apk) apk="$2"; shift 2 ;; + *) tests+=("$1"); shift ;; + esac +done +if [ -n "$apk" ] && { [ -n "$export_only" ] || [ ${#tests[@]} -gt 0 ]; }; then + echo -e "${RED}--apk runs an exported build as is, so it can't be combined with --export-only or tests.${NC}" + exit 1 +fi + +if [ -z "$apk" ] && [ -z "$arch" ]; then + if ! abi=$("$ADB" shell getprop ro.product.cpu.abi 2>/dev/null | tr -d '\r'); then + echo -e "${RED}No Android device found. Start an emulator or connect a device, or pass --arch.${NC}" + exit 1 + fi + case "$abi" in + arm64-v8a) arch="arm64" ;; + x86_64) arch="x86_64" ;; + *) + echo -e "${RED}Unsupported device ABI: ${abi}${NC}" + exit 1 + ;; + esac +fi + +WORK_DIR=$(mktemp -d) +LOG_FILE="$WORK_DIR/test.log" +STARTED_APP=false +HAD_PRESETS=false +WROTE_PRESETS=false +cleanup() { + [ "$STARTED_APP" = true ] && "$ADB" shell am force-stop "$PACKAGE" >/dev/null 2>&1 || true + # The test preset temporarily replaces the developer's own export presets. (Nothing to undo if + # it was never written, e.g. because backing up the original failed.) + if [ "$WROTE_PRESETS" = true ]; then + if [ "$HAD_PRESETS" = true ]; then + mv -f "$WORK_DIR/export_presets.cfg" export_presets.cfg + else + rm -f export_presets.cfg + fi + fi + rm -rf "$WORK_DIR" +} +trap cleanup EXIT +trap 'exit 130' INT TERM + +## Exports the test app to the given path. +export_apk() { + local apk_path="$1" + if [ ! -f "addons/godot-livekit/bin/libgodot-livekit.android.${arch}.so" ]; then + echo -e "${RED}Android ${arch} build not found. Run ./build.sh android ${arch} first.${NC}" + exit 1 + fi + if [ ! -f livekit-sdk/java/libwebrtc.jar ]; then + echo -e "${RED}livekit-sdk/java/libwebrtc.jar not found. Rebuild with ./build.sh android ${arch}.${NC}" + exit 1 + fi + + godot_version=$($GODOT_CMD --version | sed -E 's/^([0-9]+\.[0-9]+(\.[0-9]+)?\.[a-z0-9_]+).*/\1/') + templates_dir="${GODOT_TEMPLATES_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/godot/export_templates/${godot_version}}" + if [ ! -f "$templates_dir/android_source.zip" ]; then + echo -e "${RED}Godot's Android export templates for ${godot_version} weren't found in ${templates_dir}.${NC}" + exit 1 + fi + + if [ ${#tests[@]} -eq 0 ]; then + dirs=(test/unit) + [ -n "${LIVEKIT_TEST_URL:-}" ] && dirs+=(test/integration) + [ -n "${RUN_E2E:-}" ] && dirs+=(test/e2e) + for path in $(find "${dirs[@]}" -maxdepth 1 -name 'test_*.gd' | sort); do + t="res://$path" + [[ " ${EXCLUDED_TESTS[*]} " == *" $t "* ]] || tests+=("$t") + done + fi + + # Exporting with Gradle needs Godot's Android build template in the project. The plugin doesn't add + # WebRTC's Java classes to exports yet, so the test build adds them to the template. + if [ "$(cat android/.build_version 2>/dev/null)" != "$godot_version" ]; then + # A template this script didn't install may have the project's own changes, so leave it alone. + if [ -d android ] && [ ! -f android/.livekit_test_template ]; then + echo -e "${RED}android/ has an Android build template this script didn't install. Move it aside to run the tests.${NC}" + exit 1 + fi + echo -e "${YELLOW}Installing Godot's Android build template...${NC}" + rm -rf android + mkdir -p android/build + unzip -q "$templates_dir/android_source.zip" -d android/build + echo "$godot_version" > android/.build_version + touch android/.livekit_test_template + touch android/build/.gdignore + fi + mkdir -p android/build/libs/debug + cp livekit-sdk/java/libwebrtc.jar android/build/libs/debug/ + + # The keystore that signs debug exports. Godot creates one for the editor, but not when headless. + if [ -z "${GODOT_ANDROID_KEYSTORE_DEBUG_PATH:-}" ]; then + export GODOT_ANDROID_KEYSTORE_DEBUG_PATH="$WORK_DIR/debug.keystore" + export GODOT_ANDROID_KEYSTORE_DEBUG_USER="androiddebugkey" + export GODOT_ANDROID_KEYSTORE_DEBUG_PASSWORD="android" + keytool -genkeypair -keystore "$GODOT_ANDROID_KEYSTORE_DEBUG_PATH" -alias androiddebugkey \ + -storepass android -keypass android -keyalg RSA -validity 365 -dname "CN=Android Debug,O=Android,C=US" \ + >/dev/null 2>&1 + fi + + # Android has no command line, so the tests and env vars become user arguments in the export. + # The emulator's Vulkan driver crashes Godot, so tests render with OpenGL. + user_args="tests=$(IFS=,; echo "${tests[*]}")" + for key in "${PASSTHROUGH_ENV[@]}"; do + [ -n "${!key:-}" ] && user_args="$user_args $key=${!key}" + done + extra_args="res://test/android/android_runner.tscn --rendering-method gl_compatibility --rendering-driver opengl3 --log-file user://$APP_LOG ++ $user_args" + + if [ -f export_presets.cfg ]; then + cp export_presets.cfg "$WORK_DIR/export_presets.cfg" + HAD_PRESETS=true + fi + WROTE_PRESETS=true + cat > export_presets.cfg </dev/null 2>&1 || true + rm -f "$apk_path" + mkdir -p "$(dirname "$apk_path")" + if ! $TIMEOUT_CMD 1200 $GODOT_CMD --headless --export-debug "Android Test" "$apk_path" >"$WORK_DIR/export.log" 2>&1 \ + || [ ! -f "$apk_path" ]; then + tail -40 "$WORK_DIR/export.log" + echo -e "${RED}Android export failed.${NC}" + exit 1 + fi + # Gradle leaves a daemon running, which would compete with an emulator for memory. + (cd android/build && ./gradlew --stop >/dev/null 2>&1) || true + echo -e "${GREEN}Exported ${#tests[@]} test script(s) to ${apk_path}${NC}" +} + +if [ -z "$apk" ]; then + if [ -n "$export_only" ]; then + export_apk "$export_only" + exit 0 + fi + apk="$WORK_DIR/tests.apk" + export_apk "$apk" +fi + +# Launches the app and waits for it to exit, then reads its log. Sets app_exited and result. +run_app() { + "$ADB" shell run-as "$PACKAGE" rm -f "files/$APP_LOG" >/dev/null 2>&1 || true + "$ADB" shell am start -W -n "$PACKAGE/com.godot.game.GodotApp" >/dev/null + # Emulators can drop off adb for a moment, so a failed adb command doesn't mean the app has + # stopped. Only adb answering that the process is gone does. + app_exited=false + while [ $SECONDS -lt $deadline ]; do + sleep 5 + running=$("$ADB" shell "pidof $PACKAGE >/dev/null && echo yes || echo no" 2>/dev/null | tr -d '\r' || true) + if [ "$running" = "no" ]; then + app_exited=true + break + fi + done + # The app writes Godot's log to a file (see extra_args), which survives adb dropping out. + : >"$LOG_FILE" + for _ in 1 2 3 4 5; do + "$ADB" shell run-as "$PACKAGE" cat "files/$APP_LOG" >"$LOG_FILE" 2>/dev/null && [ -s "$LOG_FILE" ] && break + sleep 2 + done + cat "$LOG_FILE" + result=$(grep -ao 'GUT_ANDROID_RESULT.*' "$LOG_FILE" | head -1 || true) +} + +# Prints what Android logged about the app stopping (a crash, the system killing it, or its activity +# going away), and Godot's own output, which reaches logcat even when the log file doesn't. +print_stop_diagnostics() { + echo "App exited: $app_exited" + "$ADB" logcat -d -v tag AndroidRuntime:E DEBUG:V libc:F ActivityManager:I ActivityTaskManager:I lmkd:I \ + Godot:V GodotActivity:V godot:V '*:S' 2>/dev/null | tail -300 || true +} + +# Prints every thread's stack, to show where an app that's still running is stuck. debuggerd needs +# root, which emulator images (but not most devices) allow. +print_app_stacks() { + local pid + if ! "$ADB" root >/dev/null 2>&1; then + echo "Can't print the app's stacks without root." + return + fi + "$ADB" wait-for-device + pid=$("$ADB" shell pidof "$PACKAGE" 2>/dev/null | tr -d '\r' || true) + [ -n "$pid" ] && "$ADB" shell debuggerd -b "$pid" 2>&1 || true + "$ADB" unroot >/dev/null 2>&1 || true +} + +echo -e "${YELLOW}Running the tests...${NC}" +# A fresh install, since each export may be signed with a new key. -g grants the microphone +# permission. +$TIMEOUT_CMD 300 "$ADB" wait-for-device +"$ADB" uninstall "$PACKAGE" >/dev/null 2>&1 || true +"$ADB" install -g "$apk" >/dev/null +# Includes the crash buffer, so only this run's crashes fail it. +"$ADB" logcat -b main,system,crash -c +STARTED_APP=true +deadline=$((SECONDS + TIMEOUT_SEC)) +# On CI's emulator, the app occasionally exits before Godot finishes starting, so before any of the +# plugin's tests run. That's retried, while a crash or a failure once the tests start isn't. +for attempt in 1 2 3; do + exited_early=false + run_app + crash_log=$("$ADB" logcat -d -b crash -v tag 2>/dev/null | tr -d '\r' || true) + if [ -n "$result" ] || [ "$app_exited" != true ] || grep -aq 'GUT_ANDROID_STARTED' "$LOG_FILE" \ + || echo "$crash_log" | grep -q "$PACKAGE"; then + break + fi + exited_early=true + print_stop_diagnostics + echo -e "${YELLOW}The app exited before the tests started (attempt ${attempt}).${NC}" +done +if [ -z "$result" ] && [ "$exited_early" != true ]; then + print_stop_diagnostics +fi +if [ "$app_exited" != true ]; then + print_app_stacks +fi + +if [ -z "$result" ]; then + echo -e "${RED}Android tests did not finish (the app stopped early or took over ${TIMEOUT_SEC}s).${NC}" + exit 1 +fi +# The runner reports before quitting, so a hang or crash while shutting down would otherwise go unnoticed. +if [ "$app_exited" != true ]; then + echo -e "${RED}The app didn't exit within ${TIMEOUT_SEC}s after running the tests.${NC}" + exit 1 +fi +crash_log=$("$ADB" logcat -d -b crash -v tag 2>/dev/null | tr -d '\r' || true) +if echo "$crash_log" | grep -q "$PACKAGE"; then + echo "$crash_log" + echo -e "${RED}The app crashed after running the tests.${NC}" + exit 1 +fi +failing=$(echo "$result" | sed -E 's/.*failing=([0-9]+).*/\1/') +ran=$(echo "$result" | sed -E 's/.*tests=([0-9]+).*/\1/') +echo -e "${BLUE}${result}${NC}" +# GUT skips scripts that fail to compile, rather than failing them. +if grep -q 'SCRIPT ERROR: Parse Error' "$LOG_FILE"; then + echo -e "${RED}Some test scripts failed to compile.${NC}" + exit 1 +fi +if [ "$failing" != "0" ] || [ "$ran" == "0" ]; then + echo -e "${RED}Android tests failed.${NC}" + exit 1 +fi +echo -e "${GREEN}Android tests passed!${NC}"