// Set type of input connected to video output of the crossbar private void SetCurrentCrossbarInput( IAMCrossbar crossbar, VideoInput videoInput ) { if ( videoInput.Type != PhysicalConnectorType.Default ) { int inPinsCount, outPinsCount; // gen number of pins in the crossbar if ( crossbar.get_PinCounts( out outPinsCount, out inPinsCount ) == 0 ) { int videoOutputPinIndex = -1; int videoInputPinIndex = -1; int pinIndexRelated; PhysicalConnectorType type; // find index of the video output pin for ( int i = 0; i < outPinsCount; i++ ) { if ( crossbar.get_CrossbarPinInfo( false, i, out pinIndexRelated, out type ) != 0 ) continue; if ( type == PhysicalConnectorType.VideoDecoder ) { videoOutputPinIndex = i; break; } } // find index of the required input pin for ( int i = 0; i < inPinsCount; i++ ) { if ( crossbar.get_CrossbarPinInfo( true, i, out pinIndexRelated, out type ) != 0 ) continue; if ( ( type == videoInput.Type ) && ( i == videoInput.Index ) ) { videoInputPinIndex = i; break; } } // try connecting pins if ( ( videoInputPinIndex != -1 ) && ( videoOutputPinIndex != -1 ) && ( crossbar.CanRoute( videoOutputPinIndex, videoInputPinIndex ) == 0 ) ) { crossbar.Route( videoOutputPinIndex, videoInputPinIndex ); } } } }
// Get type of input connected to video output of the crossbar private VideoInput GetCurrentCrossbarInput( IAMCrossbar crossbar ) { VideoInput videoInput = VideoInput.Default; int inPinsCount, outPinsCount; // gen number of pins in the crossbar if ( crossbar.get_PinCounts( out outPinsCount, out inPinsCount ) == 0 ) { int videoOutputPinIndex = -1; int pinIndexRelated; // find index of the video output pin for ( int i = 0; i < outPinsCount; i++ ) { PhysicalConnectorType type; if ( crossbar.get_CrossbarPinInfo( false, i, out pinIndexRelated, out type ) != 0 ) continue; if ( type == PhysicalConnectorType.VideoDecoder ) { videoOutputPinIndex = i; break; } } if ( videoOutputPinIndex != -1 ) { int videoInputPinIndex; // get index of the input pin connected to the output if ( crossbar.get_IsRoutedTo( videoOutputPinIndex, out videoInputPinIndex ) == 0 ) { PhysicalConnectorType inputType; crossbar.get_CrossbarPinInfo( true, videoInputPinIndex, out pinIndexRelated, out inputType ); videoInput = new VideoInput( videoInputPinIndex, inputType ); } } } return videoInput; }
private void WorkerThread( bool runGraph ) { var res = ReasonToFinishPlaying.StoppedByUser; bool isSnapshotSupported = false; // grabber var videoGrabber = new Grabber( this, false ); var snapshotGrabber = new Grabber( this, true ); // objects object captureGraphObject = null; object graphObject = null; object videoGrabberObject = null; object snapshotGrabberObject = null; object crossbarObject = null; // interfaces IAMVideoControl videoControl = null; IPin pinStillImage = null; IAMCrossbar crossbar = null; try { // get type of capture graph builder Type type = Type.GetTypeFromCLSID( Clsid.CaptureGraphBuilder2 ); if ( type == null ) throw new ApplicationException( "Failed creating capture graph builder" ); // create capture graph builder captureGraphObject = Activator.CreateInstance( type ); var captureGraph = (ICaptureGraphBuilder2) captureGraphObject; // get type of filter graph type = Type.GetTypeFromCLSID( Clsid.FilterGraph ); if ( type == null ) throw new ApplicationException( "Failed creating filter graph" ); // create filter graph graphObject = Activator.CreateInstance( type ); var graph = (IFilterGraph2) graphObject; // set filter graph to the capture graph builder captureGraph.SetFiltergraph( graph ); // create source device's object _sourceObject = FilterInfo.CreateFilter( _deviceMoniker ); if ( _sourceObject == null ) throw new ApplicationException( "Failed creating device object for moniker" ); // get base filter interface of source device var sourceBase = (IBaseFilter) _sourceObject; // get video control interface of the device try { videoControl = (IAMVideoControl) _sourceObject; } catch(InvalidCastException) { // some camera drivers may not support IAMVideoControl interface } // get type of sample grabber type = Type.GetTypeFromCLSID( Clsid.SampleGrabber ); if ( type == null ) throw new ApplicationException( "Failed creating sample grabber" ); // create sample grabber used for video capture videoGrabberObject = Activator.CreateInstance( type ); var videoSampleGrabber = (ISampleGrabber) videoGrabberObject; var videoGrabberBase = (IBaseFilter) videoGrabberObject; // create sample grabber used for snapshot capture snapshotGrabberObject = Activator.CreateInstance( type ); var snapshotSampleGrabber = (ISampleGrabber) snapshotGrabberObject; var snapshotGrabberBase = (IBaseFilter) snapshotGrabberObject; // add source and grabber filters to graph graph.AddFilter( sourceBase, "source" ); graph.AddFilter( videoGrabberBase, "grabber_video" ); graph.AddFilter( snapshotGrabberBase, "grabber_snapshot" ); // set media type var mediaType = new AMMediaType {MajorType = MediaType.Video, SubType = MediaSubType.RGB24}; videoSampleGrabber.SetMediaType( mediaType ); snapshotSampleGrabber.SetMediaType( mediaType ); // get crossbar object to to allows configuring pins of capture card captureGraph.FindInterface( FindDirection.UpstreamOnly, Guid.Empty, sourceBase, typeof( IAMCrossbar ).GUID, out crossbarObject ); if ( crossbarObject != null ) { crossbar = (IAMCrossbar) crossbarObject; } _isCrossbarAvailable = ( crossbar != null ); _crossbarVideoInputs = ColletCrossbarVideoInputs( crossbar ); if ( videoControl != null ) { // find Still Image output pin of the vedio device captureGraph.FindPin( _sourceObject, PinDirection.Output, PinCategory.StillImage, MediaType.Video, false, 0, out pinStillImage ); // check if it support trigger mode if ( pinStillImage != null ) { VideoControlFlags caps; videoControl.GetCaps( pinStillImage, out caps ); isSnapshotSupported = ( ( caps & VideoControlFlags.ExternalTriggerEnable ) != 0 ); } } // configure video sample grabber videoSampleGrabber.SetBufferSamples( false ); videoSampleGrabber.SetOneShot( false ); videoSampleGrabber.SetCallback( videoGrabber, 1 ); // configure snapshot sample grabber snapshotSampleGrabber.SetBufferSamples( true ); snapshotSampleGrabber.SetOneShot( false ); snapshotSampleGrabber.SetCallback( snapshotGrabber, 1 ); // configure pins GetPinCapabilitiesAndConfigureSizeAndRate( captureGraph, sourceBase, PinCategory.Capture, _videoResolution, ref _videoCapabilities ); if ( isSnapshotSupported ) { GetPinCapabilitiesAndConfigureSizeAndRate( captureGraph, sourceBase, PinCategory.StillImage, _snapshotResolution, ref _snapshotCapabilities ); } else { _snapshotCapabilities = new VideoCapabilities[0]; } // put video/snapshot capabilities into cache lock ( CacheVideoCapabilities ) { if ( ( _videoCapabilities != null ) && ( !CacheVideoCapabilities.ContainsKey( _deviceMoniker ) ) ) { CacheVideoCapabilities.Add( _deviceMoniker, _videoCapabilities ); } } lock ( CacheSnapshotCapabilities ) { if ( ( _snapshotCapabilities != null ) && ( !CacheSnapshotCapabilities.ContainsKey( _deviceMoniker ) ) ) { CacheSnapshotCapabilities.Add( _deviceMoniker, _snapshotCapabilities ); } } if ( runGraph ) { // render capture pin captureGraph.RenderStream( PinCategory.Capture, MediaType.Video, sourceBase, null, videoGrabberBase ); if ( videoSampleGrabber.GetConnectedMediaType( mediaType ) == 0 ) { var vih = (VideoInfoHeader) Marshal.PtrToStructure( mediaType.FormatPtr, typeof( VideoInfoHeader ) ); videoGrabber.Width = vih.BmiHeader.Width; videoGrabber.Height = vih.BmiHeader.Height; mediaType.Dispose( ); } else { if ((isSnapshotSupported) && (_provideSnapshots)) { // render snapshot pin captureGraph.RenderStream(PinCategory.StillImage, MediaType.Video, sourceBase, null, snapshotGrabberBase); if (snapshotSampleGrabber.GetConnectedMediaType(mediaType) == 0) { var vih = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.FormatPtr, typeof(VideoInfoHeader)); snapshotGrabber.Width = vih.BmiHeader.Width; snapshotGrabber.Height = vih.BmiHeader.Height; mediaType.Dispose(); } } } // get media control var mediaControl = (IMediaControl) graphObject; // get media events' interface var mediaEvent = (IMediaEventEx) graphObject; // run mediaControl.Run( ); if ( ( isSnapshotSupported ) && ( _provideSnapshots ) ) { _startTime = DateTime.Now; videoControl.SetMode( pinStillImage, VideoControlFlags.ExternalTriggerEnable ); } do { if ( mediaEvent != null ) { IntPtr p1; IntPtr p2; DsEvCode code; if ( mediaEvent.GetEvent( out code, out p1, out p2, 0 ) >= 0 ) { mediaEvent.FreeEventParams( code, p1, p2 ); if ( code == DsEvCode.DeviceLost ) { res = ReasonToFinishPlaying.DeviceLost; break; } } } if ( _needToSetVideoInput ) { _needToSetVideoInput = false; // set/check current input type of a video card (frame grabber) if ( _isCrossbarAvailable.Value ) { SetCurrentCrossbarInput( crossbar, _crossbarVideoInput ); _crossbarVideoInput = GetCurrentCrossbarInput( crossbar ); } } if ( _needToSimulateTrigger ) { _needToSimulateTrigger = false; if ( ( isSnapshotSupported ) && ( _provideSnapshots ) ) { videoControl.SetMode( pinStillImage, VideoControlFlags.Trigger ); } } if ( _needToDisplayPropertyPage ) { _needToDisplayPropertyPage = false; DisplayPropertyPage( _parentWindowForPropertyPage, _sourceObject ); if ( crossbar != null ) { _crossbarVideoInput = GetCurrentCrossbarInput( crossbar ); } } if ( _needToDisplayCrossBarPropertyPage ) { _needToDisplayCrossBarPropertyPage = false; if ( crossbar != null ) { DisplayPropertyPage( _parentWindowForPropertyPage, crossbar ); _crossbarVideoInput = GetCurrentCrossbarInput( crossbar ); } } } while ( !_stopEvent.WaitOne( 100, false ) ); mediaControl.Stop( ); } } catch ( Exception ex ) { Logger.LogExceptionToFile(ex,"Device"); // provide information to clients res = ReasonToFinishPlaying.DeviceLost; } finally { // release all objects if ( graphObject != null ) { Marshal.FinalReleaseComObject( graphObject ); } if ( _sourceObject != null ) { Marshal.FinalReleaseComObject( _sourceObject ); _sourceObject = null; } if ( videoGrabberObject != null ) { Marshal.FinalReleaseComObject( videoGrabberObject ); } if ( snapshotGrabberObject != null ) { Marshal.FinalReleaseComObject( snapshotGrabberObject ); } if ( captureGraphObject != null ) { Marshal.FinalReleaseComObject( captureGraphObject ); } if ( crossbarObject != null ) { Marshal.FinalReleaseComObject( crossbarObject ); } if (_stopEvent != null) { _stopEvent?.Close(); _stopEvent = null; } } PlayingFinished?.Invoke(this, new PlayingFinishedEventArgs(res)); }
// Collect all video inputs of the specified crossbar private VideoInput[] ColletCrossbarVideoInputs( IAMCrossbar crossbar ) { lock ( CacheCrossbarVideoInputs ) { if ( CacheCrossbarVideoInputs.ContainsKey( _deviceMoniker ) ) { return CacheCrossbarVideoInputs[_deviceMoniker]; } var videoInputsList = new List<VideoInput>( ); if ( crossbar != null ) { int inPinsCount, outPinsCount; // gen number of pins in the crossbar if ( crossbar.get_PinCounts( out outPinsCount, out inPinsCount ) == 0 ) { // collect all video inputs for ( int i = 0; i < inPinsCount; i++ ) { int pinIndexRelated; PhysicalConnectorType type; if ( crossbar.get_CrossbarPinInfo( true, i, out pinIndexRelated, out type ) != 0 ) continue; if ( type < PhysicalConnectorType.AudioTuner ) { videoInputsList.Add( new VideoInput( i, type ) ); } } } } var videoInputs = new VideoInput[videoInputsList.Count]; videoInputsList.CopyTo( videoInputs ); CacheCrossbarVideoInputs.Add( _deviceMoniker, videoInputs ); return videoInputs; } }
private void VideoSourceLoad(object sender, EventArgs e) { UISync.Init(this); tlpVLC.Enabled = VlcHelper.VlcInstalled; linkLabel3.Visible = !tlpVLC.Enabled; cmbJPEGURL.Text = MainForm.Conf.JPEGURL; cmbMJPEGURL.Text = MainForm.Conf.MJPEGURL; cmbVLCURL.Text = MainForm.Conf.VLCURL; cmbFile.Text = MainForm.Conf.AVIFileName; ConfigureSnapshots = true; txtOnvifUsername.Text = txtLogin.Text = txtLogin2.Text = CameraControl.Camobject.settings.login; txtOnvifPassword.Text = txtPassword.Text = txtPassword2.Text = CameraControl.Camobject.settings.password; VideoSourceString = CameraControl.Camobject.settings.videosourcestring; SourceIndex = CameraControl.Camobject.settings.sourceindex; if (SourceIndex == 3) { VideoDeviceMoniker = VideoSourceString; string[] wh= CameraControl.Camobject.resolution.Split('x'); CaptureSize = new Size(Convert.ToInt32(wh[0]), Convert.ToInt32(wh[1])); } txtFrameInterval.Text = txtFrameInterval2.Text = CameraControl.Camobject.settings.frameinterval.ToString(CultureInfo.InvariantCulture); txtVLCArgs.Text = CameraControl.Camobject.settings.vlcargs.Replace("\r\n","\n").Replace("\n\n","\n").Replace("\n", Environment.NewLine); foreach (var cam in MainForm.Cameras) { if (cam.id != CameraControl.Camobject.id && cam.settings.sourceindex!=10) //dont allow a clone of a clone as the events get too complicated (and also it's pointless) ddlCloneCamera.Items.Add(new MainForm.ListItem2(cam.name, cam.id)); } ddlCustomProvider.SelectedIndex = 0; switch (SourceIndex) { case 0: cmbJPEGURL.Text = VideoSourceString; txtFrameInterval.Text = CameraControl.Camobject.settings.frameinterval.ToString(CultureInfo.InvariantCulture); break; case 1: cmbMJPEGURL.Text = VideoSourceString; break; case 2: cmbFile.Text = VideoSourceString; break; case 3: chkAutoImageSettings.Checked = NV("manual") != "true"; break; case 5: cmbVLCURL.Text = VideoSourceString; break; case 8: txtCustomURL.Text = VideoSourceString; switch (NV("custom")) { default: ddlCustomProvider.SelectedIndex = 0; break; } break; case 10: int id; if (Int32.TryParse(VideoSourceString, out id)) { foreach (MainForm.ListItem2 li in ddlCloneCamera.Items) { if (li.Value == id) { ddlCloneCamera.SelectedItem = li; break; } } } break; } if (!string.IsNullOrEmpty(CameraControl.Camobject.decodekey)) txtDecodeKey.Text = CameraControl.Camobject.decodekey; chkMousePointer.Checked = CameraControl.Camobject.settings.desktopmouse; numBorderTimeout.Value = CameraControl.Camobject.settings.bordertimeout; cmbJPEGURL.Items.AddRange(ObjectList(MainForm.Conf.RecentJPGList)); cmbMJPEGURL.Items.AddRange(ObjectList(MainForm.Conf.RecentMJPGList)); cmbFile.Items.AddRange(ObjectList(MainForm.Conf.RecentFileList)); cmbVLCURL.Items.AddRange(ObjectList(MainForm.Conf.RecentVLCList)); numAnalyseDuration.Value = CameraControl.Camobject.settings.analyseduration; int selectedCameraIndex = 0; for (int i = 0; i < _videoDevices.Count; i++) { if (_videoDeviceMoniker == _videoDevices[i].MonikerString) { selectedCameraIndex = i; break; } } devicesCombo.SelectedIndex = selectedCameraIndex; ddlScreen.SuspendLayout(); foreach (Screen s in Screen.AllScreens) { ddlScreen.Items.Add(s.DeviceName); } ddlScreen.Items.Insert(0, LocRm.GetString("PleaseSelect")); if (SourceIndex == 4) { int screenIndex = Convert.ToInt32(VideoSourceString) + 1; ddlScreen.SelectedIndex = ddlScreen.Items.Count>screenIndex ? screenIndex : 1; } else ddlScreen.SelectedIndex = 0; ddlScreen.ResumeLayout(); SetSourceIndex(SourceIndex); if (CameraControl?.Camera?.VideoSource is VideoCaptureDevice) { _videoCaptureDevice = (VideoCaptureDevice)CameraControl.Camera.VideoSource; _videoInput = _videoCaptureDevice.CrossbarVideoInput; EnumeratedSupportedFrameSizes(); } //ximea int deviceCount = 0; try { deviceCount = XimeaCamera.CamerasCount; } catch(Exception) { //Ximea DLL not installed //Logger.LogMessageToFile("This is not a XIMEA device"); } pnlXimea.Enabled = deviceCount>0; if (pnlXimea.Enabled) { for (int i = 0; i < deviceCount; i++) { ddlXimeaDevice.Items.Add("Device " + i); } if (NV("type")=="ximea") { int deviceIndex = Convert.ToInt32(NV("device")); ddlXimeaDevice.SelectedIndex = ddlXimeaDevice.Items.Count > deviceIndex?deviceIndex:0; numXimeaWidth.Text = NV("width"); numXimeaHeight.Text = NV("height"); numXimeaOffsetX.Value = Convert.ToInt32(NV("x")); numXimeaOffestY.Value = Convert.ToInt32(NV("y")); decimal gain; decimal.TryParse(NV("gain"), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out gain); numXimeaGain.Value = gain; decimal exp; decimal.TryParse(NV("exposure"), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out exp); if (exp == 0) exp = 100; numXimeaExposure.Value = exp; combo_dwnsmpl.SelectedItem = NV("downsampling"); } } else { ddlXimeaDevice.Items.Add(LocRm.GetString("NoDevicesFound")); ddlXimeaDevice.SelectedIndex = 0; } deviceCount = 0; try { foreach (var potentialSensor in KinectSensor.KinectSensors) { if (potentialSensor.Status == KinectStatus.Connected) { deviceCount++; ddlKinectDevice.Items.Add(potentialSensor.UniqueKinectId); if (NV("type") == "kinect") { if (NV("UniqueKinectId") == potentialSensor.UniqueKinectId) { ddlKinectDevice.SelectedIndex = ddlKinectDevice.Items.Count - 1; } } } } } catch (Exception) { //Type error if not installed Logger.LogMessageToFile("Kinect is not installed"); } if (deviceCount>0) { if (ddlKinectDevice.SelectedIndex == -1) ddlKinectDevice.SelectedIndex = 0; } else { pnlKinect.Enabled = false; } ddlKinectVideoMode.SelectedIndex = 0; if (NV("type") == "kinect") { try { chkKinectSkeletal.Checked = Convert.ToBoolean(NV("KinectSkeleton")); chkTripWires.Checked = Convert.ToBoolean(NV("TripWires")); if (NV("StreamMode")!="") ddlKinectVideoMode.SelectedIndex = Convert.ToInt32(NV("StreamMode")); } catch (Exception) { // ignored } } ddlTransport.Items.AddRange(_transports); ddlTransport.SelectedIndex = 0; ddlRTSP.SelectedIndex = CameraControl.Camobject.settings.rtspmode; chkConnectVLC.Enabled = chkConnectVLC.Checked = VlcHelper.VlcInstalled; int j = 0; foreach(var dev in MainForm.ONVIFDevices) { string n = dev.Name; if (!string.IsNullOrEmpty(dev.Location)) n += " (" + dev.Location + ")"; lbONVIFDevices.Items.Add(new MainForm.ListItem2(n,j)); j++; } _loaded = true; if (StartWizard) Wizard(); }