-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideowidget.cpp
More file actions
138 lines (123 loc) · 4.45 KB
/
Copy pathvideowidget.cpp
File metadata and controls
138 lines (123 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "videowidget.h"
VideoWidget::VideoWidget(QWidget *parent) :
QOpenGLWidget(parent),
m_videoTexture(nullptr)
{;}
VideoWidget::~VideoWidget()
{
if(m_VAO.isCreated())
{
makeCurrent();
if(m_videoTexture)
delete m_videoTexture;
m_VAO.release();
}
}
void VideoWidget::setFrame(const QVideoFrame &frame)
{
// qDebug("Setting Frame");
QVideoFrame localFrame = frame;
localFrame.map(QAbstractVideoBuffer::ReadOnly);
makeCurrent();
if(
!m_videoTexture ||
m_videoTexture->width() != localFrame.width() ||
m_videoTexture->height() != localFrame.height()
)
{
if(m_videoTexture)
delete m_videoTexture;
qDebug("Creating m_videoTexture %dX%d",localFrame.width(), localFrame.height());
m_videoTexture = new QOpenGLTexture(QOpenGLTexture::Target2D);
m_videoTexture->setSize(localFrame.width(), localFrame.height());
m_videoTexture->setFormat(QOpenGLTexture::RGBA32F);
m_videoTexture->setWrapMode(QOpenGLTexture::ClampToBorder);
m_videoTexture->setMinificationFilter(QOpenGLTexture::Nearest);
m_videoTexture->setMagnificationFilter(QOpenGLTexture::Nearest);
m_videoTexture->allocateStorage();
}
QVideoFrame::PixelFormat format = localFrame.pixelFormat();
if(format == QVideoFrame::Format_ARGB32)
{
unsigned int bytesPerSample = localFrame.bytesPerLine() / localFrame.width() / 4;
if(bytesPerSample == sizeof(unsigned char))
m_videoTexture->setData(QOpenGLTexture::BGRA, QOpenGLTexture::UInt8, (const void *)localFrame.bits());
}
localFrame.unmap();
process();
update();
}
void VideoWidget::initializeGL(){initialize();}
void VideoWidget::initialize()
{
initializeOpenGLFunctions();
glClearColor(0.4f,0.1f,0.2f,1.0f);
m_VAO.create();
m_VAO.bind();
// Vertex Buffer
m_VBO = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
m_VBO.create();
m_VBO.setUsagePattern(QOpenGLBuffer::StaticDraw);
m_VBO.bind();
m_VBO.allocate(16*sizeof(float));
const float _vBuffer[16] = {-1.0,-1.0,0.0,1.0,+1.0,-1.0,0.0,1.0,+1.0,+1.0,0.0,1.0,-1.0,+1.0,0.0,1.0};
m_VBO.write(0,(const void*)_vBuffer,16*sizeof(float));
m_VBO.release();
// Index Buffer
m_IBO = QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
m_IBO.create();
m_IBO.setUsagePattern(QOpenGLBuffer::StaticDraw);
m_IBO.bind();
m_IBO.allocate(6*sizeof(unsigned int));
const unsigned int _iBuffer[6] = {0,1,2,0,2,3};
m_IBO.write(0,(const void*)_iBuffer,6*sizeof(unsigned int));
m_IBO.release();
QString shaderDirectory = SHADER_DIR;
m_program.addShaderFromSourceFile(QOpenGLShader::Vertex, shaderDirectory+"displayRGBVideo.vert");
m_program.addShaderFromSourceFile(QOpenGLShader::Fragment, shaderDirectory+"displayRGBVideo.frag");
m_program.link();
m_program.bind();
m_program.setUniformValue("qt_flip",true);
m_program.release();
qDebug("VideoWidget::initialize");
initializeStreamer();
}
void VideoWidget::resizeGL(int W, int H){resize(W,H);}
void VideoWidget::resize(int W,int H)
{
QDesktopWidget *dkWidget = QApplication::desktop();
QList<QScreen *> screenList = QGuiApplication::screens();
float devicePixelRatio = screenList[dkWidget->screenNumber(this)]->devicePixelRatio();
qDebug("RESIZING %dX%d|%f",W,H,devicePixelRatio);
m_frameHeight = H * devicePixelRatio;
m_frameWidth = W * devicePixelRatio;
}
void VideoWidget::paintGL(){paint();}
void VideoWidget::paint()
{
// qDebug("Paint");
glViewport(0, 0, m_frameWidth, m_frameHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(!m_videoTexture) return;
if(!m_program.bind()) return;
if(!m_VBO.bind()) return;
if(!m_IBO.bind()) return;
m_videoTexture->bind();
m_program.setUniformValue("qt_texture",0);
m_program.setAttributeBuffer("qt_vertex",GL_FLOAT,0,4,4*sizeof(float));
m_program.enableAttributeArray("qt_vertex");
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
m_videoTexture->release();
m_IBO.release();
m_VBO.release();
m_program.release();
}
void VideoWidget::initializeStreamer()
{
m_streamThread = new QThread();
m_streamer = new Streamer();
m_streamer->moveToThread(m_streamThread);
connect(m_streamThread,&QThread::started,m_streamer,&Streamer::initialize);
connect(this,&VideoWidget::streamData, m_streamer,&Streamer::streamData);
m_streamThread->start();
}