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 518
|
/****************************************************************************
* *
* OpenNI 1.x Alpha *
* Copyright (C) 2011 PrimeSense Ltd. *
* *
* This file is part of OpenNI. *
* *
* OpenNI 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. *
* *
* OpenNI 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 OpenNI. If not, see <http://www.gnu.org/licenses/>. *
* *
****************************************************************************/
#include "MockGenerator.h"
#include <XnPropNames.h>
#include <XnLog.h>
#define XN_MOCK_LOG_MASK "Mock"
MockGenerator::MockGenerator(xn::Context& context, const XnChar* strName, XnBool bAggregateData /* = FALSE */) :
MockProductionNode(context, strName),
m_bAggregateData(bAggregateData),
m_bGenerating(FALSE),
m_bMirror(FALSE),
m_nCurrentDataIdx(0),
m_nNextDataIdx(1),
m_bNewDataAvailable(FALSE),
m_bMirrorCap(FALSE),
m_bFrameSyncCap(FALSE),
m_bFrameSyncWithExists(FALSE),
m_hNodeCreationCallback(NULL),
m_hNodeDestructionCallback(NULL)
{
memset(&m_data, 0, sizeof(m_data));
m_strFrameSyncWith[0] = '\0';
}
MockGenerator::~MockGenerator()
{
if (m_hNodeCreationCallback != NULL)
{
m_context.UnregisterFromNodeCreation(m_hNodeCreationCallback);
m_hNodeCreationCallback = NULL;
}
if (m_hNodeDestructionCallback != NULL)
{
m_context.UnregisterFromNodeDestruction(m_hNodeDestructionCallback);
m_hNodeDestructionCallback = NULL;
}
for (XnUInt32 i = 0; i < NUM_BUFFERS; i++)
{
xnOSFreeAligned(m_data[i].pData);
}
}
XnBool MockGenerator::IsCapabilitySupported(const XnChar* strCapabilityName)
{
if (strcmp(strCapabilityName, XN_CAPABILITY_MIRROR) == 0)
{
return (!m_bStateReady || m_bMirrorCap);
}
else if (strcmp(strCapabilityName, XN_CAPABILITY_FRAME_SYNC) == 0)
{
return (!m_bStateReady || m_bFrameSyncCap);
}
//TODO: Support alt view cap
else
{
return MockProductionNode::IsCapabilitySupported(strCapabilityName);
}
}
XnStatus MockGenerator::SetIntProperty(const XnChar* strName, XnUInt64 nValue)
{
XnStatus nRetVal = XN_STATUS_OK;
if (strcmp(strName, XN_PROP_IS_GENERATING) == 0)
{
SetGenerating((XnBool)nValue);
}
else if (strcmp(strName, XN_PROP_TIMESTAMP) == 0)
{
m_data[m_nNextDataIdx].nTimeStamp = nValue;
}
else if (strcmp(strName, XN_PROP_FRAME_ID) == 0)
{
m_data[m_nNextDataIdx].nFrameID = (XnUInt32)nValue;
}
else if (strcmp(strName, XN_CAPABILITY_MIRROR) == 0)
{
m_bMirrorCap = (XnBool)nValue;
}
else if (strcmp(strName, XN_CAPABILITY_FRAME_SYNC) == 0)
{
m_bFrameSyncCap = (XnBool)nValue;
}
else if (strcmp(strName, XN_PROP_MIRROR) == 0)
{
nRetVal = SetMirror((XnBool)nValue);
XN_IS_STATUS_OK(nRetVal);
}
//TODO: Check for alternative view point cap, framesync cap
else
{
return MockProductionNode::SetIntProperty(strName, nValue);
}
return XN_STATUS_OK;
}
XnStatus MockGenerator::SetStringProperty(const XnChar* strName, const XnChar* strValue)
{
XnStatus nRetVal = XN_STATUS_OK;
if (strcmp(strName, XN_PROP_FRAME_SYNCED_WITH) == 0)
{
nRetVal = SetFrameSyncNode(strValue);
XN_IS_STATUS_OK(nRetVal);
}
else
{
nRetVal = MockProductionNode::SetStringProperty(strName, strValue);
XN_IS_STATUS_OK(nRetVal);
}
return (XN_STATUS_OK);
}
XnStatus MockGenerator::SetGeneralProperty(const XnChar* strName, XnUInt32 nBufferSize, const void* pBuffer)
{
XnStatus nRetVal = XN_STATUS_OK;
if (strcmp(strName, XN_PROP_NEWDATA) == 0)
{
nRetVal = SetNextData(pBuffer, nBufferSize);
XN_IS_STATUS_OK(nRetVal);
}
else
{
nRetVal = MockProductionNode::SetGeneralProperty(strName, nBufferSize, pBuffer);
XN_IS_STATUS_OK(nRetVal);
}
return XN_STATUS_OK;
}
XnStatus MockGenerator::SetNextData(const void *pData, XnUInt32 nSize)
{
XnStatus nRetVal = XN_STATUS_OK;
//Make sure the node is in Generating state so it would be recorded properly
SetGenerating(TRUE);
DataInfo& nextData = m_data[m_nNextDataIdx];
if (!m_bAggregateData)
{
nextData.nDataSize = 0;
}
nRetVal = ResizeBuffer(m_nNextDataIdx, nextData.nDataSize + nSize);
XN_IS_STATUS_OK(nRetVal);
xnOSMemCopy((XnUChar*)nextData.pData + nextData.nDataSize, pData, nSize);
nextData.nDataSize += nSize;
nRetVal = SetNewDataAvailable();
XN_IS_STATUS_OK(nRetVal);
return XN_STATUS_OK;
}
XnStatus MockGenerator::SetNewDataAvailable()
{
m_bNewDataAvailable = TRUE;
return m_newDataAvailableEvent.Raise();
}
XnStatus MockGenerator::StartGenerating()
{
SetGenerating(TRUE);
return XN_STATUS_OK;
}
XnBool MockGenerator::IsGenerating()
{
return m_bGenerating;
}
void MockGenerator::StopGenerating()
{
SetGenerating(FALSE);
}
XnStatus MockGenerator::RegisterToGenerationRunningChange(XnModuleStateChangedHandler handler, void* pCookie, XnCallbackHandle& hCallback)
{
return m_generatingChangedEvent.Register(handler, pCookie, hCallback);
}
void MockGenerator::UnregisterFromGenerationRunningChange(XnCallbackHandle hCallback)
{
m_generatingChangedEvent.Unregister(hCallback);
}
XnStatus MockGenerator::RegisterToNewDataAvailable(XnModuleStateChangedHandler handler, void* pCookie, XnCallbackHandle& hCallback)
{
return m_newDataAvailableEvent.Register(handler, pCookie, hCallback);
}
void MockGenerator::UnregisterFromNewDataAvailable(XnCallbackHandle hCallback)
{
m_newDataAvailableEvent.Unregister(hCallback);
}
XnBool MockGenerator::IsNewDataAvailable(XnUInt64& nTimestamp)
{
if (m_bNewDataAvailable)
{
// the timestamp is used for frame sync algorithm. If frameID is zero, this means
// there is no actual frame, so we don't want the timestamp to be checked for sync.
// instead, return an "invalid" timestamp. This is allowed, as FrameID 0 is a non-valid
// frame.
if (m_data[m_nNextDataIdx].nFrameID == 0)
{
nTimestamp = XN_MAX_UINT64;
}
else
{
nTimestamp = m_data[m_nNextDataIdx].nTimeStamp;
}
}
return m_bNewDataAvailable;
}
XnStatus MockGenerator::UpdateData()
{
if (m_bNewDataAvailable)
{
//Rotate current data and next data
m_nCurrentDataIdx = 1 - m_nCurrentDataIdx;
m_nNextDataIdx = 1 - m_nNextDataIdx;
m_data[m_nNextDataIdx].nDataSize = 0;
m_bNewDataAvailable = FALSE;
}
return XN_STATUS_OK;
}
const void* MockGenerator::GetData()
{
return m_data[m_nCurrentDataIdx].pData;
}
XnUInt32 MockGenerator::GetDataSize()
{
return m_data[m_nCurrentDataIdx].nDataSize;
}
XnUInt64 MockGenerator::GetTimestamp()
{
return m_data[m_nCurrentDataIdx].nTimeStamp;
}
XnUInt32 MockGenerator::GetFrameID()
{
return m_data[m_nCurrentDataIdx].nFrameID;
}
xn::ModuleMirrorInterface* MockGenerator::GetMirrorInterface()
{
// the interface always exists, even if capability is not supported (support may change later on)
return this;
}
xn::ModuleAlternativeViewPointInterface* MockGenerator::GetAlternativeViewPointInterface()
{
//TODO: Support AlternativeViewPointInterface
return NULL;
}
xn::ModuleFrameSyncInterface* MockGenerator::GetFrameSyncInterface()
{
// the interface always exists, even if capability is not supported (support may change later on)
return this;
}
XnStatus MockGenerator::SetMirror(XnBool bMirror)
{
XnStatus nRetVal = XN_STATUS_OK;
if (!m_bMirrorCap)
{
return XN_STATUS_NOT_IMPLEMENTED;
}
if (bMirror != m_bMirror)
{
m_bMirror = bMirror;
nRetVal = m_mirrorChangeEvent.Raise();
XN_IS_STATUS_OK(nRetVal);
}
return XN_STATUS_OK;
}
XnBool MockGenerator::IsMirrored()
{
return m_bMirror;
}
XnStatus MockGenerator::RegisterToMirrorChange(XnModuleStateChangedHandler handler, void* pCookie, XnCallbackHandle& hCallback)
{
if (!m_bMirrorCap)
{
return XN_STATUS_NOT_IMPLEMENTED;
}
return m_mirrorChangeEvent.Register(handler, pCookie, hCallback);
}
void MockGenerator::UnregisterFromMirrorChange(XnCallbackHandle hCallback)
{
if (m_bMirrorCap)
{
m_mirrorChangeEvent.Unregister(hCallback);
}
}
void MockGenerator::SetGenerating(XnBool bGenerating)
{
if (bGenerating != m_bGenerating)
{
m_bGenerating = bGenerating;
m_generatingChangedEvent.Raise();
}
}
XnStatus MockGenerator::OnStateReady()
{
XnStatus nRetVal = XN_STATUS_OK;
nRetVal = MockProductionNode::OnStateReady();
XN_IS_STATUS_OK(nRetVal);
// allocate current buffer (so the app won't get a NULL pointer)
XnUInt32 nNeededSize = GetRequiredBufferSize();
nRetVal = ResizeBuffer(m_nCurrentDataIdx, nNeededSize);
XN_IS_STATUS_OK(nRetVal);
xnOSMemSet(m_data[m_nCurrentDataIdx].pData, 0, nNeededSize);
return (XN_STATUS_OK);
}
XnUInt32 MockGenerator::GetRequiredBufferSize()
{
return 0;
}
XnStatus MockGenerator::ResizeBuffer(XnUInt32 nIndex, XnUInt32 nNeededSize)
{
DataInfo& dataInfo = m_data[nIndex];
if (nNeededSize > dataInfo.nAllocatedSize)
{
xnOSFreeAligned(dataInfo.pData);
dataInfo.pData = xnOSMallocAligned(nNeededSize, XN_DEFAULT_MEM_ALIGN);
XN_VALIDATE_ALLOC_PTR(dataInfo.pData);
dataInfo.nAllocatedSize = nNeededSize;
}
return (XN_STATUS_OK);
}
XnBool MockGenerator::CanFrameSyncWith(xn::ProductionNode& other)
{
if (!m_bFrameSyncCap)
{
return FALSE;
}
if (!other.IsValid())
{
return XN_STATUS_BAD_PARAM;
}
return (strcmp(other.GetName(), m_strFrameSyncWith) == 0);
}
XnStatus MockGenerator::FrameSyncWith(xn::ProductionNode& other)
{
XnStatus nRetVal = XN_STATUS_OK;
if (!other.IsValid())
{
return XN_STATUS_BAD_PARAM;
}
nRetVal = SetFrameSyncNode(other.GetName());
XN_IS_STATUS_OK(nRetVal);
return (XN_STATUS_OK);
}
XnStatus MockGenerator::StopFrameSyncWith(xn::ProductionNode& other)
{
XnStatus nRetVal = XN_STATUS_OK;
if (!other.IsValid())
{
return XN_STATUS_BAD_PARAM;
}
if (strcmp(other.GetName(), m_strFrameSyncWith) != 0)
{
// not synced right now with this node
return XN_STATUS_BAD_PARAM;
}
nRetVal = SetFrameSyncNode("");
XN_IS_STATUS_OK(nRetVal);
return (XN_STATUS_OK);
}
XnBool MockGenerator::IsFrameSyncedWith(xn::ProductionNode& other)
{
return other.IsValid() && (strcmp(other.GetName(), m_strFrameSyncWith) == 0);
}
XnStatus MockGenerator::RegisterToFrameSyncChange(XnModuleStateChangedHandler handler, void* pCookie, XnCallbackHandle& hCallback)
{
return m_frameSyncChangeEvent.Register(handler, pCookie, hCallback);
}
void MockGenerator::UnregisterFromFrameSyncChange(XnCallbackHandle hCallback)
{
m_frameSyncChangeEvent.Unregister(hCallback);
}
XnStatus MockGenerator::SetFrameSyncNode(const XnChar* strOther)
{
XnStatus nRetVal = XN_STATUS_OK;
if (!m_bFrameSyncCap)
{
return XN_STATUS_INVALID_OPERATION;
}
// if we haven't registered yet to the events, do it now (we need to know the lifetime of this other node)
if (m_hNodeCreationCallback == NULL)
{
nRetVal = m_context.RegisterToNodeCreation(OnNodeCreationCallback, this, m_hNodeCreationCallback);
XN_IS_STATUS_OK(nRetVal);
}
if (m_hNodeDestructionCallback == NULL)
{
nRetVal = m_context.RegisterToNodeDestruction(OnNodeDestructionCallback, this, m_hNodeDestructionCallback);
XN_IS_STATUS_OK(nRetVal);
}
if (strcmp(strOther, m_strFrameSyncWith) != 0)
{
nRetVal = xnOSStrCopy(m_strFrameSyncWith, strOther, sizeof(m_strFrameSyncWith));
XN_IS_STATUS_OK(nRetVal);
if (strOther[0] != '\0')
{
// check if node already exists
xn::ProductionNode other;
nRetVal = m_context.GetProductionNodeByName(strOther, other);
m_bFrameSyncWithExists = (nRetVal == XN_STATUS_OK);
}
nRetVal = m_frameSyncChangeEvent.Raise();
XN_IS_STATUS_OK(nRetVal);
}
return (XN_STATUS_OK);
}
void MockGenerator::OnNodeCreation(xn::ProductionNode& createdNode)
{
if (strcmp(createdNode.GetName(), m_strFrameSyncWith) == 0)
{
m_bFrameSyncWithExists = TRUE;
m_frameSyncChangeEvent.Raise();
}
}
void MockGenerator::OnNodeDestruction(const XnChar* strDestroyedNodeName)
{
if (strcmp(strDestroyedNodeName, m_strFrameSyncWith) == 0)
{
m_bFrameSyncWithExists = FALSE;
m_frameSyncChangeEvent.Raise();
}
}
void XN_CALLBACK_TYPE MockGenerator::OnNodeCreationCallback(xn::Context& /*context*/, xn::ProductionNode& createdNode, void* pCookie)
{
MockGenerator* pThis = (MockGenerator*)pCookie;
pThis->OnNodeCreation(createdNode);
}
void XN_CALLBACK_TYPE MockGenerator::OnNodeDestructionCallback(xn::Context& /*context*/, const XnChar* strDestroyedNodeName, void* pCookie)
{
MockGenerator* pThis = (MockGenerator*)pCookie;
pThis->OnNodeDestruction(strDestroyedNodeName);
}
|