AVFoundationErrorDomain Code=-11847 even when AVAssetWriter is not backgrounded

Hello,

I'm finding that AVAssetWriter status is failing in the middle of recording frames obtained using RPScreenRecorder::startCapture. The failure seems to occur about a minute and half into a recording. The error code when asked from AVAssetWriter is -11847. It appears that error code is usually thrown when the app is backgrounded but in my case the app is never backgrounded. This error happens once every 2-3 runs on my iPhone 12 Pro Max. It's also reproducible on an iPhone 11. iOS version is 15.1.1.

Any clues as to why this might be occurring? Thanks in advance.

Note I'm using movieFragmentInterval on an mp4 file. I couldn't tell from the documentation if its only for QuickTime files or not.

I'm setting up an AVAssetWriter like so. Code simplified for readability.

let assetWriter = try? AVAssetWriter(outputURL: fileURL, fileType: AVFileType.mp4)!

assetWriter.movieFragmentInterval = CMTimeMakeWithSeconds(60, preferredTimescale: 1000)      
let preset = AVOutputSettingsAssistant(preset: .preset1280x720)

var outputSettings = preset?.videoSettings
outputSettings?[AVVideoScalingModeKey] = AVVideoScalingModeResizeAspect

var compressionSettings = outputSettings?[AVVideoCompressionPropertiesKey] as? [String:Any]
var bitRate:Float = 4.0
compressionSettings?[AVVideoAverageBitRateKey] = NSNumber(value:720.0 * 1280.0 * bitRate)
outputSettings?[AVVideoCompressionPropertiesKey] = compressionSettings
     
videoInput = AVAssetWriterInput(mediaType: AVMediaType.video, outputSettings: outputSettings)
videoInput.expectsMediaDataInRealTime = true
     
assetWriter.add(videoInput)
     
let audioOutputSettings = preset?.audioSettings
     
audioInput = AVAssetWriterInput(mediaType: .audio, outputSettings: audioOutputSettings)
audioInput.expectsMediaDataInRealTime = true;
      
assetWriter.add(audioInput)

assetWriter.startWriting() 

Some more information on when I see the error being triggered. It happens pretty consistently after the second fragment interval time has elapsed. So if I set the fragment interval time to be 60 seconds, it almost always happens after the 2 minute mark. So it seems like something is going wrong when the second fragment is being written to the movie file.

Hope this provides some insights. Happy to file a radar if its a bug but want to confirm that it is in the first place and that I'm not doing something wrong with my setup (see code above).

Thanks

I'm facing the same issue here. Most other discussions of error code 11847 seem to indicate the backgrounding of the app as the main issue. However, my app is certainly not being backgrounded (since I'm facing this issue despite actively interacting with the UI of my app). I'm also noticing that this error occurs roughly 70-80 seconds into a AVWriterInput session being active.

@krissb78 did you make any progress on this?

AVFoundationErrorDomain Code=-11847 even when AVAssetWriter is not backgrounded
 
 
Q