davidliu
Committed by GitHub

Fix memory leaks (#306)

* Fix memory leaks

* Fix data channel leaking RTCEngine

* Add room to leak canary in sample app

* remove leakcanary watching of room
... ... @@ -91,6 +91,7 @@ object RTCModule {
moduleCustomizer: ((builder: JavaAudioDeviceModule.Builder) -> Unit)?,
audioOutputAttributes: AudioAttributes,
appContext: Context,
closeableManager: CloseableManager,
): AudioDeviceModule {
if (audioDeviceModuleOverride != null) {
return audioDeviceModuleOverride
... ... @@ -167,12 +168,13 @@ object RTCModule {
moduleCustomizer?.invoke(builder)
return builder.createAudioDeviceModule()
.apply { closeableManager.registerClosable { release() } }
}
@Provides
@Singleton
fun eglBase(
@Singleton memoryManager: CloseableManager,
memoryManager: CloseableManager,
): EglBase {
val eglBase = EglBase.create()
memoryManager.registerResource(eglBase) { eglBase.release() }
... ... @@ -234,12 +236,14 @@ object RTCModule {
audioDeviceModule: AudioDeviceModule,
videoEncoderFactory: VideoEncoderFactory,
videoDecoderFactory: VideoDecoderFactory,
memoryManager: CloseableManager,
): PeerConnectionFactory {
return PeerConnectionFactory.builder()
.setAudioDeviceModule(audioDeviceModule)
.setVideoEncoderFactory(videoEncoderFactory)
.setVideoDecoderFactory(videoDecoderFactory)
.createPeerConnectionFactory()
.apply { memoryManager.registerClosable { dispose() } }
}
@Provides
... ...
... ... @@ -25,7 +25,6 @@ import io.livekit.android.stats.AndroidNetworkInfo
import io.livekit.android.stats.NetworkInfo
import okhttp3.OkHttpClient
import okhttp3.WebSocket
import java.util.concurrent.TimeUnit
import javax.inject.Named
import javax.inject.Singleton
... ... @@ -41,10 +40,6 @@ object WebModule {
@Nullable
okHttpClientOverride: OkHttpClient?,
): OkHttpClient {
OkHttpClient.Builder()
.pingInterval(20, TimeUnit.SECONDS)
.build()
return okHttpClientOverride ?: OkHttpClient()
}
... ...
... ... @@ -149,7 +149,7 @@ constructor(
}
fun close() {
peerConnection.close()
peerConnection.dispose()
}
fun updateRTCConfig(config: RTCConfiguration) {
... ...
... ... @@ -314,13 +314,19 @@ internal constructor(
_publisher = null
_subscriber?.close()
_subscriber = null
reliableDataChannel?.close()
fun DataChannel?.completeDispose() {
this?.unregisterObserver()
this?.close()
this?.dispose()
}
reliableDataChannel?.completeDispose()
reliableDataChannel = null
reliableDataChannelSub?.close()
reliableDataChannelSub?.completeDispose()
reliableDataChannelSub = null
lossyDataChannel?.close()
lossyDataChannel?.completeDispose()
lossyDataChannel = null
lossyDataChannelSub?.close()
lossyDataChannelSub?.completeDispose()
lossyDataChannelSub = null
isSubscriberPrimary = false
client.close(reason = reason)
... ...
... ... @@ -49,7 +49,6 @@ import livekit.LivekitModels
import livekit.LivekitRtc
import org.webrtc.*
import javax.inject.Named
import javax.inject.Singleton
class Room
@AssistedInject
... ... @@ -64,7 +63,6 @@ constructor(
@Named(InjectionNames.DISPATCHER_IO)
private val ioDispatcher: CoroutineDispatcher,
val audioHandler: AudioHandler,
@Singleton
private val closeableManager: CloseableManager,
private val e2EEManagerFactory: E2EEManager.Factory,
) : RTCEngine.Listener, ParticipantListener {
... ...
... ... @@ -127,7 +127,7 @@ class CallViewModel(
handlePrimarySpeaker(
participantsList,
speakers,
room
room,
)
}
}
... ... @@ -142,6 +142,7 @@ class CallViewModel(
messagesReceived++
Timber.e { "message received from $identity, count $messagesReceived" }
}
else -> {
Timber.e { "Room event: $it" }
}
... ... @@ -182,7 +183,7 @@ class CallViewModel(
room.connect(
url = url,
token = token,
roomOptions = RoomOptions(e2eeOptions = getE2EEOptions())
roomOptions = RoomOptions(e2eeOptions = getE2EEOptions()),
)
// Create and publish audio/video tracks
... ... @@ -246,7 +247,7 @@ class CallViewModel(
val screencastTrack =
localParticipant.createScreencastTrack(mediaProjectionPermissionResultData = mediaProjectionPermissionResultData)
localParticipant.publishVideoTrack(
screencastTrack
screencastTrack,
)
// Must start the foreground prior to startCapture.
... ... @@ -270,6 +271,8 @@ class CallViewModel(
override fun onCleared() {
super.onCleared()
// Make sure to release any resources associated with LiveKit
room.disconnect()
room.release()
... ...