void QSpotifyAudioThreadWorker::startStreaming(int channels, int sampleRate)
{
    qDebug() << "QSpotifyAudioThreadWorker::startStreaming";
    if (!m_audioOutput) {
        QAudioFormat af;
        af.setChannelCount(channels);
        af.setCodec("audio/pcm");
        af.setSampleRate(sampleRate);
        af.setSampleSize(16);
        af.setSampleType(QAudioFormat::SignedInt);

        QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
        if (!info.isFormatSupported(af)) {
            QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
            for (int i = 0; i < devices.size(); i++) {
                QAudioDeviceInfo dev = devices[i];
                qWarning() << dev.deviceName();
            }
            QCoreApplication::postEvent(QSpotifySession::instance(), new QEvent(QEvent::Type(StopEventType)));
            return;
        }

        m_audioOutput = new QAudioOutput(af);
        connect(m_audioOutput, SIGNAL(stateChanged(QAudio::State)), QSpotifySession::instance(), SLOT(audioStateChange(QAudio::State)));
        m_audioOutput->setBufferSize(BUF_SIZE);

        startAudioOutput();
    }
}
bool QSpotifyAudioThreadWorker::event(QEvent *e)
{
    // Ignore timer events to have less log trashing
    if(e->type() != QEvent::Timer)
        qDebug() << "QSpotifyAudioThreadWorker::event" << e->type();
    if (e->type() == StreamingStartedEventType) {
        QMutexLocker lock(&g_mutex);
        QSpotifyStreamingStartedEvent *ev = static_cast<QSpotifyStreamingStartedEvent *>(e);
        startStreaming(ev->channels(), ev->sampleRate());
        e->accept();
        return true;
    } else if (e->type() == ResumeEventType) {
        QMutexLocker lock(&g_mutex);
        if (m_audioOutput) {
            m_audioOutput->resume();
            m_audioTimerID = startTimer(AUDIOSTREAM_UPDATE_INTERVAL);
        }
        e->accept();
        return true;
    } else if (e->type() == SuspendEventType) {
        QMutexLocker lock(&g_mutex);
        if (m_audioOutput) {
            killTimer(m_audioTimerID);
            m_audioOutput->suspend();
        }
        e->accept();
        return true;
    } else if (e->type() == AudioStopEventType) {
        QMutexLocker lock(&g_mutex);
        killTimer(m_audioTimerID);
        g_buffer.close();
        if (m_audioOutput) {
            m_audioOutput->stop();
            m_audioOutput->deleteLater();
            m_audioOutput = nullptr;
            m_iodevice = nullptr;
        }
        e->accept();
        return true;
    } else if (e->type() == ResetBufferEventType) {
        QMutexLocker lock(&g_mutex);
        if (m_audioOutput) {
            killTimer(m_audioTimerID);
            m_audioOutput->reset();
            g_buffer.reset();
            startAudioOutput();
        }
        e->accept();
        return true;
    } else if (e->type() == QEvent::Timer) {
        QTimerEvent *te = static_cast<QTimerEvent *>(e);
        if (te->timerId() == m_audioTimerID) {
            updateAudioBuffer();
            e->accept();
            return true;
        }
    }
    return QObject::event(e);
}
Esempio n. 3
0
void Client::callEstablished()
{
    _callEstablished = true;

    stopAudioInput();
    startAudioInput();

    stopAudioOutput();
    startAudioOutput();

    _audioInputList->clear();

    _dataThread = new OutputDataThread(_connection, _audioInputList);
    QObject::connect( _dataThread, SIGNAL( finished() ), this, SLOT( finishedThread() ) );

    _dataThread->sendData();

    _dataThread->start();
}