Last active
January 27, 2023 05:28
-
-
Save skydoves/4e95ef22e8d9c74ed710e2278af2c7b6 to your computer and use it in GitHub Desktop.
WebRtcSessionManagerImpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2023 Stream.IO, Inc. All Rights Reserved. | |
// SPDX-License-Identifier: Apache-2.0 | |
class WebRtcSessionManagerImpl( | |
private val context: Context, | |
override val signalingClient: SignalingClient, | |
override val peerConnectionFactory: StreamPeerConnectionFactory | |
) : WebRtcSessionManager { | |
private val logger by taggedLogger("Call:LocalWebRtcSessionManager") | |
private val sessionManagerScope = CoroutineScope(SupervisorJob() + Dispatchers.Default) | |
// used to send remote video track to the sender | |
private val _remoteVideoSinkFlow = MutableSharedFlow<VideoTrack>() | |
override val remoteVideoSinkFlow: SharedFlow<VideoTrack> = _remoteVideoSinkFlow | |
// declaring video constraints and setting OfferToReceiveVideo to true | |
// this step is mandatory to create valid offer and answer | |
private val mediaConstraints = MediaConstraints().apply { | |
mandatory.addAll( | |
listOf( | |
MediaConstraints.KeyValuePair("OfferToReceiveAudio", "true"), | |
MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true") | |
) | |
) | |
} | |
private val peerConnection: StreamPeerConnection by lazy { | |
peerConnectionFactory.makePeerConnection( | |
coroutineScope = sessionManagerScope, | |
configuration = peerConnectionFactory.rtcConfig, | |
type = StreamPeerType.SUBSCRIBER, | |
mediaConstraints = mediaConstraints, | |
onIceCandidateRequest = { iceCandidate, _ -> | |
signalingClient.sendCommand( | |
SignalingCommand.ICE, | |
"${iceCandidate.sdpMid}$ICE_SEPARATOR${iceCandidate.sdpMLineIndex}$ICE_SEPARATOR${iceCandidate.sdp}" | |
) | |
}, | |
onVideoTrack = { rtpTransceiver -> | |
val track = rtpTransceiver?.receiver?.track() ?: return@makePeerConnection | |
if (track.kind() == MediaStreamTrack.VIDEO_TRACK_KIND) { | |
val videoTrack = track as VideoTrack | |
sessionManagerScope.launch { | |
_remoteVideoSinkFlow.emit(videoTrack) | |
} | |
} | |
} | |
) | |
} | |
.. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment