开发者

Playback MP3 data buffer using AudioQueue Service : Prime failed (-50)

These days I was struggling for finding the way to playback network audio stream (in MP3 packet format) using AudioQueue Service on iPhone.

To achieve this target successively, I firstly got the common local MP3 file to playback using AudioQueue, and it does work. And then, I replaced the AudioFileReadPackets with stdio function fread, and everytime I fread the same amount of mp3 packets like AudioFileReadPackets ever did, to simulate the audio stream coming from network. However, this time, the error came:

2011-09-28 14:21:28.245 SunFlower[1554:207] Prime: Exiting because mConverterError is -50 (0x11940 req, 0x0 primed)
2011-09-28 14:21:28.253 SunFlower[1554:207] Prime failed (-50); will stop (72000/0 frames)

Does someone know what's the cause for this "Prime failed (-50)" error? Help zzzzzzz

*********************************************************************************************************************************************************************.

To explain what I did on the replacing work, I’d like to show you the main changed part of the code, there are two part: "before replacing" and "after replacing"

  1. Before replacing (playbak local MP3 file):

1) The audio queue runloop callback

static void HandleOutputBuffer(void                *aqData, 
                               AudioQueueRef       inAQ, 
                               AudioQueueBufferRef inBuffer) 
{
    AQPlayerState *pAqData = (AQPlayerState *) aqData; 
    UInt32 numBytesReadFromFile;
    UInt32 numPackets = pAqData->mNumPacketsToRead;
    UInt32 i = 0;

    printf("HandleOutputBuffer::Start!\n");
    //If the audio queue is stopped, returns immediately
    if(pAqData->mIsRunning == 0) 
    {
        printf("HandleOutputBuffer::Error Return!\n");
        return;
    }

    //Read a packet of audio data from file stream
    AudioFileReadPackets(pAqData->mAudioFile,
                         false,
                         &numBytesReadFromFile,
                         pAqData->mPacketDescs,
                         pAqData->mCurrentPacket,
                         &numPackets,
                         inBuffer->mAudioData);

    //Enqueue the audio packet into the audio queue
    if(numPackets > 0) 
    {
        printf("HandleOutputBuffer::Step 1!\n");
        inBuffer->mAudioDataByteSize = numBytesReadFromFile;
        AudioQueueEnqueueBuffer(pAqData->mQueue,
                                inBuffer,
                                (pAqData->mPacketDescs ? numPackets : 0),
                                pAqData->mPacketDescs);

        pAqData->mCurrentPacket += numPackets;
    }
    else
    {
        printf("HandleOutputBuffer::Step 2!\n");
        AudioQueueStop(pAqData->mQueue,
                       false);
        pAqData->mIsRunning = false;
    }
}

2) Open audio file

OSStatus AqOpenAudioFile(char *filePath, AQPlayerState *pAqData)
{
    CFURLRef audioFileURL;
    OSStatus result;
    UInt32 maxPacketSize;
    UInt32 propertySize = sizeof (maxPacketSize);

    audioFileURL = 
        CFURLCreateFromFileSystemRepresentation(NULL,
                                                (const UInt8 *) filePath,
                                                strlen (filePath),
                                                false);

    result = AudioFileOpenURL(audioFileURL,
                              kAudioFileReadPermission,
                              0,
                              &(pAqData->mAudioFile));
    CFRelease(audioFileURL);

    AudioFileGetProperty(pAqData->mAudioFile,
                         kAudioFilePropertyPacketSizeUpperBound,
                         &propertySize,
                         &maxPacketSize);

    DeriveBufferSize(pAqData->mDataFormat,
                     maxPacketSize,
                     0.5,
                     &(pAqData->bufferByteSize),
                     &(pAqData->mNumPacketsToRead));

    return result;
}

3) Create audio queue

