天天看点

基于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推流类三、测试代码

继续阅读