源码位置
webrtc/webrtc/modules/audio_device/ios/audio_device_ios.cc
函数
OSStatus
AudioDeviceIPhone::RecordProcessImpl(
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
uint32_t inBusNumber,
uint32_t inNumberFrames)
{
...........
while (bufPos < N_REC_BUFFERS)
{
if ((_recordingLength[bufPos] > 0) && (_recordingLength[bufPos] < noSamp10ms))
{
// Found the partially full buffer
insertPos = static_cast<int16_t>(bufPos);
// Don't need to search more, quit loop
bufPos = N_REC_BUFFERS;
}
else if ((-1 == insertPos) && (0 == _recordingLength[bufPos]))
{
// Found an empty buffer
insertPos = static_cast<int16_t>(bufPos);
}
++bufPos;
}
...............
}
一开始阅读这段函数时,简单理解就是查找未使用,或者未完全填满的槽位,理解是理解了但感觉有点效率不高,故优化了下,结果理解就出了问题.
OSStatus
AudioDeviceIPhone::RecordProcessImpl(
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
uint32_t inBusNumber,
uint32_t inNumberFrames)
{
...........
bufPos = 0;
insertPos = -1;
nCopy = 0;
//查找未使用,或者未完全填满的槽位
while (bufPos < N_REC_BUFFERS)
{
if ((_recordingLength[bufPos] > 0) && (_recordingLength[bufPos] < noSamp10ms))
{
// Found the partially full buffer
insertPos = static_cast<int16_t>(bufPos);
// Don't need to search more, quit loop
break;
}
else if ((-1 == insertPos) && (0 == _recordingLength[bufPos]))
{
insertPos = static_cast<int16_t>(bufPos);
break;
}
++bufPos;
}
...............
}
相比原版,增加了两个break,结果仔细思考发现,第二个break是不对的.找到空槽位之后,仍然要考虑遍历下有没有未填满的槽位.
OSStatus
AudioDeviceIPhone::RecordProcessImpl(
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
uint32_t inBusNumber,
uint32_t inNumberFrames)
{
...........
bufPos = 0;
insertPos = -1;
nCopy = 0;
//查找未使用,或者未完全填满的槽位
while (bufPos < N_REC_BUFFERS)
{
if ((_recordingLength[bufPos] > 0) && (_recordingLength[bufPos] < noSamp10ms))
{
// Found the partially full buffer
insertPos = static_cast<int16_t>(bufPos);
// Don't need to search more, quit loop
break;
}
else if ((-1 == insertPos) && (0 == _recordingLength[bufPos]))
{
// Found an empty buffer,找到空槽后,再继续找长度不足的槽位,优先使用
insertPos = static_cast<int16_t>(bufPos);
}
++bufPos;
}
...............
}
修正后OK了,这还是要仔细阅读,理解清楚了需求.如果使用链表,就不会有问题了.