OSStatus AqCreateAudioQueue(AQPlayerState *pAqData)
{
    UInt32 dataFormatSize = sizeof (AudioStreamBasicDescription);
    OSStatus result;
    bool isFormatVBR;
    UInt32 cookieSize = sizeof (UInt32);
    bool couldNotGetProperty;

    AudioFileGetProperty(pAqData->mAudioFile,
                         kAudioFilePropertyDataFormat,
                         &dataFormatSize,
                         &(pAqData->mDataFormat));

    result = AudioQueueNewOutput(&(pAqData->mDataFormat),
                                 HandleOutputBuffer,
                                 pAqData,
                                 CFRunLoopGetCurrent(),
                                 kCFRunLoopCommonModes,
                                 0,
                                 &(pAqData->mQueue));

    //Configurate the VBR property if any
    isFormatVBR = (pAqData->mDataFormat.mBytesPerPacket == 0 || 
                   pAqData->mDataFormat.mFramesPerPacket == 0);

    if(isFormatVBR)
    {
        pAqData->mPacketDescs = 
            (AudioStreamPacketDescription*)malloc(pAqData->mNumPacketsToRead * sizeof(AudioStreamPacketDescription));
    }
    else
    {
        pAqData->mPacketDescs = NULL;
    }

    //Set Metadata for Audio Queue
    couldNotGetProperty = 
        AudioFileGetPropertyInfo(pAqData->mAudioFile,
                                 kAudioFilePropertyMagicCookieData,
                                 &cookieSize,
                                 NULL);

    if (!couldNotGetProperty && cookieSize)
    {
        char* magicCookie = (char *)malloc(cookieSize);
        AudioFileGetProperty(pAqData->mAudioFile,
                             kAudioFilePropertyMagicCookieData,
                             &cookieSize,
                             magicCookie);

        AudioQueueSetProperty(pAqData->mQueue,
                              kAudioQueueProperty_MagicCookie,
                              magicCookie,
                              cookieSize);
        free(magicCookie);
    }

    //Set the playback gain
    AudioQueueSetParameter(pAqData->mQueue,
                           kAudioQueueParam_Volume,
                           AQ_PLAYBACK_GAIN);    
}

2. After replacing (playbak mp3 data buffer get by fread):

In order to make the code porting smoothly, I copied the run-time value of critical variables, like pAqData->bufferByteSize, pAqData->mNumPacketsToRead, pAqData->mDataFormat…etc. And directly initialize these variables using the copied value in the replaced code. The in开发者_JAVA技巧tent of this behavior is to discard involking the interfaces of AudioToolbox framework like: AudioFileOpenURL, AudioFileGetProperty, AudioFileReadPackets… And then, we can use the stdio function fread to get the mp3 packet directly. The changed code is shown below:

1) The audio queue runloop callback (In previous code, the AudioFileReadPackets read 338 packets, and totally 129792 bytes, I copied these values directly into the replaced code)

static void HandleOutputBuffer(void                *aqData, 
                               AudioQueueRef       inAQ, 
                               AudioQueueBufferRef inBuffer) 
{
    AQPlayerState *pAqData = (AQPlayerState *) aqData; 
    UInt32 numBytesReadFromFile;
    UInt32 numPackets = pAqData->mNumPacketsToRead;
    UInt32 i = 0;

    printf("HandleOutputBuffer::Start!\n");
    //If the audio queue is stopped, returns immediately
    if(pAqData->mIsRunning == 0) 
    {
        printf("HandleOutputBuffer::Error Return!\n");
        return;
    }

    //Read a packet of audio data using fread
    memset(audio_buffer, 0, 327680);
    memset(inBuffer->mAudioData, 0, 327680);
    pAqData->mPacketDescs->mStartOffset = 0;
    pAqData->mPacketDescs->mVariableFramesInPacket = 0;
    pAqData->mPacketDescs->mDataByteSize = 384;

    numBytesReadFromFile = fread(audio_buffer, sizeof(uint8_t), 129792, source_file);
    numPackets = 338;
    memcpy(inBuffer->mAudioData, audio_buffer, 327680);

    //Enqueue the audio packet into the audio queue
    if(numPackets > 0) 
    {
        printf("HandleOutputBuffer::Step 1!\n");
        inBuffer->mAudioDataByteSize = numBytesReadFromFile;
        AudioQueueEnqueueBuffer(pAqData->mQueue,
                                inBuffer,
                                (pAqData->mPacketDescs ? numPackets : 0),
                                pAqData->mPacketDescs);

        pAqData->mCurrentPacket += numPackets;
    }
    else
    {
        printf("HandleOutputBuffer::Step 2!\n");
        AudioQueueStop(pAqData->mQueue,
                       false);
        pAqData->mIsRunning = false;
    }
}

