-
Notifications
You must be signed in to change notification settings - Fork 2
fix: repair lost video packets with retransmissions #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
anderson-oki
merged 6 commits into
OpenCloudGaming:main
from
olivierapex:fix/nack-repair
Oct 5, 2026
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5ae7c9a
fix: repair lost video packets with retransmissions before giving up
c43f827
fix: send retransmission requests as the version 2 control command
24832df
fix: wait a round trip before retrying a retransmission request
4023617
fix: request only the packets one retransmission request can name
561e04a
fix: hold a gap for the measured round trip and stop when resends nev…
anderson-oki 6d5516f
fix: reach the retransmission wait verdict on the next scan, not the …
anderson-oki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // When to ask the seat to resend a missing video packet. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| /// Schedules retransmission requests for missing video packets on the official client's timing, | ||
| /// as its log prints it: the first request 1 ms after a packet goes missing, then up to three | ||
| /// retries, each one round trip plus 4 ms after the last (`useRtdForRtpNackToggle`), within the | ||
| /// 52 ms it holds a frame for them. A retry sooner than the round trip asks for a packet that is | ||
| /// already on its way, and the seat sends it again. | ||
| struct NvstNackTracker { | ||
| static let initialDelayNanoseconds: UInt64 = 1_000_000 | ||
| static let extraRetryWaitNanoseconds: UInt64 = 4_000_000 | ||
| static let maximumRetries = 3 | ||
| static let maximumWaitNanoseconds: UInt64 = 52_000_000 | ||
|
|
||
| /// The wait before a retry: the extra wait alone until a round trip has been measured. | ||
| private(set) var retryIntervalNanoseconds = NvstNackTracker.extraRetryWaitNanoseconds | ||
| private(set) var retryCount = 0 | ||
|
|
||
| private struct Request { | ||
| let firstMissedAt: UInt64 | ||
| var lastSentAt: UInt64? | ||
| var sendCount = 0 | ||
| } | ||
|
|
||
| private var requests: [UInt64: Request] = [:] | ||
|
|
||
| var isEmpty: Bool { requests.isEmpty } | ||
|
|
||
| mutating func useRoundTrip(nanoseconds: UInt64) { | ||
| retryIntervalNanoseconds = nanoseconds + Self.extraRetryWaitNanoseconds | ||
| } | ||
|
|
||
| /// The missing indices to request now, recording that they were requested. | ||
| mutating func due(missing: [UInt64], now: UInt64) -> [UInt64] { | ||
| var due: [UInt64] = [] | ||
| for index in missing { | ||
| var request = requests[index] ?? Request(firstMissedAt: now) | ||
| let isDue: Bool | ||
| if let lastSentAt = request.lastSentAt { | ||
| isDue = request.sendCount <= Self.maximumRetries | ||
| && now &- lastSentAt >= retryIntervalNanoseconds | ||
| } else { | ||
| isDue = now &- request.firstMissedAt >= Self.initialDelayNanoseconds | ||
| } | ||
| if isDue { | ||
| if request.sendCount > 0 { retryCount += 1 } | ||
| request.lastSentAt = now | ||
| request.sendCount += 1 | ||
| due.append(index) | ||
| } | ||
| requests[index] = request | ||
| } | ||
| return due | ||
| } | ||
|
|
||
| /// Whether `index` was requested and is still inside the time its retransmission takes. | ||
| func isAwaitingRetransmission(of index: UInt64, now: UInt64) -> Bool { | ||
| guard let request = requests[index], request.sendCount > 0 else { return false } | ||
| return now &- request.firstMissedAt < Self.maximumWaitNanoseconds | ||
| } | ||
|
|
||
| /// Forgets an index that arrived. True when it had been requested, so the arrival is a repair. | ||
| mutating func arrived(_ index: UInt64) -> Bool { | ||
| (requests.removeValue(forKey: index)?.sendCount ?? 0) > 0 | ||
| } | ||
|
|
||
| /// Forgets every index below `index`: delivered, or given up on. | ||
| mutating func forget(below index: UInt64) { | ||
| guard !requests.isEmpty else { return } | ||
| requests = requests.filter { $0.key >= index } | ||
| } | ||
|
|
||
| mutating func reset() { | ||
| requests.removeAll() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // The version 2 retransmission request the seat answers. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| /// Control command `0x317`, which the official client sends for `rtpNackVersion` 2 | ||
| /// (`NvscClientPipeline::createAndSendNackRequest` → `ServerControl::sendRtpNackRequest`) instead | ||
| /// of an RTCP NACK. Layout from `RtpSourceQueueExtV2::createNackRequest`: version, stream index | ||
| /// and entry count, one byte each, then per entry a little-endian u16 sequence number and a | ||
| /// little-endian u64 whose bit n names sequence + n + 1. At most 64 sequence numbers per request. | ||
| public struct NvstRtpNackRequest: Equatable, Sendable { | ||
| public static let version: UInt8 = 2 | ||
| public static let maximumSequenceNumbers = 64 | ||
|
|
||
| public struct Entry: Equatable, Sendable { | ||
| public let sequenceNumber: UInt16 | ||
| public let followingMask: UInt64 | ||
| } | ||
|
|
||
| public let streamIndex: UInt8 | ||
| public let entries: [Entry] | ||
|
|
||
| /// `sequenceNumbers` in RTP order; only the first `maximumSequenceNumbers` are named. | ||
| public init(streamIndex: UInt8 = 0, sequenceNumbers: [UInt16]) { | ||
| self.streamIndex = streamIndex | ||
| let named = sequenceNumbers.prefix(Self.maximumSequenceNumbers) | ||
| var entries: [Entry] = [] | ||
| var index = named.startIndex | ||
| while index < named.endIndex { | ||
| let base = named[index] | ||
| var mask: UInt64 = 0 | ||
| index += 1 | ||
| while index < named.endIndex { | ||
| let offset = named[index] &- base | ||
| guard offset >= 1, offset <= 64 else { break } | ||
| mask |= 1 << UInt64(offset - 1) | ||
| index += 1 | ||
| } | ||
| entries.append(Entry(sequenceNumber: base, followingMask: mask)) | ||
| } | ||
| self.entries = entries | ||
| } | ||
|
|
||
| public var payload: Data { | ||
| var writer = NvstByteWriter(capacity: 3 + entries.count * 10) | ||
| writer.u8(Self.version) | ||
| writer.u8(streamIndex) | ||
| writer.u8(UInt8(entries.count)) | ||
| for entry in entries { | ||
| writer.u16LE(entry.sequenceNumber) | ||
| writer.u64LE(entry.followingMask) | ||
| } | ||
| return writer.data | ||
| } | ||
|
|
||
| public var command: NvstControlCommand { | ||
| NvstControlCommand(code: .rtpNackRequest, payload: payload) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.