From 2b4303272dfb37632cc0dad9bc20f075277e4e7f Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sat, 3 Dec 2022 14:06:34 +0100 Subject: [PATCH] Initial audio support Co-authored-by: Guillaume Meunier --- CMakeLists.txt | 1 + README.md | 3 +- client/CMakeLists.txt | 7 +- client/audio/android/audio.cpp | 250 +++++++++++++++++++++++ client/audio/android/audio.h | 53 +++++ client/audio/audio.h | 42 ++++ client/scenes/stream.cpp | 3 + client/scenes/stream.h | 4 + client/scenes/stream_network.cpp | 7 + client/wivrn_client.cpp | 4 +- client/wivrn_client.h | 2 + common/wivrn_packets.h | 28 ++- server/CMakeLists.txt | 2 + server/driver/audio_setup.cpp | 327 +++++++++++++++++++++++++++++++ server/driver/audio_setup.h | 42 ++++ server/driver/wivrn_session.cpp | 17 ++ server/driver/wivrn_session.h | 3 + 17 files changed, 786 insertions(+), 9 deletions(-) create mode 100644 client/audio/android/audio.cpp create mode 100644 client/audio/android/audio.h create mode 100644 client/audio/audio.h create mode 100644 server/driver/audio_setup.cpp create mode 100644 server/driver/audio_setup.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 536190a0..25856ff8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,7 @@ if (WIVRN_BUILD_SERVER) endif() pkg_check_modules(AVAHI IMPORTED_TARGET avahi-client) + pkg_check_modules(libpulse IMPORTED_TARGET libpulse) find_package(Eigen3 REQUIRED) endif() diff --git a/README.md b/README.md index 850122c4..4c5a79be 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,8 @@ You will briefly have a "Waiting for connection" screen, followed by "Waiting fo Now on your computer you can run an OpenXR application, and it will show on your headset, enjoy! # TODO -* Sound support (playback and recording) +* Sound support (recording) +* Improve sound playback latency * SteamVR support with [OpenComposite](https://gitlab.com/znixian/OpenOVR) * Latency improvement * Application launcher diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 56727671..c23f2946 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -17,7 +17,8 @@ if(ANDROID) find_library(ANDROID_LIBRARY NAMES android REQUIRED) find_library(MEDIA_LIBRARY NAMES mediandk REQUIRED) - target_link_libraries(wivrn ${ANDROID_LIBRARY} ${MEDIA_LIBRARY}) + find_library(AAUDIO_LIBRARY NAMES aaudio REQUIRED) + target_link_libraries(wivrn ${ANDROID_LIBRARY} ${MEDIA_LIBRARY} ${AAUDIO_LIBRARY}) if(OCULUS) set(OCULUS_SDK_PATH ${CMAKE_SOURCE_DIR}/external/oculus) @@ -58,7 +59,9 @@ if(ANDROID) target_link_options(wivrn PRIVATE -Wl,--undefined=ANativeActivity_onCreate) file(GLOB PLATFORM_SOURCE CONFIGURE_DEPENDS - "decoder/android/*.cpp") + "audio/android/*.cpp" + "decoder/android/*.cpp" + ) target_sources(wivrn PRIVATE ${PLATFORM_SOURCE}) else() add_executable(wivrn) diff --git a/client/audio/android/audio.cpp b/client/audio/android/audio.cpp new file mode 100644 index 00000000..392eff28 --- /dev/null +++ b/client/audio/android/audio.cpp @@ -0,0 +1,250 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022 Guillaume Meunier + * Copyright (C) 2022 Patrick Nicolas + * + * 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 . + */ + +#include "audio.h" +#include "spdlog/spdlog.h" +#include +#include +#include +#include +#include +#include + + +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"); + + try + { + 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 output stream: ") + AAudio_convertResultToText(result)); + + size_t offset = 0; + char buffer[2000]; + const int frame_size = format.num_channels * sizeof(int16_t); + + while(!quit) + { + pollfd pfd{}; + pfd.fd = fd; + pfd.events = POLLIN; + + int r = ::poll(&pfd, 1, 100); + 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); + int num_frames = (size + offset) / frame_size; + + num_frames = AAudioStream_write(stream, buffer, num_frames, 1'000'000'000); + if (num_frames < 0) + throw std::runtime_error(std::string("Cannot play output stream: ") + AAudio_convertResultToText(result)); + + size_t consumed_bytes = num_frames * frame_size; + memmove(buffer, buffer + consumed_bytes, size + offset - consumed_bytes); + offset = size + offset - consumed_bytes; + + assert(offset >= 0); + assert(offset < sizeof(buffer)); + } + } + } + catch(std::exception& e) + { + spdlog::error("Error in audio output thread: {}", e.what()); + quit = true; + } + + AAudioStream_close(stream); +} + +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) + // { + // + // } + } + catch(...) + { + quit = true; + } + + AAudioStream_close(stream); +} + +void wivrn::android::audio::init(const xrt::drivers::wivrn::to_headset::audio_stream_description & desc, sockaddr * address, socklen_t address_size) +{ + if (connect(fd, address, address_size) < 0) + { + ::close(fd); + throw std::system_error{errno, std::generic_category()}; + } + + int nodelay = 1; + if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) < 0) + { + ::close(fd); + throw std::system_error{errno, std::generic_category()}; + } + + AAudioStreamBuilder * builder; + + aaudio_result_t result = AAudio_createStreamBuilder(&builder); + if (result != AAUDIO_OK) + { + ::close(fd); + throw std::runtime_error(std::string("Cannot create stream builder: ") + AAudio_convertResultToText(result)); + } + + if (desc.speaker) + { + AAudioStreamBuilder_setDirection(builder, AAUDIO_DIRECTION_OUTPUT); + AAudioStreamBuilder_setSampleRate(builder, desc.speaker->sample_rate); + AAudioStreamBuilder_setChannelCount(builder, desc.speaker->num_channels); + AAudioStreamBuilder_setPerformanceMode(builder, AAUDIO_PERFORMANCE_MODE_LOW_LATENCY); + AAudioStreamBuilder_setFormat(builder, AAUDIO_FORMAT_PCM_I16); + + AAudioStream * stream; + result = AAudioStreamBuilder_openStream(builder, &stream); + if (result != AAUDIO_OK) + spdlog::error("Cannot create output stream: {}", AAudio_convertResultToText(result)); + else + output_thread = std::thread(&audio::output, this, stream, *desc.speaker); + } + + if (desc.microphone) + { + AAudioStreamBuilder_setDirection(builder, AAUDIO_DIRECTION_INPUT); + AAudioStreamBuilder_setSampleRate(builder, desc.microphone->sample_rate); + AAudioStreamBuilder_setChannelCount(builder, desc.microphone->num_channels); + AAudioStreamBuilder_setPerformanceMode(builder, AAUDIO_PERFORMANCE_MODE_LOW_LATENCY); + AAudioStreamBuilder_setFormat(builder, AAUDIO_FORMAT_PCM_I16); + + AAudioStream * stream; + result = AAudioStreamBuilder_openStream(builder, &stream); + if (result != AAUDIO_OK) + spdlog::error("Cannot create input stream: {}", AAudio_convertResultToText(result)); + else + input_thread = std::thread(&audio::input, this, stream, *desc.microphone); + } + + AAudioStreamBuilder_delete(builder); +} + +wivrn::android::audio::audio(const xrt::drivers::wivrn::to_headset::audio_stream_description & desc, const in_addr & address) +{ + fd = socket(AF_INET, SOCK_STREAM, 0); + + if (fd < 0) + throw std::system_error{errno, std::generic_category()}; + + sockaddr_in sa; + sa.sin_family = AF_INET; + sa.sin_addr = address; + sa.sin_port = htons(desc.port); + + init(desc, (sockaddr *)&sa, sizeof(sa)); +} + +wivrn::android::audio::audio(const xrt::drivers::wivrn::to_headset::audio_stream_description & desc, const in6_addr & address) +{ + fd = socket(AF_INET6, SOCK_STREAM, 0); + + if (fd < 0) + throw std::system_error{errno, std::generic_category()}; + + sockaddr_in6 sa; + sa.sin6_family = AF_INET6; + sa.sin6_addr = address; + sa.sin6_port = htons(desc.port); + + init(desc, (sockaddr *)&sa, sizeof(sa)); +} + +wivrn::android::audio::~audio() +{ + quit = true; + + if (input_thread.joinable()) + input_thread.join(); + + if (output_thread.joinable()) + output_thread.join(); + + if (fd >= 0) + ::close(fd); +} + +void wivrn::android::audio::get_audio_description(xrt::drivers::wivrn::from_headset::headset_info_packet & info) +{ + AAudioStreamBuilder * builder; + + aaudio_result_t result = AAudio_createStreamBuilder(&builder); + if (result != AAUDIO_OK) + return; + + AAudioStreamBuilder_setDirection(builder, AAUDIO_DIRECTION_OUTPUT); + AAudioStream * stream; + result = AAudioStreamBuilder_openStream(builder, &stream); + + if (result == AAUDIO_OK) + { + info.speaker = { + .num_channels = (uint8_t)AAudioStream_getChannelCount(stream), + .sample_rate = (uint32_t)AAudioStream_getSampleRate(stream) + }; + + AAudioStream_close(stream); + stream = nullptr; + } + + AAudioStreamBuilder_setDirection(builder, AAUDIO_DIRECTION_INPUT); + result = AAudioStreamBuilder_openStream(builder, &stream); + + if (result == AAUDIO_OK) + { + info.microphone = { + .num_channels = (uint8_t)AAudioStream_getChannelCount(stream), + .sample_rate = (uint32_t)AAudioStream_getSampleRate(stream) + }; + + AAudioStream_close(stream); + stream = nullptr; + } + + AAudioStreamBuilder_delete(builder); +} + diff --git a/client/audio/android/audio.h b/client/audio/android/audio.h new file mode 100644 index 00000000..741747e3 --- /dev/null +++ b/client/audio/android/audio.h @@ -0,0 +1,53 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022 Guillaume Meunier + * Copyright (C) 2022 Patrick Nicolas + * + * 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 . + */ + +#pragma once + +#include "wivrn_packets.h" +#include +#include + +struct AAudioStreamStruct; + +namespace wivrn::android +{ +class audio +{ + void output(AAudioStreamStruct * stream, const xrt::drivers::wivrn::to_headset::audio_stream_description::device& format); + void input(AAudioStreamStruct * stream, const xrt::drivers::wivrn::to_headset::audio_stream_description::device& format); + + std::thread output_thread; + std::thread input_thread; + + + std::atomic quit = false; + int fd = -1; + + void init(const xrt::drivers::wivrn::to_headset::audio_stream_description & desc, sockaddr * address, socklen_t address_size); + +public: + audio(const audio&) = delete; + audio& operator=(const audio&) = delete; + audio(const xrt::drivers::wivrn::to_headset::audio_stream_description&, const in_addr& ); + audio(const xrt::drivers::wivrn::to_headset::audio_stream_description&, const in6_addr& ); + ~audio(); + + static void get_audio_description(xrt::drivers::wivrn::from_headset::headset_info_packet& info); +}; +} diff --git a/client/audio/audio.h b/client/audio/audio.h new file mode 100644 index 00000000..20ebc7de --- /dev/null +++ b/client/audio/audio.h @@ -0,0 +1,42 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022 Guillaume Meunier + * Copyright (C) 2022 Patrick Nicolas + * + * 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 . + */ + +#pragma once + +#include "wivrn_packets.h" + +#ifdef XR_USE_PLATFORM_ANDROID +#include "android/audio.h" +using audio = ::wivrn::android::audio; +#else + +class audio +{ +public: + + audio(const audio&) = delete; + audio& operator=(const audio&) = delete; + audio(const xrt::drivers::wivrn::to_headset::audio_stream_description&, const in_addr& ) {} + audio(const xrt::drivers::wivrn::to_headset::audio_stream_description&, const in6_addr& ) {} + ~audio() = default; + + static void get_audio_description(xrt::drivers::wivrn::from_headset::headset_info_packet& info) {} +}; + +#endif diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index b4ad3017..e053ff10 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -38,6 +38,7 @@ #include #include #include +#include "audio/audio.h" using namespace xrt::drivers::wivrn; @@ -90,6 +91,8 @@ std::shared_ptr scenes::stream::create(std::unique_ptrnetwork_session->send_control(info); self->network_thread = std::thread(&stream::process_packets, self.get()); diff --git a/client/scenes/stream.h b/client/scenes/stream.h index 889dfa11..4cd7e1b9 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -33,6 +33,7 @@ #include #include #include +#include "audio/audio.h" namespace scenes { @@ -88,6 +89,8 @@ class stream : public scene, public std::enable_shared_from_this XrTime first_frame_time{}; const float dbrightness = 2; + std::optional