diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index fe2d38d6..e9d9444b 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -21,6 +21,8 @@
+
+
diff --git a/client/audio/android/audio.cpp b/client/audio/android/audio.cpp
index 392eff28..74b03dd3 100644
--- a/client/audio/android/audio.cpp
+++ b/client/audio/android/audio.cpp
@@ -19,19 +19,26 @@
#include "audio.h"
#include "spdlog/spdlog.h"
-#include
-#include
#include
-#include
-#include
+#include
#include
+#include
+#include
+#include
+#include
+#include
+#include
+static unsigned int buffer_size_ms = 10;
+static unsigned int buffer_size_mult = 4;
-void wivrn::android::audio::output(AAudioStream * stream, const xrt::drivers::wivrn::to_headset::audio_stream_description::device& format)
+void wivrn::android::audio::output(AAudioStream * stream, const xrt::drivers::wivrn::to_headset::audio_stream_description::device & format)
{
pthread_setname_np(pthread_self(), "audio_out_thread");
spdlog::info("audio_out_thread started");
+ char * buffer;
+ char * sewer;
try
{
@@ -42,10 +49,14 @@ void wivrn::android::audio::output(AAudioStream * stream, const xrt::drivers::wi
throw std::runtime_error(std::string("Cannot start output stream: ") + AAudio_convertResultToText(result));
size_t offset = 0;
- char buffer[2000];
- const int frame_size = format.num_channels * sizeof(int16_t);
+ const size_t frame_size = format.num_channels * sizeof(int16_t);
- while(!quit)
+ size_t bufsize = frame_size * format.sample_rate / 1000 * buffer_size_ms;
+ size_t max_bytes_available = bufsize * buffer_size_mult;
+ buffer = new char[bufsize];
+ sewer = new char[bufsize];
+
+ while (!quit)
{
pollfd pfd{};
pfd.fd = fd;
@@ -55,13 +66,16 @@ void wivrn::android::audio::output(AAudioStream * stream, const xrt::drivers::wi
if (r < 0)
throw std::system_error(errno, std::system_category());
-
if (pfd.revents & (POLLHUP | POLLERR))
throw std::runtime_error("Error on audio socket");
if (pfd.revents & POLLIN)
{
- ssize_t size = recv(fd, buffer + offset, sizeof(buffer) - offset, 0);
+ ssize_t bytes_available;
+ while (ioctl(fd, FIONREAD, &bytes_available) == 0 && bytes_available > max_bytes_available)
+ recv(fd, sewer, bufsize, 0);
+
+ ssize_t size = recv(fd, buffer + offset, bufsize - offset, 0);
int num_frames = (size + offset) / frame_size;
num_frames = AAudioStream_write(stream, buffer, num_frames, 1'000'000'000);
@@ -72,33 +86,46 @@ void wivrn::android::audio::output(AAudioStream * stream, const xrt::drivers::wi
memmove(buffer, buffer + consumed_bytes, size + offset - consumed_bytes);
offset = size + offset - consumed_bytes;
- assert(offset >= 0);
- assert(offset < sizeof(buffer));
+ assert(offset < bufsize);
}
}
}
- catch(std::exception& e)
+ catch (std::exception & e)
{
spdlog::error("Error in audio output thread: {}", e.what());
quit = true;
}
-
+ delete buffer;
+ delete sewer;
AAudioStream_close(stream);
}
-void wivrn::android::audio::input(AAudioStream * stream, const xrt::drivers::wivrn::to_headset::audio_stream_description::device& format)
+void wivrn::android::audio::input(AAudioStream * stream, const xrt::drivers::wivrn::to_headset::audio_stream_description::device & format)
{
pthread_setname_np(pthread_self(), "audio_in_thread");
try
{
- // while(!quit)
- // {
- //
- // }
+ AAudioStream_requestStart(stream);
+ aaudio_stream_state_t state;
+ aaudio_result_t result = AAudioStream_waitForStateChange(stream, AAUDIO_STREAM_STATE_STARTING, &state, 1'000'000'000);
+ if (result != AAUDIO_OK)
+ throw std::runtime_error(std::string("Cannot start input stream: ") + AAudio_convertResultToText(result));
+
+ char buffer[2000];
+ const int frame_size = format.num_channels * sizeof(int16_t);
+ const int max_frames = 2000 / frame_size;
+
+ while (!quit)
+ {
+ result = AAudioStream_read(stream, buffer, max_frames, 1'000'000'000);
+ if (result > 0)
+ send(fd, buffer, frame_size * result, 0);
+ }
}
- catch(...)
+ catch (std::exception & e)
{
+ spdlog::error("Error in audio input thread: {}", e.what());
quit = true;
}
@@ -223,9 +250,8 @@ void wivrn::android::audio::get_audio_description(xrt::drivers::wivrn::from_head
if (result == AAUDIO_OK)
{
info.speaker = {
- .num_channels = (uint8_t)AAudioStream_getChannelCount(stream),
- .sample_rate = (uint32_t)AAudioStream_getSampleRate(stream)
- };
+ .num_channels = (uint8_t)AAudioStream_getChannelCount(stream),
+ .sample_rate = (uint32_t)AAudioStream_getSampleRate(stream)};
AAudioStream_close(stream);
stream = nullptr;
@@ -237,9 +263,8 @@ void wivrn::android::audio::get_audio_description(xrt::drivers::wivrn::from_head
if (result == AAUDIO_OK)
{
info.microphone = {
- .num_channels = (uint8_t)AAudioStream_getChannelCount(stream),
- .sample_rate = (uint32_t)AAudioStream_getSampleRate(stream)
- };
+ .num_channels = (uint8_t)AAudioStream_getChannelCount(stream),
+ .sample_rate = (uint32_t)AAudioStream_getSampleRate(stream)};
AAudioStream_close(stream);
stream = nullptr;
@@ -247,4 +272,3 @@ void wivrn::android::audio::get_audio_description(xrt::drivers::wivrn::from_head
AAudioStreamBuilder_delete(builder);
}
-
diff --git a/server/driver/audio_setup.cpp b/server/driver/audio_setup.cpp
index dcea2d0c..807da185 100644
--- a/server/driver/audio_setup.cpp
+++ b/server/driver/audio_setup.cpp
@@ -4,6 +4,10 @@
#include "../wivrn_ipc.h"
#include "wivrn_sockets.h"
+#include
+#include
+#include
+#include
#include
#include
#include
@@ -18,7 +22,9 @@
#include
#include
#include
+#include
#include
+#include
#include
#include
@@ -27,6 +33,12 @@ static const char * sink_name = "WiVRn";
static const char * source_pipe = "wivrn-source";
static const char * sink_pipe = "wivrn-sink";
+static unsigned int buffer_size_ms = 10;
+
+// max allowed bytes in the output pipe before we start discarding data
+// total buffer size = buffer_size_ms + buffer_size_ms * buffer_size_mult
+static unsigned int buffer_size_mult = 4;
+
struct module_entry
{
uint32_t module;
@@ -284,6 +296,9 @@ struct pulse_publish_handle : public audio_publish_handle
std::optional speaker;
std::optional microphone;
+ int mic_buf_size;
+ int spk_buf_size;
+
~pulse_publish_handle()
{
quit = true;
@@ -323,27 +338,42 @@ struct pulse_publish_handle : public audio_publish_handle
// in pairs (read, write)
// negative if already available
std::vector pfd;
+ size_t num_pairs;
+
xrt::drivers::wivrn::fd_base speaker_pipe;
xrt::drivers::wivrn::fd_base mic_pipe;
+
+ ssize_t bufsize[] = {0, 0};
+ ssize_t max_bytes_in_pipe[] = {0, 0};
+ char * buf = new char[std::max(spk_buf_size, mic_buf_size)];
+
if (speaker)
{
+ bufsize[num_pairs] = spk_buf_size;
speaker_pipe = open(speaker->socket.c_str(), O_RDONLY | O_NONBLOCK);
if (speaker_pipe.get_fd() < 0)
throw std::system_error(errno, std::system_category(), "failed to open speaker pipe " + speaker->socket.string());
pfd.push_back({.fd = speaker_pipe.get_fd(), .events = POLLIN});
pfd.push_back({.fd = client.get_fd(), .events = POLLOUT});
+
+ num_pairs++;
+
// flush pipe, so we don't have old samples
- char sewer[1024];
- while (read(speaker_pipe.get_fd(), sewer, sizeof(sewer)) > 0)
+ while (read(speaker_pipe.get_fd(), buf, spk_buf_size) > 0)
{}
}
if (microphone)
{
+ bufsize[num_pairs] = mic_buf_size;
+ max_bytes_in_pipe[num_pairs] = mic_buf_size * buffer_size_mult;
+
mic_pipe = open(microphone->socket.c_str(), O_WRONLY);
if (mic_pipe.get_fd() < 0)
throw std::system_error(errno, std::system_category(), "failed to open microphone pipe " + microphone->socket.string());
pfd.push_back({.fd = client.get_fd(), .events = POLLIN});
pfd.push_back({.fd = mic_pipe.get_fd(), .events = POLLOUT});
+
+ num_pairs++;
}
while (not quit)
{
@@ -351,27 +381,32 @@ struct pulse_publish_handle : public audio_publish_handle
if (r < 0)
throw std::system_error(errno, std::system_category());
- for (size_t i = 0; i < pfd.size(); ++i)
+ for (size_t p = 0; p < num_pairs; p++)
{
- if (pfd[i].revents & (POLLHUP | POLLERR))
+ int i = p * 2;
+ int o = i + 1;
+ if ((pfd[i].revents | pfd[o].revents) & (POLLHUP | POLLERR))
throw std::runtime_error("Error on audio socket");
- if (pfd[i].revents & (POLLIN | POLLOUT))
+ if ((pfd[i].revents) & POLLIN)
{
- // mark fd as ready
- pfd[i].fd = -pfd[i].fd;
- size_t base = i - i % 2;
+ ssize_t bytes_read = read(pfd[i].fd, buf, bufsize[p]);
+ ssize_t bytes_to_write;
- // if both in and out fd are ready, do something
- if (pfd[base].fd < 0 and pfd[base + 1].fd < 0)
+ if (max_bytes_in_pipe[p] != 0)
{
- pfd[base].fd = -pfd[base].fd;
- pfd[base + 1].fd = -pfd[base + 1].fd;
-
- ssize_t written = splice(pfd[base].fd, nullptr, pfd[base + 1].fd, nullptr, 1024, SPLICE_F_MOVE);
- if (written < 0)
- throw std::system_error(errno, std::system_category(), "failed to transfer audio data");
+ ssize_t bytes_in_pipe;
+ if (ioctl(pfd[o].fd, FIONREAD, &bytes_in_pipe) == 0)
+ bytes_to_write = std::min((int)max_bytes_in_pipe[p] - (int)bytes_in_pipe, (int)bytes_read);
+ else
+ bytes_to_write = bytes_read;
}
+ else
+ bytes_to_write = bytes_read;
+
+ ssize_t written = write(pfd[o].fd, buf, bytes_to_write);
+ if (written < 0)
+ throw std::system_error(errno, std::system_category(), "failed to transfer audio data");
}
}
}
@@ -426,11 +461,13 @@ struct pulse_publish_handle : public audio_publish_handle
if (info.microphone)
{
result->microphone = ensure_source(cnx, source_name.c_str(), source_description, info.microphone->num_channels, info.microphone->sample_rate);
+ result->mic_buf_size = 2 * info.microphone->num_channels * (info.microphone->sample_rate / 1000 * buffer_size_ms);
result->desc.microphone = {.num_channels = info.microphone->num_channels, .sample_rate = info.microphone->sample_rate};
}
if (info.speaker)
{
result->speaker = ensure_sink(cnx, sink_name.c_str(), sink_description, info.speaker->num_channels, info.speaker->sample_rate);
+ result->spk_buf_size = 2 * info.speaker->num_channels * (info.speaker->sample_rate / 1000 * buffer_size_ms);
result->desc.speaker = {.num_channels = info.speaker->num_channels, .sample_rate = info.speaker->sample_rate};
}