Skip to content

Commit

Permalink
Fixed primarySpeaker videoTrack, mute observer (#98)
Browse files Browse the repository at this point in the history
* Add private keyword

* Set primarySpeaker muteIndicator visibility

* Fixed primarySpeaker videoTrack, mute observer
  • Loading branch information
newjins-papa authored Jun 27, 2022
1 parent 907d9b9 commit 7306069
Showing 1 changed file with 46 additions and 42 deletions.
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 @@ -77,53 +77,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

0 comments on commit 7306069

Please sign in to comment.