Recover monitoring audio without stopping capture

Compared with 8cde31f:

- Keep transient zero-byte QAudioSink writes retryable while detecting two seconds of continuous no-progress as a playback stall.

- Rebuild only the monitoring QAudioSink after write failure, output-device loss, or sustained backpressure instead of stopping the channel capture.

- Drop stale queued monitoring PCM before recovery and reacquire the current default audio output on bounded retry intervals.

- Preserve video capture, audio acquisition, SDK frame ownership, and stop-time worker wakeups while playback recovers independently.

- Update the README and customer API guidance to document playback isolation and recovery responsibilities.

Validation: git diff --check and focused source inspection passed; no build, audio-device removal, or hardware capture validation was run.
This commit is contained in:
Mark.lin
2026-09-08 14:05:58 +08:00
parent 8cde31feaf
commit 7e5d3d2cca
3 changed files with 130 additions and 78 deletions
+5
View File
@@ -139,6 +139,11 @@ SDK-owned frame buffer,再以和 video 相同的 read/release ownership 交付
SDK 不建立 audio ring buffer。Audio zero-copy SDK 不建立 audio ring buffer。Audio zero-copy
尚未納入,CH1 audio 也尚未正式支援。 尚未納入,CH1 audio 也尚未正式支援。
Qt sample 的 monitoring playback 與 capture reader 分開。若 `QAudioSink` write
失敗或連續兩秒沒有寫入進度,只會清除過期的播放 queue 並在 audio thread 重新取得
預設 output device、重建 sink;video/audio capture 不會因此停止。Playback recovery
不影響 SDK frame release contract。
### 同一裝置雙 channel ### 同一裝置雙 channel
同一個 `gvfg_handle` 可對同一個 device index 開啟 CH0、CH1。SDK 只建立一個 同一個 `gvfg_handle` 可對同一個 device index 開啟 CH0、CH1。SDK 只建立一個
+4
View File
@@ -121,6 +121,10 @@ Audio 採用相同的 pull 與 ownership 模型:
- 若播放或錄音需要在 release 後繼續使用 PCM,Application 必須先複製。 - 若播放或錄音需要在 release 後繼續使用 PCM,Application 必須先複製。
- SDK 只保有單一交付 buffer,不建立 audio ring;播放排程與 buffering 屬於 - SDK 只保有單一交付 buffer,不建立 audio ring;播放排程與 buffering 屬於
Application/Qt/WASAPI。 Application/Qt/WASAPI。
- Monitoring playback 不應反向阻塞 capture 或 recording。`QAudioSink` 暫時回傳
zero bytes 時可短暫重試;若持續沒有進度,Application 應丟棄過期播放資料並只
重建 output sink,保留 capture 與 recording。音訊 output device 恢復後從最新
PCM 繼續播放。
`timeout_ms` 的共同規則:`0` 表示不等待;`GVFG_TIMEOUT_INFINITE` 表示無限等待; `timeout_ms` 的共同規則:`0` 表示不等待;`GVFG_TIMEOUT_INFINITE` 表示無限等待;
其他值的單位為毫秒。 其他值的單位為毫秒。
+121 -78
View File
@@ -1499,98 +1499,141 @@ void MainWindow::audioPlaybackLoop(int channelIndex)
return; return;
} }
const QAudioDevice outputDevice = QMediaDevices::defaultAudioOutput(); bool recovering = false;
if (outputDevice.isNull() || !outputDevice.isFormatSupported(outputFormat)) while (!channel.stopRequested.load(std::memory_order_acquire))
{ {
QMetaObject::invokeMethod(this, [this, channelIndex, format]() const QAudioDevice outputDevice = QMediaDevices::defaultAudioOutput();
{ if (outputDevice.isNull() || !outputDevice.isFormatSupported(outputFormat))
appendLog(QStringLiteral("CH%1 Audio output does not support %2 Hz %3 ch %4-bit PCM")
.arg(channelIndex)
.arg(format.sample_rate)
.arg(format.channels)
.arg(format.bits_per_sample));
stopCapture(channelIndex); }, Qt::QueuedConnection);
return;
}
QAudioSink audioSink(outputDevice, outputFormat);
QIODevice *audioOutput = audioSink.start();
if (!audioOutput)
{
QMetaObject::invokeMethod(this, [this, channelIndex]()
{
appendLog(QStringLiteral("CH%1 Audio output failed to start").arg(channelIndex));
stopCapture(channelIndex); }, Qt::QueuedConnection);
return;
}
for (;;)
{
std::vector<uint8_t> pcm;
{ {
if (!recovering)
QMetaObject::invokeMethod(this, [this, channelIndex]() {
appendLog(QStringLiteral("CH%1 Audio output unavailable; monitoring playback will retry").arg(channelIndex));
}, Qt::QueuedConnection);
recovering = true;
std::unique_lock<std::mutex> lock(channel.audioQueueMutex); std::unique_lock<std::mutex> lock(channel.audioQueueMutex);
channel.audioQueueReady.wait(lock, [&channel]() { while (!channel.audioQueue.empty())
return channel.stopRequested.load(std::memory_order_acquire) || {
!channel.audioQueue.empty(); channel.audioDroppedBytes += channel.audioQueue.front().pcm.size();
channel.audioQueuedBytes -= channel.audioQueue.front().pcm.size();
channel.audioQueue.pop_front();
channel.audioQueueDrops.fetch_add(1, std::memory_order_relaxed);
}
channel.audioQueueReady.wait_for(lock, std::chrono::milliseconds(500), [&channel]() {
return channel.stopRequested.load(std::memory_order_acquire);
}); });
if (channel.stopRequested.load(std::memory_order_acquire)) continue;
break;
pcm = std::move(channel.audioQueue.front().pcm);
channel.audioQueue.pop_front();
channel.audioQueuedBytes -= pcm.size();
} }
qint64 written = 0; QAudioSink audioSink(outputDevice, outputFormat);
auto lastProgress = std::chrono::steady_clock::now(); QIODevice *audioOutput = audioSink.start();
while (written < static_cast<qint64>(pcm.size()) && if (!audioOutput)
!channel.stopRequested.load(std::memory_order_acquire))
{ {
const qint64 result = audioOutput->write( if (!recovering)
reinterpret_cast<const char *>(pcm.data()) + written, QMetaObject::invokeMethod(this, [this, channelIndex]() {
static_cast<qint64>(pcm.size()) - written); appendLog(QStringLiteral("CH%1 Audio output failed to start; monitoring playback will retry").arg(channelIndex));
const auto writeEnd = std::chrono::steady_clock::now(); }, Qt::QueuedConnection);
const bool stalled = result == 0 && writeEnd - lastProgress >= std::chrono::seconds(2); recovering = true;
std::unique_lock<std::mutex> lock(channel.audioQueueMutex);
while (!channel.audioQueue.empty())
{
channel.audioDroppedBytes += channel.audioQueue.front().pcm.size();
channel.audioQueuedBytes -= channel.audioQueue.front().pcm.size();
channel.audioQueue.pop_front();
channel.audioQueueDrops.fetch_add(1, std::memory_order_relaxed);
}
channel.audioQueueReady.wait_for(lock, std::chrono::milliseconds(500), [&channel]() {
return channel.stopRequested.load(std::memory_order_acquire);
});
continue;
}
if (recovering)
QMetaObject::invokeMethod(this, [this, channelIndex]() {
appendLog(QStringLiteral("CH%1 Audio monitoring playback recovered").arg(channelIndex));
}, Qt::QueuedConnection);
recovering = false;
bool restartPlayback = false;
while (!channel.stopRequested.load(std::memory_order_acquire) && !restartPlayback)
{
std::vector<uint8_t> pcm;
{
std::unique_lock<std::mutex> lock(channel.audioQueueMutex);
channel.audioQueueReady.wait(lock, [&channel]() {
return channel.stopRequested.load(std::memory_order_acquire) ||
!channel.audioQueue.empty();
});
if (channel.stopRequested.load(std::memory_order_acquire))
break;
pcm = std::move(channel.audioQueue.front().pcm);
channel.audioQueue.pop_front();
channel.audioQueuedBytes -= pcm.size();
}
qint64 written = 0;
auto lastProgress = std::chrono::steady_clock::now();
while (written < static_cast<qint64>(pcm.size()) &&
!channel.stopRequested.load(std::memory_order_acquire))
{
const qint64 result = audioOutput->write(
reinterpret_cast<const char *>(pcm.data()) + written,
static_cast<qint64>(pcm.size()) - written);
const auto now = std::chrono::steady_clock::now();
const bool stalled = result == 0 && now - lastProgress >= std::chrono::seconds(2);
{
std::lock_guard<std::mutex> lock(channel.audioQueueMutex);
channel.audioMaxWriteStallMs = (std::max)(channel.audioMaxWriteStallMs,
std::chrono::duration<double, std::milli>(now - lastProgress).count());
if (result > 0)
channel.audioAcceptedBytes += static_cast<uint64_t>(result);
else if (result < 0 || stalled)
channel.audioFailedBytes += pcm.size() - static_cast<uint64_t>(written);
}
if (result > 0)
{
written += result;
lastProgress = now;
continue;
}
if (result < 0 || stalled)
{
QMetaObject::invokeMethod(this, [this, channelIndex, stalled]() {
appendLog(stalled
? QStringLiteral("CH%1 Audio output made no progress for 2 seconds; rebuilding monitoring playback").arg(channelIndex)
: QStringLiteral("CH%1 Audio output write failed; rebuilding monitoring playback").arg(channelIndex));
}, Qt::QueuedConnection);
restartPlayback = true;
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
if (written == static_cast<qint64>(pcm.size()))
{
channel.audioFrames.fetch_add(1, std::memory_order_relaxed);
channel.audioBytes.fetch_add(pcm.size(), std::memory_order_relaxed);
}
else if (!restartPlayback)
{ {
std::lock_guard<std::mutex> lock(channel.audioQueueMutex); std::lock_guard<std::mutex> lock(channel.audioQueueMutex);
channel.audioMaxWriteStallMs = (std::max)(channel.audioMaxWriteStallMs, channel.audioCancelledBytes += pcm.size() - static_cast<uint64_t>(written);
std::chrono::duration<double, std::milli>(writeEnd - lastProgress).count());
if (result > 0)
channel.audioAcceptedBytes += static_cast<uint64_t>(result);
else if (result < 0 || stalled)
channel.audioFailedBytes += pcm.size() - static_cast<uint64_t>(written);
} }
if (result > 0)
lastProgress = writeEnd;
if (result < 0 || stalled)
{
QMetaObject::invokeMethod(this, [this, channelIndex, stalled]()
{
appendLog(stalled
? QStringLiteral("CH%1 Audio output made no write progress for 2 seconds; stopping. Check the playback device, then Start again.").arg(channelIndex)
: QStringLiteral("CH%1 Audio output write failed").arg(channelIndex));
stopCapture(channelIndex); }, Qt::QueuedConnection);
audioSink.stop();
return;
}
if (result == 0)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
written += result;
} }
if (written == static_cast<qint64>(pcm.size())) audioSink.stop();
if (restartPlayback)
{ {
channel.audioFrames.fetch_add(1, std::memory_order_relaxed); recovering = true;
channel.audioBytes.fetch_add(pcm.size(), std::memory_order_relaxed); std::unique_lock<std::mutex> lock(channel.audioQueueMutex);
} while (!channel.audioQueue.empty())
else {
{ channel.audioDroppedBytes += channel.audioQueue.front().pcm.size();
std::lock_guard<std::mutex> lock(channel.audioQueueMutex); channel.audioQueuedBytes -= channel.audioQueue.front().pcm.size();
channel.audioCancelledBytes += pcm.size() - static_cast<uint64_t>(written); channel.audioQueue.pop_front();
channel.audioQueueDrops.fetch_add(1, std::memory_order_relaxed);
}
channel.audioQueueReady.wait_for(lock, std::chrono::milliseconds(500), [&channel]() {
return channel.stopRequested.load(std::memory_order_acquire);
});
} }
} }
audioSink.stop();
} }
void MainWindow::joinAudioThread(int channel) void MainWindow::joinAudioThread(int channel)