Closed NebuPookins closed 2 years ago
What audio output device are you using? Is the issue specific to one type of output (ex: Bluetooth vs HDMI vs headphones)?
I know SDL 2.24 had changes to the PulseAudio backend, but I'm not sure how one application would affect everything on the system.
I have two audio devices, and the issue occurs with both of them.
The issue only affects apps that are playing audio on the same device as Moonlight. For example, if YouTube is outputting audio to the Family 17h device, and Moonlight is configured to use the Ellesmere HDMI Audio device, then YouTube's audio will not be distorted. If I switch Moonlight over to use the Family 17h device, YouTube's audio will become distorted. If I switch Moonlight back to Ellesmere HDMI Audio, YouTube's audio will become clear again.
I'm able to reproduce the problem with the following program. Compiling and running this program causes audio in other apps (e.g. Youtube) to become distorted when SDL 2.24.0-1 is installed. There is no audio distortion if 2.0.22-2 is installed.
#include <SDL2/SDL.h>
#include <stdio.h>
int main(int argc, char* args[]) {
int initResult = SDL_Init(SDL_INIT_AUDIO);
if (initResult < 0) {
printf("SDL could not initialize: %s\n", SDL_GetError());
return -1;
}
SDL_AudioSpec want, have;
SDL_zero(want);
want.freq = 48000;
want.format = AUDIO_S16;
want.channels = 2;
want.samples = 240;
SDL_AudioDeviceID dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
printf("Waiting to continue...");
getchar();
SDL_Quit();
}
Because the program to reproduce the issue is so simple, I'm guessing this is a bug in SDL and not Moonlight. I've filed a issue with SDL at https://github.com/libsdl-org/SDL/issues/6121
It looks like the issue is related to the number of samples requested at https://github.com/moonlight-stream/moonlight-qt/blob/master/app/streaming/audio/renderers/sdlaud.cpp#L29-L33
If you request a value that's too low like 240, this does something "globally" which causes audio from other programs to become choppy. If you request a value that's too big like 4096, then audio in other programs are fine, but audio produced by Moonlight (originating from the remote PC) becomes choppy, I guess because whatever logic there is in Moonlight does not fill the buffer quickly enough.
I'm not sure where the value 240 comes from, perhaps https://github.com/moonlight-stream/moonlight-qt/blob/master/app/streaming/audio/audio.cpp#L94
If I set the samples to 480, I get no audio problems in Moonlight nor in other programs, so I've made the following workaround in https://github.com/moonlight-stream/moonlight-qt/blob/master/app/streaming/audio/renderers/sdlaud.cpp#L29-L33
diff --git a/app/streaming/audio/renderers/sdlaud.cpp b/app/streaming/audio/renderers/sdlaud.cpp
index 0d931a43..85da4a6a 100644
--- a/app/streaming/audio/renderers/sdlaud.cpp
+++ b/app/streaming/audio/renderers/sdlaud.cpp
@@ -1,5 +1,5 @@
#include "sdl.h"
-
+#include <algorithm>
#include <Limelight.h>
#include <SDL.h>
@@ -30,7 +30,7 @@ bool SdlAudioRenderer::prepareForPlayback(const OPUS_MULTISTREAM_CONFIGURATION*
// frames contain a non-power of 2 number of samples,
// so the slop would require buffering another full frame.
// Specifying non-Po2 seems to work for our supported platforms.
- want.samples = opusConfig->samplesPerFrame;
+ want.samples = std::max(480, opusConfig->samplesPerFrame);
m_FrameSize = opusConfig->samplesPerFrame * sizeof(short) * opusConfig->channelCount;
I'm not sure if this is a change worth merging in, or if there was a specific reason 480 is "too big" and you really want to stick with 240.
@NebuPookins Huge thanks for your investigation and creating a test case for SDL.
I think I never saw this because I run Fedora which use Pipewire instead of PulseAudio.
Describe the bug
I'm running Archlinux. I performed a system upgrade today, and there were no updates to Moonlight, but there was an update to the SDL2 library. After the update, whenever I connect to another machine via Moonlight, all audio produced by my machine becomes very crackly, including audio produced by other apps. For example, I can have a YouTube video running in the background, and as long as Moonlight isn't connected to another machine , the audio is fine. As soon as I connect to my remote desktop, the audio becomes crackly.
If I rollback the update to the SDL2 library, I am able to use Moonlight just fine with no audio distortion.
I'm not sure if this problem is specific to the way Moonlight is calling the SDL2 APIs. I was not able to reproduce the issue when using snes9x-gtk nor supertuxkart which I believe also use SDL2.
Steps to reproduce
Client PC details (please complete the following information)
[AMD/ATI] Ellesmere [Radeon RX 470/480/570/570X/580/580X/590] (rev e7)
Server PC details (please complete the following information)
Moonlight Logs (please attach)
With the SDL2 2.0.24.0-1 (problem is present):
With SDL 2.0.22-2 (problem is absent):