NorMIT-nav  22.09
An IGT application
cxVideoImplService.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 #include "cxVideoImplService.h"
13 
14 #include <ctkPluginContext.h>
15 #include "boost/bind.hpp"
16 #include "cxData.h"
17 
19 
21 #include "cxVideoConnection.h"
22 #include "cxBasicVideoSource.h"
23 #include "cxTypeConversions.h"
24 #include "cxTrackingService.h"
25 #include "cxVideoServiceBackend.h"
26 
27 #include "cxLogger.h"
28 #include "cxTrackingServiceProxy.h"
30 #include "cxFileHelpers.h"
31 #include "cxSpaceProviderImpl.h"
32 #include "cxVideoServiceBackend.h"
33 #include "cxStreamerService.h"
34 #include "cxStreamerServiceProxy.h"
35 #include "cxStreamerServiceNull.h"
36 #include "cxNullDeleter.h"
38 
39 namespace cx
40 {
41 
42 VideoImplService::VideoImplService(ctkPluginContext *context) :
43  mContext(context )
44 {
45  VideoServiceBackendPtr videoBackend;
46 
49  SpaceProviderPtr spaceProvider;
50  spaceProvider.reset(new cx::SpaceProviderImpl(tracking, pasm));
52  mBackend = VideoServiceBackend::create(pasm,tracking, spaceProvider, filemanager, context);
53 
54  mEmptyVideoSource.reset(new BasicVideoSource());
55  mVideoConnection.reset(new VideoConnection(mBackend));
56  mActiveVideoSource = mEmptyVideoSource;
57 
58  connect(mVideoConnection.get(), &VideoConnection::connected, this, &VideoImplService::autoSelectActiveVideoSource);
59  connect(mVideoConnection.get(), &VideoConnection::videoSourcesChanged, this, &VideoImplService::autoSelectActiveVideoSource);
60  connect(mVideoConnection.get(), &VideoConnection::fps, this, &VideoImplService::fpsSlot);
61  connect(mBackend->tracking().get(), &TrackingService::activeToolChanged, this, &VideoImplService::autoSelectActiveVideoSource);
62  connect(mVideoConnection.get(), &VideoConnection::connected, this, &VideoImplService::connected);
63 
64  this->initServiceListener();
65 }
66 
68 {
69  // Disconnect before deleting videoconnection:
70  // The VideoConnection might emit events AND call processevents, causing
71  // recursive calls back to this during deletion.
72  disconnect(mVideoConnection.get(), &VideoConnection::connected, this, &VideoImplService::autoSelectActiveVideoSource);
73  disconnect(mVideoConnection.get(), &VideoConnection::videoSourcesChanged, this, &VideoImplService::autoSelectActiveVideoSource);
74  disconnect(mVideoConnection.get(), &VideoConnection::fps, this, &VideoImplService::fpsSlot);
75  disconnect(mBackend->tracking().get(), &TrackingService::activeToolChanged, this, &VideoImplService::autoSelectActiveVideoSource);
76  disconnect(mVideoConnection.get(), &VideoConnection::connected, this, &VideoImplService::connected);
77 
78  mVideoConnection.reset();
79 }
80 
82 {
83  QList<StreamerServicePtr> services = this->getStreamerServices();
84  foreach(StreamerServicePtr service, services)
85  {
86  if (service->getType()==uid)
87  {
88  return service;
89  }
90  }
92 }
93 
94 QList<StreamerServicePtr> VideoImplService::getStreamerServices()
95 {
96  QList<StreamerServicePtr> retval;
97  QList<StreamerService *> services = mStreamerServiceListener->getServices();
98  foreach(StreamerService* service, services)
99  {
100  if(service)
101  {
102  StreamerServicePtr temp(new StreamerServiceProxy(mBackend->mContext, service->getName()), null_deleter());
103  retval.append(temp);
104  }
105  }
106  return retval;
107 }
108 
110 {
111  return false;
112 }
113 
114 void VideoImplService::autoSelectActiveVideoSource()
115 {
116  VideoSourcePtr suggestion = this->getGuessForActiveVideoSource(mActiveVideoSource);
117  this->setActiveVideoSource(suggestion->getUid());
118 }
119 
121 {
122  mActiveVideoSource = mEmptyVideoSource;
123 
124  std::vector<VideoSourcePtr> sources = this->getVideoSources();
125  for (unsigned i=0; i<sources.size(); ++i)
126  if (sources[i]->getUid()==uid)
127  mActiveVideoSource = sources[i];
128 
129  // set active stream in all probes if stream is present:
130  TrackingService::ToolMap tools = mBackend->tracking()->getTools();
131  for (TrackingService::ToolMap::iterator iter=tools.begin(); iter!=tools.end(); ++iter)
132  {
133  ProbePtr probe = iter->second->getProbe();
134  if (!probe)
135  continue;
136  if (!probe->getAvailableVideoSources().count(uid)){
137  report("No active streams");
138  continue;
139  }
140  probe->setActiveStream(uid);
141  }
142 
144 }
145 
146 VideoSourcePtr VideoImplService::getGuessForActiveVideoSource(VideoSourcePtr old)
147 {
148 
149  if(old && old->getUid().contains("playback"))
150  return old;
151 
152  QStringList nameFilters;
153  nameFilters << "TissueAngio.fts" << "TissueFlow.fts" << "ScanConverted.fts";
154  // ask for playback stream:
155  foreach(USAcquisitionVideoPlaybackPtr uSAcquisitionVideoPlayback,mUSAcquisitionVideoPlaybacks)
156  {
157  if (uSAcquisitionVideoPlayback->isActive() && nameFilters.contains(uSAcquisitionVideoPlayback->getType()) )
158  return uSAcquisitionVideoPlayback->getVideoSource();
159  }
160 
161  // ask for playback stream:
162  foreach(USAcquisitionVideoPlaybackPtr uSAcquisitionVideoPlayback,mUSAcquisitionVideoPlaybacks)
163  {
164  if (uSAcquisitionVideoPlayback->isActive())
165  return uSAcquisitionVideoPlayback->getVideoSource();
166  }
167 
168  // ask for active stream in first probe:
169  ToolPtr tool = mBackend->tracking()->getFirstProbe();
170  if (tool && tool->getProbe() && tool->getProbe()->getRTSource())
171  {
172  // keep existing if present
173  if (old)
174  {
175  if (tool->getProbe()->getAvailableVideoSources().count(old->getUid()))
176  return old;
177  }
178 
179  return tool->getProbe()->getRTSource();
180  }
181 
182  std::vector<VideoSourcePtr> allSources = this->getVideoSources();
183  // keep existing if present
184  if (old)
185  {
186  if (std::count(allSources.begin(), allSources.end(), old))
187  return old;
188  }
189  // ask for anything
190  if (!allSources.empty())
191  return allSources.front();
192 
193  // give up: return empty
194  return mEmptyVideoSource;
195 }
196 
198 {
199  return mActiveVideoSource;
200 }
201 
203 {
204 
205  QStringList res = getAbsolutePathToFiles( mBackend->getDataManager()->getActivePatientFolder() + "/US_Acq/",QStringList("*.fts"), true);
206  QSet<QString> types;
207  foreach (const QString &acq, res)
208  {
209  types.insert(acq.split("_").back());
210  }
211  USAcquisitionVideoPlaybackPtr tempUSAcquisitionVideoPlayback;
212  foreach(const QString type, types.toList() ){
213 
214  tempUSAcquisitionVideoPlayback.reset(new USAcquisitionVideoPlayback(mBackend,type));
215  mUSAcquisitionVideoPlaybacks.push_back(tempUSAcquisitionVideoPlayback );
216 
217 
218  mUSAcquisitionVideoPlaybacks.back()->setTime(controller);
219 
220  VideoSourcePtr playbackSource = mUSAcquisitionVideoPlaybacks.back()->getVideoSource();
221  TrackingService::ToolMap tools = mBackend->tracking()->getTools();
222  for (TrackingService::ToolMap::iterator iter=tools.begin(); iter!=tools.end(); ++iter)
223  {
224  ProbePtr probe = iter->second->getProbe();
225  if (!probe)
226  continue;
227  if (mUSAcquisitionVideoPlaybacks.back()->isActive())
228  probe->setRTSource(playbackSource);
229  else
230  probe->removeRTSource(playbackSource);
231  }
232  mUSAcquisitionVideoPlaybacks.back()->setRoot(mBackend->getDataManager()->getActivePatientFolder() + "/US_Acq/");
233  }
234  this->autoSelectActiveVideoSource();
235 }
236 
237 std::vector<VideoSourcePtr> VideoImplService::getVideoSources()
238 {
239  std::vector<VideoSourcePtr> retval = mVideoConnection->getVideoSources();
240  foreach(USAcquisitionVideoPlaybackPtr uSAcquisitionVideoPlayback,mUSAcquisitionVideoPlaybacks)
241  {
242  if (uSAcquisitionVideoPlayback->isActive())
243  retval.push_back(uSAcquisitionVideoPlayback->getVideoSource());
244  }
245  return retval;
246 }
247 
248 void VideoImplService::fpsSlot(QString source, int val)
249 {
250  if (source==mActiveVideoSource->getUid())
251  emit fps(val);
252 }
253 
255 {
256  if (mVideoConnection->isConnected())
257  return;
258 
259  //StreamerService* service = this->getStreamerService(mConnectionMethod);
260  StreamerServicePtr service = this->getStreamerService(mConnectionMethod);
261  if (!service)
262  {
263  reportError(QString("Found no streamer for method [%1]").arg(mConnectionMethod));
264  return;
265  }
266 
267  mVideoConnection->runDirectLinkClient(service);
268 }
269 
271 {
272  mVideoConnection->disconnectServer();
273 }
274 
276 {
277  return mVideoConnection->isConnected();
278 }
279 
281 {
282  return mConnectionMethod;
283 }
284 
285 void VideoImplService::setConnectionMethod(QString connectionMethod)
286 {
287  if (mConnectionMethod == connectionMethod)
288  return;
289 
290  if(connectionMethod.isEmpty())
291  {
292  reportWarning("Trying to set connection method to empty string");
293  return;
294  }
295 
296  mConnectionMethod = connectionMethod;
298 }
299 
300 std::vector<TimelineEvent> VideoImplService::getPlaybackEvents()
301 {
302  std::vector<TimelineEvent> retval;
303  foreach(USAcquisitionVideoPlaybackPtr uSAcquisitionVideoPlayback,mUSAcquisitionVideoPlaybacks)
304  {
305  std::vector<TimelineEvent> events = uSAcquisitionVideoPlayback->getEvents();
306  retval.reserve(retval.size() + events.size());
307  retval.insert( retval.end(), events.begin(), events.end() );
308  }
309 
310  return retval;
311 }
312 
313 void VideoImplService::initServiceListener()
314 {
315  mStreamerServiceListener.reset(new ServiceTrackerListener<StreamerService>(
316  mBackend->mContext,
317  boost::bind(&VideoImplService::onStreamerServiceAdded, this, _1),
318  boost::function<void (StreamerService*)>(),
319  boost::bind(&VideoImplService::onStreamerServiceRemoved, this, _1)
320  ));
321  mStreamerServiceListener->open();
322 
323 }
324 
325 void VideoImplService::onStreamerServiceAdded(StreamerService* service)
326 {
327  if (mConnectionMethod.isEmpty())
328  mConnectionMethod = service->getType();
329 
330  emit StreamerServiceAdded(service);
331 }
332 
333 void VideoImplService::onStreamerServiceRemoved(StreamerService *service)
334 {
335  emit StreamerServiceRemoved(service);
336 }
337 
338 
339 } /* namespace cx */
cx::null_deleter
Definition: cxNullDeleter.h:8
cxLogger.h
cx::VideoService::connected
void connected(bool on)
cx::PatientModelServiceProxy::create
static PatientModelServicePtr create(ctkPluginContext *pluginContext)
Definition: cxPatientModelServiceProxy.cpp:24
cx::VideoService::connectionMethodChanged
void connectionMethodChanged()
cx::VideoService::StreamerServiceAdded
void StreamerServiceAdded(StreamerService *service)
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::ServiceTrackerListener
Helper class for listening to services being added, modified and removed.
Definition: cxServiceTrackerListener.h:53
cx::report
void report(QString msg)
Definition: cxLogger.cpp:69
cx::VideoImplService::setPlaybackMode
virtual void setPlaybackMode(PlaybackTimePtr controller)
Definition: cxVideoImplService.cpp:202
cx::VideoImplService::getActiveVideoSource
virtual VideoSourcePtr getActiveVideoSource()
Definition: cxVideoImplService.cpp:197
cx::USAcquisitionVideoPlayback
Handler for playback of US image data from a US recording session.
Definition: cxPlaybackUSAcquisitionVideo.h:44
cx::VideoImplService::isNull
virtual bool isNull()
Definition: cxVideoImplService.cpp:109
cxVideoServiceBackend.h
cx::VideoConnection::fps
void fps(QString source, int fps)
cx::VideoImplService::getVideoSources
virtual std::vector< VideoSourcePtr > getVideoSources()
Definition: cxVideoImplService.cpp:237
cx::VideoImplService::VideoImplService
VideoImplService(ctkPluginContext *context)
Definition: cxVideoImplService.cpp:42
cx::FileManagerServicePtr
boost::shared_ptr< class FileManagerService > FileManagerServicePtr
Definition: cxLogicManager.h:31
cx::StreamerService::getName
virtual QString getName()=0
cxStreamerService.h
cx::PlaybackTimePtr
boost::shared_ptr< class PlaybackTime > PlaybackTimePtr
Definition: cxTrackingSystemBronchoscopyService.h:42
cx::VideoSourcePtr
boost::shared_ptr< class VideoSource > VideoSourcePtr
Definition: cxForwardDeclarations.h:109
cxPlaybackUSAcquisitionVideo.h
cxData.h
cx::VideoImplService::openConnection
virtual void openConnection()
Definition: cxVideoImplService.cpp:254
cxVideoConnection.h
cx::VideoImplService::getPlaybackEvents
virtual std::vector< TimelineEvent > getPlaybackEvents()
Definition: cxVideoImplService.cpp:300
cx::VideoService::fps
void fps(int)
cx::VideoServiceBackend::create
static VideoServiceBackendPtr create(PatientModelServicePtr dataManager, TrackingServicePtr trackingService, SpaceProviderPtr spaceProvider, FileManagerServicePtr filemanager, ctkPluginContext *context)
Definition: cxVideoServiceBackend.cpp:21
cxBasicVideoSource.h
cxStreamerServiceProxy.h
cx::VideoImplService::setActiveVideoSource
virtual void setActiveVideoSource(QString uid)
Definition: cxVideoImplService.cpp:120
cxNullDeleter.h
cx::StreamerServicePtr
boost::shared_ptr< class StreamerService > StreamerServicePtr
Definition: cxPlusConnectWidget.h:26
cxPatientModelServiceProxy.h
cxSpaceProviderImpl.h
cx::TrackingServicePtr
boost::shared_ptr< class TrackingService > TrackingServicePtr
Definition: cxToolFilterWidget.h:27
cx::PatientModelServicePtr
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
Definition: cxLogicManager.h:25
cx::VideoImplService::getStreamerServices
virtual QList< StreamerServicePtr > getStreamerServices()
Definition: cxVideoImplService.cpp:94
cxTypeConversions.h
cx::BasicVideoSource
VideoSource controlled by a vtkImageData.
Definition: cxBasicVideoSource.h:44
cx::TrackingService::activeToolChanged
void activeToolChanged(const QString &uId)
cx::VideoImplService::getConnectionMethod
virtual QString getConnectionMethod()
Definition: cxVideoImplService.cpp:280
cx::FileManagerServiceProxy::create
static FileManagerServicePtr create(ctkPluginContext *context)
Definition: cxFileManagerServiceProxy.cpp:19
cx::VideoImplService::isConnected
virtual bool isConnected() const
Definition: cxVideoImplService.cpp:275
cxRegistrationTransform.h
cx::SpaceProviderImpl
Definition: cxSpaceProviderImpl.h:29
cx::StreamerServiceProxy
Proxy for StreamerServices.
Definition: cxStreamerServiceProxy.h:27
cxFileHelpers.h
cx::VideoImplService::setConnectionMethod
virtual void setConnectionMethod(QString connectionMethod)
Definition: cxVideoImplService.cpp:285
cx::VideoConnection::connected
bool connected(bool)
cxStreamerServiceNull.h
cx::ToolPtr
boost::shared_ptr< class Tool > ToolPtr
Definition: cxVideoConnectionWidget.h:43
cx::TrackingServiceProxy::create
static TrackingServicePtr create(ctkPluginContext *pluginContext)
Definition: cxTrackingServiceProxy.cpp:22
cx::VideoImplService::getStreamerService
virtual StreamerServicePtr getStreamerService(QString uid)
Definition: cxVideoImplService.cpp:81
cxTrackingServiceProxy.h
cx::USAcquisitionVideoPlaybackPtr
boost::shared_ptr< USAcquisitionVideoPlayback > USAcquisitionVideoPlaybackPtr
Definition: cxPlaybackUSAcquisitionVideo.h:82
cx::StreamerServiceNull
Null implementation of the StreamerService.
Definition: cxStreamerServiceNull.h:27
cx::VideoService::activeVideoSourceChanged
void activeVideoSourceChanged()
cx::VideoImplService::closeConnection
virtual void closeConnection()
Definition: cxVideoImplService.cpp:270
cxFileManagerServiceProxy.h
cx::getAbsolutePathToFiles
QStringList getAbsolutePathToFiles(QString path, QStringList nameFilters, bool includeSubDirs)
Definition: cxFileHelpers.cpp:101
cx::reportError
void reportError(QString msg)
Definition: cxLogger.cpp:71
cxTrackingService.h
cx::SpaceProviderPtr
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
Definition: cxLogicManager.h:23
cx::ProbePtr
boost::shared_ptr< Probe > ProbePtr
Definition: cxProbe.h:72
cx::reportWarning
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
cx::TrackingService::ToolMap
std::map< QString, ToolPtr > ToolMap
Definition: cxTrackingService.h:54
cx::VideoConnection::videoSourcesChanged
void videoSourcesChanged()
cx::VideoService::StreamerServiceRemoved
void StreamerServiceRemoved(StreamerService *service)
cx::VideoImplService::~VideoImplService
virtual ~VideoImplService()
Definition: cxVideoImplService.cpp:67
cx::VideoConnection
Represent one video grabber connection.
Definition: cxVideoConnection.h:54
cxVideoImplService.h
cx::StreamerService
Abstract class. Interface to Streamers.
Definition: cxStreamerService.h:42
cx::VideoServiceBackendPtr
boost::shared_ptr< class VideoServiceBackend > VideoServiceBackendPtr
Definition: cxPlaybackUSAcquisitionVideo.h:28