Fixing camera enumeration on hotplug
authorLibor Tomsik <libor@tomsik.eu>
Tue, 24 Feb 2015 12:51:34 +0000 (13:51 +0100)
committerYoann Lopes <yoann.lopes@theqtcompany.com>
Wed, 25 Feb 2015 10:29:21 +0000 (10:29 +0000)
Emptying camera HW devices buffer when it becomes obsolete (500ms).
Leaving the list filled was causing video cameras to keep
on the list even when the camera was unplugged. Or vice versa, empty
after the camera was plugged into the system.

Change-Id: I3b38a0b327553cbb6e18dd5421cdde3ded1b7895
Reviewed-by: Yoann Lopes <yoann.lopes@theqtcompany.com>
src/gsttools/qgstutils.cpp

index 1281d3f..465f439 100644 (file)
@@ -41,6 +41,7 @@
 #include <QtCore/qset.h>
 #include <QtCore/qstringlist.h>
 #include <qaudioformat.h>
+#include <QtCore/qelapsedtimer.h>
 
 #ifdef USE_V4L
 #  include <private/qcore_unix_p.h>
@@ -411,6 +412,10 @@ Q_GLOBAL_STATIC(FactoryCameraInfoMap, qt_camera_device_info);
 
 QVector<QGstUtils::CameraInfo> QGstUtils::enumerateCameras(GstElementFactory *factory)
 {
+    static QElapsedTimer camerasCacheAgeTimer;
+    if (camerasCacheAgeTimer.isValid() && camerasCacheAgeTimer.elapsed() > 500) // ms
+        qt_camera_device_info()->clear();
+
     FactoryCameraInfoMap::const_iterator it = qt_camera_device_info()->constFind(factory);
     if (it != qt_camera_device_info()->constEnd())
         return *it;
@@ -469,6 +474,7 @@ QVector<QGstUtils::CameraInfo> QGstUtils::enumerateCameras(GstElementFactory *fa
         }
 
         if (!devices.isEmpty() || !hasVideoSource) {
+            camerasCacheAgeTimer.restart();
             return devices;
         }
     }
@@ -527,6 +533,7 @@ QVector<QGstUtils::CameraInfo> QGstUtils::enumerateCameras(GstElementFactory *fa
         }
         qt_safe_close(fd);
     }
+    camerasCacheAgeTimer.restart();
 #endif // USE_V4L
 
     return devices;