diff --git a/src/platform/linux/audio.cpp b/src/platform/linux/audio.cpp index fe246bff5..43db87b10 100644 --- a/src/platform/linux/audio.cpp +++ b/src/platform/linux/audio.cpp @@ -3,6 +3,8 @@ * @brief Definitions for audio control on Linux. */ // standard includes +#include +#include #include #include #include @@ -23,9 +25,9 @@ namespace platf { using namespace std::literals; /** - * @brief Position mapping. + * @brief PulseAudio position for each `speaker::speaker_e`, in `speaker_e` order. */ - constexpr pa_channel_position_t position_mapping[] { + constexpr std::array position_mapping { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT, PA_CHANNEL_POSITION_FRONT_CENTER, @@ -88,26 +90,65 @@ namespace platf { } }; + /** + * @brief Fill a PulseAudio channel map with the canonical layout for a channel count. + * + * Capture always uses the canonical layout, never the Opus channel mapping: the Opus mapping + * is applied by the multistream encoder and undone by the client, so applying it here too + * would leave the samples permuted. `speaker::map_stereo`, `map_surround51` and + * `map_surround71` are the identity over `speaker::speaker_e`, so the canonical position of + * channel `i` is `position_mapping[i]`. + * + * @param channels Number of audio channels in the stream. + * @param pa_map Channel map to fill; left untouched when `channels` is out of range. + * @return `true` when the channel count maps onto a known speaker layout. + */ + bool fill_canonical_channel_map(int channels, pa_channel_map &pa_map) { + if (channels < 1 || channels > speaker::MAX_SPEAKERS) { + return false; + } + + pa_channel_map_init(&pa_map); + pa_map.channels = static_cast(channels); + for (int i = 0; i < channels; ++i) { + pa_map.map[i] = position_mapping[i]; + } + + return true; + } + /** * @brief Create a microphone capture stream for the requested layout. * - * @param mapping Opus channel mapping table for the requested layout. + * @param mapping Opus channel mapping table for the requested layout. Ignored: capture always + * uses the canonical layout, as on the WASAPI and macOS backends. * @param channels Number of audio channels in the stream. * @param sample_rate Audio sample rate in hertz. * @param frame_size Number of samples captured per audio frame. * @param source_name Source name. - * @return Microphone capture object for the requested audio layout. + * @return Microphone capture object for the requested audio layout, or nullptr on failure. */ - std::unique_ptr microphone(const std::uint8_t *mapping, int channels, std::uint32_t sample_rate, std::uint32_t frame_size, std::string source_name) { + std::unique_ptr microphone([[maybe_unused]] const std::uint8_t *mapping, int channels, std::uint32_t sample_rate, std::uint32_t frame_size, std::string source_name) { + // Capture in the canonical channel layout, not the one described by the Opus mapping. + // + // The Opus mapping is applied by the multistream encoder in audio.cpp and is undone by + // the client when it decodes, so the two cancel out. Passing it to PulseAudio as the + // capture channel map makes PulseAudio reorder the samples as well, and that reordering + // is never undone: the client ends up rendering permuted channels. + // + // This is invisible with the built-in mappings, which are all identities, but it + // scrambles the audio whenever a client requests custom surround-params (the webOS + // client asks for 0,1,4,5,2,3). The WASAPI backend ignores the mapping for this reason. + // + pa_channel_map pa_map; + if (!fill_canonical_channel_map(channels, pa_map)) { + BOOST_LOG(error) << "Unsupported channel count for audio capture: "sv << channels; + return nullptr; + } + auto mic = std::make_unique(); pa_sample_spec ss {PA_SAMPLE_FLOAT32, sample_rate, (std::uint8_t) channels}; - pa_channel_map pa_map; - - pa_map.channels = channels; - std::for_each_n(pa_map.map, pa_map.channels, [mapping](auto &channel) mutable { - channel = position_mapping[*mapping++]; - }); pa_buffer_attr pa_attr = { .maxlength = uint32_t(-1), diff --git a/tests/unit/platform/linux/test_audio.cpp b/tests/unit/platform/linux/test_audio.cpp new file mode 100644 index 000000000..f905bd48f --- /dev/null +++ b/tests/unit/platform/linux/test_audio.cpp @@ -0,0 +1,112 @@ +/** + * @file tests/unit/platform/linux/test_audio.cpp + * @brief Tests for the Linux audio capture channel map. + */ + +#ifdef __linux__ + + // standard includes + #include + + // lib includes + #include + #include + + // 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 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 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 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 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__