一、引言
通过《音视频入门基础:RTP专题(2)——使用FFmpeg命令生成RTP流》中的FFmpeg命令可以将一个媒体文件转推RTP:
ffmpeg -re -stream_loop -1 -i input.mp4 -vcodec copy -an -f rtp rtp://192.168.0.102:6005 -acodec copy -vn -sdp_file XXX.sdp -f rtp rtp://192.168.0.102:7005
默认FFmpeg的packetization-mode(打包模式)为1。通过上述FFmpeg命令生成的SDP文件,其packetization-mode的值为1,表示采用Non-interleaved mode(非交错模式):
但是某些音视频播放器只支持packetization-mode为1或其它值,这就导致播放器无法播放或解析packetization-mode为1的RTP流,或者播放时遇到报错。所以有时需要设置packetization-mode。本文讲述通过FFmpeg命令进行RTP推流时,如何设置packetization-mode。
二、设置packetization-mode为0
查看FFmpeg帮助信息:
ffmpeg --help full
查看到通过“-rtpflags h264_mode0”选项可以设置packetization-mode为0:
-rtpflags <flags> E.......... RTP muxer flags (default 0)
latm E.......... Use MP4A-LATM packetization instead of MPEG4-GENERIC for AAC
rfc2190 E.......... Use RFC 2190 packetization instead of RFC 4629 for H.263
skip_rtcp E.......... Don't send RTCP sender reports
h264_mode0 E.......... Use mode 0 for H.264 in RTP
send_bye E.......... Send RTCP BYE packets when finishing
修改转推RTP的命令如下:
ffmpeg -re -stream_loop -1 -i ckin.mp4 -rtpflags h264_mode0 -vcodec copy -an -f rtp rtp://192.168.0.102:6005 -acodec copy -vn -sdp_file XXX.sdp -f rtp rtp://192.168.0.102:7005
执行上述FFmpeg命令后生成的SDP文件,其packetization-mode的值为0,表示采用Single NAL unit mode(单一的NAL模式):
三、设置packetization-mode为2
目前FFmpeg(截止FFmpeg7.0.1)并不支持设置packetization-mode为2(Interleaved mode,交错模式)。查看FFmpeg7.0.1的源文件rtpdec_h264.c,可以看到注释如下:
/**
* @file
* @brief H.264 / RTP Code (RFC3984)
* @author Ryan Martell <[email protected]>
*
* @note Notes:
* Notes:
* This currently supports packetization mode:
* Single Nal Unit Mode (0), or
* Non-Interleaved Mode (1). It currently does not support
* Interleaved Mode (2). (This requires implementing STAP-B, MTAP16, MTAP24,
* FU-B packet types)
*/
上面的注释表示FFmpeg目前只支持设置packetization-mode的值为0和1,不支持设置为2。