1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "camerabinlocks.h" |
41 | #include "camerabinsession.h" |
42 | #include "camerabinfocus.h" |
43 | #include "camerabinimageprocessing.h" |
44 | |
45 | #include <QtCore/qcoreevent.h> |
46 | |
47 | #include <gst/interfaces/photography.h> |
48 | |
49 | #include <QDebug> |
50 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | CameraBinLocks::CameraBinLocks(CameraBinSession *session) |
54 | :QCameraLocksControl(session), |
55 | m_session(session), |
56 | m_focus(m_session->cameraFocusControl()) |
57 | { |
58 | connect(sender: m_focus, SIGNAL(_q_focusStatusChanged(QCamera::LockStatus,QCamera::LockChangeReason)), |
59 | receiver: this, SLOT(updateFocusStatus(QCamera::LockStatus,QCamera::LockChangeReason))); |
60 | } |
61 | |
62 | CameraBinLocks::~CameraBinLocks() |
63 | { |
64 | } |
65 | |
66 | QCamera::LockTypes CameraBinLocks::supportedLocks() const |
67 | { |
68 | QCamera::LockTypes locks = QCamera::LockFocus; |
69 | |
70 | #if GST_CHECK_VERSION(1, 2, 0) |
71 | if (GstPhotography *photography = m_session->photography()) { |
72 | if (gst_photography_get_capabilities(photo: photography) & GST_PHOTOGRAPHY_CAPS_WB_MODE) |
73 | locks |= QCamera::LockWhiteBalance; |
74 | |
75 | if (GstElement *source = m_session->cameraSource()) { |
76 | if (g_object_class_find_property( |
77 | G_OBJECT_GET_CLASS(source), property_name: "exposure-mode" )) { |
78 | locks |= QCamera::LockExposure; |
79 | } |
80 | } |
81 | } |
82 | #endif |
83 | |
84 | return locks; |
85 | } |
86 | |
87 | QCamera::LockStatus CameraBinLocks::lockStatus(QCamera::LockType lock) const |
88 | { |
89 | switch (lock) { |
90 | case QCamera::LockFocus: |
91 | return m_focus->focusStatus(); |
92 | #if GST_CHECK_VERSION(1, 2, 0) |
93 | case QCamera::LockExposure: |
94 | if (m_pendingLocks & QCamera::LockExposure) |
95 | return QCamera::Searching; |
96 | return isExposureLocked() ? QCamera::Locked : QCamera::Unlocked; |
97 | case QCamera::LockWhiteBalance: |
98 | if (m_pendingLocks & QCamera::LockWhiteBalance) |
99 | return QCamera::Searching; |
100 | return isWhiteBalanceLocked() ? QCamera::Locked : QCamera::Unlocked; |
101 | #endif |
102 | default: |
103 | return QCamera::Unlocked; |
104 | } |
105 | } |
106 | |
107 | void CameraBinLocks::searchAndLock(QCamera::LockTypes locks) |
108 | { |
109 | m_pendingLocks &= ~locks; |
110 | |
111 | if (locks & QCamera::LockFocus) { |
112 | m_pendingLocks |= QCamera::LockFocus; |
113 | m_focus->_q_startFocusing(); |
114 | } |
115 | #if GST_CHECK_VERSION(1, 2, 0) |
116 | if (!m_pendingLocks) |
117 | m_lockTimer.stop(); |
118 | |
119 | if (locks & QCamera::LockExposure) { |
120 | if (isExposureLocked()) { |
121 | unlockExposure(status: QCamera::Searching, reason: QCamera::UserRequest); |
122 | m_pendingLocks |= QCamera::LockExposure; |
123 | m_lockTimer.start(msec: 1000, obj: this); |
124 | } else { |
125 | lockExposure(reason: QCamera::UserRequest); |
126 | } |
127 | } |
128 | if (locks & QCamera::LockWhiteBalance) { |
129 | if (isWhiteBalanceLocked()) { |
130 | unlockWhiteBalance(status: QCamera::Searching, reason: QCamera::UserRequest); |
131 | m_pendingLocks |= QCamera::LockWhiteBalance; |
132 | m_lockTimer.start(msec: 1000, obj: this); |
133 | } else { |
134 | lockWhiteBalance(reason: QCamera::UserRequest); |
135 | } |
136 | } |
137 | #endif |
138 | |
139 | } |
140 | |
141 | void CameraBinLocks::unlock(QCamera::LockTypes locks) |
142 | { |
143 | m_pendingLocks &= ~locks; |
144 | |
145 | if (locks & QCamera::LockFocus) |
146 | m_focus->_q_stopFocusing(); |
147 | |
148 | #if GST_CHECK_VERSION(1, 2, 0) |
149 | if (!m_pendingLocks) |
150 | m_lockTimer.stop(); |
151 | |
152 | if (locks & QCamera::LockExposure) |
153 | unlockExposure(status: QCamera::Unlocked, reason: QCamera::UserRequest); |
154 | if (locks & QCamera::LockWhiteBalance) |
155 | unlockWhiteBalance(status: QCamera::Unlocked, reason: QCamera::UserRequest); |
156 | #endif |
157 | } |
158 | |
159 | void CameraBinLocks::updateFocusStatus(QCamera::LockStatus status, QCamera::LockChangeReason reason) |
160 | { |
161 | if (status != QCamera::Searching) |
162 | m_pendingLocks &= ~QCamera::LockFocus; |
163 | |
164 | #if GST_CHECK_VERSION(1, 2, 0) |
165 | if (status == QCamera::Locked && !m_lockTimer.isActive()) { |
166 | if (m_pendingLocks & QCamera::LockExposure) |
167 | lockExposure(reason: QCamera::LockAcquired); |
168 | if (m_pendingLocks & QCamera::LockWhiteBalance) |
169 | lockWhiteBalance(reason: QCamera::LockAcquired); |
170 | } |
171 | #endif |
172 | emit lockStatusChanged(type: QCamera::LockFocus, status, reason); |
173 | } |
174 | |
175 | #if GST_CHECK_VERSION(1, 2, 0) |
176 | |
177 | void CameraBinLocks::timerEvent(QTimerEvent *event) |
178 | { |
179 | if (event->timerId() != m_lockTimer.timerId()) |
180 | return QCameraLocksControl::timerEvent(event); |
181 | |
182 | m_lockTimer.stop(); |
183 | |
184 | if (!(m_pendingLocks & QCamera::LockFocus)) { |
185 | if (m_pendingLocks & QCamera::LockExposure) |
186 | lockExposure(reason: QCamera::LockAcquired); |
187 | if (m_pendingLocks & QCamera::LockWhiteBalance) |
188 | lockWhiteBalance(reason: QCamera::LockAcquired); |
189 | } |
190 | } |
191 | |
192 | bool CameraBinLocks::isExposureLocked() const |
193 | { |
194 | if (GstElement *source = m_session->cameraSource()) { |
195 | GstPhotographyExposureMode exposureMode = GST_PHOTOGRAPHY_EXPOSURE_MODE_AUTO; |
196 | g_object_get (G_OBJECT(source), first_property_name: "exposure-mode" , &exposureMode, NULL); |
197 | return exposureMode == GST_PHOTOGRAPHY_EXPOSURE_MODE_MANUAL; |
198 | } else { |
199 | return false; |
200 | } |
201 | } |
202 | |
203 | void CameraBinLocks::lockExposure(QCamera::LockChangeReason reason) |
204 | { |
205 | GstElement *source = m_session->cameraSource(); |
206 | if (!source) |
207 | return; |
208 | |
209 | m_pendingLocks &= ~QCamera::LockExposure; |
210 | g_object_set( |
211 | G_OBJECT(source), |
212 | first_property_name: "exposure-mode" , |
213 | GST_PHOTOGRAPHY_EXPOSURE_MODE_MANUAL, |
214 | NULL); |
215 | emit lockStatusChanged(type: QCamera::LockExposure, status: QCamera::Locked, reason); |
216 | } |
217 | |
218 | void CameraBinLocks::unlockExposure(QCamera::LockStatus status, QCamera::LockChangeReason reason) |
219 | { |
220 | GstElement *source = m_session->cameraSource(); |
221 | if (!source) |
222 | return; |
223 | |
224 | g_object_set( |
225 | G_OBJECT(source), |
226 | first_property_name: "exposure-mode" , |
227 | GST_PHOTOGRAPHY_EXPOSURE_MODE_AUTO, |
228 | NULL); |
229 | emit lockStatusChanged(type: QCamera::LockExposure, status, reason); |
230 | } |
231 | |
232 | bool CameraBinLocks::isWhiteBalanceLocked() const |
233 | { |
234 | if (GstPhotography *photography = m_session->photography()) { |
235 | GstPhotographyWhiteBalanceMode whiteBalanceMode; |
236 | return gst_photography_get_white_balance_mode(photo: photography, wb_mode: &whiteBalanceMode) |
237 | && whiteBalanceMode == GST_PHOTOGRAPHY_WB_MODE_MANUAL; |
238 | } else { |
239 | return false; |
240 | } |
241 | } |
242 | |
243 | void CameraBinLocks::lockWhiteBalance(QCamera::LockChangeReason reason) |
244 | { |
245 | m_pendingLocks &= ~QCamera::LockWhiteBalance; |
246 | m_session->imageProcessingControl()->lockWhiteBalance(); |
247 | emit lockStatusChanged(type: QCamera::LockWhiteBalance, status: QCamera::Locked, reason); |
248 | } |
249 | |
250 | void CameraBinLocks::unlockWhiteBalance( |
251 | QCamera::LockStatus status, QCamera::LockChangeReason reason) |
252 | { |
253 | m_session->imageProcessingControl()->lockWhiteBalance(); |
254 | emit lockStatusChanged(type: QCamera::LockWhiteBalance, status, reason); |
255 | } |
256 | |
257 | #endif |
258 | |
259 | QT_END_NAMESPACE |
260 | |