/
Camera.swift
executable file
·254 lines (214 loc) · 11.5 KB
/
Camera.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
import Foundation
import AVFoundation
let initialBenchmarkFramesToIgnore = 5
public class Camera: NSObject, ImageSource, AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureAudioDataOutputSampleBufferDelegate {
public var orientation:ImageOrientation
public var runBenchmark:Bool = false
public var logFPS:Bool = false
public var audioEncodingTarget:AudioEncodingTarget? {
didSet {
guard let audioEncodingTarget = audioEncodingTarget else {
self.removeAudioInputsAndOutputs()
return
}
do {
try self.addAudioInputsAndOutputs()
audioEncodingTarget.activateAudioTrack()
} catch {
fatalError("ERROR: Could not connect audio target with error: \(error)")
}
}
}
public let targets = TargetContainer()
let captureSession:AVCaptureSession
public let inputCamera:AVCaptureDevice
let videoInput:AVCaptureDeviceInput!
let videoOutput:AVCaptureVideoDataOutput!
var microphone:AVCaptureDevice?
var audioInput:AVCaptureDeviceInput?
var audioOutput:AVCaptureAudioDataOutput?
var supportsFullYUVRange:Bool = false
let captureAsYUV:Bool
let yuvConversionShader:ShaderProgram?
let frameRenderingSemaphore = DispatchSemaphore(value:1)
let cameraProcessingQueue = standardProcessingQueue
let audioProcessingQueue = lowProcessingQueue
var numberOfFramesCaptured = 0
var totalFrameTimeDuringCapture:Double = 0.0
var framesSinceLastCheck = 0
var lastCheckTime = CFAbsoluteTimeGetCurrent()
public init(sessionPreset:AVCaptureSession.Preset, cameraDevice:AVCaptureDevice? = nil, orientation:ImageOrientation = .portrait, captureAsYUV:Bool = true) throws {
guard let inputCamera = cameraDevice ?? AVCaptureDevice.default(for: AVMediaType.video) else {
fatalError("Failed to get input camera.")
}
self.inputCamera = inputCamera
self.orientation = orientation
self.captureAsYUV = captureAsYUV
self.captureSession = AVCaptureSession()
self.captureSession.beginConfiguration()
do {
self.videoInput = try AVCaptureDeviceInput(device:inputCamera)
} catch {
self.videoInput = nil
self.videoOutput = nil
self.yuvConversionShader = nil
super.init()
throw error
}
if (captureSession.canAddInput(videoInput)) {
captureSession.addInput(videoInput)
}
// Add the video frame output
videoOutput = AVCaptureVideoDataOutput()
videoOutput.alwaysDiscardsLateVideoFrames = false
if captureAsYUV {
supportsFullYUVRange = false
let supportedPixelFormats = videoOutput.availableVideoPixelFormatTypes
for currentPixelFormat in supportedPixelFormats {
if ((currentPixelFormat as NSNumber).int32Value == Int32(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)) {
supportsFullYUVRange = true
}
}
if (supportsFullYUVRange) {
yuvConversionShader = crashOnShaderCompileFailure("Camera"){try sharedImageProcessingContext.programForVertexShader(defaultVertexShaderForInputs(2), fragmentShader:YUVConversionFullRangeFragmentShader)}
videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String:NSNumber(value:Int32(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange))]
} else {
yuvConversionShader = crashOnShaderCompileFailure("Camera"){try sharedImageProcessingContext.programForVertexShader(defaultVertexShaderForInputs(2), fragmentShader:YUVConversionVideoRangeFragmentShader)}
videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String:NSNumber(value:Int32(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange))]
}
} else {
yuvConversionShader = nil
videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String:NSNumber(value:Int32(kCVPixelFormatType_32BGRA))]
}
if (captureSession.canAddOutput(videoOutput)) {
captureSession.addOutput(videoOutput)
}
captureSession.sessionPreset = sessionPreset
captureSession.commitConfiguration()
super.init()
videoOutput.setSampleBufferDelegate(self, queue:cameraProcessingQueue)
}
deinit {
sharedImageProcessingContext.runOperationSynchronously{
self.stopCapture()
self.videoOutput.setSampleBufferDelegate(nil, queue:nil)
self.audioOutput?.setSampleBufferDelegate(nil, queue:nil)
}
}
public func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard (output != audioOutput) else {
self.processAudioSampleBuffer(sampleBuffer)
return
}
guard (frameRenderingSemaphore.wait(timeout:DispatchTime.now()) == DispatchTimeoutResult.success) else { return }
let startTime = CFAbsoluteTimeGetCurrent()
let cameraFrame = CMSampleBufferGetImageBuffer(sampleBuffer)!
let bufferWidth = CVPixelBufferGetWidth(cameraFrame)
let bufferHeight = CVPixelBufferGetHeight(cameraFrame)
let currentTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
CVPixelBufferLockBaseAddress(cameraFrame, CVPixelBufferLockFlags(rawValue: CVOptionFlags(0)))
sharedImageProcessingContext.runOperationAsynchronously{
let cameraFramebuffer:Framebuffer
if (self.captureAsYUV) {
let luminanceFramebuffer = sharedImageProcessingContext.framebufferCache.requestFramebufferWithProperties(orientation:self.orientation, size:GLSize(width:GLint(bufferWidth), height:GLint(bufferHeight)), textureOnly:true)
luminanceFramebuffer.lock()
glActiveTexture(GLenum(GL_TEXTURE0))
glBindTexture(GLenum(GL_TEXTURE_2D), luminanceFramebuffer.texture)
glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_LUMINANCE, GLsizei(bufferWidth), GLsizei(bufferHeight), 0, GLenum(GL_LUMINANCE), GLenum(GL_UNSIGNED_BYTE), CVPixelBufferGetBaseAddressOfPlane(cameraFrame, 0))
let chrominanceFramebuffer = sharedImageProcessingContext.framebufferCache.requestFramebufferWithProperties(orientation:self.orientation, size:GLSize(width:GLint(bufferWidth), height:GLint(bufferHeight)), textureOnly:true)
chrominanceFramebuffer.lock()
glActiveTexture(GLenum(GL_TEXTURE1))
glBindTexture(GLenum(GL_TEXTURE_2D), chrominanceFramebuffer.texture)
glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_LUMINANCE_ALPHA, GLsizei(bufferWidth / 2), GLsizei(bufferHeight / 2), 0, GLenum(GL_LUMINANCE_ALPHA), GLenum(GL_UNSIGNED_BYTE), CVPixelBufferGetBaseAddressOfPlane(cameraFrame, 1))
cameraFramebuffer = sharedImageProcessingContext.framebufferCache.requestFramebufferWithProperties(orientation:.portrait, size:GLSize(width:GLint(bufferWidth), height:GLint(bufferHeight)), textureOnly:false)
let conversionMatrix:Matrix3x3
if (self.supportsFullYUVRange) {
conversionMatrix = colorConversionMatrix601FullRangeDefault
} else {
conversionMatrix = colorConversionMatrix601Default
}
convertYUVToRGB(shader:self.yuvConversionShader!, luminanceFramebuffer:luminanceFramebuffer, chrominanceFramebuffer:chrominanceFramebuffer, resultFramebuffer:cameraFramebuffer, colorConversionMatrix:conversionMatrix)
} else {
cameraFramebuffer = sharedImageProcessingContext.framebufferCache.requestFramebufferWithProperties(orientation:self.orientation, size:GLSize(width:GLint(bufferWidth), height:GLint(bufferHeight)), textureOnly:true)
glActiveTexture(GLenum(GL_TEXTURE0))
glBindTexture(GLenum(GL_TEXTURE_2D), cameraFramebuffer.texture)
glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, GLsizei(bufferWidth), GLsizei(bufferHeight), 0, GLenum(GL_BGRA), GLenum(GL_UNSIGNED_BYTE), CVPixelBufferGetBaseAddress(cameraFrame))
}
CVPixelBufferUnlockBaseAddress(cameraFrame, CVPixelBufferLockFlags(rawValue: CVOptionFlags(0)))
cameraFramebuffer.timingStyle = .videoFrame(timestamp:Timestamp(currentTime))
self.updateTargetsWithFramebuffer(cameraFramebuffer)
if self.runBenchmark {
self.numberOfFramesCaptured += 1
if (self.numberOfFramesCaptured > initialBenchmarkFramesToIgnore) {
let currentFrameTime = (CFAbsoluteTimeGetCurrent() - startTime)
self.totalFrameTimeDuringCapture += currentFrameTime
print("Average frame time : \(1000.0 * self.totalFrameTimeDuringCapture / Double(self.numberOfFramesCaptured - initialBenchmarkFramesToIgnore)) ms")
print("Current frame time : \(1000.0 * currentFrameTime) ms")
}
}
if self.logFPS {
if ((CFAbsoluteTimeGetCurrent() - self.lastCheckTime) > 1.0) {
self.lastCheckTime = CFAbsoluteTimeGetCurrent()
print("FPS: \(self.framesSinceLastCheck)")
self.framesSinceLastCheck = 0
}
self.framesSinceLastCheck += 1
}
self.frameRenderingSemaphore.signal()
}
}
public func startCapture() {
sharedImageProcessingContext.runOperationAsynchronously{
self.numberOfFramesCaptured = 0
self.totalFrameTimeDuringCapture = 0
}
if (!captureSession.isRunning) {
captureSession.startRunning()
}
}
public func stopCapture() {
if (captureSession.isRunning) {
captureSession.stopRunning()
}
}
public func transmitPreviousImage(to target:ImageConsumer, atIndex:UInt) {
// Not needed for camera inputs
}
// MARK: -
// MARK: Audio processing
func addAudioInputsAndOutputs() throws {
guard (self.audioOutput == nil) else { return }
captureSession.beginConfiguration()
defer {
captureSession.commitConfiguration()
}
guard let microphone = AVCaptureDevice.default(for: .audio) else {
return
}
let audioInput = try AVCaptureDeviceInput(device:microphone)
if captureSession.canAddInput(audioInput) {
captureSession.addInput(audioInput)
}
let audioOutput = AVCaptureAudioDataOutput()
if captureSession.canAddOutput(audioOutput) {
captureSession.addOutput(audioOutput)
}
self.microphone = microphone
self.audioInput = audioInput
self.audioOutput = audioOutput
audioOutput.setSampleBufferDelegate(self, queue:audioProcessingQueue)
}
func removeAudioInputsAndOutputs() {
guard (audioOutput != nil) else { return }
captureSession.beginConfiguration()
captureSession.removeInput(audioInput!)
captureSession.removeOutput(audioOutput!)
audioInput = nil
audioOutput = nil
microphone = nil
captureSession.commitConfiguration()
}
func processAudioSampleBuffer(_ sampleBuffer:CMSampleBuffer) {
self.audioEncodingTarget?.processAudioBuffer(sampleBuffer)
}
}