From a8b01d6c294561f67c145ae66be7054ed514bcad Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sun, 27 May 2018 18:45:09 -0700 Subject: [PATCH] Enable high quality surround sound when the video bitrate is over 15 Mbps --- src/AudioStream.c | 13 ++++++++++--- src/Connection.c | 1 + src/Limelight-internal.h | 1 + src/SdpGenerator.c | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/AudioStream.c b/src/AudioStream.c index 8d9ecec..ff4c0b1 100644 --- a/src/AudioStream.c +++ b/src/AudioStream.c @@ -17,7 +17,7 @@ static unsigned short lastSeq; #define RTP_PORT 48000 -#define MAX_PACKET_SIZE 400 +#define MAX_PACKET_SIZE 1400 // This is much larger than we should typically have buffered, but // it needs to be. We need a cushion in case our thread gets blocked @@ -282,9 +282,16 @@ void stopAudioStream(void) { int startAudioStream(void* audioContext, int arFlags) { int err; + OPUS_MULTISTREAM_CONFIGURATION chosenConfig; - err = AudioCallbacks.init(StreamConfig.audioConfiguration, - opusConfigArray[StreamConfig.audioConfiguration], audioContext, arFlags); + memcpy(&chosenConfig, opusConfigArray[StreamConfig.audioConfiguration], sizeof(chosenConfig)); + if (HighQualitySurroundEnabled) { + // With high quality mode enabled, the encoder stops using coupled streams + chosenConfig.coupledStreams = 0; + chosenConfig.streams = chosenConfig.channelCount; + } + + err = AudioCallbacks.init(StreamConfig.audioConfiguration, &chosenConfig, audioContext, arFlags); if (err != 0) { return err; } diff --git a/src/Connection.c b/src/Connection.c index dee8bfa..b763043 100644 --- a/src/Connection.c +++ b/src/Connection.c @@ -18,6 +18,7 @@ DECODER_RENDERER_CALLBACKS VideoCallbacks; AUDIO_RENDERER_CALLBACKS AudioCallbacks; int NegotiatedVideoFormat; volatile int ConnectionInterrupted; +int HighQualitySurroundEnabled; // Connection stages static const char* stageNames[STAGE_MAX] = { diff --git a/src/Limelight-internal.h b/src/Limelight-internal.h index 4ed1f02..e9bc8bb 100644 --- a/src/Limelight-internal.h +++ b/src/Limelight-internal.h @@ -20,6 +20,7 @@ extern DECODER_RENDERER_CALLBACKS VideoCallbacks; extern AUDIO_RENDERER_CALLBACKS AudioCallbacks; extern int NegotiatedVideoFormat; extern volatile int ConnectionInterrupted; +extern int HighQualitySurroundEnabled; #ifndef UINT24_MAX #define UINT24_MAX 0xFFFFFF diff --git a/src/SdpGenerator.c b/src/SdpGenerator.c index c59df53..d63de22 100644 --- a/src/SdpGenerator.c +++ b/src/SdpGenerator.c @@ -12,6 +12,7 @@ #define CHANNEL_MASK_51_SURROUND 0xFC #define LOW_BITRATE_THRESHOLD 5000 +#define HIGH_BITRATE_THRESHOLD 15000 typedef struct _SDP_OPTION { char name[MAX_OPTION_NAME_LEN + 1]; @@ -362,6 +363,19 @@ static PSDP_OPTION getAttributesList(char*urlSafeAddr) { } if (AppVersionQuad[0] >= 7) { + if (StreamConfig.bitrate > HIGH_BITRATE_THRESHOLD && audioChannelCount > 2) { + // Enable high quality mode for surround sound + err |= addAttributeString(&optionHead, "x-nv-audio.surround.AudioQuality", "1"); + + // Let the audio stream code know that it needs to disable coupled streams when + // decoding this audio stream. + HighQualitySurroundEnabled = 1; + } + else { + err |= addAttributeString(&optionHead, "x-nv-audio.surround.AudioQuality", "0"); + HighQualitySurroundEnabled = 0; + } + if (StreamConfig.bitrate < LOW_BITRATE_THRESHOLD && audioChannelCount == 2) { // At low bitrates, cap the stereo audio bitrate to reduce data usage. For some reason, // GFE seems to always enable high quality (512 Kbps) mode for stereo even though we