Truly independent web browser
LibAudio+AudioServer: Factor out Windows channel mask conversion
Zaggy102481424f3926f3
diff --git a/Libraries/LibAudio/CMakeLists.txt b/Libraries/LibAudio/CMakeLists.txtindex 58d5ec134fc..265e9a1c677 100644--- a/Libraries/LibAudio/CMakeLists.txt+++ b/Libraries/LibAudio/CMakeLists.txt@@ -23,6 +23,10 @@ if (APPLE)list(APPEND SOURCES CoreAudioChannelLayout.cpp)endif()+if (WIN32)+ list(APPEND SOURCES WindowsChannelMask.cpp)+endif()+compile_ipc(AudioClient.ipc AudioClientEndpoint.h)compile_ipc(AudioServer.ipc AudioServerEndpoint.h)compile_ipc(../../Services/AudioServer/AudioServerControl.ipc ../../Services/AudioServer/AudioServerControlEndpoint.h)diff --git a/Libraries/LibAudio/WindowsChannelMask.cpp b/Libraries/LibAudio/WindowsChannelMask.cppnew file mode 100644index 00000000000..a4a4b36d3bb--- /dev/null+++ b/Libraries/LibAudio/WindowsChannelMask.cpp@@ -0,0 +1,55 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/Vector.h>+#include <LibAudio/WindowsChannelMask.h>++#include <AK/Windows.h>+#include <mmreg.h>++namespace Audio {++// This needs to be kept up to date with KSAUDIO_CHANNEL_LAYOUT in ksmedia.h+#define ENUMERATE_CHANNEL_POSITIONS(C) \+ C(SPEAKER_FRONT_LEFT, Channel::FrontLeft) \+ C(SPEAKER_FRONT_RIGHT, Channel::FrontRight) \+ C(SPEAKER_FRONT_CENTER, Channel::FrontCenter) \+ C(SPEAKER_LOW_FREQUENCY, Channel::LowFrequency) \+ C(SPEAKER_BACK_LEFT, Channel::BackLeft) \+ C(SPEAKER_BACK_RIGHT, Channel::BackRight) \+ C(SPEAKER_FRONT_LEFT_OF_CENTER, Channel::FrontLeftOfCenter) \+ C(SPEAKER_FRONT_RIGHT_OF_CENTER, Channel::FrontRightOfCenter) \+ C(SPEAKER_BACK_CENTER, Channel::BackCenter) \+ C(SPEAKER_SIDE_LEFT, Channel::SideLeft) \+ C(SPEAKER_SIDE_RIGHT, Channel::SideRight) \+ C(SPEAKER_TOP_CENTER, Channel::TopCenter) \+ C(SPEAKER_TOP_FRONT_LEFT, Channel::TopFrontLeft) \+ C(SPEAKER_TOP_FRONT_CENTER, Channel::TopFrontCenter) \+ C(SPEAKER_TOP_FRONT_RIGHT, Channel::TopFrontRight) \+ C(SPEAKER_TOP_BACK_LEFT, Channel::TopBackLeft) \+ C(SPEAKER_TOP_BACK_CENTER, Channel::TopBackCenter) \+ C(SPEAKER_TOP_BACK_RIGHT, Channel::TopBackRight)++ErrorOr<ChannelMap> windows_channel_mask_to_channel_map(u32 channel_mask)+{+ Vector<Channel, ChannelMap::capacity()> channels;++#define MAYBE_ADD_CHANNEL_FROM_BITMAP_FLAG(ksmedia_channel_name, audio_channel) \+ if ((channel_mask & ksmedia_channel_name) != 0) { \+ if (channels.size() == ChannelMap::capacity()) [[unlikely]] \+ return Error::from_string_literal("Channel layout had too many channels"); \+ channels.unchecked_append(audio_channel); \+ }++ ENUMERATE_CHANNEL_POSITIONS(MAYBE_ADD_CHANNEL_FROM_BITMAP_FLAG);++ if ((channel_mask & SPEAKER_RESERVED) != 0) [[unlikely]]+ return Error::from_string_literal("Unsupported new KSMEDIA version");++ return ChannelMap { channels };+}++}diff --git a/Libraries/LibAudio/WindowsChannelMask.h b/Libraries/LibAudio/WindowsChannelMask.hnew file mode 100644index 00000000000..9755c7db4c2--- /dev/null+++ b/Libraries/LibAudio/WindowsChannelMask.h@@ -0,0 +1,18 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Error.h>+#include <LibAudio/ChannelMap.h>+#include <LibAudio/Export.h>++namespace Audio {++// Reads the speaker positions of a WAVEFORMATEXTENSIBLE channel mask, in the order its channels are interleaved.+AUDIO_API ErrorOr<ChannelMap> windows_channel_mask_to_channel_map(u32 channel_mask);++}diff --git a/Services/AudioServer/PlaybackStreamWasapi.cpp b/Services/AudioServer/PlaybackStreamWasapi.cppindex 1dc6f8e16ff..6ddf3b745b0 100644--- a/Services/AudioServer/PlaybackStreamWasapi.cpp+++ b/Services/AudioServer/PlaybackStreamWasapi.cpp@@ -25,6 +25,7 @@#include <AudioServer/PlaybackStreamWasapi.h>#include <LibAudio/ChannelMap.h>#include <LibAudio/SampleSpecification.h>+#include <LibAudio/WindowsChannelMask.h>#include <LibCore/System.h>#include <LibCore/ThreadedPromise.h>#include <LibThreading/Thread.h>@@ -181,46 +182,6 @@ static void print_audio_format(WAVEFORMATEXTENSIBLE& format)Span<u8> { reinterpret_cast<char*>(&format.SubFormat), 16 });}-// This needs to be kept up to date with KSAUDIO_CHANNEL_LAYOUT in ksmedia.h-#define ENUMERATE_CHANNEL_POSITIONS(C) \- C(SPEAKER_FRONT_LEFT, Channel::FrontLeft) \- C(SPEAKER_FRONT_RIGHT, Channel::FrontRight) \- C(SPEAKER_FRONT_CENTER, Channel::FrontCenter) \- C(SPEAKER_LOW_FREQUENCY, Channel::LowFrequency) \- C(SPEAKER_BACK_LEFT, Channel::BackLeft) \- C(SPEAKER_BACK_RIGHT, Channel::BackRight) \- C(SPEAKER_FRONT_LEFT_OF_CENTER, Channel::FrontLeftOfCenter) \- C(SPEAKER_FRONT_RIGHT_OF_CENTER, Channel::FrontRightOfCenter) \- C(SPEAKER_BACK_CENTER, Channel::BackCenter) \- C(SPEAKER_SIDE_LEFT, Channel::SideLeft) \- C(SPEAKER_SIDE_RIGHT, Channel::SideRight) \- C(SPEAKER_TOP_CENTER, Channel::TopCenter) \- C(SPEAKER_TOP_FRONT_LEFT, Channel::TopFrontLeft) \- C(SPEAKER_TOP_FRONT_CENTER, Channel::TopFrontCenter) \- C(SPEAKER_TOP_FRONT_RIGHT, Channel::TopFrontRight) \- C(SPEAKER_TOP_BACK_LEFT, Channel::TopBackLeft) \- C(SPEAKER_TOP_BACK_CENTER, Channel::TopBackCenter) \- C(SPEAKER_TOP_BACK_RIGHT, Channel::TopBackRight)--static ErrorOr<ChannelMap> convert_bitmask_to_channel_map(u32 channel_bitmask)-{- Vector<Channel, ChannelMap::capacity()> channels;--#define MAYBE_ADD_CHANNEL_FROM_BITMAP_FLAG(ksmedia_channel_name, audio_channel) \- if ((channel_bitmask & ksmedia_channel_name) != 0) { \- if (channels.size() == ChannelMap::capacity()) [[unlikely]] \- return Error::from_string_literal("Device channel layout had too many channels"); \- channels.unchecked_append(audio_channel); \- }-- ENUMERATE_CHANNEL_POSITIONS(MAYBE_ADD_CHANNEL_FROM_BITMAP_FLAG);-- if ((channel_bitmask & SPEAKER_RESERVED) != 0) [[unlikely]]- return Error::from_string_literal("Unsupported new KSMEDIA version");-- return ChannelMap { channels };-}-NonnullRefPtr<PlaybackStream::CreatePromise> PlaybackStreamWASAPI::create(OutputState initial_output_state, u32, AudioDataRequestCallback&& data_request_callback){auto promise = CreatePromise::construct();@@ -331,7 +292,7 @@ NonnullRefPtr<PlaybackStream::CreatePromise> PlaybackStreamWASAPI::create(OutputSampleSpecification PlaybackStreamWASAPI::sample_specification() const{- auto channel_map = MUST(convert_bitmask_to_channel_map(m_state->wave_format.dwChannelMask));+ auto channel_map = MUST(windows_channel_mask_to_channel_map(m_state->wave_format.dwChannelMask));return SampleSpecification { m_state->wave_format.Format.nSamplesPerSec, channel_map };}