This repository was archived by the owner on Jul 8, 2024. It is now read-only.
forked from HaishinKit/HaishinKit.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOMixer.swift
More file actions
477 lines (434 loc) · 15.1 KB
/
Copy pathIOMixer.swift
File metadata and controls
477 lines (434 loc) · 15.1 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import AVFoundation
#if canImport(SwiftPMSupport)
import SwiftPMSupport
#endif
#if os(iOS)
import UIKit
#endif
#if os(iOS) || os(macOS)
public extension AVCaptureSession.Preset {
static let `default`: AVCaptureSession.Preset = .hd1280x720
}
#endif
protocol IOMixerDelegate: AnyObject {
func mixer(_ mixer: IOMixer, didOutput audio: AVAudioPCMBuffer, when: AVAudioTime)
func mixer(_ mixer: IOMixer, didOutput video: CMSampleBuffer)
func mixer(_ mixer: IOMixer, videoCodecErrorOccurred error: VideoCodec.Error)
func mixer(_ mixer: IOMixer, audioCodecErrorOccurred error: AudioCodec.Error)
#if os(iOS) || os(tvOS)
@available(tvOS 17.0, *)
func mixer(_ mixer: IOMixer, sessionWasInterrupted session: AVCaptureSession, reason: AVCaptureSession.InterruptionReason?)
@available(tvOS 17.0, *)
func mixer(_ mixer: IOMixer, sessionInterruptionEnded session: AVCaptureSession)
#endif
}
/// An object that mixies audio and video for streaming.
public final class IOMixer {
/// The default fps for an IOMixer, value is 30.
public static let defaultFrameRate: Float64 = 30
/// The AVAudioEngine shared instance holder.
public static let audioEngineHolder: InstanceHolder<AVAudioEngine> = .init {
return AVAudioEngine()
}
public var hasVideo: Bool {
get {
mediaLink.hasVideo
}
set {
mediaLink.hasVideo = newValue
}
}
public var isPaused: Bool {
get {
mediaLink.isPaused
}
set {
mediaLink.isPaused = newValue
}
}
#if os(tvOS)
private var _session: Any?
/// The capture session instance.
@available(tvOS 17.0, *)
public var session: AVCaptureSession {
get {
if _session == nil {
_session = makeSession()
}
return _session as! AVCaptureSession
}
set {
_session = newValue
}
}
#elseif os(iOS) || os(macOS)
/// The capture session instance.
public internal(set) lazy var session: AVCaptureSession = makeSession() {
didSet {
if oldValue.isRunning {
removeSessionObservers(oldValue)
oldValue.stopRunning()
}
audioIO.capture.detachSession(oldValue)
videoIO.capture.detachSession(oldValue)
if session.canSetSessionPreset(sessionPreset) {
session.sessionPreset = sessionPreset
}
audioIO.capture.attachSession(session)
videoIO.capture.attachSession(session)
}
}
#endif
public private(set) var isRunning: Atomic<Bool> = .init(false)
/// The recorder instance.
public lazy var recorder = IORecorder()
/// Specifies the drawable object.
public weak var drawable: (any NetStreamDrawable)? {
get {
videoIO.drawable
}
set {
videoIO.drawable = newValue
}
}
weak var delegate: (any IOMixerDelegate)?
lazy var audioIO: IOAudioUnit = {
var audioIO = IOAudioUnit()
audioIO.mixer = self
return audioIO
}()
lazy var videoIO: IOVideoUnit = {
var videoIO = IOVideoUnit()
videoIO.mixer = self
return videoIO
}()
lazy var mediaLink: MediaLink = {
var mediaLink = MediaLink<IOMixer>()
mediaLink.delegate = self
return mediaLink
}()
var audioFormat: AVAudioFormat? {
didSet {
guard let audioEngine else {
return
}
nstry({
if let audioFormat = self.audioFormat {
audioEngine.connect(self.mediaLink.playerNode, to: audioEngine.mainMixerNode, format: audioFormat)
} else {
audioEngine.disconnectNodeInput(self.mediaLink.playerNode)
}
}, { exeption in
logger.warn(exeption)
})
}
}
var isMultiCamSessionEnabled = false {
didSet {
guard oldValue != isMultiCamSessionEnabled else {
return
}
#if os(iOS)
session = makeSession()
#endif
}
}
#if os(tvOS)
private var _sessionPreset: Any?
@available(tvOS 17.0, *)
var sessionPreset: AVCaptureSession.Preset {
get {
if _sessionPreset == nil {
_sessionPreset = AVCaptureSession.Preset.default
}
return _sessionPreset as! AVCaptureSession.Preset
}
set {
guard sessionPreset != newValue, session.canSetSessionPreset(newValue) else {
return
}
session.beginConfiguration()
session.sessionPreset = newValue
session.commitConfiguration()
}
}
#elseif os(iOS) || os(macOS)
var sessionPreset: AVCaptureSession.Preset = .default {
didSet {
guard sessionPreset != oldValue, session.canSetSessionPreset(sessionPreset) else {
return
}
session.beginConfiguration()
session.sessionPreset = sessionPreset
session.commitConfiguration()
}
}
#endif
#if os(iOS) || os(macOS) || os(tvOS)
var inBackgroundMode = false {
didSet {
if #available(tvOS 17.0, *) {
guard inBackgroundMode != oldValue else {
return
}
if inBackgroundMode {
if !session.isMultitaskingCameraAccessEnabled {
videoIO.multiCamCapture.detachSession(session)
videoIO.capture.detachSession(session)
}
} else {
startCaptureSessionIfNeeded()
if !session.isMultitaskingCameraAccessEnabled {
videoIO.capture.attachSession(session)
videoIO.multiCamCapture.attachSession(session)
}
}
}
}
}
#endif
private(set) lazy var audioEngine: AVAudioEngine? = {
return IOMixer.audioEngineHolder.retain()
}()
deinit {
#if os(iOS) || os(macOS) || os(tvOS)
if #available(tvOS 17.0, *) {
if session.isRunning {
session.stopRunning()
}
}
#endif
IOMixer.audioEngineHolder.release(audioEngine)
}
#if os(iOS) || os(tvOS)
@available(tvOS 17.0, *)
private func makeSession() -> AVCaptureSession {
let session: AVCaptureSession
if isMultiCamSessionEnabled, #available(iOS 13.0, *) {
session = AVCaptureMultiCamSession()
} else {
session = AVCaptureSession()
}
if session.canSetSessionPreset(sessionPreset) {
session.sessionPreset = sessionPreset
}
if session.isMultitaskingCameraAccessSupported {
session.isMultitaskingCameraAccessEnabled = true
}
return session
}
#elseif os(macOS)
private func makeSession() -> AVCaptureSession {
let session = AVCaptureSession()
if session.canSetSessionPreset(sessionPreset) {
session.sessionPreset = sessionPreset
}
return session
}
#endif
}
extension IOMixer: IOUnitEncoding {
/// Starts encoding for video and audio data.
public func startEncoding(_ delegate: any AVCodecDelegate) {
videoIO.startEncoding(delegate)
audioIO.startEncoding(delegate)
}
/// Stop encoding.
public func stopEncoding() {
videoIO.stopEncoding()
audioIO.stopEncoding()
}
}
extension IOMixer: IOUnitDecoding {
/// Starts decoding for video and audio data.
public func startDecoding() {
audioIO.startDecoding()
videoIO.startDecoding()
mediaLink.startRunning()
}
/// Stop decoding.
public func stopDecoding() {
mediaLink.stopRunning()
audioIO.stopDecoding()
videoIO.stopDecoding()
}
}
extension IOMixer: MediaLinkDelegate {
// MARK: MediaLinkDelegate
func mediaLink(_ mediaLink: MediaLink<IOMixer>, dequeue sampleBuffer: CMSampleBuffer) {
delegate?.mixer(self, didOutput: sampleBuffer)
drawable?.enqueue(sampleBuffer)
}
func mediaLink(_ mediaLink: MediaLink<IOMixer>, didBufferingChanged: Bool) {
logger.info(didBufferingChanged)
}
}
#if os(iOS) || os(macOS) || os(tvOS)
extension IOMixer: Running {
// MARK: Running
public func startRunning() {
guard !isRunning.value else {
return
}
if #available(tvOS 17.0, *) {
addSessionObservers(session)
session.startRunning()
isRunning.mutate { $0 = session.isRunning }
}
}
public func stopRunning() {
guard isRunning.value else {
return
}
if #available(tvOS 17.0, *) {
removeSessionObservers(session)
session.stopRunning()
isRunning.mutate { $0 = session.isRunning }
}
}
@available(tvOS 17.0, *)
func startCaptureSessionIfNeeded() {
guard isRunning.value && !session.isRunning else {
return
}
session.startRunning()
isRunning.mutate { $0 = session.isRunning }
}
@available(tvOS 17.0, *)
private func addSessionObservers(_ session: AVCaptureSession) {
NotificationCenter.default.addObserver(self, selector: #selector(sessionRuntimeError(_:)), name: .AVCaptureSessionRuntimeError, object: session)
#if os(iOS) || os(tvOS)
NotificationCenter.default.addObserver(self, selector: #selector(sessionInterruptionEnded(_:)), name: .AVCaptureSessionInterruptionEnded, object: session)
NotificationCenter.default.addObserver(self, selector: #selector(sessionWasInterrupted(_:)), name: .AVCaptureSessionWasInterrupted, object: session)
#endif
}
@available(tvOS 17.0, *)
private func removeSessionObservers(_ session: AVCaptureSession) {
#if os(iOS) || os(tvOS)
NotificationCenter.default.removeObserver(self, name: .AVCaptureSessionWasInterrupted, object: session)
NotificationCenter.default.removeObserver(self, name: .AVCaptureSessionInterruptionEnded, object: session)
#endif
NotificationCenter.default.removeObserver(self, name: .AVCaptureSessionRuntimeError, object: session)
}
@available(tvOS 17.0, *)
@objc
private func sessionRuntimeError(_ notification: NSNotification) {
guard
let errorValue = notification.userInfo?[AVCaptureSessionErrorKey] as? NSError else {
return
}
let error = AVError(_nsError: errorValue)
switch error.code {
case .unsupportedDeviceActiveFormat:
#if os(iOS) || os(tvOS)
let isMultiCamSupported: Bool
if #available(iOS 13.0, *) {
isMultiCamSupported = session is AVCaptureMultiCamSession
} else {
isMultiCamSupported = false
}
#else
let isMultiCamSupported = true
#endif
guard let device = error.device, let format = device.videoFormat(
width: sessionPreset.width ?? Int32(videoIO.settings.videoSize.width),
height: sessionPreset.height ?? Int32(videoIO.settings.videoSize.height),
frameRate: videoIO.frameRate,
isMultiCamSupported: isMultiCamSupported
), device.activeFormat != format else {
return
}
do {
try device.lockForConfiguration()
device.activeFormat = format
if format.isFrameRateSupported(videoIO.frameRate) {
device.activeVideoMinFrameDuration = CMTime(value: 100, timescale: CMTimeScale(100 * videoIO.frameRate))
device.activeVideoMaxFrameDuration = CMTime(value: 100, timescale: CMTimeScale(100 * videoIO.frameRate))
}
device.unlockForConfiguration()
session.startRunning()
} catch {
logger.warn(error)
}
#if os(iOS) || os(tvOS)
case .mediaServicesWereReset:
startCaptureSessionIfNeeded()
#endif
default:
break
}
}
#if os(iOS) || os(tvOS)
@available(tvOS 17.0, *)
@objc
private func sessionWasInterrupted(_ notification: Notification) {
guard let session = notification.object as? AVCaptureSession else {
return
}
guard let userInfoValue = notification.userInfo?[AVCaptureSessionInterruptionReasonKey] as AnyObject?,
let reasonIntegerValue = userInfoValue.integerValue,
let reason = AVCaptureSession.InterruptionReason(rawValue: reasonIntegerValue) else {
delegate?.mixer(self, sessionWasInterrupted: session, reason: nil)
return
}
delegate?.mixer(self, sessionWasInterrupted: session, reason: reason)
}
@available(tvOS 17.0, *)
@objc
private func sessionInterruptionEnded(_ notification: Notification) {
delegate?.mixer(self, sessionInterruptionEnded: session)
}
#endif
}
#else
extension IOMixer: Running {
public func startRunning() {
}
public func stopRunning() {
}
}
#endif
extension IOMixer: VideoCodecDelegate {
// MARK: VideoCodecDelegate
public func videoCodec(_ codec: VideoCodec, didOutput formatDescription: CMFormatDescription?) {
}
public func videoCodec(_ codec: VideoCodec, didOutput sampleBuffer: CMSampleBuffer) {
mediaLink.enqueueVideo(sampleBuffer)
}
public func videoCodec(_ codec: VideoCodec, errorOccurred error: VideoCodec.Error) {
delegate?.mixer(self, videoCodecErrorOccurred: error)
}
}
extension IOMixer: AudioCodecDelegate {
// MARK: AudioCodecDelegate
public func audioCodec(_ codec: AudioCodec, errorOccurred error: AudioCodec.Error) {
delegate?.mixer(self, audioCodecErrorOccurred: error)
}
public func audioCodec(_ codec: AudioCodec, didOutput audioFormat: AVAudioFormat) {
do {
self.audioFormat = audioFormat
if let audioEngine = audioEngine, audioEngine.isRunning == false {
try audioEngine.start()
}
} catch {
logger.error(error)
}
}
public func audioCodec(_ codec: AudioCodec, didOutput audioBuffer: AVAudioBuffer, when: AVAudioTime) {
guard let audioBuffer = audioBuffer as? AVAudioPCMBuffer else {
return
}
delegate?.mixer(self, didOutput: audioBuffer, when: when)
mediaLink.enqueueAudio(audioBuffer)
}
}
extension IOMixer: IOAudioUnitDelegate {
// MARK: IOAudioUnitDelegate
func audioUnit(_ audioUnit: IOAudioUnit, errorOccurred error: AudioCodec.Error) {
delegate?.mixer(self, audioCodecErrorOccurred: error)
}
func audioUnit(_ audioUnit: IOAudioUnit, didOutput audioBuffer: AVAudioPCMBuffer, when: AVAudioTime) {
delegate?.mixer(self, didOutput: audioBuffer, when: when)
recorder.appendAudioPCMBuffer(audioBuffer, when: when)
}
}