private async void mLaunchButton_Click(object sender, RoutedEventArgs e) { if (mLaunchButton.Content.ToString() == "Stop") { lock (this) { mAccess = false; } if (mISession != null) { await mISession.closeSessionAsync(); } mLaunchButton.Content = "Launch"; mISession = null; return; } var lSourceNode = mSourcesComboBox.SelectedItem as XmlNode; if (lSourceNode == null) { return; } var lNode = lSourceNode.SelectSingleNode("Source.Attributes/Attribute[@Name='MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK']/SingleValue/@Value"); if (lNode == null) { return; } string lSymbolicLink = lNode.Value; lSourceNode = mStreamsComboBox.SelectedItem as XmlNode; if (lSourceNode == null) { return; } lNode = lSourceNode.SelectSingleNode("@Index"); if (lNode == null) { return; } uint lStreamIndex = 0; if (!uint.TryParse(lNode.Value, out lStreamIndex)) { return; } lSourceNode = mMediaTypesComboBox.SelectedItem as XmlNode; if (lSourceNode == null) { return; } lNode = lSourceNode.SelectSingleNode("@Index"); if (lNode == null) { return; } uint lMediaTypeIndex = 0; if (!uint.TryParse(lNode.Value, out lMediaTypeIndex)) { return; } lNode = lSourceNode.SelectSingleNode("MediaTypeItem[@Name='MF_MT_FRAME_SIZE']/Value.ValueParts/ValuePart[1]/@Value"); if (lNode == null) { return; } uint lVideoWidth = 0; if (!uint.TryParse(lNode.Value, out lVideoWidth)) { return; } lNode = lSourceNode.SelectSingleNode("MediaTypeItem[@Name='MF_MT_FRAME_SIZE']/Value.ValueParts/ValuePart[2]/@Value"); if (lNode == null) { return; } uint lVideoHeight = 0; if (!uint.TryParse(lNode.Value, out lVideoHeight)) { return; } int lWidthInBytes = await mCaptureManager.getStrideForBitmapInfoHeaderAsync( MFVideoFormat_RGB24, lVideoWidth); lsampleByteSize = (uint)Math.Abs(lWidthInBytes) * lVideoHeight; mData = new byte[lsampleByteSize]; var lSinkControl = await mCaptureManager.createSinkControlAsync(); string lxmldoc = await mCaptureManager.getCollectionOfSinksAsync(); XmlDocument doc = new XmlDocument(); doc.LoadXml(lxmldoc); var lSinkNode = doc.SelectSingleNode("SinkFactories/SinkFactory[@GUID='{3D64C48E-EDA4-4EE1-8436-58B64DD7CF13}']"); if (lSinkNode == null) { return; } var lContainerNode = lSinkNode.SelectSingleNode("Value.ValueParts/ValuePart[1]"); if (lContainerNode == null) { return; } setContainerFormat(lContainerNode); var lSinkFactory = await lSinkControl.createSampleGrabberCallbackSinkFactoryAsync(mReadMode); var lISampleGrabberCallbackAsync = await lSinkFactory.createOutputNodeAsync( MFMediaType_Video, MFVideoFormat_RGB24); if (lISampleGrabberCallbackAsync != null) { byte[] lData = new byte[lsampleByteSize]; lISampleGrabberCallbackAsync.mUpdateEvent += delegate (byte[] aData, uint aLength) { lock (this) { if (!mAccess) { return; } } Dispatcher.Invoke( DispatcherPriority.Normal, new Action(() => mDisplayImage.Source = FromArray(aData, lVideoWidth, lVideoHeight, mChannels))); }; var lSampleGrabberCallNode = lISampleGrabberCallbackAsync.getTopologyNode(); if (lSampleGrabberCallNode == null) { return; } var lSourceControl = await mCaptureManager.createSourceControlAsync(); if (lSourceControl == null) { return; } object lPtrSourceNode = await lSourceControl.createSourceNodeWithDownStreamConnectionAsync( lSymbolicLink, lStreamIndex, lMediaTypeIndex, lSampleGrabberCallNode); List <object> lSourceMediaNodeList = new List <object>(); lSourceMediaNodeList.Add(lPtrSourceNode); var lSessionControl = await mCaptureManager.createSessionControlAsync(); if (lSessionControl == null) { return; } mISession = await lSessionControl.createSessionAsync( lSourceMediaNodeList.ToArray()); if (mISession == null) { return; } await mISession.startSessionAsync(0, Guid.Empty); mLaunchButton.Content = "Stop"; mWebCamControl = await lSourceControl.createWebCamControlAsync(lSymbolicLink); if (mWebCamControl != null) { string lXMLstring = await mWebCamControl.getCamParametrsAsync(); XmlDataProvider lXmlDataProvider = (XmlDataProvider)this.Resources["XmlWebCamParametrsProvider"]; if (lXmlDataProvider == null) { return; } System.Xml.XmlDocument ldoc = new System.Xml.XmlDocument(); ldoc.LoadXml(lXMLstring); lXmlDataProvider.Document = ldoc; } lock (this) { mAccess = true; } } }
private async void mLaunchButton_Click(object sender, RoutedEventArgs e) { if (mLaunchButton.Content.ToString() == "Stop") { mTimer.Stop(); if (mISession != null) { await mISession.closeSessionAsync(); } mLaunchButton.Content = "Launch"; if (mISampleGrabberCall != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(mISampleGrabberCall.getTopologyNode()); } return; } var lSourceNode = mSourcesComboBox.SelectedItem as XmlNode; if (lSourceNode == null) { return; } var lNode = lSourceNode.SelectSingleNode("Source.Attributes/Attribute[@Name='MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK']/SingleValue/@Value"); if (lNode == null) { return; } string lSymbolicLink = lNode.Value; lSourceNode = mStreamsComboBox.SelectedItem as XmlNode; if (lSourceNode == null) { return; } lNode = lSourceNode.SelectSingleNode("@Index"); if (lNode == null) { return; } uint lStreamIndex = 0; if (!uint.TryParse(lNode.Value, out lStreamIndex)) { return; } lSourceNode = mMediaTypesComboBox.SelectedItem as XmlNode; if (lSourceNode == null) { return; } lNode = lSourceNode.SelectSingleNode("@Index"); if (lNode == null) { return; } uint lMediaTypeIndex = 0; if (!uint.TryParse(lNode.Value, out lMediaTypeIndex)) { return; } lNode = lSourceNode.SelectSingleNode("MediaTypeItem[@Name='MF_MT_FRAME_SIZE']/Value.ValueParts/ValuePart[1]/@Value"); if (lNode == null) { return; } if (!uint.TryParse(lNode.Value, out mVideoWidth)) { return; } lNode = lSourceNode.SelectSingleNode("MediaTypeItem[@Name='MF_MT_FRAME_SIZE']/Value.ValueParts/ValuePart[2]/@Value"); if (lNode == null) { return; } if (!uint.TryParse(lNode.Value, out mVideoHeight)) { return; } int lWidthInBytes = await mCaptureManager.getStrideForBitmapInfoHeaderAsync( MFVideoFormat_RGB32, mVideoWidth); lsampleByteSize = (uint)Math.Abs(lWidthInBytes) * mVideoHeight; mData = new byte[lsampleByteSize]; var lSinkControl = await mCaptureManager.createSinkControlAsync(); string lxmldoc = await mCaptureManager.getCollectionOfSinksAsync(); XmlDocument doc = new XmlDocument(); doc.LoadXml(lxmldoc); var lSinkNode = doc.SelectSingleNode("SinkFactories/SinkFactory[@GUID='{759D24FF-C5D6-4B65-8DDF-8A2B2BECDE39}']"); if (lSinkNode == null) { return; } var lContainerNode = lSinkNode.SelectSingleNode("Value.ValueParts/ValuePart[1]"); if (lContainerNode == null) { return; } setContainerFormat(lContainerNode); mSinkFactory = await lSinkControl.createSampleGrabberCallSinkFactoryAsync(mReadMode); mISampleGrabberCall = await mSinkFactory.createOutputNodeAsync( MFMediaType_Video, MFVideoFormat_RGB32, lsampleByteSize); if (mISampleGrabberCall != null) { var lSampleGrabberCallNode = mISampleGrabberCall.getTopologyNode(); if (lSampleGrabberCallNode == null) { return; } object lPtrSourceNode; var lSourceControl = await mCaptureManager.createSourceControlAsync(); if (lSourceControl == null) { return; } lPtrSourceNode = await lSourceControl.createSourceNodeWithDownStreamConnectionAsync( lSymbolicLink, lStreamIndex, lMediaTypeIndex, lSampleGrabberCallNode); List <object> lSourceMediaNodeList = new List <object>(); lSourceMediaNodeList.Add(lPtrSourceNode); var lSessionControl = await mCaptureManager.createSessionControlAsync(); if (lSessionControl == null) { return; } mISession = await lSessionControl.createSessionAsync( lSourceMediaNodeList.ToArray()); if (mISession == null) { return; } await mISession.startSessionAsync(0, Guid.Empty); mLaunchButton.Content = "Stop"; mWebCamControl = await lSourceControl.createWebCamControlAsync(lSymbolicLink); if (mWebCamControl != null) { string lXMLstring = await mWebCamControl.getCamParametrsAsync(); XmlDataProvider lXmlDataProvider = (XmlDataProvider)this.Resources["XmlWebCamParametrsProvider"]; if (lXmlDataProvider == null) { return; } System.Xml.XmlDocument ldoc = new System.Xml.XmlDocument(); if (!string.IsNullOrWhiteSpace(lXMLstring)) { ldoc.LoadXml(lXMLstring); } lXmlDataProvider.Document = ldoc; } mTimer.Start(); } }