直播问题

发布于 2024-10-30 17:45:12 字数 3490 浏览 3 评论 0原文

我正在使用 DirectShow.net 从网络摄像头捕获图像,在搜索网络后我得到了此代码,它工作正常:

要查看原始的完整代码,请点击下面的链接

 bool SetupGraph()
        {
            int hr;
            try
            {
                hr = capGraph.SetFiltergraph(graphBuilder);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                hr = graphBuilder.AddFilter(capFilter, "Ds.NET Video Capture Device");
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                DsUtils.ShowCapPinDialog(capGraph, capFilter, this.Handle);

                AMMediaType media = new AMMediaType();
                media.majorType = MediaType.Video;
                media.subType = MediaSubType.RGB24;
                media.formatType = FormatType.VideoInfo;        // ???
                hr = sampGrabber.SetMediaType(media);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                hr = graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber");
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                Guid cat = PinCategory.Preview;
                Guid med = MediaType.Video;
                hr = capGraph.RenderStream(ref cat, ref med, capFilter, null, null); // baseGrabFlt 
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                cat = PinCategory.Capture;
                med = MediaType.Video;
                hr = capGraph.RenderStream(ref cat, ref med, capFilter, null, baseGrabFlt); // baseGrabFlt 
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                media = new AMMediaType();
                hr = sampGrabber.GetConnectedMediaType(media);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);
                if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero))
                    throw new NotSupportedException("صيغه غير معروفه");

                videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader));
                Marshal.FreeCoTaskMem(media.formatPtr); media.formatPtr = IntPtr.Zero;

                hr = sampGrabber.SetBufferSamples(false);
                if (hr == 0)
                    hr = sampGrabber.SetOneShot(false);
                if (hr == 0)
                    hr = sampGrabber.SetCallback(null, 0);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                return true;
            }
            catch (Exception ee)
            {
                MessageBox.Show(this, "Could not setup graph\r\n" + ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return false;
            }
        }

我的问题是当我从另一个表单调用此表单时第二次,当编译器到达此部分时,它给我这个错误:“值不在预期范围内”hr = capGraph.RenderStream(ref cat, ref med, capFilter, null, null);

实际上,我发现当我物理拔下网络摄像头并再次插入时,问题就消失了,所以我得出的结论是我需要使用代码拔掉它 所以,如果您知道请告诉我该怎么做,或者如果您有任何更好的想法,请告诉我

您可以访问原始代码 http://www.codeproject.com/script/Content/ViewAssociatedFile.aspx?rzp=%2FKB%2Fdotnet%2FROTEsys%2Frotesys_src.zip&zep=Tracking.cs&obid=9401& ;obtid=2&ovid=1[^]

感谢您的时间和耐心

I'm using DirectShow.net to capture images from a webCame,after searching the web I got this code and it works fine:

to see the original full code kindly follow the link below

 bool SetupGraph()
        {
            int hr;
            try
            {
                hr = capGraph.SetFiltergraph(graphBuilder);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                hr = graphBuilder.AddFilter(capFilter, "Ds.NET Video Capture Device");
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                DsUtils.ShowCapPinDialog(capGraph, capFilter, this.Handle);

                AMMediaType media = new AMMediaType();
                media.majorType = MediaType.Video;
                media.subType = MediaSubType.RGB24;
                media.formatType = FormatType.VideoInfo;        // ???
                hr = sampGrabber.SetMediaType(media);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                hr = graphBuilder.AddFilter(baseGrabFlt, "Ds.NET Grabber");
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                Guid cat = PinCategory.Preview;
                Guid med = MediaType.Video;
                hr = capGraph.RenderStream(ref cat, ref med, capFilter, null, null); // baseGrabFlt 
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                cat = PinCategory.Capture;
                med = MediaType.Video;
                hr = capGraph.RenderStream(ref cat, ref med, capFilter, null, baseGrabFlt); // baseGrabFlt 
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                media = new AMMediaType();
                hr = sampGrabber.GetConnectedMediaType(media);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);
                if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero))
                    throw new NotSupportedException("صيغه غير معروفه");

                videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader));
                Marshal.FreeCoTaskMem(media.formatPtr); media.formatPtr = IntPtr.Zero;

                hr = sampGrabber.SetBufferSamples(false);
                if (hr == 0)
                    hr = sampGrabber.SetOneShot(false);
                if (hr == 0)
                    hr = sampGrabber.SetCallback(null, 0);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                return true;
            }
            catch (Exception ee)
            {
                MessageBox.Show(this, "Could not setup graph\r\n" + ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return false;
            }
        }

my problem is when I call this form from another form for a second time it gives me this error: "Value does not fall within the expected range" when the compiler comes to this section:
hr = capGraph.RenderStream(ref cat, ref med, capFilter, null, null);

actually i discovered that the problem disappear when I unplug the web-came physically and plug it again, so I concluded that i need to unplug it using code
so please if u know tell me how to do it or if u have any better idea please tell me

you can access the original code on
http://www.codeproject.com/script/Content/ViewAssociatedFile.aspx?rzp=%2FKB%2Fdotnet%2FROTEsys%2Frotesys_src.zip&zep=Tracking.cs&obid=9401&obtid=2&ovid=1[^]

thank you for ur time and patience

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若能看破又如何 2024-11-06 17:45:12

单个视频源一次只能运行一个图,将其视为具有单个源的管道。

如果您想启动另一个图表,则必须先停止第一个图表 - 您的代码当前在 CloseInterfaces(); 中执行此操作 - 因此,如果您在构建第二个图表之前调用该图表,应该没问题图形。

Only one graph for a single video source can be running at a time, think of it as a pipeline with a single source.

If you want to start another graph, you will have to stop the first one first - your code currently does that in CloseInterfaces(); - so you should be fine if you call that before building up the second graph.

十二 2024-11-06 17:45:12

问题可能是当你关闭接口时,你必须在 DirectShow 的所有接口中使用 Marshal.ReleaseComObject ,而且,你必须使用方法 RemoveFilter您的 IGraphBuilder 来释放您使用的捕获过滤器,如果您不这样做,图形生成器将不会被释放。

The problem is probably when you are closing the interfaces, you have to use Marshal.ReleaseComObject in all your interfaces of DirectShow, and also, you have to use the method RemoveFilter of your IGraphBuilder to relase your used capture filter, if you don't do so the graph builder won't be released.

天邊彩虹 2024-11-06 17:45:12

是的,在 CloseInterfases() 方法中,添加以下行:

if (graphBuilder != null)
{
    graphBuilder.RemoveFilter(capFilter);
}
baseGrabFlt = null;

Yes, in the CloseInterfases() method, add the following lines:

if (graphBuilder != null)
{
    graphBuilder.RemoveFilter(capFilter);
}
baseGrabFlt = null;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文