Skip to content

Commit 3e2e935

Browse files
committed
Adapt upstream OBSproject/libdshowcapture. Prepare for version 1.0.2
1 parent 585bc62 commit 3e2e935

File tree

8 files changed

+76
-16
lines changed

8 files changed

+76
-16
lines changed

.github/workflows/msbuild.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@ jobs:
2424
runs-on: windows-latest
2525

2626
steps:
27-
- uses: actions/checkout@v3
27+
- uses: actions/checkout@v4
2828
with:
2929
submodules: recursive
3030

3131
- name: Add MSBuild to PATH
32-
uses: microsoft/setup-msbuild@v1.0.2
32+
uses: microsoft/setup-msbuild@v2
3333

3434
- name: Restore NuGet packages
3535
working-directory: ${{env.GITHUB_WORKSPACE}}
3636
run: nuget restore ${{env.SOLUTION_FILE_PATH}}
3737

3838
- name: Build
3939
working-directory: ${{env.GITHUB_WORKSPACE}}
40-
# Add additional options to the MSBuild command line here (like platform or verbosity level).
41-
# See https://docs.microsoft.com/visualstudio/msbuild/msbuild-command-line-reference
4240
run: msbuild /m /p:Configuration=${{env.BUILD_CONFIGURATION}} ${{env.SOLUTION_FILE_PATH}}
41+
42+
- name: Pack
43+
working-directory: ${{env.GITHUB_WORKSPACE}}
44+
run: msbuild /m /t:NetLibDirectshowCapture:pack /p:Configuration=${{env.BUILD_CONFIGURATION}} ${{env.SOLUTION_FILE_PATH}}

.github/workflows/nuget-release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ jobs:
1515
runs-on: windows-latest
1616

1717
steps:
18-
- uses: actions/checkout@v3
18+
- uses: actions/checkout@v4
1919
with:
2020
submodules: recursive
2121

2222
- name: Add MSBuild to PATH
23-
uses: microsoft/setup-msbuild@v1.0.2
23+
uses: microsoft/setup-msbuild@v2
2424

2525
- name: Restore NuGet packages
2626
working-directory: ${{env.GITHUB_WORKSPACE}}

NetLibDirectshowCapture/AudioConfig.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,15 @@ namespace NetLibDirectshowCapture
128128
{
129129
_native->mode = static_cast<DShow::AudioMode>(value);
130130
}
131+
132+
int NetLibDirectshowCapture::AudioConfig::Buffer::get()
133+
{
134+
return _native->buffer;
135+
}
136+
137+
void AudioConfig::Buffer::set(int value)
138+
{
139+
_native->buffer = value;
140+
}
141+
131142
}

NetLibDirectshowCapture/NetLibDirectshowCapture.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,16 @@ namespace NetLibDirectshowCapture
532532
AudioMode get();
533533
void set(AudioMode value);
534534
}
535+
536+
/// <summary>
537+
/// Desired audio buffer size
538+
/// </summary>
539+
property int Buffer
540+
{
541+
int get();
542+
void set(int value);
543+
}
544+
535545
};
536546

537547
public ref class Device : public ManagedObjectBase<DShow::Device>

NetLibDirectshowCapture/NetLibDirectshowCapture.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
<Keyword>NetCoreCProj</Keyword>
2525
<RootNamespace>NetLibDirectshowCapture</RootNamespace>
2626
<TargetFramework>net8.0</TargetFramework>
27-
<WindowsTargetPlatformMinVersion>7.0</WindowsTargetPlatformMinVersion>
27+
<WindowsTargetPlatformMinVersion>10.0.20348.0</WindowsTargetPlatformMinVersion>
2828
<EnableManagedPackageReferenceSupport>true</EnableManagedPackageReferenceSupport>
2929
<IsPackable>true</IsPackable>
3030
<IncludeBuildOutput>true</IncludeBuildOutput>
3131
<PackageId>NetLibDirectshowCapture</PackageId>
32-
<PackageVersion>1.0.1</PackageVersion>
32+
<PackageVersion>1.0.2</PackageVersion>
3333
<Authors>Shirui Cao</Authors>
3434
<Title>.NET Wrapper of OBSproject/libdshowcapture</Title>
3535
<Description>

NetLibDirectshowCaptureExample/MainWindow.xaml.cs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public MainWindow()
5353
{
5454
_waveOut = new WaveOutEvent();
5555
_waveBuffer = new BufferedWaveProvider(new WaveFormat(48000, 16, 2));
56+
_waveBuffer.BufferDuration = TimeSpan.FromMilliseconds(320);
5657
_displayingBitmap = new WriteableBitmap(1920, 1080, 96, 96, PixelFormats.Bgr24, null);
5758
InitializeComponent();
5859
DSLogger.CallBack = (t, s) =>
@@ -106,12 +107,10 @@ private void OnDeviceMenuItemClick(object sender, RoutedEventArgs e, VideoDevice
106107
InternalFormat = VideoFormat.YUY2,
107108
Format = VideoFormat.RGB24,
108109
};
109-
var audioDevices = Device.EnumAudioDevices();
110-
var audioDevice = audioDevices.Find(d => d.Name.Contains(videoConfig.Name, StringComparison.InvariantCultureIgnoreCase));
110+
111+
// TODO: Use WMI to find audio device through device tree
111112
AudioConfig audioConfig = new AudioConfig()
112113
{
113-
Name = audioDevice?.Name,
114-
Path = audioDevice?.Path,
115114
Channels = 2,
116115
SampleRate = 48000,
117116
Format = AudioFormat.Wave16bit,
@@ -120,6 +119,15 @@ private void OnDeviceMenuItemClick(object sender, RoutedEventArgs e, VideoDevice
120119
UseDefaultConfig = false
121120
};
122121

122+
var audioDevices = Device.EnumAudioDevices();
123+
var audioDevice = audioDevices.Find(d => d.Name.Contains(videoConfig.Name, StringComparison.InvariantCultureIgnoreCase))
124+
?? FindAttachedAudioWithSpecialHandling(device, audioDevices);
125+
if (audioDevice != null)
126+
{
127+
audioConfig.Name = audioDevice.Name;
128+
audioConfig.Path = audioDevice.Path;
129+
}
130+
123131
// Special handling for Corsair Elgato devices
124132
if (videoConfig.Name.Contains("Game Capture", StringComparison.InvariantCultureIgnoreCase) ||
125133
videoConfig.Name.Contains("Elgato", StringComparison.InvariantCultureIgnoreCase))
@@ -144,7 +152,11 @@ private void OnDeviceMenuItemClick(object sender, RoutedEventArgs e, VideoDevice
144152
}
145153

146154
_device.VideoConfiguration = videoConfig;
147-
_device.AudioConfiguration = audioConfig;
155+
156+
if (audioConfig.Name != null || audioConfig.Path != null)
157+
{
158+
_device.AudioConfiguration = audioConfig;
159+
}
148160

149161
if (!_device.ConnectFilters())
150162
{
@@ -159,6 +171,23 @@ private void OnDeviceMenuItemClick(object sender, RoutedEventArgs e, VideoDevice
159171
CompositionTarget.Rendering += UpdateBitmapFromRawArray;
160172
}
161173

174+
private static AudioDevice FindAttachedAudioWithSpecialHandling(VideoDevice videoDevice, List<AudioDevice> audioDevices)
175+
{
176+
// ACASIS PCI-E Capture Cards, E.g., "HDPro 1" video is paired with "HDPro Audio 1" audio
177+
if (videoDevice.Name.Contains("HDPro", StringComparison.InvariantCultureIgnoreCase))
178+
{
179+
string audioName = videoDevice.Name.Replace("HDPro", "HDPro Audio");
180+
return audioDevices.Find(a => a.Name.Contains(audioName, StringComparison.InvariantCultureIgnoreCase));
181+
}
182+
// ACASIS Thunderbolt Capture Cards, E.g., "UHD Video 1" video is pared with "UHD Audio 1"
183+
if (videoDevice.Name.Contains("UHD Video", StringComparison.InvariantCultureIgnoreCase))
184+
{
185+
string audioName = videoDevice.Name.Replace("UHD Video", "UHD Audio");
186+
return audioDevices.Find(a => a.Name.Contains(audioName, StringComparison.InvariantCultureIgnoreCase));
187+
}
188+
return null;
189+
}
190+
162191

163192
private void OnRefreshClick(object sender, RoutedEventArgs e)
164193
{
@@ -174,7 +203,15 @@ private void OnAudioFrame(object sender, AudioCapturedEventArgs e)
174203
_rawWaveArray = new byte[e.Length];
175204
}
176205
Marshal.Copy(e.Ptr, _rawWaveArray, 0, e.Length);
177-
_waveBuffer.AddSamples(_rawWaveArray, 0, e.Length);
206+
try
207+
{
208+
_waveBuffer.AddSamples(_rawWaveArray, 0, e.Length);
209+
}
210+
catch (InvalidOperationException) // buffer full
211+
{
212+
_waveBuffer.ClearBuffer();
213+
}
214+
178215
if (_waveOut.PlaybackState != PlaybackState.Playing)
179216
{
180217
_waveOut.Init(_waveBuffer);

NetLibDirectshowCaptureExample/NetLibDirectshowCaptureExample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
5-
<TargetFramework>net8.0-windows7.0</TargetFramework>
5+
<TargetFramework>net8.0-windows10.0.20348.0</TargetFramework>
66
<UseWPF>true</UseWPF>
77
</PropertyGroup>
88

0 commit comments

Comments
 (0)