我有一个奇怪的内存“泄漏” AVAssetWriterInput appendSampleBuffer
。我同时写视频和音频,所以我有一个AVAssetWriter
有两个输入,一个用于视频,一个用于音频:
self.videoWriter = [[[AVAssetWriter alloc] initWithURL:[self.currentVideo currentVideoClipLocalURL]
fileType:AVFileTypeMPEG4
error:&error] autorelease];
...
self.videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings];
self.videoWriterInput.expectsMediaDataInRealTime = YES;
[self.videoWriter addInput:self.videoWriterInput];
...
self.audioWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio
outputSettings:audioSettings];
self.audioWriterInput.expectsMediaDataInRealTime = YES;
[self.videoWriter addInput:self.audioWriterInput];
我开始写作,表面上一切正常。视频和音频被写入并对齐等。但是,我将代码放入分配工具并注意到以下内容:
音频字节被保留在内存中,我稍后会证明。这就是内存的增长。音频字节仅在我调用 后才被释放[self.videoWriter endSessionAtSourceTime:...]
,您认为这是内存使用量的急剧下降。这是我的音频编写代码,它作为一个块发送到串行队列中:
@autoreleasepool
{
// The objects that will hold the audio data
CMSampleBufferRef sampleBuffer;
CMBlockBufferRef blockBuffer1;
CMBlockBufferRef blockBuffer2;
size_t nbytes = numSamples * asbd_.mBytesPerPacket;
OSStatus status = noErr;
status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,
data,
nbytes,
kCFAllocatorNull,
NULL,
0,
nbytes,
kCMBlockBufferAssureMemoryNowFlag,
&blockBuffer1);
if (status != noErr)
{
NLog(@"CMBlockBufferCreateWithMemoryBlock error at buffer 1");
return;
}
status = CMBlockBufferCreateContiguous(kCFAllocatorDefault,
blockBuffer1,
kCFAllocatorDefault,
NULL,
0,
nbytes,
kCMBlockBufferAssureMemoryNowFlag | kCMBlockBufferAlwaysCopyDataFlag,
&blockBuffer2);
if (status != noErr)
{
NSLog(@"CMBlockBufferCreateWithMemoryBlock error at buffer 2");
CFRelease(blockBuffer1);
return;
}
// Finally, create the CMSampleBufferRef
status = CMAudioSampleBufferCreateWithPacketDescriptions(kCFAllocatorDefault,
blockBuffer2,
YES, // Yes data is ready
NULL, // No callback needed to make data ready
NULL,
audioFormatDescription_,
1,
timestamp,
NULL,
&sampleBuffer);
if (status != noErr)
{
NSLog(@"CMAudioSampleBufferCreateWithPacketDescriptions error.");
CFRelease(blockBuffer1);
CFRelease(blockBuffer2);
return;
}
if ([self.audioWriterInput isReadyForMoreMediaData])
{
if (![self.audioWriterInput appendSampleBuffer:sampleBuffer])
{
NSLog(@"Couldn't append audio sample buffer: %d", numAudioCallbacks_);
}
} else {
NSLog(@"AudioWriterInput isn't ready for more data.");
}
// One release per create
CFRelease(blockBuffer1);
CFRelease(blockBuffer2);
CFRelease(sampleBuffer);
}
如您所见,每次创建时我都会释放每个缓冲区一次。我已经将“泄漏”追溯到附加音频缓冲区的行:
[self.audioWriterInput appendSampleBuffer:sampleBuffer]
我通过注释掉该行向自己证明了这一点,之后我得到了以下“无泄漏”分配图(当然,现在录制的视频现在没有音频了):
我尝试了另一件事,即加回该appendSamplebuffer
行,而不是 double-release blockBuffer2
:
CFRelease(blockBuffer1);
CFRelease(blockBuffer2);
CFRelease(blockBuffer2); // Double release to test the hypothesis that appendSamplebuffer is retaining this
CFRelease(sampleBuffer);
这样做不会导致双重释放,表明blockBuffer2
此时的保留计数为 2。这产生了相同的“无泄漏”分配图,除了当我调用 时[self.videoWriter endSessionAtSourceTime:...]
,我从双重释放中崩溃了release (表示self.videoWriter
正在尝试释放其所有指向blockBuffer2
已传入的 s 的指针)。
如果相反,我尝试以下方法:
CFRelease(blockBuffer1);
CFRelease(blockBuffer2);
CMSampleBufferInvalidate(sampleBuffer); // Invalidate sample buffer
CFRelease(sampleBuffer);
然后[self.audioWriterInput appendSampleBuffer:sampleBuffer]
,在此之后的每个呼叫都开始附加视频帧的呼叫失败。
所以我的结论是,AVAssetWriter
或者在视频完成录制之前一直AVAssetWriterInput
保留。blockBuffer2
显然,如果视频录制时间足够长,这可能会导致真正的内存问题。难道我做错了什么?
编辑:我得到的音频字节是 PCM 格式,而我正在编写的视频格式是 MPEG4,而该视频的音频格式是 MPEG4AAC。视频编写器是否有可能正在执行 PCM --> AAC 格式,这就是它被缓冲的原因?