@@ -63,6 +63,11 @@ typealias CapabilitiesGetter = @JvmSuppressWildcards (MediaStreamTrack.MediaType
6363@Module
6464internal object RTCModule {
6565
66+ /* *
67+ * To only be written to on the WebRTC thread.
68+ */
69+ private var hasInitializedWebrtc = false
70+
6671 /* *
6772 * Certain classes require libwebrtc to be initialized prior to use.
6873 *
@@ -86,32 +91,39 @@ internal object RTCModule {
8691 @Singleton
8792 @Named(InjectionNames .LIB_WEBRTC_INITIALIZATION )
8893 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+ }
115127 return LibWebrtcInitialization
116128 }
117129
0 commit comments