diff --git a/android/src/main/java/org/membraneframework/reactnative/MembraneWebRTC.kt b/android/src/main/java/org/membraneframework/reactnative/MembraneWebRTC.kt index 9dc07b3..efce804 100644 --- a/android/src/main/java/org/membraneframework/reactnative/MembraneWebRTC.kt +++ b/android/src/main/java/org/membraneframework/reactnative/MembraneWebRTC.kt @@ -79,7 +79,12 @@ class MembraneWebRTC(val sendEvent: (name: String, data: Map) -> U var onTracksUpdateListeners: MutableList = mutableListOf() } - fun onDestroy() { + fun onModuleCreate(appContext: AppContext){ + this.appContext = appContext + this.audioSwitchManager = AudioSwitchManager(appContext.reactContext!!) + } + + fun onModuleDestroy() { audioSwitchManager?.stop() } @@ -98,7 +103,7 @@ class MembraneWebRTC(val sendEvent: (name: String, data: Map) -> U } } - private fun getSimulcastConfigFromOptions(simulcastConfigMap: com.reactnativemembrane.SimulcastConfig): SimulcastConfig { + private fun getSimulcastConfigFromOptions(simulcastConfigMap: org.membraneframework.reactnative.SimulcastConfig): SimulcastConfig { val simulcastEnabled = simulcastConfigMap.enabled val activeEncodings = simulcastConfigMap.activeEncodings.map { e -> e.toTrackEncoding() } return SimulcastConfig( @@ -137,7 +142,6 @@ class MembraneWebRTC(val sendEvent: (name: String, data: Map) -> U } fun create() { - audioSwitchManager = AudioSwitchManager(appContext?.reactContext!!) membraneRTC = MembraneRTC.create( appContext = appContext?.reactContext!!, listener = this ) diff --git a/android/src/main/java/org/membraneframework/reactnative/MembraneWebRTCModule.kt b/android/src/main/java/org/membraneframework/reactnative/MembraneWebRTCModule.kt index a9179df..ba86fd2 100644 --- a/android/src/main/java/org/membraneframework/reactnative/MembraneWebRTCModule.kt +++ b/android/src/main/java/org/membraneframework/reactnative/MembraneWebRTCModule.kt @@ -91,7 +91,19 @@ class MembraneWebRTCModule : Module() { } OnCreate { - membraneWebRTC.appContext = appContext + membraneWebRTC.onModuleCreate(appContext) + } + + OnDestroy { + membraneWebRTC.onModuleDestroy() + } + + OnActivityDestroys { + membraneWebRTC.disconnect() + } + + OnActivityResult { _, result -> + membraneWebRTC.onActivityResult(result.requestCode, result.resultCode, result.data) } AsyncFunction("create") Coroutine ({ -> @@ -272,17 +284,5 @@ class MembraneWebRTCModule : Module() { AsyncFunction("getStatistics") { -> membraneWebRTC.getStatistics() } - - OnActivityResult { _, result -> - membraneWebRTC.onActivityResult(result.requestCode, result.resultCode, result.data) - } - - OnDestroy { - membraneWebRTC.onDestroy() - } - - OnActivityDestroys { - membraneWebRTC.disconnect() - } } } diff --git a/src/common/webRTC.ts b/src/common/webRTC.ts index da2eb78..386e200 100644 --- a/src/common/webRTC.ts +++ b/src/common/webRTC.ts @@ -1,14 +1,6 @@ import { LoggingSeverity, TrackEncoding } from '../MembraneWebRTC.types'; import MembraneWebRTCModule from '../MembraneWebRTCModule'; -/** - * This function initialize necessary native objects to properly handle sound and video. - * Call it only once in your app before any other functionality, otherwise package will not work as intended. - */ -export async function initializeWebRTC() { - await MembraneWebRTCModule.create(); -} - /** * sets track encoding that server should send to the client library. * The encoding will be sent whenever it is available. If chooses encoding is diff --git a/src/hooks/useWebRTC.ts b/src/hooks/useWebRTC.ts index 94395fc..5381b2a 100644 --- a/src/hooks/useWebRTC.ts +++ b/src/hooks/useWebRTC.ts @@ -5,14 +5,6 @@ import { ConnectionOptions, Metadata } from '../MembraneWebRTC.types'; import MembraneWebRTCModule from '../MembraneWebRTCModule'; import { ReceivableEvents, eventEmitter } from '../common/eventEmitter'; -/** - * This function initialize necessary native objects to properly handle sound and video. - * Call it only once in your app before any other functionality, otherwise package will not work as intended. - */ -export async function initializeWebRTC() { - await MembraneWebRTCModule.create(); -} - /** * The hook used to manage a connection with membrane server. * @returns An object with functions to manage membrane server connection and `error` if connection failed. @@ -113,6 +105,9 @@ export function useWebRTC() { socket.current = _socket; webrtcChannel.current = _webrtcChannel; + + await MembraneWebRTCModule.create(); + await new Promise((resolve, reject) => { _webrtcChannel .join() diff --git a/src/index.tsx b/src/index.tsx index 8ae067e..6bca02e 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -13,7 +13,6 @@ export { updateVideoTrackMetadata, } from './common/metadata'; export { - initializeWebRTC, changeWebRTCLoggingSeverity, setTargetTrackEncoding, } from './common/webRTC';