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_cameradevicemonitor.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 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 <QtMultimedia/private/qcameradevice_p.h>
7#include <QtMultimedia/private/qpipewire_async_support_p.h>
8#include <QtMultimedia/private/qpipewire_cameracontextmanager_p.h>
9#include <QtMultimedia/private/qpipewire_cameranode_p.h>
10#include <QtMultimedia/private/qmultimedia_ranges_p.h>
11#include <QtCore/qcoreapplication.h>
12#include <QtCore/qloggingcategory.h>
13#include <QtCore/qthread.h>
14
15#include <spa/param/param.h>
16
17QT_BEGIN_NAMESPACE
18
19namespace QtPipeWire {
20
21Q_STATIC_LOGGING_CATEGORY(lcPipewireCameraMonitor, "qt.multimedia.pipewire.camera.devicemonitor")
22
24{
25 using namespace QtMultimediaPrivate;
26
27 std::vector<CameraNode> result = [&] {
29 return m_nodes | views::values | ranges::to<std::vector>();
30 }();
31
32 ranges::sort(result, [](const CameraNode &lhs, const CameraNode &rhs) {
34 });
35
36 return result;
37}
38
40{
41 if (!QThread::isMainThread())
42 // ensure that device monitor runs on application thread
44}
45
47{
49
50 for (const auto &[id, node] : m_nodes) {
52 return id;
53 }
54
55 return std::nullopt;
56}
57
69
71{
73
74 for (const auto &[id, node] : m_nodes) {
76 continue;
77
78 // The serial is unambiguous and survives a node name being reused.
81
84
85 return std::nullopt;
86 }
87
88 return std::nullopt;
89}
90
91const PwPropertyDict *
93{
95 if (!deviceId)
96 return nullptr;
97
98 const auto device = m_devices.find(*deviceId);
99 if (device == m_devices.end())
100 return nullptr;
101
102 return &device->second.properties;
103}
104
105namespace {
106
107std::vector<SpaObjectVideoFormat> enumerateNodeFormats(QCameraContextManager &context,
108 ObjectId node)
109{
110 using namespace std::chrono_literals;
111 QPipeWireInstance &pwInstance = context.pwInstance();
112
113 auto formats = std::make_shared<std::vector<SpaObjectVideoFormat>>();
114
115 auto onParam = [formats](int /*seq*/, uint32_t /*id*/, uint32_t /*index*/, uint32_t /*next*/,
116 const spa_pod *param) {
117 if (std::optional<SpaObjectVideoFormat> format = SpaObjectVideoFormat::parse(param))
118 formats->push_back(std::move(*format));
119 };
120
121 std::optional<NodeEventListener> listener;
122
123 pwInstance.runWithEventLoopLock([&] {
124 listener.emplace(context.bindNode(node),
125 NodeEventListener::NodeHandler{
126 {},
127 std::move(onParam),
128 });
129 listener->enumParams(SPA_PARAM_EnumFormat);
130 });
131
132 CoreEventSyncHelper sync;
133 // wait for the enumeration to complete, so that we can return the formats to the caller.
134 auto syncOrErr = sync.sync(context.coreConnection().get(), 3s);
135 if (syncOrErr != true)
136 qCWarning(lcPipewireCameraMonitor) << "timed out enumerating the formats of node" << node;
137
138 // The listener has to go before the formats are handed out, so that no
139 // further param event can append to them.
140 listener.reset();
141
142 return std::move(*formats);
143}
144
145} // namespace
146
148{
151
152 // Make sure everything the server had to say about the graph has arrived
153 // before deciding which cameras there are.
155
157
158 for (const CameraNode &node : cameraNodes()) {
160 if (id.isEmpty())
161 continue;
162
164
166 info->id = id;
170
171 if (info->videoFormats.isEmpty()) {
172 qCDebug(lcPipewireCameraMonitor) << "skipping camera without usable formats" << id;
173 continue;
174 }
175
176 for (const QCameraFormat &format : info->videoFormats) {
179 }
180
181 // PipeWire has no notion of a default video source, unlike
182 // default.audio.source, so the first camera in a stable order it is.
184
186 }
187
188 return result;
189}
190
192 const spa_dict &properties)
193{
194 using namespace QtMultimediaPrivate;
195 bool changed = false;
196
197 switch (type) {
200
203
204 // A device may show up after the nodes that belong to it, so the
205 // descriptions of those nodes can change now.
206 for (auto &[nodeId, node] : m_nodes) {
207 if (getDeviceId(node.properties) == id) {
209 changed = true;
210 }
211 }
212 break;
213 }
214
217 if (!isCameraNode(dict))
218 return;
219
220 qCDebug(lcPipewireCameraMonitor) << "camera node added" << id << cameraDeviceId(dict);
221
223 id,
224 std::move(dict),
226 };
227
231
233 changed = true;
234 break;
235 }
236
237 default:
238 return;
239 }
240
241 if (changed)
243}
244
246{
247 bool changed = false;
248
249 {
251
252 if (m_nodes.erase(id) != 0) {
253 qCDebug(lcPipewireCameraMonitor) << "camera node removed" << id;
254 changed = true;
255 }
256
257 if (m_devices.erase(id) != 0) {
258 // Drop the borrowed description of any node that outlives its
259 // device, rather than keeping stale properties around.
260 for (auto &[nodeId, node] : m_nodes) {
261 if (getDeviceId(node.properties) == id) {
263 changed = true;
264 }
265 }
266 }
267 }
268
269 if (changed)
271}
272
273} // namespace QtPipeWire
274
275QT_END_NAMESPACE
Q_STATIC_LOGGING_CATEGORY(lcPipewireAudioSink, "qt.multimedia.pipewire.audiosink")