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. Transferring a video file (.mp4) through Ethernet port.
Forum Update on Monday, May 27th 2025

Transferring a video file (.mp4) through Ethernet port.

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtnetworkqtcpsocketqtmultimedia
17 Posts 5 Posters 1.4k 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.
  • C Christian Ehrlicher
    16 Apr 2024, 05:43

    @Prakash08 said in Transferring a video file (.mp4) through Ethernet port.:

    QBuffer buffer(&received_data);

    How long does this object live?

    P Offline
    P Offline
    Prakash08
    wrote on 16 Apr 2024, 06:36 last edited by
    #3

    @Christian-Ehrlicher which object do you mean, buffer or received_data?
    the buffer object is created when receiveVideo() is called and exists until the function returns ...

    J C 2 Replies Last reply 16 Apr 2024, 06:44
    0
    • P Prakash08
      16 Apr 2024, 06:36

      @Christian-Ehrlicher which object do you mean, buffer or received_data?
      the buffer object is created when receiveVideo() is called and exists until the function returns ...

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 16 Apr 2024, 06:44 last edited by
      #4

      @Prakash08 said in Transferring a video file (.mp4) through Ethernet port.:

      the buffer object is created when receiveVideo() is called and exists until the function returns ...

      right, you're almost there !

      how long does QVideoWidget *videoWidget = new QVideoWidget; live ?


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      P 1 Reply Last reply 16 Apr 2024, 06:56
      1
      • P Prakash08
        16 Apr 2024, 05:17

        I'm transferring a video file (.mp4) from server to client PC through ethernet port. my goal is to transfer the complete video file data to client, and play it on a QWidget in the client side GUI. I'm facing two problems here:

        1)the complete video data is not tranferring to the client. the video size is 1206855 bytes. but the client is receiving only 1205960 bytes. i modified the code to receive the remaining bytes but it is not working.

        2)I'm encountering "Error:failed to seek" error. I dont know whether it is due to incomplete data transfer or due to video format issue. I tried with transferring different videos, but still the error persists.

        connect(socket, &QTcpSocket::disconnected, this, &MainWindow::receiveVideo); 
            connect(socket, &QTcpSocket::readyRead, this, &MainWindow::receive_data);
        
        //this function is for receiving the video data and storing in a QByteArray
        
        void MainWindow::receive_data()
        {
            // Ensure that the sender is indeed a QTcpSocket
            if (QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender())) {
                count += 1;
                if (received_data.size() < 1206855) {
                    qDebug() << count << " receiving video data"<<received_data.size();
                    received_data.append(socket->readAll());
        
                    resetTimer();
                } else {
                    qDebug() << "Data size exceeds threshold. Closing socket.";
                    socket->close();
                }
            }
        }
        
        void MainWindow::resetTimer()
        {
            // Reset the timer whenever data is received
            if (dataTimer->isActive()) {
                dataTimer->stop();
            }
            dataTimer->start(5000); // Restart the timer for 5 seconds
        }
        
        void MainWindow::onTimeout()
        {
            qDebug() << "No data received for 5 seconds. Closing socket.";
            socket->disconnected();
        }
        
        //this function is to process the received data into video and display it on the GUI. This function is called when the program emits "disconnected" signal.
        
        void MainWindow::receiveVideo()
        {
        //    qDebug()<<"processing received data ..";
        
            if (!received_data.isEmpty()) {
                // Convert QByteArray to QBuffer
                QBuffer buffer(&received_data);
                buffer.open(QIODevice::ReadOnly);
        
                // Create a QMediaPlayer object
                QMediaPlayer *player = new QMediaPlayer;
        
                // Connect to the mediaStatusChanged signal
                connect(player, &QMediaPlayer::mediaStatusChanged, [&](QMediaPlayer::MediaStatus status){
                    if (status == QMediaPlayer::LoadedMedia) {
                        // Check if seeking is supported for the current media
                        bool isSeekable = player->isSeekable();
                        if (isSeekable) {
                            qDebug() << "Seeking is supported for the current media.";
                        } else {
                            qDebug() << "Seeking is not supported for the current media.";
                        }
                    }
                });
        
                // Set the media content of the QMediaPlayer
                player->setMedia(QMediaContent(), &buffer);
        
                // Create a QVideoWidget to display the video
                QVideoWidget *videoWidget = new QVideoWidget;
                player->setVideoOutput(videoWidget);
        
                // Set the video output to the QLabel
        //        ui->label->setGeometry(videoWidget->geometry());
        //        player->setVideoOutput(videoWidget);
        
                // Set the layout of ui->widget to a vertical layout (only needed if not already set)
                        if (!ui->widget->layout()) {
                            QVBoxLayout *layout = new QVBoxLayout(ui->widget);
                            ui->widget->setLayout(layout);
                        }
        
                        // Add the videoWidget to the layout of ui->widget
                               ui->widget->layout()->addWidget(videoWidget);
        
                // Set the videoContainer as the central widget of the MainWindow
                setCentralWidget(ui->widget);
        
                // Play the video
                player->play();
            }
            else{
                qDebug()<<"Received data is empty..";
            }
        }
        
        J Offline
        J Offline
        JonB
        wrote on 16 Apr 2024, 06:48 last edited by
        #5

        @Prakash08 said in Transferring a video file (.mp4) through Ethernet port.:

            // Set the media content of the QMediaPlayer
            player->setMedia(QMediaContent(), &buffer);
        

        player is using &buffer as its buffer. Compare the lifetimes of player and buffer.

        J 1 Reply Last reply 16 Apr 2024, 07:34
        1
        • P Prakash08
          16 Apr 2024, 06:36

          @Christian-Ehrlicher which object do you mean, buffer or received_data?
          the buffer object is created when receiveVideo() is called and exists until the function returns ...

          C Offline
          C Offline
          ChrisW67
          wrote on 16 Apr 2024, 06:51 last edited by
          #6

          exists until the function returns ...

          immediately.

          How long would the video take to play in real time if the data existed?

          @Prakash08 said in Transferring a video file (.mp4) through Ethernet port.:

          i modified the code to receive the remaining bytes but it is not working.

          You cannot receive bytes not sent. Are you certain the sender is sending more? Are you seeing the client disconnect signals? Apart from a gross error check, i.e. are we in danger of exhausting memory, why are you looking for a size anyway? Presumably the sender will close the connection when they are done and you can go then.

          1 Reply Last reply
          0
          • J J.Hilk
            16 Apr 2024, 06:44

            @Prakash08 said in Transferring a video file (.mp4) through Ethernet port.:

            the buffer object is created when receiveVideo() is called and exists until the function returns ...

            right, you're almost there !

            how long does QVideoWidget *videoWidget = new QVideoWidget; live ?

            P Offline
            P Offline
            Prakash08
            wrote on 16 Apr 2024, 06:56 last edited by
            #7

            @J-Hilk it is also created when the receiveVideo() is called .. it exists until the function returns ..

            J 1 Reply Last reply 16 Apr 2024, 07:02
            0
            • P Prakash08
              16 Apr 2024, 06:56

              @J-Hilk it is also created when the receiveVideo() is called .. it exists until the function returns ..

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 16 Apr 2024, 07:02 last edited by
              #8

              @Prakash08 no, it's on the heap and you actually never delete it, so it exists forever.

              Maybe QVideoWidgets takes ownership of it? I would have to look that up

              either way the core issue stays, you're trying to play data that is destroyed as soon as it's created


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              1
              • P Offline
                P Offline
                Prakash08
                wrote on 16 Apr 2024, 07:10 last edited by
                #9

                @ChrisW67 the video is 5 secs long. And how to give the condition in sender to code to disconnect the connection when its done sending data ..?

                C 1 Reply Last reply 16 Apr 2024, 08:15
                0
                • P Offline
                  P Offline
                  Prakash08
                  wrote on 16 Apr 2024, 07:20 last edited by
                  #10

                  @J-Hilk the main issue is, "Error: failed to seek". this is the message i'm getting. what does this mean? how to solve this ? any idea ?

                  C 1 Reply Last reply 16 Apr 2024, 07:23
                  0
                  • P Prakash08
                    16 Apr 2024, 07:20

                    @J-Hilk the main issue is, "Error: failed to seek". this is the message i'm getting. what does this mean? how to solve this ? any idea ?

                    C Online
                    C Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 16 Apr 2024, 07:23 last edited by
                    #11

                    @Prakash08 said in Transferring a video file (.mp4) through Ethernet port.:

                    how to solve this ? any idea ?

                    Fix your code - 4 people told you what's wrong...

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    1
                    • P Offline
                      P Offline
                      Prakash08
                      wrote on 16 Apr 2024, 07:33 last edited by
                      #12

                      @Christian-Ehrlicher I'm sorry, I did not get where the issue is, can you please elaborate, which part of code should be modified ?

                      1 Reply Last reply
                      0
                      • J JonB
                        16 Apr 2024, 06:48

                        @Prakash08 said in Transferring a video file (.mp4) through Ethernet port.:

                            // Set the media content of the QMediaPlayer
                            player->setMedia(QMediaContent(), &buffer);
                        

                        player is using &buffer as its buffer. Compare the lifetimes of player and buffer.

                        J Offline
                        J Offline
                        JonB
                        wrote on 16 Apr 2024, 07:34 last edited by JonB
                        #13

                        @Christian-Ehrlicher said in Transferring a video file (.mp4) through Ethernet port.:

                        QBuffer buffer(&received_data);

                        How long does this object live?

                        @JonB said in Transferring a video file (.mp4) through Ethernet port.:

                        player is using &buffer as its buffer. Compare the lifetimes of player and buffer.

                        P 1 Reply Last reply 16 Apr 2024, 09:50
                        0
                        • P Prakash08
                          16 Apr 2024, 07:10

                          @ChrisW67 the video is 5 secs long. And how to give the condition in sender to code to disconnect the connection when its done sending data ..?

                          C Offline
                          C Offline
                          ChrisW67
                          wrote on 16 Apr 2024, 08:15 last edited by
                          #14

                          @Prakash08 said in Transferring a video file (.mp4) through Ethernet port.:

                          And how to give the condition in sender to code to disconnect the connection when its done sending data ..?

                          When the sender runs out of file content, or send its entire in-memory buffer, then it should call close() on its socket. Exactly how that looks depends on your sender code.

                          P 1 Reply Last reply 16 Apr 2024, 09:39
                          0
                          • C ChrisW67
                            16 Apr 2024, 08:15

                            @Prakash08 said in Transferring a video file (.mp4) through Ethernet port.:

                            And how to give the condition in sender to code to disconnect the connection when its done sending data ..?

                            When the sender runs out of file content, or send its entire in-memory buffer, then it should call close() on its socket. Exactly how that looks depends on your sender code.

                            P Offline
                            P Offline
                            Prakash08
                            wrote on 16 Apr 2024, 09:39 last edited by
                            #15

                            @ChrisW67 how to send its in-memory buffer ? And here is the sender code ..

                            void MainWindow::send_video()
                            {
                                qDebug()<<"Connected to Server:"<<socket->peerAddress()<<" on port:"<<socket->peerPort()<<" name:"<<socket->peerName();
                                // QFile file("C:/Users/Intel/Pictures/sunset.png"); // Replace with the actual video path
                                QFile file("C:/Users/Intel/Desktop/video_2.mp4");
                                if (!file.open(QIODevice::ReadOnly)) {
                                    qDebug() << "Unable to open file:" << file.errorString();
                                    return;
                                }
                            
                                while (!file.atEnd()) {
                                    QByteArray imageData = file.readAll(); // Read data in chunks
                                    socket->write(imageData); // Send data to server
                                    qDebug()<<" video data ..";
                                }
                            
                                // QByteArray imageData = file.read(16955); // Read data in chunks
                                // socket->write(imageData);
                            
                                qDebug()<<" video data sent..";
                                file.close();
                                socket->disconnectFromHost();
                            }
                            
                            1 Reply Last reply
                            0
                            • J JonB
                              16 Apr 2024, 07:34

                              @Christian-Ehrlicher said in Transferring a video file (.mp4) through Ethernet port.:

                              QBuffer buffer(&received_data);

                              How long does this object live?

                              @JonB said in Transferring a video file (.mp4) through Ethernet port.:

                              player is using &buffer as its buffer. Compare the lifetimes of player and buffer.

                              P Offline
                              P Offline
                              Prakash08
                              wrote on 16 Apr 2024, 09:50 last edited by
                              #16

                              @JonB In the provided code , the player and buffer objects are created locally within the receiveVideo() function scope. Their lifetime is limited to the duration of the function execution.

                              J 1 Reply Last reply 16 Apr 2024, 11:18
                              0
                              • P Prakash08
                                16 Apr 2024, 09:50

                                @JonB In the provided code , the player and buffer objects are created locally within the receiveVideo() function scope. Their lifetime is limited to the duration of the function execution.

                                J Offline
                                J Offline
                                JonB
                                wrote on 16 Apr 2024, 11:18 last edited by
                                #17

                                @Prakash08
                                Sorry, but you're wrong. @Christian-Ehrlicher & I have both tried to point this out to you.

                                First of all, QMediaPlayer *player = new QMediaPlayer is a pointer to an allocated object on the heap. That means it persists until something deletes it. The fact that QMediaPlayer *player is itself a local variable is neither here nor there, when it goes out of scope that does not destroy the object it points to. That is quite different from QBuffer buffer(&received_data), which is a local stack variable, not a pointer to an object. That does get destroyed at the end of the if.

                                Secondly, player->setMedia(QMediaContent(), &buffer) means that player is using the stack buffer, so as soon as the if exits buffer is destroyed but the QMediaPlayer is still using it as its buffer. When you go player->play() at the end that only starts playing the video, it does not finish playing it. It carries on playing after the function exits. But it's reading from a buffer which has now been destroyed. Not good! Lucky it does not "crash". Your "Error:failed to seek" error may be from code trying to access it when it is no longer valid.

                                This is all standard C++ stuff.

                                1 Reply Last reply
                                4

                                12/17

                                16 Apr 2024, 07:33

                                • Login

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