GStreamer跨PC RTP音视频同步录制问题求助
音视频RTP传输同步问题解决方案
问题背景
两台独立PC分别采集视频、音频并通过RTP发送到局域网笔记本,合成MP4时出现音视频不同步。发送端命令如下:
- 视频发送:
gst-launch-1.0 -v v4l2src ! videoconvert ! x264enc ! rtph264pay ! udpsink host=192.168.0.14 port=5000
- 音频发送:
gst-launch-1.0 -v alsasrc ! audioconvert ! audioresample ! audio/x-raw,channels=1 ! audioresample ! audio/x-raw,rate=48000 ! rtpL24pay ! udpsink host=192.168.0.14 port=5001
接收端当前命令:
gst-launch-1.0 -e -v udpsrc port=5000 caps="application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264" ! rtpjitterbuffer drop-on-latency=true latency=200 ! rtph264depay ! h264parse ! queue ! mux. udpsrc port=5001 caps="application/x-rtp, media=(string)audio, clock-rate=(int)48000, encoding-name=(string)L24" ! rtpjitterbuffer drop-on-latency=true latency=200 ! rtpL24depay ! audioconvert ! audioresample ! lamemp3enc ! queue ! mux. mp4mux name=mux ! filesink location=output.mp4
问题核心
- 两台发送端PC系统时钟未同步,RTP流时间戳基准存在偏差
- 接收端缓冲配置过小,无法抵消时钟偏差与传输延迟
- 音频编码环节(
lamemp3enc)引入额外延迟,加剧同步错位 mp4mux未针对实时流优化时间戳对齐逻辑
修复方案
1. 优化发送端命令(可选,提升时间戳稳定性)
给视频流添加帧率约束,简化音频流冗余处理:
- 优化后视频发送命令:
gst-launch-1.0 -v v4l2src ! videoconvert ! x264enc ! video/x-h264,framerate=30/1 ! rtph264pay pt=96 ! udpsink host=192.168.0.14 port=5000
- 优化后音频发送命令:
gst-launch-1.0 -v alsasrc ! audioconvert ! audioresample ! audio/x-raw,channels=1,rate=48000 ! rtpL24pay pt=97 ! udpsink host=192.168.0.14 port=5001
2. 调整接收端命令(核心修复)
增大缓冲空间、优化同步逻辑、替换低延迟音频编码:
gst-launch-1.0 -e -v \ udpsrc port=5000 caps="application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! \ rtpjitterbuffer drop-on-latency=true latency=500 ! \ rtph264depay ! h264parse ! queue sync=true ! mux. \ udpsrc port=5001 caps="application/x-rtp, media=(string)audio, clock-rate=(int)48000, encoding-name=(string)L24, payload=(int)97" ! \ rtpjitterbuffer drop-on-latency=true latency=500 ! \ rtpL24depay ! audioconvert ! audioresample ! aacenc ! queue sync=true ! mux. \ mp4mux name=mux streamable=true ! filesink location=output.mp4
3. 关键调整说明
rtpjitterbuffer latency=500:增大缓冲时间,抵消两台发送端的时钟偏差,避免因微小延迟导致的帧丢弃与同步错位queue sync=true:强制队列使用流自身时间戳同步,确保音视频帧按正确时序进入封装器mp4mux streamable=true:针对实时流优化时间戳处理逻辑,提升封装同步精度- 替换
lamemp3enc为aacenc:AAC编码延迟更低,且MP4原生支持更好,减少编码环节引入的同步误差
额外建议
若局域网内PC时钟偏差较大,可在两台发送端开启NTP时钟同步,从根源缩小时间基准差异。
内容的提问来源于stack exchange,提问作者Alex Cocora
相关产品推荐
相关产品推荐

