Skip to content

Commit 660f8f8

Browse files
authored
fix: properly initialize audio processing controller on android (#213)
1 parent 3924a74 commit 660f8f8

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

android/src/main/java/com/livekit/reactnative/LiveKitReactNative.kt

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,25 @@ object LiveKitReactNative {
2424

2525
val audioDeviceModule: JavaAudioDeviceModule
2626
get() {
27-
val adm = this.adm
27+
return this.adm
2828
?: throw IllegalStateException("Audio device module is not initialized! Did you remember to call LiveKitReactNative.setup in your Application.onCreate?")
29-
return adm
3029
}
3130

32-
private lateinit var _audioProcessingController: AudioProcessingController
31+
private var _audioProcessingController: CustomAudioProcessingController? = null
3332

3433
val audioProcessingController: AudioProcessingController
3534
get() {
36-
if (!::_audioProcessingController.isInitialized) {
37-
throw IllegalStateException("audioProcessingController is not initialized! Did you remember to call LiveKitReactNative.setup in your Application.onCreate?")
38-
}
39-
return _audioProcessingController
35+
return this._audioProcessingController
36+
?: throw IllegalStateException("audioProcessingController is not initialized! Did you remember to call LiveKitReactNative.setup in your Application.onCreate?")
4037
}
4138

4239

43-
lateinit var _audioRecordSamplesDispatcher: AudioRecordSamplesDispatcher
40+
private var _audioRecordSamplesDispatcher: AudioRecordSamplesDispatcher? = null
4441

4542
val audioRecordSamplesDispatcher: AudioRecordSamplesDispatcher
4643
get() {
47-
if (!::_audioRecordSamplesDispatcher.isInitialized) {
44+
return this._audioRecordSamplesDispatcher ?:
4845
throw IllegalStateException("audioRecordSamplesDispatcher is not initialized! Did you remember to call LiveKitReactNative.setup in your Application.onCreate?")
49-
}
50-
return _audioRecordSamplesDispatcher
5146
}
5247

5348

@@ -73,6 +68,13 @@ object LiveKitReactNative {
7368

7469
setupAdm(context)
7570
options.audioDeviceModule = adm
71+
// CustomAudioProcessingController can't be instantiated before WebRTC is loaded.
72+
options.audioProcessingFactoryFactory = Callable {
73+
val apc = CustomAudioProcessingController()
74+
_audioProcessingController = apc
75+
return@Callable apc.externalAudioProcessor
76+
}
77+
7678
}
7779

7880
private fun setupAdm(context: Context) {
@@ -94,15 +96,11 @@ object LiveKitReactNative {
9496
adm?.release()
9597
adm = null
9698

99+
_audioProcessingController = null
100+
101+
// adm needs to be re-setup with a new one each time.
97102
setupAdm(context)
98103
options.audioDeviceModule = adm
99104

100-
// CustomAudioProcessingController can't be instantiated before WebRTC is loaded.
101-
options.audioProcessingFactoryFactory = Callable {
102-
val apc = CustomAudioProcessingController()
103-
_audioProcessingController = apc
104-
return@Callable apc.externalAudioProcessor
105-
}
106-
107105
}
108106
}

example/src/PreJoinPage.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ import type { RootStackParamList } from './App';
1414
import { useTheme } from '@react-navigation/native';
1515
import AsyncStorage from '@react-native-async-storage/async-storage';
1616

17-
const DEFAULT_URL = 'ws://192.168.11.3:7880';
18-
const DEFAULT_TOKEN =
19-
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzU3ODc4OTYsImlzcyI6IkFQSVRMV3JLOHRid3I0NyIsIm5iZiI6MTczMzE5NTg5Niwic3ViIjoicGhvbmUiLCJ2aWRlbyI6eyJyb29tIjoibXlyb29tIiwicm9vbUpvaW4iOnRydWV9fQ.2ucoFK_t1rX8cnUVdx_7nS-TOGv_2Io6uNEw11kPs3M';
17+
const DEFAULT_URL = 'wss://example.livekit.cloud';
18+
const DEFAULT_TOKEN = 'your-token-here';
2019
const DEFAULT_E2EE = false;
2120
const DEFAULT_E2EE_KEY = '';
2221

0 commit comments

Comments
 (0)