I searched thoroughly to find an answer to my problem but no other post has been helpful so far. I am developing an application in Qt where I need to playback a video stream which is received through a custom protocol. I found myself trying in every possible way to feed these packets in QMediaPlayer with no success. My idea was to write incoming packets in a QBuffer and then read them from QMediaPlayer. Follows my trial:
/// VideoPlayer.h
class VideoPlayer : public QWidget
{
public slots:
void play();
void handlePacket(QByteArray);
[...]
private:
QMediaPlayer mediaPlayer;
QBuffer buffer;
};
/// VideoPlayer.cpp
VideoPlayer::VideoPlayer(QWidget *parent)
: QWidget(parent)
, mediaPlayer(0, (QMediaPlayer::StreamPlayback))
{
buffer.open(QBuffer::ReadWrite);
}
void VideoPlayer::handlePacket(QByteArray packet)
{
buffer.buffer().append(packet);
}
void VideoPlayer::play()
{
mediaPlayer.setMedia(QMediaContent(), &buffer);
mediaPlayer.play();
}
With the above QMediaPlayer plays back data in the buffer at the moment of calling mediaPlayer.setMedia(QMediaContent(), &buffer)
but seems to ignore that new packets were appended to the buffer. May it be because I am accessing the internal QByteArray directly (I checked that QIODevice::readyRead signal is emitted and it is)? I found no way to make QMediaPlayer play new incoming data other than calling setMedia again. Is there a way to notify QMediaPlayer that media length has changed?
Is there an easier way to make this? I thought about writing my own QIODevice or somehow integrate my packet receiver in the Qt framework to provide my custom stream as a QMediaContent?
Are there any other libraries or methods which would allow me to accomplish this task?
I am using Qt 5.4. Thanks in advance for your help.