Skip to content

Commit f48ada0

Browse files
authored
Do not fail audio capture when local recording pre-warm fails on Android (#1188)
## Problem `LocalAudioTrack.startCapture()` calls `Native.startLocalRecording(...)` before publishing. On Android, `handleStartLocalRecording` catches every `Throwable` from `JavaAudioDeviceModule.prewarmRecording(options)` and reports it as `applyFailed`, which Dart converts into `AudioProcessingException` and rethrows out of `startCapture()`, aborting the publish. On devices where WebRTC cannot open an `AudioRecord` at that moment, the pre-warm throws `java.lang.AssertionError: Expected condition to be true`, so the app sees ``` AudioProcessingException(applyFailed): Expected condition to be true ``` and the microphone is never published — although nothing is wrong with the requested audio processing options, and the assertion text says nothing an application can act on. ### Why the assertion fires In webrtc-sdk `144.7559.09`, the pinned native build: ```java public void prewarmRecording(@nullable AudioProcessingOptions options) { audioInput.applyPlatformAudioProcessingOptions(options); audioInput.initRecordingIfNeeded(); // boolean result discarded audioInput.prewarmRecordingIfNeeded(); } ``` `initRecordingIfNeeded()` returns `false` when `AudioRecord` creation or initialization fails — the microphone is already captured by another process, a vendor capture restriction applies, or `getMinBufferSize` rejects the configuration. That result is discarded, and `initAudioRecord()` has already called `releaseAudioResources()`, which leaves `audioRecord` null. `prewarmRecordingIfNeeded()` then runs anyway and reaches `startRecordingImpl()`: ```java assertTrue(audioThread == null); if (useAudioRecord) { assertTrue(audioRecord != null); // throws AssertionError here ``` The real reason for the failure is only in logcat under `WebRtcAudioRecordExternal` ("Creation or initialization of audio recorder failed.", "AudioRecord.getMinBufferSize failed: ..."). The same assertion has been reported against the Android SDK in livekit/client-sdk-android#700. ## Fix Treat the pre-warm as best effort: log the failure and let capture continue. - **The processing options are already applied.** `applyPlatformAudioProcessingOptions(options)` is the first statement of `prewarmRecording`, so the reason the pre-warm exists — applying capture-time processing before WebRTC opens the microphone — is still satisfied when the later recorder preparation fails. - **WebRTC opens the recorder again on the real start path.** Because the failed attempt leaves `audioRecord` null, the native `initRecording()` re-enters `initRecordingImpl()` when the track is actually published, and failures there are reported through the audio device module's own recording callbacks. Aborting at pre-warm time removes that second attempt entirely, which turns a transiently busy microphone into a permanent publish failure. - **A recorder that cannot be prepared is not an audio processing failure.** Reporting it as `applyFailed` is misleading, and it makes `AudioProcessingException` mean two different things. After this change the exception keeps its documented meaning, that the requested options could not be applied, and `AudioManager.getAudioProcessingState()` still reports the resolved state. The iOS and macOS paths are unchanged. ## Reproducing On Android, hold the microphone with another application, then publish the mic. - Before: `startCapture()` throws `AudioProcessingException(applyFailed): Expected condition to be true` and the track is never published. - After: a warning is logged under the `LiveKitPlugin` tag, the publish proceeds, and the recorder is opened again on the normal start path. ## Notes - Includes a `patch` / `fixed` changeset. - No automated test: reproducing it needs a device on which `AudioRecord` creation fails, and the Android plugin has no instrumentation-test harness here. `:livekit_client:compileDebugKotlin` and the existing `:livekit_client` unit tests pass.
1 parent 7f63585 commit f48ada0

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
patch type="fixed" "Android: a failed local recording pre-warm no longer aborts microphone capture with AudioProcessingException(applyFailed)"

‎android/src/main/kotlin/io/livekit/plugin/LiveKitPlugin.kt‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package io.livekit.plugin
1919
import android.annotation.SuppressLint
2020
import android.os.Handler
2121
import android.os.Looper
22+
import android.util.Log
2223
import androidx.annotation.NonNull
2324

2425
import io.flutter.embedding.engine.plugins.FlutterPlugin
@@ -276,8 +277,18 @@ class LiveKitPlugin : FlutterPlugin, MethodCallHandler {
276277
result.success(null)
277278
}
278279
} catch (error: Throwable) {
280+
// Pre-warming is best effort. prewarmRecording() applies the platform
281+
// audio processing options before it prepares the recorder, so the
282+
// options are in effect even when preparing fails, and WebRTC opens
283+
// the recorder again on the real start path and reports its own
284+
// init/start errors there. Surfacing this as applyFailed instead
285+
// aborts startCapture() and fails the publish outright on devices
286+
// that cannot open an AudioRecord yet (microphone held by another
287+
// app, vendor capture restrictions) — an audio processing failure
288+
// the requested options never caused.
289+
Log.w(TAG, "Failed to prewarm local recording, continuing without it", error)
279290
mainHandler.post {
280-
result.error("applyFailed", error.message, null)
291+
result.success(null)
281292
}
282293
}
283294
}
@@ -479,4 +490,8 @@ class LiveKitPlugin : FlutterPlugin, MethodCallHandler {
479490
audioProcessors.values.forEach { it.cleanup() }
480491
audioProcessors.clear()
481492
}
493+
494+
companion object {
495+
private const val TAG = "LiveKitPlugin"
496+
}
482497
}

0 commit comments

Comments
 (0)