1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

VideoSurface: aspect ratio

This commit is contained in:
krepa098 2014-10-11 20:53:40 +02:00
parent 9df5ab02ec
commit 1d4055858f
4 changed files with 165 additions and 104 deletions

View File

@ -24,10 +24,7 @@ AVForm::AVForm() :
bodyUI = new Ui::AVSettings; bodyUI = new Ui::AVSettings;
bodyUI->setupUi(this); bodyUI->setupUi(this);
//cam->setVideoMode(cam->getBestVideoMode()); bodyUI->CamVideoSurface->setSource(Camera::getInstance());
camView = new VideoSurface(Camera::getInstance(), this);
bodyUI->CamViewLayout->addWidget(camView);
} }
AVForm::~AVForm() AVForm::~AVForm()

View File

@ -6,14 +6,14 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>394</width> <width>390</width>
<height>391</height> <height>387</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Form</string> <string>Form</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout_2">
<item> <item>
<widget class="QGroupBox" name="groupBox"> <widget class="QGroupBox" name="groupBox">
<property name="title"> <property name="title">
@ -56,6 +56,33 @@
<property name="title"> <property name="title">
<string>Video settings</string> <string>Video settings</string>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout" stretch="0">
<item>
<widget class="QFrame" name="CamFrame">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>1</horstretch>
<verstretch>99</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="VideoSurface" name="CamVideoSurface" native="true"/>
</item>
</layout>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QFormLayout" name="formLayout"> <layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy"> <property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum> <enum>QFormLayout::ExpandingFieldsGrow</enum>
@ -133,15 +160,7 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="5" column="0"> </layout>
<widget class="QLabel" name="label_6">
<property name="text">
<string>Preview</string>
</property>
</widget>
</item>
<item row="5" column="1">
<layout class="QVBoxLayout" name="CamViewLayout"/>
</item> </item>
</layout> </layout>
</widget> </widget>
@ -161,6 +180,14 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<customwidgets>
<customwidget>
<class>VideoSurface</class>
<extends>QWidget</extends>
<header>widget/videosurface.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/> <resources/>
<connections/> <connections/>
</ui> </ui>

View File

