windowcapture
исходный код / Native/DirectShowInterop.cs

DirectShowInterop.cs

210 строк · 11,299 байт · модуль Native
  1using System;
  2using System.Runtime.InteropServices;
  3
  4namespace WindowCapture.Native
  5{
  6    // Minimal DirectShow COM interop for video playback (all codecs via installed filters)
  7
  8    [ComImport, Guid("56a868a9-0ad4-11ce-b03a-0020af0ba770"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  9    public interface IGraphBuilder
 10    {
 11        // IFilterGraph
 12        [PreserveSig] int AddFilter([MarshalAs(UnmanagedType.Interface)] IBaseFilter pFilter, [MarshalAs(UnmanagedType.LPWStr)] string pName);
 13        [PreserveSig] int RemoveFilter([MarshalAs(UnmanagedType.Interface)] IBaseFilter pFilter);
 14        [PreserveSig] int EnumFilters(out IntPtr ppEnum);
 15        [PreserveSig] int FindFilterByName([MarshalAs(UnmanagedType.LPWStr)] string pName, [MarshalAs(UnmanagedType.Interface)] out object ppFilter);
 16        [PreserveSig] int ConnectDirect(IntPtr ppinOut, IntPtr ppinIn, IntPtr pmt);
 17        [PreserveSig] int Reconnect(IntPtr ppin);
 18        [PreserveSig] int Disconnect(IntPtr ppin);
 19        [PreserveSig] int SetDefaultSyncSource();
 20        // IGraphBuilder
 21        [PreserveSig] int Connect(IntPtr ppinOut, IntPtr ppinIn);
 22        [PreserveSig] int Render(IntPtr ppinOut);
 23        [PreserveSig] int RenderFile([MarshalAs(UnmanagedType.LPWStr)] string lpcwstrFile, [MarshalAs(UnmanagedType.LPWStr)] string lpcwstrPlayList);
 24        [PreserveSig] int AddSourceFilter([MarshalAs(UnmanagedType.LPWStr)] string lpcwstrFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpcwstrFilterName, [MarshalAs(UnmanagedType.Interface)] out object ppFilter);
 25        [PreserveSig] int SetLogFile(IntPtr hFile);
 26        [PreserveSig] int Abort();
 27        [PreserveSig] int ShouldOperationContinue();
 28    }
 29
 30    [ComImport, Guid("56a868b1-0ad4-11ce-b03a-0020af0ba770"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
 31    public interface IMediaControl
 32    {
 33        [PreserveSig] int Run();
 34        [PreserveSig] int Pause();
 35        [PreserveSig] int Stop();
 36        [PreserveSig] int GetState(int msTimeout, out int pfs);
 37        [PreserveSig] int RenderFile([MarshalAs(UnmanagedType.BStr)] string strFilename);
 38        [PreserveSig] int AddSourceFilter([MarshalAs(UnmanagedType.BStr)] string strFilename, [MarshalAs(UnmanagedType.Interface)] out object ppUnk);
 39        [PreserveSig] int get_FilterCollection([MarshalAs(UnmanagedType.Interface)] out object ppUnk);
 40        [PreserveSig] int get_RegFilterCollection([MarshalAs(UnmanagedType.Interface)] out object ppUnk);
 41        [PreserveSig] int StopWhenReady();
 42    }
 43
 44    [ComImport, Guid("56a868b2-0ad4-11ce-b03a-0020af0ba770"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
 45    public interface IMediaPosition
 46    {
 47        [PreserveSig] int get_Duration(out double pLength);
 48        [PreserveSig] int put_CurrentPosition(double llTime);
 49        [PreserveSig] int get_CurrentPosition(out double pllTime);
 50        [PreserveSig] int get_StopTime(out double pllTime);
 51        [PreserveSig] int put_StopTime(double llTime);
 52        [PreserveSig] int get_PrerollTime(out double pllTime);
 53        [PreserveSig] int put_PrerollTime(double llTime);
 54        [PreserveSig] int put_Rate(double dRate);
 55        [PreserveSig] int get_Rate(out double pdRate);
 56        [PreserveSig] int CanSeekForward(out int pCanSeekForward);
 57        [PreserveSig] int CanSeekBackward(out int pCanSeekBackward);
 58    }
 59
 60    [ComImport, Guid("56a868b4-0ad4-11ce-b03a-0020af0ba770"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
 61    public interface IVideoWindow
 62    {
 63        [PreserveSig] int put_Caption([MarshalAs(UnmanagedType.BStr)] string strCaption);
 64        [PreserveSig] int get_Caption([MarshalAs(UnmanagedType.BStr)] out string strCaption);
 65        [PreserveSig] int put_WindowStyle(int WindowStyle);
 66        [PreserveSig] int get_WindowStyle(out int WindowStyle);
 67        [PreserveSig] int put_WindowStyleEx(int WindowStyleEx);
 68        [PreserveSig] int get_WindowStyleEx(out int WindowStyleEx);
 69        [PreserveSig] int put_AutoShow(int AutoShow);
 70        [PreserveSig] int get_AutoShow(out int AutoShow);
 71        [PreserveSig] int put_WindowState(int WindowState);
 72        [PreserveSig] int get_WindowState(out int WindowState);
 73        [PreserveSig] int put_BackgroundPalette(int BackgroundPalette);
 74        [PreserveSig] int get_BackgroundPalette(out int pBackgroundPalette);
 75        [PreserveSig] int put_Visible(int Visible);
 76        [PreserveSig] int get_Visible(out int pVisible);
 77        [PreserveSig] int put_Left(int Left);
 78        [PreserveSig] int get_Left(out int pLeft);
 79        [PreserveSig] int put_Width(int Width);
 80        [PreserveSig] int get_Width(out int pWidth);
 81        [PreserveSig] int put_Top(int Top);
 82        [PreserveSig] int get_Top(out int pTop);
 83        [PreserveSig] int put_Height(int Height);
 84        [PreserveSig] int get_Height(out int pHeight);
 85        [PreserveSig] int put_Owner(IntPtr Owner);
 86        [PreserveSig] int get_Owner(out IntPtr Owner);
 87        [PreserveSig] int put_MessageDrain(IntPtr Drain);
 88        [PreserveSig] int get_MessageDrain(out IntPtr Drain);
 89        [PreserveSig] int get_BorderColor(out int Color);
 90        [PreserveSig] int put_BorderColor(int Color);
 91        [PreserveSig] int get_FullScreenMode(out int FullScreenMode);
 92        [PreserveSig] int put_FullScreenMode(int FullScreenMode);
 93        [PreserveSig] int SetWindowForeground(int Focus);
 94        [PreserveSig] int NotifyOwnerMessage(IntPtr hwnd, int uMsg, IntPtr wParam, IntPtr lParam);
 95        [PreserveSig] int SetWindowPosition(int Left, int Top, int Width, int Height);
 96        [PreserveSig] int GetWindowPosition(out int pLeft, out int pTop, out int pWidth, out int pHeight);
 97        [PreserveSig] int GetMinIdealImageSize(out int pWidth, out int pHeight);
 98        [PreserveSig] int GetMaxIdealImageSize(out int pWidth, out int pHeight);
 99        [PreserveSig] int GetRestorePosition(out int pLeft, out int pTop, out int pWidth, out int pHeight);
100        [PreserveSig] int HideCursor(int HideCursor);
101        [PreserveSig] int IsCursorHidden(out int CursorHidden);
102    }
103
104    // ISampleGrabber for audio waveform extraction
105    [ComImport, Guid("6B652FFF-11FE-4fce-92AD-0266B5D7C78F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
106    public interface ISampleGrabber
107    {
108        [PreserveSig] int SetOneShot([MarshalAs(UnmanagedType.Bool)] bool oneShot);
109        [PreserveSig] int SetMediaType([In, MarshalAs(UnmanagedType.LPStruct)] AMMediaType pmt);
110        [PreserveSig] int GetConnectedMediaType([Out, MarshalAs(UnmanagedType.LPStruct)] AMMediaType pmt);
111        [PreserveSig] int SetBufferSamples([MarshalAs(UnmanagedType.Bool)] bool bufferThem);
112        [PreserveSig] int GetCurrentBuffer(ref int pBufferSize, IntPtr pBuffer);
113        [PreserveSig] int GetCurrentSample(out IntPtr ppSample);
114        [PreserveSig] int SetCallback(ISampleGrabberCB pCallback, int whichMethodToCallback);
115    }
116
117    [ComImport, Guid("0579154A-2B53-4994-B0D0-E773148EFF85"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
118    public interface ISampleGrabberCB
119    {
120        [PreserveSig] int SampleCB(double sampleTime, IntPtr pSample);
121        [PreserveSig] int BufferCB(double sampleTime, IntPtr pBuffer, int bufferLen);
122    }
123
124    [StructLayout(LayoutKind.Sequential)]
125    public class AMMediaType
126    {
127        public Guid majorType;
128        public Guid subType;
129        [MarshalAs(UnmanagedType.Bool)] public bool fixedSizeSamples;
130        [MarshalAs(UnmanagedType.Bool)] public bool temporalCompression;
131        public int sampleSize;
132        public Guid formatType;
133        public IntPtr unkPtr;
134        public int formatSize;
135        public IntPtr formatPtr;
136    }
137
138    // IBaseFilter for adding filters to graph
139    [ComImport, Guid("56a86895-0ad4-11ce-b03a-0020af0ba770"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
140    public interface IBaseFilter
141    {
142        // Inherited from IPersist
143        [PreserveSig] int GetClassID(out Guid pClassID);
144        // Inherited from IMediaFilter
145        [PreserveSig] int Stop();
146        [PreserveSig] int Pause();
147        [PreserveSig] int Run(long tStart);
148        [PreserveSig] int GetState(int dwMilliSecsTimeout, out int filtState);
149        [PreserveSig] int SetSyncSource(IntPtr pClock);
150        [PreserveSig] int GetSyncSource(out IntPtr pClock);
151        // IBaseFilter
152        [PreserveSig] int EnumPins(out IntPtr ppEnum);
153        [PreserveSig] int FindPin([MarshalAs(UnmanagedType.LPWStr)] string Id, out IntPtr ppPin);
154        [PreserveSig] int QueryFilterInfo(IntPtr pInfo);
155        [PreserveSig] int JoinFilterGraph(IntPtr pGraph, [MarshalAs(UnmanagedType.LPWStr)] string pName);
156        [PreserveSig] int QueryVendorInfo([MarshalAs(UnmanagedType.LPWStr)] out string pVendorInfo);
157    }
158
159    [ComImport, Guid("56a868b3-0ad4-11ce-b03a-0020af0ba770"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
160    public interface IBasicAudio
161    {
162        [PreserveSig] int put_Volume(int lVolume);
163        [PreserveSig] int get_Volume(out int plVolume);
164        [PreserveSig] int put_Balance(int lBalance);
165        [PreserveSig] int get_Balance(out int plBalance);
166    }
167
168    // IMFVideoDisplayControl for EVR renderer (GPU-accelerated)
169    [ComImport, Guid("a490b1e4-ab84-4d31-a1b2-181e03b1077a"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
170    public interface IMFVideoDisplayControl
171    {
172        [PreserveSig] int GetNativeVideoSize(out MFSize pszVideo, out MFSize pszARVideo);
173        [PreserveSig] int GetIdealVideoSize(out MFSize pszMin, out MFSize pszMax);
174        [PreserveSig] int SetVideoPosition(IntPtr pnrcSource, [In] ref MFRect pnrcDest);
175        [PreserveSig] int GetVideoPosition(out IntPtr pnrcSource, out MFRect pnrcDest);
176        [PreserveSig] int SetAspectRatioMode(int dwAspectRatioMode);
177        [PreserveSig] int GetAspectRatioMode(out int pdwAspectRatioMode);
178        [PreserveSig] int SetVideoWindow(IntPtr hwndVideo);
179        [PreserveSig] int GetVideoWindow(out IntPtr phwndVideo);
180        [PreserveSig] int RepaintVideo();
181        [PreserveSig] int GetCurrentImage(IntPtr pBih, out IntPtr pDib, out int pcbDib, out long pTimeStamp);
182        [PreserveSig] int SetBorderColor(int Clr);
183        [PreserveSig] int GetBorderColor(out int pClr);
184        [PreserveSig] int SetRenderingPrefs(int dwRenderFlags);
185        [PreserveSig] int GetRenderingPrefs(out int pdwRenderFlags);
186        [PreserveSig] int SetFullscreen([MarshalAs(UnmanagedType.Bool)] bool fFullscreen);
187        [PreserveSig] int GetFullscreen([MarshalAs(UnmanagedType.Bool)] out bool pfFullscreen);
188    }
189
190    // IMFGetService to get EVR sub-services
191    [ComImport, Guid("fa993888-4383-415a-a930-dd472a8cf6f7"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
192    public interface IMFGetService
193    {
194        [PreserveSig] int GetService([In, MarshalAs(UnmanagedType.LPStruct)] Guid guidService,
195            [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppvObject);
196    }
197
198    [StructLayout(LayoutKind.Sequential)]
199    public struct MFRect
200    {
201        public int left, top, right, bottom;
202        public MFRect(int l, int t, int r, int b) { left = l; top = t; right = r; bottom = b; }
203    }
204
205    [StructLayout(LayoutKind.Sequential)]
206    public struct MFSize
207    {
208        public int cx, cy;
209    }
210}