AVAssetWriter failed on macOS 12.0.01/iOS15.1.1 with error code -11800 and underlying code -17771

Hi there,

I used AVAssetWriter to export mp4 file, input audio and video samples are encoded data (aac and h264 samples), sometimes it works well, but sometime it will failed randomly with error code -11800 and underlying code -17771 after calling finishWritingWithCompletionHandler, log looks like this:

 AVAssetWriter status: Failed, status:err:Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-17771), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x60400008e1d0 {Error Domain=NSOSStatusErrorDomain Code=-17771 "(null)"}}, status=3

And I notice these logs on Console.app just before the error message:

default	17:35:08.380220+0800	MyAPP	<<<< FigExportCommmon >>>> remakerFamily_formatWriterErrorOccurred: (0x6140001ba240)
default	17:35:08.380561+0800	MyAPP	<<<< FigExportCommmon >>>> remakerFamily_PostFailureNotificationIfError: (0x6140001ba240) state finish writing called
default	17:35:08.381010+0800	MyAPP	<<<< FigExportCommmon >>>> remakerfamily_setFailureStatusIfError: (0x6140001ba240) err -17771 state finish writing called

I don't know why this will happen, any replies are appreciate. thx.

I have tried: 1.both the pull style and push style API, neither works well. I use serial NSOperationQueue to make sure all writer operation be serial in push sytle, doesn't help too 2._writer.shouldOptimizeForNetworkUse set to be YES or NO, neither helps. When YES is set, there's no file after finishing writing, when NO is set, there's a mp4 file without moov atom, only 'ftyp', 'wide' and a broken 'mdat' atom exist

Please file a report so we can take a look at what is going on. Ideally you could attach a project that we can build and run to reproduce the problem.

Thanks for you reply. I found the solution yestoday, after giving duration value for every video frame's CMSampleTimingInfo struct, this problem disappeared.

///// Code with Problem

 CMSampleTimingInfo timingInfo;
 timingInfo.presentationTimeStamp = CMTimeMake(pts_ms, 1000);
 timingInfo.decodeTimeStamp = CMTimeMake(dts_ms, 1000);

 size_t sampleSize[1] = { frame_size };

 CMSampleBufferRef sampleBuffer;
 status = CMSampleBufferCreate(kCFAllocatorDefault,
                dataBlock,
                true, // dataReady
                NULL, // makeDataReadyCallback
                NULL, // makeDataReadyRefcon
                _videoFormatDescription,
                1, // numSamples
                1, // numSampleTimingEntries
                &timingInfo, // CMSampleTimingInfo *sampleTimingArray
                1, // numSampleSizeEntries
                sampleSize, // sampleSizeArray
                &sampleBuffer); //sampleBufferOut

///// Code without Problem

 CMSampleTimingInfo timingInfo;
 timingInfo.presentationTimeStamp = CMTimeMake(pts_ms, 1000);
 timingInfo.decodeTimeStamp = CMTimeMake(dts_ms, 1000);

// ⬇️⬇️⬇️
timingInfo.duration = CMTimeMake(duration, 1000)

 size_t sampleSize[1] = { frame_size };

 CMSampleBufferRef sampleBuffer;
 status = CMSampleBufferCreate(kCFAllocatorDefault,
                dataBlock,
                true, // dataReady
                NULL, // makeDataReadyCallback
                NULL, // makeDataReadyRefcon
                _videoFormatDescription,
                1, // numSamples
                1, // numSampleTimingEntries
                &timingInfo, // CMSampleTimingInfo *sampleTimingArray
                1, // numSampleSizeEntries
                sampleSize, // sampleSizeArray
                &sampleBuffer); //sampleBufferOut
AVAssetWriter failed on macOS 12.0.01/iOS15.1.1 with error code -11800 and underlying code -17771
 
 
Q