Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpipewire_spa_pod_support.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtCore/qdebug.h>
7
8#include <pipewire/version.h>
9#include <spa/pod/parser.h>
10#include <spa/param/format.h>
11
12#if __has_include(<spa/param/audio/raw-utils.h>)
13# include <spa/param/audio/raw-utils.h>
14#else
15# include "qpipewire_spa_compat_p.h"
16#endif
17
18#if PW_CHECK_VERSION(0, 3, 44)
19# include <spa/param/audio/iec958.h>
20#else
22static constexpr spa_format SPA_FORMAT_AUDIO_iec958Codec = spa_format(65542);
23static constexpr spa_media_subtype SPA_MEDIA_SUBTYPE_iec958 = spa_media_subtype(3);
24#endif
25
26QT_BEGIN_NAMESPACE
27
28namespace QtPipeWire {
29
30namespace {
31
32std::optional<std::variant<spa_audio_format, SpaEnum<spa_audio_format>>>
33parseSampleFormat(const spa_pod &pod)
34{
35 std::optional<spa_audio_format> format = spaParsePodPropertyScalar<spa_audio_format>(
36 pod, SPA_TYPE_OBJECT_Format, SPA_FORMAT_AUDIO_format);
37 if (format)
38 return format;
39
40 std::optional<SpaEnum<spa_audio_format>> choice =
41 spaParsePodPropertyChoice<spa_audio_format, SPA_CHOICE_Enum>(
42 pod, SPA_TYPE_OBJECT_Format, SPA_FORMAT_AUDIO_format);
43 if (choice)
44 return *choice;
45 return std::nullopt;
46}
47
48std::optional<std::variant<SpaRange<int>, int>> parseSamplingRates(const spa_pod &pod)
49{
50 std::optional<int> rate =
51 spaParsePodPropertyScalar<int>(pod, SPA_TYPE_OBJECT_Format, SPA_FORMAT_AUDIO_format);
52 if (rate)
53 return rate;
54
55 std::optional<SpaRange<int>> choice = spaParsePodPropertyChoice<int, SPA_CHOICE_Range>(
56 pod, SPA_TYPE_OBJECT_Format, SPA_FORMAT_AUDIO_rate);
57 if (choice)
58 return *choice;
59 return std::nullopt;
60}
61
62bool isIec958Device(const spa_pod &pod)
63{
64 return spaParsePodPropertyScalar<spa_media_subtype>(pod, SPA_TYPE_OBJECT_Format,
65 SPA_FORMAT_mediaSubtype)
66 == SPA_MEDIA_SUBTYPE_iec958;
67}
68
69bool isIec958PCMDevice(const spa_pod &pod)
70{
71 std::optional<spa_audio_iec958_codec> codec = spaParsePodPropertyScalar<spa_audio_iec958_codec>(
72 pod, SPA_TYPE_OBJECT_Format, SPA_FORMAT_AUDIO_iec958Codec);
73 if (codec)
74 return codec == spa_audio_iec958_codec::SPA_AUDIO_IEC958_CODEC_PCM;
75
76 std::optional<SpaEnum<spa_audio_iec958_codec>> choice =
77 spaParsePodPropertyChoice<spa_audio_iec958_codec, SPA_CHOICE_Enum>(
78 pod, SPA_TYPE_OBJECT_Format, SPA_FORMAT_AUDIO_iec958Codec);
79 if (choice)
80 return choice->defaultValue() == spa_audio_iec958_codec::SPA_AUDIO_IEC958_CODEC_PCM;
81 return false;
82}
83
84} // namespace
85
86std::optional<SpaObjectAudioFormat> SpaObjectAudioFormat::parse(const spa_pod_object *obj)
87{
88 struct spa_audio_info_raw info = {};
89 if (spa_format_audio_raw_parse(&obj->pod, &info) < 0)
90 return std::nullopt;
91
93 result.channelCount = int(info.channels);
94
95 bool isIec958 = isIec958Device(obj->pod);
96
97 if (info.format != spa_audio_format::SPA_AUDIO_FORMAT_UNKNOWN) {
98 result.sampleTypes = info.format;
99 } else if (isIec958) {
100 bool isIec958Pcm = isIec958PCMDevice(obj->pod);
101 if (isIec958Pcm) {
102 result.channelCount = 2; // IEC958 PCM is always stereo
103 result.sampleTypes = spa_audio_iec958_codec::SPA_AUDIO_IEC958_CODEC_PCM;
104 } else {
105 return std::nullopt;
106 }
107 } else {
108 auto optionalSampleFormat = parseSampleFormat(obj->pod);
109 if (!optionalSampleFormat)
110 return std::nullopt;
111
112 std::visit([&](auto &&arg) {
113 result.sampleTypes = std::forward<decltype(arg)>(arg);
114 }, *optionalSampleFormat);
115 }
116
117 if (info.rate != 0) {
118 result.rates = int(info.rate);
119 } else {
120 auto optionalSamplingRates = parseSamplingRates(obj->pod);
121 if (!optionalSamplingRates)
122 return std::nullopt;
123 std::visit([&](auto arg) {
124 result.rates = arg;
125 }, *optionalSamplingRates);
126 }
127
128 if (isIec958) {
129 // IEC958 PCM is always stereo, and the POD won't contain any information about channel
130 // positioning.
131 } else if (!SPA_FLAG_IS_SET(info.flags, SPA_AUDIO_FLAG_UNPOSITIONED)) {
132 result.channelPositions = QList<spa_audio_channel>();
133 for (int channelIndex = 0; channelIndex != result.channelCount; ++channelIndex)
134 result.channelPositions->push_back(spa_audio_channel(info.position[channelIndex]));
135 } else {
136 // unpositionioned
137 }
138
139 return result;
140}
141
142std::optional<SpaObjectAudioFormat> SpaObjectAudioFormat::parse(const spa_pod *pod)
143{
144 if (spa_pod_is_object_type(pod, SPA_TYPE_OBJECT_Format)) {
145 const spa_pod_object *obj = reinterpret_cast<const spa_pod_object *>(pod);
146 return parse(obj);
147 }
148 return std::nullopt;
149}
150
151namespace {
152
153spa_audio_format toSpaAudioFormat(QAudioFormat::SampleFormat fmt)
154{
155 switch (fmt) {
156 case QAudioFormat::Int16:
157 return SPA_AUDIO_FORMAT_S16;
158 case QAudioFormat::Int32:
159 return SPA_AUDIO_FORMAT_S32;
160 case QAudioFormat::UInt8:
161 return SPA_AUDIO_FORMAT_U8;
162 case QAudioFormat::Float:
163 return SPA_AUDIO_FORMAT_F32;
164 default:
165 return SPA_AUDIO_FORMAT_UNKNOWN;
166 }
167}
168
169void initializeChannelPositions(spa_audio_info_raw &info, const QAudioFormat &fmt)
170{
171 using ChannelConfig = QAudioFormat::ChannelConfig;
172 const ChannelConfig cfg = fmt.channelConfig();
173
174 auto fillPositions = [&](QSpan<const spa_audio_channel> positions) {
175 std::copy(positions.begin(), positions.end(), std::begin(info.position));
176 };
177
178 switch (cfg) {
179 case ChannelConfig::ChannelConfigMono:
180 return fillPositions(channelPositionsMono);
181
182 case ChannelConfig::ChannelConfigStereo:
183 return fillPositions(channelPositionsStereo);
184 case ChannelConfig::ChannelConfig2Dot1:
185 return fillPositions(channelPositions2Dot1);
186 case ChannelConfig::ChannelConfig3Dot0:
187 return fillPositions(channelPositions3Dot0);
188 case ChannelConfig::ChannelConfig3Dot1:
189 return fillPositions(channelPositions3Dot1);
190 case ChannelConfig::ChannelConfigSurround5Dot0:
191 return fillPositions(channelPositions5Dot0);
192 case ChannelConfig::ChannelConfigSurround5Dot1:
193 return fillPositions(channelPositions5Dot1);
194 case ChannelConfig::ChannelConfigSurround7Dot0:
195 return fillPositions(channelPositions7Dot0);
196 case ChannelConfig::ChannelConfigSurround7Dot1:
197 return fillPositions(channelPositions7Dot1);
198 case ChannelConfig::ChannelConfigUnknown:
199 default: {
200#if !PW_CHECK_VERSION(0, 3, 33)
201 uint32_t SPA_AUDIO_CHANNEL_START_Aux = 0x1000;
202#endif
203
204 // now we're in speculative territory: ChannelConfig is a bitmask and isn't
205 // able to represent arbitrary channel configurations.
206 //
207 // as a "best effort", we can try to populate all channels as "Aux" channels
208 // depending on the channel count
209 std::iota(info.position, info.position + fmt.channelCount(),
210 uint32_t(SPA_AUDIO_CHANNEL_START_Aux));
211 return;
212 }
213 }
214}
215
216} // namespace
217
218spa_audio_info_raw asSpaAudioInfoRaw(const QAudioFormat &fmt)
219{
220 spa_audio_info_raw ret{
221 .format = toSpaAudioFormat(fmt.sampleFormat()),
222 .flags = {},
223 .rate = uint32_t(fmt.sampleRate()),
224 .channels = uint32_t(fmt.channelCount()),
225 .position = {},
226 };
227
228 initializeChannelPositions(ret, fmt);
229
230 return ret;
231}
232
233} // namespace QtPipeWire
234
235QT_END_NAMESPACE
spa_audio_info_raw asSpaAudioInfoRaw(const QAudioFormat &)
#define __has_include(x)
static constexpr spa_media_subtype SPA_MEDIA_SUBTYPE_iec958
static constexpr spa_format SPA_FORMAT_AUDIO_iec958Codec