ios - 从麦克风捕获音频时,我陷入内存泄漏或音频中的过度静态之间
<p><p>我正在编写一个 iPhone 应用程序,我需要从麦克风捕获音频并将其以 AAC 格式流式传输到流媒体服务器。所以我先捕获音频,然后使用 </p>
<pre><code>AudioConverterFillComplexBuffer
</code></pre>
<p>将音频转换为 AAC 的方法。 </p>
<p>下面是代码</p>
<pre><code>- (void) captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
NSArray *audioChannels = connection.audioChannels;
if (audioChannels == nil || ==0) {
// NSLog(@"We have Video Frame");
;
}else{
// NSLog(@"We have Audio Frame");
if (hasAudio) {
CMTime prestime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
double dPTS = (double)(prestime.value) / prestime.timescale;
;
// describe output data buffers into which we can receive data.
AudioBufferList outputBufferList;
outputBufferList.mNumberBuffers = 1;
outputBufferList.mBuffers.mNumberChannels = _aacASBD.mChannelsPerFrame;
outputBufferList.mBuffers.mDataByteSize = _aacBufferSize;
outputBufferList.mBuffers.mData = _aacBuffer;
OSStatus st = AudioConverterFillComplexBuffer(_converter, &putPcmSamplesInBufferList, (__bridge void *) self, &_numOutputPackets, &outputBufferList, NULL);
if (0 == st) {
.mDataByteSize :dPTS];
}else{
NSLog(@"Error converting Buffer");
NSError *error = ;
NSLog( );
char * str = new char;
FormatError(str, st);
}
if (_blockBuffer) // Double check that what you are releasing actually exists!
{
CFRelease(_blockBuffer);
}
}
}
</code></pre>
<p>代码
getAudioBufferDataFromCMSampleBufferRef
如下</p>
<pre><code>- (AudioBuffer) getAudioBufferDataFromCMSampleBufferRef: (CMSampleBufferRef)audioSampleBuffer{
AudioBufferList audioBufferList;
OSStatus err = CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(audioSampleBuffer, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &_blockBuffer);
AudioBuffer audioBuffer ;
if (!err && _blockBuffer && audioBufferList.mBuffers.mData && (audioBufferList.mBuffers.mDataByteSize > 0))
{
for( int y=0; y<audioBufferList.mNumberBuffers; y++ )
{
audioBuffer = audioBufferList.mBuffers;
break;
}
}
inputBuffer.mData=audioBuffer.mData;
inputBuffer.mDataByteSize=audioBuffer.mDataByteSize;
inputBuffer.mNumberChannels=1;
return audioBuffer;
}
</code></pre>
<p>在上面的代码版本中,我收到了 BAD_ACCESS 错误。相反,如果我删除了释放 blockBuffer 的代码,则会出现内存泄漏,并且应用程序最终会因为内存压力而终止。</p>
<p>如果我不保留blockBuffer而写的代码</p>
<pre><code> getAudioBufferDataFromCMSampleBufferRef
</code></pre>
<p>下面给出的不同</p>
<pre><code>- (AudioBuffer) getAudioBufferDataFromCMSampleBufferRef: (CMSampleBufferRef)audioSampleBuffer
{
_blockBuffer = CMSampleBufferGetDataBuffer(audioSampleBuffer);
int audioBufferByteSize = CMSampleBufferGetTotalSampleSize(audioSampleBuffer);
CMBlockBufferCopyDataBytes(_blockBuffer,0,audioBufferByteSize,inputBuffer.mData);
inputBuffer.mDataByteSize=audioBuffer.mDataByteSize;
inputBuffer.mNumberChannels=1;
}
</code></pre>
<p>在这个版本中, block 缓冲区没有被保留,所以不需要释放它。但是现在我在音频中得到了可怕的静电。 </p>
<p>有人知道如何解决这个问题吗?</p>
<p>谢谢,
奥兹古尔</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>通常这样做的一种方法是将所有音频样本数据从未保留的缓冲区复制到您自己的(预分配和保留的、无锁的)循环 fifo 缓冲区中。然后忽略未保留的缓冲区,因为它会在某个时候被释放。使用您自己的循环缓冲区中的音频数据。</p></p>
<p style="font-size: 20px;">关于ios - 从麦克风捕获音频时,我陷入内存泄漏或音频中的过度静态之间,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31433442/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31433442/
</a>
</p>
页:
[1]