我正在尝试在c#代码中实现filtergraph。 过滤器在graphstudionext中完全可以正常工作,但在尝试连接最后两个过滤器(mpeg-mux过滤器和文件编写器过滤器)时,我得到一个AccessViolationException。
访问错误的原因是什么?它什么时候发生的?你怎么弄清楚如何解决它?可能是错误的原因不是我的代码而是过滤器吗?
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)未知
答案 0 :(得分:0)
一些调试这个的建议。其中大部分适用于调试从托管代码调用的本机代码的任何问题,而某些问题特定于DirectShow。如果不了解更多关于崩溃本身的具体建议是很困难的。任何过滤器甚至DirectShow本身都可能崩溃。