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_camera.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_camera_p.h"
5
6
#
include
<
QtMultimedia
/
private
/
qpipewire_cameracontextmanager_p
.
h
>
7
#
include
<
QtMultimedia
/
private
/
qpipewire_videostream_p
.
h
>
8
#
include
<
QtCore
/
qloggingcategory
.
h
>
9
10
QT_BEGIN_NAMESPACE
11
12
namespace
QtPipeWire
{
13
14
using
namespace
Qt
::
StringLiterals
;
15
16
Q_STATIC_LOGGING_CATEGORY
(
lcPipewireCamera
,
"qt.multimedia.pipewire.camera"
)
17
18
QPipeWireCamera
::
QPipeWireCamera
(
QCamera
*
camera
) :
QPlatformCamera
(
camera
)
19
{
20
Q_ASSERT
(
QCameraContextManager
::
isSupported
());
21
}
22
23
QPipeWireCamera
::~
QPipeWireCamera
()
24
{
25
const
bool
wasActive
=
m_active
;
26
stopStream
();
27
if
(
wasActive
)
28
emitEndOfStreamFrame
(
m_lastFrameStart
.
value_or
(
std
::
chrono
::
microseconds
{}));
29
}
30
31
bool
QPipeWireCamera
::
isActive
()
const
32
{
33
return
m_active
;
34
}
35
36
void
QPipeWireCamera
::
setActive
(
bool
active
)
37
{
38
if
(
m_active
==
active
)
39
return
;
40
41
if
(
active
&&
m_cameraDevice
.
isNull
())
42
return
;
43
44
if
(
m_cameraFormat
.
isNull
())
45
resolveCameraFormat
({});
46
47
if
(
active
) {
48
m_lastFrameStart
.
reset
();
49
if
(!
startStream
())
50
return
;
51
m_active
=
true
;
52
}
else
{
53
m_active
=
false
;
54
stopStream
();
55
emitEndOfStreamFrame
(
m_lastFrameStart
.
value_or
(
std
::
chrono
::
microseconds
{}));
56
}
57
58
emit
activeChanged
(
m_active
);
59
}
60
61
void
QPipeWireCamera
::
setCamera
(
const
QCameraDevice
&
camera
)
62
{
63
if
(
m_cameraDevice
==
camera
)
64
return
;
65
66
const
bool
wasActive
=
m_active
;
67
stopStream
();
68
69
m_cameraDevice
=
camera
;
70
resolveCameraFormat
({});
71
72
if
(
wasActive
)
73
startStream
();
74
}
75
76
bool
QPipeWireCamera
::
setCameraFormat
(
const
QCameraFormat
&
format
)
77
{
78
if
(!
format
.
isNull
() && !
m_cameraDevice
.
videoFormats
().
contains
(
format
))
79
return
false
;
80
81
if
(!
resolveCameraFormat
(
format
))
82
return
true
;
83
84
if
(
m_active
) {
85
stopStream
();
86
startStream
();
87
}
88
89
return
true
;
90
}
91
92
bool
QPipeWireCamera
::
resolveCameraFormat
(
const
QCameraFormat
&
format
)
93
{
94
QCameraFormat
resolved
=
format
;
95
if
(
resolved
.
isNull
())
96
resolved
=
findBestCameraFormat
(
m_cameraDevice
);
97
98
if
(
resolved
==
m_cameraFormat
)
99
return
false
;
100
101
m_cameraFormat
=
resolved
;
102
return
true
;
103
}
104
105
QVideoFrameFormat
QPipeWireCamera
::
frameFormat
()
const
106
{
107
// Until the stream has negotiated a format, report what was requested, so
108
// that a sink has something to work with.
109
if
(
m_stream
) {
110
QVideoFrameFormat
negotiated
=
m_stream
->
frameFormat
();
111
if
(
negotiated
.
isValid
())
112
return
negotiated
;
113
}
114
115
return
QPlatformCamera
::
frameFormat
();
116
}
117
118
bool
QPipeWireCamera
::
startStream
()
119
{
120
Q_ASSERT
(!
m_stream
);
121
122
QCameraContextManager
&
context
= *
QCameraContextManager
::
instance
();
123
124
const
std
::
optional
<
QByteArray
>
target
=
125
QCameraContextManager
::
deviceMonitor
().
findTargetObject
(
m_cameraDevice
.
id
());
126
if
(!
target
) {
127
const
QByteArray
id
=
m_cameraDevice
.
id
();
128
if
(!
id
.
startsWith
(
"pw:"
)) {
129
updateError
(
QCamera
::
CameraError
,
130
u"The camera id \"%1\" does not name a PipeWire camera. Camera ids are "
131
"not interchangeable between backends."_s
.
arg
(
QString
::
fromUtf8
(
id
)));
132
}
else
{
133
updateError
(
QCamera
::
CameraError
,
134
u"No PipeWire camera with the id \"%1\" is present."_s.
arg
(
135
QString
::
fromUtf8
(
id
)));
136
}
137
return
false
;
138
}
139
140
m_stream
=
std
::
make_unique
<
QPipeWireVideoStream
>(
141
context
, *
target
,
m_cameraFormat
.
pixelFormat
(),
m_cameraFormat
.
resolution
(),
142
m_cameraFormat
.
maxFrameRate
(),
143
QCameraContextManager
::
deviceMonitor
().
dmaBufStrategy
(
m_cameraDevice
.
id
()));
144
145
connect
(
m_stream
.
get
(), &
QPipeWireVideoStream
::
newVideoFrame
,
this
,
146
[
this
](
const
QVideoFrame
&
frame
) {
147
m_lastFrameStart
=
std
::
chrono
::
microseconds
{
frame
.
startTime
() };
148
Q_EMIT
newVideoFrame
(
frame
);
149
},
Qt
::
DirectConnection
);
150
connect
(
m_stream
.
get
(), &
QPipeWireVideoStream
::
errorOccurred
,
this
,
151
[
this
](
const
QString
&
errorString
) {
152
updateError
(
QCamera
::
CameraError
,
errorString
);
153
},
Qt
::
QueuedConnection
);
154
155
if
(!
m_stream
->
start
()) {
156
m_stream
.
reset
();
157
updateError
(
QCamera
::
CameraError
, u"Could not start the PipeWire camera stream."_s);
158
return
false
;
159
}
160
161
qCDebug
(
lcPipewireCamera
) <<
"streaming from"
<< *
target
<<
"as"
<<
m_cameraFormat
.
pixelFormat
()
162
<<
m_cameraFormat
.
resolution
();
163
return
true
;
164
}
165
166
void
QPipeWireCamera
::
stopStream
()
167
{
168
m_stream
.
reset
();
169
}
170
171
}
// namespace QtPipeWire
172
173
QT_END_NAMESPACE
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
QtPipeWire
Definition
qpipewire_async_support.cpp:10
QtPipeWire::Q_STATIC_LOGGING_CATEGORY
Q_STATIC_LOGGING_CATEGORY(lcPipewireAudioSink, "qt.multimedia.pipewire.audiosink")
qtmultimedia
src
multimedia
pipewire
qpipewire_camera.cpp
Generated on
for Qt by
1.18.0