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
qdarwinaudiodevice.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
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/private/qcore_mac_p.h>
7
8#include <QtMultimedia/private/qcoreaudioutils_p.h>
9#include <QtMultimedia/private/qaudioformat_p.h>
10#ifdef Q_OS_MACOS
11#include <QtMultimedia/private/qmacosaudiodatautils_p.h>
12#endif
13
14#include <optional>
15
16QT_BEGIN_NAMESPACE
17
18namespace {
19
21 QAudioDevice::Mode mode,
22 QAudioFormat::ChannelConfig channelConfig)
23{
24 QAudioFormat format;
25 format.setSampleRate(44100);
26 format.setSampleFormat(QAudioFormat::Int16);
27 format.setChannelCount(mode == QAudioDevice::Input ? 1 : 2);
28 format.setChannelConfig(channelConfig);
29 return format;
30}
31
32[[nodiscard]] QAudioFormat::ChannelConfig qGetDefaultChannelLayout(QAudioDevice::Mode mode)
33{
34 return (mode == QAudioDevice::Input) ? QAudioFormat::ChannelConfigMono : QAudioFormat::ChannelConfigStereo;
35}
36
37[[nodiscard]] QString qGetDefaultDescription(const QByteArray &id)
38{
39 return QString::fromUtf8(id);
40}
41
42#ifdef Q_OS_MACOS
43
44[[nodiscard]] std::optional<QAudioFormat> qGetPreferredFormatForCoreAudioDevice(
45 QAudioDevice::Mode mode,
46 AudioDeviceID deviceId)
47{
48 using namespace QCoreAudioUtils;
49
50 const auto audioDevicePropertyStreamsAddress =
51 makePropertyAddress(kAudioDevicePropertyStreams, mode);
52 const auto audioDevicePhysicalFormatPropertyAddress =
53 makePropertyAddress(kAudioStreamPropertyPhysicalFormat, mode);
54
55 auto streamIDs =
56 getAudioPropertyList<AudioStreamID>(deviceId, audioDevicePropertyStreamsAddress);
57 if (!streamIDs || streamIDs->empty())
58 return std::nullopt;
59
60 for (auto streamID : *streamIDs) {
61 auto streamDescription = getAudioProperty<AudioStreamBasicDescription>(
62 streamID, audioDevicePhysicalFormatPropertyAddress);
63 if (!streamDescription)
64 continue;
65
66 QAudioFormat fmt = QCoreAudioUtils::toPreferredQAudioFormat(*streamDescription);
67 if (fmt.isValid())
68 return fmt;
69 }
70 return std::nullopt;
71}
72
73[[nodiscard]] std::optional<QAudioFormat::ChannelConfig> qGetChannelLayoutForCoreAudioDevice(
74 QAudioDevice::Mode mode,
75 AudioDeviceID deviceId)
76{
77 using namespace QCoreAudioUtils;
78
79 const auto propertyAddress =
80 makePropertyAddress(kAudioDevicePropertyPreferredChannelLayout, mode);
81
82 if (auto layout = getAudioPropertyWithFlexibleArrayMember<AudioChannelLayout>(deviceId, propertyAddress))
83 return QCoreAudioUtils::fromAudioChannelLayout(layout.get());
84
85 return std::nullopt;
86}
87
88[[nodiscard]] std::optional<QString> qGetDescriptionForCoreAudioDevice(
89 QAudioDevice::Mode mode,
90 AudioDeviceID deviceId)
91{
92 using namespace QCoreAudioUtils;
93
94 const auto propertyAddress = makePropertyAddress(kAudioObjectPropertyName, mode);
95 if (auto name = getAudioProperty<QCFString>(deviceId, propertyAddress))
96 return name;
97
98 return std::nullopt;
99}
100
101[[nodiscard]] std::optional<int> qSupportedNumberOfChannels(
102 QAudioDevice::Mode mode,
103 AudioDeviceID deviceId)
104{
105 using namespace QCoreAudioUtils;
106
107 const auto audioDevicePropertyStreamsAddress =
108 makePropertyAddress(kAudioDevicePropertyStreams, mode);
109
110 auto streamIDs = getAudioPropertyList<AudioStreamID>(deviceId, audioDevicePropertyStreamsAddress);
111 if (!streamIDs)
112 return std::nullopt;
113
114 const auto propVirtualFormat = makePropertyAddress(kAudioStreamPropertyVirtualFormat, mode);
115
116 int ret{};
117
118 for (auto streamID : *streamIDs) {
119 auto streamDescription = getAudioProperty<AudioStreamBasicDescription>(streamID, propVirtualFormat);
120 if (!streamDescription)
121 continue;
122 ret += streamDescription->mChannelsPerFrame;
123 }
124
125 return ret;
126}
127
128#endif
129
130} // namespace
131
132#ifdef Q_OS_MACOS
133
134static QString getDescription(AudioDeviceID id, const QByteArray &device, QAudioDevice::Mode mode)
135{
136 if (auto optionalDescription = qGetDescriptionForCoreAudioDevice(mode, id))
137 return *optionalDescription;
138 return qGetDefaultDescription(device);
139}
140
141QCoreAudioDeviceInfo::QCoreAudioDeviceInfo(AudioDeviceID id, const QByteArray &device, QAudioDevice::Mode mode):
142 QAudioDevicePrivate{
143 device,
144 mode,
145 getDescription(id, device, mode),
146 }
147{
148 const std::optional<QAudioFormat::ChannelConfig> channelConfigOpt =
149 qGetChannelLayoutForCoreAudioDevice(mode, id);
150 if (channelConfigOpt.has_value())
151 channelConfiguration = channelConfigOpt.value();
152 else
153 channelConfiguration = qGetDefaultChannelLayout(mode);
154
155 const std::optional<QAudioFormat> preferredFormatOpt =
156 qGetPreferredFormatForCoreAudioDevice(mode, id);
157 if (preferredFormatOpt.has_value())
158 preferredFormat = preferredFormatOpt.value();
159 else
160 preferredFormat = qDefaultPreferredFormat(mode, channelConfiguration);
161
162 minimumSampleRate = QtMultimediaPrivate::allSupportedSampleRates.front();
163 maximumSampleRate = QtMultimediaPrivate::allSupportedSampleRates.back();
164 minimumChannelCount = 1;
165 maximumChannelCount = qSupportedNumberOfChannels(mode, id).value_or(16);
166
167 supportedSampleFormats = qAllSupportedSampleFormats();
168}
169
170#else
171
172QCoreAudioDeviceInfo::QCoreAudioDeviceInfo(const QByteArray &device, QAudioDevice::Mode mode)
173 : QAudioDevicePrivate(device, mode, qGetDefaultDescription(device))
174{
175 channelConfiguration = qGetDefaultChannelLayout(mode);
176 preferredFormat = qDefaultPreferredFormat(mode, channelConfiguration);
177
178 minimumSampleRate = 1;
179 maximumSampleRate = 96000;
180 minimumChannelCount = 1;
181 maximumChannelCount = 16;
182 supportedSampleFormats = qAllSupportedSampleFormats();
183}
184
185#endif
186
187QT_END_NAMESPACE
QAudioFormat::ChannelConfig qGetDefaultChannelLayout(QAudioDevice::Mode mode)
QAudioFormat qDefaultPreferredFormat(QAudioDevice::Mode mode, QAudioFormat::ChannelConfig channelConfig)
QString qGetDefaultDescription(const QByteArray &id)