From a838175a2c4f874b59bd88a0ef392256700a92a4 Mon Sep 17 00:00:00 2001 From: avinoamn Date: Mon, 9 Nov 2026 15:33:09 +0200 Subject: [PATCH 01/20] Build on LiveKit's official C++ SDK 1.13.1 The build used a fork of the C++ SDK at 0.3.1. Move Linux, Windows and macOS onto the official releases (1.13.1), which ship every week or two, and adapt to their API: connect(), weak_ptr accessors (localParticipant, remoteParticipants, e2eeManager, keyProvider), roomInfo(), numChannels()/sampleRate(), streamState(). publishTrack() now returns nothing and throws, so the publication is read from the track and an error is reported instead of crashing. On Windows, against the 0.3.1 fork, sending audio stalled the caller and subscriptions were sometimes lost; both went away on 1.13.1. LiveKit publishes no Android build of the C++ SDK, so Android stays on the fork's 0.3.1 for now. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PKkspXDoVPkiLYskHdfRWX --- build.sh | 13 ++++++++----- src/livekit_audio_stream.cpp | 4 ++-- src/livekit_e2ee.cpp | 2 +- src/livekit_participant.cpp | 14 +++++++++++++- src/livekit_room.cpp | 16 ++++++++-------- src/livekit_track.cpp | 2 +- 6 files changed, 33 insertions(+), 18 deletions(-) diff --git a/build.sh b/build.sh index a4f0c3c..421bdf3 100755 --- a/build.sh +++ b/build.sh @@ -15,7 +15,9 @@ 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; Android still uses the fork. +LIVEKIT_ANDROID_VERSION="0.3.1" GODOT_CPP_VERSION="godot-4.5-stable" FRAMETAP_VERSION="0.1.3" LIVEKIT_JS_VERSION="2.22.3" @@ -68,8 +70,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 +91,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,7 +100,7 @@ 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}" } @@ -106,6 +108,7 @@ setup_android() { PLATFORM="android" ARCH="arm64" SCONS_FLAGS="platform=android arch=arm64 screen_capture=no" + LIVEKIT_VERSION="$LIVEKIT_ANDROID_VERSION" 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}" SKIP_FRAMETAP=true diff --git a/src/livekit_audio_stream.cpp b/src/livekit_audio_stream.cpp index 2fa616c..69792c7 100644 --- a/src/livekit_audio_stream.cpp +++ b/src/livekit_audio_stream.cpp @@ -46,10 +46,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. diff --git a/src/livekit_e2ee.cpp b/src/livekit_e2ee.cpp index cc31c36..d0cbd7a 100644 --- a/src/livekit_e2ee.cpp +++ b/src/livekit_e2ee.cpp @@ -299,7 +299,7 @@ Ref LiveKitE2eeManager::get_key_provider() { return Ref(); } - livekit::E2EEManager::KeyProvider *kp = e2ee_manager_->keyProvider(); + livekit::E2EEManager::KeyProvider *kp = e2ee_manager_->keyProvider().lock().get(); if (!kp) { return Ref(); } diff --git a/src/livekit_participant.cpp b/src/livekit_participant.cpp index 9d2df03..3b2e404 100644 --- a/src/livekit_participant.cpp +++ b/src/livekit_participant.cpp @@ -238,7 +238,19 @@ 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); + // The SDK's publishTrack throws on failure and leaves the publication on the track. + try { + local_participant_->publishTrack(native_track, pub_options); + } catch (const std::exception &e) { + UtilityFunctions::push_error("LiveKitLocalParticipant::publish_track: ", String(e.what())); + 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(); diff --git a/src/livekit_room.cpp b/src/livekit_room.cpp index dec4b42..eaa2f73 100644 --- a/src/livekit_room.cpp +++ b/src/livekit_room.cpp @@ -268,7 +268,7 @@ bool LiveKitRoom::connect_to_room(const String &url, const String &token, const 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.c_str(), token_str.c_str(), room_options); } catch (const std::exception &e) { UtilityFunctions::push_error("LiveKitRoom::connect_to_room: connection failed: ", String(e.what())); } catch (...) { @@ -304,7 +304,7 @@ void LiveKitRoom::_finalize_connection(bool success) { connection_state = STATE_CONNECTED; // Initialize local participant - livekit::LocalParticipant *lp = room->localParticipant(); + livekit::LocalParticipant *lp = room->localParticipant().lock().get(); if (lp) { local_participant.instantiate(); local_participant->bind_participant(lp); @@ -316,15 +316,15 @@ void LiveKitRoom::_finalize_connection(bool success) { for (auto const &r : remotes) { Ref p; p.instantiate(); - p->bind_participant(r.get()); - p->bind_remote_participant(static_cast(r.get())); + p->bind_participant(r.lock().get()); + p->bind_remote_participant(static_cast(r.lock().get())); 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(); + livekit::E2EEManager *mgr = room->e2eeManager().lock().get(); if (mgr) { e2ee_manager_.instantiate(); e2ee_manager_->bind_manager(mgr); @@ -440,7 +440,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 +452,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 +462,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(); 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; } From 7c1bec7c107e0e2211c30d5de4d16ba93e6b25d2 Mon Sep 17 00:00:00 2001 From: avinoamn Date: Mon, 9 Nov 2026 15:33:09 +0200 Subject: [PATCH 02/20] Windows: pin LiveKit's DLLs so quitting doesn't crash LiveKit's runtime threads outlive livekit::shutdown(). When Godot unloads the extension at exit, Windows unmaps livekit_ffi.dll and livekit.dll while those threads still run their code, and the process crashes on quit (every process that had subscribed to a track). Pin the DLLs and this module with GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_PIN) at initialization, so they stay mapped until the process ends. Linux keeps such libraries mapped anyway. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PKkspXDoVPkiLYskHdfRWX --- SConstruct | 3 +++ src/register_types.cpp | 7 +++++++ src/windows_pin.cpp | 16 ++++++++++++++++ src/windows_pin.h | 4 ++++ 4 files changed, 30 insertions(+) create mode 100644 src/windows_pin.cpp create mode 100644 src/windows_pin.h diff --git a/SConstruct b/SConstruct index 78f363d..ffd3ae0 100644 --- a/SConstruct +++ b/SConstruct @@ -226,6 +226,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/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/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(); From cc56be5c0bbe985ea5e017f5be8ffb5e2d8e4487 Mon Sep 17 00:00:00 2001 From: Mike Date: Fri, 13 Nov 2026 21:18:58 -0800 Subject: [PATCH 03/20] Hold the official SDK's objects safely client-sdk-cpp 1.x hands out participants, the E2EE manager and its key provider as weak_ptrs, and frees them when a participant leaves or the room disconnects. Keep them weak in the wrappers, rather than as raw pointers, so that objects a game keeps report "not bound" instead of reading freed memory. The SDK also only holds published tracks weakly, and forgets a freed track's publication while leaving it published. So the local participant now keeps its published tracks until they're unpublished. SDK calls throw while a room is disconnecting, which aborted the game. They're now reported with push_error(). FrameCryptor's setters abort the process in 1.13.1 (they don't send the track sid the FFI requires), so they fail with an error until the SDK is fixed. Co-Authored-By: Claude Opus 5.5 --- src/livekit_audio_stream.cpp | 5 +- src/livekit_e2ee.cpp | 74 +++++++++------ src/livekit_e2ee.h | 9 +- src/livekit_participant.cpp | 147 ++++++++++++++++++------------ src/livekit_participant.h | 19 ++-- src/livekit_room.cpp | 42 ++++----- src/livekit_room.h | 2 +- src/livekit_video_stream.cpp | 5 +- src/sdk_call.h | 18 ++++ src/web/README.md | 2 +- test/e2e/test_e2ee.gd | 47 +++++----- test/e2e/test_native_lifetimes.gd | 79 ++++++++++++++++ test/e2e/test_track_lifecycle.gd | 25 +++++ 13 files changed, 325 insertions(+), 149 deletions(-) create mode 100644 src/sdk_call.h create mode 100644 test/e2e/test_native_lifetimes.gd diff --git a/src/livekit_audio_stream.cpp b/src/livekit_audio_stream.cpp index 69792c7..ad1232f 100644 --- a/src/livekit_audio_stream.cpp +++ b/src/livekit_audio_stream.cpp @@ -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) { diff --git a/src/livekit_e2ee.cpp b/src/livekit_e2ee.cpp index d0cbd7a..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().lock().get(); - 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 3b2e404..4cf4908 100644 --- a/src/livekit_participant.cpp +++ b/src/livekit_participant.cpp @@ -1,6 +1,7 @@ #include "livekit_participant.h" #include "livekit_track.h" #include "livekit_track_publication.h" +#include "sdk_call.h" #include @@ -40,42 +41,42 @@ LiveKitParticipant::LiveKitParticipant() { LiveKitParticipant::~LiveKitParticipant() { } -void LiveKitParticipant::bind_participant(livekit::Participant *p_participant) { +void LiveKitParticipant::bind_participant(const std::weak_ptr &p_participant) { participant_ = p_participant; } 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()); + if (auto participant = participant_.lock()) { + return String(participant->identity().c_str()); } return String(); } 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 +85,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; } @@ -135,19 +136,24 @@ LiveKitLocalParticipant::~LiveKitLocalParticipant() { // Unregister all RPC methods so the SDK doesn't invoke callbacks // into a freed object. - if (local_participant_) { + if (auto lp = get_native_local_participant()) { for (const auto &method : registered_rpc_methods_) { - local_participant_->unregisterRpcMethod(method); + try { + lp->unregisterRpcMethod(method); + } catch (const std::exception &) { + // The room is going away, so it won't invoke the method anyway. + } } } } -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 +165,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 +200,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 +221,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,13 +249,10 @@ Ref LiveKitLocalParticipant::publish_track(const R } // video_codec is a VideoCodec enum in the SDK - not easily set from string - // The SDK's publishTrack throws on failure and leaves the publication on the track. - try { - local_participant_->publishTrack(native_track, pub_options); - } catch (const std::exception &e) { - UtilityFunctions::push_error("LiveKitLocalParticipant::publish_track: ", String(e.what())); + 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(); @@ -256,6 +264,10 @@ Ref LiveKitLocalParticipant::publish_track(const R 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); @@ -263,17 +275,26 @@ 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()); } // 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; } @@ -292,47 +313,56 @@ 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; - }); + bool registered = call_sdk("LiveKitLocalParticipant::register_rpc_method", [&] { + lp->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; + }); + }); + if (registered) { + registered_rpc_methods_.push_back(method_name); + } } 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()); @@ -361,17 +391,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..694c860 100644 --- a/src/livekit_participant.h +++ b/src/livekit_participant.h @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -35,13 +36,14 @@ 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_; public: LiveKitParticipant(); ~LiveKitParticipant(); - virtual void bind_participant(livekit::Participant *p_participant); + void bind_participant(const std::weak_ptr &p_participant); String get_sid() const; String get_name() const; @@ -55,7 +57,9 @@ 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_; @@ -66,8 +70,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); @@ -89,9 +92,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 +99,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 eaa2f73..bf88115 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); @@ -239,7 +239,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 +261,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,11 +304,9 @@ void LiveKitRoom::_finalize_connection(bool success) { connection_state = STATE_CONNECTED; // Initialize local participant - livekit::LocalParticipant *lp = room->localParticipant().lock().get(); - if (lp) { + if (auto lp = room->localParticipant().lock()) { local_participant.instantiate(); local_participant->bind_participant(lp); - local_participant->bind_local_participant(lp); } // Initialize remote participants @@ -316,16 +314,15 @@ void LiveKitRoom::_finalize_connection(bool success) { for (auto const &r : remotes) { Ref p; p.instantiate(); - p->bind_participant(r.lock().get()); - p->bind_remote_participant(static_cast(r.lock().get())); + p->bind_participant(r); 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().lock().get(); - if (mgr) { + auto mgr = room->e2eeManager(); + if (!mgr.expired()) { e2ee_manager_.instantiate(); e2ee_manager_->bind_manager(mgr); } @@ -339,7 +336,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 +351,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; @@ -410,7 +407,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. @@ -519,16 +516,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); { 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_video_stream.cpp b/src/livekit_video_stream.cpp index 4f565e8..2bb5eaf 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) { 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 a9032aa..67895e6 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/test/e2e/test_e2ee.gd b/test/e2e/test_e2ee.gd index 496cef2..f90da07 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_no_server(): 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_no_server(): 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_receiver_records_a_participant_key_it_ratcheted(): - if _skip_if_unsupported(): + if _skip_if_no_server(): return var no_shared_key := {"e2ee": LiveKitE2eeOptions.new()} assert_true(await _connect_both_rooms(no_shared_key, {"e2ee": LiveKitE2eeOptions.new()}), "Both rooms should connect") @@ -205,7 +205,7 @@ 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 manager := _room.get_e2ee_manager() @@ -226,7 +226,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() @@ -235,6 +235,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) @@ -243,7 +245,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() @@ -270,7 +272,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()) @@ -293,6 +295,17 @@ func _assert_recovers_from_wrong_key(shared: bool) -> void: _close(video) +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() @@ -303,16 +316,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..552bb1c --- /dev/null +++ b/test/e2e/test_native_lifetimes.gd @@ -0,0 +1,79 @@ +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(): + 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()], ["", {}, {}]) + + +func test_local_participant_kept_after_disconnecting_is_unbound(): + 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() + # Used while the SDK is still tearing the room down, it may fail, but mustn't crash. + local.publish_data("stale".to_utf8_buffer(), true) + await get_tree().create_timer(2.0).timeout + _take_errors() + local.publish_data("stale".to_utf8_buffer(), true) + + assert_eq( + [local.get_identity(), local.get_attributes(), local.get_track_publications(), _take_errors()], + ["", {}, {}, ["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..ef11a67 100644 --- a/test/e2e/test_track_lifecycle.gd +++ b/test/e2e/test_track_lifecycle.gd @@ -114,6 +114,31 @@ 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) + + await _poll_until(_room, func(): return not published_sids.is_empty(), 10.0) + # 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 From 5f499cc87f06d347099e91a404c9deacb6e250cd Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 14 Nov 2026 16:55:37 -0800 Subject: [PATCH 04/20] Keep a participant's identity after the SDK frees it Handlers for a participant leaving, e.g. participant_disconnected, often run after the SDK has freed it, and still need to know who left. The identity never changes, so the wrapper keeps the one it was bound with, while everything else still reports "not bound". Co-Authored-By: Claude Opus 5.5 --- src/livekit_participant.cpp | 8 +++----- src/livekit_participant.h | 5 ++++- src/livekit_room.cpp | 10 +++++++--- test/e2e/test_native_lifetimes.gd | 8 ++++---- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/livekit_participant.cpp b/src/livekit_participant.cpp index 4cf4908..823eab0 100644 --- a/src/livekit_participant.cpp +++ b/src/livekit_participant.cpp @@ -41,8 +41,9 @@ LiveKitParticipant::LiveKitParticipant() { LiveKitParticipant::~LiveKitParticipant() { } -void LiveKitParticipant::bind_participant(const std::weak_ptr &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 { @@ -60,10 +61,7 @@ String LiveKitParticipant::get_name() const { } String LiveKitParticipant::get_identity() const { - if (auto participant = participant_.lock()) { - return String(participant->identity().c_str()); - } - return String(); + return String(identity_.c_str()); } String LiveKitParticipant::get_metadata() const { diff --git a/src/livekit_participant.h b/src/livekit_participant.h index 694c860..db06d09 100644 --- a/src/livekit_participant.h +++ b/src/livekit_participant.h @@ -38,12 +38,15 @@ class LiveKitParticipant : public RefCounted { static void _bind_methods(); // 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(); - void bind_participant(const std::weak_ptr &p_participant); + void bind_participant(const std::weak_ptr &p_participant, const std::string &p_identity); String get_sid() const; String get_name() const; diff --git a/src/livekit_room.cpp b/src/livekit_room.cpp index bf88115..930efc6 100644 --- a/src/livekit_room.cpp +++ b/src/livekit_room.cpp @@ -306,15 +306,19 @@ void LiveKitRoom::_finalize_connection(bool success) { // Initialize local participant if (auto lp = room->localParticipant().lock()) { local_participant.instantiate(); - local_participant->bind_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); + p->bind_participant(r, remote->identity()); std::lock_guard plock(participants_mutex_); remote_participants[p->get_identity()] = p; } @@ -523,7 +527,7 @@ void LiveKitRoom::GodotRoomDelegate::onParticipantConnected(livekit::Room &r, co room->pending_events_.push_back([this, identity, rp]() { Ref p; p.instantiate(); - p->bind_participant(rp); + p->bind_participant(rp, identity); { std::lock_guard plock(room->participants_mutex_); room->remote_participants[String(identity.c_str())] = p; diff --git a/test/e2e/test_native_lifetimes.gd b/test/e2e/test_native_lifetimes.gd index 552bb1c..75e67c7 100644 --- a/test/e2e/test_native_lifetimes.gd +++ b/test/e2e/test_native_lifetimes.gd @@ -3,7 +3,7 @@ extends "res://test/e2e/e2e_helper.gd" ## the web build, where participants outlive their connection.) -func test_remote_participant_kept_after_it_leaves_is_unbound(): +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") @@ -15,10 +15,10 @@ func test_remote_participant_kept_after_it_leaves_is_unbound(): 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()], ["", {}, {}]) + assert_eq([p.get_identity(), p.get_attributes(), p.get_track_publications()], ["e2e-user-2", {}, {}]) -func test_local_participant_kept_after_disconnecting_is_unbound(): +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") @@ -33,7 +33,7 @@ func test_local_participant_kept_after_disconnecting_is_unbound(): assert_eq( [local.get_identity(), local.get_attributes(), local.get_track_publications(), _take_errors()], - ["", {}, {}, ["LiveKitLocalParticipant::publish_data: not bound"]], + ["e2e-user-1", {}, {}, ["LiveKitLocalParticipant::publish_data: not bound"]], ) From cbf207f4f73059f93a77eec1c77610691a3a772d Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 14 Nov 2026 15:37:36 -0800 Subject: [PATCH 05/20] Android: build the official C++ SDK instead of using the fork LiveKit publishes no Android build of client-sdk-cpp, and the fork's 0.3.1 build doesn't match the official SDK's API. So on a cache miss, build.sh now builds v1.13.1 for Android arm64 from LiveKit's release, with patches/client-sdk-cpp-android.patch for cross-compiling with the NDK, and caches it with a key that includes the patch. That needs NDK r27 (r26d's libc++ can't build webrtc-sys), plus git, CMake, Ninja and Rust, so CI moves to r27d, installs Ninja, and caches livekit-sdk. On Android the FFI uses the bundled webpki roots, as LiveKit's own Android FFI builds do, and the SDK's HTTP token sources aren't available (they need libcurl). Co-Authored-By: Claude Opus 5.5 --- .github/workflows/build_and_test.yml | 14 +- .github/workflows/build_release.yml | 9 +- .gitignore | 1 + build.sh | 88 +++++++-- patches/client-sdk-cpp-android.patch | 273 +++++++++++++++++++++++++++ 5 files changed, 366 insertions(+), 19 deletions(-) create mode 100644 patches/client-sdk-cpp-android.patch diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index beaebe9..38e3ba7 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 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..2490db3 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 diff --git a/build.sh b/build.sh index 421bdf3..6c24877 100755 --- a/build.sh +++ b/build.sh @@ -16,14 +16,20 @@ PLATFORM="" ARCH="" SCONS_FLAGS="" LIVEKIT_VERSION="1.13.1" -# LiveKit publishes no Android build of the C++ SDK; Android still uses the fork. -LIVEKIT_ANDROID_VERSION="0.3.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_SHA256="f853e691868d0557425ea290bf7ba6384eef2fa9b04c323afab49a770ba9da80" +# 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.22.3" LIVEKIT_JS_SHA256="0f920230001fcbfdccb0bcd2efffd64da8594808908953ed466d4c39c1529fa3" 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() { @@ -108,9 +114,7 @@ setup_android() { PLATFORM="android" ARCH="arm64" SCONS_FLAGS="platform=android arch=arm64 screen_capture=no" - LIVEKIT_VERSION="$LIVEKIT_ANDROID_VERSION" - 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}" + BUILD_LIVEKIT_SDK=true SKIP_FRAMETAP=true echo -e "${BLUE}=== Godot-LiveKit Local Build Script (Android arm64) ===${NC}" } @@ -221,12 +225,47 @@ 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" + + 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_zip="${work_dir}/protoc.zip" + curl -sL "https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-x86_64.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 aarch64-linux-android) + + cmake -S "$src_dir" -B "${work_dir}/build" -G Ninja \ + -DCMAKE_TOOLCHAIN_FILE="${ndk_root}/build/cmake/android.toolchain.cmake" \ + -DANDROID_ABI=arm64-v8a \ + -DANDROID_PLATFORM=android-24 \ + -DCMAKE_BUILD_TYPE=Release \ + -DLIVEKIT_VERSION="$LIVEKIT_VERSION" \ + -DProtobuf_PROTOC_EXECUTABLE="${work_dir}/protoc/bin/protoc" + cmake --build "${work_dir}/build" -j "$(nproc)" + cmake --install "${work_dir}/build" --prefix "$(pwd)/livekit-sdk" + + 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 +349,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 +438,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 @@ -456,6 +500,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}-$(sha256sum "$LIVEKIT_ANDROID_PATCH" | cut -c1-12)" + else + echo "$LIVEKIT_VERSION" + fi +} + # --- Main Execution --- main() { # Parse command-line arguments @@ -519,10 +575,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..b6989f6 --- /dev/null +++ b/patches/client-sdk-cpp-android.patch @@ -0,0 +1,273 @@ +diff --git a/CMakeLists.txt b/CMakeLists.txt +index 00d4bd3..bf829a9 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,44 @@ 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() + endif() + + # Cargo artifacts cannot be shared between capture-enabled and disabled SDKs. +@@ -293,7 +332,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 +437,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 +577,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 +728,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) { From ba0e9bd35c94b618a1729e9ccc1b1db3d6791b36 Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 14 Nov 2026 18:03:47 -0800 Subject: [PATCH 06/20] Android: build the SDK with the shared C++ runtime The SDK reports errors by throwing, but with the NDK's default static C++ runtime, liblivekit and the extension each had their own copy, so the extension's catch blocks never matched and the app aborted instead (found running the test suites on an emulator). Building the SDK with c++_shared makes both use the libc++_shared.so the app already ships. The patch also picks up the upstream proposal's warning for static runtimes. Co-Authored-By: Claude Opus 5.5 --- build.sh | 3 +++ patches/client-sdk-cpp-android.patch | 23 +++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/build.sh b/build.sh index 6c24877..a912ac1 100755 --- a/build.sh +++ b/build.sh @@ -251,10 +251,13 @@ build_livekit_android() { # 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 aarch64-linux-android) + # 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=arm64-v8a \ -DANDROID_PLATFORM=android-24 \ + -DANDROID_STL=c++_shared \ -DCMAKE_BUILD_TYPE=Release \ -DLIVEKIT_VERSION="$LIVEKIT_VERSION" \ -DProtobuf_PROTOC_EXECUTABLE="${work_dir}/protoc/bin/protoc" diff --git a/patches/client-sdk-cpp-android.patch b/patches/client-sdk-cpp-android.patch index b6989f6..bad1802 100644 --- a/patches/client-sdk-cpp-android.patch +++ b/patches/client-sdk-cpp-android.patch @@ -1,5 +1,5 @@ diff --git a/CMakeLists.txt b/CMakeLists.txt -index 00d4bd3..bf829a9 100644 +index 00d4bd3..230df50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -211,6 +211,15 @@ if(_lk_capture_features) @@ -18,7 +18,7 @@ index 00d4bd3..bf829a9 100644 message(STATUS "LIVEKIT_CAPTURE='${LIVEKIT_CAPTURE}' -> cargo features: '${LIVEKIT_CARGO_FEATURES}'") set(GIT_COMMIT "unknown") -@@ -261,14 +270,44 @@ configure_file( +@@ -261,14 +270,55 @@ configure_file( find_program(CARGO_EXECUTABLE NAMES cargo REQUIRED) @@ -60,11 +60,22 @@ index 00d4bd3..bf829a9 100644 + "${_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 +332,8 @@ endif() +@@ -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 "") @@ -74,7 +85,7 @@ index 00d4bd3..bf829a9 100644 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 +437,12 @@ add_custom_command( +@@ -397,8 +448,12 @@ add_custom_command( -DCARGO=${CARGO_EXECUTABLE} -DPROTOC_PATH=${Protobuf_PROTOC_EXECUTABLE} -DRUST_TARGET=${RUST_TARGET_TRIPLE} @@ -87,7 +98,7 @@ index 00d4bd3..bf829a9 100644 -DCARGO_TARGET_DIR=${LIVEKIT_CARGO_TARGET_DIR} -P "${RUN_CARGO_SCRIPT}" WORKING_DIRECTORY "${RUST_ROOT}" -@@ -533,7 +577,7 @@ target_link_libraries(livekit +@@ -533,7 +588,7 @@ target_link_libraries(livekit if(WIN32) target_link_libraries(livekit PRIVATE winhttp) @@ -96,7 +107,7 @@ index 00d4bd3..bf829a9 100644 find_package(CURL REQUIRED) target_link_libraries(livekit PRIVATE CURL::libcurl) endif() -@@ -684,7 +728,7 @@ if(WIN32) +@@ -684,7 +739,7 @@ if(WIN32) ) endif() From a043d5ebcb1a0e2670ac7ca441f87f72e2b542b4 Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 14 Nov 2026 21:39:55 -0800 Subject: [PATCH 07/20] Warn about stream lock contention only when it keeps happening The reader threads hold the lock only briefly and keep what they read for the next poll, so a single skipped poll is harmless. Warning on the first one made tests that check for unexpected warnings fail at random (three E2EE tests on an Android emulator). The quickstart already describes the warning as appearing when contention happens repeatedly. Co-Authored-By: Claude Opus 5.5 --- src/livekit_audio_stream.cpp | 3 ++- src/livekit_video_stream.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/livekit_audio_stream.cpp b/src/livekit_audio_stream.cpp index ad1232f..d4cbbe7 100644 --- a/src/livekit_audio_stream.cpp +++ b/src/livekit_audio_stream.cpp @@ -152,7 +152,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_video_stream.cpp b/src/livekit_video_stream.cpp index 2bb5eaf..cb1c21b 100644 --- a/src/livekit_video_stream.cpp +++ b/src/livekit_video_stream.cpp @@ -132,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; From a5c3f752d2210926026936f711ad111569dce4dc Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 14 Nov 2026 21:39:55 -0800 Subject: [PATCH 08/20] Let the unit tests compile in builds without screen capture Android builds leave out LiveKitScreenCapture, so scripts that use it don't compile there, and GUT skips them without failing. That hid all of test_threading_stress.gd on Android, so its screen capture tests move to their own file. The registration check no longer expects the class on Android. Co-Authored-By: Claude Opus 5.5 --- test/unit/test_platform.gd | 3 + test/unit/test_screen_capture_threading.gd | 74 ++++++++++++++++++++ test/unit/test_threading_stress.gd | 80 ++-------------------- 3 files changed, 83 insertions(+), 74 deletions(-) create mode 100644 test/unit/test_screen_capture_threading.gd 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(): From ecf18f45c6b605a66ac260ee000c0748960d8abc Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 14 Nov 2026 21:39:55 -0800 Subject: [PATCH 09/20] Run the test suites on an Android emulator in CI The new job builds for Android x86_64 (which GitHub's runners can emulate at a usable speed), exports a test app with Godot's Gradle build, and runs the unit, integration and e2e suites on an Android 14 emulator against a local livekit-server. test_android.sh does the export and run, and works with any device or emulator on adb. - build.sh and SConstruct take `android x86_64`, and the Android build keeps the SDK build's libwebrtc.jar, which apps need. The godot-cpp cache check now notices a build for another architecture. - The project enables ETC2/ASTC import, which Android exports require. - Join tokens for the integration tests come from test/dev_token.py, shared with the web job. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/build_and_test.yml | 139 ++++++++- .gitignore | 3 + README.md | 2 + SConstruct | 6 +- .../godot-livekit/godot-livekit.gdextension | 2 + build.sh | 39 ++- project.godot | 4 + test/android/android_runner.gd | 37 +++ test/android/android_runner.tscn | 6 + test/dev_token.py | 37 +++ test_android.sh | 268 ++++++++++++++++++ 11 files changed, 517 insertions(+), 26 deletions(-) create mode 100644 test/android/android_runner.gd create mode 100644 test/android/android_runner.tscn create mode 100644 test/dev_token.py create mode 100755 test_android.sh diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 38e3ba7..8a44c38 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -190,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/.gitignore b/.gitignore index 2490db3..63235ec 100644 --- a/.gitignore +++ b/.gitignore @@ -39,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 ffd3ae0..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']) 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 a912ac1..d9a5f08 100755 --- a/build.sh +++ b/build.sh @@ -65,7 +65,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 @@ -112,11 +112,11 @@ setup_windows() { setup_android() { PLATFORM="android" - ARCH="arm64" - SCONS_FLAGS="platform=android arch=arm64 screen_capture=no" + 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() { @@ -140,6 +140,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}" @@ -235,6 +241,12 @@ build_livekit_android() { 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" @@ -249,13 +261,13 @@ build_livekit_android() { 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 aarch64-linux-android) + (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=arm64-v8a \ + -DANDROID_ABI="$android_abi" \ -DANDROID_PLATFORM=android-24 \ -DANDROID_STL=c++_shared \ -DCMAKE_BUILD_TYPE=Release \ @@ -263,6 +275,15 @@ build_livekit_android() { -DProtobuf_PROTOC_EXECUTABLE="${work_dir}/protoc/bin/protoc" cmake --build "${work_dir}/build" -j "$(nproc)" 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 + webrtc_jar=$(find "${src_dir}/client-sdk-rust/target" -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" @@ -476,7 +497,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 @@ -509,7 +530,7 @@ livekit_version_key() { echo "${LIVEKIT_VERSION}-${ARCH}" elif [ "$BUILD_LIVEKIT_SDK" = true ]; then # A changed patch needs a rebuild. - echo "${LIVEKIT_VERSION}-$(sha256sum "$LIVEKIT_ANDROID_PATCH" | cut -c1-12)" + echo "${LIVEKIT_VERSION}-${ARCH}-$(sha256sum "$LIVEKIT_ANDROID_PATCH" | cut -c1-12)" else echo "$LIVEKIT_VERSION" fi @@ -556,7 +577,7 @@ main() { setup_windows ;; android) - setup_android + setup_android "$arch_arg" ;; web) setup_web 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/test/android/android_runner.gd b/test/android/android_runner.gd new file mode 100644 index 0000000..512c30a --- /dev/null +++ b/test/android/android_runner.gd @@ -0,0 +1,37 @@ +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_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: + 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_android.sh b/test_android.sh new file mode 100755 index 0000000..22c96fe --- /dev/null +++ b/test_android.sh @@ -0,0 +1,268 @@ +#!/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}" + +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 + 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/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 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 + +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 300 "$ADB" wait-for-device +"$ADB" uninstall "$PACKAGE" >/dev/null 2>&1 || true +"$ADB" install -g "$apk" >/dev/null +"$ADB" logcat -c +STARTED_APP=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. +deadline=$((SECONDS + TIMEOUT_SEC)) +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) + [ "$running" = "no" ] && break +done + +# The app writes Godot's log to a file (see extra_args), which survives adb dropping out. +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) +if [ -z "$result" ]; then + # Anything Android logged about a crash. + "$ADB" logcat -d -v tag AndroidRuntime:E DEBUG:V libc:F '*:S' 2>/dev/null || true +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 +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}" From b69d6d052dc8a06ce7f43e318e1b1474f51c890f Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 15 Nov 2026 00:13:33 -0800 Subject: [PATCH 10/20] Pend E2EE tests that need encrypted video to start, on Android On Android, encrypted video sometimes never starts: the subscriber gets every packet but decodes none and never asks for a keyframe (#19). The three tests that need it to start as soon as the rooms connect are pending there until the cause is found and fixed upstream. Co-Authored-By: Claude Opus 5.5 --- test/e2e/test_e2ee.gd | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/test/e2e/test_e2ee.gd b/test/e2e/test_e2ee.gd index f90da07..db90ae8 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_no_server(): + 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 := {} @@ -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_no_server(): + 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_receiver_records_a_participant_key_it_ratcheted(): - if _skip_if_no_server(): + 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") @@ -295,6 +295,17 @@ 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 From be2e1cf960e82c2c6919406504f8b83fc765b01d Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 15 Nov 2026 00:49:22 -0800 Subject: [PATCH 11/20] Allocate an audio stream's ring buffer only once it starts reading Every LiveKitAudioStream zero-filled a 1.9 MB ring buffer in its constructor, though only streams made from a track or participant use it. On an Android emulator, that made test_audio_stream_immediate_gc's 50 bare streams exceed its 500 ms budget. Co-Authored-By: Claude Opus 5.5 --- src/livekit_audio_stream.cpp | 8 +++++--- src/livekit_audio_stream.h | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/livekit_audio_stream.cpp b/src/livekit_audio_stream.cpp index d4cbbe7..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); @@ -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_; @@ -125,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]() { 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 From 5f357213d47c81f809987dcb7a4783ee14918fa2 Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 15 Nov 2026 01:12:43 -0800 Subject: [PATCH 12/20] Android: Java-load the extension library for the architecture it's built for livekit_android_jni_init always loaded godot-livekit.android.arm64, so on x86_64 the call failed (logging an error) and the AV1 stubs weren't registered. Co-Authored-By: Claude Opus 5.5 --- src/android_jni_init.cpp | 4 ++++ 1 file changed, 4 insertions(+) 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; } From 46d3b596597ce24849d8a8e1791dbde6219e8ff4 Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 15 Nov 2026 10:42:17 -0800 Subject: [PATCH 13/20] Address review: Android build hosts, template safety, stricter test checks - build.sh downloads the protoc for the build machine (Linux x86_64 or arm64, or macOS), avoids nproc and sha256sum, and finds libwebrtc.jar in CARGO_TARGET_DIR when it's set. - test_android.sh only replaces an Android build template it installed, and fails if the app doesn't exit after the tests. - The unkept-track test asserts that the track gets published. Co-Authored-By: Claude Opus 5.5 --- build.sh | 26 ++++++++++++++++++++------ test/e2e/test_track_lifecycle.gd | 3 ++- test_android.sh | 17 ++++++++++++++++- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/build.sh b/build.sh index d9a5f08..47435e1 100755 --- a/build.sh +++ b/build.sh @@ -20,7 +20,10 @@ LIVEKIT_VERSION="1.13.1" 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_SHA256="f853e691868d0557425ea290bf7ba6384eef2fa9b04c323afab49a770ba9da80" +# 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" @@ -254,10 +257,20 @@ build_livekit_android() { 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}-linux-x86_64.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" + 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. @@ -273,11 +286,12 @@ build_livekit_android() { -DCMAKE_BUILD_TYPE=Release \ -DLIVEKIT_VERSION="$LIVEKIT_VERSION" \ -DProtobuf_PROTOC_EXECUTABLE="${work_dir}/protoc/bin/protoc" - cmake --build "${work_dir}/build" -j "$(nproc)" + 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 - webrtc_jar=$(find "${src_dir}/client-sdk-rust/target" -path "*/out/livekit_webrtc/*" -name libwebrtc.jar | head -1) + # 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 @@ -530,7 +544,7 @@ livekit_version_key() { echo "${LIVEKIT_VERSION}-${ARCH}" elif [ "$BUILD_LIVEKIT_SDK" = true ]; then # A changed patch needs a rebuild. - echo "${LIVEKIT_VERSION}-${ARCH}-$(sha256sum "$LIVEKIT_ANDROID_PATCH" | cut -c1-12)" + echo "${LIVEKIT_VERSION}-${ARCH}-$(git hash-object "$LIVEKIT_ANDROID_PATCH" | cut -c1-12)" else echo "$LIVEKIT_VERSION" fi diff --git a/test/e2e/test_track_lifecycle.gd b/test/e2e/test_track_lifecycle.gd index ef11a67..eaa3130 100644 --- a/test/e2e/test_track_lifecycle.gd +++ b/test/e2e/test_track_lifecycle.gd @@ -125,7 +125,8 @@ func test_track_published_without_keeping_it_stays_published(): _publish_unkept_audio_track(lp) - await _poll_until(_room, func(): return not published_sids.is_empty(), 10.0) + 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) diff --git a/test_android.sh b/test_android.sh index 22c96fe..0a35183 100755 --- a/test_android.sh +++ b/test_android.sh @@ -131,11 +131,17 @@ export_apk() { # 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 @@ -231,10 +237,14 @@ STARTED_APP=true # 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. deadline=$((SECONDS + TIMEOUT_SEC)) +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) - [ "$running" = "no" ] && break + 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. @@ -253,6 +263,11 @@ 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 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 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}" From 84196ae8b9bdf937f4681887d101040f7af85b38 Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 15 Nov 2026 10:45:59 -0800 Subject: [PATCH 14/20] Log why the Android app stopped when the tests don't finish A CI run's app stopped about 15 seconds after launch, before the tests started, and logcat's crash tags showed nothing. Also print whether the app exited, what ActivityManager and lmkd logged about it, and Godot's own logcat output. Co-Authored-By: Claude Opus 5.5 --- test_android.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test_android.sh b/test_android.sh index 0a35183..16ab5c6 100755 --- a/test_android.sh +++ b/test_android.sh @@ -255,8 +255,11 @@ done cat "$LOG_FILE" result=$(grep -ao 'GUT_ANDROID_RESULT.*' "$LOG_FILE" | head -1 || true) if [ -z "$result" ]; then - # Anything Android logged about a crash. - "$ADB" logcat -d -v tag AndroidRuntime:E DEBUG:V libc:F '*:S' 2>/dev/null || true + # What Android logged about the app stopping (a crash, or the system killing it), and Godot's own + # output, which reaches logcat even when the log file doesn't. + echo "App exited: $app_exited" + "$ADB" logcat -d -v tag AndroidRuntime:E DEBUG:V libc:F ActivityManager:I lmkd:I godot:V '*:S' 2>/dev/null \ + | tail -300 || true fi if [ -z "$result" ]; then From 3dac22dad4ae50fba2db31ef3f6976a4f1739cfe Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 15 Nov 2026 10:51:26 -0800 Subject: [PATCH 15/20] Fail Android test runs when the app crashes while shutting down The runner reports its result before quitting, so check the crash buffer for the app too. Co-Authored-By: Claude Opus 5.5 --- test_android.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test_android.sh b/test_android.sh index 16ab5c6..b68e3e8 100755 --- a/test_android.sh +++ b/test_android.sh @@ -230,7 +230,8 @@ echo -e "${YELLOW}Running the tests...${NC}" timeout 300 "$ADB" wait-for-device "$ADB" uninstall "$PACKAGE" >/dev/null 2>&1 || true "$ADB" install -g "$apk" >/dev/null -"$ADB" logcat -c +# Includes the crash buffer, so only this run's crashes fail it. +"$ADB" logcat -b main,system,crash -c STARTED_APP=true "$ADB" shell am start -W -n "$PACKAGE/com.godot.game.GodotApp" >/dev/null @@ -266,11 +267,17 @@ 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 while shutting down would otherwise go unnoticed. +# 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}" From f7c5fca7566b0bde32e271a2fabc2ed9156afbb3 Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 15 Nov 2026 11:06:49 -0800 Subject: [PATCH 16/20] Address review: release published tracks when the connection ends Apps may keep the local participant after disconnecting, which kept the tracks it published alive too. Also fall back to gtimeout in test_android.sh, as the other test scripts do. Co-Authored-By: Claude Opus 5.5 --- src/livekit_participant.cpp | 4 ++++ src/livekit_participant.h | 3 +++ src/livekit_room.cpp | 6 ++++++ test_android.sh | 11 ++++++++--- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/livekit_participant.cpp b/src/livekit_participant.cpp index 823eab0..c8f2327 100644 --- a/src/livekit_participant.cpp +++ b/src/livekit_participant.cpp @@ -288,6 +288,10 @@ void LiveKitLocalParticipant::unpublish_track(const String &track_sid) { published_tracks_.end()); } +void LiveKitLocalParticipant::release_published_tracks() { + published_tracks_.clear(); +} + // RPC void LiveKitLocalParticipant::perform_rpc(const String &destination, const String &method, const String &payload, double timeout) { diff --git a/src/livekit_participant.h b/src/livekit_participant.h index db06d09..2cec419 100644 --- a/src/livekit_participant.h +++ b/src/livekit_participant.h @@ -83,6 +83,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); + /// Lets go of the tracks this participant published, for the room to call when its connection + /// ends, since apps may keep the participant afterwards. + void release_published_tracks(); // 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); diff --git a/src/livekit_room.cpp b/src/livekit_room.cpp index 930efc6..0ecbfbe 100644 --- a/src/livekit_room.cpp +++ b/src/livekit_room.cpp @@ -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->release_published_tracks(); + } local_participant.unref(); { std::lock_guard plock(participants_mutex_); @@ -387,6 +390,9 @@ void LiveKitRoom::disconnect_from_room() { disconnect_thread_.detach(); } + if (local_participant.is_valid()) { + local_participant->release_published_tracks(); + } local_participant.unref(); { std::lock_guard plock(participants_mutex_); diff --git a/test_android.sh b/test_android.sh index b68e3e8..c847443 100755 --- a/test_android.sh +++ b/test_android.sh @@ -34,6 +34,11 @@ 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" @@ -201,10 +206,10 @@ EOF echo -e "${YELLOW}Exporting Android test build (${arch})...${NC}" # Importing fails harmlessly on errors the export reports too (e.g. a missing native library). - timeout 300 $GODOT_CMD --headless --import >/dev/null 2>&1 || true + $TIMEOUT_CMD 300 $GODOT_CMD --headless --import >/dev/null 2>&1 || true rm -f "$apk_path" mkdir -p "$(dirname "$apk_path")" - if ! timeout 1200 $GODOT_CMD --headless --export-debug "Android Test" "$apk_path" >"$WORK_DIR/export.log" 2>&1 \ + 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}" @@ -227,7 +232,7 @@ fi 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 300 "$ADB" wait-for-device +$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. From 9850d018ae591635e2a1d0138282f752a9fee218 Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 15 Nov 2026 13:45:01 -0800 Subject: [PATCH 17/20] Unbind the local participant when its connection ends The SDK's local participant methods wait for a reply without a timeout, and a room being torn down may not send one. An Android CI run hung in the test that publishes data from a kept participant right after disconnecting, so calls made after the connection ends now fail with "not bound" instead of reaching the SDK. Co-Authored-By: Claude Opus 5.5 --- src/livekit_participant.cpp | 5 ++++- src/livekit_participant.h | 6 +++--- src/livekit_room.cpp | 4 ++-- test/e2e/test_native_lifetimes.gd | 5 +---- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/livekit_participant.cpp b/src/livekit_participant.cpp index c8f2327..3e93454 100644 --- a/src/livekit_participant.cpp +++ b/src/livekit_participant.cpp @@ -288,7 +288,10 @@ void LiveKitLocalParticipant::unpublish_track(const String &track_sid) { published_tracks_.end()); } -void LiveKitLocalParticipant::release_published_tracks() { +void LiveKitLocalParticipant::unbind() { + // 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(); } diff --git a/src/livekit_participant.h b/src/livekit_participant.h index 2cec419..7e05ffe 100644 --- a/src/livekit_participant.h +++ b/src/livekit_participant.h @@ -83,9 +83,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); - /// Lets go of the tracks this participant published, for the room to call when its connection - /// ends, since apps may keep the participant afterwards. - void release_published_tracks(); + /// 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); diff --git a/src/livekit_room.cpp b/src/livekit_room.cpp index 0ecbfbe..b01f777 100644 --- a/src/livekit_room.cpp +++ b/src/livekit_room.cpp @@ -214,7 +214,7 @@ 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->release_published_tracks(); + local_participant->unbind(); } local_participant.unref(); { @@ -391,7 +391,7 @@ void LiveKitRoom::disconnect_from_room() { } if (local_participant.is_valid()) { - local_participant->release_published_tracks(); + local_participant->unbind(); } local_participant.unref(); { diff --git a/test/e2e/test_native_lifetimes.gd b/test/e2e/test_native_lifetimes.gd index 75e67c7..3d1f0a1 100644 --- a/test/e2e/test_native_lifetimes.gd +++ b/test/e2e/test_native_lifetimes.gd @@ -25,10 +25,7 @@ func test_local_participant_kept_after_disconnecting_is_unbound_but_keeps_its_id var local := _room.get_local_participant() _room.disconnect_from_room() - # Used while the SDK is still tearing the room down, it may fail, but mustn't crash. - local.publish_data("stale".to_utf8_buffer(), true) - await get_tree().create_timer(2.0).timeout - _take_errors() + # Right away, while the SDK is still tearing the room down. local.publish_data("stale".to_utf8_buffer(), true) assert_eq( From 3a4ff39140efb2c85e7348812d89e1bac57f6886 Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 15 Nov 2026 13:45:01 -0800 Subject: [PATCH 18/20] Android tests: relaunch an app that exits before the tests start On CI's emulator, the app has twice exited before Godot finished starting, so before any of the plugin's tests ran. Relaunch it up to twice when that happens, but not after a crash or once the tests have started. When a run times out with the app still running, print every thread's stack, which emulator images allow through root. Co-Authored-By: Claude Opus 5.5 --- test/android/android_runner.gd | 5 +- test_android.sh | 87 ++++++++++++++++++++++++---------- 2 files changed, 66 insertions(+), 26 deletions(-) diff --git a/test/android/android_runner.gd b/test/android/android_runner.gd index 512c30a..e56d13b 100644 --- a/test/android/android_runner.gd +++ b/test/android/android_runner.gd @@ -2,14 +2,15 @@ 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_RESULT tests=N failing=N pending=N` when done, for test_android.sh to parse, -## and quits. +## 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) diff --git a/test_android.sh b/test_android.sh index c847443..c87fa37 100755 --- a/test_android.sh +++ b/test_android.sh @@ -229,6 +229,53 @@ if [ -z "$apk" ]; then 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. @@ -238,34 +285,26 @@ $TIMEOUT_CMD 300 "$ADB" wait-for-device # Includes the crash buffer, so only this run's crashes fail it. "$ADB" logcat -b main,system,crash -c STARTED_APP=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. deadline=$((SECONDS + TIMEOUT_SEC)) -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 +# 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 - -# The app writes Godot's log to a file (see extra_args), which survives adb dropping out. -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) -if [ -z "$result" ]; then - # What Android logged about the app stopping (a crash, or the system killing it), and Godot's own - # output, which reaches logcat even when the log file doesn't. - echo "App exited: $app_exited" - "$ADB" logcat -d -v tag AndroidRuntime:E DEBUG:V libc:F ActivityManager:I lmkd:I godot:V '*:S' 2>/dev/null \ - | tail -300 || true +if [ -z "$result" ] && [ "$exited_early" != true ]; then + print_stop_diagnostics +fi +if [ "$app_exited" != true ]; then + print_app_stacks fi if [ -z "$result" ]; then From a0e314909c92feb6b08b78d3df769a2b8777e636 Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 15 Nov 2026 14:16:23 -0800 Subject: [PATCH 19/20] Address review: unregister RPC handlers when unbinding the local participant Unbinding stopped the destructor from unregistering them, and they held the participant's raw pointer, so an RPC arriving during teardown could reach a freed object. Unregister them when unbinding, and have handlers hold only the participant's ID and emit from the main thread if it still exists, since the SDK may still be running one when it's freed. Co-Authored-By: Claude Opus 5.5 --- src/livekit_participant.cpp | 54 ++++++++++++++++++++++++------------- src/livekit_participant.h | 5 ++++ 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/livekit_participant.cpp b/src/livekit_participant.cpp index 3e93454..4aa5039 100644 --- a/src/livekit_participant.cpp +++ b/src/livekit_participant.cpp @@ -3,6 +3,7 @@ #include "livekit_track_publication.h" #include "sdk_call.h" +#include #include #include @@ -131,18 +132,7 @@ 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 (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. - } - } - } + unregister_rpc_methods(); } std::shared_ptr LiveKitLocalParticipant::get_native_local_participant() const { @@ -289,6 +279,8 @@ void LiveKitLocalParticipant::unpublish_track(const String &track_sid) { } 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(); @@ -342,15 +334,18 @@ void LiveKitLocalParticipant::register_rpc_method(const String &method) { } std::string method_name = method.utf8().get_data(); + // 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, - [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); + [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; }); @@ -360,6 +355,14 @@ void LiveKitLocalParticipant::register_rpc_method(const String &method) { } } +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) { auto lp = get_native_local_participant(); if (!lp) { @@ -386,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); } diff --git a/src/livekit_participant.h b/src/livekit_participant.h index 7e05ffe..5b786a7 100644 --- a/src/livekit_participant.h +++ b/src/livekit_participant.h @@ -66,6 +66,11 @@ class LiveKitLocalParticipant : public LiveKitParticipant { 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(); From 79774cfedf06abd634055f13f1ed69d4de4c816b Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 15 Nov 2026 17:39:49 -0800 Subject: [PATCH 20/20] Stop a receiver's ratchets from undoing its new key in an E2EE test test_encryption_turned_off_and_on_again_stays_on failed about 1 run in 6, on Linux as well as Android. Room 2 was failing to decrypt video with a wrong key, and WebRTC's frame cryptor puts back the key it started a failed ratchet attempt with, even if set_shared_key() replaced it meanwhile. So room 2 usually lost the right key within milliseconds, and the test only passed when its data message got in first. The test is about encryption staying on, so room 2 now doesn't ratchet, like the tests that recover from a wrong key. Co-Authored-By: Claude Opus 5.5 --- test/e2e/test_e2ee.gd | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/e2e/test_e2ee.gd b/test/e2e/test_e2ee.gd index 3eee86e..202c878 100644 --- a/test/e2e/test_e2ee.gd +++ b/test/e2e/test_e2ee.gd @@ -226,7 +226,11 @@ func test_receiver_records_a_participant_key_it_ratcheted(): func test_encryption_turned_off_and_on_again_stays_on(): 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)