<commit_before>/*  This file is part of the KDE project
    Copyright (C) 2006 <NAME> <<EMAIL>>
    Copyright (C) 2006-2007 <NAME> <<EMAIL>>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

*/

#include "backend.h"
#include <phonon/experimental/backendinterface.h>
#include "mediaobject.h"
#include "effect.h"
#include "events.h"
#include "audiooutput.h"
#include "audiodataoutput.h"
#include "nullsink.h"
#include "visualization.h"
#include "volumefadereffect.h"
#include "videodataoutput.h"
#include "videowidget.h"
#include "wirecall.h"
#include "xinethread.h"
#include "keepreference.h"
#include "sinknode.h"
#include "sourcenode.h"
#include "config-xine-widget.h"

#include <QtCore/QByteArray>
#include <QtCore/QThread>
#include <QtCore/QCoreApplication>
#include <QtCore/QSet>
#include <QtCore/QVariant>
#include <QtDBus/QDBusConnection>
#include <QtGui/QApplication>
#include <QtCore/QSet>
#include <QtCore/QVariant>
#include <QtCore/QtPlugin>
#include <QtCore/QSettings>

Q_EXPORT_PLUGIN2(phonon_xine, Phonon::Xine::Backend)

static Phonon::Xine::Backend *s_instance = 0;

