qtmobility/plugins/multimedia/pulseaudio/pulseaudioplayercontrol.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtCore/qobject.h>
       
    43 #include <QtCore/qdebug.h>
       
    44 #include <QtNetwork>
       
    45 
       
    46 #include "wavedecoder.h"
       
    47 #include "pulseaudioplayercontrol.h"
       
    48 
       
    49 // Less than ideal
       
    50 #define PA_SCACHE_ENTRY_SIZE_MAX (1024*1024*16)
       
    51 
       
    52 namespace
       
    53 {
       
    54 
       
    55 inline pa_sample_spec audioFormatToSampleSpec(const QAudioFormat &format)
       
    56 {
       
    57     pa_sample_spec  spec;
       
    58 
       
    59     spec.rate = format.frequency();
       
    60     spec.channels = format.channels();
       
    61 
       
    62     if (format.sampleSize() == 8)
       
    63         spec.format = PA_SAMPLE_U8;
       
    64     else if (format.sampleSize() == 16) {
       
    65         switch (format.byteOrder()) {
       
    66         case QAudioFormat::BigEndian: spec.format = PA_SAMPLE_S16BE; break;
       
    67         case QAudioFormat::LittleEndian: spec.format = PA_SAMPLE_S16LE; break;
       
    68         }
       
    69     }
       
    70     else if (format.sampleSize() == 32) {
       
    71         switch (format.byteOrder()) {
       
    72         case QAudioFormat::BigEndian: spec.format = PA_SAMPLE_S32BE; break;
       
    73         case QAudioFormat::LittleEndian: spec.format = PA_SAMPLE_S32LE; break;
       
    74         }
       
    75     }
       
    76 
       
    77     return spec;
       
    78 }
       
    79 
       
    80 class PulseDaemon
       
    81 {
       
    82 
       
    83 public:
       
    84      PulseDaemon():m_prepared(false)
       
    85     {
       
    86         prepare();
       
    87     }
       
    88 
       
    89     ~PulseDaemon()
       
    90     {
       
    91         if (m_prepared)
       
    92             release();
       
    93     }
       
    94 
       
    95     inline void lock()
       
    96     {
       
    97         pa_threaded_mainloop_lock(m_mainLoop);
       
    98     }
       
    99 
       
   100     inline void unlock()
       
   101     {
       
   102         pa_threaded_mainloop_unlock(m_mainLoop);
       
   103     }
       
   104 
       
   105     inline pa_context *context() const
       
   106     {
       
   107         return m_context;
       
   108     }
       
   109 
       
   110 private:
       
   111     void prepare()
       
   112     {
       
   113         m_mainLoop = pa_threaded_mainloop_new();
       
   114         if (m_mainLoop == 0) {
       
   115             qWarning("PulseAudioService: unable to create pulseaudio mainloop");
       
   116             return;
       
   117         }
       
   118 
       
   119         if (pa_threaded_mainloop_start(m_mainLoop) != 0) {
       
   120             qWarning("PulseAudioService: unable to start pulseaudio mainloop");
       
   121             pa_threaded_mainloop_free(m_mainLoop);
       
   122             return;
       
   123         }
       
   124 
       
   125         m_mainLoopApi = pa_threaded_mainloop_get_api(m_mainLoop);
       
   126 
       
   127         lock();
       
   128         m_context = pa_context_new(m_mainLoopApi, QString("QtPulseAudio:%1").arg(::getpid()).toAscii().constData());
       
   129         if (m_context == 0) {
       
   130             qWarning("PulseAudioService: Unable to create new pulseaudio context");
       
   131             pa_threaded_mainloop_free(m_mainLoop);
       
   132             return;
       
   133         }
       
   134 
       
   135         if (pa_context_connect(m_context, NULL, (pa_context_flags_t)0, NULL) < 0) {
       
   136             qWarning("PulseAudioService: pa_context_connect() failed");
       
   137             pa_context_unref(m_context);
       
   138             pa_threaded_mainloop_free(m_mainLoop);
       
   139             return;
       
   140         }
       
   141         unlock();
       
   142 
       
   143         m_prepared = true;
       
   144     }
       
   145 
       
   146     void release()
       
   147     {
       
   148         if (!m_prepared) return;
       
   149         pa_threaded_mainloop_stop(m_mainLoop);
       
   150         pa_threaded_mainloop_free(m_mainLoop);
       
   151         m_prepared = false;
       
   152     }
       
   153 
       
   154     bool m_prepared;
       
   155     pa_context *m_context;
       
   156     pa_threaded_mainloop *m_mainLoop;
       
   157     pa_mainloop_api *m_mainLoopApi;
       
   158 };
       
   159 
       
   160 }
       
   161 
       
   162 Q_GLOBAL_STATIC(PulseDaemon, daemon)
       
   163 
       
   164 
       
   165 
       
   166 PulseAudioPlayerControl::PulseAudioPlayerControl(QObject *parent) :
       
   167     QMediaPlayerControl(parent),
       
   168     m_muted(false),
       
   169     m_playQueued(false),
       
   170     m_volume(100),
       
   171     m_duration(0),
       
   172     m_dataUploaded(0),
       
   173     m_state(QMediaPlayer::StoppedState),
       
   174     m_status(QMediaPlayer::NoMedia),
       
   175     m_reply(0),
       
   176     m_stream(0),
       
   177     m_networkAccessManager(0)
       
   178 {
       
   179 }
       
   180 
       
   181 PulseAudioPlayerControl::~PulseAudioPlayerControl()
       
   182 {
       
   183     delete m_reply;
       
   184 
       
   185     unloadSample();
       
   186 }
       
   187 
       
   188 QMediaPlayer::State PulseAudioPlayerControl::state() const
       
   189 {
       
   190     return m_state;
       
   191 }
       
   192 
       
   193 QMediaPlayer::MediaStatus PulseAudioPlayerControl::mediaStatus() const
       
   194 {
       
   195     return m_status;
       
   196 }
       
   197 
       
   198 qint64 PulseAudioPlayerControl::duration() const
       
   199 {
       
   200     return m_duration;
       
   201 }
       
   202 
       
   203 qint64 PulseAudioPlayerControl::position() const
       
   204 {
       
   205     return 0;
       
   206 }
       
   207 
       
   208 void PulseAudioPlayerControl::setPosition(qint64 position)
       
   209 {
       
   210     Q_UNUSED(position);
       
   211 }
       
   212 
       
   213 int PulseAudioPlayerControl::volume() const
       
   214 {
       
   215     return m_volume;
       
   216 }
       
   217 
       
   218 void PulseAudioPlayerControl::setVolume(int volume)
       
   219 {
       
   220     if (m_volume == volume)
       
   221         return;
       
   222 
       
   223     emit volumeChanged(m_volume = volume);
       
   224 }
       
   225 
       
   226 bool PulseAudioPlayerControl::isMuted() const
       
   227 {
       
   228     return m_muted;
       
   229 }
       
   230 
       
   231 void PulseAudioPlayerControl::setMuted(bool muted)
       
   232 {
       
   233     if (m_muted == muted)
       
   234         return;
       
   235 
       
   236     emit mutedChanged(m_muted = muted);
       
   237 }
       
   238 
       
   239 int PulseAudioPlayerControl::bufferStatus() const
       
   240 {
       
   241     return 0;
       
   242 }
       
   243 
       
   244 bool PulseAudioPlayerControl::isAudioAvailable() const
       
   245 {
       
   246     return m_status != QMediaPlayer::NoMedia && m_status != QMediaPlayer::LoadingMedia;
       
   247 }
       
   248 
       
   249 bool PulseAudioPlayerControl::isVideoAvailable() const
       
   250 {
       
   251     return false;
       
   252 }
       
   253 
       
   254 bool PulseAudioPlayerControl::isSeekable() const
       
   255 {
       
   256     return false;
       
   257 }
       
   258 
       
   259 QMediaTimeRange PulseAudioPlayerControl::availablePlaybackRanges() const
       
   260 {
       
   261     return QMediaTimeRange();
       
   262 }
       
   263 
       
   264 qreal PulseAudioPlayerControl::playbackRate() const
       
   265 {
       
   266     return 1.0;
       
   267 }
       
   268 
       
   269 void PulseAudioPlayerControl::setPlaybackRate(qreal rate)
       
   270 {
       
   271     Q_UNUSED(rate);
       
   272 }
       
   273 
       
   274 QMediaContent PulseAudioPlayerControl::media() const
       
   275 {
       
   276     return m_media;
       
   277 }
       
   278 
       
   279 const QIODevice * PulseAudioPlayerControl::mediaStream() const
       
   280 {
       
   281     return m_media.isNull() ? m_stream : 0;
       
   282 }
       
   283 
       
   284 void PulseAudioPlayerControl::setMedia(const QMediaContent &media, QIODevice *stream)
       
   285 {
       
   286     if (media.isNull() && stream == 0) {
       
   287         m_media = QMediaContent();
       
   288         m_stream = 0;
       
   289         unloadSample();
       
   290         return;
       
   291     }
       
   292 
       
   293     if (stream != 0) {
       
   294         if (m_stream == stream)
       
   295             return;
       
   296 
       
   297         m_stream = stream;
       
   298         m_media = QMediaContent();
       
   299     }
       
   300     else {
       
   301         if (m_media == media)
       
   302             return;
       
   303         m_media = media;
       
   304 
       
   305         if (m_networkAccessManager == 0)
       
   306             m_networkAccessManager = new QNetworkAccessManager(this);
       
   307 
       
   308         m_stream = m_networkAccessManager->get(QNetworkRequest(m_media.canonicalUrl()));
       
   309     }
       
   310 
       
   311     unloadSample();
       
   312     loadSample();
       
   313 
       
   314     emit mediaChanged(m_media);
       
   315 }
       
   316 
       
   317 void PulseAudioPlayerControl::play()
       
   318 {
       
   319     if (m_status == QMediaPlayer::LoadingMedia) {
       
   320         m_playQueued = true;
       
   321         return;
       
   322     }
       
   323 
       
   324     if (m_status != QMediaPlayer::BufferedMedia ||
       
   325         m_state == QMediaPlayer::PlayingState)
       
   326         return;
       
   327 
       
   328 #ifdef MAEMO_VOLUME
       
   329     // Need to do this in code! (TODO)
       
   330     QString cmd = QString("/usr/bin/pasr -r -s x-maemo|/usr/bin/tail -n3|/usr/bin/head -n1|/usr/bin/awk '{print $3}'|/bin/sed 's/\%//g'>/tmp/vol");
       
   331     system(cmd.toLocal8Bit().constData());
       
   332     QFile volFile("/tmp/vol");
       
   333     volFile.open(QIODevice::ReadOnly);
       
   334     QByteArray volNum = volFile.readLine();
       
   335     cmd = QString("/usr/bin/pasr -u -s event -l %1 -c mono").arg((m_volume+QString(volNum.constData()).toInt())/2);
       
   336     system(cmd.toLocal8Bit().constData());
       
   337 #endif
       
   338 
       
   339     daemon()->lock();
       
   340     pa_operation_unref(
       
   341             pa_context_play_sample(daemon()->context(),
       
   342                                    m_name.constData(),
       
   343                                    0,
       
   344                                   -1,
       
   345                                    play_callback,
       
   346                                    this)
       
   347             );
       
   348     daemon()->unlock();
       
   349 
       
   350     m_playbackTime.start();
       
   351 
       
   352     emit stateChanged(m_state = QMediaPlayer::PlayingState);
       
   353 }
       
   354 
       
   355 void PulseAudioPlayerControl::pause()
       
   356 {
       
   357     emit stateChanged(m_state = QMediaPlayer::StoppedState);
       
   358 }
       
   359 
       
   360 void PulseAudioPlayerControl::stop()
       
   361 {
       
   362     emit stateChanged(m_state = QMediaPlayer::StoppedState);
       
   363 }
       
   364 
       
   365 void PulseAudioPlayerControl::loadSample()
       
   366 {
       
   367     m_waveDecoder = new WaveDecoder(m_stream);
       
   368     connect(m_waveDecoder, SIGNAL(formatKnown()), SLOT(decoderReady()));
       
   369     connect(m_waveDecoder, SIGNAL(invalidFormat()), SLOT(decoderError()));
       
   370 
       
   371     m_status = QMediaPlayer::LoadingMedia;
       
   372     emit mediaStatusChanged(m_status);
       
   373 }
       
   374 
       
   375 void PulseAudioPlayerControl::unloadSample()
       
   376 {
       
   377     if (m_status != QMediaPlayer::BufferedMedia)
       
   378         return;
       
   379 
       
   380     m_status = QMediaPlayer::NoMedia;
       
   381 
       
   382     daemon()->lock();
       
   383     pa_context_remove_sample(daemon()->context(), m_name.constData(), NULL, NULL);
       
   384     daemon()->unlock();
       
   385 
       
   386     m_duration = 0;
       
   387     m_dataUploaded = 0;
       
   388 }
       
   389 
       
   390 void PulseAudioPlayerControl::timerEvent(QTimerEvent *event)
       
   391 {
       
   392     if (m_state == QMediaPlayer::PlayingState) {
       
   393         m_state = QMediaPlayer::StoppedState;
       
   394         emit stateChanged(m_state);
       
   395     }
       
   396 
       
   397     killTimer(event->timerId());
       
   398 }
       
   399 
       
   400 void PulseAudioPlayerControl::decoderReady()
       
   401 {
       
   402     if (m_waveDecoder->size() >= PA_SCACHE_ENTRY_SIZE_MAX) {
       
   403         m_status = QMediaPlayer::InvalidMedia;
       
   404         emit mediaStatusChanged(m_status);
       
   405         qWarning("QtPulseAudio: attempting to load to large a sample");
       
   406         return;
       
   407     }
       
   408 
       
   409     if (m_name.isNull())
       
   410         m_name = QString("QtPulseSample-%1-%2").arg(::getpid()).arg(reinterpret_cast<unsigned long>(this)).toUtf8();
       
   411 
       
   412     pa_sample_spec spec = audioFormatToSampleSpec(m_waveDecoder->audioFormat());
       
   413 
       
   414     daemon()->lock();
       
   415     pa_stream *stream = pa_stream_new(daemon()->context(), m_name.constData(), &spec, 0);
       
   416     pa_stream_set_state_callback(stream, stream_state_callback, this);
       
   417     pa_stream_set_write_callback(stream, stream_write_callback, this);
       
   418     pa_stream_connect_upload(stream, (size_t)m_waveDecoder->size());
       
   419     daemon()->unlock();
       
   420 }
       
   421 
       
   422 void PulseAudioPlayerControl::decoderError()
       
   423 {
       
   424     emit mediaStatusChanged(m_status = QMediaPlayer::InvalidMedia);
       
   425 }
       
   426 
       
   427 void PulseAudioPlayerControl::checkPlayTime()
       
   428 {
       
   429     int elapsed = m_playbackTime.elapsed();
       
   430 
       
   431     if (elapsed >= m_duration) {
       
   432         m_state = QMediaPlayer::StoppedState;
       
   433         emit stateChanged(m_state);
       
   434     }
       
   435     else
       
   436         startTimer(m_duration - elapsed);
       
   437 }
       
   438 
       
   439 void PulseAudioPlayerControl::stream_write_callback(pa_stream *s, size_t length, void *userdata)
       
   440 {
       
   441     Q_UNUSED(length);
       
   442 
       
   443     PulseAudioPlayerControl *self = reinterpret_cast<PulseAudioPlayerControl*>(userdata);
       
   444 
       
   445     size_t bufferSize = qMin(pa_stream_writable_size(s),
       
   446                              size_t(self->m_waveDecoder->bytesAvailable()));
       
   447     char buffer[bufferSize];
       
   448 
       
   449     size_t len = 0;
       
   450     while (len < length) {
       
   451         qint64 read = self->m_waveDecoder->read(buffer, qMin(bufferSize, length -len));
       
   452         if (read > 0) {
       
   453             if (pa_stream_write(s, buffer, size_t(read), 0, 0, PA_SEEK_RELATIVE) == 0)
       
   454                 len += size_t(read);
       
   455             else
       
   456                 break;
       
   457         }
       
   458     }
       
   459     self->m_dataUploaded += len;
       
   460 
       
   461     if (self->m_waveDecoder->size() == self->m_dataUploaded) {
       
   462         pa_stream_finish_upload(s);
       
   463 
       
   464         self->m_duration = self->m_waveDecoder->duration();
       
   465         emit self->durationChanged(self->m_duration);
       
   466 
       
   467         self->m_status = QMediaPlayer::BufferedMedia;
       
   468         emit self->mediaStatusChanged(self->m_status);
       
   469         emit self->audioAvailableChanged(true);
       
   470 
       
   471         self->m_waveDecoder->deleteLater();
       
   472         if (!self->m_media.isNull())
       
   473             self->m_stream->deleteLater();
       
   474 
       
   475         if (self->m_playQueued) {
       
   476             self->m_playQueued = false;
       
   477             QMetaObject::invokeMethod(self, "play");
       
   478         }
       
   479     }
       
   480 }
       
   481 
       
   482 void PulseAudioPlayerControl::stream_state_callback(pa_stream *s, void *userdata)
       
   483 {
       
   484     PulseAudioPlayerControl *self = reinterpret_cast<PulseAudioPlayerControl*>(userdata);
       
   485 
       
   486     switch (pa_stream_get_state(s)) {
       
   487     case PA_STREAM_CREATING:
       
   488     case PA_STREAM_READY:
       
   489     case PA_STREAM_TERMINATED:
       
   490         break;
       
   491 
       
   492     case PA_STREAM_FAILED:
       
   493     default:
       
   494         self->m_status = QMediaPlayer::InvalidMedia;
       
   495         emit self->mediaStatusChanged(self->m_status);
       
   496         break;
       
   497     }
       
   498 }
       
   499 
       
   500 void PulseAudioPlayerControl::play_callback(pa_context *c, int success, void *userdata)
       
   501 {
       
   502     Q_UNUSED(c);
       
   503 
       
   504     PulseAudioPlayerControl *self = reinterpret_cast<PulseAudioPlayerControl*>(userdata);
       
   505 
       
   506     if (success == 1)
       
   507         QMetaObject::invokeMethod(self, "checkPlayTime", Qt::QueuedConnection);
       
   508     else {
       
   509         self->m_state = QMediaPlayer::StoppedState;
       
   510         emit self->stateChanged(self->m_state);
       
   511     }
       
   512 }
       
   513