fix: 兼容 a=fmtp:<pt> 不带参数的 SDP,修复 WebRTC 播放返回 400 (#4870)

This commit is contained in:
Honghao Zhang
2026-11-02 15:07:11 +08:00
committed by GitHub
parent fcb2889ad1
commit fb3265725f
3 changed files with 110 additions and 5 deletions

View File

@@ -35,16 +35,17 @@ find_package(PCAP QUIET)
# automatic discovery. # automatic discovery.
# 仅收录仓库中确实存在、且已实测可通过的用例。其余候选的现状: # 仅收录仓库中确实存在、且已实测可通过的用例。其余候选的现状:
# test_vp9_rtp 当前为失败状态,待单独修复后并入 # test_vp9_rtp 当前为失败状态,待单独修复后并入
# test_rtcp_nack/test_webrtc_regression 仅在启用 WebRTC 时构建,尚未验证 # test_rtcp_nack 仅在启用 WebRTC 时构建,尚未验证
# Only cases that exist in the repository and are verified to pass are listed. The remaining # Only cases that exist in the repository and are verified to pass are listed. The remaining
# candidates: test_vp9_rtp currently fails and will be added once fixed; test_rtcp_nack and # candidates: test_vp9_rtp currently fails and will be added once fixed; test_rtcp_nack is built
# test_webrtc_regression are built only with WebRTC enabled and are not verified yet. # only with WebRTC enabled and is not verified yet.
set(GATING_TESTS set(GATING_TESTS
test_av1_rtmp test_av1_rtmp
test_amf test_amf
test_hls_cookie_data test_hls_cookie_data
test_sortor test_sortor
test_rtx_twcc) test_rtx_twcc
test_webrtc_regression)
foreach(TEST_SRC ${TEST_SRC_LIST}) foreach(TEST_SRC ${TEST_SRC_LIST})
get_filename_component(TEST_EXE_NAME ${TEST_SRC} NAME_WE) get_filename_component(TEST_EXE_NAME ${TEST_SRC} NAME_WE)

View File

@@ -100,12 +100,108 @@ void testDeleteWebrtcLocationQueryRoundTrip() {
"encoded token should round-trip through query parsing"); "encoded token should round-trip through query parsing");
} }
// issue #4863 中真实摄像机的 DESCRIBE SDP:a=fmtp:96 只有 payload type、没有参数
// The real camera DESCRIBE SDP from issue #4863, whose a=fmtp:96 has no parameters at all
string makeCameraSdpWithoutFmtpParams() {
return
"v=0\r\n"
"o=- 1109162014219182 1109162014219192 IN IP4 127.0.0.1\r\n"
"s=Media Presentation\r\n"
"c=IN IP4 0.0.0.0\r\n"
"t=0 0\r\n"
"a=range:npt=now-\r\n"
"m=video 0 RTP/AVP 96\r\n"
"a=rtpmap:96 H264/90000\r\n"
"a=fmtp:96\r\n"
"a=control:trackID=1\r\n";
}
// 源流 SDP 中 a=fmtp:<pt> 不带参数(部分摄像机/NVR 的 SDP 如此)时,应视为没有 fmtp 参数,而不是解析失败
// a=fmtp:<pt> without parameters (as some cameras/NVRs write it) means "no fmtp" rather than a parse error
void testFmtpWithoutParameters() {
RtcSession session;
session.loadFrom(makeCameraSdpWithoutFmtpParams());
auto media = session.getMedia(TrackType::TrackVideo);
expect(media != nullptr, "video m-line should be parsed");
expect(media->plan.size() == 1, "video m-line should have one codec plan");
expect(media->plan[0].pt == 96, "payload type should be 96");
expect(media->plan[0].codec == "H264", "codec should be H264");
expect(media->plan[0].fmtp.empty(), "a=fmtp without parameters should yield an empty fmtp map");
// 带参数的 fmtp 行为必须保持不变
// fmtp with parameters must keep working as before
RtcSession with_params;
with_params.loadFrom(
"v=0\r\no=- 1 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\n"
"m=video 0 RTP/AVP 96\r\na=rtpmap:96 H264/90000\r\n"
"a=fmtp:96 packetization-mode=1;profile-level-id=420029\r\n");
auto media2 = with_params.getMedia(TrackType::TrackVideo);
expect(media2 != nullptr, "video m-line should be parsed");
expect(media2->plan.size() == 1, "video m-line should have one codec plan");
expect(media2->plan[0].fmtp.size() == 2, "two fmtp parameters should be kept");
expect(media2->plan[0].getFmtp("packetization-mode") == "1", "packetization-mode should be kept");
expect(media2->plan[0].getFmtp("profile-level-id") == "420029", "profile-level-id should be kept");
}
// 浏览器侧 offer(只保留 H264 视频),用于校验摄像机流仍能协商出视频
// Browser side offer (H264 video only), to check the camera stream still negotiates a video track
string makeBrowserVideoOffer() {
return
"v=0\r\n"
"o=- 0 0 IN IP4 127.0.0.1\r\n"
"s=-\r\n"
"t=0 0\r\n"
"a=group:BUNDLE 0\r\n"
"m=video 9 UDP/TLS/RTP/SAVPF 96\r\n"
"c=IN IP4 0.0.0.0\r\n"
"a=ice-ufrag:remoteUfrag\r\n"
"a=ice-pwd:remotePassword1234567890\r\n"
"a=fingerprint:sha-256 "
"00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:"
"00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF\r\n"
"a=setup:actpass\r\n"
"a=mid:0\r\n"
"a=recvonly\r\n"
"a=rtcp-mux\r\n"
"a=rtpmap:96 H264/90000\r\n"
"a=fmtp:96 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f\r\n";
}
// 摄像机流(fmtp 无参数)作为播放源时,仍应协商出 H264 视频,而不是直接失败
// A camera stream whose fmtp has no parameters should still negotiate H264 video instead of failing
void testPlayCameraStreamWithoutFmtpParams() {
SdpAttrFingerprint local_fingerprint;
local_fingerprint.algorithm = "sha-256";
local_fingerprint.hash =
"FF:EE:DD:CC:BB:AA:99:88:77:66:55:44:33:22:11:00:"
"FF:EE:DD:CC:BB:AA:99:88:77:66:55:44:33:22:11:00";
RtcConfigure configure;
configure.setDefaultSetting("localUfrag", "localPassword1234567890", RtpDirection::sendrecv, local_fingerprint);
configure.setPlayRtspInfo(makeCameraSdpWithoutFmtpParams());
RtcSession offer;
offer.loadFrom(makeBrowserVideoOffer());
auto answer = configure.createAnswer(offer);
expect(answer != nullptr, "answer should be created for a camera stream without fmtp parameters");
auto video = answer->getMedia(TrackType::TrackVideo);
expect(video != nullptr, "answer should keep the video m-line");
expect(!video->plan.empty(), "answer should select one codec plan");
expect(video->plan[0].codec == "H264", "answer should select H264");
}
} // namespace } // namespace
int main() { int main() {
try { try {
testBundleOnlyDatachannelAnswer(); testBundleOnlyDatachannelAnswer();
testDeleteWebrtcLocationQueryRoundTrip(); testDeleteWebrtcLocationQueryRoundTrip();
testFmtpWithoutParameters();
testPlayCameraStreamWithoutFmtpParams();
cout << "test_webrtc_regression passed" << endl; cout << "test_webrtc_regression passed" << endl;
return 0; return 0;
} catch (const exception &ex) { } catch (const exception &ex) {

View File

@@ -589,7 +589,15 @@ string SdpAttrRtcpFb::toString() const {
void SdpAttrFmtp::parse(const string &str) { void SdpAttrFmtp::parse(const string &str) {
auto pos = str.find(' '); auto pos = str.find(' ');
CHECK_SDP(pos != string::npos); // 兼容 a=fmtp:<pt> 不带参数的情况(部分摄像机/NVR 的 SDP 如此),它等价于没有 fmtp 参数,
// 不能按解析失败处理,否则整段 SDP 解析失败,WebRTC 播放会直接返回 400
// Compatible with a=fmtp:<pt> without parameters (as some cameras/NVRs write it): it means no
// fmtp parameter, and must not fail the whole SDP parsing which breaks WebRTC playback with 400
if (pos == string::npos) {
CHECK_SDP(!str.empty());
pt = atoi(str.data());
return;
}
pt = atoi(str.substr(0, pos).data()); pt = atoi(str.substr(0, pos).data());
auto vec = split(str.substr(pos + 1), ";"); auto vec = split(str.substr(pos + 1), ";");
for (auto &item : vec) { for (auto &item : vec) {