Peter Akakpo
New Member
good morning everyone
am experimenting with QML and wants the best and efficient way to preview video (display) using a sub class of QQuickItem. this is what i came up with
attached is the full source codes of the MainVideoOut. this works, but i want to know if it is the most efficient way to do this with the OBS library. will be glad if someone will offer me a sample code showing how to best achieve this
am experimenting with QML and wants the best and efficient way to preview video (display) using a sub class of QQuickItem. this is what i came up with
attached is the full source codes of the MainVideoOut. this works, but i want to know if it is the most efficient way to do this with the OBS library. will be glad if someone will offer me a sample code showing how to best achieve this
Code:
MainVideoOut::MainVideoOut(QQuickItem *parent) : QQuickItem(parent)
{
setFlag(ItemHasContents, true);
obs_add_raw_video_callback(nullptr, render, this);
}
MainVideoOut::~MainVideoOut()
{
obs_remove_raw_video_callback(render, this);
}
void MainVideoOut::render(void *data, video_data *frame)
{
MainVideoOut *videoOut = static_cast<MainVideoOut *>(data);
videoOut->_frame =
frame QMetaObject::invokeMethod(videoOut, "updateFrame");
}
void MainVideoOut::updateFrame()
{
update();
}
QSGNode *MainVideoOut::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
{
if (_frame != nullptr) {
QSGSimpleTextureNode *node =
static_cast<QSGSimpleTextureNode *>(oldNode);
if (!node) {
node = new QSGSimpleTextureNode();
}
obs_video_info vi obs_get_video_info(&vi);
QImage image(reinterpret_cast<uchar *>(_frame->data[0]),
vi.base_width, vi.base_height,
QImage::Format::Format_RGBA8888);
QSGTexture *texture = window()->createTextureFromImage(image);
QSGTexture *oldTexture = node->texture();
node->setTexture(texture);
node->setRect(boundingRect());
node->setSourceRect(0, 0, vi.base_width, vi.base_height);
if (oldTexture) {
delete oldTexture;
oldTexture = nullptr;
}
return node;
}
return nullptr;
}
Attachments
Last edited by a moderator: