Video transmission through ethernet port.
Unsolved
General and Desktop
-
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"
-
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. -
Using something like GStreamer for example. You can use it both on the server and client side.