1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qsound.h"
43 
44 #ifndef QT_NO_SOUND
45 
46 #include "qapplication.h"
47 #include "qapplication_p.h"
48 #include <qfile.h>
49 #include "qpointer.h"
50 #include "qsound_p.h"
51 
52 #include <qt_windows.h>
53 
54 QT_BEGIN_NAMESPACE
55 
56 class QAuServerWindows : public QAuServer {
57     Q_OBJECT
58 
59 public:
60     QAuServerWindows(QObject* parent);
61     ~QAuServerWindows();
62 
63     void playHelper(const QString &filename, int loop, QSound *snd);
64     void play(const QString& filename, int loop);
65     void play(QSound*);
66 
67     void stop(QSound*);
68     bool okay();
69 
decLoop(QSound * snd)70     int decLoop(QSound *snd) { return QAuServer::decLoop(snd); }
71 
72     HANDLE current;
73     HANDLE mutex;
74     HANDLE event;
75 };
76 
QAuServerWindows(QObject * parent)77 QAuServerWindows::QAuServerWindows(QObject* parent) :
78     QAuServer(parent), current(0)
79 {
80     mutex = CreateMutex(0, 0, 0);
81     event = CreateEvent(0, FALSE, FALSE, 0);
82 }
83 
~QAuServerWindows()84 QAuServerWindows::~QAuServerWindows()
85 {
86     HANDLE mtx = mutex;
87     WaitForSingleObject(mtx, INFINITE);
88     mutex = 0;
89 
90     ReleaseMutex(mtx);
91     CloseHandle(mtx);
92     CloseHandle(event);
93 }
94 
95 struct SoundInfo
96 {
SoundInfoSoundInfo97     SoundInfo(const QString &fn, int lp, QSound *snd, QAuServerWindows *srv)
98         : sound(snd), server(srv), filename(fn), loops(lp)
99     {
100     }
101 
102     QSound *sound;
103     QAuServerWindows *server;
104     QString filename;
105     int loops;
106 };
107 
SoundPlayProc(LPVOID param)108 DWORD WINAPI SoundPlayProc(LPVOID param)
109 {
110     SoundInfo *info = (SoundInfo*)param;
111 
112     // copy data before waking up GUI thread
113     QAuServerWindows *server = info->server;
114     QSound *sound = info->sound;
115     int loops = info->loops;
116     QString filename = info->filename;
117     HANDLE mutex = server->mutex;
118     HANDLE event = server->event;
119     info = 0;
120 
121     // server must not be destroyed until thread finishes
122     // and all other sounds have to wait
123     WaitForSingleObject(mutex, INFINITE);
124 
125     if (loops <= 1) {
126         server->current = 0;
127         int flags = SND_FILENAME|SND_ASYNC;
128         if (loops == -1)
129             flags |= SND_LOOP;
130 
131         PlaySound((wchar_t*)filename.utf16(), 0, flags);
132         if (sound && loops == 1)
133             server->decLoop(sound);
134 
135         // GUI thread continues, but we are done as well.
136         SetEvent(event);
137     } else {
138         // signal GUI thread to continue - sound might be reset!
139         QPointer<QSound> guarded_sound = sound;
140         SetEvent(event);
141 
142         for (int l = 0; l < loops && server->current; ++l) {
143             PlaySound((wchar_t*)filename.utf16(), 0, SND_FILENAME | SND_SYNC);
144 
145             if (guarded_sound)
146                 server->decLoop(guarded_sound);
147         }
148         server->current = 0;
149     }
150     ReleaseMutex(mutex);
151 
152     return 0;
153 }
154 
playHelper(const QString & filename,int loop,QSound * snd)155 void QAuServerWindows::playHelper(const QString &filename, int loop, QSound *snd)
156 {
157     if (loop == 0)
158         return;
159     // busy?
160     if (WaitForSingleObject(mutex, 0) == WAIT_TIMEOUT)
161         return;
162     ReleaseMutex(mutex);
163 
164     DWORD threadid = 0;
165     SoundInfo info(filename, loop, snd, this);
166     current = CreateThread(0, 0, SoundPlayProc, &info, 0, &threadid);
167     CloseHandle(current);
168 
169     WaitForSingleObject(event, INFINITE);
170 }
171 
play(const QString & filename,int loop)172 void QAuServerWindows::play(const QString& filename, int loop)
173 {
174     playHelper(filename, loop, 0);
175 }
176 
play(QSound * s)177 void QAuServerWindows::play(QSound* s)
178 {
179     playHelper(s->fileName(), s->loops(), s);
180 }
181 
stop(QSound *)182 void QAuServerWindows::stop(QSound*)
183 {
184     // stop unlooped sound
185     if (!current)
186         PlaySound(0, 0, 0);
187     // stop after loop is done
188     current = 0;
189 }
190 
okay()191 bool QAuServerWindows::okay()
192 {
193     return true;
194 }
195 
qt_new_audio_server()196 QAuServer* qt_new_audio_server()
197 {
198     return new QAuServerWindows(qApp);
199 }
200 
201 QT_END_NAMESPACE
202 
203 #include "qsound_win.moc"
204 
205 #endif // QT_NO_SOUND
206