Compare commits

..

No commits in common. "be67abaf64fc4086dac9ade2b29e8f0a7fb50d2a" and "faf091ac2c2c07b5f83d4e23b7b898e70f1bdda9" have entirely different histories.

2 changed files with 48 additions and 96 deletions

View File

@ -18,9 +18,9 @@
<PackageTags>EonaCat, Audio, Volume, Mixer .NET Standard, Jeroen, Saey</PackageTags> <PackageTags>EonaCat, Audio, Volume, Mixer .NET Standard, Jeroen, Saey</PackageTags>
<PackageReleaseNotes></PackageReleaseNotes> <PackageReleaseNotes></PackageReleaseNotes>
<Description>EonaCat VolumeMixer</Description> <Description>EonaCat VolumeMixer</Description>
<Version>1.0.6</Version> <Version>1.0.5</Version>
<AssemblyVersion>1.0.0.6</AssemblyVersion> <AssemblyVersion>1.0.0.5</AssemblyVersion>
<FileVersion>1.0.0.6</FileVersion> <FileVersion>1.0.0.5</FileVersion>
<PackageIcon>icon.png</PackageIcon> <PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>https://git.saey.me/EonaCat/EonaCat.VolumeMixer</RepositoryUrl> <RepositoryUrl>https://git.saey.me/EonaCat/EonaCat.VolumeMixer</RepositoryUrl>
<RepositoryType>git</RepositoryType> <RepositoryType>git</RepositoryType>

View File

@ -49,12 +49,9 @@ namespace EonaCat.VolumeMixer.Managers
lock (_syncLock) lock (_syncLock)
{ {
IMultiMediaDeviceCollection deviceCollection = null;
IMultiMediaDevice defaultDevice = null;
try try
{ {
var result = _deviceEnumerator.EnumAudioEndpoints(dataFlow, DeviceState.Active, out deviceCollection); var result = _deviceEnumerator.EnumAudioEndpoints(dataFlow, DeviceState.Active, out var deviceCollection);
if (result != 0 || deviceCollection == null) if (result != 0 || deviceCollection == null)
{ {
return devices; return devices;
@ -63,16 +60,18 @@ namespace EonaCat.VolumeMixer.Managers
result = deviceCollection.GetCount(out var count); result = deviceCollection.GetCount(out var count);
if (result != 0) if (result != 0)
{ {
ComHelper.ReleaseComObject(deviceCollection);
return devices; return devices;
} }
string defaultId = ""; string defaultId = "";
try try
{ {
result = _deviceEnumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia, out defaultDevice); result = _deviceEnumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia, out var defaultDevice);
if (result == 0 && defaultDevice != null) if (result == 0 && defaultDevice != null)
{ {
defaultDevice.GetId(out defaultId); defaultDevice.GetId(out defaultId);
ComHelper.ReleaseComObject(defaultDevice);
} }
} }
catch catch
@ -82,11 +81,9 @@ namespace EonaCat.VolumeMixer.Managers
for (uint i = 0; i < count; i++) for (uint i = 0; i < count; i++)
{ {
IMultiMediaDevice device = null;
try try
{ {
result = deviceCollection.Item(i, out device); result = deviceCollection.Item(i, out var device);
if (result == 0 && device != null) if (result == 0 && device != null)
{ {
result = device.GetId(out var id); result = device.GetId(out var id);
@ -97,37 +94,24 @@ namespace EonaCat.VolumeMixer.Managers
bool isDefault = id == defaultId; bool isDefault = id == defaultId;
devices.Add(new AudioDevice(device, id, name, isDefault, dataFlow, type)); devices.Add(new AudioDevice(device, id, name, isDefault, dataFlow, type));
} }
else
{
ComHelper.ReleaseComObject(device);
}
} }
} }
catch catch
{ {
// Do nothing // Do nothing
} }
finally
{
if (device != null)
{
ComHelper.ReleaseComObject(device);
}
}
} }
ComHelper.ReleaseComObject(deviceCollection);
} }
catch catch
{ {
// Do nothing // Do nothing
} }
finally
{
if (deviceCollection != null)
{
ComHelper.ReleaseComObject(deviceCollection);
}
if (defaultDevice != null)
{
ComHelper.ReleaseComObject(defaultDevice);
}
}
} }
return devices; return devices;
@ -145,11 +129,9 @@ namespace EonaCat.VolumeMixer.Managers
lock (_syncLock) lock (_syncLock)
{ {
IMultiMediaDevice device = null;
try try
{ {
var result = _deviceEnumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia, out device); var result = _deviceEnumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia, out var device);
if (result == 0 && device != null) if (result == 0 && device != null)
{ {
result = device.GetId(out var id); result = device.GetId(out var id);
@ -159,81 +141,58 @@ namespace EonaCat.VolumeMixer.Managers
var type = GetDeviceType(device); var type = GetDeviceType(device);
return new AudioDevice(device, id, name, true, dataFlow, type); return new AudioDevice(device, id, name, true, dataFlow, type);
} }
ComHelper.ReleaseComObject(device);
} }
} }
catch catch
{ {
// Do nothing // Do nothing
} }
finally
{
if (device != null)
{
ComHelper.ReleaseComObject(device);
}
}
return null; return null;
} }
}); });
} }
[DllImport("ole32.dll")]
private static extern int PropVariantClear(ref PropVariant pvar);
private string GetDeviceName(IMultiMediaDevice device) private string GetDeviceName(IMultiMediaDevice device)
{ {
IPropertyStore? propertyStore = null;
PropVariant propVariant = new PropVariant();
try try
{ {
var result = device.OpenPropertyStore(0, out propertyStore); var result = device.OpenPropertyStore(0, out var propertyStore);
if (result == 0 && propertyStore != null) if (result == 0 && propertyStore != null)
{ {
var propertyKey = PKEY_Device_FriendlyName; var propertyKey = PKEY_Device_FriendlyName;
result = propertyStore.GetValue(ref propertyKey, out propVariant); result = propertyStore.GetValue(ref propertyKey, out var propVariant);
if (result == 0) if (result == 0 && propVariant.data1 != IntPtr.Zero)
{ {
// Get string
string name = Marshal.PtrToStringUni(propVariant.data1); string name = Marshal.PtrToStringUni(propVariant.data1);
ComHelper.ReleaseComObject(propertyStore);
return !string.IsNullOrEmpty(name) ? name : "Unknown Device"; return !string.IsNullOrEmpty(name) ? name : "Unknown Device";
} }
ComHelper.ReleaseComObject(propertyStore);
} }
} }
catch catch
{ {
// Do nothing // Do nothing
} }
finally
{
// Clear memory
PropVariantClear(ref propVariant);
if (propertyStore != null)
{
ComHelper.ReleaseComObject(propertyStore);
}
}
return "Unknown Device"; return "Unknown Device";
} }
private DeviceType GetDeviceType(IMultiMediaDevice device) private DeviceType GetDeviceType(IMultiMediaDevice device)
{ {
IPropertyStore propertyStore = null;
PropVariant propVariant = new PropVariant();
try try
{ {
int result = device.OpenPropertyStore(0, out propertyStore); int result = device.OpenPropertyStore(0, out var propertyStore);
if (result == 0 && propertyStore != null) if (result == 0 && propertyStore != null)
{ {
try try
{ {
var propertyKey = PKEY_AudioEndpoint_FormFactor; var propertyKey = PKEY_AudioEndpoint_FormFactor;
result = propertyStore.GetValue(ref propertyKey, out propVariant); result = propertyStore.GetValue(ref propertyKey, out var propVariant);
// 0x13 == VT_UI4 // 0x13 == VT_UI4
if (result == 0 && propVariant.vt == 0x13) if (result == 0 && propVariant.vt == 0x13)
@ -268,7 +227,7 @@ namespace EonaCat.VolumeMixer.Managers
} }
finally finally
{ {
ComHelper.ReleaseComObject(propertyStore);
} }
} }
} }
@ -276,14 +235,7 @@ namespace EonaCat.VolumeMixer.Managers
{ {
// Do nothing // Do nothing
} }
finally
{
PropVariantClear(ref propVariant);
if (propertyStore != null)
{
ComHelper.ReleaseComObject(propertyStore);
}
}
return DeviceType.Unknown; return DeviceType.Unknown;
} }