34 #include <vtkDataSetMapper.h>
35 #include <vtkImageFlip.h>
44 #include <QApplication>
56 mUnsusedProbeDataVector.clear();
59 connect(mBackend->getToolManager().get(), SIGNAL(activeToolChanged(QString)),
this, SLOT(connectVideoToProbe()));
67 void VideoConnection::fpsSlot(QString source,
double fpsNumber)
70 emit
fps(source, fpsNumber);
75 return mClient && mConnected;
78 void VideoConnection::connectedSlot(
bool on)
83 this->startAllSources();
87 std::cout <<
"EMIT CONNECTED " << on << std::endl;
93 return mStreamerInterface;
101 this->runClient(imageReceiverThread);
106 class EventProcessingThread :
public QThread
111 qApp->processEvents();
120 std::cout <<
"client already exist - returning" << std::endl;
124 connect(mClient.get(), SIGNAL(imageReceived()),
this, SLOT(imageReceivedSlot()));
125 connect(mClient.get(), SIGNAL(sonixStatusReceived()),
this, SLOT(statusReceivedSlot()));
126 connect(mClient.get(), SIGNAL(
fps(QString,
double)),
this, SLOT(fpsSlot(QString,
double)));
127 connect(mClient.get(), SIGNAL(
connected(
bool)),
this, SLOT(connectedSlot(
bool)));
129 mThread.reset(
new EventProcessingThread);
130 mThread->setObjectName(
"org.custusx.core.video.imagereceiver");
131 mClient->moveToThread(mThread.get());
133 connect(mThread.get(), &QThread::finished,
this, &VideoConnection::clientFinishedSlot);
134 QMetaObject::invokeMethod(mClient.get(),
"initialize", Qt::QueuedConnection);
139 void VideoConnection::imageReceivedSlot()
143 this->updateImage(mClient->getLastImageMessage());
146 void VideoConnection::statusReceivedSlot()
150 this->updateStatus(mClient->getLastSonixStatusMessage());
153 void VideoConnection::stopClient()
160 QMetaObject::invokeMethod(mClient.get(),
"shutdown", Qt::QueuedConnection);
161 QString hostdescription = mClient->hostDescription();
166 for (
unsigned i=0; i<timeout/interval; ++i)
168 if (!thread->isRunning())
170 thread->wait(interval);
171 qApp->processEvents();
173 if (thread->isRunning())
175 reportWarning(QString(
"Video Client [%1] did not quit normally - attempting to terminate.").arg(hostdescription));
178 reportWarning(QString(
"Video Client [%1] did not quit normally - terminated.").arg(hostdescription));
188 this->cleanupAfterDisconnectServer();
191 void VideoConnection::cleanupAfterDisconnectServer()
198 this->stopAllSources();
200 for (
unsigned i=0; i<mSources.size(); ++i)
203 ToolPtr tool = mBackend->getToolManager()->getFirstProbe();
204 if (tool && tool->getProbe())
205 this->removeSourceFromProbe(tool);
208 mStreamerInterface.reset();
212 void VideoConnection::clientFinishedSlot()
217 this->cleanupAfterDisconnectServer();
220 void VideoConnection::useUnusedProbeDataSlot()
223 for (std::vector<ProbeDefinitionPtr>::const_iterator citer = mUnsusedProbeDataVector.begin(); citer != mUnsusedProbeDataVector.end(); ++citer)
224 this->updateStatus(*citer);
225 mUnsusedProbeDataVector.clear();
228 void VideoConnection::resetProbe()
230 ToolPtr tool = mBackend->getToolManager()->getFirstProbe();
231 if (!tool || !tool->getProbe())
236 ProbeDefinition data = probe->getProbeData();
237 data.setUseDigitalVideo(
false);
238 probe->setProbeSector(data);
248 ToolPtr tool = mBackend->getToolManager()->getFirstProbe();
249 if (!tool || !tool->getProbe())
252 if (mUnsusedProbeDataVector.empty())
254 mUnsusedProbeDataVector.push_back(msg);
262 ProbeDefinition data = probe->getProbeData();
264 data.setUid(msg->getUid());
265 data.setType(msg->getType());
266 data.setSector(msg->getDepthStart(), msg->getDepthEnd(), msg->getWidth());
267 data.setOrigin_p(msg->getOrigin_p());
268 data.setSize(msg->getSize());
269 data.setSpacing(msg->getSpacing());
270 data.setClipRect_p(msg->getClipRect_p());
271 data.setUseDigitalVideo(
true);
273 probe->setProbeSector(data);
274 probe->setActiveStream(msg->getUid());
277 void VideoConnection::startAllSources()
279 for (
unsigned i=0; i<mSources.size(); ++i)
280 mSources[i]->start();
283 void VideoConnection::stopAllSources()
285 for (
unsigned i=0; i<mSources.size(); ++i)
289 void VideoConnection::removeSourceFromProbe(
ToolPtr tool)
292 for (
unsigned i=0; i<mSources.size(); ++i)
293 probe->removeRTSource(mSources[i]);
296 void VideoConnection::updateImage(
ImagePtr message)
301 for (
unsigned i=0; i<mSources.size(); ++i)
303 if (message && message->getUid() == mSources[i]->getUid())
304 source = mSources[i];
307 bool newSource =
false;
311 source.reset(
new BasicVideoSource());
312 mSources.push_back(source);
317 source->setInput(message);
319 QString info = mClient->hostDescription() +
" - " + QString::number(mFPS,
'f', 1) +
" fps";
320 source->setInfoString(info);
324 this->connectVideoToProbe();
331 std::vector<VideoSourcePtr> retval;
332 std::copy(mSources.begin(), mSources.end(), std::back_inserter(retval));
343 void VideoConnection::connectVideoToProbe()
345 ToolPtr tool = mBackend->getToolManager()->getFirstProbe();
353 for (
unsigned i=0; i<mSources.size(); ++i)
354 probe->setRTSource(mSources[i]);
boost::shared_ptr< BasicVideoSource > BasicVideoSourcePtr
boost::shared_ptr< class Image > ImagePtr
VideoConnection(VideoServiceBackendPtr backend)
void videoSourcesChanged()
std::vector< VideoSourcePtr > getVideoSources()
boost::shared_ptr< class VideoServiceBackend > VideoServiceBackendPtr
boost::shared_ptr< Probe > ProbePtr
boost::shared_ptr< QThread > QThreadPtr
void reportWarning(QString msg)
Base class for receiving images from a video stream.
virtual bool isConnected() const
vtkSmartPointer< vtkDataSetMapper > vtkDataSetMapperPtr
virtual ~VideoConnection()
vtkSmartPointer< vtkImageFlip > vtkImageFlipPtr
boost::shared_ptr< class StreamerService > StreamerServicePtr
void runDirectLinkClient(StreamerService *service)
Abstract class. Interface to Streamers.
boost::shared_ptr< class ImageReceiverThread > ImageReceiverThreadPtr
boost::shared_ptr< class ProbeDefinition > ProbeDefinitionPtr
void fps(QString source, int fps)
boost::shared_ptr< class Tool > ToolPtr