基于gstreamer的rtsp推送和转发

article/2025/11/7 19:24:28

基于gstreamer的rtsp推送和转发

    • 一、配置gstreamer环境
    • 二、安装gstreamer-rtsp-server
    • 三、读取usb摄像头并推rtsp流
    • 四、转发rtsp

前段时间因为实验室项目要求,需要读取摄像头并推rtsp流,由于我们实验室不是做与之相关的工作,所以并没有什么参考资料或者学长去问,只能自己在网上瞎找,也是费了很大的劲儿才最终实现,不过也只是简单推流。

本教程系统为ubuntu16.04

一、配置gstreamer环境

安装gstreamer基础库

sudo apt-get install libgl1-mesa-dev
sudo apt-get install gstreamer1.0-libav
sudo apt-get install gstreamer1.0-plugins-bad
sudo apt-get install gstreamer1.0-plugins-base
sudo apt-get install gstreamer1.0-plugins-ugly
sudo apt-get install gstreamer1.0-plugins-good

装完之后最好在bachrc中加两句路径,不加的话有时候gstreamer不好用,读不到组件。

sudo gedit ~/.bachrc   //打开bachrc文件//在bachrc文件中加下边两句,我试过只加第二句也可以
export LD_LIBRARY_PATH=/usr/local/lib
export GST_PLUGIN_PATH=/usr/local/lib:/usr/lib/x86_64-linux-gnu/gstreamer-1.0source ~/.bachrc  //在终端输入,每次改完bachrc都要source一下

加完上边两句刚开始报错,不用管它,等一会儿就好啦

做完这些基本gstreamer就能用了,可以用下边命令试一下:

gst-launch-1.0 rtspsrc latency=20 location="rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov" ! rtph264depay ! h264parse ! avdec_h264 ! autovideosink


出现上边这个画面应该就配置好环境了

二、安装gstreamer-rtsp-server

git clone  -b 1.8 https://github.com/GStreamer/gst-rtsp-server.git  //下载源码
cd gst-rtsp-server       //目录名可能不正确,自己注意点,就是刚刚下载的目录
./autogen.sh
sudo make
sudo make install

三、读取usb摄像头并推rtsp流

直接上源码:

#include <gst/gst.h>
#include <gst/rtsp-server/rtsp-server.h>
#define DEFAULT_RTSP_PORT "7551"static char *port = (char *) DEFAULT_RTSP_PORT;static GOptionEntry entries[] = {{"port", 'p', 0, G_OPTION_ARG_STRING, &port,"Port to listen on (default: " DEFAULT_RTSP_PORT ")", "PORT"},{NULL}
};int
main (int argc, char *argv[])
{GMainLoop *loop;GstRTSPServer *server;GstRTSPMountPoints *mounts;GstRTSPMediaFactory *factory;GOptionContext *optctx;GError *error = NULL;optctx = g_option_context_new ("<launch line> - Test RTSP Server, Launch\n\n""Example: \"( videotestsrc ! x264enc ! rtph264pay name=pay0 pt=96 )\"");g_option_context_add_main_entries (optctx, entries, NULL);g_option_context_add_group (optctx, gst_init_get_option_group ());if (!g_option_context_parse (optctx, &argc, &argv, &error)) {g_printerr ("Error parsing options: %s\n", error->message);g_option_context_free (optctx);g_clear_error (&error);return -1;}g_option_context_free (optctx);loop = g_main_loop_new (NULL, FALSE);/* create a server instance */server = gst_rtsp_server_new ();g_object_set (server, "service", port, NULL);/* get the mount points for this server, every server has a default object* that be used to map uri mount points to media factories */mounts = gst_rtsp_server_get_mount_points (server);/* make a media factory for a test stream. The default media factory can use* gst-launch syntax to create pipelines.* any launch line works as long as it contains elements named pay%d. Each* element with pay%d names will be a stream */factory = gst_rtsp_media_factory_new ();gst_rtsp_media_factory_set_launch (factory, argv[1]);/* attach the test factory to the /test url */gst_rtsp_mount_points_add_factory (mounts, "/test", factory);/* don't need the ref to the mapper anymore */g_object_unref (mounts);/* attach the server to the default maincontext */gst_rtsp_server_attach (server, NULL);/* start serving */g_print ("stream ready at rtsp://127.0.0.1:%s/test\n", port);g_main_loop_run (loop);return 0;
}

