qv4l2: fix broken fps determination
authorHans Verkuil <hverkuil@xs4all.nl>
Wed, 4 Aug 2010 11:07:27 +0000 (13:07 +0200)
committerHans Verkuil <hverkuil@xs4all.nl>
Wed, 4 Aug 2010 11:07:27 +0000 (13:07 +0200)
The fps determination was done based on a 2s-period timer. However,
this timer would become unreliable if there is a heavy CPU load.

Replaced the timer with gettimeofday calculations.

Signed-off-by: Hans Verkuil <hverkuil@xs4all.nl>
utils/qv4l2/capture-win.cpp
utils/qv4l2/capture-win.h
utils/qv4l2/qv4l2.cpp

index 4acf465..6628b94 100644 (file)
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
-
+#include <stdio.h>
 #include <QLabel>
 #include <QImage>
-#include <QTimer>
 #include <QVBoxLayout>
 #include <QCloseEvent>
 
@@ -33,8 +32,6 @@ CaptureWin::CaptureWin()
        m_frame = 0;
        m_label = new QLabel();
        m_msg = new QLabel("No frame");
-       m_timer = new QTimer(this);
-       connect(m_timer, SIGNAL(timeout()), this, SLOT(update()));
 
        vbox->addWidget(m_label);
        vbox->addWidget(m_msg);
@@ -47,25 +44,23 @@ void CaptureWin::setImage(const QImage &image, bool init)
                m_frame = m_lastFrame = m_fps = 0;
                m_msg->setText("No frame");
        } else {
-               if (m_frame == 0) {
-                       m_timer->start(2000);
+               struct timeval tv, res;
+
+               if (m_frame == 0)
+                       gettimeofday(&m_tv, NULL);
+               gettimeofday(&tv, NULL);
+               timersub(&tv, &m_tv, &res);
+               if (res.tv_sec) {
+                       m_fps = (100 * (m_frame - m_lastFrame)) /
+                               (res.tv_sec * 100 + res.tv_usec / 10000);
+                       m_lastFrame = m_frame;
+                       m_tv = tv;
                }
                m_msg->setText(QString("Frame: %1 Fps: %2")
                                .arg(++m_frame).arg(m_fps));
        }
 }
 
-void CaptureWin::stop()
-{
-       m_timer->stop();
-}
-
-void CaptureWin::update()
-{
-       m_fps = (m_frame - m_lastFrame + 1) / 2;
-       m_lastFrame = m_frame;
-}
-
 void CaptureWin::closeEvent(QCloseEvent *event)
 {
        QWidget::closeEvent(event);
index 5546056..51e554d 100644 (file)
 #define CAPTURE_WIN_H
 
 #include <QWidget>
+#include <sys/time.h>
 
 class QImage;
 class QLabel;
-class QTimer;
 
 class CaptureWin : public QWidget
 {
@@ -35,15 +35,11 @@ public:
        virtual ~CaptureWin() {}
 
        void setImage(const QImage &image, bool init = false);
-       void stop();
        unsigned frame() const { return m_frame; }
 
 protected:
        virtual void closeEvent(QCloseEvent *event);
 
-private slots:
-       void update();
-
 signals:
        void close();
 
@@ -53,7 +49,7 @@ private:
        unsigned m_frame;
        unsigned m_lastFrame;
        unsigned m_fps;
-       QTimer *m_timer;
+       struct timeval m_tv;
 };
 
 #endif
index 82fd691..2931cae 100644 (file)
@@ -370,7 +370,6 @@ void ApplicationWindow::stopCapture()
        }
        free(m_buffers);
        m_buffers = NULL;
-       m_capture->stop();
        refresh();
 }