VIDEOINFOHEADER m_VideoInfo; // Derived from the AMMediaType object #endregion #region Constructor internal VideoCapability(AMMediaType mediaType, VIDEO_STREAM_CONFIG_CAPS caps) { if (null == mediaType) throw new ArgumentNullException("mediaType"); m_MediaType = mediaType; m_StreamConfigCaps = caps; m_VideoInfo = (VIDEOINFOHEADER)Marshal.PtrToStructure(mediaType.formatPtr, typeof(VIDEOINFOHEADER)); }
void GetCaptureSize() { int hr = 0; // After the filter is added to the graph, and before // it is connected to anything, we need to set the format // and size that we expect. AMMediaType mt = new AMMediaType(); hr = sampleGrabber.GetConnectedMediaType(mt); DsError.ThrowExceptionForHR(hr); try { if ((mt.formatType != FormatType.VideoInfo) || (mt.formatPtr == IntPtr.Zero)) { throw new NotSupportedException("Unknown Grabber Media Format"); } // Get the struct VIDEOINFOHEADER videoInfoHeader = new VIDEOINFOHEADER(); Marshal.PtrToStructure(mt.formatPtr, videoInfoHeader); // Grab the size info fGrabber.Width = videoInfoHeader.BmiHeader.biWidth; fGrabber.Height = videoInfoHeader.BmiHeader.biHeight; fGrabber.FrameBytes = (int)videoInfoHeader.BmiHeader.biSizeImage; } finally { mt.Dispose(); } }
/// <summary> /// Configure the capture filter once it is in the graph. /// </summary> /// <param name="streamConfig"></param> /// <param name="iWidth">Desired width of video</param> /// <param name="iHeight">Desired height of video</param> /// <param name="bitsPerPixel">Number of bits per pixel</param> /// <remarks> /// We configure the source filter with the desired size, mediasubtype, and /// bits per pixel. This must be done before the capture pin is connected /// to other filters in the graph. /// </remarks> private void ConfigureSourceFilter(IAMStreamConfig streamConfig, int iWidth, int iHeight, short bitsPerPixel) { int hr; AMMediaType mType = new AMMediaType(); VIDEOINFOHEADER v; // First, we need to get the existing format block // If there's an error, throw an exception. hr = streamConfig.GetFormat(out mType); DsError.ThrowExceptionForHR(hr); // Return early if the media type is not what we expect // First, the majorType must be 'Video' // Second, the size of the format structure must be sizeof(VIDEOINFOHEADER) if ((MediaType.Video != mType.majorType) || (mType.formatSize != Marshal.SizeOf(typeof(VIDEOINFOHEADER)))) return; try { // The formatPtr member of the AMMediaType object points // to a chunk of unmanaged memory that contains the data // for the format structure. We need to copy this data // into a structure object so we can manipulate it. v = new VIDEOINFOHEADER(); Marshal.PtrToStructure(mType.formatPtr, v); // In order to change the size and bits per pixel, we // need to change the values of the VIDEOINFOHEADER object // that we pulled out from the AMMediaType object. if (iWidth > 0) { v.BmiHeader.biWidth = iWidth; } if (iHeight > 0) { v.BmiHeader.biHeight = iHeight; } if (bitsPerPixel > 0) { v.BmiHeader.biBitCount = (ushort)bitsPerPixel; } // Now we copy the VIDEOINFOHEADER structure back into // a piece of unmanaged memory and give the pointer to // the AMMediaType structure. Marshal.StructureToPtr(v, mType.formatPtr, false); // Finally, we set the new format on the pin hr = streamConfig.SetFormat(mType); DsError.ThrowExceptionForHR(hr); } finally { mType.Dispose(); } }