public void Start() { nowColor = DateTime.Now; nowDepth = DateTime.Now; nowInfrared = DateTime.Now; laterColor = nowColor.AddSeconds(1); laterDepth = nowDepth.AddSeconds(1); laterInfrared = nowInfrared.AddSeconds(1); if (_isStarted) return; if (_sensor != null) { if (EnableColorRecorder) { _colorReader = _sensor.ColorFrameSource.OpenReader(); _colorReader.FrameArrived += _colorReader_FrameArrived; } if (EnableDepthRecorder) { _depthReader = _sensor.DepthFrameSource.OpenReader(); _depthReader.FrameArrived += _depthReader_FrameArrived; } if (EnableInfraredRecorder) { _infraredReader = _sensor.InfraredFrameSource.OpenReader(); _infraredReader.FrameArrived += _infraredReader_FrameArrived; } if (!_sensor.IsOpen) _sensor.Open(); } _isStarted = true; try { _generalSemaphore.Wait(); var metadata = new FileMetadata() { Version = this.GetType().GetTypeInfo().Assembly.GetName().Version.ToString(), HasColor = this.EnableColorRecorder, HasDepth = this.EnableDepthRecorder, HasInfrared = this.EnableInfraredRecorder, ColorLocation = this.ColorLocation, DepthLocation = this.DepthLocation, InfraredLocation = this.InfraredLocation, ColorCodecId = this.ColorRecorderCodec.ColorCodecId, DepthCodecId = this.DepthRecorderCodec.DepthCodecId, InfraredCodecId = this.InfraredRecorderCodec.InfraredCodecId, FpsColor = 100-this.ColorFramerate, FpsDepth = 100-this.DepthFramerate, FpsInfrared = 100-this.InfraredFramerate }; _fileMetaDataWriter.Write(JsonConvert.SerializeObject(metadata)); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Error Saving MetaData: " + ex); } finally { _generalSemaphore.Release(); if (_fileMetaDataWriter != null) { _fileMetaDataWriter.Flush(); if (_fileMetaDataWriter.BaseStream != null) { _fileMetaDataWriter.BaseStream.Flush(); } _fileMetaDataWriter.Dispose(); _fileMetaDataWriter = null; } } _processColorFramesTask = ProcessColorFramesAsync(); _processDepthFramesTask = ProcessDepthFramesAsync(); _processInfraredFramesTask = ProcessInfraredFramesAsync(); }
public KinectPlay(string colorLocation, string depthLocation, string infraredLocation, FileMetadata metadata) { _timer.Tick += _timer_Tick; this._colorStream = File.Open(colorLocation, FileMode.Open, FileAccess.Read); this._depthStream = File.Open(depthLocation, FileMode.Open, FileAccess.Read); this._infraredStream = File.Open(infraredLocation, FileMode.Open, FileAccess.Read); _colorReader = new BinaryReader(_colorStream); _depthReader = new BinaryReader(_depthStream); _infraredReader = new BinaryReader(_infraredStream); while (_colorReader.BaseStream.Position != _colorReader.BaseStream.Length) { FrameTypes type = (FrameTypes)_colorReader.ReadInt32(); if (_colorPlay == null) { ICodec codec = new RawCodec(); if (metadata.ColorCodecId == Codecs.JpegColor.ColorCodecId) codec = new JpegCodec(); _colorPlay = new PlayColorSystem(codec); _activePlaySystems.Add(_colorPlay); _colorPlay.PropertyChanged += play_PropertyChanged; _colorPlay.FrameArrived += colorPlay_FrameArrived; } _colorPlay.AddFrame(_colorReader); } while (_depthReader.BaseStream.Position != _depthReader.BaseStream.Length) { FrameTypes type = (FrameTypes)_depthReader.ReadInt32(); if (_depthPlay == null) { ICodec codec = new RawCodec(); if (metadata.DepthCodecId == Codecs.JpegDepth.DepthCodecId) codec = new JpegCodec(); else if (metadata.DepthCodecId == Codecs.PngDepth.DepthCodecId) codec = new PngCodec(); _depthPlay = new PlayDepthSystem(codec); _activePlaySystems.Add(_depthPlay); _depthPlay.PropertyChanged += play_PropertyChanged; _depthPlay.FrameArrived += depthPlay_FrameArrived; } _depthPlay.AddFrame(_depthReader); } while (_infraredReader.BaseStream.Position != _infraredReader.BaseStream.Length) { FrameTypes type = (FrameTypes)_infraredReader.ReadInt32(); if (_infraredPlay == null) { ICodec codec = new RawCodec(); if (metadata.InfraredCodecId == Codecs.JpegInfrared.InfraredCodecId) codec = new JpegCodec(); else if (metadata.InfraredCodecId == Codecs.PngInfrared.InfraredCodecId) codec = new PngCodec(); _infraredPlay = new PlayInfraredSystem(codec); _activePlaySystems.Add(_infraredPlay); _infraredPlay.PropertyChanged += play_PropertyChanged; _infraredPlay.FrameArrived += infraredPlay_FrameArrived; } _infraredPlay.AddFrame(_infraredReader); } foreach (var playSystem in _activePlaySystems) { if (playSystem.Frames.Count > 0) { playSystem.Frames.Sort(); for (var i = 0; i < playSystem.Frames.Count; i++) { playSystem.FrameTimeToIndex[playSystem.Frames[i].RelativeTime] = i; } var first = playSystem.Frames.First().RelativeTime; var last = playSystem.Frames.Last().RelativeTime; if (first < _minTimespan) _minTimespan = first; if (last > _maxTimespan) _maxTimespan = last; } } bool hasFrames = false; foreach (var playSystem in _activePlaySystems) { if (playSystem.Frames.Count > 0) { playSystem.StartingOffset = _minTimespan; hasFrames = true; } } if (hasFrames) { this.Duration = _maxTimespan - _minTimespan; } else { this.Duration = TimeSpan.Zero; } }