Trying to play video/stream Fixing Error in QGraphicsVideoItem [SOLVED]



  • Can anyone help? Just grabbed this example from the help location of Qt5.1 however the code doesn't compile and fails with these errors.

    C:\Qt\Qt5.1.0\5.1.0\msvc2010_opengl\examples\multimediawidgets\videographicsitem\main.cpp:50: error: C2065: 'player' : undeclared identifier

    C:\Qt\Qt5.1.0\5.1.0\msvc2010_opengl\examples\multimediawidgets\videographicsitem\main.cpp:54: error: C2065: 'graphicsView' : undeclared identifier

    These are the two reason it fails however i have no idea what class i should set these to. For example tried setting player to the class VideoPlayer which it didn't like but i have no idea for graphicsView variable. Anyone got any ideas ?

    Main.cpp

    @

    #include "videoplayer.h"

    #include <QApplication>
    #include <QGraphicsVideoItem>
    #include <QGraphicsView>

    int main(int argc, char **argv)
    {
    QApplication app(argc, argv);
    player = new QMediaPlayer;

    QGraphicsVideoItem *item = new QGraphicsVideoItem;
    player->setVideoOutput(item);
    graphicsView->scene()->addItem(item);
    graphicsView->show();
    
    player->setMedia(QUrl("rtsp://localhost:8554/stream"));
    player->play();
    VideoPlayer player;
    player.show();
    
    return app.exec&#40;&#41;; 
    

    }@

    Videoplayer.cpp-
    @
    #include "videoplayer.h"
    VideoPlayer::VideoPlayer(QWidget *parent)
    : QWidget(parent)
    , mediaPlayer(0, QMediaPlayer::VideoSurface)
    , videoItem(0)
    , playButton(0)
    , positionSlider(0)
    {
    videoItem = new QGraphicsVideoItem;
    videoItem->setSize(QSizeF(640, 480));

    QGraphicsScene *scene = new QGraphicsScene(this);
    QGraphicsView *graphicsView = new QGraphicsView(scene);
    
    scene->addItem(videoItem);
    
    QSlider *rotateSlider = new QSlider(Qt::Horizontal);
    rotateSlider->setRange(-180,  180);
    rotateSlider->setValue(0);
    
    connect(rotateSlider, SIGNAL(valueChanged(int)),
            this, SLOT(rotateVideo(int)));
    
    QAbstractButton *openButton = new QPushButton(tr("Open..."));
    connect(openButton, SIGNAL(clicked()), this, SLOT(openFile&#40;&#41;&#41;&#41;;
    
    playButton = new QPushButton;
    playButton->setEnabled(false);
    playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
    
    connect(playButton, SIGNAL(clicked()),
            this, SLOT(play()));
    
    positionSlider = new QSlider(Qt::Horizontal);
    positionSlider->setRange(0, 0);
    
    connect(positionSlider, SIGNAL(sliderMoved(int)),
            this, SLOT(setPosition(int)));
    
    QBoxLayout *controlLayout = new QHBoxLayout;
    controlLayout->setMargin(0);
    controlLayout->addWidget(openButton);
    controlLayout->addWidget(playButton);
    controlLayout->addWidget(positionSlider);
    
    QBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(graphicsView);
    layout->addWidget(rotateSlider);
    layout->addLayout(controlLayout);
    
    setLayout(layout);
    
    mediaPlayer.setVideoOutput(videoItem);
    connect(&mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)),
            this, SLOT(mediaStateChanged(QMediaPlayer::State)));
    connect(&mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
    connect(&mediaPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64)));
    

    }

    VideoPlayer::~VideoPlayer()
    {
    }

    void VideoPlayer::openFile()
    {
    //QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"),QDir::homePath());
    QString fileName = "rtsp://192.168.100.58:8554/stream";
    if (!fileName.isEmpty()) {
    mediaPlayer.setMedia(QUrl::fromLocalFile(fileName));

        playButton->setEnabled(true&#41;;
    }
    

    }

    void VideoPlayer::play()
    {
    switch(mediaPlayer.state()) {
    case QMediaPlayer::PlayingState:
    mediaPlayer.pause();
    break;
    default:
    mediaPlayer.play();
    break;
    }
    }
    @
    videoplayer.h
    @
    #ifndef VIDEOPLAYER_H
    #define VIDEOPLAYER_H

    #include <QMediaPlayer>
    #include <QMovie>
    #include <QWidget>
    #include <QtWidgets>
    #include <QVideoSurfaceFormat>
    #include <QGraphicsVideoItem>

    QT_BEGIN_NAMESPACE
    class QAbstractButton;
    class QSlider;
    class QGraphicsVideoItem;
    QT_END_NAMESPACE

    class VideoPlayer : public QWidget
    {
    Q_OBJECT

    public:
    VideoPlayer(QWidget *parent = 0);
    ~VideoPlayer();

    QSize sizeHint() const { return QSize(800, 600); }
    

    public slots:
    void openFile();
    void play();

    private slots:
    void mediaStateChanged(QMediaPlayer::State state);
    void positionChanged(qint64 position);
    void durationChanged(qint64 duration);
    void setPosition(int position);
    void rotateVideo(int angle);

    private:
    QMediaPlayer mediaPlayer;
    QGraphicsVideoItem *videoItem;
    QAbstractButton *playButton;
    QSlider *positionSlider;
    };

    #endif

    @
    .pro
    @TEMPLATE = app
    TARGET = videographicsitem

    QT += multimedia multimediawidgets

    HEADERS += videoplayer.h

    SOURCES += main.cpp
    videoplayer.cpp

    target.path = $$[QT_INSTALL_EXAMPLES]/multimediawidgets/videographicsitem
    INSTALLS += target

    QT+=widgets
    @


  • Lifetime Qt Champion

    Hi,

    Line 10: You didn't give the type of player
    Line 14: You didn't declare nor instantiate an object of class QGraphicsView named graphicsView



  • Like I said SGaist this is from the examples on Qt, as said before on line 10 have tried

    VideoPlayer *player = new QMediaPlayer
    got
    C:\Qt\Qt5.1.0\5.1.0\msvc2010_opengl\examples\multimediawidgets\videographicsitem\main.cpp:50: error: C2440: 'initializing' : cannot convert from 'QMediaPlayer *' to 'VideoPlayer'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    tried
    QMediaPlayer *player =new QMediaPlayer
    got
    C:\Qt\Qt5.1.0\5.1.0\msvc2010_opengl\examples\multimediawidgets\videographicsitem\main.cpp:53: error: C2819: type 'QMediaPlayer' does not have an overloaded member 'operator ->'

    neither worked

    also tried
    QGraphicsView graphicsView = new QGraphicsView;

    All I want to do is test this example but have no idea what is missing does seem pretty rubbish that even the examples that Qt provide don't even compile.

    Well found out what the problem was the example under the help section of Qt5.1 isn't correct googled the same page and it has different code tried the code listed on the site instead and it works.

    http://qt-project.org/doc/qt-5.0/qtmultimedia/multimediawidgets-videographicsitem.html

    However if anyone knows how to fix this code please let me know as the example above is alot easier to understand than the link above where i got the working code (using just a normal video file instead of an rtsp stream of course).


  • Lifetime Qt Champion

    @VideoPlayer *player = new QMediaPlayer; << VideoPlayer is a not the same as a QMediaPlayer@

    Anyway, just keep the main from the example since you already modified openFile.

    Or add a new function to MediaPlayer to open your network media.

    Also note that
    @
    QString fileName = "rtsp://192.168.100.58:8554/stream";
    if (!fileName.isEmpty()) {
    mediaPlayer.setMedia(QUrl::fromLocalFile(fileName));

        playButton-&gt;setEnabled(true);
    }
    

    @

    won't work as expected, you are not giving a local file name there



  • Yea don't really care about the rtsp section until i can get a normal video playing and get the code compiling. So using the default atm

    QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"),QDir::homePath());

    So should line 10 be

    QMediaPlayer *player = new QMediaPlayer;?

    As already tried that to no luck , (ha but just tried that again found a typing error gotta love it).

    So after fixing that this is now my current main.

    @#include "videoplayer.h"

    #include <QApplication>
    #include <QGraphicsView>

    int main(int argc, char **argv)
    {
    QApplication app(argc, argv);
    //VideoPlayer player;
    //QGraphicsView graphicsView;
    QMediaPlayer *player = new QMediaPlayer;

    QGraphicsVideoItem *item;
    QGraphicsView *graphicsView;
    player->setVideoOutput(item);
    graphicsView->scene()->addItem(item);
    graphicsView->show();
    
    player->setMedia(QUrl("http://example.com/myclip4.ogv"));
    player->play();
    
    player->show();
    
    return app.exec();
    

    }@

    but gettings these errors.

    C:\Qt\Qt5.1.0\5.1.0\msvc2010_opengl\examples\multimediawidgets\videographicsitem\main.cpp:56: error: C2664: 'QGraphicsScene::addItem' : cannot convert parameter 1 from 'QGraphicsVideoItem *' to 'QGraphicsItem *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    C:\Qt\Qt5.1.0\5.1.0\msvc2010_opengl\examples\multimediawidgets\videographicsitem\main.cpp:62: error: C2039: 'show' : is not a member of 'QMediaPlayer'

    Think i understand how to fix the second error by changing player to graphicsView

    e.g.

    graphicsView->show();

    instead of

    player->show();

    am i correct in thinking that ?

    But with the first error not really sure how to proceed, as no idea how you would typecast
    'QGraphicsVideoItem *' to 'QGraphicsItem *'

    is it something like

    addItem(QGraphicsVideoItem(*item);?

    If anyone has a link of how to do this type of typecasting that would be great.



  • Ok broke down my player and think I have the most basic QMediaPlayer possible. The audio of this file plays from the console but the video doesn't the file type is .mov (h.264). Is there something that I am missing ?

    main.cpp
    @#include <QCoreApplication>
    #include <QMediaPlayer>
    #include <QFileInfo>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    QMediaPlayer media;
    const QString file = "C:/Users/nick/Downloads/big_buck_bunny_1080p_h264.mov";
    QUrl url(QFileInfo(file).absoluteFilePath());
    media.setMedia(url);
    media.play();
    return a.exec();
    }@


  • Lifetime Qt Champion

    You're creating a QCoreApplication, which means no GUI. Create QApplication and you should be good.



  • Well i think I am getting there (but changing it to a GUI application doesn't seemed to of changed anything) but I'm presuming by pushing the file into a surface now it should play ? (windows error below related to not having a surface set up for the media file ?).

    However the linux version just looks like i need to have the correct URI path (or do i need the URI and to push it to a surface ?). How should i be defining a path in linux to a file like "file://home/ion/Downloads/big_buck_bunny.mov"? Cause at the moment all i get is the errors listed below.

    Cheers for help so far.

    Windows Error:

    • no videoWindowControl or videoRendererControl, unable to add output node for video data

    Linux Errors:

    • GStreamer; Unable to pause - "/home/ion/Qt_practice/build-test-Desktop_Qt_5_1_0_GCC_64bit-Debug/big_buck_bunny_480p_h264.mov"

    • GStreamer; Unable to play - "/home/ion/Qt_practice/build-test-Desktop_Qt_5_1_0_GCC_64bit-Debug/big_buck_bunny_480p_h264.mov"

    • Error: "Invalid URI "/home/ion/Qt_practice/build-test-Desktop_Qt_5_1_0_GCC_64bit-Debug/big_buck_bunny_480p_h264.mov"."

    @#include "mainwindow.h"
    #include <QApplication>
    #include <QMediaPlayer>
    #include <QFileInfo>
    #include <QDebug>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    QMediaPlayer media;
    const QString file = "big_buck_bunny_480p_h264.mov";
    QUrl url(QFileInfo(file).absoluteFilePath());
    qDebug() << url << endl;
    media.setMedia(url);
    media.play();
    w.show();

    return a.exec&#40;&#41;;
    

    }
    @

    Ha well i think I am getting closer hopefully this should be the last post :P.


  • Lifetime Qt Champion

    Do you have the same error using

    @QUrl url = QUrl::fromLocalFile(QFileInfo(file).absoluteFilePath());@

    ?



  • Lol well that sorted that linux problem feel a bit foolish now (note to self don't watch a video when it's in a foreign language just asking for trouble) . Now both the linux and the Windows version are doing the exact same only difference is the error output.

    Both playing audio but no video. Do i need a Abstract Video surface or ?
    Linux:

    OpenGL Warning: XGetVisualInfo returned 0 visuals for 0x1b800a0

    OpenGL Warning: Retry with 0x8002 returned 0 visuals

    OpenGL Warning: XGetVisualInfo returned 0 visuals for 0x1b800a0

    OpenGL Warning: Retry with 0x8002 returned 0 visuals

    OpenGL Warning: glXGetFBConfigAttrib for 0x1b800a0, failed to get XVisualInfo

    over and over

    Windows:

    no videoWindowControl or videoRendererControl, unable to add output node for video data

    but yea still get the same error on the windows side. Also just so your aware running the linux machine in a VM.


  • Lifetime Qt Champion

    A QVideoWidget should do it



  • Ha yea What i ended up using in the end below is the updated code using QGraphicsView or QVideoWidget. Ha now all i need to do is figure out how to apply this to an RTSP stream but thats for another day :D. Cheers SGaist been very helpful.

    @#include "mainwindow.h"
    #include <QApplication>
    #include <QMediaPlayer>
    #include <QFileInfo>
    #include <QDebug>
    #include <QVideoWidget>
    #include <QGraphicsView>
    #include <QGraphicsVideoItem>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;

    QMediaPlayer *player = new QMediaPlayer;
     player->setMedia(QUrl::fromLocalFile&#40;"C:/Users/nick/Downloads/big_buck_bunny_1080p_h264.mov"&#41;&#41;;
     QVideoWidget *videoWidget = new QVideoWidget;
     w.setCentralWidget(videoWidget&#41;; // if w is a QMainWindow
     player->setVideoOutput(videoWidget);
     player->play();
    

    /*
    QGraphicsView *graphicsView = new QGraphicsView;
    w.setCentralWidget(graphicsView); // w = QMainWindow
    QGraphicsScene *scene = new QGraphicsScene;
    QMediaPlayer *player = new QMediaPlayer;
    QGraphicsVideoItem *item = new QGraphicsVideoItem;

    graphicsView->setScene(scene);
    player->setVideoOutput(item);
    
    graphicsView->scene()->addItem(item);
    //player->setMedia(QUrl::fromLocalFile&#40;"C:/Users/nick/Downloads/big_buck_bunny_1080p_h264.mov"&#41;&#41;;
    player->setMedia(QUrl("rtsp://localhost:8554/stream"&#41;);
    player->play();*/
    
    w.show();
    
    return a.exec(&#41;;
    

    }@

    Btw when should I be using either? because currently just tempted to do all video output using videowidget as it looks the simpliest, is there any situation when i should use Graphics View or QAbstractVideoSurface over VideoWidget?


  • Lifetime Qt Champion

    For your tests, you should keep using the QVideoWidget. Once you have everything working, you can start playing with QGraphicsView and friends



  • Doesn't really answer my question but ah well setting it to solved.


  • Lifetime Qt Champion

    Did you meant: In which case should I use QVideoWidget and in which one QGraphicsView ?



  • Yea can't seemed to find a good example of their usage. Also would you know why QVideoWidget doesn't work on linux with that code but graphicsview does?

    @#include "mainwindow.h"
    #include <QApplication>
    #include <QMediaPlayer>
    #include <QFileInfo>
    #include <QDebug>
    #include <QVideoWidget>
    #include <QGraphicsView>
    #include <QGraphicsVideoItem>

    int main(int argc, char argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    /
    QMediaPlayer *player = new QMediaPlayer;
    const QString file = "big_buck_bunny_480p_h264.mov";
    QUrl url = QUrl::fromLocalFile(QFileInfo(file).absoluteFilePath());
    player->setMedia(url);
    QVideoWidget videoWidget = new QVideoWidget;
    w.setCentralWidget(videoWidget); // if w is a QMainWindow
    player->setVideoOutput(videoWidget);
    player->play();
    /

     QGraphicsView *graphicsView = new QGraphicsView;
     w.setCentralWidget(graphicsView); // w = QMainWindow
     QGraphicsScene *scene       = new QGraphicsScene;
     QMediaPlayer *player        = new QMediaPlayer;
     QGraphicsVideoItem *item    = new QGraphicsVideoItem;
    
     graphicsView->setScene(scene);
     player->setVideoOutput(item);
     const QString file = "big_buck_bunny_480p_h264.mov";
     QUrl url = QUrl::fromLocalFile&#40;QFileInfo(file&#41;.absoluteFilePath(&#41;&#41;;
     graphicsView->scene(&#41;->addItem(item);
     player->setMedia(url);
     //player->setMedia(QUrl("rtsp://localhost:8554/stream"));
     player->play();
          qDebug() << url << endl;
    w.show();
    return a.exec&#40;&#41;;
    

    }
    @

    The weird thing is linux says that it "Failed to start video surface" yet the video still plays bit random (but crashes after a while sometimes). (mind you probably a good idea to just create a new post to answer this question).


  • Lifetime Qt Champion

    The only good answer is: it really depends on the application you are writing (I know might not sound helpful) If you are not doing anything special that would require a QGraphicsView then go for the widget.

    As for your last question, you could directly try to see the "bug report system":http://bugreports.qt-project.org/issues and check whether it's a known issue


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.