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
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
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
|
|
|
|
|
2018-10-25 23:24:23 +08:00
|
|
|
|
typedef struct {
|
|
|
|
|
unsigned S :1;
|
|
|
|
|
unsigned E :1;
|
|
|
|
|
unsigned R :1;
|
|
|
|
|
unsigned type :5;
|
|
|
|
|
} FU;
|
|
|
|
|
|
2018-10-30 17:11:36 +08:00
|
|
|
|
static bool MakeFU(uint8_t in, FU &fu) {
|
2018-10-25 23:24:23 +08:00
|
|
|
|
fu.S = in >> 7;
|
|
|
|
|
fu.E = (in >> 6) & 0x01;
|
|
|
|
|
fu.R = (in >> 5) & 0x01;
|
|
|
|
|
fu.type = in & 0x1f;
|
|
|
|
|
if (fu.R != 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-21 22:24:24 +08:00
|
|
|
|
H264RtpDecoder::H264RtpDecoder() {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_h264frame = obtainFrame();
|
2018-10-21 22:24:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
H264Frame::Ptr H264RtpDecoder::obtainFrame() {
|
|
|
|
|
//从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
|
2018-10-24 12:01:40 +08:00
|
|
|
|
auto frame = ResourcePoolHelper<H264Frame>::obtainObj();
|
2020-01-14 10:04:24 +08:00
|
|
|
|
frame->_buffer.clear();
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2018-10-21 22:24:24 +08:00
|
|
|
|
bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtppack) {
|
2018-10-21 21:21:14 +08:00
|
|
|
|
/**
|
|
|
|
|
* h264帧类型
|
|
|
|
|
* Type==1:P/B frame
|
|
|
|
|
* Type==5:IDR frame
|
|
|
|
|
* Type==6:SEI frame
|
|
|
|
|
* Type==7:SPS frame
|
|
|
|
|
* Type==8:PPS frame
|
|
|
|
|
*/
|
2020-03-20 11:51:24 +08:00
|
|
|
|
/*
|
|
|
|
|
RTF3984 5.2节 Common Structure of the RTP Payload Format
|
2019-08-30 11:17:27 +08:00
|
|
|
|
Table 1. Summary of NAL unit types and their payload structures
|
|
|
|
|
|
2020-03-20 11:51:24 +08:00
|
|
|
|
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 -
|
|
|
|
|
*/
|
2019-06-24 16:07:44 +08:00
|
|
|
|
const uint8_t *frame = (uint8_t *) rtppack->data() + rtppack->offset;
|
|
|
|
|
int length = rtppack->size() - rtppack->offset;
|
2020-07-26 19:56:39 +08:00
|
|
|
|
int nal_type = *frame & 0x1F;
|
|
|
|
|
int 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
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_h264frame->_buffer.assign("\x0\x0\x0\x1", 4);
|
2020-07-26 19:56:39 +08:00
|
|
|
|
_h264frame->_buffer.append((char *) frame, length);
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_h264frame->_pts = rtppack->timeStamp;
|
2019-08-01 14:39:09 +08:00
|
|
|
|
auto key = _h264frame->keyFrame();
|
2018-10-24 15:43:52 +08:00
|
|
|
|
onGetH264(_h264frame);
|
2019-08-01 14:39:09 +08:00
|
|
|
|
return (key); //i frame
|
2018-10-21 21:21:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-26 19:56:39 +08:00
|
|
|
|
switch (nal_type){
|
2019-01-17 18:26:28 +08:00
|
|
|
|
case 24:{
|
|
|
|
|
// 24 STAP-A 单一时间的组合包
|
|
|
|
|
bool haveIDR = false;
|
|
|
|
|
auto ptr = frame + 1;
|
2020-07-26 19:56:39 +08:00
|
|
|
|
while (true) {
|
2019-01-17 18:26:28 +08:00
|
|
|
|
int off = ptr - frame;
|
|
|
|
|
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) {
|
|
|
|
|
//有有效数据
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_h264frame->_buffer.assign("\x0\x0\x0\x1", 4);
|
2020-07-26 19:56:39 +08:00
|
|
|
|
_h264frame->_buffer.append((char *) ptr, len);
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_h264frame->_pts = rtppack->timeStamp;
|
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;
|
|
|
|
|
}
|
|
|
|
|
onGetH264(_h264frame);
|
|
|
|
|
}
|
|
|
|
|
ptr += len;
|
|
|
|
|
}
|
|
|
|
|
return haveIDR;
|
2018-10-21 21:21:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-17 18:26:28 +08:00
|
|
|
|
case 28:{
|
|
|
|
|
//FU-A
|
|
|
|
|
FU fu;
|
|
|
|
|
MakeFU(frame[1], fu);
|
2019-08-01 14:39:09 +08:00
|
|
|
|
if (fu.S) {
|
2019-08-30 11:17:27 +08:00
|
|
|
|
//该帧的第一个rtp包 FU-A start
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_h264frame->_buffer.assign("\x0\x0\x0\x1", 4);
|
2020-07-26 19:56:39 +08:00
|
|
|
|
_h264frame->_buffer.push_back(nal_suffix | fu.type);
|
|
|
|
|
_h264frame->_buffer.append((char *) frame + 2, length - 2);
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_h264frame->_pts = rtppack->timeStamp;
|
2019-08-01 14:39:09 +08:00
|
|
|
|
//该函数return时,保存下当前sequence,以便下次对比seq是否连续
|
|
|
|
|
_lastSeq = rtppack->sequence;
|
|
|
|
|
return _h264frame->keyFrame();
|
2019-01-17 18:26:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-01 14:39:09 +08:00
|
|
|
|
if (rtppack->sequence != _lastSeq + 1 && rtppack->sequence != 0) {
|
|
|
|
|
//中间的或末尾的rtp包,其seq必须连续(如果回环了则判定为连续),否则说明rtp丢包,那么该帧不完整,必须得丢弃
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_h264frame->_buffer.clear();
|
2020-05-25 14:28:02 +08:00
|
|
|
|
WarnL << "rtp丢包: " << rtppack->sequence << " != " << _lastSeq << " + 1,该帧被废弃";
|
2019-01-17 18:26:28 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-08-01 14:39:09 +08:00
|
|
|
|
|
|
|
|
|
if (!fu.E) {
|
2019-08-30 11:17:27 +08:00
|
|
|
|
//该帧的中间rtp包 FU-A mid
|
2020-07-26 19:56:39 +08:00
|
|
|
|
_h264frame->_buffer.append((char *) frame + 2, length - 2);
|
2019-08-01 14:39:09 +08:00
|
|
|
|
//该函数return时,保存下当前sequence,以便下次对比seq是否连续
|
|
|
|
|
_lastSeq = rtppack->sequence;
|
|
|
|
|
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
|
2020-07-26 19:56:39 +08:00
|
|
|
|
_h264frame->_buffer.append((char *) frame + 2, length - 2);
|
2020-01-14 10:04:24 +08:00
|
|
|
|
_h264frame->_pts = rtppack->timeStamp;
|
2019-08-01 14:39:09 +08:00
|
|
|
|
onGetH264(_h264frame);
|
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 多个时间的组合包
|
|
|
|
|
// 0 udef
|
|
|
|
|
// 30 udef
|
|
|
|
|
// 31 udef
|
2020-07-26 19:56:39 +08:00
|
|
|
|
WarnL << "不支持的rtp类型:" << (int) nal_type << " " << rtppack->sequence;
|
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);
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_h264frame = 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
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
H264RtpEncoder::H264RtpEncoder(uint32_t ui32Ssrc,
|
|
|
|
|
uint32_t ui32MtuSize,
|
|
|
|
|
uint32_t ui32SampleRate,
|
2020-05-25 13:51:00 +08:00
|
|
|
|
uint8_t ui8PayloadType,
|
2018-10-21 21:45:44 +08:00
|
|
|
|
uint8_t ui8Interleaved) :
|
2018-10-23 11:38:56 +08:00
|
|
|
|
RtpInfo(ui32Ssrc,
|
|
|
|
|
ui32MtuSize,
|
|
|
|
|
ui32SampleRate,
|
2020-05-25 13:51:00 +08:00
|
|
|
|
ui8PayloadType,
|
2018-10-23 11:38:56 +08:00
|
|
|
|
ui8Interleaved) {
|
2018-10-21 21:45:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-23 21:41:45 +08:00
|
|
|
|
void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
2019-05-28 17:14:36 +08:00
|
|
|
|
GET_CONFIG(uint32_t,cycleMS,Rtp::kCycleMS);
|
2020-07-26 19:56:39 +08:00
|
|
|
|
auto ptr = frame->data() + frame->prefixSize();
|
|
|
|
|
auto pts = frame->pts() % cycleMS;
|
|
|
|
|
auto len = frame->size() - frame->prefixSize();
|
|
|
|
|
auto nal_type = H264_TYPE(ptr[0]);
|
|
|
|
|
auto max_rtp_size = _ui32MtuSize - 2;
|
2018-10-21 21:45:44 +08:00
|
|
|
|
|
2019-09-06 23:22:04 +08:00
|
|
|
|
//超过MTU则按照FU-A模式打包
|
2020-07-26 19:56:39 +08:00
|
|
|
|
if (len > max_rtp_size) {
|
2019-09-06 23:22:04 +08:00
|
|
|
|
//最高位bit为forbidden_zero_bit,
|
|
|
|
|
//后面2bit为nal_ref_idc(帧重要程度),00:可以丢,11:不能丢
|
|
|
|
|
//末尾5bit为nalu type,固定为28(FU-A)
|
2020-07-26 19:56:39 +08:00
|
|
|
|
unsigned char nal_fu_a = (*((unsigned char *) ptr) & (~0x1F)) | 28;
|
2019-09-06 23:22:04 +08:00
|
|
|
|
unsigned char s_e_r_flags;
|
2020-07-26 19:56:39 +08:00
|
|
|
|
bool fu_a_start = true;
|
|
|
|
|
bool mark_bit = false;
|
|
|
|
|
int offset = 1;
|
|
|
|
|
while (!mark_bit) {
|
|
|
|
|
if (len <= offset + max_rtp_size) {
|
2019-09-06 23:22:04 +08:00
|
|
|
|
//已经拆分结束
|
2020-07-26 19:56:39 +08:00
|
|
|
|
max_rtp_size = len - offset;
|
|
|
|
|
mark_bit = true;
|
2019-09-06 23:22:04 +08:00
|
|
|
|
//FU-A end
|
2020-07-26 19:56:39 +08:00
|
|
|
|
s_e_r_flags = (1 << 6) | nal_type;
|
|
|
|
|
} else if (fu_a_start) {
|
2019-09-06 23:22:04 +08:00
|
|
|
|
//FU-A start
|
2020-07-26 19:56:39 +08:00
|
|
|
|
s_e_r_flags = (1 << 7) | nal_type;
|
2018-10-21 21:45:44 +08:00
|
|
|
|
} else {
|
2019-09-06 23:22:04 +08:00
|
|
|
|
//FU-A mid
|
2020-07-26 19:56:39 +08:00
|
|
|
|
s_e_r_flags = nal_type;
|
2018-10-21 21:45:44 +08:00
|
|
|
|
}
|
2019-09-06 15:16:22 +08:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
//传入nullptr先不做payload的内存拷贝
|
2020-07-26 19:56:39 +08:00
|
|
|
|
auto rtp = makeRtp(getTrackType(), nullptr, max_rtp_size + 2, mark_bit, pts);
|
2019-09-06 15:16:22 +08:00
|
|
|
|
//rtp payload 负载部分
|
|
|
|
|
uint8_t *payload = (uint8_t*)rtp->data() + rtp->offset;
|
|
|
|
|
//FU-A 第1个字节
|
2020-07-26 19:56:39 +08:00
|
|
|
|
payload[0] = nal_fu_a;
|
2019-09-06 15:16:22 +08:00
|
|
|
|
//FU-A 第2个字节
|
2019-09-06 23:22:04 +08:00
|
|
|
|
payload[1] = s_e_r_flags;
|
2019-09-06 15:16:22 +08:00
|
|
|
|
//H264 数据
|
2020-07-26 19:56:39 +08:00
|
|
|
|
memcpy(payload + 2, (unsigned char *) ptr + offset, max_rtp_size);
|
2019-09-06 15:16:22 +08:00
|
|
|
|
//输入到rtp环形缓存
|
2020-07-26 19:56:39 +08:00
|
|
|
|
RtpCodec::inputRtp(rtp, fu_a_start && nal_type == H264Frame::NAL_IDR);
|
2019-09-06 15:16:22 +08:00
|
|
|
|
}
|
2020-07-26 19:56:39 +08:00
|
|
|
|
offset += max_rtp_size;
|
|
|
|
|
fu_a_start = false;
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-26 19:56:39 +08:00
|
|
|
|
void H264RtpEncoder::makeH264Rtp(const void* data, unsigned int len, bool mark, bool gop_pos, uint32_t uiStamp) {
|
|
|
|
|
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
|