CustusX  2023.01.05-dev+develop.0da12
An IGT application
cxVideoSourceSHM.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 
13 #include "cxVideoSourceSHM.h"
14 
15 #include <boost/array.hpp>
16 #include <vector>
17 
18 #include <vtkImageData.h>
19 #include <vtkImageImport.h>
20 #include <vtkPNGReader.h>
21 
22 
23 #include <qtimer.h>
24 
25 namespace cx
26 {
27 
28 VideoSourceSHM::VideoSourceSHM(int width, int height, int depth)
29  : mImageWidth(width), mImageHeight(height), mImageColorDepth(depth), mImageImport(vtkImageImportPtr::New())
30 {
31  mImportInitialized = false;
32  mStartWhenConnected = false;
33 
34  mImageImport->SetDataScalarTypeToUnsignedChar();
35  mImageImport->SetNumberOfScalarComponents(3);
36 
37  mConnected = false;
38  mStreaming = false;
39 
40  mPollTimer = new QTimer(this);
41  mPollTimer->setInterval(40); // Polling interval (currently set @ 25 fps)
42  mTimeStamp = 0;
43 
44 
45  mImageData = mImageImport->GetOutput();
46 }
47 
49 {
50  // Disconnect
52 }
53 
55 {
56  return mSource.key();
57 }
58 
60 {
61  return "SHMLink";
62 }
63 
65 {
66  return mImageImport->GetOutput();
67 }
68 
70 {
71  return (double) mSource.timestamp().toMSecsSinceEpoch();
72 }
73 
76 {
77  return "NOT YET IMPLEMENTED";
78 }
79 
82 {
83  return QString("%1 and %2").arg(isConnected() ? "Connected" : "Not connected").arg(isStreaming() ? "streaming" : "not streaming");;
84 }
85 
87 {
88  if (!isConnected())
89  {
90  mStartWhenConnected = true;
91  return;
92  }
93  mStartWhenConnected = false;
94  if (!mStreaming)
95  {
96  mPollTimer->start();
97  // If all is well - tell the system we're streaming
98  mStreaming = true;
99 
100  emit streaming(mStreaming);
101  }
102 }
103 
105 {
106  mStartWhenConnected = false;
107  if (mStreaming)
108  {
109  mPollTimer->stop();
110  // If all is well - tell the system we've stopped streaming
111  mStreaming = false;
112 
113  emit streaming(mStreaming);
114  }
115 
116 }
117 
119 {
120  // return (isConnected() && isStreaming()); // Currently only check available
121  return isConnected() && mImportInitialized;
122 }
123 
125 {
126  return mConnected;
127 }
128 
130 {
131  return (isConnected() && mStreaming);
132 }
133 
139 {
140  unsigned char* buffer = (unsigned char*) mSource.isNew(); // Fetch new data from server - NULL if no new data present
141  if (!buffer)
142  return;
143 
144  mTimeStamp = mSource.timestamp().currentMSecsSinceEpoch();
145 
146  int numChannels = mImageColorDepth/sizeof(uchar);
147  Q_UNUSED(numChannels);
148 
149  mImageImport->SetWholeExtent(0, mImageWidth - 1, 0, mImageHeight - 1, 0, 0);
150  mImageImport->SetDataExtentToWholeExtent();
151 
152  mImageImport->SetImportVoidPointer(buffer);
153  mImageImport->Update();
154  mImportInitialized = true;
155 
156  emit newFrame();
157 }
158 
163 void VideoSourceSHM::connectServer(const QString& key)
164 {
165  mConnected = mSource.attach(key);
166 
167  if (mConnected)
168  {
169  connect(mPollTimer, SIGNAL(timeout()), this, SLOT(serverPollSlot()));
170  serverPollSlot(); // Pull in a new frame here, even if we may no be started yet to initialize the image import
171  }
172  if (mStartWhenConnected)
173  {
174  start();
175  }
176 }
177 
183 {
184  stop();
185 
186  if (mConnected)
187  {
188  disconnect(mPollTimer, SIGNAL(timeout()), this, SLOT(serverPollSlot()));
189  mSource.release();
190  }
191 
192  mSource.detach();
193  mConnected = false;
194 }
195 
199 void VideoSourceSHM::serverPollSlot()
200 {
201  this->update();
202 }
203 
204 void VideoSourceSHM::setResolution(double resolution)
205 {
206  mImageImport->SetDataSpacing(resolution, resolution, 1);
207 }
208 } // end namespace
virtual QString getInfoString() const
Returns a short info message.
void release()
Release our read buffer.
void streaming(bool on)
emitted when streaming started/stopped
virtual double getTimestamp()
virtual void start()
start streaming
virtual vtkImageDataPtr getVtkImageData()
const void * isNew()
Return new buffer only if new is available, otherwise return NULL.
virtual QString getName()
virtual bool isConnected() const
return true when a connection to the data source is established.
void connectServer(const QString &key)
virtual QString getStatusString() const
Returns a short status message.
vtkSmartPointer< class vtkImageImport > vtkImageImportPtr
virtual bool isStreaming() const
return true when the source is streaming data.
VideoSourceSHM(int width=0, int height=0, int depth=24)
bool attach(const QString &key)
unsigned char uchar
virtual void stop()
stop streaming
virtual QString getUid()
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
void newFrame()
emitted when a new frame has arrived (getVtkImageData() returns something new). info/status/name/vali...
virtual bool validData() const
return true is data stream is ok to display. This is a heuristic based on the data rate...
virtual void setResolution(double resolution)
Namespace for all CustusX production code.