diff --git a/native/capture/windows/main.cpp b/native/capture/windows/main.cpp index 6e3a8e0c8..7b80cff98 100644 --- a/native/capture/windows/main.cpp +++ b/native/capture/windows/main.cpp @@ -1541,11 +1541,18 @@ struct CaptureInput final { UINT64 capture_timestamp = 0; }; -std::shared_ptr RefreshCaptureInput(const CaptureInput& latest) { +UINT64 NextPresentationTimestamp(UINT64 candidate, UINT64 previous) { + constexpr UINT64 rtp_tick_100ns = (10'000'000 + 90'000 - 1) / 90'000; + return std::max(candidate, previous + rtp_tick_100ns); +} + +std::shared_ptr RefreshCaptureInput( + const CaptureInput& latest, UINT64 previous_presentation) { auto input = std::make_shared(latest); // WGC SystemRelativeTime and MFGetSystemTime share the 100 ns QPC clock. - // A requested still frame is presented now, including after a quiet gap. - input->timestamp = static_cast(MFGetSystemTime()); + // Delayed capture can already have advanced presentation beyond the clock. + input->timestamp = NextPresentationTimestamp( + static_cast(MFGetSystemTime()), previous_presentation); return input; } @@ -1555,8 +1562,7 @@ std::optional CapturePresentationTimestamp( if (captured <= previous_capture || captured < minimum_capture) return std::nullopt; // A real frame can have been captured before a requested still was presented. // Retain its new pixels and keep separate access units at least one RTP tick apart. - constexpr UINT64 rtp_tick_100ns = (10'000'000 + 90'000 - 1) / 90'000; - return std::max(captured, previous_presentation + rtp_tick_100ns); + return NextPresentationTimestamp(captured, previous_presentation); } class OutputWorker final { @@ -2196,7 +2202,7 @@ void RunVideoCapture(ProductArguments arguments) { if (wait == WAIT_TIMEOUT) { // An explicit output/control request may reuse a quiet source; silence is not EOF. if (refresh_input && latest_input) { - submit(RefreshCaptureInput(*latest_input)); + submit(RefreshCaptureInput(*latest_input, previous_timestamp)); } continue; } diff --git a/native/capture/windows/output_worker.test.cpp b/native/capture/windows/output_worker.test.cpp index 4741fb774..3959ff82b 100644 --- a/native/capture/windows/output_worker.test.cpp +++ b/native/capture/windows/output_worker.test.cpp @@ -71,7 +71,7 @@ void CheckQuietCaptureRefresh() { latest.capture_timestamp = latest.timestamp; const auto previous = latest.timestamp; const auto before = static_cast(MFGetSystemTime()); - const auto refreshed = RefreshCaptureInput(latest); + const auto refreshed = RefreshCaptureInput(latest, previous); const auto after = static_cast(MFGetSystemTime()); assert(refreshed->timestamp >= before && refreshed->timestamp <= after); assert(refreshed->timestamp - previous >= 50'000'000); @@ -81,6 +81,17 @@ void CheckQuietCaptureRefresh() { refreshed->presentation.cx == latest.presentation.cx && refreshed->presentation.cy == latest.presentation.cy && refreshed->color_space == latest.color_space && refreshed->texture.Get() == latest.texture.Get()); + // A delayed real frame can advance presentation beyond the current QPC time. + CaptureInput ahead = latest; + ahead.timestamp = after + 10'000'000; + const auto after_ahead = RefreshCaptureInput(latest, ahead.timestamp); + assert(after_ahead->timestamp > ahead.timestamp); + assert((after_ahead->timestamp - ahead.timestamp) * 90'000 >= 10'000'000); + const auto repeated = RefreshCaptureInput(latest, after_ahead->timestamp); + assert(repeated->timestamp > after_ahead->timestamp); + assert((repeated->timestamp - after_ahead->timestamp) * 90'000 >= 10'000'000); + assert(repeated->capture_timestamp == latest.capture_timestamp); + assert(latest.timestamp == previous); // The compositor changed the picture just before the refresh, but its frame // callback arrives afterwards. It is new capture input, not a stale still. const auto delayed_capture = before - 1; @@ -150,14 +161,16 @@ void CheckRetainedContentAtAdaptedCadence(ID3D11Device* device) { input->presentation = {64, 64}; input->timestamp = static_cast(MFGetSystemTime()); input->capture_timestamp = input->timestamp; + UINT64 previous_timestamp = 0; auto submit = [&](std::shared_ptr next) { for (auto& worker : workers) worker->Submit(next); + previous_timestamp = next->timestamp; }; const auto deadline = EncoderClock::now() + std::chrono::seconds(3); auto wait = [&](auto done) { while (!done()) { assert(EncoderClock::now() < deadline); - if (refresh.exchange(false)) submit(RefreshCaptureInput(*input)); + if (refresh.exchange(false)) submit(RefreshCaptureInput(*input, previous_timestamp)); std::this_thread::sleep_for(std::chrono::milliseconds(34)); } }; @@ -172,20 +185,20 @@ void CheckRetainedContentAtAdaptedCadence(ID3D11Device* device) { submit(input); wait([&] { return changed[0] > 0 && changed[1] > 0; }); // One rendition's drop must not reopen the other rendition's delivered content. - if (refresh.exchange(false)) submit(RefreshCaptureInput(*input)); + if (refresh.exchange(false)) submit(RefreshCaptureInput(*input, previous_timestamp)); std::this_thread::sleep_for(std::chrono::milliseconds(100)); assert(!refresh.load()); const auto before = keys[0].load(); workers[0]->RequestKeyFrame(); - submit(RefreshCaptureInput(*input)); + submit(RefreshCaptureInput(*input, previous_timestamp)); wait([&] { return keys[0] > before; }); - if (refresh.exchange(false)) submit(RefreshCaptureInput(*input)); + if (refresh.exchange(false)) submit(RefreshCaptureInput(*input, previous_timestamp)); std::this_thread::sleep_for(std::chrono::milliseconds(100)); assert(!refresh.load()); workers[0]->SetBitrate(profile.bit_rate); - submit(RefreshCaptureInput(*input)); + submit(RefreshCaptureInput(*input, previous_timestamp)); wait([&] { return published_bitrate[0] == profile.bit_rate; }); - if (refresh.exchange(false)) submit(RefreshCaptureInput(*input)); + if (refresh.exchange(false)) submit(RefreshCaptureInput(*input, previous_timestamp)); std::this_thread::sleep_for(std::chrono::milliseconds(100)); assert(!refresh.load()); for (auto& worker : workers) worker->Stop();