Seong-eun Jin
Committed by GitHub

Fixed primarySpeaker videoTrack, mute observer (#98)

* Add private keyword

* Set primarySpeaker muteIndicator visibility

* Fixed primarySpeaker videoTrack, mute observer
@@ -77,53 +77,57 @@ class CallActivity : AppCompatActivity() { @@ -77,53 +77,57 @@ class CallActivity : AppCompatActivity() {
77 // Observe primary speaker changes 77 // Observe primary speaker changes
78 emitAll(viewModel.primarySpeaker) 78 emitAll(viewModel.primarySpeaker)
79 }.flatMapLatest { primarySpeaker -> 79 }.flatMapLatest { primarySpeaker ->
80 - // Update new primary speaker identity  
81 - binding.identityText.text = primarySpeaker?.identity  
82 -  
83 if (primarySpeaker != null) { 80 if (primarySpeaker != null) {
84 - primarySpeaker::audioTracks.flow  
85 - .flatMapLatest { tracks ->  
86 - val audioTrack = tracks.firstOrNull()?.first  
87 - if (audioTrack != null) {  
88 - audioTrack::muted.flow  
89 - } else {  
90 - flowOf(true)  
91 - }  
92 - }  
93 - .collect { muted ->  
94 - binding.muteIndicator.visibility = if (muted) View.VISIBLE else View.INVISIBLE  
95 - }  
96 - }  
97 -  
98 - // observe videoTracks changes.  
99 - if (primarySpeaker != null) {  
100 - primarySpeaker::videoTracks.flow  
101 - .map { primarySpeaker to it } 81 + flowOf(primarySpeaker)
102 } else { 82 } else {
103 emptyFlow() 83 emptyFlow()
104 } 84 }
105 - }.flatMapLatest { (participant, videoTracks) ->  
106 -  
107 - // Prioritize any screenshare streams.  
108 - val trackPublication = participant.getTrackPublication(Track.Source.SCREEN_SHARE)  
109 - ?: participant.getTrackPublication(Track.Source.CAMERA)  
110 - ?: videoTracks.firstOrNull()?.first  
111 - ?: return@flatMapLatest emptyFlow()  
112 -  
113 - trackPublication::track.flow  
114 - }.collect { videoTrack ->  
115 - // Cleanup old video track  
116 - val oldVideoTrack = binding.speakerVideoView.tag as? VideoTrack  
117 - oldVideoTrack?.removeRenderer(binding.speakerVideoView)  
118 -  
119 - // Bind new video track to video view.  
120 - if (videoTrack is VideoTrack) {  
121 - videoTrack.addRenderer(binding.speakerVideoView)  
122 - binding.speakerVideoView.visibility = View.VISIBLE  
123 - } else {  
124 - binding.speakerVideoView.visibility = View.INVISIBLE 85 + }.collect { participant ->
  86 + // Update new primary speaker identity
  87 + binding.identityText.text = participant.identity
  88 +
  89 + // observe videoTracks changes.
  90 + val videoTrackFlow = participant::videoTracks.flow
  91 + .map { participant to it }
  92 + .flatMapLatest { (participant, videoTracks) ->
  93 + // Prioritize any screenshare streams.
  94 + val trackPublication = participant.getTrackPublication(Track.Source.SCREEN_SHARE)
  95 + ?: participant.getTrackPublication(Track.Source.CAMERA)
  96 + ?: videoTracks.firstOrNull()?.first
  97 + ?: return@flatMapLatest emptyFlow()
  98 +
  99 + trackPublication::track.flow
  100 + }
  101 +
  102 + // observe audioTracks changes.
  103 + val mutedFlow = participant::audioTracks.flow
  104 + .flatMapLatest { tracks ->
  105 + val audioTrack = tracks.firstOrNull()?.first
  106 + if (audioTrack != null) {
  107 + audioTrack::muted.flow
  108 + } else {
  109 + flowOf(true)
  110 + }
  111 + }
  112 +
  113 + combine(videoTrackFlow, mutedFlow) { videoTrack, muted ->
  114 + videoTrack to muted
  115 + }.collect { (videoTrack, muted) ->
  116 + // Cleanup old video track
  117 + val oldVideoTrack = binding.speakerVideoView.tag as? VideoTrack
  118 + oldVideoTrack?.removeRenderer(binding.speakerVideoView)
  119 +
  120 + // Bind new video track to video view.
  121 + if (videoTrack is VideoTrack) {
  122 + videoTrack.addRenderer(binding.speakerVideoView)
  123 + binding.speakerVideoView.visibility = View.VISIBLE
  124 + } else {
  125 + binding.speakerVideoView.visibility = View.INVISIBLE
  126 + }
  127 + binding.speakerVideoView.tag = videoTrack
  128 +
  129 + binding.muteIndicator.visibility = if (muted) View.VISIBLE else View.INVISIBLE
125 } 130 }
126 - binding.speakerVideoView.tag = videoTrack  
127 } 131 }
128 } 132 }
129 133