ios AudioConverterFillComplexBuffer返回561015652(!pkd)?

waxmsbnn  于 2023-06-25  发布在  iOS
关注(0)|答案(1)|浏览(141)

我使用AudioConverter将aac转换为pcm,从服务器接收,AudioConverterFillComplexBuffer总是返回'!pkd'错误,如何设置ioOutputDataPacketSize参数?我的错误在哪里?谢谢大家!

- (void)createAACDecoder {
    if(aacDecoder!=nil) {
        return;
    }

    AudioStreamBasicDescription srcASBD = {0};
    srcASBD.mSampleRate = 44100;
    srcASBD.mFormatID = kAudioFormatMPEG4AAC;
    srcASBD.mFormatFlags = kMPEG4Object_AAC_Main;
    srcASBD.mChannelsPerFrame = 1;

    AudioStreamBasicDescription dstASBD = {0};
    dstASBD.mSampleRate = 44100;
    dstASBD.mFormatID = kAudioFormatLinearPCM;
    dstASBD.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
    dstASBD.mFramesPerPacket = 1;
    dstASBD.mBitsPerChannel = 16;
    dstASBD.mChannelsPerFrame = 1;
    dstASBD.mBytesPerFrame = dstASBD.mBytesPerPacket = (dstASBD.mBitsPerChannel / 8) *dstASBD.mChannelsPerFrame;

    UInt32 size = sizeof(srcASBD);
    OSStatus error = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo, 0, NULL, &size, &srcASBD);
    if(error!=noErr) {
         NSLog(@"couldn't create dec destination data format");
    }

    AudioClassDescription *desc = [self getAudioClassDescriptionWithType:kAudioFormatLinearPCM fromManufacturer:kAppleSoftwareAudioCodecManufacturer];

    if (AudioConverterNewSpecific(&srcASBD, &dstASBD, 1, desc, &aacDecoder) != noErr) {
        NSLog(@"AudioConverterNewSpecific dec failed");
    }
}

- (AudioClassDescription*)getAudioClassDescriptionWithType:(UInt32)type fromManufacturer:(UInt32)manufacturer {
    static AudioClassDescription audioDesc;

    UInt32 encoderSpecifier = type, size = 0;
    OSStatus status;

    memset(&audioDesc, 0, sizeof(audioDesc));
    status = AudioFormatGetPropertyInfo(kAudioFormatProperty_Encoders, sizeof(encoderSpecifier), &encoderSpecifier, &size);
    if (status) {
        return nil;
    }

    uint32_t count = size / sizeof(AudioClassDescription);
    AudioClassDescription descs[count];
    status = AudioFormatGetProperty(kAudioFormatProperty_Encoders, sizeof(encoderSpecifier), &encoderSpecifier, &size, descs);
    for (uint32_t i = 0; i < count; i++) {
        if ((type == descs[i].mSubType) && (manufacturer == descs[i].mManufacturer)) {
            memcpy(&audioDesc, &descs[i], sizeof(audioDesc));
            break;
        }
    }
    return &audioDesc;
   }

OSStatus outInputDataProc(AudioConverterRef inConverter, UInt32 *ioNumberDataPackets, AudioBufferList *ioData,AudioStreamPacketDescription **outDataPacketDescription, void *inUserData) {
    AudioBufferList inBufferList = *(AudioBufferList *)inUserData;
    ioData->mNumberBuffers = 1;
    ioData->mBuffers[0].mNumberChannels = 1;
    ioData->mBuffers[0].mData = inBufferList.mBuffers[0].mData;
    ioData->mBuffers[0].mDataByteSize = inBufferList.mBuffers[0].mDataByteSize;
    *ioNumberDataPackets = 1;
    return noErr;
}

- (NSData *)startAACDecode:(NSData *)aacData {
    NSInteger sampleLen = [aacData length];
    char *samples = malloc(sizeof(char)*sampleLen);
    [aacData getBytes:samples length:sampleLen];

    AudioBufferList inBufferList;
    inBufferList.mNumberBuffers = 1;
    inBufferList.mBuffers[0].mNumberChannels = 1;
    inBufferList.mBuffers[0].mDataByteSize = sampleLen;
    inBufferList.mBuffers[0].mData = samples;

    AudioBufferList outBufferList;
    outBufferList.mNumberBuffers = 1;
    outBufferList.mBuffers[0].mNumberChannels = 1;
    outBufferList.mBuffers[0].mDataByteSize = sampleLen;
    outBufferList.mBuffers[0].mData = samples;

    UInt32 outputDataPacketSize               = 1;

    OSStatus error = AudioConverterFillComplexBuffer(aacDecoder, outInputDataProc, &inBufferList, &outputDataPacketSize, &outBufferList, NULL);
    if(error!= noErr)
    {
        NSLog(@"AudioConverterFillComplexBuffer dec failed");
        return nil;
    }

    NSData *pcmData = [NSData dataWithBytes:outBufferList.mBuffers[0].mData length:outBufferList.mBuffers[0].mDataByteSize];;
    return pcmData;
}
z2acfund

z2acfund1#

Use应填写outInputDataProc()的outDataPacketDescription。

相关问题