[英]Audio Queue is playing too fast when the buffer size is small
我能夠使用音頻文件服務+音頻隊列服務流式傳輸和播放m4a文件。 由於文件類型,音頻隊列無法使用該文件的比特率信息。
下載完所有音頻數據包后,我將它們提供給播放器。
當我選擇一個大約32768或16384的緩沖區大小,因為回調被調用較少而且每個緩沖區大小很大,它似乎幾乎以常規速度播放。 問題有時我也要播放小文件但是當我選擇小型緩沖區大小-512或1024或2048到8192時 - 音頻播放速度非常快且偶爾出現故障。
我知道在c回調中調用objective-c函數不是一個好主意,但為了可讀性和易用性,我這樣做。 無論我認為這不是問題。
// allocate the buffers and prime the queue with some data before starting
AudioQueueBufferRef buffers[XMNumberPlaybackBuffers];
int i;
for (i = 0; i < XMNumberPlaybackBuffers; ++i)
{
err=AudioQueueAllocateBuffer(queue, XMAQDefaultBufSize, &buffers[i]);
if (err) {
[self failWithErrorCode:err customError:AP_AUDIO_QUEUE_BUFFER_ALLOCATION_FAILED];
}
@synchronized(self)
{
state=AP_WAITING_FOR_QUEUE_TO_START;
}
// manually invoke callback to fill buffers with data
MyAQOutputCallBack((__bridge void *)(self), queue, buffers[i]);
}
我還從一個可變的字典中獲取音頻包...
#define XMNumberPlaybackBuffers 4
#define XMAQDefaultBufSize 8192
#pragma mark playback callback function
static void MyAQOutputCallBack(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inCompleteAQBuffer)
{
// this is called by the audio queue when it has finished decoding our data.
// The buffer is now free to be reused.
NSLog(@"MyAQOutputCallBack..");
//printf("MyAQOutputCallBack...\n");
XMAudioPlayer* player = (__bridge XMAudioPlayer *)inUserData;
[player handleBufferCompleteForQueue:inAQ buffer:inCompleteAQBuffer];
//printf("##################\n");
}
- (void)handleBufferCompleteForQueue:(AudioQueueRef)inAQ
buffer:(AudioQueueBufferRef)inBuffer
{
//NSLog(@"######################\n");
AudioTimeStamp queueTime;
Boolean discontinuity;
err = AudioQueueGetCurrentTime(queue, NULL, &queueTime, &discontinuity);
printf("queueTime.mSampleTime %.2f\n",queueTime.mSampleTime/dataFormat.mSampleRate);
AudioStreamPacketDescription packetDescs[XMAQMaxPacketDescs]; // packet descriptions for enqueuing audio
BOOL isBufferFilled=NO;
size_t bytesFilled=0; // how many bytes have been filled
size_t packetsFilled=0; // how many packets have been filled
size_t bufSpaceRemaining;
while (isBufferFilled==NO && isEOF==NO) {
if (currentlyReadingBufferIndex<[sharedCache.audioCache count]) {
//loop thru untill buffer is enqued
if (sharedCache.audioCache) {
NSMutableDictionary *myDict= [[NSMutableDictionary alloc] init];
myDict=[sharedCache.audioCache objectAtIndex:currentlyReadingBufferIndex];
//why I cant use this info?
//UInt32 inNumberBytes =[[myDict objectForKey:@"inNumberBytes"] intValue];
UInt32 inNumberPackets =[[myDict objectForKey:@"inNumberPackets"] intValue];
NSData *convert=[myDict objectForKey:@"inInputData"];
const void *inInputData=(const char *)[convert bytes];
//AudioStreamPacketDescription *inPacketDescriptions;
AudioStreamPacketDescription *inPacketDescriptions= malloc(sizeof(AudioStreamPacketDescription));
NSNumber *mStartOffset = [myDict objectForKey:@"mStartOffset"];
NSNumber *mDataByteSize = [myDict objectForKey:@"mDataByteSize"];
NSNumber *mVariableFramesInPacket = [myDict objectForKey:@"mVariableFramesInPacket"];
inPacketDescriptions->mVariableFramesInPacket=[mVariableFramesInPacket intValue];
inPacketDescriptions->mStartOffset=[mStartOffset intValue];
inPacketDescriptions->mDataByteSize=[mDataByteSize intValue];
for (int i = 0; i < inNumberPackets; ++i)
{
SInt64 packetOffset = [mStartOffset intValue];
SInt64 packetSize = [mDataByteSize intValue];
//printf("packetOffset %lli\n",packetOffset);
//printf("packetSize %lli\n",packetSize);
currentlyReadingBufferIndex++;
if (packetSize > packetBufferSize)
{
//[self failWithErrorCode:AS_AUDIO_BUFFER_TOO_SMALL];
}
bufSpaceRemaining = packetBufferSize - bytesFilled;
//printf("bufSpaceRemaining %zu\n",bufSpaceRemaining);
// if the space remaining in the buffer is not enough for this packet, then enqueue the buffer.
if (bufSpaceRemaining < packetSize)
{
inBuffer->mAudioDataByteSize = (UInt32)bytesFilled;
err=AudioQueueEnqueueBuffer(inAQ,inBuffer,(UInt32)packetsFilled,packetDescs);
if (err) {
[self failWithErrorCode:err customError:AP_AUDIO_QUEUE_ENQUEUE_FAILED];
}
isBufferFilled=YES;
[self incrementBufferUsedCount];
return;
}
@synchronized(self)
{
//
// If there was some kind of issue with enqueueBuffer and we didn't
// make space for the new audio data then back out
//
if (bytesFilled + packetSize > packetBufferSize)
{
return;
}
// copy data to the audio queue buffer
//error -66686 refers to
//kAudioQueueErr_BufferEmpty = -66686
//memcpy((char*)inBuffer->mAudioData + bytesFilled, (const char*)inInputData + packetOffset, packetSize);
memcpy(inBuffer->mAudioData + bytesFilled, (const char*)inInputData + packetOffset, packetSize);
// fill out packet description
packetDescs[packetsFilled] = inPacketDescriptions[0];
packetDescs[packetsFilled].mStartOffset = bytesFilled;
bytesFilled += packetSize;
packetsFilled += 1;
free(inPacketDescriptions);
}
// if that was the last free packet description, then enqueue the buffer.
// size_t packetsDescsRemaining = kAQMaxPacketDescs - packetsFilled;
// if (packetsDescsRemaining == 0) {
//
// }
if (sharedCache.numberOfToTalPackets>0)
{
if (currentlyReadingBufferIndex==[sharedCache.audioCache count]-1) {
if (loop==NO) {
inBuffer->mAudioDataByteSize = (UInt32)bytesFilled;
lastEnqueudBufferSize=bytesFilled;
lastbufferPacketCount=(int)packetsFilled;
err=AudioQueueEnqueueBuffer(inAQ,inBuffer,(UInt32)packetsFilled,packetDescs);
if (err) {
[self failWithErrorCode:err customError:AP_AUDIO_QUEUE_ENQUEUE_FAILED];
}
printf("if that was the last free packet description, then enqueue the buffer\n");
//go to the next item on keepbuffer array
isBufferFilled=YES;
[self incrementBufferUsedCount];
return;
}
else
{
//if loop is yes return to first packet pointer and fill the rest of the buffer before enqueing it
//set the currently reading to zero
//check the space in buffer
//if space is avaialbele create a while loop till it is filled
//then enqueu the buffer
currentlyReadingBufferIndex=0;
}
}
}
}
}
}
}
}
#######################################
編輯:
對於將來訪問此內容的任何人,事實證明我的確切問題是AudioStreamPacketDescription packetDescs[XMAQMaxPacketDescs];
所以XMAQMaxPacketDescs
在這里是512,當我選擇更大的緩沖區大小我為每個緩沖區排列更接近的數據到512個數據包所以它以正常速度播放
然而,對於像1024這樣的小緩沖區大小,這總共只有2-3個數據包,因此508個數據包的其余部分為0,並且播放器試圖播放其中的所有數據包描述512這就是為什么它太快了。
我通過計算放入緩沖區的數據包總數來解決問題然后我創建了一個動態的AudioStreamPacketDescription
描述數組。
AudioStreamPacketDescription * tempDesc = (AudioStreamPacketDescription *)(malloc(packetsFilledDesc * sizeof(AudioStreamPacketDescription))); memcpy(tempDesc,packetDescs, packetsFilledDesc*sizeof(AudioStreamPacketDescription)); err = AudioQueueEnqueueBuffer(inAQ,inBuffer,packetsFilledDesc,tempDesc); if (err) { [self failWithErrorCode:err customError:AP_AUDIO_QUEUE_ENQUEUE_FAILED]; }
然而,我接受並獎勵了100分以下的DAVE答案,很快我意識到我的問題是不同的.....
當您為可變比特率分配隊列而不是使用XMAQDefaultBufSize時,對於可變比特率,您需要計算數據包大小。 我從拉的方法本教程由這本書,說明它是如何做。
void DeriveBufferSize (AudioQueueRef audioQueue, AudioStreamBasicDescription ASBDescription, Float64 seconds, UInt32 *outBufferSize)
{
static const int maxBufferSize = 0x50000; // punting with 50k
int maxPacketSize = ASBDescription.mBytesPerPacket;
if (maxPacketSize == 0)
{
UInt32 maxVBRPacketSize = sizeof(maxPacketSize);
AudioQueueGetProperty(audioQueue, kAudioConverterPropertyMaximumOutputPacketSize, &maxPacketSize, &maxVBRPacketSize);
}
Float64 numBytesForTime = ASBDescription.mSampleRate * maxPacketSize * seconds;
*outBufferSize = (UInt32)((numBytesForTime < maxBufferSize) ? numBytesForTime : maxBufferSize);
}
你會像這樣使用它。
Float64 bufferDurSeconds = 0.54321;
AudioStreamBasicDescription myAsbd = self.format; // or something
UInt32 bufferByteSize;
DeriveBufferSize(recordState.queue, myAsbd, bufferDurSeconds, &bufferByteSize);
AudioQueueAllocateBuffer(queue, bufferByteSize, &buffers[i]);
使用kAudioConverterPropertyMaximumOutputPacketSize,可以計算可安全用於不可預測的可變比特率文件的最小緩沖區大小。 如果您的文件太小,您只需要確定哪些樣本填充了編解碼器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.