private void AddAndConnectWDMBoardFilters()
{
int hr = 0;
DsDevice[] devices;
Guid iid = typeof(IBaseFilter).GUID;
this.captureGraphBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
captureGraphBuilder.SetFiltergraph(this.graphBuilder);
try
{
if (this.VideoCaptureDevice != null)
{
IBaseFilter tmp;
object o;
this.VideoCaptureDevice.Mon.BindToObject(null, null, ref iid, out o);
tmp = o as IBaseFilter;
//MessageBox.Show(this.VideoInputDevice.DevicePath);
//Add the Video input device to the graph
hr = graphBuilder.AddFilter(tmp, this.VideoCaptureDevice.Name);
this.videoCaptureFilter = tmp;
SetCaptureResolution(this.captureFormat); // this.captureResolution);
this.videoCaptureFilter = null;
//Render any preview pin of the device
//int hr1 = captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, tmp, null, this.videoRenderer);
int hr1 = captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, tmp, null, this.videoRenderer);
if (hr >= 0 && hr1 >= 0)
{
// Got it !
this.videoCaptureFilter = tmp;
}
else
{
// Try another...
int hr2 = graphBuilder.RemoveFilter(tmp);
Marshal.ReleaseComObject(tmp);
if (hr >= 0 && hr1 < 0)
DsError.ThrowExceptionForHR(hr1);
DsError.ThrowExceptionForHR(hr);
return;
}
}
else
{
// Enumerate WDM Source filters category and found one that can connect to the network provider
devices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
for (int i = 0; i < devices.Length; i++)
{
IBaseFilter tmp;
object o;
devices[i].Mon.BindToObject(null, null, ref iid, out o);
tmp = o as IBaseFilter;
//Add the Video input device to the graph
hr = graphBuilder.AddFilter(tmp, devices[i].Name);
//Render any preview pin of the device
//int hr1 = captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, tmp, null, this.videoRenderer);
int hr1 = captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, tmp, null, this.videoRenderer);
if (hr >= 0 && hr1 >= 0)
{
// Got it !
this.videoCaptureFilter = tmp;
break;
}
else
{
// Try another...
hr = graphBuilder.RemoveFilter(tmp);
Marshal.ReleaseComObject(tmp);
}
}
}
// Assume we found a video filter...
AddAndConnectNullRendererForWPF();
//if (useWPF)
//{
// IPin pinOutFromFilterOut = DsFindPin.ByDirection(this.videoRenderer, PinDirection.Output, 0);
// if (pinOutFromFilterOut != null)
// {
// hr = this.graphBuilder.Render(pinOutFromFilterOut);
// Marshal.ReleaseComObject(pinOutFromFilterOut);
// }
//}
if (this.AudioCaptureDevice != null)
{
if (this.VideoCaptureDevice != null && this.videoCaptureFilter != null && this.AudioCaptureDevice.DevicePath == this.VideoCaptureDevice.DevicePath)
{
//Render any preview pin of the device
int hr1 = captureGraphBuilder.RenderStream(null, MediaType.Audio, this.videoCaptureFilter, null, this.audioRenderer);
DsError.ThrowExceptionForHR(hr);
if (hr1 >= 0)
{
// Got it !
this.audioCaptureFilter = null; // this.videoCaptureFilter;
}
else
{
// No audio?
}
}
else
{
IBaseFilter tmp;
object o;
this.AudioCaptureDevice.Mon.BindToObject(null, null, ref iid, out o);
tmp = o as IBaseFilter;
//Add the audio input device to the graph
hr = graphBuilder.AddFilter(tmp, this.AudioCaptureDevice.Name);
DsError.ThrowExceptionForHR(hr);
//Render any preview pin of the device
int hr1 = captureGraphBuilder.RenderStream(null, MediaType.Audio, tmp, null, this.audioRenderer);
DsError.ThrowExceptionForHR(hr);
if (hr >= 0 && hr1 >= 0)
{
// Got it !
this.audioCaptureFilter = tmp;
}
else
{
// Try another...
int hr2 = graphBuilder.RemoveFilter(tmp);
Marshal.ReleaseComObject(tmp);
DsError.ThrowExceptionForHR(hr);
return;
}
}
}
else
{
// Then enumerate WDM AudioInputDevice category
//devices = DsDevice.GetDevicesOfCat(FilterCategory.AudioInputDevice);
devices = DsDevice.GetDevicesOfCat(FilterCategory.AMKSCapture);
for (int i = 0; i < devices.Length; i++)
{
IBaseFilter tmp;
object o;
devices[i].Mon.BindToObject(null, null, ref iid, out o);
tmp = o as IBaseFilter;
//Add the audio input device to the graph
hr = graphBuilder.AddFilter(tmp, devices[i].Name);
DsError.ThrowExceptionForHR(hr);
//Render any preview pin of the device
int hr1 = captureGraphBuilder.RenderStream(null, MediaType.Audio, tmp, null, this.audioRenderer);
if (hr >= 0 && hr1 >= 0)
{
// Got it !
this.audioCaptureFilter = tmp;
break;
}
else
{
// Try another...
hr = graphBuilder.RemoveFilter(tmp);
Marshal.ReleaseComObject(tmp);
}
}
}
}
catch (Exception ex)
{
Trace.WriteLineIf(trace.TraceError, ex.ToString());
}
finally
{
}
}