private void Record() { try { _stopWrite.Reset(); if (!String.IsNullOrEmpty(Micobject.recorder.trigger)) { string[] tid = Micobject.recorder.trigger.Split(','); switch (tid[0]) { case "1": VolumeLevel vl = MainForm.InstanceReference.GetVolumeLevel(Convert.ToInt32(tid[1])); if (vl != null && !vl.Recording) vl.RecordSwitch(true); break; case "2": CameraWindow c = MainForm.InstanceReference.GetCameraWindow(Convert.ToInt32(tid[1])); if (c != null && !c.Recording) c.RecordSwitch(true); break; } } var cw = CameraControl; // if (cw != null) { if (cw.AbortedAudio) { MainForm.LogErrorToFile(Micobject.name + ": paired recording aborted as the camera is already recording"); ForcedRecording = false; return; } } try { if (cw != null) { cw.ForcedRecording = ForcedRecording; cw.StartSaving(); _stopWrite.WaitOne(); } else { #region mp3writer DateTime date = DateTime.Now; string filename = String.Format("{0}-{1}-{2}_{3}-{4}-{5}", date.Year, Helper.ZeroPad(date.Month), Helper.ZeroPad(date.Day), Helper.ZeroPad(date.Hour), Helper.ZeroPad(date.Minute), Helper.ZeroPad(date.Second)); AudioFileName = Micobject.id + "_" + filename; string folder = Dir.Entry + "audio\\" + Micobject.directory + "\\"; if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); filename = folder + AudioFileName; _writer = new AudioFileWriter(); try { Program.FFMPEGMutex.WaitOne(); _writer.Open(filename + ".mp3", AudioCodec.MP3, AudioSource.RecordingFormat.BitsPerSample*AudioSource.RecordingFormat.SampleRate* AudioSource.RecordingFormat.Channels, AudioSource.RecordingFormat.SampleRate, AudioSource.RecordingFormat.Channels); } finally { try { Program.FFMPEGMutex.ReleaseMutex(); } catch (ObjectDisposedException) { //can happen on shutdown } } double maxlevel = 0; bool first = true; DateTime recordingStart = Helper.Now; try { while (!_stopWrite.WaitOne(5)) { Helper.FrameAction fa; while (Buffer.TryDequeue(out fa)) { if (first) { recordingStart = fa.TimeStamp; first = false; } if (fa.FrameType == Enums.FrameType.Audio) { unsafe { fixed (byte* p = fa.Content) { _writer.WriteAudio(p, fa.DataLength); } } float d = Levels.Max(); _soundData.Append(String.Format(CultureInfo.InvariantCulture, "{0:0.000}", d)); _soundData.Append(","); if (d > maxlevel) maxlevel = d; } fa.Nullify(); } } FilesFile ff = _filelist.FirstOrDefault(p => p.Filename.EndsWith(AudioFileName + ".mp3")); bool newfile = false; if (ff == null) { ff = new FilesFile(); newfile = true; } string[] fnpath = (filename + ".mp3").Split('\\'); string fn = fnpath[fnpath.Length - 1]; var fi = new FileInfo(filename + ".mp3"); var dSeconds = Convert.ToInt32((Helper.Now - recordingStart).TotalSeconds); ff.CreatedDateTicks = DateTime.Now.Ticks; ff.Filename = fnpath[fnpath.Length - 1]; ff.MaxAlarm = maxlevel; ff.SizeBytes = fi.Length; ff.DurationSeconds = dSeconds; ff.IsTimelapse = false; ff.IsMergeFile = false; ff.AlertData = Helper.GetMotionDataPoints(_soundData); _soundData.Clear(); ff.TriggerLevel = Micobject.detector.minsensitivity; ff.TriggerLevelMax = Micobject.detector.maxsensitivity; if (newfile) { lock (_lockobject) { _filelist.Insert(0, ff); } MainForm.MasterFileAdd(new FilePreview(fn, dSeconds, Micobject.name, DateTime.Now.Ticks, 1, Micobject.id, ff.MaxAlarm, false, false)); MainForm.NeedsMediaRefresh = Helper.Now; } } catch (Exception ex) { if (ErrorHandler != null) ErrorHandler(ex.Message); } if (_writer != null && _writer.IsOpen) { try { Program.FFMPEGMutex.WaitOne(); _writer.Dispose(); } catch (Exception ex) { if (ErrorHandler != null) ErrorHandler(ex.Message); } finally { try { Program.FFMPEGMutex.ReleaseMutex(); } catch (ObjectDisposedException) { //can happen on shutdown } } } _writer = null; #endregion } UpdateFloorplans(false); } catch (Exception ex) { if (ErrorHandler != null) ErrorHandler(ex.Message); } if (!String.IsNullOrEmpty(Micobject.recorder.trigger)) { string[] tid = Micobject.recorder.trigger.Split(','); switch (tid[0]) { case "1": VolumeLevel vl = MainForm.InstanceReference.GetVolumeLevel(Convert.ToInt32(tid[1])); if (vl != null) vl.RecordSwitch(false); break; case "2": CameraWindow c = MainForm.InstanceReference.GetCameraWindow(Convert.ToInt32(tid[1])); if (c != null) c.RecordSwitch(false); break; } } if (cw == null) { Micobject.newrecordingcount++; if (Notification != null) Notification(this, new NotificationType("NewRecording", Micobject.name, "")); } } catch (Exception ex) { MainForm.LogExceptionToFile(ex); } }
private void Record() { _stopWrite = false; DateTime recordingStart = DateTime.MinValue; if (!String.IsNullOrEmpty(Micobject.recorder.trigger) && TopLevelControl != null) { string[] tid = Micobject.recorder.trigger.Split(','); switch (tid[0]) { case "1": VolumeLevel vl = ((MainForm)TopLevelControl).GetVolumeLevel(Convert.ToInt32(tid[1])); if (vl != null) vl.RecordSwitch(true); break; case "2": CameraWindow cw = ((MainForm)TopLevelControl).GetCameraWindow(Convert.ToInt32(tid[1])); if (cw != null) cw.RecordSwitch(true); break; } } try { WriterBuffer = new QueueWithEvents<AudioAction>(); WriterBuffer.Changed += WriterBufferChanged; _pairedRecording = false; if (CameraControl!=null && CameraControl.Camobject.settings.active) { _pairedRecording = true; CameraControl.StartSaving(); CameraControl.ForcedRecording = ForcedRecording; while (!_stopWrite) { _newRecordingFrame.WaitOne(200); } } else { #region mp3writer DateTime date = DateTime.Now; string filename = String.Format("{0}-{1}-{2}_{3}-{4}-{5}", date.Year, Helper.ZeroPad(date.Month), Helper.ZeroPad(date.Day), Helper.ZeroPad(date.Hour), Helper.ZeroPad(date.Minute), Helper.ZeroPad(date.Second)); AudioFileName = Micobject.id + "_" + filename; filename = MainForm.Conf.MediaDirectory + "audio\\" + Micobject.directory + "\\"; filename += AudioFileName; Program.WriterMutex.WaitOne(); try { _writer = new AudioFileWriter(); _writer.Open(filename + ".mp3", AudioCodec.MP3, AudioSource.RecordingFormat.BitsPerSample * AudioSource.RecordingFormat.SampleRate * AudioSource.RecordingFormat.Channels, AudioSource.RecordingFormat.SampleRate, AudioSource.RecordingFormat.Channels); } catch (Exception ex) { Log.Error("",ex);//MainForm.LogExceptionToFile(ex); } finally { Program.WriterMutex.ReleaseMutex(); } double maxlevel = 0; foreach (AudioAction aa in AudioBuffer.OrderBy(p=>p.TimeStamp)) { if (recordingStart == DateTime.MinValue) { recordingStart = aa.TimeStamp; } unsafe { fixed (byte* p = aa.Decoded) { _writer.WriteAudio(p, aa.Decoded.Length); } } _soundData.Append(String.Format(CultureInfo.InvariantCulture, "{0:0.000}", aa.SoundLevel)); _soundData.Append(","); if (aa.SoundLevel > maxlevel) maxlevel = aa.SoundLevel; } AudioBuffer.Clear(); if (recordingStart == DateTime.MinValue) recordingStart = DateTime.Now; try { while (!_stopWrite) { while (WriterBuffer.Count > 0) { AudioAction b; lock (_obj) { b = WriterBuffer.Dequeue(); } unsafe { fixed (byte* p = b.Decoded) { _writer.WriteAudio(p, b.Decoded.Length); } } float d = Levels.Max(); _soundData.Append(String.Format(CultureInfo.InvariantCulture, "{0:0.000}", d)); _soundData.Append(","); if (d > maxlevel) maxlevel = d; } _newRecordingFrame.WaitOne(200); } FilesFile ff = FileList.FirstOrDefault(p => p.Filename.EndsWith(AudioFileName + ".mp3")); bool newfile = false; if (ff == null) { ff = new FilesFile(); newfile = true; } string[] fnpath = (filename + ".mp3").Split('\\'); string fn = fnpath[fnpath.Length - 1]; var fi = new FileInfo(filename + ".mp3"); var dSeconds = Convert.ToInt32((DateTime.Now - recordingStart).TotalSeconds); ff.CreatedDateTicks = DateTime.Now.Ticks; ff.Filename = fnpath[fnpath.Length - 1]; ff.MaxAlarm = maxlevel; ff.SizeBytes = fi.Length; ff.DurationSeconds = dSeconds; ff.IsTimelapse = false; ff.AlertData = Helper.GetMotionDataPoints(_soundData); _soundData.Clear(); ff.TriggerLevel = Micobject.detector.sensitivity; if (newfile) { FileList.Insert(0, ff); if (MainForm.MasterFileList.Count(p => p.Filename.EndsWith(fn)) == 0) { MainForm.MasterFileList.Add(new FilePreview(fn, dSeconds, Micobject.name, DateTime.Now.Ticks, 1,Micobject.id, ff.MaxAlarm)); } } } catch (Exception ex) { Log.Error("",ex);//MainForm.LogExceptionToFile(ex); } Program.WriterMutex.WaitOne(); try { _writer.Close(); _writer.Dispose(); } catch (Exception ex) { Log.Error("",ex);//MainForm.LogExceptionToFile(ex); } finally { Program.WriterMutex.ReleaseMutex(); } _writer = null; #endregion } _stopWrite = false; WriterBuffer = null; _recordingTime = 0; UpdateFloorplans(false); } catch (Exception ex) { Log.Error("",ex);//MainForm.LogExceptionToFile(ex); } if (!String.IsNullOrEmpty(Micobject.recorder.trigger) && TopLevelControl != null) { string[] tid = Micobject.recorder.trigger.Split(','); switch (tid[0]) { case "1": VolumeLevel vl = ((MainForm)TopLevelControl).GetVolumeLevel(Convert.ToInt32(tid[1])); if (vl != null) vl.RecordSwitch(false); break; case "2": CameraWindow cw = ((MainForm)TopLevelControl).GetCameraWindow(Convert.ToInt32(tid[1])); if (cw != null) cw.RecordSwitch(false); break; } } if (!_pairedRecording) { Micobject.newrecordingcount++; if (Notification != null) Notification(this, new NotificationType("NewRecording", Micobject.name, "")); } }