1 /*****************************************************************************
2 * VLC backend for the Phonon library *
3 * Copyright (C) 2007-2008 Tanguy Krotoff <tkrotoff@gmail.com> *
4 * Copyright (C) 2008 Lukas Durfina <lukas.durfina@gmail.com> *
5 * Copyright (C) 2009 Fathi Boudra <fabo@kde.org> *
7 * This program is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU Lesser General Public *
9 * License as published by the Free Software Foundation; either *
10 * version 3 of the License, or (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15 * Lesser General Public License for more details. *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with this package; if not, write to the Free Software *
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 *****************************************************************************/
22 #include "vlcloader.h"
24 #include <QtCore/QDebug>
25 #include <QtCore/QDir>
26 #include <QtCore/QLibrary>
27 #include <QtCore/QSettings>
28 #include <QtCore/QString>
29 #include <QtCore/QStringList>
32 libvlc_instance_t *vlc_instance = 0;
33 libvlc_exception_t *vlc_exception = new libvlc_exception_t();
34 libvlc_media_player_t *vlc_current_media_player = 0;
44 vlc_exception = new libvlc_exception_t();
46 QString path = vlcPath();
47 if (!path.isEmpty()) {
48 QString pluginsPath = QString("--plugin-path=") + QDir::toNativeSeparators(QFileInfo(vlcPath()).dir().path());
49 #if defined(Q_OS_UNIX)
50 pluginsPath.append("/vlc");
51 #elif defined(Q_OS_WIN)
52 pluginsPath.append("\\plugins");
54 QByteArray p = path.toLatin1();
55 QByteArray pp = pluginsPath.toLatin1();
56 // VLC command line options. See vlc --full-help
57 const char *vlcArgs[] = {
64 "--reset-plugins-cache",
69 "--no-video-title-show"
72 libvlc_exception_init(vlc_exception);
74 // Create and initialize a libvlc instance (it should be done only once)
75 vlc_instance = libvlc_new(sizeof(vlcArgs) / sizeof(*vlcArgs),
88 libvlc_release(vlc_instance);
93 void vlcExceptionRaised()
95 if (libvlc_exception_raised(vlc_exception)) {
96 qDebug() << "libvlc exception:" << libvlc_errmsg();
97 libvlc_exception_clear(vlc_exception);
101 #if defined(Q_OS_UNIX)
102 static bool libGreaterThan(const QString &lhs, const QString &rhs)
104 QStringList lhsparts = lhs.split(QLatin1Char('.'));
105 QStringList rhsparts = rhs.split(QLatin1Char('.'));
106 Q_ASSERT(lhsparts.count() > 1 && rhsparts.count() > 1);
108 for (int i = 1; i < rhsparts.count(); ++i) {
109 if (lhsparts.count() <= i)
110 // left hand side is shorter, so it's less than rhs
115 int a = lhsparts.at(i).toInt(&ok);
117 b = rhsparts.at(i).toInt(&ok);
119 // both toInt succeeded
124 // compare as strings;
125 if (lhsparts.at(i) == rhsparts.at(i))
127 return lhsparts.at(i) > rhsparts.at(i);
131 // they compared strictly equally so far
132 // lhs cannot be less than rhs
137 static QStringList findAllLibVlc()
140 #if defined(Q_OS_UNIX)
141 paths = QString::fromLatin1(qgetenv("LD_LIBRARY_PATH"))
142 .split(QLatin1Char(':'), QString::SkipEmptyParts);
143 paths << QLatin1String("/usr/lib") << QLatin1String("/usr/local/lib");
145 QStringList foundVlcs;
146 foreach (const QString &path, paths) {
147 QDir dir = QDir(path);
148 QStringList entryList = dir.entryList(QStringList() << QLatin1String("libvlc.*"), QDir::Files);
150 qSort(entryList.begin(), entryList.end(), libGreaterThan);
151 foreach (const QString &entry, entryList)
152 foundVlcs << path + QLatin1Char('/') + entry;
156 #elif defined(Q_OS_WIN)
157 // Read VLC version and installation directory from Windows registry
158 QSettings settings(QSettings::SystemScope, "VideoLAN", "VLC");
159 QString vlcVersion = settings.value("Version").toString();
160 QString vlcInstallDir = settings.value("InstallDir").toString();
161 if (vlcVersion.startsWith("1.0") && !vlcInstallDir.isEmpty()) {
162 paths << vlcInstallDir + QLatin1Char('\\') + "libvlc.dll";
165 return QStringList();
170 static QLibrary *vlcLibrary = 0;
175 if (!path.isEmpty()) {
179 vlcLibrary = new QLibrary();
180 QStringList paths = findAllLibVlc();
181 foreach(path, paths) {
182 vlcLibrary->setFileName(path);
184 if (vlcLibrary->resolve("libvlc_exception_init")) {
187 qDebug("Cannot resolve the symbol or load VLC library");
189 qWarning() << vlcLibrary->errorString();
199 vlcLibrary->unload();
205 } // Namespace Phonon::VLC