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
|
/*****************************************************************************
* *
* 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. *
* *
*****************************************************************************/
/// @file
/// Contains the definition of Device class that implements a virtual OpenNI
/// device, capable of reading data from a *.ONI file.
#include "PlayerStream.h"
#include "PlayerSource.h"
#include "XnMemory.h"
#include "OniProperties.h"
#include "XnPlatform.h"
#include <XnLog.h>
namespace oni_file {
PlayerStream::PlayerStream(PlayerSource* pSource) :
m_pSource(pSource), m_newDataHandle(NULL), m_isStarted(false), m_requiredFrameSize(0)
{
}
/// Destructor.
PlayerStream::~PlayerStream()
{
// Destroy the stream (if it was not destroyed before).
destroy();
}
OniStatus PlayerStream::Initialize()
{
// Register events in the source.
OniStatus rc = m_pSource->RegisterNewDataEvent(OnNewDataCallback, this, m_newDataHandle);
if (rc != ONI_STATUS_OK)
{
destroy();
return rc;
}
return ONI_STATUS_OK;
}
void PlayerStream::destroy()
{
stop();
if (m_newDataHandle != NULL)
{
// Send the destroy event.
DestroyEventArgs destroyEventArgs;
destroyEventArgs.pStream = this;
m_destroyEvent.Raise(destroyEventArgs);
// Unregister from events.
m_pSource->UnregisterNewDataEvent(m_newDataHandle);
m_newDataHandle = NULL;
}
}
OniStatus PlayerStream::start()
{
m_isStarted = true;
m_requiredFrameSize = getRequiredFrameSize();
return ONI_STATUS_OK;
}
void PlayerStream::stop()
{
m_isStarted = false;
}
PlayerSource* PlayerStream::GetSource()
{
return m_pSource;
}
OniStatus PlayerStream::getProperty(int propertyId, void* pData, int* pDataSize)
{
// Check if the property exists.
m_cs.Lock();
OniStatus rc = m_properties.GetProperty(propertyId, pData, pDataSize);
if (rc != ONI_STATUS_OK)
{
rc = m_pSource->GetProperty(propertyId, pData, pDataSize);
}
m_cs.Unlock();
return rc;
}
OniStatus PlayerStream::setProperty(int /*propertyId*/, const void* /*pData*/, int /*dataSize*/)
{
return ONI_STATUS_ERROR;
}
OniStatus PlayerStream::RegisterReadyForDataEvent(ReadyForDataCallback callback, void* pCookie, OniCallbackHandle& handle)
{
XnStatus rc = m_readyForDataEvent.Register(callback, pCookie, (XnCallbackHandle&)handle);
if (rc != XN_STATUS_OK)
{
return ONI_STATUS_ERROR;
}
return ONI_STATUS_OK;
}
void PlayerStream::UnregisterReadyForDataEvent(OniCallbackHandle handle)
{
m_readyForDataEvent.Unregister((XnCallbackHandle)handle);
}
OniStatus PlayerStream::RegisterDestroyEvent(DestroyCallback callback, void* pCookie, OniCallbackHandle& handle)
{
XnStatus rc = m_destroyEvent.Register(callback, pCookie, (XnCallbackHandle&)handle);
if (rc != XN_STATUS_OK)
{
return ONI_STATUS_ERROR;
}
return ONI_STATUS_OK;
}
void PlayerStream::UnregisterDestroyEvent(OniCallbackHandle handle)
{
m_destroyEvent.Unregister((XnCallbackHandle)handle);
}
void ONI_CALLBACK_TYPE PlayerStream::OnNewDataCallback(const PlayerSource::NewDataEventArgs& newDataEventArgs, void* pCookie)
{
PlayerStream* pStream = (PlayerStream*)pCookie;
// Don't process new frames until the stream is started.
if(!pStream->m_isStarted)
{
return;
}
// Get the video mode.
OniVideoMode videoMode;
int valueSize = sizeof(videoMode);
OniStatus rc = pStream->m_pSource->GetProperty(ONI_STREAM_PROPERTY_VIDEO_MODE, &videoMode, &valueSize);
if (rc != ONI_STATUS_OK)
{
XN_ASSERT(FALSE);
return;
}
// Get stride.
int stride;
valueSize = sizeof(stride);
rc = pStream->m_pSource->GetProperty(ONI_STREAM_PROPERTY_STRIDE, &stride, &valueSize);
if (rc != ONI_STATUS_OK)
{
XN_ASSERT(FALSE);
return;
}
// Set the cropping property.
OniCropping cropping;
cropping.enabled = FALSE;
int dataSize = sizeof(cropping);
rc = pStream->m_pSource->GetProperty(ONI_STREAM_PROPERTY_CROPPING, &cropping, &dataSize);
if (rc != ONI_STATUS_OK)
{
XN_ASSERT(FALSE);
return;
}
pStream->m_cs.Lock();
// Allocate new frame and fill it.
OniFrame* pFrame = pStream->getServices().acquireFrame();
if (pFrame == NULL)
{
return;
}
// Fill the frame.
pFrame->frameIndex = newDataEventArgs.nFrameId;
pFrame->videoMode.pixelFormat = videoMode.pixelFormat;
pFrame->videoMode.resolutionX = videoMode.resolutionX;
pFrame->videoMode.resolutionY = videoMode.resolutionY;
pFrame->videoMode.fps = videoMode.fps;
if (!cropping.enabled)
{
// Set the full resolution, stride and origin.
pFrame->width = videoMode.resolutionX;
pFrame->height = videoMode.resolutionY;
pFrame->stride = stride;
pFrame->cropOriginX = 0;
pFrame->cropOriginY = 0;
pFrame->croppingEnabled = FALSE;
}
else
{
// Take resolution, and origin from cropping and calculate new stride.
pFrame->width = cropping.width;
pFrame->height = cropping.height;
pFrame->stride = (stride / pFrame->videoMode.resolutionX) * cropping.width;
pFrame->cropOriginX = cropping.originX;
pFrame->cropOriginY = cropping.originY;
pFrame->croppingEnabled = TRUE;
}
pFrame->sensorType = pStream->m_pSource->GetInfo()->sensorType;
pFrame->timestamp = newDataEventArgs.nTimeStamp;
pFrame->dataSize = newDataEventArgs.nSize;
if (pFrame->dataSize > pStream->m_requiredFrameSize)
{
xnLogWarning("Player", "File contains a frame with size %d whereas required frame size is %d", pFrame->dataSize, pStream->m_requiredFrameSize);
XN_ASSERT(FALSE);
pFrame->dataSize = pStream->m_requiredFrameSize;
}
memcpy(pFrame->data, newDataEventArgs.pData, pFrame->dataSize);
pStream->m_cs.Unlock();
// Process the new frame.
pStream->raiseNewFrame(pFrame);
pStream->getServices().releaseFrame(pFrame);
}
int PlayerStream::getRequiredFrameSize()
{
int requiredFrameSize = m_pSource->GetRequiredFrameSize();
if (requiredFrameSize == 0)
{
// not set for some reason, use default one
requiredFrameSize = StreamBase::getRequiredFrameSize();
}
return requiredFrameSize;
}
} // namespace oni_files_player
|