-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1639 lines (1287 loc) · 52.3 KB
/
main.c
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -----------------------------------------------------------------------
//
// (c) Copyright 1997-2014, SensoMotoric Instruments GmbH
//
// Permission is hereby granted, free of charge, to any person or
// organization obtaining a copy of the software and accompanying
// documentation covered by this license (the "Software") to use,
// reproduce, display, distribute, execute, and transmit the Software,
// and to prepare derivative works of the Software, and to permit
// third-parties to whom the Software is furnished to do so, all subject
// to the following:
//
// The copyright notices in the Software and this entire statement,
// including the above license grant, this restriction and the following
// disclaimer, must be included in all copies of the Software, in whole
// or in part, and all derivative works of the Software, unless such
// copies or derivative works are solely in the form of
// machine-executable object code generated by a source language
// processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
// NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE
// DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER
// LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
// OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// -----------------------------------------------------------------------
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
//to deal with compile error from pthreads (Win 32 version)
#define HAVE_STRUCT_TIMESPEC
#include <opencv.hpp>
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <direct.h>
#include <stdlib.h>
#include <pthread.h>
#include <iViewNG-Core.h>
#include <iViewNG-Calibration.h>
#include <iViewNG-Connection.h>
#include <iViewNG-DataAcquisition.h>
#include <iViewNG-Device-ETG.h>
#include <string.h>
#include <limits.h>
#include <sys/stat.h>
#include <errno.h>
#include <iViewNG-Utility.h>
#include "iViewNG-Convenience.h"
#include "main.h"
#include "OpenCvVisualization.h"
/* **************************************************************************************** */
/* *************************************** PROTOTYPES ************************************* */
/* **************************************************************************************** */
iViewRC ParseCommandLine (int, char **);
iViewRC Setup ();
iViewRC SetupCalib ();
iViewRC Subscribe ();
iViewRC Start ();
iViewRC Cleanup ();
iViewRC Calibrate ();
void WaitForUserInteraction ();
void MyCallback (iViewTicket * const ticket);
/* **************************************************************************************** */
/* *************************************** DATA ************************************* */
/* **************************************************************************************** */
iViewTicket * gTicketStartAcquisition = NULL;
iViewTicket * gTicketConnect = NULL;
iViewTicket * gTicketAddLicense = NULL;
iViewTicket * gTicketDeviceParameters = NULL;
iViewTicket * gTicketSubscriptionGaze = NULL;
iViewTicket * gTicketSubscriptionLeftEye = NULL;
iViewTicket * gTicketSubscriptionRightEye = NULL;
iViewTicket * gTicketSubscriptionScene = NULL;
iViewTicket * gTicketSubscriptionSceneWithGaze = NULL;
iViewTicket * gTicketSubscriptionSceneH264 = NULL;
iViewTicket * gTicketSubscriptionSceneH264WithGaze = NULL;
iViewTicket * gTicketUnsubscription = NULL;
iViewTicket * gTicketStopAcquisition = NULL;
iViewTicket * gTicketCalibration1Pt = NULL;
iViewTicket * gTicketCalibration3Pt[3];
iViewHost gServer;
// Flag which tells us that the remote iViewNG-Server is used
// in conjunction with iViewETG-Client, so this Tutorial-RemoteViewer
// will not set device parameters (as the parameters set by
// iViewETG-Client would then be overwritten).
char gRemoteIsIviewEtg = 0;
// Flag which specifies whether an explicit shutdown command
// is to be sent to the server when this tutorial ends.
char gShutdownServer = 0;
// User-provided flags
char gShowGaze = 0;
char gShowEyeImages = 0;
char gShowSceneImages = 0;
char gShowSceneH264 = 0;
char gShowSceneImagesWithGaze = 0;
char gShowSceneH264ImagesWithGaze = 0;
char gCalibrate1Pt = 0;
char gCalibrate3Pt = 0;
char gScene24 = 0;
char gScene30 = 0;
char gGazeOverlay = 0;
iViewSamplingRate gSamplingRate = IVIEWSAMPLINGRATE_CURRENT;
char gTimeMaster = 0;
iViewEyeCamExposureMode gEyeExposureMode = IVIEWEYECAMEXPOSUREMODE_CURRENT;
// Scale factors applied to the eye/scene image for downscaling.
float gScaleEyes = 1.;
float gScaleScene = 1.;
char gScaleSceneSet = 0;
const float gSamplerateEyes = 99.;
const float gSamplerateScene = 99.;
// Local data for calibration
char gCalibrationPointsToDo = 0;
unsigned int gCurrentFrameNumber = 0;
// Local data for gaze overlay
int gGazeX = 0;
int gGazeY = 0;
unsigned int gTimeOfSetupCalibMsec = 0;
unsigned int gSetupCalibCooldownMsec = 2000;
const unsigned int TICKET_WAIT_MS = 5000;
using namespace cv;
vector<int> compression_params;
// File handling: Rakshit
std::ofstream GazeFile;
char str2Write[1000];
// Image writeout parameters: Rakshit
string RightEyeImageLoc;
string LeftEyeImageLoc;
string SceneImageLoc;
string pathToGazeText;
string pathToGaze;
// Queue data structures: Brendan
Queue_Eye LeftEyeQueue;
Queue_Eye RightEyeQueue;
Queue_Scene SceneQueue;
// Mutex objects, for locking of image queue: Brendan
pthread_mutex_t mutexEyeL = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutexEyeR = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutexScene = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutexFlag = PTHREAD_MUTEX_INITIALIZER;
// Int to work as data collection flag. 1 if data is being collected. Set to 0 when done.
int studyFlag;
/* **************************************************************************************** */
/* *************************************** FUNCTIONS ************************************* */
/* **************************************************************************************** */
int main (int argc, char ** argv) {
printf("%d \n", argc);
string prName;
string taskNum;
cout << "Enter subject name: ";
getline(cin, prName);
cout << "Enter task number: ";
getline(cin, taskNum);
RightEyeImageLoc = "C:\\Users\\Rakshit\\Documents\\ETGData\\" + prName +"\\" + taskNum + "\\RightEyeImages";
LeftEyeImageLoc = "C:\\Users\\Rakshit\\Documents\\ETGData\\" + prName +"\\" + taskNum + "\\LeftEyeImages";
SceneImageLoc = "C:\\Users\\Rakshit\\Documents\\ETGData\\" + prName +"\\" + taskNum + "\\SceneImages";
pathToGaze = "C:\\Users\\Rakshit\\Documents\\ETGData\\" + prName +"\\" + taskNum + "\\GazeData";
pathToGazeText = pathToGaze + "\\Gaze_Data.txt";
if (mkdir_p(RightEyeImageLoc.c_str()) == 0)
{
mkdir_p(LeftEyeImageLoc.c_str());
mkdir_p(SceneImageLoc.c_str());
mkdir_p(pathToGaze.c_str());
printf("Directories successfully created");
}
else
{
printf("Directories already existed. Deleting.");
_rmdir(RightEyeImageLoc.c_str());
_rmdir(LeftEyeImageLoc.c_str());
_rmdir(SceneImageLoc.c_str());
_rmdir(pathToGaze.c_str());
mkdir_p(RightEyeImageLoc.c_str());
mkdir_p(LeftEyeImageLoc.c_str());
mkdir_p(SceneImageLoc.c_str());
mkdir_p(pathToGaze.c_str());
printf("Directories successfully created");
}
cout << RightEyeImageLoc << "\n";
cout << LeftEyeImageLoc << "\n";
cout << SceneImageLoc << "\n";
cout << pathToGazeText << "\n";
// Compression parameters
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(50);
iViewRC rc;
memset (&gServer, 0, sizeof(iViewHost));
if ( (rc = ParseCommandLine (argc, argv)) || (rc = Setup ()) || (rc = Subscribe ()) || (rc = Start ()) || (rc = SetupCalib ()))
return RC_OPERATION_FAILED;
printf ("Receiving data...\n");
// Create file for writing Gaze Data
GazeFile.open(pathToGazeText);
GazeFile << "R_EyeBallUncert,R_PupilConf,L_EyeBallUncert,L_PupilConf,EyeFrameNumber,SceneFrameNumber,serverTime,BporX,BporY,GV_R_x,GV_R_y,GV_R_z,GV_L_x,GV_L_y,GV_L_z,Year,Month,Day,Hour,Minute,Second,Millisecond\n";
// Create queue's for scene and eye images
//LeftEyeQueue = createEyeQueueL();111
//RightEyeQueue = createEyeQueueR();
//SceneQueue = createSceneQueue();
//studyFlag = 1;
WaitForUserInteraction();
// destroy the queue's
//LeftEyeQueue.destroy_Eye(&LeftEyeQueue);
//RightEyeQueue.destroy_Eye(&RightEyeQueue);
//SceneQueue.destroy_Scene(&SceneQueue);
printf ("Cleaning up...\n");
Cleanup ();
GazeFile.close();
return 0;
}
/* **************************************************************************************** */
// Personal make directory code
int mkdir_p(const char *path)
{
const int PATH_MAX = 256;
/* Adapted from http://stackoverflow.com/a/2336245/119527 */
const size_t len = strlen(path);
char _path[PATH_MAX];
char *p;
errno = 0;
/* Copy string so its mutable */
if (len > sizeof(_path) - 1) {
errno = ENAMETOOLONG;
return -1;
}
strcpy(_path, path);
/* Iterate the string */
for (p = _path + 1; *p; p++) {
if (*p == '\\') {
/* Temporarily truncate */
*p = '\0';
if (_mkdir(_path) != 0) {
if (errno != EEXIST)
return -1;
}
*p = '/';
}
}
if (_mkdir(_path) != 0) {
if (errno != EEXIST)
return -1;
}
return 0;
}
/**
* Create and initiate a scene queue
*/
Queue_Scene createSceneQueue () {
Queue_Scene queue;
queue.size = 0;
queue.head = NULL;
queue.tail = NULL;
queue.push_Scene = &push_Scene;
queue.pop_Scene = &pop_Scene;
queue.destroy_Scene = &destroy_SceneQueue;
return queue;
}
/**
* Create and initiate an eye queue
*/
Queue_Eye createEyeQueueL () {
Queue_Eye queue;
queue.size = 0;
queue.head = NULL;
queue.tail = NULL;
queue.push_Eye = &push_EyeL;
queue.pop_Eye = &pop_EyeL;
queue.destroy_Eye = &destroy_EyeQueue;
return queue;
}
/**
* Create and initiate an eye queue
*/
Queue_Eye createEyeQueueR() {
Queue_Eye queue;
queue.size = 0;
queue.head = NULL;
queue.tail = NULL;
queue.push_Eye = &push_EyeR;
queue.pop_Eye = &pop_EyeR;
queue.destroy_Eye = &destroy_EyeQueue;
return queue;
}
/**
* Push an item into scene queue, if this is the first item,
* both queue->head and queue->tail will point to it,
* otherwise the oldtail->next and tail will point to it.
*/
void push_Scene (Queue_Scene* queue, iViewDataStreamSceneImage* img) {
// Create a new node
Node_Scene* n = (Node_Scene*) malloc (sizeof(Node_Scene));
n->img = img;
n->next = NULL;
if (queue->head == NULL) { // no head
queue->head = n;
} else{
queue->tail->next = n;
}
queue->tail = n;
queue->size++;
}
/**
* Return and remove the first item from scene queue
*/
iViewDataStreamSceneImage* pop_Scene (Queue_Scene* queue) {
// get the first item
Node_Scene* head = queue->head;
iViewDataStreamSceneImage* img = head->img;
// move head pointer to next node, decrease size
queue->head = head->next;
queue->size--;
// free the memory of original head
free(head);
return img;
}
/**
* Push an item into scene queue, if this is the first item,
* both queue->head and queue->tail will point to it,
* otherwise the oldtail->next and tail will point to it.
*/
void push_EyeL(Queue_Eye* queue, iViewDataStreamEyeImage* img) {
// Create a new node
Node_Eye* n = (Node_Eye*)malloc(sizeof(Node_Eye));
n->img = img;
n->next = NULL;
if (queue->head == NULL) { // no head
queue->head = n;
}
else {
queue->tail->next = n;
}
queue->tail = n;
queue->size++;
}
/**
* Push an item into scene queue, if this is the first item,
* both queue->head and queue->tail will point to it,
* otherwise the oldtail->next and tail will point to it.
*/
void push_EyeR (Queue_Eye* queue, iViewDataStreamEyeImage* img) {
// Create a new node
Node_Eye* n = (Node_Eye*) malloc (sizeof(Node_Eye));
n->img = img;
n->next = NULL;
if (queue->head == NULL) { // no head
queue->head = n;
} else{
queue->tail->next = n;
}
queue->tail = n;
queue->size++;
}
/**
* Return and remove the first item from scene queue
*/
iViewDataStreamEyeImage* pop_EyeL(Queue_Eye* queue) {
// get the first item
Node_Eye* head = queue->head;
iViewDataStreamEyeImage* img = head->img;
// move head pointer to next node, decrease size
queue->head = head->next;
queue->size--;
// free the memory of original head
free(head);
return img;
}
/**
* Return and remove the first item from scene queue
*/
iViewDataStreamEyeImage* pop_EyeR (Queue_Eye* queue) {
// get the first item
Node_Eye* head = queue->head;
iViewDataStreamEyeImage* img = head->img;
// move head pointer to next node, decrease size
queue->head = head->next;
queue->size--;
// free the memory of original head
free(head);
return img;
}
void destroy_EyeQueue(Queue_Eye* queue) {
iViewDataStreamEyeImage* temp;
//destroy by popping until empty
while (queue->size > 0) {
temp = queue->pop_Eye(queue);
free(temp);
}
}
void destroy_SceneQueue(Queue_Scene* queue) {
iViewDataStreamSceneImage* temp;
//destroy by popping until empty
while (queue->size > 0) {
temp = queue->pop_Scene(queue);
free(temp);
}
}
static void * _cdecl worker_EyeThreadL(void * param) {
iViewDataStreamEyeImage* imgToWriteL;
int valL = 0;
int doLoopL = 1;
while (doLoopL == 1)
{
// first grab lock, check size. If has element then pop it and save off
pthread_mutex_lock(&mutexEyeL);
if (LeftEyeQueue.size > 0) {
imgToWriteL = LeftEyeQueue.pop_Eye(&LeftEyeQueue);
valL = 1;
}
pthread_mutex_unlock(&mutexEyeL);
if (valL == 1) {
if (imgToWriteL->imageData != NULL) {
writeImage(imgToWriteL->imageData, imgToWriteL->eyeFrameNumber, LeftEyeImageLoc, compression_params);
}
valL = 0;
}
//optionally could add small time (ms) sleep here for worker thread
//Currently use another variable that signifies that the study is over, then can call pthread_exit() here
pthread_mutex_lock(&mutexFlag);
if (studyFlag == 0) {
doLoopL = 0;
//pthread_exit(0);
}
pthread_mutex_unlock(&mutexFlag);
}
return NULL;
}
static void * _cdecl worker_EyeThreadR(void * param) {
iViewDataStreamEyeImage* imgToWriteR;
int valR = 0;
int doLoopR = 1;
while (doLoopR == 1)
{
// first grab lock, check size. If has element then pop it and save off
pthread_mutex_lock(&mutexEyeR);
if (RightEyeQueue.size > 0) {
imgToWriteR = RightEyeQueue.pop_Eye(&RightEyeQueue);
valR = 1;
}
pthread_mutex_unlock(&mutexEyeR);
if (valR == 1) {
//Lets do a null check on the image data
if (imgToWriteR->imageData != NULL){
writeImage(imgToWriteR->imageData, imgToWriteR->eyeFrameNumber, RightEyeImageLoc, compression_params);
}
valR = 0;
//bjohn: do I need to free this ptr?
//free(imgToWriteR);
//imgToWriteR = NULL;
}
//optionally could add small time (ms) sleep here for worker thread
pthread_mutex_lock(&mutexFlag);
if (studyFlag == 0) {
doLoopR = 0;
//pthread_exit(0);
}
pthread_mutex_unlock(&mutexFlag);
}
return NULL;
}
static void * _cdecl worker_SceneThread(void * param) {
iViewDataStreamSceneImage* imgToWrite;
int val = 0;
int doLoop = 1;
while (doLoop == 1)
{
// first grab lock, check size. If has element then pop it and save off
pthread_mutex_lock(&mutexScene);
if (SceneQueue.size > 0) {
imgToWrite = SceneQueue.pop_Scene(&SceneQueue);
val = 1;
}
pthread_mutex_unlock(&mutexScene);
if (val == 1) {
if (imgToWrite->imageData != NULL) {
writeImage(imgToWrite->imageData, imgToWrite->sceneFrameNumber, SceneImageLoc, compression_params);
}
val = 0;
}
//optionally could add small time (ms) sleep here for worker thread
pthread_mutex_lock(&mutexFlag);
if (studyFlag == 0) {
doLoop = 0;
//pthread_exit(0);
}
pthread_mutex_unlock(&mutexFlag);
}
return NULL;
}
/* **************************************************************************************** */
/**
* This function will be called from MyCallback() when a new gaze sample is available.
*/
void handleGazeSample (iViewDataStreamGazeSample const * const gazeSample) {
// round the x and y coordinate
gGazeX = (int) (gazeSample->pointOfRegard.x + 0.5);
gGazeY = (int) (gazeSample->pointOfRegard.y + 0.5);
// Print to console only if requested.
if (gShowGaze)
sprintf(str2Write,"%f,%f,%f,%f,%u,%u,%u,%d,%d,%f,%f,%f,%f,%f,%f,%04d,%02d,%02d,%02d,%02d,%02d,%03d\n",
gazeSample->rightEye.eyeballUncertainty, gazeSample->rightEye.pupilConfidence, gazeSample->leftEye.eyeballUncertainty, gazeSample->leftEye.pupilConfidence,
gazeSample->eyeFrameNumber, gazeSample->sceneFrameNumber,
(uint32_t) (gazeSample->timestamp / 1000000), gGazeX, gGazeY,
gazeSample->rightEye.gazeDirection.x, gazeSample->rightEye.gazeDirection.y, gazeSample->rightEye.gazeDirection.z,
gazeSample->leftEye.gazeDirection.x, gazeSample->leftEye.gazeDirection.y, gazeSample->leftEye.gazeDirection.z,
gazeSample->year, gazeSample->month, gazeSample->day,
gazeSample->hour, gazeSample->minute, gazeSample->second,
gazeSample->millisecond);
GazeFile << str2Write;
return;
}
/* **************************************************************************************** */
/**
* This function will be called from MyCallback() when a new eye image is available.
*/
void handleEyeImage (iViewDataStreamEyeImage * image) {
switch (image->eye) {
case EYE_LEFT:
//displayLeftEyeImage (image->imageData);
//writeImage(image->imageData, image->eyeFrameNumber, LeftEyeImageLoc, compression_params);
//bjohn: instead of writing image we add to queue using mutex lock.
//pthread_mutex_lock(&mutexEyeL);
//LeftEyeQueue.push_Eye(&LeftEyeQueue, image);
//pthread_mutex_unlock(&mutexEyeL);
break;
case EYE_RIGHT:
//displayRightEyeImage (image->imageData);
writeImage(image->imageData, image->eyeFrameNumber, RightEyeImageLoc, compression_params);
//bjohn: instead of writing image we add to queue using mutex lock.
//pthread_mutex_lock(&mutexEyeR);
//RightEyeQueue.push_Eye(&RightEyeQueue, image);
//pthread_mutex_unlock(&mutexEyeR);
break;
case EYE_UNKNOWN:
fprintf (stderr, "ERROR in callback: unknown eye type.\n");
break;
}
return;
}
/* **************************************************************************************** */
/**
* This function will be called from MyCallback() when a new scene image is available.
*/
void handleSceneImageWithGaze (iViewDataStreamSceneImage * image) {
if (gCalibrationPointsToDo <= 3 && gCalibrationPointsToDo >= 1)
{
drawGazeOverlay(image->imageData, gGazeX, gGazeY);
gCurrentFrameNumber = image->sceneFrameNumber;
displaySceneImage(image->imageData);
if (gCalibrationPointsToDo == 0)
{
closeSceneImageDisplay();
}
}
writeImage(image->imageData, image->sceneFrameNumber, SceneImageLoc, compression_params);
//bjohn: instead of writing image we add to queue using mutex lock.
//pthread_mutex_lock(&mutexScene);
//SceneQueue.push_Scene(&SceneQueue, image);
//pthread_mutex_unlock(&mutexScene);
return;
}
/* **************************************************************************************** */
/**
* This function will be called from MyCallback() when a new scene image is available.
*/
void handleH264DecodedSceneImage (iViewDataStreamSceneImage * image) {
//gCurrentFrameNumber = image->sceneFrameNumber;
/*
// Draw gaze onto scene only if requested.
if (gShowSceneImagesWithGaze || gShowSceneH264ImagesWithGaze) {
drawGazeOverlay (image->imageData, gGazeX, gGazeY);
}
*/
//displaySceneImage (image->imageData);
writeImage(image->imageData, image->sceneFrameNumber, SceneImageLoc, compression_params);
//bjohn: instead of writing image we add to queue using mutex lock.
//pthread_mutex_lock(&mutexScene);
//SceneQueue.push_Scene(&SceneQueue, image);
//pthread_mutex_unlock(&mutexScene);
return;
}
/* **************************************************************************************** */
/**
* The callback analyses the result type and delegates operation to dedicated event handler.
*/
void MyCallback (iViewTicket * const ticket) {
//cout<<"Func call "<<ticket->functionName<<"\n";
// this should never happen
if (NULL == ticket)
return;
// extract the result
iViewResult const * const result = ticket->result;
// Check if we got a 1pt calibration response
if (gTicketCalibration1Pt == ticket) {
printf ("Sending 1pt calibration data %s successful.\n", RC_NO_ERROR == ticket->returnCode ? "was" : "was not");
iView_ReleaseTicket( &gTicketCalibration1Pt );
return;
}
// Check if we got a 3pt calibration response
for (int i=0; i<3; i++) {
if (gTicketCalibration3Pt[i] == ticket) {
printf ("Sending 3pt calibration data %s successful.\n", RC_NO_ERROR == ticket->returnCode ? "was" : "was not");
iView_ReleaseTicket( &gTicketCalibration3Pt[i] );
return;
}
}
// if the ticket carries no result or if it's not a data stream ticket, we're not interested
if (NULL == result || IVIEWRESULT_SUBSCRIBE_DATASTREAM != result->type)
return;
// cast to the proper result type
iViewDataStream const * const stream = (iViewDataStream const *) result->data;
if(stream->lastStreamEntity == IVIEWDATASTREAM_END){
fprintf(stderr,"Got last stream Entity\n");
return;
}
//cout << "Stream type" << stream->type << "\n";
switch (stream->type) {
case IVIEWDATASTREAM_GAZE_INFORMATION:
handleGazeSample ((iViewDataStreamGazeSample const *) stream->data);
break;
case IVIEWDATASTREAM_EYEIMAGES_LEFT:
handleEyeImage ((iViewDataStreamEyeImage*) stream->data);
break;
case IVIEWDATASTREAM_EYEIMAGES_RIGHT:
handleEyeImage ((iViewDataStreamEyeImage*) stream->data);
break;
case IVIEWDATASTREAM_SCENEIMAGES:
case IVIEWDATASTREAM_SCENEIMAGES_WITH_GAZE:
case IVIEWDATASTREAM_SCENEIMAGES_H264_DECODED_WITH_GAZE:
handleSceneImageWithGaze ((iViewDataStreamSceneImage*) stream->data);
break;
case IVIEWDATASTREAM_SCENEIMAGES_H264_DECODED:
handleH264DecodedSceneImage ((iViewDataStreamSceneImage*) stream->data);
break;
}
iView_ReleaseResult (ticket);
return;
}
/* **************************************************************************************** */
void Usage (char const * const cmd) {
fprintf (stderr, "\nUSAGE\n\t%s [OPTION]...\n\n", cmd ? cmd : "");
fprintf(stderr, "OPTIONS\n\n");
fprintf(stderr, "\t--help to print this information\n");
fprintf(stderr, "\t--show-gaze to print the gaze coordinates to the console\n");
fprintf(stderr, "\t--show-eyes to open two windows displaying the eye images\n");
fprintf(stderr, "\t--scale-eyes f rescale eye images by multiplying image size by f\n");
fprintf(stderr, "\t--show-scene to open a window displaying the scene images (the\n");
fprintf(stderr, "\t scene video is decoded by the server and full image\n");
fprintf(stderr, "\t will be transferred to the remote viewer)\n");
fprintf(stderr, "\t--show-scene-h264 to open a window displaying the scene images (the\n");
fprintf(stderr, "\t scene video is transferred as H.264 stream,\n");
fprintf(stderr, "\t decoded by the remote viewer)\n");
fprintf(stderr, "\t--show-scene-with-gaze to open a window displaying the scene images (the\n");
fprintf(stderr, "\t scene video is decoded by the server and already\n");
fprintf(stderr, "\t contains the gaze cursor, full image is transferred to\n");
fprintf(stderr, "\t the remote viewer)\n");
fprintf(stderr, "\t--show-scene-h264-with-gaze to open a window displaying the scene images\n");
fprintf(stderr, "\t (the scene video is transferred as H.264 stream,\n");
fprintf(stderr, "\t decoded by the client and contains a gaze cursor\n");
fprintf(stderr, "\t overlay created by the client)\n");
fprintf(stderr, "\t--scale-scene f rescale scene images by multiplying image size by f\n");
fprintf(stderr, "\t--server ip_address connect to the specified remote iViewNG server,\n");
fprintf(stderr, "\t the server can be setup (device parameters) by this\n");
fprintf(stderr, "\t Tutorial-RemoteViewer\n");
fprintf(stderr, "\t--iviewetg ip_address connect to the specified remote iViewNG server\n");
fprintf(stderr, "\t which had already been setup by an iViewETG-Client.\n");
fprintf(stderr, "\t This Tutorial-RemoteViewer will not submit any new device\n");
fprintf(stderr, "\t parameters, it will only try to subscribe and listen to\n");
fprintf(stderr, "\t the data it wants. Whether the data can be received\n");
fprintf(stderr, "\t depends on the parameters that iViewETG-Client has set\n");
fprintf(stderr, "\t--calibrate1pt to perform a 1-point calibration\n");
fprintf(stderr, "\t--calibrate3pt to perform a 3-point calibration\n");
fprintf(stderr, "\t--samplingrate n set eye tracking sampling rate to 30 or 60Hz or 120hz\n");
fprintf(stderr, "\t--scene30 scene 30hz\n");
fprintf(stderr, "\t--scene24 scene 24hz\n");
fprintf(stderr, "\t--gazeoverlay turns on gaze overlay mode. if you have wireless\n");
fprintf(stderr, "\t observation license, this is the only mode you can use.\n");
fprintf(stderr, "\t--shutdownserver specifies that the server is to be shutdown by an\n");
fprintf(stderr, "\t explicit command; if not specified, server's behavior\n");
fprintf(stderr, "\t defines how it will behave when this client leaves\n");
fprintf(stderr, "\t--timemaster specifies that this sdk instance is the time master of\n");
fprintf(stderr, "\t the server it connects to. If not set, the server will\n");
fprintf(stderr, "\t use its local date and time to stamp the gaze samples.\n");
fprintf(stderr, "\t User must ensure there is only one time master running\n");
fprintf(stderr, "\t at a time (otherwise the times will compete).\n");
fprintf(stderr, "\t--ica to turn on the ICA recording mode.\n");
fprintf(stderr, "\t The ICA Recording Mode summarizes custom hardware\n");
fprintf(stderr, "\t settings to especially enable optimal results when\n");
fprintf(stderr, "\t the SMI ETG are used in conjunction with the Index\n");
fprintf(stderr, "\t of Cognitive Activity from Eye Tracking Inc.\n");
fprintf(stderr, "\t This mode might lead to a limited robustness\n");
fprintf(stderr, "\t against external IR light. Please make sure that\n");
fprintf(stderr, "\t you use this mode only in combination with the ICA.\n");
fprintf(stderr, "\t This mode does not run with 120 Hz.\n");
fprintf(stderr, "\t--ica-off turns off ICA recording mode.\n");
fprintf(stderr, "\t If you have enabled it before, you can turn it off.\n");
exit (-1);
}
/* **************************************************************************************** */
iViewRC ParseCommandLine (int argc, char ** argv) {
// if not directed to server, use local server
gServer.connectionType = IVIEW_SERVERADRRESS_SHAREDMEMORY;
// check each parameter
for (int i = 1; i < argc; i++) {
//cout<<"<<<- arg "<<argv[i]<<"\n";
if (argv [i] [0] == '-') {
if (0 == strncmp (argv [i], "--help", MAX (strlen (argv [i]), strlen ("--help")))) {
Usage (argv [0]);
}
if (0 == strncmp (argv [i], "--server", MAX (strlen (argv [i]), strlen ("--server")))) {
if (argc <= ++i) {
fprintf (stderr, "ERROR: missing argument for parameter '--server'");
Usage(argv[0]);
}
gServer.connectionType = IVIEW_SERVERADRRESS_IPV4;
unsigned int argLen = strlen(argv [i]);
strncpy (gServer.hostAddress.ipAddress.ipv4, argv [i], MIN (argLen, HOSTADDRESSLENGTH_IPV4 - 1));
gServer.hostAddress.port = 0;
printf ("Server host: '%s:%u'\n", gServer.hostAddress.ipAddress.ipv4, gServer.hostAddress.port);
continue;
}
if (0 == strncmp (argv [i], "--iviewetg", MAX (strlen (argv [i]), strlen ("--iviewetg")))) {
if (argc <= ++i) {
fprintf (stderr, "ERROR: missing argument for parameter '--iviewetg'");
Usage(argv[0]);
}
gServer.connectionType = IVIEW_SERVERADRRESS_IPV4;
unsigned int argLen = strlen(argv [i]);
strncpy (gServer.hostAddress.ipAddress.ipv4, argv [i], MIN (argLen, HOSTADDRESSLENGTH_IPV4 - 1));
gServer.hostAddress.port = 0;
gRemoteIsIviewEtg = 1;
printf ("Server (with iViewETG) host: '%s:%u'\n", gServer.hostAddress.ipAddress.ipv4, gServer.hostAddress.port);
continue;
}
if (0 == strncmp (argv [i], "--samplingrate", MAX (strlen (argv [i]), strlen ("--samplingrate")))) {
if (argc <= ++i) {
fprintf (stderr, "ERROR: missing argument for parameter '--samplingrate'");
Usage(argv[0]);
}
switch (atoi(argv [i])) {
case 30:
gSamplingRate = IVIEWSAMPLERATE_ETG_30;
break;
case 60:
gSamplingRate = IVIEWSAMPLERATE_ETG_60;
break;
case 120:
gSamplingRate = IVIEWSAMPLERATE_ETG_120;
break;
default:
fprintf (stderr, "Invalid sampling rate: neither '30' nor '60' nor '120'.\n");
Usage(argv[0]);
}
continue;
}
if (0 == strncmp (argv [i], "--show-gaze", MAX (strlen (argv [i]), strlen ("--show-gaze")))) {
gShowGaze = 1;
continue;
}
if (0 == strncmp (argv [i], "--show-eyes", MAX (strlen (argv [i]), strlen ("--show-eyes")))) {
gShowEyeImages = 1;
continue;
}
//cout<<" parameter "<<argv [i]<<"\n";
if (0 == strncmp (argv [i], "--show-scene", MAX (strlen (argv [i]), strlen ("--show-scene")))) {
gShowSceneImages = 1;
continue;
}
if (0 == strncmp (argv [i], "--show-scene-h264", MAX (strlen (argv [i]), strlen ("--show-scene-h264")))) {
gShowSceneH264 = 1;
continue;
}
if (0 == strncmp (argv [i], "--show-scene-with-gaze", MAX (strlen (argv [i]), strlen ("--show-scene-with-gaze")))) {
gShowSceneImagesWithGaze = 1;
continue;
}
if (0 == strncmp (argv [i], "--show-scene-h264-with-gaze", MAX (strlen (argv [i]), strlen ("--show-scene-h264-with-gaze")))) {
gShowSceneH264ImagesWithGaze = 1;
continue;
}
if (0 == strncmp (argv [i], "--calibrate1pt", MAX (strlen (argv [i]), strlen ("--calibrate1pt")))) {
gCalibrate1Pt = 1;
continue;
}
if (0 == strncmp (argv [i], "--calibrate3pt", MAX (strlen (argv [i]), strlen ("--calibrate3pt")))) {
gCalibrate3Pt = 1;
continue;
}
if (0 == strncmp (argv [i], "--scale-eyes", MAX (strlen (argv [i]), strlen ("--scale-eyes")))) {
if (argc <= ++i) {
fprintf (stderr, "ERROR: missing argument for parameter '--scale-eyes'");
Usage(argv[0]);
}
gScaleEyes = atof(argv[i]);
continue;
}
if (0 == strncmp (argv [i], "--scale-scene", MAX (strlen (argv [i]), strlen ("--scale-scene")))) {
if (argc <= ++i) {
fprintf (stderr, "ERROR: missing argument for parameter '--scale-scene'");
Usage(argv[0]);
}
gScaleScene = atof(argv[i]);
gScaleSceneSet = 1;
continue;
}
if (0 == strncmp (argv [i], "--scene24", MAX (strlen (argv [i]), strlen ("--scene24")))) {
gScene24 = 1;
continue;
}
if (0 == strncmp (argv [i], "--scene30", MAX (strlen (argv [i]), strlen ("--scene30")))) {
gScene30 = 1;
continue;
}