File: PlayerNode.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 (1806 lines) | stat: -rw-r--r-- 53,052 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
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
/*****************************************************************************
*                                                                            *
*  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 "PlayerNode.h"
#include "DataRecords.h"
#include "XnPropNames.h"
#include "Formats/XnCodecIDs.h"
#include "Formats/XnCodec.h"
#include <XnLog.h>
#include <math.h>

namespace oni_file {

#define XN_MASK_OPEN_NI ""

#ifdef PLAYER_NODE_LOG_RECORDS
	template <typename T>
	inline static void DEBUG_LOG_RECORD(T record, const XnChar* strRecordName)
	{
		XnChar s[1024];
		XnUInt32 nCharsWritten = 0;
		XnStatus nRetVal = record.AsString(s, sizeof(s), nCharsWritten);
		XN_ASSERT(nRetVal == XN_STATUS_OK);
		xnLogVerbose(XN_MASK_OPEN_NI, "--PLAYER--> %s: %s", strRecordName, s);
	}
#else
	#ifdef __WIN32
		#define DEBUG_LOG_RECORD(record, name) __noop
	#else
		#define DEBUG_LOG_RECORD(record, name)
	#endif
#endif

//---------------------------------------------------------------------------
// Backward Compatibility Issues
//---------------------------------------------------------------------------
#define XN_PROP_REAL_WORLD_TRANSLATION_DATA "xnRealWorldTranslationData" //general

typedef struct XnRealWorldTranslationData
{
	XnDouble dZeroPlaneDistance;
	XnDouble dPixelSizeAtZeroPlane;
	XnDouble dSourceToDepthPixelRatio;
} XnRealWorldTranslationData;

//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------

//DATA_MAX_SIZE is set to support a resolution of 1600x1200 with 24 bits per pixel
const XnUInt64 PlayerNode::DATA_MAX_SIZE = 1600 * 1200 * 3; 
const XnUInt64 PlayerNode::RECORD_MAX_SIZE = 
	NewDataRecordHeader::MAX_SIZE +
	PlayerNode::DATA_MAX_SIZE; //Maximum data size

const XnVersion PlayerNode::OLDEST_SUPPORTED_FILE_FORMAT_VERSION = {1, 0, 0, 4};
const XnVersion PlayerNode::FIRST_FILESIZE64BIT_FILE_FORMAT_VERSION = {1, 0, 1, 0};

PlayerNode::PlayerNode(const XnChar* strName) :
	m_bOpen(FALSE),
	m_bIs32bitFileFormat(FALSE),
	m_pRecordBuffer(NULL),
	m_pUncompressedData(NULL),
	m_pStreamCookie(NULL),
	m_pInputStream(NULL),
	m_pNotificationsCookie(NULL),
	m_pNodeNotifications(NULL),
	m_bRepeat(TRUE),
	m_bDataBegun(FALSE),
	m_bEOF(FALSE),
	m_nTimeStamp(0),
	m_nGlobalMaxTimeStamp(0),
	m_pNodeInfoMap(NULL),
	m_nMaxNodes(0),
	m_aSeekTempArray(NULL)
{
	xnOSMemSet(&m_fileVersion, 0, sizeof(m_fileVersion));
	xnOSStrCopy(m_strName, strName, sizeof(m_strName));
	xnOSMemSet(&m_lastOutputMode, 0, sizeof(XnMapOutputMode));
}

PlayerNode::~PlayerNode()
{
	Destroy();
}

XnStatus PlayerNode::Init()
{
	m_pRecordBuffer = XN_NEW_ARR(XnUInt8, RECORD_MAX_SIZE);
	XN_VALIDATE_ALLOC_PTR(m_pRecordBuffer);
	m_pUncompressedData = XN_NEW_ARR(XnUInt8, DATA_MAX_SIZE);
	XN_VALIDATE_ALLOC_PTR(m_pUncompressedData);
	return XN_STATUS_OK;
}

XnStatus PlayerNode::Destroy()
{
	CloseStream();
	//Don't verify return value - proceed anyway

	if (m_pNodeInfoMap != NULL)
	{
		for (XnUInt32 i = 0; i < m_nMaxNodes; i++)
		{
			RemovePlayerNodeInfo(i);
		}

		XN_DELETE_ARR(m_pNodeInfoMap);
		m_pNodeInfoMap = NULL;
	}

	if (m_aSeekTempArray != NULL)
	{
		xnOSFree(m_aSeekTempArray);
		m_aSeekTempArray = NULL;
	}

	XN_DELETE_ARR(m_pRecordBuffer);
	m_pRecordBuffer = NULL;
	XN_DELETE_ARR(m_pUncompressedData);
	m_pUncompressedData = NULL;

	return XN_STATUS_OK;
}

XnStatus PlayerNode::SetInputStream(void *pStreamCookie, XnPlayerInputStreamInterface *pStream)
{
	XN_VALIDATE_INPUT_PTR(pStream);
	m_pStreamCookie = pStreamCookie;
	m_pInputStream = pStream;
	XnStatus nRetVal = OpenStream();
	XN_IS_STATUS_OK(nRetVal);
	return XN_STATUS_OK;
}

XnStatus PlayerNode::SetNodeNotifications(void *pNotificationsCookie, XnNodeNotifications *pNodeNotifications)
{
	XN_VALIDATE_INPUT_PTR(pNodeNotifications);
	m_pNotificationsCookie = pNotificationsCookie;
	m_pNodeNotifications = pNodeNotifications;
	return XN_STATUS_OK;
}

XnStatus PlayerNode::SetNodeCodecFactory(void* pPlayerNodeCodecFactoryCookie, 
										 PlayerNode::CodecFactory* pPlayerNodeCodecFactory)
{
	XN_VALIDATE_INPUT_PTR(pPlayerNodeCodecFactory);
	m_pNodeCodecFactoryCookie = pPlayerNodeCodecFactoryCookie;
	m_pNodeCodecFactory = pPlayerNodeCodecFactory;
	return XN_STATUS_OK;
}

XnStatus PlayerNode::SetRepeat(XnBool bRepeat)
{
	m_bRepeat = bRepeat;
	return XN_STATUS_OK;
}

XnStatus PlayerNode::SeekToTimeStamp(XnInt64 /*nTimeOffset*/, XnPlayerSeekOrigin /*origin*/)
{
	/*
	switch (origin)
	{
		case XN_PLAYER_SEEK_SET:
			return SeekToTimeStampAbsolute((XnUInt64)nTimeOffset);
		case XN_PLAYER_SEEK_CUR:
			return SeekToTimeStampRelative(nTimeOffset);
		case XN_PLAYER_SEEK_END:
			return SeekToTimeStampAbsolute(m_nGlobalMaxTimeStamp);
		default:
			XN_ASSERT(FALSE);
			XN_LOG_ERROR_RETURN(XN_STATUS_BAD_PARAM, XN_MASK_OPEN_NI, "Invalid seek origin: %u", origin);
	}*/
	return XN_STATUS_NOT_IMPLEMENTED;
}

XnStatus PlayerNode::SeekToFrame(const XnChar* strNodeName, XnInt32 nFrameOffset, XnPlayerSeekOrigin origin)
{
	XnStatus nRetVal = XN_STATUS_OK;
	XnUInt32 nNodeID = GetPlayerNodeIDByName(strNodeName);
	if (nNodeID == INVALID_NODE_ID)
	{
		XN_LOG_ERROR_RETURN(XN_STATUS_BAD_NODE_NAME, XN_MASK_OPEN_NI, "Bad node name '%s'", strNodeName);
	}

	PlayerNodeInfo* pPlayerNodeInfo = &m_pNodeInfoMap[nNodeID];

	XnInt64 nOriginFrame = 0;
	switch (origin)
	{
		case XN_PLAYER_SEEK_SET:
		{
			nOriginFrame = 0;
			break;
		}
		case XN_PLAYER_SEEK_CUR:
		{
			nOriginFrame = pPlayerNodeInfo->nCurFrame;
			break;
		}
		case XN_PLAYER_SEEK_END:
		{
			nOriginFrame = pPlayerNodeInfo->nFrames;
			break;
		}
		default:
		{
			XN_ASSERT(FALSE);
			XN_LOG_ERROR_RETURN(XN_STATUS_BAD_PARAM, XN_MASK_OPEN_NI, "Invalid seek origin: %u", origin);
		}
	}
	XnUInt32 nDestFrame = (XnUInt32)XN_MIN(XN_MAX(1, nOriginFrame + nFrameOffset), pPlayerNodeInfo->nFrames);
	nRetVal = SeekToFrameAbsolute(nNodeID, nDestFrame);
	XN_IS_STATUS_OK(nRetVal);

	return XN_STATUS_OK;
}

