File: XnStreamData.cpp

package info (click to toggle)
openni-sensor-pointclouds 5.1.0.41.11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,656 kB
  • sloc: cpp: 34,881; ansic: 14,901; sh: 249; python: 155; makefile: 93; xml: 8
file content (188 lines) | stat: -rw-r--r-- 6,611 bytes parent folder | download | duplicates (12)
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
/****************************************************************************
*                                                                           *
*  PrimeSense Sensor 5.x Alpha                                              *
*  Copyright (C) 2011 PrimeSense Ltd.                                       *
*                                                                           *
*  This file is part of PrimeSense Sensor.                                  *
*                                                                           *
*  PrimeSense Sensor 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.                                      *
*                                                                           *
*  PrimeSense Sensor 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 PrimeSense Sensor. If not, see <http://www.gnu.org/licenses/>.*
*                                                                           *
****************************************************************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include "XnStreamDataInternal.h"
#include <XnOS.h>
#include <XnDDKStatus.h>

//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------
XN_DDK_API XnStatus XnStreamDataCreateNoBuffer(XnStreamData** ppStreamOutput, const XnChar* StreamName)
{
	XN_VALIDATE_OUTPUT_PTR(ppStreamOutput);

	// allocate struct
	XN_VALIDATE_CALLOC(*ppStreamOutput, XnStreamData, 1);
	XnStreamData* pStreamOutput = *ppStreamOutput;

	// allocate internal data
	pStreamOutput->pInternal = (XnStreamDataInternal*)xnOSCalloc(1, sizeof(XnStreamDataInternal));
	if (pStreamOutput->pInternal == NULL)
	{
		XnStreamDataDestroy(ppStreamOutput);
		return (XN_STATUS_ALLOC_FAILED);
	}

	// fill internal data
	pStreamOutput->pInternal->bAllocated = FALSE;
	pStreamOutput->pInternal->nAllocSize = 0;
	pStreamOutput->pInternal->UpdateMode = XN_STREAM_DATA_UPDATE_AUTOMATICALLY;
	pStreamOutput->pInternal->Callback = NULL;
	pStreamOutput->pInternal->pLockedBuffer = NULL;

	// take name
	xnOSStrCopy(pStreamOutput->StreamName, StreamName, XN_DEVICE_MAX_STRING_LENGTH);

	return (XN_STATUS_OK);
}

XN_DDK_API XnStatus XnStreamDataCreate(XnStreamData** ppStreamOutput, const XnChar* StreamName, XnUInt32 nAllocSize)
{
	XnStatus nRetVal = XN_STATUS_OK;

	nRetVal = XnStreamDataCreateNoBuffer(ppStreamOutput, StreamName);
	XN_IS_STATUS_OK(nRetVal);

	XnStreamData* pStreamOutput = *ppStreamOutput;

	// allocate buffer
	nRetVal = XnStreamDataUpdateSize(pStreamOutput, nAllocSize);
	if (nRetVal != XN_STATUS_OK)
	{
		XnStreamDataDestroy(ppStreamOutput);
		return nRetVal;
	}
	
	return (XN_STATUS_OK);
}

XN_DDK_API XnStatus XnStreamDataUpdateSize(XnStreamData* pStreamOutput, XnUInt32 nAllocSize)
{
	XN_VALIDATE_INPUT_PTR(pStreamOutput);

	// allocate new memory
	void* pNew = xnOSMallocAligned(nAllocSize, XN_DEFAULT_MEM_ALIGN);
	if (pNew == NULL)
		return (XN_STATUS_ALLOC_FAILED);

	// zero it
	xnOSMemSet(pNew, 0, nAllocSize);

	// free the buffer if it is allocated
	XN_ALIGNED_FREE_AND_NULL(pStreamOutput->pData);

	// and now set new buffer
	pStreamOutput->pData = pNew;

	// and size
	pStreamOutput->pInternal->nAllocSize = nAllocSize;

	pStreamOutput->pInternal->bAllocated = TRUE;

	return XN_STATUS_OK;
}

XN_DDK_API XnStatus XnStreamDataDestroy(XnStreamData** ppStreamOutput)
{
	XN_VALIDATE_INPUT_PTR(ppStreamOutput);
	XnStreamData* pStreamOutput = *ppStreamOutput;

	if (pStreamOutput != NULL)
	{
		// only free buffer if allocated
		if (pStreamOutput->pInternal->nAllocSize != 0)
		{
			xnOSFreeAligned(pStreamOutput->pData);
		}

		pStreamOutput->pData = NULL;

		XN_FREE_AND_NULL(pStreamOutput->pInternal);

		XN_FREE_AND_NULL(*ppStreamOutput);
	}

	return (XN_STATUS_OK);
}

XN_DDK_API XnStatus XnStreamDataSetUpdateMode(XnStreamData* pStreamOutput, XnStreamDataUpdateMode UpdateMode, XnStreamOutputNotificationCallback Callback, void* pCallbackData)
{
	XN_VALIDATE_INPUT_PTR(pStreamOutput);

	if (UpdateMode == XN_STREAM_DATA_UPDATE_NOTIFY && Callback == NULL)
		return XN_STATUS_DEVICE_BAD_PARAM;

	pStreamOutput->pInternal->UpdateMode = UpdateMode;
	pStreamOutput->pInternal->Callback = Callback;
	pStreamOutput->pInternal->pCallbackData = pCallbackData;

	return XN_STATUS_OK;
}

void XnStreamOutputCallCallbackFunction(XnStreamData* pStreamOutput, XnUInt32 nNeededSize)
{
	if (pStreamOutput->pInternal->Callback != NULL)
	{
		pStreamOutput->pInternal->Callback(pStreamOutput, pStreamOutput->pInternal->pCallbackData, nNeededSize);
	}
}

XN_DDK_API XnStatus XnStreamDataCheckSize(XnStreamData* pStreamOutput, XnUInt32 nNeededSize)
{
	XnStatus nRetVal = XN_STATUS_OK;
	XN_VALIDATE_INPUT_PTR(pStreamOutput);

	// if not allocated, then nothing to check
	if (!pStreamOutput->pInternal->bAllocated)
		return (XN_STATUS_OK);

	if (nNeededSize <= pStreamOutput->pInternal->nAllocSize)
		return (XN_STATUS_OK);

	// oh no! we don't have enough space

	if (pStreamOutput->pInternal->UpdateMode == XN_STREAM_DATA_UPDATE_AUTOMATICALLY)
	{
		// reallocate
		nRetVal = XnStreamDataUpdateSize(pStreamOutput, nNeededSize);
		XN_IS_STATUS_OK(nRetVal);

		// and call callback
		XnStreamOutputCallCallbackFunction(pStreamOutput, nNeededSize);
		return XN_STATUS_OK;
	}
	else if (pStreamOutput->pInternal->UpdateMode == XN_STREAM_DATA_UPDATE_NOTIFY)
	{
		// let client decide
		XnStreamOutputCallCallbackFunction(pStreamOutput, nNeededSize);

		// check if it was fixed
		if (nNeededSize <= pStreamOutput->pInternal->nAllocSize)
			return XN_STATUS_OK;
	}

	// if we got here, problem still exists
	return XN_STATUS_STREAM_OUTPUT_BUFFER_TOO_SMALL;
}