Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed #1209 AVCaptureSession startRunning] should be called from background thread. #1216

Merged
merged 1 commit into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Sources/Media/IOMixer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ protocol IOMixerDelegate: AnyObject {
func mixer(_ mixer: IOMixer, sessionWasInterrupted session: AVCaptureSession, reason: AVCaptureSession.InterruptionReason)
func mixer(_ mixer: IOMixer, sessionInterruptionEnded session: AVCaptureSession, reason: AVCaptureSession.InterruptionReason)
#endif
func mixerSessionWillResume(_ mixer: IOMixer)
}

/// An object that mixies audio and video for streaming.
Expand Down Expand Up @@ -323,6 +324,11 @@ extension IOMixer: Running {
isRunning.mutate { $0 = session.isRunning }
}

func startCaptureSession() {
session.startRunning()
isRunning.mutate { $0 = session.isRunning }
}

private func addSessionObservers(_ session: AVCaptureSession) {
NotificationCenter.default.addObserver(self, selector: #selector(sessionRuntimeError(_:)), name: .AVCaptureSessionRuntimeError, object: session)
#if os(iOS)
Expand Down Expand Up @@ -438,8 +444,7 @@ extension IOMixer: Running {
guard isRunning.value && !session.isRunning else {
return
}
session.startRunning()
isRunning.mutate { $0 = session.isRunning }
delegate?.mixerSessionWillResume(self)
}
}
#else
Expand Down
33 changes: 32 additions & 1 deletion Sources/Net/NetStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ open class NetStream: NSObject {
private static let queueValue = UnsafeMutableRawPointer.allocate(byteCount: 1, alignment: 1)

/// The mixer object.
public private(set) var mixer = IOMixer()
public private(set) lazy var mixer: IOMixer = {
let mixer = IOMixer()
mixer.delegate = self
return mixer
}()
/// Specifies the delegate of the NetStream.
public weak var delegate: (any NetStreamDelegate)?

Expand Down Expand Up @@ -282,6 +286,33 @@ open class NetStream: NSObject {
}
}

extension NetStream: IOMixerDelegate {
// MARK: IOMixerDelegate
func mixer(_ mixer: IOMixer, didOutput video: CMSampleBuffer) {
delegate?.stream(self, didOutput: video)
}

func mixer(_ mixer: IOMixer, didOutput audio: AVAudioPCMBuffer, presentationTimeStamp: CMTime) {
delegate?.stream(self, didOutput: audio, presentationTimeStamp: presentationTimeStamp)
}

func mixerSessionWillResume(_ mixer: IOMixer) {
lockQueue.async {
mixer.startCaptureSession()
}
}

#if os(iOS)
func mixer(_ mixer: IOMixer, sessionWasInterrupted session: AVCaptureSession, reason: AVCaptureSession.InterruptionReason) {
delegate?.stream(self, sessionWasInterrupted: session, reason: reason)
}

func mixer(_ mixer: IOMixer, sessionInterruptionEnded session: AVCaptureSession, reason: AVCaptureSession.InterruptionReason) {
delegate?.stream(self, sessionInterruptionEnded: session, reason: reason)
}
#endif
}

extension NetStream: IOScreenCaptureUnitDelegate {
// MARK: IOScreenCaptureUnitDelegate
public func session(_ session: any IOScreenCaptureUnit, didOutput pixelBuffer: CVPixelBuffer, presentationTime: CMTime) {
Expand Down
22 changes: 0 additions & 22 deletions Sources/RTMP/RTMPStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -638,25 +638,3 @@ extension RTMPStream: RTMPMuxerDelegate {
return delegate?.streamWillDropFrame(self) ?? false
}
}

extension RTMPStream: IOMixerDelegate {
// MARK: IOMixerDelegate
func mixer(_ mixer: IOMixer, didOutput video: CMSampleBuffer) {
frameCount += 1
delegate?.stream(self, didOutput: video)
}

func mixer(_ mixer: IOMixer, didOutput audio: AVAudioPCMBuffer, presentationTimeStamp: CMTime) {
delegate?.stream(self, didOutput: audio, presentationTimeStamp: presentationTimeStamp)
}

#if os(iOS)
func mixer(_ mixer: IOMixer, sessionWasInterrupted session: AVCaptureSession, reason: AVCaptureSession.InterruptionReason) {
delegate?.stream(self, sessionWasInterrupted: session, reason: reason)
}

func mixer(_ mixer: IOMixer, sessionInterruptionEnded session: AVCaptureSession, reason: AVCaptureSession.InterruptionReason) {
delegate?.stream(self, sessionInterruptionEnded: session, reason: reason)
}
#endif
}