- 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.
351 lines
16 KiB
Markdown
351 lines
16 KiB
Markdown
# GVFG 客戶 API 使用手冊
|
||
|
||
本文件說明 SDK 2.1.1 的公開擷取介面 `gvfg_capture.h`。
|
||
影像/音訊/同步播放另使用 `gvfg_playback.h`,完整流程見
|
||
[統一影音播放使用手冊](GVFG_PLAYBACK_API.md)。Capture 與 Playback 是不同 handle 與 DLL。
|
||
|
||
客戶套件不應包含 `gvfg_debug.h`、`sdk/gvfg/src/`、driver IOCTL、FPGA
|
||
register、IRQ 或 DMA ring 實作細節。
|
||
|
||
## 1. 套件內容
|
||
|
||
核心擷取:
|
||
|
||
```text
|
||
include/gvfg_capture.h
|
||
lib/gvfg.lib
|
||
bin/gvfg.dll
|
||
```
|
||
|
||
Application 只 link `gvfg.lib`。內部 Gigabyte library 已靜態連結進
|
||
`gvfg.dll`,不需要部署 `giga_ioctl.dll` 或額外公開 private header。
|
||
|
||
選用播放(純影像、純音訊或影音):
|
||
|
||
```text
|
||
include/gvfg_playback.h
|
||
include/gvfg_preview.h
|
||
include/gvfg_audio_playback.h
|
||
lib/gvfg_playback.lib
|
||
bin/gvfg_playback.dll
|
||
```
|
||
|
||
目前支援 Windows x64。API 使用 C ABI,可由 C 或 C++ 呼叫。
|
||
|
||
## 2. 最小擷取流程
|
||
|
||
```text
|
||
gvfg_enumerate_devices
|
||
-> gvfg_create
|
||
-> gvfg_set_channel_zero_copy_enabled (optional, before channel open)
|
||
-> gvfg_open_channel
|
||
-> gvfg_set_channel_audio_enabled (optional, before start)
|
||
-> gvfg_start_channel
|
||
-> 重複:
|
||
gvfg_read_channel_frame
|
||
使用/複製/轉換 frame
|
||
gvfg_release_channel_frame
|
||
若已啟用 audio,另一個 worker thread 重複:
|
||
gvfg_read_channel_audio_frame
|
||
使用/複製 PCM
|
||
gvfg_release_channel_audio_frame
|
||
-> gvfg_stop
|
||
-> gvfg_destroy
|
||
```
|
||
|
||
完整範例:
|
||
|
||
```c
|
||
#include <gvfg_capture.h>
|
||
|
||
int main(void)
|
||
{
|
||
gvfg_device_info_t devices[GVFG_MAX_DEVICES] = {0};
|
||
int count = gvfg_enumerate_devices(devices, GVFG_MAX_DEVICES);
|
||
if (count <= 0)
|
||
return 1;
|
||
|
||
gvfg_handle h = NULL;
|
||
if (gvfg_create(&h) != GVFG_OK)
|
||
return 2;
|
||
|
||
gvfg_status_t st = gvfg_open_channel(h, 0, GVFG_CHANNEL_0);
|
||
if (st == GVFG_OK)
|
||
st = gvfg_start_channel(h, GVFG_CHANNEL_0);
|
||
|
||
if (st == GVFG_OK) {
|
||
for (int i = 0; i < 100; ++i) {
|
||
gvfg_frame_t frame = {0};
|
||
st = gvfg_read_channel_frame(h, GVFG_CHANNEL_0, &frame, 1000);
|
||
if (st == GVFG_ETIMEOUT)
|
||
continue;
|
||
if (st != GVFG_OK)
|
||
break;
|
||
|
||
/* 在 release 前處理、轉換或複製 frame.data。 */
|
||
|
||
st = gvfg_release_channel_frame(h, GVFG_CHANNEL_0, &frame);
|
||
if (st != GVFG_OK)
|
||
break;
|
||
}
|
||
}
|
||
|
||
gvfg_stop(h);
|
||
gvfg_destroy(h);
|
||
return st == GVFG_OK ? 0 : 3;
|
||
}
|
||
```
|
||
|
||
## 3. 執行緒與 frame 所有權
|
||
|
||
- API 是 pull model;應用程式自行決定在哪個執行緒呼叫
|
||
`gvfg_read_channel_frame()`。GUI 程式建議每個 channel 使用一個 worker thread,再把 UI 更新送回
|
||
UI thread。
|
||
- `frame.data` 是唯讀的借用 pointer,可能指向原生擷取 buffer 或 SDK 處理後的
|
||
buffer。Zero-copy 設定控制擷取方式;啟用 OSD 時仍可能交付 SDK-owned buffer。
|
||
Pointer 只在對應的 `gvfg_release_channel_frame()` 前有效,資料若要長期保存,必須先複製。
|
||
- 每個 channel 同時最多持有一個 frame。尚未 release 又對同一 channel 呼叫
|
||
`gvfg_read_channel_frame()`,會回傳 `GVFG_ESTATE`。
|
||
- release 時傳回 read 取得的 `gvfg_frame_t`;SDK 以 `data + frame_id` 確認目前 held frame。
|
||
- `gvfg_stop()` 會中止等待並使尚未 release 的 frame 失效。
|
||
- 同一個 handle 的 lifecycle 操作應由應用程式自行序列化;不要同時 open、
|
||
start、stop 或 destroy。
|
||
|
||
Audio 採用相同的 pull 與 ownership 模型:
|
||
|
||
- 建議使用獨立 audio worker thread,避免阻塞 video read 或 UI thread。
|
||
- `gvfg_audio_frame_t.data` 是 SDK-owned PCM,只在對應的
|
||
`gvfg_release_channel_audio_frame()` 前有效。
|
||
- 同一 channel 同時只能持有一個 audio frame;未 release 再 read 會回傳
|
||
`GVFG_ESTATE`。
|
||
- release 時傳回 read 取得的 `gvfg_audio_frame_t`;SDK 以 `data + frame_id` 確認目前 held frame。
|
||
- 若播放或錄音需要在 release 後繼續使用 PCM,Application 必須先複製。
|
||
- Capture 保有自己的交付 buffer;播放副本、queue、同步與輸出由 Playback 管理。
|
||
- 使用 Playback 時,submit 已完成複製後即可 release,Demo 不自行實作音訊調速或同步。
|
||
- 播放錯誤由 Playback status/last_error 回報,caller 決定停止與重啟。
|
||
輸出裝置故障後,應用程式須處理錯誤並決定是否重新啟動播放。
|
||
|
||
`timeout_ms` 的共同規則:`0` 表示不等待;`GVFG_TIMEOUT_INFINITE` 表示無限等待;
|
||
其他值的單位為毫秒。
|
||
|
||
## 4. 狀態碼
|
||
|
||
| 狀態 | 說明 |
|
||
| --------------- | --------------------------- |
|
||
| `GVFG_OK` | 成功 |
|
||
| `GVFG_EINVAL` | 參數、channel 或 frame token 無效 |
|
||
| `GVFG_ENODEV` | 找不到裝置或裝置無法開啟 |
|
||
| `GVFG_ESTATE` | 呼叫時機或 handle 狀態不正確 |
|
||
| `GVFG_EIO` | SDK/backend I/O、GPU 或 internal extension 失敗 |
|
||
| `GVFG_ENOTSUP` | 格式或功能不支援 |
|
||
| `GVFG_ETIMEOUT` | 等待逾時 |
|
||
| `GVFG_EBUSY` | 裝置或 capture resource 正在使用中 |
|
||
|
||
公開 API 不暴露底層 capture library 的錯誤型別。所有失敗都會轉成上述
|
||
`GVFG_E*` 狀態;Application 應使用 `status != GVFG_OK` 判斷,不可套用
|
||
Windows `FAILED()`/`SUCCEEDED()`。
|
||
|
||
可用 `gvfg_strerror()` 取得靜態英文說明字串;呼叫端不可釋放該字串。
|
||
|
||
可用 `gvfg_get_version()` 查詢目前實際載入的 `gvfg.dll` 版本。回傳值為
|
||
靜態字串,例如 `"1.0.0"`,呼叫端不可釋放。
|
||
需要記錄最近一次失敗的詳細原因時,可在 API 失敗後立即呼叫
|
||
`gvfg_get_channel_last_sdk_error_detail()`。此 API 只提供不含底層 library 名稱與原始錯誤碼的
|
||
公開診斷文字;library、driver、register 等實作資訊只保留在未隨客戶套件交付的 debug API。
|
||
|
||
## 5. 資料格式
|
||
|
||
### 原生擷取格式
|
||
|
||
| 格式 | `pixel_format` | bit depth | 每列大小 |
|
||
| ----------------- | ------------------:| ---------:| -----------:|
|
||
| YUY2 packed 4:2:2 (`Y0 U0 Y1 V0`) | `GVFG_PIXFMT_YUY2` | 8 | `width * 2` |
|
||
| Y210 packed 4:2:2 | `GVFG_PIXFMT_Y210` | 10 | `width * 4` |
|
||
|
||
實際列距以 `gvfg_frame_t.row_stride_bytes` 為準。
|
||
目前不支援的輸入格式會回傳 `GVFG_ENOTSUP`。可用
|
||
`gvfg_pixel_format_name()` 取得靜態格式名稱。
|
||
|
||
### `gvfg_frame_t`
|
||
|
||
- `data`、`data_size`:SDK buffer 與可用 byte 數。
|
||
- `width`、`height`:影像尺寸。
|
||
- `row_stride_bytes`:相鄰兩列起點的 byte 距離。
|
||
- `pixel_format`、`bit_depth`:原生 payload 格式。
|
||
- `frame_id`:SDK 成功交付序號;每次 channel Start 從 1 重新開始。
|
||
- `timestamp_ns`:SDK monotonic delivery timestamp,與 audio 使用相同 clock domain。
|
||
|
||
### PCM audio
|
||
|
||
`gvfg_audio_format_t` 只提供上層實際需要的播放格式:`sample_rate`、
|
||
`channels`、`bits_per_sample`。Driver frame 大小與 block alignment 是 backend
|
||
細節,不要求 Application 配置相同大小的 buffer。
|
||
|
||
`gvfg_audio_frame_t` 包含:
|
||
|
||
- `data`、`data_size`:SDK-owned PCM 與本次有效 byte 數。
|
||
- `sample_rate`、`channels`、`bits_per_sample`:此 frame 的 PCM 格式。
|
||
- `frame_id`:SDK 成功交付序號;每次 channel Start 從 1 重新開始。
|
||
- `timestamp_ns`:SDK monotonic delivery timestamp,與 video 使用相同 clock domain。
|
||
|
||
`timestamp_ns` 用於比較同一 process/session 內的 video/audio delivery timing;它
|
||
不是 driver 或硬體 capture timestamp。
|
||
|
||
目前 audio 僅支援 CH0,且必須與 video 一起啟動;不支援 audio-only。公開
|
||
audio API 不是 zero-copy,但上層仍使用和 video 相同的 read/release contract。
|
||
|
||
## 6. 公開 API
|
||
|
||
### 裝置與生命週期
|
||
|
||
- `gvfg_enumerate_devices(out_devices, max_devices)`:列舉裝置。傳入
|
||
`NULL, 0` 可只查數量;回傳值是寫入數量或可用裝置數,無裝置時為 `0`。
|
||
- `gvfg_create(&handle)`:建立 closed session。
|
||
- `gvfg_set_channel_zero_copy_enabled(handle, channel, enabled)`:替指定 channel
|
||
選用 zero-copy;只能在該 channel open 前呼叫,預設為關閉。
|
||
- `gvfg_get_channel_zero_copy_enabled(handle, channel, &enabled)`:查詢指定
|
||
channel 選擇的模式。
|
||
- `gvfg_open_channel(handle, device_index, channel)`:開啟列舉所得裝置,channel
|
||
必須為 `GVFG_CHANNEL_0` 或 `GVFG_CHANNEL_1`。
|
||
- `gvfg_start_channel(handle, channel)`:開始指定 channel 擷取。每次明確 Start 都會重新查詢一次
|
||
signal;若目前沒有 lock,回傳 `GVFG_ETIMEOUT`,channel 維持 stopped,接上訊號後須由
|
||
Application 再次呼叫 Start。
|
||
- `gvfg_stop(handle)`:停止擷取;重複呼叫仍回傳成功。
|
||
- `gvfg_destroy(handle)`:必要時先停止,再銷毀 handle。銷毀後不得再使用。
|
||
|
||
Zero-copy mode 的 driver lifecycle 由 SDK 管理:open 時 enable,destroy/close 前 disable。
|
||
每次成功 `gvfg_read_channel_frame()` 後仍須呼叫 `gvfg_release_channel_frame()`,用來結束
|
||
公開 pointer/token lifetime;依目前 Lib 契約不送逐 frame driver release。Device open 後
|
||
不可直接切換;應 destroy session、重新 create、設定模式後再 open。
|
||
|
||
### Frame
|
||
|
||
- `gvfg_read_channel_frame(handle, channel, &frame, timeout_ms)`:取得原生或 SDK 處理後的唯讀
|
||
frame;copy/zero-copy acquisition 的 release contract 相同。
|
||
- `gvfg_release_channel_frame(handle, channel, &frame)`:釋放原 frame token。
|
||
|
||
### SDK OSD
|
||
|
||
OSD 由 `gvfg.dll` 提供。啟用後,
|
||
`gvfg_read_channel_frame()` 交付含 OSD 的完整 YUY2/Y210 frame;使用後照常 release。
|
||
|
||
- `gvfg_measure_osd_text(text, text_length, font_size_px, &width, &height)`:量測與實際
|
||
OSD 相同的單行文字框;不需要 handle,也不會開啟 capture device。
|
||
- `gvfg_set_channel_osd(handle, channel, &config)`:啟用或更新已 open channel 的 OSD。
|
||
SDK 同步複製文字設定,後續 frame 使用新設定。
|
||
- `gvfg_clear_channel_osd(handle, channel)`:停用後續 frame 的 OSD。
|
||
|
||
`gvfg_channel_osd_config_t` 可設定文字、`x/y`、`font_size_px` 與 `color_rgb`。
|
||
文字為 1–32 個可列印 ASCII 字元(`0x20`–`0x7e`),使用 Arial-preferred normal 字型;
|
||
保持單行,不自動換行。字號必須大於 0;SDK 沒有 viewer 的 512 px UI 上限。
|
||
顏色為 `0x00RRGGBB`,轉為 BT.709 limited range 後由 GPU 與影片混合。
|
||
|
||
量測的 `width/height` 包含排版空白、glyph overhang 及四邊各 1 px 安全邊,
|
||
是保守的完整文字框,不是逐像素緊貼的字形邊界。`x/y` 表示這個框的左上角;
|
||
SDK 會依實際 frame 限制在 `0..frame.width - text_width`、
|
||
`0..frame.height - text_height`。文字框比 frame 大時,SDK 暫不疊字並繼續擷取;
|
||
設定或 frame 尺寸改成可容納後會恢復,不會縮字、截字或換行。
|
||
|
||
量測的兩個 output pointer 不可為 NULL;失敗時可寫入的 output 會歸零。
|
||
無效文字、長度、字號或 pointer 回傳 `GVFG_EINVAL`;量測失敗回傳 `GVFG_EIO`。
|
||
|
||
### Audio frame
|
||
|
||
- `gvfg_set_channel_audio_enabled(handle, channel, enabled)`:在 start 前啟用或停用
|
||
audio;video 永遠保留,不需要組合 stream flags。
|
||
- `gvfg_get_channel_audio_format(handle, channel, &format)`:取得 PCM 播放格式。
|
||
- `gvfg_read_channel_audio_frame(handle, channel, &frame, timeout_ms)`:取得一個
|
||
SDK-owned PCM frame。
|
||
- `gvfg_release_channel_audio_frame(handle, channel, &frame)`:釋放原 audio token。
|
||
|
||
成功 read 後,不論 PCM 是送往播放、錄音或被丟棄,都必須正好 release 一次。
|
||
|
||
`gvfg_set_channel_video_format()` 可在 stream 尚未開始或已 stop 時選擇 YUY2/Y210。
|
||
此功能由 SDK 的 capture backend 實作。
|
||
|
||
### 查詢
|
||
|
||
- `gvfg_get_channel_signal_status(handle, channel, &status)`:查詢指定 channel 的連線、尺寸、
|
||
原生格式與 bit depth。沒有輸入訊號是正常狀態:回傳 `GVFG_OK` 且
|
||
`connected == 0`。Channel 尚未 running 時會向 capture backend 更新;running 期間則回傳
|
||
SDK cache,不再額外讀取硬體。
|
||
- `gvfg_get_channel_runtime_info(handle, channel, &info)`:取得指定 channel 已交付 frame 數與
|
||
依 read 間隔估算的 `capture_fps`。start 時統計值重設。
|
||
|
||
### Event
|
||
|
||
`gvfg_poll_channel_event(handle, channel, &event, timeout_ms)` 一次取出指定 channel 的事件,只能在 running
|
||
狀態使用。stop 會喚醒阻塞中的 poll,並回傳 `GVFG_ESTATE`。
|
||
|
||
| Event | 應用程式動作 |
|
||
| -------------------------------- | --------------------------- |
|
||
| `GVFG_EVENT_VIDEO_FORMAT_CHANGED` | Lib 的 video format changed event |
|
||
| `GVFG_EVENT_VIDEO_INPUT_PLUGIN` | Lib 的 video input plug-in event |
|
||
| `GVFG_EVENT_VIDEO_INPUT_UNPLUG` | Lib 的 video input unplug event |
|
||
|
||
`event` 使用 `gvfg_event_type_t`;SDK 直接寫入本次取出的事件種類。
|
||
SDK 會在 format changed、input plug-in 或 input unplug event 放入 public queue 前先同步
|
||
signal cache。Application 收到事件後可呼叫 `gvfg_get_channel_signal_status()` 取得對應的新狀態;
|
||
running 期間此查詢讀取 cache,不會再次向 driver 查詢。
|
||
|
||
## 7. GPU 同步轉換
|
||
|
||
GPU 轉換在 `gvfg.dll` 內完成,呼叫是同步的。必須在
|
||
`gvfg_release_channel_frame()` 前轉換;函式返回後 SDK 不保留 source 或 destination
|
||
pointer。
|
||
|
||
支援輸出:
|
||
|
||
| 格式 | 配置需求 |
|
||
| ------------------------- | --------------------------------------- |
|
||
| `GVFG_GPU_OUTPUT_BGRA8` | `row_bytes >= width * 4`;alpha = 255 |
|
||
| `GVFG_GPU_OUTPUT_RGB10A2` | `row_bytes >= width * 4`;alpha = 3 |
|
||
| `GVFG_GPU_OUTPUT_NV12` | width/height 必須為偶數;`row_bytes >= width` |
|
||
|
||
BGRA8/RGB10A2 的 destination 至少為 `row_bytes * height`。NV12 至少為
|
||
`row_bytes * (height + height / 2)`,排列為 Y plane,接著是 interleaved UV
|
||
plane;使用 BT.709 limited range。
|
||
|
||
可使用通用函式 `gvfg_gpu_convert_to_buffer()`,或便利函式:
|
||
|
||
- `gvfg_gpu_convert_to_bgra8()`
|
||
- `gvfg_gpu_convert_to_rgb10a2()`
|
||
- `gvfg_gpu_convert_to_nv12()`
|
||
|
||
## 8. 播放 API
|
||
|
||
`gvfg_playback.dll` 提供影像、音訊播放與影音同步功能。
|
||
擷取仍由應用程式呼叫 `gvfg.dll`。典型流程:
|
||
|
||
```text
|
||
gvfg_playback_create -> attach_window -> default_config -> start
|
||
capture video/audio -> submit_video / submit_audio -> release SDK frame
|
||
停止提交 -> gvfg_playback_stop -> gvfg_playback_destroy
|
||
```
|
||
|
||
submit 成功前已完成資料複製,返回後即可 release SDK frame;失敗時也必須 release,
|
||
時間不連續或輸出錯誤仍須處理;queue 壓力由 Playback 自動降級並略過過時資料,不要求停止擷取。
|
||
stop/reset 會丟棄待播資料,不代表無損排空。
|
||
單張影像用 `gvfg_playback_present_image()`,在已綁定視窗、未 start 的 handle 上同步呼叫。
|
||
詳細模式、master、緩衝與 timestamp 規則見
|
||
[`統一影音播放使用手冊`](GVFG_PLAYBACK_API.md)。
|
||
|
||
## 9. 錯誤處理建議
|
||
|
||
- `GVFG_ETIMEOUT`:連線期間通常可重試,並檢查 signal status/event。收到
|
||
`GVFG_EVENT_VIDEO_INPUT_UNPLUG` 後,不應高速輪詢 read API;應等待
|
||
`GVFG_EVENT_VIDEO_INPUT_PLUGIN`(或 application stop)再恢復 video/audio read。
|
||
- 每次成功取得的 frame 仍必須恰好 release 一次。若 release 與實體拔線競爭,
|
||
SDK 會處理 driver 已撤銷 zero-copy ownership 的情況,application 不應特判
|
||
`ERROR_BAD_COMMAND (22)`。
|
||
- `GVFG_EVENT_VIDEO_FORMAT_CHANGED`:依 Lib 更新後的 video info 重建格式相關資源。
|
||
- `GVFG_ESTATE`:檢查 lifecycle、是否重複 read、或是否已 stop。
|
||
- `GVFG_EIO`/`GVFG_ENODEV`:停止 session,記錄 `gvfg_strerror()`,再由應用程式
|
||
決定是否重新列舉與開啟。
|
||
- 不要讀寫硬體 register 或依賴 driver/backend counter;這些不是客戶 API。
|
||
|
||
## 完整 API Reference
|
||
|
||
所有公開常數、型別、結構欄位、函式參數與回傳值,請參閱
|
||
[`GVFG_CUSTOMER_API_REFERENCE.md`](GVFG_Customer_doc/GVFG_CUSTOMER_API_REFERENCE.md)。
|