mpd/src/output/plugins/WinmmOutputPlugin.cxx

341 lines
7.6 KiB
C++
Raw Normal View History

/*
2016-02-26 17:54:05 +01:00
* Copyright 2003-2016 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"
2013-02-22 20:29:03 +01:00
#include "WinmmOutputPlugin.hxx"
2014-01-23 23:49:50 +01:00
#include "../OutputAPI.hxx"
2016-11-07 08:08:42 +01:00
#include "../Wrapper.hxx"
#include "pcm/PcmBuffer.hxx"
2014-01-24 16:25:21 +01:00
#include "mixer/MixerList.hxx"
#include "fs/AllocatedPath.hxx"
#include "util/RuntimeError.hxx"
#include "util/Macros.hxx"
#include "util/StringCompare.hxx"
#include <stdlib.h>
#include <string.h>
2013-02-22 20:29:03 +01:00
struct WinmmBuffer {
PcmBuffer buffer;
WAVEHDR hdr;
};
2016-11-07 08:08:42 +01:00
class WinmmOutput {
friend struct AudioOutputWrapper<WinmmOutput>;
AudioOutput base;
const UINT device_id;
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];
unsigned next_buffer;
2016-11-07 08:08:42 +01:00
public:
WinmmOutput(const ConfigBlock &block);
2016-11-07 08:08:42 +01:00
HWAVEOUT GetHandle() {
return handle;
}
static WinmmOutput *Create(const ConfigBlock &block, Error &) {
return new WinmmOutput(block);
}
bool Open(AudioFormat &audio_format, Error &);
void Close();
size_t Play(const void *chunk, size_t size, Error &);
void Drain();
void Cancel();
private:
/**
* Wait until the buffer is finished.
*/
void DrainBuffer(WinmmBuffer &buffer);
void DrainAllBuffers();
void Stop();
};
static std::runtime_error
MakeWaveOutError(MMRESULT result, const char *prefix)
{
char buffer[256];
if (waveOutGetErrorTextA(result, buffer,
ARRAY_SIZE(buffer)) == MMSYSERR_NOERROR)
return FormatRuntimeError("%s: %s", prefix, buffer);
else
return std::runtime_error(prefix);
}
2010-10-08 22:45:08 +02:00
HWAVEOUT
winmm_output_get_handle(WinmmOutput &output)
2010-10-08 22:45:08 +02:00
{
2016-11-07 08:08:42 +01:00
return output.GetHandle();
2010-10-08 22:45:08 +02:00
}
static bool
winmm_output_test_default_device(void)
{
return waveOutGetNumDevs() > 0;
}
static UINT
get_device_id(const char *device_name)
{
/* if device is not specified use wave mapper */
if (device_name == nullptr)
return WAVE_MAPPER;
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)
throw FormatRuntimeError("device \"%s\" is not found",
device_name);
return id;
}
/* check for device name */
const AllocatedPath device_name_fs =
AllocatedPath::FromUTF8Throw(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 (StringStartsWith(device_name_fs.c_str(), caps.szPname))
return i;
}
throw FormatRuntimeError("device \"%s\" is not found", device_name);
}
WinmmOutput::WinmmOutput(const ConfigBlock &block)
:base(winmm_output_plugin, block),
device_id(get_device_id(block.GetBlockValue("device")))
{
}
2016-11-07 08:08:42 +01:00
bool
WinmmOutput::Open(AudioFormat &audio_format, Error &)
{
2016-11-07 08:08:42 +01:00
event = CreateEvent(nullptr, false, false, nullptr);
if (event == nullptr)
throw std::runtime_error("CreateEvent() failed");
2013-08-03 21:00:50 +02:00
switch (audio_format.format) {
case SampleFormat::S8:
case SampleFormat::S16:
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:
/* we havn't tested formats other than S16 */
2013-08-03 21:00:50 +02:00
audio_format.format = SampleFormat::S16;
break;
}
2013-08-03 21:00:50 +02:00
if (audio_format.channels > 2)
/* same here: more than stereo was not tested */
2013-08-03 21:00:50 +02:00
audio_format.channels = 2;
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();
format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
2013-08-03 21:00:50 +02:00
format.wBitsPerSample = audio_format.GetSampleSize() * 8;
format.cbSize = 0;
2016-11-07 08:08:42 +01:00
MMRESULT result = waveOutOpen(&handle, device_id, &format,
(DWORD_PTR)event, 0, CALLBACK_EVENT);
if (result != MMSYSERR_NOERROR) {
2016-11-07 08:08:42 +01:00
CloseHandle(event);
throw MakeWaveOutError(result, "waveOutOpen() failed");
}
2016-11-07 08:08:42 +01:00
for (unsigned i = 0; i < ARRAY_SIZE(buffers); ++i)
memset(&buffers[i].hdr, 0, sizeof(buffers[i].hdr));
2016-11-07 08:08:42 +01:00
next_buffer = 0;
return true;
}
2016-11-07 08:08:42 +01:00
void
WinmmOutput::Close()
{
2016-11-07 08:08:42 +01:00
for (unsigned i = 0; i < ARRAY_SIZE(buffers); ++i)
buffers[i].buffer.Clear();
2016-11-07 08:08:42 +01:00
waveOutClose(handle);
2016-11-07 08:08:42 +01:00
CloseHandle(event);
}
/**
* Copy data into a buffer, and prepare the wave header.
*/
static void
2016-11-07 08:08:42 +01:00
winmm_set_buffer(HWAVEOUT handle, WinmmBuffer *buffer,
const void *data, size_t size)
{
void *dest = buffer->buffer.Get(size);
2013-02-22 20:29:03 +01:00
assert(dest != nullptr);
memcpy(dest, data, size);
memset(&buffer->hdr, 0, sizeof(buffer->hdr));
2013-02-22 20:29:03 +01:00
buffer->hdr.lpData = (LPSTR)dest;
buffer->hdr.dwBufferLength = size;
2016-11-07 08:08:42 +01:00
MMRESULT result = waveOutPrepareHeader(handle, &buffer->hdr,
sizeof(buffer->hdr));
if (result != MMSYSERR_NOERROR)
throw MakeWaveOutError(result,
"waveOutPrepareHeader() failed");
}
2016-11-07 08:08:42 +01:00
void
WinmmOutput::DrainBuffer(WinmmBuffer &buffer)
{
2016-11-07 08:08:42 +01:00
if ((buffer.hdr.dwFlags & WHDR_DONE) == WHDR_DONE)
/* already finished */
return;
while (true) {
2016-11-07 08:08:42 +01:00
MMRESULT result = waveOutUnprepareHeader(handle,
&buffer.hdr,
sizeof(buffer.hdr));
if (result == MMSYSERR_NOERROR)
return;
else if (result != WAVERR_STILLPLAYING)
throw MakeWaveOutError(result,
"waveOutUnprepareHeader() failed");
/* wait some more */
2016-11-07 08:08:42 +01:00
WaitForSingleObject(event, INFINITE);
}
}
2016-11-07 08:08:42 +01:00
size_t
WinmmOutput::Play(const void *chunk, size_t size, Error &)
{
/* get the next buffer from the ring and prepare it */
2016-11-07 08:08:42 +01:00
WinmmBuffer *buffer = &buffers[next_buffer];
DrainBuffer(*buffer);
winmm_set_buffer(handle, buffer, chunk, size);
/* enqueue the buffer */
2016-11-07 08:08:42 +01:00
MMRESULT result = waveOutWrite(handle, &buffer->hdr,
sizeof(buffer->hdr));
if (result != MMSYSERR_NOERROR) {
2016-11-07 08:08:42 +01:00
waveOutUnprepareHeader(handle, &buffer->hdr,
sizeof(buffer->hdr));
throw MakeWaveOutError(result, "waveOutWrite() failed");
}
/* mark our buffer as "used" */
2016-11-07 08:08:42 +01:00
next_buffer = (next_buffer + 1) % ARRAY_SIZE(buffers);
return size;
}
2016-11-07 08:08:42 +01:00
void
WinmmOutput::DrainAllBuffers()
{
2016-11-07 08:08:42 +01:00
for (unsigned i = next_buffer; i < ARRAY_SIZE(buffers); ++i)
DrainBuffer(buffers[i]);
2016-11-07 08:08:42 +01:00
for (unsigned i = 0; i < next_buffer; ++i)
DrainBuffer(buffers[i]);
}
2016-11-07 08:08:42 +01:00
void
WinmmOutput::Stop()
{
2016-11-07 08:08:42 +01:00
waveOutReset(handle);
2016-11-07 08:08:42 +01:00
for (unsigned i = 0; i < ARRAY_SIZE(buffers); ++i) {
WinmmBuffer *buffer = &buffers[i];
waveOutUnprepareHeader(handle, &buffer->hdr,
sizeof(buffer->hdr));
}
}
2016-11-07 08:08:42 +01:00
void
WinmmOutput::Drain()
{
try {
2016-11-07 08:08:42 +01:00
DrainAllBuffers();
} catch (...) {
2016-11-07 08:08:42 +01:00
Stop();
throw;
}
}
2016-11-07 08:08:42 +01:00
void
WinmmOutput::Cancel()
{
2016-11-07 08:08:42 +01:00
Stop();
}
2016-11-07 08:08:42 +01:00
typedef AudioOutputWrapper<WinmmOutput> Wrapper;
const struct AudioOutputPlugin winmm_output_plugin = {
2013-02-22 20:29:03 +01:00
"winmm",
winmm_output_test_default_device,
2016-11-07 08:08:42 +01:00
&Wrapper::Init,
&Wrapper::Finish,
2013-02-22 20:29:03 +01:00
nullptr,
nullptr,
2016-11-07 08:08:42 +01:00
&Wrapper::Open,
&Wrapper::Close,
2013-02-22 20:29:03 +01:00
nullptr,
nullptr,
2016-11-07 08:08:42 +01:00
&Wrapper::Play,
&Wrapper::Drain,
&Wrapper::Cancel,
2013-02-22 20:29:03 +01:00
nullptr,
&winmm_mixer_plugin,
};