VideoOutput QML 的自定义源属性

2023-12-03

到底需要做什么才能提供自定义框架源VideoOutputQML 对象?

Does VideoOuput本身提供了一个实例QAbstractVideoSurface类到“源”?

Qt5 文档说明了有关此问题的以下内容:

如果您正在扩展自己的 C++ 类以与 VideoOutput,您可以提供一个基于 QObject 的类 mediaObject 属性公开 QMediaObject 派生类,该类 有一个可用的 QVideoRendererControl,或者您可以提供一个 QObject 具有可写 videoSurface 属性的基类,可以接受 基于 QAbstractVideoSurface 的类并且可以遵循正确的协议 向其传递 QVideoFrames。

根据以下文档,我执行了以下操作:

  1. 我实现了自己的课程myFrameProvider源自QObject具有可写的videoSurface财产。
  2. 创建一个连接到以下内容的类,将帧发送到 myFrameProvider。
  3. 实例化myFrameProvider类并使其在与“VideoOutput”小部件相同的 QML 上下文中可访问。

之后 - 每当访问“videSurface”属性时,我都会遇到段错误。 我应该设置自己的视频表面属性吗?


当我遇到类似的问题时,我偶然发现了你的问题。过了一会儿,我找到了一个对我有用的解决方案。即使您的问题是一个较旧的问题并且您可能会继续前进,我也想分享我的答案以可能对其他人有所帮助。

我在中找到了答案QT文档在“使用低级视频帧”部分中。那里发布的代码片段作为起点非常有帮助,但我必须修改它,以便它正常工作。一个最小的工作示例如下所示:

FrameProvider.h

#include <QObject>
#include <QAbstractVideoSurface>
#include <QVideoSurfaceFormat>

class FameProvider : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QAbstractVideoSurface *videoSurface READ videoSurface WRITE setVideoSurface)


public:
    QAbstractVideoSurface* videoSurface() const { return m_surface; }

private:
    QAbstractVideoSurface *m_surface = NULL;
    QVideoSurfaceFormat m_format;

public:


    void setVideoSurface(QAbstractVideoSurface *surface)
    {
        if (m_surface && m_surface != surface  && m_surface->isActive()) {
            m_surface->stop();
        }

        m_surface = surface;

        if (m_surface && m_format.isValid())
        {
            m_format = m_surface->nearestFormat(m_format);
            m_surface->start(m_format);

        }
    }

    void setFormat(int width, int heigth, int format)
    {
        QSize size(width, heigth);
        QVideoSurfaceFormat format(size, format);
        m_format = format;

        if (m_surface) 
        {
            if (m_surface->isActive())
            {
                m_surface->stop();
            }
            m_format = m_surface->nearestFormat(m_format);
            m_surface->start(m_format);
        }
    }

public slots:
    void onNewVideoContentReceived(const QVideoFrame &frame)
    {

        if (m_surface)
            m_surface->present(frame);
    }
};

main.qml

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Controls.Material 2.2
import QtMultimedia 5.4
import com.yourcompany.FrameProvider 1.0

ApplicationWindow {
    objectName: "mainWindow"
    visible: true
    width: 640
    height: 480

    FrameProvider{
        objectName: "provider"
        id: provider
    }

    VideoOutput {
        id: display
        objectName: "display"
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        width: parent.width
        source: provider
    }
}

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

 int main(int argc, char *argv[])
{
    // initialize the qml application engine
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;

    //register the custom control to the qml application engine
    qmlRegisterType<FameProvider>("com.yourcompany.FrameProvider", 1, 0, "FrameProvider");

    // start the view
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
    {
    return -1;
    }

    // find your custom control
    QObject *rootObject = engine.rootObjects().first();
    Qobject *display = rootObject->findChild<QObject *>("display");
    auto provider = qvariant_cast<FameProvider *>(display->property("source"));

    // Create your custom frame source class, which inherits from QObject. This source is expected to have the following public fields and signals:
    // - int width
    // - int height
    // - int format (following QVideoFrame::PixelFormat)
    // - signals: void newFrameAvailable(const QVideoFrame &frame);
    CustomFramesource source;

    // Set the correct format for the video surface (Make sure your selected format is supported by the surface)
    provider->setFormat(source.width,source.height, source.format);

    // Connect your frame source with the provider
    QObject::connect(&source, SIGNAL(newFrameAvailable(const QVideoFrame &)), provider, SLOT(onNewVideoContentReceived(const QVideoFrame &)));

    // run the app
    int retVal =  app.exec();

    return 0;
}

MWE 是从我的实际代码中浓缩而来的,因此未经测试。我希望它无论如何都能运行并显示所有需要的步骤。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

