我正在尝试在 Xcode 中运行的(新鲜)蒸汽应用程序中捕获 UDP 视频流。数据正在由 ffmpeg 流式传输,我可以使用 VLC 成功查看目标机器上的流,这也是运行蒸汽应用程序的一个,使用udp://0.0.0.0:5000
. 我使用了各种 Apple 文档来获取下面的代码。当我运行它时,我在控制台日志中得到了这些输出行,但我想知道它们是否不相关:
2021-07-07 17:59:27.102681+0100 Run[10550:2494617] [si_destination_compare] send failed: Invalid argument
2021-07-07 17:59:27.104056+0100 Run[10550:2494617] [si_destination_compare] send failed: Undefined error: 0
在 configure.swift 中:
try setupClient()
这是客户端代码:
final class FrameHandler : ChannelInboundHandler {
typealias InboundIn = AddressedEnvelope<ByteBuffer>
typealias OutboundOut = AddressedEnvelope<ByteBuffer>
func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
// minimal for question
}
func errorCaught(ctx: ChannelHandlerContext, error: Error) {
// minimal for question
}
}
func setupClient() throws {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let bootstrap = DatagramBootstrap(group: group)
.channelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
.channelInitializer { channel in
channel.pipeline.addHandler(FrameHandler())
}
defer {
try! group.syncShutdownGracefully()
}
let channel = try bootstrap.bind(host: "0.0.0.0", port: 5000).wait()
try channel.closeFuture.wait()
}
问题是,虽然channelRegistered
和channelActive
被调用,然后是一个永无止境的 流readComplete
,但重要的channelRead
永远不会被调用 - 也不会errorCaught
。如果我注释掉对 setupClient 的调用,则没有网络活动,但是,如果它运行,则 Xcode 的网络监视器显示与 ffmpeg 中的级别一致的活动。所以,我相信连接正在建立。
我想知道问题是否出在我设置处理程序的方式上?所有示例都使用 echo 或反射聊天示例,因此入站处理程序是在使用上下文的数据写入函数的闭包中设置的,而不是将其添加到初始化程序中(尽管出站处理程序是以这种方式设置的)。