OpenNI 2.0
OpenNI.h
Go to the documentation of this file.
1/*****************************************************************************
2* *
3* OpenNI 2.x Alpha *
4* Copyright (C) 2012 PrimeSense Ltd. *
5* *
6* This file is part of OpenNI. *
7* *
8* Licensed under the Apache License, Version 2.0 (the "License"); *
9* you may not use this file except in compliance with the License. *
10* You may obtain a copy of the License at *
11* *
12* http://www.apache.org/licenses/LICENSE-2.0 *
13* *
14* Unless required by applicable law or agreed to in writing, software *
15* distributed under the License is distributed on an "AS IS" BASIS, *
16* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
17* See the License for the specific language governing permissions and *
18* limitations under the License. *
19* *
20*****************************************************************************/
21#ifndef _OPENNI_H_
22#define _OPENNI_H_
23
24#include "OniPlatform.h"
25#include "OniProperties.h"
26#include "OniEnums.h"
27
28#include "OniCAPI.h"
29#include "OniCProperties.h"
30
34namespace openni
35{
36
38typedef uint16_t DepthPixel;
39
41typedef uint16_t Grayscale16Pixel;
42
43// structs
45typedef struct
46{
48 int major;
50 int minor;
54 int build;
55} Version;
56
58typedef struct
59{
60 /* Red value of this pixel. */
61 uint8_t r;
62 /* Green value of this pixel. */
63 uint8_t g;
64 /* Blue value of this pixel. */
65 uint8_t b;
67
73typedef struct
74{
76 uint8_t u;
78 uint8_t y1;
80 uint8_t v;
82 uint8_t y2;
84
86#if ONI_PLATFORM != ONI_PLATFORM_WIN32
87#pragma GCC diagnostic ignored "-Wunused-variable"
88#pragma GCC diagnostic push
89#endif
90static const char* ANY_DEVICE = NULL;
91#if ONI_PLATFORM != ONI_PLATFORM_WIN32
92#pragma GCC diagnostic pop
93#endif
94
99template<class T>
100class Array
101{
102public:
106 Array() : m_data(NULL), m_count(0), m_owner(false) {}
107
115 Array(const T* data, int count) : m_owner(false) { _setData(data, count); }
116
121 {
122 clear();
123 }
124
129 int getSize() const { return m_count; }
130
134 const T& operator[](int index) const {return m_data[index];}
135
146 void _setData(const T* data, int count, bool isOwner = false)
147 {
148 clear();
149 m_count = count;
150 m_owner = isOwner;
151 if (!isOwner)
152 {
153 m_data = data;
154 }
155 else
156 {
157 m_data = new T[count];
158 memcpy((void*)m_data, data, count*sizeof(T));
159 }
160 }
161
162private:
163 Array(const Array<T>&);
164 Array<T>& operator=(const Array<T>&);
165
166 void clear()
167 {
168 if (m_owner && m_data != NULL)
169 delete []m_data;
170 m_owner = false;
171 m_data = NULL;
172 m_count = 0;
173 }
174
175 const T* m_data;
176 int m_count;
177 bool m_owner;
178};
179
180// Forward declaration of all
181class SensorInfo;
182class VideoStream;
183class VideoFrameRef;
184class Device;
185class OpenNI;
186class CameraSettings;
187class PlaybackControl;
188
203class VideoMode : private OniVideoMode
204{
205public:
212 {}
213
220 {
221 *this = other;
222 }
223
231 {
232 setPixelFormat(other.getPixelFormat());
233 setResolution(other.getResolutionX(), other.getResolutionY());
234 setFps(other.getFps());
235
236 return *this;
237 }
238
244
249 int getResolutionX() const { return resolutionX; }
250
255 int getResolutionY() const {return resolutionY;}
256
261 int getFps() const { return fps; }
262
270
279 {
280 this->resolutionX = resolutionX;
281 this->resolutionY = resolutionY;
282 }
283
290 void setFps(int fps) { this->fps = fps; }
291
292 friend class SensorInfo;
293 friend class VideoStream;
294 friend class VideoFrameRef;
295};
296
315{
316public:
321 SensorType getSensorType() const { return (SensorType)m_pInfo->sensorType; }
322
330 const Array<VideoMode>& getSupportedVideoModes() const { return m_videoModes; }
331
332private:
333 SensorInfo(const SensorInfo&);
334 SensorInfo& operator=(const SensorInfo&);
335
336 SensorInfo() : m_pInfo(NULL), m_videoModes(NULL, 0) {}
337
338 SensorInfo(const OniSensorInfo* pInfo) : m_pInfo(NULL), m_videoModes(NULL, 0)
339 {
340 _setInternal(pInfo);
341 }
342
343 void _setInternal(const OniSensorInfo* pInfo)
344 {
345 m_pInfo = pInfo;
346 if (pInfo == NULL)
347 {
348 m_videoModes._setData(NULL, 0);
349 }
350 else
351 {
352 m_videoModes._setData(static_cast<VideoMode*>(pInfo->pSupportedVideoModes), pInfo->numSupportedVideoModes);
353 }
354 }
355
356 const OniSensorInfo* m_pInfo;
357 Array<VideoMode> m_videoModes;
358
359 friend class VideoStream;
360 friend class Device;
361};
362
372class DeviceInfo : private OniDeviceInfo
373{
374public:
379 const char* getUri() const { return uri; }
381 const char* getVendor() const { return vendor; }
383 const char* getName() const { return name; }
388
389 friend class Device;
390 friend class OpenNI;
391};
392
407{
408public:
414 {
415 m_pFrame = NULL;
416 }
417
422 {
423 release();
424 }
425
432 {
433 _setFrame(other.m_pFrame);
434 }
435
442 {
443 _setFrame(other.m_pFrame);
444 return *this;
445 }
446
452 inline int getDataSize() const
453 {
454 return m_pFrame->dataSize;
455 }
456
462 inline const void* getData() const
463 {
464 return m_pFrame->data;
465 }
466
474 {
475 return (SensorType)m_pFrame->sensorType;
476 }
477
485 inline const VideoMode& getVideoMode() const
486 {
487 return static_cast<const VideoMode&>(m_pFrame->videoMode);
488 }
489
497 inline uint64_t getTimestamp() const
498 {
499 return m_pFrame->timestamp;
500 }
501
512 inline int getFrameIndex() const
513 {
514 return m_pFrame->frameIndex;
515 }
516
523 inline int getWidth() const
524 {
525 return m_pFrame->width;
526 }
527
533 inline int getHeight() const
534 {
535 return m_pFrame->height;
536 }
537
542 inline bool getCroppingEnabled() const
543 {
544 return m_pFrame->croppingEnabled == TRUE;
545 }
546
551 inline int getCropOriginX() const
552 {
553 return m_pFrame->cropOriginX;
554 }
555
560 inline int getCropOriginY() const
561 {
562 return m_pFrame->cropOriginY;
563 }
564
570 inline int getStrideInBytes() const
571 {
572 return m_pFrame->stride;
573 }
574
578 inline bool isValid() const
579 {
580 return m_pFrame != NULL;
581 }
582
587 void release()
588 {
589 if (m_pFrame != NULL)
590 {
591 oniFrameRelease(m_pFrame);
592 m_pFrame = NULL;
593 }
594 }
595
597 void _setFrame(OniFrame* pFrame)
598 {
599 setReference(pFrame);
600 if (pFrame != NULL)
601 {
603 }
604 }
605
607 OniFrame* _getFrame()
608 {
609 return m_pFrame;
610 }
611
612private:
613 friend class VideoStream;
614 inline void setReference(OniFrame* pFrame)
615 {
616 // Initial - don't addref. This is the reference from OpenNI
617 release();
618 m_pFrame = pFrame;
619 }
620
621 OniFrame* m_pFrame; // const!!?
622};
623
646{
647public:
656 {
657 public:
661 NewFrameListener() : m_callbackHandle(NULL)
662 {
663 }
664
666 {
667 }
668
672 virtual void onNewFrame(VideoStream&) = 0;
673
674 private:
675 friend class VideoStream;
676
677 static void ONI_CALLBACK_TYPE callback(OniStreamHandle streamHandle, void* pCookie)
678 {
681 stream._setHandle(streamHandle);
682 pListener->onNewFrame(stream);
683 stream._setHandle(NULL);
684 }
685 OniCallbackHandle m_callbackHandle;
686 };
687
689 {
690 public:
691 virtual ~FrameAllocator() {}
692 virtual void* allocateFrameBuffer(int size) = 0;
693 virtual void freeFrameBuffer(void* data) = 0;
694
695 private:
696 friend class VideoStream;
697
698 static void* ONI_CALLBACK_TYPE allocateFrameBufferCallback(int size, void* pCookie)
699 {
701 return pThis->allocateFrameBuffer(size);
702 }
703
704 static void ONI_CALLBACK_TYPE freeFrameBufferCallback(void* data, void* pCookie)
705 {
707 pThis->freeFrameBuffer(data);
708 }
709 };
710
715 VideoStream() : m_stream(NULL), m_sensorInfo(), m_pCameraSettings(NULL), m_isOwner(true)
716 {}
717
722 explicit VideoStream(OniStreamHandle handle) : m_stream(NULL), m_sensorInfo(), m_pCameraSettings(NULL), m_isOwner(false)
723 {
724 _setHandle(handle);
725 }
726
732 {
733 destroy();
734 }
735
740 bool isValid() const
741 {
742 return m_stream != NULL;
743 }
744
755
761 inline void destroy();
762
772 {
773 return m_sensorInfo;
774 }
775
780 {
781 if (!isValid())
782 {
783 return STATUS_ERROR;
784 }
785
786 return (Status)oniStreamStart(m_stream);
787 }
788
792 void stop()
793 {
794 if (!isValid())
795 {
796 return;
797 }
798
799 oniStreamStop(m_stream);
800 }
801
813 {
814 if (!isValid())
815 {
816 return STATUS_ERROR;
817 }
818
821
822 pFrame->setReference(pOniFrame);
823 return rc;
824 }
825
834 {
835 if (!isValid())
836 {
837 return STATUS_ERROR;
838 }
839
840 return (Status)oniStreamRegisterNewFrameCallback(m_stream, pListener->callback, pListener, &pListener->m_callbackHandle);
841 }
842
848 {
849 if (!isValid())
850 {
851 return;
852 }
853
854 oniStreamUnregisterNewFrameCallback(m_stream, pListener->m_callbackHandle);
855 pListener->m_callbackHandle = NULL;
856 }
857
864 {
865 if (!isValid())
866 {
867 return STATUS_ERROR;
868 }
869
870 if (pAllocator == NULL)
871 {
873 }
874 else
875 {
876 return (Status)oniStreamSetFrameBuffersAllocator(m_stream, pAllocator->allocateFrameBufferCallback, pAllocator->freeFrameBufferCallback, pAllocator);
877 }
878 }
879
884 OniStreamHandle _getHandle() const
885 {
886 return m_stream;
887 }
888
893 CameraSettings* getCameraSettings() {return m_pCameraSettings;}
894
905 Status getProperty(int propertyId, void* data, int* dataSize) const
906 {
907 if (!isValid())
908 {
909 return STATUS_ERROR;
910 }
911
913 }
914
926 {
927 if (!isValid())
928 {
929 return STATUS_ERROR;
930 }
931
933 }
934
942 {
945 return videoMode;
946 }
947
957 {
958 return setProperty<OniVideoMode>(STREAM_PROPERTY_VIDEO_MODE, static_cast<const OniVideoMode&>(videoMode));
959 }
960
967 {
968 int maxValue;
970 if (rc != STATUS_OK)
971 {
972 return 0;
973 }
974 return maxValue;
975 }
976
983 {
984 int minValue;
986 if (rc != STATUS_OK)
987 {
988 return 0;
989 }
990 return minValue;
991 }
992
998 {
1000 }
1001
1010 bool getCropping(int* pOriginX, int* pOriginY, int* pWidth, int* pHeight) const
1011 {
1013 bool enabled = false;
1014
1016
1017 if (rc == STATUS_OK)
1018 {
1019 *pOriginX = cropping.originX;
1020 *pOriginY = cropping.originY;
1021 *pWidth = cropping.width;
1022 *pHeight = cropping.height;
1023 enabled = (cropping.enabled == TRUE);
1024 }
1025
1026 return enabled;
1027 }
1028
1039 {
1041 cropping.enabled = true;
1042 cropping.originX = originX;
1043 cropping.originY = originY;
1044 cropping.width = width;
1045 cropping.height = height;
1047 }
1048
1054 {
1056 cropping.enabled = false;
1058 }
1059
1065 {
1068 if (rc != STATUS_OK)
1069 {
1070 return false;
1071 }
1072 return enabled == TRUE;
1073 }
1074
1081 {
1083 }
1084
1090 {
1091 float horizontal = 0;
1093 return horizontal;
1094 }
1095
1101 {
1102 float vertical = 0;
1104 return vertical;
1105 }
1106
1116 template <class T>
1118 {
1119 return setProperty(propertyId, &value, sizeof(T));
1120 }
1121
1131 template <class T>
1133 {
1134 int size = sizeof(T);
1135 return getProperty(propertyId, value, &size);
1136 }
1137
1144 {
1145 if (!isValid())
1146 {
1147 return false;
1148 }
1149
1150 return oniStreamIsPropertySupported(m_stream, propertyId) == TRUE;
1151 }
1152
1163 {
1164 if (!isValid())
1165 {
1166 return STATUS_ERROR;
1167 }
1168
1169 return (Status)oniStreamInvoke(m_stream, commandId, data, dataSize);
1170 }
1171
1181 template <class T>
1183 {
1184 return invoke(commandId, &value, sizeof(T));
1185 }
1186
1193 {
1194 if (!isValid())
1195 {
1196 return false;
1197 }
1198
1199 return (Status)oniStreamIsCommandSupported(m_stream, commandId) == TRUE;
1200 }
1201
1202private:
1203 friend class Device;
1204
1205 void _setHandle(OniStreamHandle stream)
1206 {
1207 m_sensorInfo._setInternal(NULL);
1208 m_stream = stream;
1209
1210 if (stream != NULL)
1211 {
1212 m_sensorInfo._setInternal(oniStreamGetSensorInfo(m_stream));
1213 }
1214 }
1215
1216private:
1217 VideoStream(const VideoStream& other);
1218 VideoStream& operator=(const VideoStream& other);
1219
1220 OniStreamHandle m_stream;
1221 SensorInfo m_sensorInfo;
1222 CameraSettings* m_pCameraSettings;
1223 bool m_isOwner;
1224};
1225
1243{
1244public:
1249 Device() : m_pPlaybackControl(NULL), m_device(NULL), m_isOwner(true)
1250 {
1251 clearSensors();
1252 }
1253
1258 explicit Device(OniDeviceHandle handle) : m_pPlaybackControl(NULL), m_device(NULL), m_isOwner(false)
1259 {
1260 _setHandle(handle);
1261 }
1262
1268 {
1269 if (m_device != NULL)
1270 {
1271 close();
1272 }
1273 }
1274
1304 inline Status open(const char* uri);
1305
1311 inline void close();
1312
1323 {
1324 return m_deviceInfo;
1325 }
1326
1335 {
1336 int i;
1337 for (i = 0; (i < ONI_MAX_SENSORS) && (m_aSensorInfo[i].m_pInfo != NULL); ++i)
1338 {
1339 if (m_aSensorInfo[i].getSensorType() == sensorType)
1340 {
1341 return true;
1342 }
1343 }
1344
1345 if (i == ONI_MAX_SENSORS)
1346 {
1347 return false;
1348 }
1349
1351
1352 if (pInfo == NULL)
1353 {
1354 return false;
1355 }
1356
1357 m_aSensorInfo[i]._setInternal(pInfo);
1358
1359 return true;
1360 }
1361
1370 {
1371 int i;
1372 for (i = 0; (i < ONI_MAX_SENSORS) && (m_aSensorInfo[i].m_pInfo != NULL); ++i)
1373 {
1374 if (m_aSensorInfo[i].getSensorType() == sensorType)
1375 {
1376 return &m_aSensorInfo[i];
1377 }
1378 }
1379
1380 // not found. check to see we have additional space
1381 if (i == ONI_MAX_SENSORS)
1382 {
1383 return NULL;
1384 }
1385
1387 if (pInfo == NULL)
1388 {
1389 return NULL;
1390 }
1391
1392 m_aSensorInfo[i]._setInternal(pInfo);
1393 return &m_aSensorInfo[i];
1394 }
1395
1400 OniDeviceHandle _getHandle() const
1401 {
1402 return m_device;
1403 }
1404
1409 PlaybackControl* getPlaybackControl() {return m_pPlaybackControl;}
1410
1423 {
1424 return (Status)oniDeviceGetProperty(m_device, propertyId, data, dataSize);
1425 }
1426
1439 {
1440 return (Status)oniDeviceSetProperty(m_device, propertyId, data, dataSize);
1441 }
1442
1451 {
1453 }
1454
1463 {
1466 if (rc != STATUS_OK)
1467 {
1469 }
1470 return mode;
1471 }
1472
1487 {
1489 }
1490
1495 bool isValid() const
1496 {
1497 return m_device != NULL;
1498 }
1499
1504 bool isFile() const
1505 {
1509 }
1510
1520 {
1522
1523 if (isEnabled)
1524 {
1526 }
1527 else
1528 {
1530 }
1531
1532 return rc;
1533 }
1534
1536 {
1537 return oniDeviceGetDepthColorSyncEnabled(m_device) == TRUE;
1538 }
1539
1550 template <class T>
1552 {
1553 return setProperty(propertyId, &value, sizeof(T));
1554 }
1555
1565 template <class T>
1567 {
1568 int size = sizeof(T);
1569 return getProperty(propertyId, value, &size);
1570 }
1571
1578 {
1579 return oniDeviceIsPropertySupported(m_device, propertyId) == TRUE;
1580 }
1581
1592 {
1593 return (Status)oniDeviceInvoke(m_device, commandId, data, dataSize);
1594 }
1595
1605 template <class T>
1607 {
1608 return invoke(propertyId, &value, sizeof(T));
1609 }
1610
1617 {
1618 return oniDeviceIsCommandSupported(m_device, commandId) == TRUE;
1619 }
1620
1622 inline Status _openEx(const char* uri, const char* mode);
1623
1624private:
1625 Device(const Device&);
1626 Device& operator=(const Device&);
1627
1628 void clearSensors()
1629 {
1630 for (int i = 0; i < ONI_MAX_SENSORS; ++i)
1631 {
1632 m_aSensorInfo[i]._setInternal(NULL);
1633 }
1634 }
1635
1636 inline Status _setHandle(OniDeviceHandle deviceHandle);
1637
1638private:
1639 PlaybackControl* m_pPlaybackControl;
1640
1641 OniDeviceHandle m_device;
1642 DeviceInfo m_deviceInfo;
1643 SensorInfo m_aSensorInfo[ONI_MAX_SENSORS];
1644
1645 bool m_isOwner;
1646};
1647
1662{
1663public:
1664
1671 {
1672 detach();
1673 }
1674
1695 float getSpeed() const
1696 {
1697 if (!isValid())
1698 {
1699 return 0.0f;
1700 }
1701 float speed;
1703 if (rc != STATUS_OK)
1704 {
1705 return 1.0f;
1706 }
1707 return speed;
1708 }
1717 {
1718 if (!isValid())
1719 {
1720 return STATUS_NO_DEVICE;
1721 }
1722 return m_pDevice->setProperty<float>(DEVICE_PROPERTY_PLAYBACK_SPEED, speed);
1723 }
1724
1730 bool getRepeatEnabled() const
1731 {
1732 if (!isValid())
1733 {
1734 return false;
1735 }
1736
1739 if (rc != STATUS_OK)
1740 {
1741 return false;
1742 }
1743
1744 return repeat == TRUE;
1745 }
1746
1756 {
1757 if (!isValid())
1758 {
1759 return STATUS_NO_DEVICE;
1760 }
1761
1763 }
1764
1776 {
1777 if (!isValid())
1778 {
1779 return STATUS_NO_DEVICE;
1780 }
1781 OniSeek seek;
1782 seek.frameIndex = frameIndex;
1783 seek.stream = stream._getHandle();
1784 return m_pDevice->invoke(DEVICE_COMMAND_SEEK, seek);
1785 }
1786
1796 {
1797 int numOfFrames = -1;
1799 if (rc != STATUS_OK)
1800 {
1801 return 0;
1802 }
1803 return numOfFrames;
1804 }
1805
1806 bool isValid() const
1807 {
1808 return m_pDevice != NULL;
1809 }
1810private:
1811 Status attach(Device* device)
1812 {
1813 if (!device->isValid() || !device->isFile())
1814 {
1815 return STATUS_ERROR;
1816 }
1817
1818 detach();
1819 m_pDevice = device;
1820
1821 return STATUS_OK;
1822 }
1823 void detach()
1824 {
1825 m_pDevice = NULL;
1826 }
1827
1828 friend class Device;
1829 PlaybackControl(Device* pDevice) : m_pDevice(NULL)
1830 {
1831 if (pDevice != NULL)
1832 {
1833 attach(pDevice);
1834 }
1835 }
1836
1837 Device* m_pDevice;
1838};
1839
1841{
1842public:
1843 // setters
1845 {
1846 return setProperty(STREAM_PROPERTY_AUTO_EXPOSURE, enabled ? TRUE : FALSE);
1847 }
1849 {
1850 return setProperty(STREAM_PROPERTY_AUTO_WHITE_BALANCE, enabled ? TRUE : FALSE);
1851 }
1852
1854 {
1856
1858 return rc == STATUS_OK && enabled == TRUE;
1859 }
1861 {
1863
1865 return rc == STATUS_OK && enabled == TRUE;
1866 }
1867
1869 {
1870 return setProperty(STREAM_PROPERTY_GAIN, gain);
1871 }
1873 {
1874 return setProperty(STREAM_PROPERTY_EXPOSURE, exposure);
1875 }
1877 {
1878 int gain;
1879 Status rc = getProperty(STREAM_PROPERTY_GAIN, &gain);
1880 if (rc != STATUS_OK)
1881 {
1882 return 100;
1883 }
1884 return gain;
1885 }
1887 {
1888 int exposure;
1889 Status rc = getProperty(STREAM_PROPERTY_EXPOSURE, &exposure);
1890 if (rc != STATUS_OK)
1891 {
1892 return 0;
1893 }
1894 return exposure;
1895 }
1896
1897 bool isValid() const {return m_pStream != NULL;}
1898private:
1899 template <class T>
1900 Status getProperty(int propertyId, T* value) const
1901 {
1902 if (!isValid()) return STATUS_NOT_SUPPORTED;
1903
1904 return m_pStream->getProperty<T>(propertyId, value);
1905 }
1906 template <class T>
1907 Status setProperty(int propertyId, const T& value)
1908 {
1909 if (!isValid()) return STATUS_NOT_SUPPORTED;
1910
1911 return m_pStream->setProperty<T>(propertyId, value);
1912 }
1913
1914 friend class VideoStream;
1916 {
1917 m_pStream = pStream;
1918 }
1919
1920 VideoStream* m_pStream;
1921};
1922
1923
1937{
1938public:
1939
1956 {
1957 public:
1959 {
1960 m_deviceConnectedCallbacks.deviceConnected = deviceConnectedCallback;
1961 m_deviceConnectedCallbacks.deviceDisconnected = NULL;
1962 m_deviceConnectedCallbacks.deviceStateChanged = NULL;
1963 m_deviceConnectedCallbacksHandle = NULL;
1964 }
1965
1967 {
1968 }
1969
1981 virtual void onDeviceConnected(const DeviceInfo*) = 0;
1982 private:
1983 static void ONI_CALLBACK_TYPE deviceConnectedCallback(const OniDeviceInfo* pInfo, void* pCookie)
1984 {
1986 pListener->onDeviceConnected(static_cast<const DeviceInfo*>(pInfo));
1987 }
1988
1989 friend class OpenNI;
1990 OniDeviceCallbacks m_deviceConnectedCallbacks;
1991 OniCallbackHandle m_deviceConnectedCallbacksHandle;
1992
1993 };
2011 {
2012 public:
2014 {
2015 m_deviceDisconnectedCallbacks.deviceConnected = NULL;
2016 m_deviceDisconnectedCallbacks.deviceDisconnected = deviceDisconnectedCallback;
2017 m_deviceDisconnectedCallbacks.deviceStateChanged = NULL;
2018 m_deviceDisconnectedCallbacksHandle = NULL;
2019 }
2020
2022 {
2023 }
2024
2033 virtual void onDeviceDisconnected(const DeviceInfo*) = 0;
2034 private:
2035 static void ONI_CALLBACK_TYPE deviceDisconnectedCallback(const OniDeviceInfo* pInfo, void* pCookie)
2036 {
2038 pListener->onDeviceDisconnected(static_cast<const DeviceInfo*>(pInfo));
2039 }
2040
2041 friend class OpenNI;
2042 OniDeviceCallbacks m_deviceDisconnectedCallbacks;
2043 OniCallbackHandle m_deviceDisconnectedCallbacksHandle;
2044 };
2059 {
2060 public:
2062 {
2063 m_deviceStateChangedCallbacks.deviceConnected = NULL;
2064 m_deviceStateChangedCallbacks.deviceDisconnected = NULL;
2065 m_deviceStateChangedCallbacks.deviceStateChanged = deviceStateChangedCallback;
2066 m_deviceStateChangedCallbacksHandle = NULL;
2067 }
2068
2070 {
2071 }
2072
2080 private:
2081 static void ONI_CALLBACK_TYPE deviceStateChangedCallback(const OniDeviceInfo* pInfo, OniDeviceState state, void* pCookie)
2082 {
2084 pListener->onDeviceStateChanged(static_cast<const DeviceInfo*>(pInfo), DeviceState(state));
2085 }
2086
2087 friend class OpenNI;
2088 OniDeviceCallbacks m_deviceStateChangedCallbacks;
2089 OniCallbackHandle m_deviceStateChangedCallbacksHandle;
2090 };
2091
2098 {
2099 return (Status)oniInitialize(ONI_API_VERSION); // provide version of API, to make sure proper struct sizes are used
2100 }
2101
2106 static void shutdown()
2107 {
2108 oniShutdown();
2109 }
2110
2115 {
2118 version.major = oniVersion.major;
2119 version.minor = oniVersion.minor;
2120 version.maintenance = oniVersion.maintenance;
2121 version.build = oniVersion.build;
2122 return version;
2123 }
2124
2132 static const char* getExtendedError()
2133 {
2134 return oniGetExtendedError();
2135 }
2136
2142 {
2143 OniDeviceInfo* m_pDeviceInfos;
2148 }
2149
2159 {
2160 static const int ONI_MAX_STREAMS = 50;
2162
2164 {
2165 printf("Too many streams for wait: %d > %d\n", streamCount, ONI_MAX_STREAMS);
2166 return STATUS_BAD_PARAMETER;
2167 }
2168
2169 *pReadyStreamIndex = -1;
2170 for (int i = 0; i < streamCount; ++i)
2171 {
2172 if (pStreams[i] != NULL)
2173 {
2174 streams[i] = pStreams[i]->_getHandle();
2175 }
2176 else
2177 {
2178 streams[i] = NULL;
2179 }
2180 }
2182
2183 return rc;
2184 }
2185
2194 {
2195 if (pListener->m_deviceConnectedCallbacksHandle != NULL)
2196 {
2197 return STATUS_ERROR;
2198 }
2199 return (Status)oniRegisterDeviceCallbacks(&pListener->m_deviceConnectedCallbacks, pListener, &pListener->m_deviceConnectedCallbacksHandle);
2200 }
2209 {
2210 if (pListener->m_deviceDisconnectedCallbacksHandle != NULL)
2211 {
2212 return STATUS_ERROR;
2213 }
2214 return (Status)oniRegisterDeviceCallbacks(&pListener->m_deviceDisconnectedCallbacks, pListener, &pListener->m_deviceDisconnectedCallbacksHandle);
2215 }
2224 {
2225 if (pListener->m_deviceStateChangedCallbacksHandle != NULL)
2226 {
2227 return STATUS_ERROR;
2228 }
2229 return (Status)oniRegisterDeviceCallbacks(&pListener->m_deviceStateChangedCallbacks, pListener, &pListener->m_deviceStateChangedCallbacksHandle);
2230 }
2239 {
2240 oniUnregisterDeviceCallbacks(pListener->m_deviceConnectedCallbacksHandle);
2241 pListener->m_deviceConnectedCallbacksHandle = NULL;
2242 }
2251 {
2252 oniUnregisterDeviceCallbacks(pListener->m_deviceDisconnectedCallbacksHandle);
2253 pListener->m_deviceDisconnectedCallbacksHandle = NULL;
2254 }
2263 {
2264 oniUnregisterDeviceCallbacks(pListener->m_deviceStateChangedCallbacksHandle);
2265 pListener->m_deviceStateChangedCallbacksHandle = NULL;
2266 }
2267
2277 {
2279 }
2280
2291 {
2293 }
2294
2305 {
2307 }
2308
2318 {
2320 }
2321
2331 {
2333 }
2334
2335 #if ONI_PLATFORM == ONI_PLATFORM_ANDROID_ARM
2346 {
2348 }
2349 #endif
2350
2351private:
2352 OpenNI()
2353 {
2354 }
2355};
2356
2393{
2394public:
2406 {
2407 float depthX, depthY, depthZ;
2409 *pDepthX = (int)depthX;
2410 *pDepthY = (int)depthY;
2412 return rc;
2413 }
2414
2425 static Status convertWorldToDepth(const VideoStream& depthStream, float worldX, float worldY, float worldZ, float* pDepthX, float* pDepthY, float* pDepthZ)
2426 {
2428 }
2429
2441 {
2442 return (Status)oniCoordinateConverterDepthToWorld(depthStream._getHandle(), float(depthX), float(depthY), float(depthZ), pWorldX, pWorldY, pWorldZ);
2443 }
2444
2455 static Status convertDepthToWorld(const VideoStream& depthStream, float depthX, float depthY, float depthZ, float* pWorldX, float* pWorldY, float* pWorldZ)
2456 {
2458 }
2459
2472 {
2474 }
2475};
2476
2492{
2493public:
2498 Recorder() : m_recorder(NULL)
2499 {
2500 }
2501
2506 {
2507 destroy();
2508 }
2509
2522 {
2523 if (!isValid())
2524 {
2525 return (Status)oniCreateRecorder(fileName, &m_recorder);
2526 }
2527 return STATUS_ERROR;
2528 }
2529
2536 bool isValid() const
2537 {
2538 return NULL != getHandle();
2539 }
2540
2552 {
2553 if (!isValid() || !stream.isValid())
2554 {
2555 return STATUS_ERROR;
2556 }
2558 m_recorder,
2559 stream._getHandle(),
2561 }
2562
2570 {
2571 if (!isValid())
2572 {
2573 return STATUS_ERROR;
2574 }
2575 return (Status)oniRecorderStart(m_recorder);
2576 }
2577
2581 void stop()
2582 {
2583 if (isValid())
2584 {
2585 oniRecorderStop(m_recorder);
2586 }
2587 }
2588
2592 void destroy()
2593 {
2594 if (isValid())
2595 {
2596 oniRecorderDestroy(&m_recorder);
2597 }
2598 }
2599
2600private:
2601 Recorder(const Recorder&);
2602 Recorder& operator=(const Recorder&);
2603
2607 OniRecorderHandle getHandle() const
2608 {
2609 return m_recorder;
2610 }
2611
2612
2613 OniRecorderHandle m_recorder;
2614};
2615
2616// Implemetation
2618{
2621 if (rc != STATUS_OK)
2622 {
2623 return rc;
2624 }
2625
2626 m_isOwner = true;
2627 _setHandle(streamHandle);
2628
2630 {
2631 m_pCameraSettings = new CameraSettings(this);
2632 }
2633
2634 return STATUS_OK;
2635}
2636
2638{
2639 if (!isValid())
2640 {
2641 return;
2642 }
2643
2644 if (m_pCameraSettings != NULL)
2645 {
2646 delete m_pCameraSettings;
2647 m_pCameraSettings = NULL;
2648 }
2649
2650 if (m_stream != NULL)
2651 {
2652 if(m_isOwner)
2653 oniStreamDestroy(m_stream);
2654 m_stream = NULL;
2655 }
2656}
2657
2659{
2660 //If we are not the owners, we stick with our own device
2661 if(!m_isOwner)
2662 {
2663 if(isValid()){
2664 return STATUS_OK;
2665 }else{
2666 return STATUS_OUT_OF_FLOW;
2667 }
2668 }
2669
2672 if (rc != STATUS_OK)
2673 {
2674 return rc;
2675 }
2676
2677 _setHandle(deviceHandle);
2678
2679 return STATUS_OK;
2680}
2681
2682Status Device::_openEx(const char* uri, const char* mode)
2683{
2684 //If we are not the owners, we stick with our own device
2685 if(!m_isOwner)
2686 {
2687 if(isValid()){
2688 return STATUS_OK;
2689 }else{
2690 return STATUS_OUT_OF_FLOW;
2691 }
2692 }
2693
2694 OniDeviceHandle deviceHandle;
2695 Status rc = (Status)oniDeviceOpenEx(uri, mode, &deviceHandle);
2696 if (rc != STATUS_OK)
2697 {
2698 return rc;
2699 }
2700
2701 _setHandle(deviceHandle);
2702
2703 return STATUS_OK;
2704}
2705
2706Status Device::_setHandle(OniDeviceHandle deviceHandle)
2707{
2708 if (m_device == NULL)
2709 {
2710 m_device = deviceHandle;
2711
2712 clearSensors();
2713
2714 oniDeviceGetInfo(m_device, &m_deviceInfo);
2715
2716 if (isFile())
2717 {
2718 m_pPlaybackControl = new PlaybackControl(this);
2719 }
2720
2721 // Read deviceInfo
2722 return STATUS_OK;
2723 }
2724
2725 return STATUS_OUT_OF_FLOW;
2726}
2727
2729{
2730 if (m_pPlaybackControl != NULL)
2731 {
2732 delete m_pPlaybackControl;
2733 m_pPlaybackControl = NULL;
2734 }
2735
2736 if (m_device != NULL)
2737 {
2738 if(m_isOwner)
2739 {
2740 oniDeviceClose(m_device);
2741 }
2742
2743 m_device = NULL;
2744 }
2745}
2746
2747
2748}
2749
2750#endif // _OPEN_NI_HPP_
Definition OpenNI.h:101
int getSize() const
Definition OpenNI.h:129
Array()
Definition OpenNI.h:106
Array(const T *data, int count)
Definition OpenNI.h:115
const T & operator[](int index) const
Definition OpenNI.h:134
~Array()
Definition OpenNI.h:120
Definition OpenNI.h:1841
Status setAutoExposureEnabled(bool enabled)
Definition OpenNI.h:1844
Status setExposure(int exposure)
Definition OpenNI.h:1872
Status setGain(int gain)
Definition OpenNI.h:1868
bool getAutoWhiteBalanceEnabled() const
Definition OpenNI.h:1860
int getGain()
Definition OpenNI.h:1876
int getExposure()
Definition OpenNI.h:1886
bool isValid() const
Definition OpenNI.h:1897
Status setAutoWhiteBalanceEnabled(bool enabled)
Definition OpenNI.h:1848
bool getAutoExposureEnabled() const
Definition OpenNI.h:1853
Definition OpenNI.h:2393
static Status convertDepthToWorld(const VideoStream &depthStream, int depthX, int depthY, DepthPixel depthZ, float *pWorldX, float *pWorldY, float *pWorldZ)
Definition OpenNI.h:2440
static Status convertWorldToDepth(const VideoStream &depthStream, float worldX, float worldY, float worldZ, int *pDepthX, int *pDepthY, DepthPixel *pDepthZ)
Definition OpenNI.h:2405
static Status convertDepthToColor(const VideoStream &depthStream, const VideoStream &colorStream, int depthX, int depthY, DepthPixel depthZ, int *pColorX, int *pColorY)
Definition OpenNI.h:2471
static Status convertDepthToWorld(const VideoStream &depthStream, float depthX, float depthY, float depthZ, float *pWorldX, float *pWorldY, float *pWorldZ)
Definition OpenNI.h:2455
static Status convertWorldToDepth(const VideoStream &depthStream, float worldX, float worldY, float worldZ, float *pDepthX, float *pDepthY, float *pDepthZ)
Definition OpenNI.h:2425
Definition OpenNI.h:1243
Status setProperty(int propertyId, const T &value)
Definition OpenNI.h:1551
Device()
Definition OpenNI.h:1249
void close()
Definition OpenNI.h:2728
Status open(const char *uri)
Definition OpenNI.h:2658
Status setImageRegistrationMode(ImageRegistrationMode mode)
Definition OpenNI.h:1486
bool isPropertySupported(int propertyId) const
Definition OpenNI.h:1577
Status getProperty(int propertyId, T *value) const
Definition OpenNI.h:1566
bool isFile() const
Definition OpenNI.h:1504
Device(OniDeviceHandle handle)
Definition OpenNI.h:1258
~Device()
Definition OpenNI.h:1267
Status invoke(int commandId, void *data, int dataSize)
Definition OpenNI.h:1591
bool isCommandSupported(int commandId) const
Definition OpenNI.h:1616
bool isImageRegistrationModeSupported(ImageRegistrationMode mode) const
Definition OpenNI.h:1450
Status setDepthColorSyncEnabled(bool isEnabled)
Definition OpenNI.h:1519
PlaybackControl * getPlaybackControl()
Definition OpenNI.h:1409
Status invoke(int propertyId, T &value)
Definition OpenNI.h:1606
const SensorInfo * getSensorInfo(SensorType sensorType)
Definition OpenNI.h:1369
Status getProperty(int propertyId, void *data, int *dataSize) const
Definition OpenNI.h:1422
const DeviceInfo & getDeviceInfo() const
Definition OpenNI.h:1322
Status setProperty(int propertyId, const void *data, int dataSize)
Definition OpenNI.h:1438
bool getDepthColorSyncEnabled()
Definition OpenNI.h:1535
ImageRegistrationMode getImageRegistrationMode() const
Definition OpenNI.h:1462
bool isValid() const
Definition OpenNI.h:1495
bool hasSensor(SensorType sensorType)
Definition OpenNI.h:1334
Definition OpenNI.h:373
const char * getUri() const
Definition OpenNI.h:379
const char * getVendor() const
Definition OpenNI.h:381
const char * getName() const
Definition OpenNI.h:383
uint16_t getUsbProductId() const
Definition OpenNI.h:387
uint16_t getUsbVendorId() const
Definition OpenNI.h:385
virtual ~DeviceConnectedListener()
Definition OpenNI.h:1966
DeviceConnectedListener()
Definition OpenNI.h:1958
virtual void onDeviceConnected(const DeviceInfo *)=0
virtual void onDeviceDisconnected(const DeviceInfo *)=0
DeviceDisconnectedListener()
Definition OpenNI.h:2013
virtual ~DeviceDisconnectedListener()
Definition OpenNI.h:2021
virtual void onDeviceStateChanged(const DeviceInfo *, DeviceState)=0
DeviceStateChangedListener()
Definition OpenNI.h:2061
virtual ~DeviceStateChangedListener()
Definition OpenNI.h:2069
Definition OpenNI.h:1937
static Status initialize()
Definition OpenNI.h:2097
static Status addDeviceConnectedListener(DeviceConnectedListener *pListener)
Definition OpenNI.h:2193
static Status setLogConsoleOutput(bool bConsoleOutput)
Definition OpenNI.h:2317
static void shutdown()
Definition OpenNI.h:2106
static void removeDeviceConnectedListener(DeviceConnectedListener *pListener)
Definition OpenNI.h:2238
static Status setLogMinSeverity(int nMinSeverity)
Definition OpenNI.h:2304
static const char * getExtendedError()
Definition OpenNI.h:2132
static Status setLogOutputFolder(const char *strLogOutputFolder)
Definition OpenNI.h:2276
static Status setLogFileOutput(bool bFileOutput)
Definition OpenNI.h:2330
static Status addDeviceDisconnectedListener(DeviceDisconnectedListener *pListener)
Definition OpenNI.h:2208
static Status addDeviceStateChangedListener(DeviceStateChangedListener *pListener)
Definition OpenNI.h:2223
static Version getVersion()
Definition OpenNI.h:2114
static void removeDeviceStateChangedListener(DeviceStateChangedListener *pListener)
Definition OpenNI.h:2262
static Status waitForAnyStream(VideoStream **pStreams, int streamCount, int *pReadyStreamIndex, int timeout=TIMEOUT_FOREVER)
Definition OpenNI.h:2158
static Status getLogFileName(char *strFileName, int nBufferSize)
Definition OpenNI.h:2290
static void enumerateDevices(Array< DeviceInfo > *deviceInfoList)
Definition OpenNI.h:2141
static void removeDeviceDisconnectedListener(DeviceDisconnectedListener *pListener)
Definition OpenNI.h:2250
Definition OpenNI.h:1662
~PlaybackControl()
Definition OpenNI.h:1670
Status setSpeed(float speed)
Definition OpenNI.h:1716
int getNumberOfFrames(const VideoStream &stream) const
Definition OpenNI.h:1795
friend class Device
Definition OpenNI.h:1828
bool getRepeatEnabled() const
Definition OpenNI.h:1730
Status seek(const VideoStream &stream, int frameIndex)
Definition OpenNI.h:1775
Status setRepeatEnabled(bool repeat)
Definition OpenNI.h:1755
bool isValid() const
Definition OpenNI.h:1806
float getSpeed() const
Definition OpenNI.h:1695
Definition OpenNI.h:2492
~Recorder()
Definition OpenNI.h:2505
void stop()
Definition OpenNI.h:2581
void destroy()
Definition OpenNI.h:2592
Status create(const char *fileName)
Definition OpenNI.h:2521
Recorder()
Definition OpenNI.h:2498
Status attach(VideoStream &stream, bool allowLossyCompression=false)
Definition OpenNI.h:2551
bool isValid() const
Definition OpenNI.h:2536
Status start()
Definition OpenNI.h:2569
Definition OpenNI.h:315
SensorType getSensorType() const
Definition OpenNI.h:321
const Array< VideoMode > & getSupportedVideoModes() const
Definition OpenNI.h:330
Definition OpenNI.h:407
~VideoFrameRef()
Definition OpenNI.h:421
bool getCroppingEnabled() const
Definition OpenNI.h:542
VideoFrameRef & operator=(const VideoFrameRef &other)
Definition OpenNI.h:441
uint64_t getTimestamp() const
Definition OpenNI.h:497
int getFrameIndex() const
Definition OpenNI.h:512
bool isValid() const
Definition OpenNI.h:578
int getDataSize() const
Definition OpenNI.h:452
int getCropOriginY() const
Definition OpenNI.h:560
int getHeight() const
Definition OpenNI.h:533
void release()
Definition OpenNI.h:587
SensorType getSensorType() const
Definition OpenNI.h:473
int getCropOriginX() const
Definition OpenNI.h:551
int getWidth() const
Definition OpenNI.h:523
int getStrideInBytes() const
Definition OpenNI.h:570
VideoFrameRef(const VideoFrameRef &other)
Definition OpenNI.h:431
const void * getData() const
Definition OpenNI.h:462
const VideoMode & getVideoMode() const
Definition OpenNI.h:485
VideoFrameRef()
Definition OpenNI.h:413
Definition OpenNI.h:204
void setResolution(int resolutionX, int resolutionY)
Definition OpenNI.h:278
VideoMode(const VideoMode &other)
Definition OpenNI.h:219
VideoMode()
Definition OpenNI.h:211
int getResolutionX() const
Definition OpenNI.h:249
int getResolutionY() const
Definition OpenNI.h:255
PixelFormat getPixelFormat() const
Definition OpenNI.h:243
void setFps(int fps)
Definition OpenNI.h:290
VideoMode & operator=(const VideoMode &other)
Definition OpenNI.h:230
int getFps() const
Definition OpenNI.h:261
void setPixelFormat(PixelFormat format)
Definition OpenNI.h:269
virtual void * allocateFrameBuffer(int size)=0
virtual ~FrameAllocator()
Definition OpenNI.h:691
virtual void freeFrameBuffer(void *data)=0
virtual void onNewFrame(VideoStream &)=0
NewFrameListener()
Definition OpenNI.h:661
virtual ~NewFrameListener()
Definition OpenNI.h:665
Definition OpenNI.h:646
~VideoStream()
Definition OpenNI.h:731
Status start()
Definition OpenNI.h:779
bool isValid() const
Definition OpenNI.h:740
Status readFrame(VideoFrameRef *pFrame)
Definition OpenNI.h:812
Status create(const Device &device, SensorType sensorType)
Definition OpenNI.h:2617
bool isCommandSupported(int commandId) const
Definition OpenNI.h:1192
bool getCropping(int *pOriginX, int *pOriginY, int *pWidth, int *pHeight) const
Definition OpenNI.h:1010
CameraSettings * getCameraSettings()
Definition OpenNI.h:893
const SensorInfo & getSensorInfo() const
Definition OpenNI.h:771
int getMaxPixelValue() const
Definition OpenNI.h:966
void removeNewFrameListener(NewFrameListener *pListener)
Definition OpenNI.h:847
Status setVideoMode(const VideoMode &videoMode)
Definition OpenNI.h:956
void stop()
Definition OpenNI.h:792
float getVerticalFieldOfView() const
Definition OpenNI.h:1100
Status setCropping(int originX, int originY, int width, int height)
Definition OpenNI.h:1038
Status setFrameBuffersAllocator(FrameAllocator *pAllocator)
Definition OpenNI.h:863
Status getProperty(int propertyId, T *value) const
Definition OpenNI.h:1132
Status setProperty(int propertyId, const void *data, int dataSize)
Definition OpenNI.h:925
Status addNewFrameListener(NewFrameListener *pListener)
Definition OpenNI.h:833
Status invoke(int commandId, T &value)
Definition OpenNI.h:1182
Status setProperty(int propertyId, const T &value)
Definition OpenNI.h:1117
bool isCroppingSupported() const
Definition OpenNI.h:997
VideoMode getVideoMode() const
Definition OpenNI.h:941
Status getProperty(int propertyId, void *data, int *dataSize) const
Definition OpenNI.h:905
float getHorizontalFieldOfView() const
Definition OpenNI.h:1089
Status invoke(int commandId, void *data, int dataSize)
Definition OpenNI.h:1162
VideoStream(OniStreamHandle handle)
Definition OpenNI.h:722
VideoStream()
Definition OpenNI.h:715
bool isPropertySupported(int propertyId) const
Definition OpenNI.h:1143
void destroy()
Definition OpenNI.h:2637
int getMinPixelValue() const
Definition OpenNI.h:982
Status resetCropping()
Definition OpenNI.h:1053
Status setMirroringEnabled(bool isEnabled)
Definition OpenNI.h:1080
bool getMirroringEnabled() const
Definition OpenNI.h:1064
Definition OpenNI.h:35
uint16_t Grayscale16Pixel
Definition OpenNI.h:41
SensorType
Definition OniEnums.h:42
static const int TIMEOUT_FOREVER
Definition OniEnums.h:82
ImageRegistrationMode
Definition OniEnums.h:76
@ IMAGE_REGISTRATION_OFF
Definition OniEnums.h:77
PixelFormat
Definition OniEnums.h:51
uint16_t DepthPixel
Definition OpenNI.h:38
Status
Definition OniEnums.h:29
@ STATUS_OUT_OF_FLOW
Definition OniEnums.h:35
@ STATUS_ERROR
Definition OniEnums.h:31
@ STATUS_NO_DEVICE
Definition OniEnums.h:36
@ STATUS_OK
Definition OniEnums.h:30
@ STATUS_BAD_PARAMETER
Definition OniEnums.h:34
@ STATUS_NOT_SUPPORTED
Definition OniEnums.h:33
static const char * ANY_DEVICE
Definition OpenNI.h:90
DeviceState
Definition OniEnums.h:68
Definition OpenNI.h:59
uint8_t b
Definition OpenNI.h:65
uint8_t r
Definition OpenNI.h:61
uint8_t g
Definition OpenNI.h:63
Definition OpenNI.h:46
int major
Definition OpenNI.h:48
int build
Definition OpenNI.h:54
int maintenance
Definition OpenNI.h:52
int minor
Definition OpenNI.h:50
Definition OpenNI.h:74
uint8_t u
Definition OpenNI.h:76
uint8_t y2
Definition OpenNI.h:82
uint8_t v
Definition OpenNI.h:80
uint8_t y1
Definition OpenNI.h:78