11package com.livekit.reactnative
22
3+ import android.annotation.SuppressLint
34import android.app.Application
45import android.content.Context
56import android.os.Build
67import com.livekit.reactnative.audio.AudioType
7- import com.livekit.reactnative.video.CustomVideoEncoderFactory
88import com.livekit.reactnative.video.CustomVideoDecoderFactory
9+ import com.livekit.reactnative.video.CustomVideoEncoderFactory
910import com.oney.WebRTCModule.WebRTCModuleOptions
1011import org.webrtc.audio.JavaAudioDeviceModule
1112
1213object LiveKitReactNative {
1314
14- private lateinit var adm: JavaAudioDeviceModule
15+
16+ private var audioType: AudioType = AudioType .CommunicationAudioType ()
17+
18+ @SuppressLint(" StaticFieldLeak" )
19+ private var adm: JavaAudioDeviceModule ? = null
1520
1621 val audioDeviceModule: JavaAudioDeviceModule
1722 get() {
18- if (! ::adm.isInitialized) {
19- throw IllegalStateException (" Audio device module is not initialized! Did you remember to call LiveKitReactNative.setup in your Application.onCreate?" )
20- }
21-
23+ val adm = this .adm
24+ ? : throw IllegalStateException (" Audio device module is not initialized! Did you remember to call LiveKitReactNative.setup in your Application.onCreate?" )
2225 return adm
2326 }
2427
@@ -34,19 +37,35 @@ object LiveKitReactNative {
3437 context : Context ,
3538 audioType : AudioType = AudioType .CommunicationAudioType ()
3639 ) {
40+ this .audioType = audioType
3741 val options = WebRTCModuleOptions .getInstance()
3842 options.videoEncoderFactory = CustomVideoEncoderFactory (null , true , true )
3943 options.videoDecoderFactory = CustomVideoDecoderFactory ()
4044 options.enableMediaProjectionService = true
4145
46+ setupAdm(context)
47+ options.audioDeviceModule = adm
48+ }
49+
50+ private fun setupAdm (context : Context ) {
4251 val useHardwareAudioProcessing = Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q
4352
4453 adm = JavaAudioDeviceModule .builder(context)
4554 .setUseHardwareAcousticEchoCanceler(useHardwareAudioProcessing)
4655 .setUseHardwareNoiseSuppressor(useHardwareAudioProcessing)
4756 .setAudioAttributes(audioType.audioAttributes)
4857 .createAudioDeviceModule()
58+ }
59+
60+ internal fun invalidate (context : Context ) {
61+ val options = WebRTCModuleOptions .getInstance()
62+ if (options.audioDeviceModule == adm) {
63+ options.audioDeviceModule = null
64+ }
65+ adm?.release()
66+ adm = null
4967
68+ setupAdm(context)
5069 options.audioDeviceModule = adm
5170 }
5271}
0 commit comments