File: OpenNI.cpp

package info (click to toggle)
openni2 2.2.0.33%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,216 kB
  • sloc: cpp: 111,197; ansic: 35,511; sh: 10,542; python: 1,313; java: 952; makefile: 575; xml: 12
file content (517 lines) | stat: -rw-r--r-- 15,898 bytes parent folder | download | duplicates (4)
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
/*****************************************************************************
*                                                                            *
*  OpenNI 2.x Alpha                                                          *
*  Copyright (C) 2012 PrimeSense Ltd.                                        *
*                                                                            *
*  This file is part of OpenNI.                                              *
*                                                                            *
*  Licensed under the Apache License, Version 2.0 (the "License");           *
*  you may not use this file except in compliance with the License.          *
*  You may obtain a copy of the License at                                   *
*                                                                            *
*      http://www.apache.org/licenses/LICENSE-2.0                            *
*                                                                            *
*  Unless required by applicable law or agreed to in writing, software       *
*  distributed under the License is distributed on an "AS IS" BASIS,         *
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
*  See the License for the specific language governing permissions and       *
*  limitations under the License.                                            *
*                                                                            *
*****************************************************************************/
#include "OniContext.h"
#include "OniVersion.h"
#include "OniProperties.h"
#include "OniInternal.h"
#include <XnLog.h>

oni::implementation::Context g_Context;

ONI_C_API OniStatus oniInitialize(int /*apiVersion*/)
{
	g_Context.clearErrorLogger();
	OniStatus rc = g_Context.initialize();
	return rc;
}

ONI_C_API void oniShutdown()
{
	g_Context.clearErrorLogger();
	g_Context.shutdown();
}

ONI_C_API OniStatus oniGetDeviceList(OniDeviceInfo** pDevices, int* pDeviceCount)
{
	g_Context.clearErrorLogger();
	return g_Context.getDeviceList(pDevices, pDeviceCount);
}
ONI_C_API OniStatus oniReleaseDeviceList(OniDeviceInfo* pDevices)
{
	g_Context.clearErrorLogger();
	return g_Context.releaseDeviceList(pDevices);
}

struct DeviceHandles
{
	OniCallbackHandle deviceConnectedHandle;
	OniCallbackHandle deviceDisconnectedHandle;
	OniCallbackHandle deviceStateChangedHandle;
	void* pCookie;
};
ONI_C_API OniStatus oniRegisterDeviceCallbacks(OniDeviceCallbacks* pCallbacks, void* pCookie, OniCallbackHandle* pHandle)
{
	g_Context.clearErrorLogger();
	DeviceHandles* pDeviceHandles = XN_NEW(DeviceHandles);
	XN_VALIDATE_PTR(pDeviceHandles, ONI_STATUS_ERROR);

	pDeviceHandles->deviceConnectedHandle = NULL;
	pDeviceHandles->deviceDisconnectedHandle = NULL;
	pDeviceHandles->deviceStateChangedHandle = NULL;
	pDeviceHandles->pCookie = pCookie;

	g_Context.registerDeviceConnectedCallback(pCallbacks->deviceConnected, pCookie, pDeviceHandles->deviceConnectedHandle);
	g_Context.registerDeviceDisconnectedCallback(pCallbacks->deviceDisconnected, pCookie, pDeviceHandles->deviceDisconnectedHandle);
	g_Context.registerDeviceStateChangedCallback(pCallbacks->deviceStateChanged, pCookie, pDeviceHandles->deviceStateChangedHandle);
	*pHandle = (OniCallbackHandle)pDeviceHandles;

	return ONI_STATUS_OK;
}
ONI_C_API void oniUnregisterDeviceCallbacks(OniCallbackHandle handle)
{
	g_Context.clearErrorLogger();
	DeviceHandles* pDevicesHandles = (DeviceHandles*)handle;
	if (pDevicesHandles == NULL)
	{
		return;
	}

	g_Context.unregisterDeviceConnectedCallback(pDevicesHandles->deviceConnectedHandle);
	g_Context.unregisterDeviceDisconnectedCallback(pDevicesHandles->deviceDisconnectedHandle);
	g_Context.unregisterDeviceStateChangedCallback(pDevicesHandles->deviceStateChangedHandle);

	XN_DELETE(pDevicesHandles);
}

