2017-03-22 4 views
5

私のアプリケーションでSIP呼び出しを実装したいのですが、解決する必要がある最初の問題は、ADTSヘッダーを含む圧縮AACフォーマットからリニアPCMにオーディオを変換することです。AudioConverterFillComplexBufferを使用してAAC圧縮フレームをPCMにデコードする方法iOS

私の入力データは、フレームサイズの異なるADTSフレームのNSArrayです。各フレームはNSMutableDataの型です。各フレームのフォーマットとサンプルレートは同じですが、違いはフレームサイズだけです。

Igor Rotaruが提案したthis issueのサンプルコードを実装しようとしましたが、動作させることができません。

私のコードは次のようになります。その後

- (void)configureAudioConverter { 
    AudioStreamBasicDescription inFormat; 
    memset(&inFormat, 0, sizeof(inFormat)); 
    inputFormat.mBitsPerChannel = 0; 
    inputFormat.mBytesPerFrame = 0; 
    inputFormat.mBytesPerPacket = 0; 
    inputFormat.mChannelsPerFrame = 1; 
    inputFormat.mFormatFlags = kMPEG4Object_AAC_LC; 
    inputFormat.mFormatID = kAudioFormatMPEG4AAC; 
    inputFormat.mFramesPerPacket = 1024; 
    inputFormat.mReserved = 0; 
    inputFormat.mSampleRate = 22050; 

    AudioStreamBasicDescription outputFormat; 
    memset(&outputFormat, 0, sizeof(outputFormat)); 
    outputFormat.mSampleRate  = inputFormat.mSampleRate; 
    outputFormat.mFormatID   = kAudioFormatLinearPCM; 
    outputFormat.mFormatFlags  = kLinearPCMFormatFlagIsSignedInteger; 
    outputFormat.mBytesPerPacket = 2; 
    outputFormat.mFramesPerPacket = 1; 
    outputFormat.mBytesPerFrame = 2; 
    outputFormat.mChannelsPerFrame = 1; 
    outputFormat.mBitsPerChannel = 16; 
    outputFormat.mReserved   = 0; 

    AudioClassDescription *description = [self 
             getAudioClassDescriptionWithType:kAudioFormatMPEG4AAC 
             fromManufacturer:kAppleSoftwareAudioCodecManufacturer]; 

    OSStatus status = AudioConverterNewSpecific(&inputFormat, &outputFormat, 1, description, &_audioConverter); 

    if (status != 0) { 
     printf("setup converter error, status: %i\n", (int)status); 
    } 
} 

私はコールバック関数を書いた:まず第一に、私はAudioConverterを設定

struct MyUserData { 
    UInt32 mChannels; 
    UInt32 mDataSize; 
    const void* mData; 
    AudioStreamPacketDescription mPacket; 
}; 

OSStatus inInputDataProc(AudioConverterRef inAudioConverter, 
         UInt32 *ioNumberDataPackets, 
         AudioBufferList *ioData, 
         AudioStreamPacketDescription **outDataPacketDescription, 
         void *inUserData) 
{ 
    struct MyUserData* userData = (struct MyUserData*)(inUserData); 

    if (!userData->mDataSize) { 
     *ioNumberDataPackets = 0; 
     return kNoMoreDataError; 
    } 

    if (outDataPacketDescription) { 
     userData->mPacket.mStartOffset = 0; 
     userData->mPacket.mVariableFramesInPacket = 0; 
     userData->mPacket.mDataByteSize = userData->mDataSize; 
     *outDataPacketDescription = &userData->mPacket; 
    } 

    ioData->mBuffers[0].mNumberChannels = userData->mChannels; 
    ioData->mBuffers[0].mDataByteSize = userData->mDataSize; 
    ioData->mBuffers[0].mData = (void *)userData->mData; 

    // No more data to provide following this run. 
    userData->mDataSize = 0; 

    return noErr; 
} 

とデコードフレームのための私の関数は次のようになります。

- (void)startDecodingAudio { 
    if (!_converterConfigured){ 
     return; 
    } 

    while (true){ 
     if ([self hasFramesToDecode]){ 
      struct MyUserData userData = {1, (UInt32)_decoderBuffer[_currPosInDecoderBuf].length, _decoderBuffer[_currPosInDecoderBuf].bytes}; 

      uint8_t *buffer = (uint8_t *)malloc(128 * sizeof(short int)); 
      AudioBufferList decBuffer; 
      decBuffer.mNumberBuffers = 1; 
      decBuffer.mBuffers[0].mNumberChannels = 1; 
      decBuffer.mBuffers[0].mDataByteSize = 128 * sizeof(short int); 
      decBuffer.mBuffers[0].mData = buffer; 

      UInt32 numFrames = 128; 

      AudioStreamPacketDescription outPacketDescription; 
      memset(&outPacketDescription, 0, sizeof(AudioStreamPacketDescription)); 
      outPacketDescription.mDataByteSize = 128; 
      outPacketDescription.mStartOffset = 0; 
      outPacketDescription.mVariableFramesInPacket = 0; 

      OSStatus status = AudioConverterFillComplexBuffer(_audioConverter, 
                   inInputDataProc, 
                   &userData, 
                   &numFrames, 
                   &decBuffer, 
                   &outPacketDescription); 

      NSError *error = nil; 

      if (status == kNoMoreDataError) { 
       NSLog(@"%u bytes decoded", (unsigned int)decBuffer.mBuffers[0].mDataByteSize); 
       [_decodedData appendData:[NSData dataWithBytes:decBuffer.mBuffers[0].mData length:decBuffer.mBuffers[0].mDataByteSize]]; 
       _currPosInDecoderBuf += 1; 
      } else { 
       error = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil]; 
      } 
     } else { 
      break; 
     } 
    } 
} 

たびに、AudioConverterFillComplexBufferはApple APIに従って、kAudioCodecIllegalOperationErrorというステータス1852797029を返します。誰かがそのような形式で変換することに成功した場合は、いくつかの例や助言をしてください。

+0

あなたはすでにあなたの問題を解決しましたか? – vladiqtx

+0

@VladislavRudskoyはい、下記の私の答えを参照してください。 –

答えて

4

最後に、私はStreamingKitライブラリ(元のリポジトリhere)で私のバイトをデコードしました。

関連する問題