XnStatus PlayerNode::UndoRecord(PlayerNode::RecordUndoInfo& undoInfo, XnUInt64 nDestPos, XnBool& bUndone)
{
	XnStatus nRetVal = XN_STATUS_OK;
	XnUInt64 nOriginalPos = TellStream();
	bUndone = FALSE;
	Record record(m_pRecordBuffer, RECORD_MAX_SIZE, m_bIs32bitFileFormat);
	while ((undoInfo.nRecordPos > nDestPos) && (undoInfo.nUndoRecordPos != 0))
	{
		nRetVal = SeekStream(XN_OS_SEEK_SET, undoInfo.nUndoRecordPos);
		XN_IS_STATUS_OK(nRetVal);
		nRetVal = ReadRecordHeader(record);
		XN_IS_STATUS_OK(nRetVal);
		undoInfo.nRecordPos = undoInfo.nUndoRecordPos;
		undoInfo.nUndoRecordPos = record.GetUndoRecordPos();
	}

	if (undoInfo.nRecordPos <= nDestPos)
	{
		/*We found a record that can undo the record originally pointed to by undoInfo.nDestRecordPos, 
		  so now we handle it. */
		nRetVal = ReadRecordFields(record);
		XN_IS_STATUS_OK(nRetVal);
		nRetVal = HandleRecord(record, FALSE);
		XN_IS_STATUS_OK(nRetVal);
		bUndone = TRUE;
	}
	else
	{
		nRetVal = SeekStream(XN_OS_SEEK_SET, nOriginalPos);
		XN_IS_STATUS_OK(nRetVal);
	}
	return XN_STATUS_OK;
}

DataIndexEntry* PlayerNode::FindTimestampInDataIndex(XnUInt32 nNodeID, XnUInt64 nTimestamp)
{
	XN_ASSERT((nNodeID != INVALID_NODE_ID) && (nNodeID < m_nMaxNodes));
	PlayerNodeInfo* pPlayerNodeInfo = &m_pNodeInfoMap[nNodeID];
	
	// perform binary search. We're looking for the highest timestamp BEFORE searched timestamp
	int first = 1;
	int last = pPlayerNodeInfo->nFrames;
	int mid;
	XnUInt64 nMidTimestamp;

	while (first <= last)
	{
		mid = (first + last) / 2;
		nMidTimestamp = pPlayerNodeInfo->pDataIndex[mid].nTimestamp;

		if (nMidTimestamp > nTimestamp)
		{
			last = mid - 1;
		}
		else if (nMidTimestamp < nTimestamp)
		{
			first = mid + 1;
		}
		else // equals
		{
			break;
		}
	}

	return &pPlayerNodeInfo->pDataIndex[first-1];
}

DataIndexEntry** PlayerNode::GetSeekLocationsFromDataIndex(XnUInt32 nNodeID, XnUInt32 nDestFrame)
{
	PlayerNodeInfo* pPlayerNodeInfo = &m_pNodeInfoMap[nNodeID];
	if (pPlayerNodeInfo->pDataIndex == NULL)
	{
		xnLogVerbose(XN_MASK_OPEN_NI, "Slow seek being used (recording doesn't have seek tables)");
		return NULL;
	}

	DataIndexEntry* pCurrentFrame = &pPlayerNodeInfo->pDataIndex[pPlayerNodeInfo->nCurFrame];
	DataIndexEntry* pDestFrame = &pPlayerNodeInfo->pDataIndex[nDestFrame];

	if (pCurrentFrame->nConfigurationID != pDestFrame->nConfigurationID)
	{
		// can't use fast seek. We'll have to do it the old fashion way
		xnLogVerbose(XN_MASK_OPEN_NI, "Seeking from %u to %u: Slow seek being used (configuration was changed between source and destination frames)", pPlayerNodeInfo->nCurFrame, nDestFrame);
		return NULL;
	}

	m_aSeekTempArray[nNodeID] = pDestFrame;

	// find corresponding frames of other nodes
	for (XnUInt32 i = 0; i < m_nMaxNodes; ++i)
	{
		if (m_pNodeInfoMap[i].bIsGenerator && i != nNodeID)
		{
			m_aSeekTempArray[i] = FindTimestampInDataIndex(i, pDestFrame->nTimestamp);
			if (m_aSeekTempArray[i] != NULL && m_aSeekTempArray[i]->nConfigurationID != pCurrentFrame->nConfigurationID)
			{
				xnLogVerbose(XN_MASK_OPEN_NI, "Seeking from %u to %u: Slow seek being used (configuration was changed between source and destination frames or other nodes)", pPlayerNodeInfo->nCurFrame, nDestFrame);
				return NULL;
			}
		}
	}

	// if we got here, all nodes are in the same configuration ID, and we can perform fast seek
	return m_aSeekTempArray;
}

XnStatus PlayerNode::SeekToFrameAbsolute(XnUInt32 nNodeID, XnUInt32 nDestFrame)
{
	XN_ASSERT((nNodeID != INVALID_NODE_ID) && (nNodeID < m_nMaxNodes));
	PlayerNodeInfo* pPlayerNodeInfo = &m_pNodeInfoMap[nNodeID];
	XN_ASSERT((nDestFrame > 0) && (nDestFrame <= pPlayerNodeInfo->nFrames));
	XN_VALIDATE_INPUT_PTR(m_pNodeNotifications);

	XnStatus nRetVal = XN_STATUS_OK;

	if (nDestFrame == pPlayerNodeInfo->nCurFrame)
	{
		//Just go back to position of current frame
		nRetVal = SeekStream(XN_OS_SEEK_SET, pPlayerNodeInfo->nLastDataPos);
		XN_IS_STATUS_OK(nRetVal);
		// and re-read it
		nRetVal = ReadNext();
		XN_IS_STATUS_OK(nRetVal);

		return XN_STATUS_OK;
	}

	// not same frame. Find seek locations of each stream
	DataIndexEntry** pDataIndex = GetSeekLocationsFromDataIndex(nNodeID, nDestFrame);
	if (pDataIndex != NULL)
	{
		XnUInt64 nLastPos = 0;

		// move each node to its relevant data
		for (XnUInt32 i = 0; i < m_nMaxNodes; i++)
		{
			if (m_aSeekTempArray[i] != NULL)
			{
				// read data
				nRetVal = SeekStream(XN_OS_SEEK_SET, m_aSeekTempArray[i]->nSeekPos);
				XN_IS_STATUS_OK(nRetVal);
				nRetVal = ReadNext();
				XN_IS_STATUS_OK(nRetVal);

				// check for latest position. This will be directly after the frame we seeked to.
				XnUInt64 nPos = TellStream();
				if (nPos > nLastPos)
				{
					nLastPos = nPos;
				}
			}
		}

		// now seek to directly after last position
		SeekStream(XN_OS_SEEK_SET, nLastPos);
	}
	else
	{
		// perform old seek (no data indexes)
		XnUInt64 nStartPos = TellStream();
		XnUInt32 nNextFrame = pPlayerNodeInfo->nCurFrame + 1;
		XnStatus nRetVal = XN_STATUS_OK;

		if (nDestFrame < nNextFrame)
		{
			//Seek backwards
			XnUInt64 nDestRecordPos = pPlayerNodeInfo->newDataUndoInfo.nRecordPos;
			XnUInt64 nUndoRecordPos = pPlayerNodeInfo->newDataUndoInfo.nUndoRecordPos;
			NewDataRecordHeader record(m_pRecordBuffer, RECORD_MAX_SIZE, m_bIs32bitFileFormat);
			
			/*Scan back through the frames' undo positions until we get to a frame number that is smaller or equal
			  to nDestFrame. We put the position of the frame we find in nDestRecordPos. */
			do
			{
				if (nUndoRecordPos == 0)
				{
					/* The last frame we encountered doesn't have an undo frame. But this data frame can't be the first,
					   so the file is corrupt */
					XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Undo frame not found for frame in position %u", nDestRecordPos);
				}
				nRetVal = SeekStream(XN_OS_SEEK_SET, nUndoRecordPos);
				XN_IS_STATUS_OK(nRetVal);
				nDestRecordPos = nUndoRecordPos;
				record.ResetRead();
				nRetVal = ReadRecordHeader(record);
				XN_IS_STATUS_OK(nRetVal);
				if (record.GetType() != RECORD_NEW_DATA)
				{
					XN_ASSERT(FALSE);
					XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Unexpected record type: %u", record.GetType());
				}

				if (record.GetNodeID() != nNodeID)
				{
					XN_ASSERT(FALSE);
					XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Unexpected node id: %u", record.GetNodeID());
				}

				nRetVal = ReadRecordFields(record);
				XN_IS_STATUS_OK(nRetVal);
				nRetVal = record.Decode();
				XN_IS_STATUS_OK(nRetVal);
				nUndoRecordPos = record.GetUndoRecordPos();
			} while (record.GetFrameNumber() > nDestFrame);

			//Now handle the frame
			nRetVal = HandleNewDataRecord(record, FALSE);
			XnBool bUndone = FALSE;

			for (XnUInt32 i = 0; i < m_nMaxNodes; ++i)
			{
				//Rollback all properties to match the state the stream was in at position nDestRecordPos
				PlayerNodeInfo &pni = m_pNodeInfoMap[i];
				for (RecordUndoInfoMap::Iterator it = pni.recordUndoInfoMap.Begin(); 
					 it != pni.recordUndoInfoMap.End(); ++it)
				{
					if ((it->Value().nRecordPos > nDestRecordPos) && (it->Value().nRecordPos < nStartPos))
					{
						//This property was set between nDestRecordPos and our start position, so we need to undo it.
						nRetVal = UndoRecord(it->Value(), nDestRecordPos, bUndone);
						XN_IS_STATUS_OK(nRetVal);
					}
				}

				if ((i != nNodeID) && pni.bIsGenerator)
				{
					//Undo all other generator nodes' data
					RecordUndoInfo &undoInfo = pni.newDataUndoInfo;
					if ((undoInfo.nRecordPos > nDestRecordPos) && (undoInfo.nRecordPos < nStartPos))
					{
						nRetVal = UndoRecord(undoInfo, nDestRecordPos, bUndone);
						XN_IS_STATUS_OK(nRetVal);
						if (!bUndone)
						{
							//We couldn't find a record that can undo this data record
							pni.nLastDataPos = 0;
							pni.newDataUndoInfo.Reset();
						}
					}
				}
			}

			/*Now, for each node, go to the position of the last encountered data record, and process that record
			  (including its payload).*/
			/*TODO: Optimization: remember each node's last data pos, and later, see if it changes. Only process data
			  frames of nodes whose last data pos actually changed.*/

			nRetVal = ProcessEachNodeLastData(nNodeID);
			XN_IS_STATUS_OK(nRetVal);
		}
		else //(nDestFrame >= nNextFrame)
		{
			//Skip all frames until we get to our frame number, but handle any properties we run into.
			while (pPlayerNodeInfo->nCurFrame < nDestFrame)
			{
				nRetVal = ProcessRecord(FALSE);
				XN_IS_STATUS_OK(nRetVal);
			}

			/*Now, for each node, go to the position of the last encountered data record, and process that record
			  (including its payload).*/
			/*TODO: Optimization: remember each node's last data pos, and later, see if it changes. Only process data
			  frames of nodes whose last data pos actually changed.*/
			nRetVal = ProcessEachNodeLastData(nNodeID);
			XN_IS_STATUS_OK(nRetVal);
		}
	}

	return XN_STATUS_OK;
}

