====== RTSP ====== ## 文档 - RTSP [RFC2326](http://www.rfcreader.com/#rfc2326) - SDP [RFC2327](http://www.rfcreader.com/#rfc2327)[RFC3266](http://www.rfcreader.com/#rfc3266) -> [RFC4566](http://www.rfcreader.com/#rfc4566) -> [RFC8866](http://www.rfcreader.com/#rfc8866) - SDP 中音视频媒体的描述:[RFC5939](https://www.rfcreader.com/#rfc5939), [RFC6236](https://www.rfcreader.com/#rfc6236), [RFC6871](https://www.rfcreader.com/#rfc6871) - RTP [格式](https://cloud.tencent.com/developer/article/1736777), [RFC1889](http://www.rfcreader.com/#rfc1889) , [RFC3550](https://www.rfcreader.com/#rfc3550) , [RFC3551](https://www.rfcreader.com/#rfc3551) - RTP 对各种格式的分包:[RTP_payload_formats](https://en.wikipedia.org/wiki/RTP_payload_formats) - RTP 对 H264 的分包相关: [RFC6184](https://www.rfcreader.com/#rfc6184) - [协议指南](https://cloud.tencent.com/developer/article/1711409) ## Tips - Transport: 格式 `//`,未指明TCP或UDP的 **RTP/AVP**, 默认是 **UDP**. - 音视频媒体的编码类型以 `m=` 为准, `a=fmtp`, `a=rtpmap` 可参考; ## 关于音频对讲 - [ONVIF 双向对讲](https://blog.csdn.net/yuanchunsi/article/details/72877603) - `DESCRIBE` 时添加头 `Require: www.onvif.org/ver20/backchannel\r\n`, 支持的摄像头会返回带有支持音频格式的 `m`, `rtpmap`指示可支持对讲时传输什么类型的音频。一般 g711a 即 PCMA/8000 的类型值为 `8`, g711u(PCMU/8000) 的类型值为 `0` ## 关于 h264 的分包 - RTP 对 H264 的分包相关: [RFC6184](https://www.rfcreader.com/#rfc6184) - Table 1. Summary of NAL unit types and the corresponding packet types ``` NAL Unit Packet Packet Type Name Section Type Type ------------------------------------------------------------- 0 reserved - 1-23 NAL unit Single NAL unit packet 5.6 24 STAP-A Single-time aggregation packet 5.7.1 25 STAP-B Single-time aggregation packet 5.7.1 26 MTAP16 Multi-time aggregation packet 5.7.2 27 MTAP24 Multi-time aggregation packet 5.7.2 28 FU-A Fragmentation unit 5.8 29 FU-B Fragmentation unit 5.8 30-31 reserved - ``` - 三种分包模式: three cases of packetization modes: - Single NAL unit mode - Non-interleaved mode - Interleaved mode - 三种分包模式允许的包类型: ``` Table 3. Summary of allowed NAL unit types for each packetization mode (yes = allowed, no = disallowed, ig = ignore) Payload Packet Single NAL Non-Interleaved Interleaved Type Type Unit Mode Mode Mode ------------------------------------------------------------- 0 reserved ig ig ig 1-23 NAL unit yes yes no 24 STAP-A no yes no 25 STAP-B no no yes 26 MTAP16 no no yes 27 MTAP24 no no yes 28 FU-A no yes yes 29 FU-B no no yes 30-31 reserved ig ig ig ``` ### STAP-A 包 - 24(0x18)格式,某些 rtsp server 推流会把关键帧前的 sps pps 作为一个STAP-A包发送, 比如 [ mediamtx 的流](:/375f37943e3d4a659d329a260eb1efca) - STAP-A 的格式: 头字节 0x18 , 跟两字节长度,然后跟h264 nalu内容,再跟两字节长度,如此循环: ``` 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | RTP Header | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |STAP-A NAL HDR | NALU 1 Size | NALU 1 HDR | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | NALU 1 Data | : : + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | NALU 2 Size | NALU 2 HDR | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | NALU 2 Data | : : | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | :...OPTIONAL RTP padding | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ```