Allow to have separate idle & silence thresholds

For asio (e.g. pipe) there is a difference between idle (no data
being written to) and silence.
Silence also happens on normal music (silent periods or fade out).
Too short a silence timeout messes up playback with meta streams.
The active stream would switch on fade-out / silence and immediately
switch back on next track / non-silence, causing clients to resync
and stutter.
This commit is contained in:
Stefan Keller 2024-10-14 13:25:55 +02:00
parent 208066e5bb
commit 21b35efc98

View file

@ -74,6 +74,8 @@ protected:
std::chrono::microseconds silence_{0ms};
/// silence duration before switching the stream to idle
std::chrono::milliseconds idle_threshold_;
std::chrono::milliseconds silence_threshold_;
bool silence_detection_disabled_;
};
@ -104,7 +106,9 @@ AsioStream<ReadStream>::AsioStream(PcmStream::Listener* pcmListener, boost::asio
LOG(DEBUG, "AsioStream") << "Chunk duration: " << chunk_->durationMs() << " ms, frames: " << chunk_->getFrameCount() << ", size: " << chunk_->payloadSize
<< "\n";
idle_threshold_ = std::chrono::milliseconds(std::max(cpt::stoi(uri_.getQuery("idle_threshold", "100")), 10));
idle_threshold_ = std::chrono::milliseconds(std::max(cpt::stoi(uri_.getQuery("idle_threshold", "500")), 10));
silence_threshold_ = std::chrono::milliseconds(std::max(cpt::stoi(uri_.getQuery("silence_threshold", "1500")), 10));
silence_detection_disabled_ = cpt::stoi(uri_.getQuery("silence_threshold", "1500")) == -1;
buffer_ms_ = 50;
@ -194,14 +198,14 @@ void AsioStream<ReadStream>::do_read()
lastException_.clear();
if (isSilent(*chunk_))
if (isSilent(*chunk_) && !silence_detection_disabled_)
{
silence_ += chunk_->duration<std::chrono::microseconds>();
if (silence_ >= idle_threshold_)
if (silence_ >= silence_threshold_)
{
setState(ReaderState::kIdle);
// Avoid overflow
silence_ = idle_threshold_;
silence_ = silence_threshold_;
}
}
else