File: wasapi-loopback.patch

package info (click to toggle)
audacity 2.4.2~dfsg0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 174,812 kB
  • sloc: ansic: 399,725; cpp: 292,221; python: 53,279; sh: 28,982; lisp: 8,078; makefile: 4,045; xml: 2,235; perl: 2,110; java: 1,551; asm: 545; pascal: 395; cs: 122; sed: 42; awk: 35
file content (181 lines) | stat: -rw-r--r-- 7,452 bytes parent folder | download
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
diff --git a/lib-src/portaudio-v19/include/pa_win_wasapi.h b/lib-src/portaudio-v19/include/pa_win_wasapi.h
index a4416b121..1999e8dad 100644
--- a/lib-src/portaudio-v19/include/pa_win_wasapi.h
+++ b/lib-src/portaudio-v19/include/pa_win_wasapi.h
@@ -564,6 +564,15 @@ const wchar_t *PaWasapi_GetInputDeviceID( PaStream *s );
 */
 const wchar_t *PaWasapi_GetOutputDeviceID( PaStream *s );
 
+/** Returns device loopback indicator.
+
+ @param nDevice device index.
+
+ @return 0 = Not loopback, 1 = loopback, < 0 = PaErrorCode
+         if PortAudio is not initialized or an error is encountered.
+*/
+int PaWasapi_IsLoopback( PaDeviceIndex nDevice );
+
 
 /*
     IMPORTANT:
diff --git a/lib-src/portaudio-v19/src/hostapi/wasapi/pa_win_wasapi.c b/lib-src/portaudio-v19/src/hostapi/wasapi/pa_win_wasapi.c
index f64049ad5..4b28f7f0b 100644
--- a/lib-src/portaudio-v19/src/hostapi/wasapi/pa_win_wasapi.c
+++ b/lib-src/portaudio-v19/src/hostapi/wasapi/pa_win_wasapi.c
@@ -449,6 +449,9 @@ typedef struct PaWasapiDeviceInfo
 
 	// Formfactor
 	EndpointFormFactor formFactor;
+
+	// Loopback indicator
+	int loopBack;
 }
 PaWasapiDeviceInfo;
 
@@ -1674,6 +1677,8 @@ static PaError CreateDeviceList(PaWasapiHostApiRepresentation *paWasapi, PaHostA
 	IMMDeviceEnumerator *pEnumerator = NULL;
 #endif
 	IAudioClient *tmpClient;
+	UINT renderCount;
+	INT devIndex;
 
 	// Make sure device list empty
 	if ((paWasapi->deviceCount != 0) || (hostApi->info.deviceCount != 0))
@@ -1738,6 +1743,19 @@ static PaError CreateDeviceList(PaWasapiHostApiRepresentation *paWasapi, PaHostA
         }
     }
 
+	hr = IMMDeviceEnumerator_EnumAudioEndpoints(pEnumerator, eRender, DEVICE_STATE_ACTIVE, &pEndPoints);
+	// We need to set the result to a value otherwise we will return paNoError
+	// [IF_FAILED_JUMP(hResult, error);]
+	IF_FAILED_INTERNAL_ERROR_JUMP(hr, result, error);
+
+	hr = IMMDeviceCollection_GetCount(pEndPoints, &renderCount);
+	// We need to set the result to a value otherwise we will return paNoError
+	// [IF_FAILED_JUMP(hResult, error);]
+	IF_FAILED_INTERNAL_ERROR_JUMP(hr, result, error);
+
+	SAFE_RELEASE(pEndPoints);
+	pEndPoints = NULL;
+
     hr = IMMDeviceEnumerator_EnumAudioEndpoints(pEnumerator, eAll, DEVICE_STATE_ACTIVE, &pEndPoints);
 	// We need to set the result to a value otherwise we will return paNoError
 	// [IF_FAILED_JUMP(hResult, error);]
@@ -1748,6 +1766,7 @@ static PaError CreateDeviceList(PaWasapiHostApiRepresentation *paWasapi, PaHostA
 	// [IF_FAILED_JUMP(hResult, error);]
 	IF_FAILED_INTERNAL_ERROR_JUMP(hr, result, error);
 
+	paWasapi->deviceCount += renderCount;
 #else
 	// Determine number of available devices by activating AudioClient for render and capture data flows
 	if (SUCCEEDED(ActivateAudioInterface_WINRT(eRender, GetAudioClientIID(), &tmpClient)))
@@ -1797,7 +1816,7 @@ static PaError CreateDeviceList(PaWasapiHostApiRepresentation *paWasapi, PaHostA
         }
 		memset(deviceInfoArray, 0, sizeof(PaDeviceInfo) * deviceCount);
 
-        for (i = 0; i < paWasapi->deviceCount; ++i)
+        for (devIndex = 0, i = 0; i < paWasapi->deviceCount; ++i, ++devIndex)
 		{
             PaDeviceInfo *deviceInfo  = &deviceInfoArray[i];
             deviceInfo->structVersion = 2;
@@ -1807,7 +1826,7 @@ static PaError CreateDeviceList(PaWasapiHostApiRepresentation *paWasapi, PaHostA
 			PA_DEBUG(("WASAPI: ---------------\n"));
 
 		#ifndef PA_WINRT
-            hr = IMMDeviceCollection_Item(pEndPoints, i, &paWasapi->devInfo[i].device);
+            hr = IMMDeviceCollection_Item(pEndPoints, devIndex, &paWasapi->devInfo[i].device);
 			// We need to set the result to a value otherwise we will return paNoError
 			// [IF_FAILED_JUMP(hResult, error);]
 			IF_FAILED_INTERNAL_ERROR_JUMP(hr, result, error);
@@ -2039,6 +2058,42 @@ static PaError CreateDeviceList(PaWasapiHostApiRepresentation *paWasapi, PaHostA
 
             hostApi->deviceInfos[i] = deviceInfo;
             ++hostApi->info.deviceCount;
+
+			if (paWasapi->devInfo[i].flow == eRender)
+			{
+				char *deviceName;
+
+				memcpy(&deviceInfoArray[i + 1], deviceInfo, sizeof(*deviceInfo));
+				memcpy(&paWasapi->devInfo[i + 1], &paWasapi->devInfo[i], sizeof(*paWasapi->devInfo));
+
+				i++;
+				paWasapi->devInfo[i].loopBack = 1;
+
+				deviceInfo = &deviceInfoArray[i];
+
+				deviceInfo->maxInputChannels			= deviceInfo->maxOutputChannels;
+				deviceInfo->defaultHighInputLatency		= deviceInfo->defaultHighOutputLatency;
+				deviceInfo->defaultLowInputLatency		= deviceInfo->defaultLowOutputLatency;
+				deviceInfo->maxOutputChannels			= 0;
+				deviceInfo->defaultHighOutputLatency	= 0;
+				deviceInfo->defaultLowOutputLatency		= 0;
+				PA_DEBUG(("WASAPI:%d| def.SR[%d] max.CH[%d] latency{hi[%f] lo[%f]}\n", i, (UINT32)deviceInfo->defaultSampleRate,
+						 deviceInfo->maxInputChannels, (float)deviceInfo->defaultHighInputLatency, (float)deviceInfo->defaultLowInputLatency));
+
+				IMMDevice_AddRef(paWasapi->devInfo[i].device);
+
+				deviceName = (char *)PaUtil_GroupAllocateMemory(paWasapi->allocations, MAX_STR_LEN + 1);
+				if (deviceName == NULL)
+				{
+					result = paInsufficientMemory;
+					goto error;
+				}
+				_snprintf(deviceName, MAX_STR_LEN-1, "%s (loopback)", deviceInfo->name);
+				deviceInfo->name = deviceName;
+
+				hostApi->deviceInfos[i] = deviceInfo;
+				++hostApi->info.deviceCount;
+			}
         }
 
 	#if defined(PA_WASAPI_MAX_CONST_DEVICE_COUNT) && (PA_WASAPI_MAX_CONST_DEVICE_COUNT > 0)
@@ -2392,6 +2447,29 @@ int PaWasapi_GetDeviceRole( PaDeviceIndex device )
 	return paWasapi->devInfo[ index ].formFactor;
 }
 
+// ------------------------------------------------------------------------------------------
+int PaWasapi_IsLoopback( PaDeviceIndex nDevice )
+{
+	PaError ret;
+	PaDeviceIndex index;
+
+	// Get API
+	PaWasapiHostApiRepresentation *paWasapi = _GetHostApi(&ret);
+	if (paWasapi == NULL)
+		return paNotInitialized;
+
+	// Get device index
+	ret = PaUtil_DeviceIndexToHostApiDeviceIndex(&index, nDevice, &paWasapi->inheritedHostApiRep);
+	if (ret != paNoError)
+		return ret;
+
+	// Validate index
+	if ((UINT32)index >= paWasapi->deviceCount)
+		return paInvalidDevice;
+
+	return paWasapi->devInfo[ index ].loopBack;
+}
+
 // ------------------------------------------------------------------------------------------
 PaError PaWasapi_GetFramesPerHostBuffer( PaStream *pStream, unsigned int *pInput, unsigned int *pOutput )
 {
@@ -3055,7 +3133,7 @@ static HRESULT CreateAudioClient(PaWasapiStream *pStream, PaWasapiSubStream *pSu
 	if ((params->channelCount == 1) && (pSub->wavex.Format.nChannels == 2))
 	{
 		// select mixer
-		pSub->monoMixer = GetMonoToStereoMixer(&pSub->wavex, (pInfo->flow == eRender ? MIX_DIR__1TO2 : MIX_DIR__2TO1_L));
+		pSub->monoMixer = GetMonoToStereoMixer(&pSub->wavex, (output ? MIX_DIR__1TO2 : MIX_DIR__2TO1_L));
 		if (pSub->monoMixer == NULL)
 		{
 			(*pa_error) = paInvalidChannelCount;
@@ -3618,6 +3696,9 @@ static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi,
 			((inputStreamInfo != NULL) && (inputStreamInfo->flags & paWinWasapiAutoConvert)))
 			stream->in.streamFlags |= (AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM | AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY);
 
+        if (info->flow == eRender)
+            stream->in.streamFlags |= AUDCLNT_STREAMFLAGS_LOOPBACK;
+
 		// Fill parameters for Audio Client creation
 		stream->in.params.device_info       = info;
 		stream->in.params.stream_params     = (*inputParameters);