Reputation: 95
I am creating a streaming application in QT using libvlc, which will play the RTSP streaming coming from any source. I have created it and it is working fine for me except that whenever i want to close it my application crashes. My code is as below.
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>
I am using Qt 4.8 and Microsoft visual studio 2010 to build it. It is streaming fine from server. I am using Vlc Media player to create a streaming server that is streaming a video. But when i try to close this it is crashing down my window. I have checked for several errors & fond that libvlc is unable to process command libvlc_media_player_stop() . Thanks for any suggestions.
Upvotes: 0
Views: 2399
Reputation: 95
Found the mainProblem in the code. The main problem was due to multithreading because multiple threads were unable to communicate while we called libvlc_media_player_stop() in threadVLC.cpp. So we have to forcefully terminate the thread either, or we have to skip threading. libvlc_media_player_stop() is working fine. I have searched about it on the videoLAN forums. https://forum.videolan.org/viewtopic.php?t=106415
Thanks RSATom for suggesting me about Forums.
Upvotes: 1