qtmobility/src/multimedia/effects/qsoundeffect_pulse_p.cpp
changeset 14 6fbed849b4f4
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 <qaudioformat.h>
       
    55 #include <QtNetwork>
       
    56 #include <QTime>
       
    57 
       
    58 #include "wavedecoder_p.h"
       
    59 
       
    60 #include "qsoundeffect_pulse_p.h"
       
    61 
       
    62 #if defined(Q_WS_MAEMO_5)
       
    63 #include <pulse/ext-stream-restore.h>
       
    64 #endif
       
    65 
       
    66 #include <unistd.h>
       
    67 
       
    68 // Less than ideal
       
    69 #define PA_SCACHE_ENTRY_SIZE_MAX (1024*1024*16)
       
    70 
       
    71 QT_BEGIN_NAMESPACE
       
    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_vol;
       
   132     }
       
   133 
       
   134 private:
       
   135     void prepare()
       
   136     {
       
   137         m_vol = 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(QLatin1String("QtPulseAudio:%1")).arg(::getpid()).toAscii().constData());
       
   155 
       
   156 #if defined(Q_WS_MAEMO_5)
       
   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 defined(Q_WS_MAEMO_5)
       
   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_vol = QString(str).replace(" ","").replace("%","").mid(2).toInt();
       
   219             }
       
   220         }
       
   221     }
       
   222 #endif
       
   223 
       
   224     int  m_vol;
       
   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_retry(false),
       
   238     m_muted(false),
       
   239     m_playQueued(false),
       
   240     m_sampleLoaded(false),
       
   241     m_volume(100),
       
   242     m_duration(0),
       
   243     m_dataUploaded(0),
       
   244     m_loopCount(1),
       
   245     m_runningCount(0),
       
   246     m_reply(0),
       
   247     m_stream(0),
       
   248     m_networkAccessManager(0)
       
   249 {
       
   250 }
       
   251 
       
   252 QSoundEffectPrivate::~QSoundEffectPrivate()
       
   253 {
       
   254     m_reply->deleteLater();
       
   255     unloadSample();
       
   256 }
       
   257 
       
   258 QUrl QSoundEffectPrivate::source() const
       
   259 {
       
   260     return m_source;
       
   261 }
       
   262 
       
   263 void QSoundEffectPrivate::setSource(const QUrl &url)
       
   264 {
       
   265     if (url.isEmpty()) {
       
   266         m_source = QUrl();
       
   267         unloadSample();
       
   268         return;
       
   269     }
       
   270 
       
   271     m_source = url;
       
   272 
       
   273     if (m_networkAccessManager == 0)
       
   274         m_networkAccessManager = new QNetworkAccessManager(this);
       
   275 
       
   276     m_stream = m_networkAccessManager->get(QNetworkRequest(m_source));
       
   277 
       
   278     unloadSample();
       
   279     loadSample();
       
   280 }
       
   281 
       
   282 int QSoundEffectPrivate::loopCount() const
       
   283 {
       
   284     return m_loopCount;
       
   285 }
       
   286 
       
   287 void QSoundEffectPrivate::setLoopCount(int loopCount)
       
   288 {
       
   289     m_loopCount = loopCount;
       
   290 }
       
   291 
       
   292 int QSoundEffectPrivate::volume() const
       
   293 {
       
   294     return m_volume;
       
   295 }
       
   296 
       
   297 void QSoundEffectPrivate::setVolume(int volume)
       
   298 {
       
   299     m_volume = volume;
       
   300 }
       
   301 
       
   302 bool QSoundEffectPrivate::isMuted() const
       
   303 {
       
   304     return m_muted;
       
   305 }
       
   306 
       
   307 void QSoundEffectPrivate::setMuted(bool muted)
       
   308 {
       
   309     m_muted = muted;
       
   310 }
       
   311 
       
   312 void QSoundEffectPrivate::play()
       
   313 {
       
   314     if (m_retry) {
       
   315         m_retry = false;
       
   316         setSource(m_source);
       
   317         return;
       
   318     }
       
   319 
       
   320     if (!m_sampleLoaded) {
       
   321         m_playQueued = true;
       
   322         return;
       
   323     }
       
   324 
       
   325     m_runningCount += m_loopCount;
       
   326 
       
   327     playSample();
       
   328 }
       
   329 
       
   330 void QSoundEffectPrivate::decoderReady()
       
   331 {
       
   332     if (m_waveDecoder->size() >= PA_SCACHE_ENTRY_SIZE_MAX) {
       
   333         m_waveDecoder->deleteLater();
       
   334         qWarning("QSoundEffect(pulseaudio): Attempting to load to large a sample");
       
   335         return;
       
   336     }
       
   337 
       
   338     if (m_name.isNull())
       
   339         m_name = QString(QLatin1String("QtPulseSample-%1-%2")).arg(::getpid()).arg(quintptr(this)).toUtf8();
       
   340 
       
   341     pa_sample_spec spec = audioFormatToSampleSpec(m_waveDecoder->audioFormat());
       
   342 
       
   343     daemon()->lock();
       
   344     pa_stream *stream = pa_stream_new(daemon()->context(), m_name.constData(), &spec, 0);
       
   345     pa_stream_set_state_callback(stream, stream_state_callback, this);
       
   346     pa_stream_set_write_callback(stream, stream_write_callback, this);
       
   347     pa_stream_connect_upload(stream, (size_t)m_waveDecoder->size());
       
   348     m_pulseStream = stream;
       
   349     daemon()->unlock();
       
   350 }
       
   351 
       
   352 void QSoundEffectPrivate::decoderError()
       
   353 {
       
   354     qWarning("QSoundEffect(pulseaudio): Error decoding source");
       
   355 }
       
   356 
       
   357 void QSoundEffectPrivate::checkPlayTime()
       
   358 {
       
   359     int elapsed = m_playbackTime.elapsed();
       
   360 
       
   361     if (elapsed < m_duration)
       
   362         startTimer(m_duration - elapsed);
       
   363 }
       
   364 
       
   365 void QSoundEffectPrivate::loadSample()
       
   366 {
       
   367     m_sampleLoaded = false;
       
   368     m_dataUploaded = 0;
       
   369     m_waveDecoder = new WaveDecoder(m_stream);
       
   370     connect(m_waveDecoder, SIGNAL(formatKnown()), SLOT(decoderReady()));
       
   371     connect(m_waveDecoder, SIGNAL(invalidFormat()), SLOT(decoderError()));
       
   372 }
       
   373 
       
   374 void QSoundEffectPrivate::unloadSample()
       
   375 {
       
   376     if (!m_sampleLoaded)
       
   377         return;
       
   378 
       
   379     daemon()->lock();
       
   380     pa_context_remove_sample(daemon()->context(), m_name.constData(), NULL, NULL);
       
   381     daemon()->unlock();
       
   382 
       
   383     m_duration = 0;
       
   384     m_dataUploaded = 0;
       
   385     m_sampleLoaded = false;
       
   386 }
       
   387 
       
   388 void QSoundEffectPrivate::uploadSample()
       
   389 {
       
   390     daemon()->lock();
       
   391 
       
   392     size_t bufferSize = qMin(pa_stream_writable_size(m_pulseStream),
       
   393             size_t(m_waveDecoder->bytesAvailable()));
       
   394     char buffer[bufferSize];
       
   395 
       
   396     size_t len = 0;
       
   397     while (len < (m_waveDecoder->size())) {
       
   398         qint64 read = m_waveDecoder->read(buffer, qMin((int)bufferSize,
       
   399                       (int)(m_waveDecoder->size()-len)));
       
   400         if (read > 0) {
       
   401             if (pa_stream_write(m_pulseStream, buffer, size_t(read), 0, 0, PA_SEEK_RELATIVE) == 0)
       
   402                 len += size_t(read);
       
   403             else
       
   404                 break;
       
   405         }
       
   406     }
       
   407 
       
   408     m_dataUploaded += len;
       
   409     pa_stream_set_write_callback(m_pulseStream, NULL, NULL);
       
   410 
       
   411     if (m_waveDecoder->size() == m_dataUploaded) {
       
   412         int err = pa_stream_finish_upload(m_pulseStream);
       
   413         if(err != 0) {
       
   414             qWarning("pa_stream_finish_upload() err=%d",err);
       
   415             pa_stream_disconnect(m_pulseStream);
       
   416             m_retry = true;
       
   417             m_playQueued = false;
       
   418             QMetaObject::invokeMethod(this, "play");
       
   419             daemon()->unlock();
       
   420             return;
       
   421         }
       
   422         m_duration = m_waveDecoder->duration();
       
   423         m_waveDecoder->deleteLater();
       
   424         m_stream->deleteLater();
       
   425         m_sampleLoaded = true;
       
   426         if (m_playQueued) {
       
   427             m_playQueued = false;
       
   428             QMetaObject::invokeMethod(this, "play");
       
   429         }
       
   430     }
       
   431     daemon()->unlock();
       
   432 }
       
   433 
       
   434 void QSoundEffectPrivate::playSample()
       
   435 {
       
   436     pa_volume_t volume = PA_VOLUME_NORM;
       
   437 
       
   438     daemon()->lock();
       
   439 #ifdef Q_WS_MAEMO_5
       
   440     volume = PA_VOLUME_NORM / 100 * ((daemon()->volume() + m_volume) / 2);
       
   441 #endif
       
   442     pa_operation_unref(
       
   443             pa_context_play_sample(daemon()->context(),
       
   444                 m_name.constData(),
       
   445                 0,
       
   446                 volume,
       
   447                 play_callback,
       
   448                 this)
       
   449             );
       
   450     daemon()->unlock();
       
   451 
       
   452     m_playbackTime.start();
       
   453 }
       
   454 
       
   455 void QSoundEffectPrivate::timerEvent(QTimerEvent *event)
       
   456 {
       
   457     if (m_runningCount > 0)
       
   458         playSample();
       
   459 
       
   460     killTimer(event->timerId());
       
   461 }
       
   462 
       
   463 void QSoundEffectPrivate::stream_write_callback(pa_stream *s, size_t length, void *userdata)
       
   464 {
       
   465     Q_UNUSED(length);
       
   466 
       
   467     QSoundEffectPrivate *self = reinterpret_cast<QSoundEffectPrivate*>(userdata);
       
   468 
       
   469     QMetaObject::invokeMethod(self, "uploadSample", Qt::QueuedConnection);
       
   470 }
       
   471 
       
   472 void QSoundEffectPrivate::stream_state_callback(pa_stream *s, void *userdata)
       
   473 {
       
   474     switch (pa_stream_get_state(s)) {
       
   475         case PA_STREAM_CREATING:
       
   476         case PA_STREAM_READY:
       
   477         case PA_STREAM_TERMINATED:
       
   478             break;
       
   479 
       
   480         case PA_STREAM_FAILED:
       
   481         default:
       
   482             qWarning("QSoundEffect(pulseaudio): Error in pulse audio stream");
       
   483             break;
       
   484     }
       
   485 }
       
   486 
       
   487 void QSoundEffectPrivate::play_callback(pa_context *c, int success, void *userdata)
       
   488 {
       
   489     Q_UNUSED(c);
       
   490 
       
   491     QSoundEffectPrivate *self = reinterpret_cast<QSoundEffectPrivate*>(userdata);
       
   492 
       
   493     if (success == 1) {
       
   494         self->m_runningCount--;
       
   495         QMetaObject::invokeMethod(self, "checkPlayTime", Qt::QueuedConnection);
       
   496     }
       
   497 }
       
   498 
       
   499 QT_END_NAMESPACE