Redesign audio capture around frame ownership

- Remove the public stream-mask and caller-buffer audio APIs.
- Add explicit audio enable, format query, and frame read/release APIs.
- Move PCM buffer ownership into the SDK.
- Allow each channel to hold at most one audio frame at a time.
- Validate the complete audio frame token during release.
- Reset the audio frame ID when capture starts.
- Invalidate any held audio frame when capture stops.
- Migrate the Qt preview sample to the new audio frame API.
- Update the README, Customer API guide, API Reference, and giga_ioctl boundary documentation.
This commit is contained in:
Mark.lin
2026-09-01 17:10:25 +08:00
parent 3f905d8a09
commit bfc598ed4d
7 changed files with 285 additions and 108 deletions
+8 -7
View File
@@ -117,22 +117,23 @@ Channel 預設只啟動 video。CH0 可在 start 前改成 video + audio:
```c ```c
gvfg_open_channel(handle, device_index, GVFG_CHANNEL_0); gvfg_open_channel(handle, device_index, GVFG_CHANNEL_0);
gvfg_set_channel_streams(handle, GVFG_CHANNEL_0, gvfg_set_channel_audio_enabled(handle, GVFG_CHANNEL_0, 1);
GVFG_STREAM_VIDEO | GVFG_STREAM_AUDIO);
gvfg_audio_format_t audio = {0}; gvfg_audio_format_t audio = {0};
gvfg_get_channel_audio_format(handle, GVFG_CHANNEL_0, &audio); gvfg_get_channel_audio_format(handle, GVFG_CHANNEL_0, &audio);
gvfg_start_channel(handle, GVFG_CHANNEL_0); gvfg_start_channel(handle, GVFG_CHANNEL_0);
uint8_t pcm[8192]; gvfg_audio_frame_t pcm = {0};
uint32_t pcm_bytes = 0; if (gvfg_read_channel_audio_frame(handle, GVFG_CHANNEL_0, &pcm, 1000) == GVFG_OK) {
gvfg_read_channel_audio(handle, GVFG_CHANNEL_0, /* Consume pcm.data / pcm.data_size here. */
pcm, sizeof(pcm), &pcm_bytes, 1000); gvfg_release_channel_audio_frame(handle, GVFG_CHANNEL_0, &pcm);
}
``` ```
Video-only 使用 `IOCTL_GIGA_VIDEO_START/STOP`;video + audio 使用 Video-only 使用 `IOCTL_GIGA_VIDEO_START/STOP`;video + audio 使用
`IOCTL_GIGA_START/STOP_VIDEO_AUDIO`。PCM 由 driver DMA buffer 複製到 `IOCTL_GIGA_START/STOP_VIDEO_AUDIO`。PCM 由 driver DMA buffer 複製到
caller-owned destination,SDK 不建立 audio ring buffer。Audio zero-copy SDK-owned frame buffer,再以和 video 相同的 read/release ownership 交付。
SDK 不建立 audio ring buffer。Audio zero-copy
尚未納入,CH1 audio 也尚未正式支援。 尚未納入,CH1 audio 也尚未正式支援。
### 同一裝置雙 channel ### 同一裝置雙 channel
+43
View File
@@ -38,11 +38,16 @@ gvfg_enumerate_devices
-> gvfg_create -> gvfg_create
-> gvfg_set_channel_zero_copy_enabled (optional, before channel open) -> gvfg_set_channel_zero_copy_enabled (optional, before channel open)
-> gvfg_open_channel -> gvfg_open_channel
-> gvfg_set_channel_audio_enabled (optional, before start)
-> gvfg_start_channel -> gvfg_start_channel
-> 重複: -> 重複:
gvfg_read_channel_frame gvfg_read_channel_frame
使用/複製/轉換 frame 使用/複製/轉換 frame
gvfg_release_channel_frame gvfg_release_channel_frame
若已啟用 audio,另一個 worker thread 重複:
gvfg_read_channel_audio_frame
使用/複製 PCM
gvfg_release_channel_audio_frame
-> gvfg_stop -> gvfg_stop
-> gvfg_destroy -> gvfg_destroy
``` ```
@@ -105,6 +110,18 @@ int main(void)
- 同一個 handle 的 lifecycle 操作應由應用程式自行序列化;不要同時 open、 - 同一個 handle 的 lifecycle 操作應由應用程式自行序列化;不要同時 open、
start、stop 或 destroy。 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 必須傳回未修改的完整 `gvfg_audio_frame_t` token。
- 若播放或錄音需要在 release 後繼續使用 PCM,Application 必須先複製。
- SDK 只保有單一交付 buffer,不建立 audio ring;播放排程與 buffering 屬於
Application/Qt/WASAPI。
`timeout_ms` 的共同規則:`0` 表示不等待;`GVFG_TIMEOUT_INFINITE` 表示無限等待; `timeout_ms` 的共同規則:`0` 表示不等待;`GVFG_TIMEOUT_INFINITE` 表示無限等待;
其他值的單位為毫秒。 其他值的單位為毫秒。
@@ -148,6 +165,21 @@ int main(void)
- `pixel_format`、`bit_depth`:原生 payload 格式。 - `pixel_format`、`bit_depth`:原生 payload 格式。
- `frame_id`:同一次 start/stop session 中單調遞增的識別值。 - `frame_id`:同一次 start/stop session 中單調遞增的識別值。
### 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`:同一次 start/stop session 中單調遞增的 audio frame ID。
目前 audio 僅支援 CH0,且必須與 video 一起啟動;不支援 audio-only。公開
audio API 不是 zero-copy,但上層仍使用和 video 相同的 read/release contract。
## 6. 公開 API ## 6. 公開 API
### 裝置與生命週期 ### 裝置與生命週期
@@ -180,6 +212,17 @@ Zero-copy mode 的 driver lifecycle 由 SDK 管理:open 時 enable,每次成
zero-copy mode 而定,但 release contract 相同。 zero-copy mode 而定,但 release contract 相同。
- `gvfg_release_channel_frame(handle, channel, &frame)`:釋放原 frame token。 - `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 時呼叫;streaming 中 `gvfg_set_channel_video_format()` 只能在指定 stream 尚未開始或已 stop 時呼叫;streaming 中
切換會回傳 `GVFG_ESTATE`。應用程式應在 UI 上同步鎖定格式選項。 切換會回傳 `GVFG_ESTATE`。應用程式應在 UI 上同步鎖定格式選項。
+54 -15
View File
@@ -100,6 +100,37 @@ register 與 DMA 實作不屬於本文件。
Caller 不得修改任何欄位再 release。SDK 會將完整 token 與目前 held frame 比對。 Caller 不得修改任何欄位再 release。SDK 會將完整 token 與目前 held frame 比對。
### 1.8a `gvfg_audio_format_t`
由 `gvfg_get_channel_audio_format()` 填入,只包含 Application 建立播放或錄音
格式所需的 PCM metadata。
| 欄位 | 型別 | 說明 |
| --- | --- | --- |
| `sample_rate` | `uint32_t` | 每秒 sample 數 |
| `channels` | `uint32_t` | interleaved PCM channel 數 |
| `bits_per_sample` | `uint32_t` | 每個 PCM sample 的 bit 數 |
Driver 一次傳回多少 bytes、buffer capacity 與 block alignment 均由 SDK/backend
管理,不是公開格式的一部分。
### 1.8b `gvfg_audio_frame_t`
由 `gvfg_read_channel_audio_frame()` 填入,也是稍後傳給
`gvfg_release_channel_audio_frame()` 的完整 token。
| 欄位 | 型別 | 說明 |
| --- | --- | --- |
| `data` | `const void *` | SDK-owned interleaved PCM;release 或 stop 後失效 |
| `data_size` | `uint64_t` | 此 frame 的有效 PCM byte 數 |
| `sample_rate` | `uint32_t` | 每秒 sample 數 |
| `channels` | `uint32_t` | interleaved PCM channel 數 |
| `bits_per_sample` | `uint32_t` | 每個 PCM sample 的 bit 數 |
| `frame_id` | `uint64_t` | 此次 start/stop run 中單調遞增的 audio frame ID |
與 video 相同,每個 channel 同時只能持有一個 audio frame,且 caller 不得修改
descriptor 後再 release。
### 1.9 `gvfg_gpu_output_format_t` ### 1.9 `gvfg_gpu_output_format_t`
| 成員 | 值 | 說明 | | 成員 | 值 | 說明 |
@@ -242,28 +273,36 @@ gvfg_status_t gvfg_set_channel_video_format(gvfg_handle handle,
- 同一裝置的 CH0、CH1 不可同時設定 Y210;第二個 Y210 request 回傳 - 同一裝置的 CH0、CH1 不可同時設定 Y210;第二個 Y210 request 回傳
`GVFG_ENOTSUP`。 `GVFG_ENOTSUP`。
### 1.19a Audio capture selection and copy-out ### 1.19a Audio capture selection and frame ownership
```c ```c
gvfg_status_t gvfg_set_channel_streams(gvfg_handle handle, gvfg_status_t gvfg_set_channel_audio_enabled(gvfg_handle handle,
int channel_index, int channel_index,
uint32_t streams); int enabled);
gvfg_status_t gvfg_get_channel_audio_format(gvfg_handle handle, gvfg_status_t gvfg_get_channel_audio_format(gvfg_handle handle,
int channel_index, int channel_index,
gvfg_audio_format_t *out_format); gvfg_audio_format_t *out_format);
gvfg_status_t gvfg_read_channel_audio(gvfg_handle handle, gvfg_status_t gvfg_read_channel_audio_frame(gvfg_handle handle,
int channel_index, int channel_index,
void *destination, gvfg_audio_frame_t *out_frame,
uint32_t destination_capacity, uint32_t timeout_ms);
uint32_t *out_bytes, gvfg_status_t gvfg_release_channel_audio_frame(gvfg_handle handle,
uint32_t timeout_ms); int channel_index,
const gvfg_audio_frame_t *frame);
``` ```
- 預設為 `GVFG_STREAM_VIDEO`。CH0 可在 start 前設定為 - 預設只擷取 video。CH0 可在 start 前用 `gvfg_set_channel_audio_enabled()`
`GVFG_STREAM_VIDEO | GVFG_STREAM_AUDIO`;audio-only 與 CH1 audio 尚未支援。 啟用 audio;audio-only 與 CH1 audio 尚未支援。
- `gvfg_start_channel()` 依 streams 選擇 video-only 或 video + audio driver start。 - `gvfg_start_channel()` 依 audio enabled 狀態選擇 video-only 或 video + audio driver start。
- `gvfg_read_channel_audio()` 以 `MAXULONG` 取得下一個 PCM frame,並複製到 - `gvfg_read_channel_audio_frame()` 取得下一個 PCM frame 並交付 SDK-owned
caller-owned destination。`out_bytes` 是 driver 回報的有效 byte 數。 descriptor。使用完成後必須呼叫 `gvfg_release_channel_audio_frame()`。
- 未 release 前再次 read 會回傳 `GVFG_ESTATE`。
- `out_frame` 為 NULL 或 release token 被修改時回傳 `GVFG_EINVAL`。
- `timeout_ms` 遵循其他 read API:`0` 不等待,`GVFG_TIMEOUT_INFINITE` 無限等待,
超時回傳 `GVFG_ETIMEOUT`。
- Video 與 audio 應由不同 worker thread read;同一 channel 不可同時執行兩個
audio read。
- Stop/close 後 descriptor 立即失效;要跨越 release/stop 保存 PCM 必須先複製。
- SDK 不建立 audio ring,也不提供尚未完成的 audio zero-copy。 - SDK 不建立 audio ring,也不提供尚未完成的 audio zero-copy。
### 1.20 `gvfg_start_channel` ### 1.20 `gvfg_start_channel`
+23 -20
View File
@@ -492,11 +492,10 @@ void MainWindow::startCapture(int channelIndex)
} }
const bool audioEnabled = channelIndex == GVFG_CHANNEL_0 && ui_->ch0AudioCheckBox->isChecked(); const bool audioEnabled = channelIndex == GVFG_CHANNEL_0 && ui_->ch0AudioCheckBox->isChecked();
gvfg_status_t st = gvfg_set_channel_streams( gvfg_status_t st = gvfg_set_channel_audio_enabled(handle_, channelIndex, audioEnabled ? 1 : 0);
handle_, channelIndex, GVFG_STREAM_VIDEO | (audioEnabled ? GVFG_STREAM_AUDIO : 0));
if (st != GVFG_OK) if (st != GVFG_OK)
{ {
showError(QStringLiteral("gvfg_set_channel_streams"), st, channelIndex); showError(QStringLiteral("gvfg_set_channel_audio_enabled"), st, channelIndex);
return; return;
} }
channel.audioFormat = {}; channel.audioFormat = {};
@@ -508,18 +507,16 @@ void MainWindow::startCapture(int channelIndex)
showError(QStringLiteral("gvfg_get_channel_audio_format"), st, channelIndex); showError(QStringLiteral("gvfg_get_channel_audio_format"), st, channelIndex);
return; return;
} }
if (channel.audioFormat.frame_bytes == 0 || channel.audioFormat.block_align == 0 || if (channel.audioFormat.channels == 0 || channel.audioFormat.sample_rate == 0 ||
channel.audioFormat.channels == 0 || channel.audioFormat.sample_rate == 0 ||
(channel.audioFormat.bits_per_sample != 8 && (channel.audioFormat.bits_per_sample != 8 &&
channel.audioFormat.bits_per_sample != 16 && channel.audioFormat.bits_per_sample != 16 &&
channel.audioFormat.bits_per_sample != 32)) channel.audioFormat.bits_per_sample != 32))
{ {
appendLog(QStringLiteral("CH%1 Audio format unsupported by Qt playback | %2 Hz %3 ch %4-bit frame_bytes=%5") appendLog(QStringLiteral("CH%1 Audio format unsupported by Qt playback | %2 Hz %3 ch %4-bit")
.arg(channelIndex) .arg(channelIndex)
.arg(channel.audioFormat.sample_rate) .arg(channel.audioFormat.sample_rate)
.arg(channel.audioFormat.channels) .arg(channel.audioFormat.channels)
.arg(channel.audioFormat.bits_per_sample) .arg(channel.audioFormat.bits_per_sample));
.arg(channel.audioFormat.frame_bytes));
return; return;
} }
} }
@@ -1233,7 +1230,6 @@ void MainWindow::audioReadLoop(int channelIndex)
} }
QAudioSink audioSink(outputDevice, outputFormat); QAudioSink audioSink(outputDevice, outputFormat);
audioSink.setBufferSize(static_cast<int>(format.frame_bytes * 4u));
QIODevice *audioOutput = audioSink.start(); QIODevice *audioOutput = audioSink.start();
if (!audioOutput) if (!audioOutput)
{ {
@@ -1244,13 +1240,11 @@ void MainWindow::audioReadLoop(int channelIndex)
return; return;
} }
std::vector<uint8_t> audioBuffer(format.frame_bytes);
while (!channel.stopRequested.load(std::memory_order_acquire)) while (!channel.stopRequested.load(std::memory_order_acquire))
{ {
uint32_t bytesRead = 0; gvfg_audio_frame_t frame{};
const gvfg_status_t status = gvfg_read_channel_audio( const gvfg_status_t status = gvfg_read_channel_audio_frame(
handle_, channelIndex, audioBuffer.data(), handle_, channelIndex, &frame, 200);
static_cast<uint32_t>(audioBuffer.size()), &bytesRead, 200);
if (status == GVFG_ETIMEOUT) if (status == GVFG_ETIMEOUT)
continue; continue;
if (status != GVFG_OK) if (status != GVFG_OK)
@@ -1258,24 +1252,25 @@ void MainWindow::audioReadLoop(int channelIndex)
if (!channel.stopRequested.load(std::memory_order_acquire)) if (!channel.stopRequested.load(std::memory_order_acquire))
{ {
QMetaObject::invokeMethod(this, [this, channelIndex, status]() QMetaObject::invokeMethod(this, [this, channelIndex, status]()
{ showError(QStringLiteral("gvfg_read_channel_audio"), status, channelIndex); }, Qt::QueuedConnection); { showError(QStringLiteral("gvfg_read_channel_audio_frame"), status, channelIndex); }, Qt::QueuedConnection);
} }
break; break;
} }
qint64 written = 0; qint64 written = 0;
while (written < static_cast<qint64>(bytesRead) && while (written < static_cast<qint64>(frame.data_size) &&
!channel.stopRequested.load(std::memory_order_acquire)) !channel.stopRequested.load(std::memory_order_acquire))
{ {
const qint64 result = audioOutput->write( const qint64 result = audioOutput->write(
reinterpret_cast<const char *>(audioBuffer.data()) + written, static_cast<const char *>(frame.data) + written,
static_cast<qint64>(bytesRead) - written); static_cast<qint64>(frame.data_size) - written);
if (result < 0) if (result < 0)
{ {
QMetaObject::invokeMethod(this, [this, channelIndex]() QMetaObject::invokeMethod(this, [this, channelIndex]()
{ {
appendLog(QStringLiteral("CH%1 Audio output write failed").arg(channelIndex)); appendLog(QStringLiteral("CH%1 Audio output write failed").arg(channelIndex));
stopCapture(channelIndex); }, Qt::QueuedConnection); stopCapture(channelIndex); }, Qt::QueuedConnection);
gvfg_release_channel_audio_frame(handle_, channelIndex, &frame);
audioSink.stop(); audioSink.stop();
return; return;
} }
@@ -1286,10 +1281,18 @@ void MainWindow::audioReadLoop(int channelIndex)
} }
written += result; written += result;
} }
if (written == static_cast<qint64>(bytesRead)) if (written == static_cast<qint64>(frame.data_size))
{ {
channel.audioFrames.fetch_add(1, std::memory_order_relaxed); channel.audioFrames.fetch_add(1, std::memory_order_relaxed);
channel.audioBytes.fetch_add(bytesRead, std::memory_order_relaxed); channel.audioBytes.fetch_add(frame.data_size, std::memory_order_relaxed);
}
const gvfg_status_t releaseStatus =
gvfg_release_channel_audio_frame(handle_, channelIndex, &frame);
if (releaseStatus != GVFG_OK && !channel.stopRequested.load(std::memory_order_acquire))
{
QMetaObject::invokeMethod(this, [this, channelIndex, releaseStatus]()
{ showError(QStringLiteral("gvfg_release_channel_audio_frame"), releaseStatus, channelIndex); }, Qt::QueuedConnection);
break;
} }
} }
audioSink.stop(); audioSink.stop();
+9 -1
View File
@@ -17,7 +17,8 @@ The header is an internal dependency of `gvfg`; it is not a customer SDK API.
## Current audio ABI ## Current audio ABI
The implemented audio path is copy-out, not zero-copy: At this private driver boundary the implemented audio path is copy-out, not
zero-copy:
- Register `GIGA_IOCTL_EVENT_AUDIO_DMA` and - Register `GIGA_IOCTL_EVENT_AUDIO_DMA` and
`GIGA_IOCTL_EVENT_EXTRA_AUDIO_FRAME` as wake-up notifications. `GIGA_IOCTL_EVENT_EXTRA_AUDIO_FRAME` as wake-up notifications.
@@ -32,3 +33,10 @@ The extra-frame events mean that more frames are ready and the consumer should
drain promptly. They do not transfer ownership of a frame. Audio zero-copy is a drain promptly. They do not transfer ownership of a frame. Audio zero-copy is a
future driver capability and is intentionally not exposed here until its future driver capability and is intentionally not exposed here until its
acquire/release ABI is complete. acquire/release ABI is complete.
This does not define the customer-facing ownership contract. `gvfg` copies the
driver result into its SDK-owned delivery buffer and exposes it as
`gvfg_audio_frame_t`; applications acquire it with
`gvfg_read_channel_audio_frame()` and return the unchanged token with
`gvfg_release_channel_audio_frame()`. Driver frame size, destination capacity,
and valid-byte output parameters remain private below that public API.
+28 -21
View File
@@ -39,6 +39,8 @@
* - The frame data pointer remains valid until gvfg_release_channel_frame() is called. * - The frame data pointer remains valid until gvfg_release_channel_frame() is called.
* Copy the data if it must outlive that call. * Copy the data if it must outlive that call.
* - At most one frame may be held by each channel at a time. * - At most one frame may be held by each channel at a time.
* - Audio uses the same read/release ownership model. Applications should use
* a separate worker thread when reading video and audio concurrently.
*/ */
#ifdef _WIN32 #ifdef _WIN32
@@ -109,20 +111,11 @@ extern "C"
GVFG_CHANNEL_1 = 1 GVFG_CHANNEL_1 = 1
} gvfg_channel_t; } gvfg_channel_t;
typedef enum
{
GVFG_STREAM_VIDEO = 1u << 0,
GVFG_STREAM_AUDIO = 1u << 1
} gvfg_stream_flag_t;
typedef struct typedef struct
{ {
uint32_t sample_rate; uint32_t sample_rate;
uint32_t channels; uint32_t channels;
uint32_t bits_per_sample; uint32_t bits_per_sample;
uint32_t frames_per_second;
uint32_t frame_bytes;
uint32_t block_align;
} gvfg_audio_format_t; } gvfg_audio_format_t;
typedef struct typedef struct
@@ -164,6 +157,17 @@ extern "C"
uint64_t frame_id; /* Monotonic identifier within the current channel start/stop run. */ uint64_t frame_id; /* Monotonic identifier within the current channel start/stop run. */
} gvfg_frame_t; } gvfg_frame_t;
typedef struct
{
const void *data; /* PCM data. Valid until gvfg_release_channel_audio_frame(). */
uint64_t data_size; /* Valid PCM bytes available from data. */
uint32_t sample_rate; /* Samples per second. */
uint32_t channels; /* Interleaved PCM channel count. */
uint32_t bits_per_sample; /* Bits in each native PCM sample. */
uint32_t reserved;
uint64_t frame_id; /* Monotonic identifier within the current start/stop run. */
} gvfg_audio_frame_t;
/* Formats produced by gvfg_gpu_convert_to_buffer(). */ /* Formats produced by gvfg_gpu_convert_to_buffer(). */
typedef enum typedef enum
{ {
@@ -339,14 +343,13 @@ extern "C"
_In_ gvfg_pixel_format_t format); _In_ gvfg_pixel_format_t format);
/* /*
* Select video-only or combined video+audio capture before starting the * Enable or disable audio for the next channel start. Video remains
* channel. The default is GVFG_STREAM_VIDEO. Audio-only capture is not * enabled; applications do not need to construct stream flag masks.
* supported by the current driver ABI.
*/ */
GVFG_API gvfg_status_t gvfg_set_channel_streams( GVFG_API gvfg_status_t gvfg_set_channel_audio_enabled(
_In_ gvfg_handle handle, _In_ gvfg_handle handle,
_In_ int channel_index, _In_ int channel_index,
_In_ uint32_t streams); _In_ int enabled);
GVFG_API gvfg_status_t gvfg_get_channel_audio_format( GVFG_API gvfg_status_t gvfg_get_channel_audio_format(
_In_ gvfg_handle handle, _In_ gvfg_handle handle,
@@ -422,18 +425,22 @@ extern "C"
_In_ const gvfg_frame_t *frame); _In_ const gvfg_frame_t *frame);
/* /*
* Copy one PCM frame from the driver into caller-owned memory. The channel * Acquire one PCM frame. The channel must have audio enabled before start.
* must have been started with GVFG_STREAM_VIDEO | GVFG_STREAM_AUDIO. * The returned SDK-owned data remains valid until the matching release.
* out_bytes receives the valid byte count reported by the driver. * Each channel may hold only one audio frame at a time.
*/ */
GVFG_API gvfg_status_t gvfg_read_channel_audio( GVFG_API gvfg_status_t gvfg_read_channel_audio_frame(
_In_ gvfg_handle handle, _In_ gvfg_handle handle,
_In_ int channel_index, _In_ int channel_index,
_Out_writes_bytes_(destination_capacity) void *destination, _Out_ gvfg_audio_frame_t *out_frame,
_In_ uint32_t destination_capacity,
_Out_ uint32_t *out_bytes,
_In_ uint32_t timeout_ms); _In_ uint32_t timeout_ms);
/* Release the unchanged token returned by gvfg_read_channel_audio_frame(). */
GVFG_API gvfg_status_t gvfg_release_channel_audio_frame(
_In_ gvfg_handle handle,
_In_ int channel_index,
_In_ const gvfg_audio_frame_t *frame);
/* /*
* Convert a captured YUY2 or Y210 frame with the GPU and copy the result * Convert a captured YUY2 or Y210 frame with the GPU and copy the result
* into caller-owned memory. * into caller-owned memory.
+120 -44
View File
@@ -33,7 +33,13 @@ namespace
static_assert(GVFG_EVENT_MASK_FORMAT_CHANGE_BEGIN == PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN); static_assert(GVFG_EVENT_MASK_FORMAT_CHANGE_BEGIN == PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN);
#if INTPTR_MAX == INT64_MAX #if INTPTR_MAX == INT64_MAX
static_assert(sizeof(gvfg_audio_format_t) == 24, "gvfg_audio_format_t ABI must remain frozen"); static_assert(sizeof(gvfg_audio_format_t) == 12, "gvfg_audio_format_t ABI must remain frozen");
static_assert(std::is_standard_layout_v<gvfg_audio_frame_t>);
static_assert(sizeof(gvfg_audio_frame_t) == 40, "gvfg_audio_frame_t x64 ABI must remain frozen");
static_assert(offsetof(gvfg_audio_frame_t, data) == 0);
static_assert(offsetof(gvfg_audio_frame_t, data_size) == 8);
static_assert(offsetof(gvfg_audio_frame_t, sample_rate) == 16);
static_assert(offsetof(gvfg_audio_frame_t, frame_id) == 32);
static_assert(sizeof(gvfg_runtime_info_t) == 56, "gvfg_runtime_info_t x64 ABI must remain frozen"); static_assert(sizeof(gvfg_runtime_info_t) == 56, "gvfg_runtime_info_t x64 ABI must remain frozen");
static_assert(std::is_standard_layout_v<gvfg_frame_t>); static_assert(std::is_standard_layout_v<gvfg_frame_t>);
static_assert(sizeof(gvfg_frame_t) == 48, "gvfg_frame_t x64 ABI must remain frozen"); static_assert(sizeof(gvfg_frame_t) == 48, "gvfg_frame_t x64 ABI must remain frozen");
@@ -240,6 +246,12 @@ struct gvfg_channel_session_t
return cfg; return cfg;
resetRuntimeCounters(); resetRuntimeCounters();
{
std::lock_guard<std::mutex> lock(audioMutex);
heldAudioFrame = {};
audioFrameHeld = false;
audioFrameId = 0;
}
{ {
std::lock_guard<std::mutex> lock(eventMutex); std::lock_guard<std::mutex> lock(eventMutex);
eventQueue.clear(); eventQueue.clear();
@@ -257,6 +269,7 @@ struct gvfg_channel_session_t
{ {
running = false; running = false;
releaseHeldFrameForStop(); releaseHeldFrameForStop();
releaseHeldAudioFrameForStop();
pcies2mm_status_t st = PCIES2MM_OK; pcies2mm_status_t st = PCIES2MM_OK;
if (backend) if (backend)
@@ -673,21 +686,16 @@ struct gvfg_channel_session_t
return map_status(status); return map_status(status);
} }
gvfg_status_t setStreams(uint32_t value) gvfg_status_t setAudioEnabled(bool enabled)
{ {
if (!backend) if (!backend)
return reject(GVFG_ESTATE, "gvfg_set_channel_streams rejected: channel is not open"); return reject(GVFG_ESTATE, "gvfg_set_channel_audio_enabled rejected: channel is not open");
if (running) if (running)
return reject(GVFG_ESTATE, "gvfg_set_channel_streams rejected: channel is running"); return reject(GVFG_ESTATE, "gvfg_set_channel_audio_enabled rejected: channel is running");
const uint32_t supported = GVFG_STREAM_VIDEO | GVFG_STREAM_AUDIO; const pcies2mm_status_t status = backend->set_audio_enabled(enabled);
if ((value & ~supported) != 0 || (value & GVFG_STREAM_VIDEO) == 0)
return reject(GVFG_EINVAL, "gvfg_set_channel_streams rejected: use video or video plus audio");
const bool enableAudio = (value & GVFG_STREAM_AUDIO) != 0;
const pcies2mm_status_t status = backend->set_audio_enabled(enableAudio);
if (status != PCIES2MM_OK) if (status != PCIES2MM_OK)
return map_status(status); return map_status(status);
streams = value; audioEnabled = enabled;
return GVFG_OK; return GVFG_OK;
} }
@@ -703,21 +711,76 @@ struct gvfg_channel_session_t
out.sample_rate = format.sample_rate; out.sample_rate = format.sample_rate;
out.channels = format.channels; out.channels = format.channels;
out.bits_per_sample = format.bits_per_sample; out.bits_per_sample = format.bits_per_sample;
out.frames_per_second = format.frames_per_second;
out.frame_bytes = format.frame_bytes;
out.block_align = format.block_align;
return GVFG_OK; return GVFG_OK;
} }
gvfg_status_t readAudio(void *destination, uint32_t destinationCapacity, gvfg_status_t readAudioFrame(gvfg_audio_frame_t &out, uint32_t timeoutMs)
uint32_t &outBytes, uint32_t timeoutMs)
{ {
outBytes = 0; std::memset(&out, 0, sizeof(out));
if (!backend) if (!backend)
return reject(GVFG_ESTATE, "gvfg_read_channel_audio rejected: channel is not open"); return reject(GVFG_ESTATE, "gvfg_read_channel_audio_frame rejected: channel is not open");
if (!running || (streams & GVFG_STREAM_AUDIO) == 0) if (!running || !audioEnabled)
return reject(GVFG_ESTATE, "gvfg_read_channel_audio rejected: audio capture is not running"); return reject(GVFG_ESTATE, "gvfg_read_channel_audio_frame rejected: audio capture is not running");
return map_status(backend->wait_audio(timeoutMs, destination, destinationCapacity, outBytes));
{
std::lock_guard<std::mutex> lock(audioMutex);
if (audioFrameHeld)
return reject(GVFG_ESTATE, "gvfg_read_channel_audio_frame rejected: previous audio frame has not been released");
if (audioReadInProgress)
return reject(GVFG_ESTATE, "gvfg_read_channel_audio_frame rejected: another audio read is already in progress");
audioReadInProgress = true;
}
pcies2mm_audio_format_t format{};
pcies2mm_status_t status = backend->get_audio_format(format);
uint32_t bytes = 0;
if (status == PCIES2MM_OK)
{
audioBuffer.resize(format.frame_bytes);
status = backend->wait_audio(timeoutMs, audioBuffer.data(),
static_cast<uint32_t>(audioBuffer.size()), bytes);
}
{
std::lock_guard<std::mutex> lock(audioMutex);
audioReadInProgress = false;
if (status != PCIES2MM_OK)
return map_status(status);
if (!running.load(std::memory_order_acquire) || bytes == 0 || bytes > audioBuffer.size())
return reject(GVFG_ESTATE, "gvfg_read_channel_audio_frame completed after capture stopped or returned invalid data");
audioFrameHeld = true;
++audioFrameId;
out.data = audioBuffer.data();
out.data_size = bytes;
out.sample_rate = format.sample_rate;
out.channels = format.channels;
out.bits_per_sample = format.bits_per_sample;
out.frame_id = audioFrameId;
heldAudioFrame = out;
}
return GVFG_OK;
}
gvfg_status_t releaseAudioFrame(const gvfg_audio_frame_t &token)
{
std::lock_guard<std::mutex> lock(audioMutex);
if (!audioFrameHeld)
return reject(GVFG_ESTATE, "gvfg_release_channel_audio_frame rejected: no audio frame is currently held");
if (token.data != heldAudioFrame.data || token.data_size != heldAudioFrame.data_size ||
token.sample_rate != heldAudioFrame.sample_rate || token.channels != heldAudioFrame.channels ||
token.bits_per_sample != heldAudioFrame.bits_per_sample ||
token.reserved != heldAudioFrame.reserved || token.frame_id != heldAudioFrame.frame_id)
return reject(GVFG_EINVAL, "gvfg_release_channel_audio_frame rejected: frame token does not match the held audio frame");
heldAudioFrame = {};
audioFrameHeld = false;
return GVFG_OK;
}
void releaseHeldAudioFrameForStop()
{
std::lock_guard<std::mutex> lock(audioMutex);
heldAudioFrame = {};
audioFrameHeld = false;
} }
gvfg_status_t debugWriteRegister(uint32_t offset, uint32_t value) gvfg_status_t debugWriteRegister(uint32_t offset, uint32_t value)
@@ -782,7 +845,7 @@ struct gvfg_channel_session_t
int currentIndex = -1; int currentIndex = -1;
uint32_t selectedChannel = GVFG_CHANNEL_0; uint32_t selectedChannel = GVFG_CHANNEL_0;
bool zeroCopyRequested = false; bool zeroCopyRequested = false;
uint32_t streams = GVFG_STREAM_VIDEO; bool audioEnabled = false;
uint32_t eventMask = GVFG_EVENT_MASK_ALL; uint32_t eventMask = GVFG_EVENT_MASK_ALL;
uint32_t width = 0; uint32_t width = 0;
@@ -805,6 +868,12 @@ struct gvfg_channel_session_t
bool readInProgress = false; bool readInProgress = false;
bool frameHeld = false; bool frameHeld = false;
pcies2mm_frame_t heldBackendFrame{}; pcies2mm_frame_t heldBackendFrame{};
std::mutex audioMutex;
std::vector<uint8_t> audioBuffer;
bool audioReadInProgress = false;
bool audioFrameHeld = false;
uint64_t audioFrameId = 0;
gvfg_audio_frame_t heldAudioFrame{};
std::mutex eventMutex; std::mutex eventMutex;
std::condition_variable eventCv; std::condition_variable eventCv;
std::deque<gvfg_event_t> eventQueue; std::deque<gvfg_event_t> eventQueue;
@@ -1112,17 +1181,16 @@ extern "C"
return handle->setChannelVideoFormat(channel_index, format); return handle->setChannelVideoFormat(channel_index, format);
} }
gvfg_status_t gvfg_set_channel_streams(gvfg_handle handle, gvfg_status_t gvfg_set_channel_audio_enabled(gvfg_handle handle,
int channel_index, int channel_index,
uint32_t streams) int enabled)
{ {
if (!handle) if (!handle)
return GVFG_EINVAL; return GVFG_EINVAL;
gvfg_channel_session_t *channel = handle->findChannel(channel_index); gvfg_channel_session_t *channel = handle->findChannel(channel_index);
return channel ? channel->setStreams(streams) return channel ? channel->setAudioEnabled(enabled != 0)
: handle->rejectChannel(channel_index, : handle->rejectChannel(channel_index, GVFG_ESTATE,
GVFG_ESTATE, "gvfg_set_channel_audio_enabled rejected: channel is not open");
"gvfg_set_channel_streams rejected: channel is not open");
} }
gvfg_status_t gvfg_get_channel_audio_format(gvfg_handle handle, gvfg_status_t gvfg_get_channel_audio_format(gvfg_handle handle,
@@ -1177,29 +1245,37 @@ extern "C"
"gvfg_release_channel_frame rejected: channel is not open"); "gvfg_release_channel_frame rejected: channel is not open");
} }
gvfg_status_t gvfg_read_channel_audio(gvfg_handle handle, gvfg_status_t gvfg_read_channel_audio_frame(gvfg_handle handle,
int channel_index, int channel_index,
void *destination, gvfg_audio_frame_t *out_frame,
uint32_t destination_capacity, uint32_t timeout_ms)
uint32_t *out_bytes,
uint32_t timeout_ms)
{ {
if (!handle) if (!handle)
return GVFG_EINVAL; return GVFG_EINVAL;
if (out_bytes) if (!out_frame)
*out_bytes = 0;
if (!destination || destination_capacity == 0 || !out_bytes)
return handle->rejectChannel(channel_index, return handle->rejectChannel(channel_index,
GVFG_EINVAL, GVFG_EINVAL,
"gvfg_read_channel_audio rejected: output buffer is invalid"); "gvfg_read_channel_audio_frame rejected: out_frame is null");
gvfg_channel_session_t *channel = handle->findChannel(channel_index); gvfg_channel_session_t *channel = handle->findChannel(channel_index);
return channel ? channel->readAudio(destination, return channel ? channel->readAudioFrame(*out_frame, timeout_ms)
destination_capacity,
*out_bytes,
timeout_ms)
: handle->rejectChannel(channel_index, : handle->rejectChannel(channel_index,
GVFG_ESTATE, GVFG_ESTATE,
"gvfg_read_channel_audio rejected: channel is not open"); "gvfg_read_channel_audio_frame rejected: channel is not open");
}
gvfg_status_t gvfg_release_channel_audio_frame(gvfg_handle handle,
int channel_index,
const gvfg_audio_frame_t *frame)
{
if (!handle)
return GVFG_EINVAL;
if (!frame)
return handle->rejectChannel(channel_index, GVFG_EINVAL,
"gvfg_release_channel_audio_frame rejected: frame is null");
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
return channel ? channel->releaseAudioFrame(*frame)
: handle->rejectChannel(channel_index, GVFG_ESTATE,
"gvfg_release_channel_audio_frame rejected: channel is not open");
} }
gvfg_status_t gvfg_poll_channel_event(gvfg_handle handle, gvfg_status_t gvfg_poll_channel_event(gvfg_handle handle,