fix: avoid main-actor stopCapture error

This commit is contained in:
Peter Steinberger
2025-12-29 20:20:14 +01:00
parent 41be9232fe
commit 65478a6ff3

View File

@@ -72,116 +72,116 @@ final class ScreenRecordService {
func setHandlerError(_ error: Error) { func setHandlerError(_ error: Error) {
withStateLock { withStateLock {
if handlerError == nil { handlerError = error } if handlerError == nil { handlerError = error }
} }
} }
try await withCheckedThrowingContinuation { (cont: CheckedContinuation<Void, Error>) in try await withCheckedThrowingContinuation { (cont: CheckedContinuation<Void, Error>) in
Task { @MainActor in Task { @MainActor in
recorder.startCapture(handler: { sample, type, error in recorder.startCapture(handler: { sample, type, error in
if let error { if let error {
setHandlerError(error) setHandlerError(error)
return return
}
guard CMSampleBufferDataIsReady(sample) else { return }
switch type {
case .video:
let pts = CMSampleBufferGetPresentationTimeStamp(sample)
let shouldSkip = withStateLock {
if let lastVideoTime {
let delta = CMTimeSubtract(pts, lastVideoTime)
return delta.seconds < (1.0 / fpsValue)
}
return false
} }
if shouldSkip { return } guard CMSampleBufferDataIsReady(sample) else { return }
if withStateLock({ writer == nil }) { switch type {
guard let imageBuffer = CMSampleBufferGetImageBuffer(sample) else { case .video:
setHandlerError(ScreenRecordError.captureFailed("Missing image buffer")) let pts = CMSampleBufferGetPresentationTimeStamp(sample)
return let shouldSkip = withStateLock {
} if let lastVideoTime {
let width = CVPixelBufferGetWidth(imageBuffer) let delta = CMTimeSubtract(pts, lastVideoTime)
let height = CVPixelBufferGetHeight(imageBuffer) return delta.seconds < (1.0 / fpsValue)
do {
let w = try AVAssetWriter(outputURL: outURL, fileType: .mp4)
let settings: [String: Any] = [
AVVideoCodecKey: AVVideoCodecType.h264,
AVVideoWidthKey: width,
AVVideoHeightKey: height,
]
let vInput = AVAssetWriterInput(mediaType: .video, outputSettings: settings)
vInput.expectsMediaDataInRealTime = true
guard w.canAdd(vInput) else {
throw ScreenRecordError.writeFailed("Cannot add video input")
} }
w.add(vInput) return false
}
if shouldSkip { return }
if includeAudio { if withStateLock({ writer == nil }) {
let aInput = AVAssetWriterInput(mediaType: .audio, outputSettings: nil) guard let imageBuffer = CMSampleBufferGetImageBuffer(sample) else {
aInput.expectsMediaDataInRealTime = true setHandlerError(ScreenRecordError.captureFailed("Missing image buffer"))
if w.canAdd(aInput) { return
w.add(aInput) }
withStateLock { let width = CVPixelBufferGetWidth(imageBuffer)
audioInput = aInput let height = CVPixelBufferGetHeight(imageBuffer)
do {
let w = try AVAssetWriter(outputURL: outURL, fileType: .mp4)
let settings: [String: Any] = [
AVVideoCodecKey: AVVideoCodecType.h264,
AVVideoWidthKey: width,
AVVideoHeightKey: height,
]
let vInput = AVAssetWriterInput(mediaType: .video, outputSettings: settings)
vInput.expectsMediaDataInRealTime = true
guard w.canAdd(vInput) else {
throw ScreenRecordError.writeFailed("Cannot add video input")
}
w.add(vInput)
if includeAudio {
let aInput = AVAssetWriterInput(mediaType: .audio, outputSettings: nil)
aInput.expectsMediaDataInRealTime = true
if w.canAdd(aInput) {
w.add(aInput)
withStateLock {
audioInput = aInput
}
} }
} }
}
guard w.startWriting() else { guard w.startWriting() else {
throw ScreenRecordError throw ScreenRecordError
.writeFailed(w.error?.localizedDescription ?? "Failed to start writer") .writeFailed(w.error?.localizedDescription ?? "Failed to start writer")
} }
w.startSession(atSourceTime: pts) w.startSession(atSourceTime: pts)
withStateLock { withStateLock {
writer = w writer = w
videoInput = vInput videoInput = vInput
started = true started = true
} }
} catch { } catch {
setHandlerError(error) setHandlerError(error)
return return
}
}
let vInput = withStateLock { videoInput }
let isStarted = withStateLock { started }
guard let vInput, isStarted else { return }
if vInput.isReadyForMoreMediaData {
if vInput.append(sample) {
withStateLock {
sawVideo = true
lastVideoTime = pts
}
} else {
if let err = withStateLock({ writer?.error }) {
setHandlerError(ScreenRecordError.writeFailed(err.localizedDescription))
} }
} }
}
case .audioApp, .audioMic: let vInput = withStateLock { videoInput }
let aInput = withStateLock { audioInput } let isStarted = withStateLock { started }
let isStarted = withStateLock { started } guard let vInput, isStarted else { return }
guard includeAudio, let aInput, isStarted else { return } if vInput.isReadyForMoreMediaData {
if aInput.isReadyForMoreMediaData { if vInput.append(sample) {
_ = aInput.append(sample) withStateLock {
} sawVideo = true
lastVideoTime = pts
}
} else {
if let err = withStateLock({ writer?.error }) {
setHandlerError(ScreenRecordError.writeFailed(err.localizedDescription))
}
}
}
@unknown default: case .audioApp, .audioMic:
break let aInput = withStateLock { audioInput }
} let isStarted = withStateLock { started }
guard includeAudio, let aInput, isStarted else { return }
if aInput.isReadyForMoreMediaData {
_ = aInput.append(sample)
}
@unknown default:
break
}
}, completionHandler: { error in }, completionHandler: { error in
if let error { cont.resume(throwing: error) } else { cont.resume() } if let error { cont.resume(throwing: error) } else { cont.resume() }
}) })
} }
} }
try await Task.sleep(nanoseconds: UInt64(durationMs) * 1_000_000) try await Task.sleep(nanoseconds: UInt64(durationMs) * 1_000_000)
let stopError = await MainActor.run { let stopError = await withCheckedContinuation { cont in
await withCheckedContinuation { cont in Task { @MainActor in
recorder.stopCapture { error in cont.resume(returning: error) } recorder.stopCapture { error in cont.resume(returning: error) }
} }
} }