From 9ede4c5f3c5b72e8be2ee2e58ffdf36af019b607 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 22 Feb 2013 20:29:03 +0100 Subject: {output,mixer}/winmm: convert to C++ --- Makefile.am | 4 +- src/OutputList.cxx | 2 +- src/mixer/WinmmMixerPlugin.cxx | 119 +++++++++++++ src/mixer/winmm_mixer_plugin.c | 116 ------------- src/output/WinmmOutputPlugin.cxx | 359 +++++++++++++++++++++++++++++++++++++++ src/output/WinmmOutputPlugin.hxx | 42 +++++ src/output/winmm_output_plugin.c | 355 -------------------------------------- src/output/winmm_output_plugin.h | 38 ----- 8 files changed, 523 insertions(+), 512 deletions(-) create mode 100644 src/mixer/WinmmMixerPlugin.cxx delete mode 100644 src/mixer/winmm_mixer_plugin.c create mode 100644 src/output/WinmmOutputPlugin.cxx create mode 100644 src/output/WinmmOutputPlugin.hxx delete mode 100644 src/output/winmm_output_plugin.c delete mode 100644 src/output/winmm_output_plugin.h diff --git a/Makefile.am b/Makefile.am index fd4ff377..71d2c5ec 100644 --- a/Makefile.am +++ b/Makefile.am @@ -903,8 +903,8 @@ endif if ENABLE_WINMM_OUTPUT liboutput_plugins_a_SOURCES += \ - src/output/winmm_output_plugin.c src/output/winmm_output_plugin.h -libmixer_plugins_a_SOURCES += src/mixer/winmm_mixer_plugin.c + src/output/WinmmOutputPlugin.cxx src/output/WinmmOutputPlugin.hxx +libmixer_plugins_a_SOURCES += src/mixer/WinmmMixerPlugin.cxx endif diff --git a/src/OutputList.cxx b/src/OutputList.cxx index 72f241bd..dc4e2319 100644 --- a/src/OutputList.cxx +++ b/src/OutputList.cxx @@ -37,7 +37,7 @@ #include "output/RoarOutputPlugin.hxx" #include "output/shout_output_plugin.h" #include "output/solaris_output_plugin.h" -#include "output/winmm_output_plugin.h" +#include "output/WinmmOutputPlugin.hxx" const struct audio_output_plugin *const audio_output_plugins[] = { #ifdef HAVE_SHOUT diff --git a/src/mixer/WinmmMixerPlugin.cxx b/src/mixer/WinmmMixerPlugin.cxx new file mode 100644 index 00000000..e3b7948e --- /dev/null +++ b/src/mixer/WinmmMixerPlugin.cxx @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2003-2013 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 "mixer_api.h" +#include "output_api.h" +#include "output/WinmmOutputPlugin.hxx" + +#include + +#include +#include +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "winmm_mixer" + +struct winmm_mixer { + struct mixer base; + WinmmOutput *output; +}; + +static inline GQuark +winmm_mixer_quark(void) +{ + return g_quark_from_static_string("winmm_mixer"); +} + +static inline int +winmm_volume_decode(DWORD volume) +{ + return lround((volume & 0xFFFF) / 655.35); +} + +static inline DWORD +winmm_volume_encode(int volume) +{ + int value = lround(volume * 655.35); + return MAKELONG(value, value); +} + +static struct mixer * +winmm_mixer_init(void *ao, G_GNUC_UNUSED const struct config_param *param, + G_GNUC_UNUSED GError **error_r) +{ + assert(ao != nullptr); + + struct winmm_mixer *wm = g_new(struct winmm_mixer, 1); + mixer_init(&wm->base, &winmm_mixer_plugin); + wm->output = (WinmmOutput *) ao; + + return &wm->base; +} + +static void +winmm_mixer_finish(struct mixer *data) +{ + g_free(data); +} + +static int +winmm_mixer_get_volume(struct mixer *mixer, GError **error_r) +{ + struct winmm_mixer *wm = (struct winmm_mixer *) mixer; + DWORD volume; + HWAVEOUT handle = winmm_output_get_handle(wm->output); + MMRESULT result = waveOutGetVolume(handle, &volume); + + if (result != MMSYSERR_NOERROR) { + g_set_error(error_r, 0, winmm_mixer_quark(), + "Failed to get winmm volume"); + return -1; + } + + return winmm_volume_decode(volume); +} + +static bool +winmm_mixer_set_volume(struct mixer *mixer, unsigned volume, GError **error_r) +{ + struct winmm_mixer *wm = (struct winmm_mixer *) mixer; + DWORD value = winmm_volume_encode(volume); + HWAVEOUT handle = winmm_output_get_handle(wm->output); + MMRESULT result = waveOutSetVolume(handle, value); + + if (result != MMSYSERR_NOERROR) { + g_set_error(error_r, 0, winmm_mixer_quark(), + "Failed to set winmm volume"); + return false; + } + + return true; +} + +const struct mixer_plugin winmm_mixer_plugin = { + winmm_mixer_init, + winmm_mixer_finish, + nullptr, + nullptr, + winmm_mixer_get_volume, + winmm_mixer_set_volume, + false, +}; diff --git a/src/mixer/winmm_mixer_plugin.c b/src/mixer/winmm_mixer_plugin.c deleted file mode 100644 index 99da60cc..00000000 --- a/src/mixer/winmm_mixer_plugin.c +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (C) 2003-2011 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 "mixer_api.h" -#include "output_api.h" -#include "output/winmm_output_plugin.h" - -#include - -#include -#include -#include - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "winmm_mixer" - -struct winmm_mixer { - struct mixer base; - struct winmm_output *output; -}; - -static inline GQuark -winmm_mixer_quark(void) -{ - return g_quark_from_static_string("winmm_mixer"); -} - -static inline int -winmm_volume_decode(DWORD volume) -{ - return lround((volume & 0xFFFF) / 655.35); -} - -static inline DWORD -winmm_volume_encode(int volume) -{ - int value = lround(volume * 655.35); - return MAKELONG(value, value); -} - -static struct mixer * -winmm_mixer_init(void *ao, G_GNUC_UNUSED const struct config_param *param, - G_GNUC_UNUSED GError **error_r) -{ - assert(ao != NULL); - - struct winmm_mixer *wm = g_new(struct winmm_mixer, 1); - mixer_init(&wm->base, &winmm_mixer_plugin); - wm->output = (struct winmm_output *) ao; - - return &wm->base; -} - -static void -winmm_mixer_finish(struct mixer *data) -{ - g_free(data); -} - -static int -winmm_mixer_get_volume(struct mixer *mixer, GError **error_r) -{ - struct winmm_mixer *wm = (struct winmm_mixer *) mixer; - DWORD volume; - HWAVEOUT handle = winmm_output_get_handle(wm->output); - MMRESULT result = waveOutGetVolume(handle, &volume); - - if (result != MMSYSERR_NOERROR) { - g_set_error(error_r, 0, winmm_mixer_quark(), - "Failed to get winmm volume"); - return -1; - } - - return winmm_volume_decode(volume); -} - -static bool -winmm_mixer_set_volume(struct mixer *mixer, unsigned volume, GError **error_r) -{ - struct winmm_mixer *wm = (struct winmm_mixer *) mixer; - DWORD value = winmm_volume_encode(volume); - HWAVEOUT handle = winmm_output_get_handle(wm->output); - MMRESULT result = waveOutSetVolume(handle, value); - - if (result != MMSYSERR_NOERROR) { - g_set_error(error_r, 0, winmm_mixer_quark(), - "Failed to set winmm volume"); - return false; - } - - return true; -} - -const struct mixer_plugin winmm_mixer_plugin = { - .init = winmm_mixer_init, - .finish = winmm_mixer_finish, - .get_volume = winmm_mixer_get_volume, - .set_volume = winmm_mixer_set_volume, -}; diff --git a/src/output/WinmmOutputPlugin.cxx b/src/output/WinmmOutputPlugin.cxx new file mode 100644 index 00000000..7944e2bd --- /dev/null +++ b/src/output/WinmmOutputPlugin.cxx @@ -0,0 +1,359 @@ +/* + * Copyright (C) 2003-2013 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 "WinmmOutputPlugin.hxx" +#include "output_api.h" +#include "pcm_buffer.h" +#include "mixer_list.h" + +#include +#include + +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "winmm_output" + +struct WinmmBuffer { + struct pcm_buffer buffer; + + WAVEHDR hdr; +}; + +struct WinmmOutput { + struct audio_output base; + + UINT device_id; + HWAVEOUT handle; + + /** + * This event is triggered by Windows when a buffer is + * finished. + */ + HANDLE event; + + WinmmBuffer 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(WinmmOutput *output) +{ + return output->handle; +} + +static bool +winmm_output_test_default_device(void) +{ + return waveOutGetNumDevs() > 0; +} + +static bool +get_device_id(const char *device_name, UINT *device_id, GError **error_r) +{ + /* if device is not specified use wave mapper */ + if (device_name == nullptr) { + *device_id = WAVE_MAPPER; + return true; + } + + UINT numdevs = waveOutGetNumDevs(); + + /* check for device id */ + char *endptr; + UINT id = strtoul(device_name, &endptr, 0); + if (endptr > device_name && *endptr == 0) { + if (id >= numdevs) + goto fail; + *device_id = id; + return true; + } + + /* check for device name */ + for (UINT i = 0; i < numdevs; i++) { + WAVEOUTCAPS caps; + MMRESULT result = waveOutGetDevCaps(i, &caps, sizeof(caps)); + if (result != MMSYSERR_NOERROR) + continue; + /* szPname is only 32 chars long, so it is often truncated. + Use partial match to work around this. */ + if (strstr(device_name, caps.szPname) == device_name) { + *device_id = i; + return true; + } + } + +fail: + g_set_error(error_r, winmm_output_quark(), 0, + "device \"%s\" is not found", device_name); + return false; +} + +static struct audio_output * +winmm_output_init(const struct config_param *param, GError **error_r) +{ + WinmmOutput *wo = new WinmmOutput(); + if (!ao_base_init(&wo->base, &winmm_output_plugin, param, error_r)) { + g_free(wo); + return nullptr; + } + + const char *device = config_get_block_string(param, "device", nullptr); + if (!get_device_id(device, &wo->device_id, error_r)) { + ao_base_finish(&wo->base); + g_free(wo); + return nullptr; + } + + return &wo->base; +} + +static void +winmm_output_finish(struct audio_output *ao) +{ + WinmmOutput *wo = (WinmmOutput *)ao; + + ao_base_finish(&wo->base); + delete wo; +} + +static bool +winmm_output_open(struct audio_output *ao, struct audio_format *audio_format, + GError **error_r) +{ + WinmmOutput *wo = (WinmmOutput *)ao; + + wo->event = CreateEvent(nullptr, false, false, nullptr); + if (wo->event == nullptr) { + 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_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, wo->device_id, &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(struct audio_output *ao) +{ + WinmmOutput *wo = (WinmmOutput *)ao; + + 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(WinmmOutput *wo, WinmmBuffer *buffer, + const void *data, size_t size, + GError **error_r) +{ + void *dest = pcm_buffer_get(&buffer->buffer, size); + assert(dest != nullptr); + + memcpy(dest, data, size); + + memset(&buffer->hdr, 0, sizeof(buffer->hdr)); + buffer->hdr.lpData = (LPSTR)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(WinmmOutput *wo, WinmmBuffer *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(struct audio_output *ao, const void *chunk, size_t size, GError **error_r) +{ + WinmmOutput *wo = (WinmmOutput *)ao; + + /* get the next buffer from the ring and prepare it */ + WinmmBuffer *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(WinmmOutput *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(WinmmOutput *wo) +{ + waveOutReset(wo->handle); + + for (unsigned i = 0; i < G_N_ELEMENTS(wo->buffers); ++i) { + WinmmBuffer *buffer = &wo->buffers[i]; + waveOutUnprepareHeader(wo->handle, &buffer->hdr, + sizeof(buffer->hdr)); + } +} + +static void +winmm_output_drain(struct audio_output *ao) +{ + WinmmOutput *wo = (WinmmOutput *)ao; + + if (!winmm_drain_all_buffers(wo, nullptr)) + winmm_stop(wo); +} + +static void +winmm_output_cancel(struct audio_output *ao) +{ + WinmmOutput *wo = (WinmmOutput *)ao; + + winmm_stop(wo); +} + +const struct audio_output_plugin winmm_output_plugin = { + "winmm", + winmm_output_test_default_device, + winmm_output_init, + winmm_output_finish, + nullptr, + nullptr, + winmm_output_open, + winmm_output_close, + nullptr, + nullptr, + winmm_output_play, + winmm_output_drain, + winmm_output_cancel, + nullptr, + &winmm_mixer_plugin, +}; diff --git a/src/output/WinmmOutputPlugin.hxx b/src/output/WinmmOutputPlugin.hxx new file mode 100644 index 00000000..e8688782 --- /dev/null +++ b/src/output/WinmmOutputPlugin.hxx @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2003-2013 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. + */ + +#ifndef MPD_WINMM_OUTPUT_PLUGIN_HXX +#define MPD_WINMM_OUTPUT_PLUGIN_HXX + +#include "check.h" + +#ifdef ENABLE_WINMM_OUTPUT + +#include "gcc.h" + +#include +#include + +struct WinmmOutput; + +extern const struct audio_output_plugin winmm_output_plugin; + +gcc_pure +HWAVEOUT +winmm_output_get_handle(WinmmOutput *); + +#endif + +#endif diff --git a/src/output/winmm_output_plugin.c b/src/output/winmm_output_plugin.c deleted file mode 100644 index c1b3af12..00000000 --- a/src/output/winmm_output_plugin.c +++ /dev/null @@ -1,355 +0,0 @@ -/* - * Copyright (C) 2003-2011 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 "winmm_output_plugin.h" -#include "output_api.h" -#include "pcm_buffer.h" -#include "mixer_list.h" -#include "winmm_output_plugin.h" - -#include -#include - -#undef G_LOG_DOMAIN -#define G_LOG_DOMAIN "winmm_output" - -struct winmm_buffer { - struct pcm_buffer buffer; - - WAVEHDR hdr; -}; - -struct winmm_output { - struct audio_output base; - - UINT device_id; - 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) -{ - return waveOutGetNumDevs() > 0; -} - -static bool -get_device_id(const char *device_name, UINT *device_id, GError **error_r) -{ - /* if device is not specified use wave mapper */ - if (device_name == NULL) { - *device_id = WAVE_MAPPER; - return true; - } - - UINT numdevs = waveOutGetNumDevs(); - - /* check for device id */ - char *endptr; - UINT id = strtoul(device_name, &endptr, 0); - if (endptr > device_name && *endptr == 0) { - if (id >= numdevs) - goto fail; - *device_id = id; - return true; - } - - /* check for device name */ - for (UINT i = 0; i < numdevs; i++) { - WAVEOUTCAPS caps; - MMRESULT result = waveOutGetDevCaps(i, &caps, sizeof(caps)); - if (result != MMSYSERR_NOERROR) - continue; - /* szPname is only 32 chars long, so it is often truncated. - Use partial match to work around this. */ - if (strstr(device_name, caps.szPname) == device_name) { - *device_id = i; - return true; - } - } - -fail: - g_set_error(error_r, winmm_output_quark(), 0, - "device \"%s\" is not found", device_name); - return false; -} - -static struct audio_output * -winmm_output_init(const struct config_param *param, GError **error_r) -{ - struct winmm_output *wo = g_new(struct winmm_output, 1); - if (!ao_base_init(&wo->base, &winmm_output_plugin, param, error_r)) { - g_free(wo); - return NULL; - } - - const char *device = config_get_block_string(param, "device", NULL); - if (!get_device_id(device, &wo->device_id, error_r)) { - ao_base_finish(&wo->base); - g_free(wo); - return NULL; - } - - return &wo->base; -} - -static void -winmm_output_finish(struct audio_output *ao) -{ - struct winmm_output *wo = (struct winmm_output *)ao; - - ao_base_finish(&wo->base); - g_free(wo); -} - -static bool -winmm_output_open(struct audio_output *ao, struct audio_format *audio_format, - GError **error_r) -{ - struct winmm_output *wo = (struct winmm_output *)ao; - - 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_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, wo->device_id, &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(struct audio_output *ao) -{ - struct winmm_output *wo = (struct winmm_output *)ao; - - 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); - assert(dest != NULL); - - 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(struct audio_output *ao, const void *chunk, size_t size, GError **error_r) -{ - struct winmm_output *wo = (struct winmm_output *)ao; - - /* 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(struct audio_output *ao) -{ - struct winmm_output *wo = (struct winmm_output *)ao; - - if (!winmm_drain_all_buffers(wo, NULL)) - winmm_stop(wo); -} - -static void -winmm_output_cancel(struct audio_output *ao) -{ - struct winmm_output *wo = (struct winmm_output *)ao; - - 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, -}; diff --git a/src/output/winmm_output_plugin.h b/src/output/winmm_output_plugin.h deleted file mode 100644 index 36435648..00000000 --- a/src/output/winmm_output_plugin.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2003-2011 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. - */ - -#ifndef MPD_WINMM_OUTPUT_PLUGIN_H -#define MPD_WINMM_OUTPUT_PLUGIN_H - -#include "check.h" - -#ifdef ENABLE_WINMM_OUTPUT - -#include -#include - -struct winmm_output; - -extern const struct audio_output_plugin winmm_output_plugin; - -HWAVEOUT winmm_output_get_handle(struct winmm_output*); - -#endif - -#endif -- cgit v1.2.3