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,6 +91,10 @@ internal object RTCModule { @@ -86,6 +91,10 @@ 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 {
  94 + if (!hasInitializedWebrtc) {
  95 + executeBlockingOnRTCThread {
  96 + if (!hasInitializedWebrtc) {
  97 + hasInitializedWebrtc = true
89 PeerConnectionFactory.initialize( 98 PeerConnectionFactory.initialize(
90 PeerConnectionFactory.InitializationOptions 99 PeerConnectionFactory.InitializationOptions
91 .builder(appContext) 100 .builder(appContext)
@@ -112,6 +121,9 @@ internal object RTCModule { @@ -112,6 +121,9 @@ internal object RTCModule {
112 ) 121 )
113 .createInitializationOptions(), 122 .createInitializationOptions(),
114 ) 123 )
  124 + }
  125 + }
  126 + }
115 return LibWebrtcInitialization 127 return LibWebrtcInitialization
116 } 128 }
117 129