2) Open audio file(use fopen to replace AudioFileOpenURL)

OSStatus AqOpenAudioFile(char *filePath, AQPlayerState *pAqData)
{
    CFURLRef audioFileURL;
    OSStatus result;
    UInt32 maxPacketSize;
    UInt32 propertySize = sizeof (maxPacketSize);

    source_file = fopen(filePath, "r");

    memset(audio_buffer, 0, 327680);
    fread(audio_buffer, sizeof(uint8_t), 32, source_file);

    pAqData->bufferByteSize = 327680;
    pAqData->mNumPacketsToRead = 338;

    return result;
}

3) Create audio queue(Directly initialize the pAqData->mDataFormat with the value assigned in local MP3 playback mode)

OSStatus AqCreateAudioQueue(AQPlayerState *pAqData)
{
    UInt32 dataFormatSize = sizeof (AudioStreamBasicDescription);
    OSStatus result;
    bool isFormatVBR;
    UInt32 cookieSize = sizeof (UInt32);
    bool couldNotGetProperty;


    memset(&(pAqData->mDataFormat), 0, sizeof(AudioStreamBasicDescription));
    pAqData->mDataFormat.mSampleRate = 48000;
    pAqData->mDataFormat.mFormatID = 778924083;//mp3 ID
    pAqData->mDataFormat.mFramesPerPacket = 1152;
    pAqData->mDataFormat.mChannelsPerFrame = 2;


    result = AudioQueueNewOutput(&(pAqData->mDataFormat),
                                 HandleOutputBuffer,
                                 pAqData,
                                 CFRunLoopGetCurrent(),
                                 kCFRunLoopCommonModes,
                                 0,
                                 &(pAqData->mQueue));

    //Configurate the VBR property if any
    isFormatVBR = (pAqData->mDataFormat.mBytesPerPacket == 0 || 
                   pAqData->mDataFormat.mFramesPerPacket == 0);

    if(isFormatVBR)
    {
        pAqData->mPacketDescs = 
            (AudioStreamPacketDescription*)malloc(pAqData->mNumPacketsToRead * 
                                              sizeof(AudioStreamPacketDescription));
    }
    else
    {
        pAqData->mPacketDescs = NULL;
    }

    //Set the playback gain
    AudioQueueSetParameter(pAqData->mQueue,
                           kAudioQueueParam_Volume,
                           AQ_PLAYBACK_GAIN);    
}


Guys:

I found the root cause!

The problem came from the function HandleOutputBuffer (The changed one)!

Because every time, the function fread 338 packets of mp3 data (not just 1 packet), therefore, [pAqData->mPacketDescs] is not a single variable, it's actually an array with the size of 338 array items. So, we have to initialize the all 338 array items.

So, we need to change the code:

static void HandleOutputBuffer(void                *aqData, 
                               AudioQueueRef       inAQ, 
                               AudioQueueBufferRef inBuffer) 
{
    ...
    pAqData->mPacketDescs->mStartOffset = 0;
    pAqData->mPacketDescs->mVariableFramesInPacket = 0;
    pAqData->mPacketDescs->mDataByteSize = 384;
    ...
}

into

static void HandleOutputBuffer(void                *aqData, 
                               AudioQueueRef       inAQ, 
                               AudioQueueBufferRef inBuffer) 
{
    ...
    for (i = 0; i < 338; i++) 
    {
        pAqData->mPacketDescs[i].mStartOffset = PACKET_SIZE * i;
        pAqData->mPacketDescs[i].mVariableFramesInPacket = 0;
        pAqData->mPacketDescs[i].mDataByteSize = PACKET_SIZE;
    }
    ...
}

Finally, the problem fixed!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