/
AESinkWASAPI.cpp
1344 lines (1149 loc) · 52.4 KB
/
AESinkWASAPI.cpp
1
2
3
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2010-2013 Team XBMC
* http://xbmc.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, 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 XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "AESinkWASAPI.h"
#include <Audioclient.h>
#include <avrt.h>
#include <initguid.h>
#include <Mmreg.h>
#include <stdint.h>
#include "../Utils/AEUtil.h"
#include "settings/AdvancedSettings.h"
#include "utils/StdString.h"
#include "utils/log.h"
#include "threads/SingleLock.h"
#include "utils/CharsetConverter.h"
#include "../Utils/AEDeviceInfo.h"
#include <Mmreg.h>
#include <mmdeviceapi.h>
#include "utils/StringUtils.h"
#pragma comment(lib, "Avrt.lib")
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
const IID IID_IAudioClient = __uuidof(IAudioClient);
const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
static const unsigned int WASAPISampleRateCount = 10;
static const unsigned int WASAPISampleRates[] = {384000, 192000, 176400, 96000, 88200, 48000, 44100, 32000, 22050, 11025};
#define WASAPI_SPEAKER_COUNT 21
static const unsigned int WASAPIChannelOrder[] = {AE_CH_RAW,
SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT, SPEAKER_FRONT_CENTER,
SPEAKER_LOW_FREQUENCY, SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
SPEAKER_FRONT_LEFT_OF_CENTER, SPEAKER_FRONT_RIGHT_OF_CENTER,
SPEAKER_BACK_CENTER, SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
SPEAKER_TOP_FRONT_LEFT, SPEAKER_TOP_FRONT_RIGHT, SPEAKER_TOP_FRONT_CENTER,
SPEAKER_TOP_CENTER, SPEAKER_TOP_BACK_LEFT, SPEAKER_TOP_BACK_RIGHT,
SPEAKER_TOP_BACK_CENTER, SPEAKER_RESERVED, SPEAKER_RESERVED};
static const enum AEChannel AEChannelNames[] = {AE_CH_RAW,
AE_CH_FL, AE_CH_FR, AE_CH_FC,
AE_CH_LFE, AE_CH_BL, AE_CH_BR,
AE_CH_FLOC, AE_CH_FROC,
AE_CH_BC, AE_CH_SL, AE_CH_SR,
AE_CH_TFL, AE_CH_TFR, AE_CH_TFC ,
AE_CH_TC , AE_CH_TBL, AE_CH_TBR,
AE_CH_TBC, AE_CH_BLOC, AE_CH_BROC};
static const enum AEChannel layoutsList[][16] =
{
/* Most common configurations */
{AE_CH_FC, AE_CH_NULL}, // Mono
{AE_CH_FL, AE_CH_FR, AE_CH_NULL}, // Stereo
{AE_CH_FL, AE_CH_FR, AE_CH_BL, AE_CH_BR, AE_CH_NULL}, // Quad
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_BC, AE_CH_NULL}, // Surround
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_SL, AE_CH_SR, AE_CH_NULL}, // Standard 5.1
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_SL, AE_CH_SR, AE_CH_BL, AE_CH_BR, AE_CH_NULL}, // Standard 7.1
/* Less common configurations */
{AE_CH_FL, AE_CH_FR, AE_CH_LFE, AE_CH_NULL}, // 2.1
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_BL, AE_CH_BR, AE_CH_NULL}, // 5.1 wide (obsolete)
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_BL, AE_CH_BR, AE_CH_FLOC,AE_CH_FROC,AE_CH_NULL}, // 7.1 wide (obsolete)
/* Exotic configurations */
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_NULL}, // 3 front speakers
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_NULL}, // 3 front speakers + LFE
{AE_CH_FL, AE_CH_FR, AE_CH_BL, AE_CH_BR, AE_CH_LFE, AE_CH_NULL}, // Quad + LFE
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_BC, AE_CH_LFE, AE_CH_NULL}, // Surround + LFE
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_SL, AE_CH_SR, AE_CH_NULL}, // Standard 5.1 w/o LFE
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_BL, AE_CH_BR, AE_CH_NULL}, // 5.1 wide w/o LFE
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_SL, AE_CH_SR, AE_CH_BC, AE_CH_NULL}, // Standard 5.1 w/o LFE + Back Center
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_BL, AE_CH_BC, AE_CH_BR, AE_CH_NULL}, // 5.1 wide w/o LFE + Back Center
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_BL, AE_CH_BR, AE_CH_TC, AE_CH_NULL}, // DVD speakers
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_BL, AE_CH_BR, AE_CH_BC, AE_CH_LFE, AE_CH_NULL}, // 5.1 wide + Back Center
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_SL, AE_CH_SR, AE_CH_BL, AE_CH_BR, AE_CH_NULL}, // Standard 7.1 w/o LFE
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_BL, AE_CH_BR, AE_CH_FLOC,AE_CH_FROC,AE_CH_NULL}, // 7.1 wide w/o LFE
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_SL, AE_CH_SR, AE_CH_BL, AE_CH_BC, AE_CH_BR, AE_CH_NULL}, // Standard 7.1 + Back Center
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_SL, AE_CH_SR, AE_CH_BL, AE_CH_BR, AE_CH_FLOC,AE_CH_FROC,AE_CH_NULL}, // Standard 7.1 + front wide
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_SL, AE_CH_SR, AE_CH_BL, AE_CH_BR, AE_CH_TFL, AE_CH_TFR, AE_CH_NULL}, // Standard 7.1 + 2 front top
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_SL, AE_CH_SR, AE_CH_BL, AE_CH_BR, AE_CH_TFL, AE_CH_TFR, AE_CH_TFC, AE_CH_NULL}, // Standard 7.1 + 3 front top
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_SL, AE_CH_SR, AE_CH_BL, AE_CH_BR, AE_CH_TFL, AE_CH_TFR, AE_CH_TBL, AE_CH_TBR, AE_CH_NULL}, // Standard 7.1 + 2 front top + 2 back top
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_SL, AE_CH_SR, AE_CH_BL, AE_CH_BR, AE_CH_TFL, AE_CH_TFR, AE_CH_TFC, AE_CH_TBL, AE_CH_TBR, AE_CH_NULL}, // Standard 7.1 + 3 front top + 2 back top
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_SL, AE_CH_SR, AE_CH_BL, AE_CH_BR, AE_CH_TFL, AE_CH_TFR, AE_CH_TFC, AE_CH_TBL, AE_CH_TBR, AE_CH_TBC, AE_CH_NULL}, // Standard 7.1 + 3 front top + 3 back top
{AE_CH_FL, AE_CH_FR, AE_CH_FC, AE_CH_LFE, AE_CH_SL, AE_CH_SR, AE_CH_BL, AE_CH_BR, AE_CH_TFL, AE_CH_TFR, AE_CH_TFC, AE_CH_TBL, AE_CH_TBR, AE_CH_TBC, AE_CH_TC, AE_CH_NULL} // Standard 7.1 + 3 front top + 3 back top + Top Center
};
struct sampleFormat
{
GUID subFormat;
unsigned int bitsPerSample;
unsigned int validBitsPerSample;
AEDataFormat subFormatType;
};
/* Sample formats go from float -> 32 bit int -> 24 bit int (packed in 32) -> -> 24 bit int -> 16 bit int */
static const sampleFormat testFormats[] = { {KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 32, 32, AE_FMT_FLOAT},
{KSDATAFORMAT_SUBTYPE_PCM, 32, 32, AE_FMT_S32NE},
{KSDATAFORMAT_SUBTYPE_PCM, 32, 24, AE_FMT_S24NE4},
{KSDATAFORMAT_SUBTYPE_PCM, 24, 24, AE_FMT_S24NE3},
{KSDATAFORMAT_SUBTYPE_PCM, 16, 16, AE_FMT_S16NE} };
struct winEndpointsToAEDeviceType
{
std::string winEndpointType;
AEDeviceType aeDeviceType;
};
static const winEndpointsToAEDeviceType winEndpoints[EndpointFormFactor_enum_count] =
{
{"Network Device - ", AE_DEVTYPE_PCM},
{"Speakers - ", AE_DEVTYPE_PCM},
{"LineLevel - ", AE_DEVTYPE_PCM},
{"Headphones - ", AE_DEVTYPE_PCM},
{"Microphone - ", AE_DEVTYPE_PCM},
{"Headset - ", AE_DEVTYPE_PCM},
{"Handset - ", AE_DEVTYPE_PCM},
{"Digital Passthrough - ", AE_DEVTYPE_IEC958},
{"SPDIF - ", AE_DEVTYPE_IEC958},
{"HDMI - ", AE_DEVTYPE_HDMI},
{"Unknown - ", AE_DEVTYPE_PCM},
};
AEDeviceInfoList DeviceInfoList;
#define EXIT_ON_FAILURE(hr, reason, ...) if(FAILED(hr)) {CLog::Log(LOGERROR, reason " - %s", __VA_ARGS__, WASAPIErrToStr(hr)); goto failed;}
#define ERRTOSTR(err) case err: return #err
DEFINE_PROPERTYKEY(PKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 14);
DWORD ChLayoutToChMask(const enum AEChannel * layout, unsigned int * numberOfChannels = NULL)
{
if (numberOfChannels)
*numberOfChannels = 0;
if (!layout)
return 0;
DWORD mask = 0;
unsigned int i;
for (i = 0; layout[i] != AE_CH_NULL; i++)
mask |= WASAPIChannelOrder[layout[i]];
if (numberOfChannels)
*numberOfChannels = i;
return mask;
}
CStdStringA localWideToUtf(LPCWSTR wstr)
{
if (wstr == NULL)
return "";
int bufSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
CStdStringA strA ("", bufSize);
if ( bufSize == 0 || WideCharToMultiByte(CP_UTF8, 0, wstr, -1, strA.GetBuf(bufSize), bufSize, NULL, NULL) != bufSize )
strA.clear();
strA.RelBuf();
return strA;
}
CAESinkWASAPI::CAESinkWASAPI() :
m_needDataEvent(0),
m_pDevice(NULL),
m_pAudioClient(NULL),
m_pRenderClient(NULL),
m_encodedFormat(AE_FMT_INVALID),
m_encodedChannels(0),
m_encodedSampleRate(0),
sinkReqFormat(AE_FMT_INVALID),
sinkRetFormat(AE_FMT_INVALID),
m_running(false),
m_initialized(false),
m_isSuspended(false),
m_isDirty(false),
m_uiBufferLen(0),
m_avgTimeWaiting(50),
m_sinkLatency(0.0),
m_pBuffer(NULL),
m_bufferPtr(0)
{
m_channelLayout.Reset();
}
CAESinkWASAPI::~CAESinkWASAPI()
{
}
bool CAESinkWASAPI::Initialize(AEAudioFormat &format, std::string &device)
{
if (m_initialized)
return false;
m_device = device;
bool bdefault = false;
/* Save requested format */
/* Clear returned format */
sinkReqFormat = format.m_dataFormat;
sinkRetFormat = AE_FMT_INVALID;
IMMDeviceEnumerator* pEnumerator = NULL;
IMMDeviceCollection* pEnumDevices = NULL;
HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&pEnumerator);
EXIT_ON_FAILURE(hr, __FUNCTION__": Could not allocate WASAPI device enumerator. CoCreateInstance error code: %li", hr)
/* Get our device. First try to find the named device. */
UINT uiCount = 0;
hr = pEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pEnumDevices);
EXIT_ON_FAILURE(hr, __FUNCTION__": Retrieval of audio endpoint enumeration failed.")
hr = pEnumDevices->GetCount(&uiCount);
EXIT_ON_FAILURE(hr, __FUNCTION__": Retrieval of audio endpoint count failed.")
if(StringUtils::EndsWithNoCase(device, std::string("default")))
bdefault = true;
if(!bdefault)
{
for (UINT i = 0; i < uiCount; i++)
{
IPropertyStore *pProperty = NULL;
PROPVARIANT varName;
hr = pEnumDevices->Item(i, &m_pDevice);
EXIT_ON_FAILURE(hr, __FUNCTION__": Retrieval of WASAPI endpoint failed.")
hr = m_pDevice->OpenPropertyStore(STGM_READ, &pProperty);
EXIT_ON_FAILURE(hr, __FUNCTION__": Retrieval of WASAPI endpoint properties failed.")
hr = pProperty->GetValue(PKEY_AudioEndpoint_GUID, &varName);
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Retrieval of WASAPI endpoint GUID failed.");
SAFE_RELEASE(pProperty);
goto failed;
}
std::string strDevName = localWideToUtf(varName.pwszVal);
if (device == strDevName)
i = uiCount;
else
SAFE_RELEASE(m_pDevice);
PropVariantClear(&varName);
SAFE_RELEASE(pProperty);
}
}
SAFE_RELEASE(pEnumDevices);
if (!m_pDevice)
{
if(!bdefault)
CLog::Log(LOGINFO, __FUNCTION__": Could not locate the device named \"%s\" in the list of WASAPI endpoint devices. Trying the default device...", device.c_str());
hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &m_pDevice);
EXIT_ON_FAILURE(hr, __FUNCTION__": Could not retrieve the default WASAPI audio endpoint.")
IPropertyStore *pProperty = NULL;
PROPVARIANT varName;
hr = m_pDevice->OpenPropertyStore(STGM_READ, &pProperty);
EXIT_ON_FAILURE(hr, __FUNCTION__": Retrieval of WASAPI endpoint properties failed.")
hr = pProperty->GetValue(PKEY_AudioEndpoint_GUID, &varName);
device = localWideToUtf(varName.pwszVal);
PropVariantClear(&varName);
SAFE_RELEASE(pProperty);
}
SAFE_RELEASE(pEnumerator);
hr = m_pDevice->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void**)&m_pAudioClient);
EXIT_ON_FAILURE(hr, __FUNCTION__": Activating the WASAPI endpoint device failed.")
if (!InitializeExclusive(format))
{
CLog::Log(LOGINFO, __FUNCTION__": Could not Initialize Exclusive with that format");
goto failed;
}
/* get the buffer size and calculate the frames for AE */
m_pAudioClient->GetBufferSize(&m_uiBufferLen);
format.m_frames = m_uiBufferLen;
format.m_frameSamples = format.m_frames * format.m_channelLayout.Count();
m_format = format;
sinkRetFormat = format.m_dataFormat;
hr = m_pAudioClient->GetService(IID_IAudioRenderClient, (void**)&m_pRenderClient);
EXIT_ON_FAILURE(hr, __FUNCTION__": Could not initialize the WASAPI render client interface.")
m_needDataEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
hr = m_pAudioClient->SetEventHandle(m_needDataEvent);
EXIT_ON_FAILURE(hr, __FUNCTION__": Could not set the WASAPI event handler.");
m_initialized = true;
m_isDirty = false;
// allow feeding less samples than buffer size
// if the device is opened exclusive and event driven, provided samples must match buffersize
// ActiveAE tries to align provided samples with buffer size but cannot guarantee (e.g. transcoding)
// this can be avoided by dropping the event mode which has not much benefit; SoftAE polls anyway
delete [] m_pBuffer;
m_pBuffer = new uint8_t[format.m_frames * format.m_frameSize];
m_bufferPtr = 0;
return true;
failed:
CLog::Log(LOGERROR, __FUNCTION__": WASAPI initialization failed.");
SAFE_RELEASE(pEnumDevices);
SAFE_RELEASE(pEnumerator);
SAFE_RELEASE(m_pRenderClient);
SAFE_RELEASE(m_pAudioClient);
SAFE_RELEASE(m_pDevice);
if(m_needDataEvent)
{
CloseHandle(m_needDataEvent);
m_needDataEvent = 0;
}
return false;
}
void CAESinkWASAPI::Deinitialize()
{
if (!m_initialized && !m_isDirty)
return;
if (m_running)
{
try
{
m_pAudioClient->Stop(); //stop the audio output
m_pAudioClient->Reset(); //flush buffer and reset audio clock stream position
}
catch (...)
{
CLog::Log(LOGDEBUG, __FUNCTION__, "Invalidated AudioClient - Releasing");
}
}
m_running = false;
CloseHandle(m_needDataEvent);
SAFE_RELEASE(m_pRenderClient);
SAFE_RELEASE(m_pAudioClient);
SAFE_RELEASE(m_pDevice);
m_initialized = false;
delete [] m_pBuffer;
m_bufferPtr = 0;
}
bool CAESinkWASAPI::IsCompatible(const AEAudioFormat &format, const std::string &device)
{
if (!m_initialized || m_isDirty)
return false;
u_int notCompatible = 0;
const u_int numTests = 5;
std::string strDiffBecause ("");
static const char* compatibleParams[numTests] = {":Devices",
":Channels",
":Sample Rates",
":Data Formats",
":Passthrough Formats"};
notCompatible = (notCompatible +!((AE_IS_RAW(format.m_dataFormat) == AE_IS_RAW(m_encodedFormat)) ||
(!AE_IS_RAW(format.m_dataFormat) == !AE_IS_RAW(m_encodedFormat)))) << 1;
notCompatible = (notCompatible +!((sinkReqFormat == format.m_dataFormat) &&
(sinkRetFormat == m_format.m_dataFormat))) << 1;
notCompatible = (notCompatible + !(format.m_sampleRate == m_format.m_sampleRate)) << 1;
notCompatible = (notCompatible + !(format.m_channelLayout.Count() == m_format.m_channelLayout.Count())) << 1;
notCompatible = (notCompatible + !(m_device == device));
if (!notCompatible)
{
CLog::Log(LOGDEBUG, __FUNCTION__": Formats compatible - reusing existing sink");
return true;
}
for (int i = 0; i < numTests ; i++)
{
strDiffBecause += (notCompatible & 0x01) ? (std::string) compatibleParams[i] : "";
notCompatible = notCompatible >> 1;
}
CLog::Log(LOGDEBUG, __FUNCTION__": Formats Incompatible due to different %s", strDiffBecause.c_str());
return false;
}
double CAESinkWASAPI::GetDelay()
{
if (!m_initialized)
return 0.0;
double time_played = 0.0;
if (m_running)
{
unsigned int now = XbmcThreads::SystemClockMillis();
time_played = (double)(now-m_lastWriteToBuffer) / 1000;
}
double delay = m_sinkLatency - time_played + (double)m_bufferPtr / (double)m_format.m_sampleRate;
if (delay < 0)
delay = 0.0;
return delay;
}
double CAESinkWASAPI::GetCacheTime()
{
/* This function deviates from the defined usage due to the event-driven */
/* mode of WASAPI utilizing twin buffers which are written to in single */
/* buffer chunks. Therefore the buffers are either 100% full or 50% full */
/* At 50% issues arise with water levels in the stream and player. For */
/* this reason the cache is shown as 100% full at all times, and control */
/* of the buffer filling is assumed in AddPackets() and by the WASAPI */
/* implementation of the WaitforSingleObject event indicating one of the */
/* buffers is ready for filling via AddPackets */
if (!m_initialized)
return 0.0;
return m_sinkLatency;
}
double CAESinkWASAPI::GetCacheTotal()
{
if (!m_initialized)
return 0.0;
return m_sinkLatency;
}
unsigned int CAESinkWASAPI::AddPackets(uint8_t *data, unsigned int frames, bool hasAudio, bool blocking)
{
if (!m_initialized)
return 0;
HRESULT hr;
BYTE *buf;
DWORD flags = 0;
#ifndef _DEBUG
LARGE_INTEGER timerStart;
LARGE_INTEGER timerStop;
LARGE_INTEGER timerFreq;
#endif
unsigned int NumFramesRequested = m_format.m_frames;
unsigned int FramesToCopy = std::min(m_format.m_frames - m_bufferPtr, frames);
if (m_bufferPtr != 0 || frames != m_format.m_frames)
{
memcpy(m_pBuffer+m_bufferPtr*m_format.m_frameSize, data, FramesToCopy*m_format.m_frameSize);
m_bufferPtr += FramesToCopy;
if (m_bufferPtr != m_format.m_frames)
return frames;
}
if (!m_running) //first time called, pre-fill buffer then start audio client
{
hr = m_pAudioClient->Reset();
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__ " AudioClient reset failed due to %s", WASAPIErrToStr(hr));
return 0;
}
hr = m_pRenderClient->GetBuffer(NumFramesRequested, &buf);
if (FAILED(hr))
{
#ifdef _DEBUG
CLog::Log(LOGERROR, __FUNCTION__": GetBuffer failed due to %s", WASAPIErrToStr(hr));
#endif
m_isDirty = true; //flag new device or re-init needed
return INT_MAX;
}
/* Inject one buffer of silence if sink has just opened */
/* to avoid losing start of stream or GUI sound */
if (g_advancedSettings.m_streamSilence)
memcpy(buf, data, NumFramesRequested * m_format.m_frameSize); //fill buffer with audio
else
memset(buf, 0, NumFramesRequested * m_format.m_frameSize); //fill buffer with silence
hr = m_pRenderClient->ReleaseBuffer(NumFramesRequested, flags); //pass back to audio driver
if (FAILED(hr))
{
#ifdef _DEBUG
CLog::Log(LOGDEBUG, __FUNCTION__": ReleaseBuffer failed due to %s.", WASAPIErrToStr(hr));
#endif
m_isDirty = true; //flag new device or re-init needed
return INT_MAX;
}
hr = m_pAudioClient->Start(); //start the audio driver running
if (FAILED(hr))
CLog::Log(LOGERROR, __FUNCTION__": AudioClient Start Failed");
m_running = true; //signal that we're processing frames
return g_advancedSettings.m_streamSilence ? NumFramesRequested : 0U;
}
#ifndef _DEBUG
/* Get clock time for latency checks */
QueryPerformanceFrequency(&timerFreq);
QueryPerformanceCounter(&timerStart);
#endif
/* Wait for Audio Driver to tell us it's got a buffer available */
DWORD eventAudioCallback;
if(!blocking)
eventAudioCallback = WaitForSingleObject(m_needDataEvent, 0);
else
eventAudioCallback = WaitForSingleObject(m_needDataEvent, 1100);
if (!blocking)
{
if(eventAudioCallback != WAIT_OBJECT_0)
return 0;
}
else
{
if(eventAudioCallback != WAIT_OBJECT_0 || !&buf)
{
/* Event handle timed out - flag sink as dirty for re-initializing */
CLog::Log(LOGERROR, __FUNCTION__": Endpoint Buffer timed out");
if (g_advancedSettings.m_streamSilence)
{
m_isDirty = true; //flag new device or re-init needed
Deinitialize();
m_running = false;
return INT_MAX;
}
}
}
if (!m_running)
return 0;
#ifndef _DEBUG
QueryPerformanceCounter(&timerStop);
LONGLONG timerDiff = timerStop.QuadPart - timerStart.QuadPart;
double timerElapsed = (double) timerDiff * 1000.0 / (double) timerFreq.QuadPart;
m_avgTimeWaiting += (timerElapsed - m_avgTimeWaiting) * 0.5;
if (m_avgTimeWaiting < 3.0)
{
CLog::Log(LOGDEBUG, __FUNCTION__": Possible AQ Loss: Avg. Time Waiting for Audio Driver callback : %dmsec", (int)m_avgTimeWaiting);
}
#endif
hr = m_pRenderClient->GetBuffer(NumFramesRequested, &buf);
if (FAILED(hr))
{
#ifdef _DEBUG
CLog::Log(LOGERROR, __FUNCTION__": GetBuffer failed due to %s", WASAPIErrToStr(hr));
#endif
return INT_MAX;
}
memcpy(buf, m_bufferPtr == 0 ? data : m_pBuffer, NumFramesRequested * m_format.m_frameSize); //fill buffer
m_bufferPtr = 0;
hr = m_pRenderClient->ReleaseBuffer(NumFramesRequested, flags); //pass back to audio driver
if (FAILED(hr))
{
#ifdef _DEBUG
CLog::Log(LOGDEBUG, __FUNCTION__": ReleaseBuffer failed due to %s.", WASAPIErrToStr(hr));
#endif
return INT_MAX;
}
m_lastWriteToBuffer = XbmcThreads::SystemClockMillis();
if (FramesToCopy != frames)
{
m_bufferPtr = frames-FramesToCopy;
memcpy(m_pBuffer, data+FramesToCopy*m_format.m_frameSize, m_bufferPtr*m_format.m_frameSize);
}
return frames;
}
bool CAESinkWASAPI::SoftSuspend()
{
/* Sink has been asked to suspend output - we release audio */
/* device as we are in exclusive mode and thus allow external */
/* audio sources to play. This requires us to reinitialize */
/* on resume. */
Deinitialize();
return true;
}
bool CAESinkWASAPI::SoftResume()
{
/* Sink asked to resume output. To release audio device in */
/* exclusive mode we release the device context and therefore */
/* must reinitialize. Return false to force re-init by engine */
return false;
}
void CAESinkWASAPI::EnumerateDevicesEx(AEDeviceInfoList &deviceInfoList, bool force)
{
IMMDeviceEnumerator* pEnumerator = NULL;
IMMDeviceCollection* pEnumDevices = NULL;
IMMDevice* pDefaultDevice = NULL;
CAEDeviceInfo deviceInfo;
CAEChannelInfo deviceChannels;
LPWSTR pwszID = NULL;
std::wstring wstrDDID;
WAVEFORMATEXTENSIBLE wfxex = {0};
HRESULT hr;
hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&pEnumerator);
EXIT_ON_FAILURE(hr, __FUNCTION__": Could not allocate WASAPI device enumerator. CoCreateInstance error code: %li", hr)
UINT uiCount = 0;
// get the default audio endpoint
if(pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDefaultDevice) == S_OK)
{
if(pDefaultDevice->GetId(&pwszID) == S_OK)
{
wstrDDID = pwszID;
CoTaskMemFree(pwszID);
}
SAFE_RELEASE(pDefaultDevice);
}
// enumerate over all audio endpoints
hr = pEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pEnumDevices);
EXIT_ON_FAILURE(hr, __FUNCTION__": Retrieval of audio endpoint enumeration failed.")
hr = pEnumDevices->GetCount(&uiCount);
EXIT_ON_FAILURE(hr, __FUNCTION__": Retrieval of audio endpoint count failed.")
for (UINT i = 0; i < uiCount; i++)
{
IMMDevice *pDevice = NULL;
IPropertyStore *pProperty = NULL;
PROPVARIANT varName;
PropVariantInit(&varName);
deviceInfo.m_channels.Reset();
deviceInfo.m_dataFormats.clear();
deviceInfo.m_sampleRates.clear();
hr = pEnumDevices->Item(i, &pDevice);
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Retrieval of WASAPI endpoint failed.");
goto failed;
}
hr = pDevice->OpenPropertyStore(STGM_READ, &pProperty);
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Retrieval of WASAPI endpoint properties failed.");
SAFE_RELEASE(pDevice);
goto failed;
}
hr = pProperty->GetValue(PKEY_Device_FriendlyName, &varName);
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Retrieval of WASAPI endpoint device name failed.");
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pProperty);
goto failed;
}
std::string strFriendlyName = localWideToUtf(varName.pwszVal);
PropVariantClear(&varName);
hr = pProperty->GetValue(PKEY_AudioEndpoint_GUID, &varName);
if(FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Retrieval of WASAPI endpoint GUID failed.");
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pProperty);
goto failed;
}
std::string strDevName = localWideToUtf(varName.pwszVal);
PropVariantClear(&varName);
hr = pProperty->GetValue(PKEY_AudioEndpoint_FormFactor, &varName);
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Retrieval of WASAPI endpoint form factor failed.");
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pProperty);
goto failed;
}
std::string strWinDevType = winEndpoints[(EndpointFormFactor)varName.uiVal].winEndpointType;
AEDeviceType aeDeviceType = winEndpoints[(EndpointFormFactor)varName.uiVal].aeDeviceType;
PropVariantClear(&varName);
hr = pProperty->GetValue(PKEY_AudioEndpoint_PhysicalSpeakers, &varName);
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Retrieval of WASAPI endpoint speaker layout failed.");
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pProperty);
goto failed;
}
unsigned int uiChannelMask = std::max(varName.uintVal, (unsigned int) KSAUDIO_SPEAKER_STEREO);
deviceChannels.Reset();
for (unsigned int c = 0; c < WASAPI_SPEAKER_COUNT; c++)
{
if (uiChannelMask & WASAPIChannelOrder[c])
deviceChannels += AEChannelNames[c];
}
PropVariantClear(&varName);
IAudioClient *pClient;
hr = pDevice->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void**)&pClient);
if (SUCCEEDED(hr))
{
/* Test format DTS-HD */
wfxex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX);
wfxex.Format.nSamplesPerSec = 192000;
wfxex.dwChannelMask = KSAUDIO_SPEAKER_7POINT1_SURROUND;
wfxex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DTS_HD;
wfxex.Format.wBitsPerSample = 16;
wfxex.Samples.wValidBitsPerSample = 16;
wfxex.Format.nChannels = 8;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
deviceInfo.m_dataFormats.push_back(AEDataFormat(AE_FMT_DTSHD));
/* Test format Dolby TrueHD */
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_MLP;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
deviceInfo.m_dataFormats.push_back(AEDataFormat(AE_FMT_TRUEHD));
/* Test format Dolby EAC3 */
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_DIGITAL_PLUS;
wfxex.Format.nChannels = 2;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
deviceInfo.m_dataFormats.push_back(AEDataFormat(AE_FMT_EAC3));
/* Test format DTS */
wfxex.Format.nSamplesPerSec = 48000;
wfxex.dwChannelMask = KSAUDIO_SPEAKER_5POINT1;
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DTS;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
deviceInfo.m_dataFormats.push_back(AEDataFormat(AE_FMT_DTS));
/* Test format Dolby AC3 */
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_DIGITAL;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
deviceInfo.m_dataFormats.push_back(AEDataFormat(AE_FMT_AC3));
/* Test format AAC */
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_AAC;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
deviceInfo.m_dataFormats.push_back(AEDataFormat(AE_FMT_AAC));
/* Test format for PCM format iteration */
wfxex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX);
wfxex.dwChannelMask = KSAUDIO_SPEAKER_STEREO;
wfxex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
for (int p = AE_FMT_FLOAT; p > AE_FMT_INVALID; p--)
{
if (p < AE_FMT_FLOAT)
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
wfxex.Format.wBitsPerSample = CAEUtil::DataFormatToBits((AEDataFormat) p);
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
if (p <= AE_FMT_S24NE4 && p >= AE_FMT_S24BE4)
{
wfxex.Samples.wValidBitsPerSample = 24;
}
else
{
wfxex.Samples.wValidBitsPerSample = wfxex.Format.wBitsPerSample;
}
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
deviceInfo.m_dataFormats.push_back((AEDataFormat) p);
}
/* Test format for sample rate iteration */
wfxex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX);
wfxex.dwChannelMask = KSAUDIO_SPEAKER_STEREO;
wfxex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
wfxex.Format.wBitsPerSample = 16;
wfxex.Samples.wValidBitsPerSample = 16;
wfxex.Format.nChannels = 2;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
for (int j = 0; j < WASAPISampleRateCount; j++)
{
wfxex.Format.nSamplesPerSec = WASAPISampleRates[j];
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
deviceInfo.m_sampleRates.push_back(WASAPISampleRates[j]);
}
/* Test format for channels iteration */
wfxex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX);
wfxex.dwChannelMask = KSAUDIO_SPEAKER_STEREO;
wfxex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
wfxex.Format.nSamplesPerSec = 48000;
wfxex.Format.wBitsPerSample = 16;
wfxex.Samples.wValidBitsPerSample = 16;
wfxex.Format.nChannels = 2;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
bool hasLpcm = false;
// Try with KSAUDIO_SPEAKER_DIRECTOUT
for (unsigned int k = WASAPI_SPEAKER_COUNT; k > 0; k--)
{
wfxex.dwChannelMask = KSAUDIO_SPEAKER_DIRECTOUT;
wfxex.Format.nChannels = k;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
{
if (k > 3) // Add only multichannel LPCM
{
deviceInfo.m_dataFormats.push_back(AE_FMT_LPCM);
hasLpcm = true;
}
break;
}
}
/* Try with reported channel mask */
for (unsigned int k = WASAPI_SPEAKER_COUNT; k > 0; k--)
{
wfxex.dwChannelMask = uiChannelMask;
wfxex.Format.nChannels = k;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
{
if ( !hasLpcm && k > 3) // Add only multichannel LPCM
{
deviceInfo.m_dataFormats.push_back(AE_FMT_LPCM);
hasLpcm = true;
}
break;
}
}
/* Try with specific speakers configurations */
for (unsigned int i = 0; i < ARRAYSIZE(layoutsList); i++)
{
unsigned int nmbOfCh;
wfxex.dwChannelMask = ChLayoutToChMask(layoutsList[i], &nmbOfCh);
wfxex.Format.nChannels = nmbOfCh;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
{
if ( deviceChannels.Count() < nmbOfCh)
deviceChannels = layoutsList[i];
if ( !hasLpcm && nmbOfCh > 3) // Add only multichannel LPCM
{
deviceInfo.m_dataFormats.push_back(AE_FMT_LPCM);
hasLpcm = true;
}
}
}
pClient->Release();
}
else
{
CLog::Log(LOGDEBUG, __FUNCTION__": Failed to activate device for passthrough capability testing.");
}
deviceInfo.m_deviceName = strDevName;
deviceInfo.m_displayName = strWinDevType.append(strFriendlyName);
deviceInfo.m_displayNameExtra = std::string("WASAPI: ").append(strFriendlyName);
deviceInfo.m_deviceType = aeDeviceType;
deviceInfo.m_channels = deviceChannels;
/* Store the device info */
deviceInfoList.push_back(deviceInfo);
if(pDevice->GetId(&pwszID) == S_OK)
{
if(wstrDDID.compare(pwszID) == 0)
{
deviceInfo.m_deviceName = std::string("default");
deviceInfo.m_displayName = std::string("default");
deviceInfo.m_displayNameExtra = std::string("");
deviceInfoList.push_back(deviceInfo);
}
CoTaskMemFree(pwszID);
}
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pProperty);
}
return;
failed:
if (FAILED(hr))
CLog::Log(LOGERROR, __FUNCTION__": Failed to enumerate WASAPI endpoint devices (%s).", WASAPIErrToStr(hr));
SAFE_RELEASE(pEnumDevices);
SAFE_RELEASE(pEnumerator);
}
//Private utility functions////////////////////////////////////////////////////
void CAESinkWASAPI::BuildWaveFormatExtensible(AEAudioFormat &format, WAVEFORMATEXTENSIBLE &wfxex)
{
wfxex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wfxex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX);
if (!AE_IS_RAW(format.m_dataFormat)) // PCM data
{
wfxex.dwChannelMask = SpeakerMaskFromAEChannels(format.m_channelLayout);
wfxex.Format.nChannels = (WORD)format.m_channelLayout.Count();
wfxex.Format.nSamplesPerSec = format.m_sampleRate;
wfxex.Format.wBitsPerSample = CAEUtil::DataFormatToBits((AEDataFormat) format.m_dataFormat);
wfxex.SubFormat = format.m_dataFormat <= AE_FMT_FLOAT ? KSDATAFORMAT_SUBTYPE_PCM : KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
}
else //Raw bitstream
{
wfxex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
if (format.m_dataFormat == AE_FMT_AC3 || format.m_dataFormat == AE_FMT_DTS)
{
wfxex.dwChannelMask = bool (format.m_channelLayout.Count() == 2) ? KSAUDIO_SPEAKER_STEREO : KSAUDIO_SPEAKER_5POINT1;
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_DIGITAL;
wfxex.Format.wBitsPerSample = 16;
wfxex.Samples.wValidBitsPerSample = 16;
wfxex.Format.nChannels = (WORD)format.m_channelLayout.Count();
wfxex.Format.nSamplesPerSec = format.m_sampleRate;
}
else if (format.m_dataFormat == AE_FMT_EAC3 || format.m_dataFormat == AE_FMT_TRUEHD || format.m_dataFormat == AE_FMT_DTSHD)
{
/* IEC 61937 transmissions over HDMI */
wfxex.Format.nSamplesPerSec = 192000L;
wfxex.Format.wBitsPerSample = 16;
wfxex.Samples.wValidBitsPerSample = 16;
wfxex.dwChannelMask = KSAUDIO_SPEAKER_7POINT1_SURROUND;
switch (format.m_dataFormat)
{
case AE_FMT_EAC3:
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_DIGITAL_PLUS;