remove copies in audio thread
This commit is contained in:
@@ -84,7 +84,8 @@ enum video_codec
|
||||
struct audio_data
|
||||
{
|
||||
uint64_t timestamp;
|
||||
std::vector<uint8_t> payload;
|
||||
std::span<uint8_t> payload;
|
||||
data_holder data;
|
||||
};
|
||||
|
||||
namespace from_headset
|
||||
|
||||
@@ -100,10 +100,11 @@ public:
|
||||
class serialization_packet
|
||||
{
|
||||
std::vector<uint8_t> buffer;
|
||||
// Either the size to read from the buffer, or an actual span
|
||||
// Last element is always an integer
|
||||
std::vector<std::variant<size_t, std::span<uint8_t>>> spans = {size_t(0)};
|
||||
|
||||
public:
|
||||
std::span<uint8_t> span; // Will be sent at the end, untouched
|
||||
|
||||
serialization_packet() = default;
|
||||
|
||||
void write(const void * data, size_t size)
|
||||
@@ -111,6 +112,13 @@ public:
|
||||
size_t index = buffer.size();
|
||||
buffer.resize(index + size);
|
||||
memcpy(&buffer[index], data, size);
|
||||
std::get<size_t>(spans.back()) += size;
|
||||
}
|
||||
|
||||
void write(std::span<uint8_t> span)
|
||||
{
|
||||
spans.push_back(span);
|
||||
spans.push_back(size_t(0));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -119,11 +127,29 @@ public:
|
||||
serialization_traits<T>::serialize(value, *this);
|
||||
}
|
||||
|
||||
operator std::vector<std::span<uint8_t>> ()
|
||||
operator std::vector<std::span<uint8_t>>()
|
||||
{
|
||||
if (span.empty())
|
||||
return {buffer};
|
||||
return {buffer, span};
|
||||
struct visitor
|
||||
{
|
||||
std::vector<uint8_t>::iterator it;
|
||||
std::vector<std::span<uint8_t>> res;
|
||||
void operator()(size_t s)
|
||||
{
|
||||
if (s > 0)
|
||||
res.emplace_back(it, it + s);
|
||||
it += s;
|
||||
}
|
||||
void operator()(const std::span<uint8_t> & span)
|
||||
{
|
||||
res.push_back(span);
|
||||
}
|
||||
};
|
||||
visitor v{.it = buffer.begin()};
|
||||
for (const auto & span_variant: spans)
|
||||
{
|
||||
std::visit(v, span_variant);
|
||||
}
|
||||
return v.res;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -515,11 +541,10 @@ struct serialization_traits<std::span<uint8_t>>
|
||||
h.feed("span<uint8_t>");
|
||||
}
|
||||
|
||||
static void serialize(const std::span<uint8_t>& value, serialization_packet & packet)
|
||||
static void serialize(const std::span<uint8_t> & value, serialization_packet & packet)
|
||||
{
|
||||
packet.serialize<uint16_t>(value.size());
|
||||
//FIXME: nocopy
|
||||
packet.write(value.data(), value.size_bytes());
|
||||
packet.write(value);
|
||||
}
|
||||
|
||||
static std::span<uint8_t> deserialize(deserialization_packet & packet)
|
||||
@@ -536,7 +561,7 @@ struct serialization_traits<data_holder>
|
||||
{
|
||||
}
|
||||
|
||||
static void serialize(const data_holder&, serialization_packet &)
|
||||
static void serialize(const data_holder &, serialization_packet &)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
+36
-19
@@ -48,7 +48,7 @@ xrt::drivers::wivrn::fd_base::fd_base(xrt::drivers::wivrn::fd_base && other) :
|
||||
other.fd = -1;
|
||||
}
|
||||
|
||||
xrt::drivers::wivrn::fd_base& xrt::drivers::wivrn::fd_base::operator=(xrt::drivers::wivrn::fd_base && other)
|
||||
xrt::drivers::wivrn::fd_base & xrt::drivers::wivrn::fd_base::operator=(xrt::drivers::wivrn::fd_base && other)
|
||||
{
|
||||
std::swap(fd, other.fd);
|
||||
return *this;
|
||||
@@ -286,8 +286,8 @@ void xrt::drivers::wivrn::UDP::send_raw(const std::vector<uint8_t> & data)
|
||||
void xrt::drivers::wivrn::UDP::send_raw(const std::vector<std::span<uint8_t>> & data)
|
||||
{
|
||||
std::vector<iovec> spans;
|
||||
for (const auto& span: data)
|
||||
spans.emplace_back((void*)span.data(), span.size());
|
||||
for (const auto & span: data)
|
||||
spans.emplace_back((void *)span.data(), span.size());
|
||||
|
||||
if (::writev(fd, spans.data(), spans.size()) < 0)
|
||||
throw std::system_error{errno, std::generic_category()};
|
||||
@@ -338,23 +338,30 @@ void xrt::drivers::wivrn::TCP::send_raw(const std::vector<std::span<uint8_t>> &
|
||||
{
|
||||
std::lock_guard lock(*mutex);
|
||||
|
||||
assert(spans.size() == 1);
|
||||
const auto& data = spans[0];
|
||||
uint32_t size = data.size();
|
||||
ssize_t sent = ::send(fd, &size, sizeof(size), MSG_NOSIGNAL);
|
||||
std::vector<iovec> iovecs;
|
||||
|
||||
if (sent == 0)
|
||||
throw socket_shutdown{};
|
||||
|
||||
if (sent < 0)
|
||||
throw std::system_error{errno, std::generic_category()};
|
||||
|
||||
assert(sent == sizeof(size));
|
||||
|
||||
size_t index = 0;
|
||||
while (index < data.size())
|
||||
uint32_t size = 0;
|
||||
iovecs.emplace_back(&size, sizeof(size));
|
||||
for (const auto & span: spans)
|
||||
{
|
||||
sent = ::send(fd, data.data() + index, data.size() - index, MSG_NOSIGNAL);
|
||||
size += span.size_bytes();
|
||||
iovecs.emplace_back(span.data(), span.size_bytes());
|
||||
}
|
||||
|
||||
msghdr hdr{
|
||||
.msg_name = nullptr,
|
||||
.msg_namelen = 0,
|
||||
.msg_iov = iovecs.data(),
|
||||
.msg_iovlen = iovecs.size(),
|
||||
.msg_control = nullptr,
|
||||
.msg_controllen = 0,
|
||||
.msg_flags = 0,
|
||||
};
|
||||
|
||||
while (true)
|
||||
{
|
||||
const auto & data = spans[0];
|
||||
ssize_t sent = ::sendmsg(fd, &hdr, MSG_NOSIGNAL);
|
||||
|
||||
if (sent == 0)
|
||||
throw socket_shutdown{};
|
||||
@@ -362,6 +369,16 @@ void xrt::drivers::wivrn::TCP::send_raw(const std::vector<std::span<uint8_t>> &
|
||||
if (sent < 0)
|
||||
throw std::system_error{errno, std::generic_category()};
|
||||
|
||||
index += sent;
|
||||
// iov fully consumed
|
||||
while (hdr.msg_iovlen > 0 and sent >= hdr.msg_iov[0].iov_len)
|
||||
{
|
||||
sent -= hdr.msg_iov[0].iov_len;
|
||||
++hdr.msg_iov;
|
||||
--hdr.msg_iovlen;
|
||||
}
|
||||
if (hdr.msg_iovlen == 0)
|
||||
return;
|
||||
*(uintptr_t *)&hdr.msg_iov[0].iov_base += sent;
|
||||
hdr.msg_iov[0].iov_len -= sent;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user