Skip to content

Commit

Permalink
Added CSCore.DeviceTopology.
Browse files Browse the repository at this point in the history
Updated Assembly version.
Added Jack Colour.
  • Loading branch information
William David Cossey committed Mar 20, 2018
1 parent 54c72d6 commit 0b27d01
Show file tree
Hide file tree
Showing 56 changed files with 941 additions and 157 deletions.
37 changes: 37 additions & 0 deletions CSCore.DeviceTopology/COLORREF.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Runtime.InteropServices;

namespace CSCore.DeviceTopology
{
[StructLayout(LayoutKind.Explicit, Size = 3)]
// ReSharper disable once InconsistentNaming
public struct COLORREF
{
public COLORREF(byte r, byte g, byte b)
{
Value = 0;
R = r;
G = g;
B = b;
}

public COLORREF(uint value)
{
R = 0;
G = 0;
B = 0;
Value = value & 0xFFFFFFU;
}

[FieldOffset(0)]
public byte B;

[FieldOffset(1)]
public byte G;

[FieldOffset(2)]
public byte R;

[FieldOffset(0)]
public uint Value;
}
}
69 changes: 69 additions & 0 deletions CSCore.DeviceTopology/CSCore.DeviceTopology.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{38A7752A-D14E-4F42-B7A3-91BFE7E059F1}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CSCore.DeviceTopology</RootNamespace>
<AssemblyName>CSCore.DeviceTopology</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="CSCore, Version=1.2.1.2, Culture=neutral, PublicKeyToken=5a08f2b6f4415dea, processorArchitecture=MSIL">
<HintPath>..\packages\CSCore.1.2.1.2\lib\net35-client\CSCore.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="COLORREF.cs" />
<Compile Include="ExtensionMethods\ConnectorExtensions.cs" />
<Compile Include="ConnectorType.cs" />
<Compile Include="ExtensionMethods\DeviceTopologyExtensions.cs" />
<Compile Include="ExtensionMethods\JackDescriptionExtensions.cs" />
<Compile Include="IConnector.cs" />
<Compile Include="IControlChangeNotify.cs" />
<Compile Include="IControlInterface.cs" />
<Compile Include="IDeviceTopology.cs" />
<Compile Include="IKsJackDescription.cs" />
<Compile Include="IPart.cs" />
<Compile Include="IPartsList.cs" />
<Compile Include="ISubunit.cs" />
<Compile Include="ExtensionMethods\PartExtensions.cs" />
<Compile Include="KSJACK_DESCRIPTION.cs" />
<Compile Include="ExtensionMethods\MMDeviceExtensions.cs" />
<Compile Include="PartType.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
19 changes: 19 additions & 0 deletions CSCore.DeviceTopology/ConnectorType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace CSCore.DeviceTopology
{
public enum ConnectorType
{
// ReSharper disable InconsistentNaming
Unknown_Connector,

Physical_Internal,

Physical_External,

Software_IO,

Software_Fixed,

Network
// ReSharper restore InconsistentNaming
}
}
16 changes: 16 additions & 0 deletions CSCore.DeviceTopology/ExtensionMethods/ConnectorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace CSCore.DeviceTopology.ExtensionMethods
{
public static class ConnectorExtensions
{
public static IConnector GetConnectedTo(this IConnector connector)
{
connector.GetConnectedTo(out var resultConnector);
return resultConnector;
}
public static IPart GetConnectedToAsPart(this IConnector connector)
{
// ReSharper disable once SuspiciousTypeConversion.Global
return connector.GetConnectedTo() as IPart;
}
}
}
11 changes: 11 additions & 0 deletions CSCore.DeviceTopology/ExtensionMethods/DeviceTopologyExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace CSCore.DeviceTopology.ExtensionMethods
{
public static class DeviceTopologyExtensions
{
public static IConnector GetConnector(this IDeviceTopology deviceTopology)
{
deviceTopology.GetConnector(0, out var resultConnector);
return resultConnector;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace CSCore.DeviceTopology.ExtensionMethods
{
public static class JackDescriptionExtensions
{
public static uint GetJackCount(this IKsJackDescription ksJackDescription)
{
ksJackDescription.GetJackCount(out var count);

return count;
}

public static KSJACK_DESCRIPTION GetJackDescription(this IKsJackDescription ksJackDescription, uint index)
{
ksJackDescription.GetJackDescription(index, out var ksjackDescription);
return ksjackDescription;
}
}
}
122 changes: 122 additions & 0 deletions CSCore.DeviceTopology/ExtensionMethods/MMDeviceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using CSCore.CoreAudioAPI;
using CSCore.Win32;

namespace CSCore.DeviceTopology.ExtensionMethods
{
// ReSharper disable once InconsistentNaming
public static class MMDeviceExtensions
{
public static IDeviceTopology DeviceTopology(this IMMDevice device)
{
var attribute = typeof(IDeviceTopology).GetCustomAttributes(typeof(GuidAttribute), true).Cast<GuidAttribute>().FirstOrDefault();

if (attribute == null)
{
return null;
}

device.Activate(new Guid(attribute.Value), CLSCTX.CLSCTX_ALL, IntPtr.Zero, out var pInterface);

return Marshal.GetObjectForIUnknown(pInterface) as IDeviceTopology;
}

public static IDeviceTopology DeviceTopology(this MMDevice device)
{
var attribute = typeof(IDeviceTopology).GetCustomAttributes(typeof(GuidAttribute), true).Cast<GuidAttribute>().FirstOrDefault();

if (attribute == null)
{
return null;
}

var pInterface = device.Activate(new Guid(attribute.Value), CLSCTX.CLSCTX_ALL, IntPtr.Zero);

return Marshal.GetObjectForIUnknown(pInterface) as IDeviceTopology;
}

public static IEnumerable<KSJACK_DESCRIPTION> GetJackDescriptions(this IMMDevice device)
{
try
{
var deviceTopology = device.DeviceTopology();

var connector = deviceTopology.GetConnector();

var part = connector.GetConnectedToAsPart();

var jackDescription = part.KsJackDescription();

if (jackDescription == null)
{
return null;
}

jackDescription.GetJackCount(out var conCount);

if (conCount <= 0)
{
return null;
}

var result = new List<KSJACK_DESCRIPTION>();

for (uint index = 0; index < conCount; index++)
{
result.Add(jackDescription.GetJackDescription(index));
}

return result;
}
catch
{
return null;
}

}


public static IEnumerable<KSJACK_DESCRIPTION> GetJackDescriptions(this MMDevice device)
{
try
{
var deviceTopology = device.DeviceTopology();

var connector = deviceTopology.GetConnector();

var part = connector.GetConnectedToAsPart();

var jackDescription = part.KsJackDescription();

if (jackDescription == null)
{
return null;
}

jackDescription.GetJackCount(out var conCount);

if (conCount <= 0)
{
return null;
}

var result = new List<KSJACK_DESCRIPTION>();

for (uint index = 0; index < conCount; index++)
{
result.Add(jackDescription.GetJackDescription(index));
}

return result;
}
catch
{
return null;
}

}
}
}
26 changes: 26 additions & 0 deletions CSCore.DeviceTopology/ExtensionMethods/PartExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using CSCore.Win32;
using System;
using System.Linq;
using System.Runtime.InteropServices;

namespace CSCore.DeviceTopology.ExtensionMethods
{
public static class PartExtensions
{
public static IKsJackDescription KsJackDescription(this IPart part)
{
var attribute = typeof(IKsJackDescription).GetCustomAttributes(typeof(GuidAttribute), true).Cast<GuidAttribute>().FirstOrDefault();

if (attribute == null)
{
return null;
}

var iidKsJackDescription = new Guid(attribute.Value);

part.Activate((uint)CLSCTX.CLSCTX_INPROC_SERVER, ref iidKsJackDescription, out var instancePtr);

return instancePtr as IKsJackDescription;
}
}
}
46 changes: 46 additions & 0 deletions CSCore.DeviceTopology/IConnector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Runtime.InteropServices;
using CSCore.CoreAudioAPI;

namespace CSCore.DeviceTopology
{
[Guid("9c2c4058-23f5-41de-877a-df3af236a09e")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IConnector
{
[PreserveSig]
int GetType(
[Out] [MarshalAs(UnmanagedType.I4)] out ConnectorType connectorType);

[PreserveSig]
int GetDataFlow(
[Out] [MarshalAs(UnmanagedType.I4)] out DataFlow dataFlow);

[PreserveSig]
int ConnectTo(
[In] [MarshalAs(UnmanagedType.Interface)]
IConnector connector);

[PreserveSig]
int Disconnect();

[PreserveSig]
int IsConnected(
[Out] [MarshalAs(UnmanagedType.Bool)] out bool isConnected);

[PreserveSig]
int GetConnectedTo(
[Out] [MarshalAs(UnmanagedType.Interface)]
out IConnector connector);

[PreserveSig]
int GetConnectorIdConnectedTo(
[Out] [MarshalAs(UnmanagedType.LPWStr)]
out string connectorId);

[PreserveSig]
int GetDeviceIdConnectedTo(
[Out] [MarshalAs(UnmanagedType.LPWStr)]
out string deviceId);

}
}
Loading

0 comments on commit 0b27d01

Please sign in to comment.