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 "qmediapluginloader_p.h"
41#include <QtCore/qcoreapplication.h>
42#include <QtCore/qdebug.h>
43#include <QtCore/qjsonarray.h>
44#include <private/qfactoryloader_p.h>
45
46#include "qmediaserviceproviderplugin.h"
47
48QT_BEGIN_NAMESPACE
49
50QMediaPluginLoader::QMediaPluginLoader(const char *iid, const QString &location, Qt::CaseSensitivity caseSensitivity):
51 m_iid(iid)
52{
53 m_location = QString::fromLatin1("/%1").arg(location);
54 m_factoryLoader = new QFactoryLoader(m_iid, m_location, caseSensitivity);
55 loadMetadata();
56}
57
58QMediaPluginLoader::~QMediaPluginLoader()
59{
60 delete m_factoryLoader;
61}
62
63QStringList QMediaPluginLoader::keys() const
64{
65 return m_metadata.keys();
66}
67
68QObject* QMediaPluginLoader::instance(QString const &key)
69{
70 if (!m_metadata.contains(key))
71 return nullptr;
72
73 int idx = m_metadata.value(key).first().value(QStringLiteral("index")).toDouble();
74 if (idx < 0)
75 return nullptr;
76
77 return m_factoryLoader->instance(idx);
78}
79
80QList<QObject*> QMediaPluginLoader::instances(QString const &key)
81{
82 if (!m_metadata.contains(key))
83 return QList<QObject*>();
84
85 QList<QString> keys;
86 QList<QObject *> objects;
87 const auto list = m_metadata.value(key);
88 for (const QJsonObject &jsonobj : list) {
89 int idx = jsonobj.value(QStringLiteral("index")).toDouble();
90 if (idx < 0)
91 continue;
92
93 QObject *object = m_factoryLoader->instance(idx);
94 if (!objects.contains(object)) {
95 QJsonArray arr = jsonobj.value(QStringLiteral("Keys")).toArray();
96 keys.append(!arr.isEmpty() ? arr.at(0).toString() : QStringLiteral(""));
97 objects.append(object);
98 }
99 }
100
101 static const bool showDebug = qEnvironmentVariableIntValue("QT_DEBUG_PLUGINS");
102 static const QStringList preferredPlugins =
103 qEnvironmentVariable("QT_MULTIMEDIA_PREFERRED_PLUGINS").split(QLatin1Char(','), QString::SkipEmptyParts);
104 for (int i = preferredPlugins.size() - 1; i >= 0; --i) {
105 auto name = preferredPlugins[i];
106 bool found = false;
107 for (int j = 0; j < keys.size(); ++j) {
108 if (!keys[j].startsWith(name))
109 continue;
110
111 auto obj = objects[j];
112 objects.removeAt(j);
113 objects.prepend(obj);
114 auto k = keys[j];
115 keys.removeAt(j);
116 keys.prepend(k);
117 found = true;
118 break;
119 }
120
121 if (showDebug && !found)
122 qWarning() << "QMediaPluginLoader: pattern" << name << "did not match any loaded plugin";
123 }
124
125 if (showDebug)
126 qDebug() << "QMediaPluginLoader: loaded plugins for key" << key << ":" << keys;
127
128 return objects;
129}
130
131void QMediaPluginLoader::loadMetadata()
132{
133#if !defined QT_NO_DEBUG
134 const bool showDebug = qgetenv("QT_DEBUG_PLUGINS").toInt() > 0;
135#endif
136
137#if !defined QT_NO_DEBUG
138 if (showDebug)
139 qDebug() << "QMediaPluginLoader: loading metadata for iid " << m_iid << " at location " << m_location;
140#endif
141
142 if (!m_metadata.isEmpty()) {
143#if !defined QT_NO_DEBUG
144 if (showDebug)
145 qDebug() << "QMediaPluginLoader: already loaded metadata, returning";
146#endif
147 return;
148 }
149
150 QList<QJsonObject> meta = m_factoryLoader->metaData();
151 for (int i = 0; i < meta.size(); i++) {
152 QJsonObject jsonobj = meta.at(i).value(QStringLiteral("MetaData")).toObject();
153 jsonobj.insert(QStringLiteral("index"), i);
154#if !defined QT_NO_DEBUG
155 if (showDebug)
156 qDebug() << "QMediaPluginLoader: Inserted index " << i << " into metadata: " << jsonobj;
157#endif
158
159 QJsonArray arr = jsonobj.value(QStringLiteral("Services")).toArray();
160 // Preserve compatibility with older plugins (made before 5.1) in which
161 // services were declared in the 'Keys' property
162 if (arr.isEmpty())
163 arr = jsonobj.value(QStringLiteral("Keys")).toArray();
164
165 for (const QJsonValue &value : qAsConst(arr)) {
166 QString key = value.toString();
167
168 if (!m_metadata.contains(key)) {
169#if !defined QT_NO_DEBUG
170 if (showDebug)
171 qDebug() << "QMediaPluginLoader: Inserting new list for key: " << key;
172#endif
173 m_metadata.insert(key, QList<QJsonObject>());
174 }
175
176 m_metadata[key].append(jsonobj);
177 }
178 }
179}
180
181QT_END_NAMESPACE
182
183