CustusX  15.3.4-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 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 #include "cxVideoImplService.h"
34 
35 #include <ctkPluginContext.h>
36 #include "boost/bind.hpp"
37 #include "cxData.h"
38 
40 
42 #include "cxVideoConnection.h"
43 #include "cxBasicVideoSource.h"
44 #include "cxTypeConversions.h"
45 #include "cxTrackingService.h"
46 #include "cxVideoServiceBackend.h"
47 
48 #include "cxLogger.h"
49 #include "cxTrackingServiceProxy.h"
51 #include "cxSpaceProviderImpl.h"
52 #include "cxVideoServiceBackend.h"
53 #include "cxStreamerService.h"
54 
55 namespace cx
56 {
57 
58 VideoImplService::VideoImplService(ctkPluginContext *context) :
59  mContext(context )
60 {
61  VideoServiceBackendPtr videoBackend;
62 
66  spaceProvider.reset(new cx::SpaceProviderImpl(tracking, pasm));
67  mBackend = VideoServiceBackend::create(pasm,tracking, spaceProvider, context);
68 
69  mEmptyVideoSource.reset(new BasicVideoSource());
70  mVideoConnection.reset(new VideoConnection(mBackend));
71  mActiveVideoSource = mEmptyVideoSource;
72  mUSAcquisitionVideoPlayback.reset(new USAcquisitionVideoPlayback(mBackend));
73 
74  connect(mVideoConnection.get(), &VideoConnection::connected, this, &VideoImplService::autoSelectActiveVideoSource);
75  connect(mVideoConnection.get(), &VideoConnection::videoSourcesChanged, this, &VideoImplService::autoSelectActiveVideoSource);
76  connect(mVideoConnection.get(), &VideoConnection::fps, this, &VideoImplService::fpsSlot);
77  connect(mBackend->getToolManager().get(), &TrackingService::activeToolChanged, this, &VideoImplService::autoSelectActiveVideoSource);
78  connect(mVideoConnection.get(), &VideoConnection::connected, this, &VideoImplService::connected);
79 
80  this->initServiceListener();
81 }
82 
84 {
85  mVideoConnection.reset();
86 }
87 
89 {
90  return mStreamerServiceListener->getService(service);
91 }
92 
93 QList<StreamerService *> VideoImplService::getStreamerServices()
94 {
95  return mStreamerServiceListener->getServices();
96 }
97 
99 {
100  return false;
101 }
102 
103 void VideoImplService::autoSelectActiveVideoSource()
104 {
105  VideoSourcePtr suggestion = this->getGuessForActiveVideoSource(mActiveVideoSource);
106  this->setActiveVideoSource(suggestion->getUid());
107 }
108 
110 {
111  mActiveVideoSource = mEmptyVideoSource;
112 
113  std::vector<VideoSourcePtr> sources = this->getVideoSources();
114  for (unsigned i=0; i<sources.size(); ++i)
115  if (sources[i]->getUid()==uid)
116  mActiveVideoSource = sources[i];
117 
118  // set active stream in all probes if stream is present:
119  TrackingService::ToolMap tools = mBackend->getToolManager()->getTools();
120  for (TrackingService::ToolMap::iterator iter=tools.begin(); iter!=tools.end(); ++iter)
121  {
122  ProbePtr probe = iter->second->getProbe();
123  if (!probe)
124  continue;
125  if (!probe->getAvailableVideoSources().count(uid))
126  continue;
127  probe->setActiveStream(uid);
128  }
129 
131 }
132 
133 VideoSourcePtr VideoImplService::getGuessForActiveVideoSource(VideoSourcePtr old)
134 {
135  // ask for playback stream:
136  if (mUSAcquisitionVideoPlayback->isActive())
137  return mUSAcquisitionVideoPlayback->getVideoSource();
138 
139  // ask for active stream in first probe:
140  ToolPtr tool = mBackend->getToolManager()->getFirstProbe();
141  if (tool && tool->getProbe() && tool->getProbe()->getRTSource())
142  {
143  // keep existing if present
144  if (old)
145  {
146  if (tool->getProbe()->getAvailableVideoSources().count(old->getUid()))
147  return old;
148  }
149 
150  return tool->getProbe()->getRTSource();
151  }
152 
153  // ask for anything
154  std::vector<VideoSourcePtr> allSources = this->getVideoSources();
155  // keep existing if present
156  if (old)
157  {
158  if (std::count(allSources.begin(), allSources.end(), old))
159  return old;
160  }
161  if (!allSources.empty())
162  return allSources.front();
163 
164  // give up: return empty
165  return mEmptyVideoSource;
166 }
167 
169 {
170  return mActiveVideoSource;
171 }
172 
174 {
175  mUSAcquisitionVideoPlayback->setTime(controller);
176  this->autoSelectActiveVideoSource();
177 
178  VideoSourcePtr playbackSource = mUSAcquisitionVideoPlayback->getVideoSource();
179  TrackingService::ToolMap tools = mBackend->getToolManager()->getTools();
180  for (TrackingService::ToolMap::iterator iter=tools.begin(); iter!=tools.end(); ++iter)
181  {
182  ProbePtr probe = iter->second->getProbe();
183  if (!probe)
184  continue;
185  if (mUSAcquisitionVideoPlayback->isActive())
186  probe->setRTSource(playbackSource);
187  else
188  probe->removeRTSource(playbackSource);
189  }
190  if (mUSAcquisitionVideoPlayback->isActive())
191  this->setActiveVideoSource(playbackSource->getUid());
192  else
193  this->autoSelectActiveVideoSource();
194 
195  mUSAcquisitionVideoPlayback->setRoot(mBackend->getDataManager()->getActivePatientFolder() + "/US_Acq/");
196 }
197 
198 std::vector<VideoSourcePtr> VideoImplService::getVideoSources()
199 {
200  std::vector<VideoSourcePtr> retval = mVideoConnection->getVideoSources();
201  if (mUSAcquisitionVideoPlayback->isActive())
202  retval.push_back(mUSAcquisitionVideoPlayback->getVideoSource());
203  return retval;
204 }
205 
206 void VideoImplService::fpsSlot(QString source, int val)
207 {
208  if (source==mActiveVideoSource->getUid())
209  emit fps(val);
210 }
211 
213 {
214  if (mVideoConnection->isConnected())
215  return;
216 
217  StreamerService* service = this->getStreamerService(mConnectionMethod);
218  if (!service)
219  {
220  reportError(QString("Found no streamer for method [%1]").arg(mConnectionMethod));
221  return;
222  }
223 
224  mVideoConnection->runDirectLinkClient(service);
225 }
226 
228 {
229  mVideoConnection->disconnectServer();
230 }
231 
233 {
234  return mVideoConnection->isConnected();
235 }
236 
238 {
239  return mConnectionMethod;
240 }
241 
242 void VideoImplService::setConnectionMethod(QString connectionMethod)
243 {
244  if (mConnectionMethod == connectionMethod)
245  return;
246 
247  if(connectionMethod.isEmpty())
248  {
249  reportWarning("Trying to set connection method to empty string");
250  return;
251  }
252 
253  mConnectionMethod = connectionMethod;
254 // emit connectionMethodChanged();
255 }
256 
257 std::vector<TimelineEvent> VideoImplService::getPlaybackEvents()
258 {
259  return mUSAcquisitionVideoPlayback->getEvents();
260 }
261 
262 void VideoImplService::initServiceListener()
263 {
264  mStreamerServiceListener.reset(new ServiceTrackerListener<StreamerService>(
265  mBackend->mContext,
266  boost::bind(&VideoImplService::onStreamerServiceAdded, this, _1),
267  boost::function<void (StreamerService*)>(),
268  boost::bind(&VideoImplService::onStreamerServiceRemoved, this, _1)
269  ));
270  mStreamerServiceListener->open();
271 
272 }
273 
274 void VideoImplService::onStreamerServiceAdded(StreamerService* service)
275 {
276  if (mConnectionMethod.isEmpty())
277  mConnectionMethod = service->getName();
278 
279  emit StreamerServiceAdded(service);
280 }
281 
282 void VideoImplService::onStreamerServiceRemoved(StreamerService *service)
283 {
284  emit StreamerServiceRemoved(service);
285 }
286 
287 
288 } /* namespace cx */
bool connected(bool)
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
virtual QString getConnectionMethod()
void reportError(QString msg)
Definition: cxLogger.cpp:92
virtual void setPlaybackMode(PlaybackTimePtr controller)
void connected(bool on)
virtual VideoSourcePtr getActiveVideoSource()
void StreamerServiceRemoved(StreamerService *service)
boost::shared_ptr< class TrackingService > TrackingServicePtr
VideoImplService(ctkPluginContext *context)
virtual std::vector< TimelineEvent > getPlaybackEvents()
void StreamerServiceAdded(StreamerService *service)
virtual bool isConnected() const
Handler for playback of US image data from a US recording session.
boost::shared_ptr< class VideoServiceBackend > VideoServiceBackendPtr
static TrackingServicePtr create(ctkPluginContext *pluginContext)
boost::shared_ptr< Probe > ProbePtr
Definition: cxProbe.h:93
static PatientModelServicePtr create(ctkPluginContext *pluginContext)
void activeToolChanged(const QString &uId)
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
boost::shared_ptr< class VideoSource > VideoSourcePtr
virtual void setConnectionMethod(QString connectionMethod)
virtual std::vector< VideoSourcePtr > getVideoSources()
VideoSource controlled by a vtkImageData.
cxLogicManager_EXPORT SpaceProviderPtr spaceProvider()
Helper class for listening to services being added, modified and removed.
std::map< QString, ToolPtr > ToolMap
static VideoServiceBackendPtr create(PatientModelServicePtr dataManager, TrackingServicePtr trackingService, SpaceProviderPtr spaceProvider, ctkPluginContext *context)
Represent one video grabber connection.
virtual StreamerService * getStreamerService(QString service)
Abstract class. Interface to Streamers.
virtual void setActiveVideoSource(QString uid)
virtual void closeConnection()
boost::shared_ptr< class PlaybackTime > PlaybackTimePtr
void activeVideoSourceChanged()
virtual QList< StreamerService * > getStreamerServices()
void fps(QString source, int fps)
boost::shared_ptr< class Tool > ToolPtr