davidliu
Committed by GitHub

Initialize RTC library only once (#508)

* Initialize RTC library only once

* spotless and changeset
  1 +---
  2 +"client-sdk-android": patch
  3 +---
  4 +
  5 +Initialize WebRTC library only once
@@ -64,6 +64,11 @@ typealias CapabilitiesGetter = @JvmSuppressWildcards (MediaStreamTrack.MediaType @@ -64,6 +64,11 @@ typealias CapabilitiesGetter = @JvmSuppressWildcards (MediaStreamTrack.MediaType
64 internal object RTCModule { 64 internal object RTCModule {
65 65
66 /** 66 /**
  67 + * To only be written to on the WebRTC thread.
  68 + */
  69 + private var hasInitializedWebrtc = false
  70 +
  71 + /**
67 * Certain classes require libwebrtc to be initialized prior to use. 72 * Certain classes require libwebrtc to be initialized prior to use.
68 * 73 *
69 * If your provision depends on libwebrtc initialization, just add it 74 * If your provision depends on libwebrtc initialization, just add it
@@ -86,32 +91,39 @@ internal object RTCModule { @@ -86,32 +91,39 @@ internal object RTCModule {
86 @Singleton 91 @Singleton
87 @Named(InjectionNames.LIB_WEBRTC_INITIALIZATION) 92 @Named(InjectionNames.LIB_WEBRTC_INITIALIZATION)
88 fun libWebrtcInitialization(appContext: Context): LibWebrtcInitialization { 93 fun libWebrtcInitialization(appContext: Context): LibWebrtcInitialization {
89 - PeerConnectionFactory.initialize(  
90 - PeerConnectionFactory.InitializationOptions  
91 - .builder(appContext)  
92 - .setNativeLibraryName("lkjingle_peerconnection_so")  
93 - .setInjectableLogger(  
94 - { s, severity, s2 ->  
95 - if (!LiveKit.enableWebRTCLogging) {  
96 - return@setInjectableLogger  
97 - }  
98 -  
99 - val loggingLevel = when (severity) {  
100 - Logging.Severity.LS_VERBOSE -> LoggingLevel.VERBOSE  
101 - Logging.Severity.LS_INFO -> LoggingLevel.INFO  
102 - Logging.Severity.LS_WARNING -> LoggingLevel.WARN  
103 - Logging.Severity.LS_ERROR -> LoggingLevel.ERROR  
104 - else -> LoggingLevel.OFF  
105 - }  
106 -  
107 - LKLog.log(loggingLevel) {  
108 - Timber.log(loggingLevel.toAndroidLogPriority(), "$s2: $s")  
109 - }  
110 - },  
111 - Logging.Severity.LS_VERBOSE,  
112 - )  
113 - .createInitializationOptions(),  
114 - ) 94 + if (!hasInitializedWebrtc) {
  95 + executeBlockingOnRTCThread {
  96 + if (!hasInitializedWebrtc) {
  97 + hasInitializedWebrtc = true
  98 + PeerConnectionFactory.initialize(
  99 + PeerConnectionFactory.InitializationOptions
  100 + .builder(appContext)
  101 + .setNativeLibraryName("lkjingle_peerconnection_so")
  102 + .setInjectableLogger(
  103 + { s, severity, s2 ->
  104 + if (!LiveKit.enableWebRTCLogging) {
  105 + return@setInjectableLogger
  106 + }
  107 +
  108 + val loggingLevel = when (severity) {
  109 + Logging.Severity.LS_VERBOSE -> LoggingLevel.VERBOSE
  110 + Logging.Severity.LS_INFO -> LoggingLevel.INFO
  111 + Logging.Severity.LS_WARNING -> LoggingLevel.WARN
  112 + Logging.Severity.LS_ERROR -> LoggingLevel.ERROR
  113 + else -> LoggingLevel.OFF
  114 + }
  115 +
  116 + LKLog.log(loggingLevel) {
  117 + Timber.log(loggingLevel.toAndroidLogPriority(), "$s2: $s")
  118 + }
  119 + },
  120 + Logging.Severity.LS_VERBOSE,
  121 + )
  122 + .createInitializationOptions(),
  123 + )
  124 + }
  125 + }
  126 + }
115 return LibWebrtcInitialization 127 return LibWebrtcInitialization
116 } 128 }
117 129