From 20accd9724d715bca1eb4ffd18689ab3f7972123 Mon Sep 17 00:00:00 2001 From: geneotech Date: Thu, 11 Apr 2024 21:36:34 +0200 Subject: [PATCH] Add and build WebRTC submodule on native --- .gitmodules | 3 +++ CMakeLists.txt | 33 +++++++++++++++++++++++++++++- docs/pages/todo/brainstorm_now.md | 3 +++ src/3rdparty/libdatachannel | 1 + src/augs/network/network_types.cpp | 19 +++++++++++++++++ src/work.cpp | 4 ++++ 6 files changed, 62 insertions(+), 1 deletion(-) create mode 160000 src/3rdparty/libdatachannel diff --git a/.gitmodules b/.gitmodules index 718ee6481..ccfe77dba 100644 --- a/.gitmodules +++ b/.gitmodules @@ -31,3 +31,6 @@ [submodule "src/3rdparty/rapidjson"] path = src/3rdparty/rapidjson url = https://github.com/Tencent/rapidjson +[submodule "src/3rdparty/libdatachannel"] + path = src/3rdparty/libdatachannel + url = https://github.com/paullouisageneau/libdatachannel.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e7d870de..b1b60d9af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -191,7 +191,8 @@ endif() option(BUILD_OPENAL "Build OpenAL Soft." ${DEFAULT_OPT}) option(BUILD_OPENGL "Build OpenGL-related code." ${DEFAULT_OPT}) -option(BUILD_NETWORKING "Build the networking backend." ${DEFAULT_NET_OPT}) +option(BUILD_NETWORKING "Build networking." ${DEFAULT_NET_OPT}) +option(BUILD_WEBRTC "Build WebRTC for client and server. This will enable networking on the Web and cross-play between web and native clients." ${DEFAULT_NET_OPT}) option(BUILD_OPENSSL "Build SSL support. Required for secure automatic application updates." ${DEFAULT_NET_OPT}) option(BUILD_FREETYPE "Build FreeType library responsible for loading fonts." ${DEFAULT_OPT}) option(BUILD_WINDOW_FRAMEWORK "Build code specific to a given platfom, e.g. window management done by WinAPI." ${DEFAULT_OPT}) @@ -245,6 +246,10 @@ else() message("Building without debugger setup.") endif() +if (NOT BUILD_NETWORKING) + set(BUILD_WEBRTC OFF) +endif() + if (BUILD_FOR_WEB OR HYPERSOMNIA_BARE) # Slowly enabling one by one set(BUILD_FREETYPE ON) @@ -1717,6 +1722,10 @@ if(BUILD_NETWORKING) add_definitions(-DBUILD_NETWORKING=1) endif() +if(BUILD_WEBRTC) + add_definitions(-DBUILD_WEBRTC=1) +endif() + if(BUILD_FREETYPE) add_definitions(-DBUILD_FREETYPE=1) endif() @@ -1963,6 +1972,28 @@ if(BUILD_WINDOW_FRAMEWORK) endif() endif() +if(BUILD_WEBRTC) + set(USE_GNUTLS OFF CACHE BOOL "" FORCE) + set(USE_MBEDTLS OFF CACHE BOOL "" FORCE) + + set(NO_MEDIA ON CACHE BOOL "" FORCE) + set(NO_WEBSOCKET ON CACHE BOOL "" FORCE) + set(NO_EXAMPLES ON CACHE BOOL "" FORCE) + set(NO_TESTS ON CACHE BOOL "" FORCE) + + if(BUILD_FOR_WEB) + add_subdirectory("${PROJECT_SOURCE_DIR}/src/3rdparty/datachannel-wasm" EXCLUDE_FROM_ALL) + list(APPEND HYPERSOMNIA_LIBS datachannel-wasm) + else() + add_subdirectory("${PROJECT_SOURCE_DIR}/src/3rdparty/libdatachannel" EXCLUDE_FROM_ALL) + + # Would otherwise give error on src/3rdparty/libdatachannel/src/rtp.cpp + target_compile_options(datachannel-static PRIVATE -Wno-cast-align) + + list(APPEND HYPERSOMNIA_LIBS datachannel-static) + endif() +endif() + if(BUILD_OPENAL AND NOT BUILD_FOR_WEB) list(APPEND HYPERSOMNIA_LIBS OpenAL) endif() diff --git a/docs/pages/todo/brainstorm_now.md b/docs/pages/todo/brainstorm_now.md index 9a843e8de..696a5a48e 100644 --- a/docs/pages/todo/brainstorm_now.md +++ b/docs/pages/todo/brainstorm_now.md @@ -6,6 +6,9 @@ permalink: brainstorm_now summary: That which we are brainstorming at the moment. --- +- fix spawn prediction on connect to prevent the unpleasant glitch + - might just have to make it predictable instead of dependent on cosmos step? + - "download native client" in the right top - clicking outside should close buy menu too diff --git a/src/3rdparty/libdatachannel b/src/3rdparty/libdatachannel new file mode 160000 index 000000000..fbd8f23d6 --- /dev/null +++ b/src/3rdparty/libdatachannel @@ -0,0 +1 @@ +Subproject commit fbd8f23d6af2cc3cd3b8bbc5154d3bbbbd0f01b1 diff --git a/src/augs/network/network_types.cpp b/src/augs/network/network_types.cpp index f50f7eb6c..a25293366 100644 --- a/src/augs/network/network_types.cpp +++ b/src/augs/network/network_types.cpp @@ -8,6 +8,10 @@ #include #include +#if BUILD_WEBRTC +#include "rtc/rtc.hpp" +#endif + bool InitializeYojimbo(); void ShutdownYojimbo(); @@ -25,6 +29,12 @@ LOG_NOFORMAT(Buffer); // finaly print the text return 0; } +#if BUILD_WEBRTC +void rtc_log_callback(rtc::LogLevel, std::string message) { + LOG_NOFORMAT(message); +} +#endif + namespace augs { namespace network { void enable_detailed_logs(const bool flag) { @@ -38,11 +48,20 @@ namespace augs { yojimbo_log_level(YOJIMBO_LOG_LEVEL_INFO); yojimbo_set_printf_function(custom_print); yojimbo_set_assert_function(log_ensure); + +#if BUILD_WEBRTC + rtc::InitLogger(rtc::LogLevel::Info, rtc_log_callback); + rtc::Preload(); +#endif + return result; } bool deinit() { LOG("Shutting down the network library."); +#if BUILD_WEBRTC + rtc::Cleanup(); +#endif ShutdownYojimbo(); return true; } diff --git a/src/work.cpp b/src/work.cpp index 1f917dbef..6c608e10e 100644 --- a/src/work.cpp +++ b/src/work.cpp @@ -163,6 +163,10 @@ #include "application/setups/editor/packaged_official_content.h" #include "augs/string/parse_url.h" +#if BUILD_WEBRTC +#include "rtc/rtc.hpp" +#endif + #include "work_result.h" namespace augs {