Commit Graph

4676 Commits

Author SHA1 Message Date
Xiaofeng Wang
fcb2889ad1 fix: makeRtp增加负载长度校验 (#4857)
RTP 包的长度字段为 16 位,但 `RtpInfo::makeRtp` 未校验入参长度:分配内存时
长度被截断为 16 位,拷贝数据时却使用原始的 `size_t` 长度,二者并不一致;被
截断的长度字段同样无法正确表示 RTSP interleaved 包长。

## 改动

- `src/Rtsp/RtpCodec.cpp`:在相加、分配内存和写头部之前,拒绝超过
  `UINT16_MAX - kRtpHeaderSize`(65523)的长度,抛出 `std::length_error`。
  允许的最大负载恰好填满 RTSP interleaved 长度字段,不会产生被截断的头部。
- `src/Rtsp/RtpCodec.h`:补充接口注释,说明异常行为。
- `src/Rtsp/RtspMuxer.cpp`:在 `inputFrame`/`flush` 中捕获异常,避免 MTU
  配置过大时异常扩散到事件循环。
- `webrtc/WebRtcPlayer.cpp`:跳过超长 config 帧,并保持后续包的 seq 序号对齐。

## 兼容性

65523 字节的上限不会拒绝任何合法负载:其余 `makeRtp` 调用点的长度均由
`getMaxSize()`(MTU - 12)限制,而 UDP 数据报本身最大也只有 65507 字节。
正常 MTU 下的大帧仍按原有逻辑分片,行为不变。

## 验证

在 `ENABLE_ASAN=ON` + `ENABLE_WEBRTC=ON` 下编译通过,并以 ASan 运行了
长度边界(0 / 4 / 1388 / 65523 接受,65524 及以上拒绝)、muxer 异常边界
与 WebRTC config 帧路径的检查,均无内存错误。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-10-25 21:53:57 +08:00
yand9807
095eb47f48 修复接收 RTX 包时遗漏 TWCC 到包记录 (#4859) 2026-10-25 21:52:56 +08:00
xiongziliang
bb83e697a6 修复OpenSSL 3.6 X509_get_subject_name返回值类型变更兼容问题 2026-10-22 20:44:47 +08:00
baigao
08329b39bf fix: 修复ICEAgent Data Indications协议处理仅注册到第一个实例问题 (#4848) 2026-10-12 20:42:31 +08:00
xiongziliang
b91e75e516 修复ext-codec静态库插件未链接问题 2026-10-10 22:49:37 +08:00
xiongziliang
49ea0e736f 重构codec、webrtc插件注册为懒加载方式 2026-10-10 21:50:10 +08:00
夏楚
63629a3ebc 修复AV1 RTP打包相关bug (#4846) 2026-10-09 16:53:32 +08:00
pannal
46220e6a86 Fix Enhanced RTMP AV1 metadata and VPx packet framing (#4840)
Co-authored-by: xiongziliang <771730766@qq.com>
2026-10-08 15:06:05 +08:00
YuLi
de04a56c2b fix(rtmp): bound leading AMF compatibility parsing (#4842)
### Motivation
- Prevent a memory-amplification DoS in RTMP DATA/CMD handling where the
compatibility helper decoded arbitrary leading AMF values (including
large strict arrays) while searching for the first string.
- Support the observed non-standard publisher case (two leading AMF0
numbers) while avoiding unbounded recursive decoding and
attacker-controlled allocations.

### Description
- Replace the unbounded `amfLoadLeadingString()` behavior that
repeatedly `load<AMFValue>()` with a bounded compatibility rule that
skips at most two AMF0 `Number` values and then requires a string
(`amfLoadLeadingString` in `src/Rtmp/amf.cpp` and declaration in
`src/Rtmp/amf.h`).
- Add `friend` linkage for the helper in `AMFDecoder` and update header
comments to document the tightened compatibility semantics.
- Extend `tests/test_amf.cpp` with regression checks covering:
zero/one/two leading numbers, rejection of complex leading values (e.g.
strict array), excessive leading numbers, and AMF3 string handling.
- No new source files added and existing file UTF-8 BOMs were preserved.

### Testing
- Ran configuration and unit build: `cmake -S . -B build
-DENABLE_TESTS=ON -DCMAKE_BUILD_TYPE=Debug` then `cmake --build build
--target test_amf -j8` and the build completed successfully.
- Executed the test binary via CTest: `ctest --test-dir build -R
'^test_amf$' --output-on-failure` and `test_amf` passed (1/1 tests
passed).
- Ran `git diff --check` and ensured no trailing whitespace or check
errors after changes.

------
[Codex
Task](https://chatgpt.com/codex/cloud/tasks/task_e_6abb8adc6f5c8320a84e8b198e28f68b)
2026-10-07 11:57:48 +08:00
YuLi
57f206e726 Avoid HLS cookies retaining HTTP sockets while preserving Session info (#4841) 2026-09-30 20:37:51 +08:00
xiongziliang
de040511d2 跨域配置改为支持域名白名单 2026-09-29 11:31:43 +08:00
xiongziliang
ed3925e950 新增DASH协议支持 (#4822) 2026-09-29 11:16:55 +08:00
xiongziliang
e6c19bfea7 修复全局变量构造顺序紊乱相关的bug (#4836) 2026-09-29 11:16:55 +08:00
YuLi
10268396b8 feat(hls): m3u8增加EXT-X-PROGRAM-DATE-TIME标签 (#4831)
按RFC 8216第4.3.2.6节,在每个切片的EXTINF之前写入该切片首帧对应的服务器
系统时间(UTC的ISO 8601格式,含毫秒),供播放器对齐字幕流、标记事件点。
直播m3u8与点播vod.m3u8均已支持。

默认关闭,由hls.programDateTime配置项开启。

时间统一以UTC输出而不采用本地时区偏移,因为ZLToolKit的getGMTOff()不含夏令
时修正、且在夏令时切换后不会刷新,会使标注的偏移与实际时刻相差1小时。

索引文件会被反复重建(点播模式下切片不删除,每次重建都遍历全部切片),故取值
在切片入列时格式化一次并缓存,避免在makeIndexFile()中对全部切片重复格式化。

close #4769
2026-09-22 11:54:01 +08:00
xiongziliang
eb2601ee1d fix(codec): 修复VP8 RTP解码器中的缓冲区越界访问问题 2026-09-21 00:43:25 +08:00
xiongziliang
1d7f4db6ca fix(security): 防止通过setServerConfig接口绕过下载权限限制 2026-09-21 00:19:51 +08:00
xiongziliang
cd7b0a943b fix(codec): 修复H264 RTP解析中的缓冲区读越界问题 2026-09-20 23:50:33 +08:00
xiongziliang
dc84508ce5 fix(rtmp): 修复RTMP协议消息解析读越界问题 2026-09-20 23:43:11 +08:00
xiongziliang
5b5de37a17 fix(security): 防止HTTP响应头注入漏洞 2026-09-20 23:13:54 +08:00
xiongziliang
3e5bd1cfdc fix(srt): 修复握手扩展消息解析中的内存越界问题 2026-09-20 23:05:45 +08:00
xiongziliang
4a2db9e4b7 fix(codec): 修复H264和H265 RTP解码器中的小载荷读越界问题 2026-09-20 22:55:29 +08:00
xiongziliang
205150d72b fix(webrtc): 修复RTP扩展头部解析中的堆越界读写漏洞 2026-09-20 22:49:09 +08:00
xiongziliang
2fd8d479ed fix(srt): 解决NAK数据包解析中的缓冲区溢出问题 2026-09-20 22:17:53 +08:00
xiongziliang
18531d3ee9 fix(api): 避免在getServerConfig响应中泄露密钥信息 2026-09-20 22:01:18 +08:00
xiongziliang
3f684a653c config(rtc): 默认关闭某些非必要端口 2026-09-20 21:57:58 +08:00
taotaobujue
6676b8d8fc fix(webrtc): 修复异常 ICE 会话导致的端口资源泄漏 (#4782)
公网部署时,ICE 端口可能遭受恶意请求或扫描。异常会话长期不退出,
导致 UDP 会话、TURN relay 端口及关联资源无法及时释放,最终可能耗尽
可用端口。

- 增加 UDP ICE 会话空闲超时配置,自动关闭长期无数据的会话
- 完善 TURN Allocation 的创建、刷新、超时和主动释放流程
- 会话异常退出或销毁时清理 relay 端口、Permission 和 ChannelBind
- 为 relay 会话注册表增加并发保护,避免残留或竞态访问
- 使用弱引用解除会话对象之间的循环引用
- 修正 Allocation Mismatch 错误码为 437
- 补充 iceSessionTimeoutSec 配置及使用说明

Plan-Item: ICEBUGFIX

---------

Co-authored-by: xiongziliang <771730766@qq.com>
2026-09-20 21:49:18 +08:00
jimying
5a3af1b9fc Fix whep/whip 注销失败问题 (#4808)
- delete_webrtc时,不应该使用whep/whip开始协商时的HttpRequester,可能已经keepalive过期失效
- 修复_is_negotiate_finished=false赋值一行不生效的bug(重复发delete_webrtc)

---------

Co-authored-by: xia-chu <771730766@qq.com>
2026-09-20 14:21:03 +08:00
YuLi
12ba56f534 ci: 引入 ctest 门禁并接入全平台流水线 (#4833)
tests 目录下的程序此前仅被编译、从不执行,项目中也没有任何 add_test 注册,
测试因此会静默腐烂:master 上的 test_vp9_rtp 目前即为失败状态,却无人察觉。

改为以白名单注册可无人值守执行的用例,并在流水线构建之后执行 ctest,
失败即中断。该目录其余程序多为服务型或压测工具,运行后不会自行退出,全量
自动收录会使 ctest 挂起,故采用白名单。

首批收录
test_amf、test_sortor、test_ts_splitter、test_mp4_muxer_video_dimension
四个,均已本地实测通过,总耗时 0.05 秒;并为每个用例设置 120 秒超时,避免
个别用例卡住流水线。

test_vp9_rtp 当前为失败状态,本次不收录,留待单独修复后再并入白名单。

现在先搭好框架, 后续逐个添加单元测试/门禁
2026-09-20 10:20:21 +08:00
YuLi
b7947723b9 Fix/ci fork build failures (#4832)
解决两个问题:
1. fork的每次ci不通过很烦人
2. debian 11已经过了维护期了, 所以构建不成功. 当前是使用归档仓库, 但是更好的方式其实是切换到12
2026-09-20 10:19:53 +08:00
xiongziliang
4b07053aa5 fix(hls): 修复getMediaPlayerList获取hls播放器列表失败的bug 2026-09-09 22:32:37 +08:00
ceoicac
da9b2017fb fix(rtsp): 保留 Content-Base 末尾斜杠,适配严格匹配的 RTSP 服务器 (#4820)
---

## 标题

```
fix(rtsp): 保留 Content-Base 末尾斜杠,适配严格匹配的 RTSP 服务器
```

## 描述

```markdown
## 问题描述

使用 `addStreamProxy` 拉流地址形如 `rtsp://<ip>/live` 时(注意 URL 末尾无斜杠),
部分 RTSP 服务器(例如 Alltech streaming server)会在 DESCRIBE 响应中返回
**带末尾斜杠**的 `Content-Base`:

```
Content-Base: rtsp://192.168.16.108:8554/live/
```

这类服务器对后续 PLAY 请求的 URL 做**精确字符串匹配**。

ZLMediaKit 原实现会去掉 `_content_base` 末尾的 `/`,随后发送无斜杠的 PLAY 请求:

```
PLAY rtsp://192.168.16.108:8554/live RTSP/1.0
```

服务器匹配失败,返回 `404 Stream Not Found`。

## 现象佐证

- 用 VLC 直接播放同一个 `rtsp://<ip>/live` 源**可以正常播放**,说明源本身正常;
- 用 VLC 播放经 ZLM 转出的流同样失败,说明问题出在 ZLM 拉流环节的 URL 处理;
- 日志中明确出现 `404 Stream Not Found`。

## 根因

`RtspPlayer::handleResDESCRIBE` 中以下代码去掉了 `_content_base` 的末尾斜杠:

```cpp
if (_content_base.back() == '/') {
    _content_base.pop_back();
}
```

导致 PLAY 请求使用与 `Content-Base` 不一致、去掉斜杠后的 URL。

## 修复内容

1. `src/Rtsp/RtspPlayer.cpp`
   - 移除去掉 `_content_base` 末尾斜杠的 `pop_back` 逻辑,保留 `Content-Base` 原始内容(含末尾斜杠),
     使 PLAY 请求发送与服务器 `Content-Base` 完全一致的 URL。

2. `src/Rtsp/Rtsp.cpp`
   - `SdpTrack::getControlUrl` 在拼接前先去除 `base_url` 末尾斜杠,
     避免 `_content_base` 带斜杠时 SETUP 的 track URL 被错误拼接为 `rtsp://<ip>/live//<track>` 这样的双斜杠。

## 影响范围

- 对返回不带末尾斜杠 `Content-Base` 的标准服务器:行为完全不变(原 `pop_back` 仅在末尾有 `/` 时生效)。
- 对返回带末尾斜杠 `Content-Base` 的服务器:由"去斜杠"改为"保留原始斜杠",修复 404 问题。

## 测试

- 环境:拉流 `rtsp://192.168.16.108:8554/live`(Content-Base 带末尾斜杠)。
- 修复前:PLAY 请求报 `404 Stream Not Found`,无法拉流。
- 修复后:拉流成功,RTSP 可正常播放。

---

## 提交说明(可选,供参考)

- [x] 已本地编译通过
- [x] 已在目标服务器验证可正常拉流
```

---
2026-09-08 15:05:50 +08:00
Xiaofeng Wang
5793bb56fe fix: discard invalid RTP packets (#4823)
## 问题

部分海康 RTSP 流的首个 RTP 包使用 version 1。当前 `inputRtp` 对 version 非法的 RTP 包抛出异常。

## 修改

- 对 version 非法的 RTP 包记录警告日志并返回 `nullptr` 丢弃。
- RTP 头长度不足和 payload 长度非法时仍抛出 `BadRtpException`,保留 RTP-over-TCP 的重同步机制。
- 保持后续合法 RTP 包的处理逻辑不变。
2026-09-08 15:05:02 +08:00
xiongziliang
296dcebe8f 优化配置项读取 2026-09-03 21:20:19 +08:00
xiongziliang
9f90548a67 修复编译问题 2026-08-27 23:18:00 +08:00
xiongziliang
2fa5398073 修复webrtc播放时插入sps pps rtp相关字段不正确的bug 2026-08-27 23:10:50 +08:00
xiongziliang
2049df6d1a webrtc whip拉流支持直接代理模式 2026-08-27 23:10:43 +08:00
xiongziliang
d554ba1c0c 修复webrtc sdp中不传递aac fmtp的bug 2026-08-27 23:10:37 +08:00
xiongziliang
43be587b74 修复webrtc offer sdp aac payload type冲突的bug 2026-08-27 23:10:02 +08:00
xiongziliang
2bc239fca7 更新ZLToolKit 2026-08-27 23:09:39 +08:00
xiongziliang
70bd146aa6 fix(rtsp): 修复RTSP传输模式字符串格式问题(#4797) 2026-08-26 17:59:44 +08:00
xiongziliang
034eae91ad 更新ZLToolKit 2026-08-26 17:53:58 +08:00
xiongziliang
39eac339d5 修复ice建连成功前关闭WebRtcTransport对象失败的bug 2026-08-26 17:52:35 +08:00
xiongziliang
fdaec2604d 修复访问空指针的bug 2026-08-21 13:22:38 +08:00
xiongziliang
ee42f6b2c2 修复rtsp点播seek与直播时间戳相关的bug (#4802) 2026-08-20 21:43:56 +08:00
mtdxc
d072f0f4d4 修复vp9无法秒开bug (#4815) 2026-08-20 16:47:01 +08:00
xiongziliang
9fbe034a8a 优化webrtc批量发送性能 2026-08-16 14:31:59 +08:00
Caner Ateş
992e34ab90 Fix publish failure from non-standard RTMP publishers (#4800)
## 问题 / Problem

部分摄像机(实测:宇视/Uniview)推流时,在 `@setDataFrame` 之前插入了 **2 个多余的 AMF0 number(共
18 字节)**。抓包结果:

Some publishers (verified with a Uniview camera) insert **two redundant
AMF0 numbers (18 bytes)** before `@setDataFrame`. Captured bytes:

```
44 00 00 28 00 00 a9 12   <- chunk header, type 0x12 (AMF0_DATA), body_size 169
00 0000000000000000        <- AMF0 number 0.0   (多余 / redundant)
00 0000000000000000        <- AMF0 number 0.0   (多余 / redundant)
02 00 0d "@setDataFrame"   <- 按标准应从这里开始 / message should start here
02 00 0a "onMetaData" ...
```

`RtmpSession::onRtmpChunk()` 在 `MSG_DATA` 分支中无条件把首个 AMF 元素当作字符串读取,遇到
`0x00`(number)标记时 `amf.cpp` 抛出 `Expected a string`,异常未被捕获,推流连接被断开。

`RtmpSession::onRtmpChunk()` reads the first AMF element as a string
unconditionally in the `MSG_DATA` branch. With a `0x00` (number) marker,
`amf.cpp` throws `Expected a string`; the exception is not caught and
the publisher is dropped.

nginx-rtmp 可以正常接收同一路流,ZLMediaKit 无法接收。

nginx-rtmp accepts the very same stream; ZLMediaKit does not.

### 修复前日志 / Log before the fix

```
RtmpSession.cpp:130 | publish 回复时间:2ms
TcpServer.cpp:210   | on err: 6(Expected a string)
RtmpSession.cpp:28  | RTMP推流器(__defaultVhost__/live/stream)断开:Expected a string,耗时(s):0
```

## 修改内容 / Changes

新增 `amfLoadLeadingString()`,跳过前置的非字符串 AMF 值后再取首个字符串。该写法参考了 `FlvSplitter`
中已有的防御式解析(先 `load<AMFValue>()`
再判断类型),并把它统一成一个公共函数。`AMFDecoder::remain()` 用于限定循环边界,避免缓冲区读完后再次抛异常。

Add `amfLoadLeadingString()`, which skips leading non-string AMF values
before returning the first string. This generalizes the defensive
parsing already present in `FlvSplitter` (`load<AMFValue>()` followed by
a type check) into one shared helper. `AMFDecoder::remain()` bounds the
loop so it does not throw once the buffer is exhausted.

- `amf.h` / `amf.cpp` — 新增 `remain()` 与 `amfLoadLeadingString()` / add
`remain()` and `amfLoadLeadingString()`
- `RtmpSession.cpp` — 推流侧,本 issue 的根因 / publish side, the root cause
here
- `RtmpPlayer.cpp` — 拉流侧存在同样的崩溃风险 / pull side has the identical failure
mode
- `FlvSplitter.cpp` — 原本不会断开,但会静默丢弃 metadata;现在能正确解析 / did not
disconnect before, but silently dropped the metadata; now parses it

## 验证 / Verification

用真实摄像机推流抓取了 17.8 MB 原始 RTMP 字节流,分别回放到修改前后的 MediaServer:

A 17.8 MB raw RTMP capture from the live camera was replayed against
MediaServer before and after the change:

| 场景 / Scenario | 修改前 / Before | 修改后 / After |
|---|---|---|
| 原始流(含多余 AMF 值)/ raw stream with redundant AMF values | `Expected a
string`,推流被断开 / dropped | 全部回放完成,无断开 / replayed fully, no disconnect |
| 去掉那 18 字节的标准流 / same stream with the 18 bytes removed | 正常 / OK | 正常 /
OK(回归测试 / regression) |

对照实验:**仅**从原始抓包中删除那 18 字节,其余字节完全不变,修改前的版本即可正常收流 —— 说明问题确实由这段多余的 AMF 值引起。

Control experiment: removing **only** those 18 bytes from the capture,
leaving every other byte untouched, makes the unpatched build work —
confirming those bytes are the cause.

修复后的日志 / Log after the fix:

```
MultiMediaSourceMuxer.cpp | stream: rtmp://__defaultVhost__/live/stream , codec info: H264[1920/1080/25]
MediaSource.cpp | 媒体注册:rtmp://__defaultVhost__/live/stream
MediaSource.cpp | 媒体注册:rtsp/hls/ts/fmp4://__defaultVhost__/live/stream
```

摄像机实时推流也已验证:拉取 HTTP-FLV 后用 ffmpeg 解码无报错(H264 1920x1080 25fps)。

Also verified end to end with the live camera: pulling HTTP-FLV and
decoding it with ffmpeg produces no errors (H264 1920x1080 25fps).

## 行为变化说明 / Note on behavior change

`RtmpPlayer` 中被修改的分支同时处理
`MSG_CMD`。命令消息的首个元素本来就是字符串,正常情况下行为不变;但如果收到首元素非字符串的畸形命令消息,以前会断开连接,现在会打印
`can not support cmd:` 警告并继续。符合标准的流行为完全不变(已通过上面的回归测试验证)。

The modified branch in `RtmpPlayer` also handles `MSG_CMD`. The first
element of a command message is already a string, so normal behavior is
unchanged; for a malformed command message whose first element is not a
string, the connection used to be dropped and now a `can not support
cmd:` warning is logged instead. Standard-compliant streams are
unaffected, as shown by the regression test above.
2026-08-14 13:29:23 +08:00
xiongziliang
0e9e59bf43 fix(webrtc): 修复webrtc连接迁移后close失败的bug 2026-08-06 21:42:31 +08:00
YuLi
49b99f106f 优化 PlayerProxy 重连时的 muxer 保留策略 (#4794)
当 `general.resetWhenRePlay=0` 时,上游临时断开不再立即重置
muxer。重连成功后会比较新旧轨道的编码、初始化配置、视频尺寸及音频参数:

  - 轨道兼容时只替换输入 delegate,保留原 MediaSource、HLS 状态和下游播放连接。
  - 轨道不兼容时正常重置并重新初始化封装状态。
  - 重试耗尽或主动关闭时释放 muxer,并取消等待中的重连任务。

这样做的目的是上游自动重连不会导致客户端播放器断开, 对客户端播放器来说上游重连是无感的
2026-08-02 13:13:19 +08:00
YuLi
7768aee571 修复HTTP-TS 媒体帧超时 (#4793)
这个修复是因为有时的可能由于上游网络flow损坏,会导致http-ts拉流时连接不会断开, 只有几kb的速度.
这样实际上流媒体是无法正常播放的, 但是又因为能读取到非常少的数据, 不会触发超时. 等于链接假死在那里了.常见与跨域/跨洲传输时.
所以这个pr就是用于判断如果媒体帧长时间接收不全, 就也应该认为是超时断开.

后面还会提交一个新的pr, 主要是配合这个pr实现"PlayerProxy 无感重连与 muxer 保留策略", 这样就能在遇到坏flow时,
自动重连, 但是播放器无感知, 也不会断开.
2026-08-02 13:09:12 +08:00