private void StartCapture()
{
int hr;
ISampleGrabber sampGrabber = null;
IBaseFilter capFilter = null;
ICaptureGraphBuilder2 capGraph = null;
if (System.IO.File.Exists(txtAviFileName.Text))
{
// Get the graphbuilder object
m_FilterGraph = (IFilterGraph2) new FilterGraph();
m_mediaCtrl = m_FilterGraph as IMediaControl;
// Get the ICaptureGraphBuilder2
capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();
// Get the SampleGrabber interface
sampGrabber = (ISampleGrabber) new SampleGrabber();
// Start building the graph
hr = capGraph.SetFiltergraph(m_FilterGraph);
DsError.ThrowExceptionForHR(hr);
// Add the video source
hr = m_FilterGraph.AddSourceFilter(txtAviFileName.Text, "File Source (Async.)", out capFilter);
DsError.ThrowExceptionForHR(hr);
//add AVI Decompressor
IBaseFilter pAVIDecompressor = (IBaseFilter) new AVIDec();
hr = m_FilterGraph.AddFilter(pAVIDecompressor, "AVI Decompressor");
DsError.ThrowExceptionForHR(hr);
IBaseFilter ffdshow;
try
{
// Create Decoder filter COM object (ffdshow video decoder)
Type comtype = Type.GetTypeFromCLSID(new Guid("{04FE9017-F873-410E-871E-AB91661A4EF7}"));
if (comtype == null)
throw new NotSupportedException("Creating ffdshow video decoder COM object fails.");
object comobj = Activator.CreateInstance(comtype);
ffdshow = (IBaseFilter) comobj; // error ocurrs! raised exception
comobj = null;
}
catch
{
CustomMessageBox.Show("Please install/reinstall ffdshow");
return;
}
hr = m_FilterGraph.AddFilter(ffdshow, "ffdshow");
DsError.ThrowExceptionForHR(hr);
//
IBaseFilter baseGrabFlt = (IBaseFilter) sampGrabber;
ConfigureSampleGrabber(sampGrabber);
// Add the frame grabber to the graph
hr = m_FilterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber");
DsError.ThrowExceptionForHR(hr);
IBaseFilter vidrender = (IBaseFilter) new VideoRenderer();
hr = m_FilterGraph.AddFilter(vidrender, "Render");
DsError.ThrowExceptionForHR(hr);
IPin captpin = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0);
IPin ffdpinin = DsFindPin.ByName(ffdshow, "In");
IPin ffdpinout = DsFindPin.ByName(ffdshow, "Out");
IPin samppin = DsFindPin.ByName(baseGrabFlt, "Input");
hr = m_FilterGraph.Connect(captpin, ffdpinin);
DsError.ThrowExceptionForHR(hr);
hr = m_FilterGraph.Connect(ffdpinout, samppin);
DsError.ThrowExceptionForHR(hr);
FileWriter filewritter = new FileWriter();
IFileSinkFilter filemux = (IFileSinkFilter) filewritter;
//filemux.SetFileName("test.avi",);
//hr = capGraph.RenderStream(null, MediaType.Video, capFilter, null, vidrender);
// DsError.ThrowExceptionForHR(hr);
SaveSizeInfo(sampGrabber);
// setup buffer
if (m_handle == IntPtr.Zero)
m_handle = Marshal.AllocCoTaskMem(m_stride*m_videoHeight);
// tell the callback to ignore new images
m_PictureReady = new ManualResetEvent(false);
m_bGotOne = false;
m_bRunning = false;
timer1 = new Thread(timer);
timer1.IsBackground = true;
timer1.Start();
m_mediaextseek = m_FilterGraph as IAMExtendedSeeking;
m_mediapos = m_FilterGraph as IMediaPosition;
m_mediaseek = m_FilterGraph as IMediaSeeking;
double length = 0;
m_mediapos.get_Duration(out length);
trackBar_mediapos.Minimum = 0;
trackBar_mediapos.Maximum = (int) length;
Start();
}
else
{
MessageBox.Show("File does not exist");
}
}