qtmobility/src/multimedia/qsoundeffect_pulse_p.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 //
       
    43 //  W A R N I N G
       
    44 //  -------------
       
    45 //
       
    46 // This file is not part of the Qt API. It exists purely as an
       
    47 // implementation detail. This header file may change from version to
       
    48 // version without notice, or even be removed.
       
    49 //
       
    50 // We mean it.
       
    51 //
       
    52 
       
    53 #include <QtCore/qcoreapplication.h>
       
    54 #include <QtMultimedia/qaudioformat.h>
       
    55 #include <QtNetwork>
       
    56 #include <QTime>
       
    57 
       
    58 #include "qmediacontent.h"
       
    59 #include "qmediaplayer.h"
       
    60 #include "qsoundeffect_p.h"
       
    61 
       
    62 #include "wavedecoder.h"
       
    63 
       
    64 #include "qsoundeffect_pulse_p.h"
       
    65 
       
    66 #if(QT_MULTIMEDIA_MAEMO5)
       
    67 #include <pulse/ext-stream-restore.h>
       
    68 #endif
       
    69 
       
    70 // Less than ideal
       
    71 #define PA_SCACHE_ENTRY_SIZE_MAX (1024*1024*16)
       
    72 
       
    73 namespace
       
    74 {
       
    75 inline pa_sample_spec audioFormatToSampleSpec(const QAudioFormat &format)
       
    76 {
       
    77     pa_sample_spec  spec;
       
    78 
       
    79     spec.rate = format.frequency();
       
    80     spec.channels = format.channels();
       
    81 
       
    82     if (format.sampleSize() == 8)
       
    83         spec.format = PA_SAMPLE_U8;
       
    84     else if (format.sampleSize() == 16) {
       
    85         switch (format.byteOrder()) {
       
    86             case QAudioFormat::BigEndian: spec.format = PA_SAMPLE_S16BE; break;
       
    87             case QAudioFormat::LittleEndian: spec.format = PA_SAMPLE_S16LE; break;
       
    88         }
       
    89     }
       
    90     else if (format.sampleSize() == 32) {
       
    91         switch (format.byteOrder()) {
       
    92             case QAudioFormat::BigEndian: spec.format = PA_SAMPLE_S32BE; break;
       
    93             case QAudioFormat::LittleEndian: spec.format = PA_SAMPLE_S32LE; break;
       
    94         }
       
    95     }
       
    96 
       
    97     return spec;
       
    98 }
       
    99 
       
   100 class PulseDaemon
       
   101 {
       
   102 public:
       
   103     PulseDaemon():m_prepared(false)
       
   104     {
       
   105         prepare();
       
   106     }
       
   107 
       
   108     ~PulseDaemon()
       
   109     {
       
   110         if (m_prepared)
       
   111             release();
       
   112     }
       
   113 
       
   114     inline void lock()
       
   115     {
       
   116         pa_threaded_mainloop_lock(m_mainLoop);
       
   117     }
       
   118 
       
   119     inline void unlock()
       
   120     {
       
   121         pa_threaded_mainloop_unlock(m_mainLoop);
       
   122     }
       
   123 
       
   124     inline pa_context *context() const
       
   125     {
       
   126         return m_context;
       
   127     }
       
   128 
       
   129     int volume()
       
   130     {
       
   131         return m_volume;
       
   132     }
       
   133 
       
   134 private:
       
   135     void prepare()
       
   136     {
       
   137         m_volume = 100;
       
   138 
       
   139         m_mainLoop = pa_threaded_mainloop_new();
       
   140         if (m_mainLoop == 0) {
       
   141             qWarning("PulseAudioService: unable to create pulseaudio mainloop");
       
   142             return;
       
   143         }
       
   144 
       
   145         if (pa_threaded_mainloop_start(m_mainLoop) != 0) {
       
   146             qWarning("PulseAudioService: unable to start pulseaudio mainloop");
       
   147             pa_threaded_mainloop_free(m_mainLoop);
       
   148             return;
       
   149         }
       
   150 
       
   151         m_mainLoopApi = pa_threaded_mainloop_get_api(m_mainLoop);
       
   152 
       
   153         lock();
       
   154         m_context = pa_context_new(m_mainLoopApi, QString("QtPulseAudio:%1").arg(::getpid()).toAscii().constData());
       
   155 
       
   156 #if(QT_MULTIMEDIA_MAEMO5)
       
   157         pa_context_set_state_callback(m_context, context_state_callback, this);
       
   158 #endif
       
   159         if (m_context == 0) {
       
   160             qWarning("PulseAudioService: Unable to create new pulseaudio context");
       
   161             pa_threaded_mainloop_free(m_mainLoop);
       
   162             return;
       
   163         }
       
   164 
       
   165         if (pa_context_connect(m_context, NULL, (pa_context_flags_t)0, NULL) < 0) {
       
   166             qWarning("PulseAudioService: pa_context_connect() failed");
       
   167             pa_context_unref(m_context);
       
   168             pa_threaded_mainloop_free(m_mainLoop);
       
   169             return;
       
   170         }
       
   171         unlock();
       
   172 
       
   173         m_prepared = true;
       
   174     }
       
   175 
       
   176     void release()
       
   177     {
       
   178         if (!m_prepared) return;
       
   179         pa_threaded_mainloop_stop(m_mainLoop);
       
   180         pa_threaded_mainloop_free(m_mainLoop);
       
   181         m_prepared = false;
       
   182     }
       
   183 
       
   184 #if(QT_MULTIMEDIA_MAEMO5)
       
   185     static void context_state_callback(pa_context *c, void *userdata)
       
   186     {
       
   187         PulseDaemon *self = reinterpret_cast<PulseDaemon*>(userdata);
       
   188         switch (pa_context_get_state(c)) {
       
   189             case PA_CONTEXT_CONNECTING:
       
   190             case PA_CONTEXT_AUTHORIZING:
       
   191             case PA_CONTEXT_SETTING_NAME:
       
   192                 break;
       
   193             case PA_CONTEXT_READY:
       
   194                 pa_ext_stream_restore_set_subscribe_cb(c, &stream_restore_monitor_callback, self);
       
   195                 pa_ext_stream_restore_subscribe(c, 1, NULL, self);
       
   196                 break;
       
   197             default:
       
   198                 break;
       
   199         }
       
   200     }
       
   201     static void stream_restore_monitor_callback(pa_context *c, void *userdata)
       
   202     {
       
   203         PulseDaemon *self = reinterpret_cast<PulseDaemon*>(userdata);
       
   204         pa_ext_stream_restore2_read(c, &stream_restore_info_callback, self);
       
   205     }
       
   206     static void stream_restore_info_callback(pa_context *c, const pa_ext_stream_restore2_info *info,
       
   207             int eol, void *userdata)
       
   208     {
       
   209         Q_UNUSED(c)
       
   210 
       
   211         PulseDaemon *self = reinterpret_cast<PulseDaemon*>(userdata);
       
   212 
       
   213         if (!eol) {
       
   214             if (QString(info->name).startsWith(QLatin1String("sink-input-by-media-role:x-maemo"))) {
       
   215                 const unsigned str_length = 256;
       
   216                 char str[str_length];
       
   217                 pa_cvolume_snprint(str, str_length, &info->volume);
       
   218                 self->m_volume = QString(str).replace(" ","").replace("%","").mid(2).toInt();
       
   219             }
       
   220         }
       
   221     }
       
   222 #endif
       
   223 
       
   224     int  m_volume;
       
   225     bool m_prepared;
       
   226     pa_context *m_context;
       
   227     pa_threaded_mainloop *m_mainLoop;
       
   228     pa_mainloop_api *m_mainLoopApi;
       
   229 };
       
   230 }
       
   231 
       
   232 Q_GLOBAL_STATIC(PulseDaemon, daemon)
       
   233 
       
   234 
       
   235 QSoundEffectPrivate::QSoundEffectPrivate(QObject* parent):
       
   236     QObject(parent),
       
   237     m_muted(false),
       
   238     m_playQueued(false),
       
   239     m_volume(100),
       
   240     m_duration(0),
       
   241     m_dataUploaded(0),
       
   242     m_state(QMediaPlayer::StoppedState),
       
   243     m_status(QMediaPlayer::NoMedia),
       
   244     m_reply(0),
       
   245     m_stream(0),
       
   246     m_networkAccessManager(0)
       
   247 {
       
   248 }
       
   249 
       
   250 QSoundEffectPrivate::~QSoundEffectPrivate()
       
   251 {
       
   252     delete m_reply;
       
   253     unloadSample();
       
   254 }
       
   255 
       
   256 qint64 QSoundEffectPrivate::duration() const
       
   257 {
       
   258     return m_duration;
       
   259 }
       
   260 
       
   261 int QSoundEffectPrivate::volume() const
       
   262 {
       
   263     return m_volume;
       
   264 }
       
   265 
       
   266 bool QSoundEffectPrivate::isMuted() const
       
   267 {
       
   268     return m_muted;
       
   269 }
       
   270 
       
   271 QMediaContent QSoundEffectPrivate::media() const
       
   272 {
       
   273     return m_media;
       
   274 }
       
   275 
       
   276 QMediaPlayer::State QSoundEffectPrivate::state() const
       
   277 {
       
   278     return m_state;
       
   279 }
       
   280 
       
   281 QMediaPlayer::MediaStatus QSoundEffectPrivate::mediaStatus() const
       
   282 {
       
   283     return m_status;
       
   284 }
       
   285 
       
   286 void QSoundEffectPrivate::play()
       
   287 {
       
   288     if (m_status == QMediaPlayer::LoadingMedia) {
       
   289         m_playQueued = true;
       
   290         return;
       
   291     }
       
   292 
       
   293     if (m_status != QMediaPlayer::BufferedMedia ||
       
   294             m_state == QMediaPlayer::PlayingState)
       
   295         return;
       
   296 
       
   297     pa_volume_t m_vol = PA_VOLUME_NORM;
       
   298 
       
   299     daemon()->lock();
       
   300 #if(QT_MULTIMEDIA_MAEMO5)
       
   301     m_vol = PA_VOLUME_NORM/100*((daemon()->volume()+m_volume)/2);
       
   302 #endif
       
   303     pa_operation_unref(
       
   304             pa_context_play_sample(daemon()->context(),
       
   305                 m_name.constData(),
       
   306                 0,
       
   307                 m_vol,
       
   308                 play_callback,
       
   309                 this)
       
   310             );
       
   311     daemon()->unlock();
       
   312 
       
   313     m_playbackTime.start();
       
   314 
       
   315     emit stateChanged(m_state = QMediaPlayer::PlayingState);
       
   316 }
       
   317 
       
   318 void QSoundEffectPrivate::stop()
       
   319 {
       
   320     emit stateChanged(m_state = QMediaPlayer::StoppedState);
       
   321 }
       
   322 
       
   323 void QSoundEffectPrivate::setVolume(int volume)
       
   324 {
       
   325     m_volume = volume;
       
   326 }
       
   327 
       
   328 void QSoundEffectPrivate::setMuted(bool muted)
       
   329 {
       
   330     m_muted = muted;
       
   331 }
       
   332 
       
   333 void QSoundEffectPrivate::setMedia(const QMediaContent &media)
       
   334 {
       
   335     if (media.isNull()) {
       
   336         m_media = QMediaContent();
       
   337         unloadSample();
       
   338         return;
       
   339     }
       
   340     if (m_media == media)
       
   341         return;
       
   342     m_media = media;
       
   343 
       
   344     if (m_networkAccessManager == 0)
       
   345         m_networkAccessManager = new QNetworkAccessManager(this);
       
   346 
       
   347     m_stream = m_networkAccessManager->get(QNetworkRequest(m_media.canonicalUrl()));
       
   348 
       
   349     unloadSample();
       
   350     loadSample();
       
   351 
       
   352     emit mediaChanged(m_media);
       
   353 }
       
   354 
       
   355 void QSoundEffectPrivate::decoderReady()
       
   356 {
       
   357     if (m_waveDecoder->size() >= PA_SCACHE_ENTRY_SIZE_MAX) {
       
   358         m_status = QMediaPlayer::InvalidMedia;
       
   359         emit mediaStatusChanged(m_status);
       
   360         qWarning("QtPulseAudio: attempting to load to large a sample");
       
   361         return;
       
   362     }
       
   363 
       
   364     if (m_name.isNull())
       
   365         m_name = QString("QtPulseSample-%1-%2").arg(::getpid()).arg(int(this)).toUtf8();
       
   366 
       
   367     pa_sample_spec spec = audioFormatToSampleSpec(m_waveDecoder->audioFormat());
       
   368 
       
   369     daemon()->lock();
       
   370     pa_stream *stream = pa_stream_new(daemon()->context(), m_name.constData(), &spec, 0);
       
   371     pa_stream_set_state_callback(stream, stream_state_callback, this);
       
   372     pa_stream_set_write_callback(stream, stream_write_callback, this);
       
   373     pa_stream_connect_upload(stream, (size_t)m_waveDecoder->size());
       
   374     daemon()->unlock();
       
   375 }
       
   376 
       
   377 void QSoundEffectPrivate::decoderError()
       
   378 {
       
   379     emit mediaStatusChanged(m_status = QMediaPlayer::InvalidMedia);
       
   380 }
       
   381 
       
   382 void QSoundEffectPrivate::checkPlayTime()
       
   383 {
       
   384     int elapsed = m_playbackTime.elapsed();
       
   385 
       
   386     if (elapsed >= m_duration) {
       
   387         m_state = QMediaPlayer::StoppedState;
       
   388         emit stateChanged(m_state);
       
   389     }
       
   390     else
       
   391         startTimer(m_duration - elapsed);
       
   392 }
       
   393 
       
   394 void QSoundEffectPrivate::loadSample()
       
   395 {
       
   396     m_waveDecoder = new WaveDecoder(m_stream);
       
   397     connect(m_waveDecoder, SIGNAL(formatKnown()), SLOT(decoderReady()));
       
   398     connect(m_waveDecoder, SIGNAL(invalidFormat()), SLOT(decoderError()));
       
   399 
       
   400     m_status = QMediaPlayer::LoadingMedia;
       
   401     emit mediaStatusChanged(m_status);
       
   402 }
       
   403 
       
   404 void QSoundEffectPrivate::unloadSample()
       
   405 {
       
   406     if (m_status != QMediaPlayer::BufferedMedia)
       
   407         return;
       
   408 
       
   409     m_status = QMediaPlayer::NoMedia;
       
   410 
       
   411     daemon()->lock();
       
   412     pa_context_remove_sample(daemon()->context(), m_name.constData(), NULL, NULL);
       
   413     daemon()->unlock();
       
   414 
       
   415     m_duration = 0;
       
   416     m_dataUploaded = 0;
       
   417 }
       
   418 
       
   419 void QSoundEffectPrivate::timerEvent(QTimerEvent *event)
       
   420 {
       
   421     if (m_state == QMediaPlayer::PlayingState) {
       
   422         m_state = QMediaPlayer::StoppedState;
       
   423         emit stateChanged(m_state);
       
   424     }
       
   425 
       
   426     killTimer(event->timerId());
       
   427 }
       
   428 
       
   429 void QSoundEffectPrivate::stream_write_callback(pa_stream *s, size_t length, void *userdata)
       
   430 {
       
   431     Q_UNUSED(length);
       
   432 
       
   433     QSoundEffectPrivate *self = reinterpret_cast<QSoundEffectPrivate*>(userdata);
       
   434 
       
   435     size_t bufferSize = qMin(pa_stream_writable_size(s),
       
   436             size_t(self->m_waveDecoder->bytesAvailable()));
       
   437     char buffer[bufferSize];
       
   438 
       
   439     size_t len = 0;
       
   440     while (len < length) {
       
   441         qint64 read = self->m_waveDecoder->read(buffer, qMin(bufferSize, length -len));
       
   442         if (read > 0) {
       
   443             if (pa_stream_write(s, buffer, size_t(read), 0, 0, PA_SEEK_RELATIVE) == 0)
       
   444                 len += size_t(read);
       
   445             else
       
   446                 break;
       
   447         }
       
   448     }
       
   449     self->m_dataUploaded += len;
       
   450 
       
   451     if (self->m_waveDecoder->size() == self->m_dataUploaded) {
       
   452         pa_stream_finish_upload(s);
       
   453 
       
   454         self->m_duration = self->m_waveDecoder->duration();
       
   455         emit self->durationChanged(self->m_duration);
       
   456 
       
   457         self->m_status = QMediaPlayer::BufferedMedia;
       
   458         emit self->mediaStatusChanged(self->m_status);
       
   459 
       
   460         self->m_waveDecoder->deleteLater();
       
   461         if (!self->m_media.isNull())
       
   462             self->m_stream->deleteLater();
       
   463 
       
   464         if (self->m_playQueued) {
       
   465             self->m_playQueued = false;
       
   466             QMetaObject::invokeMethod(self, "play");
       
   467         }
       
   468     }
       
   469 }
       
   470 
       
   471 void QSoundEffectPrivate::stream_state_callback(pa_stream *s, void *userdata)
       
   472 {
       
   473     QSoundEffectPrivate *self = reinterpret_cast<QSoundEffectPrivate*>(userdata);
       
   474 
       
   475     switch (pa_stream_get_state(s)) {
       
   476         case PA_STREAM_CREATING:
       
   477         case PA_STREAM_READY:
       
   478         case PA_STREAM_TERMINATED:
       
   479             break;
       
   480 
       
   481         case PA_STREAM_FAILED:
       
   482         default:
       
   483             self->m_status = QMediaPlayer::InvalidMedia;
       
   484             emit self->mediaStatusChanged(self->m_status);
       
   485             break;
       
   486     }
       
   487 }
       
   488 
       
   489 void QSoundEffectPrivate::play_callback(pa_context *c, int success, void *userdata)
       
   490 {
       
   491     Q_UNUSED(c);
       
   492 
       
   493     QSoundEffectPrivate *self = reinterpret_cast<QSoundEffectPrivate*>(userdata);
       
   494 
       
   495     if (success == 1)
       
   496         QMetaObject::invokeMethod(self, "checkPlayTime", Qt::QueuedConnection);
       
   497     else {
       
   498         self->m_state = QMediaPlayer::StoppedState;
       
   499         emit self->stateChanged(self->m_state);
       
   500     }
       
   501 }
       
   502