-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
PluginMediaStreamRenderer.swift
341 lines (277 loc) · 11 KB
/
PluginMediaStreamRenderer.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import Foundation
import AVFoundation
class PluginMediaStreamRenderer : NSObject, RTCEAGLVideoViewDelegate {
var id: String
var eventListener: (_ data: NSDictionary) -> Void
var closed: Bool
var webView: UIView
var elementView: UIView
var pluginMediaStream: PluginMediaStream?
var videoView: RTCEAGLVideoView
var rtcAudioTrack: RTCAudioTrack?
var rtcVideoTrack: RTCVideoTrack?
init(
webView: UIView,
eventListener: @escaping (_ data: NSDictionary) -> Void
) {
NSLog("PluginMediaStreamRenderer#init()")
// Open Renderer
self.id = UUID().uuidString;
self.closed = false
// The browser HTML view.
self.webView = webView
self.eventListener = eventListener
// The video element view.
self.elementView = UIView()
// The effective video view in which the the video stream is shown.
// It's placed over the elementView.
self.videoView = RTCEAGLVideoView()
self.videoView.isUserInteractionEnabled = false
self.elementView.isUserInteractionEnabled = false
self.elementView.isHidden = true
self.elementView.backgroundColor = UIColor.black
self.elementView.addSubview(self.videoView)
self.elementView.layer.masksToBounds = true
// Place the video element view inside the WebView's superview
self.webView.addSubview(self.elementView)
self.webView.isOpaque = false
self.webView.backgroundColor = UIColor.clear
// https://stackoverflow.com/questions/46317061/use-safe-area-layout-programmatically
// https://developer.apple.com/documentation/uikit/uiview/2891102-safearealayoutguide
// https://developer.apple.com/documentation/uikit/
let view = self.elementView;
if #available(iOS 11.0, *) {
let guide = webView.safeAreaLayoutGuide;
view.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
view.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
view.leftAnchor.constraint(equalTo: guide.leftAnchor).isActive = true
view.rightAnchor.constraint(equalTo: guide.rightAnchor).isActive = true
} else {
NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: webView, attribute: .top, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: webView, attribute: .bottom, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: view, attribute: .leading, relatedBy: .equal, toItem: webView, attribute: .leading, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: webView, attribute: .trailing, multiplier: 1.0, constant: 0).isActive = true
}
}
deinit {
NSLog("PluginMediaStreamRenderer#deinit()")
}
func run() {
NSLog("PluginMediaStreamRenderer#run()")
self.videoView.delegate = self
}
func render(_ pluginMediaStream: PluginMediaStream) {
NSLog("PluginMediaStreamRenderer#render()")
if self.pluginMediaStream != nil {
self.reset()
}
self.pluginMediaStream = pluginMediaStream
// Take the first audio track.
for (_, track) in pluginMediaStream.audioTracks {
self.rtcAudioTrack = track.rtcMediaStreamTrack as? RTCAudioTrack
break
}
// Take the first video track.
var pluginVideoTrack: PluginMediaStreamTrack?
for (_, track) in pluginMediaStream.videoTracks {
pluginVideoTrack = track
self.rtcVideoTrack = track.rtcMediaStreamTrack as? RTCVideoTrack
break
}
if self.rtcVideoTrack != nil {
self.rtcVideoTrack!.add(self.videoView)
pluginVideoTrack?.registerRender(render: self)
}
}
func mediaStreamChanged() {
NSLog("PluginMediaStreamRenderer#mediaStreamChanged()")
if self.pluginMediaStream == nil {
return
}
let oldRtcVideoTrack: RTCVideoTrack? = self.rtcVideoTrack
self.rtcAudioTrack = nil
self.rtcVideoTrack = nil
// Take the first audio track.
for (_, track) in self.pluginMediaStream!.audioTracks {
self.rtcAudioTrack = track.rtcMediaStreamTrack as? RTCAudioTrack
break
}
// Take the first video track.
for (_, track) in pluginMediaStream!.videoTracks {
self.rtcVideoTrack = track.rtcMediaStreamTrack as? RTCVideoTrack
break
}
// If same video track as before do nothing.
if oldRtcVideoTrack != nil && self.rtcVideoTrack != nil &&
oldRtcVideoTrack!.trackId == self.rtcVideoTrack!.trackId {
NSLog("PluginMediaStreamRenderer#mediaStreamChanged() | same video track as before")
}
// Different video track.
else if oldRtcVideoTrack != nil && self.rtcVideoTrack != nil &&
oldRtcVideoTrack!.trackId != self.rtcVideoTrack!.trackId {
NSLog("PluginMediaStreamRenderer#mediaStreamChanged() | has a new video track")
oldRtcVideoTrack!.remove(self.videoView)
self.rtcVideoTrack!.add(self.videoView)
}
// Did not have video but now it has.
else if oldRtcVideoTrack == nil && self.rtcVideoTrack != nil {
NSLog("PluginMediaStreamRenderer#mediaStreamChanged() | video track added")
self.rtcVideoTrack!.add(self.videoView)
}
// Had video but now it has not.
else if oldRtcVideoTrack != nil && self.rtcVideoTrack == nil {
NSLog("PluginMediaStreamRenderer#mediaStreamChanged() | video track removed")
oldRtcVideoTrack!.remove(self.videoView)
}
}
func refresh(_ data: NSDictionary) {
let elementLeft = data.object(forKey: "elementLeft") as? Double ?? 0
let elementTop = data.object(forKey: "elementTop") as? Double ?? 0
let elementWidth = data.object(forKey: "elementWidth") as? Double ?? 0
let elementHeight = data.object(forKey: "elementHeight") as? Double ?? 0
var videoViewWidth = data.object(forKey: "videoViewWidth") as? Double ?? 0
var videoViewHeight = data.object(forKey: "videoViewHeight") as? Double ?? 0
let visible = data.object(forKey: "visible") as? Bool ?? true
let opacity = data.object(forKey: "opacity") as? Double ?? 1
let zIndex = data.object(forKey: "zIndex") as? Double ?? 0
let mirrored = data.object(forKey: "mirrored") as? Bool ?? false
let clip = data.object(forKey: "clip") as? Bool ?? true
let borderRadius = data.object(forKey: "borderRadius") as? Double ?? 0
let backgroundColor = data.object(forKey: "backgroundColor") as? String ?? "0,0,0"
NSLog("PluginMediaStreamRenderer#refresh() [elementLeft:%@, elementTop:%@, elementWidth:%@, elementHeight:%@, videoViewWidth:%@, videoViewHeight:%@, visible:%@, opacity:%@, zIndex:%@, mirrored:%@, clip:%@, borderRadius:%@]",
String(elementLeft), String(elementTop), String(elementWidth), String(elementHeight),
String(videoViewWidth), String(videoViewHeight), String(visible), String(opacity), String(zIndex),
String(mirrored), String(clip), String(borderRadius))
let videoViewLeft: Double = (elementWidth - videoViewWidth) / 2
let videoViewTop: Double = (elementHeight - videoViewHeight) / 2
self.elementView.frame = CGRect(
x: CGFloat(elementLeft),
y: CGFloat(elementTop),
width: CGFloat(elementWidth),
height: CGFloat(elementHeight)
)
// NOTE: Avoid a zero-size UIView for the video (the library complains).
if videoViewWidth == 0 || videoViewHeight == 0 {
videoViewWidth = 1
videoViewHeight = 1
self.videoView.isHidden = true
} else {
self.videoView.isHidden = false
}
self.videoView.frame = CGRect(
x: CGFloat(videoViewLeft),
y: CGFloat(videoViewTop),
width: CGFloat(videoViewWidth),
height: CGFloat(videoViewHeight)
)
if visible {
self.elementView.isHidden = false
} else {
self.elementView.isHidden = true
}
self.elementView.alpha = CGFloat(opacity)
self.elementView.layer.zPosition = CGFloat(zIndex)
// if the zIndex is 0 (the default) bring the view to the top, last one wins
if zIndex == 0 {
self.webView.bringSubviewToFront(self.elementView)
//self.webView?.bringSubview(toFront: self.elementView)
}
if !mirrored {
self.elementView.transform = CGAffineTransform.identity
} else {
self.elementView.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
}
if clip {
self.elementView.clipsToBounds = true
} else {
self.elementView.clipsToBounds = false
}
self.elementView.layer.cornerRadius = CGFloat(borderRadius)
let rgb = backgroundColor.components(separatedBy: ",").map{ CGFloat(($0 as NSString).floatValue) / 256.0 }
let color = UIColor(red: rgb[0], green: rgb[1], blue: rgb[2], alpha: 1)
self.elementView.backgroundColor = color
}
func save() -> String {
NSLog("PluginMediaStreamRenderer#save()")
UIGraphicsBeginImageContextWithOptions(videoView.bounds.size, videoView.isOpaque, 0.0)
videoView.drawHierarchy(in: videoView.bounds, afterScreenUpdates: false)
let snapshotImageFromMyView = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let imageData = snapshotImageFromMyView?.jpegData(compressionQuality: 1.0)
let strBase64 = imageData?.base64EncodedString(options: .lineLength64Characters)
return strBase64!;
}
func stop() {
NSLog("PluginMediaStreamRenderer | video stop")
self.eventListener([
"type": "videostop"
])
}
func close() {
NSLog("PluginMediaStreamRenderer#close()")
self.closed = true
self.reset()
self.elementView.removeFromSuperview()
}
/**
* Private API.
*/
fileprivate func reset() {
NSLog("PluginMediaStreamRenderer#reset()")
if self.rtcVideoTrack != nil {
self.rtcVideoTrack!.remove(self.videoView)
}
self.pluginMediaStream = nil
self.rtcAudioTrack = nil
self.rtcVideoTrack = nil
}
/**
* Methods inherited from RTCEAGLVideoViewDelegate.
*/
func videoView(_ videoView: RTCVideoRenderer, didChangeVideoSize size: CGSize) {
NSLog("PluginMediaStreamRenderer | video size changed [width:%@, height:%@]",
String(describing: size.width), String(describing: size.height))
self.eventListener([
"type": "videoresize",
"size": [
"width": Int(size.width),
"height": Int(size.height)
]
])
}
func videoView(_ videoView: RTCVideoRenderer, didChange frame: RTCVideoFrame?) {
// TODO save from frame buffer instead of renderer
/*
let i420: RTCI420BufferProtocol = frame!.buffer.toI420()
let YPtr: UnsafePointer<UInt8> = i420.dataY
let UPtr: UnsafePointer<UInt8> = i420.dataU
let VPtr: UnsafePointer<UInt8> = i420.dataV
let YSize: Int = Int(frame!.width * frame!.height)
let USize: Int = Int(YSize / 4)
let VSize: Int = Int(YSize / 4)
var frameSize:Int32 = Int32(YSize + USize + VSize)
var width: Int16 = Int16(frame!.width)
var height: Int16 = Int16(frame!.height)
var rotation: Int16 = Int16(frame!.rotation.rawValue)
var timestamp: Int32 = Int32(frame!.timeStamp)
// head + body
// head: type(2B)+len(4B)+width(2B)+height(2B)+rotation(2B)+timestamp(4B)
// body: data(len)
let headSize:Int32 = 16
let dataSize:Int32 = headSize + frameSize
let pduData: NSMutableData? = NSMutableData(length: Int(dataSize))
let headPtr = pduData!.mutableBytes
var pduType:UInt16 = 0x2401
memcpy(headPtr, &pduType, 2)
memcpy(headPtr+2, &frameSize, 4)
memcpy(headPtr+2+4, &width, 2)
memcpy(headPtr+2+4+2, &height, 2)
memcpy(headPtr+2+4+2+2, &rotation, 2)
//memcpy(headPtr+2+4+2+2+2, ×tamp, 4)
let bodyPtr = pduData!.mutableBytes + Int(headSize)
memcpy(bodyPtr, YPtr, YSize)
memcpy(bodyPtr + YSize, UPtr, USize);
memcpy(bodyPtr + YSize + USize, VPtr, VSize);
*/
}
}