Improve audio latency

Switch from module-simple-protocol-tcp + null sink/source to pipe
sink/source and custom network streaming.
This commit is contained in:
Patrick Nicolas
2022-12-04 18:09:11 +01:00
parent cea4e88b43
commit 9dd10e5261
3 changed files with 242 additions and 111 deletions
+12 -15
View File
@@ -41,13 +41,19 @@ const char * xrt::drivers::wivrn::socket_shutdown::what() const noexcept
return "Socket shutdown";
}
xrt::drivers::wivrn::socket_base::socket_base(xrt::drivers::wivrn::socket_base && other) :
xrt::drivers::wivrn::fd_base::fd_base(xrt::drivers::wivrn::fd_base && other) :
fd(other.fd)
{
other.fd = -1;
}
xrt::drivers::wivrn::socket_base::~socket_base()
xrt::drivers::wivrn::fd_base& xrt::drivers::wivrn::fd_base::operator=(xrt::drivers::wivrn::fd_base && other)
{
std::swap(fd, other.fd);
return *this;
}
xrt::drivers::wivrn::fd_base::~fd_base()
{
if (fd >= 0)
::close(fd);
@@ -188,6 +194,10 @@ xrt::drivers::wivrn::TCP::TCP(in_addr address, int port)
init();
}
xrt::drivers::wivrn::TCPListener::TCPListener()
{
}
xrt::drivers::wivrn::TCPListener::TCPListener(int port)
{
fd = socket(AF_INET6, SOCK_STREAM, 0);
@@ -223,19 +233,6 @@ xrt::drivers::wivrn::TCPListener::TCPListener(int port)
}
}
std::pair<xrt::drivers::wivrn::TCP, sockaddr_in6>
xrt::drivers::wivrn::TCPListener::accept()
{
sockaddr_in6 addr{};
socklen_t addrlen = sizeof(addr);
int fd2 = ::accept(fd, (sockaddr *)&addr, &addrlen);
if (fd2 < 0)
throw std::system_error{errno, std::generic_category()};
return {TCP{fd2}, addr};
}
// std::pair<xrt::drivers::wivrn::deserialization_packet, sockaddr_in6> xrt::drivers::wivrn::UDP::receive_from_raw()
// {
// std::vector<uint8_t> buffer(2000);
+31 -16
View File
@@ -21,6 +21,7 @@
#include "wivrn_serialization.h"
#include <cassert>
#include <memory>
#include <mutex>
#include <netinet/ip.h>
@@ -57,29 +58,34 @@ struct index_of_type<T, T2, Ts...> : std::integral_constant<size_t, 1 + index_of
static_assert(index_of_type<int, int, float>::value == 0);
static_assert(index_of_type<float, int, float>::value == 1);
class socket_base
class fd_base
{
protected:
int fd = -1;
socket_base() = default;
socket_base(const socket_base &) = delete;
socket_base(socket_base &&);
~socket_base();
fd_base(const fd_base &) = delete;
public:
fd_base() = default;
fd_base(int fd): fd{fd} {}
fd_base(fd_base &&);
fd_base& operator=(fd_base &&);
~fd_base();
int get_fd() const
{
return fd;
}
operator bool() const
{
return fd != -1;
}
};
class UDP : public socket_base
class UDP : public fd_base
{
public:
UDP();
UDP(const UDP &) = delete;
UDP(UDP &&) = default;
deserialization_packet receive_raw();
void send_raw(const std::vector<uint8_t> & data);
@@ -92,7 +98,7 @@ public:
void set_receive_buffer_size(int size);
};
class TCP : public socket_base
class TCP : public fd_base
{
std::vector<uint8_t> buffer;
std::unique_ptr<std::mutex> mutex;
@@ -103,21 +109,30 @@ public:
TCP(in6_addr address, int port);
TCP(in_addr address, int port);
explicit TCP(int fd);
TCP(const TCP &) = delete;
TCP(TCP &&) = default;
deserialization_packet receive_raw();
void send_raw(const std::vector<uint8_t> & data);
};
class TCPListener : public socket_base
class TCPListener : public fd_base
{
public:
TCPListener();
TCPListener(int port);
TCPListener(const TCPListener &) = delete;
TCPListener(TCPListener &&) = default;
std::pair<TCP, sockaddr_in6> accept();
template <typename T = TCP>
std::pair<T, sockaddr_in6> accept()
{
assert(fd != -1);
sockaddr_in6 addr{};
socklen_t addrlen = sizeof(addr);
int fd2 = ::accept(fd, (sockaddr *)&addr, &addrlen);
if (fd2 < 0)
throw std::system_error{errno, std::generic_category()};
return {T{fd2}, addr};
}
};
template <typename Socket, typename ReceivedType, typename SentType>