-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinectRecording.h
executable file
·227 lines (182 loc) · 4.99 KB
/
KinectRecording.h
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
#ifndef __KINECT_RECORDING_H__
#define __KINECT_RECORDING_H__
#include <Messaging/Serializable.h>
#include <System/Mutex.h>
#include <System/LockManagement.h>
#include "KinectBasics.h"
#include <sys/timeb.h>
class KinectRecording : public Omiscid::Serializable
{
public:
FILE* TimestampedFile;
bool RawRecording;
FILE * RawFile;
// Keep trace of BufferSize in RawMode
unsigned int BufferSize;
bool InitDescription;
FILE * DescriptionFile;
double StartTime;
unsigned int InputNumber;
TIMESPAN LastFrameTime;
void * BufferData;
float FrameRate;
Omiscid::SimpleString FilePrefix;
bool IsActive;
Omiscid::Mutex InternalProtection;
static void CloseAndSetNull(FILE*& fd)
{
if ((fd) != (FILE*)NULL)
{
fclose(fd);
fd = (FILE*)NULL;
}
}
KinectRecording()
{
Init( "" );
}
KinectRecording(const Omiscid::SimpleString& Prefix)
{
Init( Prefix );
}
virtual ~KinectRecording()
{
StopRecording(0.0);
if ( BufferData != nullptr )
{
delete BufferData;
}
}
void Init(const Omiscid::SimpleString& Prefix, bool doRawRecord = false )
{
Omiscid::SmartLocker InternalProtection_SL(InternalProtection);
// For standard recording
FilePrefix = Prefix;
TimestampedFile = (FILE*)nullptr;
InputNumber = 0;
StartTime = 0.0;
FrameRate = 0.0;
LastFrameTime = 0;
BufferData = nullptr;
// Raw recording
RawRecording = doRawRecord;
RawFile = (FILE*)nullptr;
BufferSize = 0;
InitDescription = true;
DescriptionFile = (FILE*)nullptr;
// Set if I am active
IsActive = FilePrefix != "";
}
virtual void DeclareSerializeMapping()
{
// TODO : change it for skeleton, better description
AddToSerialization("EventFrameRate", FrameRate);
}
void AllocateBuffer(size_t SizeOfBuffer)
{
if ( SizeOfBuffer != 0 )
{
BufferData = (void*)new char[SizeOfBuffer];
}
}
virtual bool StartRecording( const char* SessionFolder )
{
Omiscid::SmartLocker InternalProtection_SL(InternalProtection);
Omiscid::SimpleString str;
Omiscid::SimpleString str2;
InputNumber = 0;
InitDescription = true;
StartTime = 0;
LastFrameTime = 0;
// Create session folder
if ( CreateDirectory(SessionFolder, NULL) == FALSE && GetLastError() != ERROR_ALREADY_EXISTS)
{
fprintf( stderr, "Could not create '%s' folder\n", SessionFolder );
return false;
}
// Prefix
str = SessionFolder;
str += "/" + FilePrefix;
// Create folder if not already done
if ( CreateDirectory(str.GetStr(), NULL) == FALSE && GetLastError() != ERROR_ALREADY_EXISTS)
{
fprintf( stderr, "Could not create '%s' folder\n", str.GetStr() );
return false;
}
// Set root name
str += "/" + FilePrefix;
// Create standard timestamp file
// str equals for instance "depth/depth."
str2 = str;
str2 += ".timestamp";
TimestampedFile = fopen(str2.GetStr(), "wb");
if ( TimestampedFile == nullptr )
{
fprintf( stderr, "Could not create '%s' timestamp file\n", str2.GetStr() );
return false;
}
// Create Raw and Description file if mandatory
if ( RawRecording == true )
{
str2 = str + ".raw";
RawFile = fopen(str2.GetStr(), "wb");
if ( RawFile == nullptr )
{
fprintf( stderr, "Could not create '%s' raw file\n", str2.GetStr() );
CloseAndSetNull(TimestampedFile);
return false;
}
str2 = str + ".desc";
DescriptionFile = fopen(str2.GetStr(), "wb");
if ( DescriptionFile == nullptr )
{
fprintf( stderr, "Could not create '%s' desc file\n", str2.GetStr() );
CloseAndSetNull(TimestampedFile);
CloseAndSetNull(RawFile);
return false;
}
}
return true;
}
virtual void StopRecording(double CurrentTime)
{
Omiscid::SmartLocker InternalProtection_SL(InternalProtection);
if ( RawRecording == true )
{
CloseAndSetNull(DescriptionFile);
CloseAndSetNull(RawFile);
}
CloseAndSetNull(TimestampedFile);
}
void SaveDataAndIncreaseInputNumber( const struct timeb& lTimestamp, char * SuppInfo /* = nullptr */ )
{
Omiscid::SmartLocker InternalProtection_SL(InternalProtection);
if ( RawFile != (FILE*)nullptr )
{
if ( BufferSize != 0 )
{
while (fwrite(BufferData, BufferSize, 1, RawFile) != 1) {}
}
SaveTimestamp(TimestampedFile, lTimestamp, InputNumber, LastFrameTime, SuppInfo);
}
// We had an input
InputNumber++;
}
// Static utility function
static void SaveTimestamp(FILE* f, const struct timeb& lTimestamp, unsigned int numFrame, TIMESPAN FrameTime, char * SuppInfo /* = nullptr */ )
{
if ( f == (FILE*)nullptr)
{
return;
}
if ( SuppInfo != (char *)nullptr )
{
fprintf(f, "%d.%03d %u, %s, %I64d\n", (int)lTimestamp.time, (int)lTimestamp.millitm, numFrame, SuppInfo, FrameTime);
}
else
{
fprintf(f, "%d.%03d %u, %I64d\n", (int)lTimestamp.time, (int)lTimestamp.millitm, numFrame, FrameTime);
}
}
};
#endif // __KINECT_RECORDING_H__