From cd71055f14b70284d8bd3eb35fadc15b36ba0780 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Mon, 13 Feb 2012 21:16:59 +0100 Subject: use PulseAudio instead of QAudioDevice --- fmrxservice.cpp | 110 +++++++++++++++++++++++++------------------------------- 1 file changed, 48 insertions(+), 62 deletions(-) (limited to 'fmrxservice.cpp') diff --git a/fmrxservice.cpp b/fmrxservice.cpp index 3a990bd..750ae8f 100644 --- a/fmrxservice.cpp +++ b/fmrxservice.cpp @@ -1,90 +1,73 @@ -#include -#include #include #include -#include "fmrxservice.h" #include "fmrxcontrol.h" #include "fmrxproxy.h" +#include "fmrxthread.h" +#include "fmrxservice.h" // Again, this would be incredibly shorter if QAudioOuput could be used. // Unfortunately, it is completely useless in the Harmattan version. struct FmRxPriv { - QAudioOutput *out; - QFile *in; - int fd; + FmRxThread *thread; + FmRxProxy *proxy; + FmRxControl *control; + FmRxRds *rds; bool active; double frequency; + quint16 signalLevel; }; FmRxService::FmRxService(QObject *parent) : QMediaService(parent) { - m_proxy = new FmRxProxy(this); - m_control = new FmRxControl(this); - m_priv = new FmRxPriv; - - QAudioFormat format; - format.setFrequency(48000); - format.setChannels(2); - format.setSampleType(QAudioFormat::SignedInt); - format.setSampleSize(16); - format.setByteOrder(QAudioFormat::LittleEndian); - format.setCodec("audio/pcm"); - - QAudioDeviceInfo info = QAudioDeviceInfo::defaultOutputDevice(); - if (!info.isFormatSupported(format)) { - qWarning("FM radio format not supported by audio output"); - } - qDebug("Starting output to %s", qPrintable(info.deviceName())); - - m_priv->out = new QAudioOutput(info, format, this); - m_priv->in = new QFile(this); - m_priv->active = false; - - connect(m_proxy, SIGNAL(Tuned(double)), this, SLOT(handleTuned(double))); - connect(m_proxy, SIGNAL(Stopped()), this, SLOT(handleStopped())); - connect(m_proxy, SIGNAL(PiReceived(ushort)), this, SIGNAL(piReceived(ushort))); - connect(m_proxy, SIGNAL(PsReceived(QString)), this, SIGNAL(psReceived(QString))); - connect(m_proxy, SIGNAL(RtReceived(QString)), this, SIGNAL(rtReceived(QString))); + d = new FmRxPriv; + d->thread = new FmRxThread(this); + d->proxy = new FmRxProxy(this); + d->control = new FmRxControl(this); + d->active = false; - connect(m_priv->out, SIGNAL(stateChanged(QAudio::State)), - this, SLOT(handleOutState(QAudio::State))); + connect(d->proxy, SIGNAL(Tuned(double)), this, SLOT(handleTuned(double))); + connect(d->proxy, SIGNAL(Stopped()), this, SLOT(handleStopped())); + connect(d->proxy, SIGNAL(SignalLevelChanged(ushort)), this, SLOT(handleSignalLevelChanged(ushort))); + connect(d->proxy, SIGNAL(PiReceived(ushort)), this, SIGNAL(piReceived(ushort))); + connect(d->proxy, SIGNAL(PsReceived(QString)), this, SIGNAL(psReceived(QString))); + connect(d->proxy, SIGNAL(RtReceived(QString)), this, SIGNAL(rtReceived(QString))); } FmRxService::~FmRxService() { - delete m_priv; + delete d; } QMediaControl *FmRxService::requestControl(const char *name) { if (qstrcmp(name, QRadioTunerControl_iid) == 0) - return m_control; + return d->control; return 0; } void FmRxService::releaseControl(QMediaControl *control) { - // Do nothing + // Nothing to do; the control instance will be free when the service is freed } void FmRxService::start() { - m_priv->fd = m_proxy->Connect(); - m_priv->in->open(m_priv->fd, QIODevice::ReadOnly); - m_priv->out->start(m_priv->in); - qDebug("Starting fmrx service"); + if (d->thread->isRunning()) { + qWarning("FmRxService already started"); + return; + } + d->thread->setInputFd(d->proxy->Connect()); + d->thread->start(); } void FmRxService::stop() { - m_priv->out->stop(); - m_priv->in->close(); - close(m_priv->fd); + d->thread->stop(); } bool FmRxService::isAvailable() const @@ -100,55 +83,58 @@ QtMultimediaKit::AvailabilityError FmRxService::availabilityError() const bool FmRxService::isActive() const { - return m_priv->active; + return d->active; } void FmRxService::handleTuned(double frequency) { - if (!qFuzzyCompare(m_priv->frequency, frequency)) { - m_priv->frequency = frequency; + if (!qFuzzyCompare(d->frequency, frequency)) { + d->frequency = frequency; emit tuned(frequency); } - if (!m_priv->active) { - m_priv->active = true; + if (!d->active) { + d->active = true; emit started(); } } void FmRxService::handleStopped() { - if (m_priv->active) { - m_priv->active = false; + if (d->active) { + d->active = false; emit stopped(); } } -void FmRxService::handleOutState(QAudio::State state) +void FmRxService::handleSignalLevelChanged(ushort level) { - qDebug() << "New out state = " << state; - if (state == QAudio::IdleState && - m_priv->out->error() == QAudio::UnderrunError) { - qDebug() << "Restarting"; - m_priv->out->start(m_priv->in); + d->signalLevel = level; + if (d->active) { + emit signalLevelChanged(level); } } double FmRxService::frequency() { - return m_priv->frequency; + return d->frequency; } void FmRxService::setFrequency(double frequency) { - m_proxy->Tune(frequency); + d->proxy->Tune(frequency); } void FmRxService::searchForward() { - m_proxy->SearchForward(); + d->proxy->SearchForward(); } void FmRxService::searchBackward() { - m_proxy->SearchBackward(); + d->proxy->SearchBackward(); +} + +ushort FmRxService::signalLevel() const +{ + return d->signalLevel; } -- cgit v1.2.3