XnStatus PlayerNode::ProcessEachNodeLastData(XnUInt32 nIDToProcessLast)
{
	XnStatus nRetVal = XN_STATUS_OK;
	XnUInt32 nItNodeID = 0; //Node ID handled in each iteration.
	
	for (XnUInt32 i = 0; i < m_nMaxNodes; i++)
	{
		/*We switch positions between nIDToProcessLast and the last position, to make sure that nIDToProcessLast is 
		  handled last. This way the position at the end of our seek operation is right after the record we read
		  for nIDToProcessLast.*/
		if (i == nIDToProcessLast)
		{
			nItNodeID = m_nMaxNodes - 1;
		}
		else if (i == m_nMaxNodes - 1)
		{
			nItNodeID = nIDToProcessLast;
		}
		else
		{
			nItNodeID = i;
		}
		PlayerNodeInfo &pni = m_pNodeInfoMap[nItNodeID];
		if (pni.bIsGenerator)
		{
			if (!pni.bValid)
			{
				xnLogError(XN_MASK_OPEN_NI, "Node with ID %u is not valid", nItNodeID);
				XN_ASSERT(FALSE);
				return XN_STATUS_CORRUPT_FILE;
			}

			if (pni.nLastDataPos == 0)
			{
				/*This means we had to undo this node's data, but found no data frame before our main node's
			      data frame. In this case we push a 0 frame.*/
				memset(m_pRecordBuffer, 0, RECORD_MAX_SIZE);
				nRetVal = m_pNodeNotifications->OnNodeNewData(m_pNotificationsCookie, pni.strName, 0, 0, m_pRecordBuffer, RECORD_MAX_SIZE);
				XN_IS_STATUS_OK(nRetVal);
			}
			else
			{
				nRetVal = SeekStream(XN_OS_SEEK_SET, pni.nLastDataPos);
				XN_IS_STATUS_OK(nRetVal);
				nRetVal = ProcessRecord(TRUE);
				XN_IS_STATUS_OK(nRetVal);
			}
		}
	}
	//Now our position is right after the last data of the node with id nIDToProcessLast

	return XN_STATUS_OK;
}

XnStatus PlayerNode::TellTimestamp(XnUInt64& nTimestamp)
{
	nTimestamp = m_nTimeStamp;
	return (XN_STATUS_OK);
}

XnStatus PlayerNode::TellFrame(const XnChar* strNodeName, XnUInt32& nFrameNumber)
{
	PlayerNodeInfo* pPlayerNodeInfo = GetPlayerNodeInfoByName(strNodeName);
	XN_VALIDATE_PTR(pPlayerNodeInfo, XN_STATUS_BAD_NODE_NAME);
	if (!pPlayerNodeInfo->bValid)
	{
		XN_ASSERT(FALSE);
		return XN_STATUS_BAD_NODE_NAME;
	}

	nFrameNumber = pPlayerNodeInfo->nCurFrame;
	return XN_STATUS_OK;
}

XnUInt32 PlayerNode::GetNumFrames(const XnChar* strNodeName, XnUInt32& nFrames)
{
	PlayerNodeInfo* pPlayerNodeInfo = GetPlayerNodeInfoByName(strNodeName);
	XN_VALIDATE_PTR(pPlayerNodeInfo, XN_STATUS_BAD_NODE_NAME);
	if (!pPlayerNodeInfo->bValid)
	{
		XN_ASSERT(FALSE);
		return XN_STATUS_BAD_NODE_NAME;
	}

	nFrames = pPlayerNodeInfo->nFrames;
	return XN_STATUS_OK;
}

const XnChar* PlayerNode::GetSupportedFormat()
{
	return XN_FORMAT_NAME_ONI;
}

XnBool PlayerNode::IsEOF()
{
	return m_bEOF;
}

XnStatus PlayerNode::RegisterToEndOfFileReached(EndOfFileReachedHandler handler, void* pCookie, XnCallbackHandle& hCallback)
{
	return m_eofReachedEvent.Register(handler, pCookie, hCallback);
}

void PlayerNode::UnregisterFromEndOfFileReached(XnCallbackHandle hCallback)
{
	m_eofReachedEvent.Unregister(hCallback);
}

XnStatus PlayerNode::ValidateStream(void *pStreamCookie, XnPlayerInputStreamInterface* pInputStream)
{
	XN_VALIDATE_INPUT_PTR(pInputStream);
	XnStatus nRetVal = pInputStream->Open(pStreamCookie);
	XN_IS_STATUS_OK(nRetVal);
	RecordingHeader header;
	XnUInt32 nBytesRead = 0;

	nRetVal = pInputStream->Read(pStreamCookie, &header, sizeof(header), &nBytesRead);
	XN_IS_STATUS_OK(nRetVal);
	if (nBytesRead < sizeof(header))
	{
		pInputStream->Close(pStreamCookie);
		XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Not enough bytes read");
	}

	/* Check header */
	if (xnOSMemCmp(header.headerMagic, DEFAULT_RECORDING_HEADER.headerMagic, sizeof(header.headerMagic)) != 0)
	{
		pInputStream->Close(pStreamCookie);
		XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Invalid header magic");
	}

	if ((CompareVersions(&header.version, &OLDEST_SUPPORTED_FILE_FORMAT_VERSION) < 0) || //File format is too old
		(CompareVersions(&header.version, &DEFAULT_RECORDING_HEADER.version) > 0)) //File format is too new
	{
		pInputStream->Close(pStreamCookie);
		XN_LOG_ERROR_RETURN(XN_STATUS_UNSUPPORTED_VERSION, XN_MASK_OPEN_NI, "Unsupported file format version: %u.%u.%u.%u", header.version.nMajor, header.version.nMinor, header.version.nMaintenance, header.version.nBuild);
	}

	// Close the stream.
	pInputStream->Close(pStreamCookie);

	return XN_STATUS_OK;
}

XnStatus PlayerNode::ReadNext()
{
	return ProcessRecord(TRUE);
}

XnStatus PlayerNode::ProcessRecord(XnBool bProcessPayload)
{
	//Read a record and handle it
	Record record(m_pRecordBuffer, RECORD_MAX_SIZE, m_bIs32bitFileFormat);
	XnStatus nRetVal = ReadRecord(record);
	XN_IS_STATUS_OK(nRetVal);
	nRetVal = HandleRecord(record, bProcessPayload);
	XN_IS_STATUS_OK(nRetVal);
	return XN_STATUS_OK;
}

XnInt32 PlayerNode::CompareVersions(const XnVersion* pV0, const XnVersion* pV1)
{
	XnInt32 comparison = 0;
	comparison = pV0->nMajor - pV1->nMajor;
	if (0 != comparison)
	{
		return comparison;
	}
	comparison = pV0->nMinor - pV1->nMinor;
	if (0 != comparison)
	{
		return comparison;
	}
	comparison = pV0->nMaintenance - pV1->nMaintenance;
	if (0 != comparison)
	{
		return comparison;
	}
	return pV0->nBuild - pV1->nBuild;
}

