要进行音视频相关的开发,除了需要掌握音视频相关的协议、格式、工具、语言、库以外,模拟环境也是非常重要的条件。
有了模拟环境,我们才能随时测试、更改自己写的音视频代码。比如,我们要进行RTSP播放器开发,就需要模拟RTSP媒体源;要进行RTSP客户端开发,就需要模拟的RTSP服务器;要进行RTSP服务器开发,就需要模拟的推流、拉流。
用vlc模拟RTSP媒体源
搭建测试环境的RTSP有很多方法。比较简单的是使用vlc,vlc可以把一个视频文件转成RTSP服务器。比如:
vlc -vvv /home/mp4/a.mp4 --sout "#rtp{sdp=rtsp://127.0.0.1:8554/}"
,就可以使用:
vlc rtsp://127.0.0.1:8554/
来播放/home/mp4/a.mp4 文件了。
使用rtsp-simple-server 模拟RTSP服务器
还可以使用开源的简单的RTSP服务器,如:rtsp-simple-server 。
rtsp-simple-server的第一个优点是依赖简单,开箱即用,只需要下载二进制包以后,执行
./rtsp-simple-server
即可。如果要更改配置,只需编辑同目录下的rtsp-simple-server.yml,比如我把rtsp端口号改成了9000。
# Address of the TCP/RTSP listener. This is needed only when encryption is "no" or "optional".
#rtspAddress: :8554
rtspAddress: :9000
# Address of the TCP/TLS/RTSPS listener. This is needed only when encryption is "strict" or "optional".
rtspsAddress: :8322
# Address of the UDP/RTP listener. This is needed only when "udp" is in protocols.
rtpAddress: :8000
# Address of the UDP/RTCP listener. This is needed only when "udp" is in protocols.
rtcpAddress: :8001
启动以后,终端会显示:
2024/08/23 10:39:14 INF rtsp-simple-server v0.20.1
2024/08/23 10:39:14 INF [RTSP] listener opened on :9000 (TCP), :8000 (UDP/RTP), :8001 (UDP/RTCP)
2024/08/23 10:39:14 INF [RTMP] listener opened on :1935
2024/08/23 10:39:14 INF [HLS] listener opened on :8888
rtsp-simple-server另一个优点是功能丰富,不仅支持RTSP,还支持RTMP、HLS,而且RTSP/RTMP还支持推流。以下是官方源码里的功能列表:
protocol | description | variants | publish | read | proxy |
---|---|---|---|---|---|
RTSP | fastest way to publish and read streams | RTSP, RTSPS | ✔️ | ✔️ | ✔️ |
RTMP | allows to interact with legacy software | RTMP, RTMPS | ✔️ | ✔️ | ✔️ |
HLS | allows to embed streams into a web page | Low-Latency HLS, standard HLS | ❌ | ✔️ | ✔️ |
ffmpeg推流RTSP、拉流RTSP
有了支持推流的RTSP服务器,就可以使用ffmpeg把本地文件推成一路RTSP的流媒体。如:
ffmpeg -re -i /home/mp4/a.mp4 -c copy -rtsp_transport tcp -f rtsp rtsp://127.0.0.1:9000/video1
推流/home/mp4/a.mp4 到了9000端口上的RTSP服务的/video1下。
服务端rtsp-simple-server输出:
2024/08/23 10:42:23 INF [RTSP] [conn 127.0.0.1:51632] opened
2024/08/23 10:42:23 INF [RTSP] [session 818710836] created by 127.0.0.1:51632
2024/08/23 10:42:23 INF [RTSP] [session 818710836] is publishing to path 'video1', with TCP, 2 tracks (H264 , MPEG4Audio)
ffmpeg推流端输出:
Output #0, rtsp, to 'rtsp://127.0.0.1:9000/video1':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
description : Packed by Bilibili XCoder v2.0.2
encoder : Lavf60.16.100
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1920x1080, q=2-31, 361 kb/s, 25 fps, 25 tbr, 90k tbn (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0]
Stream #0:1(und):
Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 92 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]Stream mapping:
Stream #0:0 -> #0:0 (copy) Stream #0:1 -> #0:1 (copy)Press [q] to stop, [?] for help[out#0/rtsp @ 0x558477e7b380] video:189kB audio:2kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknownsize=N/A time=00:00:04.92 bitrate=N/A speed=1.11x
就可以使用RTSP客户端播放rtsp://127.0.0.1:9000/video1 了。
比如可以使用ffplay:
ffplay rtsp://127.0.0.1:9000/video1
,或者vlc:
vlc rtsp://127.0.0.1:9000/video1