-
Notifications
You must be signed in to change notification settings - Fork 55
/
SceneKitVideoRecorder.swift
390 lines (279 loc) · 12.2 KB
/
SceneKitVideoRecorder.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//
// SceneKitVideoRecorder.swift
//
// Created by Omer Karisman on 2017/08/29.
//
import UIKit
import SceneKit
import ARKit
import AVFoundation
import CoreImage
import BrightFutures
public class SceneKitVideoRecorder: NSObject, AVAudioRecorderDelegate {
private var writer: AVAssetWriter!
private var videoInput: AVAssetWriterInput!
var recordingSession: AVAudioSession!
var audioRecorder: AVAudioRecorder!
private var pixelBufferAdaptor: AVAssetWriterInputPixelBufferAdaptor!
private var options: Options
private let frameQueue = DispatchQueue(label: "com.svtek.SceneKitVideoRecorder.frameQueue")
private let bufferQueue = DispatchQueue(label: "com.svtek.SceneKitVideoRecorder.bufferQueue", attributes: .concurrent)
private let audioQueue = DispatchQueue(label: "com.svtek.SceneKitVideoRecorder.audioQueue")
private let errorDomain = "com.svtek.SceneKitVideoRecorder"
private var displayLink: CADisplayLink? = nil
private var initialTime: CMTime = CMTime.invalid
private var currentTime: CMTime = CMTime.invalid
private var sceneView: SCNView
private var audioSettings: [String : Any]?
public var isAudioSetup: Bool = false
private var isPrepared: Bool = false
private var isRecording: Bool = false
private var useAudio: Bool {
return options.useMicrophone && AVAudioSession.sharedInstance().recordPermission == .granted && isAudioSetup
}
private var videoFramesWritten: Bool = false
private var waitingForPermissions: Bool = false
private var renderer: SCNRenderer!
public var updateFrameHandler: ((_ image: UIImage) -> Void)? = nil
private var finishedCompletionHandler: ((_ url: URL) -> Void)? = nil
@available(iOS 11.0, *)
public convenience init(withARSCNView view: ARSCNView, options: Options = .`default`) throws {
try self.init(scene: view, options: options)
}
public init(scene: SCNView, options: Options = .`default`, setupAudio: Bool = true) throws {
self.sceneView = scene
self.options = options
self.isRecording = false
self.videoFramesWritten = false
super.init()
FileController.clearTemporaryDirectory()
self.prepare()
if setupAudio {
self.setupAudio()
}
}
private func prepare() {
self.prepare(with: self.options)
isPrepared = true
}
private func prepare(with options: Options) {
guard let device = MTLCreateSystemDefaultDevice() else { return }
self.renderer = SCNRenderer(device: device, options: nil)
renderer.scene = self.sceneView.scene
initialTime = CMTime.invalid
self.options.videoSize = options.videoSize
writer = try! AVAssetWriter(outputURL: self.options.videoOnlyUrl, fileType: self.options.fileType)
self.setupVideo()
}
@discardableResult public func cleanUp() -> URL {
var output = options.outputUrl
if options.deleteFileIfExists {
let nameOnly = (options.outputUrl.lastPathComponent as NSString).deletingPathExtension
let fileExt = (options.outputUrl.lastPathComponent as NSString).pathExtension
let tempFileName = NSTemporaryDirectory() + nameOnly + "TMP." + fileExt
output = URL(fileURLWithPath: tempFileName)
FileController.move(from: options.outputUrl, to: output)
FileController.delete(file: self.options.audioOnlyUrl)
FileController.delete(file: self.options.videoOnlyUrl)
}
return output
}
public func setupAudio() {
guard self.options.useMicrophone, !self.isAudioSetup else { return }
recordingSession = AVAudioSession.sharedInstance()
do {
try recordingSession.setCategory(AVAudioSession.Category.playAndRecord, mode: .default)
try recordingSession.setActive(true)
recordingSession.requestRecordPermission() { allowed in
DispatchQueue.main.async {
if allowed {
self.isAudioSetup = true
} else {
self.isAudioSetup = false
}
}
}
} catch {
self.isAudioSetup = false
}
}
private func startRecordingAudio() {
let audioUrl = self.options.audioOnlyUrl
let settings = self.options.assetWriterAudioInputSettings
do {
audioRecorder = try AVAudioRecorder(url: audioUrl, settings: settings)
audioRecorder.delegate = self
audioRecorder.record()
} catch {
finishRecordingAudio(success: false)
}
}
private func finishRecordingAudio(success: Bool) {
audioRecorder.stop()
audioRecorder = nil
}
private func setupVideo() {
self.videoInput = AVAssetWriterInput(mediaType: AVMediaType.video,
outputSettings: self.options.assetWriterVideoInputSettings)
self.videoInput.mediaTimeScale = self.options.timeScale
self.pixelBufferAdaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: videoInput,sourcePixelBufferAttributes: self.options.sourcePixelBufferAttributes)
writer.add(videoInput)
}
public func startWriting() -> Future<Void, NSError> {
let promise = Promise<Void, NSError>()
guard !isRecording else {
promise.failure(NSError(domain: errorDomain, code: ErrorCode.recorderBusy.rawValue, userInfo: nil))
return promise.future
}
isRecording = true
startDisplayLink()
guard startInputPipeline() else {
stopDisplayLink()
cleanUp()
promise.failure(NSError(domain: errorDomain, code: ErrorCode.unknown.rawValue, userInfo: nil))
return promise.future
}
promise.success(())
return promise.future
}
public func finishWriting() -> Future<URL, NSError> {
let promise = Promise<URL, NSError>()
guard isRecording, writer.status == .writing else {
let error = NSError(domain: errorDomain, code: ErrorCode.notReady.rawValue, userInfo: nil)
promise.failure(error)
return promise.future
}
videoInput.markAsFinished()
if audioRecorder != nil {
finishRecordingAudio(success: true)
}
isRecording = false
isPrepared = false
videoFramesWritten = false
currentTime = CMTime.invalid
writer.finishWriting { [weak self] in
guard let this = self else { return }
this.stopDisplayLink()
if this.useAudio {
this.mergeVideoAndAudio(videoUrl: this.options.videoOnlyUrl, audioUrl: this.options.audioOnlyUrl).onSuccess {
let outputUrl = this.cleanUp()
promise.success(outputUrl)
}
.onFailure { error in
this.cleanUp()
promise.failure(error)
}
} else {
FileController.move(from: this.options.videoOnlyUrl, to: this.options.outputUrl)
let outputUrl = this.cleanUp()
promise.success(outputUrl)
}
this.prepare()
}
return promise.future
}
private func getCurrentCMTime() -> CMTime {
return CMTimeMakeWithSeconds(CACurrentMediaTime(), preferredTimescale: 1000);
}
private func getAppendTime() -> CMTime {
currentTime = getCurrentCMTime() - initialTime
return currentTime
}
private func startDisplayLink() {
displayLink = CADisplayLink(target: self, selector: #selector(updateDisplayLink))
displayLink?.preferredFramesPerSecond = options.fps
displayLink?.add(to: .main, forMode: RunLoop.Mode.common)
}
@objc private func updateDisplayLink() {
frameQueue.async { [weak self] in
if self?.writer.status == .unknown { return }
if self?.writer.status == .failed { return }
guard let input = self?.videoInput, input.isReadyForMoreMediaData else { return }
self?.renderSnapshot()
}
}
private func startInputPipeline() -> Bool {
guard writer.status == .unknown else { return false }
guard writer.startWriting() else { return false }
writer.startSession(atSourceTime: CMTime.zero)
videoInput.requestMediaDataWhenReady(on: frameQueue, using: {})
return true
}
private func renderSnapshot() {
autoreleasepool {
let time = CACurrentMediaTime()
let image = renderer.snapshot(atTime: time, with: self.options.videoSize, antialiasingMode: self.options.antialiasingMode)
updateFrameHandler?(image)
guard let pool = self.pixelBufferAdaptor.pixelBufferPool else { print("No pool"); return }
let pixelBufferTemp = PixelBufferFactory.make(with: image, usingBuffer: pool)
guard let pixelBuffer = pixelBufferTemp else { print("No buffer"); return }
guard videoInput.isReadyForMoreMediaData else { print("No ready for media data"); return }
if videoFramesWritten == false {
videoFramesWritten = true
startRecordingAudio()
initialTime = getCurrentCMTime()
}
let currentTime = getCurrentCMTime()
guard CMTIME_IS_VALID(currentTime) else { print("No current time"); return }
let appendTime = getAppendTime()
guard CMTIME_IS_VALID(appendTime) else { print("No append time"); return }
bufferQueue.async { [weak self] in
self?.pixelBufferAdaptor.append(pixelBuffer, withPresentationTime: appendTime)
}
}
}
private func stopDisplayLink() {
displayLink?.invalidate()
displayLink = nil
}
private func mergeVideoAndAudio(videoUrl:URL, audioUrl:URL) -> Future<Void, NSError>
{
let promise = Promise<Void, NSError>()
let mixComposition : AVMutableComposition = AVMutableComposition()
var mutableCompositionVideoTrack : [AVMutableCompositionTrack] = []
var mutableCompositionAudioTrack : [AVMutableCompositionTrack] = []
let totalVideoCompositionInstruction : AVMutableVideoCompositionInstruction = AVMutableVideoCompositionInstruction()
let aVideoAsset : AVAsset = AVAsset(url: videoUrl)
let aAudioAsset : AVAsset = AVAsset(url: audioUrl)
mutableCompositionVideoTrack.append(mixComposition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)!)
mutableCompositionAudioTrack.append(mixComposition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)!)
guard !aVideoAsset.tracks.isEmpty, !aAudioAsset.tracks.isEmpty else {
let error = NSError(domain: errorDomain, code: ErrorCode.zeroFrames.rawValue, userInfo: nil)
promise.failure(error)
return promise.future
}
let aVideoAssetTrack : AVAssetTrack = aVideoAsset.tracks(withMediaType: AVMediaType.video)[0]
let aAudioAssetTrack : AVAssetTrack = aAudioAsset.tracks(withMediaType: AVMediaType.audio)[0]
do {
try mutableCompositionVideoTrack[0].insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: aVideoAssetTrack.timeRange.duration), of: aVideoAssetTrack, at: CMTime.zero)
try mutableCompositionAudioTrack[0].insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: aVideoAssetTrack.timeRange.duration), of: aAudioAssetTrack, at: CMTime.zero)
} catch {
}
totalVideoCompositionInstruction.timeRange = CMTimeRangeMake(start: CMTime.zero,duration: aVideoAssetTrack.timeRange.duration )
let mutableVideoComposition : AVMutableVideoComposition = AVMutableVideoComposition()
mutableVideoComposition.frameDuration = CMTimeMake(value: 1, timescale: Int32(self.options.fps))
mutableVideoComposition.renderSize = self.options.videoSize
let savePathUrl : URL = self.options.outputUrl
let assetExport: AVAssetExportSession = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)!
assetExport.outputFileType = AVFileType.mp4
assetExport.outputURL = savePathUrl
assetExport.shouldOptimizeForNetworkUse = true
assetExport.exportAsynchronously { () -> Void in
switch assetExport.status {
case AVAssetExportSession.Status.completed:
promise.success(())
case AVAssetExportSession.Status.failed:
let assetExportErrorMessage = "failed \(String(describing: assetExport.error))"
let error = NSError(domain: self.errorDomain, code: ErrorCode.assetExport.rawValue, userInfo: ["Reason": assetExportErrorMessage])
promise.failure(error)
case AVAssetExportSession.Status.cancelled:
let assetExportErrorMessage = "cancelled \(String(describing: assetExport.error))"
let error = NSError(domain: self.errorDomain, code: ErrorCode.assetExport.rawValue, userInfo: ["Reason": assetExportErrorMessage])
promise.failure(error)
default:
promise.success(())
}
}
return promise.future
}
}