【发布时间】:2019-11-11 17:05:41
【问题描述】:
我正在尝试从 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 以检查是否存在线程问题。任何形式的帮助将不胜感激。
【问题讨论】:
标签: ios swift core-audio playback audiounit