From c79bde66fc3afcc436711bf6979486fed982ce2d Mon Sep 17 00:00:00 2001 From: Daniil Vinogradov Date: Thu, 18 Jan 2024 20:08:00 +0100 Subject: [PATCH] Code improvements --- .idea/.name | 2 +- .vscode/c_cpp_properties.json | 4 +- CMakeLists.txt | 16 +++--- README.md | 48 +++++++++++------ .../streaming/ffmpeg/FFmpegVideoDecoder.cpp | 52 +++++++++---------- .../streaming/ffmpeg/FFmpegVideoDecoder.hpp | 6 ++- extern/cmake/toolchain.cmake | 1 + 7 files changed, 75 insertions(+), 54 deletions(-) diff --git a/.idea/.name b/.idea/.name index 570d070..9d9b9a7 100644 --- a/.idea/.name +++ b/.idea/.name @@ -1 +1 @@ -borealis_demo \ No newline at end of file +Moonlight \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 125146f..963e4d6 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -26,8 +26,8 @@ ], "defines": [ "__SWITCH__", - "USE_DEKO3D", - "BOREALIS_USE_DEKO3D" + // "USE_DEKO3D", + // "BOREALIS_USE_DEKO3D" ], "macFrameworkPath": [ "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks" diff --git a/CMakeLists.txt b/CMakeLists.txt index e997f4a..a7ad875 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -153,6 +153,8 @@ elseif (PLATFORM_IOS) "${CMAKE_CURRENT_SOURCE_DIR}/app/ios/iOSBundleInfo.plist.in" "borealis" "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_ALTER}") + set_target_properties(${PROJECT_NAME} PROPERTIES + XCODE_EMBED_FRAMEWORKS_CODE_SIGN_ON_COPY "${CMAKE_CURRENT_SOURCE_DIR}/extern/borealis/library/lib/extern/angle/MetalANGLE.framework") endif () target_include_directories(${PROJECT_NAME} PUBLIC @@ -180,14 +182,16 @@ set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "${EXTERN_PATH}/cmake") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${EXTERN_PATH}/cmake") #find_package(PkgConfig REQUIRED) -find_package(CURL CONFIG REQUIRED) +find_package(CURL REQUIRED) find_package(mbedTLS REQUIRED) find_package(SDL2 REQUIRED) find_package(Jansson REQUIRED) find_package(EXPAT REQUIRED) -find_package(Opus CONFIG REQUIRED) find_package(PNG REQUIRED) +find_library(OPUS_LIBRARY opus) +message("opus: ${OPUS_LIBRARY}") + find_library(AVCODEC_LIBRARY avcodec) message("avcodec: ${AVCODEC_LIBRARY}") @@ -200,9 +204,6 @@ message("avutil: ${AVUTIL_LIBRARY}") find_library(SWRESAMPLE_LIBRARY swresample) message("swresample: ${SWRESAMPLE_LIBRARY}") -target_include_directories(moonlight-common-c PUBLIC - ${MBEDTLS_INCLUDE}) - target_link_libraries(${PROJECT_NAME} PRIVATE borealis moonlight-common-c @@ -216,11 +217,12 @@ target_link_libraries(${PROJECT_NAME} PRIVATE ${AVUTIL_LIBRARY} ${AVFORMAT_LIBRARY} ${SWRESAMPLE_LIBRARY} - Opus::opus + ${OPUS_LIBRARY} PNG::PNG SDL2::SDL2 ${APP_PLATFORM_LIB}) if (APPLE) - target_link_libraries(${PROJECT_NAME} PRIVATE "-framework CoreMedia" "-framework CoreVideo" "-framework VideoToolbox" "-framework AudioToolbox") + set(XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC OFF) + target_link_libraries(${PROJECT_NAME} PRIVATE "-framework CoreMedia" "-framework VideoToolbox") endif () \ No newline at end of file diff --git a/README.md b/README.md index 3539706..cd47590 100644 --- a/README.md +++ b/README.md @@ -14,20 +14,13 @@ Moonlight-Switch is a port of [Moonlight Game Streaming Project](https://github.

-# If you cannot use bitrate higher than 10 Mbps - read this! -To be able to use any streaming setting higher than 720p - resolution, 10MBbps - Bitrate, you need to overclock CPU of your console. - -This leads from lack of access to GPU decoder and because of that all decoding performs on CPU, while it is not powerfull enough to handle it. +# How to reach higher bitrate +To be able to use high bitrate setting especially with 1080p - resolution, you need to overclock CPU/GPU of your console. To learn more about that you can take a look at [Sys-Clk homebrew](https://github.com/retronx-team/sys-clk) or entire [Atmosphere build - 4IFIR](https://github.com/rashevskyv/4IFIR/blob/main/README_ENG.md) which includes everything you need to overclock your console I DO NOT RESPOSIBLE FOR ANY DAMAGE TO YOUR CONSOLE IF ANYTHING WILL GO WRONG! I am using 4IFIR by myself and not find any issue, but everything possible. So think by you own head and be responsible for what you do with your devices! -# Attention for developers -If you know how to debug Nintendo Switch Homebrew applications please let me know! I have no idea how to do this, I'm just an iOS developer who wants "click-click, UI debugger here we go" and not this GDB and Coredump stuff... - -Jokes aside, I seriously couldn't find any usefull information about it, all crashes shows ?? instead of function names and I cannot understand what I do wrong. - # Installing 1. Download latest Moonlight-Switch [release](https://github.com/XITRIX/Moonlight-Switch/releases). 2. Put Moonlight-Switch.nro to sdcard:/switch/Moonlight-Switch; @@ -91,13 +84,36 @@ ATTENTION! Currently there is no way to select language inside of app, it takes 1. Clone this repo with submodules by `git clone https://github.com/XITRIX/Moonlight-Switch.git --recursive` 2. `cd` into folder -## Switch: -3. Install core Switch packages `dkp-pacman -Suy switch-dev` -4. Install other packages `dkp-pacman -Suy switch-ffmpeg switch-mbedtls switch-opusfile switch-sdl2 switch-curl switch-libexpat switch-jansson switch-glfw switch-glm switch-libvpx switch-glad` -5. Configure by `cmake -B build_switch -DPLATFORM_SWITCH=ON` -6. Build by `make -C build_switch borealis_demo.nro -j$(nproc)` -7. Moonlight-Switch.nro should be created. If it doesn't work, try to install missing packages -8. If it still doesn't work, god bless you! +```bash +cd 'folder/to/store/the/sources' +git clone https://github.com/XITRIX/Moonlight-Switch.git --recursive +cd Moonlight-Switch +``` + +## Switch + +To build for Switch, a standard development environment must first be set up. In order to do so, [refer to the Getting Started guide](https://devkitpro.org/wiki/Getting_Started). + +```bash +cmake -B build_switch -DPLATFORM_SWITCH=ON -DUSE_SDL2=ON +make -C build_switch Moonlight-Switch.nro -j$(nproc) +``` + +## PC (Windows/Linux/MacOS) + +To build for PC, the following components are required: + +- cmake/make build system +- A C++ compiler supporting the C++17 standard + +Please refer to the usual sources of information for your particular operating system. Usually the commands needed to build this project will look like this: + +```bash +cmake -B build_pc -DPLATFORM_DESKTOP=ON -DCMAKE_BUILD_TYPE=Release +make -C build_pc -j$(nproc) +``` + +Also, please note that the `resources` folder must be available in the working directory, otherwise the program will fail to find the shaders. ## iOS: diff --git a/app/src/streaming/ffmpeg/FFmpegVideoDecoder.cpp b/app/src/streaming/ffmpeg/FFmpegVideoDecoder.cpp index bef75de..964edfb 100644 --- a/app/src/streaming/ffmpeg/FFmpegVideoDecoder.cpp +++ b/app/src/streaming/ffmpeg/FFmpegVideoDecoder.cpp @@ -99,16 +99,16 @@ int FFmpegVideoDecoder::setup(int video_format, int width, int height, return err; } - m_frames_count = 2; - m_frames = (AVFrame**)malloc(m_frames_count * sizeof(AVFrame*)); - if (m_frames == NULL) { - brls::Logger::error("FFmpeg: Couldn't allocate frames"); - return -1; - } +// m_frames = (AVFrame**)malloc(m_frames_count * sizeof(AVFrame*)); +// if (m_frames == NULL) { +// brls::Logger::error("FFmpeg: Couldn't allocate frames"); +// return -1; +// } tmp_frame = av_frame_alloc(); for (int i = 0; i < m_frames_count; i++) { - m_frames[i] = av_frame_alloc(); + m_extra_frames[i] = av_frame_alloc(); + m_frames[i] = m_extra_frames[i]; if (m_frames[i] == NULL) { brls::Logger::error("FFmpeg: Couldn't allocate frame"); return -1; @@ -116,24 +116,24 @@ int FFmpegVideoDecoder::setup(int video_format, int width, int height, #ifdef __SWITCH__ #ifndef BOREALIS_USE_DEKO3D - m_frames[i]->format = AV_PIX_FMT_YUV420P; + m_frames[i]->format = AV_PIX_FMT_YUV420P; #endif #else // m_frames[i]->format = AV_PIX_FMT_VIDEOTOOLBOX; - m_frames[i]->format = AV_PIX_FMT_YUV420P; + m_frames[i]->format = AV_PIX_FMT_YUV420P; #endif - m_frames[i]->width = width; - m_frames[i]->height = height; + m_frames[i]->width = width; + m_frames[i]->height = height; #ifndef BOREALIS_USE_DEKO3D - int err = av_frame_get_buffer(m_frames[i], 256); - if (err < 0) { - char errs[64]; - brls::Logger::error("FFmpeg: Couldn't allocate frame buffer: {}", av_make_error_string(errs, 64, err)); - return -1; - } +// int err = av_frame_get_buffer(m_frames[i], 256); +// if (err < 0) { +// char errs[64]; +// brls::Logger::error("FFmpeg: Couldn't allocate frame buffer: {}", av_make_error_string(errs, 64, err)); +// return -1; +// } #endif - } + } m_ffmpeg_buffer = (char*)malloc(DECODER_BUFFER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE); @@ -179,15 +179,15 @@ void FFmpegVideoDecoder::cleanup() { m_decoder_context = NULL; } - if (m_frames) { - for (int i = 0; i < m_frames_count; i++) { - if (m_frames[i]) - av_frame_free(&m_frames[i]); - } +// if (m_frames) { + for (int i = 0; i < m_frames_count; i++) { + // if (m_extra_frames[i]) + av_frame_free(&m_extra_frames[i]); + } - free(m_frames); - m_frames = nullptr; - } +// free(m_frames); +// m_frames = nullptr; +// } if (tmp_frame) { av_frame_free(&tmp_frame); diff --git a/app/src/streaming/ffmpeg/FFmpegVideoDecoder.hpp b/app/src/streaming/ffmpeg/FFmpegVideoDecoder.hpp index 550f2e4..9e67a31 100644 --- a/app/src/streaming/ffmpeg/FFmpegVideoDecoder.hpp +++ b/app/src/streaming/ffmpeg/FFmpegVideoDecoder.hpp @@ -1,6 +1,8 @@ #include "IFFmpegVideoDecoder.hpp" #pragma once +#define m_frames_count 2 + class FFmpegVideoDecoder : public IFFmpegVideoDecoder { public: FFmpegVideoDecoder(); @@ -21,13 +23,13 @@ class FFmpegVideoDecoder : public IFFmpegVideoDecoder { AVBufferRef *hw_device_ctx = nullptr; const AVCodec* m_decoder = nullptr; AVCodecContext* m_decoder_context = nullptr; - AVFrame** m_frames = nullptr; AVFrame *tmp_frame = nullptr; + AVFrame* m_extra_frames[m_frames_count]; + AVFrame* m_frames[m_frames_count]; int m_stream_fps = 0; int m_frames_in = 0; int m_frames_out = 0; - int m_frames_count = 0; int m_current_frame = 0, m_next_frame = 0; uint32_t m_last_frame = 0; diff --git a/extern/cmake/toolchain.cmake b/extern/cmake/toolchain.cmake index 86afc05..1efd899 100644 --- a/extern/cmake/toolchain.cmake +++ b/extern/cmake/toolchain.cmake @@ -15,6 +15,7 @@ if (PLATFORM_DESKTOP) set(CMAKE_TOOLCHAIN_FILE ${EXTERN_PATH}/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE PATH "vcpkg toolchain file") set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -g2 -ggdb -Wall") set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall") + set(USE_SDL2 ON) elseif (PLATFORM_IOS) message(STATUS "building for iOS") set(ENABLE_STRICT_TRY_COMPILE ON)