mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
non-blocking cam communication, fixed CameraWorker::probeResolutions ...
called from wrong thread, try fix #408 and #495
This commit is contained in:
parent
2d4329767e
commit
c5dfb33999
|
@ -36,7 +36,10 @@ Camera::Camera()
|
|||
connect(workerThread, &QThread::started, worker, &CameraWorker::onStart);
|
||||
connect(workerThread, &QThread::finished, worker, &CameraWorker::deleteLater);
|
||||
connect(worker, &CameraWorker::newFrameAvailable, this, &Camera::onNewFrameAvailable);
|
||||
connect(worker, &CameraWorker::resProbingFinished, this, &Camera::onResProbingFinished);
|
||||
|
||||
connect(worker, &CameraWorker::resProbingFinished, this, &Camera::resolutionProbingFinished);
|
||||
connect(worker, &CameraWorker::propProbingFinished, this, [=](int prop, double val) { emit propProbingFinished(Prop(prop), val); } );
|
||||
|
||||
workerThread->start();
|
||||
}
|
||||
|
||||
|
@ -48,29 +51,29 @@ Camera::~Camera()
|
|||
|
||||
void Camera::subscribe()
|
||||
{
|
||||
if (needsInit)
|
||||
{
|
||||
worker->probeResolutions();
|
||||
needsInit = false;
|
||||
}
|
||||
|
||||
if (refcount <= 0)
|
||||
if (refcount++ <= 0)
|
||||
worker->resume();
|
||||
|
||||
refcount++;
|
||||
}
|
||||
|
||||
void Camera::unsubscribe()
|
||||
{
|
||||
refcount--;
|
||||
|
||||
if (refcount <= 0)
|
||||
if (--refcount <= 0)
|
||||
{
|
||||
worker->suspend();
|
||||
refcount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Camera::probeProp(Camera::Prop prop)
|
||||
{
|
||||
worker->probeProp(int(prop));
|
||||
}
|
||||
|
||||
void Camera::probeResolutions()
|
||||
{
|
||||
worker->probeResolutions();
|
||||
}
|
||||
|
||||
vpx_image Camera::getLastVPXImage()
|
||||
{
|
||||
QMutexLocker lock(&mutex);
|
||||
|
@ -115,75 +118,25 @@ vpx_image Camera::getLastVPXImage()
|
|||
return img;
|
||||
}
|
||||
|
||||
QList<QSize> Camera::getSupportedResolutions()
|
||||
{
|
||||
return resolutions;
|
||||
}
|
||||
|
||||
QSize Camera::getBestVideoMode()
|
||||
{
|
||||
int bestScore = 0;
|
||||
QSize bestRes;
|
||||
|
||||
for (QSize res : getSupportedResolutions())
|
||||
{
|
||||
int score = res.width() * res.height();
|
||||
|
||||
if (score > bestScore)
|
||||
{
|
||||
bestScore = score;
|
||||
bestRes = res;
|
||||
}
|
||||
}
|
||||
|
||||
return bestRes;
|
||||
}
|
||||
|
||||
void Camera::setResolution(QSize res)
|
||||
{
|
||||
worker->setProp(CV_CAP_PROP_FRAME_WIDTH, res.width());
|
||||
worker->setProp(CV_CAP_PROP_FRAME_HEIGHT, res.height());
|
||||
}
|
||||
|
||||
QSize Camera::getResolution()
|
||||
QSize Camera::getCurrentResolution()
|
||||
{
|
||||
return QSize(worker->getProp(CV_CAP_PROP_FRAME_WIDTH), worker->getProp(CV_CAP_PROP_FRAME_HEIGHT));
|
||||
}
|
||||
|
||||
void Camera::setProp(Camera::Prop prop, double val)
|
||||
{
|
||||
switch (prop)
|
||||
{
|
||||
case BRIGHTNESS:
|
||||
worker->setProp(CV_CAP_PROP_BRIGHTNESS, val);
|
||||
break;
|
||||
case SATURATION:
|
||||
worker->setProp(CV_CAP_PROP_SATURATION, val);
|
||||
break;
|
||||
case CONTRAST:
|
||||
worker->setProp(CV_CAP_PROP_CONTRAST, val);
|
||||
break;
|
||||
case HUE:
|
||||
worker->setProp(CV_CAP_PROP_HUE, val);
|
||||
break;
|
||||
}
|
||||
worker->setProp(int(prop), val);
|
||||
}
|
||||
|
||||
double Camera::getProp(Camera::Prop prop)
|
||||
{
|
||||
switch (prop)
|
||||
{
|
||||
case BRIGHTNESS:
|
||||
return worker->getProp(CV_CAP_PROP_BRIGHTNESS);
|
||||
case SATURATION:
|
||||
return worker->getProp(CV_CAP_PROP_SATURATION);
|
||||
case CONTRAST:
|
||||
return worker->getProp(CV_CAP_PROP_CONTRAST);
|
||||
case HUE:
|
||||
return worker->getProp(CV_CAP_PROP_HUE);
|
||||
}
|
||||
|
||||
return 0.0;
|
||||
return worker->getProp(int(prop));
|
||||
}
|
||||
|
||||
void Camera::onNewFrameAvailable(const VideoFrame frame)
|
||||
|
@ -195,11 +148,6 @@ void Camera::onNewFrameAvailable(const VideoFrame frame)
|
|||
mutex.unlock();
|
||||
}
|
||||
|
||||
void Camera::onResProbingFinished(QList<QSize> res)
|
||||
{
|
||||
resolutions = res;
|
||||
}
|
||||
|
||||
Camera* Camera::getInstance()
|
||||
{
|
||||
if (!instance)
|
||||
|
|
25
src/camera.h
25
src/camera.h
|
@ -36,11 +36,13 @@ class Camera : public VideoSource
|
|||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum Prop {
|
||||
BRIGHTNESS,
|
||||
SATURATION,
|
||||
CONTRAST,
|
||||
HUE,
|
||||
enum Prop : int {
|
||||
BRIGHTNESS = CV_CAP_PROP_BRIGHTNESS,
|
||||
SATURATION = CV_CAP_PROP_SATURATION,
|
||||
CONTRAST = CV_CAP_PROP_CONTRAST,
|
||||
HUE = CV_CAP_PROP_HUE,
|
||||
WIDTH = CV_CAP_PROP_FRAME_WIDTH,
|
||||
HEIGHT = CV_CAP_PROP_FRAME_HEIGHT,
|
||||
};
|
||||
|
||||
~Camera();
|
||||
|
@ -48,19 +50,23 @@ public:
|
|||
static Camera* getInstance(); ///< Returns the global widget's Camera instance
|
||||
vpx_image getLastVPXImage(); ///< Convert the last frame to a vpx_image (can be expensive !)
|
||||
|
||||
QList<QSize> getSupportedResolutions();
|
||||
QSize getBestVideoMode();
|
||||
|
||||
void setResolution(QSize res);
|
||||
QSize getResolution();
|
||||
QSize getCurrentResolution();
|
||||
|
||||
void setProp(Prop prop, double val);
|
||||
double getProp(Prop prop);
|
||||
|
||||
void probeProp(Prop prop);
|
||||
void probeResolutions();
|
||||
|
||||
// VideoSource interface
|
||||
virtual void subscribe();
|
||||
virtual void unsubscribe();
|
||||
|
||||
signals:
|
||||
void resolutionProbingFinished(QList<QSize> res);
|
||||
void propProbingFinished(Prop prop, double val);
|
||||
|
||||
protected:
|
||||
Camera();
|
||||
|
||||
|
@ -79,7 +85,6 @@ private:
|
|||
|
||||
private slots:
|
||||
void onNewFrameAvailable(const VideoFrame frame);
|
||||
void onResProbingFinished(QList<QSize> res);
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
#include <QTimer>
|
||||
#include <QDebug>
|
||||
#include <QCoreApplication>
|
||||
#include <QThread>
|
||||
|
||||
CameraWorker::CameraWorker(int index)
|
||||
: clock(nullptr)
|
||||
|
@ -26,6 +26,7 @@ CameraWorker::CameraWorker(int index)
|
|||
, refCount(0)
|
||||
{
|
||||
qRegisterMetaType<VideoFrame>();
|
||||
qRegisterMetaType<QList<QSize>>();
|
||||
}
|
||||
|
||||
void CameraWorker::onStart()
|
||||
|
@ -67,13 +68,14 @@ double CameraWorker::_getProp(int prop)
|
|||
{
|
||||
subscribe();
|
||||
props[prop] = cam.get(prop);
|
||||
emit propProbingFinished(prop, props[prop]);
|
||||
unsubscribe();
|
||||
}
|
||||
|
||||
return props.value(prop);
|
||||
}
|
||||
|
||||
void CameraWorker::probeResolutions()
|
||||
void CameraWorker::_probeResolutions()
|
||||
{
|
||||
if (resolutions.isEmpty())
|
||||
{
|
||||
|
@ -115,9 +117,9 @@ void CameraWorker::probeResolutions()
|
|||
}
|
||||
|
||||
unsubscribe();
|
||||
}
|
||||
|
||||
qDebug() << "Camera resolutions:" << resolutions;
|
||||
qDebug() << "CameraWorker: Resolutions" <<resolutions;
|
||||
}
|
||||
|
||||
emit resProbingFinished(resolutions);
|
||||
}
|
||||
|
@ -133,7 +135,7 @@ void CameraWorker::applyProps()
|
|||
|
||||
void CameraWorker::subscribe()
|
||||
{
|
||||
if (refCount == 0)
|
||||
if (refCount++ == 0)
|
||||
{
|
||||
if (!cam.isOpened())
|
||||
{
|
||||
|
@ -142,15 +144,11 @@ void CameraWorker::subscribe()
|
|||
applyProps(); // restore props
|
||||
}
|
||||
}
|
||||
|
||||
refCount++;
|
||||
}
|
||||
|
||||
void CameraWorker::unsubscribe()
|
||||
{
|
||||
refCount--;
|
||||
|
||||
if(refCount <= 0)
|
||||
if(--refCount <= 0)
|
||||
{
|
||||
cam.release();
|
||||
refCount = 0;
|
||||
|
@ -188,10 +186,19 @@ void CameraWorker::setProp(int prop, double val)
|
|||
QMetaObject::invokeMethod(this, "_setProp", Q_ARG(int, prop), Q_ARG(double, val));
|
||||
}
|
||||
|
||||
void CameraWorker::probeProp(int prop)
|
||||
{
|
||||
QMetaObject::invokeMethod(this, "_getProp", Q_ARG(int, prop));
|
||||
}
|
||||
|
||||
void CameraWorker::probeResolutions()
|
||||
{
|
||||
QMetaObject::invokeMethod(this, "_probeResolutions");
|
||||
}
|
||||
|
||||
double CameraWorker::getProp(int prop)
|
||||
{
|
||||
double ret = 0.0;
|
||||
qApp->processEvents();
|
||||
QMetaObject::invokeMethod(this, "_getProp", Qt::BlockingQueuedConnection, Q_RETURN_ARG(double, ret), Q_ARG(int, prop));
|
||||
|
||||
return ret;
|
||||
|
|
|
@ -40,21 +40,24 @@ public:
|
|||
void resume();
|
||||
void setProp(int prop, double val);
|
||||
double getProp(int prop); // blocking call!
|
||||
void probeResolutions();
|
||||
|
||||
public slots:
|
||||
void onStart();
|
||||
void probeProp(int prop);
|
||||
void probeResolutions();
|
||||
|
||||
signals:
|
||||
void started();
|
||||
void newFrameAvailable(const VideoFrame frame);
|
||||
void resProbingFinished(QList<QSize> res);
|
||||
void propProbingFinished(int prop, double val);
|
||||
|
||||
private slots:
|
||||
void _suspend();
|
||||
void _resume();
|
||||
void _setProp(int prop, double val);
|
||||
double _getProp(int prop);
|
||||
void _probeResolutions();
|
||||
|
||||
private:
|
||||
void applyProps();
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
*/
|
||||
|
||||
#include "avform.h"
|
||||
#include "src/camera.h"
|
||||
#include "ui_avsettings.h"
|
||||
|
||||
AVForm::AVForm() :
|
||||
|
@ -23,6 +22,9 @@ AVForm::AVForm() :
|
|||
{
|
||||
bodyUI = new Ui::AVSettings;
|
||||
bodyUI->setupUi(this);
|
||||
|
||||
connect(Camera::getInstance(), &Camera::propProbingFinished, this, &AVForm::onPropProbingFinished);
|
||||
connect(Camera::getInstance(), &Camera::resolutionProbingFinished, this, &AVForm::onResProbingFinished);
|
||||
}
|
||||
|
||||
AVForm::~AVForm()
|
||||
|
@ -34,15 +36,12 @@ void AVForm::present()
|
|||
{
|
||||
bodyUI->CamVideoSurface->setSource(Camera::getInstance());
|
||||
|
||||
bodyUI->videoModescomboBox->clear();
|
||||
QList<QSize> res = Camera::getInstance()->getSupportedResolutions();
|
||||
for (QSize r : res)
|
||||
bodyUI->videoModescomboBox->addItem(QString("%1x%2").arg(QString::number(r.width()),QString::number(r.height())));
|
||||
Camera::getInstance()->probeProp(Camera::SATURATION);
|
||||
Camera::getInstance()->probeProp(Camera::CONTRAST);
|
||||
Camera::getInstance()->probeProp(Camera::BRIGHTNESS);
|
||||
Camera::getInstance()->probeProp(Camera::HUE);
|
||||
|
||||
bodyUI->ContrastSlider->setValue(Camera::getInstance()->getProp(Camera::CONTRAST)*100);
|
||||
bodyUI->BrightnessSlider->setValue(Camera::getInstance()->getProp(Camera::BRIGHTNESS)*100);
|
||||
bodyUI->SaturationSlider->setValue(Camera::getInstance()->getProp(Camera::SATURATION)*100);
|
||||
bodyUI->HueSlider->setValue(Camera::getInstance()->getProp(Camera::HUE)*100);
|
||||
Camera::getInstance()->probeResolutions();
|
||||
}
|
||||
|
||||
void AVForm::on_ContrastSlider_sliderMoved(int position)
|
||||
|
@ -65,13 +64,39 @@ void AVForm::on_HueSlider_sliderMoved(int position)
|
|||
Camera::getInstance()->setProp(Camera::HUE, position / 100.0);
|
||||
}
|
||||
|
||||
void AVForm::on_videoModescomboBox_currentIndexChanged(const QString &arg1)
|
||||
void AVForm::on_videoModescomboBox_activated(int index)
|
||||
{
|
||||
QStringList resStr = arg1.split("x");
|
||||
int w = resStr[0].toInt();
|
||||
int h = resStr[0].toInt();
|
||||
Camera::getInstance()->setResolution(bodyUI->videoModescomboBox->itemData(index).toSize());
|
||||
}
|
||||
|
||||
Camera::getInstance()->setResolution(QSize(w,h));
|
||||
void AVForm::onPropProbingFinished(Camera::Prop prop, double val)
|
||||
{
|
||||
switch (prop)
|
||||
{
|
||||
case Camera::BRIGHTNESS:
|
||||
bodyUI->BrightnessSlider->setValue(val*100);
|
||||
break;
|
||||
case Camera::CONTRAST:
|
||||
bodyUI->ContrastSlider->setValue(val*100);
|
||||
break;
|
||||
case Camera::SATURATION:
|
||||
bodyUI->SaturationSlider->setValue(val*100);
|
||||
break;
|
||||
case Camera::HUE:
|
||||
bodyUI->HueSlider->setValue(val*100);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void AVForm::onResProbingFinished(QList<QSize> res)
|
||||
{
|
||||
bodyUI->videoModescomboBox->clear();
|
||||
for (QSize r : res)
|
||||
bodyUI->videoModescomboBox->addItem(QString("%1x%2").arg(QString::number(r.width()),QString::number(r.height())), r);
|
||||
|
||||
bodyUI->videoModescomboBox->setCurrentIndex(bodyUI->videoModescomboBox->count()-1);
|
||||
}
|
||||
|
||||
void AVForm::hideEvent(QHideEvent *)
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
#include "genericsettings.h"
|
||||
#include "src/widget/videosurface.h"
|
||||
#include "src/camera.h"
|
||||
#include <QGroupBox>
|
||||
#include <QVBoxLayout>
|
||||
#include <QPushButton>
|
||||
|
@ -43,7 +44,11 @@ private slots:
|
|||
void on_SaturationSlider_sliderMoved(int position);
|
||||
void on_BrightnessSlider_sliderMoved(int position);
|
||||
void on_HueSlider_sliderMoved(int position);
|
||||
void on_videoModescomboBox_currentIndexChanged(const QString &arg1);
|
||||
void on_videoModescomboBox_activated(int index);
|
||||
|
||||
// camera
|
||||
void onPropProbingFinished(Camera::Prop prop, double val);
|
||||
void onResProbingFinished(QList<QSize> res);
|
||||
|
||||
virtual void hideEvent(QHideEvent*);
|
||||
|
||||
|
|
|
@ -130,10 +130,14 @@ void VideoSurface::paintGL()
|
|||
VideoFrame currFrame = frame;
|
||||
mutex.unlock();
|
||||
|
||||
if (res != currFrame.resolution)
|
||||
if (res != currFrame.resolution && currFrame.resolution.isValid())
|
||||
{
|
||||
res = currFrame.resolution;
|
||||
|
||||
// delete old texture
|
||||
if (textureId != 0)
|
||||
glDeleteTextures(1, &textureId);
|
||||
|
||||
// a texture used to render the pbo (has the match the pixelformat of the source)
|
||||
glGenTextures(1,&textureId);
|
||||
glBindTexture(GL_TEXTURE_2D, textureId);
|
||||
|
@ -148,7 +152,7 @@ void VideoSurface::paintGL()
|
|||
|
||||
if (pboAllocSize != currFrame.frameData.size())
|
||||
{
|
||||
qDebug() << "VideoSurface: Resize pbo " << currFrame.frameData.size() << "bytes (before" << pboAllocSize << ")";
|
||||
qDebug() << "VideoSurface: Resize pbo " << currFrame.frameData.size() << "(" << currFrame.resolution << ")" << "bytes (before" << pboAllocSize << ")";
|
||||
|
||||
pbo[0]->bind();
|
||||
pbo[0]->allocate(currFrame.frameData.size());
|
||||
|
|
Loading…
Reference in New Issue
Block a user