File: KinectDriver.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 (219 lines) | stat: -rw-r--r-- 5,925 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
#include "KinectDriver.h"
#include "KinectDevice.h"
#include <Shlobj.h>
#include "NuiApi.h"
#include <atlstr.h>
#include "XnLog.h"

using namespace oni::driver;
using namespace kinect_device;
static const char VENDOR_VAL[] = "Microsoft";
static const char NAME_VAL[] = "Kinect";
#define MICROSOFT_VENDOR_ID 0x045e
#define KINECT_FOR_WINDOWS_PRODUCT_ID 0x02bf

KinectDriver::KinectDriver(OniDriverServices* pDriverServices) : DriverBase(pDriverServices)
{
	NuiSetDeviceStatusCallback( &(KinectDriver::StatusProc), this);
}
	
KinectDriver::~KinectDriver()
{
	NuiSetDeviceStatusCallback(NULL, NULL);	
}

OniStatus KinectDriver::initialize(DeviceConnectedCallback connectedCallback, DeviceDisconnectedCallback disconnectedCallback, DeviceStateChangedCallback deviceStateChangedCallback, void* pCookie)
{
	HRESULT hr;
	int iSensorCount = 0;
	INuiSensor * pNuiSensor;
	DriverBase::initialize(connectedCallback, disconnectedCallback, deviceStateChangedCallback, pCookie);
	hr = NuiGetSensorCount(&iSensorCount);
	if (FAILED(hr))
	{
		return ONI_STATUS_OK;
	}

	// Look at each Kinect sensor
	for (int i = 0; i < iSensorCount; ++i)
	{
		// Create the sensor so we can check status, if we can't create it, move on to the next
		hr = NuiCreateSensorByIndex(i, &pNuiSensor);
		if (FAILED(hr))
		{
			continue;
		}

		// Get the status of the sensor, and if connected, then we can initialize it
		hr = pNuiSensor->NuiStatus();
		OniDeviceInfo* pInfo = XN_NEW(OniDeviceInfo);
		BSTR str = pNuiSensor->NuiDeviceConnectionId();
		size_t convertedChars = 0;
		const size_t newsize = ONI_MAX_STR;
		size_t origsize = wcslen(str) + 1;
		wcstombs_s(&convertedChars, pInfo->uri, origsize, str, _TRUNCATE);
		xnOSStrCopy(pInfo->vendor, VENDOR_VAL, ONI_MAX_STR);
		xnOSStrCopy(pInfo->name, NAME_VAL, ONI_MAX_STR);
		m_devices[pInfo] = NULL;
		deviceConnected(pInfo);
		deviceStateChanged(pInfo, hr);
		
		// This sensor wasn't OK, so release it since we're not using it
		pNuiSensor->Release();
	}
	return ONI_STATUS_OK;
}

DeviceBase* KinectDriver::deviceOpen(const char* uri, const char* /*mode*/)
{
	for (xnl::Hash<OniDeviceInfo*, oni::driver::DeviceBase*>::Iterator iter = m_devices.Begin(); iter != m_devices.End(); ++iter)
	{
		if (xnOSStrCmp(iter->Key()->uri, uri) == 0)
		{
			// Found
			if (iter->Value() != NULL)
			{
				// already using
				return iter->Value();
			}
			else
			{
				INuiSensor * pNuiSensor;
				HRESULT hr;
				size_t convertedChars = 0;
				wchar_t wcstring[ONI_MAX_STR];
				mbstowcs_s(&convertedChars, wcstring, ONI_MAX_STR, uri, _TRUNCATE);
				// Create the sensor so we can check status, if we can't create it, move on to the next
				hr = NuiCreateSensorById(wcstring, &pNuiSensor);

				if (FAILED(hr))
				{
					return NULL;
				}

				if (NULL != pNuiSensor)
				{
					// Initialize the Kinect and specify that we'll be using color
					hr = pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH); 
					KinectDevice* pDevice = XN_NEW(KinectDevice, pNuiSensor);
					if (pDevice == NULL)
					{
						return NULL;
					}
					iter->Value() = pDevice;
					return pDevice;
				}

				if (NULL == pNuiSensor || FAILED(hr))
				{
					return NULL;
				}
			}			
		}
	}
	return NULL;	
}

void kinect_device::KinectDriver::deviceClose(oni::driver::DeviceBase* pDevice)
{
	for (xnl::Hash<OniDeviceInfo*, oni::driver::DeviceBase*>::Iterator iter = m_devices.Begin(); iter != m_devices.End(); ++iter)
	{
		if (iter->Value() == pDevice)
		{
			iter->Value() = NULL;
			XN_DELETE(pDevice);
			return;
		}
	}

	// not our device?!
	XN_ASSERT(FALSE);
}

void KinectDriver::shutdown()
{
}

OniStatus KinectDriver::tryDevice(const char* uri)
{
	return ONI_STATUS_OK;	
}

void* KinectDriver::enableFrameSync(StreamBase** pStreams, int streamCount)
{
	return NULL;
}

void KinectDriver::disableFrameSync(void* frameSyncGroup)
{

}

void KinectDriver::StatusUpdate(const OLECHAR* instanceName, bool isConnected)
{
	char str[ONI_MAX_STR];
	size_t convertedChars = 0;
	const size_t newsize = ONI_MAX_STR;
	size_t origsize = wcslen(instanceName) + 1;
	wcstombs_s(&convertedChars, str, origsize, instanceName, _TRUNCATE);
	for (xnl::Hash<OniDeviceInfo*, oni::driver::DeviceBase*>::Iterator iter = m_devices.Begin(); iter != m_devices.End(); ++iter)
	{
		if (xnOSStrCmp(iter->Key()->uri, str) == 0)
		{
			if (isConnected)
			{
				INuiSensor * pNuiSensor;
				HRESULT hr = NuiCreateSensorById( instanceName, &pNuiSensor);
				if (FAILED(hr))
				{
					return;
				}
				// Get the status of the sensor, and if connected, then we can initialize it
				hr = pNuiSensor->NuiStatus();
				deviceStateChanged(iter->Key(), (int)hr);
			}
			else
			{
				deviceDisconnected(iter->Key());
				KinectDevice* pDevice = (KinectDevice*)iter->Value();
				OniDeviceInfo* pInfo = (OniDeviceInfo*)iter->Key();
				m_devices.Remove(iter);
				if (pDevice != NULL)
					XN_DELETE(pDevice);
				
				XN_DELETE(pInfo);
			}
			return;
		}
	}
	
	if (isConnected)
	{
		INuiSensor * pNuiSensor;
		HRESULT hr = NuiCreateSensorById( instanceName, &pNuiSensor);
		if (FAILED(hr))
		{
			return;
		}

		// Get the status of the sensor, and if connected, then we can initialize it
		hr = pNuiSensor->NuiStatus();
		OniDeviceInfo* pInfo = XN_NEW(OniDeviceInfo);
		int index = pNuiSensor->NuiInstanceIndex();
		strcpy((char*)pInfo->uri, str);
		xnOSStrCopy(pInfo->vendor, VENDOR_VAL, ONI_MAX_STR);
		xnOSStrCopy(pInfo->name, NAME_VAL, ONI_MAX_STR);
		pInfo->usbVendorId = MICROSOFT_VENDOR_ID;
		pInfo->usbProductId = KINECT_FOR_WINDOWS_PRODUCT_ID;
		m_devices[pInfo] = NULL;
		deviceConnected(pInfo);
		deviceStateChanged(pInfo, hr);
	}
}

void CALLBACK KinectDriver::StatusProc( HRESULT hrStatus, const OLECHAR* instanceName, const OLECHAR* uniqueDeviceName,  void* pUserData )
{      
	((KinectDriver*)pUserData)->StatusUpdate(instanceName,SUCCEEDED( hrStatus ));
}

ONI_EXPORT_DRIVER(kinect_device::KinectDriver)