15

我在处理将音频数据包转换为 PCMAudioFileStream_PacketsProc期间设置了一个回调。我遇到的问题是在被调用后我得到了 -50 OSStatus (paramErr) 。以下是使用了哪些参数以及它们是如何制作的片段:AudioFileStreamOpenAudioConverterFillComplexBufferAudioConverterFillComplexBufferAudioConverterFillComplexBuffer

        audioConverterRef = AudioConverterRef()

        // AudioConvertInfo is a struct that contains information
        // for the converter regarding the number of packets and 
        // which audiobuffer is being allocated
        convertInfo? = AudioConvertInfo(done: false, numberOfPackets: numberPackets, audioBuffer: buffer,
            packetDescriptions: packetDescriptions) 

        var framesToDecode: UInt32 = pcmBufferTotalFrameCount! - end

        var localPcmAudioBuffer = AudioBuffer()
        localPcmAudioBuffer.mData = pcmAudioBuffer!.mData.advancedBy(Int(end * pcmBufferFrameSizeInBytes!))

        var localPcmBufferList = AudioBufferList(mNumberBuffers: 1, mBuffers: AudioBuffer(mNumberChannels: 0, mDataByteSize: 0, mData: nil))
        localPcmAudioBuffer = localPcmBufferList.mBuffers
        localPcmAudioBuffer.mData = pcmAudioBuffer!.mData.advancedBy(Int(end * pcmBufferFrameSizeInBytes!))
        localPcmAudioBuffer.mDataByteSize = framesToDecode * pcmBufferFrameSizeInBytes!;
        localPcmAudioBuffer.mNumberChannels = pcmAudioBuffer!.mNumberChannels

        var localPcmBufferList = AudioBufferList(mNumberBuffers: 1, mBuffers: AudioBuffer(mNumberChannels: 0, mDataByteSize: 0, mData: nil))
        localPcmAudioBuffer = localPcmBufferList.mBuffers

        AudioConverterFillComplexBuffer(audioConverterRef, AudioConverter_Callback, &convertInfo, &framesToDecode, &localPcmBufferList, nil)

是什么可能导致参数错误?

如果需要,这是回调的完整方法:

