remove copies in audio thread

This commit is contained in:
Patrick Nicolas
2023-11-22 00:00:55 +01:00
parent 8b0895913b
commit e2868547e2
7 changed files with 101 additions and 59 deletions
+7 -5
View File
@@ -70,7 +70,7 @@ void wivrn::android::audio::output(AAudioStream * stream, const xrt::drivers::wi
while (!exiting) while (!exiting)
{ {
auto packet = output_buffer.pop(); auto packet = output_buffer.pop();
auto &buffer = packet.payload; auto & buffer = packet.payload;
int num_frames = buffer.size() / frame_size; int num_frames = buffer.size() / frame_size;
num_frames = AAudioStream_write(stream, buffer.data(), num_frames, 0); num_frames = AAudioStream_write(stream, buffer.data(), num_frames, 0);
@@ -103,15 +103,16 @@ void wivrn::android::audio::input(AAudioStream * stream, const xrt::drivers::wiv
xrt::drivers::wivrn::audio_data packet; xrt::drivers::wivrn::audio_data packet;
const int frame_size = format.num_channels * sizeof(int16_t); const int frame_size = format.num_channels * sizeof(int16_t);
const int max_frames = (format.sample_rate * 10) / 1000; // 10ms const int max_frames = (format.sample_rate * 10) / 1000; // 10ms
auto & buffer = packet.data.c;
buffer.resize(frame_size * max_frames);
while (!exiting) while (!exiting)
{ {
packet.payload.resize(frame_size * max_frames); result = AAudioStream_read(stream, buffer.data(), max_frames, 1'000'000'000);
result = AAudioStream_read(stream, packet.payload.data(), max_frames, 1'000'000'000);
if (result > 0) if (result > 0)
{ {
packet.timestamp = instance.now(); packet.timestamp = instance.now();
packet.payload.resize(frame_size * result); packet.payload = std::span<uint8_t>(buffer.data(), frame_size * result);
session.send_control(packet); session.send_control(packet);
} }
} }
@@ -131,7 +132,8 @@ void wivrn::android::audio::exit()
output_buffer.close(); output_buffer.close();
} }
wivrn::android::audio::audio(const xrt::drivers::wivrn::to_headset::audio_stream_description & desc, wivrn_session& session, xr::instance& instance): session(session), instance(instance) wivrn::android::audio::audio(const xrt::drivers::wivrn::to_headset::audio_stream_description & desc, wivrn_session & session, xr::instance & instance) :
session(session), instance(instance)
{ {
AAudioStreamBuilder * builder; AAudioStreamBuilder * builder;
+2 -1
View File
@@ -84,7 +84,8 @@ enum video_codec
struct audio_data struct audio_data
{ {
uint64_t timestamp; uint64_t timestamp;
std::vector<uint8_t> payload; std::span<uint8_t> payload;
data_holder data;
}; };
namespace from_headset namespace from_headset
+35 -10
View File
@@ -100,10 +100,11 @@ public:
class serialization_packet class serialization_packet
{ {
std::vector<uint8_t> buffer; 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: public:
std::span<uint8_t> span; // Will be sent at the end, untouched
serialization_packet() = default; serialization_packet() = default;
void write(const void * data, size_t size) void write(const void * data, size_t size)
@@ -111,6 +112,13 @@ public:
size_t index = buffer.size(); size_t index = buffer.size();
buffer.resize(index + size); buffer.resize(index + size);
memcpy(&buffer[index], data, 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> template <typename T>
@@ -119,11 +127,29 @@ public:
serialization_traits<T>::serialize(value, *this); serialization_traits<T>::serialize(value, *this);
} }
operator std::vector<std::span<uint8_t>> () operator std::vector<std::span<uint8_t>>()
{ {
if (span.empty()) struct visitor
return {buffer}; {
return {buffer, span}; 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>"); 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()); packet.serialize<uint16_t>(value.size());
//FIXME: nocopy packet.write(value);
packet.write(value.data(), value.size_bytes());
} }
static std::span<uint8_t> deserialize(deserialization_packet & packet) 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
View File
@@ -48,7 +48,7 @@ xrt::drivers::wivrn::fd_base::fd_base(xrt::drivers::wivrn::fd_base && other) :
other.fd = -1; 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); std::swap(fd, other.fd);
return *this; 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) void xrt::drivers::wivrn::UDP::send_raw(const std::vector<std::span<uint8_t>> & data)
{ {
std::vector<iovec> spans; std::vector<iovec> spans;
for (const auto& span: data) for (const auto & span: data)
spans.emplace_back((void*)span.data(), span.size()); spans.emplace_back((void *)span.data(), span.size());
if (::writev(fd, spans.data(), spans.size()) < 0) if (::writev(fd, spans.data(), spans.size()) < 0)
throw std::system_error{errno, std::generic_category()}; 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); std::lock_guard lock(*mutex);
assert(spans.size() == 1); std::vector<iovec> iovecs;
const auto& data = spans[0];
uint32_t size = data.size();
ssize_t sent = ::send(fd, &size, sizeof(size), MSG_NOSIGNAL);
if (sent == 0) uint32_t size = 0;
throw socket_shutdown{}; iovecs.emplace_back(&size, sizeof(size));
for (const auto & span: spans)
if (sent < 0)
throw std::system_error{errno, std::generic_category()};
assert(sent == sizeof(size));
size_t index = 0;
while (index < data.size())
{ {
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) if (sent == 0)
throw socket_shutdown{}; throw socket_shutdown{};
@@ -362,6 +369,16 @@ void xrt::drivers::wivrn::TCP::send_raw(const std::vector<std::span<uint8_t>> &
if (sent < 0) if (sent < 0)
throw std::system_error{errno, std::generic_category()}; 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;
} }
} }
+14 -16
View File
@@ -297,21 +297,19 @@ struct pulse_device : public audio_device
// read buffers must be smaller than buffer size on client or we will discard chunks often // read buffers must be smaller than buffer size on client or we will discard chunks often
const size_t buffer_size = (desc.speaker->sample_rate * sample_size * 2) / 1000; const size_t buffer_size = (desc.speaker->sample_rate * sample_size * 2) / 1000;
xrt::drivers::wivrn::audio_data packet; xrt::drivers::wivrn::audio_data packet;
// only send complete samples, keep remainder here std::vector<uint8_t> buffer(buffer_size, 0);
std::vector<uint8_t> remainder; size_t remainder = 0;
// Flush existing data, but keep alignment // Flush existing data, but keep alignment
{ {
int alignment = 0;
char sewer[1024]; char sewer[1024];
while (true) while (true)
{ {
int size = read(speaker_pipe.get_fd(), sewer, sizeof(sewer)); int size = read(speaker_pipe.get_fd(), sewer, sizeof(sewer));
if (size <= 0) if (size <= 0)
break; break;
alignment = (alignment + size) % sample_size; remainder = (remainder + size) % sample_size;
} }
packet.payload.resize(alignment, 0);
} }
try try
@@ -329,18 +327,18 @@ struct pulse_device : public audio_device
throw std::runtime_error("Error on speaker pipe"); throw std::runtime_error("Error on speaker pipe");
if (pfd.revents & POLLIN) if (pfd.revents & POLLIN)
{ {
size_t excess = packet.payload.size(); int size = read(pfd.fd, buffer.data() + remainder, buffer_size - remainder);
packet.payload.resize(buffer_size);
int size = read(pfd.fd, packet.payload.data() + excess, buffer_size - excess);
if (size < 0) if (size < 0)
throw std::system_error(errno, std::system_category()); throw std::system_error(errno, std::system_category());
packet.payload.resize(size + excess); size += remainder; // full size of available data
excess = (excess + size) % sample_size; remainder = size % sample_size; // data to keep for next iteration
remainder.assign(packet.payload.end() - excess, packet.payload.end()); size -= remainder; // size of data to send
packet.payload.resize(packet.payload.size() - excess); packet.payload = std::span<uint8_t>(buffer.begin(), size);
packet.timestamp = session.get_offset().to_headset(os_monotonic_get_ns()).count(); packet.timestamp = session.get_offset().to_headset(os_monotonic_get_ns()).count();
session.send_control(packet); session.send_control(packet);
std::swap(packet.payload, remainder); U_LOG_I("sendo audio: %zu bytes", packet.payload.size_bytes());
// put the remaining data at the beginning of the buffer
memmove(buffer.data(), buffer.data() + size, remainder);
} }
} }
} }
@@ -372,9 +370,9 @@ struct pulse_device : public audio_device
if (pfd.revents & POLLOUT) if (pfd.revents & POLLOUT)
{ {
auto packet = mic_buffer.pop(); auto packet = mic_buffer.pop();
auto &buffer = packet.payload; auto & buffer = packet.payload;
int written = write(mic_pipe.get_fd(), buffer.data() , buffer.size()); int written = write(mic_pipe.get_fd(), buffer.data(), buffer.size());
if (written < 0) if (written < 0)
throw std::system_error(errno, std::system_category()); throw std::system_error(errno, std::system_category());
@@ -388,7 +386,7 @@ struct pulse_device : public audio_device
} }
} }
void process_mic_data(const xrt::drivers::wivrn::audio_data & mic_data) override void process_mic_data(xrt::drivers::wivrn::audio_data && mic_data) override
{ {
mic_buffer.push(std::move(mic_data)); mic_buffer.push(std::move(mic_data));
} }
+2 -3
View File
@@ -36,7 +36,7 @@ struct audio_device
virtual xrt::drivers::wivrn::to_headset::audio_stream_description description() const = 0; virtual xrt::drivers::wivrn::to_headset::audio_stream_description description() const = 0;
virtual void process_mic_data(const xrt::drivers::wivrn::audio_data &) = 0; virtual void process_mic_data(xrt::drivers::wivrn::audio_data &&) = 0;
static std::shared_ptr<audio_device> create( static std::shared_ptr<audio_device> create(
const std::string & source_name, const std::string & source_name,
@@ -44,6 +44,5 @@ struct audio_device
const std::string & sink_name, const std::string & sink_name,
const std::string & sink_description, const std::string & sink_description,
const xrt::drivers::wivrn::from_headset::headset_info_packet & info, const xrt::drivers::wivrn::from_headset::headset_info_packet & info,
xrt::drivers::wivrn::wivrn_session& session); xrt::drivers::wivrn::wivrn_session & session);
}; };
+5 -5
View File
@@ -74,7 +74,8 @@ wivrn_system_devices * xrt::drivers::wivrn::wivrn_session::create_session(xrt::d
{ {
self = std::shared_ptr<wivrn_session>(new wivrn_session(std::move(tcp))); self = std::shared_ptr<wivrn_session>(new wivrn_session(std::move(tcp)));
while (not(control = self->connection.poll_control(-1))) while (not(control = self->connection.poll_control(-1)))
{} {
}
} }
catch (std::exception & e) catch (std::exception & e)
{ {
@@ -92,8 +93,7 @@ wivrn_system_devices * xrt::drivers::wivrn::wivrn_session::create_session(xrt::d
"wivrn.sink", "wivrn.sink",
"WiVRn", "WiVRn",
info, info,
*self *self);
);
self->send_control(self->audio_handle->description()); self->send_control(self->audio_handle->description());
} }
catch (const std::exception & e) catch (const std::exception & e)
@@ -195,10 +195,10 @@ void wivrn_session::operator()(from_headset::feedback && feedback)
dump_time("display", feedback.frame_index, o.from_headset(feedback.displayed), feedback.stream_index); dump_time("display", feedback.frame_index, o.from_headset(feedback.displayed), feedback.stream_index);
} }
void wivrn_session::operator()(audio_data&& data) void wivrn_session::operator()(audio_data && data)
{ {
if (audio_handle) if (audio_handle)
audio_handle->process_mic_data(data); audio_handle->process_mic_data(std::move(data));
} }
uint64_t clock_offset::from_headset(uint64_t ts) const uint64_t clock_offset::from_headset(uint64_t ts) const