音频队列缓冲区空错误

时间:2015-05-19 15:30:42

标签: ios objective-c buffer memcpy audioqueue

我分配缓冲区并启动音频队列,如

// 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,

enter image description here

1 个答案:

答案 0 :(得分:1)

在获取缓冲区之前填充缓冲区的mAudioDataByteSize解决了问题

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