Files
Sunshine/tests/unit/platform/linux/test_audio.cpp
T
Carlos BarreroandClaude Opus 5 bfb7645cd1 fix(linux/audio): capture in the canonical channel layout
The PulseAudio backend built the capture stream's channel map from the Opus
channel mapping. That mapping is applied by the multistream encoder in
audio.cpp and undone by the client when it decodes, so the two cancel out.
Using it as the capture channel map makes PulseAudio reorder the samples as
well, and that second reordering is never undone.

It is invisible with the built-in mappings, which are all identities, so it
went unnoticed until custom surround-params arrived in #2424. A client that
requests its own mapping gets permuted channels: the webOS Moonlight client
asks for 0,1,4,5,2,3 and ends up with centre on rear left, rear left on
centre, LFE on rear right and rear right on the subwoofer.

Capture in the canonical layout instead, which is what the WASAPI and macOS
backends already do by ignoring the mapping argument entirely. Since
map_stereo, map_surround51 and map_surround71 are all the identity over
speaker_e, the canonical position of channel i is position_mapping[i], so
the map is now filled directly from the channel index.

Clients that do not send surroundParams are unaffected: the resulting
pa_channel_map is identical to the current one for every layout in
stream_configs. No other Moonlight client sends surroundParams today;
android, ios, qt, embedded and tizen send only the standard
surroundAudioInfo.

The map construction moves into fill_canonical_channel_map() so it can be
tested without a running PulseAudio server, and position_mapping becomes a
std::array bounded by speaker::MAX_SPEAKERS so the parallel-array
relationship with speaker_e is enforced by the compiler. Out-of-range
channel counts are now rejected rather than read past the end.

Refs: https://github.com/mariotaku/moonlight-tv/issues/575

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-09 15:51:12 -04:00

113 lines
3.4 KiB
C++

/**
* @file tests/unit/platform/linux/test_audio.cpp
* @brief Tests for the Linux audio capture channel map.
*/
#ifdef __linux__
// standard includes
#include <vector>
// lib includes
#include <gtest/gtest.h>
#include <pulse/pulseaudio.h>
// local includes
#include "src/platform/common.h"
namespace platf {
bool fill_canonical_channel_map(int channels, pa_channel_map &pa_map);
} // namespace platf
namespace {
/**
* @brief Collect the speaker positions a channel map describes.
*
* @param pa_map Channel map to read.
* @return The positions of its channels, in channel order.
*/
std::vector<pa_channel_position_t> positions_of(const pa_channel_map &pa_map) {
return {pa_map.map, pa_map.map + pa_map.channels};
}
} // namespace
/**
* @brief Capture must use the canonical layout, whatever Opus mapping the client asked for.
*
* The Opus mapping is applied by the multistream encoder and undone by the client when it
* decodes, so applying it to the captured PCM as well leaves the client rendering permuted
* channels. The webOS client requests 0,1,4,5,2,3, which would put the centre channel on the
* rear left speaker.
*/
TEST(LinuxAudioChannelMap, Surround51UsesCanonicalOrder) {
pa_channel_map pa_map;
ASSERT_TRUE(platf::fill_canonical_channel_map(6, pa_map));
const std::vector<pa_channel_position_t> expected {
PA_CHANNEL_POSITION_FRONT_LEFT,
PA_CHANNEL_POSITION_FRONT_RIGHT,
PA_CHANNEL_POSITION_FRONT_CENTER,
PA_CHANNEL_POSITION_LFE,
PA_CHANNEL_POSITION_REAR_LEFT,
PA_CHANNEL_POSITION_REAR_RIGHT
};
EXPECT_EQ(positions_of(pa_map), expected);
}
/**
* @brief Stereo capture maps onto the two front channels.
*/
TEST(LinuxAudioChannelMap, StereoUsesCanonicalOrder) {
pa_channel_map pa_map;
ASSERT_TRUE(platf::fill_canonical_channel_map(2, pa_map));
const std::vector<pa_channel_position_t> expected {
PA_CHANNEL_POSITION_FRONT_LEFT,
PA_CHANNEL_POSITION_FRONT_RIGHT
};
EXPECT_EQ(positions_of(pa_map), expected);
}
/**
* @brief 7.1 capture adds the two side channels to the 5.1 layout.
*/
TEST(LinuxAudioChannelMap, Surround71UsesCanonicalOrder) {
pa_channel_map pa_map;
ASSERT_TRUE(platf::fill_canonical_channel_map(8, pa_map));
const std::vector<pa_channel_position_t> expected {
PA_CHANNEL_POSITION_FRONT_LEFT,
PA_CHANNEL_POSITION_FRONT_RIGHT,
PA_CHANNEL_POSITION_FRONT_CENTER,
PA_CHANNEL_POSITION_LFE,
PA_CHANNEL_POSITION_REAR_LEFT,
PA_CHANNEL_POSITION_REAR_RIGHT,
PA_CHANNEL_POSITION_SIDE_LEFT,
PA_CHANNEL_POSITION_SIDE_RIGHT
};
EXPECT_EQ(positions_of(pa_map), expected);
}
/**
* @brief The map the backend hands PulseAudio must be a valid one.
*/
TEST(LinuxAudioChannelMap, ProducesValidPulseAudioMaps) {
for (const int channels : {2, 6, 8}) {
pa_channel_map pa_map;
ASSERT_TRUE(platf::fill_canonical_channel_map(channels, pa_map)) << "channels: " << channels;
EXPECT_TRUE(pa_channel_map_valid(&pa_map)) << "channels: " << channels;
}
}
/**
* @brief Channel counts with no known speaker layout are rejected rather than read out of bounds.
*/
TEST(LinuxAudioChannelMap, RejectsUnsupportedChannelCounts) {
pa_channel_map pa_map;
EXPECT_FALSE(platf::fill_canonical_channel_map(0, pa_map));
EXPECT_FALSE(platf::fill_canonical_channel_map(-1, pa_map));
EXPECT_FALSE(platf::fill_canonical_channel_map(platf::speaker::MAX_SPEAKERS + 1, pa_map));
}
#endif // __linux__