]> code.delx.au - pulseaudio/blob - polyp/module-waveout.c
Merge Pierre's changes
[pulseaudio] / polyp / module-waveout.c
1 /* $Id: module-waveout.c 333 2005-01-08 21:36:53Z lennart $ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <windows.h>
27 #include <mmsystem.h>
28 #include <assert.h>
29
30 #include "sink.h"
31 #include "source.h"
32 #include "module.h"
33 #include "mainloop-api.h"
34 #include "modargs.h"
35 #include "sample-util.h"
36 #include "util.h"
37 #include "log.h"
38 #include "xmalloc.h"
39 #include "module-waveout-symdef.h"
40
41 PA_MODULE_AUTHOR("Pierre Ossman")
42 PA_MODULE_DESCRIPTION("Windows waveOut Sink/Source")
43 PA_MODULE_VERSION(PACKAGE_VERSION)
44 PA_MODULE_USAGE("sink_name=<name for the sink> source_name=<name for the source> record=<enable source?> playback=<enable sink?> format=<sample format> channels=<number of channels> rate=<sample rate> fragments=<number of fragments> fragment_size=<fragment size>")
45
46 #define PA_TYPEID_WAVEOUT PA_TYPEID_MAKE('W', 'A', 'V', 'E')
47
48 #define DEFAULT_SINK_NAME "wave_output"
49 #define DEFAULT_SOURCE_NAME "wave_input"
50
51 struct userdata {
52 struct pa_sink *sink;
53 struct pa_source *source;
54 struct pa_core *core;
55 struct pa_time_event *event;
56 struct pa_defer_event *defer;
57 pa_usec_t poll_timeout;
58
59 uint32_t fragments, fragment_size;
60
61 uint32_t free_ofrags, free_ifrags;
62
63 DWORD written_bytes;
64
65 int cur_ohdr, cur_ihdr;
66 unsigned int oremain;
67 WAVEHDR *ohdrs, *ihdrs;
68 struct pa_memchunk silence;
69
70 HWAVEOUT hwo;
71 HWAVEIN hwi;
72 struct pa_module *module;
73
74 CRITICAL_SECTION crit;
75 };
76
77 static const char* const valid_modargs[] = {
78 "sink_name",
79 "source_name",
80 "record",
81 "playback",
82 "fragments",
83 "fragment_size",
84 "format",
85 "rate",
86 "channels",
87 NULL
88 };
89
90 static void update_usage(struct userdata *u) {
91 pa_module_set_used(u->module,
92 (u->sink ? pa_idxset_ncontents(u->sink->inputs) : 0) +
93 (u->sink ? pa_idxset_ncontents(u->sink->monitor_source->outputs) : 0) +
94 (u->source ? pa_idxset_ncontents(u->source->outputs) : 0));
95 }
96
97 static void do_write(struct userdata *u)
98 {
99 uint32_t free_frags, remain;
100 struct pa_memchunk memchunk, *cur_chunk;
101 WAVEHDR *hdr;
102 MMRESULT res;
103
104 if (!u->sink)
105 return;
106
107 EnterCriticalSection(&u->crit);
108
109 free_frags = u->free_ofrags;
110 u->free_ofrags = 0;
111
112 LeaveCriticalSection(&u->crit);
113
114 while (free_frags) {
115 hdr = &u->ohdrs[u->cur_ohdr];
116 if (hdr->dwFlags & WHDR_PREPARED)
117 waveOutUnprepareHeader(u->hwo, hdr, sizeof(WAVEHDR));
118
119 remain = u->oremain;
120 while (remain) {
121 cur_chunk = &memchunk;
122
123 if (pa_sink_render(u->sink, remain, cur_chunk) < 0) {
124 /*
125 * Don't fill with silence unless we're getting close to
126 * underflowing.
127 */
128 if (free_frags > u->fragments/2)
129 cur_chunk = &u->silence;
130 else {
131 EnterCriticalSection(&u->crit);
132
133 u->free_ofrags += free_frags;
134
135 LeaveCriticalSection(&u->crit);
136
137 u->oremain = remain;
138 return;
139 }
140 }
141
142 assert(cur_chunk->memblock);
143 assert(cur_chunk->memblock->data);
144 assert(cur_chunk->length);
145
146 memcpy(hdr->lpData + u->fragment_size - remain,
147 (char*)cur_chunk->memblock->data + cur_chunk->index,
148 (cur_chunk->length < remain)?cur_chunk->length:remain);
149
150 remain -= (cur_chunk->length < remain)?cur_chunk->length:remain;
151
152 if (cur_chunk != &u->silence) {
153 pa_memblock_unref(cur_chunk->memblock);
154 cur_chunk->memblock = NULL;
155 }
156 }
157
158 res = waveOutPrepareHeader(u->hwo, hdr, sizeof(WAVEHDR));
159 if (res != MMSYSERR_NOERROR) {
160 pa_log_error(__FILE__ ": ERROR: Unable to prepare waveOut block: %d\n",
161 res);
162 }
163 res = waveOutWrite(u->hwo, hdr, sizeof(WAVEHDR));
164 if (res != MMSYSERR_NOERROR) {
165 pa_log_error(__FILE__ ": ERROR: Unable to write waveOut block: %d\n",
166 res);
167 }
168
169 u->written_bytes += u->fragment_size;
170
171 free_frags--;
172 u->cur_ohdr++;
173 u->cur_ohdr %= u->fragments;
174 u->oremain = u->fragment_size;
175 }
176 }
177
178 static void do_read(struct userdata *u)
179 {
180 uint32_t free_frags;
181 struct pa_memchunk memchunk;
182 WAVEHDR *hdr;
183 MMRESULT res;
184
185 if (!u->source)
186 return;
187
188 EnterCriticalSection(&u->crit);
189
190 free_frags = u->free_ifrags;
191 u->free_ifrags = 0;
192
193 LeaveCriticalSection(&u->crit);
194
195 while (free_frags) {
196 hdr = &u->ihdrs[u->cur_ihdr];
197 if (hdr->dwFlags & WHDR_PREPARED)
198 waveInUnprepareHeader(u->hwi, hdr, sizeof(WAVEHDR));
199
200 if (hdr->dwBytesRecorded) {
201 memchunk.memblock = pa_memblock_new(hdr->dwBytesRecorded, u->core->memblock_stat);
202 assert(memchunk.memblock);
203
204 memcpy((char*)memchunk.memblock->data, hdr->lpData, hdr->dwBytesRecorded);
205
206 memchunk.length = memchunk.memblock->length = hdr->dwBytesRecorded;
207 memchunk.index = 0;
208
209 pa_source_post(u->source, &memchunk);
210 pa_memblock_unref(memchunk.memblock);
211 }
212
213 res = waveInPrepareHeader(u->hwi, hdr, sizeof(WAVEHDR));
214 if (res != MMSYSERR_NOERROR) {
215 pa_log_error(__FILE__ ": ERROR: Unable to prepare waveIn block: %d\n",
216 res);
217 }
218 res = waveInAddBuffer(u->hwi, hdr, sizeof(WAVEHDR));
219 if (res != MMSYSERR_NOERROR) {
220 pa_log_error(__FILE__ ": ERROR: Unable to add waveIn block: %d\n",
221 res);
222 }
223
224 free_frags--;
225 u->cur_ihdr++;
226 u->cur_ihdr %= u->fragments;
227 }
228 }
229
230 static void poll_cb(struct pa_mainloop_api*a, struct pa_time_event *e, const struct timeval *tv, void *userdata) {
231 struct userdata *u = userdata;
232 struct timeval ntv;
233
234 assert(u);
235
236 update_usage(u);
237
238 do_write(u);
239 do_read(u);
240
241 pa_gettimeofday(&ntv);
242 pa_timeval_add(&ntv, u->poll_timeout);
243
244 a->time_restart(e, &ntv);
245 }
246
247 static void defer_cb(struct pa_mainloop_api*a, struct pa_defer_event *e, void *userdata) {
248 struct userdata *u = userdata;
249
250 assert(u);
251
252 a->defer_enable(e, 0);
253
254 do_write(u);
255 do_read(u);
256 }
257
258 static void CALLBACK chunk_done_cb(HWAVEOUT hwo, UINT msg, DWORD_PTR inst, DWORD param1, DWORD param2) {
259 struct userdata *u = (struct userdata *)inst;
260
261 if (msg != WOM_DONE)
262 return;
263
264 EnterCriticalSection(&u->crit);
265
266 u->free_ofrags++;
267 assert(u->free_ofrags <= u->fragments);
268
269 LeaveCriticalSection(&u->crit);
270 }
271
272 static void CALLBACK chunk_ready_cb(HWAVEIN hwi, UINT msg, DWORD_PTR inst, DWORD param1, DWORD param2) {
273 struct userdata *u = (struct userdata *)inst;
274
275 if (msg != WIM_DATA)
276 return;
277
278 EnterCriticalSection(&u->crit);
279
280 u->free_ifrags++;
281 assert(u->free_ifrags <= u->fragments);
282
283 LeaveCriticalSection(&u->crit);
284 }
285
286 static pa_usec_t sink_get_latency_cb(struct pa_sink *s) {
287 struct userdata *u = s->userdata;
288 uint32_t free_frags;
289 MMTIME mmt;
290 assert(s && u && u->sink);
291
292 memset(&mmt, 0, sizeof(mmt));
293 mmt.wType = TIME_BYTES;
294 if (waveOutGetPosition(u->hwo, &mmt, sizeof(mmt)) == MMSYSERR_NOERROR)
295 return pa_bytes_to_usec(u->written_bytes - mmt.u.cb, &s->sample_spec);
296 else {
297 EnterCriticalSection(&u->crit);
298
299 free_frags = u->free_ofrags;
300
301 LeaveCriticalSection(&u->crit);
302
303 return pa_bytes_to_usec((u->fragments - free_frags) * u->fragment_size,
304 &s->sample_spec);
305 }
306 }
307
308 static pa_usec_t source_get_latency_cb(struct pa_source *s) {
309 pa_usec_t r = 0;
310 struct userdata *u = s->userdata;
311 uint32_t free_frags;
312 assert(s && u && u->sink);
313
314 EnterCriticalSection(&u->crit);
315
316 free_frags = u->free_ifrags;
317
318 LeaveCriticalSection(&u->crit);
319
320 r += pa_bytes_to_usec((free_frags + 1) * u->fragment_size, &s->sample_spec);
321
322 fprintf(stderr, "Latency: %d us\n", (int)r);
323
324 return r;
325 }
326
327 static void notify_sink_cb(struct pa_sink *s) {
328 struct userdata *u = s->userdata;
329 assert(u);
330
331 u->core->mainloop->defer_enable(u->defer, 1);
332 }
333
334 static void notify_source_cb(struct pa_source *s) {
335 struct userdata *u = s->userdata;
336 assert(u);
337
338 u->core->mainloop->defer_enable(u->defer, 1);
339 }
340
341 static int ss_to_waveformat(struct pa_sample_spec *ss, LPWAVEFORMATEX wf) {
342 wf->wFormatTag = WAVE_FORMAT_PCM;
343
344 if (ss->channels > 2) {
345 pa_log_error(__FILE__": ERROR: More than two channels not supported.\n");
346 return -1;
347 }
348
349 wf->nChannels = ss->channels;
350
351 switch (ss->rate) {
352 case 8000:
353 case 11025:
354 case 22005:
355 case 44100:
356 break;
357 default:
358 pa_log_error(__FILE__": ERROR: Unsupported sample rate.\n");
359 return -1;
360 }
361
362 wf->nSamplesPerSec = ss->rate;
363
364 if (ss->format == PA_SAMPLE_U8)
365 wf->wBitsPerSample = 8;
366 else if (ss->format == PA_SAMPLE_S16NE)
367 wf->wBitsPerSample = 16;
368 else {
369 pa_log_error(__FILE__": ERROR: Unsupported sample format.\n");
370 return -1;
371 }
372
373 wf->nBlockAlign = wf->nChannels * wf->wBitsPerSample/8;
374 wf->nAvgBytesPerSec = wf->nSamplesPerSec * wf->nBlockAlign;
375
376 wf->cbSize = 0;
377
378 return 0;
379 }
380
381 int pa__init(struct pa_core *c, struct pa_module*m) {
382 struct userdata *u = NULL;
383 HWAVEOUT hwo = INVALID_HANDLE_VALUE;
384 HWAVEIN hwi = INVALID_HANDLE_VALUE;
385 WAVEFORMATEX wf;
386 int nfrags, frag_size;
387 int record = 1, playback = 1;
388 struct pa_sample_spec ss;
389 struct pa_modargs *ma = NULL;
390 unsigned int i;
391 struct timeval tv;
392
393 assert(c && m);
394
395 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
396 pa_log(__FILE__": failed to parse module arguments.\n");
397 goto fail;
398 }
399
400 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
401 pa_log(__FILE__": record= and playback= expect boolean argument.\n");
402 goto fail;
403 }
404
405 if (!playback && !record) {
406 pa_log(__FILE__": neither playback nor record enabled for device.\n");
407 goto fail;
408 }
409
410 nfrags = 20;
411 frag_size = 1024;
412 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
413 pa_log(__FILE__": failed to parse fragments arguments\n");
414 goto fail;
415 }
416
417 ss = c->default_sample_spec;
418 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
419 pa_log(__FILE__": failed to parse sample specification\n");
420 goto fail;
421 }
422
423 if (ss_to_waveformat(&ss, &wf) < 0)
424 goto fail;
425
426 u = pa_xmalloc(sizeof(struct userdata));
427
428 if (record) {
429 if (waveInOpen(&hwi, WAVE_MAPPER, &wf, (DWORD_PTR)chunk_ready_cb, (DWORD_PTR)u, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
430 goto fail;
431 if (waveInStart(hwi) != MMSYSERR_NOERROR)
432 goto fail;
433 pa_log_debug(__FILE__": Opened waveIn subsystem.\n");
434 }
435
436 if (playback) {
437 if (waveOutOpen(&hwo, WAVE_MAPPER, &wf, (DWORD_PTR)chunk_done_cb, (DWORD_PTR)u, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
438 goto fail;
439 pa_log_debug(__FILE__": Opened waveOut subsystem.\n");
440 }
441
442 InitializeCriticalSection(&u->crit);
443
444 if (hwi != INVALID_HANDLE_VALUE) {
445 u->source = pa_source_new(c, PA_TYPEID_WAVEOUT, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss);
446 assert(u->source);
447 u->source->userdata = u;
448 u->source->notify = notify_source_cb;
449 u->source->get_latency = source_get_latency_cb;
450 pa_source_set_owner(u->source, m);
451 u->source->description = pa_sprintf_malloc("Windows waveIn PCM");
452 } else
453 u->source = NULL;
454
455 if (hwo != INVALID_HANDLE_VALUE) {
456 u->sink = pa_sink_new(c, PA_TYPEID_WAVEOUT, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss);
457 assert(u->sink);
458 u->sink->notify = notify_sink_cb;
459 u->sink->get_latency = sink_get_latency_cb;
460 u->sink->userdata = u;
461 pa_sink_set_owner(u->sink, m);
462 u->sink->description = pa_sprintf_malloc("Windows waveOut PCM");
463 } else
464 u->sink = NULL;
465
466 assert(u->source || u->sink);
467
468 u->core = c;
469 u->hwi = hwi;
470 u->hwo = hwo;
471
472 u->fragments = nfrags;
473 u->free_ifrags = u->fragments;
474 u->free_ofrags = u->fragments;
475 u->fragment_size = frag_size - (frag_size % pa_frame_size(&ss));
476
477 u->written_bytes = 0;
478
479 u->oremain = u->fragment_size;
480
481 u->poll_timeout = pa_bytes_to_usec(u->fragments * u->fragment_size / 3, &ss);
482
483 pa_gettimeofday(&tv);
484 pa_timeval_add(&tv, u->poll_timeout);
485
486 u->event = c->mainloop->time_new(c->mainloop, &tv, poll_cb, u);
487 assert(u->event);
488
489 u->defer = c->mainloop->defer_new(c->mainloop, defer_cb, u);
490 assert(u->defer);
491 c->mainloop->defer_enable(u->defer, 0);
492
493 u->cur_ihdr = 0;
494 u->cur_ohdr = 0;
495 u->ihdrs = pa_xmalloc0(sizeof(WAVEHDR) * u->fragments);
496 assert(u->ihdrs);
497 u->ohdrs = pa_xmalloc0(sizeof(WAVEHDR) * u->fragments);
498 assert(u->ohdrs);
499 for (i = 0;i < u->fragments;i++) {
500 u->ihdrs[i].dwBufferLength = u->fragment_size;
501 u->ohdrs[i].dwBufferLength = u->fragment_size;
502 u->ihdrs[i].lpData = pa_xmalloc(u->fragment_size);
503 assert(u->ihdrs);
504 u->ohdrs[i].lpData = pa_xmalloc(u->fragment_size);
505 assert(u->ohdrs);
506 }
507
508 u->silence.length = u->fragment_size;
509 u->silence.memblock = pa_memblock_new(u->silence.length, u->core->memblock_stat);
510 assert(u->silence.memblock);
511 pa_silence_memblock(u->silence.memblock, &ss);
512 u->silence.index = 0;
513
514 u->module = m;
515 m->userdata = u;
516
517 pa_modargs_free(ma);
518
519 return 0;
520
521 fail:
522 if (hwi != INVALID_HANDLE_VALUE)
523 waveInClose(hwi);
524
525 if (hwo != INVALID_HANDLE_VALUE)
526 waveOutClose(hwo);
527
528 if (u)
529 pa_xfree(u);
530
531 if (ma)
532 pa_modargs_free(ma);
533
534 return -1;
535 }
536
537 void pa__done(struct pa_core *c, struct pa_module*m) {
538 struct userdata *u;
539 unsigned int i;
540
541 assert(c && m);
542
543 if (!(u = m->userdata))
544 return;
545
546 if (u->event)
547 c->mainloop->time_free(u->event);
548
549 if (u->defer)
550 c->mainloop->defer_free(u->defer);
551
552 if (u->sink) {
553 pa_sink_disconnect(u->sink);
554 pa_sink_unref(u->sink);
555 }
556
557 if (u->source) {
558 pa_source_disconnect(u->source);
559 pa_source_unref(u->source);
560 }
561
562 if (u->hwi != INVALID_HANDLE_VALUE) {
563 waveInReset(u->hwi);
564 waveInClose(u->hwi);
565 }
566
567 if (u->hwo != INVALID_HANDLE_VALUE) {
568 waveOutReset(u->hwo);
569 waveOutClose(u->hwo);
570 }
571
572 for (i = 0;i < u->fragments;i++) {
573 pa_xfree(u->ihdrs[i].lpData);
574 pa_xfree(u->ohdrs[i].lpData);
575 }
576
577 pa_xfree(u->ihdrs);
578 pa_xfree(u->ohdrs);
579
580 DeleteCriticalSection(&u->crit);
581
582 pa_xfree(u);
583 }