linux_手把手教ubuntu搭建rtsp视频推送服务

公司业务需要使用rtsp服务,搜索了几个rtsp源,但都非常不稳定,并且无法指定特定视频作为源。为了方便开发测试,决定自行建立rtsp服务器。

live555编译安装启动

编译

1
2
3
4
5
wget  http://www.live555.com/liveMedia/public/live555-latest.tar.gz
tar xzf live555-latest.tar.gz
cd live
./genMakefiles linux-64bit #注意后面这个参数是根据当前文件夹下config.<后缀>获取得到的
make

启动:cd mediaServer && ./live555MediaServer
打印出这些就说明编译安装成功了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@localhost mediaServer]# ./live555MediaServer 
LIVE555 Media Server
version 0.89 (LIVE555 Streaming Media library version 2016.06.26).
Play streams from this server using the URL
rtsp://192.168.0.111/<filename> #这个就是访问的url地址
where <filename> is a file present in the current directory.
Each file's type is inferred from its name suffix:
".264" => a H.264 Video Elementary Stream file
".265" => a H.265 Video Elementary Stream file
".aac" => an AAC Audio (ADTS format) file
".ac3" => an AC-3 Audio file
".amr" => an AMR Audio file
".dv" => a DV Video file
".m4e" => a MPEG-4 Video Elementary Stream file
".mkv" => a Matroska audio+video+(optional)subtitles file
".mp3" => a MPEG-1 or 2 Audio file
".mpg" => a MPEG-1 or 2 Program Stream (audio+video) file
".ogg" or ".ogv" or ".opus" => an Ogg audio and/or video file
".ts" => a MPEG Transport Stream file
(a ".tsx" index file - if present - provides server 'trick play' support)
".vob" => a VOB (MPEG-2 video with AC-3 audio) file
".wav" => a WAV Audio file
".webm" => a WebM audio(Vorbis)+video(VP8) file
See http://www.live555.com/mediaServer/ for additional documentation.
(We use port 80 for optional RTSP-over-HTTP tunneling, or for HTTP live streaming (for indexed Transport Stream files only).)

填充视频

将视频放到和live555MediaServer同路径下就可以了。
需要留意的是live555并不支持mp4格式,需要将mp4转为mkv

1
ffmpeg -i xxx.mp4 xxx.mkv

此时用播放软件播放地址:

1
2
3
vlc rtsp://192.168.0.111/xxx.mkv
or
ffplay rtsp://192.168.0.111/xxx.mkv

花屏问题01缓冲区大小

