From 50006a9a32cb360706173feabf4677535ffa108c Mon Sep 17 00:00:00 2001 From: tux3 Date: Sat, 24 Oct 2015 05:19:23 +0200 Subject: [PATCH] Faster video device opening --- src/video/camerasource.cpp | 10 +++++++++- src/video/camerasource.h | 1 + src/widget/form/settings/avform.cpp | 3 +-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/video/camerasource.cpp b/src/video/camerasource.cpp index 586cdbf9a..c006ea863 100644 --- a/src/video/camerasource.cpp +++ b/src/video/camerasource.cpp @@ -37,7 +37,7 @@ CameraSource* CameraSource::instance{nullptr}; CameraSource::CameraSource() : deviceName{"none"}, device{nullptr}, mode(VideoMode{0,0,0}), cctx{nullptr}, cctxOrig{nullptr}, videoStreamIndex{-1}, - _isOpen{false}, subscriptions{0} + _isOpen{false}, streamBlocker{false}, subscriptions{0} { subscriptions = 0; av_register_all(); @@ -72,10 +72,14 @@ void CameraSource::open(const QString deviceName) void CameraSource::open(const QString DeviceName, VideoMode Mode) { + streamBlocker = true; QMutexLocker l{&biglock}; if (DeviceName == deviceName && Mode == mode) + { + streamBlocker = false; return; + } if (subscriptions) closeDevice(); @@ -86,6 +90,8 @@ void CameraSource::open(const QString DeviceName, VideoMode Mode) if (subscriptions && _isOpen) openDevice(); + + streamBlocker = false; } void CameraSource::close() @@ -348,6 +354,8 @@ void CameraSource::stream() // Give a chance to other functions to pick up the lock if needed biglock.unlock(); + while (streamBlocker) + QThread::yieldCurrentThread(); QThread::yieldCurrentThread(); } } diff --git a/src/video/camerasource.h b/src/video/camerasource.h index 24ee5aada..8b59ce850 100644 --- a/src/video/camerasource.h +++ b/src/video/camerasource.h @@ -93,6 +93,7 @@ private: int videoStreamIndex; ///< A camera can have multiple streams, this is the one we're decoding QMutex biglock, freelistLock; ///< True when locked. Faster than mutexes for video decoding. std::atomic_bool _isOpen; + std::atomic_bool streamBlocker; ///< Holds the streaming thread still when true std::atomic_int subscriptions; ///< Remember how many times we subscribed for RAII static CameraSource* instance; diff --git a/src/widget/form/settings/avform.cpp b/src/widget/form/settings/avform.cpp index 9db9f844c..80be8a100 100644 --- a/src/widget/form/settings/avform.cpp +++ b/src/widget/form/settings/avform.cpp @@ -204,14 +204,13 @@ void AVForm::onVideoDevChanged(int index) qWarning() << "Invalid index"; return; } + QString dev = videoDeviceList[index].first; Settings::getInstance().setVideoDev(dev); bool previouslyBlocked = bodyUI->videoModescomboBox->blockSignals(true); updateVideoModes(index); bodyUI->videoModescomboBox->blockSignals(previouslyBlocked); camera.open(dev); - killVideoSurface(); - createVideoSurface(); if (dev == "none") Core::getInstance()->getAv()->sendNoVideo(); }