添加RTMP Payload XOR保护功能,更新相关UI和逻辑,支持可选的流加密

This commit is contained in:
2026-02-25 14:59:56 +08:00
parent d0b1678833
commit 6a3421fa4d
9 changed files with 157 additions and 16 deletions

View File

@@ -18,6 +18,7 @@ Selly Live SDK 提供完整的音视频直播能力,支持 **推流(直播
- 拉流播放状态与错误回调
- 支持视频帧处理(美颜 / 滤镜 / 水印)
- 基于 **Token 的安全鉴权机制**
- 支持 **RTMP H264 + AAC payload XOR 保护(可选)**
---
@@ -103,6 +104,31 @@ dependencies {
2. 调用 `pusher.token = newToken` / `player.token = newToken`
3. 停止并重新开始推流 / 拉流流程
### 4.4 RTMP Payload XOR 保护(可选)
用途:
- 防止他人拿到 RTMP 地址后直接播放、转码或截图
生效范围与约束:
- 仅对 **RTMP** 生效
- 仅支持 **H264 + AAC**(当前版本)
- 只处理 payload配置帧SPS/PPS、AAC Sequence Header保持不变
- 推流端与播放端必须使用**同一个 key**
Key 格式:
- `hex` 字符串,建议 16 或 32 字节(即 32/64 个 hex 字符)
- 支持 `0x` 前缀
- 长度必须为偶数
- 非法 key 会被忽略并关闭 XOR会输出 warning 日志)
时机要求:
- 推流:请在 `startLiveWithStreamId(...)` / `startLiveWithUrl(...)` 之前设置 key
- 拉流:请在 `initWithStreamId(...)` / `initWithUrl(...)` 创建播放器时传入 `xorKeyHex`
---
## 5. 推流接入详解
@@ -146,6 +172,17 @@ pusher.startRunning(
pusher.token = pushToken
```
#### RTMP Payload XOR可选
```kotlin
val xorKeyHex = "A1B2C3D4E5F6A7B8C9D0E1F2A3B4C5D6"
// 建议在 startLiveWith... 之前设置
pusher.setXorKey(xorKeyHex)
```
> 若在推流中修改 key需停止并重新开始推流后才会使用新 key。
### 5.4 开始/停止推流
```kotlin
@@ -228,6 +265,7 @@ pusher.stopLive { error ->
- `startRunning(cameraPosition, videoConfig, audioConfig)`:开始采集预览
- `setVideoConfiguration(config)`:更新视频参数
- `setXorKey(hexKey)`:设置 RTMP payload XOR key可选
- `startLiveWithStreamId(streamId)`:使用 streamId 推流
- `startLiveWithUrl(url)`:使用完整 URL 推流
- `stopLive()` / `stopLive(callback)`:停止推流
@@ -263,10 +301,11 @@ pusher.stopLive { error ->
val player = SellyLiveVideoPlayer.initWithStreamId(
context = this,
streamId = streamId,
liveMode = SellyLiveMode.RTC
liveMode = SellyLiveMode.RTC,
xorKeyHex = "" // RTC 场景可留空
)
// 或直接使用完整 URL
// val player = SellyLiveVideoPlayer.initWithUrl(this, playUrl)
// val player = SellyLiveVideoPlayer.initWithUrl(this, playUrl, xorKeyHex = "A1B2...")
```
若需要指定 `vhost` / `appName`
@@ -277,10 +316,13 @@ val player = SellyLiveVideoPlayer.initWithStreamId(
streamId = streamId,
liveMode = SellyLiveMode.RTMP,
vhost = "your-vhost",
appName = "live"
appName = "live",
xorKeyHex = "A1B2C3D4E5F6A7B8C9D0E1F2A3B4C5D6"
)
```
> 使用 RTMP 加密流时,请在创建播放器时传入 `xorKeyHex`;后续如需换 key请重建播放器实例。
### 6.2 设置拉流 Token使用 streamId 时)
```kotlin
@@ -336,8 +378,8 @@ player.delegate = object : SellyLiveVideoPlayerDelegate {
创建与渲染:
- `initWithStreamId(context, streamId, liveMode, vhost, appName)`:使用 streamId 创建播放器
- `initWithUrl(context, url)`:使用完整 URL 创建播放器
- `initWithStreamId(context, streamId, liveMode, vhost, appName, xorKeyHex)`:使用 streamId 创建播放器
- `initWithUrl(context, url, xorKeyHex)`:使用完整 URL 创建播放器
- `attachRenderView(container)` / `setRenderView(view)`:设置渲染 View
- `getRenderView()`:获取当前渲染 View
@@ -388,7 +430,7 @@ player.delegate = object : SellyLiveVideoPlayerDelegate {
SDK 不解析 URL 中的鉴权信息,所有鉴权均通过 `token` 属性完成。
### Q2运行中修改 Token 是否生效?
**A**
**A**
运行中修改 Token **不会影响当前已建立的连接**
**下次重连或重新启动推流 / 拉流时会使用新的 Token**
@@ -399,3 +441,11 @@ SDK 不解析 URL 中的鉴权信息,所有鉴权均通过 `token` 属性完
- 确认当前网络连接正常
- 查看播放器回调中的错误信息
- 确认视频流格式是否被 SDK 支持
### Q4加密流播放花屏/噪音怎么办?
**A** 重点检查以下项:
- 推流端与播放端 `xorKeyHex` 是否完全一致
- key 格式是否为合法 hex偶数长度支持 `0x` 前缀)
- 当前是否为 RTMP + H264 + AAC
- 变更 key 后是否已重启推流 / 重建播放器