Skip to content

Commit

Permalink
idk just tried of this tearing problem
Browse files Browse the repository at this point in the history
  • Loading branch information
TheElixZammuto committed Mar 21, 2023
1 parent 9997ae4 commit b341e56
Show file tree
Hide file tree
Showing 13 changed files with 1,577 additions and 115 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,5 @@ ASALocalRun/
# BeatPulse healthcheck temp database
healthchecksdb
/libgamestream/build
vcpkg_installed
vcpkg_old
5 changes: 4 additions & 1 deletion Common/DeviceResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "DirectXHelper.h"
#include <windows.ui.xaml.media.dxinterop.h>
#include <Pages/StreamPage.xaml.h>
#include <Streaming/FFmpegDecoder.h>

using namespace moonlight_xbox_dx;
using namespace D2D1;
Expand Down Expand Up @@ -644,7 +645,7 @@ void DX::DeviceResources::Present()
// to sleep until the next VSync. This ensures we don't waste any cycles rendering
// frames that will never be displayed to the screen.
DXGI_PRESENT_PARAMETERS parameters = { 0 };
HRESULT hr = m_swapChain->Present1(1, 0, &parameters);
HRESULT hr = m_swapChain->Present1(0, 0, &parameters);

// Discard the contents of the render target.
// This is a valid operation only when the existing contents will be entirely
Expand All @@ -664,6 +665,8 @@ void DX::DeviceResources::Present()
{
DX::ThrowIfFailed(hr);
}
if(moonlight_xbox_dx::FFMpegDecoder::getInstance() != nullptr && moonlight_xbox_dx::FFMpegDecoder::getInstance()->shouldUnlock)
moonlight_xbox_dx::FFMpegDecoder::getInstance()->mutex.unlock();
}

// This method determines the rotation between the display device's native orientation and the
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# moonlight-xbox
A port of [Moonlight Stream](https://moonlight-stream.org/) for playing games using GeForce Experience or [Sunshine](https://github.com/loki-47-6F-64/sunshine) for the Xbox One and Xbox Series X|S family of consoles
A port of [Moonlight Stream](https://moonlight-stream.org/) for playing games using GeForce Experience or [Sunshine](https://github.com/LizardByte/sunshine) for the Xbox One and Xbox Series X|S family of consoles

**This application is still in early stages of development. Expect things to not work or working badly**

Expand Down Expand Up @@ -36,7 +36,6 @@ A port of [Moonlight Stream](https://moonlight-stream.org/) for playing games us

## What does NOT work
- Other means of input (e.g. Hardware Mouse and Keyboard)
- 4K Support (Pending)
- 120FPS
- HDR (Probably not possible with the Xbox UWP Platform)
- Everything else not listed above
Expand All @@ -53,10 +52,9 @@ A port of [Moonlight Stream](https://moonlight-stream.org/) for playing games us
1. Clone this repository (`moonlight-xbox`) with submodules enabled!
2. Install [VCPKG](https://vcpkg.io/en/index.html) and all dependencies:
1. Clone VCPKG (`git clone https://github.com/Microsoft/vcpkg.git`) into `moonlight-xbox/vcpkg`
2. Run `vcpkg/bootstrap-vcpkg.bat`
3. Hack `ffmpeg` port by adding `set(OPTIONS "${OPTIONS} --enable-d3d11va")` to `vcpkg/ports/ffmpeg/portfile.cmake`
4. Install dependencies: `vcpkg install pthread:x64-uwp pthreads:x64-uwp curl:x64-uwp openssl:x64-uwp expat:x64-uwp zlib:x64-uwp ffmpeg[avcodec,avdevice,avfilter,avformat,core,gpl,postproc,swresample,swscale]:x64-uwp nlohmann-json:x64-uwp bzip2:x64-uwp brotli:x64-uwp x264:x64-uwp freetype:x64-uwp opus:x64-uwp`
2. Run `vcpkg\bootstrap-vcpkg.bat`
4. Install dependencies: `.\vcpkg\vcpkg.exe install --triplet x64-uwp`
3. Run x64 Visual Studio Prompt (Tools → Command Line → Developer Command Prompt)
1. Run `generate-thirdparty-projects.bat` to generate `moonlight-common-c` VS project
2. Go to `libgamestream` and run `build-uwp.bat` to generate `libgamestream` VS project
4. After all the actions above, you finnally can open and build solution. Please, build it only in Release mode (Debug mode is broken)
4. After all the actions above, you finally can open and build solution.
58 changes: 53 additions & 5 deletions Streaming/FFmpegDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,32 @@ extern "C" {
}
#define DECODER_BUFFER_SIZE 1048576
namespace moonlight_xbox_dx {

void lock_context(void* dec) {
Utils::Log("Lock");
auto ff = (FFMpegDecoder*)dec;
ff->mutex.lock();
}

void unlock_context(void* dec) {
Utils::Log("Unlock");
auto ff = (FFMpegDecoder*)dec;
ff->mutex.unlock();
}

void ffmpeg_log_callback(void* avcl,int level,const char* fmt,va_list vl) {
/*if (level > AV_LOG_INFO)return;
//if (level > AV_LOG_INFO)return;
char message[2048];
vsprintf_s(message, fmt, vl);
Utils::Log(message);*/
OutputDebugStringA("[FFMPEG]");
OutputDebugStringA(message);
}

int FFMpegDecoder::Init(int videoFormat, int width, int height, int redrawRate, void* context, int drFlags) {
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58,10,100)
avcodec_register_all();
#endif

av_log_set_level(AV_LOG_VERBOSE);
av_log_set_callback(&ffmpeg_log_callback);
#pragma warning(suppress : 4996)
av_init_packet(&pkt);
Expand Down Expand Up @@ -65,6 +78,9 @@ namespace moonlight_xbox_dx {
d3d11va_device_ctx = reinterpret_cast<AVD3D11VADeviceContext*>(device_ctx->hwctx);
d3d11va_device_ctx->device = this->resources->GetD3DDevice();
d3d11va_device_ctx->device_context = this->resources->GetD3DDeviceContext();
d3d11va_device_ctx->lock = lock_context;
d3d11va_device_ctx->unlock = unlock_context;
d3d11va_device_ctx->lock_ctx = this;
int err2;
if ((err2 = av_hwdevice_ctx_init(hw_device_ctx)) < 0) {
char msg[2048];
Expand All @@ -73,12 +89,44 @@ namespace moonlight_xbox_dx {
return err2;

}
auto hw_frames_ctx = av_hwframe_ctx_alloc(hw_device_ctx);
if (!hw_frames_ctx) {
return 1;
}

AVHWFramesContext* framesContext = (AVHWFramesContext*)hw_frames_ctx->data;

// We require NV12 or P010 textures for our shader
framesContext->format = AV_PIX_FMT_D3D11;
framesContext->sw_format = AV_PIX_FMT_NV12;

framesContext->width = FFALIGN(width, 16);
framesContext->height = FFALIGN(height, 16);

// We can have up to 16 reference frames plus a working surface
framesContext->initial_pool_size = 2;

AVD3D11VAFramesContext* d3d11vaFramesContext = (AVD3D11VAFramesContext*)framesContext->hwctx;

d3d11vaFramesContext->texture = NULL;
d3d11vaFramesContext->BindFlags = D3D11_BIND_DECODER;

int err = av_hwframe_ctx_init(hw_frames_ctx);
if (err < 0) {
return -1;
}

auto a = d3d11vaFramesContext->texture_infos != nullptr;
if (a) {
Utils::Log("B");
}
decoder_ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
decoder_ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ctx);

decoder_ctx->width = width;
decoder_ctx->height = height;

int err = avcodec_open2(decoder_ctx, decoder, NULL);
err = avcodec_open2(decoder_ctx, decoder, NULL);
if (err < 0) {
char msg[2048];
sprintf(msg, "Failed to create FFMpeg Codec: %d\n", err);
Expand Down Expand Up @@ -193,7 +241,7 @@ namespace moonlight_xbox_dx {
}
return nullptr;
}

//Helpers
FFMpegDecoder *instance;

Expand Down
4 changes: 3 additions & 1 deletion Streaming/FFmpegDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ namespace moonlight_xbox_dx
static FFMpegDecoder* getInstance();
static DECODER_RENDERER_CALLBACKS getDecoder();
static FFMpegDecoder* createDecoderInstance(std::shared_ptr<DX::DeviceResources> resources);
std::recursive_mutex mutex;
bool shouldUnlock = false;
private:
int Decode(unsigned char* indata, int inlen);
AVPacket pkt;
AVCodec* decoder;
const AVCodec* decoder;
AVCodecContext* decoder_ctx;
AVHWDeviceContext* device_ctx;
AVD3D11VADeviceContext* d3d11va_device_ctx;
Expand Down
5 changes: 4 additions & 1 deletion Streaming/VideoRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ void VideoRenderer::Render()
//Create a rendering texture
LARGE_INTEGER start, end,frequency;
QueryPerformanceCounter(&start);
FFMpegDecoder::getInstance()->shouldUnlock = false;
FFMpegDecoder::getInstance()->SubmitDU();
AVFrame *frame = FFMpegDecoder::getInstance()->GetFrame();
if (frame == nullptr)return;
FFMpegDecoder::getInstance()->mutex.lock();
FFMpegDecoder::getInstance()->shouldUnlock = true;
ID3D11Texture2D* ffmpegTexture = (ID3D11Texture2D*)(frame->data[0]);
D3D11_TEXTURE2D_DESC ffmpegDesc;
ffmpegTexture->GetDesc(&ffmpegDesc);
Expand All @@ -66,7 +69,7 @@ void VideoRenderer::Render()
box.bottom = min(renderTextureDesc.Height, ffmpegDesc.Height);
box.front = 0;
box.back = 1;
if(renderTexture != NULL)renderTexture->Release();
Microsoft::WRL::ComPtr<ID3D11Texture2D> renderTexture;
DX::ThrowIfFailed(m_deviceResources->GetD3DDevice()->CreateTexture2D(&renderTextureDesc, NULL, renderTexture.GetAddressOf()), "Render Texture Creation");
m_deviceResources->GetD3DDeviceContext()->CopySubresourceRegion(renderTexture.Get(), 0, 0, 0, 0, ffmpegTexture, index, &box);
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_luminance_shader_resource_view;
Expand Down
1 change: 0 additions & 1 deletion Streaming/VideoRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ namespace moonlight_xbox_dx
Microsoft::WRL::ComPtr<ID3D11PixelShader> m_pixelShader;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_constantBuffer;
Microsoft::WRL::ComPtr<ID3D11SamplerState> samplerState;
Microsoft::WRL::ComPtr<ID3D11Texture2D> renderTexture;
D3D11_TEXTURE2D_DESC renderTextureDesc;

// System resources for cube geometry.
Expand Down
4 changes: 2 additions & 2 deletions generate-thirdparty-projects.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cd third_party\moonlight-common-c
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=..\..\vcpkg\scripts\buildsystems\vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-uwp -G "Visual Studio 17 2022" -DCMAKE_SYSTEM_NAME=WindowsStore -DCMAKE_SYSTEM_VERSION="10.0" -DTARGET_UWP=ON
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=..\..\vcpkg\scripts\buildsystems\vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-uwp -G "Visual Studio 17 2022" -DCMAKE_SYSTEM_NAME=WindowsStore -DCMAKE_SYSTEM_VERSION="10.0" -DTARGET_UWP=ON -DVCPKG_MANIFEST_MODE=on -DVCPKG_MANIFEST_DIR=../..
cd ..\..\libgamestream
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=..\vcpkg\scripts\buildsystems\vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-uwp -G "Visual Studio 17 2022" -DCMAKE_SYSTEM_NAME=WindowsStore -DCMAKE_SYSTEM_VERSION="10.0" -DTARGET_UWP=ON
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=..\vcpkg\scripts\buildsystems\vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-uwp -G "Visual Studio 17 2022" -DCMAKE_SYSTEM_NAME=WindowsStore -DCMAKE_SYSTEM_VERSION="10.0" -DTARGET_UWP=ON -DVCPKG_MANIFEST_MODE=on -DVCPKG_MANIFEST_DIR=..
cd ..
2 changes: 1 addition & 1 deletion libgamestream/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ add_library(gamestream STATIC ${GAMESTREAM_SRC_LIST})

target_link_libraries(gamestream moonlight-common-c)

target_include_directories(gamestream PRIVATE ../third_party/moonlight-common-c/src ../third_party/h264bitstream)
target_include_directories(gamestream PRIVATE ${EXPAT_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS} ../third_party/moonlight-common-c/src ../third_party/h264bitstream)
target_link_libraries(gamestream ${CURL_LIBRARIES} ${OPENSSL_LIBRARIES} ${EXPAT_LIBRARIES})
target_link_libraries(gamestream ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS})

Expand Down
1 change: 0 additions & 1 deletion libgamestream/build-uwp.bat

This file was deleted.

76 changes: 5 additions & 71 deletions moonlight-xbox-dx.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,12 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Link>
<AdditionalDependencies>$(ProjectDir)/libgamestream/build/$(Configuration)/gamestream.lib;libexpat.lib;libcurl.lib;libcrypto.lib;libssl.lib;avcodec.lib;avutil.lib;swscale.lib;d2d1.lib; d3d11.lib; dxgi.lib; windowscodecs.lib; dwrite.lib;opus.lib; %(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories);$(ProjectDir)vcpkg\installed\x64-uwp\lib;$(VCInstallDir)\lib\store\amd64;$(VCInstallDir)\lib\amd64</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories);$(ProjectDir)vcpkg_installed\x64-uwp\lib;$(VCInstallDir)\lib\store\amd64;$(VCInstallDir)\lib\amd64</AdditionalLibraryDirectories>
</Link>
<ClCompile>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
<AdditionalIncludeDirectories>$(ProjectDir);$(ProjectDir)vcpkg\installed\x64-uwp\include;$(IntermediateOutputPath);third_party\moonlight-common-c\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir);$(ProjectDir)vcpkg_installed\x64-uwp\include;$(IntermediateOutputPath);third_party\moonlight-common-c\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>4453;28204</DisableSpecificWarnings>
<PreprocessorDefinitions>_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
Expand All @@ -240,12 +240,12 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Link>
<AdditionalDependencies>$(ProjectDir)/libgamestream/build/$(Configuration)/gamestream.lib;libexpat.lib;libcurl.lib;libcrypto.lib;libssl.lib;avcodec.lib;avutil.lib;swscale.lib;d2d1.lib; d3d11.lib; dxgi.lib; windowscodecs.lib; dwrite.lib;opus.lib; %(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories);$(ProjectDir)vcpkg\installed\x64-uwp\lib;$(VCInstallDir)\lib\store\amd64;$(VCInstallDir)\lib\amd64</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories);$(ProjectDir)vcpkg_installed\x64-uwp\lib;$(VCInstallDir)\lib\store\amd64;$(VCInstallDir)\lib\amd64</AdditionalLibraryDirectories>
</Link>
<ClCompile>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
<AdditionalIncludeDirectories>$(ProjectDir);$(ProjectDir)vcpkg\installed\x64-uwp\include;$(IntermediateOutputPath);third_party\moonlight-common-c\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir);$(ProjectDir)vcpkg_installed\x64-uwp\include;$(IntermediateOutputPath);third_party\moonlight-common-c\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>4453;28204</DisableSpecificWarnings>
<PreprocessorDefinitions>_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
Expand Down Expand Up @@ -407,70 +407,7 @@
</ApplicationDefinition>
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\avcodec-58.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\avdevice-58.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\avfilter-7.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\avformat-58.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\avutil-56.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\brotlicommon.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\brotlidec.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\brotlienc.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\bz2.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\freetype.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\libcrypto-1_1-x64.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\libcurl.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\libexpat.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\libpng16.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\libssl-1_1-x64.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\postproc-55.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\pthreadVC3.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\pthreadVCE3.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\pthreadVSE3.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\swresample-3.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\swscale-5.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\zlib1.dll">
<CopyFileToFolders Include="vcpkg_installed\x64-uwp\bin\*">
<FileType>Document</FileType>
</CopyFileToFolders>
</ItemGroup>
Expand All @@ -491,9 +428,6 @@
<DeploymentContent>true</DeploymentContent>
</None>
<None Include="moonlight-xbox-dx_TemporaryKey.pfx" />
<CopyFileToFolders Include="vcpkg\installed\x64-uwp\bin\opus.dll">
<FileType>Document</FileType>
</CopyFileToFolders>
<None Include="packages.config" />
<None Include="README.md" />
</ItemGroup>
Expand Down
Loading

0 comments on commit b341e56

Please sign in to comment.