X-Git-Url: https://code.delx.au/pulseaudio/blobdiff_plain/7e5123705a4811d168890e35cff2354922777c71..6031546f6660e06e05d0b40a4dbc0c8769eb10a6:/src/modules/module-waveout.c diff --git a/src/modules/module-waveout.c b/src/modules/module-waveout.c index 2176e6e7..53efce9e 100644 --- a/src/modules/module-waveout.c +++ b/src/modules/module-waveout.c @@ -1,20 +1,21 @@ -/* $Id$ */ - /*** - This file is part of polypaudio. - - polypaudio is free software; you can redistribute it and/or modify + This file is part of PulseAudio. + + Copyright 2006 Lennart Poettering + Copyright 2006-2007 Pierre Ossman for Cendio AB + + PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published - by the Free Software Foundation; either version 2 of the License, + by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. - - polypaudio is distributed in the hope that it will be useful, but + + PulseAudio 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 Lesser General Public License - along with polypaudio; if not, write to the Free Software + along with PulseAudio; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. ***/ @@ -25,25 +26,38 @@ #include #include -#include -#include +#include +#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "module-waveout-symdef.h" -PA_MODULE_AUTHOR("Pierre Ossman") -PA_MODULE_DESCRIPTION("Windows waveOut Sink/Source") -PA_MODULE_VERSION(PACKAGE_VERSION) -PA_MODULE_USAGE("sink_name= source_name= record= playback= format= channels= rate= fragments= fragment_size=") +PA_MODULE_AUTHOR("Pierre Ossman"); +PA_MODULE_DESCRIPTION("Windows waveOut Sink/Source"); +PA_MODULE_VERSION(PACKAGE_VERSION); +PA_MODULE_USAGE( + "sink_name= " + "source_name= " + "device= " + "device_name= " + "record= " + "playback= " + "format= " + "rate= " + "channels= " + "channel_map= " + "fragments= " + "fragment_size="); #define DEFAULT_SINK_NAME "wave_output" #define DEFAULT_SOURCE_NAME "wave_input" @@ -54,20 +68,21 @@ struct userdata { pa_sink *sink; pa_source *source; pa_core *core; - pa_time_event *event; - pa_defer_event *defer; pa_usec_t poll_timeout; + pa_thread *thread; + pa_thread_mq thread_mq; + pa_rtpoll *rtpoll; + uint32_t fragments, fragment_size; uint32_t free_ofrags, free_ifrags; DWORD written_bytes; + int sink_underflow; int cur_ohdr, cur_ihdr; - unsigned int oremain; WAVEHDR *ohdrs, *ihdrs; - pa_memchunk silence; HWAVEOUT hwo; HWAVEIN hwi; @@ -79,6 +94,8 @@ struct userdata { static const char* const valid_modargs[] = { "sink_name", "source_name", + "device", + "device_name", "record", "playback", "fragments", @@ -86,126 +103,121 @@ static const char* const valid_modargs[] = { "format", "rate", "channels", + "channel_map", NULL }; -static void update_usage(struct userdata *u) { - pa_module_set_used(u->module, - (u->sink ? pa_idxset_size(u->sink->inputs) : 0) + - (u->sink ? pa_idxset_size(u->sink->monitor_source->outputs) : 0) + - (u->source ? pa_idxset_size(u->source->outputs) : 0)); -} - -static void do_write(struct userdata *u) -{ - uint32_t free_frags, remain; - pa_memchunk memchunk, *cur_chunk; +static void do_write(struct userdata *u) { + uint32_t free_frags; + pa_memchunk memchunk; WAVEHDR *hdr; MMRESULT res; + void *p; if (!u->sink) return; - EnterCriticalSection(&u->crit); + if (!PA_SINK_IS_LINKED(u->sink->state)) + return; + EnterCriticalSection(&u->crit); free_frags = u->free_ofrags; - u->free_ofrags = 0; - LeaveCriticalSection(&u->crit); + if (!u->sink_underflow && (free_frags == u->fragments)) + pa_log_debug("WaveOut underflow!"); + while (free_frags) { hdr = &u->ohdrs[u->cur_ohdr]; if (hdr->dwFlags & WHDR_PREPARED) waveOutUnprepareHeader(u->hwo, hdr, sizeof(WAVEHDR)); - remain = u->oremain; - while (remain) { - cur_chunk = &memchunk; + hdr->dwBufferLength = 0; + while (hdr->dwBufferLength < u->fragment_size) { + size_t len; - if (pa_sink_render(u->sink, remain, cur_chunk) < 0) { - /* - * Don't fill with silence unless we're getting close to - * underflowing. - */ - if (free_frags > u->fragments/2) - cur_chunk = &u->silence; - else { - EnterCriticalSection(&u->crit); + len = u->fragment_size - hdr->dwBufferLength; - u->free_ofrags += free_frags; + pa_sink_render(u->sink, len, &memchunk); - LeaveCriticalSection(&u->crit); + pa_assert(memchunk.memblock); + pa_assert(memchunk.length); - u->oremain = remain; - return; - } - } + if (memchunk.length < len) + len = memchunk.length; - assert(cur_chunk->memblock); - assert(cur_chunk->memblock->data); - assert(cur_chunk->length); + p = pa_memblock_acquire(memchunk.memblock); + memcpy(hdr->lpData + hdr->dwBufferLength, (char*) p + memchunk.index, len); + pa_memblock_release(memchunk.memblock); - memcpy(hdr->lpData + u->fragment_size - remain, - (char*)cur_chunk->memblock->data + cur_chunk->index, - (cur_chunk->length < remain)?cur_chunk->length:remain); + hdr->dwBufferLength += len; - remain -= (cur_chunk->length < remain)?cur_chunk->length:remain; + pa_memblock_unref(memchunk.memblock); + memchunk.memblock = NULL; + } - if (cur_chunk != &u->silence) { - pa_memblock_unref(cur_chunk->memblock); - cur_chunk->memblock = NULL; - } + /* Underflow detection */ + if (hdr->dwBufferLength == 0) { + u->sink_underflow = 1; + break; } + u->sink_underflow = 0; res = waveOutPrepareHeader(u->hwo, hdr, sizeof(WAVEHDR)); - if (res != MMSYSERR_NOERROR) { - pa_log_error(__FILE__ ": ERROR: Unable to prepare waveOut block: %d\n", - res); - } + if (res != MMSYSERR_NOERROR) + pa_log_error("Unable to prepare waveOut block: %d", res); + res = waveOutWrite(u->hwo, hdr, sizeof(WAVEHDR)); - if (res != MMSYSERR_NOERROR) { - pa_log_error(__FILE__ ": ERROR: Unable to write waveOut block: %d\n", - res); - } - - u->written_bytes += u->fragment_size; + if (res != MMSYSERR_NOERROR) + pa_log_error("Unable to write waveOut block: %d", res); + + u->written_bytes += hdr->dwBufferLength; + + EnterCriticalSection(&u->crit); + u->free_ofrags--; + LeaveCriticalSection(&u->crit); free_frags--; u->cur_ohdr++; u->cur_ohdr %= u->fragments; - u->oremain = u->fragment_size; } } -static void do_read(struct userdata *u) -{ +static void do_read(struct userdata *u) { uint32_t free_frags; pa_memchunk memchunk; WAVEHDR *hdr; MMRESULT res; + void *p; if (!u->source) return; - EnterCriticalSection(&u->crit); + if (!PA_SOURCE_IS_LINKED(u->source->state)) + return; + EnterCriticalSection(&u->crit); free_frags = u->free_ifrags; u->free_ifrags = 0; - LeaveCriticalSection(&u->crit); + if (free_frags == u->fragments) + pa_log_debug("WaveIn overflow!"); + while (free_frags) { hdr = &u->ihdrs[u->cur_ihdr]; if (hdr->dwFlags & WHDR_PREPARED) waveInUnprepareHeader(u->hwi, hdr, sizeof(WAVEHDR)); if (hdr->dwBytesRecorded) { - memchunk.memblock = pa_memblock_new(hdr->dwBytesRecorded, u->core->memblock_stat); - assert(memchunk.memblock); + memchunk.memblock = pa_memblock_new(u->core->mempool, hdr->dwBytesRecorded); + pa_assert(memchunk.memblock); - memcpy((char*)memchunk.memblock->data, hdr->lpData, hdr->dwBytesRecorded); + p = pa_memblock_acquire(memchunk.memblock); + memcpy((char*) p, hdr->lpData, hdr->dwBytesRecorded); + pa_memblock_release(memchunk.memblock); - memchunk.length = memchunk.memblock->length = hdr->dwBytesRecorded; + memchunk.length = hdr->dwBytesRecorded; memchunk.index = 0; pa_source_post(u->source, &memchunk); @@ -213,188 +225,240 @@ static void do_read(struct userdata *u) } res = waveInPrepareHeader(u->hwi, hdr, sizeof(WAVEHDR)); - if (res != MMSYSERR_NOERROR) { - pa_log_error(__FILE__ ": ERROR: Unable to prepare waveIn block: %d\n", - res); - } + if (res != MMSYSERR_NOERROR) + pa_log_error("Unable to prepare waveIn block: %d", res); + res = waveInAddBuffer(u->hwi, hdr, sizeof(WAVEHDR)); - if (res != MMSYSERR_NOERROR) { - pa_log_error(__FILE__ ": ERROR: Unable to add waveIn block: %d\n", - res); - } - + if (res != MMSYSERR_NOERROR) + pa_log_error("Unable to add waveIn block: %d", res); + free_frags--; u->cur_ihdr++; u->cur_ihdr %= u->fragments; } } -static void poll_cb(pa_mainloop_api*a, pa_time_event *e, const struct timeval *tv, void *userdata) { +static void thread_func(void *userdata) { struct userdata *u = userdata; - struct timeval ntv; - assert(u); + pa_assert(u); + pa_assert(u->sink || u->source); - update_usage(u); + pa_log_debug("Thread starting up"); - do_write(u); - do_read(u); + if (u->core->realtime_scheduling) + pa_make_realtime(u->core->realtime_priority); - pa_gettimeofday(&ntv); - pa_timeval_add(&ntv, u->poll_timeout); + pa_thread_mq_install(&u->thread_mq); - a->time_restart(e, &ntv); -} + for (;;) { + int ret; + pa_bool_t need_timer = FALSE; -static void defer_cb(pa_mainloop_api*a, pa_defer_event *e, void *userdata) { - struct userdata *u = userdata; + if (u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state)) { + if (u->sink->thread_info.rewind_requested) + pa_sink_process_rewind(u->sink, 0); - assert(u); + do_write(u); + need_timer = TRUE; + } + if (u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state)) { + do_read(u); + need_timer = TRUE; + } - a->defer_enable(e, 0); + if (need_timer) + pa_rtpoll_set_timer_relative(u->rtpoll, u->poll_timeout); + else + pa_rtpoll_set_timer_disabled(u->rtpoll); + + /* Hmm, nothing to do. Let's sleep */ + if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) + goto fail; + + if (ret == 0) + goto finish; + } - do_write(u); - do_read(u); +fail: + /* If this was no regular exit from the loop we have to continue + * processing messages until we received PA_MESSAGE_SHUTDOWN */ + pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL); + pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN); + +finish: + pa_log_debug("Thread shutting down"); } static void CALLBACK chunk_done_cb(HWAVEOUT hwo, UINT msg, DWORD_PTR inst, DWORD param1, DWORD param2) { - struct userdata *u = (struct userdata *)inst; + struct userdata *u = (struct userdata*) inst; + if (msg == WOM_OPEN) + pa_log_debug("WaveOut subsystem opened."); + if (msg == WOM_CLOSE) + pa_log_debug("WaveOut subsystem closed."); if (msg != WOM_DONE) return; EnterCriticalSection(&u->crit); - u->free_ofrags++; - assert(u->free_ofrags <= u->fragments); - + pa_assert(u->free_ofrags <= u->fragments); LeaveCriticalSection(&u->crit); } static void CALLBACK chunk_ready_cb(HWAVEIN hwi, UINT msg, DWORD_PTR inst, DWORD param1, DWORD param2) { - struct userdata *u = (struct userdata *)inst; + struct userdata *u = (struct userdata*) inst; + if (msg == WIM_OPEN) + pa_log_debug("WaveIn subsystem opened."); + if (msg == WIM_CLOSE) + pa_log_debug("WaveIn subsystem closed."); if (msg != WIM_DATA) return; EnterCriticalSection(&u->crit); - u->free_ifrags++; - assert(u->free_ifrags <= u->fragments); - + pa_assert(u->free_ifrags <= u->fragments); LeaveCriticalSection(&u->crit); } -static pa_usec_t sink_get_latency_cb(pa_sink *s) { - struct userdata *u = s->userdata; +static pa_usec_t sink_get_latency(struct userdata *u) { uint32_t free_frags; MMTIME mmt; - assert(s && u && u->sink); + pa_assert(u); + pa_assert(u->sink); memset(&mmt, 0, sizeof(mmt)); mmt.wType = TIME_BYTES; if (waveOutGetPosition(u->hwo, &mmt, sizeof(mmt)) == MMSYSERR_NOERROR) - return pa_bytes_to_usec(u->written_bytes - mmt.u.cb, &s->sample_spec); + return pa_bytes_to_usec(u->written_bytes - mmt.u.cb, &u->sink->sample_spec); else { EnterCriticalSection(&u->crit); - free_frags = u->free_ofrags; - LeaveCriticalSection(&u->crit); - return pa_bytes_to_usec((u->fragments - free_frags) * u->fragment_size, - &s->sample_spec); + return pa_bytes_to_usec((u->fragments - free_frags) * u->fragment_size, &u->sink->sample_spec); } } -static pa_usec_t source_get_latency_cb(pa_source *s) { +static pa_usec_t source_get_latency(struct userdata *u) { pa_usec_t r = 0; - struct userdata *u = s->userdata; uint32_t free_frags; - assert(s && u && u->sink); + pa_assert(u); + pa_assert(u->source); EnterCriticalSection(&u->crit); - free_frags = u->free_ifrags; - LeaveCriticalSection(&u->crit); - r += pa_bytes_to_usec((free_frags + 1) * u->fragment_size, &s->sample_spec); + r += pa_bytes_to_usec((free_frags + 1) * u->fragment_size, &u->source->sample_spec); return r; } -static void notify_sink_cb(pa_sink *s) { - struct userdata *u = s->userdata; - assert(u); +static int process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { + struct userdata *u; - u->core->mainloop->defer_enable(u->defer, 1); -} + if (pa_sink_isinstance(o)) { + u = PA_SINK(o)->userdata; -static void notify_source_cb(pa_source *s) { - struct userdata *u = s->userdata; - assert(u); + switch (code) { - u->core->mainloop->defer_enable(u->defer, 1); + case PA_SINK_MESSAGE_GET_LATENCY: { + pa_usec_t r = 0; + if (u->hwo) + r = sink_get_latency(u); + *((pa_usec_t*) data) = r; + return 0; + } + + } + + return pa_sink_process_msg(o, code, data, offset, chunk); + } + + if (pa_source_isinstance(o)) { + u = PA_SOURCE(o)->userdata; + + switch (code) { + + case PA_SOURCE_MESSAGE_GET_LATENCY: { + pa_usec_t r = 0; + if (u->hwi) + r = source_get_latency(u); + *((pa_usec_t*) data) = r; + return 0; + } + + } + + return pa_source_process_msg(o, code, data, offset, chunk); + } + + return -1; } -static int sink_get_hw_volume_cb(pa_sink *s) { +static void sink_get_volume_cb(pa_sink *s) { struct userdata *u = s->userdata; + WAVEOUTCAPS caps; DWORD vol; pa_volume_t left, right; + if (waveOutGetDevCaps(u->hwo, &caps, sizeof(caps)) != MMSYSERR_NOERROR) + return; + if (!(caps.dwSupport & WAVECAPS_VOLUME)) + return; + if (waveOutGetVolume(u->hwo, &vol) != MMSYSERR_NOERROR) - return -1; + return; - left = (vol & 0xFFFF) * PA_VOLUME_NORM / WAVEOUT_MAX_VOLUME; - right = ((vol >> 16) & 0xFFFF) * PA_VOLUME_NORM / WAVEOUT_MAX_VOLUME; + left = PA_CLAMP_VOLUME((vol & 0xFFFF) * PA_VOLUME_NORM / WAVEOUT_MAX_VOLUME); + if (caps.dwSupport & WAVECAPS_LRVOLUME) + right = PA_CLAMP_VOLUME(((vol >> 16) & 0xFFFF) * PA_VOLUME_NORM / WAVEOUT_MAX_VOLUME); + else + right = left; /* Windows supports > 2 channels, except for volume control */ - if (s->hw_volume.channels > 2) - pa_cvolume_set(&s->hw_volume, s->hw_volume.channels, (left + right)/2); + if (s->real_volume.channels > 2) + pa_cvolume_set(&s->real_volume, s->real_volume.channels, (left + right)/2); - s->hw_volume.values[0] = left; - if (s->hw_volume.channels > 1) - s->hw_volume.values[1] = right; - - return 0; + s->real_volume.values[0] = left; + if (s->real_volume.channels > 1) + s->real_volume.values[1] = right; } -static int sink_set_hw_volume_cb(pa_sink *s) { +static void sink_set_volume_cb(pa_sink *s) { struct userdata *u = s->userdata; + WAVEOUTCAPS caps; DWORD vol; - vol = s->hw_volume.values[0] * WAVEOUT_MAX_VOLUME / PA_VOLUME_NORM; - if (s->hw_volume.channels > 1) - vol |= (s->hw_volume.values[0] * WAVEOUT_MAX_VOLUME / PA_VOLUME_NORM) << 16; + if (waveOutGetDevCaps(u->hwo, &caps, sizeof(caps)) != MMSYSERR_NOERROR) + return; + if (!(caps.dwSupport & WAVECAPS_VOLUME)) + return; - if (waveOutSetVolume(u->hwo, vol) != MMSYSERR_NOERROR) - return -1; + if (s->real_volume.channels == 2 && caps.dwSupport & WAVECAPS_LRVOLUME) { + vol = (s->real_volume.values[0] * WAVEOUT_MAX_VOLUME / PA_VOLUME_NORM) + | (s->real_volume.values[1] * WAVEOUT_MAX_VOLUME / PA_VOLUME_NORM) << 16; + } else { + vol = (pa_cvolume_avg(&(s->real_volume)) * WAVEOUT_MAX_VOLUME / PA_VOLUME_NORM) + | (pa_cvolume_avg(&(s->real_volume)) * WAVEOUT_MAX_VOLUME / PA_VOLUME_NORM) << 16; + } - return 0; + if (waveOutSetVolume(u->hwo, vol) != MMSYSERR_NOERROR) + return; } static int ss_to_waveformat(pa_sample_spec *ss, LPWAVEFORMATEX wf) { wf->wFormatTag = WAVE_FORMAT_PCM; if (ss->channels > 2) { - pa_log_error(__FILE__": ERROR: More than two channels not supported.\n"); + pa_log_error("More than two channels not supported."); return -1; } wf->nChannels = ss->channels; - switch (ss->rate) { - case 8000: - case 11025: - case 22005: - case 44100: - break; - default: - pa_log_error(__FILE__": ERROR: Unsupported sample rate.\n"); - return -1; - } - wf->nSamplesPerSec = ss->rate; if (ss->format == PA_SAMPLE_U8) @@ -402,7 +466,7 @@ static int ss_to_waveformat(pa_sample_spec *ss, LPWAVEFORMATEX wf) { else if (ss->format == PA_SAMPLE_S16NE) wf->wBitsPerSample = 16; else { - pa_log_error(__FILE__": ERROR: Unsupported sample format.\n"); + pa_log_error("Unsupported sample format, only u8 and s16 are supported."); return -1; } @@ -414,45 +478,86 @@ static int ss_to_waveformat(pa_sample_spec *ss, LPWAVEFORMATEX wf) { return 0; } -int pa__init(pa_core *c, pa_module*m) { +int pa__get_n_used(pa_module *m) { + struct userdata *u; + pa_assert(m); + pa_assert(m->userdata); + u = (struct userdata*) m->userdata; + + return (u->sink ? pa_sink_used_by(u->sink) : 0) + + (u->source ? pa_source_used_by(u->source) : 0); +} + +int pa__init(pa_module *m) { struct userdata *u = NULL; HWAVEOUT hwo = INVALID_HANDLE_VALUE; HWAVEIN hwi = INVALID_HANDLE_VALUE; WAVEFORMATEX wf; + WAVEOUTCAPS pwoc; + MMRESULT result; int nfrags, frag_size; - int record = 1, playback = 1; + pa_bool_t record = TRUE, playback = TRUE; + unsigned int device; pa_sample_spec ss; + pa_channel_map map; pa_modargs *ma = NULL; + const char *device_name = NULL; unsigned int i; - struct timeval tv; - assert(c && m); + pa_assert(m); + pa_assert(m->core); if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { - pa_log(__FILE__": failed to parse module arguments.\n"); + pa_log("failed to parse module arguments."); goto fail; } if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) { - pa_log(__FILE__": record= and playback= expect boolean argument.\n"); + pa_log("record= and playback= expect boolean argument."); goto fail; } if (!playback && !record) { - pa_log(__FILE__": neither playback nor record enabled for device.\n"); + pa_log("neither playback nor record enabled for device."); goto fail; } - nfrags = 20; - frag_size = 1024; + /* Set the device to be opened. If set device_name is used, + * else device if set and lastly WAVE_MAPPER is the default */ + device = WAVE_MAPPER; + if (pa_modargs_get_value_u32(ma, "device", &device) < 0) { + pa_log("failed to parse device argument"); + goto fail; + } + if ((device_name = pa_modargs_get_value(ma, "device_name", NULL)) != NULL) { + unsigned int num_devices = waveOutGetNumDevs(); + for (i = 0; i < num_devices; i++) { + if (waveOutGetDevCaps(i, &pwoc, sizeof(pwoc)) == MMSYSERR_NOERROR) + if (_stricmp(device_name, pwoc.szPname) == 0) + break; + } + if (i < num_devices) + device = i; + else { + pa_log("device not found: %s", device_name); + goto fail; + } + } + if (waveOutGetDevCaps(device, &pwoc, sizeof(pwoc)) == MMSYSERR_NOERROR) + device_name = pwoc.szPname; + else + device_name = "unknown"; + + nfrags = 5; + frag_size = 8192; if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) { - pa_log(__FILE__": failed to parse fragments arguments\n"); + pa_log("failed to parse fragments arguments"); goto fail; } - ss = c->default_sample_spec; - if (pa_modargs_get_sample_spec(ma, &ss) < 0) { - pa_log(__FILE__": failed to parse sample specification\n"); + ss = m->core->default_sample_spec; + if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_WAVEEX) < 0) { + pa_log("failed to parse sample specification"); goto fail; } @@ -462,48 +567,89 @@ int pa__init(pa_core *c, pa_module*m) { u = pa_xmalloc(sizeof(struct userdata)); if (record) { - if (waveInOpen(&hwi, WAVE_MAPPER, &wf, (DWORD_PTR)chunk_ready_cb, (DWORD_PTR)u, CALLBACK_FUNCTION) != MMSYSERR_NOERROR) + result = waveInOpen(&hwi, device, &wf, 0, 0, WAVE_FORMAT_DIRECT | WAVE_FORMAT_QUERY); + if (result != MMSYSERR_NOERROR) { + pa_log_warn("Sample spec not supported by WaveIn, falling back to default sample rate."); + ss.rate = wf.nSamplesPerSec = m->core->default_sample_spec.rate; + } + result = waveInOpen(&hwi, device, &wf, (DWORD_PTR) chunk_ready_cb, (DWORD_PTR) u, CALLBACK_FUNCTION); + if (result != MMSYSERR_NOERROR) { + char errortext[MAXERRORLENGTH]; + pa_log("Failed to open WaveIn."); + if (waveInGetErrorText(result, errortext, sizeof(errortext)) == MMSYSERR_NOERROR) + pa_log("Error: %s", errortext); goto fail; - if (waveInStart(hwi) != MMSYSERR_NOERROR) + } + if (waveInStart(hwi) != MMSYSERR_NOERROR) { + pa_log("failed to start waveIn"); goto fail; - pa_log_debug(__FILE__": Opened waveIn subsystem.\n"); + } } if (playback) { - if (waveOutOpen(&hwo, WAVE_MAPPER, &wf, (DWORD_PTR)chunk_done_cb, (DWORD_PTR)u, CALLBACK_FUNCTION) != MMSYSERR_NOERROR) + result = waveOutOpen(&hwo, device, &wf, 0, 0, WAVE_FORMAT_DIRECT | WAVE_FORMAT_QUERY); + if (result != MMSYSERR_NOERROR) { + pa_log_warn("Sample spec not supported by WaveOut, falling back to default sample rate."); + ss.rate = wf.nSamplesPerSec = m->core->default_sample_spec.rate; + } + result = waveOutOpen(&hwo, device, &wf, (DWORD_PTR) chunk_done_cb, (DWORD_PTR) u, CALLBACK_FUNCTION); + if (result != MMSYSERR_NOERROR) { + char errortext[MAXERRORLENGTH]; + pa_log("Failed to open WaveOut."); + if (waveOutGetErrorText(result, errortext, sizeof(errortext)) == MMSYSERR_NOERROR) + pa_log("Error: %s", errortext); goto fail; - pa_log_debug(__FILE__": Opened waveOut subsystem.\n"); + } } InitializeCriticalSection(&u->crit); if (hwi != INVALID_HANDLE_VALUE) { - u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, NULL); - assert(u->source); + char *description = pa_sprintf_malloc("WaveIn on %s", device_name); + pa_source_new_data data; + pa_source_new_data_init(&data); + data.driver = __FILE__; + data.module = m; + pa_source_new_data_set_sample_spec(&data, &ss); + pa_source_new_data_set_channel_map(&data, &map); + pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME)); + u->source = pa_source_new(m->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY); + pa_source_new_data_done(&data); + + pa_assert(u->source); u->source->userdata = u; - u->source->notify = notify_source_cb; - u->source->get_latency = source_get_latency_cb; - pa_source_set_owner(u->source, m); - u->source->description = pa_sprintf_malloc("Windows waveIn PCM"); + pa_source_set_description(u->source, description); + u->source->parent.process_msg = process_msg; + pa_xfree(description); } else u->source = NULL; if (hwo != INVALID_HANDLE_VALUE) { - u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL); - assert(u->sink); - u->sink->notify = notify_sink_cb; - u->sink->get_latency = sink_get_latency_cb; - u->sink->get_hw_volume = sink_get_hw_volume_cb; - u->sink->set_hw_volume = sink_set_hw_volume_cb; + char *description = pa_sprintf_malloc("WaveOut on %s", device_name); + pa_sink_new_data data; + pa_sink_new_data_init(&data); + data.driver = __FILE__; + data.module = m; + pa_sink_new_data_set_sample_spec(&data, &ss); + pa_sink_new_data_set_channel_map(&data, &map); + pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME)); + u->sink = pa_sink_new(m->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY); + pa_sink_new_data_done(&data); + + pa_assert(u->sink); + pa_sink_set_get_volume_callback(u->sink, sink_get_volume_cb); + pa_sink_set_set_volume_callback(u->sink, sink_set_volume_cb); u->sink->userdata = u; - pa_sink_set_owner(u->sink, m); - u->sink->description = pa_sprintf_malloc("Windows waveOut PCM"); + pa_sink_set_description(u->sink, description); + u->sink->parent.process_msg = process_msg; + pa_xfree(description); } else u->sink = NULL; - assert(u->source || u->sink); + pa_assert(u->source || u->sink); + pa_modargs_free(ma); - u->core = c; + u->core = m->core; u->hwi = hwi; u->hwo = hwo; @@ -513,90 +659,94 @@ int pa__init(pa_core *c, pa_module*m) { u->fragment_size = frag_size - (frag_size % pa_frame_size(&ss)); u->written_bytes = 0; + u->sink_underflow = 1; - u->oremain = u->fragment_size; - - u->poll_timeout = pa_bytes_to_usec(u->fragments * u->fragment_size / 3, &ss); - - pa_gettimeofday(&tv); - pa_timeval_add(&tv, u->poll_timeout); - - u->event = c->mainloop->time_new(c->mainloop, &tv, poll_cb, u); - assert(u->event); - - u->defer = c->mainloop->defer_new(c->mainloop, defer_cb, u); - assert(u->defer); - c->mainloop->defer_enable(u->defer, 0); + u->poll_timeout = pa_bytes_to_usec(u->fragments * u->fragment_size / 10, &ss); + pa_log_debug("Poll timeout = %.1f ms", (double) u->poll_timeout / PA_USEC_PER_MSEC); u->cur_ihdr = 0; u->cur_ohdr = 0; u->ihdrs = pa_xmalloc0(sizeof(WAVEHDR) * u->fragments); - assert(u->ihdrs); + pa_assert(u->ihdrs); u->ohdrs = pa_xmalloc0(sizeof(WAVEHDR) * u->fragments); - assert(u->ohdrs); - for (i = 0;i < u->fragments;i++) { + pa_assert(u->ohdrs); + for (i = 0; i < u->fragments; i++) { u->ihdrs[i].dwBufferLength = u->fragment_size; u->ohdrs[i].dwBufferLength = u->fragment_size; u->ihdrs[i].lpData = pa_xmalloc(u->fragment_size); - assert(u->ihdrs); + pa_assert(u->ihdrs); u->ohdrs[i].lpData = pa_xmalloc(u->fragment_size); - assert(u->ohdrs); + pa_assert(u->ohdrs); } - - u->silence.length = u->fragment_size; - u->silence.memblock = pa_memblock_new(u->silence.length, u->core->memblock_stat); - assert(u->silence.memblock); - pa_silence_memblock(u->silence.memblock, &ss); - u->silence.index = 0; u->module = m; m->userdata = u; - pa_modargs_free(ma); + /* Read mixer settings */ + if (u->sink) + sink_get_volume_cb(u->sink); - return 0; + u->rtpoll = pa_rtpoll_new(); + pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll); -fail: - if (hwi != INVALID_HANDLE_VALUE) - waveInClose(hwi); + if (u->sink) { + pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); + pa_sink_set_rtpoll(u->sink, u->rtpoll); + } + if (u->source) { + pa_source_set_asyncmsgq(u->source, u->thread_mq.inq); + pa_source_set_rtpoll(u->source, u->rtpoll); + } + + if (!(u->thread = pa_thread_new("waveout", thread_func, u))) { + pa_log("Failed to create thread."); + goto fail; + } - if (hwo != INVALID_HANDLE_VALUE) - waveOutClose(hwo); + if (u->sink) + pa_sink_put(u->sink); + if (u->source) + pa_source_put(u->source); - if (u) - pa_xfree(u); + return 0; +fail: if (ma) pa_modargs_free(ma); - + + pa__done(m); + return -1; } -void pa__done(pa_core *c, pa_module*m) { +void pa__done(pa_module *m) { struct userdata *u; unsigned int i; - assert(c && m); + pa_assert(m); + pa_assert(m->core); if (!(u = m->userdata)) return; - - if (u->event) - c->mainloop->time_free(u->event); - if (u->defer) - c->mainloop->defer_free(u->defer); + if (u->sink) + pa_sink_unlink(u->sink); + if (u->source) + pa_source_unlink(u->source); - if (u->sink) { - pa_sink_disconnect(u->sink); + pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL); + if (u->thread) + pa_thread_free(u->thread); + pa_thread_mq_done(&u->thread_mq); + + if (u->sink) pa_sink_unref(u->sink); - } - - if (u->source) { - pa_source_disconnect(u->source); + if (u->source) pa_source_unref(u->source); - } - + + if (u->rtpoll) + pa_rtpoll_free(u->rtpoll); + if (u->hwi != INVALID_HANDLE_VALUE) { waveInReset(u->hwi); waveInClose(u->hwi); @@ -607,7 +757,7 @@ void pa__done(pa_core *c, pa_module*m) { waveOutClose(u->hwo); } - for (i = 0;i < u->fragments;i++) { + for (i = 0; i < u->fragments; i++) { pa_xfree(u->ihdrs[i].lpData); pa_xfree(u->ohdrs[i].lpData); } @@ -616,6 +766,6 @@ void pa__done(pa_core *c, pa_module*m) { pa_xfree(u->ohdrs); DeleteCriticalSection(&u->crit); - + pa_xfree(u); }