Skip to content

Commit

Permalink
fix: surround audio mapping between Gstreamer and Moonlight
Browse files Browse the repository at this point in the history
  • Loading branch information
ABeltramo committed Aug 17, 2024
1 parent 75c7bd1 commit 6cd2c72
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
29 changes: 28 additions & 1 deletion src/moonlight-server/rtsp/commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ RTSP_PACKET ok_msg(int sequence_number,
// Additional feature supports
constexpr uint32_t FS_PEN_TOUCH_EVENTS = 0x01;
constexpr uint32_t FS_CONTROLLER_TOUCH_EVENTS = 0x02;
using namespace wolf::core::audio;

RTSP_PACKET
describe(const RTSP_PACKET &req, const state::StreamSession &session) {
Expand All @@ -48,12 +49,38 @@ describe(const RTSP_PACKET &req, const state::StreamSession &session) {
payloads.push_back({"a", "a=rtpmap:98 AV1/90000"});
}

auto mapping_p = session.audio_mode.speakers;
// Opusenc forces a re-mapping to Vorbis; see
// https://gitlab.freedesktop.org/gstreamer/gstreamer/-/blob/main/subprojects/gst-plugins-base/ext/opus/gstopusenc.c#L549-572
if (session.audio_mode.channels == 6) { // 5.1
mapping_p = {
// The mapping for 5.1 is: [0 1 4 5 2 3]
AudioMode::FRONT_LEFT,
AudioMode::FRONT_RIGHT,
AudioMode::BACK_LEFT,
AudioMode::BACK_RIGHT,
AudioMode::FRONT_CENTER,
AudioMode::LOW_FREQUENCY,
};
} else if (session.audio_mode.channels == 8) { // 7.1
mapping_p = {
// The mapping for 7.1 is: [0 1 4 5 2 3 6 7]
AudioMode::FRONT_LEFT,
AudioMode::FRONT_RIGHT,
AudioMode::BACK_LEFT,
AudioMode::BACK_RIGHT,
AudioMode::FRONT_CENTER,
AudioMode::LOW_FREQUENCY,
AudioMode::SIDE_LEFT,
AudioMode::SIDE_RIGHT,
};
}

/**
* GFE advertises incorrect mapping for normal quality configurations,
* as a result, Moonlight rotates all channels from index '3' to the right
* To work around this, rotate channels to the left from index '3'
*/
auto mapping_p = session.audio_mode.speakers;
if (session.audio_mode.channels > 2) { // 5.1 and 7.1
std::rotate(mapping_p.begin() + 3, mapping_p.begin() + 4, mapping_p.end());
}
Expand Down
3 changes: 2 additions & 1 deletion tests/testRTSP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using Catch::Matchers::Equals;
using namespace std::string_literals;
using namespace state;
using namespace rtsp;
using namespace wolf::core::audio;

/**
* In order to test rtsp::tcp_connection we create a derived class that does the opposite:
Expand Down Expand Up @@ -252,7 +253,7 @@ TEST_CASE("Custom Parser", "[RTSP]") {
state::SessionsAtoms test_init_state() {
StreamSession session = {
.display_mode = {1920, 1080, 60},
.audio_mode = {2, 1, 1, {state::AudioMode::FRONT_LEFT, state::AudioMode::FRONT_RIGHT}},
.audio_mode = {2, 1, 1, {AudioMode::FRONT_LEFT, AudioMode::FRONT_RIGHT}},
.app = std::make_shared<state::App>(state::App{.base = {},
.h264_gst_pipeline = "",
.hevc_gst_pipeline = "",
Expand Down

0 comments on commit 6cd2c72

Please sign in to comment.