尝试连接筛选器时AccessViolationException

时间:2014-05-08 10:37:13

标签: c# directshow directshow.net

我正在尝试在c#代码中实现filtergraph。 过滤器在graphstudionext中完全可以正常工作,但在尝试连接最后两个过滤器(mpeg-mux过滤器和文件编写器过滤器)时,我得到一个AccessViolationException。

访问错误的原因是什么?它什么时候发生的?你怎么弄清楚如何解决它?可能是错误的原因不是我的代码而是过滤器吗?

filtergraph Mpeg-4 Multiplexor和Filewriter之间发生链接错误 这是我的代码:(错误发生在底部)

  private void init()
        {
            m_filterGraph = new FilterGraph() as IFilterGraph2;
            mediaControl = (IMediaControl)m_filterGraph;
            mediaEvent = (IMediaEvent)m_filterGraph;


            int hr;
            DsDevice[] audiDevices;

            audiDevices = DsDevice.GetDevicesOfCat(FilterCategory.AudioInputDevice);
            List<IPin> audioOutputs = new List<IPin>();

            DsDevice[] capDevices;

            // Get the collection of video devices
            capDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);

            if (capDevices.Count() < 2)
            {
                throw new Exception("Not enough video input devices found");
            }
            ISampleGrabber sampGrabber = null;
            IBaseFilter baseGrabFlt = null;
            List<IBaseFilter> capFilter = new List<IBaseFilter>();
            IBaseFilter muxFilter = null;
            IFileSinkFilter fileWriterFilter = null;
            ICaptureGraphBuilder2 capGraph = null;

            try
            {
                capGraph = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
                hr = capGraph.SetFiltergraph(m_filterGraph);
                DsError.ThrowExceptionForHR(hr);
                for (int i = 0; i < Math.Min(capDevices.Count(), 2); i++)
                {
                    IBaseFilter _capFIlter;
                    hr = m_filterGraph.AddSourceFilterForMoniker(capDevices[i].Mon, null, capDevices[i].Name, out _capFIlter);

                    DsError.ThrowExceptionForHR(hr);
                    capFilter.Add(_capFIlter);
                }


                DsError.ThrowExceptionForHR(hr);

                for (int i = 0; i < Math.Min(audiDevices.Length, 1); i++)
                {
                    IBaseFilter _audioFilter;
                    hr = m_filterGraph.AddSourceFilterForMoniker(audiDevices[i].Mon, null, audiDevices[i].Name,
                        out _audioFilter);
                    IPin audiooutpin = FindPinByDirection(_audioFilter, PinDirection.Output);
                    audioOutputs.Add(audiooutpin);
                }

                //MJPEG Decompressor
                IBaseFilter mjpeg_dec0 = null;
                IBaseFilter mjpeg_dec1 = null;
                Guid mjpegguid = Guid.Parse("301056D0-6DFF-11D2-9EEB-006008039E37");
                Type t1 = Type.GetTypeFromCLSID(mjpegguid);
                mjpeg_dec0 = (IBaseFilter) Activator.CreateInstance(t1);
                mjpeg_dec1 = (IBaseFilter) Activator.CreateInstance(t1);
                hr = m_filterGraph.AddFilter(mjpeg_dec0, "MJPEG0");
                hr = m_filterGraph.AddFilter(mjpeg_dec1, "MJPEG1");
                IPin cam1out;
                IPin cam2out;

                IPin pinmjpegIn = FindPinByDirection(mjpeg_dec0, PinDirection.Input);
                IPin pinjmpegIn1 = FindPinByDirection(mjpeg_dec1, PinDirection.Input);

                if (capFilter.Count < 1 || capFilter[0] == null)
                {
                    throw new Exception("No input devices found!");
                }
                IPin cam0Out = FindPinByDirection(capFilter[0], PinDirection.Output);
                m_filterGraph.Connect(cam0Out, pinmjpegIn);

                if (capFilter.Count >= 1 && capFilter[1] != null)
                {
                    cam1out = FindPinByDirection(capFilter[1], PinDirection.Output);
                    m_filterGraph.Connect(cam1out, pinjmpegIn1);
                }


                //MyFilter
                IPin mjpegOut = FindPinByDirection(mjpeg_dec0, PinDirection.Output);
                IPin mjpegout1 = FindPinByDirection(mjpeg_dec1, PinDirection.Output);





                IBaseFilter myfitlter = null;


                Guid myfilterGuid = Guid.Parse("067216DE-E6A1-49C9-A016-074624C20FE5");
                Type t = Type.GetTypeFromCLSID(myfilterGuid);
                myfitlter = (IBaseFilter)Activator.CreateInstance(t);

                var imyfilter = (IMyFilter)myfitlter;

                //int result = imyfilter.SetIntervalText("THis is intervall test");
                //int result2 = imyfilter.SetIntroText("This is intro text test");

                hr = m_filterGraph.AddFilter(myfitlter, "MyFilter");
                DsError.ThrowExceptionForHR(hr);
                IPin mypin0;
                hr = myfitlter.FindPin("PIN0", out mypin0);
                DsError.ThrowExceptionForHR(hr);
                IPin mypin1;
                hr = myfitlter.FindPin("PIN1", out mypin1);
                DsError.ThrowExceptionForHR(hr);
                IPin mypinin2;

                IPin mypinout;
                hr = myfitlter.FindPin("PINOUT0", out mypinout);

                hr = m_filterGraph.Connect(mjpegOut, mypin0);
                DsError.ThrowExceptionForHR(hr);
                hr = m_filterGraph.Connect(mjpegout1, mypin1);
                DsError.ThrowExceptionForHR(hr);



                //Color space convertor

                IBaseFilter colorSpance = null;
                Guid csGUID = Guid.Parse("1643E180-90F5-11CE-97D5-00AA0055595A");
                Type t2 = Type.GetTypeFromCLSID(csGUID);
                colorSpance = (IBaseFilter)Activator.CreateInstance(t2);
                hr = m_filterGraph.AddFilter(colorSpance, "ColorSpaceConvertor");
                IPin csIn = FindPinByDirection(colorSpance, PinDirection.Input);
                IPin csOut = FindPinByDirection(colorSpance, PinDirection.Output);

                hr = m_filterGraph.Connect(mypinout, csIn);
                DsError.ThrowExceptionForHR(hr);



                IBaseFilter ffdshowfilter = null;
                Guid ffdshowguid = Guid.Parse("4DB2B5D9-4556-4340-B189-AD20110D953F");
                Type ffdshowtype = Type.GetTypeFromCLSID(ffdshowguid);
                ffdshowfilter = (IBaseFilter)Activator.CreateInstance(ffdshowtype);
                m_filterGraph.AddFilter(ffdshowfilter, "FFD");
                IPin pinffdin = FindPinByDirection(ffdshowfilter, PinDirection.Input);
                IPin pinffdout = FindPinByDirection(ffdshowfilter, PinDirection.Output);
                hr = m_filterGraph.Connect(csOut, pinffdin);
                DsError.ThrowExceptionForHR(hr);


                IBaseFilter mpegmuxfilter = null;
                Guid mpegmux = Guid.Parse("5FD85181-E542-4E52-8D9D-5D613C30131B");
                Type mpegmuxType = Type.GetTypeFromCLSID(mpegmux);
                mpegmuxfilter = (IBaseFilter)Activator.CreateInstance(mpegmuxType);
                hr = m_filterGraph.AddFilter(mpegmuxfilter, "MpegMux");

                DsError.ThrowExceptionForHR(hr);
                IPin mpegIn = FindPinByDirection(mpegmuxfilter, PinDirection.Input);

                hr = m_filterGraph.Connect(pinffdout, mpegIn);
                IPin mpegOut = FindPinByDirection(mpegmuxfilter, PinDirection.Output);

                DsError.ThrowExceptionForHR(hr);


                m_filterGraph.Connect(pinffdout, mpegIn);

                IBaseFilter filewriter = null;
                filewriter = new FileWriter() as IBaseFilter;

               filesinkFilter= (IFileSinkFilter)filewriter;

                hr = m_filterGraph.AddFilter(filewriter, "FileWriter");
                DsError.ThrowExceptionForHR(hr);
                IPin fileIn = FindPinByDirection(filewriter, PinDirection.Input);
                m_filterGraph.Connect(mpegOut, fileIn); //Here an access violation exception is triggered
                foreach (IPin pin in audioOutputs)
                {
                    IPin muxInPin = FindPinByDirection(mpegmuxfilter, PinDirection.Input);
                    m_filterGraph.Connect(pin, muxInPin);
                }

             }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
        }

