From 2a34e88c1e1ced28e75c487cd13402e1c9cf9fa3 Mon Sep 17 00:00:00 2001 From: Michael Goddard Date: Wed, 29 Jun 2011 13:38:46 +1000 Subject: Initial copy of QtMultimediaKit. Comes from original repo, with SHA1: 2c82d5611655e5967f5c5095af50c0991c4378b2 --- src/plugins/pulseaudio/pulseaudio.pro | 26 + src/plugins/pulseaudio/qaudiodeviceinfo_pulse.cpp | 105 ++++ src/plugins/pulseaudio/qaudiodeviceinfo_pulse.h | 92 ++++ src/plugins/pulseaudio/qaudioinput_pulse.cpp | 600 ++++++++++++++++++++++ src/plugins/pulseaudio/qaudioinput_pulse.h | 153 ++++++ src/plugins/pulseaudio/qaudiooutput_pulse.cpp | 573 +++++++++++++++++++++ src/plugins/pulseaudio/qaudiooutput_pulse.h | 153 ++++++ src/plugins/pulseaudio/qpulseaudioengine.cpp | 354 +++++++++++++ src/plugins/pulseaudio/qpulseaudioengine.h | 100 ++++ src/plugins/pulseaudio/qpulseaudioplugin.cpp | 89 ++++ src/plugins/pulseaudio/qpulseaudioplugin.h | 71 +++ src/plugins/pulseaudio/qpulsehelpers.cpp | 220 ++++++++ src/plugins/pulseaudio/qpulsehelpers.h | 73 +++ 13 files changed, 2609 insertions(+) create mode 100644 src/plugins/pulseaudio/pulseaudio.pro create mode 100644 src/plugins/pulseaudio/qaudiodeviceinfo_pulse.cpp create mode 100644 src/plugins/pulseaudio/qaudiodeviceinfo_pulse.h create mode 100644 src/plugins/pulseaudio/qaudioinput_pulse.cpp create mode 100644 src/plugins/pulseaudio/qaudioinput_pulse.h create mode 100644 src/plugins/pulseaudio/qaudiooutput_pulse.cpp create mode 100644 src/plugins/pulseaudio/qaudiooutput_pulse.h create mode 100644 src/plugins/pulseaudio/qpulseaudioengine.cpp create mode 100644 src/plugins/pulseaudio/qpulseaudioengine.h create mode 100644 src/plugins/pulseaudio/qpulseaudioplugin.cpp create mode 100644 src/plugins/pulseaudio/qpulseaudioplugin.h create mode 100644 src/plugins/pulseaudio/qpulsehelpers.cpp create mode 100644 src/plugins/pulseaudio/qpulsehelpers.h (limited to 'src/plugins/pulseaudio') diff --git a/src/plugins/pulseaudio/pulseaudio.pro b/src/plugins/pulseaudio/pulseaudio.pro new file mode 100644 index 000000000..2f9b7a854 --- /dev/null +++ b/src/plugins/pulseaudio/pulseaudio.pro @@ -0,0 +1,26 @@ +load(qt_module) + +TARGET = qtmedia_pulse +QT += multimediakit-private +PLUGIN_TYPE = audio + +load(qt_plugin) +DESTDIR = $$QT.multimediakit.plugins/$${PLUGIN_TYPE} + +CONFIG += link_pkgconfig +PKGCONFIG += libpulse + +# Input +HEADERS += qpulseaudioplugin.h \ + qaudiodeviceinfo_pulse.h \ + qaudiooutput_pulse.h \ + qaudioinput_pulse.h \ + qpulseaudioengine.h \ + qpulsehelpers.h + +SOURCES += qpulseaudioplugin.cpp \ + qaudiodeviceinfo_pulse.cpp \ + qaudiooutput_pulse.cpp \ + qaudioinput_pulse.cpp \ + qpulseaudioengine.cpp \ + qpulsehelpers.cpp diff --git a/src/plugins/pulseaudio/qaudiodeviceinfo_pulse.cpp b/src/plugins/pulseaudio/qaudiodeviceinfo_pulse.cpp new file mode 100644 index 000000000..54296861d --- /dev/null +++ b/src/plugins/pulseaudio/qaudiodeviceinfo_pulse.cpp @@ -0,0 +1,105 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qaudiodeviceinfo_pulse.h" +#include "qpulseaudioengine.h" +#include "qpulsehelpers.h" + +QT_BEGIN_NAMESPACE + +QPulseAudioDeviceInfo::QPulseAudioDeviceInfo(const QByteArray &device, QAudio::Mode mode) + : m_device(device) + , m_mode(mode) +{ +} + +bool QPulseAudioDeviceInfo::isFormatSupported(const QAudioFormat &format) const +{ + pa_sample_spec spec = QPulseAudioInternal::audioFormatToSampleSpec(format); + if (!pa_sample_spec_valid(&spec)) + return false; + + return true; +} + +QAudioFormat QPulseAudioDeviceInfo::preferredFormat() const +{ + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + QAudioFormat format = pulseEngine->m_preferredFormats.value(m_device); + return format; +} + +QString QPulseAudioDeviceInfo::deviceName() const +{ + return m_device; +} + +QStringList QPulseAudioDeviceInfo::supportedCodecs() +{ + return QStringList() << "audio/pcm"; +} + +QList QPulseAudioDeviceInfo::supportedSampleRates() +{ + return QList() << 8000 << 11025 << 22050 << 44100 << 48000; +} + +QList QPulseAudioDeviceInfo::supportedChannelCounts() +{ + return QList() << 1 << 2 << 4 << 6 << 8; +} + +QList QPulseAudioDeviceInfo::supportedSampleSizes() +{ + return QList() << 8 << 16 << 24 << 32; +} + +QList QPulseAudioDeviceInfo::supportedByteOrders() +{ + return QList() << QAudioFormat::BigEndian << QAudioFormat::LittleEndian; +} + +QList QPulseAudioDeviceInfo::supportedSampleTypes() +{ + return QList() << QAudioFormat::SignedInt << QAudioFormat::UnSignedInt << QAudioFormat::Float; +} + +QT_END_NAMESPACE diff --git a/src/plugins/pulseaudio/qaudiodeviceinfo_pulse.h b/src/plugins/pulseaudio/qaudiodeviceinfo_pulse.h new file mode 100644 index 000000000..f76913ca3 --- /dev/null +++ b/src/plugins/pulseaudio/qaudiodeviceinfo_pulse.h @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QAUDIODEVICEINFOPULSE_H +#define QAUDIODEVICEINFOPULSE_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include + +#include "qaudio.h" +#include "qaudiodeviceinfo.h" +#include "qaudiosystem.h" + +QT_BEGIN_NAMESPACE + +class QPulseAudioDeviceInfo : public QAbstractAudioDeviceInfo +{ + Q_OBJECT + +public: + QPulseAudioDeviceInfo(const QByteArray &device, QAudio::Mode mode); + ~QPulseAudioDeviceInfo() {} + + QAudioFormat preferredFormat() const; + bool isFormatSupported(const QAudioFormat &format) const; + QString deviceName() const; + QStringList supportedCodecs(); + QList supportedSampleRates(); + QList supportedChannelCounts(); + QList supportedSampleSizes(); + QList supportedByteOrders(); + QList supportedSampleTypes(); + +private: + QByteArray m_device; + QAudio::Mode m_mode; +}; + +QT_END_NAMESPACE + +#endif + diff --git a/src/plugins/pulseaudio/qaudioinput_pulse.cpp b/src/plugins/pulseaudio/qaudioinput_pulse.cpp new file mode 100644 index 000000000..083bbabb6 --- /dev/null +++ b/src/plugins/pulseaudio/qaudioinput_pulse.cpp @@ -0,0 +1,600 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#include "qaudioinput_pulse.h" +#include "qaudiodeviceinfo_pulse.h" +#include "qpulseaudioengine.h" +#include "qpulsehelpers.h" + +QT_BEGIN_NAMESPACE + +const int PeriodTimeMs = 50; + +static void inputStreamReadCallback(pa_stream *stream, size_t length, void *userdata) +{ + Q_UNUSED(userdata); + Q_UNUSED(length); + Q_UNUSED(stream); + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); +} + +static void inputStreamStateCallback(pa_stream *stream, void *userdata) +{ + Q_UNUSED(userdata); + pa_stream_state_t state = pa_stream_get_state(stream); +#ifdef DEBUG_PULSE + qDebug() << "Stream state: " << QPulseAudioInternal::stateToQString(state); +#endif + switch (state) { + case PA_STREAM_CREATING: + break; + case PA_STREAM_READY: { +#ifdef DEBUG_PULSE + QPulseAudioInput *audioInput = static_cast(userdata); + const pa_buffer_attr *buffer_attr = pa_stream_get_buffer_attr(stream); + qDebug() << "*** maxlength: " << buffer_attr->maxlength; + qDebug() << "*** prebuf: " << buffer_attr->prebuf; + qDebug() << "*** fragsize: " << buffer_attr->fragsize; + qDebug() << "*** minreq: " << buffer_attr->minreq; + qDebug() << "*** tlength: " << buffer_attr->tlength; + + pa_sample_spec spec = QPulseAudioInternal::audioFormatToSampleSpec(audioInput->format()); + qDebug() << "*** bytes_to_usec: " << pa_bytes_to_usec(buffer_attr->fragsize, &spec); +#endif + } + break; + case PA_STREAM_TERMINATED: + break; + case PA_STREAM_FAILED: + default: + qWarning() << QString("Stream error: %1").arg(pa_strerror(pa_context_errno(pa_stream_get_context(stream)))); + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); + break; + } +} + +static void inputStreamUnderflowCallback(pa_stream *stream, void *userdata) +{ + Q_UNUSED(userdata) + Q_UNUSED(stream) + qWarning() << "Got a buffer underflow!"; +} + +static void inputStreamOverflowCallback(pa_stream *stream, void *userdata) +{ + Q_UNUSED(stream) + Q_UNUSED(userdata) + qWarning() << "Got a buffer overflow!"; +} + +static void inputStreamSuccessCallback(pa_stream *stream, int success, void *userdata) +{ + Q_UNUSED(stream); + Q_UNUSED(userdata); + Q_UNUSED(success); + + //if (!success) + //TODO: Is cork success? i->operation_success = success; + + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); +} + +QPulseAudioInput::QPulseAudioInput(const QByteArray &device) + : m_totalTimeValue(0) + , m_audioSource(0) + , m_errorState(QAudio::NoError) + , m_deviceState(QAudio::StoppedState) + , m_pullMode(true) + , m_opened(false) + , m_bytesAvailable(0) + , m_bufferSize(0) + , m_periodSize(0) + , m_intervalTime(1000) + , m_stream(0) + , m_device(device) +{ + m_timer = new QTimer(this); + connect(m_timer, SIGNAL(timeout()), SLOT(userFeed())); +} + +QPulseAudioInput::~QPulseAudioInput() +{ + close(); + disconnect(m_timer, SIGNAL(timeout())); + QCoreApplication::processEvents(); + delete m_timer; +} + +QAudio::Error QPulseAudioInput::error() const +{ + return m_errorState; +} + +QAudio::State QPulseAudioInput::state() const +{ + return m_deviceState; +} + +void QPulseAudioInput::setFormat(const QAudioFormat &format) +{ + if (m_deviceState == QAudio::StoppedState) + m_format = format; +} + +QAudioFormat QPulseAudioInput::format() const +{ + return m_format; +} + +void QPulseAudioInput::start(QIODevice *device) +{ + if (m_deviceState != QAudio::StoppedState) + close(); + + if (!m_pullMode && m_audioSource) + delete m_audioSource; + + m_pullMode = true; + m_audioSource = device; + + m_deviceState = QAudio::ActiveState; + + if (!open()) + return; + + emit stateChanged(m_deviceState); +} + +QIODevice *QPulseAudioInput::start() +{ + if (m_deviceState != QAudio::StoppedState) + close(); + + if (!m_pullMode && m_audioSource) + delete m_audioSource; + + m_pullMode = false; + m_audioSource = new InputPrivate(this); + m_audioSource->open(QIODevice::ReadOnly | QIODevice::Unbuffered); + + m_deviceState = QAudio::IdleState; + + if (!open()) + return 0; + + emit stateChanged(m_deviceState); + + return m_audioSource; +} + +void QPulseAudioInput::stop() +{ + if (m_deviceState == QAudio::StoppedState) + return; + + m_errorState = QAudio::NoError; + m_deviceState = QAudio::StoppedState; + + close(); + emit stateChanged(m_deviceState); +} + +bool QPulseAudioInput::open() +{ + if (m_opened) + return false; + +#ifdef DEBUG_PULSE +// QTime now(QTime::currentTime()); +// qDebug()<mainloop()); + pa_channel_map channel_map; + + pa_channel_map_init_extend(&channel_map, spec.channels, PA_CHANNEL_MAP_DEFAULT); + + if (!pa_channel_map_compatible(&channel_map, &spec)) { + qWarning() << "Channel map doesn't match sample specification!"; + } + + m_stream = pa_stream_new(pulseEngine->context(), m_streamName.constData(), &spec, &channel_map); + + pa_stream_set_state_callback(m_stream, inputStreamStateCallback, this); + pa_stream_set_read_callback(m_stream, inputStreamReadCallback, this); + + pa_stream_set_underflow_callback(m_stream, inputStreamUnderflowCallback, this); + pa_stream_set_overflow_callback(m_stream, inputStreamOverflowCallback, this); + + m_periodSize = pa_usec_to_bytes(PeriodTimeMs*1000, &spec); + + int flags = 0; + pa_buffer_attr buffer_attr; + buffer_attr.maxlength = (uint32_t) -1; + buffer_attr.prebuf = (uint32_t) -1; + buffer_attr.tlength = (uint32_t) -1; + buffer_attr.minreq = (uint32_t) -1; + flags |= PA_STREAM_ADJUST_LATENCY; + + if (m_bufferSize > 0) + buffer_attr.fragsize = (uint32_t) m_bufferSize; + else + buffer_attr.fragsize = (uint32_t) m_periodSize; + + if (pa_stream_connect_record(m_stream, m_device.data(), &buffer_attr, (pa_stream_flags_t)flags) < 0) { + qWarning() << "pa_stream_connect_record() failed!"; + m_errorState = QAudio::FatalError; + return false; + } + + while (pa_stream_get_state(m_stream) != PA_STREAM_READY) { + pa_threaded_mainloop_wait(pulseEngine->mainloop()); + } + pa_threaded_mainloop_unlock(pulseEngine->mainloop()); + + m_opened = true; + m_periodSize = pa_usec_to_bytes(PeriodTimeMs*1000, &spec); + m_timer->start(PeriodTimeMs); + m_errorState = QAudio::NoError; + + m_totalTimeValue = 0; + + return true; +} + +void QPulseAudioInput::close() +{ + m_timer->stop(); + + if (m_stream) { + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + pa_threaded_mainloop_lock(pulseEngine->mainloop()); + + pa_stream_set_read_callback(m_stream, 0, 0); + + pa_stream_disconnect(m_stream); + pa_stream_unref(m_stream); + m_stream = 0; + + pa_threaded_mainloop_unlock(pulseEngine->mainloop()); + } + + if (!m_pullMode && m_audioSource) { + delete m_audioSource; + m_audioSource = 0; + } + m_opened = false; +} + +int QPulseAudioInput::checkBytesReady() +{ + if (m_deviceState != QAudio::ActiveState && m_deviceState != QAudio::IdleState) { + m_bytesAvailable = 0; + } else { + m_bytesAvailable = pa_stream_readable_size(m_stream); + } + + return m_bytesAvailable; +} + +int QPulseAudioInput::bytesReady() const +{ + return qMax(m_bytesAvailable, 0); +} + +qint64 QPulseAudioInput::read(char *data, qint64 len) +{ + m_bytesAvailable = checkBytesReady(); + + if (m_deviceState != QAudio::ActiveState) { + m_errorState = QAudio::NoError; + m_deviceState = QAudio::ActiveState; + emit stateChanged(m_deviceState); + } + + size_t readBytes = 0; + + if (!m_pullMode && !m_tempBuffer.isEmpty()) { + readBytes = qMin(static_cast(len), m_tempBuffer.size()); + memcpy(data, m_tempBuffer.constData(), readBytes); + m_totalTimeValue += readBytes; + + if (readBytes < m_tempBuffer.size()) { + m_tempBuffer.remove(0, readBytes); + return readBytes; + } + + m_tempBuffer.clear(); + } + + while (pa_stream_readable_size(m_stream) > 0) { + size_t readLength = 0; + +#ifdef DEBUG_PULSE + qDebug() << "QPulseAudioInput::read -- " << pa_stream_readable_size(m_stream) << " bytes available from pulse audio"; +#endif + + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + pa_threaded_mainloop_lock(pulseEngine->mainloop()); + const void *audioBuffer; + + // Second and third parameters (audioBuffer and length) to pa_stream_peek are output parameters, + // the audioBuffer pointer is set to point to the actual pulse audio data, + // and the length is set to the length of this data. + if (pa_stream_peek(m_stream, &audioBuffer, &readLength) < 0) { + qWarning() << QString("pa_stream_peek() failed: %1").arg(pa_strerror(pa_context_errno(pa_stream_get_context(m_stream)))); + pa_threaded_mainloop_unlock(pulseEngine->mainloop()); + return 0; + } + + qint64 actualLength = 0; + if (m_pullMode) { + actualLength = m_audioSource->write(static_cast(audioBuffer), readLength); + + if (actualLength < readLength) { + pa_threaded_mainloop_unlock(pulseEngine->mainloop()); + + m_errorState = QAudio::UnderrunError; + m_deviceState = QAudio::IdleState; + emit stateChanged(m_deviceState); + + return actualLength; + } + } else { + actualLength = qMin(static_cast(len - readBytes), static_cast(readLength)); + memcpy(data + readBytes, audioBuffer, actualLength); + } + +#ifdef DEBUG_PULSE + qDebug() << "QPulseAudioInput::read -- wrote " << actualLength << " to client"; +#endif + + if (actualLength < readLength) { +#ifdef DEBUG_PULSE + qDebug() << "QPulseAudioInput::read -- appending " << readLength - actualLength << " bytes of data to temp buffer"; +#endif + m_tempBuffer.append(static_cast(audioBuffer) + actualLength, readLength - actualLength); + QMetaObject::invokeMethod(this, "userFeed", Qt::QueuedConnection); + } + + m_totalTimeValue += actualLength; + readBytes += actualLength; + + pa_stream_drop(m_stream); + pa_threaded_mainloop_unlock(pulseEngine->mainloop()); + + if (!m_pullMode && readBytes >= len) + break; + } + +#ifdef DEBUG_PULSE + qDebug() << "QPulseAudioInput::read -- returning after reading " << readBytes << " bytes"; +#endif + + return readBytes; +} + +void QPulseAudioInput::resume() +{ + if (m_deviceState == QAudio::SuspendedState || m_deviceState == QAudio::IdleState) { + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + pa_operation *operation; + + pa_threaded_mainloop_lock(pulseEngine->mainloop()); + + operation = pa_stream_cork(m_stream, 0, inputStreamSuccessCallback, 0); + + while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING) + pa_threaded_mainloop_wait(pulseEngine->mainloop()); + + pa_operation_unref(operation); + + pa_threaded_mainloop_unlock(pulseEngine->mainloop()); + + m_timer->start(PeriodTimeMs); + + m_deviceState = QAudio::ActiveState; + + emit stateChanged(m_deviceState); + } +} + +void QPulseAudioInput::setBufferSize(int value) +{ + m_bufferSize = value; +} + +int QPulseAudioInput::bufferSize() const +{ + return m_bufferSize; +} + +int QPulseAudioInput::periodSize() const +{ + return m_periodSize; +} + +void QPulseAudioInput::setNotifyInterval(int ms) +{ + m_intervalTime = qMax(0, ms); +} + +int QPulseAudioInput::notifyInterval() const +{ + return m_intervalTime; +} + +qint64 QPulseAudioInput::processedUSecs() const +{ + pa_sample_spec spec = QPulseAudioInternal::audioFormatToSampleSpec(m_format); + qint64 result = pa_bytes_to_usec(m_totalTimeValue, &spec); + + return result; +} + +void QPulseAudioInput::suspend() +{ + if (m_deviceState == QAudio::ActiveState) { + m_timer->stop(); + m_deviceState = QAudio::SuspendedState; + emit stateChanged(m_deviceState); + + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + pa_operation *operation; + + pa_threaded_mainloop_lock(pulseEngine->mainloop()); + + operation = pa_stream_cork(m_stream, 1, inputStreamSuccessCallback, 0); + + while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING) + pa_threaded_mainloop_wait(pulseEngine->mainloop()); + + pa_operation_unref(operation); + + pa_threaded_mainloop_unlock(pulseEngine->mainloop()); + } +} + +void QPulseAudioInput::userFeed() +{ + if (m_deviceState == QAudio::StoppedState || m_deviceState == QAudio::SuspendedState) + return; +#ifdef DEBUG_PULSE +// QTime now(QTime::currentTime()); +// qDebug()<< now.second() << "s " << now.msec() << "ms :userFeed() IN"; +#endif + deviceReady(); +} + +bool QPulseAudioInput::deviceReady() +{ + if (m_pullMode) { + // reads some audio data and writes it to QIODevice + read(0,0); + } else { + // emits readyRead() so user will call read() on QIODevice to get some audio data + if (m_audioSource != 0) { + InputPrivate *a = qobject_cast(m_audioSource); + a->trigger(); + } + } + m_bytesAvailable = checkBytesReady(); + + if (m_deviceState != QAudio::ActiveState) + return true; + + if (m_intervalTime && (m_timeStamp.elapsed() + m_elapsedTimeOffset) > m_intervalTime) { + emit notify(); + m_elapsedTimeOffset = m_timeStamp.elapsed() + m_elapsedTimeOffset - m_intervalTime; + m_timeStamp.restart(); + } + + return true; +} + +qint64 QPulseAudioInput::elapsedUSecs() const +{ + if (m_deviceState == QAudio::StoppedState) + return 0; + + return m_clockStamp.elapsed() * 1000; +} + +void QPulseAudioInput::reset() +{ + stop(); + m_bytesAvailable = 0; +} + +InputPrivate::InputPrivate(QPulseAudioInput *audio) +{ + m_audioDevice = qobject_cast(audio); +} + +qint64 InputPrivate::readData(char *data, qint64 len) +{ + return m_audioDevice->read(data, len); +} + +qint64 InputPrivate::writeData(const char *data, qint64 len) +{ + Q_UNUSED(data) + Q_UNUSED(len) + return 0; +} + +void InputPrivate::trigger() +{ + emit readyRead(); +} + +QT_END_NAMESPACE + +#include "moc_qaudioinput_pulse.cpp" diff --git a/src/plugins/pulseaudio/qaudioinput_pulse.h b/src/plugins/pulseaudio/qaudioinput_pulse.h new file mode 100644 index 000000000..c186f91ab --- /dev/null +++ b/src/plugins/pulseaudio/qaudioinput_pulse.h @@ -0,0 +1,153 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of other Qt classes. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#ifndef QAUDIOINPUTPULSE_H +#define QAUDIOINPUTPULSE_H + +#include +#include +#include +#include +#include + +#include "qaudio.h" +#include "qaudiodeviceinfo.h" +#include "qaudiosystem.h" + +#include + +QT_BEGIN_NAMESPACE + +class InputPrivate; + +class QPulseAudioInput : public QAbstractAudioInput +{ + Q_OBJECT + +public: + QPulseAudioInput(const QByteArray &device); + ~QPulseAudioInput(); + + qint64 read(char *data, qint64 len); + + void start(QIODevice *device); + QIODevice *start(); + void stop(); + void reset(); + void suspend(); + void resume(); + int bytesReady() const; + int periodSize() const; + void setBufferSize(int value); + int bufferSize() const; + void setNotifyInterval(int milliSeconds); + int notifyInterval() const; + qint64 processedUSecs() const; + qint64 elapsedUSecs() const; + QAudio::Error error() const; + QAudio::State state() const; + void setFormat(const QAudioFormat &format); + QAudioFormat format() const; + + qint64 m_totalTimeValue; + QIODevice *m_audioSource; + QAudioFormat m_format; + QAudio::Error m_errorState; + QAudio::State m_deviceState; + +private slots: + void userFeed(); + bool deviceReady(); + +private: + int checkBytesReady(); + bool open(); + void close(); + + bool m_pullMode; + bool m_opened; + int m_bytesAvailable; + int m_bufferSize; + int m_periodSize; + int m_intervalTime; + unsigned int m_bufferTime; + unsigned int m_periodTime; + QTimer *m_timer; + qint64 m_elapsedTimeOffset; + char *audioBuffer; + pa_stream *m_stream; + QTime m_timeStamp; + QTime m_clockStamp; + QByteArray m_streamName; + QByteArray m_device; + QByteArray m_tempBuffer; +}; + +class InputPrivate : public QIODevice +{ + Q_OBJECT +public: + InputPrivate(QPulseAudioInput *audio); + ~InputPrivate() {}; + + qint64 readData(char *data, qint64 len); + qint64 writeData(const char *data, qint64 len); + + void trigger(); + +private: + QPulseAudioInput *m_audioDevice; +}; + +QT_END_NAMESPACE + +#endif diff --git a/src/plugins/pulseaudio/qaudiooutput_pulse.cpp b/src/plugins/pulseaudio/qaudiooutput_pulse.cpp new file mode 100644 index 000000000..8e5b3999c --- /dev/null +++ b/src/plugins/pulseaudio/qaudiooutput_pulse.cpp @@ -0,0 +1,573 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#include "qaudiooutput_pulse.h" +#include "qaudiodeviceinfo_pulse.h" +#include "qpulseaudioengine.h" +#include "qpulsehelpers.h" + +QT_BEGIN_NAMESPACE + +const int PeriodTimeMs = 20; + +static void outputStreamWriteCallback(pa_stream *stream, size_t length, void *userdata) +{ + Q_UNUSED(stream); + Q_UNUSED(length); + Q_UNUSED(userdata); + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); +} + +static void outputStreamStateCallback(pa_stream *stream, void *userdata) +{ + Q_UNUSED(userdata) + pa_stream_state_t state = pa_stream_get_state(stream); +#ifdef DEBUG_PULSE + qDebug() << "Stream state: " << QPulseAudioInternal::stateToQString(state); +#endif + switch (state) { + case PA_STREAM_CREATING: + case PA_STREAM_READY: + case PA_STREAM_TERMINATED: + break; + + case PA_STREAM_FAILED: + default: + qWarning() << QString("Stream error: %1").arg(pa_strerror(pa_context_errno(pa_stream_get_context(stream)))); + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); + break; + } +} + +static void outputStreamUnderflowCallback(pa_stream *stream, void *userdata) +{ + Q_UNUSED(stream) + ((QPulseAudioOutput*)userdata)->streamUnderflowCallback(); + qWarning() << "Got a buffer underflow!"; +} + +static void outputStreamOverflowCallback(pa_stream *stream, void *userdata) +{ + Q_UNUSED(stream) + Q_UNUSED(userdata) + qWarning() << "Got a buffer overflow!"; +} + +static void outputStreamLatencyCallback(pa_stream *stream, void *userdata) +{ + Q_UNUSED(stream) + Q_UNUSED(userdata) + +#ifdef DEBUG_PULSE + const pa_timing_info *info = pa_stream_get_timing_info(stream); + + qDebug() << "Write index corrupt: " << info->write_index_corrupt; + qDebug() << "Write index: " << info->write_index; + qDebug() << "Read index corrupt: " << info->read_index_corrupt; + qDebug() << "Read index: " << info->read_index; + qDebug() << "Sink usec: " << info->sink_usec; + qDebug() << "Configured sink usec: " << info->configured_sink_usec; +#endif +} + +static void outputStreamSuccessCallback(pa_stream *stream, int success, void *userdata) +{ + Q_UNUSED(stream); + Q_UNUSED(success); + Q_UNUSED(userdata); + + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); +} + +static void outputStreamDrainComplete(pa_stream *stream, int success, void *userdata) +{ + Q_UNUSED(stream); + Q_UNUSED(success); + Q_UNUSED(userdata); + +#ifdef DEBUG_PULSE + qDebug() << "Draining completed successfully: " << (bool)success; +#endif +} + +QPulseAudioOutput::QPulseAudioOutput(const QByteArray &device) + : m_device(device) + , m_errorState(QAudio::NoError) + , m_deviceState(QAudio::StoppedState) + , m_pullMode(true) + , m_opened(false) + , m_audioSource(0) + , m_bytesAvailable(0) + , m_stream(0) + , m_notifyInterval(1000) + , m_periodSize(0) + , m_bufferSize(0) + , m_totalTimeValue(0) + , m_tickTimer(new QTimer(this)) + , m_audioBuffer(0) + , m_resuming(false) +{ + connect(m_tickTimer, SIGNAL(timeout()), SLOT(userFeed())); +} + +QPulseAudioOutput::~QPulseAudioOutput() +{ + close(); + disconnect(m_tickTimer, SIGNAL(timeout())); + QCoreApplication::processEvents(); +} + +QAudio::Error QPulseAudioOutput::error() const +{ + return m_errorState; +} + +QAudio::State QPulseAudioOutput::state() const +{ + return m_deviceState; +} + +void QPulseAudioOutput::streamUnderflowCallback() +{ + if (m_deviceState != QAudio::IdleState && !m_resuming) { + m_errorState = QAudio::UnderrunError; + emit errorChanged(m_errorState); + m_deviceState = QAudio::IdleState; + emit stateChanged(m_deviceState); + } +} + +void QPulseAudioOutput::start(QIODevice *device) +{ + if (m_deviceState != QAudio::StoppedState) + m_deviceState = QAudio::StoppedState; + + m_errorState = QAudio::NoError; + + // Handle change of mode + if (m_audioSource && !m_pullMode) { + delete m_audioSource; + m_audioSource = 0; + } + + close(); + + m_pullMode = true; + m_audioSource = device; + + m_deviceState = QAudio::ActiveState; + + open(); + + emit stateChanged(m_deviceState); +} + +QIODevice *QPulseAudioOutput::start() +{ + if (m_deviceState != QAudio::StoppedState) + m_deviceState = QAudio::StoppedState; + + m_errorState = QAudio::NoError; + + // Handle change of mode + if (m_audioSource && !m_pullMode) { + delete m_audioSource; + m_audioSource = 0; + } + + close(); + + m_audioSource = new OutputPrivate(this); + m_audioSource->open(QIODevice::WriteOnly|QIODevice::Unbuffered); + m_pullMode = false; + + m_deviceState = QAudio::IdleState; + + open(); + + emit stateChanged(m_deviceState); + + return m_audioSource; +} + +bool QPulseAudioOutput::open() +{ + if (m_opened) + return false; + + pa_sample_spec spec = QPulseAudioInternal::audioFormatToSampleSpec(m_format); + + if (!pa_sample_spec_valid(&spec)) { + m_errorState = QAudio::OpenError; + m_deviceState = QAudio::StoppedState; + return false; + } + + m_totalTimeValue = 0; + m_elapsedTimeOffset = 0; + m_timeStamp.restart(); + + if (m_streamName.isNull()) + m_streamName = QString(QLatin1String("QtmPulseStream-%1-%2")).arg(::getpid()).arg(quintptr(this)).toUtf8(); + +#ifdef DEBUG_PULSE + qDebug() << "Format: " << QPulseAudioInternal::sampleFormatToQString(spec.format); + qDebug() << "Rate: " << spec.rate; + qDebug() << "Channels: " << spec.channels; + qDebug() << "Frame size: " << pa_frame_size(&spec); +#endif + + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + pa_threaded_mainloop_lock(pulseEngine->mainloop()); + m_stream = pa_stream_new(pulseEngine->context(), m_streamName.constData(), &spec, 0); + + pa_stream_set_state_callback(m_stream, outputStreamStateCallback, this); + pa_stream_set_write_callback(m_stream, outputStreamWriteCallback, this); + + pa_stream_set_underflow_callback(m_stream, outputStreamUnderflowCallback, this); + pa_stream_set_overflow_callback(m_stream, outputStreamOverflowCallback, this); + pa_stream_set_latency_update_callback(m_stream, outputStreamLatencyCallback, this); + + if (pa_stream_connect_playback(m_stream, m_device.data(), NULL, (pa_stream_flags_t)0, NULL, NULL) < 0) { + qWarning() << "pa_stream_connect_playback() failed!"; + return false; + } + + while (pa_stream_get_state(m_stream) != PA_STREAM_READY) { + pa_threaded_mainloop_wait(pulseEngine->mainloop()); + } + pa_threaded_mainloop_unlock(pulseEngine->mainloop()); + +#ifdef DEBUG_PULSE + const pa_buffer_attr *buffer = pa_stream_get_buffer_attr(m_stream); + + qDebug() << "Buffering info:"; + qDebug() << "\tMax length: " << buffer->maxlength; + qDebug() << "\tTarget length: " << buffer->tlength; + qDebug() << "\tPre-buffering: " << buffer->prebuf; + qDebug() << "\tMinimum request: " << buffer->minreq; + qDebug() << "\tFragment size: " << buffer->fragsize; +#endif + + m_periodSize = pa_usec_to_bytes(PeriodTimeMs*1000, &spec); + m_opened = true; + m_tickTimer->start(PeriodTimeMs); + + m_elapsedTimeOffset = 0; + m_timeStamp.restart(); + m_clockStamp.restart(); + + return true; +} + +void QPulseAudioOutput::userFeed() +{ + if (m_deviceState == QAudio::StoppedState || m_deviceState == QAudio::SuspendedState) + return; + + if (m_deviceState == QAudio::IdleState) + m_bytesAvailable = bytesFree(); + + deviceReady(); +} + +void QPulseAudioOutput::close() +{ + m_tickTimer->stop(); + + if (m_stream) { + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + pa_threaded_mainloop_lock(pulseEngine->mainloop()); + pa_operation *o; + + pa_stream_set_write_callback(m_stream, NULL, NULL); + + if (!(o = pa_stream_drain(m_stream, outputStreamDrainComplete, NULL))) { + qWarning() << QString("pa_stream_drain(): %1").arg(pa_strerror(pa_context_errno(pa_stream_get_context(m_stream)))); + return; + } + pa_operation_unref(o); + + pa_stream_disconnect(m_stream); + pa_stream_unref(m_stream); + m_stream = NULL; + + pa_threaded_mainloop_unlock(pulseEngine->mainloop()); + } + + if (!m_pullMode && m_audioSource) { + delete m_audioSource; + m_audioSource = 0; + } + m_opened = false; +} + +bool QPulseAudioOutput::deviceReady() +{ + m_resuming = false; + + if (m_pullMode) { + int l = 0; + int chunks = m_bytesAvailable/m_periodSize; + if (chunks==0) { + m_bytesAvailable = bytesFree(); + return false; + } + + char buffer[m_periodSize]; + + l = m_audioSource->read(buffer, m_periodSize); + if (l > 0) { + if (m_deviceState != QAudio::ActiveState) + return true; + + qint64 bytesWritten = write(buffer, l); + Q_UNUSED(bytesWritten); + } + } + + if (m_deviceState != QAudio::ActiveState) + return true; + + if (m_notifyInterval && (m_timeStamp.elapsed() + m_elapsedTimeOffset) > m_notifyInterval) { + emit notify(); + m_elapsedTimeOffset = m_timeStamp.elapsed() + m_elapsedTimeOffset - m_notifyInterval; + m_timeStamp.restart(); + } + + return true; +} + +qint64 QPulseAudioOutput::write(const char *data, qint64 len) +{ + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + + pa_threaded_mainloop_lock(pulseEngine->mainloop()); + len = qMin(len, static_cast(pa_stream_writable_size(m_stream))); + pa_stream_write(m_stream, data, len, 0, 0, PA_SEEK_RELATIVE); + pa_threaded_mainloop_unlock(pulseEngine->mainloop()); + m_totalTimeValue += len; + + m_errorState = QAudio::NoError; + if (m_deviceState != QAudio::ActiveState) { + m_deviceState = QAudio::ActiveState; + emit stateChanged(m_deviceState); + } + + return len; +} + +void QPulseAudioOutput::stop() +{ + if (m_deviceState == QAudio::StoppedState) + return; + + m_errorState = QAudio::NoError; + m_deviceState = QAudio::StoppedState; + close(); + emit stateChanged(m_deviceState); +} + +int QPulseAudioOutput::bytesFree() const +{ + if (m_deviceState != QAudio::ActiveState && m_deviceState != QAudio::IdleState) + return 0; + + return pa_stream_writable_size(m_stream); +} + +int QPulseAudioOutput::periodSize() const +{ + return m_periodSize; +} + +void QPulseAudioOutput::setBufferSize(int value) +{ + m_bufferSize = value; +} + +int QPulseAudioOutput::bufferSize() const +{ + return m_bufferSize; +} + +void QPulseAudioOutput::setNotifyInterval(int ms) +{ + m_notifyInterval = qMax(0, ms); +} + +int QPulseAudioOutput::notifyInterval() const +{ + return m_notifyInterval; +} + +qint64 QPulseAudioOutput::processedUSecs() const +{ + qint64 result = qint64(1000000) * m_totalTimeValue / + (m_format.channels() * (m_format.sampleSize() / 8)) / + m_format.frequency(); + + return result; +} + +void QPulseAudioOutput::resume() +{ + if (m_deviceState == QAudio::SuspendedState) { + m_resuming = true; + + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + + pa_threaded_mainloop_lock(pulseEngine->mainloop()); + + pa_operation *operation = pa_stream_cork(m_stream, 0, outputStreamSuccessCallback, NULL); + + while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING) + pa_threaded_mainloop_wait(pulseEngine->mainloop()); + + pa_operation_unref(operation); + + operation = pa_stream_trigger(m_stream, outputStreamSuccessCallback, NULL); + + while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING) + pa_threaded_mainloop_wait(pulseEngine->mainloop()); + + pa_operation_unref(operation); + + pa_threaded_mainloop_unlock(pulseEngine->mainloop()); + + m_deviceState = QAudio::ActiveState; + + m_errorState = QAudio::NoError; + m_tickTimer->start(PeriodTimeMs); + + emit stateChanged(m_deviceState); + } +} + +void QPulseAudioOutput::setFormat(const QAudioFormat &format) +{ + m_format = format; +} + +QAudioFormat QPulseAudioOutput::format() const +{ + return m_format; +} + +void QPulseAudioOutput::suspend() +{ + if (m_deviceState == QAudio::ActiveState || m_deviceState == QAudio::IdleState) { + m_tickTimer->stop(); + m_deviceState = QAudio::SuspendedState; + m_errorState = QAudio::NoError; + emit stateChanged(m_deviceState); + + QPulseAudioEngine *pulseEngine = QPulseAudioEngine::instance(); + pa_operation *operation; + + pa_threaded_mainloop_lock(pulseEngine->mainloop()); + + operation = pa_stream_cork(m_stream, 1, outputStreamSuccessCallback, NULL); + + while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING) + pa_threaded_mainloop_wait(pulseEngine->mainloop()); + + pa_operation_unref(operation); + + pa_threaded_mainloop_unlock(pulseEngine->mainloop()); + } +} + +qint64 QPulseAudioOutput::elapsedUSecs() const +{ + if (m_deviceState == QAudio::StoppedState) + return 0; + + return m_clockStamp.elapsed() * 1000; +} + +void QPulseAudioOutput::reset() +{ + stop(); +} + +OutputPrivate::OutputPrivate(QPulseAudioOutput *audio) +{ + m_audioDevice = qobject_cast(audio); +} + +qint64 OutputPrivate::readData(char *data, qint64 len) +{ + Q_UNUSED(data) + Q_UNUSED(len) + + return 0; +} + +qint64 OutputPrivate::writeData(const char *data, qint64 len) +{ + int retry = 0; + qint64 written = 0; + + if ((m_audioDevice->m_deviceState == QAudio::ActiveState) + ||(m_audioDevice->m_deviceState == QAudio::IdleState)) { + while(written < len) { + int chunk = m_audioDevice->write(data+written, (len-written)); + if (chunk <= 0) + retry++; + written+=chunk; + if (retry > 10) + return written; + } + } + + return written; +} + +QT_END_NAMESPACE + +#include "moc_qaudiooutput_pulse.cpp" diff --git a/src/plugins/pulseaudio/qaudiooutput_pulse.h b/src/plugins/pulseaudio/qaudiooutput_pulse.h new file mode 100644 index 000000000..064754dbc --- /dev/null +++ b/src/plugins/pulseaudio/qaudiooutput_pulse.h @@ -0,0 +1,153 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QAUDIOOUTPUTPULSE_H +#define QAUDIOOUTPUTPULSE_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include +#include +#include + +#include "qaudio.h" +#include "qaudiodeviceinfo.h" +#include "qaudiosystem.h" + +#include + +QT_BEGIN_NAMESPACE + +class QPulseAudioOutput : public QAbstractAudioOutput +{ + friend class OutputPrivate; + Q_OBJECT + +public: + QPulseAudioOutput(const QByteArray &device); + ~QPulseAudioOutput(); + + void start(QIODevice *device); + QIODevice *start(); + void stop(); + void reset(); + void suspend(); + void resume(); + int bytesFree() const; + int periodSize() const; + void setBufferSize(int value); + int bufferSize() const; + void setNotifyInterval(int milliSeconds); + int notifyInterval() const; + qint64 processedUSecs() const; + qint64 elapsedUSecs() const; + QAudio::Error error() const; + QAudio::State state() const; + void setFormat(const QAudioFormat &format); + QAudioFormat format() const; + +public: + void streamUnderflowCallback(); + +private: + bool open(); + void close(); + qint64 write(const char *data, qint64 len); + +private Q_SLOTS: + bool deviceReady(); + void userFeed(); + +private: + QByteArray m_device; + QByteArray m_streamName; + QAudioFormat m_format; + QAudio::Error m_errorState; + QAudio::State m_deviceState; + bool m_pullMode; + bool m_opened; + QIODevice *m_audioSource; + int m_bytesAvailable; + QTimer m_periodTimer; + pa_stream *m_stream; + int m_notifyInterval; + int m_periodSize; + int m_bufferSize; + QTime m_clockStamp; + qint64 m_totalTimeValue; + QTimer *m_tickTimer; + char *m_audioBuffer; + QTime m_timeStamp; + qint64 m_elapsedTimeOffset; + bool m_resuming; +}; + +class OutputPrivate : public QIODevice +{ + friend class QPulseAudioOutput; + Q_OBJECT + +public: + OutputPrivate(QPulseAudioOutput *audio); + virtual ~OutputPrivate() {} + +protected: + qint64 readData(char *data, qint64 len); + qint64 writeData(const char *data, qint64 len); + +private: + QPulseAudioOutput *m_audioDevice; +}; + +QT_END_NAMESPACE + +#endif diff --git a/src/plugins/pulseaudio/qpulseaudioengine.cpp b/src/plugins/pulseaudio/qpulseaudioengine.cpp new file mode 100644 index 000000000..da0963fa9 --- /dev/null +++ b/src/plugins/pulseaudio/qpulseaudioengine.cpp @@ -0,0 +1,354 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include +#include "qpulseaudioengine.h" +#include "qaudiodeviceinfo_pulse.h" +#include "qaudiooutput_pulse.h" +#include "qpulsehelpers.h" + +QT_BEGIN_NAMESPACE + +static void serverInfoCallback(pa_context *context, const pa_server_info *info, void *userdata) +{ + if (!info) { + qWarning() << QString("Failed to get server information: %s").arg(pa_strerror(pa_context_errno(context))); + return; + } + +#ifdef DEBUG_PULSE + char ss[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX]; + + pa_sample_spec_snprint(ss, sizeof(ss), &info->sample_spec); + pa_channel_map_snprint(cm, sizeof(cm), &info->channel_map); + + qDebug() << QString("User name: %1\n" + "Host Name: %2\n" + "Server Name: %3\n" + "Server Version: %4\n" + "Default Sample Specification: %5\n" + "Default Channel Map: %6\n" + "Default Sink: %7\n" + "Default Source: %8\n").arg( + info->user_name, + info->host_name, + info->server_name, + info->server_version, + ss, + cm, + info->default_sink_name, + info->default_source_name); +#endif + + QPulseAudioEngine *pulseEngine = static_cast(userdata); + pulseEngine->m_defaultSink = info->default_sink_name; + pulseEngine->m_defaultSource = info->default_source_name; + + pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); +} + +static void sinkInfoCallback(pa_context *context, const pa_sink_info *info, int isLast, void *userdata) +{ + QPulseAudioEngine *pulseEngine = static_cast(userdata); + QMap stateMap; + stateMap[PA_SINK_INVALID_STATE] = "n/a"; + stateMap[PA_SINK_RUNNING] = "RUNNING"; + stateMap[PA_SINK_IDLE] = "IDLE"; + stateMap[PA_SINK_SUSPENDED] = "SUSPENDED"; + + if (isLast < 0) { + qWarning() << QString("Failed to get sink information: %s").arg(pa_strerror(pa_context_errno(context))); + return; + } + + if (isLast) { + pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); + return; + } + + Q_ASSERT(info); + +#ifdef DEBUG_PULSE + qDebug() << QString("Sink #%1\n" + "\tState: %2\n" + "\tName: %3\n" + "\tDescription: %4\n" + ).arg(QString::number(info->index), + stateMap.value(info->state), + info->name, + info->description); +#endif + + QAudioFormat format = QPulseAudioInternal::sampleSpecToAudioFormat(info->sample_spec); + pulseEngine->m_preferredFormats.insert(info->name, format); + pulseEngine->m_sinks.append(info->name); +} + +static void sourceInfoCallback(pa_context *context, const pa_source_info *info, int isLast, void *userdata) +{ + Q_UNUSED(context) + QPulseAudioEngine *pulseEngine = static_cast(userdata); + + QMap stateMap; + stateMap[PA_SOURCE_INVALID_STATE] = "n/a"; + stateMap[PA_SOURCE_RUNNING] = "RUNNING"; + stateMap[PA_SOURCE_IDLE] = "IDLE"; + stateMap[PA_SOURCE_SUSPENDED] = "SUSPENDED"; + + if (isLast) { + pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); + return; + } + + Q_ASSERT(info); + +#ifdef DEBUG_PULSE + qDebug() << QString("Source #%1\n" + "\tState: %2\n" + "\tName: %3\n" + "\tDescription: %4\n" + ).arg(QString::number(info->index), + stateMap.value(info->state), + info->name, + info->description); +#endif + + QAudioFormat format = QPulseAudioInternal::sampleSpecToAudioFormat(info->sample_spec); + pulseEngine->m_preferredFormats.insert(info->name, format); + pulseEngine->m_sources.append(info->name); +} + +static void contextStateCallbackInit(pa_context *context, void *userdata) +{ + Q_UNUSED(context); +#ifdef DEBUG_PULSE + qDebug() << QPulseAudioInternal::stateToQString(pa_context_get_state(context)); +#endif + QPulseAudioEngine *pulseEngine = reinterpret_cast(userdata); + pa_threaded_mainloop_signal(pulseEngine->mainloop(), 0); +} + +static void contextStateCallback(pa_context *context, void *userdata) +{ + Q_UNUSED(userdata); + Q_UNUSED(context); + +#ifdef DEBUG_PULSE + pa_context_state_t state = pa_context_get_state(context); + qDebug() << QPulseAudioInternal::stateToQString(state); +#endif +} + +Q_GLOBAL_STATIC(QPulseAudioEngine, pulseEngine); + +QPulseAudioEngine::QPulseAudioEngine(QObject *parent) + : QObject(parent) +{ + bool keepGoing = true; + bool ok = true; + + m_mainLoop = pa_threaded_mainloop_new(); + if (m_mainLoop == 0) { + qWarning("Unable to create pulseaudio mainloop"); + return; + } + + if (pa_threaded_mainloop_start(m_mainLoop) != 0) { + qWarning("Unable to start pulseaudio mainloop"); + pa_threaded_mainloop_free(m_mainLoop); + return; + } + + m_mainLoopApi = pa_threaded_mainloop_get_api(m_mainLoop); + + pa_threaded_mainloop_lock(m_mainLoop); + + m_context = pa_context_new(m_mainLoopApi, QString(QLatin1String("QtmPulseContext:%1")).arg(::getpid()).toAscii().constData()); + pa_context_set_state_callback(m_context, contextStateCallbackInit, this); + + if (!m_context) { + qWarning("Unable to create new pulseaudio context"); + pa_threaded_mainloop_free(m_mainLoop); + return; + } + + if (pa_context_connect(m_context, NULL, (pa_context_flags_t)0, NULL) < 0) { + qWarning("Unable to create a connection to the pulseaudio context"); + pa_context_unref(m_context); + pa_threaded_mainloop_free(m_mainLoop); + return; + } + + pa_threaded_mainloop_wait(m_mainLoop); + + while (keepGoing) { + switch (pa_context_get_state(m_context)) { + case PA_CONTEXT_CONNECTING: + case PA_CONTEXT_AUTHORIZING: + case PA_CONTEXT_SETTING_NAME: + break; + + case PA_CONTEXT_READY: +#ifdef DEBUG_PULSE + qDebug("Connection established."); +#endif + keepGoing = false; + break; + + case PA_CONTEXT_TERMINATED: + qCritical("Context terminated."); + keepGoing = false; + ok = false; + break; + + case PA_CONTEXT_FAILED: + default: + qCritical() << QString("Connection failure: %1").arg(pa_strerror(pa_context_errno(m_context))); + keepGoing = false; + ok = false; + } + + if (keepGoing) { + pa_threaded_mainloop_wait(m_mainLoop); + } + } + + if (ok) { + pa_context_set_state_callback(m_context, contextStateCallback, this); + } else { + if (m_context) { + pa_context_unref(m_context); + m_context = 0; + } + } + + pa_threaded_mainloop_unlock(m_mainLoop); + + serverInfo(); + sinks(); + sources(); +} + +QPulseAudioEngine::~QPulseAudioEngine() +{ + if (m_context) { + pa_threaded_mainloop_lock(m_mainLoop); + pa_context_disconnect(m_context); + pa_threaded_mainloop_unlock(m_mainLoop); + m_context = 0; + } + + if (m_mainLoop) { + pa_threaded_mainloop_stop(m_mainLoop); + pa_threaded_mainloop_free(m_mainLoop); + m_mainLoop = 0; + } +} + +void QPulseAudioEngine::serverInfo() +{ + pa_operation *operation; + + pa_threaded_mainloop_lock(m_mainLoop); + + operation = pa_context_get_server_info(m_context, serverInfoCallback, this); + + while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING) + pa_threaded_mainloop_wait(m_mainLoop); + + pa_operation_unref(operation); + + pa_threaded_mainloop_unlock(m_mainLoop); +} + +void QPulseAudioEngine::sinks() +{ + pa_operation *operation; + + pa_threaded_mainloop_lock(m_mainLoop); + + operation = pa_context_get_sink_info_list(m_context, sinkInfoCallback, this); + + while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING) + pa_threaded_mainloop_wait(m_mainLoop); + + pa_operation_unref(operation); + + pa_threaded_mainloop_unlock(m_mainLoop); + + // Swap the default sink to index 0 + m_sinks.removeOne(m_defaultSink); + m_sinks.prepend(m_defaultSink); +} + +void QPulseAudioEngine::sources() +{ + pa_operation *operation; + + pa_threaded_mainloop_lock(m_mainLoop); + + operation = pa_context_get_source_info_list(m_context, sourceInfoCallback, this); + + while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING) + pa_threaded_mainloop_wait(m_mainLoop); + + pa_operation_unref(operation); + + pa_threaded_mainloop_unlock(m_mainLoop); + + // Swap the default source to index 0 + m_sources.removeOne(m_defaultSource); + m_sources.prepend(m_defaultSource); +} + +QPulseAudioEngine *QPulseAudioEngine::instance() +{ + return pulseEngine(); +} + +QList QPulseAudioEngine::availableDevices(QAudio::Mode mode) const +{ + return mode == QAudio::AudioOutput ? m_sinks : m_sources; +} + +QT_END_NAMESPACE diff --git a/src/plugins/pulseaudio/qpulseaudioengine.h b/src/plugins/pulseaudio/qpulseaudioengine.h new file mode 100644 index 000000000..3519be79d --- /dev/null +++ b/src/plugins/pulseaudio/qpulseaudioengine.h @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QPULSEAUDIOENGINE_H +#define QPULSEAUDIOENGINE_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include +#include +#include "qpulsehelpers.h" +#include + +QT_BEGIN_NAMESPACE + +class QPulseAudioEngine : public QObject +{ + Q_OBJECT + +public: + QPulseAudioEngine(QObject *parent = 0); + ~QPulseAudioEngine(); + + static QPulseAudioEngine *instance(); + pa_threaded_mainloop *mainloop() { return m_mainLoop; } + pa_context *context() { return m_context; } + + QList availableDevices(QAudio::Mode mode) const; + +private: + void serverInfo(); + void sinks(); + void sources(); + +public: + QList m_sinks; + QList m_sources; + QMap m_preferredFormats; + + QByteArray m_defaultSink; + QByteArray m_defaultSource; + +private: + pa_mainloop_api *m_mainLoopApi; + pa_threaded_mainloop *m_mainLoop; + pa_context *m_context; + }; + +QT_END_NAMESPACE + +#endif diff --git a/src/plugins/pulseaudio/qpulseaudioplugin.cpp b/src/plugins/pulseaudio/qpulseaudioplugin.cpp new file mode 100644 index 000000000..99ff09211 --- /dev/null +++ b/src/plugins/pulseaudio/qpulseaudioplugin.cpp @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "qpulseaudioplugin.h" +#include "qaudiodeviceinfo_pulse.h" +#include "qaudiooutput_pulse.h" +#include "qaudioinput_pulse.h" +#include "qpulseaudioengine.h" + +QT_BEGIN_NAMESPACE + +QPulseAudioPlugin::QPulseAudioPlugin(QObject *parent) + : QAudioSystemPlugin(parent) + , m_pulseEngine(QPulseAudioEngine::instance()) +{ +} + +QStringList QPulseAudioPlugin::keys() const +{ + return QStringList() << "default"; +} + +QList QPulseAudioPlugin::availableDevices(QAudio::Mode mode) const +{ + return m_pulseEngine->availableDevices(mode); +} + +QAbstractAudioInput *QPulseAudioPlugin::createInput(const QByteArray &device) +{ + QPulseAudioInput *input = new QPulseAudioInput(device); + return input; +} + +QAbstractAudioOutput *QPulseAudioPlugin::createOutput(const QByteArray &device) +{ + + QPulseAudioOutput *output = new QPulseAudioOutput(device); + return output; +} + +QAbstractAudioDeviceInfo *QPulseAudioPlugin::createDeviceInfo(const QByteArray &device, QAudio::Mode mode) +{ + QPulseAudioDeviceInfo *deviceInfo = new QPulseAudioDeviceInfo(device, mode); + return deviceInfo; +} + +Q_EXPORT_PLUGIN2(qtmedia_pulse, QPulseAudioPlugin); + +QT_END_NAMESPACE diff --git a/src/plugins/pulseaudio/qpulseaudioplugin.h b/src/plugins/pulseaudio/qpulseaudioplugin.h new file mode 100644 index 000000000..e90270e0c --- /dev/null +++ b/src/plugins/pulseaudio/qpulseaudioplugin.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QPULSEAUDIOPLUGIN_H +#define QPULSEAUDIOPLUGIN_H + +#include + +QT_BEGIN_NAMESPACE + +class QPulseAudioEngine; + +class QPulseAudioPlugin : public QAudioSystemPlugin +{ + Q_OBJECT + +public: + QPulseAudioPlugin(QObject *parent = 0); + ~QPulseAudioPlugin() {} + + QStringList keys() const; + QList availableDevices(QAudio::Mode mode) const; + QAbstractAudioInput *createInput(const QByteArray &device); + QAbstractAudioOutput *createOutput(const QByteArray &device); + QAbstractAudioDeviceInfo *createDeviceInfo(const QByteArray &device, QAudio::Mode mode); + +private: + QPulseAudioEngine *m_pulseEngine; +}; + +QT_BEGIN_NAMESPACE + +#endif diff --git a/src/plugins/pulseaudio/qpulsehelpers.cpp b/src/plugins/pulseaudio/qpulsehelpers.cpp new file mode 100644 index 000000000..57e50758f --- /dev/null +++ b/src/plugins/pulseaudio/qpulsehelpers.cpp @@ -0,0 +1,220 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qpulsehelpers.h" + +QT_BEGIN_NAMESPACE + +namespace QPulseAudioInternal +{ +pa_sample_spec audioFormatToSampleSpec(const QAudioFormat &format) +{ + pa_sample_spec spec; + + spec.rate = format.frequency(); + spec.channels = format.channels(); + + if (format.sampleSize() == 8) { + spec.format = PA_SAMPLE_U8; + } else if (format.sampleSize() == 16) { + switch (format.byteOrder()) { + case QAudioFormat::BigEndian: spec.format = PA_SAMPLE_S16BE; break; + case QAudioFormat::LittleEndian: spec.format = PA_SAMPLE_S16LE; break; + } + } else if (format.sampleSize() == 24) { + switch (format.byteOrder()) { + case QAudioFormat::BigEndian: spec.format = PA_SAMPLE_S24BE; break; + case QAudioFormat::LittleEndian: spec.format = PA_SAMPLE_S24LE; break; + } + } else if (format.sampleSize() == 32) { + switch (format.byteOrder()) { + case QAudioFormat::BigEndian: + format.sampleType() == QAudioFormat::Float ? spec.format = PA_SAMPLE_FLOAT32BE : spec.format = PA_SAMPLE_S32BE; + break; + case QAudioFormat::LittleEndian: + format.sampleType() == QAudioFormat::Float ? spec.format = PA_SAMPLE_FLOAT32LE : spec.format = PA_SAMPLE_S32LE; + break; + } + } else { + spec.format = PA_SAMPLE_INVALID; + } + + return spec; +} + +#ifdef DEBUG_PULSE +QString stateToQString(pa_stream_state_t state) +{ + switch (state) + { + case PA_STREAM_UNCONNECTED: return "Unconnected"; + case PA_STREAM_CREATING: return "Creating"; + case PA_STREAM_READY: return "Ready"; + case PA_STREAM_FAILED: return "Failed"; + case PA_STREAM_TERMINATED: return "Terminated"; + } + + return QString("Unknown state: %0").arg(state); +} + +QString sampleFormatToQString(pa_sample_format format) +{ + switch (format) + { + case PA_SAMPLE_U8: return "Unsigned 8 Bit PCM."; + case PA_SAMPLE_ALAW: return "8 Bit a-Law "; + case PA_SAMPLE_ULAW: return "8 Bit mu-Law"; + case PA_SAMPLE_S16LE: return "Signed 16 Bit PCM, little endian (PC)."; + case PA_SAMPLE_S16BE: return "Signed 16 Bit PCM, big endian."; + case PA_SAMPLE_FLOAT32LE: return "32 Bit IEEE floating point, little endian (PC), range -1.0 to 1.0"; + case PA_SAMPLE_FLOAT32BE: return "32 Bit IEEE floating point, big endian, range -1.0 to 1.0"; + case PA_SAMPLE_S32LE: return "Signed 32 Bit PCM, little endian (PC)."; + case PA_SAMPLE_S32BE: return "Signed 32 Bit PCM, big endian."; + case PA_SAMPLE_S24LE: return "Signed 24 Bit PCM packed, little endian (PC)."; + case PA_SAMPLE_S24BE: return "Signed 24 Bit PCM packed, big endian."; + case PA_SAMPLE_S24_32LE: return "Signed 24 Bit PCM in LSB of 32 Bit words, little endian (PC)."; + case PA_SAMPLE_S24_32BE: return "Signed 24 Bit PCM in LSB of 32 Bit words, big endian."; + case PA_SAMPLE_MAX: return "Upper limit of valid sample types."; + case PA_SAMPLE_INVALID: return "Invalid sample format"; + } + + return QString("Invalid value: %0").arg(format); +} + +QString stateToQString(pa_context_state_t state) +{ + switch (state) + { + case PA_CONTEXT_UNCONNECTED: return "Unconnected"; + case PA_CONTEXT_CONNECTING: return "Connecting"; + case PA_CONTEXT_AUTHORIZING: return "Authorizing"; + case PA_CONTEXT_SETTING_NAME: return "Setting Name"; + case PA_CONTEXT_READY: return "Ready"; + case PA_CONTEXT_FAILED: return "Failed"; + case PA_CONTEXT_TERMINATED: return "Terminated"; + } + + return QString("Unknown state: %0").arg(state); +} +#endif + +QAudioFormat sampleSpecToAudioFormat(pa_sample_spec spec) +{ + QAudioFormat format; + format.setFrequency(spec.rate); + format.setChannelCount(spec.channels); + format.setCodec("audio/pcm"); + + switch (spec.format) { + case PA_SAMPLE_U8: + format.setByteOrder(QAudioFormat::LittleEndian); + format.setSampleType(QAudioFormat::UnSignedInt); + format.setSampleSize(8); + break; + case PA_SAMPLE_ALAW: + // TODO: + break; + case PA_SAMPLE_ULAW: + // TODO: + break; + case PA_SAMPLE_S16LE: + format.setByteOrder(QAudioFormat::LittleEndian); + format.setSampleType(QAudioFormat::SignedInt); + format.setSampleSize(16); + break; + case PA_SAMPLE_S16BE: + format.setByteOrder(QAudioFormat::BigEndian); + format.setSampleType(QAudioFormat::SignedInt); + format.setSampleSize(16); + break; + case PA_SAMPLE_FLOAT32LE: + format.setByteOrder(QAudioFormat::LittleEndian); + format.setSampleType(QAudioFormat::Float); + format.setSampleSize(32); + break; + case PA_SAMPLE_FLOAT32BE: + format.setByteOrder(QAudioFormat::BigEndian); + format.setSampleType(QAudioFormat::Float); + format.setSampleSize(32); + break; + case PA_SAMPLE_S32LE: + format.setByteOrder(QAudioFormat::LittleEndian); + format.setSampleType(QAudioFormat::SignedInt); + format.setSampleSize(32); + break; + case PA_SAMPLE_S32BE: + format.setByteOrder(QAudioFormat::BigEndian); + format.setSampleType(QAudioFormat::SignedInt); + format.setSampleSize(32); + break; + case PA_SAMPLE_S24LE: + format.setByteOrder(QAudioFormat::LittleEndian); + format.setSampleType(QAudioFormat::SignedInt); + format.setSampleSize(24); + break; + case PA_SAMPLE_S24BE: + format.setByteOrder(QAudioFormat::BigEndian); + format.setSampleType(QAudioFormat::SignedInt); + format.setSampleSize(24); + break; + case PA_SAMPLE_S24_32LE: + format.setByteOrder(QAudioFormat::LittleEndian); + format.setSampleType(QAudioFormat::SignedInt); + format.setSampleSize(24); + break; + case PA_SAMPLE_S24_32BE: + format.setByteOrder(QAudioFormat::BigEndian); + format.setSampleType(QAudioFormat::SignedInt); + format.setSampleSize(24); + break; + case PA_SAMPLE_MAX: + case PA_SAMPLE_INVALID: + default: + format.setByteOrder(QAudioFormat::LittleEndian); + format.setSampleType(QAudioFormat::Unknown); + format.setSampleSize(0); + } + + return format; +} +} + +QT_END_NAMESPACE diff --git a/src/plugins/pulseaudio/qpulsehelpers.h b/src/plugins/pulseaudio/qpulsehelpers.h new file mode 100644 index 000000000..255f0cf90 --- /dev/null +++ b/src/plugins/pulseaudio/qpulsehelpers.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QPULSEHELPER_H +#define QPULSEHELPER_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qaudiodeviceinfo.h" +#include +#include + +QT_BEGIN_NAMESPACE + +namespace QPulseAudioInternal +{ +pa_sample_spec audioFormatToSampleSpec(const QAudioFormat &format); +QString stateToQString(pa_stream_state_t state); +QString stateToQString(pa_context_state_t state); +QString sampleFormatToQString(pa_sample_format format); +QAudioFormat sampleSpecToAudioFormat(pa_sample_spec spec); +} + +QT_END_NAMESPACE + +#endif -- cgit v1.2.3