XnStatus XnDeviceFileWriter::WriteStream(XnStreamData* pStreamOutput) { XnStatus nRetVal = XN_STATUS_OK; // start the timer on the first data written nRetVal = StartTimer(); XN_IS_STATUS_OK(nRetVal); // fill in timestamp if needed if (pStreamOutput->nTimestamp == 0) { XnUInt64 nNow; xnOSQueryTimer(m_Timer, &nNow); if (!IsHighResTimestamps()) { nNow /= 1000; } pStreamOutput->nTimestamp = nNow; } // and write it down (using base) nRetVal = XnStreamWriterDevice::WriteStream(pStreamOutput); XN_IS_STATUS_OK(nRetVal); return (XN_STATUS_OK); }
void XnDeviceFileReader::FrameDelay(XnUInt64 nTimestamp) { if (m_FrameDelay.GetValue() != TRUE) return; if (!IsHighResTimestamps()) nTimestamp *= 1000; // first time if (m_nReferenceTime == 0) { xnOSQueryTimer(m_FrameDelayTimer, &m_nReferenceTime); m_nReferenceTimestamp = nTimestamp; return; } // delay XnUInt64 nNow; xnOSQueryTimer(m_FrameDelayTimer, &nNow); // check how much time has passed in the stream XnUInt64 nStreamDiff; if (nTimestamp < m_nReferenceTimestamp) { nStreamDiff = 0; } else { nStreamDiff = nTimestamp - m_nReferenceTimestamp; } // check how much time passed (for user) XnUInt64 nClockDiff = nNow - m_nReferenceTime; // update reference (so that frame delay will work with Pause / Resume) m_nReferenceTime = nNow; m_nReferenceTimestamp = nTimestamp; // check if we need to wait if (nClockDiff < nStreamDiff) { xnOSSleep(XnUInt32((nStreamDiff - nClockDiff) / 1000)); // take this time as a reference xnOSQueryTimer(m_FrameDelayTimer, &m_nReferenceTime); } }
XN_C_API XnStatus xnOSGetHighResTimeStamp(XnUInt64* nTimeStamp) { // Local function variables XnStatus nRetVal = XN_STATUS_OK; // TODO: Check if the core subsystem is initialized // Validate the input/output pointers (to make sure none of them is NULL) XN_VALIDATE_OUTPUT_PTR(nTimeStamp); // Get the high resolution global timer value nRetVal = xnOSQueryTimer(g_xnOSHighResGlobalTimer, nTimeStamp); XN_IS_STATUS_OK(nRetVal); // All is good... return (XN_STATUS_OK); }
XN_C_API XnStatus xnOSGetTimeStamp(XnUInt64* nTimeStamp) { XN_VALIDATE_OUTPUT_PTR(nTimeStamp); return xnOSQueryTimer(g_xnOSHighResGlobalTimer, nTimeStamp); }
OniStatus Context::waitForStreams(OniStreamHandle* pStreams, int streamCount, int* pStreamIndex, int timeout) { static const int MAX_WAITED_STREAMS = 50; Device* deviceList[MAX_WAITED_STREAMS]; VideoStream* streamsList[MAX_WAITED_STREAMS]; unsigned long long oldestTimestamp = XN_MAX_UINT64; int oldestIndex = -1; if (streamCount > MAX_WAITED_STREAMS) { m_errorLogger.Append("Cannot wait on more than %d streams", MAX_WAITED_STREAMS); return ONI_STATUS_NOT_SUPPORTED; } int numDevices = 0; for (int i = 0; i < streamCount; ++i) { if (pStreams[i] == NULL) { continue; } streamsList[i] = ((_OniStream*)pStreams[i])->pStream; Device* pDevice = &streamsList[i]->getDevice(); // Check if device already exists. bool found = false; for (int j = 0; j < numDevices; ++j) { if (deviceList[j] == pDevice) { found = true; break; } } // Add new device to list. if (!found) { deviceList[numDevices] = pDevice; ++numDevices; } } XN_EVENT_HANDLE hEvent = getThreadEvent(); XnUInt64 passedTime; XnOSTimer workTimer; XnUInt32 timeToWait = timeout; xnOSStartTimer(&workTimer); do { for (int i = 0; i < streamCount; ++i) { if (pStreams[i] == NULL) continue; VideoStream* pStream = ((_OniStream*)pStreams[i])->pStream; pStream->lockFrame(); OniFrame* pFrame = pStream->peekFrame(); if (pFrame != NULL && pFrame->timestamp < oldestTimestamp) { oldestTimestamp = pFrame->timestamp; oldestIndex = i; } pStream->unlockFrame(); } if (oldestIndex != -1) { *pStreamIndex = oldestIndex; break; } // 'Poke' the driver to attempt to receive more frames. for (int j = 0; j < numDevices; ++j) { deviceList[j]->tryManualTrigger(); } if(timeout != ONI_TIMEOUT_FOREVER) { xnOSQueryTimer(workTimer, &passedTime); if((int)passedTime < timeout) timeToWait = timeout - (int)passedTime; else timeToWait = 0; } } while (XN_STATUS_OK == xnOSWaitEvent(hEvent, timeToWait)); xnOSStopTimer(&workTimer); if (oldestIndex != -1) { return ONI_STATUS_OK; } m_errorLogger.Append("waitForStreams: timeout reached"); return ONI_STATUS_TIME_OUT; }