Skip to content

Instantly share code, notes, and snippets.

@skydoves
Last active January 27, 2023 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skydoves/4e95ef22e8d9c74ed710e2278af2c7b6 to your computer and use it in GitHub Desktop.
Save skydoves/4e95ef22e8d9c74ed710e2278af2c7b6 to your computer and use it in GitHub Desktop.
WebRtcSessionManagerImpl
// 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