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:
@@ -139,6 +139,11 @@ SDK-owned frame buffer,再以和 video 相同的 read/release ownership 交付
|
||||
SDK 不建立 audio ring buffer。Audio zero-copy
|
||||
尚未納入,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
|
||||
|
||||
同一個 `gvfg_handle` 可對同一個 device index 開啟 CH0、CH1。SDK 只建立一個
|
||||
|
||||
@@ -121,6 +121,10 @@ Audio 採用相同的 pull 與 ownership 模型:
|
||||
- 若播放或錄音需要在 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` 表示無限等待;
|
||||
其他值的單位為毫秒。
|
||||
|
||||
@@ -1499,32 +1499,61 @@ void MainWindow::audioPlaybackLoop(int channelIndex)
|
||||
return;
|
||||
}
|
||||
|
||||
bool recovering = false;
|
||||
while (!channel.stopRequested.load(std::memory_order_acquire))
|
||||
{
|
||||
const QAudioDevice outputDevice = QMediaDevices::defaultAudioOutput();
|
||||
if (outputDevice.isNull() || !outputDevice.isFormatSupported(outputFormat))
|
||||
{
|
||||
QMetaObject::invokeMethod(this, [this, channelIndex, format]()
|
||||
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);
|
||||
while (!channel.audioQueue.empty())
|
||||
{
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
QAudioSink audioSink(outputDevice, outputFormat);
|
||||
QIODevice *audioOutput = audioSink.start();
|
||||
if (!audioOutput)
|
||||
{
|
||||
QMetaObject::invokeMethod(this, [this, channelIndex]()
|
||||
if (!recovering)
|
||||
QMetaObject::invokeMethod(this, [this, channelIndex]() {
|
||||
appendLog(QStringLiteral("CH%1 Audio output failed to start; monitoring playback will retry").arg(channelIndex));
|
||||
}, Qt::QueuedConnection);
|
||||
recovering = true;
|
||||
std::unique_lock<std::mutex> lock(channel.audioQueueMutex);
|
||||
while (!channel.audioQueue.empty())
|
||||
{
|
||||
appendLog(QStringLiteral("CH%1 Audio output failed to start").arg(channelIndex));
|
||||
stopCapture(channelIndex); }, Qt::QueuedConnection);
|
||||
return;
|
||||
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;
|
||||
}
|
||||
|
||||
for (;;)
|
||||
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;
|
||||
{
|
||||
@@ -1548,49 +1577,63 @@ void MainWindow::audioPlaybackLoop(int channelIndex)
|
||||
const qint64 result = audioOutput->write(
|
||||
reinterpret_cast<const char *>(pcm.data()) + written,
|
||||
static_cast<qint64>(pcm.size()) - written);
|
||||
const auto writeEnd = std::chrono::steady_clock::now();
|
||||
const bool stalled = result == 0 && writeEnd - lastProgress >= std::chrono::seconds(2);
|
||||
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>(writeEnd - lastProgress).count());
|
||||
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)
|
||||
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));
|
||||
written += result;
|
||||
lastProgress = now;
|
||||
continue;
|
||||
}
|
||||
written += result;
|
||||
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
|
||||
else if (!restartPlayback)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(channel.audioQueueMutex);
|
||||
channel.audioCancelledBytes += pcm.size() - static_cast<uint64_t>(written);
|
||||
}
|
||||
}
|
||||
audioSink.stop();
|
||||
if (restartPlayback)
|
||||
{
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::joinAudioThread(int channel)
|
||||
|
||||
Reference in New Issue
Block a user