Browse Source

Merge branch 'JustArion-PropertyStore-Perf'

master
Mark Heath 6 months ago
parent
commit
5bd2860c20
  1. 5
      .gitignore
  2. 71
      NAudio.Wasapi/CoreAudioApi/MMDevice.cs
  3. 42
      NAudio.Wasapi/CoreAudioApi/PropertyStore.cs
  4. 2
      NAudio.Wasapi/CoreAudioApi/PropertyStoreProperty.cs

5
.gitignore

@ -12,7 +12,10 @@ _ReSharper*/
*.ncrunchproject
*.orig
*.lock.json
# Jetbrains
*.idea
.vs/
.fake/
# cake:
Tools/
Tools/
*.DotSettings

71
NAudio.Wasapi/CoreAudioApi/MMDevice.cs

@ -170,12 +170,17 @@ namespace NAudio.CoreAudioApi
{
get
{
if (propertyStore == null)
GetPropertyInformation();
EnsurePropertyStoreExists();
return propertyStore;
}
}
private void EnsurePropertyStoreExists()
{
if (propertyStore == null)
GetPropertyInformation();
}
/// <summary>
/// Friendly name for the endpoint
/// </summary>
@ -183,16 +188,11 @@ namespace NAudio.CoreAudioApi
{
get
{
if (propertyStore == null)
{
GetPropertyInformation();
}
if (propertyStore.Contains(PropertyKeys.PKEY_Device_FriendlyName))
{
return (string)propertyStore[PropertyKeys.PKEY_Device_FriendlyName].Value;
}
else
return "Unknown";
EnsurePropertyStoreExists();
return propertyStore.TryGetValue<string>(PropertyKeys.PKEY_Device_FriendlyName, out var value)
? value
: "Unknown";
}
}
@ -203,18 +203,11 @@ namespace NAudio.CoreAudioApi
{
get
{
if (propertyStore == null)
{
GetPropertyInformation();
}
if (propertyStore.Contains(PropertyKeys.PKEY_DeviceInterface_FriendlyName))
{
return (string)propertyStore[PropertyKeys.PKEY_DeviceInterface_FriendlyName].Value;
}
else
{
return "Unknown";
}
EnsurePropertyStoreExists();
return propertyStore.TryGetValue<string>(PropertyKeys.PKEY_DeviceInterface_FriendlyName, out var value)
? value
: "Unknown";
}
}
@ -225,16 +218,11 @@ namespace NAudio.CoreAudioApi
{
get
{
if (propertyStore == null)
{
GetPropertyInformation();
}
if (propertyStore.Contains(PropertyKeys.PKEY_Device_IconPath))
{
return (string)propertyStore[PropertyKeys.PKEY_Device_IconPath].Value;
}
return "Unknown";
EnsurePropertyStoreExists();
return propertyStore.TryGetValue<string>(PropertyKeys.PKEY_Device_IconPath, out var value)
? value
: "Unknown";
}
}
@ -245,16 +233,11 @@ namespace NAudio.CoreAudioApi
{
get
{
if (propertyStore == null)
{
GetPropertyInformation();
}
if (propertyStore.Contains(PropertyKeys.PKEY_Device_InstanceId))
{
return (string)propertyStore[PropertyKeys.PKEY_Device_InstanceId].Value;
}
return "Unknown";
EnsurePropertyStoreExists();
return propertyStore.TryGetValue<string>(PropertyKeys.PKEY_Device_InstanceId, out var value)
? value
: "Unknown";
}
}

42
NAudio.Wasapi/CoreAudioApi/PropertyStore.cs

@ -67,15 +67,27 @@ namespace NAudio.CoreAudioApi
/// <returns>True if found</returns>
public bool Contains(PropertyKey key)
{
for (int i = 0; i < Count; i++)
{
PropertyKey ikey = Get(i);
if ((ikey.formatId == key.formatId) && (ikey.propertyId == key.propertyId))
{
return true;
}
}
return false;
var result = storeInterface.GetValue(ref key, out var propVariant);
return result >= 0 && (VarEnum)propVariant.vt != VarEnum.VT_EMPTY;
}
/// <summary>
/// Checks if the property exists
/// </summary>
/// <param name="key">Looks for a specific key</param>
/// <param name="obj">The value of the property wrapped in a type cast</param>
/// <typeparam name="T">The type to cast the property as</typeparam>
/// <returns>True if found</returns>
public bool TryGetValue<T>(PropertyKey key, out T obj)
{
obj = default;
var result = storeInterface.GetValue(ref key, out var propVariant);
if (result < 0 || (VarEnum)propVariant.vt == VarEnum.VT_EMPTY)
return false;
obj = (T)propVariant.Value;
return true;
}
/// <summary>
@ -87,16 +99,8 @@ namespace NAudio.CoreAudioApi
{
get
{
for (int i = 0; i < Count; i++)
{
PropertyKey ikey = Get(i);
if ((ikey.formatId == key.formatId) && (ikey.propertyId == key.propertyId))
{
Marshal.ThrowExceptionForHR(storeInterface.GetValue(ref ikey, out var result));
return new PropertyStoreProperty(ikey, result);
}
}
return null;
Marshal.ThrowExceptionForHR(storeInterface.GetValue(ref key, out var propVariant));
return new PropertyStoreProperty(key, propVariant);
}
}

2
NAudio.Wasapi/CoreAudioApi/PropertyStoreProperty.cs

@ -28,7 +28,7 @@ namespace NAudio.CoreAudioApi
/// <summary>
/// Property Store Property
/// </summary>
public class PropertyStoreProperty
public struct PropertyStoreProperty
{
private PropVariant propertyValue;

Loading…
Cancel
Save