-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinectRGBStream.cpp
executable file
·120 lines (99 loc) · 3.32 KB
/
KinectRGBStream.cpp
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
/**
* @file KinectRGBStream.cpp
* @ingroup Kinect
* @author Dominique Vaufreydaz, Grenoble Alpes University, Inria
* @copyright All right reserved.
*/
#include "KinectRGBStream.h"
using namespace MobileRGBD::Kinect2;
/* virtual */ void FUNCTION_CALL_TYPE KinectRGBStream::Run()
{
if ( RecContext == nullptr )
{
fprintf( stderr, "No buffer available for RGB\n" );
return;
}
IKinectSensor * m_pKinectSensor = Caller.pSensor;
// Initialize the Kinect and get the color reader
IColorFrameSource* pColorFrameSource = nullptr;
if (FAILED(m_pKinectSensor->get_ColorFrameSource(&pColorFrameSource)) || pColorFrameSource == nullptr )
{
SafeRelease(m_pKinectSensor);
return;
}
IColorFrameReader * m_pColorFrameReader = nullptr;
if (FAILED( pColorFrameSource->OpenReader(&m_pColorFrameReader)) || m_pColorFrameReader == nullptr )
{
SafeRelease(m_pKinectSensor);
SafeRelease(pColorFrameSource);
SafeRelease(m_pColorFrameReader);
return;
}
SafeRelease(pColorFrameSource);
WAITABLE_HANDLE Wait = NULL;
m_pColorFrameReader->SubscribeFrameArrived(&Wait);
#ifdef RESIZE_KINECT_RGB
KinectImageConverter ImgConvert;
#endif
while( !StopPending() )
{
IColorFrameArrivedEventArgs* pArgs = nullptr;
if ( FAILED(m_pColorFrameReader->GetFrameArrivedEventData(Wait, &pArgs)) )
{
// Thread::Sleep(5);
continue;
}
SafeRelease(pArgs);
IColorFrame * pColorFrame = nullptr;
if ( FAILED(m_pColorFrameReader->AcquireLatestFrame(&pColorFrame)) || pColorFrame == nullptr )
{
// Thread::Sleep(5);
continue;
}
// GetFrame;
BYTE * FrameData = nullptr;
if ( RecContext->InitDescription == true )
{
Caller.GetFrameDescription(pColorFrame, *RecContext);
RecContext->InitDescription = false;
}
// return buffer size if wrong in august version of sdk, used computed one
UINT BufferSize = 0;
if ( FAILED( pColorFrame->AccessRawUnderlyingBuffer(&BufferSize, &FrameData)) || FrameData == nullptr )
{
fprintf(stderr, "Could not get %s buffer\n", "RGB" );
return;
}
else
{
#ifdef RESIZE_KINECT_RGB
if ( ImgConvert.ConvertYVY2ToBRG( FrameData, (unsigned char*)RecContext->BufferData, 1920, 1080, RESIZE_KINECT_RGB ) == false )
{
fprintf(stderr, "Could not convert %s buffer\n", "RGB" );
return;
}
RecContext->BufferSize = (1920*1080/RESIZE_KINECT_RGB)*3;
RecContext->Width = 1920/RESIZE_KINECT_RGB;
RecContext->Height = 1080/RESIZE_KINECT_RGB;
RecContext->BytesPerPixel = 3;
RecContext->FrameType = "BGR";
#else
memcpy(RecContext->BufferData, FrameData, RecContext->BufferSize );
#endif
}
// Get Frame timestamp if possible
RecContext->LastFrameTime = 0;
pColorFrame->get_RelativeTime(&(RecContext->LastFrameTime));
SafeRelease( pColorFrame );
timeb lTimestamp;
ftime(&lTimestamp);
// Save data
RecContext->SaveDataAndIncreaseInputNumber( lTimestamp, nullptr );
// Call Process function if any
Caller.ProcessColorFrame(RecContext->BufferData, RecContext->BufferSize, RecContext->Width, RecContext->Height, KS_YUV2, RecContext->InputNumber, lTimestamp, RecContext->LastFrameTime );
}
m_pColorFrameReader->UnsubscribeFrameArrived(Wait);
SafeRelease( m_pColorFrameReader );
m_pKinectSensor->Close();
SafeRelease(m_pKinectSensor);
}