1/*
2 Copyright (C) 2011-2012 vlc-phonon AUTHORS <kde-multimedia@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include "libvlc.h"
19
20#include <QtCore/QByteArray>
21#include <QtCore/QCoreApplication>
22#include <QtCore/QDebug>
23#include <QtCore/QDir>
24#include <QtCore/QSettings>
25#include <QtCore/QString>
26#include <QtCore/QStringBuilder>
27#include <QtCore/QVarLengthArray>
28
29#include <phonon/pulsesupport.h>
30
31#include <vlc/libvlc.h>
32#include <vlc/libvlc_version.h>
33
34#include "debug.h"
35
36LibVLC *LibVLC::self;
37
38LibVLC::LibVLC()
39 : m_vlcInstance(0)
40{
41}
42
43LibVLC::~LibVLC()
44{
45 if (m_vlcInstance)
46 libvlc_release(p_instance: m_vlcInstance);
47 self = 0;
48}
49
50bool LibVLC::init()
51{
52 Q_ASSERT_X(!self, "LibVLC", "there should be only one LibVLC object");
53 LibVLC::self = new LibVLC;
54
55 QList<QByteArray> args;
56
57 // Ends up as something like $HOME/.config/Phonon/vlc.conf
58 const QString configFileName = QSettings("Phonon", "vlc").fileName();
59 if (QFile::exists(fileName: configFileName)) {
60 args << QByteArray("--config=").append(a: QFile::encodeName(fileName: configFileName));
61 args << "--no-ignore-config";
62 }
63
64 int debugLevel = qgetenv(varName: "PHONON_SUBSYSTEM_DEBUG").toInt();
65 if (debugLevel > 0) {
66 args << QByteArray("--verbose=").append(a: QByteArray::number(debugLevel));
67 args << QByteArray("--extraintf=logger");
68#ifdef Q_WS_WIN
69 QDir logFilePath(QString(qgetenv("APPDATA")).append("/vlc"));
70#else
71 QDir logFilePath(QDir::homePath().append(s: "/.vlc"));
72#endif //Q_WS_WIN
73 logFilePath.mkdir(dirName: "log");
74 const QString logFile = logFilePath.path()
75 .append(s: "/log/vlc-log-")
76 .append(s: QString::number(qApp->applicationPid()))
77 .append(s: ".txt");
78 args << QByteArray("--logfile=").append(a: QFile::encodeName(fileName: QDir::toNativeSeparators(pathName: logFile)));
79 }
80
81 args << "--no-media-library";
82 args << "--no-osd";
83 args << "--no-stats";
84 // By default VLC will put a picture-in-picture when making a snapshot.
85 // This is unexpected behaviour for us, so we force it off.
86 args << "--no-snapshot-preview";
87 // Do not load xlib dependent modules as we cannot ensure proper init
88 // order as expected by xlib thus leading to crashes.
89 // KDE BUG: 240001
90 args << "--no-xlib";
91 // Do not preload services discovery modules, we don't use them.
92 args << "--services-discovery=''";
93 // The application is meant to manage this. Also, using the builtin
94 // inhibitor may cause problems on shutdown if VLC tries to uninhibit too
95 // late in the application lifecycle.
96#if (LIBVLC_VERSION_INT >= LIBVLC_VERSION(4, 0, 0, 0))
97 args << "--disable-screensaver=0";
98#else
99 args << "--no-disable-screensaver";
100#endif
101 // Allow multiple starts (one gets to wonder whether that makes a difference).
102#if !defined(Q_OS_MAC) && (defined(Q_OS_WIN) || !defined(PHONON_NO_DBUS))
103 args << "--no-one-instance";
104#endif
105 args << "--no-audio";
106 args << "--no-video";
107 // 6 seconds disk read buffer (up from vlc 2.1 default of 300ms) when using alsa, prevents most buffer underruns
108 // when the disk is very busy. We expect the pulse buffer after decoding to solve the same problem.
109 Phonon::PulseSupport *pulse = Phonon::PulseSupport::getInstance();
110 if (!pulse || !pulse->isActive()) {
111 args << "--file-caching=6000";
112 }
113
114 // Build const char* array
115 QVarLengthArray<const char *, 64> vlcArgs(args.size());
116 for (int i = 0; i < args.size(); ++i) {
117 vlcArgs[i] = args.at(i).constData();
118 }
119
120 // Create and initialize a libvlc instance (it should be done only once)
121 self->m_vlcInstance = libvlc_new(argc: vlcArgs.size(), argv: vlcArgs.constData());
122 if (!self->m_vlcInstance) {
123 fatal() << "libVLC: could not initialize";
124 return false;
125 }
126 return true;
127}
128
129const char *LibVLC::errorMessage()
130{
131 return libvlc_errmsg();
132}
133

source code of phonon-vlc/src/utils/libvlc.cpp