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 <QtCore/qstring.h> |
41 | #include <QtCore/qdebug.h> |
42 | #include <QtCore/QDir> |
43 | #include <QtCore/QDebug> |
44 | |
45 | #include "camerabinserviceplugin.h" |
46 | |
47 | #include "camerabinservice.h" |
48 | #include <private/qgstutils_p.h> |
49 | |
50 | QT_BEGIN_NAMESPACE |
51 | |
52 | template <typename T, int N> static int lengthOf(const T(&)[N]) { return N; } |
53 | |
54 | CameraBinServicePlugin::CameraBinServicePlugin() |
55 | : m_sourceFactory(0) |
56 | { |
57 | } |
58 | |
59 | CameraBinServicePlugin::~CameraBinServicePlugin() |
60 | { |
61 | if (m_sourceFactory) |
62 | gst_object_unref(GST_OBJECT(m_sourceFactory)); |
63 | } |
64 | |
65 | QMediaService* CameraBinServicePlugin::create(const QString &key) |
66 | { |
67 | QGstUtils::initializeGst(); |
68 | |
69 | if (key == QLatin1String(Q_MEDIASERVICE_CAMERA)) { |
70 | if (!CameraBinService::isCameraBinAvailable()) { |
71 | guint major, minor, micro, nano; |
72 | gst_version(major: &major, minor: &minor, micro: µ, nano: &nano); |
73 | qWarning(msg: "Error: cannot create camera service, the 'camerabin' plugin is missing for " |
74 | "GStreamer %u.%u." |
75 | "\nPlease install the 'bad' GStreamer plugin package." , |
76 | major, minor); |
77 | return nullptr; |
78 | } |
79 | |
80 | return new CameraBinService(sourceFactory()); |
81 | } |
82 | |
83 | qWarning() << "Gstreamer camerabin service plugin: unsupported key:" << key; |
84 | return 0; |
85 | } |
86 | |
87 | void CameraBinServicePlugin::release(QMediaService *service) |
88 | { |
89 | delete service; |
90 | } |
91 | |
92 | QMediaServiceProviderHint::Features CameraBinServicePlugin::supportedFeatures( |
93 | const QByteArray &service) const |
94 | { |
95 | if (service == Q_MEDIASERVICE_CAMERA) |
96 | return QMediaServiceProviderHint::VideoSurface; |
97 | |
98 | return QMediaServiceProviderHint::Features(); |
99 | } |
100 | |
101 | QByteArray CameraBinServicePlugin::defaultDevice(const QByteArray &service) const |
102 | { |
103 | return service == Q_MEDIASERVICE_CAMERA |
104 | ? QGstUtils::enumerateCameras(factory: sourceFactory()).value(i: 0).name.toUtf8() |
105 | : QByteArray(); |
106 | } |
107 | |
108 | QList<QByteArray> CameraBinServicePlugin::devices(const QByteArray &service) const |
109 | { |
110 | |
111 | return service == Q_MEDIASERVICE_CAMERA |
112 | ? QGstUtils::cameraDevices(factory: m_sourceFactory) |
113 | : QList<QByteArray>(); |
114 | } |
115 | |
116 | QString CameraBinServicePlugin::deviceDescription(const QByteArray &service, const QByteArray &deviceName) |
117 | { |
118 | return service == Q_MEDIASERVICE_CAMERA |
119 | ? QGstUtils::cameraDescription(device: deviceName, factory: m_sourceFactory) |
120 | : QString(); |
121 | } |
122 | |
123 | QVariant CameraBinServicePlugin::deviceProperty(const QByteArray &service, const QByteArray &device, const QByteArray &property) |
124 | { |
125 | Q_UNUSED(service); |
126 | Q_UNUSED(device); |
127 | Q_UNUSED(property); |
128 | return QVariant(); |
129 | } |
130 | |
131 | QCamera::Position CameraBinServicePlugin::cameraPosition(const QByteArray &deviceName) const |
132 | { |
133 | return QGstUtils::cameraPosition(device: deviceName, factory: m_sourceFactory); |
134 | } |
135 | |
136 | int CameraBinServicePlugin::cameraOrientation(const QByteArray &deviceName) const |
137 | { |
138 | return QGstUtils::cameraOrientation(device: deviceName, factory: m_sourceFactory); |
139 | } |
140 | |
141 | GstElementFactory *CameraBinServicePlugin::sourceFactory() const |
142 | { |
143 | if (!m_sourceFactory) { |
144 | GstElementFactory *factory = 0; |
145 | const QByteArray envCandidate = qgetenv(varName: "QT_GSTREAMER_CAMERABIN_SRC" ); |
146 | if (!envCandidate.isEmpty()) |
147 | factory = gst_element_factory_find(name: envCandidate.constData()); |
148 | |
149 | static const char *candidates[] = { "subdevsrc" , "wrappercamerabinsrc" }; |
150 | for (int i = 0; !factory && i < lengthOf(candidates); ++i) |
151 | factory = gst_element_factory_find(name: candidates[i]); |
152 | |
153 | if (factory) { |
154 | m_sourceFactory = GST_ELEMENT_FACTORY(gst_plugin_feature_load( |
155 | GST_PLUGIN_FEATURE(factory))); |
156 | gst_object_unref(object: (GST_OBJECT(factory))); |
157 | } |
158 | } |
159 | |
160 | return m_sourceFactory; |
161 | } |
162 | |
163 | QT_END_NAMESPACE |
164 | |