Qt 4.8
qsound_x11.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 "qsound.h"
43 
44 #ifndef QT_NO_SOUND
45 
46 #include "qhash.h"
47 #include "qsocketnotifier.h"
48 #include "qapplication.h"
49 #include "qsound_p.h"
50 
52 
53 #ifndef QT_NO_NAS
54 
56 #include <audio/audiolib.h>
57 #include <audio/soundlib.h>
59 
60 static AuServer *nas=0;
61 
62 static AuBool eventPred(AuServer *, AuEvent *e, AuPointer p)
63 {
64  if (e && (e->type == AuEventTypeElementNotify)) {
65  if (e->auelementnotify.flow == *((AuFlowID *)p))
66  return true;
67  }
68  return false;
69 }
70 
71 class QAuBucketNAS : public QAuBucket {
72 public:
73  QAuBucketNAS(AuBucketID b, AuFlowID f = 0) : id(b), flow(f), stopped(true), numplaying(0) { }
75  {
76  if (nas) {
77  AuSync(nas, false);
78  AuDestroyBucket(nas, id, NULL);
79 
80  AuEvent ev;
81  while (AuScanEvents(nas, AuEventsQueuedAfterFlush, true, eventPred, &flow, &ev))
82  ;
83  }
84  }
85 
86  AuBucketID id;
87  AuFlowID flow;
88  bool stopped;
90 };
91 
92 class QAuServerNAS : public QAuServer {
93  Q_OBJECT
94 
96 
97 public:
98  QAuServerNAS(QObject* parent);
99  ~QAuServerNAS();
100 
101  void init(QSound*);
102  void play(const QString& filename);
103  void play(QSound*);
104  void stop(QSound*);
105  bool okay();
106  void setDone(QSound*);
107 
108 public slots:
109  void dataReceived();
110  void soundDestroyed(QObject *o);
111 
112 private:
114  {
115  return (QAuBucketNAS*)QAuServer::bucket(s);
116  }
117 };
118 
120  QAuServer(parent)
121 {
122  setObjectName(QLatin1String("Network Audio System"));
123  nas = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL);
124  if (nas) {
125  AuSetCloseDownMode(nas, AuCloseDownDestroy, NULL);
126  // Ask Qt for async messages...
127  sn=new QSocketNotifier(AuServerConnectionNumber(nas),
129  QObject::connect(sn, SIGNAL(activated(int)),
130  this, SLOT(dataReceived()));
131  } else {
132  sn = 0;
133  }
134 }
135 
137 {
138  if (nas)
139  AuCloseServer(nas);
140  delete sn;
141  nas = 0;
142 }
143 
146 
148 {
149  if (inprogress) {
150  QSound *so = static_cast<QSound *>(o);
151  while (inprogress->remove(so))
152  ; // Loop while remove returns true
153  }
154 }
155 
156 void QAuServerNAS::play(const QString& filename)
157 {
158  if (nas) {
159  int iv=100;
160  AuFixedPoint volume=AuFixedPointFromFraction(iv,100);
161  AuSoundPlayFromFile(nas, filename.toLocal8Bit().constData(), AuNone, volume,
162  NULL, NULL, NULL, NULL, NULL, NULL);
163  AuFlush(nas);
164  dataReceived();
165  AuFlush(nas);
166  qApp->flush();
167  }
168 }
169 
170 static void callback(AuServer*, AuEventHandlerRec*, AuEvent* e, AuPointer p)
171 {
172  if (inprogress->contains(p) && e) {
173  if (e->type==AuEventTypeElementNotify &&
174  e->auelementnotify.kind==AuElementNotifyKindState) {
175  if (e->auelementnotify.cur_state == AuStateStop) {
176  AuServerHash::Iterator it = inprogress->find(p);
177  if (it != inprogress->end())
178  (*it)->setDone((QSound*)p);
179  }
180  }
181  }
182 }
183 
185 {
186  if (nas) {
187  decLoop(s);
188  if (s->loopsRemaining() && !bucket(s)->stopped) {
189  bucket(s)->stopped = true;
190  play(s);
191  } else {
192  if (--(bucket(s)->numplaying) == 0)
193  bucket(s)->stopped = true;
194  inprogress->remove(s);
195  }
196  }
197 }
198 
200 {
201  if (nas) {
202  ++(bucket(s)->numplaying);
203  if (!bucket(s)->stopped) {
204  stop(s);
205  }
206 
207  bucket(s)->stopped = false;
208  if (!inprogress)
209  inprogress = new AuServerHash;
210  inprogress->insert(s,this);
211  int iv=100;
212  AuFixedPoint volume=AuFixedPointFromFraction(iv,100);
213  QAuBucketNAS *b = bucket(s);
214  AuSoundPlayFromBucket(nas, b->id, AuNone, volume,
215  callback, s, 0, &b->flow, NULL, NULL, NULL);
216  AuFlush(nas);
217  dataReceived();
218  AuFlush(nas);
219  qApp->flush();
220  }
221 }
222 
224 {
225  if (nas && !bucket(s)->stopped) {
226  bucket(s)->stopped = true;
227  AuStopFlow(nas, bucket(s)->flow, NULL);
228  AuFlush(nas);
229  dataReceived();
230  AuFlush(nas);
231  qApp->flush();
232  }
233 }
234 
236 {
238  this, SLOT(soundDestroyed(QObject*)));
239 
240  if (nas) {
241  AuBucketID b_id =
242  AuSoundCreateBucketFromFile(nas, s->fileName().toLocal8Bit().constData(),
243  0 /*AuAccessAllMasks*/, NULL, NULL);
244  setBucket(s, new QAuBucketNAS(b_id));
245  }
246 }
247 
249 {
250  return !!nas;
251 }
252 
254 {
255  AuHandleEvents(nas);
256 }
257 
259 #include "qsound_x11.moc"
261 
262 #endif
263 
264 
265 class QAuServerNull : public QAuServer
266 {
267 public:
269 
270  void play(const QString&) { }
271  void play(QSound*s) { while(decLoop(s) > 0) /* nothing */ ; }
272  void stop(QSound*) { }
273  bool okay() { return false; }
274 };
275 
277  : QAuServer(parent)
278 {
279 }
280 
281 
283 {
284 #ifndef QT_NO_NAS
285  QAuServer* s = new QAuServerNAS(qApp);
286  if (s->okay())
287  return s;
288  else
289  delete s;
290 #endif
291  return new QAuServerNull(qApp);
292 }
293 
295 
296 #endif // QT_NO_SOUND
static void callback(AuServer *, AuEventHandlerRec *, AuEvent *e, AuPointer p)
Definition: qsound_x11.cpp:170
static AuBool eventPred(AuServer *, AuEvent *e, AuPointer p)
Definition: qsound_x11.cpp:62
QAuServer * qt_new_audio_server()
Definition: qsound_x11.cpp:282
void init(QSound *)
Initializes the sound.
Definition: qsound_x11.cpp:235
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
int remove(const Key &key)
Removes all the items that have the key from the hash.
Definition: qhash.h:784
#define it(className, varName)
void play(const QString &filename)
Definition: qsound_x11.cpp:156
#define SLOT(a)
Definition: qobjectdefs.h:226
#define QT_END_INCLUDE_NAMESPACE
This macro is equivalent to QT_BEGIN_NAMESPACE.
Definition: qglobal.h:92
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
int decLoop(QSound *)
Decrements the QSound::loopRemaining() value for sound s, returning the result.
Definition: qsound.cpp:374
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void stop(QSound *)
Definition: qsound_x11.cpp:223
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
int loopsRemaining() const
Returns the remaining number of times the sound will loop (this value decreases each time the sound i...
Definition: qsound.cpp:281
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
The QSocketNotifier class provides support for monitoring activity on a file descriptor.
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
QAuBucketNAS * bucket(QSound *s)
Definition: qsound_x11.cpp:113
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void destroyed(QObject *=0)
This signal is emitted immediately before the object obj is destroyed, and can not be blocked...
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
void setBucket(QSound *, QAuBucket *)
Sets the internal bucket record of sound s to b, deleting any previous setting.
Definition: qsound.cpp:356
#define qApp
static bool init
QAuBucketNAS(AuBucketID b, AuFlowID f=0)
Definition: qsound_x11.cpp:73
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
#define Q_OBJECT
Definition: qobjectdefs.h:157
QAuServerNAS(QObject *parent)
Definition: qsound_x11.cpp:119
void play(QSound *s)
Definition: qsound_x11.cpp:271
static AuServerHash * inprogress
Definition: qsound_x11.cpp:145
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QAuServerNull(QObject *parent)
Definition: qsound_x11.cpp:276
QSocketNotifier * sn
Definition: qsound_x11.cpp:95
QHash< void *, QAuServerNAS * > AuServerHash
Definition: qsound_x11.cpp:144
virtual bool okay()=0
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
static AuServer * nas
Definition: qsound_x11.cpp:60
void soundDestroyed(QObject *o)
Definition: qsound_x11.cpp:147
#define QT_BEGIN_INCLUDE_NAMESPACE
This macro is equivalent to QT_END_NAMESPACE.
Definition: qglobal.h:91
QString fileName() const
Returns the filename associated with this QSound object.
Definition: qsound.cpp:312
iterator find(const Key &key)
Returns an iterator pointing to the item with the key in the hash.
Definition: qhash.h:865
#define slots
Definition: qobjectdefs.h:68
void play(const QString &)
Definition: qsound_x11.cpp:270
The QSound class provides access to the platform audio facilities.
Definition: qsound.h:57
void setDone(QSound *)
Definition: qsound_x11.cpp:184
AuBucketID id
Definition: qsound_x11.cpp:86
void dataReceived()
Definition: qsound_x11.cpp:253
void stop(QSound *)
Definition: qsound_x11.cpp:272
AuFlowID flow
Definition: qsound_x11.cpp:87