Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
4
#
include
"qpipewire_cameradevicemonitor_p.h"
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
17
QT_BEGIN_NAMESPACE
18
19
namespace
QtPipeWire
{
20
21
Q_STATIC_LOGGING_CATEGORY
(
lcPipewireCameraMonitor
,
"qt.multimedia.pipewire.camera.devicemonitor"
)
22
23
std
::
vector
<
QCameraDeviceMonitor
::
CameraNode
>
QCameraDeviceMonitor
::
cameraNodes
()
const
24
{
25
using
namespace
QtMultimediaPrivate
;
26
27
std
::
vector
<
CameraNode
>
result
= [&] {
28
std
::
lock_guard
guard
{
m_mutex
};
29
return
m_nodes
|
views
::
values
|
ranges
::
to
<
std
::
vector
>();
30
}();
31
32
ranges
::
sort
(
result
, [](
const
CameraNode
&
lhs
,
const
CameraNode
&
rhs
) {
33
return
cameraDeviceId
(
lhs
.
properties
) <
cameraDeviceId
(
rhs
.
properties
);
34
});
35
36
return
result
;
37
}
38
39
QCameraDeviceMonitor
::
QCameraDeviceMonitor
()
40
{
41
if
(!
QThread
::
isMainThread
())
42
// ensure that device monitor runs on application thread
43
moveToThread
(
qApp
->
thread
());
44
}
45
46
std
::
optional
<
ObjectId
>
QCameraDeviceMonitor
::
findCameraNode
(
QByteArrayView
deviceId
)
const
47
{
48
std
::
lock_guard
guard
{
m_mutex
};
49
50
for
(
const
auto
&[
id
,
node
] :
m_nodes
) {
51
if
(
cameraDeviceId
(
node
.
properties
) ==
deviceId
)
52
return
id
;
53
}
54
55
return
std
::
nullopt
;
56
}
57
58
QtPipeWire
::
DmaBufStrategy
QCameraDeviceMonitor
::
dmaBufStrategy
(
QByteArrayView
deviceId
)
const
59
{
60
std
::
lock_guard
guard
{
m_mutex
};
61
62
for
(
const
auto
&[
id
,
node
] :
m_nodes
) {
63
if
(
cameraDeviceId
(
node
.
properties
) ==
deviceId
)
64
return
QtPipeWire
::
dmaBufStrategy
(
node
.
deviceProperties
);
65
}
66
67
return
QtPipeWire
::
DmaBufStrategy
::
None
;
68
}
69
70
std
::
optional
<
QByteArray
>
QCameraDeviceMonitor
::
findTargetObject
(
QByteArrayView
deviceId
)
const
71
{
72
std
::
lock_guard
guard
{
m_mutex
};
73
74
for
(
const
auto
&[
id
,
node
] :
m_nodes
) {
75
if
(
cameraDeviceId
(
node
.
properties
) !=
deviceId
)
76
continue
;
77
78
// The serial is unambiguous and survives a node name being reused.
79
if
(
std
::
optional
<
ObjectSerial
>
serial
=
getObjectSerial
(
node
.
properties
))
80
return
QByteArray
::
number
(
qulonglong
(
serial
->
value
));
81
82
if
(
std
::
optional
<
std
::
string_view
>
nodeName
=
getNodeName
(
node
.
properties
))
83
return
QByteArray
(
nodeName
->
data
(),
qsizetype
(
nodeName
->
size
()));
84
85
return
std
::
nullopt
;
86
}
87
88
return
std
::
nullopt
;
89
}
90
91
const
PwPropertyDict
*
92
QCameraDeviceMonitor
::
findDeviceProperties
(
const
PwPropertyDict
&
nodeProperties
)
const
93
{
94
const
std
::
optional
<
ObjectId
>
deviceId
=
getDeviceId
(
nodeProperties
);
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
105
namespace
{
106
107
std::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
147
QList
<
QCameraDevice
>
QCameraDeviceMonitor
::
cameraDevices
()
148
{
149
QCameraContextManager
&
context
= *
QCameraContextManager
::
instance
();
150
Q_ASSERT
(!
context
.
isInPwThreadLoop
());
151
152
// Make sure everything the server had to say about the graph has arrived
153
// before deciding which cameras there are.
154
context
.
syncRegistry
();
155
156
QList
<
QCameraDevice
>
result
;
157
158
for
(
const
CameraNode
&
node
:
cameraNodes
()) {
159
const
QByteArray
id
=
cameraDeviceId
(
node
.
properties
);
160
if
(
id
.
isEmpty
())
161
continue
;
162
163
const
std
::
vector
<
SpaObjectVideoFormat
>
formats
=
enumerateNodeFormats
(
context
,
node
.
id
);
164
165
auto
info
=
std
::
make_unique
<
QCameraDevicePrivate
>();
166
info
->
id
=
id
;
167
info
->
description
=
cameraDescription
(
node
.
properties
,
node
.
deviceProperties
);
168
info
->
position
=
cameraPosition
(
node
.
properties
);
169
info
->
videoFormats
=
toCameraFormats
(
formats
);
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
) {
177
if
(!
info
->
photoResolutions
.
contains
(
format
.
resolution
()))
178
info
->
photoResolutions
.
push_back
(
format
.
resolution
());
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.
183
info
->
isDefault
=
result
.
isEmpty
();
184
185
result
.
push_back
(
QCameraDevicePrivate
::
create
(
std
::
move
(
info
)));
186
}
187
188
return
result
;
189
}
190
191
void
QCameraDeviceMonitor
::
objectAdded
(
ObjectId
id
,
PipewireRegistryType
type
,
192
const
spa_dict
&
properties
)
193
{
194
using
namespace
QtMultimediaPrivate
;
195
bool
changed
=
false
;
196
197
switch
(
type
) {
198
case
PipewireRegistryType
::
Device
: {
199
PwPropertyDict
dict
=
toPropertyDict
(
properties
);
200
201
std
::
lock_guard
guard
{
m_mutex
};
202
m_devices
[
id
] =
DeviceRecord
{
std
::
move
(
dict
) };
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
) {
208
node
.
deviceProperties
=
m_devices
[
id
].
properties
;
209
changed
=
true
;
210
}
211
}
212
break
;
213
}
214
215
case
PipewireRegistryType
::
Node
: {
216
PwPropertyDict
dict
=
toPropertyDict
(
properties
);
217
if
(!
isCameraNode
(
dict
))
218
return
;
219
220
qCDebug
(
lcPipewireCameraMonitor
) <<
"camera node added"
<<
id
<<
cameraDeviceId
(
dict
);
221
222
CameraNode
node
{
223
id
,
224
std
::
move
(
dict
),
225
PwPropertyDict
{},
226
};
227
228
std
::
lock_guard
guard
{
m_mutex
};
229
if
(
const
PwPropertyDict
*
deviceProperties
=
findDeviceProperties
(
node
.
properties
))
230
node
.
deviceProperties
= *
deviceProperties
;
231
232
m_nodes
.
insert_or_assign
(
id
,
std
::
move
(
node
));
233
changed
=
true
;
234
break
;
235
}
236
237
default
:
238
return
;
239
}
240
241
if
(
changed
)
242
emit
camerasChanged
();
243
}
244
245
void
QCameraDeviceMonitor
::
objectRemoved
(
ObjectId
id
)
246
{
247
bool
changed
=
false
;
248
249
{
250
std
::
lock_guard
guard
{
m_mutex
};
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
) {
262
node
.
deviceProperties
.
clear
();
263
changed
=
true
;
264
}
265
}
266
}
267
}
268
269
if
(
changed
)
270
emit
camerasChanged
();
271
}
272
273
}
// namespace QtPipeWire
274
275
QT_END_NAMESPACE
QtPipeWire
Definition
qpipewire_async_support.cpp:10
QtPipeWire::Q_STATIC_LOGGING_CATEGORY
Q_STATIC_LOGGING_CATEGORY(lcPipewireAudioSink, "qt.multimedia.pipewire.audiosink")
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qtmultimedia
src
multimedia
pipewire
qpipewire_cameradevicemonitor.cpp
Generated on
for Qt by
1.18.0