Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_Kinect2Net.cpp
1 #include "VideoSource_Kinect2Net.hh"
2 #include <iostream>
3 #include <Base/Image/ImageIO.hh>
4 
5 #include <iostream>
6 
7 using namespace std;
8 using namespace BIAS;
9 
10 VideoSource_Kinect2Net::VideoSource_Kinect2Net(){
11  numDevices_ = 0;
12  stopThread_ = false;
13  port_ = 8090;
14  host_ = "localhost";
15  //grabThread_ = 0;
16 }
17 
18 VideoSource_Kinect2Net::~VideoSource_Kinect2Net(){
19 }
20 
21 void* VideoSource_Kinect2Net::runner(void *args) {
23  n->client_.SetVerbose(true);
24  n->client_.RegisterMsg("img", BIAS::CS_BINARY, 512*424*2*sizeof(float));
25  int ret = n->client_.ConnectServer(n->host_, n->port_);
26 
27  if (ret == 0) {
28 
29  std::vector<char> binary;
30  while (!n->stopThread_) {
31  timespec ts;
32 #if WIN32
33  __int64 wintime;
34  GetSystemTimeAsFileTime((FILETIME*)&wintime);
35  wintime -= 116444736000000000i64;
36  ts.tv_sec = (long)(wintime / 10000000i64);
37  ts.tv_nsec = (long)(wintime % 10000000i64 *100);
38 #else
39  clock_gettime(CLOCK_REALTIME, &ts);
40 #endif
41  ts.tv_sec += 2;
42  if (n->client_.GetData("img", binary) == 0) {
43  int semOk = sem_timedwait(&n->depthbuffer_.sProd, &ts);
44  if (semOk != 0) {
45  std::cout << "net runner dropping frames!" << std::endl;
46  biasusleep(250);
47  } else {
48  n->depthbuffer_.buffer[ n->depthbuffer_.curProd ].CopyIn_NoInit ( binary.data() );
49  n->depthbuffer_.curProd = (n->depthbuffer_.curProd+1) % buffersize_;
50  sem_post(&n->depthbuffer_.sCons);
51  }
52  semOk = sem_timedwait(&n->irbuffer_.sProd, &ts);
53  if (semOk != 0) {
54  std::cout << "net runner dropping frames!" << std::endl;
55  biasusleep(250);
56  } else {
57  //float *data = (float*)binary.data();
58  n->irbuffer_.buffer[ n->irbuffer_.curProd ].CopyIn_NoInit ( binary.data()+512*424*sizeof(float) );
59  n->irbuffer_.curProd = (n->irbuffer_.curProd+1) % buffersize_;
60  sem_post(&n->irbuffer_.sCons);
61  }
62  }
63  }
64  }
65  return NULL;
66 }
67 
68 int VideoSource_Kinect2Net::OpenDevice(){
69  return OpenDevice("localhost", 8090);
70 }
71 
72 int VideoSource_Kinect2Net::OpenDevice(std::string host, int port) {
73  host_ = host;
74  port_ = port;
75  return 0;
76 }
77 
78 int VideoSource_Kinect2Net::CloseDevice(){
79  return 0;
80 }
81 
82 
83 int VideoSource_Kinect2Net::PreGrab(){
84  stopThread_ = false;
85  pthread_create(&grabThread_, NULL, &runner, this);
86  return 0;
87 }
88 
89 int VideoSource_Kinect2Net::PostGrab(){
90  stopThread_ = true;
91  pthread_join(grabThread_, NULL);
92  return 0;
93 }
94 
95 int VideoSource_Kinect2Net::InitDepthImage(ImageBase &Image){
96  Image.Init(512,424,1,ImageBase::ST_float);
97  return 0;
98 }
99 
100 int VideoSource_Kinect2Net::InitIrImage(ImageBase &Image){
101  Image.Init(512,424,1,ImageBase::ST_float);
102  return 0;
103 }
104 
105 int VideoSource_Kinect2Net::GrabSingleIR(Camera <float> &image){
106  sem_wait(&irbuffer_.sCons);
107  image.CopyIn_NoInit( irbuffer_.buffer[ irbuffer_.curCons ].GetImageData() );
108  irbuffer_.curCons = (irbuffer_.curCons+1) % buffersize_;
109  sem_post(&irbuffer_.sProd);
110  return 0;
111 }
112 
113 int VideoSource_Kinect2Net::GrabSingleDepth(Camera <float> &image){
114  sem_wait(&depthbuffer_.sCons);
115  image.CopyIn_NoInit( depthbuffer_.buffer[ depthbuffer_.curCons ].GetImageData() );
116  depthbuffer_.curCons = (depthbuffer_.curCons+1) % buffersize_;
117  sem_post(&depthbuffer_.sProd);
118  return 0;
119 }
120 
121 
122 int VideoSource_Kinect2Net::GetNumDevices(){
123  return numDevices_;
124 }
125 
126 int VideoSource_Kinect2Net::GetCapabilities(VideoSourceCapabilities &caps){
127  return 0;
128 }
129 
130 
131 int VideoSource_Kinect2Net::GetCapabilities(const char *device, VideoSourceCapabilities &caps){
132  return 0;
133 }
void CopyIn_NoInit(void *data)
Take some data and fill it into the Image.
Definition: ImageBase.cpp:827
int RegisterMsg(std::string msgName, EdataType dataType, int amount=1)
only registered msgs are accepted by commPartners Register a msg with this functions.
Definition: CScommBase.cpp:315
int ConnectServer(std::string serverName, unsigned int port=D_CS_DEFAULT_PORT, unsigned int timeOut=20000)
for a client: tries to connect to server with hostname serverName.
void SetVerbose(bool on)
gives some information about establising conn, and disconnecting etc.
The image template class for specific storage types.
Definition: Image.hh:78
void Init(unsigned int width, unsigned int height, unsigned int nChannels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
Initialize image size and channels.
Definition: ImageBase.cpp:229
Checks for VideoSource capabilities.
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
Provides remote access to a Microsoft Kinect2 camera.
int GetData(const std::string &msgName, std::vector< float > &floatData)
returns instantly:&lt;br&gt; 0 if new data has arrived and the data in xxxData 1 if no new data has arri...
Definition: CScommBase.cpp:170