-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAudioLoopback.cpp
1050 lines (838 loc) · 29.2 KB
/
AudioLoopback.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
//------------------------------------------------------------------------------
// File: AudioLoopback.cpp
//
//
// Copyright (c) Corey Auger. All rights reserved.
//------------------------------------------------------------------------------
#include <windows.h>
#include <streams.h>
#include <mmsystem.h>
#include <mmdeviceapi.h>
#include <audioclient.h>
#include <avrt.h>
#include <math.h>
#include <mmreg.h>
#include <msacm.h>
#include <iostream>
#include <fstream>
#include <initguid.h>
#if (1100 > _MSC_VER)
#include <olectlid.h>
#else
#include <olectl.h>
#endif
#define _AUDIOSYNTH_IMPLEMENTATION_
#include "DynSrc.h"
#include "IAudioLoopbackFilter.h"
#include "AudioLoopbackFilter.h"
#include <cstdio>
#include <cstdarg>
// Quick and dirty debug..
void Log( const char* frmt, ... ){
#ifdef _DEBUG
char buf[2048];
va_list ptr;
va_start(ptr,frmt);
vsprintf_s(buf,frmt,ptr);
OutputDebugStringA(buf);
OutputDebugStringA("\n");
// static FILE *f = fopen("C:\\tmp\\_aloop.txt","a");
// fprintf(f,"%s",buf);
// fflush(f);
#endif
}
// setup data
const AMOVIESETUP_MEDIATYPE sudOpPinTypes =
{ &MEDIATYPE_Audio // clsMajorType
, &MEDIASUBTYPE_NULL }; // clsMinorType
const AMOVIESETUP_PIN sudOpPin =
{ L"Output" // strName
, FALSE // bRendered
, TRUE // bOutput
, FALSE // bZero
, FALSE // bMany
, &CLSID_NULL // clsConnectsToFilter
, L"Input" // strConnectsToPin
, 1 // nTypes
, &sudOpPinTypes }; // lpTypes
const AMOVIESETUP_FILTER sudSynth =
{ &CLSID_AudioLoopbackFilter // clsID
, L"Audio Loopback" // strName
, MERIT_UNLIKELY // dwMerit
, 1 // nPins
, &sudOpPin }; // lpPin
// -------------------------------------------------------------------------
// g_Templates
// -------------------------------------------------------------------------
// COM global table of objects in this dll
CFactoryTemplate g_Templates[] = {
{ L"Audio Loopback"
, &CLSID_AudioLoopbackFilter
, CAudioLoopbackFilter::CreateInstance
, NULL
, &sudSynth }
,
//{ L"Audio Loopback Property Page"
//, &CLSID_AudioLoopbackPropertyPage
//, CAudioLoopbackProperties::CreateInstance }
};
int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);
// There are 8 bits in a byte.
const DWORD BITS_PER_BYTE = 8;
// -------------------------------------------------------------------------
// CSynthFilter, the main filter object
// -------------------------------------------------------------------------
//
// CreateInstance
//
// The only allowed way to create Synthesizers
CUnknown * WINAPI CAudioLoopbackFilter::CreateInstance(LPUNKNOWN lpunk, HRESULT *phr)
{
ASSERT(phr);
CUnknown *punk = new CAudioLoopbackFilter(lpunk, phr);
if (punk == NULL) {
if (phr)
*phr = E_OUTOFMEMORY;
}
return punk;
}
CAudioLoopbackFilter::CAudioLoopbackFilter(LPUNKNOWN lpunk, HRESULT *phr)
: CDynamicSource(NAME("Audio Loopback Filter"),lpunk, CLSID_AudioLoopbackFilter, phr)
, CPersistStream(lpunk, phr)
{
m_paStreams = (CDynamicSourceStream **) new CSynthStream*[1];
if (m_paStreams == NULL) {
if (phr)
*phr = E_OUTOFMEMORY;
return;
}
m_paStreams[0] = new CAudioLoopbackPin(phr, this, L"Audio Loopback Pin");
if (m_paStreams[0] == NULL) {
if (phr)
*phr = E_OUTOFMEMORY;
return;
}
}
CAudioLoopbackFilter::~CAudioLoopbackFilter(void)
{
//
// Base class will free our pins
//
}
//
// NonDelegatingQueryInterface
//
// Reveal our property page, persistance, and control interfaces
STDMETHODIMP CAudioLoopbackFilter::NonDelegatingQueryInterface(REFIID riid, void **ppv)
{
if (riid == IID_IAudioLoopbackFilter) {
return GetInterface((IAudioLoopbackFilter *) this, ppv);
}
else if (riid == IID_IPersistStream) {
return GetInterface((IPersistStream *) this, ppv);
}
else if (riid == IID_ISpecifyPropertyPages) {
return GetInterface((ISpecifyPropertyPages *) this, ppv);
}
else {
return CDynamicSource::NonDelegatingQueryInterface(riid, ppv);
}
}
//
// GetPages
//
STDMETHODIMP CAudioLoopbackFilter::GetPages(CAUUID * pPages)
{
CheckPointer(pPages,E_POINTER);
pPages->cElems = 1;
pPages->pElems = (GUID *) CoTaskMemAlloc(sizeof(GUID));
if (pPages->pElems == NULL) {
return E_OUTOFMEMORY;
}
*(pPages->pElems) = CLSID_AudioLoopbackPropertyPage;
return NOERROR;
}
// -------------------------------------------------------------------------
// --- IPersistStream ---
// -------------------------------------------------------------------------
#define WRITEOUT(var) hr = pStream->Write(&var, sizeof(var), NULL); \
if (FAILED(hr)) return hr;
#define READIN(var) hr = pStream->Read(&var, sizeof(var), NULL); \
if (FAILED(hr)) return hr;
STDMETHODIMP CAudioLoopbackFilter::GetClassID(CLSID *pClsid)
{
return CBaseFilter::GetClassID(pClsid);
}
int CAudioLoopbackFilter::SizeMax ()
{
return sizeof (int) * 8;
}
HRESULT CAudioLoopbackFilter::WriteToStream(IStream *pStream)
{
CheckPointer(pStream,E_POINTER);
HRESULT hr;
int i, k;
return hr;
}
HRESULT CAudioLoopbackFilter::ReadFromStream(IStream *pStream)
{
CheckPointer(pStream,E_POINTER);
if (GetSoftwareVersion() != mPS_dwFileVersion)
return E_FAIL;
HRESULT hr;
int i, k;
return hr;
}
DWORD CAudioLoopbackFilter::GetSoftwareVersion(void)
{
return 1;
}
// -------------------------------------------------------------------------
// IAudioLoopbackFilter, the control interface for the synthesizer
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// CAudioLoopbackPin, the output pin
// -------------------------------------------------------------------------
//
// CAudioLoopbackPin::Constructor
//
CAudioLoopbackPin::CAudioLoopbackPin(HRESULT *phr, CAudioLoopbackFilter *pParent, LPCWSTR pName)
: CDynamicSourceStream(NAME("AudioLoopback Pin"),phr, pParent, pName)
, m_hPCMToMSADPCMConversionStream(NULL)
, m_dwTempPCMBufferSize(0)
, m_fFirstSampleDelivered(FALSE)
, m_llSampleMediaTimeStart(0)
, m_dwAdviseToken(NULL)
{
ASSERT(phr);
m_Loopback = new CAudioLoopback(pParent->pStateLock());
pParent->m_Loopback = m_Loopback;
if (m_Loopback == NULL) {
*phr = E_OUTOFMEMORY;
return;
}
m_pParent = pParent;
}
//
// CAudioLoopbackPin::Destructor
CAudioLoopbackPin::~CAudioLoopbackPin(void)
{
delete m_Loopback;
}
//
// FillBuffer
//
// Stuffs the buffer with data
HRESULT CAudioLoopbackPin::FillBuffer(IMediaSample *pms)
{
// Try to enter the semaphore gate.
DWORD dwWaitResult = WaitForSingleObject( m_hSemaphore, INFINITE);
CheckPointer(pms,E_POINTER);
BYTE *pData;
HRESULT hr = pms->GetPointer(&pData);
if (FAILED(hr)) {
return hr;
}
// This function must hold the state lock because it calls
// FillPCMAudioBuffer().
CAutoLock lStateLock(m_pParent->pStateLock());
// This lock must be held because this function uses
// m_dwTempPCMBufferSize, m_hPCMToMSADPCMConversionStream,
// m_rtSampleTime, m_fFirstSampleDelivered and
// m_llSampleMediaTimeStart.
CAutoLock lShared(&m_cSharedState);
WAVEFORMATEX* pwfexCurrent = (WAVEFORMATEX*)m_mt.Format();
if (WAVE_FORMAT_PCM == pwfexCurrent->wFormatTag)
{
//m_Synth->FillPCMAudioBuffer(*pwfexCurrent, pData, pms->GetSize());
long size = pms->GetSize();
m_Loopback->FillPCMAudioBuffer(*pwfexCurrent, pData, size);
//if( size == 0 )return NOERROR;
hr = pms->SetActualDataLength(size);
if (FAILED(hr))
return hr;
}
else
{
// This filter only supports two audio formats: PCM and ADPCM.
/*
ASSERT(WAVE_FORMAT_ADPCM == pwfexCurrent->wFormatTag);
// Create PCM audio samples and then compress them. We use the
// Audio Compression Manager (ACM) API to convert the samples to
// the ADPCM format.
ACMSTREAMHEADER ACMStreamHeader;
ACMStreamHeader.cbStruct = sizeof(ACMStreamHeader);
ACMStreamHeader.fdwStatus = 0;
ACMStreamHeader.dwUser = 0;
ACMStreamHeader.cbSrcLength = m_dwTempPCMBufferSize;
ACMStreamHeader.cbSrcLengthUsed = 0;
ACMStreamHeader.dwSrcUser = 0;
ACMStreamHeader.pbDst = pData;
ACMStreamHeader.cbDstLength = pms->GetSize();
ACMStreamHeader.cbDstLengthUsed = 0;
ACMStreamHeader.dwDstUser = 0;
ACMStreamHeader.pbSrc = new BYTE[m_dwTempPCMBufferSize];
if (NULL == ACMStreamHeader.pbSrc) {
return E_OUTOFMEMORY;
}
WAVEFORMATEX wfexPCMAudio;
DerivePCMFormatFromADPCMFormatStructure(*pwfexCurrent, &wfexPCMAudio);
m_Synth->FillPCMAudioBuffer(wfexPCMAudio,
ACMStreamHeader.pbSrc,
ACMStreamHeader.cbSrcLength);
MMRESULT mmr = acmStreamPrepareHeader(m_hPCMToMSADPCMConversionStream,
&ACMStreamHeader,
0);
// acmStreamPrepareHeader() returns 0 if no errors occur.
if (mmr != 0) {
delete [] ACMStreamHeader.pbSrc;
return E_FAIL;
}
mmr = acmStreamConvert(m_hPCMToMSADPCMConversionStream,
&ACMStreamHeader,
ACM_STREAMCONVERTF_BLOCKALIGN);
MMRESULT mmrUnprepare = acmStreamUnprepareHeader(m_hPCMToMSADPCMConversionStream,
&ACMStreamHeader,
0);
delete [] ACMStreamHeader.pbSrc;
// acmStreamConvert() andacmStreamUnprepareHeader() returns 0 if no errors occur.
if ((mmr != 0) || (mmrUnprepare != 0)) {
return E_FAIL;
}
hr = pms->SetActualDataLength(ACMStreamHeader.cbDstLengthUsed);
if (FAILED(hr)) {
return hr;
}
*/
}
// Set the sample's time stamps.
CRefTime rtStart = m_rtSampleTime;
m_rtSampleTime = rtStart + (REFERENCE_TIME)(UNITS * pms->GetActualDataLength()) /
(REFERENCE_TIME)pwfexCurrent->nAvgBytesPerSec;
hr = pms->SetTime((REFERENCE_TIME*)&rtStart, (REFERENCE_TIME*)&m_rtSampleTime);
if (FAILED(hr)) {
return hr;
}
// Set the sample's properties.
hr = pms->SetPreroll(FALSE);
if (FAILED(hr)) {
return hr;
}
hr = pms->SetMediaType(NULL);
if (FAILED(hr)) {
return hr;
}
hr = pms->SetDiscontinuity(!m_fFirstSampleDelivered);
if (FAILED(hr)) {
return hr;
}
hr = pms->SetSyncPoint(!m_fFirstSampleDelivered);
if (FAILED(hr)) {
return hr;
}
LONGLONG llMediaTimeStart = m_llSampleMediaTimeStart;
DWORD dwNumAudioSamplesInPacket = (pms->GetActualDataLength() * BITS_PER_BYTE) /
(pwfexCurrent->nChannels * pwfexCurrent->wBitsPerSample);
LONGLONG llMediaTimeStop = m_llSampleMediaTimeStart + dwNumAudioSamplesInPacket;
hr = pms->SetMediaTime(&llMediaTimeStart, &llMediaTimeStop);
if (FAILED(hr)) {
return hr;
}
m_llSampleMediaTimeStart = llMediaTimeStop;
m_fFirstSampleDelivered = TRUE;
return NOERROR;
}
//
// Format Support
//
//
// GetMediaType
//
HRESULT CAudioLoopbackPin::GetMediaType(CMediaType *pmt)
{
CheckPointer(pmt,E_POINTER);
// The caller must hold the state lock because this function
// calls get_OutputFormat() and GetPCMFormatStructure().
// The function assumes that the state of the m_Synth
// object does not change between the two calls. The
// m_Synth object's state will not change if the
// state lock is held.
ASSERT(CritCheckIn(m_pParent->pStateLock()));
//WAVEFORMATEX *pwfex;
WAVEFORMATEX *pwfex = m_pParent->m_Loopback->Format();
SYNTH_OUTPUT_FORMAT ofCurrent = SYNTH_OF_PCM;
if(SYNTH_OF_PCM == ofCurrent)
{
pwfex = (WAVEFORMATEX *) pmt->AllocFormatBuffer(sizeof(WAVEFORMATEX));
if(NULL == pwfex)
{
return E_OUTOFMEMORY;
}
m_Loopback->GetPCMFormatStructure(pwfex);
}
else if(SYNTH_OF_MS_ADPCM == ofCurrent)
{
DWORD dwMaxWAVEFORMATEXSize;
MMRESULT mmr = acmMetrics(NULL, ACM_METRIC_MAX_SIZE_FORMAT,
(void*)&dwMaxWAVEFORMATEXSize);
// acmMetrics() returns 0 if no errors occur.
if(0 != mmr)
{
return E_FAIL;
}
pwfex = (WAVEFORMATEX *) pmt->AllocFormatBuffer(dwMaxWAVEFORMATEXSize);
if(NULL == pwfex)
{
return E_OUTOFMEMORY;
}
WAVEFORMATEX wfexSourceFormat;
m_Loopback->GetPCMFormatStructure(&wfexSourceFormat);
ZeroMemory(pwfex, dwMaxWAVEFORMATEXSize);
pwfex->wFormatTag = WAVE_FORMAT_ADPCM;
pwfex->cbSize = (USHORT)(dwMaxWAVEFORMATEXSize - sizeof(WAVEFORMATEX));
pwfex->nChannels = wfexSourceFormat.nChannels;
pwfex->nSamplesPerSec = wfexSourceFormat.nSamplesPerSec;
mmr = acmFormatSuggest(NULL,
&wfexSourceFormat,
pwfex,
dwMaxWAVEFORMATEXSize,
ACM_FORMATSUGGESTF_WFORMATTAG |
ACM_FORMATSUGGESTF_NSAMPLESPERSEC |
ACM_FORMATSUGGESTF_NCHANNELS);
// acmFormatSuggest() returns 0 if no errors occur.
if(0 != mmr)
{
return E_FAIL;
}
}
else
{
return E_UNEXPECTED;
}
return CreateAudioMediaType(pwfex, pmt, FALSE);
}
HRESULT CAudioLoopbackPin::CompleteConnect(IPin *pReceivePin)
{
// This lock must be held because this function uses
// m_hPCMToMSADPCMConversionStream, m_fFirstSampleDelivered
// and m_llSampleMediaTimeStart.
CAutoLock lShared(&m_cSharedState);
HRESULT hr;
WAVEFORMATEX *pwfexCurrent = (WAVEFORMATEX*)m_mt.Format();
if(WAVE_FORMAT_PCM == pwfexCurrent->wFormatTag)
{
}
else if(WAVE_FORMAT_ADPCM == pwfexCurrent->wFormatTag)
{
WAVEFORMATEX wfexSourceFormat;
DerivePCMFormatFromADPCMFormatStructure(*pwfexCurrent, &wfexSourceFormat);
MMRESULT mmr = acmStreamOpen(&m_hPCMToMSADPCMConversionStream,
NULL,
&wfexSourceFormat,
pwfexCurrent,
NULL,
0,
0,
ACM_STREAMOPENF_NONREALTIME);
// acmStreamOpen() returns 0 if an no errors occur.
if(mmr != 0)
{
return E_FAIL;
}
}
else
{
ASSERT(NULL == m_hPCMToMSADPCMConversionStream);
}
hr = CDynamicSourceStream::CompleteConnect(pReceivePin);
if(FAILED(hr))
{
if(WAVE_FORMAT_ADPCM == pwfexCurrent->wFormatTag)
{
// acmStreamClose() should never fail because m_hPCMToMSADPCMConversionStream
// holds a valid ACM stream handle and all operations using the handle are
// synchronous.
EXECUTE_ASSERT(0 == acmStreamClose(m_hPCMToMSADPCMConversionStream, 0));
m_hPCMToMSADPCMConversionStream = NULL;
}
return hr;
}
m_fFirstSampleDelivered = FALSE;
m_llSampleMediaTimeStart = 0;
return S_OK;
}
void CAudioLoopbackPin::DerivePCMFormatFromADPCMFormatStructure(const WAVEFORMATEX& wfexADPCM,
WAVEFORMATEX* pwfexPCM)
{
ASSERT(pwfexPCM);
if (!pwfexPCM)
return;
pwfexPCM->wFormatTag = WAVE_FORMAT_PCM;
pwfexPCM->wBitsPerSample = 16;
pwfexPCM->cbSize = 0;
pwfexPCM->nChannels = wfexADPCM.nChannels;
pwfexPCM->nSamplesPerSec = wfexADPCM.nSamplesPerSec;
pwfexPCM->nBlockAlign = (WORD)((pwfexPCM->nChannels * pwfexPCM->wBitsPerSample) / BITS_PER_BYTE);
pwfexPCM->nAvgBytesPerSec = pwfexPCM->nBlockAlign * pwfexPCM->nSamplesPerSec;
}
HRESULT CAudioLoopbackPin::BreakConnect(void)
{
// This lock must be held because this function uses
// m_hPCMToMSADPCMConversionStream and m_dwTempPCMBufferSize.
CAutoLock lShared(&m_cSharedState);
HRESULT hr = CDynamicSourceStream::BreakConnect();
if(FAILED(hr))
{
return hr;
}
if(NULL != m_hPCMToMSADPCMConversionStream)
{
// acmStreamClose() should never fail because m_hPCMToMSADPCMConversionStream
// holds a valid ACM stream handle and all operations using the handle are
// synchronous.
EXECUTE_ASSERT(0 == acmStreamClose(m_hPCMToMSADPCMConversionStream, 0));
m_hPCMToMSADPCMConversionStream = NULL;
m_dwTempPCMBufferSize = 0;
}
if(m_dwAdviseToken){
m_pClock->Unadvise(m_dwAdviseToken);
m_dwAdviseToken = NULL;
}
return S_OK;
}
//
// DecideBufferSize
//
// This will always be called after the format has been sucessfully
// negotiated. So we have a look at m_mt to see what format we agreed to.
// Then we can ask for buffers of the correct size to contain them.
HRESULT CAudioLoopbackPin::DecideBufferSize(IMemAllocator *pAlloc,
ALLOCATOR_PROPERTIES *pProperties)
{
// The caller should always hold the shared state lock
// before calling this function. This function must hold
// the shared state lock because it uses m_hPCMToMSADPCMConversionStream
// m_dwTempPCMBufferSize.
ASSERT(CritCheckIn(&m_cSharedState));
CheckPointer(pAlloc,E_POINTER);
CheckPointer(pProperties,E_POINTER);
WAVEFORMATEX *pwfexCurrent = (WAVEFORMATEX*)m_mt.Format();
//WAVEFORMATEX *pwfexCurrent = m_pParent->m_Loopback->Format();
if(WAVE_FORMAT_PCM == pwfexCurrent->wFormatTag)
{
pProperties->cbBuffer = WaveBufferSize;
}
else
{
// This filter only supports two formats: PCM and ADPCM.
ASSERT(WAVE_FORMAT_ADPCM == pwfexCurrent->wFormatTag);
pProperties->cbBuffer = pwfexCurrent->nBlockAlign;
MMRESULT mmr = acmStreamSize(m_hPCMToMSADPCMConversionStream,
pwfexCurrent->nBlockAlign,
&m_dwTempPCMBufferSize,
ACM_STREAMSIZEF_DESTINATION);
// acmStreamSize() returns 0 if no error occurs.
if(0 != mmr)
{
return E_FAIL;
}
}
int nBitsPerSample = pwfexCurrent->wBitsPerSample;
int nSamplesPerSec = pwfexCurrent->nSamplesPerSec;
int nChannels = pwfexCurrent->nChannels;
pProperties->cBuffers = (nChannels * nSamplesPerSec * nBitsPerSample) /
(pProperties->cbBuffer * BITS_PER_BYTE);
// Get 1/2 second worth of buffers
pProperties->cBuffers /= 2;
if(pProperties->cBuffers < 1)
pProperties->cBuffers = 1 ;
// Ask the allocator to reserve us the memory
ALLOCATOR_PROPERTIES Actual;
HRESULT hr = pAlloc->SetProperties(pProperties,&Actual);
if(FAILED(hr))
{
return hr;
}
// Is this allocator unsuitable
if(Actual.cbBuffer < pProperties->cbBuffer)
{
return E_FAIL;
}
return NOERROR;
}
//
// Active
//
HRESULT CAudioLoopbackPin::Active(void)
{
// This lock must be held because the function
// uses m_rtSampleTime, m_fFirstSampleDelivered
// and m_llSampleMediaTimeStart.
CAutoLock lShared(&m_cSharedState);
HRESULT hr = CDynamicSourceStream::Active();
if(FAILED(hr))
{
return hr;
}
m_rtSampleTime = 0;
m_fFirstSampleDelivered = FALSE;
m_llSampleMediaTimeStart = 0;
return NOERROR;
}
HRESULT CAudioLoopbackPin::Run(REFERENCE_TIME tStart)
{
if( !m_dwAdviseToken ){
HRESULT hr = S_OK;
m_hSemaphore = CreateSemaphore(NULL, 0, 0x7FFFFFFF, NULL);
const REFERENCE_TIME rtPeriodTime = m_Loopback->m_hnsDefaultDevicePeriod;
hr = m_pClock->AdvisePeriodic(tStart+rtPeriodTime, rtPeriodTime, (HSEMAPHORE)m_hSemaphore, &m_dwAdviseToken);
//hr = m_pClock->AdvisePeriodic(1, rtPeriodTime, (HSEMAPHORE)m_hSemaphore, &m_dwAdviseToken);
if( hr != S_OK ){
// TODO: log this.
return hr;
}
}
//return CBaseOutputPin::Run(0);
return CBaseOutputPin::Run(tStart);
}
HRESULT CAudioLoopbackPin::SetSyncSource(IReferenceClock *pClock)
{
m_pClock = pClock;
return S_OK;
}
// -------------------------------------------------------------------------
// CAudioLoopback
// -------------------------------------------------------------------------
HRESULT get_default_device(IMMDevice **ppMMDevice) {
HRESULT hr = S_OK;
IMMDeviceEnumerator *pMMDeviceEnumerator;
// activate a device enumerator
hr = CoCreateInstance(
__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL,
__uuidof(IMMDeviceEnumerator),
(void**)&pMMDeviceEnumerator
);
if (FAILED(hr)) {
Log( "CoCreateInstance(IMMDeviceEnumerator) failed: hr = 0x%08x",hr);
return hr;
}
// get the default render endpoint
hr = pMMDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, ppMMDevice);
pMMDeviceEnumerator->Release();
if (FAILED(hr)) {
Log("IMMDeviceEnumerator::GetDefaultAudioEndpoint failed: hr = 0x%08x",hr);
return hr;
}
return S_OK;
}
CAudioLoopback::CAudioLoopback(CCritSec* pStateLock)
:pnFrames(0)
,bFirstPacket(true)
{
Initialize(); // should take out of constructor
}
CAudioLoopback::~CAudioLoopback(){
AvRevertMmThreadCharacteristics(m_hTask);
m_pAudioClient->Release();
m_pAudioCaptureClient->Release();
CoTaskMemFree(m_pwfx);
delete[] m_silenceBuf;
}
void CAudioLoopback::GetPCMFormatStructure(WAVEFORMATEX* pwfex)
{
ASSERT(pwfex);
if (!pwfex)
return;
pwfex->wFormatTag = WAVE_FORMAT_PCM;
pwfex->nChannels = m_pwfx->nChannels;
pwfex->nSamplesPerSec = m_pwfx->nSamplesPerSec;
pwfex->wBitsPerSample = m_pwfx->wBitsPerSample;
pwfex->nBlockAlign = m_pwfx->nBlockAlign;
pwfex->nAvgBytesPerSec = pwfex->nBlockAlign * pwfex->nSamplesPerSec;
pwfex->cbSize = m_pwfx->cbSize;
}
HRESULT CAudioLoopback::Initialize()
{
IMMDevice *pMMDevice;
HRESULT hr;
hr = get_default_device(&pMMDevice);
if (FAILED(hr)) {
return hr;
}
// activate an IAudioClient
hr = pMMDevice->Activate(
__uuidof(IAudioClient),
CLSCTX_ALL, NULL,
(void**)&m_pAudioClient
);
if (FAILED(hr)) {
Log("IMMDevice::Activate(IAudioClient) failed: hr = 0x%08x",hr);
return hr;
}
// get the default device periodicity
hr = m_pAudioClient->GetDevicePeriod(&m_hnsDefaultDevicePeriod, NULL);
if (FAILED(hr)) {
Log("IAudioClient::GetDevicePeriod failed: hr = 0x%08x", hr);
m_pAudioClient->Release();
return hr;
}
Log("GetDevicePeriod set %d\n", m_hnsDefaultDevicePeriod);
Log("Milliseconds beween fire %d\n", (int)(m_hnsDefaultDevicePeriod / 2 / (10 * 1000))); // convert to milliseconds
// get the default device format
hr = m_pAudioClient->GetMixFormat(&m_pwfx);
if (FAILED(hr)) {
Log("IAudioClient::GetMixFormat failed: hr = 0x%08x",hr);
return hr;
}
//if (bInt16) {
if( true ){
// coerce int-16 wave format
// can do this in-place since we're not changing the size of the format
// also, the engine will auto-convert from float to int for us
switch (m_pwfx->wFormatTag) {
case WAVE_FORMAT_IEEE_FLOAT:
m_pwfx->wFormatTag = WAVE_FORMAT_PCM;
m_pwfx->wBitsPerSample = 16;
m_pwfx->nBlockAlign = m_pwfx->nChannels * m_pwfx->wBitsPerSample / 8;
m_pwfx->nAvgBytesPerSec = m_pwfx->nBlockAlign * m_pwfx->nSamplesPerSec;
break;
case WAVE_FORMAT_EXTENSIBLE:
{
// naked scope for case-local variable
PWAVEFORMATEXTENSIBLE pEx = reinterpret_cast<PWAVEFORMATEXTENSIBLE>(m_pwfx);
if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, pEx->SubFormat)) {
pEx->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
pEx->Samples.wValidBitsPerSample = 16;
m_pwfx->wBitsPerSample = 16;
m_pwfx->nBlockAlign = m_pwfx->nChannels * m_pwfx->wBitsPerSample / 8;
m_pwfx->nAvgBytesPerSec = m_pwfx->nBlockAlign * m_pwfx->nSamplesPerSec;
} else {
Log("Don't know how to coerce mix format to int-16\n");
return E_UNEXPECTED;
}
}
break;
default:
Log("Don't know how to coerce WAVEFORMATEX with wFormatTag = 0x%08x to int-16",m_pwfx->wFormatTag);
return E_UNEXPECTED;
}
}
Log("wFormatTag: %d\n",m_pwfx->wFormatTag);
Log("wBitsPerSampl: %d\n",m_pwfx->wBitsPerSample);
Log("nChannels: %d\n",m_pwfx->nChannels);
Log("wBitsPerSample: %d\n",m_pwfx->wBitsPerSample);
Log("nBlockAlign: %d\n",m_pwfx->nBlockAlign);
Log("nSamplesPerSec: %d\n",m_pwfx->nSamplesPerSec);
Log("nAvgBytesPerSec: %d\n",m_pwfx->nAvgBytesPerSec);
m_silenceBuf = new BYTE[SILENCE_BUF_SIZE];
memset(m_silenceBuf, 128, SILENCE_BUF_SIZE);
UINT32 nBlockAlign = m_pwfx->nBlockAlign;
// call IAudioClient::Initialize
// note that AUDCLNT_STREAMFLAGS_LOOPBACK and AUDCLNT_STREAMFLAGS_EVENTCALLBACK
// do not work together...
// the "data ready" event never gets set
// so we're going to do a timer-driven loop
hr = m_pAudioClient->Initialize(
AUDCLNT_SHAREMODE_SHARED,
AUDCLNT_STREAMFLAGS_LOOPBACK,
0, 0, m_pwfx, 0
);
if (FAILED(hr)) {
Log( "IAudioClient::Initialize failed: hr = 0x%08x", hr);
m_pAudioClient->Release();
return hr;
}
// activate an IAudioCaptureClient
hr = m_pAudioClient->GetService(
__uuidof(IAudioCaptureClient),
(void**)&m_pAudioCaptureClient
);
if (FAILED(hr)) {
Log( "IAudioClient::GetService(IAudioCaptureClient) failed: hr 0x%08x", hr);
return hr;
}
// register with MMCSS
DWORD nTaskIndex = 0;
m_hTask = AvSetMmThreadCharacteristicsW(L"Capture", &nTaskIndex);
if (NULL == m_hTask) {
DWORD dwErr = GetLastError();
Log("AvSetMmThreadCharacteristics failed: last error = %u\n", dwErr);
return HRESULT_FROM_WIN32(dwErr);
}
// call IAudioClient::Start
hr = m_pAudioClient->Start();
if (FAILED(hr)) {
Log("IAudioClient::Start failed: hr = 0x%08x\n", hr);
return hr;
}
}
HRESULT CAudioLoopback::FillPCMAudioBuffer(const WAVEFORMATEX& wfex, BYTE pBuf[], long& iSize)
{
HRESULT hr = S_OK;
// grab next audio chunk...
UINT32 nNextPacketSize;
hr = m_pAudioCaptureClient->GetNextPacketSize(&nNextPacketSize); // get next packet, if one is ready...
if (FAILED(hr)) {
Log("IAudioCaptureClient::GetNextPacketSize failed after %u frames: hr = 0x%08x\n", pnFrames, hr);
m_pAudioClient->Stop();
return hr;
}
// get the captured data
BYTE *pData;
UINT32 nNumFramesToRead;
DWORD dwFlags;
hr = m_pAudioCaptureClient->GetBuffer(
&pData,
&nNumFramesToRead,
&dwFlags,
NULL,
NULL
);
if (FAILED(hr)) {
Log("IAudioCaptureClient::GetBuffer failed after %u frames: hr = 0x%08x\n", pnFrames, hr);
m_pAudioClient->Stop();
return hr;
}
if (0 == nNumFramesToRead) {
// We have silence in this case...
iSize = SILENCE_BUF_SIZE;