swift - 将远程帧缓冲区流式传输到 NSView

标签 swift macos cocoa core-graphics

我正在尝试使用 LibvnccClient 在 Swift 中实现一个简单的 VNC 客户端

我创建了 NSView 的子类。我创建一个 CGContext 并将数据指针传递给库以用作帧缓冲区。当屏幕内容发生变化时,libvncclient 会更新帧缓冲区并调用我提供的回调。

这是相关代码

class VNCClient {

    var localClient: rfbClient
    var view: NSView

    init(view: NSView) {
        guard let client_ptr = rfbGetClient(8, 3, 4) else {
            fatalError("Trouble")
        }

        self.view = view
        self.localClient = client_ptr.pointee

        localClient.MallocFrameBuffer = resize
        localClient.GotFrameBufferUpdate = update
        let fbPointer = UnsafeMutablePointer<UInt8>(OpaquePointer((view as! RFBView).buffer))
        localClient.frameBuffer = fbPointer
        rfbClientSetClientData(&localClient, &viewTag, &self.view)
        var argc: Int32 = 0
        let b = rfbInitClient(&localClient, &argc, nil)
        if b == RFB_TRUE {
            print("Connected!")
        }
        Timer.scheduledTimer(withTimeInterval: 0.001, repeats: true) {_ in
            if WaitForMessage(&self.localClient, 1) > 0 {
                HandleRFBServerMessage(&self.localClient)
            }
        }
    }

func update(client: UnsafeMutablePointer<rfbClient>?, x: Int32, y: Int32, w: Int32, h: Int32) -> Void {
    let cl = client!.pointee
    let view_ptr = rfbClientGetClientData(client, &viewTag)
    let view = view_ptr?.assumingMemoryBound(to: RFBView.self).pointee
    view?.setNeedsDisplay(NSRect(x: Int(x), y: Int(y), width: Int(w), height: Int(h)))
}


class RFBView: NSView {

    let ctx = CGContext(data: nil, width: 800, height: 600, bitsPerComponent: 8, bytesPerRow: 4 * 800, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)!

    var buffer: UnsafeMutableRawPointer? {
        return ctx.data
    }
    override func draw(_ dirtyRect: NSRect) {

        let image = ctx.makeImage()

        NSGraphicsContext.current()?.cgContext.draw(image!, in: frame)

   }

可以使用,但显示不流畅。服务器在虚拟机中的同一台计算机上运行,​​因此不存在网络问题。

我正在为每次更新重新绘制整个图像,我认为这是问题的原因。那么如何才能只重绘帧缓冲区中更新的部分呢?

CoreGraphics 足够快吗?还是应该使用 NSOpenGLView

最佳答案

我假设如果有更新,HandleRFBServerMessage 将在每次迭代时调用 update 函数一次。但事实并非如此。

我用打印到控制台的 stub 替换了 update,并注意到当屏幕上有大量事件时它被调用了数千次。

但是,通过 update 内的 setNeedsDisplay 调用,它只被调用了几百次。由于渲染花费了太多时间,因此错过了中间的几帧。

我将绘图代码移到了 Timer 内,现在它工作得很好。

localClient.GotFrameBufferUpdate = { _, _, _, _, _ in needs_update = true }
Timer.scheduledTimer(withTimeInterval: 0.016, repeats: true) {_ in
    if needs_update {
        view.display()
        needs_update = false
    }
    if WaitForMessage(&self.localClient, 1) > 0 {
        HandleRFBServerMessage(&self.localClient)
    }
}

关于swift - 将远程帧缓冲区流式传输到 NSView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43215645/

相关文章:

objective-c - Objective C 中的循环 #import/@class 问题

cocoa - 自定义 NSMenu 突出显示颜色而不使用自定义 View

cocoa - 使用 NSNumberFormatter,如何使用 2 位小数,同时仍然保留用户是否只输入 1 位或 2 位小数?

ios - iOS 的 Game Center 中没有可用数据

ios - 访问核心数据中的子对象时出现关系错误

android - 如何在 macOS Sierra Android 构建中修复 "sed: illegal option -- r"?

c - 段错误: 11 on mac but not on linux while creating an array in C

swift - 为什么 SwiftUI 的 View 协议(protocol)使用 PAT?

ios - Swift Animate 背景不适用于 Collection View 单元格

xcode - 沙箱无法访问本地套接字