What Caused the AVAssetWriter Error -16364 During Our iOS App Modernization?
While working on a custom media-editing mobile application for a client in the digital creation space, we were tasked with updating an aging Unity-based architecture. The platform captures video, applies custom visual effects and then merges the video with separately recorded audio tracks. Historically, this was handled by a proprietary, unmaintained native plugin that began crashing on newer iOS versions.
Since the legacy plugin only consisted of a binary without source code, updating it directly was impossible. To maintain project momentum, our engineering team opted to reimplement the required subset of the native library using AVFoundation in Objective-C. The API surface was relatively small: initialize a recorder, feed it video frames, feed it audio samples and finalize the MP4 file.
We quickly got the video capture working. It recorded camera input and wrote it flawlessly to disk. However, a severe issue emerged during the second phase—multiplexing. The application’s architecture was designed to send all video frames to the native layer first, followed by all audio frames. While AVFoundation accepted the video frames without complaint, the audio pipeline failed randomly with an obscure OSStatus error. This scenario highlights the importance of understanding underlying media frameworks, an expertise clients seek when they hire software developer teams capable of handling deep architectural refactoring. This article breaks down why this happened and how we engineered a robust fix so your team can avoid similar media multiplexing pitfalls.
How Was the Media Architecture Initially Structured?
The business use case required users to record video, scrub through the timeline to add visual filters and then overlay an audio track. Because of how the legacy application processed data, the rendering engine pushed data sequentially by track type rather than sequentially by time.
The application would loop through the entire video track, passing presentation timestamps (PTS) from 0 to the end of the clip, calling a custom CommitFrame function. Once the video processing finished, it invoked CommitSamples, sending audio blocks starting again from timestamp 0.
In our custom Objective-C wrapper, we initialized an AVAssetWriter with two inputs: an AVAssetWriterInputPixelBufferAdaptor for video and a standard AVAssetWriterInput for audio. Both inputs were configured with expectsMediaDataInRealTime = true to optimize memory allocation and the writer session was started at kCMTimeZero.
Why Did AVAssetWriter Fail With Error Code -11800 and -16364?
During testing, video frames processed beautifully, but the audio processing failed unexpectedly. Sometimes the failure occurred immediately on the first audio frame; other times, it accepted a few seconds of audio before crashing. The system logs revealed the following error:
[21333333] Failed sending audio sample buffer: Error Domain=AVFoundationErrorDomain Code=-11800
UserInfo={NSLocalizedFailureReason=An unknown error occurred (-16364),
NSLocalizedDescription=The operation could not be completed.,
NSUnderlyingError=0x12394c0f0 {Error Domain=NSOSStatusErrorDomain Code=-16364 "(null)"}}
The symptoms pointed to a deeper architectural mismatch. OSStatus -16364 in AVFoundation generally maps to multiplexing errors, specifically when the internal interleaving buffer overflows or fails to allocate memory (kCMSampleBufferError_AllocationFailed or similar muxer limitations).
By default, AVAssetWriter expects audio and video frames to be interleaved. This means you must append media data in order of their presentation timestamps, regardless of whether it’s audio or video. When we fed 10 seconds of video into the writer, the internal multiplexer held those frames in memory, waiting for the corresponding audio frames for timestamps 0.0 through 10.0 so it could interleave them into the MP4 container format.
Because the app was sending all video frames before a single audio frame, the AVAssetWriter‘s internal buffer grew until it exhausted its allowed memory quota or timed out, triggering the vague -16364 failure when we finally attempted to write the audio at timestamp 0.
What Alternative Architecture Approaches Were Considered?
Before overhauling the data flow between the application and the native layer, we explored several ways to solve the interleaving limitation natively. When companies hire iOS developers for native integrations, evaluating these architectural tradeoffs is a critical part of the delivery process.
Can Disabling Real-Time Expectations Solve The Buffer Issue?
We initially toggled expectsMediaDataInRealTime = false on both the video and audio AVAssetWriterInput objects. While this tells AVFoundation that the data is not coming from a live camera stream and allows for slightly larger internal buffering, it does not bypass the fundamental requirement of MP4 interleaving. The writer still eventually choked on the massive timestamp discrepancy between the audio and video streams.
Should We Write Separate Files and Merge Them Later?
Another approach was to write the video to temp_video.mp4 and the audio to temp_audio.m4a using two separate AVAssetWriter instances and then use AVAssetExportSession or AVMutableComposition to merge them. While functionally sound, this doubled the disk I/O and significantly increased processing time. For a mobile media app, this performance degradation was unacceptable.
Could We Buffer All Frames In Memory Before Writing?
We considered creating a native queue in Objective-C that would hold all video frames in memory, wait for the audio frames, interleave them dynamically and then push them to AVAssetWriter. However, raw uncompressed CVPixelBufferRef frames consume massive amounts of RAM. Buffering a 30-second 1080p video in memory would instantly trigger iOS jetsam memory warnings and crash the app.
How Do You Implement True Timed Interleaving?
The only architecturally sound solution was to refactor the data pipeline to enforce true interleaving. We had to modify the communication layer so that the app sends video and audio frames in chronological order. If video frame 1 is at 0.03s and audio block 1 spans 0.0s to 0.05s, they must be handed to the native plugin in that sequential order.
How Did We Implement The Final Interleaved Media Pipeline?
To implement the fix, we aligned the application’s export loop to step through time rather than by track. We also hardened the Objective-C implementation to enforce strict memory management, ensuring no CVPixelBuffer or CMSampleBuffer leaks occurred during the high-frequency appending process.
Here is the sanitized and corrected approach for handling the native video and audio appending securely:
#import <AVFoundation/AVFoundation.h>
// A simplified generic representation of our writer wrapper
@interface MediaRecorder : NSObject
@property (nonatomic, strong) AVAssetWriter *writer;
@property (nonatomic, strong) AVAssetWriterInput *videoInput;
@property (nonatomic, strong) AVAssetWriterInputPixelBufferAdaptor *videoAdaptor;
@property (nonatomic, strong) AVAssetWriterInput *audioInput;
@property (nonatomic, assign) CMTime lastAppendedTime;
@end
@implementation MediaRecorder
- (void)appendVideoPixelBuffer:(void *)pixelBuffer atTimestamp:(long)timestamp {
CMTime presentationTime = CMTimeMake(timestamp, 1000000000L);
// Ensure we are appending in sequential order
if (CMTIME_COMPARE_INLINE(presentationTime, <, self.lastAppendedTime)) {
NSLog(@"Warning: Out of order frame detected. Interleaving is required.");
}
CVPixelBufferRef buffer;
CVPixelBufferPoolRef pool = self.videoAdaptor.pixelBufferPool;
if (!pool) return;
CVReturn status = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pool, &buffer);
if (status != kCVReturnSuccess) return;
// Lock, copy data and unlock
CVPixelBufferLockBaseAddress(buffer, 0);
// ... [Data conversion logic here] ...
CVPixelBufferUnlockBaseAddress(buffer, 0);
// Wait for the writer to be ready to prevent buffer overruns
while (![self.videoInput isReadyForMoreMediaData]) {
[NSThread sleepForTimeInterval:0.001];
}
BOOL success = [self.videoAdaptor appendPixelBuffer:buffer withPresentationTime:presentationTime];
if (!success) {
NSLog(@"Failed appending video buffer: %@", self.writer.error);
} else {
self.lastAppendedTime = presentationTime;
}
// Crucial memory management to prevent leaks
CVPixelBufferRelease(buffer);
}
- (void)appendAudioSamples:(float *)audioData count:(int)sampleCount atTimestamp:(long)timestamp {
CMTime presentationTime = CMTimeMake(timestamp, 1000000000L);
size_t dataSize = sampleCount * sizeof(float);
CMBlockBufferRef blockBuffer;
OSStatus status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault, NULL, dataSize,
kCFAllocatorDefault, NULL, 0, dataSize,
kCMBlockBufferAssureMemoryNowFlag, &blockBuffer);
if (status != kCMBlockBufferNoErr) return;
CMBlockBufferReplaceDataBytes(audioData, blockBuffer, 0, dataSize);
CMSampleBufferRef sampleBuffer;
// Assume audioDesc is correctly configured previously
status = CMAudioSampleBufferCreateReadyWithPacketDescriptions(kCFAllocatorDefault, blockBuffer,
_audioDesc, sampleCount,
presentationTime, NULL, &sampleBuffer);
if (status == noErr) {
while (![self.audioInput isReadyForMoreMediaData]) {
[NSThread sleepForTimeInterval:0.001];
}
if (![self.audioInput appendSampleBuffer:sampleBuffer]) {
NSLog(@"Failed sending audio sample buffer: %@", self.writer.error);
} else {
self.lastAppendedTime = presentationTime;
}
}
// Release CoreFoundation objects
if (sampleBuffer) CFRelease(sampleBuffer);
if (blockBuffer) CFRelease(blockBuffer);
}
@end
By shifting the application layer to push interleaved data and ensuring rigorous memory release (CVPixelBufferRelease and CFRelease), the application stabilized immediately. Memory consumption dropped to a flat baseline and the multiplexing phase completed seamlessly without throwing the -16364 error.
What Are The Key Lessons For Engineering Teams?
When bridging cross-platform frameworks with native iOS APIs, there are several foundational rules to follow. If you plan to hire Unity developers for mobile apps or require native modernization, ensure your teams adhere to these principles:
- Interleaving is Non-Negotiable: AVFoundation’s MP4 muxer requires chronological media appending. Never batch send an entire video stream followed by an entire audio stream.
- Respect isReadyForMoreMediaData: Always check if an
AVAssetWriterInputis ready before appending. Forcing data into a busy input will drop frames or crash the internal queue. - Manage CoreFoundation Memory Explicitly: Objective-C ARC (Automatic Reference Counting) does not manage CoreFoundation or CoreVideo objects. You must pair every
CreateorCopywith aCFReleaseorCVPixelBufferRelease. - Isolate Native Dependencies: Replacing unmaintained legacy plugins with direct OS API wrappers (like AVFoundation) guarantees long-term stability and compatibility with modern OS updates.
- Use Proper Time Scales: Ensure your
CMTimeresolution (e.g., 1,000,000,000) matches the precision of your application’s frame timestamps to prevent micro-desyncs between audio and video.
How Do We Wrap Up This AVFoundation Troubleshooting Journey?
Resolving the obscure -16364 error required looking beyond the error code itself and understanding the fundamental rules of media multiplexing. By redesigning the data pipeline to support time-based interleaving and rigorously managing native memory boundaries, we transformed an unstable legacy codebase into a highly performant media engine. When project complexities require deep domain expertise across mobile architectures, decision-makers often hire mobile app developers for custom media solutions who can bridge the gap between high-level application logic and low-level system APIs.
If your organization is struggling with legacy codebase modernization, native plugin failures or complex multimedia architecture, contact us to discuss how our dedicated engineering teams can deliver scalable, reliable solutions for your business.
Social Hashtags
#AVAssetWriter #AVFoundation #iOSDevelopment #iOSAppDevelopment #iOSDebugging #VideoProcessing #AudioVideoSync #MP4Encoding #ObjectiveC #UnityDevelopment #MobileAppDevelopment #SoftwareEngineering #iOSDevelopers #MediaProcessing #AppModernization
Frequently Asked Questions
Code -11800 is a generic "unknown error" wrapper in AVFoundation. The true cause is usually found in the NSUnderlyingError key within the user info dictionary, which typically maps to a specific OSStatus code like -16364.
AVAssetWriter multiplexes media into a container format (like MP4) and relies on chronological interleaving. If you send minutes of video without accompanying audio, the internal buffer overflows waiting for the matching audio timestamps, resulting in an allocation or multiplexing failure.
Because CVPixelBuffers are part of the CoreVideo C-API, they are not managed by ARC. Every buffer acquired via CVPixelBufferPoolCreatePixelBuffer must be explicitly released using CVPixelBufferRelease() once it has been appended to the writer.
No. While setting expectsMediaDataInRealTime = NO allows the writer to process data as fast as possible (offline rendering) rather than waiting for real-time clock pacing, it does not remove the strict requirement for time-sequenced interleaving of audio and video frames.
Legacy binary plugins are often abandoned and cannot be updated for new OS architectures or API deprecations. Writing a custom wrapper around native APIs like AVFoundation ensures you have full source control, easier debugging and guaranteed compatibility with future OS releases.
Success Stories That Inspire
See how our team takes complex business challenges and turns them into powerful, scalable digital solutions. From custom software and web applications to automation, integrations, and cloud-ready systems, each project reflects our commitment to innovation, performance, and long-term value.

California-based SMB Hired Dedicated Developers to Build a Photography SaaS Platform

Recruitment Operations Automation with Offshore Software Dev Team



