func handleAudioPackets(inputData: UnsafePointer<Void>, numberBytes: UInt32, numberPackets: UInt32, packetDescriptions: UnsafeMutablePointer<AudioStreamPacketDescription>) {
        if currentlyReadingEntry == nil {
            print("currentlyReadingEntry = nil")
            return
        }
        if currentlyReadingEntry.parsedHeader == false {
            print("currentlyReadingEntry.parsedHeader == false")
            return
        }

        if disposedWasRequested == true {
            print("disposedWasRequested == true")
            return
        }

        guard let audioConverterRef = audioConverterRef else {
            return
        }

        if seekToTimeWasRequested == true && currentlyReadingEntry.calculatedBitRate() > 0.0 {
            wakeupPlaybackThread()
            print("seekToTimeWasRequested == true && currentlyReadingEntry.calculatedBitRate() > 0.0")
            return
        }

        discontinuous = false

        var buffer = AudioBuffer()
        buffer.mNumberChannels = audioConverterAudioStreamBasicDescription.mChannelsPerFrame
        buffer.mDataByteSize = numberBytes
        buffer.mData = UnsafeMutablePointer<Void>(inputData)


        convertInfo? = AudioConvertInfo(done: false, numberOfPackets: numberPackets, audioBuffer: buffer,
            packetDescriptions: packetDescriptions)


        if packetDescriptions != nil && currentlyReadingEntry.processedPacketsCount < maxCompressedBacketsForBitrateCalculation {
            let count: Int = min(Int(numberPackets), Int(maxCompressedBacketsForBitrateCalculation - currentlyReadingEntry.processedPacketsCount!))
            for var i = 0;i < count;++i{
                let packetSize: Int32 = Int32(packetDescriptions[i].mDataByteSize)
                OSAtomicAdd32(packetSize, &currentlyReadingEntry.processedPacketsSizeTotal!)
                OSAtomicIncrement32(&currentlyReadingEntry.processedPacketsCount!)
            }
        }
        while true {
            OSSpinLockLock(&pcmBufferSpinLock)
            var used: UInt32 = pcmBufferUsedFrameCount!
            var start: UInt32 = pcmBufferFrameStartIndex!
            var end = (pcmBufferFrameStartIndex! + pcmBufferUsedFrameCount!) % pcmBufferTotalFrameCount!
            var framesLeftInsideBuffer = pcmBufferTotalFrameCount! - used
            OSSpinLockUnlock(&pcmBufferSpinLock)

            if framesLeftInsideBuffer == 0 {
                pthread_mutex_lock(&playerMutex)
                while true {
                    OSSpinLockLock(&pcmBufferSpinLock)
                    used = pcmBufferUsedFrameCount!
                    start = pcmBufferFrameStartIndex!
                    end = (pcmBufferFrameStartIndex! + pcmBufferUsedFrameCount!) % pcmBufferTotalFrameCount!
                    framesLeftInsideBuffer = pcmBufferTotalFrameCount! - used
                    OSSpinLockUnlock(&pcmBufferSpinLock)

                    if framesLeftInsideBuffer > 0 {
                        break
                    }

                    if (disposedWasRequested == true
                        || internalState == SSPlayerInternalState.Disposed) {
                        pthread_mutex_unlock(&playerMutex)
                        return
                    }

                    if (seekToTimeWasRequested == true && currentlyPlayingEntry.calculatedBitRate() > 0.0)
                    {
                        pthread_mutex_unlock(&playerMutex)
                        wakeupPlaybackThread()
                        return;
                    }

                    waiting = true
                    pthread_cond_wait(&playerThreadReadyCondition, &playerMutex)
                    waiting = false
                }
                pthread_mutex_unlock(&playerMutex)
            }
            var localPcmAudioBuffer = AudioBuffer()
            var localPcmBufferList = AudioBufferList(mNumberBuffers: 1, mBuffers: AudioBuffer(mNumberChannels: 0, mDataByteSize: 0, mData: nil))
            localPcmAudioBuffer = localPcmBufferList.mBuffers

            if end >= start {
                var framesAdded: UInt32 = 0
                var framesToDecode: UInt32 = pcmBufferTotalFrameCount! - end
                localPcmAudioBuffer.mData = pcmAudioBuffer!.mData.advancedBy(Int(end * pcmBufferFrameSizeInBytes!))
                localPcmAudioBuffer.mDataByteSize = framesToDecode * pcmBufferFrameSizeInBytes!;
                localPcmAudioBuffer.mNumberChannels = pcmAudioBuffer!.mNumberChannels

                AudioConverterFillComplexBuffer(audioConverterRef, AudioConverter_Callback, &convertInfo, &framesToDecode, &localPcmBufferList, nil)

                framesAdded = framesToDecode

                if status == 100 {
                    OSSpinLockLock(&pcmBufferSpinLock)
                    let newCount = pcmBufferUsedFrameCount! + framesAdded
                    pcmBufferUsedFrameCount = newCount
                    OSSpinLockUnlock(&pcmBufferSpinLock);

                    OSSpinLockLock(&currentlyReadingEntry!.spinLock!)
                    let newFramesAddedCount = currentlyReadingEntry.framesQueued! + Int64(framesAdded)
                    currentlyReadingEntry!.framesQueued! = newFramesAddedCount
                    OSSpinLockUnlock(&currentlyReadingEntry!.spinLock!)
                    return
                } else if status != 0 {
                    print("error")
                    return
                }
                framesToDecode = start

                if framesToDecode == 0 {

                    OSSpinLockLock(&pcmBufferSpinLock)
                    let newCount = pcmBufferUsedFrameCount! + framesAdded
                    pcmBufferUsedFrameCount = newCount
                    OSSpinLockUnlock(&pcmBufferSpinLock);

                    OSSpinLockLock(&currentlyReadingEntry!.spinLock!)
                    let newFramesAddedCount = currentlyReadingEntry.framesQueued! + Int64(framesAdded)
                    currentlyReadingEntry!.framesQueued! = newFramesAddedCount
                    OSSpinLockUnlock(&currentlyReadingEntry!.spinLock!)
                    continue
                }

                localPcmAudioBuffer.mData = pcmAudioBuffer!.mData
                localPcmAudioBuffer.mDataByteSize = framesToDecode * pcmBufferFrameSizeInBytes!
                localPcmAudioBuffer.mNumberChannels = pcmAudioBuffer!.mNumberChannels

                AudioConverterFillComplexBuffer(audioConverterRef, AudioConverter_Callback, &convertInfo, &framesToDecode, &localPcmBufferList, nil)
                let decodedFramesAdded = framesAdded + framesToDecode
                framesAdded = decodedFramesAdded

                if status == 100 {
                    OSSpinLockLock(&pcmBufferSpinLock)
                    let newCount = pcmBufferUsedFrameCount! + framesAdded
                    pcmBufferUsedFrameCount = newCount
                    OSSpinLockUnlock(&pcmBufferSpinLock);

                    OSSpinLockLock(&currentlyReadingEntry!.spinLock!)
                    let newFramesAddedCount = currentlyReadingEntry.framesQueued! + Int64(framesAdded)
                    currentlyReadingEntry!.framesQueued! = newFramesAddedCount
                    OSSpinLockUnlock(&currentlyReadingEntry!.spinLock!)
                    return
                } else if status == 0 {
                    OSSpinLockLock(&pcmBufferSpinLock)
                    let newCount = pcmBufferUsedFrameCount! + framesAdded
                    pcmBufferUsedFrameCount = newCount
                    OSSpinLockUnlock(&pcmBufferSpinLock);

                    OSSpinLockLock(&currentlyReadingEntry!.spinLock!)
                    let newFramesAddedCount = currentlyReadingEntry.framesQueued! + Int64(framesAdded)
                    currentlyReadingEntry!.framesQueued! = newFramesAddedCount
                    OSSpinLockUnlock(&currentlyReadingEntry!.spinLock!)
                    continue
                } else if status != 0 {
                    print("error")
                    return
                } else {
                    var framesAdded: UInt32 = 0
                    var framesToDecode: UInt32 = start - end
                    localPcmAudioBuffer.mData = pcmAudioBuffer!.mData.advancedBy(Int(end * pcmBufferFrameSizeInBytes!))
                    localPcmAudioBuffer.mDataByteSize = framesToDecode * pcmBufferFrameSizeInBytes!;
                    localPcmAudioBuffer.mNumberChannels = pcmAudioBuffer!.mNumberChannels

                    var  convertInfoo: UnsafePointer<Void> = unsafeBitCast(convertInfo, UnsafePointer<Void>.self)

                    status = AudioConverterFillComplexBuffer(audioConverterRef, AudioConverter_Callback, &convertInfoo, &framesToDecode, &localPcmBufferList, nil)

                    framesAdded = framesToDecode

                    if status == 100 {
                        OSSpinLockLock(&pcmBufferSpinLock)
                        let newCount = pcmBufferUsedFrameCount! + framesAdded
                        pcmBufferUsedFrameCount = newCount
                        OSSpinLockUnlock(&pcmBufferSpinLock);

                        OSSpinLockLock(&currentlyReadingEntry!.spinLock!)
                        let newFramesAddedCount = currentlyReadingEntry.framesQueued! + Int64(framesAdded)
                        currentlyReadingEntry!.framesQueued! = newFramesAddedCount
                        OSSpinLockUnlock(&currentlyReadingEntry!.spinLock!)
                        return
                    } else if status == 0 {
                        OSSpinLockLock(&pcmBufferSpinLock)
                        let newCount = pcmBufferUsedFrameCount! + framesAdded
                        pcmBufferUsedFrameCount = newCount
                        OSSpinLockUnlock(&pcmBufferSpinLock);

                        OSSpinLockLock(&currentlyReadingEntry!.spinLock!)
                        let newFramesAddedCount = currentlyReadingEntry.framesQueued! + Int64(framesAdded)
                        currentlyReadingEntry!.framesQueued! = newFramesAddedCount
                        OSSpinLockUnlock(&currentlyReadingEntry!.spinLock!)
                        continue
                    } else if status != 0 {
                        print("error")
                        return
                    }

                }
            }
        }
    }