编译指令:

gcc test-launch.c -o test $(pkg-config --cflags --libs gstreamer-rtsp-server-1.0 gstreamer-1.0)

源码其实就是刚刚下载编译的gst-rtsp-server文件夹下边的examples文件夹中的test-launch.c文件,我一点都没改,因为主要还是在终端执行的命令。

编译完成后,会生成一个名为test的可执行文件。在当前目录下打开终端,输入:

./test "( v4l2src device=/dev/video0 norm=255 ! video/x-raw-yuv,format='fourcc'YUY2,width=640,height=480 ! x264enc tune="zerolatency" threads=4  ! rtph264pay name=pay0 pt=96 )"

其中device=/dev/video0是你读取的摄像头编号,如果是笔记本,video0应该是笔记本自带摄像头。
再开一个终端,输入:

gst-launch-1.0 rtspsrc latency=20 location="rtsp://127.0.0.1:7551/test" ! rtph264depay ! h264parse ! avdec_h264 ! autovideosink

应该就能看到你自己了

四、转发rtsp

转发rtsp和读取usb摄像头的源码可以一模一样,因为最主要的还是在执行可执行文件时的命令,为了便于大家理解,我还是对上述源码做微小的改动:

#include <gst/gst.h>
#include <gst/rtsp-server/rtsp-server.h>
#define DEFAULT_RTSP_PORT "8551"static char *port = (char *) DEFAULT_RTSP_PORT;static GOptionEntry entries[] = {{"port", 'p', 0, G_OPTION_ARG_STRING, &port,"Port to listen on (default: " DEFAULT_RTSP_PORT ")", "PORT"},{NULL}
};int
main (int argc, char *argv[])
{GMainLoop *loop;GstRTSPServer *server;GstRTSPMountPoints *mounts;GstRTSPMediaFactory *factory1;GOptionContext *optctx;GError *error = NULL;optctx = g_option_context_new ("<launch line> - Test RTSP Server, Launch\n\n""Example: \"( videotestsrc ! x264enc ! rtph264pay name=pay0 pt=96 )\"");g_option_context_add_main_entries (optctx, entries, NULL);g_option_context_add_group (optctx, gst_init_get_option_group ());if (!g_option_context_parse (optctx, &argc, &argv, &error)) {g_printerr ("Error parsing options: %s\n", error->message);g_option_context_free (optctx);g_clear_error (&error);return -1;}g_option_context_free (optctx);loop = g_main_loop_new (NULL, FALSE);/* create a server instance */server = gst_rtsp_server_new ();g_object_set (server, "service", port, NULL);/* get the mount points for this server, every server has a default object* that be used to map uri mount points to media factories */mounts = gst_rtsp_server_get_mount_points (server);/* make a media factory for a test stream. The default media factory can use* gst-launch syntax to create pipelines.* any launch line works as long as it contains elements named pay%d. Each* element with pay%d names will be a stream */* factory1 = gst_rtsp_media_factory_new ();gst_rtsp_media_factory_set_launch (factory1, argv[1]);/* attach the test factory to the /test url */gst_rtsp_mount_points_add_factory (mounts, "/test1", factory1);/* don't need the ref to the mapper anymore */g_object_unref (mounts);/* attach the server to the default maincontext */gst_rtsp_server_attach (server, NULL);/* start serving */g_print ("stream ready at rtsp://127.0.0.1:%s/test1\n", port);g_main_loop_run (loop);return 0;
}

我只是把端口号修改成8551,factory和test名字做了修改
编译指令同上,名字可以换一下:

gcc test-tra.c -o test-tra $(pkg-config --cflags --libs gstreamer-rtsp-server-1.0 gstreamer-1.0)

打开终端,执行:

./test-tra "( rtspsrc latency=20  location=rtsp://127.0.0.1:7551/test ! queue ! rtph264depay ! queue ! rtph264pay name=pay0 pt=96 )"

在执行这个命令之前要执行上边推usb摄像头的命令,但不要用gstreamer直接打开直推的码流,要用下边这个命令:

gst-launch-1.0 rtspsrc latency=20 location="rtsp://127.0.0.1:8551/test1" ! rtph264depay ! h264parse ! avdec_h264 ! autovideosink

