<commit_before>/*****************************************************************************
 * VLC backend for the Phonon library                                        *
 * Copyright (C) 2007-2008 <NAME> <<EMAIL>>               *
 * Copyright (C) 2008 <NAME> <<EMAIL>>                *
 * Copyright (C) 2009 <NAME> <<EMAIL>>                            *
 *                                                                           *
 * This program is free software; you can redistribute it and/or             *
 * modify it under the terms of the GNU Lesser General Public                *
 * License as published by the Free Software Foundation; either              *
 * version 2.1 of the License, or (at your option) any later version.        *
 *                                                                           *
 * This program 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         *
 * Lesser General Public License for more details.                           *
 *                                                                           *
 * You should have received a copy of the GNU Lesser General Public          *
 * License along with this package; if not, write to the Free Software       *
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA *
 *****************************************************************************/

#include "audiooutput.h"

#ifdef PHONON_PULSESUPPORT
#include <phonon/pulsesupport.h>
#endif

#include <vlc/vlc.h>

#include "backend.h"
#include "debug.h"
#include "devicemanager.h"
#include "mediaobject.h"

#ifndef PHONON_VLC_NO_EXPERIMENTAL
#include <experimental/avcapture.h>
#endif // PHONON_VLC_NO_EXPERIMENTAL

