forked from PrimeSense/Sensor
/
XnDeviceBase.h
308 lines (251 loc) · 14.5 KB
/
XnDeviceBase.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
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
/****************************************************************************
* *
* PrimeSense Sensor 5.x Alpha *
* Copyright (C) 2011 PrimeSense Ltd. *
* *
* This file is part of PrimeSense Sensor. *
* *
* PrimeSense Sensor is free software: you can redistribute it and/or modify*
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* PrimeSense Sensor is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with PrimeSense Sensor. If not, see <http://www.gnu.org/licenses/>.*
* *
****************************************************************************/
#ifndef __XN_DEVICE_BASE_H__
#define __XN_DEVICE_BASE_H__
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnDDK/IXnDevice.h>
#include <XnStringsHash.h>
#include <XnDevice.h>
#include <XnDDK/XnDeviceModule.h>
#include "XnDeviceModuleHolder.h"
#include <XnEvent.h>
#include <XnDDK/XnDeviceStream.h>
#include <XnDDK/XnActualStringProperty.h>
#include <XnDDK/XnActualIntProperty.h>
#include <XnDDK/XnActualGeneralProperty.h>
//---------------------------------------------------------------------------
// Defines
//---------------------------------------------------------------------------
#define XN_MASK_DEVICE "Device"
#define XN_DEVICE_BASE_MAX_STREAMS_COUNT 100
//---------------------------------------------------------------------------
// Types
//---------------------------------------------------------------------------
class XN_DDK_CPP_API XnDeviceBase : public IXnDevice
{
public:
XnDeviceBase(const XnChar* csName, XnBool bStrictProperties);
~XnDeviceBase();
//---------------------------------------------------------------------------
// Properties Getters
//---------------------------------------------------------------------------
inline XnActualIntProperty& ReadWriteModeProperty() { return m_ReadWriteMode; }
inline XnActualStringProperty& PrimaryStreamProperty() { return m_PrimaryStream; }
inline XnActualIntProperty& DeviceMirrorProperty() { return m_DeviceMirror; }
//---------------------------------------------------------------------------
// Getters
//---------------------------------------------------------------------------
inline XnDeviceMode GetReadWriteMode() const { return (XnDeviceMode)m_ReadWriteMode.GetValue(); }
inline const XnChar* GetPrimaryStream() const { return m_PrimaryStream.GetValue(); }
inline XnBool GetDeviceMirror() const { return (XnBool)m_DeviceMirror.GetValue(); }
inline XnBool IsHighResTimestamps() const { return (XnBool)m_HighResTimestamps.GetValue(); }
inline XnDeviceModule* DeviceModule() { return m_pDevicePropertiesHolder->GetModule(); }
inline XnDeviceModuleHolder* DeviceModuleHolder() { return m_pDevicePropertiesHolder; }
//---------------------------------------------------------------------------
// Setters
//---------------------------------------------------------------------------
virtual XnStatus SetPrimaryStream(const XnChar* strPrimaryStream);
virtual XnStatus SetMirror(XnBool bMirror);
virtual XnStatus SetHighresTimestamps(XnBool bHighRes);
//---------------------------------------------------------------------------
// Helpers
//---------------------------------------------------------------------------
static inline XnDeviceBase* GetFromDeviceHandle(XnDeviceHandle DeviceHandle) { return (XnDeviceBase*)DeviceHandle; }
//---------------------------------------------------------------------------
// IXnDevice Methods
//---------------------------------------------------------------------------
virtual XnStatus Init(const XnDeviceConfig* pDeviceConfig);
virtual XnStatus Destroy();
virtual XnStatus GetSupportedStreams(const XnChar** aStreamNames, XnUInt32* pnStreamNamesCount);
virtual XnStatus CreateStream(const XnChar* StreamType, const XnChar* StreamName = NULL, const XnPropertySet* pInitialValues = NULL);
virtual XnStatus DestroyStream(const XnChar* StreamName);
virtual XnStatus OpenStream(const XnChar* StreamName);
virtual XnStatus CloseStream(const XnChar* StreamName);
virtual XnStatus GetStreamNames(const XnChar** pstrNames, XnUInt32* pnNamesCount);
virtual XnStatus DoesModuleExist(const XnChar* ModuleName, XnBool* pbDoesExist);
virtual XnStatus OpenAllStreams();
virtual XnStatus CloseAllStreams();
virtual XnStatus RegisterToStreamsChange(XnDeviceOnStreamsChangedEventHandler Handler, void* pCookie, XnCallbackHandle* phCallback);
virtual XnStatus UnregisterFromStreamsChange(XnCallbackHandle hCallback);
virtual XnStatus CreateStreamData(const XnChar* StreamName, XnStreamData** ppStreamData);
static XnStatus DestroyStreamData(XnStreamData** ppStreamData);
virtual XnStatus RegisterToNewStreamData(XnDeviceOnNewStreamDataEventHandler Handler, void* pCookie, XnCallbackHandle* phCallback);
virtual XnStatus UnregisterFromNewStreamData(XnCallbackHandle hCallback);
virtual XnStatus IsNewDataAvailable(const XnChar* StreamName, XnBool* pbNewDataAvailable, XnUInt64* pnTimestamp);
virtual XnStatus ReadStream(XnStreamData* pStreamOutput);
virtual XnStatus Read(XnStreamDataSet* pStreamOutputSet);
virtual XnStatus WriteStream(XnStreamData* pStreamOutput);
virtual XnStatus Write(XnStreamDataSet* pStreamOutputSet);
virtual XnStatus Tell(XnUInt64* pnTimestamp);
virtual XnStatus TellFrame(XnUInt32* pnFrameID);
virtual XnStatus DoesPropertyExist(const XnChar* ModuleName, const XnChar* PropertyName, XnBool* pbDoesExist);
virtual XnStatus GetPropertyType(const XnChar* ModuleName, const XnChar* PropertyName, XnPropertyType* pnType);
virtual XnStatus SetProperty(const XnChar* ModuleName, const XnChar* PropertyName, XnUInt64 nValue);
virtual XnStatus SetProperty(const XnChar* ModuleName, const XnChar* PropertyName, XnDouble dValue);
virtual XnStatus SetProperty(const XnChar* ModuleName, const XnChar* PropertyName, const XnChar* csValue);
virtual XnStatus SetProperty(const XnChar* ModuleName, const XnChar* PropertyName, const XnGeneralBuffer& Value);
virtual XnStatus GetProperty(const XnChar* ModuleName, const XnChar* PropertyName, XnUInt64* pnValue);
virtual XnStatus GetProperty(const XnChar* ModuleName, const XnChar* PropertyName, XnDouble* pdValue);
virtual XnStatus GetProperty(const XnChar* ModuleName, const XnChar* PropertyName, XnChar* csValue);
virtual XnStatus GetProperty(const XnChar* ModuleName, const XnChar* PropertyName, const XnGeneralBuffer& pValue);
virtual XnStatus LoadConfigFromFile(const XnChar* csINIFilePath, const XnChar* csSectionName);
virtual XnStatus BatchConfig(const XnPropertySet* pChangeSet);
virtual XnStatus GetAllProperties(XnPropertySet* pSet, XnBool bNoStreams = FALSE, const XnChar* strModule = NULL);
virtual XnStatus RegisterToPropertyChange(const XnChar* Module, const XnChar* PropertyName, XnDeviceOnPropertyChangedEventHandler Handler, void* pCookie, XnCallbackHandle* phCallback);
virtual XnStatus UnregisterFromPropertyChange(const XnChar* Module, const XnChar* PropertyName, XnCallbackHandle hCallback);
XN_DECLARE_EVENT_3ARG(StreamCollectionChangedEvent, StreamCollectionChangedEventInterface, XnDeviceHandle, DeviceHandle, const XnChar*, StreamName, XnStreamsChangeEventType, EventType);
StreamCollectionChangedEventInterface& OnStreamCollectionChangedEvent() { return m_OnStreamsChangeEvent; }
XN_DECLARE_EVENT_2ARG(NewStreamDataEvent, NewStreamDataEventInterface, XnDeviceHandle, DeviceHandle, const XnChar*, StreamName);
NewStreamDataEventInterface& OnNewStreamDataEvent() { return m_OnNewStreamDataEvent; }
protected:
virtual XnStatus InitImpl(const XnDeviceConfig* pDeviceConfig);
virtual XnStatus CreateStreamImpl(const XnChar* strType, const XnChar* strName, const XnActualPropertiesHash* pInitialSet);
virtual XnStatus CreateModule(const XnChar* strName, XnDeviceModuleHolder** ppModuleHolder);
virtual XnStatus CreateDeviceModule(XnDeviceModuleHolder** ppModuleHolder);
virtual void DestroyModule(XnDeviceModuleHolder* pModuleHolder);
XnStatus CreateStreams(const XnPropertySet* pSet);
/**
* Adds a module to the device modules.
*/
XnStatus AddModule(XnDeviceModuleHolder* pModuleHolder);
/**
* Removes a module from the device modules.
*/
XnStatus RemoveModule(const XnChar* ModuleName);
/**
* Finds a module.
*/
XnStatus FindModule(const XnChar* ModuleName, XnDeviceModule** ppModule);
/**
* Finds a module.
*/
XnStatus FindModule(const XnChar* ModuleName, XnDeviceModuleHolder** ppModuleHolder);
/**
* Checks if a module is a stream.
*/
static XnBool IsStream(XnDeviceModule* pModule);
/**
* Finds a stream (a module which has the IS_STREAM property set to TRUE).
*/
XnStatus FindStream(const XnChar* StreamName, XnDeviceStream** ppStream);
/**
* Finds a stream holder (a module which has the IS_STREAM property set to TRUE).
*/
XnStatus FindStream(const XnChar* StreamName, XnDeviceModuleHolder** ppStreamHolder);
/**
* Adds a stream to the list of supported streams.
*/
XnStatus AddSupportedStream(const XnChar* StreamType);
/**
* Creates a stream.
*
* @param StreamType [in] Type of the stream to create.
* @param StreamName [in] The name of the new stream.
*/
virtual XnStatus CreateStreamModule(const XnChar* StreamType, const XnChar* StreamName, XnDeviceModuleHolder** ppStreamHolder) = 0;
virtual void DestroyStreamModule(XnDeviceModuleHolder* pStreamHolder) = 0;
/**
* Starts a transaction.
*/
virtual XnStatus StartTransaction();
/**
* Commits a transaction.
*/
virtual XnStatus CommitTransaction();
/**
* Rollbacks a transaction.
*/
virtual XnStatus RollbackTransaction();
/**
* Gets the required output size of a stream.
*/
XnStatus GetStreamRequiredDataSize(const XnChar* StreamName, XnUInt32* pnRequiredSize);
/**
* Gets the list of modules the device supports.
*
* @param aModules [out] an array of modules.
* @param pnModules [out] The number of modules.
*/
XnStatus GetModulesList(XnDeviceModuleHolder** apModules, XnUInt32* pnCount);
XnStatus GetModulesList(XnDeviceModuleHolderList& list);
XnStatus GetStreamsList(XnDeviceModuleHolderList& list);
/**
* Raises the NewStreamData event.
*
* @param StreamName [in] The name of the stream with new data.
*/
XnStatus RaiseNewStreamDataEvent(const XnChar* StreamName);
/** Gets called when a stream has new data. */
virtual void OnNewStreamData(XnDeviceStream* pStream, XnUInt64 nTimestamp, XnUInt32 nFrameID);
virtual XnStatus WaitForPrimaryStream(XN_EVENT_HANDLE hNewDataEvent, XnStreamDataSet* pSet);
virtual XnStatus WaitForStream(XN_EVENT_HANDLE hNewDataEvent, XnDeviceStream* pStream);
XnBool HasPrimaryStreamAdvanced(XnStreamDataSet* pSet);
XnBool HasStreamAdvanced(XnDeviceStream* pStream);
inline XnUInt64 GetLastReadTimestamp() const { return m_nLastReadTimestamp; }
inline XnUInt32 GetLastReadFrameID() const { return m_nLastReadFrameID; }
inline XnUInt64 GetLastTimestamp() const { return m_nLastTimestamp; }
inline XnUInt32 GetLastFrameID() const { return m_nLastFrameID; }
void ResetLastTimestampAndFrame();
XnStatus ValidateOnlyModule(const XnPropertySet* pSet, const XnChar* StreamName);
XnStatus CreateStreamsFromFile(const XnChar* csINIFilePath, const XnChar* csSectionName);
XnStatus StreamAdded(XnDeviceStream* pStream);
inline XnDeviceSharingMode GetSharingMode() const { return (XnDeviceSharingMode)m_SharingMode.GetValue(); }
private:
typedef XnBool (XnDeviceBase::*ConditionFuncPtr)(void*);
static XnBool XN_CALLBACK_TYPE HasPrimaryStreamAdvancedCallback(void* pSet);
static XnBool XN_CALLBACK_TYPE HasStreamAdvancedCallback(void* pStream);
XnStatus ReadFromStreamImpl(XnDeviceStream* pStream, XnStreamData* pStreamData);
void FreeModuleRegisteredProperties(const XnChar* strModule);
static XnStatus XN_CALLBACK_TYPE PropertyValueChangedCallback(const XnProperty* pSender, void* pCookie);
static XnStatus XN_CALLBACK_TYPE SetPrimaryStreamCallback(XnActualStringProperty* pSender, const XnChar* strValue, void* pCookie);
static XnStatus XN_CALLBACK_TYPE SetMirrorCallback(XnActualIntProperty* pSender, XnUInt64 nValue, void* pCookie);
static XnStatus XN_CALLBACK_TYPE SetHighresTimestampsCallback(XnActualIntProperty* pSender, XnUInt64 nValue, void* pCookie);
static void NewStreamDataCallback(XnDeviceStream* pSender, XnUInt64 nTimestamp, XnUInt32 nFrameID, void* pCookie);
XnBool m_bStrictProperties;
XnDeviceModuleHolder* m_pDevicePropertiesHolder;
XnActualIntProperty m_ReadWriteMode;
XnActualIntProperty m_SharingMode;
XnActualStringProperty m_PrimaryStream;
XnActualIntProperty m_DeviceMirror;
XnActualGeneralProperty m_SDKVersionProp;
XnActualIntProperty m_HighResTimestamps;
XnActualStringProperty m_DeviceName;
/** Used to tell the read thread new data is available. */
XN_EVENT_HANDLE m_hNewDataEvent;
/** Keeps the last read timestamp and frame id. */
XnUInt64 m_nLastReadTimestamp;
XnUInt32 m_nLastReadFrameID;
/** Keeps the latest available timestamp and frame id. */
XnUInt64 m_nLastTimestamp;
XnUInt32 m_nLastFrameID;
static XnStatus XN_CALLBACK_TYPE StreamNewDataCallback(XnDeviceStream* pStream, void* pCookie);
XnStringsHash m_Modules;
XnStringsHash m_SupportedStreams;
XnList m_PropertyCallbacks;
StreamCollectionChangedEvent m_OnStreamsChangeEvent;
NewStreamDataEvent m_OnNewStreamDataEvent;
XnSDKVersion m_SDKVersion;
XnDumpFile* m_StreamsDataDump;
};
#endif //__XN_DEVICE_BASE_H__