ONI_C_API OniStatus oniWaitForAnyStream(OniStreamHandle* pStreams, int streamCount, int* pStreamIndex, int timeout)
{
	g_Context.clearErrorLogger();
	return g_Context.waitForStreams(pStreams, streamCount, pStreamIndex, timeout);
}

ONI_C_API const char* oniGetExtendedError()
{
	return g_Context.getExtendedError();
}

ONI_C_API OniVersion oniGetVersion()
{
	g_Context.clearErrorLogger();
	OniVersion version;

	version.major = ONI_VERSION_MAJOR;
	version.minor = ONI_VERSION_MINOR;
	version.maintenance = ONI_VERSION_MAINTENANCE;
	version.build = ONI_VERSION_BUILD;

	return version;
}

ONI_C_API int oniFormatBytesPerPixel(OniPixelFormat format)
{
	g_Context.clearErrorLogger();
	switch (format)
	{
	case ONI_PIXEL_FORMAT_GRAY8:
		return 1;
	case ONI_PIXEL_FORMAT_DEPTH_1_MM:
	case ONI_PIXEL_FORMAT_DEPTH_100_UM:
	case ONI_PIXEL_FORMAT_SHIFT_9_2:
	case ONI_PIXEL_FORMAT_SHIFT_9_3:
	case ONI_PIXEL_FORMAT_GRAY16:
		return 2;
	case ONI_PIXEL_FORMAT_RGB888:
		return 3;
	case ONI_PIXEL_FORMAT_YUV422:
	case ONI_PIXEL_FORMAT_YUYV:
		return 2;
	case ONI_PIXEL_FORMAT_JPEG:
		return 1;
	default:
		XN_ASSERT(FALSE);
		return 0;
	}
}

/////////

ONI_C_API OniStatus oniDeviceOpen(const char* uri, OniDeviceHandle* pDevice)
{
	return oniDeviceOpenEx(uri, NULL, pDevice);
}
ONI_C_API OniStatus oniDeviceOpenEx(const char* uri, const char* mode, OniDeviceHandle* pDevice)
{
	g_Context.clearErrorLogger();
	return g_Context.deviceOpen(uri, mode, pDevice);
}
ONI_C_API OniStatus oniDeviceClose(OniDeviceHandle device)
{
	g_Context.clearErrorLogger();
	if (!oni::implementation::Context::s_valid)
	{
		return ONI_STATUS_ERROR;
	}
	return g_Context.deviceClose(device);
}

ONI_C_API OniStatus oniDeviceGetInfo(OniDeviceHandle device, OniDeviceInfo* pInfo)
{
	g_Context.clearErrorLogger();
	const OniDeviceInfo* pDeviceInfo = device->pDevice->getInfo();
	xnOSMemCopy(pInfo, pDeviceInfo, sizeof(OniDeviceInfo));

	return ONI_STATUS_OK;
}

ONI_C_API const OniSensorInfo* oniDeviceGetSensorInfo(OniDeviceHandle device, OniSensorType sensorType)
{
	g_Context.clearErrorLogger();
	return g_Context.getSensorInfo(device, sensorType);
}

ONI_C_API OniStatus oniDeviceCreateStream(OniDeviceHandle device, OniSensorType sensorType, OniStreamHandle* pStream)
{
	g_Context.clearErrorLogger();
	return g_Context.createStream(device, sensorType, pStream);
}

ONI_C_API OniStatus oniDeviceEnableDepthColorSync(OniDeviceHandle device)
{
	g_Context.clearErrorLogger();
	return device->pDevice->enableDepthColorSync(&g_Context);
}
ONI_C_API void oniDeviceDisableDepthColorSync(OniDeviceHandle device)
{
	g_Context.clearErrorLogger();
	device->pDevice->disableDepthColorSync();
}
ONI_C_API OniBool oniDeviceGetDepthColorSyncEnabled(OniDeviceHandle device)
{
	g_Context.clearErrorLogger();
	return device->pDevice->isDepthColorSyncEnabled();
}

