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

MediaFoundationInterop.cs

276 строк · 17,207 байт · модуль Native
  1using System;
  2using System.Runtime.InteropServices;
  3
  4namespace WindowCapture.Native
  5{
  6    [ComImport, Guid("2CD2D921-C447-44A7-A13C-4ADABFC247E3"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  7    public interface IMFAttributes
  8    {
  9        void GetItem(ref Guid guidKey, IntPtr pValue);
 10        void GetItemType(ref Guid guidKey, out int pType);
 11        void CompareItem(ref Guid guidKey, IntPtr Value, out bool pbResult);
 12        void Compare([MarshalAs(UnmanagedType.Interface)] IMFAttributes pTheirs, int MatchType, out bool pbResult);
 13        void GetUINT32(ref Guid guidKey, out uint punValue);
 14        void GetUINT64(ref Guid guidKey, out ulong punValue);
 15        void GetDouble(ref Guid guidKey, out double pfValue);
 16        void GetGUID(ref Guid guidKey, out Guid pguidValue);
 17        void GetStringLength(ref Guid guidKey, out uint pcchLength);
 18        void GetString(ref Guid guidKey, [Out, MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pwszValue, uint cchBufSize, out uint pcchLength);
 19        void GetAllocatedString(ref Guid guidKey, [MarshalAs(UnmanagedType.LPWStr)] out string ppwszValue, out uint pcchLength);
 20        void GetBlobSize(ref Guid guidKey, out uint pcbBlobSize);
 21        void GetBlob(ref Guid guidKey, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pBuf, uint cbBufSize, out uint pcbBlobSize);
 22        void GetAllocatedBlob(ref Guid guidKey, out IntPtr ppBuf, out uint pcbSize);
 23        void GetUnknown(ref Guid guidKey, ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppv);
 24        void SetItem(ref Guid guidKey, IntPtr Value);
 25        void DeleteItem(ref Guid guidKey);
 26        void DeleteAllItems();
 27        void SetUINT32(ref Guid guidKey, uint unValue);
 28        void SetUINT64(ref Guid guidKey, ulong unValue);
 29        void SetDouble(ref Guid guidKey, double fValue);
 30        void SetGUID(ref Guid guidKey, ref Guid guidValue);
 31        void SetString(ref Guid guidKey, [MarshalAs(UnmanagedType.LPWStr)] string wszValue);
 32        void SetBlob(ref Guid guidKey, [In, MarshalAs(UnmanagedType.LPArray)] byte[] pBuf, uint cbBufSize);
 33        void SetUnknown(ref Guid guidKey, [MarshalAs(UnmanagedType.IUnknown)] object pUnknown);
 34        void LockStore();
 35        void UnlockStore();
 36        void GetCount(out uint pcItems);
 37        void GetItemByIndex(uint unIndex, out Guid pguidKey, IntPtr pValue);
 38        void CopyAllItems([MarshalAs(UnmanagedType.Interface)] IMFAttributes pDest);
 39    }
 40
 41    [ComImport, Guid("44AE0FA8-EA31-4109-8D2E-4CAE4997C555"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
 42    public interface IMFMediaType : IMFAttributes
 43    {
 44        void GetMajorType(out Guid pguidMajorType);
 45        void IsCompressedFormat(out bool pfCompressed);
 46        void IsEqual([MarshalAs(UnmanagedType.Interface)] IMFMediaType pIMediaType, out uint pdwFlags);
 47        void GetRepresentation(Guid guidRepresentation, out IntPtr ppvRepresentation);
 48        void FreeRepresentation(Guid guidRepresentation, IntPtr pvRepresentation);
 49    }
 50
 51    [ComImport, Guid("3137f1cd-fe5e-4805-a5d8-fb477448cb3d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
 52    public interface IMFSinkWriter
 53    {
 54        void AddStream([MarshalAs(UnmanagedType.Interface)] IMFMediaType pTargetMediaType, out uint pdwStreamIndex);
 55        void SetInputMediaType(uint dwStreamIndex, [MarshalAs(UnmanagedType.Interface)] IMFMediaType pInputMediaType, [MarshalAs(UnmanagedType.Interface)] IMFAttributes pEncodingParameters);
 56        void BeginWriting();
 57        void WriteSample(uint dwStreamIndex, [MarshalAs(UnmanagedType.Interface)] IMFSample pSample);
 58        void SendStreamTick(uint dwStreamIndex, long hnsTimestamp);
 59        void PlaceMarker(uint dwStreamIndex, IntPtr pvContext);
 60        void NotifyEndOfSegment(uint dwStreamIndex);
 61        void Flush(uint dwStreamIndex);
 62        void Finalize_();
 63        void GetServiceForStream(uint dwStreamIndex, ref Guid guidService, ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppvObject);
 64        void GetStatistics(uint dwStreamIndex, IntPtr pStats);
 65    }
 66
 67    [ComImport, Guid("C40A00F2-B93A-4D80-AE8C-5A1C634F58E4"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
 68    public interface IMFSample : IMFAttributes
 69    {
 70        // Must re-declare all 30 IMFAttributes methods with 'new' for correct COM vtable layout
 71        new void GetItem(ref Guid guidKey, IntPtr pValue);
 72        new void GetItemType(ref Guid guidKey, out int pType);
 73        new void CompareItem(ref Guid guidKey, IntPtr Value, out bool pbResult);
 74        new void Compare([MarshalAs(UnmanagedType.Interface)] IMFAttributes pTheirs, int MatchType, out bool pbResult);
 75        new void GetUINT32(ref Guid guidKey, out uint punValue);
 76        new void GetUINT64(ref Guid guidKey, out ulong punValue);
 77        new void GetDouble(ref Guid guidKey, out double pfValue);
 78        new void GetGUID(ref Guid guidKey, out Guid pguidValue);
 79        new void GetStringLength(ref Guid guidKey, out uint pcchLength);
 80        new void GetString(ref Guid guidKey, [Out, MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pwszValue, uint cchBufSize, out uint pcchLength);
 81        new void GetAllocatedString(ref Guid guidKey, [MarshalAs(UnmanagedType.LPWStr)] out string ppwszValue, out uint pcchLength);
 82        new void GetBlobSize(ref Guid guidKey, out uint pcbBlobSize);
 83        new void GetBlob(ref Guid guidKey, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pBuf, uint cbBufSize, out uint pcbBlobSize);
 84        new void GetAllocatedBlob(ref Guid guidKey, out IntPtr ppBuf, out uint pcbSize);
 85        new void GetUnknown(ref Guid guidKey, ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppv);
 86        new void SetItem(ref Guid guidKey, IntPtr Value);
 87        new void DeleteItem(ref Guid guidKey);
 88        new void DeleteAllItems();
 89        new void SetUINT32(ref Guid guidKey, uint unValue);
 90        new void SetUINT64(ref Guid guidKey, ulong unValue);
 91        new void SetDouble(ref Guid guidKey, double fValue);
 92        new void SetGUID(ref Guid guidKey, ref Guid guidValue);
 93        new void SetString(ref Guid guidKey, [MarshalAs(UnmanagedType.LPWStr)] string wszValue);
 94        new void SetBlob(ref Guid guidKey, [In, MarshalAs(UnmanagedType.LPArray)] byte[] pBuf, uint cbBufSize);
 95        new void SetUnknown(ref Guid guidKey, [MarshalAs(UnmanagedType.IUnknown)] object pUnknown);
 96        new void LockStore();
 97        new void UnlockStore();
 98        new void GetCount(out uint pcItems);
 99        new void GetItemByIndex(uint unIndex, out Guid pguidKey, IntPtr pValue);
100        new void CopyAllItems([MarshalAs(UnmanagedType.Interface)] IMFAttributes pDest);
101        // IMFSample own methods
102        void GetSampleFlags(out uint pdwSampleFlags);
103        void SetSampleFlags(uint dwSampleFlags);
104        void GetSampleTime(out long phnsSampleTime);
105        void SetSampleTime(long hnsSampleTime);
106        void GetSampleDuration(out long phnsSampleDuration);
107        void SetSampleDuration(long hnsSampleDuration);
108        void GetBufferCount(out uint pdwBufferCount);
109        void GetBufferByIndex(uint dwIndex, [MarshalAs(UnmanagedType.Interface)] out IMFMediaBuffer ppBuffer);
110        void ConvertToContiguousBuffer([MarshalAs(UnmanagedType.Interface)] out IMFMediaBuffer ppBuffer);
111        void AddBuffer([MarshalAs(UnmanagedType.Interface)] IMFMediaBuffer pBuffer);
112        void RemoveBufferByIndex(uint dwIndex);
113        void RemoveAllBuffers();
114        void GetTotalLength(out uint pcbTotalLength);
115        void CopyToBuffer([MarshalAs(UnmanagedType.Interface)] IMFMediaBuffer pBuffer);
116    }
117
118    [ComImport, Guid("045FA593-8799-42B8-BC8D-8968C6453507"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
119    public interface IMFMediaBuffer
120    {
121        void Lock(out IntPtr ppbBuffer, out int pcbMaxLength, out int pcbCurrentLength);
122        void Unlock();
123        void GetCurrentLength(out int pcbCurrentLength);
124        void SetCurrentLength(int cbCurrentLength);
125        void GetMaxLength(out int pcbMaxLength);
126    }
127
128    // IMFTransform for direct MFT access (H.264 encoder)
129    [ComImport, Guid("bf94c121-5b05-4e6f-8000-ba598961414d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
130    public interface IMFTransform
131    {
132        [PreserveSig] int GetStreamLimits(out uint pdwInputMinimum, out uint pdwInputMaximum, out uint pdwOutputMinimum, out uint pdwOutputMaximum);
133        [PreserveSig] int GetStreamCount(out uint pcInputStreams, out uint pcOutputStreams);
134        [PreserveSig] int GetStreamIDs(uint dwInputIDArraySize, [Out, MarshalAs(UnmanagedType.LPArray)] uint[] pdwInputIDs, uint dwOutputIDArraySize, [Out, MarshalAs(UnmanagedType.LPArray)] uint[] pdwOutputIDs);
135        [PreserveSig] int GetInputStreamInfo(uint dwInputStreamID, IntPtr pStreamInfo);
136        [PreserveSig] int GetOutputStreamInfo(uint dwOutputStreamID, out MFT_OUTPUT_STREAM_INFO pStreamInfo);
137        [PreserveSig] int GetAttributes([MarshalAs(UnmanagedType.Interface)] out IMFAttributes pAttributes);
138        [PreserveSig] int GetInputStreamAttributes(uint dwInputStreamID, [MarshalAs(UnmanagedType.Interface)] out IMFAttributes pAttributes);
139        [PreserveSig] int GetOutputStreamAttributes(uint dwOutputStreamID, [MarshalAs(UnmanagedType.Interface)] out IMFAttributes pAttributes);
140        [PreserveSig] int DeleteInputStream(uint dwStreamID);
141        [PreserveSig] int AddInputStreams(uint cStreams, [In, MarshalAs(UnmanagedType.LPArray)] uint[] adwStreamIDs);
142        [PreserveSig] int GetInputAvailableType(uint dwInputStreamID, uint dwTypeIndex, [MarshalAs(UnmanagedType.Interface)] out IMFMediaType ppType);
143        [PreserveSig] int GetOutputAvailableType(uint dwOutputStreamID, uint dwTypeIndex, [MarshalAs(UnmanagedType.Interface)] out IMFMediaType ppType);
144        [PreserveSig] int SetInputType(uint dwInputStreamID, [MarshalAs(UnmanagedType.Interface)] IMFMediaType pType, uint dwFlags);
145        [PreserveSig] int SetOutputType(uint dwOutputStreamID, [MarshalAs(UnmanagedType.Interface)] IMFMediaType pType, uint dwFlags);
146        [PreserveSig] int GetInputCurrentType(uint dwInputStreamID, [MarshalAs(UnmanagedType.Interface)] out IMFMediaType ppType);
147        [PreserveSig] int GetOutputCurrentType(uint dwOutputStreamID, [MarshalAs(UnmanagedType.Interface)] out IMFMediaType ppType);
148        [PreserveSig] int GetInputStatus(uint dwInputStreamID, out uint pdwFlags);
149        [PreserveSig] int GetOutputStatus(out uint pdwFlags);
150        [PreserveSig] int SetOutputBounds(long hnsLowerBound, long hnsUpperBound);
151        [PreserveSig] int ProcessEvent(uint dwInputStreamID, IntPtr pEvent);
152        [PreserveSig] int ProcessMessage(uint eMessage, IntPtr ulParam);
153        [PreserveSig] int ProcessInput(uint dwInputStreamID, [MarshalAs(UnmanagedType.Interface)] IMFSample pSample, uint dwFlags);
154        [PreserveSig] int ProcessOutput(uint dwFlags, uint cOutputBufferCount, [In, Out, MarshalAs(UnmanagedType.LPArray)] MFT_OUTPUT_DATA_BUFFER[] pOutputSamples, out uint pdwStatus);
155    }
156
157    [StructLayout(LayoutKind.Sequential)]
158    public struct MFT_OUTPUT_STREAM_INFO
159    {
160        public uint dwFlags;
161        public uint cbSize;
162        public uint cbAlignment;
163    }
164
165    [StructLayout(LayoutKind.Sequential)]
166    public struct MFT_OUTPUT_DATA_BUFFER
167    {
168        public uint dwStreamID;
169        public IntPtr pSample;   // IMFSample*
170        public uint dwStatus;
171        public IntPtr pEvents;   // IMFCollection*
172    }
173
174    // ICodecAPI — for configuring encoder properties before SetOutputType
175    [ComImport, Guid("901db4c7-31ce-41a2-85dc-8fa0bf41b8da"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
176    public interface ICodecAPI
177    {
178        [PreserveSig] int IsSupported(ref Guid Api);
179        [PreserveSig] int IsModifiable(ref Guid Api);
180        [PreserveSig] int GetParameterRange(ref Guid Api, out IntPtr ValueMin, out IntPtr ValueMax, out IntPtr SteppingDelta);
181        [PreserveSig] int GetParameterValues(ref Guid Api, out IntPtr Values, out uint ValuesCount);
182        [PreserveSig] int GetDefaultValue(ref Guid Api, IntPtr Value);
183        [PreserveSig] int GetValue(ref Guid Api, IntPtr Value);
184        [PreserveSig] int SetValue(ref Guid Api, IntPtr Value);
185    }
186
187    // IMFActivate : IMFAttributes — needed for MFTEnumEx
188    [ComImport, Guid("7FEE9E9A-4A89-47a6-899C-B6A53A70FB67"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
189    public interface IMFActivate : IMFAttributes
190    {
191        [PreserveSig] int ActivateObject(ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppv);
192        [PreserveSig] int ShutdownObject();
193        [PreserveSig] int DetachObject();
194    }
195
196    [ComImport, Guid("70ae66f2-c809-4e4f-8915-bdcb406b7993"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
197    public interface IMFSourceReader
198    {
199        [PreserveSig] int GetStreamSelection(uint dwStreamIndex, out bool pfSelected);
200        [PreserveSig] int SetStreamSelection(uint dwStreamIndex, bool fSelected);
201        [PreserveSig] int GetNativeMediaType(uint dwStreamIndex, uint dwMediaTypeIndex, out IMFMediaType ppMediaType);
202        [PreserveSig] int GetCurrentMediaType(uint dwStreamIndex, out IMFMediaType ppMediaType);
203        [PreserveSig] int SetCurrentMediaType(uint dwStreamIndex, IntPtr pdwReserved, IMFMediaType pMediaType);
204        [PreserveSig] int SetCurrentPosition([In] ref Guid guidTimeFormat, IntPtr varPosition);
205        [PreserveSig] int ReadSample(uint dwStreamIndex, uint dwControlFlags, out uint pdwActualStreamIndex, out int pdwStreamFlags, out long pllTimestamp, out IMFSample ppSample);
206        [PreserveSig] int Flush(uint dwStreamIndex);
207        [PreserveSig] int GetServiceForStream(uint dwStreamIndex, [In] ref Guid guidService, [In] ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppvObject);
208        [PreserveSig] int GetPresentationAttribute(uint dwStreamIndex, [In] ref Guid guidAttribute, IntPtr pvarAttribute);
209    }
210
211    // ===== IMFMediaEngine — built-in GPU-accelerated video playback (Windows 8+) =====
212
213    [ComImport, Guid("fee7c112-e776-42b5-9bbf-0048524e2bd5")]
214    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
215    public interface IMFMediaEngineNotify
216    {
217        [PreserveSig] int EventNotify(uint eventCode, IntPtr param1, uint param2);
218    }
219
220    [ComImport, Guid("4d645ace-26aa-4688-9be1-df3516990b93")]
221    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
222    public interface IMFMediaEngineClassFactory
223    {
224        [PreserveSig] int CreateInstance(uint dwFlags,
225            [MarshalAs(UnmanagedType.Interface)] IMFAttributes pAttr,
226            [MarshalAs(UnmanagedType.Interface)] out IMFMediaEngine ppPlayer);
227    }
228
229    [ComImport, Guid("98a1b0bb-03eb-4935-ae7c-93c1fa0e1c93")]
230    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
231    public interface IMFMediaEngine
232    {
233        [PreserveSig] int    GetError(out IntPtr ppError);
234        [PreserveSig] int    SetErrorCode(int error);
235        [PreserveSig] int    SetSourceElements(IntPtr pSrcElements);
236        [PreserveSig] int    SetSource([MarshalAs(UnmanagedType.BStr)] string pUrl);
237        [PreserveSig] int    GetCurrentSource([MarshalAs(UnmanagedType.BStr)] out string ppUrl);
238        [PreserveSig] ushort GetNetworkState();
239        [PreserveSig] int    GetPreload();
240        [PreserveSig] int    SetPreload(int preload);
241        [PreserveSig] int    GetBuffered(out IntPtr ppBuffered);
242        [PreserveSig] int    Load();
243        [PreserveSig] int    CanPlayType([MarshalAs(UnmanagedType.BStr)] string type, out int pAnswer);
244        [PreserveSig] ushort GetReadyState();
245        [PreserveSig] bool   IsSeeking();
246        [PreserveSig] double GetCurrentTime();
247        [PreserveSig] int    SetCurrentTime(double seekTime);
248        [PreserveSig] double GetStartTime();
249        [PreserveSig] double GetDuration();
250        [PreserveSig] bool   IsPaused();
251        [PreserveSig] double GetDefaultPlaybackRate();
252        [PreserveSig] int    SetDefaultPlaybackRate(double rate);
253        [PreserveSig] double GetPlaybackRate();
254        [PreserveSig] int    SetPlaybackRate(double rate);
255        [PreserveSig] int    GetPlayed(out IntPtr ppPlayed);
256        [PreserveSig] int    GetSeekable(out IntPtr ppSeekable);
257        [PreserveSig] bool   IsEnded();
258        [PreserveSig] bool   GetAutoPlay();
259        [PreserveSig] int    SetAutoPlay(bool autoPlay);
260        [PreserveSig] bool   GetLoop();
261        [PreserveSig] int    SetLoop(bool loop);
262        [PreserveSig] int    Play();
263        [PreserveSig] int    Pause();
264        [PreserveSig] bool   GetMuted();
265        [PreserveSig] int    SetMuted(bool muted);
266        [PreserveSig] double GetVolume();
267        [PreserveSig] int    SetVolume(double volume);
268        [PreserveSig] bool   HasVideo();
269        [PreserveSig] bool   HasAudio();
270        [PreserveSig] int    GetNativeVideoSize(out uint cx, out uint cy);
271        [PreserveSig] int    GetVideoAspectRatio(out uint cx, out uint cy);
272        [PreserveSig] int    Shutdown();
273        [PreserveSig] int    TransferVideoFrame(IntPtr pDstSurf, IntPtr pSrc, IntPtr pDst, IntPtr pBorderClr);
274        [PreserveSig] int    OnVideoStreamTick(out long pPts);
275    }
276}