From 5541f9dca80b7fd66f2862f537b0ff926cd6f9fd Mon Sep 17 00:00:00 2001 From: loki Date: Sat, 14 Dec 2019 16:47:17 +0100 Subject: [PATCH] Fix bug causing a crash when changing resolution of the monitor while in a session --- sunshine/platform/linux.cpp | 7 ++++++- sunshine/video.cpp | 28 ++++++++++++++++++---------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/sunshine/platform/linux.cpp b/sunshine/platform/linux.cpp index 5d069bb5..343d08e0 100644 --- a/sunshine/platform/linux.cpp +++ b/sunshine/platform/linux.cpp @@ -86,13 +86,17 @@ std::string get_local_ip() { return get_local_ip(AF_INET); } struct display_attr_t { display_attr_t() : display { XOpenDisplay(nullptr) }, window { DefaultRootWindow(display) }, attr {} { - XGetWindowAttributes(display, window, &attr); + refresh(); } ~display_attr_t() { XCloseDisplay(display); } + void refresh() { + XGetWindowAttributes(display, window, &attr); + } + Display *display; Window window; XWindowAttributes attr; @@ -110,6 +114,7 @@ display_t display() { img_t snapshot(display_t &display_void) { auto &display = *((display_attr_t*)display_void.get()); + display.refresh(); XImage *img { XGetImage( display.display, display.window, diff --git a/sunshine/video.cpp b/sunshine/video.cpp index 4f2042e3..7dc356fd 100644 --- a/sunshine/video.cpp +++ b/sunshine/video.cpp @@ -125,7 +125,6 @@ void encodeThread( ctx->flags |= (AV_CODEC_FLAG_CLOSED_GOP | AV_CODEC_FLAG_LOW_DELAY); ctx->flags2 |= AV_CODEC_FLAG2_FAST; - auto fromformat = AV_PIX_FMT_BGR0; auto lg = open_codec(ctx, codec, &options); yuv_frame->format = ctx->pix_fmt; @@ -136,18 +135,27 @@ void encodeThread( int64_t frame = 1; + auto img_width = 0; + auto img_height = 0; + // Initiate scaling context with correct height and width sws_t sws; - if(auto img = images->pop()) { - sws.reset( - sws_getContext( - platf::img_width(img), platf::img_height(img), fromformat, - ctx->width, ctx->height, ctx->pix_fmt, - SWS_LANCZOS | SWS_ACCURATE_RND, - nullptr, nullptr, nullptr)); - } - while (auto img = images->pop()) { + auto new_width = platf::img_width(img); + auto new_height = platf::img_height(img); + + if(img_width != new_width || img_height != new_height) { + img_width = new_width; + img_height = new_height; + + sws.reset( + sws_getContext( + img_width, img_height, AV_PIX_FMT_BGR0, + ctx->width, ctx->height, ctx->pix_fmt, + SWS_LANCZOS | SWS_ACCURATE_RND, + nullptr, nullptr, nullptr)); + } + if(idr_events->peek()) { yuv_frame->pict_type = AV_PICTURE_TYPE_I; frame = idr_events->pop()->first;