2018-11-04 10:19:17 -05:00
|
|
|
/*******************************************************************************
|
|
|
|
* plugins/VOIP/gui/QVideoDevice.cpp *
|
|
|
|
* *
|
|
|
|
* Copyright (C) 2012 by Retroshare Team <retroshare.project@gmail.com> *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Affero 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 *
|
|
|
|
* GNU Affero General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Affero General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2020-11-22 15:58:56 -05:00
|
|
|
#include <opencv2/opencv.hpp>
|
|
|
|
#include <opencv2/highgui.hpp>
|
|
|
|
#include <opencv2/imgproc/types_c.h>
|
2014-07-13 09:57:25 -04:00
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QPainter>
|
|
|
|
#include "QVideoDevice.h"
|
|
|
|
#include "VideoProcessor.h"
|
|
|
|
|
|
|
|
QVideoInputDevice::QVideoInputDevice(QWidget *parent)
|
2015-05-11 15:40:07 -04:00
|
|
|
:QObject(parent)
|
2014-07-13 09:57:25 -04:00
|
|
|
{
|
|
|
|
_timer = NULL ;
|
|
|
|
_capture_device = NULL ;
|
2015-08-14 16:44:20 -04:00
|
|
|
_video_processor = NULL ;
|
2014-07-13 09:57:25 -04:00
|
|
|
_echo_output_device = NULL ;
|
|
|
|
}
|
|
|
|
|
2015-09-02 22:14:04 -04:00
|
|
|
bool QVideoInputDevice::stopped()
|
|
|
|
{
|
|
|
|
return _timer == NULL ;
|
|
|
|
}
|
|
|
|
|
2014-07-13 09:57:25 -04:00
|
|
|
void QVideoInputDevice::stop()
|
|
|
|
{
|
|
|
|
if(_timer != NULL)
|
|
|
|
{
|
|
|
|
QObject::disconnect(_timer,SIGNAL(timeout()),this,SLOT(grabFrame())) ;
|
|
|
|
_timer->stop() ;
|
|
|
|
delete _timer ;
|
|
|
|
_timer = NULL ;
|
|
|
|
}
|
|
|
|
if(_capture_device != NULL)
|
|
|
|
{
|
2015-10-03 22:08:24 -04:00
|
|
|
// the camera will be deinitialized automatically in VideoCapture destructor
|
|
|
|
_capture_device->release();
|
|
|
|
delete _capture_device ;
|
2014-07-13 09:57:25 -04:00
|
|
|
_capture_device = NULL ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void QVideoInputDevice::start()
|
|
|
|
{
|
2015-10-03 22:08:24 -04:00
|
|
|
// make sure everything is re-initialised
|
2014-07-13 09:57:25 -04:00
|
|
|
//
|
|
|
|
stop() ;
|
|
|
|
|
2015-10-03 22:08:24 -04:00
|
|
|
// Initialise la capture
|
2014-07-13 09:57:25 -04:00
|
|
|
static const int cam_id = 0 ;
|
2015-10-03 22:08:24 -04:00
|
|
|
_capture_device = new cv::VideoCapture(cam_id);
|
2014-07-13 09:57:25 -04:00
|
|
|
|
2015-10-03 22:08:24 -04:00
|
|
|
if(!_capture_device->isOpened())
|
2014-07-13 09:57:25 -04:00
|
|
|
{
|
|
|
|
std::cerr << "Cannot initialise camera. Something's wrong." << std::endl;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
_timer = new QTimer ;
|
|
|
|
QObject::connect(_timer,SIGNAL(timeout()),this,SLOT(grabFrame())) ;
|
|
|
|
|
|
|
|
_timer->start(50) ; // 10 images per second.
|
|
|
|
}
|
|
|
|
|
|
|
|
void QVideoInputDevice::grabFrame()
|
|
|
|
{
|
2015-09-02 22:14:04 -04:00
|
|
|
if(!_timer)
|
|
|
|
return ;
|
2014-07-13 09:57:25 -04:00
|
|
|
|
2015-10-03 22:08:24 -04:00
|
|
|
cv::Mat frame;
|
|
|
|
if(!_capture_device->read(frame))
|
2015-08-09 18:09:17 -04:00
|
|
|
{
|
2015-10-03 22:08:24 -04:00
|
|
|
std::cerr << "(EE) Cannot capture image from camera. Something's wrong." << std::endl;
|
|
|
|
return ;
|
2015-08-09 18:09:17 -04:00
|
|
|
}
|
2015-10-03 22:08:24 -04:00
|
|
|
|
2015-08-09 18:09:17 -04:00
|
|
|
// get the image data
|
2014-07-13 09:57:25 -04:00
|
|
|
|
2015-10-03 22:08:24 -04:00
|
|
|
if(frame.channels() != 3)
|
2015-08-09 18:09:17 -04:00
|
|
|
{
|
2015-10-03 22:08:24 -04:00
|
|
|
std::cerr << "(EE) expected 3 channels. Got " << frame.channels() << std::endl;
|
|
|
|
return ;
|
2015-08-09 18:09:17 -04:00
|
|
|
}
|
2014-07-13 09:57:25 -04:00
|
|
|
|
2014-12-30 10:42:15 -05:00
|
|
|
// convert to RGB and copy to new buffer, because cvQueryFrame tells us to not modify the buffer
|
|
|
|
cv::Mat img_rgb;
|
2015-10-03 22:08:24 -04:00
|
|
|
cv::cvtColor(frame, img_rgb, CV_BGR2RGB);
|
2015-08-09 18:09:17 -04:00
|
|
|
QImage image = QImage(img_rgb.data,img_rgb.cols,img_rgb.rows,QImage::Format_RGB888);
|
2014-07-13 09:57:25 -04:00
|
|
|
|
2015-10-03 22:08:24 -04:00
|
|
|
if(_video_processor != NULL)
|
2015-08-09 18:09:17 -04:00
|
|
|
{
|
2015-10-03 22:08:24 -04:00
|
|
|
_video_processor->processImage(image) ;
|
2015-08-09 18:09:17 -04:00
|
|
|
|
2015-10-03 22:08:24 -04:00
|
|
|
emit networkPacketReady() ;
|
2015-08-09 18:09:17 -04:00
|
|
|
}
|
2015-10-03 22:08:24 -04:00
|
|
|
if(_echo_output_device != NULL)
|
|
|
|
_echo_output_device->showFrame(image) ;
|
2014-07-13 09:57:25 -04:00
|
|
|
}
|
|
|
|
|
2015-05-11 15:40:07 -04:00
|
|
|
bool QVideoInputDevice::getNextEncodedPacket(RsVOIPDataChunk& chunk)
|
2014-07-15 16:04:31 -04:00
|
|
|
{
|
2015-09-02 22:14:04 -04:00
|
|
|
if(!_timer)
|
|
|
|
return false ;
|
2015-10-03 22:08:24 -04:00
|
|
|
|
2015-08-14 16:44:20 -04:00
|
|
|
if(_video_processor)
|
2015-10-03 22:08:24 -04:00
|
|
|
return _video_processor->nextEncodedPacket(chunk) ;
|
|
|
|
else
|
2015-08-24 21:15:33 -04:00
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t QVideoInputDevice::currentBandwidth() const
|
|
|
|
{
|
2015-10-03 22:08:24 -04:00
|
|
|
return _video_processor->currentBandwidthOut() ;
|
2014-07-15 16:04:31 -04:00
|
|
|
}
|
|
|
|
|
2014-07-13 09:57:25 -04:00
|
|
|
QVideoInputDevice::~QVideoInputDevice()
|
|
|
|
{
|
2015-09-02 22:14:04 -04:00
|
|
|
stop() ;
|
|
|
|
_video_processor = NULL ;
|
2014-07-13 09:57:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QVideoOutputDevice::QVideoOutputDevice(QWidget *parent)
|
2015-10-03 22:08:24 -04:00
|
|
|
: QLabel(parent)
|
2014-07-20 16:50:36 -04:00
|
|
|
{
|
|
|
|
showFrameOff() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QVideoOutputDevice::showFrameOff()
|
2014-07-13 09:57:25 -04:00
|
|
|
{
|
2015-10-02 20:11:52 -04:00
|
|
|
setPixmap(QPixmap(":/images/video-icon-big.png").scaled(QSize(height()*4/3,height()),Qt::KeepAspectRatio,Qt::SmoothTransformation)) ;
|
2015-10-06 19:35:31 -04:00
|
|
|
setAlignment(Qt::AlignCenter);
|
2014-07-13 09:57:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void QVideoOutputDevice::showFrame(const QImage& img)
|
|
|
|
{
|
2015-08-08 18:46:49 -04:00
|
|
|
std::cerr << "img.size = " << img.width() << " x " << img.height() << std::endl;
|
2015-10-03 22:08:24 -04:00
|
|
|
setPixmap(QPixmap::fromImage(img).scaled( QSize(height()*4/3,height()),Qt::IgnoreAspectRatio,Qt::SmoothTransformation)) ;
|
2014-07-13 09:57:25 -04:00
|
|
|
}
|
|
|
|
|