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) :
code(code),
yuvBuffer(NULL),
yuvBufferSize(0),
timestampOffset(INT_MAX),
timeoutMs(2e3)
timestampOffset(0),
timeoutMs(2e3)
{
#ifdef TURBOJPEG
tjh = tjInitDecompress();
......@@ -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...
//watchdog->start(timeoutMs);
watchdog->start(15e3);
watchdog->start(15e3);
pmIdx = gPerformanceMonitor.enrol(id, "Frame Grabber");
}
......@@ -86,11 +86,15 @@ bool FrameGrabber::present(const QVideoFrame &frame)
Timestamp t = gTimer.elapsed();
QVariant ft = frame.metaData("timestamp");
if (ft.isValid() ) {
if (timestampOffset == INT_MAX) // offset between gTimer and frame timer
timestampOffset = t - ft.toInt();
t = ft.toInt() + timestampOffset;
}
if (ft.isValid() ) {
if ( timestampOffsetEstimators.size() < 30) {
// Use median of first N frames to estimate the offset
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())
return false;
......@@ -117,8 +121,8 @@ bool FrameGrabber::present(const QVideoFrame &frame)
copy.unmap();
if (success && !cvFrame.empty()) {
watchdog->start(timeoutMs);
emit newFrame(t, cvFrame);
watchdog->start(timeoutMs);
emit newFrame(t, cvFrame);
} else
gPerformanceMonitor.account(pmIdx);
......
......@@ -35,7 +35,8 @@ public slots:
private:
QTimer *watchdog;
int timeoutMs;
int timestampOffset;
Timestamp timestampOffset;
std::vector<Timestamp> timestampOffsetEstimators;
#ifdef TURBOJPEG
tjhandle tjh;
#endif
......
......@@ -41,9 +41,9 @@ unsigned int PerformanceMonitor::enrol(const QString &id, const QString &stage)
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]++;
return frameDropEnabled;
}
......
......@@ -12,7 +12,7 @@ public:
std::vector<unsigned long int> droppedFrameCount;
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);
QString getEnrolled(const unsigned int &idx) { return idx < enrolled.size() ? enrolled[idx] : QString(); }
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