engine: count decoded pictures by video source

Nothing the package offered was a frame rate. The decoder's total counts
tiles, up to four to a picture, and only changed tiles are sent; the
renderer's commits count a picture twice when its tiles reach the main
thread in two batches. A session now keeps a count for each source of
each display, and the most any one source gained is that display's
frames.
This commit is contained in:
Burak Goncu
2026-11-11 04:06:18 +03:00
parent a7e955d716
commit 392cd23bbd
2 changed files with 87 additions and 0 deletions

View File

@@ -1096,6 +1096,16 @@ public final class VNCSession {
/// one, and must never share its decoder or band compositor.
@ObservationIgnored
public let secondaryVideoBandRenderer = VideoBandLayerRenderer()
/// How many pictures each video source of each display has decoded.
///
/// A display's picture is cut into tiles, each its own source, and only
/// the tiles that changed are sent. So no single count is the frame
/// rate: the decoder's total counts tiles, and the renderer's commits
/// count a picture twice when its tiles arrive in two batches. The tile
/// that changes most is the best witness, and a host that shows a frame
/// rate takes the largest increase among a display's sources.
public let videoSourceFrames = VideoSourceFrameCounter()
var primaryVideoDecodeProgress: VideoStreamManager.DecodeProgress? {
videoStreamManager?.decodeProgress
}
@@ -4474,9 +4484,11 @@ public final class VNCSession {
// output and on-screen result can be compared for the same session.
let frameDumper = DiagnosticFrameDumper.fromEnvironment()
let decodedBands = DecodedBandTracker()
let sourceFrames = videoSourceFrames
let callback: VideoStreamManager.FrameCallback = { pixelBuffer, ssrc in
frameDumper?.maybeDump(pixelBuffer, ssrc: ssrc)
decodedBands.record(ssrc)
sourceFrames.record(ssrc, display: 0)
coalescer.submit(ssrc: ssrc, pixelBuffer: pixelBuffer)
}
@@ -4533,6 +4545,7 @@ public final class VNCSession {
numberOfTiles: initialTileCount
) { pixelBuffer, ssrc in
secondDecodedBands.record(ssrc)
sourceFrames.record(ssrc, display: 1)
secondCoalescer.submit(ssrc: ssrc, pixelBuffer: pixelBuffer)
}
secondaryManager = secondManager
@@ -5189,6 +5202,47 @@ final class DiagnosticFrameDumper: @unchecked Sendable {
}
}
/// Decoded pictures by video source, for each display. Counts only go up;
/// a renegotiated stream brings new sources and leaves the old ones behind.
public final class VideoSourceFrameCounter: @unchecked Sendable {
private let lock = NSLock()
private var counts: [[UInt32: UInt64]] = [[:], [:]]
init() {}
func record(_ ssrc: UInt32, display: Int) {
lock.lock()
defer { lock.unlock() }
guard counts.indices.contains(display) else { return }
// A long session renegotiates many times; keep the table from
// growing without end by starting over once it is far larger than
// any one generation needs.
if counts[display].count > 64, counts[display][ssrc] == nil {
counts[display].removeAll(keepingCapacity: true)
}
counts[display][ssrc, default: 0] &+= 1
}
/// The counts as they stand: one table for each display, by source.
public func snapshot() -> [[UInt32: UInt64]] {
lock.lock()
defer { lock.unlock() }
return counts
}
/// The most pictures any one source of a display gained between two
/// snapshots: that display's frame count over the time between them.
public static func frames(from earlier: [[UInt32: UInt64]], to later: [[UInt32: UInt64]]) -> [UInt64] {
later.enumerated().map { display, sources in
let before = earlier.indices.contains(display) ? earlier[display] : [:]
return sources.reduce(0) { most, source in
let was = before[source.key] ?? 0
return max(most, source.value >= was ? source.value - was : source.value)
}
}
}
}
/// Tracks which video sources (bands) have delivered at least one decoded
/// frame; the dead-band watchdog compares this against the transport's count.
final class DecodedBandTracker: @unchecked Sendable {

View File

@@ -0,0 +1,33 @@
import XCTest
@testable import rootshellVNC
final class VideoSourceFrameCounterTests: XCTestCase {
/// A picture's tiles are separate sources and only the changed ones are
/// sent, so a display's frame count is what its busiest source gained.
func testFramesAreTheMostAnyOneSourceGained() {
let earlier: [[UInt32: UInt64]] = [[1: 100, 2: 100, 3: 100, 4: 100], [9: 40]]
let later: [[UInt32: UInt64]] = [[1: 160, 2: 130, 3: 100, 4: 115], [9: 70]]
XCTAssertEqual(VideoSourceFrameCounter.frames(from: earlier, to: later), [60, 30])
}
func testASourceThatAppearedBetweenSnapshotsCountsFromNothing() {
let earlier: [[UInt32: UInt64]] = [[1: 500], [:]]
let later: [[UInt32: UInt64]] = [[7: 25], [8: 12]]
XCTAssertEqual(VideoSourceFrameCounter.frames(from: earlier, to: later), [25, 12])
}
func testNothingDecodedIsNoFrames() {
XCTAssertEqual(VideoSourceFrameCounter.frames(from: [[:], [:]], to: [[:], [:]]), [0, 0])
XCTAssertEqual(VideoSourceFrameCounter.frames(from: [], to: [[3: 4], [:]]), [4, 0])
}
func testRecordsByDisplayAndSource() {
let counter = VideoSourceFrameCounter()
counter.record(1, display: 0)
counter.record(1, display: 0)
counter.record(2, display: 0)
counter.record(5, display: 1)
counter.record(5, display: 7) // no such display
XCTAssertEqual(counter.snapshot(), [[1: 2, 2: 1], [5: 1]])
}
}