summaryrefslogtreecommitdiffstats
path: root/src/multimedia/camera/qcamerainfo.cpp
blob: dcd96b71c652714abe3631383c6275f647eafcec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qcamerainfo.h"

#include "qcamera_p.h"
#include "qmediaserviceprovider_p.h"

#include <qvideodeviceselectorcontrol.h>
#include <qcamerainfocontrol.h>

QT_BEGIN_NAMESPACE

/*!
    \class QCameraInfo
    \brief The QCameraInfo class provides general information about camera devices.
    \since 5.3
    \inmodule QtMultimedia
    \ingroup multimedia
    \ingroup multimedia_camera

    QCameraInfo lets you query for camera devices that are currently available on the system.

    The static functions defaultCamera() and availableCameras() provide you a list of all
    available cameras.

    This example prints the name of all available cameras:

    \snippet multimedia-snippets/camera.cpp Camera listing

    A QCameraInfo can be used to construct a QCamera. The following example instantiates a QCamera
    whose camera device is named 'mycamera':

    \snippet multimedia-snippets/camera.cpp Camera selection

    You can also use QCameraInfo to get general information about a camera device such as
    description, physical position on the system, or camera sensor orientation.

    \snippet multimedia-snippets/camera.cpp Camera info

    \sa QCamera
*/

class QCameraInfoPrivate
{
public:
    QCameraInfoPrivate() : isNull(true), position(QCamera::UnspecifiedPosition), orientation(0)
    { }

    bool isNull;
    QString deviceName;
    QString description;
    QCamera::Position position;
    int orientation;
};

/*!
    Constructs a camera info object for \a camera.

    You can use it to query information about the \a camera object passed as argument.

    If the \a camera is invalid, for example when no camera device is available on the system,
    the QCameraInfo object will be invalid and isNull() will return true.
*/
QCameraInfo::QCameraInfo(const QCamera &camera)
    : d(new QCameraInfoPrivate)
{
    const QVideoDeviceSelectorControl *deviceControl = camera.d_func()->deviceControl;
    if (deviceControl && deviceControl->deviceCount() > 0) {
        const int selectedDevice = deviceControl->selectedDevice();
        d->deviceName = deviceControl->deviceName(selectedDevice);
        d->description = deviceControl->deviceDescription(selectedDevice);
        d->isNull = false;
    }

    const QCameraInfoControl *infoControl = camera.d_func()->infoControl;
    if (infoControl) {
        d->position = infoControl->cameraPosition(d->deviceName);
        d->orientation = infoControl->cameraOrientation(d->deviceName);
        d->isNull = false;
    }
}

/*!
    Constructs a camera info object from a camera device \a name.

    If no such device exists, the QCameraInfo object will be invalid and isNull() will return true.
*/
QCameraInfo::QCameraInfo(const QByteArray &name)
    : d(new QCameraInfoPrivate)
{
    if (!name.isNull()) {
        QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider();
        const QByteArray service(Q_MEDIASERVICE_CAMERA);
        if (provider->devices(service).contains(name)) {
            d->deviceName = QString::fromLatin1(name);
            d->description = provider->deviceDescription(service, name);
            d->position = provider->cameraPosition(name);
            d->orientation = provider->cameraOrientation(name);
            d->isNull = false;
        }
    }
}

/*!
    Constructs a copy of \a other.
*/
QCameraInfo::QCameraInfo(const QCameraInfo &other)
    : d(other.d)
{
}

/*!
    Destroys the QCameraInfo.
*/
QCameraInfo::~QCameraInfo()
{
}

/*!
    Returns true if this QCameraInfo is equal to \a other.
*/
bool QCameraInfo::operator==(const QCameraInfo &other) const
{
    if (d == other.d)
        return true;

    return (d->deviceName == other.d->deviceName
            && d->description == other.d->description
            && d->position == other.d->position
            && d->orientation == other.d->orientation);
}

/*!
    Returns true if this QCameraInfo is null or invalid.
*/
bool QCameraInfo::isNull() const
{
    return d->isNull;
}

/*!
    Returns the device name of the camera

    This is a unique ID to identify the camera and may not be human-readable.
*/
QString QCameraInfo::deviceName() const
{
    return d->deviceName;
}

/*!
    Returns the human-readable description of the camera.
*/
QString QCameraInfo::description() const
{
    return d->description;
}

/*!
    Returns the physical position of the camera on the hardware system.
*/
QCamera::Position QCameraInfo::position() const
{
    return d->position;
}

/*!
    Returns the physical orientation of the camera sensor.

    The value is the orientation angle (clockwise, in steps of 90 degrees) of the camera sensor
    in relation to the display in its natural orientation.

    You can show the camera image in the correct orientation by rotating it by this value in the
    anti-clockwise direction.

    For example, suppose a mobile device which is naturally in portrait orientation. The back-facing
    camera is mounted in landscape. If the top side of the camera sensor is aligned with the
    right edge of the screen in natural orientation, the value should be 270. If the top side of a
    front-facing camera sensor is aligned with the right of the screen, the value should be 90.
*/
int QCameraInfo::orientation() const
{
    return d->orientation;
}

/*!
    Returns the default camera on the system.

    The returned object should be checked using isNull() before being used, in case there is no
    default camera or no cameras at all.

    \sa availableCameras()
*/
QCameraInfo QCameraInfo::defaultCamera()
{
    return QCameraInfo(QMediaServiceProvider::defaultServiceProvider()->defaultDevice(Q_MEDIASERVICE_CAMERA));
}

/*!
    Returns a list of available cameras on the system which are located at \a position.

    If \a position is not specified or if the value is QCamera::UnspecifiedPosition, a list of
    all available cameras will be returned.
*/
QList<QCameraInfo> QCameraInfo::availableCameras(QCamera::Position position)
{
    QList<QCameraInfo> cameras;

    const QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider();
    const QByteArray service(Q_MEDIASERVICE_CAMERA);
    const QList<QByteArray> devices = provider->devices(service);
    for (int i = 0; i < devices.count(); ++i) {
        const QByteArray &name = devices.at(i);
        if (position == QCamera::UnspecifiedPosition
                || position == provider->cameraPosition(name)) {
            cameras.append(QCameraInfo(name));
        }
    }

    return cameras;
}

/*!
    Sets the QCameraInfo object to be equal to \a other.
*/
QCameraInfo& QCameraInfo::operator=(const QCameraInfo& other)
{
    d = other.d;
    return *this;
}

/*!
    \fn QCameraInfo::operator!=(const QCameraInfo &other) const

    Returns true if this QCameraInfo is different from \a other.
*/

#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug d, const QCameraInfo &camera)
{
    d.maybeSpace() << QStringLiteral("QCameraInfo(deviceName=%1, position=%2, orientation=%3)")
                          .arg(camera.deviceName())
                          .arg(QString::fromLatin1(QCamera::staticMetaObject.enumerator(QCamera::staticMetaObject.indexOfEnumerator("Position"))
                               .valueToKey(camera.position())))
                          .arg(camera.orientation());
    return d.space();
}
#endif

QT_END_NAMESPACE