我使用c#创建了一个Directshow Filtergraph,但我不知道如何添加一个 WmAsfReader过滤器,可以将视频文件解码为过滤器图形。 任何人都可以帮忙,谢谢!
以下代码就是我所做的。
/*MediaUriPlayer*/
protected virtual void OpenSource()
{
FrameCount = 0;
/* Make sure we clean up any remaining mess */
FreeResources();
/* make sure source is not null */
if (m_sourceUri == null)
return;
string fileSource = m_sourceUri.OriginalString;
if (string.IsNullOrEmpty(fileSource))
return;
try
{
/* Creates the GraphBuilder COM object */
m_graph = new FilterGraphNoThread() as IGraphBuilder;
if (m_graph == null)
throw new Exception("Could not create a graph");
/* Add our prefered audio renderer */
InsertAudioRenderer(AudioRenderer);
var filterGraph = m_graph as IFilterGraph2;
if (filterGraph == null)
throw new Exception("Could not QueryInterface for the IFilterGraph2");
IBaseFilter renderer = CreateVideoMixingRenderer9(m_graph, 1);
IBaseFilter sourceFilter;
/* Have DirectShow find the correct source filter for the Uri */
var hr = filterGraph.AddSourceFilter(fileSource, fileSource, out sourceFilter);
DsError.ThrowExceptionForHR(hr);
/* We will want to enum all the pins on the source filter */
IEnumPins pinEnum;
hr = sourceFilter.EnumPins(out pinEnum);
DsError.ThrowExceptionForHR(hr);
IntPtr fetched = IntPtr.Zero;
IPin[] pins = { null };
/* Counter for how many pins successfully rendered */
int pinsRendered = 0;
/* Loop over each pin of the source filter */
while (pinEnum.Next(pins.Length, pins, fetched) == 0)
{
if (filterGraph.RenderEx(pins[0],
AMRenderExFlags.RenderToExistingRenderers,
IntPtr.Zero) >= 0)
pinsRendered++;
Marshal.ReleaseComObject(pins[0]);
}
Marshal.ReleaseComObject(pinEnum);
Marshal.ReleaseComObject(sourceFilter);
if (pinsRendered == 0)
throw new Exception("Could not render any streams from the source Uri");
/* Configure the graph in the base class */
SetupFilterGraph(m_graph);
HasVideo = true;
/* Sets the NaturalVideoWidth/Height */
//SetNativePixelSizes(renderer);
}
catch (Exception ex)
{
/* This exection will happen usually if the media does
* not exist or could not open due to not having the
* proper filters installed */
FreeResources();
/* Fire our failed event */
InvokeMediaFailed(new MediaFailedEventArgs(ex.Message, ex));
}
InvokeMediaOpened();
}
现在Directshow Filtergraph没有sourcefilter,我想添加一个WmAsfReader作为sourcefilter,但我不知道如何添加!
答案 0 :(得分:0)
...现在Directshow Filtergraph没有sourcefilter,我想添加一个WmAsfReader作为sourcefilter,但我不知道如何添加!
如果没有提及代码整体状况良好,则code snippet会回答您的问题:
hr = graphBuilder.AddSourceFilter(filename, "Source", out sourceFilter);
if (hr < 0)
{
//if it doesn't work before failing try to load it with the WMV reader
sourceFilter = (IBaseFilter)new WMAsfReader();
hr = graphBuilder.AddFilter(sourceFilter, "WM/ASF Reader");
DsError.ThrowExceptionForHR(hr);