6#include <QtCore/qdebug.h>
8#include <pipewire/version.h>
9#include <spa/pod/parser.h>
10#include <spa/param/format.h>
13# include <spa/param/audio/raw-utils.h>
15# include "qpipewire_spa_compat_p.h"
18#if PW_CHECK_VERSION
(0
, 3
, 44
)
19# include <spa/param/audio/iec958.h>
32std::optional<std::variant<spa_audio_format, SpaEnum<spa_audio_format>>>
33parseSampleFormat(
const spa_pod &pod)
35 std::optional<spa_audio_format> format = spaParsePodPropertyScalar<spa_audio_format>(
36 pod, SPA_TYPE_OBJECT_Format, SPA_FORMAT_AUDIO_format);
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);
48std::optional<std::variant<SpaRange<
int>,
int>> parseSamplingRates(
const spa_pod &pod)
50 std::optional<
int> rate =
51 spaParsePodPropertyScalar<
int>(pod, SPA_TYPE_OBJECT_Format, SPA_FORMAT_AUDIO_format);
55 std::optional<SpaRange<
int>> choice = spaParsePodPropertyChoice<
int, SPA_CHOICE_Range>(
56 pod, SPA_TYPE_OBJECT_Format, SPA_FORMAT_AUDIO_rate);
62bool isIec958Device(
const spa_pod &pod)
64 return spaParsePodPropertyScalar<spa_media_subtype>(pod, SPA_TYPE_OBJECT_Format,
65 SPA_FORMAT_mediaSubtype)
66 == SPA_MEDIA_SUBTYPE_iec958;
69bool isIec958PCMDevice(
const spa_pod &pod)
71 std::optional<spa_audio_iec958_codec> codec = spaParsePodPropertyScalar<spa_audio_iec958_codec>(
72 pod, SPA_TYPE_OBJECT_Format, SPA_FORMAT_AUDIO_iec958Codec);
74 return codec == spa_audio_iec958_codec::SPA_AUDIO_IEC958_CODEC_PCM;
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);
80 return choice->defaultValue() == spa_audio_iec958_codec::SPA_AUDIO_IEC958_CODEC_PCM;
88 struct spa_audio_info_raw info = {};
89 if (spa_format_audio_raw_parse(&obj->pod, &info) < 0)
95 bool isIec958 = isIec958Device(obj->pod);
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);
103 result.sampleTypes = spa_audio_iec958_codec::SPA_AUDIO_IEC958_CODEC_PCM;
108 auto optionalSampleFormat = parseSampleFormat(obj->pod);
109 if (!optionalSampleFormat)
112 std::visit([&](
auto &&arg) {
113 result.sampleTypes = std::forward<
decltype(arg)>(arg);
114 }, *optionalSampleFormat);
117 if (info.rate != 0) {
118 result.rates =
int(info.rate);
120 auto optionalSamplingRates = parseSamplingRates(obj->pod);
121 if (!optionalSamplingRates)
123 std::visit([&](
auto arg) {
125 }, *optionalSamplingRates);
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]));
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);
153spa_audio_format toSpaAudioFormat(QAudioFormat::SampleFormat 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;
165 return SPA_AUDIO_FORMAT_UNKNOWN;
169void initializeChannelPositions(spa_audio_info_raw &info,
const QAudioFormat &fmt)
171 using ChannelConfig = QAudioFormat::ChannelConfig;
172 const ChannelConfig cfg = fmt.channelConfig();
174 auto fillPositions = [&](QSpan<
const spa_audio_channel> positions) {
175 std::copy(positions.begin(), positions.end(), std::begin(info.position));
179 case ChannelConfig::ChannelConfigMono:
180 return fillPositions(channelPositionsMono);
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:
200#if !PW_CHECK_VERSION
(0
, 3
, 33
)
201 uint32_t SPA_AUDIO_CHANNEL_START_Aux = 0x1000;
209 std::iota(info.position, info.position + fmt.channelCount(),
210 uint32_t(SPA_AUDIO_CHANNEL_START_Aux));
220 spa_audio_info_raw ret{
221 .format = toSpaAudioFormat(fmt.sampleFormat()),
223 .rate = uint32_t(fmt.sampleRate()),
224 .channels = uint32_t(fmt.channelCount()),
228 initializeChannelPositions(ret, fmt);
spa_audio_info_raw asSpaAudioInfoRaw(const QAudioFormat &)
static constexpr spa_media_subtype SPA_MEDIA_SUBTYPE_iec958
static constexpr spa_format SPA_FORMAT_AUDIO_iec958Codec