Multiple video streaming on QT desktop application simultaneously.



  • I try to make application for camera security system, in which all camera stream on my desktop application simultaneously. In my code I can play one video at a time with help od QMediaPlayer and QVideoWidget, now I try to play two video at a time on QGraphicsView widget using ui->graphicsView->setViewport(videoWidget[i][j]);

    I paste my all code here:
    mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        for(int i=0; i<2; i++){
            for(int j=0; j<2; j++){
    
                player[i][j] = new QMediaPlayer(this);
                vw[i][j] = new QVideoWidget(this);
                player[i][j]->setVideoOutput(vw[i][j]);
                ui->graphicsView->setViewport(vw[i][j]);
                //ui->graphicsView_2->setViewport(vw[i][j]);
    
                slider = new QSlider(this);
                bar = new QProgressBar(this);
                slider1 = new QSlider(this);
    
                slider->setOrientation(Qt::Horizontal);
    
                ui->statusBar->addPermanentWidget(slider);
                ui->statusBar->addPermanentWidget(bar);
                ui->statusBar->addPermanentWidget(slider1);
    
                connect(player[i][j],&QMediaPlayer::durationChanged,slider,&QSlider::setMaximum);
                connect(player[i][j],&QMediaPlayer::positionChanged,slider,&QSlider::setValue);
                connect(slider,&QSlider::sliderMoved,player[i][j],&QMediaPlayer::setPosition);
    
                slider1->setValue(50);
                connect(slider1,&QSlider::sliderMoved,player[i][j],&QMediaPlayer::setVolume);
    
                connect(player[i][j],&QMediaPlayer::durationChanged,bar,&QProgressBar::setMaximum);
                connect(player[i][j],&QMediaPlayer::positionChanged,bar,&QProgressBar::setValue);
    
            }
        }
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_actionOpen_triggered()
    {
        QString filename = QFileDialog::getOpenFileName(this,"Open a File","","Video File (*.*)");
        on_actionStop_triggered();
    
        player[1][1]->setMedia(QUrl::fromLocalFile(filename));
    
        on_actionPlay_triggered();
    }
    
    void MainWindow::on_actionPlay_triggered()
    {
        player[1][1]->play();
        ui->statusBar->showMessage("Playing");
    }
    
    void MainWindow::on_actionPause_triggered()
    {
        player[1][1]->pause();
        ui->statusBar->showMessage("Paused...");
    }
    
    void MainWindow::on_actionStop_triggered()
    {
        player[1][1]->stop();
        ui->statusBar->showMessage("Stopped");
    }
    
    void MainWindow::on_actionMute_triggered()
    {
        player[1][1]->setMuted(1);
        ui->statusBar->showMessage("Muted...");
    }
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QMediaPlayer>
    #include <QVideoWidget>
    #include <QFileDialog>
    #include <QProgressBar>
    #include <QSlider>
    #include <QWidget>
    #include <QGridLayout>
    #include <QThread>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_actionOpen_triggered();
    
        void on_actionPlay_triggered();
    
        void on_actionPause_triggered();
    
        void on_actionStop_triggered();
    
        void on_actionMute_triggered();
    
    private:
        Ui::MainWindow *ui;
        QMediaPlayer* player[2][2];
        QVideoWidget* vw[2][2];
        QProgressBar* bar;
        QSlider* slider;
        QSlider* slider1;
    
    };
    
    #endif // MAINWINDOW_H
    

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    videogui.pro

    QT       += core gui multimedia multimediawidgets
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = videogui
    TEMPLATE = app
    
    SOURCES += main.cpp\
            mainwindow.cpp
    
    HEADERS  += mainwindow.h
    
    FORMS    += mainwindow.ui
    
    RESOURCES += \
        resources.qrc
    

    mainwindow.ui
    http://i.stack.imgur.com/tvBVN.png

    Application Output:
    http://i.stack.imgur.com/ewAiy.png

    Should I have to add thread? If yes then How?
    Please guide me to run two video simultaneously.

    Thanks in Advance.



  • @Tejas-Virpariya : I got what I want. I changed mainwindow.cpp file.
    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        player = new QMediaPlayer;
        vw = new QVideoWidget;
        player->setVideoOutput(vw);
        ui->graphicsView->setViewport(vw);
        //ui->graphicsView_2->setViewport(vw1);
    
        slider = new QSlider(this);
        bar = new QProgressBar(this);
        slider1 = new QSlider(this);
    
        slider->setOrientation(Qt::Horizontal);
    
        ui->statusBar->addPermanentWidget(slider);
        ui->statusBar->addPermanentWidget(bar);
        ui->statusBar->addPermanentWidget(slider1);
    
        connect(player,&QMediaPlayer::durationChanged,slider,&QSlider::setMaximum);
        connect(player,&QMediaPlayer::positionChanged,slider,&QSlider::setValue);
        connect(slider,&QSlider::sliderMoved,player,&QMediaPlayer::setPosition);
    
        slider1->setValue(50);
        connect(slider1,&QSlider::sliderMoved,player,&QMediaPlayer::setVolume);
    
        connect(player,&QMediaPlayer::durationChanged,bar,&QProgressBar::setMaximum);
        connect(player,&QMediaPlayer::positionChanged,bar,&QProgressBar::setValue);
    
    
    
        player1 = new QMediaPlayer;
        vw1 = new QVideoWidget;
        player1->setVideoOutput(vw1);
        //ui->graphicsView->setViewport(vw);
        ui->graphicsView_2->setViewport(vw1);
    
        slider2 = new QSlider(this);
        bar1 = new QProgressBar(this);
        slider3 = new QSlider(this);
    
        slider2->setOrientation(Qt::Horizontal);
    
        ui->statusBar->addPermanentWidget(slider2);
        ui->statusBar->addPermanentWidget(bar1);
        ui->statusBar->addPermanentWidget(slider3);
    
        connect(player1,&QMediaPlayer::durationChanged,slider2,&QSlider::setMaximum);
        connect(player1,&QMediaPlayer::positionChanged,slider2,&QSlider::setValue);
        connect(slider2,&QSlider::sliderMoved,player1,&QMediaPlayer::setPosition);
    
        slider3->setValue(50);
        connect(slider3,&QSlider::sliderMoved,player1,&QMediaPlayer::setVolume);
    
        connect(player1,&QMediaPlayer::durationChanged,bar1,&QProgressBar::setMaximum);
        connect(player1,&QMediaPlayer::positionChanged,bar1,&QProgressBar::setValue);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_actionOpen_triggered()
    {
        QString filename = QFileDialog::getOpenFileName(this,"Open a File","","Video File (*.*)");
        QString filename1 = "C:/Users/Public/Videos/Sample Videos/Wildlife.wmv";
        on_actionStop_triggered();
    
        player->setMedia(QUrl::fromLocalFile(filename));
        player1->setMedia(QUrl::fromLocalFile(filename1));
    
        on_actionPlay_triggered();
    }
    
    void MainWindow::on_actionPlay_triggered()
    {
        player->play();
        player1->play();
        ui->statusBar->showMessage("Playing");
    }
    
    void MainWindow::on_actionPause_triggered()
    {
        player->pause();
        player1->pause();
        ui->statusBar->showMessage("Paused...");
    }
    
    void MainWindow::on_actionStop_triggered()
    {
        player->stop();
        player1->stop();
        ui->statusBar->showMessage("Stopped");
    }
    
    void MainWindow::on_actionMute_triggered()
    {
        if(player->isMuted()){
            player->setMuted(0);
        }else{
            player->setMuted(1);
        }
        if(player1->isMuted()){
            player1->setMuted(0);
        }else{
            player1->setMuted(1);
        }
        ui->statusBar->showMessage("Muted...");
    }
    

  • Lifetime Qt Champion

    Hi,

    Why not just use a QHBoxLayout to put all your QVideoWidgets in ? Or even better, make a custom widget that embedded that video player + control and make four of them ?

    What you are currently doing is setting the exact same view port on all video widgets.

    Out of curiosity, why are you using two dimensional arrays ?


Log in to reply
 

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