也可以用公共的rtsp地址测试:

gst-launch-1.0 rtspsrc latency=20 location="rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov" ! rtph264depay ! h264parse ! avdec_h264 ! autovideosink

至此,基本读取usb摄像头推rtsp和转发就都实现了。


http://chatgpt.dhexx.cn/article/0vIfqDyf.shtml

相关文章

深入浅出gstreamer开发

Gstreamer解决什么问题&#xff1f; — 上层接口和应用方式的 相对稳定 与底层接口、平台环境的 多样化 。例如&#xff1a; codec 不同种类不同实现&#xff0c;音视频处理不同&#xff0c;硬件输入、输出、采集播放不同&#xff0c;芯片不同&#xff0c;操作系统不同。 — 通…

【GStreamer 】3-1 gstreamer插件之 videotestsrc 介绍

目录 ​编辑 1、简介 2、videotestsrc 3、videotestsrc 不同pattern参数测试罗列 3.1 (0): smpte - SMPTE 100% color bars 3.2 (1): snow - Random (television snow) 3.3 (2): black - 100% Black ​编辑 3.4 checkers 方块 ​编辑 3.5 几何图形 4、videotestsrc…

Gstreamer概述

1、什么是GStreamer GStreamer 是用来构建流媒体应用的开源多媒体框架(framework)&#xff0c;其基本设计思想来自于俄勒冈(Oregon)研究生学院有关视频管道的创意, 同时也借鉴了DirectShow的设计思想。其目标是要简化音/视频应用程序的开发&#xff0c;已经能够被用来处理像 M…

gstreamer简介

常用 gchar * caps_string gst_caps_to_string (new_selected_caps); g_free (caps_string); 需要弄懂的问题 tunnel tee queue 最后列一下Gstreamer中常见的时间宏&#xff0c;注意Gstreamer中的时间单位是&#xff1a;纳秒 #define G_USEC_PER_SEC 1000000 #define GST_S…

Gstreamer基础知识介绍

由于deepstream是基于gstreamer的&#xff0c;所以要想在deepstream上做拓展&#xff0c;需要对gstreamer有一定的认识。以下主要介绍Gstreamer整体框架和Gstreamer基础概念。 一、Gstreamer整体框架 gstreamer是一个用于开发流式多媒体应用的开源框架。本身这个框架是为了更…

【GStreamer 】1-扫盲介绍

从历史的角度来看&#xff0c;Linux 在多媒体方面已经远远落后于其它的操作系统。微软的Windows和苹果的MacOS它们对多媒体设备、多媒体创作、播放和实时处理等方面已经有了很好的支持。另一方面&#xff0c;Linux对多媒体应用的综合贡献比较少&#xff0c;这也使得Linux很难在…

详细的GStreamer开发教程

详细的GStreamer开发教程 文章目录 详细的GStreamer开发教程1. 什么是GStreamer&#xff1f;2. GStreamer架构2.1 Media Applications2.2 Core Framework2.3 Plugins 3. GStreamer组件3.1 Element创建一个 GstElement 3.2 箱柜&#xff08;bin&#xff09;元件的状态 3.3 衬垫&…

gstreamer(一)入门和概述

一&#xff0e;概述 在音视频领域接触最多实现的方案通常是通过ffmpeg&#xff08;PC和sever端居多&#xff09;或者硬件厂家的的SDK实现特定硬件的编解码功能&#xff08;机顶盒&#xff0c;电视等嵌入式设备&#xff09;。这里我们介绍一个在国内不太常用的解决方案----gstr…

二、什么是GStreamer

GStreamer是一个用于创建流媒体应用程序的框架。基本的设计来自俄勒冈研究生院的视频管道&#xff0c;还有一些来自DirectShow的想法。 GStreamer的开发框架使编写任何类型的流媒体应用程序成为可能。GStreamer框架旨在使编写处理音频或视频或两者同时处理的应用程序变得容易。…

Gstreamer 应用开发:1-基础介绍

我们之前的系列&#xff0c;正式的介绍了Gstreamer&#xff0c;并且围绕如何使用USB相机推流实现RTSP服务器来做了介绍&#xff0c;并在Jeston TX1 平台上做了优化急速的一些探索。 今天我们开始围绕如何用命令实现一个音视频混合&#xff0c;或者单独的音频&#xff0c;和单独…

