1// Copyright (C) 2016 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 QGSTREAMERCAMERACONTROL_H
5#define QGSTREAMERCAMERACONTROL_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
18#include <private/qplatformcamera_p.h>
19#include <private/qmultimediautils_p.h>
20#include <QtCore/private/qexpected_p.h>
21#include <QtCore/private/quniquehandle_types_p.h>
22
23#include <mediacapture/qgstreamermediacapturesession_p.h>
24#include <common/qgst_p.h>
25#include <common/qgstpipeline_p.h>
26
27QT_BEGIN_NAMESPACE
28
29class QGstreamerCameraBase : public QPlatformCamera
30{
31public:
32 using QPlatformCamera::QPlatformCamera;
33
34 virtual QGstElement gstElement() const = 0;
35};
36
37class QGstreamerCamera : public QGstreamerCameraBase
38{
39public:
40 static q23::expected<QPlatformCamera *, QString> create(QCamera *camera);
41
42 ~QGstreamerCamera() override;
43
44 bool isActive() const override;
45 void setActive(bool active) override;
46
47 void setCamera(const QCameraDevice &camera) override;
48 bool setCameraFormat(const QCameraFormat &format) override;
49
50 QGstElement gstElement() const override { return gstCameraBin; }
51#if QT_CONFIG(gstreamer_photography)
52 GstPhotography *photography() const;
53#endif
54
55 void setFocusMode(QCamera::FocusMode mode) override;
56 bool isFocusModeSupported(QCamera::FocusMode mode) const override;
57
58 void setFlashMode(QCamera::FlashMode mode) override;
59 bool isFlashModeSupported(QCamera::FlashMode mode) const override;
60 bool isFlashReady() const override;
61
62 void setExposureMode(QCamera::ExposureMode) override;
63 bool isExposureModeSupported(QCamera::ExposureMode mode) const override;
64 void setExposureCompensation(float) override;
65 void setManualIsoSensitivity(int) override;
66 int isoSensitivity() const override;
67 void setManualExposureTime(float) override;
68 float exposureTime() const override;
69
70 bool isWhiteBalanceModeSupported(QCamera::WhiteBalanceMode mode) const override;
71 void setWhiteBalanceMode(QCamera::WhiteBalanceMode mode) override;
72 void setColorTemperature(int temperature) override;
73
74private:
75 QGstreamerCamera(QCamera *camera);
76
77 void updateCameraProperties();
78
79#if QT_CONFIG(linux_v4l)
80 bool isV4L2Camera() const;
81 void initV4L2Controls();
82 int setV4L2ColorTemperature(int temperature);
83 bool setV4L2Parameter(quint32 id, qint32 value);
84 int getV4L2Parameter(quint32 id) const;
85
86 bool v4l2AutoWhiteBalanceSupported = false;
87 bool v4l2ColorTemperatureSupported = false;
88 bool v4l2AutoExposureSupported = false;
89 bool v4l2ManualExposureSupported = false;
90 qint32 v4l2MinColorTemp = 5600; // Daylight...
91 qint32 v4l2MaxColorTemp = 5600;
92 qint32 v4l2MinExposure = 0;
93 qint32 v4l2MaxExposure = 0;
94 qint32 v4l2MinExposureAdjustment = 0;
95 qint32 v4l2MaxExposureAdjustment = 0;
96
97 template <typename Functor>
98 auto withV4L2DeviceFileDescriptor(Functor &&f) const
99 {
100 using ReturnType = std::invoke_result_t<Functor, int>;
101 Q_ASSERT(isV4L2Camera());
102
103 if (int gstreamerDeviceFd = gstCamera.getInt(property: "device-fd"); gstreamerDeviceFd != -1)
104 return f(gstreamerDeviceFd);
105
106 auto v4l2FileDescriptor = QUniqueFileDescriptorHandle{
107 QT_OPEN(pathname: m_v4l2DevicePath.toLocal8Bit().constData(), O_RDONLY),
108 };
109 if (!v4l2FileDescriptor) {
110 qWarning() << "Unable to open the camera" << m_v4l2DevicePath
111 << "for read to query the parameter info:" << qt_error_string(errno);
112 if constexpr (std::is_void_v<ReturnType>)
113 return;
114 else
115 return ReturnType{};
116 }
117 return f(v4l2FileDescriptor.get());
118 }
119#endif
120
121 QCameraDevice m_cameraDevice;
122
123 QGstBin gstCameraBin;
124 QGstElement gstCamera;
125 QGstElement gstCapsFilter;
126 QGstElement gstDecode;
127 QGstElement gstVideoConvert;
128 QGstElement gstVideoScale;
129
130 bool m_active = false;
131 QString m_v4l2DevicePath;
132
133 std::optional<QCameraFormat> m_currentCameraFormat;
134
135 template <typename Functor>
136 void updateCamera(Functor &&f)
137 {
138 QGstPipeline pipeline = gstVideoConvert.getPipeline();
139 if (pipeline)
140 pipeline.setState(GstState::GST_STATE_READY);
141
142 gstVideoConvert.sink().modifyPipelineInIdleProbe([&] {
143 f();
144 });
145
146 if (pipeline)
147 pipeline.setState(GstState::GST_STATE_PLAYING);
148 }
149};
150
151class QGstreamerCustomCamera : public QGstreamerCameraBase
152{
153public:
154 explicit QGstreamerCustomCamera(QCamera *);
155 explicit QGstreamerCustomCamera(QCamera *, QGstElement element);
156
157 QGstElement gstElement() const override { return gstCamera; }
158 void setCamera(const QCameraDevice &) override;
159
160 bool isActive() const override;
161 void setActive(bool) override;
162
163private:
164 QGstElement gstCamera;
165 bool m_active{};
166 const bool m_userProvidedGstElement;
167};
168
169QT_END_NAMESPACE
170
171#endif // QGSTREAMERCAMERACONTROL_H
172

source code of qtmultimedia/src/plugins/multimedia/gstreamer/mediacapture/qgstreamercamera_p.h