Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
add mutexes
Browse files Browse the repository at this point in the history
  • Loading branch information
karkakol committed Jun 10, 2024
1 parent 6ccf2e8 commit f467fbe
Showing 1 changed file with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ internal class InternalMembraneRTC(

private val localTracks = mutableListOf<LocalTrack>()
private val localTracksMutex = Mutex()
private val localVideoMutex = Mutex()
private val localMicrophoneMutex = Mutex()
private val localScreencastMutex = Mutex()
private var canUpdateMetadata = false

private val coroutineScope: CoroutineScope =
Expand All @@ -68,14 +71,25 @@ internal class InternalMembraneRTC(
): InternalMembraneRTC
}

private fun getMutexForTrack(trackId: String) : Mutex? {
var mutex: Mutex? = null
localTracks.find { it.id() == trackId }?.let {
mutex = when (it) {
is LocalAudioTrack -> localMicrophoneMutex
is LocalVideoTrack -> localVideoMutex
else -> localScreencastMutex
}
}
return mutex
}

fun disconnect() {
coroutineScope.launch {
rtcEngineCommunication.disconnect()
localTracksMutex.withLock {
localTracks.forEach { it.stop() }
}
peerConnectionManager.close()
canUpdateMetadata = false
}
}

Expand All @@ -85,6 +99,9 @@ internal class InternalMembraneRTC(

fun connect(endpointMetadata: Metadata? = mapOf()) {
coroutineScope.launch {
localMicrophoneMutex.lock()
localVideoMutex.lock()
localScreencastMutex.lock()
localEndpoint = localEndpoint.copy(metadata = endpointMetadata)
rtcEngineCommunication.connect(endpointMetadata ?: mapOf())
}
Expand All @@ -110,6 +127,7 @@ internal class InternalMembraneRTC(
localEndpoint = localEndpoint.withTrack(videoTrack.id(), metadata)

coroutineScope.launch {
localVideoMutex.lock()
peerConnectionManager.addTrack(videoTrack)
rtcEngineCommunication.renegotiateTracks()
}
Expand All @@ -130,6 +148,7 @@ internal class InternalMembraneRTC(
localEndpoint = localEndpoint.withTrack(audioTrack.id(), metadata)

coroutineScope.launch {
localMicrophoneMutex.lock()
peerConnectionManager.addTrack(audioTrack)
rtcEngineCommunication.renegotiateTracks()
}
Expand Down Expand Up @@ -184,6 +203,7 @@ internal class InternalMembraneRTC(
}

coroutineScope.launch {
localScreencastMutex.lock()
peerConnectionManager.addTrack(screencastTrack)
rtcEngineCommunication.renegotiateTracks()
}
Expand Down Expand Up @@ -212,7 +232,6 @@ internal class InternalMembraneRTC(
}

fun updateEndpointMetadata(endpointMetadata: Metadata) {
if (!canUpdateMetadata)return
coroutineScope.launch {
rtcEngineCommunication.updateEndpointMetadata(endpointMetadata)
localEndpoint = localEndpoint.copy(metadata = endpointMetadata)
Expand All @@ -223,13 +242,18 @@ internal class InternalMembraneRTC(
trackId: String,
trackMetadata: Metadata
) {
if (!canUpdateMetadata)return
coroutineScope.launch {
rtcEngineCommunication.updateTrackMetadata(trackId, trackMetadata)
localEndpoint = localEndpoint.withTrack(trackId, trackMetadata)
val mutex = getMutexForTrack(trackId)

mutex?.run{
coroutineScope.launch {
rtcEngineCommunication.updateTrackMetadata(trackId, trackMetadata)
localEndpoint = localEndpoint.withTrack(trackId, trackMetadata)
}
}
}



override fun onConnected(
endpointID: String,
otherEndpoints: List<Endpoint>
Expand Down Expand Up @@ -351,7 +375,9 @@ internal class InternalMembraneRTC(
}
}
}
canUpdateMetadata = true
localVideoMutex.unlock()
localMicrophoneMutex.unlock()
localScreencastMutex.unlock()
}
}
}
Expand Down

0 comments on commit f467fbe

Please sign in to comment.