Reduce some copies in serialization
Also update a bunch of stuff
This commit is contained in:
+5
-60
@@ -29,12 +29,11 @@
|
||||
#include <vector>
|
||||
#include <openxr/openxr.h>
|
||||
|
||||
#include "wivrn_serialization_types.h"
|
||||
|
||||
namespace xrt::drivers::wivrn
|
||||
{
|
||||
|
||||
template <typename T, typename Enable>
|
||||
struct serialization_traits;
|
||||
|
||||
// Default port for server to listen, both TCP and UDP
|
||||
static const int default_port = 9757;
|
||||
|
||||
@@ -180,9 +179,7 @@ struct feedback
|
||||
std::array<XrPosef, 2> real_pose;
|
||||
|
||||
uint8_t data_packets;
|
||||
uint8_t parity_packets;
|
||||
uint8_t received_data_packets;
|
||||
uint8_t received_parity_packets;
|
||||
};
|
||||
|
||||
using control_packets = std::variant<headset_info_packet, feedback, audio_data>;
|
||||
@@ -270,22 +267,11 @@ public:
|
||||
std::array<XrFovf, 2> fov;
|
||||
};
|
||||
std::optional<view_info_t> view_info;
|
||||
|
||||
// Container for the data, read payload instead
|
||||
std::vector<uint8_t> data;
|
||||
// Actual video data, may contain multiple NAL units
|
||||
std::span<uint8_t> payload;
|
||||
};
|
||||
|
||||
struct video_stream_parity_shard
|
||||
{
|
||||
uint8_t stream_item_idx;
|
||||
uint64_t frame_idx;
|
||||
uint16_t data_shard_count;
|
||||
uint8_t num_parity_elements;
|
||||
uint8_t parity_element;
|
||||
// Parity data
|
||||
std::vector<uint8_t> payload;
|
||||
// Container for the data, read payload instead
|
||||
data_holder data;
|
||||
};
|
||||
|
||||
struct haptics
|
||||
@@ -302,49 +288,8 @@ struct timesync_query
|
||||
};
|
||||
|
||||
using control_packets = std::variant<handshake, audio_stream_description, video_stream_description, audio_data>;
|
||||
using stream_packets = std::variant<video_stream_data_shard, video_stream_parity_shard, haptics, timesync_query>;
|
||||
using stream_packets = std::variant<video_stream_data_shard, haptics, timesync_query>;
|
||||
|
||||
} // namespace to_headset
|
||||
|
||||
template <>
|
||||
struct serialization_traits<to_headset::video_stream_data_shard, void>
|
||||
{
|
||||
template <typename T>
|
||||
static constexpr void type_hash(T & h)
|
||||
{
|
||||
h.feed("video_v0");
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void serialize(const to_headset::video_stream_data_shard & shard, T & packet)
|
||||
{
|
||||
packet.reserve(to_headset::video_stream_data_shard::max_payload_size + 20);
|
||||
packet.serialize(shard.stream_item_idx);
|
||||
packet.serialize(shard.frame_idx);
|
||||
packet.serialize(shard.shard_idx);
|
||||
packet.serialize(shard.flags);
|
||||
packet.serialize(shard.view_info);
|
||||
uint16_t size = shard.payload.size();
|
||||
packet.serialize(size);
|
||||
packet.write(shard.payload.data(), shard.payload.size_bytes());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static to_headset::video_stream_data_shard deserialize(T & packet)
|
||||
{
|
||||
to_headset::video_stream_data_shard shard;
|
||||
packet.deserialize(shard.stream_item_idx);
|
||||
packet.deserialize(shard.frame_idx);
|
||||
packet.deserialize(shard.shard_idx);
|
||||
packet.deserialize(shard.flags);
|
||||
packet.deserialize(shard.view_info);
|
||||
uint16_t size;
|
||||
packet.deserialize(size);
|
||||
size_t read_index;
|
||||
std::tie(read_index, shard.data) = packet.steal_buffer();
|
||||
shard.payload = std::span<uint8_t>(shard.data.data() + read_index, shard.data.size() - read_index);
|
||||
return shard;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace xrt::drivers::wivrn
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
@@ -35,6 +36,8 @@
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "wivrn_serialization_types.h"
|
||||
|
||||
namespace xrt::drivers::wivrn
|
||||
{
|
||||
|
||||
@@ -99,12 +102,9 @@ class serialization_packet
|
||||
std::vector<uint8_t> buffer;
|
||||
|
||||
public:
|
||||
serialization_packet() = default;
|
||||
std::span<uint8_t> span; // Will be sent at the end, untouched
|
||||
|
||||
void reserve(size_t n)
|
||||
{
|
||||
buffer.reserve(n);
|
||||
}
|
||||
serialization_packet() = default;
|
||||
|
||||
void write(const void * data, size_t size)
|
||||
{
|
||||
@@ -113,24 +113,17 @@ public:
|
||||
memcpy(&buffer[index], data, size);
|
||||
}
|
||||
|
||||
const uint8_t * data() const
|
||||
{
|
||||
return buffer.data();
|
||||
}
|
||||
size_t size() const
|
||||
{
|
||||
return buffer.size();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void serialize(const T & value)
|
||||
{
|
||||
serialization_traits<T>::serialize(value, *this);
|
||||
}
|
||||
|
||||
operator std::vector<uint8_t> &&() &&
|
||||
operator std::vector<std::span<uint8_t>> ()
|
||||
{
|
||||
return std::move(buffer);
|
||||
if (span.empty())
|
||||
return {buffer};
|
||||
return {buffer, span};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -154,6 +147,14 @@ public:
|
||||
read_index += size;
|
||||
}
|
||||
|
||||
std::span<uint8_t> read_span(size_t size)
|
||||
{
|
||||
check_remaining_size(size);
|
||||
std::span<uint8_t> res(&buffer[read_index], size);
|
||||
read_index += size;
|
||||
return res;
|
||||
}
|
||||
|
||||
size_t remaining() const
|
||||
{
|
||||
return buffer.size() - read_index;
|
||||
@@ -455,7 +456,7 @@ struct serialization_traits<std::variant<T...>>
|
||||
|
||||
static void serialize(const std::variant<T...> & value, serialization_packet & packet)
|
||||
{
|
||||
packet.serialize<uint32_t>(value.index());
|
||||
packet.serialize<uint8_t>(value.index());
|
||||
std::visit([&](const auto & x) { packet.serialize(x); }, value);
|
||||
}
|
||||
|
||||
@@ -472,7 +473,7 @@ struct serialization_traits<std::variant<T...>>
|
||||
|
||||
static std::variant<T...> deserialize(deserialization_packet & packet)
|
||||
{
|
||||
uint32_t type_index = packet.deserialize<uint32_t>();
|
||||
uint8_t type_index = packet.deserialize<uint8_t>();
|
||||
if (type_index >= sizeof...(T))
|
||||
throw deserialization_error();
|
||||
|
||||
@@ -506,6 +507,48 @@ struct serialization_traits<std::chrono::duration<Rep, Period>>
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct serialization_traits<std::span<uint8_t>>
|
||||
{
|
||||
static constexpr void type_hash(details::hash_context & h)
|
||||
{
|
||||
h.feed("span<uint8_t>");
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
static std::span<uint8_t> deserialize(deserialization_packet & packet)
|
||||
{
|
||||
size_t size = packet.deserialize<uint16_t>();
|
||||
return packet.read_span(size);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct serialization_traits<data_holder>
|
||||
{
|
||||
static constexpr void type_hash(details::hash_context & h)
|
||||
{
|
||||
}
|
||||
|
||||
static void serialize(const data_holder&, serialization_packet &)
|
||||
{
|
||||
}
|
||||
|
||||
static data_holder deserialize(deserialization_packet & packet)
|
||||
{
|
||||
data_holder value;
|
||||
size_t size;
|
||||
std::tie(size, value.c) = packet.steal_buffer();
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
namespace unit_tests
|
||||
{
|
||||
template <typename T>
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2023 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2023 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* 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
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace xrt::drivers::wivrn
|
||||
{
|
||||
|
||||
// Intended to be the last element of a serializable type
|
||||
// contains the data referenced by spans
|
||||
struct data_holder
|
||||
{
|
||||
data_holder() = default;
|
||||
data_holder(const data_holder&) = delete;
|
||||
data_holder(data_holder&&) = default;
|
||||
data_holder& operator=(const data_holder&) = delete;
|
||||
data_holder& operator=(data_holder&&) = default;
|
||||
|
||||
std::vector<uint8_t> c;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/uio.h>
|
||||
#include <system_error>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -282,6 +283,16 @@ void xrt::drivers::wivrn::UDP::send_raw(const std::vector<uint8_t> & data)
|
||||
throw std::system_error{errno, std::generic_category()};
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
if (::writev(fd, spans.data(), spans.size()) < 0)
|
||||
throw std::system_error{errno, std::generic_category()};
|
||||
}
|
||||
|
||||
xrt::drivers::wivrn::deserialization_packet xrt::drivers::wivrn::TCP::receive_raw()
|
||||
{
|
||||
size_t expected_size;
|
||||
@@ -323,10 +334,12 @@ xrt::drivers::wivrn::deserialization_packet xrt::drivers::wivrn::TCP::receive_ra
|
||||
return deserialization_packet{std::move(new_buffer), 4};
|
||||
}
|
||||
|
||||
void xrt::drivers::wivrn::TCP::send_raw(const std::vector<uint8_t> & data)
|
||||
void xrt::drivers::wivrn::TCP::send_raw(const std::vector<std::span<uint8_t>> & spans)
|
||||
{
|
||||
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);
|
||||
|
||||
|
||||
+29
-7
@@ -26,6 +26,7 @@
|
||||
#include <mutex>
|
||||
#include <netinet/ip.h>
|
||||
#include <exception>
|
||||
#include <span>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -90,6 +91,7 @@ public:
|
||||
deserialization_packet receive_raw();
|
||||
std::pair<xrt::drivers::wivrn::deserialization_packet, sockaddr_in6> receive_from_raw();
|
||||
void send_raw(const std::vector<uint8_t> & data);
|
||||
void send_raw(const std::vector<std::span<uint8_t>> & data);
|
||||
|
||||
void connect(in6_addr address, int port);
|
||||
void connect(in_addr address, int port);
|
||||
@@ -114,7 +116,7 @@ public:
|
||||
explicit TCP(int fd);
|
||||
|
||||
deserialization_packet receive_raw();
|
||||
void send_raw(const std::vector<uint8_t> & data);
|
||||
void send_raw(const std::vector<std::span<uint8_t>> & data);
|
||||
};
|
||||
|
||||
class TCPListener : public fd_base
|
||||
@@ -137,9 +139,27 @@ public:
|
||||
return {T{fd2}, addr};
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Socket, typename ReceivedType, typename SentType>
|
||||
class typed_socket : public Socket
|
||||
class typed_socket;
|
||||
|
||||
namespace details
|
||||
{
|
||||
template <class T, class Tuple>
|
||||
struct Index;
|
||||
|
||||
template <class T, class... Types>
|
||||
struct Index<T, std::tuple<T, Types...>> {
|
||||
static const std::size_t value = 0;
|
||||
};
|
||||
|
||||
template <class T, class U, class... Types>
|
||||
struct Index<T, std::tuple<U, Types...>> {
|
||||
static const std::size_t value = 1 + Index<T, std::tuple<Types...>>::value;
|
||||
};
|
||||
}
|
||||
|
||||
template <typename Socket, typename ReceivedType, typename ...VariantTypes>
|
||||
class typed_socket<Socket, ReceivedType, std::variant<VariantTypes...>> : public Socket
|
||||
{
|
||||
public:
|
||||
template <typename... Args>
|
||||
@@ -156,12 +176,14 @@ public:
|
||||
return packet.deserialize<ReceivedType>();
|
||||
}
|
||||
|
||||
template <typename T = SentType, typename = std::enable_if_t<std::is_same_v<T, SentType>>>
|
||||
void send(const T & data)
|
||||
template <typename T>
|
||||
void send(T && data)
|
||||
{
|
||||
serialization_packet p;
|
||||
p.serialize(data);
|
||||
this->send_raw(std::move(p));
|
||||
uint8_t index = details::Index<std::decay_t<T>, std::tuple<VariantTypes...>>::value;
|
||||
p.serialize(index);
|
||||
p.serialize(std::forward<T>(data));
|
||||
this->send_raw(p);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user