这又是触发异常的行:

 m_filterGraph.Connect(mpegOut, fileIn); //Here an access violation exception is triggered

非常感谢任何帮助,建议或信息,谢谢。

callstack没有显示很多有用的信息。有一个原生的转换,我无法在visual studio中看到哪些函数在那里被调用,也无法使用调试器进入该方法。

  

[管理到原生过渡]

     
    

YourControlService.dll!YourControlService.YourControlRecorder.init()第207行C#

  
     

YourControlService.dll!YourControlService.YourControlRecorder.startRecording(串   filename =&#34; D:\ TEMP \ test_2014_5_31.avi&#34;)第229行C#

     

YourControlService.dll!YourControlService.Service.StartRecording(串   name =&#34; test&#34 ;, string text =&#34; text&#34;)第79行C#[轻量级   功能]

     

System.ServiceModel.dll!System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象   实例,对象[]输入,输出对象[]输出)未知     System.ServiceModel.dll!System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(REF   System.ServiceModel.Dispatcher.MessageRpc rpc =   {System.ServiceModel.Dispatcher.MessageRpc})未知

     

System.ServiceModel.dll!System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(参照   System.ServiceModel.Dispatcher.MessageRpc rpc =   {System.ServiceModel.Dispatcher.MessageRpc})未知

     

System.ServiceModel.dll!System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(参照   System.ServiceModel.Dispatcher.MessageRpc rpc)未知

     

System.ServiceModel.dll!System.ServiceModel.Dispatcher.MessageRpc.Process(布尔   isOperationContextSet = false)未知

     

System.ServiceModel.dll!System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(System.ServiceModel.Channels.RequestContext   请求=   {} System.ServiceModel.Channels.HttpRequestContext.ListenerHttpContext,   bool cleanThread,System.ServiceModel.OperationContext   currentOperationContext)未知

     

System.ServiceModel.dll!System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(System.ServiceModel.Channels.RequestContext   request,System.ServiceModel.OperationContext   currentOperationContext)未知

     

System.ServiceModel.dll!System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(System.IAsyncResult   结果)未知

     

System.ServiceModel.Internals.dll!System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(System.IAsyncResult   结果)未知

     

System.ServiceModel.Internals.dll!System.Runtime.AsyncResult.Complete(布尔   completedSynchronously)未知

     

System.ServiceModel.Internals.dll!System.Runtime.InputQueue.AsyncQueueReader.Set(System.Runtime.InputQueue.Item   item)未知

     

System.ServiceModel.Internals.dll!System.Runtime.InputQueue.EnqueueAndDispatch(System.Runtime.InputQueue.Item   item =   {} System.Runtime.InputQueue.Item,   bool canDispatchOnThisThread)未知

     

System.ServiceModel.Internals.dll!System.Runtime.InputQueue.EnqueueAndDispatch(System.ServiceModel.Channels.RequestContext   item,System.Action dequeuedCallback,bool   canDispatchOnThisThread)未知

     

System.ServiceModel.dll!System.ServiceModel.Channels.SingletonChannelAcceptor.Enqueue(System.ServiceModel.Channels.RequestContext   item,System.Action dequeuedCallback,bool   canDispatchOnThisThread)未知

     

System.ServiceModel.dll!System.ServiceModel.Channels.HttpPipeline.EnqueueMessageAsyncResult.CompleteParseAndEnqueue(System.IAsyncResult   结果)未知

     

System.ServiceModel.dll!System.ServiceModel.Channels.HttpPipeline.EnqueueMessageAsyncResult.HandleParseIncomingMessage(System.IAsyncResult   结果)未知

     

System.ServiceModel.Internals.dll!System.Runtime.AsyncResult.SyncContinue(System.IAsyncResult   结果)未知

     

System.ServiceModel.dll!System.ServiceModel.Channels.HttpPipeline.EmptyHttpPipeline.BeginProcessInboundRequest(System.ServiceModel.Channels.ReplyChannelAcceptor   replyChannelAcceptor,System.Action dequeuedCallback,   System.AsyncCallback回调,对象状态)未知

     

System.ServiceModel.dll!System.ServiceModel.Channels.HttpChannelListener.HttpContextReceivedAsyncResult.ProcessHttpContextAsync()未知

     

System.ServiceModel.dll!System.ServiceModel.Channels.HttpChannelListener.BeginHttpContextReceived(System.ServiceModel.Channels.HttpRequestContext   context,System.Action acceptorCallback,System.AsyncCallback   回调,对象状态)未知

     

System.ServiceModel.dll!System.ServiceModel.Channels.SharedHttpTransportManager.EnqueueContext(System.IAsyncResult   listenerContextResult)未知

     

System.ServiceModel.dll!System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContextCore(System.IAsyncResult   listenerContextResult = {System.Net.ListenerAsyncResult})未知

     

System.ServiceModel.Internals.dll!System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(System.IAsyncResult   结果)未知

     

System.dll中!System.Net.LazyAsyncResult.Complete(System.IntPtr   userToken)未知

     

System.dll中!System.Net.ListenerAsyncResult.IOCompleted(System.Net.ListenerAsyncResult   asyncResult,uint errorCode,uint numBytes)Unknown

     

mscorlib.dll中!System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UINT   errorCode = 0x00000000,uint numBytes = 0x000004ed,   System.Threading.NativeOverlapped * pOVERLAP =   0x000000000330faf8)未知

