public void Start(CMSVideoSource videoSource, CMSVideoDisplay videoDisplay, CMSControlToggler controlToggler) { mutex = new object(); try { ProcessKeys[] procKeysDelegates = null; lock (mutex) { CMSConstants.Init(); controllerState = CMSState.Starting; model = new CMSModel(); model.Init("./" + CMSConstants.SUITE_LIB_DIR, "./" + CMSConstants.SUITE_CONFIG_DIR, "./" + CMSConstants.MAIN_CONFIG_FILE, "./" + CMSConstants.MAIN_CAMERA_CONFIG_FILE, "./" + CMSConstants.MAIN_LOG_CONFIG_FILE, "./" + CMSConstants.MAIN_ID_CONFIG_FILE); model.Load(); model.IncrementAndSaveSessionNum(); CMSLogger.SetUidReceivedDelegate(model.UidUpdated); CMSLogger.Init(model.LogConfig, model.IdConfig); //CMSLogger.CanCreateLogEvent( if (CMSLogger.CanCreateLogEvent(false, false, false, "CMSLogStartEvent")) { CMSLogStartEvent startEvent = new CMSLogStartEvent(); startEvent.SetDateTime(DateTime.Now); CMSLogger.SendLogEvent(startEvent); } this.videoSource = videoSource; videoSource.CameraLost += new CameraLost(CameraLost); videoSource.CameraFound += new CameraFound(CameraFound); videoSource.VideoInputSizesDetermined += new VideoInputSizesDetermined(VideoInputSizesDetermined); videoSource.ProcessFrame += new ProcessFrame(ProcessFrameFromSource); videoSource.Init(videoDisplay.GetParentForm()); this.videoDisplay = videoDisplay; videoDisplay.Init(new CMSViewAdapter(model, this, videoSource)); model.SelectedSuite.CMSTrackingSuiteAdapter = new CMSStandardTrackingSuiteAdapter(model, this, videoDisplay); string currentMoniker = model.CurrentMonikor; if (videoSource.StartSource(currentMoniker)) { string newMoniker = videoSource.GetCurrentMonikor(); model.CurrentMonikor = newMoniker; controllerState = CMSState.Setup; } else { controllerState = CMSState.CameraNotFound; } this.controlToggler = controlToggler; controlToggler.GetState = GetState; controlToggler.ToggleControl = ToggleControl; controlToggler.GetCursorPos = GetCursorPos; controlToggler.ControlTogglerConfig = model.GeneralConfig.ControlTogglerConfig; controlToggler.Start(); procKeysDelegates = new ProcessKeys[2]; procKeysDelegates[0] = ProcessKeys; procKeysDelegates[1] = controlToggler.ProcessKeys; } CMSKeyHook.initHook(procKeysDelegates); Application.Run(videoDisplay.GetParentForm()); CMSKeyHook.removeHook(); } catch (Exception e) { try { if (CMSLogger.CanCreateLogEvent(true, true, false, "CMSLogExceptionEvent")) { CMSLogExceptionEvent exceptionEvent = new CMSLogExceptionEvent(); exceptionEvent.SetException(e); CMSLogger.SendLogEvent(exceptionEvent); } } catch { } MessageBox.Show("Error occurred during startup:" + e.Message); Application.Exit(); } }
// Thread entry point public void WorkerThread() { // grabber Grabber grabber = new Grabber(this); // objects object graphObj = null; object sourceObj = null; object grabberObj = null; // interfaces IGraphBuilder graph = null; //IBaseFilter sourceBase = null; IBaseFilter grabberBase = null; ISampleGrabber sg = null; IMediaControl mc = null; try { // Get type for filter graph Type srvType = Type.GetTypeFromCLSID(Clsid.FilterGraph); if (srvType == null) { throw new ApplicationException("Failed creating filter graph"); } // create filter graph graphObj = Activator.CreateInstance(srvType); graph = (IGraphBuilder)graphObj; // ---- UCOMIBindCtx bindCtx = null; UCOMIMoniker moniker = null; int n = 0; // create bind context if (Win32.CreateBindCtx(0, out bindCtx) == 0) { // convert moniker`s string to a moniker if (Win32.MkParseDisplayName(bindCtx, source, ref n, out moniker) == 0) { // get device base filter Guid filterId = typeof(IBaseFilter).GUID; moniker.BindToObject(null, null, ref filterId, out sourceObj); Marshal.ReleaseComObject(moniker); moniker = null; } Marshal.ReleaseComObject(bindCtx); bindCtx = null; } // ---- if (sourceObj == null) { throw new ApplicationException("Failed creating device object for moniker"); } sourceBase = (IBaseFilter)sourceObj; // Get type for sample grabber srvType = Type.GetTypeFromCLSID(Clsid.SampleGrabber); if (srvType == null) { throw new ApplicationException("Failed creating sample grabber"); } // create sample grabber grabberObj = Activator.CreateInstance(srvType); sg = (ISampleGrabber)grabberObj; grabberBase = (IBaseFilter)grabberObj; // add source filter to graph graph.AddFilter(sourceBase, "source"); graph.AddFilter(grabberBase, "grabber"); // set media type AMMediaType mt = new AMMediaType(); mt.majorType = MediaType.Video; mt.subType = MediaSubType.RGB24; sg.SetMediaType(mt); // connect pins if (graph.Connect(DSTools.GetOutPin(sourceBase, 0), DSTools.GetInPin(grabberBase, 0)) < 0) { throw new ApplicationException("Failed connecting filters"); } // get media type if (sg.GetConnectedMediaType(mt) == 0) { // This is where the actual video input info is obtained VideoInfoHeader vih = (VideoInfoHeader)Marshal.PtrToStructure(mt.formatPtr, typeof(VideoInfoHeader)); //System.Diagnostics.Debug.WriteLine("width = " + vih.BmiHeader.Width + ", height = " + vih.BmiHeader.Height); grabber.Width = vih.BmiHeader.Width; grabber.Height = vih.BmiHeader.Height; mt.Dispose(); if (VideoInputSizeDetermined != null) { VideoInputSizeDetermined(this, new Size(vih.BmiHeader.Width, vih.BmiHeader.Height)); } } // render graph.Render(DSTools.GetOutPin(grabberBase, 0)); // sg.SetBufferSamples(false); sg.SetOneShot(false); sg.SetCallback(grabber, 1); // window IVideoWindow win = (IVideoWindow)graphObj; win.put_AutoShow(false); win = null; // get media control mc = (IMediaControl)graphObj; // run mc.Run(); while (!stopEvent.WaitOne(0, true)) { Thread.Sleep(100); } mc.StopWhenReady(); //mc.Stop(); } // catch any exceptions catch (Exception e) { try { if (CMSLogger.CanCreateLogEvent(true, true, false, "CMSLogExceptionEvent")) { CMSLogExceptionEvent exceptionEvent = new CMSLogExceptionEvent(); exceptionEvent.SetException(e); CMSLogger.SendLogEvent(exceptionEvent); } } catch { } } // finalization block finally { // release all objects mc = null; graph = null; sourceBase = null; grabberBase = null; sg = null; if (graphObj != null) { Marshal.ReleaseComObject(graphObj); graphObj = null; } if (sourceObj != null) { Marshal.ReleaseComObject(sourceObj); sourceObj = null; } if (grabberObj != null) { Marshal.ReleaseComObject(grabberObj); grabberObj = null; } } }