Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix: Invoke device methods in deviceThread
Browse files Browse the repository at this point in the history
Fix #2058
  • Loading branch information
Diadlo committed Jul 5, 2017
1 parent d720cca commit d86912e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/video/camerasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ CameraSource::CameraSource()
, _isNone{true}
, subscriptions{0}
{
qRegisterMetaType<VideoMode>("VideoMode");
deviceThread->setObjectName("Device thread");
deviceThread->start();
moveToThread(deviceThread);
Expand Down Expand Up @@ -155,6 +156,12 @@ void CameraSource::setupDefault()
*/
void CameraSource::setupDevice(const QString& DeviceName, const VideoMode& Mode)
{
if (QThread::currentThread() != deviceThread) {
QMetaObject::invokeMethod(this, "setupDevice", Q_ARG(const QString&, DeviceName),
Q_ARG(const VideoMode&, Mode));
return;
}

QWriteLocker locker{&deviceMutex};

if (DeviceName == deviceName && Mode == mode) {
Expand Down Expand Up @@ -240,6 +247,11 @@ void CameraSource::unsubscribe()
*/
void CameraSource::openDevice()
{
if (QThread::currentThread() != deviceThread) {
QMetaObject::invokeMethod(this, "openDevice");
return;
}

QWriteLocker locker{&streamMutex};
qDebug() << "Opening device " << deviceName;

Expand Down Expand Up @@ -347,6 +359,11 @@ void CameraSource::openDevice()
*/
void CameraSource::closeDevice()
{
if (QThread::currentThread() != deviceThread) {
QMetaObject::invokeMethod(this, "closeDevice");
return;
}

QWriteLocker locker{&streamMutex};
qDebug() << "Closing device " << deviceName;

Expand Down
6 changes: 5 additions & 1 deletion src/video/camerasource.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ class CameraSource : public VideoSource
static CameraSource& getInstance();
static void destroyInstance();
void setupDefault();
void setupDevice(const QString& deviceName, const VideoMode& mode);
bool isNone() const;

// VideoSource interface
virtual void subscribe() override;
virtual void unsubscribe() override;

public slots:
void setupDevice(const QString& deviceName, const VideoMode& mode);

signals:
void deviceOpened();
void openFailed();
Expand All @@ -55,6 +57,8 @@ class CameraSource : public VideoSource
CameraSource();
~CameraSource();
void stream();

private slots:
void openDevice();
void closeDevice();

Expand Down

0 comments on commit d86912e

Please sign in to comment.