XnStatus PlayerNode::OpenStream()
{
	XN_VALIDATE_INPUT_PTR(m_pInputStream);
	XnStatus nRetVal = m_pInputStream->Open(m_pStreamCookie);
	XN_IS_STATUS_OK(nRetVal);
	RecordingHeader header;
	XnUInt32 nBytesRead = 0;
	
	nRetVal = m_pInputStream->Read(m_pStreamCookie, &header, sizeof(header), &nBytesRead);
	XN_IS_STATUS_OK(nRetVal);
	if (nBytesRead < sizeof(header))
	{
		XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Not enough bytes read");
	}

	/* Check header */
	if (xnOSMemCmp(header.headerMagic, DEFAULT_RECORDING_HEADER.headerMagic, sizeof(header.headerMagic)) != 0)
	{
		XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Invalid header magic");
	}

	if ((CompareVersions(&header.version, &OLDEST_SUPPORTED_FILE_FORMAT_VERSION) < 0) || //File format is too old
		(CompareVersions(&header.version, &DEFAULT_RECORDING_HEADER.version) > 0)) //File format is too new
	{
		XN_LOG_ERROR_RETURN(XN_STATUS_UNSUPPORTED_VERSION, XN_MASK_OPEN_NI, "Unsupported file format version: %u.%u.%u.%u", header.version.nMajor, header.version.nMinor, header.version.nMaintenance, header.version.nBuild);
	}

	/* Do we need to parse an old 32bit-filesize file? */
	if (CompareVersions(&header.version, &FIRST_FILESIZE64BIT_FILE_FORMAT_VERSION) >= 0)
	{
		m_bIs32bitFileFormat = FALSE;
	} else {
		m_bIs32bitFileFormat = TRUE;
	}

	m_fileVersion = header.version;
	m_nGlobalMaxTimeStamp = header.nGlobalMaxTimeStamp;
	m_nMaxNodes = header.nMaxNodeID + 1;
	XN_ASSERT(m_nMaxNodes > 0);
	XN_DELETE_ARR(m_pNodeInfoMap);
	xnOSFree(m_aSeekTempArray);
	m_pNodeInfoMap = XN_NEW_ARR(PlayerNodeInfo, m_nMaxNodes);
	XN_VALIDATE_ALLOC_PTR(m_pNodeInfoMap);
	XN_VALIDATE_CALLOC(m_aSeekTempArray, DataIndexEntry*, m_nMaxNodes);
	
	m_bOpen = TRUE;
	nRetVal = ProcessUntilFirstData();
	if (nRetVal != XN_STATUS_OK)
	{
		XN_DELETE_ARR(m_pNodeInfoMap);
		m_pNodeInfoMap = NULL;
		xnOSFree(m_aSeekTempArray);
		m_aSeekTempArray = NULL;
		return nRetVal;
	}

	return XN_STATUS_OK;
}

XnStatus PlayerNode::Read(void *pData, XnUInt32 nSize, XnUInt32 &nBytesRead)
{
	XN_VALIDATE_INPUT_PTR(m_pInputStream);
	if (!m_bOpen)
	{
		XN_LOG_ERROR_RETURN(XN_STATUS_INVALID_OPERATION, XN_MASK_OPEN_NI, "Stream was not opened");
	}

	return m_pInputStream->Read(m_pStreamCookie, pData, nSize, &nBytesRead);
} 

XnStatus PlayerNode::ReadRecordHeader(Record &record)
{
	XnUInt32 nBytesRead = 0;
	XnStatus nRetVal = Read(record.GetData(), record.HEADER_SIZE, nBytesRead);
	XN_IS_STATUS_OK(nRetVal);

	if (nBytesRead != record.HEADER_SIZE)
	{
		XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Incorrect number of bytes read");
	}

	if (!record.IsHeaderValid())
	{
		XN_ASSERT(FALSE);
		XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Invalid record header");
	}
	return XN_STATUS_OK;
}

XnStatus PlayerNode::ReadRecordFields(Record &record)
{
	XnUInt32 nBytesToRead = record.GetSize() - record.HEADER_SIZE;
	XnUInt32 nBytesRead = 0;
	XnStatus nRetVal = Read(record.GetData() + record.HEADER_SIZE, nBytesToRead, nBytesRead);
	XN_IS_STATUS_OK(nRetVal);
	if (nBytesRead < nBytesToRead)
	{
		XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Incorrect number of bytes read");
	}
	return XN_STATUS_OK;
}

XnStatus PlayerNode::ReadRecord(Record &record)
{
	XnStatus nRetVal = ReadRecordHeader(record);
	XN_IS_STATUS_OK(nRetVal);
	nRetVal = ReadRecordFields(record);
	XN_IS_STATUS_OK(nRetVal);
	return XN_STATUS_OK;
}

XnStatus PlayerNode::SeekStream(XnOSSeekType seekType, XnInt64 nOffset)
{
	XN_VALIDATE_INPUT_PTR(m_pInputStream);
	return m_pInputStream->Seek64(m_pStreamCookie, seekType, nOffset);
}

XnUInt64 PlayerNode::TellStream()
{
	XN_VALIDATE_PTR(m_pInputStream, (XnUInt64)-1);
	return m_pInputStream->Tell64(m_pStreamCookie);
}

XnStatus PlayerNode::CloseStream()
{
	if (m_bOpen)
	{
		XN_VALIDATE_INPUT_PTR(m_pInputStream);
		m_pInputStream->Close(m_pStreamCookie);
		m_pInputStream = NULL;
		m_pStreamCookie = NULL;
		m_bOpen = FALSE;
	}
	return XN_STATUS_OK;
}

XnStatus PlayerNode::HandleRecord(Record &record, XnBool bHandlePayload)
{
	XN_ASSERT(record.IsHeaderValid());
	switch (record.GetType())
	{
		case RECORD_NODE_ADDED:
			return HandleNodeAddedRecord(record);
		case RECORD_INT_PROPERTY:
			return HandleIntPropRecord(record);
		case RECORD_REAL_PROPERTY:
			return HandleRealPropRecord(record);
		case RECORD_STRING_PROPERTY:
			return HandleStringPropRecord(record);
		case RECORD_GENERAL_PROPERTY:
			return HandleGeneralPropRecord(record);
		case RECORD_NODE_REMOVED:
			return HandleNodeRemovedRecord(record);
		case RECORD_NODE_STATE_READY:
			return HandleNodeStateReadyRecord(record);
		case RECORD_NODE_DATA_BEGIN:
			return HandleNodeDataBeginRecord(record);
		case RECORD_NEW_DATA:
			return HandleNewDataRecord(record, bHandlePayload);
		case RECORD_SEEK_TABLE:
			// never process this record (it is processed only during node added)
			return HandleDataIndexRecord(record, FALSE);
		case RECORD_END:
			return HandleEndRecord(record);

		// BC stuff
		case RECORD_NODE_ADDED_1_0_0_5:
			return HandleNodeAdded_1_0_0_5_Record(record);
		case RECORD_NODE_ADDED_1_0_0_4:
			return HandleNodeAdded_1_0_0_4_Record(record);

		default:
			XN_ASSERT(FALSE);
			XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Unrecognized record type: %u", record.GetType());
	}		
}

PlayerNode::PlayerNodeInfo* PlayerNode::GetPlayerNodeInfo(XnUInt32 nNodeID)
{
	if (nNodeID >= m_nMaxNodes)
	{
		xnLogWarning(XN_MASK_OPEN_NI, "Got node ID %u, bigger than said max of %u", nNodeID, m_nMaxNodes);
		XN_ASSERT(FALSE);
		return NULL;
	}

	return &m_pNodeInfoMap[nNodeID];
}

XnStatus PlayerNode::RemovePlayerNodeInfo(XnUInt32 nNodeID)
{
	XnStatus nRetVal = XN_STATUS_OK;

	PlayerNodeInfo* pPlayerNodeInfo = GetPlayerNodeInfo(nNodeID);
	XN_VALIDATE_PTR(pPlayerNodeInfo, XN_STATUS_CORRUPT_FILE);
	if (pPlayerNodeInfo->bValid)
	{
		if (m_pNodeNotifications != NULL)
		{
			nRetVal = m_pNodeNotifications->OnNodeRemoved(m_pNotificationsCookie, pPlayerNodeInfo->strName);
			if (nRetVal != XN_STATUS_OK)
			{
				return nRetVal;
			}
		}

		if (pPlayerNodeInfo->pCodec != NULL)
		{
			m_pNodeCodecFactory->Destroy(m_pNodeCodecFactoryCookie, 
											   pPlayerNodeInfo->pCodec);
			pPlayerNodeInfo->pCodec = NULL;
		}
		pPlayerNodeInfo->Reset(); //Now it's not valid anymore
	}

	return XN_STATUS_OK;
}

XnBool PlayerNode::IsTypeGenerator(XnProductionNodeType type)
{
	if ((type == XN_NODE_TYPE_DEPTH) || 
		(type == XN_NODE_TYPE_IMAGE) || 
		(type == XN_NODE_TYPE_IR))
	{
		return TRUE;
	}
	return FALSE;
}

XnStatus PlayerNode::HandleNodeAddedImpl(XnUInt32 nNodeID, XnProductionNodeType type, const XnChar* strName, XnCodecID compression, XnUInt32 nNumberOfFrames, XnUInt64 /*nMinTimestamp*/, XnUInt64 nMaxTimestamp)
{
	XN_VALIDATE_INPUT_PTR(m_pNodeNotifications);

	XnStatus nRetVal = XN_STATUS_OK;

	PlayerNodeInfo* pPlayerNodeInfo = GetPlayerNodeInfo(nNodeID);
	XN_VALIDATE_PTR(pPlayerNodeInfo, XN_STATUS_CORRUPT_FILE);

	//Notify node was added
	nRetVal = m_pNodeNotifications->OnNodeAdded(m_pNotificationsCookie, strName, type, compression, nNumberOfFrames);
	XN_IS_STATUS_OK(nRetVal);

	pPlayerNodeInfo->compression = compression;
	nRetVal = xnOSStrCopy(pPlayerNodeInfo->strName, strName, sizeof(pPlayerNodeInfo->strName));
	XN_IS_STATUS_OK(nRetVal);

	if (IsTypeGenerator(type))
	{
		pPlayerNodeInfo->bIsGenerator = TRUE;
		pPlayerNodeInfo->nFrames = nNumberOfFrames;
		pPlayerNodeInfo->nMaxTimeStamp = nMaxTimestamp;
	}

	//Mark this player node as valid
	pPlayerNodeInfo->bValid = TRUE;

	//Loop until this node's state is ready.
	//TODO: Check for eof
	while (!pPlayerNodeInfo->bStateReady)
	{
		nRetVal = ProcessRecord(TRUE);
		if (nRetVal != XN_STATUS_OK)
		{
			pPlayerNodeInfo->bValid = FALSE;
			return nRetVal;
		}
	}

	return (XN_STATUS_OK);
}

XnStatus PlayerNode::HandleNodeAdded_1_0_0_4_Record(NodeAdded_1_0_0_4_Record record)
{
	XnStatus nRetVal = XN_STATUS_OK;

	nRetVal = record.Decode();
	XN_IS_STATUS_OK(nRetVal);

	DEBUG_LOG_RECORD(record, "NodeAdded1_0_0_4");

	/** BC issue **/
	// NOTE: ONI files up to version 1.0.0.4 didn't had a different NodeAdded record. It did
	// not contain seek data (number of frames and min/max timestamp). Instead, this data was
	// in the DataBegin record. So we need to also find this record, and read these props from it.

	XnUInt32 nNodeID = record.GetNodeID();
	XnChar strName[XN_MAX_NAME_LENGTH];
	nRetVal = xnOSStrCopy(strName, record.GetNodeName(), XN_MAX_NAME_LENGTH);
	XN_IS_STATUS_OK(nRetVal);
	XnProductionNodeType type = record.GetNodeType();
	XnCodecID compression = record.GetCompression();
	XnUInt32 nNumFrames = 0;
	XnUInt64 nMinTimestamp = 0;
	XnUInt64 nMaxTimestamp = 0;

	if (IsTypeGenerator(type))
	{
		// we need to look for the DataBegin record to have number of frames, etc.
		XnUInt64 nStartPos = TellStream();

		// NOTE: this overwrites the current NodeAdded record buffer!!!
		nRetVal = SeekToRecordByType(nNodeID, RECORD_NODE_DATA_BEGIN);
		if (nRetVal == XN_STATUS_OK)
		{
			NodeDataBeginRecord dataBeginRecord(m_pRecordBuffer, RECORD_MAX_SIZE, m_bIs32bitFileFormat);
			nRetVal = ReadRecord(dataBeginRecord);
			XN_IS_STATUS_OK(nRetVal);

			nRetVal = dataBeginRecord.Decode();
			XN_IS_STATUS_OK(nRetVal);

			nNumFrames = dataBeginRecord.GetNumFrames();
			nMaxTimestamp = dataBeginRecord.GetMaxTimeStamp();

			// also find data record for min timestamp
			nRetVal = SeekToRecordByType(record.GetNodeID(), RECORD_NEW_DATA);
			if (nRetVal == XN_STATUS_OK)
			{
				NewDataRecordHeader newDataRecord(m_pRecordBuffer, RECORD_MAX_SIZE, m_bIs32bitFileFormat);
				nRetVal = ReadRecord(newDataRecord);
				XN_IS_STATUS_OK(nRetVal);

				nRetVal = newDataRecord.Decode();
				XN_IS_STATUS_OK(nRetVal);

				nMinTimestamp = newDataRecord.GetTimeStamp();
			}

			// get back to start position
			nRetVal = SeekStream(XN_OS_SEEK_SET, nStartPos);
			XN_IS_STATUS_OK(nRetVal);
		}
	}

	nRetVal = HandleNodeAddedImpl(nNodeID, type, strName, compression, nNumFrames, nMinTimestamp, nMaxTimestamp);
	XN_IS_STATUS_OK(nRetVal);

	return XN_STATUS_OK;
}

XnStatus PlayerNode::HandleNodeAdded_1_0_0_5_Record(NodeAdded_1_0_0_5_Record record)
{
	XnStatus nRetVal = XN_STATUS_OK;

	nRetVal = record.Decode();
	XN_IS_STATUS_OK(nRetVal);

	DEBUG_LOG_RECORD(record, "NodeAdded1_0_0_5");

	nRetVal = HandleNodeAddedImpl(
		record.GetNodeID(), record.GetNodeType(), record.GetNodeName(), record.GetCompression(),
		record.GetNumberOfFrames(), record.GetMinTimestamp(), record.GetMaxTimestamp());
	XN_IS_STATUS_OK(nRetVal);

	return (XN_STATUS_OK);
}

XnStatus PlayerNode::HandleNodeAddedRecord(NodeAddedRecord record)
{
	XnStatus nRetVal = XN_STATUS_OK;

	nRetVal = record.Decode();

	XN_IS_STATUS_OK(nRetVal);

	DEBUG_LOG_RECORD(record, "NodeAdded");

	nRetVal = HandleNodeAddedImpl(
		record.GetNodeID(), record.GetNodeType(), record.GetNodeName(), record.GetCompression(),
		record.GetNumberOfFrames(), record.GetMinTimestamp(), record.GetMaxTimestamp());
	XN_IS_STATUS_OK(nRetVal);

	// get seek table (if exists)
	if (record.GetNumberOfFrames() > 0 && record.GetSeekTablePosition() != 0)
	{
		XnUInt64 nCurrPos = TellStream();

		nRetVal = SeekStream(XN_OS_SEEK_SET, record.GetSeekTablePosition());
		XN_IS_STATUS_OK(nRetVal);

		DataIndexRecordHeader seekTableHeader(m_pRecordBuffer, RECORD_MAX_SIZE, m_bIs32bitFileFormat);
		nRetVal = ReadRecord(seekTableHeader);
		XN_IS_STATUS_OK(nRetVal);

		nRetVal = HandleDataIndexRecord(seekTableHeader, TRUE);
		XN_IS_STATUS_OK(nRetVal);

		// and seek back
		nRetVal = SeekStream(XN_OS_SEEK_SET, nCurrPos);
		XN_IS_STATUS_OK(nRetVal);
	}

	return (XN_STATUS_OK);
}

XnStatus PlayerNode::SeekToRecordByType(XnUInt32 nNodeID, RecordType type)
{
	XnStatus nRetVal = XN_STATUS_OK;
	
	Record record(m_pRecordBuffer, RECORD_MAX_SIZE, m_bIs32bitFileFormat);

	XnUInt64 nStartPos = TellStream();

	XnBool bFound = FALSE;
	XnUInt64 nPosBeforeRecord = 0;
	while (!bFound && nRetVal == XN_STATUS_OK)
	{
		nPosBeforeRecord = TellStream();

		nRetVal = ReadRecord(record);
		XN_IS_STATUS_OK(nRetVal);

		if ((record.GetType() == type) && (record.GetNodeID() == nNodeID))
		{
			bFound = TRUE;
		}
		else if (record.GetType() == RECORD_END)
		{
			nRetVal = XN_STATUS_NO_MATCH;
		}
		else
		{
			// if record has payload, skip it
			nRetVal = SkipRecordPayload(record);
		}
	}

	if (bFound)
	{
		// seek to before requested record
		nRetVal = SeekStream(XN_OS_SEEK_SET, nPosBeforeRecord);
		XN_IS_STATUS_OK(nRetVal);
	}
	else
	{
		// seek back to starting position
		SeekStream(XN_OS_SEEK_SET, nStartPos);
		return (nRetVal);
	}
	
	return (XN_STATUS_OK);
}

XnStatus PlayerNode::HandleGeneralPropRecord(GeneralPropRecord record)
{
	XN_VALIDATE_INPUT_PTR(m_pNodeNotifications);
	XnStatus nRetVal = record.Decode();
	XN_IS_STATUS_OK(nRetVal);
	DEBUG_LOG_RECORD(record, "GeneralProp");

	PlayerNodeInfo* pPlayerNodeInfo = GetPlayerNodeInfo(record.GetNodeID());
	XN_VALIDATE_PTR(pPlayerNodeInfo, XN_STATUS_CORRUPT_FILE);
	if (!pPlayerNodeInfo->bValid)
	{
		XN_ASSERT(FALSE);
		return XN_STATUS_CORRUPT_FILE;
	}

	// Save map output mode (resolution) - for real world translation (BC)
	if (strcmp(record.GetPropName(), XN_PROP_MAP_OUTPUT_MODE) == 0)
	{
		xnOSMemCopy(&m_lastOutputMode, record.GetPropData(), sizeof(XnMapOutputMode));
	}
	// Fix backwards compatibility issues
	if (strcmp(record.GetPropName(), XN_PROP_REAL_WORLD_TRANSLATION_DATA) == 0)
	{
		// old recordings held the RealWorldTranslationData, but API has changed. Translate
		// it to Field Of View
		if (record.GetPropDataSize() != sizeof(XnRealWorldTranslationData))
		{
			return XN_STATUS_CORRUPT_FILE;
		}

		const XnRealWorldTranslationData* pTransData = (const XnRealWorldTranslationData*)record.GetPropData();

		XnFieldOfView FOV;

		FOV.fHFOV = 2*atan(pTransData->dPixelSizeAtZeroPlane * pTransData->dSourceToDepthPixelRatio * m_lastOutputMode.nXRes / 2 / pTransData->dZeroPlaneDistance);
		FOV.fVFOV = 2*atan(pTransData->dPixelSizeAtZeroPlane * pTransData->dSourceToDepthPixelRatio * m_lastOutputMode.nYRes / 2 / pTransData->dZeroPlaneDistance);

		nRetVal = m_pNodeNotifications->OnNodeGeneralPropChanged(m_pNotificationsCookie,
			pPlayerNodeInfo->strName,
			XN_PROP_FIELD_OF_VIEW,
			sizeof(FOV),
			&FOV);
		XN_IS_STATUS_OK(nRetVal);

	}
	else
	{
		nRetVal = m_pNodeNotifications->OnNodeGeneralPropChanged(m_pNotificationsCookie,
			pPlayerNodeInfo->strName, 
			record.GetPropName(),
			record.GetPropDataSize(),
			record.GetPropData());
		XN_IS_STATUS_OK(nRetVal);
	}
	
	nRetVal = SaveRecordUndoInfo(pPlayerNodeInfo, 
		record.GetPropName(), 
		TellStream() - record.GetSize(),
		record.GetUndoRecordPos());
	XN_IS_STATUS_OK(nRetVal);

	return XN_STATUS_OK;
}

XnStatus PlayerNode::HandleIntPropRecord(IntPropRecord record)
{
	XN_VALIDATE_INPUT_PTR(m_pNodeNotifications);
	XnStatus nRetVal = record.Decode();
	XN_IS_STATUS_OK(nRetVal);
	DEBUG_LOG_RECORD(record, "IntProp");
	PlayerNodeInfo* pPlayerNodeInfo = GetPlayerNodeInfo(record.GetNodeID());
	XN_VALIDATE_PTR(pPlayerNodeInfo, XN_STATUS_CORRUPT_FILE);
	if (!pPlayerNodeInfo->bValid)
	{
		XN_ASSERT(FALSE);
		return XN_STATUS_CORRUPT_FILE;
	}

	const XnChar* strPropName = record.GetPropName();
	XnUInt64 nValue = record.GetValue();

	// old files workaround: some old files recorded nodes as not generating though having frames.
	// make them generating.
	if (strcmp(strPropName, XN_PROP_IS_GENERATING) == 0 &&
		nValue == FALSE &&
		pPlayerNodeInfo->nFrames > 0)
	{
		nValue = TRUE;
	}

	nRetVal = m_pNodeNotifications->OnNodeIntPropChanged(m_pNotificationsCookie, 
		pPlayerNodeInfo->strName,
		strPropName,
		nValue);
	XN_IS_STATUS_OK(nRetVal);

	nRetVal = SaveRecordUndoInfo(pPlayerNodeInfo, 
		record.GetPropName(), 
		TellStream() - record.GetSize(),
		record.GetUndoRecordPos());
	XN_IS_STATUS_OK(nRetVal);

	return XN_STATUS_OK;
}

XnStatus PlayerNode::HandleRealPropRecord(RealPropRecord record)
{
	XN_VALIDATE_INPUT_PTR(m_pNodeNotifications);
	XnStatus nRetVal = record.Decode();
	XN_IS_STATUS_OK(nRetVal);
	DEBUG_LOG_RECORD(record, "RealProp");

	PlayerNodeInfo* pPlayerNodeInfo = GetPlayerNodeInfo(record.GetNodeID());
	XN_VALIDATE_PTR(pPlayerNodeInfo, XN_STATUS_CORRUPT_FILE);
	if (!pPlayerNodeInfo->bValid)
	{
		XN_ASSERT(FALSE);
		return XN_STATUS_CORRUPT_FILE;
	}

	nRetVal = m_pNodeNotifications->OnNodeRealPropChanged(m_pNotificationsCookie, 
		pPlayerNodeInfo->strName,
		record.GetPropName(),
		record.GetValue());
	XN_IS_STATUS_OK(nRetVal);

	nRetVal = SaveRecordUndoInfo(pPlayerNodeInfo, 
		record.GetPropName(), 
		TellStream() - record.GetSize(),
		record.GetUndoRecordPos());
	XN_IS_STATUS_OK(nRetVal);

	return XN_STATUS_OK;
}

XnStatus PlayerNode::HandleStringPropRecord(StringPropRecord record)
{
	XN_VALIDATE_INPUT_PTR(m_pNodeNotifications);
	XnStatus nRetVal = record.Decode();
	XN_IS_STATUS_OK(nRetVal);
	DEBUG_LOG_RECORD(record, "StringProp");
	PlayerNodeInfo* pPlayerNodeInfo = GetPlayerNodeInfo(record.GetNodeID());
	XN_VALIDATE_PTR(pPlayerNodeInfo, XN_STATUS_CORRUPT_FILE);
	if (!pPlayerNodeInfo->bValid)
	{
		XN_ASSERT(FALSE);
		return XN_STATUS_CORRUPT_FILE;
	}

	nRetVal = m_pNodeNotifications->OnNodeStringPropChanged(m_pNotificationsCookie, 
		pPlayerNodeInfo->strName,
		record.GetPropName(),
		record.GetValue());
	XN_IS_STATUS_OK(nRetVal);

	nRetVal = SaveRecordUndoInfo(pPlayerNodeInfo, 
		record.GetPropName(), 
		TellStream() - record.GetSize(),
		record.GetUndoRecordPos());
	XN_IS_STATUS_OK(nRetVal);

	return XN_STATUS_OK;
}

XnStatus PlayerNode::HandleNodeRemovedRecord(NodeRemovedRecord record)
{
	XN_VALIDATE_INPUT_PTR(m_pNodeNotifications);
	XnStatus nRetVal = record.Decode();
	XN_IS_STATUS_OK(nRetVal);
	DEBUG_LOG_RECORD(record, "NodeRemoved");

	PlayerNodeInfo* pPlayerNodeInfo = GetPlayerNodeInfo(record.GetNodeID());
	XN_VALIDATE_PTR(pPlayerNodeInfo, XN_STATUS_CORRUPT_FILE);
	if (!pPlayerNodeInfo->bValid)
	{
		XN_ASSERT(FALSE);
		XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Got a node removed record for non-existing node %u.", record.GetNodeID());
	}

	nRetVal = RemovePlayerNodeInfo(record.GetNodeID());
	XN_IS_STATUS_OK(nRetVal);
	return XN_STATUS_OK;
}

XnStatus PlayerNode::HandleNodeStateReadyRecord(NodeStateReadyRecord record)
{
	XN_VALIDATE_INPUT_PTR(m_pNodeNotifications);
	XnStatus nRetVal = record.Decode();
	XN_IS_STATUS_OK(nRetVal);
	DEBUG_LOG_RECORD(record, "NodeStateReady");
	PlayerNodeInfo* pPlayerNodeInfo = GetPlayerNodeInfo(record.GetNodeID());
	XN_VALIDATE_PTR(pPlayerNodeInfo, XN_STATUS_CORRUPT_FILE);
	if (!pPlayerNodeInfo->bValid)
	{
		XN_ASSERT(FALSE);
		return XN_STATUS_CORRUPT_FILE;
	}

	// after wrap-around, if node wasn't destroyed, no need to notify about state ready
	if (!pPlayerNodeInfo->bStateReady)
	{
		nRetVal = m_pNodeNotifications->OnNodeStateReady(m_pNotificationsCookie, pPlayerNodeInfo->strName);
		XN_IS_STATUS_OK(nRetVal);
	}

	if (pPlayerNodeInfo->bIsGenerator && 
		(pPlayerNodeInfo->compression != XN_CODEC_NULL) && 
		(pPlayerNodeInfo->pCodec == NULL))
	{
		// TODO: create codec
		if (m_pNodeCodecFactory == NULL)
		{
			XN_ASSERT(FALSE);
			return XN_STATUS_NOT_INIT;
		}

		// Create the required codec.
		nRetVal = m_pNodeCodecFactory->Create(m_pNodeCodecFactoryCookie, 
											  pPlayerNodeInfo->strName,
											  pPlayerNodeInfo->compression, 
											  &pPlayerNodeInfo->pCodec);
		XN_IS_STATUS_OK_LOG_ERROR("Create codec", nRetVal);
	}

	pPlayerNodeInfo->bStateReady = TRUE;
	return XN_STATUS_OK;
}

XnStatus PlayerNode::HandleNodeDataBeginRecord(NodeDataBeginRecord record)
{
	XN_VALIDATE_INPUT_PTR(m_pNodeNotifications);
	XnStatus nRetVal = record.Decode();
	XN_IS_STATUS_OK(nRetVal);
	DEBUG_LOG_RECORD(record, "NodeDataBegin");
	PlayerNodeInfo* pPlayerNodeInfo = GetPlayerNodeInfo(record.GetNodeID());
	XN_VALIDATE_PTR(pPlayerNodeInfo, XN_STATUS_CORRUPT_FILE);
	if (!pPlayerNodeInfo->bValid)
	{
		XN_ASSERT(FALSE);
		return XN_STATUS_CORRUPT_FILE;
	}

	if (!pPlayerNodeInfo->bIsGenerator)
	{
		XN_ASSERT(FALSE);
		XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Got data for non-generator node '%s'", pPlayerNodeInfo->strName);
	}

	m_bDataBegun = TRUE;

	return XN_STATUS_OK;	
}

XnStatus PlayerNode::HandleNewDataRecord(NewDataRecordHeader record, XnBool bReadPayload)
{
	XN_VALIDATE_INPUT_PTR(m_pNodeNotifications);
	XnStatus nRetVal = record.Decode();
	XN_IS_STATUS_OK(nRetVal);
	DEBUG_LOG_RECORD(record, "NewData");

	XN_ASSERT(record.GetNodeID() != INVALID_NODE_ID);
	PlayerNodeInfo* pPlayerNodeInfo = GetPlayerNodeInfo(record.GetNodeID());
	XN_VALIDATE_PTR(pPlayerNodeInfo, XN_STATUS_CORRUPT_FILE);
	if (!pPlayerNodeInfo->bValid)
	{
		XN_ASSERT(FALSE);
		return XN_STATUS_CORRUPT_FILE;
	}

	XnUInt32 nRecordTotalSize = record.GetSize() + record.GetPayloadSize();
	if (nRecordTotalSize > RECORD_MAX_SIZE)
	{
		XN_ASSERT(FALSE);
		XN_LOG_ERROR_RETURN(XN_STATUS_INTERNAL_BUFFER_TOO_SMALL, XN_MASK_OPEN_NI, "Record size %u is larger than player internal buffer", nRecordTotalSize);
	}
	
	pPlayerNodeInfo->nLastDataPos = TellStream() - record.GetSize();
	pPlayerNodeInfo->newDataUndoInfo.nRecordPos =  pPlayerNodeInfo->nLastDataPos;
	pPlayerNodeInfo->newDataUndoInfo.nUndoRecordPos = record.GetUndoRecordPos();
	if (record.GetFrameNumber() > pPlayerNodeInfo->nFrames)
	{
		XN_ASSERT(FALSE);
		return XN_STATUS_CORRUPT_FILE;
	}

	pPlayerNodeInfo->nCurFrame = record.GetFrameNumber();
	
	if (record.GetTimeStamp() > m_nGlobalMaxTimeStamp)	
	{
		XN_ASSERT(FALSE);
		XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Record timestamp for record in position %u is larger than reported max timestamp", pPlayerNodeInfo->nLastDataPos);
	}

	m_nTimeStamp = record.GetTimeStamp();

	if (bReadPayload)
	{
		//Now read the actual data
		XnUInt32 nBytesRead = 0;
		nRetVal = Read(record.GetPayload(), record.GetPayloadSize(), nBytesRead);
		XN_IS_STATUS_OK(nRetVal);
		if (nBytesRead < record.GetPayloadSize())
		{
			XN_ASSERT(FALSE);
			XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Not enough bytes read");
		}

		const XnUInt8* pCompressedData = record.GetPayload(); //The new (compressed) data is right at the end of the header
		XnUInt32 nCompressedDataSize = record.GetPayloadSize();
		const XnUInt8* pUncompressedData = NULL;
		XnUInt32 nUncompressedDataSize = 0;
		XnCodecID compression = (pPlayerNodeInfo->pCodec == NULL) ? XN_CODEC_NULL :
								 pPlayerNodeInfo->pCodec->GetCodecID();
		if (compression == XN_CODEC_UNCOMPRESSED)
		{
			pUncompressedData = pCompressedData;
			nUncompressedDataSize = nCompressedDataSize;
		}
		else
		{
			//Decode data with codec
			nUncompressedDataSize = DATA_MAX_SIZE;
			nRetVal = pPlayerNodeInfo->pCodec->Decompress(pCompressedData, nCompressedDataSize, 
														  m_pUncompressedData, &nUncompressedDataSize);
			XN_IS_STATUS_OK_ASSERT(nRetVal);
			pUncompressedData = m_pUncompressedData;
		}

		nRetVal = m_pNodeNotifications->OnNodeNewData(m_pNotificationsCookie, pPlayerNodeInfo->strName, 
													  record.GetTimeStamp(), record.GetFrameNumber(), 
													  pUncompressedData, nUncompressedDataSize);
		XN_IS_STATUS_OK_ASSERT(nRetVal);
	}
	else
	{
		//Just skip the data
		nRetVal = SkipRecordPayload(record);
		XN_IS_STATUS_OK_ASSERT(nRetVal);
	}

	return XN_STATUS_OK;
}

XnStatus PlayerNode::HandleDataIndexRecord(DataIndexRecordHeader record, XnBool bReadPayload)
{
	XnStatus nRetVal = XN_STATUS_OK;
	
	XN_VALIDATE_INPUT_PTR(m_pNodeNotifications);
	nRetVal = record.Decode();
	XN_IS_STATUS_OK_ASSERT(nRetVal);
	DEBUG_LOG_RECORD(record, "DataIndex");

	XN_ASSERT(record.GetNodeID() != INVALID_NODE_ID);
	PlayerNodeInfo* pPlayerNodeInfo = GetPlayerNodeInfo(record.GetNodeID());
	XN_VALIDATE_PTR(pPlayerNodeInfo, XN_STATUS_CORRUPT_FILE);

	XnUInt32 nRecordTotalSize = record.GetSize() + record.GetPayloadSize();
	if (nRecordTotalSize > RECORD_MAX_SIZE)
	{
		XN_ASSERT(FALSE);
		XN_LOG_ERROR_RETURN(XN_STATUS_INTERNAL_BUFFER_TOO_SMALL, XN_MASK_OPEN_NI, "Record size %u is larger than player internal buffer", nRecordTotalSize);
	}

	if (bReadPayload)
	{
		// make sure node exists
		if (!pPlayerNodeInfo->bValid)
		{
			XN_ASSERT(FALSE);
			return XN_STATUS_CORRUPT_FILE;
		}

		XnUInt32 DIESize;
		if (m_bIs32bitFileFormat) 	DIESize = sizeof(DataIndexEntry_old32);
		else						DIESize = sizeof(DataIndexEntry);

		if (record.GetPayloadSize() != (pPlayerNodeInfo->nFrames+1) * DIESize)
		{
			XN_ASSERT(FALSE);
			XN_LOG_WARNING_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Seek table has %u entries, but node has %u frames!", record.GetPayloadSize() / DIESize, pPlayerNodeInfo->nFrames);
		}

		// allocate our data index
		pPlayerNodeInfo->pDataIndex = (DataIndexEntry*)xnOSCalloc(pPlayerNodeInfo->nFrames+1, sizeof(DataIndexEntry));
		XN_VALIDATE_ALLOC_PTR(pPlayerNodeInfo->pDataIndex);

		//Now read the actual data
		XnUInt32 nBytesRead = 0;

		if (m_bIs32bitFileFormat)
		{
			DataIndexEntry_old32 old32;
			XnUInt32 nRead = 0;
			for(XnUInt32 n = 0; n < pPlayerNodeInfo->nFrames+1; n++)
			{
				nRetVal = Read(&old32, sizeof(DataIndexEntry_old32), nRead);
				XN_IS_STATUS_OK(nRetVal); nBytesRead += nRead;
				DataIndexEntry::FillFromOld32Entry(&pPlayerNodeInfo->pDataIndex[n], &old32);
			}
		}
		else
		{
			nRetVal = Read(pPlayerNodeInfo->pDataIndex, record.GetPayloadSize(), nBytesRead);
			XN_IS_STATUS_OK(nRetVal);
		}

		if (nBytesRead < record.GetPayloadSize())
		{
			XN_LOG_ERROR_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "Not enough bytes read");
		}
	}
	else
	{
		//Just skip the data
		nRetVal = SkipRecordPayload(record);
		XN_IS_STATUS_OK(nRetVal);
	}

	return XN_STATUS_OK;
}

XnStatus PlayerNode::HandleEndRecord(EndRecord record)
{
	XN_VALIDATE_INPUT_PTR(m_pNodeNotifications);
	XnStatus nRetVal = record.Decode();
	XN_IS_STATUS_OK(nRetVal);
	DEBUG_LOG_RECORD(record, "End");

	if (!m_bDataBegun)
	{
		XN_LOG_WARNING_RETURN(XN_STATUS_CORRUPT_FILE, XN_MASK_OPEN_NI, "File does not contain any data!");
	}

	m_bEOF = !m_bRepeat;

	nRetVal = m_eofReachedEvent.Raise();
	XN_IS_STATUS_OK(nRetVal);

	if (m_bRepeat)
	{
		nRetVal = Rewind();
		XN_IS_STATUS_OK(nRetVal);
	}
	else
	{
		CloseStream();
	}

	return XN_STATUS_OK;	
}


XnStatus PlayerNode::Rewind()
{
	//skip recording header
	XnStatus nRetVal = SeekStream(XN_OS_SEEK_SET, sizeof(RecordingHeader));
	XN_IS_STATUS_OK(nRetVal);

	//Reset all node info's
	for (XnUInt32 i = 0; i < m_nMaxNodes; i++)
	{
		m_pNodeInfoMap[i].Reset();
	}

	m_bDataBegun = FALSE;
	m_nTimeStamp = 0;
	m_bEOF = FALSE;
	
	//Skip to first data
	nRetVal = ProcessUntilFirstData();
	XN_IS_STATUS_OK(nRetVal);
	return XN_STATUS_OK;
}

XnStatus PlayerNode::ProcessUntilFirstData()
{
	XnStatus nRetVal = XN_STATUS_OK;
	//Loop until we get to the first 'data begin' marker in the file.

	while (!m_bDataBegun)
	{
		nRetVal = ProcessRecord(TRUE);	
		XN_IS_STATUS_OK(nRetVal);
	}
	
	return XN_STATUS_OK;
}

XnStatus PlayerNode::SeekToTimeStampAbsolute(XnUInt64 nDestTimeStamp)
{
	XnStatus nRetVal = XN_STATUS_OK;
	XnUInt64 nRecordTimeStamp = 0LL;
	XnUInt64 nStartPos = TellStream(); //We'll revert to this in case nDestTimeStamp is beyond end of stream
	XN_IS_STATUS_OK(nRetVal);

	if (nDestTimeStamp < m_nTimeStamp)
	{
		nRetVal = Rewind();
	}
	else if (nDestTimeStamp == m_nTimeStamp)
	{
		//Nothing to do
		return XN_STATUS_OK;
	}
	else if (nDestTimeStamp > m_nGlobalMaxTimeStamp)
	{
		nDestTimeStamp = m_nGlobalMaxTimeStamp;
	}

	Record record(m_pRecordBuffer, RECORD_MAX_SIZE, m_bIs32bitFileFormat);
	XnBool bEnd = FALSE;
	XnUInt32 nBytesRead = 0;

	while ((nRecordTimeStamp < nDestTimeStamp) && !bEnd)
	{
		nRetVal = ReadRecordHeader(record);
		XN_IS_STATUS_OK(nRetVal);
		switch (record.GetType())
		{
			case RECORD_NEW_DATA:
			{
				//We already read Record::HEADER_SIZE, now read the rest of the new data record header
				nRetVal = Read(m_pRecordBuffer + record.HEADER_SIZE, 
					NewDataRecordHeader::MAX_SIZE - record.HEADER_SIZE, 
					nBytesRead);
				XN_IS_STATUS_OK(nRetVal);
				if (nBytesRead < NewDataRecordHeader::MAX_SIZE - record.HEADER_SIZE)
				{
					return XN_STATUS_CORRUPT_FILE;
				}
				NewDataRecordHeader newDataRecordHeader(record);
				nRetVal = newDataRecordHeader.Decode();
				XN_IS_STATUS_OK(nRetVal);
				//Save record time stamp
				nRecordTimeStamp = newDataRecordHeader.GetTimeStamp();
				
				if (nRecordTimeStamp >= nDestTimeStamp)
				{
					//We're done - move back to beginning of record
					nRetVal = SeekStream(XN_OS_SEEK_CUR, -XnInt32(nBytesRead));
					XN_IS_STATUS_OK(nRetVal);
				}
				else
				{
					//Skip to next record
					nRetVal = SeekStream(XN_OS_SEEK_CUR, 
						newDataRecordHeader.GetSize() - NewDataRecordHeader::MAX_SIZE);
					XN_IS_STATUS_OK(nRetVal);
				}
				break;
			}

			case RECORD_END:
			{
				bEnd = TRUE;
				break;
			}

			case RECORD_NODE_ADDED_1_0_0_4:
			case RECORD_NODE_ADDED_1_0_0_5:
			case RECORD_NODE_ADDED:
			case RECORD_INT_PROPERTY:	
			case RECORD_REAL_PROPERTY:	
			case RECORD_STRING_PROPERTY:
			case RECORD_GENERAL_PROPERTY:
			case RECORD_NODE_REMOVED:	
			case RECORD_NODE_DATA_BEGIN:
			case RECORD_NODE_STATE_READY:
			{
				//Read rest of record and handle it normally
				nRetVal = Read(m_pRecordBuffer + record.HEADER_SIZE, record.GetSize() - record.HEADER_SIZE, nBytesRead);
				XN_IS_STATUS_OK(nRetVal);
				Record record(m_pRecordBuffer, RECORD_MAX_SIZE, m_bIs32bitFileFormat);
				nRetVal = HandleRecord(record, TRUE);
				XN_IS_STATUS_OK(nRetVal);
				break;
			}
			default:
			{
				XN_ASSERT(FALSE);
				return XN_STATUS_CORRUPT_FILE;
			}

		} //switch
	} //while
	
	if (bEnd)
	{
		SeekStream(XN_OS_SEEK_SET, nStartPos);
		return XN_STATUS_ILLEGAL_POSITION;
	}

	return XN_STATUS_OK;
}//function

XnStatus PlayerNode::SeekToTimeStampRelative(XnInt64 nOffset)
{
	//TODO: Implement more efficiently
	return SeekToTimeStampAbsolute(m_nTimeStamp + nOffset);
}

XnUInt32 PlayerNode::GetPlayerNodeIDByName(const XnChar* strNodeName)
{
	for (XnUInt32 i = 0; i < m_nMaxNodes; i++)
	{
		if (xnOSStrCmp(strNodeName, m_pNodeInfoMap[i].strName) == 0)
		{
			return i;
		}
	}
	return INVALID_NODE_ID;
}

PlayerNode::PlayerNodeInfo* PlayerNode::GetPlayerNodeInfoByName(const XnChar* strNodeName)
{
	XnUInt32 nNodeID = GetPlayerNodeIDByName(strNodeName);
	return (nNodeID == INVALID_NODE_ID) ? NULL : &m_pNodeInfoMap[nNodeID];
}

XnStatus PlayerNode::SaveRecordUndoInfo(PlayerNodeInfo* pPlayerNodeInfo, 
										const XnChar* strPropName, 
										XnUInt64 nRecordPos,
										XnUInt64 nUndoRecordPos)
{
	RecordUndoInfo recordUndoInfo;
	recordUndoInfo.nRecordPos = nRecordPos;
	recordUndoInfo.nUndoRecordPos = nUndoRecordPos;
	XnStatus nRetVal = pPlayerNodeInfo->recordUndoInfoMap.Set(strPropName, recordUndoInfo);
	XN_IS_STATUS_OK(nRetVal);
	return XN_STATUS_OK;
}

XnStatus PlayerNode::GetRecordUndoInfo(PlayerNodeInfo* pPlayerNodeInfo, const XnChar* strPropName, XnUInt64& nRecordPos, XnUInt64& nUndoRecordPos)
{
	RecordUndoInfo *pRecordUndoInfo = NULL;
	XnStatus nRetVal = pPlayerNodeInfo->recordUndoInfoMap.Get(strPropName, pRecordUndoInfo);
	XN_IS_STATUS_OK(nRetVal);
	nRecordPos = pRecordUndoInfo->nRecordPos;
	nUndoRecordPos = pRecordUndoInfo->nUndoRecordPos;
	return XN_STATUS_OK;
}

XnStatus PlayerNode::SkipRecordPayload(Record record)
{
	return SeekStream(XN_OS_SEEK_CUR, record.GetPayloadSize());
}

PlayerNode::PlayerNodeInfo::PlayerNodeInfo()
{
	pCodec = NULL;
	pDataIndex = NULL;
	Reset();
}

PlayerNode::PlayerNodeInfo::~PlayerNodeInfo()
{
}

void PlayerNode::PlayerNodeInfo::Reset()
{
	xnOSMemSet(strName, 0, sizeof(strName));
	nLastDataPos = 0;
	compression = XN_CODEC_NULL;
	nFrames = 0; 
	nCurFrame = 0;
	nMaxTimeStamp = 0;
	bStateReady = FALSE;
	bIsGenerator = FALSE;
	recordUndoInfoMap.Clear();
	newDataUndoInfo.Reset();
	bValid = FALSE;
	xnOSFree(pDataIndex);
	pDataIndex = NULL;
}

}