2008-05-14 18:02:27 +00:00
|
|
|
/*
|
|
|
|
* Windows waveOut interface
|
2002-10-25 16:18:33 +00:00
|
|
|
*
|
2008-05-14 18:02:27 +00:00
|
|
|
* Copyright (c) 2002 - 2004 Sascha Sommer <saschasommer@freenet.de>
|
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
2002-10-25 16:18:33 +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,
|
2002-10-25 16:18:33 +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.
|
|
|
|
*/
|
2002-10-25 16:18:33 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <mmsystem.h>
|
|
|
|
|
2005-01-12 22:00:02 +00:00
|
|
|
#include "config.h"
|
2004-12-27 17:30:15 +00:00
|
|
|
#include "libaf/af_format.h"
|
2002-10-25 16:18:33 +00:00
|
|
|
#include "audio_out.h"
|
|
|
|
#include "audio_out_internal.h"
|
2004-12-07 02:24:15 +00:00
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "libvo/fastmemcpy.h"
|
2004-04-01 19:33:58 +00:00
|
|
|
#include "osdep/timer.h"
|
2002-10-25 16:18:33 +00:00
|
|
|
|
2004-06-27 11:19:11 +00:00
|
|
|
#define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
|
2004-08-14 10:03:26 +00:00
|
|
|
#define WAVE_FORMAT_EXTENSIBLE 0xFFFE
|
|
|
|
|
|
|
|
static const GUID KSDATAFORMAT_SUBTYPE_PCM = {
|
|
|
|
0x1,0x0000,0x0010,{0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
WAVEFORMATEX Format;
|
|
|
|
union {
|
|
|
|
WORD wValidBitsPerSample;
|
|
|
|
WORD wSamplesPerBlock;
|
|
|
|
WORD wReserved;
|
|
|
|
} Samples;
|
|
|
|
DWORD dwChannelMask;
|
|
|
|
GUID SubFormat;
|
|
|
|
} WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
|
|
|
|
|
|
|
|
#define SPEAKER_FRONT_LEFT 0x1
|
|
|
|
#define SPEAKER_FRONT_RIGHT 0x2
|
|
|
|
#define SPEAKER_FRONT_CENTER 0x4
|
|
|
|
#define SPEAKER_LOW_FREQUENCY 0x8
|
|
|
|
#define SPEAKER_BACK_LEFT 0x10
|
|
|
|
#define SPEAKER_BACK_RIGHT 0x20
|
|
|
|
#define SPEAKER_FRONT_LEFT_OF_CENTER 0x40
|
|
|
|
#define SPEAKER_FRONT_RIGHT_OF_CENTER 0x80
|
|
|
|
#define SPEAKER_BACK_CENTER 0x100
|
|
|
|
#define SPEAKER_SIDE_LEFT 0x200
|
|
|
|
#define SPEAKER_SIDE_RIGHT 0x400
|
|
|
|
#define SPEAKER_TOP_CENTER 0x800
|
|
|
|
#define SPEAKER_TOP_FRONT_LEFT 0x1000
|
|
|
|
#define SPEAKER_TOP_FRONT_CENTER 0x2000
|
|
|
|
#define SPEAKER_TOP_FRONT_RIGHT 0x4000
|
|
|
|
#define SPEAKER_TOP_BACK_LEFT 0x8000
|
|
|
|
#define SPEAKER_TOP_BACK_CENTER 0x10000
|
|
|
|
#define SPEAKER_TOP_BACK_RIGHT 0x20000
|
|
|
|
|
|
|
|
static const int channel_mask[] = {
|
|
|
|
SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_LOW_FREQUENCY,
|
|
|
|
SPEAKER_FRONT_LEFT | SPEAKER_FRONT_CENTER | SPEAKER_FRONT_RIGHT | SPEAKER_LOW_FREQUENCY,
|
|
|
|
SPEAKER_FRONT_LEFT | SPEAKER_FRONT_CENTER | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_CENTER | SPEAKER_LOW_FREQUENCY,
|
|
|
|
SPEAKER_FRONT_LEFT | SPEAKER_FRONT_CENTER | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-06-27 11:19:11 +00:00
|
|
|
|
2004-04-07 22:27:33 +00:00
|
|
|
#define SAMPLESIZE 1024
|
2002-10-25 16:18:33 +00:00
|
|
|
#define BUFFER_SIZE 4096
|
2004-04-07 22:27:33 +00:00
|
|
|
#define BUFFER_COUNT 16
|
2002-10-25 16:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
static WAVEHDR* waveBlocks; //pointer to our ringbuffer memory
|
|
|
|
static HWAVEOUT hWaveOut; //handle to the waveout device
|
|
|
|
static unsigned int buf_write=0;
|
2009-03-06 19:23:24 +00:00
|
|
|
static volatile int buf_read=0;
|
2002-10-25 16:18:33 +00:00
|
|
|
|
|
|
|
|
2009-03-06 19:43:12 +00:00
|
|
|
static const ao_info_t info =
|
2002-10-25 16:18:33 +00:00
|
|
|
{
|
|
|
|
"Windows waveOut audio output",
|
|
|
|
"win32",
|
|
|
|
"Sascha Sommer <saschasommer@freenet.de>",
|
|
|
|
""
|
|
|
|
};
|
|
|
|
|
|
|
|
LIBAO_EXTERN(win32)
|
|
|
|
|
2009-03-06 19:10:54 +00:00
|
|
|
static void CALLBACK waveOutProc(HWAVEOUT hWaveOut,UINT uMsg,DWORD dwInstance,
|
2002-10-25 16:18:33 +00:00
|
|
|
DWORD dwParam1,DWORD dwParam2)
|
|
|
|
{
|
|
|
|
if(uMsg != WOM_DONE)
|
|
|
|
return;
|
2009-03-06 19:23:24 +00:00
|
|
|
buf_read = (buf_read + 1) % BUFFER_COUNT;
|
2002-10-25 16:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// to set/get/query special features/parameters
|
2003-03-21 16:42:50 +00:00
|
|
|
static int control(int cmd,void *arg)
|
2002-10-25 16:18:33 +00:00
|
|
|
{
|
|
|
|
DWORD volume;
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case AOCONTROL_GET_VOLUME:
|
|
|
|
{
|
|
|
|
ao_control_vol_t* vol = (ao_control_vol_t*)arg;
|
|
|
|
waveOutGetVolume(hWaveOut,&volume);
|
|
|
|
vol->left = (float)(LOWORD(volume)/655.35);
|
|
|
|
vol->right = (float)(HIWORD(volume)/655.35);
|
|
|
|
mp_msg(MSGT_AO, MSGL_DBG2,"ao_win32: volume left:%f volume right:%f\n",vol->left,vol->right);
|
|
|
|
return CONTROL_OK;
|
|
|
|
}
|
|
|
|
case AOCONTROL_SET_VOLUME:
|
|
|
|
{
|
|
|
|
ao_control_vol_t* vol = (ao_control_vol_t*)arg;
|
|
|
|
volume = MAKELONG(vol->left*655.35,vol->right*655.35);
|
|
|
|
waveOutSetVolume(hWaveOut,volume);
|
|
|
|
return CONTROL_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// open & setup audio device
|
|
|
|
// return: 1=success 0=fail
|
|
|
|
static int init(int rate,int channels,int format,int flags)
|
|
|
|
{
|
2009-03-06 19:10:54 +00:00
|
|
|
WAVEFORMATEXTENSIBLE wformat;
|
2002-10-25 16:18:33 +00:00
|
|
|
MMRESULT result;
|
|
|
|
unsigned char* buffer;
|
|
|
|
int i;
|
2009-03-06 19:10:54 +00:00
|
|
|
|
2004-12-08 09:52:55 +00:00
|
|
|
switch(format){
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_AC3:
|
|
|
|
case AF_FORMAT_S24_LE:
|
|
|
|
case AF_FORMAT_S16_LE:
|
2009-03-06 13:22:48 +00:00
|
|
|
case AF_FORMAT_U8:
|
2004-12-08 09:52:55 +00:00
|
|
|
break;
|
|
|
|
default:
|
2004-12-28 19:11:14 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_V,"ao_win32: format %s not supported defaulting to Signed 16-bit Little-Endian\n",af_fmt2str_short(format));
|
2004-12-27 17:30:15 +00:00
|
|
|
format=AF_FORMAT_S16_LE;
|
2009-03-06 19:10:54 +00:00
|
|
|
}
|
2006-09-15 16:26:25 +00:00
|
|
|
|
|
|
|
// FIXME multichannel mode is buggy
|
|
|
|
if(channels > 2)
|
|
|
|
channels = 2;
|
2009-03-06 19:10:54 +00:00
|
|
|
|
|
|
|
//fill global ao_data
|
2002-10-25 16:18:33 +00:00
|
|
|
ao_data.channels=channels;
|
|
|
|
ao_data.samplerate=rate;
|
|
|
|
ao_data.format=format;
|
|
|
|
ao_data.bps=channels*rate;
|
2004-12-27 17:30:15 +00:00
|
|
|
if(format != AF_FORMAT_U8 && format != AF_FORMAT_S8)
|
2002-10-25 16:18:33 +00:00
|
|
|
ao_data.bps*=2;
|
2009-03-06 18:50:50 +00:00
|
|
|
ao_data.outburst = BUFFER_SIZE;
|
2002-10-25 16:18:33 +00:00
|
|
|
if(ao_data.buffersize==-1)
|
|
|
|
{
|
2004-12-28 19:11:14 +00:00
|
|
|
ao_data.buffersize=af_fmt2bits(format)/8;
|
2002-10-25 16:18:33 +00:00
|
|
|
ao_data.buffersize*= channels;
|
|
|
|
ao_data.buffersize*= SAMPLESIZE;
|
|
|
|
}
|
2004-12-28 19:11:14 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_V,"ao_win32: Samplerate:%iHz Channels:%i Format:%s\n",rate, channels, af_fmt2str_short(format));
|
2002-10-25 16:18:33 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_V,"ao_win32: Buffersize:%d\n",ao_data.buffersize);
|
2009-03-06 19:10:54 +00:00
|
|
|
|
2002-10-25 16:18:33 +00:00
|
|
|
//fill waveformatex
|
2004-08-14 10:03:26 +00:00
|
|
|
ZeroMemory( &wformat, sizeof(WAVEFORMATEXTENSIBLE));
|
2004-10-22 12:43:34 +00:00
|
|
|
wformat.Format.cbSize = (channels>2)?sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX):0;
|
2009-03-06 19:10:54 +00:00
|
|
|
wformat.Format.nChannels = channels;
|
|
|
|
wformat.Format.nSamplesPerSec = rate;
|
2004-12-27 17:30:15 +00:00
|
|
|
if(format == AF_FORMAT_AC3)
|
2004-06-26 11:20:20 +00:00
|
|
|
{
|
2004-08-14 10:03:26 +00:00
|
|
|
wformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
|
|
|
|
wformat.Format.wBitsPerSample = 16;
|
|
|
|
wformat.Format.nBlockAlign = 4;
|
2004-06-26 11:20:20 +00:00
|
|
|
}
|
2009-03-06 19:10:54 +00:00
|
|
|
else
|
2004-06-26 11:20:20 +00:00
|
|
|
{
|
2004-08-14 10:03:26 +00:00
|
|
|
wformat.Format.wFormatTag = (channels>2)?WAVE_FORMAT_EXTENSIBLE:WAVE_FORMAT_PCM;
|
2009-03-06 19:10:54 +00:00
|
|
|
wformat.Format.wBitsPerSample = af_fmt2bits(format);
|
2004-08-14 10:03:26 +00:00
|
|
|
wformat.Format.nBlockAlign = wformat.Format.nChannels * (wformat.Format.wBitsPerSample >> 3);
|
|
|
|
}
|
|
|
|
if(channels>2)
|
|
|
|
{
|
|
|
|
wformat.dwChannelMask = channel_mask[channels-3];
|
|
|
|
wformat.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
|
2004-12-28 19:11:14 +00:00
|
|
|
wformat.Samples.wValidBitsPerSample=af_fmt2bits(format);
|
2004-08-14 10:03:26 +00:00
|
|
|
}
|
2009-03-06 19:10:54 +00:00
|
|
|
|
2004-08-14 10:03:26 +00:00
|
|
|
wformat.Format.nAvgBytesPerSec = wformat.Format.nSamplesPerSec * wformat.Format.nBlockAlign;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2002-10-25 16:18:33 +00:00
|
|
|
//open sound device
|
|
|
|
//WAVE_MAPPER always points to the default wave device on the system
|
2005-01-17 21:19:59 +00:00
|
|
|
result = waveOutOpen(&hWaveOut,WAVE_MAPPER,(WAVEFORMATEX*)&wformat,(DWORD_PTR)waveOutProc,0,CALLBACK_FUNCTION);
|
2002-10-25 16:18:33 +00:00
|
|
|
if(result == WAVERR_BADFORMAT)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_AO, MSGL_ERR,"ao_win32: format not supported switching to default\n");
|
2004-08-14 10:03:26 +00:00
|
|
|
ao_data.channels = wformat.Format.nChannels = 2;
|
|
|
|
ao_data.samplerate = wformat.Format.nSamplesPerSec = 44100;
|
2004-12-27 17:30:15 +00:00
|
|
|
ao_data.format = AF_FORMAT_S16_LE;
|
2004-08-14 10:03:26 +00:00
|
|
|
ao_data.bps=ao_data.channels * ao_data.samplerate*2;
|
|
|
|
wformat.Format.wBitsPerSample=16;
|
|
|
|
wformat.Format.wFormatTag=WAVE_FORMAT_PCM;
|
|
|
|
wformat.Format.nBlockAlign = wformat.Format.nChannels * (wformat.Format.wBitsPerSample >> 3);
|
|
|
|
wformat.Format.nAvgBytesPerSec = wformat.Format.nSamplesPerSec * wformat.Format.nBlockAlign;
|
|
|
|
ao_data.buffersize=(wformat.Format.wBitsPerSample>>3)*wformat.Format.nChannels*SAMPLESIZE;
|
|
|
|
result = waveOutOpen(&hWaveOut,WAVE_MAPPER,(WAVEFORMATEX*)&wformat,(DWORD_PTR)waveOutProc,0,CALLBACK_FUNCTION);
|
2002-10-25 16:18:33 +00:00
|
|
|
}
|
|
|
|
if(result != MMSYSERR_NOERROR)
|
|
|
|
{
|
2005-01-15 19:27:00 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_ERR,"ao_win32: unable to open wave mapper device (result=%i)\n",result);
|
2002-10-25 16:18:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
//allocate buffer memory as one big block
|
2009-03-06 16:27:31 +00:00
|
|
|
buffer = calloc(BUFFER_COUNT, BUFFER_SIZE + sizeof(WAVEHDR));
|
2009-03-06 19:10:54 +00:00
|
|
|
//and setup pointers to each buffer
|
2002-10-25 16:18:33 +00:00
|
|
|
waveBlocks = (WAVEHDR*)buffer;
|
|
|
|
buffer += sizeof(WAVEHDR) * BUFFER_COUNT;
|
|
|
|
for(i = 0; i < BUFFER_COUNT; i++) {
|
|
|
|
waveBlocks[i].lpData = buffer;
|
|
|
|
buffer += BUFFER_SIZE;
|
|
|
|
}
|
2004-10-22 12:43:34 +00:00
|
|
|
buf_write=0;
|
2009-03-06 19:23:24 +00:00
|
|
|
buf_read=0;
|
2002-10-25 16:18:33 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// close audio device
|
2004-04-06 17:55:36 +00:00
|
|
|
static void uninit(int immed)
|
2002-10-25 16:18:33 +00:00
|
|
|
{
|
2009-03-05 22:07:06 +00:00
|
|
|
if(!immed)
|
|
|
|
usec_sleep(get_delay() * 1000 * 1000);
|
2009-03-09 19:15:20 +00:00
|
|
|
else
|
2002-10-25 16:18:33 +00:00
|
|
|
waveOutReset(hWaveOut);
|
2009-03-09 19:15:20 +00:00
|
|
|
while (waveOutClose(hWaveOut) == WAVERR_STILLPLAYING) usec_sleep(0);
|
2002-10-25 16:18:33 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_V,"waveOut device closed\n");
|
|
|
|
free(waveBlocks);
|
|
|
|
mp_msg(MSGT_AO, MSGL_V,"buffer memory freed\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop playing and empty buffers (for seeking/pause)
|
2006-07-06 04:30:19 +00:00
|
|
|
static void reset(void)
|
2002-10-25 16:18:33 +00:00
|
|
|
{
|
|
|
|
waveOutReset(hWaveOut);
|
|
|
|
buf_write=0;
|
2009-03-06 19:23:24 +00:00
|
|
|
buf_read=0;
|
2002-10-25 16:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop playing, keep buffers (for pause)
|
2006-07-06 04:30:19 +00:00
|
|
|
static void audio_pause(void)
|
2002-10-25 16:18:33 +00:00
|
|
|
{
|
|
|
|
waveOutPause(hWaveOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
// resume playing, after audio_pause()
|
2006-07-06 04:30:19 +00:00
|
|
|
static void audio_resume(void)
|
2002-10-25 16:18:33 +00:00
|
|
|
{
|
|
|
|
waveOutRestart(hWaveOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return: how many bytes can be played without blocking
|
2006-07-06 04:30:19 +00:00
|
|
|
static int get_space(void)
|
2002-10-25 16:18:33 +00:00
|
|
|
{
|
2009-03-06 19:23:24 +00:00
|
|
|
int free = buf_read - buf_write - 1;
|
|
|
|
if (free < 0) free += BUFFER_COUNT;
|
|
|
|
return free * BUFFER_SIZE;
|
2002-10-25 16:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//writes data into buffer, based on ringbuffer code in ao_sdl.c
|
|
|
|
static int write_waveOutBuffer(unsigned char* data,int len){
|
|
|
|
WAVEHDR* current;
|
|
|
|
int len2=0;
|
|
|
|
int x;
|
2009-03-06 19:10:54 +00:00
|
|
|
while(len>0){
|
2009-03-06 19:23:24 +00:00
|
|
|
int buf_next = (buf_write + 1) % BUFFER_COUNT;
|
2002-10-25 16:18:33 +00:00
|
|
|
current = &waveBlocks[buf_write];
|
2009-03-06 19:23:24 +00:00
|
|
|
if(buf_next == buf_read) break;
|
2002-10-25 16:18:33 +00:00
|
|
|
//unprepare the header if it is prepared
|
2009-03-06 19:10:54 +00:00
|
|
|
if(current->dwFlags & WHDR_PREPARED)
|
2002-10-25 16:18:33 +00:00
|
|
|
waveOutUnprepareHeader(hWaveOut, current, sizeof(WAVEHDR));
|
2009-03-06 19:10:54 +00:00
|
|
|
x=BUFFER_SIZE;
|
|
|
|
if(x>len) x=len;
|
|
|
|
fast_memcpy(current->lpData,data+len2,x);
|
|
|
|
len2+=x; len-=x;
|
2002-10-25 16:18:33 +00:00
|
|
|
//prepare header and write data to device
|
2009-03-06 18:50:50 +00:00
|
|
|
current->dwBufferLength = x;
|
2002-10-25 16:18:33 +00:00
|
|
|
waveOutPrepareHeader(hWaveOut, current, sizeof(WAVEHDR));
|
|
|
|
waveOutWrite(hWaveOut, current, sizeof(WAVEHDR));
|
2009-03-06 19:10:54 +00:00
|
|
|
|
2009-03-06 19:23:24 +00:00
|
|
|
buf_write = buf_next;
|
2002-10-25 16:18:33 +00:00
|
|
|
}
|
|
|
|
return len2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// plays 'len' bytes of 'data'
|
|
|
|
// it should round it down to outburst*n
|
|
|
|
// return: number of bytes played
|
|
|
|
static int play(void* data,int len,int flags)
|
|
|
|
{
|
2006-10-15 20:48:26 +00:00
|
|
|
if (!(flags & AOPLAY_FINAL_CHUNK))
|
2004-04-07 17:53:42 +00:00
|
|
|
len = (len/ao_data.outburst)*ao_data.outburst;
|
2002-10-25 16:18:33 +00:00
|
|
|
return write_waveOutBuffer(data,len);
|
|
|
|
}
|
2003-05-16 13:06:37 +00:00
|
|
|
|
2002-10-25 16:18:33 +00:00
|
|
|
// return: delay in seconds between first and last sample in buffer
|
2006-07-06 04:30:19 +00:00
|
|
|
static float get_delay(void)
|
2002-10-25 16:18:33 +00:00
|
|
|
{
|
2009-03-06 19:23:24 +00:00
|
|
|
int used = buf_write - buf_read;
|
|
|
|
if (used < 0) used += BUFFER_COUNT;
|
|
|
|
return (float)(used * BUFFER_SIZE + ao_data.buffersize)/(float)ao_data.bps;
|
2002-10-25 16:18:33 +00:00
|
|
|
}
|