From 4478fd2a14e47bb8534d37ea88df7837174e70ce Mon Sep 17 00:00:00 2001 From: Nightmare Date: Tue, 15 Jul 2025 02:33:36 +0800 Subject: [PATCH] build(android): support android platform (config and logging) (#3741) --- src/config.cpp | 7 ++++-- src/logging.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index 6d999719..c1dba388 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -32,7 +32,7 @@ #include #endif -#ifndef __APPLE__ +#if !defined(__ANDROID__) && !defined(__APPLE__) // For NVENC legacy constants #include #endif @@ -1038,9 +1038,12 @@ namespace config { } void apply_config(std::unordered_map &&vars) { +#ifndef __ANDROID__ + // TODO: Android can possibly support this if (!fs::exists(stream.file_apps.c_str())) { fs::copy_file(SUNSHINE_ASSETS_DIR "/apps.json", stream.file_apps); } +#endif for (auto &[name, val] : vars) { BOOST_LOG(info) << "config: '"sv << name << "' = "sv << val; @@ -1066,7 +1069,7 @@ namespace config { bool_f(vars, "nvenc_opengl_vulkan_on_dxgi", video.nv_opengl_vulkan_on_dxgi); bool_f(vars, "nvenc_latency_over_power", video.nv_sunshine_high_power_mode); -#ifndef __APPLE__ +#if !defined(__ANDROID__) && !defined(__APPLE__) video.nv_legacy.preset = video.nv.quality_preset + 11; video.nv_legacy.multipass = video.nv.two_pass == nvenc::nvenc_two_pass::quarter_resolution ? NV_ENC_TWO_PASS_QUARTER_RESOLUTION : video.nv.two_pass == nvenc::nvenc_two_pass::full_resolution ? NV_ENC_TWO_PASS_FULL_RESOLUTION : diff --git a/src/logging.cpp b/src/logging.cpp index 1913eef1..d52e255b 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -15,11 +15,17 @@ #include #include #include -#include // local includes #include "logging.h" +// conditional includes +#ifdef __ANDROID__ + #include +#else + #include +#endif + extern "C" { #include } @@ -97,6 +103,48 @@ namespace logging { os << "["sv << std::put_time(<, "%Y-%m-%d %H:%M:%S.") << boost::format("%03u") % ms.count() << "]: "sv << log_type << view.attribute_values()[message].extract(); } +#ifdef __ANDROID__ + namespace sinks = boost::log::sinks; + namespace expr = boost::log::expressions; + + void android_log(const std::string &message, int severity) { + android_LogPriority android_priority; + switch (severity) { + case 0: + android_priority = ANDROID_LOG_VERBOSE; + break; + case 1: + android_priority = ANDROID_LOG_DEBUG; + break; + case 2: + android_priority = ANDROID_LOG_INFO; + break; + case 3: + android_priority = ANDROID_LOG_WARN; + break; + case 4: + android_priority = ANDROID_LOG_ERROR; + break; + case 5: + android_priority = ANDROID_LOG_FATAL; + break; + default: + android_priority = ANDROID_LOG_UNKNOWN; + break; + } + __android_log_print(android_priority, "Sunshine", "%s", message.c_str()); + } + + // custom sink backend for android + struct android_sink_backend: public sinks::basic_sink_backend { + void consume(const bl::record_view &rec) { + int log_sev = rec[severity].get(); + const std::string log_msg = rec[expr::smessage].get(); + // log to android + android_log(log_msg, log_sev); + } + }; +#endif [[nodiscard]] std::unique_ptr init(int min_log_level, const std::string &log_file) { if (sink) { @@ -104,8 +152,10 @@ namespace logging { deinit(); } +#ifndef __ANDROID__ setup_av_logging(min_log_level); setup_libdisplaydevice_logging(min_log_level); +#endif sink = boost::make_shared(); @@ -113,6 +163,7 @@ namespace logging { boost::shared_ptr stream {&std::cout, boost::null_deleter()}; sink->locked_backend()->add_stream(stream); #endif + sink->locked_backend()->add_stream(boost::make_shared(log_file)); sink->set_filter(severity >= min_log_level); sink->set_formatter(&formatter); @@ -122,9 +173,15 @@ namespace logging { sink->locked_backend()->auto_flush(true); bl::core::get()->add_sink(sink); + +#ifdef __ANDROID__ + auto android_sink = boost::make_shared>(); + bl::core::get()->add_sink(android_sink); +#endif return std::make_unique(); } +#ifndef __ANDROID__ void setup_av_logging(int min_log_level) { if (min_log_level >= 1) { av_log_set_level(AV_LOG_QUIET); @@ -182,6 +239,7 @@ namespace logging { } }); } +#endif void log_flush() { if (sink) {