2018-10-25 10:00:17 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*/
|
2018-10-21 21:21:14 +08:00
|
|
|
|
|
2019-06-28 17:37:11 +08:00
|
|
|
|
#include "H264Rtp.h"
|
2018-10-21 21:21:14 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit{
|
2018-10-21 22:24:24 +08:00
|
|
|
|
|
2021-02-07 18:29:48 +08:00
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
|
#endif // defined(_WIN32)
|
|
|
|
|
|
|
|
|
|
class FuFlags {
|
|
|
|
|
public:
|
|
|
|
|
#if __BYTE_ORDER == __BIG_ENDIAN
|
|
|
|
|
unsigned start_bit: 1;
|
|
|
|
|
unsigned end_bit: 1;
|
|
|
|
|
unsigned reserved: 1;
|
|
|
|
|
unsigned nal_type: 5;
|
|
|
|
|
#else
|
|
|
|
|
unsigned nal_type: 5;
|
|
|
|
|
unsigned reserved: 1;
|
|
|
|
|
unsigned end_bit: 1;
|
|
|
|
|
unsigned start_bit: 1;
|
|
|
|
|
#endif
|
|
|
|
|
} PACKED;
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
#endif // defined(_WIN32)
|
2018-10-25 23:24:23 +08:00
|
|
|
|
|
2018-10-21 22:24:24 +08:00
|
|
|
|
H264RtpDecoder::H264RtpDecoder() {
|
2021-02-07 18:29:48 +08:00
|
|
|
|
_frame = obtainFrame();
|
2018-10-21 22:24:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-05 11:51:16 +08:00
|
|
|
|
H264Frame::Ptr H264RtpDecoder::obtainFrame() {
|
|
|
|
|
auto frame = FrameImp::create<H264Frame>();
|
2020-01-14 10:04:24 +08:00
|
|
|
|
frame->_prefix_size = 4;
|
2018-10-21 22:24:24 +08:00
|
|
|
|
return frame;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-23 18:39:17 +08:00
|
|
|
|
bool H264RtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
|
2019-12-26 09:43:44 +08:00
|
|
|
|
return decodeRtp(rtp);
|
2018-10-21 21:21:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 18:29:48 +08:00
|
|
|
|
/*
|
|
|
|
|
RTF3984 5.2节 Common Structure of the RTP Payload Format
|
|
|
|
|
Table 1. Summary of NAL unit types and their payload structures
|
|
|
|
|
|
|
|
|
|
Type Packet Type name Section
|
|
|
|
|
---------------------------------------------------------
|
|
|
|
|
0 undefined -
|
|
|
|
|
1-23 NAL unit Single NAL unit packet per H.264 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 undefined -
|
|
|
|
|
*/
|
|
|
|
|
|
2021-01-31 19:19:24 +08:00
|
|
|
|
bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtp) {
|
|
|
|
|
auto frame = rtp->getPayload();
|
|
|
|
|
auto length = rtp->getPayloadSize();
|
|
|
|
|
auto stamp = rtp->getStampMS();
|
|
|
|
|
auto seq = rtp->getSeq();
|
2021-02-07 18:29:48 +08:00
|
|
|
|
auto nal_type = *frame & 0x1F;
|
|
|
|
|
auto nal_suffix = *frame & (~0x1F);
|
2018-10-21 21:21:14 +08:00
|
|
|
|
|
2020-07-26 19:56:39 +08:00
|
|
|
|
if (nal_type >= 0 && nal_type < 24) {
|
2018-10-21 21:21:14 +08:00
|
|
|
|
//a full frame
|
2021-02-07 18:29:48 +08:00
|
|
|
|
_frame->_buffer.assign("\x00\x00\x00\x01", 4);
|
|
|
|
|
_frame->_buffer.append((char *) frame, length);
|
|
|
|
|
_frame->_pts = stamp;
|
|
|
|
|
auto key = _frame->keyFrame();
|
|
|
|
|
onGetH264(_frame);
|
2019-08-01 14:39:09 +08:00
|
|
|
|
return (key); //i frame
|
2018-10-21 21:21:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 18:29:48 +08:00
|
|
|
|
switch (nal_type) {
|
|
|
|
|
case 24: {
|
2019-01-17 18:26:28 +08:00
|
|
|
|
// 24 STAP-A 单一时间的组合包
|
|
|
|
|
bool haveIDR = false;
|
|
|
|
|
auto ptr = frame + 1;
|
2020-07-26 19:56:39 +08:00
|
|
|
|
while (true) {
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t off = ptr - frame;
|
2019-01-17 18:26:28 +08:00
|
|
|
|
if (off >= length) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
//获取当前nalu的大小
|
|
|
|
|
uint16_t len = *ptr++;
|
|
|
|
|
len <<= 8;
|
|
|
|
|
len |= *ptr++;
|
|
|
|
|
if (off + len > length) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-08-01 10:17:09 +08:00
|
|
|
|
if (len > 0) {
|
|
|
|
|
//有有效数据
|
2021-02-07 18:29:48 +08:00
|
|
|
|
_frame->_buffer.assign("\x00\x00\x00\x01", 4);
|
|
|
|
|
_frame->_buffer.append((char *) ptr, len);
|
|
|
|
|
_frame->_pts = stamp;
|
2020-07-26 19:56:39 +08:00
|
|
|
|
if ((ptr[0] & 0x1F) == H264Frame::NAL_IDR) {
|
2019-01-17 18:26:28 +08:00
|
|
|
|
haveIDR = true;
|
|
|
|
|
}
|
2021-02-07 18:29:48 +08:00
|
|
|
|
onGetH264(_frame);
|
2019-01-17 18:26:28 +08:00
|
|
|
|
}
|
|
|
|
|
ptr += len;
|
|
|
|
|
}
|
|
|
|
|
return haveIDR;
|
2018-10-21 21:21:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 18:29:48 +08:00
|
|
|
|
case 28: {
|
2019-01-17 18:26:28 +08:00
|
|
|
|
//FU-A
|
2021-02-07 18:29:48 +08:00
|
|
|
|
FuFlags *fu = (FuFlags *) (frame + 1);
|
|
|
|
|
if (fu->start_bit) {
|
2019-08-30 11:17:27 +08:00
|
|
|
|
//该帧的第一个rtp包 FU-A start
|
2021-02-07 18:29:48 +08:00
|
|
|
|
//预留空间,防止频繁扩容拷贝
|
|
|
|
|
_frame->_buffer.reserve(_max_frame_size);
|
|
|
|
|
_frame->_buffer.assign("\x00\x00\x00\x01", 4);
|
|
|
|
|
_frame->_buffer.push_back(nal_suffix | fu->nal_type);
|
|
|
|
|
_frame->_buffer.append((char *) frame + 2, length - 2);
|
|
|
|
|
_frame->_pts = stamp;
|
2019-08-01 14:39:09 +08:00
|
|
|
|
//该函数return时,保存下当前sequence,以便下次对比seq是否连续
|
2021-02-07 18:29:48 +08:00
|
|
|
|
_last_seq = seq;
|
|
|
|
|
return _frame->keyFrame();
|
2019-01-17 18:26:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 18:29:48 +08:00
|
|
|
|
if (seq != (uint16_t) (_last_seq + 1)) {
|
2019-08-01 14:39:09 +08:00
|
|
|
|
//中间的或末尾的rtp包,其seq必须连续(如果回环了则判定为连续),否则说明rtp丢包,那么该帧不完整,必须得丢弃
|
2021-02-07 18:29:48 +08:00
|
|
|
|
_frame->_buffer.clear();
|
|
|
|
|
WarnL << "rtp丢包: " << seq << " != " << _last_seq << " + 1,该帧被废弃";
|
2019-01-17 18:26:28 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-08-01 14:39:09 +08:00
|
|
|
|
|
2021-02-07 18:29:48 +08:00
|
|
|
|
if (!fu->end_bit) {
|
2019-08-30 11:17:27 +08:00
|
|
|
|
//该帧的中间rtp包 FU-A mid
|
2021-02-07 18:29:48 +08:00
|
|
|
|
_frame->_buffer.append((char *) frame + 2, length - 2);
|
2019-08-01 14:39:09 +08:00
|
|
|
|
//该函数return时,保存下当前sequence,以便下次对比seq是否连续
|
2021-02-07 18:29:48 +08:00
|
|
|
|
_last_seq = seq;
|
2019-08-01 14:39:09 +08:00
|
|
|
|
return false;
|
2019-01-17 18:26:28 +08:00
|
|
|
|
}
|
2019-08-01 14:39:09 +08:00
|
|
|
|
|
2019-08-30 11:17:27 +08:00
|
|
|
|
//该帧最后一个rtp包 FU-A end
|
2021-02-07 18:29:48 +08:00
|
|
|
|
_frame->_buffer.append((char *) frame + 2, length - 2);
|
|
|
|
|
_frame->_pts = stamp;
|
|
|
|
|
//计算最大的帧
|
|
|
|
|
auto frame_size = _frame->size();
|
|
|
|
|
if (frame_size > _max_frame_size) {
|
|
|
|
|
_max_frame_size = frame_size;
|
|
|
|
|
}
|
|
|
|
|
onGetH264(_frame);
|
2020-01-14 19:41:55 +08:00
|
|
|
|
return false;
|
2018-10-21 21:21:14 +08:00
|
|
|
|
}
|
2019-01-17 18:26:28 +08:00
|
|
|
|
|
2020-07-26 19:56:39 +08:00
|
|
|
|
default: {
|
2019-01-17 18:26:28 +08:00
|
|
|
|
// 29 FU-B 单NAL单元B模式
|
|
|
|
|
// 25 STAP-B 单一时间的组合包
|
|
|
|
|
// 26 MTAP16 多个时间的组合包
|
|
|
|
|
// 27 MTAP24 多个时间的组合包
|
2021-01-31 19:19:24 +08:00
|
|
|
|
WarnL << "不支持的rtp类型:" << (int) nal_type << " " << seq;
|
2019-01-17 18:26:28 +08:00
|
|
|
|
return false;
|
2018-10-21 21:21:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-21 21:45:44 +08:00
|
|
|
|
void H264RtpDecoder::onGetH264(const H264Frame::Ptr &frame) {
|
2020-04-29 18:05:29 +08:00
|
|
|
|
//rtsp没有dts,那么根据pts排序算法生成dts
|
|
|
|
|
_dts_generator.getDts(frame->_pts,frame->_dts);
|
|
|
|
|
RtpCodec::inputFrame(frame);
|
2021-02-07 18:29:48 +08:00
|
|
|
|
_frame = obtainFrame();
|
2018-10-21 21:21:14 +08:00
|
|
|
|
}
|
2018-10-21 21:45:44 +08:00
|
|
|
|
|
2018-10-21 22:24:24 +08:00
|
|
|
|
|
2018-10-21 21:45:44 +08:00
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2021-02-07 18:29:48 +08:00
|
|
|
|
H264RtpEncoder::H264RtpEncoder(uint32_t ssrc, uint32_t mtu, uint32_t sample_rate, uint8_t pt, uint8_t interleaved)
|
|
|
|
|
: RtpInfo(ssrc, mtu, sample_rate, pt, interleaved) {
|
2018-10-21 21:45:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-23 21:41:45 +08:00
|
|
|
|
void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
2020-07-26 19:56:39 +08:00
|
|
|
|
auto ptr = frame->data() + frame->prefixSize();
|
|
|
|
|
auto len = frame->size() - frame->prefixSize();
|
2021-01-31 19:19:24 +08:00
|
|
|
|
auto pts = frame->pts();
|
2020-07-26 19:56:39 +08:00
|
|
|
|
auto nal_type = H264_TYPE(ptr[0]);
|
2021-02-07 18:29:48 +08:00
|
|
|
|
auto packet_size = getMaxSize() - 2;
|
|
|
|
|
|
|
|
|
|
//末尾5bit为nalu type,固定为28(FU-A)
|
|
|
|
|
auto fu_char_0 = (ptr[0] & (~0x1F)) | 28;
|
|
|
|
|
auto fu_char_1 = nal_type;
|
|
|
|
|
FuFlags *fu_flags = (FuFlags *) (&fu_char_1);
|
|
|
|
|
fu_flags->start_bit = 1;
|
2018-10-21 21:45:44 +08:00
|
|
|
|
|
2019-09-06 23:22:04 +08:00
|
|
|
|
//超过MTU则按照FU-A模式打包
|
2021-02-07 18:29:48 +08:00
|
|
|
|
if (len > packet_size + 1) {
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t offset = 1;
|
2021-02-07 18:29:48 +08:00
|
|
|
|
while (!fu_flags->end_bit) {
|
|
|
|
|
if (!fu_flags->start_bit && len <= offset + packet_size) {
|
2019-09-06 23:22:04 +08:00
|
|
|
|
//FU-A end
|
2021-02-07 18:29:48 +08:00
|
|
|
|
packet_size = len - offset;
|
|
|
|
|
fu_flags->end_bit = 1;
|
2018-10-21 21:45:44 +08:00
|
|
|
|
}
|
2019-09-06 15:16:22 +08:00
|
|
|
|
|
2021-02-07 18:29:48 +08:00
|
|
|
|
//传入nullptr先不做payload的内存拷贝
|
|
|
|
|
auto rtp = makeRtp(getTrackType(), nullptr, packet_size + 2, fu_flags->end_bit, pts);
|
|
|
|
|
//rtp payload 负载部分
|
|
|
|
|
uint8_t *payload = rtp->getPayload();
|
|
|
|
|
//FU-A 第1个字节
|
|
|
|
|
payload[0] = fu_char_0;
|
|
|
|
|
//FU-A 第2个字节
|
|
|
|
|
payload[1] = fu_char_1;
|
|
|
|
|
//H264 数据
|
|
|
|
|
memcpy(payload + 2, (uint8_t *) ptr + offset, packet_size);
|
|
|
|
|
//输入到rtp环形缓存
|
|
|
|
|
RtpCodec::inputRtp(rtp, fu_flags->start_bit && nal_type == H264Frame::NAL_IDR);
|
|
|
|
|
|
|
|
|
|
offset += packet_size;
|
|
|
|
|
fu_flags->start_bit = 0;
|
2018-10-21 21:45:44 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-07-26 19:56:39 +08:00
|
|
|
|
//如果帧长度不超过mtu, 则按照Single NAL unit packet per H.264 方式打包
|
|
|
|
|
makeH264Rtp(ptr, len, false, false, pts);
|
2018-10-21 21:45:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void H264RtpEncoder::makeH264Rtp(const void* data, size_t len, bool mark, bool gop_pos, uint32_t uiStamp) {
|
2020-07-26 19:56:39 +08:00
|
|
|
|
RtpCodec::inputRtp(makeRtp(getTrackType(), data, len, mark, uiStamp), gop_pos);
|
2018-10-24 17:17:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-19 15:21:18 +08:00
|
|
|
|
}//namespace mediakit
|