QT 创建一个线程播放监控视频

Posted 远方是什么样子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QT 创建一个线程播放监控视频相关的知识,希望对你有一定的参考价值。

 

1.创建一个线程类(PlayVideoThread)

PlayVideoThread.h头文件

#pragma once

#include <QObject>
#include "DataDefine.h"
#include "hi_sdk_api.h"

class PlayVideoThread : public QObject
{
    Q_OBJECT

public:
    PlayVideoThread();
    ~PlayVideoThread();
    void setParam(VIDEO_CHANNEL ch, int win);

public slots:
    void doWork();

signals:
    void sglOnMessage(VIDEO_STATUS_EVENT event, QString msg);

signals:
    void sglSendHandle(HI_HANDLE loginHandle, HI_S32 play);

private:
    VIDEO_CHANNEL m_Ch;
    int m_Win;

    HI_HANDLE m_lLoginHandle;
    HI_S32 m_lPlay;
};

 

PlayVideoThread.cpp源文件

#include "PlayVideoThread.h"


PlayVideoThread::PlayVideoThread()
{
}

PlayVideoThread::~PlayVideoThread()
{
}

void PlayVideoThread::setParam(VIDEO_CHANNEL ch, int win)
{
    m_Ch = ch;
    m_Win = win;
}

void PlayVideoThread::doWork()
{
    HI_S32 err;

    /* 成功将返回 HI_HANDLE 句柄,失败返回 0 */
    m_lLoginHandle = HI_SDK_Login(m_Ch.device.ip.toStdString().c_str(), m_Ch.device.user.toStdString().c_str(), m_Ch.device.password.toStdString().c_str(), m_Ch.device.port, &err);

    emit sglOnMessage(m_lLoginHandle == 0 ? E_LOGIN_FAILURE : E_LOGIN_SUCCESS, "login err");
    emit sglOnMessage(E_DEBUG_LOG, m_lLoginHandle == 0 ? "login err" : "login success");

    HI_S_STREAM_INFO info;
    info.u32Channel = HI_CHANNEL_1;
    info.blFlag = HI_TRUE;
    info.u32Mode = HI_STREAM_MODE_TCP;
    info.u8Type = HI_STREAM_ALL;

    /* 成功返回 #define HI_SUCCESS  0 ,失败返回错误代码。*/
    m_lPlay = HI_SDK_RealPlay(m_lLoginHandle, (HI_VOID*)m_Win, &info);

    emit sglOnMessage(m_lPlay != 0 ? E_REALPLAY_FAILURE : E_REALPLAY_SUCCESS, "play err");
    emit sglOnMessage(E_DEBUG_LOG, m_lPlay == 0 ? "play err" : "play success");

    //登录并播放成功后发送句柄
    emit sglSendHandle(m_lLoginHandle, m_lPlay);
}

 

2.调用这个线程类:

LZYVideo.h头文件

#pragma once

#include <BaseVideo.h>
#include "PlayVideoThread.h"
#include "hi_sdk_api.h"
#include <QThread>

class LZYVideo : public BaseVideo
{
    Q_OBJECT

public:
    LZYVideo(QObject *parent=0);
    ~LZYVideo();
    int startRealPlay(VIDEO_CHANNEL ch, int win) ;
    int stopRealPlay() ;private:
    HI_HANDLE m_lLoginHandle;
    HI_S32 m_lPlay;

    VIDEO_CHANNEL m_VideoChannel;

    PlayVideoThread* m_pPlayWorker;
    QThread* m_pPlayWorkerThread;

private:
    void createThreadForPlayVideo(VIDEO_CHANNEL ch, int win);
    void destroyThreadForPlayVideo();

private slots:
    void sltGetHandle(HI_HANDLE loginHandle, HI_S32 play);
};

 

LZYVideo.cpp源文件

 
#include "LZYVideo.h"


void LZYVideo::createThreadForPlayVideo(VIDEO_CHANNEL ch, int win)   //创建线程
{
    destroyThreadForPlayVideo();
    m_pPlayWorker = new PlayVideoThread;
    m_pPlayWorker->setParam(ch, win);
    m_pPlayWorkerThread = new QThread(this);
    m_pPlayWorker->moveToThread(m_pPlayWorkerThread);
    connect(m_pPlayWorkerThread, SIGNAL(finished()), m_pPlayWorkerThread, SLOT(deleteLater()));
    connect(m_pPlayWorkerThread, SIGNAL(finished()), m_pPlayWorker, SLOT(deleteLater()));
    connect(m_pPlayWorkerThread, SIGNAL(started()), m_pPlayWorker, SLOT(doWork()));

    connect(m_pPlayWorker, SIGNAL(sglOnMessage(VIDEO_STATUS_EVENT, QString)), this, SIGNAL(sglOnMessage(VIDEO_STATUS_EVENT, QString)));
    connect(m_pPlayWorker, SIGNAL(sglSendHandle(HI_HANDLE, HI_S32)), this, SLOT(sltGetHandle(HI_HANDLE, HI_S32)));

    m_pPlayWorkerThread->start();
}

void LZYVideo::destroyThreadForPlayVideo()
{
    if (m_pPlayWorkerThread && m_pPlayWorkerThread->isRunning())
    {
        m_pPlayWorkerThread->exit();
        m_pPlayWorkerThread->wait();
        m_pPlayWorkerThread->deleteLater();
        m_pPlayWorkerThread = NULL;
    }
}

int LZYVideo::startRealPlay(VIDEO_CHANNEL ch, int win)
{
    m_VideoChannel = ch;

    stopRealPlay();

    createThreadForPlayVideo(ch, win);   //创建一个线程播放视频
    
    return 0;
}

void LZYVideo::sltGetHandle(HI_HANDLE loginHandle, HI_S32 play)
{
    m_lLoginHandle = loginHandle;
    m_lPlay = play;

    destroyThreadForPlayVideo();
}

 

 

插入的代码框都不能删掉。。。服了

 

以上是关于QT 创建一个线程播放监控视频的主要内容,如果未能解决你的问题,请参考以下文章

Qt编写安防视频监控系统2-视频播放

Qt视频播放窗口叠加透明窗口

Qt编写的RTSP播放器+视频监控(android版本)

Qt + OpenCV 使用 std::thread 播放视频

QT+ffmpeg+多线程的视频播放器的基本使用

Qt编写的项目作品12-简易视频播放器