[英]Rendering texture (video) in OpenGL in a blocking way so my received video frame won't get replaced by new one while rendering
[英]Rendering video in QT/OpenGL using updatePaintNode: new frame only appears when I resize screen
我正在Qt中編寫OpenGL視頻渲染器,並且幾乎可以正常工作。 如果我調整窗口大小,它只會渲染視頻。 因此,每次我調整窗口大小時,屏幕上都會繪制一個新框架。 我想我忘了在更新框架后調用某些函數。
#include <QtQuick/QQuickItem>
#include <qguiapplication.h>
#include <qsgmaterial.h>
#include <qsgnode.h>
#include <qquickitem.h>
#include <qquickview.h>
#include <qsgsimplerectnode.h>
#include <qsgsimplematerial.h>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include "OpenGlMaterialQQuickItem.h"
#include <iostream>
#define GET_STR(x) #x
#define VERTEX_ATTRIBUTE 3
struct State
{
public:
int frameWidth = 1920;
int frameHeight = 1080;
unsigned char *datas[3] = {0};
private:
bool firstRender = true;
public:
void updateData(unsigned char**data, int frameWidth, int frameHeight)
{
this->frameWidth = frameWidth;
this->frameHeight = frameHeight;
if (firstRender) {
datas[0] = new unsigned char[frameWidth*frameHeight]; //Y
datas[1] = new unsigned char[frameWidth*frameHeight/4];//U
datas[2] = new unsigned char[frameWidth*frameHeight/4];//V
firstRender = false;
}
memcpy(datas[0], data[0], frameWidth*frameHeight);
memcpy(datas[1], data[1], frameWidth*frameHeight/4);
memcpy(datas[2], data[2], frameWidth*frameHeight/4);
};
class Shader : public QSGSimpleMaterialShader<State>
{
QSG_DECLARE_SIMPLE_SHADER(Shader, State);
private:
QOpenGLFunctions *glFuncs = nullptr;
GLuint unis[3] = {0};
GLuint texs[3] = {0};
QSize m_viewportSize;
bool firstRender = true;
public:
const char *vertexShader() const override {
return GET_STR(
uniform highp mat4 qt_Matrix;
attribute vec4 vertexIn;
attribute vec2 textureIn;
varying vec2 textureOut;
void main(void)
{
gl_Position = qt_Matrix * vertexIn;
textureOut = textureIn;
}
);
}
const char *fragmentShader() const override {
return GET_STR(
varying vec2 textureOut;
uniform sampler2D tex_y;
uniform sampler2D tex_u;
uniform sampler2D tex_v;
uniform lowp float qt_Opacity;
void main(void)
{
vec3 yuv;
vec3 rgb;
yuv.x = texture2D(tex_y, textureOut).r;
yuv.y = texture2D(tex_u, textureOut).r - 0.5;
yuv.z = texture2D(tex_v, textureOut).r - 0.5;
rgb = mat3(1.0, 1.0, 1.0,
0.0, -0.39465, 2.03211,
1.13983, -0.58060, 0.0) * yuv;
gl_FragColor = vec4(rgb, 1.0) * qt_Opacity;
}
);
}
QList<QByteArray> attributes() const override
{
return {QByteArrayLiteral("vertexIn"), QByteArrayLiteral("textureIn")};
}
void initialize()
{
if (!program()->isLinked()) {
return;
}
QSGSimpleMaterialShader<State>::initialize();
glFuncs = QOpenGLContext::currentContext()->functions();
program()->bind();
glFuncs->glGenTextures(3, texs);
}
void updateState(const State *state, const State *) override
{
//Y
glFuncs->glBindTexture(GL_TEXTURE_2D, texs[0]);
glFuncs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFuncs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glFuncs->glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, state->frameWidth, state->frameHeight, 0, GL_RED, GL_UNSIGNED_BYTE, 0);
//U
glFuncs->glBindTexture(GL_TEXTURE_2D, texs[1]);
glFuncs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFuncs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glFuncs->glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, state->frameWidth/2, state->frameHeight / 2, 0, GL_RED, GL_UNSIGNED_BYTE, 0);
//V
glFuncs->glBindTexture(GL_TEXTURE_2D, texs[2]);
glFuncs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFuncs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glFuncs->glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, state->frameWidth / 2, state->frameHeight / 2, 0, GL_RED, GL_UNSIGNED_BYTE, 0);
glFuncs->glActiveTexture(GL_TEXTURE0);
glFuncs->glBindTexture(GL_TEXTURE_2D, texs[0]);
glFuncs->glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, state->frameWidth, state->frameHeight, GL_RED, GL_UNSIGNED_BYTE, state->datas[0]);
glFuncs->glUniform1i(unis[0], 0);
glFuncs->glActiveTexture(GL_TEXTURE0+1);
glFuncs->glBindTexture(GL_TEXTURE_2D, texs[1]);
glFuncs->glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, state->frameWidth/2, state->frameHeight / 2, GL_RED, GL_UNSIGNED_BYTE, state->datas[1]);
glFuncs->glUniform1i(unis[1],1);
glFuncs->glActiveTexture(GL_TEXTURE0+2);
glFuncs->glBindTexture(GL_TEXTURE_2D, texs[2]);
glFuncs->glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, state->frameWidth / 2, state->frameHeight / 2, GL_RED, GL_UNSIGNED_BYTE, state->datas[2]);
glFuncs->glUniform1i(unis[2], 2);
glFuncs->glDrawArrays(GL_TRIANGLE_STRIP,0,4);
}
void resolveUniforms() override
{
unis[0] = program()->uniformLocation("tex_y");
unis[1] = program()->uniformLocation("tex_u");
unis[2] = program()->uniformLocation("tex_v");
}
};
class Node: public QSGGeometryNode, public FrameUpdater
{
public:
State state;
//QQuickItem* item;
Node()
{
material = Shader::createMaterial();
setMaterial(material);
setFlag(OwnsMaterial, true);
QSGGeometry *g = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4);
QSGGeometry::updateTexturedRectGeometry(g, QRect(), QRect());
setGeometry(g);
setFlag(QSGNode::OwnsGeometry, true);
stream = new MediaStream("rtsp://admin:19929394@192.168.1.178:10554/tcp/av0_0");
stream->setFrameUpdater((FrameUpdater *) this);
boost::thread mediaThread(&MediaStream::run, stream);
}
void updateData(unsigned char**data, int frameWidth, int frameHeight)
{
material->state()->updateData(data, frameWidth, frameHeight);
markDirty(QSGNode::DirtyMaterial);
//item->update();
}
private:
QSGSimpleMaterial<State> *material;
MediaStream* stream;
};
QSGNode * OpenGlMaterialQQuickItem::updatePaintNode(QSGNode *node, UpdatePaintNodeData *) //override
{
std::cout << "updatePaintNode called " << std::endl;
Node *n = static_cast<Node *>(node);
if (!node)
n = new Node();
//n->item = this;
QSGGeometry::updateTexturedRectGeometry(n->geometry(), boundingRect(), QRectF(0, 0, 1, 1));
n->markDirty(QSGNode::DirtyGeometry | QSGNode::DirtyMaterial);
return n;
}
要了解代碼如何更新視頻幀,請執行以下操作: boost::thread mediaThread(&MediaStream::run, stream);
創建一個線程,該線程將通過void Node::updateData(unsigned char**data, int frameWidth, int frameHeight)
插入視頻數據。 此數據寫入State
結構中。
唯一缺少的是一種告訴Qt它需要繪制另一幀的方法。 什么東西少了?
PS:這是QQuickItem
:
class OpenGlMaterialQQuickItem: public ReactItem
{
//Q_OBJECT
Q_PROPERTY(QString uri WRITE setUri)// NOTIFY uriChanged)
public:
std::string uri;
QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *) override;
OpenGlMaterialQQuickItem()
{
setFlag(ItemHasContents, true);
}
void setUri(const QString &a) {
uri = a.toStdString();
}
};
根據Qt文檔,這是通過調用QQuickItem::update()
。 請注意,必須QQuickItem::ItemHasContents
在項目上設置標志QQuickItem::ItemHasContents
。
取消注釋item->update();
您得到QObject::startTimer: Timers cannot be started from another thread
由於以下兩種原因, QObject::startTimer: Timers cannot be started from another thread
:
QQuickItem::update()
將請求轉發到窗口,然后轉發到平台適配。 在某些(所有?)情況下,實現使用QTimer安排重新繪制。
您調用item->update();
不是來自GUI線程。 您可能直接從用於閱讀視頻的增強線程中調用它。
所以最后, item->update();
無法啟動計時器,沒有任何反應。
一種解決方案是確保您從GUI線程調用QQuickItem::update()
。
也許這應該工作:
QMetaObject::invokeMethod(item, &QQuickItem::update, Qt::QueuedConnection);
這應該從item->thread()
調用QQuickItem::update()
item->thread()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.