ONI_C_API OniStatus oniDeviceSetProperty(OniDeviceHandle device, int propertyId, const void* data, int dataSize)
{
	g_Context.clearErrorLogger();
	return device->pDevice->setProperty(propertyId, data, dataSize);
}
ONI_C_API OniStatus oniDeviceGetProperty(OniDeviceHandle device, int propertyId, void* data, int* pDataSize)
{
	g_Context.clearErrorLogger();
	return device->pDevice->getProperty(propertyId, data, pDataSize);
}
ONI_C_API OniBool oniDeviceIsPropertySupported(OniDeviceHandle device, int propertyId)
{
	g_Context.clearErrorLogger();
	return device->pDevice->isPropertySupported(propertyId);
}
ONI_C_API OniStatus oniDeviceInvoke(OniDeviceHandle device, int commandId, void* data, int dataSize)
{
	g_Context.clearErrorLogger();
	return device->pDevice->invoke(commandId, data, dataSize);
}
ONI_C_API OniBool oniDeviceIsCommandSupported(OniDeviceHandle device, int commandId)
{
	g_Context.clearErrorLogger();
	return device->pDevice->isCommandSupported(commandId);
}
ONI_C_API OniBool oniDeviceIsImageRegistrationModeSupported(OniDeviceHandle device, OniImageRegistrationMode mode)
{
	g_Context.clearErrorLogger();
	return device->pDevice->isImageRegistrationModeSupported(mode);
}

/////////

ONI_C_API void oniStreamDestroy(OniStreamHandle stream)
{
	g_Context.clearErrorLogger();
	if (!oni::implementation::Context::s_valid)
	{
		return;
	}
	g_Context.streamDestroy(stream);
}

ONI_C_API const OniSensorInfo* oniStreamGetSensorInfo(OniStreamHandle stream)
{
	g_Context.clearErrorLogger();
	return g_Context.getSensorInfo(stream);
}

ONI_C_API OniStatus oniStreamStart(OniStreamHandle stream)
{
	g_Context.clearErrorLogger();
	return stream->pStream->start();
}
ONI_C_API void oniStreamStop(OniStreamHandle stream)
{
	g_Context.clearErrorLogger();
	if (stream == NULL || !oni::implementation::Context::s_valid)
	{
		return;
	}
	stream->pStream->stop();
}

ONI_C_API OniStatus oniStreamReadFrame(OniStreamHandle stream, OniFrame** pFrame)
{
	g_Context.clearErrorLogger();
	return g_Context.readFrame(stream, pFrame);
}

struct OniNewFrameCookie
{
	OniStreamHandle streamHandle;
	OniNewFrameCallback handler;
	void* pCookie;
	XnCallbackHandle handle;
};

void ONI_CALLBACK_TYPE OniNewFrameTranslationHandler(void* pCookie)
{
	OniNewFrameCookie* pNewFrameCookie = (OniNewFrameCookie*)pCookie;

	(*pNewFrameCookie->handler)(pNewFrameCookie->streamHandle, pNewFrameCookie->pCookie);
}

ONI_C_API OniStatus oniStreamRegisterNewFrameCallback(OniStreamHandle stream, OniNewFrameCallback handler, void* pCookie, OniCallbackHandle* pHandle)
{
	g_Context.clearErrorLogger();

	if (*pHandle != NULL)
	{
		// Already registered to something
		g_Context.addToLogger("Can't register same listener instance to multiple events");
		return ONI_STATUS_ERROR;
	}

	OniNewFrameCookie* pNewFrameCookie = XN_NEW(OniNewFrameCookie);
	XN_VALIDATE_PTR(pNewFrameCookie, ONI_STATUS_ERROR);

	pNewFrameCookie->streamHandle = stream;
	pNewFrameCookie->handler = handler;
	pNewFrameCookie->pCookie = pCookie;

	*pHandle = (OniCallbackHandle)pNewFrameCookie;
	return stream->pStream->registerNewFrameCallback(OniNewFrameTranslationHandler, pNewFrameCookie, &(pNewFrameCookie->handle));
}
ONI_C_API void oniStreamUnregisterNewFrameCallback(OniStreamHandle stream, OniCallbackHandle handle)
{
	g_Context.clearErrorLogger();
	OniNewFrameCookie* pNewFrameCookie = (OniNewFrameCookie*)handle;

	if (pNewFrameCookie == NULL)
	{
		return;
	}

	if (oni::implementation::Context::s_valid)
	{
		stream->pStream->unregisterNewFrameCallback(pNewFrameCookie->handle);
	}
	XN_DELETE(pNewFrameCookie);
}

ONI_C_API OniStatus oniStreamSetProperty(OniStreamHandle stream, int propertyId, const void* data, int dataSize)
{
	g_Context.clearErrorLogger();
	return stream->pStream->setProperty(propertyId, data, dataSize);
}

ONI_C_API OniStatus oniStreamGetProperty(OniStreamHandle stream, int propertyId, void* data, int* pDataSize)
{
	g_Context.clearErrorLogger();
	return stream->pStream->getProperty(propertyId, data, pDataSize);
}
ONI_C_API OniBool oniStreamIsPropertySupported(OniStreamHandle stream, int propertyId)
{
	g_Context.clearErrorLogger();
	return stream->pStream->isPropertySupported(propertyId);
}

ONI_C_API OniStatus oniStreamInvoke(OniStreamHandle stream, int commandId, void* data, int dataSize)
{
	g_Context.clearErrorLogger();
	return stream->pStream->invoke(commandId, data, dataSize);
}
ONI_C_API OniBool oniStreamIsCommandSupported(OniStreamHandle stream, int commandId)
{
	g_Context.clearErrorLogger();
	return stream->pStream->isCommandSupported(commandId);
}

ONI_C_API OniStatus oniStreamSetFrameBuffersAllocator(OniStreamHandle stream, OniFrameAllocBufferCallback alloc, OniFrameFreeBufferCallback free, void* pCookie)
{
	g_Context.clearErrorLogger();
	return stream->pStream->setFrameBufferAllocator(alloc, free, pCookie);	
}

////
ONI_C_API void oniFrameRelease(OniFrame* pFrame)
{
	g_Context.clearErrorLogger();
	if (!oni::implementation::Context::s_valid)
	{
		return;
	}
	g_Context.frameRelease(pFrame);
}
ONI_C_API void oniFrameAddRef(OniFrame* pFrame)
{
	g_Context.clearErrorLogger();
	g_Context.frameAddRef(pFrame);
}

//////////////////////////////////////////////////////////////////////////
// Recorder
//////////////////////////////////////////////////////////////////////////

ONI_C_API OniStatus oniCreateRecorder(
        const char* fileName, 
        OniRecorderHandle* pRecorder)
{
	g_Context.clearErrorLogger();
    return g_Context.recorderOpen(fileName, pRecorder);
}

ONI_C_API OniStatus oniRecorderAttachStream(
        OniRecorderHandle   recorder, 
        OniStreamHandle     stream, 
        OniBool                allowLossyCompression)
{
	g_Context.clearErrorLogger();

	// Validate parameters.
    if (NULL == recorder || NULL == recorder->pRecorder ||
        NULL == stream   || NULL == stream->pStream)
    {
        return ONI_STATUS_BAD_PARAMETER;
    }
    // Attach a stream to the recorder.
    return recorder->pRecorder->attachStream(
            *stream->pStream, allowLossyCompression);
}

ONI_C_API OniStatus oniRecorderStart(OniRecorderHandle recorder)
{
	g_Context.clearErrorLogger();
    // Validate parameters.
    if (NULL == recorder || NULL == recorder->pRecorder)
    {
        return ONI_STATUS_BAD_PARAMETER;
    }
    return recorder->pRecorder->start();
}

ONI_C_API void oniRecorderStop(OniRecorderHandle recorder)
{
	g_Context.clearErrorLogger();
    // Validate parameters.
    if (NULL == recorder || NULL == recorder->pRecorder)
    {
        return;
    }
    recorder->pRecorder->stop();
}

ONI_C_API OniStatus oniRecorderDestroy(OniRecorderHandle* pRecorder)
{
	g_Context.clearErrorLogger();
    return g_Context.recorderClose(pRecorder);
}

ONI_C_API void oniWriteLogEntry(const char* mask, int severity, const char* message)
{
	xnLogWrite(mask, (XnLogSeverity)severity, "External", 0, message);
}

ONI_C_API OniStatus oniSetLogOutputFolder(const char* strOutputFolder)
{
	XnStatus rc = xnLogSetOutputFolder((XnChar*)strOutputFolder);

	if (rc != XN_STATUS_OK)
		return ONI_STATUS_ERROR;
	
	return ONI_STATUS_OK;
}

ONI_C_API OniStatus oniGetLogFileName(char* strFileName, int nBufferSize)
{
	XnStatus rc = xnLogGetFileName((XnChar*)strFileName, (XnUInt32)nBufferSize);

	if (rc != XN_STATUS_OK)
		return ONI_STATUS_ERROR;

	return ONI_STATUS_OK;
}

ONI_C_API OniStatus oniSetLogMinSeverity(int nMinSeverity)
{
	XnStatus rc = xnLogSetMaskMinSeverity(XN_LOG_MASK_ALL, (XnLogSeverity)nMinSeverity);

	if (rc != XN_STATUS_OK)
		return ONI_STATUS_ERROR;

	return ONI_STATUS_OK;
}

ONI_C_API OniStatus oniSetLogConsoleOutput(OniBool bConsoleOutput)
{
	XnStatus rc = xnLogSetConsoleOutput(bConsoleOutput);

	if (rc != XN_STATUS_OK)
		return ONI_STATUS_ERROR;

	return ONI_STATUS_OK;
}

ONI_C_API OniStatus oniSetLogFileOutput(OniBool bFileOutput)
{
	XnStatus rc = xnLogSetFileOutput(bFileOutput);

	if (rc != XN_STATUS_OK)
		return ONI_STATUS_ERROR;

	return ONI_STATUS_OK;
}

#if ONI_PLATFORM == ONI_PLATFORM_ANDROID_ARM
ONI_C_API OniStatus oniSetLogAndroidOutput(OniBool bAndroidOutput)
{
	XnStatus rc = xnLogSetAndroidOutput((XnBool)bAndroidOutput);

	if (rc != XN_STATUS_OK)
		return ONI_STATUS_ERROR;

	return ONI_STATUS_OK;
}
#endif
ONI_C_API OniStatus oniCoordinateConverterDepthToWorld(OniStreamHandle depthStream, float depthX, float depthY, float depthZ, float* pWorldX, float* pWorldY, float* pWorldZ)
{
	g_Context.clearErrorLogger();
	return depthStream->pStream->convertDepthToWorldCoordinates(depthX, depthY, depthZ, pWorldX, pWorldY, pWorldZ);
}

ONI_C_API OniStatus oniCoordinateConverterWorldToDepth(OniStreamHandle depthStream, float worldX, float worldY, float worldZ, float* pDepthX, float* pDepthY, float* pDepthZ)
{
	g_Context.clearErrorLogger();
	return depthStream->pStream->convertWorldToDepthCoordinates(worldX, worldY, worldZ, pDepthX, pDepthY, pDepthZ);
}

ONI_C_API OniStatus oniCoordinateConverterDepthToColor(OniStreamHandle depthStream, OniStreamHandle colorStream, int depthX, int depthY, OniDepthPixel depthZ, int* pColorX, int* pColorY)
{
	g_Context.clearErrorLogger();
	return depthStream->pStream->convertDepthToColorCoordinates(colorStream->pStream, depthX, depthY, depthZ, pColorX, pColorY);
}

XN_API_EXPORT_INIT()