This example demonstrates how to paint video on QML.
Much of the code here is borrowed from the player example and it is not generally a good example on how to write QML and connect it with C++. The intention is just to demonstrate the use of QGst::Ui::GraphicsVideoSurface, which is bound on the QML context and then used by the VideoItem.
qmlplayer.qml:
/*
Copyright (C) 2012 Collabora Ltd. <info@collabora.com>
@author George Kiagiadakis <george.kiagiadakis@collabora.com>
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 1.0
import QtGStreamer 1.0
Rectangle {
id: window
width: 400
height: 300
Column {
VideoItem {
id: video
width: window.width
height: 260
surface: videoSurface1 //bound on the context from main()
}
Row {
id: buttons
width: window.width
height: 35
spacing: 5
Rectangle {
id: playButton
color: "black"
width: 60
height: 30
Text { text: "Play"; color: "white"; anchors.centerIn: parent }
MouseArea { anchors.fill: parent; onClicked: player.play() }
}
Rectangle {
id: stopButton
color: "black"
width: 60
height: 30
Text { text: "Stop"; color: "white"; anchors.centerIn: parent }
MouseArea { anchors.fill: parent; onClicked: player.stop() }
}
Rectangle {
id: openButton
color: "black"
width: 60
height: 30
Text { text: "Open file"; color: "white"; anchors.centerIn: parent }
MouseArea { anchors.fill: parent; onClicked: player.open() }
}
}
}
}
player.h:
#ifndef PLAYER_H
#define PLAYER_H
#include <QObject>
#include <QGst/Pipeline>
#include <QGst/Message>
class Player : public QObject
{
Q_OBJECT
public:
explicit Player(QObject *parent = 0);
public Q_SLOTS:
void play();
void stop();
void open();
private:
void openFile(const QString & fileName);
void setUri(const QString & uri);
QString m_baseDir;
};
#endif
player.cpp:
#include "player.h"
#include <QUrl>
#include <QFileDialog>
#include <QGlib/Connect>
#include <QGlib/Error>
#include <QGst/ElementFactory>
#include <QGst/Bus>
Player::Player(QObject *parent)
: QObject(parent)
{
}
{
m_videoSink = sink;
}
void Player::play()
{
if (m_pipeline) {
m_pipeline->setState(QGst::StatePlaying);
}
}
void Player::stop()
{
if (m_pipeline) {
m_pipeline->setState(QGst::StateNull);
}
}
void Player::open()
{
QString fileName = QFileDialog::getOpenFileName(qobject_cast<QWidget*>(parent()),
tr("Open a Movie"), m_baseDir);
if (!fileName.isEmpty()) {
openFile(fileName);
}
}
void Player::openFile(const QString & fileName)
{
m_baseDir = QFileInfo(fileName).path();
stop();
setUri(QUrl::fromLocalFile(fileName).toEncoded());
play();
}
void Player::setUri(const QString & uri)
{
if (!m_pipeline) {
if (m_pipeline) {
bus->addSignalWatch();
} else {
qCritical() << "Failed to create the pipeline";
}
}
if (m_pipeline) {
m_pipeline->setProperty("uri", uri);
}
}
{
switch (message->type()) {
case QGst::MessageEos:
stop();
break;
case QGst::MessageError:
stop();
break;
default:
break;
}
}
#include "moc_player.cpp"
void setProperty(const char *name, const T &value)
RefPointer< X > staticCast() const
RefPointer< X > dynamicCast() const
Wrapper class for messages of type QGst::MessageError.
Wrapper class for GstPipeline.
bool connect(void *instance, const char *detailedSignal, T *receiver, R(T::*slot)(Args...), ConnectFlags flags=0)
main.cpp:
#include "player.h"
#include <cstdlib>
#include <QApplication>
#include <QDeclarativeView>
#include <QDeclarativeContext>
#include <QDeclarativeEngine>
#include <QGst/Ui/GraphicsVideoSurface>
#include <QGst/Init>
#ifndef QMLPLAYER_NO_OPENGL
# include <QGLWidget>
#endif
int main(int argc, char **argv)
{
#if defined(QTVIDEOSINK_PATH)
qputenv("GST_PLUGIN_PATH", QTVIDEOSINK_PATH);
#endif
QApplication app(argc, argv);
QDeclarativeView view;
#if !defined(QMLPLAYER_NO_OPENGL)
view.setViewport(new QGLWidget);
#endif
view.rootContext()->setContextProperty(QLatin1String("videoSurface1"), surface);
Player *player = new Player(&view);
view.rootContext()->setContextProperty(QLatin1String("player"), player);
#if defined(UNINSTALLED_IMPORTS_DIR)
view.engine()->addImportPath(QLatin1String(UNINSTALLED_IMPORTS_DIR));
#endif
view.setSource(QUrl(QLatin1String("qrc:///qmlplayer.qml")));
view.show();
return app.exec();
}
Helper class for painting video on a QGraphicsView.
ElementPtr videoSink() const