Gstreamer 使用
Gstreamer安装#
安装依赖包#
sudo apt-get install gtk-doc-tools libgstreamer1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-doc gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio
查看版本信息
#若成功,则有对应版本信息
gst-inspect-1.0 --version
Gstreamer推流--(gst-rstp-sever)#
下载、编译 gst-rtsp-server#
git clone git://anongit.freedesktop.org/gstreamer/gst-rtsp-server
cd gst-rtsp-server
./autogen.sh
make
进入examples目录就能看到已经编译好的各种示例程序以及源码。
如果编译错误
configure: error: You need to have gtk-doc >= 1.12 installed to build GStreamer RTSP Server Library
configure failed
安装gtk-doc
sudo apt-get install gtk-doc-tools
推流#
cd examples
./test-launch "( videotestsrc ! x264enc ! rtph264pay name=pay0 pt=96 )"
Opencv拉流解码#
测试在NVIDA Jetson TX2上拉流解码,硬解码和gpu使用
参考 https://blog.csdn.net/hello_dear_you/article/details/129290712
查看 opencv 是否支持 gstreamer#
opencv 中可以通过调用 getBuildInformance 函数查看 opencv 的编译情况,具体如下所示:
#include <opencv2/opencv.hpp>
int main(void)
{
std::cout << cv::getBuildInformation() << std::endl;
}
通过运行上述命令可以得到如下输出,其中 Video I/O 中可以找到 Gstreamer 选项是否打开。
Gstreamer软解码#
默认使用cpu进行软解码
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace cv;
using namespace std;
int main(int, char**)
{
Mat frame;
//--- INITIALIZE VIDEOCAPTURE
VideoCapture cap;
string rtsp_url = "rtsp://admin:password@192.168.170.xxx:554";
int apiID = cv::CAP_ANY; // 0 = autodetect default API
// open rtsp stream using selected API
cap.open(rtsp_url, apiID);
// check if we succeeded
if (!cap.isOpened()) {
cerr << "ERROR! Unable to open camera\n";
return -1;
}
//--- GRAB AND WRITE LOOP
cout << "Start grabbing" << endl
<< "Press any key to terminate" << endl;
for (;;)
{
// wait for a new frame from camera and store it into 'frame'
cap.read(frame);
// check if we succeeded
if (frame.empty()) {
cerr << "ERROR! blank frame grabbed\n";
break;
}
// show live and wait for a key with timeout long enough to show images
imshow("Live", frame);
if (waitKey(5) >= 0)
break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
Gstreamer Jetson硬解码#
gstreamer 进行硬件解码的流程如下图所示:
demo代码
#include <opencv2/opencv.hpp>
#include <iostream>
#include <chrono>
int main()
{
using std::chrono::steady_clock;
typedef std::chrono::milliseconds milliseconds_type;
const int interval = 15;
std::stringstream ss;
std::string rtsp_url = "rtsp://admin:password@192.168.170.xxx:554";
size_t latency = 200;
size_t frame_width = 1920;
size_t frame_height = 1080;
size_t framerate = 15;
ss << "rtspsrc location=" << rtsp_url << " latency=" << latency << " ! application/x-rtp, media=video, encoding-name=H264 "
<< "! rtph264depay ! video/x-h264, clock-rate=90000, width=" << frame_width << ", height=" << frame_height << ", framerate="
<< framerate << "/1 ! nvv4l2decoder ! video/x-raw(memory:NVMM), width=" << frame_width << ", height=" << frame_height
<< ", framerate=" << framerate << "/1 ! nvvideoconvert ! video/x-raw, format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink";
std::cout << ss.str() << std::endl;
cv::VideoCapture cap = cv::VideoCapture(ss.str(), cv::CAP_GSTREAMER);
if(!cap.isOpened())
{
std::cerr << "error to open camera." << std::endl;
return -1;
}
std::cout << cv::getBuildInformation() << std::endl;
cv::Mat frame ;
steady_clock::time_point start = steady_clock::now();
size_t frame_idx = 0;
while(1)
{
bool ret = cap.read(frame);
if(ret)
{
// cv::imwrite("tmp.jpg", frame);
++frame_idx;
}
if(frame_idx % interval == 0)
{
steady_clock::time_point end = steady_clock::now();
milliseconds_type span = std::chrono::duration_cast<milliseconds_type>(end - start) ;
std::cout << "it took " << span.count() / frame_idx << " millisencods." << std::endl;
start = end;
}
}
return 0;
}
资料收集#
https://vinming.github.io/2022/03/22/gstreamer_basic_tutorial_10_gstreamer_tools/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)