Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed primarySpeaker videoTrack, mute observer #98

Merged
merged 5 commits into from
Jun 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 46 additions & 42 deletions sample-app/src/main/java/io/livekit/android/sample/CallActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,53 +84,57 @@ class CallActivity : AppCompatActivity() {
// Observe primary speaker changes
emitAll(viewModel.primarySpeaker)
}.flatMapLatest { primarySpeaker ->
// Update new primary speaker identity
binding.identityText.text = primarySpeaker?.identity

if (primarySpeaker != null) {
primarySpeaker::audioTracks.flow
.flatMapLatest { tracks ->
val audioTrack = tracks.firstOrNull()?.first
if (audioTrack != null) {
audioTrack::muted.flow
} else {
flowOf(true)
}
}
.collect { muted ->
binding.muteIndicator.visibility = if (muted) View.VISIBLE else View.INVISIBLE
}
}

// observe videoTracks changes.
if (primarySpeaker != null) {
primarySpeaker::videoTracks.flow
.map { primarySpeaker to it }
flowOf(primarySpeaker)
} else {
emptyFlow()
}
}.flatMapLatest { (participant, videoTracks) ->

// Prioritize any screenshare streams.
val trackPublication = participant.getTrackPublication(Track.Source.SCREEN_SHARE)
?: participant.getTrackPublication(Track.Source.CAMERA)
?: videoTracks.firstOrNull()?.first
?: return@flatMapLatest emptyFlow()

trackPublication::track.flow
}.collect { videoTrack ->
// Cleanup old video track
val oldVideoTrack = binding.speakerVideoView.tag as? VideoTrack
oldVideoTrack?.removeRenderer(binding.speakerVideoView)

// Bind new video track to video view.
if (videoTrack is VideoTrack) {
videoTrack.addRenderer(binding.speakerVideoView)
binding.speakerVideoView.visibility = View.VISIBLE
} else {
binding.speakerVideoView.visibility = View.INVISIBLE
}.collect { participant ->
// Update new primary speaker identity
binding.identityText.text = participant.identity

// observe videoTracks changes.
val videoTrackFlow = participant::videoTracks.flow
.map { participant to it }
.flatMapLatest { (participant, videoTracks) ->
// Prioritize any screenshare streams.
val trackPublication = participant.getTrackPublication(Track.Source.SCREEN_SHARE)
?: participant.getTrackPublication(Track.Source.CAMERA)
?: videoTracks.firstOrNull()?.first
?: return@flatMapLatest emptyFlow()

trackPublication::track.flow
}

// observe audioTracks changes.
val mutedFlow = participant::audioTracks.flow
.flatMapLatest { tracks ->
val audioTrack = tracks.firstOrNull()?.first
if (audioTrack != null) {
audioTrack::muted.flow
} else {
flowOf(true)
}
}

combine(videoTrackFlow, mutedFlow) { videoTrack, muted ->
videoTrack to muted
}.collect { (videoTrack, muted) ->
// Cleanup old video track
val oldVideoTrack = binding.speakerVideoView.tag as? VideoTrack
oldVideoTrack?.removeRenderer(binding.speakerVideoView)

// Bind new video track to video view.
if (videoTrack is VideoTrack) {
videoTrack.addRenderer(binding.speakerVideoView)
binding.speakerVideoView.visibility = View.VISIBLE
} else {
binding.speakerVideoView.visibility = View.INVISIBLE
}
binding.speakerVideoView.tag = videoTrack

binding.muteIndicator.visibility = if (muted) View.VISIBLE else View.INVISIBLE
}
binding.speakerVideoView.tag = videoTrack
}
}

Expand Down