From 43dc7cf7c0a2a8acc177dde9691d2c6c937fc5a3 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Fri, 9 Jul 2021 19:53:52 -0500 Subject: [PATCH] Fix video bitstream corruption when matching replacement isn't found NVENC doesn't always include the HEVC bitstream prefix that Sunshine is looking to patch. When this happens replace() appends a spurious 00 00 00 01 28 NALU prefix to the end of the bitstream rather than simply doing nothing. This causes varying degrees of malfunctioning depending on the client, with the worst being complete video corruption on iOS. --- sunshine/stream.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sunshine/stream.cpp b/sunshine/stream.cpp index aae647fe..27725272 100644 --- a/sunshine/stream.cpp +++ b/sunshine/stream.cpp @@ -410,11 +410,14 @@ std::vector replace(const std::string_view &original, const std::string std::vector replaced; auto begin = std::begin(original); - auto next = std::search(begin, std::end(original), std::begin(old), std::end(old)); + auto end = std::end(original); + auto next = std::search(begin, end, std::begin(old), std::end(old)); std::copy(begin, next, std::back_inserter(replaced)); - std::copy(std::begin(_new), std::end(_new), std::back_inserter(replaced)); - std::copy(next + old.size(), std::end(original), std::back_inserter(replaced)); + if(next != end) { + std::copy(std::begin(_new), std::end(_new), std::back_inserter(replaced)); + std::copy(next + old.size(), end, std::back_inserter(replaced)); + } return replaced; }