4 This file is part of polypaudio.
6 polypaudio 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 polypaudio 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 polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <polyp/mainloop-api.h>
32 #include <polypcore/sink.h>
33 #include <polypcore/source.h>
34 #include <polypcore/module.h>
35 #include <polypcore/modargs.h>
36 #include <polypcore/sample-util.h>
37 #include <polypcore/util.h>
38 #include <polypcore/log.h>
39 #include <polypcore/xmalloc.h>
41 #include "module-waveout-symdef.h"
43 PA_MODULE_AUTHOR("Pierre Ossman")
44 PA_MODULE_DESCRIPTION("Windows waveOut Sink/Source")
45 PA_MODULE_VERSION(PACKAGE_VERSION)
47 "sink_name=<name for the sink> "
48 "source_name=<name for the source>"
49 "record=<enable source?> "
50 "playback=<enable sink?> "
51 "format=<sample format> "
52 "channels=<number of channels> "
54 "fragments=<number of fragments> "
55 "fragment_size=<fragment size> "
56 "channel_map=<channel map>")
58 #define DEFAULT_SINK_NAME "wave_output"
59 #define DEFAULT_SOURCE_NAME "wave_input"
61 #define WAVEOUT_MAX_VOLUME 0xFFFF
68 pa_defer_event *defer;
69 pa_usec_t poll_timeout;
71 uint32_t fragments, fragment_size;
73 uint32_t free_ofrags, free_ifrags;
77 int cur_ohdr, cur_ihdr;
79 WAVEHDR *ohdrs, *ihdrs;
86 CRITICAL_SECTION crit;
89 static const char* const valid_modargs[] = {
103 static void update_usage(struct userdata *u) {
104 pa_module_set_used(u->module,
105 (u->sink ? pa_idxset_size(u->sink->inputs) : 0) +
106 (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0) +
107 (u->source ? pa_idxset_size(u->source->outputs) : 0));
110 static void do_write(struct userdata *u)
112 uint32_t free_frags, remain;
113 pa_memchunk memchunk, *cur_chunk;
120 EnterCriticalSection(&u->crit);
122 free_frags = u->free_ofrags;
125 LeaveCriticalSection(&u->crit);
127 if (free_frags == u->fragments)
128 pa_log_debug(__FILE__": WaveOut underflow!");
131 hdr = &u->ohdrs[u->cur_ohdr];
132 if (hdr->dwFlags & WHDR_PREPARED)
133 waveOutUnprepareHeader(u->hwo, hdr, sizeof(WAVEHDR));
137 cur_chunk = &memchunk;
139 if (pa_sink_render(u->sink, remain, cur_chunk) < 0) {
141 * Don't fill with silence unless we're getting close to
144 if (free_frags > u->fragments/2)
145 cur_chunk = &u->silence;
147 EnterCriticalSection(&u->crit);
149 u->free_ofrags += free_frags;
151 LeaveCriticalSection(&u->crit);
158 assert(cur_chunk->memblock);
159 assert(cur_chunk->memblock->data);
160 assert(cur_chunk->length);
162 memcpy(hdr->lpData + u->fragment_size - remain,
163 (char*)cur_chunk->memblock->data + cur_chunk->index,
164 (cur_chunk->length < remain)?cur_chunk->length:remain);
166 remain -= (cur_chunk->length < remain)?cur_chunk->length:remain;
168 if (cur_chunk != &u->silence) {
169 pa_memblock_unref(cur_chunk->memblock);
170 cur_chunk->memblock = NULL;
174 res = waveOutPrepareHeader(u->hwo, hdr, sizeof(WAVEHDR));
175 if (res != MMSYSERR_NOERROR) {
176 pa_log_error(__FILE__ ": ERROR: Unable to prepare waveOut block: %d",
179 res = waveOutWrite(u->hwo, hdr, sizeof(WAVEHDR));
180 if (res != MMSYSERR_NOERROR) {
181 pa_log_error(__FILE__ ": ERROR: Unable to write waveOut block: %d",
185 u->written_bytes += u->fragment_size;
189 u->cur_ohdr %= u->fragments;
190 u->oremain = u->fragment_size;
194 static void do_read(struct userdata *u)
197 pa_memchunk memchunk;
204 EnterCriticalSection(&u->crit);
206 free_frags = u->free_ifrags;
209 LeaveCriticalSection(&u->crit);
211 if (free_frags == u->fragments)
212 pa_log_debug(__FILE__": WaveIn overflow!");
215 hdr = &u->ihdrs[u->cur_ihdr];
216 if (hdr->dwFlags & WHDR_PREPARED)
217 waveInUnprepareHeader(u->hwi, hdr, sizeof(WAVEHDR));
219 if (hdr->dwBytesRecorded) {
220 memchunk.memblock = pa_memblock_new(hdr->dwBytesRecorded, u->core->memblock_stat);
221 assert(memchunk.memblock);
223 memcpy((char*)memchunk.memblock->data, hdr->lpData, hdr->dwBytesRecorded);
225 memchunk.length = memchunk.memblock->length = hdr->dwBytesRecorded;
228 pa_source_post(u->source, &memchunk);
229 pa_memblock_unref(memchunk.memblock);
232 res = waveInPrepareHeader(u->hwi, hdr, sizeof(WAVEHDR));
233 if (res != MMSYSERR_NOERROR) {
234 pa_log_error(__FILE__ ": ERROR: Unable to prepare waveIn block: %d",
237 res = waveInAddBuffer(u->hwi, hdr, sizeof(WAVEHDR));
238 if (res != MMSYSERR_NOERROR) {
239 pa_log_error(__FILE__ ": ERROR: Unable to add waveIn block: %d",
245 u->cur_ihdr %= u->fragments;
249 static void poll_cb(pa_mainloop_api*a, pa_time_event *e, const struct timeval *tv, void *userdata) {
250 struct userdata *u = userdata;
260 pa_gettimeofday(&ntv);
261 pa_timeval_add(&ntv, u->poll_timeout);
263 a->time_restart(e, &ntv);
266 static void defer_cb(pa_mainloop_api*a, pa_defer_event *e, void *userdata) {
267 struct userdata *u = userdata;
271 a->defer_enable(e, 0);
277 static void CALLBACK chunk_done_cb(HWAVEOUT hwo, UINT msg, DWORD_PTR inst, DWORD param1, DWORD param2) {
278 struct userdata *u = (struct userdata *)inst;
283 EnterCriticalSection(&u->crit);
286 assert(u->free_ofrags <= u->fragments);
288 LeaveCriticalSection(&u->crit);
291 static void CALLBACK chunk_ready_cb(HWAVEIN hwi, UINT msg, DWORD_PTR inst, DWORD param1, DWORD param2) {
292 struct userdata *u = (struct userdata *)inst;
297 EnterCriticalSection(&u->crit);
300 assert(u->free_ifrags <= u->fragments);
302 LeaveCriticalSection(&u->crit);
305 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
306 struct userdata *u = s->userdata;
309 assert(s && u && u->sink);
311 memset(&mmt, 0, sizeof(mmt));
312 mmt.wType = TIME_BYTES;
313 if (waveOutGetPosition(u->hwo, &mmt, sizeof(mmt)) == MMSYSERR_NOERROR)
314 return pa_bytes_to_usec(u->written_bytes - mmt.u.cb, &s->sample_spec);
316 EnterCriticalSection(&u->crit);
318 free_frags = u->free_ofrags;
320 LeaveCriticalSection(&u->crit);
322 return pa_bytes_to_usec((u->fragments - free_frags) * u->fragment_size,
327 static pa_usec_t source_get_latency_cb(pa_source *s) {
329 struct userdata *u = s->userdata;
331 assert(s && u && u->sink);
333 EnterCriticalSection(&u->crit);
335 free_frags = u->free_ifrags;
337 LeaveCriticalSection(&u->crit);
339 r += pa_bytes_to_usec((free_frags + 1) * u->fragment_size, &s->sample_spec);
344 static void notify_sink_cb(pa_sink *s) {
345 struct userdata *u = s->userdata;
348 u->core->mainloop->defer_enable(u->defer, 1);
351 static void notify_source_cb(pa_source *s) {
352 struct userdata *u = s->userdata;
355 u->core->mainloop->defer_enable(u->defer, 1);
358 static int sink_get_hw_volume_cb(pa_sink *s) {
359 struct userdata *u = s->userdata;
361 pa_volume_t left, right;
363 if (waveOutGetVolume(u->hwo, &vol) != MMSYSERR_NOERROR)
366 left = (vol & 0xFFFF) * PA_VOLUME_NORM / WAVEOUT_MAX_VOLUME;
367 right = ((vol >> 16) & 0xFFFF) * PA_VOLUME_NORM / WAVEOUT_MAX_VOLUME;
369 /* Windows supports > 2 channels, except for volume control */
370 if (s->hw_volume.channels > 2)
371 pa_cvolume_set(&s->hw_volume, s->hw_volume.channels, (left + right)/2);
373 s->hw_volume.values[0] = left;
374 if (s->hw_volume.channels > 1)
375 s->hw_volume.values[1] = right;
380 static int sink_set_hw_volume_cb(pa_sink *s) {
381 struct userdata *u = s->userdata;
384 vol = s->hw_volume.values[0] * WAVEOUT_MAX_VOLUME / PA_VOLUME_NORM;
385 if (s->hw_volume.channels > 1)
386 vol |= (s->hw_volume.values[0] * WAVEOUT_MAX_VOLUME / PA_VOLUME_NORM) << 16;
388 if (waveOutSetVolume(u->hwo, vol) != MMSYSERR_NOERROR)
394 static int ss_to_waveformat(pa_sample_spec *ss, LPWAVEFORMATEX wf) {
395 wf->wFormatTag = WAVE_FORMAT_PCM;
397 if (ss->channels > 2) {
398 pa_log_error(__FILE__": ERROR: More than two channels not supported.");
402 wf->nChannels = ss->channels;
411 pa_log_error(__FILE__": ERROR: Unsupported sample rate.");
415 wf->nSamplesPerSec = ss->rate;
417 if (ss->format == PA_SAMPLE_U8)
418 wf->wBitsPerSample = 8;
419 else if (ss->format == PA_SAMPLE_S16NE)
420 wf->wBitsPerSample = 16;
422 pa_log_error(__FILE__": ERROR: Unsupported sample format.");
426 wf->nBlockAlign = wf->nChannels * wf->wBitsPerSample/8;
427 wf->nAvgBytesPerSec = wf->nSamplesPerSec * wf->nBlockAlign;
434 int pa__init(pa_core *c, pa_module*m) {
435 struct userdata *u = NULL;
436 HWAVEOUT hwo = INVALID_HANDLE_VALUE;
437 HWAVEIN hwi = INVALID_HANDLE_VALUE;
439 int nfrags, frag_size;
440 int record = 1, playback = 1;
443 pa_modargs *ma = NULL;
449 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
450 pa_log(__FILE__": failed to parse module arguments.");
454 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
455 pa_log(__FILE__": record= and playback= expect boolean argument.");
459 if (!playback && !record) {
460 pa_log(__FILE__": neither playback nor record enabled for device.");
466 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
467 pa_log(__FILE__": failed to parse fragments arguments");
471 ss = c->default_sample_spec;
472 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_WAVEEX) < 0) {
473 pa_log(__FILE__": failed to parse sample specification");
477 if (ss_to_waveformat(&ss, &wf) < 0)
480 u = pa_xmalloc(sizeof(struct userdata));
483 if (waveInOpen(&hwi, WAVE_MAPPER, &wf, (DWORD_PTR)chunk_ready_cb, (DWORD_PTR)u, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
485 if (waveInStart(hwi) != MMSYSERR_NOERROR)
487 pa_log_debug(__FILE__": Opened waveIn subsystem.");
491 if (waveOutOpen(&hwo, WAVE_MAPPER, &wf, (DWORD_PTR)chunk_done_cb, (DWORD_PTR)u, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
493 pa_log_debug(__FILE__": 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 u->source->description = pa_sprintf_malloc("Windows waveIn PCM");
509 if (hwo != INVALID_HANDLE_VALUE) {
510 u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map);
512 u->sink->notify = notify_sink_cb;
513 u->sink->get_latency = sink_get_latency_cb;
514 u->sink->get_hw_volume = sink_get_hw_volume_cb;
515 u->sink->set_hw_volume = sink_set_hw_volume_cb;
516 u->sink->userdata = u;
517 pa_sink_set_owner(u->sink, m);
518 u->sink->description = pa_sprintf_malloc("Windows waveOut PCM");
522 assert(u->source || u->sink);
528 u->fragments = nfrags;
529 u->free_ifrags = u->fragments;
530 u->free_ofrags = u->fragments;
531 u->fragment_size = frag_size - (frag_size % pa_frame_size(&ss));
533 u->written_bytes = 0;
535 u->oremain = u->fragment_size;
537 u->poll_timeout = pa_bytes_to_usec(u->fragments * u->fragment_size / 10, &ss);
539 pa_gettimeofday(&tv);
540 pa_timeval_add(&tv, u->poll_timeout);
542 u->event = c->mainloop->time_new(c->mainloop, &tv, poll_cb, u);
545 u->defer = c->mainloop->defer_new(c->mainloop, defer_cb, u);
547 c->mainloop->defer_enable(u->defer, 0);
551 u->ihdrs = pa_xmalloc0(sizeof(WAVEHDR) * u->fragments);
553 u->ohdrs = pa_xmalloc0(sizeof(WAVEHDR) * u->fragments);
555 for (i = 0;i < u->fragments;i++) {
556 u->ihdrs[i].dwBufferLength = u->fragment_size;
557 u->ohdrs[i].dwBufferLength = u->fragment_size;
558 u->ihdrs[i].lpData = pa_xmalloc(u->fragment_size);
560 u->ohdrs[i].lpData = pa_xmalloc(u->fragment_size);
564 u->silence.length = u->fragment_size;
565 u->silence.memblock = pa_memblock_new(u->silence.length, u->core->memblock_stat);
566 assert(u->silence.memblock);
567 pa_silence_memblock(u->silence.memblock, &ss);
568 u->silence.index = 0;
575 /* Read mixer settings */
577 sink_get_hw_volume_cb(u->sink);
582 if (hwi != INVALID_HANDLE_VALUE)
585 if (hwo != INVALID_HANDLE_VALUE)
597 void pa__done(pa_core *c, pa_module*m) {
603 if (!(u = m->userdata))
607 c->mainloop->time_free(u->event);
610 c->mainloop->defer_free(u->defer);
613 pa_sink_disconnect(u->sink);
614 pa_sink_unref(u->sink);
618 pa_source_disconnect(u->source);
619 pa_source_unref(u->source);
622 if (u->hwi != INVALID_HANDLE_VALUE) {
627 if (u->hwo != INVALID_HANDLE_VALUE) {
628 waveOutReset(u->hwo);
629 waveOutClose(u->hwo);
632 for (i = 0;i < u->fragments;i++) {
633 pa_xfree(u->ihdrs[i].lpData);
634 pa_xfree(u->ohdrs[i].lpData);
640 DeleteCriticalSection(&u->crit);