我正在使用libvlc在QT中创建流媒体应用程序,它将播放来自任何来源的RTSP流。我创建它并且它对我来说很好,除了每当我想要关闭它我的应用程序崩溃。我的代码如下。
ThreadVlc.h
<pre>
#ifndef THREADVLC_H
#define THREADVLC_H
#include <vlc\vlc.h>
#include <vlc\libvlc.h>
#include <QtGui>
#include <QWidget>
#include <QThread>
#include <QApplication>
class vlcOnQtPlayer : public QThread
{
Q_OBJECT
bool isVideoPlayingCheck;
libvlc_instance_t *libvlcInstance;
libvlc_media_player_t *libvlcMediaPlayer;
libvlc_media_t *libvlcMedia;
QString rtspLink;
QMutex mmutex;
public:
vlcOnQtPlayer(WId parentWinId);
void stop();
protected:
void run();
public slots:
void vlcOnQtSetLink(QString link);
//void playRtspLink();
int onVlcStreamCapture(QString imageSavePath);
private:
WId windowId;
};
#endif
</pre>
ThreadVlc.cpp
<pre>
#include "ThreadVlc.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QFrame>
#include<iostream>
#include<QMessageBox>
using namespace std;
vlcOnQtPlayer::vlcOnQtPlayer(WId parentWinId) : QThread()
{
mmutex.lock();
windowId=parentWinId;
mmutex.unlock();
isVideoPlayingCheck=false;
}
void vlcOnQtPlayer::stop()
{
//QMessageBox::warning(0,"Warning", "before libvlc_media_player_stop");
libvlc_media_player_stop (libvlcMediaPlayer);
QMessageBox::warning(0,"Warning", "After libvlc_media_player_stop");
libvlc_media_player_release (libvlcMediaPlayer);
libvlc_release (libvlcInstance);
}
void vlcOnQtPlayer::run()
{
if(rtspLink!=NULL)
{
const char * const vlc_args[] = { "--no-audio","-vv" };
libvlcInstance=libvlc_new(sizeof(vlc_args)/sizeof(vlc_args[0]), vlc_args);
//Create a new LibVLC media descriptor
libvlcMedia = libvlc_media_new_location(libvlcInstance, rtspLink.toAscii());
libvlcMediaPlayer=libvlc_media_player_new_from_media (libvlcMedia);
// Get our media instance to use our window
libvlc_media_player_set_hwnd(libvlcMediaPlayer, windowId);//vlcOnQtVideoFrame->winId());
// Play
libvlc_media_player_play (libvlcMediaPlayer);
isVideoPlayingCheck=true;
//return 0;
}
else
isVideoPlayingCheck=false;
//return 1;
}
int vlcOnQtPlayer::onVlcStreamCapture(QString imageSavePath)
{
const char *file_Path=imageSavePath.toLocal8Bit().constData();
//Function to take the snap shot of the screen
int i=libvlc_video_take_snapshot(libvlcMediaPlayer,0,file_Path,0,0);
return i;
}
void vlcOnQtPlayer::vlcOnQtSetLink(QString link)
{
mmutex.lock();
rtspLink=link;
mmutex.unlock();
}
</pre>
ThreadVlcMainWindow.h
<pre>
#ifndef THREADVLCMAINWINDOW_H
#define THREADVLCMAINWINDOW_H
/* Including necessary classes*/
#include <QtGui>
#include <QMainWindow>
#include <QApplication>
#include <ThreadVlc.h>
#include <QThread>
#include <QEvent>
/* Proto type for the Main Window Class*/
class streamParentClass : public QMainWindow
{
Q_OBJECT
public:
int count,noOfCam; // Variables for munber of cameras and counter
streamParentClass(QWidget *parent = 0);
~streamParentClass();
QWidget *centralWidgetWindow; //Window to be set as central widget (central widget of main window)
QGridLayout *streamIfaceLayout;
QFrame * streamFrame;
vlcOnQtPlayer * streamPlayerInstance;
//QList <QPushButton *>btn;
void guiDesign(); //For basic design of controls
void addStreamWin(int); // To add vlc instances to main window
void closeEvent(QCloseEvent *);
};
#endif
</pre>
ThreadVlcMainWindow.cpp
<pre>
#include "ThreadVlcMainWindow.h"
#include <QtGui>
#include <QToolBar>
#include <QIcon>
#include <QAction>
streamParentClass::streamParentClass(QWidget *parent) : QMainWindow(parent)
{
noOfCam=1;
guiDesign(); //Initializing controls
addStreamWin(noOfCam);
}
streamParentClass::~streamParentClass()
{
for(int i=0;i<noOfCam;i++)
{
streamPlayerInstance->stop();
streamIfaceLayout->removeWidget(streamFrame);
}
qApp->quit();
}
void streamParentClass::guiDesign()
{
centralWidgetWindow=new QWidget(this);
streamIfaceLayout=new huffnetInterfaceLayout(centralWidgetWindow,4);
centralWidgetWindow->setLayout(streamIfaceLayout);
setCentralWidget(centralWidgetWindow);
}
void streamParentClass::addStreamWin(int n)
{
for(int i=0;i<n;i++)
{
streamFrame=new QFrame(centralWidgetWindow);
streamFrame->setWindowFlags(Qt::FramelessWindowHint);
streamPlayerInstance= new vlcOnQtPlayer(streamFrame->winId());
streamIfaceLayout->addWidget(streamFrame,0,0);
connect(streamPlayerInstance,SIGNAL(finished()),streamPlayerInstance,SLOT(deleteLater()));
streamPlayerInstance->vlcOnQtSetLink("rtsp://:8554/strm");
streamPlayerInstance->start();
}
}
void streamParentClass::closeEvent(QCloseEvent *event)
{
for(int i=0;i<noOfCam;i++)
{
streamPlayerInstance->stop();//~vlcOnQtPlayer();
delete streamFrame;
delete centralWidgetWindow;
//QMessageBox::warning(0,"Warning", "Inside for ");
}
qApp->quit();
}
</pre>
Main.cpp
<pre>
#include "ThreadVlcMainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
streamParentClass mp;
mp.resize(500,400);
mp.show();
return app.exec();
}
</pre>
我正在使用Qt 4.8和Microsoft visual studio 2010来构建它。它从服务器流畅。我正在使用Vlc媒体播放器创建流媒体视频流媒体服务器。但当我试图关闭它时,它正在撞毁我的窗户。我检查了几个错误&amp;喜欢libvlc无法处理命令 libvlc_media_player_stop()。谢谢你的任何建议。
答案 0 :(得分:1)
在代码中找到mainProblem。 主要问题是由于多线程,因为当我们在 threadVLC.cpp 中调用 libvlc_media_player_stop()时,多个线程无法通信。所以我们必须强制终止线程,否则我们必须跳过线程。 libvlc_media_player_stop()工作正常。我在videoLAN论坛上搜索过它。 https://forum.videolan.org/viewtopic.php?t=106415
感谢RSATom向我推荐论坛。