首页 > 解决方案 > IOS AudioUnit播放爆裂问题(swift)

问题描述

我正在尝试从 IOS 中的 android 设备播放来自 UDP 的字节。我正在使用 TPCircularBuffer 播放字节。我的代码如下:

let success = initCircularBuffer(&circularBuffer, 1024)
        if success {
            print("Circular buffer init was successful")
        } else {
            print("Circular buffer init not successful")
        }



func udpReceive() {
        receivingQueue.async {
            repeat {
                do {
                    let datagram = try self.tcpClient?.receive()
                    let byteData = datagram?["data"] as? Data
                    let dataLength = datagram?["length"] as? Int
                    self.dataLength = dataLength!

                    let _ = TPCircularBufferProduceBytes(&self.circularBuffer, byteData!.bytes, UInt32(dataLength! * MemoryLayout<UInt8>.stride * 2))

                } catch {
                    fatalError(error.localizedDescription)
                }
            } while true
        }


    }

    func consumeBuffer() -> UnsafeMutableRawPointer? {
        self.availableBytes = 0
        let tail = TPCircularBufferTail(&self.circularBuffer, &self.availableBytes)
        return tail
    }

我们正在以 16K 采样率录制并通过 UDP 从 Android 端发送到 IOS,然后我们使用 AudioUnit 播放我们的字节,但问题是我们声音中的噼啪声和削波声

播放回调代码:

func performPlayback(
        _ ioActionFlags: UnsafeMutablePointer<AudioUnitRenderActionFlags>,
        inTimeStamp: UnsafePointer<AudioTimeStamp>,
        inBufNumber: UInt32,
        inNumberFrames: UInt32,
        ioData: UnsafeMutablePointer<AudioBufferList>
    ) -> OSStatus {

        var buffer = ioData[0].mBuffers             

        let bufferTail = consumeBuffer()

        memcpy(buffer.mData, bufferTail, min(self.dataLength, Int(availableBytes)))
        buffer.mDataByteSize = UInt32(min(self.dataLength, Int(availableBytes)))


        TPCircularBufferConsume(&self.circularBuffer, UInt32(min(self.dataLength, Int(availableBytes))))

        return noErr
    }

UDP 每个样本向我们发送 1280 个字节。我们认为问题在于未正确设置的 BUFFER SIZE。谁能指导我如何设置适当的缓冲区大小。确实会有很大帮助。我知道@Gruntcakes 作为 voip 工程师的工作https://stackoverflow.com/a/57136561/12020007。我还研究了@hotpaw2 的工作,并查看了https://stackoverflow.com/a/58545845/12020007以检查是否存在线程问题。任何形式的帮助将不胜感激。

标签: iosswiftcore-audioplaybackaudiounit

解决方案


音频单元回调应仅返回请求的帧数(样本),如 inNumberFrames 参数所示。您的代码似乎将一些不同数量的样本复制到 AudioBufferList 中,这不起作用,因为 iOS 音频单元只会将请求数量的帧发送到音频输出。

您可以在音频会话配置中建议首选的缓冲持续时间,但这只是一个建议。iOS 可以随意忽略此建议,并使用与设备的音频硬件、系统活动和当前电源状态更好匹配的 inNumberFrames。

不要忘记预先填充足够的循环缓冲区以考虑网络 (UDP) 传输时间中的最大预期抖动。也许测量网络数据包到数据包延迟抖动,并计算其统计数据,最小值,最大值,std.dev。等等

如果您的 UDP 缓冲区的大小不是 2 的幂次方,或者包含的样本不是 iOS 硬件采样率,那么您还必须在安全缓冲开销中考虑小数缓冲区和重采样抖动。


推荐阅读