2008-05-14 18:02:27 +00:00
|
|
|
/*
|
|
|
|
* Windows DirectSound interface
|
|
|
|
*
|
2004-09-25 15:34:42 +00:00
|
|
|
* Copyright (c) 2004 Gabor Szecsi <deje@miki.hu>
|
|
|
|
*
|
2008-05-14 18:02:27 +00:00
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
2004-09-25 15:34:42 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2008-05-14 18:02:27 +00:00
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
2004-09-25 15:34:42 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2008-05-14 18:02:27 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2004-09-25 15:34:42 +00:00
|
|
|
/**
|
|
|
|
\todo verify/extend multichannel support
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <windows.h>
|
2004-10-14 13:03:03 +00:00
|
|
|
#define DIRECTSOUND_VERSION 0x0600
|
2004-09-25 15:34:42 +00:00
|
|
|
#include <dsound.h>
|
2005-11-11 03:07:01 +00:00
|
|
|
#include <math.h>
|
2004-09-25 15:34:42 +00:00
|
|
|
|
2012-04-20 02:52:51 +00:00
|
|
|
#include <libavutil/avutil.h>
|
2013-03-23 19:18:10 +00:00
|
|
|
#include <libavutil/common.h>
|
2012-04-20 02:52:51 +00:00
|
|
|
|
2005-01-12 22:00:02 +00:00
|
|
|
#include "config.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "audio/format.h"
|
|
|
|
#include "ao.h"
|
2012-04-20 02:52:51 +00:00
|
|
|
#include "audio/reorder_ch.h"
|
2013-08-06 20:41:30 +00:00
|
|
|
#include "mpvcore/mp_msg.h"
|
2004-09-25 15:34:42 +00:00
|
|
|
#include "osdep/timer.h"
|
2013-08-06 20:41:30 +00:00
|
|
|
#include "mpvcore/m_option.h"
|
2004-09-25 15:34:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\todo use the definitions from the win32 api headers when they define these
|
|
|
|
*/
|
|
|
|
#define WAVE_FORMAT_IEEE_FLOAT 0x0003
|
|
|
|
#define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
|
|
|
|
#define WAVE_FORMAT_EXTENSIBLE 0xFFFE
|
|
|
|
|
2013-06-03 22:51:07 +00:00
|
|
|
static const GUID KSDATAFORMAT_SUBTYPE_PCM = {
|
|
|
|
0x1, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
|
|
|
|
};
|
2004-09-25 15:34:42 +00:00
|
|
|
|
2012-02-27 18:15:13 +00:00
|
|
|
#if 0
|
2004-09-25 15:34:42 +00:00
|
|
|
#define DSSPEAKER_HEADPHONE 0x00000001
|
|
|
|
#define DSSPEAKER_MONO 0x00000002
|
|
|
|
#define DSSPEAKER_QUAD 0x00000003
|
|
|
|
#define DSSPEAKER_STEREO 0x00000004
|
|
|
|
#define DSSPEAKER_SURROUND 0x00000005
|
|
|
|
#define DSSPEAKER_5POINT1 0x00000006
|
2012-02-27 18:15:13 +00:00
|
|
|
#endif
|
2004-09-25 15:34:42 +00:00
|
|
|
|
|
|
|
#ifndef _WAVEFORMATEXTENSIBLE_
|
|
|
|
typedef struct {
|
2013-06-03 22:51:07 +00:00
|
|
|
WAVEFORMATEX Format;
|
2004-09-25 15:34:42 +00:00
|
|
|
union {
|
|
|
|
WORD wValidBitsPerSample; /* bits of precision */
|
|
|
|
WORD wSamplesPerBlock; /* valid if wBitsPerSample==0 */
|
|
|
|
WORD wReserved; /* If neither applies, set to zero. */
|
|
|
|
} Samples;
|
2013-06-03 22:51:07 +00:00
|
|
|
DWORD dwChannelMask; /* which channels are */
|
2004-09-25 15:34:42 +00:00
|
|
|
/* present in stream */
|
2013-06-03 22:51:07 +00:00
|
|
|
GUID SubFormat;
|
2004-09-25 15:34:42 +00:00
|
|
|
} WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
|
|
|
|
#endif
|
|
|
|
|
2013-06-03 23:17:36 +00:00
|
|
|
struct priv {
|
|
|
|
HINSTANCE hdsound_dll; ///handle to the dll
|
|
|
|
LPDIRECTSOUND hds; ///direct sound object
|
|
|
|
LPDIRECTSOUNDBUFFER hdspribuf; ///primary direct sound buffer
|
|
|
|
LPDIRECTSOUNDBUFFER hdsbuf; ///secondary direct sound buffer (stream buffer)
|
|
|
|
int buffer_size; ///size in bytes of the direct sound buffer
|
|
|
|
int write_offset; ///offset of the write cursor in the direct sound buffer
|
|
|
|
int min_free_space; ///if the free space is below this value get_space() will return 0
|
|
|
|
///there will always be at least this amout of free space to prevent
|
|
|
|
///get_space() from returning wrong values when buffer is 100% full.
|
|
|
|
///will be replaced with nBlockAlign in init()
|
|
|
|
int underrun_check; ///0 or last reported free space (underrun detection)
|
|
|
|
int device_num; ///wanted device number
|
|
|
|
GUID device; ///guid of the device
|
|
|
|
int audio_volume;
|
|
|
|
|
|
|
|
int device_index;
|
2013-06-16 17:15:32 +00:00
|
|
|
|
|
|
|
int outburst; ///play in multiple of chunks of this size
|
2013-07-21 22:11:06 +00:00
|
|
|
|
|
|
|
int cfg_device;
|
2013-06-03 23:17:36 +00:00
|
|
|
};
|
2004-09-25 15:34:42 +00:00
|
|
|
|
2013-06-03 22:59:53 +00:00
|
|
|
static float get_delay(struct ao *ao);
|
|
|
|
|
2004-09-25 15:34:42 +00:00
|
|
|
/***************************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief output error message
|
|
|
|
\param err error code
|
|
|
|
\return string with the error message
|
|
|
|
*/
|
|
|
|
static char * dserr2str(int err)
|
|
|
|
{
|
2013-06-03 22:51:07 +00:00
|
|
|
switch (err) {
|
|
|
|
case DS_OK: return "DS_OK";
|
|
|
|
case DS_NO_VIRTUALIZATION: return "DS_NO_VIRTUALIZATION";
|
|
|
|
case DSERR_ALLOCATED: return "DS_NO_VIRTUALIZATION";
|
|
|
|
case DSERR_CONTROLUNAVAIL: return "DSERR_CONTROLUNAVAIL";
|
|
|
|
case DSERR_INVALIDPARAM: return "DSERR_INVALIDPARAM";
|
|
|
|
case DSERR_INVALIDCALL: return "DSERR_INVALIDCALL";
|
|
|
|
case DSERR_GENERIC: return "DSERR_GENERIC";
|
|
|
|
case DSERR_PRIOLEVELNEEDED: return "DSERR_PRIOLEVELNEEDED";
|
|
|
|
case DSERR_OUTOFMEMORY: return "DSERR_OUTOFMEMORY";
|
|
|
|
case DSERR_BADFORMAT: return "DSERR_BADFORMAT";
|
|
|
|
case DSERR_UNSUPPORTED: return "DSERR_UNSUPPORTED";
|
|
|
|
case DSERR_NODRIVER: return "DSERR_NODRIVER";
|
|
|
|
case DSERR_ALREADYINITIALIZED: return "DSERR_ALREADYINITIALIZED";
|
|
|
|
case DSERR_NOAGGREGATION: return "DSERR_NOAGGREGATION";
|
|
|
|
case DSERR_BUFFERLOST: return "DSERR_BUFFERLOST";
|
|
|
|
case DSERR_OTHERAPPHASPRIO: return "DSERR_OTHERAPPHASPRIO";
|
|
|
|
case DSERR_UNINITIALIZED: return "DSERR_UNINITIALIZED";
|
|
|
|
case DSERR_NOINTERFACE: return "DSERR_NOINTERFACE";
|
|
|
|
case DSERR_ACCESSDENIED: return "DSERR_ACCESSDENIED";
|
|
|
|
}
|
|
|
|
return "unknown";
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief uninitialize direct sound
|
|
|
|
*/
|
2013-06-03 23:17:36 +00:00
|
|
|
static void UninitDirectSound(struct ao *ao)
|
2004-09-25 15:34:42 +00:00
|
|
|
{
|
2013-06-03 23:17:36 +00:00
|
|
|
struct priv *p = ao->priv;
|
|
|
|
|
2004-09-25 15:34:42 +00:00
|
|
|
// finally release the DirectSound object
|
2013-06-03 23:17:36 +00:00
|
|
|
if (p->hds) {
|
|
|
|
IDirectSound_Release(p->hds);
|
|
|
|
p->hds = NULL;
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
|
|
|
// free DSOUND.DLL
|
2013-06-03 23:17:36 +00:00
|
|
|
if (p->hdsound_dll) {
|
|
|
|
FreeLibrary(p->hdsound_dll);
|
|
|
|
p->hdsound_dll = NULL;
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_VERBOSE(ao, "DirectSound uninitialized\n");
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
|
|
|
|
2005-01-06 13:31:02 +00:00
|
|
|
/**
|
|
|
|
\brief enumerate direct sound devices
|
|
|
|
\return TRUE to continue with the enumeration
|
|
|
|
*/
|
2013-06-03 22:51:07 +00:00
|
|
|
static BOOL CALLBACK DirectSoundEnum(LPGUID guid, LPCSTR desc, LPCSTR module,
|
|
|
|
LPVOID context)
|
2005-01-06 13:31:02 +00:00
|
|
|
{
|
2013-06-03 23:17:36 +00:00
|
|
|
struct ao *ao = context;
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_VERBOSE(ao, "%i %s ", p->device_index, desc);
|
2013-06-03 23:17:36 +00:00
|
|
|
if (p->device_num == p->device_index) {
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_VERBOSE(ao, "<--");
|
2013-06-03 22:51:07 +00:00
|
|
|
if (guid)
|
2013-06-03 23:17:36 +00:00
|
|
|
memcpy(&p->device, guid, sizeof(GUID));
|
2005-01-06 13:31:02 +00:00
|
|
|
}
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_VERBOSE(ao, "\n");
|
2013-06-03 23:17:36 +00:00
|
|
|
p->device_index++;
|
2005-01-06 13:31:02 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-25 15:34:42 +00:00
|
|
|
/**
|
|
|
|
\brief initilize direct sound
|
|
|
|
\return 0 if error, 1 if ok
|
|
|
|
*/
|
2013-07-21 22:11:06 +00:00
|
|
|
static int InitDirectSound(struct ao *ao)
|
2004-09-25 15:34:42 +00:00
|
|
|
{
|
2013-06-03 23:17:36 +00:00
|
|
|
struct priv *p = ao->priv;
|
|
|
|
|
2013-06-03 22:51:07 +00:00
|
|
|
DSCAPS dscaps;
|
2004-09-25 15:34:42 +00:00
|
|
|
|
2013-06-03 22:51:07 +00:00
|
|
|
// initialize directsound
|
2004-09-25 15:34:42 +00:00
|
|
|
HRESULT (WINAPI *OurDirectSoundCreate)(LPGUID, LPDIRECTSOUND *, LPUNKNOWN);
|
2013-06-03 22:51:07 +00:00
|
|
|
HRESULT (WINAPI *OurDirectSoundEnumerate)(LPDSENUMCALLBACKA, LPVOID);
|
2013-06-03 23:17:36 +00:00
|
|
|
p->device_index = 0;
|
2013-07-21 22:11:06 +00:00
|
|
|
p->device_num = p->cfg_device;
|
2013-06-03 22:51:07 +00:00
|
|
|
|
2013-06-03 23:17:36 +00:00
|
|
|
p->hdsound_dll = LoadLibrary("DSOUND.DLL");
|
|
|
|
if (p->hdsound_dll == NULL) {
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_ERR(ao, "cannot load DSOUND.DLL\n");
|
2013-06-03 22:51:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-06-03 23:17:36 +00:00
|
|
|
OurDirectSoundCreate = (void *)GetProcAddress(p->hdsound_dll,
|
2013-06-03 22:51:07 +00:00
|
|
|
"DirectSoundCreate");
|
2013-06-03 23:17:36 +00:00
|
|
|
OurDirectSoundEnumerate = (void *)GetProcAddress(p->hdsound_dll,
|
2013-06-03 22:51:07 +00:00
|
|
|
"DirectSoundEnumerateA");
|
|
|
|
|
|
|
|
if (OurDirectSoundCreate == NULL || OurDirectSoundEnumerate == NULL) {
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_ERR(ao, "GetProcAddress FAILED\n");
|
2013-06-03 23:17:36 +00:00
|
|
|
FreeLibrary(p->hdsound_dll);
|
2013-06-03 22:51:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-03 23:17:36 +00:00
|
|
|
// Enumerate all directsound p->devices
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_VERBOSE(ao, "Output Devices:\n");
|
2013-06-03 23:17:36 +00:00
|
|
|
OurDirectSoundEnumerate(DirectSoundEnum, ao);
|
2013-06-03 22:51:07 +00:00
|
|
|
|
|
|
|
// Create the direct sound object
|
2013-06-03 23:17:36 +00:00
|
|
|
if (FAILED(OurDirectSoundCreate((p->device_num) ? &p->device : NULL,
|
|
|
|
&p->hds, NULL)))
|
2013-06-03 22:51:07 +00:00
|
|
|
{
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_ERR(ao, "cannot create a DirectSound device\n");
|
2013-06-03 23:17:36 +00:00
|
|
|
FreeLibrary(p->hdsound_dll);
|
2013-06-03 22:51:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set DirectSound Cooperative level, ie what control we want over Windows
|
|
|
|
* sound device. In our case, DSSCL_EXCLUSIVE means that we can modify the
|
|
|
|
* settings of the primary buffer, but also that only the sound of our
|
|
|
|
* application will be hearable when it will have the focus.
|
|
|
|
* !!! (this is not really working as intended yet because to set the
|
|
|
|
* cooperative level you need the window handle of your application, and
|
|
|
|
* I don't know of any easy way to get it. Especially since we might play
|
|
|
|
* sound without any video, and so what window handle should we use ???
|
|
|
|
* The hack for now is to use the Desktop window handle - it seems to be
|
|
|
|
* working */
|
2013-06-03 23:17:36 +00:00
|
|
|
if (IDirectSound_SetCooperativeLevel(p->hds, GetDesktopWindow(),
|
2013-06-03 22:51:07 +00:00
|
|
|
DSSCL_EXCLUSIVE))
|
|
|
|
{
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_ERR(ao, "cannot set direct sound cooperative level\n");
|
2013-06-03 23:17:36 +00:00
|
|
|
IDirectSound_Release(p->hds);
|
|
|
|
FreeLibrary(p->hdsound_dll);
|
2013-06-03 22:51:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_VERBOSE(ao, "DirectSound initialized\n");
|
2013-06-03 22:51:07 +00:00
|
|
|
|
|
|
|
memset(&dscaps, 0, sizeof(DSCAPS));
|
|
|
|
dscaps.dwSize = sizeof(DSCAPS);
|
2013-06-03 23:17:36 +00:00
|
|
|
if (DS_OK == IDirectSound_GetCaps(p->hds, &dscaps)) {
|
2013-06-03 22:51:07 +00:00
|
|
|
if (dscaps.dwFlags & DSCAPS_EMULDRIVER)
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_VERBOSE(ao, "DirectSound is emulated\n");
|
2013-06-03 22:51:07 +00:00
|
|
|
} else {
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_VERBOSE(ao, "cannot get device capabilities\n");
|
2013-06-03 22:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief destroy the direct sound buffer
|
|
|
|
*/
|
2013-06-03 23:17:36 +00:00
|
|
|
static void DestroyBuffer(struct ao *ao)
|
2004-09-25 15:34:42 +00:00
|
|
|
{
|
2013-06-03 23:17:36 +00:00
|
|
|
struct priv *p = ao->priv;
|
|
|
|
|
|
|
|
if (p->hdsbuf) {
|
|
|
|
IDirectSoundBuffer_Release(p->hdsbuf);
|
|
|
|
p->hdsbuf = NULL;
|
2013-06-03 22:51:07 +00:00
|
|
|
}
|
2013-06-03 23:17:36 +00:00
|
|
|
if (p->hdspribuf) {
|
|
|
|
IDirectSoundBuffer_Release(p->hdspribuf);
|
|
|
|
p->hdspribuf = NULL;
|
2013-06-03 22:51:07 +00:00
|
|
|
}
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief fill sound buffer
|
|
|
|
\param data pointer to the sound data to copy
|
|
|
|
\param len length of the data to copy in bytes
|
|
|
|
\return number of copyed bytes
|
|
|
|
*/
|
2013-06-03 22:59:53 +00:00
|
|
|
static int write_buffer(struct ao *ao, unsigned char *data, int len)
|
2004-09-25 15:34:42 +00:00
|
|
|
{
|
2013-06-03 23:17:36 +00:00
|
|
|
struct priv *p = ao->priv;
|
2013-06-03 22:51:07 +00:00
|
|
|
HRESULT res;
|
|
|
|
LPVOID lpvPtr1;
|
|
|
|
DWORD dwBytes1;
|
|
|
|
LPVOID lpvPtr2;
|
|
|
|
DWORD dwBytes2;
|
|
|
|
|
2013-06-03 23:17:36 +00:00
|
|
|
p->underrun_check = 0;
|
2013-06-03 22:51:07 +00:00
|
|
|
|
|
|
|
// Lock the buffer
|
2013-06-03 23:17:36 +00:00
|
|
|
res = IDirectSoundBuffer_Lock(p->hdsbuf, p->write_offset, len, &lpvPtr1,
|
|
|
|
&dwBytes1, &lpvPtr2, &dwBytes2, 0);
|
2013-06-03 22:51:07 +00:00
|
|
|
// If the buffer was lost, restore and retry lock.
|
|
|
|
if (DSERR_BUFFERLOST == res) {
|
2013-06-03 23:17:36 +00:00
|
|
|
IDirectSoundBuffer_Restore(p->hdsbuf);
|
|
|
|
res = IDirectSoundBuffer_Lock(p->hdsbuf, p->write_offset, len, &lpvPtr1,
|
2013-06-03 22:51:07 +00:00
|
|
|
&dwBytes1, &lpvPtr2, &dwBytes2, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(res)) {
|
2013-06-03 22:59:53 +00:00
|
|
|
if (!AF_FORMAT_IS_AC3(ao->format)) {
|
2013-04-05 21:06:22 +00:00
|
|
|
memcpy(lpvPtr1, data, dwBytes1);
|
2012-04-20 02:52:51 +00:00
|
|
|
if (lpvPtr2 != NULL)
|
2013-04-05 21:06:22 +00:00
|
|
|
memcpy(lpvPtr2, (char *)data + dwBytes1, dwBytes2);
|
2004-10-18 19:23:13 +00:00
|
|
|
|
2013-06-03 23:17:36 +00:00
|
|
|
p->write_offset += dwBytes1 + dwBytes2;
|
|
|
|
if (p->write_offset >= p->buffer_size)
|
|
|
|
p->write_offset = dwBytes2;
|
2013-06-03 22:51:07 +00:00
|
|
|
} else {
|
|
|
|
// Write to pointers without reordering.
|
|
|
|
memcpy(lpvPtr1, data, dwBytes1);
|
|
|
|
if (NULL != lpvPtr2)
|
|
|
|
memcpy(lpvPtr2, data + dwBytes1, dwBytes2);
|
2013-06-03 23:17:36 +00:00
|
|
|
p->write_offset += dwBytes1 + dwBytes2;
|
|
|
|
if (p->write_offset >= p->buffer_size)
|
|
|
|
p->write_offset = dwBytes2;
|
2013-06-03 22:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release the data back to DirectSound.
|
2013-06-03 23:17:36 +00:00
|
|
|
res = IDirectSoundBuffer_Unlock(p->hdsbuf, lpvPtr1, dwBytes1, lpvPtr2,
|
2013-06-03 22:51:07 +00:00
|
|
|
dwBytes2);
|
|
|
|
if (SUCCEEDED(res)) {
|
|
|
|
// Success.
|
|
|
|
DWORD status;
|
2013-06-03 23:17:36 +00:00
|
|
|
IDirectSoundBuffer_GetStatus(p->hdsbuf, &status);
|
2013-06-03 22:51:07 +00:00
|
|
|
if (!(status & DSBSTATUS_PLAYING))
|
2013-06-03 23:17:36 +00:00
|
|
|
res = IDirectSoundBuffer_Play(p->hdsbuf, 0, 0, DSBPLAY_LOOPING);
|
2013-06-03 22:51:07 +00:00
|
|
|
return dwBytes1 + dwBytes2;
|
|
|
|
}
|
2009-05-13 02:58:57 +00:00
|
|
|
}
|
2013-06-03 22:51:07 +00:00
|
|
|
// Lock, Unlock, or Restore failed.
|
|
|
|
return 0;
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief handle control commands
|
|
|
|
\param cmd command
|
|
|
|
\param arg argument
|
2013-06-03 22:59:53 +00:00
|
|
|
\return CONTROL_OK or CONTROL_UNKNOWN in case the command is not supported
|
2004-09-25 15:34:42 +00:00
|
|
|
*/
|
2013-06-03 22:59:53 +00:00
|
|
|
static int control(struct ao *ao, enum aocontrol cmd, void *arg)
|
2004-09-25 15:34:42 +00:00
|
|
|
{
|
2013-06-03 23:17:36 +00:00
|
|
|
struct priv *p = ao->priv;
|
2013-06-03 22:51:07 +00:00
|
|
|
DWORD volume;
|
|
|
|
switch (cmd) {
|
|
|
|
case AOCONTROL_GET_VOLUME: {
|
|
|
|
ao_control_vol_t *vol = (ao_control_vol_t *)arg;
|
2013-06-03 23:17:36 +00:00
|
|
|
vol->left = vol->right = p->audio_volume;
|
2013-06-03 22:51:07 +00:00
|
|
|
return CONTROL_OK;
|
|
|
|
}
|
|
|
|
case AOCONTROL_SET_VOLUME: {
|
|
|
|
ao_control_vol_t *vol = (ao_control_vol_t *)arg;
|
2013-06-03 23:17:36 +00:00
|
|
|
volume = p->audio_volume = vol->right;
|
2013-06-03 22:51:07 +00:00
|
|
|
if (volume < 1)
|
|
|
|
volume = 1;
|
|
|
|
volume = (DWORD)(log10(volume) * 5000.0) - 10000;
|
2013-06-03 23:17:36 +00:00
|
|
|
IDirectSoundBuffer_SetVolume(p->hdsbuf, volume);
|
2013-06-03 22:51:07 +00:00
|
|
|
return CONTROL_OK;
|
|
|
|
}
|
|
|
|
}
|
2013-06-03 22:59:53 +00:00
|
|
|
return CONTROL_UNKNOWN;
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
|
|
|
|
2009-05-13 02:58:57 +00:00
|
|
|
/**
|
2004-09-25 15:34:42 +00:00
|
|
|
\brief setup sound device
|
|
|
|
\param rate samplerate
|
|
|
|
\param channels number of channels
|
|
|
|
\param format format
|
|
|
|
\param flags unused
|
2013-06-03 22:59:53 +00:00
|
|
|
\return 0=success -1=fail
|
2004-09-25 15:34:42 +00:00
|
|
|
*/
|
2013-07-22 20:57:51 +00:00
|
|
|
static int init(struct ao *ao)
|
2004-09-25 15:34:42 +00:00
|
|
|
{
|
2013-07-21 22:11:06 +00:00
|
|
|
struct priv *p = ao->priv;
|
2004-09-25 15:34:42 +00:00
|
|
|
int res;
|
2013-06-03 23:17:36 +00:00
|
|
|
|
2013-07-21 22:11:06 +00:00
|
|
|
if (!InitDirectSound(ao))
|
2013-06-03 22:59:53 +00:00
|
|
|
return -1;
|
2013-06-03 22:51:07 +00:00
|
|
|
|
2013-06-03 22:59:53 +00:00
|
|
|
ao->no_persistent_volume = true;
|
2013-06-03 23:17:36 +00:00
|
|
|
p->audio_volume = 100;
|
2013-06-03 22:51:07 +00:00
|
|
|
|
|
|
|
// ok, now create the buffers
|
|
|
|
WAVEFORMATEXTENSIBLE wformat;
|
|
|
|
DSBUFFERDESC dsbpridesc;
|
|
|
|
DSBUFFERDESC dsbdesc;
|
2013-06-03 22:59:53 +00:00
|
|
|
int format = ao->format;
|
|
|
|
int rate = ao->samplerate;
|
2013-06-03 22:51:07 +00:00
|
|
|
|
|
|
|
if (AF_FORMAT_IS_AC3(format))
|
|
|
|
format = AF_FORMAT_AC3_NE;
|
|
|
|
else {
|
2013-06-03 22:59:53 +00:00
|
|
|
struct mp_chmap_sel sel = {0};
|
2013-06-03 22:51:07 +00:00
|
|
|
mp_chmap_sel_add_waveext(&sel);
|
2013-06-03 22:59:53 +00:00
|
|
|
if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels))
|
|
|
|
return -1;
|
2013-06-03 22:51:07 +00:00
|
|
|
}
|
|
|
|
switch (format) {
|
|
|
|
case AF_FORMAT_AC3_NE:
|
|
|
|
case AF_FORMAT_S24_LE:
|
|
|
|
case AF_FORMAT_S16_LE:
|
|
|
|
case AF_FORMAT_U8:
|
|
|
|
break;
|
|
|
|
default:
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_VERBOSE(ao, "format %s not supported defaulting to Signed 16-bit Little-Endian\n",
|
|
|
|
af_fmt2str_short(format));
|
2013-06-03 22:51:07 +00:00
|
|
|
format = AF_FORMAT_S16_LE;
|
|
|
|
}
|
2013-06-03 22:59:53 +00:00
|
|
|
//set our audio parameters
|
|
|
|
ao->samplerate = rate;
|
|
|
|
ao->format = format;
|
|
|
|
ao->bps = ao->channels.num * rate * (af_fmt2bits(format) >> 3);
|
2013-06-16 17:15:32 +00:00
|
|
|
int buffersize = ao->bps; // space for 1 sec
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_VERBOSE(ao, "Samplerate:%iHz Channels:%i Format:%s\n", rate,
|
|
|
|
ao->channels.num, af_fmt2str_short(format));
|
|
|
|
MP_VERBOSE(ao, "Buffersize:%d bytes (%d msec)\n",
|
|
|
|
buffersize, buffersize / ao->bps * 1000);
|
2013-06-03 22:51:07 +00:00
|
|
|
|
|
|
|
//fill waveformatex
|
|
|
|
ZeroMemory(&wformat, sizeof(WAVEFORMATEXTENSIBLE));
|
2013-06-03 22:59:53 +00:00
|
|
|
wformat.Format.cbSize = (ao->channels.num > 2)
|
2013-06-03 22:51:07 +00:00
|
|
|
? sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX) : 0;
|
2013-06-03 22:59:53 +00:00
|
|
|
wformat.Format.nChannels = ao->channels.num;
|
2013-06-03 22:51:07 +00:00
|
|
|
wformat.Format.nSamplesPerSec = rate;
|
|
|
|
if (AF_FORMAT_IS_AC3(format)) {
|
|
|
|
wformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
|
|
|
|
wformat.Format.wBitsPerSample = 16;
|
|
|
|
wformat.Format.nBlockAlign = 4;
|
|
|
|
} else {
|
2013-06-03 22:59:53 +00:00
|
|
|
wformat.Format.wFormatTag = (ao->channels.num > 2)
|
2013-06-03 22:51:07 +00:00
|
|
|
? WAVE_FORMAT_EXTENSIBLE : WAVE_FORMAT_PCM;
|
|
|
|
wformat.Format.wBitsPerSample = af_fmt2bits(format);
|
|
|
|
wformat.Format.nBlockAlign = wformat.Format.nChannels *
|
|
|
|
(wformat.Format.wBitsPerSample >> 3);
|
|
|
|
}
|
2004-09-25 15:34:42 +00:00
|
|
|
|
2013-06-03 22:51:07 +00:00
|
|
|
// fill in primary sound buffer descriptor
|
|
|
|
memset(&dsbpridesc, 0, sizeof(DSBUFFERDESC));
|
|
|
|
dsbpridesc.dwSize = sizeof(DSBUFFERDESC);
|
|
|
|
dsbpridesc.dwFlags = DSBCAPS_PRIMARYBUFFER;
|
|
|
|
dsbpridesc.dwBufferBytes = 0;
|
|
|
|
dsbpridesc.lpwfxFormat = NULL;
|
|
|
|
|
|
|
|
// fill in the secondary sound buffer (=stream buffer) descriptor
|
|
|
|
memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
|
|
|
|
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
|
|
|
|
dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 /** Better position accuracy */
|
|
|
|
| DSBCAPS_GLOBALFOCUS /** Allows background playing */
|
|
|
|
| DSBCAPS_CTRLVOLUME; /** volume control enabled */
|
|
|
|
|
2013-06-03 22:59:53 +00:00
|
|
|
if (ao->channels.num > 2) {
|
|
|
|
wformat.dwChannelMask = mp_chmap_to_waveext(&ao->channels);
|
2013-06-03 22:51:07 +00:00
|
|
|
wformat.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
|
|
|
|
wformat.Samples.wValidBitsPerSample = wformat.Format.wBitsPerSample;
|
|
|
|
// Needed for 5.1 on emu101k - shit soundblaster
|
|
|
|
dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE;
|
|
|
|
}
|
|
|
|
wformat.Format.nAvgBytesPerSec = wformat.Format.nSamplesPerSec *
|
|
|
|
wformat.Format.nBlockAlign;
|
|
|
|
|
2013-06-16 17:15:32 +00:00
|
|
|
dsbdesc.dwBufferBytes = buffersize;
|
2013-06-03 22:51:07 +00:00
|
|
|
dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&wformat;
|
2013-06-03 23:17:36 +00:00
|
|
|
p->buffer_size = dsbdesc.dwBufferBytes;
|
|
|
|
p->write_offset = 0;
|
|
|
|
p->min_free_space = wformat.Format.nBlockAlign;
|
2013-06-16 17:15:32 +00:00
|
|
|
p->outburst = wformat.Format.nBlockAlign * 512;
|
2013-06-03 22:51:07 +00:00
|
|
|
|
|
|
|
// create primary buffer and set its format
|
|
|
|
|
2013-06-03 23:17:36 +00:00
|
|
|
res = IDirectSound_CreateSoundBuffer(p->hds, &dsbpridesc, &p->hdspribuf, NULL);
|
2013-06-03 22:51:07 +00:00
|
|
|
if (res != DS_OK) {
|
2013-06-03 23:17:36 +00:00
|
|
|
UninitDirectSound(ao);
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_ERR(ao, "cannot create primary buffer (%s)\n", dserr2str(res));
|
2013-06-03 22:59:53 +00:00
|
|
|
return -1;
|
2013-06-03 22:51:07 +00:00
|
|
|
}
|
2013-06-03 23:17:36 +00:00
|
|
|
res = IDirectSoundBuffer_SetFormat(p->hdspribuf, (WAVEFORMATEX *)&wformat);
|
2013-06-03 22:51:07 +00:00
|
|
|
if (res != DS_OK) {
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_WARN(ao, "cannot set primary buffer format (%s), using "
|
|
|
|
"standard setting (bad quality)", dserr2str(res));
|
2013-06-03 22:51:07 +00:00
|
|
|
}
|
2012-01-15 20:01:44 +00:00
|
|
|
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_VERBOSE(ao, "primary buffer created\n");
|
2004-09-25 15:34:42 +00:00
|
|
|
|
2013-06-03 22:51:07 +00:00
|
|
|
// now create the stream buffer
|
|
|
|
|
2013-06-03 23:17:36 +00:00
|
|
|
res = IDirectSound_CreateSoundBuffer(p->hds, &dsbdesc, &p->hdsbuf, NULL);
|
2013-06-03 22:51:07 +00:00
|
|
|
if (res != DS_OK) {
|
|
|
|
if (dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE) {
|
|
|
|
// Try without DSBCAPS_LOCHARDWARE
|
|
|
|
dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE;
|
2013-06-03 23:17:36 +00:00
|
|
|
res = IDirectSound_CreateSoundBuffer(p->hds, &dsbdesc, &p->hdsbuf, NULL);
|
2013-05-09 16:06:26 +00:00
|
|
|
}
|
2013-06-03 22:51:07 +00:00
|
|
|
if (res != DS_OK) {
|
2013-06-03 23:17:36 +00:00
|
|
|
UninitDirectSound(ao);
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_ERR(ao, "cannot create secondary (stream)buffer (%s)\n",
|
2013-06-03 22:51:07 +00:00
|
|
|
dserr2str(res));
|
2013-06-03 22:59:53 +00:00
|
|
|
return -1;
|
2013-06-03 22:51:07 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-23 21:30:09 +00:00
|
|
|
MP_VERBOSE(ao, "secondary (stream)buffer created\n");
|
2013-06-03 22:59:53 +00:00
|
|
|
return 0;
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief stop playing and empty buffers (for seeking/pause)
|
|
|
|
*/
|
2013-06-03 22:59:53 +00:00
|
|
|
static void reset(struct ao *ao)
|
2004-09-25 15:34:42 +00:00
|
|
|
{
|
2013-06-03 23:17:36 +00:00
|
|
|
struct priv *p = ao->priv;
|
|
|
|
|
|
|
|
IDirectSoundBuffer_Stop(p->hdsbuf);
|
2013-06-03 22:51:07 +00:00
|
|
|
// reset directsound buffer
|
2013-06-03 23:17:36 +00:00
|
|
|
IDirectSoundBuffer_SetCurrentPosition(p->hdsbuf, 0);
|
|
|
|
p->write_offset = 0;
|
|
|
|
p->underrun_check = 0;
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief stop playing, keep buffers (for pause)
|
|
|
|
*/
|
2013-06-03 22:59:53 +00:00
|
|
|
static void audio_pause(struct ao *ao)
|
2004-09-25 15:34:42 +00:00
|
|
|
{
|
2013-06-03 23:17:36 +00:00
|
|
|
struct priv *p = ao->priv;
|
|
|
|
|
|
|
|
IDirectSoundBuffer_Stop(p->hdsbuf);
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief resume playing, after audio_pause()
|
|
|
|
*/
|
2013-06-03 22:59:53 +00:00
|
|
|
static void audio_resume(struct ao *ao)
|
2004-09-25 15:34:42 +00:00
|
|
|
{
|
2013-06-03 23:17:36 +00:00
|
|
|
struct priv *p = ao->priv;
|
|
|
|
|
|
|
|
IDirectSoundBuffer_Play(p->hdsbuf, 0, 0, DSBPLAY_LOOPING);
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
|
|
|
|
2009-05-13 02:58:57 +00:00
|
|
|
/**
|
2004-09-25 15:34:42 +00:00
|
|
|
\brief close audio device
|
2005-01-21 13:22:59 +00:00
|
|
|
\param immed stop playback immediately
|
2004-09-25 15:34:42 +00:00
|
|
|
*/
|
2013-06-03 22:59:53 +00:00
|
|
|
static void uninit(struct ao *ao, bool immed)
|
2004-09-25 15:34:42 +00:00
|
|
|
{
|
2013-06-03 22:51:07 +00:00
|
|
|
if (!immed)
|
2013-06-03 22:59:53 +00:00
|
|
|
mp_sleep_us(get_delay(ao) * 1000000);
|
|
|
|
reset(ao);
|
2012-03-12 00:14:47 +00:00
|
|
|
|
2013-06-03 23:17:36 +00:00
|
|
|
DestroyBuffer(ao);
|
|
|
|
UninitDirectSound(ao);
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
|
|
|
|
2012-03-12 00:14:47 +00:00
|
|
|
// return exact number of free (safe to write) bytes
|
2013-06-03 22:59:53 +00:00
|
|
|
static int check_free_buffer_size(struct ao *ao)
|
2004-09-25 15:34:42 +00:00
|
|
|
{
|
2013-06-03 23:17:36 +00:00
|
|
|
struct priv *p = ao->priv;
|
2013-06-03 22:51:07 +00:00
|
|
|
int space;
|
|
|
|
DWORD play_offset;
|
2013-06-03 23:17:36 +00:00
|
|
|
IDirectSoundBuffer_GetCurrentPosition(p->hdsbuf, &play_offset, NULL);
|
|
|
|
space = p->buffer_size - (p->write_offset - play_offset);
|
2013-06-03 22:51:07 +00:00
|
|
|
// | | <-- const --> | | |
|
2013-06-03 23:17:36 +00:00
|
|
|
// buffer start play_cursor write_cursor p->write_offset buffer end
|
2013-06-03 22:51:07 +00:00
|
|
|
// play_cursor is the actual postion of the play cursor
|
|
|
|
// write_cursor is the position after which it is assumed to be save to write data
|
2013-06-03 23:17:36 +00:00
|
|
|
// p->write_offset is the postion where we actually write the data to
|
|
|
|
if (space > p->buffer_size)
|
|
|
|
space -= p->buffer_size; // p->write_offset < play_offset
|
2013-06-03 22:51:07 +00:00
|
|
|
// Check for buffer underruns. An underrun happens if DirectSound
|
2013-06-03 23:17:36 +00:00
|
|
|
// started to play old data beyond the current p->write_offset. Detect this
|
2013-06-03 22:51:07 +00:00
|
|
|
// by checking whether the free space shrinks, even though no data was
|
|
|
|
// written (i.e. no write_buffer). Doesn't always work, but the only
|
|
|
|
// reason we need this is to deal with the situation when playback ends,
|
|
|
|
// and the buffer is only half-filled.
|
2013-06-03 23:17:36 +00:00
|
|
|
if (space < p->underrun_check) {
|
2013-06-03 22:51:07 +00:00
|
|
|
// there's no useful data in the buffers
|
2013-06-03 23:17:36 +00:00
|
|
|
space = p->buffer_size;
|
2013-06-03 22:59:53 +00:00
|
|
|
reset(ao);
|
2013-06-03 22:51:07 +00:00
|
|
|
}
|
2013-06-03 23:17:36 +00:00
|
|
|
p->underrun_check = space;
|
2013-06-03 22:51:07 +00:00
|
|
|
return space;
|
2012-03-12 00:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-03 22:51:07 +00:00
|
|
|
\brief find out how many bytes can be written into the audio buffer without
|
|
|
|
\return free space in bytes, has to return 0 if the buffer is almost full
|
|
|
|
*/
|
2013-06-03 22:59:53 +00:00
|
|
|
static int get_space(struct ao *ao)
|
2012-03-12 00:14:47 +00:00
|
|
|
{
|
2013-06-03 23:17:36 +00:00
|
|
|
struct priv *p = ao->priv;
|
|
|
|
|
2013-06-03 22:59:53 +00:00
|
|
|
int space = check_free_buffer_size(ao);
|
2013-06-03 23:17:36 +00:00
|
|
|
if (space < p->min_free_space)
|
2013-06-03 22:51:07 +00:00
|
|
|
return 0;
|
2013-06-03 23:17:36 +00:00
|
|
|
return space - p->min_free_space;
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief play 'len' bytes of 'data'
|
|
|
|
\param data pointer to the data to play
|
|
|
|
\param len size in bytes of the data buffer, gets rounded down to outburst*n
|
|
|
|
\param flags currently unused
|
|
|
|
\return number of played bytes
|
|
|
|
*/
|
2013-06-03 22:59:53 +00:00
|
|
|
static int play(struct ao *ao, void *data, int len, int flags)
|
2004-09-25 15:34:42 +00:00
|
|
|
{
|
2013-06-16 20:17:25 +00:00
|
|
|
struct priv *p = ao->priv;
|
|
|
|
|
2013-06-03 22:59:53 +00:00
|
|
|
int space = check_free_buffer_size(ao);
|
2013-06-03 22:51:07 +00:00
|
|
|
if (space < len)
|
|
|
|
len = space;
|
2004-10-18 19:23:13 +00:00
|
|
|
|
2013-06-03 22:51:07 +00:00
|
|
|
if (!(flags & AOPLAY_FINAL_CHUNK))
|
2013-06-16 17:15:32 +00:00
|
|
|
len = (len / p->outburst) * p->outburst;
|
2013-06-03 22:59:53 +00:00
|
|
|
return write_buffer(ao, data, len);
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief get the delay between the first and last sample in the buffer
|
|
|
|
\return delay in seconds
|
|
|
|
*/
|
2013-06-03 22:59:53 +00:00
|
|
|
static float get_delay(struct ao *ao)
|
2004-09-25 15:34:42 +00:00
|
|
|
{
|
2013-06-03 23:17:36 +00:00
|
|
|
struct priv *p = ao->priv;
|
|
|
|
|
2013-06-03 22:59:53 +00:00
|
|
|
int space = check_free_buffer_size(ao);
|
2013-06-03 23:17:36 +00:00
|
|
|
return (float)(p->buffer_size - space) / (float)ao->bps;
|
2004-09-25 15:34:42 +00:00
|
|
|
}
|
2013-06-03 22:59:53 +00:00
|
|
|
|
2013-07-21 22:11:06 +00:00
|
|
|
#define OPT_BASE_STRUCT struct priv
|
|
|
|
|
2013-06-03 22:59:53 +00:00
|
|
|
const struct ao_driver audio_out_dsound = {
|
|
|
|
.info = &(const struct ao_info) {
|
|
|
|
"Windows DirectSound audio output",
|
|
|
|
"dsound",
|
|
|
|
"Gabor Szecsi <deje@miki.hu>",
|
|
|
|
""
|
|
|
|
},
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.control = control,
|
|
|
|
.get_space = get_space,
|
|
|
|
.play = play,
|
|
|
|
.get_delay = get_delay,
|
|
|
|
.pause = audio_pause,
|
|
|
|
.resume = audio_resume,
|
|
|
|
.reset = reset,
|
2013-07-21 22:11:06 +00:00
|
|
|
.priv_size = sizeof(struct priv),
|
|
|
|
.options = (const struct m_option[]) {
|
|
|
|
OPT_INT("device", cfg_device, 0),
|
|
|
|
{0}
|
|
|
|
},
|
2013-06-03 22:59:53 +00:00
|
|
|
};
|