Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Jan 18, 2024
1 parent e63803c commit c79bde6
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 9 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}")

Expand All @@ -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
Expand All @@ -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 ()
48 changes: 32 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,13 @@ Moonlight-Switch is a port of [Moonlight Game Streaming Project](https://github.
</p>
</details>

# 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;
Expand Down Expand Up @@ -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:

Expand Down
52 changes: 26 additions & 26 deletions app/src/streaming/ffmpeg/FFmpegVideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,41 +99,41 @@ 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;
}

#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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions app/src/streaming/ffmpeg/FFmpegVideoDecoder.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "IFFmpegVideoDecoder.hpp"
#pragma once

#define m_frames_count 2

class FFmpegVideoDecoder : public IFFmpegVideoDecoder {
public:
FFmpegVideoDecoder();
Expand All @@ -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;

Expand Down
1 change: 1 addition & 0 deletions extern/cmake/toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c79bde6

Please sign in to comment.