65 mStreamerInterface(streamerInterface)
67 this->setObjectName(
"imagereceiver worker");
68 mGeneratingTimeCalibration =
false;
69 mLastReferenceTimestampDiff = 0.0;
70 mLastTimeStamps.reserve(20);
73 void ImageReceiverThread::initialize()
76 QDomElement element = xmlFile.
getElement(
"video");
77 mImageStreamer = mStreamerInterface->createStreamer(element);
78 report(QString(
"Starting streamer: [%1]").arg(mImageStreamer->getType()));
85 mSender.reset(
new DirectlyLinkedSender());
87 connect(mSender.get(), SIGNAL(newImage()),
this, SLOT(addImageToQueueSlot()), Qt::DirectConnection);
88 connect(mSender.get(), SIGNAL(newUSStatus()),
this, SLOT(addSonixStatusToQueueSlot()), Qt::DirectConnection);
90 if(!mImageStreamer->startStreaming(mSender))
98 void ImageReceiverThread::shutdown()
102 report(QString(
"Stopping streamer: [%1]...").arg(mImageStreamer->getType()));
103 mImageStreamer->stopStreaming();
104 report(QString(
"Stopped streamer: [%1]").arg(mImageStreamer->getType()));
105 mImageStreamer.reset();
111 void ImageReceiverThread::addImageToQueueSlot()
116 void ImageReceiverThread::addSonixStatusToQueueSlot()
124 this->reportFPS(imgMsg->getUid());
128 bool needToCalibrateMsgTimeStamp = this->imageComesFromSonix(imgMsg);
132 if (needToCalibrateMsgTimeStamp)
135 QMutexLocker sentry(&mImageMutex);
136 mMutexedImageMessageQueue.push_back(imgMsg);
144 QMutexLocker sentry(&mSonixStatusMutex);
145 mMutexedSonixStatusMessageQueue.push_back(msg);
152 QMutexLocker sentry(&mImageMutex);
153 if (mMutexedImageMessageQueue.empty())
155 ImagePtr retval = mMutexedImageMessageQueue.front();
156 mMutexedImageMessageQueue.pop_front();
168 QMutexLocker sentry(&mSonixStatusMutex);
169 if (mMutexedSonixStatusMessageQueue.empty())
172 mMutexedSonixStatusMessageQueue.pop_front();
178 QDateTime timestamp_dt = imgMsg->getAcquisitionTime();
179 double timestamp_ms = timestamp_dt.toMSecsSinceEpoch();
181 if (
similar(mLastReferenceTimestampDiff, 0.0, 0.000001))
182 mLastReferenceTimestampDiff = timestamp_dt.msecsTo(QDateTime::currentDateTime());
185 if(mLastSyncTime.isNull() || ( mLastSyncTime.msecsTo(QDateTime::currentDateTime()) > 2000) )
186 mGeneratingTimeCalibration =
true;
188 if(mGeneratingTimeCalibration)
189 mLastTimeStamps.push_back(timestamp_dt.msecsTo(QDateTime::currentDateTime()));
192 if(mLastTimeStamps.size() >= 20)
194 std::sort(mLastTimeStamps.begin(), mLastTimeStamps.end(),
AbsDoubleLess(mLastReferenceTimestampDiff));
196 mLastTimeStamps.resize(15);
199 for (std::vector<double>::const_iterator citer = mLastTimeStamps.begin(); citer != mLastTimeStamps.end(); ++citer)
203 mLastReferenceTimestampDiff = sumTimes / 15.0;
206 mLastTimeStamps.clear();
207 mLastSyncTime = QDateTime::currentDateTime();
208 mGeneratingTimeCalibration =
false;
210 imgMsg->setAcquisitionTime(QDateTime::fromMSecsSinceEpoch(timestamp_ms + mLastReferenceTimestampDiff));
214 void ImageReceiverThread::reportFPS(QString streamUid)
217 if (!mFPSTimer.count(streamUid))
220 mFPSTimer[streamUid]->reset(timeout);
226 if (logger->intervalPassed())
228 emit
fps(streamUid, logger->getFPS());
229 logger->reset(timeout);
233 bool ImageReceiverThread::imageComesFromSonix(
ImagePtr imgMsg)
235 return imgMsg->getUid().contains(
"Sonix", Qt::CaseInsensitive);
240 if (!mStreamerInterface)
242 return mStreamerInterface->getName();
cxResource_EXPORT ProfilePtr profile()
void fps(QString, double)
boost::shared_ptr< class Image > ImagePtr
QDomElement getElement()
return the current element
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
virtual ImagePtr getLastImageMessage()
AbsDoubleLess(double center)
void sonixStatusReceived()
boost::shared_ptr< class CyclicActionLogger > CyclicActionLoggerPtr
void addSonixStatusToQueue(ProbeDefinitionPtr msg)
add the message to a thread-safe queue
virtual QString hostDescription() const
ImageReceiverThread(StreamerServicePtr streamerInterface, QObject *parent=NULL)
void calibrateTimeStamp(ImagePtr imgMsg)
Calibrate the time stamps of the incoming message based on the computer clock. Calibration is based o...
virtual ProbeDefinitionPtr getLastSonixStatusMessage()
boost::shared_ptr< class StreamerService > StreamerServicePtr
bool operator()(const double &d1, const double &d2)
void addImageToQueue(ImagePtr imgMsg)
boost::shared_ptr< class ProbeDefinition > ProbeDefinitionPtr
Helper class for xml files used to store ssc/cx data.