Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Video transmission through ethernet port.

Video transmission through ethernet port.

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt c++qtnetworkqtmultimedia
5 Posts 3 Posters 453 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Prakash08
    wrote on 11 May 2024, 12:16 last edited by
    #1

    I'm trying to send a video file from one PC to other PC through ethernet port. After receiving the video and when I click on play button it should play on my GUI. My problem is, I'm able to receive video data, but when click on play button it is not playing the video ...

    receiving code:

    #include <QtWidgets>
    #include <QtNetwork>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <QFile>
    #include <QBuffer>
    #include <QMediaPlayer>
    #include <QVideoWidget>
    
    class VideoReceiver : public QWidget
    {
        Q_OBJECT
    
    public:
        VideoReceiver(QWidget *parent = nullptr) : QWidget(parent)
        {
            QLabel *label = new QLabel(tr("Received Video:"));
            videoWidget = new QVideoWidget;
            QPushButton *playButton = new QPushButton(tr("Play"));
            QVBoxLayout *layout = new QVBoxLayout;
            layout->addWidget(label);
            layout->addWidget(videoWidget);
            layout->addWidget(playButton);
            setLayout(layout);
    
            connect(playButton, &QPushButton::clicked, this, &VideoReceiver::playVideo);
    
            qDebug() << "Listening for connections...";
            server.listen(QHostAddress::Any, 12345); // Listen on port 12345
            connect(&server, &QTcpServer::newConnection, this, &VideoReceiver::startPlaying);
        }
    
    private slots:
        void startPlaying()
        {
            qDebug() << "New connection established.";
            socket = server.nextPendingConnection();
            if (!socket) {
                qDebug() << "Error establishing connection";
                return;
            }
    
            qDebug() << "Connection established successfully.";
    
            player = new QMediaPlayer;
            player->setVideoOutput(videoWidget);
            buffer = new QBuffer(player);
            buffer->open(QIODevice::WriteOnly); // Open the buffer for writing
            if (!buffer->isOpen()) {
                qDebug() << "Buffer not open for writing";
                return;
            }
    
            qDebug() << "Buffer opened for writing.";
    
            connect(player, &QMediaPlayer::mediaStatusChanged, [=](QMediaPlayer::MediaStatus status) {
                qDebug() << "Media status changed:" << status;
                if (status == QMediaPlayer::LoadedMedia) {
                    // Start feeding buffered data when player is ready
                    buffer->open(QIODevice::ReadOnly);
                    player->setMedia(QMediaContent(), buffer);
                    player->play();
                    qDebug() << "Playback started.";
                }
            });
    
    
            // Buffer received data
            connect(socket, &QTcpSocket::readyRead, [=]() {
    
                while (socket->bytesAvailable() > 0) {
                    QByteArray data = socket->readAll(); // Read data from socket
                    buffer->write(data); // Write data to buffer
                    qDebug() << "Data received.";
                }
            });
        }
    
        void playVideo()
        {
            if (player) {
                qDebug() << "Attempting to play video...";
                player->play();
                qDebug() << "Play button clicked, playback started.";
            } else {
                qDebug() << "Player is not initialized.";
            }
        }
    
    private:
        QTcpServer server;
        QTcpSocket *socket = nullptr;
        QMediaPlayer *player = nullptr;
        QBuffer *buffer = nullptr;
        QVideoWidget *videoWidget;
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        VideoReceiver receiver;
        receiver.show();
        return a.exec();
    }
    
    #include "main.moc"
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 May 2024, 19:30 last edited by
      #2

      Hi,

      Are you trying to reinvent video streaming ?
      Playing a buffer of raw bytes from a video file alone won't do it. There is a minimal amount of data that you must have before trying to play anything. You might be starting to play on an empty buffer.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • P Offline
        P Offline
        Prakash08
        wrote on 12 May 2024, 05:14 last edited by
        #3

        I'm not trying to reinvent anything. Then can you tell me the usual way of streaming a video ?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 12 May 2024, 07:21 last edited by
          #4

          Using something like GStreamer for example. You can use it both on the server and client side.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • M Offline
            M Offline
            mrdebug
            wrote on 13 May 2024, 07:07 last edited by
            #5

            Hi, if you have to straming audio and video files you could use live555 on server side and, client side, only QMediaPlayer using rtsp.

            Need programmers to hire?
            www.labcsp.com
            www.denisgottardello.it
            GMT+1
            Skype: mrdebug

            1 Reply Last reply
            0

            2/5

            11 May 2024, 19:30

            • Login

            • Login or register to search.
            2 out of 5
            • First post
              2/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved