4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/mainloop-api.h>
32 #include <pulse/xmalloc.h>
33 #include <pulse/timeval.h>
35 #include <pulsecore/sink.h>
36 #include <pulsecore/source.h>
37 #include <pulsecore/module.h>
38 #include <pulsecore/modargs.h>
39 #include <pulsecore/sample-util.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/log.h>
43 #include "module-waveout-symdef.h"
45 PA_MODULE_AUTHOR("Pierre Ossman")
46 PA_MODULE_DESCRIPTION("Windows waveOut Sink/Source")
47 PA_MODULE_VERSION(PACKAGE_VERSION)
49 "sink_name=<name for the sink> "
50 "source_name=<name for the source>"
51 "record=<enable source?> "
52 "playback=<enable sink?> "
53 "format=<sample format> "
54 "channels=<number of channels> "
56 "fragments=<number of fragments> "
57 "fragment_size=<fragment size> "
58 "channel_map=<channel map>")
60 #define DEFAULT_SINK_NAME "wave_output"
61 #define DEFAULT_SOURCE_NAME "wave_input"
63 #define WAVEOUT_MAX_VOLUME 0xFFFF
70 pa_defer_event *defer;
71 pa_usec_t poll_timeout;
73 uint32_t fragments, fragment_size;
75 uint32_t free_ofrags, free_ifrags;
80 int cur_ohdr, cur_ihdr;
81 WAVEHDR *ohdrs, *ihdrs;
87 CRITICAL_SECTION crit;
90 static const char* const valid_modargs[] = {
104 static void update_usage(struct userdata *u) {
105 pa_module_set_used(u->module,
106 (u->sink ? pa_sink_used_by(u->sink) : 0) +
107 (u->source ? pa_source_used_by(u->source) : 0));
110 static void do_write(struct userdata *u)
113 pa_memchunk memchunk;
120 EnterCriticalSection(&u->crit);
121 free_frags = u->free_ofrags;
122 LeaveCriticalSection(&u->crit);
124 if (!u->sink_underflow && (free_frags == u->fragments))
125 pa_log_debug("WaveOut underflow!");
128 hdr = &u->ohdrs[u->cur_ohdr];
129 if (hdr->dwFlags & WHDR_PREPARED)
130 waveOutUnprepareHeader(u->hwo, hdr, sizeof(WAVEHDR));
132 hdr->dwBufferLength = 0;
133 while (hdr->dwBufferLength < u->fragment_size) {
136 len = u->fragment_size - hdr->dwBufferLength;
138 if (pa_sink_render(u->sink, len, &memchunk) < 0)
141 assert(memchunk.memblock);
142 assert(memchunk.memblock->data);
143 assert(memchunk.length);
145 if (memchunk.length < len)
146 len = memchunk.length;
148 memcpy(hdr->lpData + hdr->dwBufferLength,
149 (char*)memchunk.memblock->data + memchunk.index, len);
151 hdr->dwBufferLength += len;
153 pa_memblock_unref(memchunk.memblock);
154 memchunk.memblock = NULL;
157 /* Insufficient data in sink buffer? */
158 if (hdr->dwBufferLength == 0) {
159 u->sink_underflow = 1;
163 u->sink_underflow = 0;
165 res = waveOutPrepareHeader(u->hwo, hdr, sizeof(WAVEHDR));
166 if (res != MMSYSERR_NOERROR) {
167 pa_log_error(__FILE__ ": ERROR: Unable to prepare waveOut block: %d",
170 res = waveOutWrite(u->hwo, hdr, sizeof(WAVEHDR));
171 if (res != MMSYSERR_NOERROR) {
172 pa_log_error(__FILE__ ": ERROR: Unable to write waveOut block: %d",
176 u->written_bytes += hdr->dwBufferLength;
178 EnterCriticalSection(&u->crit);
180 LeaveCriticalSection(&u->crit);
184 u->cur_ohdr %= u->fragments;
188 static void do_read(struct userdata *u)
191 pa_memchunk memchunk;
198 EnterCriticalSection(&u->crit);
200 free_frags = u->free_ifrags;
203 LeaveCriticalSection(&u->crit);
205 if (free_frags == u->fragments)
206 pa_log_debug("WaveIn overflow!");
209 hdr = &u->ihdrs[u->cur_ihdr];
210 if (hdr->dwFlags & WHDR_PREPARED)
211 waveInUnprepareHeader(u->hwi, hdr, sizeof(WAVEHDR));
213 if (hdr->dwBytesRecorded) {
214 memchunk.memblock = pa_memblock_new(u->core->mempool, hdr->dwBytesRecorded);
215 assert(memchunk.memblock);
217 memcpy((char*)memchunk.memblock->data, hdr->lpData, hdr->dwBytesRecorded);
219 memchunk.length = memchunk.memblock->length = hdr->dwBytesRecorded;
222 pa_source_post(u->source, &memchunk);
223 pa_memblock_unref(memchunk.memblock);
226 res = waveInPrepareHeader(u->hwi, hdr, sizeof(WAVEHDR));
227 if (res != MMSYSERR_NOERROR) {
228 pa_log_error(__FILE__ ": ERROR: Unable to prepare waveIn block: %d",
231 res = waveInAddBuffer(u->hwi, hdr, sizeof(WAVEHDR));
232 if (res != MMSYSERR_NOERROR) {
233 pa_log_error(__FILE__ ": ERROR: Unable to add waveIn block: %d",
239 u->cur_ihdr %= u->fragments;
243 static void poll_cb(pa_mainloop_api*a, pa_time_event *e, const struct timeval *tv, void *userdata) {
244 struct userdata *u = userdata;
254 pa_gettimeofday(&ntv);
255 pa_timeval_add(&ntv, u->poll_timeout);
257 a->time_restart(e, &ntv);
260 static void defer_cb(pa_mainloop_api*a, pa_defer_event *e, void *userdata) {
261 struct userdata *u = userdata;
265 a->defer_enable(e, 0);
271 static void CALLBACK chunk_done_cb(HWAVEOUT hwo, UINT msg, DWORD_PTR inst, DWORD param1, DWORD param2) {
272 struct userdata *u = (struct userdata *)inst;
277 EnterCriticalSection(&u->crit);
280 assert(u->free_ofrags <= u->fragments);
282 LeaveCriticalSection(&u->crit);
285 static void CALLBACK chunk_ready_cb(HWAVEIN hwi, UINT msg, DWORD_PTR inst, DWORD param1, DWORD param2) {
286 struct userdata *u = (struct userdata *)inst;
291 EnterCriticalSection(&u->crit);
294 assert(u->free_ifrags <= u->fragments);
296 LeaveCriticalSection(&u->crit);
299 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
300 struct userdata *u = s->userdata;
303 assert(s && u && u->sink);
305 memset(&mmt, 0, sizeof(mmt));
306 mmt.wType = TIME_BYTES;
307 if (waveOutGetPosition(u->hwo, &mmt, sizeof(mmt)) == MMSYSERR_NOERROR)
308 return pa_bytes_to_usec(u->written_bytes - mmt.u.cb, &s->sample_spec);
310 EnterCriticalSection(&u->crit);
312 free_frags = u->free_ofrags;
314 LeaveCriticalSection(&u->crit);
316 return pa_bytes_to_usec((u->fragments - free_frags) * u->fragment_size,
321 static pa_usec_t source_get_latency_cb(pa_source *s) {
323 struct userdata *u = s->userdata;
325 assert(s && u && u->sink);
327 EnterCriticalSection(&u->crit);
329 free_frags = u->free_ifrags;
331 LeaveCriticalSection(&u->crit);
333 r += pa_bytes_to_usec((free_frags + 1) * u->fragment_size, &s->sample_spec);
338 static void notify_sink_cb(pa_sink *s) {
339 struct userdata *u = s->userdata;
342 u->core->mainloop->defer_enable(u->defer, 1);
345 static void notify_source_cb(pa_source *s) {
346 struct userdata *u = s->userdata;
349 u->core->mainloop->defer_enable(u->defer, 1);
352 static int sink_get_hw_volume_cb(pa_sink *s) {
353 struct userdata *u = s->userdata;
355 pa_volume_t left, right;
357 if (waveOutGetVolume(u->hwo, &vol) != MMSYSERR_NOERROR)
360 left = (vol & 0xFFFF) * PA_VOLUME_NORM / WAVEOUT_MAX_VOLUME;
361 right = ((vol >> 16) & 0xFFFF) * PA_VOLUME_NORM / WAVEOUT_MAX_VOLUME;
363 /* Windows supports > 2 channels, except for volume control */
364 if (s->hw_volume.channels > 2)
365 pa_cvolume_set(&s->hw_volume, s->hw_volume.channels, (left + right)/2);
367 s->hw_volume.values[0] = left;
368 if (s->hw_volume.channels > 1)
369 s->hw_volume.values[1] = right;
374 static int sink_set_hw_volume_cb(pa_sink *s) {
375 struct userdata *u = s->userdata;
378 vol = s->hw_volume.values[0] * WAVEOUT_MAX_VOLUME / PA_VOLUME_NORM;
379 if (s->hw_volume.channels > 1)
380 vol |= (s->hw_volume.values[0] * WAVEOUT_MAX_VOLUME / PA_VOLUME_NORM) << 16;
382 if (waveOutSetVolume(u->hwo, vol) != MMSYSERR_NOERROR)
388 static int ss_to_waveformat(pa_sample_spec *ss, LPWAVEFORMATEX wf) {
389 wf->wFormatTag = WAVE_FORMAT_PCM;
391 if (ss->channels > 2) {
392 pa_log_error("ERROR: More than two channels not supported.");
396 wf->nChannels = ss->channels;
405 pa_log_error("ERROR: Unsupported sample rate.");
409 wf->nSamplesPerSec = ss->rate;
411 if (ss->format == PA_SAMPLE_U8)
412 wf->wBitsPerSample = 8;
413 else if (ss->format == PA_SAMPLE_S16NE)
414 wf->wBitsPerSample = 16;
416 pa_log_error("ERROR: Unsupported sample format.");
420 wf->nBlockAlign = wf->nChannels * wf->wBitsPerSample/8;
421 wf->nAvgBytesPerSec = wf->nSamplesPerSec * wf->nBlockAlign;
428 int pa__init(pa_core *c, pa_module*m) {
429 struct userdata *u = NULL;
430 HWAVEOUT hwo = INVALID_HANDLE_VALUE;
431 HWAVEIN hwi = INVALID_HANDLE_VALUE;
433 int nfrags, frag_size;
434 int record = 1, playback = 1;
437 pa_modargs *ma = NULL;
443 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
444 pa_log("failed to parse module arguments.");
448 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
449 pa_log("record= and playback= expect boolean argument.");
453 if (!playback && !record) {
454 pa_log("neither playback nor record enabled for device.");
460 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
461 pa_log("failed to parse fragments arguments");
465 ss = c->default_sample_spec;
466 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_WAVEEX) < 0) {
467 pa_log("failed to parse sample specification");
471 if (ss_to_waveformat(&ss, &wf) < 0)
474 u = pa_xmalloc(sizeof(struct userdata));
477 if (waveInOpen(&hwi, WAVE_MAPPER, &wf, (DWORD_PTR)chunk_ready_cb, (DWORD_PTR)u, CALLBACK_FUNCTION) != MMSYSERR_NOERROR) {
478 pa_log("failed to open waveIn");
481 if (waveInStart(hwi) != MMSYSERR_NOERROR) {
482 pa_log("failed to start waveIn");
485 pa_log_debug("Opened waveIn subsystem.");
489 if (waveOutOpen(&hwo, WAVE_MAPPER, &wf, (DWORD_PTR)chunk_done_cb, (DWORD_PTR)u, CALLBACK_FUNCTION) != MMSYSERR_NOERROR) {
490 pa_log("failed to open waveOut");
493 pa_log_debug("Opened waveOut subsystem.");
496 InitializeCriticalSection(&u->crit);
498 if (hwi != INVALID_HANDLE_VALUE) {
499 u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map);
501 u->source->userdata = u;
502 u->source->notify = notify_source_cb;
503 u->source->get_latency = source_get_latency_cb;
504 pa_source_set_owner(u->source, m);
505 pa_source_set_description(u->source, "Windows waveIn PCM");
506 u->source->is_hardware = 1;
510 if (hwo != INVALID_HANDLE_VALUE) {
511 u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map);
513 u->sink->notify = notify_sink_cb;
514 u->sink->get_latency = sink_get_latency_cb;
515 u->sink->get_hw_volume = sink_get_hw_volume_cb;
516 u->sink->set_hw_volume = sink_set_hw_volume_cb;
517 u->sink->userdata = u;
518 pa_sink_set_owner(u->sink, m);
519 pa_sink_set_description(u->sink, "Windows waveOut PCM");
520 u->sink->is_hardware = 1;
524 assert(u->source || u->sink);
530 u->fragments = nfrags;
531 u->free_ifrags = u->fragments;
532 u->free_ofrags = u->fragments;
533 u->fragment_size = frag_size - (frag_size % pa_frame_size(&ss));
535 u->written_bytes = 0;
536 u->sink_underflow = 1;
538 u->poll_timeout = pa_bytes_to_usec(u->fragments * u->fragment_size / 10, &ss);
540 pa_gettimeofday(&tv);
541 pa_timeval_add(&tv, u->poll_timeout);
543 u->event = c->mainloop->time_new(c->mainloop, &tv, poll_cb, u);
546 u->defer = c->mainloop->defer_new(c->mainloop, defer_cb, u);
548 c->mainloop->defer_enable(u->defer, 0);
552 u->ihdrs = pa_xmalloc0(sizeof(WAVEHDR) * u->fragments);
554 u->ohdrs = pa_xmalloc0(sizeof(WAVEHDR) * u->fragments);
556 for (i = 0;i < u->fragments;i++) {
557 u->ihdrs[i].dwBufferLength = u->fragment_size;
558 u->ohdrs[i].dwBufferLength = u->fragment_size;
559 u->ihdrs[i].lpData = pa_xmalloc(u->fragment_size);
561 u->ohdrs[i].lpData = pa_xmalloc(u->fragment_size);
570 /* Read mixer settings */
572 sink_get_hw_volume_cb(u->sink);
577 if (hwi != INVALID_HANDLE_VALUE)
580 if (hwo != INVALID_HANDLE_VALUE)
592 void pa__done(pa_core *c, pa_module*m) {
598 if (!(u = m->userdata))
602 c->mainloop->time_free(u->event);
605 c->mainloop->defer_free(u->defer);
608 pa_sink_disconnect(u->sink);
609 pa_sink_unref(u->sink);
613 pa_source_disconnect(u->source);
614 pa_source_unref(u->source);
617 if (u->hwi != INVALID_HANDLE_VALUE) {
622 if (u->hwo != INVALID_HANDLE_VALUE) {
623 waveOutReset(u->hwo);
624 waveOutClose(u->hwo);
627 for (i = 0;i < u->fragments;i++) {
628 pa_xfree(u->ihdrs[i].lpData);
629 pa_xfree(u->ohdrs[i].lpData);
635 DeleteCriticalSection(&u->crit);