Qt Stream IP Camera Video

我与影子孤独终老i 提交于 2020-01-02 18:06:49

问题


I am trying to stream the video from an IP camera using Qt multimedia (Qt 5). A similar question could be found here: Play a Live video Stream using Qt but I try to avoid using an other library such as LibVLC. I manage to display the video from a locally stored file but not to display the stream from the IP camera.

Here is my code:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMediaPlayer>
#include <qvideowidget.h>
#include <QVideoWidget>

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this-> resize(1000,1000);
    QMediaPlayer *mp = new QMediaPlayer(0,0);
    QMediaContent *mc = new QMediaContent(QUrl("http://96.10.1.168/mjpg/video.mjpg"));
    //QMediaContent *mc = new QMediaContent(QUrl::fromLocalFile("/Users/userName/Desktop/marsUni.mp4"));
    mp->setMedia(*mc);
   QVideoWidget *vw = new QVideoWidget(this);
   vw->setMaximumSize(704, 576);
   vw->setMinimumSize(704, 576);

   mp->setVideoOutput(vw);
   this->setCentralWidget(vw);
   vw->show();
   mp->play();
}

MainWindow::~MainWindow()
{
    delete ui;
}

If I uncomment the line containing QUrl::fromLocalFile, I can display the local file. If I open a network connection in VLC pointing to http://96.10.1.168/mjpg/video.mjpg, I can display the camera stream. I am on Mac OS 10.9.

Why cant I stream the video from the IP camera? Any comments or debugging suggestion would be appreciated.


回答1:


You'll need to use the QNetworkRequest constructor to access an HTTP resource.

The specification for this is in the documentation for the QMediaContent constructor:

QMediaContent::QMediaContent(const QNetworkRequest & request)

Constructs a media content with request providing a reference to the content.

This constructor can be used to reference media content via network protocols such as HTTP. This may include additional information required to obtain the resource, such as Cookies or HTTP headers.

http://doc.qt.io/qt-5/qmediacontent.html#QMediaContent-3



来源:https://stackoverflow.com/questions/30494319/qt-stream-ip-camera-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!