mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-10 23:56:43 +02:00
Remove PosixStream layer
This commit is contained in:
parent
86cd4b2b63
commit
275a53a845
21 changed files with 201 additions and 162 deletions
|
@ -1,6 +1,6 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2022 Johannes Pohl
|
||||
Copyright (C) 2014-2024 Johannes Pohl
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
@ -36,6 +36,8 @@
|
|||
namespace streamreader
|
||||
{
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
template <typename ReadStream>
|
||||
class AsioStream : public PcmStream
|
||||
{
|
||||
|
@ -46,20 +48,21 @@ public:
|
|||
void start() override;
|
||||
void stop() override;
|
||||
|
||||
virtual void connect();
|
||||
protected:
|
||||
virtual void connect() = 0;
|
||||
virtual void disconnect();
|
||||
|
||||
protected:
|
||||
virtual void do_connect() = 0;
|
||||
virtual void do_disconnect() = 0;
|
||||
virtual void on_connect();
|
||||
virtual void do_read();
|
||||
void check_state();
|
||||
/// Start a timer that will change the stream state to idle after \p duration
|
||||
void check_state(const std::chrono::steady_clock::duration& duration);
|
||||
|
||||
/// Use Timer \p timer to call \p handler after \p duration
|
||||
template <typename Timer, typename Rep, typename Period>
|
||||
void wait(Timer& timer, const std::chrono::duration<Rep, Period>& duration, std::function<void()> handler);
|
||||
|
||||
std::unique_ptr<msg::PcmChunk> chunk_;
|
||||
/// Cache last exception to avoid repeated error logging
|
||||
std::string lastException_;
|
||||
timeval tv_chunk_;
|
||||
bool first_;
|
||||
std::chrono::time_point<std::chrono::steady_clock> nextTick_;
|
||||
|
@ -67,7 +70,11 @@ protected:
|
|||
boost::asio::steady_timer read_timer_;
|
||||
boost::asio::steady_timer state_timer_;
|
||||
std::unique_ptr<ReadStream> stream_;
|
||||
std::atomic<std::uint64_t> bytes_read_;
|
||||
|
||||
/// duration of the current silence period
|
||||
std::chrono::microseconds silence_{0ms};
|
||||
/// silence duration before switching the stream to idle
|
||||
std::chrono::milliseconds idle_threshold_;
|
||||
};
|
||||
|
||||
|
||||
|
@ -95,11 +102,11 @@ template <typename ReadStream>
|
|||
AsioStream<ReadStream>::AsioStream(PcmStream::Listener* pcmListener, boost::asio::io_context& ioc, const ServerSettings& server_settings, const StreamUri& uri)
|
||||
: PcmStream(pcmListener, ioc, server_settings, uri), read_timer_(strand_), state_timer_(strand_)
|
||||
{
|
||||
chunk_ = std::make_unique<msg::PcmChunk>(sampleFormat_, chunk_ms_);
|
||||
LOG(DEBUG, "AsioStream") << "Chunk duration: " << chunk_->durationMs() << " ms, frames: " << chunk_->getFrameCount() << ", size: " << chunk_->payloadSize
|
||||
<< "\n";
|
||||
|
||||
bytes_read_ = 0;
|
||||
idle_threshold_ = std::chrono::milliseconds(std::max(cpt::stoi(uri_.getQuery("idle_threshold", "100")), 10));
|
||||
|
||||
buffer_ms_ = 50;
|
||||
|
||||
try
|
||||
|
@ -113,18 +120,19 @@ AsioStream<ReadStream>::AsioStream(PcmStream::Listener* pcmListener, boost::asio
|
|||
|
||||
|
||||
template <typename ReadStream>
|
||||
void AsioStream<ReadStream>::check_state()
|
||||
void AsioStream<ReadStream>::check_state(const std::chrono::steady_clock::duration& duration)
|
||||
{
|
||||
uint64_t last_read = bytes_read_;
|
||||
wait(state_timer_, std::chrono::milliseconds(500 + chunk_ms_),
|
||||
[this, last_read]
|
||||
{
|
||||
LOG(TRACE, "AsioStream") << "check state last: " << last_read << ", read: " << bytes_read_ << "\n";
|
||||
if (bytes_read_ != last_read)
|
||||
setState(ReaderState::kPlaying);
|
||||
else
|
||||
state_timer_.expires_after(duration);
|
||||
state_timer_.async_wait(
|
||||
[this, duration](const boost::system::error_code& ec)
|
||||
{
|
||||
if (!ec)
|
||||
{
|
||||
|
||||
LOG(INFO, "AsioStream") << "No data since " << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count()
|
||||
<< " ms, switchung to idle\n";
|
||||
setState(ReaderState::kIdle);
|
||||
check_state();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -133,32 +141,25 @@ template <typename ReadStream>
|
|||
void AsioStream<ReadStream>::start()
|
||||
{
|
||||
PcmStream::start();
|
||||
check_state();
|
||||
connect();
|
||||
}
|
||||
|
||||
|
||||
template <typename ReadStream>
|
||||
void AsioStream<ReadStream>::connect()
|
||||
{
|
||||
do_connect();
|
||||
}
|
||||
|
||||
|
||||
template <typename ReadStream>
|
||||
void AsioStream<ReadStream>::disconnect()
|
||||
{
|
||||
do_disconnect();
|
||||
}
|
||||
|
||||
|
||||
template <typename ReadStream>
|
||||
void AsioStream<ReadStream>::stop()
|
||||
{
|
||||
active_ = false;
|
||||
read_timer_.cancel();
|
||||
state_timer_.cancel();
|
||||
disconnect();
|
||||
PcmStream::stop();
|
||||
}
|
||||
|
||||
|
||||
template <typename ReadStream>
|
||||
void AsioStream<ReadStream>::disconnect()
|
||||
{
|
||||
if (stream_ && stream_->is_open())
|
||||
stream_->close();
|
||||
setState(ReaderState::kIdle);
|
||||
}
|
||||
|
||||
|
||||
|
@ -174,35 +175,60 @@ void AsioStream<ReadStream>::on_connect()
|
|||
template <typename ReadStream>
|
||||
void AsioStream<ReadStream>::do_read()
|
||||
{
|
||||
// LOG(DEBUG, "AsioStream") << "do_read\n";
|
||||
// Reset the silence timer
|
||||
check_state(idle_threshold_ + std::chrono::milliseconds(chunk_ms_));
|
||||
boost::asio::async_read(*stream_, boost::asio::buffer(chunk_->payload, chunk_->payloadSize),
|
||||
[this](boost::system::error_code ec, std::size_t length) mutable
|
||||
{
|
||||
state_timer_.cancel();
|
||||
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, "AsioStream") << "Error reading message: " << ec.message() << ", length: " << length << "\n";
|
||||
connect();
|
||||
if (lastException_ != ec.message())
|
||||
{
|
||||
LOG(ERROR, "AsioStream") << "Error reading message: " << ec.message() << ", length: " << length << ", ec: " << ec << "\n";
|
||||
lastException_ = ec.message();
|
||||
}
|
||||
disconnect();
|
||||
wait(read_timer_, 100ms, [this] { connect(); });
|
||||
return;
|
||||
}
|
||||
|
||||
bytes_read_ += length;
|
||||
lastException_.clear();
|
||||
|
||||
if (isSilent(*chunk_))
|
||||
{
|
||||
silence_ += chunk_->duration<std::chrono::microseconds>();
|
||||
if (silence_ >= idle_threshold_)
|
||||
{
|
||||
setState(ReaderState::kIdle);
|
||||
// Avoid overflow
|
||||
silence_ = idle_threshold_;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
silence_ = 0ms;
|
||||
setState(ReaderState::kPlaying);
|
||||
}
|
||||
|
||||
// LOG(DEBUG, "AsioStream") << "Read: " << length << " bytes\n";
|
||||
// First read after connect. Set the initial read timestamp
|
||||
// the timestamp will be incremented after encoding,
|
||||
// since we do not know how much the encoder actually encoded
|
||||
|
||||
if (!first_)
|
||||
{
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto stream2systime_diff = now - tvEncodedChunk_;
|
||||
if (stream2systime_diff > chronos::sec(5) + chronos::msec(chunk_ms_))
|
||||
{
|
||||
LOG(WARNING, "AsioStream") << "Stream and system time out of sync: "
|
||||
<< std::chrono::duration_cast<std::chrono::microseconds>(stream2systime_diff).count() / 1000.
|
||||
<< " ms, resetting stream time.\n";
|
||||
first_ = true;
|
||||
}
|
||||
}
|
||||
// if (!first_)
|
||||
// {
|
||||
// auto now = std::chrono::steady_clock::now();
|
||||
// auto stream2systime_diff = now - tvEncodedChunk_;
|
||||
// if (stream2systime_diff > chronos::sec(5) + chronos::msec(chunk_ms_))
|
||||
// {
|
||||
// LOG(WARNING, "AsioStream") << "Stream and system time out of sync: "
|
||||
// << std::chrono::duration_cast<std::chrono::microseconds>(stream2systime_diff).count() / 1000.
|
||||
// << " ms, resetting stream time.\n";
|
||||
// first_ = true;
|
||||
// }
|
||||
// }
|
||||
if (first_)
|
||||
{
|
||||
first_ = false;
|
||||
|
@ -236,7 +262,6 @@ void AsioStream<ReadStream>::do_read()
|
|||
else
|
||||
{
|
||||
resync(std::chrono::duration_cast<std::chrono::nanoseconds>(currentTick - nextTick_));
|
||||
nextTick_ = currentTick + std::chrono::milliseconds(buffer_ms_);
|
||||
first_ = true;
|
||||
do_read();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue