Last active
November 30, 2020 11:57
-
-
Save samyak-jain/4ccc3da5cf07839978daa7098ec428f4 to your computer and use it in GitHub Desktop.
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
mRtcEngine = RtcEngine.create(baseContext, APP_ID, object : IRtcEngineEventHandler() { | |
override fun onUserJoined(uid: Int, elapsed: Int) { | |
// onUserJoined callback is called anytime a new remote user joins the channel | |
super.onUserJoined(uid, elapsed) | |
// We mute the stream by default so that it doesn't consume unnecessary bandwidth | |
mRtcEngine?.muteRemoteVideoStream(uid, true) | |
// We are using a lock since uidList is shared and there can be race conditions | |
lock.lock() | |
try { | |
// We are using uidList to keep track of the UIDs of the remote users | |
uidList.add(uid) | |
} finally { | |
lock.unlock() | |
} | |
// We are using the handler since UI Changes need to be run on the UI Thread | |
handler.post { | |
// When the number of remote users grows to 4, we switch to the lower stream | |
// When this happens, we will now be using a lower resolution and bitrate to save on bandwidth | |
if (uidList.size == 4) { | |
mRtcEngine?.setRemoteDefaultVideoStreamType(Constants.VIDEO_STREAM_LOW) | |
Toast.makeText( | |
applicationContext, | |
"Fallback to Low Video Stream", | |
Toast.LENGTH_LONG | |
).show() | |
} | |
// We notify our RecyclerView adapter that a new video stream is added | |
remoteViewAdapter.notifyItemInserted(uidList.size - 1) | |
} | |
} | |
override fun onUserOffline(uid: Int, reason: Int) { | |
// onUserOffline is called whenever a remote user leaves the channel | |
super.onUserOffline(uid, reason) | |
// We use toRemove to inform the RecyclerView of the index of item we are removing | |
val toRemove: Int | |
// We are using a lock since uidList is shared and there can be race conditions | |
lock.lock() | |
try { | |
// We are fetching the index of the item we are about to remove and then remove the item | |
toRemove = uidList.indexOf(uid) | |
uidList.remove(uid) | |
} finally { | |
lock.unlock() | |
} | |
// We are using the handler since UI Changes need to be run on the UI Thread | |
handler.post { | |
// We are using this to remove the remote video from being rendered on the SurfaceView | |
mRtcEngine?.setupRemoteVideo(VideoCanvas(null, VideoCanvas.RENDER_MODE_HIDDEN, uid)) | |
// When the number of remote users shrinks to 3, we switch back to the higher stream | |
// When we have <= 3 remote users, the bandwidth savings are no longer necessary | |
if (uidList.size == 3) { | |
mRtcEngine?.setRemoteDefaultVideoStreamType(Constants.VIDEO_STREAM_HIGH) | |
Toast.makeText( | |
applicationContext, | |
"Go back to High Video Stream", | |
Toast.LENGTH_LONG | |
).show() | |
} | |
// We notify our RecyclerView adapter that the video stream can be removed | |
remoteViewAdapter.notifyItemRemoved(toRemove) | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment