File: OniDevice.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 (305 lines) | stat: -rw-r--r-- 8,841 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
/*****************************************************************************
*                                                                            *
*  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 "OniDevice.h"
#include "OniStream.h"
#include "OniContext.h"
#include "OniSensor.h"
#include <OniProperties.h>
#include <XnLog.h>

#define XN_MASK_ONI_DEVICE "OniDevice"

ONI_NAMESPACE_IMPLEMENTATION_BEGIN

Device::Device(DeviceDriver* pDeviceDriver, const DriverHandler& driverHandler, FrameManager& frameManager, const OniDeviceInfo* pDeviceInfo, xnl::ErrorLogger& errorLogger) : 
	m_driverHandler(driverHandler),
	m_frameManager(frameManager),
	m_errorLogger(errorLogger),
	m_active(false),
	m_openCount(0),
	m_deviceHandle(NULL),
	m_pDeviceDriver(pDeviceDriver),
	m_depthColorSyncHandle(NULL),
	m_pContext(NULL),
	m_syncEnabled(FALSE)
{
	m_pInfo = XN_NEW(OniDeviceInfo);
	xnOSMemCopy(m_pInfo, pDeviceInfo, sizeof(OniDeviceInfo));
	xnOSMemSet(&m_sensors, 0, sizeof(m_sensors));
}
Device::~Device()
{
	while (m_openCount > 0)
	{
		close();
	}

	XN_DELETE(m_pInfo);
	m_pInfo = NULL;
}

OniStatus Device::open(const char* mode)
{
	if (m_openCount == 0)
	{
		m_deviceHandle = m_driverHandler.deviceOpen(m_pInfo->uri, mode);
		if (m_deviceHandle == NULL)
		{
			return ONI_STATUS_ERROR;
		}
	}
	m_openCount++;

	return ONI_STATUS_OK;
}

OniStatus Device::close()
{
	--m_openCount;

	if (m_openCount == 0)
	{
		for (int i = 0; i < MAX_SENSORS_PER_DEVICE; ++i)
		{
			if (m_sensors[i] != NULL)
			{
				XN_DELETE(m_sensors[i]);
				m_sensors[i] = NULL;
			}
		}

		if (m_deviceHandle != NULL)
		{
			m_driverHandler.deviceClose(m_deviceHandle);
		}
		m_deviceHandle = NULL;
	}

	return ONI_STATUS_OK;
}

const OniDeviceInfo* Device::getInfo() const {return m_pInfo;}

OniStatus Device::getSensorInfoList(OniSensorInfo** pSensorInfos, int& numSensors)
{
	return m_driverHandler.deviceGetSensorInfoList(m_deviceHandle, pSensorInfos, &numSensors);
}

VideoStream* Device::createStream(OniSensorType sensorType)
{
	OniSensorInfo* pSensorInfos;
	OniSensorInfo* pSensorInfo = NULL;

	int numSensors = 0;
	getSensorInfoList(&pSensorInfos, numSensors);
	for (int i = 0; i < numSensors; ++i)
	{
		if (pSensorInfos[i].sensorType == sensorType)
		{
			pSensorInfo = &pSensorInfos[i];
			break;
		}
	}

	if (pSensorInfo == NULL)
	{
		m_errorLogger.Append("Device: Can't find this source %d", sensorType);
		return NULL;
	}

	// make sure our sensor array is big enough
	if ((int)sensorType >= MAX_SENSORS_PER_DEVICE)
	{
		xnLogError(XN_MASK_ONI_DEVICE, "Internal error!");
		m_errorLogger.Append("Device: Can't find this source %d", sensorType);
		XN_ASSERT(FALSE);
		return NULL;
	}

	xnl::AutoCSLocker lock(m_cs);
	if (m_sensors[sensorType] == NULL)
	{
		m_sensors[sensorType] = XN_NEW(Sensor, m_errorLogger, m_frameManager, m_driverHandler);
		if (m_sensors[sensorType] == NULL)
		{
			XN_ASSERT(FALSE);
			return NULL;
		}
	}

	{
		// check if stream already exists. Do this in a lock to make it thread-safe
		xnl::AutoCSLocker lock(m_sensors[sensorType]->m_refCountCS);
		if (m_sensors[sensorType]->m_streamCount == 0)
		{
			void* streamHandle = m_driverHandler.deviceCreateStream(m_deviceHandle, sensorType);
			if (streamHandle == NULL)
			{
				m_errorLogger.Append("Stream: couldn't create using source %d", sensorType);
				return NULL;
			}

			m_sensors[sensorType]->setDriverStream(streamHandle);
		}

		++m_sensors[sensorType]->m_streamCount;
	}

	VideoStream* pStream = XN_NEW(VideoStream, m_sensors[sensorType], pSensorInfo, *this, m_driverHandler, m_frameManager, m_errorLogger);
	m_streams.AddLast(pStream);

	if ((sensorType == ONI_SENSOR_DEPTH || sensorType == ONI_SENSOR_COLOR) &&
		m_depthColorSyncHandle != NULL && m_pContext != NULL)
	{
		refreshDepthColorSyncState();
	}
	return pStream;
}

OniStatus oni::implementation::Device::setProperty(int propertyId, const void* data, int dataSize)
{
	OniStatus rc = m_driverHandler.deviceSetProperty(m_deviceHandle, propertyId, data, dataSize);
	if (rc != ONI_STATUS_OK)
	{
		m_errorLogger.Append("Device.setProperty(%x) failed\n", propertyId);
	}
	return rc;
}
OniStatus oni::implementation::Device::getProperty(int propertyId, void* data, int* pDataSize)
{
	OniStatus rc = m_driverHandler.deviceGetProperty(m_deviceHandle, propertyId, data, pDataSize);
	if (rc != ONI_STATUS_OK)
	{
		m_errorLogger.Append("Device.getProperty(%x) failed\n", propertyId);
	}
	return rc;
}
OniBool oni::implementation::Device::isPropertySupported(int propertyId)
{
	return m_driverHandler.deviceIsPropertySupported(m_deviceHandle, propertyId);
}
void Device::notifyAllProperties()
{
	m_driverHandler.deviceNotifyAllProperties(m_deviceHandle);
}
OniStatus Device::invoke(int commandId, void* data, int dataSize)
{
	if (commandId == ONI_DEVICE_COMMAND_SEEK)
	{
		if (dataSize != sizeof(OniSeek))
		{
			return ONI_STATUS_BAD_PARAMETER;
		}

		// Change seek's stream handle.
		Device::Seek seek;
		OniSeek* pSeek = (OniSeek*)data;
		seek.frameId = pSeek->frameIndex;
		seek.pStream = ((_OniStream*)pSeek->stream)->pStream->getHandle();

		// Update data to point to new structure.
		data = &seek;
		dataSize = sizeof(seek);
	}

	return m_driverHandler.deviceInvoke(m_deviceHandle, commandId, data, dataSize);
}
OniBool Device::isCommandSupported(int commandId)
{
	return m_driverHandler.deviceIsCommandSupported(m_deviceHandle, commandId);
}
OniStatus Device::tryManualTrigger()
{
	return m_driverHandler.tryManualTrigger(m_deviceHandle);
}

void Device::refreshDepthColorSyncState()
{
	if (!m_syncEnabled)
		return;
	Context* pTmpContext = m_pContext;
	disableDepthColorSync();
	enableDepthColorSync(pTmpContext);
}

void Device::clearStream(VideoStream* pStream)
{
	xnl::AutoCSLocker lock(m_cs);
	m_streams.Remove(pStream);

	if ((pStream->getSensorInfo()->sensorType == ONI_SENSOR_DEPTH ||
		pStream->getSensorInfo()->sensorType == ONI_SENSOR_COLOR) &&
		m_depthColorSyncHandle != NULL && m_pContext != NULL)
	{
		refreshDepthColorSyncState();
	}
}

OniStatus Device::enableDepthColorSync(Context* pContext)
{
	m_pContext = pContext;
	m_syncEnabled = TRUE;
	xnl::Array<VideoStream*> streamArray(m_streams.Size());
	streamArray.SetSize(m_streams.Size());

	int streamsUsed = 0;
	for (xnl::List<VideoStream*>::Iterator iter = m_streams.Begin(); iter != m_streams.End(); ++iter)
	{
		if (((*iter)->getSensorInfo()->sensorType == ONI_SENSOR_DEPTH || (*iter)->getSensorInfo()->sensorType == ONI_SENSOR_COLOR) &&
			(*iter)->isStarted())
		{
			streamArray[streamsUsed++] = *iter;
		}
	}

	if (streamsUsed == 0)
	{
		return ONI_STATUS_OK;
	}
	return m_pContext->enableFrameSyncEx(streamArray.GetData(), streamsUsed, m_pDeviceDriver, &m_depthColorSyncHandle);
}
void Device::disableDepthColorSync()
{
	if (m_pContext == NULL || m_depthColorSyncHandle == NULL || !m_syncEnabled)
		return;
	m_pContext->disableFrameSync(m_depthColorSyncHandle);
	m_depthColorSyncHandle = NULL;
	m_pContext = NULL;
	m_syncEnabled = FALSE;
}

OniBool Device::isDepthColorSyncEnabled()
{
	return m_syncEnabled;
}

void ONI_CALLBACK_TYPE Device::stream_PropertyChanged(void* /*deviceHandle*/, int /*propertyId*/, const void* /*data*/, int /*dataSize*/, void* pCookie)
{
	Device* pStream = (Device*)pCookie;
	XN_REFERENCE_VARIABLE(pStream);
}

OniBool Device::isImageRegistrationModeSupported(OniImageRegistrationMode mode)
{
	return m_driverHandler.deviceIsImageRegistrationModeSupported(m_deviceHandle, mode);
}

ONI_NAMESPACE_IMPLEMENTATION_END