1 个答案:

答案 0 :(得分:0)

一些调试这个的建议。其中大部分适用于调试从托管代码调用的本机代码的任何问题,而某些问题特定于DirectShow。如果不了解更多关于崩溃本身的具体建议是很困难的。任何过滤器甚至DirectShow本身都可能崩溃。

  • 尽可能使用ConnectDirect而不是Connect(智能连接)。智能连接可能会创建在系统上注册的任何其他DirectShow过滤器。有缺陷的过滤器可能会导致崩溃。在GraphStudioNext中,图形构建报告将显示智能连接在幕后进行的一些操作。 GraphStudioNext还允许您在DirectConnect,DirectConnect和媒体类型以及智能连接模式和黑名单过滤器之间进行选择,以防止它们被Intelligent Connect使用。
  • 当您连接或流式传输时,图表中没有任何额外的未连接过滤器,除非您希望智能连接使用它们。写得好的过滤器什么都不做,但其他过滤器可能导致图形失败。
  • 在混合模式下调试以调试本机代码和托管(假设您没有使用不支持此功能的免费版Visual Studio)。在Visual Studio 2010中,转到Solution explorer,右键单击项目名称,属性,调试,启用“启用非托管代码调试”。
  • 如果在Visual Studio外部及时调试,请调试崩溃,请求手动选择调试引擎并启用本机和托管代码调试。
  • 下载系统DLL的符号以获得更好的调用堆栈。在“模块”窗口中,选择“全部”,右键单击“加载符号”,Microsoft Symbol服务器。这可能需要一段时间,不能中断。 Visual Studio可以缓存这些符号,因此您不必每次都下载它们(此目录中的工具,选项,调试,符号,缓存符号)。拥有Quartz,Kernel32,User32,Gdi。
  • 的最新符号尤为重要
  • 保存崩溃转储以供以后分析,调试,保存转储为,Minidump(您不需要带堆的minidump,这些是巨大的文件)。
  • 在GraphEdit或GraphStudioNext中保存GRF文件以进行分析,并在加载GRF文件并在图形编辑工具中尝试连接时查看它是否仍然崩溃。 C ++本机GRF文件保存代码在Saving a Filter Graph to a GraphEdit File
  • Create a log file for the graph
  • Export your graph to the running object table并从图表编辑工具
  • 连接