2010-05-18 22:56:42 +02:00
|
|
|
/*
|
2019-06-17 11:17:30 +02:00
|
|
|
* Copyright 2003-2019 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.
|
|
|
|
*/
|
|
|
|
|
2013-02-22 20:29:03 +01:00
|
|
|
#include "WinmmOutputPlugin.hxx"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "../OutputAPI.hxx"
|
2019-06-17 12:23:34 +02:00
|
|
|
#include "pcm/Buffer.hxx"
|
2014-01-24 16:25:21 +01:00
|
|
|
#include "mixer/MixerList.hxx"
|
2015-03-05 09:03:25 +01:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
2016-11-07 07:42:18 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2013-10-15 22:04:17 +02:00
|
|
|
#include "util/Macros.hxx"
|
2015-11-06 09:09:02 +01:00
|
|
|
#include "util/StringCompare.hxx"
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2016-11-07 08:50:58 +01:00
|
|
|
#include <array>
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
class WinmmOutput final : AudioOutput {
|
2016-11-07 07:42:18 +01:00
|
|
|
const 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;
|
|
|
|
|
2016-11-07 08:50:58 +01:00
|
|
|
std::array<WinmmBuffer, 8> buffers;
|
2010-05-18 22:56:42 +02:00
|
|
|
unsigned next_buffer;
|
2014-01-28 23:39:48 +01:00
|
|
|
|
2016-11-07 08:08:42 +01:00
|
|
|
public:
|
2016-11-07 07:42:18 +01:00
|
|
|
WinmmOutput(const ConfigBlock &block);
|
2016-11-07 08:08:42 +01:00
|
|
|
|
|
|
|
HWAVEOUT GetHandle() {
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
static AudioOutput *Create(EventLoop &, const ConfigBlock &block) {
|
2016-11-07 08:08:42 +01:00
|
|
|
return new WinmmOutput(block);
|
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
private:
|
|
|
|
void Open(AudioFormat &audio_format) override;
|
|
|
|
void Close() noexcept override;
|
2016-11-07 08:08:42 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
size_t Play(const void *chunk, size_t size) override;
|
|
|
|
void Drain() override;
|
|
|
|
void Cancel() noexcept override;
|
2016-11-07 08:08:42 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Wait until the buffer is finished.
|
|
|
|
*/
|
|
|
|
void DrainBuffer(WinmmBuffer &buffer);
|
|
|
|
|
|
|
|
void DrainAllBuffers();
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void Stop() noexcept;
|
2016-11-07 08:08:42 +01:00
|
|
|
|
2010-05-18 22:56:42 +02:00
|
|
|
};
|
|
|
|
|
2016-11-07 07:42:18 +01:00
|
|
|
static std::runtime_error
|
|
|
|
MakeWaveOutError(MMRESULT result, const char *prefix)
|
2014-12-17 19:43:14 +01:00
|
|
|
{
|
|
|
|
char buffer[256];
|
|
|
|
if (waveOutGetErrorTextA(result, buffer,
|
|
|
|
ARRAY_SIZE(buffer)) == MMSYSERR_NOERROR)
|
2016-11-07 07:42:18 +01:00
|
|
|
return FormatRuntimeError("%s: %s", prefix, buffer);
|
2014-12-17 19:43:14 +01:00
|
|
|
else
|
2016-11-07 07:42:18 +01:00
|
|
|
return std::runtime_error(prefix);
|
2014-12-17 19:43:14 +01:00
|
|
|
}
|
|
|
|
|
2010-10-08 22:45:08 +02:00
|
|
|
HWAVEOUT
|
2014-02-06 21:10:12 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-07 07:42:18 +01:00
|
|
|
static UINT
|
|
|
|
get_device_id(const char *device_name)
|
2010-11-03 20:51:18 +01:00
|
|
|
{
|
|
|
|
/* if device is not specified use wave mapper */
|
2016-11-07 07:42:18 +01:00
|
|
|
if (device_name == nullptr)
|
|
|
|
return WAVE_MAPPER;
|
2011-10-23 08:59:31 +02:00
|
|
|
|
|
|
|
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) {
|
2016-11-07 07:42:18 +01:00
|
|
|
if (id >= numdevs)
|
|
|
|
throw FormatRuntimeError("device \"%s\" is not found",
|
|
|
|
device_name);
|
|
|
|
|
|
|
|
return id;
|
2011-10-23 08:59:31 +02:00
|
|
|
}
|
2010-11-03 20:51:18 +01:00
|
|
|
|
|
|
|
/* check for device name */
|
2015-03-05 09:03:25 +01:00
|
|
|
const AllocatedPath device_name_fs =
|
2016-11-07 07:42:18 +01:00
|
|
|
AllocatedPath::FromUTF8Throw(device_name);
|
2015-03-05 09:03:25 +01:00
|
|
|
|
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. */
|
2016-11-07 07:42:18 +01:00
|
|
|
if (StringStartsWith(device_name_fs.c_str(), caps.szPname))
|
|
|
|
return i;
|
2010-11-03 20:51:18 +01:00
|
|
|
}
|
|
|
|
|
2016-11-07 07:42:18 +01:00
|
|
|
throw FormatRuntimeError("device \"%s\" is not found", device_name);
|
2010-11-03 20:51:18 +01:00
|
|
|
}
|
|
|
|
|
2016-11-07 07:42:18 +01:00
|
|
|
WinmmOutput::WinmmOutput(const ConfigBlock &block)
|
2017-08-09 16:58:44 +02:00
|
|
|
:AudioOutput(0),
|
2016-11-07 07:42:18 +01:00
|
|
|
device_id(get_device_id(block.GetBlockValue("device")))
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2016-11-07 07:42:18 +01:00
|
|
|
}
|
2011-10-23 08:59:31 +02:00
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
void
|
|
|
|
WinmmOutput::Open(AudioFormat &audio_format)
|
2016-11-07 07:42:18 +01:00
|
|
|
{
|
2016-11-07 08:08:42 +01:00
|
|
|
event = CreateEvent(nullptr, false, false, nullptr);
|
|
|
|
if (event == nullptr)
|
2016-11-07 07:42:18 +01:00
|
|
|
throw std::runtime_error("CreateEvent() failed");
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
switch (audio_format.format) {
|
|
|
|
case SampleFormat::S16:
|
2010-05-18 22:56:42 +02:00
|
|
|
break;
|
|
|
|
|
2016-11-07 08:53:57 +01:00
|
|
|
case SampleFormat::S8:
|
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;
|
|
|
|
|
2016-11-07 08:08:42 +01:00
|
|
|
MMRESULT result = waveOutOpen(&handle, device_id, &format,
|
|
|
|
(DWORD_PTR)event, 0, CALLBACK_EVENT);
|
2010-05-18 22:56:42 +02:00
|
|
|
if (result != MMSYSERR_NOERROR) {
|
2016-11-07 08:08:42 +01:00
|
|
|
CloseHandle(event);
|
2016-11-07 07:42:18 +01:00
|
|
|
throw MakeWaveOutError(result, "waveOutOpen() failed");
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 08:28:41 +01:00
|
|
|
for (auto &i : buffers)
|
|
|
|
memset(&i.hdr, 0, sizeof(i.hdr));
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2016-11-07 08:08:42 +01:00
|
|
|
next_buffer = 0;
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 08:08:42 +01:00
|
|
|
void
|
2017-08-09 16:58:44 +02:00
|
|
|
WinmmOutput::Close() noexcept
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2016-11-07 08:28:41 +01:00
|
|
|
for (auto &i : buffers)
|
|
|
|
i.buffer.Clear();
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2016-11-07 08:08:42 +01:00
|
|
|
waveOutClose(handle);
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2016-11-07 08:08:42 +01:00
|
|
|
CloseHandle(event);
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy data into a buffer, and prepare the wave header.
|
|
|
|
*/
|
2016-11-07 07:42:18 +01:00
|
|
|
static void
|
2016-11-07 08:08:42 +01:00
|
|
|
winmm_set_buffer(HWAVEOUT handle, WinmmBuffer *buffer,
|
2016-11-07 07:42:18 +01:00
|
|
|
const void *data, size_t size)
|
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;
|
|
|
|
|
2016-11-07 08:08:42 +01:00
|
|
|
MMRESULT result = waveOutPrepareHeader(handle, &buffer->hdr,
|
2010-05-18 22:56:42 +02:00
|
|
|
sizeof(buffer->hdr));
|
2016-11-07 07:42:18 +01:00
|
|
|
if (result != MMSYSERR_NOERROR)
|
|
|
|
throw MakeWaveOutError(result,
|
|
|
|
"waveOutPrepareHeader() failed");
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 08:08:42 +01:00
|
|
|
void
|
|
|
|
WinmmOutput::DrainBuffer(WinmmBuffer &buffer)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2016-11-07 08:08:42 +01:00
|
|
|
if ((buffer.hdr.dwFlags & WHDR_DONE) == WHDR_DONE)
|
2010-05-18 22:56:42 +02:00
|
|
|
/* already finished */
|
2016-11-07 07:42:18 +01:00
|
|
|
return;
|
2010-05-18 22:56:42 +02:00
|
|
|
|
|
|
|
while (true) {
|
2016-11-07 08:08:42 +01:00
|
|
|
MMRESULT result = waveOutUnprepareHeader(handle,
|
|
|
|
&buffer.hdr,
|
|
|
|
sizeof(buffer.hdr));
|
2010-05-18 22:56:42 +02:00
|
|
|
if (result == MMSYSERR_NOERROR)
|
2016-11-07 07:42:18 +01:00
|
|
|
return;
|
|
|
|
else if (result != WAVERR_STILLPLAYING)
|
|
|
|
throw MakeWaveOutError(result,
|
|
|
|
"waveOutUnprepareHeader() failed");
|
2010-05-18 22:56:42 +02:00
|
|
|
|
|
|
|
/* wait some more */
|
2016-11-07 08:08:42 +01:00
|
|
|
WaitForSingleObject(event, INFINITE);
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 08:08:42 +01:00
|
|
|
size_t
|
2016-11-09 11:56:01 +01:00
|
|
|
WinmmOutput::Play(const void *chunk, size_t size)
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
|
|
|
/* 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);
|
2010-05-18 22:56:42 +02:00
|
|
|
|
|
|
|
/* enqueue the buffer */
|
2016-11-07 08:08:42 +01:00
|
|
|
MMRESULT result = waveOutWrite(handle, &buffer->hdr,
|
2010-05-18 22:56:42 +02:00
|
|
|
sizeof(buffer->hdr));
|
|
|
|
if (result != MMSYSERR_NOERROR) {
|
2016-11-07 08:08:42 +01:00
|
|
|
waveOutUnprepareHeader(handle, &buffer->hdr,
|
2010-05-18 22:56:42 +02:00
|
|
|
sizeof(buffer->hdr));
|
2016-11-07 07:42:18 +01:00
|
|
|
throw MakeWaveOutError(result, "waveOutWrite() failed");
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* mark our buffer as "used" */
|
2016-11-07 08:50:58 +01:00
|
|
|
next_buffer = (next_buffer + 1) % buffers.size();
|
2010-05-18 22:56:42 +02:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2016-11-07 08:08:42 +01:00
|
|
|
void
|
|
|
|
WinmmOutput::DrainAllBuffers()
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2016-11-07 08:50:58 +01:00
|
|
|
for (unsigned i = next_buffer; i < buffers.size(); ++i)
|
2016-11-07 08:08:42 +01:00
|
|
|
DrainBuffer(buffers[i]);
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2016-11-07 08:08:42 +01:00
|
|
|
for (unsigned i = 0; i < next_buffer; ++i)
|
|
|
|
DrainBuffer(buffers[i]);
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 08:08:42 +01:00
|
|
|
void
|
2017-08-09 16:58:44 +02:00
|
|
|
WinmmOutput::Stop() noexcept
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2016-11-07 08:08:42 +01:00
|
|
|
waveOutReset(handle);
|
2010-05-18 22:56:42 +02:00
|
|
|
|
2016-11-07 08:28:41 +01:00
|
|
|
for (auto &i : buffers)
|
|
|
|
waveOutUnprepareHeader(handle, &i.hdr, sizeof(i.hdr));
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 08:08:42 +01:00
|
|
|
void
|
|
|
|
WinmmOutput::Drain()
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2016-11-07 07:42:18 +01:00
|
|
|
try {
|
2016-11-07 08:08:42 +01:00
|
|
|
DrainAllBuffers();
|
2016-11-07 07:42:18 +01:00
|
|
|
} catch (...) {
|
2016-11-07 08:08:42 +01:00
|
|
|
Stop();
|
2016-11-07 07:42:18 +01:00
|
|
|
throw;
|
|
|
|
}
|
2010-05-18 22:56:42 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 08:08:42 +01:00
|
|
|
void
|
2017-08-09 16:58:44 +02:00
|
|
|
WinmmOutput::Cancel() noexcept
|
2010-05-18 22:56:42 +02:00
|
|
|
{
|
2016-11-07 08:08:42 +01:00
|
|
|
Stop();
|
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,
|
2017-08-09 16:58:44 +02:00
|
|
|
WinmmOutput::Create,
|
2013-02-22 20:29:03 +01:00
|
|
|
&winmm_mixer_plugin,
|
2010-05-18 22:56:42 +02:00
|
|
|
};
|