4

3 回答 3

5

Hej @3254523,我为您提供了一些可能的解决方案。尽管我不是该专业的专家,但我希望以正确的方式指导您。因此,问题肯定是以下配置:

AudioBufferList

这里是探索与 AudioBufferList相关的-50 OSStatus提示的链接: http : //lists.apple.com/archives/coreaudio-api/2012/Apr/msg00041.html https://forums.developer.apple。 com/thread/6313

现在,我们必须专注于解决方案。查看您的AudioBufferList,您没有分配任何值,但mNumberBuffers为 1。尝试按以下方式更改值(如第二个链接所示):

var localPcmBufferList = AudioBufferList(mNumberBuffers: 2, mBuffers: AudioBuffer(mNumberChannels: 2, mDataByteSize: UInt32(buffer.count), mData: &buffer))

如果仍然无法正常工作,我们必须集中精力正确纠正它,因此您可以在这里找到AudioConverterFillComplexBuffer中-50 OSStatus的解决方案,尽管不是很快:

AudioConverterFillComplexBuffer 返回 -50 (paramErr)

iPhone:AudioBufferList 初始化和释放

于 2015-07-24T22:13:27.920 回答
0

使用tanersener/mobile-ffmpeg轻松实现

      let command = "-i input.mp3 -f s16le -acodec pcm_s16le -ac 1 -ar 44100  output.raw"
      let result = MobileFFmpeg.execute(command)
           switch result {
           case RETURN_CODE_SUCCESS:
               print("command exe completed successfully.\n")
           case RETURN_CODE_CANCEL:
               print("command exe cancelled by user.\n")
           default:
               print("command exe failed with rc=\(result) and output=\(String(describing: MobileFFmpegConfig.getLastCommandOutput())).\n")
          }
于 2020-05-29T06:59:10.413 回答
0

取自AudioKit的一个很好的例子


  • 音频文件服务(读取 MP3 格式并写入 AIFF 或 WAV)

  • 音频文件转换服务(将 MP3 数据转换为 PCM,或从 PCM 编码为其他编解码器,如果您要编写文件)

给定的转换器无法在两种编码格式之间进行转换。

你可以做 MP3 到 PCM 或 PCM 到 AAC

而做 MP3-to-AAC,需要两个转换器

于 2020-05-28T07:24:11.337 回答