How to use the QMediaPlayer module to connect to a rtsp stream?

1.6k Views Asked by At

I'm creating an RTSP stream using FFmpeg:

ffmpeg -f gdigrab -framerate 30 -probesize 100M -i title="" -c:v libx264 -preset veryfast -maxrate 1000k -bufsize 1000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 128k -f rtsp -rtsp_transport udp rtsp://...129:8554/stream

and trying to connect to it using the Qt module QMediaPlayer.

It stuck for many seconds when it read the line player->setSource(QUrl("rtsp://....129:8554/stream"));

and then debug this message:

qt.multimedia.ffmpeg.mediadataholder: AVStream duration -9223372036854775808 is invalid. Taking it from the metadata

Also, connect(player, &QMediaPlayer::errorOccurred print:

Error: "Invalid argument"

class MediaPlayer : public QObject
{
    Q_OBJECT
public:
    MediaPlayer (QObject* parent = nullptr) : QObject(parent)
    {
        player = new QMediaPlayer;
        player->setSource(QUrl("rtsp://....129:8554/stream"));
        connect(player, &QMediaPlayer::errorOccurred, this, [this](QMediaPlayer::Error error, const QString& errorString)
        {
            qDebug() << "Error:" << errorString;
        });

        videoWidget = new QVideoWidget;
        player->setVideoOutput(videoWidget);

        videoWidget->show();
        player->play();
    }

private:
    QMediaPlayer* player;
    QVideoWidget* videoWidget;
};

Testing the exactly same streamUrl on vlc does work correctly. I'm using Qt 6.6, and Win10. ffmpeg 6.0

How I could debug this?

2

There are 2 best solutions below

3
cheungxiongwei On

On the window platform, use the ffmpeg backend, currently Qt does not support playing rtsp video streams.

If you want to support rtsp protocol, you need to compile qt & ffmpeg yourself

--enable-protocol=rtsp

qt 6.5 build config Env.FFMPEG_DIR_MSVC ref https://wiki.qt.io/Qt_6.5_Tools_and_Versions

0
Can Dursun On

FFMpeg in Qt does not support RTSP streams directly. If you want, you can use GStreamer. By changing the environment variable to gstreamer. Please see these links:

QtMultimedia

QPut Env