2014-07-07 00:19:45 +08:00
|
|
|
/*
|
|
|
|
Copyright (C) 2014 by Project Tox <https://tox.im>
|
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
This program is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the COPYING file for more details.
|
|
|
|
*/
|
|
|
|
|
2014-06-30 05:41:47 +08:00
|
|
|
#include "camera.h"
|
2014-09-11 21:44:34 +08:00
|
|
|
#include "widget.h"
|
2014-10-08 20:25:32 +08:00
|
|
|
#include <QtConcurrent/QtConcurrentRun>
|
2014-10-07 20:17:35 +08:00
|
|
|
#include <QDebug>
|
2014-10-08 20:25:32 +08:00
|
|
|
#include <QThread>
|
|
|
|
#include <QTimer>
|
2014-06-30 05:41:47 +08:00
|
|
|
|
2014-10-08 20:25:32 +08:00
|
|
|
Camera* Camera::instance = nullptr;
|
2014-08-29 19:40:37 +08:00
|
|
|
|
2014-06-30 05:41:47 +08:00
|
|
|
Camera::Camera()
|
2014-10-08 20:25:32 +08:00
|
|
|
: refcount(0)
|
|
|
|
, workerThread(nullptr)
|
|
|
|
, worker(nullptr)
|
2014-06-30 05:41:47 +08:00
|
|
|
{
|
2014-10-08 20:25:32 +08:00
|
|
|
qDebug() << "New Worker";
|
|
|
|
worker = new SelfCamWorker(0);
|
|
|
|
workerThread = new QThread();
|
|
|
|
|
|
|
|
worker->moveToThread(workerThread);
|
|
|
|
|
|
|
|
connect(workerThread, &QThread::started, worker, &SelfCamWorker::onStart);
|
|
|
|
connect(workerThread, &QThread::finished, worker, &SelfCamWorker::deleteLater);
|
|
|
|
connect(workerThread, &QThread::deleteLater, worker, &SelfCamWorker::deleteLater);
|
2014-10-08 21:48:02 +08:00
|
|
|
connect(worker, &SelfCamWorker::started, this, &Camera::onWorkerStarted);
|
2014-10-08 20:25:32 +08:00
|
|
|
connect(worker, &SelfCamWorker::newFrameAvailable, this, &Camera::onNewFrameAvailable);
|
2014-10-08 21:48:02 +08:00
|
|
|
connect(worker, &SelfCamWorker::resProbingFinished, this, &Camera::onResProbingFinished);
|
2014-10-08 20:25:32 +08:00
|
|
|
workerThread->start();
|
|
|
|
}
|
|
|
|
|
2014-10-08 21:48:02 +08:00
|
|
|
void Camera::onWorkerStarted()
|
|
|
|
{
|
|
|
|
worker->probeResolutions();
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:25:32 +08:00
|
|
|
Camera::~Camera()
|
|
|
|
{
|
|
|
|
workerThread->exit();
|
|
|
|
workerThread->deleteLater();
|
2014-06-30 05:41:47 +08:00
|
|
|
}
|
|
|
|
|
2014-10-07 22:59:21 +08:00
|
|
|
void Camera::subscribe()
|
2014-06-30 05:41:47 +08:00
|
|
|
{
|
|
|
|
if (refcount <= 0)
|
2014-10-08 20:25:32 +08:00
|
|
|
worker->resume();
|
2014-10-08 21:48:02 +08:00
|
|
|
|
|
|
|
refcount++;
|
2014-06-30 05:41:47 +08:00
|
|
|
}
|
|
|
|
|
2014-10-07 22:59:21 +08:00
|
|
|
void Camera::unsubscribe()
|
2014-06-30 05:41:47 +08:00
|
|
|
{
|
|
|
|
refcount--;
|
|
|
|
|
|
|
|
if (refcount <= 0)
|
|
|
|
{
|
2014-10-08 20:25:32 +08:00
|
|
|
worker->suspend();
|
2014-06-30 05:41:47 +08:00
|
|
|
refcount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:25:32 +08:00
|
|
|
cv::Mat Camera::getLastFrame()
|
2014-08-29 19:40:37 +08:00
|
|
|
{
|
2014-10-08 21:48:02 +08:00
|
|
|
return currFrame;
|
2014-06-30 05:41:47 +08:00
|
|
|
}
|
2014-06-30 20:49:42 +08:00
|
|
|
|
|
|
|
vpx_image Camera::getLastVPXImage()
|
|
|
|
{
|
2014-10-08 20:25:32 +08:00
|
|
|
cv::Mat3b frame = getLastFrame();
|
2014-07-01 05:48:12 +08:00
|
|
|
vpx_image img;
|
2014-08-29 20:20:32 +08:00
|
|
|
int w = frame.size().width, h = frame.size().height;
|
2014-06-30 20:49:42 +08:00
|
|
|
vpx_img_alloc(&img, VPX_IMG_FMT_I420, w, h, 1); // I420 == YUV420P, same as YV12 with U and V switched
|
|
|
|
|
2014-08-29 20:20:32 +08:00
|
|
|
size_t i=0, j=0;
|
|
|
|
for( int line = 0; line < h; ++line )
|
|
|
|
{
|
|
|
|
const cv::Vec3b *srcrow = frame[line];
|
|
|
|
if( !(line % 2) )
|
2014-07-12 20:41:42 +08:00
|
|
|
{
|
2014-08-29 20:20:32 +08:00
|
|
|
for( int x = 0; x < w; x += 2 )
|
2014-07-12 20:41:42 +08:00
|
|
|
{
|
2014-08-29 20:20:32 +08:00
|
|
|
uint8_t r = srcrow[x][2];
|
|
|
|
uint8_t g = srcrow[x][1];
|
|
|
|
uint8_t b = srcrow[x][0];
|
|
|
|
|
|
|
|
img.planes[VPX_PLANE_Y][i] = ((66*r + 129*g + 25*b) >> 8) + 16;
|
2014-08-29 20:33:19 +08:00
|
|
|
img.planes[VPX_PLANE_V][j] = ((-38*r + -74*g + 112*b) >> 8) + 128;
|
|
|
|
img.planes[VPX_PLANE_U][j] = ((112*r + -94*g + -18*b) >> 8) + 128;
|
2014-08-29 20:20:32 +08:00
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
|
|
|
|
r = srcrow[x+1][2];
|
|
|
|
g = srcrow[x+1][1];
|
|
|
|
b = srcrow[x+1][0];
|
|
|
|
img.planes[VPX_PLANE_Y][i] = ((66*r + 129*g + 25*b) >> 8) + 16;
|
|
|
|
i++;
|
2014-07-12 20:41:42 +08:00
|
|
|
}
|
2014-08-29 20:20:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( int x = 0; x < w; x += 1 )
|
2014-07-12 20:41:42 +08:00
|
|
|
{
|
2014-08-29 20:20:32 +08:00
|
|
|
uint8_t r = srcrow[x][2];
|
|
|
|
uint8_t g = srcrow[x][1];
|
|
|
|
uint8_t b = srcrow[x][0];
|
2014-07-12 20:41:42 +08:00
|
|
|
|
2014-08-29 20:20:32 +08:00
|
|
|
img.planes[VPX_PLANE_Y][i] = ((66*r + 129*g + 25*b) >> 8) + 16;
|
|
|
|
i++;
|
2014-07-12 20:41:42 +08:00
|
|
|
}
|
|
|
|
}
|
2014-06-30 20:49:42 +08:00
|
|
|
}
|
|
|
|
return img;
|
|
|
|
}
|
2014-09-11 21:44:34 +08:00
|
|
|
|
2014-10-08 21:48:02 +08:00
|
|
|
QList<QSize> Camera::getSupportedResolutions()
|
2014-10-07 20:17:35 +08:00
|
|
|
{
|
2014-10-08 21:48:02 +08:00
|
|
|
return resolutions;
|
2014-10-07 20:17:35 +08:00
|
|
|
}
|
|
|
|
|
2014-10-08 21:48:02 +08:00
|
|
|
QSize Camera::getBestVideoMode()
|
2014-10-07 20:17:35 +08:00
|
|
|
{
|
|
|
|
int bestScore = 0;
|
2014-10-08 21:48:02 +08:00
|
|
|
QSize bestRes;
|
2014-10-07 20:17:35 +08:00
|
|
|
|
2014-10-08 21:48:02 +08:00
|
|
|
for (QSize res : getSupportedResolutions())
|
2014-10-07 20:17:35 +08:00
|
|
|
{
|
2014-10-08 21:48:02 +08:00
|
|
|
int score = res.width() * res.height();
|
2014-10-07 20:17:35 +08:00
|
|
|
|
|
|
|
if (score > bestScore)
|
|
|
|
{
|
|
|
|
bestScore = score;
|
2014-10-08 21:48:02 +08:00
|
|
|
bestRes = res;
|
2014-10-07 20:17:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 21:48:02 +08:00
|
|
|
return bestRes;
|
2014-10-07 20:17:35 +08:00
|
|
|
}
|
|
|
|
|
2014-10-08 21:48:02 +08:00
|
|
|
void Camera::setResolution(QSize res)
|
2014-10-07 20:17:35 +08:00
|
|
|
{
|
2014-10-08 21:48:02 +08:00
|
|
|
worker->setProp(CV_CAP_PROP_FRAME_WIDTH, res.width());
|
|
|
|
worker->setProp(CV_CAP_PROP_FRAME_HEIGHT, res.height());
|
2014-10-07 20:17:35 +08:00
|
|
|
}
|
|
|
|
|
2014-10-08 21:48:02 +08:00
|
|
|
QSize Camera::getResolution()
|
2014-10-07 20:17:35 +08:00
|
|
|
{
|
2014-10-08 21:48:02 +08:00
|
|
|
return QSize(worker->getProp(CV_CAP_PROP_FRAME_WIDTH), worker->getProp(CV_CAP_PROP_FRAME_HEIGHT));
|
2014-10-07 20:17:35 +08:00
|
|
|
}
|
|
|
|
|
2014-10-07 22:59:21 +08:00
|
|
|
void Camera::setProp(Camera::Prop prop, double val)
|
|
|
|
{
|
|
|
|
switch (prop)
|
|
|
|
{
|
|
|
|
case BRIGHTNESS:
|
2014-10-08 21:48:02 +08:00
|
|
|
worker->setProp(CV_CAP_PROP_BRIGHTNESS, val);
|
2014-10-07 22:59:21 +08:00
|
|
|
break;
|
|
|
|
case SATURATION:
|
2014-10-08 21:48:02 +08:00
|
|
|
worker->setProp(CV_CAP_PROP_SATURATION, val);
|
2014-10-07 22:59:21 +08:00
|
|
|
break;
|
|
|
|
case CONTRAST:
|
2014-10-08 21:48:02 +08:00
|
|
|
worker->setProp(CV_CAP_PROP_CONTRAST, val);
|
2014-10-07 22:59:21 +08:00
|
|
|
break;
|
|
|
|
case HUE:
|
2014-10-08 21:48:02 +08:00
|
|
|
worker->setProp(CV_CAP_PROP_HUE, val);
|
2014-10-07 22:59:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double Camera::getProp(Camera::Prop prop)
|
|
|
|
{
|
|
|
|
switch (prop)
|
|
|
|
{
|
|
|
|
case BRIGHTNESS:
|
2014-10-08 21:48:02 +08:00
|
|
|
return worker->getProp(CV_CAP_PROP_BRIGHTNESS);
|
2014-10-07 22:59:21 +08:00
|
|
|
case SATURATION:
|
2014-10-08 21:48:02 +08:00
|
|
|
return worker->getProp(CV_CAP_PROP_SATURATION);
|
2014-10-07 22:59:21 +08:00
|
|
|
case CONTRAST:
|
2014-10-08 21:48:02 +08:00
|
|
|
return worker->getProp(CV_CAP_PROP_CONTRAST);
|
2014-10-07 22:59:21 +08:00
|
|
|
case HUE:
|
2014-10-08 21:48:02 +08:00
|
|
|
return worker->getProp(CV_CAP_PROP_HUE);
|
2014-10-07 22:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:25:32 +08:00
|
|
|
void Camera::onNewFrameAvailable()
|
|
|
|
{
|
|
|
|
emit frameAvailable();
|
|
|
|
}
|
|
|
|
|
2014-10-08 21:48:02 +08:00
|
|
|
void Camera::onResProbingFinished(QList<QSize> res)
|
|
|
|
{
|
|
|
|
resolutions = res;
|
|
|
|
}
|
|
|
|
|
2014-10-07 22:59:21 +08:00
|
|
|
void *Camera::getData()
|
|
|
|
{
|
|
|
|
return currFrame.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Camera::getDataSize()
|
|
|
|
{
|
|
|
|
return currFrame.total() * currFrame.channels();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::lock()
|
|
|
|
{
|
|
|
|
mutex.lock();
|
2014-10-08 20:25:32 +08:00
|
|
|
|
|
|
|
if (worker->hasFrame())
|
|
|
|
currFrame = worker->deqeueFrame();
|
2014-10-07 22:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::unlock()
|
|
|
|
{
|
|
|
|
mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize Camera::resolution()
|
|
|
|
{
|
2014-10-08 20:25:32 +08:00
|
|
|
return QSize(currFrame.cols, currFrame.rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
Camera* Camera::getInstance()
|
|
|
|
{
|
|
|
|
if (!instance)
|
|
|
|
instance = new Camera();
|
|
|
|
|
|
|
|
return instance;
|
2014-10-07 22:59:21 +08:00
|
|
|
}
|
|
|
|
|
2014-10-08 20:25:32 +08:00
|
|
|
// ====================
|
|
|
|
// WORKER
|
|
|
|
// ====================
|
|
|
|
|
|
|
|
SelfCamWorker::SelfCamWorker(int index)
|
|
|
|
: clock(nullptr)
|
|
|
|
, camIndex(index)
|
2014-10-08 21:48:02 +08:00
|
|
|
, refCount(0)
|
2014-10-07 22:59:21 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:25:32 +08:00
|
|
|
void SelfCamWorker::onStart()
|
|
|
|
{
|
|
|
|
clock = new QTimer(this);
|
|
|
|
clock->setSingleShot(false);
|
2014-10-08 21:48:02 +08:00
|
|
|
clock->setInterval(5);
|
2014-10-08 20:25:32 +08:00
|
|
|
|
|
|
|
connect(clock, &QTimer::timeout, this, &SelfCamWorker::doWork);
|
|
|
|
|
2014-10-08 21:48:02 +08:00
|
|
|
emit started();
|
2014-10-08 20:25:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SelfCamWorker::_suspend()
|
|
|
|
{
|
|
|
|
qDebug() << "Suspend";
|
2014-10-08 21:48:02 +08:00
|
|
|
clock->stop();
|
|
|
|
unsubscribe();
|
2014-10-08 20:25:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SelfCamWorker::_resume()
|
|
|
|
{
|
|
|
|
qDebug() << "Resume";
|
2014-10-08 21:48:02 +08:00
|
|
|
subscribe();
|
|
|
|
clock->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelfCamWorker::_setProp(int prop, double val)
|
|
|
|
{
|
|
|
|
props[prop] = val;
|
|
|
|
|
|
|
|
if (cam.isOpened())
|
|
|
|
cam.set(prop, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
double SelfCamWorker::_getProp(int prop)
|
|
|
|
{
|
|
|
|
if (!props.contains(prop))
|
|
|
|
{
|
|
|
|
subscribe();
|
|
|
|
props[prop] = cam.get(prop);
|
|
|
|
unsubscribe();
|
|
|
|
qDebug() << "ASKED " << prop << " VAL " << props[prop];
|
|
|
|
}
|
|
|
|
|
|
|
|
return props.value(prop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelfCamWorker::probeResolutions()
|
|
|
|
{
|
|
|
|
if (resolutions.isEmpty())
|
|
|
|
{
|
|
|
|
subscribe();
|
|
|
|
|
|
|
|
// probe resolutions (TODO: add more)
|
|
|
|
QList<QSize> propbeRes = {
|
|
|
|
QSize( 160, 120), // QQVGA
|
|
|
|
QSize( 320, 240), // HVGA
|
|
|
|
QSize(1024, 768), // XGA
|
|
|
|
QSize( 432, 240), // WQVGA
|
|
|
|
QSize( 640, 360), // nHD
|
|
|
|
};
|
|
|
|
|
|
|
|
for (QSize res : propbeRes)
|
|
|
|
{
|
2014-10-08 22:19:42 +08:00
|
|
|
cam.set(CV_CAP_PROP_FRAME_WIDTH, res.width());
|
|
|
|
cam.set(CV_CAP_PROP_FRAME_HEIGHT, res.height());
|
2014-10-08 21:48:02 +08:00
|
|
|
|
2014-10-08 22:19:42 +08:00
|
|
|
double w = cam.get(CV_CAP_PROP_FRAME_WIDTH);
|
|
|
|
double h = cam.get(CV_CAP_PROP_FRAME_HEIGHT);
|
2014-10-08 21:48:02 +08:00
|
|
|
|
2014-10-08 22:19:42 +08:00
|
|
|
//qDebug() << "PROBING:" << res << " got " << w << h;
|
2014-10-08 21:48:02 +08:00
|
|
|
|
2014-10-08 22:19:42 +08:00
|
|
|
if (!resolutions.contains(QSize(w,h)))
|
|
|
|
resolutions.append(QSize(w,h));
|
2014-10-08 21:48:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << resolutions;
|
|
|
|
|
|
|
|
emit resProbingFinished(resolutions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelfCamWorker::applyProps()
|
|
|
|
{
|
2014-10-08 20:25:32 +08:00
|
|
|
if (!cam.isOpened())
|
2014-10-08 21:48:02 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
for(int prop : props.keys())
|
|
|
|
cam.set(prop, props.value(prop));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelfCamWorker::subscribe()
|
|
|
|
{
|
|
|
|
if (refCount == 0)
|
|
|
|
{
|
|
|
|
if (!cam.isOpened())
|
|
|
|
{
|
|
|
|
cam.open(camIndex);
|
|
|
|
applyProps(); // restore props
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelfCamWorker::unsubscribe()
|
|
|
|
{
|
|
|
|
refCount--;
|
|
|
|
|
|
|
|
if(refCount <= 0)
|
|
|
|
{
|
|
|
|
cam.release();
|
|
|
|
}
|
2014-10-08 20:25:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SelfCamWorker::doWork()
|
|
|
|
{
|
|
|
|
if (!cam.isOpened())
|
|
|
|
return;
|
|
|
|
|
2014-10-08 21:48:02 +08:00
|
|
|
if (qeue.size() > 3)
|
|
|
|
{
|
|
|
|
qeue.dequeue();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:25:32 +08:00
|
|
|
cam >> frame;
|
|
|
|
//qDebug() << "Decoding frame";
|
|
|
|
mutex.lock();
|
|
|
|
|
|
|
|
qeue.enqueue(frame);
|
|
|
|
mutex.unlock();
|
|
|
|
|
|
|
|
emit newFrameAvailable();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SelfCamWorker::hasFrame()
|
|
|
|
{
|
|
|
|
mutex.lock();
|
|
|
|
bool b = !qeue.empty();
|
|
|
|
mutex.unlock();
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
cv::Mat3b SelfCamWorker::deqeueFrame()
|
|
|
|
{
|
|
|
|
mutex.lock();
|
|
|
|
cv::Mat3b f = qeue.dequeue();
|
|
|
|
mutex.unlock();
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelfCamWorker::suspend()
|
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(this, "_suspend");
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelfCamWorker::resume()
|
2014-09-11 21:44:34 +08:00
|
|
|
{
|
2014-10-08 20:25:32 +08:00
|
|
|
QMetaObject::invokeMethod(this, "_resume");
|
2014-09-11 21:44:34 +08:00
|
|
|
}
|
2014-10-08 21:48:02 +08:00
|
|
|
|
|
|
|
void SelfCamWorker::setProp(int prop, double val)
|
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(this, "_setProp", Q_ARG(int, prop), Q_ARG(double, val));
|
|
|
|
}
|
|
|
|
|
|
|
|
double SelfCamWorker::getProp(int prop)
|
|
|
|
{
|
|
|
|
double ret = 0.0;
|
|
|
|
QMetaObject::invokeMethod(this, "_getProp", Qt::BlockingQueuedConnection, Q_RETURN_ARG(double, ret), Q_ARG(int, prop));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|