protected void RenderGraph()
{
Guid cat;
Guid med;
int hr;
bool didSomething = false;
const int WS_CHILD = 0x40000000;
const int WS_CLIPCHILDREN = 0x02000000;
const int WS_CLIPSIBLINGS = 0x04000000;
// Stop the graph
if (MediaControl != null)
MediaControl.Stop();
// Create the graph if needed (group should already be created)
CreateGraph();
// Derender the graph if we have a capture or preview stream
// that we no longer want. We can't derender the capture and
// preview streams seperately.
// Notice the second case will leave a capture stream intact
// even if we no longer want it. This allows the user that is
// not using the preview to Stop() and Start() without
// rerendering the graph.
if (!WantPreviewRendered && IsPreviewRendered)
DerenderGraph();
// Render preview stream (only if necessary)
if (WantPreviewRendered && !IsPreviewRendered)
{
// Render preview (video -> renderer)
cat = Uuid.PinCategory.Preview;
med = Uuid.MediaType.Video;
hr = CaptureGraphBuilder.RenderStream(ref cat, ref med, VideoDeviceFilter, _baseGrabFlt, null);
if (hr < 0) Marshal.ThrowExceptionForHR(hr);
// Get the IVideoWindow interface
VideoWindow = (ControlStreaming.IVideoWindow)GraphBuilder;
// Set the video window to be a child of the main window
var source = PresentationSource.FromVisual(PreviewWindow) as HwndSource;
hr = VideoWindow.put_Owner(source.Handle);
if (hr < 0) Marshal.ThrowExceptionForHR(hr);
// Set video window style
hr = VideoWindow.put_WindowStyle(WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
if (hr < 0) Marshal.ThrowExceptionForHR(hr);
// Position video window in client rect of owner window
PreviewWindow.SizeChanged += OnPreviewWindowResize;
OnPreviewWindowResize(this, null);
// Make the video window visible, now that it is properly positioned
hr = VideoWindow.put_Visible(CoreStreaming.DsHlp.OATRUE);
if (hr < 0) Marshal.ThrowExceptionForHR(hr);
IsPreviewRendered = true;
didSomething = true;
var media = new CoreStreaming.AMMediaType();
hr = SampGrabber.GetConnectedMediaType(media);
if (hr < 0)
Marshal.ThrowExceptionForHR(hr);
if ((media.formatType != Uuid.FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero))
throw new NotSupportedException("Unknown Grabber Media Format");
_videoInfoHeader = (EditStreaming.VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(EditStreaming.VideoInfoHeader));
Marshal.FreeCoTaskMem(media.formatPtr); media.formatPtr = IntPtr.Zero;
}
if (didSomething)
ActualGraphState = GraphState.Rendered;
}