2010-05-18 22:56:42 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2010 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 "output_api.h"
|
|
|
|
#include "pcm_buffer.h"
|
2010-10-08 22:45:08 +02:00
|
|
|
#include "mixer_list.h"
|
|
|
|
#include "winmm_output_plugin.h"
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2010-11-03 20:51:18 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-05-18 22:56:42 +02:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#undef G_LOG_DOMAIN
|
2010-10-08 19:55:14 +02:00
|
|
|
#define G_LOG_DOMAIN "winmm_output"
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2010-10-08 19:55:14 +02:00
|
|
|
struct winmm_buffer {
|
2010-05-18 22:56:42 +02:00
|
|
|
struct pcm_buffer buffer;
|
|
|
|
|
|
|
|
WAVEHDR hdr;
|
|
|
|
};
|
|
|
|
|
2010-10-08 19:55:14 +02:00
|
|
|
struct winmm_output {
|
2010-11-03 20:51:18 +01:00
|
|
|
UINT device_id;
|
2010-05-18 22:56:42 +02:00
|
|
|
HWAVEOUT handle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This event is triggered by Windows when a buffer is
|
|
|
|
* finished.
|
|
|
|
*/
|
|
|
|
HANDLE event;
|
|
|
|
|
2010-10-08 19:55:14 +02:00
|
|
|
struct winmm_buffer buffers[8];
|
2010-05-18 22:56:42 +02:00
|
|
|
unsigned next_buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The quark used for GError.domain.
|
|
|
|
*/
|
|
|
|
static inline GQuark
|
2010-10-08 19:55:14 +02:00
|
|
|
winmm_output_quark(void)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2010-10-08 19:55:14 +02:00
|
|
|
return g_quark_from_static_string("winmm_output");
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
|
2010-10-08 22:45:08 +02:00
|
|
|
HWAVEOUT
|
|
|
|
winmm_output_get_handle(struct winmm_output* output)
|
|
|
|
{
|
|
|
|
return output->handle;
|
|
|
|
}
|
|
|
|
|
2010-05-18 22:56:42 +02:00
|
|
|
static bool
|
2010-10-08 19:55:14 +02:00
|
|
|
winmm_output_test_default_device(void)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2010-11-03 19:26:19 +01:00
|
|
|
return waveOutGetNumDevs() > 0;
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
|
2010-11-03 20:51:18 +01:00
|
|
|
static UINT
|
|
|
|
get_device_id(const char *device_name)
|
|
|
|
{
|
|
|
|
/* if device is not specified use wave mapper */
|
|
|
|
if (device_name == NULL)
|
|
|
|
return WAVE_MAPPER;
|
|
|
|
|
|
|
|
/* check for device id */
|
|
|
|
char *endptr;
|
|
|
|
UINT id = strtoul(device_name, &endptr, 0);
|
|
|
|
if (*endptr == 0)
|
|
|
|
return id;
|
|
|
|
|
|
|
|
/* check for device name */
|
|
|
|
for (UINT i = 0; i < waveOutGetNumDevs(); 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)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fallback to wave mapper */
|
|
|
|
return WAVE_MAPPER;
|
|
|
|
}
|
|
|
|
|
2010-05-18 22:56:42 +02:00
|
|
|
static void *
|
2010-10-08 19:55:14 +02:00
|
|
|
winmm_output_init(G_GNUC_UNUSED const struct audio_format *audio_format,
|
2010-05-18 22:56:42 +02:00
|
|
|
G_GNUC_UNUSED const struct config_param *param,
|
|
|
|
G_GNUC_UNUSED GError **error)
|
|
|
|
{
|
2010-10-08 19:55:14 +02:00
|
|
|
struct winmm_output *wo = g_new(struct winmm_output, 1);
|
2010-11-03 20:51:18 +01:00
|
|
|
const char *device = config_get_block_string(param, "device", NULL);
|
|
|
|
wo->device_id = get_device_id(device);
|
2010-05-18 22:56:42 +02:00
|
|
|
return wo;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-10-08 19:55:14 +02:00
|
|
|
winmm_output_finish(void *data)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2010-10-08 19:55:14 +02:00
|
|
|
struct winmm_output *wo = data;
|
2010-05-18 22:56:42 +02:00
|
|
|
|
|
|
|
g_free(wo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2010-10-08 19:55:14 +02:00
|
|
|
winmm_output_open(void *data, struct audio_format *audio_format,
|
2010-05-18 22:56:42 +02:00
|
|
|
GError **error_r)
|
|
|
|
{
|
2010-10-08 19:55:14 +02:00
|
|
|
struct winmm_output *wo = data;
|
2010-05-18 22:56:42 +02:00
|
|
|
|
|
|
|
wo->event = CreateEvent(NULL, false, false, NULL);
|
|
|
|
if (wo->event == NULL) {
|
2010-10-08 19:55:14 +02:00
|
|
|
g_set_error(error_r, winmm_output_quark(), 0,
|
2010-05-18 22:56:42 +02:00
|
|
|
"CreateEvent() failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (audio_format->format) {
|
|
|
|
case SAMPLE_FORMAT_S8:
|
|
|
|
case SAMPLE_FORMAT_S16:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAMPLE_FORMAT_S24:
|
|
|
|
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;
|
|
|
|
|
2010-11-03 20:51:18 +01:00
|
|
|
MMRESULT result = waveOutOpen(&wo->handle, wo->device_id, &format,
|
2010-05-18 22:56:42 +02:00
|
|
|
(DWORD_PTR)wo->event, 0, CALLBACK_EVENT);
|
|
|
|
if (result != MMSYSERR_NOERROR) {
|
|
|
|
CloseHandle(wo->event);
|
2010-10-08 19:55:14 +02:00
|
|
|
g_set_error(error_r, winmm_output_quark(), result,
|
2010-05-18 22:56:42 +02:00
|
|
|
"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
|
2010-10-08 19:55:14 +02:00
|
|
|
winmm_output_close(void *data)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2010-10-08 19:55:14 +02:00
|
|
|
struct winmm_output *wo = data;
|
2010-05-18 22:56:42 +02:00
|
|
|
|
|
|
|
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
|
2010-10-08 19:55:14 +02:00
|
|
|
winmm_set_buffer(struct winmm_output *wo, struct winmm_buffer *buffer,
|
2010-05-18 22:56:42 +02:00
|
|
|
const void *data, size_t size,
|
|
|
|
GError **error_r)
|
|
|
|
{
|
|
|
|
void *dest = pcm_buffer_get(&buffer->buffer, size);
|
|
|
|
if (dest == NULL) {
|
2010-10-08 19:55:14 +02:00
|
|
|
g_set_error(error_r, winmm_output_quark(), 0,
|
2010-05-18 22:56:42 +02:00
|
|
|
"Out of memory");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2010-10-08 19:55:14 +02:00
|
|
|
g_set_error(error_r, winmm_output_quark(), result,
|
2010-05-18 22:56:42 +02:00
|
|
|
"waveOutPrepareHeader() failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait until the buffer is finished.
|
|
|
|
*/
|
|
|
|
static bool
|
2010-10-08 19:55:14 +02:00
|
|
|
winmm_drain_buffer(struct winmm_output *wo, struct winmm_buffer *buffer,
|
2010-05-18 22:56:42 +02:00
|
|
|
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) {
|
2010-10-08 19:55:14 +02:00
|
|
|
g_set_error(error_r, winmm_output_quark(), result,
|
2010-05-18 22:56:42 +02:00
|
|
|
"waveOutUnprepareHeader() failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait some more */
|
|
|
|
WaitForSingleObject(wo->event, INFINITE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2010-10-08 19:55:14 +02:00
|
|
|
winmm_output_play(void *data, const void *chunk, size_t size, GError **error_r)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2010-10-08 19:55:14 +02:00
|
|
|
struct winmm_output *wo = data;
|
2010-05-18 22:56:42 +02:00
|
|
|
|
|
|
|
/* get the next buffer from the ring and prepare it */
|
2010-10-08 19:55:14 +02:00
|
|
|
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))
|
2010-05-18 22:56:42 +02:00
|
|
|
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));
|
2010-10-08 19:55:14 +02:00
|
|
|
g_set_error(error_r, winmm_output_quark(), result,
|
2010-05-18 22:56:42 +02:00
|
|
|
"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
|
2010-10-08 19:55:14 +02:00
|
|
|
winmm_drain_all_buffers(struct winmm_output *wo, GError **error_r)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
|
|
|
for (unsigned i = wo->next_buffer; i < G_N_ELEMENTS(wo->buffers); ++i)
|
2010-10-08 19:55:14 +02:00
|
|
|
if (!winmm_drain_buffer(wo, &wo->buffers[i], error_r))
|
2010-05-18 22:56:42 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < wo->next_buffer; ++i)
|
2010-10-08 19:55:14 +02:00
|
|
|
if (!winmm_drain_buffer(wo, &wo->buffers[i], error_r))
|
2010-05-18 22:56:42 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-10-08 19:55:14 +02:00
|
|
|
winmm_stop(struct winmm_output *wo)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
|
|
|
waveOutReset(wo->handle);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < G_N_ELEMENTS(wo->buffers); ++i) {
|
2010-10-08 19:55:14 +02:00
|
|
|
struct winmm_buffer *buffer = &wo->buffers[i];
|
2010-05-18 22:56:42 +02:00
|
|
|
waveOutUnprepareHeader(wo->handle, &buffer->hdr,
|
|
|
|
sizeof(buffer->hdr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-10-08 19:55:14 +02:00
|
|
|
winmm_output_drain(void *data)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2010-10-08 19:55:14 +02:00
|
|
|
struct winmm_output *wo = data;
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2010-10-08 19:55:14 +02:00
|
|
|
if (!winmm_drain_all_buffers(wo, NULL))
|
|
|
|
winmm_stop(wo);
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-10-08 19:55:14 +02:00
|
|
|
winmm_output_cancel(void *data)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2010-10-08 19:55:14 +02:00
|
|
|
struct winmm_output *wo = data;
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2010-10-08 19:55:14 +02:00
|
|
|
winmm_stop(wo);
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
|
2010-10-08 19:55:14 +02:00
|
|
|
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,
|
2010-10-08 22:45:08 +02:00
|
|
|
.mixer_plugin = &winmm_mixer_plugin,
|
2010-05-18 22:56:42 +02:00
|
|
|
};
|