ios - 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以检查是否存在线程问题。任何形式的帮助将不胜感激。
解决方案
音频单元回调应仅返回请求的帧数(样本),如 inNumberFrames 参数所示。您的代码似乎将一些不同数量的样本复制到 AudioBufferList 中,这不起作用,因为 iOS 音频单元只会将请求数量的帧发送到音频输出。
您可以在音频会话配置中建议首选的缓冲持续时间,但这只是一个建议。iOS 可以随意忽略此建议,并使用与设备的音频硬件、系统活动和当前电源状态更好匹配的 inNumberFrames。
不要忘记预先填充足够的循环缓冲区以考虑网络 (UDP) 传输时间中的最大预期抖动。也许测量网络数据包到数据包延迟抖动,并计算其统计数据,最小值,最大值,std.dev。等等
如果您的 UDP 缓冲区的大小不是 2 的幂次方,或者包含的样本不是 iOS 硬件采样率,那么您还必须在安全缓冲开销中考虑小数缓冲区和重采样抖动。
推荐阅读
- php - 如何从 HTML 中的 curl 返回 XML?
- apache-nifi - 无法在 Mac 中完全删除 NiFi 状态(本地设置)
- mongodb - 我无法将文档插入到远程数据库服务器,出现错误 13(未经授权):“命令插入需要身份验证”
- mqtt - 如何获取以“mqtts://”开头的 mqtt 代理 url?
- amazon-web-services - 如何配置 AWS CodeBuild 构建规范以使用由 CloudFormation 创建的动态 ReportGroup
- nativescript - 使用 nativescript typescript 在 ios 中关闭 Modal 时显示 FancyAlert 时 UI 将被阻止
- python - Pandas 数据帧重采样方法“BA”和“A”之间的差异
- amazon-web-services - 使用 Prometheus 和 Grafana 可视化 EC2 元数据
- arrays - 如何将数组中对象的属性分配给多个类属性?
- c# - 错误:“Microsoft.WindowsAPICodePack.Shell.PropertySystem.PropertySystemException 无法获取此属性的可写属性存储。”