added the possiblity to choose which camera to use

This commit is contained in:
csoler 2021-05-27 21:21:26 +02:00
parent 7b5be347bf
commit b78f7f11de
4 changed files with 116 additions and 4 deletions

View File

@ -28,6 +28,8 @@
#include "QVideoDevice.h"
#include "VideoProcessor.h"
// #define DEBUG_QVIDEODEVICE 1
QVideoInputDevice::QVideoInputDevice(QWidget *parent)
:QObject(parent)
{
@ -54,6 +56,8 @@ bool QVideoInputDevice::stopped()
void QVideoInputDevice::stop()
{
_capture_device_info = QCameraInfo();
if(_timer != NULL)
{
_capture_device->stop();
@ -70,6 +74,8 @@ void QVideoInputDevice::stop()
_capture_device = NULL ;
_image_capture = NULL ;
}
if(_echo_output_device != NULL)
_echo_output_device->showFrameOff() ;
}
void QVideoInputDevice::getAvailableDevices(QList<QString>& device_desc)
{
@ -105,6 +111,7 @@ void QVideoInputDevice::start(const QString& description)
std::cerr << "No video camera available in this system!" << std::endl;
return ;
}
_capture_device_info = caminfo;
_capture_device = new QCamera(caminfo);
if(_capture_device->error() != QCamera::NoError)
@ -144,8 +151,9 @@ void QVideoInputDevice::start(const QString& description)
void QVideoInputDevice::errorHandling(CameraStatus status,QCamera::Error error)
{
#ifdef DEBUG_QVIDEODEVICE
std::cerr << "Received msg from camera capture: status=" << (int)status << " error=" << (int)error << std::endl;
#endif
if(status == CANNOT_INITIALIZE_CAMERA)
{
std::cerr << "Cannot initialize camera. Make sure to install package libqt5multimedia5-plugins, as this is a common cause for camera not being found." << std::endl;
@ -169,7 +177,9 @@ void QVideoInputDevice::grabFrame(int id,QVideoFrame frame)
reader.setScaledSize(QSize(640,480));
QImage image(reader.read());
#ifdef DEBUG_QVIDEODEVICE
std::cerr << "Frame " << id << ". Pixel format: " << frame.pixelFormat() << ". Size: " << image.size().width() << " x " << image.size().height() << std::endl; // if(frame.pixelFormat() != QVideoFrame::Format_Jpeg)
#endif
if(_video_processor != NULL)
{
@ -211,7 +221,9 @@ void QVideoOutputDevice::showFrameOff()
void QVideoOutputDevice::showFrame(const QImage& img)
{
#ifdef DEBUG_QVIDEODEVICE
std::cerr << "img.size = " << img.width() << " x " << img.height() << std::endl;
#endif
setPixmap(QPixmap::fromImage(img).scaled( QSize(height()*4/3,height()),Qt::IgnoreAspectRatio,Qt::SmoothTransformation)) ;
}

View File

@ -22,6 +22,7 @@
#include <QLabel>
#include <QCamera>
#include <QCameraInfo>
#include "interface/rsVOIP.h"
#include "gui/VideoProcessor.h"
@ -85,6 +86,7 @@ class QVideoInputDevice: public QObject
static void getAvailableDevices(QList<QString>& device_desc);
QString currentCameraDescriptionString() const { return _capture_device_info.deviceName(); }
protected slots:
void grabFrame(int id, QVideoFrame f) ;
void errorHandling(CameraStatus status,QCamera::Error error);
@ -98,6 +100,7 @@ protected slots:
QTimer *_timer ;
QCamera *_capture_device;
QCameraImageCapture *_image_capture;
QCameraInfo _capture_device_info;
QVideoOutputDevice *_echo_output_device ;

View File

@ -25,7 +25,8 @@
#include "audiodevicehelper.h"
#include "AudioWizard.h"
#include "gui/VideoProcessor.h"
#include "gui/common/RSGraphWidget.h"
#include "gui/VideoProcessor.h"
#include "util/misc.h"
#include "util/RsProtectedTimer.h"
#include <interface/rsVOIP.h>
@ -104,9 +105,13 @@ VOIPConfigPanel::VOIPConfigPanel(QWidget * parent, Qt::WindowFlags flags)
QList<QString> input_devices;
QVideoInputDevice::getAvailableDevices(input_devices);
ui.inputDevice_CB->clear();
ui.inputDevice_CB->addItem(tr("[No video]"),QString(""));
for(auto& s:input_devices)
ui.inputDevice_CB->addItem(s,QVariant(s));
if(!input_devices.empty())
whileBlocking(ui.inputDevice_CB)->setCurrentIndex(1); // select default cam
connect( ui.qsTransmitHold, SIGNAL( valueChanged ( int ) ), this, SLOT( on_qsTransmitHold_valueChanged(int) ) );
connect( ui.qsNoise, SIGNAL( valueChanged ( int ) ), this, SLOT( on_qsNoise_valueChanged(int) ) );
connect( ui.qsAmp, SIGNAL( valueChanged ( int ) ), this, SLOT( on_qsAmp_valueChanged(int) ) );
@ -367,3 +372,92 @@ void VOIPConfigPanel::on_qpbAudioWizard_clicked() {
aw.exec();
loadSettings();
}
void VOIPConfigPanel::on_changedCurrentInputDevice(int i)
{
QString s = dynamic_cast<QComboBox*>(sender())->itemData(i).toString();
videoInput->stop();
// check that the camera still exists
QList<QString> input_devices;
QVideoInputDevice::getAvailableDevices(input_devices);
for(const QString& cams:input_devices)
if(s == cams)
{
std::cerr << "Switching to camera \"" << s.toStdString() << "\"" << std::endl;
videoInput->start(s);
return;
}
// if not, re-create the ComboBox
checkAvailableCameras();
}
void VOIPConfigPanel::checkAvailableCameras()
{
// save current camera
QString current_cam = videoInput->currentCameraDescriptionString();
// Check that the list of cams we had previously is the same than the list of available camera.
QList<QString> input_devices;
QVideoInputDevice::getAvailableDevices(input_devices);
bool same = true;
if(input_devices.size() != ui.inputDevice_CB->count())
same = false;
if(same)
{
int n=0;
for(auto& s:input_devices)
{
if(ui.inputDevice_CB->itemData(n).toString() != s)
{
same = false;
break;
}
n++;
}
}
// If not, re-add them to the comboBox and make sure to re-select the same camera.
if(!same)
{
whileBlocking(ui.inputDevice_CB)->clear(); // remove existing entries
whileBlocking(ui.inputDevice_CB)->addItem(tr("[No video]"),QString(""));
int n=0;
int found_index = -1;
for(auto& s:input_devices)
{
whileBlocking(ui.inputDevice_CB)->addItem(s,QVariant(s));
if(s == current_cam)
found_index = n;
n++;
}
if(found_index >= 0)
ui.inputDevice_CB->setCurrentIndex(found_index);
else
ui.inputDevice_CB->setCurrentIndex(0); // no video
}
}

View File

@ -59,8 +59,9 @@ class VOIPConfigPanel : public ConfigPage
//VideoDecoder *videoDecoder ;
//VideoEncoder *videoEncoder ;
QVideoInputDevice *videoInput ;
VideoProcessor *videoProcessor ;
VideoProcessor *videoProcessor ;
bool loaded;
QString currentCameraDescription;
voipGraphSource *graph_source ;
@ -86,7 +87,9 @@ class VOIPConfigPanel : public ConfigPage
virtual void showEvent(QShowEvent *) override;
virtual void hideEvent(QHideEvent *event) override;
private slots:
void updateAvailableBW(double r);
void on_changedCurrentInputDevice(int i);
void checkAvailableCameras();
void updateAvailableBW(double r);
void loadSettings();
void emptyBuffer();
void togglePreview(bool) ;