| 1 |
/* |
| 2 |
win32: audio output for Windows 32bit |
| 3 |
|
| 4 |
copyright ?-2013 by the mpg123 project - free software under the terms of the LGPL 2.1 |
| 5 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 6 |
|
| 7 |
initially written (as it seems) by Tony Million |
| 8 |
rewrite of basic functionality for callback-less and properly ringbuffered operation by ravenexp |
| 9 |
Closing buffer playback fixed by David Wohlferd <limegreensocks (*) yahoo dod com> |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "../out123_int.h" |
| 13 |
#include <windows.h> |
| 14 |
#include "../../common/debug.h" |
| 15 |
|
| 16 |
/* |
| 17 |
Buffer size and number of buffers in the playback ring |
| 18 |
NOTE: This particular num/size combination performs best under heavy |
| 19 |
loads for my system, however this may not be true for any hardware/OS out there. |
| 20 |
Generally, BUFFER_SIZE < 8k || NUM_BUFFERS > 16 || NUM_BUFFERS < 4 are not recommended. |
| 21 |
|
| 22 |
Introducing user-settable device buffer. We fix 8 buffers, scale the individual |
| 23 |
buffer size, rounded/truncated a bit to not be too odd. The old default of 64K |
| 24 |
buffers leads to 2.97 s with CD-DA. Quite excessive. We will probably reduce this |
| 25 |
soon. |
| 26 |
*/ |
| 27 |
#define DEFAULT_DEVICE_BUFFER 0.25 |
| 28 |
// Buffers are multiples of this. |
| 29 |
#define BUFFER_GRANULARITY 256 |
| 30 |
#define NUM_BUFFERS 8 |
| 31 |
|
| 32 |
static void wait_for_buffer(WAVEHDR* hdr, HANDLE hEvent); |
| 33 |
static void drain_win32(out123_handle *ao); |
| 34 |
|
| 35 |
/* Buffer ring queue state */ |
| 36 |
struct queue_state |
| 37 |
{ |
| 38 |
WAVEHDR buffer_headers[NUM_BUFFERS]; |
| 39 |
size_t bufsize; |
| 40 |
/* The next buffer to be filled and put in playback */ |
| 41 |
int next_buffer; |
| 42 |
/* Buffer playback completion event */ |
| 43 |
HANDLE play_done_event; |
| 44 |
HWAVEOUT waveout; |
| 45 |
}; |
| 46 |
|
| 47 |
static UINT dev_select(out123_handle *ao){ |
| 48 |
UINT ret; |
| 49 |
if(ao->device) { |
| 50 |
sscanf(ao->device, "%u", &ret); |
| 51 |
} else { |
| 52 |
ret = WAVE_MAPPER; |
| 53 |
} |
| 54 |
return ret; |
| 55 |
} |
| 56 |
|
| 57 |
static int open_win32(out123_handle *ao) |
| 58 |
{ |
| 59 |
struct queue_state* state; |
| 60 |
int i; |
| 61 |
MMRESULT res; |
| 62 |
WAVEFORMATEX out_fmt; |
| 63 |
UINT dev_id; |
| 64 |
|
| 65 |
if(!ao) return -1; |
| 66 |
if(ao->rate == -1) return 0; |
| 67 |
|
| 68 |
/* only 8 and 16 supported */ |
| 69 |
if(!(ao->format == MPG123_ENC_SIGNED_8 || ao->format == MPG123_ENC_SIGNED_16)) return -1; |
| 70 |
/* only mono and stereo supported */ |
| 71 |
if(!(ao->channels == 1 || ao->channels == 2)) return -1; |
| 72 |
|
| 73 |
/* Allocate queue state struct for this device */ |
| 74 |
state = calloc(1, sizeof(struct queue_state)); |
| 75 |
if(!state) return -1; |
| 76 |
|
| 77 |
ao->userptr = state; |
| 78 |
|
| 79 |
state->play_done_event = CreateEvent(0,FALSE,FALSE,0); |
| 80 |
if(state->play_done_event == INVALID_HANDLE_VALUE) return -1; |
| 81 |
|
| 82 |
dev_id = dev_select(ao); |
| 83 |
|
| 84 |
out_fmt.wFormatTag = WAVE_FORMAT_PCM; |
| 85 |
out_fmt.wBitsPerSample = ao->format == MPG123_ENC_SIGNED_8 ? 8 : 16; |
| 86 |
out_fmt.nChannels = ao->channels; |
| 87 |
out_fmt.nSamplesPerSec = ao->rate; |
| 88 |
out_fmt.nBlockAlign = out_fmt.nChannels*out_fmt.wBitsPerSample/8; |
| 89 |
out_fmt.nAvgBytesPerSec = out_fmt.nBlockAlign*out_fmt.nSamplesPerSec; |
| 90 |
out_fmt.cbSize = 0; |
| 91 |
|
| 92 |
res = waveOutOpen(&state->waveout, dev_id, &out_fmt, |
| 93 |
(DWORD_PTR)state->play_done_event, 0, CALLBACK_EVENT); |
| 94 |
|
| 95 |
switch(res) |
| 96 |
{ |
| 97 |
case MMSYSERR_NOERROR: |
| 98 |
break; |
| 99 |
case MMSYSERR_ALLOCATED: |
| 100 |
ereturn(-1, "Audio output device is already allocated."); |
| 101 |
case MMSYSERR_NODRIVER: |
| 102 |
ereturn(-1, "No device driver is present."); |
| 103 |
case MMSYSERR_NOMEM: |
| 104 |
ereturn(-1, "Unable to allocate or lock memory."); |
| 105 |
case WAVERR_BADFORMAT: |
| 106 |
ereturn(-1, "Unsupported waveform-audio format."); |
| 107 |
default: |
| 108 |
ereturn(-1, "Unable to open wave output device."); |
| 109 |
} |
| 110 |
|
| 111 |
state->bufsize = (size_t)( (double) |
| 112 |
(ao->device_buffer > 0. ? ao->device_buffer : DEFAULT_DEVICE_BUFFER) |
| 113 |
* out_fmt.nAvgBytesPerSec / NUM_BUFFERS / BUFFER_GRANULARITY); |
| 114 |
if(state->bufsize < 1) |
| 115 |
state->bufsize = 1; |
| 116 |
state->bufsize *= BUFFER_GRANULARITY; |
| 117 |
|
| 118 |
/* Reset event from the "device open" message */ |
| 119 |
ResetEvent(state->play_done_event); |
| 120 |
/* Allocate playback buffers */ |
| 121 |
for(i = 0; i < NUM_BUFFERS; i++) |
| 122 |
if(!(state->buffer_headers[i].lpData = (LPSTR)malloc(state->bufsize))) |
| 123 |
{ |
| 124 |
ereturn(-1, "Out of memory for playback buffers."); |
| 125 |
} |
| 126 |
else |
| 127 |
{ |
| 128 |
/* Tell waveOutPrepareHeader the maximum value of dwBufferLength |
| 129 |
we will ever send */ |
| 130 |
state->buffer_headers[i].dwBufferLength = state->bufsize; |
| 131 |
state->buffer_headers[i].dwFlags = 0; |
| 132 |
res = waveOutPrepareHeader(state->waveout, &state->buffer_headers[i], sizeof(WAVEHDR)); |
| 133 |
if(res != MMSYSERR_NOERROR) ereturn(-1, "Can't write to audio output device (prepare)."); |
| 134 |
|
| 135 |
/* set the current size of the buffer to 0 */ |
| 136 |
state->buffer_headers[i].dwBufferLength = 0; |
| 137 |
|
| 138 |
/* set flags to unprepared - must reset this to WHDR_PREPARED before calling write */ |
| 139 |
state->buffer_headers[i].dwFlags = 0; |
| 140 |
} |
| 141 |
|
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
static void wait_for_buffer(WAVEHDR* hdr, HANDLE hEvent) |
| 146 |
{ |
| 147 |
/* At this point there are several possible states: |
| 148 |
1) Empty or partial buffer (unqueued) - dwFlags == 0 |
| 149 |
2) Buffer queued or being played - dwFlags == WHDR_PREPARED | WHDR_INQUEUE |
| 150 |
3) Buffer unqueued and finished being played - dwFlags == WHDR_PREPARED | WHDR_DONE |
| 151 |
4) Buffer removed from queue, but not yet marked as done - dwFlags == WHDR_PREPARED |
| 152 |
*/ |
| 153 |
|
| 154 |
/* Check buffer header and wait if it's being played. */ |
| 155 |
if (hdr->dwFlags & WHDR_PREPARED) |
| 156 |
{ |
| 157 |
while(!(hdr->dwFlags & WHDR_DONE)) |
| 158 |
{ |
| 159 |
/*debug1("waiting for buffer %i...", state->next_buffer);*/ |
| 160 |
/* Waits for *a* buffer to finish. May not be the one we |
| 161 |
want, so check again */ |
| 162 |
WaitForSingleObject(hEvent, INFINITE); |
| 163 |
} |
| 164 |
hdr->dwFlags = 0; |
| 165 |
hdr->dwBufferLength = 0; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
static int get_formats_win32(out123_handle *ao) |
| 170 |
{ |
| 171 |
WAVEOUTCAPSA caps; |
| 172 |
int ret = 0; |
| 173 |
UINT dev_id = dev_select(ao); |
| 174 |
|
| 175 |
MMRESULT mr = waveOutGetDevCapsA(dev_id, &caps, sizeof(caps)); |
| 176 |
if(mr != MMSYSERR_NOERROR) |
| 177 |
return 0; /* no formats? */ |
| 178 |
|
| 179 |
if(ao->channels == 1) { |
| 180 |
switch(ao->rate) { |
| 181 |
case 44100: |
| 182 |
if(caps.dwFormats & WAVE_FORMAT_4M08) |
| 183 |
ret |= MPG123_ENC_SIGNED_8; |
| 184 |
if(caps.dwFormats & WAVE_FORMAT_4M16) |
| 185 |
ret |= MPG123_ENC_SIGNED_16; |
| 186 |
break; |
| 187 |
case 22050: |
| 188 |
if(caps.dwFormats & WAVE_FORMAT_2M08) |
| 189 |
ret |= MPG123_ENC_SIGNED_8; |
| 190 |
if(caps.dwFormats & WAVE_FORMAT_2M16) |
| 191 |
ret |= MPG123_ENC_SIGNED_16; |
| 192 |
break; |
| 193 |
case 11025: |
| 194 |
if(caps.dwFormats & WAVE_FORMAT_1M08) |
| 195 |
ret |= MPG123_ENC_SIGNED_8; |
| 196 |
if(caps.dwFormats & WAVE_FORMAT_1M16) |
| 197 |
ret |= MPG123_ENC_SIGNED_16; |
| 198 |
break; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if(ao->channels == 2) { |
| 203 |
switch(ao->rate) { |
| 204 |
case 44100: |
| 205 |
if(caps.dwFormats & WAVE_FORMAT_4S08) |
| 206 |
ret |= MPG123_ENC_SIGNED_8; |
| 207 |
if(caps.dwFormats & WAVE_FORMAT_4S16) |
| 208 |
ret |= MPG123_ENC_SIGNED_16; |
| 209 |
break; |
| 210 |
case 22050: |
| 211 |
if(caps.dwFormats & WAVE_FORMAT_2S08) |
| 212 |
ret |= MPG123_ENC_SIGNED_8; |
| 213 |
if(caps.dwFormats & WAVE_FORMAT_2S16) |
| 214 |
ret |= MPG123_ENC_SIGNED_16; |
| 215 |
break; |
| 216 |
case 11025: |
| 217 |
if(caps.dwFormats & WAVE_FORMAT_2S08) |
| 218 |
ret |= MPG123_ENC_SIGNED_8; |
| 219 |
if(caps.dwFormats & WAVE_FORMAT_2S16) |
| 220 |
ret |= MPG123_ENC_SIGNED_16; |
| 221 |
break; |
| 222 |
} |
| 223 |
} |
| 224 |
return ret; |
| 225 |
} |
| 226 |
|
| 227 |
/* Stores audio data to the fixed size buffers and pushes them into the playback queue. |
| 228 |
I have one grief with that: The last piece of a track may not reach the output, |
| 229 |
only full buffers sent... But we don't get smooth audio otherwise. */ |
| 230 |
static int write_win32(out123_handle *ao, unsigned char *buf, int len) |
| 231 |
{ |
| 232 |
struct queue_state* state; |
| 233 |
MMRESULT res; |
| 234 |
WAVEHDR* hdr; |
| 235 |
|
| 236 |
int rest_len; /* Input data bytes left for next recursion. */ |
| 237 |
int bufill; /* Bytes we stuff into buffer now. */ |
| 238 |
|
| 239 |
if(!ao || !ao->userptr) return -1; |
| 240 |
if(!buf || len <= 0) return 0; |
| 241 |
|
| 242 |
state = (struct queue_state*)ao->userptr; |
| 243 |
hdr = &state->buffer_headers[state->next_buffer]; |
| 244 |
|
| 245 |
wait_for_buffer(hdr, state->play_done_event); |
| 246 |
|
| 247 |
/* Now see how much we want to stuff in and then stuff it in. */ |
| 248 |
bufill = state->bufsize - hdr->dwBufferLength; |
| 249 |
if(len < bufill) bufill = len; |
| 250 |
|
| 251 |
rest_len = len - bufill; |
| 252 |
memcpy(hdr->lpData + hdr->dwBufferLength, buf, bufill); |
| 253 |
hdr->dwBufferLength += bufill; |
| 254 |
if(hdr->dwBufferLength == state->bufsize) |
| 255 |
{ /* Send the buffer out when it's full. */ |
| 256 |
hdr->dwFlags |= WHDR_PREPARED; |
| 257 |
|
| 258 |
res = waveOutWrite(state->waveout, hdr, sizeof(WAVEHDR)); |
| 259 |
if(res != MMSYSERR_NOERROR) ereturn(-1, "Can't write to audio output device."); |
| 260 |
|
| 261 |
/* Cycle to the next buffer in the ring queue */ |
| 262 |
state->next_buffer = (state->next_buffer + 1) % NUM_BUFFERS; |
| 263 |
} |
| 264 |
/* I'd like to propagate error codes or something... but there are no catchable surprises left. |
| 265 |
Anyhow: Here is the recursion that makes ravenexp happy;-) */ |
| 266 |
if(rest_len && write_win32(ao, buf + bufill, rest_len) < 0) /* Write the rest. */ |
| 267 |
return -1; |
| 268 |
else |
| 269 |
return len; |
| 270 |
} |
| 271 |
|
| 272 |
/* Flush means abort any pending playback */ |
| 273 |
static void flush_win32(out123_handle *ao) |
| 274 |
{ |
| 275 |
struct queue_state* state; |
| 276 |
WAVEHDR* hdr; |
| 277 |
|
| 278 |
if(!ao || !ao->userptr) return; |
| 279 |
state = (struct queue_state*)ao->userptr; |
| 280 |
|
| 281 |
/* Cancel any buffers in queue. Ignore errors since we are void and |
| 282 |
can't return them anyway */ |
| 283 |
waveOutReset(state->waveout); |
| 284 |
|
| 285 |
/* Discard any partial buffer */ |
| 286 |
hdr = &state->buffer_headers[state->next_buffer]; |
| 287 |
|
| 288 |
/* If WHDR_PREPARED is not set, this is (potentially) a partial buffer */ |
| 289 |
if (!(hdr->dwFlags & WHDR_PREPARED)) |
| 290 |
hdr->dwBufferLength = 0; |
| 291 |
|
| 292 |
/* Finish processing the buffers */ |
| 293 |
drain_win32(ao); |
| 294 |
} |
| 295 |
|
| 296 |
/* output final buffer (if any) */ |
| 297 |
static void write_final_buffer(struct queue_state *state) |
| 298 |
{ |
| 299 |
WAVEHDR* hdr; |
| 300 |
hdr = &state->buffer_headers[state->next_buffer]; |
| 301 |
if((!(hdr->dwFlags & WHDR_PREPARED)) && (hdr->dwBufferLength != 0)) |
| 302 |
{ |
| 303 |
hdr->dwFlags |= WHDR_PREPARED; |
| 304 |
/* ignore any errors */ |
| 305 |
waveOutWrite(state->waveout, hdr, sizeof(WAVEHDR)); |
| 306 |
|
| 307 |
/* Cycle to the next buffer in the ring queue */ |
| 308 |
state->next_buffer = (state->next_buffer + 1) % NUM_BUFFERS; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
/* Note: I tried to fix this stuff without testing. |
| 313 |
There were some obvious errors in the code. |
| 314 |
Someone run this on a win32 machine! -- ThOr */ |
| 315 |
static void drain_win32(out123_handle *ao) |
| 316 |
{ |
| 317 |
int i, z; |
| 318 |
struct queue_state* state; |
| 319 |
|
| 320 |
if(!ao || !ao->userptr) return; |
| 321 |
state = (struct queue_state*)ao->userptr; |
| 322 |
|
| 323 |
/* output final buffer (if any) */ |
| 324 |
write_final_buffer(state); |
| 325 |
|
| 326 |
/* I _think_ I understood how this should work. -- ThOr */ |
| 327 |
z = state->next_buffer; |
| 328 |
for(i = 0; i < NUM_BUFFERS; i++) |
| 329 |
{ |
| 330 |
wait_for_buffer(&state->buffer_headers[z], state->play_done_event); |
| 331 |
z = (z + 1) % NUM_BUFFERS; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
static int close_win32(out123_handle *ao) |
| 336 |
{ |
| 337 |
int i; |
| 338 |
struct queue_state* state; |
| 339 |
|
| 340 |
if(!ao || !ao->userptr) return -1; |
| 341 |
state = (struct queue_state*)ao->userptr; |
| 342 |
|
| 343 |
/* wait for all active buffers to complete */ |
| 344 |
drain_win32(ao); |
| 345 |
CloseHandle(state->play_done_event); |
| 346 |
|
| 347 |
for(i = 0; i < NUM_BUFFERS; i++) |
| 348 |
{ |
| 349 |
state->buffer_headers[i].dwFlags |= WHDR_PREPARED; |
| 350 |
waveOutUnprepareHeader(state->waveout, &state->buffer_headers[i], sizeof(WAVEHDR)); |
| 351 |
free(state->buffer_headers[i].lpData); |
| 352 |
} |
| 353 |
|
| 354 |
waveOutClose(state->waveout); |
| 355 |
free(ao->userptr); |
| 356 |
ao->userptr = 0; |
| 357 |
return 0; |
| 358 |
} |
| 359 |
|
| 360 |
static int enumerate_win32( out123_handle *ao, int (*store_device)(void *devlist |
| 361 |
, const char *name, const char *description), void *devlist ) |
| 362 |
{ |
| 363 |
char id[10]; |
| 364 |
WAVEOUTCAPSA caps; |
| 365 |
MMRESULT mr; |
| 366 |
UINT i, devices = waveOutGetNumDevs(); |
| 367 |
for(i = 0; i < devices; i++){ |
| 368 |
memset(id, 0, sizeof(id)); |
| 369 |
memset(&caps, 0, sizeof(caps)); |
| 370 |
mr = waveOutGetDevCapsA(i, &caps, sizeof(caps)); |
| 371 |
if (mr != MMSYSERR_NOERROR) { |
| 372 |
switch(mr) { |
| 373 |
case MMSYSERR_BADDEVICEID: |
| 374 |
error("enumerate_win32: Specified device identifier is out of range."); |
| 375 |
break; |
| 376 |
case MMSYSERR_NODRIVER: |
| 377 |
error("enumerate_win32: No device driver is present."); |
| 378 |
break; |
| 379 |
case MMSYSERR_NOMEM: |
| 380 |
error("enumerate_win32: Unable to allocate or lock memory."); |
| 381 |
break; |
| 382 |
default: |
| 383 |
merror("enumerate_win32: Uknown error 0x%x.", mr); |
| 384 |
} |
| 385 |
} |
| 386 |
mdebug("waveOutGetDevCaps mr %x", mr); |
| 387 |
snprintf(id, sizeof(id) - 1, "%u", i); |
| 388 |
store_device(devlist, id, caps.szPname); |
| 389 |
} |
| 390 |
return 0; |
| 391 |
} |
| 392 |
|
| 393 |
static int init_win32(out123_handle* ao) |
| 394 |
{ |
| 395 |
if(!ao) return -1; |
| 396 |
|
| 397 |
/* Set callbacks */ |
| 398 |
ao->open = open_win32; |
| 399 |
ao->flush = flush_win32; |
| 400 |
ao->write = write_win32; |
| 401 |
ao->get_formats = get_formats_win32; |
| 402 |
ao->close = close_win32; |
| 403 |
ao->enumerate = enumerate_win32; |
| 404 |
|
| 405 |
/* Success */ |
| 406 |
return 0; |
| 407 |
} |
| 408 |
|
| 409 |
/* |
| 410 |
Module information data structure |
| 411 |
*/ |
| 412 |
mpg123_module_t mpg123_output_module_info = { |
| 413 |
/* api_version */ MPG123_MODULE_API_VERSION, |
| 414 |
/* name */ "win32", |
| 415 |
/* description */ "Audio output for Windows (winmm).", |
| 416 |
/* revision */ "$Rev:$", |
| 417 |
/* handle */ NULL, |
| 418 |
|
| 419 |
/* init_output */ init_win32, |
| 420 |
}; |