2010-05-18 22:56:42 +02:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
2010-05-18 22:56:42 +02:00
|
|
|
* 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"
|
2013-02-22 20:29:03 +01:00
|
|
|
#include "WinmmOutputPlugin.hxx"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "../OutputAPI.hxx"
|
2013-07-31 00:57:52 +02:00
|
|
|
#include "pcm/PcmBuffer.hxx"
|
2014-01-24 16:25:21 +01:00
|
|
|
#include "mixer/MixerList.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2013-10-15 22:04:17 +02:00
|
|
|
#include "util/Macros.hxx"
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2013-09-27 22:56:30 +02:00
|
|
|
#include <glib.h>
|
|
|
|
|
2010-11-03 20:51:18 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2013-02-22 20:29:03 +01:00
|
|
|
struct WinmmBuffer {
|
2013-07-31 00:57:52 +02:00
|
|
|
PcmBuffer buffer;
|
2010-05-18 22:56:42 +02:00
|
|
|
|
|
|
|
WAVEHDR hdr;
|
|
|
|
};
|
|
|
|
|
2013-02-22 20:29:03 +01:00
|
|
|
struct WinmmOutput {
|
2014-01-28 11:34:09 +01:00
|
|
|
AudioOutput base;
|
2011-09-16 23:31:48 +02:00
|
|
|
|
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;
|
|
|
|
|
2013-02-22 20:29:03 +01:00
|
|
|
WinmmBuffer buffers[8];
|
2010-05-18 22:56:42 +02:00
|
|
|
unsigned next_buffer;
|
2014-01-28 23:39:48 +01:00
|
|
|
|
2014-01-28 11:39:12 +01:00
|
|
|
WinmmOutput()
|
2014-01-28 23:39:48 +01:00
|
|
|
:base(winmm_output_plugin) {}
|
2010-05-18 22:56:42 +02:00
|
|
|
};
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain winmm_output_domain("winmm_output");
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2010-10-08 22:45:08 +02:00
|
|
|
HWAVEOUT
|
2013-02-22 20:29:03 +01:00
|
|
|
winmm_output_get_handle(WinmmOutput *output)
|
2010-10-08 22:45:08 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-10-23 08:59:31 +02:00
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
get_device_id(const char *device_name, UINT *device_id, Error &error)
|
2010-11-03 20:51:18 +01:00
|
|
|
{
|
|
|
|
/* if device is not specified use wave mapper */
|
2013-02-22 20:29:03 +01:00
|
|
|
if (device_name == nullptr) {
|
2011-10-23 08:59:31 +02:00
|
|
|
*device_id = WAVE_MAPPER;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT numdevs = waveOutGetNumDevs();
|
2010-11-03 20:51:18 +01:00
|
|
|
|
|
|
|
/* check for device id */
|
|
|
|
char *endptr;
|
|
|
|
UINT id = strtoul(device_name, &endptr, 0);
|
2011-10-23 08:59:31 +02:00
|
|
|
if (endptr > device_name && *endptr == 0) {
|
|
|
|
if (id >= numdevs)
|
|
|
|
goto fail;
|
|
|
|
*device_id = id;
|
|
|
|
return true;
|
|
|
|
}
|
2010-11-03 20:51:18 +01:00
|
|
|
|
|
|
|
/* check for device name */
|
2011-10-23 08:59:31 +02:00
|
|
|
for (UINT i = 0; i < numdevs; i++) {
|
2010-11-03 20:51:18 +01:00
|
|
|
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. */
|
2011-10-23 08:59:31 +02:00
|
|
|
if (strstr(device_name, caps.szPname) == device_name) {
|
|
|
|
*device_id = i;
|
|
|
|
return true;
|
|
|
|
}
|
2010-11-03 20:51:18 +01:00
|
|
|
}
|
|
|
|
|
2011-10-23 08:59:31 +02:00
|
|
|
fail:
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(winmm_output_domain,
|
|
|
|
"device \"%s\" is not found", device_name);
|
2011-10-23 08:59:31 +02:00
|
|
|
return false;
|
2010-11-03 20:51:18 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:34:09 +01:00
|
|
|
static AudioOutput *
|
2013-08-10 18:02:44 +02:00
|
|
|
winmm_output_init(const config_param ¶m, Error &error)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2013-02-22 20:29:03 +01:00
|
|
|
WinmmOutput *wo = new WinmmOutput();
|
2014-01-28 11:39:12 +01:00
|
|
|
if (!wo->base.Configure(param, error)) {
|
2013-10-15 22:10:42 +02:00
|
|
|
delete wo;
|
2013-02-22 20:29:03 +01:00
|
|
|
return nullptr;
|
2011-09-16 23:31:48 +02:00
|
|
|
}
|
|
|
|
|
2013-08-04 12:25:08 +02:00
|
|
|
const char *device = param.GetBlockValue("device");
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!get_device_id(device, &wo->device_id, error)) {
|
2013-10-15 22:10:42 +02:00
|
|
|
delete wo;
|
2013-02-22 20:29:03 +01:00
|
|
|
return nullptr;
|
2011-10-23 08:59:31 +02:00
|
|
|
}
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
return &wo->base;
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-01-28 11:34:09 +01:00
|
|
|
winmm_output_finish(AudioOutput *ao)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2013-02-22 20:29:03 +01:00
|
|
|
WinmmOutput *wo = (WinmmOutput *)ao;
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2013-02-22 20:29:03 +01:00
|
|
|
delete wo;
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-01-28 11:34:09 +01:00
|
|
|
winmm_output_open(AudioOutput *ao, AudioFormat &audio_format,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2013-02-22 20:29:03 +01:00
|
|
|
WinmmOutput *wo = (WinmmOutput *)ao;
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2013-02-22 20:29:03 +01:00
|
|
|
wo->event = CreateEvent(nullptr, false, false, nullptr);
|
|
|
|
if (wo->event == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(winmm_output_domain, "CreateEvent() failed");
|
2010-05-18 22:56:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
switch (audio_format.format) {
|
|
|
|
case SampleFormat::S8:
|
|
|
|
case SampleFormat::S16:
|
2010-05-18 22:56:42 +02:00
|
|
|
break;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S24_P32:
|
|
|
|
case SampleFormat::S32:
|
|
|
|
case SampleFormat::FLOAT:
|
|
|
|
case SampleFormat::DSD:
|
|
|
|
case SampleFormat::UNDEFINED:
|
2010-05-18 22:56:42 +02:00
|
|
|
/* we havn't tested formats other than S16 */
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.format = SampleFormat::S16;
|
2010-05-18 22:56:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
if (audio_format.channels > 2)
|
2010-05-18 22:56:42 +02:00
|
|
|
/* same here: more than stereo was not tested */
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.channels = 2;
|
2010-05-18 22:56:42 +02:00
|
|
|
|
|
|
|
WAVEFORMATEX format;
|
|
|
|
format.wFormatTag = WAVE_FORMAT_PCM;
|
2013-08-03 21:00:50 +02:00
|
|
|
format.nChannels = audio_format.channels;
|
|
|
|
format.nSamplesPerSec = audio_format.sample_rate;
|
|
|
|
format.nBlockAlign = audio_format.GetFrameSize();
|
2010-05-18 22:56:42 +02:00
|
|
|
format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
|
2013-08-03 21:00:50 +02:00
|
|
|
format.wBitsPerSample = audio_format.GetSampleSize() * 8;
|
2010-05-18 22:56:42 +02:00
|
|
|
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);
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(winmm_output_domain, "waveOutOpen() failed");
|
2010-05-18 22:56:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-15 22:04:17 +02:00
|
|
|
for (unsigned i = 0; i < ARRAY_SIZE(wo->buffers); ++i) {
|
2010-05-18 22:56:42 +02:00
|
|
|
memset(&wo->buffers[i].hdr, 0, sizeof(wo->buffers[i].hdr));
|
|
|
|
}
|
|
|
|
|
|
|
|
wo->next_buffer = 0;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-01-28 11:34:09 +01:00
|
|
|
winmm_output_close(AudioOutput *ao)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2013-02-22 20:29:03 +01:00
|
|
|
WinmmOutput *wo = (WinmmOutput *)ao;
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2013-10-15 22:04:17 +02:00
|
|
|
for (unsigned i = 0; i < ARRAY_SIZE(wo->buffers); ++i)
|
2013-07-31 00:57:52 +02:00
|
|
|
wo->buffers[i].buffer.Clear();
|
2010-05-18 22:56:42 +02:00
|
|
|
|
|
|
|
waveOutClose(wo->handle);
|
|
|
|
|
|
|
|
CloseHandle(wo->event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy data into a buffer, and prepare the wave header.
|
|
|
|
*/
|
|
|
|
static bool
|
2013-02-22 20:29:03 +01:00
|
|
|
winmm_set_buffer(WinmmOutput *wo, WinmmBuffer *buffer,
|
2010-05-18 22:56:42 +02:00
|
|
|
const void *data, size_t size,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2013-07-31 00:57:52 +02:00
|
|
|
void *dest = buffer->buffer.Get(size);
|
2013-02-22 20:29:03 +01:00
|
|
|
assert(dest != nullptr);
|
2010-05-18 22:56:42 +02:00
|
|
|
|
|
|
|
memcpy(dest, data, size);
|
|
|
|
|
|
|
|
memset(&buffer->hdr, 0, sizeof(buffer->hdr));
|
2013-02-22 20:29:03 +01:00
|
|
|
buffer->hdr.lpData = (LPSTR)dest;
|
2010-05-18 22:56:42 +02:00
|
|
|
buffer->hdr.dwBufferLength = size;
|
|
|
|
|
|
|
|
MMRESULT result = waveOutPrepareHeader(wo->handle, &buffer->hdr,
|
|
|
|
sizeof(buffer->hdr));
|
|
|
|
if (result != MMSYSERR_NOERROR) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(winmm_output_domain, result,
|
|
|
|
"waveOutPrepareHeader() failed");
|
2010-05-18 22:56:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait until the buffer is finished.
|
|
|
|
*/
|
|
|
|
static bool
|
2013-02-22 20:29:03 +01:00
|
|
|
winmm_drain_buffer(WinmmOutput *wo, WinmmBuffer *buffer,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
|
|
|
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) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(winmm_output_domain, result,
|
|
|
|
"waveOutUnprepareHeader() failed");
|
2010-05-18 22:56:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait some more */
|
|
|
|
WaitForSingleObject(wo->event, INFINITE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2014-01-28 11:34:09 +01:00
|
|
|
winmm_output_play(AudioOutput *ao, const void *chunk, size_t size, Error &error)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2013-02-22 20:29:03 +01:00
|
|
|
WinmmOutput *wo = (WinmmOutput *)ao;
|
2010-05-18 22:56:42 +02:00
|
|
|
|
|
|
|
/* get the next buffer from the ring and prepare it */
|
2013-02-22 20:29:03 +01:00
|
|
|
WinmmBuffer *buffer = &wo->buffers[wo->next_buffer];
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!winmm_drain_buffer(wo, buffer, error) ||
|
|
|
|
!winmm_set_buffer(wo, buffer, chunk, size, error))
|
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));
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(winmm_output_domain, result,
|
|
|
|
"waveOutWrite() failed");
|
2010-05-18 22:56:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mark our buffer as "used" */
|
|
|
|
wo->next_buffer = (wo->next_buffer + 1) %
|
2013-10-15 22:04:17 +02:00
|
|
|
ARRAY_SIZE(wo->buffers);
|
2010-05-18 22:56:42 +02:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
winmm_drain_all_buffers(WinmmOutput *wo, Error &error)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2013-10-15 22:04:17 +02:00
|
|
|
for (unsigned i = wo->next_buffer; i < ARRAY_SIZE(wo->buffers); ++i)
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!winmm_drain_buffer(wo, &wo->buffers[i], error))
|
2010-05-18 22:56:42 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < wo->next_buffer; ++i)
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!winmm_drain_buffer(wo, &wo->buffers[i], error))
|
2010-05-18 22:56:42 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-02-22 20:29:03 +01:00
|
|
|
winmm_stop(WinmmOutput *wo)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
|
|
|
waveOutReset(wo->handle);
|
|
|
|
|
2013-10-15 22:04:17 +02:00
|
|
|
for (unsigned i = 0; i < ARRAY_SIZE(wo->buffers); ++i) {
|
2013-02-22 20:29:03 +01:00
|
|
|
WinmmBuffer *buffer = &wo->buffers[i];
|
2010-05-18 22:56:42 +02:00
|
|
|
waveOutUnprepareHeader(wo->handle, &buffer->hdr,
|
|
|
|
sizeof(buffer->hdr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-01-28 11:34:09 +01:00
|
|
|
winmm_output_drain(AudioOutput *ao)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2013-02-22 20:29:03 +01:00
|
|
|
WinmmOutput *wo = (WinmmOutput *)ao;
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!winmm_drain_all_buffers(wo, IgnoreError()))
|
2010-10-08 19:55:14 +02:00
|
|
|
winmm_stop(wo);
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-01-28 11:34:09 +01:00
|
|
|
winmm_output_cancel(AudioOutput *ao)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2013-02-22 20:29:03 +01:00
|
|
|
WinmmOutput *wo = (WinmmOutput *)ao;
|
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
|
|
|
}
|
|
|
|
|
2014-01-28 11:22:27 +01:00
|
|
|
const struct AudioOutputPlugin winmm_output_plugin = {
|
2013-02-22 20:29:03 +01:00
|
|
|
"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,
|
2010-05-18 22:56:42 +02:00
|
|
|
};
|