gstreamer学习笔记:将音视频合成MPEG2-TS流并打包通过rtp传输

时间:2023-01-02 08:42:41

一、通过rtpbin插件发送

(1)发送端

gst-launch -v gstrtpbin name=rtpbin latency=100 mpegtsmux name="mux" ! rtpmp2tpay pt=96 ! rtpbin.send_rtp_sink_0 rtpbin.send_rtp_src_0 \

! udpsink host=localhost port=5002 async=false sync=false videotestsrc pattern=ball !x264enc ! mux. \

filesrc audiotestsrc ! ffenc_aac ! aacparse ! mux.


补充:gstreamer1.0版本命令:

gst-launch-1.0 -v rtpbin name=rtpbin latency=100 mpegtsmux name=mux ! rtpmp2tpay pt=96 ! rtpbin.send_rtp_sink_0 rtpbin.send_rtp_src_0
! udpsink host=172.26.178.209 port=5002 async=false videotestsrc pattern=ball ! "video/x-raw,framerate=60/1,width=360,height=240" !
avenc_mpeg2video ! mux. audiotestsrc ! avenc_aac compliance=-2 ! mux.

说明:1.0用x264enc编码器编码,接收端显示不出图像不知道为啥,于是用avenc_mpeg2video代替之。

流程图:

gstreamer学习笔记:将音视频合成MPEG2-TS流并打包通过rtp传输

(2)接收端

gst-launch -v gstrtpbin name=rtpbin latency=100 udpsrc caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)MP2T-ES,\
ssrc=(uint)2461487073,payload=(int)96,clock-base=(uint)1108150669,seqnum-base=(uint)44671" port=5002 ! rtpbin.recv_rtp_sink_0 rtpbin. ! queue2 ! \
rtpmp2tdepay ! mpegtsdemux name=demux demux. ! decodebin2 ! queue2 ! ffmpegcolorspace ! videoscale ! autovideosink sync=false async=false \
demux. ! decodebin2 ! autoaudiosink

补充:gstreamer1.0版命令:

gst-launch-1.0 -v rtpbin name=rtpbin latency=100 udpsrc caps="application/x-rtp, media=(string)video, clock-rate=(int)90000,
encoding-name=(string)MP2T, ssrc=(uint)4280388371, payload=(int)96, timestamp-offset=(uint)696115925, seqnum-offset=(uint)1582"
port=5002 ! queue2 ! rtpbin.recv_rtp_sink_0 rtpbin. ! queue2 ! rtpmp2tdepay ! queue2 ! tsdemux name=demux demux. ! queue2 !
decodebin ! audioconvert ! "audio/x-raw,layout=(string)interleaved,rate=(int)44100,channels=(int)2" ! autoaudiosink demux. !
queue2 ! avdec_mpeg2video ! "video/x-raw,framerate=60/1,width=360,height=240" ! videoconvert ! ximagesink

流程图:

gstreamer学习笔记:将音视频合成MPEG2-TS流并打包通过rtp传输


二、通过Rtsp发送

(1)发送端

./rtsp_launch "mpegtsmux name=mux ! rtpmp2tpay name=pay0 pt=96 videotestsrc pattern=ball !
"video/x-raw,framerate=60/1,width=360,height=240" ! avenc_mpeg2video ! mux. audiotestsrc ! avenc_aac compliance=-2 ! mux."


注:rtsp_launch为官方源码编译的工具,源码如下;

/* GStreamer
* Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#include <gst/gst.h>

#include <gst/rtsp-server/rtsp-server.h>

#define DEFAULT_RTSP_PORT "8554"

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://172.26.178.105:%s/test\n", port);
g_main_loop_run (loop);

return 0;
}


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

编译:(需要下载安装gst-rtsp-server-1.xx.tar.xz;路径:https://gstreamer.freedesktop.org/src/gst-rtsp-server/)

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

(2)接收端:

gst-launch-1.0 -v rtspsrc location=rtsp://172.26.178.105:8554/test ! queue2 ! rtpmp2tdepay ! queue2 ! tsdemux name=demux
demux. ! queue2 ! decodebin ! audioconvert ! "audio/x-raw,layout=(string)interleaved,rate=(int)44100,channels=(int)2" !
autoaudiosink demux. ! queue2 ! avdec_mpeg2video ! "video/x-raw,framerate=60/1,width=360,height=240" ! videoconvert ! ximagesink

以上命令亲测可用,学习Gstreamer中,持续更新。