视频的前几秒钟可能会有花屏问题,网上查了查,原因在于缓冲区大小不足。需要修改缓冲区大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
live555推送之后的视频流出现花屏,查看源码DynamicRTSPServer.cpp文件,源码如下:
sms->addSubsession(MPEG4VideoFileServerMediaSubsession::createNew(env, fileName, reuseSource));
} else if (strcmp(extension, ".264") == 0) {
// Assumed to be a H.264 Video Elementary Stream file:
NEW_SMS("H.264 Video");
OutPacketBuffer::maxSize = 100000; // allow for some possibly large H.264 frames
sms->addSubsession(H264VideoFileServerMediaSubsession::createNew(env, fileName, reuseSource));
} else if (strcmp(extension, ".265") == 0) {
// Assumed to be a H.265 Video Elementary Stream file:
NEW_SMS("H.265 Video");
OutPacketBuffer::maxSize = 100000; // allow for some possibly large H.265 frames
sms->addSubsession(H265VideoFileServerMediaSubsession::createNew(env, fileName, reuseSource));
} else if (strcmp(extension, ".mp3") == 0) {
// Assumed to be a MPEG-1 or 2 Audio file:
NEW_SMS("MPEG-1 or 2 Audio")

查看上面红色部分对于H264和H265输出包最大缓冲100000字节(100K),对于高清视频缓冲区太小了,必需更改大些。目前更改到800000,对于1080P视频使用VLC播放时,不会再出现花屏。
可用如下信息查询出需要修改那些文件:grep -rnw . -e ‘OutPacketBuffer::maxSize = ‘,需要修改buff的文件
修改后花屏问题可能存在,但视频卡顿问题会得到解决。

花屏问题02视频格式

花屏问题依然存在,本人曾经尝试过用h264格式代替mkv,发现live555无法识别h264格式,偶然扫到命令提示部分,发现live555支持的h264需要的扩展格式为264,将h264扩展改为264,发现花屏问题得到解决,(但是解决的并不完美)。
视频处理:

1
2
ffmpeg -i xxx.mp4 xxx.h264
mv xxx.h264 xxx.264

将视频复制到live555MediaServer同路径下,启动后连接rtsp地址,发现花屏问题得以解决。
小视频一般都是ok的(小于10M),大视频则会有问题(大于200M),vlc连接大视频的rtsp时依然会出现花屏问题。

1
2
3
opencv连接rtsp:ok
ffplay连接rtsp:ok
vlc连接rtsp:花屏

猜测是vlc对部分码流数据兼容不佳,或者视频源有问题,总之3个播放工具2个没问题,可以认为rtsp是ok的。

无法推送到外网(腾讯云)

使用腾讯云主机建立rtsp服务器,发现生成的pull地址IP并非外网ip,而是172开头的ip。
首先:live555是如何获取ip的?
发现在 live555源码的groupsock/GroupsockHelper.cpp 中 live555是通过连接本机的15947端口来确定自己的ip的。
其实就是eth0网卡地址
腾讯云运行:ip addr show eth0
发现的确是172开头的ip

为何呢?因为腾讯云搞了鬼,详见参考文献《腾讯云公网IP无法访问(公网IP无法绑定-监听)的解决办法》
监控172地址的话,从外网访问必然无法访问到,导致外网无法拉流。

怎么办呢?让其监控0.0.0.0,如何让其监控0呢?
从其他博文可以看出,groupsock/GroupsockHelper.cpp的函数ipv4AddressBits ourIPAddress(UsageEnvironment& env)的返回值就是ip。将其返回值修改为0即可。

修改后,需要重新编译源码。

参考:
live555学习心得二(获取本地IP地址方法1):https://blog.csdn.net/wesleyluo/article/details/6204635
腾讯云公网IP无法访问(公网IP无法绑定-监听)的解决办法:https://blog.csdn.net/chenggong2dm/article/details/51475222

报错:The input frame data was too large for our buffer size

修改01:vim live/liveMedia/MultiFramedRTPSource.cpp
line76:increaseReceiveBufferTo(env, RTPgs->socketNum(), 2000*1024);
修改02:vim live/liveMedia/StreamParser.cpp
line26:BANK_SIZE 600000.
修改03:vim live/liveMedia/MediaSink.cpp
line113: unsigned OutPacketBuffer::maxSize = 600000;
修改04:vim live/mediaServer/DynamicRTSPServer.cpp
修改line 141,146,202,208:OutPacketBuffer::maxSize = 600000;
修改06:vim live/testProgs/playCommon.cpp
line116: unsigned fileSinkBufferSize = 600000;
重新编译

参考:
live555: The input frame data was too large for our buffer size 解决方法:https://caibiao-lee.blog.csdn.net/article/details/59520914
Live555:MultiFramedRTPSink::afterGettingFrame1(): The input frame data was too large for our buffer:https://blog.csdn.net/xionglifei2014/article/details/90266162

参考

用VLC做流媒体服务器:https://blog.csdn.net/redstarofsleep/article/details/49273405
win:利用live555搭建最简单的rtsp流媒体服务:https://blog.csdn.net/huweijian5/article/details/53928521
使用live555 在linux下搭建 rtsp server:https://www.cnblogs.com/dpf-10/p/5623101.html
nginx+rtmp:https://hub.docker.com/r/datarhei/nginx-rtmp/
nginx+rtsp:https://hub.docker.com/r/srnbckr/nginx-rtsp
ffmpeg+ffserver搭建rtsp服务器:https://blog.csdn.net/FPGATOM/article/details/98782202
live555推送1080p花屏:https://blog.csdn.net/youyicc/article/details/79862762
流媒体开发之开源项目live555—更改server端的帧率大小和码率大小:https://www.cnblogs.com/pengkunfan/p/3975442.html

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×