@ -22,17 +22,24 @@
#include <QOpenGLShaderProgram> #include <QOpenGLShaderProgram>
#include <QDebug> #include <QDebug>
VideoSurface::VideoSurface(VideoSource *Source, QWidget* parent) VideoSurface::VideoSurface(QWidget* parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent) : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
, source(Source) , source(nullptr)
, pbo(nullptr) , pbo(nullptr)
, program(nullptr) , program(nullptr)
, textureId(0) , textureId(0)
, pboAllocSize(0) , pboAllocSize(0)
, uploadFrame(false) , uploadFrame(false)
, hasSubscribed(false) , hasSubscribed(false)
, lastWidth(0)
{ {
setFixedSize(source->resolution());
}
VideoSurface::VideoSurface(VideoSource *Source, QWidget* parent)
: VideoSurface(parent)
{
source = Source;
} }
VideoSurface::~VideoSurface() VideoSurface::~VideoSurface()
@ -43,12 +50,18 @@ VideoSurface::~VideoSurface()
if (textureId != 0) if (textureId != 0)
glDeleteTextures(1, &textureId); glDeleteTextures(1, &textureId);
if (source && hasSubscribed)
source->unsubscribe(); source->unsubscribe();
} }
void VideoSurface::setSource(VideoSource *src)
{
source = src;
}
void VideoSurface::hideEvent(QHideEvent *ev) void VideoSurface::hideEvent(QHideEvent *ev)
{ {
if (hasSubscribed) if (source && hasSubscribed)
{ {
source->unsubscribe(); source->unsubscribe();
hasSubscribed = false; hasSubscribed = false;
@ -60,7 +73,7 @@ void VideoSurface::hideEvent(QHideEvent *ev)
void VideoSurface::showEvent(QShowEvent *ev) void VideoSurface::showEvent(QShowEvent *ev)
{ {
if (!hasSubscribed) if (source && !hasSubscribed)
{ {
source->subscribe(); source->subscribe();
hasSubscribed = true; hasSubscribed = true;
@ -70,6 +83,11 @@ void VideoSurface::showEvent(QShowEvent *ev)
QGLWidget::showEvent(ev); QGLWidget::showEvent(ev);
} }
QSize VideoSurface::sizeHint() const
{
return QGLWidget::sizeHint();
}
void VideoSurface::initializeGL() void VideoSurface::initializeGL()
{ {
@ -77,9 +95,12 @@ void VideoSurface::initializeGL()
void VideoSurface::paintGL() void VideoSurface::paintGL()
{ {
if (!source)
return;
if (!pbo) if (!pbo)
{ {
qDebug() << "Creating pbo, program"; qDebug() << "VideoSurface: Init";
// pbo // pbo
pbo = new QOpenGLBuffer(QOpenGLBuffer::PixelUnpackBuffer); pbo = new QOpenGLBuffer(QOpenGLBuffer::PixelUnpackBuffer);
@ -95,6 +116,8 @@ void VideoSurface::paintGL()
" gl_Position = vec4(vertices.xy,0.0,1.0);" " gl_Position = vec4(vertices.xy,0.0,1.0);"
" coords = vertices.xy*vec2(0.5,0.5)+vec2(0.5,0.5);" " coords = vertices.xy*vec2(0.5,0.5)+vec2(0.5,0.5);"
"}"); "}");
// brg frag-shader
program->addShaderFromSourceCode(QOpenGLShader::Fragment, program->addShaderFromSourceCode(QOpenGLShader::Fragment,
"uniform sampler2D texture0;" "uniform sampler2D texture0;"
"varying vec2 coords;" "varying vec2 coords;"
@ -109,7 +132,7 @@ void VideoSurface::paintGL()
if (res != source->resolution()) if (res != source->resolution())
{ {
qDebug() << "Change resolution " << res << " to " << source->resolution(); qDebug() << "VideoSurface: Change resolution from " << res << " to " << source->resolution();
res = source->resolution(); res = source->resolution();
// a texture used to render the pbo (has the match the pixelformat of the source) // a texture used to render the pbo (has the match the pixelformat of the source)
@ -118,7 +141,7 @@ void VideoSurface::paintGL()
glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, res.width(), res.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, 0); glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, res.width(), res.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
setFixedSize(res); updateGeometry();
} }
@ -130,7 +153,7 @@ void VideoSurface::paintGL()
if (pboAllocSize != frameBytes && frameBytes > 0) if (pboAllocSize != frameBytes && frameBytes > 0)
{ {
qDebug() << "Resize pbo " << frameBytes << "bytes (was" << pboAllocSize << ") res " << source->resolution(); qDebug() << "VideoSurface: Resize pbo " << frameBytes << "bytes (before" << pboAllocSize << ")";
pbo->bind(); pbo->bind();
pbo->allocate(frameBytes); pbo->allocate(frameBytes);
@ -169,11 +192,21 @@ void VideoSurface::paintGL()
program->setAttributeArray(0, GL_FLOAT, values, 2); program->setAttributeArray(0, GL_FLOAT, values, 2);
glClearColor(0, 0, 0, 1); glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, width(), height()); // keep aspect ratio
float aspectRatio = float(res.width()) / float(res.height());
if (width() < float(height()) * aspectRatio)
{
float h = float(width()) / aspectRatio;
glViewport(0, (height() - h)*0.5f, width(), h);
}
else
{
float w = float(height()) * float(aspectRatio);
glViewport((width() - w)*0.5f, 0, w, height());
}
program->bind(); program->bind();
program->enableAttributeArray(0); program->enableAttributeArray(0);
@ -195,3 +228,4 @@ void VideoSurface::updateGL()
} }

View File

@ -29,11 +29,14 @@ class VideoSurface : public QGLWidget
Q_OBJECT Q_OBJECT
public: public:
VideoSurface(QWidget* parent=0);
VideoSurface(VideoSource* source, QWidget* parent=0); VideoSurface(VideoSource* source, QWidget* parent=0);
~VideoSurface(); ~VideoSurface();
void setSource(VideoSource* src);
virtual void hideEvent(QHideEvent* ev); virtual void hideEvent(QHideEvent* ev);
virtual void showEvent(QShowEvent* ev); virtual void showEvent(QShowEvent* ev);
virtual QSize sizeHint() const;
// QGLWidget interface // QGLWidget interface
protected: protected:
@ -52,7 +55,7 @@ private:
QSize res; QSize res;
bool uploadFrame; bool uploadFrame;
bool hasSubscribed; bool hasSubscribed;
mutable int lastWidth;
}; };
#endif // SELFCAMVIEW_H #endif // SELFCAMVIEW_H