Compared with 5ee2ee1:
- update the bundled GigabyteLib interface and binary, reset backend events across stream transitions, and wait for requested video format changes before reporting refreshed signal state
- translate private GigabyteLib results into stable public GVFG errors, including GVFG_EBUSY, while keeping vendor API names, enum values, and raw codes in internal diagnostics
- keep customer-facing error strings and detailed faults backend-neutral, and show the original library failure only in the Debug internal Sample
- update the public and internal error-handling documentation to match the new ownership boundary
Validation: git diff --check passed. The SDK MSVC Release build and Qt Sample MSVC Release and Debug builds completed successfully. Hardware behavior was not tested.
14 KiB
GVFG 客戶 API 使用手冊
本文件說明客戶可使用的 GVFG 公開介面。唯一的公開核心標頭是
sdk/gvfg/include/gvfg_capture.h;選用預覽功能時才需要
helpers/gvfg_preview/include/gvfg_preview.h。
客戶套件不應包含 gvfg_debug.h、sdk/gvfg/src/、driver IOCTL、FPGA
register、IRQ 或 DMA ring 實作細節。
1. 套件內容
核心擷取:
include/gvfg_capture.h
lib/gvfg.lib
bin/gvfg.dll
Application 只 link gvfg.lib。內部 Gigabyte library 已靜態連結進
gvfg.dll,不需要部署 giga_ioctl.dll 或額外公開 private header。
選用預覽:
include/gvfg_preview.h
lib/gvfg_preview.lib
bin/gvfg_preview.dll
目前支援 Windows x64。API 使用 C ABI,可由 C 或 C++ 呼叫。
2. 最小擷取流程
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
完整範例:
#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。 - Copy mode 的
frame.data由 SDK 擁有;zero-copy mode 則指向 driver-owned buffer。兩種模式都只在對應的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 必須先複製。
- SDK 只保有單一交付 buffer,不建立 audio ring;播放排程與 buffering 屬於 Application/Qt/WASAPI。
- Monitoring playback 不應反向阻塞 capture 或 recording。
QAudioSink暫時回傳 zero bytes 時可短暫重試;若持續沒有進度,Application 應丟棄過期播放資料並只 重建 output sink,保留 capture 與 recording。音訊 output device 恢復後從最新 PCM 繼續播放。
timeout_ms 的共同規則:0 表示不等待;GVFG_TIMEOUT_INFINITE 表示無限等待;
其他值的單位為毫秒。
4. 狀態碼
| 狀態 | 說明 |
|---|---|
GVFG_OK |
成功 |
GVFG_EINVAL |
參數、channel 或 frame token 無效 |
GVFG_ENODEV |
找不到裝置或裝置無法開啟 |
GVFG_ESTATE |
呼叫時機或 handle 狀態不正確 |
GVFG_EIO |
SDK 自身 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):取得一個 frame;ownership 依 copy/ zero-copy mode 而定,但 release contract 相同。gvfg_release_channel_frame(handle, channel, &frame):釋放原 frame token。
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 |
呼叫前應將 gvfg_event_t 清零並設定 struct_size = sizeof(gvfg_event_t)。
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. 選用預覽 helper
gvfg_preview.dll 是同步顯示 helper,不擁有 capture handle,也不依賴
gvfg.dll。典型流程:
gvfg_read_channel_frame
-> 將 gvfg_frame_t 欄位對應到 gvfg_preview_frame_t
-> gvfg_preview_render_frame
-> gvfg_release_channel_frame
建立後用 gvfg_preview_attach_window() 傳入 Windows HWND。render 返回前
frame memory 必須保持有效。gvfg_preview_get_stats() 的 present_fps 只計算
DXGI 成功接受的 Present;swapchain busy 而略過者記在 skipped_presents。
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。