From bc52fe9b82e163474d9d860210531931c058f4d2 Mon Sep 17 00:00:00 2001 From: loki Date: Fri, 23 Jul 2021 17:36:32 +0200 Subject: [PATCH 1/2] Improve fps for software encoding on Windows --- sunshine/video.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sunshine/video.cpp b/sunshine/video.cpp index 9d52a0b8..29e6d9ee 100644 --- a/sunshine/video.cpp +++ b/sunshine/video.cpp @@ -1038,8 +1038,7 @@ void encode_run( std::this_thread::sleep_until(next_frame); next_frame += delay; - // When Moonlight request an IDR frame, send frames even if there is no new captured frame - if(!frame->key_frame || images->peek()) { + if(images->peek()) { if(auto img = images->pop(delay)) { session->device->convert(*img); } From 2a5fd78789bd3eb8782e7d21f9072a1bfc554acf Mon Sep 17 00:00:00 2001 From: loki Date: Fri, 23 Jul 2021 18:43:35 +0200 Subject: [PATCH 2/2] Fix crackling audio on Linux --- sunshine/audio.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/sunshine/audio.cpp b/sunshine/audio.cpp index 437703ba..cb4fac28 100644 --- a/sunshine/audio.cpp +++ b/sunshine/audio.cpp @@ -87,11 +87,14 @@ void encodeThread(sample_queue_t samples, config_t config, void *channel_data) { OPUS_APPLICATION_AUDIO, nullptr) }; - opus_multistream_encoder_ctl(opus.get(), OPUS_SET_VBR(0)); + // For some reason, audio is crackling when the encoder is set to constant bitstream. + // We simulate a constant bitstream with OPUS_SET_BITRATE(OPUS_BITRATE_MAX) --> + // which tries to occupy as much space as possible in the packet + opus_multistream_encoder_ctl(opus.get(), OPUS_SET_BITRATE(OPUS_BITRATE_MAX)); auto frame_size = config.packetDuration * stream->sampleRate / 1000; while(auto sample = samples->pop()) { - buffer_t packet { 1024 }; // 1KB + buffer_t packet { 1400 }; // 1KB int bytes = opus_multistream_encode(opus.get(), sample->data(), frame_size, std::begin(packet), packet.size()); if(bytes < 0) { @@ -101,6 +104,14 @@ void encodeThread(sample_queue_t samples, config_t config, void *channel_data) { return; } + // Even with OPUS_SET_BITRATE(OPUS_BITRATE_MAX), silent packets are smaller than the rest + // Drop silent packets to ensure Moonlight won't complain + // A packet size of 128 seems a reasonable enough threshold + if(bytes < 128) { + BOOST_LOG(verbose) << "Dropped silent packet"sv; + continue; + } + packet.fake_resize(bytes); packets->raise(channel_data, std::move(packet)); }