音频队列缓冲区为空错误

gen

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

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

在此处输入图片说明

gen

在对缓冲区进行填充之前填充缓冲区的mAudioDataByteSize解决了问题

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Flatbuffer缓冲区始终为空

来自分类Dev

ffmpeg:将音频与混合音频添加到叠加过滤的视频时,会出现缓冲区队列溢出错误

来自分类Dev

缓冲区较小时,音频队列的播放速度过快

来自分类Dev

如何检查缓冲区是否为空?

来自分类Dev

检查stdin缓冲区是否为空

来自分类Dev

从QTcpSocket读取时为空缓冲区

来自分类Dev

ReactJS简单图像上传缓冲区显示为空

来自分类Dev

Vim:如果缓冲区列表为空,则退出

来自分类Dev

从QTcpSocket读取时为空缓冲区

来自分类Dev

Ubuntu文件系统缓冲区/缓存始终为空

来自分类Dev

缓冲区为空!:无法分配内存

来自分类Dev

RECV缓冲区为空,但返回值> 1

来自分类Dev

Superagent .attach() 文件或缓冲区数据为空

来自分类Dev

如何检查输入缓冲区是否为空?

来自分类Dev

在ObjectiveC中串联音频缓冲区

来自分类Dev

如何从音频标签创建音频缓冲区?

来自分类Dev

为什么录制音频5秒后出现错误“缓冲区已满”?

来自分类Dev

用ajax上传数组缓冲区(音频缓冲区)的最佳方法

来自分类Dev

用ajax上传数组缓冲区(音频缓冲区)的最佳方法

来自分类Dev

FBO深度缓冲区为红色

来自分类Dev

对RxJava中的缓冲区的错误理解

来自分类Dev

zlib膨胀返回缓冲区错误

来自分类Dev

输出缓冲区错误处理

来自分类Dev

gtk文本缓冲区错误

来自分类Dev

UDP接收缓冲区错误

来自分类Dev

输出缓冲区错误处理

来自分类Dev

预期的字符缓冲区对象错误

来自分类Dev

BufferedImage错误的缓冲区大小

来自分类Dev

类型错误:数据必须是缓冲区