【gstreamer】入门介绍

概述 GStreamer是一个基于流媒体的框架&#xff0c;是一个开放源代码的多媒体框架&#xff0c;用于创建音频和视频处理应用程序。它是一个运行在多个操作系统上的跨平台框架&#xff0c;支持多种不同的多媒体格式。 GStreamer框架的核心是基于插件的体系结构&#xff0c;插件…

Gstreamer基础讲解

Gstreamer讲解 文章目录 Gstreamer讲解基础背景小结 元件&#xff08;Element&#xff09;衬垫(Pads)Gstreamer的面向对象Gstreamer的多线程 实用工具Gstreamer常用插件介绍gstreamer工程记录关于YUV的补充知识 基础 背景 ​ 从历史的角度来看&#xff0c;Linux在多媒体方面已…

Gstreamer 简介

转载自&#xff1a;John.Leng - 博客园http://www.cnblogs.com/xleng/ 什么是Gstreamer&#xff1f; Gstreamer是一个支持Windows&#xff0c;Linux&#xff0c;Android&#xff0c; iOS的跨平台的多媒体框架&#xff0c;应用程序可以通过管道&#xff08;Pipeline&#xff0…

微博视频怎么下载?微博视频下载和保存工具

本文转载自&#xff1a;怎么下载微博视频 ​​​微博视频怎么下载&#xff1f;当你刷微博看到自己特别喜欢的视频时&#xff0c;除了转发该视频到自己的微博外&#xff0c;你可能还想把这个微博视频保存到自己电脑或者手机中&#xff0c;那应该怎么实现weibo视频的下载呢&#…

GitHub 标星 120K+!这些神器仅需一行代码即可下载全网视频!

大家好&#xff0c;我是 JackTian。 今天这篇文章的内容对于经常浏览各大视频网站的同学来说&#xff0c;是一大神器工具。当你看到自己目前所需的视频时想尽各种办法保存至本地&#xff0c;方便后期再次回看。恰巧有些视频可能需要会员才能够下载&#xff0c;有些第三方的视频…

那些你可能不知道的视频下载奇技淫巧

之前已经写过好几篇的奇技淫巧系列了&#xff0c;今天说说怎么快速下载视频。 我平常经常看视频的网站就是爱奇艺&#xff0c;b站&#xff0c;微博&#xff0c;知乎&#xff0c;网易这些。 这里以微博为例&#xff0c;下载周杰伦的《晴天》&#xff0c;微博地址是 https://w…

部分主流视频网站下载方法

如果在微博或其他视频网站上看到非常喜欢的视频&#xff0c;想下载下来&#xff0c;但是又苦于不知如何下载。 这里我推荐一个网站&#xff0c;支持主流的86视频网站&#xff08;发表博客时&#xff09;的视频下载。 主流视频网站(16)&#xff1a; 搜狐视频 乐视网 酷6网 56…

快手火山抖音视频怎么快速去重消重和去水印秒拍视频批量采集下载怎么快速去重消重去水印视频批量采...

快手火山抖音视频怎么快速去重消重和去水印 秒拍视频批量采集下载 怎么快速去重消重去水印 视频批量采集下载软件&#xff0c;怎么找到视频批量采集下载软件 趣头条今日头条等自媒体视频批量采集下载软件有哪些&#xff1f; 大鱼号视频批量采集下载去重消重去水印软件 视频怎么…

硕鼠——前些年很好用的视频下载神器

前言 首先解释一下为什么要说是前几年。因为它太好用&#xff0c;知道的人越来越多&#xff0c;导致引起了视频平台的注意&#xff0c;以版权为由禁止自己的视频从硕鼠下载。这里推荐小众软件Annie&#xff0c;在我的另一篇中有介绍&#xff0c;传送门。 官方网站 http://w…

网页保存视频最有效的几种方法

最近朋友问我了这样的问题&#xff1a; 如何才能把网络上的视频下载下来当做课堂资料播放呢&#xff1f; 的确&#xff0c;这个问题也困过我&#xff1a; 想要下载视频作为课件素材播放&#xff1a; 在微博收藏了许多有关「技能学习」的视频&#xff0c;但等到想看的时候发…