ZLMediaKit/src/Extension/H264Rtp.cpp

297 lines
10 KiB
C++
Raw Normal View History

2018-10-25 10:00:17 +08:00
/*
* MIT License
*
2019-05-08 15:40:07 +08:00
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
* Copyright (c) 2019 <459502659@qq.com>
2018-10-25 10:00:17 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
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 forbidden_zero_bit :1;
unsigned nal_ref_idc :2;
unsigned type :5;
} NALU;
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 MakeNalu(uint8_t in, NALU &nal) {
2018-10-25 23:24:23 +08:00
nal.forbidden_zero_bit = in >> 7;
if (nal.forbidden_zero_bit) {
return false;
}
nal.nal_ref_idc = (in & 0x60) >> 5;
nal.type = in & 0x1f;
return true;
}
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();
2018-10-21 22:24:24 +08:00
frame->buffer.clear();
frame->iPrefixSize = 4;
return frame;
}
2018-10-23 18:39:17 +08:00
bool H264RtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
key_pos = decodeRtp(rtp);
RtpCodec::inputRtp(rtp, key_pos);
return key_pos;
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
*/
2019-08-30 11:17:27 +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 -
*/
2019-06-24 16:07:44 +08:00
const uint8_t *frame = (uint8_t *) rtppack->data() + rtppack->offset;
int length = rtppack->size() - rtppack->offset;
2018-10-21 21:21:14 +08:00
NALU nal;
MakeNalu(*frame, nal);
if (nal.type >= 0 && nal.type < 24) {
//a full frame
2018-10-24 15:43:52 +08:00
_h264frame->buffer.assign("\x0\x0\x0\x1", 4);
_h264frame->buffer.append((char *)frame, length);
2018-10-26 14:12:16 +08:00
_h264frame->timeStamp = rtppack->timeStamp;
auto key = _h264frame->keyFrame();
2018-10-24 15:43:52 +08:00
onGetH264(_h264frame);
return (key); //i frame
2018-10-21 21:21:14 +08:00
}
switch (nal.type){
case 24:{
// 24 STAP-A 单一时间的组合包
bool haveIDR = false;
auto ptr = frame + 1;
while(true){
int off = ptr - frame;
if (off >= length) {
break;
}
//获取当前nalu的大小
uint16_t len = *ptr++;
len <<= 8;
len |= *ptr++;
if (off + len > length) {
break;
}
if(len >= 10){
//过小的帧丢弃
NALU nal;
MakeNalu(ptr[0], nal);
_h264frame->buffer.assign("\x0\x0\x0\x1", 4);
_h264frame->buffer.append((char *)ptr, len);
_h264frame->timeStamp = rtppack->timeStamp;
if(nal.type == H264Frame::NAL_IDR){
haveIDR = true;
}
onGetH264(_h264frame);
}
ptr += len;
}
return haveIDR;
2018-10-21 21:21:14 +08:00
}
case 28:{
//FU-A
FU fu;
MakeFU(frame[1], fu);
if (fu.S) {
2019-08-30 11:17:27 +08:00
//该帧的第一个rtp包 FU-A start
char tmp = (nal.forbidden_zero_bit << 7 | nal.nal_ref_idc << 5 | fu.type);
_h264frame->buffer.assign("\x0\x0\x0\x1", 4);
_h264frame->buffer.push_back(tmp);
_h264frame->buffer.append((char *)frame + 2, length - 2);
_h264frame->timeStamp = rtppack->timeStamp;
//该函数return时保存下当前sequence,以便下次对比seq是否连续
_lastSeq = rtppack->sequence;
return _h264frame->keyFrame();
}
if (rtppack->sequence != _lastSeq + 1 && rtppack->sequence != 0) {
//中间的或末尾的rtp包其seq必须连续(如果回环了则判定为连续)否则说明rtp丢包那么该帧不完整必须得丢弃
_h264frame->buffer.clear();
WarnL << "rtp sequence不连续: " << rtppack->sequence << " != " << _lastSeq << " + 1,该帧被废弃";
return false;
}
if (!fu.E) {
2019-08-30 11:17:27 +08:00
//该帧的中间rtp包 FU-A mid
_h264frame->buffer.append((char *)frame + 2, length - 2);
//该函数return时保存下当前sequence,以便下次对比seq是否连续
_lastSeq = rtppack->sequence;
return false;
}
2019-08-30 11:17:27 +08:00
//该帧最后一个rtp包 FU-A end
_h264frame->buffer.append((char *)frame + 2, length - 2);
_h264frame->timeStamp = rtppack->timeStamp;
auto key = _h264frame->keyFrame();
onGetH264(_h264frame);
return key;
2018-10-21 21:21:14 +08:00
}
default:{
// 29 FU-B 单NAL单元B模式
// 25 STAP-B 单一时间的组合包
// 26 MTAP16 多个时间的组合包
// 27 MTAP24 多个时间的组合包
// 0 udef
// 30 udef
// 31 udef
WarnL << "不支持的rtp类型:" << (int)nal.type << " " << rtppack->sequence;
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) {
2018-10-21 21:27:05 +08:00
//写入环形缓存
2018-10-23 21:41:45 +08:00
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,
uint8_t ui8PlayloadType,
uint8_t ui8Interleaved) :
2018-10-23 11:38:56 +08:00
RtpInfo(ui32Ssrc,
ui32MtuSize,
ui32SampleRate,
ui8PlayloadType,
ui8Interleaved) {
2018-10-21 21:45:44 +08:00
}
2018-10-23 21:41:45 +08:00
void H264RtpEncoder::inputFrame(const Frame::Ptr &frame) {
RtpCodec::inputFrame(frame);
2018-10-21 21:45:44 +08:00
2019-05-28 17:14:36 +08:00
GET_CONFIG(uint32_t,cycleMS,Rtp::kCycleMS);
2018-10-23 21:41:45 +08:00
auto pcData = frame->data() + frame->prefixSize();
2018-10-21 21:45:44 +08:00
auto uiStamp = frame->stamp();
2018-10-21 22:24:24 +08:00
auto iLen = frame->size() - frame->prefixSize();
2019-09-06 23:22:04 +08:00
//获取NALU的5bit 帧类型
unsigned char naluType = H264_TYPE(pcData[0]);
2018-10-21 21:45:44 +08:00
uiStamp %= cycleMS;
2018-10-24 15:43:52 +08:00
int iSize = _ui32MtuSize - 2;
2019-09-06 23:22:04 +08:00
//超过MTU则按照FU-A模式打包
if (iLen > iSize) {
//最高位bit为forbidden_zero_bit,
//后面2bit为nal_ref_idc(帧重要程度),00:可以丢,11:不能丢
//末尾5bit为nalu type固定为28(FU-A)
unsigned char f_nri_flags = (*((unsigned char *) pcData) & 0x60) | 28;
unsigned char s_e_r_flags;
2018-10-21 21:45:44 +08:00
bool bFirst = true;
bool mark = false;
int nOffset = 1;
while (!mark) {
2019-09-06 23:22:04 +08:00
if (iLen < nOffset + iSize) {
//已经拆分结束
2018-10-21 21:45:44 +08:00
iSize = iLen - nOffset;
mark = true;
2019-09-06 23:22:04 +08:00
//FU-A end
s_e_r_flags = (1 << 6) | naluType;
} else if (bFirst) {
//FU-A start
s_e_r_flags = (1 << 7) | naluType;
2018-10-21 21:45:44 +08:00
} else {
2019-09-06 23:22:04 +08:00
//FU-A mid
s_e_r_flags = naluType;
2018-10-21 21:45:44 +08:00
}
2019-09-06 15:16:22 +08:00
{
//传入nullptr先不做payload的内存拷贝
auto rtp = makeRtp(getTrackType(), nullptr, iSize + 2, mark, uiStamp);
//rtp payload 负载部分
uint8_t *payload = (uint8_t*)rtp->data() + rtp->offset;
//FU-A 第1个字节
2019-09-06 23:22:04 +08:00
payload[0] = f_nri_flags;
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 数据
memcpy(payload + 2, (unsigned char *) pcData + nOffset, iSize);
//输入到rtp环形缓存
RtpCodec::inputRtp(rtp,bFirst && naluType == H264Frame::NAL_IDR);
}
2018-10-21 21:45:44 +08:00
nOffset += iSize;
2018-11-19 16:15:47 +08:00
bFirst = false;
2018-10-21 21:45:44 +08:00
}
} else {
2018-11-19 16:15:47 +08:00
makeH264Rtp(naluType,pcData, iLen, true, true, uiStamp);
2018-10-21 21:45:44 +08:00
}
}
2018-11-19 16:15:47 +08:00
void H264RtpEncoder::makeH264Rtp(int nal_type,const void* data, unsigned int len, bool mark, bool first_packet, uint32_t uiStamp) {
RtpCodec::inputRtp(makeRtp(getTrackType(),data,len,mark,uiStamp),first_packet && nal_type == H264Frame::NAL_IDR);
2018-10-24 17:17:55 +08:00
}
}//namespace mediakit