-
Notifications
You must be signed in to change notification settings - Fork 178
/
ExampleWebViewSource.swift
220 lines (189 loc) · 9.55 KB
/
ExampleWebViewSource.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
//
// ExampleWebViewSource.swift
// ScreenCapturerExample
//
// Copyright © 2016-2019 Twilio, Inc. All rights reserved.
//
import Accelerate
import TwilioVideo
import WebKit
class ExampleWebViewSource: NSObject {
// TVIVideoSource
public var isScreencast: Bool = true
public weak var sink: VideoSink? = nil
// Private variables
weak var view: WKWebView?
var displayTimer: CADisplayLink?
var willEnterForegroundObserver: NSObjectProtocol?
var didEnterBackgroundObserver: NSObjectProtocol?
// Constants
static let kCaptureFrameRate = 5
static let kCaptureScaleFactor: CGFloat = 1.0
init(aView: WKWebView) {
sink = nil
view = aView
}
func startCapture() {
if (view == nil || view?.superview == nil) {
print("Can't capture from a nil view, or one with no superview:", view as Any)
return
}
print("Start capturing.")
startTimer()
registerNotificationObservers()
}
func stopCapture() {
print("Stop capturing.")
unregisterNotificationObservers()
invalidateTimer()
}
private func startTimer() {
invalidateTimer()
// Use a CADisplayLink timer so that our drawing is synchronized to the display vsync.
displayTimer = CADisplayLink(target: self, selector: #selector(ExampleWebViewSource.captureView))
displayTimer?.preferredFramesPerSecond = ExampleWebViewSource.kCaptureFrameRate
displayTimer?.add(to: RunLoop.main, forMode: RunLoop.Mode.common)
displayTimer?.isPaused = UIApplication.shared.applicationState == UIApplication.State.background
}
private func invalidateTimer() {
displayTimer?.invalidate()
displayTimer = nil
}
private func registerNotificationObservers() {
let notificationCenter = NotificationCenter.default;
willEnterForegroundObserver = notificationCenter.addObserver(forName: UIApplication.willEnterForegroundNotification,
object: nil,
queue: OperationQueue.main,
using: { (Notification) in
self.displayTimer?.isPaused = false;
})
didEnterBackgroundObserver = notificationCenter.addObserver(forName: UIApplication.didEnterBackgroundNotification,
object: nil,
queue: OperationQueue.main,
using: { (Notification) in
self.displayTimer?.isPaused = true;
})
}
private func unregisterNotificationObservers() {
let notificationCenter = NotificationCenter.default
notificationCenter.removeObserver(willEnterForegroundObserver!)
notificationCenter.removeObserver(didEnterBackgroundObserver!)
willEnterForegroundObserver = nil
didEnterBackgroundObserver = nil
}
@objc func captureView( timer: CADisplayLink ) {
guard let webView = self.view else {
return
}
guard let window = webView.window else {
return
}
let configuration = WKSnapshotConfiguration()
// Configure a width (in points) appropriate for our desired scale factor.
configuration.snapshotWidth = NSNumber(value: Double(webView.bounds.width * ExampleWebViewSource.kCaptureScaleFactor / window.screen.scale))
webView.takeSnapshot(with:configuration, completionHandler: { (image, error) in
if let deliverableImage = image {
self.deliverCapturedImage(image: deliverableImage,
orientation: VideoOrientation.up,
timestamp: timer.timestamp)
} else if let theError = error {
print("Snapshot error:", theError as Any)
}
})
}
private func deliverCapturedImage(image: UIImage,
orientation: VideoOrientation,
timestamp: CFTimeInterval) {
/*
* Make a (deep) copy of the UIImage's underlying data. We do this by getting the CGImage, and its CGDataProvider.
* In some cases, the bitmap's pixel format is not compatible with CVPixelBuffer and we need to repack the pixels.
*/
guard let cgImage = image.cgImage else {
return
}
let alphaInfo = cgImage.alphaInfo
let byteOrderInfo = CGBitmapInfo(rawValue: cgImage.bitmapInfo.rawValue & CGBitmapInfo.byteOrderMask.rawValue)
let dataProvider = cgImage.dataProvider
let data = dataProvider?.data
let baseAddress = CFDataGetBytePtr(data!)!
/*
* The underlying data is marked as immutable, but we are the sole owner and can do as we please.
* Also, the CVPixelBuffer constructor will only accept a mutable pointer.
*/
let mutableBaseAddress = UnsafeMutablePointer<UInt8>(mutating: baseAddress)
var pixelFormat = PixelFormat.format32BGRA
var imageBuffer = vImage_Buffer(data: mutableBaseAddress,
height: vImagePixelCount(cgImage.height),
width: vImagePixelCount(cgImage.width),
rowBytes: cgImage.bytesPerRow)
switch byteOrderInfo {
case .byteOrder32Little:
/*
* Pixel format encountered on iOS simulators. Note: We have observed that pre-multiplied images
* do not contain any transparent alpha, but still appear to be too dim. This appears to be a simulator only bug.
* Without proper alpha information it is impossible to un-premultiply the data.
*/
assert(alphaInfo == .premultipliedFirst || alphaInfo == .noneSkipFirst)
case .byteOrder32Big:
// Never encountered with snapshots on iOS, but maybe on macOS?
assert(alphaInfo == .premultipliedFirst || alphaInfo == .noneSkipFirst)
pixelFormat = PixelFormat.format32ARGB
case .byteOrder16Little:
assert(false)
case .byteOrder16Big:
assert(false)
default:
/*
* The pixels are formatted in the default order for CoreGraphics, which on iOS is kCVPixelFormatType_32RGBA.
* This pixel format is defined by Core Video, but creating a buffer returns kCVReturnInvalidPixelFormat on an iOS device.
* We will instead repack the memory from RGBA to BGRA, which is supported by Core Video (and Twilio Video).
* Note: While UIImages captured on a device claim to have pre-multiplied alpha, the alpha channel is always opaque (0xFF).
*/
assert(alphaInfo == .premultipliedLast || alphaInfo == .noneSkipLast)
// Swap the red and blue channels.
var permuteMap = [UInt8(2), UInt8(1), UInt8(0), UInt8(3)]
vImagePermuteChannels_ARGB8888(&imageBuffer,
&imageBuffer,
&permuteMap,
vImage_Flags(kvImageDoNotTile))
}
/*
* We own the copied CFData which will back the CVPixelBuffer, thus the data's lifetime is bound to the buffer.
* We will use a CVPixelBufferReleaseBytesCallback in order to release the CFData when the buffer dies.
*/
let unmanagedData = Unmanaged<CFData>.passRetained(data!)
var pixelBuffer: CVPixelBuffer?
let status = CVPixelBufferCreateWithBytes(nil,
cgImage.width,
cgImage.height,
pixelFormat.rawValue,
mutableBaseAddress,
cgImage.bytesPerRow,
{ releaseContext, baseAddress in
let contextData = Unmanaged<CFData>.fromOpaque(releaseContext!)
contextData.release()
},
unmanagedData.toOpaque(),
nil,
&pixelBuffer)
if let buffer = pixelBuffer {
// Deliver a frame to the consumer.
let frame = VideoFrame(timeInterval: timestamp,
buffer: buffer,
orientation: orientation)
// The consumer retains the CVPixelBuffer and will own it as the buffer flows through the video pipeline.
self.sink?.onVideoFrame(frame!)
} else {
print("Video source failed with status code: \(status).")
}
}
}
extension ExampleWebViewSource: VideoSource {
func requestOutputFormat(_ outputFormat: VideoFormat) {
/*
* This class doesn't explicitly support different scaling factors or frame rates.
* That being said, we won't disallow cropping and/or scaling if its absolutely needed.
*/
self.sink?.onVideoFormatRequest(outputFormat)
}
}