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

Commit

Permalink
add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
karkakol committed Jun 10, 2024
1 parent f467fbe commit 0cbd096
Showing 1 changed file with 89 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.membraneframework.rtc

import android.content.Context
import android.content.Intent
import android.util.Log
import com.fishjamdev.client.BuildConfig
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
Expand All @@ -25,7 +26,7 @@ import org.webrtc.MediaStreamTrack
import org.webrtc.VideoTrack
import timber.log.Timber
import java.util.*

const val T = "IMRTC"
internal class InternalMembraneRTC(
createOptions: CreateOptions,
private val listener: MembraneRTCListener,
Expand All @@ -49,10 +50,10 @@ 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 localVideoMutex = Mutex()
// private val localMicrophoneMutex = Mutex()
// private val localScreencastMutex = Mutex()
// private var canUpdateMetadata = false

private val coroutineScope: CoroutineScope =
ClosableCoroutineScope(SupervisorJob() + defaultDispatcher)
Expand All @@ -71,19 +72,20 @@ 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
}
// 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() {
Log.i(T, "disconnect")
coroutineScope.launch {
rtcEngineCommunication.disconnect()
localTracksMutex.withLock {
Expand All @@ -94,14 +96,17 @@ internal class InternalMembraneRTC(
}

fun receiveMediaEvent(event: SerializedMediaEvent) {
Log.i(T, "receiveMediaEvent")
rtcEngineCommunication.onEvent(event)
}

fun connect(endpointMetadata: Metadata? = mapOf()) {
Log.i(T, "connect")

coroutineScope.launch {
localMicrophoneMutex.lock()
localVideoMutex.lock()
localScreencastMutex.lock()
// localMicrophoneMutex.lock()
// localVideoMutex.lock()
// localScreencastMutex.lock()
localEndpoint = localEndpoint.copy(metadata = endpointMetadata)
rtcEngineCommunication.connect(endpointMetadata ?: mapOf())
}
Expand All @@ -112,6 +117,8 @@ internal class InternalMembraneRTC(
metadata: Metadata = mapOf(),
captureDeviceName: String? = null
): LocalVideoTrack {
Log.i(T, "createLocalVideoTrack")

val videoTrack =
LocalVideoTrack.create(
context,
Expand All @@ -127,7 +134,7 @@ internal class InternalMembraneRTC(
localEndpoint = localEndpoint.withTrack(videoTrack.id(), metadata)

coroutineScope.launch {
localVideoMutex.lock()
// localVideoMutex.lock()
peerConnectionManager.addTrack(videoTrack)
rtcEngineCommunication.renegotiateTracks()
}
Expand All @@ -136,6 +143,8 @@ internal class InternalMembraneRTC(
}

fun createLocalAudioTrack(metadata: Metadata = mapOf()): LocalAudioTrack {
Log.i(T, "createLocalAudioTrack")

val audioTrack =
LocalAudioTrack.create(
context,
Expand All @@ -148,7 +157,7 @@ internal class InternalMembraneRTC(
localEndpoint = localEndpoint.withTrack(audioTrack.id(), metadata)

coroutineScope.launch {
localMicrophoneMutex.lock()
// localMicrophoneMutex.lock()
peerConnectionManager.addTrack(audioTrack)
rtcEngineCommunication.renegotiateTracks()
}
Expand All @@ -160,6 +169,8 @@ internal class InternalMembraneRTC(
trackId: String,
bandwidthLimit: TrackBandwidthLimit.BandwidthLimit
) {
Log.i(T, "setTrackBandwidth")

coroutineScope.launch {
peerConnectionManager.setTrackBandwidth(trackId, bandwidthLimit)
}
Expand All @@ -170,6 +181,8 @@ internal class InternalMembraneRTC(
encoding: String,
bandwidthLimit: TrackBandwidthLimit.BandwidthLimit
) {
Log.i(T, "setEncodingBandwidth")

coroutineScope.launch {
peerConnectionManager.setEncodingBandwidth(trackId, encoding, bandwidthLimit)
}
Expand All @@ -181,6 +194,8 @@ internal class InternalMembraneRTC(
metadata: Metadata = mapOf(),
onEnd: (() -> Unit)?
): LocalScreencastTrack {
Log.i(T, "createScreencastTrack")

val screencastTrack =
LocalScreencastTrack.create(
context,
Expand All @@ -203,7 +218,7 @@ internal class InternalMembraneRTC(
}

coroutineScope.launch {
localScreencastMutex.lock()
// localScreencastMutex.lock()
peerConnectionManager.addTrack(screencastTrack)
rtcEngineCommunication.renegotiateTracks()
}
Expand All @@ -212,6 +227,8 @@ internal class InternalMembraneRTC(
}

fun removeTrack(trackId: String): Boolean {
Log.i(T, "removeTrack")

return runBlocking(Dispatchers.Default) {
localTracksMutex.withLock {
val track =
Expand All @@ -232,6 +249,8 @@ internal class InternalMembraneRTC(
}

fun updateEndpointMetadata(endpointMetadata: Metadata) {
Log.i(T, "updateEndpointMetadata")

coroutineScope.launch {
rtcEngineCommunication.updateEndpointMetadata(endpointMetadata)
localEndpoint = localEndpoint.copy(metadata = endpointMetadata)
Expand All @@ -242,13 +261,12 @@ internal class InternalMembraneRTC(
trackId: String,
trackMetadata: Metadata
) {
val mutex = getMutexForTrack(trackId)
Log.i(T, "updateTrackMetadata")

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

Expand All @@ -258,6 +276,8 @@ internal class InternalMembraneRTC(
endpointID: String,
otherEndpoints: List<Endpoint>
) {
Log.i(T, "onConnected")

this.localEndpoint = localEndpoint.copy(id = endpointID)
listener.onConnected(endpointID, otherEndpoints)

Expand All @@ -282,10 +302,14 @@ internal class InternalMembraneRTC(
}

override fun onSendMediaEvent(event: SerializedMediaEvent) {
Log.i(T, "onSendMediaEvent")

listener.onSendMediaEvent(event)
}

override fun onEndpointAdded(endpoint: Endpoint) {
Log.i(T, "onEndpointAdded")

if (endpoint.id == this.localEndpoint.id) {
return
}
Expand All @@ -296,6 +320,8 @@ internal class InternalMembraneRTC(
}

override fun onEndpointRemoved(endpointId: String) {
Log.i(T, "onEndpointRemoved")

if (endpointId == localEndpoint.id) {
listener.onDisconnected()
return
Expand All @@ -321,6 +347,8 @@ internal class InternalMembraneRTC(
endpointId: String,
endpointMetadata: Metadata?
) {
Log.i(T, "onEndpointUpdated")

val endpoint =
remoteEndpoints.remove(endpointId) ?: run {
Timber.e("Failed to process EndpointUpdated event: Endpoint not found: $endpointId")
Expand All @@ -334,6 +362,8 @@ internal class InternalMembraneRTC(
integratedTurnServers: List<OfferData.TurnServer>,
tracksTypes: Map<String, Int>
) {
Log.i(T, "onOfferData")

coroutineScope.launch {
try {
val offer =
Expand All @@ -356,6 +386,8 @@ internal class InternalMembraneRTC(
sdp: String,
midToTrackId: Map<String, String>
) {
Log.i(T, "onSdpAnswer")

coroutineScope.launch {
peerConnectionManager.onSdpAnswer(sdp, midToTrackId)

Expand All @@ -375,9 +407,9 @@ internal class InternalMembraneRTC(
}
}
}
localVideoMutex.unlock()
localMicrophoneMutex.unlock()
localScreencastMutex.unlock()
// localVideoMutex.unlock()
// localMicrophoneMutex.unlock()
// localScreencastMutex.unlock()
}
}
}
Expand All @@ -387,6 +419,8 @@ internal class InternalMembraneRTC(
sdpMLineIndex: Int,
sdpMid: String?
) {
Log.i(T, "onRemoteCandidate")

coroutineScope.launch {
val iceCandidate =
IceCandidate(
Expand All @@ -403,6 +437,8 @@ internal class InternalMembraneRTC(
endpointId: String,
tracks: Map<String, TrackData>
) {
Log.i(T, "onTracksAdded")

if (localEndpoint.id == endpointId) return

val endpoint =
Expand Down Expand Up @@ -435,6 +471,8 @@ internal class InternalMembraneRTC(
endpointId: String,
trackIds: List<String>
) {
Log.i(T, "onTracksRemoved")

val endpoint =
remoteEndpoints[endpointId] ?: run {
Timber.e("Failed to process TracksRemoved event: Endpoint not found: $endpointId")
Expand All @@ -460,6 +498,8 @@ internal class InternalMembraneRTC(
trackId: String,
metadata: Metadata?
) {
Log.i(T, "onTrackUpdated")

val endpoint =
remoteEndpoints[endpointId] ?: run {
Timber.e("Failed to process TrackUpdated event: Endpoint not found: $endpointId")
Expand Down Expand Up @@ -490,6 +530,8 @@ internal class InternalMembraneRTC(
encoding: String,
encodingReason: String
) {
Log.i(T, "onTrackEncodingChanged")

val encodingReasonEnum = EncodingReason.fromString(encodingReason)
if (encodingReasonEnum == null) {
Timber.e("Invalid encoding reason: $encodingReason")
Expand All @@ -512,6 +554,8 @@ internal class InternalMembraneRTC(
trackId: String,
status: String
) {
Log.i(T, "onVadNotification")

val trackContext = trackContexts[trackId]
if (trackContext == null) {
Timber.e("Invalid track id = $trackId")
Expand All @@ -526,13 +570,17 @@ internal class InternalMembraneRTC(
}

override fun onBandwidthEstimation(estimation: Long) {
Log.i(T, "onBandwidthEstimation")

listener.onBandwidthEstimationChanged(estimation)
}

fun setTargetTrackEncoding(
trackId: String,
encoding: TrackEncoding
) {
Log.i(T, "setTargetTrackEncoding")

coroutineScope.launch {
rtcEngineCommunication.setTargetTrackEncoding(trackId, encoding)
}
Expand All @@ -542,6 +590,8 @@ internal class InternalMembraneRTC(
trackId: String,
encoding: TrackEncoding
) {
Log.i(T, "enableTrackEncoding")

coroutineScope.launch {
peerConnectionManager.setTrackEncoding(trackId, encoding, true)
}
Expand All @@ -551,12 +601,16 @@ internal class InternalMembraneRTC(
trackId: String,
encoding: TrackEncoding
) {
Log.i(T, "disableTrackEncoding")

coroutineScope.launch {
peerConnectionManager.setTrackEncoding(trackId, encoding, false)
}
}

override fun onLocalIceCandidate(candidate: IceCandidate) {
Log.i(T, "onLocalIceCandidate")

coroutineScope.launch {
rtcEngineCommunication.localCandidate(candidate.sdp, candidate.sdpMLineIndex)
}
Expand All @@ -566,6 +620,8 @@ internal class InternalMembraneRTC(
trackId: String,
track: MediaStreamTrack
) {
Log.i(T, "onAddTrack")

val trackContext =
trackContexts[trackId] ?: run {
Timber.e("onAddTrack: Track context with trackId=$trackId not found")
Expand All @@ -587,6 +643,8 @@ internal class InternalMembraneRTC(
}

fun getStats(): Map<String, RTCStats> {
Log.i(T, "getStats")

return peerConnectionManager.getStats()
}
}

0 comments on commit 0cbd096

Please sign in to comment.