/// <summary> /// Releases unmanaged and - optionally - managed resources. /// </summary> /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> protected virtual void Dispose(bool disposing) { if (disposing) { Stop(); _colorReplay = null; _depthReplay = null; _bodyReplay = null; if (_reader != null) { _reader.Dispose(); _reader = null; } if (_stream != null) { _stream.Dispose(); _stream = null; } } }
//////////////////////////////////////////////////////////////////////////// #region CONSTRUCTOR / DESTRUCTOR //////////////////////////////////////////////////////////////////////////// /// <summary> /// Creates a new instance of a <c>KinectReplay</c> using the referenced stream. /// This will read in the headers for all frames in the stream and make them /// available for playback. The stream will remain open until the instance /// is disposed. /// </summary> public KinectReplay(Stream stream) { this._stream = stream; _reader = new BinaryReader(stream); _timer.Tick += _timer_Tick; var metadata = JsonConvert.DeserializeObject <FileMetadata>(_reader.ReadString()); Version version = this.GetType().GetTypeInfo().Assembly.GetName().Version; // default to this Version.TryParse(metadata.Version, out version); while (_reader.BaseStream.Position != _reader.BaseStream.Length) { try { FrameTypes type = (FrameTypes)_reader.ReadInt32(); switch (type) { case FrameTypes.Body: if (_bodyReplay == null) { _bodyReplay = new ReplayBodySystem(); _activeReplaySystems.Add(_bodyReplay); _bodyReplay.PropertyChanged += replay_PropertyChanged; _bodyReplay.FrameArrived += bodyReplay_FrameArrived; } _bodyReplay.AddFrame(_reader, version); break; case FrameTypes.Color: if (_colorReplay == null) { IColorCodec codec = new RawColorCodec(); if (metadata.ColorCodecId == ColorCodecs.Jpeg.CodecId) { codec = new JpegColorCodec(); } _colorReplay = new ReplayColorSystem(codec); _activeReplaySystems.Add(_colorReplay); _colorReplay.PropertyChanged += replay_PropertyChanged; _colorReplay.FrameArrived += colorReplay_FrameArrived; } _colorReplay.AddFrame(_reader); break; case FrameTypes.Depth: if (_depthReplay == null) { _depthReplay = new ReplayDepthSystem(); _activeReplaySystems.Add(_depthReplay); _depthReplay.PropertyChanged += replay_PropertyChanged; _depthReplay.FrameArrived += depthReplay_FrameArrived; } _depthReplay.AddFrame(_reader); break; case FrameTypes.Infrared: if (_infraredReplay == null) { _infraredReplay = new ReplayInfraredSystem(); _activeReplaySystems.Add(_infraredReplay); _infraredReplay.PropertyChanged += replay_PropertyChanged; _infraredReplay.FrameArrived += infraredReplay_FrameArrived; } _infraredReplay.AddFrame(_reader); break; } } catch { throw; } } foreach (var replaySystem in _activeReplaySystems) { if (replaySystem.Frames.Count > 0) { replaySystem.Frames.Sort(); for (var i = 0; i < replaySystem.Frames.Count; i++) { replaySystem.FrameTimeToIndex[replaySystem.Frames[i].RelativeTime] = i; } var first = replaySystem.Frames.First().RelativeTime; var last = replaySystem.Frames.Last().RelativeTime; if (first < _minTimespan) { _minTimespan = first; } if (last > _maxTimespan) { _maxTimespan = last; } } } bool hasFrames = false; foreach (var replaySystem in _activeReplaySystems) { if (replaySystem.Frames.Count > 0) { replaySystem.StartingOffset = _minTimespan; hasFrames = true; } } if (hasFrames) { this.Duration = _maxTimespan - _minTimespan; } else { this.Duration = TimeSpan.Zero; } }
//////////////////////////////////////////////////////////////////////////// #region CONSTRUCTOR / DESTRUCTOR //////////////////////////////////////////////////////////////////////////// /// <summary> /// Creates a new instance of a <c>KinectReplay</c> using the referenced stream. /// This will read in the headers for all frames in the stream and make them /// available for playback. The stream will remain open until the instance /// is disposed. /// </summary> public KinectReplay(Stream stream) { this._stream = stream; _reader = new BinaryReader(stream); _timer.Tick += _timer_Tick; var metadata = JsonConvert.DeserializeObject<FileMetadata>(_reader.ReadString()); Version version = this.GetType().GetTypeInfo().Assembly.GetName().Version; // default to this Version.TryParse(metadata.Version, out version); while (_reader.BaseStream.Position != _reader.BaseStream.Length) { try { FrameTypes type = (FrameTypes)_reader.ReadInt32(); switch (type) { case FrameTypes.Body: if (_bodyReplay == null) { _bodyReplay = new ReplayBodySystem(); _activeReplaySystems.Add(_bodyReplay); _bodyReplay.PropertyChanged += replay_PropertyChanged; _bodyReplay.FrameArrived += bodyReplay_FrameArrived; } _bodyReplay.AddFrame(_reader, version); break; case FrameTypes.Color: if (_colorReplay == null) { IColorCodec codec = new RawColorCodec(); if (metadata.ColorCodecId == ColorCodecs.Jpeg.CodecId) codec = new JpegColorCodec(); _colorReplay = new ReplayColorSystem(codec); _activeReplaySystems.Add(_colorReplay); _colorReplay.PropertyChanged += replay_PropertyChanged; _colorReplay.FrameArrived += colorReplay_FrameArrived; } _colorReplay.AddFrame(_reader); break; case FrameTypes.Depth: if (_depthReplay == null) { _depthReplay = new ReplayDepthSystem(); _activeReplaySystems.Add(_depthReplay); _depthReplay.PropertyChanged += replay_PropertyChanged; _depthReplay.FrameArrived += depthReplay_FrameArrived; } _depthReplay.AddFrame(_reader); break; case FrameTypes.Infrared: if (_infraredReplay == null) { _infraredReplay = new ReplayInfraredSystem(); _activeReplaySystems.Add(_infraredReplay); _infraredReplay.PropertyChanged += replay_PropertyChanged; _infraredReplay.FrameArrived += infraredReplay_FrameArrived; } _infraredReplay.AddFrame(_reader); break; } } catch { throw; } } foreach (var replaySystem in _activeReplaySystems) { if (replaySystem.Frames.Count > 0) { replaySystem.Frames.Sort(); for (var i = 0; i < replaySystem.Frames.Count; i++) { replaySystem.FrameTimeToIndex[replaySystem.Frames[i].RelativeTime] = i; } var first = replaySystem.Frames.First().RelativeTime; var last = replaySystem.Frames.Last().RelativeTime; if (first < _minTimespan) _minTimespan = first; if (last > _maxTimespan) _maxTimespan = last; } } bool hasFrames = false; foreach (var replaySystem in _activeReplaySystems) { if (replaySystem.Frames.Count > 0) { replaySystem.StartingOffset = _minTimespan; hasFrames = true; } } if (hasFrames) { this.Duration = _maxTimespan - _minTimespan; } else { this.Duration = TimeSpan.Zero; } }