diff --git a/README.md b/README.md index bd14214..1974137 100644 --- a/README.md +++ b/README.md @@ -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 只建立一個 diff --git a/docs/GVFG_CUSTOMER_API.md b/docs/GVFG_CUSTOMER_API.md index 4fe20bd..80136c3 100644 --- a/docs/GVFG_CUSTOMER_API.md +++ b/docs/GVFG_CUSTOMER_API.md @@ -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` 表示無限等待; 其他值的單位為毫秒。 diff --git a/samples/gvfg_qt_preview/mainwindow.cpp b/samples/gvfg_qt_preview/mainwindow.cpp index d1ef782..249aab7 100644 --- a/samples/gvfg_qt_preview/mainwindow.cpp +++ b/samples/gvfg_qt_preview/mainwindow.cpp @@ -1499,98 +1499,141 @@ void MainWindow::audioPlaybackLoop(int channelIndex) return; } - const QAudioDevice outputDevice = QMediaDevices::defaultAudioOutput(); - if (outputDevice.isNull() || !outputDevice.isFormatSupported(outputFormat)) + bool recovering = false; + while (!channel.stopRequested.load(std::memory_order_acquire)) { - QMetaObject::invokeMethod(this, [this, channelIndex, format]() - { - 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 pcm; + const QAudioDevice outputDevice = QMediaDevices::defaultAudioOutput(); + if (outputDevice.isNull() || !outputDevice.isFormatSupported(outputFormat)) { + 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 lock(channel.audioQueueMutex); - channel.audioQueueReady.wait(lock, [&channel]() { - return channel.stopRequested.load(std::memory_order_acquire) || - !channel.audioQueue.empty(); + 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); }); - if (channel.stopRequested.load(std::memory_order_acquire)) - break; - pcm = std::move(channel.audioQueue.front().pcm); - channel.audioQueue.pop_front(); - channel.audioQueuedBytes -= pcm.size(); + continue; } - qint64 written = 0; - auto lastProgress = std::chrono::steady_clock::now(); - while (written < static_cast(pcm.size()) && - !channel.stopRequested.load(std::memory_order_acquire)) + QAudioSink audioSink(outputDevice, outputFormat); + QIODevice *audioOutput = audioSink.start(); + if (!audioOutput) { - const qint64 result = audioOutput->write( - reinterpret_cast(pcm.data()) + written, - static_cast(pcm.size()) - written); - const auto writeEnd = std::chrono::steady_clock::now(); - const bool stalled = result == 0 && writeEnd - lastProgress >= std::chrono::seconds(2); + 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 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 pcm; + { + std::unique_lock 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(pcm.size()) && + !channel.stopRequested.load(std::memory_order_acquire)) + { + const qint64 result = audioOutput->write( + reinterpret_cast(pcm.data()) + written, + static_cast(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 lock(channel.audioQueueMutex); + channel.audioMaxWriteStallMs = (std::max)(channel.audioMaxWriteStallMs, + std::chrono::duration(now - lastProgress).count()); + if (result > 0) + channel.audioAcceptedBytes += static_cast(result); + else if (result < 0 || stalled) + channel.audioFailedBytes += pcm.size() - static_cast(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(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 lock(channel.audioQueueMutex); - channel.audioMaxWriteStallMs = (std::max)(channel.audioMaxWriteStallMs, - std::chrono::duration(writeEnd - lastProgress).count()); - if (result > 0) - channel.audioAcceptedBytes += static_cast(result); - else if (result < 0 || stalled) - channel.audioFailedBytes += pcm.size() - static_cast(written); + channel.audioCancelledBytes += pcm.size() - static_cast(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(pcm.size())) + audioSink.stop(); + if (restartPlayback) { - channel.audioFrames.fetch_add(1, std::memory_order_relaxed); - channel.audioBytes.fetch_add(pcm.size(), std::memory_order_relaxed); - } - else - { - std::lock_guard lock(channel.audioQueueMutex); - channel.audioCancelledBytes += pcm.size() - static_cast(written); + recovering = true; + std::unique_lock 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); + }); } } - audioSink.stop(); } void MainWindow::joinAudioThread(int channel)