File indexing completed on 2024-05-12 04:44:37

0001 /*
0002     Copyright (C) 2011-2012 vlc-phonon AUTHORS <kde-multimedia@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 of the License, or (at your option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Lesser General Public License for more details.
0013 
0014     You should have received a copy of the GNU Lesser General Public
0015     License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0016 */
0017 
0018 #include "libvlc.h"
0019 
0020 #include <QtCore/QByteArray>
0021 #include <QtCore/QCoreApplication>
0022 #include <QtCore/QDebug>
0023 #include <QtCore/QDir>
0024 #include <QtCore/QSettings>
0025 #include <QtCore/QString>
0026 #include <QtCore/QStringBuilder>
0027 #include <QtCore/QVarLengthArray>
0028 
0029 #include <phonon/pulsesupport.h>
0030 
0031 #include <vlc/libvlc.h>
0032 #include <vlc/libvlc_version.h>
0033 
0034 #include "debug.h"
0035 
0036 LibVLC *LibVLC::self;
0037 
0038 LibVLC::LibVLC()
0039     : m_vlcInstance(0)
0040 {
0041 }
0042 
0043 LibVLC::~LibVLC()
0044 {
0045     if (m_vlcInstance)
0046         libvlc_release(m_vlcInstance);
0047     self = 0;
0048 }
0049 
0050 bool LibVLC::init()
0051 {
0052     Q_ASSERT_X(!self, "LibVLC", "there should be only one LibVLC object");
0053     LibVLC::self = new LibVLC;
0054 
0055     QList<QByteArray> args;
0056 
0057     // Ends up as something like $HOME/.config/Phonon/vlc.conf
0058     const QString configFileName = QSettings("Phonon", "vlc").fileName();
0059     if (QFile::exists(configFileName)) {
0060         args << QByteArray("--config=").append(QFile::encodeName(configFileName));
0061         args << "--no-ignore-config";
0062     }
0063 
0064     int debugLevel = qgetenv("PHONON_SUBSYSTEM_DEBUG").toInt();
0065     if (debugLevel > 0) {
0066         args << QByteArray("--verbose=").append(QByteArray::number(debugLevel));
0067         args << QByteArray("--extraintf=logger");
0068 #ifdef Q_WS_WIN
0069         QDir logFilePath(QString(qgetenv("APPDATA")).append("/vlc"));
0070 #else
0071         QDir logFilePath(QDir::homePath().append("/.vlc"));
0072 #endif //Q_WS_WIN
0073         logFilePath.mkdir("log");
0074         const QString logFile = logFilePath.path()
0075                 .append("/log/vlc-log-")
0076                 .append(QString::number(qApp->applicationPid()))
0077                 .append(".txt");
0078         args << QByteArray("--logfile=").append(QFile::encodeName(QDir::toNativeSeparators(logFile)));
0079     }
0080 
0081     args << "--no-media-library";
0082     args << "--no-osd";
0083     args << "--no-stats";
0084     // By default VLC will put a picture-in-picture when making a snapshot.
0085     // This is unexpected behaviour for us, so we force it off.
0086     args << "--no-snapshot-preview";
0087     // Do not load xlib dependent modules as we cannot ensure proper init
0088     // order as expected by xlib thus leading to crashes.
0089     // KDE BUG: 240001
0090     args << "--no-xlib";
0091     // Do not preload services discovery modules, we don't use them.
0092     args << "--services-discovery=''";
0093     // The application is meant to manage this. Also, using the builtin
0094     // inhibitor may cause problems on shutdown if VLC tries to uninhibit too
0095     // late in the application lifecycle.
0096 #if (LIBVLC_VERSION_INT >= LIBVLC_VERSION(4, 0, 0, 0))
0097     args << "--disable-screensaver=0";
0098 #else
0099     args << "--no-disable-screensaver";
0100 #endif
0101     // Allow multiple starts (one gets to wonder whether that makes a difference).
0102 #if !defined(Q_OS_MAC) && (defined(Q_OS_WIN) || !defined(PHONON_NO_DBUS))
0103     args << "--no-one-instance";
0104 #endif
0105     args << "--no-audio";
0106     args << "--no-video";
0107     // 6 seconds disk read buffer (up from vlc 2.1 default of 300ms) when using alsa, prevents most buffer underruns
0108     // when the disk is very busy. We expect the pulse buffer after decoding to solve the same problem.
0109     Phonon::PulseSupport *pulse = Phonon::PulseSupport::getInstance();
0110     if (!pulse || !pulse->isActive()) {
0111         args << "--file-caching=6000";
0112     }
0113 
0114     // Build const char* array
0115     QVarLengthArray<const char *, 64> vlcArgs(args.size());
0116     for (int i = 0; i < args.size(); ++i) {
0117         vlcArgs[i] = args.at(i).constData();
0118     }
0119 
0120     // Create and initialize a libvlc instance (it should be done only once)
0121     self->m_vlcInstance = libvlc_new(vlcArgs.size(), vlcArgs.constData());
0122     if (!self->m_vlcInstance) {
0123         fatal() << "libVLC: could not initialize";
0124         return false;
0125     }
0126     return true;
0127 }
0128 
0129 const char *LibVLC::errorMessage()
0130 {
0131     return libvlc_errmsg();
0132 }