Qt 4.8
qsound_qws.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
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  {
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
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:
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 
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 
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)),
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 
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 
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 
342 {
343  return new QAuServerQWS(qApp);
344 }
345 
346 #include "qsound_qws.moc"
347 
348 #endif // QT_NO_SOUND
349 
void insert(QAuBucketQWS *bucket)
Definition: qsound_qws.cpp:156
QAuServerQWS(QObject *parent)
Definition: qsound_qws.cpp:253
QAuBucket * bucket(QSound *)
Returns the internal bucket record of sound s.
Definition: qsound.cpp:365
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define it(className, varName)
void init(QSound *s)
Initializes the sound.
Definition: qsound_qws.cpp:144
static QUuid createUuid()
On any platform other than Windows, this function returns a new UUID with variant QUuid::DCE and vers...
Definition: quuid.cpp:897
static int next
Definition: qsound_qws.cpp:133
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define SLOT(a)
Definition: qobjectdefs.h:226
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static bool send(const QString &channel, const QString &msg)
This is an overloaded member function, provided for convenience. It differs from the above function o...
QAuServerQWS * server_
Definition: qsound_qws.cpp:131
T & value() const
Returns a modifiable reference to the current item&#39;s value.
Definition: qhash.h:348
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual void play(const QString &filename)
Definition: qsound.cpp:71
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
QHash< int, QAuBucketQWS * > buckets
Definition: qsound_qws.cpp:242
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void complete(int id)
Definition: qsound_qws.cpp:195
QAuBucketQWS * find(int id)
Definition: qsound_qws.cpp:232
void stop(QSound *s)
Definition: qsound_qws.cpp:180
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
void complete(QAuBucketQWS *bucket)
Definition: qsound_qws.cpp:211
#define SIGNAL(a)
Definition: qobjectdefs.h:227
int id() const
Definition: qsound_qws.cpp:102
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QAuBucketQWS * bucket(QSound *s)
Definition: qsound_qws.cpp:224
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
#define qApp
#define emit
Definition: qobjectdefs.h:76
void processMessage(const QString &msg, const QByteArray &data)
Definition: qsound_qws.cpp:318
static const char * data(const QByteArray &arr)
#define Q_OBJECT
Definition: qobjectdefs.h:157
static QAuServer & server()
Definition: qsound.cpp:79
void done(QAuBucketQWS *)
QIODevice * device() const
Returns the I/O device currently set, or 0 if no device is currently set.
Definition: qdatastream.h:206
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QSound * sound() const
Definition: qsound_qws.cpp:105
QWSSoundClient * client
Definition: qsound_qws.cpp:247
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
virtual void stop(QSound *)=0
QAuBucketQWS(QAuServerQWS *, QSound *, QObject *parent=0)
Definition: qsound_qws.cpp:274
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570
The QCopChannel class provides communication capabilities between clients in Qt for Embedded Linux...
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
QString fileName() const
Returns the filename associated with this QSound object.
Definition: qsound.cpp:312
QAuServer * qt_new_audio_server()
Definition: qsound_qws.cpp:341
void play(QSound *s)
Definition: qsound_qws.cpp:168
void remove(QAuBucketQWS *bucket)
Definition: qsound_qws.cpp:162
#define slots
Definition: qobjectdefs.h:68
The QSound class provides access to the platform audio facilities.
Definition: qsound.h:57
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
#define signals
Definition: qobjectdefs.h:69
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
The QUuid class stores a Universally Unique Identifier (UUID).
Definition: quuid.h:67
QSound * sound_
Definition: qsound_qws.cpp:130