-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathGroupCallRemoteVideoManager.swift
223 lines (181 loc) · 7.69 KB
/
GroupCallRemoteVideoManager.swift
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
//
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import SignalRingRTC
import SignalServiceKit
import WebRTC
@MainActor
class GroupCallRemoteVideoManager {
private let callServiceState: CallServiceState
init(callServiceState: CallServiceState) {
self.callServiceState = callServiceState
}
private var currentRingRtcCall: SignalRingRTC.GroupCall? {
switch callServiceState.currentCall?.mode {
case nil:
return nil
case .individual:
return nil
case .groupThread(let call as GroupCall), .callLink(let call as GroupCall):
return call.ringRtcCall
}
}
// MARK: - Remote Video Views
private var videoViews = [UInt32: [CallMemberVisualContext: GroupCallRemoteVideoView]]()
func remoteVideoView(for device: RemoteDeviceState, context: CallMemberVisualContext) -> GroupCallRemoteVideoView {
var currentVideoViewsDevice = videoViews[device.demuxId] ?? [:]
if let current = currentVideoViewsDevice[context] { return current }
let videoView = GroupCallRemoteVideoView(demuxId: device.demuxId)
videoView.sizeDelegate = self
videoView.isGroupCall = true
if context == .speaker { videoView.isFullScreen = true }
currentVideoViewsDevice[context] = videoView
videoViews[device.demuxId] = currentVideoViewsDevice
return videoView
}
private func destroyRemoteVideoView(for demuxId: UInt32) {
videoViews[demuxId]?.forEach { $0.value.removeFromSuperview() }
videoViews[demuxId] = nil
}
private var updateVideoRequestsDebounceTimer: Timer?
private func updateVideoRequests() {
updateVideoRequestsDebounceTimer?.invalidate()
updateVideoRequestsDebounceTimer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: false, block: { [weak self] _ in
guard let self = self else { return }
// scheduledTimer promises to schedule this block on the same run loop that called scheduled timer so we should be main actor here
MainActor.assumeIsolated {
guard let groupCall = self.currentRingRtcCall else { return }
var activeSpeakerHeight: UInt16 = 0
let videoRequests: [VideoRequest] = groupCall.remoteDeviceStates.map { demuxId, _ in
guard
let renderingVideoViews = self.videoViews[demuxId]?.filter({ $0.value.isRenderingVideo }),
!renderingVideoViews.isEmpty
else {
return VideoRequest(demuxId: demuxId, width: 0, height: 0, framerate: nil)
}
if let activeSpeakerVideoView = renderingVideoViews[.speaker] {
activeSpeakerHeight = max(activeSpeakerHeight, UInt16(activeSpeakerVideoView.currentSize.height))
}
let size = renderingVideoViews.reduce(CGSize.zero) { partialResult, element in
partialResult.max(element.value.currentSize)
}
return VideoRequest(
demuxId: demuxId,
width: UInt16(size.width),
height: UInt16(size.height),
framerate: size.height <= GroupCallVideoOverflow.itemHeight ? 15 : 30
)
}
groupCall.updateVideoRequests(resolutions: videoRequests, activeSpeakerHeight: activeSpeakerHeight)
}
})
}
}
extension GroupCallRemoteVideoManager: GroupCallRemoteVideoViewSizeDelegate {
@MainActor
func groupCallRemoteVideoViewDidChangeSize(remoteVideoView: GroupCallRemoteVideoView) {
updateVideoRequests()
}
@MainActor
func groupCallRemoteVideoViewDidChangeSuperview(remoteVideoView: GroupCallRemoteVideoView) {
guard let device = currentRingRtcCall?.remoteDeviceStates[remoteVideoView.demuxId] else { return }
remoteVideoView.configure(for: device)
updateVideoRequests()
}
}
extension GroupCallRemoteVideoManager: CallServiceStateObserver {
func didUpdateCall(from oldValue: SignalCall?, to newValue: SignalCall?) {
videoViews.forEach { self.destroyRemoteVideoView(for: $0.key) }
switch oldValue?.mode {
case nil, .individual:
break
case .groupThread(let call as GroupCall), .callLink(let call as GroupCall):
call.removeObserver(self)
}
switch newValue?.mode {
case nil, .individual:
break
case .groupThread(let call as GroupCall), .callLink(let call as GroupCall):
call.addObserver(self, syncStateImmediately: true)
}
}
}
extension GroupCallRemoteVideoManager: GroupCallObserver {
func groupCallRemoteDeviceStatesChanged(_ call: GroupCall) {
for (demuxId, videoViews) in videoViews {
guard let device = call.ringRtcCall.remoteDeviceStates[demuxId] else {
destroyRemoteVideoView(for: demuxId)
continue
}
videoViews.values.forEach { $0.configure(for: device) }
}
}
func groupCallEnded(_ call: GroupCall, reason: GroupCallEndReason) {
videoViews.keys.forEach { destroyRemoteVideoView(for: $0) }
}
}
private protocol GroupCallRemoteVideoViewSizeDelegate: AnyObject {
@MainActor
func groupCallRemoteVideoViewDidChangeSize(remoteVideoView: GroupCallRemoteVideoView)
@MainActor
func groupCallRemoteVideoViewDidChangeSuperview(remoteVideoView: GroupCallRemoteVideoView)
}
class GroupCallRemoteVideoView: UIView {
fileprivate weak var sizeDelegate: GroupCallRemoteVideoViewSizeDelegate?
fileprivate private(set) var currentSize: CGSize = .zero {
didSet {
guard oldValue != currentSize else { return }
remoteVideoView.frame = CGRect(origin: .zero, size: currentSize)
sizeDelegate?.groupCallRemoteVideoViewDidChangeSize(remoteVideoView: self)
}
}
// We cannot subclass this, for some unknown reason WebRTC
// will not render frames properly if we try to.
private let remoteVideoView = RemoteVideoView()
private weak var videoTrack: RTCVideoTrack? {
didSet {
guard oldValue != videoTrack else { return }
oldValue?.remove(remoteVideoView)
videoTrack?.add(remoteVideoView)
}
}
override var frame: CGRect {
didSet { currentSize = frame.size }
}
override var bounds: CGRect {
didSet { currentSize = bounds.size }
}
override func didMoveToSuperview() {
sizeDelegate?.groupCallRemoteVideoViewDidChangeSuperview(remoteVideoView: self)
}
var isGroupCall: Bool {
get { remoteVideoView.isGroupCall }
set { remoteVideoView.isGroupCall = newValue }
}
var isFullScreen: Bool {
get { remoteVideoView.isFullScreen }
set { remoteVideoView.isFullScreen = newValue }
}
var isScreenShare: Bool {
get { remoteVideoView.isScreenShare }
set { remoteVideoView.isScreenShare = newValue }
}
var isRenderingVideo: Bool { videoTrack != nil }
fileprivate let demuxId: UInt32
fileprivate init(demuxId: UInt32) {
self.demuxId = demuxId
super.init(frame: .zero)
addSubview(remoteVideoView)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit { videoTrack = nil }
func configure(for device: RemoteDeviceState) {
guard device.demuxId == demuxId else {
return owsFailDebug("Tried to configure with incorrect device")
}
videoTrack = superview == nil ? nil : device.videoTrack
}
}