// F1 on real hardware, deterministically: stop + start while a read sits between // capture and delivery. A frame processor (the hook OSD uses) parks the reader in // exactly that window, so no timing luck is needed. // // Exit 0: fixed behaviour (the read is rejected, the next read works). // Exit 1: F1 reproduced (stale frame delivered, release rejected or channel stuck). // Exit 2: not verifiable here (no device / no signal). Exit 3: unexpected result. #include #include #include #include #include #include #include "gvfg_capture.h" #include "gvfg_frame_processor.h" namespace { std::mutex g_mutex; std::condition_variable g_cv; bool g_armed = false; bool g_parked = false; bool g_resume = false; gvfg_status_t parkerCreate(void **context) { *context = nullptr; return GVFG_OK; } void parkerDestroy(void *) {} gvfg_status_t parkerProcess(void *, const gvfg_frame_t &in, gvfg_frame_t &out, bool &applied, gvfg_debug_osd_frame_timing_t *, uint8_t *, bool *inPlace) { out = in; applied = false; // keep the backend hold, like OSD off / in-place OSD if (inPlace) *inPlace = false; std::unique_lock lock(g_mutex); if (g_armed) { g_armed = false; g_parked = true; g_cv.notify_all(); g_cv.wait(lock, [] { return g_resume; }); } return GVFG_OK; } gvfg_status_t parkerControl(void *, const void *) { return GVFG_OK; } const gvfg_frame_processor kParker{parkerCreate, parkerDestroy, parkerProcess}; const char *name(gvfg_status_t status) { return gvfg_strerror(status); } void lastError(gvfg_handle handle, const char *label) { char message[512] = {}; gvfg_get_channel_last_sdk_error_detail(handle, GVFG_CHANNEL_0, message, sizeof(message)); std::printf(" %-16s last SDK error: %s\n", label, message[0] ? message : "(none)"); } } int main() { std::printf("GVFG SDK %s\n", gvfg_get_version()); if (gvfg_enumerate_devices(nullptr, 0) < 1) { std::puts("NOT VERIFIED: no GVFG device"); return 2; } gvfg_handle handle = nullptr; if (gvfg_create(&handle) != GVFG_OK || gvfg_open_channel(handle, 0, GVFG_CHANNEL_0) != GVFG_OK) { std::puts("NOT VERIFIED: cannot open device 0 channel 0"); return 2; } gvfg_status_t st = gvfg_start_channel(handle, GVFG_CHANNEL_0); if (st != GVFG_OK) { std::printf("NOT VERIFIED: start failed (%s); is a signal connected?\n", name(st)); gvfg_destroy(handle); return 2; } st = gvfg_control_frame_processor(handle, GVFG_CHANNEL_0, &kParker, true, parkerControl, nullptr, "attach parker failed"); if (st != GVFG_OK) { std::printf("UNEXPECTED: cannot attach frame processor (%s)\n", name(st)); gvfg_destroy(handle); return 3; } // Warm up: capture really flows before the race. int warm = 0; for (int i = 0; i < 20 && warm < 5; ++i) { gvfg_frame_t frame{}; if (gvfg_read_channel_frame(handle, GVFG_CHANNEL_0, &frame, 1000) == GVFG_OK) { ++warm; gvfg_release_channel_frame(handle, GVFG_CHANNEL_0, &frame); } } if (warm < 5) { std::puts("NOT VERIFIED: frames are not flowing"); gvfg_destroy(handle); return 2; } { std::lock_guard lock(g_mutex); g_armed = true; } gvfg_status_t readStatus = GVFG_ETIMEOUT; gvfg_status_t releaseStatus = GVFG_ETIMEOUT; bool released = false; std::thread reader([&] { gvfg_frame_t frame{}; readStatus = gvfg_read_channel_frame(handle, GVFG_CHANNEL_0, &frame, 2000); if (readStatus == GVFG_OK) { released = true; releaseStatus = gvfg_release_channel_frame(handle, GVFG_CHANNEL_0, &frame); } }); bool parked = false; { std::unique_lock lock(g_mutex); parked = g_cv.wait_for(lock, std::chrono::seconds(5), [] { return g_parked; }); } gvfg_status_t stopStatus = GVFG_ETIMEOUT; gvfg_status_t startStatus = GVFG_ETIMEOUT; if (parked) { stopStatus = gvfg_stop_channel(handle, GVFG_CHANNEL_0); startStatus = gvfg_start_channel(handle, GVFG_CHANNEL_0); } { std::lock_guard lock(g_mutex); g_resume = true; } g_cv.notify_all(); reader.join(); lastError(handle, "after race:"); gvfg_status_t nextRead = GVFG_ETIMEOUT; gvfg_status_t nextRelease = GVFG_ETIMEOUT; for (int i = 0; i < 5 && nextRead != GVFG_OK; ++i) { gvfg_frame_t frame{}; nextRead = gvfg_read_channel_frame(handle, GVFG_CHANNEL_0, &frame, 1000); if (nextRead == GVFG_OK) nextRelease = gvfg_release_channel_frame(handle, GVFG_CHANNEL_0, &frame); else if (nextRead == GVFG_ESTATE) break; // ESTATE here means the channel is stuck; retrying will not help } lastError(handle, "after next read:"); std::printf("parked in window : %s\n", parked ? "yes" : "no"); std::printf("stop / start : %s / %s\n", name(stopStatus), name(startStatus)); std::printf("racing read : %s\n", name(readStatus)); std::printf("its release : %s\n", released ? name(releaseStatus) : "(not called: no frame)"); std::printf("next read : %s (release %s)\n", name(nextRead), nextRead == GVFG_OK ? name(nextRelease) : "-"); gvfg_stop_channel(handle, GVFG_CHANNEL_0); gvfg_destroy(handle); if (!parked || stopStatus != GVFG_OK || startStatus != GVFG_OK) { std::puts("RESULT: UNEXPECTED (race not staged)"); return 3; } if (readStatus == GVFG_OK && (releaseStatus != GVFG_OK || nextRead != GVFG_OK)) { std::puts("RESULT: F1 REPRODUCED (stale frame delivered after stop->start)"); return 1; } if (readStatus != GVFG_OK && nextRead == GVFG_OK && nextRelease == GVFG_OK) { std::puts("RESULT: FIXED (racing read rejected, channel keeps working)"); return 0; } std::puts("RESULT: UNEXPECTED"); return 3; }