// On timer event - gather statistic private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Camera camera = cameraWindow.Camera; if (camera != null) { // get number of frames for the last second mStat.Push(camera.FramesReceived); this.toolStripStatusLabelFps.Text = mStat.FPS.ToString("F2") + " fps"; } this.UpdateUIWithFrame(); if (this.EditMode) { return; } mMutexAlarm.WaitOne(); if (this.mFireAlarmEvent) { this.AlarmCount = this.AlarmCount + 1; this.CameraClass.PlugIns.AlarmAll(true); this.mFireAlarmEvent = false; } if (this.CameraClass != null && this.CameraClass.CaptureElapse > 0 && this.CameraClass.Capturing == true) { Motion.Core.DateTimeEx date = new Motion.Core.DateTimeEx(); if ((date.TimeStamp - mCaptureElapse) / 3600 >= this.CameraClass.CaptureElapse) { this.StopAlarm(); this.StopCapture(); mMutexAlarm.ReleaseMutex(); return; } } // descrease save counter if (mIntervalsToSave > 0) { mIntervalsToSave--; if (mIntervalsToSave <= 0) { this.StopAlarm(); if (this.CaptureFlag != CAPTUREFLAG.ALWAYS) { this.StopCapture(); } } } mMutexAlarm.ReleaseMutex(); }
// On new frame private void camera_NewFrame(object sender, System.EventArgs e) { this.mHasFrame = true; if (this.EditMode == true || this.CameraClass == null || (this.CameraClass.Capture == CAPTUREFLAG.NOCAPTURE)) { return; } mMutexCapture.WaitOne(); if ((this.mIntervalsToSave > 0) || (this.CameraClass.Capture == CAPTUREFLAG.ALWAYS)) { // lets save the frame if (mAVIWriter == null && MotionConfiguration.Instance.StorageIsValid) { // create file name Motion.Core.DateTimeEx date = new Motion.Core.DateTimeEx(); String fileName = String.Format("{0}.{1}.{2}.avi", this.CameraClass.ID, (int)RecordMark.INVALID, date.TimeStamp); try { // create AVI writer mAVIWriter = new AVIWriterEx(this.CameraClass.Codec); // open AVI file mAVIWriter.Open(MotionConfiguration.Instance.StorageAVI + @"\" + fileName, cameraWindow.Camera.Width, cameraWindow.Camera.Height); DateTime n = DateTime.Now; string s = "" + n.Hour + ":" + n.Minute + ":" + n.Second; this.toolStripStatusLabelCapture.Text = string.Format(Translator.Instance.T("录像开始 ({0})"), s); this.CameraClass.Capturing = true; this.OnCameraViewRecordStarted(new CameraViewCaptureEventArgs(this)); mCaptureElapse = date.TimeStamp; } catch (Exception) { if (mAVIWriter != null) { mAVIWriter.Dispose(); mAVIWriter = null; } this.toolStripStatusLabelCapture.Text = Translator.Instance.T("录像失败"); this.CameraClass.Capturing = false; this.mCaptureElapse = 0; mMutexCapture.ReleaseMutex(); return; } } Camera camera = cameraWindow.Camera; if (camera != null && mAVIWriter != null) { Bitmap b = camera.LastFrame; if (b != null) { try { if (this.ShowBanner) { Bitmap bmp = CameraBanner.Render(b, this.CameraClass.Name, true); mAVIWriter.AddFrame(bmp); } else { mAVIWriter.AddFrame(b); } } catch (Exception) { this.toolStripStatusLabelCapture.Text = Translator.Instance.T("录像失败"); //OnCameraViewLog(new CameraViewLogEventArgs(Motion.Core.LogLevel.LOG_ERROR, ex.Message, this)); //MessageBox.Show(ex.Message + "\n" + ex.StackTrace.ToString(), this.Camera.Name); } } } } mMutexCapture.ReleaseMutex(); }