This commit is contained in:
Mark.lin
2026-08-21 10:13:48 +08:00
parent 024df690c3
commit f783e21476
3 changed files with 44 additions and 30 deletions
+3 -3
View File
@@ -412,7 +412,7 @@ extern "C"
* Returns: * Returns:
* - GVFG_OK on success. * - GVFG_OK on success.
* - GVFG_EINVAL if handle/out_event is NULL or struct_size is invalid. * - GVFG_EINVAL if handle/out_event is NULL or struct_size is invalid.
* - GVFG_ESTATE if no capture device is open. * - GVFG_ESTATE if no capture device is open or capture has been stopped.
* - GVFG_ETIMEOUT if no event is available before timeout_ms expires. * - GVFG_ETIMEOUT if no event is available before timeout_ms expires.
*/ */
GVFG_API gvfg_status_t gvfg_poll_event( GVFG_API gvfg_status_t gvfg_poll_event(
@@ -430,8 +430,8 @@ extern "C"
* - GVFG_OK on success, including when capture is already stopped. * - GVFG_OK on success, including when capture is already stopped.
* - GVFG_EINVAL if handle is NULL. * - GVFG_EINVAL if handle is NULL.
* *
* This stops DMA capture and invalidates any unreleased frame. Signal and * This stops DMA capture and signal/format event monitoring, invalidates
* format event monitoring remains active until gvfg_destroy(). * any unreleased frame, and wakes blocking gvfg_poll_event() calls.
*/ */
GVFG_API gvfg_status_t gvfg_stop( GVFG_API gvfg_status_t gvfg_stop(
_In_ gvfg_handle handle); _In_ gvfg_handle handle);
@@ -295,9 +295,10 @@ namespace gvfg::internal
signal_present_.store(signalPresent, std::memory_order_release); signal_present_.store(signalPresent, std::memory_order_release);
if (!signalPresent) if (!signalPresent)
{ {
out.connected = 0;
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
signal_metadata_valid_ = false; signal_metadata_valid_ = false;
return PCIES2MM_ENODEV; return PCIES2MM_OK;
} }
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
+39 -26
View File
@@ -239,7 +239,7 @@ struct gvfg_handle_t
} }
} }
querySignal(); // querySignal();
return GVFG_OK; return GVFG_OK;
} }
@@ -282,14 +282,23 @@ struct gvfg_handle_t
gvfg_status_t stop() gvfg_status_t stop()
{ {
running = false; running = false;
eventCv.notify_all();
releaseHeldFrameForStop(); releaseHeldFrameForStop();
if (!backend)
return GVFG_OK;
const pcies2mm_status_t st = backend->stop_stream(); pcies2mm_status_t st = PCIES2MM_OK;
if (st != PCIES2MM_OK) if (backend)
recordError(pcies2mm_error_text(st, backend.get())); {
// stop_stream() joins the backend event thread, so no new event can
// be queued after it returns.
st = backend->stop_stream();
if (st != PCIES2MM_OK)
recordError(pcies2mm_error_text(st, backend.get()));
}
{
std::lock_guard<std::mutex> lock(eventMutex);
eventQueue.clear();
}
eventCv.notify_all();
return map_status(st); return map_status(st);
} }
@@ -318,9 +327,7 @@ struct gvfg_handle_t
out.pixel_format = to_gvfg_pixel_format(pixelFormat); out.pixel_format = to_gvfg_pixel_format(pixelFormat);
out.bit_depth = static_cast<int>(bitDepth); out.bit_depth = static_cast<int>(bitDepth);
} }
// A disconnected input is a normal query result, not a missing-device return map_status(status);
// error. The opened device remains usable for plug-in monitoring.
return status == PCIES2MM_ENODEV ? GVFG_OK : map_status(status);
} }
gvfg_status_t getRuntimeInfo(gvfg_runtime_info_t &out) gvfg_status_t getRuntimeInfo(gvfg_runtime_info_t &out)
@@ -377,7 +384,6 @@ struct gvfg_handle_t
eventQueue.push_back(out); eventQueue.push_back(out);
} }
eventCv.notify_one(); eventCv.notify_one();
} }
pcies2mm_status_t querySignal() pcies2mm_status_t querySignal()
@@ -403,17 +409,25 @@ struct gvfg_handle_t
return GVFG_ESTATE; return GVFG_ESTATE;
const pcies2mm_status_t signalStatus = querySignal(); const pcies2mm_status_t signalStatus = querySignal();
if (signalStatus != PCIES2MM_OK && signalStatus != PCIES2MM_ENODEV) if (signalStatus != PCIES2MM_OK)
return map_status(signalStatus); return map_status(signalStatus);
const bool waitingForSignal = signalStatus == PCIES2MM_ENODEV; bool waitingForSignal = false;
uint32_t configureWidth = 0;
uint32_t configureHeight = 0;
pcies2mm_pixel_format_t configureFormat = PCIES2MM_PIXFMT_UNKNOWN;
{
std::lock_guard<std::mutex> lock(stateMutex);
waitingForSignal = !signalConnected;
configureWidth = waitingForSignal ? 2 : width;
configureHeight = waitingForSignal ? 1 : height;
configureFormat =
waitingForSignal || pixelFormat == PCIES2MM_PIXFMT_UNKNOWN ? PCIES2MM_PIXFMT_YUY2 : pixelFormat;
}
// configure_stream() is also used to enter event-monitoring mode. Keep // configure_stream() is also used to enter event-monitoring mode. Keep
// its inactive placeholder ring minimal; the real signal descriptor // its inactive placeholder ring minimal; the real signal descriptor
// replaces it before DMA is enabled after reconnect. // replaces it before DMA is enabled after reconnect.
const uint32_t configureWidth = waitingForSignal ? 2 : width;
const uint32_t configureHeight = waitingForSignal ? 1 : height;
const pcies2mm_pixel_format_t configureFormat =
waitingForSignal || pixelFormat == PCIES2MM_PIXFMT_UNKNOWN ? PCIES2MM_PIXFMT_YUY2 : pixelFormat;
pcies2mm_stream_desc_t desc{}; pcies2mm_stream_desc_t desc{};
desc.width = configureWidth; desc.width = configureWidth;
@@ -575,35 +589,37 @@ struct gvfg_handle_t
return GVFG_ESTATE; return GVFG_ESTATE;
std::unique_lock<std::mutex> lock(eventMutex); std::unique_lock<std::mutex> lock(eventMutex);
const auto eventAvailable = [this]() const auto eventAvailableOrStopped = [this]()
{ {
return !eventQueue.empty(); return !eventQueue.empty() ||
!running.load(std::memory_order_acquire);
}; };
bool waitConditionMet = false; bool waitConditionMet = false;
if (timeoutMs == 0) if (timeoutMs == 0)
{ {
// Non-blocking: inspect the current state without sleeping. // Non-blocking: inspect the current state without sleeping.
waitConditionMet = eventAvailable(); waitConditionMet = eventAvailableOrStopped();
} }
else if (timeoutMs == GVFG_TIMEOUT_INFINITE) else if (timeoutMs == GVFG_TIMEOUT_INFINITE)
{ {
// Wake when an event arrives or capture is stopped. // Wake when an event arrives or capture is stopped.
eventCv.wait(lock, eventAvailable); eventCv.wait(lock, eventAvailableOrStopped);
waitConditionMet = true; waitConditionMet = true;
} }
else else
{ {
waitConditionMet = eventCv.wait_for(lock, waitConditionMet = eventCv.wait_for(lock,
std::chrono::milliseconds(timeoutMs), std::chrono::milliseconds(timeoutMs),
eventAvailable); eventAvailableOrStopped);
} }
if (!waitConditionMet) if (!waitConditionMet)
return GVFG_ETIMEOUT; return GVFG_ETIMEOUT;
if (eventQueue.empty()) if (eventQueue.empty())
return GVFG_ETIMEOUT; return running.load(std::memory_order_acquire) ? GVFG_ETIMEOUT
: GVFG_ESTATE;
out = eventQueue.front(); out = eventQueue.front();
eventQueue.pop_front(); eventQueue.pop_front();
@@ -1011,6 +1027,3 @@ extern "C"
return handle->debugWriteRegister(offset, value); return handle->debugWriteRegister(offset, value);
} }
} }