namespace Phonon
{
namespace VLC
{

AudioOutput::AudioOutput(QObject *parent)
    : QObject(parent),
      m_volume(1.0),
      m_deviceIndex(0)
{
}

AudioOutput::~AudioOutput()
{
}

void AudioOutput::connectToMediaObject(MediaObject *mediaObject)
{
    SinkNode::connectToMediaObject(mediaObject);
    setAudioOutputDeviceImplementation();
    connect(m_mediaObject, SIGNAL(playbackCommenced()), this, SLOT(updateVolume()));
}

void AudioOutput::disconnectFromMediaObject(MediaObject *mediaObject)
{
    SinkNode::disconnectFromMediaObject(mediaObject);
    if (m_mediaObject) {
        disconnect(m_mediaObject, SIGNAL(playbackCommenced()), this, SLOT(updateVolume()));
    }
}

#ifndef PHONON_VLC_NO_EXPERIMENTAL
void AudioOutput::connectToAvCapture(Experimental::AvCapture *avCapture)
{
    connectToMediaObject(avCapture->audioMediaObject());
}

void AudioOutput::disconnectFromAvCapture(Experimental::AvCapture *avCapture)
{
    disconnectFromMediaObject(avCapture->audioMediaObject());
}
#endif // PHONON_VLC_NO_EXPERIMENTAL

qreal AudioOutput::volume() const
{
    return m_volume;
}

void AudioOutput::setVolume(qreal volume)
{
    if (m_player) {
        const int previous_volume = libvlc_audio_get_volume(m_player);
        m_volume = volume;
        libvlc_audio_set_volume(m_player, (int)(m_volume * 50));
        debug() << "Volume changed from" << previous_volume << "to" << (int)(m_volume * 100);
        emit volumeChanged(m_volume);
    }
}

int AudioOutput::outputDevice() const
{
    return m_deviceIndex;
}

bool AudioOutput::setOutputDevice(int deviceIndex)
{
    const QList<DeviceInfo> deviceList = Backend::self->deviceManager()->audioOutputDevices();
    if (deviceIndex < 0 || deviceIndex >= deviceList.size()) {
        return false;
    }
    if (m_deviceIndex != deviceIndex) {
        m_deviceIndex = deviceIndex;
        if (m_player) {
            setAudioOutputDeviceImplementation();
        }
    }
    return true;
}

void AudioOutput::setAudioOutputDeviceImplementation()
{
    Q_ASSERT(m_player);
#ifdef PHONON_PULSESUPPORT
    if (PulseSupport::getInstance()->isActive()) {
        libvlc_audio_output_set(m_player, "pulse");
        debug() << "Setting aout to pulse";
        return;
    }
#endif
    const QList<DeviceInfo> deviceList = Backend::self->deviceManager()->audioOutputDevices();

    Q_ASSERT(m_deviceIndex >= 0 && m_deviceIndex < deviceList.size());
    const DeviceInfo &device = deviceList.at(m_deviceIndex);

    // ### we're not trying the whole access list (could mean same device on different soundsystems)
    QByteArray soundSystem = device.accessList.first().first;
    debug() << "Setting output soundsystem to" << soundSystem;
    libvlc_audio_output_set(m_player, soundSystem);

    QByteArray deviceName = device.accessList.first().second.toLatin1();
    // print the name as possibly messed up by toLatin1() to see conversion problems
    debug() << "Setting output device to" << deviceName;
    libvlc_audio_output_device_set(m_player, soundSystem, deviceName);
}

#if (PHONON_VERSION >= PHONON_VERSION_CHECK(4, 2, 0))
bool AudioOutput::setOutputDevice(const Phonon::AudioOutputDevice &device)
{
    return true;
}
#endif

void AudioOutput::updateVolume()
{<commit_msg>Add long name of device to debug output when setting the device.<commit_after>/*****************************************************************************
 * VLC backend for the Phonon library                                        *
 * Copyright (C) 2007-2008 <NAME> <<EMAIL>>               *
 * Copyright (C) 2008 <NAME> <<EMAIL>>                *
 * Copyright (C) 2009 <NAME> <<EMAIL>>                            *
 *                                                                           *
 * This program is free software; you can redistribute it and/or             *
 * modify it under the terms of the GNU Lesser General Public                *
 * License as published by the Free Software Foundation; either              *
 * version 2.1 of the License, or (at your option) any later version.        *
 *                                                                           *
 * This program 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         *
 * Lesser General Public License for more details.                           *
 *                                                                           *
 * You should have received a copy of the GNU Lesser General Public          *
 * License along with this package; if not, write to the Free Software       *
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA *
 *****************************************************************************/

#include "audiooutput.h"

#ifdef PHONON_PULSESUPPORT
#include <phonon/pulsesupport.h>
#endif

#include <vlc/vlc.h>

#include "backend.h"
#include "debug.h"
#include "devicemanager.h"
#include "mediaobject.h"

#ifndef PHONON_VLC_NO_EXPERIMENTAL
#include <experimental/avcapture.h>
#endif // PHONON_VLC_NO_EXPERIMENTAL

namespace Phonon
{
namespace VLC
{

AudioOutput::AudioOutput(QObject *parent)
    : QObject(parent),
      m_volume(1.0),
      m_deviceIndex(0)
{
}

AudioOutput::~AudioOutput()
{
}

void AudioOutput::connectToMediaObject(MediaObject *mediaObject)
{
    SinkNode::connectToMediaObject(mediaObject);
    setAudioOutputDeviceImplementation();
    connect(m_mediaObject, SIGNAL(playbackCommenced()), this, SLOT(updateVolume()));
}

void AudioOutput::disconnectFromMediaObject(MediaObject *mediaObject)
{
    SinkNode::disconnectFromMediaObject(mediaObject);
    if (m_mediaObject) {
        disconnect(m_mediaObject, SIGNAL(playbackCommenced()), this, SLOT(updateVolume()));
    }
}

#ifndef PHONON_VLC_NO_EXPERIMENTAL
void AudioOutput::connectToAvCapture(Experimental::AvCapture *avCapture)
{
    connectToMediaObject(avCapture->audioMediaObject());
}

void AudioOutput::disconnectFromAvCapture(Experimental::AvCapture *avCapture)
{
    disconnectFromMediaObject(avCapture->audioMediaObject());
}
#endif // PHONON_VLC_NO_EXPERIMENTAL

qreal AudioOutput::volume() const
{
    return m_volume;
}

void AudioOutput::setVolume(qreal volume)
{
    if (m_player) {
        const int previous_volume = libvlc_audio_get_volume(m_player);
        m_volume = volume;
        libvlc_audio_set_volume(m_player, (int)(m_volume * 50));
        debug() << "Volume changed from" << previous_volume << "to" << (int)(m_volume * 100);
        emit volumeChanged(m_volume);
    }
}

int AudioOutput::outputDevice() const
{
    return m_deviceIndex;
}

bool AudioOutput::setOutputDevice(int deviceIndex)
{
    const QList<DeviceInfo> deviceList = Backend::self->deviceManager()->audioOutputDevices();
    if (deviceIndex < 0 || deviceIndex >= deviceList.size()) {
        return false;
    }
    if (m_deviceIndex != deviceIndex) {
        m_deviceIndex = deviceIndex;
        if (m_player) {
            setAudioOutputDeviceImplementation();
        }
    }
    return true;
}

void AudioOutput::setAudioOutputDeviceImplementation()
{
    Q_ASSERT(m_player);
#ifdef PHONON_PULSESUPPORT
    if (PulseSupport::getInstance()->isActive()) {
        libvlc_audio_output_set(m_player, "pulse");
        debug() << "Setting aout to pulse";
        return;
    }
#endif
    const QList<DeviceInfo> deviceList = Backend::self->deviceManager()->audioOutputDevices();

    Q_ASSERT(m_deviceIndex >= 0 && m_deviceIndex < deviceList.size());
    const DeviceInfo &device = deviceList.at(m_deviceIndex);

    // ### we're not trying the whole access list (could mean same device on different soundsystems)
    QByteArray soundSystem = device.accessList.first().first;
    debug() << "Setting output soundsystem to" << soundSystem;
    libvlc_audio_output_set(m_player, soundSystem);

    QByteArray deviceName = device.accessList.first().second.toLatin1();
    // print the name as possibly messed up by toLatin1() to see conversion problems
    debug() << "Setting output device to" << deviceName << '(' << device.name << ')';
    libvlc_audio_output_device_set(m_player, soundSystem, deviceName);
}

#if (PHONON_VERSION >= PHONON_VERSION_CHECK(4, 2, 0))
bool AudioOutput::setOutputDevice(const Phonon::AudioOutputDevice &device)
{
    return true;
}
#endif

void AudioOutput::updateVolume()
{
{'fraction_non_alphanumeric': 0.13027415905113746, 'fraction_numerical': 0.008555317907835893, 'mean_word_length': 3.088632750397456, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1958559', 'n_tokens_mistral': 2935, 'n_tokens_neox': 2836, 'n_words': 716}