Skip to content

Commit

Permalink
Initialize RTC library only once (#508)
Browse files Browse the repository at this point in the history
* Initialize RTC library only once

* spotless and changeset
  • Loading branch information
davidliu authored Sep 20, 2024
1 parent 619cd4c commit 41e5aa6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-cherries-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"client-sdk-android": patch
---

Initialize WebRTC library only once
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ typealias CapabilitiesGetter = @JvmSuppressWildcards (MediaStreamTrack.MediaType
@Module
internal object RTCModule {

/**
* To only be written to on the WebRTC thread.
*/
private var hasInitializedWebrtc = false

/**
* Certain classes require libwebrtc to be initialized prior to use.
*
Expand All @@ -86,32 +91,39 @@ internal object RTCModule {
@Singleton
@Named(InjectionNames.LIB_WEBRTC_INITIALIZATION)
fun libWebrtcInitialization(appContext: Context): LibWebrtcInitialization {
PeerConnectionFactory.initialize(
PeerConnectionFactory.InitializationOptions
.builder(appContext)
.setNativeLibraryName("lkjingle_peerconnection_so")
.setInjectableLogger(
{ s, severity, s2 ->
if (!LiveKit.enableWebRTCLogging) {
return@setInjectableLogger
}

val loggingLevel = when (severity) {
Logging.Severity.LS_VERBOSE -> LoggingLevel.VERBOSE
Logging.Severity.LS_INFO -> LoggingLevel.INFO
Logging.Severity.LS_WARNING -> LoggingLevel.WARN
Logging.Severity.LS_ERROR -> LoggingLevel.ERROR
else -> LoggingLevel.OFF
}

LKLog.log(loggingLevel) {
Timber.log(loggingLevel.toAndroidLogPriority(), "$s2: $s")
}
},
Logging.Severity.LS_VERBOSE,
)
.createInitializationOptions(),
)
if (!hasInitializedWebrtc) {
executeBlockingOnRTCThread {
if (!hasInitializedWebrtc) {
hasInitializedWebrtc = true
PeerConnectionFactory.initialize(
PeerConnectionFactory.InitializationOptions
.builder(appContext)
.setNativeLibraryName("lkjingle_peerconnection_so")
.setInjectableLogger(
{ s, severity, s2 ->
if (!LiveKit.enableWebRTCLogging) {
return@setInjectableLogger
}

val loggingLevel = when (severity) {
Logging.Severity.LS_VERBOSE -> LoggingLevel.VERBOSE
Logging.Severity.LS_INFO -> LoggingLevel.INFO
Logging.Severity.LS_WARNING -> LoggingLevel.WARN
Logging.Severity.LS_ERROR -> LoggingLevel.ERROR
else -> LoggingLevel.OFF
}

LKLog.log(loggingLevel) {
Timber.log(loggingLevel.toAndroidLogPriority(), "$s2: $s")
}
},
Logging.Severity.LS_VERBOSE,
)
.createInitializationOptions(),
)
}
}
}
return LibWebrtcInitialization
}

Expand Down

0 comments on commit 41e5aa6

Please sign in to comment.