- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
要为 VideoOutput
QML 对象提供自定义帧源,究竟需要做什么?
VideoOuput
本身是否为“源”提供了一个 QAbstractVideoSurface
类的实例?
Qt5 文档说明了有关提供有关此问题的以下内容:
If you are extending your own C++ classes to interoperate with VideoOutput, you can either provide a QObject based class with a mediaObject property that exposes a QMediaObject derived class that has a QVideoRendererControl available, or you can provide a QObject based class with a writable videoSurface property that can accept a QAbstractVideoSurface based class and can follow the correct protocol to deliver QVideoFrames to it.
根据以下文档,我执行了以下操作:
myFrameProvider
,派生自 QObject
,它具有可写的 videoSurface
属性。myFrameProvider
类,并使其在与“VideoOutput”小部件相同的 QML 上下文中可访问。之后 - 每当访问“videSurface”属性时,我都会遇到段错误。我应该设置自己的视频表面属性吗??
最佳答案
在遇到类似问题时,我偶然发现了您的问题。过了一会儿,我找到了一个对我有用的解决方案。即使您的问题是一个较旧的问题并且您可能继续前进,我也想分享我的答案以帮助其他人。
我在 QT documentation 中找到了答案在“使用低级视频帧”部分中。那里发布的一段代码作为起点非常有帮助,但我必须对其进行修改,以便它正常工作。一个最小的工作示例如下所示:
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 是从我的实际代码中浓缩而来的,因此未经测试。我希望它无论如何都能运行并显示所有需要的步骤。
关于c++ - VideoOutput QML 的自定义源属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43854589/
要为 VideoOutput QML 对象提供自定义帧源,究竟需要做什么? VideoOuput 本身是否为“源”提供了一个 QAbstractVideoSurface 类的实例? Qt5 文档说明了
我正在尝试通过以下代码使用 qml 播放测试视频: import QtQuick 2.2import QtMultimedia 5.0Item { width: 300 height: 3
我在将用 C++ 创建的 QCamera 显示到 QML 中的 VideoOutput 时遇到问题。如果我在 QML 中使用这种方式,一切都很好,我得到了视频输出: Item{ Vid
我正在尝试使用 Qt 或 Qml 在 iOS 应用程序中播放带声音的视频。我的环境是: Qt 5.4 OSX 优胜美地 10.10.2 iPad 2 (iOS 8.3) 和 Iphone6+ (iOS
我是一名优秀的程序员,十分优秀!