Skip to content

Commit

Permalink
Mitigate corruption, ask for an IDR every X frames
Browse files Browse the repository at this point in the history
To mitigate TheElixZammuto#117 (Compression artifacts forming when left on the same scene for a while), request an IDR frame from the host every 1000 packets. This seems to reset the decoder well enough to completely avoid slow corruption problem, but shouldn't add too much extra bandwidth.

Unless I'm missing something, Sunshine never sends I or IDR frames after initially establishing the stream unless it's specifically asked to by the client.
  • Loading branch information
BradyBrenot committed Aug 8, 2024
1 parent 2f5289c commit bdffb80
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Streaming/FFmpegDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ extern "C" {
#include<libswscale/swscale.h>
#include<libavutil/hwcontext_d3d11va.h>
}

#define DECODER_BUFFER_SIZE 1048576
#define MAX_DELAY_BETWEEN_IDR_FRAMES 1000

namespace moonlight_xbox_dx {

Expand Down Expand Up @@ -163,6 +165,9 @@ namespace moonlight_xbox_dx {
Utils::Log("Using hack for Xbox One Consoles");
hackWait = true;
}

framesSinceLastIDR = 0;

return 0;
}

Expand Down Expand Up @@ -205,12 +210,21 @@ namespace moonlight_xbox_dx {
entry = entry->next;
}
int err;

err = Decode(ffmpeg_buffer, length);
if (err < 0) {
LiCompleteVideoFrame(frameHandle, DR_NEED_IDR);
return false;
}

LiCompleteVideoFrame(frameHandle, DR_OK);

if (framesSinceLastIDR++ >= MAX_DELAY_BETWEEN_IDR_FRAMES) {
// This mitigates the issue where the video slowly gets more corrupt
LiRequestIdrFrame();
framesSinceLastIDR = 0;
}

return true;
}

Expand Down
1 change: 1 addition & 0 deletions Streaming/FFmpegDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace moonlight_xbox_dx
AVFrame** dec_frames;
AVFrame** ready_frames;
int next_frame, current_frame;
int framesSinceLastIDR;
std::shared_ptr<DX::DeviceResources> resources;
};
}

0 comments on commit bdffb80

Please sign in to comment.