1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/*
* Copyright (C) 2003-2010 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "output_api.h"
#include "pcm_buffer.h"
#include "mixer_list.h"
#include "winmm_output_plugin.h"
#include <windows.h>
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "winmm_output"
struct winmm_buffer {
struct pcm_buffer buffer;
WAVEHDR hdr;
};
struct winmm_output {
HWAVEOUT handle;
/**
* This event is triggered by Windows when a buffer is
* finished.
*/
HANDLE event;
struct winmm_buffer buffers[8];
unsigned next_buffer;
};
/**
* The quark used for GError.domain.
*/
static inline GQuark
winmm_output_quark(void)
{
return g_quark_from_static_string("winmm_output");
}
HWAVEOUT
winmm_output_get_handle(struct winmm_output* output)
{
return output->handle;
}
static bool
winmm_output_test_default_device(void)
{
/* we assume that Wave is always available */
return true;
}
static void *
winmm_output_init(G_GNUC_UNUSED const struct audio_format *audio_format,
G_GNUC_UNUSED const struct config_param *param,
G_GNUC_UNUSED GError **error)
{
struct winmm_output *wo = g_new(struct winmm_output, 1);
return wo;
}
static void
winmm_output_finish(void *data)
{
struct winmm_output *wo = data;
g_free(wo);
}
static bool
winmm_output_open(void *data, struct audio_format *audio_format,
GError **error_r)
{
struct winmm_output *wo = data;
wo->event = CreateEvent(NULL, false, false, NULL);
if (wo->event == NULL) {
g_set_error(error_r, winmm_output_quark(), 0,
"CreateEvent() failed");
return false;
}
switch (audio_format->format) {
case SAMPLE_FORMAT_S8:
case SAMPLE_FORMAT_S16:
break;
case SAMPLE_FORMAT_S24:
case SAMPLE_FORMAT_S24_P32:
case SAMPLE_FORMAT_S32:
case SAMPLE_FORMAT_UNDEFINED:
/* we havn't tested formats other than S16 */
audio_format->format = SAMPLE_FORMAT_S16;
break;
}
if (audio_format->channels > 2)
/* same here: more than stereo was not tested */
audio_format->channels = 2;
WAVEFORMATEX format;
format.wFormatTag = WAVE_FORMAT_PCM;
format.nChannels = audio_format->channels;
format.nSamplesPerSec = audio_format->sample_rate;
format.nBlockAlign = audio_format_frame_size(audio_format);
format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
format.wBitsPerSample = audio_format_sample_size(audio_format) * 8;
format.cbSize = 0;
MMRESULT result = waveOutOpen(&wo->handle, WAVE_MAPPER, &format,
(DWORD_PTR)wo->event, 0, CALLBACK_EVENT);
if (result != MMSYSERR_NOERROR) {
CloseHandle(wo->event);
g_set_error(error_r, winmm_output_quark(), result,
"waveOutOpen() failed");
return false;
}
for (unsigned i = 0; i < G_N_ELEMENTS(wo->buffers); ++i) {
pcm_buffer_init(&wo->buffers[i].buffer);
memset(&wo->buffers[i].hdr, 0, sizeof(wo->buffers[i].hdr));
}
wo->next_buffer = 0;
return true;
}
static void
winmm_output_close(void *data)
{
struct winmm_output *wo = data;
for (unsigned i = 0; i < G_N_ELEMENTS(wo->buffers); ++i)
pcm_buffer_deinit(&wo->buffers[i].buffer);
waveOutClose(wo->handle);
CloseHandle(wo->event);
}
/**
* Copy data into a buffer, and prepare the wave header.
*/
static bool
winmm_set_buffer(struct winmm_output *wo, struct winmm_buffer *buffer,
const void *data, size_t size,
GError **error_r)
{
void *dest = pcm_buffer_get(&buffer->buffer, size);
if (dest == NULL) {
g_set_error(error_r, winmm_output_quark(), 0,
"Out of memory");
return false;
}
memcpy(dest, data, size);
memset(&buffer->hdr, 0, sizeof(buffer->hdr));
buffer->hdr.lpData = dest;
buffer->hdr.dwBufferLength = size;
MMRESULT result = waveOutPrepareHeader(wo->handle, &buffer->hdr,
sizeof(buffer->hdr));
if (result != MMSYSERR_NOERROR) {
g_set_error(error_r, winmm_output_quark(), result,
"waveOutPrepareHeader() failed");
return false;
}
return true;
}
/**
* Wait until the buffer is finished.
*/
static bool
winmm_drain_buffer(struct winmm_output *wo, struct winmm_buffer *buffer,
GError **error_r)
{
if ((buffer->hdr.dwFlags & WHDR_DONE) == WHDR_DONE)
/* already finished */
return true;
while (true) {
MMRESULT result = waveOutUnprepareHeader(wo->handle,
&buffer->hdr,
sizeof(buffer->hdr));
if (result == MMSYSERR_NOERROR)
return true;
else if (result != WAVERR_STILLPLAYING) {
g_set_error(error_r, winmm_output_quark(), result,
"waveOutUnprepareHeader() failed");
return false;
}
/* wait some more */
WaitForSingleObject(wo->event, INFINITE);
}
}
static size_t
winmm_output_play(void *data, const void *chunk, size_t size, GError **error_r)
{
struct winmm_output *wo = data;
/* get the next buffer from the ring and prepare it */
struct winmm_buffer *buffer = &wo->buffers[wo->next_buffer];
if (!winmm_drain_buffer(wo, buffer, error_r) ||
!winmm_set_buffer(wo, buffer, chunk, size, error_r))
return 0;
/* enqueue the buffer */
MMRESULT result = waveOutWrite(wo->handle, &buffer->hdr,
sizeof(buffer->hdr));
if (result != MMSYSERR_NOERROR) {
waveOutUnprepareHeader(wo->handle, &buffer->hdr,
sizeof(buffer->hdr));
g_set_error(error_r, winmm_output_quark(), result,
"waveOutWrite() failed");
return 0;
}
/* mark our buffer as "used" */
wo->next_buffer = (wo->next_buffer + 1) %
G_N_ELEMENTS(wo->buffers);
return size;
}
static bool
winmm_drain_all_buffers(struct winmm_output *wo, GError **error_r)
{
for (unsigned i = wo->next_buffer; i < G_N_ELEMENTS(wo->buffers); ++i)
if (!winmm_drain_buffer(wo, &wo->buffers[i], error_r))
return false;
for (unsigned i = 0; i < wo->next_buffer; ++i)
if (!winmm_drain_buffer(wo, &wo->buffers[i], error_r))
return false;
return true;
}
static void
winmm_stop(struct winmm_output *wo)
{
waveOutReset(wo->handle);
for (unsigned i = 0; i < G_N_ELEMENTS(wo->buffers); ++i) {
struct winmm_buffer *buffer = &wo->buffers[i];
waveOutUnprepareHeader(wo->handle, &buffer->hdr,
sizeof(buffer->hdr));
}
}
static void
winmm_output_drain(void *data)
{
struct winmm_output *wo = data;
if (!winmm_drain_all_buffers(wo, NULL))
winmm_stop(wo);
}
static void
winmm_output_cancel(void *data)
{
struct winmm_output *wo = data;
winmm_stop(wo);
}
const struct audio_output_plugin winmm_output_plugin = {
.name = "winmm",
.test_default_device = winmm_output_test_default_device,
.init = winmm_output_init,
.finish = winmm_output_finish,
.open = winmm_output_open,
.close = winmm_output_close,
.play = winmm_output_play,
.drain = winmm_output_drain,
.cancel = winmm_output_cancel,
.mixer_plugin = &winmm_mixer_plugin,
};
|