forked from raj-ship-it/Orate-for-ineuron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActiveSpeakerFragment.kt
227 lines (193 loc) · 7.15 KB
/
ActiveSpeakerFragment.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package live.hms.app2.ui.meeting.activespeaker
import android.graphics.Bitmap
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import live.hms.app2.R
import live.hms.app2.databinding.FragmentActiveSpeakerBinding
import live.hms.app2.ui.meeting.CustomPeerMetadata
import live.hms.app2.ui.meeting.MeetingTrack
import live.hms.app2.ui.meeting.commons.VideoGridBaseFragment
import live.hms.app2.ui.meeting.pinnedvideo.StatsInterpreter
import live.hms.app2.util.*
import live.hms.video.media.tracks.HMSVideoTrack
import live.hms.video.sdk.models.enums.HMSPeerUpdate
import live.hms.video.utils.HMSLogger
import org.webrtc.EglRenderer
import org.webrtc.RendererCommon
import org.webrtc.SurfaceViewRenderer
class ActiveSpeakerFragment : VideoGridBaseFragment() {
companion object {
private const val TAG = "ActiveSpeakerFragment"
}
private var binding by viewLifecycle<FragmentActiveSpeakerBinding>()
private var screenShareTrack: MeetingTrack? = null
private var wasLastModePip = false
private val mediaPlayerManager by lazy { MediaPlayerManager(lifecycle) }
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentActiveSpeakerBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initViewModels()
}
private fun unBindScreenShareTrack() {
screenShareTrack?.let {
unbindSurfaceView(binding.screenShare, it)
}
}
override fun onResume() {
if (wasLastModePip) {
//if it's coming back from pip --> full screen i.e [pause --> resume] we wont' bind the screenshare track again since we never removed it in the first place
super.onResume()
wasLastModePip = false
screenShareOverLocalVideoInGrid()
return
}
screenShareTrack?.let {
screenShareStats.initiateStats(
this,
meetingViewModel.getStats(),
it.video,
it.audio, it.peer.isLocal
) { statsString ->
meetingViewModel?.updateTrackStatus(statsString)
}
binding.screenShare.raisedHand.alpha = visibilityOpacity(CustomPeerMetadata.fromJson(it.peer.metadata)?.isHandRaised == true)
bindSurfaceView(binding.screenShare, it, RendererCommon.ScalingType.SCALE_ASPECT_FIT)
binding.screenShare.surfaceView.setOnLongClickListener { view ->
openDialog(view as? SurfaceViewRenderer, it.video, it.peer.name.orEmpty())
return@setOnLongClickListener true
}
}
super.onResume()
}
private fun openDialog(
surfaceView: SurfaceViewRenderer?,
videoTrack: HMSVideoTrack?,
peerName: String
) {
if (videoTrack.isValid().not())
return
contextSafe { context, activity ->
context.showTileListDialog (peerName){ captureVideoFrame(surfaceView, videoTrack) }
}
}
private fun captureVideoFrame(surfaceView: SurfaceViewRenderer?, videoTrack: HMSVideoTrack?) {
//safe check incase video
if (videoTrack.isValid().not()){
return
}
contextSafe { context, activity -> mediaPlayerManager.startPlay(R.raw.camera_shut, context )}
surfaceView?.vibrateStrong()
surfaceView?.onBitMap(onBitmap = { bitmap ->
contextSafe { context, activity ->
//stores the bitmap in local cache thus avoiding any permission
val uri = bitmap?.saveCaptureToLocalCache(context)
//the uri is used to open share intent
uri?.let { activity.openShareIntent(it) }
}
})
}
override fun onPause() {
if (activity?.isInPictureInPictureMode == true) {
//when it's pip mode don't unbind views
wasLastModePip = true
screenShareOverLocalVideoInGrid()
} else {
unBindScreenShareTrack()
}
// screenShareStats.close()
super.onPause()
}
private fun screenShareOverLocalVideoInGrid() {
//hide video grid when screen share is shown!
binding.container.visibility = if (screenShareTrack != null && activity?.isInPictureInPictureMode == true) {
View.GONE
} else {
View.VISIBLE
}
}
override fun initViewModels() {
super.initViewModels()
meetingViewModel.tracks.observe(viewLifecycleOwner) { tracks ->
HMSLogger.v(TAG, "tracks update received 🎼 [size=${tracks.size}]")
updateScreenshareTracks(tracks)
}
meetingViewModel.activeSpeakersUpdatedTracks.observe(viewLifecycleOwner) { tracks ->
HMSLogger.v(TAG, "tracks update received 🎼 [size=${tracks.size}]")
updateVideos(binding.container, tracks, false)
}
meetingViewModel.activeSpeakers.observe(viewLifecycleOwner) { (videos, speakers) ->
updateVideos(binding.container, videos, false)
// Active speaker should be updated via, tracks AND actual active speakers.
applySpeakerUpdates(speakers)
}
meetingViewModel.peerMetadataNameUpdate.observe(viewLifecycleOwner) {
if( screenShareTrack?.peer?.peerID == it.first.peerID) {
when(it.second) {
HMSPeerUpdate.METADATA_CHANGED -> {
binding.screenShare.raisedHand.alpha = visibilityOpacity(CustomPeerMetadata.fromJson(it.first.metadata)?.isHandRaised == true)
}
HMSPeerUpdate.NAME_CHANGED -> {
binding.screenShare.name.text = it.first.name
}
else -> {}
}
}
}
meetingViewModel.trackStatus.observe(viewLifecycleOwner) { statsString ->
binding.screenShare.statsView.text = statsString
}
}
private val screenShareStats by lazy { StatsInterpreter(settings.showStats) }
private fun updateScreenshareTracks(tracks: List<MeetingTrack>) {
// Check if the currently shared screen-share track is removed
screenShareTrack?.let { screen ->
if (!tracks.contains(screen)) {
screenShareTrack?.let { unbindSurfaceView(binding.screenShare, it) }
// screenShareStats.close()
screenShareTrack = null
screenShareOverLocalVideoInGrid()
}
}
// Check for screen share
if (screenShareTrack == null) tracks.find { it.isScreen }?.let { screen ->
screenShareStats.initiateStats(
viewLifecycleOwner,
meetingViewModel.getStats(),
screen.video,
screen.audio,
screen.peer.isLocal
) {
binding.screenShare.statsView.text = it
}
screenShareTrack = screen
screenShareOverLocalVideoInGrid()
if (isFragmentVisible) {
bindSurfaceView(
binding.screenShare,
screen,
RendererCommon.ScalingType.SCALE_ASPECT_FIT
)
}
bindVideo(binding.screenShare, screen)
binding.screenShare.apply {
iconAudioOff.visibility = View.GONE
iconScreenShare.visibility = View.GONE
audioLevel.visibility = View.GONE
raisedHand.alpha = visibilityOpacity(CustomPeerMetadata.fromJson(screen.peer.metadata)?.isHandRaised == true)
}
binding.screenShareContainer.visibility = View.VISIBLE
}
if (screenShareTrack == null && binding.screenShareContainer.visibility != View.GONE) {
binding.screenShareContainer.visibility = View.GONE
}
}
}