Connect mechanism to Build the graph
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using DirectShowLib;
namespace AddRendererFilter
{
class AddRendererFilter
{
private IGraphBuilder m_pGraphBuilder = null;
private IMediaControl m_pMediaControl = null;
private IMediaEventEx m_pMediaEventEx = null;
private IBaseFilter m_pSourceFilter = null;
private IBaseFilter m_pRendererFilter = null;
public AddRendererFilter()
{
m_pGraphBuilder = new FilterGraph() as IGraphBuilder;
}
~AddRendererFilter()
{
ReleaseGraph();
}
private void ReleaseGraph()
{
if (m_pGraphBuilder != null)
{
Marshal.ReleaseComObject(m_pGraphBuilder);
m_pGraphBuilder = null;
}
GC.Collect();
}
public void Render()
{
int hr = 0;
IPin pOutputPin = null;
IPin pInputPin = null;
hr = m_pGraphBuilder.AddSourceFilter("D:\\hands.avi", "Source Filter", out m_pSourceFilter);
DsError.ThrowExceptionForHR(hr);
hr = AddFilterByCLSID(ref m_pGraphBuilder, typeof(VideoRenderer), "Renderer Filter", out m_pRendererFilter);
DsError.ThrowExceptionForHR(hr);
pOutputPin = DsFindPin.ByDirection(m_pSourceFilter, PinDirection.Output, 0);
pInputPin = DsFindPin.ByDirection(m_pRendererFilter, PinDirection.Input, 0);
hr = m_pGraphBuilder.Connect(pOutputPin, pInputPin);
DsError.ThrowExceptionForHR(hr);
m_pMediaControl = m_pGraphBuilder as IMediaControl;
m_pMediaControl.Run();
}
private int AddFilterByCLSID(ref IGraphBuilder pGraph,
Type comType,
string FilterName,
out IBaseFilter pFilter
)
{
Object comObject = null;
int hr = 0;
try
{
comObject = Activator.CreateInstance(comType);
if (comObject == null)
{
throw new NotSupportedException("\n Invalid Class Id Type");
}
pFilter = comObject as IBaseFilter;
hr = m_pGraphBuilder.AddFilter(pFilter, FilterName);
DsError.ThrowExceptionForHR(hr);
return hr;
}
catch (Exception ex)
{
pFilter = null;
return -1;
}
}
}
}
client Usage :
---------------------
AddRendererFilter m_pAddRendererFilter = null;
m_pAddRendererFilter = new AddRendererFilter();
m_pAddRendererFilter.Render();
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using DirectShowLib;
namespace AddRendererFilter
{
class AddRendererFilter
{
private IGraphBuilder m_pGraphBuilder = null;
private IMediaControl m_pMediaControl = null;
private IMediaEventEx m_pMediaEventEx = null;
private IBaseFilter m_pSourceFilter = null;
private IBaseFilter m_pRendererFilter = null;
public AddRendererFilter()
{
m_pGraphBuilder = new FilterGraph() as IGraphBuilder;
}
~AddRendererFilter()
{
ReleaseGraph();
}
private void ReleaseGraph()
{
if (m_pGraphBuilder != null)
{
Marshal.ReleaseComObject(m_pGraphBuilder);
m_pGraphBuilder = null;
}
GC.Collect();
}
public void Render()
{
int hr = 0;
IPin pOutputPin = null;
IPin pInputPin = null;
hr = m_pGraphBuilder.AddSourceFilter("D:\\hands.avi", "Source Filter", out m_pSourceFilter);
DsError.ThrowExceptionForHR(hr);
hr = AddFilterByCLSID(ref m_pGraphBuilder, typeof(VideoRenderer), "Renderer Filter", out m_pRendererFilter);
DsError.ThrowExceptionForHR(hr);
pOutputPin = DsFindPin.ByDirection(m_pSourceFilter, PinDirection.Output, 0);
pInputPin = DsFindPin.ByDirection(m_pRendererFilter, PinDirection.Input, 0);
hr = m_pGraphBuilder.Connect(pOutputPin, pInputPin);
DsError.ThrowExceptionForHR(hr);
m_pMediaControl = m_pGraphBuilder as IMediaControl;
m_pMediaControl.Run();
}
private int AddFilterByCLSID(ref IGraphBuilder pGraph,
Type comType,
string FilterName,
out IBaseFilter pFilter
)
{
Object comObject = null;
int hr = 0;
try
{
comObject = Activator.CreateInstance(comType);
if (comObject == null)
{
throw new NotSupportedException("\n Invalid Class Id Type");
}
pFilter = comObject as IBaseFilter;
hr = m_pGraphBuilder.AddFilter(pFilter, FilterName);
DsError.ThrowExceptionForHR(hr);
return hr;
}
catch (Exception ex)
{
pFilter = null;
return -1;
}
}
}
}
client Usage :
---------------------
AddRendererFilter m_pAddRendererFilter = null;
m_pAddRendererFilter = new AddRendererFilter();
m_pAddRendererFilter.Render();
Labels: C# with Directshow
0 Comments:
Post a Comment
<< Home