/// <summary> /// Records a <c>ReplayBodyFrame</c>. /// </summary> /// <param name="frame">The frame.</param> public async Task RecordAsync(ReplayBodyFrame frame) { if (_writer.BaseStream == null || _writer.BaseStream.CanWrite == false) { return; } await Task.Run(() => { try { // Header _writer.Write((int)frame.FrameType); _writer.Write(frame.RelativeTime.TotalMilliseconds); // Data using (var dataStream = new MemoryStream()) { using (var dataWriter = new BinaryWriter(dataStream)) { dataWriter.Write(frame.BodyCount); dataWriter.Write(frame.FloorClipPlane.W); dataWriter.Write(frame.FloorClipPlane.X); dataWriter.Write(frame.FloorClipPlane.Y); dataWriter.Write(frame.FloorClipPlane.Z); int trackedBodyCount = 0; for (var i = 0; i < frame.BodyCount; i++) { if (RecordBody(frame.Bodies[i], dataWriter)) { trackedBodyCount++; } } // Reset frame data stream dataWriter.Flush(); dataStream.Position = 0; // Write FrameSize _writer.Write(dataStream.Length); // Write actual frame data dataStream.CopyTo(_writer.BaseStream); // Write end of frame marker _writer.Write(ReplayFrame.EndOfFrameMarker); System.Diagnostics.Debug.WriteLine(" (recorded {0} tracked bodies)", trackedBodyCount); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } }); }
private void bodyReplay_FrameArrived(ReplayBodyFrame frame) { if (BodyFrameArrived != null) { BodyFrameArrived(this, new ReplayFrameArrivedEventArgs <ReplayBodyFrame> { Frame = frame }); } }
/// <summary> /// Adds a frame to the Frames list. /// </summary> /// <param name="reader">The reader.</param> /// <param name="version">The version.</param> public void AddFrame(BinaryReader reader, Version version) { var frame = ReplayBodyFrame.FromReader(reader, version); if (frame != null) { this.Frames.Add(frame); } }
/// <summary> /// Initializes a new instance of the <see cref="ReplayBodyFrame"/> class /// by reading from the specified <c>BinaryReader</c>. /// </summary> /// <param name="reader">The reader.</param> /// <param name="version">The version.</param> /// <returns>The <c>ReplayBodyFrame</c></returns> /// <exception cref="System.IO.IOException">The recording appears to be corrupt.</exception> public static ReplayBodyFrame FromReader(BinaryReader reader, Version version) { var frame = new ReplayBodyFrame(); frame.FrameType = FrameTypes.Body; frame.RelativeTime = TimeSpan.FromMilliseconds(reader.ReadDouble()); frame.FrameSize = reader.ReadInt64(); long frameStartPos = reader.BaseStream.Position; frame.BodyCount = reader.ReadInt32(); frame.FloorClipPlane = new Vector4() { W = reader.ReadSingle(), X = reader.ReadSingle(), Y = reader.ReadSingle(), Z = reader.ReadSingle() }; frame.Bodies = new List <CustomBody>(); for (var i = 0; i < frame.BodyCount; i++) { frame.Bodies.Add(CreateBodyFromReader(reader, version)); } // Do Frame Integrity Check if (reader.ReadString() != ReplayFrame.EndOfFrameMarker) { System.Diagnostics.Debug.WriteLine("BAD FRAME...RESETTING"); reader.BaseStream.Position = frameStartPos + frame.FrameSize; if (reader.ReadString() != ReplayFrame.EndOfFrameMarker) { throw new IOException("The recording appears to be corrupt."); } return(null); } return(frame); }