blob: 4b0517b34f70a88fd0e10d53e05419be2a341997 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
using Google.Protobuf;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using Tango.BL.Entities;
using Tango.Core;
using Tango.Core.Helpers;
using Tango.Core.IO;
using Tango.Logging;
using Tango.PMR.Diagnostics;
using Tango.Serialization;
namespace Tango.Integration.Diagnostics
{
/// <summary>
/// Represents a Tango machine diagnostics file recorder capable of recording diagnostics data coming from the machine to a file.
/// This file can then be played using the <see cref="DiagnosticsFilePlayer"/>.
/// </summary>
/// <seealso cref="Tango.Core.ExtendedObject" />
/// <seealso cref="System.IDisposable" />
public class DiagnosticsFileRecorder : ExtendedObject, IDisposable
{
private ConcurrentQueue<DataFileFrame> _frames; //Holds the queue of data frames ready to be written to file.
private Thread _recordingThread; //Holds the recording thread.
private TemporaryFile _tempDataFile; //Holds the temporary recording file name.
private FileStream _dataFileStream; //Holds the temporary recording file stream.
private TaskCompletionSource<object> _stopCompletionSource; //Holds the "Stop" async method completion source.
private DiagnosticsTimeCodeChannel _timeCodeChannel; //Holds the diagnostics time code channel.
private List<DiagnosticsFileEvent> _events;
private Stopwatch _stopWatch; //Holds the stop watch for keeping tracks over frames time stamps.
private DateTime _lastVideoPush;
private List<BitmapSource> _videoFrames;
#region Constructors
public DiagnosticsFileRecorder()
{
_lastVideoPush = DateTime.Now;
VideoFPS = 10;
VideoQuality = 30;
}
#endregion
#region Properties
private bool _isRecording;
/// <summary>
/// Gets a value indicating whether the recorder is currently recording.
/// </summary>
public bool IsRecording
{
get { return _isRecording; }
private set { _isRecording = value; RaisePropertyChangedAuto(); }
}
private long _totalFramesRecorded;
/// <summary>
/// Gets the total frames recorded.
/// </summary>
public long TotalFramesRecorded
{
get { return _totalFramesRecorded; }
private set { _totalFramesRecorded = value; RaisePropertyChanged(nameof(TotalFramesRecorded)); }
}
private long _totalVideoFramesRecorded;
/// <summary>
/// Gets or sets the total video frames recorded.
/// </summary>
public long TotalVideoFramesRecorded
{
get { return _totalVideoFramesRecorded; }
private set { _totalVideoFramesRecorded = value; RaisePropertyChangedAuto(); }
}
private int _videoQuality;
/// <summary>
/// Gets or sets the video quality.
/// </summary>
public int VideoQuality
{
get { return _videoQuality; }
set { _videoQuality = value; RaisePropertyChangedAuto(); }
}
private long _totalBytesDataRecorded;
/// <summary>
/// Gets the total bytes recorded.
/// </summary>
public long TotalDataBytesRecorded
{
get { return _totalBytesDataRecorded; }
private set { _totalBytesDataRecorded = value; RaisePropertyChanged(nameof(TotalDataBytesRecorded)); }
}
private long _totalBytesRecorded;
/// <summary>
/// Gets or sets the total bytes recorded.
/// </summary>
public long TotalBytesRecorded
{
get { return _totalBytesRecorded; }
set { _totalBytesRecorded = value; RaisePropertyChangedAuto(); }
}
/// <summary>
/// Gets the recording time.
/// </summary>
public TimeSpan RecordingTime
{
get { return _stopWatch != null ? _stopWatch.Elapsed : TimeSpan.Zero; }
}
/// <summary>
/// Gets or sets the video recording frame rate.
/// </summary>
public int VideoFPS { get; set; }
#endregion
#region Public Methods
/// <summary>
/// Start recording. The actual recording will start only on the next <see cref="Write(PushDiagnosticsResponse)"/> call.
/// </summary>
public void Start()
{
if (!IsRecording)
{
if (_stopWatch != null)
{
_stopWatch.Stop();
_stopWatch = null;
}
_tempDataFile = TemporaryManager.CreateFile();
_frames = new ConcurrentQueue<DataFileFrame>();
_events = new List<DiagnosticsFileEvent>();
_timeCodeChannel = new DiagnosticsTimeCodeChannel();
TotalDataBytesRecorded = 0;
TotalFramesRecorded = 0;
TotalVideoFramesRecorded = 0;
TotalBytesRecorded = 0;
_dataFileStream = _tempDataFile.CreateStream();
IsRecording = true;
_recordingThread = new Thread(RecordingThreadMethod);
_recordingThread.IsBackground = true;
_recordingThread.Start();
}
}
/// <summary>
/// Stop recording.
/// </summary>
/// <returns></returns>
public Task Stop()
{
if (IsRecording)
{
IsRecording = false;
_stopCompletionSource = new TaskCompletionSource<object>();
return _stopCompletionSource.Task;
}
else
{
return Task.FromResult(new object());
}
}
/// <summary>
/// Writes a machine event to the diagnostics file.
/// </summary>
/// <param name="data">The data.</param>
public void Write(MachinesEvent ev)
{
_events.Add(new DiagnosticsFileEvent(ev));
}
/// <summary>
/// Writes a diagnostics packet to the recording.
/// </summary>
/// <param name="data">The data.</param>
public void Write(StartDiagnosticsResponse data)
{
DataFileFrame frame = new DataFileFrame();
frame.StartDiagnosticsResponse = data;
Write(frame);
}
/// <summary>
/// Writes the specified video frames. Each video frame is a different camera!
/// </summary>
/// <param name="videoFrames">The video frames.</param>
public void Write(IEnumerable<BitmapSource> videoFrames)
{
if (!IsRecording) throw LogManager.Log(new InvalidOperationException("Please start the recorder before attempting to push data."));
int milli = 1000 / VideoFPS;
if (DateTime.Now >= _lastVideoPush.AddMilliseconds(milli))
{
_videoFrames = videoFrames.ToList();
_lastVideoPush = DateTime.Now;
}
}
/// <summary>
/// Writes the specified data frame to the recording.
/// </summary>
/// <param name="frame">The frame.</param>
/// <exception cref="InvalidOperationException">Please start the recorder before attempting to push data.</exception>
public void Write(DataFileFrame frame)
{
if (!IsRecording) throw LogManager.Log(new InvalidOperationException("Please start the recorder before attempting to push data."));
if (_stopWatch == null)
{
_stopWatch = new Stopwatch();
_stopWatch.Start();
}
frame.Milliseconds = (int)_stopWatch.Elapsed.TotalMilliseconds;
_frames.Enqueue(frame);
RaisePropertyChanged(nameof(RecordingTime));
}
/// <summary>
/// Saves the current recording to the specified file. (Use only after calling "Stop").
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <returns></returns>
/// <exception cref="InvalidOperationException">
/// Cannot save a recording file while recording.
/// or
/// No data was captured during recording time. Cannot save recording
/// </exception>
public Task Save(String fileName)
{
if (IsRecording)
{
throw LogManager.Log(new InvalidOperationException("Cannot save a recording file while recording."));
}
if (_timeCodeChannel.Frames.Count == 0)
{
throw LogManager.Log(new InvalidOperationException("No data was captured during recording time. Cannot save recording"));
}
return Task.Factory.StartNew(() =>
{
BinaryDataSerializer serializer = new BinaryDataSerializer();
_timeCodeChannel.Events = _events;
byte[] timeCodeData = serializer.SerializeToBytes(_timeCodeChannel);
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
BinaryWriter writer = new BinaryWriter(fs);
writer.Write(timeCodeData.Length);
writer.Write(timeCodeData, 0, timeCodeData.Length);
using (Stream dataFileStream = _tempDataFile.CreateStream())
{
dataFileStream.CopyTo(fs);
}
}
});
}
#endregion
#region Recording Thread
/// <summary>
/// Handles the recording thread.
/// </summary>
private void RecordingThreadMethod()
{
while (IsRecording)
{
while (_frames.Count > 0)
{
DataFileFrame frame = null;
if (_frames.TryDequeue(out frame))
{
DiagnosticsTimeCodeChannelFrame timeCodeFrame = new DiagnosticsTimeCodeChannelFrame();
timeCodeFrame.Position = TotalDataBytesRecorded;
if (_videoFrames != null && _videoFrames.Count > 0)
{
foreach (var videoFrame in _videoFrames)
{
if (videoFrame != null)
{
var bytes = ByteString.CopyFrom(videoFrame.ToJpegBytes(VideoQuality));
frame.VideoFrames.Add(bytes);
TotalBytesRecorded += bytes.Length;
}
}
_videoFrames = null;
TotalVideoFramesRecorded++;
}
byte[] data = frame.ToBytes();
_dataFileStream.Write(data, 0, data.Length);
TotalFramesRecorded++;
TotalDataBytesRecorded += data.Length;
TotalBytesRecorded += data.Length;
timeCodeFrame.Milliseconds = frame.Milliseconds;
timeCodeFrame.FrameLength = data.Length;
_timeCodeChannel.Frames.Add(timeCodeFrame);
}
}
Thread.Sleep(10);
}
_dataFileStream.Dispose();
if (_stopCompletionSource != null)
{
_stopCompletionSource.SetResult(new object());
}
}
#endregion
#region IDisposable
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
_tempDataFile.Delete();
}
#endregion
}
}
|