VideoOutput QML 的自定义源属性 的相关文章

  • -ffast-math 可以安全地用于典型项目吗?

    在回答我建议的问题时 ffast math 有评论指出这是危险的 我个人的感觉是 在科学计算之外 是可以的 我还假设严肃的金融应用程序使用定点而不是浮点 当然 如果你想在你的项目中使用它 最终的答案是在你的项目上测试它 看看它有多大影响 但
  • std::map find 在 C++ 中不起作用[重复]

    这个问题在这里已经有答案了 我使用以下几行创建了一个哈希映射和一个迭代器 std map
  • C++ STL 映射,std::pair 作为键

    这就是我通过地图定义的方式 std map
  • StackExchange Redis 删除所有以以下开头的键

    我有一个格式的密钥 Error 1 Error 24 Error 32 Using StackExchange Redis 我该怎么办KeyDelete在与格式匹配的所有键上Error 在另一个答案中我看到了 LUA 脚本 EVAL ret
  • 二叉树和快速排序?

    我有一个家庭作业 内容如下 别生气 担心 我是not请你帮我做作业 编写一个程序 通过使用二分查找的快速排序方法对一组数字进行排序 树 推荐的实现是使用递归算法 这是什么意思 到目前为止 这是我的解释 正如我在下面解释的那样 我认为两者都有
  • 如何在建立上下文时设置连接超时-PrincipalContext

    using PrincipalContext ctx new PrincipalContext ContextType Domain Domain UserName Password UserPrincipal U new UserPrin
  • 图片框、双击和单击事件

    我有一个奇怪的问题 我有一个图片框双击事件以及单击事件 问题是即使我双击该控件 也会引发单击事件 如果我禁用单击事件 则双击事件正在工作 这个问题已经在这里讨论过 https stackoverflow com questions 1830
  • 如何在单例类和未命名类之间进行选择?

    我会使用这样的单例 Singleton single Singleton instance single gt do it 我会使用这样的未命名类 single do it 我觉得单例模式除了具有可读的错误消息之外 与未命名的类相比没有任何
  • 使用c#在mac上启动外部进程

    我成功地使用 System Diagnostics Process Start 在 Windows 上启动我的外部单声道可执行文件 然而在mac上却失败了 我没有收到任何错误 只是什么也没发生 我尝试按以下方式进行操作 System Dia
  • C 编程中的 rand() 问题? [复制]

    这个问题在这里已经有答案了 可能的重复 为什么我总是用 rand 得到相同的随机数序列 https stackoverflow com questions 1108780 why do i always get the same seque
  • 对象变空似乎是 Hangfire 中的反序列化问题

    Hangfire 似乎无法反序列化我的原始版本Scheduler对象及其所有状态 我正在调用其 Execute 方法BackgroundJob Enqueue 如下所示 Scheduler new FileInFileOut FileIn
  • 批量插入,asp.net

    我需要获取与会员相对应的 ID 号列表 在任何给定时间处理的数量可能在 10 到 10 000 之间 我可以毫无问题地收集数据 解析数据并将其加载到 DataTable 或任何内容 C 中 但我想在数据库中执行一些操作 将所有这些数据插入表
  • 在Android中提取视频帧

    我想知道是否可以从 Android 中正在运行的视频中提取帧 我需要定期提取帧并将其发送以进行进一步处理 有人能为我找到答案吗 Thanks Abhi 您可以使用媒体元数据检索器 http developer android com ref
  • PyQt:在运行时向滚动区域添加小部件

    我试图在运行时通过按下按钮来添加新的小部件 在下面的示例中我使用标签 这里是例子 import sys from PyQt4 QtCore import from PyQt4 QtGui import class Widget QWidge
  • 为什么使用 .AsEnumerable() 而不是转换为 IEnumerable

    扩展方法之一IEnumerable
  • 在 OSX 上检测 Objective C 或 C++ 中的文件夹访问(如 fs_usage 命令)

    我正在 OSX 上开发实时病毒扫描程序 OSX 的命令行命令fs usage可以通过以下方式确定文件夹访问权限 并且只能以 root 用户身份运行 fs usage w f pathname grep Users Documents Use
  • C 中的 2 个字符要短

    我有2个字符 Char 128和查尔2 如何将这些字符转为 Short640 in C 我试过了 unsigned short getShort unsigned char array int offset short returnVal
  • 使用属性和性能

    我正在优化我的代码 我注意到使用属性 甚至自动属性 对执行时间有深远的影响 请参阅下面的示例 Test public void GetterVsField PropertyTest propertyTest new PropertyTest
  • 在for循环中声明和初始化变量

    可以简单写一下吗 for int i 0 代替 int i for i 0 在 C 或 C 中 并且会变量i只能在循环内部访问 它在 C 中有效 它在 C 的原始版本中是不合法的 但在 C99 中被采用为 C 的一部分 当时一些 C 功能被
  • C# 使用 .Equals() 比较两个 double

    我使用 ReShaper 当我用 比较两个双精度值时 它建议我应该使用 Math 具有公差的 ABS 方法 看 https www jetbrains com help resharper 2016 2 CompareOfFloatsByE

随机推荐