src/gui/kernel/qsound_qws.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 QtGui module of the Qt Toolkit.
       
     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 "qapplication.h"
       
    43 
       
    44 #ifndef QT_NO_SOUND
       
    45 
       
    46 #include "qsound.h"
       
    47 #include "qpaintdevice.h"
       
    48 #include "qwsdisplay_qws.h"
       
    49 #include "qsound_p.h"
       
    50 
       
    51 #include "qsoundqss_qws.h"
       
    52 
       
    53 #include "qhash.h"
       
    54 #include "qfileinfo.h"
       
    55 
       
    56 #include "qbytearray.h"
       
    57 #include "quuid.h"
       
    58 #include "qdatastream.h"
       
    59 #include "qcopchannel_qws.h"
       
    60 #include "qbuffer.h"
       
    61 
       
    62 
       
    63 QT_BEGIN_NAMESPACE
       
    64 
       
    65 #ifdef MEDIA_SERVER
       
    66 
       
    67 #define SERVER_CHANNEL "QPE/MediaServer"
       
    68 
       
    69 class QCopMessage : public QDataStream
       
    70 {
       
    71 public:
       
    72     QCopMessage( const QString& channel, const QString& message )
       
    73         : QDataStream( new QBuffer ), m_channel( channel ), m_message( message )
       
    74     {
       
    75         device()->open( QIODevice::WriteOnly );
       
    76     }
       
    77 
       
    78     ~QCopMessage()
       
    79     {
       
    80         QCopChannel::send( m_channel, m_message, ((QBuffer*)device())->buffer() );
       
    81         delete device();
       
    82     }
       
    83 
       
    84 private:
       
    85     QString m_channel;
       
    86     QString m_message;
       
    87 };
       
    88 
       
    89 #endif // MEDIA_SERVER
       
    90 
       
    91 class QAuServerQWS;
       
    92 
       
    93 class QAuBucketQWS : public QObject, public QAuBucket
       
    94 {
       
    95     Q_OBJECT
       
    96 public:
       
    97     QAuBucketQWS( QAuServerQWS*, QSound*, QObject* parent = 0 );
       
    98 
       
    99     ~QAuBucketQWS();
       
   100 
       
   101 #ifndef MEDIA_SERVER
       
   102     int id() const { return id_; }
       
   103 #endif
       
   104 
       
   105     QSound* sound() const { return sound_; }
       
   106 
       
   107 #ifdef MEDIA_SERVER
       
   108     void play();
       
   109 
       
   110     void stop();
       
   111 #endif
       
   112 
       
   113 signals:
       
   114     // Only for Media Server
       
   115     void done( QAuBucketQWS* );
       
   116 
       
   117 private slots:
       
   118     // Only for Media Server
       
   119     void processMessage( const QString& msg, const QByteArray& data );
       
   120 
       
   121 private:
       
   122 #ifdef MEDIA_SERVER
       
   123     QCopChannel *m_channel;
       
   124     QUuid m_id;
       
   125 #endif
       
   126 
       
   127 #ifndef MEDIA_SERVER
       
   128     int id_;
       
   129 #endif
       
   130     QSound *sound_;
       
   131     QAuServerQWS *server_;
       
   132 
       
   133     static int next;
       
   134 };
       
   135 
       
   136 int QAuBucketQWS::next = 0;
       
   137 
       
   138 class QAuServerQWS : public QAuServer
       
   139 {
       
   140     Q_OBJECT
       
   141 public:
       
   142     QAuServerQWS( QObject* parent );
       
   143 
       
   144     void init( QSound* s )
       
   145     {
       
   146         QAuBucketQWS *bucket = new QAuBucketQWS( this, s );
       
   147 #ifdef MEDIA_SERVER
       
   148         connect( bucket, SIGNAL(done(QAuBucketQWS*)),
       
   149             this, SLOT(complete(QAuBucketQWS*)) );
       
   150 #endif
       
   151         setBucket( s, bucket );
       
   152     }
       
   153 
       
   154 #ifndef MEDIA_SERVER
       
   155     // Register bucket
       
   156     void insert( QAuBucketQWS *bucket )
       
   157     {
       
   158         buckets.insert( bucket->id(), bucket );
       
   159     }
       
   160 
       
   161     // Remove bucket from register
       
   162     void remove( QAuBucketQWS *bucket )
       
   163     {
       
   164         buckets.remove( bucket->id() );
       
   165     }
       
   166 #endif
       
   167 
       
   168     void play( QSound* s )
       
   169     {
       
   170         QString filepath = QFileInfo( s->fileName() ).absoluteFilePath();
       
   171 #if defined(QT_NO_QWS_SOUNDSERVER)
       
   172         server->playFile( bucket( s )->id(), filepath );
       
   173 #elif defined(MEDIA_SERVER)
       
   174         bucket( s )->play();
       
   175 #else
       
   176         client->play( bucket( s )->id(), filepath );
       
   177 #endif
       
   178     }
       
   179 
       
   180     void stop( QSound* s )
       
   181     {
       
   182 #if defined(QT_NO_QWS_SOUNDSERVER)
       
   183         server->stopFile( bucket( s )->id() );
       
   184 #elif defined(MEDIA_SERVER)
       
   185         bucket( s )->stop();
       
   186 #else
       
   187         client->stop( bucket( s )->id() );
       
   188 #endif
       
   189     }
       
   190 
       
   191     bool okay() { return true; }
       
   192 
       
   193 private slots:
       
   194     // Continue playing sound if loops remain
       
   195     void complete( int id )
       
   196     {
       
   197 #ifndef MEDIA_SERVER
       
   198         QAuBucketQWS *bucket = find( id );
       
   199         if( bucket ) {
       
   200             QSound *sound = bucket->sound();
       
   201             if( decLoop( sound ) ) {
       
   202                 play( sound );
       
   203             }
       
   204         }
       
   205 #else
       
   206         Q_UNUSED(id);
       
   207 #endif
       
   208     }
       
   209 
       
   210     // Only for Media Server
       
   211     void complete( QAuBucketQWS* bucket )
       
   212     {
       
   213 #ifndef MEDIA_SERVER
       
   214         Q_UNUSED(bucket);
       
   215 #else
       
   216         QSound *sound = bucket->sound();
       
   217         if( decLoop( sound ) ) {
       
   218             play( sound );
       
   219         }
       
   220 #endif
       
   221     }
       
   222 
       
   223 protected:
       
   224     QAuBucketQWS* bucket( QSound *s )
       
   225     {
       
   226         return (QAuBucketQWS*)QAuServer::bucket( s );
       
   227     }
       
   228 
       
   229 private:
       
   230 #ifndef MEDIA_SERVER
       
   231     // Find registered bucket with given id, return null if none found
       
   232     QAuBucketQWS* find( int id )
       
   233     {
       
   234         QHash<int, QAuBucketQWS*>::Iterator it = buckets.find( id );
       
   235         if( it != buckets.end() ) {
       
   236             return it.value();
       
   237         }
       
   238 
       
   239         return 0;
       
   240     }
       
   241 
       
   242     QHash<int, QAuBucketQWS*> buckets; // ### possible problem with overlapping keys
       
   243 
       
   244 #ifdef QT_NO_QWS_SOUNDSERVER
       
   245     QWSSoundServer *server;
       
   246 #else
       
   247     QWSSoundClient *client;
       
   248 #endif
       
   249 
       
   250 #endif // MEDIA_SERVER
       
   251 };
       
   252 
       
   253 QAuServerQWS::QAuServerQWS(QObject* parent) :
       
   254     QAuServer(parent)
       
   255 {
       
   256 #ifndef MEDIA_SERVER
       
   257     setObjectName(QLatin1String("qauserverqws"));
       
   258 
       
   259 #ifdef QT_NO_QWS_SOUNDSERVER
       
   260     server = new QWSSoundServer( this ); // ### only suitable for single application
       
   261 
       
   262     connect( server, SIGNAL(soundCompleted(int)),
       
   263         this, SLOT(complete(int)) );
       
   264 #else
       
   265     client = new QWSSoundClient( this ); // ### requires successful connection
       
   266 
       
   267     connect( client, SIGNAL(soundCompleted(int)),
       
   268         this, SLOT(complete(int)) );
       
   269 #endif
       
   270 
       
   271 #endif // MEDIA_SERVER
       
   272 }
       
   273 
       
   274 QAuBucketQWS::QAuBucketQWS( QAuServerQWS *server, QSound *sound, QObject* parent )
       
   275     : QObject( parent ), sound_( sound ), server_( server )
       
   276 {
       
   277 #ifdef MEDIA_SERVER
       
   278     m_id = QUuid::createUuid();
       
   279 
       
   280     sound->setObjectName( m_id.toString() );
       
   281 
       
   282     m_channel = new QCopChannel(QLatin1String("QPE/QSound/") + m_id, this );
       
   283     connect( m_channel, SIGNAL(received(QString,QByteArray)),
       
   284         this, SLOT(processMessage(QString,QByteArray)) );
       
   285 
       
   286     {
       
   287         QCopMessage message( QLatin1String(SERVER_CHANNEL), QLatin1String("subscribe(QUuid)") );
       
   288         message << m_id;
       
   289     }
       
   290 
       
   291     {
       
   292         QString filepath = QFileInfo( sound_->fileName() ).absoluteFilePath();
       
   293         QCopMessage message( QLatin1String(SERVER_CHANNEL), QLatin1String("open(QUuid,QString)") );
       
   294         message << m_id << filepath;
       
   295     }
       
   296 #else
       
   297     id_ = next++;
       
   298     server_->insert( this );
       
   299 #endif
       
   300 }
       
   301 
       
   302 #ifdef MEDIA_SERVER
       
   303 void QAuBucketQWS::play()
       
   304 {
       
   305     QString filepath = QFileInfo( sound_->fileName() ).absoluteFilePath();
       
   306 
       
   307     QCopMessage message( QLatin1String(SERVER_CHANNEL), QLatin1String("play(QUuid)") );
       
   308     message << m_id;
       
   309 }
       
   310 
       
   311 void QAuBucketQWS::stop()
       
   312 {
       
   313     QCopMessage message( QLatin1String(SERVER_CHANNEL), QLatin1String("stop(QUuid)") );
       
   314     message << m_id;
       
   315 }
       
   316 #endif // MEDIA_SERVER
       
   317 
       
   318 void QAuBucketQWS::processMessage( const QString& msg, const QByteArray& data )
       
   319 {
       
   320     Q_UNUSED(data);
       
   321 #ifndef MEDIA_SERVER
       
   322     Q_UNUSED(msg);
       
   323 #else
       
   324     if( msg == QLatin1String("done()") ) {
       
   325         emit done( this );
       
   326     }
       
   327 #endif
       
   328 }
       
   329 
       
   330 QAuBucketQWS::~QAuBucketQWS()
       
   331 {
       
   332 #ifdef MEDIA_SERVER
       
   333     QCopMessage message( QLatin1String(SERVER_CHANNEL), QLatin1String("revoke(QUuid)") );
       
   334     message << m_id;
       
   335 #else
       
   336     server_->remove( this );
       
   337 #endif
       
   338 }
       
   339 
       
   340 
       
   341 QAuServer* qt_new_audio_server()
       
   342 {
       
   343     return new QAuServerQWS(qApp);
       
   344 }
       
   345 
       
   346 #include "qsound_qws.moc"
       
   347 
       
   348 #endif // QT_NO_SOUND
       
   349 
       
   350 QT_END_NAMESPACE