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_audiodevicemonitor_p.h
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
4#ifndef QPIPEWIRE_AUDIODEVICEMONITOR_P_H
5#define QPIPEWIRE_AUDIODEVICEMONITOR_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
22
23#include <QtCore/qfuture.h>
24#include <QtCore/qtimer.h>
25#include <QtCore/qreadwritelock.h>
26#include <QtMultimedia/qaudiodevice.h>
27
28#include <list>
29#include <vector>
30
31#include <pipewire/pipewire.h>
32
33QT_BEGIN_NAMESPACE
34
35namespace QtPipeWire {
36
38{
40public:
42 ObjectSerial serial() const;
43
46
47private:
48 const ObjectSerial m_observedSerial;
49};
50
52
53// TODO: can we make use of COW here?
55{
57
58 enum class Direction : uint8_t
59 {
60 sink,
61 source,
62 };
63
64public:
66 void objectAdded(ObjectId, uint32_t permissions, PipewireRegistryType, uint32_t version,
67 const spa_dict &props);
69
71 {
72 };
76
77 std::optional<ObjectSerial> findSinkNodeSerial(std::string_view nodeName) const;
78 std::optional<ObjectSerial> findSourceNodeSerial(std::string_view nodeName) const;
79
80 // ObjectId/ObjectSerial mapping
81 std::optional<ObjectId> findObjectId(ObjectSerial) const;
82 std::optional<ObjectSerial> findObjectSerial(ObjectId) const;
83
86
87 // Obtaining device lists
93 DeviceLists getDeviceLists(bool verifyThreading = true);
94
98
99private:
100 struct DeviceRecord
101 {
102 ObjectSerial serial;
103 PwPropertyDict properties;
104 };
105
106 struct PendingNodeRecord
107 {
108 PendingNodeRecord(ObjectId, ObjectSerial serial, std::optional<ObjectSerial> deviceSerial,
109 PwPropertyDict properties);
110
111 ObjectSerial serial;
112 std::optional<ObjectSerial> deviceSerial; // may be nullopt for virtual nodes
113 PwPropertyDict properties;
114 std::unique_ptr<NodeEventListener> enumFormatListener;
115 std::unique_ptr<CoreEventDoneListener> enumFormatDoneListener;
116 QFuture<std::vector<SpaObjectAudioFormat>> formatFuture;
117
118 // owned by he instance, updated in the formatFuture's continuation (we capture a weak reference in the
119 // continuation to avoid lifetime issues
120 const std::shared_ptr<std::optional<std::vector<SpaObjectAudioFormat>>> formatResults;
121 };
122
123 struct NodeRecord
124 {
125 ObjectSerial serial;
126 std::optional<ObjectSerial> deviceSerial; // may be nullopt for virtual nodes
127 PwPropertyDict properties;
129 };
130
131 // discovered, but format not resolved
132 struct PendingRecords
133 {
134 std::list<PendingNodeRecord> m_sources;
135 std::list<PendingNodeRecord> m_sinks;
136 std::vector<ObjectSerial> m_removals;
137
138 std::optional<std::variant<QByteArray, NoDefaultDeviceType>> m_defaultSource;
139 std::optional<std::variant<QByteArray, NoDefaultDeviceType>> m_defaultSink;
140
141 void removeRecordsForObject(ObjectSerial);
142 };
143 QMutex m_pendingRecordsMutex;
144 PendingRecords m_pendingRecords QT_MM_GUARDED_BY(m_pendingRecordsMutex);
145
146 // discovered, format resolved. living on application thread
147 mutable QReadWriteLock m_mutex;
148 std::map<ObjectSerial, DeviceRecord> m_devices QT_MM_GUARDED_BY(m_mutex);
149 std::vector<NodeRecord> m_sources QT_MM_GUARDED_BY(m_mutex);
150 std::vector<NodeRecord> m_sinks QT_MM_GUARDED_BY(m_mutex);
151 std::optional<QByteArray> m_defaultSourceName;
152 std::optional<QByteArray> m_defaultSinkName;
153
154 QTimer m_compressionTimer;
155 void startCompressionTimer();
156
157 // Device list updates
158 void audioDevicesChanged(bool verifyThreading = true);
159 void updateSources(std::list<PendingNodeRecord> addedNodes,
160 QSpan<const ObjectSerial> removedObjects);
161 void updateSinks(std::list<PendingNodeRecord> addedNodes,
162 QSpan<const ObjectSerial> removedObjects);
163 template <Direction>
164 void updateSourcesOrSinks(std::list<PendingNodeRecord>, QSpan<const ObjectSerial>);
165
166 QList<QAudioDevice> m_sourceDeviceList;
167 QList<QAudioDevice> m_sinkDeviceList;
168
169 std::optional<ObjectSerial> findDeviceSerial(std::string_view deviceName) const;
170
171 template <Direction>
172 std::optional<ObjectSerial> findNodeSerialForNodeName(std::string_view nodeName) const;
173
174 // ObjectId/ObjectSerial mapping
175 // CHECK: can we completely rely on only accessing ObjectId under the pipewire event loop lock?
176 mutable QReadWriteLock m_objectDictMutex;
177 std::map<ObjectId, ObjectSerial> m_objectSerialDict QT_MM_GUARDED_BY(m_objectDictMutex);
178 std::map<ObjectSerial, ObjectId> m_serialObjectDict QT_MM_GUARDED_BY(m_objectDictMutex);
179
180 std::vector<SharedObjectRemoveObserver>
181 m_objectRemoveObserver QT_MM_GUARDED_BY(m_objectDictMutex);
182};
183
184} // namespace QtPipeWire
185
186QT_END_NAMESPACE
187
188#endif // QPIPEWIRE_AUDIODEVICEMONITOR_P_H
void registerStreamReference(std::shared_ptr< QPipewireAudioStream >)
void unregisterStreamReference(const std::shared_ptr< QPipewireAudioStream > &)
const PwCoreConnectionHandle & coreConnection() const
void audioSourcesChanged(QList< QAudioDevice >)
std::optional< ObjectSerial > findObjectSerial(ObjectId) const
void unregisterObserver(const SharedObjectRemoveObserver &)
std::optional< ObjectSerial > findSourceNodeSerial(std::string_view nodeName) const
void setDefaultAudioSink(std::variant< QByteArray, NoDefaultDeviceType >)
DeviceLists getDeviceLists(bool verifyThreading=true)
void setDefaultAudioSource(std::variant< QByteArray, NoDefaultDeviceType >)
std::optional< ObjectSerial > findSinkNodeSerial(std::string_view nodeName) const
static constexpr NoDefaultDeviceType NoDefaultDevice
void objectAdded(ObjectId, uint32_t permissions, PipewireRegistryType, uint32_t version, const spa_dict &props)
std::optional< ObjectId > findObjectId(ObjectSerial) const
bool registerObserver(SharedObjectRemoveObserver)
Combined button and popup list for selecting options.
static std::atomic_int s_sequenceNumberAllocator
std::unique_ptr< pw_core, PwCoreConnectionDeleter > PwCoreConnectionHandle
StrongIdType< uint32_t, ObjectIdTag > ObjectId
StrongIdType< uint64_t, ObjectSerialTag > ObjectSerial
std::shared_ptr< ObjectRemoveObserver > SharedObjectRemoveObserver
std::error_code make_error_code(int errnoValue=errno)
bool pw_check_library_version(int major, int minor, int micro)
QDebug operator<<(QDebug dbg, const pw_time &state)
QDebug operator<<(QDebug dbg, const spa_dict &dict)
QDebug operator<<(QDebug dbg, enum pw_stream_state)
#define QT_MM_GUARDED_BY(Mutex)
QDebug operator<<(QDebug dbg, const spa_pod &pod)
q23::expected< void, int > asyncWait(pw_core *coreConnection, std::function< void()> handler)
q23::expected< bool, int > sync(pw_core *coreConnection, std::optional< std::chrono::nanoseconds > timeout={})
void operator()(Type *handle) const
NodeEventListener(PwNodeHandle, NodeHandler)
std::function< void(const struct pw_node_info *)> InfoHandler
std::function< void(int, uint32_t, uint32_t, uint32_t, const struct spa_pod *)> ParamHandler
void operator()(pw_core *handle) const
friend QDebug operator<<(QDebug dbg, const StrongIdType &self)
friend bool comparesEqual(const StrongIdType &lhs, const StrongIdType &rhs) noexcept
friend Qt::strong_ordering compareThreeWay(const StrongIdType &lhs, const StrongIdType &rhs) noexcept