output/snapcast: calculate the latency for TIME responses

This commit is contained in:
Max Kellermann 2021-02-22 17:40:15 +01:00
parent e875da5d38
commit dfc67c45c7
2 changed files with 21 additions and 2 deletions

View File

@ -22,6 +22,7 @@
#include "Timestamp.hxx"
#include "Internal.hxx"
#include "tag/RiffFormat.hxx"
#include "event/Loop.hxx"
#include "net/SocketError.hxx"
#include "net/UniqueSocketDescriptor.hxx"
#include "util/StringView.hxx"
@ -146,7 +147,8 @@ SendTime(SocketDescriptor s, const PackedBE16 id,
const SnapcastBase &request_header,
const SnapcastTime &request_payload) noexcept
{
SnapcastTime payload = request_payload; // TODO
SnapcastTime payload = request_payload;
payload.latency = request_header.received - request_header.sent;
SnapcastBase base{};
base.type = uint16_t(SnapcastMessageType::TIME);
@ -196,12 +198,14 @@ SnapcastClient::SendWireChunk(ConstBuffer<void> payload,
BufferedSocket::InputResult
SnapcastClient::OnSocketInput(void *data, size_t length) noexcept
{
const auto &base = *(const SnapcastBase *)data;
auto &base = *(SnapcastBase *)data;
if (length < sizeof(base) ||
length < sizeof(base) + base.size)
return InputResult::MORE;
base.received = ToSnapcastTimestamp(GetEventLoop().SteadyNow());
ConsumeInput(sizeof(base) + base.size);
const ConstBuffer<void> payload{&base + 1, base.size};

View File

@ -35,6 +35,21 @@ enum class SnapcastMessageType : uint16_t {
struct SnapcastTimestamp {
PackedLE32 sec, usec;
constexpr SnapcastTimestamp operator-(SnapcastTimestamp other) const noexcept {
const uint32_t a_sec = sec, a_usec = usec;
const uint32_t b_sec = other.sec, b_usec = other.usec;
uint32_t result_sec = a_sec - b_sec;
uint32_t result_usec = a_usec - b_usec;
if (a_usec < b_usec) {
--result_sec;
result_usec += 1'000'0000;
}
return {result_sec, result_usec};
}
};
struct SnapcastBase {