mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-10 07:36:41 +02:00
Reformat code
This commit is contained in:
parent
c6518a4709
commit
e1c8250876
32 changed files with 505 additions and 351 deletions
|
@ -76,7 +76,9 @@ template <typename Timer, typename Rep, typename Period>
|
|||
void AsioStream<ReadStream>::wait(Timer& timer, const std::chrono::duration<Rep, Period>& duration, std::function<void()> handler)
|
||||
{
|
||||
timer.expires_after(duration);
|
||||
timer.async_wait([handler = std::move(handler)](const boost::system::error_code& ec) {
|
||||
timer.async_wait(
|
||||
[handler = std::move(handler)](const boost::system::error_code& ec)
|
||||
{
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, "AsioStream") << "Error during async wait: " << ec.message() << "\n";
|
||||
|
@ -114,7 +116,9 @@ template <typename ReadStream>
|
|||
void AsioStream<ReadStream>::check_state()
|
||||
{
|
||||
uint64_t last_read = bytes_read_;
|
||||
wait(state_timer_, std::chrono::milliseconds(500 + chunk_ms_), [this, last_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);
|
||||
|
@ -172,68 +176,71 @@ void AsioStream<ReadStream>::do_read()
|
|||
{
|
||||
// LOG(DEBUG, "AsioStream") << "do_read\n";
|
||||
boost::asio::async_read(*stream_, boost::asio::buffer(chunk_->payload, chunk_->payloadSize),
|
||||
[this](boost::system::error_code ec, std::size_t length) mutable {
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, "AsioStream") << "Error reading message: " << ec.message() << ", length: " << length << "\n";
|
||||
connect();
|
||||
return;
|
||||
}
|
||||
[this](boost::system::error_code ec, std::size_t length) mutable
|
||||
{
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, "AsioStream") << "Error reading message: " << ec.message() << ", length: " << length << "\n";
|
||||
connect();
|
||||
return;
|
||||
}
|
||||
|
||||
bytes_read_ += length;
|
||||
// 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
|
||||
bytes_read_ += length;
|
||||
// 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_)
|
||||
{
|
||||
first_ = false;
|
||||
tvEncodedChunk_ = std::chrono::steady_clock::now() - chunk_->duration<std::chrono::nanoseconds>();
|
||||
nextTick_ = std::chrono::steady_clock::now();
|
||||
}
|
||||
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;
|
||||
tvEncodedChunk_ = std::chrono::steady_clock::now() - chunk_->duration<std::chrono::nanoseconds>();
|
||||
nextTick_ = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
chunkRead(*chunk_);
|
||||
nextTick_ += chunk_->duration<std::chrono::nanoseconds>();
|
||||
auto currentTick = std::chrono::steady_clock::now();
|
||||
chunkRead(*chunk_);
|
||||
nextTick_ += chunk_->duration<std::chrono::nanoseconds>();
|
||||
auto currentTick = std::chrono::steady_clock::now();
|
||||
|
||||
// Synchronize read to chunk_ms_
|
||||
if (nextTick_ >= currentTick)
|
||||
{
|
||||
read_timer_.expires_after(nextTick_ - currentTick);
|
||||
read_timer_.async_wait([this](const boost::system::error_code& ec) {
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, "AsioStream") << "Error during async wait: " << ec.message() << "\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
do_read();
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
// Read took longer, wait for the buffer to fill up
|
||||
else
|
||||
{
|
||||
resync(std::chrono::duration_cast<std::chrono::nanoseconds>(currentTick - nextTick_));
|
||||
nextTick_ = currentTick + std::chrono::milliseconds(buffer_ms_);
|
||||
first_ = true;
|
||||
do_read();
|
||||
}
|
||||
});
|
||||
// Synchronize read to chunk_ms_
|
||||
if (nextTick_ >= currentTick)
|
||||
{
|
||||
read_timer_.expires_after(nextTick_ - currentTick);
|
||||
read_timer_.async_wait(
|
||||
[this](const boost::system::error_code& ec)
|
||||
{
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, "AsioStream") << "Error during async wait: " << ec.message() << "\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
do_read();
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
// Read took longer, wait for the buffer to fill up
|
||||
else
|
||||
{
|
||||
resync(std::chrono::duration_cast<std::chrono::nanoseconds>(currentTick - nextTick_));
|
||||
nextTick_ = currentTick + std::chrono::milliseconds(buffer_ms_);
|
||||
first_ = true;
|
||||
do_read();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace streamreader
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue