現在C#および.NET FrameworkでUVCカメラのExtension Unitにデータを送信したいと考えております。
C++では下記リンクを参考にDirectShowのIKsTopologyInfo
とIKsControl
インターフェイスを使用し、データの送受信を行うことができました。
video streaming - Enumerating UVC Properties items of a property set of a camera - Stack Overflow
C#でDirectShowLibとDirectNを使用し同様のことをしたいと考えていますが、インターフェイスへのアクセスやCOM APIの取得ができません。
改善方法をご存じの方がいらっしゃいましたら、ご教授お願い致します。
開発環境:
Windows 10 (22H2)
Visual Studio 17.8.4
.NET Framework 4.7.2
DirectShowLib 1.0.0
DirectN 1.15.0.2
C++
int SetValue(IBaseFilter * pMediaSource, GUID cameraGuid, int nodeId, int propId, int transData)
{
HRESULT hRet;
IKsTopologyInfo* ksTopology;
IUnknown* unk;
IKsControl* ksCnt;
hRet = pMediaSource->QueryInterface(__uuidof(IKsTopologyInfo), (void**)&ksTopology);
if (FAILED(hRet))
return FALSE;
hRet = ksTopology->CreateNodeInstance(nodeId, __uuidof(IUnknown), (VOID**)&unk);
if (FAILED(hRet))
return FALSE;
hRet = unk->QueryInterface(__uuidof(IKsControl), (VOID**)&ksCnt);
if (FAILED(hRet))
return FALSE;
KSP_NODE prop;
unsigned char propData[16];
ULONG bytesReturned;
prop.Property.Set = cameraGuid;
prop.Property.Id = propId;
prop.Property.Flags = KSPROPERTY_TYPE_SET | KSPROPERTY_TYPE_TOPOLOGY;
prop.NodeId = nodeId;
propData[0] = (unsigned char)transData & 0xff;
propData[1] = 0;
hRet = ksCnt->KsProperty((PKSPROPERTY)&prop, sizeof(prop), &propData[0], 1, &bytesReturned);
ksCnt->Release();
unk->Release();
ksTopology->Release();
if (FAILED(hRet))
return FALSE;
return TRUE;
}
C#
public const int KSPROPERTY_TYPE_GET = 0x00000001;
public const int KSPROPERTY_TYPE_SET = 0x00000002;
public const int KSPROPERTY_TYPE_TOPOLOGY = 0x10000000;
public Boolean SetValue(DirectShowLib.IBaseFilter pMediaSource, Guid cameraGuid, int nodeId, int propId, int transData)
{
HRESULT hRet;
DirectN.IKsTopologyInfo ksTopology;
Guid iid;
IntPtr pUnk1;
IntPtr ppv1;
object unk;
pUnk1 = Marshal.GetComInterfaceForObject(pMediaSource, typeof(DirectShowLib.IBaseFilter));
iid = new Guid("720D4AC0-7533-11D0-A5D6-28DB04C10000");
ksTopology = pMediaSource as DirectN.IKsTopologyInfo;
ppv1 = Marshal.GetComInterfaceForObject(ksTopology, typeof(DirectN.IKsTopologyInfo));
hRet = Marshal.QueryInterface(pUnk1, ref iid, out ppv1);
if (hRet != 0)
return false;
iid = new Guid("00000000-0000-0000-C000-000000000046");
hRet = ksTopology.CreateNodeInstance((uint)nodeId, iid, out unk);
if (hRet != 0)
return false;
IKsControl ksCnt;
IntPtr pUnk2;
IntPtr ppv2;
pUnk2 = GCHandle.ToIntPtr(GCHandle.Alloc(unk));
iid = new Guid("28F54685-06FD-11D2-B27A-00A0C9223196");
ksCnt = unk as IKsControl;
ppv2 = Marshal.GetComInterfaceForObject(ksCnt, typeof(IKsControl));
hRet = Marshal.QueryInterface(pUnk2, ref iid, out ppv2);
if (hRet != 0)
return false;
__struct_ks_2__union_0__struct_0 st;
KSIDENTIFIER property;
KSP_NODE prop;
byte[] data = new byte[16];
IntPtr propData = IntPtr.Zero;
uint bytesReturned = 0;
st.Set = cameraGuid;
st.Id = (uint)propId;
st.Flags = KSPROPERTY_TYPE_SET | KSPROPERTY_TYPE_TOPOLOGY;
property = new KSIDENTIFIER();
property.__union_0.__field_0 = st;
prop = new KSP_NODE();
prop.Property = property;
prop.NodeId = (uint)nodeId;
data[0] = (byte)(transData & 0xff);
data[1] = 0;
propData = Marshal.AllocHGlobal(data.Length);
Marshal.Copy(data, 0, propData, data.Length);
hRet = ksCnt.KsProperty(ref prop.Property, (uint)Marshal.SizeOf(prop), out propData, 1, out bytesReturned);
Marshal.FreeHGlobal(propData);
Marshal.FreeHGlobal(pUnk2);
Marshal.FreeHGlobal(ppv2);
Marshal.FreeHGlobal(pUnk1);
Marshal.FreeHGlobal(ppv1);
if (hRet != 0)
return false;
return true;
}