Repository navigation
audio: fix 64-bit overflow in GetReadPacket's QPC timestamp (SimpleAudioSample, SysVAD) - #1439
Open
Ali Error (aliforever) wants to merge 1 commit into
Open
Ali Error (aliforever) wants to merge 1 commit into
Ali Error (aliforever) wants to merge 1 commit into
Conversation
SimpleAudioSample and SysVAD convert the packet time from 100 ns units
to performance-counter ticks with
timeOfAvailablePacketInHns * frequency / 10000000
The product overflows 64 bits once timeOfAvailablePacketInHns exceeds
2^64 / frequency, which at the common 10 MHz frequency is about 2.1
days of uptime. From then on every timestamp GetReadPacket returns is
2^64 / 10^7 ticks (about 51 hours) too early, so capture clients get
wrong packet timestamps from IAudioCaptureClient::GetBuffer.
Split the conversion into whole seconds and remainder. The result is
identical (no rounding change) and the intermediate stays below
10^7 * frequency.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
CMiniportWaveRTStream::GetReadPacketin SimpleAudioSample and SysVAD converts the packet time from 100 ns units to performance-counter ticks with:The product
timeOfAvailablePacketInHns * frequencyoverflows 64 bits oncetimeOfAvailablePacketInHnsexceeds 2^64 / frequency. With the common 10 MHz frequency (for example Hyper-V guests and many current PCs), that happens after about 2.1 days of uptime. After that, every timestampGetReadPacketreturns is 2^64 / 10^7 ticks (about 51 hours) too early. Capture clients get these values as the QPC position fromIAudioCaptureClient::GetBuffer. Audio data is not affected, but anything that relies on capture timestamps (echo cancellation, A/V sync, latency measurement) is.Repro
On a machine that has been up for more than about 2.1 days, stream into the sample's capture endpoint and compare
GetBuffer's QPC position withQueryPerformanceCounter. The difference is about −1.84 × 10^12 ticks instead of a few milliseconds.Fix
Split the conversion into whole seconds and remainder:
The result is identical to the original (floor(h·f/10^7) = q·f + floor(r·f/10^7) for h = q·10^7 + r), and the intermediate stays below 10^7 · frequency. The same line is fixed in both samples, which share this code.
Testing
🤖 Generated with Claude Code