/// <summary> /// Returns an array of DsDevices of type devcat. /// </summary> /// <param name="cat">Any one of FilterCategory</param> public static MFDevice[] GetDevicesOfCat(Guid FilterCategory) { // Use arrayList to build the retun list since it is easily resizable MFDevice[] devret = null; IMFActivate[] ppDevices; ////////// HResult hr = 0; IMFAttributes pAttributes = null; // Initialize an attribute store. We will use this to // specify the enumeration parameters. hr = MFExtern.MFCreateAttributes(out pAttributes, 1); // Ask for source type = video capture devices if (hr >= 0) { hr = pAttributes.SetGUID( MFAttributesClsid.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, FilterCategory ); } // Enumerate devices. int cDevices; if (hr >= 0) { hr = MFExtern.MFEnumDeviceSources(pAttributes, out ppDevices, out cDevices); if (hr >= 0) { devret = new MFDevice[cDevices]; for (int x = 0; x < cDevices; x++) { devret[x] = new MFDevice(ppDevices[x]); } } } if (pAttributes != null) { Marshal.ReleaseComObject(pAttributes); } return(devret); }
private void LoadDevicesList() { // Populate the list with the friendly names of the devices. MFDevice[] arDevices = MFDevice.GetDevicesOfCat(CLSID.MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID); lstbDevices.BeginUpdate(); lstbDevices.Items.Clear(); foreach (MFDevice m in arDevices) { lstbDevices.Items.Add(m); } lstbDevices.EndUpdate(); bttOK.Enabled = false; }
//------------------------------------------------------------------- // SetDevice // // Set up preview for a specified video capture device. //------------------------------------------------------------------- public HResult SetDevice(MFDevice pDevice) { HResult hr = HResult.S_OK; IMFActivate pActivate = pDevice.Activator; IMFMediaSource pSource = null; IMFAttributes pAttributes = null; object o = null; lock (this) { try { // Release the current device, if any. hr = CloseDevice(); if (Succeeded(hr)) { // Create the media source for the device. hr = pActivate.ActivateObject(typeof(IMFMediaSource).GUID, out o); } if (Succeeded(hr)) { pSource = (IMFMediaSource)o; } // Get Symbolic device link m_pwszSymbolicLink = pDevice.SymbolicName; // // Create the source reader. // // Create an attribute store to hold initialization settings. if (Succeeded(hr)) { hr = MFExtern.MFCreateAttributes(out pAttributes, 2); } if (Succeeded(hr)) { hr = pAttributes.SetUINT32(MFAttributesClsid.MF_READWRITE_DISABLE_CONVERTERS, 1); } if (Succeeded(hr)) { hr = pAttributes.SetUnknown(MFAttributesClsid.MF_SOURCE_READER_ASYNC_CALLBACK, this); } IMFSourceReader pRead = null; if (Succeeded(hr)) { hr = MFExtern.MFCreateSourceReaderFromMediaSource(pSource, pAttributes, out pRead); } if (Succeeded(hr)) { m_pReader = (IMFSourceReaderAsync)pRead; } if (Succeeded(hr)) { // Try to find a suitable output type. for (int i = 0; ; i++) { IMFMediaType pType; hr = m_pReader.GetNativeMediaType((int)MF_SOURCE_READER.FirstVideoStream, i, out pType); if (Failed(hr)) { break; } try { hr = TryMediaType(pType); if (Succeeded(hr)) { // Found an output type. break; } } finally { SafeRelease(pType); } } } if (Succeeded(hr)) { hr = ConfigWriter(); if (Succeeded(hr)) { m_draw.SetBitmap(BitmapOverlayFile); // Ask for the first sample. hr = m_pReader.ReadSample((int)MF_SOURCE_READER.FirstVideoStream, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } } if (Failed(hr)) { if (pSource != null) { pSource.Shutdown(); // NOTE: The source reader shuts down the media source // by default, but we might not have gotten that far. } CloseDevice(); } } finally { SafeRelease(pSource); SafeRelease(pAttributes); } } return(hr); }