Skip to content

fix Android PlatformAudio#1104

Open
xianshijing-lk wants to merge 1 commit into
mainfrom
sxian/CLT-2934/fix_platformAudio_on_android
Open

fix Android PlatformAudio#1104
xianshijing-lk wants to merge 1 commit into
mainfrom
sxian/CLT-2934/fix_platformAudio_on_android

Conversation

@xianshijing-lk
Copy link
Copy Markdown
Contributor

Before you submit your PR

Make sure the following is true before submitting your PR:

  • I have read the contributing guidelines and validated that this PR will be accepted.
  • I have read and followed the principles regarding breaking changes, testing, and code quality.

PR description

⏺ Android PlatformAudio: Problems & Solutions

Problem 1: ADM Creation Failed

Symptom:
CreateAudioDeviceModule returned nullptr
Failed to initialize PlatformAudio

Root Cause:
WebRTC's CreateAndroidAudioDeviceModule internally calls GetAppContext() to get the Android application context. But GetAppContext() returns null unless ContextUtils.initialize(context) was called first.

CreateAndroidAudioDeviceModule()
└── GetAppContext()
└── Returns null ❌ (ContextUtils not initialized)

Solution:
Pass the Android Context from Unity to Rust, then call ContextUtils.initialize(context) before any WebRTC audio initialization.

Unity Rust/C++
───── ────────
GetApplicationContext() ──────► livekit_ffi_initialize_android_context()

├── do_android_init(vm) // JVM init
└── ContextUtils.initialize(context) // Now GetAppContext() works!

Files changed:

  • livekit-ffi/src/cabi.rs - Added livekit_ffi_initialize_android_context()
  • webrtc-sys/src/android.cpp - Added init_android_context()
  • client-sdk-unity/.../FFIClient.cs - Calls the init with context
  • client-sdk-unity/.../NativeMethods.cs - P/Invoke declaration

Problem 2: Wrong ADM Factory Function

Symptom:
AdmProxy: All Android audio layers failed!

Root Cause:
The code was using the generic CreateAudioDeviceModule() which doesn't work properly on Android. Android requires CreateAndroidAudioDeviceModule() from the Android-specific header.

Solution:
Changed adm_proxy.cpp to use the correct function:

// Before (wrong):
#include "modules/audio_device/include/audio_device.h"
platform_adm_ = webrtc::AudioDeviceModule::Create(...);

// After (correct):
#include "sdk/android/native_api/audio_device_module/audio_device_android.h"
platform_adm_ = webrtc::CreateAndroidAudioDeviceModule(...);

File changed:

  • webrtc-sys/src/adm_proxy.cpp

Problem 3: Empty GUID Validation Failed

Symptom:
Recording device at index 0 has no GUID

Root Cause:
Unity SDK threw an exception when device GUID was empty. But on Android, devices don't have GUIDs - WebRTC returns empty strings.

Solution:
Allow empty GUIDs and pass them to native code, which falls back to index 0:

// Before:
if (string.IsNullOrEmpty(deviceId))
throw new InvalidOperationException("has no GUID");

// After:
SetRecordingDevice(deviceId ?? ""); // Empty string triggers fallback

File changed:

  • client-sdk-unity/.../PlatformAudio.cs

Problem 4: Device Not Found with Empty GUID

Symptom:
Failed to set recording device: Device not found

Root Cause:
Native code searched for a device matching the empty GUID string, found nothing.

Solution:
Added fallback in C++ - if GUID is empty and devices exist, use index 0:

bool AudioDeviceController::set_recording_device_by_guid(rust::String guid) const {
int16_t count = adm_proxy_->RecordingDevices();

  // Android fallback: empty GUID → use index 0
  if (guid.empty() && count > 0) {
      return adm_proxy_->SetRecordingDevice(0) == 0;
  }
  // ... normal GUID lookup for desktop

}

File changed:

  • webrtc-sys/src/audio_device_controller.cpp

Breaking changes

N/A

MSRV

N/A

Testing

Tested with Unity

Async

N/A

@github-actions
Copy link
Copy Markdown
Contributor

Changeset

The following package versions will be affected by this PR:

Package Bump
libwebrtc patch
livekit patch
livekit-ffi minor
webrtc-sys patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants