Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 
34 #include "cxVideoSourceSHM.h"
35 
36 #include <boost/array.hpp>
37 #include <vector>
38 
39 #include <vtkImageData.h>
40 #include <vtkImageImport.h>
41 #include <vtkPNGReader.h>
42 
43 
44 #include <qtimer.h>
45 
46 namespace cx
47 {
48 
49 VideoSourceSHM::VideoSourceSHM(int width, int height, int depth)
50  : mImageWidth(width), mImageHeight(height), mImageColorDepth(depth), mImageImport(vtkImageImportPtr::New())
51 {
52  mImportInitialized = false;
53  mStartWhenConnected = false;
54 
55  mImageImport->SetDataScalarTypeToUnsignedChar();
56  mImageImport->SetNumberOfScalarComponents(3);
57 
58  mConnected = false;
59  mStreaming = false;
60 
61  mPollTimer = new QTimer(this);
62  mPollTimer->setInterval(40); // Polling interval (currently set @ 25 fps)
63  mTimeStamp = 0;
64 
65 
66  mImageData = mImageImport->GetOutput();
67 }
68 
70 {
71  // Disconnect
73 }
74 
76 {
77  return mSource.key();
78 }
79 
81 {
82  return "SHMLink";
83 }
84 
86 {
87  return mImageImport->GetOutput();
88 }
89 
91 {
92  return (double) mSource.timestamp().toMSecsSinceEpoch();
93 }
94 
97 {
98  return "NOT YET IMPLEMENTED";
99 }
100 
103 {
104  return QString("%1 and %2").arg(isConnected() ? "Connected" : "Not connected").arg(isStreaming() ? "streaming" : "not streaming");;
105 }
106 
108 {
109  if (!isConnected())
110  {
111  mStartWhenConnected = true;
112  return;
113  }
114  mStartWhenConnected = false;
115  if (!mStreaming)
116  {
117  mPollTimer->start();
118  // If all is well - tell the system we're streaming
119  mStreaming = true;
120 
121  emit streaming(mStreaming);
122  }
123 }
124 
126 {
127  mStartWhenConnected = false;
128  if (mStreaming)
129  {
130  mPollTimer->stop();
131  // If all is well - tell the system we've stopped streaming
132  mStreaming = false;
133 
134  emit streaming(mStreaming);
135  }
136 
137 }
138 
140 {
141  // return (isConnected() && isStreaming()); // Currently only check available
142  return isConnected() && mImportInitialized;
143 }
144 
146 {
147  return mConnected;
148 }
149 
151 {
152  return (isConnected() && mStreaming);
153 }
154 
160 {
161  unsigned char* buffer = (unsigned char*) mSource.isNew(); // Fetch new data from server - NULL if no new data present
162  if (!buffer)
163  return;
164 
165  mTimeStamp = mSource.timestamp().currentMSecsSinceEpoch();
166 
167  int numChannels = mImageColorDepth/sizeof(uchar);
168  Q_UNUSED(numChannels);
169 
170  mImageImport->SetWholeExtent(0, mImageWidth - 1, 0, mImageHeight - 1, 0, 0);
171  mImageImport->SetDataExtentToWholeExtent();
172 
173  mImageImport->SetImportVoidPointer(buffer);
174  mImageImport->Update();
175  mImportInitialized = true;
176 
177  emit newFrame();
178 }
179 
184 void VideoSourceSHM::connectServer(const QString& key)
185 {
186  mConnected = mSource.attach(key);
187 
188  if (mConnected)
189  {
190  connect(mPollTimer, SIGNAL(timeout()), this, SLOT(serverPollSlot()));
191  serverPollSlot(); // Pull in a new frame here, even if we may no be started yet to initialize the image import
192  }
193  if (mStartWhenConnected)
194  {
195  start();
196  }
197 }
198 
204 {
205  stop();
206 
207  if (mConnected)
208  {
209  disconnect(mPollTimer, SIGNAL(timeout()), this, SLOT(serverPollSlot()));
210  mSource.release();
211  }
212 
213  mSource.detach();
214  mConnected = false;
215 }
216 
220 void VideoSourceSHM::serverPollSlot()
221 {
222  this->update();
223 }
224 
225 void VideoSourceSHM::setResolution(double resolution)
226 {
227  mImageImport->SetDataSpacing(resolution, resolution, 1);
228 }
229 } // 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)