Skip to content

Latest commit

 

History

History
785 lines (660 loc) · 25.8 KB

SDL_wasapi.c

File metadata and controls

785 lines (660 loc) · 25.8 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 3, 2018
Jan 3, 2018
3
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_AUDIO_DRIVER_WASAPI
#include "../../core/windows/SDL_windows.h"
#include "SDL_audio.h"
#include "SDL_timer.h"
#include "../SDL_audio_c.h"
#include "../SDL_sysaudio.h"
#include "SDL_assert.h"
#include "SDL_log.h"
#define COBJMACROS
#include <mmdeviceapi.h>
#include <audioclient.h>
#include "SDL_wasapi.h"
Feb 24, 2018
Feb 24, 2018
40
41
42
43
44
/* This constant isn't available on MinGW-w64 */
#ifndef AUDCLNT_STREAMFLAGS_RATEADJUST
#define AUDCLNT_STREAMFLAGS_RATEADJUST 0x00100000
#endif
Mar 30, 2017
Mar 30, 2017
45
/* these increment as default devices change. Opened default devices pick up changes in their threads. */
Dec 6, 2017
Dec 6, 2017
46
47
SDL_atomic_t WASAPI_DefaultPlaybackGeneration;
SDL_atomic_t WASAPI_DefaultCaptureGeneration;
Mar 30, 2017
Mar 30, 2017
48
49
50
51
/* This is a list of device id strings we have inflight, so we have consistent pointers to the same device. */
typedef struct DevIdList
{
May 18, 2017
May 18, 2017
52
53
WCHAR *str;
struct DevIdList *next;
54
55
56
57
58
} DevIdList;
static DevIdList *deviceid_list = NULL;
/* Some GUIDs we need to know without linking to libraries that aren't available before Vista. */
Dec 6, 2017
Dec 6, 2017
59
60
61
62
static const IID SDL_IID_IAudioRenderClient = { 0xf294acfc, 0x3146, 0x4483,{ 0xa7, 0xbf, 0xad, 0xdc, 0xa7, 0xc2, 0x60, 0xe2 } };
static const IID SDL_IID_IAudioCaptureClient = { 0xc8adbd64, 0xe71e, 0x48a0,{ 0xa4, 0xde, 0x18, 0x5c, 0x39, 0x5c, 0xd3, 0x17 } };
static const GUID SDL_KSDATAFORMAT_SUBTYPE_PCM = { 0x00000001, 0x0000, 0x0010,{ 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } };
static const GUID SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = { 0x00000003, 0x0000, 0x0010,{ 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } };
63
64
65
66
static SDL_bool
WStrEqual(const WCHAR *a, const WCHAR *b)
{
May 18, 2017
May 18, 2017
67
68
69
70
71
72
73
74
while (*a) {
if (*a != *b) {
return SDL_FALSE;
}
a++;
b++;
}
return *b == 0;
Dec 6, 2017
Dec 6, 2017
77
78
79
80
81
82
83
84
85
86
87
88
static size_t
WStrLen(const WCHAR *wstr)
{
size_t retval = 0;
if (wstr) {
while (*(wstr++)) {
retval++;
}
}
return retval;
}
89
90
91
static WCHAR *
WStrDupe(const WCHAR *wstr)
{
Dec 31, 2017
Dec 31, 2017
92
const size_t len = (WStrLen(wstr) + 1) * sizeof (WCHAR);
May 18, 2017
May 18, 2017
93
94
95
96
97
WCHAR *retval = (WCHAR *) SDL_malloc(len);
if (retval) {
SDL_memcpy(retval, wstr, len);
}
return retval;
Dec 6, 2017
Dec 6, 2017
100
101
102
void
WASAPI_RemoveDevice(const SDL_bool iscapture, LPCWSTR devid)
May 18, 2017
May 18, 2017
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
DevIdList *i;
DevIdList *next;
DevIdList *prev = NULL;
for (i = deviceid_list; i; i = next) {
next = i->next;
if (WStrEqual(i->str, devid)) {
if (prev) {
prev->next = next;
} else {
deviceid_list = next;
}
SDL_RemoveAudioDevice(iscapture, i->str);
SDL_free(i->str);
SDL_free(i);
}
prev = i;
}
Dec 6, 2017
Dec 6, 2017
123
124
void
WASAPI_AddDevice(const SDL_bool iscapture, const char *devname, LPCWSTR devid)
May 18, 2017
May 18, 2017
126
DevIdList *devidlist;
May 18, 2017
May 18, 2017
128
/* You can have multiple endpoints on a device that are mutually exclusive ("Speakers" vs "Line Out" or whatever).
129
130
131
132
In a perfect world, things that are unplugged won't be in this collection. The only gotcha is probably for
phones and tablets, where you might have an internal speaker and a headphone jack and expect both to be
available and switch automatically. (!!! FIXME...?) */
May 18, 2017
May 18, 2017
133
134
135
136
137
138
/* see if we already have this one. */
for (devidlist = deviceid_list; devidlist; devidlist = devidlist->next) {
if (WStrEqual(devidlist->str, devid)) {
return; /* we already have this. */
}
}
May 18, 2017
May 18, 2017
140
141
142
143
devidlist = (DevIdList *) SDL_malloc(sizeof (*devidlist));
if (!devidlist) {
return; /* oh well. */
}
May 18, 2017
May 18, 2017
145
146
147
148
149
devid = WStrDupe(devid);
if (!devid) {
SDL_free(devidlist);
return; /* oh well. */
}
May 18, 2017
May 18, 2017
151
152
153
devidlist->str = (WCHAR *) devid;
devidlist->next = deviceid_list;
deviceid_list = devidlist;
Dec 6, 2017
Dec 6, 2017
155
SDL_AddAudioDevice(iscapture, devname, (void *) devid);
156
157
158
159
160
}
static void
WASAPI_DetectDevices(void)
{
Dec 6, 2017
Dec 6, 2017
161
WASAPI_EnumerateEndpoints();
Mar 30, 2017
Mar 30, 2017
164
165
166
167
168
169
static int
WASAPI_GetPendingBytes(_THIS)
{
UINT32 frames = 0;
/* it's okay to fail here; we'll deal with failures in the audio thread. */
Jul 28, 2017
Jul 28, 2017
170
/* FIXME: need a lock around checking this->hidden->client */
Dec 6, 2017
Dec 6, 2017
171
172
173
174
if (this->hidden->client != NULL) { /* definitely activated? */
if (FAILED(IAudioClient_GetCurrentPadding(this->hidden->client, &frames))) {
return 0; /* oh well. */
}
Mar 30, 2017
Mar 30, 2017
175
176
177
178
179
180
181
182
183
184
185
186
187
188
}
return ((int) frames) * this->hidden->framesize;
}
static SDL_INLINE SDL_bool
WasapiFailed(_THIS, const HRESULT err)
{
if (err == S_OK) {
return SDL_FALSE;
}
if (err == AUDCLNT_E_DEVICE_INVALIDATED) {
this->hidden->device_lost = SDL_TRUE;
} else if (SDL_AtomicGet(&this->enabled)) {
May 18, 2017
May 18, 2017
189
190
IAudioClient_Stop(this->hidden->client);
SDL_OpenedAudioDeviceDisconnected(this);
Mar 30, 2017
Mar 30, 2017
191
192
193
194
195
196
SDL_assert(!SDL_AtomicGet(&this->enabled));
}
return SDL_TRUE;
}
Dec 6, 2017
Dec 6, 2017
197
198
static int
UpdateAudioStream(_THIS, const SDL_AudioSpec *oldspec)
Mar 29, 2017
Mar 29, 2017
199
200
201
202
203
204
205
206
207
208
209
210
211
{
/* Since WASAPI requires us to handle all audio conversion, and our
device format might have changed, we might have to add/remove/change
the audio stream that the higher level uses to convert data, so
SDL keeps firing the callback as if nothing happened here. */
if ( (this->callbackspec.channels == this->spec.channels) &&
(this->callbackspec.format == this->spec.format) &&
(this->callbackspec.freq == this->spec.freq) &&
(this->callbackspec.samples == this->spec.samples) ) {
/* no need to buffer/convert in an AudioStream! */
SDL_FreeAudioStream(this->stream);
this->stream = NULL;
Dec 6, 2017
Dec 6, 2017
212
213
214
} else if ( (oldspec->channels == this->spec.channels) &&
(oldspec->format == this->spec.format) &&
(oldspec->freq == this->spec.freq) ) {
Mar 29, 2017
Mar 29, 2017
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* The existing audio stream is okay to keep using. */
} else {
/* replace the audiostream for new format */
SDL_FreeAudioStream(this->stream);
if (this->iscapture) {
this->stream = SDL_NewAudioStream(this->spec.format,
this->spec.channels, this->spec.freq,
this->callbackspec.format,
this->callbackspec.channels,
this->callbackspec.freq);
} else {
this->stream = SDL_NewAudioStream(this->callbackspec.format,
this->callbackspec.channels,
this->callbackspec.freq, this->spec.format,
this->spec.channels, this->spec.freq);
}
if (!this->stream) {
Dec 6, 2017
Dec 6, 2017
233
return -1;
Mar 29, 2017
Mar 29, 2017
234
235
236
237
238
239
240
}
}
/* make sure our scratch buffer can cover the new device spec. */
if (this->spec.size > this->work_buffer_len) {
Uint8 *ptr = (Uint8 *) SDL_realloc(this->work_buffer, this->spec.size);
if (ptr == NULL) {
Dec 6, 2017
Dec 6, 2017
241
return SDL_OutOfMemory();
Mar 29, 2017
Mar 29, 2017
242
243
244
245
246
}
this->work_buffer = ptr;
this->work_buffer_len = this->spec.size;
}
Dec 6, 2017
Dec 6, 2017
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
return 0;
}
static void ReleaseWasapiDevice(_THIS);
static SDL_bool
RecoverWasapiDevice(_THIS)
{
ReleaseWasapiDevice(this); /* dump the lost device's handles. */
if (this->hidden->default_device_generation) {
this->hidden->default_device_generation = SDL_AtomicGet(this->iscapture ? &WASAPI_DefaultCaptureGeneration : &WASAPI_DefaultPlaybackGeneration);
}
/* this can fail for lots of reasons, but the most likely is we had a
non-default device that was disconnected, so we can't recover. Default
devices try to reinitialize whatever the new default is, so it's more
likely to carry on here, but this handles a non-default device that
simply had its format changed in the Windows Control Panel. */
if (WASAPI_ActivateDevice(this, SDL_TRUE) == -1) {
SDL_OpenedAudioDeviceDisconnected(this);
return SDL_FALSE;
}
Mar 30, 2017
Mar 30, 2017
272
273
this->hidden->device_lost = SDL_FALSE;
Mar 29, 2017
Mar 29, 2017
274
275
276
277
return SDL_TRUE; /* okay, carry on with new device details! */
}
static SDL_bool
Mar 30, 2017
Mar 30, 2017
278
RecoverWasapiIfLost(_THIS)
Mar 29, 2017
Mar 29, 2017
279
{
Mar 30, 2017
Mar 30, 2017
280
281
const int generation = this->hidden->default_device_generation;
SDL_bool lost = this->hidden->device_lost;
Mar 29, 2017
Mar 29, 2017
282
Mar 30, 2017
Mar 30, 2017
283
284
285
if (!SDL_AtomicGet(&this->enabled)) {
return SDL_FALSE; /* already failed. */
}
Mar 29, 2017
Mar 29, 2017
286
Dec 6, 2017
Dec 6, 2017
287
288
289
290
if (!this->hidden->client) {
return SDL_TRUE; /* still waiting for activation. */
}
Mar 30, 2017
Mar 30, 2017
291
if (!lost && (generation > 0)) { /* is a default device? */
Dec 6, 2017
Dec 6, 2017
292
const int newgen = SDL_AtomicGet(this->iscapture ? &WASAPI_DefaultCaptureGeneration : &WASAPI_DefaultPlaybackGeneration);
Mar 30, 2017
Mar 30, 2017
293
294
295
if (generation != newgen) { /* the desired default device was changed, jump over to it. */
lost = SDL_TRUE;
}
Mar 30, 2017
Mar 30, 2017
298
return lost ? RecoverWasapiDevice(this) : SDL_TRUE;
299
300
301
302
303
304
305
}
static Uint8 *
WASAPI_GetDeviceBuf(_THIS)
{
/* get an endpoint buffer from WASAPI. */
BYTE *buffer = NULL;
Mar 29, 2017
Mar 29, 2017
306
Dec 6, 2017
Dec 6, 2017
307
while (RecoverWasapiIfLost(this) && this->hidden->render) {
Mar 30, 2017
Mar 30, 2017
308
309
310
if (!WasapiFailed(this, IAudioRenderClient_GetBuffer(this->hidden->render, this->spec.samples, &buffer))) {
return (Uint8 *) buffer;
}
311
312
SDL_assert(buffer == NULL);
}
Mar 30, 2017
Mar 30, 2017
313
314
315
316
317
318
319
return (Uint8 *) buffer;
}
static void
WASAPI_PlayDevice(_THIS)
{
Dec 6, 2017
Dec 6, 2017
320
321
322
323
if (this->hidden->render != NULL) { /* definitely activated? */
/* WasapiFailed() will mark the device for reacquisition or removal elsewhere. */
WasapiFailed(this, IAudioRenderClient_ReleaseBuffer(this->hidden->render, this->spec.samples, 0));
}
324
325
326
327
328
}
static void
WASAPI_WaitDevice(_THIS)
{
Dec 13, 2017
Dec 13, 2017
329
330
while (RecoverWasapiIfLost(this) && this->hidden->client && this->hidden->event) {
/*SDL_Log("WAITDEVICE");*/
Dec 31, 2017
Dec 31, 2017
331
if (WaitForSingleObjectEx(this->hidden->event, INFINITE, FALSE) == WAIT_OBJECT_0) {
Dec 13, 2017
Dec 13, 2017
332
333
334
335
336
337
338
const UINT32 maxpadding = this->spec.samples;
UINT32 padding = 0;
if (!WasapiFailed(this, IAudioClient_GetCurrentPadding(this->hidden->client, &padding))) {
/*SDL_Log("WASAPI EVENT! padding=%u maxpadding=%u", (unsigned int)padding, (unsigned int)maxpadding);*/
if (padding <= maxpadding) {
break;
}
Mar 30, 2017
Mar 30, 2017
339
}
Dec 13, 2017
Dec 13, 2017
340
341
342
343
} else {
/*SDL_Log("WASAPI FAILED EVENT!");*/
IAudioClient_Stop(this->hidden->client);
SDL_OpenedAudioDeviceDisconnected(this);
May 18, 2017
May 18, 2017
345
}
346
347
348
349
350
351
352
353
354
355
356
357
358
}
static int
WASAPI_CaptureFromDevice(_THIS, void *buffer, int buflen)
{
SDL_AudioStream *stream = this->hidden->capturestream;
const int avail = SDL_AudioStreamAvailable(stream);
if (avail > 0) {
const int cpy = SDL_min(buflen, avail);
SDL_AudioStreamGet(stream, buffer, cpy);
return cpy;
}
Mar 30, 2017
Mar 30, 2017
359
while (RecoverWasapiIfLost(this)) {
360
361
362
363
364
HRESULT ret;
BYTE *ptr = NULL;
UINT32 frames = 0;
DWORD flags = 0;
Dec 6, 2017
Dec 6, 2017
365
366
367
368
369
370
371
372
/* uhoh, client isn't activated yet, just return silence. */
if (!this->hidden->capture) {
/* Delay so we run at about the speed that audio would be arriving. */
SDL_Delay(((this->spec.samples * 1000) / this->spec.freq));
SDL_memset(buffer, this->spec.silence, buflen);
return buflen;
}
Mar 30, 2017
Mar 30, 2017
373
ret = IAudioCaptureClient_GetBuffer(this->hidden->capture, &ptr, &frames, &flags, NULL, NULL);
May 19, 2017
May 19, 2017
374
375
376
if (ret != AUDCLNT_S_BUFFER_EMPTY) {
WasapiFailed(this, ret); /* mark device lost/failed if necessary. */
}
Mar 29, 2017
Mar 29, 2017
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
if ((ret == AUDCLNT_S_BUFFER_EMPTY) || !frames) {
WASAPI_WaitDevice(this);
} else if (ret == S_OK) {
const int total = ((int) frames) * this->hidden->framesize;
const int cpy = SDL_min(buflen, total);
const int leftover = total - cpy;
const SDL_bool silent = (flags & AUDCLNT_BUFFERFLAGS_SILENT) ? SDL_TRUE : SDL_FALSE;
if (silent) {
SDL_memset(buffer, this->spec.silence, cpy);
} else {
SDL_memcpy(buffer, ptr, cpy);
}
if (leftover > 0) {
ptr += cpy;
if (silent) {
SDL_memset(ptr, this->spec.silence, leftover); /* I guess this is safe? */
}
if (SDL_AudioStreamPut(stream, ptr, leftover) == -1) {
return -1; /* uhoh, out of memory, etc. Kill device. :( */
}
}
Mar 30, 2017
Mar 30, 2017
403
404
405
ret = IAudioCaptureClient_ReleaseBuffer(this->hidden->capture, frames);
WasapiFailed(this, ret); /* mark device lost/failed if necessary. */
406
407
408
409
410
411
412
413
414
415
return cpy;
}
}
return -1; /* unrecoverable error. */
}
static void
WASAPI_FlushCapture(_THIS)
{
Jul 28, 2017
Jul 28, 2017
416
417
418
419
BYTE *ptr = NULL;
UINT32 frames = 0;
DWORD flags = 0;
Dec 6, 2017
Dec 6, 2017
420
421
422
423
if (!this->hidden->capture) {
return; /* not activated yet? */
}
Jul 28, 2017
Jul 28, 2017
424
425
426
427
428
429
430
431
432
/* just read until we stop getting packets, throwing them away. */
while (SDL_TRUE) {
const HRESULT ret = IAudioCaptureClient_GetBuffer(this->hidden->capture, &ptr, &frames, &flags, NULL, NULL);
if (ret == AUDCLNT_S_BUFFER_EMPTY) {
break; /* no more buffered data; we're done. */
} else if (WasapiFailed(this, ret)) {
break; /* failed for some other reason, abort. */
} else if (WasapiFailed(this, IAudioCaptureClient_ReleaseBuffer(this->hidden->capture, frames))) {
break; /* something broke. */
Jul 28, 2017
Jul 28, 2017
435
SDL_AudioStreamClear(this->hidden->capturestream);
436
437
438
}
static void
Mar 29, 2017
Mar 29, 2017
439
ReleaseWasapiDevice(_THIS)
May 18, 2017
May 18, 2017
441
if (this->hidden->client) {
442
IAudioClient_Stop(this->hidden->client);
Dec 13, 2017
Dec 13, 2017
443
444
IAudioClient_SetEventHandle(this->hidden->client, NULL);
IAudioClient_Release(this->hidden->client);
Mar 29, 2017
Mar 29, 2017
445
446
this->hidden->client = NULL;
}
447
448
449
if (this->hidden->render) {
IAudioRenderClient_Release(this->hidden->render);
Mar 29, 2017
Mar 29, 2017
450
this->hidden->render = NULL;
Mar 29, 2017
Mar 29, 2017
453
454
if (this->hidden->capture) {
IAudioCaptureClient_Release(this->hidden->capture);
Jul 27, 2017
Jul 27, 2017
455
this->hidden->capture = NULL;
456
457
458
459
}
if (this->hidden->waveformat) {
CoTaskMemFree(this->hidden->waveformat);
Mar 29, 2017
Mar 29, 2017
460
this->hidden->waveformat = NULL;
461
462
463
464
}
if (this->hidden->capturestream) {
SDL_FreeAudioStream(this->hidden->capturestream);
Mar 29, 2017
Mar 29, 2017
465
this->hidden->capturestream = NULL;
Dec 6, 2017
Dec 6, 2017
467
468
469
470
471
if (this->hidden->activation_handler) {
WASAPI_PlatformDeleteActivationHandler(this->hidden->activation_handler);
this->hidden->activation_handler = NULL;
}
Dec 13, 2017
Dec 13, 2017
472
473
474
475
476
if (this->hidden->event) {
CloseHandle(this->hidden->event);
this->hidden->event = NULL;
}
Mar 29, 2017
Mar 29, 2017
477
}
Mar 29, 2017
Mar 29, 2017
479
480
481
static void
WASAPI_CloseDevice(_THIS)
{
Dec 6, 2017
Dec 6, 2017
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
WASAPI_UnrefDevice(this);
}
void
WASAPI_RefDevice(_THIS)
{
SDL_AtomicIncRef(&this->hidden->refcount);
}
void
WASAPI_UnrefDevice(_THIS)
{
if (!SDL_AtomicDecRef(&this->hidden->refcount)) {
return;
}
/* actual closing happens here. */
Mar 29, 2017
Mar 29, 2017
500
/* don't touch this->hidden->task in here; it has to be reverted from
Dec 6, 2017
Dec 6, 2017
501
502
our callback thread. We do that in WASAPI_ThreadDeinit().
(likewise for this->hidden->coinitialized). */
Mar 29, 2017
Mar 29, 2017
503
ReleaseWasapiDevice(this);
Dec 6, 2017
Dec 6, 2017
504
SDL_free(this->hidden->devid);
505
506
507
SDL_free(this->hidden);
}
Dec 6, 2017
Dec 6, 2017
508
509
510
/* This is called once a device is activated, possibly asynchronously. */
int
WASAPI_PrepDevice(_THIS, const SDL_bool updatestream)
511
512
513
514
515
516
517
518
519
520
521
522
{
/* !!! FIXME: we could request an exclusive mode stream, which is lower latency;
!!! it will write into the kernel's audio buffer directly instead of
!!! shared memory that a user-mode mixer then writes to the kernel with
!!! everything else. Doing this means any other sound using this device will
!!! stop playing, including the user's MP3 player and system notification
!!! sounds. You'd probably need to release the device when the app isn't in
!!! the foreground, to be a good citizen of the system. It's doable, but it's
!!! more work and causes some annoyances, and I don't know what the latency
!!! wins actually look like. Maybe add a hint to force exclusive mode at
!!! some point. To be sure, defaulting to shared mode is the right thing to
!!! do in any case. */
Dec 6, 2017
Dec 6, 2017
523
const SDL_AudioSpec oldspec = this->spec;
524
525
526
const AUDCLNT_SHAREMODE sharemode = AUDCLNT_SHAREMODE_SHARED;
UINT32 bufsize = 0; /* this is in sample frames, not samples, not bytes. */
REFERENCE_TIME duration = 0;
Dec 6, 2017
Dec 6, 2017
527
IAudioClient *client = this->hidden->client;
528
529
530
531
532
533
534
IAudioRenderClient *render = NULL;
IAudioCaptureClient *capture = NULL;
WAVEFORMATEX *waveformat = NULL;
SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
SDL_AudioFormat wasapi_format = 0;
SDL_bool valid_format = SDL_FALSE;
HRESULT ret = S_OK;
Feb 22, 2018
Feb 22, 2018
535
DWORD streamflags = 0;
536
537
538
SDL_assert(client != NULL);
Dec 31, 2017
Dec 31, 2017
539
540
541
#ifdef __WINRT__ /* CreateEventEx() arrived in Vista, so we need an #ifdef for XP. */
this->hidden->event = CreateEventEx(NULL, NULL, 0, EVENT_ALL_ACCESS);
#else
Dec 13, 2017
Dec 13, 2017
542
this->hidden->event = CreateEventW(NULL, 0, 0, NULL);
Dec 31, 2017
Dec 31, 2017
543
544
#endif
Dec 13, 2017
Dec 13, 2017
545
546
547
548
if (this->hidden->event == NULL) {
return WIN_SetError("WASAPI can't create an event handle");
}
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
ret = IAudioClient_GetMixFormat(client, &waveformat);
if (FAILED(ret)) {
return WIN_SetErrorFromHRESULT("WASAPI can't determine mix format", ret);
}
SDL_assert(waveformat != NULL);
this->hidden->waveformat = waveformat;
this->spec.channels = (Uint8) waveformat->nChannels;
/* Make sure we have a valid format that we can convert to whatever WASAPI wants. */
if ((waveformat->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) && (waveformat->wBitsPerSample == 32)) {
wasapi_format = AUDIO_F32SYS;
} else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 16)) {
wasapi_format = AUDIO_S16SYS;
} else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 32)) {
wasapi_format = AUDIO_S32SYS;
} else if (waveformat->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
const WAVEFORMATEXTENSIBLE *ext = (const WAVEFORMATEXTENSIBLE *) waveformat;
if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, sizeof (GUID)) == 0) && (waveformat->wBitsPerSample == 32)) {
wasapi_format = AUDIO_F32SYS;
} else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof (GUID)) == 0) && (waveformat->wBitsPerSample == 16)) {
wasapi_format = AUDIO_S16SYS;
} else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof (GUID)) == 0) && (waveformat->wBitsPerSample == 32)) {
wasapi_format = AUDIO_S32SYS;
}
}
while ((!valid_format) && (test_format)) {
if (test_format == wasapi_format) {
this->spec.format = test_format;
valid_format = SDL_TRUE;
break;
}
test_format = SDL_NextAudioFormat();
}
if (!valid_format) {
return SDL_SetError("WASAPI: Unsupported audio format");
}
ret = IAudioClient_GetDevicePeriod(client, NULL, &duration);
if (FAILED(ret)) {
return WIN_SetErrorFromHRESULT("WASAPI can't determine minimum device period", ret);
}
Feb 22, 2018
Feb 22, 2018
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/* favor WASAPI's resampler over our own, in Win7+. */
if (this->spec.freq != waveformat->nSamplesPerSec) {
/* RATEADJUST only works with output devices in share mode, and is available in Win7 and later.*/
if (WIN_IsWindows7OrGreater() && !this->iscapture && (sharemode == AUDCLNT_SHAREMODE_SHARED)) {
streamflags |= AUDCLNT_STREAMFLAGS_RATEADJUST;
waveformat->nSamplesPerSec = this->spec.freq;
waveformat->nAvgBytesPerSec = waveformat->nSamplesPerSec * waveformat->nChannels * (waveformat->wBitsPerSample / 8);
}
else {
this->spec.freq = waveformat->nSamplesPerSec; /* force sampling rate so our resampler kicks in. */
}
}
streamflags |= AUDCLNT_STREAMFLAGS_EVENTCALLBACK;
ret = IAudioClient_Initialize(client, sharemode, streamflags, duration, sharemode == AUDCLNT_SHAREMODE_SHARED ? 0 : duration, waveformat, NULL);
610
611
612
613
if (FAILED(ret)) {
return WIN_SetErrorFromHRESULT("WASAPI can't initialize audio client", ret);
}
Dec 13, 2017
Dec 13, 2017
614
615
616
617
618
ret = IAudioClient_SetEventHandle(client, this->hidden->event);
if (FAILED(ret)) {
return WIN_SetErrorFromHRESULT("WASAPI can't set event handle", ret);
}
May 18, 2017
May 18, 2017
619
ret = IAudioClient_GetBufferSize(client, &bufsize);
620
621
622
623
624
if (FAILED(ret)) {
return WIN_SetErrorFromHRESULT("WASAPI can't determine buffer size", ret);
}
this->spec.samples = (Uint16) bufsize;
Dec 6, 2017
Dec 6, 2017
625
if (!this->iscapture) {
May 18, 2017
May 18, 2017
626
627
this->spec.samples /= 2; /* fill half of the DMA buffer on each run. */
}
628
629
630
631
632
633
/* Update the fragment size as size in bytes */
SDL_CalculateAudioSpec(&this->spec);
this->hidden->framesize = (SDL_AUDIO_BITSIZE(this->spec.format) / 8) * this->spec.channels;
Dec 6, 2017
Dec 6, 2017
634
if (this->iscapture) {
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
this->hidden->capturestream = SDL_NewAudioStream(this->spec.format, this->spec.channels, this->spec.freq, this->spec.format, this->spec.channels, this->spec.freq);
if (!this->hidden->capturestream) {
return -1; /* already set SDL_Error */
}
ret = IAudioClient_GetService(client, &SDL_IID_IAudioCaptureClient, (void**) &capture);
if (FAILED(ret)) {
return WIN_SetErrorFromHRESULT("WASAPI can't get capture client service", ret);
}
SDL_assert(capture != NULL);
this->hidden->capture = capture;
ret = IAudioClient_Start(client);
if (FAILED(ret)) {
return WIN_SetErrorFromHRESULT("WASAPI can't start capture", ret);
}
WASAPI_FlushCapture(this); /* MSDN says you should flush capture endpoint right after startup. */
} else {
ret = IAudioClient_GetService(client, &SDL_IID_IAudioRenderClient, (void**) &render);
if (FAILED(ret)) {
return WIN_SetErrorFromHRESULT("WASAPI can't get render client service", ret);
}
SDL_assert(render != NULL);
this->hidden->render = render;
ret = IAudioClient_Start(client);
if (FAILED(ret)) {
return WIN_SetErrorFromHRESULT("WASAPI can't start playback", ret);
}
}
Dec 6, 2017
Dec 6, 2017
667
668
669
670
671
672
if (updatestream) {
if (UpdateAudioStream(this, &oldspec) == -1) {
return -1;
}
}
673
674
675
return 0; /* good to go. */
}
Dec 6, 2017
Dec 6, 2017
676
Mar 29, 2017
Mar 29, 2017
677
678
679
static int
WASAPI_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
{
Dec 6, 2017
Dec 6, 2017
680
LPCWSTR devid = (LPCWSTR) handle;
Mar 29, 2017
Mar 29, 2017
681
682
683
684
685
686
687
688
689
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
SDL_malloc((sizeof *this->hidden));
if (this->hidden == NULL) {
return SDL_OutOfMemory();
}
SDL_zerop(this->hidden);
Dec 6, 2017
Dec 6, 2017
690
691
692
693
WASAPI_RefDevice(this); /* so CloseDevice() will unref to zero. */
if (!devid) { /* is default device? */
this->hidden->default_device_generation = SDL_AtomicGet(iscapture ? &WASAPI_DefaultCaptureGeneration : &WASAPI_DefaultPlaybackGeneration);
May 18, 2017
May 18, 2017
694
} else {
Dec 6, 2017
Dec 6, 2017
695
696
697
698
this->hidden->devid = WStrDupe(devid);
if (!this->hidden->devid) {
return SDL_OutOfMemory();
}
Mar 29, 2017
Mar 29, 2017
699
700
}
Dec 6, 2017
Dec 6, 2017
701
702
if (WASAPI_ActivateDevice(this, SDL_FALSE) == -1) {
return -1; /* already set error. */
Mar 29, 2017
Mar 29, 2017
703
704
}
Dec 6, 2017
Dec 6, 2017
705
706
707
708
709
710
711
712
713
/* Ready, but waiting for async device activation.
Until activation is successful, we will report silence from capture
devices and ignore data on playback devices.
Also, since we don't know the _actual_ device format until after
activation, we let the app have whatever it asks for. We set up
an SDL_AudioStream to convert, if necessary, once the activation
completes. */
return 0;
Mar 29, 2017
Mar 29, 2017
714
715
}
716
717
718
static void
WASAPI_ThreadInit(_THIS)
{
Dec 6, 2017
Dec 6, 2017
719
WASAPI_PlatformThreadInit(this);
720
721
722
723
724
}
static void
WASAPI_ThreadDeinit(_THIS)
{
Dec 6, 2017
Dec 6, 2017
725
WASAPI_PlatformThreadDeinit(this);
Sep 25, 2018
Sep 25, 2018
728
729
730
731
732
733
void
WASAPI_BeginLoopIteration(_THIS)
{
/* no-op. */
}
734
735
736
static void
WASAPI_Deinitialize(void)
{
May 18, 2017
May 18, 2017
737
738
DevIdList *devidlist;
DevIdList *next;
Dec 6, 2017
Dec 6, 2017
740
WASAPI_PlatformDeinit();
May 18, 2017
May 18, 2017
742
743
744
745
746
747
for (devidlist = deviceid_list; devidlist; devidlist = next) {
next = devidlist->next;
SDL_free(devidlist->str);
SDL_free(devidlist);
}
deviceid_list = NULL;
748
749
750
751
752
}
static int
WASAPI_Init(SDL_AudioDriverImpl * impl)
{
Dec 6, 2017
Dec 6, 2017
753
754
SDL_AtomicSet(&WASAPI_DefaultPlaybackGeneration, 1);
SDL_AtomicSet(&WASAPI_DefaultCaptureGeneration, 1);
Mar 30, 2017
Mar 30, 2017
755
Dec 6, 2017
Dec 6, 2017
756
if (WASAPI_PlatformInit() == -1) {
757
758
759
760
761
762
763
return 0;
}
/* Set the function pointers */
impl->DetectDevices = WASAPI_DetectDevices;
impl->ThreadInit = WASAPI_ThreadInit;
impl->ThreadDeinit = WASAPI_ThreadDeinit;
Dec 6, 2017
Dec 6, 2017
764
impl->BeginLoopIteration = WASAPI_BeginLoopIteration;
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
impl->OpenDevice = WASAPI_OpenDevice;
impl->PlayDevice = WASAPI_PlayDevice;
impl->WaitDevice = WASAPI_WaitDevice;
impl->GetPendingBytes = WASAPI_GetPendingBytes;
impl->GetDeviceBuf = WASAPI_GetDeviceBuf;
impl->CaptureFromDevice = WASAPI_CaptureFromDevice;
impl->FlushCapture = WASAPI_FlushCapture;
impl->CloseDevice = WASAPI_CloseDevice;
impl->Deinitialize = WASAPI_Deinitialize;
impl->HasCaptureSupport = 1;
return 1; /* this audio target is available. */
}
AudioBootStrap WASAPI_bootstrap = {
"wasapi", "WASAPI", WASAPI_Init, 0
};
#endif /* SDL_AUDIO_DRIVER_WASAPI */
/* vi: set ts=4 sw=4 expandtab: */