SRS使用
SRS介绍
Github :https://github.com/ossrs/srs
运行部署SRS服务器
docker run -d -it -p 1935:1935 -p 1985:1985 -p 8080:8080 --restart=always registry.cn-hangzhou.aliyuncs.com/ossrs/srs:4 ./objs/srs -c conf/docker.conf
拷贝配置文件到宿主机方持久化
docker cp 28aa:/conf /liuylData/srs/conf
1935:默认rtmp地址端口
8080:http协议端口、SRS后台管理端口
SRS管理端
服务概览:
访问以下地址进入服务器资源概览,包括当前网络带宽,内存消耗,cpu消耗等
http://192.168.23.56:8080/console/ng_index.html#/summaries
在线视频测试:
访问以下地址 进入在线视频测试地址
http://192.168.23.56:8080/players/srs_player.html?schema=http
推流
推流命令
ffmpeg -i "rtsp://username:pwd@172.16.36.34:554/Streaming/Channels/103?transportmode=unicast&profile=Profile_3" -r 5 -c copy -f flv rtmp://192.168.23.56:1935/live/livestream
如需要移除音频轨道,添加–an即可
ffmpeg -i "rtsp://username:pwd@172.16.36.34:554/Streaming/Channels/103?transportmode=unicast&profile=Profile_3" -r 5 -c copy -an -f flv rtmp://192.168.23.56:1935/live/livestream
-c copy命令将视频复制到rtmp://192.168.23.56:1935/live/livestream
-an告诉FFmpeg不要复制音频
响应以下表示成功
PS F:\workcode\Aiot> ffmpeg -i "rtsp://admin:Dswy8866@172.16.36.34:554/Streaming/Channels/103?transportmode=unicast&profile=Profile_3" -r 5 -c copy -f flv rtmp://192.168.23.56:1935:1935/live/livestream
ffmpeg version N-107055-g73302aa193-20220606 Copyright (c) 2000-2022 the FFmpeg developers
built with gcc 11.2.0 (crosstool-NG 1.24.0.533_681aaef)
configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-shared --disable-static --disable-w32threads --enable-pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvorbis --enable-opencl --disable-libpulse --enable-libvmaf --disable-libxcb --disable-xlib --enable-amf --enable-libaom --enable-libaribb24 --enable-avisynth --enable-libdav1d --enable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --enable-frei0r --enable-libgme --enable-libass --enable-libbluray --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librist --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --disable-libdrm --disable-vaapi --enable-libvidstab --enable-vulkan --enable-libshaderc --enable-libplacebo --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libzimg --enable-libzvbi --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexeflags= --extra-libs=-lgomp --extra-version=20220606
libavutil 57. 26.100 / 57. 26.100
libavcodec 59. 33.100 / 59. 33.100
libavformat 59. 24.100 / 59. 24.100
libavdevice 59. 6.100 / 59. 6.100
libavfilter 8. 40.100 / 8. 40.100
libswscale 6. 6.100 / 6. 6.100
libswresample 4. 6.100 / 4. 6.100
libpostproc 56. 5.100 / 56. 5.100
Input #0, rtsp, from 'rtsp://admin:Dswy8866@172.16.36.34:554/Streaming/Channels/103?transportmode=unicast&profile=Profile_3':
Metadata:
title : Media Presentation
Duration: N/A, start: 0.000000, bitrate: N/A
Stream #0:0: Video: h264 (Main), yuvj420p(pc, bt709, progressive), 704x576, 10 fps, 25 tbr, 90k tbn
Stream #0:1: Audio: aac (LC), 16000 Hz, mono, fltp
Output #0, flv, to 'rtmp://1192.168.23.56:1935:1935/live/livestream':
Metadata:
title : Media Presentation
encoder : Lavf59.24.100
Stream #0:0: Video: h264 (Main) ([7][0][0][0] / 0x0007), yuvj420p(pc, bt709, progressive), 704x576, q=2-31, 10 fps, 25 tbr, 1k tbn
Stream #0:1: Audio: aac (LC) ([10][0][0][0] / 0x000A), 16000 Hz, mono, fltp
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[flv @ 0000028c5f83c740] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly
frame= 89 fps= 15 q=-1.0 size= 890kB time=00:00:09.02 bitrate= 807.9kbits/s speed=1.54x
SRS 接口
直接在浏览器中就可以访问,或者用curl发起http请求。
SRS提供了api的面包屑,可以从根目录开始导航,不需要任何记忆。一般的字段包括:
code表示错误码,按照linux惯例,0表示成功。 urls表示是面包屑导航,该api下面的子api(链接)。 data表示最后一级提供服务的api,返回的数据。 例如调用根路径
# curl http://192.168.1.102:1985/
{
"code": 0,
"urls": {
"api": "the api root"
}
}
返回的urls表示子链接可以访问。接着访问:
curl http://192.168.1.102:1985/api/
{
"code": 0,
"urls": {
"v1": "the api version 1.0"
}
}
继续
# curl http://192.168.1.102:1985/api/v1/
{
"code": 0,
"server": "vid-51f0qq4",
"urls": { //所有支持的接口
"versions": "the version of SRS",
"summaries": "the summary(pid, argv, pwd, cpu, mem) of SRS",
"rusages": "the rusage of SRS",
"self_proc_stats": "the self process stats",
"system_proc_stats": "the system process stats",
"meminfos": "the meminfo of system",
"authors": "the license, copyright, authors and contributors",
"features": "the supported features of SRS",
"requests": "the request itself, for http debug",
"vhosts": "manage all vhosts or specified vhost",
"streams": "manage all streams or specified stream",
"clients": "manage all clients or specified client, default query top 10 clients",
"raw": "raw api for srs, support CUID srs for instance the config",
"clusters": "origin cluster server API",
"perf": "System performance stat",
"tcmalloc": "tcmalloc api with params ?page=summary|api"
},
"tests": {
"requests": "show the request info",
"errors": "always return an error 100",
"redirects": "always redirect to /api/v1/test/errors",
"[vhost]": "http vhost for http://error.srs.com:1985/api/v1/tests/errors"
}
}
例如访问clients查看所有连接的客户端,包括推流、拉流端
# curl http://192.168.1.102:1985/api/v1/clients/
{
"code": 0,
"server": "vid-51f0qq4",
"clients": [
{
"id": "p6n0544s",
"vhost": "vid-3j376yo",
"stream": "vid-0i60625",
"ip": "172.168.70.90",
"pageUrl": "",
"swfUrl": "",
"tcUrl": "rtmp://192.168.1.102/srs/33",
"url": "/srs/33/live/26F3570DE6B9A31A19BF72FF74DE8712A81A75ABB71DD1BA0A902DB78F4991DB",
"type": "rtmp-play",
"publish": false,
"alive": 617.01
},
{
"id": "u0t2s30u",
"vhost": "vid-3j376yo",
"stream": "vid-0i60625",
"ip": "172.168.70.90",
"pageUrl": "",
"swfUrl": "",
"tcUrl": "rtmp://192.168.1.102/srs/33",
"url": "/srs/33/live/26F3570DE6B9A31A19BF72FF74DE8712A81A75ABB71DD1BA0A902DB78F4991DB",
"type": "fmle-publish",
"publish": true,
"alive": 754.91
}
]
}
API参数查询
SRS接口支持按id
查询,只需要把id作为接口路径的最后一部分即可,例如查询client id为p6n0544s 的客户端
curl http://192.168.1.102:1985/api/v1/clients/p6n0544s
{
"code": 0,
"server": "vid-51f0qq4",
"client": {
"id": "p6n0544s",
"vhost": "vid-3j376yo",
"stream": "vid-0i60625",
"ip": "172.168.70.90",
"pageUrl": "",
"swfUrl": "",
"tcUrl": "rtmp://192.168.1.102:1935/srs/33",
"url": "/srs/33/live/26F3570DE6B9A31A19BF72FF74DE8712A81A75ABB71DD1BA0A902DB78F4991DB",
"type": "rtmp-play",
"publish": false,
"alive": 899.12
}
}
问题
在测试中会发生对同一路视频5个客户端拉流会出现服务器会发生以下错误
SRS Server:
[2022-07-02 15:22:57.618][Warn][1][39r3q9c5][4] 1 frames left in the queue on closing
[2022-07-02 15:22:57.618][Trace][1][39r3q9c5] TCP: before dispose resource(RtmpConn)(0x123a430), conns=7, zombies=0, ign=0, inz=0, ind=0
[2022-07-02 15:22:57.618][Error][1][39r3q9c5][4] serve error code=1011 : service cycle : rtmp: stream service : rtmp: publish timeout 5000ms, nb_msgs=7362
thread [1][39r3q9c5]: do_cycle() [src/app/srs_app_rtmp_conn.cpp:217][errno=4]
thread [1][39r3q9c5]: service_cycle() [src/app/srs_app_rtmp_conn.cpp:414][errno=4]
thread [1][39r3q9c5]: do_publishing() [src/app/srs_app_rtmp_conn.cpp:916][errno=62](Interrupted system call)
FFmpeg client:
[flv @ 0000014b1741eb80] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly
[rtsp @ 0000014b1736c080] max delay reached. need to consume packet=1007.4kbits/s speed=0.99x
[rtsp @ 0000014b1736c080] RTP: missed 19 packets
av_interleaved_write_frame(): End of fileB time=00:03:01.24 bitrate=1006.7kbits/s speed=0.965x
[flv @ 0000014b1741eb80] Failed to update header with correct duration.
[flv @ 0000014b1741eb80] Failed to update header with correct filesize.
Error writing trailer of rtmp://192.168.23.56:1935/live/livestream: End of file
frame= 4602 fps= 25 q=-1.0 Lsize= 22293kB time=00:03:04.14 bitrate= 991.7kbits/s speed=0.981x
video:21833kB audio:689kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Error closing file rtmp://192.168.23.56:1935/live/livestream: Error number -10053 occurred
Conversion failed!
参数使用错误:
ffmpeg -i "rtsp://admin:pwd@host/Streaming/Channels/103?transportmode=unicast&profile=Profile_3" -c:v libx265 -crf 26 -preset fast -b:a 128k -r 5 -c:a aac -c copy -f flv rtmp://192.168.67.66:1935/live/livestream
输出提示信息如下
Multiple -c, -codec, -acodec, -vcodec, -scodec or -dcodec options specified for stream 0, only the last option '-c copy' will be used.
缺少解码器错误
例如音频编码改为pcm,通过ffmpeg收到音频流时,如没有pcm解码器,会有以下表现
元数据描述中的音频流#0:1 为 Audio: none
Stream #0:0: Video: h264 (Main), yuvj420p(pc, bt709, progressive), 704x576, 25 fps, 25 tbr, 90k tbn
Stream #0:1: Audio: none, 16000 Hz, mono
黄色提示信息中提示如下,找不到解码器
Could not find codec parameters for stream 1 (Audio: none, 16000 Hz, mono): unknown codec
Consider increasing the value for the 'analyzeduration' (0) and 'probesize' (5000000) options
缺少编码器错误
Decoder (codec none) not found for input stream #0:1
解决办法
1、安装缺失编码器插件 2、用参数 -an 移除音频流
ffmpeg -i "rtsp://admin:pwd@host/Streaming/Channels/103?transportmode=unicast&profile=Profile_3" -c:v libx265 -crf 26 -preset fast -b:a 128k -r 5 -vcodec copy -an -f flv rtmp://192.168.67.66:1935/live/livestream
reference
ffmpeg命令使用 https://www.kancloud.cn/cpp-jdxia/cpp/1139257