65 mStreamerInterface(streamerInterface)
67 this->setObjectName(
"imagereceiver worker");
68 mGeneratingTimeCalibration =
false;
69 mLastReferenceTimestampDiff = 0.0;
70 mLastTimeStamps.reserve(20);
75 if (!this->attemptInitialize())
78 mImageStreamer.reset();
85 bool ImageReceiverThread::attemptInitialize()
88 QDomElement element = xmlFile.
getElement(
"video");
89 mImageStreamer = mStreamerInterface->createStreamer(element);
90 report(QString(
"Starting streamer: [%1]").arg(mImageStreamer->getType()));
96 mSender.reset(
new DirectlyLinkedSender());
98 connect(mSender.get(), SIGNAL(newImage()),
this, SLOT(addImageToQueueSlot()), Qt::DirectConnection);
99 connect(mSender.get(), SIGNAL(newUSStatus()),
this, SLOT(addSonixStatusToQueueSlot()), Qt::DirectConnection);
101 if(!mImageStreamer->startStreaming(mSender))
113 report(QString(
"Stopping streamer: [%1]...").arg(mImageStreamer->getType()));
114 mImageStreamer->stopStreaming();
115 report(QString(
"Stopped streamer: [%1]").arg(mImageStreamer->getType()));
116 mImageStreamer.reset();
123 void ImageReceiverThread::addImageToQueueSlot()
128 void ImageReceiverThread::addSonixStatusToQueueSlot()
136 this->reportFPS(imgMsg->getUid());
140 bool needToCalibrateMsgTimeStamp = this->imageComesFromSonix(imgMsg);
144 if (needToCalibrateMsgTimeStamp)
147 QMutexLocker sentry(&mImageMutex);
148 mMutexedImageMessageQueue.push_back(imgMsg);
156 QMutexLocker sentry(&mSonixStatusMutex);
157 mMutexedSonixStatusMessageQueue.push_back(msg);
164 QMutexLocker sentry(&mImageMutex);
165 if (mMutexedImageMessageQueue.empty())
167 ImagePtr retval = mMutexedImageMessageQueue.front();
168 mMutexedImageMessageQueue.pop_front();
180 QMutexLocker sentry(&mSonixStatusMutex);
181 if (mMutexedSonixStatusMessageQueue.empty())
184 mMutexedSonixStatusMessageQueue.pop_front();
190 QDateTime timestamp_dt = imgMsg->getAcquisitionTime();
191 double timestamp_ms = timestamp_dt.toMSecsSinceEpoch();
193 if (
similar(mLastReferenceTimestampDiff, 0.0, 0.000001))
194 mLastReferenceTimestampDiff = timestamp_dt.msecsTo(QDateTime::currentDateTime());
197 if(mLastSyncTime.isNull() || ( mLastSyncTime.msecsTo(QDateTime::currentDateTime()) > 2000) )
198 mGeneratingTimeCalibration =
true;
200 if(mGeneratingTimeCalibration)
201 mLastTimeStamps.push_back(timestamp_dt.msecsTo(QDateTime::currentDateTime()));
204 if(mLastTimeStamps.size() >= 20)
206 std::sort(mLastTimeStamps.begin(), mLastTimeStamps.end(),
AbsDoubleLess(mLastReferenceTimestampDiff));
208 mLastTimeStamps.resize(15);
211 for (std::vector<double>::const_iterator citer = mLastTimeStamps.begin(); citer != mLastTimeStamps.end(); ++citer)
215 mLastReferenceTimestampDiff = sumTimes / 15.0;
218 mLastTimeStamps.clear();
219 mLastSyncTime = QDateTime::currentDateTime();
220 mGeneratingTimeCalibration =
false;
222 imgMsg->setAcquisitionTime(QDateTime::fromMSecsSinceEpoch(timestamp_ms + mLastReferenceTimestampDiff));
226 void ImageReceiverThread::reportFPS(QString streamUid)
229 if (!mFPSTimer.count(streamUid))
232 mFPSTimer[streamUid]->reset(timeout);
238 if (logger->intervalPassed())
240 emit
fps(streamUid, logger->getFPS());
241 logger->reset(timeout);
245 bool ImageReceiverThread::imageComesFromSonix(
ImagePtr imgMsg)
247 return imgMsg->getUid().contains(
"Sonix", Qt::CaseInsensitive);
252 if (!mStreamerInterface)
254 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.