namespace Phonon
{
namespace Xine
{

Backend *Backend::instance()
{
    Q_ASSERT(s_instance);
    return s_instance;
}

Backend::Backend(QObject *parent, const QVariantList &)
    : QObject(parent),
    m_inShutdown(false),
    m_debugMessages(!qgetenv("PHONON_XINE_DEBUG").isEmpty()),
    m_thread(0)
{
    Q_ASSERT(s_instance == 0);
    s_instance = this;

    m_xine.create();
    m_freeEngines << m_xine;

    setProperty("identifier",     QLatin1String("phonon_xine"));
    setProperty("backendName",    QLatin1String("Xine"));
    setProperty("backendComment", tr("Phonon Xine Backend"));
    setProperty("backendVersion", QLatin1String("0.1"));
    setProperty("backendIcon",    QLatin1String("phonon-xine"));
    setProperty("backendWebsite", QLatin1String("http://multimedia.kde.org/"));

    QSettings cg("kde.org", "Phonon-Xine");
    m_deinterlaceDVD = cg.value("Settings/deinterlaceDVD", true).toBool();
    m_deinterlaceVCD = cg.value("Settings/deinterlaceVCD", false).toBool();
    m_deinterlaceFile = cg.value("Settings/deinterlaceFile", false).toBool();
    m_deinterlaceMethod = cg.value("Settings/deinterlaceMethod", 0).toInt();

    signalTimer.setSingleShot(true);
    connect(&signalTimer, SIGNAL(timeout()), SLOT(emitAudioDeviceChange()));
    QDBusConnection::sessionBus().registerObject("/internal/PhononXine", this, QDBusConnection::ExportScriptableSlots);

    debug() << Q_FUNC_INFO << "Using Xine version " << xine_get_version_string();
}

Backend::~Backend()
{
    m_inShutdown = true;

    if (!m_cleanupObjects.isEmpty()) {
        Q_ASSERT(m_thread);
        QCoreApplication::postEvent(m_thread, new Event(Event::Cleanup));
        while (!m_cleanupObjects.isEmpty()) {
            XineThread::msleep(200); // static QThread::msleep, but that one is protected and XineThread is our friend
        }
    }

    if (m_thread) {
        m_thread->quit();
        m_thread->wait();
        delete m_thread;
    }

    s_instance = 0;
}

XineEngine Backend::xineEngineForStream()
{
    XineEngine e;
    if (s_instance->m_freeEngines.isEmpty()) {
        e.create();
    } else {
        e = s_instance->m_freeEngines.takeLast();
    }
    s_instance->m_usedEngines << e;
    return e;
}

void Backend::returnXineEngine(const XineEngine &e)
{
    s_instance->m_usedEngines.removeAll(e);
    s_instance->m_freeEngines << e;
    if (s_instance->m_freeEngines.size() > 5) {
        s_instance->m_freeEngines.takeLast();
        s_instance->m_freeEngines.takeLast();
        s_instance->m_freeEngines.takeLast();
    }
}

QObject *Backend::createObject(BackendInterface::Class c, QObject *parent, const QList<QVariant> &args)
{
    qWarning() << c;
    switch (c) {
    case MediaObjectClass:
        return new MediaObject(parent);
    case VolumeFaderEffectClass:
        return new VolumeFaderEffect(parent);
    case AudioOutputClass:
        return new AudioOutput(parent);
    case AudioDataOutputClass:
        return new AudioDataOutput(parent);
    case VisualizationClass:
        return new Visualization(parent);
    case VideoDataOutputClass:
        return 0;
    case Phonon::Experimental::BackendInterface::VideoDataOutputClass:
        return new VideoDataOutput(parent);
    case EffectClass:
        {
            Q_ASSERT(args.size() == 1);
            debug() << Q_FUNC_INFO << "creating Effect(" << args[0];
            Effect *e = new Effect(args[0].toInt(), parent);
            if (e->isValid()) {
                return e;
            }
            delete e;
            return 0;
        }
    case VideoWidgetClass:
        {
#ifdef XINEWIDGET_FOUND
            VideoWidget *vw = new VideoWidget(qobject_cast<QWidget *>(parent));<commit_msg>remove some useless debug output<commit_after>/*  This file is part of the KDE project
    Copyright (C) 2006 <NAME> <<EMAIL>>
    Copyright (C) 2006-2007 <NAME> <<EMAIL>>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

*/

#include "backend.h"
#include <phonon/experimental/backendinterface.h>
#include "mediaobject.h"
#include "effect.h"
#include "events.h"
#include "audiooutput.h"
#include "audiodataoutput.h"
#include "nullsink.h"
#include "visualization.h"
#include "volumefadereffect.h"
#include "videodataoutput.h"
#include "videowidget.h"
#include "wirecall.h"
#include "xinethread.h"
#include "keepreference.h"
#include "sinknode.h"
#include "sourcenode.h"
#include "config-xine-widget.h"

#include <QtCore/QByteArray>
#include <QtCore/QThread>
#include <QtCore/QCoreApplication>
#include <QtCore/QSet>
#include <QtCore/QVariant>
#include <QtDBus/QDBusConnection>
#include <QtGui/QApplication>
#include <QtCore/QSet>
#include <QtCore/QVariant>
#include <QtCore/QtPlugin>
#include <QtCore/QSettings>

Q_EXPORT_PLUGIN2(phonon_xine, Phonon::Xine::Backend)

static Phonon::Xine::Backend *s_instance = 0;

namespace Phonon
{
namespace Xine
{

Backend *Backend::instance()
{
    Q_ASSERT(s_instance);
    return s_instance;
}

Backend::Backend(QObject *parent, const QVariantList &)
    : QObject(parent),
    m_inShutdown(false),
    m_debugMessages(!qgetenv("PHONON_XINE_DEBUG").isEmpty()),
    m_thread(0)
{
    Q_ASSERT(s_instance == 0);
    s_instance = this;

    m_xine.create();
    m_freeEngines << m_xine;

    setProperty("identifier",     QLatin1String("phonon_xine"));
    setProperty("backendName",    QLatin1String("Xine"));
    setProperty("backendComment", tr("Phonon Xine Backend"));
    setProperty("backendVersion", QLatin1String("0.1"));
    setProperty("backendIcon",    QLatin1String("phonon-xine"));
    setProperty("backendWebsite", QLatin1String("http://multimedia.kde.org/"));

    QSettings cg("kde.org", "Phonon-Xine");
    m_deinterlaceDVD = cg.value("Settings/deinterlaceDVD", true).toBool();
    m_deinterlaceVCD = cg.value("Settings/deinterlaceVCD", false).toBool();
    m_deinterlaceFile = cg.value("Settings/deinterlaceFile", false).toBool();
    m_deinterlaceMethod = cg.value("Settings/deinterlaceMethod", 0).toInt();

    signalTimer.setSingleShot(true);
    connect(&signalTimer, SIGNAL(timeout()), SLOT(emitAudioDeviceChange()));
    QDBusConnection::sessionBus().registerObject("/internal/PhononXine", this, QDBusConnection::ExportScriptableSlots);

    debug() << Q_FUNC_INFO << "Using Xine version " << xine_get_version_string();
}

Backend::~Backend()
{
    m_inShutdown = true;

    if (!m_cleanupObjects.isEmpty()) {
        Q_ASSERT(m_thread);
        QCoreApplication::postEvent(m_thread, new Event(Event::Cleanup));
        while (!m_cleanupObjects.isEmpty()) {
            XineThread::msleep(200); // static QThread::msleep, but that one is protected and XineThread is our friend
        }
    }

    if (m_thread) {
        m_thread->quit();
        m_thread->wait();
        delete m_thread;
    }

    s_instance = 0;
}

XineEngine Backend::xineEngineForStream()
{
    XineEngine e;
    if (s_instance->m_freeEngines.isEmpty()) {
        e.create();
    } else {
        e = s_instance->m_freeEngines.takeLast();
    }
    s_instance->m_usedEngines << e;
    return e;
}

void Backend::returnXineEngine(const XineEngine &e)
{
    s_instance->m_usedEngines.removeAll(e);
    s_instance->m_freeEngines << e;
    if (s_instance->m_freeEngines.size() > 5) {
        s_instance->m_freeEngines.takeLast();
        s_instance->m_freeEngines.takeLast();
        s_instance->m_freeEngines.takeLast();
    }
}

QObject *Backend::createObject(BackendInterface::Class c, QObject *parent, const QList<QVariant> &args)
{
    switch (c) {
    case MediaObjectClass:
        return new MediaObject(parent);
    case VolumeFaderEffectClass:
        return new VolumeFaderEffect(parent);
    case AudioOutputClass:
        return new AudioOutput(parent);
    case AudioDataOutputClass:
        return new AudioDataOutput(parent);
    case VisualizationClass:
        return new Visualization(parent);
    case VideoDataOutputClass:
        return 0;
    case Phonon::Experimental::BackendInterface::VideoDataOutputClass:
        return new VideoDataOutput(parent);
    case EffectClass:
        {
            Q_ASSERT(args.size() == 1);
            debug() << Q_FUNC_INFO << "creating Effect(" << args[0];
            Effect *e = new Effect(args[0].toInt(), parent);
            if (e->isValid()) {
                return e;
            }
            delete e;
            return 0;
        }
    case VideoWidgetClass:
        {
#ifdef XINEWIDGET_FOUND
            VideoWidget *vw = new VideoWidget(qobject_cast<QWidget *>(parent));
{'fraction_non_alphanumeric': 0.12139095696386418, 'fraction_numerical': 0.00835300526602506, 'mean_word_length': 3.743755383290267, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3169934', 'n_tokens_mistral': 3591, 'n_tokens_neox': 3502, 'n_words': 811}