一、环境配置
由于最新版本VLC播放器中已经不包含SDK,特贴出下载链接方便各位朋友进行下载(本人下载的是最新版本vlc-3.0.4-win64)。
下载链接为 http://download.videolan.org/pub/videolan/vlc/
1.windows端
(1)拷贝压缩包vlc-3.0.4-win64\vlc-3.0.4\sdk
中的include和lib文件夹
到个人项目中。
(2)拷贝压缩包vlc-3.0.4-win64\vlc-3.0.4\
中的plugins文件夹
和libvlc.dll、libvlccore.dll
至可执行程序目录。
(3)配置项目引用。(本人使用VS2013,属性-》配置属性-》C/C++ -》常规-》附加包含目录、属性-》配置属性-》链接器 -》常规-》附加库目录)
2.linux端
详情请见github https://github.com/alonegiveup/vlc_demo
以上配置完成后即可使用libvlc库进行rtsp取流实验。
二、代码实现
vlc_reader.h
#pragma once
#include
#include
#include "vlc/vlc.h"
#include "opencv2/opencv.hpp"
class vlc_reader
{
public:
vlc_reader(char* url = 0);
~vlc_reader();
void start(int wantW = 640, int wantH = 480);
void pause(bool paused);
cv::Mat frame();
int w, h;
private:
char* rtspAddress = "";
std::mutex mtx;
std::queue video_queue;
libvlc_instance_t *inst;
libvlc_media_player_t *mp;
unsigned char *pixel;
static void *cb_lock(void *opaque, void **plane);
static void cb_unlock(void *opaque, void *picture, void * const *plane);
static void cb_display(void *opaque, void *picture);
};
vlc_reader.cpp
#include "vlc_reader.h"
#pragma comment(lib, "libvlc.lib")
#pragma comment(lib, "libvlccore.lib")
#define MAX_QUEUE 50 //最大队列空间
vlc_reader::vlc_reader(char*url)
: inst(0), mp(0), pixel(0), w(0), h(0), rtspAddress(url)
{
//vlc构建参数 测试--network-caching=300效果较好
const char * const vlc_args[] = {
"-I", "dummy",
"--ignore-config",
"--network-caching=300",
"--rtsp-tcp",
"--no-audio",
"--extraintf=logger",
"--verbose=1"
};
printf("%d\n", sizeof(vlc_args) / sizeof(vlc_args[0]));
inst = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args);
mp = libvlc_media_player_new(inst);
libvlc_video_set_callbacks(mp, &cb_lock, &cb_unlock, &cb_display, this);//设置回调函数
}
vlc_reader::~vlc_reader()
{
delete[] pixel; //释放图像数据储存内存
std::queue empty;
swap(empty, video_queue); //释放队列存储空间
libvlc_media_player_stop(mp); //释放vlc句柄
libvlc_media_player_release(mp);
libvlc_release(inst);
}
void vlc_reader::start(int wantW, int wantH)
{
this->w = wantW;
this->h = wantH;
if (pixel == nullptr)
{
pixel = new unsigned char[wantW*wantH * 4];
}
libvlc_media_player_pause(mp);
libvlc_media_t *media = libvlc_media_new_location(inst, rtspAddress);
libvlc_media_player_set_media(mp, media);
libvlc_media_release(media);
libvlc_video_set_format(mp, "RGBA", wantW, wantH, wantW * 4);//设置取流参数 以RGBA格式取流
libvlc_media_player_play(mp);
}
void vlc_reader::pause(bool paused)
{
if (mp){
libvlc_media_player_set_pause(mp, paused);
}
}
cv::Mat vlc_reader::frame() //从队列中取出一帧数据
{
cv::Mat img;
if (!this->video_queue.empty())
{
img = this->video_queue.front();
this->video_queue.pop();
}
return img;
}
void* vlc_reader::cb_lock(void *opaque, void **plane)
{
vlc_reader *p = (vlc_reader*)opaque;
p->mtx.lock();
*plane = p->pixel;
return *plane;
}
void vlc_reader::cb_unlock(void *opaque, void *picture, void * const *plane)
{
vlc_reader *p = (vlc_reader*)opaque;
unsigned char *pix = (unsigned char*)picture;
if (pix)
{
cv::Mat img = cv::Mat(cv::Size(p->w, p->h), CV_8UC4, pix);
cv::cvtColor(img, img, CV_RGBA2BGR);
if (p->video_queue.size() < MAX_QUEUE)
{
p->video_queue.push(img);
}
else
{
std::queue empty;
swap(empty, p->video_queue);
}
p->mtx.unlock();
}
}
void vlc_reader::cb_display(void *opaque, void *picture)
{
}
vlc_test.cpp
#include "vlc_reader.h"
int main()
{
vlc_reader r("rtsp://admin:[email protected]:554/cam/realmonitor?channel=1&subtype=0");
vlc_reader r2("rtsp://admin:[email protected]:554/Streaming/Channels/101?transportmode=unicast ");
int rtsp_w = 640, rtsp_h = 480;
r.start(rtsp_w, rtsp_h);
r2.start(rtsp_w, rtsp_h);
while (1)
{
cv::Mat frame = r.frame();
cv::Mat frame2 = r2.frame();
if (!frame.empty())
{
imshow("test", frame);
int ch = cv::waitKey(1);
if (ch > 0)
break;
}
if (!frame2.empty()){
imshow("test2",frame2);
int ch = cv::waitKey(1);
if (ch > 0)
break;
}
}
return 0;
}
以上即可完成,rtsp取流显示。若各位使用中发现任何问题,请及时和我联系。我会尽我可能给各位提供帮助。