diff --git a/GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp b/GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp index 8bf977e..d068515 100644 --- a/GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp +++ b/GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp @@ -218,7 +218,11 @@ struct gvfg_channel_session_t if (st != GVFG_OK) return st; - running = true; + { + std::lock_guard lock(frameMutex); + ++runGeneration; + running = true; + } return GVFG_OK; } @@ -383,6 +387,7 @@ struct gvfg_channel_session_t return reject(GVFG_ESTATE, "gvfg_read_channel_frame rejected: channel is not running"); const char *stateError = nullptr; + uint64_t readRunGeneration = 0; { std::lock_guard lock(frameMutex); if (frameHeld) @@ -390,7 +395,10 @@ struct gvfg_channel_session_t else if (readInProgress) stateError = "gvfg_read_channel_frame rejected: another read is already in progress"; else + { readInProgress = true; + readRunGeneration = runGeneration; + } } if (stateError) return reject(GVFG_ESTATE, stateError); @@ -478,7 +486,9 @@ struct gvfg_channel_session_t bool stoppedWhileWaiting = false; { std::lock_guard lock(frameMutex); - if (!running.load(std::memory_order_acquire)) + // A stop and start during this read set running back to true, but the + // frame belongs to the stopped stream and its backend hold is gone. + if (!running.load(std::memory_order_acquire) || runGeneration != readRunGeneration) { readInProgress = false; stoppedWhileWaiting = true; @@ -496,7 +506,8 @@ struct gvfg_channel_session_t { if (backendFrameHeld) session->release_frame(); - return GVFG_ESTATE; + return reject(GVFG_ESTATE, + "gvfg_read_channel_frame rejected: capture stopped or restarted during the read"); } updateRuntimeFps(now_ns()); @@ -839,6 +850,7 @@ struct gvfg_channel_session_t bool readInProgress = false; bool frameHeld = false; bool heldFrameNeedsBackendRelease = false; + uint64_t runGeneration = 0; // Protected by frameMutex; bumped by every successful start. uint64_t videoFrameId = 0; gvfg_frame_t heldSessionFrame{}; std::mutex processorMutex; diff --git a/docs/GVFG_Customer_doc/GVFG_CUSTOMER_API_REFERENCE.md b/docs/GVFG_Customer_doc/GVFG_CUSTOMER_API_REFERENCE.md index 141b539..e70678e 100644 --- a/docs/GVFG_Customer_doc/GVFG_CUSTOMER_API_REFERENCE.md +++ b/docs/GVFG_Customer_doc/GVFG_CUSTOMER_API_REFERENCE.md @@ -490,7 +490,7 @@ gvfg_status_t gvfg_read_channel_frame(/* [in] */ gvfg_handle handle, - `timeout_ms`:0 為 non-blocking;`GVFG_TIMEOUT_INFINITE` 為無限等待;其他值為毫秒。 - `GVFG_OK`:成功取得 frame,caller 現在持有一個必須 release 的 token。 - `GVFG_EINVAL`:handle 或 output pointer 為 NULL。 -- `GVFG_ESTATE`:未 running、已有 held frame、已有另一個 read,或等待時被 stop。 +- `GVFG_ESTATE`:未 running、已有 held frame、已有另一個 read,或 read 期間被 stop(包含 stop 後又重新 start;此時不會交出 stop 前的 frame)。 - `GVFG_ETIMEOUT`:期限內沒有 frame。 - `GVFG_EBUSY` 表示 capture resource 正在使用中;`GVFG_EIO` 表示 capture 或 I/O 失敗; `GVFG_ENOTSUP` 表示 SDK 不支援該格式。