-
Notifications
You must be signed in to change notification settings - Fork 0
/
OryxCamera.cs
421 lines (369 loc) · 18.8 KB
/
OryxCamera.cs
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
using System;
using System.Collections.Generic;
using SpinnakerNET;
using SpinnakerNET.GenApi;
using System.Threading;
using System.Drawing;
using Emgu.CV;
using Emgu.CV.UI;
using System.Windows.Forms;
using Emgu.CV.Structure;
using System.Collections.Concurrent;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;
using System.Linq.Expressions;
using Emgu.CV.Ocl;
using System.Runtime.InteropServices;
using System.Windows.Forms.VisualStyles;
using System.Drawing.Text;
using System.Security.Cryptography.X509Certificates;
using Emgu.CV.Stitching;
using Emgu.CV.Cuda;
using Emgu.CV.Util;
namespace FetchRig3
{
public class OryxCamera
{
readonly int camNumber;
public string settingsFileName;
private string sessionPath;
private string encodePipeName;
private Size frameSize;
private ConcurrentQueue<ButtonCommands> camControlMessageQueue;
private ConcurrentQueue<RawMat> streamOutputQueue;
// These fields will be accessed by an OryxCameraSettings object to set and save camera settings.
public IManagedCamera managedCamera;
public Util.OryxSetupInfo setupInfo;
public INodeMap nodeMapTLDevice;
public INodeMap nodeMapTLStream;
public INodeMap nodeMap;
readonly Size streamFrameSize;
readonly int streamEnqueueDutyCycle;
public OryxCamera(int camNumber, IManagedCamera managedCamera, ConcurrentQueue<ButtonCommands> camControlMessageQueue, ConcurrentQueue<RawMat> streamOutputQueue, Util.OryxSetupInfo setupInfo, string sessionPath)
{
this.camNumber = camNumber;
this.managedCamera = managedCamera;
this.camControlMessageQueue = camControlMessageQueue;
this.streamOutputQueue = streamOutputQueue;
this.setupInfo = setupInfo;
this.sessionPath = sessionPath;
settingsFileName = this.sessionPath + @"\" + "cam" + this.camNumber.ToString() + @"_cameraSettings.txt";
encodePipeName = "ffpipe" + camNumber.ToString();
frameSize = new Size(width: setupInfo.maxFramesize.Width, height: setupInfo.maxFramesize.Height);
streamFrameSize = this.setupInfo.streamFramesize;
streamEnqueueDutyCycle = 2;
GetNodeMapsAndInitialize();
LoadCameraSettings();
DisplayLoop();
}
private void GetNodeMapsAndInitialize()
{
nodeMapTLDevice = managedCamera.GetTLDeviceNodeMap();
nodeMapTLStream = managedCamera.GetTLStreamNodeMap();
managedCamera.Init();
nodeMap = managedCamera.GetNodeMap();
Console.WriteLine("Camera number {0} opened and initialized on thread {1}", camNumber, Thread.CurrentThread.ManagedThreadId);
}
private void LoadCameraSettings()
{
OryxCameraSettings oryxCameraSettings = new OryxCameraSettings(this);
oryxCameraSettings.SaveSettings(_printSettings: false);
}
public class FFProcess
{
private string _pipeName;
private string _videoFileName;
private string inputArgs;
private string outputArgs;
private string fullArgs;
public Process process;
public FFProcess(string pipeName, string videoFileName)
{
_pipeName = @"\\.\pipe\" + pipeName;
_videoFileName = videoFileName;
inputArgs = "-nostats -y -vsync 0 -f rawvideo -s 3208x2200 -pix_fmt gray -framerate 100 -i " + _pipeName + " -an -sn";
outputArgs = "-gpu 0 -vcodec h264_nvenc -r 100 -preset fast -qp 20 " + _videoFileName;
fullArgs = inputArgs + " " + outputArgs;
}
public void OpenWithStartInfo()
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "ffmpeg.exe";
startInfo.Arguments = fullArgs;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
process = Process.Start(startInfo);
}
}
private void DisplayLoop()
{
// Setup EncodeQueue and EncodeThread
ConcurrentQueue<byte[]> encodeQueue = new ConcurrentQueue<byte[]>();
Thread encodeThread = null;
// Use enum to indicate loop state and which DisplayLoop block to execute
DisplayLoopState loopState = DisplayLoopState.WaitingForMessagesWhileNotStreaming;
int streamImageCtr = 0;
List<long> skipEvents = new List<long>();
int rawImageSizeInBytes = frameSize.Width * frameSize.Height;
bool isMessageDequeueSuccess;
bool go = true;
while (go)
{
if (loopState == DisplayLoopState.WaitingForMessagesWhileNotStreaming)
{
isMessageDequeueSuccess = camControlMessageQueue.TryDequeue(out ButtonCommands message);
if (isMessageDequeueSuccess)
{
if (message == ButtonCommands.BeginAcquisition)
{
Console.WriteLine("{0} message received in WaitForMessagesWhileNotStreaming block on camera {1}. Press BeginStreaming Button after memory allocation complete.", message, camNumber.ToString());
managedCamera.BeginAcquisition();
continue;
}
else if (message == ButtonCommands.BeginStreaming)
{
loopState = DisplayLoopState.BeginStreaming;
continue;
}
else if (message == ButtonCommands.PlayInitiateTrialTone || message == ButtonCommands.PlayRewardTone)
{
continue;
}
else if (message == ButtonCommands.Exit)
{
loopState = DisplayLoopState.Exit;
continue;
}
else
{
Console.WriteLine("Invalid message ({0}) received in WaitForMessagesWhileNotStreaming on camera {1}", message, camNumber.ToString());
continue;
}
}
Thread.Sleep(50);
continue;
}
else if (loopState == DisplayLoopState.BeginStreaming)
{
loopState = DisplayLoopState.StreamingAndWaitingForMessages;
if (!managedCamera.IsStreaming())
{
managedCamera.BeginAcquisition();
}
streamImageCtr = 0;
using (IManagedImage rawImage = managedCamera.GetNextImage())
{
streamImageCtr++;
long currFrameID = rawImage.ChunkData.FrameID;
long currFrameTimestamp = rawImage.ChunkData.Timestamp;
}
continue;
}
else if (loopState == DisplayLoopState.StreamingAndWaitingForMessages)
{
try
{
using (IManagedImage rawImage = managedCamera.GetNextImage())
{
streamImageCtr++;
long currFrameTimestamp = rawImage.ChunkData.Timestamp;
long currFrameID = rawImage.ChunkData.FrameID;
if (streamImageCtr % streamEnqueueDutyCycle == 0)
{
Mat streamImageMat = new Mat(rows: frameSize.Height, cols: frameSize.Width, type: Emgu.CV.CvEnum.DepthType.Cv8U, channels: 1, data: rawImage.DataPtr, step: frameSize.Width);
Mat streamImageMatResized = new Mat(size: streamFrameSize, type: Emgu.CV.CvEnum.DepthType.Cv8U, channels: 1);
CvInvoke.Resize(src: streamImageMat, dst: streamImageMatResized, dsize: streamFrameSize, interpolation: Emgu.CV.CvEnum.Inter.Linear);
RawMat matWithMetaData = new RawMat(frameID: currFrameID, frameTimestamp: currFrameTimestamp,
isNewBackgroundImage: false, closeForm: false, rawMat: streamImageMatResized);
streamOutputQueue.Enqueue(matWithMetaData);
streamImageMat.Dispose();
}
}
}
catch (SpinnakerException ex)
{
Console.WriteLine("Error in DisplayLoop block on camera {0}: {1}", camNumber.ToString(), ex.Message);
}
if (streamImageCtr % 10 == 0)
{
isMessageDequeueSuccess = camControlMessageQueue.TryDequeue(out ButtonCommands message);
if (isMessageDequeueSuccess)
{
if (message == ButtonCommands.StartRecording)
{
loopState = DisplayLoopState.InitiateFFProcessAndRecord;
continue;
}
else if (message == ButtonCommands.EndStreaming)
{
loopState = DisplayLoopState.EndAcquisition;
continue;
}
else
{
Console.WriteLine("{0} message invalid: LoopState = Streaming.", message);
continue;
}
}
}
}
else if (loopState == DisplayLoopState.InitiateFFProcessAndRecord)
{
loopState = DisplayLoopState.StreamingAndRecordingWhileWaitingForMessages;
encodeThread = new Thread(() => EncodeThreadInit(_camNumber: camNumber, _encodePipeName: encodePipeName, _sessionPath: sessionPath, _count: 7057600, _encodeQueue: encodeQueue));
encodeThread.Start();
}
else if (loopState == DisplayLoopState.StreamingAndRecordingWhileWaitingForMessages)
{
try
{
using (IManagedImage rawImage = managedCamera.GetNextImage())
{
streamImageCtr++;
long currFrameTimestamp = rawImage.ChunkData.Timestamp;
long currFrameID = rawImage.ChunkData.FrameID;
// Write image to pipe for encoding:
byte[] encodeImageCopy = new byte[rawImageSizeInBytes];
Marshal.Copy(source: rawImage.DataPtr, destination: encodeImageCopy, startIndex: 0, length: rawImageSizeInBytes);
encodeQueue.Enqueue(item: encodeImageCopy);
if (streamImageCtr % streamEnqueueDutyCycle == 0)
{
Mat streamImageMat = new Mat(rows: frameSize.Height, cols: frameSize.Width, type: Emgu.CV.CvEnum.DepthType.Cv8U, channels: 1, data: rawImage.DataPtr, step: frameSize.Width);
Mat streamImageMatResized = new Mat(size: streamFrameSize, type: Emgu.CV.CvEnum.DepthType.Cv8U, channels: 1);
CvInvoke.Resize(src: streamImageMat, dst: streamImageMatResized, dsize: streamFrameSize, interpolation: Emgu.CV.CvEnum.Inter.Linear);
RawMat matWithMetaData = new RawMat(frameID: currFrameID, frameTimestamp: currFrameTimestamp,
isNewBackgroundImage: false, closeForm: false, rawMat: streamImageMatResized);
streamOutputQueue.Enqueue(matWithMetaData);
streamImageMat.Dispose();
}
}
}
catch (SpinnakerException ex)
{
Console.WriteLine("Error in DisplayLoop block on camera {0}: {1}", camNumber.ToString(), ex.Message);
}
if (streamImageCtr % 10 == 0)
{
isMessageDequeueSuccess = camControlMessageQueue.TryDequeue(out ButtonCommands message);
if (isMessageDequeueSuccess)
{
if (message == ButtonCommands.StopRecording)
{
loopState = DisplayLoopState.InitiateProcessTermination;
continue;
}
else if (message == ButtonCommands.EndStreaming)
{
loopState = DisplayLoopState.EndAcquisition;
continue;
}
{
Console.WriteLine("{0} message invalid: LoopState = StreamingAndRecording.", message);
continue;
}
}
}
}
else if (loopState == DisplayLoopState.InitiateProcessTermination)
{
Console.WriteLine("InitiateProcessTermination Block reached. Back to StreamingAndWaitingForMessages.");
loopState = DisplayLoopState.StreamingAndWaitingForMessages;
}
else if (loopState == DisplayLoopState.EndAcquisition)
{
Mat emptyMat = new Mat(size: streamFrameSize, type: Emgu.CV.CvEnum.DepthType.Cv8U, channels: 1);
RawMat matWithMetaData = new RawMat(frameID: 0, frameTimestamp: 0,
isNewBackgroundImage: false, closeForm: true, rawMat: emptyMat);
managedCamera.EndAcquisition();
loopState = DisplayLoopState.WaitingForMessagesWhileNotStreaming;
Console.WriteLine("Acquisition ended on camera {0}. LoopState = WaitingForMessagesWhileNotStreaming.", camNumber.ToString());
}
else if (loopState == DisplayLoopState.Exit)
{
go = false;
CloseOryxCamera(Util.CloseCameraMethod.DeInitAndDeviceReset);
}
}
}
private void EncodeThreadInit(int _camNumber, string _encodePipeName, string _sessionPath, int _count, ConcurrentQueue<byte[]> _encodeQueue)
{
int camNumber = _camNumber;
string pipeName = _encodePipeName;
string sessionPath = _sessionPath;
int count = _count;
int nFramesWritten = 0;
ConcurrentQueue<byte[]> encodeQueue = _encodeQueue;
NamedPipeServerStream pipe = new NamedPipeServerStream(pipeName: pipeName, direction: PipeDirection.Out, maxNumberOfServerInstances: 1,
transmissionMode: PipeTransmissionMode.Byte, options: PipeOptions.Asynchronous, inBufferSize: 7057600, outBufferSize: 7057600);
string videoFileName = sessionPath + @"\" + DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss") + ".mp4";
FFProcess ffProcess = new FFProcess(pipeName: pipeName, videoFileName: videoFileName);
ffProcess.OpenWithStartInfo();
pipe.WaitForConnection();
Console.WriteLine("Pipe connected to ffProcess for camera {0} on thread {1}", camNumber.ToString(), Thread.CurrentThread.ManagedThreadId.ToString());
bool goEncode = true;
bool isDequeueSuccess;
while (goEncode)
{
isDequeueSuccess = encodeQueue.TryDequeue(out byte[] result);
if (isDequeueSuccess)
{
pipe.Write(buffer: result, offset: 0, count: count);
nFramesWritten++;
if (nFramesWritten % 1000 == 0)
{
Console.WriteLine("nFramesWritten for cam number {0}: {1}", camNumber.ToString(), nFramesWritten.ToString());
}
}
else
{
Thread.Sleep(10);
if (encodeQueue.Count == 0)
{
goEncode = false;
}
}
}
pipe.FlushAsync();
pipe.WaitForPipeDrain();
pipe.Close();
ffProcess.process.WaitForExit();
ffProcess.process.Close();
Console.WriteLine("EncodeThread for camera {0}: Pipe flushed, drained, and closed. FFProcess exited and closed.", camNumber.ToString());
if (camNumber == 0)
{
GC.Collect();
Console.WriteLine("Garbage collected on camera 1 EncodeThread. Now exiting.");
}
}
private void CloseOryxCamera(Util.CloseCameraMethod closeMethod)
{
if (!managedCamera.IsInitialized())
{
Console.WriteLine("Camera number {0} not initialized. Cannot execute DeviceReset or FactoryReset command", camNumber.ToString());
return;
}
if (managedCamera.IsStreaming())
{
managedCamera.EndAcquisition();
Console.WriteLine("EndAcquisition executed from CloseOryxCamera block on camera {0}", camNumber.ToString());
}
if (closeMethod == Util.CloseCameraMethod.DeInit)
{
managedCamera.DeInit();
Console.WriteLine("Camera number {0} deinitialized.", camNumber.ToString());
}
else if (closeMethod == Util.CloseCameraMethod.DeInitAndDeviceReset)
{
nodeMap.GetNode<ICommand>("DeviceReset").Execute();
Console.WriteLine("DeviceReset command executed on camera number {0}.", camNumber.ToString());
}
else if (closeMethod == Util.CloseCameraMethod.DeInitAndFactoryReset)
{
nodeMap.GetNode<ICommand>("FactoryReset").Execute();
Console.WriteLine("FactoryReset command executed on camera number {0}.", camNumber.ToString());
}
}
}
}