Unify playback and harden preview/audio recovery for SDK 2.1.1

- expose one gvfg_playback DLL backed by static audio and preview libraries, retain public backend data types, and remove obsolete standalone backend exports and unused wrappers

- centralize playback clocks, timestamp scheduling, optional synchronization, audio rate correction, bounded queues and fallback while preserving legacy statistics buffer sizes

- provide still-image presentation and a helper-owned NO SIGNAL card with explicit SDK connection state, native window ownership and UI-thread checks

- report monitoring failures through EAUDIO without stopping video, retain the failed output state across timeline resets, and reject audio-only startup when output is unavailable

- make clearing an unconfigured preview a successful no-op so resetting before the first frame cannot leave playback stopped

- trace actual format-change events before event resets in Debug builds and refresh video metadata after plugin stream recovery while retaining the existing temporary HDMI lock bypass

- update SDK 2.1.1 public documentation, the playback guide and rendered API reference to describe the unified lifecycle, error handling and delivery boundaries

Validation: Full SDK Release build and git diff --cached --check passed. Native tests passed for video/reset lifecycle with synchronization enabled and disabled, signal-card lifecycle, bounded queue pressure and legacy statistics guards. Process-local WASAPI failure injection verified continued video, persistent monitoring failure and audio-only rejection; real WASAPI silence playback advanced the output clock in both synchronization modes. Public exports and rendered PDF pages/portable links were checked. No hardware capture, unplug/replug or long-running acoustic A/V synchronization test was performed.
This commit is contained in:
Mark.lin
2026-10-22 14:02:43 +08:00
parent da2ad24459
commit bfa9397f6b
36 changed files with 2389 additions and 2089 deletions
+59 -248
View File
@@ -1,266 +1,77 @@
# GVFG SDK 錯誤處理架構
# GVFG 擷取與播放錯誤處理
本文說明目前 SDK 如何產生、傳遞與保存錯誤。公開 API 只回傳 `GVFG_*` 狀態;
底層 library 的原始錯誤只保留在內部 diagnostics。
依目前公開標頭、gvfg_error_state.h、Capture facade 與 vendor adapter 整理。
Capture 與 Playback 是兩套 status,必須使用各自的 strerror,不可只看相同整數就混用。
## 1. 三層錯誤資訊
## 1. Capture status
```text
公開 gvfg_status_t
0 成功;負數為公開 GVFG 錯誤
| 值 | 名稱 | 意義 |
|---:|---|---|
| 0 | GVFG_OK | 操作成功 |
| -1 | GVFG_EINVAL | 參數或 token 不符 |
| -2 | GVFG_ENODEV | 沒有裝置或無法開啟 |
| -3 | GVFG_ESTATE | channel/read/held token 狀態不允許操作 |
| -4 | GVFG_EIO | SDK/backend I/O、GPU 或內部 extension 失敗 |
| -5 | GVFG_ENOTSUP | 不支援的格式或功能 |
| -6 | GVFG_ETIMEOUT | 等待期限用完或 start 未取得有效 signal |
| -7 | GVFG_EBUSY | 裝置或資源忙碌 |
ChannelErrorState 詳細字串
公開文字只指出操作被拒絕或 capture backend 失敗,不暴露底層 library
目前 GigabyteCaptureSession::from_vendor 將 OK 轉 GVFG_OK、DEV_BUSY 轉 EBUSY、
CONTEXT_ERROR 轉 ESTATE、VIDEO_CHN_INVALID 轉 EINVAL,其餘 vendor 失敗轉 EIO。
不要沿用曾經存在的 vendor 正數原樣回傳說明。
Internal debug API
保存底層 API 名稱、原始 code、enum 名稱與其他 driver/OS 診斷
```
## 2. 詳細文字與內部 detail
例如 driver IOCTL 失敗可能得到:
ChannelErrorState 由 handle 逐 channel 持有,facade/adapter 使用同一份錯誤狀態,
mutex 保護 message 與 vendor detail。公開 detail 是客戶可理解的操作原因;
內部 vendor_detail 另保存 API、原始 code 與名稱,供 private debug API 使用。
```text
回傳值:GVFG_EIO
詳細字串:copy get frame failed: The device is not ready. (21)
```
## 2. 公開 status
公開型別是 `gvfg_status_t`,常見分類如下:
| Status | 意義 | 常見情況 |
|---|---|---|
| `GVFG_OK` | 成功 | 操作完成 |
| `GVFG_EINVAL` | 參數錯誤 | null pointer、錯誤 channel、frame token 不符 |
| `GVFG_ENODEV` | 找不到裝置 | device index 不存在 |
| `GVFG_ESTATE` | 呼叫時機或狀態錯誤 | 尚未 open、尚未 start、上一張 frame 未 release |
| `GVFG_EIO` | SDK 自身 I/O 失敗 | GPU、register extension 或 SDK 內部資料錯誤 |
| `GVFG_ENOTSUP` | 不支援 | pixel format 或功能不支援 |
| `GVFG_ETIMEOUT` | 等待超時 | deadline 前沒有 frame/event |
| `GVFG_EBUSY` | 資源忙碌 | 裝置或 capture resource 已被使用 |
`gvfg_strerror(status)` 對公開狀態回傳固定分類文字。失敗時可以另外查詢不含底層
library 資訊的 channel detail。
## 3. 每條 channel 的 `ChannelErrorState`
`gvfg_handle_t` 直接持有兩份錯誤狀態:
公開程式可在 Capture 失敗時查 `gvfg_get_channel_last_sdk_error_detail()`,
搭配原始 API 名稱與 status。SDK 沒有宣告呼叫端 GetLastError() 就等於本次 SDK 錯誤;
不要在 DLL 返回後任意讀 Win32 last-error 作為 SDK 的權威原因。
正常 timeout/stop wake 不應當成永久故障,舊 detail 也不能自動配到下一次 timeout。
```cpp
std::array<ChannelErrorState, 2> channelErrors;
```
對應關係:
```text
channelErrors[0] = CH0 最近一次詳細 fault/rejection
channelErrors[1] = CH1 最近一次詳細 fault/rejection
```
每個 `gvfg_channel_session_t` 建立時,取得對應錯誤狀態的 reference:
```cpp
explicit gvfg_channel_session_t(ChannelErrorState &errorState)
: errorState(errorState)
{
gvfg_frame_t frame{};
const auto st = gvfg_read_channel_frame(handle, channel, &frame, 100);
if (st == GVFG_OK) {
// 在這裡完成使用、複製或 submit Playback。
const auto released = gvfg_release_channel_frame(handle, channel, &frame);
// released 也需要檢查;不要先修改原 token。
} else if (st != GVFG_ETIMEOUT) {
char detail[512]{};
gvfg_get_channel_last_sdk_error_detail(handle, channel, detail, sizeof(detail));
// 顯示 API 名稱、gvfg_strerror(st) 與公開 detail。
}
```
backend 也取得同一份 reference:
## 3. Playback status 與非同步錯誤
```cpp
backend = std::make_unique<GigabyteCaptureSession>(errorState);
```
Playback 的錯誤型別是 gvfg_playback_status_t:OK=0、EINVAL=-1、ESTATE=-2、
EOUTPUT=-3、EQUEUE_FULL=-4、EDISCONTINUITY=-5。
例如 Capture 的 -4 是 EIO,Playback 的 -4 是 queue full;這正是不能混用 strerror 的原因。
因此同一條 channel 的三層共用同一份詳細訊息:
submit 是接受/複製,不是實體輸出完成。非同步 GPU/WASAPI 失敗會保存為 last_error,
後續 submit 回報該錯誤;running 仍可能為 1。get_stats 回傳 OK 只是查詢成功。
目前輸出錯誤沒有自動重建裝置的保證,由 caller 協調停止與重啟。
```text
公開 C API wrapper
↓
gvfg_channel_session_t
↓
GigabyteCaptureSession
↓
同一個 ChannelErrorState
```
| 情況 | ownership 與後續處理 |
|---|---|
| read timeout/失敗,沒有 frame | 不 release 未取得的 token |
| read 成功,submit 成功 | submit 完成複製,release 原始 token |
| read 成功,submit 拒收/失敗 | 仍 release 原始 token;若要重試,先保存應用程式自有副本 |
| Playback queue 壓力 | 自動進入獨立播放並略過過時資料;不因 queue 滿而停止擷取,單筆超出配置容量則是 EINVAL |
| Playback timestamp 不連續 | 協調 producer 後 reset;音訊格式改變用新 config stop/start |
| 過期預覽或 GPU busy 略過 | 計入 video_dropped,不當成 Capture failure |
| Playback EOUTPUT | 檢查裝置/視窗與輸出路徑,明確決定重啟,不無限重試 |
即使 `gvfg_open_channel()` 中途失敗、session 沒有存進 `channels[]`,詳細錯誤仍保留在 `gvfg_handle_t::channelErrors[]`。
## 4. 生命週期與 Demo
## 4. 為什麼 `ChannelErrorState` 內部有 mutex
關閉前先停止並 join producer,完成所有成功 read 的 release,再結束 Playback 與 Capture。
不要讓另一個 thread 在 handle destroy 後繼續呼叫 getter/submit。
reset/stop 是取消待播資料,不是錯誤重試或無損 drain。
```cpp
class ChannelErrorState final
{
public:
void set(std::string message);
void clear();
std::string message() const;
private:
mutable std::mutex mutex_;
std::string message_;
};
```
backend event thread、capture/read thread 與 application thread 都可能產生或讀取錯誤。`mutex_` 防止 `std::string` 同時讀寫造成 data race。
`message()` 回傳一份字串 copy,而不是把內部 `c_str()` 直接交出去;離開 lock 後仍安全。
`gvfg_poll_channel_event()` 的 timeout/non-blocking 無資料不會寫入
`ChannelErrorState`。目前 `gvfg_read_channel_frame()` 的 backend timeout 會經過
`GigabyteCaptureSession::reject()`,因此會保存 timeout 詳細訊息。新的
`gvfg_start_channel()` request 會先清除舊內容;若 start 失敗,失敗路徑會立即寫入新的
詳細資訊。
這裡必須區分「目前實作」與「可能希望的契約」:公開 header 曾將所有
`GVFG_ETIMEOUT` 描述成不保存,但目前 backend 並非如此。若要改成 timeout 全部不覆寫,
應修改 backend 行為,而不是只改文件。
## 5. `reject()` 與 `fail()` 的差異
### `reject()`:SDK 主動拒絕不合法操作
典型例子:
```cpp
if (frameHeld)
return reject(
GVFG_ESTATE,
"previous frame has not been released");
```
這不是 driver 呼叫失敗,而是 SDK 在呼叫 driver 前就判定狀態不合法。
常見來源:
- channel 尚未 open/start。
- 已有 read 正在進行。
- 上一張 frame 尚未 release。
- release token 與目前 held frame 不符。
- 設定功能的呼叫時機不合法。
### `fail()`:Win32/driver 操作已經失敗
backend 的 `fail()` 會組合:
```text
操作名稱 + FormatMessage(GetLastError()) + 數字 error code
```
例如:
```cpp
return fail(GIGABYTE_EIO, "wait DMA event", err);
```
同時會:
1. 寫入 `ChannelErrorState`。
2. 透過 error diagnostic log 輸出。
3. 回傳 backend status。
## 6. Backend result 如何回到公開 API
SDK 將底層 library 錯誤轉成穩定的公開狀態:
```text
GVFG_HRESULT_OK -> GVFG_OK
GVFG_HRESULT_DEV_BUSY -> GVFG_EBUSY
GVFG_HRESULT_CONTEXT_ERROR -> GVFG_ESTATE
GVFG_HRESULT_VIDEO_CHN_INVALID -> GVFG_EINVAL
其他 GVFG_HRESULT -> GVFG_EIO
```
公開 detail 只保存客戶可理解的失敗原因。內部 `gvfg_debug.h` 另外保存最後一次失敗的
library API、原始 code 與 enum 名稱;該 header 不隨客戶套件交付。
## 7. Application 正確取得錯誤的方式
```cpp
const gvfg_status_t status =
gvfg_read_channel_frame(handle, channel, &frame, 1000);
if (status < 0 && status != GVFG_ETIMEOUT)
{
char detail[512] = {};
gvfg_get_channel_last_sdk_error_detail(
handle,
channel,
detail,
sizeof(detail));
printf("%s: %s\n", gvfg_strerror(status), detail);
}
```
應在失敗後立即讀取,因為同一 channel 後續發生的新錯誤可能覆蓋舊訊息。
`GVFG_ETIMEOUT` 本身已完整表達「期限內沒有結果」,不應讀取詳細錯誤並把先前內容配到這次 timeout。
## 8. Frame 錯誤時的 ownership 規則
| 情況 | 是否呼叫 release | 原因 |
|---|---:|---|
| `read` 成功 | 是,恰好一次 | SDK/backend 已進入 held state |
| `GVFG_ETIMEOUT` | 否 | 沒有 frame 交付給 caller |
| `GVFG_EIO` 且沒有成功 frame | 否 | caller 沒有取得 ownership token |
| 第二次 read 被 `frameHeld` 拒絕 | 否 | 應先 release 原本成功取得的 frame |
| release token 不符 | 不可清除 held state | 正確 token 仍需重試 release |
| zero-copy 正確 token release | 清除 held state | 只結束 SDK pointer lifetime,不送 Lib/driver release |
| stop 時仍持有 zero-copy token | 清除 held state | driver/Lib 不要求逐 frame release,stop 負責結束 capture lifecycle |
## 9. Transient retry 規則
DMA event 到達後,Acquire/GetFrame 仍可能短暫回報 frame not ready。目前只將以下 Win32 error 視為 transient:
- `ERROR_NOT_READY`
- `ERROR_BUSY`
- `ERROR_RETRY`
- `ERROR_NO_MORE_ITEMS`
處理規則:
```text
transient error
-> 保留原始 timeout deadline
-> 等下一次 event
-> deadline 到達後回 GVFG_ETIMEOUT
其他 error
-> 保存 GetLastError()
-> fail(...)
-> 立即回 GVFG_EIO
```
不得使用無界 retry,也不得加入 `Sleep()` 讓總等待時間超出 caller 指定的 timeout。
## 10. Stop 與錯誤
`stop()` 的核心順序是:
```text
facade running = false
-> 清除 facade held frame
-> backend stop_stream()
-> 停 DMA/喚醒 wait
-> 等 read 結束
-> 清除 backend pending zero-copy token
-> 停 event monitoring/thread
```
依目前 driver/Lib 契約,zero-copy 公開 release 不送底層 release command。Token 驗證
仍由 SDK 執行;錯誤 token 不清除 held state,正確 token 或 stop 才結束 SDK lifetime。
正常 `stop()` 為了結束 blocking read/event poll 而喚醒 waiter 時,只回傳 `GVFG_ESTATE`,不寫入
永久錯誤;application 應以自己的 stop flag 判斷這是不是預期的結束流程。
## 11. 查錯順序
遇到錯誤時依序記錄:
1. 公開 API 名稱與 `gvfg_status_t`。
2. 失敗時呼叫 `gvfg_get_channel_last_sdk_error_detail()` 取得公開 detail。
3. channel index、copy/zero-copy mode、解析度與格式。
4. 是否有成功 read 但尚未 release 的 frame。
5. 是否正在 stop、拔插或 format change。
6. 內部版本再查 debug API 的 library API、原始 code,以及 driver/Win32 資訊。
不要只根據 `GVFG_EIO` 推斷 driver 壞掉;它只是分類,實際失敗邊界必須看詳細字串與當時狀態。
Customer Demo 只顯示 API 名稱與必要公開錯誤;queue/PTS/driver counters 屬於另行選用的診斷。
內部分支的 Debug diagnostics 與 Release Demo 分開,不能把內部輸出當成公開 API 契約。
完整欄位與狀態限制見 [API Reference](GVFG_Customer_doc/GVFG_CUSTOMER_API_REFERENCE.md),
操作流程見 [Playback 手冊](GVFG_PLAYBACK_API.md)。