CodeTV.GraphBuilderWDM.SetCaptureResolution C# (CSharp) Method

SetCaptureResolution() private method

private SetCaptureResolution ( ChannelAnalogic captureFormat ) : void
captureFormat ChannelAnalogic
return void
        private void SetCaptureResolution(ChannelAnalogic.CaptureFormat captureFormat)
        {
            object o = null;
            int hr = this.captureGraphBuilder.FindInterface(null, //PinCategory.Preview, // Preview pin.
                MediaType.Video, //null,    // Any media type.
                this.videoCaptureFilter, // Pointer to the capture filter.
                typeof(IAMStreamConfig).GUID,
                out o);
            if (hr >= 0)
            {
                IAMStreamConfig amStreamConfig = o as IAMStreamConfig;

                AMMediaType mediaType;
                hr = amStreamConfig.GetFormat(out mediaType);
                if (hr >= 0)
                {
                    if ((mediaType.majorType == MediaType.Video) &&
                        (mediaType.formatType == FormatType.VideoInfo) &&
                        (mediaType.formatSize >= Marshal.SizeOf(typeof(VideoInfoHeader))) &&
                        (mediaType.formatPtr != IntPtr.Zero))
                    {
                        VideoInfoHeader videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.formatPtr, typeof(VideoInfoHeader));

                        Size resolution = new Size(videoInfoHeader.BmiHeader.Width, videoInfoHeader.BmiHeader.Height);
                        int framePerSecond = (int)(10000000.0 / videoInfoHeader.AvgTimePerFrame);
                        string mediaSubType = (string)DeviceEnumerator.MediaSubTypeByGUID[mediaType.subType];

                        if (captureFormat.Resolution == resolution &&
                            captureFormat.FramePerSecond == framePerSecond &&
                            captureFormat.MediaSubType == mediaSubType)
                            return;
                    }
                    DsUtils.FreeAMMediaType(mediaType);
                }

                int iCount = 0, iSize = 0;
                hr = amStreamConfig.GetNumberOfCapabilities(out iCount, out iSize);

                // Check the size to make sure we pass in the correct structure.
                if (iSize == Marshal.SizeOf(typeof(VideoStreamConfigCaps)))
                {
                    // Use the video capabilities structure.

                    VideoStreamConfigCaps scc = new VideoStreamConfigCaps();
                    GCHandle gchScc = GCHandle.Alloc(scc, GCHandleType.Pinned);
                    IntPtr pScc = gchScc.AddrOfPinnedObject();

                    for (int iFormat = 0; iFormat < iCount; iFormat++)
                    {
                        hr = amStreamConfig.GetStreamCaps(iFormat, out mediaType, pScc);
                        if (hr >= 0)
                        {
                            if (mediaType != null &&
                                mediaType.majorType == MediaType.Video &&
                                mediaType.formatType == FormatType.VideoInfo &&
                                mediaType.formatSize >= Marshal.SizeOf(typeof(VideoInfoHeader)) &&
                                mediaType.formatPtr != IntPtr.Zero)
                            {
                                VideoInfoHeader videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.formatPtr, typeof(VideoInfoHeader));

                                Size resolution = new Size(videoInfoHeader.BmiHeader.Width, videoInfoHeader.BmiHeader.Height);
                                int framePerSecond = (int)(10000000.0 / videoInfoHeader.AvgTimePerFrame);
                                string mediaSubType = (string)DeviceEnumerator.MediaSubTypeByGUID[mediaType.subType];

                                if (captureFormat.Resolution == resolution &&
                                    captureFormat.FramePerSecond == framePerSecond &&
                                    captureFormat.MediaSubType == mediaSubType)
                                {
                                    StopGraph();

                                    hr = amStreamConfig.SetFormat(mediaType);
                                    break;
                                }
                                DsUtils.FreeAMMediaType(mediaType);
                            }
                        }
                    }
                    gchScc.Free();
                }
            }
        }