簡體   English   中英

音頻隊列緩沖區為空錯誤

[英]Audio Queue Buffer Empty Error

我分配緩沖區並啟動音頻隊列,例如

// allocate the buffers and prime the queue with some data before starting
AudioQueueBufferRef buffers[kNumberPlaybackBuffers];
isDone = false;
packetPosition = 0;
int i;
for (i = 0; i < kNumberPlaybackBuffers; ++i)
{
    CheckError(AudioQueueAllocateBuffer(queue, packetBufferSize, &buffers[i]), "AudioQueueAllocateBuffer failed");

    // manually invoke callback to fill buffers with data
    MyAQOutputCallBack((__bridge void *)(self), queue, buffers[i]);

    // EOF (the entire file's contents fit in the buffers)
    if (isDone)
        break;
}

// start the queue. this function returns immedatly and begins
// invoking the callback, as needed, asynchronously.
CheckError(AudioQueueStart(queue, NULL), "AudioQueueStart failed");

上面的代碼成功調用了outputcallback函數

#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.
    printf("MyAQOutputCallBack...\n");
    printf("######################\n");
    AnotherPlayer* player = (__bridge AnotherPlayer *)inUserData;
    [player handleBufferCompleteForQueue:inAQ buffer:inCompleteAQBuffer];
}

它調用了Objective-C函數,我在其中填充緩沖區並將其排隊。

- (void)handleBufferCompleteForQueue:(AudioQueueRef)inAQ
                              buffer:(AudioQueueBufferRef)inBuffer
{
    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 ) {
        if (currentlyReadingBufferIndex<[sharedCache.baseAudioCache count]) {

            printf("currentlyReadingBufferIndex %i\n",currentlyReadingBufferIndex);
            //loop thru untill buffer is enqued
            if (sharedCache.baseAudioCache) {

                NSMutableDictionary *myDict= [[NSMutableDictionary alloc] init];
                myDict=[sharedCache.baseAudioCache objectAtIndex:currentlyReadingBufferIndex];

                //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)
                    {
                        CheckError(AudioQueueEnqueueBuffer(inAQ,
                                                           inBuffer,
                                                           packetsFilled,
                                                           packetDescs), "AudioQueueEnqueueBuffer failed");

                        //                        OSStatus status = AudioQueueEnqueueBuffer(inAQ,
                        //                                                                  fillBuf,
                        //                                                                  packetsFilled,
                        //                                                                  packetDescs);
                        //                        if (status) {
                        //                            // This is also not called.
                        //                            NSLog(@"Error enqueueing buffer %d", (int)status);
                        //                        }

                        //printf("bufSpaceRemaining < packetSize\n");
                        //go to the next item on keepbuffer array
                        isBufferFilled=YES;
                    }
                    @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, (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) {
                        CheckError(AudioQueueEnqueueBuffer(inAQ,
                                                           inBuffer,
                                                           packetsFilled,
                                                           packetDescs), "AudioQueueEnqueueBuffer 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;
                    }

                }

            }
        }
        else
        {
            isDone=YES;
        }
    }

}

在這里,一旦緩沖區已滿,我就調用AudioQueueEnqueueBuffer ,我認為問題可能是memcpy(但通過斷點,緩沖區中似乎有一些數據,但它仍然給了我

錯誤:AudioQueueEnqueueBuffer失敗(-66686)這意味着在AudioQueue.h kAudioQueueErr_BufferEmpty = -66686,

在此處輸入圖片說明

在對緩沖區進行填充之前填充緩沖區的mAudioDataByteSize解決了問題

inBuffer->mAudioDataByteSize = bytesFilled;
                        CheckError(AudioQueueEnqueueBuffer(inAQ,
                                                           inBuffer,
                                                           packetsFilled,
                                                           packetDescs), "AudioQueueEnqueueBuffer failed");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM