Commit 4d97006e authored by Thiago Santini's avatar Thiago Santini

Fixes timer offset estimation and updates uvcengine/libuvc

libuvc: 69e45682220ff93abc6d675acfaa8fc6de8f5fca

uvcengine: fce1f275eb53cc87725f7232944813bc36da75cf
parent 0267349b
...@@ -9,8 +9,8 @@ FrameGrabber::FrameGrabber(QString id, int code, QObject *parent) : ...@@ -9,8 +9,8 @@ FrameGrabber::FrameGrabber(QString id, int code, QObject *parent) :
code(code), code(code),
yuvBuffer(NULL), yuvBuffer(NULL),
yuvBufferSize(0), yuvBufferSize(0),
timestampOffset(INT_MAX), timestampOffset(0),
timeoutMs(2e3) timeoutMs(2e3)
{ {
#ifdef TURBOJPEG #ifdef TURBOJPEG
tjh = tjInitDecompress(); tjh = tjInitDecompress();
...@@ -21,7 +21,7 @@ FrameGrabber::FrameGrabber(QString id, int code, QObject *parent) : ...@@ -21,7 +21,7 @@ FrameGrabber::FrameGrabber(QString id, int code, QObject *parent) :
// for the first frame, we give a bit of extra leeway becase gstreamer is slow... // for the first frame, we give a bit of extra leeway becase gstreamer is slow...
//watchdog->start(timeoutMs); //watchdog->start(timeoutMs);
watchdog->start(15e3); watchdog->start(15e3);
pmIdx = gPerformanceMonitor.enrol(id, "Frame Grabber"); pmIdx = gPerformanceMonitor.enrol(id, "Frame Grabber");
} }
...@@ -86,11 +86,15 @@ bool FrameGrabber::present(const QVideoFrame &frame) ...@@ -86,11 +86,15 @@ bool FrameGrabber::present(const QVideoFrame &frame)
Timestamp t = gTimer.elapsed(); Timestamp t = gTimer.elapsed();
QVariant ft = frame.metaData("timestamp"); QVariant ft = frame.metaData("timestamp");
if (ft.isValid() ) { if (ft.isValid() ) {
if (timestampOffset == INT_MAX) // offset between gTimer and frame timer if ( timestampOffsetEstimators.size() < 30) {
timestampOffset = t - ft.toInt(); // Use median of first N frames to estimate the offset
t = ft.toInt() + timestampOffset; timestampOffsetEstimators.emplace_back( t - ft.toInt() );
} std::sort(timestampOffsetEstimators.begin(), timestampOffsetEstimators.end());
timestampOffset = timestampOffsetEstimators[ 0.5*(timestampOffsetEstimators.size()-1) ];
}
t = ft.toInt() + timestampOffset;
}
if (!frame.isValid()) if (!frame.isValid())
return false; return false;
...@@ -117,8 +121,8 @@ bool FrameGrabber::present(const QVideoFrame &frame) ...@@ -117,8 +121,8 @@ bool FrameGrabber::present(const QVideoFrame &frame)
copy.unmap(); copy.unmap();
if (success && !cvFrame.empty()) { if (success && !cvFrame.empty()) {
watchdog->start(timeoutMs); watchdog->start(timeoutMs);
emit newFrame(t, cvFrame); emit newFrame(t, cvFrame);
} else } else
gPerformanceMonitor.account(pmIdx); gPerformanceMonitor.account(pmIdx);
......
...@@ -35,7 +35,8 @@ public slots: ...@@ -35,7 +35,8 @@ public slots:
private: private:
QTimer *watchdog; QTimer *watchdog;
int timeoutMs; int timeoutMs;
int timestampOffset; Timestamp timestampOffset;
std::vector<Timestamp> timestampOffsetEstimators;
#ifdef TURBOJPEG #ifdef TURBOJPEG
tjhandle tjh; tjhandle tjh;
#endif #endif
......
...@@ -41,9 +41,9 @@ unsigned int PerformanceMonitor::enrol(const QString &id, const QString &stage) ...@@ -41,9 +41,9 @@ unsigned int PerformanceMonitor::enrol(const QString &id, const QString &stage)
return idx; return idx;
} }
bool PerformanceMonitor::shouldDrop(const unsigned int &idx, const unsigned int &delay, const unsigned int &maxDelay) bool PerformanceMonitor::shouldDrop(const unsigned int &idx, const int &delay, const int &maxDelay)
{ {
if (delay > maxDelay) { if (delay > maxDelay) {
droppedFrameCount[idx]++; droppedFrameCount[idx]++;
return frameDropEnabled; return frameDropEnabled;
} }
......
...@@ -12,7 +12,7 @@ public: ...@@ -12,7 +12,7 @@ public:
std::vector<unsigned long int> droppedFrameCount; std::vector<unsigned long int> droppedFrameCount;
unsigned int enrol(const QString &id, const QString &stage); unsigned int enrol(const QString &id, const QString &stage);
bool shouldDrop(const unsigned int &idx, const unsigned int &delay, const unsigned int &maxDelay=50); bool shouldDrop(const unsigned int &idx, const int &delay, const int &maxDelay=50);
void account(const unsigned int &idx); void account(const unsigned int &idx);
QString getEnrolled(const unsigned int &idx) { return idx < enrolled.size() ? enrolled[idx] : QString(); } QString getEnrolled(const unsigned int &idx) { return idx < enrolled.size() ? enrolled[idx] : QString(); }
unsigned int enrolledCount() { return (unsigned int) enrolled.size(); } unsigned int enrolledCount() { return (unsigned int) enrolled.size(); }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment