天天看點

基于VLC實作RTSP推流桌面(共享桌面)一、添加VLC頭檔案和庫檔案二、封裝RTSPServer推流類三、測試代碼

基于VLC實作RTSP推流桌面(共享桌面)

  • 一、添加VLC頭檔案和庫檔案
  • 二、封裝RTSPServer推流類
  • 三、測試代碼

不清楚推流大概原理的小夥伴,參考《設定VLC播放器進行RTSP推流桌面(共享桌面)》

這裡以VLC 2.2.6版本為例,因為使用VLC 2.2.4的庫執行以下代碼,會出現崩潰的問題,不知道什麼原因。

我們下載下傳VLC播放器,其中帶有VLC的SDK,下載下傳位址:

http://download.videolan.org/vlc/2.2.6/win64/vlc-2.2.6-win64.7z

一、添加VLC頭檔案和庫檔案

我們建立一個測試工程RTSPDesktop,在pro檔案中添加如下内容:

win32 {
# VLC相關庫
LIBS += -L$$PWD/../VLC/lib -llibvlc -llibvlccore

# VLC頭檔案目錄
INCLUDEPATH += $$PWD/../VLC/include
}
           

二、封裝RTSPServer推流類

封裝一個RSTPServer類,實作推流桌面實時視訊的主要邏輯代碼。

RTSPServer.h

#ifndef RTSPSERVER_H
#define RTSPSERVER_H

#include <QString>
#include <vlc/vlc.h>

/**
 * @brief The RTSPServer class
 * 建議不要使用vlc-2.2.4,該版本推流桌面,會發生崩潰。
 * 在實際使用vlc-2.2.6下測試時,可以正常推流桌面。
 */
class RTSPServer
{
public:
    RTSPServer();
    ~RTSPServer();

    // 使用VLC實作RTSP推流桌面(共享桌面)
    // 将桌面錄制的視訊資料,推流到指定ip的端口上
    bool pushDesktop(const QString& ip, int port);

private:
    libvlc_instance_t *instance;
};

#endif // RTSPSERVER_H
           

RTSPServer.cpp

#include "RTSPServer.h"
#include <QDir>

#define DESKTOP_MEDIA_NAME      "Desktop"

RTSPServer::RTSPServer()
{
    // 建立VLC執行個體
    instance = libvlc_new (0, nullptr);
}

RTSPServer::~RTSPServer()
{
    // 停止推流
    libvlc_vlm_stop_media(instance, DESKTOP_MEDIA_NAME);

    // 釋放VLC執行個體
    libvlc_vlm_release(instance);
    instance = nullptr;
}

bool RTSPServer::pushDesktop(const QString& ip, int port)
{
    // 轉碼參數:#transcode{vcodec=h264,acodec=mpga,ab=128,channels=2,samplerate=44100,scodec=none}
    QString convertPara = "#transcode{vcodec=h264,acodec=mpga,ab=128,channels=2,samplerate=44100,scodec=none}";

    // 網絡參數:rtp{sdp=rtsp://xx.xx.xx.xx:yyyy/}
    // 表示本機ip時,可省略ip,隻寫端口,如rtp{sdp=rtsp://:8554/}
    QString netPara = "rtp{sdp=rtsp://" + ip + ":" + QString::number(port) + "/}";

    // 如sout = "#transcode{vcodec=h264,acodec=mpga,ab=128,channels=2,samplerate=44100,scodec=none}:rtp{sdp=rtsp://127.0.0.1:8554/}"
    QString sout = convertPara + ":" + netPara;

    // 選項參數,可設定推流的大小,幀率,緩存時間...
    // 測試時,此options為必須,否則推流失敗。
    const char* options[] = {"screen-top=0",
                             "screen-left=0",
                             "screen-width=640",
                             "screen-height=480",
                             "screen-fps=30"};

    // 添加名為VIDEO_MEDIA_NAME的廣播
    int ret = libvlc_vlm_add_broadcast(instance, DESKTOP_MEDIA_NAME,
                                       "screen://",
                                       sout.toStdString().c_str(),
                                       5, options, true, false);
    if (ret != 0)
    {
        return false;
    }

    // 播放該廣播
    ret = libvlc_vlm_play_media(instance, DESKTOP_MEDIA_NAME);
    return (ret == 0);
}
           

三、測試代碼

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "RTSPServer.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
    RTSPServer rtspServer;
};

#endif // MAINWINDOW_H
           

mainwindow.cpp

void MainWindow::on_pushButton_clicked()
{
    bool ret = rtspServer.pushDesktop("127.0.0.1", 8554);
    if (ret)
    {
        QMessageBox::information(nullptr, "Info", "Push streaming desktop successfully");
    }
    else
    {
        QMessageBox::information(nullptr, "Info", "Failed to push streaming desktop");
    }
    ui->pushButton->setEnabled(false);
}
           

代碼非常簡單,将目前桌面錄制實時視訊,并推流到127.0.0.1,8554端口上。

注意:運作時,依賴的動态庫有,libvlc.dll、libvlccore.dll、plugins插件,如下:

基于VLC實作RTSP推流桌面(共享桌面)一、添加VLC頭檔案和庫檔案二、封裝RTSPServer推流類三、測試代碼

程式啟動後,點選“Push Desktop”按鈕啟動推流;然後在VLC播放器中,打開如下串流位址

運作效果如下:

基于VLC實作RTSP推流桌面(共享桌面)一、添加VLC頭檔案和庫檔案二、封裝RTSPServer推流類三、測試代碼

左邊播放器中,播放推流的實時桌面視訊。

本文涉及工程代碼:

https://gitee.com/bailiyang/cdemo/tree/master/Qt/63VLCTest/RTSPDesktop

若對你有幫助,歡迎點贊、收藏、評論,你的支援就是我的最大動力!!!

同時,阿超為大家準備了豐富的學習資料,歡迎關注公衆号“超哥學程式設計”,即可領取。

基于VLC實作RTSP推流桌面(共享桌面)一、添加VLC頭檔案和庫檔案二、封裝RTSPServer推流類三、測試代碼

繼續閱讀